testsuite: Update scanning symbol sections to support AIX.
[official-gcc.git] / gcc / lra-constraints.c
blobf034c7749e9b8245dc4f6ce4e891ffd75248e8f9
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 /* Avoid further trouble with this insn. Don't generate use
4108 pattern here as we could use the insn SP offset. */
4109 lra_set_insn_deleted (curr_insn);
4110 return true;
4113 /* If the best alternative is with operands 1 and 2 swapped, swap
4114 them. Update the operand numbers of any reloads already
4115 pushed. */
4117 if (goal_alt_swapped)
4119 if (lra_dump_file != NULL)
4120 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
4121 INSN_UID (curr_insn));
4123 /* Swap the duplicates too. */
4124 swap_operands (commutative);
4125 change_p = true;
4128 /* Some targets' TARGET_SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
4129 too conservatively. So we use the secondary memory only if there
4130 is no any alternative without reloads. */
4131 use_sec_mem_p = false;
4132 if (! alt_p)
4133 use_sec_mem_p = true;
4134 else if (sec_mem_p)
4136 for (i = 0; i < n_operands; i++)
4137 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
4138 break;
4139 use_sec_mem_p = i < n_operands;
4142 if (use_sec_mem_p)
4144 int in = -1, out = -1;
4145 rtx new_reg, src, dest, rld;
4146 machine_mode sec_mode, rld_mode;
4148 lra_assert (curr_insn_set != NULL_RTX && sec_mem_p);
4149 dest = SET_DEST (curr_insn_set);
4150 src = SET_SRC (curr_insn_set);
4151 for (i = 0; i < n_operands; i++)
4152 if (*curr_id->operand_loc[i] == dest)
4153 out = i;
4154 else if (*curr_id->operand_loc[i] == src)
4155 in = i;
4156 for (i = 0; i < curr_static_id->n_dups; i++)
4157 if (out < 0 && *curr_id->dup_loc[i] == dest)
4158 out = curr_static_id->dup_num[i];
4159 else if (in < 0 && *curr_id->dup_loc[i] == src)
4160 in = curr_static_id->dup_num[i];
4161 lra_assert (out >= 0 && in >= 0
4162 && curr_static_id->operand[out].type == OP_OUT
4163 && curr_static_id->operand[in].type == OP_IN);
4164 rld = partial_subreg_p (GET_MODE (src), GET_MODE (dest)) ? src : dest;
4165 rld_mode = GET_MODE (rld);
4166 sec_mode = targetm.secondary_memory_needed_mode (rld_mode);
4167 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
4168 NO_REGS, "secondary");
4169 /* If the mode is changed, it should be wider. */
4170 lra_assert (!partial_subreg_p (sec_mode, rld_mode));
4171 if (sec_mode != rld_mode)
4173 /* If the target says specifically to use another mode for
4174 secondary memory moves we cannot reuse the original
4175 insn. */
4176 after = emit_spill_move (false, new_reg, dest);
4177 lra_process_new_insns (curr_insn, NULL, after,
4178 "Inserting the sec. move");
4179 /* We may have non null BEFORE here (e.g. after address
4180 processing. */
4181 push_to_sequence (before);
4182 before = emit_spill_move (true, new_reg, src);
4183 emit_insn (before);
4184 before = get_insns ();
4185 end_sequence ();
4186 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
4187 lra_set_insn_deleted (curr_insn);
4189 else if (dest == rld)
4191 *curr_id->operand_loc[out] = new_reg;
4192 lra_update_dup (curr_id, out);
4193 after = emit_spill_move (false, new_reg, dest);
4194 lra_process_new_insns (curr_insn, NULL, after,
4195 "Inserting the sec. move");
4197 else
4199 *curr_id->operand_loc[in] = new_reg;
4200 lra_update_dup (curr_id, in);
4201 /* See comments above. */
4202 push_to_sequence (before);
4203 before = emit_spill_move (true, new_reg, src);
4204 emit_insn (before);
4205 before = get_insns ();
4206 end_sequence ();
4207 lra_process_new_insns (curr_insn, before, NULL,
4208 "Inserting the sec. move");
4210 lra_update_insn_regno_info (curr_insn);
4211 return true;
4214 lra_assert (goal_alt_number >= 0);
4215 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
4217 if (lra_dump_file != NULL)
4219 const char *p;
4221 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
4222 goal_alt_number, INSN_UID (curr_insn));
4223 for (i = 0; i < n_operands; i++)
4225 p = (curr_static_id->operand_alternative
4226 [goal_alt_number * n_operands + i].constraint);
4227 if (*p == '\0')
4228 continue;
4229 fprintf (lra_dump_file, " (%d) ", i);
4230 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
4231 fputc (*p, lra_dump_file);
4233 if (INSN_CODE (curr_insn) >= 0
4234 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
4235 fprintf (lra_dump_file, " {%s}", p);
4236 if (maybe_ne (curr_id->sp_offset, 0))
4238 fprintf (lra_dump_file, " (sp_off=");
4239 print_dec (curr_id->sp_offset, lra_dump_file);
4240 fprintf (lra_dump_file, ")");
4242 fprintf (lra_dump_file, "\n");
4245 /* Right now, for any pair of operands I and J that are required to
4246 match, with J < I, goal_alt_matches[I] is J. Add I to
4247 goal_alt_matched[J]. */
4249 for (i = 0; i < n_operands; i++)
4250 if ((j = goal_alt_matches[i]) >= 0)
4252 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
4254 /* We allow matching one output operand and several input
4255 operands. */
4256 lra_assert (k == 0
4257 || (curr_static_id->operand[j].type == OP_OUT
4258 && curr_static_id->operand[i].type == OP_IN
4259 && (curr_static_id->operand
4260 [goal_alt_matched[j][0]].type == OP_IN)));
4261 goal_alt_matched[j][k] = i;
4262 goal_alt_matched[j][k + 1] = -1;
4265 for (i = 0; i < n_operands; i++)
4266 goal_alt_win[i] |= goal_alt_match_win[i];
4268 /* Any constants that aren't allowed and can't be reloaded into
4269 registers are here changed into memory references. */
4270 for (i = 0; i < n_operands; i++)
4271 if (goal_alt_win[i])
4273 int regno;
4274 enum reg_class new_class;
4275 rtx reg = *curr_id->operand_loc[i];
4277 if (GET_CODE (reg) == SUBREG)
4278 reg = SUBREG_REG (reg);
4280 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
4282 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
4284 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
4286 lra_assert (ok_p);
4287 lra_change_class (regno, new_class, " Change to", true);
4291 else
4293 const char *constraint;
4294 char c;
4295 rtx op = *curr_id->operand_loc[i];
4296 rtx subreg = NULL_RTX;
4297 machine_mode mode = curr_operand_mode[i];
4299 if (GET_CODE (op) == SUBREG)
4301 subreg = op;
4302 op = SUBREG_REG (op);
4303 mode = GET_MODE (op);
4306 if (CONST_POOL_OK_P (mode, op)
4307 && ((targetm.preferred_reload_class
4308 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
4309 || no_input_reloads_p))
4311 rtx tem = force_const_mem (mode, op);
4313 change_p = true;
4314 if (subreg != NULL_RTX)
4315 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
4317 *curr_id->operand_loc[i] = tem;
4318 lra_update_dup (curr_id, i);
4319 process_address (i, false, &before, &after);
4321 /* If the alternative accepts constant pool refs directly
4322 there will be no reload needed at all. */
4323 if (subreg != NULL_RTX)
4324 continue;
4325 /* Skip alternatives before the one requested. */
4326 constraint = (curr_static_id->operand_alternative
4327 [goal_alt_number * n_operands + i].constraint);
4328 for (;
4329 (c = *constraint) && c != ',' && c != '#';
4330 constraint += CONSTRAINT_LEN (c, constraint))
4332 enum constraint_num cn = lookup_constraint (constraint);
4333 if ((insn_extra_memory_constraint (cn)
4334 || insn_extra_special_memory_constraint (cn))
4335 && satisfies_memory_constraint_p (tem, cn))
4336 break;
4338 if (c == '\0' || c == ',' || c == '#')
4339 continue;
4341 goal_alt_win[i] = true;
4345 n_outputs = 0;
4346 outputs[0] = -1;
4347 for (i = 0; i < n_operands; i++)
4349 int regno;
4350 bool optional_p = false;
4351 rtx old, new_reg;
4352 rtx op = *curr_id->operand_loc[i];
4354 if (goal_alt_win[i])
4356 if (goal_alt[i] == NO_REGS
4357 && REG_P (op)
4358 /* When we assign NO_REGS it means that we will not
4359 assign a hard register to the scratch pseudo by
4360 assigment pass and the scratch pseudo will be
4361 spilled. Spilled scratch pseudos are transformed
4362 back to scratches at the LRA end. */
4363 && ira_former_scratch_operand_p (curr_insn, i)
4364 && ira_former_scratch_p (REGNO (op)))
4366 int regno = REGNO (op);
4367 lra_change_class (regno, NO_REGS, " Change to", true);
4368 if (lra_get_regno_hard_regno (regno) >= 0)
4369 /* We don't have to mark all insn affected by the
4370 spilled pseudo as there is only one such insn, the
4371 current one. */
4372 reg_renumber[regno] = -1;
4373 lra_assert (bitmap_single_bit_set_p
4374 (&lra_reg_info[REGNO (op)].insn_bitmap));
4376 /* We can do an optional reload. If the pseudo got a hard
4377 reg, we might improve the code through inheritance. If
4378 it does not get a hard register we coalesce memory/memory
4379 moves later. Ignore move insns to avoid cycling. */
4380 if (! lra_simple_p
4381 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
4382 && goal_alt[i] != NO_REGS && REG_P (op)
4383 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
4384 && regno < new_regno_start
4385 && ! ira_former_scratch_p (regno)
4386 && reg_renumber[regno] < 0
4387 /* Check that the optional reload pseudo will be able to
4388 hold given mode value. */
4389 && ! (prohibited_class_reg_set_mode_p
4390 (goal_alt[i], reg_class_contents[goal_alt[i]],
4391 PSEUDO_REGNO_MODE (regno)))
4392 && (curr_insn_set == NULL_RTX
4393 || !((REG_P (SET_SRC (curr_insn_set))
4394 || MEM_P (SET_SRC (curr_insn_set))
4395 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
4396 && (REG_P (SET_DEST (curr_insn_set))
4397 || MEM_P (SET_DEST (curr_insn_set))
4398 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
4399 optional_p = true;
4400 else if (goal_alt_matched[i][0] != -1
4401 && curr_static_id->operand[i].type == OP_OUT
4402 && (curr_static_id->operand_alternative
4403 [goal_alt_number * n_operands + i].earlyclobber)
4404 && REG_P (op))
4406 for (j = 0; goal_alt_matched[i][j] != -1; j++)
4408 rtx op2 = *curr_id->operand_loc[goal_alt_matched[i][j]];
4410 if (REG_P (op2) && REGNO (op) != REGNO (op2))
4411 break;
4413 if (goal_alt_matched[i][j] != -1)
4415 /* Generate reloads for different output and matched
4416 input registers. This is the easiest way to avoid
4417 creation of non-existing register conflicts in
4418 lra-lives.c. */
4419 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i], &before,
4420 &after, TRUE);
4421 outputs[n_outputs++] = i;
4422 outputs[n_outputs] = -1;
4424 continue;
4426 else
4427 continue;
4430 /* Operands that match previous ones have already been handled. */
4431 if (goal_alt_matches[i] >= 0)
4432 continue;
4434 /* We should not have an operand with a non-offsettable address
4435 appearing where an offsettable address will do. It also may
4436 be a case when the address should be special in other words
4437 not a general one (e.g. it needs no index reg). */
4438 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
4440 enum reg_class rclass;
4441 rtx *loc = &XEXP (op, 0);
4442 enum rtx_code code = GET_CODE (*loc);
4444 push_to_sequence (before);
4445 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
4446 MEM, SCRATCH);
4447 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
4448 new_reg = emit_inc (rclass, *loc, *loc,
4449 /* This value does not matter for MODIFY. */
4450 GET_MODE_SIZE (GET_MODE (op)));
4451 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
4452 "offsetable address", &new_reg))
4454 rtx addr = *loc;
4455 enum rtx_code code = GET_CODE (addr);
4456 bool align_p = false;
4458 if (code == AND && CONST_INT_P (XEXP (addr, 1)))
4460 /* (and ... (const_int -X)) is used to align to X bytes. */
4461 align_p = true;
4462 addr = XEXP (*loc, 0);
4464 else
4465 addr = canonicalize_reload_addr (addr);
4467 lra_emit_move (new_reg, addr);
4468 if (align_p)
4469 emit_move_insn (new_reg, gen_rtx_AND (GET_MODE (new_reg), new_reg, XEXP (*loc, 1)));
4471 before = get_insns ();
4472 end_sequence ();
4473 *loc = new_reg;
4474 lra_update_dup (curr_id, i);
4476 else if (goal_alt_matched[i][0] == -1)
4478 machine_mode mode;
4479 rtx reg, *loc;
4480 int hard_regno;
4481 enum op_type type = curr_static_id->operand[i].type;
4483 loc = curr_id->operand_loc[i];
4484 mode = curr_operand_mode[i];
4485 if (GET_CODE (*loc) == SUBREG)
4487 reg = SUBREG_REG (*loc);
4488 poly_int64 byte = SUBREG_BYTE (*loc);
4489 if (REG_P (reg)
4490 /* Strict_low_part requires reloading the register and not
4491 just the subreg. Likewise for a strict subreg no wider
4492 than a word for WORD_REGISTER_OPERATIONS targets. */
4493 && (curr_static_id->operand[i].strict_low
4494 || (!paradoxical_subreg_p (mode, GET_MODE (reg))
4495 && (hard_regno
4496 = get_try_hard_regno (REGNO (reg))) >= 0
4497 && (simplify_subreg_regno
4498 (hard_regno,
4499 GET_MODE (reg), byte, mode) < 0)
4500 && (goal_alt[i] == NO_REGS
4501 || (simplify_subreg_regno
4502 (ira_class_hard_regs[goal_alt[i]][0],
4503 GET_MODE (reg), byte, mode) >= 0)))
4504 || (partial_subreg_p (mode, GET_MODE (reg))
4505 && known_le (GET_MODE_SIZE (GET_MODE (reg)),
4506 UNITS_PER_WORD)
4507 && WORD_REGISTER_OPERATIONS)))
4509 /* An OP_INOUT is required when reloading a subreg of a
4510 mode wider than a word to ensure that data beyond the
4511 word being reloaded is preserved. Also automatically
4512 ensure that strict_low_part reloads are made into
4513 OP_INOUT which should already be true from the backend
4514 constraints. */
4515 if (type == OP_OUT
4516 && (curr_static_id->operand[i].strict_low
4517 || read_modify_subreg_p (*loc)))
4518 type = OP_INOUT;
4519 loc = &SUBREG_REG (*loc);
4520 mode = GET_MODE (*loc);
4523 old = *loc;
4524 if (get_reload_reg (type, mode, old, goal_alt[i],
4525 loc != curr_id->operand_loc[i], "", &new_reg)
4526 && type != OP_OUT)
4528 push_to_sequence (before);
4529 lra_emit_move (new_reg, old);
4530 before = get_insns ();
4531 end_sequence ();
4533 *loc = new_reg;
4534 if (type != OP_IN
4535 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
4537 start_sequence ();
4538 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
4539 emit_insn (after);
4540 after = get_insns ();
4541 end_sequence ();
4542 *loc = new_reg;
4544 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
4545 if (goal_alt_dont_inherit_ops[j] == i)
4547 lra_set_regno_unique_value (REGNO (new_reg));
4548 break;
4550 lra_update_dup (curr_id, i);
4552 else if (curr_static_id->operand[i].type == OP_IN
4553 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4554 == OP_OUT
4555 || (curr_static_id->operand[goal_alt_matched[i][0]].type
4556 == OP_INOUT
4557 && (operands_match_p
4558 (*curr_id->operand_loc[i],
4559 *curr_id->operand_loc[goal_alt_matched[i][0]],
4560 -1)))))
4562 /* generate reloads for input and matched outputs. */
4563 match_inputs[0] = i;
4564 match_inputs[1] = -1;
4565 match_reload (goal_alt_matched[i][0], match_inputs, outputs,
4566 goal_alt[i], &before, &after,
4567 curr_static_id->operand_alternative
4568 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
4569 .earlyclobber);
4571 else if ((curr_static_id->operand[i].type == OP_OUT
4572 || (curr_static_id->operand[i].type == OP_INOUT
4573 && (operands_match_p
4574 (*curr_id->operand_loc[i],
4575 *curr_id->operand_loc[goal_alt_matched[i][0]],
4576 -1))))
4577 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4578 == OP_IN))
4579 /* Generate reloads for output and matched inputs. */
4580 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i], &before,
4581 &after, curr_static_id->operand_alternative
4582 [goal_alt_number * n_operands + i].earlyclobber);
4583 else if (curr_static_id->operand[i].type == OP_IN
4584 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4585 == OP_IN))
4587 /* Generate reloads for matched inputs. */
4588 match_inputs[0] = i;
4589 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
4590 match_inputs[j + 1] = k;
4591 match_inputs[j + 1] = -1;
4592 match_reload (-1, match_inputs, outputs, goal_alt[i], &before,
4593 &after, false);
4595 else
4596 /* We must generate code in any case when function
4597 process_alt_operands decides that it is possible. */
4598 gcc_unreachable ();
4600 /* Memorise processed outputs so that output remaining to be processed
4601 can avoid using the same register value (see match_reload). */
4602 if (curr_static_id->operand[i].type == OP_OUT)
4604 outputs[n_outputs++] = i;
4605 outputs[n_outputs] = -1;
4608 if (optional_p)
4610 rtx reg = op;
4612 lra_assert (REG_P (reg));
4613 regno = REGNO (reg);
4614 op = *curr_id->operand_loc[i]; /* Substitution. */
4615 if (GET_CODE (op) == SUBREG)
4616 op = SUBREG_REG (op);
4617 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
4618 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
4619 lra_reg_info[REGNO (op)].restore_rtx = reg;
4620 if (lra_dump_file != NULL)
4621 fprintf (lra_dump_file,
4622 " Making reload reg %d for reg %d optional\n",
4623 REGNO (op), regno);
4626 if (before != NULL_RTX || after != NULL_RTX
4627 || max_regno_before != max_reg_num ())
4628 change_p = true;
4629 if (change_p)
4631 lra_update_operator_dups (curr_id);
4632 /* Something changes -- process the insn. */
4633 lra_update_insn_regno_info (curr_insn);
4635 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
4636 return change_p;
4639 /* Return true if INSN satisfies all constraints. In other words, no
4640 reload insns are needed. */
4641 bool
4642 lra_constrain_insn (rtx_insn *insn)
4644 int saved_new_regno_start = new_regno_start;
4645 int saved_new_insn_uid_start = new_insn_uid_start;
4646 bool change_p;
4648 curr_insn = insn;
4649 curr_id = lra_get_insn_recog_data (curr_insn);
4650 curr_static_id = curr_id->insn_static_data;
4651 new_insn_uid_start = get_max_uid ();
4652 new_regno_start = max_reg_num ();
4653 change_p = curr_insn_transform (true);
4654 new_regno_start = saved_new_regno_start;
4655 new_insn_uid_start = saved_new_insn_uid_start;
4656 return ! change_p;
4659 /* Return true if X is in LIST. */
4660 static bool
4661 in_list_p (rtx x, rtx list)
4663 for (; list != NULL_RTX; list = XEXP (list, 1))
4664 if (XEXP (list, 0) == x)
4665 return true;
4666 return false;
4669 /* Return true if X contains an allocatable hard register (if
4670 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
4671 static bool
4672 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
4674 int i, j;
4675 const char *fmt;
4676 enum rtx_code code;
4678 code = GET_CODE (x);
4679 if (REG_P (x))
4681 int regno = REGNO (x);
4682 HARD_REG_SET alloc_regs;
4684 if (hard_reg_p)
4686 if (regno >= FIRST_PSEUDO_REGISTER)
4687 regno = lra_get_regno_hard_regno (regno);
4688 if (regno < 0)
4689 return false;
4690 alloc_regs = ~lra_no_alloc_regs;
4691 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
4693 else
4695 if (regno < FIRST_PSEUDO_REGISTER)
4696 return false;
4697 if (! spilled_p)
4698 return true;
4699 return lra_get_regno_hard_regno (regno) < 0;
4702 fmt = GET_RTX_FORMAT (code);
4703 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4705 if (fmt[i] == 'e')
4707 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4708 return true;
4710 else if (fmt[i] == 'E')
4712 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4713 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4714 return true;
4717 return false;
4720 /* Process all regs in location *LOC and change them on equivalent
4721 substitution. Return true if any change was done. */
4722 static bool
4723 loc_equivalence_change_p (rtx *loc)
4725 rtx subst, reg, x = *loc;
4726 bool result = false;
4727 enum rtx_code code = GET_CODE (x);
4728 const char *fmt;
4729 int i, j;
4731 if (code == SUBREG)
4733 reg = SUBREG_REG (x);
4734 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4735 && GET_MODE (subst) == VOIDmode)
4737 /* We cannot reload debug location. Simplify subreg here
4738 while we know the inner mode. */
4739 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4740 GET_MODE (reg), SUBREG_BYTE (x));
4741 return true;
4744 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4746 *loc = subst;
4747 return true;
4750 /* Scan all the operand sub-expressions. */
4751 fmt = GET_RTX_FORMAT (code);
4752 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4754 if (fmt[i] == 'e')
4755 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4756 else if (fmt[i] == 'E')
4757 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4758 result
4759 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4761 return result;
4764 /* Similar to loc_equivalence_change_p, but for use as
4765 simplify_replace_fn_rtx callback. DATA is insn for which the
4766 elimination is done. If it null we don't do the elimination. */
4767 static rtx
4768 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4770 if (!REG_P (loc))
4771 return NULL_RTX;
4773 rtx subst = (data == NULL
4774 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4775 if (subst != loc)
4776 return subst;
4778 return NULL_RTX;
4781 /* Maximum number of generated reload insns per an insn. It is for
4782 preventing this pass cycling in a bug case. */
4783 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4785 /* The current iteration number of this LRA pass. */
4786 int lra_constraint_iter;
4788 /* True if we should during assignment sub-pass check assignment
4789 correctness for all pseudos and spill some of them to correct
4790 conflicts. It can be necessary when we substitute equiv which
4791 needs checking register allocation correctness because the
4792 equivalent value contains allocatable hard registers, or when we
4793 restore multi-register pseudo, or when we change the insn code and
4794 its operand became INOUT operand when it was IN one before. */
4795 bool check_and_force_assignment_correctness_p;
4797 /* Return true if REGNO is referenced in more than one block. */
4798 static bool
4799 multi_block_pseudo_p (int regno)
4801 basic_block bb = NULL;
4802 unsigned int uid;
4803 bitmap_iterator bi;
4805 if (regno < FIRST_PSEUDO_REGISTER)
4806 return false;
4808 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4809 if (bb == NULL)
4810 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4811 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4812 return true;
4813 return false;
4816 /* Return true if LIST contains a deleted insn. */
4817 static bool
4818 contains_deleted_insn_p (rtx_insn_list *list)
4820 for (; list != NULL_RTX; list = list->next ())
4821 if (NOTE_P (list->insn ())
4822 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4823 return true;
4824 return false;
4827 /* Return true if X contains a pseudo dying in INSN. */
4828 static bool
4829 dead_pseudo_p (rtx x, rtx_insn *insn)
4831 int i, j;
4832 const char *fmt;
4833 enum rtx_code code;
4835 if (REG_P (x))
4836 return (insn != NULL_RTX
4837 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4838 code = GET_CODE (x);
4839 fmt = GET_RTX_FORMAT (code);
4840 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4842 if (fmt[i] == 'e')
4844 if (dead_pseudo_p (XEXP (x, i), insn))
4845 return true;
4847 else if (fmt[i] == 'E')
4849 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4850 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4851 return true;
4854 return false;
4857 /* Return true if INSN contains a dying pseudo in INSN right hand
4858 side. */
4859 static bool
4860 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4862 rtx set = single_set (insn);
4864 gcc_assert (set != NULL);
4865 return dead_pseudo_p (SET_SRC (set), insn);
4868 /* Return true if any init insn of REGNO contains a dying pseudo in
4869 insn right hand side. */
4870 static bool
4871 init_insn_rhs_dead_pseudo_p (int regno)
4873 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4875 if (insns == NULL)
4876 return false;
4877 for (; insns != NULL_RTX; insns = insns->next ())
4878 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4879 return true;
4880 return false;
4883 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4884 reverse only if we have one init insn with given REGNO as a
4885 source. */
4886 static bool
4887 reverse_equiv_p (int regno)
4889 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4890 rtx set;
4892 if (insns == NULL)
4893 return false;
4894 if (! INSN_P (insns->insn ())
4895 || insns->next () != NULL)
4896 return false;
4897 if ((set = single_set (insns->insn ())) == NULL_RTX)
4898 return false;
4899 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4902 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4903 call this function only for non-reverse equivalence. */
4904 static bool
4905 contains_reloaded_insn_p (int regno)
4907 rtx set;
4908 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4910 for (; list != NULL; list = list->next ())
4911 if ((set = single_set (list->insn ())) == NULL_RTX
4912 || ! REG_P (SET_DEST (set))
4913 || (int) REGNO (SET_DEST (set)) != regno)
4914 return true;
4915 return false;
4918 /* Entry function of LRA constraint pass. Return true if the
4919 constraint pass did change the code. */
4920 bool
4921 lra_constraints (bool first_p)
4923 bool changed_p;
4924 int i, hard_regno, new_insns_num;
4925 unsigned int min_len, new_min_len, uid;
4926 rtx set, x, reg, dest_reg;
4927 basic_block last_bb;
4928 bitmap_iterator bi;
4930 lra_constraint_iter++;
4931 if (lra_dump_file != NULL)
4932 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4933 lra_constraint_iter);
4934 changed_p = false;
4935 if (pic_offset_table_rtx
4936 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4937 check_and_force_assignment_correctness_p = true;
4938 else if (first_p)
4939 /* On the first iteration we should check IRA assignment
4940 correctness. In rare cases, the assignments can be wrong as
4941 early clobbers operands are ignored in IRA or usages of
4942 paradoxical sub-registers are not taken into account by
4943 IRA. */
4944 check_and_force_assignment_correctness_p = true;
4945 new_insn_uid_start = get_max_uid ();
4946 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4947 /* Mark used hard regs for target stack size calulations. */
4948 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4949 if (lra_reg_info[i].nrefs != 0
4950 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4952 int j, nregs;
4954 nregs = hard_regno_nregs (hard_regno, lra_reg_info[i].biggest_mode);
4955 for (j = 0; j < nregs; j++)
4956 df_set_regs_ever_live (hard_regno + j, true);
4958 /* Do elimination before the equivalence processing as we can spill
4959 some pseudos during elimination. */
4960 lra_eliminate (false, first_p);
4961 auto_bitmap equiv_insn_bitmap (&reg_obstack);
4962 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4963 if (lra_reg_info[i].nrefs != 0)
4965 ira_reg_equiv[i].profitable_p = true;
4966 reg = regno_reg_rtx[i];
4967 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4969 bool pseudo_p = contains_reg_p (x, false, false);
4971 /* After RTL transformation, we cannot guarantee that
4972 pseudo in the substitution was not reloaded which might
4973 make equivalence invalid. For example, in reverse
4974 equiv of p0
4976 p0 <- ...
4978 equiv_mem <- p0
4980 the memory address register was reloaded before the 2nd
4981 insn. */
4982 if ((! first_p && pseudo_p)
4983 /* We don't use DF for compilation speed sake. So it
4984 is problematic to update live info when we use an
4985 equivalence containing pseudos in more than one
4986 BB. */
4987 || (pseudo_p && multi_block_pseudo_p (i))
4988 /* If an init insn was deleted for some reason, cancel
4989 the equiv. We could update the equiv insns after
4990 transformations including an equiv insn deletion
4991 but it is not worthy as such cases are extremely
4992 rare. */
4993 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4994 /* If it is not a reverse equivalence, we check that a
4995 pseudo in rhs of the init insn is not dying in the
4996 insn. Otherwise, the live info at the beginning of
4997 the corresponding BB might be wrong after we
4998 removed the insn. When the equiv can be a
4999 constant, the right hand side of the init insn can
5000 be a pseudo. */
5001 || (! reverse_equiv_p (i)
5002 && (init_insn_rhs_dead_pseudo_p (i)
5003 /* If we reloaded the pseudo in an equivalence
5004 init insn, we cannot remove the equiv init
5005 insns and the init insns might write into
5006 const memory in this case. */
5007 || contains_reloaded_insn_p (i)))
5008 /* Prevent access beyond equivalent memory for
5009 paradoxical subregs. */
5010 || (MEM_P (x)
5011 && maybe_gt (GET_MODE_SIZE (lra_reg_info[i].biggest_mode),
5012 GET_MODE_SIZE (GET_MODE (x))))
5013 || (pic_offset_table_rtx
5014 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
5015 && (targetm.preferred_reload_class
5016 (x, lra_get_allocno_class (i)) == NO_REGS))
5017 || contains_symbol_ref_p (x))))
5018 ira_reg_equiv[i].defined_p = false;
5019 if (contains_reg_p (x, false, true))
5020 ira_reg_equiv[i].profitable_p = false;
5021 if (get_equiv (reg) != reg)
5022 bitmap_ior_into (equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
5025 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5026 update_equiv (i);
5027 /* We should add all insns containing pseudos which should be
5028 substituted by their equivalences. */
5029 EXECUTE_IF_SET_IN_BITMAP (equiv_insn_bitmap, 0, uid, bi)
5030 lra_push_insn_by_uid (uid);
5031 min_len = lra_insn_stack_length ();
5032 new_insns_num = 0;
5033 last_bb = NULL;
5034 changed_p = false;
5035 while ((new_min_len = lra_insn_stack_length ()) != 0)
5037 curr_insn = lra_pop_insn ();
5038 --new_min_len;
5039 curr_bb = BLOCK_FOR_INSN (curr_insn);
5040 if (curr_bb != last_bb)
5042 last_bb = curr_bb;
5043 bb_reload_num = lra_curr_reload_num;
5045 if (min_len > new_min_len)
5047 min_len = new_min_len;
5048 new_insns_num = 0;
5050 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
5051 internal_error
5052 ("maximum number of generated reload insns per insn achieved (%d)",
5053 MAX_RELOAD_INSNS_NUMBER);
5054 new_insns_num++;
5055 if (DEBUG_INSN_P (curr_insn))
5057 /* We need to check equivalence in debug insn and change
5058 pseudo to the equivalent value if necessary. */
5059 curr_id = lra_get_insn_recog_data (curr_insn);
5060 if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn)))
5062 rtx old = *curr_id->operand_loc[0];
5063 *curr_id->operand_loc[0]
5064 = simplify_replace_fn_rtx (old, NULL_RTX,
5065 loc_equivalence_callback, curr_insn);
5066 if (old != *curr_id->operand_loc[0])
5068 lra_update_insn_regno_info (curr_insn);
5069 changed_p = true;
5073 else if (INSN_P (curr_insn))
5075 if ((set = single_set (curr_insn)) != NULL_RTX)
5077 dest_reg = SET_DEST (set);
5078 /* The equivalence pseudo could be set up as SUBREG in a
5079 case when it is a call restore insn in a mode
5080 different from the pseudo mode. */
5081 if (GET_CODE (dest_reg) == SUBREG)
5082 dest_reg = SUBREG_REG (dest_reg);
5083 if ((REG_P (dest_reg)
5084 && (x = get_equiv (dest_reg)) != dest_reg
5085 /* Remove insns which set up a pseudo whose value
5086 cannot be changed. Such insns might be not in
5087 init_insns because we don't update equiv data
5088 during insn transformations.
5090 As an example, let suppose that a pseudo got
5091 hard register and on the 1st pass was not
5092 changed to equivalent constant. We generate an
5093 additional insn setting up the pseudo because of
5094 secondary memory movement. Then the pseudo is
5095 spilled and we use the equiv constant. In this
5096 case we should remove the additional insn and
5097 this insn is not init_insns list. */
5098 && (! MEM_P (x) || MEM_READONLY_P (x)
5099 /* Check that this is actually an insn setting
5100 up the equivalence. */
5101 || in_list_p (curr_insn,
5102 ira_reg_equiv
5103 [REGNO (dest_reg)].init_insns)))
5104 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
5105 && in_list_p (curr_insn,
5106 ira_reg_equiv
5107 [REGNO (SET_SRC (set))].init_insns)))
5109 /* This is equiv init insn of pseudo which did not get a
5110 hard register -- remove the insn. */
5111 if (lra_dump_file != NULL)
5113 fprintf (lra_dump_file,
5114 " Removing equiv init insn %i (freq=%d)\n",
5115 INSN_UID (curr_insn),
5116 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
5117 dump_insn_slim (lra_dump_file, curr_insn);
5119 if (contains_reg_p (x, true, false))
5120 check_and_force_assignment_correctness_p = true;
5121 lra_set_insn_deleted (curr_insn);
5122 continue;
5125 curr_id = lra_get_insn_recog_data (curr_insn);
5126 curr_static_id = curr_id->insn_static_data;
5127 init_curr_insn_input_reloads ();
5128 init_curr_operand_mode ();
5129 if (curr_insn_transform (false))
5130 changed_p = true;
5131 /* Check non-transformed insns too for equiv change as USE
5132 or CLOBBER don't need reloads but can contain pseudos
5133 being changed on their equivalences. */
5134 else if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn))
5135 && loc_equivalence_change_p (&PATTERN (curr_insn)))
5137 lra_update_insn_regno_info (curr_insn);
5138 changed_p = true;
5143 /* If we used a new hard regno, changed_p should be true because the
5144 hard reg is assigned to a new pseudo. */
5145 if (flag_checking && !changed_p)
5147 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5148 if (lra_reg_info[i].nrefs != 0
5149 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
5151 int j, nregs = hard_regno_nregs (hard_regno,
5152 PSEUDO_REGNO_MODE (i));
5154 for (j = 0; j < nregs; j++)
5155 lra_assert (df_regs_ever_live_p (hard_regno + j));
5158 return changed_p;
5161 static void initiate_invariants (void);
5162 static void finish_invariants (void);
5164 /* Initiate the LRA constraint pass. It is done once per
5165 function. */
5166 void
5167 lra_constraints_init (void)
5169 initiate_invariants ();
5172 /* Finalize the LRA constraint pass. It is done once per
5173 function. */
5174 void
5175 lra_constraints_finish (void)
5177 finish_invariants ();
5182 /* Structure describes invariants for ineheritance. */
5183 struct lra_invariant
5185 /* The order number of the invariant. */
5186 int num;
5187 /* The invariant RTX. */
5188 rtx invariant_rtx;
5189 /* The origin insn of the invariant. */
5190 rtx_insn *insn;
5193 typedef lra_invariant invariant_t;
5194 typedef invariant_t *invariant_ptr_t;
5195 typedef const invariant_t *const_invariant_ptr_t;
5197 /* Pointer to the inheritance invariants. */
5198 static vec<invariant_ptr_t> invariants;
5200 /* Allocation pool for the invariants. */
5201 static object_allocator<lra_invariant> *invariants_pool;
5203 /* Hash table for the invariants. */
5204 static htab_t invariant_table;
5206 /* Hash function for INVARIANT. */
5207 static hashval_t
5208 invariant_hash (const void *invariant)
5210 rtx inv = ((const_invariant_ptr_t) invariant)->invariant_rtx;
5211 return lra_rtx_hash (inv);
5214 /* Equal function for invariants INVARIANT1 and INVARIANT2. */
5215 static int
5216 invariant_eq_p (const void *invariant1, const void *invariant2)
5218 rtx inv1 = ((const_invariant_ptr_t) invariant1)->invariant_rtx;
5219 rtx inv2 = ((const_invariant_ptr_t) invariant2)->invariant_rtx;
5221 return rtx_equal_p (inv1, inv2);
5224 /* Insert INVARIANT_RTX into the table if it is not there yet. Return
5225 invariant which is in the table. */
5226 static invariant_ptr_t
5227 insert_invariant (rtx invariant_rtx)
5229 void **entry_ptr;
5230 invariant_t invariant;
5231 invariant_ptr_t invariant_ptr;
5233 invariant.invariant_rtx = invariant_rtx;
5234 entry_ptr = htab_find_slot (invariant_table, &invariant, INSERT);
5235 if (*entry_ptr == NULL)
5237 invariant_ptr = invariants_pool->allocate ();
5238 invariant_ptr->invariant_rtx = invariant_rtx;
5239 invariant_ptr->insn = NULL;
5240 invariants.safe_push (invariant_ptr);
5241 *entry_ptr = (void *) invariant_ptr;
5243 return (invariant_ptr_t) *entry_ptr;
5246 /* Initiate the invariant table. */
5247 static void
5248 initiate_invariants (void)
5250 invariants.create (100);
5251 invariants_pool
5252 = new object_allocator<lra_invariant> ("Inheritance invariants");
5253 invariant_table = htab_create (100, invariant_hash, invariant_eq_p, NULL);
5256 /* Finish the invariant table. */
5257 static void
5258 finish_invariants (void)
5260 htab_delete (invariant_table);
5261 delete invariants_pool;
5262 invariants.release ();
5265 /* Make the invariant table empty. */
5266 static void
5267 clear_invariants (void)
5269 htab_empty (invariant_table);
5270 invariants_pool->release ();
5271 invariants.truncate (0);
5276 /* This page contains code to do inheritance/split
5277 transformations. */
5279 /* Number of reloads passed so far in current EBB. */
5280 static int reloads_num;
5282 /* Number of calls passed so far in current EBB. */
5283 static int calls_num;
5285 /* Index ID is the CALLS_NUM associated the last call we saw with
5286 ABI identifier ID. */
5287 static int last_call_for_abi[NUM_ABI_IDS];
5289 /* Which registers have been fully or partially clobbered by a call
5290 since they were last used. */
5291 static HARD_REG_SET full_and_partial_call_clobbers;
5293 /* Current reload pseudo check for validity of elements in
5294 USAGE_INSNS. */
5295 static int curr_usage_insns_check;
5297 /* Info about last usage of registers in EBB to do inheritance/split
5298 transformation. Inheritance transformation is done from a spilled
5299 pseudo and split transformations from a hard register or a pseudo
5300 assigned to a hard register. */
5301 struct usage_insns
5303 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
5304 value INSNS is valid. The insns is chain of optional debug insns
5305 and a finishing non-debug insn using the corresponding reg. The
5306 value is also used to mark the registers which are set up in the
5307 current insn. The negated insn uid is used for this. */
5308 int check;
5309 /* Value of global reloads_num at the last insn in INSNS. */
5310 int reloads_num;
5311 /* Value of global reloads_nums at the last insn in INSNS. */
5312 int calls_num;
5313 /* It can be true only for splitting. And it means that the restore
5314 insn should be put after insn given by the following member. */
5315 bool after_p;
5316 /* Next insns in the current EBB which use the original reg and the
5317 original reg value is not changed between the current insn and
5318 the next insns. In order words, e.g. for inheritance, if we need
5319 to use the original reg value again in the next insns we can try
5320 to use the value in a hard register from a reload insn of the
5321 current insn. */
5322 rtx insns;
5325 /* Map: regno -> corresponding pseudo usage insns. */
5326 static struct usage_insns *usage_insns;
5328 static void
5329 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
5331 usage_insns[regno].check = curr_usage_insns_check;
5332 usage_insns[regno].insns = insn;
5333 usage_insns[regno].reloads_num = reloads_num;
5334 usage_insns[regno].calls_num = calls_num;
5335 usage_insns[regno].after_p = after_p;
5336 if (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0)
5337 remove_from_hard_reg_set (&full_and_partial_call_clobbers,
5338 PSEUDO_REGNO_MODE (regno),
5339 reg_renumber[regno]);
5342 /* The function is used to form list REGNO usages which consists of
5343 optional debug insns finished by a non-debug insn using REGNO.
5344 RELOADS_NUM is current number of reload insns processed so far. */
5345 static void
5346 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
5348 rtx next_usage_insns;
5350 if (usage_insns[regno].check == curr_usage_insns_check
5351 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
5352 && DEBUG_INSN_P (insn))
5354 /* Check that we did not add the debug insn yet. */
5355 if (next_usage_insns != insn
5356 && (GET_CODE (next_usage_insns) != INSN_LIST
5357 || XEXP (next_usage_insns, 0) != insn))
5358 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
5359 next_usage_insns);
5361 else if (NONDEBUG_INSN_P (insn))
5362 setup_next_usage_insn (regno, insn, reloads_num, false);
5363 else
5364 usage_insns[regno].check = 0;
5367 /* Return first non-debug insn in list USAGE_INSNS. */
5368 static rtx_insn *
5369 skip_usage_debug_insns (rtx usage_insns)
5371 rtx insn;
5373 /* Skip debug insns. */
5374 for (insn = usage_insns;
5375 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
5376 insn = XEXP (insn, 1))
5378 return safe_as_a <rtx_insn *> (insn);
5381 /* Return true if we need secondary memory moves for insn in
5382 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
5383 into the insn. */
5384 static bool
5385 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
5386 rtx usage_insns ATTRIBUTE_UNUSED)
5388 rtx_insn *insn;
5389 rtx set, dest;
5390 enum reg_class cl;
5392 if (inher_cl == ALL_REGS
5393 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
5394 return false;
5395 lra_assert (INSN_P (insn));
5396 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
5397 return false;
5398 dest = SET_DEST (set);
5399 if (! REG_P (dest))
5400 return false;
5401 lra_assert (inher_cl != NO_REGS);
5402 cl = get_reg_class (REGNO (dest));
5403 return (cl != NO_REGS && cl != ALL_REGS
5404 && targetm.secondary_memory_needed (GET_MODE (dest), inher_cl, cl));
5407 /* Registers involved in inheritance/split in the current EBB
5408 (inheritance/split pseudos and original registers). */
5409 static bitmap_head check_only_regs;
5411 /* Reload pseudos cannot be involded in invariant inheritance in the
5412 current EBB. */
5413 static bitmap_head invalid_invariant_regs;
5415 /* Do inheritance transformations for insn INSN, which defines (if
5416 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
5417 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
5418 form as the "insns" field of usage_insns. Return true if we
5419 succeed in such transformation.
5421 The transformations look like:
5423 p <- ... i <- ...
5424 ... p <- i (new insn)
5425 ... =>
5426 <- ... p ... <- ... i ...
5428 ... i <- p (new insn)
5429 <- ... p ... <- ... i ...
5430 ... =>
5431 <- ... p ... <- ... i ...
5432 where p is a spilled original pseudo and i is a new inheritance pseudo.
5435 The inheritance pseudo has the smallest class of two classes CL and
5436 class of ORIGINAL REGNO. */
5437 static bool
5438 inherit_reload_reg (bool def_p, int original_regno,
5439 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
5441 if (optimize_function_for_size_p (cfun))
5442 return false;
5444 enum reg_class rclass = lra_get_allocno_class (original_regno);
5445 rtx original_reg = regno_reg_rtx[original_regno];
5446 rtx new_reg, usage_insn;
5447 rtx_insn *new_insns;
5449 lra_assert (! usage_insns[original_regno].after_p);
5450 if (lra_dump_file != NULL)
5451 fprintf (lra_dump_file,
5452 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
5453 if (! ira_reg_classes_intersect_p[cl][rclass])
5455 if (lra_dump_file != NULL)
5457 fprintf (lra_dump_file,
5458 " Rejecting inheritance for %d "
5459 "because of disjoint classes %s and %s\n",
5460 original_regno, reg_class_names[cl],
5461 reg_class_names[rclass]);
5462 fprintf (lra_dump_file,
5463 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5465 return false;
5467 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
5468 /* We don't use a subset of two classes because it can be
5469 NO_REGS. This transformation is still profitable in most
5470 cases even if the classes are not intersected as register
5471 move is probably cheaper than a memory load. */
5472 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
5474 if (lra_dump_file != NULL)
5475 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
5476 reg_class_names[cl], reg_class_names[rclass]);
5478 rclass = cl;
5480 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
5482 /* Reject inheritance resulting in secondary memory moves.
5483 Otherwise, there is a danger in LRA cycling. Also such
5484 transformation will be unprofitable. */
5485 if (lra_dump_file != NULL)
5487 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
5488 rtx set = single_set (insn);
5490 lra_assert (set != NULL_RTX);
5492 rtx dest = SET_DEST (set);
5494 lra_assert (REG_P (dest));
5495 fprintf (lra_dump_file,
5496 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
5497 "as secondary mem is needed\n",
5498 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
5499 original_regno, reg_class_names[rclass]);
5500 fprintf (lra_dump_file,
5501 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5503 return false;
5505 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
5506 rclass, "inheritance");
5507 start_sequence ();
5508 if (def_p)
5509 lra_emit_move (original_reg, new_reg);
5510 else
5511 lra_emit_move (new_reg, original_reg);
5512 new_insns = get_insns ();
5513 end_sequence ();
5514 if (NEXT_INSN (new_insns) != NULL_RTX)
5516 if (lra_dump_file != NULL)
5518 fprintf (lra_dump_file,
5519 " Rejecting inheritance %d->%d "
5520 "as it results in 2 or more insns:\n",
5521 original_regno, REGNO (new_reg));
5522 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
5523 fprintf (lra_dump_file,
5524 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5526 return false;
5528 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
5529 lra_update_insn_regno_info (insn);
5530 if (! def_p)
5531 /* We now have a new usage insn for original regno. */
5532 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
5533 if (lra_dump_file != NULL)
5534 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
5535 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
5536 lra_reg_info[REGNO (new_reg)].restore_rtx = regno_reg_rtx[original_regno];
5537 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5538 bitmap_set_bit (&check_only_regs, original_regno);
5539 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
5540 if (def_p)
5541 lra_process_new_insns (insn, NULL, new_insns,
5542 "Add original<-inheritance");
5543 else
5544 lra_process_new_insns (insn, new_insns, NULL,
5545 "Add inheritance<-original");
5546 while (next_usage_insns != NULL_RTX)
5548 if (GET_CODE (next_usage_insns) != INSN_LIST)
5550 usage_insn = next_usage_insns;
5551 lra_assert (NONDEBUG_INSN_P (usage_insn));
5552 next_usage_insns = NULL;
5554 else
5556 usage_insn = XEXP (next_usage_insns, 0);
5557 lra_assert (DEBUG_INSN_P (usage_insn));
5558 next_usage_insns = XEXP (next_usage_insns, 1);
5560 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
5561 DEBUG_INSN_P (usage_insn));
5562 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5563 if (lra_dump_file != NULL)
5565 basic_block bb = BLOCK_FOR_INSN (usage_insn);
5566 fprintf (lra_dump_file,
5567 " Inheritance reuse change %d->%d (bb%d):\n",
5568 original_regno, REGNO (new_reg),
5569 bb ? bb->index : -1);
5570 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5573 if (lra_dump_file != NULL)
5574 fprintf (lra_dump_file,
5575 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5576 return true;
5579 /* Return true if we need a caller save/restore for pseudo REGNO which
5580 was assigned to a hard register. */
5581 static inline bool
5582 need_for_call_save_p (int regno)
5584 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
5585 if (usage_insns[regno].calls_num < calls_num)
5587 unsigned int abis = 0;
5588 for (unsigned int i = 0; i < NUM_ABI_IDS; ++i)
5589 if (last_call_for_abi[i] > usage_insns[regno].calls_num)
5590 abis |= 1 << i;
5591 gcc_assert (abis);
5592 if (call_clobbered_in_region_p (abis, full_and_partial_call_clobbers,
5593 PSEUDO_REGNO_MODE (regno),
5594 reg_renumber[regno]))
5595 return true;
5597 return false;
5600 /* Global registers occurring in the current EBB. */
5601 static bitmap_head ebb_global_regs;
5603 /* Return true if we need a split for hard register REGNO or pseudo
5604 REGNO which was assigned to a hard register.
5605 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
5606 used for reloads since the EBB end. It is an approximation of the
5607 used hard registers in the split range. The exact value would
5608 require expensive calculations. If we were aggressive with
5609 splitting because of the approximation, the split pseudo will save
5610 the same hard register assignment and will be removed in the undo
5611 pass. We still need the approximation because too aggressive
5612 splitting would result in too inaccurate cost calculation in the
5613 assignment pass because of too many generated moves which will be
5614 probably removed in the undo pass. */
5615 static inline bool
5616 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
5618 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
5620 lra_assert (hard_regno >= 0);
5621 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
5622 /* Don't split eliminable hard registers, otherwise we can
5623 split hard registers like hard frame pointer, which
5624 lives on BB start/end according to DF-infrastructure,
5625 when there is a pseudo assigned to the register and
5626 living in the same BB. */
5627 && (regno >= FIRST_PSEUDO_REGISTER
5628 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
5629 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
5630 /* Don't split call clobbered hard regs living through
5631 calls, otherwise we might have a check problem in the
5632 assign sub-pass as in the most cases (exception is a
5633 situation when check_and_force_assignment_correctness_p value is
5634 true) the assign pass assumes that all pseudos living
5635 through calls are assigned to call saved hard regs. */
5636 && (regno >= FIRST_PSEUDO_REGISTER
5637 || !TEST_HARD_REG_BIT (full_and_partial_call_clobbers, regno))
5638 /* We need at least 2 reloads to make pseudo splitting
5639 profitable. We should provide hard regno splitting in
5640 any case to solve 1st insn scheduling problem when
5641 moving hard register definition up might result in
5642 impossibility to find hard register for reload pseudo of
5643 small register class. */
5644 && (usage_insns[regno].reloads_num
5645 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
5646 && (regno < FIRST_PSEUDO_REGISTER
5647 /* For short living pseudos, spilling + inheritance can
5648 be considered a substitution for splitting.
5649 Therefore we do not splitting for local pseudos. It
5650 decreases also aggressiveness of splitting. The
5651 minimal number of references is chosen taking into
5652 account that for 2 references splitting has no sense
5653 as we can just spill the pseudo. */
5654 || (regno >= FIRST_PSEUDO_REGISTER
5655 && lra_reg_info[regno].nrefs > 3
5656 && bitmap_bit_p (&ebb_global_regs, regno))))
5657 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
5660 /* Return class for the split pseudo created from original pseudo with
5661 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
5662 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
5663 results in no secondary memory movements. */
5664 static enum reg_class
5665 choose_split_class (enum reg_class allocno_class,
5666 int hard_regno ATTRIBUTE_UNUSED,
5667 machine_mode mode ATTRIBUTE_UNUSED)
5669 int i;
5670 enum reg_class cl, best_cl = NO_REGS;
5671 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
5672 = REGNO_REG_CLASS (hard_regno);
5674 if (! targetm.secondary_memory_needed (mode, allocno_class, allocno_class)
5675 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
5676 return allocno_class;
5677 for (i = 0;
5678 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
5679 i++)
5680 if (! targetm.secondary_memory_needed (mode, cl, hard_reg_class)
5681 && ! targetm.secondary_memory_needed (mode, hard_reg_class, cl)
5682 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
5683 && (best_cl == NO_REGS
5684 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
5685 best_cl = cl;
5686 return best_cl;
5689 /* Copy any equivalence information from ORIGINAL_REGNO to NEW_REGNO.
5690 It only makes sense to call this function if NEW_REGNO is always
5691 equal to ORIGINAL_REGNO. */
5693 static void
5694 lra_copy_reg_equiv (unsigned int new_regno, unsigned int original_regno)
5696 if (!ira_reg_equiv[original_regno].defined_p)
5697 return;
5699 ira_expand_reg_equiv ();
5700 ira_reg_equiv[new_regno].defined_p = true;
5701 if (ira_reg_equiv[original_regno].memory)
5702 ira_reg_equiv[new_regno].memory
5703 = copy_rtx (ira_reg_equiv[original_regno].memory);
5704 if (ira_reg_equiv[original_regno].constant)
5705 ira_reg_equiv[new_regno].constant
5706 = copy_rtx (ira_reg_equiv[original_regno].constant);
5707 if (ira_reg_equiv[original_regno].invariant)
5708 ira_reg_equiv[new_regno].invariant
5709 = copy_rtx (ira_reg_equiv[original_regno].invariant);
5712 /* Do split transformations for insn INSN, which defines or uses
5713 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
5714 the EBB next uses ORIGINAL_REGNO; it has the same form as the
5715 "insns" field of usage_insns. If TO is not NULL, we don't use
5716 usage_insns, we put restore insns after TO insn. It is a case when
5717 we call it from lra_split_hard_reg_for, outside the inheritance
5718 pass.
5720 The transformations look like:
5722 p <- ... p <- ...
5723 ... s <- p (new insn -- save)
5724 ... =>
5725 ... p <- s (new insn -- restore)
5726 <- ... p ... <- ... p ...
5728 <- ... p ... <- ... p ...
5729 ... s <- p (new insn -- save)
5730 ... =>
5731 ... p <- s (new insn -- restore)
5732 <- ... p ... <- ... p ...
5734 where p is an original pseudo got a hard register or a hard
5735 register and s is a new split pseudo. The save is put before INSN
5736 if BEFORE_P is true. Return true if we succeed in such
5737 transformation. */
5738 static bool
5739 split_reg (bool before_p, int original_regno, rtx_insn *insn,
5740 rtx next_usage_insns, rtx_insn *to)
5742 enum reg_class rclass;
5743 rtx original_reg;
5744 int hard_regno, nregs;
5745 rtx new_reg, usage_insn;
5746 rtx_insn *restore, *save;
5747 bool after_p;
5748 bool call_save_p;
5749 machine_mode mode;
5751 if (original_regno < FIRST_PSEUDO_REGISTER)
5753 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
5754 hard_regno = original_regno;
5755 call_save_p = false;
5756 nregs = 1;
5757 mode = lra_reg_info[hard_regno].biggest_mode;
5758 machine_mode reg_rtx_mode = GET_MODE (regno_reg_rtx[hard_regno]);
5759 /* A reg can have a biggest_mode of VOIDmode if it was only ever seen
5760 as part of a multi-word register. In that case, or if the biggest
5761 mode was larger than a register, just use the reg_rtx. Otherwise,
5762 limit the size to that of the biggest access in the function. */
5763 if (mode == VOIDmode
5764 || paradoxical_subreg_p (mode, reg_rtx_mode))
5766 original_reg = regno_reg_rtx[hard_regno];
5767 mode = reg_rtx_mode;
5769 else
5770 original_reg = gen_rtx_REG (mode, hard_regno);
5772 else
5774 mode = PSEUDO_REGNO_MODE (original_regno);
5775 hard_regno = reg_renumber[original_regno];
5776 nregs = hard_regno_nregs (hard_regno, mode);
5777 rclass = lra_get_allocno_class (original_regno);
5778 original_reg = regno_reg_rtx[original_regno];
5779 call_save_p = need_for_call_save_p (original_regno);
5781 lra_assert (hard_regno >= 0);
5782 if (lra_dump_file != NULL)
5783 fprintf (lra_dump_file,
5784 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
5786 if (call_save_p)
5788 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
5789 hard_regno_nregs (hard_regno, mode),
5790 mode);
5791 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
5793 else
5795 rclass = choose_split_class (rclass, hard_regno, mode);
5796 if (rclass == NO_REGS)
5798 if (lra_dump_file != NULL)
5800 fprintf (lra_dump_file,
5801 " Rejecting split of %d(%s): "
5802 "no good reg class for %d(%s)\n",
5803 original_regno,
5804 reg_class_names[lra_get_allocno_class (original_regno)],
5805 hard_regno,
5806 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
5807 fprintf
5808 (lra_dump_file,
5809 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5811 return false;
5813 /* Split_if_necessary can split hard registers used as part of a
5814 multi-register mode but splits each register individually. The
5815 mode used for each independent register may not be supported
5816 so reject the split. Splitting the wider mode should theoretically
5817 be possible but is not implemented. */
5818 if (!targetm.hard_regno_mode_ok (hard_regno, mode))
5820 if (lra_dump_file != NULL)
5822 fprintf (lra_dump_file,
5823 " Rejecting split of %d(%s): unsuitable mode %s\n",
5824 original_regno,
5825 reg_class_names[lra_get_allocno_class (original_regno)],
5826 GET_MODE_NAME (mode));
5827 fprintf
5828 (lra_dump_file,
5829 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5831 return false;
5833 new_reg = lra_create_new_reg (mode, original_reg, rclass, "split");
5834 reg_renumber[REGNO (new_reg)] = hard_regno;
5836 int new_regno = REGNO (new_reg);
5837 save = emit_spill_move (true, new_reg, original_reg);
5838 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
5840 if (lra_dump_file != NULL)
5842 fprintf
5843 (lra_dump_file,
5844 " Rejecting split %d->%d resulting in > 2 save insns:\n",
5845 original_regno, new_regno);
5846 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
5847 fprintf (lra_dump_file,
5848 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5850 return false;
5852 restore = emit_spill_move (false, new_reg, original_reg);
5853 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
5855 if (lra_dump_file != NULL)
5857 fprintf (lra_dump_file,
5858 " Rejecting split %d->%d "
5859 "resulting in > 2 restore insns:\n",
5860 original_regno, new_regno);
5861 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
5862 fprintf (lra_dump_file,
5863 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5865 return false;
5867 /* Transfer equivalence information to the spill register, so that
5868 if we fail to allocate the spill register, we have the option of
5869 rematerializing the original value instead of spilling to the stack. */
5870 if (!HARD_REGISTER_NUM_P (original_regno)
5871 && mode == PSEUDO_REGNO_MODE (original_regno))
5872 lra_copy_reg_equiv (new_regno, original_regno);
5873 lra_reg_info[new_regno].restore_rtx = regno_reg_rtx[original_regno];
5874 bitmap_set_bit (&lra_split_regs, new_regno);
5875 if (to != NULL)
5877 lra_assert (next_usage_insns == NULL);
5878 usage_insn = to;
5879 after_p = TRUE;
5881 else
5883 /* We need check_only_regs only inside the inheritance pass. */
5884 bitmap_set_bit (&check_only_regs, new_regno);
5885 bitmap_set_bit (&check_only_regs, original_regno);
5886 after_p = usage_insns[original_regno].after_p;
5887 for (;;)
5889 if (GET_CODE (next_usage_insns) != INSN_LIST)
5891 usage_insn = next_usage_insns;
5892 break;
5894 usage_insn = XEXP (next_usage_insns, 0);
5895 lra_assert (DEBUG_INSN_P (usage_insn));
5896 next_usage_insns = XEXP (next_usage_insns, 1);
5897 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
5898 true);
5899 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5900 if (lra_dump_file != NULL)
5902 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5903 original_regno, new_regno);
5904 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5908 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5909 lra_assert (usage_insn != insn || (after_p && before_p));
5910 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5911 after_p ? NULL : restore,
5912 after_p ? restore : NULL,
5913 call_save_p
5914 ? "Add reg<-save" : "Add reg<-split");
5915 lra_process_new_insns (insn, before_p ? save : NULL,
5916 before_p ? NULL : save,
5917 call_save_p
5918 ? "Add save<-reg" : "Add split<-reg");
5919 if (nregs > 1)
5920 /* If we are trying to split multi-register. We should check
5921 conflicts on the next assignment sub-pass. IRA can allocate on
5922 sub-register levels, LRA do this on pseudos level right now and
5923 this discrepancy may create allocation conflicts after
5924 splitting. */
5925 check_and_force_assignment_correctness_p = true;
5926 if (lra_dump_file != NULL)
5927 fprintf (lra_dump_file,
5928 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5929 return true;
5932 /* Split a hard reg for reload pseudo REGNO having RCLASS and living
5933 in the range [FROM, TO]. Return true if did a split. Otherwise,
5934 return false. */
5935 bool
5936 spill_hard_reg_in_range (int regno, enum reg_class rclass, rtx_insn *from, rtx_insn *to)
5938 int i, hard_regno;
5939 int rclass_size;
5940 rtx_insn *insn;
5941 unsigned int uid;
5942 bitmap_iterator bi;
5943 HARD_REG_SET ignore;
5945 lra_assert (from != NULL && to != NULL);
5946 CLEAR_HARD_REG_SET (ignore);
5947 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
5949 lra_insn_recog_data_t id = lra_insn_recog_data[uid];
5950 struct lra_static_insn_data *static_id = id->insn_static_data;
5951 struct lra_insn_reg *reg;
5953 for (reg = id->regs; reg != NULL; reg = reg->next)
5954 if (reg->regno < FIRST_PSEUDO_REGISTER)
5955 SET_HARD_REG_BIT (ignore, reg->regno);
5956 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
5957 SET_HARD_REG_BIT (ignore, reg->regno);
5959 rclass_size = ira_class_hard_regs_num[rclass];
5960 for (i = 0; i < rclass_size; i++)
5962 hard_regno = ira_class_hard_regs[rclass][i];
5963 if (! TEST_HARD_REG_BIT (lra_reg_info[regno].conflict_hard_regs, hard_regno)
5964 || TEST_HARD_REG_BIT (ignore, hard_regno))
5965 continue;
5966 for (insn = from; insn != NEXT_INSN (to); insn = NEXT_INSN (insn))
5968 struct lra_static_insn_data *static_id;
5969 struct lra_insn_reg *reg;
5971 if (!INSN_P (insn))
5972 continue;
5973 if (bitmap_bit_p (&lra_reg_info[hard_regno].insn_bitmap,
5974 INSN_UID (insn)))
5975 break;
5976 static_id = lra_get_insn_recog_data (insn)->insn_static_data;
5977 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
5978 if (reg->regno == hard_regno)
5979 break;
5980 if (reg != NULL)
5981 break;
5983 if (insn != NEXT_INSN (to))
5984 continue;
5985 if (split_reg (TRUE, hard_regno, from, NULL, to))
5986 return true;
5988 return false;
5991 /* Recognize that we need a split transformation for insn INSN, which
5992 defines or uses REGNO in its insn biggest MODE (we use it only if
5993 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5994 hard registers which might be used for reloads since the EBB end.
5995 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5996 uid before starting INSN processing. Return true if we succeed in
5997 such transformation. */
5998 static bool
5999 split_if_necessary (int regno, machine_mode mode,
6000 HARD_REG_SET potential_reload_hard_regs,
6001 bool before_p, rtx_insn *insn, int max_uid)
6003 bool res = false;
6004 int i, nregs = 1;
6005 rtx next_usage_insns;
6007 if (regno < FIRST_PSEUDO_REGISTER)
6008 nregs = hard_regno_nregs (regno, mode);
6009 for (i = 0; i < nregs; i++)
6010 if (usage_insns[regno + i].check == curr_usage_insns_check
6011 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
6012 /* To avoid processing the register twice or more. */
6013 && ((GET_CODE (next_usage_insns) != INSN_LIST
6014 && INSN_UID (next_usage_insns) < max_uid)
6015 || (GET_CODE (next_usage_insns) == INSN_LIST
6016 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
6017 && need_for_split_p (potential_reload_hard_regs, regno + i)
6018 && split_reg (before_p, regno + i, insn, next_usage_insns, NULL))
6019 res = true;
6020 return res;
6023 /* Return TRUE if rtx X is considered as an invariant for
6024 inheritance. */
6025 static bool
6026 invariant_p (const_rtx x)
6028 machine_mode mode;
6029 const char *fmt;
6030 enum rtx_code code;
6031 int i, j;
6033 if (side_effects_p (x))
6034 return false;
6036 code = GET_CODE (x);
6037 mode = GET_MODE (x);
6038 if (code == SUBREG)
6040 x = SUBREG_REG (x);
6041 code = GET_CODE (x);
6042 mode = wider_subreg_mode (mode, GET_MODE (x));
6045 if (MEM_P (x))
6046 return false;
6048 if (REG_P (x))
6050 int i, nregs, regno = REGNO (x);
6052 if (regno >= FIRST_PSEUDO_REGISTER || regno == STACK_POINTER_REGNUM
6053 || TEST_HARD_REG_BIT (eliminable_regset, regno)
6054 || GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
6055 return false;
6056 nregs = hard_regno_nregs (regno, mode);
6057 for (i = 0; i < nregs; i++)
6058 if (! fixed_regs[regno + i]
6059 /* A hard register may be clobbered in the current insn
6060 but we can ignore this case because if the hard
6061 register is used it should be set somewhere after the
6062 clobber. */
6063 || bitmap_bit_p (&invalid_invariant_regs, regno + i))
6064 return false;
6066 fmt = GET_RTX_FORMAT (code);
6067 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6069 if (fmt[i] == 'e')
6071 if (! invariant_p (XEXP (x, i)))
6072 return false;
6074 else if (fmt[i] == 'E')
6076 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6077 if (! invariant_p (XVECEXP (x, i, j)))
6078 return false;
6081 return true;
6084 /* We have 'dest_reg <- invariant'. Let us try to make an invariant
6085 inheritance transformation (using dest_reg instead invariant in a
6086 subsequent insn). */
6087 static bool
6088 process_invariant_for_inheritance (rtx dst_reg, rtx invariant_rtx)
6090 invariant_ptr_t invariant_ptr;
6091 rtx_insn *insn, *new_insns;
6092 rtx insn_set, insn_reg, new_reg;
6093 int insn_regno;
6094 bool succ_p = false;
6095 int dst_regno = REGNO (dst_reg);
6096 machine_mode dst_mode = GET_MODE (dst_reg);
6097 enum reg_class cl = lra_get_allocno_class (dst_regno), insn_reg_cl;
6099 invariant_ptr = insert_invariant (invariant_rtx);
6100 if ((insn = invariant_ptr->insn) != NULL_RTX)
6102 /* We have a subsequent insn using the invariant. */
6103 insn_set = single_set (insn);
6104 lra_assert (insn_set != NULL);
6105 insn_reg = SET_DEST (insn_set);
6106 lra_assert (REG_P (insn_reg));
6107 insn_regno = REGNO (insn_reg);
6108 insn_reg_cl = lra_get_allocno_class (insn_regno);
6110 if (dst_mode == GET_MODE (insn_reg)
6111 /* We should consider only result move reg insns which are
6112 cheap. */
6113 && targetm.register_move_cost (dst_mode, cl, insn_reg_cl) == 2
6114 && targetm.register_move_cost (dst_mode, cl, cl) == 2)
6116 if (lra_dump_file != NULL)
6117 fprintf (lra_dump_file,
6118 " [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[\n");
6119 new_reg = lra_create_new_reg (dst_mode, dst_reg,
6120 cl, "invariant inheritance");
6121 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
6122 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
6123 lra_reg_info[REGNO (new_reg)].restore_rtx = PATTERN (insn);
6124 start_sequence ();
6125 lra_emit_move (new_reg, dst_reg);
6126 new_insns = get_insns ();
6127 end_sequence ();
6128 lra_process_new_insns (curr_insn, NULL, new_insns,
6129 "Add invariant inheritance<-original");
6130 start_sequence ();
6131 lra_emit_move (SET_DEST (insn_set), new_reg);
6132 new_insns = get_insns ();
6133 end_sequence ();
6134 lra_process_new_insns (insn, NULL, new_insns,
6135 "Changing reload<-inheritance");
6136 lra_set_insn_deleted (insn);
6137 succ_p = true;
6138 if (lra_dump_file != NULL)
6140 fprintf (lra_dump_file,
6141 " Invariant inheritance reuse change %d (bb%d):\n",
6142 REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
6143 dump_insn_slim (lra_dump_file, insn);
6144 fprintf (lra_dump_file,
6145 " ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]\n");
6149 invariant_ptr->insn = curr_insn;
6150 return succ_p;
6153 /* Check only registers living at the current program point in the
6154 current EBB. */
6155 static bitmap_head live_regs;
6157 /* Update live info in EBB given by its HEAD and TAIL insns after
6158 inheritance/split transformation. The function removes dead moves
6159 too. */
6160 static void
6161 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
6163 unsigned int j;
6164 int i, regno;
6165 bool live_p;
6166 rtx_insn *prev_insn;
6167 rtx set;
6168 bool remove_p;
6169 basic_block last_bb, prev_bb, curr_bb;
6170 bitmap_iterator bi;
6171 struct lra_insn_reg *reg;
6172 edge e;
6173 edge_iterator ei;
6175 last_bb = BLOCK_FOR_INSN (tail);
6176 prev_bb = NULL;
6177 for (curr_insn = tail;
6178 curr_insn != PREV_INSN (head);
6179 curr_insn = prev_insn)
6181 prev_insn = PREV_INSN (curr_insn);
6182 /* We need to process empty blocks too. They contain
6183 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
6184 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
6185 continue;
6186 curr_bb = BLOCK_FOR_INSN (curr_insn);
6187 if (curr_bb != prev_bb)
6189 if (prev_bb != NULL)
6191 /* Update df_get_live_in (prev_bb): */
6192 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6193 if (bitmap_bit_p (&live_regs, j))
6194 bitmap_set_bit (df_get_live_in (prev_bb), j);
6195 else
6196 bitmap_clear_bit (df_get_live_in (prev_bb), j);
6198 if (curr_bb != last_bb)
6200 /* Update df_get_live_out (curr_bb): */
6201 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6203 live_p = bitmap_bit_p (&live_regs, j);
6204 if (! live_p)
6205 FOR_EACH_EDGE (e, ei, curr_bb->succs)
6206 if (bitmap_bit_p (df_get_live_in (e->dest), j))
6208 live_p = true;
6209 break;
6211 if (live_p)
6212 bitmap_set_bit (df_get_live_out (curr_bb), j);
6213 else
6214 bitmap_clear_bit (df_get_live_out (curr_bb), j);
6217 prev_bb = curr_bb;
6218 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
6220 if (! NONDEBUG_INSN_P (curr_insn))
6221 continue;
6222 curr_id = lra_get_insn_recog_data (curr_insn);
6223 curr_static_id = curr_id->insn_static_data;
6224 remove_p = false;
6225 if ((set = single_set (curr_insn)) != NULL_RTX
6226 && REG_P (SET_DEST (set))
6227 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
6228 && SET_DEST (set) != pic_offset_table_rtx
6229 && bitmap_bit_p (&check_only_regs, regno)
6230 && ! bitmap_bit_p (&live_regs, regno))
6231 remove_p = true;
6232 /* See which defined values die here. */
6233 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6234 if (reg->type == OP_OUT && ! reg->subreg_p)
6235 bitmap_clear_bit (&live_regs, reg->regno);
6236 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6237 if (reg->type == OP_OUT && ! reg->subreg_p)
6238 bitmap_clear_bit (&live_regs, reg->regno);
6239 if (curr_id->arg_hard_regs != NULL)
6240 /* Make clobbered argument hard registers die. */
6241 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6242 if (regno >= FIRST_PSEUDO_REGISTER)
6243 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
6244 /* Mark each used value as live. */
6245 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6246 if (reg->type != OP_OUT
6247 && bitmap_bit_p (&check_only_regs, reg->regno))
6248 bitmap_set_bit (&live_regs, reg->regno);
6249 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6250 if (reg->type != OP_OUT
6251 && bitmap_bit_p (&check_only_regs, reg->regno))
6252 bitmap_set_bit (&live_regs, reg->regno);
6253 if (curr_id->arg_hard_regs != NULL)
6254 /* Make used argument hard registers live. */
6255 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6256 if (regno < FIRST_PSEUDO_REGISTER
6257 && bitmap_bit_p (&check_only_regs, regno))
6258 bitmap_set_bit (&live_regs, regno);
6259 /* It is quite important to remove dead move insns because it
6260 means removing dead store. We don't need to process them for
6261 constraints. */
6262 if (remove_p)
6264 if (lra_dump_file != NULL)
6266 fprintf (lra_dump_file, " Removing dead insn:\n ");
6267 dump_insn_slim (lra_dump_file, curr_insn);
6269 lra_set_insn_deleted (curr_insn);
6274 /* The structure describes info to do an inheritance for the current
6275 insn. We need to collect such info first before doing the
6276 transformations because the transformations change the insn
6277 internal representation. */
6278 struct to_inherit
6280 /* Original regno. */
6281 int regno;
6282 /* Subsequent insns which can inherit original reg value. */
6283 rtx insns;
6286 /* Array containing all info for doing inheritance from the current
6287 insn. */
6288 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
6290 /* Number elements in the previous array. */
6291 static int to_inherit_num;
6293 /* Add inheritance info REGNO and INSNS. Their meaning is described in
6294 structure to_inherit. */
6295 static void
6296 add_to_inherit (int regno, rtx insns)
6298 int i;
6300 for (i = 0; i < to_inherit_num; i++)
6301 if (to_inherit[i].regno == regno)
6302 return;
6303 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
6304 to_inherit[to_inherit_num].regno = regno;
6305 to_inherit[to_inherit_num++].insns = insns;
6308 /* Return the last non-debug insn in basic block BB, or the block begin
6309 note if none. */
6310 static rtx_insn *
6311 get_last_insertion_point (basic_block bb)
6313 rtx_insn *insn;
6315 FOR_BB_INSNS_REVERSE (bb, insn)
6316 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
6317 return insn;
6318 gcc_unreachable ();
6321 /* Set up RES by registers living on edges FROM except the edge (FROM,
6322 TO) or by registers set up in a jump insn in BB FROM. */
6323 static void
6324 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
6326 rtx_insn *last;
6327 struct lra_insn_reg *reg;
6328 edge e;
6329 edge_iterator ei;
6331 lra_assert (to != NULL);
6332 bitmap_clear (res);
6333 FOR_EACH_EDGE (e, ei, from->succs)
6334 if (e->dest != to)
6335 bitmap_ior_into (res, df_get_live_in (e->dest));
6336 last = get_last_insertion_point (from);
6337 if (! JUMP_P (last))
6338 return;
6339 curr_id = lra_get_insn_recog_data (last);
6340 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6341 if (reg->type != OP_IN)
6342 bitmap_set_bit (res, reg->regno);
6345 /* Used as a temporary results of some bitmap calculations. */
6346 static bitmap_head temp_bitmap;
6348 /* We split for reloads of small class of hard regs. The following
6349 defines how many hard regs the class should have to be qualified as
6350 small. The code is mostly oriented to x86/x86-64 architecture
6351 where some insns need to use only specific register or pair of
6352 registers and these register can live in RTL explicitly, e.g. for
6353 parameter passing. */
6354 static const int max_small_class_regs_num = 2;
6356 /* Do inheritance/split transformations in EBB starting with HEAD and
6357 finishing on TAIL. We process EBB insns in the reverse order.
6358 Return true if we did any inheritance/split transformation in the
6359 EBB.
6361 We should avoid excessive splitting which results in worse code
6362 because of inaccurate cost calculations for spilling new split
6363 pseudos in such case. To achieve this we do splitting only if
6364 register pressure is high in given basic block and there are reload
6365 pseudos requiring hard registers. We could do more register
6366 pressure calculations at any given program point to avoid necessary
6367 splitting even more but it is to expensive and the current approach
6368 works well enough. */
6369 static bool
6370 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
6372 int i, src_regno, dst_regno, nregs;
6373 bool change_p, succ_p, update_reloads_num_p;
6374 rtx_insn *prev_insn, *last_insn;
6375 rtx next_usage_insns, curr_set;
6376 enum reg_class cl;
6377 struct lra_insn_reg *reg;
6378 basic_block last_processed_bb, curr_bb = NULL;
6379 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
6380 bitmap to_process;
6381 unsigned int j;
6382 bitmap_iterator bi;
6383 bool head_p, after_p;
6385 change_p = false;
6386 curr_usage_insns_check++;
6387 clear_invariants ();
6388 reloads_num = calls_num = 0;
6389 for (unsigned int i = 0; i < NUM_ABI_IDS; ++i)
6390 last_call_for_abi[i] = 0;
6391 CLEAR_HARD_REG_SET (full_and_partial_call_clobbers);
6392 bitmap_clear (&check_only_regs);
6393 bitmap_clear (&invalid_invariant_regs);
6394 last_processed_bb = NULL;
6395 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6396 live_hard_regs = eliminable_regset | lra_no_alloc_regs;
6397 /* We don't process new insns generated in the loop. */
6398 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
6400 prev_insn = PREV_INSN (curr_insn);
6401 if (BLOCK_FOR_INSN (curr_insn) != NULL)
6402 curr_bb = BLOCK_FOR_INSN (curr_insn);
6403 if (last_processed_bb != curr_bb)
6405 /* We are at the end of BB. Add qualified living
6406 pseudos for potential splitting. */
6407 to_process = df_get_live_out (curr_bb);
6408 if (last_processed_bb != NULL)
6410 /* We are somewhere in the middle of EBB. */
6411 get_live_on_other_edges (curr_bb, last_processed_bb,
6412 &temp_bitmap);
6413 to_process = &temp_bitmap;
6415 last_processed_bb = curr_bb;
6416 last_insn = get_last_insertion_point (curr_bb);
6417 after_p = (! JUMP_P (last_insn)
6418 && (! CALL_P (last_insn)
6419 || (find_reg_note (last_insn,
6420 REG_NORETURN, NULL_RTX) == NULL_RTX
6421 && ! SIBLING_CALL_P (last_insn))));
6422 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6423 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
6425 if ((int) j >= lra_constraint_new_regno_start)
6426 break;
6427 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
6429 if (j < FIRST_PSEUDO_REGISTER)
6430 SET_HARD_REG_BIT (live_hard_regs, j);
6431 else
6432 add_to_hard_reg_set (&live_hard_regs,
6433 PSEUDO_REGNO_MODE (j),
6434 reg_renumber[j]);
6435 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
6439 src_regno = dst_regno = -1;
6440 curr_set = single_set (curr_insn);
6441 if (curr_set != NULL_RTX && REG_P (SET_DEST (curr_set)))
6442 dst_regno = REGNO (SET_DEST (curr_set));
6443 if (curr_set != NULL_RTX && REG_P (SET_SRC (curr_set)))
6444 src_regno = REGNO (SET_SRC (curr_set));
6445 update_reloads_num_p = true;
6446 if (src_regno < lra_constraint_new_regno_start
6447 && src_regno >= FIRST_PSEUDO_REGISTER
6448 && reg_renumber[src_regno] < 0
6449 && dst_regno >= lra_constraint_new_regno_start
6450 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
6452 /* 'reload_pseudo <- original_pseudo'. */
6453 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6454 reloads_num++;
6455 update_reloads_num_p = false;
6456 succ_p = false;
6457 if (usage_insns[src_regno].check == curr_usage_insns_check
6458 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
6459 succ_p = inherit_reload_reg (false, src_regno, cl,
6460 curr_insn, next_usage_insns);
6461 if (succ_p)
6462 change_p = true;
6463 else
6464 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6465 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6466 potential_reload_hard_regs |= reg_class_contents[cl];
6468 else if (src_regno < 0
6469 && dst_regno >= lra_constraint_new_regno_start
6470 && invariant_p (SET_SRC (curr_set))
6471 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS
6472 && ! bitmap_bit_p (&invalid_invariant_regs, dst_regno)
6473 && ! bitmap_bit_p (&invalid_invariant_regs,
6474 ORIGINAL_REGNO(regno_reg_rtx[dst_regno])))
6476 /* 'reload_pseudo <- invariant'. */
6477 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6478 reloads_num++;
6479 update_reloads_num_p = false;
6480 if (process_invariant_for_inheritance (SET_DEST (curr_set), SET_SRC (curr_set)))
6481 change_p = true;
6482 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6483 potential_reload_hard_regs |= reg_class_contents[cl];
6485 else if (src_regno >= lra_constraint_new_regno_start
6486 && dst_regno < lra_constraint_new_regno_start
6487 && dst_regno >= FIRST_PSEUDO_REGISTER
6488 && reg_renumber[dst_regno] < 0
6489 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
6490 && usage_insns[dst_regno].check == curr_usage_insns_check
6491 && (next_usage_insns
6492 = usage_insns[dst_regno].insns) != NULL_RTX)
6494 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6495 reloads_num++;
6496 update_reloads_num_p = false;
6497 /* 'original_pseudo <- reload_pseudo'. */
6498 if (! JUMP_P (curr_insn)
6499 && inherit_reload_reg (true, dst_regno, cl,
6500 curr_insn, next_usage_insns))
6501 change_p = true;
6502 /* Invalidate. */
6503 usage_insns[dst_regno].check = 0;
6504 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6505 potential_reload_hard_regs |= reg_class_contents[cl];
6507 else if (INSN_P (curr_insn))
6509 int iter;
6510 int max_uid = get_max_uid ();
6512 curr_id = lra_get_insn_recog_data (curr_insn);
6513 curr_static_id = curr_id->insn_static_data;
6514 to_inherit_num = 0;
6515 /* Process insn definitions. */
6516 for (iter = 0; iter < 2; iter++)
6517 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6518 reg != NULL;
6519 reg = reg->next)
6520 if (reg->type != OP_IN
6521 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
6523 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
6524 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
6525 && usage_insns[dst_regno].check == curr_usage_insns_check
6526 && (next_usage_insns
6527 = usage_insns[dst_regno].insns) != NULL_RTX)
6529 struct lra_insn_reg *r;
6531 for (r = curr_id->regs; r != NULL; r = r->next)
6532 if (r->type != OP_OUT && r->regno == dst_regno)
6533 break;
6534 /* Don't do inheritance if the pseudo is also
6535 used in the insn. */
6536 if (r == NULL)
6537 /* We cannot do inheritance right now
6538 because the current insn reg info (chain
6539 regs) can change after that. */
6540 add_to_inherit (dst_regno, next_usage_insns);
6542 /* We cannot process one reg twice here because of
6543 usage_insns invalidation. */
6544 if ((dst_regno < FIRST_PSEUDO_REGISTER
6545 || reg_renumber[dst_regno] >= 0)
6546 && ! reg->subreg_p && reg->type != OP_IN)
6548 HARD_REG_SET s;
6550 if (split_if_necessary (dst_regno, reg->biggest_mode,
6551 potential_reload_hard_regs,
6552 false, curr_insn, max_uid))
6553 change_p = true;
6554 CLEAR_HARD_REG_SET (s);
6555 if (dst_regno < FIRST_PSEUDO_REGISTER)
6556 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
6557 else
6558 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
6559 reg_renumber[dst_regno]);
6560 live_hard_regs &= ~s;
6561 potential_reload_hard_regs &= ~s;
6563 /* We should invalidate potential inheritance or
6564 splitting for the current insn usages to the next
6565 usage insns (see code below) as the output pseudo
6566 prevents this. */
6567 if ((dst_regno >= FIRST_PSEUDO_REGISTER
6568 && reg_renumber[dst_regno] < 0)
6569 || (reg->type == OP_OUT && ! reg->subreg_p
6570 && (dst_regno < FIRST_PSEUDO_REGISTER
6571 || reg_renumber[dst_regno] >= 0)))
6573 /* Invalidate and mark definitions. */
6574 if (dst_regno >= FIRST_PSEUDO_REGISTER)
6575 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
6576 else
6578 nregs = hard_regno_nregs (dst_regno,
6579 reg->biggest_mode);
6580 for (i = 0; i < nregs; i++)
6581 usage_insns[dst_regno + i].check
6582 = -(int) INSN_UID (curr_insn);
6586 /* Process clobbered call regs. */
6587 if (curr_id->arg_hard_regs != NULL)
6588 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6589 if (dst_regno >= FIRST_PSEUDO_REGISTER)
6590 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
6591 = -(int) INSN_UID (curr_insn);
6592 if (! JUMP_P (curr_insn))
6593 for (i = 0; i < to_inherit_num; i++)
6594 if (inherit_reload_reg (true, to_inherit[i].regno,
6595 ALL_REGS, curr_insn,
6596 to_inherit[i].insns))
6597 change_p = true;
6598 if (CALL_P (curr_insn))
6600 rtx cheap, pat, dest;
6601 rtx_insn *restore;
6602 int regno, hard_regno;
6604 calls_num++;
6605 function_abi callee_abi = insn_callee_abi (curr_insn);
6606 last_call_for_abi[callee_abi.id ()] = calls_num;
6607 full_and_partial_call_clobbers
6608 |= callee_abi.full_and_partial_reg_clobbers ();
6609 if ((cheap = find_reg_note (curr_insn,
6610 REG_RETURNED, NULL_RTX)) != NULL_RTX
6611 && ((cheap = XEXP (cheap, 0)), true)
6612 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
6613 && (hard_regno = reg_renumber[regno]) >= 0
6614 && usage_insns[regno].check == curr_usage_insns_check
6615 /* If there are pending saves/restores, the
6616 optimization is not worth. */
6617 && usage_insns[regno].calls_num == calls_num - 1
6618 && callee_abi.clobbers_reg_p (GET_MODE (cheap), hard_regno))
6620 /* Restore the pseudo from the call result as
6621 REG_RETURNED note says that the pseudo value is
6622 in the call result and the pseudo is an argument
6623 of the call. */
6624 pat = PATTERN (curr_insn);
6625 if (GET_CODE (pat) == PARALLEL)
6626 pat = XVECEXP (pat, 0, 0);
6627 dest = SET_DEST (pat);
6628 /* For multiple return values dest is PARALLEL.
6629 Currently we handle only single return value case. */
6630 if (REG_P (dest))
6632 start_sequence ();
6633 emit_move_insn (cheap, copy_rtx (dest));
6634 restore = get_insns ();
6635 end_sequence ();
6636 lra_process_new_insns (curr_insn, NULL, restore,
6637 "Inserting call parameter restore");
6638 /* We don't need to save/restore of the pseudo from
6639 this call. */
6640 usage_insns[regno].calls_num = calls_num;
6641 remove_from_hard_reg_set
6642 (&full_and_partial_call_clobbers,
6643 GET_MODE (cheap), hard_regno);
6644 bitmap_set_bit (&check_only_regs, regno);
6648 to_inherit_num = 0;
6649 /* Process insn usages. */
6650 for (iter = 0; iter < 2; iter++)
6651 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6652 reg != NULL;
6653 reg = reg->next)
6654 if ((reg->type != OP_OUT
6655 || (reg->type == OP_OUT && reg->subreg_p))
6656 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
6658 if (src_regno >= FIRST_PSEUDO_REGISTER
6659 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
6661 if (usage_insns[src_regno].check == curr_usage_insns_check
6662 && (next_usage_insns
6663 = usage_insns[src_regno].insns) != NULL_RTX
6664 && NONDEBUG_INSN_P (curr_insn))
6665 add_to_inherit (src_regno, next_usage_insns);
6666 else if (usage_insns[src_regno].check
6667 != -(int) INSN_UID (curr_insn))
6668 /* Add usages but only if the reg is not set up
6669 in the same insn. */
6670 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6672 else if (src_regno < FIRST_PSEUDO_REGISTER
6673 || reg_renumber[src_regno] >= 0)
6675 bool before_p;
6676 rtx_insn *use_insn = curr_insn;
6678 before_p = (JUMP_P (curr_insn)
6679 || (CALL_P (curr_insn) && reg->type == OP_IN));
6680 if (NONDEBUG_INSN_P (curr_insn)
6681 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
6682 && split_if_necessary (src_regno, reg->biggest_mode,
6683 potential_reload_hard_regs,
6684 before_p, curr_insn, max_uid))
6686 if (reg->subreg_p)
6687 check_and_force_assignment_correctness_p = true;
6688 change_p = true;
6689 /* Invalidate. */
6690 usage_insns[src_regno].check = 0;
6691 if (before_p)
6692 use_insn = PREV_INSN (curr_insn);
6694 if (NONDEBUG_INSN_P (curr_insn))
6696 if (src_regno < FIRST_PSEUDO_REGISTER)
6697 add_to_hard_reg_set (&live_hard_regs,
6698 reg->biggest_mode, src_regno);
6699 else
6700 add_to_hard_reg_set (&live_hard_regs,
6701 PSEUDO_REGNO_MODE (src_regno),
6702 reg_renumber[src_regno]);
6704 if (src_regno >= FIRST_PSEUDO_REGISTER)
6705 add_next_usage_insn (src_regno, use_insn, reloads_num);
6706 else
6708 for (i = 0; i < hard_regno_nregs (src_regno, reg->biggest_mode); i++)
6709 add_next_usage_insn (src_regno + i, use_insn, reloads_num);
6713 /* Process used call regs. */
6714 if (curr_id->arg_hard_regs != NULL)
6715 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6716 if (src_regno < FIRST_PSEUDO_REGISTER)
6718 SET_HARD_REG_BIT (live_hard_regs, src_regno);
6719 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6721 for (i = 0; i < to_inherit_num; i++)
6723 src_regno = to_inherit[i].regno;
6724 if (inherit_reload_reg (false, src_regno, ALL_REGS,
6725 curr_insn, to_inherit[i].insns))
6726 change_p = true;
6727 else
6728 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6731 if (update_reloads_num_p
6732 && NONDEBUG_INSN_P (curr_insn) && curr_set != NULL_RTX)
6734 int regno = -1;
6735 if ((REG_P (SET_DEST (curr_set))
6736 && (regno = REGNO (SET_DEST (curr_set))) >= lra_constraint_new_regno_start
6737 && reg_renumber[regno] < 0
6738 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
6739 || (REG_P (SET_SRC (curr_set))
6740 && (regno = REGNO (SET_SRC (curr_set))) >= lra_constraint_new_regno_start
6741 && reg_renumber[regno] < 0
6742 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
6744 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6745 reloads_num++;
6746 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6747 potential_reload_hard_regs |= reg_class_contents[cl];
6750 if (NONDEBUG_INSN_P (curr_insn))
6752 int regno;
6754 /* Invalidate invariants with changed regs. */
6755 curr_id = lra_get_insn_recog_data (curr_insn);
6756 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6757 if (reg->type != OP_IN)
6759 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6760 bitmap_set_bit (&invalid_invariant_regs,
6761 ORIGINAL_REGNO (regno_reg_rtx[reg->regno]));
6763 curr_static_id = curr_id->insn_static_data;
6764 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6765 if (reg->type != OP_IN)
6766 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6767 if (curr_id->arg_hard_regs != NULL)
6768 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6769 if (regno >= FIRST_PSEUDO_REGISTER)
6770 bitmap_set_bit (&invalid_invariant_regs,
6771 regno - FIRST_PSEUDO_REGISTER);
6773 /* We reached the start of the current basic block. */
6774 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
6775 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
6777 /* We reached the beginning of the current block -- do
6778 rest of spliting in the current BB. */
6779 to_process = df_get_live_in (curr_bb);
6780 if (BLOCK_FOR_INSN (head) != curr_bb)
6782 /* We are somewhere in the middle of EBB. */
6783 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
6784 curr_bb, &temp_bitmap);
6785 to_process = &temp_bitmap;
6787 head_p = true;
6788 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
6790 if ((int) j >= lra_constraint_new_regno_start)
6791 break;
6792 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
6793 && usage_insns[j].check == curr_usage_insns_check
6794 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
6796 if (need_for_split_p (potential_reload_hard_regs, j))
6798 if (lra_dump_file != NULL && head_p)
6800 fprintf (lra_dump_file,
6801 " ----------------------------------\n");
6802 head_p = false;
6804 if (split_reg (false, j, bb_note (curr_bb),
6805 next_usage_insns, NULL))
6806 change_p = true;
6808 usage_insns[j].check = 0;
6813 return change_p;
6816 /* This value affects EBB forming. If probability of edge from EBB to
6817 a BB is not greater than the following value, we don't add the BB
6818 to EBB. */
6819 #define EBB_PROBABILITY_CUTOFF \
6820 ((REG_BR_PROB_BASE * param_lra_inheritance_ebb_probability_cutoff) / 100)
6822 /* Current number of inheritance/split iteration. */
6823 int lra_inheritance_iter;
6825 /* Entry function for inheritance/split pass. */
6826 void
6827 lra_inheritance (void)
6829 int i;
6830 basic_block bb, start_bb;
6831 edge e;
6833 lra_inheritance_iter++;
6834 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6835 return;
6836 timevar_push (TV_LRA_INHERITANCE);
6837 if (lra_dump_file != NULL)
6838 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
6839 lra_inheritance_iter);
6840 curr_usage_insns_check = 0;
6841 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
6842 for (i = 0; i < lra_constraint_new_regno_start; i++)
6843 usage_insns[i].check = 0;
6844 bitmap_initialize (&check_only_regs, &reg_obstack);
6845 bitmap_initialize (&invalid_invariant_regs, &reg_obstack);
6846 bitmap_initialize (&live_regs, &reg_obstack);
6847 bitmap_initialize (&temp_bitmap, &reg_obstack);
6848 bitmap_initialize (&ebb_global_regs, &reg_obstack);
6849 FOR_EACH_BB_FN (bb, cfun)
6851 start_bb = bb;
6852 if (lra_dump_file != NULL)
6853 fprintf (lra_dump_file, "EBB");
6854 /* Form a EBB starting with BB. */
6855 bitmap_clear (&ebb_global_regs);
6856 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
6857 for (;;)
6859 if (lra_dump_file != NULL)
6860 fprintf (lra_dump_file, " %d", bb->index);
6861 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
6862 || LABEL_P (BB_HEAD (bb->next_bb)))
6863 break;
6864 e = find_fallthru_edge (bb->succs);
6865 if (! e)
6866 break;
6867 if (e->probability.initialized_p ()
6868 && e->probability.to_reg_br_prob_base () < EBB_PROBABILITY_CUTOFF)
6869 break;
6870 bb = bb->next_bb;
6872 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
6873 if (lra_dump_file != NULL)
6874 fprintf (lra_dump_file, "\n");
6875 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
6876 /* Remember that the EBB head and tail can change in
6877 inherit_in_ebb. */
6878 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
6880 bitmap_release (&ebb_global_regs);
6881 bitmap_release (&temp_bitmap);
6882 bitmap_release (&live_regs);
6883 bitmap_release (&invalid_invariant_regs);
6884 bitmap_release (&check_only_regs);
6885 free (usage_insns);
6887 timevar_pop (TV_LRA_INHERITANCE);
6892 /* This page contains code to undo failed inheritance/split
6893 transformations. */
6895 /* Current number of iteration undoing inheritance/split. */
6896 int lra_undo_inheritance_iter;
6898 /* Fix BB live info LIVE after removing pseudos created on pass doing
6899 inheritance/split which are REMOVED_PSEUDOS. */
6900 static void
6901 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
6903 unsigned int regno;
6904 bitmap_iterator bi;
6906 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
6907 if (bitmap_clear_bit (live, regno)
6908 && REG_P (lra_reg_info[regno].restore_rtx))
6909 bitmap_set_bit (live, REGNO (lra_reg_info[regno].restore_rtx));
6912 /* Return regno of the (subreg of) REG. Otherwise, return a negative
6913 number. */
6914 static int
6915 get_regno (rtx reg)
6917 if (GET_CODE (reg) == SUBREG)
6918 reg = SUBREG_REG (reg);
6919 if (REG_P (reg))
6920 return REGNO (reg);
6921 return -1;
6924 /* Delete a move INSN with destination reg DREGNO and a previous
6925 clobber insn with the same regno. The inheritance/split code can
6926 generate moves with preceding clobber and when we delete such moves
6927 we should delete the clobber insn too to keep the correct life
6928 info. */
6929 static void
6930 delete_move_and_clobber (rtx_insn *insn, int dregno)
6932 rtx_insn *prev_insn = PREV_INSN (insn);
6934 lra_set_insn_deleted (insn);
6935 lra_assert (dregno >= 0);
6936 if (prev_insn != NULL && NONDEBUG_INSN_P (prev_insn)
6937 && GET_CODE (PATTERN (prev_insn)) == CLOBBER
6938 && dregno == get_regno (XEXP (PATTERN (prev_insn), 0)))
6939 lra_set_insn_deleted (prev_insn);
6942 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
6943 return true if we did any change. The undo transformations for
6944 inheritance looks like
6945 i <- i2
6946 p <- i => p <- i2
6947 or removing
6948 p <- i, i <- p, and i <- i3
6949 where p is original pseudo from which inheritance pseudo i was
6950 created, i and i3 are removed inheritance pseudos, i2 is another
6951 not removed inheritance pseudo. All split pseudos or other
6952 occurrences of removed inheritance pseudos are changed on the
6953 corresponding original pseudos.
6955 The function also schedules insns changed and created during
6956 inheritance/split pass for processing by the subsequent constraint
6957 pass. */
6958 static bool
6959 remove_inheritance_pseudos (bitmap remove_pseudos)
6961 basic_block bb;
6962 int regno, sregno, prev_sregno, dregno;
6963 rtx restore_rtx;
6964 rtx set, prev_set;
6965 rtx_insn *prev_insn;
6966 bool change_p, done_p;
6968 change_p = ! bitmap_empty_p (remove_pseudos);
6969 /* We cannot finish the function right away if CHANGE_P is true
6970 because we need to marks insns affected by previous
6971 inheritance/split pass for processing by the subsequent
6972 constraint pass. */
6973 FOR_EACH_BB_FN (bb, cfun)
6975 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
6976 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
6977 FOR_BB_INSNS_REVERSE (bb, curr_insn)
6979 if (! INSN_P (curr_insn))
6980 continue;
6981 done_p = false;
6982 sregno = dregno = -1;
6983 if (change_p && NONDEBUG_INSN_P (curr_insn)
6984 && (set = single_set (curr_insn)) != NULL_RTX)
6986 dregno = get_regno (SET_DEST (set));
6987 sregno = get_regno (SET_SRC (set));
6990 if (sregno >= 0 && dregno >= 0)
6992 if (bitmap_bit_p (remove_pseudos, dregno)
6993 && ! REG_P (lra_reg_info[dregno].restore_rtx))
6995 /* invariant inheritance pseudo <- original pseudo */
6996 if (lra_dump_file != NULL)
6998 fprintf (lra_dump_file, " Removing invariant inheritance:\n");
6999 dump_insn_slim (lra_dump_file, curr_insn);
7000 fprintf (lra_dump_file, "\n");
7002 delete_move_and_clobber (curr_insn, dregno);
7003 done_p = true;
7005 else if (bitmap_bit_p (remove_pseudos, sregno)
7006 && ! REG_P (lra_reg_info[sregno].restore_rtx))
7008 /* reload pseudo <- invariant inheritance pseudo */
7009 start_sequence ();
7010 /* We cannot just change the source. It might be
7011 an insn different from the move. */
7012 emit_insn (lra_reg_info[sregno].restore_rtx);
7013 rtx_insn *new_insns = get_insns ();
7014 end_sequence ();
7015 lra_assert (single_set (new_insns) != NULL
7016 && SET_DEST (set) == SET_DEST (single_set (new_insns)));
7017 lra_process_new_insns (curr_insn, NULL, new_insns,
7018 "Changing reload<-invariant inheritance");
7019 delete_move_and_clobber (curr_insn, dregno);
7020 done_p = true;
7022 else if ((bitmap_bit_p (remove_pseudos, sregno)
7023 && (get_regno (lra_reg_info[sregno].restore_rtx) == dregno
7024 || (bitmap_bit_p (remove_pseudos, dregno)
7025 && get_regno (lra_reg_info[sregno].restore_rtx) >= 0
7026 && (get_regno (lra_reg_info[sregno].restore_rtx)
7027 == get_regno (lra_reg_info[dregno].restore_rtx)))))
7028 || (bitmap_bit_p (remove_pseudos, dregno)
7029 && get_regno (lra_reg_info[dregno].restore_rtx) == sregno))
7030 /* One of the following cases:
7031 original <- removed inheritance pseudo
7032 removed inherit pseudo <- another removed inherit pseudo
7033 removed inherit pseudo <- original pseudo
7035 removed_split_pseudo <- original_reg
7036 original_reg <- removed_split_pseudo */
7038 if (lra_dump_file != NULL)
7040 fprintf (lra_dump_file, " Removing %s:\n",
7041 bitmap_bit_p (&lra_split_regs, sregno)
7042 || bitmap_bit_p (&lra_split_regs, dregno)
7043 ? "split" : "inheritance");
7044 dump_insn_slim (lra_dump_file, curr_insn);
7046 delete_move_and_clobber (curr_insn, dregno);
7047 done_p = true;
7049 else if (bitmap_bit_p (remove_pseudos, sregno)
7050 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
7052 /* Search the following pattern:
7053 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
7054 original_pseudo <- inherit_or_split_pseudo1
7055 where the 2nd insn is the current insn and
7056 inherit_or_split_pseudo2 is not removed. If it is found,
7057 change the current insn onto:
7058 original_pseudo <- inherit_or_split_pseudo2. */
7059 for (prev_insn = PREV_INSN (curr_insn);
7060 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
7061 prev_insn = PREV_INSN (prev_insn))
7063 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
7064 && (prev_set = single_set (prev_insn)) != NULL_RTX
7065 /* There should be no subregs in insn we are
7066 searching because only the original reg might
7067 be in subreg when we changed the mode of
7068 load/store for splitting. */
7069 && REG_P (SET_DEST (prev_set))
7070 && REG_P (SET_SRC (prev_set))
7071 && (int) REGNO (SET_DEST (prev_set)) == sregno
7072 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
7073 >= FIRST_PSEUDO_REGISTER)
7074 && (lra_reg_info[prev_sregno].restore_rtx == NULL_RTX
7076 /* As we consider chain of inheritance or
7077 splitting described in above comment we should
7078 check that sregno and prev_sregno were
7079 inheritance/split pseudos created from the
7080 same original regno. */
7081 (get_regno (lra_reg_info[sregno].restore_rtx) >= 0
7082 && (get_regno (lra_reg_info[sregno].restore_rtx)
7083 == get_regno (lra_reg_info[prev_sregno].restore_rtx))))
7084 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
7086 lra_assert (GET_MODE (SET_SRC (prev_set))
7087 == GET_MODE (regno_reg_rtx[sregno]));
7088 /* Although we have a single set, the insn can
7089 contain more one sregno register occurrence
7090 as a source. Change all occurrences. */
7091 lra_substitute_pseudo_within_insn (curr_insn, sregno,
7092 SET_SRC (prev_set),
7093 false);
7094 /* As we are finishing with processing the insn
7095 here, check the destination too as it might
7096 inheritance pseudo for another pseudo. */
7097 if (bitmap_bit_p (remove_pseudos, dregno)
7098 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
7099 && (restore_rtx
7100 = lra_reg_info[dregno].restore_rtx) != NULL_RTX)
7102 if (GET_CODE (SET_DEST (set)) == SUBREG)
7103 SUBREG_REG (SET_DEST (set)) = restore_rtx;
7104 else
7105 SET_DEST (set) = restore_rtx;
7107 lra_push_insn_and_update_insn_regno_info (curr_insn);
7108 lra_set_used_insn_alternative_by_uid
7109 (INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
7110 done_p = true;
7111 if (lra_dump_file != NULL)
7113 fprintf (lra_dump_file, " Change reload insn:\n");
7114 dump_insn_slim (lra_dump_file, curr_insn);
7119 if (! done_p)
7121 struct lra_insn_reg *reg;
7122 bool restored_regs_p = false;
7123 bool kept_regs_p = false;
7125 curr_id = lra_get_insn_recog_data (curr_insn);
7126 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
7128 regno = reg->regno;
7129 restore_rtx = lra_reg_info[regno].restore_rtx;
7130 if (restore_rtx != NULL_RTX)
7132 if (change_p && bitmap_bit_p (remove_pseudos, regno))
7134 lra_substitute_pseudo_within_insn
7135 (curr_insn, regno, restore_rtx, false);
7136 restored_regs_p = true;
7138 else
7139 kept_regs_p = true;
7142 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
7144 /* The instruction has changed since the previous
7145 constraints pass. */
7146 lra_push_insn_and_update_insn_regno_info (curr_insn);
7147 lra_set_used_insn_alternative_by_uid
7148 (INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
7150 else if (restored_regs_p)
7151 /* The instruction has been restored to the form that
7152 it had during the previous constraints pass. */
7153 lra_update_insn_regno_info (curr_insn);
7154 if (restored_regs_p && lra_dump_file != NULL)
7156 fprintf (lra_dump_file, " Insn after restoring regs:\n");
7157 dump_insn_slim (lra_dump_file, curr_insn);
7162 return change_p;
7165 /* If optional reload pseudos failed to get a hard register or was not
7166 inherited, it is better to remove optional reloads. We do this
7167 transformation after undoing inheritance to figure out necessity to
7168 remove optional reloads easier. Return true if we do any
7169 change. */
7170 static bool
7171 undo_optional_reloads (void)
7173 bool change_p, keep_p;
7174 unsigned int regno, uid;
7175 bitmap_iterator bi, bi2;
7176 rtx_insn *insn;
7177 rtx set, src, dest;
7178 auto_bitmap removed_optional_reload_pseudos (&reg_obstack);
7180 bitmap_copy (removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
7181 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7183 keep_p = false;
7184 /* Keep optional reloads from previous subpasses. */
7185 if (lra_reg_info[regno].restore_rtx == NULL_RTX
7186 /* If the original pseudo changed its allocation, just
7187 removing the optional pseudo is dangerous as the original
7188 pseudo will have longer live range. */
7189 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] >= 0)
7190 keep_p = true;
7191 else if (reg_renumber[regno] >= 0)
7192 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
7194 insn = lra_insn_recog_data[uid]->insn;
7195 if ((set = single_set (insn)) == NULL_RTX)
7196 continue;
7197 src = SET_SRC (set);
7198 dest = SET_DEST (set);
7199 if (! REG_P (src) || ! REG_P (dest))
7200 continue;
7201 if (REGNO (dest) == regno
7202 /* Ignore insn for optional reloads itself. */
7203 && REGNO (lra_reg_info[regno].restore_rtx) != REGNO (src)
7204 /* Check only inheritance on last inheritance pass. */
7205 && (int) REGNO (src) >= new_regno_start
7206 /* Check that the optional reload was inherited. */
7207 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
7209 keep_p = true;
7210 break;
7213 if (keep_p)
7215 bitmap_clear_bit (removed_optional_reload_pseudos, regno);
7216 if (lra_dump_file != NULL)
7217 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
7220 change_p = ! bitmap_empty_p (removed_optional_reload_pseudos);
7221 auto_bitmap insn_bitmap (&reg_obstack);
7222 EXECUTE_IF_SET_IN_BITMAP (removed_optional_reload_pseudos, 0, regno, bi)
7224 if (lra_dump_file != NULL)
7225 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
7226 bitmap_copy (insn_bitmap, &lra_reg_info[regno].insn_bitmap);
7227 EXECUTE_IF_SET_IN_BITMAP (insn_bitmap, 0, uid, bi2)
7229 insn = lra_insn_recog_data[uid]->insn;
7230 if ((set = single_set (insn)) != NULL_RTX)
7232 src = SET_SRC (set);
7233 dest = SET_DEST (set);
7234 if (REG_P (src) && REG_P (dest)
7235 && ((REGNO (src) == regno
7236 && (REGNO (lra_reg_info[regno].restore_rtx)
7237 == REGNO (dest)))
7238 || (REGNO (dest) == regno
7239 && (REGNO (lra_reg_info[regno].restore_rtx)
7240 == REGNO (src)))))
7242 if (lra_dump_file != NULL)
7244 fprintf (lra_dump_file, " Deleting move %u\n",
7245 INSN_UID (insn));
7246 dump_insn_slim (lra_dump_file, insn);
7248 delete_move_and_clobber (insn, REGNO (dest));
7249 continue;
7251 /* We should not worry about generation memory-memory
7252 moves here as if the corresponding inheritance did
7253 not work (inheritance pseudo did not get a hard reg),
7254 we remove the inheritance pseudo and the optional
7255 reload. */
7257 lra_substitute_pseudo_within_insn
7258 (insn, regno, lra_reg_info[regno].restore_rtx, false);
7259 lra_update_insn_regno_info (insn);
7260 if (lra_dump_file != NULL)
7262 fprintf (lra_dump_file,
7263 " Restoring original insn:\n");
7264 dump_insn_slim (lra_dump_file, insn);
7268 /* Clear restore_regnos. */
7269 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7270 lra_reg_info[regno].restore_rtx = NULL_RTX;
7271 return change_p;
7274 /* Entry function for undoing inheritance/split transformation. Return true
7275 if we did any RTL change in this pass. */
7276 bool
7277 lra_undo_inheritance (void)
7279 unsigned int regno;
7280 int hard_regno;
7281 int n_all_inherit, n_inherit, n_all_split, n_split;
7282 rtx restore_rtx;
7283 bitmap_iterator bi;
7284 bool change_p;
7286 lra_undo_inheritance_iter++;
7287 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
7288 return false;
7289 if (lra_dump_file != NULL)
7290 fprintf (lra_dump_file,
7291 "\n********** Undoing inheritance #%d: **********\n\n",
7292 lra_undo_inheritance_iter);
7293 auto_bitmap remove_pseudos (&reg_obstack);
7294 n_inherit = n_all_inherit = 0;
7295 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7296 if (lra_reg_info[regno].restore_rtx != NULL_RTX)
7298 n_all_inherit++;
7299 if (reg_renumber[regno] < 0
7300 /* If the original pseudo changed its allocation, just
7301 removing inheritance is dangerous as for changing
7302 allocation we used shorter live-ranges. */
7303 && (! REG_P (lra_reg_info[regno].restore_rtx)
7304 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] < 0))
7305 bitmap_set_bit (remove_pseudos, regno);
7306 else
7307 n_inherit++;
7309 if (lra_dump_file != NULL && n_all_inherit != 0)
7310 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
7311 n_inherit, n_all_inherit,
7312 (double) n_inherit / n_all_inherit * 100);
7313 n_split = n_all_split = 0;
7314 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7315 if ((restore_rtx = lra_reg_info[regno].restore_rtx) != NULL_RTX)
7317 int restore_regno = REGNO (restore_rtx);
7319 n_all_split++;
7320 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
7321 ? reg_renumber[restore_regno] : restore_regno);
7322 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
7323 bitmap_set_bit (remove_pseudos, regno);
7324 else
7326 n_split++;
7327 if (lra_dump_file != NULL)
7328 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
7329 regno, restore_regno);
7332 if (lra_dump_file != NULL && n_all_split != 0)
7333 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
7334 n_split, n_all_split,
7335 (double) n_split / n_all_split * 100);
7336 change_p = remove_inheritance_pseudos (remove_pseudos);
7337 /* Clear restore_regnos. */
7338 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7339 lra_reg_info[regno].restore_rtx = NULL_RTX;
7340 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7341 lra_reg_info[regno].restore_rtx = NULL_RTX;
7342 change_p = undo_optional_reloads () || change_p;
7343 return change_p;