[ARM] PR rtl-optimization/69904: Disallow copying/duplicating of load-exclusive opera...
[official-gcc.git] / gcc / lra-constraints.c
blobef18100125df83a8740ac154078f66d3d75bdf08
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2016 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 (special) 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 /* Searches X for any reference to a reg with the same value as REGNO,
844 returning the rtx of the reference found if any. Otherwise,
845 returns NULL_RTX. */
846 static rtx
847 regno_val_use_in (unsigned int regno, rtx x)
849 const char *fmt;
850 int i, j;
851 rtx tem;
853 if (REG_P (x) && lra_reg_info[REGNO (x)].val == lra_reg_info[regno].val)
854 return x;
856 fmt = GET_RTX_FORMAT (GET_CODE (x));
857 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
859 if (fmt[i] == 'e')
861 if ((tem = regno_val_use_in (regno, XEXP (x, i))))
862 return tem;
864 else if (fmt[i] == 'E')
865 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
866 if ((tem = regno_val_use_in (regno , XVECEXP (x, i, j))))
867 return tem;
870 return NULL_RTX;
873 /* Generate reloads for matching OUT and INS (array of input operand
874 numbers with end marker -1) with reg class GOAL_CLASS. Add input
875 and output reloads correspondingly to the lists *BEFORE and *AFTER.
876 OUT might be negative. In this case we generate input reloads for
877 matched input operands INS. EARLY_CLOBBER_P is a flag that the
878 output operand is early clobbered for chosen alternative. */
879 static void
880 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
881 rtx_insn **before, rtx_insn **after, bool early_clobber_p)
883 int i, in;
884 rtx new_in_reg, new_out_reg, reg;
885 machine_mode inmode, outmode;
886 rtx in_rtx = *curr_id->operand_loc[ins[0]];
887 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
889 inmode = curr_operand_mode[ins[0]];
890 outmode = out < 0 ? inmode : curr_operand_mode[out];
891 push_to_sequence (*before);
892 if (inmode != outmode)
894 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
896 reg = new_in_reg
897 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
898 goal_class, "");
899 if (SCALAR_INT_MODE_P (inmode))
900 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
901 else
902 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
903 LRA_SUBREG_P (new_out_reg) = 1;
904 /* If the input reg is dying here, we can use the same hard
905 register for REG and IN_RTX. We do it only for original
906 pseudos as reload pseudos can die although original
907 pseudos still live where reload pseudos dies. */
908 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
909 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
910 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
912 else
914 reg = new_out_reg
915 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
916 goal_class, "");
917 if (SCALAR_INT_MODE_P (outmode))
918 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
919 else
920 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
921 /* NEW_IN_REG is non-paradoxical subreg. We don't want
922 NEW_OUT_REG living above. We add clobber clause for
923 this. This is just a temporary clobber. We can remove
924 it at the end of LRA work. */
925 rtx_insn *clobber = emit_clobber (new_out_reg);
926 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
927 LRA_SUBREG_P (new_in_reg) = 1;
928 if (GET_CODE (in_rtx) == SUBREG)
930 rtx subreg_reg = SUBREG_REG (in_rtx);
932 /* If SUBREG_REG is dying here and sub-registers IN_RTX
933 and NEW_IN_REG are similar, we can use the same hard
934 register for REG and SUBREG_REG. */
935 if (REG_P (subreg_reg)
936 && (int) REGNO (subreg_reg) < lra_new_regno_start
937 && GET_MODE (subreg_reg) == outmode
938 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
939 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
940 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
944 else
946 /* Pseudos have values -- see comments for lra_reg_info.
947 Different pseudos with the same value do not conflict even if
948 they live in the same place. When we create a pseudo we
949 assign value of original pseudo (if any) from which we
950 created the new pseudo. If we create the pseudo from the
951 input pseudo, the new pseudo will have no conflict with the
952 input pseudo which is wrong when the input pseudo lives after
953 the insn and as the new pseudo value is changed by the insn
954 output. Therefore we create the new pseudo from the output
955 except the case when we have single matched dying input
956 pseudo.
958 We cannot reuse the current output register because we might
959 have a situation like "a <- a op b", where the constraints
960 force the second input operand ("b") to match the output
961 operand ("a"). "b" must then be copied into a new register
962 so that it doesn't clobber the current value of "a".
964 We can not use the same value if the output pseudo is
965 early clobbered or the input pseudo is mentioned in the
966 output, e.g. as an address part in memory, because
967 output reload will actually extend the pseudo liveness.
968 We don't care about eliminable hard regs here as we are
969 interesting only in pseudos. */
971 new_in_reg = new_out_reg
972 = (! early_clobber_p && ins[1] < 0 && REG_P (in_rtx)
973 && (int) REGNO (in_rtx) < lra_new_regno_start
974 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
975 && (out < 0
976 || regno_val_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
977 ? lra_create_new_reg (inmode, in_rtx, goal_class, "")
978 : lra_create_new_reg_with_unique_value (outmode, out_rtx,
979 goal_class, ""));
981 /* In operand can be got from transformations before processing insn
982 constraints. One example of such transformations is subreg
983 reloading (see function simplify_operand_subreg). The new
984 pseudos created by the transformations might have inaccurate
985 class (ALL_REGS) and we should make their classes more
986 accurate. */
987 narrow_reload_pseudo_class (in_rtx, goal_class);
988 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
989 *before = get_insns ();
990 end_sequence ();
991 for (i = 0; (in = ins[i]) >= 0; i++)
993 lra_assert
994 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
995 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
996 *curr_id->operand_loc[in] = new_in_reg;
998 lra_update_dups (curr_id, ins);
999 if (out < 0)
1000 return;
1001 /* See a comment for the input operand above. */
1002 narrow_reload_pseudo_class (out_rtx, goal_class);
1003 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
1005 start_sequence ();
1006 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
1007 emit_insn (*after);
1008 *after = get_insns ();
1009 end_sequence ();
1011 *curr_id->operand_loc[out] = new_out_reg;
1012 lra_update_dup (curr_id, out);
1015 /* Return register class which is union of all reg classes in insn
1016 constraint alternative string starting with P. */
1017 static enum reg_class
1018 reg_class_from_constraints (const char *p)
1020 int c, len;
1021 enum reg_class op_class = NO_REGS;
1024 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1026 case '#':
1027 case ',':
1028 return op_class;
1030 case 'g':
1031 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1032 break;
1034 default:
1035 enum constraint_num cn = lookup_constraint (p);
1036 enum reg_class cl = reg_class_for_constraint (cn);
1037 if (cl == NO_REGS)
1039 if (insn_extra_address_constraint (cn))
1040 op_class
1041 = (reg_class_subunion
1042 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1043 ADDRESS, SCRATCH)]);
1044 break;
1047 op_class = reg_class_subunion[op_class][cl];
1048 break;
1050 while ((p += len), c);
1051 return op_class;
1054 /* If OP is a register, return the class of the register as per
1055 get_reg_class, otherwise return NO_REGS. */
1056 static inline enum reg_class
1057 get_op_class (rtx op)
1059 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1062 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1063 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1064 SUBREG for VAL to make them equal. */
1065 static rtx_insn *
1066 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1068 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1070 /* Usually size of mem_pseudo is greater than val size but in
1071 rare cases it can be less as it can be defined by target
1072 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1073 if (! MEM_P (val))
1075 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1076 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1078 LRA_SUBREG_P (val) = 1;
1080 else
1082 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1083 LRA_SUBREG_P (mem_pseudo) = 1;
1086 return to_p ? gen_move_insn (mem_pseudo, val)
1087 : gen_move_insn (val, mem_pseudo);
1090 /* Process a special case insn (register move), return true if we
1091 don't need to process it anymore. INSN should be a single set
1092 insn. Set up that RTL was changed through CHANGE_P and macro
1093 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1094 SEC_MEM_P. */
1095 static bool
1096 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1098 int sregno, dregno;
1099 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1100 rtx_insn *before;
1101 enum reg_class dclass, sclass, secondary_class;
1102 secondary_reload_info sri;
1104 lra_assert (curr_insn_set != NULL_RTX);
1105 dreg = dest = SET_DEST (curr_insn_set);
1106 sreg = src = SET_SRC (curr_insn_set);
1107 if (GET_CODE (dest) == SUBREG)
1108 dreg = SUBREG_REG (dest);
1109 if (GET_CODE (src) == SUBREG)
1110 sreg = SUBREG_REG (src);
1111 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1112 return false;
1113 sclass = dclass = NO_REGS;
1114 if (REG_P (dreg))
1115 dclass = get_reg_class (REGNO (dreg));
1116 if (dclass == ALL_REGS)
1117 /* ALL_REGS is used for new pseudos created by transformations
1118 like reload of SUBREG_REG (see function
1119 simplify_operand_subreg). We don't know their class yet. We
1120 should figure out the class from processing the insn
1121 constraints not in this fast path function. Even if ALL_REGS
1122 were a right class for the pseudo, secondary_... hooks usually
1123 are not define for ALL_REGS. */
1124 return false;
1125 if (REG_P (sreg))
1126 sclass = get_reg_class (REGNO (sreg));
1127 if (sclass == ALL_REGS)
1128 /* See comments above. */
1129 return false;
1130 if (sclass == NO_REGS && dclass == NO_REGS)
1131 return false;
1132 #ifdef SECONDARY_MEMORY_NEEDED
1133 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1134 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1135 && ((sclass != NO_REGS && dclass != NO_REGS)
1136 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1137 #endif
1140 *sec_mem_p = true;
1141 return false;
1143 #endif
1144 if (! REG_P (dreg) || ! REG_P (sreg))
1145 return false;
1146 sri.prev_sri = NULL;
1147 sri.icode = CODE_FOR_nothing;
1148 sri.extra_cost = 0;
1149 secondary_class = NO_REGS;
1150 /* Set up hard register for a reload pseudo for hook
1151 secondary_reload because some targets just ignore unassigned
1152 pseudos in the hook. */
1153 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1155 dregno = REGNO (dreg);
1156 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1158 else
1159 dregno = -1;
1160 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1162 sregno = REGNO (sreg);
1163 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1165 else
1166 sregno = -1;
1167 if (sclass != NO_REGS)
1168 secondary_class
1169 = (enum reg_class) targetm.secondary_reload (false, dest,
1170 (reg_class_t) sclass,
1171 GET_MODE (src), &sri);
1172 if (sclass == NO_REGS
1173 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1174 && dclass != NO_REGS))
1176 enum reg_class old_sclass = secondary_class;
1177 secondary_reload_info old_sri = sri;
1179 sri.prev_sri = NULL;
1180 sri.icode = CODE_FOR_nothing;
1181 sri.extra_cost = 0;
1182 secondary_class
1183 = (enum reg_class) targetm.secondary_reload (true, src,
1184 (reg_class_t) dclass,
1185 GET_MODE (src), &sri);
1186 /* Check the target hook consistency. */
1187 lra_assert
1188 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1189 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1190 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1192 if (sregno >= 0)
1193 reg_renumber [sregno] = -1;
1194 if (dregno >= 0)
1195 reg_renumber [dregno] = -1;
1196 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1197 return false;
1198 *change_p = true;
1199 new_reg = NULL_RTX;
1200 if (secondary_class != NO_REGS)
1201 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1202 secondary_class,
1203 "secondary");
1204 start_sequence ();
1205 if (sri.icode == CODE_FOR_nothing)
1206 lra_emit_move (new_reg, src);
1207 else
1209 enum reg_class scratch_class;
1211 scratch_class = (reg_class_from_constraints
1212 (insn_data[sri.icode].operand[2].constraint));
1213 scratch_reg = (lra_create_new_reg_with_unique_value
1214 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1215 scratch_class, "scratch"));
1216 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1217 src, scratch_reg));
1219 before = get_insns ();
1220 end_sequence ();
1221 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1222 if (new_reg != NULL_RTX)
1223 SET_SRC (curr_insn_set) = new_reg;
1224 else
1226 if (lra_dump_file != NULL)
1228 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1229 dump_insn_slim (lra_dump_file, curr_insn);
1231 lra_set_insn_deleted (curr_insn);
1232 return true;
1234 return false;
1237 /* The following data describe the result of process_alt_operands.
1238 The data are used in curr_insn_transform to generate reloads. */
1240 /* The chosen reg classes which should be used for the corresponding
1241 operands. */
1242 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1243 /* True if the operand should be the same as another operand and that
1244 other operand does not need a reload. */
1245 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1246 /* True if the operand does not need a reload. */
1247 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1248 /* True if the operand can be offsetable memory. */
1249 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1250 /* The number of an operand to which given operand can be matched to. */
1251 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1252 /* The number of elements in the following array. */
1253 static int goal_alt_dont_inherit_ops_num;
1254 /* Numbers of operands whose reload pseudos should not be inherited. */
1255 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1256 /* True if the insn commutative operands should be swapped. */
1257 static bool goal_alt_swapped;
1258 /* The chosen insn alternative. */
1259 static int goal_alt_number;
1261 /* The following five variables are used to choose the best insn
1262 alternative. They reflect final characteristics of the best
1263 alternative. */
1265 /* Number of necessary reloads and overall cost reflecting the
1266 previous value and other unpleasantness of the best alternative. */
1267 static int best_losers, best_overall;
1268 /* Overall number hard registers used for reloads. For example, on
1269 some targets we need 2 general registers to reload DFmode and only
1270 one floating point register. */
1271 static int best_reload_nregs;
1272 /* Overall number reflecting distances of previous reloading the same
1273 value. The distances are counted from the current BB start. It is
1274 used to improve inheritance chances. */
1275 static int best_reload_sum;
1277 /* True if the current insn should have no correspondingly input or
1278 output reloads. */
1279 static bool no_input_reloads_p, no_output_reloads_p;
1281 /* True if we swapped the commutative operands in the current
1282 insn. */
1283 static int curr_swapped;
1285 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1286 register of class CL. Add any input reloads to list BEFORE. AFTER
1287 is nonnull if *LOC is an automodified value; handle that case by
1288 adding the required output reloads to list AFTER. Return true if
1289 the RTL was changed.
1291 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1292 register. Return false if the address register is correct. */
1293 static bool
1294 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1295 enum reg_class cl)
1297 int regno;
1298 enum reg_class rclass, new_class;
1299 rtx reg;
1300 rtx new_reg;
1301 machine_mode mode;
1302 bool subreg_p, before_p = false;
1304 subreg_p = GET_CODE (*loc) == SUBREG;
1305 if (subreg_p)
1306 loc = &SUBREG_REG (*loc);
1307 reg = *loc;
1308 mode = GET_MODE (reg);
1309 if (! REG_P (reg))
1311 if (check_only_p)
1312 return true;
1313 /* Always reload memory in an address even if the target supports
1314 such addresses. */
1315 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1316 before_p = true;
1318 else
1320 regno = REGNO (reg);
1321 rclass = get_reg_class (regno);
1322 if (! check_only_p
1323 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1325 if (lra_dump_file != NULL)
1327 fprintf (lra_dump_file,
1328 "Changing pseudo %d in address of insn %u on equiv ",
1329 REGNO (reg), INSN_UID (curr_insn));
1330 dump_value_slim (lra_dump_file, *loc, 1);
1331 fprintf (lra_dump_file, "\n");
1333 *loc = copy_rtx (*loc);
1335 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1337 if (check_only_p)
1338 return true;
1339 reg = *loc;
1340 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1341 mode, reg, cl, subreg_p, "address", &new_reg))
1342 before_p = true;
1344 else if (new_class != NO_REGS && rclass != new_class)
1346 if (check_only_p)
1347 return true;
1348 lra_change_class (regno, new_class, " Change to", true);
1349 return false;
1351 else
1352 return false;
1354 if (before_p)
1356 push_to_sequence (*before);
1357 lra_emit_move (new_reg, reg);
1358 *before = get_insns ();
1359 end_sequence ();
1361 *loc = new_reg;
1362 if (after != NULL)
1364 start_sequence ();
1365 lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1366 emit_insn (*after);
1367 *after = get_insns ();
1368 end_sequence ();
1370 return true;
1373 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1374 the insn to be inserted before curr insn. AFTER returns the
1375 the insn to be inserted after curr insn. ORIGREG and NEWREG
1376 are the original reg and new reg for reload. */
1377 static void
1378 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1379 rtx newreg)
1381 if (before)
1383 push_to_sequence (*before);
1384 lra_emit_move (newreg, origreg);
1385 *before = get_insns ();
1386 end_sequence ();
1388 if (after)
1390 start_sequence ();
1391 lra_emit_move (origreg, newreg);
1392 emit_insn (*after);
1393 *after = get_insns ();
1394 end_sequence ();
1398 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1400 /* Make reloads for subreg in operand NOP with internal subreg mode
1401 REG_MODE, add new reloads for further processing. Return true if
1402 any change was done. */
1403 static bool
1404 simplify_operand_subreg (int nop, machine_mode reg_mode)
1406 int hard_regno;
1407 rtx_insn *before, *after;
1408 machine_mode mode, innermode;
1409 rtx reg, new_reg;
1410 rtx operand = *curr_id->operand_loc[nop];
1411 enum reg_class regclass;
1412 enum op_type type;
1414 before = after = NULL;
1416 if (GET_CODE (operand) != SUBREG)
1417 return false;
1419 mode = GET_MODE (operand);
1420 reg = SUBREG_REG (operand);
1421 innermode = GET_MODE (reg);
1422 type = curr_static_id->operand[nop].type;
1423 /* If we change address for paradoxical subreg of memory, the
1424 address might violate the necessary alignment or the access might
1425 be slow. So take this into consideration. We should not worry
1426 about access beyond allocated memory for paradoxical memory
1427 subregs as we don't substitute such equiv memory (see processing
1428 equivalences in function lra_constraints) and because for spilled
1429 pseudos we allocate stack memory enough for the biggest
1430 corresponding paradoxical subreg. */
1431 if (MEM_P (reg)
1432 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1433 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1435 rtx subst, old = *curr_id->operand_loc[nop];
1437 alter_subreg (curr_id->operand_loc[nop], false);
1438 subst = *curr_id->operand_loc[nop];
1439 lra_assert (MEM_P (subst));
1440 if (! valid_address_p (innermode, XEXP (reg, 0),
1441 MEM_ADDR_SPACE (reg))
1442 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1443 MEM_ADDR_SPACE (subst)))
1444 return true;
1445 else if ((get_constraint_type (lookup_constraint
1446 (curr_static_id->operand[nop].constraint))
1447 != CT_SPECIAL_MEMORY)
1448 /* We still can reload address and if the address is
1449 valid, we can remove subreg without reloading its
1450 inner memory. */
1451 && valid_address_p (GET_MODE (subst),
1452 regno_reg_rtx
1453 [ira_class_hard_regs
1454 [base_reg_class (GET_MODE (subst),
1455 MEM_ADDR_SPACE (subst),
1456 ADDRESS, SCRATCH)][0]],
1457 MEM_ADDR_SPACE (subst)))
1458 return true;
1460 /* If the address was valid and became invalid, prefer to reload
1461 the memory. Typical case is when the index scale should
1462 correspond the memory. */
1463 *curr_id->operand_loc[nop] = old;
1465 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1467 alter_subreg (curr_id->operand_loc[nop], false);
1468 return true;
1470 else if (CONSTANT_P (reg))
1472 /* Try to simplify subreg of constant. It is usually result of
1473 equivalence substitution. */
1474 if (innermode == VOIDmode
1475 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1476 innermode = curr_static_id->operand[nop].mode;
1477 if ((new_reg = simplify_subreg (mode, reg, innermode,
1478 SUBREG_BYTE (operand))) != NULL_RTX)
1480 *curr_id->operand_loc[nop] = new_reg;
1481 return true;
1484 /* Put constant into memory when we have mixed modes. It generates
1485 a better code in most cases as it does not need a secondary
1486 reload memory. It also prevents LRA looping when LRA is using
1487 secondary reload memory again and again. */
1488 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1489 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1491 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1492 alter_subreg (curr_id->operand_loc[nop], false);
1493 return true;
1495 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1496 if there may be a problem accessing OPERAND in the outer
1497 mode. */
1498 if ((REG_P (reg)
1499 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1500 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1501 /* Don't reload paradoxical subregs because we could be looping
1502 having repeatedly final regno out of hard regs range. */
1503 && (hard_regno_nregs[hard_regno][innermode]
1504 >= hard_regno_nregs[hard_regno][mode])
1505 && simplify_subreg_regno (hard_regno, innermode,
1506 SUBREG_BYTE (operand), mode) < 0
1507 /* Don't reload subreg for matching reload. It is actually
1508 valid subreg in LRA. */
1509 && ! LRA_SUBREG_P (operand))
1510 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1512 enum reg_class rclass;
1514 if (REG_P (reg))
1515 /* There is a big probability that we will get the same class
1516 for the new pseudo and we will get the same insn which
1517 means infinite looping. So spill the new pseudo. */
1518 rclass = NO_REGS;
1519 else
1520 /* The class will be defined later in curr_insn_transform. */
1521 rclass
1522 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1524 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1525 rclass, TRUE, "subreg reg", &new_reg))
1527 bool insert_before, insert_after;
1528 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1530 insert_before = (type != OP_OUT
1531 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1532 insert_after = (type != OP_IN);
1533 insert_move_for_subreg (insert_before ? &before : NULL,
1534 insert_after ? &after : NULL,
1535 reg, new_reg);
1537 SUBREG_REG (operand) = new_reg;
1538 lra_process_new_insns (curr_insn, before, after,
1539 "Inserting subreg reload");
1540 return true;
1542 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1543 IRA allocates hardreg to the inner pseudo reg according to its mode
1544 instead of the outermode, so the size of the hardreg may not be enough
1545 to contain the outermode operand, in that case we may need to insert
1546 reload for the reg. For the following two types of paradoxical subreg,
1547 we need to insert reload:
1548 1. If the op_type is OP_IN, and the hardreg could not be paired with
1549 other hardreg to contain the outermode operand
1550 (checked by in_hard_reg_set_p), we need to insert the reload.
1551 2. If the op_type is OP_OUT or OP_INOUT.
1553 Here is a paradoxical subreg example showing how the reload is generated:
1555 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1556 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1558 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1559 here, if reg107 is assigned to hardreg R15, because R15 is the last
1560 hardreg, compiler cannot find another hardreg to pair with R15 to
1561 contain TImode data. So we insert a TImode reload reg180 for it.
1562 After reload is inserted:
1564 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1565 (reg:DI 107 [ __comp ])) -1
1566 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1567 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1569 Two reload hard registers will be allocated to reg180 to save TImode data
1570 in LRA_assign. */
1571 else if (REG_P (reg)
1572 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1573 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1574 && (hard_regno_nregs[hard_regno][innermode]
1575 < hard_regno_nregs[hard_regno][mode])
1576 && (regclass = lra_get_allocno_class (REGNO (reg)))
1577 && (type != OP_IN
1578 || !in_hard_reg_set_p (reg_class_contents[regclass],
1579 mode, hard_regno)))
1581 /* The class will be defined later in curr_insn_transform. */
1582 enum reg_class rclass
1583 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1585 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1586 rclass, TRUE, "paradoxical subreg", &new_reg))
1588 rtx subreg;
1589 bool insert_before, insert_after;
1591 PUT_MODE (new_reg, mode);
1592 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1593 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1595 insert_before = (type != OP_OUT);
1596 insert_after = (type != OP_IN);
1597 insert_move_for_subreg (insert_before ? &before : NULL,
1598 insert_after ? &after : NULL,
1599 reg, subreg);
1601 SUBREG_REG (operand) = new_reg;
1602 lra_process_new_insns (curr_insn, before, after,
1603 "Inserting paradoxical subreg reload");
1604 return true;
1606 return false;
1609 /* Return TRUE if X refers for a hard register from SET. */
1610 static bool
1611 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1613 int i, j, x_hard_regno;
1614 machine_mode mode;
1615 const char *fmt;
1616 enum rtx_code code;
1618 if (x == NULL_RTX)
1619 return false;
1620 code = GET_CODE (x);
1621 mode = GET_MODE (x);
1622 if (code == SUBREG)
1624 x = SUBREG_REG (x);
1625 code = GET_CODE (x);
1626 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1627 mode = GET_MODE (x);
1630 if (REG_P (x))
1632 x_hard_regno = get_hard_regno (x);
1633 return (x_hard_regno >= 0
1634 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1636 if (MEM_P (x))
1638 struct address_info ad;
1640 decompose_mem_address (&ad, x);
1641 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1642 return true;
1643 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1644 return true;
1646 fmt = GET_RTX_FORMAT (code);
1647 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1649 if (fmt[i] == 'e')
1651 if (uses_hard_regs_p (XEXP (x, i), set))
1652 return true;
1654 else if (fmt[i] == 'E')
1656 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1657 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1658 return true;
1661 return false;
1664 /* Return true if OP is a spilled pseudo. */
1665 static inline bool
1666 spilled_pseudo_p (rtx op)
1668 return (REG_P (op)
1669 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1672 /* Return true if X is a general constant. */
1673 static inline bool
1674 general_constant_p (rtx x)
1676 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1679 static bool
1680 reg_in_class_p (rtx reg, enum reg_class cl)
1682 if (cl == NO_REGS)
1683 return get_reg_class (REGNO (reg)) == NO_REGS;
1684 return in_class_p (reg, cl, NULL);
1687 /* Return true if SET of RCLASS contains no hard regs which can be
1688 used in MODE. */
1689 static bool
1690 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1691 HARD_REG_SET &set,
1692 enum machine_mode mode)
1694 HARD_REG_SET temp;
1696 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1697 COPY_HARD_REG_SET (temp, set);
1698 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1699 return (hard_reg_set_subset_p
1700 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1703 /* Major function to choose the current insn alternative and what
1704 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1705 negative we should consider only this alternative. Return false if
1706 we can not choose the alternative or find how to reload the
1707 operands. */
1708 static bool
1709 process_alt_operands (int only_alternative)
1711 bool ok_p = false;
1712 int nop, overall, nalt;
1713 int n_alternatives = curr_static_id->n_alternatives;
1714 int n_operands = curr_static_id->n_operands;
1715 /* LOSERS counts the operands that don't fit this alternative and
1716 would require loading. */
1717 int losers;
1718 /* REJECT is a count of how undesirable this alternative says it is
1719 if any reloading is required. If the alternative matches exactly
1720 then REJECT is ignored, but otherwise it gets this much counted
1721 against it in addition to the reloading needed. */
1722 int reject;
1723 int op_reject;
1724 /* The number of elements in the following array. */
1725 int early_clobbered_regs_num;
1726 /* Numbers of operands which are early clobber registers. */
1727 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1728 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1729 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1730 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1731 bool curr_alt_win[MAX_RECOG_OPERANDS];
1732 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1733 int curr_alt_matches[MAX_RECOG_OPERANDS];
1734 /* The number of elements in the following array. */
1735 int curr_alt_dont_inherit_ops_num;
1736 /* Numbers of operands whose reload pseudos should not be inherited. */
1737 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1738 rtx op;
1739 /* The register when the operand is a subreg of register, otherwise the
1740 operand itself. */
1741 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1742 /* The register if the operand is a register or subreg of register,
1743 otherwise NULL. */
1744 rtx operand_reg[MAX_RECOG_OPERANDS];
1745 int hard_regno[MAX_RECOG_OPERANDS];
1746 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1747 int reload_nregs, reload_sum;
1748 bool costly_p;
1749 enum reg_class cl;
1751 /* Calculate some data common for all alternatives to speed up the
1752 function. */
1753 for (nop = 0; nop < n_operands; nop++)
1755 rtx reg;
1757 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1758 /* The real hard regno of the operand after the allocation. */
1759 hard_regno[nop] = get_hard_regno (op);
1761 operand_reg[nop] = reg = op;
1762 biggest_mode[nop] = GET_MODE (op);
1763 if (GET_CODE (op) == SUBREG)
1765 operand_reg[nop] = reg = SUBREG_REG (op);
1766 if (GET_MODE_SIZE (biggest_mode[nop])
1767 < GET_MODE_SIZE (GET_MODE (reg)))
1768 biggest_mode[nop] = GET_MODE (reg);
1770 if (! REG_P (reg))
1771 operand_reg[nop] = NULL_RTX;
1772 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1773 || ((int) REGNO (reg)
1774 == lra_get_elimination_hard_regno (REGNO (reg))))
1775 no_subreg_reg_operand[nop] = reg;
1776 else
1777 operand_reg[nop] = no_subreg_reg_operand[nop]
1778 /* Just use natural mode for elimination result. It should
1779 be enough for extra constraints hooks. */
1780 = regno_reg_rtx[hard_regno[nop]];
1783 /* The constraints are made of several alternatives. Each operand's
1784 constraint looks like foo,bar,... with commas separating the
1785 alternatives. The first alternatives for all operands go
1786 together, the second alternatives go together, etc.
1788 First loop over alternatives. */
1789 alternative_mask preferred = curr_id->preferred_alternatives;
1790 if (only_alternative >= 0)
1791 preferred &= ALTERNATIVE_BIT (only_alternative);
1793 for (nalt = 0; nalt < n_alternatives; nalt++)
1795 /* Loop over operands for one constraint alternative. */
1796 if (!TEST_BIT (preferred, nalt))
1797 continue;
1799 overall = losers = reject = reload_nregs = reload_sum = 0;
1800 for (nop = 0; nop < n_operands; nop++)
1802 int inc = (curr_static_id
1803 ->operand_alternative[nalt * n_operands + nop].reject);
1804 if (lra_dump_file != NULL && inc != 0)
1805 fprintf (lra_dump_file,
1806 " Staticly defined alt reject+=%d\n", inc);
1807 reject += inc;
1809 early_clobbered_regs_num = 0;
1811 for (nop = 0; nop < n_operands; nop++)
1813 const char *p;
1814 char *end;
1815 int len, c, m, i, opalt_num, this_alternative_matches;
1816 bool win, did_match, offmemok, early_clobber_p;
1817 /* false => this operand can be reloaded somehow for this
1818 alternative. */
1819 bool badop;
1820 /* true => this operand can be reloaded if the alternative
1821 allows regs. */
1822 bool winreg;
1823 /* True if a constant forced into memory would be OK for
1824 this operand. */
1825 bool constmemok;
1826 enum reg_class this_alternative, this_costly_alternative;
1827 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1828 bool this_alternative_match_win, this_alternative_win;
1829 bool this_alternative_offmemok;
1830 bool scratch_p;
1831 machine_mode mode;
1832 enum constraint_num cn;
1834 opalt_num = nalt * n_operands + nop;
1835 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1837 /* Fast track for no constraints at all. */
1838 curr_alt[nop] = NO_REGS;
1839 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1840 curr_alt_win[nop] = true;
1841 curr_alt_match_win[nop] = false;
1842 curr_alt_offmemok[nop] = false;
1843 curr_alt_matches[nop] = -1;
1844 continue;
1847 op = no_subreg_reg_operand[nop];
1848 mode = curr_operand_mode[nop];
1850 win = did_match = winreg = offmemok = constmemok = false;
1851 badop = true;
1853 early_clobber_p = false;
1854 p = curr_static_id->operand_alternative[opalt_num].constraint;
1856 this_costly_alternative = this_alternative = NO_REGS;
1857 /* We update set of possible hard regs besides its class
1858 because reg class might be inaccurate. For example,
1859 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1860 is translated in HI_REGS because classes are merged by
1861 pairs and there is no accurate intermediate class. */
1862 CLEAR_HARD_REG_SET (this_alternative_set);
1863 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1864 this_alternative_win = false;
1865 this_alternative_match_win = false;
1866 this_alternative_offmemok = false;
1867 this_alternative_matches = -1;
1869 /* An empty constraint should be excluded by the fast
1870 track. */
1871 lra_assert (*p != 0 && *p != ',');
1873 op_reject = 0;
1874 /* Scan this alternative's specs for this operand; set WIN
1875 if the operand fits any letter in this alternative.
1876 Otherwise, clear BADOP if this operand could fit some
1877 letter after reloads, or set WINREG if this operand could
1878 fit after reloads provided the constraint allows some
1879 registers. */
1880 costly_p = false;
1883 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1885 case '\0':
1886 len = 0;
1887 break;
1888 case ',':
1889 c = '\0';
1890 break;
1892 case '&':
1893 early_clobber_p = true;
1894 break;
1896 case '$':
1897 op_reject += LRA_MAX_REJECT;
1898 break;
1899 case '^':
1900 op_reject += LRA_LOSER_COST_FACTOR;
1901 break;
1903 case '#':
1904 /* Ignore rest of this alternative. */
1905 c = '\0';
1906 break;
1908 case '0': case '1': case '2': case '3': case '4':
1909 case '5': case '6': case '7': case '8': case '9':
1911 int m_hregno;
1912 bool match_p;
1914 m = strtoul (p, &end, 10);
1915 p = end;
1916 len = 0;
1917 lra_assert (nop > m);
1919 this_alternative_matches = m;
1920 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1921 /* We are supposed to match a previous operand.
1922 If we do, we win if that one did. If we do
1923 not, count both of the operands as losers.
1924 (This is too conservative, since most of the
1925 time only a single reload insn will be needed
1926 to make the two operands win. As a result,
1927 this alternative may be rejected when it is
1928 actually desirable.) */
1929 match_p = false;
1930 if (operands_match_p (*curr_id->operand_loc[nop],
1931 *curr_id->operand_loc[m], m_hregno))
1933 /* We should reject matching of an early
1934 clobber operand if the matching operand is
1935 not dying in the insn. */
1936 if (! curr_static_id->operand[m].early_clobber
1937 || operand_reg[nop] == NULL_RTX
1938 || (find_regno_note (curr_insn, REG_DEAD,
1939 REGNO (op))
1940 || REGNO (op) == REGNO (operand_reg[m])))
1941 match_p = true;
1943 if (match_p)
1945 /* If we are matching a non-offsettable
1946 address where an offsettable address was
1947 expected, then we must reject this
1948 combination, because we can't reload
1949 it. */
1950 if (curr_alt_offmemok[m]
1951 && MEM_P (*curr_id->operand_loc[m])
1952 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1953 continue;
1955 else
1957 /* Operands don't match. Both operands must
1958 allow a reload register, otherwise we
1959 cannot make them match. */
1960 if (curr_alt[m] == NO_REGS)
1961 break;
1962 /* Retroactively mark the operand we had to
1963 match as a loser, if it wasn't already and
1964 it wasn't matched to a register constraint
1965 (e.g it might be matched by memory). */
1966 if (curr_alt_win[m]
1967 && (operand_reg[m] == NULL_RTX
1968 || hard_regno[m] < 0))
1970 losers++;
1971 reload_nregs
1972 += (ira_reg_class_max_nregs[curr_alt[m]]
1973 [GET_MODE (*curr_id->operand_loc[m])]);
1976 /* Prefer matching earlyclobber alternative as
1977 it results in less hard regs required for
1978 the insn than a non-matching earlyclobber
1979 alternative. */
1980 if (curr_static_id->operand[m].early_clobber)
1982 if (lra_dump_file != NULL)
1983 fprintf
1984 (lra_dump_file,
1985 " %d Matching earlyclobber alt:"
1986 " reject--\n",
1987 nop);
1988 reject--;
1990 /* Otherwise we prefer no matching
1991 alternatives because it gives more freedom
1992 in RA. */
1993 else if (operand_reg[nop] == NULL_RTX
1994 || (find_regno_note (curr_insn, REG_DEAD,
1995 REGNO (operand_reg[nop]))
1996 == NULL_RTX))
1998 if (lra_dump_file != NULL)
1999 fprintf
2000 (lra_dump_file,
2001 " %d Matching alt: reject+=2\n",
2002 nop);
2003 reject += 2;
2006 /* If we have to reload this operand and some
2007 previous operand also had to match the same
2008 thing as this operand, we don't know how to do
2009 that. */
2010 if (!match_p || !curr_alt_win[m])
2012 for (i = 0; i < nop; i++)
2013 if (curr_alt_matches[i] == m)
2014 break;
2015 if (i < nop)
2016 break;
2018 else
2019 did_match = true;
2021 /* This can be fixed with reloads if the operand
2022 we are supposed to match can be fixed with
2023 reloads. */
2024 badop = false;
2025 this_alternative = curr_alt[m];
2026 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
2027 winreg = this_alternative != NO_REGS;
2028 break;
2031 case 'g':
2032 if (MEM_P (op)
2033 || general_constant_p (op)
2034 || spilled_pseudo_p (op))
2035 win = true;
2036 cl = GENERAL_REGS;
2037 goto reg;
2039 default:
2040 cn = lookup_constraint (p);
2041 switch (get_constraint_type (cn))
2043 case CT_REGISTER:
2044 cl = reg_class_for_constraint (cn);
2045 if (cl != NO_REGS)
2046 goto reg;
2047 break;
2049 case CT_CONST_INT:
2050 if (CONST_INT_P (op)
2051 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2052 win = true;
2053 break;
2055 case CT_MEMORY:
2056 if (MEM_P (op)
2057 && satisfies_memory_constraint_p (op, cn))
2058 win = true;
2059 else if (spilled_pseudo_p (op))
2060 win = true;
2062 /* If we didn't already win, we can reload constants
2063 via force_const_mem or put the pseudo value into
2064 memory, or make other memory by reloading the
2065 address like for 'o'. */
2066 if (CONST_POOL_OK_P (mode, op)
2067 || MEM_P (op) || REG_P (op))
2068 badop = false;
2069 constmemok = true;
2070 offmemok = true;
2071 break;
2073 case CT_ADDRESS:
2074 /* If we didn't already win, we can reload the address
2075 into a base register. */
2076 if (satisfies_address_constraint_p (op, cn))
2077 win = true;
2078 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2079 ADDRESS, SCRATCH);
2080 badop = false;
2081 goto reg;
2083 case CT_FIXED_FORM:
2084 if (constraint_satisfied_p (op, cn))
2085 win = true;
2086 break;
2088 case CT_SPECIAL_MEMORY:
2089 if (MEM_P (op)
2090 && satisfies_memory_constraint_p (op, cn))
2091 win = true;
2092 else if (spilled_pseudo_p (op))
2093 win = true;
2094 break;
2096 break;
2098 reg:
2099 this_alternative = reg_class_subunion[this_alternative][cl];
2100 IOR_HARD_REG_SET (this_alternative_set,
2101 reg_class_contents[cl]);
2102 if (costly_p)
2104 this_costly_alternative
2105 = reg_class_subunion[this_costly_alternative][cl];
2106 IOR_HARD_REG_SET (this_costly_alternative_set,
2107 reg_class_contents[cl]);
2109 if (mode == BLKmode)
2110 break;
2111 winreg = true;
2112 if (REG_P (op))
2114 if (hard_regno[nop] >= 0
2115 && in_hard_reg_set_p (this_alternative_set,
2116 mode, hard_regno[nop]))
2117 win = true;
2118 else if (hard_regno[nop] < 0
2119 && in_class_p (op, this_alternative, NULL))
2120 win = true;
2122 break;
2124 if (c != ' ' && c != '\t')
2125 costly_p = c == '*';
2127 while ((p += len), c);
2129 scratch_p = (operand_reg[nop] != NULL_RTX
2130 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2131 /* Record which operands fit this alternative. */
2132 if (win)
2134 this_alternative_win = true;
2135 if (operand_reg[nop] != NULL_RTX)
2137 if (hard_regno[nop] >= 0)
2139 if (in_hard_reg_set_p (this_costly_alternative_set,
2140 mode, hard_regno[nop]))
2142 if (lra_dump_file != NULL)
2143 fprintf (lra_dump_file,
2144 " %d Costly set: reject++\n",
2145 nop);
2146 reject++;
2149 else
2151 /* Prefer won reg to spilled pseudo under other
2152 equal conditions for possibe inheritance. */
2153 if (! scratch_p)
2155 if (lra_dump_file != NULL)
2156 fprintf
2157 (lra_dump_file,
2158 " %d Non pseudo reload: reject++\n",
2159 nop);
2160 reject++;
2162 if (in_class_p (operand_reg[nop],
2163 this_costly_alternative, NULL))
2165 if (lra_dump_file != NULL)
2166 fprintf
2167 (lra_dump_file,
2168 " %d Non pseudo costly reload:"
2169 " reject++\n",
2170 nop);
2171 reject++;
2174 /* We simulate the behavior of old reload here.
2175 Although scratches need hard registers and it
2176 might result in spilling other pseudos, no reload
2177 insns are generated for the scratches. So it
2178 might cost something but probably less than old
2179 reload pass believes. */
2180 if (scratch_p)
2182 if (lra_dump_file != NULL)
2183 fprintf (lra_dump_file,
2184 " %d Scratch win: reject+=2\n",
2185 nop);
2186 reject += 2;
2190 else if (did_match)
2191 this_alternative_match_win = true;
2192 else
2194 int const_to_mem = 0;
2195 bool no_regs_p;
2197 reject += op_reject;
2198 /* Never do output reload of stack pointer. It makes
2199 impossible to do elimination when SP is changed in
2200 RTL. */
2201 if (op == stack_pointer_rtx && ! frame_pointer_needed
2202 && curr_static_id->operand[nop].type != OP_IN)
2203 goto fail;
2205 /* If this alternative asks for a specific reg class, see if there
2206 is at least one allocatable register in that class. */
2207 no_regs_p
2208 = (this_alternative == NO_REGS
2209 || (hard_reg_set_subset_p
2210 (reg_class_contents[this_alternative],
2211 lra_no_alloc_regs)));
2213 /* For asms, verify that the class for this alternative is possible
2214 for the mode that is specified. */
2215 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2217 int i;
2218 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2219 if (HARD_REGNO_MODE_OK (i, mode)
2220 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2221 mode, i))
2222 break;
2223 if (i == FIRST_PSEUDO_REGISTER)
2224 winreg = false;
2227 /* If this operand accepts a register, and if the
2228 register class has at least one allocatable register,
2229 then this operand can be reloaded. */
2230 if (winreg && !no_regs_p)
2231 badop = false;
2233 if (badop)
2235 if (lra_dump_file != NULL)
2236 fprintf (lra_dump_file,
2237 " alt=%d: Bad operand -- refuse\n",
2238 nalt);
2239 goto fail;
2242 /* If not assigned pseudo has a class which a subset of
2243 required reg class, it is a less costly alternative
2244 as the pseudo still can get a hard reg of necessary
2245 class. */
2246 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2247 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2248 && ira_class_subset_p[this_alternative][cl])
2250 if (lra_dump_file != NULL)
2251 fprintf
2252 (lra_dump_file,
2253 " %d Super set class reg: reject-=3\n", nop);
2254 reject -= 3;
2257 this_alternative_offmemok = offmemok;
2258 if (this_costly_alternative != NO_REGS)
2260 if (lra_dump_file != NULL)
2261 fprintf (lra_dump_file,
2262 " %d Costly loser: reject++\n", nop);
2263 reject++;
2265 /* If the operand is dying, has a matching constraint,
2266 and satisfies constraints of the matched operand
2267 which failed to satisfy the own constraints, most probably
2268 the reload for this operand will be gone. */
2269 if (this_alternative_matches >= 0
2270 && !curr_alt_win[this_alternative_matches]
2271 && REG_P (op)
2272 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2273 && (hard_regno[nop] >= 0
2274 ? in_hard_reg_set_p (this_alternative_set,
2275 mode, hard_regno[nop])
2276 : in_class_p (op, this_alternative, NULL)))
2278 if (lra_dump_file != NULL)
2279 fprintf
2280 (lra_dump_file,
2281 " %d Dying matched operand reload: reject++\n",
2282 nop);
2283 reject++;
2285 else
2287 /* Strict_low_part requires to reload the register
2288 not the sub-register. In this case we should
2289 check that a final reload hard reg can hold the
2290 value mode. */
2291 if (curr_static_id->operand[nop].strict_low
2292 && REG_P (op)
2293 && hard_regno[nop] < 0
2294 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2295 && ira_class_hard_regs_num[this_alternative] > 0
2296 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2297 [this_alternative][0],
2298 GET_MODE
2299 (*curr_id->operand_loc[nop])))
2301 if (lra_dump_file != NULL)
2302 fprintf
2303 (lra_dump_file,
2304 " alt=%d: Strict low subreg reload -- refuse\n",
2305 nalt);
2306 goto fail;
2308 losers++;
2310 if (operand_reg[nop] != NULL_RTX
2311 /* Output operands and matched input operands are
2312 not inherited. The following conditions do not
2313 exactly describe the previous statement but they
2314 are pretty close. */
2315 && curr_static_id->operand[nop].type != OP_OUT
2316 && (this_alternative_matches < 0
2317 || curr_static_id->operand[nop].type != OP_IN))
2319 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2320 (operand_reg[nop])]
2321 .last_reload);
2323 /* The value of reload_sum has sense only if we
2324 process insns in their order. It happens only on
2325 the first constraints sub-pass when we do most of
2326 reload work. */
2327 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2328 reload_sum += last_reload - bb_reload_num;
2330 /* If this is a constant that is reloaded into the
2331 desired class by copying it to memory first, count
2332 that as another reload. This is consistent with
2333 other code and is required to avoid choosing another
2334 alternative when the constant is moved into memory.
2335 Note that the test here is precisely the same as in
2336 the code below that calls force_const_mem. */
2337 if (CONST_POOL_OK_P (mode, op)
2338 && ((targetm.preferred_reload_class
2339 (op, this_alternative) == NO_REGS)
2340 || no_input_reloads_p))
2342 const_to_mem = 1;
2343 if (! no_regs_p)
2344 losers++;
2347 /* Alternative loses if it requires a type of reload not
2348 permitted for this insn. We can always reload
2349 objects with a REG_UNUSED note. */
2350 if ((curr_static_id->operand[nop].type != OP_IN
2351 && no_output_reloads_p
2352 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2353 || (curr_static_id->operand[nop].type != OP_OUT
2354 && no_input_reloads_p && ! const_to_mem)
2355 || (this_alternative_matches >= 0
2356 && (no_input_reloads_p
2357 || (no_output_reloads_p
2358 && (curr_static_id->operand
2359 [this_alternative_matches].type != OP_IN)
2360 && ! find_reg_note (curr_insn, REG_UNUSED,
2361 no_subreg_reg_operand
2362 [this_alternative_matches])))))
2364 if (lra_dump_file != NULL)
2365 fprintf
2366 (lra_dump_file,
2367 " alt=%d: No input/otput reload -- refuse\n",
2368 nalt);
2369 goto fail;
2372 /* Alternative loses if it required class pseudo can not
2373 hold value of required mode. Such insns can be
2374 described by insn definitions with mode iterators. */
2375 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2376 && ! hard_reg_set_empty_p (this_alternative_set)
2377 /* It is common practice for constraints to use a
2378 class which does not have actually enough regs to
2379 hold the value (e.g. x86 AREG for mode requiring
2380 more one general reg). Therefore we have 2
2381 conditions to check that the reload pseudo can
2382 not hold the mode value. */
2383 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2384 [this_alternative][0],
2385 GET_MODE (*curr_id->operand_loc[nop]))
2386 /* The above condition is not enough as the first
2387 reg in ira_class_hard_regs can be not aligned for
2388 multi-words mode values. */
2389 && (prohibited_class_reg_set_mode_p
2390 (this_alternative, this_alternative_set,
2391 GET_MODE (*curr_id->operand_loc[nop]))))
2393 if (lra_dump_file != NULL)
2394 fprintf (lra_dump_file,
2395 " alt=%d: reload pseudo for op %d "
2396 " can not hold the mode value -- refuse\n",
2397 nalt, nop);
2398 goto fail;
2401 /* Check strong discouragement of reload of non-constant
2402 into class THIS_ALTERNATIVE. */
2403 if (! CONSTANT_P (op) && ! no_regs_p
2404 && (targetm.preferred_reload_class
2405 (op, this_alternative) == NO_REGS
2406 || (curr_static_id->operand[nop].type == OP_OUT
2407 && (targetm.preferred_output_reload_class
2408 (op, this_alternative) == NO_REGS))))
2410 if (lra_dump_file != NULL)
2411 fprintf (lra_dump_file,
2412 " %d Non-prefered reload: reject+=%d\n",
2413 nop, LRA_MAX_REJECT);
2414 reject += LRA_MAX_REJECT;
2417 if (! (MEM_P (op) && offmemok)
2418 && ! (const_to_mem && constmemok))
2420 /* We prefer to reload pseudos over reloading other
2421 things, since such reloads may be able to be
2422 eliminated later. So bump REJECT in other cases.
2423 Don't do this in the case where we are forcing a
2424 constant into memory and it will then win since
2425 we don't want to have a different alternative
2426 match then. */
2427 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2429 if (lra_dump_file != NULL)
2430 fprintf
2431 (lra_dump_file,
2432 " %d Non-pseudo reload: reject+=2\n",
2433 nop);
2434 reject += 2;
2437 if (! no_regs_p)
2438 reload_nregs
2439 += ira_reg_class_max_nregs[this_alternative][mode];
2441 if (SMALL_REGISTER_CLASS_P (this_alternative))
2443 if (lra_dump_file != NULL)
2444 fprintf
2445 (lra_dump_file,
2446 " %d Small class reload: reject+=%d\n",
2447 nop, LRA_LOSER_COST_FACTOR / 2);
2448 reject += LRA_LOSER_COST_FACTOR / 2;
2452 /* We are trying to spill pseudo into memory. It is
2453 usually more costly than moving to a hard register
2454 although it might takes the same number of
2455 reloads. */
2456 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2458 if (lra_dump_file != NULL)
2459 fprintf
2460 (lra_dump_file,
2461 " %d Spill pseudo into memory: reject+=3\n",
2462 nop);
2463 reject += 3;
2464 if (VECTOR_MODE_P (mode))
2466 /* Spilling vectors into memory is usually more
2467 costly as they contain big values. */
2468 if (lra_dump_file != NULL)
2469 fprintf
2470 (lra_dump_file,
2471 " %d Spill vector pseudo: reject+=2\n",
2472 nop);
2473 reject += 2;
2477 #ifdef SECONDARY_MEMORY_NEEDED
2478 /* If reload requires moving value through secondary
2479 memory, it will need one more insn at least. */
2480 if (this_alternative != NO_REGS
2481 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2482 && ((curr_static_id->operand[nop].type != OP_OUT
2483 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2484 GET_MODE (op)))
2485 || (curr_static_id->operand[nop].type != OP_IN
2486 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2487 GET_MODE (op)))))
2488 losers++;
2489 #endif
2490 /* Input reloads can be inherited more often than output
2491 reloads can be removed, so penalize output
2492 reloads. */
2493 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2495 if (lra_dump_file != NULL)
2496 fprintf
2497 (lra_dump_file,
2498 " %d Non input pseudo reload: reject++\n",
2499 nop);
2500 reject++;
2504 if (early_clobber_p && ! scratch_p)
2506 if (lra_dump_file != NULL)
2507 fprintf (lra_dump_file,
2508 " %d Early clobber: reject++\n", nop);
2509 reject++;
2511 /* ??? We check early clobbers after processing all operands
2512 (see loop below) and there we update the costs more.
2513 Should we update the cost (may be approximately) here
2514 because of early clobber register reloads or it is a rare
2515 or non-important thing to be worth to do it. */
2516 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2517 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2519 if (lra_dump_file != NULL)
2520 fprintf (lra_dump_file,
2521 " alt=%d,overall=%d,losers=%d -- refuse\n",
2522 nalt, overall, losers);
2523 goto fail;
2526 curr_alt[nop] = this_alternative;
2527 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2528 curr_alt_win[nop] = this_alternative_win;
2529 curr_alt_match_win[nop] = this_alternative_match_win;
2530 curr_alt_offmemok[nop] = this_alternative_offmemok;
2531 curr_alt_matches[nop] = this_alternative_matches;
2533 if (this_alternative_matches >= 0
2534 && !did_match && !this_alternative_win)
2535 curr_alt_win[this_alternative_matches] = false;
2537 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2538 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2540 if (curr_insn_set != NULL_RTX && n_operands == 2
2541 /* Prevent processing non-move insns. */
2542 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2543 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2544 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2545 && REG_P (no_subreg_reg_operand[0])
2546 && REG_P (no_subreg_reg_operand[1])
2547 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2548 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2549 || (! curr_alt_win[0] && curr_alt_win[1]
2550 && REG_P (no_subreg_reg_operand[1])
2551 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2552 || (curr_alt_win[0] && ! curr_alt_win[1]
2553 && REG_P (no_subreg_reg_operand[0])
2554 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2555 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2556 no_subreg_reg_operand[1])
2557 || (targetm.preferred_reload_class
2558 (no_subreg_reg_operand[1],
2559 (enum reg_class) curr_alt[1]) != NO_REGS))
2560 /* If it is a result of recent elimination in move
2561 insn we can transform it into an add still by
2562 using this alternative. */
2563 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2565 /* We have a move insn and a new reload insn will be similar
2566 to the current insn. We should avoid such situation as it
2567 results in LRA cycling. */
2568 overall += LRA_MAX_REJECT;
2570 ok_p = true;
2571 curr_alt_dont_inherit_ops_num = 0;
2572 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2574 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2575 HARD_REG_SET temp_set;
2577 i = early_clobbered_nops[nop];
2578 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2579 || hard_regno[i] < 0)
2580 continue;
2581 lra_assert (operand_reg[i] != NULL_RTX);
2582 clobbered_hard_regno = hard_regno[i];
2583 CLEAR_HARD_REG_SET (temp_set);
2584 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2585 first_conflict_j = last_conflict_j = -1;
2586 for (j = 0; j < n_operands; j++)
2587 if (j == i
2588 /* We don't want process insides of match_operator and
2589 match_parallel because otherwise we would process
2590 their operands once again generating a wrong
2591 code. */
2592 || curr_static_id->operand[j].is_operator)
2593 continue;
2594 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2595 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2596 continue;
2597 /* If we don't reload j-th operand, check conflicts. */
2598 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2599 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2601 if (first_conflict_j < 0)
2602 first_conflict_j = j;
2603 last_conflict_j = j;
2605 if (last_conflict_j < 0)
2606 continue;
2607 /* If earlyclobber operand conflicts with another
2608 non-matching operand which is actually the same register
2609 as the earlyclobber operand, it is better to reload the
2610 another operand as an operand matching the earlyclobber
2611 operand can be also the same. */
2612 if (first_conflict_j == last_conflict_j
2613 && operand_reg[last_conflict_j] != NULL_RTX
2614 && ! curr_alt_match_win[last_conflict_j]
2615 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2617 curr_alt_win[last_conflict_j] = false;
2618 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2619 = last_conflict_j;
2620 losers++;
2621 /* Early clobber was already reflected in REJECT. */
2622 lra_assert (reject > 0);
2623 if (lra_dump_file != NULL)
2624 fprintf
2625 (lra_dump_file,
2626 " %d Conflict early clobber reload: reject--\n",
2628 reject--;
2629 overall += LRA_LOSER_COST_FACTOR - 1;
2631 else
2633 /* We need to reload early clobbered register and the
2634 matched registers. */
2635 for (j = 0; j < n_operands; j++)
2636 if (curr_alt_matches[j] == i)
2638 curr_alt_match_win[j] = false;
2639 losers++;
2640 overall += LRA_LOSER_COST_FACTOR;
2642 if (! curr_alt_match_win[i])
2643 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2644 else
2646 /* Remember pseudos used for match reloads are never
2647 inherited. */
2648 lra_assert (curr_alt_matches[i] >= 0);
2649 curr_alt_win[curr_alt_matches[i]] = false;
2651 curr_alt_win[i] = curr_alt_match_win[i] = false;
2652 losers++;
2653 /* Early clobber was already reflected in REJECT. */
2654 lra_assert (reject > 0);
2655 if (lra_dump_file != NULL)
2656 fprintf
2657 (lra_dump_file,
2658 " %d Matched conflict early clobber reloads:"
2659 "reject--\n",
2661 reject--;
2662 overall += LRA_LOSER_COST_FACTOR - 1;
2665 if (lra_dump_file != NULL)
2666 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2667 nalt, overall, losers, reload_nregs);
2669 /* If this alternative can be made to work by reloading, and it
2670 needs less reloading than the others checked so far, record
2671 it as the chosen goal for reloading. */
2672 if ((best_losers != 0 && losers == 0)
2673 || (((best_losers == 0 && losers == 0)
2674 || (best_losers != 0 && losers != 0))
2675 && (best_overall > overall
2676 || (best_overall == overall
2677 /* If the cost of the reloads is the same,
2678 prefer alternative which requires minimal
2679 number of reload regs. */
2680 && (reload_nregs < best_reload_nregs
2681 || (reload_nregs == best_reload_nregs
2682 && (best_reload_sum < reload_sum
2683 || (best_reload_sum == reload_sum
2684 && nalt < goal_alt_number))))))))
2686 for (nop = 0; nop < n_operands; nop++)
2688 goal_alt_win[nop] = curr_alt_win[nop];
2689 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2690 goal_alt_matches[nop] = curr_alt_matches[nop];
2691 goal_alt[nop] = curr_alt[nop];
2692 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2694 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2695 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2696 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2697 goal_alt_swapped = curr_swapped;
2698 best_overall = overall;
2699 best_losers = losers;
2700 best_reload_nregs = reload_nregs;
2701 best_reload_sum = reload_sum;
2702 goal_alt_number = nalt;
2704 if (losers == 0)
2705 /* Everything is satisfied. Do not process alternatives
2706 anymore. */
2707 break;
2708 fail:
2711 return ok_p;
2714 /* Make reload base reg from address AD. */
2715 static rtx
2716 base_to_reg (struct address_info *ad)
2718 enum reg_class cl;
2719 int code = -1;
2720 rtx new_inner = NULL_RTX;
2721 rtx new_reg = NULL_RTX;
2722 rtx_insn *insn;
2723 rtx_insn *last_insn = get_last_insn();
2725 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2726 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2727 get_index_code (ad));
2728 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2729 cl, "base");
2730 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2731 ad->disp_term == NULL
2732 ? gen_int_mode (0, ad->mode)
2733 : *ad->disp_term);
2734 if (!valid_address_p (ad->mode, new_inner, ad->as))
2735 return NULL_RTX;
2736 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base_term));
2737 code = recog_memoized (insn);
2738 if (code < 0)
2740 delete_insns_since (last_insn);
2741 return NULL_RTX;
2744 return new_inner;
2747 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2748 static rtx
2749 base_plus_disp_to_reg (struct address_info *ad)
2751 enum reg_class cl;
2752 rtx new_reg;
2754 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2755 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2756 get_index_code (ad));
2757 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2758 cl, "base + disp");
2759 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2760 return new_reg;
2763 /* Make reload of index part of address AD. Return the new
2764 pseudo. */
2765 static rtx
2766 index_part_to_reg (struct address_info *ad)
2768 rtx new_reg;
2770 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2771 INDEX_REG_CLASS, "index term");
2772 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2773 GEN_INT (get_index_scale (ad)), new_reg, 1);
2774 return new_reg;
2777 /* Return true if we can add a displacement to address AD, even if that
2778 makes the address invalid. The fix-up code requires any new address
2779 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2780 static bool
2781 can_add_disp_p (struct address_info *ad)
2783 return (!ad->autoinc_p
2784 && ad->segment == NULL
2785 && ad->base == ad->base_term
2786 && ad->disp == ad->disp_term);
2789 /* Make equiv substitution in address AD. Return true if a substitution
2790 was made. */
2791 static bool
2792 equiv_address_substitution (struct address_info *ad)
2794 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2795 HOST_WIDE_INT disp, scale;
2796 bool change_p;
2798 base_term = strip_subreg (ad->base_term);
2799 if (base_term == NULL)
2800 base_reg = new_base_reg = NULL_RTX;
2801 else
2803 base_reg = *base_term;
2804 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2806 index_term = strip_subreg (ad->index_term);
2807 if (index_term == NULL)
2808 index_reg = new_index_reg = NULL_RTX;
2809 else
2811 index_reg = *index_term;
2812 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2814 if (base_reg == new_base_reg && index_reg == new_index_reg)
2815 return false;
2816 disp = 0;
2817 change_p = false;
2818 if (lra_dump_file != NULL)
2820 fprintf (lra_dump_file, "Changing address in insn %d ",
2821 INSN_UID (curr_insn));
2822 dump_value_slim (lra_dump_file, *ad->outer, 1);
2824 if (base_reg != new_base_reg)
2826 if (REG_P (new_base_reg))
2828 *base_term = new_base_reg;
2829 change_p = true;
2831 else if (GET_CODE (new_base_reg) == PLUS
2832 && REG_P (XEXP (new_base_reg, 0))
2833 && CONST_INT_P (XEXP (new_base_reg, 1))
2834 && can_add_disp_p (ad))
2836 disp += INTVAL (XEXP (new_base_reg, 1));
2837 *base_term = XEXP (new_base_reg, 0);
2838 change_p = true;
2840 if (ad->base_term2 != NULL)
2841 *ad->base_term2 = *ad->base_term;
2843 if (index_reg != new_index_reg)
2845 if (REG_P (new_index_reg))
2847 *index_term = new_index_reg;
2848 change_p = true;
2850 else if (GET_CODE (new_index_reg) == PLUS
2851 && REG_P (XEXP (new_index_reg, 0))
2852 && CONST_INT_P (XEXP (new_index_reg, 1))
2853 && can_add_disp_p (ad)
2854 && (scale = get_index_scale (ad)))
2856 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2857 *index_term = XEXP (new_index_reg, 0);
2858 change_p = true;
2861 if (disp != 0)
2863 if (ad->disp != NULL)
2864 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2865 else
2867 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2868 update_address (ad);
2870 change_p = true;
2872 if (lra_dump_file != NULL)
2874 if (! change_p)
2875 fprintf (lra_dump_file, " -- no change\n");
2876 else
2878 fprintf (lra_dump_file, " on equiv ");
2879 dump_value_slim (lra_dump_file, *ad->outer, 1);
2880 fprintf (lra_dump_file, "\n");
2883 return change_p;
2886 /* Major function to make reloads for an address in operand NOP or
2887 check its correctness (If CHECK_ONLY_P is true). The supported
2888 cases are:
2890 1) an address that existed before LRA started, at which point it
2891 must have been valid. These addresses are subject to elimination
2892 and may have become invalid due to the elimination offset being out
2893 of range.
2895 2) an address created by forcing a constant to memory
2896 (force_const_to_mem). The initial form of these addresses might
2897 not be valid, and it is this function's job to make them valid.
2899 3) a frame address formed from a register and a (possibly zero)
2900 constant offset. As above, these addresses might not be valid and
2901 this function must make them so.
2903 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2904 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2905 address. Return true for any RTL change.
2907 The function is a helper function which does not produce all
2908 transformations (when CHECK_ONLY_P is false) which can be
2909 necessary. It does just basic steps. To do all necessary
2910 transformations use function process_address. */
2911 static bool
2912 process_address_1 (int nop, bool check_only_p,
2913 rtx_insn **before, rtx_insn **after)
2915 struct address_info ad;
2916 rtx new_reg;
2917 rtx op = *curr_id->operand_loc[nop];
2918 const char *constraint = curr_static_id->operand[nop].constraint;
2919 enum constraint_num cn = lookup_constraint (constraint);
2920 bool change_p = false;
2922 if (MEM_P (op)
2923 && GET_MODE (op) == BLKmode
2924 && GET_CODE (XEXP (op, 0)) == SCRATCH)
2925 return false;
2927 if (insn_extra_address_constraint (cn))
2928 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2929 else if (MEM_P (op))
2930 decompose_mem_address (&ad, op);
2931 else if (GET_CODE (op) == SUBREG
2932 && MEM_P (SUBREG_REG (op)))
2933 decompose_mem_address (&ad, SUBREG_REG (op));
2934 else
2935 return false;
2936 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
2937 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
2938 when INDEX_REG_CLASS is a single register class. */
2939 if (ad.base_term != NULL
2940 && ad.index_term != NULL
2941 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
2942 && REG_P (*ad.base_term)
2943 && REG_P (*ad.index_term)
2944 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
2945 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
2947 std::swap (ad.base, ad.index);
2948 std::swap (ad.base_term, ad.index_term);
2950 if (! check_only_p)
2951 change_p = equiv_address_substitution (&ad);
2952 if (ad.base_term != NULL
2953 && (process_addr_reg
2954 (ad.base_term, check_only_p, before,
2955 (ad.autoinc_p
2956 && !(REG_P (*ad.base_term)
2957 && find_regno_note (curr_insn, REG_DEAD,
2958 REGNO (*ad.base_term)) != NULL_RTX)
2959 ? after : NULL),
2960 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2961 get_index_code (&ad)))))
2963 change_p = true;
2964 if (ad.base_term2 != NULL)
2965 *ad.base_term2 = *ad.base_term;
2967 if (ad.index_term != NULL
2968 && process_addr_reg (ad.index_term, check_only_p,
2969 before, NULL, INDEX_REG_CLASS))
2970 change_p = true;
2972 /* Target hooks sometimes don't treat extra-constraint addresses as
2973 legitimate address_operands, so handle them specially. */
2974 if (insn_extra_address_constraint (cn)
2975 && satisfies_address_constraint_p (&ad, cn))
2976 return change_p;
2978 if (check_only_p)
2979 return change_p;
2981 /* There are three cases where the shape of *AD.INNER may now be invalid:
2983 1) the original address was valid, but either elimination or
2984 equiv_address_substitution was applied and that made
2985 the address invalid.
2987 2) the address is an invalid symbolic address created by
2988 force_const_to_mem.
2990 3) the address is a frame address with an invalid offset.
2992 4) the address is a frame address with an invalid base.
2994 All these cases involve a non-autoinc address, so there is no
2995 point revalidating other types. */
2996 if (ad.autoinc_p || valid_address_p (&ad))
2997 return change_p;
2999 /* Any index existed before LRA started, so we can assume that the
3000 presence and shape of the index is valid. */
3001 push_to_sequence (*before);
3002 lra_assert (ad.disp == ad.disp_term);
3003 if (ad.base == NULL)
3005 if (ad.index == NULL)
3007 rtx_insn *insn;
3008 rtx_insn *last = get_last_insn ();
3009 int code = -1;
3010 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3011 SCRATCH, SCRATCH);
3012 rtx addr = *ad.inner;
3014 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3015 if (HAVE_lo_sum)
3017 /* addr => lo_sum (new_base, addr), case (2) above. */
3018 insn = emit_insn (gen_rtx_SET
3019 (new_reg,
3020 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
3021 code = recog_memoized (insn);
3022 if (code >= 0)
3024 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
3025 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
3027 /* Try to put lo_sum into register. */
3028 insn = emit_insn (gen_rtx_SET
3029 (new_reg,
3030 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
3031 code = recog_memoized (insn);
3032 if (code >= 0)
3034 *ad.inner = new_reg;
3035 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
3037 *ad.inner = addr;
3038 code = -1;
3044 if (code < 0)
3045 delete_insns_since (last);
3048 if (code < 0)
3050 /* addr => new_base, case (2) above. */
3051 lra_emit_move (new_reg, addr);
3053 for (insn = last == NULL_RTX ? get_insns () : NEXT_INSN (last);
3054 insn != NULL_RTX;
3055 insn = NEXT_INSN (insn))
3056 if (recog_memoized (insn) < 0)
3057 break;
3058 if (insn != NULL_RTX)
3060 /* Do nothing if we cannot generate right insns.
3061 This is analogous to reload pass behavior. */
3062 delete_insns_since (last);
3063 end_sequence ();
3064 return false;
3066 *ad.inner = new_reg;
3069 else
3071 /* index * scale + disp => new base + index * scale,
3072 case (1) above. */
3073 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3074 GET_CODE (*ad.index));
3076 lra_assert (INDEX_REG_CLASS != NO_REGS);
3077 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3078 lra_emit_move (new_reg, *ad.disp);
3079 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3080 new_reg, *ad.index);
3083 else if (ad.index == NULL)
3085 int regno;
3086 enum reg_class cl;
3087 rtx set;
3088 rtx_insn *insns, *last_insn;
3089 /* Try to reload base into register only if the base is invalid
3090 for the address but with valid offset, case (4) above. */
3091 start_sequence ();
3092 new_reg = base_to_reg (&ad);
3094 /* base + disp => new base, cases (1) and (3) above. */
3095 /* Another option would be to reload the displacement into an
3096 index register. However, postreload has code to optimize
3097 address reloads that have the same base and different
3098 displacements, so reloading into an index register would
3099 not necessarily be a win. */
3100 if (new_reg == NULL_RTX)
3101 new_reg = base_plus_disp_to_reg (&ad);
3102 insns = get_insns ();
3103 last_insn = get_last_insn ();
3104 /* If we generated at least two insns, try last insn source as
3105 an address. If we succeed, we generate one less insn. */
3106 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3107 && GET_CODE (SET_SRC (set)) == PLUS
3108 && REG_P (XEXP (SET_SRC (set), 0))
3109 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3111 *ad.inner = SET_SRC (set);
3112 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3114 *ad.base_term = XEXP (SET_SRC (set), 0);
3115 *ad.disp_term = XEXP (SET_SRC (set), 1);
3116 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3117 get_index_code (&ad));
3118 regno = REGNO (*ad.base_term);
3119 if (regno >= FIRST_PSEUDO_REGISTER
3120 && cl != lra_get_allocno_class (regno))
3121 lra_change_class (regno, cl, " Change to", true);
3122 new_reg = SET_SRC (set);
3123 delete_insns_since (PREV_INSN (last_insn));
3126 /* Try if target can split displacement into legitimite new disp
3127 and offset. If it's the case, we replace the last insn with
3128 insns for base + offset => new_reg and set new_reg + new disp
3129 to *ad.inner. */
3130 last_insn = get_last_insn ();
3131 if ((set = single_set (last_insn)) != NULL_RTX
3132 && GET_CODE (SET_SRC (set)) == PLUS
3133 && REG_P (XEXP (SET_SRC (set), 0))
3134 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3135 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3137 rtx addend, disp = XEXP (SET_SRC (set), 1);
3138 if (targetm.legitimize_address_displacement (&disp, &addend,
3139 ad.mode))
3141 rtx_insn *new_insns;
3142 start_sequence ();
3143 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3144 new_insns = get_insns ();
3145 end_sequence ();
3146 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3147 delete_insns_since (PREV_INSN (last_insn));
3148 add_insn (new_insns);
3149 insns = get_insns ();
3152 end_sequence ();
3153 emit_insn (insns);
3154 *ad.inner = new_reg;
3156 else if (ad.disp_term != NULL)
3158 /* base + scale * index + disp => new base + scale * index,
3159 case (1) above. */
3160 new_reg = base_plus_disp_to_reg (&ad);
3161 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3162 new_reg, *ad.index);
3164 else if (get_index_scale (&ad) == 1)
3166 /* The last transformation to one reg will be made in
3167 curr_insn_transform function. */
3168 end_sequence ();
3169 return false;
3171 else
3173 /* base + scale * index => base + new_reg,
3174 case (1) above.
3175 Index part of address may become invalid. For example, we
3176 changed pseudo on the equivalent memory and a subreg of the
3177 pseudo onto the memory of different mode for which the scale is
3178 prohibitted. */
3179 new_reg = index_part_to_reg (&ad);
3180 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3181 *ad.base_term, new_reg);
3183 *before = get_insns ();
3184 end_sequence ();
3185 return true;
3188 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3189 Use process_address_1 as a helper function. Return true for any
3190 RTL changes.
3192 If CHECK_ONLY_P is true, just check address correctness. Return
3193 false if the address correct. */
3194 static bool
3195 process_address (int nop, bool check_only_p,
3196 rtx_insn **before, rtx_insn **after)
3198 bool res = false;
3200 while (process_address_1 (nop, check_only_p, before, after))
3202 if (check_only_p)
3203 return true;
3204 res = true;
3206 return res;
3209 /* Emit insns to reload VALUE into a new register. VALUE is an
3210 auto-increment or auto-decrement RTX whose operand is a register or
3211 memory location; so reloading involves incrementing that location.
3212 IN is either identical to VALUE, or some cheaper place to reload
3213 value being incremented/decremented from.
3215 INC_AMOUNT is the number to increment or decrement by (always
3216 positive and ignored for POST_MODIFY/PRE_MODIFY).
3218 Return pseudo containing the result. */
3219 static rtx
3220 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3222 /* REG or MEM to be copied and incremented. */
3223 rtx incloc = XEXP (value, 0);
3224 /* Nonzero if increment after copying. */
3225 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3226 || GET_CODE (value) == POST_MODIFY);
3227 rtx_insn *last;
3228 rtx inc;
3229 rtx_insn *add_insn;
3230 int code;
3231 rtx real_in = in == value ? incloc : in;
3232 rtx result;
3233 bool plus_p = true;
3235 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3237 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3238 || GET_CODE (XEXP (value, 1)) == MINUS);
3239 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3240 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3241 inc = XEXP (XEXP (value, 1), 1);
3243 else
3245 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3246 inc_amount = -inc_amount;
3248 inc = GEN_INT (inc_amount);
3251 if (! post && REG_P (incloc))
3252 result = incloc;
3253 else
3254 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3255 "INC/DEC result");
3257 if (real_in != result)
3259 /* First copy the location to the result register. */
3260 lra_assert (REG_P (result));
3261 emit_insn (gen_move_insn (result, real_in));
3264 /* We suppose that there are insns to add/sub with the constant
3265 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3266 old reload worked with this assumption. If the assumption
3267 becomes wrong, we should use approach in function
3268 base_plus_disp_to_reg. */
3269 if (in == value)
3271 /* See if we can directly increment INCLOC. */
3272 last = get_last_insn ();
3273 add_insn = emit_insn (plus_p
3274 ? gen_add2_insn (incloc, inc)
3275 : gen_sub2_insn (incloc, inc));
3277 code = recog_memoized (add_insn);
3278 if (code >= 0)
3280 if (! post && result != incloc)
3281 emit_insn (gen_move_insn (result, incloc));
3282 return result;
3284 delete_insns_since (last);
3287 /* If couldn't do the increment directly, must increment in RESULT.
3288 The way we do this depends on whether this is pre- or
3289 post-increment. For pre-increment, copy INCLOC to the reload
3290 register, increment it there, then save back. */
3291 if (! post)
3293 if (real_in != result)
3294 emit_insn (gen_move_insn (result, real_in));
3295 if (plus_p)
3296 emit_insn (gen_add2_insn (result, inc));
3297 else
3298 emit_insn (gen_sub2_insn (result, inc));
3299 if (result != incloc)
3300 emit_insn (gen_move_insn (incloc, result));
3302 else
3304 /* Post-increment.
3306 Because this might be a jump insn or a compare, and because
3307 RESULT may not be available after the insn in an input
3308 reload, we must do the incrementing before the insn being
3309 reloaded for.
3311 We have already copied IN to RESULT. Increment the copy in
3312 RESULT, save that back, then decrement RESULT so it has
3313 the original value. */
3314 if (plus_p)
3315 emit_insn (gen_add2_insn (result, inc));
3316 else
3317 emit_insn (gen_sub2_insn (result, inc));
3318 emit_insn (gen_move_insn (incloc, result));
3319 /* Restore non-modified value for the result. We prefer this
3320 way because it does not require an additional hard
3321 register. */
3322 if (plus_p)
3324 if (CONST_INT_P (inc))
3325 emit_insn (gen_add2_insn (result,
3326 gen_int_mode (-INTVAL (inc),
3327 GET_MODE (result))));
3328 else
3329 emit_insn (gen_sub2_insn (result, inc));
3331 else
3332 emit_insn (gen_add2_insn (result, inc));
3334 return result;
3337 /* Return true if the current move insn does not need processing as we
3338 already know that it satisfies its constraints. */
3339 static bool
3340 simple_move_p (void)
3342 rtx dest, src;
3343 enum reg_class dclass, sclass;
3345 lra_assert (curr_insn_set != NULL_RTX);
3346 dest = SET_DEST (curr_insn_set);
3347 src = SET_SRC (curr_insn_set);
3348 return ((dclass = get_op_class (dest)) != NO_REGS
3349 && (sclass = get_op_class (src)) != NO_REGS
3350 /* The backend guarantees that register moves of cost 2
3351 never need reloads. */
3352 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3355 /* Swap operands NOP and NOP + 1. */
3356 static inline void
3357 swap_operands (int nop)
3359 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3360 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3361 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3362 /* Swap the duplicates too. */
3363 lra_update_dup (curr_id, nop);
3364 lra_update_dup (curr_id, nop + 1);
3367 /* Main entry point of the constraint code: search the body of the
3368 current insn to choose the best alternative. It is mimicking insn
3369 alternative cost calculation model of former reload pass. That is
3370 because machine descriptions were written to use this model. This
3371 model can be changed in future. Make commutative operand exchange
3372 if it is chosen.
3374 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3375 constraints. Return true if any change happened during function
3376 call.
3378 If CHECK_ONLY_P is true then don't do any transformation. Just
3379 check that the insn satisfies all constraints. If the insn does
3380 not satisfy any constraint, return true. */
3381 static bool
3382 curr_insn_transform (bool check_only_p)
3384 int i, j, k;
3385 int n_operands;
3386 int n_alternatives;
3387 int commutative;
3388 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3389 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3390 rtx_insn *before, *after;
3391 bool alt_p = false;
3392 /* Flag that the insn has been changed through a transformation. */
3393 bool change_p;
3394 bool sec_mem_p;
3395 #ifdef SECONDARY_MEMORY_NEEDED
3396 bool use_sec_mem_p;
3397 #endif
3398 int max_regno_before;
3399 int reused_alternative_num;
3401 curr_insn_set = single_set (curr_insn);
3402 if (curr_insn_set != NULL_RTX && simple_move_p ())
3403 return false;
3405 no_input_reloads_p = no_output_reloads_p = false;
3406 goal_alt_number = -1;
3407 change_p = sec_mem_p = false;
3408 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3409 reloads; neither are insns that SET cc0. Insns that use CC0 are
3410 not allowed to have any input reloads. */
3411 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3412 no_output_reloads_p = true;
3414 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3415 no_input_reloads_p = true;
3416 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3417 no_output_reloads_p = true;
3419 n_operands = curr_static_id->n_operands;
3420 n_alternatives = curr_static_id->n_alternatives;
3422 /* Just return "no reloads" if insn has no operands with
3423 constraints. */
3424 if (n_operands == 0 || n_alternatives == 0)
3425 return false;
3427 max_regno_before = max_reg_num ();
3429 for (i = 0; i < n_operands; i++)
3431 goal_alt_matched[i][0] = -1;
3432 goal_alt_matches[i] = -1;
3435 commutative = curr_static_id->commutative;
3437 /* Now see what we need for pseudos that didn't get hard regs or got
3438 the wrong kind of hard reg. For this, we must consider all the
3439 operands together against the register constraints. */
3441 best_losers = best_overall = INT_MAX;
3442 best_reload_sum = 0;
3444 curr_swapped = false;
3445 goal_alt_swapped = false;
3447 if (! check_only_p)
3448 /* Make equivalence substitution and memory subreg elimination
3449 before address processing because an address legitimacy can
3450 depend on memory mode. */
3451 for (i = 0; i < n_operands; i++)
3453 rtx op, subst, old;
3454 bool op_change_p = false;
3456 if (curr_static_id->operand[i].is_operator)
3457 continue;
3459 old = op = *curr_id->operand_loc[i];
3460 if (GET_CODE (old) == SUBREG)
3461 old = SUBREG_REG (old);
3462 subst = get_equiv_with_elimination (old, curr_insn);
3463 original_subreg_reg_mode[i] = VOIDmode;
3464 if (subst != old)
3466 subst = copy_rtx (subst);
3467 lra_assert (REG_P (old));
3468 if (GET_CODE (op) != SUBREG)
3469 *curr_id->operand_loc[i] = subst;
3470 else
3472 SUBREG_REG (op) = subst;
3473 if (GET_MODE (subst) == VOIDmode)
3474 original_subreg_reg_mode[i] = GET_MODE (old);
3476 if (lra_dump_file != NULL)
3478 fprintf (lra_dump_file,
3479 "Changing pseudo %d in operand %i of insn %u on equiv ",
3480 REGNO (old), i, INSN_UID (curr_insn));
3481 dump_value_slim (lra_dump_file, subst, 1);
3482 fprintf (lra_dump_file, "\n");
3484 op_change_p = change_p = true;
3486 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3488 change_p = true;
3489 lra_update_dup (curr_id, i);
3493 /* Reload address registers and displacements. We do it before
3494 finding an alternative because of memory constraints. */
3495 before = after = NULL;
3496 for (i = 0; i < n_operands; i++)
3497 if (! curr_static_id->operand[i].is_operator
3498 && process_address (i, check_only_p, &before, &after))
3500 if (check_only_p)
3501 return true;
3502 change_p = true;
3503 lra_update_dup (curr_id, i);
3506 if (change_p)
3507 /* If we've changed the instruction then any alternative that
3508 we chose previously may no longer be valid. */
3509 lra_set_used_insn_alternative (curr_insn, -1);
3511 if (! check_only_p && curr_insn_set != NULL_RTX
3512 && check_and_process_move (&change_p, &sec_mem_p))
3513 return change_p;
3515 try_swapped:
3517 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3518 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3519 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3520 reused_alternative_num, INSN_UID (curr_insn));
3522 if (process_alt_operands (reused_alternative_num))
3523 alt_p = true;
3525 if (check_only_p)
3526 return ! alt_p || best_losers != 0;
3528 /* If insn is commutative (it's safe to exchange a certain pair of
3529 operands) then we need to try each alternative twice, the second
3530 time matching those two operands as if we had exchanged them. To
3531 do this, really exchange them in operands.
3533 If we have just tried the alternatives the second time, return
3534 operands to normal and drop through. */
3536 if (reused_alternative_num < 0 && commutative >= 0)
3538 curr_swapped = !curr_swapped;
3539 if (curr_swapped)
3541 swap_operands (commutative);
3542 goto try_swapped;
3544 else
3545 swap_operands (commutative);
3548 if (! alt_p && ! sec_mem_p)
3550 /* No alternative works with reloads?? */
3551 if (INSN_CODE (curr_insn) >= 0)
3552 fatal_insn ("unable to generate reloads for:", curr_insn);
3553 error_for_asm (curr_insn,
3554 "inconsistent operand constraints in an %<asm%>");
3555 /* Avoid further trouble with this insn. */
3556 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3557 lra_invalidate_insn_data (curr_insn);
3558 return true;
3561 /* If the best alternative is with operands 1 and 2 swapped, swap
3562 them. Update the operand numbers of any reloads already
3563 pushed. */
3565 if (goal_alt_swapped)
3567 if (lra_dump_file != NULL)
3568 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3569 INSN_UID (curr_insn));
3571 /* Swap the duplicates too. */
3572 swap_operands (commutative);
3573 change_p = true;
3576 #ifdef SECONDARY_MEMORY_NEEDED
3577 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3578 too conservatively. So we use the secondary memory only if there
3579 is no any alternative without reloads. */
3580 use_sec_mem_p = false;
3581 if (! alt_p)
3582 use_sec_mem_p = true;
3583 else if (sec_mem_p)
3585 for (i = 0; i < n_operands; i++)
3586 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3587 break;
3588 use_sec_mem_p = i < n_operands;
3591 if (use_sec_mem_p)
3593 int in = -1, out = -1;
3594 rtx new_reg, src, dest, rld;
3595 machine_mode sec_mode, rld_mode;
3597 lra_assert (curr_insn_set != NULL_RTX && sec_mem_p);
3598 dest = SET_DEST (curr_insn_set);
3599 src = SET_SRC (curr_insn_set);
3600 for (i = 0; i < n_operands; i++)
3601 if (*curr_id->operand_loc[i] == dest)
3602 out = i;
3603 else if (*curr_id->operand_loc[i] == src)
3604 in = i;
3605 for (i = 0; i < curr_static_id->n_dups; i++)
3606 if (out < 0 && *curr_id->dup_loc[i] == dest)
3607 out = curr_static_id->dup_num[i];
3608 else if (in < 0 && *curr_id->dup_loc[i] == src)
3609 in = curr_static_id->dup_num[i];
3610 lra_assert (out >= 0 && in >= 0
3611 && curr_static_id->operand[out].type == OP_OUT
3612 && curr_static_id->operand[in].type == OP_IN);
3613 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3614 ? dest : src);
3615 rld_mode = GET_MODE (rld);
3616 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3617 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3618 #else
3619 sec_mode = rld_mode;
3620 #endif
3621 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3622 NO_REGS, "secondary");
3623 /* If the mode is changed, it should be wider. */
3624 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3625 if (sec_mode != rld_mode)
3627 /* If the target says specifically to use another mode for
3628 secondary memory moves we can not reuse the original
3629 insn. */
3630 after = emit_spill_move (false, new_reg, dest);
3631 lra_process_new_insns (curr_insn, NULL, after,
3632 "Inserting the sec. move");
3633 /* We may have non null BEFORE here (e.g. after address
3634 processing. */
3635 push_to_sequence (before);
3636 before = emit_spill_move (true, new_reg, src);
3637 emit_insn (before);
3638 before = get_insns ();
3639 end_sequence ();
3640 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3641 lra_set_insn_deleted (curr_insn);
3643 else if (dest == rld)
3645 *curr_id->operand_loc[out] = new_reg;
3646 lra_update_dup (curr_id, out);
3647 after = emit_spill_move (false, new_reg, dest);
3648 lra_process_new_insns (curr_insn, NULL, after,
3649 "Inserting the sec. move");
3651 else
3653 *curr_id->operand_loc[in] = new_reg;
3654 lra_update_dup (curr_id, in);
3655 /* See comments above. */
3656 push_to_sequence (before);
3657 before = emit_spill_move (true, new_reg, src);
3658 emit_insn (before);
3659 before = get_insns ();
3660 end_sequence ();
3661 lra_process_new_insns (curr_insn, before, NULL,
3662 "Inserting the sec. move");
3664 lra_update_insn_regno_info (curr_insn);
3665 return true;
3667 #endif
3669 lra_assert (goal_alt_number >= 0);
3670 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3672 if (lra_dump_file != NULL)
3674 const char *p;
3676 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3677 goal_alt_number, INSN_UID (curr_insn));
3678 for (i = 0; i < n_operands; i++)
3680 p = (curr_static_id->operand_alternative
3681 [goal_alt_number * n_operands + i].constraint);
3682 if (*p == '\0')
3683 continue;
3684 fprintf (lra_dump_file, " (%d) ", i);
3685 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3686 fputc (*p, lra_dump_file);
3688 if (INSN_CODE (curr_insn) >= 0
3689 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3690 fprintf (lra_dump_file, " {%s}", p);
3691 if (curr_id->sp_offset != 0)
3692 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3693 curr_id->sp_offset);
3694 fprintf (lra_dump_file, "\n");
3697 /* Right now, for any pair of operands I and J that are required to
3698 match, with J < I, goal_alt_matches[I] is J. Add I to
3699 goal_alt_matched[J]. */
3701 for (i = 0; i < n_operands; i++)
3702 if ((j = goal_alt_matches[i]) >= 0)
3704 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3706 /* We allow matching one output operand and several input
3707 operands. */
3708 lra_assert (k == 0
3709 || (curr_static_id->operand[j].type == OP_OUT
3710 && curr_static_id->operand[i].type == OP_IN
3711 && (curr_static_id->operand
3712 [goal_alt_matched[j][0]].type == OP_IN)));
3713 goal_alt_matched[j][k] = i;
3714 goal_alt_matched[j][k + 1] = -1;
3717 for (i = 0; i < n_operands; i++)
3718 goal_alt_win[i] |= goal_alt_match_win[i];
3720 /* Any constants that aren't allowed and can't be reloaded into
3721 registers are here changed into memory references. */
3722 for (i = 0; i < n_operands; i++)
3723 if (goal_alt_win[i])
3725 int regno;
3726 enum reg_class new_class;
3727 rtx reg = *curr_id->operand_loc[i];
3729 if (GET_CODE (reg) == SUBREG)
3730 reg = SUBREG_REG (reg);
3732 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3734 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3736 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3738 lra_assert (ok_p);
3739 lra_change_class (regno, new_class, " Change to", true);
3743 else
3745 const char *constraint;
3746 char c;
3747 rtx op = *curr_id->operand_loc[i];
3748 rtx subreg = NULL_RTX;
3749 machine_mode mode = curr_operand_mode[i];
3751 if (GET_CODE (op) == SUBREG)
3753 subreg = op;
3754 op = SUBREG_REG (op);
3755 mode = GET_MODE (op);
3758 if (CONST_POOL_OK_P (mode, op)
3759 && ((targetm.preferred_reload_class
3760 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3761 || no_input_reloads_p))
3763 rtx tem = force_const_mem (mode, op);
3765 change_p = true;
3766 if (subreg != NULL_RTX)
3767 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3769 *curr_id->operand_loc[i] = tem;
3770 lra_update_dup (curr_id, i);
3771 process_address (i, false, &before, &after);
3773 /* If the alternative accepts constant pool refs directly
3774 there will be no reload needed at all. */
3775 if (subreg != NULL_RTX)
3776 continue;
3777 /* Skip alternatives before the one requested. */
3778 constraint = (curr_static_id->operand_alternative
3779 [goal_alt_number * n_operands + i].constraint);
3780 for (;
3781 (c = *constraint) && c != ',' && c != '#';
3782 constraint += CONSTRAINT_LEN (c, constraint))
3784 enum constraint_num cn = lookup_constraint (constraint);
3785 if ((insn_extra_memory_constraint (cn)
3786 || insn_extra_special_memory_constraint (cn))
3787 && satisfies_memory_constraint_p (tem, cn))
3788 break;
3790 if (c == '\0' || c == ',' || c == '#')
3791 continue;
3793 goal_alt_win[i] = true;
3797 for (i = 0; i < n_operands; i++)
3799 int regno;
3800 bool optional_p = false;
3801 rtx old, new_reg;
3802 rtx op = *curr_id->operand_loc[i];
3804 if (goal_alt_win[i])
3806 if (goal_alt[i] == NO_REGS
3807 && REG_P (op)
3808 /* When we assign NO_REGS it means that we will not
3809 assign a hard register to the scratch pseudo by
3810 assigment pass and the scratch pseudo will be
3811 spilled. Spilled scratch pseudos are transformed
3812 back to scratches at the LRA end. */
3813 && lra_former_scratch_operand_p (curr_insn, i)
3814 && lra_former_scratch_p (REGNO (op)))
3816 int regno = REGNO (op);
3817 lra_change_class (regno, NO_REGS, " Change to", true);
3818 if (lra_get_regno_hard_regno (regno) >= 0)
3819 /* We don't have to mark all insn affected by the
3820 spilled pseudo as there is only one such insn, the
3821 current one. */
3822 reg_renumber[regno] = -1;
3823 lra_assert (bitmap_single_bit_set_p
3824 (&lra_reg_info[REGNO (op)].insn_bitmap));
3826 /* We can do an optional reload. If the pseudo got a hard
3827 reg, we might improve the code through inheritance. If
3828 it does not get a hard register we coalesce memory/memory
3829 moves later. Ignore move insns to avoid cycling. */
3830 if (! lra_simple_p
3831 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3832 && goal_alt[i] != NO_REGS && REG_P (op)
3833 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3834 && regno < new_regno_start
3835 && ! lra_former_scratch_p (regno)
3836 && reg_renumber[regno] < 0
3837 /* Check that the optional reload pseudo will be able to
3838 hold given mode value. */
3839 && ! (prohibited_class_reg_set_mode_p
3840 (goal_alt[i], reg_class_contents[goal_alt[i]],
3841 PSEUDO_REGNO_MODE (regno)))
3842 && (curr_insn_set == NULL_RTX
3843 || !((REG_P (SET_SRC (curr_insn_set))
3844 || MEM_P (SET_SRC (curr_insn_set))
3845 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3846 && (REG_P (SET_DEST (curr_insn_set))
3847 || MEM_P (SET_DEST (curr_insn_set))
3848 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3849 optional_p = true;
3850 else
3851 continue;
3854 /* Operands that match previous ones have already been handled. */
3855 if (goal_alt_matches[i] >= 0)
3856 continue;
3858 /* We should not have an operand with a non-offsettable address
3859 appearing where an offsettable address will do. It also may
3860 be a case when the address should be special in other words
3861 not a general one (e.g. it needs no index reg). */
3862 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3864 enum reg_class rclass;
3865 rtx *loc = &XEXP (op, 0);
3866 enum rtx_code code = GET_CODE (*loc);
3868 push_to_sequence (before);
3869 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3870 MEM, SCRATCH);
3871 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3872 new_reg = emit_inc (rclass, *loc, *loc,
3873 /* This value does not matter for MODIFY. */
3874 GET_MODE_SIZE (GET_MODE (op)));
3875 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3876 "offsetable address", &new_reg))
3877 lra_emit_move (new_reg, *loc);
3878 before = get_insns ();
3879 end_sequence ();
3880 *loc = new_reg;
3881 lra_update_dup (curr_id, i);
3883 else if (goal_alt_matched[i][0] == -1)
3885 machine_mode mode;
3886 rtx reg, *loc;
3887 int hard_regno, byte;
3888 enum op_type type = curr_static_id->operand[i].type;
3890 loc = curr_id->operand_loc[i];
3891 mode = curr_operand_mode[i];
3892 if (GET_CODE (*loc) == SUBREG)
3894 reg = SUBREG_REG (*loc);
3895 byte = SUBREG_BYTE (*loc);
3896 if (REG_P (reg)
3897 /* Strict_low_part requires reload the register not
3898 the sub-register. */
3899 && (curr_static_id->operand[i].strict_low
3900 || (GET_MODE_SIZE (mode)
3901 <= GET_MODE_SIZE (GET_MODE (reg))
3902 && (hard_regno
3903 = get_try_hard_regno (REGNO (reg))) >= 0
3904 && (simplify_subreg_regno
3905 (hard_regno,
3906 GET_MODE (reg), byte, mode) < 0)
3907 && (goal_alt[i] == NO_REGS
3908 || (simplify_subreg_regno
3909 (ira_class_hard_regs[goal_alt[i]][0],
3910 GET_MODE (reg), byte, mode) >= 0)))))
3912 if (type == OP_OUT)
3913 type = OP_INOUT;
3914 loc = &SUBREG_REG (*loc);
3915 mode = GET_MODE (*loc);
3918 old = *loc;
3919 if (get_reload_reg (type, mode, old, goal_alt[i],
3920 loc != curr_id->operand_loc[i], "", &new_reg)
3921 && type != OP_OUT)
3923 push_to_sequence (before);
3924 lra_emit_move (new_reg, old);
3925 before = get_insns ();
3926 end_sequence ();
3928 *loc = new_reg;
3929 if (type != OP_IN
3930 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3932 start_sequence ();
3933 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3934 emit_insn (after);
3935 after = get_insns ();
3936 end_sequence ();
3937 *loc = new_reg;
3939 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3940 if (goal_alt_dont_inherit_ops[j] == i)
3942 lra_set_regno_unique_value (REGNO (new_reg));
3943 break;
3945 lra_update_dup (curr_id, i);
3947 else if (curr_static_id->operand[i].type == OP_IN
3948 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3949 == OP_OUT))
3951 /* generate reloads for input and matched outputs. */
3952 match_inputs[0] = i;
3953 match_inputs[1] = -1;
3954 match_reload (goal_alt_matched[i][0], match_inputs,
3955 goal_alt[i], &before, &after,
3956 curr_static_id->operand_alternative
3957 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
3958 .earlyclobber);
3960 else if (curr_static_id->operand[i].type == OP_OUT
3961 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3962 == OP_IN))
3963 /* Generate reloads for output and matched inputs. */
3964 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after,
3965 curr_static_id->operand_alternative
3966 [goal_alt_number * n_operands + i].earlyclobber);
3967 else if (curr_static_id->operand[i].type == OP_IN
3968 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3969 == OP_IN))
3971 /* Generate reloads for matched inputs. */
3972 match_inputs[0] = i;
3973 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3974 match_inputs[j + 1] = k;
3975 match_inputs[j + 1] = -1;
3976 match_reload (-1, match_inputs, goal_alt[i], &before, &after, false);
3978 else
3979 /* We must generate code in any case when function
3980 process_alt_operands decides that it is possible. */
3981 gcc_unreachable ();
3982 if (optional_p)
3984 lra_assert (REG_P (op));
3985 regno = REGNO (op);
3986 op = *curr_id->operand_loc[i]; /* Substitution. */
3987 if (GET_CODE (op) == SUBREG)
3988 op = SUBREG_REG (op);
3989 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3990 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3991 lra_reg_info[REGNO (op)].restore_regno = regno;
3992 if (lra_dump_file != NULL)
3993 fprintf (lra_dump_file,
3994 " Making reload reg %d for reg %d optional\n",
3995 REGNO (op), regno);
3998 if (before != NULL_RTX || after != NULL_RTX
3999 || max_regno_before != max_reg_num ())
4000 change_p = true;
4001 if (change_p)
4003 lra_update_operator_dups (curr_id);
4004 /* Something changes -- process the insn. */
4005 lra_update_insn_regno_info (curr_insn);
4007 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
4008 return change_p;
4011 /* Return true if INSN satisfies all constraints. In other words, no
4012 reload insns are needed. */
4013 bool
4014 lra_constrain_insn (rtx_insn *insn)
4016 int saved_new_regno_start = new_regno_start;
4017 int saved_new_insn_uid_start = new_insn_uid_start;
4018 bool change_p;
4020 curr_insn = insn;
4021 curr_id = lra_get_insn_recog_data (curr_insn);
4022 curr_static_id = curr_id->insn_static_data;
4023 new_insn_uid_start = get_max_uid ();
4024 new_regno_start = max_reg_num ();
4025 change_p = curr_insn_transform (true);
4026 new_regno_start = saved_new_regno_start;
4027 new_insn_uid_start = saved_new_insn_uid_start;
4028 return ! change_p;
4031 /* Return true if X is in LIST. */
4032 static bool
4033 in_list_p (rtx x, rtx list)
4035 for (; list != NULL_RTX; list = XEXP (list, 1))
4036 if (XEXP (list, 0) == x)
4037 return true;
4038 return false;
4041 /* Return true if X contains an allocatable hard register (if
4042 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
4043 static bool
4044 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
4046 int i, j;
4047 const char *fmt;
4048 enum rtx_code code;
4050 code = GET_CODE (x);
4051 if (REG_P (x))
4053 int regno = REGNO (x);
4054 HARD_REG_SET alloc_regs;
4056 if (hard_reg_p)
4058 if (regno >= FIRST_PSEUDO_REGISTER)
4059 regno = lra_get_regno_hard_regno (regno);
4060 if (regno < 0)
4061 return false;
4062 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
4063 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
4065 else
4067 if (regno < FIRST_PSEUDO_REGISTER)
4068 return false;
4069 if (! spilled_p)
4070 return true;
4071 return lra_get_regno_hard_regno (regno) < 0;
4074 fmt = GET_RTX_FORMAT (code);
4075 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4077 if (fmt[i] == 'e')
4079 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4080 return true;
4082 else if (fmt[i] == 'E')
4084 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4085 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4086 return true;
4089 return false;
4092 /* Process all regs in location *LOC and change them on equivalent
4093 substitution. Return true if any change was done. */
4094 static bool
4095 loc_equivalence_change_p (rtx *loc)
4097 rtx subst, reg, x = *loc;
4098 bool result = false;
4099 enum rtx_code code = GET_CODE (x);
4100 const char *fmt;
4101 int i, j;
4103 if (code == SUBREG)
4105 reg = SUBREG_REG (x);
4106 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4107 && GET_MODE (subst) == VOIDmode)
4109 /* We cannot reload debug location. Simplify subreg here
4110 while we know the inner mode. */
4111 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4112 GET_MODE (reg), SUBREG_BYTE (x));
4113 return true;
4116 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4118 *loc = subst;
4119 return true;
4122 /* Scan all the operand sub-expressions. */
4123 fmt = GET_RTX_FORMAT (code);
4124 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4126 if (fmt[i] == 'e')
4127 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4128 else if (fmt[i] == 'E')
4129 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4130 result
4131 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4133 return result;
4136 /* Similar to loc_equivalence_change_p, but for use as
4137 simplify_replace_fn_rtx callback. DATA is insn for which the
4138 elimination is done. If it null we don't do the elimination. */
4139 static rtx
4140 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4142 if (!REG_P (loc))
4143 return NULL_RTX;
4145 rtx subst = (data == NULL
4146 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4147 if (subst != loc)
4148 return subst;
4150 return NULL_RTX;
4153 /* Maximum number of generated reload insns per an insn. It is for
4154 preventing this pass cycling in a bug case. */
4155 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4157 /* The current iteration number of this LRA pass. */
4158 int lra_constraint_iter;
4160 /* True if we substituted equiv which needs checking register
4161 allocation correctness because the equivalent value contains
4162 allocatable hard registers or when we restore multi-register
4163 pseudo. */
4164 bool lra_risky_transformations_p;
4166 /* Return true if REGNO is referenced in more than one block. */
4167 static bool
4168 multi_block_pseudo_p (int regno)
4170 basic_block bb = NULL;
4171 unsigned int uid;
4172 bitmap_iterator bi;
4174 if (regno < FIRST_PSEUDO_REGISTER)
4175 return false;
4177 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4178 if (bb == NULL)
4179 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4180 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4181 return true;
4182 return false;
4185 /* Return true if LIST contains a deleted insn. */
4186 static bool
4187 contains_deleted_insn_p (rtx_insn_list *list)
4189 for (; list != NULL_RTX; list = list->next ())
4190 if (NOTE_P (list->insn ())
4191 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4192 return true;
4193 return false;
4196 /* Return true if X contains a pseudo dying in INSN. */
4197 static bool
4198 dead_pseudo_p (rtx x, rtx_insn *insn)
4200 int i, j;
4201 const char *fmt;
4202 enum rtx_code code;
4204 if (REG_P (x))
4205 return (insn != NULL_RTX
4206 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4207 code = GET_CODE (x);
4208 fmt = GET_RTX_FORMAT (code);
4209 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4211 if (fmt[i] == 'e')
4213 if (dead_pseudo_p (XEXP (x, i), insn))
4214 return true;
4216 else if (fmt[i] == 'E')
4218 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4219 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4220 return true;
4223 return false;
4226 /* Return true if INSN contains a dying pseudo in INSN right hand
4227 side. */
4228 static bool
4229 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4231 rtx set = single_set (insn);
4233 gcc_assert (set != NULL);
4234 return dead_pseudo_p (SET_SRC (set), insn);
4237 /* Return true if any init insn of REGNO contains a dying pseudo in
4238 insn right hand side. */
4239 static bool
4240 init_insn_rhs_dead_pseudo_p (int regno)
4242 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4244 if (insns == NULL)
4245 return false;
4246 for (; insns != NULL_RTX; insns = insns->next ())
4247 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4248 return true;
4249 return false;
4252 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4253 reverse only if we have one init insn with given REGNO as a
4254 source. */
4255 static bool
4256 reverse_equiv_p (int regno)
4258 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4259 rtx set;
4261 if (insns == NULL)
4262 return false;
4263 if (! INSN_P (insns->insn ())
4264 || insns->next () != NULL)
4265 return false;
4266 if ((set = single_set (insns->insn ())) == NULL_RTX)
4267 return false;
4268 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4271 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4272 call this function only for non-reverse equivalence. */
4273 static bool
4274 contains_reloaded_insn_p (int regno)
4276 rtx set;
4277 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4279 for (; list != NULL; list = list->next ())
4280 if ((set = single_set (list->insn ())) == NULL_RTX
4281 || ! REG_P (SET_DEST (set))
4282 || (int) REGNO (SET_DEST (set)) != regno)
4283 return true;
4284 return false;
4287 /* Entry function of LRA constraint pass. Return true if the
4288 constraint pass did change the code. */
4289 bool
4290 lra_constraints (bool first_p)
4292 bool changed_p;
4293 int i, hard_regno, new_insns_num;
4294 unsigned int min_len, new_min_len, uid;
4295 rtx set, x, reg, dest_reg;
4296 basic_block last_bb;
4297 bitmap_head equiv_insn_bitmap;
4298 bitmap_iterator bi;
4300 lra_constraint_iter++;
4301 if (lra_dump_file != NULL)
4302 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4303 lra_constraint_iter);
4304 changed_p = false;
4305 if (pic_offset_table_rtx
4306 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4307 lra_risky_transformations_p = true;
4308 else
4309 lra_risky_transformations_p = false;
4310 new_insn_uid_start = get_max_uid ();
4311 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4312 /* Mark used hard regs for target stack size calulations. */
4313 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4314 if (lra_reg_info[i].nrefs != 0
4315 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4317 int j, nregs;
4319 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4320 for (j = 0; j < nregs; j++)
4321 df_set_regs_ever_live (hard_regno + j, true);
4323 /* Do elimination before the equivalence processing as we can spill
4324 some pseudos during elimination. */
4325 lra_eliminate (false, first_p);
4326 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4327 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4328 if (lra_reg_info[i].nrefs != 0)
4330 ira_reg_equiv[i].profitable_p = true;
4331 reg = regno_reg_rtx[i];
4332 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4334 bool pseudo_p = contains_reg_p (x, false, false);
4336 /* After RTL transformation, we can not guarantee that
4337 pseudo in the substitution was not reloaded which might
4338 make equivalence invalid. For example, in reverse
4339 equiv of p0
4341 p0 <- ...
4343 equiv_mem <- p0
4345 the memory address register was reloaded before the 2nd
4346 insn. */
4347 if ((! first_p && pseudo_p)
4348 /* We don't use DF for compilation speed sake. So it
4349 is problematic to update live info when we use an
4350 equivalence containing pseudos in more than one
4351 BB. */
4352 || (pseudo_p && multi_block_pseudo_p (i))
4353 /* If an init insn was deleted for some reason, cancel
4354 the equiv. We could update the equiv insns after
4355 transformations including an equiv insn deletion
4356 but it is not worthy as such cases are extremely
4357 rare. */
4358 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4359 /* If it is not a reverse equivalence, we check that a
4360 pseudo in rhs of the init insn is not dying in the
4361 insn. Otherwise, the live info at the beginning of
4362 the corresponding BB might be wrong after we
4363 removed the insn. When the equiv can be a
4364 constant, the right hand side of the init insn can
4365 be a pseudo. */
4366 || (! reverse_equiv_p (i)
4367 && (init_insn_rhs_dead_pseudo_p (i)
4368 /* If we reloaded the pseudo in an equivalence
4369 init insn, we can not remove the equiv init
4370 insns and the init insns might write into
4371 const memory in this case. */
4372 || contains_reloaded_insn_p (i)))
4373 /* Prevent access beyond equivalent memory for
4374 paradoxical subregs. */
4375 || (MEM_P (x)
4376 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4377 > GET_MODE_SIZE (GET_MODE (x))))
4378 || (pic_offset_table_rtx
4379 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4380 && (targetm.preferred_reload_class
4381 (x, lra_get_allocno_class (i)) == NO_REGS))
4382 || contains_symbol_ref_p (x))))
4383 ira_reg_equiv[i].defined_p = false;
4384 if (contains_reg_p (x, false, true))
4385 ira_reg_equiv[i].profitable_p = false;
4386 if (get_equiv (reg) != reg)
4387 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4390 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4391 update_equiv (i);
4392 /* We should add all insns containing pseudos which should be
4393 substituted by their equivalences. */
4394 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4395 lra_push_insn_by_uid (uid);
4396 min_len = lra_insn_stack_length ();
4397 new_insns_num = 0;
4398 last_bb = NULL;
4399 changed_p = false;
4400 while ((new_min_len = lra_insn_stack_length ()) != 0)
4402 curr_insn = lra_pop_insn ();
4403 --new_min_len;
4404 curr_bb = BLOCK_FOR_INSN (curr_insn);
4405 if (curr_bb != last_bb)
4407 last_bb = curr_bb;
4408 bb_reload_num = lra_curr_reload_num;
4410 if (min_len > new_min_len)
4412 min_len = new_min_len;
4413 new_insns_num = 0;
4415 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4416 internal_error
4417 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4418 MAX_RELOAD_INSNS_NUMBER);
4419 new_insns_num++;
4420 if (DEBUG_INSN_P (curr_insn))
4422 /* We need to check equivalence in debug insn and change
4423 pseudo to the equivalent value if necessary. */
4424 curr_id = lra_get_insn_recog_data (curr_insn);
4425 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4427 rtx old = *curr_id->operand_loc[0];
4428 *curr_id->operand_loc[0]
4429 = simplify_replace_fn_rtx (old, NULL_RTX,
4430 loc_equivalence_callback, curr_insn);
4431 if (old != *curr_id->operand_loc[0])
4433 lra_update_insn_regno_info (curr_insn);
4434 changed_p = true;
4438 else if (INSN_P (curr_insn))
4440 if ((set = single_set (curr_insn)) != NULL_RTX)
4442 dest_reg = SET_DEST (set);
4443 /* The equivalence pseudo could be set up as SUBREG in a
4444 case when it is a call restore insn in a mode
4445 different from the pseudo mode. */
4446 if (GET_CODE (dest_reg) == SUBREG)
4447 dest_reg = SUBREG_REG (dest_reg);
4448 if ((REG_P (dest_reg)
4449 && (x = get_equiv (dest_reg)) != dest_reg
4450 /* Remove insns which set up a pseudo whose value
4451 can not be changed. Such insns might be not in
4452 init_insns because we don't update equiv data
4453 during insn transformations.
4455 As an example, let suppose that a pseudo got
4456 hard register and on the 1st pass was not
4457 changed to equivalent constant. We generate an
4458 additional insn setting up the pseudo because of
4459 secondary memory movement. Then the pseudo is
4460 spilled and we use the equiv constant. In this
4461 case we should remove the additional insn and
4462 this insn is not init_insns list. */
4463 && (! MEM_P (x) || MEM_READONLY_P (x)
4464 /* Check that this is actually an insn setting
4465 up the equivalence. */
4466 || in_list_p (curr_insn,
4467 ira_reg_equiv
4468 [REGNO (dest_reg)].init_insns)))
4469 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4470 && in_list_p (curr_insn,
4471 ira_reg_equiv
4472 [REGNO (SET_SRC (set))].init_insns)))
4474 /* This is equiv init insn of pseudo which did not get a
4475 hard register -- remove the insn. */
4476 if (lra_dump_file != NULL)
4478 fprintf (lra_dump_file,
4479 " Removing equiv init insn %i (freq=%d)\n",
4480 INSN_UID (curr_insn),
4481 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4482 dump_insn_slim (lra_dump_file, curr_insn);
4484 if (contains_reg_p (x, true, false))
4485 lra_risky_transformations_p = true;
4486 lra_set_insn_deleted (curr_insn);
4487 continue;
4490 curr_id = lra_get_insn_recog_data (curr_insn);
4491 curr_static_id = curr_id->insn_static_data;
4492 init_curr_insn_input_reloads ();
4493 init_curr_operand_mode ();
4494 if (curr_insn_transform (false))
4495 changed_p = true;
4496 /* Check non-transformed insns too for equiv change as USE
4497 or CLOBBER don't need reloads but can contain pseudos
4498 being changed on their equivalences. */
4499 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4500 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4502 lra_update_insn_regno_info (curr_insn);
4503 changed_p = true;
4507 bitmap_clear (&equiv_insn_bitmap);
4508 /* If we used a new hard regno, changed_p should be true because the
4509 hard reg is assigned to a new pseudo. */
4510 if (flag_checking && !changed_p)
4512 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4513 if (lra_reg_info[i].nrefs != 0
4514 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4516 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4518 for (j = 0; j < nregs; j++)
4519 lra_assert (df_regs_ever_live_p (hard_regno + j));
4522 return changed_p;
4525 /* Initiate the LRA constraint pass. It is done once per
4526 function. */
4527 void
4528 lra_constraints_init (void)
4532 /* Finalize the LRA constraint pass. It is done once per
4533 function. */
4534 void
4535 lra_constraints_finish (void)
4541 /* This page contains code to do inheritance/split
4542 transformations. */
4544 /* Number of reloads passed so far in current EBB. */
4545 static int reloads_num;
4547 /* Number of calls passed so far in current EBB. */
4548 static int calls_num;
4550 /* Current reload pseudo check for validity of elements in
4551 USAGE_INSNS. */
4552 static int curr_usage_insns_check;
4554 /* Info about last usage of registers in EBB to do inheritance/split
4555 transformation. Inheritance transformation is done from a spilled
4556 pseudo and split transformations from a hard register or a pseudo
4557 assigned to a hard register. */
4558 struct usage_insns
4560 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4561 value INSNS is valid. The insns is chain of optional debug insns
4562 and a finishing non-debug insn using the corresponding reg. The
4563 value is also used to mark the registers which are set up in the
4564 current insn. The negated insn uid is used for this. */
4565 int check;
4566 /* Value of global reloads_num at the last insn in INSNS. */
4567 int reloads_num;
4568 /* Value of global reloads_nums at the last insn in INSNS. */
4569 int calls_num;
4570 /* It can be true only for splitting. And it means that the restore
4571 insn should be put after insn given by the following member. */
4572 bool after_p;
4573 /* Next insns in the current EBB which use the original reg and the
4574 original reg value is not changed between the current insn and
4575 the next insns. In order words, e.g. for inheritance, if we need
4576 to use the original reg value again in the next insns we can try
4577 to use the value in a hard register from a reload insn of the
4578 current insn. */
4579 rtx insns;
4582 /* Map: regno -> corresponding pseudo usage insns. */
4583 static struct usage_insns *usage_insns;
4585 static void
4586 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4588 usage_insns[regno].check = curr_usage_insns_check;
4589 usage_insns[regno].insns = insn;
4590 usage_insns[regno].reloads_num = reloads_num;
4591 usage_insns[regno].calls_num = calls_num;
4592 usage_insns[regno].after_p = after_p;
4595 /* The function is used to form list REGNO usages which consists of
4596 optional debug insns finished by a non-debug insn using REGNO.
4597 RELOADS_NUM is current number of reload insns processed so far. */
4598 static void
4599 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
4601 rtx next_usage_insns;
4603 if (usage_insns[regno].check == curr_usage_insns_check
4604 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4605 && DEBUG_INSN_P (insn))
4607 /* Check that we did not add the debug insn yet. */
4608 if (next_usage_insns != insn
4609 && (GET_CODE (next_usage_insns) != INSN_LIST
4610 || XEXP (next_usage_insns, 0) != insn))
4611 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4612 next_usage_insns);
4614 else if (NONDEBUG_INSN_P (insn))
4615 setup_next_usage_insn (regno, insn, reloads_num, false);
4616 else
4617 usage_insns[regno].check = 0;
4620 /* Return first non-debug insn in list USAGE_INSNS. */
4621 static rtx_insn *
4622 skip_usage_debug_insns (rtx usage_insns)
4624 rtx insn;
4626 /* Skip debug insns. */
4627 for (insn = usage_insns;
4628 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4629 insn = XEXP (insn, 1))
4631 return safe_as_a <rtx_insn *> (insn);
4634 /* Return true if we need secondary memory moves for insn in
4635 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4636 into the insn. */
4637 static bool
4638 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4639 rtx usage_insns ATTRIBUTE_UNUSED)
4641 #ifndef SECONDARY_MEMORY_NEEDED
4642 return false;
4643 #else
4644 rtx_insn *insn;
4645 rtx set, dest;
4646 enum reg_class cl;
4648 if (inher_cl == ALL_REGS
4649 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4650 return false;
4651 lra_assert (INSN_P (insn));
4652 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4653 return false;
4654 dest = SET_DEST (set);
4655 if (! REG_P (dest))
4656 return false;
4657 lra_assert (inher_cl != NO_REGS);
4658 cl = get_reg_class (REGNO (dest));
4659 return (cl != NO_REGS && cl != ALL_REGS
4660 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4661 #endif
4664 /* Registers involved in inheritance/split in the current EBB
4665 (inheritance/split pseudos and original registers). */
4666 static bitmap_head check_only_regs;
4668 /* Do inheritance transformations for insn INSN, which defines (if
4669 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4670 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4671 form as the "insns" field of usage_insns. Return true if we
4672 succeed in such transformation.
4674 The transformations look like:
4676 p <- ... i <- ...
4677 ... p <- i (new insn)
4678 ... =>
4679 <- ... p ... <- ... i ...
4681 ... i <- p (new insn)
4682 <- ... p ... <- ... i ...
4683 ... =>
4684 <- ... p ... <- ... i ...
4685 where p is a spilled original pseudo and i is a new inheritance pseudo.
4688 The inheritance pseudo has the smallest class of two classes CL and
4689 class of ORIGINAL REGNO. */
4690 static bool
4691 inherit_reload_reg (bool def_p, int original_regno,
4692 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4694 if (optimize_function_for_size_p (cfun))
4695 return false;
4697 enum reg_class rclass = lra_get_allocno_class (original_regno);
4698 rtx original_reg = regno_reg_rtx[original_regno];
4699 rtx new_reg, usage_insn;
4700 rtx_insn *new_insns;
4702 lra_assert (! usage_insns[original_regno].after_p);
4703 if (lra_dump_file != NULL)
4704 fprintf (lra_dump_file,
4705 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4706 if (! ira_reg_classes_intersect_p[cl][rclass])
4708 if (lra_dump_file != NULL)
4710 fprintf (lra_dump_file,
4711 " Rejecting inheritance for %d "
4712 "because of disjoint classes %s and %s\n",
4713 original_regno, reg_class_names[cl],
4714 reg_class_names[rclass]);
4715 fprintf (lra_dump_file,
4716 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4718 return false;
4720 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4721 /* We don't use a subset of two classes because it can be
4722 NO_REGS. This transformation is still profitable in most
4723 cases even if the classes are not intersected as register
4724 move is probably cheaper than a memory load. */
4725 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4727 if (lra_dump_file != NULL)
4728 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4729 reg_class_names[cl], reg_class_names[rclass]);
4731 rclass = cl;
4733 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4735 /* Reject inheritance resulting in secondary memory moves.
4736 Otherwise, there is a danger in LRA cycling. Also such
4737 transformation will be unprofitable. */
4738 if (lra_dump_file != NULL)
4740 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4741 rtx set = single_set (insn);
4743 lra_assert (set != NULL_RTX);
4745 rtx dest = SET_DEST (set);
4747 lra_assert (REG_P (dest));
4748 fprintf (lra_dump_file,
4749 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4750 "as secondary mem is needed\n",
4751 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4752 original_regno, reg_class_names[rclass]);
4753 fprintf (lra_dump_file,
4754 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4756 return false;
4758 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4759 rclass, "inheritance");
4760 start_sequence ();
4761 if (def_p)
4762 lra_emit_move (original_reg, new_reg);
4763 else
4764 lra_emit_move (new_reg, original_reg);
4765 new_insns = get_insns ();
4766 end_sequence ();
4767 if (NEXT_INSN (new_insns) != NULL_RTX)
4769 if (lra_dump_file != NULL)
4771 fprintf (lra_dump_file,
4772 " Rejecting inheritance %d->%d "
4773 "as it results in 2 or more insns:\n",
4774 original_regno, REGNO (new_reg));
4775 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4776 fprintf (lra_dump_file,
4777 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4779 return false;
4781 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
4782 lra_update_insn_regno_info (insn);
4783 if (! def_p)
4784 /* We now have a new usage insn for original regno. */
4785 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4786 if (lra_dump_file != NULL)
4787 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4788 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4789 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4790 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4791 bitmap_set_bit (&check_only_regs, original_regno);
4792 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4793 if (def_p)
4794 lra_process_new_insns (insn, NULL, new_insns,
4795 "Add original<-inheritance");
4796 else
4797 lra_process_new_insns (insn, new_insns, NULL,
4798 "Add inheritance<-original");
4799 while (next_usage_insns != NULL_RTX)
4801 if (GET_CODE (next_usage_insns) != INSN_LIST)
4803 usage_insn = next_usage_insns;
4804 lra_assert (NONDEBUG_INSN_P (usage_insn));
4805 next_usage_insns = NULL;
4807 else
4809 usage_insn = XEXP (next_usage_insns, 0);
4810 lra_assert (DEBUG_INSN_P (usage_insn));
4811 next_usage_insns = XEXP (next_usage_insns, 1);
4813 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
4814 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4815 if (lra_dump_file != NULL)
4817 fprintf (lra_dump_file,
4818 " Inheritance reuse change %d->%d (bb%d):\n",
4819 original_regno, REGNO (new_reg),
4820 BLOCK_FOR_INSN (usage_insn)->index);
4821 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
4824 if (lra_dump_file != NULL)
4825 fprintf (lra_dump_file,
4826 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4827 return true;
4830 /* Return true if we need a caller save/restore for pseudo REGNO which
4831 was assigned to a hard register. */
4832 static inline bool
4833 need_for_call_save_p (int regno)
4835 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4836 return (usage_insns[regno].calls_num < calls_num
4837 && (overlaps_hard_reg_set_p
4838 ((flag_ipa_ra &&
4839 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4840 ? lra_reg_info[regno].actual_call_used_reg_set
4841 : call_used_reg_set,
4842 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4843 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4844 PSEUDO_REGNO_MODE (regno))));
4847 /* Global registers occurring in the current EBB. */
4848 static bitmap_head ebb_global_regs;
4850 /* Return true if we need a split for hard register REGNO or pseudo
4851 REGNO which was assigned to a hard register.
4852 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4853 used for reloads since the EBB end. It is an approximation of the
4854 used hard registers in the split range. The exact value would
4855 require expensive calculations. If we were aggressive with
4856 splitting because of the approximation, the split pseudo will save
4857 the same hard register assignment and will be removed in the undo
4858 pass. We still need the approximation because too aggressive
4859 splitting would result in too inaccurate cost calculation in the
4860 assignment pass because of too many generated moves which will be
4861 probably removed in the undo pass. */
4862 static inline bool
4863 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4865 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4867 lra_assert (hard_regno >= 0);
4868 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4869 /* Don't split eliminable hard registers, otherwise we can
4870 split hard registers like hard frame pointer, which
4871 lives on BB start/end according to DF-infrastructure,
4872 when there is a pseudo assigned to the register and
4873 living in the same BB. */
4874 && (regno >= FIRST_PSEUDO_REGISTER
4875 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4876 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4877 /* Don't split call clobbered hard regs living through
4878 calls, otherwise we might have a check problem in the
4879 assign sub-pass as in the most cases (exception is a
4880 situation when lra_risky_transformations_p value is
4881 true) the assign pass assumes that all pseudos living
4882 through calls are assigned to call saved hard regs. */
4883 && (regno >= FIRST_PSEUDO_REGISTER
4884 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4885 || usage_insns[regno].calls_num == calls_num)
4886 /* We need at least 2 reloads to make pseudo splitting
4887 profitable. We should provide hard regno splitting in
4888 any case to solve 1st insn scheduling problem when
4889 moving hard register definition up might result in
4890 impossibility to find hard register for reload pseudo of
4891 small register class. */
4892 && (usage_insns[regno].reloads_num
4893 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4894 && (regno < FIRST_PSEUDO_REGISTER
4895 /* For short living pseudos, spilling + inheritance can
4896 be considered a substitution for splitting.
4897 Therefore we do not splitting for local pseudos. It
4898 decreases also aggressiveness of splitting. The
4899 minimal number of references is chosen taking into
4900 account that for 2 references splitting has no sense
4901 as we can just spill the pseudo. */
4902 || (regno >= FIRST_PSEUDO_REGISTER
4903 && lra_reg_info[regno].nrefs > 3
4904 && bitmap_bit_p (&ebb_global_regs, regno))))
4905 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4908 /* Return class for the split pseudo created from original pseudo with
4909 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4910 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4911 results in no secondary memory movements. */
4912 static enum reg_class
4913 choose_split_class (enum reg_class allocno_class,
4914 int hard_regno ATTRIBUTE_UNUSED,
4915 machine_mode mode ATTRIBUTE_UNUSED)
4917 #ifndef SECONDARY_MEMORY_NEEDED
4918 return allocno_class;
4919 #else
4920 int i;
4921 enum reg_class cl, best_cl = NO_REGS;
4922 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4923 = REGNO_REG_CLASS (hard_regno);
4925 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4926 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4927 return allocno_class;
4928 for (i = 0;
4929 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4930 i++)
4931 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4932 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4933 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4934 && (best_cl == NO_REGS
4935 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4936 best_cl = cl;
4937 return best_cl;
4938 #endif
4941 /* Do split transformations for insn INSN, which defines or uses
4942 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4943 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4944 "insns" field of usage_insns.
4946 The transformations look like:
4948 p <- ... p <- ...
4949 ... s <- p (new insn -- save)
4950 ... =>
4951 ... p <- s (new insn -- restore)
4952 <- ... p ... <- ... p ...
4954 <- ... p ... <- ... p ...
4955 ... s <- p (new insn -- save)
4956 ... =>
4957 ... p <- s (new insn -- restore)
4958 <- ... p ... <- ... p ...
4960 where p is an original pseudo got a hard register or a hard
4961 register and s is a new split pseudo. The save is put before INSN
4962 if BEFORE_P is true. Return true if we succeed in such
4963 transformation. */
4964 static bool
4965 split_reg (bool before_p, int original_regno, rtx_insn *insn,
4966 rtx next_usage_insns)
4968 enum reg_class rclass;
4969 rtx original_reg;
4970 int hard_regno, nregs;
4971 rtx new_reg, usage_insn;
4972 rtx_insn *restore, *save;
4973 bool after_p;
4974 bool call_save_p;
4976 if (original_regno < FIRST_PSEUDO_REGISTER)
4978 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4979 hard_regno = original_regno;
4980 call_save_p = false;
4981 nregs = 1;
4983 else
4985 hard_regno = reg_renumber[original_regno];
4986 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4987 rclass = lra_get_allocno_class (original_regno);
4988 original_reg = regno_reg_rtx[original_regno];
4989 call_save_p = need_for_call_save_p (original_regno);
4991 original_reg = regno_reg_rtx[original_regno];
4992 lra_assert (hard_regno >= 0);
4993 if (lra_dump_file != NULL)
4994 fprintf (lra_dump_file,
4995 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4996 if (call_save_p)
4998 machine_mode mode = GET_MODE (original_reg);
5000 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
5001 hard_regno_nregs[hard_regno][mode],
5002 mode);
5003 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
5005 else
5007 rclass = choose_split_class (rclass, hard_regno,
5008 GET_MODE (original_reg));
5009 if (rclass == NO_REGS)
5011 if (lra_dump_file != NULL)
5013 fprintf (lra_dump_file,
5014 " Rejecting split of %d(%s): "
5015 "no good reg class for %d(%s)\n",
5016 original_regno,
5017 reg_class_names[lra_get_allocno_class (original_regno)],
5018 hard_regno,
5019 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
5020 fprintf
5021 (lra_dump_file,
5022 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5024 return false;
5026 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
5027 rclass, "split");
5028 reg_renumber[REGNO (new_reg)] = hard_regno;
5030 save = emit_spill_move (true, new_reg, original_reg);
5031 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
5033 if (lra_dump_file != NULL)
5035 fprintf
5036 (lra_dump_file,
5037 " Rejecting split %d->%d resulting in > 2 save insns:\n",
5038 original_regno, REGNO (new_reg));
5039 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
5040 fprintf (lra_dump_file,
5041 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5043 return false;
5045 restore = emit_spill_move (false, new_reg, original_reg);
5046 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
5048 if (lra_dump_file != NULL)
5050 fprintf (lra_dump_file,
5051 " Rejecting split %d->%d "
5052 "resulting in > 2 restore insns:\n",
5053 original_regno, REGNO (new_reg));
5054 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
5055 fprintf (lra_dump_file,
5056 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5058 return false;
5060 after_p = usage_insns[original_regno].after_p;
5061 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
5062 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5063 bitmap_set_bit (&check_only_regs, original_regno);
5064 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
5065 for (;;)
5067 if (GET_CODE (next_usage_insns) != INSN_LIST)
5069 usage_insn = next_usage_insns;
5070 break;
5072 usage_insn = XEXP (next_usage_insns, 0);
5073 lra_assert (DEBUG_INSN_P (usage_insn));
5074 next_usage_insns = XEXP (next_usage_insns, 1);
5075 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5076 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5077 if (lra_dump_file != NULL)
5079 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5080 original_regno, REGNO (new_reg));
5081 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5084 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5085 lra_assert (usage_insn != insn || (after_p && before_p));
5086 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5087 after_p ? NULL : restore,
5088 after_p ? restore : NULL,
5089 call_save_p
5090 ? "Add reg<-save" : "Add reg<-split");
5091 lra_process_new_insns (insn, before_p ? save : NULL,
5092 before_p ? NULL : save,
5093 call_save_p
5094 ? "Add save<-reg" : "Add split<-reg");
5095 if (nregs > 1)
5096 /* If we are trying to split multi-register. We should check
5097 conflicts on the next assignment sub-pass. IRA can allocate on
5098 sub-register levels, LRA do this on pseudos level right now and
5099 this discrepancy may create allocation conflicts after
5100 splitting. */
5101 lra_risky_transformations_p = true;
5102 if (lra_dump_file != NULL)
5103 fprintf (lra_dump_file,
5104 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5105 return true;
5108 /* Recognize that we need a split transformation for insn INSN, which
5109 defines or uses REGNO in its insn biggest MODE (we use it only if
5110 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5111 hard registers which might be used for reloads since the EBB end.
5112 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5113 uid before starting INSN processing. Return true if we succeed in
5114 such transformation. */
5115 static bool
5116 split_if_necessary (int regno, machine_mode mode,
5117 HARD_REG_SET potential_reload_hard_regs,
5118 bool before_p, rtx_insn *insn, int max_uid)
5120 bool res = false;
5121 int i, nregs = 1;
5122 rtx next_usage_insns;
5124 if (regno < FIRST_PSEUDO_REGISTER)
5125 nregs = hard_regno_nregs[regno][mode];
5126 for (i = 0; i < nregs; i++)
5127 if (usage_insns[regno + i].check == curr_usage_insns_check
5128 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5129 /* To avoid processing the register twice or more. */
5130 && ((GET_CODE (next_usage_insns) != INSN_LIST
5131 && INSN_UID (next_usage_insns) < max_uid)
5132 || (GET_CODE (next_usage_insns) == INSN_LIST
5133 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5134 && need_for_split_p (potential_reload_hard_regs, regno + i)
5135 && split_reg (before_p, regno + i, insn, next_usage_insns))
5136 res = true;
5137 return res;
5140 /* Check only registers living at the current program point in the
5141 current EBB. */
5142 static bitmap_head live_regs;
5144 /* Update live info in EBB given by its HEAD and TAIL insns after
5145 inheritance/split transformation. The function removes dead moves
5146 too. */
5147 static void
5148 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5150 unsigned int j;
5151 int i, regno;
5152 bool live_p;
5153 rtx_insn *prev_insn;
5154 rtx set;
5155 bool remove_p;
5156 basic_block last_bb, prev_bb, curr_bb;
5157 bitmap_iterator bi;
5158 struct lra_insn_reg *reg;
5159 edge e;
5160 edge_iterator ei;
5162 last_bb = BLOCK_FOR_INSN (tail);
5163 prev_bb = NULL;
5164 for (curr_insn = tail;
5165 curr_insn != PREV_INSN (head);
5166 curr_insn = prev_insn)
5168 prev_insn = PREV_INSN (curr_insn);
5169 /* We need to process empty blocks too. They contain
5170 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5171 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5172 continue;
5173 curr_bb = BLOCK_FOR_INSN (curr_insn);
5174 if (curr_bb != prev_bb)
5176 if (prev_bb != NULL)
5178 /* Update df_get_live_in (prev_bb): */
5179 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5180 if (bitmap_bit_p (&live_regs, j))
5181 bitmap_set_bit (df_get_live_in (prev_bb), j);
5182 else
5183 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5185 if (curr_bb != last_bb)
5187 /* Update df_get_live_out (curr_bb): */
5188 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5190 live_p = bitmap_bit_p (&live_regs, j);
5191 if (! live_p)
5192 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5193 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5195 live_p = true;
5196 break;
5198 if (live_p)
5199 bitmap_set_bit (df_get_live_out (curr_bb), j);
5200 else
5201 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5204 prev_bb = curr_bb;
5205 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5207 if (! NONDEBUG_INSN_P (curr_insn))
5208 continue;
5209 curr_id = lra_get_insn_recog_data (curr_insn);
5210 curr_static_id = curr_id->insn_static_data;
5211 remove_p = false;
5212 if ((set = single_set (curr_insn)) != NULL_RTX
5213 && REG_P (SET_DEST (set))
5214 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5215 && SET_DEST (set) != pic_offset_table_rtx
5216 && bitmap_bit_p (&check_only_regs, regno)
5217 && ! bitmap_bit_p (&live_regs, regno))
5218 remove_p = true;
5219 /* See which defined values die here. */
5220 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5221 if (reg->type == OP_OUT && ! reg->subreg_p)
5222 bitmap_clear_bit (&live_regs, reg->regno);
5223 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5224 if (reg->type == OP_OUT && ! reg->subreg_p)
5225 bitmap_clear_bit (&live_regs, reg->regno);
5226 if (curr_id->arg_hard_regs != NULL)
5227 /* Make clobbered argument hard registers die. */
5228 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5229 if (regno >= FIRST_PSEUDO_REGISTER)
5230 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
5231 /* Mark each used value as live. */
5232 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5233 if (reg->type != OP_OUT
5234 && bitmap_bit_p (&check_only_regs, reg->regno))
5235 bitmap_set_bit (&live_regs, reg->regno);
5236 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5237 if (reg->type != OP_OUT
5238 && bitmap_bit_p (&check_only_regs, reg->regno))
5239 bitmap_set_bit (&live_regs, reg->regno);
5240 if (curr_id->arg_hard_regs != NULL)
5241 /* Make used argument hard registers live. */
5242 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5243 if (regno < FIRST_PSEUDO_REGISTER
5244 && bitmap_bit_p (&check_only_regs, regno))
5245 bitmap_set_bit (&live_regs, regno);
5246 /* It is quite important to remove dead move insns because it
5247 means removing dead store. We don't need to process them for
5248 constraints. */
5249 if (remove_p)
5251 if (lra_dump_file != NULL)
5253 fprintf (lra_dump_file, " Removing dead insn:\n ");
5254 dump_insn_slim (lra_dump_file, curr_insn);
5256 lra_set_insn_deleted (curr_insn);
5261 /* The structure describes info to do an inheritance for the current
5262 insn. We need to collect such info first before doing the
5263 transformations because the transformations change the insn
5264 internal representation. */
5265 struct to_inherit
5267 /* Original regno. */
5268 int regno;
5269 /* Subsequent insns which can inherit original reg value. */
5270 rtx insns;
5273 /* Array containing all info for doing inheritance from the current
5274 insn. */
5275 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5277 /* Number elements in the previous array. */
5278 static int to_inherit_num;
5280 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5281 structure to_inherit. */
5282 static void
5283 add_to_inherit (int regno, rtx insns)
5285 int i;
5287 for (i = 0; i < to_inherit_num; i++)
5288 if (to_inherit[i].regno == regno)
5289 return;
5290 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5291 to_inherit[to_inherit_num].regno = regno;
5292 to_inherit[to_inherit_num++].insns = insns;
5295 /* Return the last non-debug insn in basic block BB, or the block begin
5296 note if none. */
5297 static rtx_insn *
5298 get_last_insertion_point (basic_block bb)
5300 rtx_insn *insn;
5302 FOR_BB_INSNS_REVERSE (bb, insn)
5303 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5304 return insn;
5305 gcc_unreachable ();
5308 /* Set up RES by registers living on edges FROM except the edge (FROM,
5309 TO) or by registers set up in a jump insn in BB FROM. */
5310 static void
5311 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5313 rtx_insn *last;
5314 struct lra_insn_reg *reg;
5315 edge e;
5316 edge_iterator ei;
5318 lra_assert (to != NULL);
5319 bitmap_clear (res);
5320 FOR_EACH_EDGE (e, ei, from->succs)
5321 if (e->dest != to)
5322 bitmap_ior_into (res, df_get_live_in (e->dest));
5323 last = get_last_insertion_point (from);
5324 if (! JUMP_P (last))
5325 return;
5326 curr_id = lra_get_insn_recog_data (last);
5327 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5328 if (reg->type != OP_IN)
5329 bitmap_set_bit (res, reg->regno);
5332 /* Used as a temporary results of some bitmap calculations. */
5333 static bitmap_head temp_bitmap;
5335 /* We split for reloads of small class of hard regs. The following
5336 defines how many hard regs the class should have to be qualified as
5337 small. The code is mostly oriented to x86/x86-64 architecture
5338 where some insns need to use only specific register or pair of
5339 registers and these register can live in RTL explicitly, e.g. for
5340 parameter passing. */
5341 static const int max_small_class_regs_num = 2;
5343 /* Do inheritance/split transformations in EBB starting with HEAD and
5344 finishing on TAIL. We process EBB insns in the reverse order.
5345 Return true if we did any inheritance/split transformation in the
5346 EBB.
5348 We should avoid excessive splitting which results in worse code
5349 because of inaccurate cost calculations for spilling new split
5350 pseudos in such case. To achieve this we do splitting only if
5351 register pressure is high in given basic block and there are reload
5352 pseudos requiring hard registers. We could do more register
5353 pressure calculations at any given program point to avoid necessary
5354 splitting even more but it is to expensive and the current approach
5355 works well enough. */
5356 static bool
5357 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5359 int i, src_regno, dst_regno, nregs;
5360 bool change_p, succ_p, update_reloads_num_p;
5361 rtx_insn *prev_insn, *last_insn;
5362 rtx next_usage_insns, set;
5363 enum reg_class cl;
5364 struct lra_insn_reg *reg;
5365 basic_block last_processed_bb, curr_bb = NULL;
5366 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5367 bitmap to_process;
5368 unsigned int j;
5369 bitmap_iterator bi;
5370 bool head_p, after_p;
5372 change_p = false;
5373 curr_usage_insns_check++;
5374 reloads_num = calls_num = 0;
5375 bitmap_clear (&check_only_regs);
5376 last_processed_bb = NULL;
5377 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5378 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5379 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5380 /* We don't process new insns generated in the loop. */
5381 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5383 prev_insn = PREV_INSN (curr_insn);
5384 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5385 curr_bb = BLOCK_FOR_INSN (curr_insn);
5386 if (last_processed_bb != curr_bb)
5388 /* We are at the end of BB. Add qualified living
5389 pseudos for potential splitting. */
5390 to_process = df_get_live_out (curr_bb);
5391 if (last_processed_bb != NULL)
5393 /* We are somewhere in the middle of EBB. */
5394 get_live_on_other_edges (curr_bb, last_processed_bb,
5395 &temp_bitmap);
5396 to_process = &temp_bitmap;
5398 last_processed_bb = curr_bb;
5399 last_insn = get_last_insertion_point (curr_bb);
5400 after_p = (! JUMP_P (last_insn)
5401 && (! CALL_P (last_insn)
5402 || (find_reg_note (last_insn,
5403 REG_NORETURN, NULL_RTX) == NULL_RTX
5404 && ! SIBLING_CALL_P (last_insn))));
5405 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5406 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5408 if ((int) j >= lra_constraint_new_regno_start)
5409 break;
5410 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5412 if (j < FIRST_PSEUDO_REGISTER)
5413 SET_HARD_REG_BIT (live_hard_regs, j);
5414 else
5415 add_to_hard_reg_set (&live_hard_regs,
5416 PSEUDO_REGNO_MODE (j),
5417 reg_renumber[j]);
5418 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5422 src_regno = dst_regno = -1;
5423 if (NONDEBUG_INSN_P (curr_insn)
5424 && (set = single_set (curr_insn)) != NULL_RTX
5425 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5427 src_regno = REGNO (SET_SRC (set));
5428 dst_regno = REGNO (SET_DEST (set));
5430 update_reloads_num_p = true;
5431 if (src_regno < lra_constraint_new_regno_start
5432 && src_regno >= FIRST_PSEUDO_REGISTER
5433 && reg_renumber[src_regno] < 0
5434 && dst_regno >= lra_constraint_new_regno_start
5435 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5437 /* 'reload_pseudo <- original_pseudo'. */
5438 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5439 reloads_num++;
5440 update_reloads_num_p = false;
5441 succ_p = false;
5442 if (usage_insns[src_regno].check == curr_usage_insns_check
5443 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5444 succ_p = inherit_reload_reg (false, src_regno, cl,
5445 curr_insn, next_usage_insns);
5446 if (succ_p)
5447 change_p = true;
5448 else
5449 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5450 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5451 IOR_HARD_REG_SET (potential_reload_hard_regs,
5452 reg_class_contents[cl]);
5454 else if (src_regno >= lra_constraint_new_regno_start
5455 && dst_regno < lra_constraint_new_regno_start
5456 && dst_regno >= FIRST_PSEUDO_REGISTER
5457 && reg_renumber[dst_regno] < 0
5458 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5459 && usage_insns[dst_regno].check == curr_usage_insns_check
5460 && (next_usage_insns
5461 = usage_insns[dst_regno].insns) != NULL_RTX)
5463 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5464 reloads_num++;
5465 update_reloads_num_p = false;
5466 /* 'original_pseudo <- reload_pseudo'. */
5467 if (! JUMP_P (curr_insn)
5468 && inherit_reload_reg (true, dst_regno, cl,
5469 curr_insn, next_usage_insns))
5470 change_p = true;
5471 /* Invalidate. */
5472 usage_insns[dst_regno].check = 0;
5473 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5474 IOR_HARD_REG_SET (potential_reload_hard_regs,
5475 reg_class_contents[cl]);
5477 else if (INSN_P (curr_insn))
5479 int iter;
5480 int max_uid = get_max_uid ();
5482 curr_id = lra_get_insn_recog_data (curr_insn);
5483 curr_static_id = curr_id->insn_static_data;
5484 to_inherit_num = 0;
5485 /* Process insn definitions. */
5486 for (iter = 0; iter < 2; iter++)
5487 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5488 reg != NULL;
5489 reg = reg->next)
5490 if (reg->type != OP_IN
5491 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5493 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5494 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5495 && usage_insns[dst_regno].check == curr_usage_insns_check
5496 && (next_usage_insns
5497 = usage_insns[dst_regno].insns) != NULL_RTX)
5499 struct lra_insn_reg *r;
5501 for (r = curr_id->regs; r != NULL; r = r->next)
5502 if (r->type != OP_OUT && r->regno == dst_regno)
5503 break;
5504 /* Don't do inheritance if the pseudo is also
5505 used in the insn. */
5506 if (r == NULL)
5507 /* We can not do inheritance right now
5508 because the current insn reg info (chain
5509 regs) can change after that. */
5510 add_to_inherit (dst_regno, next_usage_insns);
5512 /* We can not process one reg twice here because of
5513 usage_insns invalidation. */
5514 if ((dst_regno < FIRST_PSEUDO_REGISTER
5515 || reg_renumber[dst_regno] >= 0)
5516 && ! reg->subreg_p && reg->type != OP_IN)
5518 HARD_REG_SET s;
5520 if (split_if_necessary (dst_regno, reg->biggest_mode,
5521 potential_reload_hard_regs,
5522 false, curr_insn, max_uid))
5523 change_p = true;
5524 CLEAR_HARD_REG_SET (s);
5525 if (dst_regno < FIRST_PSEUDO_REGISTER)
5526 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5527 else
5528 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5529 reg_renumber[dst_regno]);
5530 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5532 /* We should invalidate potential inheritance or
5533 splitting for the current insn usages to the next
5534 usage insns (see code below) as the output pseudo
5535 prevents this. */
5536 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5537 && reg_renumber[dst_regno] < 0)
5538 || (reg->type == OP_OUT && ! reg->subreg_p
5539 && (dst_regno < FIRST_PSEUDO_REGISTER
5540 || reg_renumber[dst_regno] >= 0)))
5542 /* Invalidate and mark definitions. */
5543 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5544 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5545 else
5547 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5548 for (i = 0; i < nregs; i++)
5549 usage_insns[dst_regno + i].check
5550 = -(int) INSN_UID (curr_insn);
5554 /* Process clobbered call regs. */
5555 if (curr_id->arg_hard_regs != NULL)
5556 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5557 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5558 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
5559 = -(int) INSN_UID (curr_insn);
5560 if (! JUMP_P (curr_insn))
5561 for (i = 0; i < to_inherit_num; i++)
5562 if (inherit_reload_reg (true, to_inherit[i].regno,
5563 ALL_REGS, curr_insn,
5564 to_inherit[i].insns))
5565 change_p = true;
5566 if (CALL_P (curr_insn))
5568 rtx cheap, pat, dest;
5569 rtx_insn *restore;
5570 int regno, hard_regno;
5572 calls_num++;
5573 if ((cheap = find_reg_note (curr_insn,
5574 REG_RETURNED, NULL_RTX)) != NULL_RTX
5575 && ((cheap = XEXP (cheap, 0)), true)
5576 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5577 && (hard_regno = reg_renumber[regno]) >= 0
5578 /* If there are pending saves/restores, the
5579 optimization is not worth. */
5580 && usage_insns[regno].calls_num == calls_num - 1
5581 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5583 /* Restore the pseudo from the call result as
5584 REG_RETURNED note says that the pseudo value is
5585 in the call result and the pseudo is an argument
5586 of the call. */
5587 pat = PATTERN (curr_insn);
5588 if (GET_CODE (pat) == PARALLEL)
5589 pat = XVECEXP (pat, 0, 0);
5590 dest = SET_DEST (pat);
5591 /* For multiple return values dest is PARALLEL.
5592 Currently we handle only single return value case. */
5593 if (REG_P (dest))
5595 start_sequence ();
5596 emit_move_insn (cheap, copy_rtx (dest));
5597 restore = get_insns ();
5598 end_sequence ();
5599 lra_process_new_insns (curr_insn, NULL, restore,
5600 "Inserting call parameter restore");
5601 /* We don't need to save/restore of the pseudo from
5602 this call. */
5603 usage_insns[regno].calls_num = calls_num;
5604 bitmap_set_bit (&check_only_regs, regno);
5608 to_inherit_num = 0;
5609 /* Process insn usages. */
5610 for (iter = 0; iter < 2; iter++)
5611 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5612 reg != NULL;
5613 reg = reg->next)
5614 if ((reg->type != OP_OUT
5615 || (reg->type == OP_OUT && reg->subreg_p))
5616 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5618 if (src_regno >= FIRST_PSEUDO_REGISTER
5619 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5621 if (usage_insns[src_regno].check == curr_usage_insns_check
5622 && (next_usage_insns
5623 = usage_insns[src_regno].insns) != NULL_RTX
5624 && NONDEBUG_INSN_P (curr_insn))
5625 add_to_inherit (src_regno, next_usage_insns);
5626 else if (usage_insns[src_regno].check
5627 != -(int) INSN_UID (curr_insn))
5628 /* Add usages but only if the reg is not set up
5629 in the same insn. */
5630 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5632 else if (src_regno < FIRST_PSEUDO_REGISTER
5633 || reg_renumber[src_regno] >= 0)
5635 bool before_p;
5636 rtx_insn *use_insn = curr_insn;
5638 before_p = (JUMP_P (curr_insn)
5639 || (CALL_P (curr_insn) && reg->type == OP_IN));
5640 if (NONDEBUG_INSN_P (curr_insn)
5641 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5642 && split_if_necessary (src_regno, reg->biggest_mode,
5643 potential_reload_hard_regs,
5644 before_p, curr_insn, max_uid))
5646 if (reg->subreg_p)
5647 lra_risky_transformations_p = true;
5648 change_p = true;
5649 /* Invalidate. */
5650 usage_insns[src_regno].check = 0;
5651 if (before_p)
5652 use_insn = PREV_INSN (curr_insn);
5654 if (NONDEBUG_INSN_P (curr_insn))
5656 if (src_regno < FIRST_PSEUDO_REGISTER)
5657 add_to_hard_reg_set (&live_hard_regs,
5658 reg->biggest_mode, src_regno);
5659 else
5660 add_to_hard_reg_set (&live_hard_regs,
5661 PSEUDO_REGNO_MODE (src_regno),
5662 reg_renumber[src_regno]);
5664 add_next_usage_insn (src_regno, use_insn, reloads_num);
5667 /* Process used call regs. */
5668 if (curr_id->arg_hard_regs != NULL)
5669 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5670 if (src_regno < FIRST_PSEUDO_REGISTER)
5672 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5673 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5675 for (i = 0; i < to_inherit_num; i++)
5677 src_regno = to_inherit[i].regno;
5678 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5679 curr_insn, to_inherit[i].insns))
5680 change_p = true;
5681 else
5682 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5685 if (update_reloads_num_p
5686 && NONDEBUG_INSN_P (curr_insn)
5687 && (set = single_set (curr_insn)) != NULL_RTX)
5689 int regno = -1;
5690 if ((REG_P (SET_DEST (set))
5691 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5692 && reg_renumber[regno] < 0
5693 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5694 || (REG_P (SET_SRC (set))
5695 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5696 && reg_renumber[regno] < 0
5697 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5699 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5700 reloads_num++;
5701 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5702 IOR_HARD_REG_SET (potential_reload_hard_regs,
5703 reg_class_contents[cl]);
5706 /* We reached the start of the current basic block. */
5707 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5708 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5710 /* We reached the beginning of the current block -- do
5711 rest of spliting in the current BB. */
5712 to_process = df_get_live_in (curr_bb);
5713 if (BLOCK_FOR_INSN (head) != curr_bb)
5715 /* We are somewhere in the middle of EBB. */
5716 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5717 curr_bb, &temp_bitmap);
5718 to_process = &temp_bitmap;
5720 head_p = true;
5721 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5723 if ((int) j >= lra_constraint_new_regno_start)
5724 break;
5725 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5726 && usage_insns[j].check == curr_usage_insns_check
5727 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5729 if (need_for_split_p (potential_reload_hard_regs, j))
5731 if (lra_dump_file != NULL && head_p)
5733 fprintf (lra_dump_file,
5734 " ----------------------------------\n");
5735 head_p = false;
5737 if (split_reg (false, j, bb_note (curr_bb),
5738 next_usage_insns))
5739 change_p = true;
5741 usage_insns[j].check = 0;
5746 return change_p;
5749 /* This value affects EBB forming. If probability of edge from EBB to
5750 a BB is not greater than the following value, we don't add the BB
5751 to EBB. */
5752 #define EBB_PROBABILITY_CUTOFF \
5753 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
5755 /* Current number of inheritance/split iteration. */
5756 int lra_inheritance_iter;
5758 /* Entry function for inheritance/split pass. */
5759 void
5760 lra_inheritance (void)
5762 int i;
5763 basic_block bb, start_bb;
5764 edge e;
5766 lra_inheritance_iter++;
5767 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5768 return;
5769 timevar_push (TV_LRA_INHERITANCE);
5770 if (lra_dump_file != NULL)
5771 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5772 lra_inheritance_iter);
5773 curr_usage_insns_check = 0;
5774 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5775 for (i = 0; i < lra_constraint_new_regno_start; i++)
5776 usage_insns[i].check = 0;
5777 bitmap_initialize (&check_only_regs, &reg_obstack);
5778 bitmap_initialize (&live_regs, &reg_obstack);
5779 bitmap_initialize (&temp_bitmap, &reg_obstack);
5780 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5781 FOR_EACH_BB_FN (bb, cfun)
5783 start_bb = bb;
5784 if (lra_dump_file != NULL)
5785 fprintf (lra_dump_file, "EBB");
5786 /* Form a EBB starting with BB. */
5787 bitmap_clear (&ebb_global_regs);
5788 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5789 for (;;)
5791 if (lra_dump_file != NULL)
5792 fprintf (lra_dump_file, " %d", bb->index);
5793 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5794 || LABEL_P (BB_HEAD (bb->next_bb)))
5795 break;
5796 e = find_fallthru_edge (bb->succs);
5797 if (! e)
5798 break;
5799 if (e->probability < EBB_PROBABILITY_CUTOFF)
5800 break;
5801 bb = bb->next_bb;
5803 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5804 if (lra_dump_file != NULL)
5805 fprintf (lra_dump_file, "\n");
5806 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5807 /* Remember that the EBB head and tail can change in
5808 inherit_in_ebb. */
5809 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5811 bitmap_clear (&ebb_global_regs);
5812 bitmap_clear (&temp_bitmap);
5813 bitmap_clear (&live_regs);
5814 bitmap_clear (&check_only_regs);
5815 free (usage_insns);
5817 timevar_pop (TV_LRA_INHERITANCE);
5822 /* This page contains code to undo failed inheritance/split
5823 transformations. */
5825 /* Current number of iteration undoing inheritance/split. */
5826 int lra_undo_inheritance_iter;
5828 /* Fix BB live info LIVE after removing pseudos created on pass doing
5829 inheritance/split which are REMOVED_PSEUDOS. */
5830 static void
5831 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5833 unsigned int regno;
5834 bitmap_iterator bi;
5836 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5837 if (bitmap_clear_bit (live, regno))
5838 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5841 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5842 number. */
5843 static int
5844 get_regno (rtx reg)
5846 if (GET_CODE (reg) == SUBREG)
5847 reg = SUBREG_REG (reg);
5848 if (REG_P (reg))
5849 return REGNO (reg);
5850 return -1;
5853 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5854 return true if we did any change. The undo transformations for
5855 inheritance looks like
5856 i <- i2
5857 p <- i => p <- i2
5858 or removing
5859 p <- i, i <- p, and i <- i3
5860 where p is original pseudo from which inheritance pseudo i was
5861 created, i and i3 are removed inheritance pseudos, i2 is another
5862 not removed inheritance pseudo. All split pseudos or other
5863 occurrences of removed inheritance pseudos are changed on the
5864 corresponding original pseudos.
5866 The function also schedules insns changed and created during
5867 inheritance/split pass for processing by the subsequent constraint
5868 pass. */
5869 static bool
5870 remove_inheritance_pseudos (bitmap remove_pseudos)
5872 basic_block bb;
5873 int regno, sregno, prev_sregno, dregno, restore_regno;
5874 rtx set, prev_set;
5875 rtx_insn *prev_insn;
5876 bool change_p, done_p;
5878 change_p = ! bitmap_empty_p (remove_pseudos);
5879 /* We can not finish the function right away if CHANGE_P is true
5880 because we need to marks insns affected by previous
5881 inheritance/split pass for processing by the subsequent
5882 constraint pass. */
5883 FOR_EACH_BB_FN (bb, cfun)
5885 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5886 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5887 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5889 if (! INSN_P (curr_insn))
5890 continue;
5891 done_p = false;
5892 sregno = dregno = -1;
5893 if (change_p && NONDEBUG_INSN_P (curr_insn)
5894 && (set = single_set (curr_insn)) != NULL_RTX)
5896 dregno = get_regno (SET_DEST (set));
5897 sregno = get_regno (SET_SRC (set));
5900 if (sregno >= 0 && dregno >= 0)
5902 if ((bitmap_bit_p (remove_pseudos, sregno)
5903 && (lra_reg_info[sregno].restore_regno == dregno
5904 || (bitmap_bit_p (remove_pseudos, dregno)
5905 && (lra_reg_info[sregno].restore_regno
5906 == lra_reg_info[dregno].restore_regno))))
5907 || (bitmap_bit_p (remove_pseudos, dregno)
5908 && lra_reg_info[dregno].restore_regno == sregno))
5909 /* One of the following cases:
5910 original <- removed inheritance pseudo
5911 removed inherit pseudo <- another removed inherit pseudo
5912 removed inherit pseudo <- original pseudo
5914 removed_split_pseudo <- original_reg
5915 original_reg <- removed_split_pseudo */
5917 if (lra_dump_file != NULL)
5919 fprintf (lra_dump_file, " Removing %s:\n",
5920 bitmap_bit_p (&lra_split_regs, sregno)
5921 || bitmap_bit_p (&lra_split_regs, dregno)
5922 ? "split" : "inheritance");
5923 dump_insn_slim (lra_dump_file, curr_insn);
5925 lra_set_insn_deleted (curr_insn);
5926 done_p = true;
5928 else if (bitmap_bit_p (remove_pseudos, sregno)
5929 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5931 /* Search the following pattern:
5932 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5933 original_pseudo <- inherit_or_split_pseudo1
5934 where the 2nd insn is the current insn and
5935 inherit_or_split_pseudo2 is not removed. If it is found,
5936 change the current insn onto:
5937 original_pseudo <- inherit_or_split_pseudo2. */
5938 for (prev_insn = PREV_INSN (curr_insn);
5939 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5940 prev_insn = PREV_INSN (prev_insn))
5942 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5943 && (prev_set = single_set (prev_insn)) != NULL_RTX
5944 /* There should be no subregs in insn we are
5945 searching because only the original reg might
5946 be in subreg when we changed the mode of
5947 load/store for splitting. */
5948 && REG_P (SET_DEST (prev_set))
5949 && REG_P (SET_SRC (prev_set))
5950 && (int) REGNO (SET_DEST (prev_set)) == sregno
5951 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5952 >= FIRST_PSEUDO_REGISTER)
5953 /* As we consider chain of inheritance or
5954 splitting described in above comment we should
5955 check that sregno and prev_sregno were
5956 inheritance/split pseudos created from the
5957 same original regno. */
5958 && (lra_reg_info[sregno].restore_regno
5959 == lra_reg_info[prev_sregno].restore_regno)
5960 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5962 lra_assert (GET_MODE (SET_SRC (prev_set))
5963 == GET_MODE (regno_reg_rtx[sregno]));
5964 if (GET_CODE (SET_SRC (set)) == SUBREG)
5965 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5966 else
5967 SET_SRC (set) = SET_SRC (prev_set);
5968 /* As we are finishing with processing the insn
5969 here, check the destination too as it might
5970 inheritance pseudo for another pseudo. */
5971 if (bitmap_bit_p (remove_pseudos, dregno)
5972 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
5973 && (restore_regno
5974 = lra_reg_info[dregno].restore_regno) >= 0)
5976 if (GET_CODE (SET_DEST (set)) == SUBREG)
5977 SUBREG_REG (SET_DEST (set))
5978 = regno_reg_rtx[restore_regno];
5979 else
5980 SET_DEST (set) = regno_reg_rtx[restore_regno];
5982 lra_push_insn_and_update_insn_regno_info (curr_insn);
5983 lra_set_used_insn_alternative_by_uid
5984 (INSN_UID (curr_insn), -1);
5985 done_p = true;
5986 if (lra_dump_file != NULL)
5988 fprintf (lra_dump_file, " Change reload insn:\n");
5989 dump_insn_slim (lra_dump_file, curr_insn);
5994 if (! done_p)
5996 struct lra_insn_reg *reg;
5997 bool restored_regs_p = false;
5998 bool kept_regs_p = false;
6000 curr_id = lra_get_insn_recog_data (curr_insn);
6001 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6003 regno = reg->regno;
6004 restore_regno = lra_reg_info[regno].restore_regno;
6005 if (restore_regno >= 0)
6007 if (change_p && bitmap_bit_p (remove_pseudos, regno))
6009 lra_substitute_pseudo_within_insn
6010 (curr_insn, regno, regno_reg_rtx[restore_regno],
6011 false);
6012 restored_regs_p = true;
6014 else
6015 kept_regs_p = true;
6018 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
6020 /* The instruction has changed since the previous
6021 constraints pass. */
6022 lra_push_insn_and_update_insn_regno_info (curr_insn);
6023 lra_set_used_insn_alternative_by_uid
6024 (INSN_UID (curr_insn), -1);
6026 else if (restored_regs_p)
6027 /* The instruction has been restored to the form that
6028 it had during the previous constraints pass. */
6029 lra_update_insn_regno_info (curr_insn);
6030 if (restored_regs_p && lra_dump_file != NULL)
6032 fprintf (lra_dump_file, " Insn after restoring regs:\n");
6033 dump_insn_slim (lra_dump_file, curr_insn);
6038 return change_p;
6041 /* If optional reload pseudos failed to get a hard register or was not
6042 inherited, it is better to remove optional reloads. We do this
6043 transformation after undoing inheritance to figure out necessity to
6044 remove optional reloads easier. Return true if we do any
6045 change. */
6046 static bool
6047 undo_optional_reloads (void)
6049 bool change_p, keep_p;
6050 unsigned int regno, uid;
6051 bitmap_iterator bi, bi2;
6052 rtx_insn *insn;
6053 rtx set, src, dest;
6054 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
6056 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
6057 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
6058 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6060 keep_p = false;
6061 /* Keep optional reloads from previous subpasses. */
6062 if (lra_reg_info[regno].restore_regno < 0
6063 /* If the original pseudo changed its allocation, just
6064 removing the optional pseudo is dangerous as the original
6065 pseudo will have longer live range. */
6066 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
6067 keep_p = true;
6068 else if (reg_renumber[regno] >= 0)
6069 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
6071 insn = lra_insn_recog_data[uid]->insn;
6072 if ((set = single_set (insn)) == NULL_RTX)
6073 continue;
6074 src = SET_SRC (set);
6075 dest = SET_DEST (set);
6076 if (! REG_P (src) || ! REG_P (dest))
6077 continue;
6078 if (REGNO (dest) == regno
6079 /* Ignore insn for optional reloads itself. */
6080 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
6081 /* Check only inheritance on last inheritance pass. */
6082 && (int) REGNO (src) >= new_regno_start
6083 /* Check that the optional reload was inherited. */
6084 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6086 keep_p = true;
6087 break;
6090 if (keep_p)
6092 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6093 if (lra_dump_file != NULL)
6094 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6097 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6098 bitmap_initialize (&insn_bitmap, &reg_obstack);
6099 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6101 if (lra_dump_file != NULL)
6102 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6103 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6104 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6106 insn = lra_insn_recog_data[uid]->insn;
6107 if ((set = single_set (insn)) != NULL_RTX)
6109 src = SET_SRC (set);
6110 dest = SET_DEST (set);
6111 if (REG_P (src) && REG_P (dest)
6112 && ((REGNO (src) == regno
6113 && (lra_reg_info[regno].restore_regno
6114 == (int) REGNO (dest)))
6115 || (REGNO (dest) == regno
6116 && (lra_reg_info[regno].restore_regno
6117 == (int) REGNO (src)))))
6119 if (lra_dump_file != NULL)
6121 fprintf (lra_dump_file, " Deleting move %u\n",
6122 INSN_UID (insn));
6123 dump_insn_slim (lra_dump_file, insn);
6125 lra_set_insn_deleted (insn);
6126 continue;
6128 /* We should not worry about generation memory-memory
6129 moves here as if the corresponding inheritance did
6130 not work (inheritance pseudo did not get a hard reg),
6131 we remove the inheritance pseudo and the optional
6132 reload. */
6134 lra_substitute_pseudo_within_insn
6135 (insn, regno, regno_reg_rtx[lra_reg_info[regno].restore_regno],
6136 false);
6137 lra_update_insn_regno_info (insn);
6138 if (lra_dump_file != NULL)
6140 fprintf (lra_dump_file,
6141 " Restoring original insn:\n");
6142 dump_insn_slim (lra_dump_file, insn);
6146 /* Clear restore_regnos. */
6147 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6148 lra_reg_info[regno].restore_regno = -1;
6149 bitmap_clear (&insn_bitmap);
6150 bitmap_clear (&removed_optional_reload_pseudos);
6151 return change_p;
6154 /* Entry function for undoing inheritance/split transformation. Return true
6155 if we did any RTL change in this pass. */
6156 bool
6157 lra_undo_inheritance (void)
6159 unsigned int regno;
6160 int restore_regno, hard_regno;
6161 int n_all_inherit, n_inherit, n_all_split, n_split;
6162 bitmap_head remove_pseudos;
6163 bitmap_iterator bi;
6164 bool change_p;
6166 lra_undo_inheritance_iter++;
6167 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6168 return false;
6169 if (lra_dump_file != NULL)
6170 fprintf (lra_dump_file,
6171 "\n********** Undoing inheritance #%d: **********\n\n",
6172 lra_undo_inheritance_iter);
6173 bitmap_initialize (&remove_pseudos, &reg_obstack);
6174 n_inherit = n_all_inherit = 0;
6175 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6176 if (lra_reg_info[regno].restore_regno >= 0)
6178 n_all_inherit++;
6179 if (reg_renumber[regno] < 0
6180 /* If the original pseudo changed its allocation, just
6181 removing inheritance is dangerous as for changing
6182 allocation we used shorter live-ranges. */
6183 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6184 bitmap_set_bit (&remove_pseudos, regno);
6185 else
6186 n_inherit++;
6188 if (lra_dump_file != NULL && n_all_inherit != 0)
6189 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6190 n_inherit, n_all_inherit,
6191 (double) n_inherit / n_all_inherit * 100);
6192 n_split = n_all_split = 0;
6193 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6194 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6196 n_all_split++;
6197 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6198 ? reg_renumber[restore_regno] : restore_regno);
6199 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6200 bitmap_set_bit (&remove_pseudos, regno);
6201 else
6203 n_split++;
6204 if (lra_dump_file != NULL)
6205 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6206 regno, restore_regno);
6209 if (lra_dump_file != NULL && n_all_split != 0)
6210 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6211 n_split, n_all_split,
6212 (double) n_split / n_all_split * 100);
6213 change_p = remove_inheritance_pseudos (&remove_pseudos);
6214 bitmap_clear (&remove_pseudos);
6215 /* Clear restore_regnos. */
6216 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6217 lra_reg_info[regno].restore_regno = -1;
6218 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6219 lra_reg_info[regno].restore_regno = -1;
6220 change_p = undo_optional_reloads () || change_p;
6221 return change_p;