Fix gnat.dg/opt39.adb on hppa.
[official-gcc.git] / gcc / lra-constraints.cc
blobff4e8f06063682e4e240701e389e5014323ea0e8
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2023 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains code for 3 passes: constraint pass,
23 inheritance/split pass, and pass for undoing failed inheritance and
24 split.
26 The major goal of constraint pass is to transform RTL to satisfy
27 insn and address constraints by:
28 o choosing insn alternatives;
29 o generating *reload insns* (or reloads in brief) and *reload
30 pseudos* which will get necessary hard registers later;
31 o substituting pseudos with equivalent values and removing the
32 instructions that initialized those pseudos.
34 The constraint pass has biggest and most complicated code in LRA.
35 There are a lot of important details like:
36 o reuse of input reload pseudos to simplify reload pseudo
37 allocations;
38 o some heuristics to choose insn alternative to improve the
39 inheritance;
40 o early clobbers etc.
42 The pass is mimicking former reload pass in alternative choosing
43 because the reload pass is oriented to current machine description
44 model. It might be changed if the machine description model is
45 changed.
47 There is special code for preventing all LRA and this pass cycling
48 in case of bugs.
50 On the first iteration of the pass we process every instruction and
51 choose an alternative for each one. On subsequent iterations we try
52 to avoid reprocessing instructions if we can be sure that the old
53 choice is still valid.
55 The inheritance/spilt pass is to transform code to achieve
56 ineheritance and live range splitting. It is done on backward
57 traversal of EBBs.
59 The inheritance optimization goal is to reuse values in hard
60 registers. There is analogous optimization in old reload pass. The
61 inheritance is achieved by following transformation:
63 reload_p1 <- p reload_p1 <- p
64 ... new_p <- reload_p1
65 ... => ...
66 reload_p2 <- p reload_p2 <- new_p
68 where p is spilled and not changed between the insns. Reload_p1 is
69 also called *original pseudo* and new_p is called *inheritance
70 pseudo*.
72 The subsequent assignment pass will try to assign the same (or
73 another if it is not possible) hard register to new_p as to
74 reload_p1 or reload_p2.
76 If the assignment pass fails to assign a hard register to new_p,
77 this file will undo the inheritance and restore the original code.
78 This is because implementing the above sequence with a spilled
79 new_p would make the code much worse. The inheritance is done in
80 EBB scope. The above is just a simplified example to get an idea
81 of the inheritance as the inheritance is also done for non-reload
82 insns.
84 Splitting (transformation) is also done in EBB scope on the same
85 pass as the inheritance:
87 r <- ... or ... <- r r <- ... or ... <- r
88 ... s <- r (new insn -- save)
89 ... =>
90 ... r <- s (new insn -- restore)
91 ... <- r ... <- r
93 The *split pseudo* s is assigned to the hard register of the
94 original pseudo or hard register r.
96 Splitting is done:
97 o In EBBs with high register pressure for global pseudos (living
98 in at least 2 BBs) and assigned to hard registers when there
99 are more one reloads needing the hard registers;
100 o for pseudos needing save/restore code around calls.
102 If the split pseudo still has the same hard register as the
103 original pseudo after the subsequent assignment pass or the
104 original pseudo was split, the opposite transformation is done on
105 the same pass for undoing inheritance. */
107 #undef REG_OK_STRICT
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "backend.h"
113 #include "hooks.h"
114 #include "target.h"
115 #include "rtl.h"
116 #include "tree.h"
117 #include "predict.h"
118 #include "df.h"
119 #include "memmodel.h"
120 #include "tm_p.h"
121 #include "expmed.h"
122 #include "optabs.h"
123 #include "regs.h"
124 #include "ira.h"
125 #include "recog.h"
126 #include "output.h"
127 #include "addresses.h"
128 #include "expr.h"
129 #include "cfgrtl.h"
130 #include "rtl-error.h"
131 #include "lra.h"
132 #include "lra-int.h"
133 #include "print-rtl.h"
134 #include "function-abi.h"
135 #include "rtl-iter.h"
137 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
138 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
139 reload insns. */
140 static int bb_reload_num;
142 /* The current insn being processed and corresponding its single set
143 (NULL otherwise), its data (basic block, the insn data, the insn
144 static data, and the mode of each operand). */
145 static rtx_insn *curr_insn;
146 static rtx curr_insn_set;
147 static basic_block curr_bb;
148 static lra_insn_recog_data_t curr_id;
149 static struct lra_static_insn_data *curr_static_id;
150 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
151 /* Mode of the register substituted by its equivalence with VOIDmode
152 (e.g. constant) and whose subreg is given operand of the current
153 insn. VOIDmode in all other cases. */
154 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
158 /* Start numbers for new registers and insns at the current constraints
159 pass start. */
160 static int new_regno_start;
161 static int new_insn_uid_start;
163 /* If LOC is nonnull, strip any outer subreg from it. */
164 static inline rtx *
165 strip_subreg (rtx *loc)
167 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
170 /* Return hard regno of REGNO or if it is was not assigned to a hard
171 register, use a hard register from its allocno class. */
172 static int
173 get_try_hard_regno (int regno)
175 int hard_regno;
176 enum reg_class rclass;
178 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
179 hard_regno = lra_get_regno_hard_regno (regno);
180 if (hard_regno >= 0)
181 return hard_regno;
182 rclass = lra_get_allocno_class (regno);
183 if (rclass == NO_REGS)
184 return -1;
185 return ira_class_hard_regs[rclass][0];
188 /* Return the hard regno of X after removing its subreg. If X is not a
189 register or a subreg of a register, return -1. If X is a pseudo, use its
190 assignment. If X is a hard regno, return the final hard regno which will be
191 after elimination. */
192 static int
193 get_hard_regno (rtx x)
195 rtx reg;
196 int hard_regno;
198 reg = x;
199 if (SUBREG_P (x))
200 reg = SUBREG_REG (x);
201 if (! REG_P (reg))
202 return -1;
203 if (! HARD_REGISTER_NUM_P (hard_regno = REGNO (reg)))
204 hard_regno = lra_get_regno_hard_regno (hard_regno);
205 if (hard_regno < 0)
206 return -1;
207 if (HARD_REGISTER_NUM_P (REGNO (reg)))
208 hard_regno = lra_get_elimination_hard_regno (hard_regno);
209 if (SUBREG_P (x))
210 hard_regno += subreg_regno_offset (hard_regno, GET_MODE (reg),
211 SUBREG_BYTE (x), GET_MODE (x));
212 return hard_regno;
215 /* If REGNO is a hard register or has been allocated a hard register,
216 return the class of that register. If REGNO is a reload pseudo
217 created by the current constraints pass, return its allocno class.
218 Return NO_REGS otherwise. */
219 static enum reg_class
220 get_reg_class (int regno)
222 int hard_regno;
224 if (! HARD_REGISTER_NUM_P (hard_regno = regno))
225 hard_regno = lra_get_regno_hard_regno (regno);
226 if (hard_regno >= 0)
228 hard_regno = lra_get_elimination_hard_regno (hard_regno);
229 return REGNO_REG_CLASS (hard_regno);
231 if (regno >= new_regno_start)
232 return lra_get_allocno_class (regno);
233 return NO_REGS;
236 /* Return true if REG satisfies (or will satisfy) reg class constraint
237 CL. Use elimination first if REG is a hard register. If REG is a
238 reload pseudo created by this constraints pass, assume that it will
239 be allocated a hard register from its allocno class, but allow that
240 class to be narrowed to CL if it is currently a superset of CL and
241 if either:
243 - ALLOW_ALL_RELOAD_CLASS_CHANGES_P is true or
244 - the instruction we're processing is not a reload move.
246 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
247 REGNO (reg), or NO_REGS if no change in its class was needed. */
248 static bool
249 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class,
250 bool allow_all_reload_class_changes_p = false)
252 enum reg_class rclass, common_class;
253 machine_mode reg_mode;
254 rtx src;
255 int class_size, hard_regno, nregs, i, j;
256 int regno = REGNO (reg);
258 if (new_class != NULL)
259 *new_class = NO_REGS;
260 if (regno < FIRST_PSEUDO_REGISTER)
262 rtx final_reg = reg;
263 rtx *final_loc = &final_reg;
265 lra_eliminate_reg_if_possible (final_loc);
266 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
268 reg_mode = GET_MODE (reg);
269 rclass = get_reg_class (regno);
270 src = curr_insn_set != NULL ? SET_SRC (curr_insn_set) : NULL;
271 if (regno < new_regno_start
272 /* Do not allow the constraints for reload instructions to
273 influence the classes of new pseudos. These reloads are
274 typically moves that have many alternatives, and restricting
275 reload pseudos for one alternative may lead to situations
276 where other reload pseudos are no longer allocatable. */
277 || (!allow_all_reload_class_changes_p
278 && INSN_UID (curr_insn) >= new_insn_uid_start
279 && src != NULL
280 && ((REG_P (src) || MEM_P (src))
281 || (GET_CODE (src) == SUBREG
282 && (REG_P (SUBREG_REG (src)) || MEM_P (SUBREG_REG (src)))))))
283 /* When we don't know what class will be used finally for reload
284 pseudos, we use ALL_REGS. */
285 return ((regno >= new_regno_start && rclass == ALL_REGS)
286 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
287 && ! hard_reg_set_subset_p (reg_class_contents[cl],
288 lra_no_alloc_regs)));
289 else
291 common_class = ira_reg_class_subset[rclass][cl];
292 if (new_class != NULL)
293 *new_class = common_class;
294 if (hard_reg_set_subset_p (reg_class_contents[common_class],
295 lra_no_alloc_regs))
296 return false;
297 /* Check that there are enough allocatable regs. */
298 class_size = ira_class_hard_regs_num[common_class];
299 for (i = 0; i < class_size; i++)
301 hard_regno = ira_class_hard_regs[common_class][i];
302 nregs = hard_regno_nregs (hard_regno, reg_mode);
303 if (nregs == 1)
304 return true;
305 for (j = 0; j < nregs; j++)
306 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
307 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
308 hard_regno + j))
309 break;
310 if (j >= nregs)
311 return true;
313 return false;
317 /* Return true if REGNO satisfies a memory constraint. */
318 static bool
319 in_mem_p (int regno)
321 return get_reg_class (regno) == NO_REGS;
324 /* Return 1 if ADDR is a valid memory address for mode MODE in address
325 space AS, and check that each pseudo has the proper kind of hard
326 reg. */
327 static int
328 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
329 rtx addr, addr_space_t as)
331 #ifdef GO_IF_LEGITIMATE_ADDRESS
332 lra_assert (ADDR_SPACE_GENERIC_P (as));
333 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
334 return 0;
336 win:
337 return 1;
338 #else
339 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
340 #endif
343 namespace {
344 /* Temporarily eliminates registers in an address (for the lifetime of
345 the object). */
346 class address_eliminator {
347 public:
348 address_eliminator (struct address_info *ad);
349 ~address_eliminator ();
351 private:
352 struct address_info *m_ad;
353 rtx *m_base_loc;
354 rtx m_base_reg;
355 rtx *m_index_loc;
356 rtx m_index_reg;
360 address_eliminator::address_eliminator (struct address_info *ad)
361 : m_ad (ad),
362 m_base_loc (strip_subreg (ad->base_term)),
363 m_base_reg (NULL_RTX),
364 m_index_loc (strip_subreg (ad->index_term)),
365 m_index_reg (NULL_RTX)
367 if (m_base_loc != NULL)
369 m_base_reg = *m_base_loc;
370 /* If we have non-legitimate address which is decomposed not in
371 the way we expected, don't do elimination here. In such case
372 the address will be reloaded and elimination will be done in
373 reload insn finally. */
374 if (REG_P (m_base_reg))
375 lra_eliminate_reg_if_possible (m_base_loc);
376 if (m_ad->base_term2 != NULL)
377 *m_ad->base_term2 = *m_ad->base_term;
379 if (m_index_loc != NULL)
381 m_index_reg = *m_index_loc;
382 if (REG_P (m_index_reg))
383 lra_eliminate_reg_if_possible (m_index_loc);
387 address_eliminator::~address_eliminator ()
389 if (m_base_loc && *m_base_loc != m_base_reg)
391 *m_base_loc = m_base_reg;
392 if (m_ad->base_term2 != NULL)
393 *m_ad->base_term2 = *m_ad->base_term;
395 if (m_index_loc && *m_index_loc != m_index_reg)
396 *m_index_loc = m_index_reg;
399 /* Return true if the eliminated form of AD is a legitimate target address.
400 If OP is a MEM, AD is the address within OP, otherwise OP should be
401 ignored. CONSTRAINT is one constraint that the operand may need
402 to meet. */
403 static bool
404 valid_address_p (rtx op, struct address_info *ad,
405 enum constraint_num constraint)
407 address_eliminator eliminator (ad);
409 /* Allow a memory OP if it matches CONSTRAINT, even if CONSTRAINT is more
410 forgiving than "m".
411 Need to extract memory from op for special memory constraint,
412 i.e. bcst_mem_operand in i386 backend. */
413 if (MEM_P (extract_mem_from_operand (op))
414 && insn_extra_relaxed_memory_constraint (constraint)
415 && constraint_satisfied_p (op, constraint))
416 return true;
418 return valid_address_p (ad->mode, *ad->outer, ad->as);
421 /* For special_memory_operand, it could be false for MEM_P (op),
422 i.e. bcst_mem_operand in i386 backend.
423 Extract and return real memory operand or op. */
425 extract_mem_from_operand (rtx op)
427 for (rtx x = op;; x = XEXP (x, 0))
429 if (MEM_P (x))
430 return x;
431 if (GET_RTX_LENGTH (GET_CODE (x)) != 1
432 || GET_RTX_FORMAT (GET_CODE (x))[0] != 'e')
433 break;
435 return op;
438 /* Return true if the eliminated form of memory reference OP satisfies
439 extra (special) memory constraint CONSTRAINT. */
440 static bool
441 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
443 struct address_info ad;
444 rtx mem = extract_mem_from_operand (op);
445 if (!MEM_P (mem))
446 return false;
448 decompose_mem_address (&ad, mem);
449 address_eliminator eliminator (&ad);
450 return constraint_satisfied_p (op, constraint);
453 /* Return true if the eliminated form of address AD satisfies extra
454 address constraint CONSTRAINT. */
455 static bool
456 satisfies_address_constraint_p (struct address_info *ad,
457 enum constraint_num constraint)
459 address_eliminator eliminator (ad);
460 return constraint_satisfied_p (*ad->outer, constraint);
463 /* Return true if the eliminated form of address OP satisfies extra
464 address constraint CONSTRAINT. */
465 static bool
466 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
468 struct address_info ad;
470 decompose_lea_address (&ad, &op);
471 return satisfies_address_constraint_p (&ad, constraint);
474 /* Initiate equivalences for LRA. As we keep original equivalences
475 before any elimination, we need to make copies otherwise any change
476 in insns might change the equivalences. */
477 void
478 lra_init_equiv (void)
480 ira_expand_reg_equiv ();
481 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
483 rtx res;
485 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
486 ira_reg_equiv[i].memory = copy_rtx (res);
487 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
488 ira_reg_equiv[i].invariant = copy_rtx (res);
492 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
494 /* Update equivalence for REGNO. We need to this as the equivalence
495 might contain other pseudos which are changed by their
496 equivalences. */
497 static void
498 update_equiv (int regno)
500 rtx x;
502 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
503 ira_reg_equiv[regno].memory
504 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
505 NULL_RTX);
506 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
507 ira_reg_equiv[regno].invariant
508 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
509 NULL_RTX);
512 /* If we have decided to substitute X with another value, return that
513 value, otherwise return X. */
514 static rtx
515 get_equiv (rtx x)
517 int regno;
518 rtx res;
520 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
521 || ! ira_reg_equiv[regno].defined_p
522 || ! ira_reg_equiv[regno].profitable_p
523 || lra_get_regno_hard_regno (regno) >= 0)
524 return x;
525 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
527 if (targetm.cannot_substitute_mem_equiv_p (res))
528 return x;
529 return res;
531 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
532 return res;
533 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
534 return res;
535 gcc_unreachable ();
538 /* If we have decided to substitute X with the equivalent value,
539 return that value after elimination for INSN, otherwise return
540 X. */
541 static rtx
542 get_equiv_with_elimination (rtx x, rtx_insn *insn)
544 rtx res = get_equiv (x);
546 if (x == res || CONSTANT_P (res))
547 return res;
548 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
549 false, false, 0, true);
552 /* Set up curr_operand_mode. */
553 static void
554 init_curr_operand_mode (void)
556 int nop = curr_static_id->n_operands;
557 for (int i = 0; i < nop; i++)
559 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
560 if (mode == VOIDmode)
562 /* The .md mode for address operands is the mode of the
563 addressed value rather than the mode of the address itself. */
564 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
565 mode = Pmode;
566 else
567 mode = curr_static_id->operand[i].mode;
569 curr_operand_mode[i] = mode;
575 /* The page contains code to reuse input reloads. */
577 /* Structure describes input reload of the current insns. */
578 struct input_reload
580 /* True for input reload of matched operands. */
581 bool match_p;
582 /* Reloaded value. */
583 rtx input;
584 /* Reload pseudo used. */
585 rtx reg;
588 /* The number of elements in the following array. */
589 static int curr_insn_input_reloads_num;
590 /* Array containing info about input reloads. It is used to find the
591 same input reload and reuse the reload pseudo in this case. */
592 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
594 /* Initiate data concerning reuse of input reloads for the current
595 insn. */
596 static void
597 init_curr_insn_input_reloads (void)
599 curr_insn_input_reloads_num = 0;
602 /* The canonical form of an rtx inside a MEM is not necessarily the same as the
603 canonical form of the rtx outside the MEM. Fix this up in the case that
604 we're reloading an address (and therefore pulling it outside a MEM). */
605 static rtx
606 canonicalize_reload_addr (rtx addr)
608 subrtx_var_iterator::array_type array;
609 FOR_EACH_SUBRTX_VAR (iter, array, addr, NONCONST)
611 rtx x = *iter;
612 if (GET_CODE (x) == MULT && CONST_INT_P (XEXP (x, 1)))
614 const HOST_WIDE_INT ci = INTVAL (XEXP (x, 1));
615 const int pwr2 = exact_log2 (ci);
616 if (pwr2 > 0)
618 /* Rewrite this to use a shift instead, which is canonical when
619 outside of a MEM. */
620 PUT_CODE (x, ASHIFT);
621 XEXP (x, 1) = GEN_INT (pwr2);
626 return addr;
629 /* Create a new pseudo using MODE, RCLASS, EXCLUDE_START_HARD_REGS, ORIGINAL or
630 reuse an existing reload pseudo. Don't reuse an existing reload pseudo if
631 IN_SUBREG_P is true and the reused pseudo should be wrapped up in a SUBREG.
632 The result pseudo is returned through RESULT_REG. Return TRUE if we created
633 a new pseudo, FALSE if we reused an existing reload pseudo. Use TITLE to
634 describe new registers for debug purposes. */
635 static bool
636 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
637 enum reg_class rclass, HARD_REG_SET *exclude_start_hard_regs,
638 bool in_subreg_p, const char *title, rtx *result_reg)
640 int i, regno;
641 enum reg_class new_class;
642 bool unique_p = false;
644 if (type == OP_OUT)
646 /* Output reload registers tend to start out with a conservative
647 choice of register class. Usually this is ALL_REGS, although
648 a target might narrow it (for performance reasons) through
649 targetm.preferred_reload_class. It's therefore quite common
650 for a reload instruction to require a more restrictive class
651 than the class that was originally assigned to the reload register.
653 In these situations, it's more efficient to refine the choice
654 of register class rather than create a second reload register.
655 This also helps to avoid cycling for registers that are only
656 used by reload instructions. */
657 if (REG_P (original)
658 && (int) REGNO (original) >= new_regno_start
659 && INSN_UID (curr_insn) >= new_insn_uid_start
660 && in_class_p (original, rclass, &new_class, true))
662 unsigned int regno = REGNO (original);
663 if (lra_dump_file != NULL)
665 fprintf (lra_dump_file, " Reuse r%d for output ", regno);
666 dump_value_slim (lra_dump_file, original, 1);
668 if (new_class != lra_get_allocno_class (regno))
669 lra_change_class (regno, new_class, ", change to", false);
670 if (lra_dump_file != NULL)
671 fprintf (lra_dump_file, "\n");
672 *result_reg = original;
673 return false;
675 *result_reg
676 = lra_create_new_reg_with_unique_value (mode, original, rclass,
677 exclude_start_hard_regs, title);
678 return true;
680 /* Prevent reuse value of expression with side effects,
681 e.g. volatile memory. */
682 if (! side_effects_p (original))
683 for (i = 0; i < curr_insn_input_reloads_num; i++)
685 if (! curr_insn_input_reloads[i].match_p
686 && rtx_equal_p (curr_insn_input_reloads[i].input, original)
687 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
689 rtx reg = curr_insn_input_reloads[i].reg;
690 regno = REGNO (reg);
691 /* If input is equal to original and both are VOIDmode,
692 GET_MODE (reg) might be still different from mode.
693 Ensure we don't return *result_reg with wrong mode. */
694 if (GET_MODE (reg) != mode)
696 if (in_subreg_p)
697 continue;
698 if (maybe_lt (GET_MODE_SIZE (GET_MODE (reg)),
699 GET_MODE_SIZE (mode)))
700 continue;
701 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
702 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
703 continue;
705 *result_reg = reg;
706 if (lra_dump_file != NULL)
708 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
709 dump_value_slim (lra_dump_file, original, 1);
711 if (new_class != lra_get_allocno_class (regno))
712 lra_change_class (regno, new_class, ", change to", false);
713 if (lra_dump_file != NULL)
714 fprintf (lra_dump_file, "\n");
715 return false;
717 /* If we have an input reload with a different mode, make sure it
718 will get a different hard reg. */
719 else if (REG_P (original)
720 && REG_P (curr_insn_input_reloads[i].input)
721 && REGNO (original) == REGNO (curr_insn_input_reloads[i].input)
722 && (GET_MODE (original)
723 != GET_MODE (curr_insn_input_reloads[i].input)))
724 unique_p = true;
726 *result_reg = (unique_p
727 ? lra_create_new_reg_with_unique_value
728 : lra_create_new_reg) (mode, original, rclass,
729 exclude_start_hard_regs, title);
730 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
731 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
732 curr_insn_input_reloads[curr_insn_input_reloads_num].match_p = false;
733 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
734 return true;
738 /* The page contains major code to choose the current insn alternative
739 and generate reloads for it. */
741 /* Return the offset from REGNO of the least significant register
742 in (reg:MODE REGNO).
744 This function is used to tell whether two registers satisfy
745 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
747 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
748 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
750 lra_constraint_offset (int regno, machine_mode mode)
752 lra_assert (regno < FIRST_PSEUDO_REGISTER);
754 scalar_int_mode int_mode;
755 if (WORDS_BIG_ENDIAN
756 && is_a <scalar_int_mode> (mode, &int_mode)
757 && GET_MODE_SIZE (int_mode) > UNITS_PER_WORD)
758 return hard_regno_nregs (regno, mode) - 1;
759 return 0;
762 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
763 if they are the same hard reg, and has special hacks for
764 auto-increment and auto-decrement. This is specifically intended for
765 process_alt_operands to use in determining whether two operands
766 match. X is the operand whose number is the lower of the two.
768 It is supposed that X is the output operand and Y is the input
769 operand. Y_HARD_REGNO is the final hard regno of register Y or
770 register in subreg Y as we know it now. Otherwise, it is a
771 negative value. */
772 static bool
773 operands_match_p (rtx x, rtx y, int y_hard_regno)
775 int i;
776 RTX_CODE code = GET_CODE (x);
777 const char *fmt;
779 if (x == y)
780 return true;
781 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
782 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
784 int j;
786 i = get_hard_regno (x);
787 if (i < 0)
788 goto slow;
790 if ((j = y_hard_regno) < 0)
791 goto slow;
793 i += lra_constraint_offset (i, GET_MODE (x));
794 j += lra_constraint_offset (j, GET_MODE (y));
796 return i == j;
799 /* If two operands must match, because they are really a single
800 operand of an assembler insn, then two post-increments are invalid
801 because the assembler insn would increment only once. On the
802 other hand, a post-increment matches ordinary indexing if the
803 post-increment is the output operand. */
804 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
805 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
807 /* Two pre-increments are invalid because the assembler insn would
808 increment only once. On the other hand, a pre-increment matches
809 ordinary indexing if the pre-increment is the input operand. */
810 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
811 || GET_CODE (y) == PRE_MODIFY)
812 return operands_match_p (x, XEXP (y, 0), -1);
814 slow:
816 if (code == REG && REG_P (y))
817 return REGNO (x) == REGNO (y);
819 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
820 && x == SUBREG_REG (y))
821 return true;
822 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
823 && SUBREG_REG (x) == y)
824 return true;
826 /* Now we have disposed of all the cases in which different rtx
827 codes can match. */
828 if (code != GET_CODE (y))
829 return false;
831 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
832 if (GET_MODE (x) != GET_MODE (y))
833 return false;
835 switch (code)
837 CASE_CONST_UNIQUE:
838 return false;
840 case CONST_VECTOR:
841 if (!same_vector_encodings_p (x, y))
842 return false;
843 break;
845 case LABEL_REF:
846 return label_ref_label (x) == label_ref_label (y);
847 case SYMBOL_REF:
848 return XSTR (x, 0) == XSTR (y, 0);
850 default:
851 break;
854 /* Compare the elements. If any pair of corresponding elements fail
855 to match, return false for the whole things. */
857 fmt = GET_RTX_FORMAT (code);
858 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
860 int val, j;
861 switch (fmt[i])
863 case 'w':
864 if (XWINT (x, i) != XWINT (y, i))
865 return false;
866 break;
868 case 'i':
869 if (XINT (x, i) != XINT (y, i))
870 return false;
871 break;
873 case 'p':
874 if (maybe_ne (SUBREG_BYTE (x), SUBREG_BYTE (y)))
875 return false;
876 break;
878 case 'e':
879 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
880 if (val == 0)
881 return false;
882 break;
884 case '0':
885 break;
887 case 'E':
888 if (XVECLEN (x, i) != XVECLEN (y, i))
889 return false;
890 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
892 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
893 if (val == 0)
894 return false;
896 break;
898 /* It is believed that rtx's at this level will never
899 contain anything but integers and other rtx's, except for
900 within LABEL_REFs and SYMBOL_REFs. */
901 default:
902 gcc_unreachable ();
905 return true;
908 /* True if X is a constant that can be forced into the constant pool.
909 MODE is the mode of the operand, or VOIDmode if not known. */
910 #define CONST_POOL_OK_P(MODE, X) \
911 ((MODE) != VOIDmode \
912 && CONSTANT_P (X) \
913 && GET_CODE (X) != HIGH \
914 && GET_MODE_SIZE (MODE).is_constant () \
915 && !targetm.cannot_force_const_mem (MODE, X))
917 /* True if C is a non-empty register class that has too few registers
918 to be safely used as a reload target class. */
919 #define SMALL_REGISTER_CLASS_P(C) \
920 (ira_class_hard_regs_num [(C)] == 1 \
921 || (ira_class_hard_regs_num [(C)] >= 1 \
922 && targetm.class_likely_spilled_p (C)))
924 /* If REG is a reload pseudo, try to make its class satisfying CL. */
925 static void
926 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
928 enum reg_class rclass;
930 /* Do not make more accurate class from reloads generated. They are
931 mostly moves with a lot of constraints. Making more accurate
932 class may results in very narrow class and impossibility of find
933 registers for several reloads of one insn. */
934 if (INSN_UID (curr_insn) >= new_insn_uid_start)
935 return;
936 if (GET_CODE (reg) == SUBREG)
937 reg = SUBREG_REG (reg);
938 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
939 return;
940 if (in_class_p (reg, cl, &rclass) && rclass != cl)
941 lra_change_class (REGNO (reg), rclass, " Change to", true);
944 /* Searches X for any reference to a reg with the same value as REGNO,
945 returning the rtx of the reference found if any. Otherwise,
946 returns NULL_RTX. */
947 static rtx
948 regno_val_use_in (unsigned int regno, rtx x)
950 const char *fmt;
951 int i, j;
952 rtx tem;
954 if (REG_P (x) && lra_reg_info[REGNO (x)].val == lra_reg_info[regno].val)
955 return x;
957 fmt = GET_RTX_FORMAT (GET_CODE (x));
958 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
960 if (fmt[i] == 'e')
962 if ((tem = regno_val_use_in (regno, XEXP (x, i))))
963 return tem;
965 else if (fmt[i] == 'E')
966 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
967 if ((tem = regno_val_use_in (regno , XVECEXP (x, i, j))))
968 return tem;
971 return NULL_RTX;
974 /* Return true if all current insn non-output operands except INS (it
975 has a negaitve end marker) do not use pseudos with the same value
976 as REGNO. */
977 static bool
978 check_conflict_input_operands (int regno, signed char *ins)
980 int in;
981 int n_operands = curr_static_id->n_operands;
983 for (int nop = 0; nop < n_operands; nop++)
984 if (! curr_static_id->operand[nop].is_operator
985 && curr_static_id->operand[nop].type != OP_OUT)
987 for (int i = 0; (in = ins[i]) >= 0; i++)
988 if (in == nop)
989 break;
990 if (in < 0
991 && regno_val_use_in (regno, *curr_id->operand_loc[nop]) != NULL_RTX)
992 return false;
994 return true;
997 /* Generate reloads for matching OUT and INS (array of input operand numbers
998 with end marker -1) with reg class GOAL_CLASS and EXCLUDE_START_HARD_REGS,
999 considering output operands OUTS (similar array to INS) needing to be in
1000 different registers. Add input and output reloads correspondingly to the
1001 lists *BEFORE and *AFTER. OUT might be negative. In this case we generate
1002 input reloads for matched input operands INS. EARLY_CLOBBER_P is a flag
1003 that the output operand is early clobbered for chosen alternative. */
1004 static void
1005 match_reload (signed char out, signed char *ins, signed char *outs,
1006 enum reg_class goal_class, HARD_REG_SET *exclude_start_hard_regs,
1007 rtx_insn **before, rtx_insn **after, bool early_clobber_p)
1009 bool out_conflict;
1010 int i, in;
1011 rtx new_in_reg, new_out_reg, reg;
1012 machine_mode inmode, outmode;
1013 rtx in_rtx = *curr_id->operand_loc[ins[0]];
1014 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
1016 inmode = curr_operand_mode[ins[0]];
1017 outmode = out < 0 ? inmode : curr_operand_mode[out];
1018 push_to_sequence (*before);
1019 if (inmode != outmode)
1021 /* process_alt_operands has already checked that the mode sizes
1022 are ordered. */
1023 if (partial_subreg_p (outmode, inmode))
1025 reg = new_in_reg
1026 = lra_create_new_reg_with_unique_value (inmode, in_rtx, goal_class,
1027 exclude_start_hard_regs,
1028 "");
1029 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
1030 LRA_SUBREG_P (new_out_reg) = 1;
1031 /* If the input reg is dying here, we can use the same hard
1032 register for REG and IN_RTX. We do it only for original
1033 pseudos as reload pseudos can die although original
1034 pseudos still live where reload pseudos dies. */
1035 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
1036 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
1037 && (!early_clobber_p
1038 || check_conflict_input_operands(REGNO (in_rtx), ins)))
1039 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
1041 else
1043 reg = new_out_reg
1044 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
1045 goal_class,
1046 exclude_start_hard_regs,
1047 "");
1048 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
1049 /* NEW_IN_REG is non-paradoxical subreg. We don't want
1050 NEW_OUT_REG living above. We add clobber clause for
1051 this. This is just a temporary clobber. We can remove
1052 it at the end of LRA work. */
1053 rtx_insn *clobber = emit_clobber (new_out_reg);
1054 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
1055 LRA_SUBREG_P (new_in_reg) = 1;
1056 if (GET_CODE (in_rtx) == SUBREG)
1058 rtx subreg_reg = SUBREG_REG (in_rtx);
1060 /* If SUBREG_REG is dying here and sub-registers IN_RTX
1061 and NEW_IN_REG are similar, we can use the same hard
1062 register for REG and SUBREG_REG. */
1063 if (REG_P (subreg_reg)
1064 && (int) REGNO (subreg_reg) < lra_new_regno_start
1065 && GET_MODE (subreg_reg) == outmode
1066 && known_eq (SUBREG_BYTE (in_rtx), SUBREG_BYTE (new_in_reg))
1067 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg))
1068 && (! early_clobber_p
1069 || check_conflict_input_operands (REGNO (subreg_reg),
1070 ins)))
1071 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
1075 else
1077 /* Pseudos have values -- see comments for lra_reg_info.
1078 Different pseudos with the same value do not conflict even if
1079 they live in the same place. When we create a pseudo we
1080 assign value of original pseudo (if any) from which we
1081 created the new pseudo. If we create the pseudo from the
1082 input pseudo, the new pseudo will have no conflict with the
1083 input pseudo which is wrong when the input pseudo lives after
1084 the insn and as the new pseudo value is changed by the insn
1085 output. Therefore we create the new pseudo from the output
1086 except the case when we have single matched dying input
1087 pseudo.
1089 We cannot reuse the current output register because we might
1090 have a situation like "a <- a op b", where the constraints
1091 force the second input operand ("b") to match the output
1092 operand ("a"). "b" must then be copied into a new register
1093 so that it doesn't clobber the current value of "a".
1095 We cannot use the same value if the output pseudo is
1096 early clobbered or the input pseudo is mentioned in the
1097 output, e.g. as an address part in memory, because
1098 output reload will actually extend the pseudo liveness.
1099 We don't care about eliminable hard regs here as we are
1100 interesting only in pseudos. */
1102 /* Matching input's register value is the same as one of the other
1103 output operand. Output operands in a parallel insn must be in
1104 different registers. */
1105 out_conflict = false;
1106 if (REG_P (in_rtx))
1108 for (i = 0; outs[i] >= 0; i++)
1110 rtx other_out_rtx = *curr_id->operand_loc[outs[i]];
1111 if (outs[i] != out && REG_P (other_out_rtx)
1112 && (regno_val_use_in (REGNO (in_rtx), other_out_rtx)
1113 != NULL_RTX))
1115 out_conflict = true;
1116 break;
1121 new_in_reg = new_out_reg
1122 = (! early_clobber_p && ins[1] < 0 && REG_P (in_rtx)
1123 && (int) REGNO (in_rtx) < lra_new_regno_start
1124 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
1125 && (! early_clobber_p
1126 || check_conflict_input_operands (REGNO (in_rtx), ins))
1127 && (out < 0
1128 || regno_val_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
1129 && !out_conflict
1130 ? lra_create_new_reg (inmode, in_rtx, goal_class,
1131 exclude_start_hard_regs, "")
1132 : lra_create_new_reg_with_unique_value (outmode, out_rtx, goal_class,
1133 exclude_start_hard_regs,
1134 ""));
1136 /* In operand can be got from transformations before processing insn
1137 constraints. One example of such transformations is subreg
1138 reloading (see function simplify_operand_subreg). The new
1139 pseudos created by the transformations might have inaccurate
1140 class (ALL_REGS) and we should make their classes more
1141 accurate. */
1142 narrow_reload_pseudo_class (in_rtx, goal_class);
1143 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
1144 *before = get_insns ();
1145 end_sequence ();
1146 /* Add the new pseudo to consider values of subsequent input reload
1147 pseudos. */
1148 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
1149 curr_insn_input_reloads[curr_insn_input_reloads_num].input = in_rtx;
1150 curr_insn_input_reloads[curr_insn_input_reloads_num].match_p = true;
1151 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = new_in_reg;
1152 for (i = 0; (in = ins[i]) >= 0; i++)
1153 if (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
1154 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]))
1155 *curr_id->operand_loc[in] = new_in_reg;
1156 else
1158 lra_assert
1159 (GET_MODE (new_out_reg) == GET_MODE (*curr_id->operand_loc[in]));
1160 *curr_id->operand_loc[in] = new_out_reg;
1162 lra_update_dups (curr_id, ins);
1163 if (out < 0)
1164 return;
1165 /* See a comment for the input operand above. */
1166 narrow_reload_pseudo_class (out_rtx, goal_class);
1167 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
1169 reg = SUBREG_P (out_rtx) ? SUBREG_REG (out_rtx) : out_rtx;
1170 start_sequence ();
1171 /* If we had strict_low_part, use it also in reload to keep other
1172 parts unchanged but do it only for regs as strict_low_part
1173 has no sense for memory and probably there is no insn pattern
1174 to match the reload insn in memory case. */
1175 if (out >= 0 && curr_static_id->operand[out].strict_low && REG_P (reg))
1176 out_rtx = gen_rtx_STRICT_LOW_PART (VOIDmode, out_rtx);
1177 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
1178 emit_insn (*after);
1179 *after = get_insns ();
1180 end_sequence ();
1182 *curr_id->operand_loc[out] = new_out_reg;
1183 lra_update_dup (curr_id, out);
1186 /* Return register class which is union of all reg classes in insn
1187 constraint alternative string starting with P. */
1188 static enum reg_class
1189 reg_class_from_constraints (const char *p)
1191 int c, len;
1192 enum reg_class op_class = NO_REGS;
1195 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1197 case '#':
1198 case ',':
1199 return op_class;
1201 case 'g':
1202 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1203 break;
1205 default:
1206 enum constraint_num cn = lookup_constraint (p);
1207 enum reg_class cl = reg_class_for_constraint (cn);
1208 if (cl == NO_REGS)
1210 if (insn_extra_address_constraint (cn))
1211 op_class
1212 = (reg_class_subunion
1213 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1214 ADDRESS, SCRATCH)]);
1215 break;
1218 op_class = reg_class_subunion[op_class][cl];
1219 break;
1221 while ((p += len), c);
1222 return op_class;
1225 /* If OP is a register, return the class of the register as per
1226 get_reg_class, otherwise return NO_REGS. */
1227 static inline enum reg_class
1228 get_op_class (rtx op)
1230 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1233 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1234 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1235 SUBREG for VAL to make them equal. */
1236 static rtx_insn *
1237 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1239 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1241 /* Usually size of mem_pseudo is greater than val size but in
1242 rare cases it can be less as it can be defined by target
1243 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1244 if (! MEM_P (val))
1246 val = gen_lowpart_SUBREG (GET_MODE (mem_pseudo),
1247 GET_CODE (val) == SUBREG
1248 ? SUBREG_REG (val) : val);
1249 LRA_SUBREG_P (val) = 1;
1251 else
1253 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1254 LRA_SUBREG_P (mem_pseudo) = 1;
1257 return to_p ? gen_move_insn (mem_pseudo, val)
1258 : gen_move_insn (val, mem_pseudo);
1261 /* Process a special case insn (register move), return true if we
1262 don't need to process it anymore. INSN should be a single set
1263 insn. Set up that RTL was changed through CHANGE_P and that hook
1264 TARGET_SECONDARY_MEMORY_NEEDED says to use secondary memory through
1265 SEC_MEM_P. */
1266 static bool
1267 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1269 int sregno, dregno;
1270 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1271 rtx_insn *before;
1272 enum reg_class dclass, sclass, secondary_class;
1273 secondary_reload_info sri;
1275 lra_assert (curr_insn_set != NULL_RTX);
1276 dreg = dest = SET_DEST (curr_insn_set);
1277 sreg = src = SET_SRC (curr_insn_set);
1278 if (GET_CODE (dest) == SUBREG)
1279 dreg = SUBREG_REG (dest);
1280 if (GET_CODE (src) == SUBREG)
1281 sreg = SUBREG_REG (src);
1282 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1283 return false;
1284 sclass = dclass = NO_REGS;
1285 if (REG_P (dreg))
1286 dclass = get_reg_class (REGNO (dreg));
1287 gcc_assert (dclass < LIM_REG_CLASSES && dclass >= NO_REGS);
1288 if (dclass == ALL_REGS)
1289 /* ALL_REGS is used for new pseudos created by transformations
1290 like reload of SUBREG_REG (see function
1291 simplify_operand_subreg). We don't know their class yet. We
1292 should figure out the class from processing the insn
1293 constraints not in this fast path function. Even if ALL_REGS
1294 were a right class for the pseudo, secondary_... hooks usually
1295 are not define for ALL_REGS. */
1296 return false;
1297 if (REG_P (sreg))
1298 sclass = get_reg_class (REGNO (sreg));
1299 gcc_assert (sclass < LIM_REG_CLASSES && sclass >= NO_REGS);
1300 if (sclass == ALL_REGS)
1301 /* See comments above. */
1302 return false;
1303 if (sclass == NO_REGS && dclass == NO_REGS)
1304 return false;
1305 if (targetm.secondary_memory_needed (GET_MODE (src), sclass, dclass)
1306 && ((sclass != NO_REGS && dclass != NO_REGS)
1307 || (GET_MODE (src)
1308 != targetm.secondary_memory_needed_mode (GET_MODE (src)))))
1310 *sec_mem_p = true;
1311 return false;
1313 if (! REG_P (dreg) || ! REG_P (sreg))
1314 return false;
1315 sri.prev_sri = NULL;
1316 sri.icode = CODE_FOR_nothing;
1317 sri.extra_cost = 0;
1318 secondary_class = NO_REGS;
1319 /* Set up hard register for a reload pseudo for hook
1320 secondary_reload because some targets just ignore unassigned
1321 pseudos in the hook. */
1322 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1324 dregno = REGNO (dreg);
1325 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1327 else
1328 dregno = -1;
1329 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1331 sregno = REGNO (sreg);
1332 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1334 else
1335 sregno = -1;
1336 if (sclass != NO_REGS)
1337 secondary_class
1338 = (enum reg_class) targetm.secondary_reload (false, dest,
1339 (reg_class_t) sclass,
1340 GET_MODE (src), &sri);
1341 if (sclass == NO_REGS
1342 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1343 && dclass != NO_REGS))
1345 enum reg_class old_sclass = secondary_class;
1346 secondary_reload_info old_sri = sri;
1348 sri.prev_sri = NULL;
1349 sri.icode = CODE_FOR_nothing;
1350 sri.extra_cost = 0;
1351 secondary_class
1352 = (enum reg_class) targetm.secondary_reload (true, src,
1353 (reg_class_t) dclass,
1354 GET_MODE (src), &sri);
1355 /* Check the target hook consistency. */
1356 lra_assert
1357 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1358 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1359 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1361 if (sregno >= 0)
1362 reg_renumber [sregno] = -1;
1363 if (dregno >= 0)
1364 reg_renumber [dregno] = -1;
1365 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1366 return false;
1367 *change_p = true;
1368 new_reg = NULL_RTX;
1369 if (secondary_class != NO_REGS)
1370 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1371 secondary_class, NULL,
1372 "secondary");
1373 start_sequence ();
1374 if (sri.icode == CODE_FOR_nothing)
1375 lra_emit_move (new_reg, src);
1376 else
1378 enum reg_class scratch_class;
1380 scratch_class = (reg_class_from_constraints
1381 (insn_data[sri.icode].operand[2].constraint));
1382 scratch_reg = (lra_create_new_reg_with_unique_value
1383 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1384 scratch_class, NULL, "scratch"));
1385 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1386 src, scratch_reg));
1388 before = get_insns ();
1389 end_sequence ();
1390 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1391 if (new_reg != NULL_RTX)
1392 SET_SRC (curr_insn_set) = new_reg;
1393 else
1395 if (lra_dump_file != NULL)
1397 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1398 dump_insn_slim (lra_dump_file, curr_insn);
1400 lra_set_insn_deleted (curr_insn);
1401 return true;
1403 return false;
1406 /* The following data describe the result of process_alt_operands.
1407 The data are used in curr_insn_transform to generate reloads. */
1409 /* The chosen reg classes which should be used for the corresponding
1410 operands. */
1411 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1412 /* Hard registers which cannot be a start hard register for the corresponding
1413 operands. */
1414 static HARD_REG_SET goal_alt_exclude_start_hard_regs[MAX_RECOG_OPERANDS];
1415 /* True if the operand should be the same as another operand and that
1416 other operand does not need a reload. */
1417 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1418 /* True if the operand does not need a reload. */
1419 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1420 /* True if the operand can be offsetable memory. */
1421 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1422 /* The number of an operand to which given operand can be matched to. */
1423 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1424 /* The number of elements in the following array. */
1425 static int goal_alt_dont_inherit_ops_num;
1426 /* Numbers of operands whose reload pseudos should not be inherited. */
1427 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1428 /* True if the insn commutative operands should be swapped. */
1429 static bool goal_alt_swapped;
1430 /* The chosen insn alternative. */
1431 static int goal_alt_number;
1433 /* True if the corresponding operand is the result of an equivalence
1434 substitution. */
1435 static bool equiv_substition_p[MAX_RECOG_OPERANDS];
1437 /* The following five variables are used to choose the best insn
1438 alternative. They reflect final characteristics of the best
1439 alternative. */
1441 /* Number of necessary reloads and overall cost reflecting the
1442 previous value and other unpleasantness of the best alternative. */
1443 static int best_losers, best_overall;
1444 /* Overall number hard registers used for reloads. For example, on
1445 some targets we need 2 general registers to reload DFmode and only
1446 one floating point register. */
1447 static int best_reload_nregs;
1448 /* Overall number reflecting distances of previous reloading the same
1449 value. The distances are counted from the current BB start. It is
1450 used to improve inheritance chances. */
1451 static int best_reload_sum;
1453 /* True if the current insn should have no correspondingly input or
1454 output reloads. */
1455 static bool no_input_reloads_p, no_output_reloads_p;
1457 /* True if we swapped the commutative operands in the current
1458 insn. */
1459 static int curr_swapped;
1461 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1462 register of class CL. Add any input reloads to list BEFORE. AFTER
1463 is nonnull if *LOC is an automodified value; handle that case by
1464 adding the required output reloads to list AFTER. Return true if
1465 the RTL was changed.
1467 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1468 register. Return false if the address register is correct. */
1469 static bool
1470 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1471 enum reg_class cl)
1473 int regno;
1474 enum reg_class rclass, new_class;
1475 rtx reg;
1476 rtx new_reg;
1477 machine_mode mode;
1478 bool subreg_p, before_p = false;
1480 subreg_p = GET_CODE (*loc) == SUBREG;
1481 if (subreg_p)
1483 reg = SUBREG_REG (*loc);
1484 mode = GET_MODE (reg);
1486 /* For mode with size bigger than ptr_mode, there unlikely to be "mov"
1487 between two registers with different classes, but there normally will
1488 be "mov" which transfers element of vector register into the general
1489 register, and this normally will be a subreg which should be reloaded
1490 as a whole. This is particularly likely to be triggered when
1491 -fno-split-wide-types specified. */
1492 if (!REG_P (reg)
1493 || in_class_p (reg, cl, &new_class)
1494 || known_le (GET_MODE_SIZE (mode), GET_MODE_SIZE (ptr_mode)))
1495 loc = &SUBREG_REG (*loc);
1498 reg = *loc;
1499 mode = GET_MODE (reg);
1500 if (! REG_P (reg))
1502 if (check_only_p)
1503 return true;
1504 /* Always reload memory in an address even if the target supports
1505 such addresses. */
1506 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, NULL,
1507 "address");
1508 before_p = true;
1510 else
1512 regno = REGNO (reg);
1513 rclass = get_reg_class (regno);
1514 if (! check_only_p
1515 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1517 if (lra_dump_file != NULL)
1519 fprintf (lra_dump_file,
1520 "Changing pseudo %d in address of insn %u on equiv ",
1521 REGNO (reg), INSN_UID (curr_insn));
1522 dump_value_slim (lra_dump_file, *loc, 1);
1523 fprintf (lra_dump_file, "\n");
1525 *loc = copy_rtx (*loc);
1527 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1529 if (check_only_p)
1530 return true;
1531 reg = *loc;
1532 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1533 mode, reg, cl, NULL,
1534 subreg_p, "address", &new_reg))
1535 before_p = true;
1537 else if (new_class != NO_REGS && rclass != new_class)
1539 if (check_only_p)
1540 return true;
1541 lra_change_class (regno, new_class, " Change to", true);
1542 return false;
1544 else
1545 return false;
1547 if (before_p)
1549 push_to_sequence (*before);
1550 lra_emit_move (new_reg, reg);
1551 *before = get_insns ();
1552 end_sequence ();
1554 *loc = new_reg;
1555 if (after != NULL)
1557 start_sequence ();
1558 lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1559 emit_insn (*after);
1560 *after = get_insns ();
1561 end_sequence ();
1563 return true;
1566 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1567 the insn to be inserted before curr insn. AFTER returns the
1568 the insn to be inserted after curr insn. ORIGREG and NEWREG
1569 are the original reg and new reg for reload. */
1570 static void
1571 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1572 rtx newreg)
1574 if (before)
1576 push_to_sequence (*before);
1577 lra_emit_move (newreg, origreg);
1578 *before = get_insns ();
1579 end_sequence ();
1581 if (after)
1583 start_sequence ();
1584 lra_emit_move (origreg, newreg);
1585 emit_insn (*after);
1586 *after = get_insns ();
1587 end_sequence ();
1591 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1592 static bool process_address (int, bool, rtx_insn **, rtx_insn **);
1594 /* Make reloads for subreg in operand NOP with internal subreg mode
1595 REG_MODE, add new reloads for further processing. Return true if
1596 any change was done. */
1597 static bool
1598 simplify_operand_subreg (int nop, machine_mode reg_mode)
1600 int hard_regno, inner_hard_regno;
1601 rtx_insn *before, *after;
1602 machine_mode mode, innermode;
1603 rtx reg, new_reg;
1604 rtx operand = *curr_id->operand_loc[nop];
1605 enum reg_class regclass;
1606 enum op_type type;
1608 before = after = NULL;
1610 if (GET_CODE (operand) != SUBREG)
1611 return false;
1613 mode = GET_MODE (operand);
1614 reg = SUBREG_REG (operand);
1615 innermode = GET_MODE (reg);
1616 type = curr_static_id->operand[nop].type;
1617 if (MEM_P (reg))
1619 const bool addr_was_valid
1620 = valid_address_p (innermode, XEXP (reg, 0), MEM_ADDR_SPACE (reg));
1621 alter_subreg (curr_id->operand_loc[nop], false);
1622 rtx subst = *curr_id->operand_loc[nop];
1623 lra_assert (MEM_P (subst));
1624 const bool addr_is_valid = valid_address_p (GET_MODE (subst),
1625 XEXP (subst, 0),
1626 MEM_ADDR_SPACE (subst));
1627 if (!addr_was_valid
1628 || addr_is_valid
1629 || ((get_constraint_type (lookup_constraint
1630 (curr_static_id->operand[nop].constraint))
1631 != CT_SPECIAL_MEMORY)
1632 /* We still can reload address and if the address is
1633 valid, we can remove subreg without reloading its
1634 inner memory. */
1635 && valid_address_p (GET_MODE (subst),
1636 regno_reg_rtx
1637 [ira_class_hard_regs
1638 [base_reg_class (GET_MODE (subst),
1639 MEM_ADDR_SPACE (subst),
1640 ADDRESS, SCRATCH)][0]],
1641 MEM_ADDR_SPACE (subst))))
1643 /* If we change the address for a paradoxical subreg of memory, the
1644 new address might violate the necessary alignment or the access
1645 might be slow; take this into consideration. We need not worry
1646 about accesses beyond allocated memory for paradoxical memory
1647 subregs as we don't substitute such equiv memory (see processing
1648 equivalences in function lra_constraints) and because for spilled
1649 pseudos we allocate stack memory enough for the biggest
1650 corresponding paradoxical subreg.
1652 However, do not blindly simplify a (subreg (mem ...)) for
1653 WORD_REGISTER_OPERATIONS targets as this may lead to loading junk
1654 data into a register when the inner is narrower than outer or
1655 missing important data from memory when the inner is wider than
1656 outer. This rule only applies to modes that are no wider than
1657 a word.
1659 If valid memory becomes invalid after subreg elimination
1660 and address might be different we still have to reload
1661 memory.
1663 if ((! addr_was_valid
1664 || addr_is_valid
1665 || known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (innermode)))
1666 && !(maybe_ne (GET_MODE_PRECISION (mode),
1667 GET_MODE_PRECISION (innermode))
1668 && known_le (GET_MODE_SIZE (mode), UNITS_PER_WORD)
1669 && known_le (GET_MODE_SIZE (innermode), UNITS_PER_WORD)
1670 && WORD_REGISTER_OPERATIONS)
1671 && (!(MEM_ALIGN (subst) < GET_MODE_ALIGNMENT (mode)
1672 && targetm.slow_unaligned_access (mode, MEM_ALIGN (subst)))
1673 || (MEM_ALIGN (reg) < GET_MODE_ALIGNMENT (innermode)
1674 && targetm.slow_unaligned_access (innermode,
1675 MEM_ALIGN (reg)))))
1676 return true;
1678 *curr_id->operand_loc[nop] = operand;
1680 /* But if the address was not valid, we cannot reload the MEM without
1681 reloading the address first. */
1682 if (!addr_was_valid)
1683 process_address (nop, false, &before, &after);
1685 /* INNERMODE is fast, MODE slow. Reload the mem in INNERMODE. */
1686 enum reg_class rclass
1687 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1688 if (get_reload_reg (curr_static_id->operand[nop].type, innermode,
1689 reg, rclass, NULL,
1690 TRUE, "slow/invalid mem", &new_reg))
1692 bool insert_before, insert_after;
1693 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1695 insert_before = (type != OP_OUT
1696 || partial_subreg_p (mode, innermode));
1697 insert_after = type != OP_IN;
1698 insert_move_for_subreg (insert_before ? &before : NULL,
1699 insert_after ? &after : NULL,
1700 reg, new_reg);
1702 SUBREG_REG (operand) = new_reg;
1704 /* Convert to MODE. */
1705 reg = operand;
1706 rclass
1707 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1708 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1709 rclass, NULL,
1710 TRUE, "slow/invalid mem", &new_reg))
1712 bool insert_before, insert_after;
1713 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1715 insert_before = type != OP_OUT;
1716 insert_after = type != OP_IN;
1717 insert_move_for_subreg (insert_before ? &before : NULL,
1718 insert_after ? &after : NULL,
1719 reg, new_reg);
1721 *curr_id->operand_loc[nop] = new_reg;
1722 lra_process_new_insns (curr_insn, before, after,
1723 "Inserting slow/invalid mem reload");
1724 return true;
1727 /* If the address was valid and became invalid, prefer to reload
1728 the memory. Typical case is when the index scale should
1729 correspond the memory. */
1730 *curr_id->operand_loc[nop] = operand;
1731 /* Do not return false here as the MEM_P (reg) will be processed
1732 later in this function. */
1734 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1736 alter_subreg (curr_id->operand_loc[nop], false);
1737 return true;
1739 else if (CONSTANT_P (reg))
1741 /* Try to simplify subreg of constant. It is usually result of
1742 equivalence substitution. */
1743 if (innermode == VOIDmode
1744 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1745 innermode = curr_static_id->operand[nop].mode;
1746 if ((new_reg = simplify_subreg (mode, reg, innermode,
1747 SUBREG_BYTE (operand))) != NULL_RTX)
1749 *curr_id->operand_loc[nop] = new_reg;
1750 return true;
1753 /* Put constant into memory when we have mixed modes. It generates
1754 a better code in most cases as it does not need a secondary
1755 reload memory. It also prevents LRA looping when LRA is using
1756 secondary reload memory again and again. */
1757 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1758 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1760 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1761 alter_subreg (curr_id->operand_loc[nop], false);
1762 return true;
1764 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1765 if there may be a problem accessing OPERAND in the outer
1766 mode. */
1767 if ((REG_P (reg)
1768 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1769 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1770 /* Don't reload paradoxical subregs because we could be looping
1771 having repeatedly final regno out of hard regs range. */
1772 && (hard_regno_nregs (hard_regno, innermode)
1773 >= hard_regno_nregs (hard_regno, mode))
1774 && simplify_subreg_regno (hard_regno, innermode,
1775 SUBREG_BYTE (operand), mode) < 0
1776 /* Don't reload subreg for matching reload. It is actually
1777 valid subreg in LRA. */
1778 && ! LRA_SUBREG_P (operand))
1779 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1781 enum reg_class rclass;
1783 if (REG_P (reg))
1784 /* There is a big probability that we will get the same class
1785 for the new pseudo and we will get the same insn which
1786 means infinite looping. So spill the new pseudo. */
1787 rclass = NO_REGS;
1788 else
1789 /* The class will be defined later in curr_insn_transform. */
1790 rclass
1791 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1793 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1794 rclass, NULL,
1795 TRUE, "subreg reg", &new_reg))
1797 bool insert_before, insert_after;
1798 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1800 insert_before = (type != OP_OUT
1801 || read_modify_subreg_p (operand));
1802 insert_after = (type != OP_IN);
1803 insert_move_for_subreg (insert_before ? &before : NULL,
1804 insert_after ? &after : NULL,
1805 reg, new_reg);
1807 SUBREG_REG (operand) = new_reg;
1808 lra_process_new_insns (curr_insn, before, after,
1809 "Inserting subreg reload");
1810 return true;
1812 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1813 IRA allocates hardreg to the inner pseudo reg according to its mode
1814 instead of the outermode, so the size of the hardreg may not be enough
1815 to contain the outermode operand, in that case we may need to insert
1816 reload for the reg. For the following two types of paradoxical subreg,
1817 we need to insert reload:
1818 1. If the op_type is OP_IN, and the hardreg could not be paired with
1819 other hardreg to contain the outermode operand
1820 (checked by in_hard_reg_set_p), we need to insert the reload.
1821 2. If the op_type is OP_OUT or OP_INOUT.
1823 Here is a paradoxical subreg example showing how the reload is generated:
1825 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1826 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1828 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1829 here, if reg107 is assigned to hardreg R15, because R15 is the last
1830 hardreg, compiler cannot find another hardreg to pair with R15 to
1831 contain TImode data. So we insert a TImode reload reg180 for it.
1832 After reload is inserted:
1834 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1835 (reg:DI 107 [ __comp ])) -1
1836 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1837 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1839 Two reload hard registers will be allocated to reg180 to save TImode data
1840 in LRA_assign.
1842 For LRA pseudos this should normally be handled by the biggest_mode
1843 mechanism. However, it's possible for new uses of an LRA pseudo
1844 to be introduced after we've allocated it, such as when undoing
1845 inheritance, and the allocated register might not then be appropriate
1846 for the new uses. */
1847 else if (REG_P (reg)
1848 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1849 && paradoxical_subreg_p (operand)
1850 && (inner_hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1851 && ((hard_regno
1852 = simplify_subreg_regno (inner_hard_regno, innermode,
1853 SUBREG_BYTE (operand), mode)) < 0
1854 || ((hard_regno_nregs (inner_hard_regno, innermode)
1855 < hard_regno_nregs (hard_regno, mode))
1856 && (regclass = lra_get_allocno_class (REGNO (reg)))
1857 && (type != OP_IN
1858 || !in_hard_reg_set_p (reg_class_contents[regclass],
1859 mode, hard_regno)
1860 || overlaps_hard_reg_set_p (lra_no_alloc_regs,
1861 mode, hard_regno)))))
1863 /* The class will be defined later in curr_insn_transform. */
1864 enum reg_class rclass
1865 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1867 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1868 rclass, NULL,
1869 TRUE, "paradoxical subreg", &new_reg))
1871 rtx subreg;
1872 bool insert_before, insert_after;
1874 PUT_MODE (new_reg, mode);
1875 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1876 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1878 insert_before = (type != OP_OUT);
1879 insert_after = (type != OP_IN);
1880 insert_move_for_subreg (insert_before ? &before : NULL,
1881 insert_after ? &after : NULL,
1882 reg, subreg);
1884 SUBREG_REG (operand) = new_reg;
1885 lra_process_new_insns (curr_insn, before, after,
1886 "Inserting paradoxical subreg reload");
1887 return true;
1889 return false;
1892 /* Return TRUE if X refers for a hard register from SET. */
1893 static bool
1894 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1896 int i, j, x_hard_regno;
1897 machine_mode mode;
1898 const char *fmt;
1899 enum rtx_code code;
1901 if (x == NULL_RTX)
1902 return false;
1903 code = GET_CODE (x);
1904 mode = GET_MODE (x);
1906 if (code == SUBREG)
1908 /* For all SUBREGs we want to check whether the full multi-register
1909 overlaps the set. For normal SUBREGs this means 'get_hard_regno' of
1910 the inner register, for paradoxical SUBREGs this means the
1911 'get_hard_regno' of the full SUBREG and for complete SUBREGs either is
1912 fine. Use the wider mode for all cases. */
1913 rtx subreg = SUBREG_REG (x);
1914 mode = wider_subreg_mode (x);
1915 if (mode == GET_MODE (subreg))
1917 x = subreg;
1918 code = GET_CODE (x);
1922 if (REG_P (x) || SUBREG_P (x))
1924 x_hard_regno = get_hard_regno (x);
1925 return (x_hard_regno >= 0
1926 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1928 fmt = GET_RTX_FORMAT (code);
1929 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1931 if (fmt[i] == 'e')
1933 if (uses_hard_regs_p (XEXP (x, i), set))
1934 return true;
1936 else if (fmt[i] == 'E')
1938 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1939 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1940 return true;
1943 return false;
1946 /* Return true if OP is a spilled pseudo. */
1947 static inline bool
1948 spilled_pseudo_p (rtx op)
1950 return (REG_P (op)
1951 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1954 /* Return true if X is a general constant. */
1955 static inline bool
1956 general_constant_p (rtx x)
1958 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1961 static bool
1962 reg_in_class_p (rtx reg, enum reg_class cl)
1964 if (cl == NO_REGS)
1965 return get_reg_class (REGNO (reg)) == NO_REGS;
1966 return in_class_p (reg, cl, NULL);
1969 /* Return true if SET of RCLASS contains no hard regs which can be
1970 used in MODE. */
1971 static bool
1972 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1973 HARD_REG_SET &set,
1974 machine_mode mode)
1976 HARD_REG_SET temp;
1978 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1979 temp = set & ~lra_no_alloc_regs;
1980 return (hard_reg_set_subset_p
1981 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1985 /* Used to check validity info about small class input operands. It
1986 should be incremented at start of processing an insn
1987 alternative. */
1988 static unsigned int curr_small_class_check = 0;
1990 /* Update number of used inputs of class OP_CLASS for operand NOP
1991 of alternative NALT. Return true if we have more such class operands
1992 than the number of available regs. */
1993 static bool
1994 update_and_check_small_class_inputs (int nop, int nalt,
1995 enum reg_class op_class)
1997 static unsigned int small_class_check[LIM_REG_CLASSES];
1998 static int small_class_input_nums[LIM_REG_CLASSES];
2000 if (SMALL_REGISTER_CLASS_P (op_class)
2001 /* We are interesting in classes became small because of fixing
2002 some hard regs, e.g. by an user through GCC options. */
2003 && hard_reg_set_intersect_p (reg_class_contents[op_class],
2004 ira_no_alloc_regs)
2005 && (curr_static_id->operand[nop].type != OP_OUT
2006 || TEST_BIT (curr_static_id->operand[nop].early_clobber_alts, nalt)))
2008 if (small_class_check[op_class] == curr_small_class_check)
2009 small_class_input_nums[op_class]++;
2010 else
2012 small_class_check[op_class] = curr_small_class_check;
2013 small_class_input_nums[op_class] = 1;
2015 if (small_class_input_nums[op_class] > ira_class_hard_regs_num[op_class])
2016 return true;
2018 return false;
2021 /* Major function to choose the current insn alternative and what
2022 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
2023 negative we should consider only this alternative. Return false if
2024 we cannot choose the alternative or find how to reload the
2025 operands. */
2026 static bool
2027 process_alt_operands (int only_alternative)
2029 bool ok_p = false;
2030 int nop, overall, nalt;
2031 int n_alternatives = curr_static_id->n_alternatives;
2032 int n_operands = curr_static_id->n_operands;
2033 /* LOSERS counts the operands that don't fit this alternative and
2034 would require loading. */
2035 int losers;
2036 int addr_losers;
2037 /* REJECT is a count of how undesirable this alternative says it is
2038 if any reloading is required. If the alternative matches exactly
2039 then REJECT is ignored, but otherwise it gets this much counted
2040 against it in addition to the reloading needed. */
2041 int reject;
2042 /* This is defined by '!' or '?' alternative constraint and added to
2043 reject. But in some cases it can be ignored. */
2044 int static_reject;
2045 int op_reject;
2046 /* The number of elements in the following array. */
2047 int early_clobbered_regs_num;
2048 /* Numbers of operands which are early clobber registers. */
2049 int early_clobbered_nops[MAX_RECOG_OPERANDS];
2050 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
2051 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
2052 HARD_REG_SET curr_alt_exclude_start_hard_regs[MAX_RECOG_OPERANDS];
2053 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
2054 bool curr_alt_win[MAX_RECOG_OPERANDS];
2055 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
2056 int curr_alt_matches[MAX_RECOG_OPERANDS];
2057 /* The number of elements in the following array. */
2058 int curr_alt_dont_inherit_ops_num;
2059 /* Numbers of operands whose reload pseudos should not be inherited. */
2060 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
2061 rtx op;
2062 /* The register when the operand is a subreg of register, otherwise the
2063 operand itself. */
2064 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
2065 /* The register if the operand is a register or subreg of register,
2066 otherwise NULL. */
2067 rtx operand_reg[MAX_RECOG_OPERANDS];
2068 int hard_regno[MAX_RECOG_OPERANDS];
2069 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
2070 int reload_nregs, reload_sum;
2071 bool costly_p;
2072 enum reg_class cl;
2074 /* Calculate some data common for all alternatives to speed up the
2075 function. */
2076 for (nop = 0; nop < n_operands; nop++)
2078 rtx reg;
2080 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
2081 /* The real hard regno of the operand after the allocation. */
2082 hard_regno[nop] = get_hard_regno (op);
2084 operand_reg[nop] = reg = op;
2085 biggest_mode[nop] = GET_MODE (op);
2086 if (GET_CODE (op) == SUBREG)
2088 biggest_mode[nop] = wider_subreg_mode (op);
2089 operand_reg[nop] = reg = SUBREG_REG (op);
2091 if (! REG_P (reg))
2092 operand_reg[nop] = NULL_RTX;
2093 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
2094 || ((int) REGNO (reg)
2095 == lra_get_elimination_hard_regno (REGNO (reg))))
2096 no_subreg_reg_operand[nop] = reg;
2097 else
2098 operand_reg[nop] = no_subreg_reg_operand[nop]
2099 /* Just use natural mode for elimination result. It should
2100 be enough for extra constraints hooks. */
2101 = regno_reg_rtx[hard_regno[nop]];
2104 /* The constraints are made of several alternatives. Each operand's
2105 constraint looks like foo,bar,... with commas separating the
2106 alternatives. The first alternatives for all operands go
2107 together, the second alternatives go together, etc.
2109 First loop over alternatives. */
2110 alternative_mask preferred = curr_id->preferred_alternatives;
2111 if (only_alternative >= 0)
2112 preferred &= ALTERNATIVE_BIT (only_alternative);
2114 for (nalt = 0; nalt < n_alternatives; nalt++)
2116 /* Loop over operands for one constraint alternative. */
2117 if (!TEST_BIT (preferred, nalt))
2118 continue;
2120 bool matching_early_clobber[MAX_RECOG_OPERANDS];
2121 curr_small_class_check++;
2122 overall = losers = addr_losers = 0;
2123 static_reject = reject = reload_nregs = reload_sum = 0;
2124 for (nop = 0; nop < n_operands; nop++)
2126 int inc = (curr_static_id
2127 ->operand_alternative[nalt * n_operands + nop].reject);
2128 if (lra_dump_file != NULL && inc != 0)
2129 fprintf (lra_dump_file,
2130 " Staticly defined alt reject+=%d\n", inc);
2131 static_reject += inc;
2132 matching_early_clobber[nop] = 0;
2134 reject += static_reject;
2135 early_clobbered_regs_num = 0;
2137 for (nop = 0; nop < n_operands; nop++)
2139 const char *p;
2140 char *end;
2141 int len, c, m, i, opalt_num, this_alternative_matches;
2142 bool win, did_match, offmemok, early_clobber_p;
2143 /* false => this operand can be reloaded somehow for this
2144 alternative. */
2145 bool badop;
2146 /* true => this operand can be reloaded if the alternative
2147 allows regs. */
2148 bool winreg;
2149 /* True if a constant forced into memory would be OK for
2150 this operand. */
2151 bool constmemok;
2152 enum reg_class this_alternative, this_costly_alternative;
2153 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
2154 HARD_REG_SET this_alternative_exclude_start_hard_regs;
2155 bool this_alternative_match_win, this_alternative_win;
2156 bool this_alternative_offmemok;
2157 bool scratch_p;
2158 machine_mode mode;
2159 enum constraint_num cn;
2161 opalt_num = nalt * n_operands + nop;
2162 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
2164 /* Fast track for no constraints at all. */
2165 curr_alt[nop] = NO_REGS;
2166 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
2167 curr_alt_win[nop] = true;
2168 curr_alt_match_win[nop] = false;
2169 curr_alt_offmemok[nop] = false;
2170 curr_alt_matches[nop] = -1;
2171 continue;
2174 op = no_subreg_reg_operand[nop];
2175 mode = curr_operand_mode[nop];
2177 win = did_match = winreg = offmemok = constmemok = false;
2178 badop = true;
2180 early_clobber_p = false;
2181 p = curr_static_id->operand_alternative[opalt_num].constraint;
2183 this_costly_alternative = this_alternative = NO_REGS;
2184 /* We update set of possible hard regs besides its class
2185 because reg class might be inaccurate. For example,
2186 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
2187 is translated in HI_REGS because classes are merged by
2188 pairs and there is no accurate intermediate class. */
2189 CLEAR_HARD_REG_SET (this_alternative_set);
2190 CLEAR_HARD_REG_SET (this_costly_alternative_set);
2191 CLEAR_HARD_REG_SET (this_alternative_exclude_start_hard_regs);
2192 this_alternative_win = false;
2193 this_alternative_match_win = false;
2194 this_alternative_offmemok = false;
2195 this_alternative_matches = -1;
2197 /* An empty constraint should be excluded by the fast
2198 track. */
2199 lra_assert (*p != 0 && *p != ',');
2201 op_reject = 0;
2202 /* Scan this alternative's specs for this operand; set WIN
2203 if the operand fits any letter in this alternative.
2204 Otherwise, clear BADOP if this operand could fit some
2205 letter after reloads, or set WINREG if this operand could
2206 fit after reloads provided the constraint allows some
2207 registers. */
2208 costly_p = false;
2211 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
2213 case '\0':
2214 len = 0;
2215 break;
2216 case ',':
2217 c = '\0';
2218 break;
2220 case '&':
2221 early_clobber_p = true;
2222 break;
2224 case '$':
2225 op_reject += LRA_MAX_REJECT;
2226 break;
2227 case '^':
2228 op_reject += LRA_LOSER_COST_FACTOR;
2229 break;
2231 case '#':
2232 /* Ignore rest of this alternative. */
2233 c = '\0';
2234 break;
2236 case '0': case '1': case '2': case '3': case '4':
2237 case '5': case '6': case '7': case '8': case '9':
2239 int m_hregno;
2240 bool match_p;
2242 m = strtoul (p, &end, 10);
2243 p = end;
2244 len = 0;
2245 lra_assert (nop > m);
2247 /* Reject matches if we don't know which operand is
2248 bigger. This situation would arguably be a bug in
2249 an .md pattern, but could also occur in a user asm. */
2250 if (!ordered_p (GET_MODE_SIZE (biggest_mode[m]),
2251 GET_MODE_SIZE (biggest_mode[nop])))
2252 break;
2254 /* Don't match wrong asm insn operands for proper
2255 diagnostic later. */
2256 if (INSN_CODE (curr_insn) < 0
2257 && (curr_operand_mode[m] == BLKmode
2258 || curr_operand_mode[nop] == BLKmode)
2259 && curr_operand_mode[m] != curr_operand_mode[nop])
2260 break;
2262 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
2263 /* We are supposed to match a previous operand.
2264 If we do, we win if that one did. If we do
2265 not, count both of the operands as losers.
2266 (This is too conservative, since most of the
2267 time only a single reload insn will be needed
2268 to make the two operands win. As a result,
2269 this alternative may be rejected when it is
2270 actually desirable.) */
2271 match_p = false;
2272 if (operands_match_p (*curr_id->operand_loc[nop],
2273 *curr_id->operand_loc[m], m_hregno))
2275 /* We should reject matching of an early
2276 clobber operand if the matching operand is
2277 not dying in the insn. */
2278 if (!TEST_BIT (curr_static_id->operand[m]
2279 .early_clobber_alts, nalt)
2280 || operand_reg[nop] == NULL_RTX
2281 || (find_regno_note (curr_insn, REG_DEAD,
2282 REGNO (op))
2283 || REGNO (op) == REGNO (operand_reg[m])))
2284 match_p = true;
2286 if (match_p)
2288 /* If we are matching a non-offsettable
2289 address where an offsettable address was
2290 expected, then we must reject this
2291 combination, because we can't reload
2292 it. */
2293 if (curr_alt_offmemok[m]
2294 && MEM_P (*curr_id->operand_loc[m])
2295 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
2296 continue;
2298 else
2300 /* If the operands do not match and one
2301 operand is INOUT, we can not match them.
2302 Try other possibilities, e.g. other
2303 alternatives or commutative operand
2304 exchange. */
2305 if (curr_static_id->operand[nop].type == OP_INOUT
2306 || curr_static_id->operand[m].type == OP_INOUT)
2307 break;
2308 /* Operands don't match. If the operands are
2309 different user defined explicit hard
2310 registers, then we cannot make them match
2311 when one is early clobber operand. */
2312 if ((REG_P (*curr_id->operand_loc[nop])
2313 || SUBREG_P (*curr_id->operand_loc[nop]))
2314 && (REG_P (*curr_id->operand_loc[m])
2315 || SUBREG_P (*curr_id->operand_loc[m])))
2317 rtx nop_reg = *curr_id->operand_loc[nop];
2318 if (SUBREG_P (nop_reg))
2319 nop_reg = SUBREG_REG (nop_reg);
2320 rtx m_reg = *curr_id->operand_loc[m];
2321 if (SUBREG_P (m_reg))
2322 m_reg = SUBREG_REG (m_reg);
2324 if (REG_P (nop_reg)
2325 && HARD_REGISTER_P (nop_reg)
2326 && REG_USERVAR_P (nop_reg)
2327 && REG_P (m_reg)
2328 && HARD_REGISTER_P (m_reg)
2329 && REG_USERVAR_P (m_reg))
2331 int i;
2333 for (i = 0; i < early_clobbered_regs_num; i++)
2334 if (m == early_clobbered_nops[i])
2335 break;
2336 if (i < early_clobbered_regs_num
2337 || early_clobber_p)
2338 break;
2341 /* Both operands must allow a reload register,
2342 otherwise we cannot make them match. */
2343 if (curr_alt[m] == NO_REGS)
2344 break;
2345 /* Retroactively mark the operand we had to
2346 match as a loser, if it wasn't already and
2347 it wasn't matched to a register constraint
2348 (e.g it might be matched by memory). */
2349 if (curr_alt_win[m]
2350 && (operand_reg[m] == NULL_RTX
2351 || hard_regno[m] < 0))
2353 losers++;
2354 reload_nregs
2355 += (ira_reg_class_max_nregs[curr_alt[m]]
2356 [GET_MODE (*curr_id->operand_loc[m])]);
2359 /* Prefer matching earlyclobber alternative as
2360 it results in less hard regs required for
2361 the insn than a non-matching earlyclobber
2362 alternative. */
2363 if (TEST_BIT (curr_static_id->operand[m]
2364 .early_clobber_alts, nalt))
2366 if (lra_dump_file != NULL)
2367 fprintf
2368 (lra_dump_file,
2369 " %d Matching earlyclobber alt:"
2370 " reject--\n",
2371 nop);
2372 if (!matching_early_clobber[m])
2374 reject--;
2375 matching_early_clobber[m] = 1;
2378 /* Otherwise we prefer no matching
2379 alternatives because it gives more freedom
2380 in RA. */
2381 else if (operand_reg[nop] == NULL_RTX
2382 || (find_regno_note (curr_insn, REG_DEAD,
2383 REGNO (operand_reg[nop]))
2384 == NULL_RTX))
2386 if (lra_dump_file != NULL)
2387 fprintf
2388 (lra_dump_file,
2389 " %d Matching alt: reject+=2\n",
2390 nop);
2391 reject += 2;
2394 /* If we have to reload this operand and some
2395 previous operand also had to match the same
2396 thing as this operand, we don't know how to do
2397 that. */
2398 if (!match_p || !curr_alt_win[m])
2400 for (i = 0; i < nop; i++)
2401 if (curr_alt_matches[i] == m)
2402 break;
2403 if (i < nop)
2404 break;
2406 else
2407 did_match = true;
2409 this_alternative_matches = m;
2410 /* This can be fixed with reloads if the operand
2411 we are supposed to match can be fixed with
2412 reloads. */
2413 badop = false;
2414 this_alternative = curr_alt[m];
2415 this_alternative_set = curr_alt_set[m];
2416 this_alternative_exclude_start_hard_regs
2417 = curr_alt_exclude_start_hard_regs[m];
2418 winreg = this_alternative != NO_REGS;
2419 break;
2422 case 'g':
2423 if (MEM_P (op)
2424 || general_constant_p (op)
2425 || spilled_pseudo_p (op))
2426 win = true;
2427 cl = GENERAL_REGS;
2428 goto reg;
2430 default:
2431 cn = lookup_constraint (p);
2432 switch (get_constraint_type (cn))
2434 case CT_REGISTER:
2435 cl = reg_class_for_constraint (cn);
2436 if (cl != NO_REGS)
2437 goto reg;
2438 break;
2440 case CT_CONST_INT:
2441 if (CONST_INT_P (op)
2442 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2443 win = true;
2444 break;
2446 case CT_MEMORY:
2447 case CT_RELAXED_MEMORY:
2448 if (MEM_P (op)
2449 && satisfies_memory_constraint_p (op, cn))
2450 win = true;
2451 else if (spilled_pseudo_p (op))
2452 win = true;
2454 /* If we didn't already win, we can reload constants
2455 via force_const_mem or put the pseudo value into
2456 memory, or make other memory by reloading the
2457 address like for 'o'. */
2458 if (CONST_POOL_OK_P (mode, op)
2459 || MEM_P (op) || REG_P (op)
2460 /* We can restore the equiv insn by a
2461 reload. */
2462 || equiv_substition_p[nop])
2463 badop = false;
2464 constmemok = true;
2465 offmemok = true;
2466 break;
2468 case CT_ADDRESS:
2469 /* An asm operand with an address constraint
2470 that doesn't satisfy address_operand has
2471 is_address cleared, so that we don't try to
2472 make a non-address fit. */
2473 if (!curr_static_id->operand[nop].is_address)
2474 break;
2475 /* If we didn't already win, we can reload the address
2476 into a base register. */
2477 if (satisfies_address_constraint_p (op, cn))
2478 win = true;
2479 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2480 ADDRESS, SCRATCH);
2481 badop = false;
2482 goto reg;
2484 case CT_FIXED_FORM:
2485 if (constraint_satisfied_p (op, cn))
2486 win = true;
2487 break;
2489 case CT_SPECIAL_MEMORY:
2490 if (satisfies_memory_constraint_p (op, cn))
2491 win = true;
2492 else if (spilled_pseudo_p (op))
2493 win = true;
2494 break;
2496 break;
2498 reg:
2499 if (mode == BLKmode)
2500 break;
2501 this_alternative = reg_class_subunion[this_alternative][cl];
2502 if (hard_reg_set_subset_p (this_alternative_set,
2503 reg_class_contents[cl]))
2504 this_alternative_exclude_start_hard_regs
2505 = ira_exclude_class_mode_regs[cl][mode];
2506 else if (!hard_reg_set_subset_p (reg_class_contents[cl],
2507 this_alternative_set))
2508 this_alternative_exclude_start_hard_regs
2509 |= ira_exclude_class_mode_regs[cl][mode];
2510 this_alternative_set |= reg_class_contents[cl];
2511 if (costly_p)
2513 this_costly_alternative
2514 = reg_class_subunion[this_costly_alternative][cl];
2515 this_costly_alternative_set |= reg_class_contents[cl];
2517 winreg = true;
2518 if (REG_P (op))
2520 if (hard_regno[nop] >= 0
2521 && in_hard_reg_set_p (this_alternative_set,
2522 mode, hard_regno[nop])
2523 && !TEST_HARD_REG_BIT
2524 (this_alternative_exclude_start_hard_regs,
2525 hard_regno[nop]))
2526 win = true;
2527 else if (hard_regno[nop] < 0
2528 && in_class_p (op, this_alternative, NULL))
2529 win = true;
2531 break;
2533 if (c != ' ' && c != '\t')
2534 costly_p = c == '*';
2536 while ((p += len), c);
2538 scratch_p = (operand_reg[nop] != NULL_RTX
2539 && ira_former_scratch_p (REGNO (operand_reg[nop])));
2540 /* Record which operands fit this alternative. */
2541 if (win)
2543 this_alternative_win = true;
2544 if (operand_reg[nop] != NULL_RTX)
2546 if (hard_regno[nop] >= 0)
2548 if (in_hard_reg_set_p (this_costly_alternative_set,
2549 mode, hard_regno[nop]))
2551 if (lra_dump_file != NULL)
2552 fprintf (lra_dump_file,
2553 " %d Costly set: reject++\n",
2554 nop);
2555 reject++;
2558 else
2560 /* Prefer won reg to spilled pseudo under other
2561 equal conditions for possibe inheritance. */
2562 if (! scratch_p)
2564 if (lra_dump_file != NULL)
2565 fprintf
2566 (lra_dump_file,
2567 " %d Non pseudo reload: reject++\n",
2568 nop);
2569 reject++;
2571 if (in_class_p (operand_reg[nop],
2572 this_costly_alternative, NULL))
2574 if (lra_dump_file != NULL)
2575 fprintf
2576 (lra_dump_file,
2577 " %d Non pseudo costly reload:"
2578 " reject++\n",
2579 nop);
2580 reject++;
2583 /* We simulate the behavior of old reload here.
2584 Although scratches need hard registers and it
2585 might result in spilling other pseudos, no reload
2586 insns are generated for the scratches. So it
2587 might cost something but probably less than old
2588 reload pass believes. */
2589 if (scratch_p)
2591 if (lra_dump_file != NULL)
2592 fprintf (lra_dump_file,
2593 " %d Scratch win: reject+=2\n",
2594 nop);
2595 reject += 2;
2599 else if (did_match)
2600 this_alternative_match_win = true;
2601 else
2603 int const_to_mem = 0;
2604 bool no_regs_p;
2606 reject += op_reject;
2607 /* Never do output reload of stack pointer. It makes
2608 impossible to do elimination when SP is changed in
2609 RTL. */
2610 if (op == stack_pointer_rtx && ! frame_pointer_needed
2611 && curr_static_id->operand[nop].type != OP_IN)
2612 goto fail;
2614 /* If this alternative asks for a specific reg class, see if there
2615 is at least one allocatable register in that class. */
2616 no_regs_p
2617 = (this_alternative == NO_REGS
2618 || (hard_reg_set_subset_p
2619 (reg_class_contents[this_alternative],
2620 lra_no_alloc_regs)));
2622 /* For asms, verify that the class for this alternative is possible
2623 for the mode that is specified. */
2624 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2626 int i;
2627 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2628 if (targetm.hard_regno_mode_ok (i, mode)
2629 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2630 mode, i))
2631 break;
2632 if (i == FIRST_PSEUDO_REGISTER)
2633 winreg = false;
2636 /* If this operand accepts a register, and if the
2637 register class has at least one allocatable register,
2638 then this operand can be reloaded. */
2639 if (winreg && !no_regs_p)
2640 badop = false;
2642 if (badop)
2644 if (lra_dump_file != NULL)
2645 fprintf (lra_dump_file,
2646 " alt=%d: Bad operand -- refuse\n",
2647 nalt);
2648 goto fail;
2651 if (this_alternative != NO_REGS)
2653 HARD_REG_SET available_regs
2654 = (reg_class_contents[this_alternative]
2655 & ~((ira_prohibited_class_mode_regs
2656 [this_alternative][mode])
2657 | lra_no_alloc_regs));
2658 if (hard_reg_set_empty_p (available_regs))
2660 /* There are no hard regs holding a value of given
2661 mode. */
2662 if (offmemok)
2664 this_alternative = NO_REGS;
2665 if (lra_dump_file != NULL)
2666 fprintf (lra_dump_file,
2667 " %d Using memory because of"
2668 " a bad mode: reject+=2\n",
2669 nop);
2670 reject += 2;
2672 else
2674 if (lra_dump_file != NULL)
2675 fprintf (lra_dump_file,
2676 " alt=%d: Wrong mode -- refuse\n",
2677 nalt);
2678 goto fail;
2683 /* If not assigned pseudo has a class which a subset of
2684 required reg class, it is a less costly alternative
2685 as the pseudo still can get a hard reg of necessary
2686 class. */
2687 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2688 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2689 && ira_class_subset_p[this_alternative][cl])
2691 if (lra_dump_file != NULL)
2692 fprintf
2693 (lra_dump_file,
2694 " %d Super set class reg: reject-=3\n", nop);
2695 reject -= 3;
2698 this_alternative_offmemok = offmemok;
2699 if (this_costly_alternative != NO_REGS)
2701 if (lra_dump_file != NULL)
2702 fprintf (lra_dump_file,
2703 " %d Costly loser: reject++\n", nop);
2704 reject++;
2706 /* If the operand is dying, has a matching constraint,
2707 and satisfies constraints of the matched operand
2708 which failed to satisfy the own constraints, most probably
2709 the reload for this operand will be gone. */
2710 if (this_alternative_matches >= 0
2711 && !curr_alt_win[this_alternative_matches]
2712 && REG_P (op)
2713 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2714 && (hard_regno[nop] >= 0
2715 ? in_hard_reg_set_p (this_alternative_set,
2716 mode, hard_regno[nop])
2717 : in_class_p (op, this_alternative, NULL)))
2719 if (lra_dump_file != NULL)
2720 fprintf
2721 (lra_dump_file,
2722 " %d Dying matched operand reload: reject++\n",
2723 nop);
2724 reject++;
2726 else
2728 /* Strict_low_part requires to reload the register
2729 not the sub-register. In this case we should
2730 check that a final reload hard reg can hold the
2731 value mode. */
2732 if (curr_static_id->operand[nop].strict_low
2733 && REG_P (op)
2734 && hard_regno[nop] < 0
2735 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2736 && ira_class_hard_regs_num[this_alternative] > 0
2737 && (!targetm.hard_regno_mode_ok
2738 (ira_class_hard_regs[this_alternative][0],
2739 GET_MODE (*curr_id->operand_loc[nop]))))
2741 if (lra_dump_file != NULL)
2742 fprintf
2743 (lra_dump_file,
2744 " alt=%d: Strict low subreg reload -- refuse\n",
2745 nalt);
2746 goto fail;
2748 losers++;
2750 if (operand_reg[nop] != NULL_RTX
2751 /* Output operands and matched input operands are
2752 not inherited. The following conditions do not
2753 exactly describe the previous statement but they
2754 are pretty close. */
2755 && curr_static_id->operand[nop].type != OP_OUT
2756 && (this_alternative_matches < 0
2757 || curr_static_id->operand[nop].type != OP_IN))
2759 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2760 (operand_reg[nop])]
2761 .last_reload);
2763 /* The value of reload_sum has sense only if we
2764 process insns in their order. It happens only on
2765 the first constraints sub-pass when we do most of
2766 reload work. */
2767 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2768 reload_sum += last_reload - bb_reload_num;
2770 /* If this is a constant that is reloaded into the
2771 desired class by copying it to memory first, count
2772 that as another reload. This is consistent with
2773 other code and is required to avoid choosing another
2774 alternative when the constant is moved into memory.
2775 Note that the test here is precisely the same as in
2776 the code below that calls force_const_mem. */
2777 if (CONST_POOL_OK_P (mode, op)
2778 && ((targetm.preferred_reload_class
2779 (op, this_alternative) == NO_REGS)
2780 || no_input_reloads_p))
2782 const_to_mem = 1;
2783 if (! no_regs_p)
2784 losers++;
2787 /* Alternative loses if it requires a type of reload not
2788 permitted for this insn. We can always reload
2789 objects with a REG_UNUSED note. */
2790 if ((curr_static_id->operand[nop].type != OP_IN
2791 && no_output_reloads_p
2792 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2793 || (curr_static_id->operand[nop].type != OP_OUT
2794 && no_input_reloads_p && ! const_to_mem)
2795 || (this_alternative_matches >= 0
2796 && (no_input_reloads_p
2797 || (no_output_reloads_p
2798 && (curr_static_id->operand
2799 [this_alternative_matches].type != OP_IN)
2800 && ! find_reg_note (curr_insn, REG_UNUSED,
2801 no_subreg_reg_operand
2802 [this_alternative_matches])))))
2804 if (lra_dump_file != NULL)
2805 fprintf
2806 (lra_dump_file,
2807 " alt=%d: No input/output reload -- refuse\n",
2808 nalt);
2809 goto fail;
2812 /* Alternative loses if it required class pseudo cannot
2813 hold value of required mode. Such insns can be
2814 described by insn definitions with mode iterators. */
2815 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2816 && ! hard_reg_set_empty_p (this_alternative_set)
2817 /* It is common practice for constraints to use a
2818 class which does not have actually enough regs to
2819 hold the value (e.g. x86 AREG for mode requiring
2820 more one general reg). Therefore we have 2
2821 conditions to check that the reload pseudo cannot
2822 hold the mode value. */
2823 && (!targetm.hard_regno_mode_ok
2824 (ira_class_hard_regs[this_alternative][0],
2825 GET_MODE (*curr_id->operand_loc[nop])))
2826 /* The above condition is not enough as the first
2827 reg in ira_class_hard_regs can be not aligned for
2828 multi-words mode values. */
2829 && (prohibited_class_reg_set_mode_p
2830 (this_alternative, this_alternative_set,
2831 GET_MODE (*curr_id->operand_loc[nop]))))
2833 if (lra_dump_file != NULL)
2834 fprintf (lra_dump_file,
2835 " alt=%d: reload pseudo for op %d "
2836 "cannot hold the mode value -- refuse\n",
2837 nalt, nop);
2838 goto fail;
2841 /* Check strong discouragement of reload of non-constant
2842 into class THIS_ALTERNATIVE. */
2843 if (! CONSTANT_P (op) && ! no_regs_p
2844 && (targetm.preferred_reload_class
2845 (op, this_alternative) == NO_REGS
2846 || (curr_static_id->operand[nop].type == OP_OUT
2847 && (targetm.preferred_output_reload_class
2848 (op, this_alternative) == NO_REGS))))
2850 if (offmemok && REG_P (op))
2852 if (lra_dump_file != NULL)
2853 fprintf
2854 (lra_dump_file,
2855 " %d Spill pseudo into memory: reject+=3\n",
2856 nop);
2857 reject += 3;
2859 else
2861 if (lra_dump_file != NULL)
2862 fprintf
2863 (lra_dump_file,
2864 " %d Non-prefered reload: reject+=%d\n",
2865 nop, LRA_MAX_REJECT);
2866 reject += LRA_MAX_REJECT;
2870 if (! (MEM_P (op) && offmemok)
2871 && ! (const_to_mem && constmemok))
2873 /* We prefer to reload pseudos over reloading other
2874 things, since such reloads may be able to be
2875 eliminated later. So bump REJECT in other cases.
2876 Don't do this in the case where we are forcing a
2877 constant into memory and it will then win since
2878 we don't want to have a different alternative
2879 match then. */
2880 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2882 if (lra_dump_file != NULL)
2883 fprintf
2884 (lra_dump_file,
2885 " %d Non-pseudo reload: reject+=2\n",
2886 nop);
2887 reject += 2;
2890 if (! no_regs_p)
2891 reload_nregs
2892 += ira_reg_class_max_nregs[this_alternative][mode];
2894 if (SMALL_REGISTER_CLASS_P (this_alternative))
2896 if (lra_dump_file != NULL)
2897 fprintf
2898 (lra_dump_file,
2899 " %d Small class reload: reject+=%d\n",
2900 nop, LRA_LOSER_COST_FACTOR / 2);
2901 reject += LRA_LOSER_COST_FACTOR / 2;
2905 /* We are trying to spill pseudo into memory. It is
2906 usually more costly than moving to a hard register
2907 although it might takes the same number of
2908 reloads.
2910 Non-pseudo spill may happen also. Suppose a target allows both
2911 register and memory in the operand constraint alternatives,
2912 then it's typical that an eliminable register has a substition
2913 of "base + offset" which can either be reloaded by a simple
2914 "new_reg <= base + offset" which will match the register
2915 constraint, or a similar reg addition followed by further spill
2916 to and reload from memory which will match the memory
2917 constraint, but this memory spill will be much more costly
2918 usually.
2920 Code below increases the reject for both pseudo and non-pseudo
2921 spill. */
2922 if (no_regs_p
2923 && !(MEM_P (op) && offmemok)
2924 && !(REG_P (op) && hard_regno[nop] < 0))
2926 if (lra_dump_file != NULL)
2927 fprintf
2928 (lra_dump_file,
2929 " %d Spill %spseudo into memory: reject+=3\n",
2930 nop, REG_P (op) ? "" : "Non-");
2931 reject += 3;
2932 if (VECTOR_MODE_P (mode))
2934 /* Spilling vectors into memory is usually more
2935 costly as they contain big values. */
2936 if (lra_dump_file != NULL)
2937 fprintf
2938 (lra_dump_file,
2939 " %d Spill vector pseudo: reject+=2\n",
2940 nop);
2941 reject += 2;
2945 /* When we use an operand requiring memory in given
2946 alternative, the insn should write *and* read the
2947 value to/from memory it is costly in comparison with
2948 an insn alternative which does not use memory
2949 (e.g. register or immediate operand). We exclude
2950 memory operand for such case as we can satisfy the
2951 memory constraints by reloading address. */
2952 if (no_regs_p && offmemok && !MEM_P (op))
2954 if (lra_dump_file != NULL)
2955 fprintf
2956 (lra_dump_file,
2957 " Using memory insn operand %d: reject+=3\n",
2958 nop);
2959 reject += 3;
2962 /* If reload requires moving value through secondary
2963 memory, it will need one more insn at least. */
2964 if (this_alternative != NO_REGS
2965 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2966 && ((curr_static_id->operand[nop].type != OP_OUT
2967 && targetm.secondary_memory_needed (GET_MODE (op), cl,
2968 this_alternative))
2969 || (curr_static_id->operand[nop].type != OP_IN
2970 && (targetm.secondary_memory_needed
2971 (GET_MODE (op), this_alternative, cl)))))
2972 losers++;
2974 if (MEM_P (op) && offmemok)
2975 addr_losers++;
2976 else
2978 /* Input reloads can be inherited more often than
2979 output reloads can be removed, so penalize output
2980 reloads. */
2981 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2983 if (lra_dump_file != NULL)
2984 fprintf
2985 (lra_dump_file,
2986 " %d Non input pseudo reload: reject++\n",
2987 nop);
2988 reject++;
2991 if (curr_static_id->operand[nop].type == OP_INOUT)
2993 if (lra_dump_file != NULL)
2994 fprintf
2995 (lra_dump_file,
2996 " %d Input/Output reload: reject+=%d\n",
2997 nop, LRA_LOSER_COST_FACTOR);
2998 reject += LRA_LOSER_COST_FACTOR;
3003 if (early_clobber_p && ! scratch_p)
3005 if (lra_dump_file != NULL)
3006 fprintf (lra_dump_file,
3007 " %d Early clobber: reject++\n", nop);
3008 reject++;
3010 /* ??? We check early clobbers after processing all operands
3011 (see loop below) and there we update the costs more.
3012 Should we update the cost (may be approximately) here
3013 because of early clobber register reloads or it is a rare
3014 or non-important thing to be worth to do it. */
3015 overall = (losers * LRA_LOSER_COST_FACTOR + reject
3016 - (addr_losers == losers ? static_reject : 0));
3017 if ((best_losers == 0 || losers != 0) && best_overall < overall)
3019 if (lra_dump_file != NULL)
3020 fprintf (lra_dump_file,
3021 " alt=%d,overall=%d,losers=%d -- refuse\n",
3022 nalt, overall, losers);
3023 goto fail;
3026 if (update_and_check_small_class_inputs (nop, nalt,
3027 this_alternative))
3029 if (lra_dump_file != NULL)
3030 fprintf (lra_dump_file,
3031 " alt=%d, not enough small class regs -- refuse\n",
3032 nalt);
3033 goto fail;
3035 curr_alt[nop] = this_alternative;
3036 curr_alt_set[nop] = this_alternative_set;
3037 curr_alt_exclude_start_hard_regs[nop]
3038 = this_alternative_exclude_start_hard_regs;
3039 curr_alt_win[nop] = this_alternative_win;
3040 curr_alt_match_win[nop] = this_alternative_match_win;
3041 curr_alt_offmemok[nop] = this_alternative_offmemok;
3042 curr_alt_matches[nop] = this_alternative_matches;
3044 if (this_alternative_matches >= 0
3045 && !did_match && !this_alternative_win)
3046 curr_alt_win[this_alternative_matches] = false;
3048 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
3049 early_clobbered_nops[early_clobbered_regs_num++] = nop;
3052 if (curr_insn_set != NULL_RTX && n_operands == 2
3053 /* Prevent processing non-move insns. */
3054 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
3055 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
3056 && ((! curr_alt_win[0] && ! curr_alt_win[1]
3057 && REG_P (no_subreg_reg_operand[0])
3058 && REG_P (no_subreg_reg_operand[1])
3059 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
3060 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
3061 || (! curr_alt_win[0] && curr_alt_win[1]
3062 && REG_P (no_subreg_reg_operand[1])
3063 /* Check that we reload memory not the memory
3064 address. */
3065 && ! (curr_alt_offmemok[0]
3066 && MEM_P (no_subreg_reg_operand[0]))
3067 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
3068 || (curr_alt_win[0] && ! curr_alt_win[1]
3069 && REG_P (no_subreg_reg_operand[0])
3070 /* Check that we reload memory not the memory
3071 address. */
3072 && ! (curr_alt_offmemok[1]
3073 && MEM_P (no_subreg_reg_operand[1]))
3074 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
3075 && (! CONST_POOL_OK_P (curr_operand_mode[1],
3076 no_subreg_reg_operand[1])
3077 || (targetm.preferred_reload_class
3078 (no_subreg_reg_operand[1],
3079 (enum reg_class) curr_alt[1]) != NO_REGS))
3080 /* If it is a result of recent elimination in move
3081 insn we can transform it into an add still by
3082 using this alternative. */
3083 && GET_CODE (no_subreg_reg_operand[1]) != PLUS
3084 /* Likewise if the source has been replaced with an
3085 equivalent value. This only happens once -- the reload
3086 will use the equivalent value instead of the register it
3087 replaces -- so there should be no danger of cycling. */
3088 && !equiv_substition_p[1])))
3090 /* We have a move insn and a new reload insn will be similar
3091 to the current insn. We should avoid such situation as
3092 it results in LRA cycling. */
3093 if (lra_dump_file != NULL)
3094 fprintf (lra_dump_file,
3095 " Cycle danger: overall += LRA_MAX_REJECT\n");
3096 overall += LRA_MAX_REJECT;
3098 ok_p = true;
3099 curr_alt_dont_inherit_ops_num = 0;
3100 for (nop = 0; nop < early_clobbered_regs_num; nop++)
3102 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
3103 HARD_REG_SET temp_set;
3105 i = early_clobbered_nops[nop];
3106 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
3107 || hard_regno[i] < 0)
3108 continue;
3109 lra_assert (operand_reg[i] != NULL_RTX);
3110 clobbered_hard_regno = hard_regno[i];
3111 CLEAR_HARD_REG_SET (temp_set);
3112 add_to_hard_reg_set (&temp_set, GET_MODE (*curr_id->operand_loc[i]),
3113 clobbered_hard_regno);
3114 first_conflict_j = last_conflict_j = -1;
3115 for (j = 0; j < n_operands; j++)
3116 if (j == i
3117 /* We don't want process insides of match_operator and
3118 match_parallel because otherwise we would process
3119 their operands once again generating a wrong
3120 code. */
3121 || curr_static_id->operand[j].is_operator)
3122 continue;
3123 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
3124 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
3125 continue;
3126 /* If we don't reload j-th operand, check conflicts. */
3127 else if ((curr_alt_win[j] || curr_alt_match_win[j])
3128 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
3130 if (first_conflict_j < 0)
3131 first_conflict_j = j;
3132 last_conflict_j = j;
3133 /* Both the earlyclobber operand and conflicting operand
3134 cannot both be user defined hard registers. */
3135 if (HARD_REGISTER_P (operand_reg[i])
3136 && REG_USERVAR_P (operand_reg[i])
3137 && operand_reg[j] != NULL_RTX
3138 && HARD_REGISTER_P (operand_reg[j])
3139 && REG_USERVAR_P (operand_reg[j]))
3141 /* For asm, let curr_insn_transform diagnose it. */
3142 if (INSN_CODE (curr_insn) < 0)
3143 return false;
3144 fatal_insn ("unable to generate reloads for "
3145 "impossible constraints:", curr_insn);
3148 if (last_conflict_j < 0)
3149 continue;
3151 /* If an earlyclobber operand conflicts with another non-matching
3152 operand (ie, they have been assigned the same hard register),
3153 then it is better to reload the other operand, as there may
3154 exist yet another operand with a matching constraint associated
3155 with the earlyclobber operand. However, if one of the operands
3156 is an explicit use of a hard register, then we must reload the
3157 other non-hard register operand. */
3158 if (HARD_REGISTER_P (operand_reg[i])
3159 || (first_conflict_j == last_conflict_j
3160 && operand_reg[last_conflict_j] != NULL_RTX
3161 && !curr_alt_match_win[last_conflict_j]
3162 && !HARD_REGISTER_P (operand_reg[last_conflict_j])))
3164 curr_alt_win[last_conflict_j] = false;
3165 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
3166 = last_conflict_j;
3167 losers++;
3168 if (lra_dump_file != NULL)
3169 fprintf
3170 (lra_dump_file,
3171 " %d Conflict early clobber reload: reject--\n",
3174 else
3176 /* We need to reload early clobbered register and the
3177 matched registers. */
3178 for (j = 0; j < n_operands; j++)
3179 if (curr_alt_matches[j] == i)
3181 curr_alt_match_win[j] = false;
3182 losers++;
3183 overall += LRA_LOSER_COST_FACTOR;
3185 if (! curr_alt_match_win[i])
3186 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
3187 else
3189 /* Remember pseudos used for match reloads are never
3190 inherited. */
3191 lra_assert (curr_alt_matches[i] >= 0);
3192 curr_alt_win[curr_alt_matches[i]] = false;
3194 curr_alt_win[i] = curr_alt_match_win[i] = false;
3195 losers++;
3196 if (lra_dump_file != NULL)
3197 fprintf
3198 (lra_dump_file,
3199 " %d Matched conflict early clobber reloads: "
3200 "reject--\n",
3203 /* Early clobber was already reflected in REJECT. */
3204 if (!matching_early_clobber[i])
3206 lra_assert (reject > 0);
3207 reject--;
3208 matching_early_clobber[i] = 1;
3210 overall += LRA_LOSER_COST_FACTOR - 1;
3212 if (lra_dump_file != NULL)
3213 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
3214 nalt, overall, losers, reload_nregs);
3216 /* If this alternative can be made to work by reloading, and it
3217 needs less reloading than the others checked so far, record
3218 it as the chosen goal for reloading. */
3219 if ((best_losers != 0 && losers == 0)
3220 || (((best_losers == 0 && losers == 0)
3221 || (best_losers != 0 && losers != 0))
3222 && (best_overall > overall
3223 || (best_overall == overall
3224 /* If the cost of the reloads is the same,
3225 prefer alternative which requires minimal
3226 number of reload regs. */
3227 && (reload_nregs < best_reload_nregs
3228 || (reload_nregs == best_reload_nregs
3229 && (best_reload_sum < reload_sum
3230 || (best_reload_sum == reload_sum
3231 && nalt < goal_alt_number))))))))
3233 for (nop = 0; nop < n_operands; nop++)
3235 goal_alt_win[nop] = curr_alt_win[nop];
3236 goal_alt_match_win[nop] = curr_alt_match_win[nop];
3237 goal_alt_matches[nop] = curr_alt_matches[nop];
3238 goal_alt[nop] = curr_alt[nop];
3239 goal_alt_exclude_start_hard_regs[nop]
3240 = curr_alt_exclude_start_hard_regs[nop];
3241 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
3243 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
3244 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
3245 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
3246 goal_alt_swapped = curr_swapped;
3247 best_overall = overall;
3248 best_losers = losers;
3249 best_reload_nregs = reload_nregs;
3250 best_reload_sum = reload_sum;
3251 goal_alt_number = nalt;
3253 if (losers == 0)
3254 /* Everything is satisfied. Do not process alternatives
3255 anymore. */
3256 break;
3257 fail:
3260 return ok_p;
3263 /* Make reload base reg from address AD. */
3264 static rtx
3265 base_to_reg (struct address_info *ad)
3267 enum reg_class cl;
3268 int code = -1;
3269 rtx new_inner = NULL_RTX;
3270 rtx new_reg = NULL_RTX;
3271 rtx_insn *insn;
3272 rtx_insn *last_insn = get_last_insn();
3274 lra_assert (ad->disp == ad->disp_term);
3275 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
3276 get_index_code (ad));
3277 new_reg = lra_create_new_reg (GET_MODE (*ad->base), NULL_RTX, cl, NULL,
3278 "base");
3279 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
3280 ad->disp_term == NULL
3281 ? const0_rtx
3282 : *ad->disp_term);
3283 if (!valid_address_p (ad->mode, new_inner, ad->as))
3284 return NULL_RTX;
3285 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base));
3286 code = recog_memoized (insn);
3287 if (code < 0)
3289 delete_insns_since (last_insn);
3290 return NULL_RTX;
3293 return new_inner;
3296 /* Make reload base reg + DISP from address AD. Return the new pseudo. */
3297 static rtx
3298 base_plus_disp_to_reg (struct address_info *ad, rtx disp)
3300 enum reg_class cl;
3301 rtx new_reg;
3303 lra_assert (ad->base == ad->base_term);
3304 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
3305 get_index_code (ad));
3306 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX, cl, NULL,
3307 "base + disp");
3308 lra_emit_add (new_reg, *ad->base_term, disp);
3309 return new_reg;
3312 /* Make reload of index part of address AD. Return the new
3313 pseudo. */
3314 static rtx
3315 index_part_to_reg (struct address_info *ad)
3317 rtx new_reg;
3319 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
3320 INDEX_REG_CLASS, NULL, "index term");
3321 expand_mult (GET_MODE (*ad->index), *ad->index_term,
3322 GEN_INT (get_index_scale (ad)), new_reg, 1);
3323 return new_reg;
3326 /* Return true if we can add a displacement to address AD, even if that
3327 makes the address invalid. The fix-up code requires any new address
3328 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
3329 static bool
3330 can_add_disp_p (struct address_info *ad)
3332 return (!ad->autoinc_p
3333 && ad->segment == NULL
3334 && ad->base == ad->base_term
3335 && ad->disp == ad->disp_term);
3338 /* Make equiv substitution in address AD. Return true if a substitution
3339 was made. */
3340 static bool
3341 equiv_address_substitution (struct address_info *ad)
3343 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
3344 poly_int64 disp;
3345 HOST_WIDE_INT scale;
3346 bool change_p;
3348 base_term = strip_subreg (ad->base_term);
3349 if (base_term == NULL)
3350 base_reg = new_base_reg = NULL_RTX;
3351 else
3353 base_reg = *base_term;
3354 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
3356 index_term = strip_subreg (ad->index_term);
3357 if (index_term == NULL)
3358 index_reg = new_index_reg = NULL_RTX;
3359 else
3361 index_reg = *index_term;
3362 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
3364 if (base_reg == new_base_reg && index_reg == new_index_reg)
3365 return false;
3366 disp = 0;
3367 change_p = false;
3368 if (lra_dump_file != NULL)
3370 fprintf (lra_dump_file, "Changing address in insn %d ",
3371 INSN_UID (curr_insn));
3372 dump_value_slim (lra_dump_file, *ad->outer, 1);
3374 if (base_reg != new_base_reg)
3376 poly_int64 offset;
3377 if (REG_P (new_base_reg))
3379 *base_term = new_base_reg;
3380 change_p = true;
3382 else if (GET_CODE (new_base_reg) == PLUS
3383 && REG_P (XEXP (new_base_reg, 0))
3384 && poly_int_rtx_p (XEXP (new_base_reg, 1), &offset)
3385 && can_add_disp_p (ad))
3387 disp += offset;
3388 *base_term = XEXP (new_base_reg, 0);
3389 change_p = true;
3391 if (ad->base_term2 != NULL)
3392 *ad->base_term2 = *ad->base_term;
3394 if (index_reg != new_index_reg)
3396 poly_int64 offset;
3397 if (REG_P (new_index_reg))
3399 *index_term = new_index_reg;
3400 change_p = true;
3402 else if (GET_CODE (new_index_reg) == PLUS
3403 && REG_P (XEXP (new_index_reg, 0))
3404 && poly_int_rtx_p (XEXP (new_index_reg, 1), &offset)
3405 && can_add_disp_p (ad)
3406 && (scale = get_index_scale (ad)))
3408 disp += offset * scale;
3409 *index_term = XEXP (new_index_reg, 0);
3410 change_p = true;
3413 if (maybe_ne (disp, 0))
3415 if (ad->disp != NULL)
3416 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
3417 else
3419 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
3420 update_address (ad);
3422 change_p = true;
3424 if (lra_dump_file != NULL)
3426 if (! change_p)
3427 fprintf (lra_dump_file, " -- no change\n");
3428 else
3430 fprintf (lra_dump_file, " on equiv ");
3431 dump_value_slim (lra_dump_file, *ad->outer, 1);
3432 fprintf (lra_dump_file, "\n");
3435 return change_p;
3438 /* Skip all modifiers and whitespaces in constraint STR and return the
3439 result. */
3440 static const char *
3441 skip_constraint_modifiers (const char *str)
3443 for (;;str++)
3444 switch (*str)
3446 case '+': case '&' : case '=': case '*': case ' ': case '\t':
3447 case '$': case '^' : case '%': case '?': case '!':
3448 break;
3449 default: return str;
3453 /* Major function to make reloads for an address in operand NOP or
3454 check its correctness (If CHECK_ONLY_P is true). The supported
3455 cases are:
3457 1) an address that existed before LRA started, at which point it
3458 must have been valid. These addresses are subject to elimination
3459 and may have become invalid due to the elimination offset being out
3460 of range.
3462 2) an address created by forcing a constant to memory
3463 (force_const_to_mem). The initial form of these addresses might
3464 not be valid, and it is this function's job to make them valid.
3466 3) a frame address formed from a register and a (possibly zero)
3467 constant offset. As above, these addresses might not be valid and
3468 this function must make them so.
3470 Add reloads to the lists *BEFORE and *AFTER. We might need to add
3471 reloads to *AFTER because of inc/dec, {pre, post} modify in the
3472 address. Return true for any RTL change.
3474 The function is a helper function which does not produce all
3475 transformations (when CHECK_ONLY_P is false) which can be
3476 necessary. It does just basic steps. To do all necessary
3477 transformations use function process_address. */
3478 static bool
3479 process_address_1 (int nop, bool check_only_p,
3480 rtx_insn **before, rtx_insn **after)
3482 struct address_info ad;
3483 rtx new_reg;
3484 HOST_WIDE_INT scale;
3485 rtx op = *curr_id->operand_loc[nop];
3486 rtx mem = extract_mem_from_operand (op);
3487 const char *constraint;
3488 enum constraint_num cn;
3489 bool change_p = false;
3491 if (MEM_P (mem)
3492 && GET_MODE (mem) == BLKmode
3493 && GET_CODE (XEXP (mem, 0)) == SCRATCH)
3494 return false;
3496 constraint
3497 = skip_constraint_modifiers (curr_static_id->operand[nop].constraint);
3498 if (IN_RANGE (constraint[0], '0', '9'))
3500 char *end;
3501 unsigned long dup = strtoul (constraint, &end, 10);
3502 constraint
3503 = skip_constraint_modifiers (curr_static_id->operand[dup].constraint);
3505 cn = lookup_constraint (*constraint == '\0' ? "X" : constraint);
3506 /* If we have several alternatives or/and several constraints in an
3507 alternative and we can not say at this stage what constraint will be used,
3508 use unknown constraint. The exception is an address constraint. If
3509 operand has one address constraint, probably all others constraints are
3510 address ones. */
3511 if (constraint[0] != '\0' && get_constraint_type (cn) != CT_ADDRESS
3512 && *skip_constraint_modifiers (constraint
3513 + CONSTRAINT_LEN (constraint[0],
3514 constraint)) != '\0')
3515 cn = CONSTRAINT__UNKNOWN;
3516 if (insn_extra_address_constraint (cn)
3517 /* When we find an asm operand with an address constraint that
3518 doesn't satisfy address_operand to begin with, we clear
3519 is_address, so that we don't try to make a non-address fit.
3520 If the asm statement got this far, it's because other
3521 constraints are available, and we'll use them, disregarding
3522 the unsatisfiable address ones. */
3523 && curr_static_id->operand[nop].is_address)
3524 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
3525 /* Do not attempt to decompose arbitrary addresses generated by combine
3526 for asm operands with loose constraints, e.g 'X'.
3527 Need to extract memory from op for special memory constraint,
3528 i.e. bcst_mem_operand in i386 backend. */
3529 else if (MEM_P (mem)
3530 && !(INSN_CODE (curr_insn) < 0
3531 && get_constraint_type (cn) == CT_FIXED_FORM
3532 && constraint_satisfied_p (op, cn)))
3533 decompose_mem_address (&ad, mem);
3534 else if (GET_CODE (op) == SUBREG
3535 && MEM_P (SUBREG_REG (op)))
3536 decompose_mem_address (&ad, SUBREG_REG (op));
3537 else
3538 return false;
3539 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
3540 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
3541 when INDEX_REG_CLASS is a single register class. */
3542 if (ad.base_term != NULL
3543 && ad.index_term != NULL
3544 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
3545 && REG_P (*ad.base_term)
3546 && REG_P (*ad.index_term)
3547 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
3548 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
3550 std::swap (ad.base, ad.index);
3551 std::swap (ad.base_term, ad.index_term);
3553 if (! check_only_p)
3554 change_p = equiv_address_substitution (&ad);
3555 if (ad.base_term != NULL
3556 && (process_addr_reg
3557 (ad.base_term, check_only_p, before,
3558 (ad.autoinc_p
3559 && !(REG_P (*ad.base_term)
3560 && find_regno_note (curr_insn, REG_DEAD,
3561 REGNO (*ad.base_term)) != NULL_RTX)
3562 ? after : NULL),
3563 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3564 get_index_code (&ad)))))
3566 change_p = true;
3567 if (ad.base_term2 != NULL)
3568 *ad.base_term2 = *ad.base_term;
3570 if (ad.index_term != NULL
3571 && process_addr_reg (ad.index_term, check_only_p,
3572 before, NULL, INDEX_REG_CLASS))
3573 change_p = true;
3575 /* Target hooks sometimes don't treat extra-constraint addresses as
3576 legitimate address_operands, so handle them specially. */
3577 if (insn_extra_address_constraint (cn)
3578 && satisfies_address_constraint_p (&ad, cn))
3579 return change_p;
3581 if (check_only_p)
3582 return change_p;
3584 /* There are three cases where the shape of *AD.INNER may now be invalid:
3586 1) the original address was valid, but either elimination or
3587 equiv_address_substitution was applied and that made
3588 the address invalid.
3590 2) the address is an invalid symbolic address created by
3591 force_const_to_mem.
3593 3) the address is a frame address with an invalid offset.
3595 4) the address is a frame address with an invalid base.
3597 All these cases involve a non-autoinc address, so there is no
3598 point revalidating other types. */
3599 if (ad.autoinc_p || valid_address_p (op, &ad, cn))
3600 return change_p;
3602 /* Any index existed before LRA started, so we can assume that the
3603 presence and shape of the index is valid. */
3604 push_to_sequence (*before);
3605 lra_assert (ad.disp == ad.disp_term);
3606 if (ad.base == NULL)
3608 if (ad.index == NULL)
3610 rtx_insn *insn;
3611 rtx_insn *last = get_last_insn ();
3612 int code = -1;
3613 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3614 SCRATCH, SCRATCH);
3615 rtx addr = *ad.inner;
3617 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, NULL, "addr");
3618 if (HAVE_lo_sum)
3620 /* addr => lo_sum (new_base, addr), case (2) above. */
3621 insn = emit_insn (gen_rtx_SET
3622 (new_reg,
3623 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
3624 code = recog_memoized (insn);
3625 if (code >= 0)
3627 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
3628 if (!valid_address_p (op, &ad, cn))
3630 /* Try to put lo_sum into register. */
3631 insn = emit_insn (gen_rtx_SET
3632 (new_reg,
3633 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
3634 code = recog_memoized (insn);
3635 if (code >= 0)
3637 *ad.inner = new_reg;
3638 if (!valid_address_p (op, &ad, cn))
3640 *ad.inner = addr;
3641 code = -1;
3647 if (code < 0)
3648 delete_insns_since (last);
3651 if (code < 0)
3653 /* addr => new_base, case (2) above. */
3654 lra_emit_move (new_reg, addr);
3656 for (insn = last == NULL_RTX ? get_insns () : NEXT_INSN (last);
3657 insn != NULL_RTX;
3658 insn = NEXT_INSN (insn))
3659 if (recog_memoized (insn) < 0)
3660 break;
3661 if (insn != NULL_RTX)
3663 /* Do nothing if we cannot generate right insns.
3664 This is analogous to reload pass behavior. */
3665 delete_insns_since (last);
3666 end_sequence ();
3667 return false;
3669 *ad.inner = new_reg;
3672 else
3674 /* index * scale + disp => new base + index * scale,
3675 case (1) above. */
3676 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3677 GET_CODE (*ad.index));
3679 lra_assert (INDEX_REG_CLASS != NO_REGS);
3680 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, NULL, "disp");
3681 lra_emit_move (new_reg, *ad.disp);
3682 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3683 new_reg, *ad.index);
3686 else if (ad.index == NULL)
3688 int regno;
3689 enum reg_class cl;
3690 rtx set;
3691 rtx_insn *insns, *last_insn;
3692 /* Try to reload base into register only if the base is invalid
3693 for the address but with valid offset, case (4) above. */
3694 start_sequence ();
3695 new_reg = base_to_reg (&ad);
3697 /* base + disp => new base, cases (1) and (3) above. */
3698 /* Another option would be to reload the displacement into an
3699 index register. However, postreload has code to optimize
3700 address reloads that have the same base and different
3701 displacements, so reloading into an index register would
3702 not necessarily be a win. */
3703 if (new_reg == NULL_RTX)
3705 /* See if the target can split the displacement into a
3706 legitimate new displacement from a local anchor. */
3707 gcc_assert (ad.disp == ad.disp_term);
3708 poly_int64 orig_offset;
3709 rtx offset1, offset2;
3710 if (poly_int_rtx_p (*ad.disp, &orig_offset)
3711 && targetm.legitimize_address_displacement (&offset1, &offset2,
3712 orig_offset,
3713 ad.mode))
3715 new_reg = base_plus_disp_to_reg (&ad, offset1);
3716 new_reg = gen_rtx_PLUS (GET_MODE (new_reg), new_reg, offset2);
3718 else
3719 new_reg = base_plus_disp_to_reg (&ad, *ad.disp);
3721 insns = get_insns ();
3722 last_insn = get_last_insn ();
3723 /* If we generated at least two insns, try last insn source as
3724 an address. If we succeed, we generate one less insn. */
3725 if (REG_P (new_reg)
3726 && last_insn != insns
3727 && (set = single_set (last_insn)) != NULL_RTX
3728 && GET_CODE (SET_SRC (set)) == PLUS
3729 && REG_P (XEXP (SET_SRC (set), 0))
3730 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3732 *ad.inner = SET_SRC (set);
3733 if (valid_address_p (op, &ad, cn))
3735 *ad.base_term = XEXP (SET_SRC (set), 0);
3736 *ad.disp_term = XEXP (SET_SRC (set), 1);
3737 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3738 get_index_code (&ad));
3739 regno = REGNO (*ad.base_term);
3740 if (regno >= FIRST_PSEUDO_REGISTER
3741 && cl != lra_get_allocno_class (regno))
3742 lra_change_class (regno, cl, " Change to", true);
3743 new_reg = SET_SRC (set);
3744 delete_insns_since (PREV_INSN (last_insn));
3747 end_sequence ();
3748 emit_insn (insns);
3749 *ad.inner = new_reg;
3751 else if (ad.disp_term != NULL)
3753 /* base + scale * index + disp => new base + scale * index,
3754 case (1) above. */
3755 gcc_assert (ad.disp == ad.disp_term);
3756 new_reg = base_plus_disp_to_reg (&ad, *ad.disp);
3757 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3758 new_reg, *ad.index);
3760 else if ((scale = get_index_scale (&ad)) == 1)
3762 /* The last transformation to one reg will be made in
3763 curr_insn_transform function. */
3764 end_sequence ();
3765 return false;
3767 else if (scale != 0)
3769 /* base + scale * index => base + new_reg,
3770 case (1) above.
3771 Index part of address may become invalid. For example, we
3772 changed pseudo on the equivalent memory and a subreg of the
3773 pseudo onto the memory of different mode for which the scale is
3774 prohibitted. */
3775 new_reg = index_part_to_reg (&ad);
3776 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3777 *ad.base_term, new_reg);
3779 else
3781 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3782 SCRATCH, SCRATCH);
3783 rtx addr = *ad.inner;
3785 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, NULL, "addr");
3786 /* addr => new_base. */
3787 lra_emit_move (new_reg, addr);
3788 *ad.inner = new_reg;
3790 *before = get_insns ();
3791 end_sequence ();
3792 return true;
3795 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3796 Use process_address_1 as a helper function. Return true for any
3797 RTL changes.
3799 If CHECK_ONLY_P is true, just check address correctness. Return
3800 false if the address correct. */
3801 static bool
3802 process_address (int nop, bool check_only_p,
3803 rtx_insn **before, rtx_insn **after)
3805 bool res = false;
3807 while (process_address_1 (nop, check_only_p, before, after))
3809 if (check_only_p)
3810 return true;
3811 res = true;
3813 return res;
3816 /* Emit insns to reload VALUE into a new register. VALUE is an
3817 auto-increment or auto-decrement RTX whose operand is a register or
3818 memory location; so reloading involves incrementing that location.
3819 IN is either identical to VALUE, or some cheaper place to reload
3820 value being incremented/decremented from.
3822 INC_AMOUNT is the number to increment or decrement by (always
3823 positive and ignored for POST_MODIFY/PRE_MODIFY).
3825 Return pseudo containing the result. */
3826 static rtx
3827 emit_inc (enum reg_class new_rclass, rtx in, rtx value, poly_int64 inc_amount)
3829 /* REG or MEM to be copied and incremented. */
3830 rtx incloc = XEXP (value, 0);
3831 /* Nonzero if increment after copying. */
3832 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3833 || GET_CODE (value) == POST_MODIFY);
3834 rtx_insn *last;
3835 rtx inc;
3836 rtx_insn *add_insn;
3837 int code;
3838 rtx real_in = in == value ? incloc : in;
3839 rtx result;
3840 bool plus_p = true;
3842 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3844 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3845 || GET_CODE (XEXP (value, 1)) == MINUS);
3846 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3847 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3848 inc = XEXP (XEXP (value, 1), 1);
3850 else
3852 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3853 inc_amount = -inc_amount;
3855 inc = gen_int_mode (inc_amount, GET_MODE (value));
3858 if (! post && REG_P (incloc))
3859 result = incloc;
3860 else
3861 result = lra_create_new_reg (GET_MODE (value), value, new_rclass, NULL,
3862 "INC/DEC result");
3864 if (real_in != result)
3866 /* First copy the location to the result register. */
3867 lra_assert (REG_P (result));
3868 emit_insn (gen_move_insn (result, real_in));
3871 /* We suppose that there are insns to add/sub with the constant
3872 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3873 old reload worked with this assumption. If the assumption
3874 becomes wrong, we should use approach in function
3875 base_plus_disp_to_reg. */
3876 if (in == value)
3878 /* See if we can directly increment INCLOC. */
3879 last = get_last_insn ();
3880 add_insn = emit_insn (plus_p
3881 ? gen_add2_insn (incloc, inc)
3882 : gen_sub2_insn (incloc, inc));
3884 code = recog_memoized (add_insn);
3885 if (code >= 0)
3887 if (! post && result != incloc)
3888 emit_insn (gen_move_insn (result, incloc));
3889 return result;
3891 delete_insns_since (last);
3894 /* If couldn't do the increment directly, must increment in RESULT.
3895 The way we do this depends on whether this is pre- or
3896 post-increment. For pre-increment, copy INCLOC to the reload
3897 register, increment it there, then save back. */
3898 if (! post)
3900 if (real_in != result)
3901 emit_insn (gen_move_insn (result, real_in));
3902 if (plus_p)
3903 emit_insn (gen_add2_insn (result, inc));
3904 else
3905 emit_insn (gen_sub2_insn (result, inc));
3906 if (result != incloc)
3907 emit_insn (gen_move_insn (incloc, result));
3909 else
3911 /* Post-increment.
3913 Because this might be a jump insn or a compare, and because
3914 RESULT may not be available after the insn in an input
3915 reload, we must do the incrementing before the insn being
3916 reloaded for.
3918 We have already copied IN to RESULT. Increment the copy in
3919 RESULT, save that back, then decrement RESULT so it has
3920 the original value. */
3921 if (plus_p)
3922 emit_insn (gen_add2_insn (result, inc));
3923 else
3924 emit_insn (gen_sub2_insn (result, inc));
3925 emit_insn (gen_move_insn (incloc, result));
3926 /* Restore non-modified value for the result. We prefer this
3927 way because it does not require an additional hard
3928 register. */
3929 if (plus_p)
3931 poly_int64 offset;
3932 if (poly_int_rtx_p (inc, &offset))
3933 emit_insn (gen_add2_insn (result,
3934 gen_int_mode (-offset,
3935 GET_MODE (result))));
3936 else
3937 emit_insn (gen_sub2_insn (result, inc));
3939 else
3940 emit_insn (gen_add2_insn (result, inc));
3942 return result;
3945 /* Return true if the current move insn does not need processing as we
3946 already know that it satisfies its constraints. */
3947 static bool
3948 simple_move_p (void)
3950 rtx dest, src;
3951 enum reg_class dclass, sclass;
3953 lra_assert (curr_insn_set != NULL_RTX);
3954 dest = SET_DEST (curr_insn_set);
3955 src = SET_SRC (curr_insn_set);
3957 /* If the instruction has multiple sets we need to process it even if it
3958 is single_set. This can happen if one or more of the SETs are dead.
3959 See PR73650. */
3960 if (multiple_sets (curr_insn))
3961 return false;
3963 return ((dclass = get_op_class (dest)) != NO_REGS
3964 && (sclass = get_op_class (src)) != NO_REGS
3965 /* The backend guarantees that register moves of cost 2
3966 never need reloads. */
3967 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3970 /* Swap operands NOP and NOP + 1. */
3971 static inline void
3972 swap_operands (int nop)
3974 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3975 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3976 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3977 std::swap (equiv_substition_p[nop], equiv_substition_p[nop + 1]);
3978 /* Swap the duplicates too. */
3979 lra_update_dup (curr_id, nop);
3980 lra_update_dup (curr_id, nop + 1);
3983 /* Main entry point of the constraint code: search the body of the
3984 current insn to choose the best alternative. It is mimicking insn
3985 alternative cost calculation model of former reload pass. That is
3986 because machine descriptions were written to use this model. This
3987 model can be changed in future. Make commutative operand exchange
3988 if it is chosen.
3990 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3991 constraints. Return true if any change happened during function
3992 call.
3994 If CHECK_ONLY_P is true then don't do any transformation. Just
3995 check that the insn satisfies all constraints. If the insn does
3996 not satisfy any constraint, return true. */
3997 static bool
3998 curr_insn_transform (bool check_only_p)
4000 int i, j, k;
4001 int n_operands;
4002 int n_alternatives;
4003 int n_outputs;
4004 int commutative;
4005 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
4006 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
4007 signed char outputs[MAX_RECOG_OPERANDS + 1];
4008 rtx_insn *before, *after;
4009 bool alt_p = false;
4010 /* Flag that the insn has been changed through a transformation. */
4011 bool change_p;
4012 bool sec_mem_p;
4013 bool use_sec_mem_p;
4014 int max_regno_before;
4015 int reused_alternative_num;
4017 curr_insn_set = single_set (curr_insn);
4018 if (curr_insn_set != NULL_RTX && simple_move_p ())
4020 /* We assume that the corresponding insn alternative has no
4021 earlier clobbers. If it is not the case, don't define move
4022 cost equal to 2 for the corresponding register classes. */
4023 lra_set_used_insn_alternative (curr_insn, LRA_NON_CLOBBERED_ALT);
4024 return false;
4027 no_input_reloads_p = no_output_reloads_p = false;
4028 goal_alt_number = -1;
4029 change_p = sec_mem_p = false;
4031 /* CALL_INSNs are not allowed to have any output reloads. */
4032 if (CALL_P (curr_insn))
4033 no_output_reloads_p = true;
4035 n_operands = curr_static_id->n_operands;
4036 n_alternatives = curr_static_id->n_alternatives;
4038 /* Just return "no reloads" if insn has no operands with
4039 constraints. */
4040 if (n_operands == 0 || n_alternatives == 0)
4041 return false;
4043 max_regno_before = max_reg_num ();
4045 for (i = 0; i < n_operands; i++)
4047 goal_alt_matched[i][0] = -1;
4048 goal_alt_matches[i] = -1;
4051 commutative = curr_static_id->commutative;
4053 /* Now see what we need for pseudos that didn't get hard regs or got
4054 the wrong kind of hard reg. For this, we must consider all the
4055 operands together against the register constraints. */
4057 best_losers = best_overall = INT_MAX;
4058 best_reload_sum = 0;
4060 curr_swapped = false;
4061 goal_alt_swapped = false;
4063 if (! check_only_p)
4064 /* Make equivalence substitution and memory subreg elimination
4065 before address processing because an address legitimacy can
4066 depend on memory mode. */
4067 for (i = 0; i < n_operands; i++)
4069 rtx op, subst, old;
4070 bool op_change_p = false;
4072 if (curr_static_id->operand[i].is_operator)
4073 continue;
4075 old = op = *curr_id->operand_loc[i];
4076 if (GET_CODE (old) == SUBREG)
4077 old = SUBREG_REG (old);
4078 subst = get_equiv_with_elimination (old, curr_insn);
4079 original_subreg_reg_mode[i] = VOIDmode;
4080 equiv_substition_p[i] = false;
4081 if (subst != old)
4083 equiv_substition_p[i] = true;
4084 subst = copy_rtx (subst);
4085 lra_assert (REG_P (old));
4086 if (GET_CODE (op) != SUBREG)
4087 *curr_id->operand_loc[i] = subst;
4088 else
4090 SUBREG_REG (op) = subst;
4091 if (GET_MODE (subst) == VOIDmode)
4092 original_subreg_reg_mode[i] = GET_MODE (old);
4094 if (lra_dump_file != NULL)
4096 fprintf (lra_dump_file,
4097 "Changing pseudo %d in operand %i of insn %u on equiv ",
4098 REGNO (old), i, INSN_UID (curr_insn));
4099 dump_value_slim (lra_dump_file, subst, 1);
4100 fprintf (lra_dump_file, "\n");
4102 op_change_p = change_p = true;
4104 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
4106 change_p = true;
4107 lra_update_dup (curr_id, i);
4111 /* Reload address registers and displacements. We do it before
4112 finding an alternative because of memory constraints. */
4113 before = after = NULL;
4114 for (i = 0; i < n_operands; i++)
4115 if (! curr_static_id->operand[i].is_operator
4116 && process_address (i, check_only_p, &before, &after))
4118 if (check_only_p)
4119 return true;
4120 change_p = true;
4121 lra_update_dup (curr_id, i);
4124 if (change_p)
4125 /* If we've changed the instruction then any alternative that
4126 we chose previously may no longer be valid. */
4127 lra_set_used_insn_alternative (curr_insn, LRA_UNKNOWN_ALT);
4129 if (! check_only_p && curr_insn_set != NULL_RTX
4130 && check_and_process_move (&change_p, &sec_mem_p))
4131 return change_p;
4133 try_swapped:
4135 reused_alternative_num = check_only_p ? LRA_UNKNOWN_ALT : curr_id->used_insn_alternative;
4136 if (lra_dump_file != NULL && reused_alternative_num >= 0)
4137 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
4138 reused_alternative_num, INSN_UID (curr_insn));
4140 if (process_alt_operands (reused_alternative_num))
4141 alt_p = true;
4143 if (check_only_p)
4144 return ! alt_p || best_losers != 0;
4146 /* If insn is commutative (it's safe to exchange a certain pair of
4147 operands) then we need to try each alternative twice, the second
4148 time matching those two operands as if we had exchanged them. To
4149 do this, really exchange them in operands.
4151 If we have just tried the alternatives the second time, return
4152 operands to normal and drop through. */
4154 if (reused_alternative_num < 0 && commutative >= 0)
4156 curr_swapped = !curr_swapped;
4157 if (curr_swapped)
4159 swap_operands (commutative);
4160 goto try_swapped;
4162 else
4163 swap_operands (commutative);
4166 if (! alt_p && ! sec_mem_p)
4168 /* No alternative works with reloads?? */
4169 if (INSN_CODE (curr_insn) >= 0)
4170 fatal_insn ("unable to generate reloads for:", curr_insn);
4171 error_for_asm (curr_insn,
4172 "inconsistent operand constraints in an %<asm%>");
4173 lra_asm_error_p = true;
4174 if (! JUMP_P (curr_insn))
4176 /* Avoid further trouble with this insn. Don't generate use
4177 pattern here as we could use the insn SP offset. */
4178 lra_set_insn_deleted (curr_insn);
4180 else
4182 lra_invalidate_insn_data (curr_insn);
4183 ira_nullify_asm_goto (curr_insn);
4184 lra_update_insn_regno_info (curr_insn);
4186 return true;
4189 /* If the best alternative is with operands 1 and 2 swapped, swap
4190 them. Update the operand numbers of any reloads already
4191 pushed. */
4193 if (goal_alt_swapped)
4195 if (lra_dump_file != NULL)
4196 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
4197 INSN_UID (curr_insn));
4199 /* Swap the duplicates too. */
4200 swap_operands (commutative);
4201 change_p = true;
4204 /* Some targets' TARGET_SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
4205 too conservatively. So we use the secondary memory only if there
4206 is no any alternative without reloads. */
4207 use_sec_mem_p = false;
4208 if (! alt_p)
4209 use_sec_mem_p = true;
4210 else if (sec_mem_p)
4212 for (i = 0; i < n_operands; i++)
4213 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
4214 break;
4215 use_sec_mem_p = i < n_operands;
4218 if (use_sec_mem_p)
4220 int in = -1, out = -1;
4221 rtx new_reg, src, dest, rld;
4222 machine_mode sec_mode, rld_mode;
4224 lra_assert (curr_insn_set != NULL_RTX && sec_mem_p);
4225 dest = SET_DEST (curr_insn_set);
4226 src = SET_SRC (curr_insn_set);
4227 for (i = 0; i < n_operands; i++)
4228 if (*curr_id->operand_loc[i] == dest)
4229 out = i;
4230 else if (*curr_id->operand_loc[i] == src)
4231 in = i;
4232 for (i = 0; i < curr_static_id->n_dups; i++)
4233 if (out < 0 && *curr_id->dup_loc[i] == dest)
4234 out = curr_static_id->dup_num[i];
4235 else if (in < 0 && *curr_id->dup_loc[i] == src)
4236 in = curr_static_id->dup_num[i];
4237 lra_assert (out >= 0 && in >= 0
4238 && curr_static_id->operand[out].type == OP_OUT
4239 && curr_static_id->operand[in].type == OP_IN);
4240 rld = partial_subreg_p (GET_MODE (src), GET_MODE (dest)) ? src : dest;
4241 rld_mode = GET_MODE (rld);
4242 sec_mode = targetm.secondary_memory_needed_mode (rld_mode);
4243 new_reg = lra_create_new_reg (sec_mode, NULL_RTX, NO_REGS, NULL,
4244 "secondary");
4245 /* If the mode is changed, it should be wider. */
4246 lra_assert (!partial_subreg_p (sec_mode, rld_mode));
4247 if (sec_mode != rld_mode)
4249 /* If the target says specifically to use another mode for
4250 secondary memory moves we cannot reuse the original
4251 insn. */
4252 after = emit_spill_move (false, new_reg, dest);
4253 lra_process_new_insns (curr_insn, NULL, after,
4254 "Inserting the sec. move");
4255 /* We may have non null BEFORE here (e.g. after address
4256 processing. */
4257 push_to_sequence (before);
4258 before = emit_spill_move (true, new_reg, src);
4259 emit_insn (before);
4260 before = get_insns ();
4261 end_sequence ();
4262 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
4263 lra_set_insn_deleted (curr_insn);
4265 else if (dest == rld)
4267 *curr_id->operand_loc[out] = new_reg;
4268 lra_update_dup (curr_id, out);
4269 after = emit_spill_move (false, new_reg, dest);
4270 lra_process_new_insns (curr_insn, NULL, after,
4271 "Inserting the sec. move");
4273 else
4275 *curr_id->operand_loc[in] = new_reg;
4276 lra_update_dup (curr_id, in);
4277 /* See comments above. */
4278 push_to_sequence (before);
4279 before = emit_spill_move (true, new_reg, src);
4280 emit_insn (before);
4281 before = get_insns ();
4282 end_sequence ();
4283 lra_process_new_insns (curr_insn, before, NULL,
4284 "Inserting the sec. move");
4286 lra_update_insn_regno_info (curr_insn);
4287 return true;
4290 lra_assert (goal_alt_number >= 0);
4291 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
4293 if (lra_dump_file != NULL)
4295 const char *p;
4297 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
4298 goal_alt_number, INSN_UID (curr_insn));
4299 for (i = 0; i < n_operands; i++)
4301 p = (curr_static_id->operand_alternative
4302 [goal_alt_number * n_operands + i].constraint);
4303 if (*p == '\0')
4304 continue;
4305 fprintf (lra_dump_file, " (%d) ", i);
4306 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
4307 fputc (*p, lra_dump_file);
4309 if (INSN_CODE (curr_insn) >= 0
4310 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
4311 fprintf (lra_dump_file, " {%s}", p);
4312 if (maybe_ne (curr_id->sp_offset, 0))
4314 fprintf (lra_dump_file, " (sp_off=");
4315 print_dec (curr_id->sp_offset, lra_dump_file);
4316 fprintf (lra_dump_file, ")");
4318 fprintf (lra_dump_file, "\n");
4321 /* Right now, for any pair of operands I and J that are required to
4322 match, with J < I, goal_alt_matches[I] is J. Add I to
4323 goal_alt_matched[J]. */
4325 for (i = 0; i < n_operands; i++)
4326 if ((j = goal_alt_matches[i]) >= 0)
4328 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
4330 /* We allow matching one output operand and several input
4331 operands. */
4332 lra_assert (k == 0
4333 || (curr_static_id->operand[j].type == OP_OUT
4334 && curr_static_id->operand[i].type == OP_IN
4335 && (curr_static_id->operand
4336 [goal_alt_matched[j][0]].type == OP_IN)));
4337 goal_alt_matched[j][k] = i;
4338 goal_alt_matched[j][k + 1] = -1;
4341 for (i = 0; i < n_operands; i++)
4342 goal_alt_win[i] |= goal_alt_match_win[i];
4344 /* Any constants that aren't allowed and can't be reloaded into
4345 registers are here changed into memory references. */
4346 for (i = 0; i < n_operands; i++)
4347 if (goal_alt_win[i])
4349 int regno;
4350 enum reg_class new_class;
4351 rtx reg = *curr_id->operand_loc[i];
4353 if (GET_CODE (reg) == SUBREG)
4354 reg = SUBREG_REG (reg);
4356 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
4358 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
4360 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
4362 lra_assert (ok_p);
4363 lra_change_class (regno, new_class, " Change to", true);
4367 else
4369 const char *constraint;
4370 char c;
4371 rtx op = *curr_id->operand_loc[i];
4372 rtx subreg = NULL_RTX;
4373 machine_mode mode = curr_operand_mode[i];
4375 if (GET_CODE (op) == SUBREG)
4377 subreg = op;
4378 op = SUBREG_REG (op);
4379 mode = GET_MODE (op);
4382 if (CONST_POOL_OK_P (mode, op)
4383 && ((targetm.preferred_reload_class
4384 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
4385 || no_input_reloads_p))
4387 rtx tem = force_const_mem (mode, op);
4389 change_p = true;
4390 if (subreg != NULL_RTX)
4391 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
4393 *curr_id->operand_loc[i] = tem;
4394 lra_update_dup (curr_id, i);
4395 process_address (i, false, &before, &after);
4397 /* If the alternative accepts constant pool refs directly
4398 there will be no reload needed at all. */
4399 if (subreg != NULL_RTX)
4400 continue;
4401 /* Skip alternatives before the one requested. */
4402 constraint = (curr_static_id->operand_alternative
4403 [goal_alt_number * n_operands + i].constraint);
4404 for (;
4405 (c = *constraint) && c != ',' && c != '#';
4406 constraint += CONSTRAINT_LEN (c, constraint))
4408 enum constraint_num cn = lookup_constraint (constraint);
4409 if ((insn_extra_memory_constraint (cn)
4410 || insn_extra_special_memory_constraint (cn)
4411 || insn_extra_relaxed_memory_constraint (cn))
4412 && satisfies_memory_constraint_p (tem, cn))
4413 break;
4415 if (c == '\0' || c == ',' || c == '#')
4416 continue;
4418 goal_alt_win[i] = true;
4422 n_outputs = 0;
4423 for (i = 0; i < n_operands; i++)
4424 if (curr_static_id->operand[i].type == OP_OUT)
4425 outputs[n_outputs++] = i;
4426 outputs[n_outputs] = -1;
4427 for (i = 0; i < n_operands; i++)
4429 int regno;
4430 bool optional_p = false;
4431 rtx old, new_reg;
4432 rtx op = *curr_id->operand_loc[i];
4434 if (goal_alt_win[i])
4436 if (goal_alt[i] == NO_REGS
4437 && REG_P (op)
4438 /* When we assign NO_REGS it means that we will not
4439 assign a hard register to the scratch pseudo by
4440 assigment pass and the scratch pseudo will be
4441 spilled. Spilled scratch pseudos are transformed
4442 back to scratches at the LRA end. */
4443 && ira_former_scratch_operand_p (curr_insn, i)
4444 && ira_former_scratch_p (REGNO (op)))
4446 int regno = REGNO (op);
4447 lra_change_class (regno, NO_REGS, " Change to", true);
4448 if (lra_get_regno_hard_regno (regno) >= 0)
4449 /* We don't have to mark all insn affected by the
4450 spilled pseudo as there is only one such insn, the
4451 current one. */
4452 reg_renumber[regno] = -1;
4453 lra_assert (bitmap_single_bit_set_p
4454 (&lra_reg_info[REGNO (op)].insn_bitmap));
4456 /* We can do an optional reload. If the pseudo got a hard
4457 reg, we might improve the code through inheritance. If
4458 it does not get a hard register we coalesce memory/memory
4459 moves later. Ignore move insns to avoid cycling. */
4460 if (! lra_simple_p
4461 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
4462 && goal_alt[i] != NO_REGS && REG_P (op)
4463 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
4464 && regno < new_regno_start
4465 && ! ira_former_scratch_p (regno)
4466 && reg_renumber[regno] < 0
4467 /* Check that the optional reload pseudo will be able to
4468 hold given mode value. */
4469 && ! (prohibited_class_reg_set_mode_p
4470 (goal_alt[i], reg_class_contents[goal_alt[i]],
4471 PSEUDO_REGNO_MODE (regno)))
4472 && (curr_insn_set == NULL_RTX
4473 || !((REG_P (SET_SRC (curr_insn_set))
4474 || MEM_P (SET_SRC (curr_insn_set))
4475 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
4476 && (REG_P (SET_DEST (curr_insn_set))
4477 || MEM_P (SET_DEST (curr_insn_set))
4478 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
4479 optional_p = true;
4480 else if (goal_alt_matched[i][0] != -1
4481 && curr_static_id->operand[i].type == OP_OUT
4482 && (curr_static_id->operand_alternative
4483 [goal_alt_number * n_operands + i].earlyclobber)
4484 && REG_P (op))
4486 for (j = 0; goal_alt_matched[i][j] != -1; j++)
4488 rtx op2 = *curr_id->operand_loc[goal_alt_matched[i][j]];
4490 if (REG_P (op2) && REGNO (op) != REGNO (op2))
4491 break;
4493 if (goal_alt_matched[i][j] != -1)
4495 /* Generate reloads for different output and matched
4496 input registers. This is the easiest way to avoid
4497 creation of non-existing register conflicts in
4498 lra-lives.cc. */
4499 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i],
4500 &goal_alt_exclude_start_hard_regs[i], &before,
4501 &after, TRUE);
4503 continue;
4505 else
4506 continue;
4509 /* Operands that match previous ones have already been handled. */
4510 if (goal_alt_matches[i] >= 0)
4511 continue;
4513 /* We should not have an operand with a non-offsettable address
4514 appearing where an offsettable address will do. It also may
4515 be a case when the address should be special in other words
4516 not a general one (e.g. it needs no index reg). */
4517 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
4519 enum reg_class rclass;
4520 rtx *loc = &XEXP (op, 0);
4521 enum rtx_code code = GET_CODE (*loc);
4523 push_to_sequence (before);
4524 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
4525 MEM, SCRATCH);
4526 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
4527 new_reg = emit_inc (rclass, *loc, *loc,
4528 /* This value does not matter for MODIFY. */
4529 GET_MODE_SIZE (GET_MODE (op)));
4530 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass,
4531 NULL, FALSE,
4532 "offsetable address", &new_reg))
4534 rtx addr = *loc;
4535 enum rtx_code code = GET_CODE (addr);
4536 bool align_p = false;
4538 if (code == AND && CONST_INT_P (XEXP (addr, 1)))
4540 /* (and ... (const_int -X)) is used to align to X bytes. */
4541 align_p = true;
4542 addr = XEXP (*loc, 0);
4544 else
4545 addr = canonicalize_reload_addr (addr);
4547 lra_emit_move (new_reg, addr);
4548 if (align_p)
4549 emit_move_insn (new_reg, gen_rtx_AND (GET_MODE (new_reg), new_reg, XEXP (*loc, 1)));
4551 before = get_insns ();
4552 end_sequence ();
4553 *loc = new_reg;
4554 lra_update_dup (curr_id, i);
4556 else if (goal_alt_matched[i][0] == -1)
4558 machine_mode mode;
4559 rtx reg, *loc;
4560 int hard_regno;
4561 enum op_type type = curr_static_id->operand[i].type;
4563 loc = curr_id->operand_loc[i];
4564 mode = curr_operand_mode[i];
4565 if (GET_CODE (*loc) == SUBREG)
4567 reg = SUBREG_REG (*loc);
4568 poly_int64 byte = SUBREG_BYTE (*loc);
4569 if (REG_P (reg)
4570 /* Strict_low_part requires reloading the register and not
4571 just the subreg. Likewise for a strict subreg no wider
4572 than a word for WORD_REGISTER_OPERATIONS targets. */
4573 && (curr_static_id->operand[i].strict_low
4574 || (!paradoxical_subreg_p (mode, GET_MODE (reg))
4575 && (hard_regno
4576 = get_try_hard_regno (REGNO (reg))) >= 0
4577 && (simplify_subreg_regno
4578 (hard_regno,
4579 GET_MODE (reg), byte, mode) < 0)
4580 && (goal_alt[i] == NO_REGS
4581 || (simplify_subreg_regno
4582 (ira_class_hard_regs[goal_alt[i]][0],
4583 GET_MODE (reg), byte, mode) >= 0)))
4584 || (partial_subreg_p (mode, GET_MODE (reg))
4585 && known_le (GET_MODE_SIZE (GET_MODE (reg)),
4586 UNITS_PER_WORD)
4587 && WORD_REGISTER_OPERATIONS))
4588 /* Avoid the situation when there are no available hard regs
4589 for the pseudo mode but there are ones for the subreg
4590 mode: */
4591 && !(goal_alt[i] != NO_REGS
4592 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
4593 && (prohibited_class_reg_set_mode_p
4594 (goal_alt[i], reg_class_contents[goal_alt[i]],
4595 GET_MODE (reg)))
4596 && !(prohibited_class_reg_set_mode_p
4597 (goal_alt[i], reg_class_contents[goal_alt[i]],
4598 mode))))
4600 /* An OP_INOUT is required when reloading a subreg of a
4601 mode wider than a word to ensure that data beyond the
4602 word being reloaded is preserved. Also automatically
4603 ensure that strict_low_part reloads are made into
4604 OP_INOUT which should already be true from the backend
4605 constraints. */
4606 if (type == OP_OUT
4607 && (curr_static_id->operand[i].strict_low
4608 || read_modify_subreg_p (*loc)))
4609 type = OP_INOUT;
4610 loc = &SUBREG_REG (*loc);
4611 mode = GET_MODE (*loc);
4614 old = *loc;
4615 if (get_reload_reg (type, mode, old, goal_alt[i],
4616 &goal_alt_exclude_start_hard_regs[i],
4617 loc != curr_id->operand_loc[i], "", &new_reg)
4618 && type != OP_OUT)
4620 push_to_sequence (before);
4621 lra_emit_move (new_reg, old);
4622 before = get_insns ();
4623 end_sequence ();
4625 *loc = new_reg;
4626 if (type != OP_IN
4627 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
4629 start_sequence ();
4630 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
4631 emit_insn (after);
4632 after = get_insns ();
4633 end_sequence ();
4634 *loc = new_reg;
4636 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
4637 if (goal_alt_dont_inherit_ops[j] == i)
4639 lra_set_regno_unique_value (REGNO (new_reg));
4640 break;
4642 lra_update_dup (curr_id, i);
4644 else if (curr_static_id->operand[i].type == OP_IN
4645 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4646 == OP_OUT
4647 || (curr_static_id->operand[goal_alt_matched[i][0]].type
4648 == OP_INOUT
4649 && (operands_match_p
4650 (*curr_id->operand_loc[i],
4651 *curr_id->operand_loc[goal_alt_matched[i][0]],
4652 -1)))))
4654 /* generate reloads for input and matched outputs. */
4655 match_inputs[0] = i;
4656 match_inputs[1] = -1;
4657 match_reload (goal_alt_matched[i][0], match_inputs, outputs,
4658 goal_alt[i], &goal_alt_exclude_start_hard_regs[i],
4659 &before, &after,
4660 curr_static_id->operand_alternative
4661 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
4662 .earlyclobber);
4664 else if ((curr_static_id->operand[i].type == OP_OUT
4665 || (curr_static_id->operand[i].type == OP_INOUT
4666 && (operands_match_p
4667 (*curr_id->operand_loc[i],
4668 *curr_id->operand_loc[goal_alt_matched[i][0]],
4669 -1))))
4670 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4671 == OP_IN))
4672 /* Generate reloads for output and matched inputs. */
4673 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i],
4674 &goal_alt_exclude_start_hard_regs[i], &before, &after,
4675 curr_static_id->operand_alternative
4676 [goal_alt_number * n_operands + i].earlyclobber);
4677 else if (curr_static_id->operand[i].type == OP_IN
4678 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4679 == OP_IN))
4681 /* Generate reloads for matched inputs. */
4682 match_inputs[0] = i;
4683 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
4684 match_inputs[j + 1] = k;
4685 match_inputs[j + 1] = -1;
4686 match_reload (-1, match_inputs, outputs, goal_alt[i],
4687 &goal_alt_exclude_start_hard_regs[i],
4688 &before, &after, false);
4690 else
4691 /* We must generate code in any case when function
4692 process_alt_operands decides that it is possible. */
4693 gcc_unreachable ();
4695 if (optional_p)
4697 rtx reg = op;
4699 lra_assert (REG_P (reg));
4700 regno = REGNO (reg);
4701 op = *curr_id->operand_loc[i]; /* Substitution. */
4702 if (GET_CODE (op) == SUBREG)
4703 op = SUBREG_REG (op);
4704 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
4705 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
4706 lra_reg_info[REGNO (op)].restore_rtx = reg;
4707 if (lra_dump_file != NULL)
4708 fprintf (lra_dump_file,
4709 " Making reload reg %d for reg %d optional\n",
4710 REGNO (op), regno);
4713 if (before != NULL_RTX || after != NULL_RTX
4714 || max_regno_before != max_reg_num ())
4715 change_p = true;
4716 if (change_p)
4718 lra_update_operator_dups (curr_id);
4719 /* Something changes -- process the insn. */
4720 lra_update_insn_regno_info (curr_insn);
4722 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
4723 return change_p;
4726 /* Return true if INSN satisfies all constraints. In other words, no
4727 reload insns are needed. */
4728 bool
4729 lra_constrain_insn (rtx_insn *insn)
4731 int saved_new_regno_start = new_regno_start;
4732 int saved_new_insn_uid_start = new_insn_uid_start;
4733 bool change_p;
4735 curr_insn = insn;
4736 curr_id = lra_get_insn_recog_data (curr_insn);
4737 curr_static_id = curr_id->insn_static_data;
4738 new_insn_uid_start = get_max_uid ();
4739 new_regno_start = max_reg_num ();
4740 change_p = curr_insn_transform (true);
4741 new_regno_start = saved_new_regno_start;
4742 new_insn_uid_start = saved_new_insn_uid_start;
4743 return ! change_p;
4746 /* Return true if X is in LIST. */
4747 static bool
4748 in_list_p (rtx x, rtx list)
4750 for (; list != NULL_RTX; list = XEXP (list, 1))
4751 if (XEXP (list, 0) == x)
4752 return true;
4753 return false;
4756 /* Return true if X contains an allocatable hard register (if
4757 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
4758 static bool
4759 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
4761 int i, j;
4762 const char *fmt;
4763 enum rtx_code code;
4765 code = GET_CODE (x);
4766 if (REG_P (x))
4768 int regno = REGNO (x);
4769 HARD_REG_SET alloc_regs;
4771 if (hard_reg_p)
4773 if (regno >= FIRST_PSEUDO_REGISTER)
4774 regno = lra_get_regno_hard_regno (regno);
4775 if (regno < 0)
4776 return false;
4777 alloc_regs = ~lra_no_alloc_regs;
4778 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
4780 else
4782 if (regno < FIRST_PSEUDO_REGISTER)
4783 return false;
4784 if (! spilled_p)
4785 return true;
4786 return lra_get_regno_hard_regno (regno) < 0;
4789 fmt = GET_RTX_FORMAT (code);
4790 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4792 if (fmt[i] == 'e')
4794 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4795 return true;
4797 else if (fmt[i] == 'E')
4799 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4800 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4801 return true;
4804 return false;
4807 /* Process all regs in location *LOC and change them on equivalent
4808 substitution. Return true if any change was done. */
4809 static bool
4810 loc_equivalence_change_p (rtx *loc)
4812 rtx subst, reg, x = *loc;
4813 bool result = false;
4814 enum rtx_code code = GET_CODE (x);
4815 const char *fmt;
4816 int i, j;
4818 if (code == SUBREG)
4820 reg = SUBREG_REG (x);
4821 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4822 && GET_MODE (subst) == VOIDmode)
4824 /* We cannot reload debug location. Simplify subreg here
4825 while we know the inner mode. */
4826 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4827 GET_MODE (reg), SUBREG_BYTE (x));
4828 return true;
4831 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4833 *loc = subst;
4834 return true;
4837 /* Scan all the operand sub-expressions. */
4838 fmt = GET_RTX_FORMAT (code);
4839 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4841 if (fmt[i] == 'e')
4842 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4843 else if (fmt[i] == 'E')
4844 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4845 result
4846 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4848 return result;
4851 /* Similar to loc_equivalence_change_p, but for use as
4852 simplify_replace_fn_rtx callback. DATA is insn for which the
4853 elimination is done. If it null we don't do the elimination. */
4854 static rtx
4855 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4857 if (!REG_P (loc))
4858 return NULL_RTX;
4860 rtx subst = (data == NULL
4861 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4862 if (subst != loc)
4863 return subst;
4865 return NULL_RTX;
4868 /* Maximum number of generated reload insns per an insn. It is for
4869 preventing this pass cycling in a bug case. */
4870 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4872 /* The current iteration number of this LRA pass. */
4873 int lra_constraint_iter;
4875 /* True if we should during assignment sub-pass check assignment
4876 correctness for all pseudos and spill some of them to correct
4877 conflicts. It can be necessary when we substitute equiv which
4878 needs checking register allocation correctness because the
4879 equivalent value contains allocatable hard registers, or when we
4880 restore multi-register pseudo, or when we change the insn code and
4881 its operand became INOUT operand when it was IN one before. */
4882 bool check_and_force_assignment_correctness_p;
4884 /* Return true if REGNO is referenced in more than one block. */
4885 static bool
4886 multi_block_pseudo_p (int regno)
4888 basic_block bb = NULL;
4889 unsigned int uid;
4890 bitmap_iterator bi;
4892 if (regno < FIRST_PSEUDO_REGISTER)
4893 return false;
4895 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4896 if (bb == NULL)
4897 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4898 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4899 return true;
4900 return false;
4903 /* Return true if LIST contains a deleted insn. */
4904 static bool
4905 contains_deleted_insn_p (rtx_insn_list *list)
4907 for (; list != NULL_RTX; list = list->next ())
4908 if (NOTE_P (list->insn ())
4909 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4910 return true;
4911 return false;
4914 /* Return true if X contains a pseudo dying in INSN. */
4915 static bool
4916 dead_pseudo_p (rtx x, rtx_insn *insn)
4918 int i, j;
4919 const char *fmt;
4920 enum rtx_code code;
4922 if (REG_P (x))
4923 return (insn != NULL_RTX
4924 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4925 code = GET_CODE (x);
4926 fmt = GET_RTX_FORMAT (code);
4927 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4929 if (fmt[i] == 'e')
4931 if (dead_pseudo_p (XEXP (x, i), insn))
4932 return true;
4934 else if (fmt[i] == 'E')
4936 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4937 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4938 return true;
4941 return false;
4944 /* Return true if INSN contains a dying pseudo in INSN right hand
4945 side. */
4946 static bool
4947 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4949 rtx set = single_set (insn);
4951 gcc_assert (set != NULL);
4952 return dead_pseudo_p (SET_SRC (set), insn);
4955 /* Return true if any init insn of REGNO contains a dying pseudo in
4956 insn right hand side. */
4957 static bool
4958 init_insn_rhs_dead_pseudo_p (int regno)
4960 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4962 if (insns == NULL)
4963 return false;
4964 for (; insns != NULL_RTX; insns = insns->next ())
4965 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4966 return true;
4967 return false;
4970 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4971 reverse only if we have one init insn with given REGNO as a
4972 source. */
4973 static bool
4974 reverse_equiv_p (int regno)
4976 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4977 rtx set;
4979 if (insns == NULL)
4980 return false;
4981 if (! INSN_P (insns->insn ())
4982 || insns->next () != NULL)
4983 return false;
4984 if ((set = single_set (insns->insn ())) == NULL_RTX)
4985 return false;
4986 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4989 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4990 call this function only for non-reverse equivalence. */
4991 static bool
4992 contains_reloaded_insn_p (int regno)
4994 rtx set;
4995 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4997 for (; list != NULL; list = list->next ())
4998 if ((set = single_set (list->insn ())) == NULL_RTX
4999 || ! REG_P (SET_DEST (set))
5000 || (int) REGNO (SET_DEST (set)) != regno)
5001 return true;
5002 return false;
5005 /* Try combine secondary memory reload insn FROM for insn TO into TO insn.
5006 FROM should be a load insn (usually a secondary memory reload insn). Return
5007 TRUE in case of success. */
5008 static bool
5009 combine_reload_insn (rtx_insn *from, rtx_insn *to)
5011 bool ok_p;
5012 rtx_insn *saved_insn;
5013 rtx set, from_reg, to_reg, op;
5014 enum reg_class to_class, from_class;
5015 int n, nop;
5016 signed char changed_nops[MAX_RECOG_OPERANDS + 1];
5018 /* Check conditions for second memory reload and original insn: */
5019 if ((targetm.secondary_memory_needed
5020 == hook_bool_mode_reg_class_t_reg_class_t_false)
5021 || NEXT_INSN (from) != to
5022 || !NONDEBUG_INSN_P (to)
5023 || CALL_P (to))
5024 return false;
5026 lra_insn_recog_data_t id = lra_get_insn_recog_data (to);
5027 struct lra_static_insn_data *static_id = id->insn_static_data;
5029 if (id->used_insn_alternative == LRA_UNKNOWN_ALT
5030 || (set = single_set (from)) == NULL_RTX)
5031 return false;
5032 from_reg = SET_DEST (set);
5033 to_reg = SET_SRC (set);
5034 /* Ignore optional reloads: */
5035 if (! REG_P (from_reg) || ! REG_P (to_reg)
5036 || bitmap_bit_p (&lra_optional_reload_pseudos, REGNO (from_reg)))
5037 return false;
5038 to_class = lra_get_allocno_class (REGNO (to_reg));
5039 from_class = lra_get_allocno_class (REGNO (from_reg));
5040 /* Check that reload insn is a load: */
5041 if (to_class != NO_REGS || from_class == NO_REGS)
5042 return false;
5043 for (n = nop = 0; nop < static_id->n_operands; nop++)
5045 if (static_id->operand[nop].type != OP_IN)
5046 continue;
5047 op = *id->operand_loc[nop];
5048 if (!REG_P (op) || REGNO (op) != REGNO (from_reg))
5049 continue;
5050 *id->operand_loc[nop] = to_reg;
5051 changed_nops[n++] = nop;
5053 changed_nops[n] = -1;
5054 lra_update_dups (id, changed_nops);
5055 lra_update_insn_regno_info (to);
5056 ok_p = recog_memoized (to) >= 0;
5057 if (ok_p)
5059 /* Check that combined insn does not need any reloads: */
5060 saved_insn = curr_insn;
5061 curr_insn = to;
5062 curr_id = lra_get_insn_recog_data (curr_insn);
5063 curr_static_id = curr_id->insn_static_data;
5064 for (bool swapped_p = false;;)
5066 ok_p = !curr_insn_transform (true);
5067 if (ok_p || curr_static_id->commutative < 0)
5068 break;
5069 swap_operands (curr_static_id->commutative);
5070 if (lra_dump_file != NULL)
5072 fprintf (lra_dump_file,
5073 " Swapping %scombined insn operands:\n",
5074 swapped_p ? "back " : "");
5075 dump_insn_slim (lra_dump_file, to);
5077 if (swapped_p)
5078 break;
5079 swapped_p = true;
5081 curr_insn = saved_insn;
5082 curr_id = lra_get_insn_recog_data (curr_insn);
5083 curr_static_id = curr_id->insn_static_data;
5085 if (ok_p)
5087 id->used_insn_alternative = -1;
5088 lra_push_insn_and_update_insn_regno_info (to);
5089 if (lra_dump_file != NULL)
5091 fprintf (lra_dump_file, " Use combined insn:\n");
5092 dump_insn_slim (lra_dump_file, to);
5094 return true;
5096 if (lra_dump_file != NULL)
5098 fprintf (lra_dump_file, " Failed combined insn:\n");
5099 dump_insn_slim (lra_dump_file, to);
5101 for (int i = 0; i < n; i++)
5103 nop = changed_nops[i];
5104 *id->operand_loc[nop] = from_reg;
5106 lra_update_dups (id, changed_nops);
5107 lra_update_insn_regno_info (to);
5108 if (lra_dump_file != NULL)
5110 fprintf (lra_dump_file, " Restoring insn after failed combining:\n");
5111 dump_insn_slim (lra_dump_file, to);
5113 return false;
5116 /* Entry function of LRA constraint pass. Return true if the
5117 constraint pass did change the code. */
5118 bool
5119 lra_constraints (bool first_p)
5121 bool changed_p;
5122 int i, hard_regno, new_insns_num;
5123 unsigned int min_len, new_min_len, uid;
5124 rtx set, x, reg, dest_reg;
5125 rtx_insn *original_insn;
5126 basic_block last_bb;
5127 bitmap_iterator bi;
5129 lra_constraint_iter++;
5130 if (lra_dump_file != NULL)
5131 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
5132 lra_constraint_iter);
5133 changed_p = false;
5134 if (pic_offset_table_rtx
5135 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
5136 check_and_force_assignment_correctness_p = true;
5137 else if (first_p)
5138 /* On the first iteration we should check IRA assignment
5139 correctness. In rare cases, the assignments can be wrong as
5140 early clobbers operands are ignored in IRA or usages of
5141 paradoxical sub-registers are not taken into account by
5142 IRA. */
5143 check_and_force_assignment_correctness_p = true;
5144 new_insn_uid_start = get_max_uid ();
5145 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
5146 /* Mark used hard regs for target stack size calulations. */
5147 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5148 if (lra_reg_info[i].nrefs != 0
5149 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
5151 int j, nregs;
5153 nregs = hard_regno_nregs (hard_regno, lra_reg_info[i].biggest_mode);
5154 for (j = 0; j < nregs; j++)
5155 df_set_regs_ever_live (hard_regno + j, true);
5157 /* Do elimination before the equivalence processing as we can spill
5158 some pseudos during elimination. */
5159 lra_eliminate (false, first_p);
5160 auto_bitmap equiv_insn_bitmap (&reg_obstack);
5161 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5162 if (lra_reg_info[i].nrefs != 0)
5164 ira_reg_equiv[i].profitable_p = true;
5165 reg = regno_reg_rtx[i];
5166 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
5168 bool pseudo_p = contains_reg_p (x, false, false);
5170 /* After RTL transformation, we cannot guarantee that
5171 pseudo in the substitution was not reloaded which might
5172 make equivalence invalid. For example, in reverse
5173 equiv of p0
5175 p0 <- ...
5177 equiv_mem <- p0
5179 the memory address register was reloaded before the 2nd
5180 insn. */
5181 if ((! first_p && pseudo_p)
5182 /* We don't use DF for compilation speed sake. So it
5183 is problematic to update live info when we use an
5184 equivalence containing pseudos in more than one
5185 BB. */
5186 || (pseudo_p && multi_block_pseudo_p (i))
5187 /* If an init insn was deleted for some reason, cancel
5188 the equiv. We could update the equiv insns after
5189 transformations including an equiv insn deletion
5190 but it is not worthy as such cases are extremely
5191 rare. */
5192 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
5193 /* If it is not a reverse equivalence, we check that a
5194 pseudo in rhs of the init insn is not dying in the
5195 insn. Otherwise, the live info at the beginning of
5196 the corresponding BB might be wrong after we
5197 removed the insn. When the equiv can be a
5198 constant, the right hand side of the init insn can
5199 be a pseudo. */
5200 || (! reverse_equiv_p (i)
5201 && (init_insn_rhs_dead_pseudo_p (i)
5202 /* If we reloaded the pseudo in an equivalence
5203 init insn, we cannot remove the equiv init
5204 insns and the init insns might write into
5205 const memory in this case. */
5206 || contains_reloaded_insn_p (i)))
5207 /* Prevent access beyond equivalent memory for
5208 paradoxical subregs. */
5209 || (MEM_P (x)
5210 && maybe_gt (GET_MODE_SIZE (lra_reg_info[i].biggest_mode),
5211 GET_MODE_SIZE (GET_MODE (x))))
5212 || (pic_offset_table_rtx
5213 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
5214 && (targetm.preferred_reload_class
5215 (x, lra_get_allocno_class (i)) == NO_REGS))
5216 || contains_symbol_ref_p (x))))
5217 ira_reg_equiv[i].defined_p
5218 = ira_reg_equiv[i].caller_save_p = false;
5219 if (contains_reg_p (x, false, true))
5220 ira_reg_equiv[i].profitable_p = false;
5221 if (get_equiv (reg) != reg)
5222 bitmap_ior_into (equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
5225 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5226 update_equiv (i);
5227 /* We should add all insns containing pseudos which should be
5228 substituted by their equivalences. */
5229 EXECUTE_IF_SET_IN_BITMAP (equiv_insn_bitmap, 0, uid, bi)
5230 lra_push_insn_by_uid (uid);
5231 min_len = lra_insn_stack_length ();
5232 new_insns_num = 0;
5233 last_bb = NULL;
5234 changed_p = false;
5235 original_insn = NULL;
5236 while ((new_min_len = lra_insn_stack_length ()) != 0)
5238 curr_insn = lra_pop_insn ();
5239 --new_min_len;
5240 curr_bb = BLOCK_FOR_INSN (curr_insn);
5241 if (curr_bb != last_bb)
5243 last_bb = curr_bb;
5244 bb_reload_num = lra_curr_reload_num;
5246 if (min_len > new_min_len)
5248 min_len = new_min_len;
5249 new_insns_num = 0;
5250 original_insn = curr_insn;
5252 else if (combine_reload_insn (curr_insn, original_insn))
5254 continue;
5256 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
5257 internal_error
5258 ("maximum number of generated reload insns per insn achieved (%d)",
5259 MAX_RELOAD_INSNS_NUMBER);
5260 new_insns_num++;
5261 if (DEBUG_INSN_P (curr_insn))
5263 /* We need to check equivalence in debug insn and change
5264 pseudo to the equivalent value if necessary. */
5265 curr_id = lra_get_insn_recog_data (curr_insn);
5266 if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn)))
5268 rtx old = *curr_id->operand_loc[0];
5269 *curr_id->operand_loc[0]
5270 = simplify_replace_fn_rtx (old, NULL_RTX,
5271 loc_equivalence_callback, curr_insn);
5272 if (old != *curr_id->operand_loc[0])
5274 lra_update_insn_regno_info (curr_insn);
5275 changed_p = true;
5279 else if (INSN_P (curr_insn))
5281 if ((set = single_set (curr_insn)) != NULL_RTX)
5283 dest_reg = SET_DEST (set);
5284 /* The equivalence pseudo could be set up as SUBREG in a
5285 case when it is a call restore insn in a mode
5286 different from the pseudo mode. */
5287 if (GET_CODE (dest_reg) == SUBREG)
5288 dest_reg = SUBREG_REG (dest_reg);
5289 if ((REG_P (dest_reg)
5290 && (x = get_equiv (dest_reg)) != dest_reg
5291 /* Remove insns which set up a pseudo whose value
5292 cannot be changed. Such insns might be not in
5293 init_insns because we don't update equiv data
5294 during insn transformations.
5296 As an example, let suppose that a pseudo got
5297 hard register and on the 1st pass was not
5298 changed to equivalent constant. We generate an
5299 additional insn setting up the pseudo because of
5300 secondary memory movement. Then the pseudo is
5301 spilled and we use the equiv constant. In this
5302 case we should remove the additional insn and
5303 this insn is not init_insns list. */
5304 && (! MEM_P (x) || MEM_READONLY_P (x)
5305 /* Check that this is actually an insn setting
5306 up the equivalence. */
5307 || in_list_p (curr_insn,
5308 ira_reg_equiv
5309 [REGNO (dest_reg)].init_insns)))
5310 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
5311 && in_list_p (curr_insn,
5312 ira_reg_equiv
5313 [REGNO (SET_SRC (set))].init_insns)))
5315 /* This is equiv init insn of pseudo which did not get a
5316 hard register -- remove the insn. */
5317 if (lra_dump_file != NULL)
5319 fprintf (lra_dump_file,
5320 " Removing equiv init insn %i (freq=%d)\n",
5321 INSN_UID (curr_insn),
5322 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
5323 dump_insn_slim (lra_dump_file, curr_insn);
5325 if (contains_reg_p (x, true, false))
5326 check_and_force_assignment_correctness_p = true;
5327 lra_set_insn_deleted (curr_insn);
5328 continue;
5331 curr_id = lra_get_insn_recog_data (curr_insn);
5332 curr_static_id = curr_id->insn_static_data;
5333 init_curr_insn_input_reloads ();
5334 init_curr_operand_mode ();
5335 if (curr_insn_transform (false))
5336 changed_p = true;
5337 /* Check non-transformed insns too for equiv change as USE
5338 or CLOBBER don't need reloads but can contain pseudos
5339 being changed on their equivalences. */
5340 else if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn))
5341 && loc_equivalence_change_p (&PATTERN (curr_insn)))
5343 lra_update_insn_regno_info (curr_insn);
5344 changed_p = true;
5349 /* If we used a new hard regno, changed_p should be true because the
5350 hard reg is assigned to a new pseudo. */
5351 if (flag_checking && !changed_p)
5353 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5354 if (lra_reg_info[i].nrefs != 0
5355 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
5357 int j, nregs = hard_regno_nregs (hard_regno,
5358 PSEUDO_REGNO_MODE (i));
5360 for (j = 0; j < nregs; j++)
5361 lra_assert (df_regs_ever_live_p (hard_regno + j));
5364 return changed_p;
5367 static void initiate_invariants (void);
5368 static void finish_invariants (void);
5370 /* Initiate the LRA constraint pass. It is done once per
5371 function. */
5372 void
5373 lra_constraints_init (void)
5375 initiate_invariants ();
5378 /* Finalize the LRA constraint pass. It is done once per
5379 function. */
5380 void
5381 lra_constraints_finish (void)
5383 finish_invariants ();
5388 /* Structure describes invariants for ineheritance. */
5389 struct lra_invariant
5391 /* The order number of the invariant. */
5392 int num;
5393 /* The invariant RTX. */
5394 rtx invariant_rtx;
5395 /* The origin insn of the invariant. */
5396 rtx_insn *insn;
5399 typedef lra_invariant invariant_t;
5400 typedef invariant_t *invariant_ptr_t;
5401 typedef const invariant_t *const_invariant_ptr_t;
5403 /* Pointer to the inheritance invariants. */
5404 static vec<invariant_ptr_t> invariants;
5406 /* Allocation pool for the invariants. */
5407 static object_allocator<lra_invariant> *invariants_pool;
5409 /* Hash table for the invariants. */
5410 static htab_t invariant_table;
5412 /* Hash function for INVARIANT. */
5413 static hashval_t
5414 invariant_hash (const void *invariant)
5416 rtx inv = ((const_invariant_ptr_t) invariant)->invariant_rtx;
5417 return lra_rtx_hash (inv);
5420 /* Equal function for invariants INVARIANT1 and INVARIANT2. */
5421 static int
5422 invariant_eq_p (const void *invariant1, const void *invariant2)
5424 rtx inv1 = ((const_invariant_ptr_t) invariant1)->invariant_rtx;
5425 rtx inv2 = ((const_invariant_ptr_t) invariant2)->invariant_rtx;
5427 return rtx_equal_p (inv1, inv2);
5430 /* Insert INVARIANT_RTX into the table if it is not there yet. Return
5431 invariant which is in the table. */
5432 static invariant_ptr_t
5433 insert_invariant (rtx invariant_rtx)
5435 void **entry_ptr;
5436 invariant_t invariant;
5437 invariant_ptr_t invariant_ptr;
5439 invariant.invariant_rtx = invariant_rtx;
5440 entry_ptr = htab_find_slot (invariant_table, &invariant, INSERT);
5441 if (*entry_ptr == NULL)
5443 invariant_ptr = invariants_pool->allocate ();
5444 invariant_ptr->invariant_rtx = invariant_rtx;
5445 invariant_ptr->insn = NULL;
5446 invariants.safe_push (invariant_ptr);
5447 *entry_ptr = (void *) invariant_ptr;
5449 return (invariant_ptr_t) *entry_ptr;
5452 /* Initiate the invariant table. */
5453 static void
5454 initiate_invariants (void)
5456 invariants.create (100);
5457 invariants_pool
5458 = new object_allocator<lra_invariant> ("Inheritance invariants");
5459 invariant_table = htab_create (100, invariant_hash, invariant_eq_p, NULL);
5462 /* Finish the invariant table. */
5463 static void
5464 finish_invariants (void)
5466 htab_delete (invariant_table);
5467 delete invariants_pool;
5468 invariants.release ();
5471 /* Make the invariant table empty. */
5472 static void
5473 clear_invariants (void)
5475 htab_empty (invariant_table);
5476 invariants_pool->release ();
5477 invariants.truncate (0);
5482 /* This page contains code to do inheritance/split
5483 transformations. */
5485 /* Number of reloads passed so far in current EBB. */
5486 static int reloads_num;
5488 /* Number of calls passed so far in current EBB. */
5489 static int calls_num;
5491 /* Index ID is the CALLS_NUM associated the last call we saw with
5492 ABI identifier ID. */
5493 static int last_call_for_abi[NUM_ABI_IDS];
5495 /* Which registers have been fully or partially clobbered by a call
5496 since they were last used. */
5497 static HARD_REG_SET full_and_partial_call_clobbers;
5499 /* Current reload pseudo check for validity of elements in
5500 USAGE_INSNS. */
5501 static int curr_usage_insns_check;
5503 /* Info about last usage of registers in EBB to do inheritance/split
5504 transformation. Inheritance transformation is done from a spilled
5505 pseudo and split transformations from a hard register or a pseudo
5506 assigned to a hard register. */
5507 struct usage_insns
5509 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
5510 value INSNS is valid. The insns is chain of optional debug insns
5511 and a finishing non-debug insn using the corresponding reg. The
5512 value is also used to mark the registers which are set up in the
5513 current insn. The negated insn uid is used for this. */
5514 int check;
5515 /* Value of global reloads_num at the last insn in INSNS. */
5516 int reloads_num;
5517 /* Value of global reloads_nums at the last insn in INSNS. */
5518 int calls_num;
5519 /* It can be true only for splitting. And it means that the restore
5520 insn should be put after insn given by the following member. */
5521 bool after_p;
5522 /* Next insns in the current EBB which use the original reg and the
5523 original reg value is not changed between the current insn and
5524 the next insns. In order words, e.g. for inheritance, if we need
5525 to use the original reg value again in the next insns we can try
5526 to use the value in a hard register from a reload insn of the
5527 current insn. */
5528 rtx insns;
5531 /* Map: regno -> corresponding pseudo usage insns. */
5532 static struct usage_insns *usage_insns;
5534 static void
5535 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
5537 usage_insns[regno].check = curr_usage_insns_check;
5538 usage_insns[regno].insns = insn;
5539 usage_insns[regno].reloads_num = reloads_num;
5540 usage_insns[regno].calls_num = calls_num;
5541 usage_insns[regno].after_p = after_p;
5542 if (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0)
5543 remove_from_hard_reg_set (&full_and_partial_call_clobbers,
5544 PSEUDO_REGNO_MODE (regno),
5545 reg_renumber[regno]);
5548 /* The function is used to form list REGNO usages which consists of
5549 optional debug insns finished by a non-debug insn using REGNO.
5550 RELOADS_NUM is current number of reload insns processed so far. */
5551 static void
5552 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
5554 rtx next_usage_insns;
5556 if (usage_insns[regno].check == curr_usage_insns_check
5557 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
5558 && DEBUG_INSN_P (insn))
5560 /* Check that we did not add the debug insn yet. */
5561 if (next_usage_insns != insn
5562 && (GET_CODE (next_usage_insns) != INSN_LIST
5563 || XEXP (next_usage_insns, 0) != insn))
5564 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
5565 next_usage_insns);
5567 else if (NONDEBUG_INSN_P (insn))
5568 setup_next_usage_insn (regno, insn, reloads_num, false);
5569 else
5570 usage_insns[regno].check = 0;
5573 /* Return first non-debug insn in list USAGE_INSNS. */
5574 static rtx_insn *
5575 skip_usage_debug_insns (rtx usage_insns)
5577 rtx insn;
5579 /* Skip debug insns. */
5580 for (insn = usage_insns;
5581 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
5582 insn = XEXP (insn, 1))
5584 return safe_as_a <rtx_insn *> (insn);
5587 /* Return true if we need secondary memory moves for insn in
5588 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
5589 into the insn. */
5590 static bool
5591 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
5592 rtx usage_insns ATTRIBUTE_UNUSED)
5594 rtx_insn *insn;
5595 rtx set, dest;
5596 enum reg_class cl;
5598 if (inher_cl == ALL_REGS
5599 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
5600 return false;
5601 lra_assert (INSN_P (insn));
5602 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
5603 return false;
5604 dest = SET_DEST (set);
5605 if (! REG_P (dest))
5606 return false;
5607 lra_assert (inher_cl != NO_REGS);
5608 cl = get_reg_class (REGNO (dest));
5609 return (cl != NO_REGS && cl != ALL_REGS
5610 && targetm.secondary_memory_needed (GET_MODE (dest), inher_cl, cl));
5613 /* Registers involved in inheritance/split in the current EBB
5614 (inheritance/split pseudos and original registers). */
5615 static bitmap_head check_only_regs;
5617 /* Reload pseudos cannot be involded in invariant inheritance in the
5618 current EBB. */
5619 static bitmap_head invalid_invariant_regs;
5621 /* Do inheritance transformations for insn INSN, which defines (if
5622 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
5623 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
5624 form as the "insns" field of usage_insns. Return true if we
5625 succeed in such transformation.
5627 The transformations look like:
5629 p <- ... i <- ...
5630 ... p <- i (new insn)
5631 ... =>
5632 <- ... p ... <- ... i ...
5634 ... i <- p (new insn)
5635 <- ... p ... <- ... i ...
5636 ... =>
5637 <- ... p ... <- ... i ...
5638 where p is a spilled original pseudo and i is a new inheritance pseudo.
5641 The inheritance pseudo has the smallest class of two classes CL and
5642 class of ORIGINAL REGNO. */
5643 static bool
5644 inherit_reload_reg (bool def_p, int original_regno,
5645 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
5647 if (optimize_function_for_size_p (cfun))
5648 return false;
5650 enum reg_class rclass = lra_get_allocno_class (original_regno);
5651 rtx original_reg = regno_reg_rtx[original_regno];
5652 rtx new_reg, usage_insn;
5653 rtx_insn *new_insns;
5655 lra_assert (! usage_insns[original_regno].after_p);
5656 if (lra_dump_file != NULL)
5657 fprintf (lra_dump_file,
5658 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
5659 if (! ira_reg_classes_intersect_p[cl][rclass])
5661 if (lra_dump_file != NULL)
5663 fprintf (lra_dump_file,
5664 " Rejecting inheritance for %d "
5665 "because of disjoint classes %s and %s\n",
5666 original_regno, reg_class_names[cl],
5667 reg_class_names[rclass]);
5668 fprintf (lra_dump_file,
5669 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5671 return false;
5673 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
5674 /* We don't use a subset of two classes because it can be
5675 NO_REGS. This transformation is still profitable in most
5676 cases even if the classes are not intersected as register
5677 move is probably cheaper than a memory load. */
5678 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
5680 if (lra_dump_file != NULL)
5681 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
5682 reg_class_names[cl], reg_class_names[rclass]);
5684 rclass = cl;
5686 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
5688 /* Reject inheritance resulting in secondary memory moves.
5689 Otherwise, there is a danger in LRA cycling. Also such
5690 transformation will be unprofitable. */
5691 if (lra_dump_file != NULL)
5693 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
5694 rtx set = single_set (insn);
5696 lra_assert (set != NULL_RTX);
5698 rtx dest = SET_DEST (set);
5700 lra_assert (REG_P (dest));
5701 fprintf (lra_dump_file,
5702 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
5703 "as secondary mem is needed\n",
5704 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
5705 original_regno, reg_class_names[rclass]);
5706 fprintf (lra_dump_file,
5707 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5709 return false;
5711 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
5712 rclass, NULL, "inheritance");
5713 start_sequence ();
5714 if (def_p)
5715 lra_emit_move (original_reg, new_reg);
5716 else
5717 lra_emit_move (new_reg, original_reg);
5718 new_insns = get_insns ();
5719 end_sequence ();
5720 if (NEXT_INSN (new_insns) != NULL_RTX)
5722 if (lra_dump_file != NULL)
5724 fprintf (lra_dump_file,
5725 " Rejecting inheritance %d->%d "
5726 "as it results in 2 or more insns:\n",
5727 original_regno, REGNO (new_reg));
5728 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
5729 fprintf (lra_dump_file,
5730 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5732 return false;
5734 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
5735 lra_update_insn_regno_info (insn);
5736 if (! def_p)
5737 /* We now have a new usage insn for original regno. */
5738 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
5739 if (lra_dump_file != NULL)
5740 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
5741 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
5742 lra_reg_info[REGNO (new_reg)].restore_rtx = regno_reg_rtx[original_regno];
5743 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5744 bitmap_set_bit (&check_only_regs, original_regno);
5745 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
5746 if (def_p)
5747 lra_process_new_insns (insn, NULL, new_insns,
5748 "Add original<-inheritance");
5749 else
5750 lra_process_new_insns (insn, new_insns, NULL,
5751 "Add inheritance<-original");
5752 while (next_usage_insns != NULL_RTX)
5754 if (GET_CODE (next_usage_insns) != INSN_LIST)
5756 usage_insn = next_usage_insns;
5757 lra_assert (NONDEBUG_INSN_P (usage_insn));
5758 next_usage_insns = NULL;
5760 else
5762 usage_insn = XEXP (next_usage_insns, 0);
5763 lra_assert (DEBUG_INSN_P (usage_insn));
5764 next_usage_insns = XEXP (next_usage_insns, 1);
5766 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
5767 DEBUG_INSN_P (usage_insn));
5768 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5769 if (lra_dump_file != NULL)
5771 basic_block bb = BLOCK_FOR_INSN (usage_insn);
5772 fprintf (lra_dump_file,
5773 " Inheritance reuse change %d->%d (bb%d):\n",
5774 original_regno, REGNO (new_reg),
5775 bb ? bb->index : -1);
5776 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5779 if (lra_dump_file != NULL)
5780 fprintf (lra_dump_file,
5781 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5782 return true;
5785 /* Return true if we need a caller save/restore for pseudo REGNO which
5786 was assigned to a hard register. */
5787 static inline bool
5788 need_for_call_save_p (int regno)
5790 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
5791 if (usage_insns[regno].calls_num < calls_num)
5793 unsigned int abis = 0;
5794 for (unsigned int i = 0; i < NUM_ABI_IDS; ++i)
5795 if (last_call_for_abi[i] > usage_insns[regno].calls_num)
5796 abis |= 1 << i;
5797 gcc_assert (abis);
5798 if (call_clobbered_in_region_p (abis, full_and_partial_call_clobbers,
5799 PSEUDO_REGNO_MODE (regno),
5800 reg_renumber[regno]))
5801 return true;
5803 return false;
5806 /* Global registers occurring in the current EBB. */
5807 static bitmap_head ebb_global_regs;
5809 /* Return true if we need a split for hard register REGNO or pseudo
5810 REGNO which was assigned to a hard register.
5811 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
5812 used for reloads since the EBB end. It is an approximation of the
5813 used hard registers in the split range. The exact value would
5814 require expensive calculations. If we were aggressive with
5815 splitting because of the approximation, the split pseudo will save
5816 the same hard register assignment and will be removed in the undo
5817 pass. We still need the approximation because too aggressive
5818 splitting would result in too inaccurate cost calculation in the
5819 assignment pass because of too many generated moves which will be
5820 probably removed in the undo pass. */
5821 static inline bool
5822 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
5824 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
5826 lra_assert (hard_regno >= 0);
5827 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
5828 /* Don't split eliminable hard registers, otherwise we can
5829 split hard registers like hard frame pointer, which
5830 lives on BB start/end according to DF-infrastructure,
5831 when there is a pseudo assigned to the register and
5832 living in the same BB. */
5833 && (regno >= FIRST_PSEUDO_REGISTER
5834 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
5835 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
5836 /* Don't split call clobbered hard regs living through
5837 calls, otherwise we might have a check problem in the
5838 assign sub-pass as in the most cases (exception is a
5839 situation when check_and_force_assignment_correctness_p value is
5840 true) the assign pass assumes that all pseudos living
5841 through calls are assigned to call saved hard regs. */
5842 && (regno >= FIRST_PSEUDO_REGISTER
5843 || !TEST_HARD_REG_BIT (full_and_partial_call_clobbers, regno))
5844 /* We need at least 2 reloads to make pseudo splitting
5845 profitable. We should provide hard regno splitting in
5846 any case to solve 1st insn scheduling problem when
5847 moving hard register definition up might result in
5848 impossibility to find hard register for reload pseudo of
5849 small register class. */
5850 && (usage_insns[regno].reloads_num
5851 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
5852 && (regno < FIRST_PSEUDO_REGISTER
5853 /* For short living pseudos, spilling + inheritance can
5854 be considered a substitution for splitting.
5855 Therefore we do not splitting for local pseudos. It
5856 decreases also aggressiveness of splitting. The
5857 minimal number of references is chosen taking into
5858 account that for 2 references splitting has no sense
5859 as we can just spill the pseudo. */
5860 || (regno >= FIRST_PSEUDO_REGISTER
5861 && lra_reg_info[regno].nrefs > 3
5862 && bitmap_bit_p (&ebb_global_regs, regno))))
5863 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
5866 /* Return class for the split pseudo created from original pseudo with
5867 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
5868 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
5869 results in no secondary memory movements. */
5870 static enum reg_class
5871 choose_split_class (enum reg_class allocno_class,
5872 int hard_regno ATTRIBUTE_UNUSED,
5873 machine_mode mode ATTRIBUTE_UNUSED)
5875 int i;
5876 enum reg_class cl, best_cl = NO_REGS;
5877 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
5878 = REGNO_REG_CLASS (hard_regno);
5880 if (! targetm.secondary_memory_needed (mode, allocno_class, allocno_class)
5881 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
5882 return allocno_class;
5883 for (i = 0;
5884 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
5885 i++)
5886 if (! targetm.secondary_memory_needed (mode, cl, hard_reg_class)
5887 && ! targetm.secondary_memory_needed (mode, hard_reg_class, cl)
5888 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
5889 && (best_cl == NO_REGS
5890 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
5891 best_cl = cl;
5892 return best_cl;
5895 /* Copy any equivalence information from ORIGINAL_REGNO to NEW_REGNO. It only
5896 makes sense to call this function if NEW_REGNO is always equal to
5897 ORIGINAL_REGNO. Set up defined_p flag when caller_save_p flag is set up and
5898 CALL_SAVE_P is true. */
5900 static void
5901 lra_copy_reg_equiv (unsigned int new_regno, unsigned int original_regno,
5902 bool call_save_p)
5904 if (!ira_reg_equiv[original_regno].defined_p
5905 && !(call_save_p && ira_reg_equiv[original_regno].caller_save_p))
5906 return;
5908 ira_expand_reg_equiv ();
5909 ira_reg_equiv[new_regno].defined_p = true;
5910 if (ira_reg_equiv[original_regno].memory)
5911 ira_reg_equiv[new_regno].memory
5912 = copy_rtx (ira_reg_equiv[original_regno].memory);
5913 if (ira_reg_equiv[original_regno].constant)
5914 ira_reg_equiv[new_regno].constant
5915 = copy_rtx (ira_reg_equiv[original_regno].constant);
5916 if (ira_reg_equiv[original_regno].invariant)
5917 ira_reg_equiv[new_regno].invariant
5918 = copy_rtx (ira_reg_equiv[original_regno].invariant);
5921 /* Do split transformations for insn INSN, which defines or uses
5922 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
5923 the EBB next uses ORIGINAL_REGNO; it has the same form as the
5924 "insns" field of usage_insns. If TO is not NULL, we don't use
5925 usage_insns, we put restore insns after TO insn. It is a case when
5926 we call it from lra_split_hard_reg_for, outside the inheritance
5927 pass.
5929 The transformations look like:
5931 p <- ... p <- ...
5932 ... s <- p (new insn -- save)
5933 ... =>
5934 ... p <- s (new insn -- restore)
5935 <- ... p ... <- ... p ...
5937 <- ... p ... <- ... p ...
5938 ... s <- p (new insn -- save)
5939 ... =>
5940 ... p <- s (new insn -- restore)
5941 <- ... p ... <- ... p ...
5943 where p is an original pseudo got a hard register or a hard
5944 register and s is a new split pseudo. The save is put before INSN
5945 if BEFORE_P is true. Return true if we succeed in such
5946 transformation. */
5947 static bool
5948 split_reg (bool before_p, int original_regno, rtx_insn *insn,
5949 rtx next_usage_insns, rtx_insn *to)
5951 enum reg_class rclass;
5952 rtx original_reg;
5953 int hard_regno, nregs;
5954 rtx new_reg, usage_insn;
5955 rtx_insn *restore, *save;
5956 bool after_p;
5957 bool call_save_p;
5958 machine_mode mode;
5960 if (original_regno < FIRST_PSEUDO_REGISTER)
5962 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
5963 hard_regno = original_regno;
5964 call_save_p = false;
5965 nregs = 1;
5966 mode = lra_reg_info[hard_regno].biggest_mode;
5967 machine_mode reg_rtx_mode = GET_MODE (regno_reg_rtx[hard_regno]);
5968 /* A reg can have a biggest_mode of VOIDmode if it was only ever seen as
5969 part of a multi-word register. In that case, just use the reg_rtx
5970 mode. Do the same also if the biggest mode was larger than a register
5971 or we can not compare the modes. Otherwise, limit the size to that of
5972 the biggest access in the function or to the natural mode at least. */
5973 if (mode == VOIDmode
5974 || !ordered_p (GET_MODE_PRECISION (mode),
5975 GET_MODE_PRECISION (reg_rtx_mode))
5976 || paradoxical_subreg_p (mode, reg_rtx_mode)
5977 || maybe_gt (GET_MODE_PRECISION (reg_rtx_mode), GET_MODE_PRECISION (mode)))
5979 original_reg = regno_reg_rtx[hard_regno];
5980 mode = reg_rtx_mode;
5982 else
5983 original_reg = gen_rtx_REG (mode, hard_regno);
5985 else
5987 mode = PSEUDO_REGNO_MODE (original_regno);
5988 hard_regno = reg_renumber[original_regno];
5989 nregs = hard_regno_nregs (hard_regno, mode);
5990 rclass = lra_get_allocno_class (original_regno);
5991 original_reg = regno_reg_rtx[original_regno];
5992 call_save_p = need_for_call_save_p (original_regno);
5994 lra_assert (hard_regno >= 0);
5995 if (lra_dump_file != NULL)
5996 fprintf (lra_dump_file,
5997 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
5999 if (call_save_p)
6001 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
6002 hard_regno_nregs (hard_regno, mode),
6003 mode);
6004 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, NULL, "save");
6006 else
6008 rclass = choose_split_class (rclass, hard_regno, mode);
6009 if (rclass == NO_REGS)
6011 if (lra_dump_file != NULL)
6013 fprintf (lra_dump_file,
6014 " Rejecting split of %d(%s): "
6015 "no good reg class for %d(%s)\n",
6016 original_regno,
6017 reg_class_names[lra_get_allocno_class (original_regno)],
6018 hard_regno,
6019 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
6020 fprintf
6021 (lra_dump_file,
6022 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6024 return false;
6026 /* Split_if_necessary can split hard registers used as part of a
6027 multi-register mode but splits each register individually. The
6028 mode used for each independent register may not be supported
6029 so reject the split. Splitting the wider mode should theoretically
6030 be possible but is not implemented. */
6031 if (!targetm.hard_regno_mode_ok (hard_regno, mode))
6033 if (lra_dump_file != NULL)
6035 fprintf (lra_dump_file,
6036 " Rejecting split of %d(%s): unsuitable mode %s\n",
6037 original_regno,
6038 reg_class_names[lra_get_allocno_class (original_regno)],
6039 GET_MODE_NAME (mode));
6040 fprintf
6041 (lra_dump_file,
6042 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6044 return false;
6046 new_reg = lra_create_new_reg (mode, original_reg, rclass, NULL, "split");
6047 reg_renumber[REGNO (new_reg)] = hard_regno;
6049 int new_regno = REGNO (new_reg);
6050 save = emit_spill_move (true, new_reg, original_reg);
6051 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
6053 if (lra_dump_file != NULL)
6055 fprintf
6056 (lra_dump_file,
6057 " Rejecting split %d->%d resulting in > 2 save insns:\n",
6058 original_regno, new_regno);
6059 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
6060 fprintf (lra_dump_file,
6061 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6063 return false;
6065 restore = emit_spill_move (false, new_reg, original_reg);
6066 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
6068 if (lra_dump_file != NULL)
6070 fprintf (lra_dump_file,
6071 " Rejecting split %d->%d "
6072 "resulting in > 2 restore insns:\n",
6073 original_regno, new_regno);
6074 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
6075 fprintf (lra_dump_file,
6076 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6078 return false;
6080 /* Transfer equivalence information to the spill register, so that
6081 if we fail to allocate the spill register, we have the option of
6082 rematerializing the original value instead of spilling to the stack. */
6083 if (!HARD_REGISTER_NUM_P (original_regno)
6084 && mode == PSEUDO_REGNO_MODE (original_regno))
6085 lra_copy_reg_equiv (new_regno, original_regno, call_save_p);
6086 lra_reg_info[new_regno].restore_rtx = regno_reg_rtx[original_regno];
6087 bitmap_set_bit (&lra_split_regs, new_regno);
6088 if (to != NULL)
6090 lra_assert (next_usage_insns == NULL);
6091 usage_insn = to;
6092 after_p = TRUE;
6094 else
6096 /* We need check_only_regs only inside the inheritance pass. */
6097 bitmap_set_bit (&check_only_regs, new_regno);
6098 bitmap_set_bit (&check_only_regs, original_regno);
6099 after_p = usage_insns[original_regno].after_p;
6100 for (;;)
6102 if (GET_CODE (next_usage_insns) != INSN_LIST)
6104 usage_insn = next_usage_insns;
6105 break;
6107 usage_insn = XEXP (next_usage_insns, 0);
6108 lra_assert (DEBUG_INSN_P (usage_insn));
6109 next_usage_insns = XEXP (next_usage_insns, 1);
6110 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
6111 true);
6112 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
6113 if (lra_dump_file != NULL)
6115 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
6116 original_regno, new_regno);
6117 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
6121 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
6122 lra_assert (usage_insn != insn || (after_p && before_p));
6123 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
6124 after_p ? NULL : restore,
6125 after_p ? restore : NULL,
6126 call_save_p
6127 ? "Add reg<-save" : "Add reg<-split");
6128 lra_process_new_insns (insn, before_p ? save : NULL,
6129 before_p ? NULL : save,
6130 call_save_p
6131 ? "Add save<-reg" : "Add split<-reg");
6132 if (nregs > 1 || original_regno < FIRST_PSEUDO_REGISTER)
6133 /* If we are trying to split multi-register. We should check
6134 conflicts on the next assignment sub-pass. IRA can allocate on
6135 sub-register levels, LRA do this on pseudos level right now and
6136 this discrepancy may create allocation conflicts after
6137 splitting.
6139 If we are trying to split hard register we should also check conflicts
6140 as such splitting can create artificial conflict of the hard register
6141 with another pseudo because of simplified conflict calculation in
6142 LRA. */
6143 check_and_force_assignment_correctness_p = true;
6144 if (lra_dump_file != NULL)
6145 fprintf (lra_dump_file,
6146 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6147 return true;
6150 /* Split a hard reg for reload pseudo REGNO having RCLASS and living
6151 in the range [FROM, TO]. Return true if did a split. Otherwise,
6152 return false. */
6153 bool
6154 spill_hard_reg_in_range (int regno, enum reg_class rclass, rtx_insn *from, rtx_insn *to)
6156 int i, hard_regno;
6157 int rclass_size;
6158 rtx_insn *insn;
6159 unsigned int uid;
6160 bitmap_iterator bi;
6161 HARD_REG_SET ignore;
6163 lra_assert (from != NULL && to != NULL);
6164 ignore = lra_no_alloc_regs;
6165 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
6167 lra_insn_recog_data_t id = lra_insn_recog_data[uid];
6168 struct lra_static_insn_data *static_id = id->insn_static_data;
6169 struct lra_insn_reg *reg;
6171 for (reg = id->regs; reg != NULL; reg = reg->next)
6172 if (reg->regno < FIRST_PSEUDO_REGISTER)
6173 SET_HARD_REG_BIT (ignore, reg->regno);
6174 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
6175 SET_HARD_REG_BIT (ignore, reg->regno);
6177 rclass_size = ira_class_hard_regs_num[rclass];
6178 for (i = 0; i < rclass_size; i++)
6180 hard_regno = ira_class_hard_regs[rclass][i];
6181 if (! TEST_HARD_REG_BIT (lra_reg_info[regno].conflict_hard_regs, hard_regno)
6182 || TEST_HARD_REG_BIT (ignore, hard_regno))
6183 continue;
6184 for (insn = from; insn != NEXT_INSN (to); insn = NEXT_INSN (insn))
6186 struct lra_static_insn_data *static_id;
6187 struct lra_insn_reg *reg;
6189 if (!INSN_P (insn))
6190 continue;
6191 if (bitmap_bit_p (&lra_reg_info[hard_regno].insn_bitmap,
6192 INSN_UID (insn)))
6193 break;
6194 static_id = lra_get_insn_recog_data (insn)->insn_static_data;
6195 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
6196 if (reg->regno == hard_regno)
6197 break;
6198 if (reg != NULL)
6199 break;
6201 if (insn != NEXT_INSN (to))
6202 continue;
6203 if (split_reg (TRUE, hard_regno, from, NULL, to))
6204 return true;
6206 return false;
6209 /* Recognize that we need a split transformation for insn INSN, which
6210 defines or uses REGNO in its insn biggest MODE (we use it only if
6211 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
6212 hard registers which might be used for reloads since the EBB end.
6213 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
6214 uid before starting INSN processing. Return true if we succeed in
6215 such transformation. */
6216 static bool
6217 split_if_necessary (int regno, machine_mode mode,
6218 HARD_REG_SET potential_reload_hard_regs,
6219 bool before_p, rtx_insn *insn, int max_uid)
6221 bool res = false;
6222 int i, nregs = 1;
6223 rtx next_usage_insns;
6225 if (regno < FIRST_PSEUDO_REGISTER)
6226 nregs = hard_regno_nregs (regno, mode);
6227 for (i = 0; i < nregs; i++)
6228 if (usage_insns[regno + i].check == curr_usage_insns_check
6229 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
6230 /* To avoid processing the register twice or more. */
6231 && ((GET_CODE (next_usage_insns) != INSN_LIST
6232 && INSN_UID (next_usage_insns) < max_uid)
6233 || (GET_CODE (next_usage_insns) == INSN_LIST
6234 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
6235 && need_for_split_p (potential_reload_hard_regs, regno + i)
6236 && split_reg (before_p, regno + i, insn, next_usage_insns, NULL))
6237 res = true;
6238 return res;
6241 /* Return TRUE if rtx X is considered as an invariant for
6242 inheritance. */
6243 static bool
6244 invariant_p (const_rtx x)
6246 machine_mode mode;
6247 const char *fmt;
6248 enum rtx_code code;
6249 int i, j;
6251 if (side_effects_p (x))
6252 return false;
6254 code = GET_CODE (x);
6255 mode = GET_MODE (x);
6256 if (code == SUBREG)
6258 x = SUBREG_REG (x);
6259 code = GET_CODE (x);
6260 mode = wider_subreg_mode (mode, GET_MODE (x));
6263 if (MEM_P (x))
6264 return false;
6266 if (REG_P (x))
6268 int i, nregs, regno = REGNO (x);
6270 if (regno >= FIRST_PSEUDO_REGISTER || regno == STACK_POINTER_REGNUM
6271 || TEST_HARD_REG_BIT (eliminable_regset, regno)
6272 || GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
6273 return false;
6274 nregs = hard_regno_nregs (regno, mode);
6275 for (i = 0; i < nregs; i++)
6276 if (! fixed_regs[regno + i]
6277 /* A hard register may be clobbered in the current insn
6278 but we can ignore this case because if the hard
6279 register is used it should be set somewhere after the
6280 clobber. */
6281 || bitmap_bit_p (&invalid_invariant_regs, regno + i))
6282 return false;
6284 fmt = GET_RTX_FORMAT (code);
6285 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6287 if (fmt[i] == 'e')
6289 if (! invariant_p (XEXP (x, i)))
6290 return false;
6292 else if (fmt[i] == 'E')
6294 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6295 if (! invariant_p (XVECEXP (x, i, j)))
6296 return false;
6299 return true;
6302 /* We have 'dest_reg <- invariant'. Let us try to make an invariant
6303 inheritance transformation (using dest_reg instead invariant in a
6304 subsequent insn). */
6305 static bool
6306 process_invariant_for_inheritance (rtx dst_reg, rtx invariant_rtx)
6308 invariant_ptr_t invariant_ptr;
6309 rtx_insn *insn, *new_insns;
6310 rtx insn_set, insn_reg, new_reg;
6311 int insn_regno;
6312 bool succ_p = false;
6313 int dst_regno = REGNO (dst_reg);
6314 machine_mode dst_mode = GET_MODE (dst_reg);
6315 enum reg_class cl = lra_get_allocno_class (dst_regno), insn_reg_cl;
6317 invariant_ptr = insert_invariant (invariant_rtx);
6318 if ((insn = invariant_ptr->insn) != NULL_RTX)
6320 /* We have a subsequent insn using the invariant. */
6321 insn_set = single_set (insn);
6322 lra_assert (insn_set != NULL);
6323 insn_reg = SET_DEST (insn_set);
6324 lra_assert (REG_P (insn_reg));
6325 insn_regno = REGNO (insn_reg);
6326 insn_reg_cl = lra_get_allocno_class (insn_regno);
6328 if (dst_mode == GET_MODE (insn_reg)
6329 /* We should consider only result move reg insns which are
6330 cheap. */
6331 && targetm.register_move_cost (dst_mode, cl, insn_reg_cl) == 2
6332 && targetm.register_move_cost (dst_mode, cl, cl) == 2)
6334 if (lra_dump_file != NULL)
6335 fprintf (lra_dump_file,
6336 " [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[\n");
6337 new_reg = lra_create_new_reg (dst_mode, dst_reg, cl, NULL,
6338 "invariant inheritance");
6339 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
6340 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
6341 lra_reg_info[REGNO (new_reg)].restore_rtx = PATTERN (insn);
6342 start_sequence ();
6343 lra_emit_move (new_reg, dst_reg);
6344 new_insns = get_insns ();
6345 end_sequence ();
6346 lra_process_new_insns (curr_insn, NULL, new_insns,
6347 "Add invariant inheritance<-original");
6348 start_sequence ();
6349 lra_emit_move (SET_DEST (insn_set), new_reg);
6350 new_insns = get_insns ();
6351 end_sequence ();
6352 lra_process_new_insns (insn, NULL, new_insns,
6353 "Changing reload<-inheritance");
6354 lra_set_insn_deleted (insn);
6355 succ_p = true;
6356 if (lra_dump_file != NULL)
6358 fprintf (lra_dump_file,
6359 " Invariant inheritance reuse change %d (bb%d):\n",
6360 REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
6361 dump_insn_slim (lra_dump_file, insn);
6362 fprintf (lra_dump_file,
6363 " ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]\n");
6367 invariant_ptr->insn = curr_insn;
6368 return succ_p;
6371 /* Check only registers living at the current program point in the
6372 current EBB. */
6373 static bitmap_head live_regs;
6375 /* Update live info in EBB given by its HEAD and TAIL insns after
6376 inheritance/split transformation. The function removes dead moves
6377 too. */
6378 static void
6379 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
6381 unsigned int j;
6382 int i, regno;
6383 bool live_p;
6384 rtx_insn *prev_insn;
6385 rtx set;
6386 bool remove_p;
6387 basic_block last_bb, prev_bb, curr_bb;
6388 bitmap_iterator bi;
6389 struct lra_insn_reg *reg;
6390 edge e;
6391 edge_iterator ei;
6393 last_bb = BLOCK_FOR_INSN (tail);
6394 prev_bb = NULL;
6395 for (curr_insn = tail;
6396 curr_insn != PREV_INSN (head);
6397 curr_insn = prev_insn)
6399 prev_insn = PREV_INSN (curr_insn);
6400 /* We need to process empty blocks too. They contain
6401 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
6402 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
6403 continue;
6404 curr_bb = BLOCK_FOR_INSN (curr_insn);
6405 if (curr_bb != prev_bb)
6407 if (prev_bb != NULL)
6409 /* Update df_get_live_in (prev_bb): */
6410 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6411 if (bitmap_bit_p (&live_regs, j))
6412 bitmap_set_bit (df_get_live_in (prev_bb), j);
6413 else
6414 bitmap_clear_bit (df_get_live_in (prev_bb), j);
6416 if (curr_bb != last_bb)
6418 /* Update df_get_live_out (curr_bb): */
6419 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6421 live_p = bitmap_bit_p (&live_regs, j);
6422 if (! live_p)
6423 FOR_EACH_EDGE (e, ei, curr_bb->succs)
6424 if (bitmap_bit_p (df_get_live_in (e->dest), j))
6426 live_p = true;
6427 break;
6429 if (live_p)
6430 bitmap_set_bit (df_get_live_out (curr_bb), j);
6431 else
6432 bitmap_clear_bit (df_get_live_out (curr_bb), j);
6435 prev_bb = curr_bb;
6436 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
6438 if (! NONDEBUG_INSN_P (curr_insn))
6439 continue;
6440 curr_id = lra_get_insn_recog_data (curr_insn);
6441 curr_static_id = curr_id->insn_static_data;
6442 remove_p = false;
6443 if ((set = single_set (curr_insn)) != NULL_RTX
6444 && REG_P (SET_DEST (set))
6445 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
6446 && SET_DEST (set) != pic_offset_table_rtx
6447 && bitmap_bit_p (&check_only_regs, regno)
6448 && ! bitmap_bit_p (&live_regs, regno))
6449 remove_p = true;
6450 /* See which defined values die here. */
6451 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6452 if (reg->type == OP_OUT && ! reg->subreg_p)
6453 bitmap_clear_bit (&live_regs, reg->regno);
6454 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6455 if (reg->type == OP_OUT && ! reg->subreg_p)
6456 bitmap_clear_bit (&live_regs, reg->regno);
6457 if (curr_id->arg_hard_regs != NULL)
6458 /* Make clobbered argument hard registers die. */
6459 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6460 if (regno >= FIRST_PSEUDO_REGISTER)
6461 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
6462 /* Mark each used value as live. */
6463 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6464 if (reg->type != OP_OUT
6465 && bitmap_bit_p (&check_only_regs, reg->regno))
6466 bitmap_set_bit (&live_regs, reg->regno);
6467 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6468 if (reg->type != OP_OUT
6469 && bitmap_bit_p (&check_only_regs, reg->regno))
6470 bitmap_set_bit (&live_regs, reg->regno);
6471 if (curr_id->arg_hard_regs != NULL)
6472 /* Make used argument hard registers live. */
6473 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6474 if (regno < FIRST_PSEUDO_REGISTER
6475 && bitmap_bit_p (&check_only_regs, regno))
6476 bitmap_set_bit (&live_regs, regno);
6477 /* It is quite important to remove dead move insns because it
6478 means removing dead store. We don't need to process them for
6479 constraints. */
6480 if (remove_p)
6482 if (lra_dump_file != NULL)
6484 fprintf (lra_dump_file, " Removing dead insn:\n ");
6485 dump_insn_slim (lra_dump_file, curr_insn);
6487 lra_set_insn_deleted (curr_insn);
6492 /* The structure describes info to do an inheritance for the current
6493 insn. We need to collect such info first before doing the
6494 transformations because the transformations change the insn
6495 internal representation. */
6496 struct to_inherit
6498 /* Original regno. */
6499 int regno;
6500 /* Subsequent insns which can inherit original reg value. */
6501 rtx insns;
6504 /* Array containing all info for doing inheritance from the current
6505 insn. */
6506 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
6508 /* Number elements in the previous array. */
6509 static int to_inherit_num;
6511 /* Add inheritance info REGNO and INSNS. Their meaning is described in
6512 structure to_inherit. */
6513 static void
6514 add_to_inherit (int regno, rtx insns)
6516 int i;
6518 for (i = 0; i < to_inherit_num; i++)
6519 if (to_inherit[i].regno == regno)
6520 return;
6521 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
6522 to_inherit[to_inherit_num].regno = regno;
6523 to_inherit[to_inherit_num++].insns = insns;
6526 /* Return the last non-debug insn in basic block BB, or the block begin
6527 note if none. */
6528 static rtx_insn *
6529 get_last_insertion_point (basic_block bb)
6531 rtx_insn *insn;
6533 FOR_BB_INSNS_REVERSE (bb, insn)
6534 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
6535 return insn;
6536 gcc_unreachable ();
6539 /* Set up RES by registers living on edges FROM except the edge (FROM,
6540 TO) or by registers set up in a jump insn in BB FROM. */
6541 static void
6542 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
6544 rtx_insn *last;
6545 struct lra_insn_reg *reg;
6546 edge e;
6547 edge_iterator ei;
6549 lra_assert (to != NULL);
6550 bitmap_clear (res);
6551 FOR_EACH_EDGE (e, ei, from->succs)
6552 if (e->dest != to)
6553 bitmap_ior_into (res, df_get_live_in (e->dest));
6554 last = get_last_insertion_point (from);
6555 if (! JUMP_P (last))
6556 return;
6557 curr_id = lra_get_insn_recog_data (last);
6558 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6559 if (reg->type != OP_IN)
6560 bitmap_set_bit (res, reg->regno);
6563 /* Used as a temporary results of some bitmap calculations. */
6564 static bitmap_head temp_bitmap;
6566 /* We split for reloads of small class of hard regs. The following
6567 defines how many hard regs the class should have to be qualified as
6568 small. The code is mostly oriented to x86/x86-64 architecture
6569 where some insns need to use only specific register or pair of
6570 registers and these register can live in RTL explicitly, e.g. for
6571 parameter passing. */
6572 static const int max_small_class_regs_num = 2;
6574 /* Do inheritance/split transformations in EBB starting with HEAD and
6575 finishing on TAIL. We process EBB insns in the reverse order.
6576 Return true if we did any inheritance/split transformation in the
6577 EBB.
6579 We should avoid excessive splitting which results in worse code
6580 because of inaccurate cost calculations for spilling new split
6581 pseudos in such case. To achieve this we do splitting only if
6582 register pressure is high in given basic block and there are reload
6583 pseudos requiring hard registers. We could do more register
6584 pressure calculations at any given program point to avoid necessary
6585 splitting even more but it is to expensive and the current approach
6586 works well enough. */
6587 static bool
6588 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
6590 int i, src_regno, dst_regno, nregs;
6591 bool change_p, succ_p, update_reloads_num_p;
6592 rtx_insn *prev_insn, *last_insn;
6593 rtx next_usage_insns, curr_set;
6594 enum reg_class cl;
6595 struct lra_insn_reg *reg;
6596 basic_block last_processed_bb, curr_bb = NULL;
6597 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
6598 bitmap to_process;
6599 unsigned int j;
6600 bitmap_iterator bi;
6601 bool head_p, after_p;
6603 change_p = false;
6604 curr_usage_insns_check++;
6605 clear_invariants ();
6606 reloads_num = calls_num = 0;
6607 for (unsigned int i = 0; i < NUM_ABI_IDS; ++i)
6608 last_call_for_abi[i] = 0;
6609 CLEAR_HARD_REG_SET (full_and_partial_call_clobbers);
6610 bitmap_clear (&check_only_regs);
6611 bitmap_clear (&invalid_invariant_regs);
6612 last_processed_bb = NULL;
6613 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6614 live_hard_regs = eliminable_regset | lra_no_alloc_regs;
6615 /* We don't process new insns generated in the loop. */
6616 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
6618 prev_insn = PREV_INSN (curr_insn);
6619 if (BLOCK_FOR_INSN (curr_insn) != NULL)
6620 curr_bb = BLOCK_FOR_INSN (curr_insn);
6621 if (last_processed_bb != curr_bb)
6623 /* We are at the end of BB. Add qualified living
6624 pseudos for potential splitting. */
6625 to_process = df_get_live_out (curr_bb);
6626 if (last_processed_bb != NULL)
6628 /* We are somewhere in the middle of EBB. */
6629 get_live_on_other_edges (curr_bb, last_processed_bb,
6630 &temp_bitmap);
6631 to_process = &temp_bitmap;
6633 last_processed_bb = curr_bb;
6634 last_insn = get_last_insertion_point (curr_bb);
6635 after_p = (! JUMP_P (last_insn)
6636 && (! CALL_P (last_insn)
6637 || (find_reg_note (last_insn,
6638 REG_NORETURN, NULL_RTX) == NULL_RTX
6639 && ! SIBLING_CALL_P (last_insn))));
6640 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6641 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
6643 if ((int) j >= lra_constraint_new_regno_start)
6644 break;
6645 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
6647 if (j < FIRST_PSEUDO_REGISTER)
6648 SET_HARD_REG_BIT (live_hard_regs, j);
6649 else
6650 add_to_hard_reg_set (&live_hard_regs,
6651 PSEUDO_REGNO_MODE (j),
6652 reg_renumber[j]);
6653 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
6657 src_regno = dst_regno = -1;
6658 curr_set = single_set (curr_insn);
6659 if (curr_set != NULL_RTX && REG_P (SET_DEST (curr_set)))
6660 dst_regno = REGNO (SET_DEST (curr_set));
6661 if (curr_set != NULL_RTX && REG_P (SET_SRC (curr_set)))
6662 src_regno = REGNO (SET_SRC (curr_set));
6663 update_reloads_num_p = true;
6664 if (src_regno < lra_constraint_new_regno_start
6665 && src_regno >= FIRST_PSEUDO_REGISTER
6666 && reg_renumber[src_regno] < 0
6667 && dst_regno >= lra_constraint_new_regno_start
6668 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
6670 /* 'reload_pseudo <- original_pseudo'. */
6671 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6672 reloads_num++;
6673 update_reloads_num_p = false;
6674 succ_p = false;
6675 if (usage_insns[src_regno].check == curr_usage_insns_check
6676 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
6677 succ_p = inherit_reload_reg (false, src_regno, cl,
6678 curr_insn, next_usage_insns);
6679 if (succ_p)
6680 change_p = true;
6681 else
6682 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6683 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6684 potential_reload_hard_regs |= reg_class_contents[cl];
6686 else if (src_regno < 0
6687 && dst_regno >= lra_constraint_new_regno_start
6688 && invariant_p (SET_SRC (curr_set))
6689 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS
6690 && ! bitmap_bit_p (&invalid_invariant_regs, dst_regno)
6691 && ! bitmap_bit_p (&invalid_invariant_regs,
6692 ORIGINAL_REGNO(regno_reg_rtx[dst_regno])))
6694 /* 'reload_pseudo <- invariant'. */
6695 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6696 reloads_num++;
6697 update_reloads_num_p = false;
6698 if (process_invariant_for_inheritance (SET_DEST (curr_set), SET_SRC (curr_set)))
6699 change_p = true;
6700 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6701 potential_reload_hard_regs |= reg_class_contents[cl];
6703 else if (src_regno >= lra_constraint_new_regno_start
6704 && dst_regno < lra_constraint_new_regno_start
6705 && dst_regno >= FIRST_PSEUDO_REGISTER
6706 && reg_renumber[dst_regno] < 0
6707 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
6708 && usage_insns[dst_regno].check == curr_usage_insns_check
6709 && (next_usage_insns
6710 = usage_insns[dst_regno].insns) != NULL_RTX)
6712 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6713 reloads_num++;
6714 update_reloads_num_p = false;
6715 /* 'original_pseudo <- reload_pseudo'. */
6716 if (! JUMP_P (curr_insn)
6717 && inherit_reload_reg (true, dst_regno, cl,
6718 curr_insn, next_usage_insns))
6719 change_p = true;
6720 /* Invalidate. */
6721 usage_insns[dst_regno].check = 0;
6722 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6723 potential_reload_hard_regs |= reg_class_contents[cl];
6725 else if (INSN_P (curr_insn))
6727 int iter;
6728 int max_uid = get_max_uid ();
6730 curr_id = lra_get_insn_recog_data (curr_insn);
6731 curr_static_id = curr_id->insn_static_data;
6732 to_inherit_num = 0;
6733 /* Process insn definitions. */
6734 for (iter = 0; iter < 2; iter++)
6735 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6736 reg != NULL;
6737 reg = reg->next)
6738 if (reg->type != OP_IN
6739 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
6741 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
6742 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
6743 && usage_insns[dst_regno].check == curr_usage_insns_check
6744 && (next_usage_insns
6745 = usage_insns[dst_regno].insns) != NULL_RTX)
6747 struct lra_insn_reg *r;
6749 for (r = curr_id->regs; r != NULL; r = r->next)
6750 if (r->type != OP_OUT && r->regno == dst_regno)
6751 break;
6752 /* Don't do inheritance if the pseudo is also
6753 used in the insn. */
6754 if (r == NULL)
6755 /* We cannot do inheritance right now
6756 because the current insn reg info (chain
6757 regs) can change after that. */
6758 add_to_inherit (dst_regno, next_usage_insns);
6760 /* We cannot process one reg twice here because of
6761 usage_insns invalidation. */
6762 if ((dst_regno < FIRST_PSEUDO_REGISTER
6763 || reg_renumber[dst_regno] >= 0)
6764 && ! reg->subreg_p && reg->type != OP_IN)
6766 HARD_REG_SET s;
6768 if (split_if_necessary (dst_regno, reg->biggest_mode,
6769 potential_reload_hard_regs,
6770 false, curr_insn, max_uid))
6771 change_p = true;
6772 CLEAR_HARD_REG_SET (s);
6773 if (dst_regno < FIRST_PSEUDO_REGISTER)
6774 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
6775 else
6776 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
6777 reg_renumber[dst_regno]);
6778 live_hard_regs &= ~s;
6779 potential_reload_hard_regs &= ~s;
6781 /* We should invalidate potential inheritance or
6782 splitting for the current insn usages to the next
6783 usage insns (see code below) as the output pseudo
6784 prevents this. */
6785 if ((dst_regno >= FIRST_PSEUDO_REGISTER
6786 && reg_renumber[dst_regno] < 0)
6787 || (reg->type == OP_OUT && ! reg->subreg_p
6788 && (dst_regno < FIRST_PSEUDO_REGISTER
6789 || reg_renumber[dst_regno] >= 0)))
6791 /* Invalidate and mark definitions. */
6792 if (dst_regno >= FIRST_PSEUDO_REGISTER)
6793 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
6794 else
6796 nregs = hard_regno_nregs (dst_regno,
6797 reg->biggest_mode);
6798 for (i = 0; i < nregs; i++)
6799 usage_insns[dst_regno + i].check
6800 = -(int) INSN_UID (curr_insn);
6804 /* Process clobbered call regs. */
6805 if (curr_id->arg_hard_regs != NULL)
6806 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6807 if (dst_regno >= FIRST_PSEUDO_REGISTER)
6808 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
6809 = -(int) INSN_UID (curr_insn);
6810 if (! JUMP_P (curr_insn))
6811 for (i = 0; i < to_inherit_num; i++)
6812 if (inherit_reload_reg (true, to_inherit[i].regno,
6813 ALL_REGS, curr_insn,
6814 to_inherit[i].insns))
6815 change_p = true;
6816 if (CALL_P (curr_insn))
6818 rtx cheap, pat, dest;
6819 rtx_insn *restore;
6820 int regno, hard_regno;
6822 calls_num++;
6823 function_abi callee_abi = insn_callee_abi (curr_insn);
6824 last_call_for_abi[callee_abi.id ()] = calls_num;
6825 full_and_partial_call_clobbers
6826 |= callee_abi.full_and_partial_reg_clobbers ();
6827 if ((cheap = find_reg_note (curr_insn,
6828 REG_RETURNED, NULL_RTX)) != NULL_RTX
6829 && ((cheap = XEXP (cheap, 0)), true)
6830 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
6831 && (hard_regno = reg_renumber[regno]) >= 0
6832 && usage_insns[regno].check == curr_usage_insns_check
6833 /* If there are pending saves/restores, the
6834 optimization is not worth. */
6835 && usage_insns[regno].calls_num == calls_num - 1
6836 && callee_abi.clobbers_reg_p (GET_MODE (cheap), hard_regno))
6838 /* Restore the pseudo from the call result as
6839 REG_RETURNED note says that the pseudo value is
6840 in the call result and the pseudo is an argument
6841 of the call. */
6842 pat = PATTERN (curr_insn);
6843 if (GET_CODE (pat) == PARALLEL)
6844 pat = XVECEXP (pat, 0, 0);
6845 dest = SET_DEST (pat);
6846 /* For multiple return values dest is PARALLEL.
6847 Currently we handle only single return value case. */
6848 if (REG_P (dest))
6850 start_sequence ();
6851 emit_move_insn (cheap, copy_rtx (dest));
6852 restore = get_insns ();
6853 end_sequence ();
6854 lra_process_new_insns (curr_insn, NULL, restore,
6855 "Inserting call parameter restore");
6856 /* We don't need to save/restore of the pseudo from
6857 this call. */
6858 usage_insns[regno].calls_num = calls_num;
6859 remove_from_hard_reg_set
6860 (&full_and_partial_call_clobbers,
6861 GET_MODE (cheap), hard_regno);
6862 bitmap_set_bit (&check_only_regs, regno);
6866 to_inherit_num = 0;
6867 /* Process insn usages. */
6868 for (iter = 0; iter < 2; iter++)
6869 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6870 reg != NULL;
6871 reg = reg->next)
6872 if ((reg->type != OP_OUT
6873 || (reg->type == OP_OUT && reg->subreg_p))
6874 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
6876 if (src_regno >= FIRST_PSEUDO_REGISTER
6877 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
6879 if (usage_insns[src_regno].check == curr_usage_insns_check
6880 && (next_usage_insns
6881 = usage_insns[src_regno].insns) != NULL_RTX
6882 && NONDEBUG_INSN_P (curr_insn))
6883 add_to_inherit (src_regno, next_usage_insns);
6884 else if (usage_insns[src_regno].check
6885 != -(int) INSN_UID (curr_insn))
6886 /* Add usages but only if the reg is not set up
6887 in the same insn. */
6888 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6890 else if (src_regno < FIRST_PSEUDO_REGISTER
6891 || reg_renumber[src_regno] >= 0)
6893 bool before_p;
6894 rtx_insn *use_insn = curr_insn;
6896 before_p = (JUMP_P (curr_insn)
6897 || (CALL_P (curr_insn) && reg->type == OP_IN));
6898 if (NONDEBUG_INSN_P (curr_insn)
6899 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
6900 && split_if_necessary (src_regno, reg->biggest_mode,
6901 potential_reload_hard_regs,
6902 before_p, curr_insn, max_uid))
6904 if (reg->subreg_p)
6905 check_and_force_assignment_correctness_p = true;
6906 change_p = true;
6907 /* Invalidate. */
6908 usage_insns[src_regno].check = 0;
6909 if (before_p)
6910 use_insn = PREV_INSN (curr_insn);
6912 if (NONDEBUG_INSN_P (curr_insn))
6914 if (src_regno < FIRST_PSEUDO_REGISTER)
6915 add_to_hard_reg_set (&live_hard_regs,
6916 reg->biggest_mode, src_regno);
6917 else
6918 add_to_hard_reg_set (&live_hard_regs,
6919 PSEUDO_REGNO_MODE (src_regno),
6920 reg_renumber[src_regno]);
6922 if (src_regno >= FIRST_PSEUDO_REGISTER)
6923 add_next_usage_insn (src_regno, use_insn, reloads_num);
6924 else
6926 for (i = 0; i < hard_regno_nregs (src_regno, reg->biggest_mode); i++)
6927 add_next_usage_insn (src_regno + i, use_insn, reloads_num);
6931 /* Process used call regs. */
6932 if (curr_id->arg_hard_regs != NULL)
6933 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6934 if (src_regno < FIRST_PSEUDO_REGISTER)
6936 SET_HARD_REG_BIT (live_hard_regs, src_regno);
6937 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6939 for (i = 0; i < to_inherit_num; i++)
6941 src_regno = to_inherit[i].regno;
6942 if (inherit_reload_reg (false, src_regno, ALL_REGS,
6943 curr_insn, to_inherit[i].insns))
6944 change_p = true;
6945 else
6946 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6949 if (update_reloads_num_p
6950 && NONDEBUG_INSN_P (curr_insn) && curr_set != NULL_RTX)
6952 int regno = -1;
6953 if ((REG_P (SET_DEST (curr_set))
6954 && (regno = REGNO (SET_DEST (curr_set))) >= lra_constraint_new_regno_start
6955 && reg_renumber[regno] < 0
6956 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
6957 || (REG_P (SET_SRC (curr_set))
6958 && (regno = REGNO (SET_SRC (curr_set))) >= lra_constraint_new_regno_start
6959 && reg_renumber[regno] < 0
6960 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
6962 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6963 reloads_num++;
6964 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6965 potential_reload_hard_regs |= reg_class_contents[cl];
6968 if (NONDEBUG_INSN_P (curr_insn))
6970 int regno;
6972 /* Invalidate invariants with changed regs. */
6973 curr_id = lra_get_insn_recog_data (curr_insn);
6974 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6975 if (reg->type != OP_IN)
6977 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6978 bitmap_set_bit (&invalid_invariant_regs,
6979 ORIGINAL_REGNO (regno_reg_rtx[reg->regno]));
6981 curr_static_id = curr_id->insn_static_data;
6982 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6983 if (reg->type != OP_IN)
6984 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6985 if (curr_id->arg_hard_regs != NULL)
6986 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6987 if (regno >= FIRST_PSEUDO_REGISTER)
6988 bitmap_set_bit (&invalid_invariant_regs,
6989 regno - FIRST_PSEUDO_REGISTER);
6991 /* We reached the start of the current basic block. */
6992 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
6993 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
6995 /* We reached the beginning of the current block -- do
6996 rest of spliting in the current BB. */
6997 to_process = df_get_live_in (curr_bb);
6998 if (BLOCK_FOR_INSN (head) != curr_bb)
7000 /* We are somewhere in the middle of EBB. */
7001 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
7002 curr_bb, &temp_bitmap);
7003 to_process = &temp_bitmap;
7005 head_p = true;
7006 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
7008 if ((int) j >= lra_constraint_new_regno_start)
7009 break;
7010 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
7011 && usage_insns[j].check == curr_usage_insns_check
7012 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
7014 if (need_for_split_p (potential_reload_hard_regs, j))
7016 if (lra_dump_file != NULL && head_p)
7018 fprintf (lra_dump_file,
7019 " ----------------------------------\n");
7020 head_p = false;
7022 if (split_reg (false, j, bb_note (curr_bb),
7023 next_usage_insns, NULL))
7024 change_p = true;
7026 usage_insns[j].check = 0;
7031 return change_p;
7034 /* This value affects EBB forming. If probability of edge from EBB to
7035 a BB is not greater than the following value, we don't add the BB
7036 to EBB. */
7037 #define EBB_PROBABILITY_CUTOFF \
7038 ((REG_BR_PROB_BASE * param_lra_inheritance_ebb_probability_cutoff) / 100)
7040 /* Current number of inheritance/split iteration. */
7041 int lra_inheritance_iter;
7043 /* Entry function for inheritance/split pass. */
7044 void
7045 lra_inheritance (void)
7047 int i;
7048 basic_block bb, start_bb;
7049 edge e;
7051 lra_inheritance_iter++;
7052 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
7053 return;
7054 timevar_push (TV_LRA_INHERITANCE);
7055 if (lra_dump_file != NULL)
7056 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
7057 lra_inheritance_iter);
7058 curr_usage_insns_check = 0;
7059 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
7060 for (i = 0; i < lra_constraint_new_regno_start; i++)
7061 usage_insns[i].check = 0;
7062 bitmap_initialize (&check_only_regs, &reg_obstack);
7063 bitmap_initialize (&invalid_invariant_regs, &reg_obstack);
7064 bitmap_initialize (&live_regs, &reg_obstack);
7065 bitmap_initialize (&temp_bitmap, &reg_obstack);
7066 bitmap_initialize (&ebb_global_regs, &reg_obstack);
7067 FOR_EACH_BB_FN (bb, cfun)
7069 start_bb = bb;
7070 if (lra_dump_file != NULL)
7071 fprintf (lra_dump_file, "EBB");
7072 /* Form a EBB starting with BB. */
7073 bitmap_clear (&ebb_global_regs);
7074 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
7075 for (;;)
7077 if (lra_dump_file != NULL)
7078 fprintf (lra_dump_file, " %d", bb->index);
7079 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
7080 || LABEL_P (BB_HEAD (bb->next_bb)))
7081 break;
7082 e = find_fallthru_edge (bb->succs);
7083 if (! e)
7084 break;
7085 if (e->probability.initialized_p ()
7086 && e->probability.to_reg_br_prob_base () < EBB_PROBABILITY_CUTOFF)
7087 break;
7088 bb = bb->next_bb;
7090 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
7091 if (lra_dump_file != NULL)
7092 fprintf (lra_dump_file, "\n");
7093 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
7094 /* Remember that the EBB head and tail can change in
7095 inherit_in_ebb. */
7096 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
7098 bitmap_release (&ebb_global_regs);
7099 bitmap_release (&temp_bitmap);
7100 bitmap_release (&live_regs);
7101 bitmap_release (&invalid_invariant_regs);
7102 bitmap_release (&check_only_regs);
7103 free (usage_insns);
7105 timevar_pop (TV_LRA_INHERITANCE);
7110 /* This page contains code to undo failed inheritance/split
7111 transformations. */
7113 /* Current number of iteration undoing inheritance/split. */
7114 int lra_undo_inheritance_iter;
7116 /* Fix BB live info LIVE after removing pseudos created on pass doing
7117 inheritance/split which are REMOVED_PSEUDOS. */
7118 static void
7119 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
7121 unsigned int regno;
7122 bitmap_iterator bi;
7124 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
7125 if (bitmap_clear_bit (live, regno)
7126 && REG_P (lra_reg_info[regno].restore_rtx))
7127 bitmap_set_bit (live, REGNO (lra_reg_info[regno].restore_rtx));
7130 /* Return regno of the (subreg of) REG. Otherwise, return a negative
7131 number. */
7132 static int
7133 get_regno (rtx reg)
7135 if (GET_CODE (reg) == SUBREG)
7136 reg = SUBREG_REG (reg);
7137 if (REG_P (reg))
7138 return REGNO (reg);
7139 return -1;
7142 /* Delete a move INSN with destination reg DREGNO and a previous
7143 clobber insn with the same regno. The inheritance/split code can
7144 generate moves with preceding clobber and when we delete such moves
7145 we should delete the clobber insn too to keep the correct life
7146 info. */
7147 static void
7148 delete_move_and_clobber (rtx_insn *insn, int dregno)
7150 rtx_insn *prev_insn = PREV_INSN (insn);
7152 lra_set_insn_deleted (insn);
7153 lra_assert (dregno >= 0);
7154 if (prev_insn != NULL && NONDEBUG_INSN_P (prev_insn)
7155 && GET_CODE (PATTERN (prev_insn)) == CLOBBER
7156 && dregno == get_regno (XEXP (PATTERN (prev_insn), 0)))
7157 lra_set_insn_deleted (prev_insn);
7160 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
7161 return true if we did any change. The undo transformations for
7162 inheritance looks like
7163 i <- i2
7164 p <- i => p <- i2
7165 or removing
7166 p <- i, i <- p, and i <- i3
7167 where p is original pseudo from which inheritance pseudo i was
7168 created, i and i3 are removed inheritance pseudos, i2 is another
7169 not removed inheritance pseudo. All split pseudos or other
7170 occurrences of removed inheritance pseudos are changed on the
7171 corresponding original pseudos.
7173 The function also schedules insns changed and created during
7174 inheritance/split pass for processing by the subsequent constraint
7175 pass. */
7176 static bool
7177 remove_inheritance_pseudos (bitmap remove_pseudos)
7179 basic_block bb;
7180 int regno, sregno, prev_sregno, dregno;
7181 rtx restore_rtx;
7182 rtx set, prev_set;
7183 rtx_insn *prev_insn;
7184 bool change_p, done_p;
7186 change_p = ! bitmap_empty_p (remove_pseudos);
7187 /* We cannot finish the function right away if CHANGE_P is true
7188 because we need to marks insns affected by previous
7189 inheritance/split pass for processing by the subsequent
7190 constraint pass. */
7191 FOR_EACH_BB_FN (bb, cfun)
7193 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
7194 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
7195 FOR_BB_INSNS_REVERSE (bb, curr_insn)
7197 if (! INSN_P (curr_insn))
7198 continue;
7199 done_p = false;
7200 sregno = dregno = -1;
7201 if (change_p && NONDEBUG_INSN_P (curr_insn)
7202 && (set = single_set (curr_insn)) != NULL_RTX)
7204 dregno = get_regno (SET_DEST (set));
7205 sregno = get_regno (SET_SRC (set));
7208 if (sregno >= 0 && dregno >= 0)
7210 if (bitmap_bit_p (remove_pseudos, dregno)
7211 && ! REG_P (lra_reg_info[dregno].restore_rtx))
7213 /* invariant inheritance pseudo <- original pseudo */
7214 if (lra_dump_file != NULL)
7216 fprintf (lra_dump_file, " Removing invariant inheritance:\n");
7217 dump_insn_slim (lra_dump_file, curr_insn);
7218 fprintf (lra_dump_file, "\n");
7220 delete_move_and_clobber (curr_insn, dregno);
7221 done_p = true;
7223 else if (bitmap_bit_p (remove_pseudos, sregno)
7224 && ! REG_P (lra_reg_info[sregno].restore_rtx))
7226 /* reload pseudo <- invariant inheritance pseudo */
7227 start_sequence ();
7228 /* We cannot just change the source. It might be
7229 an insn different from the move. */
7230 emit_insn (lra_reg_info[sregno].restore_rtx);
7231 rtx_insn *new_insns = get_insns ();
7232 end_sequence ();
7233 lra_assert (single_set (new_insns) != NULL
7234 && SET_DEST (set) == SET_DEST (single_set (new_insns)));
7235 lra_process_new_insns (curr_insn, NULL, new_insns,
7236 "Changing reload<-invariant inheritance");
7237 delete_move_and_clobber (curr_insn, dregno);
7238 done_p = true;
7240 else if ((bitmap_bit_p (remove_pseudos, sregno)
7241 && (get_regno (lra_reg_info[sregno].restore_rtx) == dregno
7242 || (bitmap_bit_p (remove_pseudos, dregno)
7243 && get_regno (lra_reg_info[sregno].restore_rtx) >= 0
7244 && (get_regno (lra_reg_info[sregno].restore_rtx)
7245 == get_regno (lra_reg_info[dregno].restore_rtx)))))
7246 || (bitmap_bit_p (remove_pseudos, dregno)
7247 && get_regno (lra_reg_info[dregno].restore_rtx) == sregno))
7248 /* One of the following cases:
7249 original <- removed inheritance pseudo
7250 removed inherit pseudo <- another removed inherit pseudo
7251 removed inherit pseudo <- original pseudo
7253 removed_split_pseudo <- original_reg
7254 original_reg <- removed_split_pseudo */
7256 if (lra_dump_file != NULL)
7258 fprintf (lra_dump_file, " Removing %s:\n",
7259 bitmap_bit_p (&lra_split_regs, sregno)
7260 || bitmap_bit_p (&lra_split_regs, dregno)
7261 ? "split" : "inheritance");
7262 dump_insn_slim (lra_dump_file, curr_insn);
7264 delete_move_and_clobber (curr_insn, dregno);
7265 done_p = true;
7267 else if (bitmap_bit_p (remove_pseudos, sregno)
7268 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
7270 /* Search the following pattern:
7271 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
7272 original_pseudo <- inherit_or_split_pseudo1
7273 where the 2nd insn is the current insn and
7274 inherit_or_split_pseudo2 is not removed. If it is found,
7275 change the current insn onto:
7276 original_pseudo <- inherit_or_split_pseudo2. */
7277 for (prev_insn = PREV_INSN (curr_insn);
7278 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
7279 prev_insn = PREV_INSN (prev_insn))
7281 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
7282 && (prev_set = single_set (prev_insn)) != NULL_RTX
7283 /* There should be no subregs in insn we are
7284 searching because only the original reg might
7285 be in subreg when we changed the mode of
7286 load/store for splitting. */
7287 && REG_P (SET_DEST (prev_set))
7288 && REG_P (SET_SRC (prev_set))
7289 && (int) REGNO (SET_DEST (prev_set)) == sregno
7290 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
7291 >= FIRST_PSEUDO_REGISTER)
7292 && (lra_reg_info[prev_sregno].restore_rtx == NULL_RTX
7294 /* As we consider chain of inheritance or
7295 splitting described in above comment we should
7296 check that sregno and prev_sregno were
7297 inheritance/split pseudos created from the
7298 same original regno. */
7299 (get_regno (lra_reg_info[sregno].restore_rtx) >= 0
7300 && (get_regno (lra_reg_info[sregno].restore_rtx)
7301 == get_regno (lra_reg_info[prev_sregno].restore_rtx))))
7302 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
7304 lra_assert (GET_MODE (SET_SRC (prev_set))
7305 == GET_MODE (regno_reg_rtx[sregno]));
7306 /* Although we have a single set, the insn can
7307 contain more one sregno register occurrence
7308 as a source. Change all occurrences. */
7309 lra_substitute_pseudo_within_insn (curr_insn, sregno,
7310 SET_SRC (prev_set),
7311 false);
7312 /* As we are finishing with processing the insn
7313 here, check the destination too as it might
7314 inheritance pseudo for another pseudo. */
7315 if (bitmap_bit_p (remove_pseudos, dregno)
7316 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
7317 && (restore_rtx
7318 = lra_reg_info[dregno].restore_rtx) != NULL_RTX)
7320 if (GET_CODE (SET_DEST (set)) == SUBREG)
7321 SUBREG_REG (SET_DEST (set)) = restore_rtx;
7322 else
7323 SET_DEST (set) = restore_rtx;
7325 lra_push_insn_and_update_insn_regno_info (curr_insn);
7326 lra_set_used_insn_alternative_by_uid
7327 (INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
7328 done_p = true;
7329 if (lra_dump_file != NULL)
7331 fprintf (lra_dump_file, " Change reload insn:\n");
7332 dump_insn_slim (lra_dump_file, curr_insn);
7337 if (! done_p)
7339 struct lra_insn_reg *reg;
7340 bool restored_regs_p = false;
7341 bool kept_regs_p = false;
7343 curr_id = lra_get_insn_recog_data (curr_insn);
7344 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
7346 regno = reg->regno;
7347 restore_rtx = lra_reg_info[regno].restore_rtx;
7348 if (restore_rtx != NULL_RTX)
7350 if (change_p && bitmap_bit_p (remove_pseudos, regno))
7352 lra_substitute_pseudo_within_insn
7353 (curr_insn, regno, restore_rtx, false);
7354 restored_regs_p = true;
7356 else
7357 kept_regs_p = true;
7360 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
7362 /* The instruction has changed since the previous
7363 constraints pass. */
7364 lra_push_insn_and_update_insn_regno_info (curr_insn);
7365 lra_set_used_insn_alternative_by_uid
7366 (INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
7368 else if (restored_regs_p)
7369 /* The instruction has been restored to the form that
7370 it had during the previous constraints pass. */
7371 lra_update_insn_regno_info (curr_insn);
7372 if (restored_regs_p && lra_dump_file != NULL)
7374 fprintf (lra_dump_file, " Insn after restoring regs:\n");
7375 dump_insn_slim (lra_dump_file, curr_insn);
7380 return change_p;
7383 /* If optional reload pseudos failed to get a hard register or was not
7384 inherited, it is better to remove optional reloads. We do this
7385 transformation after undoing inheritance to figure out necessity to
7386 remove optional reloads easier. Return true if we do any
7387 change. */
7388 static bool
7389 undo_optional_reloads (void)
7391 bool change_p, keep_p;
7392 unsigned int regno, uid;
7393 bitmap_iterator bi, bi2;
7394 rtx_insn *insn;
7395 rtx set, src, dest;
7396 auto_bitmap removed_optional_reload_pseudos (&reg_obstack);
7398 bitmap_copy (removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
7399 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7401 keep_p = false;
7402 /* Keep optional reloads from previous subpasses. */
7403 if (lra_reg_info[regno].restore_rtx == NULL_RTX
7404 /* If the original pseudo changed its allocation, just
7405 removing the optional pseudo is dangerous as the original
7406 pseudo will have longer live range. */
7407 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] >= 0)
7408 keep_p = true;
7409 else if (reg_renumber[regno] >= 0)
7410 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
7412 insn = lra_insn_recog_data[uid]->insn;
7413 if ((set = single_set (insn)) == NULL_RTX)
7414 continue;
7415 src = SET_SRC (set);
7416 dest = SET_DEST (set);
7417 if ((! REG_P (src) && ! SUBREG_P (src))
7418 || (! REG_P (dest) && ! SUBREG_P (dest)))
7419 continue;
7420 if (get_regno (dest) == (int) regno
7421 /* Ignore insn for optional reloads itself. */
7422 && (get_regno (lra_reg_info[regno].restore_rtx)
7423 != get_regno (src))
7424 /* Check only inheritance on last inheritance pass. */
7425 && get_regno (src) >= new_regno_start
7426 /* Check that the optional reload was inherited. */
7427 && bitmap_bit_p (&lra_inheritance_pseudos, get_regno (src)))
7429 keep_p = true;
7430 break;
7433 if (keep_p)
7435 bitmap_clear_bit (removed_optional_reload_pseudos, regno);
7436 if (lra_dump_file != NULL)
7437 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
7440 change_p = ! bitmap_empty_p (removed_optional_reload_pseudos);
7441 auto_bitmap insn_bitmap (&reg_obstack);
7442 EXECUTE_IF_SET_IN_BITMAP (removed_optional_reload_pseudos, 0, regno, bi)
7444 if (lra_dump_file != NULL)
7445 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
7446 bitmap_copy (insn_bitmap, &lra_reg_info[regno].insn_bitmap);
7447 EXECUTE_IF_SET_IN_BITMAP (insn_bitmap, 0, uid, bi2)
7449 /* We may have already removed a clobber. */
7450 if (!lra_insn_recog_data[uid])
7451 continue;
7452 insn = lra_insn_recog_data[uid]->insn;
7453 if ((set = single_set (insn)) != NULL_RTX)
7455 src = SET_SRC (set);
7456 dest = SET_DEST (set);
7457 if ((REG_P (src) || SUBREG_P (src))
7458 && (REG_P (dest) || SUBREG_P (dest))
7459 && ((get_regno (src) == (int) regno
7460 && (get_regno (lra_reg_info[regno].restore_rtx)
7461 == get_regno (dest)))
7462 || (get_regno (dest) == (int) regno
7463 && (get_regno (lra_reg_info[regno].restore_rtx)
7464 == get_regno (src)))))
7466 if (lra_dump_file != NULL)
7468 fprintf (lra_dump_file, " Deleting move %u\n",
7469 INSN_UID (insn));
7470 dump_insn_slim (lra_dump_file, insn);
7472 delete_move_and_clobber (insn, get_regno (dest));
7473 continue;
7475 /* We should not worry about generation memory-memory
7476 moves here as if the corresponding inheritance did
7477 not work (inheritance pseudo did not get a hard reg),
7478 we remove the inheritance pseudo and the optional
7479 reload. */
7481 if (GET_CODE (PATTERN (insn)) == CLOBBER
7482 && REG_P (SET_DEST (insn))
7483 && get_regno (SET_DEST (insn)) == (int) regno)
7484 /* Refuse to remap clobbers to preexisting pseudos. */
7485 gcc_unreachable ();
7486 lra_substitute_pseudo_within_insn
7487 (insn, regno, lra_reg_info[regno].restore_rtx, false);
7488 lra_update_insn_regno_info (insn);
7489 if (lra_dump_file != NULL)
7491 fprintf (lra_dump_file,
7492 " Restoring original insn:\n");
7493 dump_insn_slim (lra_dump_file, insn);
7497 /* Clear restore_regnos. */
7498 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7499 lra_reg_info[regno].restore_rtx = NULL_RTX;
7500 return change_p;
7503 /* Entry function for undoing inheritance/split transformation. Return true
7504 if we did any RTL change in this pass. */
7505 bool
7506 lra_undo_inheritance (void)
7508 unsigned int regno;
7509 int hard_regno;
7510 int n_all_inherit, n_inherit, n_all_split, n_split;
7511 rtx restore_rtx;
7512 bitmap_iterator bi;
7513 bool change_p;
7515 lra_undo_inheritance_iter++;
7516 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
7517 return false;
7518 if (lra_dump_file != NULL)
7519 fprintf (lra_dump_file,
7520 "\n********** Undoing inheritance #%d: **********\n\n",
7521 lra_undo_inheritance_iter);
7522 auto_bitmap remove_pseudos (&reg_obstack);
7523 n_inherit = n_all_inherit = 0;
7524 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7525 if (lra_reg_info[regno].restore_rtx != NULL_RTX)
7527 n_all_inherit++;
7528 if (reg_renumber[regno] < 0
7529 /* If the original pseudo changed its allocation, just
7530 removing inheritance is dangerous as for changing
7531 allocation we used shorter live-ranges. */
7532 && (! REG_P (lra_reg_info[regno].restore_rtx)
7533 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] < 0))
7534 bitmap_set_bit (remove_pseudos, regno);
7535 else
7536 n_inherit++;
7538 if (lra_dump_file != NULL && n_all_inherit != 0)
7539 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
7540 n_inherit, n_all_inherit,
7541 (double) n_inherit / n_all_inherit * 100);
7542 n_split = n_all_split = 0;
7543 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7544 if ((restore_rtx = lra_reg_info[regno].restore_rtx) != NULL_RTX)
7546 int restore_regno = REGNO (restore_rtx);
7548 n_all_split++;
7549 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
7550 ? reg_renumber[restore_regno] : restore_regno);
7551 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
7552 bitmap_set_bit (remove_pseudos, regno);
7553 else
7555 n_split++;
7556 if (lra_dump_file != NULL)
7557 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
7558 regno, restore_regno);
7561 if (lra_dump_file != NULL && n_all_split != 0)
7562 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
7563 n_split, n_all_split,
7564 (double) n_split / n_all_split * 100);
7565 change_p = remove_inheritance_pseudos (remove_pseudos);
7566 /* Clear restore_regnos. */
7567 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7568 lra_reg_info[regno].restore_rtx = NULL_RTX;
7569 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7570 lra_reg_info[regno].restore_rtx = NULL_RTX;
7571 change_p = undo_optional_reloads () || change_p;
7572 return change_p;