MIPS compact branch support
[official-gcc.git] / gcc / lra-constraints.c
blobcdb2695acfa87875de079dc2ed19e21bff99d6fd
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2015 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 "predict.h"
114 #include "tree.h"
115 #include "rtl.h"
116 #include "df.h"
117 #include "tm_p.h"
118 #include "regs.h"
119 #include "insn-config.h"
120 #include "insn-codes.h"
121 #include "recog.h"
122 #include "output.h"
123 #include "addresses.h"
124 #include "target.h"
125 #include "flags.h"
126 #include "alias.h"
127 #include "expmed.h"
128 #include "dojump.h"
129 #include "explow.h"
130 #include "calls.h"
131 #include "emit-rtl.h"
132 #include "varasm.h"
133 #include "stmt.h"
134 #include "expr.h"
135 #include "cfgrtl.h"
136 #include "except.h"
137 #include "optabs.h"
138 #include "ira.h"
139 #include "rtl-error.h"
140 #include "params.h"
141 #include "lra.h"
142 #include "insn-attr.h"
143 #include "lra-int.h"
145 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
146 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
147 reload insns. */
148 static int bb_reload_num;
150 /* The current insn being processed and corresponding its single set
151 (NULL otherwise), its data (basic block, the insn data, the insn
152 static data, and the mode of each operand). */
153 static rtx_insn *curr_insn;
154 static rtx curr_insn_set;
155 static basic_block curr_bb;
156 static lra_insn_recog_data_t curr_id;
157 static struct lra_static_insn_data *curr_static_id;
158 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
159 /* Mode of the register substituted by its equivalence with VOIDmode
160 (e.g. constant) and whose subreg is given operand of the current
161 insn. VOIDmode in all other cases. */
162 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
166 /* Start numbers for new registers and insns at the current constraints
167 pass start. */
168 static int new_regno_start;
169 static int new_insn_uid_start;
171 /* If LOC is nonnull, strip any outer subreg from it. */
172 static inline rtx *
173 strip_subreg (rtx *loc)
175 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
178 /* Return hard regno of REGNO or if it is was not assigned to a hard
179 register, use a hard register from its allocno class. */
180 static int
181 get_try_hard_regno (int regno)
183 int hard_regno;
184 enum reg_class rclass;
186 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
187 hard_regno = lra_get_regno_hard_regno (regno);
188 if (hard_regno >= 0)
189 return hard_regno;
190 rclass = lra_get_allocno_class (regno);
191 if (rclass == NO_REGS)
192 return -1;
193 return ira_class_hard_regs[rclass][0];
196 /* Return final hard regno (plus offset) which will be after
197 elimination. We do this for matching constraints because the final
198 hard regno could have a different class. */
199 static int
200 get_final_hard_regno (int hard_regno, int offset)
202 if (hard_regno < 0)
203 return hard_regno;
204 hard_regno = lra_get_elimination_hard_regno (hard_regno);
205 return hard_regno + offset;
208 /* Return hard regno of X after removing subreg and making
209 elimination. If X is not a register or subreg of register, return
210 -1. For pseudo use its assignment. */
211 static int
212 get_hard_regno (rtx x)
214 rtx reg;
215 int offset, hard_regno;
217 reg = x;
218 if (GET_CODE (x) == SUBREG)
219 reg = SUBREG_REG (x);
220 if (! REG_P (reg))
221 return -1;
222 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
223 hard_regno = lra_get_regno_hard_regno (hard_regno);
224 if (hard_regno < 0)
225 return -1;
226 offset = 0;
227 if (GET_CODE (x) == SUBREG)
228 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
229 SUBREG_BYTE (x), GET_MODE (x));
230 return get_final_hard_regno (hard_regno, offset);
233 /* If REGNO is a hard register or has been allocated a hard register,
234 return the class of that register. If REGNO is a reload pseudo
235 created by the current constraints pass, return its allocno class.
236 Return NO_REGS otherwise. */
237 static enum reg_class
238 get_reg_class (int regno)
240 int hard_regno;
242 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
243 hard_regno = lra_get_regno_hard_regno (regno);
244 if (hard_regno >= 0)
246 hard_regno = get_final_hard_regno (hard_regno, 0);
247 return REGNO_REG_CLASS (hard_regno);
249 if (regno >= new_regno_start)
250 return lra_get_allocno_class (regno);
251 return NO_REGS;
254 /* Return true if REG satisfies (or will satisfy) reg class constraint
255 CL. Use elimination first if REG is a hard register. If REG is a
256 reload pseudo created by this constraints pass, assume that it will
257 be allocated a hard register from its allocno class, but allow that
258 class to be narrowed to CL if it is currently a superset of CL.
260 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
261 REGNO (reg), or NO_REGS if no change in its class was needed. */
262 static bool
263 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
265 enum reg_class rclass, common_class;
266 machine_mode reg_mode;
267 int class_size, hard_regno, nregs, i, j;
268 int regno = REGNO (reg);
270 if (new_class != NULL)
271 *new_class = NO_REGS;
272 if (regno < FIRST_PSEUDO_REGISTER)
274 rtx final_reg = reg;
275 rtx *final_loc = &final_reg;
277 lra_eliminate_reg_if_possible (final_loc);
278 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
280 reg_mode = GET_MODE (reg);
281 rclass = get_reg_class (regno);
282 if (regno < new_regno_start
283 /* Do not allow the constraints for reload instructions to
284 influence the classes of new pseudos. These reloads are
285 typically moves that have many alternatives, and restricting
286 reload pseudos for one alternative may lead to situations
287 where other reload pseudos are no longer allocatable. */
288 || (INSN_UID (curr_insn) >= new_insn_uid_start
289 && curr_insn_set != NULL
290 && ((OBJECT_P (SET_SRC (curr_insn_set))
291 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
292 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
293 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
294 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
295 /* When we don't know what class will be used finally for reload
296 pseudos, we use ALL_REGS. */
297 return ((regno >= new_regno_start && rclass == ALL_REGS)
298 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
299 && ! hard_reg_set_subset_p (reg_class_contents[cl],
300 lra_no_alloc_regs)));
301 else
303 common_class = ira_reg_class_subset[rclass][cl];
304 if (new_class != NULL)
305 *new_class = common_class;
306 if (hard_reg_set_subset_p (reg_class_contents[common_class],
307 lra_no_alloc_regs))
308 return false;
309 /* Check that there are enough allocatable regs. */
310 class_size = ira_class_hard_regs_num[common_class];
311 for (i = 0; i < class_size; i++)
313 hard_regno = ira_class_hard_regs[common_class][i];
314 nregs = hard_regno_nregs[hard_regno][reg_mode];
315 if (nregs == 1)
316 return true;
317 for (j = 0; j < nregs; j++)
318 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
319 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
320 hard_regno + j))
321 break;
322 if (j >= nregs)
323 return true;
325 return false;
329 /* Return true if REGNO satisfies a memory constraint. */
330 static bool
331 in_mem_p (int regno)
333 return get_reg_class (regno) == NO_REGS;
336 /* Return 1 if ADDR is a valid memory address for mode MODE in address
337 space AS, and check that each pseudo has the proper kind of hard
338 reg. */
339 static int
340 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
341 rtx addr, addr_space_t as)
343 #ifdef GO_IF_LEGITIMATE_ADDRESS
344 lra_assert (ADDR_SPACE_GENERIC_P (as));
345 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
346 return 0;
348 win:
349 return 1;
350 #else
351 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
352 #endif
355 namespace {
356 /* Temporarily eliminates registers in an address (for the lifetime of
357 the object). */
358 class address_eliminator {
359 public:
360 address_eliminator (struct address_info *ad);
361 ~address_eliminator ();
363 private:
364 struct address_info *m_ad;
365 rtx *m_base_loc;
366 rtx m_base_reg;
367 rtx *m_index_loc;
368 rtx m_index_reg;
372 address_eliminator::address_eliminator (struct address_info *ad)
373 : m_ad (ad),
374 m_base_loc (strip_subreg (ad->base_term)),
375 m_base_reg (NULL_RTX),
376 m_index_loc (strip_subreg (ad->index_term)),
377 m_index_reg (NULL_RTX)
379 if (m_base_loc != NULL)
381 m_base_reg = *m_base_loc;
382 lra_eliminate_reg_if_possible (m_base_loc);
383 if (m_ad->base_term2 != NULL)
384 *m_ad->base_term2 = *m_ad->base_term;
386 if (m_index_loc != NULL)
388 m_index_reg = *m_index_loc;
389 lra_eliminate_reg_if_possible (m_index_loc);
393 address_eliminator::~address_eliminator ()
395 if (m_base_loc && *m_base_loc != m_base_reg)
397 *m_base_loc = m_base_reg;
398 if (m_ad->base_term2 != NULL)
399 *m_ad->base_term2 = *m_ad->base_term;
401 if (m_index_loc && *m_index_loc != m_index_reg)
402 *m_index_loc = m_index_reg;
405 /* Return true if the eliminated form of AD is a legitimate target address. */
406 static bool
407 valid_address_p (struct address_info *ad)
409 address_eliminator eliminator (ad);
410 return valid_address_p (ad->mode, *ad->outer, ad->as);
413 /* Return true if the eliminated form of memory reference OP satisfies
414 extra memory constraint CONSTRAINT. */
415 static bool
416 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
418 struct address_info ad;
420 decompose_mem_address (&ad, op);
421 address_eliminator eliminator (&ad);
422 return constraint_satisfied_p (op, constraint);
425 /* Return true if the eliminated form of address AD satisfies extra
426 address constraint CONSTRAINT. */
427 static bool
428 satisfies_address_constraint_p (struct address_info *ad,
429 enum constraint_num constraint)
431 address_eliminator eliminator (ad);
432 return constraint_satisfied_p (*ad->outer, constraint);
435 /* Return true if the eliminated form of address OP satisfies extra
436 address constraint CONSTRAINT. */
437 static bool
438 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
440 struct address_info ad;
442 decompose_lea_address (&ad, &op);
443 return satisfies_address_constraint_p (&ad, constraint);
446 /* Initiate equivalences for LRA. As we keep original equivalences
447 before any elimination, we need to make copies otherwise any change
448 in insns might change the equivalences. */
449 void
450 lra_init_equiv (void)
452 ira_expand_reg_equiv ();
453 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
455 rtx res;
457 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
458 ira_reg_equiv[i].memory = copy_rtx (res);
459 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
460 ira_reg_equiv[i].invariant = copy_rtx (res);
464 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
466 /* Update equivalence for REGNO. We need to this as the equivalence
467 might contain other pseudos which are changed by their
468 equivalences. */
469 static void
470 update_equiv (int regno)
472 rtx x;
474 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
475 ira_reg_equiv[regno].memory
476 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
477 NULL_RTX);
478 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
479 ira_reg_equiv[regno].invariant
480 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
481 NULL_RTX);
484 /* If we have decided to substitute X with another value, return that
485 value, otherwise return X. */
486 static rtx
487 get_equiv (rtx x)
489 int regno;
490 rtx res;
492 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
493 || ! ira_reg_equiv[regno].defined_p
494 || ! ira_reg_equiv[regno].profitable_p
495 || lra_get_regno_hard_regno (regno) >= 0)
496 return x;
497 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
499 if (targetm.cannot_substitute_mem_equiv_p (res))
500 return x;
501 return res;
503 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
504 return res;
505 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
506 return res;
507 gcc_unreachable ();
510 /* If we have decided to substitute X with the equivalent value,
511 return that value after elimination for INSN, otherwise return
512 X. */
513 static rtx
514 get_equiv_with_elimination (rtx x, rtx_insn *insn)
516 rtx res = get_equiv (x);
518 if (x == res || CONSTANT_P (res))
519 return res;
520 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
521 false, false, 0, true);
524 /* Set up curr_operand_mode. */
525 static void
526 init_curr_operand_mode (void)
528 int nop = curr_static_id->n_operands;
529 for (int i = 0; i < nop; i++)
531 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
532 if (mode == VOIDmode)
534 /* The .md mode for address operands is the mode of the
535 addressed value rather than the mode of the address itself. */
536 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
537 mode = Pmode;
538 else
539 mode = curr_static_id->operand[i].mode;
541 curr_operand_mode[i] = mode;
547 /* The page contains code to reuse input reloads. */
549 /* Structure describes input reload of the current insns. */
550 struct input_reload
552 /* Reloaded value. */
553 rtx input;
554 /* Reload pseudo used. */
555 rtx reg;
558 /* The number of elements in the following array. */
559 static int curr_insn_input_reloads_num;
560 /* Array containing info about input reloads. It is used to find the
561 same input reload and reuse the reload pseudo in this case. */
562 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
564 /* Initiate data concerning reuse of input reloads for the current
565 insn. */
566 static void
567 init_curr_insn_input_reloads (void)
569 curr_insn_input_reloads_num = 0;
572 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
573 created input reload pseudo (only if TYPE is not OP_OUT). Don't
574 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
575 wrapped up in SUBREG. The result pseudo is returned through
576 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
577 reused the already created input reload pseudo. Use TITLE to
578 describe new registers for debug purposes. */
579 static bool
580 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
581 enum reg_class rclass, bool in_subreg_p,
582 const char *title, rtx *result_reg)
584 int i, regno;
585 enum reg_class new_class;
587 if (type == OP_OUT)
589 *result_reg
590 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
591 return true;
593 /* Prevent reuse value of expression with side effects,
594 e.g. volatile memory. */
595 if (! side_effects_p (original))
596 for (i = 0; i < curr_insn_input_reloads_num; i++)
597 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
598 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
600 rtx reg = curr_insn_input_reloads[i].reg;
601 regno = REGNO (reg);
602 /* If input is equal to original and both are VOIDmode,
603 GET_MODE (reg) might be still different from mode.
604 Ensure we don't return *result_reg with wrong mode. */
605 if (GET_MODE (reg) != mode)
607 if (in_subreg_p)
608 continue;
609 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
610 continue;
611 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
612 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
613 continue;
615 *result_reg = reg;
616 if (lra_dump_file != NULL)
618 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
619 dump_value_slim (lra_dump_file, original, 1);
621 if (new_class != lra_get_allocno_class (regno))
622 lra_change_class (regno, new_class, ", change to", false);
623 if (lra_dump_file != NULL)
624 fprintf (lra_dump_file, "\n");
625 return false;
627 *result_reg = lra_create_new_reg (mode, original, rclass, title);
628 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
629 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
630 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
631 return true;
636 /* The page contains code to extract memory address parts. */
638 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
639 static inline bool
640 ok_for_index_p_nonstrict (rtx reg)
642 unsigned regno = REGNO (reg);
644 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
647 /* A version of regno_ok_for_base_p for use here, when all pseudos
648 should count as OK. Arguments as for regno_ok_for_base_p. */
649 static inline bool
650 ok_for_base_p_nonstrict (rtx reg, machine_mode mode, addr_space_t as,
651 enum rtx_code outer_code, enum rtx_code index_code)
653 unsigned regno = REGNO (reg);
655 if (regno >= FIRST_PSEUDO_REGISTER)
656 return true;
657 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
662 /* The page contains major code to choose the current insn alternative
663 and generate reloads for it. */
665 /* Return the offset from REGNO of the least significant register
666 in (reg:MODE REGNO).
668 This function is used to tell whether two registers satisfy
669 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
671 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
672 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
674 lra_constraint_offset (int regno, machine_mode mode)
676 lra_assert (regno < FIRST_PSEUDO_REGISTER);
677 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
678 && SCALAR_INT_MODE_P (mode))
679 return hard_regno_nregs[regno][mode] - 1;
680 return 0;
683 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
684 if they are the same hard reg, and has special hacks for
685 auto-increment and auto-decrement. This is specifically intended for
686 process_alt_operands to use in determining whether two operands
687 match. X is the operand whose number is the lower of the two.
689 It is supposed that X is the output operand and Y is the input
690 operand. Y_HARD_REGNO is the final hard regno of register Y or
691 register in subreg Y as we know it now. Otherwise, it is a
692 negative value. */
693 static bool
694 operands_match_p (rtx x, rtx y, int y_hard_regno)
696 int i;
697 RTX_CODE code = GET_CODE (x);
698 const char *fmt;
700 if (x == y)
701 return true;
702 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
703 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
705 int j;
707 i = get_hard_regno (x);
708 if (i < 0)
709 goto slow;
711 if ((j = y_hard_regno) < 0)
712 goto slow;
714 i += lra_constraint_offset (i, GET_MODE (x));
715 j += lra_constraint_offset (j, GET_MODE (y));
717 return i == j;
720 /* If two operands must match, because they are really a single
721 operand of an assembler insn, then two post-increments are invalid
722 because the assembler insn would increment only once. On the
723 other hand, a post-increment matches ordinary indexing if the
724 post-increment is the output operand. */
725 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
726 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
728 /* Two pre-increments are invalid because the assembler insn would
729 increment only once. On the other hand, a pre-increment matches
730 ordinary indexing if the pre-increment is the input operand. */
731 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
732 || GET_CODE (y) == PRE_MODIFY)
733 return operands_match_p (x, XEXP (y, 0), -1);
735 slow:
737 if (code == REG && REG_P (y))
738 return REGNO (x) == REGNO (y);
740 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
741 && x == SUBREG_REG (y))
742 return true;
743 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
744 && SUBREG_REG (x) == y)
745 return true;
747 /* Now we have disposed of all the cases in which different rtx
748 codes can match. */
749 if (code != GET_CODE (y))
750 return false;
752 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
753 if (GET_MODE (x) != GET_MODE (y))
754 return false;
756 switch (code)
758 CASE_CONST_UNIQUE:
759 return false;
761 case LABEL_REF:
762 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
763 case SYMBOL_REF:
764 return XSTR (x, 0) == XSTR (y, 0);
766 default:
767 break;
770 /* Compare the elements. If any pair of corresponding elements fail
771 to match, return false for the whole things. */
773 fmt = GET_RTX_FORMAT (code);
774 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
776 int val, j;
777 switch (fmt[i])
779 case 'w':
780 if (XWINT (x, i) != XWINT (y, i))
781 return false;
782 break;
784 case 'i':
785 if (XINT (x, i) != XINT (y, i))
786 return false;
787 break;
789 case 'e':
790 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
791 if (val == 0)
792 return false;
793 break;
795 case '0':
796 break;
798 case 'E':
799 if (XVECLEN (x, i) != XVECLEN (y, i))
800 return false;
801 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
803 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
804 if (val == 0)
805 return false;
807 break;
809 /* It is believed that rtx's at this level will never
810 contain anything but integers and other rtx's, except for
811 within LABEL_REFs and SYMBOL_REFs. */
812 default:
813 gcc_unreachable ();
816 return true;
819 /* True if X is a constant that can be forced into the constant pool.
820 MODE is the mode of the operand, or VOIDmode if not known. */
821 #define CONST_POOL_OK_P(MODE, X) \
822 ((MODE) != VOIDmode \
823 && CONSTANT_P (X) \
824 && GET_CODE (X) != HIGH \
825 && !targetm.cannot_force_const_mem (MODE, X))
827 /* True if C is a non-empty register class that has too few registers
828 to be safely used as a reload target class. */
829 #define SMALL_REGISTER_CLASS_P(C) \
830 (ira_class_hard_regs_num [(C)] == 1 \
831 || (ira_class_hard_regs_num [(C)] >= 1 \
832 && targetm.class_likely_spilled_p (C)))
834 /* If REG is a reload pseudo, try to make its class satisfying CL. */
835 static void
836 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
838 enum reg_class rclass;
840 /* Do not make more accurate class from reloads generated. They are
841 mostly moves with a lot of constraints. Making more accurate
842 class may results in very narrow class and impossibility of find
843 registers for several reloads of one insn. */
844 if (INSN_UID (curr_insn) >= new_insn_uid_start)
845 return;
846 if (GET_CODE (reg) == SUBREG)
847 reg = SUBREG_REG (reg);
848 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
849 return;
850 if (in_class_p (reg, cl, &rclass) && rclass != cl)
851 lra_change_class (REGNO (reg), rclass, " Change to", true);
854 /* Generate reloads for matching OUT and INS (array of input operand
855 numbers with end marker -1) with reg class GOAL_CLASS. Add input
856 and output reloads correspondingly to the lists *BEFORE and *AFTER.
857 OUT might be negative. In this case we generate input reloads for
858 matched input operands INS. */
859 static void
860 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
861 rtx_insn **before, rtx_insn **after)
863 int i, in;
864 rtx new_in_reg, new_out_reg, reg;
865 machine_mode inmode, outmode;
866 rtx in_rtx = *curr_id->operand_loc[ins[0]];
867 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
869 inmode = curr_operand_mode[ins[0]];
870 outmode = out < 0 ? inmode : curr_operand_mode[out];
871 push_to_sequence (*before);
872 if (inmode != outmode)
874 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
876 reg = new_in_reg
877 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
878 goal_class, "");
879 if (SCALAR_INT_MODE_P (inmode))
880 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
881 else
882 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
883 LRA_SUBREG_P (new_out_reg) = 1;
884 /* If the input reg is dying here, we can use the same hard
885 register for REG and IN_RTX. We do it only for original
886 pseudos as reload pseudos can die although original
887 pseudos still live where reload pseudos dies. */
888 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
889 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
890 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
892 else
894 reg = new_out_reg
895 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
896 goal_class, "");
897 if (SCALAR_INT_MODE_P (outmode))
898 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
899 else
900 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
901 /* NEW_IN_REG is non-paradoxical subreg. We don't want
902 NEW_OUT_REG living above. We add clobber clause for
903 this. This is just a temporary clobber. We can remove
904 it at the end of LRA work. */
905 rtx_insn *clobber = emit_clobber (new_out_reg);
906 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
907 LRA_SUBREG_P (new_in_reg) = 1;
908 if (GET_CODE (in_rtx) == SUBREG)
910 rtx subreg_reg = SUBREG_REG (in_rtx);
912 /* If SUBREG_REG is dying here and sub-registers IN_RTX
913 and NEW_IN_REG are similar, we can use the same hard
914 register for REG and SUBREG_REG. */
915 if (REG_P (subreg_reg)
916 && (int) REGNO (subreg_reg) < lra_new_regno_start
917 && GET_MODE (subreg_reg) == outmode
918 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
919 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
920 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
924 else
926 /* Pseudos have values -- see comments for lra_reg_info.
927 Different pseudos with the same value do not conflict even if
928 they live in the same place. When we create a pseudo we
929 assign value of original pseudo (if any) from which we
930 created the new pseudo. If we create the pseudo from the
931 input pseudo, the new pseudo will have no conflict with the
932 input pseudo which is wrong when the input pseudo lives after
933 the insn and as the new pseudo value is changed by the insn
934 output. Therefore we create the new pseudo from the output
935 except the case when we have single matched dying input
936 pseudo.
938 We cannot reuse the current output register because we might
939 have a situation like "a <- a op b", where the constraints
940 force the second input operand ("b") to match the output
941 operand ("a"). "b" must then be copied into a new register
942 so that it doesn't clobber the current value of "a". */
944 new_in_reg = new_out_reg
945 = (ins[1] < 0 && REG_P (in_rtx)
946 && (int) REGNO (in_rtx) < lra_new_regno_start
947 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
948 ? lra_create_new_reg (inmode, in_rtx, goal_class, "")
949 : lra_create_new_reg_with_unique_value (outmode, out_rtx,
950 goal_class, ""));
952 /* In operand can be got from transformations before processing insn
953 constraints. One example of such transformations is subreg
954 reloading (see function simplify_operand_subreg). The new
955 pseudos created by the transformations might have inaccurate
956 class (ALL_REGS) and we should make their classes more
957 accurate. */
958 narrow_reload_pseudo_class (in_rtx, goal_class);
959 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
960 *before = get_insns ();
961 end_sequence ();
962 for (i = 0; (in = ins[i]) >= 0; i++)
964 lra_assert
965 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
966 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
967 *curr_id->operand_loc[in] = new_in_reg;
969 lra_update_dups (curr_id, ins);
970 if (out < 0)
971 return;
972 /* See a comment for the input operand above. */
973 narrow_reload_pseudo_class (out_rtx, goal_class);
974 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
976 start_sequence ();
977 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
978 emit_insn (*after);
979 *after = get_insns ();
980 end_sequence ();
982 *curr_id->operand_loc[out] = new_out_reg;
983 lra_update_dup (curr_id, out);
986 /* Return register class which is union of all reg classes in insn
987 constraint alternative string starting with P. */
988 static enum reg_class
989 reg_class_from_constraints (const char *p)
991 int c, len;
992 enum reg_class op_class = NO_REGS;
995 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
997 case '#':
998 case ',':
999 return op_class;
1001 case 'g':
1002 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1003 break;
1005 default:
1006 enum constraint_num cn = lookup_constraint (p);
1007 enum reg_class cl = reg_class_for_constraint (cn);
1008 if (cl == NO_REGS)
1010 if (insn_extra_address_constraint (cn))
1011 op_class
1012 = (reg_class_subunion
1013 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1014 ADDRESS, SCRATCH)]);
1015 break;
1018 op_class = reg_class_subunion[op_class][cl];
1019 break;
1021 while ((p += len), c);
1022 return op_class;
1025 /* If OP is a register, return the class of the register as per
1026 get_reg_class, otherwise return NO_REGS. */
1027 static inline enum reg_class
1028 get_op_class (rtx op)
1030 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1033 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1034 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1035 SUBREG for VAL to make them equal. */
1036 static rtx_insn *
1037 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1039 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1041 /* Usually size of mem_pseudo is greater than val size but in
1042 rare cases it can be less as it can be defined by target
1043 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1044 if (! MEM_P (val))
1046 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1047 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1049 LRA_SUBREG_P (val) = 1;
1051 else
1053 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1054 LRA_SUBREG_P (mem_pseudo) = 1;
1057 return to_p ? gen_move_insn (mem_pseudo, val)
1058 : gen_move_insn (val, mem_pseudo);
1061 /* Process a special case insn (register move), return true if we
1062 don't need to process it anymore. INSN should be a single set
1063 insn. Set up that RTL was changed through CHANGE_P and macro
1064 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1065 SEC_MEM_P. */
1066 static bool
1067 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1069 int sregno, dregno;
1070 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1071 rtx_insn *before;
1072 enum reg_class dclass, sclass, secondary_class;
1073 secondary_reload_info sri;
1075 lra_assert (curr_insn_set != NULL_RTX);
1076 dreg = dest = SET_DEST (curr_insn_set);
1077 sreg = src = SET_SRC (curr_insn_set);
1078 if (GET_CODE (dest) == SUBREG)
1079 dreg = SUBREG_REG (dest);
1080 if (GET_CODE (src) == SUBREG)
1081 sreg = SUBREG_REG (src);
1082 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1083 return false;
1084 sclass = dclass = NO_REGS;
1085 if (REG_P (dreg))
1086 dclass = get_reg_class (REGNO (dreg));
1087 if (dclass == ALL_REGS)
1088 /* ALL_REGS is used for new pseudos created by transformations
1089 like reload of SUBREG_REG (see function
1090 simplify_operand_subreg). We don't know their class yet. We
1091 should figure out the class from processing the insn
1092 constraints not in this fast path function. Even if ALL_REGS
1093 were a right class for the pseudo, secondary_... hooks usually
1094 are not define for ALL_REGS. */
1095 return false;
1096 if (REG_P (sreg))
1097 sclass = get_reg_class (REGNO (sreg));
1098 if (sclass == ALL_REGS)
1099 /* See comments above. */
1100 return false;
1101 if (sclass == NO_REGS && dclass == NO_REGS)
1102 return false;
1103 #ifdef SECONDARY_MEMORY_NEEDED
1104 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1105 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1106 && ((sclass != NO_REGS && dclass != NO_REGS)
1107 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1108 #endif
1111 *sec_mem_p = true;
1112 return false;
1114 #endif
1115 if (! REG_P (dreg) || ! REG_P (sreg))
1116 return false;
1117 sri.prev_sri = NULL;
1118 sri.icode = CODE_FOR_nothing;
1119 sri.extra_cost = 0;
1120 secondary_class = NO_REGS;
1121 /* Set up hard register for a reload pseudo for hook
1122 secondary_reload because some targets just ignore unassigned
1123 pseudos in the hook. */
1124 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1126 dregno = REGNO (dreg);
1127 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1129 else
1130 dregno = -1;
1131 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1133 sregno = REGNO (sreg);
1134 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1136 else
1137 sregno = -1;
1138 if (sclass != NO_REGS)
1139 secondary_class
1140 = (enum reg_class) targetm.secondary_reload (false, dest,
1141 (reg_class_t) sclass,
1142 GET_MODE (src), &sri);
1143 if (sclass == NO_REGS
1144 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1145 && dclass != NO_REGS))
1147 enum reg_class old_sclass = secondary_class;
1148 secondary_reload_info old_sri = sri;
1150 sri.prev_sri = NULL;
1151 sri.icode = CODE_FOR_nothing;
1152 sri.extra_cost = 0;
1153 secondary_class
1154 = (enum reg_class) targetm.secondary_reload (true, src,
1155 (reg_class_t) dclass,
1156 GET_MODE (src), &sri);
1157 /* Check the target hook consistency. */
1158 lra_assert
1159 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1160 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1161 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1163 if (sregno >= 0)
1164 reg_renumber [sregno] = -1;
1165 if (dregno >= 0)
1166 reg_renumber [dregno] = -1;
1167 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1168 return false;
1169 *change_p = true;
1170 new_reg = NULL_RTX;
1171 if (secondary_class != NO_REGS)
1172 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1173 secondary_class,
1174 "secondary");
1175 start_sequence ();
1176 if (sri.icode == CODE_FOR_nothing)
1177 lra_emit_move (new_reg, src);
1178 else
1180 enum reg_class scratch_class;
1182 scratch_class = (reg_class_from_constraints
1183 (insn_data[sri.icode].operand[2].constraint));
1184 scratch_reg = (lra_create_new_reg_with_unique_value
1185 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1186 scratch_class, "scratch"));
1187 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1188 src, scratch_reg));
1190 before = get_insns ();
1191 end_sequence ();
1192 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1193 if (new_reg != NULL_RTX)
1194 SET_SRC (curr_insn_set) = new_reg;
1195 else
1197 if (lra_dump_file != NULL)
1199 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1200 dump_insn_slim (lra_dump_file, curr_insn);
1202 lra_set_insn_deleted (curr_insn);
1203 return true;
1205 return false;
1208 /* The following data describe the result of process_alt_operands.
1209 The data are used in curr_insn_transform to generate reloads. */
1211 /* The chosen reg classes which should be used for the corresponding
1212 operands. */
1213 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1214 /* True if the operand should be the same as another operand and that
1215 other operand does not need a reload. */
1216 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1217 /* True if the operand does not need a reload. */
1218 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1219 /* True if the operand can be offsetable memory. */
1220 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1221 /* The number of an operand to which given operand can be matched to. */
1222 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1223 /* The number of elements in the following array. */
1224 static int goal_alt_dont_inherit_ops_num;
1225 /* Numbers of operands whose reload pseudos should not be inherited. */
1226 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1227 /* True if the insn commutative operands should be swapped. */
1228 static bool goal_alt_swapped;
1229 /* The chosen insn alternative. */
1230 static int goal_alt_number;
1232 /* The following five variables are used to choose the best insn
1233 alternative. They reflect final characteristics of the best
1234 alternative. */
1236 /* Number of necessary reloads and overall cost reflecting the
1237 previous value and other unpleasantness of the best alternative. */
1238 static int best_losers, best_overall;
1239 /* Overall number hard registers used for reloads. For example, on
1240 some targets we need 2 general registers to reload DFmode and only
1241 one floating point register. */
1242 static int best_reload_nregs;
1243 /* Overall number reflecting distances of previous reloading the same
1244 value. The distances are counted from the current BB start. It is
1245 used to improve inheritance chances. */
1246 static int best_reload_sum;
1248 /* True if the current insn should have no correspondingly input or
1249 output reloads. */
1250 static bool no_input_reloads_p, no_output_reloads_p;
1252 /* True if we swapped the commutative operands in the current
1253 insn. */
1254 static int curr_swapped;
1256 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1257 register of class CL. Add any input reloads to list BEFORE. AFTER
1258 is nonnull if *LOC is an automodified value; handle that case by
1259 adding the required output reloads to list AFTER. Return true if
1260 the RTL was changed.
1262 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1263 register. Return false if the address register is correct. */
1264 static bool
1265 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1266 enum reg_class cl)
1268 int regno;
1269 enum reg_class rclass, new_class;
1270 rtx reg;
1271 rtx new_reg;
1272 machine_mode mode;
1273 bool subreg_p, before_p = false;
1275 subreg_p = GET_CODE (*loc) == SUBREG;
1276 if (subreg_p)
1277 loc = &SUBREG_REG (*loc);
1278 reg = *loc;
1279 mode = GET_MODE (reg);
1280 if (! REG_P (reg))
1282 if (check_only_p)
1283 return true;
1284 /* Always reload memory in an address even if the target supports
1285 such addresses. */
1286 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1287 before_p = true;
1289 else
1291 regno = REGNO (reg);
1292 rclass = get_reg_class (regno);
1293 if (! check_only_p
1294 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1296 if (lra_dump_file != NULL)
1298 fprintf (lra_dump_file,
1299 "Changing pseudo %d in address of insn %u on equiv ",
1300 REGNO (reg), INSN_UID (curr_insn));
1301 dump_value_slim (lra_dump_file, *loc, 1);
1302 fprintf (lra_dump_file, "\n");
1304 *loc = copy_rtx (*loc);
1306 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1308 if (check_only_p)
1309 return true;
1310 reg = *loc;
1311 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1312 mode, reg, cl, subreg_p, "address", &new_reg))
1313 before_p = true;
1315 else if (new_class != NO_REGS && rclass != new_class)
1317 if (check_only_p)
1318 return true;
1319 lra_change_class (regno, new_class, " Change to", true);
1320 return false;
1322 else
1323 return false;
1325 if (before_p)
1327 push_to_sequence (*before);
1328 lra_emit_move (new_reg, reg);
1329 *before = get_insns ();
1330 end_sequence ();
1332 *loc = new_reg;
1333 if (after != NULL)
1335 start_sequence ();
1336 lra_emit_move (reg, new_reg);
1337 emit_insn (*after);
1338 *after = get_insns ();
1339 end_sequence ();
1341 return true;
1344 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1345 the insn to be inserted before curr insn. AFTER returns the
1346 the insn to be inserted after curr insn. ORIGREG and NEWREG
1347 are the original reg and new reg for reload. */
1348 static void
1349 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1350 rtx newreg)
1352 if (before)
1354 push_to_sequence (*before);
1355 lra_emit_move (newreg, origreg);
1356 *before = get_insns ();
1357 end_sequence ();
1359 if (after)
1361 start_sequence ();
1362 lra_emit_move (origreg, newreg);
1363 emit_insn (*after);
1364 *after = get_insns ();
1365 end_sequence ();
1369 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1371 /* Make reloads for subreg in operand NOP with internal subreg mode
1372 REG_MODE, add new reloads for further processing. Return true if
1373 any change was done. */
1374 static bool
1375 simplify_operand_subreg (int nop, machine_mode reg_mode)
1377 int hard_regno;
1378 rtx_insn *before, *after;
1379 machine_mode mode, innermode;
1380 rtx reg, new_reg;
1381 rtx operand = *curr_id->operand_loc[nop];
1382 enum reg_class regclass;
1383 enum op_type type;
1385 before = after = NULL;
1387 if (GET_CODE (operand) != SUBREG)
1388 return false;
1390 mode = GET_MODE (operand);
1391 reg = SUBREG_REG (operand);
1392 innermode = GET_MODE (reg);
1393 type = curr_static_id->operand[nop].type;
1394 /* If we change address for paradoxical subreg of memory, the
1395 address might violate the necessary alignment or the access might
1396 be slow. So take this into consideration. We should not worry
1397 about access beyond allocated memory for paradoxical memory
1398 subregs as we don't substitute such equiv memory (see processing
1399 equivalences in function lra_constraints) and because for spilled
1400 pseudos we allocate stack memory enough for the biggest
1401 corresponding paradoxical subreg. */
1402 if (MEM_P (reg)
1403 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1404 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1406 rtx subst, old = *curr_id->operand_loc[nop];
1408 alter_subreg (curr_id->operand_loc[nop], false);
1409 subst = *curr_id->operand_loc[nop];
1410 lra_assert (MEM_P (subst));
1411 if (! valid_address_p (innermode, XEXP (reg, 0),
1412 MEM_ADDR_SPACE (reg))
1413 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1414 MEM_ADDR_SPACE (subst)))
1415 return true;
1416 /* If the address was valid and became invalid, prefer to reload
1417 the memory. Typical case is when the index scale should
1418 correspond the memory. */
1419 *curr_id->operand_loc[nop] = old;
1421 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1423 alter_subreg (curr_id->operand_loc[nop], false);
1424 return true;
1426 else if (CONSTANT_P (reg))
1428 /* Try to simplify subreg of constant. It is usually result of
1429 equivalence substitution. */
1430 if (innermode == VOIDmode
1431 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1432 innermode = curr_static_id->operand[nop].mode;
1433 if ((new_reg = simplify_subreg (mode, reg, innermode,
1434 SUBREG_BYTE (operand))) != NULL_RTX)
1436 *curr_id->operand_loc[nop] = new_reg;
1437 return true;
1440 /* Put constant into memory when we have mixed modes. It generates
1441 a better code in most cases as it does not need a secondary
1442 reload memory. It also prevents LRA looping when LRA is using
1443 secondary reload memory again and again. */
1444 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1445 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1447 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1448 alter_subreg (curr_id->operand_loc[nop], false);
1449 return true;
1451 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1452 if there may be a problem accessing OPERAND in the outer
1453 mode. */
1454 if ((REG_P (reg)
1455 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1456 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1457 /* Don't reload paradoxical subregs because we could be looping
1458 having repeatedly final regno out of hard regs range. */
1459 && (hard_regno_nregs[hard_regno][innermode]
1460 >= hard_regno_nregs[hard_regno][mode])
1461 && simplify_subreg_regno (hard_regno, innermode,
1462 SUBREG_BYTE (operand), mode) < 0
1463 /* Don't reload subreg for matching reload. It is actually
1464 valid subreg in LRA. */
1465 && ! LRA_SUBREG_P (operand))
1466 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1468 enum reg_class rclass;
1470 if (REG_P (reg))
1471 /* There is a big probability that we will get the same class
1472 for the new pseudo and we will get the same insn which
1473 means infinite looping. So spill the new pseudo. */
1474 rclass = NO_REGS;
1475 else
1476 /* The class will be defined later in curr_insn_transform. */
1477 rclass
1478 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1480 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1481 rclass, TRUE, "subreg reg", &new_reg))
1483 bool insert_before, insert_after;
1484 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1486 insert_before = (type != OP_OUT
1487 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1488 insert_after = (type != OP_IN);
1489 insert_move_for_subreg (insert_before ? &before : NULL,
1490 insert_after ? &after : NULL,
1491 reg, new_reg);
1493 SUBREG_REG (operand) = new_reg;
1494 lra_process_new_insns (curr_insn, before, after,
1495 "Inserting subreg reload");
1496 return true;
1498 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1499 IRA allocates hardreg to the inner pseudo reg according to its mode
1500 instead of the outermode, so the size of the hardreg may not be enough
1501 to contain the outermode operand, in that case we may need to insert
1502 reload for the reg. For the following two types of paradoxical subreg,
1503 we need to insert reload:
1504 1. If the op_type is OP_IN, and the hardreg could not be paired with
1505 other hardreg to contain the outermode operand
1506 (checked by in_hard_reg_set_p), we need to insert the reload.
1507 2. If the op_type is OP_OUT or OP_INOUT.
1509 Here is a paradoxical subreg example showing how the reload is generated:
1511 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1512 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1514 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1515 here, if reg107 is assigned to hardreg R15, because R15 is the last
1516 hardreg, compiler cannot find another hardreg to pair with R15 to
1517 contain TImode data. So we insert a TImode reload reg180 for it.
1518 After reload is inserted:
1520 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1521 (reg:DI 107 [ __comp ])) -1
1522 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1523 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1525 Two reload hard registers will be allocated to reg180 to save TImode data
1526 in LRA_assign. */
1527 else if (REG_P (reg)
1528 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1529 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1530 && (hard_regno_nregs[hard_regno][innermode]
1531 < hard_regno_nregs[hard_regno][mode])
1532 && (regclass = lra_get_allocno_class (REGNO (reg)))
1533 && (type != OP_IN
1534 || !in_hard_reg_set_p (reg_class_contents[regclass],
1535 mode, hard_regno)))
1537 /* The class will be defined later in curr_insn_transform. */
1538 enum reg_class rclass
1539 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1541 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1542 rclass, TRUE, "paradoxical subreg", &new_reg))
1544 rtx subreg;
1545 bool insert_before, insert_after;
1547 PUT_MODE (new_reg, mode);
1548 subreg = simplify_gen_subreg (innermode, new_reg, mode, 0);
1549 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1551 insert_before = (type != OP_OUT);
1552 insert_after = (type != OP_IN);
1553 insert_move_for_subreg (insert_before ? &before : NULL,
1554 insert_after ? &after : NULL,
1555 reg, subreg);
1557 SUBREG_REG (operand) = new_reg;
1558 lra_process_new_insns (curr_insn, before, after,
1559 "Inserting paradoxical subreg reload");
1560 return true;
1562 return false;
1565 /* Return TRUE if X refers for a hard register from SET. */
1566 static bool
1567 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1569 int i, j, x_hard_regno;
1570 machine_mode mode;
1571 const char *fmt;
1572 enum rtx_code code;
1574 if (x == NULL_RTX)
1575 return false;
1576 code = GET_CODE (x);
1577 mode = GET_MODE (x);
1578 if (code == SUBREG)
1580 x = SUBREG_REG (x);
1581 code = GET_CODE (x);
1582 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1583 mode = GET_MODE (x);
1586 if (REG_P (x))
1588 x_hard_regno = get_hard_regno (x);
1589 return (x_hard_regno >= 0
1590 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1592 if (MEM_P (x))
1594 struct address_info ad;
1596 decompose_mem_address (&ad, x);
1597 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1598 return true;
1599 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1600 return true;
1602 fmt = GET_RTX_FORMAT (code);
1603 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1605 if (fmt[i] == 'e')
1607 if (uses_hard_regs_p (XEXP (x, i), set))
1608 return true;
1610 else if (fmt[i] == 'E')
1612 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1613 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1614 return true;
1617 return false;
1620 /* Return true if OP is a spilled pseudo. */
1621 static inline bool
1622 spilled_pseudo_p (rtx op)
1624 return (REG_P (op)
1625 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1628 /* Return true if X is a general constant. */
1629 static inline bool
1630 general_constant_p (rtx x)
1632 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1635 static bool
1636 reg_in_class_p (rtx reg, enum reg_class cl)
1638 if (cl == NO_REGS)
1639 return get_reg_class (REGNO (reg)) == NO_REGS;
1640 return in_class_p (reg, cl, NULL);
1643 /* Return true if SET of RCLASS contains no hard regs which can be
1644 used in MODE. */
1645 static bool
1646 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1647 HARD_REG_SET &set,
1648 enum machine_mode mode)
1650 HARD_REG_SET temp;
1652 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1653 COPY_HARD_REG_SET (temp, set);
1654 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1655 return (hard_reg_set_subset_p
1656 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1659 /* Major function to choose the current insn alternative and what
1660 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1661 negative we should consider only this alternative. Return false if
1662 we can not choose the alternative or find how to reload the
1663 operands. */
1664 static bool
1665 process_alt_operands (int only_alternative)
1667 bool ok_p = false;
1668 int nop, overall, nalt;
1669 int n_alternatives = curr_static_id->n_alternatives;
1670 int n_operands = curr_static_id->n_operands;
1671 /* LOSERS counts the operands that don't fit this alternative and
1672 would require loading. */
1673 int losers;
1674 /* REJECT is a count of how undesirable this alternative says it is
1675 if any reloading is required. If the alternative matches exactly
1676 then REJECT is ignored, but otherwise it gets this much counted
1677 against it in addition to the reloading needed. */
1678 int reject;
1679 int op_reject;
1680 /* The number of elements in the following array. */
1681 int early_clobbered_regs_num;
1682 /* Numbers of operands which are early clobber registers. */
1683 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1684 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1685 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1686 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1687 bool curr_alt_win[MAX_RECOG_OPERANDS];
1688 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1689 int curr_alt_matches[MAX_RECOG_OPERANDS];
1690 /* The number of elements in the following array. */
1691 int curr_alt_dont_inherit_ops_num;
1692 /* Numbers of operands whose reload pseudos should not be inherited. */
1693 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1694 rtx op;
1695 /* The register when the operand is a subreg of register, otherwise the
1696 operand itself. */
1697 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1698 /* The register if the operand is a register or subreg of register,
1699 otherwise NULL. */
1700 rtx operand_reg[MAX_RECOG_OPERANDS];
1701 int hard_regno[MAX_RECOG_OPERANDS];
1702 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1703 int reload_nregs, reload_sum;
1704 bool costly_p;
1705 enum reg_class cl;
1707 /* Calculate some data common for all alternatives to speed up the
1708 function. */
1709 for (nop = 0; nop < n_operands; nop++)
1711 rtx reg;
1713 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1714 /* The real hard regno of the operand after the allocation. */
1715 hard_regno[nop] = get_hard_regno (op);
1717 operand_reg[nop] = reg = op;
1718 biggest_mode[nop] = GET_MODE (op);
1719 if (GET_CODE (op) == SUBREG)
1721 operand_reg[nop] = reg = SUBREG_REG (op);
1722 if (GET_MODE_SIZE (biggest_mode[nop])
1723 < GET_MODE_SIZE (GET_MODE (reg)))
1724 biggest_mode[nop] = GET_MODE (reg);
1726 if (! REG_P (reg))
1727 operand_reg[nop] = NULL_RTX;
1728 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1729 || ((int) REGNO (reg)
1730 == lra_get_elimination_hard_regno (REGNO (reg))))
1731 no_subreg_reg_operand[nop] = reg;
1732 else
1733 operand_reg[nop] = no_subreg_reg_operand[nop]
1734 /* Just use natural mode for elimination result. It should
1735 be enough for extra constraints hooks. */
1736 = regno_reg_rtx[hard_regno[nop]];
1739 /* The constraints are made of several alternatives. Each operand's
1740 constraint looks like foo,bar,... with commas separating the
1741 alternatives. The first alternatives for all operands go
1742 together, the second alternatives go together, etc.
1744 First loop over alternatives. */
1745 alternative_mask preferred = curr_id->preferred_alternatives;
1746 if (only_alternative >= 0)
1747 preferred &= ALTERNATIVE_BIT (only_alternative);
1749 for (nalt = 0; nalt < n_alternatives; nalt++)
1751 /* Loop over operands for one constraint alternative. */
1752 if (!TEST_BIT (preferred, nalt))
1753 continue;
1755 overall = losers = reject = reload_nregs = reload_sum = 0;
1756 for (nop = 0; nop < n_operands; nop++)
1758 int inc = (curr_static_id
1759 ->operand_alternative[nalt * n_operands + nop].reject);
1760 if (lra_dump_file != NULL && inc != 0)
1761 fprintf (lra_dump_file,
1762 " Staticly defined alt reject+=%d\n", inc);
1763 reject += inc;
1765 early_clobbered_regs_num = 0;
1767 for (nop = 0; nop < n_operands; nop++)
1769 const char *p;
1770 char *end;
1771 int len, c, m, i, opalt_num, this_alternative_matches;
1772 bool win, did_match, offmemok, early_clobber_p;
1773 /* false => this operand can be reloaded somehow for this
1774 alternative. */
1775 bool badop;
1776 /* true => this operand can be reloaded if the alternative
1777 allows regs. */
1778 bool winreg;
1779 /* True if a constant forced into memory would be OK for
1780 this operand. */
1781 bool constmemok;
1782 enum reg_class this_alternative, this_costly_alternative;
1783 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1784 bool this_alternative_match_win, this_alternative_win;
1785 bool this_alternative_offmemok;
1786 bool scratch_p;
1787 machine_mode mode;
1788 enum constraint_num cn;
1790 opalt_num = nalt * n_operands + nop;
1791 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1793 /* Fast track for no constraints at all. */
1794 curr_alt[nop] = NO_REGS;
1795 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1796 curr_alt_win[nop] = true;
1797 curr_alt_match_win[nop] = false;
1798 curr_alt_offmemok[nop] = false;
1799 curr_alt_matches[nop] = -1;
1800 continue;
1803 op = no_subreg_reg_operand[nop];
1804 mode = curr_operand_mode[nop];
1806 win = did_match = winreg = offmemok = constmemok = false;
1807 badop = true;
1809 early_clobber_p = false;
1810 p = curr_static_id->operand_alternative[opalt_num].constraint;
1812 this_costly_alternative = this_alternative = NO_REGS;
1813 /* We update set of possible hard regs besides its class
1814 because reg class might be inaccurate. For example,
1815 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1816 is translated in HI_REGS because classes are merged by
1817 pairs and there is no accurate intermediate class. */
1818 CLEAR_HARD_REG_SET (this_alternative_set);
1819 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1820 this_alternative_win = false;
1821 this_alternative_match_win = false;
1822 this_alternative_offmemok = false;
1823 this_alternative_matches = -1;
1825 /* An empty constraint should be excluded by the fast
1826 track. */
1827 lra_assert (*p != 0 && *p != ',');
1829 op_reject = 0;
1830 /* Scan this alternative's specs for this operand; set WIN
1831 if the operand fits any letter in this alternative.
1832 Otherwise, clear BADOP if this operand could fit some
1833 letter after reloads, or set WINREG if this operand could
1834 fit after reloads provided the constraint allows some
1835 registers. */
1836 costly_p = false;
1839 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1841 case '\0':
1842 len = 0;
1843 break;
1844 case ',':
1845 c = '\0';
1846 break;
1848 case '&':
1849 early_clobber_p = true;
1850 break;
1852 case '$':
1853 op_reject += LRA_MAX_REJECT;
1854 break;
1855 case '^':
1856 op_reject += LRA_LOSER_COST_FACTOR;
1857 break;
1859 case '#':
1860 /* Ignore rest of this alternative. */
1861 c = '\0';
1862 break;
1864 case '0': case '1': case '2': case '3': case '4':
1865 case '5': case '6': case '7': case '8': case '9':
1867 int m_hregno;
1868 bool match_p;
1870 m = strtoul (p, &end, 10);
1871 p = end;
1872 len = 0;
1873 lra_assert (nop > m);
1875 this_alternative_matches = m;
1876 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1877 /* We are supposed to match a previous operand.
1878 If we do, we win if that one did. If we do
1879 not, count both of the operands as losers.
1880 (This is too conservative, since most of the
1881 time only a single reload insn will be needed
1882 to make the two operands win. As a result,
1883 this alternative may be rejected when it is
1884 actually desirable.) */
1885 match_p = false;
1886 if (operands_match_p (*curr_id->operand_loc[nop],
1887 *curr_id->operand_loc[m], m_hregno))
1889 /* We should reject matching of an early
1890 clobber operand if the matching operand is
1891 not dying in the insn. */
1892 if (! curr_static_id->operand[m].early_clobber
1893 || operand_reg[nop] == NULL_RTX
1894 || (find_regno_note (curr_insn, REG_DEAD,
1895 REGNO (op))
1896 || REGNO (op) == REGNO (operand_reg[m])))
1897 match_p = true;
1899 if (match_p)
1901 /* If we are matching a non-offsettable
1902 address where an offsettable address was
1903 expected, then we must reject this
1904 combination, because we can't reload
1905 it. */
1906 if (curr_alt_offmemok[m]
1907 && MEM_P (*curr_id->operand_loc[m])
1908 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1909 continue;
1911 else
1913 /* Operands don't match. Both operands must
1914 allow a reload register, otherwise we
1915 cannot make them match. */
1916 if (curr_alt[m] == NO_REGS)
1917 break;
1918 /* Retroactively mark the operand we had to
1919 match as a loser, if it wasn't already and
1920 it wasn't matched to a register constraint
1921 (e.g it might be matched by memory). */
1922 if (curr_alt_win[m]
1923 && (operand_reg[m] == NULL_RTX
1924 || hard_regno[m] < 0))
1926 losers++;
1927 reload_nregs
1928 += (ira_reg_class_max_nregs[curr_alt[m]]
1929 [GET_MODE (*curr_id->operand_loc[m])]);
1932 /* Prefer matching earlyclobber alternative as
1933 it results in less hard regs required for
1934 the insn than a non-matching earlyclobber
1935 alternative. */
1936 if (curr_static_id->operand[m].early_clobber)
1938 if (lra_dump_file != NULL)
1939 fprintf
1940 (lra_dump_file,
1941 " %d Matching earlyclobber alt:"
1942 " reject--\n",
1943 nop);
1944 reject--;
1946 /* Otherwise we prefer no matching
1947 alternatives because it gives more freedom
1948 in RA. */
1949 else if (operand_reg[nop] == NULL_RTX
1950 || (find_regno_note (curr_insn, REG_DEAD,
1951 REGNO (operand_reg[nop]))
1952 == NULL_RTX))
1954 if (lra_dump_file != NULL)
1955 fprintf
1956 (lra_dump_file,
1957 " %d Matching alt: reject+=2\n",
1958 nop);
1959 reject += 2;
1962 /* If we have to reload this operand and some
1963 previous operand also had to match the same
1964 thing as this operand, we don't know how to do
1965 that. */
1966 if (!match_p || !curr_alt_win[m])
1968 for (i = 0; i < nop; i++)
1969 if (curr_alt_matches[i] == m)
1970 break;
1971 if (i < nop)
1972 break;
1974 else
1975 did_match = true;
1977 /* This can be fixed with reloads if the operand
1978 we are supposed to match can be fixed with
1979 reloads. */
1980 badop = false;
1981 this_alternative = curr_alt[m];
1982 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1983 winreg = this_alternative != NO_REGS;
1984 break;
1987 case 'g':
1988 if (MEM_P (op)
1989 || general_constant_p (op)
1990 || spilled_pseudo_p (op))
1991 win = true;
1992 cl = GENERAL_REGS;
1993 goto reg;
1995 default:
1996 cn = lookup_constraint (p);
1997 switch (get_constraint_type (cn))
1999 case CT_REGISTER:
2000 cl = reg_class_for_constraint (cn);
2001 if (cl != NO_REGS)
2002 goto reg;
2003 break;
2005 case CT_CONST_INT:
2006 if (CONST_INT_P (op)
2007 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2008 win = true;
2009 break;
2011 case CT_MEMORY:
2012 if (MEM_P (op)
2013 && satisfies_memory_constraint_p (op, cn))
2014 win = true;
2015 else if (spilled_pseudo_p (op))
2016 win = true;
2018 /* If we didn't already win, we can reload constants
2019 via force_const_mem or put the pseudo value into
2020 memory, or make other memory by reloading the
2021 address like for 'o'. */
2022 if (CONST_POOL_OK_P (mode, op)
2023 || MEM_P (op) || REG_P (op))
2024 badop = false;
2025 constmemok = true;
2026 offmemok = true;
2027 break;
2029 case CT_ADDRESS:
2030 /* If we didn't already win, we can reload the address
2031 into a base register. */
2032 if (satisfies_address_constraint_p (op, cn))
2033 win = true;
2034 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2035 ADDRESS, SCRATCH);
2036 badop = false;
2037 goto reg;
2039 case CT_FIXED_FORM:
2040 if (constraint_satisfied_p (op, cn))
2041 win = true;
2042 break;
2044 break;
2046 reg:
2047 this_alternative = reg_class_subunion[this_alternative][cl];
2048 IOR_HARD_REG_SET (this_alternative_set,
2049 reg_class_contents[cl]);
2050 if (costly_p)
2052 this_costly_alternative
2053 = reg_class_subunion[this_costly_alternative][cl];
2054 IOR_HARD_REG_SET (this_costly_alternative_set,
2055 reg_class_contents[cl]);
2057 if (mode == BLKmode)
2058 break;
2059 winreg = true;
2060 if (REG_P (op))
2062 if (hard_regno[nop] >= 0
2063 && in_hard_reg_set_p (this_alternative_set,
2064 mode, hard_regno[nop]))
2065 win = true;
2066 else if (hard_regno[nop] < 0
2067 && in_class_p (op, this_alternative, NULL))
2068 win = true;
2070 break;
2072 if (c != ' ' && c != '\t')
2073 costly_p = c == '*';
2075 while ((p += len), c);
2077 scratch_p = (operand_reg[nop] != NULL_RTX
2078 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2079 /* Record which operands fit this alternative. */
2080 if (win)
2082 this_alternative_win = true;
2083 if (operand_reg[nop] != NULL_RTX)
2085 if (hard_regno[nop] >= 0)
2087 if (in_hard_reg_set_p (this_costly_alternative_set,
2088 mode, hard_regno[nop]))
2090 if (lra_dump_file != NULL)
2091 fprintf (lra_dump_file,
2092 " %d Costly set: reject++\n",
2093 nop);
2094 reject++;
2097 else
2099 /* Prefer won reg to spilled pseudo under other
2100 equal conditions for possibe inheritance. */
2101 if (! scratch_p)
2103 if (lra_dump_file != NULL)
2104 fprintf
2105 (lra_dump_file,
2106 " %d Non pseudo reload: reject++\n",
2107 nop);
2108 reject++;
2110 if (in_class_p (operand_reg[nop],
2111 this_costly_alternative, NULL))
2113 if (lra_dump_file != NULL)
2114 fprintf
2115 (lra_dump_file,
2116 " %d Non pseudo costly reload:"
2117 " reject++\n",
2118 nop);
2119 reject++;
2122 /* We simulate the behaviour of old reload here.
2123 Although scratches need hard registers and it
2124 might result in spilling other pseudos, no reload
2125 insns are generated for the scratches. So it
2126 might cost something but probably less than old
2127 reload pass believes. */
2128 if (scratch_p)
2130 if (lra_dump_file != NULL)
2131 fprintf (lra_dump_file,
2132 " %d Scratch win: reject+=2\n",
2133 nop);
2134 reject += 2;
2138 else if (did_match)
2139 this_alternative_match_win = true;
2140 else
2142 int const_to_mem = 0;
2143 bool no_regs_p;
2145 reject += op_reject;
2146 /* Never do output reload of stack pointer. It makes
2147 impossible to do elimination when SP is changed in
2148 RTL. */
2149 if (op == stack_pointer_rtx && ! frame_pointer_needed
2150 && curr_static_id->operand[nop].type != OP_IN)
2151 goto fail;
2153 /* If this alternative asks for a specific reg class, see if there
2154 is at least one allocatable register in that class. */
2155 no_regs_p
2156 = (this_alternative == NO_REGS
2157 || (hard_reg_set_subset_p
2158 (reg_class_contents[this_alternative],
2159 lra_no_alloc_regs)));
2161 /* For asms, verify that the class for this alternative is possible
2162 for the mode that is specified. */
2163 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2165 int i;
2166 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2167 if (HARD_REGNO_MODE_OK (i, mode)
2168 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2169 mode, i))
2170 break;
2171 if (i == FIRST_PSEUDO_REGISTER)
2172 winreg = false;
2175 /* If this operand accepts a register, and if the
2176 register class has at least one allocatable register,
2177 then this operand can be reloaded. */
2178 if (winreg && !no_regs_p)
2179 badop = false;
2181 if (badop)
2183 if (lra_dump_file != NULL)
2184 fprintf (lra_dump_file,
2185 " alt=%d: Bad operand -- refuse\n",
2186 nalt);
2187 goto fail;
2190 /* If not assigned pseudo has a class which a subset of
2191 required reg class, it is a less costly alternative
2192 as the pseudo still can get a hard reg of necessary
2193 class. */
2194 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2195 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2196 && ira_class_subset_p[this_alternative][cl])
2198 if (lra_dump_file != NULL)
2199 fprintf
2200 (lra_dump_file,
2201 " %d Super set class reg: reject-=3\n", nop);
2202 reject -= 3;
2205 this_alternative_offmemok = offmemok;
2206 if (this_costly_alternative != NO_REGS)
2208 if (lra_dump_file != NULL)
2209 fprintf (lra_dump_file,
2210 " %d Costly loser: reject++\n", nop);
2211 reject++;
2213 /* If the operand is dying, has a matching constraint,
2214 and satisfies constraints of the matched operand
2215 which failed to satisfy the own constraints, most probably
2216 the reload for this operand will be gone. */
2217 if (this_alternative_matches >= 0
2218 && !curr_alt_win[this_alternative_matches]
2219 && REG_P (op)
2220 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2221 && (hard_regno[nop] >= 0
2222 ? in_hard_reg_set_p (this_alternative_set,
2223 mode, hard_regno[nop])
2224 : in_class_p (op, this_alternative, NULL)))
2226 if (lra_dump_file != NULL)
2227 fprintf
2228 (lra_dump_file,
2229 " %d Dying matched operand reload: reject++\n",
2230 nop);
2231 reject++;
2233 else
2235 /* Strict_low_part requires to reload the register
2236 not the sub-register. In this case we should
2237 check that a final reload hard reg can hold the
2238 value mode. */
2239 if (curr_static_id->operand[nop].strict_low
2240 && REG_P (op)
2241 && hard_regno[nop] < 0
2242 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2243 && ira_class_hard_regs_num[this_alternative] > 0
2244 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2245 [this_alternative][0],
2246 GET_MODE
2247 (*curr_id->operand_loc[nop])))
2249 if (lra_dump_file != NULL)
2250 fprintf
2251 (lra_dump_file,
2252 " alt=%d: Strict low subreg reload -- refuse\n",
2253 nalt);
2254 goto fail;
2256 losers++;
2258 if (operand_reg[nop] != NULL_RTX
2259 /* Output operands and matched input operands are
2260 not inherited. The following conditions do not
2261 exactly describe the previous statement but they
2262 are pretty close. */
2263 && curr_static_id->operand[nop].type != OP_OUT
2264 && (this_alternative_matches < 0
2265 || curr_static_id->operand[nop].type != OP_IN))
2267 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2268 (operand_reg[nop])]
2269 .last_reload);
2271 /* The value of reload_sum has sense only if we
2272 process insns in their order. It happens only on
2273 the first constraints sub-pass when we do most of
2274 reload work. */
2275 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2276 reload_sum += last_reload - bb_reload_num;
2278 /* If this is a constant that is reloaded into the
2279 desired class by copying it to memory first, count
2280 that as another reload. This is consistent with
2281 other code and is required to avoid choosing another
2282 alternative when the constant is moved into memory.
2283 Note that the test here is precisely the same as in
2284 the code below that calls force_const_mem. */
2285 if (CONST_POOL_OK_P (mode, op)
2286 && ((targetm.preferred_reload_class
2287 (op, this_alternative) == NO_REGS)
2288 || no_input_reloads_p))
2290 const_to_mem = 1;
2291 if (! no_regs_p)
2292 losers++;
2295 /* Alternative loses if it requires a type of reload not
2296 permitted for this insn. We can always reload
2297 objects with a REG_UNUSED note. */
2298 if ((curr_static_id->operand[nop].type != OP_IN
2299 && no_output_reloads_p
2300 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2301 || (curr_static_id->operand[nop].type != OP_OUT
2302 && no_input_reloads_p && ! const_to_mem)
2303 || (this_alternative_matches >= 0
2304 && (no_input_reloads_p
2305 || (no_output_reloads_p
2306 && (curr_static_id->operand
2307 [this_alternative_matches].type != OP_IN)
2308 && ! find_reg_note (curr_insn, REG_UNUSED,
2309 no_subreg_reg_operand
2310 [this_alternative_matches])))))
2312 if (lra_dump_file != NULL)
2313 fprintf
2314 (lra_dump_file,
2315 " alt=%d: No input/otput reload -- refuse\n",
2316 nalt);
2317 goto fail;
2320 /* Alternative loses if it required class pseudo can not
2321 hold value of required mode. Such insns can be
2322 described by insn definitions with mode iterators. */
2323 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2324 && ! hard_reg_set_empty_p (this_alternative_set)
2325 /* It is common practice for constraints to use a
2326 class which does not have actually enough regs to
2327 hold the value (e.g. x86 AREG for mode requiring
2328 more one general reg). Therefore we have 2
2329 conditions to check that the reload pseudo can
2330 not hold the mode value. */
2331 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2332 [this_alternative][0],
2333 GET_MODE (*curr_id->operand_loc[nop]))
2334 /* The above condition is not enough as the first
2335 reg in ira_class_hard_regs can be not aligned for
2336 multi-words mode values. */
2337 && (prohibited_class_reg_set_mode_p
2338 (this_alternative, this_alternative_set,
2339 GET_MODE (*curr_id->operand_loc[nop]))))
2341 if (lra_dump_file != NULL)
2342 fprintf (lra_dump_file,
2343 " alt=%d: reload pseudo for op %d "
2344 " can not hold the mode value -- refuse\n",
2345 nalt, nop);
2346 goto fail;
2349 /* Check strong discouragement of reload of non-constant
2350 into class THIS_ALTERNATIVE. */
2351 if (! CONSTANT_P (op) && ! no_regs_p
2352 && (targetm.preferred_reload_class
2353 (op, this_alternative) == NO_REGS
2354 || (curr_static_id->operand[nop].type == OP_OUT
2355 && (targetm.preferred_output_reload_class
2356 (op, this_alternative) == NO_REGS))))
2358 if (lra_dump_file != NULL)
2359 fprintf (lra_dump_file,
2360 " %d Non-prefered reload: reject+=%d\n",
2361 nop, LRA_MAX_REJECT);
2362 reject += LRA_MAX_REJECT;
2365 if (! (MEM_P (op) && offmemok)
2366 && ! (const_to_mem && constmemok))
2368 /* We prefer to reload pseudos over reloading other
2369 things, since such reloads may be able to be
2370 eliminated later. So bump REJECT in other cases.
2371 Don't do this in the case where we are forcing a
2372 constant into memory and it will then win since
2373 we don't want to have a different alternative
2374 match then. */
2375 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2377 if (lra_dump_file != NULL)
2378 fprintf
2379 (lra_dump_file,
2380 " %d Non-pseudo reload: reject+=2\n",
2381 nop);
2382 reject += 2;
2385 if (! no_regs_p)
2386 reload_nregs
2387 += ira_reg_class_max_nregs[this_alternative][mode];
2389 if (SMALL_REGISTER_CLASS_P (this_alternative))
2391 if (lra_dump_file != NULL)
2392 fprintf
2393 (lra_dump_file,
2394 " %d Small class reload: reject+=%d\n",
2395 nop, LRA_LOSER_COST_FACTOR / 2);
2396 reject += LRA_LOSER_COST_FACTOR / 2;
2400 /* We are trying to spill pseudo into memory. It is
2401 usually more costly than moving to a hard register
2402 although it might takes the same number of
2403 reloads. */
2404 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2406 if (lra_dump_file != NULL)
2407 fprintf
2408 (lra_dump_file,
2409 " %d Spill pseudo into memory: reject+=3\n",
2410 nop);
2411 reject += 3;
2412 if (VECTOR_MODE_P (mode))
2414 /* Spilling vectors into memory is usually more
2415 costly as they contain big values. */
2416 if (lra_dump_file != NULL)
2417 fprintf
2418 (lra_dump_file,
2419 " %d Spill vector pseudo: reject+=2\n",
2420 nop);
2421 reject += 2;
2425 #ifdef SECONDARY_MEMORY_NEEDED
2426 /* If reload requires moving value through secondary
2427 memory, it will need one more insn at least. */
2428 if (this_alternative != NO_REGS
2429 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2430 && ((curr_static_id->operand[nop].type != OP_OUT
2431 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2432 GET_MODE (op)))
2433 || (curr_static_id->operand[nop].type != OP_IN
2434 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2435 GET_MODE (op)))))
2436 losers++;
2437 #endif
2438 /* Input reloads can be inherited more often than output
2439 reloads can be removed, so penalize output
2440 reloads. */
2441 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2443 if (lra_dump_file != NULL)
2444 fprintf
2445 (lra_dump_file,
2446 " %d Non input pseudo reload: reject++\n",
2447 nop);
2448 reject++;
2452 if (early_clobber_p && ! scratch_p)
2454 if (lra_dump_file != NULL)
2455 fprintf (lra_dump_file,
2456 " %d Early clobber: reject++\n", nop);
2457 reject++;
2459 /* ??? We check early clobbers after processing all operands
2460 (see loop below) and there we update the costs more.
2461 Should we update the cost (may be approximately) here
2462 because of early clobber register reloads or it is a rare
2463 or non-important thing to be worth to do it. */
2464 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2465 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2467 if (lra_dump_file != NULL)
2468 fprintf (lra_dump_file,
2469 " alt=%d,overall=%d,losers=%d -- refuse\n",
2470 nalt, overall, losers);
2471 goto fail;
2474 curr_alt[nop] = this_alternative;
2475 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2476 curr_alt_win[nop] = this_alternative_win;
2477 curr_alt_match_win[nop] = this_alternative_match_win;
2478 curr_alt_offmemok[nop] = this_alternative_offmemok;
2479 curr_alt_matches[nop] = this_alternative_matches;
2481 if (this_alternative_matches >= 0
2482 && !did_match && !this_alternative_win)
2483 curr_alt_win[this_alternative_matches] = false;
2485 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2486 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2488 if (curr_insn_set != NULL_RTX && n_operands == 2
2489 /* Prevent processing non-move insns. */
2490 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2491 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2492 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2493 && REG_P (no_subreg_reg_operand[0])
2494 && REG_P (no_subreg_reg_operand[1])
2495 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2496 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2497 || (! curr_alt_win[0] && curr_alt_win[1]
2498 && REG_P (no_subreg_reg_operand[1])
2499 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2500 || (curr_alt_win[0] && ! curr_alt_win[1]
2501 && REG_P (no_subreg_reg_operand[0])
2502 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2503 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2504 no_subreg_reg_operand[1])
2505 || (targetm.preferred_reload_class
2506 (no_subreg_reg_operand[1],
2507 (enum reg_class) curr_alt[1]) != NO_REGS))
2508 /* If it is a result of recent elimination in move
2509 insn we can transform it into an add still by
2510 using this alternative. */
2511 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2513 /* We have a move insn and a new reload insn will be similar
2514 to the current insn. We should avoid such situation as it
2515 results in LRA cycling. */
2516 overall += LRA_MAX_REJECT;
2518 ok_p = true;
2519 curr_alt_dont_inherit_ops_num = 0;
2520 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2522 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2523 HARD_REG_SET temp_set;
2525 i = early_clobbered_nops[nop];
2526 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2527 || hard_regno[i] < 0)
2528 continue;
2529 lra_assert (operand_reg[i] != NULL_RTX);
2530 clobbered_hard_regno = hard_regno[i];
2531 CLEAR_HARD_REG_SET (temp_set);
2532 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2533 first_conflict_j = last_conflict_j = -1;
2534 for (j = 0; j < n_operands; j++)
2535 if (j == i
2536 /* We don't want process insides of match_operator and
2537 match_parallel because otherwise we would process
2538 their operands once again generating a wrong
2539 code. */
2540 || curr_static_id->operand[j].is_operator)
2541 continue;
2542 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2543 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2544 continue;
2545 /* If we don't reload j-th operand, check conflicts. */
2546 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2547 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2549 if (first_conflict_j < 0)
2550 first_conflict_j = j;
2551 last_conflict_j = j;
2553 if (last_conflict_j < 0)
2554 continue;
2555 /* If earlyclobber operand conflicts with another
2556 non-matching operand which is actually the same register
2557 as the earlyclobber operand, it is better to reload the
2558 another operand as an operand matching the earlyclobber
2559 operand can be also the same. */
2560 if (first_conflict_j == last_conflict_j
2561 && operand_reg[last_conflict_j]
2562 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2563 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2565 curr_alt_win[last_conflict_j] = false;
2566 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2567 = last_conflict_j;
2568 losers++;
2569 /* Early clobber was already reflected in REJECT. */
2570 lra_assert (reject > 0);
2571 if (lra_dump_file != NULL)
2572 fprintf
2573 (lra_dump_file,
2574 " %d Conflict early clobber reload: reject--\n",
2576 reject--;
2577 overall += LRA_LOSER_COST_FACTOR - 1;
2579 else
2581 /* We need to reload early clobbered register and the
2582 matched registers. */
2583 for (j = 0; j < n_operands; j++)
2584 if (curr_alt_matches[j] == i)
2586 curr_alt_match_win[j] = false;
2587 losers++;
2588 overall += LRA_LOSER_COST_FACTOR;
2590 if (! curr_alt_match_win[i])
2591 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2592 else
2594 /* Remember pseudos used for match reloads are never
2595 inherited. */
2596 lra_assert (curr_alt_matches[i] >= 0);
2597 curr_alt_win[curr_alt_matches[i]] = false;
2599 curr_alt_win[i] = curr_alt_match_win[i] = false;
2600 losers++;
2601 /* Early clobber was already reflected in REJECT. */
2602 lra_assert (reject > 0);
2603 if (lra_dump_file != NULL)
2604 fprintf
2605 (lra_dump_file,
2606 " %d Matched conflict early clobber reloads:"
2607 "reject--\n",
2609 reject--;
2610 overall += LRA_LOSER_COST_FACTOR - 1;
2613 if (lra_dump_file != NULL)
2614 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2615 nalt, overall, losers, reload_nregs);
2617 /* If this alternative can be made to work by reloading, and it
2618 needs less reloading than the others checked so far, record
2619 it as the chosen goal for reloading. */
2620 if ((best_losers != 0 && losers == 0)
2621 || (((best_losers == 0 && losers == 0)
2622 || (best_losers != 0 && losers != 0))
2623 && (best_overall > overall
2624 || (best_overall == overall
2625 /* If the cost of the reloads is the same,
2626 prefer alternative which requires minimal
2627 number of reload regs. */
2628 && (reload_nregs < best_reload_nregs
2629 || (reload_nregs == best_reload_nregs
2630 && (best_reload_sum < reload_sum
2631 || (best_reload_sum == reload_sum
2632 && nalt < goal_alt_number))))))))
2634 for (nop = 0; nop < n_operands; nop++)
2636 goal_alt_win[nop] = curr_alt_win[nop];
2637 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2638 goal_alt_matches[nop] = curr_alt_matches[nop];
2639 goal_alt[nop] = curr_alt[nop];
2640 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2642 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2643 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2644 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2645 goal_alt_swapped = curr_swapped;
2646 best_overall = overall;
2647 best_losers = losers;
2648 best_reload_nregs = reload_nregs;
2649 best_reload_sum = reload_sum;
2650 goal_alt_number = nalt;
2652 if (losers == 0)
2653 /* Everything is satisfied. Do not process alternatives
2654 anymore. */
2655 break;
2656 fail:
2659 return ok_p;
2662 /* Make reload base reg from address AD. */
2663 static rtx
2664 base_to_reg (struct address_info *ad)
2666 enum reg_class cl;
2667 int code = -1;
2668 rtx new_inner = NULL_RTX;
2669 rtx new_reg = NULL_RTX;
2670 rtx_insn *insn;
2671 rtx_insn *last_insn = get_last_insn();
2673 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2674 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2675 get_index_code (ad));
2676 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2677 cl, "base");
2678 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2679 ad->disp_term == NULL
2680 ? gen_int_mode (0, ad->mode)
2681 : *ad->disp_term);
2682 if (!valid_address_p (ad->mode, new_inner, ad->as))
2683 return NULL_RTX;
2684 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base_term));
2685 code = recog_memoized (insn);
2686 if (code < 0)
2688 delete_insns_since (last_insn);
2689 return NULL_RTX;
2692 return new_inner;
2695 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2696 static rtx
2697 base_plus_disp_to_reg (struct address_info *ad)
2699 enum reg_class cl;
2700 rtx new_reg;
2702 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2703 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2704 get_index_code (ad));
2705 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2706 cl, "base + disp");
2707 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2708 return new_reg;
2711 /* Make reload of index part of address AD. Return the new
2712 pseudo. */
2713 static rtx
2714 index_part_to_reg (struct address_info *ad)
2716 rtx new_reg;
2718 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2719 INDEX_REG_CLASS, "index term");
2720 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2721 GEN_INT (get_index_scale (ad)), new_reg, 1);
2722 return new_reg;
2725 /* Return true if we can add a displacement to address AD, even if that
2726 makes the address invalid. The fix-up code requires any new address
2727 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2728 static bool
2729 can_add_disp_p (struct address_info *ad)
2731 return (!ad->autoinc_p
2732 && ad->segment == NULL
2733 && ad->base == ad->base_term
2734 && ad->disp == ad->disp_term);
2737 /* Make equiv substitution in address AD. Return true if a substitution
2738 was made. */
2739 static bool
2740 equiv_address_substitution (struct address_info *ad)
2742 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2743 HOST_WIDE_INT disp, scale;
2744 bool change_p;
2746 base_term = strip_subreg (ad->base_term);
2747 if (base_term == NULL)
2748 base_reg = new_base_reg = NULL_RTX;
2749 else
2751 base_reg = *base_term;
2752 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2754 index_term = strip_subreg (ad->index_term);
2755 if (index_term == NULL)
2756 index_reg = new_index_reg = NULL_RTX;
2757 else
2759 index_reg = *index_term;
2760 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2762 if (base_reg == new_base_reg && index_reg == new_index_reg)
2763 return false;
2764 disp = 0;
2765 change_p = false;
2766 if (lra_dump_file != NULL)
2768 fprintf (lra_dump_file, "Changing address in insn %d ",
2769 INSN_UID (curr_insn));
2770 dump_value_slim (lra_dump_file, *ad->outer, 1);
2772 if (base_reg != new_base_reg)
2774 if (REG_P (new_base_reg))
2776 *base_term = new_base_reg;
2777 change_p = true;
2779 else if (GET_CODE (new_base_reg) == PLUS
2780 && REG_P (XEXP (new_base_reg, 0))
2781 && CONST_INT_P (XEXP (new_base_reg, 1))
2782 && can_add_disp_p (ad))
2784 disp += INTVAL (XEXP (new_base_reg, 1));
2785 *base_term = XEXP (new_base_reg, 0);
2786 change_p = true;
2788 if (ad->base_term2 != NULL)
2789 *ad->base_term2 = *ad->base_term;
2791 if (index_reg != new_index_reg)
2793 if (REG_P (new_index_reg))
2795 *index_term = new_index_reg;
2796 change_p = true;
2798 else if (GET_CODE (new_index_reg) == PLUS
2799 && REG_P (XEXP (new_index_reg, 0))
2800 && CONST_INT_P (XEXP (new_index_reg, 1))
2801 && can_add_disp_p (ad)
2802 && (scale = get_index_scale (ad)))
2804 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2805 *index_term = XEXP (new_index_reg, 0);
2806 change_p = true;
2809 if (disp != 0)
2811 if (ad->disp != NULL)
2812 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2813 else
2815 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2816 update_address (ad);
2818 change_p = true;
2820 if (lra_dump_file != NULL)
2822 if (! change_p)
2823 fprintf (lra_dump_file, " -- no change\n");
2824 else
2826 fprintf (lra_dump_file, " on equiv ");
2827 dump_value_slim (lra_dump_file, *ad->outer, 1);
2828 fprintf (lra_dump_file, "\n");
2831 return change_p;
2834 /* Major function to make reloads for an address in operand NOP or
2835 check its correctness (If CHECK_ONLY_P is true). The supported
2836 cases are:
2838 1) an address that existed before LRA started, at which point it
2839 must have been valid. These addresses are subject to elimination
2840 and may have become invalid due to the elimination offset being out
2841 of range.
2843 2) an address created by forcing a constant to memory
2844 (force_const_to_mem). The initial form of these addresses might
2845 not be valid, and it is this function's job to make them valid.
2847 3) a frame address formed from a register and a (possibly zero)
2848 constant offset. As above, these addresses might not be valid and
2849 this function must make them so.
2851 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2852 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2853 address. Return true for any RTL change.
2855 The function is a helper function which does not produce all
2856 transformations (when CHECK_ONLY_P is false) which can be
2857 necessary. It does just basic steps. To do all necessary
2858 transformations use function process_address. */
2859 static bool
2860 process_address_1 (int nop, bool check_only_p,
2861 rtx_insn **before, rtx_insn **after)
2863 struct address_info ad;
2864 rtx new_reg;
2865 rtx op = *curr_id->operand_loc[nop];
2866 const char *constraint = curr_static_id->operand[nop].constraint;
2867 enum constraint_num cn = lookup_constraint (constraint);
2868 bool change_p = false;
2870 if (insn_extra_address_constraint (cn))
2871 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2872 else if (MEM_P (op))
2873 decompose_mem_address (&ad, op);
2874 else if (GET_CODE (op) == SUBREG
2875 && MEM_P (SUBREG_REG (op)))
2876 decompose_mem_address (&ad, SUBREG_REG (op));
2877 else
2878 return false;
2879 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
2880 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
2881 when INDEX_REG_CLASS is a single register class. */
2882 if (ad.base_term != NULL
2883 && ad.index_term != NULL
2884 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
2885 && REG_P (*ad.base_term)
2886 && REG_P (*ad.index_term)
2887 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
2888 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
2890 std::swap (ad.base, ad.index);
2891 std::swap (ad.base_term, ad.index_term);
2893 if (! check_only_p)
2894 change_p = equiv_address_substitution (&ad);
2895 if (ad.base_term != NULL
2896 && (process_addr_reg
2897 (ad.base_term, check_only_p, before,
2898 (ad.autoinc_p
2899 && !(REG_P (*ad.base_term)
2900 && find_regno_note (curr_insn, REG_DEAD,
2901 REGNO (*ad.base_term)) != NULL_RTX)
2902 ? after : NULL),
2903 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2904 get_index_code (&ad)))))
2906 change_p = true;
2907 if (ad.base_term2 != NULL)
2908 *ad.base_term2 = *ad.base_term;
2910 if (ad.index_term != NULL
2911 && process_addr_reg (ad.index_term, check_only_p,
2912 before, NULL, INDEX_REG_CLASS))
2913 change_p = true;
2915 /* Target hooks sometimes don't treat extra-constraint addresses as
2916 legitimate address_operands, so handle them specially. */
2917 if (insn_extra_address_constraint (cn)
2918 && satisfies_address_constraint_p (&ad, cn))
2919 return change_p;
2921 if (check_only_p)
2922 return change_p;
2924 /* There are three cases where the shape of *AD.INNER may now be invalid:
2926 1) the original address was valid, but either elimination or
2927 equiv_address_substitution was applied and that made
2928 the address invalid.
2930 2) the address is an invalid symbolic address created by
2931 force_const_to_mem.
2933 3) the address is a frame address with an invalid offset.
2935 4) the address is a frame address with an invalid base.
2937 All these cases involve a non-autoinc address, so there is no
2938 point revalidating other types. */
2939 if (ad.autoinc_p || valid_address_p (&ad))
2940 return change_p;
2942 /* Any index existed before LRA started, so we can assume that the
2943 presence and shape of the index is valid. */
2944 push_to_sequence (*before);
2945 lra_assert (ad.disp == ad.disp_term);
2946 if (ad.base == NULL)
2948 if (ad.index == NULL)
2950 int code = -1;
2951 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2952 SCRATCH, SCRATCH);
2953 rtx addr = *ad.inner;
2955 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2956 if (HAVE_lo_sum)
2958 rtx_insn *insn;
2959 rtx_insn *last = get_last_insn ();
2961 /* addr => lo_sum (new_base, addr), case (2) above. */
2962 insn = emit_insn (gen_rtx_SET
2963 (new_reg,
2964 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2965 code = recog_memoized (insn);
2966 if (code >= 0)
2968 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2969 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2971 /* Try to put lo_sum into register. */
2972 insn = emit_insn (gen_rtx_SET
2973 (new_reg,
2974 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2975 code = recog_memoized (insn);
2976 if (code >= 0)
2978 *ad.inner = new_reg;
2979 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2981 *ad.inner = addr;
2982 code = -1;
2988 if (code < 0)
2989 delete_insns_since (last);
2992 if (code < 0)
2994 /* addr => new_base, case (2) above. */
2995 lra_emit_move (new_reg, addr);
2996 *ad.inner = new_reg;
2999 else
3001 /* index * scale + disp => new base + index * scale,
3002 case (1) above. */
3003 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3004 GET_CODE (*ad.index));
3006 lra_assert (INDEX_REG_CLASS != NO_REGS);
3007 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3008 lra_emit_move (new_reg, *ad.disp);
3009 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3010 new_reg, *ad.index);
3013 else if (ad.index == NULL)
3015 int regno;
3016 enum reg_class cl;
3017 rtx set;
3018 rtx_insn *insns, *last_insn;
3019 /* Try to reload base into register only if the base is invalid
3020 for the address but with valid offset, case (4) above. */
3021 start_sequence ();
3022 new_reg = base_to_reg (&ad);
3024 /* base + disp => new base, cases (1) and (3) above. */
3025 /* Another option would be to reload the displacement into an
3026 index register. However, postreload has code to optimize
3027 address reloads that have the same base and different
3028 displacements, so reloading into an index register would
3029 not necessarily be a win. */
3030 if (new_reg == NULL_RTX)
3031 new_reg = base_plus_disp_to_reg (&ad);
3032 insns = get_insns ();
3033 last_insn = get_last_insn ();
3034 /* If we generated at least two insns, try last insn source as
3035 an address. If we succeed, we generate one less insn. */
3036 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3037 && GET_CODE (SET_SRC (set)) == PLUS
3038 && REG_P (XEXP (SET_SRC (set), 0))
3039 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3041 *ad.inner = SET_SRC (set);
3042 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3044 *ad.base_term = XEXP (SET_SRC (set), 0);
3045 *ad.disp_term = XEXP (SET_SRC (set), 1);
3046 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3047 get_index_code (&ad));
3048 regno = REGNO (*ad.base_term);
3049 if (regno >= FIRST_PSEUDO_REGISTER
3050 && cl != lra_get_allocno_class (regno))
3051 lra_change_class (regno, cl, " Change to", true);
3052 new_reg = SET_SRC (set);
3053 delete_insns_since (PREV_INSN (last_insn));
3056 /* Try if target can split displacement into legitimite new disp
3057 and offset. If it's the case, we replace the last insn with
3058 insns for base + offset => new_reg and set new_reg + new disp
3059 to *ad.inner. */
3060 last_insn = get_last_insn ();
3061 if ((set = single_set (last_insn)) != NULL_RTX
3062 && GET_CODE (SET_SRC (set)) == PLUS
3063 && REG_P (XEXP (SET_SRC (set), 0))
3064 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3065 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3067 rtx addend, disp = XEXP (SET_SRC (set), 1);
3068 if (targetm.legitimize_address_displacement (&disp, &addend,
3069 ad.mode))
3071 rtx_insn *new_insns;
3072 start_sequence ();
3073 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3074 new_insns = get_insns ();
3075 end_sequence ();
3076 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3077 delete_insns_since (PREV_INSN (last_insn));
3078 add_insn (new_insns);
3079 insns = get_insns ();
3082 end_sequence ();
3083 emit_insn (insns);
3084 *ad.inner = new_reg;
3086 else if (ad.disp_term != NULL)
3088 /* base + scale * index + disp => new base + scale * index,
3089 case (1) above. */
3090 new_reg = base_plus_disp_to_reg (&ad);
3091 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3092 new_reg, *ad.index);
3094 else if (get_index_scale (&ad) == 1)
3096 /* The last transformation to one reg will be made in
3097 curr_insn_transform function. */
3098 end_sequence ();
3099 return false;
3101 else
3103 /* base + scale * index => base + new_reg,
3104 case (1) above.
3105 Index part of address may become invalid. For example, we
3106 changed pseudo on the equivalent memory and a subreg of the
3107 pseudo onto the memory of different mode for which the scale is
3108 prohibitted. */
3109 new_reg = index_part_to_reg (&ad);
3110 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3111 *ad.base_term, new_reg);
3113 *before = get_insns ();
3114 end_sequence ();
3115 return true;
3118 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3119 Use process_address_1 as a helper function. Return true for any
3120 RTL changes.
3122 If CHECK_ONLY_P is true, just check address correctness. Return
3123 false if the address correct. */
3124 static bool
3125 process_address (int nop, bool check_only_p,
3126 rtx_insn **before, rtx_insn **after)
3128 bool res = false;
3130 while (process_address_1 (nop, check_only_p, before, after))
3132 if (check_only_p)
3133 return true;
3134 res = true;
3136 return res;
3139 /* Emit insns to reload VALUE into a new register. VALUE is an
3140 auto-increment or auto-decrement RTX whose operand is a register or
3141 memory location; so reloading involves incrementing that location.
3142 IN is either identical to VALUE, or some cheaper place to reload
3143 value being incremented/decremented from.
3145 INC_AMOUNT is the number to increment or decrement by (always
3146 positive and ignored for POST_MODIFY/PRE_MODIFY).
3148 Return pseudo containing the result. */
3149 static rtx
3150 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3152 /* REG or MEM to be copied and incremented. */
3153 rtx incloc = XEXP (value, 0);
3154 /* Nonzero if increment after copying. */
3155 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3156 || GET_CODE (value) == POST_MODIFY);
3157 rtx_insn *last;
3158 rtx inc;
3159 rtx_insn *add_insn;
3160 int code;
3161 rtx real_in = in == value ? incloc : in;
3162 rtx result;
3163 bool plus_p = true;
3165 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3167 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3168 || GET_CODE (XEXP (value, 1)) == MINUS);
3169 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3170 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3171 inc = XEXP (XEXP (value, 1), 1);
3173 else
3175 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3176 inc_amount = -inc_amount;
3178 inc = GEN_INT (inc_amount);
3181 if (! post && REG_P (incloc))
3182 result = incloc;
3183 else
3184 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3185 "INC/DEC result");
3187 if (real_in != result)
3189 /* First copy the location to the result register. */
3190 lra_assert (REG_P (result));
3191 emit_insn (gen_move_insn (result, real_in));
3194 /* We suppose that there are insns to add/sub with the constant
3195 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3196 old reload worked with this assumption. If the assumption
3197 becomes wrong, we should use approach in function
3198 base_plus_disp_to_reg. */
3199 if (in == value)
3201 /* See if we can directly increment INCLOC. */
3202 last = get_last_insn ();
3203 add_insn = emit_insn (plus_p
3204 ? gen_add2_insn (incloc, inc)
3205 : gen_sub2_insn (incloc, inc));
3207 code = recog_memoized (add_insn);
3208 if (code >= 0)
3210 if (! post && result != incloc)
3211 emit_insn (gen_move_insn (result, incloc));
3212 return result;
3214 delete_insns_since (last);
3217 /* If couldn't do the increment directly, must increment in RESULT.
3218 The way we do this depends on whether this is pre- or
3219 post-increment. For pre-increment, copy INCLOC to the reload
3220 register, increment it there, then save back. */
3221 if (! post)
3223 if (real_in != result)
3224 emit_insn (gen_move_insn (result, real_in));
3225 if (plus_p)
3226 emit_insn (gen_add2_insn (result, inc));
3227 else
3228 emit_insn (gen_sub2_insn (result, inc));
3229 if (result != incloc)
3230 emit_insn (gen_move_insn (incloc, result));
3232 else
3234 /* Post-increment.
3236 Because this might be a jump insn or a compare, and because
3237 RESULT may not be available after the insn in an input
3238 reload, we must do the incrementing before the insn being
3239 reloaded for.
3241 We have already copied IN to RESULT. Increment the copy in
3242 RESULT, save that back, then decrement RESULT so it has
3243 the original value. */
3244 if (plus_p)
3245 emit_insn (gen_add2_insn (result, inc));
3246 else
3247 emit_insn (gen_sub2_insn (result, inc));
3248 emit_insn (gen_move_insn (incloc, result));
3249 /* Restore non-modified value for the result. We prefer this
3250 way because it does not require an additional hard
3251 register. */
3252 if (plus_p)
3254 if (CONST_INT_P (inc))
3255 emit_insn (gen_add2_insn (result,
3256 gen_int_mode (-INTVAL (inc),
3257 GET_MODE (result))));
3258 else
3259 emit_insn (gen_sub2_insn (result, inc));
3261 else
3262 emit_insn (gen_add2_insn (result, inc));
3264 return result;
3267 /* Return true if the current move insn does not need processing as we
3268 already know that it satisfies its constraints. */
3269 static bool
3270 simple_move_p (void)
3272 rtx dest, src;
3273 enum reg_class dclass, sclass;
3275 lra_assert (curr_insn_set != NULL_RTX);
3276 dest = SET_DEST (curr_insn_set);
3277 src = SET_SRC (curr_insn_set);
3278 return ((dclass = get_op_class (dest)) != NO_REGS
3279 && (sclass = get_op_class (src)) != NO_REGS
3280 /* The backend guarantees that register moves of cost 2
3281 never need reloads. */
3282 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3285 /* Swap operands NOP and NOP + 1. */
3286 static inline void
3287 swap_operands (int nop)
3289 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3290 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3291 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3292 /* Swap the duplicates too. */
3293 lra_update_dup (curr_id, nop);
3294 lra_update_dup (curr_id, nop + 1);
3297 /* Main entry point of the constraint code: search the body of the
3298 current insn to choose the best alternative. It is mimicking insn
3299 alternative cost calculation model of former reload pass. That is
3300 because machine descriptions were written to use this model. This
3301 model can be changed in future. Make commutative operand exchange
3302 if it is chosen.
3304 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3305 constraints. Return true if any change happened during function
3306 call.
3308 If CHECK_ONLY_P is true then don't do any transformation. Just
3309 check that the insn satisfies all constraints. If the insn does
3310 not satisfy any constraint, return true. */
3311 static bool
3312 curr_insn_transform (bool check_only_p)
3314 int i, j, k;
3315 int n_operands;
3316 int n_alternatives;
3317 int commutative;
3318 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3319 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3320 rtx_insn *before, *after;
3321 bool alt_p = false;
3322 /* Flag that the insn has been changed through a transformation. */
3323 bool change_p;
3324 bool sec_mem_p;
3325 #ifdef SECONDARY_MEMORY_NEEDED
3326 bool use_sec_mem_p;
3327 #endif
3328 int max_regno_before;
3329 int reused_alternative_num;
3331 curr_insn_set = single_set (curr_insn);
3332 if (curr_insn_set != NULL_RTX && simple_move_p ())
3333 return false;
3335 no_input_reloads_p = no_output_reloads_p = false;
3336 goal_alt_number = -1;
3337 change_p = sec_mem_p = false;
3338 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3339 reloads; neither are insns that SET cc0. Insns that use CC0 are
3340 not allowed to have any input reloads. */
3341 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3342 no_output_reloads_p = true;
3344 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3345 no_input_reloads_p = true;
3346 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3347 no_output_reloads_p = true;
3349 n_operands = curr_static_id->n_operands;
3350 n_alternatives = curr_static_id->n_alternatives;
3352 /* Just return "no reloads" if insn has no operands with
3353 constraints. */
3354 if (n_operands == 0 || n_alternatives == 0)
3355 return false;
3357 max_regno_before = max_reg_num ();
3359 for (i = 0; i < n_operands; i++)
3361 goal_alt_matched[i][0] = -1;
3362 goal_alt_matches[i] = -1;
3365 commutative = curr_static_id->commutative;
3367 /* Now see what we need for pseudos that didn't get hard regs or got
3368 the wrong kind of hard reg. For this, we must consider all the
3369 operands together against the register constraints. */
3371 best_losers = best_overall = INT_MAX;
3372 best_reload_sum = 0;
3374 curr_swapped = false;
3375 goal_alt_swapped = false;
3377 if (! check_only_p)
3378 /* Make equivalence substitution and memory subreg elimination
3379 before address processing because an address legitimacy can
3380 depend on memory mode. */
3381 for (i = 0; i < n_operands; i++)
3383 rtx op = *curr_id->operand_loc[i];
3384 rtx subst, old = op;
3385 bool op_change_p = false;
3387 if (GET_CODE (old) == SUBREG)
3388 old = SUBREG_REG (old);
3389 subst = get_equiv_with_elimination (old, curr_insn);
3390 original_subreg_reg_mode[i] = VOIDmode;
3391 if (subst != old)
3393 subst = copy_rtx (subst);
3394 lra_assert (REG_P (old));
3395 if (GET_CODE (op) != SUBREG)
3396 *curr_id->operand_loc[i] = subst;
3397 else
3399 SUBREG_REG (op) = subst;
3400 if (GET_MODE (subst) == VOIDmode)
3401 original_subreg_reg_mode[i] = GET_MODE (old);
3403 if (lra_dump_file != NULL)
3405 fprintf (lra_dump_file,
3406 "Changing pseudo %d in operand %i of insn %u on equiv ",
3407 REGNO (old), i, INSN_UID (curr_insn));
3408 dump_value_slim (lra_dump_file, subst, 1);
3409 fprintf (lra_dump_file, "\n");
3411 op_change_p = change_p = true;
3413 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3415 change_p = true;
3416 lra_update_dup (curr_id, i);
3420 /* Reload address registers and displacements. We do it before
3421 finding an alternative because of memory constraints. */
3422 before = after = NULL;
3423 for (i = 0; i < n_operands; i++)
3424 if (! curr_static_id->operand[i].is_operator
3425 && process_address (i, check_only_p, &before, &after))
3427 if (check_only_p)
3428 return true;
3429 change_p = true;
3430 lra_update_dup (curr_id, i);
3433 if (change_p)
3434 /* If we've changed the instruction then any alternative that
3435 we chose previously may no longer be valid. */
3436 lra_set_used_insn_alternative (curr_insn, -1);
3438 if (! check_only_p && curr_insn_set != NULL_RTX
3439 && check_and_process_move (&change_p, &sec_mem_p))
3440 return change_p;
3442 try_swapped:
3444 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3445 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3446 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3447 reused_alternative_num, INSN_UID (curr_insn));
3449 if (process_alt_operands (reused_alternative_num))
3450 alt_p = true;
3452 if (check_only_p)
3453 return ! alt_p || best_losers != 0;
3455 /* If insn is commutative (it's safe to exchange a certain pair of
3456 operands) then we need to try each alternative twice, the second
3457 time matching those two operands as if we had exchanged them. To
3458 do this, really exchange them in operands.
3460 If we have just tried the alternatives the second time, return
3461 operands to normal and drop through. */
3463 if (reused_alternative_num < 0 && commutative >= 0)
3465 curr_swapped = !curr_swapped;
3466 if (curr_swapped)
3468 swap_operands (commutative);
3469 goto try_swapped;
3471 else
3472 swap_operands (commutative);
3475 if (! alt_p && ! sec_mem_p)
3477 /* No alternative works with reloads?? */
3478 if (INSN_CODE (curr_insn) >= 0)
3479 fatal_insn ("unable to generate reloads for:", curr_insn);
3480 error_for_asm (curr_insn,
3481 "inconsistent operand constraints in an %<asm%>");
3482 /* Avoid further trouble with this insn. */
3483 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3484 lra_invalidate_insn_data (curr_insn);
3485 return true;
3488 /* If the best alternative is with operands 1 and 2 swapped, swap
3489 them. Update the operand numbers of any reloads already
3490 pushed. */
3492 if (goal_alt_swapped)
3494 if (lra_dump_file != NULL)
3495 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3496 INSN_UID (curr_insn));
3498 /* Swap the duplicates too. */
3499 swap_operands (commutative);
3500 change_p = true;
3503 #ifdef SECONDARY_MEMORY_NEEDED
3504 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3505 too conservatively. So we use the secondary memory only if there
3506 is no any alternative without reloads. */
3507 use_sec_mem_p = false;
3508 if (! alt_p)
3509 use_sec_mem_p = true;
3510 else if (sec_mem_p)
3512 for (i = 0; i < n_operands; i++)
3513 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3514 break;
3515 use_sec_mem_p = i < n_operands;
3518 if (use_sec_mem_p)
3520 rtx new_reg, src, dest, rld;
3521 machine_mode sec_mode, rld_mode;
3523 lra_assert (sec_mem_p);
3524 lra_assert (curr_static_id->operand[0].type == OP_OUT
3525 && curr_static_id->operand[1].type == OP_IN);
3526 dest = *curr_id->operand_loc[0];
3527 src = *curr_id->operand_loc[1];
3528 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3529 ? dest : src);
3530 rld_mode = GET_MODE (rld);
3531 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3532 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3533 #else
3534 sec_mode = rld_mode;
3535 #endif
3536 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3537 NO_REGS, "secondary");
3538 /* If the mode is changed, it should be wider. */
3539 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3540 if (sec_mode != rld_mode)
3542 /* If the target says specifically to use another mode for
3543 secondary memory moves we can not reuse the original
3544 insn. */
3545 after = emit_spill_move (false, new_reg, dest);
3546 lra_process_new_insns (curr_insn, NULL, after,
3547 "Inserting the sec. move");
3548 /* We may have non null BEFORE here (e.g. after address
3549 processing. */
3550 push_to_sequence (before);
3551 before = emit_spill_move (true, new_reg, src);
3552 emit_insn (before);
3553 before = get_insns ();
3554 end_sequence ();
3555 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3556 lra_set_insn_deleted (curr_insn);
3558 else if (dest == rld)
3560 *curr_id->operand_loc[0] = new_reg;
3561 after = emit_spill_move (false, new_reg, dest);
3562 lra_process_new_insns (curr_insn, NULL, after,
3563 "Inserting the sec. move");
3565 else
3567 *curr_id->operand_loc[1] = new_reg;
3568 /* See comments above. */
3569 push_to_sequence (before);
3570 before = emit_spill_move (true, new_reg, src);
3571 emit_insn (before);
3572 before = get_insns ();
3573 end_sequence ();
3574 lra_process_new_insns (curr_insn, before, NULL,
3575 "Inserting the sec. move");
3577 lra_update_insn_regno_info (curr_insn);
3578 return true;
3580 #endif
3582 lra_assert (goal_alt_number >= 0);
3583 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3585 if (lra_dump_file != NULL)
3587 const char *p;
3589 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3590 goal_alt_number, INSN_UID (curr_insn));
3591 for (i = 0; i < n_operands; i++)
3593 p = (curr_static_id->operand_alternative
3594 [goal_alt_number * n_operands + i].constraint);
3595 if (*p == '\0')
3596 continue;
3597 fprintf (lra_dump_file, " (%d) ", i);
3598 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3599 fputc (*p, lra_dump_file);
3601 if (INSN_CODE (curr_insn) >= 0
3602 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3603 fprintf (lra_dump_file, " {%s}", p);
3604 if (curr_id->sp_offset != 0)
3605 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3606 curr_id->sp_offset);
3607 fprintf (lra_dump_file, "\n");
3610 /* Right now, for any pair of operands I and J that are required to
3611 match, with J < I, goal_alt_matches[I] is J. Add I to
3612 goal_alt_matched[J]. */
3614 for (i = 0; i < n_operands; i++)
3615 if ((j = goal_alt_matches[i]) >= 0)
3617 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3619 /* We allow matching one output operand and several input
3620 operands. */
3621 lra_assert (k == 0
3622 || (curr_static_id->operand[j].type == OP_OUT
3623 && curr_static_id->operand[i].type == OP_IN
3624 && (curr_static_id->operand
3625 [goal_alt_matched[j][0]].type == OP_IN)));
3626 goal_alt_matched[j][k] = i;
3627 goal_alt_matched[j][k + 1] = -1;
3630 for (i = 0; i < n_operands; i++)
3631 goal_alt_win[i] |= goal_alt_match_win[i];
3633 /* Any constants that aren't allowed and can't be reloaded into
3634 registers are here changed into memory references. */
3635 for (i = 0; i < n_operands; i++)
3636 if (goal_alt_win[i])
3638 int regno;
3639 enum reg_class new_class;
3640 rtx reg = *curr_id->operand_loc[i];
3642 if (GET_CODE (reg) == SUBREG)
3643 reg = SUBREG_REG (reg);
3645 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3647 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3649 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3651 lra_assert (ok_p);
3652 lra_change_class (regno, new_class, " Change to", true);
3656 else
3658 const char *constraint;
3659 char c;
3660 rtx op = *curr_id->operand_loc[i];
3661 rtx subreg = NULL_RTX;
3662 machine_mode mode = curr_operand_mode[i];
3664 if (GET_CODE (op) == SUBREG)
3666 subreg = op;
3667 op = SUBREG_REG (op);
3668 mode = GET_MODE (op);
3671 if (CONST_POOL_OK_P (mode, op)
3672 && ((targetm.preferred_reload_class
3673 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3674 || no_input_reloads_p))
3676 rtx tem = force_const_mem (mode, op);
3678 change_p = true;
3679 if (subreg != NULL_RTX)
3680 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3682 *curr_id->operand_loc[i] = tem;
3683 lra_update_dup (curr_id, i);
3684 process_address (i, false, &before, &after);
3686 /* If the alternative accepts constant pool refs directly
3687 there will be no reload needed at all. */
3688 if (subreg != NULL_RTX)
3689 continue;
3690 /* Skip alternatives before the one requested. */
3691 constraint = (curr_static_id->operand_alternative
3692 [goal_alt_number * n_operands + i].constraint);
3693 for (;
3694 (c = *constraint) && c != ',' && c != '#';
3695 constraint += CONSTRAINT_LEN (c, constraint))
3697 enum constraint_num cn = lookup_constraint (constraint);
3698 if (insn_extra_memory_constraint (cn)
3699 && satisfies_memory_constraint_p (tem, cn))
3700 break;
3702 if (c == '\0' || c == ',' || c == '#')
3703 continue;
3705 goal_alt_win[i] = true;
3709 for (i = 0; i < n_operands; i++)
3711 int regno;
3712 bool optional_p = false;
3713 rtx old, new_reg;
3714 rtx op = *curr_id->operand_loc[i];
3716 if (goal_alt_win[i])
3718 if (goal_alt[i] == NO_REGS
3719 && REG_P (op)
3720 /* When we assign NO_REGS it means that we will not
3721 assign a hard register to the scratch pseudo by
3722 assigment pass and the scratch pseudo will be
3723 spilled. Spilled scratch pseudos are transformed
3724 back to scratches at the LRA end. */
3725 && lra_former_scratch_operand_p (curr_insn, i))
3727 int regno = REGNO (op);
3728 lra_change_class (regno, NO_REGS, " Change to", true);
3729 if (lra_get_regno_hard_regno (regno) >= 0)
3730 /* We don't have to mark all insn affected by the
3731 spilled pseudo as there is only one such insn, the
3732 current one. */
3733 reg_renumber[regno] = -1;
3735 /* We can do an optional reload. If the pseudo got a hard
3736 reg, we might improve the code through inheritance. If
3737 it does not get a hard register we coalesce memory/memory
3738 moves later. Ignore move insns to avoid cycling. */
3739 if (! lra_simple_p
3740 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3741 && goal_alt[i] != NO_REGS && REG_P (op)
3742 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3743 && regno < new_regno_start
3744 && ! lra_former_scratch_p (regno)
3745 && reg_renumber[regno] < 0
3746 /* Check that the optional reload pseudo will be able to
3747 hold given mode value. */
3748 && ! (prohibited_class_reg_set_mode_p
3749 (goal_alt[i], reg_class_contents[goal_alt[i]],
3750 PSEUDO_REGNO_MODE (regno)))
3751 && (curr_insn_set == NULL_RTX
3752 || !((REG_P (SET_SRC (curr_insn_set))
3753 || MEM_P (SET_SRC (curr_insn_set))
3754 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3755 && (REG_P (SET_DEST (curr_insn_set))
3756 || MEM_P (SET_DEST (curr_insn_set))
3757 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3758 optional_p = true;
3759 else
3760 continue;
3763 /* Operands that match previous ones have already been handled. */
3764 if (goal_alt_matches[i] >= 0)
3765 continue;
3767 /* We should not have an operand with a non-offsettable address
3768 appearing where an offsettable address will do. It also may
3769 be a case when the address should be special in other words
3770 not a general one (e.g. it needs no index reg). */
3771 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3773 enum reg_class rclass;
3774 rtx *loc = &XEXP (op, 0);
3775 enum rtx_code code = GET_CODE (*loc);
3777 push_to_sequence (before);
3778 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3779 MEM, SCRATCH);
3780 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3781 new_reg = emit_inc (rclass, *loc, *loc,
3782 /* This value does not matter for MODIFY. */
3783 GET_MODE_SIZE (GET_MODE (op)));
3784 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3785 "offsetable address", &new_reg))
3786 lra_emit_move (new_reg, *loc);
3787 before = get_insns ();
3788 end_sequence ();
3789 *loc = new_reg;
3790 lra_update_dup (curr_id, i);
3792 else if (goal_alt_matched[i][0] == -1)
3794 machine_mode mode;
3795 rtx reg, *loc;
3796 int hard_regno, byte;
3797 enum op_type type = curr_static_id->operand[i].type;
3799 loc = curr_id->operand_loc[i];
3800 mode = curr_operand_mode[i];
3801 if (GET_CODE (*loc) == SUBREG)
3803 reg = SUBREG_REG (*loc);
3804 byte = SUBREG_BYTE (*loc);
3805 if (REG_P (reg)
3806 /* Strict_low_part requires reload the register not
3807 the sub-register. */
3808 && (curr_static_id->operand[i].strict_low
3809 || (GET_MODE_SIZE (mode)
3810 <= GET_MODE_SIZE (GET_MODE (reg))
3811 && (hard_regno
3812 = get_try_hard_regno (REGNO (reg))) >= 0
3813 && (simplify_subreg_regno
3814 (hard_regno,
3815 GET_MODE (reg), byte, mode) < 0)
3816 && (goal_alt[i] == NO_REGS
3817 || (simplify_subreg_regno
3818 (ira_class_hard_regs[goal_alt[i]][0],
3819 GET_MODE (reg), byte, mode) >= 0)))))
3821 if (type == OP_OUT)
3822 type = OP_INOUT;
3823 loc = &SUBREG_REG (*loc);
3824 mode = GET_MODE (*loc);
3827 old = *loc;
3828 if (get_reload_reg (type, mode, old, goal_alt[i],
3829 loc != curr_id->operand_loc[i], "", &new_reg)
3830 && type != OP_OUT)
3832 push_to_sequence (before);
3833 lra_emit_move (new_reg, old);
3834 before = get_insns ();
3835 end_sequence ();
3837 *loc = new_reg;
3838 if (type != OP_IN
3839 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3841 start_sequence ();
3842 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3843 emit_insn (after);
3844 after = get_insns ();
3845 end_sequence ();
3846 *loc = new_reg;
3848 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3849 if (goal_alt_dont_inherit_ops[j] == i)
3851 lra_set_regno_unique_value (REGNO (new_reg));
3852 break;
3854 lra_update_dup (curr_id, i);
3856 else if (curr_static_id->operand[i].type == OP_IN
3857 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3858 == OP_OUT))
3860 /* generate reloads for input and matched outputs. */
3861 match_inputs[0] = i;
3862 match_inputs[1] = -1;
3863 match_reload (goal_alt_matched[i][0], match_inputs,
3864 goal_alt[i], &before, &after);
3866 else if (curr_static_id->operand[i].type == OP_OUT
3867 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3868 == OP_IN))
3869 /* Generate reloads for output and matched inputs. */
3870 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3871 else if (curr_static_id->operand[i].type == OP_IN
3872 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3873 == OP_IN))
3875 /* Generate reloads for matched inputs. */
3876 match_inputs[0] = i;
3877 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3878 match_inputs[j + 1] = k;
3879 match_inputs[j + 1] = -1;
3880 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3882 else
3883 /* We must generate code in any case when function
3884 process_alt_operands decides that it is possible. */
3885 gcc_unreachable ();
3886 if (optional_p)
3888 lra_assert (REG_P (op));
3889 regno = REGNO (op);
3890 op = *curr_id->operand_loc[i]; /* Substitution. */
3891 if (GET_CODE (op) == SUBREG)
3892 op = SUBREG_REG (op);
3893 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3894 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3895 lra_reg_info[REGNO (op)].restore_regno = regno;
3896 if (lra_dump_file != NULL)
3897 fprintf (lra_dump_file,
3898 " Making reload reg %d for reg %d optional\n",
3899 REGNO (op), regno);
3902 if (before != NULL_RTX || after != NULL_RTX
3903 || max_regno_before != max_reg_num ())
3904 change_p = true;
3905 if (change_p)
3907 lra_update_operator_dups (curr_id);
3908 /* Something changes -- process the insn. */
3909 lra_update_insn_regno_info (curr_insn);
3911 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3912 return change_p;
3915 /* Return true if INSN satisfies all constraints. In other words, no
3916 reload insns are needed. */
3917 bool
3918 lra_constrain_insn (rtx_insn *insn)
3920 int saved_new_regno_start = new_regno_start;
3921 int saved_new_insn_uid_start = new_insn_uid_start;
3922 bool change_p;
3924 curr_insn = insn;
3925 curr_id = lra_get_insn_recog_data (curr_insn);
3926 curr_static_id = curr_id->insn_static_data;
3927 new_insn_uid_start = get_max_uid ();
3928 new_regno_start = max_reg_num ();
3929 change_p = curr_insn_transform (true);
3930 new_regno_start = saved_new_regno_start;
3931 new_insn_uid_start = saved_new_insn_uid_start;
3932 return ! change_p;
3935 /* Return true if X is in LIST. */
3936 static bool
3937 in_list_p (rtx x, rtx list)
3939 for (; list != NULL_RTX; list = XEXP (list, 1))
3940 if (XEXP (list, 0) == x)
3941 return true;
3942 return false;
3945 /* Return true if X contains an allocatable hard register (if
3946 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3947 static bool
3948 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3950 int i, j;
3951 const char *fmt;
3952 enum rtx_code code;
3954 code = GET_CODE (x);
3955 if (REG_P (x))
3957 int regno = REGNO (x);
3958 HARD_REG_SET alloc_regs;
3960 if (hard_reg_p)
3962 if (regno >= FIRST_PSEUDO_REGISTER)
3963 regno = lra_get_regno_hard_regno (regno);
3964 if (regno < 0)
3965 return false;
3966 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3967 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3969 else
3971 if (regno < FIRST_PSEUDO_REGISTER)
3972 return false;
3973 if (! spilled_p)
3974 return true;
3975 return lra_get_regno_hard_regno (regno) < 0;
3978 fmt = GET_RTX_FORMAT (code);
3979 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3981 if (fmt[i] == 'e')
3983 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3984 return true;
3986 else if (fmt[i] == 'E')
3988 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3989 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3990 return true;
3993 return false;
3996 /* Return true if X contains a symbol reg. */
3997 static bool
3998 contains_symbol_ref_p (rtx x)
4000 int i, j;
4001 const char *fmt;
4002 enum rtx_code code;
4004 code = GET_CODE (x);
4005 if (code == SYMBOL_REF)
4006 return true;
4007 fmt = GET_RTX_FORMAT (code);
4008 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4010 if (fmt[i] == 'e')
4012 if (contains_symbol_ref_p (XEXP (x, i)))
4013 return true;
4015 else if (fmt[i] == 'E')
4017 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4018 if (contains_symbol_ref_p (XVECEXP (x, i, j)))
4019 return true;
4022 return false;
4025 /* Process all regs in location *LOC and change them on equivalent
4026 substitution. Return true if any change was done. */
4027 static bool
4028 loc_equivalence_change_p (rtx *loc)
4030 rtx subst, reg, x = *loc;
4031 bool result = false;
4032 enum rtx_code code = GET_CODE (x);
4033 const char *fmt;
4034 int i, j;
4036 if (code == SUBREG)
4038 reg = SUBREG_REG (x);
4039 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4040 && GET_MODE (subst) == VOIDmode)
4042 /* We cannot reload debug location. Simplify subreg here
4043 while we know the inner mode. */
4044 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4045 GET_MODE (reg), SUBREG_BYTE (x));
4046 return true;
4049 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4051 *loc = subst;
4052 return true;
4055 /* Scan all the operand sub-expressions. */
4056 fmt = GET_RTX_FORMAT (code);
4057 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4059 if (fmt[i] == 'e')
4060 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4061 else if (fmt[i] == 'E')
4062 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4063 result
4064 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4066 return result;
4069 /* Similar to loc_equivalence_change_p, but for use as
4070 simplify_replace_fn_rtx callback. DATA is insn for which the
4071 elimination is done. If it null we don't do the elimination. */
4072 static rtx
4073 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4075 if (!REG_P (loc))
4076 return NULL_RTX;
4078 rtx subst = (data == NULL
4079 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4080 if (subst != loc)
4081 return subst;
4083 return NULL_RTX;
4086 /* Maximum number of generated reload insns per an insn. It is for
4087 preventing this pass cycling in a bug case. */
4088 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4090 /* The current iteration number of this LRA pass. */
4091 int lra_constraint_iter;
4093 /* True if we substituted equiv which needs checking register
4094 allocation correctness because the equivalent value contains
4095 allocatable hard registers or when we restore multi-register
4096 pseudo. */
4097 bool lra_risky_transformations_p;
4099 /* Return true if REGNO is referenced in more than one block. */
4100 static bool
4101 multi_block_pseudo_p (int regno)
4103 basic_block bb = NULL;
4104 unsigned int uid;
4105 bitmap_iterator bi;
4107 if (regno < FIRST_PSEUDO_REGISTER)
4108 return false;
4110 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4111 if (bb == NULL)
4112 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4113 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4114 return true;
4115 return false;
4118 /* Return true if LIST contains a deleted insn. */
4119 static bool
4120 contains_deleted_insn_p (rtx_insn_list *list)
4122 for (; list != NULL_RTX; list = list->next ())
4123 if (NOTE_P (list->insn ())
4124 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4125 return true;
4126 return false;
4129 /* Return true if X contains a pseudo dying in INSN. */
4130 static bool
4131 dead_pseudo_p (rtx x, rtx_insn *insn)
4133 int i, j;
4134 const char *fmt;
4135 enum rtx_code code;
4137 if (REG_P (x))
4138 return (insn != NULL_RTX
4139 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4140 code = GET_CODE (x);
4141 fmt = GET_RTX_FORMAT (code);
4142 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4144 if (fmt[i] == 'e')
4146 if (dead_pseudo_p (XEXP (x, i), insn))
4147 return true;
4149 else if (fmt[i] == 'E')
4151 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4152 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4153 return true;
4156 return false;
4159 /* Return true if INSN contains a dying pseudo in INSN right hand
4160 side. */
4161 static bool
4162 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4164 rtx set = single_set (insn);
4166 gcc_assert (set != NULL);
4167 return dead_pseudo_p (SET_SRC (set), insn);
4170 /* Return true if any init insn of REGNO contains a dying pseudo in
4171 insn right hand side. */
4172 static bool
4173 init_insn_rhs_dead_pseudo_p (int regno)
4175 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4177 if (insns == NULL)
4178 return false;
4179 for (; insns != NULL_RTX; insns = insns->next ())
4180 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4181 return true;
4182 return false;
4185 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4186 reverse only if we have one init insn with given REGNO as a
4187 source. */
4188 static bool
4189 reverse_equiv_p (int regno)
4191 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4192 rtx set;
4194 if (insns == NULL)
4195 return false;
4196 if (! INSN_P (insns->insn ())
4197 || insns->next () != NULL)
4198 return false;
4199 if ((set = single_set (insns->insn ())) == NULL_RTX)
4200 return false;
4201 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4204 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4205 call this function only for non-reverse equivalence. */
4206 static bool
4207 contains_reloaded_insn_p (int regno)
4209 rtx set;
4210 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4212 for (; list != NULL; list = list->next ())
4213 if ((set = single_set (list->insn ())) == NULL_RTX
4214 || ! REG_P (SET_DEST (set))
4215 || (int) REGNO (SET_DEST (set)) != regno)
4216 return true;
4217 return false;
4220 /* Entry function of LRA constraint pass. Return true if the
4221 constraint pass did change the code. */
4222 bool
4223 lra_constraints (bool first_p)
4225 bool changed_p;
4226 int i, hard_regno, new_insns_num;
4227 unsigned int min_len, new_min_len, uid;
4228 rtx set, x, reg, dest_reg;
4229 basic_block last_bb;
4230 bitmap_head equiv_insn_bitmap;
4231 bitmap_iterator bi;
4233 lra_constraint_iter++;
4234 if (lra_dump_file != NULL)
4235 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4236 lra_constraint_iter);
4237 changed_p = false;
4238 if (pic_offset_table_rtx
4239 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4240 lra_risky_transformations_p = true;
4241 else
4242 lra_risky_transformations_p = false;
4243 new_insn_uid_start = get_max_uid ();
4244 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4245 /* Mark used hard regs for target stack size calulations. */
4246 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4247 if (lra_reg_info[i].nrefs != 0
4248 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4250 int j, nregs;
4252 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4253 for (j = 0; j < nregs; j++)
4254 df_set_regs_ever_live (hard_regno + j, true);
4256 /* Do elimination before the equivalence processing as we can spill
4257 some pseudos during elimination. */
4258 lra_eliminate (false, first_p);
4259 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4260 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4261 if (lra_reg_info[i].nrefs != 0)
4263 ira_reg_equiv[i].profitable_p = true;
4264 reg = regno_reg_rtx[i];
4265 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4267 bool pseudo_p = contains_reg_p (x, false, false);
4269 /* After RTL transformation, we can not guarantee that
4270 pseudo in the substitution was not reloaded which might
4271 make equivalence invalid. For example, in reverse
4272 equiv of p0
4274 p0 <- ...
4276 equiv_mem <- p0
4278 the memory address register was reloaded before the 2nd
4279 insn. */
4280 if ((! first_p && pseudo_p)
4281 /* We don't use DF for compilation speed sake. So it
4282 is problematic to update live info when we use an
4283 equivalence containing pseudos in more than one
4284 BB. */
4285 || (pseudo_p && multi_block_pseudo_p (i))
4286 /* If an init insn was deleted for some reason, cancel
4287 the equiv. We could update the equiv insns after
4288 transformations including an equiv insn deletion
4289 but it is not worthy as such cases are extremely
4290 rare. */
4291 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4292 /* If it is not a reverse equivalence, we check that a
4293 pseudo in rhs of the init insn is not dying in the
4294 insn. Otherwise, the live info at the beginning of
4295 the corresponding BB might be wrong after we
4296 removed the insn. When the equiv can be a
4297 constant, the right hand side of the init insn can
4298 be a pseudo. */
4299 || (! reverse_equiv_p (i)
4300 && (init_insn_rhs_dead_pseudo_p (i)
4301 /* If we reloaded the pseudo in an equivalence
4302 init insn, we can not remove the equiv init
4303 insns and the init insns might write into
4304 const memory in this case. */
4305 || contains_reloaded_insn_p (i)))
4306 /* Prevent access beyond equivalent memory for
4307 paradoxical subregs. */
4308 || (MEM_P (x)
4309 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4310 > GET_MODE_SIZE (GET_MODE (x))))
4311 || (pic_offset_table_rtx
4312 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4313 && (targetm.preferred_reload_class
4314 (x, lra_get_allocno_class (i)) == NO_REGS))
4315 || contains_symbol_ref_p (x))))
4316 ira_reg_equiv[i].defined_p = false;
4317 if (contains_reg_p (x, false, true))
4318 ira_reg_equiv[i].profitable_p = false;
4319 if (get_equiv (reg) != reg)
4320 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4323 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4324 update_equiv (i);
4325 /* We should add all insns containing pseudos which should be
4326 substituted by their equivalences. */
4327 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4328 lra_push_insn_by_uid (uid);
4329 min_len = lra_insn_stack_length ();
4330 new_insns_num = 0;
4331 last_bb = NULL;
4332 changed_p = false;
4333 while ((new_min_len = lra_insn_stack_length ()) != 0)
4335 curr_insn = lra_pop_insn ();
4336 --new_min_len;
4337 curr_bb = BLOCK_FOR_INSN (curr_insn);
4338 if (curr_bb != last_bb)
4340 last_bb = curr_bb;
4341 bb_reload_num = lra_curr_reload_num;
4343 if (min_len > new_min_len)
4345 min_len = new_min_len;
4346 new_insns_num = 0;
4348 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4349 internal_error
4350 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4351 MAX_RELOAD_INSNS_NUMBER);
4352 new_insns_num++;
4353 if (DEBUG_INSN_P (curr_insn))
4355 /* We need to check equivalence in debug insn and change
4356 pseudo to the equivalent value if necessary. */
4357 curr_id = lra_get_insn_recog_data (curr_insn);
4358 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4360 rtx old = *curr_id->operand_loc[0];
4361 *curr_id->operand_loc[0]
4362 = simplify_replace_fn_rtx (old, NULL_RTX,
4363 loc_equivalence_callback, curr_insn);
4364 if (old != *curr_id->operand_loc[0])
4366 lra_update_insn_regno_info (curr_insn);
4367 changed_p = true;
4371 else if (INSN_P (curr_insn))
4373 if ((set = single_set (curr_insn)) != NULL_RTX)
4375 dest_reg = SET_DEST (set);
4376 /* The equivalence pseudo could be set up as SUBREG in a
4377 case when it is a call restore insn in a mode
4378 different from the pseudo mode. */
4379 if (GET_CODE (dest_reg) == SUBREG)
4380 dest_reg = SUBREG_REG (dest_reg);
4381 if ((REG_P (dest_reg)
4382 && (x = get_equiv (dest_reg)) != dest_reg
4383 /* Remove insns which set up a pseudo whose value
4384 can not be changed. Such insns might be not in
4385 init_insns because we don't update equiv data
4386 during insn transformations.
4388 As an example, let suppose that a pseudo got
4389 hard register and on the 1st pass was not
4390 changed to equivalent constant. We generate an
4391 additional insn setting up the pseudo because of
4392 secondary memory movement. Then the pseudo is
4393 spilled and we use the equiv constant. In this
4394 case we should remove the additional insn and
4395 this insn is not init_insns list. */
4396 && (! MEM_P (x) || MEM_READONLY_P (x)
4397 /* Check that this is actually an insn setting
4398 up the equivalence. */
4399 || in_list_p (curr_insn,
4400 ira_reg_equiv
4401 [REGNO (dest_reg)].init_insns)))
4402 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4403 && in_list_p (curr_insn,
4404 ira_reg_equiv
4405 [REGNO (SET_SRC (set))].init_insns)))
4407 /* This is equiv init insn of pseudo which did not get a
4408 hard register -- remove the insn. */
4409 if (lra_dump_file != NULL)
4411 fprintf (lra_dump_file,
4412 " Removing equiv init insn %i (freq=%d)\n",
4413 INSN_UID (curr_insn),
4414 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4415 dump_insn_slim (lra_dump_file, curr_insn);
4417 if (contains_reg_p (x, true, false))
4418 lra_risky_transformations_p = true;
4419 lra_set_insn_deleted (curr_insn);
4420 continue;
4423 curr_id = lra_get_insn_recog_data (curr_insn);
4424 curr_static_id = curr_id->insn_static_data;
4425 init_curr_insn_input_reloads ();
4426 init_curr_operand_mode ();
4427 if (curr_insn_transform (false))
4428 changed_p = true;
4429 /* Check non-transformed insns too for equiv change as USE
4430 or CLOBBER don't need reloads but can contain pseudos
4431 being changed on their equivalences. */
4432 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4433 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4435 lra_update_insn_regno_info (curr_insn);
4436 changed_p = true;
4440 bitmap_clear (&equiv_insn_bitmap);
4441 /* If we used a new hard regno, changed_p should be true because the
4442 hard reg is assigned to a new pseudo. */
4443 #ifdef ENABLE_CHECKING
4444 if (! changed_p)
4446 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4447 if (lra_reg_info[i].nrefs != 0
4448 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4450 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4452 for (j = 0; j < nregs; j++)
4453 lra_assert (df_regs_ever_live_p (hard_regno + j));
4456 #endif
4457 return changed_p;
4460 /* Initiate the LRA constraint pass. It is done once per
4461 function. */
4462 void
4463 lra_constraints_init (void)
4467 /* Finalize the LRA constraint pass. It is done once per
4468 function. */
4469 void
4470 lra_constraints_finish (void)
4476 /* This page contains code to do inheritance/split
4477 transformations. */
4479 /* Number of reloads passed so far in current EBB. */
4480 static int reloads_num;
4482 /* Number of calls passed so far in current EBB. */
4483 static int calls_num;
4485 /* Current reload pseudo check for validity of elements in
4486 USAGE_INSNS. */
4487 static int curr_usage_insns_check;
4489 /* Info about last usage of registers in EBB to do inheritance/split
4490 transformation. Inheritance transformation is done from a spilled
4491 pseudo and split transformations from a hard register or a pseudo
4492 assigned to a hard register. */
4493 struct usage_insns
4495 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4496 value INSNS is valid. The insns is chain of optional debug insns
4497 and a finishing non-debug insn using the corresponding reg. The
4498 value is also used to mark the registers which are set up in the
4499 current insn. The negated insn uid is used for this. */
4500 int check;
4501 /* Value of global reloads_num at the last insn in INSNS. */
4502 int reloads_num;
4503 /* Value of global reloads_nums at the last insn in INSNS. */
4504 int calls_num;
4505 /* It can be true only for splitting. And it means that the restore
4506 insn should be put after insn given by the following member. */
4507 bool after_p;
4508 /* Next insns in the current EBB which use the original reg and the
4509 original reg value is not changed between the current insn and
4510 the next insns. In order words, e.g. for inheritance, if we need
4511 to use the original reg value again in the next insns we can try
4512 to use the value in a hard register from a reload insn of the
4513 current insn. */
4514 rtx insns;
4517 /* Map: regno -> corresponding pseudo usage insns. */
4518 static struct usage_insns *usage_insns;
4520 static void
4521 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4523 usage_insns[regno].check = curr_usage_insns_check;
4524 usage_insns[regno].insns = insn;
4525 usage_insns[regno].reloads_num = reloads_num;
4526 usage_insns[regno].calls_num = calls_num;
4527 usage_insns[regno].after_p = after_p;
4530 /* The function is used to form list REGNO usages which consists of
4531 optional debug insns finished by a non-debug insn using REGNO.
4532 RELOADS_NUM is current number of reload insns processed so far. */
4533 static void
4534 add_next_usage_insn (int regno, rtx insn, int reloads_num)
4536 rtx next_usage_insns;
4538 if (usage_insns[regno].check == curr_usage_insns_check
4539 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4540 && DEBUG_INSN_P (insn))
4542 /* Check that we did not add the debug insn yet. */
4543 if (next_usage_insns != insn
4544 && (GET_CODE (next_usage_insns) != INSN_LIST
4545 || XEXP (next_usage_insns, 0) != insn))
4546 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4547 next_usage_insns);
4549 else if (NONDEBUG_INSN_P (insn))
4550 setup_next_usage_insn (regno, insn, reloads_num, false);
4551 else
4552 usage_insns[regno].check = 0;
4555 /* Return first non-debug insn in list USAGE_INSNS. */
4556 static rtx_insn *
4557 skip_usage_debug_insns (rtx usage_insns)
4559 rtx insn;
4561 /* Skip debug insns. */
4562 for (insn = usage_insns;
4563 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4564 insn = XEXP (insn, 1))
4566 return safe_as_a <rtx_insn *> (insn);
4569 /* Return true if we need secondary memory moves for insn in
4570 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4571 into the insn. */
4572 static bool
4573 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4574 rtx usage_insns ATTRIBUTE_UNUSED)
4576 #ifndef SECONDARY_MEMORY_NEEDED
4577 return false;
4578 #else
4579 rtx_insn *insn;
4580 rtx set, dest;
4581 enum reg_class cl;
4583 if (inher_cl == ALL_REGS
4584 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4585 return false;
4586 lra_assert (INSN_P (insn));
4587 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4588 return false;
4589 dest = SET_DEST (set);
4590 if (! REG_P (dest))
4591 return false;
4592 lra_assert (inher_cl != NO_REGS);
4593 cl = get_reg_class (REGNO (dest));
4594 return (cl != NO_REGS && cl != ALL_REGS
4595 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4596 #endif
4599 /* Registers involved in inheritance/split in the current EBB
4600 (inheritance/split pseudos and original registers). */
4601 static bitmap_head check_only_regs;
4603 /* Do inheritance transformations for insn INSN, which defines (if
4604 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4605 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4606 form as the "insns" field of usage_insns. Return true if we
4607 succeed in such transformation.
4609 The transformations look like:
4611 p <- ... i <- ...
4612 ... p <- i (new insn)
4613 ... =>
4614 <- ... p ... <- ... i ...
4616 ... i <- p (new insn)
4617 <- ... p ... <- ... i ...
4618 ... =>
4619 <- ... p ... <- ... i ...
4620 where p is a spilled original pseudo and i is a new inheritance pseudo.
4623 The inheritance pseudo has the smallest class of two classes CL and
4624 class of ORIGINAL REGNO. */
4625 static bool
4626 inherit_reload_reg (bool def_p, int original_regno,
4627 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4629 if (optimize_function_for_size_p (cfun))
4630 return false;
4632 enum reg_class rclass = lra_get_allocno_class (original_regno);
4633 rtx original_reg = regno_reg_rtx[original_regno];
4634 rtx new_reg, usage_insn;
4635 rtx_insn *new_insns;
4637 lra_assert (! usage_insns[original_regno].after_p);
4638 if (lra_dump_file != NULL)
4639 fprintf (lra_dump_file,
4640 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4641 if (! ira_reg_classes_intersect_p[cl][rclass])
4643 if (lra_dump_file != NULL)
4645 fprintf (lra_dump_file,
4646 " Rejecting inheritance for %d "
4647 "because of disjoint classes %s and %s\n",
4648 original_regno, reg_class_names[cl],
4649 reg_class_names[rclass]);
4650 fprintf (lra_dump_file,
4651 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4653 return false;
4655 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4656 /* We don't use a subset of two classes because it can be
4657 NO_REGS. This transformation is still profitable in most
4658 cases even if the classes are not intersected as register
4659 move is probably cheaper than a memory load. */
4660 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4662 if (lra_dump_file != NULL)
4663 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4664 reg_class_names[cl], reg_class_names[rclass]);
4666 rclass = cl;
4668 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4670 /* Reject inheritance resulting in secondary memory moves.
4671 Otherwise, there is a danger in LRA cycling. Also such
4672 transformation will be unprofitable. */
4673 if (lra_dump_file != NULL)
4675 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4676 rtx set = single_set (insn);
4678 lra_assert (set != NULL_RTX);
4680 rtx dest = SET_DEST (set);
4682 lra_assert (REG_P (dest));
4683 fprintf (lra_dump_file,
4684 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4685 "as secondary mem is needed\n",
4686 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4687 original_regno, reg_class_names[rclass]);
4688 fprintf (lra_dump_file,
4689 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4691 return false;
4693 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4694 rclass, "inheritance");
4695 start_sequence ();
4696 if (def_p)
4697 lra_emit_move (original_reg, new_reg);
4698 else
4699 lra_emit_move (new_reg, original_reg);
4700 new_insns = get_insns ();
4701 end_sequence ();
4702 if (NEXT_INSN (new_insns) != NULL_RTX)
4704 if (lra_dump_file != NULL)
4706 fprintf (lra_dump_file,
4707 " Rejecting inheritance %d->%d "
4708 "as it results in 2 or more insns:\n",
4709 original_regno, REGNO (new_reg));
4710 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4711 fprintf (lra_dump_file,
4712 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4714 return false;
4716 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
4717 lra_update_insn_regno_info (insn);
4718 if (! def_p)
4719 /* We now have a new usage insn for original regno. */
4720 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4721 if (lra_dump_file != NULL)
4722 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4723 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4724 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4725 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4726 bitmap_set_bit (&check_only_regs, original_regno);
4727 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4728 if (def_p)
4729 lra_process_new_insns (insn, NULL, new_insns,
4730 "Add original<-inheritance");
4731 else
4732 lra_process_new_insns (insn, new_insns, NULL,
4733 "Add inheritance<-original");
4734 while (next_usage_insns != NULL_RTX)
4736 if (GET_CODE (next_usage_insns) != INSN_LIST)
4738 usage_insn = next_usage_insns;
4739 lra_assert (NONDEBUG_INSN_P (usage_insn));
4740 next_usage_insns = NULL;
4742 else
4744 usage_insn = XEXP (next_usage_insns, 0);
4745 lra_assert (DEBUG_INSN_P (usage_insn));
4746 next_usage_insns = XEXP (next_usage_insns, 1);
4748 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
4749 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4750 if (lra_dump_file != NULL)
4752 fprintf (lra_dump_file,
4753 " Inheritance reuse change %d->%d (bb%d):\n",
4754 original_regno, REGNO (new_reg),
4755 BLOCK_FOR_INSN (usage_insn)->index);
4756 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
4759 if (lra_dump_file != NULL)
4760 fprintf (lra_dump_file,
4761 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4762 return true;
4765 /* Return true if we need a caller save/restore for pseudo REGNO which
4766 was assigned to a hard register. */
4767 static inline bool
4768 need_for_call_save_p (int regno)
4770 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4771 return (usage_insns[regno].calls_num < calls_num
4772 && (overlaps_hard_reg_set_p
4773 ((flag_ipa_ra &&
4774 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4775 ? lra_reg_info[regno].actual_call_used_reg_set
4776 : call_used_reg_set,
4777 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4778 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4779 PSEUDO_REGNO_MODE (regno))));
4782 /* Global registers occurring in the current EBB. */
4783 static bitmap_head ebb_global_regs;
4785 /* Return true if we need a split for hard register REGNO or pseudo
4786 REGNO which was assigned to a hard register.
4787 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4788 used for reloads since the EBB end. It is an approximation of the
4789 used hard registers in the split range. The exact value would
4790 require expensive calculations. If we were aggressive with
4791 splitting because of the approximation, the split pseudo will save
4792 the same hard register assignment and will be removed in the undo
4793 pass. We still need the approximation because too aggressive
4794 splitting would result in too inaccurate cost calculation in the
4795 assignment pass because of too many generated moves which will be
4796 probably removed in the undo pass. */
4797 static inline bool
4798 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4800 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4802 lra_assert (hard_regno >= 0);
4803 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4804 /* Don't split eliminable hard registers, otherwise we can
4805 split hard registers like hard frame pointer, which
4806 lives on BB start/end according to DF-infrastructure,
4807 when there is a pseudo assigned to the register and
4808 living in the same BB. */
4809 && (regno >= FIRST_PSEUDO_REGISTER
4810 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4811 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4812 /* Don't split call clobbered hard regs living through
4813 calls, otherwise we might have a check problem in the
4814 assign sub-pass as in the most cases (exception is a
4815 situation when lra_risky_transformations_p value is
4816 true) the assign pass assumes that all pseudos living
4817 through calls are assigned to call saved hard regs. */
4818 && (regno >= FIRST_PSEUDO_REGISTER
4819 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4820 || usage_insns[regno].calls_num == calls_num)
4821 /* We need at least 2 reloads to make pseudo splitting
4822 profitable. We should provide hard regno splitting in
4823 any case to solve 1st insn scheduling problem when
4824 moving hard register definition up might result in
4825 impossibility to find hard register for reload pseudo of
4826 small register class. */
4827 && (usage_insns[regno].reloads_num
4828 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4829 && (regno < FIRST_PSEUDO_REGISTER
4830 /* For short living pseudos, spilling + inheritance can
4831 be considered a substitution for splitting.
4832 Therefore we do not splitting for local pseudos. It
4833 decreases also aggressiveness of splitting. The
4834 minimal number of references is chosen taking into
4835 account that for 2 references splitting has no sense
4836 as we can just spill the pseudo. */
4837 || (regno >= FIRST_PSEUDO_REGISTER
4838 && lra_reg_info[regno].nrefs > 3
4839 && bitmap_bit_p (&ebb_global_regs, regno))))
4840 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4843 /* Return class for the split pseudo created from original pseudo with
4844 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4845 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4846 results in no secondary memory movements. */
4847 static enum reg_class
4848 choose_split_class (enum reg_class allocno_class,
4849 int hard_regno ATTRIBUTE_UNUSED,
4850 machine_mode mode ATTRIBUTE_UNUSED)
4852 #ifndef SECONDARY_MEMORY_NEEDED
4853 return allocno_class;
4854 #else
4855 int i;
4856 enum reg_class cl, best_cl = NO_REGS;
4857 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4858 = REGNO_REG_CLASS (hard_regno);
4860 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4861 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4862 return allocno_class;
4863 for (i = 0;
4864 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4865 i++)
4866 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4867 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4868 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4869 && (best_cl == NO_REGS
4870 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4871 best_cl = cl;
4872 return best_cl;
4873 #endif
4876 /* Do split transformations for insn INSN, which defines or uses
4877 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4878 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4879 "insns" field of usage_insns.
4881 The transformations look like:
4883 p <- ... p <- ...
4884 ... s <- p (new insn -- save)
4885 ... =>
4886 ... p <- s (new insn -- restore)
4887 <- ... p ... <- ... p ...
4889 <- ... p ... <- ... p ...
4890 ... s <- p (new insn -- save)
4891 ... =>
4892 ... p <- s (new insn -- restore)
4893 <- ... p ... <- ... p ...
4895 where p is an original pseudo got a hard register or a hard
4896 register and s is a new split pseudo. The save is put before INSN
4897 if BEFORE_P is true. Return true if we succeed in such
4898 transformation. */
4899 static bool
4900 split_reg (bool before_p, int original_regno, rtx_insn *insn,
4901 rtx next_usage_insns)
4903 enum reg_class rclass;
4904 rtx original_reg;
4905 int hard_regno, nregs;
4906 rtx new_reg, usage_insn;
4907 rtx_insn *restore, *save;
4908 bool after_p;
4909 bool call_save_p;
4911 if (original_regno < FIRST_PSEUDO_REGISTER)
4913 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4914 hard_regno = original_regno;
4915 call_save_p = false;
4916 nregs = 1;
4918 else
4920 hard_regno = reg_renumber[original_regno];
4921 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4922 rclass = lra_get_allocno_class (original_regno);
4923 original_reg = regno_reg_rtx[original_regno];
4924 call_save_p = need_for_call_save_p (original_regno);
4926 original_reg = regno_reg_rtx[original_regno];
4927 lra_assert (hard_regno >= 0);
4928 if (lra_dump_file != NULL)
4929 fprintf (lra_dump_file,
4930 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4931 if (call_save_p)
4933 machine_mode mode = GET_MODE (original_reg);
4935 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4936 hard_regno_nregs[hard_regno][mode],
4937 mode);
4938 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4940 else
4942 rclass = choose_split_class (rclass, hard_regno,
4943 GET_MODE (original_reg));
4944 if (rclass == NO_REGS)
4946 if (lra_dump_file != NULL)
4948 fprintf (lra_dump_file,
4949 " Rejecting split of %d(%s): "
4950 "no good reg class for %d(%s)\n",
4951 original_regno,
4952 reg_class_names[lra_get_allocno_class (original_regno)],
4953 hard_regno,
4954 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4955 fprintf
4956 (lra_dump_file,
4957 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4959 return false;
4961 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4962 rclass, "split");
4963 reg_renumber[REGNO (new_reg)] = hard_regno;
4965 save = emit_spill_move (true, new_reg, original_reg);
4966 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
4968 if (lra_dump_file != NULL)
4970 fprintf
4971 (lra_dump_file,
4972 " Rejecting split %d->%d resulting in > 2 save insns:\n",
4973 original_regno, REGNO (new_reg));
4974 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
4975 fprintf (lra_dump_file,
4976 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4978 return false;
4980 restore = emit_spill_move (false, new_reg, original_reg);
4981 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
4983 if (lra_dump_file != NULL)
4985 fprintf (lra_dump_file,
4986 " Rejecting split %d->%d "
4987 "resulting in > 2 restore insns:\n",
4988 original_regno, REGNO (new_reg));
4989 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
4990 fprintf (lra_dump_file,
4991 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4993 return false;
4995 after_p = usage_insns[original_regno].after_p;
4996 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4997 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4998 bitmap_set_bit (&check_only_regs, original_regno);
4999 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
5000 for (;;)
5002 if (GET_CODE (next_usage_insns) != INSN_LIST)
5004 usage_insn = next_usage_insns;
5005 break;
5007 usage_insn = XEXP (next_usage_insns, 0);
5008 lra_assert (DEBUG_INSN_P (usage_insn));
5009 next_usage_insns = XEXP (next_usage_insns, 1);
5010 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5011 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5012 if (lra_dump_file != NULL)
5014 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5015 original_regno, REGNO (new_reg));
5016 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5019 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5020 lra_assert (usage_insn != insn || (after_p && before_p));
5021 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5022 after_p ? NULL : restore,
5023 after_p ? restore : NULL,
5024 call_save_p
5025 ? "Add reg<-save" : "Add reg<-split");
5026 lra_process_new_insns (insn, before_p ? save : NULL,
5027 before_p ? NULL : save,
5028 call_save_p
5029 ? "Add save<-reg" : "Add split<-reg");
5030 if (nregs > 1)
5031 /* If we are trying to split multi-register. We should check
5032 conflicts on the next assignment sub-pass. IRA can allocate on
5033 sub-register levels, LRA do this on pseudos level right now and
5034 this discrepancy may create allocation conflicts after
5035 splitting. */
5036 lra_risky_transformations_p = true;
5037 if (lra_dump_file != NULL)
5038 fprintf (lra_dump_file,
5039 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5040 return true;
5043 /* Recognize that we need a split transformation for insn INSN, which
5044 defines or uses REGNO in its insn biggest MODE (we use it only if
5045 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5046 hard registers which might be used for reloads since the EBB end.
5047 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5048 uid before starting INSN processing. Return true if we succeed in
5049 such transformation. */
5050 static bool
5051 split_if_necessary (int regno, machine_mode mode,
5052 HARD_REG_SET potential_reload_hard_regs,
5053 bool before_p, rtx_insn *insn, int max_uid)
5055 bool res = false;
5056 int i, nregs = 1;
5057 rtx next_usage_insns;
5059 if (regno < FIRST_PSEUDO_REGISTER)
5060 nregs = hard_regno_nregs[regno][mode];
5061 for (i = 0; i < nregs; i++)
5062 if (usage_insns[regno + i].check == curr_usage_insns_check
5063 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5064 /* To avoid processing the register twice or more. */
5065 && ((GET_CODE (next_usage_insns) != INSN_LIST
5066 && INSN_UID (next_usage_insns) < max_uid)
5067 || (GET_CODE (next_usage_insns) == INSN_LIST
5068 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5069 && need_for_split_p (potential_reload_hard_regs, regno + i)
5070 && split_reg (before_p, regno + i, insn, next_usage_insns))
5071 res = true;
5072 return res;
5075 /* Check only registers living at the current program point in the
5076 current EBB. */
5077 static bitmap_head live_regs;
5079 /* Update live info in EBB given by its HEAD and TAIL insns after
5080 inheritance/split transformation. The function removes dead moves
5081 too. */
5082 static void
5083 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5085 unsigned int j;
5086 int i, regno;
5087 bool live_p;
5088 rtx_insn *prev_insn;
5089 rtx set;
5090 bool remove_p;
5091 basic_block last_bb, prev_bb, curr_bb;
5092 bitmap_iterator bi;
5093 struct lra_insn_reg *reg;
5094 edge e;
5095 edge_iterator ei;
5097 last_bb = BLOCK_FOR_INSN (tail);
5098 prev_bb = NULL;
5099 for (curr_insn = tail;
5100 curr_insn != PREV_INSN (head);
5101 curr_insn = prev_insn)
5103 prev_insn = PREV_INSN (curr_insn);
5104 /* We need to process empty blocks too. They contain
5105 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5106 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5107 continue;
5108 curr_bb = BLOCK_FOR_INSN (curr_insn);
5109 if (curr_bb != prev_bb)
5111 if (prev_bb != NULL)
5113 /* Update df_get_live_in (prev_bb): */
5114 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5115 if (bitmap_bit_p (&live_regs, j))
5116 bitmap_set_bit (df_get_live_in (prev_bb), j);
5117 else
5118 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5120 if (curr_bb != last_bb)
5122 /* Update df_get_live_out (curr_bb): */
5123 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5125 live_p = bitmap_bit_p (&live_regs, j);
5126 if (! live_p)
5127 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5128 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5130 live_p = true;
5131 break;
5133 if (live_p)
5134 bitmap_set_bit (df_get_live_out (curr_bb), j);
5135 else
5136 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5139 prev_bb = curr_bb;
5140 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5142 if (! NONDEBUG_INSN_P (curr_insn))
5143 continue;
5144 curr_id = lra_get_insn_recog_data (curr_insn);
5145 curr_static_id = curr_id->insn_static_data;
5146 remove_p = false;
5147 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
5148 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5149 && bitmap_bit_p (&check_only_regs, regno)
5150 && ! bitmap_bit_p (&live_regs, regno))
5151 remove_p = true;
5152 /* See which defined values die here. */
5153 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5154 if (reg->type == OP_OUT && ! reg->subreg_p)
5155 bitmap_clear_bit (&live_regs, reg->regno);
5156 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5157 if (reg->type == OP_OUT && ! reg->subreg_p)
5158 bitmap_clear_bit (&live_regs, reg->regno);
5159 if (curr_id->arg_hard_regs != NULL)
5160 /* Make clobbered argument hard registers die. */
5161 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5162 if (regno >= FIRST_PSEUDO_REGISTER)
5163 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
5164 /* Mark each used value as live. */
5165 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5166 if (reg->type != OP_OUT
5167 && bitmap_bit_p (&check_only_regs, reg->regno))
5168 bitmap_set_bit (&live_regs, reg->regno);
5169 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5170 if (reg->type != OP_OUT
5171 && bitmap_bit_p (&check_only_regs, reg->regno))
5172 bitmap_set_bit (&live_regs, reg->regno);
5173 if (curr_id->arg_hard_regs != NULL)
5174 /* Make used argument hard registers live. */
5175 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5176 if (regno < FIRST_PSEUDO_REGISTER
5177 && bitmap_bit_p (&check_only_regs, regno))
5178 bitmap_set_bit (&live_regs, regno);
5179 /* It is quite important to remove dead move insns because it
5180 means removing dead store. We don't need to process them for
5181 constraints. */
5182 if (remove_p)
5184 if (lra_dump_file != NULL)
5186 fprintf (lra_dump_file, " Removing dead insn:\n ");
5187 dump_insn_slim (lra_dump_file, curr_insn);
5189 lra_set_insn_deleted (curr_insn);
5194 /* The structure describes info to do an inheritance for the current
5195 insn. We need to collect such info first before doing the
5196 transformations because the transformations change the insn
5197 internal representation. */
5198 struct to_inherit
5200 /* Original regno. */
5201 int regno;
5202 /* Subsequent insns which can inherit original reg value. */
5203 rtx insns;
5206 /* Array containing all info for doing inheritance from the current
5207 insn. */
5208 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5210 /* Number elements in the previous array. */
5211 static int to_inherit_num;
5213 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5214 structure to_inherit. */
5215 static void
5216 add_to_inherit (int regno, rtx insns)
5218 int i;
5220 for (i = 0; i < to_inherit_num; i++)
5221 if (to_inherit[i].regno == regno)
5222 return;
5223 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5224 to_inherit[to_inherit_num].regno = regno;
5225 to_inherit[to_inherit_num++].insns = insns;
5228 /* Return the last non-debug insn in basic block BB, or the block begin
5229 note if none. */
5230 static rtx_insn *
5231 get_last_insertion_point (basic_block bb)
5233 rtx_insn *insn;
5235 FOR_BB_INSNS_REVERSE (bb, insn)
5236 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5237 return insn;
5238 gcc_unreachable ();
5241 /* Set up RES by registers living on edges FROM except the edge (FROM,
5242 TO) or by registers set up in a jump insn in BB FROM. */
5243 static void
5244 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5246 rtx_insn *last;
5247 struct lra_insn_reg *reg;
5248 edge e;
5249 edge_iterator ei;
5251 lra_assert (to != NULL);
5252 bitmap_clear (res);
5253 FOR_EACH_EDGE (e, ei, from->succs)
5254 if (e->dest != to)
5255 bitmap_ior_into (res, df_get_live_in (e->dest));
5256 last = get_last_insertion_point (from);
5257 if (! JUMP_P (last))
5258 return;
5259 curr_id = lra_get_insn_recog_data (last);
5260 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5261 if (reg->type != OP_IN)
5262 bitmap_set_bit (res, reg->regno);
5265 /* Used as a temporary results of some bitmap calculations. */
5266 static bitmap_head temp_bitmap;
5268 /* We split for reloads of small class of hard regs. The following
5269 defines how many hard regs the class should have to be qualified as
5270 small. The code is mostly oriented to x86/x86-64 architecture
5271 where some insns need to use only specific register or pair of
5272 registers and these register can live in RTL explicitly, e.g. for
5273 parameter passing. */
5274 static const int max_small_class_regs_num = 2;
5276 /* Do inheritance/split transformations in EBB starting with HEAD and
5277 finishing on TAIL. We process EBB insns in the reverse order.
5278 Return true if we did any inheritance/split transformation in the
5279 EBB.
5281 We should avoid excessive splitting which results in worse code
5282 because of inaccurate cost calculations for spilling new split
5283 pseudos in such case. To achieve this we do splitting only if
5284 register pressure is high in given basic block and there are reload
5285 pseudos requiring hard registers. We could do more register
5286 pressure calculations at any given program point to avoid necessary
5287 splitting even more but it is to expensive and the current approach
5288 works well enough. */
5289 static bool
5290 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5292 int i, src_regno, dst_regno, nregs;
5293 bool change_p, succ_p, update_reloads_num_p;
5294 rtx_insn *prev_insn, *last_insn;
5295 rtx next_usage_insns, set;
5296 enum reg_class cl;
5297 struct lra_insn_reg *reg;
5298 basic_block last_processed_bb, curr_bb = NULL;
5299 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5300 bitmap to_process;
5301 unsigned int j;
5302 bitmap_iterator bi;
5303 bool head_p, after_p;
5305 change_p = false;
5306 curr_usage_insns_check++;
5307 reloads_num = calls_num = 0;
5308 bitmap_clear (&check_only_regs);
5309 last_processed_bb = NULL;
5310 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5311 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5312 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5313 /* We don't process new insns generated in the loop. */
5314 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5316 prev_insn = PREV_INSN (curr_insn);
5317 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5318 curr_bb = BLOCK_FOR_INSN (curr_insn);
5319 if (last_processed_bb != curr_bb)
5321 /* We are at the end of BB. Add qualified living
5322 pseudos for potential splitting. */
5323 to_process = df_get_live_out (curr_bb);
5324 if (last_processed_bb != NULL)
5326 /* We are somewhere in the middle of EBB. */
5327 get_live_on_other_edges (curr_bb, last_processed_bb,
5328 &temp_bitmap);
5329 to_process = &temp_bitmap;
5331 last_processed_bb = curr_bb;
5332 last_insn = get_last_insertion_point (curr_bb);
5333 after_p = (! JUMP_P (last_insn)
5334 && (! CALL_P (last_insn)
5335 || (find_reg_note (last_insn,
5336 REG_NORETURN, NULL_RTX) == NULL_RTX
5337 && ! SIBLING_CALL_P (last_insn))));
5338 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5339 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5341 if ((int) j >= lra_constraint_new_regno_start)
5342 break;
5343 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5345 if (j < FIRST_PSEUDO_REGISTER)
5346 SET_HARD_REG_BIT (live_hard_regs, j);
5347 else
5348 add_to_hard_reg_set (&live_hard_regs,
5349 PSEUDO_REGNO_MODE (j),
5350 reg_renumber[j]);
5351 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5355 src_regno = dst_regno = -1;
5356 if (NONDEBUG_INSN_P (curr_insn)
5357 && (set = single_set (curr_insn)) != NULL_RTX
5358 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5360 src_regno = REGNO (SET_SRC (set));
5361 dst_regno = REGNO (SET_DEST (set));
5363 update_reloads_num_p = true;
5364 if (src_regno < lra_constraint_new_regno_start
5365 && src_regno >= FIRST_PSEUDO_REGISTER
5366 && reg_renumber[src_regno] < 0
5367 && dst_regno >= lra_constraint_new_regno_start
5368 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5370 /* 'reload_pseudo <- original_pseudo'. */
5371 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5372 reloads_num++;
5373 update_reloads_num_p = false;
5374 succ_p = false;
5375 if (usage_insns[src_regno].check == curr_usage_insns_check
5376 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5377 succ_p = inherit_reload_reg (false, src_regno, cl,
5378 curr_insn, next_usage_insns);
5379 if (succ_p)
5380 change_p = true;
5381 else
5382 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5383 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5384 IOR_HARD_REG_SET (potential_reload_hard_regs,
5385 reg_class_contents[cl]);
5387 else if (src_regno >= lra_constraint_new_regno_start
5388 && dst_regno < lra_constraint_new_regno_start
5389 && dst_regno >= FIRST_PSEUDO_REGISTER
5390 && reg_renumber[dst_regno] < 0
5391 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5392 && usage_insns[dst_regno].check == curr_usage_insns_check
5393 && (next_usage_insns
5394 = usage_insns[dst_regno].insns) != NULL_RTX)
5396 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5397 reloads_num++;
5398 update_reloads_num_p = false;
5399 /* 'original_pseudo <- reload_pseudo'. */
5400 if (! JUMP_P (curr_insn)
5401 && inherit_reload_reg (true, dst_regno, cl,
5402 curr_insn, next_usage_insns))
5403 change_p = true;
5404 /* Invalidate. */
5405 usage_insns[dst_regno].check = 0;
5406 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5407 IOR_HARD_REG_SET (potential_reload_hard_regs,
5408 reg_class_contents[cl]);
5410 else if (INSN_P (curr_insn))
5412 int iter;
5413 int max_uid = get_max_uid ();
5415 curr_id = lra_get_insn_recog_data (curr_insn);
5416 curr_static_id = curr_id->insn_static_data;
5417 to_inherit_num = 0;
5418 /* Process insn definitions. */
5419 for (iter = 0; iter < 2; iter++)
5420 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5421 reg != NULL;
5422 reg = reg->next)
5423 if (reg->type != OP_IN
5424 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5426 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5427 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5428 && usage_insns[dst_regno].check == curr_usage_insns_check
5429 && (next_usage_insns
5430 = usage_insns[dst_regno].insns) != NULL_RTX)
5432 struct lra_insn_reg *r;
5434 for (r = curr_id->regs; r != NULL; r = r->next)
5435 if (r->type != OP_OUT && r->regno == dst_regno)
5436 break;
5437 /* Don't do inheritance if the pseudo is also
5438 used in the insn. */
5439 if (r == NULL)
5440 /* We can not do inheritance right now
5441 because the current insn reg info (chain
5442 regs) can change after that. */
5443 add_to_inherit (dst_regno, next_usage_insns);
5445 /* We can not process one reg twice here because of
5446 usage_insns invalidation. */
5447 if ((dst_regno < FIRST_PSEUDO_REGISTER
5448 || reg_renumber[dst_regno] >= 0)
5449 && ! reg->subreg_p && reg->type != OP_IN)
5451 HARD_REG_SET s;
5453 if (split_if_necessary (dst_regno, reg->biggest_mode,
5454 potential_reload_hard_regs,
5455 false, curr_insn, max_uid))
5456 change_p = true;
5457 CLEAR_HARD_REG_SET (s);
5458 if (dst_regno < FIRST_PSEUDO_REGISTER)
5459 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5460 else
5461 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5462 reg_renumber[dst_regno]);
5463 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5465 /* We should invalidate potential inheritance or
5466 splitting for the current insn usages to the next
5467 usage insns (see code below) as the output pseudo
5468 prevents this. */
5469 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5470 && reg_renumber[dst_regno] < 0)
5471 || (reg->type == OP_OUT && ! reg->subreg_p
5472 && (dst_regno < FIRST_PSEUDO_REGISTER
5473 || reg_renumber[dst_regno] >= 0)))
5475 /* Invalidate and mark definitions. */
5476 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5477 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5478 else
5480 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5481 for (i = 0; i < nregs; i++)
5482 usage_insns[dst_regno + i].check
5483 = -(int) INSN_UID (curr_insn);
5487 /* Process clobbered call regs. */
5488 if (curr_id->arg_hard_regs != NULL)
5489 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5490 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5491 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
5492 = -(int) INSN_UID (curr_insn);
5493 if (! JUMP_P (curr_insn))
5494 for (i = 0; i < to_inherit_num; i++)
5495 if (inherit_reload_reg (true, to_inherit[i].regno,
5496 ALL_REGS, curr_insn,
5497 to_inherit[i].insns))
5498 change_p = true;
5499 if (CALL_P (curr_insn))
5501 rtx cheap, pat, dest;
5502 rtx_insn *restore;
5503 int regno, hard_regno;
5505 calls_num++;
5506 if ((cheap = find_reg_note (curr_insn,
5507 REG_RETURNED, NULL_RTX)) != NULL_RTX
5508 && ((cheap = XEXP (cheap, 0)), true)
5509 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5510 && (hard_regno = reg_renumber[regno]) >= 0
5511 /* If there are pending saves/restores, the
5512 optimization is not worth. */
5513 && usage_insns[regno].calls_num == calls_num - 1
5514 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5516 /* Restore the pseudo from the call result as
5517 REG_RETURNED note says that the pseudo value is
5518 in the call result and the pseudo is an argument
5519 of the call. */
5520 pat = PATTERN (curr_insn);
5521 if (GET_CODE (pat) == PARALLEL)
5522 pat = XVECEXP (pat, 0, 0);
5523 dest = SET_DEST (pat);
5524 /* For multiple return values dest is PARALLEL.
5525 Currently we handle only single return value case. */
5526 if (REG_P (dest))
5528 start_sequence ();
5529 emit_move_insn (cheap, copy_rtx (dest));
5530 restore = get_insns ();
5531 end_sequence ();
5532 lra_process_new_insns (curr_insn, NULL, restore,
5533 "Inserting call parameter restore");
5534 /* We don't need to save/restore of the pseudo from
5535 this call. */
5536 usage_insns[regno].calls_num = calls_num;
5537 bitmap_set_bit (&check_only_regs, regno);
5541 to_inherit_num = 0;
5542 /* Process insn usages. */
5543 for (iter = 0; iter < 2; iter++)
5544 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5545 reg != NULL;
5546 reg = reg->next)
5547 if ((reg->type != OP_OUT
5548 || (reg->type == OP_OUT && reg->subreg_p))
5549 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5551 if (src_regno >= FIRST_PSEUDO_REGISTER
5552 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5554 if (usage_insns[src_regno].check == curr_usage_insns_check
5555 && (next_usage_insns
5556 = usage_insns[src_regno].insns) != NULL_RTX
5557 && NONDEBUG_INSN_P (curr_insn))
5558 add_to_inherit (src_regno, next_usage_insns);
5559 else if (usage_insns[src_regno].check
5560 != -(int) INSN_UID (curr_insn))
5561 /* Add usages but only if the reg is not set up
5562 in the same insn. */
5563 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5565 else if (src_regno < FIRST_PSEUDO_REGISTER
5566 || reg_renumber[src_regno] >= 0)
5568 bool before_p;
5569 rtx_insn *use_insn = curr_insn;
5571 before_p = (JUMP_P (curr_insn)
5572 || (CALL_P (curr_insn) && reg->type == OP_IN));
5573 if (NONDEBUG_INSN_P (curr_insn)
5574 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5575 && split_if_necessary (src_regno, reg->biggest_mode,
5576 potential_reload_hard_regs,
5577 before_p, curr_insn, max_uid))
5579 if (reg->subreg_p)
5580 lra_risky_transformations_p = true;
5581 change_p = true;
5582 /* Invalidate. */
5583 usage_insns[src_regno].check = 0;
5584 if (before_p)
5585 use_insn = PREV_INSN (curr_insn);
5587 if (NONDEBUG_INSN_P (curr_insn))
5589 if (src_regno < FIRST_PSEUDO_REGISTER)
5590 add_to_hard_reg_set (&live_hard_regs,
5591 reg->biggest_mode, src_regno);
5592 else
5593 add_to_hard_reg_set (&live_hard_regs,
5594 PSEUDO_REGNO_MODE (src_regno),
5595 reg_renumber[src_regno]);
5597 add_next_usage_insn (src_regno, use_insn, reloads_num);
5600 /* Process used call regs. */
5601 if (curr_id->arg_hard_regs != NULL)
5602 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5603 if (src_regno < FIRST_PSEUDO_REGISTER)
5605 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5606 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5608 for (i = 0; i < to_inherit_num; i++)
5610 src_regno = to_inherit[i].regno;
5611 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5612 curr_insn, to_inherit[i].insns))
5613 change_p = true;
5614 else
5615 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5618 if (update_reloads_num_p
5619 && NONDEBUG_INSN_P (curr_insn)
5620 && (set = single_set (curr_insn)) != NULL_RTX)
5622 int regno = -1;
5623 if ((REG_P (SET_DEST (set))
5624 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5625 && reg_renumber[regno] < 0
5626 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5627 || (REG_P (SET_SRC (set))
5628 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5629 && reg_renumber[regno] < 0
5630 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5632 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5633 reloads_num++;
5634 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5635 IOR_HARD_REG_SET (potential_reload_hard_regs,
5636 reg_class_contents[cl]);
5639 /* We reached the start of the current basic block. */
5640 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5641 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5643 /* We reached the beginning of the current block -- do
5644 rest of spliting in the current BB. */
5645 to_process = df_get_live_in (curr_bb);
5646 if (BLOCK_FOR_INSN (head) != curr_bb)
5648 /* We are somewhere in the middle of EBB. */
5649 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5650 curr_bb, &temp_bitmap);
5651 to_process = &temp_bitmap;
5653 head_p = true;
5654 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5656 if ((int) j >= lra_constraint_new_regno_start)
5657 break;
5658 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5659 && usage_insns[j].check == curr_usage_insns_check
5660 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5662 if (need_for_split_p (potential_reload_hard_regs, j))
5664 if (lra_dump_file != NULL && head_p)
5666 fprintf (lra_dump_file,
5667 " ----------------------------------\n");
5668 head_p = false;
5670 if (split_reg (false, j, bb_note (curr_bb),
5671 next_usage_insns))
5672 change_p = true;
5674 usage_insns[j].check = 0;
5679 return change_p;
5682 /* This value affects EBB forming. If probability of edge from EBB to
5683 a BB is not greater than the following value, we don't add the BB
5684 to EBB. */
5685 #define EBB_PROBABILITY_CUTOFF \
5686 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
5688 /* Current number of inheritance/split iteration. */
5689 int lra_inheritance_iter;
5691 /* Entry function for inheritance/split pass. */
5692 void
5693 lra_inheritance (void)
5695 int i;
5696 basic_block bb, start_bb;
5697 edge e;
5699 lra_inheritance_iter++;
5700 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5701 return;
5702 timevar_push (TV_LRA_INHERITANCE);
5703 if (lra_dump_file != NULL)
5704 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5705 lra_inheritance_iter);
5706 curr_usage_insns_check = 0;
5707 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5708 for (i = 0; i < lra_constraint_new_regno_start; i++)
5709 usage_insns[i].check = 0;
5710 bitmap_initialize (&check_only_regs, &reg_obstack);
5711 bitmap_initialize (&live_regs, &reg_obstack);
5712 bitmap_initialize (&temp_bitmap, &reg_obstack);
5713 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5714 FOR_EACH_BB_FN (bb, cfun)
5716 start_bb = bb;
5717 if (lra_dump_file != NULL)
5718 fprintf (lra_dump_file, "EBB");
5719 /* Form a EBB starting with BB. */
5720 bitmap_clear (&ebb_global_regs);
5721 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5722 for (;;)
5724 if (lra_dump_file != NULL)
5725 fprintf (lra_dump_file, " %d", bb->index);
5726 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5727 || LABEL_P (BB_HEAD (bb->next_bb)))
5728 break;
5729 e = find_fallthru_edge (bb->succs);
5730 if (! e)
5731 break;
5732 if (e->probability < EBB_PROBABILITY_CUTOFF)
5733 break;
5734 bb = bb->next_bb;
5736 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5737 if (lra_dump_file != NULL)
5738 fprintf (lra_dump_file, "\n");
5739 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5740 /* Remember that the EBB head and tail can change in
5741 inherit_in_ebb. */
5742 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5744 bitmap_clear (&ebb_global_regs);
5745 bitmap_clear (&temp_bitmap);
5746 bitmap_clear (&live_regs);
5747 bitmap_clear (&check_only_regs);
5748 free (usage_insns);
5750 timevar_pop (TV_LRA_INHERITANCE);
5755 /* This page contains code to undo failed inheritance/split
5756 transformations. */
5758 /* Current number of iteration undoing inheritance/split. */
5759 int lra_undo_inheritance_iter;
5761 /* Fix BB live info LIVE after removing pseudos created on pass doing
5762 inheritance/split which are REMOVED_PSEUDOS. */
5763 static void
5764 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5766 unsigned int regno;
5767 bitmap_iterator bi;
5769 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5770 if (bitmap_clear_bit (live, regno))
5771 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5774 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5775 number. */
5776 static int
5777 get_regno (rtx reg)
5779 if (GET_CODE (reg) == SUBREG)
5780 reg = SUBREG_REG (reg);
5781 if (REG_P (reg))
5782 return REGNO (reg);
5783 return -1;
5786 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5787 return true if we did any change. The undo transformations for
5788 inheritance looks like
5789 i <- i2
5790 p <- i => p <- i2
5791 or removing
5792 p <- i, i <- p, and i <- i3
5793 where p is original pseudo from which inheritance pseudo i was
5794 created, i and i3 are removed inheritance pseudos, i2 is another
5795 not removed inheritance pseudo. All split pseudos or other
5796 occurrences of removed inheritance pseudos are changed on the
5797 corresponding original pseudos.
5799 The function also schedules insns changed and created during
5800 inheritance/split pass for processing by the subsequent constraint
5801 pass. */
5802 static bool
5803 remove_inheritance_pseudos (bitmap remove_pseudos)
5805 basic_block bb;
5806 int regno, sregno, prev_sregno, dregno, restore_regno;
5807 rtx set, prev_set;
5808 rtx_insn *prev_insn;
5809 bool change_p, done_p;
5811 change_p = ! bitmap_empty_p (remove_pseudos);
5812 /* We can not finish the function right away if CHANGE_P is true
5813 because we need to marks insns affected by previous
5814 inheritance/split pass for processing by the subsequent
5815 constraint pass. */
5816 FOR_EACH_BB_FN (bb, cfun)
5818 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5819 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5820 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5822 if (! INSN_P (curr_insn))
5823 continue;
5824 done_p = false;
5825 sregno = dregno = -1;
5826 if (change_p && NONDEBUG_INSN_P (curr_insn)
5827 && (set = single_set (curr_insn)) != NULL_RTX)
5829 dregno = get_regno (SET_DEST (set));
5830 sregno = get_regno (SET_SRC (set));
5833 if (sregno >= 0 && dregno >= 0)
5835 if ((bitmap_bit_p (remove_pseudos, sregno)
5836 && (lra_reg_info[sregno].restore_regno == dregno
5837 || (bitmap_bit_p (remove_pseudos, dregno)
5838 && (lra_reg_info[sregno].restore_regno
5839 == lra_reg_info[dregno].restore_regno))))
5840 || (bitmap_bit_p (remove_pseudos, dregno)
5841 && lra_reg_info[dregno].restore_regno == sregno))
5842 /* One of the following cases:
5843 original <- removed inheritance pseudo
5844 removed inherit pseudo <- another removed inherit pseudo
5845 removed inherit pseudo <- original pseudo
5847 removed_split_pseudo <- original_reg
5848 original_reg <- removed_split_pseudo */
5850 if (lra_dump_file != NULL)
5852 fprintf (lra_dump_file, " Removing %s:\n",
5853 bitmap_bit_p (&lra_split_regs, sregno)
5854 || bitmap_bit_p (&lra_split_regs, dregno)
5855 ? "split" : "inheritance");
5856 dump_insn_slim (lra_dump_file, curr_insn);
5858 lra_set_insn_deleted (curr_insn);
5859 done_p = true;
5861 else if (bitmap_bit_p (remove_pseudos, sregno)
5862 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5864 /* Search the following pattern:
5865 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5866 original_pseudo <- inherit_or_split_pseudo1
5867 where the 2nd insn is the current insn and
5868 inherit_or_split_pseudo2 is not removed. If it is found,
5869 change the current insn onto:
5870 original_pseudo <- inherit_or_split_pseudo2. */
5871 for (prev_insn = PREV_INSN (curr_insn);
5872 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5873 prev_insn = PREV_INSN (prev_insn))
5875 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5876 && (prev_set = single_set (prev_insn)) != NULL_RTX
5877 /* There should be no subregs in insn we are
5878 searching because only the original reg might
5879 be in subreg when we changed the mode of
5880 load/store for splitting. */
5881 && REG_P (SET_DEST (prev_set))
5882 && REG_P (SET_SRC (prev_set))
5883 && (int) REGNO (SET_DEST (prev_set)) == sregno
5884 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5885 >= FIRST_PSEUDO_REGISTER)
5886 /* As we consider chain of inheritance or
5887 splitting described in above comment we should
5888 check that sregno and prev_sregno were
5889 inheritance/split pseudos created from the
5890 same original regno. */
5891 && (lra_reg_info[sregno].restore_regno
5892 == lra_reg_info[prev_sregno].restore_regno)
5893 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5895 lra_assert (GET_MODE (SET_SRC (prev_set))
5896 == GET_MODE (regno_reg_rtx[sregno]));
5897 if (GET_CODE (SET_SRC (set)) == SUBREG)
5898 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5899 else
5900 SET_SRC (set) = SET_SRC (prev_set);
5901 /* As we are finishing with processing the insn
5902 here, check the destination too as it might
5903 inheritance pseudo for another pseudo. */
5904 if (bitmap_bit_p (remove_pseudos, dregno)
5905 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
5906 && (restore_regno
5907 = lra_reg_info[dregno].restore_regno) >= 0)
5909 if (GET_CODE (SET_DEST (set)) == SUBREG)
5910 SUBREG_REG (SET_DEST (set))
5911 = regno_reg_rtx[restore_regno];
5912 else
5913 SET_DEST (set) = regno_reg_rtx[restore_regno];
5915 lra_push_insn_and_update_insn_regno_info (curr_insn);
5916 lra_set_used_insn_alternative_by_uid
5917 (INSN_UID (curr_insn), -1);
5918 done_p = true;
5919 if (lra_dump_file != NULL)
5921 fprintf (lra_dump_file, " Change reload insn:\n");
5922 dump_insn_slim (lra_dump_file, curr_insn);
5927 if (! done_p)
5929 struct lra_insn_reg *reg;
5930 bool restored_regs_p = false;
5931 bool kept_regs_p = false;
5933 curr_id = lra_get_insn_recog_data (curr_insn);
5934 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5936 regno = reg->regno;
5937 restore_regno = lra_reg_info[regno].restore_regno;
5938 if (restore_regno >= 0)
5940 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5942 lra_substitute_pseudo_within_insn
5943 (curr_insn, regno, regno_reg_rtx[restore_regno],
5944 false);
5945 restored_regs_p = true;
5947 else
5948 kept_regs_p = true;
5951 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5953 /* The instruction has changed since the previous
5954 constraints pass. */
5955 lra_push_insn_and_update_insn_regno_info (curr_insn);
5956 lra_set_used_insn_alternative_by_uid
5957 (INSN_UID (curr_insn), -1);
5959 else if (restored_regs_p)
5960 /* The instruction has been restored to the form that
5961 it had during the previous constraints pass. */
5962 lra_update_insn_regno_info (curr_insn);
5963 if (restored_regs_p && lra_dump_file != NULL)
5965 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5966 dump_insn_slim (lra_dump_file, curr_insn);
5971 return change_p;
5974 /* If optional reload pseudos failed to get a hard register or was not
5975 inherited, it is better to remove optional reloads. We do this
5976 transformation after undoing inheritance to figure out necessity to
5977 remove optional reloads easier. Return true if we do any
5978 change. */
5979 static bool
5980 undo_optional_reloads (void)
5982 bool change_p, keep_p;
5983 unsigned int regno, uid;
5984 bitmap_iterator bi, bi2;
5985 rtx_insn *insn;
5986 rtx set, src, dest;
5987 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5989 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5990 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5991 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5993 keep_p = false;
5994 /* Keep optional reloads from previous subpasses. */
5995 if (lra_reg_info[regno].restore_regno < 0
5996 /* If the original pseudo changed its allocation, just
5997 removing the optional pseudo is dangerous as the original
5998 pseudo will have longer live range. */
5999 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
6000 keep_p = true;
6001 else if (reg_renumber[regno] >= 0)
6002 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
6004 insn = lra_insn_recog_data[uid]->insn;
6005 if ((set = single_set (insn)) == NULL_RTX)
6006 continue;
6007 src = SET_SRC (set);
6008 dest = SET_DEST (set);
6009 if (! REG_P (src) || ! REG_P (dest))
6010 continue;
6011 if (REGNO (dest) == regno
6012 /* Ignore insn for optional reloads itself. */
6013 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
6014 /* Check only inheritance on last inheritance pass. */
6015 && (int) REGNO (src) >= new_regno_start
6016 /* Check that the optional reload was inherited. */
6017 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6019 keep_p = true;
6020 break;
6023 if (keep_p)
6025 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6026 if (lra_dump_file != NULL)
6027 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6030 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6031 bitmap_initialize (&insn_bitmap, &reg_obstack);
6032 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6034 if (lra_dump_file != NULL)
6035 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6036 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6037 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6039 insn = lra_insn_recog_data[uid]->insn;
6040 if ((set = single_set (insn)) != NULL_RTX)
6042 src = SET_SRC (set);
6043 dest = SET_DEST (set);
6044 if (REG_P (src) && REG_P (dest)
6045 && ((REGNO (src) == regno
6046 && (lra_reg_info[regno].restore_regno
6047 == (int) REGNO (dest)))
6048 || (REGNO (dest) == regno
6049 && (lra_reg_info[regno].restore_regno
6050 == (int) REGNO (src)))))
6052 if (lra_dump_file != NULL)
6054 fprintf (lra_dump_file, " Deleting move %u\n",
6055 INSN_UID (insn));
6056 dump_insn_slim (lra_dump_file, insn);
6058 lra_set_insn_deleted (insn);
6059 continue;
6061 /* We should not worry about generation memory-memory
6062 moves here as if the corresponding inheritance did
6063 not work (inheritance pseudo did not get a hard reg),
6064 we remove the inheritance pseudo and the optional
6065 reload. */
6067 lra_substitute_pseudo_within_insn
6068 (insn, regno, regno_reg_rtx[lra_reg_info[regno].restore_regno],
6069 false);
6070 lra_update_insn_regno_info (insn);
6071 if (lra_dump_file != NULL)
6073 fprintf (lra_dump_file,
6074 " Restoring original insn:\n");
6075 dump_insn_slim (lra_dump_file, insn);
6079 /* Clear restore_regnos. */
6080 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6081 lra_reg_info[regno].restore_regno = -1;
6082 bitmap_clear (&insn_bitmap);
6083 bitmap_clear (&removed_optional_reload_pseudos);
6084 return change_p;
6087 /* Entry function for undoing inheritance/split transformation. Return true
6088 if we did any RTL change in this pass. */
6089 bool
6090 lra_undo_inheritance (void)
6092 unsigned int regno;
6093 int restore_regno, hard_regno;
6094 int n_all_inherit, n_inherit, n_all_split, n_split;
6095 bitmap_head remove_pseudos;
6096 bitmap_iterator bi;
6097 bool change_p;
6099 lra_undo_inheritance_iter++;
6100 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6101 return false;
6102 if (lra_dump_file != NULL)
6103 fprintf (lra_dump_file,
6104 "\n********** Undoing inheritance #%d: **********\n\n",
6105 lra_undo_inheritance_iter);
6106 bitmap_initialize (&remove_pseudos, &reg_obstack);
6107 n_inherit = n_all_inherit = 0;
6108 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6109 if (lra_reg_info[regno].restore_regno >= 0)
6111 n_all_inherit++;
6112 if (reg_renumber[regno] < 0
6113 /* If the original pseudo changed its allocation, just
6114 removing inheritance is dangerous as for changing
6115 allocation we used shorter live-ranges. */
6116 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6117 bitmap_set_bit (&remove_pseudos, regno);
6118 else
6119 n_inherit++;
6121 if (lra_dump_file != NULL && n_all_inherit != 0)
6122 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6123 n_inherit, n_all_inherit,
6124 (double) n_inherit / n_all_inherit * 100);
6125 n_split = n_all_split = 0;
6126 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6127 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6129 n_all_split++;
6130 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6131 ? reg_renumber[restore_regno] : restore_regno);
6132 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6133 bitmap_set_bit (&remove_pseudos, regno);
6134 else
6136 n_split++;
6137 if (lra_dump_file != NULL)
6138 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6139 regno, restore_regno);
6142 if (lra_dump_file != NULL && n_all_split != 0)
6143 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6144 n_split, n_all_split,
6145 (double) n_split / n_all_split * 100);
6146 change_p = remove_inheritance_pseudos (&remove_pseudos);
6147 bitmap_clear (&remove_pseudos);
6148 /* Clear restore_regnos. */
6149 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6150 lra_reg_info[regno].restore_regno = -1;
6151 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6152 lra_reg_info[regno].restore_regno = -1;
6153 change_p = undo_optional_reloads () || change_p;
6154 return change_p;