fix __builtin___clear_cache overrider fallout
[official-gcc.git] / gcc / lra-constraints.c
blob80ca1e06e3108f8a90ecbbac95456a3435a31b37
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 Need to extract memory from op for special memory constraint,
411 i.e. bcst_mem_operand in i386 backend. */
412 if (MEM_P (extract_mem_from_operand (op))
413 && (insn_extra_memory_constraint (constraint)
414 || insn_extra_special_memory_constraint (constraint))
415 && constraint_satisfied_p (op, constraint))
416 return true;
418 return valid_address_p (ad->mode, *ad->outer, ad->as);
421 /* For special_memory_operand, it could be false for MEM_P (op),
422 i.e. bcst_mem_operand in i386 backend.
423 Extract and return real memory operand or op. */
425 extract_mem_from_operand (rtx op)
427 for (rtx x = op;; x = XEXP (x, 0))
429 if (MEM_P (x))
430 return x;
431 if (GET_RTX_LENGTH (GET_CODE (x)) != 1
432 || GET_RTX_FORMAT (GET_CODE (x))[0] != 'e')
433 break;
435 return op;
438 /* Return true if the eliminated form of memory reference OP satisfies
439 extra (special) memory constraint CONSTRAINT. */
440 static bool
441 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
443 struct address_info ad;
444 rtx mem = extract_mem_from_operand (op);
445 if (!MEM_P (mem))
446 return false;
448 decompose_mem_address (&ad, mem);
449 address_eliminator eliminator (&ad);
450 return constraint_satisfied_p (op, constraint);
453 /* Return true if the eliminated form of address AD satisfies extra
454 address constraint CONSTRAINT. */
455 static bool
456 satisfies_address_constraint_p (struct address_info *ad,
457 enum constraint_num constraint)
459 address_eliminator eliminator (ad);
460 return constraint_satisfied_p (*ad->outer, constraint);
463 /* Return true if the eliminated form of address OP satisfies extra
464 address constraint CONSTRAINT. */
465 static bool
466 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
468 struct address_info ad;
470 decompose_lea_address (&ad, &op);
471 return satisfies_address_constraint_p (&ad, constraint);
474 /* Initiate equivalences for LRA. As we keep original equivalences
475 before any elimination, we need to make copies otherwise any change
476 in insns might change the equivalences. */
477 void
478 lra_init_equiv (void)
480 ira_expand_reg_equiv ();
481 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
483 rtx res;
485 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
486 ira_reg_equiv[i].memory = copy_rtx (res);
487 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
488 ira_reg_equiv[i].invariant = copy_rtx (res);
492 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
494 /* Update equivalence for REGNO. We need to this as the equivalence
495 might contain other pseudos which are changed by their
496 equivalences. */
497 static void
498 update_equiv (int regno)
500 rtx x;
502 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
503 ira_reg_equiv[regno].memory
504 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
505 NULL_RTX);
506 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
507 ira_reg_equiv[regno].invariant
508 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
509 NULL_RTX);
512 /* If we have decided to substitute X with another value, return that
513 value, otherwise return X. */
514 static rtx
515 get_equiv (rtx x)
517 int regno;
518 rtx res;
520 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
521 || ! ira_reg_equiv[regno].defined_p
522 || ! ira_reg_equiv[regno].profitable_p
523 || lra_get_regno_hard_regno (regno) >= 0)
524 return x;
525 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
527 if (targetm.cannot_substitute_mem_equiv_p (res))
528 return x;
529 return res;
531 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
532 return res;
533 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
534 return res;
535 gcc_unreachable ();
538 /* If we have decided to substitute X with the equivalent value,
539 return that value after elimination for INSN, otherwise return
540 X. */
541 static rtx
542 get_equiv_with_elimination (rtx x, rtx_insn *insn)
544 rtx res = get_equiv (x);
546 if (x == res || CONSTANT_P (res))
547 return res;
548 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
549 false, false, 0, true);
552 /* Set up curr_operand_mode. */
553 static void
554 init_curr_operand_mode (void)
556 int nop = curr_static_id->n_operands;
557 for (int i = 0; i < nop; i++)
559 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
560 if (mode == VOIDmode)
562 /* The .md mode for address operands is the mode of the
563 addressed value rather than the mode of the address itself. */
564 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
565 mode = Pmode;
566 else
567 mode = curr_static_id->operand[i].mode;
569 curr_operand_mode[i] = mode;
575 /* The page contains code to reuse input reloads. */
577 /* Structure describes input reload of the current insns. */
578 struct input_reload
580 /* True for input reload of matched operands. */
581 bool match_p;
582 /* Reloaded value. */
583 rtx input;
584 /* Reload pseudo used. */
585 rtx reg;
588 /* The number of elements in the following array. */
589 static int curr_insn_input_reloads_num;
590 /* Array containing info about input reloads. It is used to find the
591 same input reload and reuse the reload pseudo in this case. */
592 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
594 /* Initiate data concerning reuse of input reloads for the current
595 insn. */
596 static void
597 init_curr_insn_input_reloads (void)
599 curr_insn_input_reloads_num = 0;
602 /* The canonical form of an rtx inside a MEM is not necessarily the same as the
603 canonical form of the rtx outside the MEM. Fix this up in the case that
604 we're reloading an address (and therefore pulling it outside a MEM). */
605 static rtx
606 canonicalize_reload_addr (rtx addr)
608 subrtx_var_iterator::array_type array;
609 FOR_EACH_SUBRTX_VAR (iter, array, addr, NONCONST)
611 rtx x = *iter;
612 if (GET_CODE (x) == MULT && CONST_INT_P (XEXP (x, 1)))
614 const HOST_WIDE_INT ci = INTVAL (XEXP (x, 1));
615 const int pwr2 = exact_log2 (ci);
616 if (pwr2 > 0)
618 /* Rewrite this to use a shift instead, which is canonical when
619 outside of a MEM. */
620 PUT_CODE (x, ASHIFT);
621 XEXP (x, 1) = GEN_INT (pwr2);
626 return addr;
629 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse an existing
630 reload pseudo. Don't reuse an existing reload pseudo if IN_SUBREG_P
631 is true and the reused pseudo should be wrapped up in a SUBREG.
632 The result pseudo is returned through RESULT_REG. Return TRUE if we
633 created a new pseudo, FALSE if we reused an existing reload pseudo.
634 Use TITLE to describe new registers for debug purposes. */
635 static bool
636 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
637 enum reg_class rclass, bool in_subreg_p,
638 const char *title, rtx *result_reg)
640 int i, regno;
641 enum reg_class new_class;
642 bool unique_p = false;
644 if (type == OP_OUT)
646 /* Output reload registers tend to start out with a conservative
647 choice of register class. Usually this is ALL_REGS, although
648 a target might narrow it (for performance reasons) through
649 targetm.preferred_reload_class. It's therefore quite common
650 for a reload instruction to require a more restrictive class
651 than the class that was originally assigned to the reload register.
653 In these situations, it's more efficient to refine the choice
654 of register class rather than create a second reload register.
655 This also helps to avoid cycling for registers that are only
656 used by reload instructions. */
657 if (REG_P (original)
658 && (int) REGNO (original) >= new_regno_start
659 && INSN_UID (curr_insn) >= new_insn_uid_start
660 && in_class_p (original, rclass, &new_class, true))
662 unsigned int regno = REGNO (original);
663 if (lra_dump_file != NULL)
665 fprintf (lra_dump_file, " Reuse r%d for output ", regno);
666 dump_value_slim (lra_dump_file, original, 1);
668 if (new_class != lra_get_allocno_class (regno))
669 lra_change_class (regno, new_class, ", change to", false);
670 if (lra_dump_file != NULL)
671 fprintf (lra_dump_file, "\n");
672 *result_reg = original;
673 return false;
675 *result_reg
676 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
677 return true;
679 /* Prevent reuse value of expression with side effects,
680 e.g. volatile memory. */
681 if (! side_effects_p (original))
682 for (i = 0; i < curr_insn_input_reloads_num; i++)
684 if (! curr_insn_input_reloads[i].match_p
685 && rtx_equal_p (curr_insn_input_reloads[i].input, original)
686 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
688 rtx reg = curr_insn_input_reloads[i].reg;
689 regno = REGNO (reg);
690 /* If input is equal to original and both are VOIDmode,
691 GET_MODE (reg) might be still different from mode.
692 Ensure we don't return *result_reg with wrong mode. */
693 if (GET_MODE (reg) != mode)
695 if (in_subreg_p)
696 continue;
697 if (maybe_lt (GET_MODE_SIZE (GET_MODE (reg)),
698 GET_MODE_SIZE (mode)))
699 continue;
700 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
701 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
702 continue;
704 *result_reg = reg;
705 if (lra_dump_file != NULL)
707 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
708 dump_value_slim (lra_dump_file, original, 1);
710 if (new_class != lra_get_allocno_class (regno))
711 lra_change_class (regno, new_class, ", change to", false);
712 if (lra_dump_file != NULL)
713 fprintf (lra_dump_file, "\n");
714 return false;
716 /* If we have an input reload with a different mode, make sure it
717 will get a different hard reg. */
718 else if (REG_P (original)
719 && REG_P (curr_insn_input_reloads[i].input)
720 && REGNO (original) == REGNO (curr_insn_input_reloads[i].input)
721 && (GET_MODE (original)
722 != GET_MODE (curr_insn_input_reloads[i].input)))
723 unique_p = true;
725 *result_reg = (unique_p
726 ? lra_create_new_reg_with_unique_value
727 : lra_create_new_reg) (mode, original, rclass, title);
728 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
729 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
730 curr_insn_input_reloads[curr_insn_input_reloads_num].match_p = false;
731 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
732 return true;
736 /* The page contains major code to choose the current insn alternative
737 and generate reloads for it. */
739 /* Return the offset from REGNO of the least significant register
740 in (reg:MODE REGNO).
742 This function is used to tell whether two registers satisfy
743 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
745 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
746 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
748 lra_constraint_offset (int regno, machine_mode mode)
750 lra_assert (regno < FIRST_PSEUDO_REGISTER);
752 scalar_int_mode int_mode;
753 if (WORDS_BIG_ENDIAN
754 && is_a <scalar_int_mode> (mode, &int_mode)
755 && GET_MODE_SIZE (int_mode) > UNITS_PER_WORD)
756 return hard_regno_nregs (regno, mode) - 1;
757 return 0;
760 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
761 if they are the same hard reg, and has special hacks for
762 auto-increment and auto-decrement. This is specifically intended for
763 process_alt_operands to use in determining whether two operands
764 match. X is the operand whose number is the lower of the two.
766 It is supposed that X is the output operand and Y is the input
767 operand. Y_HARD_REGNO is the final hard regno of register Y or
768 register in subreg Y as we know it now. Otherwise, it is a
769 negative value. */
770 static bool
771 operands_match_p (rtx x, rtx y, int y_hard_regno)
773 int i;
774 RTX_CODE code = GET_CODE (x);
775 const char *fmt;
777 if (x == y)
778 return true;
779 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
780 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
782 int j;
784 i = get_hard_regno (x, false);
785 if (i < 0)
786 goto slow;
788 if ((j = y_hard_regno) < 0)
789 goto slow;
791 i += lra_constraint_offset (i, GET_MODE (x));
792 j += lra_constraint_offset (j, GET_MODE (y));
794 return i == j;
797 /* If two operands must match, because they are really a single
798 operand of an assembler insn, then two post-increments are invalid
799 because the assembler insn would increment only once. On the
800 other hand, a post-increment matches ordinary indexing if the
801 post-increment is the output operand. */
802 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
803 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
805 /* Two pre-increments are invalid because the assembler insn would
806 increment only once. On the other hand, a pre-increment matches
807 ordinary indexing if the pre-increment is the input operand. */
808 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
809 || GET_CODE (y) == PRE_MODIFY)
810 return operands_match_p (x, XEXP (y, 0), -1);
812 slow:
814 if (code == REG && REG_P (y))
815 return REGNO (x) == REGNO (y);
817 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
818 && x == SUBREG_REG (y))
819 return true;
820 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
821 && SUBREG_REG (x) == y)
822 return true;
824 /* Now we have disposed of all the cases in which different rtx
825 codes can match. */
826 if (code != GET_CODE (y))
827 return false;
829 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
830 if (GET_MODE (x) != GET_MODE (y))
831 return false;
833 switch (code)
835 CASE_CONST_UNIQUE:
836 return false;
838 case LABEL_REF:
839 return label_ref_label (x) == label_ref_label (y);
840 case SYMBOL_REF:
841 return XSTR (x, 0) == XSTR (y, 0);
843 default:
844 break;
847 /* Compare the elements. If any pair of corresponding elements fail
848 to match, return false for the whole things. */
850 fmt = GET_RTX_FORMAT (code);
851 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
853 int val, j;
854 switch (fmt[i])
856 case 'w':
857 if (XWINT (x, i) != XWINT (y, i))
858 return false;
859 break;
861 case 'i':
862 if (XINT (x, i) != XINT (y, i))
863 return false;
864 break;
866 case 'p':
867 if (maybe_ne (SUBREG_BYTE (x), SUBREG_BYTE (y)))
868 return false;
869 break;
871 case 'e':
872 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
873 if (val == 0)
874 return false;
875 break;
877 case '0':
878 break;
880 case 'E':
881 if (XVECLEN (x, i) != XVECLEN (y, i))
882 return false;
883 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
885 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
886 if (val == 0)
887 return false;
889 break;
891 /* It is believed that rtx's at this level will never
892 contain anything but integers and other rtx's, except for
893 within LABEL_REFs and SYMBOL_REFs. */
894 default:
895 gcc_unreachable ();
898 return true;
901 /* True if X is a constant that can be forced into the constant pool.
902 MODE is the mode of the operand, or VOIDmode if not known. */
903 #define CONST_POOL_OK_P(MODE, X) \
904 ((MODE) != VOIDmode \
905 && CONSTANT_P (X) \
906 && GET_CODE (X) != HIGH \
907 && GET_MODE_SIZE (MODE).is_constant () \
908 && !targetm.cannot_force_const_mem (MODE, X))
910 /* True if C is a non-empty register class that has too few registers
911 to be safely used as a reload target class. */
912 #define SMALL_REGISTER_CLASS_P(C) \
913 (ira_class_hard_regs_num [(C)] == 1 \
914 || (ira_class_hard_regs_num [(C)] >= 1 \
915 && targetm.class_likely_spilled_p (C)))
917 /* If REG is a reload pseudo, try to make its class satisfying CL. */
918 static void
919 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
921 enum reg_class rclass;
923 /* Do not make more accurate class from reloads generated. They are
924 mostly moves with a lot of constraints. Making more accurate
925 class may results in very narrow class and impossibility of find
926 registers for several reloads of one insn. */
927 if (INSN_UID (curr_insn) >= new_insn_uid_start)
928 return;
929 if (GET_CODE (reg) == SUBREG)
930 reg = SUBREG_REG (reg);
931 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
932 return;
933 if (in_class_p (reg, cl, &rclass) && rclass != cl)
934 lra_change_class (REGNO (reg), rclass, " Change to", true);
937 /* Searches X for any reference to a reg with the same value as REGNO,
938 returning the rtx of the reference found if any. Otherwise,
939 returns NULL_RTX. */
940 static rtx
941 regno_val_use_in (unsigned int regno, rtx x)
943 const char *fmt;
944 int i, j;
945 rtx tem;
947 if (REG_P (x) && lra_reg_info[REGNO (x)].val == lra_reg_info[regno].val)
948 return x;
950 fmt = GET_RTX_FORMAT (GET_CODE (x));
951 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
953 if (fmt[i] == 'e')
955 if ((tem = regno_val_use_in (regno, XEXP (x, i))))
956 return tem;
958 else if (fmt[i] == 'E')
959 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
960 if ((tem = regno_val_use_in (regno , XVECEXP (x, i, j))))
961 return tem;
964 return NULL_RTX;
967 /* Return true if all current insn non-output operands except INS (it
968 has a negaitve end marker) do not use pseudos with the same value
969 as REGNO. */
970 static bool
971 check_conflict_input_operands (int regno, signed char *ins)
973 int in;
974 int n_operands = curr_static_id->n_operands;
976 for (int nop = 0; nop < n_operands; nop++)
977 if (! curr_static_id->operand[nop].is_operator
978 && curr_static_id->operand[nop].type != OP_OUT)
980 for (int i = 0; (in = ins[i]) >= 0; i++)
981 if (in == nop)
982 break;
983 if (in < 0
984 && regno_val_use_in (regno, *curr_id->operand_loc[nop]) != NULL_RTX)
985 return false;
987 return true;
990 /* Generate reloads for matching OUT and INS (array of input operand
991 numbers with end marker -1) with reg class GOAL_CLASS, considering
992 output operands OUTS (similar array to INS) needing to be in different
993 registers. Add input and output reloads correspondingly to the lists
994 *BEFORE and *AFTER. OUT might be negative. In this case we generate
995 input reloads for matched input operands INS. EARLY_CLOBBER_P is a flag
996 that the output operand is early clobbered for chosen alternative. */
997 static void
998 match_reload (signed char out, signed char *ins, signed char *outs,
999 enum reg_class goal_class, rtx_insn **before,
1000 rtx_insn **after, bool early_clobber_p)
1002 bool out_conflict;
1003 int i, in;
1004 rtx new_in_reg, new_out_reg, reg;
1005 machine_mode inmode, outmode;
1006 rtx in_rtx = *curr_id->operand_loc[ins[0]];
1007 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
1009 inmode = curr_operand_mode[ins[0]];
1010 outmode = out < 0 ? inmode : curr_operand_mode[out];
1011 push_to_sequence (*before);
1012 if (inmode != outmode)
1014 /* process_alt_operands has already checked that the mode sizes
1015 are ordered. */
1016 if (partial_subreg_p (outmode, inmode))
1018 reg = new_in_reg
1019 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
1020 goal_class, "");
1021 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
1022 LRA_SUBREG_P (new_out_reg) = 1;
1023 /* If the input reg is dying here, we can use the same hard
1024 register for REG and IN_RTX. We do it only for original
1025 pseudos as reload pseudos can die although original
1026 pseudos still live where reload pseudos dies. */
1027 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
1028 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
1029 && (!early_clobber_p
1030 || check_conflict_input_operands(REGNO (in_rtx), ins)))
1031 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
1033 else
1035 reg = new_out_reg
1036 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
1037 goal_class, "");
1038 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
1039 /* NEW_IN_REG is non-paradoxical subreg. We don't want
1040 NEW_OUT_REG living above. We add clobber clause for
1041 this. This is just a temporary clobber. We can remove
1042 it at the end of LRA work. */
1043 rtx_insn *clobber = emit_clobber (new_out_reg);
1044 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
1045 LRA_SUBREG_P (new_in_reg) = 1;
1046 if (GET_CODE (in_rtx) == SUBREG)
1048 rtx subreg_reg = SUBREG_REG (in_rtx);
1050 /* If SUBREG_REG is dying here and sub-registers IN_RTX
1051 and NEW_IN_REG are similar, we can use the same hard
1052 register for REG and SUBREG_REG. */
1053 if (REG_P (subreg_reg)
1054 && (int) REGNO (subreg_reg) < lra_new_regno_start
1055 && GET_MODE (subreg_reg) == outmode
1056 && known_eq (SUBREG_BYTE (in_rtx), SUBREG_BYTE (new_in_reg))
1057 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg))
1058 && (! early_clobber_p
1059 || check_conflict_input_operands (REGNO (subreg_reg),
1060 ins)))
1061 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
1065 else
1067 /* Pseudos have values -- see comments for lra_reg_info.
1068 Different pseudos with the same value do not conflict even if
1069 they live in the same place. When we create a pseudo we
1070 assign value of original pseudo (if any) from which we
1071 created the new pseudo. If we create the pseudo from the
1072 input pseudo, the new pseudo will have no conflict with the
1073 input pseudo which is wrong when the input pseudo lives after
1074 the insn and as the new pseudo value is changed by the insn
1075 output. Therefore we create the new pseudo from the output
1076 except the case when we have single matched dying input
1077 pseudo.
1079 We cannot reuse the current output register because we might
1080 have a situation like "a <- a op b", where the constraints
1081 force the second input operand ("b") to match the output
1082 operand ("a"). "b" must then be copied into a new register
1083 so that it doesn't clobber the current value of "a".
1085 We cannot use the same value if the output pseudo is
1086 early clobbered or the input pseudo is mentioned in the
1087 output, e.g. as an address part in memory, because
1088 output reload will actually extend the pseudo liveness.
1089 We don't care about eliminable hard regs here as we are
1090 interesting only in pseudos. */
1092 /* Matching input's register value is the same as one of the other
1093 output operand. Output operands in a parallel insn must be in
1094 different registers. */
1095 out_conflict = false;
1096 if (REG_P (in_rtx))
1098 for (i = 0; outs[i] >= 0; i++)
1100 rtx other_out_rtx = *curr_id->operand_loc[outs[i]];
1101 if (REG_P (other_out_rtx)
1102 && (regno_val_use_in (REGNO (in_rtx), other_out_rtx)
1103 != NULL_RTX))
1105 out_conflict = true;
1106 break;
1111 new_in_reg = new_out_reg
1112 = (! early_clobber_p && ins[1] < 0 && REG_P (in_rtx)
1113 && (int) REGNO (in_rtx) < lra_new_regno_start
1114 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
1115 && (! early_clobber_p
1116 || check_conflict_input_operands (REGNO (in_rtx), ins))
1117 && (out < 0
1118 || regno_val_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
1119 && !out_conflict
1120 ? lra_create_new_reg (inmode, in_rtx, goal_class, "")
1121 : lra_create_new_reg_with_unique_value (outmode, out_rtx,
1122 goal_class, ""));
1124 /* In operand can be got from transformations before processing insn
1125 constraints. One example of such transformations is subreg
1126 reloading (see function simplify_operand_subreg). The new
1127 pseudos created by the transformations might have inaccurate
1128 class (ALL_REGS) and we should make their classes more
1129 accurate. */
1130 narrow_reload_pseudo_class (in_rtx, goal_class);
1131 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
1132 *before = get_insns ();
1133 end_sequence ();
1134 /* Add the new pseudo to consider values of subsequent input reload
1135 pseudos. */
1136 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
1137 curr_insn_input_reloads[curr_insn_input_reloads_num].input = in_rtx;
1138 curr_insn_input_reloads[curr_insn_input_reloads_num].match_p = true;
1139 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = new_in_reg;
1140 for (i = 0; (in = ins[i]) >= 0; i++)
1141 if (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
1142 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]))
1143 *curr_id->operand_loc[in] = new_in_reg;
1144 else
1146 lra_assert
1147 (GET_MODE (new_out_reg) == GET_MODE (*curr_id->operand_loc[in]));
1148 *curr_id->operand_loc[in] = new_out_reg;
1150 lra_update_dups (curr_id, ins);
1151 if (out < 0)
1152 return;
1153 /* See a comment for the input operand above. */
1154 narrow_reload_pseudo_class (out_rtx, goal_class);
1155 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
1157 reg = SUBREG_P (out_rtx) ? SUBREG_REG (out_rtx) : out_rtx;
1158 start_sequence ();
1159 /* If we had strict_low_part, use it also in reload to keep other
1160 parts unchanged but do it only for regs as strict_low_part
1161 has no sense for memory and probably there is no insn pattern
1162 to match the reload insn in memory case. */
1163 if (out >= 0 && curr_static_id->operand[out].strict_low && REG_P (reg))
1164 out_rtx = gen_rtx_STRICT_LOW_PART (VOIDmode, out_rtx);
1165 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
1166 emit_insn (*after);
1167 *after = get_insns ();
1168 end_sequence ();
1170 *curr_id->operand_loc[out] = new_out_reg;
1171 lra_update_dup (curr_id, out);
1174 /* Return register class which is union of all reg classes in insn
1175 constraint alternative string starting with P. */
1176 static enum reg_class
1177 reg_class_from_constraints (const char *p)
1179 int c, len;
1180 enum reg_class op_class = NO_REGS;
1183 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1185 case '#':
1186 case ',':
1187 return op_class;
1189 case 'g':
1190 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1191 break;
1193 default:
1194 enum constraint_num cn = lookup_constraint (p);
1195 enum reg_class cl = reg_class_for_constraint (cn);
1196 if (cl == NO_REGS)
1198 if (insn_extra_address_constraint (cn))
1199 op_class
1200 = (reg_class_subunion
1201 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1202 ADDRESS, SCRATCH)]);
1203 break;
1206 op_class = reg_class_subunion[op_class][cl];
1207 break;
1209 while ((p += len), c);
1210 return op_class;
1213 /* If OP is a register, return the class of the register as per
1214 get_reg_class, otherwise return NO_REGS. */
1215 static inline enum reg_class
1216 get_op_class (rtx op)
1218 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1221 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1222 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1223 SUBREG for VAL to make them equal. */
1224 static rtx_insn *
1225 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1227 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1229 /* Usually size of mem_pseudo is greater than val size but in
1230 rare cases it can be less as it can be defined by target
1231 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1232 if (! MEM_P (val))
1234 val = gen_lowpart_SUBREG (GET_MODE (mem_pseudo),
1235 GET_CODE (val) == SUBREG
1236 ? SUBREG_REG (val) : val);
1237 LRA_SUBREG_P (val) = 1;
1239 else
1241 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1242 LRA_SUBREG_P (mem_pseudo) = 1;
1245 return to_p ? gen_move_insn (mem_pseudo, val)
1246 : gen_move_insn (val, mem_pseudo);
1249 /* Process a special case insn (register move), return true if we
1250 don't need to process it anymore. INSN should be a single set
1251 insn. Set up that RTL was changed through CHANGE_P and that hook
1252 TARGET_SECONDARY_MEMORY_NEEDED says to use secondary memory through
1253 SEC_MEM_P. */
1254 static bool
1255 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1257 int sregno, dregno;
1258 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1259 rtx_insn *before;
1260 enum reg_class dclass, sclass, secondary_class;
1261 secondary_reload_info sri;
1263 lra_assert (curr_insn_set != NULL_RTX);
1264 dreg = dest = SET_DEST (curr_insn_set);
1265 sreg = src = SET_SRC (curr_insn_set);
1266 if (GET_CODE (dest) == SUBREG)
1267 dreg = SUBREG_REG (dest);
1268 if (GET_CODE (src) == SUBREG)
1269 sreg = SUBREG_REG (src);
1270 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1271 return false;
1272 sclass = dclass = NO_REGS;
1273 if (REG_P (dreg))
1274 dclass = get_reg_class (REGNO (dreg));
1275 gcc_assert (dclass < LIM_REG_CLASSES);
1276 if (dclass == ALL_REGS)
1277 /* ALL_REGS is used for new pseudos created by transformations
1278 like reload of SUBREG_REG (see function
1279 simplify_operand_subreg). We don't know their class yet. We
1280 should figure out the class from processing the insn
1281 constraints not in this fast path function. Even if ALL_REGS
1282 were a right class for the pseudo, secondary_... hooks usually
1283 are not define for ALL_REGS. */
1284 return false;
1285 if (REG_P (sreg))
1286 sclass = get_reg_class (REGNO (sreg));
1287 gcc_assert (sclass < LIM_REG_CLASSES);
1288 if (sclass == ALL_REGS)
1289 /* See comments above. */
1290 return false;
1291 if (sclass == NO_REGS && dclass == NO_REGS)
1292 return false;
1293 if (targetm.secondary_memory_needed (GET_MODE (src), sclass, dclass)
1294 && ((sclass != NO_REGS && dclass != NO_REGS)
1295 || (GET_MODE (src)
1296 != targetm.secondary_memory_needed_mode (GET_MODE (src)))))
1298 *sec_mem_p = true;
1299 return false;
1301 if (! REG_P (dreg) || ! REG_P (sreg))
1302 return false;
1303 sri.prev_sri = NULL;
1304 sri.icode = CODE_FOR_nothing;
1305 sri.extra_cost = 0;
1306 secondary_class = NO_REGS;
1307 /* Set up hard register for a reload pseudo for hook
1308 secondary_reload because some targets just ignore unassigned
1309 pseudos in the hook. */
1310 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1312 dregno = REGNO (dreg);
1313 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1315 else
1316 dregno = -1;
1317 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1319 sregno = REGNO (sreg);
1320 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1322 else
1323 sregno = -1;
1324 if (sclass != NO_REGS)
1325 secondary_class
1326 = (enum reg_class) targetm.secondary_reload (false, dest,
1327 (reg_class_t) sclass,
1328 GET_MODE (src), &sri);
1329 if (sclass == NO_REGS
1330 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1331 && dclass != NO_REGS))
1333 enum reg_class old_sclass = secondary_class;
1334 secondary_reload_info old_sri = sri;
1336 sri.prev_sri = NULL;
1337 sri.icode = CODE_FOR_nothing;
1338 sri.extra_cost = 0;
1339 secondary_class
1340 = (enum reg_class) targetm.secondary_reload (true, src,
1341 (reg_class_t) dclass,
1342 GET_MODE (src), &sri);
1343 /* Check the target hook consistency. */
1344 lra_assert
1345 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1346 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1347 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1349 if (sregno >= 0)
1350 reg_renumber [sregno] = -1;
1351 if (dregno >= 0)
1352 reg_renumber [dregno] = -1;
1353 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1354 return false;
1355 *change_p = true;
1356 new_reg = NULL_RTX;
1357 if (secondary_class != NO_REGS)
1358 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1359 secondary_class,
1360 "secondary");
1361 start_sequence ();
1362 if (sri.icode == CODE_FOR_nothing)
1363 lra_emit_move (new_reg, src);
1364 else
1366 enum reg_class scratch_class;
1368 scratch_class = (reg_class_from_constraints
1369 (insn_data[sri.icode].operand[2].constraint));
1370 scratch_reg = (lra_create_new_reg_with_unique_value
1371 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1372 scratch_class, "scratch"));
1373 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1374 src, scratch_reg));
1376 before = get_insns ();
1377 end_sequence ();
1378 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1379 if (new_reg != NULL_RTX)
1380 SET_SRC (curr_insn_set) = new_reg;
1381 else
1383 if (lra_dump_file != NULL)
1385 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1386 dump_insn_slim (lra_dump_file, curr_insn);
1388 lra_set_insn_deleted (curr_insn);
1389 return true;
1391 return false;
1394 /* The following data describe the result of process_alt_operands.
1395 The data are used in curr_insn_transform to generate reloads. */
1397 /* The chosen reg classes which should be used for the corresponding
1398 operands. */
1399 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1400 /* True if the operand should be the same as another operand and that
1401 other operand does not need a reload. */
1402 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1403 /* True if the operand does not need a reload. */
1404 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1405 /* True if the operand can be offsetable memory. */
1406 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1407 /* The number of an operand to which given operand can be matched to. */
1408 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1409 /* The number of elements in the following array. */
1410 static int goal_alt_dont_inherit_ops_num;
1411 /* Numbers of operands whose reload pseudos should not be inherited. */
1412 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1413 /* True if the insn commutative operands should be swapped. */
1414 static bool goal_alt_swapped;
1415 /* The chosen insn alternative. */
1416 static int goal_alt_number;
1418 /* True if the corresponding operand is the result of an equivalence
1419 substitution. */
1420 static bool equiv_substition_p[MAX_RECOG_OPERANDS];
1422 /* The following five variables are used to choose the best insn
1423 alternative. They reflect final characteristics of the best
1424 alternative. */
1426 /* Number of necessary reloads and overall cost reflecting the
1427 previous value and other unpleasantness of the best alternative. */
1428 static int best_losers, best_overall;
1429 /* Overall number hard registers used for reloads. For example, on
1430 some targets we need 2 general registers to reload DFmode and only
1431 one floating point register. */
1432 static int best_reload_nregs;
1433 /* Overall number reflecting distances of previous reloading the same
1434 value. The distances are counted from the current BB start. It is
1435 used to improve inheritance chances. */
1436 static int best_reload_sum;
1438 /* True if the current insn should have no correspondingly input or
1439 output reloads. */
1440 static bool no_input_reloads_p, no_output_reloads_p;
1442 /* True if we swapped the commutative operands in the current
1443 insn. */
1444 static int curr_swapped;
1446 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1447 register of class CL. Add any input reloads to list BEFORE. AFTER
1448 is nonnull if *LOC is an automodified value; handle that case by
1449 adding the required output reloads to list AFTER. Return true if
1450 the RTL was changed.
1452 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1453 register. Return false if the address register is correct. */
1454 static bool
1455 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1456 enum reg_class cl)
1458 int regno;
1459 enum reg_class rclass, new_class;
1460 rtx reg;
1461 rtx new_reg;
1462 machine_mode mode;
1463 bool subreg_p, before_p = false;
1465 subreg_p = GET_CODE (*loc) == SUBREG;
1466 if (subreg_p)
1468 reg = SUBREG_REG (*loc);
1469 mode = GET_MODE (reg);
1471 /* For mode with size bigger than ptr_mode, there unlikely to be "mov"
1472 between two registers with different classes, but there normally will
1473 be "mov" which transfers element of vector register into the general
1474 register, and this normally will be a subreg which should be reloaded
1475 as a whole. This is particularly likely to be triggered when
1476 -fno-split-wide-types specified. */
1477 if (!REG_P (reg)
1478 || in_class_p (reg, cl, &new_class)
1479 || known_le (GET_MODE_SIZE (mode), GET_MODE_SIZE (ptr_mode)))
1480 loc = &SUBREG_REG (*loc);
1483 reg = *loc;
1484 mode = GET_MODE (reg);
1485 if (! REG_P (reg))
1487 if (check_only_p)
1488 return true;
1489 /* Always reload memory in an address even if the target supports
1490 such addresses. */
1491 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1492 before_p = true;
1494 else
1496 regno = REGNO (reg);
1497 rclass = get_reg_class (regno);
1498 if (! check_only_p
1499 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1501 if (lra_dump_file != NULL)
1503 fprintf (lra_dump_file,
1504 "Changing pseudo %d in address of insn %u on equiv ",
1505 REGNO (reg), INSN_UID (curr_insn));
1506 dump_value_slim (lra_dump_file, *loc, 1);
1507 fprintf (lra_dump_file, "\n");
1509 *loc = copy_rtx (*loc);
1511 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1513 if (check_only_p)
1514 return true;
1515 reg = *loc;
1516 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1517 mode, reg, cl, subreg_p, "address", &new_reg))
1518 before_p = true;
1520 else if (new_class != NO_REGS && rclass != new_class)
1522 if (check_only_p)
1523 return true;
1524 lra_change_class (regno, new_class, " Change to", true);
1525 return false;
1527 else
1528 return false;
1530 if (before_p)
1532 push_to_sequence (*before);
1533 lra_emit_move (new_reg, reg);
1534 *before = get_insns ();
1535 end_sequence ();
1537 *loc = new_reg;
1538 if (after != NULL)
1540 start_sequence ();
1541 lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1542 emit_insn (*after);
1543 *after = get_insns ();
1544 end_sequence ();
1546 return true;
1549 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1550 the insn to be inserted before curr insn. AFTER returns the
1551 the insn to be inserted after curr insn. ORIGREG and NEWREG
1552 are the original reg and new reg for reload. */
1553 static void
1554 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1555 rtx newreg)
1557 if (before)
1559 push_to_sequence (*before);
1560 lra_emit_move (newreg, origreg);
1561 *before = get_insns ();
1562 end_sequence ();
1564 if (after)
1566 start_sequence ();
1567 lra_emit_move (origreg, newreg);
1568 emit_insn (*after);
1569 *after = get_insns ();
1570 end_sequence ();
1574 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1575 static bool process_address (int, bool, rtx_insn **, rtx_insn **);
1577 /* Make reloads for subreg in operand NOP with internal subreg mode
1578 REG_MODE, add new reloads for further processing. Return true if
1579 any change was done. */
1580 static bool
1581 simplify_operand_subreg (int nop, machine_mode reg_mode)
1583 int hard_regno, inner_hard_regno;
1584 rtx_insn *before, *after;
1585 machine_mode mode, innermode;
1586 rtx reg, new_reg;
1587 rtx operand = *curr_id->operand_loc[nop];
1588 enum reg_class regclass;
1589 enum op_type type;
1591 before = after = NULL;
1593 if (GET_CODE (operand) != SUBREG)
1594 return false;
1596 mode = GET_MODE (operand);
1597 reg = SUBREG_REG (operand);
1598 innermode = GET_MODE (reg);
1599 type = curr_static_id->operand[nop].type;
1600 if (MEM_P (reg))
1602 const bool addr_was_valid
1603 = valid_address_p (innermode, XEXP (reg, 0), MEM_ADDR_SPACE (reg));
1604 alter_subreg (curr_id->operand_loc[nop], false);
1605 rtx subst = *curr_id->operand_loc[nop];
1606 lra_assert (MEM_P (subst));
1607 const bool addr_is_valid = valid_address_p (GET_MODE (subst),
1608 XEXP (subst, 0),
1609 MEM_ADDR_SPACE (subst));
1610 if (!addr_was_valid
1611 || addr_is_valid
1612 || ((get_constraint_type (lookup_constraint
1613 (curr_static_id->operand[nop].constraint))
1614 != CT_SPECIAL_MEMORY)
1615 /* We still can reload address and if the address is
1616 valid, we can remove subreg without reloading its
1617 inner memory. */
1618 && valid_address_p (GET_MODE (subst),
1619 regno_reg_rtx
1620 [ira_class_hard_regs
1621 [base_reg_class (GET_MODE (subst),
1622 MEM_ADDR_SPACE (subst),
1623 ADDRESS, SCRATCH)][0]],
1624 MEM_ADDR_SPACE (subst))))
1626 /* If we change the address for a paradoxical subreg of memory, the
1627 new address might violate the necessary alignment or the access
1628 might be slow; take this into consideration. We need not worry
1629 about accesses beyond allocated memory for paradoxical memory
1630 subregs as we don't substitute such equiv memory (see processing
1631 equivalences in function lra_constraints) and because for spilled
1632 pseudos we allocate stack memory enough for the biggest
1633 corresponding paradoxical subreg.
1635 However, do not blindly simplify a (subreg (mem ...)) for
1636 WORD_REGISTER_OPERATIONS targets as this may lead to loading junk
1637 data into a register when the inner is narrower than outer or
1638 missing important data from memory when the inner is wider than
1639 outer. This rule only applies to modes that are no wider than
1640 a word.
1642 If valid memory becomes invalid after subreg elimination
1643 and address might be different we still have to reload
1644 memory.
1646 if ((! addr_was_valid
1647 || addr_is_valid
1648 || known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (innermode)))
1649 && !(maybe_ne (GET_MODE_PRECISION (mode),
1650 GET_MODE_PRECISION (innermode))
1651 && known_le (GET_MODE_SIZE (mode), UNITS_PER_WORD)
1652 && known_le (GET_MODE_SIZE (innermode), UNITS_PER_WORD)
1653 && WORD_REGISTER_OPERATIONS)
1654 && (!(MEM_ALIGN (subst) < GET_MODE_ALIGNMENT (mode)
1655 && targetm.slow_unaligned_access (mode, MEM_ALIGN (subst)))
1656 || (MEM_ALIGN (reg) < GET_MODE_ALIGNMENT (innermode)
1657 && targetm.slow_unaligned_access (innermode,
1658 MEM_ALIGN (reg)))))
1659 return true;
1661 *curr_id->operand_loc[nop] = operand;
1663 /* But if the address was not valid, we cannot reload the MEM without
1664 reloading the address first. */
1665 if (!addr_was_valid)
1666 process_address (nop, false, &before, &after);
1668 /* INNERMODE is fast, MODE slow. Reload the mem in INNERMODE. */
1669 enum reg_class rclass
1670 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1671 if (get_reload_reg (curr_static_id->operand[nop].type, innermode,
1672 reg, rclass, TRUE, "slow/invalid mem", &new_reg))
1674 bool insert_before, insert_after;
1675 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1677 insert_before = (type != OP_OUT
1678 || partial_subreg_p (mode, innermode));
1679 insert_after = type != OP_IN;
1680 insert_move_for_subreg (insert_before ? &before : NULL,
1681 insert_after ? &after : NULL,
1682 reg, new_reg);
1684 SUBREG_REG (operand) = new_reg;
1686 /* Convert to MODE. */
1687 reg = operand;
1688 rclass
1689 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1690 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1691 rclass, TRUE, "slow/invalid mem", &new_reg))
1693 bool insert_before, insert_after;
1694 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1696 insert_before = type != OP_OUT;
1697 insert_after = type != OP_IN;
1698 insert_move_for_subreg (insert_before ? &before : NULL,
1699 insert_after ? &after : NULL,
1700 reg, new_reg);
1702 *curr_id->operand_loc[nop] = new_reg;
1703 lra_process_new_insns (curr_insn, before, after,
1704 "Inserting slow/invalid mem reload");
1705 return true;
1708 /* If the address was valid and became invalid, prefer to reload
1709 the memory. Typical case is when the index scale should
1710 correspond the memory. */
1711 *curr_id->operand_loc[nop] = operand;
1712 /* Do not return false here as the MEM_P (reg) will be processed
1713 later in this function. */
1715 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1717 alter_subreg (curr_id->operand_loc[nop], false);
1718 return true;
1720 else if (CONSTANT_P (reg))
1722 /* Try to simplify subreg of constant. It is usually result of
1723 equivalence substitution. */
1724 if (innermode == VOIDmode
1725 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1726 innermode = curr_static_id->operand[nop].mode;
1727 if ((new_reg = simplify_subreg (mode, reg, innermode,
1728 SUBREG_BYTE (operand))) != NULL_RTX)
1730 *curr_id->operand_loc[nop] = new_reg;
1731 return true;
1734 /* Put constant into memory when we have mixed modes. It generates
1735 a better code in most cases as it does not need a secondary
1736 reload memory. It also prevents LRA looping when LRA is using
1737 secondary reload memory again and again. */
1738 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1739 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1741 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1742 alter_subreg (curr_id->operand_loc[nop], false);
1743 return true;
1745 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1746 if there may be a problem accessing OPERAND in the outer
1747 mode. */
1748 if ((REG_P (reg)
1749 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1750 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1751 /* Don't reload paradoxical subregs because we could be looping
1752 having repeatedly final regno out of hard regs range. */
1753 && (hard_regno_nregs (hard_regno, innermode)
1754 >= hard_regno_nregs (hard_regno, mode))
1755 && simplify_subreg_regno (hard_regno, innermode,
1756 SUBREG_BYTE (operand), mode) < 0
1757 /* Don't reload subreg for matching reload. It is actually
1758 valid subreg in LRA. */
1759 && ! LRA_SUBREG_P (operand))
1760 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1762 enum reg_class rclass;
1764 if (REG_P (reg))
1765 /* There is a big probability that we will get the same class
1766 for the new pseudo and we will get the same insn which
1767 means infinite looping. So spill the new pseudo. */
1768 rclass = NO_REGS;
1769 else
1770 /* The class will be defined later in curr_insn_transform. */
1771 rclass
1772 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1774 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1775 rclass, TRUE, "subreg reg", &new_reg))
1777 bool insert_before, insert_after;
1778 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1780 insert_before = (type != OP_OUT
1781 || read_modify_subreg_p (operand));
1782 insert_after = (type != OP_IN);
1783 insert_move_for_subreg (insert_before ? &before : NULL,
1784 insert_after ? &after : NULL,
1785 reg, new_reg);
1787 SUBREG_REG (operand) = new_reg;
1788 lra_process_new_insns (curr_insn, before, after,
1789 "Inserting subreg reload");
1790 return true;
1792 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1793 IRA allocates hardreg to the inner pseudo reg according to its mode
1794 instead of the outermode, so the size of the hardreg may not be enough
1795 to contain the outermode operand, in that case we may need to insert
1796 reload for the reg. For the following two types of paradoxical subreg,
1797 we need to insert reload:
1798 1. If the op_type is OP_IN, and the hardreg could not be paired with
1799 other hardreg to contain the outermode operand
1800 (checked by in_hard_reg_set_p), we need to insert the reload.
1801 2. If the op_type is OP_OUT or OP_INOUT.
1803 Here is a paradoxical subreg example showing how the reload is generated:
1805 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1806 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1808 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1809 here, if reg107 is assigned to hardreg R15, because R15 is the last
1810 hardreg, compiler cannot find another hardreg to pair with R15 to
1811 contain TImode data. So we insert a TImode reload reg180 for it.
1812 After reload is inserted:
1814 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1815 (reg:DI 107 [ __comp ])) -1
1816 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1817 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1819 Two reload hard registers will be allocated to reg180 to save TImode data
1820 in LRA_assign.
1822 For LRA pseudos this should normally be handled by the biggest_mode
1823 mechanism. However, it's possible for new uses of an LRA pseudo
1824 to be introduced after we've allocated it, such as when undoing
1825 inheritance, and the allocated register might not then be appropriate
1826 for the new uses. */
1827 else if (REG_P (reg)
1828 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1829 && paradoxical_subreg_p (operand)
1830 && (inner_hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1831 && ((hard_regno
1832 = simplify_subreg_regno (inner_hard_regno, innermode,
1833 SUBREG_BYTE (operand), mode)) < 0
1834 || ((hard_regno_nregs (inner_hard_regno, innermode)
1835 < hard_regno_nregs (hard_regno, mode))
1836 && (regclass = lra_get_allocno_class (REGNO (reg)))
1837 && (type != OP_IN
1838 || !in_hard_reg_set_p (reg_class_contents[regclass],
1839 mode, hard_regno)
1840 || overlaps_hard_reg_set_p (lra_no_alloc_regs,
1841 mode, hard_regno)))))
1843 /* The class will be defined later in curr_insn_transform. */
1844 enum reg_class rclass
1845 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1847 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1848 rclass, TRUE, "paradoxical subreg", &new_reg))
1850 rtx subreg;
1851 bool insert_before, insert_after;
1853 PUT_MODE (new_reg, mode);
1854 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1855 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1857 insert_before = (type != OP_OUT);
1858 insert_after = (type != OP_IN);
1859 insert_move_for_subreg (insert_before ? &before : NULL,
1860 insert_after ? &after : NULL,
1861 reg, subreg);
1863 SUBREG_REG (operand) = new_reg;
1864 lra_process_new_insns (curr_insn, before, after,
1865 "Inserting paradoxical subreg reload");
1866 return true;
1868 return false;
1871 /* Return TRUE if X refers for a hard register from SET. */
1872 static bool
1873 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1875 int i, j, x_hard_regno;
1876 machine_mode mode;
1877 const char *fmt;
1878 enum rtx_code code;
1880 if (x == NULL_RTX)
1881 return false;
1882 code = GET_CODE (x);
1883 mode = GET_MODE (x);
1885 if (code == SUBREG)
1887 /* For all SUBREGs we want to check whether the full multi-register
1888 overlaps the set. For normal SUBREGs this means 'get_hard_regno' of
1889 the inner register, for paradoxical SUBREGs this means the
1890 'get_hard_regno' of the full SUBREG and for complete SUBREGs either is
1891 fine. Use the wider mode for all cases. */
1892 rtx subreg = SUBREG_REG (x);
1893 mode = wider_subreg_mode (x);
1894 if (mode == GET_MODE (subreg))
1896 x = subreg;
1897 code = GET_CODE (x);
1901 if (REG_P (x) || SUBREG_P (x))
1903 x_hard_regno = get_hard_regno (x, true);
1904 return (x_hard_regno >= 0
1905 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1907 if (MEM_P (x))
1909 struct address_info ad;
1911 decompose_mem_address (&ad, x);
1912 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1913 return true;
1914 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1915 return true;
1917 fmt = GET_RTX_FORMAT (code);
1918 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1920 if (fmt[i] == 'e')
1922 if (uses_hard_regs_p (XEXP (x, i), set))
1923 return true;
1925 else if (fmt[i] == 'E')
1927 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1928 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1929 return true;
1932 return false;
1935 /* Return true if OP is a spilled pseudo. */
1936 static inline bool
1937 spilled_pseudo_p (rtx op)
1939 return (REG_P (op)
1940 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1943 /* Return true if X is a general constant. */
1944 static inline bool
1945 general_constant_p (rtx x)
1947 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1950 static bool
1951 reg_in_class_p (rtx reg, enum reg_class cl)
1953 if (cl == NO_REGS)
1954 return get_reg_class (REGNO (reg)) == NO_REGS;
1955 return in_class_p (reg, cl, NULL);
1958 /* Return true if SET of RCLASS contains no hard regs which can be
1959 used in MODE. */
1960 static bool
1961 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1962 HARD_REG_SET &set,
1963 machine_mode mode)
1965 HARD_REG_SET temp;
1967 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1968 temp = set & ~lra_no_alloc_regs;
1969 return (hard_reg_set_subset_p
1970 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1974 /* Used to check validity info about small class input operands. It
1975 should be incremented at start of processing an insn
1976 alternative. */
1977 static unsigned int curr_small_class_check = 0;
1979 /* Update number of used inputs of class OP_CLASS for operand NOP
1980 of alternative NALT. Return true if we have more such class operands
1981 than the number of available regs. */
1982 static bool
1983 update_and_check_small_class_inputs (int nop, int nalt,
1984 enum reg_class op_class)
1986 static unsigned int small_class_check[LIM_REG_CLASSES];
1987 static int small_class_input_nums[LIM_REG_CLASSES];
1989 if (SMALL_REGISTER_CLASS_P (op_class)
1990 /* We are interesting in classes became small because of fixing
1991 some hard regs, e.g. by an user through GCC options. */
1992 && hard_reg_set_intersect_p (reg_class_contents[op_class],
1993 ira_no_alloc_regs)
1994 && (curr_static_id->operand[nop].type != OP_OUT
1995 || TEST_BIT (curr_static_id->operand[nop].early_clobber_alts, nalt)))
1997 if (small_class_check[op_class] == curr_small_class_check)
1998 small_class_input_nums[op_class]++;
1999 else
2001 small_class_check[op_class] = curr_small_class_check;
2002 small_class_input_nums[op_class] = 1;
2004 if (small_class_input_nums[op_class] > ira_class_hard_regs_num[op_class])
2005 return true;
2007 return false;
2010 /* Major function to choose the current insn alternative and what
2011 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
2012 negative we should consider only this alternative. Return false if
2013 we cannot choose the alternative or find how to reload the
2014 operands. */
2015 static bool
2016 process_alt_operands (int only_alternative)
2018 bool ok_p = false;
2019 int nop, overall, nalt;
2020 int n_alternatives = curr_static_id->n_alternatives;
2021 int n_operands = curr_static_id->n_operands;
2022 /* LOSERS counts the operands that don't fit this alternative and
2023 would require loading. */
2024 int losers;
2025 int addr_losers;
2026 /* REJECT is a count of how undesirable this alternative says it is
2027 if any reloading is required. If the alternative matches exactly
2028 then REJECT is ignored, but otherwise it gets this much counted
2029 against it in addition to the reloading needed. */
2030 int reject;
2031 /* This is defined by '!' or '?' alternative constraint and added to
2032 reject. But in some cases it can be ignored. */
2033 int static_reject;
2034 int op_reject;
2035 /* The number of elements in the following array. */
2036 int early_clobbered_regs_num;
2037 /* Numbers of operands which are early clobber registers. */
2038 int early_clobbered_nops[MAX_RECOG_OPERANDS];
2039 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
2040 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
2041 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
2042 bool curr_alt_win[MAX_RECOG_OPERANDS];
2043 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
2044 int curr_alt_matches[MAX_RECOG_OPERANDS];
2045 /* The number of elements in the following array. */
2046 int curr_alt_dont_inherit_ops_num;
2047 /* Numbers of operands whose reload pseudos should not be inherited. */
2048 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
2049 rtx op;
2050 /* The register when the operand is a subreg of register, otherwise the
2051 operand itself. */
2052 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
2053 /* The register if the operand is a register or subreg of register,
2054 otherwise NULL. */
2055 rtx operand_reg[MAX_RECOG_OPERANDS];
2056 int hard_regno[MAX_RECOG_OPERANDS];
2057 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
2058 int reload_nregs, reload_sum;
2059 bool costly_p;
2060 enum reg_class cl;
2062 /* Calculate some data common for all alternatives to speed up the
2063 function. */
2064 for (nop = 0; nop < n_operands; nop++)
2066 rtx reg;
2068 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
2069 /* The real hard regno of the operand after the allocation. */
2070 hard_regno[nop] = get_hard_regno (op, true);
2072 operand_reg[nop] = reg = op;
2073 biggest_mode[nop] = GET_MODE (op);
2074 if (GET_CODE (op) == SUBREG)
2076 biggest_mode[nop] = wider_subreg_mode (op);
2077 operand_reg[nop] = reg = SUBREG_REG (op);
2079 if (! REG_P (reg))
2080 operand_reg[nop] = NULL_RTX;
2081 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
2082 || ((int) REGNO (reg)
2083 == lra_get_elimination_hard_regno (REGNO (reg))))
2084 no_subreg_reg_operand[nop] = reg;
2085 else
2086 operand_reg[nop] = no_subreg_reg_operand[nop]
2087 /* Just use natural mode for elimination result. It should
2088 be enough for extra constraints hooks. */
2089 = regno_reg_rtx[hard_regno[nop]];
2092 /* The constraints are made of several alternatives. Each operand's
2093 constraint looks like foo,bar,... with commas separating the
2094 alternatives. The first alternatives for all operands go
2095 together, the second alternatives go together, etc.
2097 First loop over alternatives. */
2098 alternative_mask preferred = curr_id->preferred_alternatives;
2099 if (only_alternative >= 0)
2100 preferred &= ALTERNATIVE_BIT (only_alternative);
2102 for (nalt = 0; nalt < n_alternatives; nalt++)
2104 /* Loop over operands for one constraint alternative. */
2105 if (!TEST_BIT (preferred, nalt))
2106 continue;
2108 bool matching_early_clobber[MAX_RECOG_OPERANDS];
2109 curr_small_class_check++;
2110 overall = losers = addr_losers = 0;
2111 static_reject = reject = reload_nregs = reload_sum = 0;
2112 for (nop = 0; nop < n_operands; nop++)
2114 int inc = (curr_static_id
2115 ->operand_alternative[nalt * n_operands + nop].reject);
2116 if (lra_dump_file != NULL && inc != 0)
2117 fprintf (lra_dump_file,
2118 " Staticly defined alt reject+=%d\n", inc);
2119 static_reject += inc;
2120 matching_early_clobber[nop] = 0;
2122 reject += static_reject;
2123 early_clobbered_regs_num = 0;
2125 for (nop = 0; nop < n_operands; nop++)
2127 const char *p;
2128 char *end;
2129 int len, c, m, i, opalt_num, this_alternative_matches;
2130 bool win, did_match, offmemok, early_clobber_p;
2131 /* false => this operand can be reloaded somehow for this
2132 alternative. */
2133 bool badop;
2134 /* true => this operand can be reloaded if the alternative
2135 allows regs. */
2136 bool winreg;
2137 /* True if a constant forced into memory would be OK for
2138 this operand. */
2139 bool constmemok;
2140 enum reg_class this_alternative, this_costly_alternative;
2141 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
2142 bool this_alternative_match_win, this_alternative_win;
2143 bool this_alternative_offmemok;
2144 bool scratch_p;
2145 machine_mode mode;
2146 enum constraint_num cn;
2148 opalt_num = nalt * n_operands + nop;
2149 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
2151 /* Fast track for no constraints at all. */
2152 curr_alt[nop] = NO_REGS;
2153 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
2154 curr_alt_win[nop] = true;
2155 curr_alt_match_win[nop] = false;
2156 curr_alt_offmemok[nop] = false;
2157 curr_alt_matches[nop] = -1;
2158 continue;
2161 op = no_subreg_reg_operand[nop];
2162 mode = curr_operand_mode[nop];
2164 win = did_match = winreg = offmemok = constmemok = false;
2165 badop = true;
2167 early_clobber_p = false;
2168 p = curr_static_id->operand_alternative[opalt_num].constraint;
2170 this_costly_alternative = this_alternative = NO_REGS;
2171 /* We update set of possible hard regs besides its class
2172 because reg class might be inaccurate. For example,
2173 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
2174 is translated in HI_REGS because classes are merged by
2175 pairs and there is no accurate intermediate class. */
2176 CLEAR_HARD_REG_SET (this_alternative_set);
2177 CLEAR_HARD_REG_SET (this_costly_alternative_set);
2178 this_alternative_win = false;
2179 this_alternative_match_win = false;
2180 this_alternative_offmemok = false;
2181 this_alternative_matches = -1;
2183 /* An empty constraint should be excluded by the fast
2184 track. */
2185 lra_assert (*p != 0 && *p != ',');
2187 op_reject = 0;
2188 /* Scan this alternative's specs for this operand; set WIN
2189 if the operand fits any letter in this alternative.
2190 Otherwise, clear BADOP if this operand could fit some
2191 letter after reloads, or set WINREG if this operand could
2192 fit after reloads provided the constraint allows some
2193 registers. */
2194 costly_p = false;
2197 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
2199 case '\0':
2200 len = 0;
2201 break;
2202 case ',':
2203 c = '\0';
2204 break;
2206 case '&':
2207 early_clobber_p = true;
2208 break;
2210 case '$':
2211 op_reject += LRA_MAX_REJECT;
2212 break;
2213 case '^':
2214 op_reject += LRA_LOSER_COST_FACTOR;
2215 break;
2217 case '#':
2218 /* Ignore rest of this alternative. */
2219 c = '\0';
2220 break;
2222 case '0': case '1': case '2': case '3': case '4':
2223 case '5': case '6': case '7': case '8': case '9':
2225 int m_hregno;
2226 bool match_p;
2228 m = strtoul (p, &end, 10);
2229 p = end;
2230 len = 0;
2231 lra_assert (nop > m);
2233 /* Reject matches if we don't know which operand is
2234 bigger. This situation would arguably be a bug in
2235 an .md pattern, but could also occur in a user asm. */
2236 if (!ordered_p (GET_MODE_SIZE (biggest_mode[m]),
2237 GET_MODE_SIZE (biggest_mode[nop])))
2238 break;
2240 /* Don't match wrong asm insn operands for proper
2241 diagnostic later. */
2242 if (INSN_CODE (curr_insn) < 0
2243 && (curr_operand_mode[m] == BLKmode
2244 || curr_operand_mode[nop] == BLKmode)
2245 && curr_operand_mode[m] != curr_operand_mode[nop])
2246 break;
2248 m_hregno = get_hard_regno (*curr_id->operand_loc[m], false);
2249 /* We are supposed to match a previous operand.
2250 If we do, we win if that one did. If we do
2251 not, count both of the operands as losers.
2252 (This is too conservative, since most of the
2253 time only a single reload insn will be needed
2254 to make the two operands win. As a result,
2255 this alternative may be rejected when it is
2256 actually desirable.) */
2257 match_p = false;
2258 if (operands_match_p (*curr_id->operand_loc[nop],
2259 *curr_id->operand_loc[m], m_hregno))
2261 /* We should reject matching of an early
2262 clobber operand if the matching operand is
2263 not dying in the insn. */
2264 if (!TEST_BIT (curr_static_id->operand[m]
2265 .early_clobber_alts, nalt)
2266 || operand_reg[nop] == NULL_RTX
2267 || (find_regno_note (curr_insn, REG_DEAD,
2268 REGNO (op))
2269 || REGNO (op) == REGNO (operand_reg[m])))
2270 match_p = true;
2272 if (match_p)
2274 /* If we are matching a non-offsettable
2275 address where an offsettable address was
2276 expected, then we must reject this
2277 combination, because we can't reload
2278 it. */
2279 if (curr_alt_offmemok[m]
2280 && MEM_P (*curr_id->operand_loc[m])
2281 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
2282 continue;
2284 else
2286 /* If the operands do not match and one
2287 operand is INOUT, we can not match them.
2288 Try other possibilities, e.g. other
2289 alternatives or commutative operand
2290 exchange. */
2291 if (curr_static_id->operand[nop].type == OP_INOUT
2292 || curr_static_id->operand[m].type == OP_INOUT)
2293 break;
2294 /* Operands don't match. If the operands are
2295 different user defined explicit hard
2296 registers, then we cannot make them match
2297 when one is early clobber operand. */
2298 if ((REG_P (*curr_id->operand_loc[nop])
2299 || SUBREG_P (*curr_id->operand_loc[nop]))
2300 && (REG_P (*curr_id->operand_loc[m])
2301 || SUBREG_P (*curr_id->operand_loc[m])))
2303 rtx nop_reg = *curr_id->operand_loc[nop];
2304 if (SUBREG_P (nop_reg))
2305 nop_reg = SUBREG_REG (nop_reg);
2306 rtx m_reg = *curr_id->operand_loc[m];
2307 if (SUBREG_P (m_reg))
2308 m_reg = SUBREG_REG (m_reg);
2310 if (REG_P (nop_reg)
2311 && HARD_REGISTER_P (nop_reg)
2312 && REG_USERVAR_P (nop_reg)
2313 && REG_P (m_reg)
2314 && HARD_REGISTER_P (m_reg)
2315 && REG_USERVAR_P (m_reg))
2317 int i;
2319 for (i = 0; i < early_clobbered_regs_num; i++)
2320 if (m == early_clobbered_nops[i])
2321 break;
2322 if (i < early_clobbered_regs_num
2323 || early_clobber_p)
2324 break;
2327 /* Both operands must allow a reload register,
2328 otherwise we cannot make them match. */
2329 if (curr_alt[m] == NO_REGS)
2330 break;
2331 /* Retroactively mark the operand we had to
2332 match as a loser, if it wasn't already and
2333 it wasn't matched to a register constraint
2334 (e.g it might be matched by memory). */
2335 if (curr_alt_win[m]
2336 && (operand_reg[m] == NULL_RTX
2337 || hard_regno[m] < 0))
2339 losers++;
2340 reload_nregs
2341 += (ira_reg_class_max_nregs[curr_alt[m]]
2342 [GET_MODE (*curr_id->operand_loc[m])]);
2345 /* Prefer matching earlyclobber alternative as
2346 it results in less hard regs required for
2347 the insn than a non-matching earlyclobber
2348 alternative. */
2349 if (TEST_BIT (curr_static_id->operand[m]
2350 .early_clobber_alts, nalt))
2352 if (lra_dump_file != NULL)
2353 fprintf
2354 (lra_dump_file,
2355 " %d Matching earlyclobber alt:"
2356 " reject--\n",
2357 nop);
2358 if (!matching_early_clobber[m])
2360 reject--;
2361 matching_early_clobber[m] = 1;
2364 /* Otherwise we prefer no matching
2365 alternatives because it gives more freedom
2366 in RA. */
2367 else if (operand_reg[nop] == NULL_RTX
2368 || (find_regno_note (curr_insn, REG_DEAD,
2369 REGNO (operand_reg[nop]))
2370 == NULL_RTX))
2372 if (lra_dump_file != NULL)
2373 fprintf
2374 (lra_dump_file,
2375 " %d Matching alt: reject+=2\n",
2376 nop);
2377 reject += 2;
2380 /* If we have to reload this operand and some
2381 previous operand also had to match the same
2382 thing as this operand, we don't know how to do
2383 that. */
2384 if (!match_p || !curr_alt_win[m])
2386 for (i = 0; i < nop; i++)
2387 if (curr_alt_matches[i] == m)
2388 break;
2389 if (i < nop)
2390 break;
2392 else
2393 did_match = true;
2395 this_alternative_matches = m;
2396 /* This can be fixed with reloads if the operand
2397 we are supposed to match can be fixed with
2398 reloads. */
2399 badop = false;
2400 this_alternative = curr_alt[m];
2401 this_alternative_set = curr_alt_set[m];
2402 winreg = this_alternative != NO_REGS;
2403 break;
2406 case 'g':
2407 if (MEM_P (op)
2408 || general_constant_p (op)
2409 || spilled_pseudo_p (op))
2410 win = true;
2411 cl = GENERAL_REGS;
2412 goto reg;
2414 default:
2415 cn = lookup_constraint (p);
2416 switch (get_constraint_type (cn))
2418 case CT_REGISTER:
2419 cl = reg_class_for_constraint (cn);
2420 if (cl != NO_REGS)
2421 goto reg;
2422 break;
2424 case CT_CONST_INT:
2425 if (CONST_INT_P (op)
2426 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2427 win = true;
2428 break;
2430 case CT_MEMORY:
2431 if (MEM_P (op)
2432 && satisfies_memory_constraint_p (op, cn))
2433 win = true;
2434 else if (spilled_pseudo_p (op))
2435 win = true;
2437 /* If we didn't already win, we can reload constants
2438 via force_const_mem or put the pseudo value into
2439 memory, or make other memory by reloading the
2440 address like for 'o'. */
2441 if (CONST_POOL_OK_P (mode, op)
2442 || MEM_P (op) || REG_P (op)
2443 /* We can restore the equiv insn by a
2444 reload. */
2445 || equiv_substition_p[nop])
2446 badop = false;
2447 constmemok = true;
2448 offmemok = true;
2449 break;
2451 case CT_ADDRESS:
2452 /* An asm operand with an address constraint
2453 that doesn't satisfy address_operand has
2454 is_address cleared, so that we don't try to
2455 make a non-address fit. */
2456 if (!curr_static_id->operand[nop].is_address)
2457 break;
2458 /* If we didn't already win, we can reload the address
2459 into a base register. */
2460 if (satisfies_address_constraint_p (op, cn))
2461 win = true;
2462 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2463 ADDRESS, SCRATCH);
2464 badop = false;
2465 goto reg;
2467 case CT_FIXED_FORM:
2468 if (constraint_satisfied_p (op, cn))
2469 win = true;
2470 break;
2472 case CT_SPECIAL_MEMORY:
2473 if (satisfies_memory_constraint_p (op, cn))
2474 win = true;
2475 else if (spilled_pseudo_p (op))
2476 win = true;
2477 break;
2479 break;
2481 reg:
2482 if (mode == BLKmode)
2483 break;
2484 this_alternative = reg_class_subunion[this_alternative][cl];
2485 this_alternative_set |= reg_class_contents[cl];
2486 if (costly_p)
2488 this_costly_alternative
2489 = reg_class_subunion[this_costly_alternative][cl];
2490 this_costly_alternative_set |= reg_class_contents[cl];
2492 winreg = true;
2493 if (REG_P (op))
2495 if (hard_regno[nop] >= 0
2496 && in_hard_reg_set_p (this_alternative_set,
2497 mode, hard_regno[nop]))
2498 win = true;
2499 else if (hard_regno[nop] < 0
2500 && in_class_p (op, this_alternative, NULL))
2501 win = true;
2503 break;
2505 if (c != ' ' && c != '\t')
2506 costly_p = c == '*';
2508 while ((p += len), c);
2510 scratch_p = (operand_reg[nop] != NULL_RTX
2511 && ira_former_scratch_p (REGNO (operand_reg[nop])));
2512 /* Record which operands fit this alternative. */
2513 if (win)
2515 this_alternative_win = true;
2516 if (operand_reg[nop] != NULL_RTX)
2518 if (hard_regno[nop] >= 0)
2520 if (in_hard_reg_set_p (this_costly_alternative_set,
2521 mode, hard_regno[nop]))
2523 if (lra_dump_file != NULL)
2524 fprintf (lra_dump_file,
2525 " %d Costly set: reject++\n",
2526 nop);
2527 reject++;
2530 else
2532 /* Prefer won reg to spilled pseudo under other
2533 equal conditions for possibe inheritance. */
2534 if (! scratch_p)
2536 if (lra_dump_file != NULL)
2537 fprintf
2538 (lra_dump_file,
2539 " %d Non pseudo reload: reject++\n",
2540 nop);
2541 reject++;
2543 if (in_class_p (operand_reg[nop],
2544 this_costly_alternative, NULL))
2546 if (lra_dump_file != NULL)
2547 fprintf
2548 (lra_dump_file,
2549 " %d Non pseudo costly reload:"
2550 " reject++\n",
2551 nop);
2552 reject++;
2555 /* We simulate the behavior of old reload here.
2556 Although scratches need hard registers and it
2557 might result in spilling other pseudos, no reload
2558 insns are generated for the scratches. So it
2559 might cost something but probably less than old
2560 reload pass believes. */
2561 if (scratch_p)
2563 if (lra_dump_file != NULL)
2564 fprintf (lra_dump_file,
2565 " %d Scratch win: reject+=2\n",
2566 nop);
2567 reject += 2;
2571 else if (did_match)
2572 this_alternative_match_win = true;
2573 else
2575 int const_to_mem = 0;
2576 bool no_regs_p;
2578 reject += op_reject;
2579 /* Never do output reload of stack pointer. It makes
2580 impossible to do elimination when SP is changed in
2581 RTL. */
2582 if (op == stack_pointer_rtx && ! frame_pointer_needed
2583 && curr_static_id->operand[nop].type != OP_IN)
2584 goto fail;
2586 /* If this alternative asks for a specific reg class, see if there
2587 is at least one allocatable register in that class. */
2588 no_regs_p
2589 = (this_alternative == NO_REGS
2590 || (hard_reg_set_subset_p
2591 (reg_class_contents[this_alternative],
2592 lra_no_alloc_regs)));
2594 /* For asms, verify that the class for this alternative is possible
2595 for the mode that is specified. */
2596 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2598 int i;
2599 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2600 if (targetm.hard_regno_mode_ok (i, mode)
2601 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2602 mode, i))
2603 break;
2604 if (i == FIRST_PSEUDO_REGISTER)
2605 winreg = false;
2608 /* If this operand accepts a register, and if the
2609 register class has at least one allocatable register,
2610 then this operand can be reloaded. */
2611 if (winreg && !no_regs_p)
2612 badop = false;
2614 if (badop)
2616 if (lra_dump_file != NULL)
2617 fprintf (lra_dump_file,
2618 " alt=%d: Bad operand -- refuse\n",
2619 nalt);
2620 goto fail;
2623 if (this_alternative != NO_REGS)
2625 HARD_REG_SET available_regs
2626 = (reg_class_contents[this_alternative]
2627 & ~((ira_prohibited_class_mode_regs
2628 [this_alternative][mode])
2629 | lra_no_alloc_regs));
2630 if (hard_reg_set_empty_p (available_regs))
2632 /* There are no hard regs holding a value of given
2633 mode. */
2634 if (offmemok)
2636 this_alternative = NO_REGS;
2637 if (lra_dump_file != NULL)
2638 fprintf (lra_dump_file,
2639 " %d Using memory because of"
2640 " a bad mode: reject+=2\n",
2641 nop);
2642 reject += 2;
2644 else
2646 if (lra_dump_file != NULL)
2647 fprintf (lra_dump_file,
2648 " alt=%d: Wrong mode -- refuse\n",
2649 nalt);
2650 goto fail;
2655 /* If not assigned pseudo has a class which a subset of
2656 required reg class, it is a less costly alternative
2657 as the pseudo still can get a hard reg of necessary
2658 class. */
2659 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2660 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2661 && ira_class_subset_p[this_alternative][cl])
2663 if (lra_dump_file != NULL)
2664 fprintf
2665 (lra_dump_file,
2666 " %d Super set class reg: reject-=3\n", nop);
2667 reject -= 3;
2670 this_alternative_offmemok = offmemok;
2671 if (this_costly_alternative != NO_REGS)
2673 if (lra_dump_file != NULL)
2674 fprintf (lra_dump_file,
2675 " %d Costly loser: reject++\n", nop);
2676 reject++;
2678 /* If the operand is dying, has a matching constraint,
2679 and satisfies constraints of the matched operand
2680 which failed to satisfy the own constraints, most probably
2681 the reload for this operand will be gone. */
2682 if (this_alternative_matches >= 0
2683 && !curr_alt_win[this_alternative_matches]
2684 && REG_P (op)
2685 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2686 && (hard_regno[nop] >= 0
2687 ? in_hard_reg_set_p (this_alternative_set,
2688 mode, hard_regno[nop])
2689 : in_class_p (op, this_alternative, NULL)))
2691 if (lra_dump_file != NULL)
2692 fprintf
2693 (lra_dump_file,
2694 " %d Dying matched operand reload: reject++\n",
2695 nop);
2696 reject++;
2698 else
2700 /* Strict_low_part requires to reload the register
2701 not the sub-register. In this case we should
2702 check that a final reload hard reg can hold the
2703 value mode. */
2704 if (curr_static_id->operand[nop].strict_low
2705 && REG_P (op)
2706 && hard_regno[nop] < 0
2707 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2708 && ira_class_hard_regs_num[this_alternative] > 0
2709 && (!targetm.hard_regno_mode_ok
2710 (ira_class_hard_regs[this_alternative][0],
2711 GET_MODE (*curr_id->operand_loc[nop]))))
2713 if (lra_dump_file != NULL)
2714 fprintf
2715 (lra_dump_file,
2716 " alt=%d: Strict low subreg reload -- refuse\n",
2717 nalt);
2718 goto fail;
2720 losers++;
2722 if (operand_reg[nop] != NULL_RTX
2723 /* Output operands and matched input operands are
2724 not inherited. The following conditions do not
2725 exactly describe the previous statement but they
2726 are pretty close. */
2727 && curr_static_id->operand[nop].type != OP_OUT
2728 && (this_alternative_matches < 0
2729 || curr_static_id->operand[nop].type != OP_IN))
2731 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2732 (operand_reg[nop])]
2733 .last_reload);
2735 /* The value of reload_sum has sense only if we
2736 process insns in their order. It happens only on
2737 the first constraints sub-pass when we do most of
2738 reload work. */
2739 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2740 reload_sum += last_reload - bb_reload_num;
2742 /* If this is a constant that is reloaded into the
2743 desired class by copying it to memory first, count
2744 that as another reload. This is consistent with
2745 other code and is required to avoid choosing another
2746 alternative when the constant is moved into memory.
2747 Note that the test here is precisely the same as in
2748 the code below that calls force_const_mem. */
2749 if (CONST_POOL_OK_P (mode, op)
2750 && ((targetm.preferred_reload_class
2751 (op, this_alternative) == NO_REGS)
2752 || no_input_reloads_p))
2754 const_to_mem = 1;
2755 if (! no_regs_p)
2756 losers++;
2759 /* Alternative loses if it requires a type of reload not
2760 permitted for this insn. We can always reload
2761 objects with a REG_UNUSED note. */
2762 if ((curr_static_id->operand[nop].type != OP_IN
2763 && no_output_reloads_p
2764 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2765 || (curr_static_id->operand[nop].type != OP_OUT
2766 && no_input_reloads_p && ! const_to_mem)
2767 || (this_alternative_matches >= 0
2768 && (no_input_reloads_p
2769 || (no_output_reloads_p
2770 && (curr_static_id->operand
2771 [this_alternative_matches].type != OP_IN)
2772 && ! find_reg_note (curr_insn, REG_UNUSED,
2773 no_subreg_reg_operand
2774 [this_alternative_matches])))))
2776 if (lra_dump_file != NULL)
2777 fprintf
2778 (lra_dump_file,
2779 " alt=%d: No input/otput reload -- refuse\n",
2780 nalt);
2781 goto fail;
2784 /* Alternative loses if it required class pseudo cannot
2785 hold value of required mode. Such insns can be
2786 described by insn definitions with mode iterators. */
2787 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2788 && ! hard_reg_set_empty_p (this_alternative_set)
2789 /* It is common practice for constraints to use a
2790 class which does not have actually enough regs to
2791 hold the value (e.g. x86 AREG for mode requiring
2792 more one general reg). Therefore we have 2
2793 conditions to check that the reload pseudo cannot
2794 hold the mode value. */
2795 && (!targetm.hard_regno_mode_ok
2796 (ira_class_hard_regs[this_alternative][0],
2797 GET_MODE (*curr_id->operand_loc[nop])))
2798 /* The above condition is not enough as the first
2799 reg in ira_class_hard_regs can be not aligned for
2800 multi-words mode values. */
2801 && (prohibited_class_reg_set_mode_p
2802 (this_alternative, this_alternative_set,
2803 GET_MODE (*curr_id->operand_loc[nop]))))
2805 if (lra_dump_file != NULL)
2806 fprintf (lra_dump_file,
2807 " alt=%d: reload pseudo for op %d "
2808 "cannot hold the mode value -- refuse\n",
2809 nalt, nop);
2810 goto fail;
2813 /* Check strong discouragement of reload of non-constant
2814 into class THIS_ALTERNATIVE. */
2815 if (! CONSTANT_P (op) && ! no_regs_p
2816 && (targetm.preferred_reload_class
2817 (op, this_alternative) == NO_REGS
2818 || (curr_static_id->operand[nop].type == OP_OUT
2819 && (targetm.preferred_output_reload_class
2820 (op, this_alternative) == NO_REGS))))
2822 if (offmemok && REG_P (op))
2824 if (lra_dump_file != NULL)
2825 fprintf
2826 (lra_dump_file,
2827 " %d Spill pseudo into memory: reject+=3\n",
2828 nop);
2829 reject += 3;
2831 else
2833 if (lra_dump_file != NULL)
2834 fprintf
2835 (lra_dump_file,
2836 " %d Non-prefered reload: reject+=%d\n",
2837 nop, LRA_MAX_REJECT);
2838 reject += LRA_MAX_REJECT;
2842 if (! (MEM_P (op) && offmemok)
2843 && ! (const_to_mem && constmemok))
2845 /* We prefer to reload pseudos over reloading other
2846 things, since such reloads may be able to be
2847 eliminated later. So bump REJECT in other cases.
2848 Don't do this in the case where we are forcing a
2849 constant into memory and it will then win since
2850 we don't want to have a different alternative
2851 match then. */
2852 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2854 if (lra_dump_file != NULL)
2855 fprintf
2856 (lra_dump_file,
2857 " %d Non-pseudo reload: reject+=2\n",
2858 nop);
2859 reject += 2;
2862 if (! no_regs_p)
2863 reload_nregs
2864 += ira_reg_class_max_nregs[this_alternative][mode];
2866 if (SMALL_REGISTER_CLASS_P (this_alternative))
2868 if (lra_dump_file != NULL)
2869 fprintf
2870 (lra_dump_file,
2871 " %d Small class reload: reject+=%d\n",
2872 nop, LRA_LOSER_COST_FACTOR / 2);
2873 reject += LRA_LOSER_COST_FACTOR / 2;
2877 /* We are trying to spill pseudo into memory. It is
2878 usually more costly than moving to a hard register
2879 although it might takes the same number of
2880 reloads.
2882 Non-pseudo spill may happen also. Suppose a target allows both
2883 register and memory in the operand constraint alternatives,
2884 then it's typical that an eliminable register has a substition
2885 of "base + offset" which can either be reloaded by a simple
2886 "new_reg <= base + offset" which will match the register
2887 constraint, or a similar reg addition followed by further spill
2888 to and reload from memory which will match the memory
2889 constraint, but this memory spill will be much more costly
2890 usually.
2892 Code below increases the reject for both pseudo and non-pseudo
2893 spill. */
2894 if (no_regs_p
2895 && !(MEM_P (op) && offmemok)
2896 && !(REG_P (op) && hard_regno[nop] < 0))
2898 if (lra_dump_file != NULL)
2899 fprintf
2900 (lra_dump_file,
2901 " %d Spill %spseudo into memory: reject+=3\n",
2902 nop, REG_P (op) ? "" : "Non-");
2903 reject += 3;
2904 if (VECTOR_MODE_P (mode))
2906 /* Spilling vectors into memory is usually more
2907 costly as they contain big values. */
2908 if (lra_dump_file != NULL)
2909 fprintf
2910 (lra_dump_file,
2911 " %d Spill vector pseudo: reject+=2\n",
2912 nop);
2913 reject += 2;
2917 /* When we use an operand requiring memory in given
2918 alternative, the insn should write *and* read the
2919 value to/from memory it is costly in comparison with
2920 an insn alternative which does not use memory
2921 (e.g. register or immediate operand). We exclude
2922 memory operand for such case as we can satisfy the
2923 memory constraints by reloading address. */
2924 if (no_regs_p && offmemok && !MEM_P (op))
2926 if (lra_dump_file != NULL)
2927 fprintf
2928 (lra_dump_file,
2929 " Using memory insn operand %d: reject+=3\n",
2930 nop);
2931 reject += 3;
2934 /* If reload requires moving value through secondary
2935 memory, it will need one more insn at least. */
2936 if (this_alternative != NO_REGS
2937 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2938 && ((curr_static_id->operand[nop].type != OP_OUT
2939 && targetm.secondary_memory_needed (GET_MODE (op), cl,
2940 this_alternative))
2941 || (curr_static_id->operand[nop].type != OP_IN
2942 && (targetm.secondary_memory_needed
2943 (GET_MODE (op), this_alternative, cl)))))
2944 losers++;
2946 if (MEM_P (op) && offmemok)
2947 addr_losers++;
2948 else
2950 /* Input reloads can be inherited more often than
2951 output reloads can be removed, so penalize output
2952 reloads. */
2953 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2955 if (lra_dump_file != NULL)
2956 fprintf
2957 (lra_dump_file,
2958 " %d Non input pseudo reload: reject++\n",
2959 nop);
2960 reject++;
2963 if (curr_static_id->operand[nop].type == OP_INOUT)
2965 if (lra_dump_file != NULL)
2966 fprintf
2967 (lra_dump_file,
2968 " %d Input/Output reload: reject+=%d\n",
2969 nop, LRA_LOSER_COST_FACTOR);
2970 reject += LRA_LOSER_COST_FACTOR;
2975 if (early_clobber_p && ! scratch_p)
2977 if (lra_dump_file != NULL)
2978 fprintf (lra_dump_file,
2979 " %d Early clobber: reject++\n", nop);
2980 reject++;
2982 /* ??? We check early clobbers after processing all operands
2983 (see loop below) and there we update the costs more.
2984 Should we update the cost (may be approximately) here
2985 because of early clobber register reloads or it is a rare
2986 or non-important thing to be worth to do it. */
2987 overall = (losers * LRA_LOSER_COST_FACTOR + reject
2988 - (addr_losers == losers ? static_reject : 0));
2989 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2991 if (lra_dump_file != NULL)
2992 fprintf (lra_dump_file,
2993 " alt=%d,overall=%d,losers=%d -- refuse\n",
2994 nalt, overall, losers);
2995 goto fail;
2998 if (update_and_check_small_class_inputs (nop, nalt,
2999 this_alternative))
3001 if (lra_dump_file != NULL)
3002 fprintf (lra_dump_file,
3003 " alt=%d, not enough small class regs -- refuse\n",
3004 nalt);
3005 goto fail;
3007 curr_alt[nop] = this_alternative;
3008 curr_alt_set[nop] = this_alternative_set;
3009 curr_alt_win[nop] = this_alternative_win;
3010 curr_alt_match_win[nop] = this_alternative_match_win;
3011 curr_alt_offmemok[nop] = this_alternative_offmemok;
3012 curr_alt_matches[nop] = this_alternative_matches;
3014 if (this_alternative_matches >= 0
3015 && !did_match && !this_alternative_win)
3016 curr_alt_win[this_alternative_matches] = false;
3018 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
3019 early_clobbered_nops[early_clobbered_regs_num++] = nop;
3022 if (curr_insn_set != NULL_RTX && n_operands == 2
3023 /* Prevent processing non-move insns. */
3024 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
3025 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
3026 && ((! curr_alt_win[0] && ! curr_alt_win[1]
3027 && REG_P (no_subreg_reg_operand[0])
3028 && REG_P (no_subreg_reg_operand[1])
3029 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
3030 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
3031 || (! curr_alt_win[0] && curr_alt_win[1]
3032 && REG_P (no_subreg_reg_operand[1])
3033 /* Check that we reload memory not the memory
3034 address. */
3035 && ! (curr_alt_offmemok[0]
3036 && MEM_P (no_subreg_reg_operand[0]))
3037 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
3038 || (curr_alt_win[0] && ! curr_alt_win[1]
3039 && REG_P (no_subreg_reg_operand[0])
3040 /* Check that we reload memory not the memory
3041 address. */
3042 && ! (curr_alt_offmemok[1]
3043 && MEM_P (no_subreg_reg_operand[1]))
3044 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
3045 && (! CONST_POOL_OK_P (curr_operand_mode[1],
3046 no_subreg_reg_operand[1])
3047 || (targetm.preferred_reload_class
3048 (no_subreg_reg_operand[1],
3049 (enum reg_class) curr_alt[1]) != NO_REGS))
3050 /* If it is a result of recent elimination in move
3051 insn we can transform it into an add still by
3052 using this alternative. */
3053 && GET_CODE (no_subreg_reg_operand[1]) != PLUS
3054 /* Likewise if the source has been replaced with an
3055 equivalent value. This only happens once -- the reload
3056 will use the equivalent value instead of the register it
3057 replaces -- so there should be no danger of cycling. */
3058 && !equiv_substition_p[1])))
3060 /* We have a move insn and a new reload insn will be similar
3061 to the current insn. We should avoid such situation as
3062 it results in LRA cycling. */
3063 if (lra_dump_file != NULL)
3064 fprintf (lra_dump_file,
3065 " Cycle danger: overall += LRA_MAX_REJECT\n");
3066 overall += LRA_MAX_REJECT;
3068 ok_p = true;
3069 curr_alt_dont_inherit_ops_num = 0;
3070 for (nop = 0; nop < early_clobbered_regs_num; nop++)
3072 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
3073 HARD_REG_SET temp_set;
3075 i = early_clobbered_nops[nop];
3076 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
3077 || hard_regno[i] < 0)
3078 continue;
3079 lra_assert (operand_reg[i] != NULL_RTX);
3080 clobbered_hard_regno = hard_regno[i];
3081 CLEAR_HARD_REG_SET (temp_set);
3082 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
3083 first_conflict_j = last_conflict_j = -1;
3084 for (j = 0; j < n_operands; j++)
3085 if (j == i
3086 /* We don't want process insides of match_operator and
3087 match_parallel because otherwise we would process
3088 their operands once again generating a wrong
3089 code. */
3090 || curr_static_id->operand[j].is_operator)
3091 continue;
3092 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
3093 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
3094 continue;
3095 /* If we don't reload j-th operand, check conflicts. */
3096 else if ((curr_alt_win[j] || curr_alt_match_win[j])
3097 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
3099 if (first_conflict_j < 0)
3100 first_conflict_j = j;
3101 last_conflict_j = j;
3102 /* Both the earlyclobber operand and conflicting operand
3103 cannot both be user defined hard registers. */
3104 if (HARD_REGISTER_P (operand_reg[i])
3105 && REG_USERVAR_P (operand_reg[i])
3106 && operand_reg[j] != NULL_RTX
3107 && HARD_REGISTER_P (operand_reg[j])
3108 && REG_USERVAR_P (operand_reg[j]))
3109 fatal_insn ("unable to generate reloads for "
3110 "impossible constraints:", curr_insn);
3112 if (last_conflict_j < 0)
3113 continue;
3115 /* If an earlyclobber operand conflicts with another non-matching
3116 operand (ie, they have been assigned the same hard register),
3117 then it is better to reload the other operand, as there may
3118 exist yet another operand with a matching constraint associated
3119 with the earlyclobber operand. However, if one of the operands
3120 is an explicit use of a hard register, then we must reload the
3121 other non-hard register operand. */
3122 if (HARD_REGISTER_P (operand_reg[i])
3123 || (first_conflict_j == last_conflict_j
3124 && operand_reg[last_conflict_j] != NULL_RTX
3125 && !curr_alt_match_win[last_conflict_j]
3126 && !HARD_REGISTER_P (operand_reg[last_conflict_j])))
3128 curr_alt_win[last_conflict_j] = false;
3129 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
3130 = last_conflict_j;
3131 losers++;
3132 if (lra_dump_file != NULL)
3133 fprintf
3134 (lra_dump_file,
3135 " %d Conflict early clobber reload: reject--\n",
3138 else
3140 /* We need to reload early clobbered register and the
3141 matched registers. */
3142 for (j = 0; j < n_operands; j++)
3143 if (curr_alt_matches[j] == i)
3145 curr_alt_match_win[j] = false;
3146 losers++;
3147 overall += LRA_LOSER_COST_FACTOR;
3149 if (! curr_alt_match_win[i])
3150 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
3151 else
3153 /* Remember pseudos used for match reloads are never
3154 inherited. */
3155 lra_assert (curr_alt_matches[i] >= 0);
3156 curr_alt_win[curr_alt_matches[i]] = false;
3158 curr_alt_win[i] = curr_alt_match_win[i] = false;
3159 losers++;
3160 if (lra_dump_file != NULL)
3161 fprintf
3162 (lra_dump_file,
3163 " %d Matched conflict early clobber reloads: "
3164 "reject--\n",
3167 /* Early clobber was already reflected in REJECT. */
3168 if (!matching_early_clobber[i])
3170 lra_assert (reject > 0);
3171 reject--;
3172 matching_early_clobber[i] = 1;
3174 overall += LRA_LOSER_COST_FACTOR - 1;
3176 if (lra_dump_file != NULL)
3177 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
3178 nalt, overall, losers, reload_nregs);
3180 /* If this alternative can be made to work by reloading, and it
3181 needs less reloading than the others checked so far, record
3182 it as the chosen goal for reloading. */
3183 if ((best_losers != 0 && losers == 0)
3184 || (((best_losers == 0 && losers == 0)
3185 || (best_losers != 0 && losers != 0))
3186 && (best_overall > overall
3187 || (best_overall == overall
3188 /* If the cost of the reloads is the same,
3189 prefer alternative which requires minimal
3190 number of reload regs. */
3191 && (reload_nregs < best_reload_nregs
3192 || (reload_nregs == best_reload_nregs
3193 && (best_reload_sum < reload_sum
3194 || (best_reload_sum == reload_sum
3195 && nalt < goal_alt_number))))))))
3197 for (nop = 0; nop < n_operands; nop++)
3199 goal_alt_win[nop] = curr_alt_win[nop];
3200 goal_alt_match_win[nop] = curr_alt_match_win[nop];
3201 goal_alt_matches[nop] = curr_alt_matches[nop];
3202 goal_alt[nop] = curr_alt[nop];
3203 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
3205 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
3206 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
3207 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
3208 goal_alt_swapped = curr_swapped;
3209 best_overall = overall;
3210 best_losers = losers;
3211 best_reload_nregs = reload_nregs;
3212 best_reload_sum = reload_sum;
3213 goal_alt_number = nalt;
3215 if (losers == 0)
3216 /* Everything is satisfied. Do not process alternatives
3217 anymore. */
3218 break;
3219 fail:
3222 return ok_p;
3225 /* Make reload base reg from address AD. */
3226 static rtx
3227 base_to_reg (struct address_info *ad)
3229 enum reg_class cl;
3230 int code = -1;
3231 rtx new_inner = NULL_RTX;
3232 rtx new_reg = NULL_RTX;
3233 rtx_insn *insn;
3234 rtx_insn *last_insn = get_last_insn();
3236 lra_assert (ad->disp == ad->disp_term);
3237 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
3238 get_index_code (ad));
3239 new_reg = lra_create_new_reg (GET_MODE (*ad->base), NULL_RTX,
3240 cl, "base");
3241 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
3242 ad->disp_term == NULL
3243 ? const0_rtx
3244 : *ad->disp_term);
3245 if (!valid_address_p (ad->mode, new_inner, ad->as))
3246 return NULL_RTX;
3247 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base));
3248 code = recog_memoized (insn);
3249 if (code < 0)
3251 delete_insns_since (last_insn);
3252 return NULL_RTX;
3255 return new_inner;
3258 /* Make reload base reg + DISP from address AD. Return the new pseudo. */
3259 static rtx
3260 base_plus_disp_to_reg (struct address_info *ad, rtx disp)
3262 enum reg_class cl;
3263 rtx new_reg;
3265 lra_assert (ad->base == ad->base_term);
3266 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
3267 get_index_code (ad));
3268 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
3269 cl, "base + disp");
3270 lra_emit_add (new_reg, *ad->base_term, disp);
3271 return new_reg;
3274 /* Make reload of index part of address AD. Return the new
3275 pseudo. */
3276 static rtx
3277 index_part_to_reg (struct address_info *ad)
3279 rtx new_reg;
3281 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
3282 INDEX_REG_CLASS, "index term");
3283 expand_mult (GET_MODE (*ad->index), *ad->index_term,
3284 GEN_INT (get_index_scale (ad)), new_reg, 1);
3285 return new_reg;
3288 /* Return true if we can add a displacement to address AD, even if that
3289 makes the address invalid. The fix-up code requires any new address
3290 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
3291 static bool
3292 can_add_disp_p (struct address_info *ad)
3294 return (!ad->autoinc_p
3295 && ad->segment == NULL
3296 && ad->base == ad->base_term
3297 && ad->disp == ad->disp_term);
3300 /* Make equiv substitution in address AD. Return true if a substitution
3301 was made. */
3302 static bool
3303 equiv_address_substitution (struct address_info *ad)
3305 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
3306 poly_int64 disp;
3307 HOST_WIDE_INT scale;
3308 bool change_p;
3310 base_term = strip_subreg (ad->base_term);
3311 if (base_term == NULL)
3312 base_reg = new_base_reg = NULL_RTX;
3313 else
3315 base_reg = *base_term;
3316 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
3318 index_term = strip_subreg (ad->index_term);
3319 if (index_term == NULL)
3320 index_reg = new_index_reg = NULL_RTX;
3321 else
3323 index_reg = *index_term;
3324 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
3326 if (base_reg == new_base_reg && index_reg == new_index_reg)
3327 return false;
3328 disp = 0;
3329 change_p = false;
3330 if (lra_dump_file != NULL)
3332 fprintf (lra_dump_file, "Changing address in insn %d ",
3333 INSN_UID (curr_insn));
3334 dump_value_slim (lra_dump_file, *ad->outer, 1);
3336 if (base_reg != new_base_reg)
3338 poly_int64 offset;
3339 if (REG_P (new_base_reg))
3341 *base_term = new_base_reg;
3342 change_p = true;
3344 else if (GET_CODE (new_base_reg) == PLUS
3345 && REG_P (XEXP (new_base_reg, 0))
3346 && poly_int_rtx_p (XEXP (new_base_reg, 1), &offset)
3347 && can_add_disp_p (ad))
3349 disp += offset;
3350 *base_term = XEXP (new_base_reg, 0);
3351 change_p = true;
3353 if (ad->base_term2 != NULL)
3354 *ad->base_term2 = *ad->base_term;
3356 if (index_reg != new_index_reg)
3358 poly_int64 offset;
3359 if (REG_P (new_index_reg))
3361 *index_term = new_index_reg;
3362 change_p = true;
3364 else if (GET_CODE (new_index_reg) == PLUS
3365 && REG_P (XEXP (new_index_reg, 0))
3366 && poly_int_rtx_p (XEXP (new_index_reg, 1), &offset)
3367 && can_add_disp_p (ad)
3368 && (scale = get_index_scale (ad)))
3370 disp += offset * scale;
3371 *index_term = XEXP (new_index_reg, 0);
3372 change_p = true;
3375 if (maybe_ne (disp, 0))
3377 if (ad->disp != NULL)
3378 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
3379 else
3381 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
3382 update_address (ad);
3384 change_p = true;
3386 if (lra_dump_file != NULL)
3388 if (! change_p)
3389 fprintf (lra_dump_file, " -- no change\n");
3390 else
3392 fprintf (lra_dump_file, " on equiv ");
3393 dump_value_slim (lra_dump_file, *ad->outer, 1);
3394 fprintf (lra_dump_file, "\n");
3397 return change_p;
3400 /* Major function to make reloads for an address in operand NOP or
3401 check its correctness (If CHECK_ONLY_P is true). The supported
3402 cases are:
3404 1) an address that existed before LRA started, at which point it
3405 must have been valid. These addresses are subject to elimination
3406 and may have become invalid due to the elimination offset being out
3407 of range.
3409 2) an address created by forcing a constant to memory
3410 (force_const_to_mem). The initial form of these addresses might
3411 not be valid, and it is this function's job to make them valid.
3413 3) a frame address formed from a register and a (possibly zero)
3414 constant offset. As above, these addresses might not be valid and
3415 this function must make them so.
3417 Add reloads to the lists *BEFORE and *AFTER. We might need to add
3418 reloads to *AFTER because of inc/dec, {pre, post} modify in the
3419 address. Return true for any RTL change.
3421 The function is a helper function which does not produce all
3422 transformations (when CHECK_ONLY_P is false) which can be
3423 necessary. It does just basic steps. To do all necessary
3424 transformations use function process_address. */
3425 static bool
3426 process_address_1 (int nop, bool check_only_p,
3427 rtx_insn **before, rtx_insn **after)
3429 struct address_info ad;
3430 rtx new_reg;
3431 HOST_WIDE_INT scale;
3432 rtx op = *curr_id->operand_loc[nop];
3433 rtx mem = extract_mem_from_operand (op);
3434 const char *constraint = curr_static_id->operand[nop].constraint;
3435 enum constraint_num cn = lookup_constraint (constraint);
3436 bool change_p = false;
3438 if (MEM_P (mem)
3439 && GET_MODE (mem) == BLKmode
3440 && GET_CODE (XEXP (mem, 0)) == SCRATCH)
3441 return false;
3443 if (insn_extra_address_constraint (cn)
3444 /* When we find an asm operand with an address constraint that
3445 doesn't satisfy address_operand to begin with, we clear
3446 is_address, so that we don't try to make a non-address fit.
3447 If the asm statement got this far, it's because other
3448 constraints are available, and we'll use them, disregarding
3449 the unsatisfiable address ones. */
3450 && curr_static_id->operand[nop].is_address)
3451 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
3452 /* Do not attempt to decompose arbitrary addresses generated by combine
3453 for asm operands with loose constraints, e.g 'X'.
3454 Need to extract memory from op for special memory constraint,
3455 i.e. bcst_mem_operand in i386 backend. */
3456 else if (MEM_P (mem)
3457 && !(INSN_CODE (curr_insn) < 0
3458 && get_constraint_type (cn) == CT_FIXED_FORM
3459 && constraint_satisfied_p (op, cn)))
3460 decompose_mem_address (&ad, mem);
3461 else if (GET_CODE (op) == SUBREG
3462 && MEM_P (SUBREG_REG (op)))
3463 decompose_mem_address (&ad, SUBREG_REG (op));
3464 else
3465 return false;
3466 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
3467 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
3468 when INDEX_REG_CLASS is a single register class. */
3469 if (ad.base_term != NULL
3470 && ad.index_term != NULL
3471 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
3472 && REG_P (*ad.base_term)
3473 && REG_P (*ad.index_term)
3474 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
3475 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
3477 std::swap (ad.base, ad.index);
3478 std::swap (ad.base_term, ad.index_term);
3480 if (! check_only_p)
3481 change_p = equiv_address_substitution (&ad);
3482 if (ad.base_term != NULL
3483 && (process_addr_reg
3484 (ad.base_term, check_only_p, before,
3485 (ad.autoinc_p
3486 && !(REG_P (*ad.base_term)
3487 && find_regno_note (curr_insn, REG_DEAD,
3488 REGNO (*ad.base_term)) != NULL_RTX)
3489 ? after : NULL),
3490 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3491 get_index_code (&ad)))))
3493 change_p = true;
3494 if (ad.base_term2 != NULL)
3495 *ad.base_term2 = *ad.base_term;
3497 if (ad.index_term != NULL
3498 && process_addr_reg (ad.index_term, check_only_p,
3499 before, NULL, INDEX_REG_CLASS))
3500 change_p = true;
3502 /* Target hooks sometimes don't treat extra-constraint addresses as
3503 legitimate address_operands, so handle them specially. */
3504 if (insn_extra_address_constraint (cn)
3505 && satisfies_address_constraint_p (&ad, cn))
3506 return change_p;
3508 if (check_only_p)
3509 return change_p;
3511 /* There are three cases where the shape of *AD.INNER may now be invalid:
3513 1) the original address was valid, but either elimination or
3514 equiv_address_substitution was applied and that made
3515 the address invalid.
3517 2) the address is an invalid symbolic address created by
3518 force_const_to_mem.
3520 3) the address is a frame address with an invalid offset.
3522 4) the address is a frame address with an invalid base.
3524 All these cases involve a non-autoinc address, so there is no
3525 point revalidating other types. */
3526 if (ad.autoinc_p || valid_address_p (op, &ad, cn))
3527 return change_p;
3529 /* Any index existed before LRA started, so we can assume that the
3530 presence and shape of the index is valid. */
3531 push_to_sequence (*before);
3532 lra_assert (ad.disp == ad.disp_term);
3533 if (ad.base == NULL)
3535 if (ad.index == NULL)
3537 rtx_insn *insn;
3538 rtx_insn *last = get_last_insn ();
3539 int code = -1;
3540 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3541 SCRATCH, SCRATCH);
3542 rtx addr = *ad.inner;
3544 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3545 if (HAVE_lo_sum)
3547 /* addr => lo_sum (new_base, addr), case (2) above. */
3548 insn = emit_insn (gen_rtx_SET
3549 (new_reg,
3550 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
3551 code = recog_memoized (insn);
3552 if (code >= 0)
3554 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
3555 if (!valid_address_p (op, &ad, cn))
3557 /* Try to put lo_sum into register. */
3558 insn = emit_insn (gen_rtx_SET
3559 (new_reg,
3560 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
3561 code = recog_memoized (insn);
3562 if (code >= 0)
3564 *ad.inner = new_reg;
3565 if (!valid_address_p (op, &ad, cn))
3567 *ad.inner = addr;
3568 code = -1;
3574 if (code < 0)
3575 delete_insns_since (last);
3578 if (code < 0)
3580 /* addr => new_base, case (2) above. */
3581 lra_emit_move (new_reg, addr);
3583 for (insn = last == NULL_RTX ? get_insns () : NEXT_INSN (last);
3584 insn != NULL_RTX;
3585 insn = NEXT_INSN (insn))
3586 if (recog_memoized (insn) < 0)
3587 break;
3588 if (insn != NULL_RTX)
3590 /* Do nothing if we cannot generate right insns.
3591 This is analogous to reload pass behavior. */
3592 delete_insns_since (last);
3593 end_sequence ();
3594 return false;
3596 *ad.inner = new_reg;
3599 else
3601 /* index * scale + disp => new base + index * scale,
3602 case (1) above. */
3603 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3604 GET_CODE (*ad.index));
3606 lra_assert (INDEX_REG_CLASS != NO_REGS);
3607 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3608 lra_emit_move (new_reg, *ad.disp);
3609 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3610 new_reg, *ad.index);
3613 else if (ad.index == NULL)
3615 int regno;
3616 enum reg_class cl;
3617 rtx set;
3618 rtx_insn *insns, *last_insn;
3619 /* Try to reload base into register only if the base is invalid
3620 for the address but with valid offset, case (4) above. */
3621 start_sequence ();
3622 new_reg = base_to_reg (&ad);
3624 /* base + disp => new base, cases (1) and (3) above. */
3625 /* Another option would be to reload the displacement into an
3626 index register. However, postreload has code to optimize
3627 address reloads that have the same base and different
3628 displacements, so reloading into an index register would
3629 not necessarily be a win. */
3630 if (new_reg == NULL_RTX)
3632 /* See if the target can split the displacement into a
3633 legitimate new displacement from a local anchor. */
3634 gcc_assert (ad.disp == ad.disp_term);
3635 poly_int64 orig_offset;
3636 rtx offset1, offset2;
3637 if (poly_int_rtx_p (*ad.disp, &orig_offset)
3638 && targetm.legitimize_address_displacement (&offset1, &offset2,
3639 orig_offset,
3640 ad.mode))
3642 new_reg = base_plus_disp_to_reg (&ad, offset1);
3643 new_reg = gen_rtx_PLUS (GET_MODE (new_reg), new_reg, offset2);
3645 else
3646 new_reg = base_plus_disp_to_reg (&ad, *ad.disp);
3648 insns = get_insns ();
3649 last_insn = get_last_insn ();
3650 /* If we generated at least two insns, try last insn source as
3651 an address. If we succeed, we generate one less insn. */
3652 if (REG_P (new_reg)
3653 && last_insn != insns
3654 && (set = single_set (last_insn)) != NULL_RTX
3655 && GET_CODE (SET_SRC (set)) == PLUS
3656 && REG_P (XEXP (SET_SRC (set), 0))
3657 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3659 *ad.inner = SET_SRC (set);
3660 if (valid_address_p (op, &ad, cn))
3662 *ad.base_term = XEXP (SET_SRC (set), 0);
3663 *ad.disp_term = XEXP (SET_SRC (set), 1);
3664 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3665 get_index_code (&ad));
3666 regno = REGNO (*ad.base_term);
3667 if (regno >= FIRST_PSEUDO_REGISTER
3668 && cl != lra_get_allocno_class (regno))
3669 lra_change_class (regno, cl, " Change to", true);
3670 new_reg = SET_SRC (set);
3671 delete_insns_since (PREV_INSN (last_insn));
3674 end_sequence ();
3675 emit_insn (insns);
3676 *ad.inner = new_reg;
3678 else if (ad.disp_term != NULL)
3680 /* base + scale * index + disp => new base + scale * index,
3681 case (1) above. */
3682 gcc_assert (ad.disp == ad.disp_term);
3683 new_reg = base_plus_disp_to_reg (&ad, *ad.disp);
3684 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3685 new_reg, *ad.index);
3687 else if ((scale = get_index_scale (&ad)) == 1)
3689 /* The last transformation to one reg will be made in
3690 curr_insn_transform function. */
3691 end_sequence ();
3692 return false;
3694 else if (scale != 0)
3696 /* base + scale * index => base + new_reg,
3697 case (1) above.
3698 Index part of address may become invalid. For example, we
3699 changed pseudo on the equivalent memory and a subreg of the
3700 pseudo onto the memory of different mode for which the scale is
3701 prohibitted. */
3702 new_reg = index_part_to_reg (&ad);
3703 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3704 *ad.base_term, new_reg);
3706 else
3708 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3709 SCRATCH, SCRATCH);
3710 rtx addr = *ad.inner;
3712 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3713 /* addr => new_base. */
3714 lra_emit_move (new_reg, addr);
3715 *ad.inner = new_reg;
3717 *before = get_insns ();
3718 end_sequence ();
3719 return true;
3722 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3723 Use process_address_1 as a helper function. Return true for any
3724 RTL changes.
3726 If CHECK_ONLY_P is true, just check address correctness. Return
3727 false if the address correct. */
3728 static bool
3729 process_address (int nop, bool check_only_p,
3730 rtx_insn **before, rtx_insn **after)
3732 bool res = false;
3734 while (process_address_1 (nop, check_only_p, before, after))
3736 if (check_only_p)
3737 return true;
3738 res = true;
3740 return res;
3743 /* Emit insns to reload VALUE into a new register. VALUE is an
3744 auto-increment or auto-decrement RTX whose operand is a register or
3745 memory location; so reloading involves incrementing that location.
3746 IN is either identical to VALUE, or some cheaper place to reload
3747 value being incremented/decremented from.
3749 INC_AMOUNT is the number to increment or decrement by (always
3750 positive and ignored for POST_MODIFY/PRE_MODIFY).
3752 Return pseudo containing the result. */
3753 static rtx
3754 emit_inc (enum reg_class new_rclass, rtx in, rtx value, poly_int64 inc_amount)
3756 /* REG or MEM to be copied and incremented. */
3757 rtx incloc = XEXP (value, 0);
3758 /* Nonzero if increment after copying. */
3759 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3760 || GET_CODE (value) == POST_MODIFY);
3761 rtx_insn *last;
3762 rtx inc;
3763 rtx_insn *add_insn;
3764 int code;
3765 rtx real_in = in == value ? incloc : in;
3766 rtx result;
3767 bool plus_p = true;
3769 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3771 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3772 || GET_CODE (XEXP (value, 1)) == MINUS);
3773 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3774 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3775 inc = XEXP (XEXP (value, 1), 1);
3777 else
3779 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3780 inc_amount = -inc_amount;
3782 inc = gen_int_mode (inc_amount, GET_MODE (value));
3785 if (! post && REG_P (incloc))
3786 result = incloc;
3787 else
3788 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3789 "INC/DEC result");
3791 if (real_in != result)
3793 /* First copy the location to the result register. */
3794 lra_assert (REG_P (result));
3795 emit_insn (gen_move_insn (result, real_in));
3798 /* We suppose that there are insns to add/sub with the constant
3799 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3800 old reload worked with this assumption. If the assumption
3801 becomes wrong, we should use approach in function
3802 base_plus_disp_to_reg. */
3803 if (in == value)
3805 /* See if we can directly increment INCLOC. */
3806 last = get_last_insn ();
3807 add_insn = emit_insn (plus_p
3808 ? gen_add2_insn (incloc, inc)
3809 : gen_sub2_insn (incloc, inc));
3811 code = recog_memoized (add_insn);
3812 if (code >= 0)
3814 if (! post && result != incloc)
3815 emit_insn (gen_move_insn (result, incloc));
3816 return result;
3818 delete_insns_since (last);
3821 /* If couldn't do the increment directly, must increment in RESULT.
3822 The way we do this depends on whether this is pre- or
3823 post-increment. For pre-increment, copy INCLOC to the reload
3824 register, increment it there, then save back. */
3825 if (! post)
3827 if (real_in != result)
3828 emit_insn (gen_move_insn (result, real_in));
3829 if (plus_p)
3830 emit_insn (gen_add2_insn (result, inc));
3831 else
3832 emit_insn (gen_sub2_insn (result, inc));
3833 if (result != incloc)
3834 emit_insn (gen_move_insn (incloc, result));
3836 else
3838 /* Post-increment.
3840 Because this might be a jump insn or a compare, and because
3841 RESULT may not be available after the insn in an input
3842 reload, we must do the incrementing before the insn being
3843 reloaded for.
3845 We have already copied IN to RESULT. Increment the copy in
3846 RESULT, save that back, then decrement RESULT so it has
3847 the original value. */
3848 if (plus_p)
3849 emit_insn (gen_add2_insn (result, inc));
3850 else
3851 emit_insn (gen_sub2_insn (result, inc));
3852 emit_insn (gen_move_insn (incloc, result));
3853 /* Restore non-modified value for the result. We prefer this
3854 way because it does not require an additional hard
3855 register. */
3856 if (plus_p)
3858 poly_int64 offset;
3859 if (poly_int_rtx_p (inc, &offset))
3860 emit_insn (gen_add2_insn (result,
3861 gen_int_mode (-offset,
3862 GET_MODE (result))));
3863 else
3864 emit_insn (gen_sub2_insn (result, inc));
3866 else
3867 emit_insn (gen_add2_insn (result, inc));
3869 return result;
3872 /* Return true if the current move insn does not need processing as we
3873 already know that it satisfies its constraints. */
3874 static bool
3875 simple_move_p (void)
3877 rtx dest, src;
3878 enum reg_class dclass, sclass;
3880 lra_assert (curr_insn_set != NULL_RTX);
3881 dest = SET_DEST (curr_insn_set);
3882 src = SET_SRC (curr_insn_set);
3884 /* If the instruction has multiple sets we need to process it even if it
3885 is single_set. This can happen if one or more of the SETs are dead.
3886 See PR73650. */
3887 if (multiple_sets (curr_insn))
3888 return false;
3890 return ((dclass = get_op_class (dest)) != NO_REGS
3891 && (sclass = get_op_class (src)) != NO_REGS
3892 /* The backend guarantees that register moves of cost 2
3893 never need reloads. */
3894 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3897 /* Swap operands NOP and NOP + 1. */
3898 static inline void
3899 swap_operands (int nop)
3901 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3902 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3903 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3904 std::swap (equiv_substition_p[nop], equiv_substition_p[nop + 1]);
3905 /* Swap the duplicates too. */
3906 lra_update_dup (curr_id, nop);
3907 lra_update_dup (curr_id, nop + 1);
3910 /* Main entry point of the constraint code: search the body of the
3911 current insn to choose the best alternative. It is mimicking insn
3912 alternative cost calculation model of former reload pass. That is
3913 because machine descriptions were written to use this model. This
3914 model can be changed in future. Make commutative operand exchange
3915 if it is chosen.
3917 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3918 constraints. Return true if any change happened during function
3919 call.
3921 If CHECK_ONLY_P is true then don't do any transformation. Just
3922 check that the insn satisfies all constraints. If the insn does
3923 not satisfy any constraint, return true. */
3924 static bool
3925 curr_insn_transform (bool check_only_p)
3927 int i, j, k;
3928 int n_operands;
3929 int n_alternatives;
3930 int n_outputs;
3931 int commutative;
3932 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3933 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3934 signed char outputs[MAX_RECOG_OPERANDS + 1];
3935 rtx_insn *before, *after;
3936 bool alt_p = false;
3937 /* Flag that the insn has been changed through a transformation. */
3938 bool change_p;
3939 bool sec_mem_p;
3940 bool use_sec_mem_p;
3941 int max_regno_before;
3942 int reused_alternative_num;
3944 curr_insn_set = single_set (curr_insn);
3945 if (curr_insn_set != NULL_RTX && simple_move_p ())
3947 /* We assume that the corresponding insn alternative has no
3948 earlier clobbers. If it is not the case, don't define move
3949 cost equal to 2 for the corresponding register classes. */
3950 lra_set_used_insn_alternative (curr_insn, LRA_NON_CLOBBERED_ALT);
3951 return false;
3954 no_input_reloads_p = no_output_reloads_p = false;
3955 goal_alt_number = -1;
3956 change_p = sec_mem_p = false;
3957 /* CALL_INSNs are not allowed to have any output reloads; neither
3958 are insns that SET cc0. Insns that use CC0 are not allowed to
3959 have any input reloads. */
3960 if (CALL_P (curr_insn))
3961 no_output_reloads_p = true;
3963 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3964 no_input_reloads_p = true;
3965 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3966 no_output_reloads_p = true;
3968 n_operands = curr_static_id->n_operands;
3969 n_alternatives = curr_static_id->n_alternatives;
3971 /* Just return "no reloads" if insn has no operands with
3972 constraints. */
3973 if (n_operands == 0 || n_alternatives == 0)
3974 return false;
3976 max_regno_before = max_reg_num ();
3978 for (i = 0; i < n_operands; i++)
3980 goal_alt_matched[i][0] = -1;
3981 goal_alt_matches[i] = -1;
3984 commutative = curr_static_id->commutative;
3986 /* Now see what we need for pseudos that didn't get hard regs or got
3987 the wrong kind of hard reg. For this, we must consider all the
3988 operands together against the register constraints. */
3990 best_losers = best_overall = INT_MAX;
3991 best_reload_sum = 0;
3993 curr_swapped = false;
3994 goal_alt_swapped = false;
3996 if (! check_only_p)
3997 /* Make equivalence substitution and memory subreg elimination
3998 before address processing because an address legitimacy can
3999 depend on memory mode. */
4000 for (i = 0; i < n_operands; i++)
4002 rtx op, subst, old;
4003 bool op_change_p = false;
4005 if (curr_static_id->operand[i].is_operator)
4006 continue;
4008 old = op = *curr_id->operand_loc[i];
4009 if (GET_CODE (old) == SUBREG)
4010 old = SUBREG_REG (old);
4011 subst = get_equiv_with_elimination (old, curr_insn);
4012 original_subreg_reg_mode[i] = VOIDmode;
4013 equiv_substition_p[i] = false;
4014 if (subst != old)
4016 equiv_substition_p[i] = true;
4017 subst = copy_rtx (subst);
4018 lra_assert (REG_P (old));
4019 if (GET_CODE (op) != SUBREG)
4020 *curr_id->operand_loc[i] = subst;
4021 else
4023 SUBREG_REG (op) = subst;
4024 if (GET_MODE (subst) == VOIDmode)
4025 original_subreg_reg_mode[i] = GET_MODE (old);
4027 if (lra_dump_file != NULL)
4029 fprintf (lra_dump_file,
4030 "Changing pseudo %d in operand %i of insn %u on equiv ",
4031 REGNO (old), i, INSN_UID (curr_insn));
4032 dump_value_slim (lra_dump_file, subst, 1);
4033 fprintf (lra_dump_file, "\n");
4035 op_change_p = change_p = true;
4037 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
4039 change_p = true;
4040 lra_update_dup (curr_id, i);
4044 /* Reload address registers and displacements. We do it before
4045 finding an alternative because of memory constraints. */
4046 before = after = NULL;
4047 for (i = 0; i < n_operands; i++)
4048 if (! curr_static_id->operand[i].is_operator
4049 && process_address (i, check_only_p, &before, &after))
4051 if (check_only_p)
4052 return true;
4053 change_p = true;
4054 lra_update_dup (curr_id, i);
4057 if (change_p)
4058 /* If we've changed the instruction then any alternative that
4059 we chose previously may no longer be valid. */
4060 lra_set_used_insn_alternative (curr_insn, LRA_UNKNOWN_ALT);
4062 if (! check_only_p && curr_insn_set != NULL_RTX
4063 && check_and_process_move (&change_p, &sec_mem_p))
4064 return change_p;
4066 try_swapped:
4068 reused_alternative_num = check_only_p ? LRA_UNKNOWN_ALT : curr_id->used_insn_alternative;
4069 if (lra_dump_file != NULL && reused_alternative_num >= 0)
4070 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
4071 reused_alternative_num, INSN_UID (curr_insn));
4073 if (process_alt_operands (reused_alternative_num))
4074 alt_p = true;
4076 if (check_only_p)
4077 return ! alt_p || best_losers != 0;
4079 /* If insn is commutative (it's safe to exchange a certain pair of
4080 operands) then we need to try each alternative twice, the second
4081 time matching those two operands as if we had exchanged them. To
4082 do this, really exchange them in operands.
4084 If we have just tried the alternatives the second time, return
4085 operands to normal and drop through. */
4087 if (reused_alternative_num < 0 && commutative >= 0)
4089 curr_swapped = !curr_swapped;
4090 if (curr_swapped)
4092 swap_operands (commutative);
4093 goto try_swapped;
4095 else
4096 swap_operands (commutative);
4099 if (! alt_p && ! sec_mem_p)
4101 /* No alternative works with reloads?? */
4102 if (INSN_CODE (curr_insn) >= 0)
4103 fatal_insn ("unable to generate reloads for:", curr_insn);
4104 error_for_asm (curr_insn,
4105 "inconsistent operand constraints in an %<asm%>");
4106 lra_asm_error_p = true;
4107 if (! JUMP_P (curr_insn))
4109 /* Avoid further trouble with this insn. Don't generate use
4110 pattern here as we could use the insn SP offset. */
4111 lra_set_insn_deleted (curr_insn);
4113 else
4115 lra_invalidate_insn_data (curr_insn);
4116 ira_nullify_asm_goto (curr_insn);
4117 lra_update_insn_regno_info (curr_insn);
4119 return true;
4122 /* If the best alternative is with operands 1 and 2 swapped, swap
4123 them. Update the operand numbers of any reloads already
4124 pushed. */
4126 if (goal_alt_swapped)
4128 if (lra_dump_file != NULL)
4129 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
4130 INSN_UID (curr_insn));
4132 /* Swap the duplicates too. */
4133 swap_operands (commutative);
4134 change_p = true;
4137 /* Some targets' TARGET_SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
4138 too conservatively. So we use the secondary memory only if there
4139 is no any alternative without reloads. */
4140 use_sec_mem_p = false;
4141 if (! alt_p)
4142 use_sec_mem_p = true;
4143 else if (sec_mem_p)
4145 for (i = 0; i < n_operands; i++)
4146 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
4147 break;
4148 use_sec_mem_p = i < n_operands;
4151 if (use_sec_mem_p)
4153 int in = -1, out = -1;
4154 rtx new_reg, src, dest, rld;
4155 machine_mode sec_mode, rld_mode;
4157 lra_assert (curr_insn_set != NULL_RTX && sec_mem_p);
4158 dest = SET_DEST (curr_insn_set);
4159 src = SET_SRC (curr_insn_set);
4160 for (i = 0; i < n_operands; i++)
4161 if (*curr_id->operand_loc[i] == dest)
4162 out = i;
4163 else if (*curr_id->operand_loc[i] == src)
4164 in = i;
4165 for (i = 0; i < curr_static_id->n_dups; i++)
4166 if (out < 0 && *curr_id->dup_loc[i] == dest)
4167 out = curr_static_id->dup_num[i];
4168 else if (in < 0 && *curr_id->dup_loc[i] == src)
4169 in = curr_static_id->dup_num[i];
4170 lra_assert (out >= 0 && in >= 0
4171 && curr_static_id->operand[out].type == OP_OUT
4172 && curr_static_id->operand[in].type == OP_IN);
4173 rld = partial_subreg_p (GET_MODE (src), GET_MODE (dest)) ? src : dest;
4174 rld_mode = GET_MODE (rld);
4175 sec_mode = targetm.secondary_memory_needed_mode (rld_mode);
4176 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
4177 NO_REGS, "secondary");
4178 /* If the mode is changed, it should be wider. */
4179 lra_assert (!partial_subreg_p (sec_mode, rld_mode));
4180 if (sec_mode != rld_mode)
4182 /* If the target says specifically to use another mode for
4183 secondary memory moves we cannot reuse the original
4184 insn. */
4185 after = emit_spill_move (false, new_reg, dest);
4186 lra_process_new_insns (curr_insn, NULL, after,
4187 "Inserting the sec. move");
4188 /* We may have non null BEFORE here (e.g. after address
4189 processing. */
4190 push_to_sequence (before);
4191 before = emit_spill_move (true, new_reg, src);
4192 emit_insn (before);
4193 before = get_insns ();
4194 end_sequence ();
4195 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
4196 lra_set_insn_deleted (curr_insn);
4198 else if (dest == rld)
4200 *curr_id->operand_loc[out] = new_reg;
4201 lra_update_dup (curr_id, out);
4202 after = emit_spill_move (false, new_reg, dest);
4203 lra_process_new_insns (curr_insn, NULL, after,
4204 "Inserting the sec. move");
4206 else
4208 *curr_id->operand_loc[in] = new_reg;
4209 lra_update_dup (curr_id, in);
4210 /* See comments above. */
4211 push_to_sequence (before);
4212 before = emit_spill_move (true, new_reg, src);
4213 emit_insn (before);
4214 before = get_insns ();
4215 end_sequence ();
4216 lra_process_new_insns (curr_insn, before, NULL,
4217 "Inserting the sec. move");
4219 lra_update_insn_regno_info (curr_insn);
4220 return true;
4223 lra_assert (goal_alt_number >= 0);
4224 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
4226 if (lra_dump_file != NULL)
4228 const char *p;
4230 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
4231 goal_alt_number, INSN_UID (curr_insn));
4232 for (i = 0; i < n_operands; i++)
4234 p = (curr_static_id->operand_alternative
4235 [goal_alt_number * n_operands + i].constraint);
4236 if (*p == '\0')
4237 continue;
4238 fprintf (lra_dump_file, " (%d) ", i);
4239 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
4240 fputc (*p, lra_dump_file);
4242 if (INSN_CODE (curr_insn) >= 0
4243 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
4244 fprintf (lra_dump_file, " {%s}", p);
4245 if (maybe_ne (curr_id->sp_offset, 0))
4247 fprintf (lra_dump_file, " (sp_off=");
4248 print_dec (curr_id->sp_offset, lra_dump_file);
4249 fprintf (lra_dump_file, ")");
4251 fprintf (lra_dump_file, "\n");
4254 /* Right now, for any pair of operands I and J that are required to
4255 match, with J < I, goal_alt_matches[I] is J. Add I to
4256 goal_alt_matched[J]. */
4258 for (i = 0; i < n_operands; i++)
4259 if ((j = goal_alt_matches[i]) >= 0)
4261 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
4263 /* We allow matching one output operand and several input
4264 operands. */
4265 lra_assert (k == 0
4266 || (curr_static_id->operand[j].type == OP_OUT
4267 && curr_static_id->operand[i].type == OP_IN
4268 && (curr_static_id->operand
4269 [goal_alt_matched[j][0]].type == OP_IN)));
4270 goal_alt_matched[j][k] = i;
4271 goal_alt_matched[j][k + 1] = -1;
4274 for (i = 0; i < n_operands; i++)
4275 goal_alt_win[i] |= goal_alt_match_win[i];
4277 /* Any constants that aren't allowed and can't be reloaded into
4278 registers are here changed into memory references. */
4279 for (i = 0; i < n_operands; i++)
4280 if (goal_alt_win[i])
4282 int regno;
4283 enum reg_class new_class;
4284 rtx reg = *curr_id->operand_loc[i];
4286 if (GET_CODE (reg) == SUBREG)
4287 reg = SUBREG_REG (reg);
4289 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
4291 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
4293 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
4295 lra_assert (ok_p);
4296 lra_change_class (regno, new_class, " Change to", true);
4300 else
4302 const char *constraint;
4303 char c;
4304 rtx op = *curr_id->operand_loc[i];
4305 rtx subreg = NULL_RTX;
4306 machine_mode mode = curr_operand_mode[i];
4308 if (GET_CODE (op) == SUBREG)
4310 subreg = op;
4311 op = SUBREG_REG (op);
4312 mode = GET_MODE (op);
4315 if (CONST_POOL_OK_P (mode, op)
4316 && ((targetm.preferred_reload_class
4317 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
4318 || no_input_reloads_p))
4320 rtx tem = force_const_mem (mode, op);
4322 change_p = true;
4323 if (subreg != NULL_RTX)
4324 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
4326 *curr_id->operand_loc[i] = tem;
4327 lra_update_dup (curr_id, i);
4328 process_address (i, false, &before, &after);
4330 /* If the alternative accepts constant pool refs directly
4331 there will be no reload needed at all. */
4332 if (subreg != NULL_RTX)
4333 continue;
4334 /* Skip alternatives before the one requested. */
4335 constraint = (curr_static_id->operand_alternative
4336 [goal_alt_number * n_operands + i].constraint);
4337 for (;
4338 (c = *constraint) && c != ',' && c != '#';
4339 constraint += CONSTRAINT_LEN (c, constraint))
4341 enum constraint_num cn = lookup_constraint (constraint);
4342 if ((insn_extra_memory_constraint (cn)
4343 || insn_extra_special_memory_constraint (cn))
4344 && satisfies_memory_constraint_p (tem, cn))
4345 break;
4347 if (c == '\0' || c == ',' || c == '#')
4348 continue;
4350 goal_alt_win[i] = true;
4354 n_outputs = 0;
4355 outputs[0] = -1;
4356 for (i = 0; i < n_operands; i++)
4358 int regno;
4359 bool optional_p = false;
4360 rtx old, new_reg;
4361 rtx op = *curr_id->operand_loc[i];
4363 if (goal_alt_win[i])
4365 if (goal_alt[i] == NO_REGS
4366 && REG_P (op)
4367 /* When we assign NO_REGS it means that we will not
4368 assign a hard register to the scratch pseudo by
4369 assigment pass and the scratch pseudo will be
4370 spilled. Spilled scratch pseudos are transformed
4371 back to scratches at the LRA end. */
4372 && ira_former_scratch_operand_p (curr_insn, i)
4373 && ira_former_scratch_p (REGNO (op)))
4375 int regno = REGNO (op);
4376 lra_change_class (regno, NO_REGS, " Change to", true);
4377 if (lra_get_regno_hard_regno (regno) >= 0)
4378 /* We don't have to mark all insn affected by the
4379 spilled pseudo as there is only one such insn, the
4380 current one. */
4381 reg_renumber[regno] = -1;
4382 lra_assert (bitmap_single_bit_set_p
4383 (&lra_reg_info[REGNO (op)].insn_bitmap));
4385 /* We can do an optional reload. If the pseudo got a hard
4386 reg, we might improve the code through inheritance. If
4387 it does not get a hard register we coalesce memory/memory
4388 moves later. Ignore move insns to avoid cycling. */
4389 if (! lra_simple_p
4390 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
4391 && goal_alt[i] != NO_REGS && REG_P (op)
4392 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
4393 && regno < new_regno_start
4394 && ! ira_former_scratch_p (regno)
4395 && reg_renumber[regno] < 0
4396 /* Check that the optional reload pseudo will be able to
4397 hold given mode value. */
4398 && ! (prohibited_class_reg_set_mode_p
4399 (goal_alt[i], reg_class_contents[goal_alt[i]],
4400 PSEUDO_REGNO_MODE (regno)))
4401 && (curr_insn_set == NULL_RTX
4402 || !((REG_P (SET_SRC (curr_insn_set))
4403 || MEM_P (SET_SRC (curr_insn_set))
4404 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
4405 && (REG_P (SET_DEST (curr_insn_set))
4406 || MEM_P (SET_DEST (curr_insn_set))
4407 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
4408 optional_p = true;
4409 else if (goal_alt_matched[i][0] != -1
4410 && curr_static_id->operand[i].type == OP_OUT
4411 && (curr_static_id->operand_alternative
4412 [goal_alt_number * n_operands + i].earlyclobber)
4413 && REG_P (op))
4415 for (j = 0; goal_alt_matched[i][j] != -1; j++)
4417 rtx op2 = *curr_id->operand_loc[goal_alt_matched[i][j]];
4419 if (REG_P (op2) && REGNO (op) != REGNO (op2))
4420 break;
4422 if (goal_alt_matched[i][j] != -1)
4424 /* Generate reloads for different output and matched
4425 input registers. This is the easiest way to avoid
4426 creation of non-existing register conflicts in
4427 lra-lives.c. */
4428 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i], &before,
4429 &after, TRUE);
4430 outputs[n_outputs++] = i;
4431 outputs[n_outputs] = -1;
4433 continue;
4435 else
4436 continue;
4439 /* Operands that match previous ones have already been handled. */
4440 if (goal_alt_matches[i] >= 0)
4441 continue;
4443 /* We should not have an operand with a non-offsettable address
4444 appearing where an offsettable address will do. It also may
4445 be a case when the address should be special in other words
4446 not a general one (e.g. it needs no index reg). */
4447 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
4449 enum reg_class rclass;
4450 rtx *loc = &XEXP (op, 0);
4451 enum rtx_code code = GET_CODE (*loc);
4453 push_to_sequence (before);
4454 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
4455 MEM, SCRATCH);
4456 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
4457 new_reg = emit_inc (rclass, *loc, *loc,
4458 /* This value does not matter for MODIFY. */
4459 GET_MODE_SIZE (GET_MODE (op)));
4460 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
4461 "offsetable address", &new_reg))
4463 rtx addr = *loc;
4464 enum rtx_code code = GET_CODE (addr);
4465 bool align_p = false;
4467 if (code == AND && CONST_INT_P (XEXP (addr, 1)))
4469 /* (and ... (const_int -X)) is used to align to X bytes. */
4470 align_p = true;
4471 addr = XEXP (*loc, 0);
4473 else
4474 addr = canonicalize_reload_addr (addr);
4476 lra_emit_move (new_reg, addr);
4477 if (align_p)
4478 emit_move_insn (new_reg, gen_rtx_AND (GET_MODE (new_reg), new_reg, XEXP (*loc, 1)));
4480 before = get_insns ();
4481 end_sequence ();
4482 *loc = new_reg;
4483 lra_update_dup (curr_id, i);
4485 else if (goal_alt_matched[i][0] == -1)
4487 machine_mode mode;
4488 rtx reg, *loc;
4489 int hard_regno;
4490 enum op_type type = curr_static_id->operand[i].type;
4492 loc = curr_id->operand_loc[i];
4493 mode = curr_operand_mode[i];
4494 if (GET_CODE (*loc) == SUBREG)
4496 reg = SUBREG_REG (*loc);
4497 poly_int64 byte = SUBREG_BYTE (*loc);
4498 if (REG_P (reg)
4499 /* Strict_low_part requires reloading the register and not
4500 just the subreg. Likewise for a strict subreg no wider
4501 than a word for WORD_REGISTER_OPERATIONS targets. */
4502 && (curr_static_id->operand[i].strict_low
4503 || (!paradoxical_subreg_p (mode, GET_MODE (reg))
4504 && (hard_regno
4505 = get_try_hard_regno (REGNO (reg))) >= 0
4506 && (simplify_subreg_regno
4507 (hard_regno,
4508 GET_MODE (reg), byte, mode) < 0)
4509 && (goal_alt[i] == NO_REGS
4510 || (simplify_subreg_regno
4511 (ira_class_hard_regs[goal_alt[i]][0],
4512 GET_MODE (reg), byte, mode) >= 0)))
4513 || (partial_subreg_p (mode, GET_MODE (reg))
4514 && known_le (GET_MODE_SIZE (GET_MODE (reg)),
4515 UNITS_PER_WORD)
4516 && WORD_REGISTER_OPERATIONS)))
4518 /* An OP_INOUT is required when reloading a subreg of a
4519 mode wider than a word to ensure that data beyond the
4520 word being reloaded is preserved. Also automatically
4521 ensure that strict_low_part reloads are made into
4522 OP_INOUT which should already be true from the backend
4523 constraints. */
4524 if (type == OP_OUT
4525 && (curr_static_id->operand[i].strict_low
4526 || read_modify_subreg_p (*loc)))
4527 type = OP_INOUT;
4528 loc = &SUBREG_REG (*loc);
4529 mode = GET_MODE (*loc);
4532 old = *loc;
4533 if (get_reload_reg (type, mode, old, goal_alt[i],
4534 loc != curr_id->operand_loc[i], "", &new_reg)
4535 && type != OP_OUT)
4537 push_to_sequence (before);
4538 lra_emit_move (new_reg, old);
4539 before = get_insns ();
4540 end_sequence ();
4542 *loc = new_reg;
4543 if (type != OP_IN
4544 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
4546 start_sequence ();
4547 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
4548 emit_insn (after);
4549 after = get_insns ();
4550 end_sequence ();
4551 *loc = new_reg;
4553 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
4554 if (goal_alt_dont_inherit_ops[j] == i)
4556 lra_set_regno_unique_value (REGNO (new_reg));
4557 break;
4559 lra_update_dup (curr_id, i);
4561 else if (curr_static_id->operand[i].type == OP_IN
4562 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4563 == OP_OUT
4564 || (curr_static_id->operand[goal_alt_matched[i][0]].type
4565 == OP_INOUT
4566 && (operands_match_p
4567 (*curr_id->operand_loc[i],
4568 *curr_id->operand_loc[goal_alt_matched[i][0]],
4569 -1)))))
4571 /* generate reloads for input and matched outputs. */
4572 match_inputs[0] = i;
4573 match_inputs[1] = -1;
4574 match_reload (goal_alt_matched[i][0], match_inputs, outputs,
4575 goal_alt[i], &before, &after,
4576 curr_static_id->operand_alternative
4577 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
4578 .earlyclobber);
4580 else if ((curr_static_id->operand[i].type == OP_OUT
4581 || (curr_static_id->operand[i].type == OP_INOUT
4582 && (operands_match_p
4583 (*curr_id->operand_loc[i],
4584 *curr_id->operand_loc[goal_alt_matched[i][0]],
4585 -1))))
4586 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4587 == OP_IN))
4588 /* Generate reloads for output and matched inputs. */
4589 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i], &before,
4590 &after, curr_static_id->operand_alternative
4591 [goal_alt_number * n_operands + i].earlyclobber);
4592 else if (curr_static_id->operand[i].type == OP_IN
4593 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4594 == OP_IN))
4596 /* Generate reloads for matched inputs. */
4597 match_inputs[0] = i;
4598 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
4599 match_inputs[j + 1] = k;
4600 match_inputs[j + 1] = -1;
4601 match_reload (-1, match_inputs, outputs, goal_alt[i], &before,
4602 &after, false);
4604 else
4605 /* We must generate code in any case when function
4606 process_alt_operands decides that it is possible. */
4607 gcc_unreachable ();
4609 /* Memorise processed outputs so that output remaining to be processed
4610 can avoid using the same register value (see match_reload). */
4611 if (curr_static_id->operand[i].type == OP_OUT)
4613 outputs[n_outputs++] = i;
4614 outputs[n_outputs] = -1;
4617 if (optional_p)
4619 rtx reg = op;
4621 lra_assert (REG_P (reg));
4622 regno = REGNO (reg);
4623 op = *curr_id->operand_loc[i]; /* Substitution. */
4624 if (GET_CODE (op) == SUBREG)
4625 op = SUBREG_REG (op);
4626 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
4627 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
4628 lra_reg_info[REGNO (op)].restore_rtx = reg;
4629 if (lra_dump_file != NULL)
4630 fprintf (lra_dump_file,
4631 " Making reload reg %d for reg %d optional\n",
4632 REGNO (op), regno);
4635 if (before != NULL_RTX || after != NULL_RTX
4636 || max_regno_before != max_reg_num ())
4637 change_p = true;
4638 if (change_p)
4640 lra_update_operator_dups (curr_id);
4641 /* Something changes -- process the insn. */
4642 lra_update_insn_regno_info (curr_insn);
4644 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
4645 return change_p;
4648 /* Return true if INSN satisfies all constraints. In other words, no
4649 reload insns are needed. */
4650 bool
4651 lra_constrain_insn (rtx_insn *insn)
4653 int saved_new_regno_start = new_regno_start;
4654 int saved_new_insn_uid_start = new_insn_uid_start;
4655 bool change_p;
4657 curr_insn = insn;
4658 curr_id = lra_get_insn_recog_data (curr_insn);
4659 curr_static_id = curr_id->insn_static_data;
4660 new_insn_uid_start = get_max_uid ();
4661 new_regno_start = max_reg_num ();
4662 change_p = curr_insn_transform (true);
4663 new_regno_start = saved_new_regno_start;
4664 new_insn_uid_start = saved_new_insn_uid_start;
4665 return ! change_p;
4668 /* Return true if X is in LIST. */
4669 static bool
4670 in_list_p (rtx x, rtx list)
4672 for (; list != NULL_RTX; list = XEXP (list, 1))
4673 if (XEXP (list, 0) == x)
4674 return true;
4675 return false;
4678 /* Return true if X contains an allocatable hard register (if
4679 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
4680 static bool
4681 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
4683 int i, j;
4684 const char *fmt;
4685 enum rtx_code code;
4687 code = GET_CODE (x);
4688 if (REG_P (x))
4690 int regno = REGNO (x);
4691 HARD_REG_SET alloc_regs;
4693 if (hard_reg_p)
4695 if (regno >= FIRST_PSEUDO_REGISTER)
4696 regno = lra_get_regno_hard_regno (regno);
4697 if (regno < 0)
4698 return false;
4699 alloc_regs = ~lra_no_alloc_regs;
4700 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
4702 else
4704 if (regno < FIRST_PSEUDO_REGISTER)
4705 return false;
4706 if (! spilled_p)
4707 return true;
4708 return lra_get_regno_hard_regno (regno) < 0;
4711 fmt = GET_RTX_FORMAT (code);
4712 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4714 if (fmt[i] == 'e')
4716 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4717 return true;
4719 else if (fmt[i] == 'E')
4721 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4722 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4723 return true;
4726 return false;
4729 /* Process all regs in location *LOC and change them on equivalent
4730 substitution. Return true if any change was done. */
4731 static bool
4732 loc_equivalence_change_p (rtx *loc)
4734 rtx subst, reg, x = *loc;
4735 bool result = false;
4736 enum rtx_code code = GET_CODE (x);
4737 const char *fmt;
4738 int i, j;
4740 if (code == SUBREG)
4742 reg = SUBREG_REG (x);
4743 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4744 && GET_MODE (subst) == VOIDmode)
4746 /* We cannot reload debug location. Simplify subreg here
4747 while we know the inner mode. */
4748 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4749 GET_MODE (reg), SUBREG_BYTE (x));
4750 return true;
4753 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4755 *loc = subst;
4756 return true;
4759 /* Scan all the operand sub-expressions. */
4760 fmt = GET_RTX_FORMAT (code);
4761 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4763 if (fmt[i] == 'e')
4764 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4765 else if (fmt[i] == 'E')
4766 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4767 result
4768 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4770 return result;
4773 /* Similar to loc_equivalence_change_p, but for use as
4774 simplify_replace_fn_rtx callback. DATA is insn for which the
4775 elimination is done. If it null we don't do the elimination. */
4776 static rtx
4777 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4779 if (!REG_P (loc))
4780 return NULL_RTX;
4782 rtx subst = (data == NULL
4783 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4784 if (subst != loc)
4785 return subst;
4787 return NULL_RTX;
4790 /* Maximum number of generated reload insns per an insn. It is for
4791 preventing this pass cycling in a bug case. */
4792 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4794 /* The current iteration number of this LRA pass. */
4795 int lra_constraint_iter;
4797 /* True if we should during assignment sub-pass check assignment
4798 correctness for all pseudos and spill some of them to correct
4799 conflicts. It can be necessary when we substitute equiv which
4800 needs checking register allocation correctness because the
4801 equivalent value contains allocatable hard registers, or when we
4802 restore multi-register pseudo, or when we change the insn code and
4803 its operand became INOUT operand when it was IN one before. */
4804 bool check_and_force_assignment_correctness_p;
4806 /* Return true if REGNO is referenced in more than one block. */
4807 static bool
4808 multi_block_pseudo_p (int regno)
4810 basic_block bb = NULL;
4811 unsigned int uid;
4812 bitmap_iterator bi;
4814 if (regno < FIRST_PSEUDO_REGISTER)
4815 return false;
4817 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4818 if (bb == NULL)
4819 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4820 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4821 return true;
4822 return false;
4825 /* Return true if LIST contains a deleted insn. */
4826 static bool
4827 contains_deleted_insn_p (rtx_insn_list *list)
4829 for (; list != NULL_RTX; list = list->next ())
4830 if (NOTE_P (list->insn ())
4831 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4832 return true;
4833 return false;
4836 /* Return true if X contains a pseudo dying in INSN. */
4837 static bool
4838 dead_pseudo_p (rtx x, rtx_insn *insn)
4840 int i, j;
4841 const char *fmt;
4842 enum rtx_code code;
4844 if (REG_P (x))
4845 return (insn != NULL_RTX
4846 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4847 code = GET_CODE (x);
4848 fmt = GET_RTX_FORMAT (code);
4849 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4851 if (fmt[i] == 'e')
4853 if (dead_pseudo_p (XEXP (x, i), insn))
4854 return true;
4856 else if (fmt[i] == 'E')
4858 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4859 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4860 return true;
4863 return false;
4866 /* Return true if INSN contains a dying pseudo in INSN right hand
4867 side. */
4868 static bool
4869 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4871 rtx set = single_set (insn);
4873 gcc_assert (set != NULL);
4874 return dead_pseudo_p (SET_SRC (set), insn);
4877 /* Return true if any init insn of REGNO contains a dying pseudo in
4878 insn right hand side. */
4879 static bool
4880 init_insn_rhs_dead_pseudo_p (int regno)
4882 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4884 if (insns == NULL)
4885 return false;
4886 for (; insns != NULL_RTX; insns = insns->next ())
4887 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4888 return true;
4889 return false;
4892 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4893 reverse only if we have one init insn with given REGNO as a
4894 source. */
4895 static bool
4896 reverse_equiv_p (int regno)
4898 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4899 rtx set;
4901 if (insns == NULL)
4902 return false;
4903 if (! INSN_P (insns->insn ())
4904 || insns->next () != NULL)
4905 return false;
4906 if ((set = single_set (insns->insn ())) == NULL_RTX)
4907 return false;
4908 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4911 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4912 call this function only for non-reverse equivalence. */
4913 static bool
4914 contains_reloaded_insn_p (int regno)
4916 rtx set;
4917 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4919 for (; list != NULL; list = list->next ())
4920 if ((set = single_set (list->insn ())) == NULL_RTX
4921 || ! REG_P (SET_DEST (set))
4922 || (int) REGNO (SET_DEST (set)) != regno)
4923 return true;
4924 return false;
4927 /* Entry function of LRA constraint pass. Return true if the
4928 constraint pass did change the code. */
4929 bool
4930 lra_constraints (bool first_p)
4932 bool changed_p;
4933 int i, hard_regno, new_insns_num;
4934 unsigned int min_len, new_min_len, uid;
4935 rtx set, x, reg, dest_reg;
4936 basic_block last_bb;
4937 bitmap_iterator bi;
4939 lra_constraint_iter++;
4940 if (lra_dump_file != NULL)
4941 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4942 lra_constraint_iter);
4943 changed_p = false;
4944 if (pic_offset_table_rtx
4945 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4946 check_and_force_assignment_correctness_p = true;
4947 else if (first_p)
4948 /* On the first iteration we should check IRA assignment
4949 correctness. In rare cases, the assignments can be wrong as
4950 early clobbers operands are ignored in IRA or usages of
4951 paradoxical sub-registers are not taken into account by
4952 IRA. */
4953 check_and_force_assignment_correctness_p = true;
4954 new_insn_uid_start = get_max_uid ();
4955 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4956 /* Mark used hard regs for target stack size calulations. */
4957 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4958 if (lra_reg_info[i].nrefs != 0
4959 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4961 int j, nregs;
4963 nregs = hard_regno_nregs (hard_regno, lra_reg_info[i].biggest_mode);
4964 for (j = 0; j < nregs; j++)
4965 df_set_regs_ever_live (hard_regno + j, true);
4967 /* Do elimination before the equivalence processing as we can spill
4968 some pseudos during elimination. */
4969 lra_eliminate (false, first_p);
4970 auto_bitmap equiv_insn_bitmap (&reg_obstack);
4971 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4972 if (lra_reg_info[i].nrefs != 0)
4974 ira_reg_equiv[i].profitable_p = true;
4975 reg = regno_reg_rtx[i];
4976 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4978 bool pseudo_p = contains_reg_p (x, false, false);
4980 /* After RTL transformation, we cannot guarantee that
4981 pseudo in the substitution was not reloaded which might
4982 make equivalence invalid. For example, in reverse
4983 equiv of p0
4985 p0 <- ...
4987 equiv_mem <- p0
4989 the memory address register was reloaded before the 2nd
4990 insn. */
4991 if ((! first_p && pseudo_p)
4992 /* We don't use DF for compilation speed sake. So it
4993 is problematic to update live info when we use an
4994 equivalence containing pseudos in more than one
4995 BB. */
4996 || (pseudo_p && multi_block_pseudo_p (i))
4997 /* If an init insn was deleted for some reason, cancel
4998 the equiv. We could update the equiv insns after
4999 transformations including an equiv insn deletion
5000 but it is not worthy as such cases are extremely
5001 rare. */
5002 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
5003 /* If it is not a reverse equivalence, we check that a
5004 pseudo in rhs of the init insn is not dying in the
5005 insn. Otherwise, the live info at the beginning of
5006 the corresponding BB might be wrong after we
5007 removed the insn. When the equiv can be a
5008 constant, the right hand side of the init insn can
5009 be a pseudo. */
5010 || (! reverse_equiv_p (i)
5011 && (init_insn_rhs_dead_pseudo_p (i)
5012 /* If we reloaded the pseudo in an equivalence
5013 init insn, we cannot remove the equiv init
5014 insns and the init insns might write into
5015 const memory in this case. */
5016 || contains_reloaded_insn_p (i)))
5017 /* Prevent access beyond equivalent memory for
5018 paradoxical subregs. */
5019 || (MEM_P (x)
5020 && maybe_gt (GET_MODE_SIZE (lra_reg_info[i].biggest_mode),
5021 GET_MODE_SIZE (GET_MODE (x))))
5022 || (pic_offset_table_rtx
5023 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
5024 && (targetm.preferred_reload_class
5025 (x, lra_get_allocno_class (i)) == NO_REGS))
5026 || contains_symbol_ref_p (x))))
5027 ira_reg_equiv[i].defined_p = false;
5028 if (contains_reg_p (x, false, true))
5029 ira_reg_equiv[i].profitable_p = false;
5030 if (get_equiv (reg) != reg)
5031 bitmap_ior_into (equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
5034 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5035 update_equiv (i);
5036 /* We should add all insns containing pseudos which should be
5037 substituted by their equivalences. */
5038 EXECUTE_IF_SET_IN_BITMAP (equiv_insn_bitmap, 0, uid, bi)
5039 lra_push_insn_by_uid (uid);
5040 min_len = lra_insn_stack_length ();
5041 new_insns_num = 0;
5042 last_bb = NULL;
5043 changed_p = false;
5044 while ((new_min_len = lra_insn_stack_length ()) != 0)
5046 curr_insn = lra_pop_insn ();
5047 --new_min_len;
5048 curr_bb = BLOCK_FOR_INSN (curr_insn);
5049 if (curr_bb != last_bb)
5051 last_bb = curr_bb;
5052 bb_reload_num = lra_curr_reload_num;
5054 if (min_len > new_min_len)
5056 min_len = new_min_len;
5057 new_insns_num = 0;
5059 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
5060 internal_error
5061 ("maximum number of generated reload insns per insn achieved (%d)",
5062 MAX_RELOAD_INSNS_NUMBER);
5063 new_insns_num++;
5064 if (DEBUG_INSN_P (curr_insn))
5066 /* We need to check equivalence in debug insn and change
5067 pseudo to the equivalent value if necessary. */
5068 curr_id = lra_get_insn_recog_data (curr_insn);
5069 if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn)))
5071 rtx old = *curr_id->operand_loc[0];
5072 *curr_id->operand_loc[0]
5073 = simplify_replace_fn_rtx (old, NULL_RTX,
5074 loc_equivalence_callback, curr_insn);
5075 if (old != *curr_id->operand_loc[0])
5077 lra_update_insn_regno_info (curr_insn);
5078 changed_p = true;
5082 else if (INSN_P (curr_insn))
5084 if ((set = single_set (curr_insn)) != NULL_RTX)
5086 dest_reg = SET_DEST (set);
5087 /* The equivalence pseudo could be set up as SUBREG in a
5088 case when it is a call restore insn in a mode
5089 different from the pseudo mode. */
5090 if (GET_CODE (dest_reg) == SUBREG)
5091 dest_reg = SUBREG_REG (dest_reg);
5092 if ((REG_P (dest_reg)
5093 && (x = get_equiv (dest_reg)) != dest_reg
5094 /* Remove insns which set up a pseudo whose value
5095 cannot be changed. Such insns might be not in
5096 init_insns because we don't update equiv data
5097 during insn transformations.
5099 As an example, let suppose that a pseudo got
5100 hard register and on the 1st pass was not
5101 changed to equivalent constant. We generate an
5102 additional insn setting up the pseudo because of
5103 secondary memory movement. Then the pseudo is
5104 spilled and we use the equiv constant. In this
5105 case we should remove the additional insn and
5106 this insn is not init_insns list. */
5107 && (! MEM_P (x) || MEM_READONLY_P (x)
5108 /* Check that this is actually an insn setting
5109 up the equivalence. */
5110 || in_list_p (curr_insn,
5111 ira_reg_equiv
5112 [REGNO (dest_reg)].init_insns)))
5113 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
5114 && in_list_p (curr_insn,
5115 ira_reg_equiv
5116 [REGNO (SET_SRC (set))].init_insns)))
5118 /* This is equiv init insn of pseudo which did not get a
5119 hard register -- remove the insn. */
5120 if (lra_dump_file != NULL)
5122 fprintf (lra_dump_file,
5123 " Removing equiv init insn %i (freq=%d)\n",
5124 INSN_UID (curr_insn),
5125 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
5126 dump_insn_slim (lra_dump_file, curr_insn);
5128 if (contains_reg_p (x, true, false))
5129 check_and_force_assignment_correctness_p = true;
5130 lra_set_insn_deleted (curr_insn);
5131 continue;
5134 curr_id = lra_get_insn_recog_data (curr_insn);
5135 curr_static_id = curr_id->insn_static_data;
5136 init_curr_insn_input_reloads ();
5137 init_curr_operand_mode ();
5138 if (curr_insn_transform (false))
5139 changed_p = true;
5140 /* Check non-transformed insns too for equiv change as USE
5141 or CLOBBER don't need reloads but can contain pseudos
5142 being changed on their equivalences. */
5143 else if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn))
5144 && loc_equivalence_change_p (&PATTERN (curr_insn)))
5146 lra_update_insn_regno_info (curr_insn);
5147 changed_p = true;
5152 /* If we used a new hard regno, changed_p should be true because the
5153 hard reg is assigned to a new pseudo. */
5154 if (flag_checking && !changed_p)
5156 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5157 if (lra_reg_info[i].nrefs != 0
5158 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
5160 int j, nregs = hard_regno_nregs (hard_regno,
5161 PSEUDO_REGNO_MODE (i));
5163 for (j = 0; j < nregs; j++)
5164 lra_assert (df_regs_ever_live_p (hard_regno + j));
5167 return changed_p;
5170 static void initiate_invariants (void);
5171 static void finish_invariants (void);
5173 /* Initiate the LRA constraint pass. It is done once per
5174 function. */
5175 void
5176 lra_constraints_init (void)
5178 initiate_invariants ();
5181 /* Finalize the LRA constraint pass. It is done once per
5182 function. */
5183 void
5184 lra_constraints_finish (void)
5186 finish_invariants ();
5191 /* Structure describes invariants for ineheritance. */
5192 struct lra_invariant
5194 /* The order number of the invariant. */
5195 int num;
5196 /* The invariant RTX. */
5197 rtx invariant_rtx;
5198 /* The origin insn of the invariant. */
5199 rtx_insn *insn;
5202 typedef lra_invariant invariant_t;
5203 typedef invariant_t *invariant_ptr_t;
5204 typedef const invariant_t *const_invariant_ptr_t;
5206 /* Pointer to the inheritance invariants. */
5207 static vec<invariant_ptr_t> invariants;
5209 /* Allocation pool for the invariants. */
5210 static object_allocator<lra_invariant> *invariants_pool;
5212 /* Hash table for the invariants. */
5213 static htab_t invariant_table;
5215 /* Hash function for INVARIANT. */
5216 static hashval_t
5217 invariant_hash (const void *invariant)
5219 rtx inv = ((const_invariant_ptr_t) invariant)->invariant_rtx;
5220 return lra_rtx_hash (inv);
5223 /* Equal function for invariants INVARIANT1 and INVARIANT2. */
5224 static int
5225 invariant_eq_p (const void *invariant1, const void *invariant2)
5227 rtx inv1 = ((const_invariant_ptr_t) invariant1)->invariant_rtx;
5228 rtx inv2 = ((const_invariant_ptr_t) invariant2)->invariant_rtx;
5230 return rtx_equal_p (inv1, inv2);
5233 /* Insert INVARIANT_RTX into the table if it is not there yet. Return
5234 invariant which is in the table. */
5235 static invariant_ptr_t
5236 insert_invariant (rtx invariant_rtx)
5238 void **entry_ptr;
5239 invariant_t invariant;
5240 invariant_ptr_t invariant_ptr;
5242 invariant.invariant_rtx = invariant_rtx;
5243 entry_ptr = htab_find_slot (invariant_table, &invariant, INSERT);
5244 if (*entry_ptr == NULL)
5246 invariant_ptr = invariants_pool->allocate ();
5247 invariant_ptr->invariant_rtx = invariant_rtx;
5248 invariant_ptr->insn = NULL;
5249 invariants.safe_push (invariant_ptr);
5250 *entry_ptr = (void *) invariant_ptr;
5252 return (invariant_ptr_t) *entry_ptr;
5255 /* Initiate the invariant table. */
5256 static void
5257 initiate_invariants (void)
5259 invariants.create (100);
5260 invariants_pool
5261 = new object_allocator<lra_invariant> ("Inheritance invariants");
5262 invariant_table = htab_create (100, invariant_hash, invariant_eq_p, NULL);
5265 /* Finish the invariant table. */
5266 static void
5267 finish_invariants (void)
5269 htab_delete (invariant_table);
5270 delete invariants_pool;
5271 invariants.release ();
5274 /* Make the invariant table empty. */
5275 static void
5276 clear_invariants (void)
5278 htab_empty (invariant_table);
5279 invariants_pool->release ();
5280 invariants.truncate (0);
5285 /* This page contains code to do inheritance/split
5286 transformations. */
5288 /* Number of reloads passed so far in current EBB. */
5289 static int reloads_num;
5291 /* Number of calls passed so far in current EBB. */
5292 static int calls_num;
5294 /* Index ID is the CALLS_NUM associated the last call we saw with
5295 ABI identifier ID. */
5296 static int last_call_for_abi[NUM_ABI_IDS];
5298 /* Which registers have been fully or partially clobbered by a call
5299 since they were last used. */
5300 static HARD_REG_SET full_and_partial_call_clobbers;
5302 /* Current reload pseudo check for validity of elements in
5303 USAGE_INSNS. */
5304 static int curr_usage_insns_check;
5306 /* Info about last usage of registers in EBB to do inheritance/split
5307 transformation. Inheritance transformation is done from a spilled
5308 pseudo and split transformations from a hard register or a pseudo
5309 assigned to a hard register. */
5310 struct usage_insns
5312 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
5313 value INSNS is valid. The insns is chain of optional debug insns
5314 and a finishing non-debug insn using the corresponding reg. The
5315 value is also used to mark the registers which are set up in the
5316 current insn. The negated insn uid is used for this. */
5317 int check;
5318 /* Value of global reloads_num at the last insn in INSNS. */
5319 int reloads_num;
5320 /* Value of global reloads_nums at the last insn in INSNS. */
5321 int calls_num;
5322 /* It can be true only for splitting. And it means that the restore
5323 insn should be put after insn given by the following member. */
5324 bool after_p;
5325 /* Next insns in the current EBB which use the original reg and the
5326 original reg value is not changed between the current insn and
5327 the next insns. In order words, e.g. for inheritance, if we need
5328 to use the original reg value again in the next insns we can try
5329 to use the value in a hard register from a reload insn of the
5330 current insn. */
5331 rtx insns;
5334 /* Map: regno -> corresponding pseudo usage insns. */
5335 static struct usage_insns *usage_insns;
5337 static void
5338 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
5340 usage_insns[regno].check = curr_usage_insns_check;
5341 usage_insns[regno].insns = insn;
5342 usage_insns[regno].reloads_num = reloads_num;
5343 usage_insns[regno].calls_num = calls_num;
5344 usage_insns[regno].after_p = after_p;
5345 if (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0)
5346 remove_from_hard_reg_set (&full_and_partial_call_clobbers,
5347 PSEUDO_REGNO_MODE (regno),
5348 reg_renumber[regno]);
5351 /* The function is used to form list REGNO usages which consists of
5352 optional debug insns finished by a non-debug insn using REGNO.
5353 RELOADS_NUM is current number of reload insns processed so far. */
5354 static void
5355 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
5357 rtx next_usage_insns;
5359 if (usage_insns[regno].check == curr_usage_insns_check
5360 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
5361 && DEBUG_INSN_P (insn))
5363 /* Check that we did not add the debug insn yet. */
5364 if (next_usage_insns != insn
5365 && (GET_CODE (next_usage_insns) != INSN_LIST
5366 || XEXP (next_usage_insns, 0) != insn))
5367 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
5368 next_usage_insns);
5370 else if (NONDEBUG_INSN_P (insn))
5371 setup_next_usage_insn (regno, insn, reloads_num, false);
5372 else
5373 usage_insns[regno].check = 0;
5376 /* Return first non-debug insn in list USAGE_INSNS. */
5377 static rtx_insn *
5378 skip_usage_debug_insns (rtx usage_insns)
5380 rtx insn;
5382 /* Skip debug insns. */
5383 for (insn = usage_insns;
5384 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
5385 insn = XEXP (insn, 1))
5387 return safe_as_a <rtx_insn *> (insn);
5390 /* Return true if we need secondary memory moves for insn in
5391 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
5392 into the insn. */
5393 static bool
5394 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
5395 rtx usage_insns ATTRIBUTE_UNUSED)
5397 rtx_insn *insn;
5398 rtx set, dest;
5399 enum reg_class cl;
5401 if (inher_cl == ALL_REGS
5402 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
5403 return false;
5404 lra_assert (INSN_P (insn));
5405 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
5406 return false;
5407 dest = SET_DEST (set);
5408 if (! REG_P (dest))
5409 return false;
5410 lra_assert (inher_cl != NO_REGS);
5411 cl = get_reg_class (REGNO (dest));
5412 return (cl != NO_REGS && cl != ALL_REGS
5413 && targetm.secondary_memory_needed (GET_MODE (dest), inher_cl, cl));
5416 /* Registers involved in inheritance/split in the current EBB
5417 (inheritance/split pseudos and original registers). */
5418 static bitmap_head check_only_regs;
5420 /* Reload pseudos cannot be involded in invariant inheritance in the
5421 current EBB. */
5422 static bitmap_head invalid_invariant_regs;
5424 /* Do inheritance transformations for insn INSN, which defines (if
5425 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
5426 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
5427 form as the "insns" field of usage_insns. Return true if we
5428 succeed in such transformation.
5430 The transformations look like:
5432 p <- ... i <- ...
5433 ... p <- i (new insn)
5434 ... =>
5435 <- ... p ... <- ... i ...
5437 ... i <- p (new insn)
5438 <- ... p ... <- ... i ...
5439 ... =>
5440 <- ... p ... <- ... i ...
5441 where p is a spilled original pseudo and i is a new inheritance pseudo.
5444 The inheritance pseudo has the smallest class of two classes CL and
5445 class of ORIGINAL REGNO. */
5446 static bool
5447 inherit_reload_reg (bool def_p, int original_regno,
5448 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
5450 if (optimize_function_for_size_p (cfun))
5451 return false;
5453 enum reg_class rclass = lra_get_allocno_class (original_regno);
5454 rtx original_reg = regno_reg_rtx[original_regno];
5455 rtx new_reg, usage_insn;
5456 rtx_insn *new_insns;
5458 lra_assert (! usage_insns[original_regno].after_p);
5459 if (lra_dump_file != NULL)
5460 fprintf (lra_dump_file,
5461 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
5462 if (! ira_reg_classes_intersect_p[cl][rclass])
5464 if (lra_dump_file != NULL)
5466 fprintf (lra_dump_file,
5467 " Rejecting inheritance for %d "
5468 "because of disjoint classes %s and %s\n",
5469 original_regno, reg_class_names[cl],
5470 reg_class_names[rclass]);
5471 fprintf (lra_dump_file,
5472 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5474 return false;
5476 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
5477 /* We don't use a subset of two classes because it can be
5478 NO_REGS. This transformation is still profitable in most
5479 cases even if the classes are not intersected as register
5480 move is probably cheaper than a memory load. */
5481 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
5483 if (lra_dump_file != NULL)
5484 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
5485 reg_class_names[cl], reg_class_names[rclass]);
5487 rclass = cl;
5489 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
5491 /* Reject inheritance resulting in secondary memory moves.
5492 Otherwise, there is a danger in LRA cycling. Also such
5493 transformation will be unprofitable. */
5494 if (lra_dump_file != NULL)
5496 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
5497 rtx set = single_set (insn);
5499 lra_assert (set != NULL_RTX);
5501 rtx dest = SET_DEST (set);
5503 lra_assert (REG_P (dest));
5504 fprintf (lra_dump_file,
5505 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
5506 "as secondary mem is needed\n",
5507 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
5508 original_regno, reg_class_names[rclass]);
5509 fprintf (lra_dump_file,
5510 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5512 return false;
5514 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
5515 rclass, "inheritance");
5516 start_sequence ();
5517 if (def_p)
5518 lra_emit_move (original_reg, new_reg);
5519 else
5520 lra_emit_move (new_reg, original_reg);
5521 new_insns = get_insns ();
5522 end_sequence ();
5523 if (NEXT_INSN (new_insns) != NULL_RTX)
5525 if (lra_dump_file != NULL)
5527 fprintf (lra_dump_file,
5528 " Rejecting inheritance %d->%d "
5529 "as it results in 2 or more insns:\n",
5530 original_regno, REGNO (new_reg));
5531 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
5532 fprintf (lra_dump_file,
5533 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5535 return false;
5537 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
5538 lra_update_insn_regno_info (insn);
5539 if (! def_p)
5540 /* We now have a new usage insn for original regno. */
5541 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
5542 if (lra_dump_file != NULL)
5543 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
5544 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
5545 lra_reg_info[REGNO (new_reg)].restore_rtx = regno_reg_rtx[original_regno];
5546 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5547 bitmap_set_bit (&check_only_regs, original_regno);
5548 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
5549 if (def_p)
5550 lra_process_new_insns (insn, NULL, new_insns,
5551 "Add original<-inheritance");
5552 else
5553 lra_process_new_insns (insn, new_insns, NULL,
5554 "Add inheritance<-original");
5555 while (next_usage_insns != NULL_RTX)
5557 if (GET_CODE (next_usage_insns) != INSN_LIST)
5559 usage_insn = next_usage_insns;
5560 lra_assert (NONDEBUG_INSN_P (usage_insn));
5561 next_usage_insns = NULL;
5563 else
5565 usage_insn = XEXP (next_usage_insns, 0);
5566 lra_assert (DEBUG_INSN_P (usage_insn));
5567 next_usage_insns = XEXP (next_usage_insns, 1);
5569 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
5570 DEBUG_INSN_P (usage_insn));
5571 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5572 if (lra_dump_file != NULL)
5574 basic_block bb = BLOCK_FOR_INSN (usage_insn);
5575 fprintf (lra_dump_file,
5576 " Inheritance reuse change %d->%d (bb%d):\n",
5577 original_regno, REGNO (new_reg),
5578 bb ? bb->index : -1);
5579 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5582 if (lra_dump_file != NULL)
5583 fprintf (lra_dump_file,
5584 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5585 return true;
5588 /* Return true if we need a caller save/restore for pseudo REGNO which
5589 was assigned to a hard register. */
5590 static inline bool
5591 need_for_call_save_p (int regno)
5593 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
5594 if (usage_insns[regno].calls_num < calls_num)
5596 unsigned int abis = 0;
5597 for (unsigned int i = 0; i < NUM_ABI_IDS; ++i)
5598 if (last_call_for_abi[i] > usage_insns[regno].calls_num)
5599 abis |= 1 << i;
5600 gcc_assert (abis);
5601 if (call_clobbered_in_region_p (abis, full_and_partial_call_clobbers,
5602 PSEUDO_REGNO_MODE (regno),
5603 reg_renumber[regno]))
5604 return true;
5606 return false;
5609 /* Global registers occurring in the current EBB. */
5610 static bitmap_head ebb_global_regs;
5612 /* Return true if we need a split for hard register REGNO or pseudo
5613 REGNO which was assigned to a hard register.
5614 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
5615 used for reloads since the EBB end. It is an approximation of the
5616 used hard registers in the split range. The exact value would
5617 require expensive calculations. If we were aggressive with
5618 splitting because of the approximation, the split pseudo will save
5619 the same hard register assignment and will be removed in the undo
5620 pass. We still need the approximation because too aggressive
5621 splitting would result in too inaccurate cost calculation in the
5622 assignment pass because of too many generated moves which will be
5623 probably removed in the undo pass. */
5624 static inline bool
5625 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
5627 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
5629 lra_assert (hard_regno >= 0);
5630 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
5631 /* Don't split eliminable hard registers, otherwise we can
5632 split hard registers like hard frame pointer, which
5633 lives on BB start/end according to DF-infrastructure,
5634 when there is a pseudo assigned to the register and
5635 living in the same BB. */
5636 && (regno >= FIRST_PSEUDO_REGISTER
5637 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
5638 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
5639 /* Don't split call clobbered hard regs living through
5640 calls, otherwise we might have a check problem in the
5641 assign sub-pass as in the most cases (exception is a
5642 situation when check_and_force_assignment_correctness_p value is
5643 true) the assign pass assumes that all pseudos living
5644 through calls are assigned to call saved hard regs. */
5645 && (regno >= FIRST_PSEUDO_REGISTER
5646 || !TEST_HARD_REG_BIT (full_and_partial_call_clobbers, regno))
5647 /* We need at least 2 reloads to make pseudo splitting
5648 profitable. We should provide hard regno splitting in
5649 any case to solve 1st insn scheduling problem when
5650 moving hard register definition up might result in
5651 impossibility to find hard register for reload pseudo of
5652 small register class. */
5653 && (usage_insns[regno].reloads_num
5654 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
5655 && (regno < FIRST_PSEUDO_REGISTER
5656 /* For short living pseudos, spilling + inheritance can
5657 be considered a substitution for splitting.
5658 Therefore we do not splitting for local pseudos. It
5659 decreases also aggressiveness of splitting. The
5660 minimal number of references is chosen taking into
5661 account that for 2 references splitting has no sense
5662 as we can just spill the pseudo. */
5663 || (regno >= FIRST_PSEUDO_REGISTER
5664 && lra_reg_info[regno].nrefs > 3
5665 && bitmap_bit_p (&ebb_global_regs, regno))))
5666 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
5669 /* Return class for the split pseudo created from original pseudo with
5670 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
5671 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
5672 results in no secondary memory movements. */
5673 static enum reg_class
5674 choose_split_class (enum reg_class allocno_class,
5675 int hard_regno ATTRIBUTE_UNUSED,
5676 machine_mode mode ATTRIBUTE_UNUSED)
5678 int i;
5679 enum reg_class cl, best_cl = NO_REGS;
5680 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
5681 = REGNO_REG_CLASS (hard_regno);
5683 if (! targetm.secondary_memory_needed (mode, allocno_class, allocno_class)
5684 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
5685 return allocno_class;
5686 for (i = 0;
5687 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
5688 i++)
5689 if (! targetm.secondary_memory_needed (mode, cl, hard_reg_class)
5690 && ! targetm.secondary_memory_needed (mode, hard_reg_class, cl)
5691 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
5692 && (best_cl == NO_REGS
5693 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
5694 best_cl = cl;
5695 return best_cl;
5698 /* Copy any equivalence information from ORIGINAL_REGNO to NEW_REGNO.
5699 It only makes sense to call this function if NEW_REGNO is always
5700 equal to ORIGINAL_REGNO. */
5702 static void
5703 lra_copy_reg_equiv (unsigned int new_regno, unsigned int original_regno)
5705 if (!ira_reg_equiv[original_regno].defined_p)
5706 return;
5708 ira_expand_reg_equiv ();
5709 ira_reg_equiv[new_regno].defined_p = true;
5710 if (ira_reg_equiv[original_regno].memory)
5711 ira_reg_equiv[new_regno].memory
5712 = copy_rtx (ira_reg_equiv[original_regno].memory);
5713 if (ira_reg_equiv[original_regno].constant)
5714 ira_reg_equiv[new_regno].constant
5715 = copy_rtx (ira_reg_equiv[original_regno].constant);
5716 if (ira_reg_equiv[original_regno].invariant)
5717 ira_reg_equiv[new_regno].invariant
5718 = copy_rtx (ira_reg_equiv[original_regno].invariant);
5721 /* Do split transformations for insn INSN, which defines or uses
5722 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
5723 the EBB next uses ORIGINAL_REGNO; it has the same form as the
5724 "insns" field of usage_insns. If TO is not NULL, we don't use
5725 usage_insns, we put restore insns after TO insn. It is a case when
5726 we call it from lra_split_hard_reg_for, outside the inheritance
5727 pass.
5729 The transformations look like:
5731 p <- ... p <- ...
5732 ... s <- p (new insn -- save)
5733 ... =>
5734 ... p <- s (new insn -- restore)
5735 <- ... p ... <- ... p ...
5737 <- ... p ... <- ... p ...
5738 ... s <- p (new insn -- save)
5739 ... =>
5740 ... p <- s (new insn -- restore)
5741 <- ... p ... <- ... p ...
5743 where p is an original pseudo got a hard register or a hard
5744 register and s is a new split pseudo. The save is put before INSN
5745 if BEFORE_P is true. Return true if we succeed in such
5746 transformation. */
5747 static bool
5748 split_reg (bool before_p, int original_regno, rtx_insn *insn,
5749 rtx next_usage_insns, rtx_insn *to)
5751 enum reg_class rclass;
5752 rtx original_reg;
5753 int hard_regno, nregs;
5754 rtx new_reg, usage_insn;
5755 rtx_insn *restore, *save;
5756 bool after_p;
5757 bool call_save_p;
5758 machine_mode mode;
5760 if (original_regno < FIRST_PSEUDO_REGISTER)
5762 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
5763 hard_regno = original_regno;
5764 call_save_p = false;
5765 nregs = 1;
5766 mode = lra_reg_info[hard_regno].biggest_mode;
5767 machine_mode reg_rtx_mode = GET_MODE (regno_reg_rtx[hard_regno]);
5768 /* A reg can have a biggest_mode of VOIDmode if it was only ever seen
5769 as part of a multi-word register. In that case, or if the biggest
5770 mode was larger than a register, just use the reg_rtx. Otherwise,
5771 limit the size to that of the biggest access in the function. */
5772 if (mode == VOIDmode
5773 || paradoxical_subreg_p (mode, reg_rtx_mode))
5775 original_reg = regno_reg_rtx[hard_regno];
5776 mode = reg_rtx_mode;
5778 else
5779 original_reg = gen_rtx_REG (mode, hard_regno);
5781 else
5783 mode = PSEUDO_REGNO_MODE (original_regno);
5784 hard_regno = reg_renumber[original_regno];
5785 nregs = hard_regno_nregs (hard_regno, mode);
5786 rclass = lra_get_allocno_class (original_regno);
5787 original_reg = regno_reg_rtx[original_regno];
5788 call_save_p = need_for_call_save_p (original_regno);
5790 lra_assert (hard_regno >= 0);
5791 if (lra_dump_file != NULL)
5792 fprintf (lra_dump_file,
5793 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
5795 if (call_save_p)
5797 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
5798 hard_regno_nregs (hard_regno, mode),
5799 mode);
5800 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
5802 else
5804 rclass = choose_split_class (rclass, hard_regno, mode);
5805 if (rclass == NO_REGS)
5807 if (lra_dump_file != NULL)
5809 fprintf (lra_dump_file,
5810 " Rejecting split of %d(%s): "
5811 "no good reg class for %d(%s)\n",
5812 original_regno,
5813 reg_class_names[lra_get_allocno_class (original_regno)],
5814 hard_regno,
5815 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
5816 fprintf
5817 (lra_dump_file,
5818 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5820 return false;
5822 /* Split_if_necessary can split hard registers used as part of a
5823 multi-register mode but splits each register individually. The
5824 mode used for each independent register may not be supported
5825 so reject the split. Splitting the wider mode should theoretically
5826 be possible but is not implemented. */
5827 if (!targetm.hard_regno_mode_ok (hard_regno, mode))
5829 if (lra_dump_file != NULL)
5831 fprintf (lra_dump_file,
5832 " Rejecting split of %d(%s): unsuitable mode %s\n",
5833 original_regno,
5834 reg_class_names[lra_get_allocno_class (original_regno)],
5835 GET_MODE_NAME (mode));
5836 fprintf
5837 (lra_dump_file,
5838 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5840 return false;
5842 new_reg = lra_create_new_reg (mode, original_reg, rclass, "split");
5843 reg_renumber[REGNO (new_reg)] = hard_regno;
5845 int new_regno = REGNO (new_reg);
5846 save = emit_spill_move (true, new_reg, original_reg);
5847 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
5849 if (lra_dump_file != NULL)
5851 fprintf
5852 (lra_dump_file,
5853 " Rejecting split %d->%d resulting in > 2 save insns:\n",
5854 original_regno, new_regno);
5855 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
5856 fprintf (lra_dump_file,
5857 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5859 return false;
5861 restore = emit_spill_move (false, new_reg, original_reg);
5862 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
5864 if (lra_dump_file != NULL)
5866 fprintf (lra_dump_file,
5867 " Rejecting split %d->%d "
5868 "resulting in > 2 restore insns:\n",
5869 original_regno, new_regno);
5870 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
5871 fprintf (lra_dump_file,
5872 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5874 return false;
5876 /* Transfer equivalence information to the spill register, so that
5877 if we fail to allocate the spill register, we have the option of
5878 rematerializing the original value instead of spilling to the stack. */
5879 if (!HARD_REGISTER_NUM_P (original_regno)
5880 && mode == PSEUDO_REGNO_MODE (original_regno))
5881 lra_copy_reg_equiv (new_regno, original_regno);
5882 lra_reg_info[new_regno].restore_rtx = regno_reg_rtx[original_regno];
5883 bitmap_set_bit (&lra_split_regs, new_regno);
5884 if (to != NULL)
5886 lra_assert (next_usage_insns == NULL);
5887 usage_insn = to;
5888 after_p = TRUE;
5890 else
5892 /* We need check_only_regs only inside the inheritance pass. */
5893 bitmap_set_bit (&check_only_regs, new_regno);
5894 bitmap_set_bit (&check_only_regs, original_regno);
5895 after_p = usage_insns[original_regno].after_p;
5896 for (;;)
5898 if (GET_CODE (next_usage_insns) != INSN_LIST)
5900 usage_insn = next_usage_insns;
5901 break;
5903 usage_insn = XEXP (next_usage_insns, 0);
5904 lra_assert (DEBUG_INSN_P (usage_insn));
5905 next_usage_insns = XEXP (next_usage_insns, 1);
5906 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
5907 true);
5908 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5909 if (lra_dump_file != NULL)
5911 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5912 original_regno, new_regno);
5913 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5917 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5918 lra_assert (usage_insn != insn || (after_p && before_p));
5919 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5920 after_p ? NULL : restore,
5921 after_p ? restore : NULL,
5922 call_save_p
5923 ? "Add reg<-save" : "Add reg<-split");
5924 lra_process_new_insns (insn, before_p ? save : NULL,
5925 before_p ? NULL : save,
5926 call_save_p
5927 ? "Add save<-reg" : "Add split<-reg");
5928 if (nregs > 1)
5929 /* If we are trying to split multi-register. We should check
5930 conflicts on the next assignment sub-pass. IRA can allocate on
5931 sub-register levels, LRA do this on pseudos level right now and
5932 this discrepancy may create allocation conflicts after
5933 splitting. */
5934 check_and_force_assignment_correctness_p = true;
5935 if (lra_dump_file != NULL)
5936 fprintf (lra_dump_file,
5937 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5938 return true;
5941 /* Split a hard reg for reload pseudo REGNO having RCLASS and living
5942 in the range [FROM, TO]. Return true if did a split. Otherwise,
5943 return false. */
5944 bool
5945 spill_hard_reg_in_range (int regno, enum reg_class rclass, rtx_insn *from, rtx_insn *to)
5947 int i, hard_regno;
5948 int rclass_size;
5949 rtx_insn *insn;
5950 unsigned int uid;
5951 bitmap_iterator bi;
5952 HARD_REG_SET ignore;
5954 lra_assert (from != NULL && to != NULL);
5955 CLEAR_HARD_REG_SET (ignore);
5956 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
5958 lra_insn_recog_data_t id = lra_insn_recog_data[uid];
5959 struct lra_static_insn_data *static_id = id->insn_static_data;
5960 struct lra_insn_reg *reg;
5962 for (reg = id->regs; reg != NULL; reg = reg->next)
5963 if (reg->regno < FIRST_PSEUDO_REGISTER)
5964 SET_HARD_REG_BIT (ignore, reg->regno);
5965 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
5966 SET_HARD_REG_BIT (ignore, reg->regno);
5968 rclass_size = ira_class_hard_regs_num[rclass];
5969 for (i = 0; i < rclass_size; i++)
5971 hard_regno = ira_class_hard_regs[rclass][i];
5972 if (! TEST_HARD_REG_BIT (lra_reg_info[regno].conflict_hard_regs, hard_regno)
5973 || TEST_HARD_REG_BIT (ignore, hard_regno))
5974 continue;
5975 for (insn = from; insn != NEXT_INSN (to); insn = NEXT_INSN (insn))
5977 struct lra_static_insn_data *static_id;
5978 struct lra_insn_reg *reg;
5980 if (!INSN_P (insn))
5981 continue;
5982 if (bitmap_bit_p (&lra_reg_info[hard_regno].insn_bitmap,
5983 INSN_UID (insn)))
5984 break;
5985 static_id = lra_get_insn_recog_data (insn)->insn_static_data;
5986 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
5987 if (reg->regno == hard_regno)
5988 break;
5989 if (reg != NULL)
5990 break;
5992 if (insn != NEXT_INSN (to))
5993 continue;
5994 if (split_reg (TRUE, hard_regno, from, NULL, to))
5995 return true;
5997 return false;
6000 /* Recognize that we need a split transformation for insn INSN, which
6001 defines or uses REGNO in its insn biggest MODE (we use it only if
6002 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
6003 hard registers which might be used for reloads since the EBB end.
6004 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
6005 uid before starting INSN processing. Return true if we succeed in
6006 such transformation. */
6007 static bool
6008 split_if_necessary (int regno, machine_mode mode,
6009 HARD_REG_SET potential_reload_hard_regs,
6010 bool before_p, rtx_insn *insn, int max_uid)
6012 bool res = false;
6013 int i, nregs = 1;
6014 rtx next_usage_insns;
6016 if (regno < FIRST_PSEUDO_REGISTER)
6017 nregs = hard_regno_nregs (regno, mode);
6018 for (i = 0; i < nregs; i++)
6019 if (usage_insns[regno + i].check == curr_usage_insns_check
6020 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
6021 /* To avoid processing the register twice or more. */
6022 && ((GET_CODE (next_usage_insns) != INSN_LIST
6023 && INSN_UID (next_usage_insns) < max_uid)
6024 || (GET_CODE (next_usage_insns) == INSN_LIST
6025 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
6026 && need_for_split_p (potential_reload_hard_regs, regno + i)
6027 && split_reg (before_p, regno + i, insn, next_usage_insns, NULL))
6028 res = true;
6029 return res;
6032 /* Return TRUE if rtx X is considered as an invariant for
6033 inheritance. */
6034 static bool
6035 invariant_p (const_rtx x)
6037 machine_mode mode;
6038 const char *fmt;
6039 enum rtx_code code;
6040 int i, j;
6042 if (side_effects_p (x))
6043 return false;
6045 code = GET_CODE (x);
6046 mode = GET_MODE (x);
6047 if (code == SUBREG)
6049 x = SUBREG_REG (x);
6050 code = GET_CODE (x);
6051 mode = wider_subreg_mode (mode, GET_MODE (x));
6054 if (MEM_P (x))
6055 return false;
6057 if (REG_P (x))
6059 int i, nregs, regno = REGNO (x);
6061 if (regno >= FIRST_PSEUDO_REGISTER || regno == STACK_POINTER_REGNUM
6062 || TEST_HARD_REG_BIT (eliminable_regset, regno)
6063 || GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
6064 return false;
6065 nregs = hard_regno_nregs (regno, mode);
6066 for (i = 0; i < nregs; i++)
6067 if (! fixed_regs[regno + i]
6068 /* A hard register may be clobbered in the current insn
6069 but we can ignore this case because if the hard
6070 register is used it should be set somewhere after the
6071 clobber. */
6072 || bitmap_bit_p (&invalid_invariant_regs, regno + i))
6073 return false;
6075 fmt = GET_RTX_FORMAT (code);
6076 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6078 if (fmt[i] == 'e')
6080 if (! invariant_p (XEXP (x, i)))
6081 return false;
6083 else if (fmt[i] == 'E')
6085 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6086 if (! invariant_p (XVECEXP (x, i, j)))
6087 return false;
6090 return true;
6093 /* We have 'dest_reg <- invariant'. Let us try to make an invariant
6094 inheritance transformation (using dest_reg instead invariant in a
6095 subsequent insn). */
6096 static bool
6097 process_invariant_for_inheritance (rtx dst_reg, rtx invariant_rtx)
6099 invariant_ptr_t invariant_ptr;
6100 rtx_insn *insn, *new_insns;
6101 rtx insn_set, insn_reg, new_reg;
6102 int insn_regno;
6103 bool succ_p = false;
6104 int dst_regno = REGNO (dst_reg);
6105 machine_mode dst_mode = GET_MODE (dst_reg);
6106 enum reg_class cl = lra_get_allocno_class (dst_regno), insn_reg_cl;
6108 invariant_ptr = insert_invariant (invariant_rtx);
6109 if ((insn = invariant_ptr->insn) != NULL_RTX)
6111 /* We have a subsequent insn using the invariant. */
6112 insn_set = single_set (insn);
6113 lra_assert (insn_set != NULL);
6114 insn_reg = SET_DEST (insn_set);
6115 lra_assert (REG_P (insn_reg));
6116 insn_regno = REGNO (insn_reg);
6117 insn_reg_cl = lra_get_allocno_class (insn_regno);
6119 if (dst_mode == GET_MODE (insn_reg)
6120 /* We should consider only result move reg insns which are
6121 cheap. */
6122 && targetm.register_move_cost (dst_mode, cl, insn_reg_cl) == 2
6123 && targetm.register_move_cost (dst_mode, cl, cl) == 2)
6125 if (lra_dump_file != NULL)
6126 fprintf (lra_dump_file,
6127 " [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[\n");
6128 new_reg = lra_create_new_reg (dst_mode, dst_reg,
6129 cl, "invariant inheritance");
6130 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
6131 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
6132 lra_reg_info[REGNO (new_reg)].restore_rtx = PATTERN (insn);
6133 start_sequence ();
6134 lra_emit_move (new_reg, dst_reg);
6135 new_insns = get_insns ();
6136 end_sequence ();
6137 lra_process_new_insns (curr_insn, NULL, new_insns,
6138 "Add invariant inheritance<-original");
6139 start_sequence ();
6140 lra_emit_move (SET_DEST (insn_set), new_reg);
6141 new_insns = get_insns ();
6142 end_sequence ();
6143 lra_process_new_insns (insn, NULL, new_insns,
6144 "Changing reload<-inheritance");
6145 lra_set_insn_deleted (insn);
6146 succ_p = true;
6147 if (lra_dump_file != NULL)
6149 fprintf (lra_dump_file,
6150 " Invariant inheritance reuse change %d (bb%d):\n",
6151 REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
6152 dump_insn_slim (lra_dump_file, insn);
6153 fprintf (lra_dump_file,
6154 " ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]\n");
6158 invariant_ptr->insn = curr_insn;
6159 return succ_p;
6162 /* Check only registers living at the current program point in the
6163 current EBB. */
6164 static bitmap_head live_regs;
6166 /* Update live info in EBB given by its HEAD and TAIL insns after
6167 inheritance/split transformation. The function removes dead moves
6168 too. */
6169 static void
6170 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
6172 unsigned int j;
6173 int i, regno;
6174 bool live_p;
6175 rtx_insn *prev_insn;
6176 rtx set;
6177 bool remove_p;
6178 basic_block last_bb, prev_bb, curr_bb;
6179 bitmap_iterator bi;
6180 struct lra_insn_reg *reg;
6181 edge e;
6182 edge_iterator ei;
6184 last_bb = BLOCK_FOR_INSN (tail);
6185 prev_bb = NULL;
6186 for (curr_insn = tail;
6187 curr_insn != PREV_INSN (head);
6188 curr_insn = prev_insn)
6190 prev_insn = PREV_INSN (curr_insn);
6191 /* We need to process empty blocks too. They contain
6192 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
6193 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
6194 continue;
6195 curr_bb = BLOCK_FOR_INSN (curr_insn);
6196 if (curr_bb != prev_bb)
6198 if (prev_bb != NULL)
6200 /* Update df_get_live_in (prev_bb): */
6201 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6202 if (bitmap_bit_p (&live_regs, j))
6203 bitmap_set_bit (df_get_live_in (prev_bb), j);
6204 else
6205 bitmap_clear_bit (df_get_live_in (prev_bb), j);
6207 if (curr_bb != last_bb)
6209 /* Update df_get_live_out (curr_bb): */
6210 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6212 live_p = bitmap_bit_p (&live_regs, j);
6213 if (! live_p)
6214 FOR_EACH_EDGE (e, ei, curr_bb->succs)
6215 if (bitmap_bit_p (df_get_live_in (e->dest), j))
6217 live_p = true;
6218 break;
6220 if (live_p)
6221 bitmap_set_bit (df_get_live_out (curr_bb), j);
6222 else
6223 bitmap_clear_bit (df_get_live_out (curr_bb), j);
6226 prev_bb = curr_bb;
6227 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
6229 if (! NONDEBUG_INSN_P (curr_insn))
6230 continue;
6231 curr_id = lra_get_insn_recog_data (curr_insn);
6232 curr_static_id = curr_id->insn_static_data;
6233 remove_p = false;
6234 if ((set = single_set (curr_insn)) != NULL_RTX
6235 && REG_P (SET_DEST (set))
6236 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
6237 && SET_DEST (set) != pic_offset_table_rtx
6238 && bitmap_bit_p (&check_only_regs, regno)
6239 && ! bitmap_bit_p (&live_regs, regno))
6240 remove_p = true;
6241 /* See which defined values die here. */
6242 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6243 if (reg->type == OP_OUT && ! reg->subreg_p)
6244 bitmap_clear_bit (&live_regs, reg->regno);
6245 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6246 if (reg->type == OP_OUT && ! reg->subreg_p)
6247 bitmap_clear_bit (&live_regs, reg->regno);
6248 if (curr_id->arg_hard_regs != NULL)
6249 /* Make clobbered argument hard registers die. */
6250 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6251 if (regno >= FIRST_PSEUDO_REGISTER)
6252 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
6253 /* Mark each used value as live. */
6254 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6255 if (reg->type != OP_OUT
6256 && bitmap_bit_p (&check_only_regs, reg->regno))
6257 bitmap_set_bit (&live_regs, reg->regno);
6258 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6259 if (reg->type != OP_OUT
6260 && bitmap_bit_p (&check_only_regs, reg->regno))
6261 bitmap_set_bit (&live_regs, reg->regno);
6262 if (curr_id->arg_hard_regs != NULL)
6263 /* Make used argument hard registers live. */
6264 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6265 if (regno < FIRST_PSEUDO_REGISTER
6266 && bitmap_bit_p (&check_only_regs, regno))
6267 bitmap_set_bit (&live_regs, regno);
6268 /* It is quite important to remove dead move insns because it
6269 means removing dead store. We don't need to process them for
6270 constraints. */
6271 if (remove_p)
6273 if (lra_dump_file != NULL)
6275 fprintf (lra_dump_file, " Removing dead insn:\n ");
6276 dump_insn_slim (lra_dump_file, curr_insn);
6278 lra_set_insn_deleted (curr_insn);
6283 /* The structure describes info to do an inheritance for the current
6284 insn. We need to collect such info first before doing the
6285 transformations because the transformations change the insn
6286 internal representation. */
6287 struct to_inherit
6289 /* Original regno. */
6290 int regno;
6291 /* Subsequent insns which can inherit original reg value. */
6292 rtx insns;
6295 /* Array containing all info for doing inheritance from the current
6296 insn. */
6297 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
6299 /* Number elements in the previous array. */
6300 static int to_inherit_num;
6302 /* Add inheritance info REGNO and INSNS. Their meaning is described in
6303 structure to_inherit. */
6304 static void
6305 add_to_inherit (int regno, rtx insns)
6307 int i;
6309 for (i = 0; i < to_inherit_num; i++)
6310 if (to_inherit[i].regno == regno)
6311 return;
6312 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
6313 to_inherit[to_inherit_num].regno = regno;
6314 to_inherit[to_inherit_num++].insns = insns;
6317 /* Return the last non-debug insn in basic block BB, or the block begin
6318 note if none. */
6319 static rtx_insn *
6320 get_last_insertion_point (basic_block bb)
6322 rtx_insn *insn;
6324 FOR_BB_INSNS_REVERSE (bb, insn)
6325 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
6326 return insn;
6327 gcc_unreachable ();
6330 /* Set up RES by registers living on edges FROM except the edge (FROM,
6331 TO) or by registers set up in a jump insn in BB FROM. */
6332 static void
6333 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
6335 rtx_insn *last;
6336 struct lra_insn_reg *reg;
6337 edge e;
6338 edge_iterator ei;
6340 lra_assert (to != NULL);
6341 bitmap_clear (res);
6342 FOR_EACH_EDGE (e, ei, from->succs)
6343 if (e->dest != to)
6344 bitmap_ior_into (res, df_get_live_in (e->dest));
6345 last = get_last_insertion_point (from);
6346 if (! JUMP_P (last))
6347 return;
6348 curr_id = lra_get_insn_recog_data (last);
6349 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6350 if (reg->type != OP_IN)
6351 bitmap_set_bit (res, reg->regno);
6354 /* Used as a temporary results of some bitmap calculations. */
6355 static bitmap_head temp_bitmap;
6357 /* We split for reloads of small class of hard regs. The following
6358 defines how many hard regs the class should have to be qualified as
6359 small. The code is mostly oriented to x86/x86-64 architecture
6360 where some insns need to use only specific register or pair of
6361 registers and these register can live in RTL explicitly, e.g. for
6362 parameter passing. */
6363 static const int max_small_class_regs_num = 2;
6365 /* Do inheritance/split transformations in EBB starting with HEAD and
6366 finishing on TAIL. We process EBB insns in the reverse order.
6367 Return true if we did any inheritance/split transformation in the
6368 EBB.
6370 We should avoid excessive splitting which results in worse code
6371 because of inaccurate cost calculations for spilling new split
6372 pseudos in such case. To achieve this we do splitting only if
6373 register pressure is high in given basic block and there are reload
6374 pseudos requiring hard registers. We could do more register
6375 pressure calculations at any given program point to avoid necessary
6376 splitting even more but it is to expensive and the current approach
6377 works well enough. */
6378 static bool
6379 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
6381 int i, src_regno, dst_regno, nregs;
6382 bool change_p, succ_p, update_reloads_num_p;
6383 rtx_insn *prev_insn, *last_insn;
6384 rtx next_usage_insns, curr_set;
6385 enum reg_class cl;
6386 struct lra_insn_reg *reg;
6387 basic_block last_processed_bb, curr_bb = NULL;
6388 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
6389 bitmap to_process;
6390 unsigned int j;
6391 bitmap_iterator bi;
6392 bool head_p, after_p;
6394 change_p = false;
6395 curr_usage_insns_check++;
6396 clear_invariants ();
6397 reloads_num = calls_num = 0;
6398 for (unsigned int i = 0; i < NUM_ABI_IDS; ++i)
6399 last_call_for_abi[i] = 0;
6400 CLEAR_HARD_REG_SET (full_and_partial_call_clobbers);
6401 bitmap_clear (&check_only_regs);
6402 bitmap_clear (&invalid_invariant_regs);
6403 last_processed_bb = NULL;
6404 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6405 live_hard_regs = eliminable_regset | lra_no_alloc_regs;
6406 /* We don't process new insns generated in the loop. */
6407 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
6409 prev_insn = PREV_INSN (curr_insn);
6410 if (BLOCK_FOR_INSN (curr_insn) != NULL)
6411 curr_bb = BLOCK_FOR_INSN (curr_insn);
6412 if (last_processed_bb != curr_bb)
6414 /* We are at the end of BB. Add qualified living
6415 pseudos for potential splitting. */
6416 to_process = df_get_live_out (curr_bb);
6417 if (last_processed_bb != NULL)
6419 /* We are somewhere in the middle of EBB. */
6420 get_live_on_other_edges (curr_bb, last_processed_bb,
6421 &temp_bitmap);
6422 to_process = &temp_bitmap;
6424 last_processed_bb = curr_bb;
6425 last_insn = get_last_insertion_point (curr_bb);
6426 after_p = (! JUMP_P (last_insn)
6427 && (! CALL_P (last_insn)
6428 || (find_reg_note (last_insn,
6429 REG_NORETURN, NULL_RTX) == NULL_RTX
6430 && ! SIBLING_CALL_P (last_insn))));
6431 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6432 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
6434 if ((int) j >= lra_constraint_new_regno_start)
6435 break;
6436 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
6438 if (j < FIRST_PSEUDO_REGISTER)
6439 SET_HARD_REG_BIT (live_hard_regs, j);
6440 else
6441 add_to_hard_reg_set (&live_hard_regs,
6442 PSEUDO_REGNO_MODE (j),
6443 reg_renumber[j]);
6444 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
6448 src_regno = dst_regno = -1;
6449 curr_set = single_set (curr_insn);
6450 if (curr_set != NULL_RTX && REG_P (SET_DEST (curr_set)))
6451 dst_regno = REGNO (SET_DEST (curr_set));
6452 if (curr_set != NULL_RTX && REG_P (SET_SRC (curr_set)))
6453 src_regno = REGNO (SET_SRC (curr_set));
6454 update_reloads_num_p = true;
6455 if (src_regno < lra_constraint_new_regno_start
6456 && src_regno >= FIRST_PSEUDO_REGISTER
6457 && reg_renumber[src_regno] < 0
6458 && dst_regno >= lra_constraint_new_regno_start
6459 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
6461 /* 'reload_pseudo <- original_pseudo'. */
6462 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6463 reloads_num++;
6464 update_reloads_num_p = false;
6465 succ_p = false;
6466 if (usage_insns[src_regno].check == curr_usage_insns_check
6467 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
6468 succ_p = inherit_reload_reg (false, src_regno, cl,
6469 curr_insn, next_usage_insns);
6470 if (succ_p)
6471 change_p = true;
6472 else
6473 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6474 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6475 potential_reload_hard_regs |= reg_class_contents[cl];
6477 else if (src_regno < 0
6478 && dst_regno >= lra_constraint_new_regno_start
6479 && invariant_p (SET_SRC (curr_set))
6480 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS
6481 && ! bitmap_bit_p (&invalid_invariant_regs, dst_regno)
6482 && ! bitmap_bit_p (&invalid_invariant_regs,
6483 ORIGINAL_REGNO(regno_reg_rtx[dst_regno])))
6485 /* 'reload_pseudo <- invariant'. */
6486 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6487 reloads_num++;
6488 update_reloads_num_p = false;
6489 if (process_invariant_for_inheritance (SET_DEST (curr_set), SET_SRC (curr_set)))
6490 change_p = true;
6491 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6492 potential_reload_hard_regs |= reg_class_contents[cl];
6494 else if (src_regno >= lra_constraint_new_regno_start
6495 && dst_regno < lra_constraint_new_regno_start
6496 && dst_regno >= FIRST_PSEUDO_REGISTER
6497 && reg_renumber[dst_regno] < 0
6498 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
6499 && usage_insns[dst_regno].check == curr_usage_insns_check
6500 && (next_usage_insns
6501 = usage_insns[dst_regno].insns) != NULL_RTX)
6503 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6504 reloads_num++;
6505 update_reloads_num_p = false;
6506 /* 'original_pseudo <- reload_pseudo'. */
6507 if (! JUMP_P (curr_insn)
6508 && inherit_reload_reg (true, dst_regno, cl,
6509 curr_insn, next_usage_insns))
6510 change_p = true;
6511 /* Invalidate. */
6512 usage_insns[dst_regno].check = 0;
6513 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6514 potential_reload_hard_regs |= reg_class_contents[cl];
6516 else if (INSN_P (curr_insn))
6518 int iter;
6519 int max_uid = get_max_uid ();
6521 curr_id = lra_get_insn_recog_data (curr_insn);
6522 curr_static_id = curr_id->insn_static_data;
6523 to_inherit_num = 0;
6524 /* Process insn definitions. */
6525 for (iter = 0; iter < 2; iter++)
6526 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6527 reg != NULL;
6528 reg = reg->next)
6529 if (reg->type != OP_IN
6530 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
6532 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
6533 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
6534 && usage_insns[dst_regno].check == curr_usage_insns_check
6535 && (next_usage_insns
6536 = usage_insns[dst_regno].insns) != NULL_RTX)
6538 struct lra_insn_reg *r;
6540 for (r = curr_id->regs; r != NULL; r = r->next)
6541 if (r->type != OP_OUT && r->regno == dst_regno)
6542 break;
6543 /* Don't do inheritance if the pseudo is also
6544 used in the insn. */
6545 if (r == NULL)
6546 /* We cannot do inheritance right now
6547 because the current insn reg info (chain
6548 regs) can change after that. */
6549 add_to_inherit (dst_regno, next_usage_insns);
6551 /* We cannot process one reg twice here because of
6552 usage_insns invalidation. */
6553 if ((dst_regno < FIRST_PSEUDO_REGISTER
6554 || reg_renumber[dst_regno] >= 0)
6555 && ! reg->subreg_p && reg->type != OP_IN)
6557 HARD_REG_SET s;
6559 if (split_if_necessary (dst_regno, reg->biggest_mode,
6560 potential_reload_hard_regs,
6561 false, curr_insn, max_uid))
6562 change_p = true;
6563 CLEAR_HARD_REG_SET (s);
6564 if (dst_regno < FIRST_PSEUDO_REGISTER)
6565 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
6566 else
6567 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
6568 reg_renumber[dst_regno]);
6569 live_hard_regs &= ~s;
6570 potential_reload_hard_regs &= ~s;
6572 /* We should invalidate potential inheritance or
6573 splitting for the current insn usages to the next
6574 usage insns (see code below) as the output pseudo
6575 prevents this. */
6576 if ((dst_regno >= FIRST_PSEUDO_REGISTER
6577 && reg_renumber[dst_regno] < 0)
6578 || (reg->type == OP_OUT && ! reg->subreg_p
6579 && (dst_regno < FIRST_PSEUDO_REGISTER
6580 || reg_renumber[dst_regno] >= 0)))
6582 /* Invalidate and mark definitions. */
6583 if (dst_regno >= FIRST_PSEUDO_REGISTER)
6584 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
6585 else
6587 nregs = hard_regno_nregs (dst_regno,
6588 reg->biggest_mode);
6589 for (i = 0; i < nregs; i++)
6590 usage_insns[dst_regno + i].check
6591 = -(int) INSN_UID (curr_insn);
6595 /* Process clobbered call regs. */
6596 if (curr_id->arg_hard_regs != NULL)
6597 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6598 if (dst_regno >= FIRST_PSEUDO_REGISTER)
6599 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
6600 = -(int) INSN_UID (curr_insn);
6601 if (! JUMP_P (curr_insn))
6602 for (i = 0; i < to_inherit_num; i++)
6603 if (inherit_reload_reg (true, to_inherit[i].regno,
6604 ALL_REGS, curr_insn,
6605 to_inherit[i].insns))
6606 change_p = true;
6607 if (CALL_P (curr_insn))
6609 rtx cheap, pat, dest;
6610 rtx_insn *restore;
6611 int regno, hard_regno;
6613 calls_num++;
6614 function_abi callee_abi = insn_callee_abi (curr_insn);
6615 last_call_for_abi[callee_abi.id ()] = calls_num;
6616 full_and_partial_call_clobbers
6617 |= callee_abi.full_and_partial_reg_clobbers ();
6618 if ((cheap = find_reg_note (curr_insn,
6619 REG_RETURNED, NULL_RTX)) != NULL_RTX
6620 && ((cheap = XEXP (cheap, 0)), true)
6621 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
6622 && (hard_regno = reg_renumber[regno]) >= 0
6623 && usage_insns[regno].check == curr_usage_insns_check
6624 /* If there are pending saves/restores, the
6625 optimization is not worth. */
6626 && usage_insns[regno].calls_num == calls_num - 1
6627 && callee_abi.clobbers_reg_p (GET_MODE (cheap), hard_regno))
6629 /* Restore the pseudo from the call result as
6630 REG_RETURNED note says that the pseudo value is
6631 in the call result and the pseudo is an argument
6632 of the call. */
6633 pat = PATTERN (curr_insn);
6634 if (GET_CODE (pat) == PARALLEL)
6635 pat = XVECEXP (pat, 0, 0);
6636 dest = SET_DEST (pat);
6637 /* For multiple return values dest is PARALLEL.
6638 Currently we handle only single return value case. */
6639 if (REG_P (dest))
6641 start_sequence ();
6642 emit_move_insn (cheap, copy_rtx (dest));
6643 restore = get_insns ();
6644 end_sequence ();
6645 lra_process_new_insns (curr_insn, NULL, restore,
6646 "Inserting call parameter restore");
6647 /* We don't need to save/restore of the pseudo from
6648 this call. */
6649 usage_insns[regno].calls_num = calls_num;
6650 remove_from_hard_reg_set
6651 (&full_and_partial_call_clobbers,
6652 GET_MODE (cheap), hard_regno);
6653 bitmap_set_bit (&check_only_regs, regno);
6657 to_inherit_num = 0;
6658 /* Process insn usages. */
6659 for (iter = 0; iter < 2; iter++)
6660 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6661 reg != NULL;
6662 reg = reg->next)
6663 if ((reg->type != OP_OUT
6664 || (reg->type == OP_OUT && reg->subreg_p))
6665 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
6667 if (src_regno >= FIRST_PSEUDO_REGISTER
6668 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
6670 if (usage_insns[src_regno].check == curr_usage_insns_check
6671 && (next_usage_insns
6672 = usage_insns[src_regno].insns) != NULL_RTX
6673 && NONDEBUG_INSN_P (curr_insn))
6674 add_to_inherit (src_regno, next_usage_insns);
6675 else if (usage_insns[src_regno].check
6676 != -(int) INSN_UID (curr_insn))
6677 /* Add usages but only if the reg is not set up
6678 in the same insn. */
6679 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6681 else if (src_regno < FIRST_PSEUDO_REGISTER
6682 || reg_renumber[src_regno] >= 0)
6684 bool before_p;
6685 rtx_insn *use_insn = curr_insn;
6687 before_p = (JUMP_P (curr_insn)
6688 || (CALL_P (curr_insn) && reg->type == OP_IN));
6689 if (NONDEBUG_INSN_P (curr_insn)
6690 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
6691 && split_if_necessary (src_regno, reg->biggest_mode,
6692 potential_reload_hard_regs,
6693 before_p, curr_insn, max_uid))
6695 if (reg->subreg_p)
6696 check_and_force_assignment_correctness_p = true;
6697 change_p = true;
6698 /* Invalidate. */
6699 usage_insns[src_regno].check = 0;
6700 if (before_p)
6701 use_insn = PREV_INSN (curr_insn);
6703 if (NONDEBUG_INSN_P (curr_insn))
6705 if (src_regno < FIRST_PSEUDO_REGISTER)
6706 add_to_hard_reg_set (&live_hard_regs,
6707 reg->biggest_mode, src_regno);
6708 else
6709 add_to_hard_reg_set (&live_hard_regs,
6710 PSEUDO_REGNO_MODE (src_regno),
6711 reg_renumber[src_regno]);
6713 if (src_regno >= FIRST_PSEUDO_REGISTER)
6714 add_next_usage_insn (src_regno, use_insn, reloads_num);
6715 else
6717 for (i = 0; i < hard_regno_nregs (src_regno, reg->biggest_mode); i++)
6718 add_next_usage_insn (src_regno + i, use_insn, reloads_num);
6722 /* Process used call regs. */
6723 if (curr_id->arg_hard_regs != NULL)
6724 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6725 if (src_regno < FIRST_PSEUDO_REGISTER)
6727 SET_HARD_REG_BIT (live_hard_regs, src_regno);
6728 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6730 for (i = 0; i < to_inherit_num; i++)
6732 src_regno = to_inherit[i].regno;
6733 if (inherit_reload_reg (false, src_regno, ALL_REGS,
6734 curr_insn, to_inherit[i].insns))
6735 change_p = true;
6736 else
6737 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6740 if (update_reloads_num_p
6741 && NONDEBUG_INSN_P (curr_insn) && curr_set != NULL_RTX)
6743 int regno = -1;
6744 if ((REG_P (SET_DEST (curr_set))
6745 && (regno = REGNO (SET_DEST (curr_set))) >= lra_constraint_new_regno_start
6746 && reg_renumber[regno] < 0
6747 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
6748 || (REG_P (SET_SRC (curr_set))
6749 && (regno = REGNO (SET_SRC (curr_set))) >= lra_constraint_new_regno_start
6750 && reg_renumber[regno] < 0
6751 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
6753 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6754 reloads_num++;
6755 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6756 potential_reload_hard_regs |= reg_class_contents[cl];
6759 if (NONDEBUG_INSN_P (curr_insn))
6761 int regno;
6763 /* Invalidate invariants with changed regs. */
6764 curr_id = lra_get_insn_recog_data (curr_insn);
6765 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6766 if (reg->type != OP_IN)
6768 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6769 bitmap_set_bit (&invalid_invariant_regs,
6770 ORIGINAL_REGNO (regno_reg_rtx[reg->regno]));
6772 curr_static_id = curr_id->insn_static_data;
6773 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6774 if (reg->type != OP_IN)
6775 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6776 if (curr_id->arg_hard_regs != NULL)
6777 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6778 if (regno >= FIRST_PSEUDO_REGISTER)
6779 bitmap_set_bit (&invalid_invariant_regs,
6780 regno - FIRST_PSEUDO_REGISTER);
6782 /* We reached the start of the current basic block. */
6783 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
6784 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
6786 /* We reached the beginning of the current block -- do
6787 rest of spliting in the current BB. */
6788 to_process = df_get_live_in (curr_bb);
6789 if (BLOCK_FOR_INSN (head) != curr_bb)
6791 /* We are somewhere in the middle of EBB. */
6792 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
6793 curr_bb, &temp_bitmap);
6794 to_process = &temp_bitmap;
6796 head_p = true;
6797 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
6799 if ((int) j >= lra_constraint_new_regno_start)
6800 break;
6801 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
6802 && usage_insns[j].check == curr_usage_insns_check
6803 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
6805 if (need_for_split_p (potential_reload_hard_regs, j))
6807 if (lra_dump_file != NULL && head_p)
6809 fprintf (lra_dump_file,
6810 " ----------------------------------\n");
6811 head_p = false;
6813 if (split_reg (false, j, bb_note (curr_bb),
6814 next_usage_insns, NULL))
6815 change_p = true;
6817 usage_insns[j].check = 0;
6822 return change_p;
6825 /* This value affects EBB forming. If probability of edge from EBB to
6826 a BB is not greater than the following value, we don't add the BB
6827 to EBB. */
6828 #define EBB_PROBABILITY_CUTOFF \
6829 ((REG_BR_PROB_BASE * param_lra_inheritance_ebb_probability_cutoff) / 100)
6831 /* Current number of inheritance/split iteration. */
6832 int lra_inheritance_iter;
6834 /* Entry function for inheritance/split pass. */
6835 void
6836 lra_inheritance (void)
6838 int i;
6839 basic_block bb, start_bb;
6840 edge e;
6842 lra_inheritance_iter++;
6843 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6844 return;
6845 timevar_push (TV_LRA_INHERITANCE);
6846 if (lra_dump_file != NULL)
6847 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
6848 lra_inheritance_iter);
6849 curr_usage_insns_check = 0;
6850 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
6851 for (i = 0; i < lra_constraint_new_regno_start; i++)
6852 usage_insns[i].check = 0;
6853 bitmap_initialize (&check_only_regs, &reg_obstack);
6854 bitmap_initialize (&invalid_invariant_regs, &reg_obstack);
6855 bitmap_initialize (&live_regs, &reg_obstack);
6856 bitmap_initialize (&temp_bitmap, &reg_obstack);
6857 bitmap_initialize (&ebb_global_regs, &reg_obstack);
6858 FOR_EACH_BB_FN (bb, cfun)
6860 start_bb = bb;
6861 if (lra_dump_file != NULL)
6862 fprintf (lra_dump_file, "EBB");
6863 /* Form a EBB starting with BB. */
6864 bitmap_clear (&ebb_global_regs);
6865 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
6866 for (;;)
6868 if (lra_dump_file != NULL)
6869 fprintf (lra_dump_file, " %d", bb->index);
6870 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
6871 || LABEL_P (BB_HEAD (bb->next_bb)))
6872 break;
6873 e = find_fallthru_edge (bb->succs);
6874 if (! e)
6875 break;
6876 if (e->probability.initialized_p ()
6877 && e->probability.to_reg_br_prob_base () < EBB_PROBABILITY_CUTOFF)
6878 break;
6879 bb = bb->next_bb;
6881 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
6882 if (lra_dump_file != NULL)
6883 fprintf (lra_dump_file, "\n");
6884 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
6885 /* Remember that the EBB head and tail can change in
6886 inherit_in_ebb. */
6887 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
6889 bitmap_release (&ebb_global_regs);
6890 bitmap_release (&temp_bitmap);
6891 bitmap_release (&live_regs);
6892 bitmap_release (&invalid_invariant_regs);
6893 bitmap_release (&check_only_regs);
6894 free (usage_insns);
6896 timevar_pop (TV_LRA_INHERITANCE);
6901 /* This page contains code to undo failed inheritance/split
6902 transformations. */
6904 /* Current number of iteration undoing inheritance/split. */
6905 int lra_undo_inheritance_iter;
6907 /* Fix BB live info LIVE after removing pseudos created on pass doing
6908 inheritance/split which are REMOVED_PSEUDOS. */
6909 static void
6910 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
6912 unsigned int regno;
6913 bitmap_iterator bi;
6915 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
6916 if (bitmap_clear_bit (live, regno)
6917 && REG_P (lra_reg_info[regno].restore_rtx))
6918 bitmap_set_bit (live, REGNO (lra_reg_info[regno].restore_rtx));
6921 /* Return regno of the (subreg of) REG. Otherwise, return a negative
6922 number. */
6923 static int
6924 get_regno (rtx reg)
6926 if (GET_CODE (reg) == SUBREG)
6927 reg = SUBREG_REG (reg);
6928 if (REG_P (reg))
6929 return REGNO (reg);
6930 return -1;
6933 /* Delete a move INSN with destination reg DREGNO and a previous
6934 clobber insn with the same regno. The inheritance/split code can
6935 generate moves with preceding clobber and when we delete such moves
6936 we should delete the clobber insn too to keep the correct life
6937 info. */
6938 static void
6939 delete_move_and_clobber (rtx_insn *insn, int dregno)
6941 rtx_insn *prev_insn = PREV_INSN (insn);
6943 lra_set_insn_deleted (insn);
6944 lra_assert (dregno >= 0);
6945 if (prev_insn != NULL && NONDEBUG_INSN_P (prev_insn)
6946 && GET_CODE (PATTERN (prev_insn)) == CLOBBER
6947 && dregno == get_regno (XEXP (PATTERN (prev_insn), 0)))
6948 lra_set_insn_deleted (prev_insn);
6951 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
6952 return true if we did any change. The undo transformations for
6953 inheritance looks like
6954 i <- i2
6955 p <- i => p <- i2
6956 or removing
6957 p <- i, i <- p, and i <- i3
6958 where p is original pseudo from which inheritance pseudo i was
6959 created, i and i3 are removed inheritance pseudos, i2 is another
6960 not removed inheritance pseudo. All split pseudos or other
6961 occurrences of removed inheritance pseudos are changed on the
6962 corresponding original pseudos.
6964 The function also schedules insns changed and created during
6965 inheritance/split pass for processing by the subsequent constraint
6966 pass. */
6967 static bool
6968 remove_inheritance_pseudos (bitmap remove_pseudos)
6970 basic_block bb;
6971 int regno, sregno, prev_sregno, dregno;
6972 rtx restore_rtx;
6973 rtx set, prev_set;
6974 rtx_insn *prev_insn;
6975 bool change_p, done_p;
6977 change_p = ! bitmap_empty_p (remove_pseudos);
6978 /* We cannot finish the function right away if CHANGE_P is true
6979 because we need to marks insns affected by previous
6980 inheritance/split pass for processing by the subsequent
6981 constraint pass. */
6982 FOR_EACH_BB_FN (bb, cfun)
6984 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
6985 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
6986 FOR_BB_INSNS_REVERSE (bb, curr_insn)
6988 if (! INSN_P (curr_insn))
6989 continue;
6990 done_p = false;
6991 sregno = dregno = -1;
6992 if (change_p && NONDEBUG_INSN_P (curr_insn)
6993 && (set = single_set (curr_insn)) != NULL_RTX)
6995 dregno = get_regno (SET_DEST (set));
6996 sregno = get_regno (SET_SRC (set));
6999 if (sregno >= 0 && dregno >= 0)
7001 if (bitmap_bit_p (remove_pseudos, dregno)
7002 && ! REG_P (lra_reg_info[dregno].restore_rtx))
7004 /* invariant inheritance pseudo <- original pseudo */
7005 if (lra_dump_file != NULL)
7007 fprintf (lra_dump_file, " Removing invariant inheritance:\n");
7008 dump_insn_slim (lra_dump_file, curr_insn);
7009 fprintf (lra_dump_file, "\n");
7011 delete_move_and_clobber (curr_insn, dregno);
7012 done_p = true;
7014 else if (bitmap_bit_p (remove_pseudos, sregno)
7015 && ! REG_P (lra_reg_info[sregno].restore_rtx))
7017 /* reload pseudo <- invariant inheritance pseudo */
7018 start_sequence ();
7019 /* We cannot just change the source. It might be
7020 an insn different from the move. */
7021 emit_insn (lra_reg_info[sregno].restore_rtx);
7022 rtx_insn *new_insns = get_insns ();
7023 end_sequence ();
7024 lra_assert (single_set (new_insns) != NULL
7025 && SET_DEST (set) == SET_DEST (single_set (new_insns)));
7026 lra_process_new_insns (curr_insn, NULL, new_insns,
7027 "Changing reload<-invariant inheritance");
7028 delete_move_and_clobber (curr_insn, dregno);
7029 done_p = true;
7031 else if ((bitmap_bit_p (remove_pseudos, sregno)
7032 && (get_regno (lra_reg_info[sregno].restore_rtx) == dregno
7033 || (bitmap_bit_p (remove_pseudos, dregno)
7034 && get_regno (lra_reg_info[sregno].restore_rtx) >= 0
7035 && (get_regno (lra_reg_info[sregno].restore_rtx)
7036 == get_regno (lra_reg_info[dregno].restore_rtx)))))
7037 || (bitmap_bit_p (remove_pseudos, dregno)
7038 && get_regno (lra_reg_info[dregno].restore_rtx) == sregno))
7039 /* One of the following cases:
7040 original <- removed inheritance pseudo
7041 removed inherit pseudo <- another removed inherit pseudo
7042 removed inherit pseudo <- original pseudo
7044 removed_split_pseudo <- original_reg
7045 original_reg <- removed_split_pseudo */
7047 if (lra_dump_file != NULL)
7049 fprintf (lra_dump_file, " Removing %s:\n",
7050 bitmap_bit_p (&lra_split_regs, sregno)
7051 || bitmap_bit_p (&lra_split_regs, dregno)
7052 ? "split" : "inheritance");
7053 dump_insn_slim (lra_dump_file, curr_insn);
7055 delete_move_and_clobber (curr_insn, dregno);
7056 done_p = true;
7058 else if (bitmap_bit_p (remove_pseudos, sregno)
7059 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
7061 /* Search the following pattern:
7062 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
7063 original_pseudo <- inherit_or_split_pseudo1
7064 where the 2nd insn is the current insn and
7065 inherit_or_split_pseudo2 is not removed. If it is found,
7066 change the current insn onto:
7067 original_pseudo <- inherit_or_split_pseudo2. */
7068 for (prev_insn = PREV_INSN (curr_insn);
7069 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
7070 prev_insn = PREV_INSN (prev_insn))
7072 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
7073 && (prev_set = single_set (prev_insn)) != NULL_RTX
7074 /* There should be no subregs in insn we are
7075 searching because only the original reg might
7076 be in subreg when we changed the mode of
7077 load/store for splitting. */
7078 && REG_P (SET_DEST (prev_set))
7079 && REG_P (SET_SRC (prev_set))
7080 && (int) REGNO (SET_DEST (prev_set)) == sregno
7081 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
7082 >= FIRST_PSEUDO_REGISTER)
7083 && (lra_reg_info[prev_sregno].restore_rtx == NULL_RTX
7085 /* As we consider chain of inheritance or
7086 splitting described in above comment we should
7087 check that sregno and prev_sregno were
7088 inheritance/split pseudos created from the
7089 same original regno. */
7090 (get_regno (lra_reg_info[sregno].restore_rtx) >= 0
7091 && (get_regno (lra_reg_info[sregno].restore_rtx)
7092 == get_regno (lra_reg_info[prev_sregno].restore_rtx))))
7093 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
7095 lra_assert (GET_MODE (SET_SRC (prev_set))
7096 == GET_MODE (regno_reg_rtx[sregno]));
7097 /* Although we have a single set, the insn can
7098 contain more one sregno register occurrence
7099 as a source. Change all occurrences. */
7100 lra_substitute_pseudo_within_insn (curr_insn, sregno,
7101 SET_SRC (prev_set),
7102 false);
7103 /* As we are finishing with processing the insn
7104 here, check the destination too as it might
7105 inheritance pseudo for another pseudo. */
7106 if (bitmap_bit_p (remove_pseudos, dregno)
7107 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
7108 && (restore_rtx
7109 = lra_reg_info[dregno].restore_rtx) != NULL_RTX)
7111 if (GET_CODE (SET_DEST (set)) == SUBREG)
7112 SUBREG_REG (SET_DEST (set)) = restore_rtx;
7113 else
7114 SET_DEST (set) = restore_rtx;
7116 lra_push_insn_and_update_insn_regno_info (curr_insn);
7117 lra_set_used_insn_alternative_by_uid
7118 (INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
7119 done_p = true;
7120 if (lra_dump_file != NULL)
7122 fprintf (lra_dump_file, " Change reload insn:\n");
7123 dump_insn_slim (lra_dump_file, curr_insn);
7128 if (! done_p)
7130 struct lra_insn_reg *reg;
7131 bool restored_regs_p = false;
7132 bool kept_regs_p = false;
7134 curr_id = lra_get_insn_recog_data (curr_insn);
7135 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
7137 regno = reg->regno;
7138 restore_rtx = lra_reg_info[regno].restore_rtx;
7139 if (restore_rtx != NULL_RTX)
7141 if (change_p && bitmap_bit_p (remove_pseudos, regno))
7143 lra_substitute_pseudo_within_insn
7144 (curr_insn, regno, restore_rtx, false);
7145 restored_regs_p = true;
7147 else
7148 kept_regs_p = true;
7151 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
7153 /* The instruction has changed since the previous
7154 constraints pass. */
7155 lra_push_insn_and_update_insn_regno_info (curr_insn);
7156 lra_set_used_insn_alternative_by_uid
7157 (INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
7159 else if (restored_regs_p)
7160 /* The instruction has been restored to the form that
7161 it had during the previous constraints pass. */
7162 lra_update_insn_regno_info (curr_insn);
7163 if (restored_regs_p && lra_dump_file != NULL)
7165 fprintf (lra_dump_file, " Insn after restoring regs:\n");
7166 dump_insn_slim (lra_dump_file, curr_insn);
7171 return change_p;
7174 /* If optional reload pseudos failed to get a hard register or was not
7175 inherited, it is better to remove optional reloads. We do this
7176 transformation after undoing inheritance to figure out necessity to
7177 remove optional reloads easier. Return true if we do any
7178 change. */
7179 static bool
7180 undo_optional_reloads (void)
7182 bool change_p, keep_p;
7183 unsigned int regno, uid;
7184 bitmap_iterator bi, bi2;
7185 rtx_insn *insn;
7186 rtx set, src, dest;
7187 auto_bitmap removed_optional_reload_pseudos (&reg_obstack);
7189 bitmap_copy (removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
7190 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7192 keep_p = false;
7193 /* Keep optional reloads from previous subpasses. */
7194 if (lra_reg_info[regno].restore_rtx == NULL_RTX
7195 /* If the original pseudo changed its allocation, just
7196 removing the optional pseudo is dangerous as the original
7197 pseudo will have longer live range. */
7198 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] >= 0)
7199 keep_p = true;
7200 else if (reg_renumber[regno] >= 0)
7201 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
7203 insn = lra_insn_recog_data[uid]->insn;
7204 if ((set = single_set (insn)) == NULL_RTX)
7205 continue;
7206 src = SET_SRC (set);
7207 dest = SET_DEST (set);
7208 if (! REG_P (src) || ! REG_P (dest))
7209 continue;
7210 if (REGNO (dest) == regno
7211 /* Ignore insn for optional reloads itself. */
7212 && REGNO (lra_reg_info[regno].restore_rtx) != REGNO (src)
7213 /* Check only inheritance on last inheritance pass. */
7214 && (int) REGNO (src) >= new_regno_start
7215 /* Check that the optional reload was inherited. */
7216 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
7218 keep_p = true;
7219 break;
7222 if (keep_p)
7224 bitmap_clear_bit (removed_optional_reload_pseudos, regno);
7225 if (lra_dump_file != NULL)
7226 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
7229 change_p = ! bitmap_empty_p (removed_optional_reload_pseudos);
7230 auto_bitmap insn_bitmap (&reg_obstack);
7231 EXECUTE_IF_SET_IN_BITMAP (removed_optional_reload_pseudos, 0, regno, bi)
7233 if (lra_dump_file != NULL)
7234 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
7235 bitmap_copy (insn_bitmap, &lra_reg_info[regno].insn_bitmap);
7236 EXECUTE_IF_SET_IN_BITMAP (insn_bitmap, 0, uid, bi2)
7238 insn = lra_insn_recog_data[uid]->insn;
7239 if ((set = single_set (insn)) != NULL_RTX)
7241 src = SET_SRC (set);
7242 dest = SET_DEST (set);
7243 if (REG_P (src) && REG_P (dest)
7244 && ((REGNO (src) == regno
7245 && (REGNO (lra_reg_info[regno].restore_rtx)
7246 == REGNO (dest)))
7247 || (REGNO (dest) == regno
7248 && (REGNO (lra_reg_info[regno].restore_rtx)
7249 == REGNO (src)))))
7251 if (lra_dump_file != NULL)
7253 fprintf (lra_dump_file, " Deleting move %u\n",
7254 INSN_UID (insn));
7255 dump_insn_slim (lra_dump_file, insn);
7257 delete_move_and_clobber (insn, REGNO (dest));
7258 continue;
7260 /* We should not worry about generation memory-memory
7261 moves here as if the corresponding inheritance did
7262 not work (inheritance pseudo did not get a hard reg),
7263 we remove the inheritance pseudo and the optional
7264 reload. */
7266 lra_substitute_pseudo_within_insn
7267 (insn, regno, lra_reg_info[regno].restore_rtx, false);
7268 lra_update_insn_regno_info (insn);
7269 if (lra_dump_file != NULL)
7271 fprintf (lra_dump_file,
7272 " Restoring original insn:\n");
7273 dump_insn_slim (lra_dump_file, insn);
7277 /* Clear restore_regnos. */
7278 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7279 lra_reg_info[regno].restore_rtx = NULL_RTX;
7280 return change_p;
7283 /* Entry function for undoing inheritance/split transformation. Return true
7284 if we did any RTL change in this pass. */
7285 bool
7286 lra_undo_inheritance (void)
7288 unsigned int regno;
7289 int hard_regno;
7290 int n_all_inherit, n_inherit, n_all_split, n_split;
7291 rtx restore_rtx;
7292 bitmap_iterator bi;
7293 bool change_p;
7295 lra_undo_inheritance_iter++;
7296 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
7297 return false;
7298 if (lra_dump_file != NULL)
7299 fprintf (lra_dump_file,
7300 "\n********** Undoing inheritance #%d: **********\n\n",
7301 lra_undo_inheritance_iter);
7302 auto_bitmap remove_pseudos (&reg_obstack);
7303 n_inherit = n_all_inherit = 0;
7304 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7305 if (lra_reg_info[regno].restore_rtx != NULL_RTX)
7307 n_all_inherit++;
7308 if (reg_renumber[regno] < 0
7309 /* If the original pseudo changed its allocation, just
7310 removing inheritance is dangerous as for changing
7311 allocation we used shorter live-ranges. */
7312 && (! REG_P (lra_reg_info[regno].restore_rtx)
7313 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] < 0))
7314 bitmap_set_bit (remove_pseudos, regno);
7315 else
7316 n_inherit++;
7318 if (lra_dump_file != NULL && n_all_inherit != 0)
7319 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
7320 n_inherit, n_all_inherit,
7321 (double) n_inherit / n_all_inherit * 100);
7322 n_split = n_all_split = 0;
7323 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7324 if ((restore_rtx = lra_reg_info[regno].restore_rtx) != NULL_RTX)
7326 int restore_regno = REGNO (restore_rtx);
7328 n_all_split++;
7329 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
7330 ? reg_renumber[restore_regno] : restore_regno);
7331 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
7332 bitmap_set_bit (remove_pseudos, regno);
7333 else
7335 n_split++;
7336 if (lra_dump_file != NULL)
7337 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
7338 regno, restore_regno);
7341 if (lra_dump_file != NULL && n_all_split != 0)
7342 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
7343 n_split, n_all_split,
7344 (double) n_split / n_all_split * 100);
7345 change_p = remove_inheritance_pseudos (remove_pseudos);
7346 /* Clear restore_regnos. */
7347 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7348 lra_reg_info[regno].restore_rtx = NULL_RTX;
7349 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7350 lra_reg_info[regno].restore_rtx = NULL_RTX;
7351 change_p = undo_optional_reloads () || change_p;
7352 return change_p;