* g++.dg/cpp/ucn-1.C: Fix typo.
[official-gcc.git] / gcc / lra-constraints.c
blob4670e811b3a9bb0288bf40f91157e3152dbaa9d0
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 "target.h"
114 #include "rtl.h"
115 #include "tree.h"
116 #include "predict.h"
117 #include "df.h"
118 #include "tm_p.h"
119 #include "expmed.h"
120 #include "optabs.h"
121 #include "regs.h"
122 #include "ira.h"
123 #include "recog.h"
124 #include "output.h"
125 #include "addresses.h"
126 #include "expr.h"
127 #include "cfgrtl.h"
128 #include "rtl-error.h"
129 #include "params.h"
130 #include "lra.h"
131 #include "lra-int.h"
132 #include "print-rtl.h"
134 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
135 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
136 reload insns. */
137 static int bb_reload_num;
139 /* The current insn being processed and corresponding its single set
140 (NULL otherwise), its data (basic block, the insn data, the insn
141 static data, and the mode of each operand). */
142 static rtx_insn *curr_insn;
143 static rtx curr_insn_set;
144 static basic_block curr_bb;
145 static lra_insn_recog_data_t curr_id;
146 static struct lra_static_insn_data *curr_static_id;
147 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
148 /* Mode of the register substituted by its equivalence with VOIDmode
149 (e.g. constant) and whose subreg is given operand of the current
150 insn. VOIDmode in all other cases. */
151 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
155 /* Start numbers for new registers and insns at the current constraints
156 pass start. */
157 static int new_regno_start;
158 static int new_insn_uid_start;
160 /* If LOC is nonnull, strip any outer subreg from it. */
161 static inline rtx *
162 strip_subreg (rtx *loc)
164 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
167 /* Return hard regno of REGNO or if it is was not assigned to a hard
168 register, use a hard register from its allocno class. */
169 static int
170 get_try_hard_regno (int regno)
172 int hard_regno;
173 enum reg_class rclass;
175 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
176 hard_regno = lra_get_regno_hard_regno (regno);
177 if (hard_regno >= 0)
178 return hard_regno;
179 rclass = lra_get_allocno_class (regno);
180 if (rclass == NO_REGS)
181 return -1;
182 return ira_class_hard_regs[rclass][0];
185 /* Return final hard regno (plus offset) which will be after
186 elimination. We do this for matching constraints because the final
187 hard regno could have a different class. */
188 static int
189 get_final_hard_regno (int hard_regno, int offset)
191 if (hard_regno < 0)
192 return hard_regno;
193 hard_regno = lra_get_elimination_hard_regno (hard_regno);
194 return hard_regno + offset;
197 /* Return hard regno of X after removing subreg and making
198 elimination. If X is not a register or subreg of register, return
199 -1. For pseudo use its assignment. */
200 static int
201 get_hard_regno (rtx x)
203 rtx reg;
204 int offset, hard_regno;
206 reg = x;
207 if (GET_CODE (x) == SUBREG)
208 reg = SUBREG_REG (x);
209 if (! REG_P (reg))
210 return -1;
211 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
212 hard_regno = lra_get_regno_hard_regno (hard_regno);
213 if (hard_regno < 0)
214 return -1;
215 offset = 0;
216 if (GET_CODE (x) == SUBREG)
217 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
218 SUBREG_BYTE (x), GET_MODE (x));
219 return get_final_hard_regno (hard_regno, offset);
222 /* If REGNO is a hard register or has been allocated a hard register,
223 return the class of that register. If REGNO is a reload pseudo
224 created by the current constraints pass, return its allocno class.
225 Return NO_REGS otherwise. */
226 static enum reg_class
227 get_reg_class (int regno)
229 int hard_regno;
231 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
232 hard_regno = lra_get_regno_hard_regno (regno);
233 if (hard_regno >= 0)
235 hard_regno = get_final_hard_regno (hard_regno, 0);
236 return REGNO_REG_CLASS (hard_regno);
238 if (regno >= new_regno_start)
239 return lra_get_allocno_class (regno);
240 return NO_REGS;
243 /* Return true if REG satisfies (or will satisfy) reg class constraint
244 CL. Use elimination first if REG is a hard register. If REG is a
245 reload pseudo created by this constraints pass, assume that it will
246 be allocated a hard register from its allocno class, but allow that
247 class to be narrowed to CL if it is currently a superset of CL.
249 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
250 REGNO (reg), or NO_REGS if no change in its class was needed. */
251 static bool
252 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
254 enum reg_class rclass, common_class;
255 machine_mode reg_mode;
256 int class_size, hard_regno, nregs, i, j;
257 int regno = REGNO (reg);
259 if (new_class != NULL)
260 *new_class = NO_REGS;
261 if (regno < FIRST_PSEUDO_REGISTER)
263 rtx final_reg = reg;
264 rtx *final_loc = &final_reg;
266 lra_eliminate_reg_if_possible (final_loc);
267 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
269 reg_mode = GET_MODE (reg);
270 rclass = get_reg_class (regno);
271 if (regno < new_regno_start
272 /* Do not allow the constraints for reload instructions to
273 influence the classes of new pseudos. These reloads are
274 typically moves that have many alternatives, and restricting
275 reload pseudos for one alternative may lead to situations
276 where other reload pseudos are no longer allocatable. */
277 || (INSN_UID (curr_insn) >= new_insn_uid_start
278 && curr_insn_set != NULL
279 && ((OBJECT_P (SET_SRC (curr_insn_set))
280 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
281 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
282 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
283 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
284 /* When we don't know what class will be used finally for reload
285 pseudos, we use ALL_REGS. */
286 return ((regno >= new_regno_start && rclass == ALL_REGS)
287 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
288 && ! hard_reg_set_subset_p (reg_class_contents[cl],
289 lra_no_alloc_regs)));
290 else
292 common_class = ira_reg_class_subset[rclass][cl];
293 if (new_class != NULL)
294 *new_class = common_class;
295 if (hard_reg_set_subset_p (reg_class_contents[common_class],
296 lra_no_alloc_regs))
297 return false;
298 /* Check that there are enough allocatable regs. */
299 class_size = ira_class_hard_regs_num[common_class];
300 for (i = 0; i < class_size; i++)
302 hard_regno = ira_class_hard_regs[common_class][i];
303 nregs = hard_regno_nregs[hard_regno][reg_mode];
304 if (nregs == 1)
305 return true;
306 for (j = 0; j < nregs; j++)
307 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
308 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
309 hard_regno + j))
310 break;
311 if (j >= nregs)
312 return true;
314 return false;
318 /* Return true if REGNO satisfies a memory constraint. */
319 static bool
320 in_mem_p (int regno)
322 return get_reg_class (regno) == NO_REGS;
325 /* Return 1 if ADDR is a valid memory address for mode MODE in address
326 space AS, and check that each pseudo has the proper kind of hard
327 reg. */
328 static int
329 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
330 rtx addr, addr_space_t as)
332 #ifdef GO_IF_LEGITIMATE_ADDRESS
333 lra_assert (ADDR_SPACE_GENERIC_P (as));
334 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
335 return 0;
337 win:
338 return 1;
339 #else
340 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
341 #endif
344 namespace {
345 /* Temporarily eliminates registers in an address (for the lifetime of
346 the object). */
347 class address_eliminator {
348 public:
349 address_eliminator (struct address_info *ad);
350 ~address_eliminator ();
352 private:
353 struct address_info *m_ad;
354 rtx *m_base_loc;
355 rtx m_base_reg;
356 rtx *m_index_loc;
357 rtx m_index_reg;
361 address_eliminator::address_eliminator (struct address_info *ad)
362 : m_ad (ad),
363 m_base_loc (strip_subreg (ad->base_term)),
364 m_base_reg (NULL_RTX),
365 m_index_loc (strip_subreg (ad->index_term)),
366 m_index_reg (NULL_RTX)
368 if (m_base_loc != NULL)
370 m_base_reg = *m_base_loc;
371 lra_eliminate_reg_if_possible (m_base_loc);
372 if (m_ad->base_term2 != NULL)
373 *m_ad->base_term2 = *m_ad->base_term;
375 if (m_index_loc != NULL)
377 m_index_reg = *m_index_loc;
378 lra_eliminate_reg_if_possible (m_index_loc);
382 address_eliminator::~address_eliminator ()
384 if (m_base_loc && *m_base_loc != m_base_reg)
386 *m_base_loc = m_base_reg;
387 if (m_ad->base_term2 != NULL)
388 *m_ad->base_term2 = *m_ad->base_term;
390 if (m_index_loc && *m_index_loc != m_index_reg)
391 *m_index_loc = m_index_reg;
394 /* Return true if the eliminated form of AD is a legitimate target address. */
395 static bool
396 valid_address_p (struct address_info *ad)
398 address_eliminator eliminator (ad);
399 return valid_address_p (ad->mode, *ad->outer, ad->as);
402 /* Return true if the eliminated form of memory reference OP satisfies
403 extra memory constraint CONSTRAINT. */
404 static bool
405 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
407 struct address_info ad;
409 decompose_mem_address (&ad, op);
410 address_eliminator eliminator (&ad);
411 return constraint_satisfied_p (op, constraint);
414 /* Return true if the eliminated form of address AD satisfies extra
415 address constraint CONSTRAINT. */
416 static bool
417 satisfies_address_constraint_p (struct address_info *ad,
418 enum constraint_num constraint)
420 address_eliminator eliminator (ad);
421 return constraint_satisfied_p (*ad->outer, constraint);
424 /* Return true if the eliminated form of address OP satisfies extra
425 address constraint CONSTRAINT. */
426 static bool
427 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
429 struct address_info ad;
431 decompose_lea_address (&ad, &op);
432 return satisfies_address_constraint_p (&ad, constraint);
435 /* Initiate equivalences for LRA. As we keep original equivalences
436 before any elimination, we need to make copies otherwise any change
437 in insns might change the equivalences. */
438 void
439 lra_init_equiv (void)
441 ira_expand_reg_equiv ();
442 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
444 rtx res;
446 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
447 ira_reg_equiv[i].memory = copy_rtx (res);
448 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
449 ira_reg_equiv[i].invariant = copy_rtx (res);
453 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
455 /* Update equivalence for REGNO. We need to this as the equivalence
456 might contain other pseudos which are changed by their
457 equivalences. */
458 static void
459 update_equiv (int regno)
461 rtx x;
463 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
464 ira_reg_equiv[regno].memory
465 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
466 NULL_RTX);
467 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
468 ira_reg_equiv[regno].invariant
469 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
470 NULL_RTX);
473 /* If we have decided to substitute X with another value, return that
474 value, otherwise return X. */
475 static rtx
476 get_equiv (rtx x)
478 int regno;
479 rtx res;
481 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
482 || ! ira_reg_equiv[regno].defined_p
483 || ! ira_reg_equiv[regno].profitable_p
484 || lra_get_regno_hard_regno (regno) >= 0)
485 return x;
486 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
488 if (targetm.cannot_substitute_mem_equiv_p (res))
489 return x;
490 return res;
492 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
493 return res;
494 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
495 return res;
496 gcc_unreachable ();
499 /* If we have decided to substitute X with the equivalent value,
500 return that value after elimination for INSN, otherwise return
501 X. */
502 static rtx
503 get_equiv_with_elimination (rtx x, rtx_insn *insn)
505 rtx res = get_equiv (x);
507 if (x == res || CONSTANT_P (res))
508 return res;
509 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
510 false, false, 0, true);
513 /* Set up curr_operand_mode. */
514 static void
515 init_curr_operand_mode (void)
517 int nop = curr_static_id->n_operands;
518 for (int i = 0; i < nop; i++)
520 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
521 if (mode == VOIDmode)
523 /* The .md mode for address operands is the mode of the
524 addressed value rather than the mode of the address itself. */
525 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
526 mode = Pmode;
527 else
528 mode = curr_static_id->operand[i].mode;
530 curr_operand_mode[i] = mode;
536 /* The page contains code to reuse input reloads. */
538 /* Structure describes input reload of the current insns. */
539 struct input_reload
541 /* Reloaded value. */
542 rtx input;
543 /* Reload pseudo used. */
544 rtx reg;
547 /* The number of elements in the following array. */
548 static int curr_insn_input_reloads_num;
549 /* Array containing info about input reloads. It is used to find the
550 same input reload and reuse the reload pseudo in this case. */
551 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
553 /* Initiate data concerning reuse of input reloads for the current
554 insn. */
555 static void
556 init_curr_insn_input_reloads (void)
558 curr_insn_input_reloads_num = 0;
561 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
562 created input reload pseudo (only if TYPE is not OP_OUT). Don't
563 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
564 wrapped up in SUBREG. The result pseudo is returned through
565 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
566 reused the already created input reload pseudo. Use TITLE to
567 describe new registers for debug purposes. */
568 static bool
569 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
570 enum reg_class rclass, bool in_subreg_p,
571 const char *title, rtx *result_reg)
573 int i, regno;
574 enum reg_class new_class;
576 if (type == OP_OUT)
578 *result_reg
579 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
580 return true;
582 /* Prevent reuse value of expression with side effects,
583 e.g. volatile memory. */
584 if (! side_effects_p (original))
585 for (i = 0; i < curr_insn_input_reloads_num; i++)
586 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
587 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
589 rtx reg = curr_insn_input_reloads[i].reg;
590 regno = REGNO (reg);
591 /* If input is equal to original and both are VOIDmode,
592 GET_MODE (reg) might be still different from mode.
593 Ensure we don't return *result_reg with wrong mode. */
594 if (GET_MODE (reg) != mode)
596 if (in_subreg_p)
597 continue;
598 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
599 continue;
600 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
601 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
602 continue;
604 *result_reg = reg;
605 if (lra_dump_file != NULL)
607 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
608 dump_value_slim (lra_dump_file, original, 1);
610 if (new_class != lra_get_allocno_class (regno))
611 lra_change_class (regno, new_class, ", change to", false);
612 if (lra_dump_file != NULL)
613 fprintf (lra_dump_file, "\n");
614 return false;
616 *result_reg = lra_create_new_reg (mode, original, rclass, title);
617 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
618 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
619 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
620 return true;
625 /* The page contains code to extract memory address parts. */
627 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
628 static inline bool
629 ok_for_index_p_nonstrict (rtx reg)
631 unsigned regno = REGNO (reg);
633 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
636 /* A version of regno_ok_for_base_p for use here, when all pseudos
637 should count as OK. Arguments as for regno_ok_for_base_p. */
638 static inline bool
639 ok_for_base_p_nonstrict (rtx reg, machine_mode mode, addr_space_t as,
640 enum rtx_code outer_code, enum rtx_code index_code)
642 unsigned regno = REGNO (reg);
644 if (regno >= FIRST_PSEUDO_REGISTER)
645 return true;
646 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
651 /* The page contains major code to choose the current insn alternative
652 and generate reloads for it. */
654 /* Return the offset from REGNO of the least significant register
655 in (reg:MODE REGNO).
657 This function is used to tell whether two registers satisfy
658 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
660 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
661 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
663 lra_constraint_offset (int regno, machine_mode mode)
665 lra_assert (regno < FIRST_PSEUDO_REGISTER);
666 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
667 && SCALAR_INT_MODE_P (mode))
668 return hard_regno_nregs[regno][mode] - 1;
669 return 0;
672 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
673 if they are the same hard reg, and has special hacks for
674 auto-increment and auto-decrement. This is specifically intended for
675 process_alt_operands to use in determining whether two operands
676 match. X is the operand whose number is the lower of the two.
678 It is supposed that X is the output operand and Y is the input
679 operand. Y_HARD_REGNO is the final hard regno of register Y or
680 register in subreg Y as we know it now. Otherwise, it is a
681 negative value. */
682 static bool
683 operands_match_p (rtx x, rtx y, int y_hard_regno)
685 int i;
686 RTX_CODE code = GET_CODE (x);
687 const char *fmt;
689 if (x == y)
690 return true;
691 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
692 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
694 int j;
696 i = get_hard_regno (x);
697 if (i < 0)
698 goto slow;
700 if ((j = y_hard_regno) < 0)
701 goto slow;
703 i += lra_constraint_offset (i, GET_MODE (x));
704 j += lra_constraint_offset (j, GET_MODE (y));
706 return i == j;
709 /* If two operands must match, because they are really a single
710 operand of an assembler insn, then two post-increments are invalid
711 because the assembler insn would increment only once. On the
712 other hand, a post-increment matches ordinary indexing if the
713 post-increment is the output operand. */
714 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
715 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
717 /* Two pre-increments are invalid because the assembler insn would
718 increment only once. On the other hand, a pre-increment matches
719 ordinary indexing if the pre-increment is the input operand. */
720 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
721 || GET_CODE (y) == PRE_MODIFY)
722 return operands_match_p (x, XEXP (y, 0), -1);
724 slow:
726 if (code == REG && REG_P (y))
727 return REGNO (x) == REGNO (y);
729 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
730 && x == SUBREG_REG (y))
731 return true;
732 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
733 && SUBREG_REG (x) == y)
734 return true;
736 /* Now we have disposed of all the cases in which different rtx
737 codes can match. */
738 if (code != GET_CODE (y))
739 return false;
741 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
742 if (GET_MODE (x) != GET_MODE (y))
743 return false;
745 switch (code)
747 CASE_CONST_UNIQUE:
748 return false;
750 case LABEL_REF:
751 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
752 case SYMBOL_REF:
753 return XSTR (x, 0) == XSTR (y, 0);
755 default:
756 break;
759 /* Compare the elements. If any pair of corresponding elements fail
760 to match, return false for the whole things. */
762 fmt = GET_RTX_FORMAT (code);
763 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
765 int val, j;
766 switch (fmt[i])
768 case 'w':
769 if (XWINT (x, i) != XWINT (y, i))
770 return false;
771 break;
773 case 'i':
774 if (XINT (x, i) != XINT (y, i))
775 return false;
776 break;
778 case 'e':
779 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
780 if (val == 0)
781 return false;
782 break;
784 case '0':
785 break;
787 case 'E':
788 if (XVECLEN (x, i) != XVECLEN (y, i))
789 return false;
790 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
792 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
793 if (val == 0)
794 return false;
796 break;
798 /* It is believed that rtx's at this level will never
799 contain anything but integers and other rtx's, except for
800 within LABEL_REFs and SYMBOL_REFs. */
801 default:
802 gcc_unreachable ();
805 return true;
808 /* True if X is a constant that can be forced into the constant pool.
809 MODE is the mode of the operand, or VOIDmode if not known. */
810 #define CONST_POOL_OK_P(MODE, X) \
811 ((MODE) != VOIDmode \
812 && CONSTANT_P (X) \
813 && GET_CODE (X) != HIGH \
814 && !targetm.cannot_force_const_mem (MODE, X))
816 /* True if C is a non-empty register class that has too few registers
817 to be safely used as a reload target class. */
818 #define SMALL_REGISTER_CLASS_P(C) \
819 (ira_class_hard_regs_num [(C)] == 1 \
820 || (ira_class_hard_regs_num [(C)] >= 1 \
821 && targetm.class_likely_spilled_p (C)))
823 /* If REG is a reload pseudo, try to make its class satisfying CL. */
824 static void
825 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
827 enum reg_class rclass;
829 /* Do not make more accurate class from reloads generated. They are
830 mostly moves with a lot of constraints. Making more accurate
831 class may results in very narrow class and impossibility of find
832 registers for several reloads of one insn. */
833 if (INSN_UID (curr_insn) >= new_insn_uid_start)
834 return;
835 if (GET_CODE (reg) == SUBREG)
836 reg = SUBREG_REG (reg);
837 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
838 return;
839 if (in_class_p (reg, cl, &rclass) && rclass != cl)
840 lra_change_class (REGNO (reg), rclass, " Change to", true);
843 /* Generate reloads for matching OUT and INS (array of input operand
844 numbers with end marker -1) with reg class GOAL_CLASS. Add input
845 and output reloads correspondingly to the lists *BEFORE and *AFTER.
846 OUT might be negative. In this case we generate input reloads for
847 matched input operands INS. EARLY_CLOBBER_P is a flag that the
848 output operand is early clobbered for chosen alternative. */
849 static void
850 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
851 rtx_insn **before, rtx_insn **after, bool early_clobber_p)
853 int i, in;
854 rtx new_in_reg, new_out_reg, reg;
855 machine_mode inmode, outmode;
856 rtx in_rtx = *curr_id->operand_loc[ins[0]];
857 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
859 inmode = curr_operand_mode[ins[0]];
860 outmode = out < 0 ? inmode : curr_operand_mode[out];
861 push_to_sequence (*before);
862 if (inmode != outmode)
864 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
866 reg = new_in_reg
867 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
868 goal_class, "");
869 if (SCALAR_INT_MODE_P (inmode))
870 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
871 else
872 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
873 LRA_SUBREG_P (new_out_reg) = 1;
874 /* If the input reg is dying here, we can use the same hard
875 register for REG and IN_RTX. We do it only for original
876 pseudos as reload pseudos can die although original
877 pseudos still live where reload pseudos dies. */
878 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
879 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
880 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
882 else
884 reg = new_out_reg
885 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
886 goal_class, "");
887 if (SCALAR_INT_MODE_P (outmode))
888 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
889 else
890 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
891 /* NEW_IN_REG is non-paradoxical subreg. We don't want
892 NEW_OUT_REG living above. We add clobber clause for
893 this. This is just a temporary clobber. We can remove
894 it at the end of LRA work. */
895 rtx_insn *clobber = emit_clobber (new_out_reg);
896 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
897 LRA_SUBREG_P (new_in_reg) = 1;
898 if (GET_CODE (in_rtx) == SUBREG)
900 rtx subreg_reg = SUBREG_REG (in_rtx);
902 /* If SUBREG_REG is dying here and sub-registers IN_RTX
903 and NEW_IN_REG are similar, we can use the same hard
904 register for REG and SUBREG_REG. */
905 if (REG_P (subreg_reg)
906 && (int) REGNO (subreg_reg) < lra_new_regno_start
907 && GET_MODE (subreg_reg) == outmode
908 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
909 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
910 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
914 else
916 /* Pseudos have values -- see comments for lra_reg_info.
917 Different pseudos with the same value do not conflict even if
918 they live in the same place. When we create a pseudo we
919 assign value of original pseudo (if any) from which we
920 created the new pseudo. If we create the pseudo from the
921 input pseudo, the new pseudo will have no conflict with the
922 input pseudo which is wrong when the input pseudo lives after
923 the insn and as the new pseudo value is changed by the insn
924 output. Therefore we create the new pseudo from the output
925 except the case when we have single matched dying input
926 pseudo.
928 We cannot reuse the current output register because we might
929 have a situation like "a <- a op b", where the constraints
930 force the second input operand ("b") to match the output
931 operand ("a"). "b" must then be copied into a new register
932 so that it doesn't clobber the current value of "a".
934 We can not use the same value if the output pseudo is
935 early clobbered or the input pseudo is mentioned in the
936 output, e.g. as an address part in memory, because
937 output reload will actually extend the pseudo liveness.
938 We don't care about eliminable hard regs here as we are
939 interesting only in pseudos. */
941 new_in_reg = new_out_reg
942 = (! early_clobber_p && ins[1] < 0 && REG_P (in_rtx)
943 && (int) REGNO (in_rtx) < lra_new_regno_start
944 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
945 && (out < 0 || regno_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
946 ? lra_create_new_reg (inmode, in_rtx, goal_class, "")
947 : lra_create_new_reg_with_unique_value (outmode, out_rtx,
948 goal_class, ""));
950 /* In operand can be got from transformations before processing insn
951 constraints. One example of such transformations is subreg
952 reloading (see function simplify_operand_subreg). The new
953 pseudos created by the transformations might have inaccurate
954 class (ALL_REGS) and we should make their classes more
955 accurate. */
956 narrow_reload_pseudo_class (in_rtx, goal_class);
957 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
958 *before = get_insns ();
959 end_sequence ();
960 for (i = 0; (in = ins[i]) >= 0; i++)
962 lra_assert
963 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
964 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
965 *curr_id->operand_loc[in] = new_in_reg;
967 lra_update_dups (curr_id, ins);
968 if (out < 0)
969 return;
970 /* See a comment for the input operand above. */
971 narrow_reload_pseudo_class (out_rtx, goal_class);
972 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
974 start_sequence ();
975 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
976 emit_insn (*after);
977 *after = get_insns ();
978 end_sequence ();
980 *curr_id->operand_loc[out] = new_out_reg;
981 lra_update_dup (curr_id, out);
984 /* Return register class which is union of all reg classes in insn
985 constraint alternative string starting with P. */
986 static enum reg_class
987 reg_class_from_constraints (const char *p)
989 int c, len;
990 enum reg_class op_class = NO_REGS;
993 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
995 case '#':
996 case ',':
997 return op_class;
999 case 'g':
1000 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1001 break;
1003 default:
1004 enum constraint_num cn = lookup_constraint (p);
1005 enum reg_class cl = reg_class_for_constraint (cn);
1006 if (cl == NO_REGS)
1008 if (insn_extra_address_constraint (cn))
1009 op_class
1010 = (reg_class_subunion
1011 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1012 ADDRESS, SCRATCH)]);
1013 break;
1016 op_class = reg_class_subunion[op_class][cl];
1017 break;
1019 while ((p += len), c);
1020 return op_class;
1023 /* If OP is a register, return the class of the register as per
1024 get_reg_class, otherwise return NO_REGS. */
1025 static inline enum reg_class
1026 get_op_class (rtx op)
1028 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1031 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1032 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1033 SUBREG for VAL to make them equal. */
1034 static rtx_insn *
1035 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1037 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1039 /* Usually size of mem_pseudo is greater than val size but in
1040 rare cases it can be less as it can be defined by target
1041 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1042 if (! MEM_P (val))
1044 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1045 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1047 LRA_SUBREG_P (val) = 1;
1049 else
1051 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1052 LRA_SUBREG_P (mem_pseudo) = 1;
1055 return to_p ? gen_move_insn (mem_pseudo, val)
1056 : gen_move_insn (val, mem_pseudo);
1059 /* Process a special case insn (register move), return true if we
1060 don't need to process it anymore. INSN should be a single set
1061 insn. Set up that RTL was changed through CHANGE_P and macro
1062 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1063 SEC_MEM_P. */
1064 static bool
1065 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1067 int sregno, dregno;
1068 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1069 rtx_insn *before;
1070 enum reg_class dclass, sclass, secondary_class;
1071 secondary_reload_info sri;
1073 lra_assert (curr_insn_set != NULL_RTX);
1074 dreg = dest = SET_DEST (curr_insn_set);
1075 sreg = src = SET_SRC (curr_insn_set);
1076 if (GET_CODE (dest) == SUBREG)
1077 dreg = SUBREG_REG (dest);
1078 if (GET_CODE (src) == SUBREG)
1079 sreg = SUBREG_REG (src);
1080 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1081 return false;
1082 sclass = dclass = NO_REGS;
1083 if (REG_P (dreg))
1084 dclass = get_reg_class (REGNO (dreg));
1085 if (dclass == ALL_REGS)
1086 /* ALL_REGS is used for new pseudos created by transformations
1087 like reload of SUBREG_REG (see function
1088 simplify_operand_subreg). We don't know their class yet. We
1089 should figure out the class from processing the insn
1090 constraints not in this fast path function. Even if ALL_REGS
1091 were a right class for the pseudo, secondary_... hooks usually
1092 are not define for ALL_REGS. */
1093 return false;
1094 if (REG_P (sreg))
1095 sclass = get_reg_class (REGNO (sreg));
1096 if (sclass == ALL_REGS)
1097 /* See comments above. */
1098 return false;
1099 if (sclass == NO_REGS && dclass == NO_REGS)
1100 return false;
1101 #ifdef SECONDARY_MEMORY_NEEDED
1102 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1103 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1104 && ((sclass != NO_REGS && dclass != NO_REGS)
1105 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1106 #endif
1109 *sec_mem_p = true;
1110 return false;
1112 #endif
1113 if (! REG_P (dreg) || ! REG_P (sreg))
1114 return false;
1115 sri.prev_sri = NULL;
1116 sri.icode = CODE_FOR_nothing;
1117 sri.extra_cost = 0;
1118 secondary_class = NO_REGS;
1119 /* Set up hard register for a reload pseudo for hook
1120 secondary_reload because some targets just ignore unassigned
1121 pseudos in the hook. */
1122 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1124 dregno = REGNO (dreg);
1125 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1127 else
1128 dregno = -1;
1129 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1131 sregno = REGNO (sreg);
1132 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1134 else
1135 sregno = -1;
1136 if (sclass != NO_REGS)
1137 secondary_class
1138 = (enum reg_class) targetm.secondary_reload (false, dest,
1139 (reg_class_t) sclass,
1140 GET_MODE (src), &sri);
1141 if (sclass == NO_REGS
1142 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1143 && dclass != NO_REGS))
1145 enum reg_class old_sclass = secondary_class;
1146 secondary_reload_info old_sri = sri;
1148 sri.prev_sri = NULL;
1149 sri.icode = CODE_FOR_nothing;
1150 sri.extra_cost = 0;
1151 secondary_class
1152 = (enum reg_class) targetm.secondary_reload (true, src,
1153 (reg_class_t) dclass,
1154 GET_MODE (src), &sri);
1155 /* Check the target hook consistency. */
1156 lra_assert
1157 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1158 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1159 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1161 if (sregno >= 0)
1162 reg_renumber [sregno] = -1;
1163 if (dregno >= 0)
1164 reg_renumber [dregno] = -1;
1165 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1166 return false;
1167 *change_p = true;
1168 new_reg = NULL_RTX;
1169 if (secondary_class != NO_REGS)
1170 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1171 secondary_class,
1172 "secondary");
1173 start_sequence ();
1174 if (sri.icode == CODE_FOR_nothing)
1175 lra_emit_move (new_reg, src);
1176 else
1178 enum reg_class scratch_class;
1180 scratch_class = (reg_class_from_constraints
1181 (insn_data[sri.icode].operand[2].constraint));
1182 scratch_reg = (lra_create_new_reg_with_unique_value
1183 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1184 scratch_class, "scratch"));
1185 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1186 src, scratch_reg));
1188 before = get_insns ();
1189 end_sequence ();
1190 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1191 if (new_reg != NULL_RTX)
1192 SET_SRC (curr_insn_set) = new_reg;
1193 else
1195 if (lra_dump_file != NULL)
1197 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1198 dump_insn_slim (lra_dump_file, curr_insn);
1200 lra_set_insn_deleted (curr_insn);
1201 return true;
1203 return false;
1206 /* The following data describe the result of process_alt_operands.
1207 The data are used in curr_insn_transform to generate reloads. */
1209 /* The chosen reg classes which should be used for the corresponding
1210 operands. */
1211 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1212 /* True if the operand should be the same as another operand and that
1213 other operand does not need a reload. */
1214 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1215 /* True if the operand does not need a reload. */
1216 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1217 /* True if the operand can be offsetable memory. */
1218 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1219 /* The number of an operand to which given operand can be matched to. */
1220 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1221 /* The number of elements in the following array. */
1222 static int goal_alt_dont_inherit_ops_num;
1223 /* Numbers of operands whose reload pseudos should not be inherited. */
1224 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1225 /* True if the insn commutative operands should be swapped. */
1226 static bool goal_alt_swapped;
1227 /* The chosen insn alternative. */
1228 static int goal_alt_number;
1230 /* The following five variables are used to choose the best insn
1231 alternative. They reflect final characteristics of the best
1232 alternative. */
1234 /* Number of necessary reloads and overall cost reflecting the
1235 previous value and other unpleasantness of the best alternative. */
1236 static int best_losers, best_overall;
1237 /* Overall number hard registers used for reloads. For example, on
1238 some targets we need 2 general registers to reload DFmode and only
1239 one floating point register. */
1240 static int best_reload_nregs;
1241 /* Overall number reflecting distances of previous reloading the same
1242 value. The distances are counted from the current BB start. It is
1243 used to improve inheritance chances. */
1244 static int best_reload_sum;
1246 /* True if the current insn should have no correspondingly input or
1247 output reloads. */
1248 static bool no_input_reloads_p, no_output_reloads_p;
1250 /* True if we swapped the commutative operands in the current
1251 insn. */
1252 static int curr_swapped;
1254 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1255 register of class CL. Add any input reloads to list BEFORE. AFTER
1256 is nonnull if *LOC is an automodified value; handle that case by
1257 adding the required output reloads to list AFTER. Return true if
1258 the RTL was changed.
1260 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1261 register. Return false if the address register is correct. */
1262 static bool
1263 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1264 enum reg_class cl)
1266 int regno;
1267 enum reg_class rclass, new_class;
1268 rtx reg;
1269 rtx new_reg;
1270 machine_mode mode;
1271 bool subreg_p, before_p = false;
1273 subreg_p = GET_CODE (*loc) == SUBREG;
1274 if (subreg_p)
1275 loc = &SUBREG_REG (*loc);
1276 reg = *loc;
1277 mode = GET_MODE (reg);
1278 if (! REG_P (reg))
1280 if (check_only_p)
1281 return true;
1282 /* Always reload memory in an address even if the target supports
1283 such addresses. */
1284 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1285 before_p = true;
1287 else
1289 regno = REGNO (reg);
1290 rclass = get_reg_class (regno);
1291 if (! check_only_p
1292 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1294 if (lra_dump_file != NULL)
1296 fprintf (lra_dump_file,
1297 "Changing pseudo %d in address of insn %u on equiv ",
1298 REGNO (reg), INSN_UID (curr_insn));
1299 dump_value_slim (lra_dump_file, *loc, 1);
1300 fprintf (lra_dump_file, "\n");
1302 *loc = copy_rtx (*loc);
1304 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1306 if (check_only_p)
1307 return true;
1308 reg = *loc;
1309 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1310 mode, reg, cl, subreg_p, "address", &new_reg))
1311 before_p = true;
1313 else if (new_class != NO_REGS && rclass != new_class)
1315 if (check_only_p)
1316 return true;
1317 lra_change_class (regno, new_class, " Change to", true);
1318 return false;
1320 else
1321 return false;
1323 if (before_p)
1325 push_to_sequence (*before);
1326 lra_emit_move (new_reg, reg);
1327 *before = get_insns ();
1328 end_sequence ();
1330 *loc = new_reg;
1331 if (after != NULL)
1333 start_sequence ();
1334 lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1335 emit_insn (*after);
1336 *after = get_insns ();
1337 end_sequence ();
1339 return true;
1342 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1343 the insn to be inserted before curr insn. AFTER returns the
1344 the insn to be inserted after curr insn. ORIGREG and NEWREG
1345 are the original reg and new reg for reload. */
1346 static void
1347 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1348 rtx newreg)
1350 if (before)
1352 push_to_sequence (*before);
1353 lra_emit_move (newreg, origreg);
1354 *before = get_insns ();
1355 end_sequence ();
1357 if (after)
1359 start_sequence ();
1360 lra_emit_move (origreg, newreg);
1361 emit_insn (*after);
1362 *after = get_insns ();
1363 end_sequence ();
1367 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1369 /* Make reloads for subreg in operand NOP with internal subreg mode
1370 REG_MODE, add new reloads for further processing. Return true if
1371 any change was done. */
1372 static bool
1373 simplify_operand_subreg (int nop, machine_mode reg_mode)
1375 int hard_regno;
1376 rtx_insn *before, *after;
1377 machine_mode mode, innermode;
1378 rtx reg, new_reg;
1379 rtx operand = *curr_id->operand_loc[nop];
1380 enum reg_class regclass;
1381 enum op_type type;
1383 before = after = NULL;
1385 if (GET_CODE (operand) != SUBREG)
1386 return false;
1388 mode = GET_MODE (operand);
1389 reg = SUBREG_REG (operand);
1390 innermode = GET_MODE (reg);
1391 type = curr_static_id->operand[nop].type;
1392 /* If we change address for paradoxical subreg of memory, the
1393 address might violate the necessary alignment or the access might
1394 be slow. So take this into consideration. We should not worry
1395 about access beyond allocated memory for paradoxical memory
1396 subregs as we don't substitute such equiv memory (see processing
1397 equivalences in function lra_constraints) and because for spilled
1398 pseudos we allocate stack memory enough for the biggest
1399 corresponding paradoxical subreg. */
1400 if (MEM_P (reg)
1401 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1402 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1404 rtx subst, old = *curr_id->operand_loc[nop];
1406 alter_subreg (curr_id->operand_loc[nop], false);
1407 subst = *curr_id->operand_loc[nop];
1408 lra_assert (MEM_P (subst));
1409 if (! valid_address_p (innermode, XEXP (reg, 0),
1410 MEM_ADDR_SPACE (reg))
1411 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1412 MEM_ADDR_SPACE (subst)))
1413 return true;
1414 /* If the address was valid and became invalid, prefer to reload
1415 the memory. Typical case is when the index scale should
1416 correspond the memory. */
1417 *curr_id->operand_loc[nop] = old;
1419 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1421 alter_subreg (curr_id->operand_loc[nop], false);
1422 return true;
1424 else if (CONSTANT_P (reg))
1426 /* Try to simplify subreg of constant. It is usually result of
1427 equivalence substitution. */
1428 if (innermode == VOIDmode
1429 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1430 innermode = curr_static_id->operand[nop].mode;
1431 if ((new_reg = simplify_subreg (mode, reg, innermode,
1432 SUBREG_BYTE (operand))) != NULL_RTX)
1434 *curr_id->operand_loc[nop] = new_reg;
1435 return true;
1438 /* Put constant into memory when we have mixed modes. It generates
1439 a better code in most cases as it does not need a secondary
1440 reload memory. It also prevents LRA looping when LRA is using
1441 secondary reload memory again and again. */
1442 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1443 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1445 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1446 alter_subreg (curr_id->operand_loc[nop], false);
1447 return true;
1449 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1450 if there may be a problem accessing OPERAND in the outer
1451 mode. */
1452 if ((REG_P (reg)
1453 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1454 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1455 /* Don't reload paradoxical subregs because we could be looping
1456 having repeatedly final regno out of hard regs range. */
1457 && (hard_regno_nregs[hard_regno][innermode]
1458 >= hard_regno_nregs[hard_regno][mode])
1459 && simplify_subreg_regno (hard_regno, innermode,
1460 SUBREG_BYTE (operand), mode) < 0
1461 /* Don't reload subreg for matching reload. It is actually
1462 valid subreg in LRA. */
1463 && ! LRA_SUBREG_P (operand))
1464 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1466 enum reg_class rclass;
1468 if (REG_P (reg))
1469 /* There is a big probability that we will get the same class
1470 for the new pseudo and we will get the same insn which
1471 means infinite looping. So spill the new pseudo. */
1472 rclass = NO_REGS;
1473 else
1474 /* The class will be defined later in curr_insn_transform. */
1475 rclass
1476 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1478 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1479 rclass, TRUE, "subreg reg", &new_reg))
1481 bool insert_before, insert_after;
1482 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1484 insert_before = (type != OP_OUT
1485 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1486 insert_after = (type != OP_IN);
1487 insert_move_for_subreg (insert_before ? &before : NULL,
1488 insert_after ? &after : NULL,
1489 reg, new_reg);
1491 SUBREG_REG (operand) = new_reg;
1492 lra_process_new_insns (curr_insn, before, after,
1493 "Inserting subreg reload");
1494 return true;
1496 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1497 IRA allocates hardreg to the inner pseudo reg according to its mode
1498 instead of the outermode, so the size of the hardreg may not be enough
1499 to contain the outermode operand, in that case we may need to insert
1500 reload for the reg. For the following two types of paradoxical subreg,
1501 we need to insert reload:
1502 1. If the op_type is OP_IN, and the hardreg could not be paired with
1503 other hardreg to contain the outermode operand
1504 (checked by in_hard_reg_set_p), we need to insert the reload.
1505 2. If the op_type is OP_OUT or OP_INOUT.
1507 Here is a paradoxical subreg example showing how the reload is generated:
1509 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1510 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1512 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1513 here, if reg107 is assigned to hardreg R15, because R15 is the last
1514 hardreg, compiler cannot find another hardreg to pair with R15 to
1515 contain TImode data. So we insert a TImode reload reg180 for it.
1516 After reload is inserted:
1518 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1519 (reg:DI 107 [ __comp ])) -1
1520 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1521 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1523 Two reload hard registers will be allocated to reg180 to save TImode data
1524 in LRA_assign. */
1525 else if (REG_P (reg)
1526 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1527 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1528 && (hard_regno_nregs[hard_regno][innermode]
1529 < hard_regno_nregs[hard_regno][mode])
1530 && (regclass = lra_get_allocno_class (REGNO (reg)))
1531 && (type != OP_IN
1532 || !in_hard_reg_set_p (reg_class_contents[regclass],
1533 mode, hard_regno)))
1535 /* The class will be defined later in curr_insn_transform. */
1536 enum reg_class rclass
1537 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1539 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1540 rclass, TRUE, "paradoxical subreg", &new_reg))
1542 rtx subreg;
1543 bool insert_before, insert_after;
1545 PUT_MODE (new_reg, mode);
1546 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1547 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1549 insert_before = (type != OP_OUT);
1550 insert_after = (type != OP_IN);
1551 insert_move_for_subreg (insert_before ? &before : NULL,
1552 insert_after ? &after : NULL,
1553 reg, subreg);
1555 SUBREG_REG (operand) = new_reg;
1556 lra_process_new_insns (curr_insn, before, after,
1557 "Inserting paradoxical subreg reload");
1558 return true;
1560 return false;
1563 /* Return TRUE if X refers for a hard register from SET. */
1564 static bool
1565 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1567 int i, j, x_hard_regno;
1568 machine_mode mode;
1569 const char *fmt;
1570 enum rtx_code code;
1572 if (x == NULL_RTX)
1573 return false;
1574 code = GET_CODE (x);
1575 mode = GET_MODE (x);
1576 if (code == SUBREG)
1578 x = SUBREG_REG (x);
1579 code = GET_CODE (x);
1580 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1581 mode = GET_MODE (x);
1584 if (REG_P (x))
1586 x_hard_regno = get_hard_regno (x);
1587 return (x_hard_regno >= 0
1588 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1590 if (MEM_P (x))
1592 struct address_info ad;
1594 decompose_mem_address (&ad, x);
1595 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1596 return true;
1597 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1598 return true;
1600 fmt = GET_RTX_FORMAT (code);
1601 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1603 if (fmt[i] == 'e')
1605 if (uses_hard_regs_p (XEXP (x, i), set))
1606 return true;
1608 else if (fmt[i] == 'E')
1610 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1611 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1612 return true;
1615 return false;
1618 /* Return true if OP is a spilled pseudo. */
1619 static inline bool
1620 spilled_pseudo_p (rtx op)
1622 return (REG_P (op)
1623 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1626 /* Return true if X is a general constant. */
1627 static inline bool
1628 general_constant_p (rtx x)
1630 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1633 static bool
1634 reg_in_class_p (rtx reg, enum reg_class cl)
1636 if (cl == NO_REGS)
1637 return get_reg_class (REGNO (reg)) == NO_REGS;
1638 return in_class_p (reg, cl, NULL);
1641 /* Return true if SET of RCLASS contains no hard regs which can be
1642 used in MODE. */
1643 static bool
1644 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1645 HARD_REG_SET &set,
1646 enum machine_mode mode)
1648 HARD_REG_SET temp;
1650 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1651 COPY_HARD_REG_SET (temp, set);
1652 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1653 return (hard_reg_set_subset_p
1654 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1657 /* Major function to choose the current insn alternative and what
1658 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1659 negative we should consider only this alternative. Return false if
1660 we can not choose the alternative or find how to reload the
1661 operands. */
1662 static bool
1663 process_alt_operands (int only_alternative)
1665 bool ok_p = false;
1666 int nop, overall, nalt;
1667 int n_alternatives = curr_static_id->n_alternatives;
1668 int n_operands = curr_static_id->n_operands;
1669 /* LOSERS counts the operands that don't fit this alternative and
1670 would require loading. */
1671 int losers;
1672 /* REJECT is a count of how undesirable this alternative says it is
1673 if any reloading is required. If the alternative matches exactly
1674 then REJECT is ignored, but otherwise it gets this much counted
1675 against it in addition to the reloading needed. */
1676 int reject;
1677 int op_reject;
1678 /* The number of elements in the following array. */
1679 int early_clobbered_regs_num;
1680 /* Numbers of operands which are early clobber registers. */
1681 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1682 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1683 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1684 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1685 bool curr_alt_win[MAX_RECOG_OPERANDS];
1686 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1687 int curr_alt_matches[MAX_RECOG_OPERANDS];
1688 /* The number of elements in the following array. */
1689 int curr_alt_dont_inherit_ops_num;
1690 /* Numbers of operands whose reload pseudos should not be inherited. */
1691 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1692 rtx op;
1693 /* The register when the operand is a subreg of register, otherwise the
1694 operand itself. */
1695 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1696 /* The register if the operand is a register or subreg of register,
1697 otherwise NULL. */
1698 rtx operand_reg[MAX_RECOG_OPERANDS];
1699 int hard_regno[MAX_RECOG_OPERANDS];
1700 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1701 int reload_nregs, reload_sum;
1702 bool costly_p;
1703 enum reg_class cl;
1705 /* Calculate some data common for all alternatives to speed up the
1706 function. */
1707 for (nop = 0; nop < n_operands; nop++)
1709 rtx reg;
1711 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1712 /* The real hard regno of the operand after the allocation. */
1713 hard_regno[nop] = get_hard_regno (op);
1715 operand_reg[nop] = reg = op;
1716 biggest_mode[nop] = GET_MODE (op);
1717 if (GET_CODE (op) == SUBREG)
1719 operand_reg[nop] = reg = SUBREG_REG (op);
1720 if (GET_MODE_SIZE (biggest_mode[nop])
1721 < GET_MODE_SIZE (GET_MODE (reg)))
1722 biggest_mode[nop] = GET_MODE (reg);
1724 if (! REG_P (reg))
1725 operand_reg[nop] = NULL_RTX;
1726 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1727 || ((int) REGNO (reg)
1728 == lra_get_elimination_hard_regno (REGNO (reg))))
1729 no_subreg_reg_operand[nop] = reg;
1730 else
1731 operand_reg[nop] = no_subreg_reg_operand[nop]
1732 /* Just use natural mode for elimination result. It should
1733 be enough for extra constraints hooks. */
1734 = regno_reg_rtx[hard_regno[nop]];
1737 /* The constraints are made of several alternatives. Each operand's
1738 constraint looks like foo,bar,... with commas separating the
1739 alternatives. The first alternatives for all operands go
1740 together, the second alternatives go together, etc.
1742 First loop over alternatives. */
1743 alternative_mask preferred = curr_id->preferred_alternatives;
1744 if (only_alternative >= 0)
1745 preferred &= ALTERNATIVE_BIT (only_alternative);
1747 for (nalt = 0; nalt < n_alternatives; nalt++)
1749 /* Loop over operands for one constraint alternative. */
1750 if (!TEST_BIT (preferred, nalt))
1751 continue;
1753 overall = losers = reject = reload_nregs = reload_sum = 0;
1754 for (nop = 0; nop < n_operands; nop++)
1756 int inc = (curr_static_id
1757 ->operand_alternative[nalt * n_operands + nop].reject);
1758 if (lra_dump_file != NULL && inc != 0)
1759 fprintf (lra_dump_file,
1760 " Staticly defined alt reject+=%d\n", inc);
1761 reject += inc;
1763 early_clobbered_regs_num = 0;
1765 for (nop = 0; nop < n_operands; nop++)
1767 const char *p;
1768 char *end;
1769 int len, c, m, i, opalt_num, this_alternative_matches;
1770 bool win, did_match, offmemok, early_clobber_p;
1771 /* false => this operand can be reloaded somehow for this
1772 alternative. */
1773 bool badop;
1774 /* true => this operand can be reloaded if the alternative
1775 allows regs. */
1776 bool winreg;
1777 /* True if a constant forced into memory would be OK for
1778 this operand. */
1779 bool constmemok;
1780 enum reg_class this_alternative, this_costly_alternative;
1781 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1782 bool this_alternative_match_win, this_alternative_win;
1783 bool this_alternative_offmemok;
1784 bool scratch_p;
1785 machine_mode mode;
1786 enum constraint_num cn;
1788 opalt_num = nalt * n_operands + nop;
1789 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1791 /* Fast track for no constraints at all. */
1792 curr_alt[nop] = NO_REGS;
1793 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1794 curr_alt_win[nop] = true;
1795 curr_alt_match_win[nop] = false;
1796 curr_alt_offmemok[nop] = false;
1797 curr_alt_matches[nop] = -1;
1798 continue;
1801 op = no_subreg_reg_operand[nop];
1802 mode = curr_operand_mode[nop];
1804 win = did_match = winreg = offmemok = constmemok = false;
1805 badop = true;
1807 early_clobber_p = false;
1808 p = curr_static_id->operand_alternative[opalt_num].constraint;
1810 this_costly_alternative = this_alternative = NO_REGS;
1811 /* We update set of possible hard regs besides its class
1812 because reg class might be inaccurate. For example,
1813 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1814 is translated in HI_REGS because classes are merged by
1815 pairs and there is no accurate intermediate class. */
1816 CLEAR_HARD_REG_SET (this_alternative_set);
1817 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1818 this_alternative_win = false;
1819 this_alternative_match_win = false;
1820 this_alternative_offmemok = false;
1821 this_alternative_matches = -1;
1823 /* An empty constraint should be excluded by the fast
1824 track. */
1825 lra_assert (*p != 0 && *p != ',');
1827 op_reject = 0;
1828 /* Scan this alternative's specs for this operand; set WIN
1829 if the operand fits any letter in this alternative.
1830 Otherwise, clear BADOP if this operand could fit some
1831 letter after reloads, or set WINREG if this operand could
1832 fit after reloads provided the constraint allows some
1833 registers. */
1834 costly_p = false;
1837 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1839 case '\0':
1840 len = 0;
1841 break;
1842 case ',':
1843 c = '\0';
1844 break;
1846 case '&':
1847 early_clobber_p = true;
1848 break;
1850 case '$':
1851 op_reject += LRA_MAX_REJECT;
1852 break;
1853 case '^':
1854 op_reject += LRA_LOSER_COST_FACTOR;
1855 break;
1857 case '#':
1858 /* Ignore rest of this alternative. */
1859 c = '\0';
1860 break;
1862 case '0': case '1': case '2': case '3': case '4':
1863 case '5': case '6': case '7': case '8': case '9':
1865 int m_hregno;
1866 bool match_p;
1868 m = strtoul (p, &end, 10);
1869 p = end;
1870 len = 0;
1871 lra_assert (nop > m);
1873 this_alternative_matches = m;
1874 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1875 /* We are supposed to match a previous operand.
1876 If we do, we win if that one did. If we do
1877 not, count both of the operands as losers.
1878 (This is too conservative, since most of the
1879 time only a single reload insn will be needed
1880 to make the two operands win. As a result,
1881 this alternative may be rejected when it is
1882 actually desirable.) */
1883 match_p = false;
1884 if (operands_match_p (*curr_id->operand_loc[nop],
1885 *curr_id->operand_loc[m], m_hregno))
1887 /* We should reject matching of an early
1888 clobber operand if the matching operand is
1889 not dying in the insn. */
1890 if (! curr_static_id->operand[m].early_clobber
1891 || operand_reg[nop] == NULL_RTX
1892 || (find_regno_note (curr_insn, REG_DEAD,
1893 REGNO (op))
1894 || REGNO (op) == REGNO (operand_reg[m])))
1895 match_p = true;
1897 if (match_p)
1899 /* If we are matching a non-offsettable
1900 address where an offsettable address was
1901 expected, then we must reject this
1902 combination, because we can't reload
1903 it. */
1904 if (curr_alt_offmemok[m]
1905 && MEM_P (*curr_id->operand_loc[m])
1906 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1907 continue;
1909 else
1911 /* Operands don't match. Both operands must
1912 allow a reload register, otherwise we
1913 cannot make them match. */
1914 if (curr_alt[m] == NO_REGS)
1915 break;
1916 /* Retroactively mark the operand we had to
1917 match as a loser, if it wasn't already and
1918 it wasn't matched to a register constraint
1919 (e.g it might be matched by memory). */
1920 if (curr_alt_win[m]
1921 && (operand_reg[m] == NULL_RTX
1922 || hard_regno[m] < 0))
1924 losers++;
1925 reload_nregs
1926 += (ira_reg_class_max_nregs[curr_alt[m]]
1927 [GET_MODE (*curr_id->operand_loc[m])]);
1930 /* Prefer matching earlyclobber alternative as
1931 it results in less hard regs required for
1932 the insn than a non-matching earlyclobber
1933 alternative. */
1934 if (curr_static_id->operand[m].early_clobber)
1936 if (lra_dump_file != NULL)
1937 fprintf
1938 (lra_dump_file,
1939 " %d Matching earlyclobber alt:"
1940 " reject--\n",
1941 nop);
1942 reject--;
1944 /* Otherwise we prefer no matching
1945 alternatives because it gives more freedom
1946 in RA. */
1947 else if (operand_reg[nop] == NULL_RTX
1948 || (find_regno_note (curr_insn, REG_DEAD,
1949 REGNO (operand_reg[nop]))
1950 == NULL_RTX))
1952 if (lra_dump_file != NULL)
1953 fprintf
1954 (lra_dump_file,
1955 " %d Matching alt: reject+=2\n",
1956 nop);
1957 reject += 2;
1960 /* If we have to reload this operand and some
1961 previous operand also had to match the same
1962 thing as this operand, we don't know how to do
1963 that. */
1964 if (!match_p || !curr_alt_win[m])
1966 for (i = 0; i < nop; i++)
1967 if (curr_alt_matches[i] == m)
1968 break;
1969 if (i < nop)
1970 break;
1972 else
1973 did_match = true;
1975 /* This can be fixed with reloads if the operand
1976 we are supposed to match can be fixed with
1977 reloads. */
1978 badop = false;
1979 this_alternative = curr_alt[m];
1980 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1981 winreg = this_alternative != NO_REGS;
1982 break;
1985 case 'g':
1986 if (MEM_P (op)
1987 || general_constant_p (op)
1988 || spilled_pseudo_p (op))
1989 win = true;
1990 cl = GENERAL_REGS;
1991 goto reg;
1993 default:
1994 cn = lookup_constraint (p);
1995 switch (get_constraint_type (cn))
1997 case CT_REGISTER:
1998 cl = reg_class_for_constraint (cn);
1999 if (cl != NO_REGS)
2000 goto reg;
2001 break;
2003 case CT_CONST_INT:
2004 if (CONST_INT_P (op)
2005 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2006 win = true;
2007 break;
2009 case CT_MEMORY:
2010 if (MEM_P (op)
2011 && satisfies_memory_constraint_p (op, cn))
2012 win = true;
2013 else if (spilled_pseudo_p (op))
2014 win = true;
2016 /* If we didn't already win, we can reload constants
2017 via force_const_mem or put the pseudo value into
2018 memory, or make other memory by reloading the
2019 address like for 'o'. */
2020 if (CONST_POOL_OK_P (mode, op)
2021 || MEM_P (op) || REG_P (op))
2022 badop = false;
2023 constmemok = true;
2024 offmemok = true;
2025 break;
2027 case CT_ADDRESS:
2028 /* If we didn't already win, we can reload the address
2029 into a base register. */
2030 if (satisfies_address_constraint_p (op, cn))
2031 win = true;
2032 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2033 ADDRESS, SCRATCH);
2034 badop = false;
2035 goto reg;
2037 case CT_FIXED_FORM:
2038 if (constraint_satisfied_p (op, cn))
2039 win = true;
2040 break;
2042 break;
2044 reg:
2045 this_alternative = reg_class_subunion[this_alternative][cl];
2046 IOR_HARD_REG_SET (this_alternative_set,
2047 reg_class_contents[cl]);
2048 if (costly_p)
2050 this_costly_alternative
2051 = reg_class_subunion[this_costly_alternative][cl];
2052 IOR_HARD_REG_SET (this_costly_alternative_set,
2053 reg_class_contents[cl]);
2055 if (mode == BLKmode)
2056 break;
2057 winreg = true;
2058 if (REG_P (op))
2060 if (hard_regno[nop] >= 0
2061 && in_hard_reg_set_p (this_alternative_set,
2062 mode, hard_regno[nop]))
2063 win = true;
2064 else if (hard_regno[nop] < 0
2065 && in_class_p (op, this_alternative, NULL))
2066 win = true;
2068 break;
2070 if (c != ' ' && c != '\t')
2071 costly_p = c == '*';
2073 while ((p += len), c);
2075 scratch_p = (operand_reg[nop] != NULL_RTX
2076 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2077 /* Record which operands fit this alternative. */
2078 if (win)
2080 this_alternative_win = true;
2081 if (operand_reg[nop] != NULL_RTX)
2083 if (hard_regno[nop] >= 0)
2085 if (in_hard_reg_set_p (this_costly_alternative_set,
2086 mode, hard_regno[nop]))
2088 if (lra_dump_file != NULL)
2089 fprintf (lra_dump_file,
2090 " %d Costly set: reject++\n",
2091 nop);
2092 reject++;
2095 else
2097 /* Prefer won reg to spilled pseudo under other
2098 equal conditions for possibe inheritance. */
2099 if (! scratch_p)
2101 if (lra_dump_file != NULL)
2102 fprintf
2103 (lra_dump_file,
2104 " %d Non pseudo reload: reject++\n",
2105 nop);
2106 reject++;
2108 if (in_class_p (operand_reg[nop],
2109 this_costly_alternative, NULL))
2111 if (lra_dump_file != NULL)
2112 fprintf
2113 (lra_dump_file,
2114 " %d Non pseudo costly reload:"
2115 " reject++\n",
2116 nop);
2117 reject++;
2120 /* We simulate the behaviour of old reload here.
2121 Although scratches need hard registers and it
2122 might result in spilling other pseudos, no reload
2123 insns are generated for the scratches. So it
2124 might cost something but probably less than old
2125 reload pass believes. */
2126 if (scratch_p)
2128 if (lra_dump_file != NULL)
2129 fprintf (lra_dump_file,
2130 " %d Scratch win: reject+=2\n",
2131 nop);
2132 reject += 2;
2136 else if (did_match)
2137 this_alternative_match_win = true;
2138 else
2140 int const_to_mem = 0;
2141 bool no_regs_p;
2143 reject += op_reject;
2144 /* Never do output reload of stack pointer. It makes
2145 impossible to do elimination when SP is changed in
2146 RTL. */
2147 if (op == stack_pointer_rtx && ! frame_pointer_needed
2148 && curr_static_id->operand[nop].type != OP_IN)
2149 goto fail;
2151 /* If this alternative asks for a specific reg class, see if there
2152 is at least one allocatable register in that class. */
2153 no_regs_p
2154 = (this_alternative == NO_REGS
2155 || (hard_reg_set_subset_p
2156 (reg_class_contents[this_alternative],
2157 lra_no_alloc_regs)));
2159 /* For asms, verify that the class for this alternative is possible
2160 for the mode that is specified. */
2161 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2163 int i;
2164 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2165 if (HARD_REGNO_MODE_OK (i, mode)
2166 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2167 mode, i))
2168 break;
2169 if (i == FIRST_PSEUDO_REGISTER)
2170 winreg = false;
2173 /* If this operand accepts a register, and if the
2174 register class has at least one allocatable register,
2175 then this operand can be reloaded. */
2176 if (winreg && !no_regs_p)
2177 badop = false;
2179 if (badop)
2181 if (lra_dump_file != NULL)
2182 fprintf (lra_dump_file,
2183 " alt=%d: Bad operand -- refuse\n",
2184 nalt);
2185 goto fail;
2188 /* If not assigned pseudo has a class which a subset of
2189 required reg class, it is a less costly alternative
2190 as the pseudo still can get a hard reg of necessary
2191 class. */
2192 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2193 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2194 && ira_class_subset_p[this_alternative][cl])
2196 if (lra_dump_file != NULL)
2197 fprintf
2198 (lra_dump_file,
2199 " %d Super set class reg: reject-=3\n", nop);
2200 reject -= 3;
2203 this_alternative_offmemok = offmemok;
2204 if (this_costly_alternative != NO_REGS)
2206 if (lra_dump_file != NULL)
2207 fprintf (lra_dump_file,
2208 " %d Costly loser: reject++\n", nop);
2209 reject++;
2211 /* If the operand is dying, has a matching constraint,
2212 and satisfies constraints of the matched operand
2213 which failed to satisfy the own constraints, most probably
2214 the reload for this operand will be gone. */
2215 if (this_alternative_matches >= 0
2216 && !curr_alt_win[this_alternative_matches]
2217 && REG_P (op)
2218 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2219 && (hard_regno[nop] >= 0
2220 ? in_hard_reg_set_p (this_alternative_set,
2221 mode, hard_regno[nop])
2222 : in_class_p (op, this_alternative, NULL)))
2224 if (lra_dump_file != NULL)
2225 fprintf
2226 (lra_dump_file,
2227 " %d Dying matched operand reload: reject++\n",
2228 nop);
2229 reject++;
2231 else
2233 /* Strict_low_part requires to reload the register
2234 not the sub-register. In this case we should
2235 check that a final reload hard reg can hold the
2236 value mode. */
2237 if (curr_static_id->operand[nop].strict_low
2238 && REG_P (op)
2239 && hard_regno[nop] < 0
2240 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2241 && ira_class_hard_regs_num[this_alternative] > 0
2242 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2243 [this_alternative][0],
2244 GET_MODE
2245 (*curr_id->operand_loc[nop])))
2247 if (lra_dump_file != NULL)
2248 fprintf
2249 (lra_dump_file,
2250 " alt=%d: Strict low subreg reload -- refuse\n",
2251 nalt);
2252 goto fail;
2254 losers++;
2256 if (operand_reg[nop] != NULL_RTX
2257 /* Output operands and matched input operands are
2258 not inherited. The following conditions do not
2259 exactly describe the previous statement but they
2260 are pretty close. */
2261 && curr_static_id->operand[nop].type != OP_OUT
2262 && (this_alternative_matches < 0
2263 || curr_static_id->operand[nop].type != OP_IN))
2265 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2266 (operand_reg[nop])]
2267 .last_reload);
2269 /* The value of reload_sum has sense only if we
2270 process insns in their order. It happens only on
2271 the first constraints sub-pass when we do most of
2272 reload work. */
2273 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2274 reload_sum += last_reload - bb_reload_num;
2276 /* If this is a constant that is reloaded into the
2277 desired class by copying it to memory first, count
2278 that as another reload. This is consistent with
2279 other code and is required to avoid choosing another
2280 alternative when the constant is moved into memory.
2281 Note that the test here is precisely the same as in
2282 the code below that calls force_const_mem. */
2283 if (CONST_POOL_OK_P (mode, op)
2284 && ((targetm.preferred_reload_class
2285 (op, this_alternative) == NO_REGS)
2286 || no_input_reloads_p))
2288 const_to_mem = 1;
2289 if (! no_regs_p)
2290 losers++;
2293 /* Alternative loses if it requires a type of reload not
2294 permitted for this insn. We can always reload
2295 objects with a REG_UNUSED note. */
2296 if ((curr_static_id->operand[nop].type != OP_IN
2297 && no_output_reloads_p
2298 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2299 || (curr_static_id->operand[nop].type != OP_OUT
2300 && no_input_reloads_p && ! const_to_mem)
2301 || (this_alternative_matches >= 0
2302 && (no_input_reloads_p
2303 || (no_output_reloads_p
2304 && (curr_static_id->operand
2305 [this_alternative_matches].type != OP_IN)
2306 && ! find_reg_note (curr_insn, REG_UNUSED,
2307 no_subreg_reg_operand
2308 [this_alternative_matches])))))
2310 if (lra_dump_file != NULL)
2311 fprintf
2312 (lra_dump_file,
2313 " alt=%d: No input/otput reload -- refuse\n",
2314 nalt);
2315 goto fail;
2318 /* Alternative loses if it required class pseudo can not
2319 hold value of required mode. Such insns can be
2320 described by insn definitions with mode iterators. */
2321 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2322 && ! hard_reg_set_empty_p (this_alternative_set)
2323 /* It is common practice for constraints to use a
2324 class which does not have actually enough regs to
2325 hold the value (e.g. x86 AREG for mode requiring
2326 more one general reg). Therefore we have 2
2327 conditions to check that the reload pseudo can
2328 not hold the mode value. */
2329 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2330 [this_alternative][0],
2331 GET_MODE (*curr_id->operand_loc[nop]))
2332 /* The above condition is not enough as the first
2333 reg in ira_class_hard_regs can be not aligned for
2334 multi-words mode values. */
2335 && (prohibited_class_reg_set_mode_p
2336 (this_alternative, this_alternative_set,
2337 GET_MODE (*curr_id->operand_loc[nop]))))
2339 if (lra_dump_file != NULL)
2340 fprintf (lra_dump_file,
2341 " alt=%d: reload pseudo for op %d "
2342 " can not hold the mode value -- refuse\n",
2343 nalt, nop);
2344 goto fail;
2347 /* Check strong discouragement of reload of non-constant
2348 into class THIS_ALTERNATIVE. */
2349 if (! CONSTANT_P (op) && ! no_regs_p
2350 && (targetm.preferred_reload_class
2351 (op, this_alternative) == NO_REGS
2352 || (curr_static_id->operand[nop].type == OP_OUT
2353 && (targetm.preferred_output_reload_class
2354 (op, this_alternative) == NO_REGS))))
2356 if (lra_dump_file != NULL)
2357 fprintf (lra_dump_file,
2358 " %d Non-prefered reload: reject+=%d\n",
2359 nop, LRA_MAX_REJECT);
2360 reject += LRA_MAX_REJECT;
2363 if (! (MEM_P (op) && offmemok)
2364 && ! (const_to_mem && constmemok))
2366 /* We prefer to reload pseudos over reloading other
2367 things, since such reloads may be able to be
2368 eliminated later. So bump REJECT in other cases.
2369 Don't do this in the case where we are forcing a
2370 constant into memory and it will then win since
2371 we don't want to have a different alternative
2372 match then. */
2373 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2375 if (lra_dump_file != NULL)
2376 fprintf
2377 (lra_dump_file,
2378 " %d Non-pseudo reload: reject+=2\n",
2379 nop);
2380 reject += 2;
2383 if (! no_regs_p)
2384 reload_nregs
2385 += ira_reg_class_max_nregs[this_alternative][mode];
2387 if (SMALL_REGISTER_CLASS_P (this_alternative))
2389 if (lra_dump_file != NULL)
2390 fprintf
2391 (lra_dump_file,
2392 " %d Small class reload: reject+=%d\n",
2393 nop, LRA_LOSER_COST_FACTOR / 2);
2394 reject += LRA_LOSER_COST_FACTOR / 2;
2398 /* We are trying to spill pseudo into memory. It is
2399 usually more costly than moving to a hard register
2400 although it might takes the same number of
2401 reloads. */
2402 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2404 if (lra_dump_file != NULL)
2405 fprintf
2406 (lra_dump_file,
2407 " %d Spill pseudo into memory: reject+=3\n",
2408 nop);
2409 reject += 3;
2410 if (VECTOR_MODE_P (mode))
2412 /* Spilling vectors into memory is usually more
2413 costly as they contain big values. */
2414 if (lra_dump_file != NULL)
2415 fprintf
2416 (lra_dump_file,
2417 " %d Spill vector pseudo: reject+=2\n",
2418 nop);
2419 reject += 2;
2423 #ifdef SECONDARY_MEMORY_NEEDED
2424 /* If reload requires moving value through secondary
2425 memory, it will need one more insn at least. */
2426 if (this_alternative != NO_REGS
2427 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2428 && ((curr_static_id->operand[nop].type != OP_OUT
2429 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2430 GET_MODE (op)))
2431 || (curr_static_id->operand[nop].type != OP_IN
2432 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2433 GET_MODE (op)))))
2434 losers++;
2435 #endif
2436 /* Input reloads can be inherited more often than output
2437 reloads can be removed, so penalize output
2438 reloads. */
2439 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2441 if (lra_dump_file != NULL)
2442 fprintf
2443 (lra_dump_file,
2444 " %d Non input pseudo reload: reject++\n",
2445 nop);
2446 reject++;
2450 if (early_clobber_p && ! scratch_p)
2452 if (lra_dump_file != NULL)
2453 fprintf (lra_dump_file,
2454 " %d Early clobber: reject++\n", nop);
2455 reject++;
2457 /* ??? We check early clobbers after processing all operands
2458 (see loop below) and there we update the costs more.
2459 Should we update the cost (may be approximately) here
2460 because of early clobber register reloads or it is a rare
2461 or non-important thing to be worth to do it. */
2462 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2463 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2465 if (lra_dump_file != NULL)
2466 fprintf (lra_dump_file,
2467 " alt=%d,overall=%d,losers=%d -- refuse\n",
2468 nalt, overall, losers);
2469 goto fail;
2472 curr_alt[nop] = this_alternative;
2473 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2474 curr_alt_win[nop] = this_alternative_win;
2475 curr_alt_match_win[nop] = this_alternative_match_win;
2476 curr_alt_offmemok[nop] = this_alternative_offmemok;
2477 curr_alt_matches[nop] = this_alternative_matches;
2479 if (this_alternative_matches >= 0
2480 && !did_match && !this_alternative_win)
2481 curr_alt_win[this_alternative_matches] = false;
2483 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2484 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2486 if (curr_insn_set != NULL_RTX && n_operands == 2
2487 /* Prevent processing non-move insns. */
2488 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2489 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2490 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2491 && REG_P (no_subreg_reg_operand[0])
2492 && REG_P (no_subreg_reg_operand[1])
2493 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2494 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2495 || (! curr_alt_win[0] && curr_alt_win[1]
2496 && REG_P (no_subreg_reg_operand[1])
2497 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2498 || (curr_alt_win[0] && ! curr_alt_win[1]
2499 && REG_P (no_subreg_reg_operand[0])
2500 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2501 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2502 no_subreg_reg_operand[1])
2503 || (targetm.preferred_reload_class
2504 (no_subreg_reg_operand[1],
2505 (enum reg_class) curr_alt[1]) != NO_REGS))
2506 /* If it is a result of recent elimination in move
2507 insn we can transform it into an add still by
2508 using this alternative. */
2509 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2511 /* We have a move insn and a new reload insn will be similar
2512 to the current insn. We should avoid such situation as it
2513 results in LRA cycling. */
2514 overall += LRA_MAX_REJECT;
2516 ok_p = true;
2517 curr_alt_dont_inherit_ops_num = 0;
2518 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2520 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2521 HARD_REG_SET temp_set;
2523 i = early_clobbered_nops[nop];
2524 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2525 || hard_regno[i] < 0)
2526 continue;
2527 lra_assert (operand_reg[i] != NULL_RTX);
2528 clobbered_hard_regno = hard_regno[i];
2529 CLEAR_HARD_REG_SET (temp_set);
2530 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2531 first_conflict_j = last_conflict_j = -1;
2532 for (j = 0; j < n_operands; j++)
2533 if (j == i
2534 /* We don't want process insides of match_operator and
2535 match_parallel because otherwise we would process
2536 their operands once again generating a wrong
2537 code. */
2538 || curr_static_id->operand[j].is_operator)
2539 continue;
2540 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2541 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2542 continue;
2543 /* If we don't reload j-th operand, check conflicts. */
2544 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2545 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2547 if (first_conflict_j < 0)
2548 first_conflict_j = j;
2549 last_conflict_j = j;
2551 if (last_conflict_j < 0)
2552 continue;
2553 /* If earlyclobber operand conflicts with another
2554 non-matching operand which is actually the same register
2555 as the earlyclobber operand, it is better to reload the
2556 another operand as an operand matching the earlyclobber
2557 operand can be also the same. */
2558 if (first_conflict_j == last_conflict_j
2559 && operand_reg[last_conflict_j]
2560 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2561 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2563 curr_alt_win[last_conflict_j] = false;
2564 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2565 = last_conflict_j;
2566 losers++;
2567 /* Early clobber was already reflected in REJECT. */
2568 lra_assert (reject > 0);
2569 if (lra_dump_file != NULL)
2570 fprintf
2571 (lra_dump_file,
2572 " %d Conflict early clobber reload: reject--\n",
2574 reject--;
2575 overall += LRA_LOSER_COST_FACTOR - 1;
2577 else
2579 /* We need to reload early clobbered register and the
2580 matched registers. */
2581 for (j = 0; j < n_operands; j++)
2582 if (curr_alt_matches[j] == i)
2584 curr_alt_match_win[j] = false;
2585 losers++;
2586 overall += LRA_LOSER_COST_FACTOR;
2588 if (! curr_alt_match_win[i])
2589 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2590 else
2592 /* Remember pseudos used for match reloads are never
2593 inherited. */
2594 lra_assert (curr_alt_matches[i] >= 0);
2595 curr_alt_win[curr_alt_matches[i]] = false;
2597 curr_alt_win[i] = curr_alt_match_win[i] = false;
2598 losers++;
2599 /* Early clobber was already reflected in REJECT. */
2600 lra_assert (reject > 0);
2601 if (lra_dump_file != NULL)
2602 fprintf
2603 (lra_dump_file,
2604 " %d Matched conflict early clobber reloads:"
2605 "reject--\n",
2607 reject--;
2608 overall += LRA_LOSER_COST_FACTOR - 1;
2611 if (lra_dump_file != NULL)
2612 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2613 nalt, overall, losers, reload_nregs);
2615 /* If this alternative can be made to work by reloading, and it
2616 needs less reloading than the others checked so far, record
2617 it as the chosen goal for reloading. */
2618 if ((best_losers != 0 && losers == 0)
2619 || (((best_losers == 0 && losers == 0)
2620 || (best_losers != 0 && losers != 0))
2621 && (best_overall > overall
2622 || (best_overall == overall
2623 /* If the cost of the reloads is the same,
2624 prefer alternative which requires minimal
2625 number of reload regs. */
2626 && (reload_nregs < best_reload_nregs
2627 || (reload_nregs == best_reload_nregs
2628 && (best_reload_sum < reload_sum
2629 || (best_reload_sum == reload_sum
2630 && nalt < goal_alt_number))))))))
2632 for (nop = 0; nop < n_operands; nop++)
2634 goal_alt_win[nop] = curr_alt_win[nop];
2635 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2636 goal_alt_matches[nop] = curr_alt_matches[nop];
2637 goal_alt[nop] = curr_alt[nop];
2638 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2640 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2641 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2642 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2643 goal_alt_swapped = curr_swapped;
2644 best_overall = overall;
2645 best_losers = losers;
2646 best_reload_nregs = reload_nregs;
2647 best_reload_sum = reload_sum;
2648 goal_alt_number = nalt;
2650 if (losers == 0)
2651 /* Everything is satisfied. Do not process alternatives
2652 anymore. */
2653 break;
2654 fail:
2657 return ok_p;
2660 /* Make reload base reg from address AD. */
2661 static rtx
2662 base_to_reg (struct address_info *ad)
2664 enum reg_class cl;
2665 int code = -1;
2666 rtx new_inner = NULL_RTX;
2667 rtx new_reg = NULL_RTX;
2668 rtx_insn *insn;
2669 rtx_insn *last_insn = get_last_insn();
2671 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2672 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2673 get_index_code (ad));
2674 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2675 cl, "base");
2676 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2677 ad->disp_term == NULL
2678 ? gen_int_mode (0, ad->mode)
2679 : *ad->disp_term);
2680 if (!valid_address_p (ad->mode, new_inner, ad->as))
2681 return NULL_RTX;
2682 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base_term));
2683 code = recog_memoized (insn);
2684 if (code < 0)
2686 delete_insns_since (last_insn);
2687 return NULL_RTX;
2690 return new_inner;
2693 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2694 static rtx
2695 base_plus_disp_to_reg (struct address_info *ad)
2697 enum reg_class cl;
2698 rtx new_reg;
2700 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2701 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2702 get_index_code (ad));
2703 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2704 cl, "base + disp");
2705 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2706 return new_reg;
2709 /* Make reload of index part of address AD. Return the new
2710 pseudo. */
2711 static rtx
2712 index_part_to_reg (struct address_info *ad)
2714 rtx new_reg;
2716 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2717 INDEX_REG_CLASS, "index term");
2718 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2719 GEN_INT (get_index_scale (ad)), new_reg, 1);
2720 return new_reg;
2723 /* Return true if we can add a displacement to address AD, even if that
2724 makes the address invalid. The fix-up code requires any new address
2725 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2726 static bool
2727 can_add_disp_p (struct address_info *ad)
2729 return (!ad->autoinc_p
2730 && ad->segment == NULL
2731 && ad->base == ad->base_term
2732 && ad->disp == ad->disp_term);
2735 /* Make equiv substitution in address AD. Return true if a substitution
2736 was made. */
2737 static bool
2738 equiv_address_substitution (struct address_info *ad)
2740 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2741 HOST_WIDE_INT disp, scale;
2742 bool change_p;
2744 base_term = strip_subreg (ad->base_term);
2745 if (base_term == NULL)
2746 base_reg = new_base_reg = NULL_RTX;
2747 else
2749 base_reg = *base_term;
2750 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2752 index_term = strip_subreg (ad->index_term);
2753 if (index_term == NULL)
2754 index_reg = new_index_reg = NULL_RTX;
2755 else
2757 index_reg = *index_term;
2758 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2760 if (base_reg == new_base_reg && index_reg == new_index_reg)
2761 return false;
2762 disp = 0;
2763 change_p = false;
2764 if (lra_dump_file != NULL)
2766 fprintf (lra_dump_file, "Changing address in insn %d ",
2767 INSN_UID (curr_insn));
2768 dump_value_slim (lra_dump_file, *ad->outer, 1);
2770 if (base_reg != new_base_reg)
2772 if (REG_P (new_base_reg))
2774 *base_term = new_base_reg;
2775 change_p = true;
2777 else if (GET_CODE (new_base_reg) == PLUS
2778 && REG_P (XEXP (new_base_reg, 0))
2779 && CONST_INT_P (XEXP (new_base_reg, 1))
2780 && can_add_disp_p (ad))
2782 disp += INTVAL (XEXP (new_base_reg, 1));
2783 *base_term = XEXP (new_base_reg, 0);
2784 change_p = true;
2786 if (ad->base_term2 != NULL)
2787 *ad->base_term2 = *ad->base_term;
2789 if (index_reg != new_index_reg)
2791 if (REG_P (new_index_reg))
2793 *index_term = new_index_reg;
2794 change_p = true;
2796 else if (GET_CODE (new_index_reg) == PLUS
2797 && REG_P (XEXP (new_index_reg, 0))
2798 && CONST_INT_P (XEXP (new_index_reg, 1))
2799 && can_add_disp_p (ad)
2800 && (scale = get_index_scale (ad)))
2802 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2803 *index_term = XEXP (new_index_reg, 0);
2804 change_p = true;
2807 if (disp != 0)
2809 if (ad->disp != NULL)
2810 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2811 else
2813 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2814 update_address (ad);
2816 change_p = true;
2818 if (lra_dump_file != NULL)
2820 if (! change_p)
2821 fprintf (lra_dump_file, " -- no change\n");
2822 else
2824 fprintf (lra_dump_file, " on equiv ");
2825 dump_value_slim (lra_dump_file, *ad->outer, 1);
2826 fprintf (lra_dump_file, "\n");
2829 return change_p;
2832 /* Major function to make reloads for an address in operand NOP or
2833 check its correctness (If CHECK_ONLY_P is true). The supported
2834 cases are:
2836 1) an address that existed before LRA started, at which point it
2837 must have been valid. These addresses are subject to elimination
2838 and may have become invalid due to the elimination offset being out
2839 of range.
2841 2) an address created by forcing a constant to memory
2842 (force_const_to_mem). The initial form of these addresses might
2843 not be valid, and it is this function's job to make them valid.
2845 3) a frame address formed from a register and a (possibly zero)
2846 constant offset. As above, these addresses might not be valid and
2847 this function must make them so.
2849 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2850 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2851 address. Return true for any RTL change.
2853 The function is a helper function which does not produce all
2854 transformations (when CHECK_ONLY_P is false) which can be
2855 necessary. It does just basic steps. To do all necessary
2856 transformations use function process_address. */
2857 static bool
2858 process_address_1 (int nop, bool check_only_p,
2859 rtx_insn **before, rtx_insn **after)
2861 struct address_info ad;
2862 rtx new_reg;
2863 rtx op = *curr_id->operand_loc[nop];
2864 const char *constraint = curr_static_id->operand[nop].constraint;
2865 enum constraint_num cn = lookup_constraint (constraint);
2866 bool change_p = false;
2868 if (MEM_P (op)
2869 && GET_MODE (op) == BLKmode
2870 && GET_CODE (XEXP (op, 0)) == SCRATCH)
2871 return false;
2873 if (insn_extra_address_constraint (cn))
2874 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2875 else if (MEM_P (op))
2876 decompose_mem_address (&ad, op);
2877 else if (GET_CODE (op) == SUBREG
2878 && MEM_P (SUBREG_REG (op)))
2879 decompose_mem_address (&ad, SUBREG_REG (op));
2880 else
2881 return false;
2882 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
2883 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
2884 when INDEX_REG_CLASS is a single register class. */
2885 if (ad.base_term != NULL
2886 && ad.index_term != NULL
2887 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
2888 && REG_P (*ad.base_term)
2889 && REG_P (*ad.index_term)
2890 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
2891 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
2893 std::swap (ad.base, ad.index);
2894 std::swap (ad.base_term, ad.index_term);
2896 if (! check_only_p)
2897 change_p = equiv_address_substitution (&ad);
2898 if (ad.base_term != NULL
2899 && (process_addr_reg
2900 (ad.base_term, check_only_p, before,
2901 (ad.autoinc_p
2902 && !(REG_P (*ad.base_term)
2903 && find_regno_note (curr_insn, REG_DEAD,
2904 REGNO (*ad.base_term)) != NULL_RTX)
2905 ? after : NULL),
2906 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2907 get_index_code (&ad)))))
2909 change_p = true;
2910 if (ad.base_term2 != NULL)
2911 *ad.base_term2 = *ad.base_term;
2913 if (ad.index_term != NULL
2914 && process_addr_reg (ad.index_term, check_only_p,
2915 before, NULL, INDEX_REG_CLASS))
2916 change_p = true;
2918 /* Target hooks sometimes don't treat extra-constraint addresses as
2919 legitimate address_operands, so handle them specially. */
2920 if (insn_extra_address_constraint (cn)
2921 && satisfies_address_constraint_p (&ad, cn))
2922 return change_p;
2924 if (check_only_p)
2925 return change_p;
2927 /* There are three cases where the shape of *AD.INNER may now be invalid:
2929 1) the original address was valid, but either elimination or
2930 equiv_address_substitution was applied and that made
2931 the address invalid.
2933 2) the address is an invalid symbolic address created by
2934 force_const_to_mem.
2936 3) the address is a frame address with an invalid offset.
2938 4) the address is a frame address with an invalid base.
2940 All these cases involve a non-autoinc address, so there is no
2941 point revalidating other types. */
2942 if (ad.autoinc_p || valid_address_p (&ad))
2943 return change_p;
2945 /* Any index existed before LRA started, so we can assume that the
2946 presence and shape of the index is valid. */
2947 push_to_sequence (*before);
2948 lra_assert (ad.disp == ad.disp_term);
2949 if (ad.base == NULL)
2951 if (ad.index == NULL)
2953 int code = -1;
2954 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2955 SCRATCH, SCRATCH);
2956 rtx addr = *ad.inner;
2958 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2959 if (HAVE_lo_sum)
2961 rtx_insn *insn;
2962 rtx_insn *last = get_last_insn ();
2964 /* addr => lo_sum (new_base, addr), case (2) above. */
2965 insn = emit_insn (gen_rtx_SET
2966 (new_reg,
2967 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2968 code = recog_memoized (insn);
2969 if (code >= 0)
2971 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2972 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2974 /* Try to put lo_sum into register. */
2975 insn = emit_insn (gen_rtx_SET
2976 (new_reg,
2977 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2978 code = recog_memoized (insn);
2979 if (code >= 0)
2981 *ad.inner = new_reg;
2982 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2984 *ad.inner = addr;
2985 code = -1;
2991 if (code < 0)
2992 delete_insns_since (last);
2995 if (code < 0)
2997 /* addr => new_base, case (2) above. */
2998 lra_emit_move (new_reg, addr);
2999 *ad.inner = new_reg;
3002 else
3004 /* index * scale + disp => new base + index * scale,
3005 case (1) above. */
3006 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3007 GET_CODE (*ad.index));
3009 lra_assert (INDEX_REG_CLASS != NO_REGS);
3010 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3011 lra_emit_move (new_reg, *ad.disp);
3012 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3013 new_reg, *ad.index);
3016 else if (ad.index == NULL)
3018 int regno;
3019 enum reg_class cl;
3020 rtx set;
3021 rtx_insn *insns, *last_insn;
3022 /* Try to reload base into register only if the base is invalid
3023 for the address but with valid offset, case (4) above. */
3024 start_sequence ();
3025 new_reg = base_to_reg (&ad);
3027 /* base + disp => new base, cases (1) and (3) above. */
3028 /* Another option would be to reload the displacement into an
3029 index register. However, postreload has code to optimize
3030 address reloads that have the same base and different
3031 displacements, so reloading into an index register would
3032 not necessarily be a win. */
3033 if (new_reg == NULL_RTX)
3034 new_reg = base_plus_disp_to_reg (&ad);
3035 insns = get_insns ();
3036 last_insn = get_last_insn ();
3037 /* If we generated at least two insns, try last insn source as
3038 an address. If we succeed, we generate one less insn. */
3039 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3040 && GET_CODE (SET_SRC (set)) == PLUS
3041 && REG_P (XEXP (SET_SRC (set), 0))
3042 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3044 *ad.inner = SET_SRC (set);
3045 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3047 *ad.base_term = XEXP (SET_SRC (set), 0);
3048 *ad.disp_term = XEXP (SET_SRC (set), 1);
3049 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3050 get_index_code (&ad));
3051 regno = REGNO (*ad.base_term);
3052 if (regno >= FIRST_PSEUDO_REGISTER
3053 && cl != lra_get_allocno_class (regno))
3054 lra_change_class (regno, cl, " Change to", true);
3055 new_reg = SET_SRC (set);
3056 delete_insns_since (PREV_INSN (last_insn));
3059 /* Try if target can split displacement into legitimite new disp
3060 and offset. If it's the case, we replace the last insn with
3061 insns for base + offset => new_reg and set new_reg + new disp
3062 to *ad.inner. */
3063 last_insn = get_last_insn ();
3064 if ((set = single_set (last_insn)) != NULL_RTX
3065 && GET_CODE (SET_SRC (set)) == PLUS
3066 && REG_P (XEXP (SET_SRC (set), 0))
3067 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3068 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3070 rtx addend, disp = XEXP (SET_SRC (set), 1);
3071 if (targetm.legitimize_address_displacement (&disp, &addend,
3072 ad.mode))
3074 rtx_insn *new_insns;
3075 start_sequence ();
3076 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3077 new_insns = get_insns ();
3078 end_sequence ();
3079 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3080 delete_insns_since (PREV_INSN (last_insn));
3081 add_insn (new_insns);
3082 insns = get_insns ();
3085 end_sequence ();
3086 emit_insn (insns);
3087 *ad.inner = new_reg;
3089 else if (ad.disp_term != NULL)
3091 /* base + scale * index + disp => new base + scale * index,
3092 case (1) above. */
3093 new_reg = base_plus_disp_to_reg (&ad);
3094 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3095 new_reg, *ad.index);
3097 else if (get_index_scale (&ad) == 1)
3099 /* The last transformation to one reg will be made in
3100 curr_insn_transform function. */
3101 end_sequence ();
3102 return false;
3104 else
3106 /* base + scale * index => base + new_reg,
3107 case (1) above.
3108 Index part of address may become invalid. For example, we
3109 changed pseudo on the equivalent memory and a subreg of the
3110 pseudo onto the memory of different mode for which the scale is
3111 prohibitted. */
3112 new_reg = index_part_to_reg (&ad);
3113 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3114 *ad.base_term, new_reg);
3116 *before = get_insns ();
3117 end_sequence ();
3118 return true;
3121 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3122 Use process_address_1 as a helper function. Return true for any
3123 RTL changes.
3125 If CHECK_ONLY_P is true, just check address correctness. Return
3126 false if the address correct. */
3127 static bool
3128 process_address (int nop, bool check_only_p,
3129 rtx_insn **before, rtx_insn **after)
3131 bool res = false;
3133 while (process_address_1 (nop, check_only_p, before, after))
3135 if (check_only_p)
3136 return true;
3137 res = true;
3139 return res;
3142 /* Emit insns to reload VALUE into a new register. VALUE is an
3143 auto-increment or auto-decrement RTX whose operand is a register or
3144 memory location; so reloading involves incrementing that location.
3145 IN is either identical to VALUE, or some cheaper place to reload
3146 value being incremented/decremented from.
3148 INC_AMOUNT is the number to increment or decrement by (always
3149 positive and ignored for POST_MODIFY/PRE_MODIFY).
3151 Return pseudo containing the result. */
3152 static rtx
3153 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3155 /* REG or MEM to be copied and incremented. */
3156 rtx incloc = XEXP (value, 0);
3157 /* Nonzero if increment after copying. */
3158 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3159 || GET_CODE (value) == POST_MODIFY);
3160 rtx_insn *last;
3161 rtx inc;
3162 rtx_insn *add_insn;
3163 int code;
3164 rtx real_in = in == value ? incloc : in;
3165 rtx result;
3166 bool plus_p = true;
3168 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3170 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3171 || GET_CODE (XEXP (value, 1)) == MINUS);
3172 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3173 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3174 inc = XEXP (XEXP (value, 1), 1);
3176 else
3178 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3179 inc_amount = -inc_amount;
3181 inc = GEN_INT (inc_amount);
3184 if (! post && REG_P (incloc))
3185 result = incloc;
3186 else
3187 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3188 "INC/DEC result");
3190 if (real_in != result)
3192 /* First copy the location to the result register. */
3193 lra_assert (REG_P (result));
3194 emit_insn (gen_move_insn (result, real_in));
3197 /* We suppose that there are insns to add/sub with the constant
3198 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3199 old reload worked with this assumption. If the assumption
3200 becomes wrong, we should use approach in function
3201 base_plus_disp_to_reg. */
3202 if (in == value)
3204 /* See if we can directly increment INCLOC. */
3205 last = get_last_insn ();
3206 add_insn = emit_insn (plus_p
3207 ? gen_add2_insn (incloc, inc)
3208 : gen_sub2_insn (incloc, inc));
3210 code = recog_memoized (add_insn);
3211 if (code >= 0)
3213 if (! post && result != incloc)
3214 emit_insn (gen_move_insn (result, incloc));
3215 return result;
3217 delete_insns_since (last);
3220 /* If couldn't do the increment directly, must increment in RESULT.
3221 The way we do this depends on whether this is pre- or
3222 post-increment. For pre-increment, copy INCLOC to the reload
3223 register, increment it there, then save back. */
3224 if (! post)
3226 if (real_in != result)
3227 emit_insn (gen_move_insn (result, real_in));
3228 if (plus_p)
3229 emit_insn (gen_add2_insn (result, inc));
3230 else
3231 emit_insn (gen_sub2_insn (result, inc));
3232 if (result != incloc)
3233 emit_insn (gen_move_insn (incloc, result));
3235 else
3237 /* Post-increment.
3239 Because this might be a jump insn or a compare, and because
3240 RESULT may not be available after the insn in an input
3241 reload, we must do the incrementing before the insn being
3242 reloaded for.
3244 We have already copied IN to RESULT. Increment the copy in
3245 RESULT, save that back, then decrement RESULT so it has
3246 the original value. */
3247 if (plus_p)
3248 emit_insn (gen_add2_insn (result, inc));
3249 else
3250 emit_insn (gen_sub2_insn (result, inc));
3251 emit_insn (gen_move_insn (incloc, result));
3252 /* Restore non-modified value for the result. We prefer this
3253 way because it does not require an additional hard
3254 register. */
3255 if (plus_p)
3257 if (CONST_INT_P (inc))
3258 emit_insn (gen_add2_insn (result,
3259 gen_int_mode (-INTVAL (inc),
3260 GET_MODE (result))));
3261 else
3262 emit_insn (gen_sub2_insn (result, inc));
3264 else
3265 emit_insn (gen_add2_insn (result, inc));
3267 return result;
3270 /* Return true if the current move insn does not need processing as we
3271 already know that it satisfies its constraints. */
3272 static bool
3273 simple_move_p (void)
3275 rtx dest, src;
3276 enum reg_class dclass, sclass;
3278 lra_assert (curr_insn_set != NULL_RTX);
3279 dest = SET_DEST (curr_insn_set);
3280 src = SET_SRC (curr_insn_set);
3281 return ((dclass = get_op_class (dest)) != NO_REGS
3282 && (sclass = get_op_class (src)) != NO_REGS
3283 /* The backend guarantees that register moves of cost 2
3284 never need reloads. */
3285 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3288 /* Swap operands NOP and NOP + 1. */
3289 static inline void
3290 swap_operands (int nop)
3292 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3293 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3294 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3295 /* Swap the duplicates too. */
3296 lra_update_dup (curr_id, nop);
3297 lra_update_dup (curr_id, nop + 1);
3300 /* Main entry point of the constraint code: search the body of the
3301 current insn to choose the best alternative. It is mimicking insn
3302 alternative cost calculation model of former reload pass. That is
3303 because machine descriptions were written to use this model. This
3304 model can be changed in future. Make commutative operand exchange
3305 if it is chosen.
3307 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3308 constraints. Return true if any change happened during function
3309 call.
3311 If CHECK_ONLY_P is true then don't do any transformation. Just
3312 check that the insn satisfies all constraints. If the insn does
3313 not satisfy any constraint, return true. */
3314 static bool
3315 curr_insn_transform (bool check_only_p)
3317 int i, j, k;
3318 int n_operands;
3319 int n_alternatives;
3320 int commutative;
3321 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3322 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3323 rtx_insn *before, *after;
3324 bool alt_p = false;
3325 /* Flag that the insn has been changed through a transformation. */
3326 bool change_p;
3327 bool sec_mem_p;
3328 #ifdef SECONDARY_MEMORY_NEEDED
3329 bool use_sec_mem_p;
3330 #endif
3331 int max_regno_before;
3332 int reused_alternative_num;
3334 curr_insn_set = single_set (curr_insn);
3335 if (curr_insn_set != NULL_RTX && simple_move_p ())
3336 return false;
3338 no_input_reloads_p = no_output_reloads_p = false;
3339 goal_alt_number = -1;
3340 change_p = sec_mem_p = false;
3341 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3342 reloads; neither are insns that SET cc0. Insns that use CC0 are
3343 not allowed to have any input reloads. */
3344 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3345 no_output_reloads_p = true;
3347 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3348 no_input_reloads_p = true;
3349 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3350 no_output_reloads_p = true;
3352 n_operands = curr_static_id->n_operands;
3353 n_alternatives = curr_static_id->n_alternatives;
3355 /* Just return "no reloads" if insn has no operands with
3356 constraints. */
3357 if (n_operands == 0 || n_alternatives == 0)
3358 return false;
3360 max_regno_before = max_reg_num ();
3362 for (i = 0; i < n_operands; i++)
3364 goal_alt_matched[i][0] = -1;
3365 goal_alt_matches[i] = -1;
3368 commutative = curr_static_id->commutative;
3370 /* Now see what we need for pseudos that didn't get hard regs or got
3371 the wrong kind of hard reg. For this, we must consider all the
3372 operands together against the register constraints. */
3374 best_losers = best_overall = INT_MAX;
3375 best_reload_sum = 0;
3377 curr_swapped = false;
3378 goal_alt_swapped = false;
3380 if (! check_only_p)
3381 /* Make equivalence substitution and memory subreg elimination
3382 before address processing because an address legitimacy can
3383 depend on memory mode. */
3384 for (i = 0; i < n_operands; i++)
3386 rtx op = *curr_id->operand_loc[i];
3387 rtx subst, old = op;
3388 bool op_change_p = false;
3390 if (GET_CODE (old) == SUBREG)
3391 old = SUBREG_REG (old);
3392 subst = get_equiv_with_elimination (old, curr_insn);
3393 original_subreg_reg_mode[i] = VOIDmode;
3394 if (subst != old)
3396 subst = copy_rtx (subst);
3397 lra_assert (REG_P (old));
3398 if (GET_CODE (op) != SUBREG)
3399 *curr_id->operand_loc[i] = subst;
3400 else
3402 SUBREG_REG (op) = subst;
3403 if (GET_MODE (subst) == VOIDmode)
3404 original_subreg_reg_mode[i] = GET_MODE (old);
3406 if (lra_dump_file != NULL)
3408 fprintf (lra_dump_file,
3409 "Changing pseudo %d in operand %i of insn %u on equiv ",
3410 REGNO (old), i, INSN_UID (curr_insn));
3411 dump_value_slim (lra_dump_file, subst, 1);
3412 fprintf (lra_dump_file, "\n");
3414 op_change_p = change_p = true;
3416 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3418 change_p = true;
3419 lra_update_dup (curr_id, i);
3423 /* Reload address registers and displacements. We do it before
3424 finding an alternative because of memory constraints. */
3425 before = after = NULL;
3426 for (i = 0; i < n_operands; i++)
3427 if (! curr_static_id->operand[i].is_operator
3428 && process_address (i, check_only_p, &before, &after))
3430 if (check_only_p)
3431 return true;
3432 change_p = true;
3433 lra_update_dup (curr_id, i);
3436 if (change_p)
3437 /* If we've changed the instruction then any alternative that
3438 we chose previously may no longer be valid. */
3439 lra_set_used_insn_alternative (curr_insn, -1);
3441 if (! check_only_p && curr_insn_set != NULL_RTX
3442 && check_and_process_move (&change_p, &sec_mem_p))
3443 return change_p;
3445 try_swapped:
3447 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3448 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3449 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3450 reused_alternative_num, INSN_UID (curr_insn));
3452 if (process_alt_operands (reused_alternative_num))
3453 alt_p = true;
3455 if (check_only_p)
3456 return ! alt_p || best_losers != 0;
3458 /* If insn is commutative (it's safe to exchange a certain pair of
3459 operands) then we need to try each alternative twice, the second
3460 time matching those two operands as if we had exchanged them. To
3461 do this, really exchange them in operands.
3463 If we have just tried the alternatives the second time, return
3464 operands to normal and drop through. */
3466 if (reused_alternative_num < 0 && commutative >= 0)
3468 curr_swapped = !curr_swapped;
3469 if (curr_swapped)
3471 swap_operands (commutative);
3472 goto try_swapped;
3474 else
3475 swap_operands (commutative);
3478 if (! alt_p && ! sec_mem_p)
3480 /* No alternative works with reloads?? */
3481 if (INSN_CODE (curr_insn) >= 0)
3482 fatal_insn ("unable to generate reloads for:", curr_insn);
3483 error_for_asm (curr_insn,
3484 "inconsistent operand constraints in an %<asm%>");
3485 /* Avoid further trouble with this insn. */
3486 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3487 lra_invalidate_insn_data (curr_insn);
3488 return true;
3491 /* If the best alternative is with operands 1 and 2 swapped, swap
3492 them. Update the operand numbers of any reloads already
3493 pushed. */
3495 if (goal_alt_swapped)
3497 if (lra_dump_file != NULL)
3498 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3499 INSN_UID (curr_insn));
3501 /* Swap the duplicates too. */
3502 swap_operands (commutative);
3503 change_p = true;
3506 #ifdef SECONDARY_MEMORY_NEEDED
3507 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3508 too conservatively. So we use the secondary memory only if there
3509 is no any alternative without reloads. */
3510 use_sec_mem_p = false;
3511 if (! alt_p)
3512 use_sec_mem_p = true;
3513 else if (sec_mem_p)
3515 for (i = 0; i < n_operands; i++)
3516 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3517 break;
3518 use_sec_mem_p = i < n_operands;
3521 if (use_sec_mem_p)
3523 rtx new_reg, src, dest, rld;
3524 machine_mode sec_mode, rld_mode;
3526 lra_assert (sec_mem_p);
3527 lra_assert (curr_static_id->operand[0].type == OP_OUT
3528 && curr_static_id->operand[1].type == OP_IN);
3529 dest = *curr_id->operand_loc[0];
3530 src = *curr_id->operand_loc[1];
3531 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3532 ? dest : src);
3533 rld_mode = GET_MODE (rld);
3534 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3535 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3536 #else
3537 sec_mode = rld_mode;
3538 #endif
3539 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3540 NO_REGS, "secondary");
3541 /* If the mode is changed, it should be wider. */
3542 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3543 if (sec_mode != rld_mode)
3545 /* If the target says specifically to use another mode for
3546 secondary memory moves we can not reuse the original
3547 insn. */
3548 after = emit_spill_move (false, new_reg, dest);
3549 lra_process_new_insns (curr_insn, NULL, after,
3550 "Inserting the sec. move");
3551 /* We may have non null BEFORE here (e.g. after address
3552 processing. */
3553 push_to_sequence (before);
3554 before = emit_spill_move (true, new_reg, src);
3555 emit_insn (before);
3556 before = get_insns ();
3557 end_sequence ();
3558 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3559 lra_set_insn_deleted (curr_insn);
3561 else if (dest == rld)
3563 *curr_id->operand_loc[0] = new_reg;
3564 after = emit_spill_move (false, new_reg, dest);
3565 lra_process_new_insns (curr_insn, NULL, after,
3566 "Inserting the sec. move");
3568 else
3570 *curr_id->operand_loc[1] = new_reg;
3571 /* See comments above. */
3572 push_to_sequence (before);
3573 before = emit_spill_move (true, new_reg, src);
3574 emit_insn (before);
3575 before = get_insns ();
3576 end_sequence ();
3577 lra_process_new_insns (curr_insn, before, NULL,
3578 "Inserting the sec. move");
3580 lra_update_insn_regno_info (curr_insn);
3581 return true;
3583 #endif
3585 lra_assert (goal_alt_number >= 0);
3586 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3588 if (lra_dump_file != NULL)
3590 const char *p;
3592 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3593 goal_alt_number, INSN_UID (curr_insn));
3594 for (i = 0; i < n_operands; i++)
3596 p = (curr_static_id->operand_alternative
3597 [goal_alt_number * n_operands + i].constraint);
3598 if (*p == '\0')
3599 continue;
3600 fprintf (lra_dump_file, " (%d) ", i);
3601 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3602 fputc (*p, lra_dump_file);
3604 if (INSN_CODE (curr_insn) >= 0
3605 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3606 fprintf (lra_dump_file, " {%s}", p);
3607 if (curr_id->sp_offset != 0)
3608 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3609 curr_id->sp_offset);
3610 fprintf (lra_dump_file, "\n");
3613 /* Right now, for any pair of operands I and J that are required to
3614 match, with J < I, goal_alt_matches[I] is J. Add I to
3615 goal_alt_matched[J]. */
3617 for (i = 0; i < n_operands; i++)
3618 if ((j = goal_alt_matches[i]) >= 0)
3620 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3622 /* We allow matching one output operand and several input
3623 operands. */
3624 lra_assert (k == 0
3625 || (curr_static_id->operand[j].type == OP_OUT
3626 && curr_static_id->operand[i].type == OP_IN
3627 && (curr_static_id->operand
3628 [goal_alt_matched[j][0]].type == OP_IN)));
3629 goal_alt_matched[j][k] = i;
3630 goal_alt_matched[j][k + 1] = -1;
3633 for (i = 0; i < n_operands; i++)
3634 goal_alt_win[i] |= goal_alt_match_win[i];
3636 /* Any constants that aren't allowed and can't be reloaded into
3637 registers are here changed into memory references. */
3638 for (i = 0; i < n_operands; i++)
3639 if (goal_alt_win[i])
3641 int regno;
3642 enum reg_class new_class;
3643 rtx reg = *curr_id->operand_loc[i];
3645 if (GET_CODE (reg) == SUBREG)
3646 reg = SUBREG_REG (reg);
3648 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3650 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3652 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3654 lra_assert (ok_p);
3655 lra_change_class (regno, new_class, " Change to", true);
3659 else
3661 const char *constraint;
3662 char c;
3663 rtx op = *curr_id->operand_loc[i];
3664 rtx subreg = NULL_RTX;
3665 machine_mode mode = curr_operand_mode[i];
3667 if (GET_CODE (op) == SUBREG)
3669 subreg = op;
3670 op = SUBREG_REG (op);
3671 mode = GET_MODE (op);
3674 if (CONST_POOL_OK_P (mode, op)
3675 && ((targetm.preferred_reload_class
3676 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3677 || no_input_reloads_p))
3679 rtx tem = force_const_mem (mode, op);
3681 change_p = true;
3682 if (subreg != NULL_RTX)
3683 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3685 *curr_id->operand_loc[i] = tem;
3686 lra_update_dup (curr_id, i);
3687 process_address (i, false, &before, &after);
3689 /* If the alternative accepts constant pool refs directly
3690 there will be no reload needed at all. */
3691 if (subreg != NULL_RTX)
3692 continue;
3693 /* Skip alternatives before the one requested. */
3694 constraint = (curr_static_id->operand_alternative
3695 [goal_alt_number * n_operands + i].constraint);
3696 for (;
3697 (c = *constraint) && c != ',' && c != '#';
3698 constraint += CONSTRAINT_LEN (c, constraint))
3700 enum constraint_num cn = lookup_constraint (constraint);
3701 if (insn_extra_memory_constraint (cn)
3702 && satisfies_memory_constraint_p (tem, cn))
3703 break;
3705 if (c == '\0' || c == ',' || c == '#')
3706 continue;
3708 goal_alt_win[i] = true;
3712 for (i = 0; i < n_operands; i++)
3714 int regno;
3715 bool optional_p = false;
3716 rtx old, new_reg;
3717 rtx op = *curr_id->operand_loc[i];
3719 if (goal_alt_win[i])
3721 if (goal_alt[i] == NO_REGS
3722 && REG_P (op)
3723 /* When we assign NO_REGS it means that we will not
3724 assign a hard register to the scratch pseudo by
3725 assigment pass and the scratch pseudo will be
3726 spilled. Spilled scratch pseudos are transformed
3727 back to scratches at the LRA end. */
3728 && lra_former_scratch_operand_p (curr_insn, i))
3730 int regno = REGNO (op);
3731 lra_change_class (regno, NO_REGS, " Change to", true);
3732 if (lra_get_regno_hard_regno (regno) >= 0)
3733 /* We don't have to mark all insn affected by the
3734 spilled pseudo as there is only one such insn, the
3735 current one. */
3736 reg_renumber[regno] = -1;
3738 /* We can do an optional reload. If the pseudo got a hard
3739 reg, we might improve the code through inheritance. If
3740 it does not get a hard register we coalesce memory/memory
3741 moves later. Ignore move insns to avoid cycling. */
3742 if (! lra_simple_p
3743 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3744 && goal_alt[i] != NO_REGS && REG_P (op)
3745 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3746 && regno < new_regno_start
3747 && ! lra_former_scratch_p (regno)
3748 && reg_renumber[regno] < 0
3749 /* Check that the optional reload pseudo will be able to
3750 hold given mode value. */
3751 && ! (prohibited_class_reg_set_mode_p
3752 (goal_alt[i], reg_class_contents[goal_alt[i]],
3753 PSEUDO_REGNO_MODE (regno)))
3754 && (curr_insn_set == NULL_RTX
3755 || !((REG_P (SET_SRC (curr_insn_set))
3756 || MEM_P (SET_SRC (curr_insn_set))
3757 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3758 && (REG_P (SET_DEST (curr_insn_set))
3759 || MEM_P (SET_DEST (curr_insn_set))
3760 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3761 optional_p = true;
3762 else
3763 continue;
3766 /* Operands that match previous ones have already been handled. */
3767 if (goal_alt_matches[i] >= 0)
3768 continue;
3770 /* We should not have an operand with a non-offsettable address
3771 appearing where an offsettable address will do. It also may
3772 be a case when the address should be special in other words
3773 not a general one (e.g. it needs no index reg). */
3774 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3776 enum reg_class rclass;
3777 rtx *loc = &XEXP (op, 0);
3778 enum rtx_code code = GET_CODE (*loc);
3780 push_to_sequence (before);
3781 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3782 MEM, SCRATCH);
3783 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3784 new_reg = emit_inc (rclass, *loc, *loc,
3785 /* This value does not matter for MODIFY. */
3786 GET_MODE_SIZE (GET_MODE (op)));
3787 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3788 "offsetable address", &new_reg))
3789 lra_emit_move (new_reg, *loc);
3790 before = get_insns ();
3791 end_sequence ();
3792 *loc = new_reg;
3793 lra_update_dup (curr_id, i);
3795 else if (goal_alt_matched[i][0] == -1)
3797 machine_mode mode;
3798 rtx reg, *loc;
3799 int hard_regno, byte;
3800 enum op_type type = curr_static_id->operand[i].type;
3802 loc = curr_id->operand_loc[i];
3803 mode = curr_operand_mode[i];
3804 if (GET_CODE (*loc) == SUBREG)
3806 reg = SUBREG_REG (*loc);
3807 byte = SUBREG_BYTE (*loc);
3808 if (REG_P (reg)
3809 /* Strict_low_part requires reload the register not
3810 the sub-register. */
3811 && (curr_static_id->operand[i].strict_low
3812 || (GET_MODE_SIZE (mode)
3813 <= GET_MODE_SIZE (GET_MODE (reg))
3814 && (hard_regno
3815 = get_try_hard_regno (REGNO (reg))) >= 0
3816 && (simplify_subreg_regno
3817 (hard_regno,
3818 GET_MODE (reg), byte, mode) < 0)
3819 && (goal_alt[i] == NO_REGS
3820 || (simplify_subreg_regno
3821 (ira_class_hard_regs[goal_alt[i]][0],
3822 GET_MODE (reg), byte, mode) >= 0)))))
3824 if (type == OP_OUT)
3825 type = OP_INOUT;
3826 loc = &SUBREG_REG (*loc);
3827 mode = GET_MODE (*loc);
3830 old = *loc;
3831 if (get_reload_reg (type, mode, old, goal_alt[i],
3832 loc != curr_id->operand_loc[i], "", &new_reg)
3833 && type != OP_OUT)
3835 push_to_sequence (before);
3836 lra_emit_move (new_reg, old);
3837 before = get_insns ();
3838 end_sequence ();
3840 *loc = new_reg;
3841 if (type != OP_IN
3842 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3844 start_sequence ();
3845 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3846 emit_insn (after);
3847 after = get_insns ();
3848 end_sequence ();
3849 *loc = new_reg;
3851 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3852 if (goal_alt_dont_inherit_ops[j] == i)
3854 lra_set_regno_unique_value (REGNO (new_reg));
3855 break;
3857 lra_update_dup (curr_id, i);
3859 else if (curr_static_id->operand[i].type == OP_IN
3860 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3861 == OP_OUT))
3863 /* generate reloads for input and matched outputs. */
3864 match_inputs[0] = i;
3865 match_inputs[1] = -1;
3866 match_reload (goal_alt_matched[i][0], match_inputs,
3867 goal_alt[i], &before, &after,
3868 curr_static_id->operand_alternative
3869 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
3870 .earlyclobber);
3872 else if (curr_static_id->operand[i].type == OP_OUT
3873 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3874 == OP_IN))
3875 /* Generate reloads for output and matched inputs. */
3876 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after,
3877 curr_static_id->operand_alternative
3878 [goal_alt_number * n_operands + i].earlyclobber);
3879 else if (curr_static_id->operand[i].type == OP_IN
3880 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3881 == OP_IN))
3883 /* Generate reloads for matched inputs. */
3884 match_inputs[0] = i;
3885 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3886 match_inputs[j + 1] = k;
3887 match_inputs[j + 1] = -1;
3888 match_reload (-1, match_inputs, goal_alt[i], &before, &after, false);
3890 else
3891 /* We must generate code in any case when function
3892 process_alt_operands decides that it is possible. */
3893 gcc_unreachable ();
3894 if (optional_p)
3896 lra_assert (REG_P (op));
3897 regno = REGNO (op);
3898 op = *curr_id->operand_loc[i]; /* Substitution. */
3899 if (GET_CODE (op) == SUBREG)
3900 op = SUBREG_REG (op);
3901 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3902 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3903 lra_reg_info[REGNO (op)].restore_regno = regno;
3904 if (lra_dump_file != NULL)
3905 fprintf (lra_dump_file,
3906 " Making reload reg %d for reg %d optional\n",
3907 REGNO (op), regno);
3910 if (before != NULL_RTX || after != NULL_RTX
3911 || max_regno_before != max_reg_num ())
3912 change_p = true;
3913 if (change_p)
3915 lra_update_operator_dups (curr_id);
3916 /* Something changes -- process the insn. */
3917 lra_update_insn_regno_info (curr_insn);
3919 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3920 return change_p;
3923 /* Return true if INSN satisfies all constraints. In other words, no
3924 reload insns are needed. */
3925 bool
3926 lra_constrain_insn (rtx_insn *insn)
3928 int saved_new_regno_start = new_regno_start;
3929 int saved_new_insn_uid_start = new_insn_uid_start;
3930 bool change_p;
3932 curr_insn = insn;
3933 curr_id = lra_get_insn_recog_data (curr_insn);
3934 curr_static_id = curr_id->insn_static_data;
3935 new_insn_uid_start = get_max_uid ();
3936 new_regno_start = max_reg_num ();
3937 change_p = curr_insn_transform (true);
3938 new_regno_start = saved_new_regno_start;
3939 new_insn_uid_start = saved_new_insn_uid_start;
3940 return ! change_p;
3943 /* Return true if X is in LIST. */
3944 static bool
3945 in_list_p (rtx x, rtx list)
3947 for (; list != NULL_RTX; list = XEXP (list, 1))
3948 if (XEXP (list, 0) == x)
3949 return true;
3950 return false;
3953 /* Return true if X contains an allocatable hard register (if
3954 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3955 static bool
3956 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3958 int i, j;
3959 const char *fmt;
3960 enum rtx_code code;
3962 code = GET_CODE (x);
3963 if (REG_P (x))
3965 int regno = REGNO (x);
3966 HARD_REG_SET alloc_regs;
3968 if (hard_reg_p)
3970 if (regno >= FIRST_PSEUDO_REGISTER)
3971 regno = lra_get_regno_hard_regno (regno);
3972 if (regno < 0)
3973 return false;
3974 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3975 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3977 else
3979 if (regno < FIRST_PSEUDO_REGISTER)
3980 return false;
3981 if (! spilled_p)
3982 return true;
3983 return lra_get_regno_hard_regno (regno) < 0;
3986 fmt = GET_RTX_FORMAT (code);
3987 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3989 if (fmt[i] == 'e')
3991 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3992 return true;
3994 else if (fmt[i] == 'E')
3996 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3997 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3998 return true;
4001 return false;
4004 /* Process all regs in location *LOC and change them on equivalent
4005 substitution. Return true if any change was done. */
4006 static bool
4007 loc_equivalence_change_p (rtx *loc)
4009 rtx subst, reg, x = *loc;
4010 bool result = false;
4011 enum rtx_code code = GET_CODE (x);
4012 const char *fmt;
4013 int i, j;
4015 if (code == SUBREG)
4017 reg = SUBREG_REG (x);
4018 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4019 && GET_MODE (subst) == VOIDmode)
4021 /* We cannot reload debug location. Simplify subreg here
4022 while we know the inner mode. */
4023 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4024 GET_MODE (reg), SUBREG_BYTE (x));
4025 return true;
4028 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4030 *loc = subst;
4031 return true;
4034 /* Scan all the operand sub-expressions. */
4035 fmt = GET_RTX_FORMAT (code);
4036 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4038 if (fmt[i] == 'e')
4039 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4040 else if (fmt[i] == 'E')
4041 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4042 result
4043 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4045 return result;
4048 /* Similar to loc_equivalence_change_p, but for use as
4049 simplify_replace_fn_rtx callback. DATA is insn for which the
4050 elimination is done. If it null we don't do the elimination. */
4051 static rtx
4052 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4054 if (!REG_P (loc))
4055 return NULL_RTX;
4057 rtx subst = (data == NULL
4058 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4059 if (subst != loc)
4060 return subst;
4062 return NULL_RTX;
4065 /* Maximum number of generated reload insns per an insn. It is for
4066 preventing this pass cycling in a bug case. */
4067 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4069 /* The current iteration number of this LRA pass. */
4070 int lra_constraint_iter;
4072 /* True if we substituted equiv which needs checking register
4073 allocation correctness because the equivalent value contains
4074 allocatable hard registers or when we restore multi-register
4075 pseudo. */
4076 bool lra_risky_transformations_p;
4078 /* Return true if REGNO is referenced in more than one block. */
4079 static bool
4080 multi_block_pseudo_p (int regno)
4082 basic_block bb = NULL;
4083 unsigned int uid;
4084 bitmap_iterator bi;
4086 if (regno < FIRST_PSEUDO_REGISTER)
4087 return false;
4089 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4090 if (bb == NULL)
4091 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4092 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4093 return true;
4094 return false;
4097 /* Return true if LIST contains a deleted insn. */
4098 static bool
4099 contains_deleted_insn_p (rtx_insn_list *list)
4101 for (; list != NULL_RTX; list = list->next ())
4102 if (NOTE_P (list->insn ())
4103 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4104 return true;
4105 return false;
4108 /* Return true if X contains a pseudo dying in INSN. */
4109 static bool
4110 dead_pseudo_p (rtx x, rtx_insn *insn)
4112 int i, j;
4113 const char *fmt;
4114 enum rtx_code code;
4116 if (REG_P (x))
4117 return (insn != NULL_RTX
4118 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4119 code = GET_CODE (x);
4120 fmt = GET_RTX_FORMAT (code);
4121 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4123 if (fmt[i] == 'e')
4125 if (dead_pseudo_p (XEXP (x, i), insn))
4126 return true;
4128 else if (fmt[i] == 'E')
4130 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4131 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4132 return true;
4135 return false;
4138 /* Return true if INSN contains a dying pseudo in INSN right hand
4139 side. */
4140 static bool
4141 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4143 rtx set = single_set (insn);
4145 gcc_assert (set != NULL);
4146 return dead_pseudo_p (SET_SRC (set), insn);
4149 /* Return true if any init insn of REGNO contains a dying pseudo in
4150 insn right hand side. */
4151 static bool
4152 init_insn_rhs_dead_pseudo_p (int regno)
4154 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4156 if (insns == NULL)
4157 return false;
4158 for (; insns != NULL_RTX; insns = insns->next ())
4159 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4160 return true;
4161 return false;
4164 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4165 reverse only if we have one init insn with given REGNO as a
4166 source. */
4167 static bool
4168 reverse_equiv_p (int regno)
4170 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4171 rtx set;
4173 if (insns == NULL)
4174 return false;
4175 if (! INSN_P (insns->insn ())
4176 || insns->next () != NULL)
4177 return false;
4178 if ((set = single_set (insns->insn ())) == NULL_RTX)
4179 return false;
4180 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4183 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4184 call this function only for non-reverse equivalence. */
4185 static bool
4186 contains_reloaded_insn_p (int regno)
4188 rtx set;
4189 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4191 for (; list != NULL; list = list->next ())
4192 if ((set = single_set (list->insn ())) == NULL_RTX
4193 || ! REG_P (SET_DEST (set))
4194 || (int) REGNO (SET_DEST (set)) != regno)
4195 return true;
4196 return false;
4199 /* Entry function of LRA constraint pass. Return true if the
4200 constraint pass did change the code. */
4201 bool
4202 lra_constraints (bool first_p)
4204 bool changed_p;
4205 int i, hard_regno, new_insns_num;
4206 unsigned int min_len, new_min_len, uid;
4207 rtx set, x, reg, dest_reg;
4208 basic_block last_bb;
4209 bitmap_head equiv_insn_bitmap;
4210 bitmap_iterator bi;
4212 lra_constraint_iter++;
4213 if (lra_dump_file != NULL)
4214 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4215 lra_constraint_iter);
4216 changed_p = false;
4217 if (pic_offset_table_rtx
4218 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4219 lra_risky_transformations_p = true;
4220 else
4221 lra_risky_transformations_p = false;
4222 new_insn_uid_start = get_max_uid ();
4223 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4224 /* Mark used hard regs for target stack size calulations. */
4225 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4226 if (lra_reg_info[i].nrefs != 0
4227 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4229 int j, nregs;
4231 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4232 for (j = 0; j < nregs; j++)
4233 df_set_regs_ever_live (hard_regno + j, true);
4235 /* Do elimination before the equivalence processing as we can spill
4236 some pseudos during elimination. */
4237 lra_eliminate (false, first_p);
4238 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4239 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4240 if (lra_reg_info[i].nrefs != 0)
4242 ira_reg_equiv[i].profitable_p = true;
4243 reg = regno_reg_rtx[i];
4244 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4246 bool pseudo_p = contains_reg_p (x, false, false);
4248 /* After RTL transformation, we can not guarantee that
4249 pseudo in the substitution was not reloaded which might
4250 make equivalence invalid. For example, in reverse
4251 equiv of p0
4253 p0 <- ...
4255 equiv_mem <- p0
4257 the memory address register was reloaded before the 2nd
4258 insn. */
4259 if ((! first_p && pseudo_p)
4260 /* We don't use DF for compilation speed sake. So it
4261 is problematic to update live info when we use an
4262 equivalence containing pseudos in more than one
4263 BB. */
4264 || (pseudo_p && multi_block_pseudo_p (i))
4265 /* If an init insn was deleted for some reason, cancel
4266 the equiv. We could update the equiv insns after
4267 transformations including an equiv insn deletion
4268 but it is not worthy as such cases are extremely
4269 rare. */
4270 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4271 /* If it is not a reverse equivalence, we check that a
4272 pseudo in rhs of the init insn is not dying in the
4273 insn. Otherwise, the live info at the beginning of
4274 the corresponding BB might be wrong after we
4275 removed the insn. When the equiv can be a
4276 constant, the right hand side of the init insn can
4277 be a pseudo. */
4278 || (! reverse_equiv_p (i)
4279 && (init_insn_rhs_dead_pseudo_p (i)
4280 /* If we reloaded the pseudo in an equivalence
4281 init insn, we can not remove the equiv init
4282 insns and the init insns might write into
4283 const memory in this case. */
4284 || contains_reloaded_insn_p (i)))
4285 /* Prevent access beyond equivalent memory for
4286 paradoxical subregs. */
4287 || (MEM_P (x)
4288 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4289 > GET_MODE_SIZE (GET_MODE (x))))
4290 || (pic_offset_table_rtx
4291 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4292 && (targetm.preferred_reload_class
4293 (x, lra_get_allocno_class (i)) == NO_REGS))
4294 || contains_symbol_ref_p (x))))
4295 ira_reg_equiv[i].defined_p = false;
4296 if (contains_reg_p (x, false, true))
4297 ira_reg_equiv[i].profitable_p = false;
4298 if (get_equiv (reg) != reg)
4299 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4302 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4303 update_equiv (i);
4304 /* We should add all insns containing pseudos which should be
4305 substituted by their equivalences. */
4306 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4307 lra_push_insn_by_uid (uid);
4308 min_len = lra_insn_stack_length ();
4309 new_insns_num = 0;
4310 last_bb = NULL;
4311 changed_p = false;
4312 while ((new_min_len = lra_insn_stack_length ()) != 0)
4314 curr_insn = lra_pop_insn ();
4315 --new_min_len;
4316 curr_bb = BLOCK_FOR_INSN (curr_insn);
4317 if (curr_bb != last_bb)
4319 last_bb = curr_bb;
4320 bb_reload_num = lra_curr_reload_num;
4322 if (min_len > new_min_len)
4324 min_len = new_min_len;
4325 new_insns_num = 0;
4327 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4328 internal_error
4329 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4330 MAX_RELOAD_INSNS_NUMBER);
4331 new_insns_num++;
4332 if (DEBUG_INSN_P (curr_insn))
4334 /* We need to check equivalence in debug insn and change
4335 pseudo to the equivalent value if necessary. */
4336 curr_id = lra_get_insn_recog_data (curr_insn);
4337 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4339 rtx old = *curr_id->operand_loc[0];
4340 *curr_id->operand_loc[0]
4341 = simplify_replace_fn_rtx (old, NULL_RTX,
4342 loc_equivalence_callback, curr_insn);
4343 if (old != *curr_id->operand_loc[0])
4345 lra_update_insn_regno_info (curr_insn);
4346 changed_p = true;
4350 else if (INSN_P (curr_insn))
4352 if ((set = single_set (curr_insn)) != NULL_RTX)
4354 dest_reg = SET_DEST (set);
4355 /* The equivalence pseudo could be set up as SUBREG in a
4356 case when it is a call restore insn in a mode
4357 different from the pseudo mode. */
4358 if (GET_CODE (dest_reg) == SUBREG)
4359 dest_reg = SUBREG_REG (dest_reg);
4360 if ((REG_P (dest_reg)
4361 && (x = get_equiv (dest_reg)) != dest_reg
4362 /* Remove insns which set up a pseudo whose value
4363 can not be changed. Such insns might be not in
4364 init_insns because we don't update equiv data
4365 during insn transformations.
4367 As an example, let suppose that a pseudo got
4368 hard register and on the 1st pass was not
4369 changed to equivalent constant. We generate an
4370 additional insn setting up the pseudo because of
4371 secondary memory movement. Then the pseudo is
4372 spilled and we use the equiv constant. In this
4373 case we should remove the additional insn and
4374 this insn is not init_insns list. */
4375 && (! MEM_P (x) || MEM_READONLY_P (x)
4376 /* Check that this is actually an insn setting
4377 up the equivalence. */
4378 || in_list_p (curr_insn,
4379 ira_reg_equiv
4380 [REGNO (dest_reg)].init_insns)))
4381 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4382 && in_list_p (curr_insn,
4383 ira_reg_equiv
4384 [REGNO (SET_SRC (set))].init_insns)))
4386 /* This is equiv init insn of pseudo which did not get a
4387 hard register -- remove the insn. */
4388 if (lra_dump_file != NULL)
4390 fprintf (lra_dump_file,
4391 " Removing equiv init insn %i (freq=%d)\n",
4392 INSN_UID (curr_insn),
4393 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4394 dump_insn_slim (lra_dump_file, curr_insn);
4396 if (contains_reg_p (x, true, false))
4397 lra_risky_transformations_p = true;
4398 lra_set_insn_deleted (curr_insn);
4399 continue;
4402 curr_id = lra_get_insn_recog_data (curr_insn);
4403 curr_static_id = curr_id->insn_static_data;
4404 init_curr_insn_input_reloads ();
4405 init_curr_operand_mode ();
4406 if (curr_insn_transform (false))
4407 changed_p = true;
4408 /* Check non-transformed insns too for equiv change as USE
4409 or CLOBBER don't need reloads but can contain pseudos
4410 being changed on their equivalences. */
4411 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4412 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4414 lra_update_insn_regno_info (curr_insn);
4415 changed_p = true;
4419 bitmap_clear (&equiv_insn_bitmap);
4420 /* If we used a new hard regno, changed_p should be true because the
4421 hard reg is assigned to a new pseudo. */
4422 if (flag_checking && !changed_p)
4424 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4425 if (lra_reg_info[i].nrefs != 0
4426 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4428 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4430 for (j = 0; j < nregs; j++)
4431 lra_assert (df_regs_ever_live_p (hard_regno + j));
4434 return changed_p;
4437 /* Initiate the LRA constraint pass. It is done once per
4438 function. */
4439 void
4440 lra_constraints_init (void)
4444 /* Finalize the LRA constraint pass. It is done once per
4445 function. */
4446 void
4447 lra_constraints_finish (void)
4453 /* This page contains code to do inheritance/split
4454 transformations. */
4456 /* Number of reloads passed so far in current EBB. */
4457 static int reloads_num;
4459 /* Number of calls passed so far in current EBB. */
4460 static int calls_num;
4462 /* Current reload pseudo check for validity of elements in
4463 USAGE_INSNS. */
4464 static int curr_usage_insns_check;
4466 /* Info about last usage of registers in EBB to do inheritance/split
4467 transformation. Inheritance transformation is done from a spilled
4468 pseudo and split transformations from a hard register or a pseudo
4469 assigned to a hard register. */
4470 struct usage_insns
4472 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4473 value INSNS is valid. The insns is chain of optional debug insns
4474 and a finishing non-debug insn using the corresponding reg. The
4475 value is also used to mark the registers which are set up in the
4476 current insn. The negated insn uid is used for this. */
4477 int check;
4478 /* Value of global reloads_num at the last insn in INSNS. */
4479 int reloads_num;
4480 /* Value of global reloads_nums at the last insn in INSNS. */
4481 int calls_num;
4482 /* It can be true only for splitting. And it means that the restore
4483 insn should be put after insn given by the following member. */
4484 bool after_p;
4485 /* Next insns in the current EBB which use the original reg and the
4486 original reg value is not changed between the current insn and
4487 the next insns. In order words, e.g. for inheritance, if we need
4488 to use the original reg value again in the next insns we can try
4489 to use the value in a hard register from a reload insn of the
4490 current insn. */
4491 rtx insns;
4494 /* Map: regno -> corresponding pseudo usage insns. */
4495 static struct usage_insns *usage_insns;
4497 static void
4498 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4500 usage_insns[regno].check = curr_usage_insns_check;
4501 usage_insns[regno].insns = insn;
4502 usage_insns[regno].reloads_num = reloads_num;
4503 usage_insns[regno].calls_num = calls_num;
4504 usage_insns[regno].after_p = after_p;
4507 /* The function is used to form list REGNO usages which consists of
4508 optional debug insns finished by a non-debug insn using REGNO.
4509 RELOADS_NUM is current number of reload insns processed so far. */
4510 static void
4511 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
4513 rtx next_usage_insns;
4515 if (usage_insns[regno].check == curr_usage_insns_check
4516 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4517 && DEBUG_INSN_P (insn))
4519 /* Check that we did not add the debug insn yet. */
4520 if (next_usage_insns != insn
4521 && (GET_CODE (next_usage_insns) != INSN_LIST
4522 || XEXP (next_usage_insns, 0) != insn))
4523 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4524 next_usage_insns);
4526 else if (NONDEBUG_INSN_P (insn))
4527 setup_next_usage_insn (regno, insn, reloads_num, false);
4528 else
4529 usage_insns[regno].check = 0;
4532 /* Return first non-debug insn in list USAGE_INSNS. */
4533 static rtx_insn *
4534 skip_usage_debug_insns (rtx usage_insns)
4536 rtx insn;
4538 /* Skip debug insns. */
4539 for (insn = usage_insns;
4540 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4541 insn = XEXP (insn, 1))
4543 return safe_as_a <rtx_insn *> (insn);
4546 /* Return true if we need secondary memory moves for insn in
4547 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4548 into the insn. */
4549 static bool
4550 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4551 rtx usage_insns ATTRIBUTE_UNUSED)
4553 #ifndef SECONDARY_MEMORY_NEEDED
4554 return false;
4555 #else
4556 rtx_insn *insn;
4557 rtx set, dest;
4558 enum reg_class cl;
4560 if (inher_cl == ALL_REGS
4561 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4562 return false;
4563 lra_assert (INSN_P (insn));
4564 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4565 return false;
4566 dest = SET_DEST (set);
4567 if (! REG_P (dest))
4568 return false;
4569 lra_assert (inher_cl != NO_REGS);
4570 cl = get_reg_class (REGNO (dest));
4571 return (cl != NO_REGS && cl != ALL_REGS
4572 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4573 #endif
4576 /* Registers involved in inheritance/split in the current EBB
4577 (inheritance/split pseudos and original registers). */
4578 static bitmap_head check_only_regs;
4580 /* Do inheritance transformations for insn INSN, which defines (if
4581 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4582 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4583 form as the "insns" field of usage_insns. Return true if we
4584 succeed in such transformation.
4586 The transformations look like:
4588 p <- ... i <- ...
4589 ... p <- i (new insn)
4590 ... =>
4591 <- ... p ... <- ... i ...
4593 ... i <- p (new insn)
4594 <- ... p ... <- ... i ...
4595 ... =>
4596 <- ... p ... <- ... i ...
4597 where p is a spilled original pseudo and i is a new inheritance pseudo.
4600 The inheritance pseudo has the smallest class of two classes CL and
4601 class of ORIGINAL REGNO. */
4602 static bool
4603 inherit_reload_reg (bool def_p, int original_regno,
4604 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4606 if (optimize_function_for_size_p (cfun))
4607 return false;
4609 enum reg_class rclass = lra_get_allocno_class (original_regno);
4610 rtx original_reg = regno_reg_rtx[original_regno];
4611 rtx new_reg, usage_insn;
4612 rtx_insn *new_insns;
4614 lra_assert (! usage_insns[original_regno].after_p);
4615 if (lra_dump_file != NULL)
4616 fprintf (lra_dump_file,
4617 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4618 if (! ira_reg_classes_intersect_p[cl][rclass])
4620 if (lra_dump_file != NULL)
4622 fprintf (lra_dump_file,
4623 " Rejecting inheritance for %d "
4624 "because of disjoint classes %s and %s\n",
4625 original_regno, reg_class_names[cl],
4626 reg_class_names[rclass]);
4627 fprintf (lra_dump_file,
4628 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4630 return false;
4632 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4633 /* We don't use a subset of two classes because it can be
4634 NO_REGS. This transformation is still profitable in most
4635 cases even if the classes are not intersected as register
4636 move is probably cheaper than a memory load. */
4637 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4639 if (lra_dump_file != NULL)
4640 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4641 reg_class_names[cl], reg_class_names[rclass]);
4643 rclass = cl;
4645 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4647 /* Reject inheritance resulting in secondary memory moves.
4648 Otherwise, there is a danger in LRA cycling. Also such
4649 transformation will be unprofitable. */
4650 if (lra_dump_file != NULL)
4652 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4653 rtx set = single_set (insn);
4655 lra_assert (set != NULL_RTX);
4657 rtx dest = SET_DEST (set);
4659 lra_assert (REG_P (dest));
4660 fprintf (lra_dump_file,
4661 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4662 "as secondary mem is needed\n",
4663 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4664 original_regno, reg_class_names[rclass]);
4665 fprintf (lra_dump_file,
4666 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4668 return false;
4670 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4671 rclass, "inheritance");
4672 start_sequence ();
4673 if (def_p)
4674 lra_emit_move (original_reg, new_reg);
4675 else
4676 lra_emit_move (new_reg, original_reg);
4677 new_insns = get_insns ();
4678 end_sequence ();
4679 if (NEXT_INSN (new_insns) != NULL_RTX)
4681 if (lra_dump_file != NULL)
4683 fprintf (lra_dump_file,
4684 " Rejecting inheritance %d->%d "
4685 "as it results in 2 or more insns:\n",
4686 original_regno, REGNO (new_reg));
4687 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4688 fprintf (lra_dump_file,
4689 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4691 return false;
4693 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
4694 lra_update_insn_regno_info (insn);
4695 if (! def_p)
4696 /* We now have a new usage insn for original regno. */
4697 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4698 if (lra_dump_file != NULL)
4699 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4700 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4701 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4702 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4703 bitmap_set_bit (&check_only_regs, original_regno);
4704 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4705 if (def_p)
4706 lra_process_new_insns (insn, NULL, new_insns,
4707 "Add original<-inheritance");
4708 else
4709 lra_process_new_insns (insn, new_insns, NULL,
4710 "Add inheritance<-original");
4711 while (next_usage_insns != NULL_RTX)
4713 if (GET_CODE (next_usage_insns) != INSN_LIST)
4715 usage_insn = next_usage_insns;
4716 lra_assert (NONDEBUG_INSN_P (usage_insn));
4717 next_usage_insns = NULL;
4719 else
4721 usage_insn = XEXP (next_usage_insns, 0);
4722 lra_assert (DEBUG_INSN_P (usage_insn));
4723 next_usage_insns = XEXP (next_usage_insns, 1);
4725 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
4726 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4727 if (lra_dump_file != NULL)
4729 fprintf (lra_dump_file,
4730 " Inheritance reuse change %d->%d (bb%d):\n",
4731 original_regno, REGNO (new_reg),
4732 BLOCK_FOR_INSN (usage_insn)->index);
4733 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
4736 if (lra_dump_file != NULL)
4737 fprintf (lra_dump_file,
4738 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4739 return true;
4742 /* Return true if we need a caller save/restore for pseudo REGNO which
4743 was assigned to a hard register. */
4744 static inline bool
4745 need_for_call_save_p (int regno)
4747 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4748 return (usage_insns[regno].calls_num < calls_num
4749 && (overlaps_hard_reg_set_p
4750 ((flag_ipa_ra &&
4751 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4752 ? lra_reg_info[regno].actual_call_used_reg_set
4753 : call_used_reg_set,
4754 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4755 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4756 PSEUDO_REGNO_MODE (regno))));
4759 /* Global registers occurring in the current EBB. */
4760 static bitmap_head ebb_global_regs;
4762 /* Return true if we need a split for hard register REGNO or pseudo
4763 REGNO which was assigned to a hard register.
4764 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4765 used for reloads since the EBB end. It is an approximation of the
4766 used hard registers in the split range. The exact value would
4767 require expensive calculations. If we were aggressive with
4768 splitting because of the approximation, the split pseudo will save
4769 the same hard register assignment and will be removed in the undo
4770 pass. We still need the approximation because too aggressive
4771 splitting would result in too inaccurate cost calculation in the
4772 assignment pass because of too many generated moves which will be
4773 probably removed in the undo pass. */
4774 static inline bool
4775 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4777 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4779 lra_assert (hard_regno >= 0);
4780 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4781 /* Don't split eliminable hard registers, otherwise we can
4782 split hard registers like hard frame pointer, which
4783 lives on BB start/end according to DF-infrastructure,
4784 when there is a pseudo assigned to the register and
4785 living in the same BB. */
4786 && (regno >= FIRST_PSEUDO_REGISTER
4787 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4788 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4789 /* Don't split call clobbered hard regs living through
4790 calls, otherwise we might have a check problem in the
4791 assign sub-pass as in the most cases (exception is a
4792 situation when lra_risky_transformations_p value is
4793 true) the assign pass assumes that all pseudos living
4794 through calls are assigned to call saved hard regs. */
4795 && (regno >= FIRST_PSEUDO_REGISTER
4796 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4797 || usage_insns[regno].calls_num == calls_num)
4798 /* We need at least 2 reloads to make pseudo splitting
4799 profitable. We should provide hard regno splitting in
4800 any case to solve 1st insn scheduling problem when
4801 moving hard register definition up might result in
4802 impossibility to find hard register for reload pseudo of
4803 small register class. */
4804 && (usage_insns[regno].reloads_num
4805 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4806 && (regno < FIRST_PSEUDO_REGISTER
4807 /* For short living pseudos, spilling + inheritance can
4808 be considered a substitution for splitting.
4809 Therefore we do not splitting for local pseudos. It
4810 decreases also aggressiveness of splitting. The
4811 minimal number of references is chosen taking into
4812 account that for 2 references splitting has no sense
4813 as we can just spill the pseudo. */
4814 || (regno >= FIRST_PSEUDO_REGISTER
4815 && lra_reg_info[regno].nrefs > 3
4816 && bitmap_bit_p (&ebb_global_regs, regno))))
4817 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4820 /* Return class for the split pseudo created from original pseudo with
4821 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4822 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4823 results in no secondary memory movements. */
4824 static enum reg_class
4825 choose_split_class (enum reg_class allocno_class,
4826 int hard_regno ATTRIBUTE_UNUSED,
4827 machine_mode mode ATTRIBUTE_UNUSED)
4829 #ifndef SECONDARY_MEMORY_NEEDED
4830 return allocno_class;
4831 #else
4832 int i;
4833 enum reg_class cl, best_cl = NO_REGS;
4834 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4835 = REGNO_REG_CLASS (hard_regno);
4837 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4838 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4839 return allocno_class;
4840 for (i = 0;
4841 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4842 i++)
4843 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4844 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4845 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4846 && (best_cl == NO_REGS
4847 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4848 best_cl = cl;
4849 return best_cl;
4850 #endif
4853 /* Do split transformations for insn INSN, which defines or uses
4854 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4855 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4856 "insns" field of usage_insns.
4858 The transformations look like:
4860 p <- ... p <- ...
4861 ... s <- p (new insn -- save)
4862 ... =>
4863 ... p <- s (new insn -- restore)
4864 <- ... p ... <- ... p ...
4866 <- ... p ... <- ... p ...
4867 ... s <- p (new insn -- save)
4868 ... =>
4869 ... p <- s (new insn -- restore)
4870 <- ... p ... <- ... p ...
4872 where p is an original pseudo got a hard register or a hard
4873 register and s is a new split pseudo. The save is put before INSN
4874 if BEFORE_P is true. Return true if we succeed in such
4875 transformation. */
4876 static bool
4877 split_reg (bool before_p, int original_regno, rtx_insn *insn,
4878 rtx next_usage_insns)
4880 enum reg_class rclass;
4881 rtx original_reg;
4882 int hard_regno, nregs;
4883 rtx new_reg, usage_insn;
4884 rtx_insn *restore, *save;
4885 bool after_p;
4886 bool call_save_p;
4888 if (original_regno < FIRST_PSEUDO_REGISTER)
4890 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4891 hard_regno = original_regno;
4892 call_save_p = false;
4893 nregs = 1;
4895 else
4897 hard_regno = reg_renumber[original_regno];
4898 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4899 rclass = lra_get_allocno_class (original_regno);
4900 original_reg = regno_reg_rtx[original_regno];
4901 call_save_p = need_for_call_save_p (original_regno);
4903 original_reg = regno_reg_rtx[original_regno];
4904 lra_assert (hard_regno >= 0);
4905 if (lra_dump_file != NULL)
4906 fprintf (lra_dump_file,
4907 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4908 if (call_save_p)
4910 machine_mode mode = GET_MODE (original_reg);
4912 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4913 hard_regno_nregs[hard_regno][mode],
4914 mode);
4915 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4917 else
4919 rclass = choose_split_class (rclass, hard_regno,
4920 GET_MODE (original_reg));
4921 if (rclass == NO_REGS)
4923 if (lra_dump_file != NULL)
4925 fprintf (lra_dump_file,
4926 " Rejecting split of %d(%s): "
4927 "no good reg class for %d(%s)\n",
4928 original_regno,
4929 reg_class_names[lra_get_allocno_class (original_regno)],
4930 hard_regno,
4931 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4932 fprintf
4933 (lra_dump_file,
4934 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4936 return false;
4938 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4939 rclass, "split");
4940 reg_renumber[REGNO (new_reg)] = hard_regno;
4942 save = emit_spill_move (true, new_reg, original_reg);
4943 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
4945 if (lra_dump_file != NULL)
4947 fprintf
4948 (lra_dump_file,
4949 " Rejecting split %d->%d resulting in > 2 save insns:\n",
4950 original_regno, REGNO (new_reg));
4951 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
4952 fprintf (lra_dump_file,
4953 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4955 return false;
4957 restore = emit_spill_move (false, new_reg, original_reg);
4958 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
4960 if (lra_dump_file != NULL)
4962 fprintf (lra_dump_file,
4963 " Rejecting split %d->%d "
4964 "resulting in > 2 restore insns:\n",
4965 original_regno, REGNO (new_reg));
4966 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
4967 fprintf (lra_dump_file,
4968 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4970 return false;
4972 after_p = usage_insns[original_regno].after_p;
4973 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4974 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4975 bitmap_set_bit (&check_only_regs, original_regno);
4976 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4977 for (;;)
4979 if (GET_CODE (next_usage_insns) != INSN_LIST)
4981 usage_insn = next_usage_insns;
4982 break;
4984 usage_insn = XEXP (next_usage_insns, 0);
4985 lra_assert (DEBUG_INSN_P (usage_insn));
4986 next_usage_insns = XEXP (next_usage_insns, 1);
4987 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
4988 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4989 if (lra_dump_file != NULL)
4991 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
4992 original_regno, REGNO (new_reg));
4993 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
4996 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
4997 lra_assert (usage_insn != insn || (after_p && before_p));
4998 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
4999 after_p ? NULL : restore,
5000 after_p ? restore : NULL,
5001 call_save_p
5002 ? "Add reg<-save" : "Add reg<-split");
5003 lra_process_new_insns (insn, before_p ? save : NULL,
5004 before_p ? NULL : save,
5005 call_save_p
5006 ? "Add save<-reg" : "Add split<-reg");
5007 if (nregs > 1)
5008 /* If we are trying to split multi-register. We should check
5009 conflicts on the next assignment sub-pass. IRA can allocate on
5010 sub-register levels, LRA do this on pseudos level right now and
5011 this discrepancy may create allocation conflicts after
5012 splitting. */
5013 lra_risky_transformations_p = true;
5014 if (lra_dump_file != NULL)
5015 fprintf (lra_dump_file,
5016 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5017 return true;
5020 /* Recognize that we need a split transformation for insn INSN, which
5021 defines or uses REGNO in its insn biggest MODE (we use it only if
5022 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5023 hard registers which might be used for reloads since the EBB end.
5024 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5025 uid before starting INSN processing. Return true if we succeed in
5026 such transformation. */
5027 static bool
5028 split_if_necessary (int regno, machine_mode mode,
5029 HARD_REG_SET potential_reload_hard_regs,
5030 bool before_p, rtx_insn *insn, int max_uid)
5032 bool res = false;
5033 int i, nregs = 1;
5034 rtx next_usage_insns;
5036 if (regno < FIRST_PSEUDO_REGISTER)
5037 nregs = hard_regno_nregs[regno][mode];
5038 for (i = 0; i < nregs; i++)
5039 if (usage_insns[regno + i].check == curr_usage_insns_check
5040 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5041 /* To avoid processing the register twice or more. */
5042 && ((GET_CODE (next_usage_insns) != INSN_LIST
5043 && INSN_UID (next_usage_insns) < max_uid)
5044 || (GET_CODE (next_usage_insns) == INSN_LIST
5045 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5046 && need_for_split_p (potential_reload_hard_regs, regno + i)
5047 && split_reg (before_p, regno + i, insn, next_usage_insns))
5048 res = true;
5049 return res;
5052 /* Check only registers living at the current program point in the
5053 current EBB. */
5054 static bitmap_head live_regs;
5056 /* Update live info in EBB given by its HEAD and TAIL insns after
5057 inheritance/split transformation. The function removes dead moves
5058 too. */
5059 static void
5060 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5062 unsigned int j;
5063 int i, regno;
5064 bool live_p;
5065 rtx_insn *prev_insn;
5066 rtx set;
5067 bool remove_p;
5068 basic_block last_bb, prev_bb, curr_bb;
5069 bitmap_iterator bi;
5070 struct lra_insn_reg *reg;
5071 edge e;
5072 edge_iterator ei;
5074 last_bb = BLOCK_FOR_INSN (tail);
5075 prev_bb = NULL;
5076 for (curr_insn = tail;
5077 curr_insn != PREV_INSN (head);
5078 curr_insn = prev_insn)
5080 prev_insn = PREV_INSN (curr_insn);
5081 /* We need to process empty blocks too. They contain
5082 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5083 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5084 continue;
5085 curr_bb = BLOCK_FOR_INSN (curr_insn);
5086 if (curr_bb != prev_bb)
5088 if (prev_bb != NULL)
5090 /* Update df_get_live_in (prev_bb): */
5091 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5092 if (bitmap_bit_p (&live_regs, j))
5093 bitmap_set_bit (df_get_live_in (prev_bb), j);
5094 else
5095 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5097 if (curr_bb != last_bb)
5099 /* Update df_get_live_out (curr_bb): */
5100 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5102 live_p = bitmap_bit_p (&live_regs, j);
5103 if (! live_p)
5104 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5105 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5107 live_p = true;
5108 break;
5110 if (live_p)
5111 bitmap_set_bit (df_get_live_out (curr_bb), j);
5112 else
5113 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5116 prev_bb = curr_bb;
5117 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5119 if (! NONDEBUG_INSN_P (curr_insn))
5120 continue;
5121 curr_id = lra_get_insn_recog_data (curr_insn);
5122 curr_static_id = curr_id->insn_static_data;
5123 remove_p = false;
5124 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
5125 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5126 && bitmap_bit_p (&check_only_regs, regno)
5127 && ! bitmap_bit_p (&live_regs, regno))
5128 remove_p = true;
5129 /* See which defined values die here. */
5130 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5131 if (reg->type == OP_OUT && ! reg->subreg_p)
5132 bitmap_clear_bit (&live_regs, reg->regno);
5133 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5134 if (reg->type == OP_OUT && ! reg->subreg_p)
5135 bitmap_clear_bit (&live_regs, reg->regno);
5136 if (curr_id->arg_hard_regs != NULL)
5137 /* Make clobbered argument hard registers die. */
5138 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5139 if (regno >= FIRST_PSEUDO_REGISTER)
5140 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
5141 /* Mark each used value as live. */
5142 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5143 if (reg->type != OP_OUT
5144 && bitmap_bit_p (&check_only_regs, reg->regno))
5145 bitmap_set_bit (&live_regs, reg->regno);
5146 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5147 if (reg->type != OP_OUT
5148 && bitmap_bit_p (&check_only_regs, reg->regno))
5149 bitmap_set_bit (&live_regs, reg->regno);
5150 if (curr_id->arg_hard_regs != NULL)
5151 /* Make used argument hard registers live. */
5152 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5153 if (regno < FIRST_PSEUDO_REGISTER
5154 && bitmap_bit_p (&check_only_regs, regno))
5155 bitmap_set_bit (&live_regs, regno);
5156 /* It is quite important to remove dead move insns because it
5157 means removing dead store. We don't need to process them for
5158 constraints. */
5159 if (remove_p)
5161 if (lra_dump_file != NULL)
5163 fprintf (lra_dump_file, " Removing dead insn:\n ");
5164 dump_insn_slim (lra_dump_file, curr_insn);
5166 lra_set_insn_deleted (curr_insn);
5171 /* The structure describes info to do an inheritance for the current
5172 insn. We need to collect such info first before doing the
5173 transformations because the transformations change the insn
5174 internal representation. */
5175 struct to_inherit
5177 /* Original regno. */
5178 int regno;
5179 /* Subsequent insns which can inherit original reg value. */
5180 rtx insns;
5183 /* Array containing all info for doing inheritance from the current
5184 insn. */
5185 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5187 /* Number elements in the previous array. */
5188 static int to_inherit_num;
5190 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5191 structure to_inherit. */
5192 static void
5193 add_to_inherit (int regno, rtx insns)
5195 int i;
5197 for (i = 0; i < to_inherit_num; i++)
5198 if (to_inherit[i].regno == regno)
5199 return;
5200 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5201 to_inherit[to_inherit_num].regno = regno;
5202 to_inherit[to_inherit_num++].insns = insns;
5205 /* Return the last non-debug insn in basic block BB, or the block begin
5206 note if none. */
5207 static rtx_insn *
5208 get_last_insertion_point (basic_block bb)
5210 rtx_insn *insn;
5212 FOR_BB_INSNS_REVERSE (bb, insn)
5213 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5214 return insn;
5215 gcc_unreachable ();
5218 /* Set up RES by registers living on edges FROM except the edge (FROM,
5219 TO) or by registers set up in a jump insn in BB FROM. */
5220 static void
5221 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5223 rtx_insn *last;
5224 struct lra_insn_reg *reg;
5225 edge e;
5226 edge_iterator ei;
5228 lra_assert (to != NULL);
5229 bitmap_clear (res);
5230 FOR_EACH_EDGE (e, ei, from->succs)
5231 if (e->dest != to)
5232 bitmap_ior_into (res, df_get_live_in (e->dest));
5233 last = get_last_insertion_point (from);
5234 if (! JUMP_P (last))
5235 return;
5236 curr_id = lra_get_insn_recog_data (last);
5237 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5238 if (reg->type != OP_IN)
5239 bitmap_set_bit (res, reg->regno);
5242 /* Used as a temporary results of some bitmap calculations. */
5243 static bitmap_head temp_bitmap;
5245 /* We split for reloads of small class of hard regs. The following
5246 defines how many hard regs the class should have to be qualified as
5247 small. The code is mostly oriented to x86/x86-64 architecture
5248 where some insns need to use only specific register or pair of
5249 registers and these register can live in RTL explicitly, e.g. for
5250 parameter passing. */
5251 static const int max_small_class_regs_num = 2;
5253 /* Do inheritance/split transformations in EBB starting with HEAD and
5254 finishing on TAIL. We process EBB insns in the reverse order.
5255 Return true if we did any inheritance/split transformation in the
5256 EBB.
5258 We should avoid excessive splitting which results in worse code
5259 because of inaccurate cost calculations for spilling new split
5260 pseudos in such case. To achieve this we do splitting only if
5261 register pressure is high in given basic block and there are reload
5262 pseudos requiring hard registers. We could do more register
5263 pressure calculations at any given program point to avoid necessary
5264 splitting even more but it is to expensive and the current approach
5265 works well enough. */
5266 static bool
5267 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5269 int i, src_regno, dst_regno, nregs;
5270 bool change_p, succ_p, update_reloads_num_p;
5271 rtx_insn *prev_insn, *last_insn;
5272 rtx next_usage_insns, set;
5273 enum reg_class cl;
5274 struct lra_insn_reg *reg;
5275 basic_block last_processed_bb, curr_bb = NULL;
5276 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5277 bitmap to_process;
5278 unsigned int j;
5279 bitmap_iterator bi;
5280 bool head_p, after_p;
5282 change_p = false;
5283 curr_usage_insns_check++;
5284 reloads_num = calls_num = 0;
5285 bitmap_clear (&check_only_regs);
5286 last_processed_bb = NULL;
5287 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5288 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5289 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5290 /* We don't process new insns generated in the loop. */
5291 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5293 prev_insn = PREV_INSN (curr_insn);
5294 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5295 curr_bb = BLOCK_FOR_INSN (curr_insn);
5296 if (last_processed_bb != curr_bb)
5298 /* We are at the end of BB. Add qualified living
5299 pseudos for potential splitting. */
5300 to_process = df_get_live_out (curr_bb);
5301 if (last_processed_bb != NULL)
5303 /* We are somewhere in the middle of EBB. */
5304 get_live_on_other_edges (curr_bb, last_processed_bb,
5305 &temp_bitmap);
5306 to_process = &temp_bitmap;
5308 last_processed_bb = curr_bb;
5309 last_insn = get_last_insertion_point (curr_bb);
5310 after_p = (! JUMP_P (last_insn)
5311 && (! CALL_P (last_insn)
5312 || (find_reg_note (last_insn,
5313 REG_NORETURN, NULL_RTX) == NULL_RTX
5314 && ! SIBLING_CALL_P (last_insn))));
5315 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5316 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5318 if ((int) j >= lra_constraint_new_regno_start)
5319 break;
5320 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5322 if (j < FIRST_PSEUDO_REGISTER)
5323 SET_HARD_REG_BIT (live_hard_regs, j);
5324 else
5325 add_to_hard_reg_set (&live_hard_regs,
5326 PSEUDO_REGNO_MODE (j),
5327 reg_renumber[j]);
5328 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5332 src_regno = dst_regno = -1;
5333 if (NONDEBUG_INSN_P (curr_insn)
5334 && (set = single_set (curr_insn)) != NULL_RTX
5335 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5337 src_regno = REGNO (SET_SRC (set));
5338 dst_regno = REGNO (SET_DEST (set));
5340 update_reloads_num_p = true;
5341 if (src_regno < lra_constraint_new_regno_start
5342 && src_regno >= FIRST_PSEUDO_REGISTER
5343 && reg_renumber[src_regno] < 0
5344 && dst_regno >= lra_constraint_new_regno_start
5345 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5347 /* 'reload_pseudo <- original_pseudo'. */
5348 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5349 reloads_num++;
5350 update_reloads_num_p = false;
5351 succ_p = false;
5352 if (usage_insns[src_regno].check == curr_usage_insns_check
5353 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5354 succ_p = inherit_reload_reg (false, src_regno, cl,
5355 curr_insn, next_usage_insns);
5356 if (succ_p)
5357 change_p = true;
5358 else
5359 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5360 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5361 IOR_HARD_REG_SET (potential_reload_hard_regs,
5362 reg_class_contents[cl]);
5364 else if (src_regno >= lra_constraint_new_regno_start
5365 && dst_regno < lra_constraint_new_regno_start
5366 && dst_regno >= FIRST_PSEUDO_REGISTER
5367 && reg_renumber[dst_regno] < 0
5368 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5369 && usage_insns[dst_regno].check == curr_usage_insns_check
5370 && (next_usage_insns
5371 = usage_insns[dst_regno].insns) != NULL_RTX)
5373 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5374 reloads_num++;
5375 update_reloads_num_p = false;
5376 /* 'original_pseudo <- reload_pseudo'. */
5377 if (! JUMP_P (curr_insn)
5378 && inherit_reload_reg (true, dst_regno, cl,
5379 curr_insn, next_usage_insns))
5380 change_p = true;
5381 /* Invalidate. */
5382 usage_insns[dst_regno].check = 0;
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 (INSN_P (curr_insn))
5389 int iter;
5390 int max_uid = get_max_uid ();
5392 curr_id = lra_get_insn_recog_data (curr_insn);
5393 curr_static_id = curr_id->insn_static_data;
5394 to_inherit_num = 0;
5395 /* Process insn definitions. */
5396 for (iter = 0; iter < 2; iter++)
5397 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5398 reg != NULL;
5399 reg = reg->next)
5400 if (reg->type != OP_IN
5401 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5403 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5404 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5405 && usage_insns[dst_regno].check == curr_usage_insns_check
5406 && (next_usage_insns
5407 = usage_insns[dst_regno].insns) != NULL_RTX)
5409 struct lra_insn_reg *r;
5411 for (r = curr_id->regs; r != NULL; r = r->next)
5412 if (r->type != OP_OUT && r->regno == dst_regno)
5413 break;
5414 /* Don't do inheritance if the pseudo is also
5415 used in the insn. */
5416 if (r == NULL)
5417 /* We can not do inheritance right now
5418 because the current insn reg info (chain
5419 regs) can change after that. */
5420 add_to_inherit (dst_regno, next_usage_insns);
5422 /* We can not process one reg twice here because of
5423 usage_insns invalidation. */
5424 if ((dst_regno < FIRST_PSEUDO_REGISTER
5425 || reg_renumber[dst_regno] >= 0)
5426 && ! reg->subreg_p && reg->type != OP_IN)
5428 HARD_REG_SET s;
5430 if (split_if_necessary (dst_regno, reg->biggest_mode,
5431 potential_reload_hard_regs,
5432 false, curr_insn, max_uid))
5433 change_p = true;
5434 CLEAR_HARD_REG_SET (s);
5435 if (dst_regno < FIRST_PSEUDO_REGISTER)
5436 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5437 else
5438 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5439 reg_renumber[dst_regno]);
5440 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5442 /* We should invalidate potential inheritance or
5443 splitting for the current insn usages to the next
5444 usage insns (see code below) as the output pseudo
5445 prevents this. */
5446 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5447 && reg_renumber[dst_regno] < 0)
5448 || (reg->type == OP_OUT && ! reg->subreg_p
5449 && (dst_regno < FIRST_PSEUDO_REGISTER
5450 || reg_renumber[dst_regno] >= 0)))
5452 /* Invalidate and mark definitions. */
5453 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5454 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5455 else
5457 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5458 for (i = 0; i < nregs; i++)
5459 usage_insns[dst_regno + i].check
5460 = -(int) INSN_UID (curr_insn);
5464 /* Process clobbered call regs. */
5465 if (curr_id->arg_hard_regs != NULL)
5466 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5467 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5468 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
5469 = -(int) INSN_UID (curr_insn);
5470 if (! JUMP_P (curr_insn))
5471 for (i = 0; i < to_inherit_num; i++)
5472 if (inherit_reload_reg (true, to_inherit[i].regno,
5473 ALL_REGS, curr_insn,
5474 to_inherit[i].insns))
5475 change_p = true;
5476 if (CALL_P (curr_insn))
5478 rtx cheap, pat, dest;
5479 rtx_insn *restore;
5480 int regno, hard_regno;
5482 calls_num++;
5483 if ((cheap = find_reg_note (curr_insn,
5484 REG_RETURNED, NULL_RTX)) != NULL_RTX
5485 && ((cheap = XEXP (cheap, 0)), true)
5486 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5487 && (hard_regno = reg_renumber[regno]) >= 0
5488 /* If there are pending saves/restores, the
5489 optimization is not worth. */
5490 && usage_insns[regno].calls_num == calls_num - 1
5491 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5493 /* Restore the pseudo from the call result as
5494 REG_RETURNED note says that the pseudo value is
5495 in the call result and the pseudo is an argument
5496 of the call. */
5497 pat = PATTERN (curr_insn);
5498 if (GET_CODE (pat) == PARALLEL)
5499 pat = XVECEXP (pat, 0, 0);
5500 dest = SET_DEST (pat);
5501 /* For multiple return values dest is PARALLEL.
5502 Currently we handle only single return value case. */
5503 if (REG_P (dest))
5505 start_sequence ();
5506 emit_move_insn (cheap, copy_rtx (dest));
5507 restore = get_insns ();
5508 end_sequence ();
5509 lra_process_new_insns (curr_insn, NULL, restore,
5510 "Inserting call parameter restore");
5511 /* We don't need to save/restore of the pseudo from
5512 this call. */
5513 usage_insns[regno].calls_num = calls_num;
5514 bitmap_set_bit (&check_only_regs, regno);
5518 to_inherit_num = 0;
5519 /* Process insn usages. */
5520 for (iter = 0; iter < 2; iter++)
5521 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5522 reg != NULL;
5523 reg = reg->next)
5524 if ((reg->type != OP_OUT
5525 || (reg->type == OP_OUT && reg->subreg_p))
5526 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5528 if (src_regno >= FIRST_PSEUDO_REGISTER
5529 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5531 if (usage_insns[src_regno].check == curr_usage_insns_check
5532 && (next_usage_insns
5533 = usage_insns[src_regno].insns) != NULL_RTX
5534 && NONDEBUG_INSN_P (curr_insn))
5535 add_to_inherit (src_regno, next_usage_insns);
5536 else if (usage_insns[src_regno].check
5537 != -(int) INSN_UID (curr_insn))
5538 /* Add usages but only if the reg is not set up
5539 in the same insn. */
5540 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5542 else if (src_regno < FIRST_PSEUDO_REGISTER
5543 || reg_renumber[src_regno] >= 0)
5545 bool before_p;
5546 rtx_insn *use_insn = curr_insn;
5548 before_p = (JUMP_P (curr_insn)
5549 || (CALL_P (curr_insn) && reg->type == OP_IN));
5550 if (NONDEBUG_INSN_P (curr_insn)
5551 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5552 && split_if_necessary (src_regno, reg->biggest_mode,
5553 potential_reload_hard_regs,
5554 before_p, curr_insn, max_uid))
5556 if (reg->subreg_p)
5557 lra_risky_transformations_p = true;
5558 change_p = true;
5559 /* Invalidate. */
5560 usage_insns[src_regno].check = 0;
5561 if (before_p)
5562 use_insn = PREV_INSN (curr_insn);
5564 if (NONDEBUG_INSN_P (curr_insn))
5566 if (src_regno < FIRST_PSEUDO_REGISTER)
5567 add_to_hard_reg_set (&live_hard_regs,
5568 reg->biggest_mode, src_regno);
5569 else
5570 add_to_hard_reg_set (&live_hard_regs,
5571 PSEUDO_REGNO_MODE (src_regno),
5572 reg_renumber[src_regno]);
5574 add_next_usage_insn (src_regno, use_insn, reloads_num);
5577 /* Process used call regs. */
5578 if (curr_id->arg_hard_regs != NULL)
5579 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5580 if (src_regno < FIRST_PSEUDO_REGISTER)
5582 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5583 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5585 for (i = 0; i < to_inherit_num; i++)
5587 src_regno = to_inherit[i].regno;
5588 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5589 curr_insn, to_inherit[i].insns))
5590 change_p = true;
5591 else
5592 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5595 if (update_reloads_num_p
5596 && NONDEBUG_INSN_P (curr_insn)
5597 && (set = single_set (curr_insn)) != NULL_RTX)
5599 int regno = -1;
5600 if ((REG_P (SET_DEST (set))
5601 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5602 && reg_renumber[regno] < 0
5603 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5604 || (REG_P (SET_SRC (set))
5605 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5606 && reg_renumber[regno] < 0
5607 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5609 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5610 reloads_num++;
5611 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5612 IOR_HARD_REG_SET (potential_reload_hard_regs,
5613 reg_class_contents[cl]);
5616 /* We reached the start of the current basic block. */
5617 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5618 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5620 /* We reached the beginning of the current block -- do
5621 rest of spliting in the current BB. */
5622 to_process = df_get_live_in (curr_bb);
5623 if (BLOCK_FOR_INSN (head) != curr_bb)
5625 /* We are somewhere in the middle of EBB. */
5626 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5627 curr_bb, &temp_bitmap);
5628 to_process = &temp_bitmap;
5630 head_p = true;
5631 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5633 if ((int) j >= lra_constraint_new_regno_start)
5634 break;
5635 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5636 && usage_insns[j].check == curr_usage_insns_check
5637 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5639 if (need_for_split_p (potential_reload_hard_regs, j))
5641 if (lra_dump_file != NULL && head_p)
5643 fprintf (lra_dump_file,
5644 " ----------------------------------\n");
5645 head_p = false;
5647 if (split_reg (false, j, bb_note (curr_bb),
5648 next_usage_insns))
5649 change_p = true;
5651 usage_insns[j].check = 0;
5656 return change_p;
5659 /* This value affects EBB forming. If probability of edge from EBB to
5660 a BB is not greater than the following value, we don't add the BB
5661 to EBB. */
5662 #define EBB_PROBABILITY_CUTOFF \
5663 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
5665 /* Current number of inheritance/split iteration. */
5666 int lra_inheritance_iter;
5668 /* Entry function for inheritance/split pass. */
5669 void
5670 lra_inheritance (void)
5672 int i;
5673 basic_block bb, start_bb;
5674 edge e;
5676 lra_inheritance_iter++;
5677 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5678 return;
5679 timevar_push (TV_LRA_INHERITANCE);
5680 if (lra_dump_file != NULL)
5681 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5682 lra_inheritance_iter);
5683 curr_usage_insns_check = 0;
5684 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5685 for (i = 0; i < lra_constraint_new_regno_start; i++)
5686 usage_insns[i].check = 0;
5687 bitmap_initialize (&check_only_regs, &reg_obstack);
5688 bitmap_initialize (&live_regs, &reg_obstack);
5689 bitmap_initialize (&temp_bitmap, &reg_obstack);
5690 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5691 FOR_EACH_BB_FN (bb, cfun)
5693 start_bb = bb;
5694 if (lra_dump_file != NULL)
5695 fprintf (lra_dump_file, "EBB");
5696 /* Form a EBB starting with BB. */
5697 bitmap_clear (&ebb_global_regs);
5698 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5699 for (;;)
5701 if (lra_dump_file != NULL)
5702 fprintf (lra_dump_file, " %d", bb->index);
5703 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5704 || LABEL_P (BB_HEAD (bb->next_bb)))
5705 break;
5706 e = find_fallthru_edge (bb->succs);
5707 if (! e)
5708 break;
5709 if (e->probability < EBB_PROBABILITY_CUTOFF)
5710 break;
5711 bb = bb->next_bb;
5713 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5714 if (lra_dump_file != NULL)
5715 fprintf (lra_dump_file, "\n");
5716 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5717 /* Remember that the EBB head and tail can change in
5718 inherit_in_ebb. */
5719 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5721 bitmap_clear (&ebb_global_regs);
5722 bitmap_clear (&temp_bitmap);
5723 bitmap_clear (&live_regs);
5724 bitmap_clear (&check_only_regs);
5725 free (usage_insns);
5727 timevar_pop (TV_LRA_INHERITANCE);
5732 /* This page contains code to undo failed inheritance/split
5733 transformations. */
5735 /* Current number of iteration undoing inheritance/split. */
5736 int lra_undo_inheritance_iter;
5738 /* Fix BB live info LIVE after removing pseudos created on pass doing
5739 inheritance/split which are REMOVED_PSEUDOS. */
5740 static void
5741 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5743 unsigned int regno;
5744 bitmap_iterator bi;
5746 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5747 if (bitmap_clear_bit (live, regno))
5748 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5751 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5752 number. */
5753 static int
5754 get_regno (rtx reg)
5756 if (GET_CODE (reg) == SUBREG)
5757 reg = SUBREG_REG (reg);
5758 if (REG_P (reg))
5759 return REGNO (reg);
5760 return -1;
5763 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5764 return true if we did any change. The undo transformations for
5765 inheritance looks like
5766 i <- i2
5767 p <- i => p <- i2
5768 or removing
5769 p <- i, i <- p, and i <- i3
5770 where p is original pseudo from which inheritance pseudo i was
5771 created, i and i3 are removed inheritance pseudos, i2 is another
5772 not removed inheritance pseudo. All split pseudos or other
5773 occurrences of removed inheritance pseudos are changed on the
5774 corresponding original pseudos.
5776 The function also schedules insns changed and created during
5777 inheritance/split pass for processing by the subsequent constraint
5778 pass. */
5779 static bool
5780 remove_inheritance_pseudos (bitmap remove_pseudos)
5782 basic_block bb;
5783 int regno, sregno, prev_sregno, dregno, restore_regno;
5784 rtx set, prev_set;
5785 rtx_insn *prev_insn;
5786 bool change_p, done_p;
5788 change_p = ! bitmap_empty_p (remove_pseudos);
5789 /* We can not finish the function right away if CHANGE_P is true
5790 because we need to marks insns affected by previous
5791 inheritance/split pass for processing by the subsequent
5792 constraint pass. */
5793 FOR_EACH_BB_FN (bb, cfun)
5795 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5796 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5797 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5799 if (! INSN_P (curr_insn))
5800 continue;
5801 done_p = false;
5802 sregno = dregno = -1;
5803 if (change_p && NONDEBUG_INSN_P (curr_insn)
5804 && (set = single_set (curr_insn)) != NULL_RTX)
5806 dregno = get_regno (SET_DEST (set));
5807 sregno = get_regno (SET_SRC (set));
5810 if (sregno >= 0 && dregno >= 0)
5812 if ((bitmap_bit_p (remove_pseudos, sregno)
5813 && (lra_reg_info[sregno].restore_regno == dregno
5814 || (bitmap_bit_p (remove_pseudos, dregno)
5815 && (lra_reg_info[sregno].restore_regno
5816 == lra_reg_info[dregno].restore_regno))))
5817 || (bitmap_bit_p (remove_pseudos, dregno)
5818 && lra_reg_info[dregno].restore_regno == sregno))
5819 /* One of the following cases:
5820 original <- removed inheritance pseudo
5821 removed inherit pseudo <- another removed inherit pseudo
5822 removed inherit pseudo <- original pseudo
5824 removed_split_pseudo <- original_reg
5825 original_reg <- removed_split_pseudo */
5827 if (lra_dump_file != NULL)
5829 fprintf (lra_dump_file, " Removing %s:\n",
5830 bitmap_bit_p (&lra_split_regs, sregno)
5831 || bitmap_bit_p (&lra_split_regs, dregno)
5832 ? "split" : "inheritance");
5833 dump_insn_slim (lra_dump_file, curr_insn);
5835 lra_set_insn_deleted (curr_insn);
5836 done_p = true;
5838 else if (bitmap_bit_p (remove_pseudos, sregno)
5839 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5841 /* Search the following pattern:
5842 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5843 original_pseudo <- inherit_or_split_pseudo1
5844 where the 2nd insn is the current insn and
5845 inherit_or_split_pseudo2 is not removed. If it is found,
5846 change the current insn onto:
5847 original_pseudo <- inherit_or_split_pseudo2. */
5848 for (prev_insn = PREV_INSN (curr_insn);
5849 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5850 prev_insn = PREV_INSN (prev_insn))
5852 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5853 && (prev_set = single_set (prev_insn)) != NULL_RTX
5854 /* There should be no subregs in insn we are
5855 searching because only the original reg might
5856 be in subreg when we changed the mode of
5857 load/store for splitting. */
5858 && REG_P (SET_DEST (prev_set))
5859 && REG_P (SET_SRC (prev_set))
5860 && (int) REGNO (SET_DEST (prev_set)) == sregno
5861 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5862 >= FIRST_PSEUDO_REGISTER)
5863 /* As we consider chain of inheritance or
5864 splitting described in above comment we should
5865 check that sregno and prev_sregno were
5866 inheritance/split pseudos created from the
5867 same original regno. */
5868 && (lra_reg_info[sregno].restore_regno
5869 == lra_reg_info[prev_sregno].restore_regno)
5870 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5872 lra_assert (GET_MODE (SET_SRC (prev_set))
5873 == GET_MODE (regno_reg_rtx[sregno]));
5874 if (GET_CODE (SET_SRC (set)) == SUBREG)
5875 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5876 else
5877 SET_SRC (set) = SET_SRC (prev_set);
5878 /* As we are finishing with processing the insn
5879 here, check the destination too as it might
5880 inheritance pseudo for another pseudo. */
5881 if (bitmap_bit_p (remove_pseudos, dregno)
5882 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
5883 && (restore_regno
5884 = lra_reg_info[dregno].restore_regno) >= 0)
5886 if (GET_CODE (SET_DEST (set)) == SUBREG)
5887 SUBREG_REG (SET_DEST (set))
5888 = regno_reg_rtx[restore_regno];
5889 else
5890 SET_DEST (set) = regno_reg_rtx[restore_regno];
5892 lra_push_insn_and_update_insn_regno_info (curr_insn);
5893 lra_set_used_insn_alternative_by_uid
5894 (INSN_UID (curr_insn), -1);
5895 done_p = true;
5896 if (lra_dump_file != NULL)
5898 fprintf (lra_dump_file, " Change reload insn:\n");
5899 dump_insn_slim (lra_dump_file, curr_insn);
5904 if (! done_p)
5906 struct lra_insn_reg *reg;
5907 bool restored_regs_p = false;
5908 bool kept_regs_p = false;
5910 curr_id = lra_get_insn_recog_data (curr_insn);
5911 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5913 regno = reg->regno;
5914 restore_regno = lra_reg_info[regno].restore_regno;
5915 if (restore_regno >= 0)
5917 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5919 lra_substitute_pseudo_within_insn
5920 (curr_insn, regno, regno_reg_rtx[restore_regno],
5921 false);
5922 restored_regs_p = true;
5924 else
5925 kept_regs_p = true;
5928 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5930 /* The instruction has changed since the previous
5931 constraints pass. */
5932 lra_push_insn_and_update_insn_regno_info (curr_insn);
5933 lra_set_used_insn_alternative_by_uid
5934 (INSN_UID (curr_insn), -1);
5936 else if (restored_regs_p)
5937 /* The instruction has been restored to the form that
5938 it had during the previous constraints pass. */
5939 lra_update_insn_regno_info (curr_insn);
5940 if (restored_regs_p && lra_dump_file != NULL)
5942 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5943 dump_insn_slim (lra_dump_file, curr_insn);
5948 return change_p;
5951 /* If optional reload pseudos failed to get a hard register or was not
5952 inherited, it is better to remove optional reloads. We do this
5953 transformation after undoing inheritance to figure out necessity to
5954 remove optional reloads easier. Return true if we do any
5955 change. */
5956 static bool
5957 undo_optional_reloads (void)
5959 bool change_p, keep_p;
5960 unsigned int regno, uid;
5961 bitmap_iterator bi, bi2;
5962 rtx_insn *insn;
5963 rtx set, src, dest;
5964 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5966 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5967 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5968 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5970 keep_p = false;
5971 /* Keep optional reloads from previous subpasses. */
5972 if (lra_reg_info[regno].restore_regno < 0
5973 /* If the original pseudo changed its allocation, just
5974 removing the optional pseudo is dangerous as the original
5975 pseudo will have longer live range. */
5976 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
5977 keep_p = true;
5978 else if (reg_renumber[regno] >= 0)
5979 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
5981 insn = lra_insn_recog_data[uid]->insn;
5982 if ((set = single_set (insn)) == NULL_RTX)
5983 continue;
5984 src = SET_SRC (set);
5985 dest = SET_DEST (set);
5986 if (! REG_P (src) || ! REG_P (dest))
5987 continue;
5988 if (REGNO (dest) == regno
5989 /* Ignore insn for optional reloads itself. */
5990 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
5991 /* Check only inheritance on last inheritance pass. */
5992 && (int) REGNO (src) >= new_regno_start
5993 /* Check that the optional reload was inherited. */
5994 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
5996 keep_p = true;
5997 break;
6000 if (keep_p)
6002 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6003 if (lra_dump_file != NULL)
6004 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6007 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6008 bitmap_initialize (&insn_bitmap, &reg_obstack);
6009 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6011 if (lra_dump_file != NULL)
6012 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6013 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6014 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6016 insn = lra_insn_recog_data[uid]->insn;
6017 if ((set = single_set (insn)) != NULL_RTX)
6019 src = SET_SRC (set);
6020 dest = SET_DEST (set);
6021 if (REG_P (src) && REG_P (dest)
6022 && ((REGNO (src) == regno
6023 && (lra_reg_info[regno].restore_regno
6024 == (int) REGNO (dest)))
6025 || (REGNO (dest) == regno
6026 && (lra_reg_info[regno].restore_regno
6027 == (int) REGNO (src)))))
6029 if (lra_dump_file != NULL)
6031 fprintf (lra_dump_file, " Deleting move %u\n",
6032 INSN_UID (insn));
6033 dump_insn_slim (lra_dump_file, insn);
6035 lra_set_insn_deleted (insn);
6036 continue;
6038 /* We should not worry about generation memory-memory
6039 moves here as if the corresponding inheritance did
6040 not work (inheritance pseudo did not get a hard reg),
6041 we remove the inheritance pseudo and the optional
6042 reload. */
6044 lra_substitute_pseudo_within_insn
6045 (insn, regno, regno_reg_rtx[lra_reg_info[regno].restore_regno],
6046 false);
6047 lra_update_insn_regno_info (insn);
6048 if (lra_dump_file != NULL)
6050 fprintf (lra_dump_file,
6051 " Restoring original insn:\n");
6052 dump_insn_slim (lra_dump_file, insn);
6056 /* Clear restore_regnos. */
6057 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6058 lra_reg_info[regno].restore_regno = -1;
6059 bitmap_clear (&insn_bitmap);
6060 bitmap_clear (&removed_optional_reload_pseudos);
6061 return change_p;
6064 /* Entry function for undoing inheritance/split transformation. Return true
6065 if we did any RTL change in this pass. */
6066 bool
6067 lra_undo_inheritance (void)
6069 unsigned int regno;
6070 int restore_regno, hard_regno;
6071 int n_all_inherit, n_inherit, n_all_split, n_split;
6072 bitmap_head remove_pseudos;
6073 bitmap_iterator bi;
6074 bool change_p;
6076 lra_undo_inheritance_iter++;
6077 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6078 return false;
6079 if (lra_dump_file != NULL)
6080 fprintf (lra_dump_file,
6081 "\n********** Undoing inheritance #%d: **********\n\n",
6082 lra_undo_inheritance_iter);
6083 bitmap_initialize (&remove_pseudos, &reg_obstack);
6084 n_inherit = n_all_inherit = 0;
6085 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6086 if (lra_reg_info[regno].restore_regno >= 0)
6088 n_all_inherit++;
6089 if (reg_renumber[regno] < 0
6090 /* If the original pseudo changed its allocation, just
6091 removing inheritance is dangerous as for changing
6092 allocation we used shorter live-ranges. */
6093 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6094 bitmap_set_bit (&remove_pseudos, regno);
6095 else
6096 n_inherit++;
6098 if (lra_dump_file != NULL && n_all_inherit != 0)
6099 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6100 n_inherit, n_all_inherit,
6101 (double) n_inherit / n_all_inherit * 100);
6102 n_split = n_all_split = 0;
6103 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6104 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6106 n_all_split++;
6107 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6108 ? reg_renumber[restore_regno] : restore_regno);
6109 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6110 bitmap_set_bit (&remove_pseudos, regno);
6111 else
6113 n_split++;
6114 if (lra_dump_file != NULL)
6115 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6116 regno, restore_regno);
6119 if (lra_dump_file != NULL && n_all_split != 0)
6120 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6121 n_split, n_all_split,
6122 (double) n_split / n_all_split * 100);
6123 change_p = remove_inheritance_pseudos (&remove_pseudos);
6124 bitmap_clear (&remove_pseudos);
6125 /* Clear restore_regnos. */
6126 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6127 lra_reg_info[regno].restore_regno = -1;
6128 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6129 lra_reg_info[regno].restore_regno = -1;
6130 change_p = undo_optional_reloads () || change_p;
6131 return change_p;