testsuite: Correct vec-rlmi-rlnm.c testsuite expected result
[official-gcc.git] / gcc / lra-constraints.c
blobb5c010d503013ad4f693de0a0f32c4fff1744fca
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2020 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains code for 3 passes: constraint pass,
23 inheritance/split pass, and pass for undoing failed inheritance and
24 split.
26 The major goal of constraint pass is to transform RTL to satisfy
27 insn and address constraints by:
28 o choosing insn alternatives;
29 o generating *reload insns* (or reloads in brief) and *reload
30 pseudos* which will get necessary hard registers later;
31 o substituting pseudos with equivalent values and removing the
32 instructions that initialized those pseudos.
34 The constraint pass has biggest and most complicated code in LRA.
35 There are a lot of important details like:
36 o reuse of input reload pseudos to simplify reload pseudo
37 allocations;
38 o some heuristics to choose insn alternative to improve the
39 inheritance;
40 o early clobbers etc.
42 The pass is mimicking former reload pass in alternative choosing
43 because the reload pass is oriented to current machine description
44 model. It might be changed if the machine description model is
45 changed.
47 There is special code for preventing all LRA and this pass cycling
48 in case of bugs.
50 On the first iteration of the pass we process every instruction and
51 choose an alternative for each one. On subsequent iterations we try
52 to avoid reprocessing instructions if we can be sure that the old
53 choice is still valid.
55 The inheritance/spilt pass is to transform code to achieve
56 ineheritance and live range splitting. It is done on backward
57 traversal of EBBs.
59 The inheritance optimization goal is to reuse values in hard
60 registers. There is analogous optimization in old reload pass. The
61 inheritance is achieved by following transformation:
63 reload_p1 <- p reload_p1 <- p
64 ... new_p <- reload_p1
65 ... => ...
66 reload_p2 <- p reload_p2 <- new_p
68 where p is spilled and not changed between the insns. Reload_p1 is
69 also called *original pseudo* and new_p is called *inheritance
70 pseudo*.
72 The subsequent assignment pass will try to assign the same (or
73 another if it is not possible) hard register to new_p as to
74 reload_p1 or reload_p2.
76 If the assignment pass fails to assign a hard register to new_p,
77 this file will undo the inheritance and restore the original code.
78 This is because implementing the above sequence with a spilled
79 new_p would make the code much worse. The inheritance is done in
80 EBB scope. The above is just a simplified example to get an idea
81 of the inheritance as the inheritance is also done for non-reload
82 insns.
84 Splitting (transformation) is also done in EBB scope on the same
85 pass as the inheritance:
87 r <- ... or ... <- r r <- ... or ... <- r
88 ... s <- r (new insn -- save)
89 ... =>
90 ... r <- s (new insn -- restore)
91 ... <- r ... <- r
93 The *split pseudo* s is assigned to the hard register of the
94 original pseudo or hard register r.
96 Splitting is done:
97 o In EBBs with high register pressure for global pseudos (living
98 in at least 2 BBs) and assigned to hard registers when there
99 are more one reloads needing the hard registers;
100 o for pseudos needing save/restore code around calls.
102 If the split pseudo still has the same hard register as the
103 original pseudo after the subsequent assignment pass or the
104 original pseudo was split, the opposite transformation is done on
105 the same pass for undoing inheritance. */
107 #undef REG_OK_STRICT
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "backend.h"
113 #include "target.h"
114 #include "rtl.h"
115 #include "tree.h"
116 #include "predict.h"
117 #include "df.h"
118 #include "memmodel.h"
119 #include "tm_p.h"
120 #include "expmed.h"
121 #include "optabs.h"
122 #include "regs.h"
123 #include "ira.h"
124 #include "recog.h"
125 #include "output.h"
126 #include "addresses.h"
127 #include "expr.h"
128 #include "cfgrtl.h"
129 #include "rtl-error.h"
130 #include "lra.h"
131 #include "lra-int.h"
132 #include "print-rtl.h"
133 #include "function-abi.h"
134 #include "rtl-iter.h"
136 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
137 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
138 reload insns. */
139 static int bb_reload_num;
141 /* The current insn being processed and corresponding its single set
142 (NULL otherwise), its data (basic block, the insn data, the insn
143 static data, and the mode of each operand). */
144 static rtx_insn *curr_insn;
145 static rtx curr_insn_set;
146 static basic_block curr_bb;
147 static lra_insn_recog_data_t curr_id;
148 static struct lra_static_insn_data *curr_static_id;
149 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
150 /* Mode of the register substituted by its equivalence with VOIDmode
151 (e.g. constant) and whose subreg is given operand of the current
152 insn. VOIDmode in all other cases. */
153 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
157 /* Start numbers for new registers and insns at the current constraints
158 pass start. */
159 static int new_regno_start;
160 static int new_insn_uid_start;
162 /* If LOC is nonnull, strip any outer subreg from it. */
163 static inline rtx *
164 strip_subreg (rtx *loc)
166 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
169 /* Return hard regno of REGNO or if it is was not assigned to a hard
170 register, use a hard register from its allocno class. */
171 static int
172 get_try_hard_regno (int regno)
174 int hard_regno;
175 enum reg_class rclass;
177 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
178 hard_regno = lra_get_regno_hard_regno (regno);
179 if (hard_regno >= 0)
180 return hard_regno;
181 rclass = lra_get_allocno_class (regno);
182 if (rclass == NO_REGS)
183 return -1;
184 return ira_class_hard_regs[rclass][0];
187 /* Return the hard regno of X after removing its subreg. If X is not
188 a register or a subreg of a register, return -1. If X is a pseudo,
189 use its assignment. If FINAL_P return the final hard regno which will
190 be after elimination. */
191 static int
192 get_hard_regno (rtx x, bool final_p)
194 rtx reg;
195 int hard_regno;
197 reg = x;
198 if (SUBREG_P (x))
199 reg = SUBREG_REG (x);
200 if (! REG_P (reg))
201 return -1;
202 if (! HARD_REGISTER_NUM_P (hard_regno = REGNO (reg)))
203 hard_regno = lra_get_regno_hard_regno (hard_regno);
204 if (hard_regno < 0)
205 return -1;
206 if (final_p)
207 hard_regno = lra_get_elimination_hard_regno (hard_regno);
208 if (SUBREG_P (x))
209 hard_regno += subreg_regno_offset (hard_regno, GET_MODE (reg),
210 SUBREG_BYTE (x), GET_MODE (x));
211 return hard_regno;
214 /* If REGNO is a hard register or has been allocated a hard register,
215 return the class of that register. If REGNO is a reload pseudo
216 created by the current constraints pass, return its allocno class.
217 Return NO_REGS otherwise. */
218 static enum reg_class
219 get_reg_class (int regno)
221 int hard_regno;
223 if (! HARD_REGISTER_NUM_P (hard_regno = regno))
224 hard_regno = lra_get_regno_hard_regno (regno);
225 if (hard_regno >= 0)
227 hard_regno = lra_get_elimination_hard_regno (hard_regno);
228 return REGNO_REG_CLASS (hard_regno);
230 if (regno >= new_regno_start)
231 return lra_get_allocno_class (regno);
232 return NO_REGS;
235 /* Return true if REG satisfies (or will satisfy) reg class constraint
236 CL. Use elimination first if REG is a hard register. If REG is a
237 reload pseudo created by this constraints pass, assume that it will
238 be allocated a hard register from its allocno class, but allow that
239 class to be narrowed to CL if it is currently a superset of CL and
240 if either:
242 - ALLOW_ALL_RELOAD_CLASS_CHANGES_P is true or
243 - the instruction we're processing is not a reload move.
245 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
246 REGNO (reg), or NO_REGS if no change in its class was needed. */
247 static bool
248 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class,
249 bool allow_all_reload_class_changes_p = false)
251 enum reg_class rclass, common_class;
252 machine_mode reg_mode;
253 int class_size, hard_regno, nregs, i, j;
254 int regno = REGNO (reg);
256 if (new_class != NULL)
257 *new_class = NO_REGS;
258 if (regno < FIRST_PSEUDO_REGISTER)
260 rtx final_reg = reg;
261 rtx *final_loc = &final_reg;
263 lra_eliminate_reg_if_possible (final_loc);
264 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
266 reg_mode = GET_MODE (reg);
267 rclass = get_reg_class (regno);
268 if (regno < new_regno_start
269 /* Do not allow the constraints for reload instructions to
270 influence the classes of new pseudos. These reloads are
271 typically moves that have many alternatives, and restricting
272 reload pseudos for one alternative may lead to situations
273 where other reload pseudos are no longer allocatable. */
274 || (!allow_all_reload_class_changes_p
275 && INSN_UID (curr_insn) >= new_insn_uid_start
276 && curr_insn_set != NULL
277 && ((OBJECT_P (SET_SRC (curr_insn_set))
278 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
279 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
280 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
281 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
282 /* When we don't know what class will be used finally for reload
283 pseudos, we use ALL_REGS. */
284 return ((regno >= new_regno_start && rclass == ALL_REGS)
285 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
286 && ! hard_reg_set_subset_p (reg_class_contents[cl],
287 lra_no_alloc_regs)));
288 else
290 common_class = ira_reg_class_subset[rclass][cl];
291 if (new_class != NULL)
292 *new_class = common_class;
293 if (hard_reg_set_subset_p (reg_class_contents[common_class],
294 lra_no_alloc_regs))
295 return false;
296 /* Check that there are enough allocatable regs. */
297 class_size = ira_class_hard_regs_num[common_class];
298 for (i = 0; i < class_size; i++)
300 hard_regno = ira_class_hard_regs[common_class][i];
301 nregs = hard_regno_nregs (hard_regno, reg_mode);
302 if (nregs == 1)
303 return true;
304 for (j = 0; j < nregs; j++)
305 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
306 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
307 hard_regno + j))
308 break;
309 if (j >= nregs)
310 return true;
312 return false;
316 /* Return true if REGNO satisfies a memory constraint. */
317 static bool
318 in_mem_p (int regno)
320 return get_reg_class (regno) == NO_REGS;
323 /* Return 1 if ADDR is a valid memory address for mode MODE in address
324 space AS, and check that each pseudo has the proper kind of hard
325 reg. */
326 static int
327 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
328 rtx addr, addr_space_t as)
330 #ifdef GO_IF_LEGITIMATE_ADDRESS
331 lra_assert (ADDR_SPACE_GENERIC_P (as));
332 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
333 return 0;
335 win:
336 return 1;
337 #else
338 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
339 #endif
342 namespace {
343 /* Temporarily eliminates registers in an address (for the lifetime of
344 the object). */
345 class address_eliminator {
346 public:
347 address_eliminator (struct address_info *ad);
348 ~address_eliminator ();
350 private:
351 struct address_info *m_ad;
352 rtx *m_base_loc;
353 rtx m_base_reg;
354 rtx *m_index_loc;
355 rtx m_index_reg;
359 address_eliminator::address_eliminator (struct address_info *ad)
360 : m_ad (ad),
361 m_base_loc (strip_subreg (ad->base_term)),
362 m_base_reg (NULL_RTX),
363 m_index_loc (strip_subreg (ad->index_term)),
364 m_index_reg (NULL_RTX)
366 if (m_base_loc != NULL)
368 m_base_reg = *m_base_loc;
369 /* If we have non-legitimate address which is decomposed not in
370 the way we expected, don't do elimination here. In such case
371 the address will be reloaded and elimination will be done in
372 reload insn finally. */
373 if (REG_P (m_base_reg))
374 lra_eliminate_reg_if_possible (m_base_loc);
375 if (m_ad->base_term2 != NULL)
376 *m_ad->base_term2 = *m_ad->base_term;
378 if (m_index_loc != NULL)
380 m_index_reg = *m_index_loc;
381 if (REG_P (m_index_reg))
382 lra_eliminate_reg_if_possible (m_index_loc);
386 address_eliminator::~address_eliminator ()
388 if (m_base_loc && *m_base_loc != m_base_reg)
390 *m_base_loc = m_base_reg;
391 if (m_ad->base_term2 != NULL)
392 *m_ad->base_term2 = *m_ad->base_term;
394 if (m_index_loc && *m_index_loc != m_index_reg)
395 *m_index_loc = m_index_reg;
398 /* Return true if the eliminated form of AD is a legitimate target address.
399 If OP is a MEM, AD is the address within OP, otherwise OP should be
400 ignored. CONSTRAINT is one constraint that the operand may need
401 to meet. */
402 static bool
403 valid_address_p (rtx op, struct address_info *ad,
404 enum constraint_num constraint)
406 address_eliminator eliminator (ad);
408 /* Allow a memory OP if it matches CONSTRAINT, even if CONSTRAINT is more
409 forgiving than "m". */
410 if (MEM_P (op)
411 && (insn_extra_memory_constraint (constraint)
412 || insn_extra_special_memory_constraint (constraint))
413 && constraint_satisfied_p (op, constraint))
414 return true;
416 return valid_address_p (ad->mode, *ad->outer, ad->as);
419 /* For special_memory_operand, it could be false for MEM_P (op),
420 i.e. bcst_mem_operand in i386 backend.
421 Extract and return real memory operand or op. */
423 extract_mem_from_operand (rtx op)
425 for (rtx x = op;; x = XEXP (x, 0))
427 if (MEM_P (x))
428 return x;
429 if (GET_RTX_LENGTH (GET_CODE (x)) != 1
430 || GET_RTX_FORMAT (GET_CODE (x))[0] != 'e')
431 break;
433 return op;
436 /* Return true if the eliminated form of memory reference OP satisfies
437 extra (special) memory constraint CONSTRAINT. */
438 static bool
439 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
441 struct address_info ad;
442 rtx mem = extract_mem_from_operand (op);
443 if (!MEM_P (mem))
444 return false;
446 decompose_mem_address (&ad, mem);
447 address_eliminator eliminator (&ad);
448 return constraint_satisfied_p (op, constraint);
451 /* Return true if the eliminated form of address AD satisfies extra
452 address constraint CONSTRAINT. */
453 static bool
454 satisfies_address_constraint_p (struct address_info *ad,
455 enum constraint_num constraint)
457 address_eliminator eliminator (ad);
458 return constraint_satisfied_p (*ad->outer, constraint);
461 /* Return true if the eliminated form of address OP satisfies extra
462 address constraint CONSTRAINT. */
463 static bool
464 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
466 struct address_info ad;
468 decompose_lea_address (&ad, &op);
469 return satisfies_address_constraint_p (&ad, constraint);
472 /* Initiate equivalences for LRA. As we keep original equivalences
473 before any elimination, we need to make copies otherwise any change
474 in insns might change the equivalences. */
475 void
476 lra_init_equiv (void)
478 ira_expand_reg_equiv ();
479 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
481 rtx res;
483 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
484 ira_reg_equiv[i].memory = copy_rtx (res);
485 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
486 ira_reg_equiv[i].invariant = copy_rtx (res);
490 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
492 /* Update equivalence for REGNO. We need to this as the equivalence
493 might contain other pseudos which are changed by their
494 equivalences. */
495 static void
496 update_equiv (int regno)
498 rtx x;
500 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
501 ira_reg_equiv[regno].memory
502 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
503 NULL_RTX);
504 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
505 ira_reg_equiv[regno].invariant
506 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
507 NULL_RTX);
510 /* If we have decided to substitute X with another value, return that
511 value, otherwise return X. */
512 static rtx
513 get_equiv (rtx x)
515 int regno;
516 rtx res;
518 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
519 || ! ira_reg_equiv[regno].defined_p
520 || ! ira_reg_equiv[regno].profitable_p
521 || lra_get_regno_hard_regno (regno) >= 0)
522 return x;
523 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
525 if (targetm.cannot_substitute_mem_equiv_p (res))
526 return x;
527 return res;
529 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
530 return res;
531 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
532 return res;
533 gcc_unreachable ();
536 /* If we have decided to substitute X with the equivalent value,
537 return that value after elimination for INSN, otherwise return
538 X. */
539 static rtx
540 get_equiv_with_elimination (rtx x, rtx_insn *insn)
542 rtx res = get_equiv (x);
544 if (x == res || CONSTANT_P (res))
545 return res;
546 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
547 false, false, 0, true);
550 /* Set up curr_operand_mode. */
551 static void
552 init_curr_operand_mode (void)
554 int nop = curr_static_id->n_operands;
555 for (int i = 0; i < nop; i++)
557 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
558 if (mode == VOIDmode)
560 /* The .md mode for address operands is the mode of the
561 addressed value rather than the mode of the address itself. */
562 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
563 mode = Pmode;
564 else
565 mode = curr_static_id->operand[i].mode;
567 curr_operand_mode[i] = mode;
573 /* The page contains code to reuse input reloads. */
575 /* Structure describes input reload of the current insns. */
576 struct input_reload
578 /* True for input reload of matched operands. */
579 bool match_p;
580 /* Reloaded value. */
581 rtx input;
582 /* Reload pseudo used. */
583 rtx reg;
586 /* The number of elements in the following array. */
587 static int curr_insn_input_reloads_num;
588 /* Array containing info about input reloads. It is used to find the
589 same input reload and reuse the reload pseudo in this case. */
590 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
592 /* Initiate data concerning reuse of input reloads for the current
593 insn. */
594 static void
595 init_curr_insn_input_reloads (void)
597 curr_insn_input_reloads_num = 0;
600 /* The canonical form of an rtx inside a MEM is not necessarily the same as the
601 canonical form of the rtx outside the MEM. Fix this up in the case that
602 we're reloading an address (and therefore pulling it outside a MEM). */
603 static rtx
604 canonicalize_reload_addr (rtx addr)
606 subrtx_var_iterator::array_type array;
607 FOR_EACH_SUBRTX_VAR (iter, array, addr, NONCONST)
609 rtx x = *iter;
610 if (GET_CODE (x) == MULT && CONST_INT_P (XEXP (x, 1)))
612 const HOST_WIDE_INT ci = INTVAL (XEXP (x, 1));
613 const int pwr2 = exact_log2 (ci);
614 if (pwr2 > 0)
616 /* Rewrite this to use a shift instead, which is canonical when
617 outside of a MEM. */
618 PUT_CODE (x, ASHIFT);
619 XEXP (x, 1) = GEN_INT (pwr2);
624 return addr;
627 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse an existing
628 reload pseudo. Don't reuse an existing reload pseudo if IN_SUBREG_P
629 is true and the reused pseudo should be wrapped up in a SUBREG.
630 The result pseudo is returned through RESULT_REG. Return TRUE if we
631 created a new pseudo, FALSE if we reused an existing reload pseudo.
632 Use TITLE to describe new registers for debug purposes. */
633 static bool
634 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
635 enum reg_class rclass, bool in_subreg_p,
636 const char *title, rtx *result_reg)
638 int i, regno;
639 enum reg_class new_class;
640 bool unique_p = false;
642 if (type == OP_OUT)
644 /* Output reload registers tend to start out with a conservative
645 choice of register class. Usually this is ALL_REGS, although
646 a target might narrow it (for performance reasons) through
647 targetm.preferred_reload_class. It's therefore quite common
648 for a reload instruction to require a more restrictive class
649 than the class that was originally assigned to the reload register.
651 In these situations, it's more efficient to refine the choice
652 of register class rather than create a second reload register.
653 This also helps to avoid cycling for registers that are only
654 used by reload instructions. */
655 if (REG_P (original)
656 && (int) REGNO (original) >= new_regno_start
657 && INSN_UID (curr_insn) >= new_insn_uid_start
658 && in_class_p (original, rclass, &new_class, true))
660 unsigned int regno = REGNO (original);
661 if (lra_dump_file != NULL)
663 fprintf (lra_dump_file, " Reuse r%d for output ", regno);
664 dump_value_slim (lra_dump_file, original, 1);
666 if (new_class != lra_get_allocno_class (regno))
667 lra_change_class (regno, new_class, ", change to", false);
668 if (lra_dump_file != NULL)
669 fprintf (lra_dump_file, "\n");
670 *result_reg = original;
671 return false;
673 *result_reg
674 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
675 return true;
677 /* Prevent reuse value of expression with side effects,
678 e.g. volatile memory. */
679 if (! side_effects_p (original))
680 for (i = 0; i < curr_insn_input_reloads_num; i++)
682 if (! curr_insn_input_reloads[i].match_p
683 && rtx_equal_p (curr_insn_input_reloads[i].input, original)
684 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
686 rtx reg = curr_insn_input_reloads[i].reg;
687 regno = REGNO (reg);
688 /* If input is equal to original and both are VOIDmode,
689 GET_MODE (reg) might be still different from mode.
690 Ensure we don't return *result_reg with wrong mode. */
691 if (GET_MODE (reg) != mode)
693 if (in_subreg_p)
694 continue;
695 if (maybe_lt (GET_MODE_SIZE (GET_MODE (reg)),
696 GET_MODE_SIZE (mode)))
697 continue;
698 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
699 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
700 continue;
702 *result_reg = reg;
703 if (lra_dump_file != NULL)
705 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
706 dump_value_slim (lra_dump_file, original, 1);
708 if (new_class != lra_get_allocno_class (regno))
709 lra_change_class (regno, new_class, ", change to", false);
710 if (lra_dump_file != NULL)
711 fprintf (lra_dump_file, "\n");
712 return false;
714 /* If we have an input reload with a different mode, make sure it
715 will get a different hard reg. */
716 else if (REG_P (original)
717 && REG_P (curr_insn_input_reloads[i].input)
718 && REGNO (original) == REGNO (curr_insn_input_reloads[i].input)
719 && (GET_MODE (original)
720 != GET_MODE (curr_insn_input_reloads[i].input)))
721 unique_p = true;
723 *result_reg = (unique_p
724 ? lra_create_new_reg_with_unique_value
725 : lra_create_new_reg) (mode, original, rclass, title);
726 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
727 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
728 curr_insn_input_reloads[curr_insn_input_reloads_num].match_p = false;
729 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
730 return true;
734 /* The page contains major code to choose the current insn alternative
735 and generate reloads for it. */
737 /* Return the offset from REGNO of the least significant register
738 in (reg:MODE REGNO).
740 This function is used to tell whether two registers satisfy
741 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
743 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
744 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
746 lra_constraint_offset (int regno, machine_mode mode)
748 lra_assert (regno < FIRST_PSEUDO_REGISTER);
750 scalar_int_mode int_mode;
751 if (WORDS_BIG_ENDIAN
752 && is_a <scalar_int_mode> (mode, &int_mode)
753 && GET_MODE_SIZE (int_mode) > UNITS_PER_WORD)
754 return hard_regno_nregs (regno, mode) - 1;
755 return 0;
758 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
759 if they are the same hard reg, and has special hacks for
760 auto-increment and auto-decrement. This is specifically intended for
761 process_alt_operands to use in determining whether two operands
762 match. X is the operand whose number is the lower of the two.
764 It is supposed that X is the output operand and Y is the input
765 operand. Y_HARD_REGNO is the final hard regno of register Y or
766 register in subreg Y as we know it now. Otherwise, it is a
767 negative value. */
768 static bool
769 operands_match_p (rtx x, rtx y, int y_hard_regno)
771 int i;
772 RTX_CODE code = GET_CODE (x);
773 const char *fmt;
775 if (x == y)
776 return true;
777 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
778 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
780 int j;
782 i = get_hard_regno (x, false);
783 if (i < 0)
784 goto slow;
786 if ((j = y_hard_regno) < 0)
787 goto slow;
789 i += lra_constraint_offset (i, GET_MODE (x));
790 j += lra_constraint_offset (j, GET_MODE (y));
792 return i == j;
795 /* If two operands must match, because they are really a single
796 operand of an assembler insn, then two post-increments are invalid
797 because the assembler insn would increment only once. On the
798 other hand, a post-increment matches ordinary indexing if the
799 post-increment is the output operand. */
800 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
801 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
803 /* Two pre-increments are invalid because the assembler insn would
804 increment only once. On the other hand, a pre-increment matches
805 ordinary indexing if the pre-increment is the input operand. */
806 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
807 || GET_CODE (y) == PRE_MODIFY)
808 return operands_match_p (x, XEXP (y, 0), -1);
810 slow:
812 if (code == REG && REG_P (y))
813 return REGNO (x) == REGNO (y);
815 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
816 && x == SUBREG_REG (y))
817 return true;
818 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
819 && SUBREG_REG (x) == y)
820 return true;
822 /* Now we have disposed of all the cases in which different rtx
823 codes can match. */
824 if (code != GET_CODE (y))
825 return false;
827 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
828 if (GET_MODE (x) != GET_MODE (y))
829 return false;
831 switch (code)
833 CASE_CONST_UNIQUE:
834 return false;
836 case LABEL_REF:
837 return label_ref_label (x) == label_ref_label (y);
838 case SYMBOL_REF:
839 return XSTR (x, 0) == XSTR (y, 0);
841 default:
842 break;
845 /* Compare the elements. If any pair of corresponding elements fail
846 to match, return false for the whole things. */
848 fmt = GET_RTX_FORMAT (code);
849 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
851 int val, j;
852 switch (fmt[i])
854 case 'w':
855 if (XWINT (x, i) != XWINT (y, i))
856 return false;
857 break;
859 case 'i':
860 if (XINT (x, i) != XINT (y, i))
861 return false;
862 break;
864 case 'p':
865 if (maybe_ne (SUBREG_BYTE (x), SUBREG_BYTE (y)))
866 return false;
867 break;
869 case 'e':
870 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
871 if (val == 0)
872 return false;
873 break;
875 case '0':
876 break;
878 case 'E':
879 if (XVECLEN (x, i) != XVECLEN (y, i))
880 return false;
881 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
883 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
884 if (val == 0)
885 return false;
887 break;
889 /* It is believed that rtx's at this level will never
890 contain anything but integers and other rtx's, except for
891 within LABEL_REFs and SYMBOL_REFs. */
892 default:
893 gcc_unreachable ();
896 return true;
899 /* True if X is a constant that can be forced into the constant pool.
900 MODE is the mode of the operand, or VOIDmode if not known. */
901 #define CONST_POOL_OK_P(MODE, X) \
902 ((MODE) != VOIDmode \
903 && CONSTANT_P (X) \
904 && GET_CODE (X) != HIGH \
905 && GET_MODE_SIZE (MODE).is_constant () \
906 && !targetm.cannot_force_const_mem (MODE, X))
908 /* True if C is a non-empty register class that has too few registers
909 to be safely used as a reload target class. */
910 #define SMALL_REGISTER_CLASS_P(C) \
911 (ira_class_hard_regs_num [(C)] == 1 \
912 || (ira_class_hard_regs_num [(C)] >= 1 \
913 && targetm.class_likely_spilled_p (C)))
915 /* If REG is a reload pseudo, try to make its class satisfying CL. */
916 static void
917 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
919 enum reg_class rclass;
921 /* Do not make more accurate class from reloads generated. They are
922 mostly moves with a lot of constraints. Making more accurate
923 class may results in very narrow class and impossibility of find
924 registers for several reloads of one insn. */
925 if (INSN_UID (curr_insn) >= new_insn_uid_start)
926 return;
927 if (GET_CODE (reg) == SUBREG)
928 reg = SUBREG_REG (reg);
929 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
930 return;
931 if (in_class_p (reg, cl, &rclass) && rclass != cl)
932 lra_change_class (REGNO (reg), rclass, " Change to", true);
935 /* Searches X for any reference to a reg with the same value as REGNO,
936 returning the rtx of the reference found if any. Otherwise,
937 returns NULL_RTX. */
938 static rtx
939 regno_val_use_in (unsigned int regno, rtx x)
941 const char *fmt;
942 int i, j;
943 rtx tem;
945 if (REG_P (x) && lra_reg_info[REGNO (x)].val == lra_reg_info[regno].val)
946 return x;
948 fmt = GET_RTX_FORMAT (GET_CODE (x));
949 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
951 if (fmt[i] == 'e')
953 if ((tem = regno_val_use_in (regno, XEXP (x, i))))
954 return tem;
956 else if (fmt[i] == 'E')
957 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
958 if ((tem = regno_val_use_in (regno , XVECEXP (x, i, j))))
959 return tem;
962 return NULL_RTX;
965 /* Return true if all current insn non-output operands except INS (it
966 has a negaitve end marker) do not use pseudos with the same value
967 as REGNO. */
968 static bool
969 check_conflict_input_operands (int regno, signed char *ins)
971 int in;
972 int n_operands = curr_static_id->n_operands;
974 for (int nop = 0; nop < n_operands; nop++)
975 if (! curr_static_id->operand[nop].is_operator
976 && curr_static_id->operand[nop].type != OP_OUT)
978 for (int i = 0; (in = ins[i]) >= 0; i++)
979 if (in == nop)
980 break;
981 if (in < 0
982 && regno_val_use_in (regno, *curr_id->operand_loc[nop]) != NULL_RTX)
983 return false;
985 return true;
988 /* Generate reloads for matching OUT and INS (array of input operand
989 numbers with end marker -1) with reg class GOAL_CLASS, considering
990 output operands OUTS (similar array to INS) needing to be in different
991 registers. Add input and output reloads correspondingly to the lists
992 *BEFORE and *AFTER. OUT might be negative. In this case we generate
993 input reloads for matched input operands INS. EARLY_CLOBBER_P is a flag
994 that the output operand is early clobbered for chosen alternative. */
995 static void
996 match_reload (signed char out, signed char *ins, signed char *outs,
997 enum reg_class goal_class, rtx_insn **before,
998 rtx_insn **after, bool early_clobber_p)
1000 bool out_conflict;
1001 int i, in;
1002 rtx new_in_reg, new_out_reg, reg;
1003 machine_mode inmode, outmode;
1004 rtx in_rtx = *curr_id->operand_loc[ins[0]];
1005 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
1007 inmode = curr_operand_mode[ins[0]];
1008 outmode = out < 0 ? inmode : curr_operand_mode[out];
1009 push_to_sequence (*before);
1010 if (inmode != outmode)
1012 /* process_alt_operands has already checked that the mode sizes
1013 are ordered. */
1014 if (partial_subreg_p (outmode, inmode))
1016 reg = new_in_reg
1017 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
1018 goal_class, "");
1019 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
1020 LRA_SUBREG_P (new_out_reg) = 1;
1021 /* If the input reg is dying here, we can use the same hard
1022 register for REG and IN_RTX. We do it only for original
1023 pseudos as reload pseudos can die although original
1024 pseudos still live where reload pseudos dies. */
1025 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
1026 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
1027 && (!early_clobber_p
1028 || check_conflict_input_operands(REGNO (in_rtx), ins)))
1029 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
1031 else
1033 reg = new_out_reg
1034 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
1035 goal_class, "");
1036 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
1037 /* NEW_IN_REG is non-paradoxical subreg. We don't want
1038 NEW_OUT_REG living above. We add clobber clause for
1039 this. This is just a temporary clobber. We can remove
1040 it at the end of LRA work. */
1041 rtx_insn *clobber = emit_clobber (new_out_reg);
1042 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
1043 LRA_SUBREG_P (new_in_reg) = 1;
1044 if (GET_CODE (in_rtx) == SUBREG)
1046 rtx subreg_reg = SUBREG_REG (in_rtx);
1048 /* If SUBREG_REG is dying here and sub-registers IN_RTX
1049 and NEW_IN_REG are similar, we can use the same hard
1050 register for REG and SUBREG_REG. */
1051 if (REG_P (subreg_reg)
1052 && (int) REGNO (subreg_reg) < lra_new_regno_start
1053 && GET_MODE (subreg_reg) == outmode
1054 && known_eq (SUBREG_BYTE (in_rtx), SUBREG_BYTE (new_in_reg))
1055 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg))
1056 && (! early_clobber_p
1057 || check_conflict_input_operands (REGNO (subreg_reg),
1058 ins)))
1059 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
1063 else
1065 /* Pseudos have values -- see comments for lra_reg_info.
1066 Different pseudos with the same value do not conflict even if
1067 they live in the same place. When we create a pseudo we
1068 assign value of original pseudo (if any) from which we
1069 created the new pseudo. If we create the pseudo from the
1070 input pseudo, the new pseudo will have no conflict with the
1071 input pseudo which is wrong when the input pseudo lives after
1072 the insn and as the new pseudo value is changed by the insn
1073 output. Therefore we create the new pseudo from the output
1074 except the case when we have single matched dying input
1075 pseudo.
1077 We cannot reuse the current output register because we might
1078 have a situation like "a <- a op b", where the constraints
1079 force the second input operand ("b") to match the output
1080 operand ("a"). "b" must then be copied into a new register
1081 so that it doesn't clobber the current value of "a".
1083 We cannot use the same value if the output pseudo is
1084 early clobbered or the input pseudo is mentioned in the
1085 output, e.g. as an address part in memory, because
1086 output reload will actually extend the pseudo liveness.
1087 We don't care about eliminable hard regs here as we are
1088 interesting only in pseudos. */
1090 /* Matching input's register value is the same as one of the other
1091 output operand. Output operands in a parallel insn must be in
1092 different registers. */
1093 out_conflict = false;
1094 if (REG_P (in_rtx))
1096 for (i = 0; outs[i] >= 0; i++)
1098 rtx other_out_rtx = *curr_id->operand_loc[outs[i]];
1099 if (REG_P (other_out_rtx)
1100 && (regno_val_use_in (REGNO (in_rtx), other_out_rtx)
1101 != NULL_RTX))
1103 out_conflict = true;
1104 break;
1109 new_in_reg = new_out_reg
1110 = (! early_clobber_p && ins[1] < 0 && REG_P (in_rtx)
1111 && (int) REGNO (in_rtx) < lra_new_regno_start
1112 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
1113 && (! early_clobber_p
1114 || check_conflict_input_operands (REGNO (in_rtx), ins))
1115 && (out < 0
1116 || regno_val_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
1117 && !out_conflict
1118 ? lra_create_new_reg (inmode, in_rtx, goal_class, "")
1119 : lra_create_new_reg_with_unique_value (outmode, out_rtx,
1120 goal_class, ""));
1122 /* In operand can be got from transformations before processing insn
1123 constraints. One example of such transformations is subreg
1124 reloading (see function simplify_operand_subreg). The new
1125 pseudos created by the transformations might have inaccurate
1126 class (ALL_REGS) and we should make their classes more
1127 accurate. */
1128 narrow_reload_pseudo_class (in_rtx, goal_class);
1129 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
1130 *before = get_insns ();
1131 end_sequence ();
1132 /* Add the new pseudo to consider values of subsequent input reload
1133 pseudos. */
1134 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
1135 curr_insn_input_reloads[curr_insn_input_reloads_num].input = in_rtx;
1136 curr_insn_input_reloads[curr_insn_input_reloads_num].match_p = true;
1137 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = new_in_reg;
1138 for (i = 0; (in = ins[i]) >= 0; i++)
1139 if (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
1140 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]))
1141 *curr_id->operand_loc[in] = new_in_reg;
1142 else
1144 lra_assert
1145 (GET_MODE (new_out_reg) == GET_MODE (*curr_id->operand_loc[in]));
1146 *curr_id->operand_loc[in] = new_out_reg;
1148 lra_update_dups (curr_id, ins);
1149 if (out < 0)
1150 return;
1151 /* See a comment for the input operand above. */
1152 narrow_reload_pseudo_class (out_rtx, goal_class);
1153 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
1155 reg = SUBREG_P (out_rtx) ? SUBREG_REG (out_rtx) : out_rtx;
1156 start_sequence ();
1157 /* If we had strict_low_part, use it also in reload to keep other
1158 parts unchanged but do it only for regs as strict_low_part
1159 has no sense for memory and probably there is no insn pattern
1160 to match the reload insn in memory case. */
1161 if (out >= 0 && curr_static_id->operand[out].strict_low && REG_P (reg))
1162 out_rtx = gen_rtx_STRICT_LOW_PART (VOIDmode, out_rtx);
1163 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
1164 emit_insn (*after);
1165 *after = get_insns ();
1166 end_sequence ();
1168 *curr_id->operand_loc[out] = new_out_reg;
1169 lra_update_dup (curr_id, out);
1172 /* Return register class which is union of all reg classes in insn
1173 constraint alternative string starting with P. */
1174 static enum reg_class
1175 reg_class_from_constraints (const char *p)
1177 int c, len;
1178 enum reg_class op_class = NO_REGS;
1181 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1183 case '#':
1184 case ',':
1185 return op_class;
1187 case 'g':
1188 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1189 break;
1191 default:
1192 enum constraint_num cn = lookup_constraint (p);
1193 enum reg_class cl = reg_class_for_constraint (cn);
1194 if (cl == NO_REGS)
1196 if (insn_extra_address_constraint (cn))
1197 op_class
1198 = (reg_class_subunion
1199 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1200 ADDRESS, SCRATCH)]);
1201 break;
1204 op_class = reg_class_subunion[op_class][cl];
1205 break;
1207 while ((p += len), c);
1208 return op_class;
1211 /* If OP is a register, return the class of the register as per
1212 get_reg_class, otherwise return NO_REGS. */
1213 static inline enum reg_class
1214 get_op_class (rtx op)
1216 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1219 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1220 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1221 SUBREG for VAL to make them equal. */
1222 static rtx_insn *
1223 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1225 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1227 /* Usually size of mem_pseudo is greater than val size but in
1228 rare cases it can be less as it can be defined by target
1229 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1230 if (! MEM_P (val))
1232 val = gen_lowpart_SUBREG (GET_MODE (mem_pseudo),
1233 GET_CODE (val) == SUBREG
1234 ? SUBREG_REG (val) : val);
1235 LRA_SUBREG_P (val) = 1;
1237 else
1239 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1240 LRA_SUBREG_P (mem_pseudo) = 1;
1243 return to_p ? gen_move_insn (mem_pseudo, val)
1244 : gen_move_insn (val, mem_pseudo);
1247 /* Process a special case insn (register move), return true if we
1248 don't need to process it anymore. INSN should be a single set
1249 insn. Set up that RTL was changed through CHANGE_P and that hook
1250 TARGET_SECONDARY_MEMORY_NEEDED says to use secondary memory through
1251 SEC_MEM_P. */
1252 static bool
1253 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1255 int sregno, dregno;
1256 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1257 rtx_insn *before;
1258 enum reg_class dclass, sclass, secondary_class;
1259 secondary_reload_info sri;
1261 lra_assert (curr_insn_set != NULL_RTX);
1262 dreg = dest = SET_DEST (curr_insn_set);
1263 sreg = src = SET_SRC (curr_insn_set);
1264 if (GET_CODE (dest) == SUBREG)
1265 dreg = SUBREG_REG (dest);
1266 if (GET_CODE (src) == SUBREG)
1267 sreg = SUBREG_REG (src);
1268 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1269 return false;
1270 sclass = dclass = NO_REGS;
1271 if (REG_P (dreg))
1272 dclass = get_reg_class (REGNO (dreg));
1273 gcc_assert (dclass < LIM_REG_CLASSES);
1274 if (dclass == ALL_REGS)
1275 /* ALL_REGS is used for new pseudos created by transformations
1276 like reload of SUBREG_REG (see function
1277 simplify_operand_subreg). We don't know their class yet. We
1278 should figure out the class from processing the insn
1279 constraints not in this fast path function. Even if ALL_REGS
1280 were a right class for the pseudo, secondary_... hooks usually
1281 are not define for ALL_REGS. */
1282 return false;
1283 if (REG_P (sreg))
1284 sclass = get_reg_class (REGNO (sreg));
1285 gcc_assert (sclass < LIM_REG_CLASSES);
1286 if (sclass == ALL_REGS)
1287 /* See comments above. */
1288 return false;
1289 if (sclass == NO_REGS && dclass == NO_REGS)
1290 return false;
1291 if (targetm.secondary_memory_needed (GET_MODE (src), sclass, dclass)
1292 && ((sclass != NO_REGS && dclass != NO_REGS)
1293 || (GET_MODE (src)
1294 != targetm.secondary_memory_needed_mode (GET_MODE (src)))))
1296 *sec_mem_p = true;
1297 return false;
1299 if (! REG_P (dreg) || ! REG_P (sreg))
1300 return false;
1301 sri.prev_sri = NULL;
1302 sri.icode = CODE_FOR_nothing;
1303 sri.extra_cost = 0;
1304 secondary_class = NO_REGS;
1305 /* Set up hard register for a reload pseudo for hook
1306 secondary_reload because some targets just ignore unassigned
1307 pseudos in the hook. */
1308 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1310 dregno = REGNO (dreg);
1311 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1313 else
1314 dregno = -1;
1315 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1317 sregno = REGNO (sreg);
1318 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1320 else
1321 sregno = -1;
1322 if (sclass != NO_REGS)
1323 secondary_class
1324 = (enum reg_class) targetm.secondary_reload (false, dest,
1325 (reg_class_t) sclass,
1326 GET_MODE (src), &sri);
1327 if (sclass == NO_REGS
1328 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1329 && dclass != NO_REGS))
1331 enum reg_class old_sclass = secondary_class;
1332 secondary_reload_info old_sri = sri;
1334 sri.prev_sri = NULL;
1335 sri.icode = CODE_FOR_nothing;
1336 sri.extra_cost = 0;
1337 secondary_class
1338 = (enum reg_class) targetm.secondary_reload (true, src,
1339 (reg_class_t) dclass,
1340 GET_MODE (src), &sri);
1341 /* Check the target hook consistency. */
1342 lra_assert
1343 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1344 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1345 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1347 if (sregno >= 0)
1348 reg_renumber [sregno] = -1;
1349 if (dregno >= 0)
1350 reg_renumber [dregno] = -1;
1351 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1352 return false;
1353 *change_p = true;
1354 new_reg = NULL_RTX;
1355 if (secondary_class != NO_REGS)
1356 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1357 secondary_class,
1358 "secondary");
1359 start_sequence ();
1360 if (sri.icode == CODE_FOR_nothing)
1361 lra_emit_move (new_reg, src);
1362 else
1364 enum reg_class scratch_class;
1366 scratch_class = (reg_class_from_constraints
1367 (insn_data[sri.icode].operand[2].constraint));
1368 scratch_reg = (lra_create_new_reg_with_unique_value
1369 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1370 scratch_class, "scratch"));
1371 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1372 src, scratch_reg));
1374 before = get_insns ();
1375 end_sequence ();
1376 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1377 if (new_reg != NULL_RTX)
1378 SET_SRC (curr_insn_set) = new_reg;
1379 else
1381 if (lra_dump_file != NULL)
1383 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1384 dump_insn_slim (lra_dump_file, curr_insn);
1386 lra_set_insn_deleted (curr_insn);
1387 return true;
1389 return false;
1392 /* The following data describe the result of process_alt_operands.
1393 The data are used in curr_insn_transform to generate reloads. */
1395 /* The chosen reg classes which should be used for the corresponding
1396 operands. */
1397 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1398 /* True if the operand should be the same as another operand and that
1399 other operand does not need a reload. */
1400 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1401 /* True if the operand does not need a reload. */
1402 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1403 /* True if the operand can be offsetable memory. */
1404 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1405 /* The number of an operand to which given operand can be matched to. */
1406 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1407 /* The number of elements in the following array. */
1408 static int goal_alt_dont_inherit_ops_num;
1409 /* Numbers of operands whose reload pseudos should not be inherited. */
1410 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1411 /* True if the insn commutative operands should be swapped. */
1412 static bool goal_alt_swapped;
1413 /* The chosen insn alternative. */
1414 static int goal_alt_number;
1416 /* True if the corresponding operand is the result of an equivalence
1417 substitution. */
1418 static bool equiv_substition_p[MAX_RECOG_OPERANDS];
1420 /* The following five variables are used to choose the best insn
1421 alternative. They reflect final characteristics of the best
1422 alternative. */
1424 /* Number of necessary reloads and overall cost reflecting the
1425 previous value and other unpleasantness of the best alternative. */
1426 static int best_losers, best_overall;
1427 /* Overall number hard registers used for reloads. For example, on
1428 some targets we need 2 general registers to reload DFmode and only
1429 one floating point register. */
1430 static int best_reload_nregs;
1431 /* Overall number reflecting distances of previous reloading the same
1432 value. The distances are counted from the current BB start. It is
1433 used to improve inheritance chances. */
1434 static int best_reload_sum;
1436 /* True if the current insn should have no correspondingly input or
1437 output reloads. */
1438 static bool no_input_reloads_p, no_output_reloads_p;
1440 /* True if we swapped the commutative operands in the current
1441 insn. */
1442 static int curr_swapped;
1444 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1445 register of class CL. Add any input reloads to list BEFORE. AFTER
1446 is nonnull if *LOC is an automodified value; handle that case by
1447 adding the required output reloads to list AFTER. Return true if
1448 the RTL was changed.
1450 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1451 register. Return false if the address register is correct. */
1452 static bool
1453 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1454 enum reg_class cl)
1456 int regno;
1457 enum reg_class rclass, new_class;
1458 rtx reg;
1459 rtx new_reg;
1460 machine_mode mode;
1461 bool subreg_p, before_p = false;
1463 subreg_p = GET_CODE (*loc) == SUBREG;
1464 if (subreg_p)
1466 reg = SUBREG_REG (*loc);
1467 mode = GET_MODE (reg);
1469 /* For mode with size bigger than ptr_mode, there unlikely to be "mov"
1470 between two registers with different classes, but there normally will
1471 be "mov" which transfers element of vector register into the general
1472 register, and this normally will be a subreg which should be reloaded
1473 as a whole. This is particularly likely to be triggered when
1474 -fno-split-wide-types specified. */
1475 if (!REG_P (reg)
1476 || in_class_p (reg, cl, &new_class)
1477 || known_le (GET_MODE_SIZE (mode), GET_MODE_SIZE (ptr_mode)))
1478 loc = &SUBREG_REG (*loc);
1481 reg = *loc;
1482 mode = GET_MODE (reg);
1483 if (! REG_P (reg))
1485 if (check_only_p)
1486 return true;
1487 /* Always reload memory in an address even if the target supports
1488 such addresses. */
1489 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1490 before_p = true;
1492 else
1494 regno = REGNO (reg);
1495 rclass = get_reg_class (regno);
1496 if (! check_only_p
1497 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1499 if (lra_dump_file != NULL)
1501 fprintf (lra_dump_file,
1502 "Changing pseudo %d in address of insn %u on equiv ",
1503 REGNO (reg), INSN_UID (curr_insn));
1504 dump_value_slim (lra_dump_file, *loc, 1);
1505 fprintf (lra_dump_file, "\n");
1507 *loc = copy_rtx (*loc);
1509 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1511 if (check_only_p)
1512 return true;
1513 reg = *loc;
1514 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1515 mode, reg, cl, subreg_p, "address", &new_reg))
1516 before_p = true;
1518 else if (new_class != NO_REGS && rclass != new_class)
1520 if (check_only_p)
1521 return true;
1522 lra_change_class (regno, new_class, " Change to", true);
1523 return false;
1525 else
1526 return false;
1528 if (before_p)
1530 push_to_sequence (*before);
1531 lra_emit_move (new_reg, reg);
1532 *before = get_insns ();
1533 end_sequence ();
1535 *loc = new_reg;
1536 if (after != NULL)
1538 start_sequence ();
1539 lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1540 emit_insn (*after);
1541 *after = get_insns ();
1542 end_sequence ();
1544 return true;
1547 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1548 the insn to be inserted before curr insn. AFTER returns the
1549 the insn to be inserted after curr insn. ORIGREG and NEWREG
1550 are the original reg and new reg for reload. */
1551 static void
1552 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1553 rtx newreg)
1555 if (before)
1557 push_to_sequence (*before);
1558 lra_emit_move (newreg, origreg);
1559 *before = get_insns ();
1560 end_sequence ();
1562 if (after)
1564 start_sequence ();
1565 lra_emit_move (origreg, newreg);
1566 emit_insn (*after);
1567 *after = get_insns ();
1568 end_sequence ();
1572 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1573 static bool process_address (int, bool, rtx_insn **, rtx_insn **);
1575 /* Make reloads for subreg in operand NOP with internal subreg mode
1576 REG_MODE, add new reloads for further processing. Return true if
1577 any change was done. */
1578 static bool
1579 simplify_operand_subreg (int nop, machine_mode reg_mode)
1581 int hard_regno, inner_hard_regno;
1582 rtx_insn *before, *after;
1583 machine_mode mode, innermode;
1584 rtx reg, new_reg;
1585 rtx operand = *curr_id->operand_loc[nop];
1586 enum reg_class regclass;
1587 enum op_type type;
1589 before = after = NULL;
1591 if (GET_CODE (operand) != SUBREG)
1592 return false;
1594 mode = GET_MODE (operand);
1595 reg = SUBREG_REG (operand);
1596 innermode = GET_MODE (reg);
1597 type = curr_static_id->operand[nop].type;
1598 if (MEM_P (reg))
1600 const bool addr_was_valid
1601 = valid_address_p (innermode, XEXP (reg, 0), MEM_ADDR_SPACE (reg));
1602 alter_subreg (curr_id->operand_loc[nop], false);
1603 rtx subst = *curr_id->operand_loc[nop];
1604 lra_assert (MEM_P (subst));
1605 const bool addr_is_valid = valid_address_p (GET_MODE (subst),
1606 XEXP (subst, 0),
1607 MEM_ADDR_SPACE (subst));
1608 if (!addr_was_valid
1609 || addr_is_valid
1610 || ((get_constraint_type (lookup_constraint
1611 (curr_static_id->operand[nop].constraint))
1612 != CT_SPECIAL_MEMORY)
1613 /* We still can reload address and if the address is
1614 valid, we can remove subreg without reloading its
1615 inner memory. */
1616 && valid_address_p (GET_MODE (subst),
1617 regno_reg_rtx
1618 [ira_class_hard_regs
1619 [base_reg_class (GET_MODE (subst),
1620 MEM_ADDR_SPACE (subst),
1621 ADDRESS, SCRATCH)][0]],
1622 MEM_ADDR_SPACE (subst))))
1624 /* If we change the address for a paradoxical subreg of memory, the
1625 new address might violate the necessary alignment or the access
1626 might be slow; take this into consideration. We need not worry
1627 about accesses beyond allocated memory for paradoxical memory
1628 subregs as we don't substitute such equiv memory (see processing
1629 equivalences in function lra_constraints) and because for spilled
1630 pseudos we allocate stack memory enough for the biggest
1631 corresponding paradoxical subreg.
1633 However, do not blindly simplify a (subreg (mem ...)) for
1634 WORD_REGISTER_OPERATIONS targets as this may lead to loading junk
1635 data into a register when the inner is narrower than outer or
1636 missing important data from memory when the inner is wider than
1637 outer. This rule only applies to modes that are no wider than
1638 a word.
1640 If valid memory becomes invalid after subreg elimination
1641 and address might be different we still have to reload
1642 memory.
1644 if ((! addr_was_valid
1645 || addr_is_valid
1646 || known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (innermode)))
1647 && !(maybe_ne (GET_MODE_PRECISION (mode),
1648 GET_MODE_PRECISION (innermode))
1649 && known_le (GET_MODE_SIZE (mode), UNITS_PER_WORD)
1650 && known_le (GET_MODE_SIZE (innermode), UNITS_PER_WORD)
1651 && WORD_REGISTER_OPERATIONS)
1652 && (!(MEM_ALIGN (subst) < GET_MODE_ALIGNMENT (mode)
1653 && targetm.slow_unaligned_access (mode, MEM_ALIGN (subst)))
1654 || (MEM_ALIGN (reg) < GET_MODE_ALIGNMENT (innermode)
1655 && targetm.slow_unaligned_access (innermode,
1656 MEM_ALIGN (reg)))))
1657 return true;
1659 *curr_id->operand_loc[nop] = operand;
1661 /* But if the address was not valid, we cannot reload the MEM without
1662 reloading the address first. */
1663 if (!addr_was_valid)
1664 process_address (nop, false, &before, &after);
1666 /* INNERMODE is fast, MODE slow. Reload the mem in INNERMODE. */
1667 enum reg_class rclass
1668 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1669 if (get_reload_reg (curr_static_id->operand[nop].type, innermode,
1670 reg, rclass, TRUE, "slow/invalid mem", &new_reg))
1672 bool insert_before, insert_after;
1673 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1675 insert_before = (type != OP_OUT
1676 || partial_subreg_p (mode, innermode));
1677 insert_after = type != OP_IN;
1678 insert_move_for_subreg (insert_before ? &before : NULL,
1679 insert_after ? &after : NULL,
1680 reg, new_reg);
1682 SUBREG_REG (operand) = new_reg;
1684 /* Convert to MODE. */
1685 reg = operand;
1686 rclass
1687 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1688 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1689 rclass, TRUE, "slow/invalid mem", &new_reg))
1691 bool insert_before, insert_after;
1692 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1694 insert_before = type != OP_OUT;
1695 insert_after = type != OP_IN;
1696 insert_move_for_subreg (insert_before ? &before : NULL,
1697 insert_after ? &after : NULL,
1698 reg, new_reg);
1700 *curr_id->operand_loc[nop] = new_reg;
1701 lra_process_new_insns (curr_insn, before, after,
1702 "Inserting slow/invalid mem reload");
1703 return true;
1706 /* If the address was valid and became invalid, prefer to reload
1707 the memory. Typical case is when the index scale should
1708 correspond the memory. */
1709 *curr_id->operand_loc[nop] = operand;
1710 /* Do not return false here as the MEM_P (reg) will be processed
1711 later in this function. */
1713 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1715 alter_subreg (curr_id->operand_loc[nop], false);
1716 return true;
1718 else if (CONSTANT_P (reg))
1720 /* Try to simplify subreg of constant. It is usually result of
1721 equivalence substitution. */
1722 if (innermode == VOIDmode
1723 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1724 innermode = curr_static_id->operand[nop].mode;
1725 if ((new_reg = simplify_subreg (mode, reg, innermode,
1726 SUBREG_BYTE (operand))) != NULL_RTX)
1728 *curr_id->operand_loc[nop] = new_reg;
1729 return true;
1732 /* Put constant into memory when we have mixed modes. It generates
1733 a better code in most cases as it does not need a secondary
1734 reload memory. It also prevents LRA looping when LRA is using
1735 secondary reload memory again and again. */
1736 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1737 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1739 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1740 alter_subreg (curr_id->operand_loc[nop], false);
1741 return true;
1743 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1744 if there may be a problem accessing OPERAND in the outer
1745 mode. */
1746 if ((REG_P (reg)
1747 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1748 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1749 /* Don't reload paradoxical subregs because we could be looping
1750 having repeatedly final regno out of hard regs range. */
1751 && (hard_regno_nregs (hard_regno, innermode)
1752 >= hard_regno_nregs (hard_regno, mode))
1753 && simplify_subreg_regno (hard_regno, innermode,
1754 SUBREG_BYTE (operand), mode) < 0
1755 /* Don't reload subreg for matching reload. It is actually
1756 valid subreg in LRA. */
1757 && ! LRA_SUBREG_P (operand))
1758 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1760 enum reg_class rclass;
1762 if (REG_P (reg))
1763 /* There is a big probability that we will get the same class
1764 for the new pseudo and we will get the same insn which
1765 means infinite looping. So spill the new pseudo. */
1766 rclass = NO_REGS;
1767 else
1768 /* The class will be defined later in curr_insn_transform. */
1769 rclass
1770 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1772 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1773 rclass, TRUE, "subreg reg", &new_reg))
1775 bool insert_before, insert_after;
1776 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1778 insert_before = (type != OP_OUT
1779 || read_modify_subreg_p (operand));
1780 insert_after = (type != OP_IN);
1781 insert_move_for_subreg (insert_before ? &before : NULL,
1782 insert_after ? &after : NULL,
1783 reg, new_reg);
1785 SUBREG_REG (operand) = new_reg;
1786 lra_process_new_insns (curr_insn, before, after,
1787 "Inserting subreg reload");
1788 return true;
1790 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1791 IRA allocates hardreg to the inner pseudo reg according to its mode
1792 instead of the outermode, so the size of the hardreg may not be enough
1793 to contain the outermode operand, in that case we may need to insert
1794 reload for the reg. For the following two types of paradoxical subreg,
1795 we need to insert reload:
1796 1. If the op_type is OP_IN, and the hardreg could not be paired with
1797 other hardreg to contain the outermode operand
1798 (checked by in_hard_reg_set_p), we need to insert the reload.
1799 2. If the op_type is OP_OUT or OP_INOUT.
1801 Here is a paradoxical subreg example showing how the reload is generated:
1803 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1804 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1806 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1807 here, if reg107 is assigned to hardreg R15, because R15 is the last
1808 hardreg, compiler cannot find another hardreg to pair with R15 to
1809 contain TImode data. So we insert a TImode reload reg180 for it.
1810 After reload is inserted:
1812 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1813 (reg:DI 107 [ __comp ])) -1
1814 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1815 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1817 Two reload hard registers will be allocated to reg180 to save TImode data
1818 in LRA_assign.
1820 For LRA pseudos this should normally be handled by the biggest_mode
1821 mechanism. However, it's possible for new uses of an LRA pseudo
1822 to be introduced after we've allocated it, such as when undoing
1823 inheritance, and the allocated register might not then be appropriate
1824 for the new uses. */
1825 else if (REG_P (reg)
1826 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1827 && paradoxical_subreg_p (operand)
1828 && (inner_hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1829 && ((hard_regno
1830 = simplify_subreg_regno (inner_hard_regno, innermode,
1831 SUBREG_BYTE (operand), mode)) < 0
1832 || ((hard_regno_nregs (inner_hard_regno, innermode)
1833 < hard_regno_nregs (hard_regno, mode))
1834 && (regclass = lra_get_allocno_class (REGNO (reg)))
1835 && (type != OP_IN
1836 || !in_hard_reg_set_p (reg_class_contents[regclass],
1837 mode, hard_regno)
1838 || overlaps_hard_reg_set_p (lra_no_alloc_regs,
1839 mode, hard_regno)))))
1841 /* The class will be defined later in curr_insn_transform. */
1842 enum reg_class rclass
1843 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1845 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1846 rclass, TRUE, "paradoxical subreg", &new_reg))
1848 rtx subreg;
1849 bool insert_before, insert_after;
1851 PUT_MODE (new_reg, mode);
1852 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1853 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1855 insert_before = (type != OP_OUT);
1856 insert_after = (type != OP_IN);
1857 insert_move_for_subreg (insert_before ? &before : NULL,
1858 insert_after ? &after : NULL,
1859 reg, subreg);
1861 SUBREG_REG (operand) = new_reg;
1862 lra_process_new_insns (curr_insn, before, after,
1863 "Inserting paradoxical subreg reload");
1864 return true;
1866 return false;
1869 /* Return TRUE if X refers for a hard register from SET. */
1870 static bool
1871 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1873 int i, j, x_hard_regno;
1874 machine_mode mode;
1875 const char *fmt;
1876 enum rtx_code code;
1878 if (x == NULL_RTX)
1879 return false;
1880 code = GET_CODE (x);
1881 mode = GET_MODE (x);
1883 if (code == SUBREG)
1885 /* For all SUBREGs we want to check whether the full multi-register
1886 overlaps the set. For normal SUBREGs this means 'get_hard_regno' of
1887 the inner register, for paradoxical SUBREGs this means the
1888 'get_hard_regno' of the full SUBREG and for complete SUBREGs either is
1889 fine. Use the wider mode for all cases. */
1890 rtx subreg = SUBREG_REG (x);
1891 mode = wider_subreg_mode (x);
1892 if (mode == GET_MODE (subreg))
1894 x = subreg;
1895 code = GET_CODE (x);
1899 if (REG_P (x) || SUBREG_P (x))
1901 x_hard_regno = get_hard_regno (x, true);
1902 return (x_hard_regno >= 0
1903 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1905 if (MEM_P (x))
1907 struct address_info ad;
1909 decompose_mem_address (&ad, x);
1910 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1911 return true;
1912 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1913 return true;
1915 fmt = GET_RTX_FORMAT (code);
1916 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1918 if (fmt[i] == 'e')
1920 if (uses_hard_regs_p (XEXP (x, i), set))
1921 return true;
1923 else if (fmt[i] == 'E')
1925 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1926 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1927 return true;
1930 return false;
1933 /* Return true if OP is a spilled pseudo. */
1934 static inline bool
1935 spilled_pseudo_p (rtx op)
1937 return (REG_P (op)
1938 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1941 /* Return true if X is a general constant. */
1942 static inline bool
1943 general_constant_p (rtx x)
1945 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1948 static bool
1949 reg_in_class_p (rtx reg, enum reg_class cl)
1951 if (cl == NO_REGS)
1952 return get_reg_class (REGNO (reg)) == NO_REGS;
1953 return in_class_p (reg, cl, NULL);
1956 /* Return true if SET of RCLASS contains no hard regs which can be
1957 used in MODE. */
1958 static bool
1959 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1960 HARD_REG_SET &set,
1961 machine_mode mode)
1963 HARD_REG_SET temp;
1965 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1966 temp = set & ~lra_no_alloc_regs;
1967 return (hard_reg_set_subset_p
1968 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1972 /* Used to check validity info about small class input operands. It
1973 should be incremented at start of processing an insn
1974 alternative. */
1975 static unsigned int curr_small_class_check = 0;
1977 /* Update number of used inputs of class OP_CLASS for operand NOP
1978 of alternative NALT. Return true if we have more such class operands
1979 than the number of available regs. */
1980 static bool
1981 update_and_check_small_class_inputs (int nop, int nalt,
1982 enum reg_class op_class)
1984 static unsigned int small_class_check[LIM_REG_CLASSES];
1985 static int small_class_input_nums[LIM_REG_CLASSES];
1987 if (SMALL_REGISTER_CLASS_P (op_class)
1988 /* We are interesting in classes became small because of fixing
1989 some hard regs, e.g. by an user through GCC options. */
1990 && hard_reg_set_intersect_p (reg_class_contents[op_class],
1991 ira_no_alloc_regs)
1992 && (curr_static_id->operand[nop].type != OP_OUT
1993 || TEST_BIT (curr_static_id->operand[nop].early_clobber_alts, nalt)))
1995 if (small_class_check[op_class] == curr_small_class_check)
1996 small_class_input_nums[op_class]++;
1997 else
1999 small_class_check[op_class] = curr_small_class_check;
2000 small_class_input_nums[op_class] = 1;
2002 if (small_class_input_nums[op_class] > ira_class_hard_regs_num[op_class])
2003 return true;
2005 return false;
2008 /* Major function to choose the current insn alternative and what
2009 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
2010 negative we should consider only this alternative. Return false if
2011 we cannot choose the alternative or find how to reload the
2012 operands. */
2013 static bool
2014 process_alt_operands (int only_alternative)
2016 bool ok_p = false;
2017 int nop, overall, nalt;
2018 int n_alternatives = curr_static_id->n_alternatives;
2019 int n_operands = curr_static_id->n_operands;
2020 /* LOSERS counts the operands that don't fit this alternative and
2021 would require loading. */
2022 int losers;
2023 int addr_losers;
2024 /* REJECT is a count of how undesirable this alternative says it is
2025 if any reloading is required. If the alternative matches exactly
2026 then REJECT is ignored, but otherwise it gets this much counted
2027 against it in addition to the reloading needed. */
2028 int reject;
2029 /* This is defined by '!' or '?' alternative constraint and added to
2030 reject. But in some cases it can be ignored. */
2031 int static_reject;
2032 int op_reject;
2033 /* The number of elements in the following array. */
2034 int early_clobbered_regs_num;
2035 /* Numbers of operands which are early clobber registers. */
2036 int early_clobbered_nops[MAX_RECOG_OPERANDS];
2037 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
2038 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
2039 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
2040 bool curr_alt_win[MAX_RECOG_OPERANDS];
2041 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
2042 int curr_alt_matches[MAX_RECOG_OPERANDS];
2043 /* The number of elements in the following array. */
2044 int curr_alt_dont_inherit_ops_num;
2045 /* Numbers of operands whose reload pseudos should not be inherited. */
2046 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
2047 rtx op;
2048 /* The register when the operand is a subreg of register, otherwise the
2049 operand itself. */
2050 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
2051 /* The register if the operand is a register or subreg of register,
2052 otherwise NULL. */
2053 rtx operand_reg[MAX_RECOG_OPERANDS];
2054 int hard_regno[MAX_RECOG_OPERANDS];
2055 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
2056 int reload_nregs, reload_sum;
2057 bool costly_p;
2058 enum reg_class cl;
2060 /* Calculate some data common for all alternatives to speed up the
2061 function. */
2062 for (nop = 0; nop < n_operands; nop++)
2064 rtx reg;
2066 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
2067 /* The real hard regno of the operand after the allocation. */
2068 hard_regno[nop] = get_hard_regno (op, true);
2070 operand_reg[nop] = reg = op;
2071 biggest_mode[nop] = GET_MODE (op);
2072 if (GET_CODE (op) == SUBREG)
2074 biggest_mode[nop] = wider_subreg_mode (op);
2075 operand_reg[nop] = reg = SUBREG_REG (op);
2077 if (! REG_P (reg))
2078 operand_reg[nop] = NULL_RTX;
2079 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
2080 || ((int) REGNO (reg)
2081 == lra_get_elimination_hard_regno (REGNO (reg))))
2082 no_subreg_reg_operand[nop] = reg;
2083 else
2084 operand_reg[nop] = no_subreg_reg_operand[nop]
2085 /* Just use natural mode for elimination result. It should
2086 be enough for extra constraints hooks. */
2087 = regno_reg_rtx[hard_regno[nop]];
2090 /* The constraints are made of several alternatives. Each operand's
2091 constraint looks like foo,bar,... with commas separating the
2092 alternatives. The first alternatives for all operands go
2093 together, the second alternatives go together, etc.
2095 First loop over alternatives. */
2096 alternative_mask preferred = curr_id->preferred_alternatives;
2097 if (only_alternative >= 0)
2098 preferred &= ALTERNATIVE_BIT (only_alternative);
2100 for (nalt = 0; nalt < n_alternatives; nalt++)
2102 /* Loop over operands for one constraint alternative. */
2103 if (!TEST_BIT (preferred, nalt))
2104 continue;
2106 bool matching_early_clobber[MAX_RECOG_OPERANDS];
2107 curr_small_class_check++;
2108 overall = losers = addr_losers = 0;
2109 static_reject = reject = reload_nregs = reload_sum = 0;
2110 for (nop = 0; nop < n_operands; nop++)
2112 int inc = (curr_static_id
2113 ->operand_alternative[nalt * n_operands + nop].reject);
2114 if (lra_dump_file != NULL && inc != 0)
2115 fprintf (lra_dump_file,
2116 " Staticly defined alt reject+=%d\n", inc);
2117 static_reject += inc;
2118 matching_early_clobber[nop] = 0;
2120 reject += static_reject;
2121 early_clobbered_regs_num = 0;
2123 for (nop = 0; nop < n_operands; nop++)
2125 const char *p;
2126 char *end;
2127 int len, c, m, i, opalt_num, this_alternative_matches;
2128 bool win, did_match, offmemok, early_clobber_p;
2129 /* false => this operand can be reloaded somehow for this
2130 alternative. */
2131 bool badop;
2132 /* true => this operand can be reloaded if the alternative
2133 allows regs. */
2134 bool winreg;
2135 /* True if a constant forced into memory would be OK for
2136 this operand. */
2137 bool constmemok;
2138 enum reg_class this_alternative, this_costly_alternative;
2139 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
2140 bool this_alternative_match_win, this_alternative_win;
2141 bool this_alternative_offmemok;
2142 bool scratch_p;
2143 machine_mode mode;
2144 enum constraint_num cn;
2146 opalt_num = nalt * n_operands + nop;
2147 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
2149 /* Fast track for no constraints at all. */
2150 curr_alt[nop] = NO_REGS;
2151 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
2152 curr_alt_win[nop] = true;
2153 curr_alt_match_win[nop] = false;
2154 curr_alt_offmemok[nop] = false;
2155 curr_alt_matches[nop] = -1;
2156 continue;
2159 op = no_subreg_reg_operand[nop];
2160 mode = curr_operand_mode[nop];
2162 win = did_match = winreg = offmemok = constmemok = false;
2163 badop = true;
2165 early_clobber_p = false;
2166 p = curr_static_id->operand_alternative[opalt_num].constraint;
2168 this_costly_alternative = this_alternative = NO_REGS;
2169 /* We update set of possible hard regs besides its class
2170 because reg class might be inaccurate. For example,
2171 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
2172 is translated in HI_REGS because classes are merged by
2173 pairs and there is no accurate intermediate class. */
2174 CLEAR_HARD_REG_SET (this_alternative_set);
2175 CLEAR_HARD_REG_SET (this_costly_alternative_set);
2176 this_alternative_win = false;
2177 this_alternative_match_win = false;
2178 this_alternative_offmemok = false;
2179 this_alternative_matches = -1;
2181 /* An empty constraint should be excluded by the fast
2182 track. */
2183 lra_assert (*p != 0 && *p != ',');
2185 op_reject = 0;
2186 /* Scan this alternative's specs for this operand; set WIN
2187 if the operand fits any letter in this alternative.
2188 Otherwise, clear BADOP if this operand could fit some
2189 letter after reloads, or set WINREG if this operand could
2190 fit after reloads provided the constraint allows some
2191 registers. */
2192 costly_p = false;
2195 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
2197 case '\0':
2198 len = 0;
2199 break;
2200 case ',':
2201 c = '\0';
2202 break;
2204 case '&':
2205 early_clobber_p = true;
2206 break;
2208 case '$':
2209 op_reject += LRA_MAX_REJECT;
2210 break;
2211 case '^':
2212 op_reject += LRA_LOSER_COST_FACTOR;
2213 break;
2215 case '#':
2216 /* Ignore rest of this alternative. */
2217 c = '\0';
2218 break;
2220 case '0': case '1': case '2': case '3': case '4':
2221 case '5': case '6': case '7': case '8': case '9':
2223 int m_hregno;
2224 bool match_p;
2226 m = strtoul (p, &end, 10);
2227 p = end;
2228 len = 0;
2229 lra_assert (nop > m);
2231 /* Reject matches if we don't know which operand is
2232 bigger. This situation would arguably be a bug in
2233 an .md pattern, but could also occur in a user asm. */
2234 if (!ordered_p (GET_MODE_SIZE (biggest_mode[m]),
2235 GET_MODE_SIZE (biggest_mode[nop])))
2236 break;
2238 /* Don't match wrong asm insn operands for proper
2239 diagnostic later. */
2240 if (INSN_CODE (curr_insn) < 0
2241 && (curr_operand_mode[m] == BLKmode
2242 || curr_operand_mode[nop] == BLKmode)
2243 && curr_operand_mode[m] != curr_operand_mode[nop])
2244 break;
2246 m_hregno = get_hard_regno (*curr_id->operand_loc[m], false);
2247 /* We are supposed to match a previous operand.
2248 If we do, we win if that one did. If we do
2249 not, count both of the operands as losers.
2250 (This is too conservative, since most of the
2251 time only a single reload insn will be needed
2252 to make the two operands win. As a result,
2253 this alternative may be rejected when it is
2254 actually desirable.) */
2255 match_p = false;
2256 if (operands_match_p (*curr_id->operand_loc[nop],
2257 *curr_id->operand_loc[m], m_hregno))
2259 /* We should reject matching of an early
2260 clobber operand if the matching operand is
2261 not dying in the insn. */
2262 if (!TEST_BIT (curr_static_id->operand[m]
2263 .early_clobber_alts, nalt)
2264 || operand_reg[nop] == NULL_RTX
2265 || (find_regno_note (curr_insn, REG_DEAD,
2266 REGNO (op))
2267 || REGNO (op) == REGNO (operand_reg[m])))
2268 match_p = true;
2270 if (match_p)
2272 /* If we are matching a non-offsettable
2273 address where an offsettable address was
2274 expected, then we must reject this
2275 combination, because we can't reload
2276 it. */
2277 if (curr_alt_offmemok[m]
2278 && MEM_P (*curr_id->operand_loc[m])
2279 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
2280 continue;
2282 else
2284 /* If the operands do not match and one
2285 operand is INOUT, we can not match them.
2286 Try other possibilities, e.g. other
2287 alternatives or commutative operand
2288 exchange. */
2289 if (curr_static_id->operand[nop].type == OP_INOUT
2290 || curr_static_id->operand[m].type == OP_INOUT)
2291 break;
2292 /* Operands don't match. If the operands are
2293 different user defined explicit hard
2294 registers, then we cannot make them match
2295 when one is early clobber operand. */
2296 if ((REG_P (*curr_id->operand_loc[nop])
2297 || SUBREG_P (*curr_id->operand_loc[nop]))
2298 && (REG_P (*curr_id->operand_loc[m])
2299 || SUBREG_P (*curr_id->operand_loc[m])))
2301 rtx nop_reg = *curr_id->operand_loc[nop];
2302 if (SUBREG_P (nop_reg))
2303 nop_reg = SUBREG_REG (nop_reg);
2304 rtx m_reg = *curr_id->operand_loc[m];
2305 if (SUBREG_P (m_reg))
2306 m_reg = SUBREG_REG (m_reg);
2308 if (REG_P (nop_reg)
2309 && HARD_REGISTER_P (nop_reg)
2310 && REG_USERVAR_P (nop_reg)
2311 && REG_P (m_reg)
2312 && HARD_REGISTER_P (m_reg)
2313 && REG_USERVAR_P (m_reg))
2315 int i;
2317 for (i = 0; i < early_clobbered_regs_num; i++)
2318 if (m == early_clobbered_nops[i])
2319 break;
2320 if (i < early_clobbered_regs_num
2321 || early_clobber_p)
2322 break;
2325 /* Both operands must allow a reload register,
2326 otherwise we cannot make them match. */
2327 if (curr_alt[m] == NO_REGS)
2328 break;
2329 /* Retroactively mark the operand we had to
2330 match as a loser, if it wasn't already and
2331 it wasn't matched to a register constraint
2332 (e.g it might be matched by memory). */
2333 if (curr_alt_win[m]
2334 && (operand_reg[m] == NULL_RTX
2335 || hard_regno[m] < 0))
2337 losers++;
2338 reload_nregs
2339 += (ira_reg_class_max_nregs[curr_alt[m]]
2340 [GET_MODE (*curr_id->operand_loc[m])]);
2343 /* Prefer matching earlyclobber alternative as
2344 it results in less hard regs required for
2345 the insn than a non-matching earlyclobber
2346 alternative. */
2347 if (TEST_BIT (curr_static_id->operand[m]
2348 .early_clobber_alts, nalt))
2350 if (lra_dump_file != NULL)
2351 fprintf
2352 (lra_dump_file,
2353 " %d Matching earlyclobber alt:"
2354 " reject--\n",
2355 nop);
2356 if (!matching_early_clobber[m])
2358 reject--;
2359 matching_early_clobber[m] = 1;
2362 /* Otherwise we prefer no matching
2363 alternatives because it gives more freedom
2364 in RA. */
2365 else if (operand_reg[nop] == NULL_RTX
2366 || (find_regno_note (curr_insn, REG_DEAD,
2367 REGNO (operand_reg[nop]))
2368 == NULL_RTX))
2370 if (lra_dump_file != NULL)
2371 fprintf
2372 (lra_dump_file,
2373 " %d Matching alt: reject+=2\n",
2374 nop);
2375 reject += 2;
2378 /* If we have to reload this operand and some
2379 previous operand also had to match the same
2380 thing as this operand, we don't know how to do
2381 that. */
2382 if (!match_p || !curr_alt_win[m])
2384 for (i = 0; i < nop; i++)
2385 if (curr_alt_matches[i] == m)
2386 break;
2387 if (i < nop)
2388 break;
2390 else
2391 did_match = true;
2393 this_alternative_matches = m;
2394 /* This can be fixed with reloads if the operand
2395 we are supposed to match can be fixed with
2396 reloads. */
2397 badop = false;
2398 this_alternative = curr_alt[m];
2399 this_alternative_set = curr_alt_set[m];
2400 winreg = this_alternative != NO_REGS;
2401 break;
2404 case 'g':
2405 if (MEM_P (op)
2406 || general_constant_p (op)
2407 || spilled_pseudo_p (op))
2408 win = true;
2409 cl = GENERAL_REGS;
2410 goto reg;
2412 default:
2413 cn = lookup_constraint (p);
2414 switch (get_constraint_type (cn))
2416 case CT_REGISTER:
2417 cl = reg_class_for_constraint (cn);
2418 if (cl != NO_REGS)
2419 goto reg;
2420 break;
2422 case CT_CONST_INT:
2423 if (CONST_INT_P (op)
2424 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2425 win = true;
2426 break;
2428 case CT_MEMORY:
2429 if (satisfies_memory_constraint_p (op, cn))
2430 win = true;
2431 else if (spilled_pseudo_p (op))
2432 win = true;
2434 /* If we didn't already win, we can reload constants
2435 via force_const_mem or put the pseudo value into
2436 memory, or make other memory by reloading the
2437 address like for 'o'. */
2438 if (CONST_POOL_OK_P (mode, op)
2439 || MEM_P (op) || REG_P (op)
2440 /* We can restore the equiv insn by a
2441 reload. */
2442 || equiv_substition_p[nop])
2443 badop = false;
2444 constmemok = true;
2445 offmemok = true;
2446 break;
2448 case CT_ADDRESS:
2449 /* An asm operand with an address constraint
2450 that doesn't satisfy address_operand has
2451 is_address cleared, so that we don't try to
2452 make a non-address fit. */
2453 if (!curr_static_id->operand[nop].is_address)
2454 break;
2455 /* If we didn't already win, we can reload the address
2456 into a base register. */
2457 if (satisfies_address_constraint_p (op, cn))
2458 win = true;
2459 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2460 ADDRESS, SCRATCH);
2461 badop = false;
2462 goto reg;
2464 case CT_FIXED_FORM:
2465 if (constraint_satisfied_p (op, cn))
2466 win = true;
2467 break;
2469 case CT_SPECIAL_MEMORY:
2470 if (satisfies_memory_constraint_p (op, cn))
2471 win = true;
2472 else if (spilled_pseudo_p (op))
2473 win = true;
2474 break;
2476 break;
2478 reg:
2479 if (mode == BLKmode)
2480 break;
2481 this_alternative = reg_class_subunion[this_alternative][cl];
2482 this_alternative_set |= reg_class_contents[cl];
2483 if (costly_p)
2485 this_costly_alternative
2486 = reg_class_subunion[this_costly_alternative][cl];
2487 this_costly_alternative_set |= reg_class_contents[cl];
2489 winreg = true;
2490 if (REG_P (op))
2492 if (hard_regno[nop] >= 0
2493 && in_hard_reg_set_p (this_alternative_set,
2494 mode, hard_regno[nop]))
2495 win = true;
2496 else if (hard_regno[nop] < 0
2497 && in_class_p (op, this_alternative, NULL))
2498 win = true;
2500 break;
2502 if (c != ' ' && c != '\t')
2503 costly_p = c == '*';
2505 while ((p += len), c);
2507 scratch_p = (operand_reg[nop] != NULL_RTX
2508 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2509 /* Record which operands fit this alternative. */
2510 if (win)
2512 this_alternative_win = true;
2513 if (operand_reg[nop] != NULL_RTX)
2515 if (hard_regno[nop] >= 0)
2517 if (in_hard_reg_set_p (this_costly_alternative_set,
2518 mode, hard_regno[nop]))
2520 if (lra_dump_file != NULL)
2521 fprintf (lra_dump_file,
2522 " %d Costly set: reject++\n",
2523 nop);
2524 reject++;
2527 else
2529 /* Prefer won reg to spilled pseudo under other
2530 equal conditions for possibe inheritance. */
2531 if (! scratch_p)
2533 if (lra_dump_file != NULL)
2534 fprintf
2535 (lra_dump_file,
2536 " %d Non pseudo reload: reject++\n",
2537 nop);
2538 reject++;
2540 if (in_class_p (operand_reg[nop],
2541 this_costly_alternative, NULL))
2543 if (lra_dump_file != NULL)
2544 fprintf
2545 (lra_dump_file,
2546 " %d Non pseudo costly reload:"
2547 " reject++\n",
2548 nop);
2549 reject++;
2552 /* We simulate the behavior of old reload here.
2553 Although scratches need hard registers and it
2554 might result in spilling other pseudos, no reload
2555 insns are generated for the scratches. So it
2556 might cost something but probably less than old
2557 reload pass believes. */
2558 if (scratch_p)
2560 if (lra_dump_file != NULL)
2561 fprintf (lra_dump_file,
2562 " %d Scratch win: reject+=2\n",
2563 nop);
2564 reject += 2;
2568 else if (did_match)
2569 this_alternative_match_win = true;
2570 else
2572 int const_to_mem = 0;
2573 bool no_regs_p;
2575 reject += op_reject;
2576 /* Never do output reload of stack pointer. It makes
2577 impossible to do elimination when SP is changed in
2578 RTL. */
2579 if (op == stack_pointer_rtx && ! frame_pointer_needed
2580 && curr_static_id->operand[nop].type != OP_IN)
2581 goto fail;
2583 /* If this alternative asks for a specific reg class, see if there
2584 is at least one allocatable register in that class. */
2585 no_regs_p
2586 = (this_alternative == NO_REGS
2587 || (hard_reg_set_subset_p
2588 (reg_class_contents[this_alternative],
2589 lra_no_alloc_regs)));
2591 /* For asms, verify that the class for this alternative is possible
2592 for the mode that is specified. */
2593 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2595 int i;
2596 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2597 if (targetm.hard_regno_mode_ok (i, mode)
2598 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2599 mode, i))
2600 break;
2601 if (i == FIRST_PSEUDO_REGISTER)
2602 winreg = false;
2605 /* If this operand accepts a register, and if the
2606 register class has at least one allocatable register,
2607 then this operand can be reloaded. */
2608 if (winreg && !no_regs_p)
2609 badop = false;
2611 if (badop)
2613 if (lra_dump_file != NULL)
2614 fprintf (lra_dump_file,
2615 " alt=%d: Bad operand -- refuse\n",
2616 nalt);
2617 goto fail;
2620 if (this_alternative != NO_REGS)
2622 HARD_REG_SET available_regs
2623 = (reg_class_contents[this_alternative]
2624 & ~((ira_prohibited_class_mode_regs
2625 [this_alternative][mode])
2626 | lra_no_alloc_regs));
2627 if (hard_reg_set_empty_p (available_regs))
2629 /* There are no hard regs holding a value of given
2630 mode. */
2631 if (offmemok)
2633 this_alternative = NO_REGS;
2634 if (lra_dump_file != NULL)
2635 fprintf (lra_dump_file,
2636 " %d Using memory because of"
2637 " a bad mode: reject+=2\n",
2638 nop);
2639 reject += 2;
2641 else
2643 if (lra_dump_file != NULL)
2644 fprintf (lra_dump_file,
2645 " alt=%d: Wrong mode -- refuse\n",
2646 nalt);
2647 goto fail;
2652 /* If not assigned pseudo has a class which a subset of
2653 required reg class, it is a less costly alternative
2654 as the pseudo still can get a hard reg of necessary
2655 class. */
2656 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2657 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2658 && ira_class_subset_p[this_alternative][cl])
2660 if (lra_dump_file != NULL)
2661 fprintf
2662 (lra_dump_file,
2663 " %d Super set class reg: reject-=3\n", nop);
2664 reject -= 3;
2667 this_alternative_offmemok = offmemok;
2668 if (this_costly_alternative != NO_REGS)
2670 if (lra_dump_file != NULL)
2671 fprintf (lra_dump_file,
2672 " %d Costly loser: reject++\n", nop);
2673 reject++;
2675 /* If the operand is dying, has a matching constraint,
2676 and satisfies constraints of the matched operand
2677 which failed to satisfy the own constraints, most probably
2678 the reload for this operand will be gone. */
2679 if (this_alternative_matches >= 0
2680 && !curr_alt_win[this_alternative_matches]
2681 && REG_P (op)
2682 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2683 && (hard_regno[nop] >= 0
2684 ? in_hard_reg_set_p (this_alternative_set,
2685 mode, hard_regno[nop])
2686 : in_class_p (op, this_alternative, NULL)))
2688 if (lra_dump_file != NULL)
2689 fprintf
2690 (lra_dump_file,
2691 " %d Dying matched operand reload: reject++\n",
2692 nop);
2693 reject++;
2695 else
2697 /* Strict_low_part requires to reload the register
2698 not the sub-register. In this case we should
2699 check that a final reload hard reg can hold the
2700 value mode. */
2701 if (curr_static_id->operand[nop].strict_low
2702 && REG_P (op)
2703 && hard_regno[nop] < 0
2704 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2705 && ira_class_hard_regs_num[this_alternative] > 0
2706 && (!targetm.hard_regno_mode_ok
2707 (ira_class_hard_regs[this_alternative][0],
2708 GET_MODE (*curr_id->operand_loc[nop]))))
2710 if (lra_dump_file != NULL)
2711 fprintf
2712 (lra_dump_file,
2713 " alt=%d: Strict low subreg reload -- refuse\n",
2714 nalt);
2715 goto fail;
2717 losers++;
2719 if (operand_reg[nop] != NULL_RTX
2720 /* Output operands and matched input operands are
2721 not inherited. The following conditions do not
2722 exactly describe the previous statement but they
2723 are pretty close. */
2724 && curr_static_id->operand[nop].type != OP_OUT
2725 && (this_alternative_matches < 0
2726 || curr_static_id->operand[nop].type != OP_IN))
2728 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2729 (operand_reg[nop])]
2730 .last_reload);
2732 /* The value of reload_sum has sense only if we
2733 process insns in their order. It happens only on
2734 the first constraints sub-pass when we do most of
2735 reload work. */
2736 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2737 reload_sum += last_reload - bb_reload_num;
2739 /* If this is a constant that is reloaded into the
2740 desired class by copying it to memory first, count
2741 that as another reload. This is consistent with
2742 other code and is required to avoid choosing another
2743 alternative when the constant is moved into memory.
2744 Note that the test here is precisely the same as in
2745 the code below that calls force_const_mem. */
2746 if (CONST_POOL_OK_P (mode, op)
2747 && ((targetm.preferred_reload_class
2748 (op, this_alternative) == NO_REGS)
2749 || no_input_reloads_p))
2751 const_to_mem = 1;
2752 if (! no_regs_p)
2753 losers++;
2756 /* Alternative loses if it requires a type of reload not
2757 permitted for this insn. We can always reload
2758 objects with a REG_UNUSED note. */
2759 if ((curr_static_id->operand[nop].type != OP_IN
2760 && no_output_reloads_p
2761 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2762 || (curr_static_id->operand[nop].type != OP_OUT
2763 && no_input_reloads_p && ! const_to_mem)
2764 || (this_alternative_matches >= 0
2765 && (no_input_reloads_p
2766 || (no_output_reloads_p
2767 && (curr_static_id->operand
2768 [this_alternative_matches].type != OP_IN)
2769 && ! find_reg_note (curr_insn, REG_UNUSED,
2770 no_subreg_reg_operand
2771 [this_alternative_matches])))))
2773 if (lra_dump_file != NULL)
2774 fprintf
2775 (lra_dump_file,
2776 " alt=%d: No input/otput reload -- refuse\n",
2777 nalt);
2778 goto fail;
2781 /* Alternative loses if it required class pseudo cannot
2782 hold value of required mode. Such insns can be
2783 described by insn definitions with mode iterators. */
2784 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2785 && ! hard_reg_set_empty_p (this_alternative_set)
2786 /* It is common practice for constraints to use a
2787 class which does not have actually enough regs to
2788 hold the value (e.g. x86 AREG for mode requiring
2789 more one general reg). Therefore we have 2
2790 conditions to check that the reload pseudo cannot
2791 hold the mode value. */
2792 && (!targetm.hard_regno_mode_ok
2793 (ira_class_hard_regs[this_alternative][0],
2794 GET_MODE (*curr_id->operand_loc[nop])))
2795 /* The above condition is not enough as the first
2796 reg in ira_class_hard_regs can be not aligned for
2797 multi-words mode values. */
2798 && (prohibited_class_reg_set_mode_p
2799 (this_alternative, this_alternative_set,
2800 GET_MODE (*curr_id->operand_loc[nop]))))
2802 if (lra_dump_file != NULL)
2803 fprintf (lra_dump_file,
2804 " alt=%d: reload pseudo for op %d "
2805 "cannot hold the mode value -- refuse\n",
2806 nalt, nop);
2807 goto fail;
2810 /* Check strong discouragement of reload of non-constant
2811 into class THIS_ALTERNATIVE. */
2812 if (! CONSTANT_P (op) && ! no_regs_p
2813 && (targetm.preferred_reload_class
2814 (op, this_alternative) == NO_REGS
2815 || (curr_static_id->operand[nop].type == OP_OUT
2816 && (targetm.preferred_output_reload_class
2817 (op, this_alternative) == NO_REGS))))
2819 if (offmemok && REG_P (op))
2821 if (lra_dump_file != NULL)
2822 fprintf
2823 (lra_dump_file,
2824 " %d Spill pseudo into memory: reject+=3\n",
2825 nop);
2826 reject += 3;
2828 else
2830 if (lra_dump_file != NULL)
2831 fprintf
2832 (lra_dump_file,
2833 " %d Non-prefered reload: reject+=%d\n",
2834 nop, LRA_MAX_REJECT);
2835 reject += LRA_MAX_REJECT;
2839 if (! (MEM_P (op) && offmemok)
2840 && ! (const_to_mem && constmemok))
2842 /* We prefer to reload pseudos over reloading other
2843 things, since such reloads may be able to be
2844 eliminated later. So bump REJECT in other cases.
2845 Don't do this in the case where we are forcing a
2846 constant into memory and it will then win since
2847 we don't want to have a different alternative
2848 match then. */
2849 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2851 if (lra_dump_file != NULL)
2852 fprintf
2853 (lra_dump_file,
2854 " %d Non-pseudo reload: reject+=2\n",
2855 nop);
2856 reject += 2;
2859 if (! no_regs_p)
2860 reload_nregs
2861 += ira_reg_class_max_nregs[this_alternative][mode];
2863 if (SMALL_REGISTER_CLASS_P (this_alternative))
2865 if (lra_dump_file != NULL)
2866 fprintf
2867 (lra_dump_file,
2868 " %d Small class reload: reject+=%d\n",
2869 nop, LRA_LOSER_COST_FACTOR / 2);
2870 reject += LRA_LOSER_COST_FACTOR / 2;
2874 /* We are trying to spill pseudo into memory. It is
2875 usually more costly than moving to a hard register
2876 although it might takes the same number of
2877 reloads.
2879 Non-pseudo spill may happen also. Suppose a target allows both
2880 register and memory in the operand constraint alternatives,
2881 then it's typical that an eliminable register has a substition
2882 of "base + offset" which can either be reloaded by a simple
2883 "new_reg <= base + offset" which will match the register
2884 constraint, or a similar reg addition followed by further spill
2885 to and reload from memory which will match the memory
2886 constraint, but this memory spill will be much more costly
2887 usually.
2889 Code below increases the reject for both pseudo and non-pseudo
2890 spill. */
2891 if (no_regs_p
2892 && !(MEM_P (op) && offmemok)
2893 && !(REG_P (op) && hard_regno[nop] < 0))
2895 if (lra_dump_file != NULL)
2896 fprintf
2897 (lra_dump_file,
2898 " %d Spill %spseudo into memory: reject+=3\n",
2899 nop, REG_P (op) ? "" : "Non-");
2900 reject += 3;
2901 if (VECTOR_MODE_P (mode))
2903 /* Spilling vectors into memory is usually more
2904 costly as they contain big values. */
2905 if (lra_dump_file != NULL)
2906 fprintf
2907 (lra_dump_file,
2908 " %d Spill vector pseudo: reject+=2\n",
2909 nop);
2910 reject += 2;
2914 /* When we use an operand requiring memory in given
2915 alternative, the insn should write *and* read the
2916 value to/from memory it is costly in comparison with
2917 an insn alternative which does not use memory
2918 (e.g. register or immediate operand). We exclude
2919 memory operand for such case as we can satisfy the
2920 memory constraints by reloading address. */
2921 if (no_regs_p && offmemok && !MEM_P (op))
2923 if (lra_dump_file != NULL)
2924 fprintf
2925 (lra_dump_file,
2926 " Using memory insn operand %d: reject+=3\n",
2927 nop);
2928 reject += 3;
2931 /* If reload requires moving value through secondary
2932 memory, it will need one more insn at least. */
2933 if (this_alternative != NO_REGS
2934 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2935 && ((curr_static_id->operand[nop].type != OP_OUT
2936 && targetm.secondary_memory_needed (GET_MODE (op), cl,
2937 this_alternative))
2938 || (curr_static_id->operand[nop].type != OP_IN
2939 && (targetm.secondary_memory_needed
2940 (GET_MODE (op), this_alternative, cl)))))
2941 losers++;
2943 if (MEM_P (op) && offmemok)
2944 addr_losers++;
2945 else
2947 /* Input reloads can be inherited more often than
2948 output reloads can be removed, so penalize output
2949 reloads. */
2950 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2952 if (lra_dump_file != NULL)
2953 fprintf
2954 (lra_dump_file,
2955 " %d Non input pseudo reload: reject++\n",
2956 nop);
2957 reject++;
2960 if (curr_static_id->operand[nop].type == OP_INOUT)
2962 if (lra_dump_file != NULL)
2963 fprintf
2964 (lra_dump_file,
2965 " %d Input/Output reload: reject+=%d\n",
2966 nop, LRA_LOSER_COST_FACTOR);
2967 reject += LRA_LOSER_COST_FACTOR;
2972 if (early_clobber_p && ! scratch_p)
2974 if (lra_dump_file != NULL)
2975 fprintf (lra_dump_file,
2976 " %d Early clobber: reject++\n", nop);
2977 reject++;
2979 /* ??? We check early clobbers after processing all operands
2980 (see loop below) and there we update the costs more.
2981 Should we update the cost (may be approximately) here
2982 because of early clobber register reloads or it is a rare
2983 or non-important thing to be worth to do it. */
2984 overall = (losers * LRA_LOSER_COST_FACTOR + reject
2985 - (addr_losers == losers ? static_reject : 0));
2986 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2988 if (lra_dump_file != NULL)
2989 fprintf (lra_dump_file,
2990 " alt=%d,overall=%d,losers=%d -- refuse\n",
2991 nalt, overall, losers);
2992 goto fail;
2995 if (update_and_check_small_class_inputs (nop, nalt,
2996 this_alternative))
2998 if (lra_dump_file != NULL)
2999 fprintf (lra_dump_file,
3000 " alt=%d, not enough small class regs -- refuse\n",
3001 nalt);
3002 goto fail;
3004 curr_alt[nop] = this_alternative;
3005 curr_alt_set[nop] = this_alternative_set;
3006 curr_alt_win[nop] = this_alternative_win;
3007 curr_alt_match_win[nop] = this_alternative_match_win;
3008 curr_alt_offmemok[nop] = this_alternative_offmemok;
3009 curr_alt_matches[nop] = this_alternative_matches;
3011 if (this_alternative_matches >= 0
3012 && !did_match && !this_alternative_win)
3013 curr_alt_win[this_alternative_matches] = false;
3015 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
3016 early_clobbered_nops[early_clobbered_regs_num++] = nop;
3019 if (curr_insn_set != NULL_RTX && n_operands == 2
3020 /* Prevent processing non-move insns. */
3021 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
3022 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
3023 && ((! curr_alt_win[0] && ! curr_alt_win[1]
3024 && REG_P (no_subreg_reg_operand[0])
3025 && REG_P (no_subreg_reg_operand[1])
3026 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
3027 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
3028 || (! curr_alt_win[0] && curr_alt_win[1]
3029 && REG_P (no_subreg_reg_operand[1])
3030 /* Check that we reload memory not the memory
3031 address. */
3032 && ! (curr_alt_offmemok[0]
3033 && MEM_P (no_subreg_reg_operand[0]))
3034 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
3035 || (curr_alt_win[0] && ! curr_alt_win[1]
3036 && REG_P (no_subreg_reg_operand[0])
3037 /* Check that we reload memory not the memory
3038 address. */
3039 && ! (curr_alt_offmemok[1]
3040 && MEM_P (no_subreg_reg_operand[1]))
3041 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
3042 && (! CONST_POOL_OK_P (curr_operand_mode[1],
3043 no_subreg_reg_operand[1])
3044 || (targetm.preferred_reload_class
3045 (no_subreg_reg_operand[1],
3046 (enum reg_class) curr_alt[1]) != NO_REGS))
3047 /* If it is a result of recent elimination in move
3048 insn we can transform it into an add still by
3049 using this alternative. */
3050 && GET_CODE (no_subreg_reg_operand[1]) != PLUS
3051 /* Likewise if the source has been replaced with an
3052 equivalent value. This only happens once -- the reload
3053 will use the equivalent value instead of the register it
3054 replaces -- so there should be no danger of cycling. */
3055 && !equiv_substition_p[1])))
3057 /* We have a move insn and a new reload insn will be similar
3058 to the current insn. We should avoid such situation as
3059 it results in LRA cycling. */
3060 if (lra_dump_file != NULL)
3061 fprintf (lra_dump_file,
3062 " Cycle danger: overall += LRA_MAX_REJECT\n");
3063 overall += LRA_MAX_REJECT;
3065 ok_p = true;
3066 curr_alt_dont_inherit_ops_num = 0;
3067 for (nop = 0; nop < early_clobbered_regs_num; nop++)
3069 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
3070 HARD_REG_SET temp_set;
3072 i = early_clobbered_nops[nop];
3073 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
3074 || hard_regno[i] < 0)
3075 continue;
3076 lra_assert (operand_reg[i] != NULL_RTX);
3077 clobbered_hard_regno = hard_regno[i];
3078 CLEAR_HARD_REG_SET (temp_set);
3079 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
3080 first_conflict_j = last_conflict_j = -1;
3081 for (j = 0; j < n_operands; j++)
3082 if (j == i
3083 /* We don't want process insides of match_operator and
3084 match_parallel because otherwise we would process
3085 their operands once again generating a wrong
3086 code. */
3087 || curr_static_id->operand[j].is_operator)
3088 continue;
3089 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
3090 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
3091 continue;
3092 /* If we don't reload j-th operand, check conflicts. */
3093 else if ((curr_alt_win[j] || curr_alt_match_win[j])
3094 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
3096 if (first_conflict_j < 0)
3097 first_conflict_j = j;
3098 last_conflict_j = j;
3099 /* Both the earlyclobber operand and conflicting operand
3100 cannot both be user defined hard registers. */
3101 if (HARD_REGISTER_P (operand_reg[i])
3102 && REG_USERVAR_P (operand_reg[i])
3103 && operand_reg[j] != NULL_RTX
3104 && HARD_REGISTER_P (operand_reg[j])
3105 && REG_USERVAR_P (operand_reg[j]))
3106 fatal_insn ("unable to generate reloads for "
3107 "impossible constraints:", curr_insn);
3109 if (last_conflict_j < 0)
3110 continue;
3112 /* If an earlyclobber operand conflicts with another non-matching
3113 operand (ie, they have been assigned the same hard register),
3114 then it is better to reload the other operand, as there may
3115 exist yet another operand with a matching constraint associated
3116 with the earlyclobber operand. However, if one of the operands
3117 is an explicit use of a hard register, then we must reload the
3118 other non-hard register operand. */
3119 if (HARD_REGISTER_P (operand_reg[i])
3120 || (first_conflict_j == last_conflict_j
3121 && operand_reg[last_conflict_j] != NULL_RTX
3122 && !curr_alt_match_win[last_conflict_j]
3123 && !HARD_REGISTER_P (operand_reg[last_conflict_j])))
3125 curr_alt_win[last_conflict_j] = false;
3126 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
3127 = last_conflict_j;
3128 losers++;
3129 if (lra_dump_file != NULL)
3130 fprintf
3131 (lra_dump_file,
3132 " %d Conflict early clobber reload: reject--\n",
3135 else
3137 /* We need to reload early clobbered register and the
3138 matched registers. */
3139 for (j = 0; j < n_operands; j++)
3140 if (curr_alt_matches[j] == i)
3142 curr_alt_match_win[j] = false;
3143 losers++;
3144 overall += LRA_LOSER_COST_FACTOR;
3146 if (! curr_alt_match_win[i])
3147 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
3148 else
3150 /* Remember pseudos used for match reloads are never
3151 inherited. */
3152 lra_assert (curr_alt_matches[i] >= 0);
3153 curr_alt_win[curr_alt_matches[i]] = false;
3155 curr_alt_win[i] = curr_alt_match_win[i] = false;
3156 losers++;
3157 if (lra_dump_file != NULL)
3158 fprintf
3159 (lra_dump_file,
3160 " %d Matched conflict early clobber reloads: "
3161 "reject--\n",
3164 /* Early clobber was already reflected in REJECT. */
3165 if (!matching_early_clobber[i])
3167 lra_assert (reject > 0);
3168 reject--;
3169 matching_early_clobber[i] = 1;
3171 overall += LRA_LOSER_COST_FACTOR - 1;
3173 if (lra_dump_file != NULL)
3174 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
3175 nalt, overall, losers, reload_nregs);
3177 /* If this alternative can be made to work by reloading, and it
3178 needs less reloading than the others checked so far, record
3179 it as the chosen goal for reloading. */
3180 if ((best_losers != 0 && losers == 0)
3181 || (((best_losers == 0 && losers == 0)
3182 || (best_losers != 0 && losers != 0))
3183 && (best_overall > overall
3184 || (best_overall == overall
3185 /* If the cost of the reloads is the same,
3186 prefer alternative which requires minimal
3187 number of reload regs. */
3188 && (reload_nregs < best_reload_nregs
3189 || (reload_nregs == best_reload_nregs
3190 && (best_reload_sum < reload_sum
3191 || (best_reload_sum == reload_sum
3192 && nalt < goal_alt_number))))))))
3194 for (nop = 0; nop < n_operands; nop++)
3196 goal_alt_win[nop] = curr_alt_win[nop];
3197 goal_alt_match_win[nop] = curr_alt_match_win[nop];
3198 goal_alt_matches[nop] = curr_alt_matches[nop];
3199 goal_alt[nop] = curr_alt[nop];
3200 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
3202 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
3203 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
3204 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
3205 goal_alt_swapped = curr_swapped;
3206 best_overall = overall;
3207 best_losers = losers;
3208 best_reload_nregs = reload_nregs;
3209 best_reload_sum = reload_sum;
3210 goal_alt_number = nalt;
3212 if (losers == 0)
3213 /* Everything is satisfied. Do not process alternatives
3214 anymore. */
3215 break;
3216 fail:
3219 return ok_p;
3222 /* Make reload base reg from address AD. */
3223 static rtx
3224 base_to_reg (struct address_info *ad)
3226 enum reg_class cl;
3227 int code = -1;
3228 rtx new_inner = NULL_RTX;
3229 rtx new_reg = NULL_RTX;
3230 rtx_insn *insn;
3231 rtx_insn *last_insn = get_last_insn();
3233 lra_assert (ad->disp == ad->disp_term);
3234 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
3235 get_index_code (ad));
3236 new_reg = lra_create_new_reg (GET_MODE (*ad->base), NULL_RTX,
3237 cl, "base");
3238 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
3239 ad->disp_term == NULL
3240 ? const0_rtx
3241 : *ad->disp_term);
3242 if (!valid_address_p (ad->mode, new_inner, ad->as))
3243 return NULL_RTX;
3244 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base));
3245 code = recog_memoized (insn);
3246 if (code < 0)
3248 delete_insns_since (last_insn);
3249 return NULL_RTX;
3252 return new_inner;
3255 /* Make reload base reg + DISP from address AD. Return the new pseudo. */
3256 static rtx
3257 base_plus_disp_to_reg (struct address_info *ad, rtx disp)
3259 enum reg_class cl;
3260 rtx new_reg;
3262 lra_assert (ad->base == ad->base_term);
3263 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
3264 get_index_code (ad));
3265 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
3266 cl, "base + disp");
3267 lra_emit_add (new_reg, *ad->base_term, disp);
3268 return new_reg;
3271 /* Make reload of index part of address AD. Return the new
3272 pseudo. */
3273 static rtx
3274 index_part_to_reg (struct address_info *ad)
3276 rtx new_reg;
3278 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
3279 INDEX_REG_CLASS, "index term");
3280 expand_mult (GET_MODE (*ad->index), *ad->index_term,
3281 GEN_INT (get_index_scale (ad)), new_reg, 1);
3282 return new_reg;
3285 /* Return true if we can add a displacement to address AD, even if that
3286 makes the address invalid. The fix-up code requires any new address
3287 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
3288 static bool
3289 can_add_disp_p (struct address_info *ad)
3291 return (!ad->autoinc_p
3292 && ad->segment == NULL
3293 && ad->base == ad->base_term
3294 && ad->disp == ad->disp_term);
3297 /* Make equiv substitution in address AD. Return true if a substitution
3298 was made. */
3299 static bool
3300 equiv_address_substitution (struct address_info *ad)
3302 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
3303 poly_int64 disp;
3304 HOST_WIDE_INT scale;
3305 bool change_p;
3307 base_term = strip_subreg (ad->base_term);
3308 if (base_term == NULL)
3309 base_reg = new_base_reg = NULL_RTX;
3310 else
3312 base_reg = *base_term;
3313 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
3315 index_term = strip_subreg (ad->index_term);
3316 if (index_term == NULL)
3317 index_reg = new_index_reg = NULL_RTX;
3318 else
3320 index_reg = *index_term;
3321 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
3323 if (base_reg == new_base_reg && index_reg == new_index_reg)
3324 return false;
3325 disp = 0;
3326 change_p = false;
3327 if (lra_dump_file != NULL)
3329 fprintf (lra_dump_file, "Changing address in insn %d ",
3330 INSN_UID (curr_insn));
3331 dump_value_slim (lra_dump_file, *ad->outer, 1);
3333 if (base_reg != new_base_reg)
3335 poly_int64 offset;
3336 if (REG_P (new_base_reg))
3338 *base_term = new_base_reg;
3339 change_p = true;
3341 else if (GET_CODE (new_base_reg) == PLUS
3342 && REG_P (XEXP (new_base_reg, 0))
3343 && poly_int_rtx_p (XEXP (new_base_reg, 1), &offset)
3344 && can_add_disp_p (ad))
3346 disp += offset;
3347 *base_term = XEXP (new_base_reg, 0);
3348 change_p = true;
3350 if (ad->base_term2 != NULL)
3351 *ad->base_term2 = *ad->base_term;
3353 if (index_reg != new_index_reg)
3355 poly_int64 offset;
3356 if (REG_P (new_index_reg))
3358 *index_term = new_index_reg;
3359 change_p = true;
3361 else if (GET_CODE (new_index_reg) == PLUS
3362 && REG_P (XEXP (new_index_reg, 0))
3363 && poly_int_rtx_p (XEXP (new_index_reg, 1), &offset)
3364 && can_add_disp_p (ad)
3365 && (scale = get_index_scale (ad)))
3367 disp += offset * scale;
3368 *index_term = XEXP (new_index_reg, 0);
3369 change_p = true;
3372 if (maybe_ne (disp, 0))
3374 if (ad->disp != NULL)
3375 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
3376 else
3378 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
3379 update_address (ad);
3381 change_p = true;
3383 if (lra_dump_file != NULL)
3385 if (! change_p)
3386 fprintf (lra_dump_file, " -- no change\n");
3387 else
3389 fprintf (lra_dump_file, " on equiv ");
3390 dump_value_slim (lra_dump_file, *ad->outer, 1);
3391 fprintf (lra_dump_file, "\n");
3394 return change_p;
3397 /* Major function to make reloads for an address in operand NOP or
3398 check its correctness (If CHECK_ONLY_P is true). The supported
3399 cases are:
3401 1) an address that existed before LRA started, at which point it
3402 must have been valid. These addresses are subject to elimination
3403 and may have become invalid due to the elimination offset being out
3404 of range.
3406 2) an address created by forcing a constant to memory
3407 (force_const_to_mem). The initial form of these addresses might
3408 not be valid, and it is this function's job to make them valid.
3410 3) a frame address formed from a register and a (possibly zero)
3411 constant offset. As above, these addresses might not be valid and
3412 this function must make them so.
3414 Add reloads to the lists *BEFORE and *AFTER. We might need to add
3415 reloads to *AFTER because of inc/dec, {pre, post} modify in the
3416 address. Return true for any RTL change.
3418 The function is a helper function which does not produce all
3419 transformations (when CHECK_ONLY_P is false) which can be
3420 necessary. It does just basic steps. To do all necessary
3421 transformations use function process_address. */
3422 static bool
3423 process_address_1 (int nop, bool check_only_p,
3424 rtx_insn **before, rtx_insn **after)
3426 struct address_info ad;
3427 rtx new_reg;
3428 HOST_WIDE_INT scale;
3429 rtx op = *curr_id->operand_loc[nop];
3430 const char *constraint = curr_static_id->operand[nop].constraint;
3431 enum constraint_num cn = lookup_constraint (constraint);
3432 bool change_p = false;
3434 if (MEM_P (op)
3435 && GET_MODE (op) == BLKmode
3436 && GET_CODE (XEXP (op, 0)) == SCRATCH)
3437 return false;
3439 if (insn_extra_address_constraint (cn)
3440 /* When we find an asm operand with an address constraint that
3441 doesn't satisfy address_operand to begin with, we clear
3442 is_address, so that we don't try to make a non-address fit.
3443 If the asm statement got this far, it's because other
3444 constraints are available, and we'll use them, disregarding
3445 the unsatisfiable address ones. */
3446 && curr_static_id->operand[nop].is_address)
3447 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
3448 /* Do not attempt to decompose arbitrary addresses generated by combine
3449 for asm operands with loose constraints, e.g 'X'. */
3450 else if (MEM_P (op)
3451 && !(INSN_CODE (curr_insn) < 0
3452 && get_constraint_type (cn) == CT_FIXED_FORM
3453 && constraint_satisfied_p (op, cn)))
3454 decompose_mem_address (&ad, op);
3455 else if (GET_CODE (op) == SUBREG
3456 && MEM_P (SUBREG_REG (op)))
3457 decompose_mem_address (&ad, SUBREG_REG (op));
3458 else
3459 return false;
3460 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
3461 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
3462 when INDEX_REG_CLASS is a single register class. */
3463 if (ad.base_term != NULL
3464 && ad.index_term != NULL
3465 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
3466 && REG_P (*ad.base_term)
3467 && REG_P (*ad.index_term)
3468 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
3469 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
3471 std::swap (ad.base, ad.index);
3472 std::swap (ad.base_term, ad.index_term);
3474 if (! check_only_p)
3475 change_p = equiv_address_substitution (&ad);
3476 if (ad.base_term != NULL
3477 && (process_addr_reg
3478 (ad.base_term, check_only_p, before,
3479 (ad.autoinc_p
3480 && !(REG_P (*ad.base_term)
3481 && find_regno_note (curr_insn, REG_DEAD,
3482 REGNO (*ad.base_term)) != NULL_RTX)
3483 ? after : NULL),
3484 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3485 get_index_code (&ad)))))
3487 change_p = true;
3488 if (ad.base_term2 != NULL)
3489 *ad.base_term2 = *ad.base_term;
3491 if (ad.index_term != NULL
3492 && process_addr_reg (ad.index_term, check_only_p,
3493 before, NULL, INDEX_REG_CLASS))
3494 change_p = true;
3496 /* Target hooks sometimes don't treat extra-constraint addresses as
3497 legitimate address_operands, so handle them specially. */
3498 if (insn_extra_address_constraint (cn)
3499 && satisfies_address_constraint_p (&ad, cn))
3500 return change_p;
3502 if (check_only_p)
3503 return change_p;
3505 /* There are three cases where the shape of *AD.INNER may now be invalid:
3507 1) the original address was valid, but either elimination or
3508 equiv_address_substitution was applied and that made
3509 the address invalid.
3511 2) the address is an invalid symbolic address created by
3512 force_const_to_mem.
3514 3) the address is a frame address with an invalid offset.
3516 4) the address is a frame address with an invalid base.
3518 All these cases involve a non-autoinc address, so there is no
3519 point revalidating other types. */
3520 if (ad.autoinc_p || valid_address_p (op, &ad, cn))
3521 return change_p;
3523 /* Any index existed before LRA started, so we can assume that the
3524 presence and shape of the index is valid. */
3525 push_to_sequence (*before);
3526 lra_assert (ad.disp == ad.disp_term);
3527 if (ad.base == NULL)
3529 if (ad.index == NULL)
3531 rtx_insn *insn;
3532 rtx_insn *last = get_last_insn ();
3533 int code = -1;
3534 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3535 SCRATCH, SCRATCH);
3536 rtx addr = *ad.inner;
3538 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3539 if (HAVE_lo_sum)
3541 /* addr => lo_sum (new_base, addr), case (2) above. */
3542 insn = emit_insn (gen_rtx_SET
3543 (new_reg,
3544 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
3545 code = recog_memoized (insn);
3546 if (code >= 0)
3548 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
3549 if (!valid_address_p (op, &ad, cn))
3551 /* Try to put lo_sum into register. */
3552 insn = emit_insn (gen_rtx_SET
3553 (new_reg,
3554 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
3555 code = recog_memoized (insn);
3556 if (code >= 0)
3558 *ad.inner = new_reg;
3559 if (!valid_address_p (op, &ad, cn))
3561 *ad.inner = addr;
3562 code = -1;
3568 if (code < 0)
3569 delete_insns_since (last);
3572 if (code < 0)
3574 /* addr => new_base, case (2) above. */
3575 lra_emit_move (new_reg, addr);
3577 for (insn = last == NULL_RTX ? get_insns () : NEXT_INSN (last);
3578 insn != NULL_RTX;
3579 insn = NEXT_INSN (insn))
3580 if (recog_memoized (insn) < 0)
3581 break;
3582 if (insn != NULL_RTX)
3584 /* Do nothing if we cannot generate right insns.
3585 This is analogous to reload pass behavior. */
3586 delete_insns_since (last);
3587 end_sequence ();
3588 return false;
3590 *ad.inner = new_reg;
3593 else
3595 /* index * scale + disp => new base + index * scale,
3596 case (1) above. */
3597 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3598 GET_CODE (*ad.index));
3600 lra_assert (INDEX_REG_CLASS != NO_REGS);
3601 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3602 lra_emit_move (new_reg, *ad.disp);
3603 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3604 new_reg, *ad.index);
3607 else if (ad.index == NULL)
3609 int regno;
3610 enum reg_class cl;
3611 rtx set;
3612 rtx_insn *insns, *last_insn;
3613 /* Try to reload base into register only if the base is invalid
3614 for the address but with valid offset, case (4) above. */
3615 start_sequence ();
3616 new_reg = base_to_reg (&ad);
3618 /* base + disp => new base, cases (1) and (3) above. */
3619 /* Another option would be to reload the displacement into an
3620 index register. However, postreload has code to optimize
3621 address reloads that have the same base and different
3622 displacements, so reloading into an index register would
3623 not necessarily be a win. */
3624 if (new_reg == NULL_RTX)
3626 /* See if the target can split the displacement into a
3627 legitimate new displacement from a local anchor. */
3628 gcc_assert (ad.disp == ad.disp_term);
3629 poly_int64 orig_offset;
3630 rtx offset1, offset2;
3631 if (poly_int_rtx_p (*ad.disp, &orig_offset)
3632 && targetm.legitimize_address_displacement (&offset1, &offset2,
3633 orig_offset,
3634 ad.mode))
3636 new_reg = base_plus_disp_to_reg (&ad, offset1);
3637 new_reg = gen_rtx_PLUS (GET_MODE (new_reg), new_reg, offset2);
3639 else
3640 new_reg = base_plus_disp_to_reg (&ad, *ad.disp);
3642 insns = get_insns ();
3643 last_insn = get_last_insn ();
3644 /* If we generated at least two insns, try last insn source as
3645 an address. If we succeed, we generate one less insn. */
3646 if (REG_P (new_reg)
3647 && last_insn != insns
3648 && (set = single_set (last_insn)) != NULL_RTX
3649 && GET_CODE (SET_SRC (set)) == PLUS
3650 && REG_P (XEXP (SET_SRC (set), 0))
3651 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3653 *ad.inner = SET_SRC (set);
3654 if (valid_address_p (op, &ad, cn))
3656 *ad.base_term = XEXP (SET_SRC (set), 0);
3657 *ad.disp_term = XEXP (SET_SRC (set), 1);
3658 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3659 get_index_code (&ad));
3660 regno = REGNO (*ad.base_term);
3661 if (regno >= FIRST_PSEUDO_REGISTER
3662 && cl != lra_get_allocno_class (regno))
3663 lra_change_class (regno, cl, " Change to", true);
3664 new_reg = SET_SRC (set);
3665 delete_insns_since (PREV_INSN (last_insn));
3668 end_sequence ();
3669 emit_insn (insns);
3670 *ad.inner = new_reg;
3672 else if (ad.disp_term != NULL)
3674 /* base + scale * index + disp => new base + scale * index,
3675 case (1) above. */
3676 gcc_assert (ad.disp == ad.disp_term);
3677 new_reg = base_plus_disp_to_reg (&ad, *ad.disp);
3678 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3679 new_reg, *ad.index);
3681 else if ((scale = get_index_scale (&ad)) == 1)
3683 /* The last transformation to one reg will be made in
3684 curr_insn_transform function. */
3685 end_sequence ();
3686 return false;
3688 else if (scale != 0)
3690 /* base + scale * index => base + new_reg,
3691 case (1) above.
3692 Index part of address may become invalid. For example, we
3693 changed pseudo on the equivalent memory and a subreg of the
3694 pseudo onto the memory of different mode for which the scale is
3695 prohibitted. */
3696 new_reg = index_part_to_reg (&ad);
3697 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3698 *ad.base_term, new_reg);
3700 else
3702 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3703 SCRATCH, SCRATCH);
3704 rtx addr = *ad.inner;
3706 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3707 /* addr => new_base. */
3708 lra_emit_move (new_reg, addr);
3709 *ad.inner = new_reg;
3711 *before = get_insns ();
3712 end_sequence ();
3713 return true;
3716 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3717 Use process_address_1 as a helper function. Return true for any
3718 RTL changes.
3720 If CHECK_ONLY_P is true, just check address correctness. Return
3721 false if the address correct. */
3722 static bool
3723 process_address (int nop, bool check_only_p,
3724 rtx_insn **before, rtx_insn **after)
3726 bool res = false;
3728 while (process_address_1 (nop, check_only_p, before, after))
3730 if (check_only_p)
3731 return true;
3732 res = true;
3734 return res;
3737 /* Emit insns to reload VALUE into a new register. VALUE is an
3738 auto-increment or auto-decrement RTX whose operand is a register or
3739 memory location; so reloading involves incrementing that location.
3740 IN is either identical to VALUE, or some cheaper place to reload
3741 value being incremented/decremented from.
3743 INC_AMOUNT is the number to increment or decrement by (always
3744 positive and ignored for POST_MODIFY/PRE_MODIFY).
3746 Return pseudo containing the result. */
3747 static rtx
3748 emit_inc (enum reg_class new_rclass, rtx in, rtx value, poly_int64 inc_amount)
3750 /* REG or MEM to be copied and incremented. */
3751 rtx incloc = XEXP (value, 0);
3752 /* Nonzero if increment after copying. */
3753 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3754 || GET_CODE (value) == POST_MODIFY);
3755 rtx_insn *last;
3756 rtx inc;
3757 rtx_insn *add_insn;
3758 int code;
3759 rtx real_in = in == value ? incloc : in;
3760 rtx result;
3761 bool plus_p = true;
3763 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3765 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3766 || GET_CODE (XEXP (value, 1)) == MINUS);
3767 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3768 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3769 inc = XEXP (XEXP (value, 1), 1);
3771 else
3773 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3774 inc_amount = -inc_amount;
3776 inc = gen_int_mode (inc_amount, GET_MODE (value));
3779 if (! post && REG_P (incloc))
3780 result = incloc;
3781 else
3782 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3783 "INC/DEC result");
3785 if (real_in != result)
3787 /* First copy the location to the result register. */
3788 lra_assert (REG_P (result));
3789 emit_insn (gen_move_insn (result, real_in));
3792 /* We suppose that there are insns to add/sub with the constant
3793 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3794 old reload worked with this assumption. If the assumption
3795 becomes wrong, we should use approach in function
3796 base_plus_disp_to_reg. */
3797 if (in == value)
3799 /* See if we can directly increment INCLOC. */
3800 last = get_last_insn ();
3801 add_insn = emit_insn (plus_p
3802 ? gen_add2_insn (incloc, inc)
3803 : gen_sub2_insn (incloc, inc));
3805 code = recog_memoized (add_insn);
3806 if (code >= 0)
3808 if (! post && result != incloc)
3809 emit_insn (gen_move_insn (result, incloc));
3810 return result;
3812 delete_insns_since (last);
3815 /* If couldn't do the increment directly, must increment in RESULT.
3816 The way we do this depends on whether this is pre- or
3817 post-increment. For pre-increment, copy INCLOC to the reload
3818 register, increment it there, then save back. */
3819 if (! post)
3821 if (real_in != result)
3822 emit_insn (gen_move_insn (result, real_in));
3823 if (plus_p)
3824 emit_insn (gen_add2_insn (result, inc));
3825 else
3826 emit_insn (gen_sub2_insn (result, inc));
3827 if (result != incloc)
3828 emit_insn (gen_move_insn (incloc, result));
3830 else
3832 /* Post-increment.
3834 Because this might be a jump insn or a compare, and because
3835 RESULT may not be available after the insn in an input
3836 reload, we must do the incrementing before the insn being
3837 reloaded for.
3839 We have already copied IN to RESULT. Increment the copy in
3840 RESULT, save that back, then decrement RESULT so it has
3841 the original value. */
3842 if (plus_p)
3843 emit_insn (gen_add2_insn (result, inc));
3844 else
3845 emit_insn (gen_sub2_insn (result, inc));
3846 emit_insn (gen_move_insn (incloc, result));
3847 /* Restore non-modified value for the result. We prefer this
3848 way because it does not require an additional hard
3849 register. */
3850 if (plus_p)
3852 poly_int64 offset;
3853 if (poly_int_rtx_p (inc, &offset))
3854 emit_insn (gen_add2_insn (result,
3855 gen_int_mode (-offset,
3856 GET_MODE (result))));
3857 else
3858 emit_insn (gen_sub2_insn (result, inc));
3860 else
3861 emit_insn (gen_add2_insn (result, inc));
3863 return result;
3866 /* Return true if the current move insn does not need processing as we
3867 already know that it satisfies its constraints. */
3868 static bool
3869 simple_move_p (void)
3871 rtx dest, src;
3872 enum reg_class dclass, sclass;
3874 lra_assert (curr_insn_set != NULL_RTX);
3875 dest = SET_DEST (curr_insn_set);
3876 src = SET_SRC (curr_insn_set);
3878 /* If the instruction has multiple sets we need to process it even if it
3879 is single_set. This can happen if one or more of the SETs are dead.
3880 See PR73650. */
3881 if (multiple_sets (curr_insn))
3882 return false;
3884 return ((dclass = get_op_class (dest)) != NO_REGS
3885 && (sclass = get_op_class (src)) != NO_REGS
3886 /* The backend guarantees that register moves of cost 2
3887 never need reloads. */
3888 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3891 /* Swap operands NOP and NOP + 1. */
3892 static inline void
3893 swap_operands (int nop)
3895 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3896 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3897 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3898 std::swap (equiv_substition_p[nop], equiv_substition_p[nop + 1]);
3899 /* Swap the duplicates too. */
3900 lra_update_dup (curr_id, nop);
3901 lra_update_dup (curr_id, nop + 1);
3904 /* Main entry point of the constraint code: search the body of the
3905 current insn to choose the best alternative. It is mimicking insn
3906 alternative cost calculation model of former reload pass. That is
3907 because machine descriptions were written to use this model. This
3908 model can be changed in future. Make commutative operand exchange
3909 if it is chosen.
3911 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3912 constraints. Return true if any change happened during function
3913 call.
3915 If CHECK_ONLY_P is true then don't do any transformation. Just
3916 check that the insn satisfies all constraints. If the insn does
3917 not satisfy any constraint, return true. */
3918 static bool
3919 curr_insn_transform (bool check_only_p)
3921 int i, j, k;
3922 int n_operands;
3923 int n_alternatives;
3924 int n_outputs;
3925 int commutative;
3926 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3927 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3928 signed char outputs[MAX_RECOG_OPERANDS + 1];
3929 rtx_insn *before, *after;
3930 bool alt_p = false;
3931 /* Flag that the insn has been changed through a transformation. */
3932 bool change_p;
3933 bool sec_mem_p;
3934 bool use_sec_mem_p;
3935 int max_regno_before;
3936 int reused_alternative_num;
3938 curr_insn_set = single_set (curr_insn);
3939 if (curr_insn_set != NULL_RTX && simple_move_p ())
3941 /* We assume that the corresponding insn alternative has no
3942 earlier clobbers. If it is not the case, don't define move
3943 cost equal to 2 for the corresponding register classes. */
3944 lra_set_used_insn_alternative (curr_insn, LRA_NON_CLOBBERED_ALT);
3945 return false;
3948 no_input_reloads_p = no_output_reloads_p = false;
3949 goal_alt_number = -1;
3950 change_p = sec_mem_p = false;
3951 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3952 reloads; neither are insns that SET cc0. Insns that use CC0 are
3953 not allowed to have any input reloads. */
3954 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3955 no_output_reloads_p = true;
3957 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3958 no_input_reloads_p = true;
3959 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3960 no_output_reloads_p = true;
3962 n_operands = curr_static_id->n_operands;
3963 n_alternatives = curr_static_id->n_alternatives;
3965 /* Just return "no reloads" if insn has no operands with
3966 constraints. */
3967 if (n_operands == 0 || n_alternatives == 0)
3968 return false;
3970 max_regno_before = max_reg_num ();
3972 for (i = 0; i < n_operands; i++)
3974 goal_alt_matched[i][0] = -1;
3975 goal_alt_matches[i] = -1;
3978 commutative = curr_static_id->commutative;
3980 /* Now see what we need for pseudos that didn't get hard regs or got
3981 the wrong kind of hard reg. For this, we must consider all the
3982 operands together against the register constraints. */
3984 best_losers = best_overall = INT_MAX;
3985 best_reload_sum = 0;
3987 curr_swapped = false;
3988 goal_alt_swapped = false;
3990 if (! check_only_p)
3991 /* Make equivalence substitution and memory subreg elimination
3992 before address processing because an address legitimacy can
3993 depend on memory mode. */
3994 for (i = 0; i < n_operands; i++)
3996 rtx op, subst, old;
3997 bool op_change_p = false;
3999 if (curr_static_id->operand[i].is_operator)
4000 continue;
4002 old = op = *curr_id->operand_loc[i];
4003 if (GET_CODE (old) == SUBREG)
4004 old = SUBREG_REG (old);
4005 subst = get_equiv_with_elimination (old, curr_insn);
4006 original_subreg_reg_mode[i] = VOIDmode;
4007 equiv_substition_p[i] = false;
4008 if (subst != old)
4010 equiv_substition_p[i] = true;
4011 subst = copy_rtx (subst);
4012 lra_assert (REG_P (old));
4013 if (GET_CODE (op) != SUBREG)
4014 *curr_id->operand_loc[i] = subst;
4015 else
4017 SUBREG_REG (op) = subst;
4018 if (GET_MODE (subst) == VOIDmode)
4019 original_subreg_reg_mode[i] = GET_MODE (old);
4021 if (lra_dump_file != NULL)
4023 fprintf (lra_dump_file,
4024 "Changing pseudo %d in operand %i of insn %u on equiv ",
4025 REGNO (old), i, INSN_UID (curr_insn));
4026 dump_value_slim (lra_dump_file, subst, 1);
4027 fprintf (lra_dump_file, "\n");
4029 op_change_p = change_p = true;
4031 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
4033 change_p = true;
4034 lra_update_dup (curr_id, i);
4038 /* Reload address registers and displacements. We do it before
4039 finding an alternative because of memory constraints. */
4040 before = after = NULL;
4041 for (i = 0; i < n_operands; i++)
4042 if (! curr_static_id->operand[i].is_operator
4043 && process_address (i, check_only_p, &before, &after))
4045 if (check_only_p)
4046 return true;
4047 change_p = true;
4048 lra_update_dup (curr_id, i);
4051 if (change_p)
4052 /* If we've changed the instruction then any alternative that
4053 we chose previously may no longer be valid. */
4054 lra_set_used_insn_alternative (curr_insn, LRA_UNKNOWN_ALT);
4056 if (! check_only_p && curr_insn_set != NULL_RTX
4057 && check_and_process_move (&change_p, &sec_mem_p))
4058 return change_p;
4060 try_swapped:
4062 reused_alternative_num = check_only_p ? LRA_UNKNOWN_ALT : curr_id->used_insn_alternative;
4063 if (lra_dump_file != NULL && reused_alternative_num >= 0)
4064 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
4065 reused_alternative_num, INSN_UID (curr_insn));
4067 if (process_alt_operands (reused_alternative_num))
4068 alt_p = true;
4070 if (check_only_p)
4071 return ! alt_p || best_losers != 0;
4073 /* If insn is commutative (it's safe to exchange a certain pair of
4074 operands) then we need to try each alternative twice, the second
4075 time matching those two operands as if we had exchanged them. To
4076 do this, really exchange them in operands.
4078 If we have just tried the alternatives the second time, return
4079 operands to normal and drop through. */
4081 if (reused_alternative_num < 0 && commutative >= 0)
4083 curr_swapped = !curr_swapped;
4084 if (curr_swapped)
4086 swap_operands (commutative);
4087 goto try_swapped;
4089 else
4090 swap_operands (commutative);
4093 if (! alt_p && ! sec_mem_p)
4095 /* No alternative works with reloads?? */
4096 if (INSN_CODE (curr_insn) >= 0)
4097 fatal_insn ("unable to generate reloads for:", curr_insn);
4098 error_for_asm (curr_insn,
4099 "inconsistent operand constraints in an %<asm%>");
4100 lra_asm_error_p = true;
4101 /* Avoid further trouble with this insn. Don't generate use
4102 pattern here as we could use the insn SP offset. */
4103 lra_set_insn_deleted (curr_insn);
4104 return true;
4107 /* If the best alternative is with operands 1 and 2 swapped, swap
4108 them. Update the operand numbers of any reloads already
4109 pushed. */
4111 if (goal_alt_swapped)
4113 if (lra_dump_file != NULL)
4114 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
4115 INSN_UID (curr_insn));
4117 /* Swap the duplicates too. */
4118 swap_operands (commutative);
4119 change_p = true;
4122 /* Some targets' TARGET_SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
4123 too conservatively. So we use the secondary memory only if there
4124 is no any alternative without reloads. */
4125 use_sec_mem_p = false;
4126 if (! alt_p)
4127 use_sec_mem_p = true;
4128 else if (sec_mem_p)
4130 for (i = 0; i < n_operands; i++)
4131 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
4132 break;
4133 use_sec_mem_p = i < n_operands;
4136 if (use_sec_mem_p)
4138 int in = -1, out = -1;
4139 rtx new_reg, src, dest, rld;
4140 machine_mode sec_mode, rld_mode;
4142 lra_assert (curr_insn_set != NULL_RTX && sec_mem_p);
4143 dest = SET_DEST (curr_insn_set);
4144 src = SET_SRC (curr_insn_set);
4145 for (i = 0; i < n_operands; i++)
4146 if (*curr_id->operand_loc[i] == dest)
4147 out = i;
4148 else if (*curr_id->operand_loc[i] == src)
4149 in = i;
4150 for (i = 0; i < curr_static_id->n_dups; i++)
4151 if (out < 0 && *curr_id->dup_loc[i] == dest)
4152 out = curr_static_id->dup_num[i];
4153 else if (in < 0 && *curr_id->dup_loc[i] == src)
4154 in = curr_static_id->dup_num[i];
4155 lra_assert (out >= 0 && in >= 0
4156 && curr_static_id->operand[out].type == OP_OUT
4157 && curr_static_id->operand[in].type == OP_IN);
4158 rld = partial_subreg_p (GET_MODE (src), GET_MODE (dest)) ? src : dest;
4159 rld_mode = GET_MODE (rld);
4160 sec_mode = targetm.secondary_memory_needed_mode (rld_mode);
4161 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
4162 NO_REGS, "secondary");
4163 /* If the mode is changed, it should be wider. */
4164 lra_assert (!partial_subreg_p (sec_mode, rld_mode));
4165 if (sec_mode != rld_mode)
4167 /* If the target says specifically to use another mode for
4168 secondary memory moves we cannot reuse the original
4169 insn. */
4170 after = emit_spill_move (false, new_reg, dest);
4171 lra_process_new_insns (curr_insn, NULL, after,
4172 "Inserting the sec. move");
4173 /* We may have non null BEFORE here (e.g. after address
4174 processing. */
4175 push_to_sequence (before);
4176 before = emit_spill_move (true, new_reg, src);
4177 emit_insn (before);
4178 before = get_insns ();
4179 end_sequence ();
4180 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
4181 lra_set_insn_deleted (curr_insn);
4183 else if (dest == rld)
4185 *curr_id->operand_loc[out] = new_reg;
4186 lra_update_dup (curr_id, out);
4187 after = emit_spill_move (false, new_reg, dest);
4188 lra_process_new_insns (curr_insn, NULL, after,
4189 "Inserting the sec. move");
4191 else
4193 *curr_id->operand_loc[in] = new_reg;
4194 lra_update_dup (curr_id, in);
4195 /* See comments above. */
4196 push_to_sequence (before);
4197 before = emit_spill_move (true, new_reg, src);
4198 emit_insn (before);
4199 before = get_insns ();
4200 end_sequence ();
4201 lra_process_new_insns (curr_insn, before, NULL,
4202 "Inserting the sec. move");
4204 lra_update_insn_regno_info (curr_insn);
4205 return true;
4208 lra_assert (goal_alt_number >= 0);
4209 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
4211 if (lra_dump_file != NULL)
4213 const char *p;
4215 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
4216 goal_alt_number, INSN_UID (curr_insn));
4217 for (i = 0; i < n_operands; i++)
4219 p = (curr_static_id->operand_alternative
4220 [goal_alt_number * n_operands + i].constraint);
4221 if (*p == '\0')
4222 continue;
4223 fprintf (lra_dump_file, " (%d) ", i);
4224 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
4225 fputc (*p, lra_dump_file);
4227 if (INSN_CODE (curr_insn) >= 0
4228 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
4229 fprintf (lra_dump_file, " {%s}", p);
4230 if (maybe_ne (curr_id->sp_offset, 0))
4232 fprintf (lra_dump_file, " (sp_off=");
4233 print_dec (curr_id->sp_offset, lra_dump_file);
4234 fprintf (lra_dump_file, ")");
4236 fprintf (lra_dump_file, "\n");
4239 /* Right now, for any pair of operands I and J that are required to
4240 match, with J < I, goal_alt_matches[I] is J. Add I to
4241 goal_alt_matched[J]. */
4243 for (i = 0; i < n_operands; i++)
4244 if ((j = goal_alt_matches[i]) >= 0)
4246 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
4248 /* We allow matching one output operand and several input
4249 operands. */
4250 lra_assert (k == 0
4251 || (curr_static_id->operand[j].type == OP_OUT
4252 && curr_static_id->operand[i].type == OP_IN
4253 && (curr_static_id->operand
4254 [goal_alt_matched[j][0]].type == OP_IN)));
4255 goal_alt_matched[j][k] = i;
4256 goal_alt_matched[j][k + 1] = -1;
4259 for (i = 0; i < n_operands; i++)
4260 goal_alt_win[i] |= goal_alt_match_win[i];
4262 /* Any constants that aren't allowed and can't be reloaded into
4263 registers are here changed into memory references. */
4264 for (i = 0; i < n_operands; i++)
4265 if (goal_alt_win[i])
4267 int regno;
4268 enum reg_class new_class;
4269 rtx reg = *curr_id->operand_loc[i];
4271 if (GET_CODE (reg) == SUBREG)
4272 reg = SUBREG_REG (reg);
4274 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
4276 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
4278 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
4280 lra_assert (ok_p);
4281 lra_change_class (regno, new_class, " Change to", true);
4285 else
4287 const char *constraint;
4288 char c;
4289 rtx op = *curr_id->operand_loc[i];
4290 rtx subreg = NULL_RTX;
4291 machine_mode mode = curr_operand_mode[i];
4293 if (GET_CODE (op) == SUBREG)
4295 subreg = op;
4296 op = SUBREG_REG (op);
4297 mode = GET_MODE (op);
4300 if (CONST_POOL_OK_P (mode, op)
4301 && ((targetm.preferred_reload_class
4302 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
4303 || no_input_reloads_p))
4305 rtx tem = force_const_mem (mode, op);
4307 change_p = true;
4308 if (subreg != NULL_RTX)
4309 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
4311 *curr_id->operand_loc[i] = tem;
4312 lra_update_dup (curr_id, i);
4313 process_address (i, false, &before, &after);
4315 /* If the alternative accepts constant pool refs directly
4316 there will be no reload needed at all. */
4317 if (subreg != NULL_RTX)
4318 continue;
4319 /* Skip alternatives before the one requested. */
4320 constraint = (curr_static_id->operand_alternative
4321 [goal_alt_number * n_operands + i].constraint);
4322 for (;
4323 (c = *constraint) && c != ',' && c != '#';
4324 constraint += CONSTRAINT_LEN (c, constraint))
4326 enum constraint_num cn = lookup_constraint (constraint);
4327 if ((insn_extra_memory_constraint (cn)
4328 || insn_extra_special_memory_constraint (cn))
4329 && satisfies_memory_constraint_p (tem, cn))
4330 break;
4332 if (c == '\0' || c == ',' || c == '#')
4333 continue;
4335 goal_alt_win[i] = true;
4339 n_outputs = 0;
4340 outputs[0] = -1;
4341 for (i = 0; i < n_operands; i++)
4343 int regno;
4344 bool optional_p = false;
4345 rtx old, new_reg;
4346 rtx op = *curr_id->operand_loc[i];
4348 if (goal_alt_win[i])
4350 if (goal_alt[i] == NO_REGS
4351 && REG_P (op)
4352 /* When we assign NO_REGS it means that we will not
4353 assign a hard register to the scratch pseudo by
4354 assigment pass and the scratch pseudo will be
4355 spilled. Spilled scratch pseudos are transformed
4356 back to scratches at the LRA end. */
4357 && lra_former_scratch_operand_p (curr_insn, i)
4358 && lra_former_scratch_p (REGNO (op)))
4360 int regno = REGNO (op);
4361 lra_change_class (regno, NO_REGS, " Change to", true);
4362 if (lra_get_regno_hard_regno (regno) >= 0)
4363 /* We don't have to mark all insn affected by the
4364 spilled pseudo as there is only one such insn, the
4365 current one. */
4366 reg_renumber[regno] = -1;
4367 lra_assert (bitmap_single_bit_set_p
4368 (&lra_reg_info[REGNO (op)].insn_bitmap));
4370 /* We can do an optional reload. If the pseudo got a hard
4371 reg, we might improve the code through inheritance. If
4372 it does not get a hard register we coalesce memory/memory
4373 moves later. Ignore move insns to avoid cycling. */
4374 if (! lra_simple_p
4375 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
4376 && goal_alt[i] != NO_REGS && REG_P (op)
4377 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
4378 && regno < new_regno_start
4379 && ! lra_former_scratch_p (regno)
4380 && reg_renumber[regno] < 0
4381 /* Check that the optional reload pseudo will be able to
4382 hold given mode value. */
4383 && ! (prohibited_class_reg_set_mode_p
4384 (goal_alt[i], reg_class_contents[goal_alt[i]],
4385 PSEUDO_REGNO_MODE (regno)))
4386 && (curr_insn_set == NULL_RTX
4387 || !((REG_P (SET_SRC (curr_insn_set))
4388 || MEM_P (SET_SRC (curr_insn_set))
4389 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
4390 && (REG_P (SET_DEST (curr_insn_set))
4391 || MEM_P (SET_DEST (curr_insn_set))
4392 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
4393 optional_p = true;
4394 else if (goal_alt_matched[i][0] != -1
4395 && curr_static_id->operand[i].type == OP_OUT
4396 && (curr_static_id->operand_alternative
4397 [goal_alt_number * n_operands + i].earlyclobber)
4398 && REG_P (op))
4400 for (j = 0; goal_alt_matched[i][j] != -1; j++)
4402 rtx op2 = *curr_id->operand_loc[goal_alt_matched[i][j]];
4404 if (REG_P (op2) && REGNO (op) != REGNO (op2))
4405 break;
4407 if (goal_alt_matched[i][j] != -1)
4409 /* Generate reloads for different output and matched
4410 input registers. This is the easiest way to avoid
4411 creation of non-existing register conflicts in
4412 lra-lives.c. */
4413 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i], &before,
4414 &after, TRUE);
4415 outputs[n_outputs++] = i;
4416 outputs[n_outputs] = -1;
4418 continue;
4420 else
4421 continue;
4424 /* Operands that match previous ones have already been handled. */
4425 if (goal_alt_matches[i] >= 0)
4426 continue;
4428 /* We should not have an operand with a non-offsettable address
4429 appearing where an offsettable address will do. It also may
4430 be a case when the address should be special in other words
4431 not a general one (e.g. it needs no index reg). */
4432 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
4434 enum reg_class rclass;
4435 rtx *loc = &XEXP (op, 0);
4436 enum rtx_code code = GET_CODE (*loc);
4438 push_to_sequence (before);
4439 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
4440 MEM, SCRATCH);
4441 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
4442 new_reg = emit_inc (rclass, *loc, *loc,
4443 /* This value does not matter for MODIFY. */
4444 GET_MODE_SIZE (GET_MODE (op)));
4445 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
4446 "offsetable address", &new_reg))
4448 rtx addr = *loc;
4449 enum rtx_code code = GET_CODE (addr);
4450 bool align_p = false;
4452 if (code == AND && CONST_INT_P (XEXP (addr, 1)))
4454 /* (and ... (const_int -X)) is used to align to X bytes. */
4455 align_p = true;
4456 addr = XEXP (*loc, 0);
4458 else
4459 addr = canonicalize_reload_addr (addr);
4461 lra_emit_move (new_reg, addr);
4462 if (align_p)
4463 emit_move_insn (new_reg, gen_rtx_AND (GET_MODE (new_reg), new_reg, XEXP (*loc, 1)));
4465 before = get_insns ();
4466 end_sequence ();
4467 *loc = new_reg;
4468 lra_update_dup (curr_id, i);
4470 else if (goal_alt_matched[i][0] == -1)
4472 machine_mode mode;
4473 rtx reg, *loc;
4474 int hard_regno;
4475 enum op_type type = curr_static_id->operand[i].type;
4477 loc = curr_id->operand_loc[i];
4478 mode = curr_operand_mode[i];
4479 if (GET_CODE (*loc) == SUBREG)
4481 reg = SUBREG_REG (*loc);
4482 poly_int64 byte = SUBREG_BYTE (*loc);
4483 if (REG_P (reg)
4484 /* Strict_low_part requires reloading the register and not
4485 just the subreg. Likewise for a strict subreg no wider
4486 than a word for WORD_REGISTER_OPERATIONS targets. */
4487 && (curr_static_id->operand[i].strict_low
4488 || (!paradoxical_subreg_p (mode, GET_MODE (reg))
4489 && (hard_regno
4490 = get_try_hard_regno (REGNO (reg))) >= 0
4491 && (simplify_subreg_regno
4492 (hard_regno,
4493 GET_MODE (reg), byte, mode) < 0)
4494 && (goal_alt[i] == NO_REGS
4495 || (simplify_subreg_regno
4496 (ira_class_hard_regs[goal_alt[i]][0],
4497 GET_MODE (reg), byte, mode) >= 0)))
4498 || (partial_subreg_p (mode, GET_MODE (reg))
4499 && known_le (GET_MODE_SIZE (GET_MODE (reg)),
4500 UNITS_PER_WORD)
4501 && WORD_REGISTER_OPERATIONS)))
4503 /* An OP_INOUT is required when reloading a subreg of a
4504 mode wider than a word to ensure that data beyond the
4505 word being reloaded is preserved. Also automatically
4506 ensure that strict_low_part reloads are made into
4507 OP_INOUT which should already be true from the backend
4508 constraints. */
4509 if (type == OP_OUT
4510 && (curr_static_id->operand[i].strict_low
4511 || read_modify_subreg_p (*loc)))
4512 type = OP_INOUT;
4513 loc = &SUBREG_REG (*loc);
4514 mode = GET_MODE (*loc);
4517 old = *loc;
4518 if (get_reload_reg (type, mode, old, goal_alt[i],
4519 loc != curr_id->operand_loc[i], "", &new_reg)
4520 && type != OP_OUT)
4522 push_to_sequence (before);
4523 lra_emit_move (new_reg, old);
4524 before = get_insns ();
4525 end_sequence ();
4527 *loc = new_reg;
4528 if (type != OP_IN
4529 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
4531 start_sequence ();
4532 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
4533 emit_insn (after);
4534 after = get_insns ();
4535 end_sequence ();
4536 *loc = new_reg;
4538 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
4539 if (goal_alt_dont_inherit_ops[j] == i)
4541 lra_set_regno_unique_value (REGNO (new_reg));
4542 break;
4544 lra_update_dup (curr_id, i);
4546 else if (curr_static_id->operand[i].type == OP_IN
4547 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4548 == OP_OUT
4549 || (curr_static_id->operand[goal_alt_matched[i][0]].type
4550 == OP_INOUT
4551 && (operands_match_p
4552 (*curr_id->operand_loc[i],
4553 *curr_id->operand_loc[goal_alt_matched[i][0]],
4554 -1)))))
4556 /* generate reloads for input and matched outputs. */
4557 match_inputs[0] = i;
4558 match_inputs[1] = -1;
4559 match_reload (goal_alt_matched[i][0], match_inputs, outputs,
4560 goal_alt[i], &before, &after,
4561 curr_static_id->operand_alternative
4562 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
4563 .earlyclobber);
4565 else if ((curr_static_id->operand[i].type == OP_OUT
4566 || (curr_static_id->operand[i].type == OP_INOUT
4567 && (operands_match_p
4568 (*curr_id->operand_loc[i],
4569 *curr_id->operand_loc[goal_alt_matched[i][0]],
4570 -1))))
4571 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4572 == OP_IN))
4573 /* Generate reloads for output and matched inputs. */
4574 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i], &before,
4575 &after, curr_static_id->operand_alternative
4576 [goal_alt_number * n_operands + i].earlyclobber);
4577 else if (curr_static_id->operand[i].type == OP_IN
4578 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4579 == OP_IN))
4581 /* Generate reloads for matched inputs. */
4582 match_inputs[0] = i;
4583 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
4584 match_inputs[j + 1] = k;
4585 match_inputs[j + 1] = -1;
4586 match_reload (-1, match_inputs, outputs, goal_alt[i], &before,
4587 &after, false);
4589 else
4590 /* We must generate code in any case when function
4591 process_alt_operands decides that it is possible. */
4592 gcc_unreachable ();
4594 /* Memorise processed outputs so that output remaining to be processed
4595 can avoid using the same register value (see match_reload). */
4596 if (curr_static_id->operand[i].type == OP_OUT)
4598 outputs[n_outputs++] = i;
4599 outputs[n_outputs] = -1;
4602 if (optional_p)
4604 rtx reg = op;
4606 lra_assert (REG_P (reg));
4607 regno = REGNO (reg);
4608 op = *curr_id->operand_loc[i]; /* Substitution. */
4609 if (GET_CODE (op) == SUBREG)
4610 op = SUBREG_REG (op);
4611 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
4612 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
4613 lra_reg_info[REGNO (op)].restore_rtx = reg;
4614 if (lra_dump_file != NULL)
4615 fprintf (lra_dump_file,
4616 " Making reload reg %d for reg %d optional\n",
4617 REGNO (op), regno);
4620 if (before != NULL_RTX || after != NULL_RTX
4621 || max_regno_before != max_reg_num ())
4622 change_p = true;
4623 if (change_p)
4625 lra_update_operator_dups (curr_id);
4626 /* Something changes -- process the insn. */
4627 lra_update_insn_regno_info (curr_insn);
4629 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
4630 return change_p;
4633 /* Return true if INSN satisfies all constraints. In other words, no
4634 reload insns are needed. */
4635 bool
4636 lra_constrain_insn (rtx_insn *insn)
4638 int saved_new_regno_start = new_regno_start;
4639 int saved_new_insn_uid_start = new_insn_uid_start;
4640 bool change_p;
4642 curr_insn = insn;
4643 curr_id = lra_get_insn_recog_data (curr_insn);
4644 curr_static_id = curr_id->insn_static_data;
4645 new_insn_uid_start = get_max_uid ();
4646 new_regno_start = max_reg_num ();
4647 change_p = curr_insn_transform (true);
4648 new_regno_start = saved_new_regno_start;
4649 new_insn_uid_start = saved_new_insn_uid_start;
4650 return ! change_p;
4653 /* Return true if X is in LIST. */
4654 static bool
4655 in_list_p (rtx x, rtx list)
4657 for (; list != NULL_RTX; list = XEXP (list, 1))
4658 if (XEXP (list, 0) == x)
4659 return true;
4660 return false;
4663 /* Return true if X contains an allocatable hard register (if
4664 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
4665 static bool
4666 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
4668 int i, j;
4669 const char *fmt;
4670 enum rtx_code code;
4672 code = GET_CODE (x);
4673 if (REG_P (x))
4675 int regno = REGNO (x);
4676 HARD_REG_SET alloc_regs;
4678 if (hard_reg_p)
4680 if (regno >= FIRST_PSEUDO_REGISTER)
4681 regno = lra_get_regno_hard_regno (regno);
4682 if (regno < 0)
4683 return false;
4684 alloc_regs = ~lra_no_alloc_regs;
4685 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
4687 else
4689 if (regno < FIRST_PSEUDO_REGISTER)
4690 return false;
4691 if (! spilled_p)
4692 return true;
4693 return lra_get_regno_hard_regno (regno) < 0;
4696 fmt = GET_RTX_FORMAT (code);
4697 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4699 if (fmt[i] == 'e')
4701 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4702 return true;
4704 else if (fmt[i] == 'E')
4706 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4707 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4708 return true;
4711 return false;
4714 /* Process all regs in location *LOC and change them on equivalent
4715 substitution. Return true if any change was done. */
4716 static bool
4717 loc_equivalence_change_p (rtx *loc)
4719 rtx subst, reg, x = *loc;
4720 bool result = false;
4721 enum rtx_code code = GET_CODE (x);
4722 const char *fmt;
4723 int i, j;
4725 if (code == SUBREG)
4727 reg = SUBREG_REG (x);
4728 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4729 && GET_MODE (subst) == VOIDmode)
4731 /* We cannot reload debug location. Simplify subreg here
4732 while we know the inner mode. */
4733 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4734 GET_MODE (reg), SUBREG_BYTE (x));
4735 return true;
4738 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4740 *loc = subst;
4741 return true;
4744 /* Scan all the operand sub-expressions. */
4745 fmt = GET_RTX_FORMAT (code);
4746 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4748 if (fmt[i] == 'e')
4749 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4750 else if (fmt[i] == 'E')
4751 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4752 result
4753 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4755 return result;
4758 /* Similar to loc_equivalence_change_p, but for use as
4759 simplify_replace_fn_rtx callback. DATA is insn for which the
4760 elimination is done. If it null we don't do the elimination. */
4761 static rtx
4762 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4764 if (!REG_P (loc))
4765 return NULL_RTX;
4767 rtx subst = (data == NULL
4768 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4769 if (subst != loc)
4770 return subst;
4772 return NULL_RTX;
4775 /* Maximum number of generated reload insns per an insn. It is for
4776 preventing this pass cycling in a bug case. */
4777 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4779 /* The current iteration number of this LRA pass. */
4780 int lra_constraint_iter;
4782 /* True if we should during assignment sub-pass check assignment
4783 correctness for all pseudos and spill some of them to correct
4784 conflicts. It can be necessary when we substitute equiv which
4785 needs checking register allocation correctness because the
4786 equivalent value contains allocatable hard registers, or when we
4787 restore multi-register pseudo, or when we change the insn code and
4788 its operand became INOUT operand when it was IN one before. */
4789 bool check_and_force_assignment_correctness_p;
4791 /* Return true if REGNO is referenced in more than one block. */
4792 static bool
4793 multi_block_pseudo_p (int regno)
4795 basic_block bb = NULL;
4796 unsigned int uid;
4797 bitmap_iterator bi;
4799 if (regno < FIRST_PSEUDO_REGISTER)
4800 return false;
4802 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4803 if (bb == NULL)
4804 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4805 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4806 return true;
4807 return false;
4810 /* Return true if LIST contains a deleted insn. */
4811 static bool
4812 contains_deleted_insn_p (rtx_insn_list *list)
4814 for (; list != NULL_RTX; list = list->next ())
4815 if (NOTE_P (list->insn ())
4816 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4817 return true;
4818 return false;
4821 /* Return true if X contains a pseudo dying in INSN. */
4822 static bool
4823 dead_pseudo_p (rtx x, rtx_insn *insn)
4825 int i, j;
4826 const char *fmt;
4827 enum rtx_code code;
4829 if (REG_P (x))
4830 return (insn != NULL_RTX
4831 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4832 code = GET_CODE (x);
4833 fmt = GET_RTX_FORMAT (code);
4834 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4836 if (fmt[i] == 'e')
4838 if (dead_pseudo_p (XEXP (x, i), insn))
4839 return true;
4841 else if (fmt[i] == 'E')
4843 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4844 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4845 return true;
4848 return false;
4851 /* Return true if INSN contains a dying pseudo in INSN right hand
4852 side. */
4853 static bool
4854 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4856 rtx set = single_set (insn);
4858 gcc_assert (set != NULL);
4859 return dead_pseudo_p (SET_SRC (set), insn);
4862 /* Return true if any init insn of REGNO contains a dying pseudo in
4863 insn right hand side. */
4864 static bool
4865 init_insn_rhs_dead_pseudo_p (int regno)
4867 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4869 if (insns == NULL)
4870 return false;
4871 for (; insns != NULL_RTX; insns = insns->next ())
4872 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4873 return true;
4874 return false;
4877 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4878 reverse only if we have one init insn with given REGNO as a
4879 source. */
4880 static bool
4881 reverse_equiv_p (int regno)
4883 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4884 rtx set;
4886 if (insns == NULL)
4887 return false;
4888 if (! INSN_P (insns->insn ())
4889 || insns->next () != NULL)
4890 return false;
4891 if ((set = single_set (insns->insn ())) == NULL_RTX)
4892 return false;
4893 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4896 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4897 call this function only for non-reverse equivalence. */
4898 static bool
4899 contains_reloaded_insn_p (int regno)
4901 rtx set;
4902 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4904 for (; list != NULL; list = list->next ())
4905 if ((set = single_set (list->insn ())) == NULL_RTX
4906 || ! REG_P (SET_DEST (set))
4907 || (int) REGNO (SET_DEST (set)) != regno)
4908 return true;
4909 return false;
4912 /* Entry function of LRA constraint pass. Return true if the
4913 constraint pass did change the code. */
4914 bool
4915 lra_constraints (bool first_p)
4917 bool changed_p;
4918 int i, hard_regno, new_insns_num;
4919 unsigned int min_len, new_min_len, uid;
4920 rtx set, x, reg, dest_reg;
4921 basic_block last_bb;
4922 bitmap_iterator bi;
4924 lra_constraint_iter++;
4925 if (lra_dump_file != NULL)
4926 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4927 lra_constraint_iter);
4928 changed_p = false;
4929 if (pic_offset_table_rtx
4930 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4931 check_and_force_assignment_correctness_p = true;
4932 else if (first_p)
4933 /* On the first iteration we should check IRA assignment
4934 correctness. In rare cases, the assignments can be wrong as
4935 early clobbers operands are ignored in IRA or usages of
4936 paradoxical sub-registers are not taken into account by
4937 IRA. */
4938 check_and_force_assignment_correctness_p = true;
4939 new_insn_uid_start = get_max_uid ();
4940 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4941 /* Mark used hard regs for target stack size calulations. */
4942 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4943 if (lra_reg_info[i].nrefs != 0
4944 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4946 int j, nregs;
4948 nregs = hard_regno_nregs (hard_regno, lra_reg_info[i].biggest_mode);
4949 for (j = 0; j < nregs; j++)
4950 df_set_regs_ever_live (hard_regno + j, true);
4952 /* Do elimination before the equivalence processing as we can spill
4953 some pseudos during elimination. */
4954 lra_eliminate (false, first_p);
4955 auto_bitmap equiv_insn_bitmap (&reg_obstack);
4956 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4957 if (lra_reg_info[i].nrefs != 0)
4959 ira_reg_equiv[i].profitable_p = true;
4960 reg = regno_reg_rtx[i];
4961 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4963 bool pseudo_p = contains_reg_p (x, false, false);
4965 /* After RTL transformation, we cannot guarantee that
4966 pseudo in the substitution was not reloaded which might
4967 make equivalence invalid. For example, in reverse
4968 equiv of p0
4970 p0 <- ...
4972 equiv_mem <- p0
4974 the memory address register was reloaded before the 2nd
4975 insn. */
4976 if ((! first_p && pseudo_p)
4977 /* We don't use DF for compilation speed sake. So it
4978 is problematic to update live info when we use an
4979 equivalence containing pseudos in more than one
4980 BB. */
4981 || (pseudo_p && multi_block_pseudo_p (i))
4982 /* If an init insn was deleted for some reason, cancel
4983 the equiv. We could update the equiv insns after
4984 transformations including an equiv insn deletion
4985 but it is not worthy as such cases are extremely
4986 rare. */
4987 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4988 /* If it is not a reverse equivalence, we check that a
4989 pseudo in rhs of the init insn is not dying in the
4990 insn. Otherwise, the live info at the beginning of
4991 the corresponding BB might be wrong after we
4992 removed the insn. When the equiv can be a
4993 constant, the right hand side of the init insn can
4994 be a pseudo. */
4995 || (! reverse_equiv_p (i)
4996 && (init_insn_rhs_dead_pseudo_p (i)
4997 /* If we reloaded the pseudo in an equivalence
4998 init insn, we cannot remove the equiv init
4999 insns and the init insns might write into
5000 const memory in this case. */
5001 || contains_reloaded_insn_p (i)))
5002 /* Prevent access beyond equivalent memory for
5003 paradoxical subregs. */
5004 || (MEM_P (x)
5005 && maybe_gt (GET_MODE_SIZE (lra_reg_info[i].biggest_mode),
5006 GET_MODE_SIZE (GET_MODE (x))))
5007 || (pic_offset_table_rtx
5008 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
5009 && (targetm.preferred_reload_class
5010 (x, lra_get_allocno_class (i)) == NO_REGS))
5011 || contains_symbol_ref_p (x))))
5012 ira_reg_equiv[i].defined_p = false;
5013 if (contains_reg_p (x, false, true))
5014 ira_reg_equiv[i].profitable_p = false;
5015 if (get_equiv (reg) != reg)
5016 bitmap_ior_into (equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
5019 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5020 update_equiv (i);
5021 /* We should add all insns containing pseudos which should be
5022 substituted by their equivalences. */
5023 EXECUTE_IF_SET_IN_BITMAP (equiv_insn_bitmap, 0, uid, bi)
5024 lra_push_insn_by_uid (uid);
5025 min_len = lra_insn_stack_length ();
5026 new_insns_num = 0;
5027 last_bb = NULL;
5028 changed_p = false;
5029 while ((new_min_len = lra_insn_stack_length ()) != 0)
5031 curr_insn = lra_pop_insn ();
5032 --new_min_len;
5033 curr_bb = BLOCK_FOR_INSN (curr_insn);
5034 if (curr_bb != last_bb)
5036 last_bb = curr_bb;
5037 bb_reload_num = lra_curr_reload_num;
5039 if (min_len > new_min_len)
5041 min_len = new_min_len;
5042 new_insns_num = 0;
5044 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
5045 internal_error
5046 ("maximum number of generated reload insns per insn achieved (%d)",
5047 MAX_RELOAD_INSNS_NUMBER);
5048 new_insns_num++;
5049 if (DEBUG_INSN_P (curr_insn))
5051 /* We need to check equivalence in debug insn and change
5052 pseudo to the equivalent value if necessary. */
5053 curr_id = lra_get_insn_recog_data (curr_insn);
5054 if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn)))
5056 rtx old = *curr_id->operand_loc[0];
5057 *curr_id->operand_loc[0]
5058 = simplify_replace_fn_rtx (old, NULL_RTX,
5059 loc_equivalence_callback, curr_insn);
5060 if (old != *curr_id->operand_loc[0])
5062 lra_update_insn_regno_info (curr_insn);
5063 changed_p = true;
5067 else if (INSN_P (curr_insn))
5069 if ((set = single_set (curr_insn)) != NULL_RTX)
5071 dest_reg = SET_DEST (set);
5072 /* The equivalence pseudo could be set up as SUBREG in a
5073 case when it is a call restore insn in a mode
5074 different from the pseudo mode. */
5075 if (GET_CODE (dest_reg) == SUBREG)
5076 dest_reg = SUBREG_REG (dest_reg);
5077 if ((REG_P (dest_reg)
5078 && (x = get_equiv (dest_reg)) != dest_reg
5079 /* Remove insns which set up a pseudo whose value
5080 cannot be changed. Such insns might be not in
5081 init_insns because we don't update equiv data
5082 during insn transformations.
5084 As an example, let suppose that a pseudo got
5085 hard register and on the 1st pass was not
5086 changed to equivalent constant. We generate an
5087 additional insn setting up the pseudo because of
5088 secondary memory movement. Then the pseudo is
5089 spilled and we use the equiv constant. In this
5090 case we should remove the additional insn and
5091 this insn is not init_insns list. */
5092 && (! MEM_P (x) || MEM_READONLY_P (x)
5093 /* Check that this is actually an insn setting
5094 up the equivalence. */
5095 || in_list_p (curr_insn,
5096 ira_reg_equiv
5097 [REGNO (dest_reg)].init_insns)))
5098 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
5099 && in_list_p (curr_insn,
5100 ira_reg_equiv
5101 [REGNO (SET_SRC (set))].init_insns)))
5103 /* This is equiv init insn of pseudo which did not get a
5104 hard register -- remove the insn. */
5105 if (lra_dump_file != NULL)
5107 fprintf (lra_dump_file,
5108 " Removing equiv init insn %i (freq=%d)\n",
5109 INSN_UID (curr_insn),
5110 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
5111 dump_insn_slim (lra_dump_file, curr_insn);
5113 if (contains_reg_p (x, true, false))
5114 check_and_force_assignment_correctness_p = true;
5115 lra_set_insn_deleted (curr_insn);
5116 continue;
5119 curr_id = lra_get_insn_recog_data (curr_insn);
5120 curr_static_id = curr_id->insn_static_data;
5121 init_curr_insn_input_reloads ();
5122 init_curr_operand_mode ();
5123 if (curr_insn_transform (false))
5124 changed_p = true;
5125 /* Check non-transformed insns too for equiv change as USE
5126 or CLOBBER don't need reloads but can contain pseudos
5127 being changed on their equivalences. */
5128 else if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn))
5129 && loc_equivalence_change_p (&PATTERN (curr_insn)))
5131 lra_update_insn_regno_info (curr_insn);
5132 changed_p = true;
5137 /* If we used a new hard regno, changed_p should be true because the
5138 hard reg is assigned to a new pseudo. */
5139 if (flag_checking && !changed_p)
5141 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5142 if (lra_reg_info[i].nrefs != 0
5143 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
5145 int j, nregs = hard_regno_nregs (hard_regno,
5146 PSEUDO_REGNO_MODE (i));
5148 for (j = 0; j < nregs; j++)
5149 lra_assert (df_regs_ever_live_p (hard_regno + j));
5152 return changed_p;
5155 static void initiate_invariants (void);
5156 static void finish_invariants (void);
5158 /* Initiate the LRA constraint pass. It is done once per
5159 function. */
5160 void
5161 lra_constraints_init (void)
5163 initiate_invariants ();
5166 /* Finalize the LRA constraint pass. It is done once per
5167 function. */
5168 void
5169 lra_constraints_finish (void)
5171 finish_invariants ();
5176 /* Structure describes invariants for ineheritance. */
5177 struct lra_invariant
5179 /* The order number of the invariant. */
5180 int num;
5181 /* The invariant RTX. */
5182 rtx invariant_rtx;
5183 /* The origin insn of the invariant. */
5184 rtx_insn *insn;
5187 typedef lra_invariant invariant_t;
5188 typedef invariant_t *invariant_ptr_t;
5189 typedef const invariant_t *const_invariant_ptr_t;
5191 /* Pointer to the inheritance invariants. */
5192 static vec<invariant_ptr_t> invariants;
5194 /* Allocation pool for the invariants. */
5195 static object_allocator<lra_invariant> *invariants_pool;
5197 /* Hash table for the invariants. */
5198 static htab_t invariant_table;
5200 /* Hash function for INVARIANT. */
5201 static hashval_t
5202 invariant_hash (const void *invariant)
5204 rtx inv = ((const_invariant_ptr_t) invariant)->invariant_rtx;
5205 return lra_rtx_hash (inv);
5208 /* Equal function for invariants INVARIANT1 and INVARIANT2. */
5209 static int
5210 invariant_eq_p (const void *invariant1, const void *invariant2)
5212 rtx inv1 = ((const_invariant_ptr_t) invariant1)->invariant_rtx;
5213 rtx inv2 = ((const_invariant_ptr_t) invariant2)->invariant_rtx;
5215 return rtx_equal_p (inv1, inv2);
5218 /* Insert INVARIANT_RTX into the table if it is not there yet. Return
5219 invariant which is in the table. */
5220 static invariant_ptr_t
5221 insert_invariant (rtx invariant_rtx)
5223 void **entry_ptr;
5224 invariant_t invariant;
5225 invariant_ptr_t invariant_ptr;
5227 invariant.invariant_rtx = invariant_rtx;
5228 entry_ptr = htab_find_slot (invariant_table, &invariant, INSERT);
5229 if (*entry_ptr == NULL)
5231 invariant_ptr = invariants_pool->allocate ();
5232 invariant_ptr->invariant_rtx = invariant_rtx;
5233 invariant_ptr->insn = NULL;
5234 invariants.safe_push (invariant_ptr);
5235 *entry_ptr = (void *) invariant_ptr;
5237 return (invariant_ptr_t) *entry_ptr;
5240 /* Initiate the invariant table. */
5241 static void
5242 initiate_invariants (void)
5244 invariants.create (100);
5245 invariants_pool
5246 = new object_allocator<lra_invariant> ("Inheritance invariants");
5247 invariant_table = htab_create (100, invariant_hash, invariant_eq_p, NULL);
5250 /* Finish the invariant table. */
5251 static void
5252 finish_invariants (void)
5254 htab_delete (invariant_table);
5255 delete invariants_pool;
5256 invariants.release ();
5259 /* Make the invariant table empty. */
5260 static void
5261 clear_invariants (void)
5263 htab_empty (invariant_table);
5264 invariants_pool->release ();
5265 invariants.truncate (0);
5270 /* This page contains code to do inheritance/split
5271 transformations. */
5273 /* Number of reloads passed so far in current EBB. */
5274 static int reloads_num;
5276 /* Number of calls passed so far in current EBB. */
5277 static int calls_num;
5279 /* Index ID is the CALLS_NUM associated the last call we saw with
5280 ABI identifier ID. */
5281 static int last_call_for_abi[NUM_ABI_IDS];
5283 /* Which registers have been fully or partially clobbered by a call
5284 since they were last used. */
5285 static HARD_REG_SET full_and_partial_call_clobbers;
5287 /* Current reload pseudo check for validity of elements in
5288 USAGE_INSNS. */
5289 static int curr_usage_insns_check;
5291 /* Info about last usage of registers in EBB to do inheritance/split
5292 transformation. Inheritance transformation is done from a spilled
5293 pseudo and split transformations from a hard register or a pseudo
5294 assigned to a hard register. */
5295 struct usage_insns
5297 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
5298 value INSNS is valid. The insns is chain of optional debug insns
5299 and a finishing non-debug insn using the corresponding reg. The
5300 value is also used to mark the registers which are set up in the
5301 current insn. The negated insn uid is used for this. */
5302 int check;
5303 /* Value of global reloads_num at the last insn in INSNS. */
5304 int reloads_num;
5305 /* Value of global reloads_nums at the last insn in INSNS. */
5306 int calls_num;
5307 /* It can be true only for splitting. And it means that the restore
5308 insn should be put after insn given by the following member. */
5309 bool after_p;
5310 /* Next insns in the current EBB which use the original reg and the
5311 original reg value is not changed between the current insn and
5312 the next insns. In order words, e.g. for inheritance, if we need
5313 to use the original reg value again in the next insns we can try
5314 to use the value in a hard register from a reload insn of the
5315 current insn. */
5316 rtx insns;
5319 /* Map: regno -> corresponding pseudo usage insns. */
5320 static struct usage_insns *usage_insns;
5322 static void
5323 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
5325 usage_insns[regno].check = curr_usage_insns_check;
5326 usage_insns[regno].insns = insn;
5327 usage_insns[regno].reloads_num = reloads_num;
5328 usage_insns[regno].calls_num = calls_num;
5329 usage_insns[regno].after_p = after_p;
5330 if (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0)
5331 remove_from_hard_reg_set (&full_and_partial_call_clobbers,
5332 PSEUDO_REGNO_MODE (regno),
5333 reg_renumber[regno]);
5336 /* The function is used to form list REGNO usages which consists of
5337 optional debug insns finished by a non-debug insn using REGNO.
5338 RELOADS_NUM is current number of reload insns processed so far. */
5339 static void
5340 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
5342 rtx next_usage_insns;
5344 if (usage_insns[regno].check == curr_usage_insns_check
5345 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
5346 && DEBUG_INSN_P (insn))
5348 /* Check that we did not add the debug insn yet. */
5349 if (next_usage_insns != insn
5350 && (GET_CODE (next_usage_insns) != INSN_LIST
5351 || XEXP (next_usage_insns, 0) != insn))
5352 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
5353 next_usage_insns);
5355 else if (NONDEBUG_INSN_P (insn))
5356 setup_next_usage_insn (regno, insn, reloads_num, false);
5357 else
5358 usage_insns[regno].check = 0;
5361 /* Return first non-debug insn in list USAGE_INSNS. */
5362 static rtx_insn *
5363 skip_usage_debug_insns (rtx usage_insns)
5365 rtx insn;
5367 /* Skip debug insns. */
5368 for (insn = usage_insns;
5369 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
5370 insn = XEXP (insn, 1))
5372 return safe_as_a <rtx_insn *> (insn);
5375 /* Return true if we need secondary memory moves for insn in
5376 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
5377 into the insn. */
5378 static bool
5379 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
5380 rtx usage_insns ATTRIBUTE_UNUSED)
5382 rtx_insn *insn;
5383 rtx set, dest;
5384 enum reg_class cl;
5386 if (inher_cl == ALL_REGS
5387 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
5388 return false;
5389 lra_assert (INSN_P (insn));
5390 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
5391 return false;
5392 dest = SET_DEST (set);
5393 if (! REG_P (dest))
5394 return false;
5395 lra_assert (inher_cl != NO_REGS);
5396 cl = get_reg_class (REGNO (dest));
5397 return (cl != NO_REGS && cl != ALL_REGS
5398 && targetm.secondary_memory_needed (GET_MODE (dest), inher_cl, cl));
5401 /* Registers involved in inheritance/split in the current EBB
5402 (inheritance/split pseudos and original registers). */
5403 static bitmap_head check_only_regs;
5405 /* Reload pseudos cannot be involded in invariant inheritance in the
5406 current EBB. */
5407 static bitmap_head invalid_invariant_regs;
5409 /* Do inheritance transformations for insn INSN, which defines (if
5410 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
5411 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
5412 form as the "insns" field of usage_insns. Return true if we
5413 succeed in such transformation.
5415 The transformations look like:
5417 p <- ... i <- ...
5418 ... p <- i (new insn)
5419 ... =>
5420 <- ... p ... <- ... i ...
5422 ... i <- p (new insn)
5423 <- ... p ... <- ... i ...
5424 ... =>
5425 <- ... p ... <- ... i ...
5426 where p is a spilled original pseudo and i is a new inheritance pseudo.
5429 The inheritance pseudo has the smallest class of two classes CL and
5430 class of ORIGINAL REGNO. */
5431 static bool
5432 inherit_reload_reg (bool def_p, int original_regno,
5433 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
5435 if (optimize_function_for_size_p (cfun))
5436 return false;
5438 enum reg_class rclass = lra_get_allocno_class (original_regno);
5439 rtx original_reg = regno_reg_rtx[original_regno];
5440 rtx new_reg, usage_insn;
5441 rtx_insn *new_insns;
5443 lra_assert (! usage_insns[original_regno].after_p);
5444 if (lra_dump_file != NULL)
5445 fprintf (lra_dump_file,
5446 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
5447 if (! ira_reg_classes_intersect_p[cl][rclass])
5449 if (lra_dump_file != NULL)
5451 fprintf (lra_dump_file,
5452 " Rejecting inheritance for %d "
5453 "because of disjoint classes %s and %s\n",
5454 original_regno, reg_class_names[cl],
5455 reg_class_names[rclass]);
5456 fprintf (lra_dump_file,
5457 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5459 return false;
5461 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
5462 /* We don't use a subset of two classes because it can be
5463 NO_REGS. This transformation is still profitable in most
5464 cases even if the classes are not intersected as register
5465 move is probably cheaper than a memory load. */
5466 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
5468 if (lra_dump_file != NULL)
5469 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
5470 reg_class_names[cl], reg_class_names[rclass]);
5472 rclass = cl;
5474 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
5476 /* Reject inheritance resulting in secondary memory moves.
5477 Otherwise, there is a danger in LRA cycling. Also such
5478 transformation will be unprofitable. */
5479 if (lra_dump_file != NULL)
5481 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
5482 rtx set = single_set (insn);
5484 lra_assert (set != NULL_RTX);
5486 rtx dest = SET_DEST (set);
5488 lra_assert (REG_P (dest));
5489 fprintf (lra_dump_file,
5490 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
5491 "as secondary mem is needed\n",
5492 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
5493 original_regno, reg_class_names[rclass]);
5494 fprintf (lra_dump_file,
5495 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5497 return false;
5499 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
5500 rclass, "inheritance");
5501 start_sequence ();
5502 if (def_p)
5503 lra_emit_move (original_reg, new_reg);
5504 else
5505 lra_emit_move (new_reg, original_reg);
5506 new_insns = get_insns ();
5507 end_sequence ();
5508 if (NEXT_INSN (new_insns) != NULL_RTX)
5510 if (lra_dump_file != NULL)
5512 fprintf (lra_dump_file,
5513 " Rejecting inheritance %d->%d "
5514 "as it results in 2 or more insns:\n",
5515 original_regno, REGNO (new_reg));
5516 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
5517 fprintf (lra_dump_file,
5518 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5520 return false;
5522 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
5523 lra_update_insn_regno_info (insn);
5524 if (! def_p)
5525 /* We now have a new usage insn for original regno. */
5526 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
5527 if (lra_dump_file != NULL)
5528 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
5529 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
5530 lra_reg_info[REGNO (new_reg)].restore_rtx = regno_reg_rtx[original_regno];
5531 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5532 bitmap_set_bit (&check_only_regs, original_regno);
5533 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
5534 if (def_p)
5535 lra_process_new_insns (insn, NULL, new_insns,
5536 "Add original<-inheritance");
5537 else
5538 lra_process_new_insns (insn, new_insns, NULL,
5539 "Add inheritance<-original");
5540 while (next_usage_insns != NULL_RTX)
5542 if (GET_CODE (next_usage_insns) != INSN_LIST)
5544 usage_insn = next_usage_insns;
5545 lra_assert (NONDEBUG_INSN_P (usage_insn));
5546 next_usage_insns = NULL;
5548 else
5550 usage_insn = XEXP (next_usage_insns, 0);
5551 lra_assert (DEBUG_INSN_P (usage_insn));
5552 next_usage_insns = XEXP (next_usage_insns, 1);
5554 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
5555 DEBUG_INSN_P (usage_insn));
5556 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5557 if (lra_dump_file != NULL)
5559 basic_block bb = BLOCK_FOR_INSN (usage_insn);
5560 fprintf (lra_dump_file,
5561 " Inheritance reuse change %d->%d (bb%d):\n",
5562 original_regno, REGNO (new_reg),
5563 bb ? bb->index : -1);
5564 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5567 if (lra_dump_file != NULL)
5568 fprintf (lra_dump_file,
5569 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5570 return true;
5573 /* Return true if we need a caller save/restore for pseudo REGNO which
5574 was assigned to a hard register. */
5575 static inline bool
5576 need_for_call_save_p (int regno)
5578 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
5579 if (usage_insns[regno].calls_num < calls_num)
5581 unsigned int abis = 0;
5582 for (unsigned int i = 0; i < NUM_ABI_IDS; ++i)
5583 if (last_call_for_abi[i] > usage_insns[regno].calls_num)
5584 abis |= 1 << i;
5585 gcc_assert (abis);
5586 if (call_clobbered_in_region_p (abis, full_and_partial_call_clobbers,
5587 PSEUDO_REGNO_MODE (regno),
5588 reg_renumber[regno]))
5589 return true;
5591 return false;
5594 /* Global registers occurring in the current EBB. */
5595 static bitmap_head ebb_global_regs;
5597 /* Return true if we need a split for hard register REGNO or pseudo
5598 REGNO which was assigned to a hard register.
5599 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
5600 used for reloads since the EBB end. It is an approximation of the
5601 used hard registers in the split range. The exact value would
5602 require expensive calculations. If we were aggressive with
5603 splitting because of the approximation, the split pseudo will save
5604 the same hard register assignment and will be removed in the undo
5605 pass. We still need the approximation because too aggressive
5606 splitting would result in too inaccurate cost calculation in the
5607 assignment pass because of too many generated moves which will be
5608 probably removed in the undo pass. */
5609 static inline bool
5610 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
5612 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
5614 lra_assert (hard_regno >= 0);
5615 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
5616 /* Don't split eliminable hard registers, otherwise we can
5617 split hard registers like hard frame pointer, which
5618 lives on BB start/end according to DF-infrastructure,
5619 when there is a pseudo assigned to the register and
5620 living in the same BB. */
5621 && (regno >= FIRST_PSEUDO_REGISTER
5622 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
5623 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
5624 /* Don't split call clobbered hard regs living through
5625 calls, otherwise we might have a check problem in the
5626 assign sub-pass as in the most cases (exception is a
5627 situation when check_and_force_assignment_correctness_p value is
5628 true) the assign pass assumes that all pseudos living
5629 through calls are assigned to call saved hard regs. */
5630 && (regno >= FIRST_PSEUDO_REGISTER
5631 || !TEST_HARD_REG_BIT (full_and_partial_call_clobbers, regno))
5632 /* We need at least 2 reloads to make pseudo splitting
5633 profitable. We should provide hard regno splitting in
5634 any case to solve 1st insn scheduling problem when
5635 moving hard register definition up might result in
5636 impossibility to find hard register for reload pseudo of
5637 small register class. */
5638 && (usage_insns[regno].reloads_num
5639 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
5640 && (regno < FIRST_PSEUDO_REGISTER
5641 /* For short living pseudos, spilling + inheritance can
5642 be considered a substitution for splitting.
5643 Therefore we do not splitting for local pseudos. It
5644 decreases also aggressiveness of splitting. The
5645 minimal number of references is chosen taking into
5646 account that for 2 references splitting has no sense
5647 as we can just spill the pseudo. */
5648 || (regno >= FIRST_PSEUDO_REGISTER
5649 && lra_reg_info[regno].nrefs > 3
5650 && bitmap_bit_p (&ebb_global_regs, regno))))
5651 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
5654 /* Return class for the split pseudo created from original pseudo with
5655 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
5656 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
5657 results in no secondary memory movements. */
5658 static enum reg_class
5659 choose_split_class (enum reg_class allocno_class,
5660 int hard_regno ATTRIBUTE_UNUSED,
5661 machine_mode mode ATTRIBUTE_UNUSED)
5663 int i;
5664 enum reg_class cl, best_cl = NO_REGS;
5665 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
5666 = REGNO_REG_CLASS (hard_regno);
5668 if (! targetm.secondary_memory_needed (mode, allocno_class, allocno_class)
5669 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
5670 return allocno_class;
5671 for (i = 0;
5672 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
5673 i++)
5674 if (! targetm.secondary_memory_needed (mode, cl, hard_reg_class)
5675 && ! targetm.secondary_memory_needed (mode, hard_reg_class, cl)
5676 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
5677 && (best_cl == NO_REGS
5678 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
5679 best_cl = cl;
5680 return best_cl;
5683 /* Copy any equivalence information from ORIGINAL_REGNO to NEW_REGNO.
5684 It only makes sense to call this function if NEW_REGNO is always
5685 equal to ORIGINAL_REGNO. */
5687 static void
5688 lra_copy_reg_equiv (unsigned int new_regno, unsigned int original_regno)
5690 if (!ira_reg_equiv[original_regno].defined_p)
5691 return;
5693 ira_expand_reg_equiv ();
5694 ira_reg_equiv[new_regno].defined_p = true;
5695 if (ira_reg_equiv[original_regno].memory)
5696 ira_reg_equiv[new_regno].memory
5697 = copy_rtx (ira_reg_equiv[original_regno].memory);
5698 if (ira_reg_equiv[original_regno].constant)
5699 ira_reg_equiv[new_regno].constant
5700 = copy_rtx (ira_reg_equiv[original_regno].constant);
5701 if (ira_reg_equiv[original_regno].invariant)
5702 ira_reg_equiv[new_regno].invariant
5703 = copy_rtx (ira_reg_equiv[original_regno].invariant);
5706 /* Do split transformations for insn INSN, which defines or uses
5707 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
5708 the EBB next uses ORIGINAL_REGNO; it has the same form as the
5709 "insns" field of usage_insns. If TO is not NULL, we don't use
5710 usage_insns, we put restore insns after TO insn. It is a case when
5711 we call it from lra_split_hard_reg_for, outside the inheritance
5712 pass.
5714 The transformations look like:
5716 p <- ... p <- ...
5717 ... s <- p (new insn -- save)
5718 ... =>
5719 ... p <- s (new insn -- restore)
5720 <- ... p ... <- ... p ...
5722 <- ... p ... <- ... p ...
5723 ... s <- p (new insn -- save)
5724 ... =>
5725 ... p <- s (new insn -- restore)
5726 <- ... p ... <- ... p ...
5728 where p is an original pseudo got a hard register or a hard
5729 register and s is a new split pseudo. The save is put before INSN
5730 if BEFORE_P is true. Return true if we succeed in such
5731 transformation. */
5732 static bool
5733 split_reg (bool before_p, int original_regno, rtx_insn *insn,
5734 rtx next_usage_insns, rtx_insn *to)
5736 enum reg_class rclass;
5737 rtx original_reg;
5738 int hard_regno, nregs;
5739 rtx new_reg, usage_insn;
5740 rtx_insn *restore, *save;
5741 bool after_p;
5742 bool call_save_p;
5743 machine_mode mode;
5745 if (original_regno < FIRST_PSEUDO_REGISTER)
5747 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
5748 hard_regno = original_regno;
5749 call_save_p = false;
5750 nregs = 1;
5751 mode = lra_reg_info[hard_regno].biggest_mode;
5752 machine_mode reg_rtx_mode = GET_MODE (regno_reg_rtx[hard_regno]);
5753 /* A reg can have a biggest_mode of VOIDmode if it was only ever seen
5754 as part of a multi-word register. In that case, or if the biggest
5755 mode was larger than a register, just use the reg_rtx. Otherwise,
5756 limit the size to that of the biggest access in the function. */
5757 if (mode == VOIDmode
5758 || paradoxical_subreg_p (mode, reg_rtx_mode))
5760 original_reg = regno_reg_rtx[hard_regno];
5761 mode = reg_rtx_mode;
5763 else
5764 original_reg = gen_rtx_REG (mode, hard_regno);
5766 else
5768 mode = PSEUDO_REGNO_MODE (original_regno);
5769 hard_regno = reg_renumber[original_regno];
5770 nregs = hard_regno_nregs (hard_regno, mode);
5771 rclass = lra_get_allocno_class (original_regno);
5772 original_reg = regno_reg_rtx[original_regno];
5773 call_save_p = need_for_call_save_p (original_regno);
5775 lra_assert (hard_regno >= 0);
5776 if (lra_dump_file != NULL)
5777 fprintf (lra_dump_file,
5778 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
5780 if (call_save_p)
5782 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
5783 hard_regno_nregs (hard_regno, mode),
5784 mode);
5785 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
5787 else
5789 rclass = choose_split_class (rclass, hard_regno, mode);
5790 if (rclass == NO_REGS)
5792 if (lra_dump_file != NULL)
5794 fprintf (lra_dump_file,
5795 " Rejecting split of %d(%s): "
5796 "no good reg class for %d(%s)\n",
5797 original_regno,
5798 reg_class_names[lra_get_allocno_class (original_regno)],
5799 hard_regno,
5800 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
5801 fprintf
5802 (lra_dump_file,
5803 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5805 return false;
5807 /* Split_if_necessary can split hard registers used as part of a
5808 multi-register mode but splits each register individually. The
5809 mode used for each independent register may not be supported
5810 so reject the split. Splitting the wider mode should theoretically
5811 be possible but is not implemented. */
5812 if (!targetm.hard_regno_mode_ok (hard_regno, mode))
5814 if (lra_dump_file != NULL)
5816 fprintf (lra_dump_file,
5817 " Rejecting split of %d(%s): unsuitable mode %s\n",
5818 original_regno,
5819 reg_class_names[lra_get_allocno_class (original_regno)],
5820 GET_MODE_NAME (mode));
5821 fprintf
5822 (lra_dump_file,
5823 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5825 return false;
5827 new_reg = lra_create_new_reg (mode, original_reg, rclass, "split");
5828 reg_renumber[REGNO (new_reg)] = hard_regno;
5830 int new_regno = REGNO (new_reg);
5831 save = emit_spill_move (true, new_reg, original_reg);
5832 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
5834 if (lra_dump_file != NULL)
5836 fprintf
5837 (lra_dump_file,
5838 " Rejecting split %d->%d resulting in > 2 save insns:\n",
5839 original_regno, new_regno);
5840 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
5841 fprintf (lra_dump_file,
5842 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5844 return false;
5846 restore = emit_spill_move (false, new_reg, original_reg);
5847 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
5849 if (lra_dump_file != NULL)
5851 fprintf (lra_dump_file,
5852 " Rejecting split %d->%d "
5853 "resulting in > 2 restore insns:\n",
5854 original_regno, new_regno);
5855 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
5856 fprintf (lra_dump_file,
5857 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5859 return false;
5861 /* Transfer equivalence information to the spill register, so that
5862 if we fail to allocate the spill register, we have the option of
5863 rematerializing the original value instead of spilling to the stack. */
5864 if (!HARD_REGISTER_NUM_P (original_regno)
5865 && mode == PSEUDO_REGNO_MODE (original_regno))
5866 lra_copy_reg_equiv (new_regno, original_regno);
5867 lra_reg_info[new_regno].restore_rtx = regno_reg_rtx[original_regno];
5868 bitmap_set_bit (&lra_split_regs, new_regno);
5869 if (to != NULL)
5871 lra_assert (next_usage_insns == NULL);
5872 usage_insn = to;
5873 after_p = TRUE;
5875 else
5877 /* We need check_only_regs only inside the inheritance pass. */
5878 bitmap_set_bit (&check_only_regs, new_regno);
5879 bitmap_set_bit (&check_only_regs, original_regno);
5880 after_p = usage_insns[original_regno].after_p;
5881 for (;;)
5883 if (GET_CODE (next_usage_insns) != INSN_LIST)
5885 usage_insn = next_usage_insns;
5886 break;
5888 usage_insn = XEXP (next_usage_insns, 0);
5889 lra_assert (DEBUG_INSN_P (usage_insn));
5890 next_usage_insns = XEXP (next_usage_insns, 1);
5891 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
5892 true);
5893 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5894 if (lra_dump_file != NULL)
5896 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5897 original_regno, new_regno);
5898 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5902 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5903 lra_assert (usage_insn != insn || (after_p && before_p));
5904 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5905 after_p ? NULL : restore,
5906 after_p ? restore : NULL,
5907 call_save_p
5908 ? "Add reg<-save" : "Add reg<-split");
5909 lra_process_new_insns (insn, before_p ? save : NULL,
5910 before_p ? NULL : save,
5911 call_save_p
5912 ? "Add save<-reg" : "Add split<-reg");
5913 if (nregs > 1)
5914 /* If we are trying to split multi-register. We should check
5915 conflicts on the next assignment sub-pass. IRA can allocate on
5916 sub-register levels, LRA do this on pseudos level right now and
5917 this discrepancy may create allocation conflicts after
5918 splitting. */
5919 check_and_force_assignment_correctness_p = true;
5920 if (lra_dump_file != NULL)
5921 fprintf (lra_dump_file,
5922 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5923 return true;
5926 /* Split a hard reg for reload pseudo REGNO having RCLASS and living
5927 in the range [FROM, TO]. Return true if did a split. Otherwise,
5928 return false. */
5929 bool
5930 spill_hard_reg_in_range (int regno, enum reg_class rclass, rtx_insn *from, rtx_insn *to)
5932 int i, hard_regno;
5933 int rclass_size;
5934 rtx_insn *insn;
5935 unsigned int uid;
5936 bitmap_iterator bi;
5937 HARD_REG_SET ignore;
5939 lra_assert (from != NULL && to != NULL);
5940 CLEAR_HARD_REG_SET (ignore);
5941 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
5943 lra_insn_recog_data_t id = lra_insn_recog_data[uid];
5944 struct lra_static_insn_data *static_id = id->insn_static_data;
5945 struct lra_insn_reg *reg;
5947 for (reg = id->regs; reg != NULL; reg = reg->next)
5948 if (reg->regno < FIRST_PSEUDO_REGISTER)
5949 SET_HARD_REG_BIT (ignore, reg->regno);
5950 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
5951 SET_HARD_REG_BIT (ignore, reg->regno);
5953 rclass_size = ira_class_hard_regs_num[rclass];
5954 for (i = 0; i < rclass_size; i++)
5956 hard_regno = ira_class_hard_regs[rclass][i];
5957 if (! TEST_HARD_REG_BIT (lra_reg_info[regno].conflict_hard_regs, hard_regno)
5958 || TEST_HARD_REG_BIT (ignore, hard_regno))
5959 continue;
5960 for (insn = from; insn != NEXT_INSN (to); insn = NEXT_INSN (insn))
5962 struct lra_static_insn_data *static_id;
5963 struct lra_insn_reg *reg;
5965 if (!INSN_P (insn))
5966 continue;
5967 if (bitmap_bit_p (&lra_reg_info[hard_regno].insn_bitmap,
5968 INSN_UID (insn)))
5969 break;
5970 static_id = lra_get_insn_recog_data (insn)->insn_static_data;
5971 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
5972 if (reg->regno == hard_regno)
5973 break;
5974 if (reg != NULL)
5975 break;
5977 if (insn != NEXT_INSN (to))
5978 continue;
5979 if (split_reg (TRUE, hard_regno, from, NULL, to))
5980 return true;
5982 return false;
5985 /* Recognize that we need a split transformation for insn INSN, which
5986 defines or uses REGNO in its insn biggest MODE (we use it only if
5987 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5988 hard registers which might be used for reloads since the EBB end.
5989 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5990 uid before starting INSN processing. Return true if we succeed in
5991 such transformation. */
5992 static bool
5993 split_if_necessary (int regno, machine_mode mode,
5994 HARD_REG_SET potential_reload_hard_regs,
5995 bool before_p, rtx_insn *insn, int max_uid)
5997 bool res = false;
5998 int i, nregs = 1;
5999 rtx next_usage_insns;
6001 if (regno < FIRST_PSEUDO_REGISTER)
6002 nregs = hard_regno_nregs (regno, mode);
6003 for (i = 0; i < nregs; i++)
6004 if (usage_insns[regno + i].check == curr_usage_insns_check
6005 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
6006 /* To avoid processing the register twice or more. */
6007 && ((GET_CODE (next_usage_insns) != INSN_LIST
6008 && INSN_UID (next_usage_insns) < max_uid)
6009 || (GET_CODE (next_usage_insns) == INSN_LIST
6010 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
6011 && need_for_split_p (potential_reload_hard_regs, regno + i)
6012 && split_reg (before_p, regno + i, insn, next_usage_insns, NULL))
6013 res = true;
6014 return res;
6017 /* Return TRUE if rtx X is considered as an invariant for
6018 inheritance. */
6019 static bool
6020 invariant_p (const_rtx x)
6022 machine_mode mode;
6023 const char *fmt;
6024 enum rtx_code code;
6025 int i, j;
6027 if (side_effects_p (x))
6028 return false;
6030 code = GET_CODE (x);
6031 mode = GET_MODE (x);
6032 if (code == SUBREG)
6034 x = SUBREG_REG (x);
6035 code = GET_CODE (x);
6036 mode = wider_subreg_mode (mode, GET_MODE (x));
6039 if (MEM_P (x))
6040 return false;
6042 if (REG_P (x))
6044 int i, nregs, regno = REGNO (x);
6046 if (regno >= FIRST_PSEUDO_REGISTER || regno == STACK_POINTER_REGNUM
6047 || TEST_HARD_REG_BIT (eliminable_regset, regno)
6048 || GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
6049 return false;
6050 nregs = hard_regno_nregs (regno, mode);
6051 for (i = 0; i < nregs; i++)
6052 if (! fixed_regs[regno + i]
6053 /* A hard register may be clobbered in the current insn
6054 but we can ignore this case because if the hard
6055 register is used it should be set somewhere after the
6056 clobber. */
6057 || bitmap_bit_p (&invalid_invariant_regs, regno + i))
6058 return false;
6060 fmt = GET_RTX_FORMAT (code);
6061 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6063 if (fmt[i] == 'e')
6065 if (! invariant_p (XEXP (x, i)))
6066 return false;
6068 else if (fmt[i] == 'E')
6070 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6071 if (! invariant_p (XVECEXP (x, i, j)))
6072 return false;
6075 return true;
6078 /* We have 'dest_reg <- invariant'. Let us try to make an invariant
6079 inheritance transformation (using dest_reg instead invariant in a
6080 subsequent insn). */
6081 static bool
6082 process_invariant_for_inheritance (rtx dst_reg, rtx invariant_rtx)
6084 invariant_ptr_t invariant_ptr;
6085 rtx_insn *insn, *new_insns;
6086 rtx insn_set, insn_reg, new_reg;
6087 int insn_regno;
6088 bool succ_p = false;
6089 int dst_regno = REGNO (dst_reg);
6090 machine_mode dst_mode = GET_MODE (dst_reg);
6091 enum reg_class cl = lra_get_allocno_class (dst_regno), insn_reg_cl;
6093 invariant_ptr = insert_invariant (invariant_rtx);
6094 if ((insn = invariant_ptr->insn) != NULL_RTX)
6096 /* We have a subsequent insn using the invariant. */
6097 insn_set = single_set (insn);
6098 lra_assert (insn_set != NULL);
6099 insn_reg = SET_DEST (insn_set);
6100 lra_assert (REG_P (insn_reg));
6101 insn_regno = REGNO (insn_reg);
6102 insn_reg_cl = lra_get_allocno_class (insn_regno);
6104 if (dst_mode == GET_MODE (insn_reg)
6105 /* We should consider only result move reg insns which are
6106 cheap. */
6107 && targetm.register_move_cost (dst_mode, cl, insn_reg_cl) == 2
6108 && targetm.register_move_cost (dst_mode, cl, cl) == 2)
6110 if (lra_dump_file != NULL)
6111 fprintf (lra_dump_file,
6112 " [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[\n");
6113 new_reg = lra_create_new_reg (dst_mode, dst_reg,
6114 cl, "invariant inheritance");
6115 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
6116 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
6117 lra_reg_info[REGNO (new_reg)].restore_rtx = PATTERN (insn);
6118 start_sequence ();
6119 lra_emit_move (new_reg, dst_reg);
6120 new_insns = get_insns ();
6121 end_sequence ();
6122 lra_process_new_insns (curr_insn, NULL, new_insns,
6123 "Add invariant inheritance<-original");
6124 start_sequence ();
6125 lra_emit_move (SET_DEST (insn_set), new_reg);
6126 new_insns = get_insns ();
6127 end_sequence ();
6128 lra_process_new_insns (insn, NULL, new_insns,
6129 "Changing reload<-inheritance");
6130 lra_set_insn_deleted (insn);
6131 succ_p = true;
6132 if (lra_dump_file != NULL)
6134 fprintf (lra_dump_file,
6135 " Invariant inheritance reuse change %d (bb%d):\n",
6136 REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
6137 dump_insn_slim (lra_dump_file, insn);
6138 fprintf (lra_dump_file,
6139 " ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]\n");
6143 invariant_ptr->insn = curr_insn;
6144 return succ_p;
6147 /* Check only registers living at the current program point in the
6148 current EBB. */
6149 static bitmap_head live_regs;
6151 /* Update live info in EBB given by its HEAD and TAIL insns after
6152 inheritance/split transformation. The function removes dead moves
6153 too. */
6154 static void
6155 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
6157 unsigned int j;
6158 int i, regno;
6159 bool live_p;
6160 rtx_insn *prev_insn;
6161 rtx set;
6162 bool remove_p;
6163 basic_block last_bb, prev_bb, curr_bb;
6164 bitmap_iterator bi;
6165 struct lra_insn_reg *reg;
6166 edge e;
6167 edge_iterator ei;
6169 last_bb = BLOCK_FOR_INSN (tail);
6170 prev_bb = NULL;
6171 for (curr_insn = tail;
6172 curr_insn != PREV_INSN (head);
6173 curr_insn = prev_insn)
6175 prev_insn = PREV_INSN (curr_insn);
6176 /* We need to process empty blocks too. They contain
6177 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
6178 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
6179 continue;
6180 curr_bb = BLOCK_FOR_INSN (curr_insn);
6181 if (curr_bb != prev_bb)
6183 if (prev_bb != NULL)
6185 /* Update df_get_live_in (prev_bb): */
6186 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6187 if (bitmap_bit_p (&live_regs, j))
6188 bitmap_set_bit (df_get_live_in (prev_bb), j);
6189 else
6190 bitmap_clear_bit (df_get_live_in (prev_bb), j);
6192 if (curr_bb != last_bb)
6194 /* Update df_get_live_out (curr_bb): */
6195 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6197 live_p = bitmap_bit_p (&live_regs, j);
6198 if (! live_p)
6199 FOR_EACH_EDGE (e, ei, curr_bb->succs)
6200 if (bitmap_bit_p (df_get_live_in (e->dest), j))
6202 live_p = true;
6203 break;
6205 if (live_p)
6206 bitmap_set_bit (df_get_live_out (curr_bb), j);
6207 else
6208 bitmap_clear_bit (df_get_live_out (curr_bb), j);
6211 prev_bb = curr_bb;
6212 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
6214 if (! NONDEBUG_INSN_P (curr_insn))
6215 continue;
6216 curr_id = lra_get_insn_recog_data (curr_insn);
6217 curr_static_id = curr_id->insn_static_data;
6218 remove_p = false;
6219 if ((set = single_set (curr_insn)) != NULL_RTX
6220 && REG_P (SET_DEST (set))
6221 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
6222 && SET_DEST (set) != pic_offset_table_rtx
6223 && bitmap_bit_p (&check_only_regs, regno)
6224 && ! bitmap_bit_p (&live_regs, regno))
6225 remove_p = true;
6226 /* See which defined values die here. */
6227 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6228 if (reg->type == OP_OUT && ! reg->subreg_p)
6229 bitmap_clear_bit (&live_regs, reg->regno);
6230 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6231 if (reg->type == OP_OUT && ! reg->subreg_p)
6232 bitmap_clear_bit (&live_regs, reg->regno);
6233 if (curr_id->arg_hard_regs != NULL)
6234 /* Make clobbered argument hard registers die. */
6235 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6236 if (regno >= FIRST_PSEUDO_REGISTER)
6237 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
6238 /* Mark each used value as live. */
6239 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6240 if (reg->type != OP_OUT
6241 && bitmap_bit_p (&check_only_regs, reg->regno))
6242 bitmap_set_bit (&live_regs, reg->regno);
6243 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6244 if (reg->type != OP_OUT
6245 && bitmap_bit_p (&check_only_regs, reg->regno))
6246 bitmap_set_bit (&live_regs, reg->regno);
6247 if (curr_id->arg_hard_regs != NULL)
6248 /* Make used argument hard registers live. */
6249 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6250 if (regno < FIRST_PSEUDO_REGISTER
6251 && bitmap_bit_p (&check_only_regs, regno))
6252 bitmap_set_bit (&live_regs, regno);
6253 /* It is quite important to remove dead move insns because it
6254 means removing dead store. We don't need to process them for
6255 constraints. */
6256 if (remove_p)
6258 if (lra_dump_file != NULL)
6260 fprintf (lra_dump_file, " Removing dead insn:\n ");
6261 dump_insn_slim (lra_dump_file, curr_insn);
6263 lra_set_insn_deleted (curr_insn);
6268 /* The structure describes info to do an inheritance for the current
6269 insn. We need to collect such info first before doing the
6270 transformations because the transformations change the insn
6271 internal representation. */
6272 struct to_inherit
6274 /* Original regno. */
6275 int regno;
6276 /* Subsequent insns which can inherit original reg value. */
6277 rtx insns;
6280 /* Array containing all info for doing inheritance from the current
6281 insn. */
6282 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
6284 /* Number elements in the previous array. */
6285 static int to_inherit_num;
6287 /* Add inheritance info REGNO and INSNS. Their meaning is described in
6288 structure to_inherit. */
6289 static void
6290 add_to_inherit (int regno, rtx insns)
6292 int i;
6294 for (i = 0; i < to_inherit_num; i++)
6295 if (to_inherit[i].regno == regno)
6296 return;
6297 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
6298 to_inherit[to_inherit_num].regno = regno;
6299 to_inherit[to_inherit_num++].insns = insns;
6302 /* Return the last non-debug insn in basic block BB, or the block begin
6303 note if none. */
6304 static rtx_insn *
6305 get_last_insertion_point (basic_block bb)
6307 rtx_insn *insn;
6309 FOR_BB_INSNS_REVERSE (bb, insn)
6310 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
6311 return insn;
6312 gcc_unreachable ();
6315 /* Set up RES by registers living on edges FROM except the edge (FROM,
6316 TO) or by registers set up in a jump insn in BB FROM. */
6317 static void
6318 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
6320 rtx_insn *last;
6321 struct lra_insn_reg *reg;
6322 edge e;
6323 edge_iterator ei;
6325 lra_assert (to != NULL);
6326 bitmap_clear (res);
6327 FOR_EACH_EDGE (e, ei, from->succs)
6328 if (e->dest != to)
6329 bitmap_ior_into (res, df_get_live_in (e->dest));
6330 last = get_last_insertion_point (from);
6331 if (! JUMP_P (last))
6332 return;
6333 curr_id = lra_get_insn_recog_data (last);
6334 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6335 if (reg->type != OP_IN)
6336 bitmap_set_bit (res, reg->regno);
6339 /* Used as a temporary results of some bitmap calculations. */
6340 static bitmap_head temp_bitmap;
6342 /* We split for reloads of small class of hard regs. The following
6343 defines how many hard regs the class should have to be qualified as
6344 small. The code is mostly oriented to x86/x86-64 architecture
6345 where some insns need to use only specific register or pair of
6346 registers and these register can live in RTL explicitly, e.g. for
6347 parameter passing. */
6348 static const int max_small_class_regs_num = 2;
6350 /* Do inheritance/split transformations in EBB starting with HEAD and
6351 finishing on TAIL. We process EBB insns in the reverse order.
6352 Return true if we did any inheritance/split transformation in the
6353 EBB.
6355 We should avoid excessive splitting which results in worse code
6356 because of inaccurate cost calculations for spilling new split
6357 pseudos in such case. To achieve this we do splitting only if
6358 register pressure is high in given basic block and there are reload
6359 pseudos requiring hard registers. We could do more register
6360 pressure calculations at any given program point to avoid necessary
6361 splitting even more but it is to expensive and the current approach
6362 works well enough. */
6363 static bool
6364 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
6366 int i, src_regno, dst_regno, nregs;
6367 bool change_p, succ_p, update_reloads_num_p;
6368 rtx_insn *prev_insn, *last_insn;
6369 rtx next_usage_insns, curr_set;
6370 enum reg_class cl;
6371 struct lra_insn_reg *reg;
6372 basic_block last_processed_bb, curr_bb = NULL;
6373 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
6374 bitmap to_process;
6375 unsigned int j;
6376 bitmap_iterator bi;
6377 bool head_p, after_p;
6379 change_p = false;
6380 curr_usage_insns_check++;
6381 clear_invariants ();
6382 reloads_num = calls_num = 0;
6383 for (unsigned int i = 0; i < NUM_ABI_IDS; ++i)
6384 last_call_for_abi[i] = 0;
6385 CLEAR_HARD_REG_SET (full_and_partial_call_clobbers);
6386 bitmap_clear (&check_only_regs);
6387 bitmap_clear (&invalid_invariant_regs);
6388 last_processed_bb = NULL;
6389 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6390 live_hard_regs = eliminable_regset | lra_no_alloc_regs;
6391 /* We don't process new insns generated in the loop. */
6392 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
6394 prev_insn = PREV_INSN (curr_insn);
6395 if (BLOCK_FOR_INSN (curr_insn) != NULL)
6396 curr_bb = BLOCK_FOR_INSN (curr_insn);
6397 if (last_processed_bb != curr_bb)
6399 /* We are at the end of BB. Add qualified living
6400 pseudos for potential splitting. */
6401 to_process = df_get_live_out (curr_bb);
6402 if (last_processed_bb != NULL)
6404 /* We are somewhere in the middle of EBB. */
6405 get_live_on_other_edges (curr_bb, last_processed_bb,
6406 &temp_bitmap);
6407 to_process = &temp_bitmap;
6409 last_processed_bb = curr_bb;
6410 last_insn = get_last_insertion_point (curr_bb);
6411 after_p = (! JUMP_P (last_insn)
6412 && (! CALL_P (last_insn)
6413 || (find_reg_note (last_insn,
6414 REG_NORETURN, NULL_RTX) == NULL_RTX
6415 && ! SIBLING_CALL_P (last_insn))));
6416 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6417 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
6419 if ((int) j >= lra_constraint_new_regno_start)
6420 break;
6421 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
6423 if (j < FIRST_PSEUDO_REGISTER)
6424 SET_HARD_REG_BIT (live_hard_regs, j);
6425 else
6426 add_to_hard_reg_set (&live_hard_regs,
6427 PSEUDO_REGNO_MODE (j),
6428 reg_renumber[j]);
6429 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
6433 src_regno = dst_regno = -1;
6434 curr_set = single_set (curr_insn);
6435 if (curr_set != NULL_RTX && REG_P (SET_DEST (curr_set)))
6436 dst_regno = REGNO (SET_DEST (curr_set));
6437 if (curr_set != NULL_RTX && REG_P (SET_SRC (curr_set)))
6438 src_regno = REGNO (SET_SRC (curr_set));
6439 update_reloads_num_p = true;
6440 if (src_regno < lra_constraint_new_regno_start
6441 && src_regno >= FIRST_PSEUDO_REGISTER
6442 && reg_renumber[src_regno] < 0
6443 && dst_regno >= lra_constraint_new_regno_start
6444 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
6446 /* 'reload_pseudo <- original_pseudo'. */
6447 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6448 reloads_num++;
6449 update_reloads_num_p = false;
6450 succ_p = false;
6451 if (usage_insns[src_regno].check == curr_usage_insns_check
6452 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
6453 succ_p = inherit_reload_reg (false, src_regno, cl,
6454 curr_insn, next_usage_insns);
6455 if (succ_p)
6456 change_p = true;
6457 else
6458 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6459 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6460 potential_reload_hard_regs |= reg_class_contents[cl];
6462 else if (src_regno < 0
6463 && dst_regno >= lra_constraint_new_regno_start
6464 && invariant_p (SET_SRC (curr_set))
6465 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS
6466 && ! bitmap_bit_p (&invalid_invariant_regs, dst_regno)
6467 && ! bitmap_bit_p (&invalid_invariant_regs,
6468 ORIGINAL_REGNO(regno_reg_rtx[dst_regno])))
6470 /* 'reload_pseudo <- invariant'. */
6471 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6472 reloads_num++;
6473 update_reloads_num_p = false;
6474 if (process_invariant_for_inheritance (SET_DEST (curr_set), SET_SRC (curr_set)))
6475 change_p = true;
6476 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6477 potential_reload_hard_regs |= reg_class_contents[cl];
6479 else if (src_regno >= lra_constraint_new_regno_start
6480 && dst_regno < lra_constraint_new_regno_start
6481 && dst_regno >= FIRST_PSEUDO_REGISTER
6482 && reg_renumber[dst_regno] < 0
6483 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
6484 && usage_insns[dst_regno].check == curr_usage_insns_check
6485 && (next_usage_insns
6486 = usage_insns[dst_regno].insns) != NULL_RTX)
6488 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6489 reloads_num++;
6490 update_reloads_num_p = false;
6491 /* 'original_pseudo <- reload_pseudo'. */
6492 if (! JUMP_P (curr_insn)
6493 && inherit_reload_reg (true, dst_regno, cl,
6494 curr_insn, next_usage_insns))
6495 change_p = true;
6496 /* Invalidate. */
6497 usage_insns[dst_regno].check = 0;
6498 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6499 potential_reload_hard_regs |= reg_class_contents[cl];
6501 else if (INSN_P (curr_insn))
6503 int iter;
6504 int max_uid = get_max_uid ();
6506 curr_id = lra_get_insn_recog_data (curr_insn);
6507 curr_static_id = curr_id->insn_static_data;
6508 to_inherit_num = 0;
6509 /* Process insn definitions. */
6510 for (iter = 0; iter < 2; iter++)
6511 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6512 reg != NULL;
6513 reg = reg->next)
6514 if (reg->type != OP_IN
6515 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
6517 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
6518 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
6519 && usage_insns[dst_regno].check == curr_usage_insns_check
6520 && (next_usage_insns
6521 = usage_insns[dst_regno].insns) != NULL_RTX)
6523 struct lra_insn_reg *r;
6525 for (r = curr_id->regs; r != NULL; r = r->next)
6526 if (r->type != OP_OUT && r->regno == dst_regno)
6527 break;
6528 /* Don't do inheritance if the pseudo is also
6529 used in the insn. */
6530 if (r == NULL)
6531 /* We cannot do inheritance right now
6532 because the current insn reg info (chain
6533 regs) can change after that. */
6534 add_to_inherit (dst_regno, next_usage_insns);
6536 /* We cannot process one reg twice here because of
6537 usage_insns invalidation. */
6538 if ((dst_regno < FIRST_PSEUDO_REGISTER
6539 || reg_renumber[dst_regno] >= 0)
6540 && ! reg->subreg_p && reg->type != OP_IN)
6542 HARD_REG_SET s;
6544 if (split_if_necessary (dst_regno, reg->biggest_mode,
6545 potential_reload_hard_regs,
6546 false, curr_insn, max_uid))
6547 change_p = true;
6548 CLEAR_HARD_REG_SET (s);
6549 if (dst_regno < FIRST_PSEUDO_REGISTER)
6550 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
6551 else
6552 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
6553 reg_renumber[dst_regno]);
6554 live_hard_regs &= ~s;
6555 potential_reload_hard_regs &= ~s;
6557 /* We should invalidate potential inheritance or
6558 splitting for the current insn usages to the next
6559 usage insns (see code below) as the output pseudo
6560 prevents this. */
6561 if ((dst_regno >= FIRST_PSEUDO_REGISTER
6562 && reg_renumber[dst_regno] < 0)
6563 || (reg->type == OP_OUT && ! reg->subreg_p
6564 && (dst_regno < FIRST_PSEUDO_REGISTER
6565 || reg_renumber[dst_regno] >= 0)))
6567 /* Invalidate and mark definitions. */
6568 if (dst_regno >= FIRST_PSEUDO_REGISTER)
6569 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
6570 else
6572 nregs = hard_regno_nregs (dst_regno,
6573 reg->biggest_mode);
6574 for (i = 0; i < nregs; i++)
6575 usage_insns[dst_regno + i].check
6576 = -(int) INSN_UID (curr_insn);
6580 /* Process clobbered call regs. */
6581 if (curr_id->arg_hard_regs != NULL)
6582 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6583 if (dst_regno >= FIRST_PSEUDO_REGISTER)
6584 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
6585 = -(int) INSN_UID (curr_insn);
6586 if (! JUMP_P (curr_insn))
6587 for (i = 0; i < to_inherit_num; i++)
6588 if (inherit_reload_reg (true, to_inherit[i].regno,
6589 ALL_REGS, curr_insn,
6590 to_inherit[i].insns))
6591 change_p = true;
6592 if (CALL_P (curr_insn))
6594 rtx cheap, pat, dest;
6595 rtx_insn *restore;
6596 int regno, hard_regno;
6598 calls_num++;
6599 function_abi callee_abi = insn_callee_abi (curr_insn);
6600 last_call_for_abi[callee_abi.id ()] = calls_num;
6601 full_and_partial_call_clobbers
6602 |= callee_abi.full_and_partial_reg_clobbers ();
6603 if ((cheap = find_reg_note (curr_insn,
6604 REG_RETURNED, NULL_RTX)) != NULL_RTX
6605 && ((cheap = XEXP (cheap, 0)), true)
6606 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
6607 && (hard_regno = reg_renumber[regno]) >= 0
6608 && usage_insns[regno].check == curr_usage_insns_check
6609 /* If there are pending saves/restores, the
6610 optimization is not worth. */
6611 && usage_insns[regno].calls_num == calls_num - 1
6612 && callee_abi.clobbers_reg_p (GET_MODE (cheap), hard_regno))
6614 /* Restore the pseudo from the call result as
6615 REG_RETURNED note says that the pseudo value is
6616 in the call result and the pseudo is an argument
6617 of the call. */
6618 pat = PATTERN (curr_insn);
6619 if (GET_CODE (pat) == PARALLEL)
6620 pat = XVECEXP (pat, 0, 0);
6621 dest = SET_DEST (pat);
6622 /* For multiple return values dest is PARALLEL.
6623 Currently we handle only single return value case. */
6624 if (REG_P (dest))
6626 start_sequence ();
6627 emit_move_insn (cheap, copy_rtx (dest));
6628 restore = get_insns ();
6629 end_sequence ();
6630 lra_process_new_insns (curr_insn, NULL, restore,
6631 "Inserting call parameter restore");
6632 /* We don't need to save/restore of the pseudo from
6633 this call. */
6634 usage_insns[regno].calls_num = calls_num;
6635 remove_from_hard_reg_set
6636 (&full_and_partial_call_clobbers,
6637 GET_MODE (cheap), hard_regno);
6638 bitmap_set_bit (&check_only_regs, regno);
6642 to_inherit_num = 0;
6643 /* Process insn usages. */
6644 for (iter = 0; iter < 2; iter++)
6645 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6646 reg != NULL;
6647 reg = reg->next)
6648 if ((reg->type != OP_OUT
6649 || (reg->type == OP_OUT && reg->subreg_p))
6650 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
6652 if (src_regno >= FIRST_PSEUDO_REGISTER
6653 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
6655 if (usage_insns[src_regno].check == curr_usage_insns_check
6656 && (next_usage_insns
6657 = usage_insns[src_regno].insns) != NULL_RTX
6658 && NONDEBUG_INSN_P (curr_insn))
6659 add_to_inherit (src_regno, next_usage_insns);
6660 else if (usage_insns[src_regno].check
6661 != -(int) INSN_UID (curr_insn))
6662 /* Add usages but only if the reg is not set up
6663 in the same insn. */
6664 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6666 else if (src_regno < FIRST_PSEUDO_REGISTER
6667 || reg_renumber[src_regno] >= 0)
6669 bool before_p;
6670 rtx_insn *use_insn = curr_insn;
6672 before_p = (JUMP_P (curr_insn)
6673 || (CALL_P (curr_insn) && reg->type == OP_IN));
6674 if (NONDEBUG_INSN_P (curr_insn)
6675 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
6676 && split_if_necessary (src_regno, reg->biggest_mode,
6677 potential_reload_hard_regs,
6678 before_p, curr_insn, max_uid))
6680 if (reg->subreg_p)
6681 check_and_force_assignment_correctness_p = true;
6682 change_p = true;
6683 /* Invalidate. */
6684 usage_insns[src_regno].check = 0;
6685 if (before_p)
6686 use_insn = PREV_INSN (curr_insn);
6688 if (NONDEBUG_INSN_P (curr_insn))
6690 if (src_regno < FIRST_PSEUDO_REGISTER)
6691 add_to_hard_reg_set (&live_hard_regs,
6692 reg->biggest_mode, src_regno);
6693 else
6694 add_to_hard_reg_set (&live_hard_regs,
6695 PSEUDO_REGNO_MODE (src_regno),
6696 reg_renumber[src_regno]);
6698 if (src_regno >= FIRST_PSEUDO_REGISTER)
6699 add_next_usage_insn (src_regno, use_insn, reloads_num);
6700 else
6702 for (i = 0; i < hard_regno_nregs (src_regno, reg->biggest_mode); i++)
6703 add_next_usage_insn (src_regno + i, use_insn, reloads_num);
6707 /* Process used call regs. */
6708 if (curr_id->arg_hard_regs != NULL)
6709 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6710 if (src_regno < FIRST_PSEUDO_REGISTER)
6712 SET_HARD_REG_BIT (live_hard_regs, src_regno);
6713 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6715 for (i = 0; i < to_inherit_num; i++)
6717 src_regno = to_inherit[i].regno;
6718 if (inherit_reload_reg (false, src_regno, ALL_REGS,
6719 curr_insn, to_inherit[i].insns))
6720 change_p = true;
6721 else
6722 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6725 if (update_reloads_num_p
6726 && NONDEBUG_INSN_P (curr_insn) && curr_set != NULL_RTX)
6728 int regno = -1;
6729 if ((REG_P (SET_DEST (curr_set))
6730 && (regno = REGNO (SET_DEST (curr_set))) >= lra_constraint_new_regno_start
6731 && reg_renumber[regno] < 0
6732 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
6733 || (REG_P (SET_SRC (curr_set))
6734 && (regno = REGNO (SET_SRC (curr_set))) >= lra_constraint_new_regno_start
6735 && reg_renumber[regno] < 0
6736 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
6738 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6739 reloads_num++;
6740 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6741 potential_reload_hard_regs |= reg_class_contents[cl];
6744 if (NONDEBUG_INSN_P (curr_insn))
6746 int regno;
6748 /* Invalidate invariants with changed regs. */
6749 curr_id = lra_get_insn_recog_data (curr_insn);
6750 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6751 if (reg->type != OP_IN)
6753 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6754 bitmap_set_bit (&invalid_invariant_regs,
6755 ORIGINAL_REGNO (regno_reg_rtx[reg->regno]));
6757 curr_static_id = curr_id->insn_static_data;
6758 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6759 if (reg->type != OP_IN)
6760 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6761 if (curr_id->arg_hard_regs != NULL)
6762 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6763 if (regno >= FIRST_PSEUDO_REGISTER)
6764 bitmap_set_bit (&invalid_invariant_regs,
6765 regno - FIRST_PSEUDO_REGISTER);
6767 /* We reached the start of the current basic block. */
6768 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
6769 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
6771 /* We reached the beginning of the current block -- do
6772 rest of spliting in the current BB. */
6773 to_process = df_get_live_in (curr_bb);
6774 if (BLOCK_FOR_INSN (head) != curr_bb)
6776 /* We are somewhere in the middle of EBB. */
6777 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
6778 curr_bb, &temp_bitmap);
6779 to_process = &temp_bitmap;
6781 head_p = true;
6782 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
6784 if ((int) j >= lra_constraint_new_regno_start)
6785 break;
6786 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
6787 && usage_insns[j].check == curr_usage_insns_check
6788 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
6790 if (need_for_split_p (potential_reload_hard_regs, j))
6792 if (lra_dump_file != NULL && head_p)
6794 fprintf (lra_dump_file,
6795 " ----------------------------------\n");
6796 head_p = false;
6798 if (split_reg (false, j, bb_note (curr_bb),
6799 next_usage_insns, NULL))
6800 change_p = true;
6802 usage_insns[j].check = 0;
6807 return change_p;
6810 /* This value affects EBB forming. If probability of edge from EBB to
6811 a BB is not greater than the following value, we don't add the BB
6812 to EBB. */
6813 #define EBB_PROBABILITY_CUTOFF \
6814 ((REG_BR_PROB_BASE * param_lra_inheritance_ebb_probability_cutoff) / 100)
6816 /* Current number of inheritance/split iteration. */
6817 int lra_inheritance_iter;
6819 /* Entry function for inheritance/split pass. */
6820 void
6821 lra_inheritance (void)
6823 int i;
6824 basic_block bb, start_bb;
6825 edge e;
6827 lra_inheritance_iter++;
6828 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6829 return;
6830 timevar_push (TV_LRA_INHERITANCE);
6831 if (lra_dump_file != NULL)
6832 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
6833 lra_inheritance_iter);
6834 curr_usage_insns_check = 0;
6835 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
6836 for (i = 0; i < lra_constraint_new_regno_start; i++)
6837 usage_insns[i].check = 0;
6838 bitmap_initialize (&check_only_regs, &reg_obstack);
6839 bitmap_initialize (&invalid_invariant_regs, &reg_obstack);
6840 bitmap_initialize (&live_regs, &reg_obstack);
6841 bitmap_initialize (&temp_bitmap, &reg_obstack);
6842 bitmap_initialize (&ebb_global_regs, &reg_obstack);
6843 FOR_EACH_BB_FN (bb, cfun)
6845 start_bb = bb;
6846 if (lra_dump_file != NULL)
6847 fprintf (lra_dump_file, "EBB");
6848 /* Form a EBB starting with BB. */
6849 bitmap_clear (&ebb_global_regs);
6850 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
6851 for (;;)
6853 if (lra_dump_file != NULL)
6854 fprintf (lra_dump_file, " %d", bb->index);
6855 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
6856 || LABEL_P (BB_HEAD (bb->next_bb)))
6857 break;
6858 e = find_fallthru_edge (bb->succs);
6859 if (! e)
6860 break;
6861 if (e->probability.initialized_p ()
6862 && e->probability.to_reg_br_prob_base () < EBB_PROBABILITY_CUTOFF)
6863 break;
6864 bb = bb->next_bb;
6866 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
6867 if (lra_dump_file != NULL)
6868 fprintf (lra_dump_file, "\n");
6869 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
6870 /* Remember that the EBB head and tail can change in
6871 inherit_in_ebb. */
6872 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
6874 bitmap_release (&ebb_global_regs);
6875 bitmap_release (&temp_bitmap);
6876 bitmap_release (&live_regs);
6877 bitmap_release (&invalid_invariant_regs);
6878 bitmap_release (&check_only_regs);
6879 free (usage_insns);
6881 timevar_pop (TV_LRA_INHERITANCE);
6886 /* This page contains code to undo failed inheritance/split
6887 transformations. */
6889 /* Current number of iteration undoing inheritance/split. */
6890 int lra_undo_inheritance_iter;
6892 /* Fix BB live info LIVE after removing pseudos created on pass doing
6893 inheritance/split which are REMOVED_PSEUDOS. */
6894 static void
6895 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
6897 unsigned int regno;
6898 bitmap_iterator bi;
6900 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
6901 if (bitmap_clear_bit (live, regno)
6902 && REG_P (lra_reg_info[regno].restore_rtx))
6903 bitmap_set_bit (live, REGNO (lra_reg_info[regno].restore_rtx));
6906 /* Return regno of the (subreg of) REG. Otherwise, return a negative
6907 number. */
6908 static int
6909 get_regno (rtx reg)
6911 if (GET_CODE (reg) == SUBREG)
6912 reg = SUBREG_REG (reg);
6913 if (REG_P (reg))
6914 return REGNO (reg);
6915 return -1;
6918 /* Delete a move INSN with destination reg DREGNO and a previous
6919 clobber insn with the same regno. The inheritance/split code can
6920 generate moves with preceding clobber and when we delete such moves
6921 we should delete the clobber insn too to keep the correct life
6922 info. */
6923 static void
6924 delete_move_and_clobber (rtx_insn *insn, int dregno)
6926 rtx_insn *prev_insn = PREV_INSN (insn);
6928 lra_set_insn_deleted (insn);
6929 lra_assert (dregno >= 0);
6930 if (prev_insn != NULL && NONDEBUG_INSN_P (prev_insn)
6931 && GET_CODE (PATTERN (prev_insn)) == CLOBBER
6932 && dregno == get_regno (XEXP (PATTERN (prev_insn), 0)))
6933 lra_set_insn_deleted (prev_insn);
6936 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
6937 return true if we did any change. The undo transformations for
6938 inheritance looks like
6939 i <- i2
6940 p <- i => p <- i2
6941 or removing
6942 p <- i, i <- p, and i <- i3
6943 where p is original pseudo from which inheritance pseudo i was
6944 created, i and i3 are removed inheritance pseudos, i2 is another
6945 not removed inheritance pseudo. All split pseudos or other
6946 occurrences of removed inheritance pseudos are changed on the
6947 corresponding original pseudos.
6949 The function also schedules insns changed and created during
6950 inheritance/split pass for processing by the subsequent constraint
6951 pass. */
6952 static bool
6953 remove_inheritance_pseudos (bitmap remove_pseudos)
6955 basic_block bb;
6956 int regno, sregno, prev_sregno, dregno;
6957 rtx restore_rtx;
6958 rtx set, prev_set;
6959 rtx_insn *prev_insn;
6960 bool change_p, done_p;
6962 change_p = ! bitmap_empty_p (remove_pseudos);
6963 /* We cannot finish the function right away if CHANGE_P is true
6964 because we need to marks insns affected by previous
6965 inheritance/split pass for processing by the subsequent
6966 constraint pass. */
6967 FOR_EACH_BB_FN (bb, cfun)
6969 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
6970 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
6971 FOR_BB_INSNS_REVERSE (bb, curr_insn)
6973 if (! INSN_P (curr_insn))
6974 continue;
6975 done_p = false;
6976 sregno = dregno = -1;
6977 if (change_p && NONDEBUG_INSN_P (curr_insn)
6978 && (set = single_set (curr_insn)) != NULL_RTX)
6980 dregno = get_regno (SET_DEST (set));
6981 sregno = get_regno (SET_SRC (set));
6984 if (sregno >= 0 && dregno >= 0)
6986 if (bitmap_bit_p (remove_pseudos, dregno)
6987 && ! REG_P (lra_reg_info[dregno].restore_rtx))
6989 /* invariant inheritance pseudo <- original pseudo */
6990 if (lra_dump_file != NULL)
6992 fprintf (lra_dump_file, " Removing invariant inheritance:\n");
6993 dump_insn_slim (lra_dump_file, curr_insn);
6994 fprintf (lra_dump_file, "\n");
6996 delete_move_and_clobber (curr_insn, dregno);
6997 done_p = true;
6999 else if (bitmap_bit_p (remove_pseudos, sregno)
7000 && ! REG_P (lra_reg_info[sregno].restore_rtx))
7002 /* reload pseudo <- invariant inheritance pseudo */
7003 start_sequence ();
7004 /* We cannot just change the source. It might be
7005 an insn different from the move. */
7006 emit_insn (lra_reg_info[sregno].restore_rtx);
7007 rtx_insn *new_insns = get_insns ();
7008 end_sequence ();
7009 lra_assert (single_set (new_insns) != NULL
7010 && SET_DEST (set) == SET_DEST (single_set (new_insns)));
7011 lra_process_new_insns (curr_insn, NULL, new_insns,
7012 "Changing reload<-invariant inheritance");
7013 delete_move_and_clobber (curr_insn, dregno);
7014 done_p = true;
7016 else if ((bitmap_bit_p (remove_pseudos, sregno)
7017 && (get_regno (lra_reg_info[sregno].restore_rtx) == dregno
7018 || (bitmap_bit_p (remove_pseudos, dregno)
7019 && get_regno (lra_reg_info[sregno].restore_rtx) >= 0
7020 && (get_regno (lra_reg_info[sregno].restore_rtx)
7021 == get_regno (lra_reg_info[dregno].restore_rtx)))))
7022 || (bitmap_bit_p (remove_pseudos, dregno)
7023 && get_regno (lra_reg_info[dregno].restore_rtx) == sregno))
7024 /* One of the following cases:
7025 original <- removed inheritance pseudo
7026 removed inherit pseudo <- another removed inherit pseudo
7027 removed inherit pseudo <- original pseudo
7029 removed_split_pseudo <- original_reg
7030 original_reg <- removed_split_pseudo */
7032 if (lra_dump_file != NULL)
7034 fprintf (lra_dump_file, " Removing %s:\n",
7035 bitmap_bit_p (&lra_split_regs, sregno)
7036 || bitmap_bit_p (&lra_split_regs, dregno)
7037 ? "split" : "inheritance");
7038 dump_insn_slim (lra_dump_file, curr_insn);
7040 delete_move_and_clobber (curr_insn, dregno);
7041 done_p = true;
7043 else if (bitmap_bit_p (remove_pseudos, sregno)
7044 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
7046 /* Search the following pattern:
7047 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
7048 original_pseudo <- inherit_or_split_pseudo1
7049 where the 2nd insn is the current insn and
7050 inherit_or_split_pseudo2 is not removed. If it is found,
7051 change the current insn onto:
7052 original_pseudo <- inherit_or_split_pseudo2. */
7053 for (prev_insn = PREV_INSN (curr_insn);
7054 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
7055 prev_insn = PREV_INSN (prev_insn))
7057 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
7058 && (prev_set = single_set (prev_insn)) != NULL_RTX
7059 /* There should be no subregs in insn we are
7060 searching because only the original reg might
7061 be in subreg when we changed the mode of
7062 load/store for splitting. */
7063 && REG_P (SET_DEST (prev_set))
7064 && REG_P (SET_SRC (prev_set))
7065 && (int) REGNO (SET_DEST (prev_set)) == sregno
7066 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
7067 >= FIRST_PSEUDO_REGISTER)
7068 && (lra_reg_info[prev_sregno].restore_rtx == NULL_RTX
7070 /* As we consider chain of inheritance or
7071 splitting described in above comment we should
7072 check that sregno and prev_sregno were
7073 inheritance/split pseudos created from the
7074 same original regno. */
7075 (get_regno (lra_reg_info[sregno].restore_rtx) >= 0
7076 && (get_regno (lra_reg_info[sregno].restore_rtx)
7077 == get_regno (lra_reg_info[prev_sregno].restore_rtx))))
7078 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
7080 lra_assert (GET_MODE (SET_SRC (prev_set))
7081 == GET_MODE (regno_reg_rtx[sregno]));
7082 /* Although we have a single set, the insn can
7083 contain more one sregno register occurrence
7084 as a source. Change all occurrences. */
7085 lra_substitute_pseudo_within_insn (curr_insn, sregno,
7086 SET_SRC (prev_set),
7087 false);
7088 /* As we are finishing with processing the insn
7089 here, check the destination too as it might
7090 inheritance pseudo for another pseudo. */
7091 if (bitmap_bit_p (remove_pseudos, dregno)
7092 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
7093 && (restore_rtx
7094 = lra_reg_info[dregno].restore_rtx) != NULL_RTX)
7096 if (GET_CODE (SET_DEST (set)) == SUBREG)
7097 SUBREG_REG (SET_DEST (set)) = restore_rtx;
7098 else
7099 SET_DEST (set) = restore_rtx;
7101 lra_push_insn_and_update_insn_regno_info (curr_insn);
7102 lra_set_used_insn_alternative_by_uid
7103 (INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
7104 done_p = true;
7105 if (lra_dump_file != NULL)
7107 fprintf (lra_dump_file, " Change reload insn:\n");
7108 dump_insn_slim (lra_dump_file, curr_insn);
7113 if (! done_p)
7115 struct lra_insn_reg *reg;
7116 bool restored_regs_p = false;
7117 bool kept_regs_p = false;
7119 curr_id = lra_get_insn_recog_data (curr_insn);
7120 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
7122 regno = reg->regno;
7123 restore_rtx = lra_reg_info[regno].restore_rtx;
7124 if (restore_rtx != NULL_RTX)
7126 if (change_p && bitmap_bit_p (remove_pseudos, regno))
7128 lra_substitute_pseudo_within_insn
7129 (curr_insn, regno, restore_rtx, false);
7130 restored_regs_p = true;
7132 else
7133 kept_regs_p = true;
7136 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
7138 /* The instruction has changed since the previous
7139 constraints pass. */
7140 lra_push_insn_and_update_insn_regno_info (curr_insn);
7141 lra_set_used_insn_alternative_by_uid
7142 (INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
7144 else if (restored_regs_p)
7145 /* The instruction has been restored to the form that
7146 it had during the previous constraints pass. */
7147 lra_update_insn_regno_info (curr_insn);
7148 if (restored_regs_p && lra_dump_file != NULL)
7150 fprintf (lra_dump_file, " Insn after restoring regs:\n");
7151 dump_insn_slim (lra_dump_file, curr_insn);
7156 return change_p;
7159 /* If optional reload pseudos failed to get a hard register or was not
7160 inherited, it is better to remove optional reloads. We do this
7161 transformation after undoing inheritance to figure out necessity to
7162 remove optional reloads easier. Return true if we do any
7163 change. */
7164 static bool
7165 undo_optional_reloads (void)
7167 bool change_p, keep_p;
7168 unsigned int regno, uid;
7169 bitmap_iterator bi, bi2;
7170 rtx_insn *insn;
7171 rtx set, src, dest;
7172 auto_bitmap removed_optional_reload_pseudos (&reg_obstack);
7174 bitmap_copy (removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
7175 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7177 keep_p = false;
7178 /* Keep optional reloads from previous subpasses. */
7179 if (lra_reg_info[regno].restore_rtx == NULL_RTX
7180 /* If the original pseudo changed its allocation, just
7181 removing the optional pseudo is dangerous as the original
7182 pseudo will have longer live range. */
7183 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] >= 0)
7184 keep_p = true;
7185 else if (reg_renumber[regno] >= 0)
7186 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
7188 insn = lra_insn_recog_data[uid]->insn;
7189 if ((set = single_set (insn)) == NULL_RTX)
7190 continue;
7191 src = SET_SRC (set);
7192 dest = SET_DEST (set);
7193 if (! REG_P (src) || ! REG_P (dest))
7194 continue;
7195 if (REGNO (dest) == regno
7196 /* Ignore insn for optional reloads itself. */
7197 && REGNO (lra_reg_info[regno].restore_rtx) != REGNO (src)
7198 /* Check only inheritance on last inheritance pass. */
7199 && (int) REGNO (src) >= new_regno_start
7200 /* Check that the optional reload was inherited. */
7201 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
7203 keep_p = true;
7204 break;
7207 if (keep_p)
7209 bitmap_clear_bit (removed_optional_reload_pseudos, regno);
7210 if (lra_dump_file != NULL)
7211 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
7214 change_p = ! bitmap_empty_p (removed_optional_reload_pseudos);
7215 auto_bitmap insn_bitmap (&reg_obstack);
7216 EXECUTE_IF_SET_IN_BITMAP (removed_optional_reload_pseudos, 0, regno, bi)
7218 if (lra_dump_file != NULL)
7219 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
7220 bitmap_copy (insn_bitmap, &lra_reg_info[regno].insn_bitmap);
7221 EXECUTE_IF_SET_IN_BITMAP (insn_bitmap, 0, uid, bi2)
7223 insn = lra_insn_recog_data[uid]->insn;
7224 if ((set = single_set (insn)) != NULL_RTX)
7226 src = SET_SRC (set);
7227 dest = SET_DEST (set);
7228 if (REG_P (src) && REG_P (dest)
7229 && ((REGNO (src) == regno
7230 && (REGNO (lra_reg_info[regno].restore_rtx)
7231 == REGNO (dest)))
7232 || (REGNO (dest) == regno
7233 && (REGNO (lra_reg_info[regno].restore_rtx)
7234 == REGNO (src)))))
7236 if (lra_dump_file != NULL)
7238 fprintf (lra_dump_file, " Deleting move %u\n",
7239 INSN_UID (insn));
7240 dump_insn_slim (lra_dump_file, insn);
7242 delete_move_and_clobber (insn, REGNO (dest));
7243 continue;
7245 /* We should not worry about generation memory-memory
7246 moves here as if the corresponding inheritance did
7247 not work (inheritance pseudo did not get a hard reg),
7248 we remove the inheritance pseudo and the optional
7249 reload. */
7251 lra_substitute_pseudo_within_insn
7252 (insn, regno, lra_reg_info[regno].restore_rtx, false);
7253 lra_update_insn_regno_info (insn);
7254 if (lra_dump_file != NULL)
7256 fprintf (lra_dump_file,
7257 " Restoring original insn:\n");
7258 dump_insn_slim (lra_dump_file, insn);
7262 /* Clear restore_regnos. */
7263 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7264 lra_reg_info[regno].restore_rtx = NULL_RTX;
7265 return change_p;
7268 /* Entry function for undoing inheritance/split transformation. Return true
7269 if we did any RTL change in this pass. */
7270 bool
7271 lra_undo_inheritance (void)
7273 unsigned int regno;
7274 int hard_regno;
7275 int n_all_inherit, n_inherit, n_all_split, n_split;
7276 rtx restore_rtx;
7277 bitmap_iterator bi;
7278 bool change_p;
7280 lra_undo_inheritance_iter++;
7281 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
7282 return false;
7283 if (lra_dump_file != NULL)
7284 fprintf (lra_dump_file,
7285 "\n********** Undoing inheritance #%d: **********\n\n",
7286 lra_undo_inheritance_iter);
7287 auto_bitmap remove_pseudos (&reg_obstack);
7288 n_inherit = n_all_inherit = 0;
7289 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7290 if (lra_reg_info[regno].restore_rtx != NULL_RTX)
7292 n_all_inherit++;
7293 if (reg_renumber[regno] < 0
7294 /* If the original pseudo changed its allocation, just
7295 removing inheritance is dangerous as for changing
7296 allocation we used shorter live-ranges. */
7297 && (! REG_P (lra_reg_info[regno].restore_rtx)
7298 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] < 0))
7299 bitmap_set_bit (remove_pseudos, regno);
7300 else
7301 n_inherit++;
7303 if (lra_dump_file != NULL && n_all_inherit != 0)
7304 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
7305 n_inherit, n_all_inherit,
7306 (double) n_inherit / n_all_inherit * 100);
7307 n_split = n_all_split = 0;
7308 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7309 if ((restore_rtx = lra_reg_info[regno].restore_rtx) != NULL_RTX)
7311 int restore_regno = REGNO (restore_rtx);
7313 n_all_split++;
7314 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
7315 ? reg_renumber[restore_regno] : restore_regno);
7316 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
7317 bitmap_set_bit (remove_pseudos, regno);
7318 else
7320 n_split++;
7321 if (lra_dump_file != NULL)
7322 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
7323 regno, restore_regno);
7326 if (lra_dump_file != NULL && n_all_split != 0)
7327 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
7328 n_split, n_all_split,
7329 (double) n_split / n_all_split * 100);
7330 change_p = remove_inheritance_pseudos (remove_pseudos);
7331 /* Clear restore_regnos. */
7332 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7333 lra_reg_info[regno].restore_rtx = NULL_RTX;
7334 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7335 lra_reg_info[regno].restore_rtx = NULL_RTX;
7336 change_p = undo_optional_reloads () || change_p;
7337 return change_p;