[gcc]
[official-gcc.git] / gcc / lra-constraints.c
blob45b65069e68729e53af3181ceec60134c52f9437
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, considering
875 output operands OUTS (similar array to INS) needing to be in different
876 registers. Add input and output reloads correspondingly to the lists
877 *BEFORE and *AFTER. OUT might be negative. In this case we generate
878 input reloads for matched input operands INS. EARLY_CLOBBER_P is a flag
879 that the output operand is early clobbered for chosen alternative. */
880 static void
881 match_reload (signed char out, signed char *ins, signed char *outs,
882 enum reg_class goal_class, rtx_insn **before,
883 rtx_insn **after, bool early_clobber_p)
885 bool out_conflict;
886 int i, in;
887 rtx new_in_reg, new_out_reg, reg;
888 machine_mode inmode, outmode;
889 rtx in_rtx = *curr_id->operand_loc[ins[0]];
890 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
892 inmode = curr_operand_mode[ins[0]];
893 outmode = out < 0 ? inmode : curr_operand_mode[out];
894 push_to_sequence (*before);
895 if (inmode != outmode)
897 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
899 reg = new_in_reg
900 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
901 goal_class, "");
902 if (SCALAR_INT_MODE_P (inmode))
903 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
904 else
905 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
906 LRA_SUBREG_P (new_out_reg) = 1;
907 /* If the input reg is dying here, we can use the same hard
908 register for REG and IN_RTX. We do it only for original
909 pseudos as reload pseudos can die although original
910 pseudos still live where reload pseudos dies. */
911 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
912 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
913 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
915 else
917 reg = new_out_reg
918 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
919 goal_class, "");
920 if (SCALAR_INT_MODE_P (outmode))
921 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
922 else
923 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
924 /* NEW_IN_REG is non-paradoxical subreg. We don't want
925 NEW_OUT_REG living above. We add clobber clause for
926 this. This is just a temporary clobber. We can remove
927 it at the end of LRA work. */
928 rtx_insn *clobber = emit_clobber (new_out_reg);
929 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
930 LRA_SUBREG_P (new_in_reg) = 1;
931 if (GET_CODE (in_rtx) == SUBREG)
933 rtx subreg_reg = SUBREG_REG (in_rtx);
935 /* If SUBREG_REG is dying here and sub-registers IN_RTX
936 and NEW_IN_REG are similar, we can use the same hard
937 register for REG and SUBREG_REG. */
938 if (REG_P (subreg_reg)
939 && (int) REGNO (subreg_reg) < lra_new_regno_start
940 && GET_MODE (subreg_reg) == outmode
941 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
942 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
943 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
947 else
949 /* Pseudos have values -- see comments for lra_reg_info.
950 Different pseudos with the same value do not conflict even if
951 they live in the same place. When we create a pseudo we
952 assign value of original pseudo (if any) from which we
953 created the new pseudo. If we create the pseudo from the
954 input pseudo, the new pseudo will have no conflict with the
955 input pseudo which is wrong when the input pseudo lives after
956 the insn and as the new pseudo value is changed by the insn
957 output. Therefore we create the new pseudo from the output
958 except the case when we have single matched dying input
959 pseudo.
961 We cannot reuse the current output register because we might
962 have a situation like "a <- a op b", where the constraints
963 force the second input operand ("b") to match the output
964 operand ("a"). "b" must then be copied into a new register
965 so that it doesn't clobber the current value of "a".
967 We can not use the same value if the output pseudo is
968 early clobbered or the input pseudo is mentioned in the
969 output, e.g. as an address part in memory, because
970 output reload will actually extend the pseudo liveness.
971 We don't care about eliminable hard regs here as we are
972 interesting only in pseudos. */
974 /* Matching input's register value is the same as one of the other
975 output operand. Output operands in a parallel insn must be in
976 different registers. */
977 out_conflict = false;
978 if (REG_P (in_rtx))
980 for (i = 0; outs[i] >= 0; i++)
982 rtx other_out_rtx = *curr_id->operand_loc[outs[i]];
983 if (REG_P (other_out_rtx)
984 && (regno_val_use_in (REGNO (in_rtx), other_out_rtx)
985 != NULL_RTX))
987 out_conflict = true;
988 break;
993 new_in_reg = new_out_reg
994 = (! early_clobber_p && ins[1] < 0 && REG_P (in_rtx)
995 && (int) REGNO (in_rtx) < lra_new_regno_start
996 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
997 && (out < 0
998 || regno_val_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
999 && !out_conflict
1000 ? lra_create_new_reg (inmode, in_rtx, goal_class, "")
1001 : lra_create_new_reg_with_unique_value (outmode, out_rtx,
1002 goal_class, ""));
1004 /* In operand can be got from transformations before processing insn
1005 constraints. One example of such transformations is subreg
1006 reloading (see function simplify_operand_subreg). The new
1007 pseudos created by the transformations might have inaccurate
1008 class (ALL_REGS) and we should make their classes more
1009 accurate. */
1010 narrow_reload_pseudo_class (in_rtx, goal_class);
1011 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
1012 *before = get_insns ();
1013 end_sequence ();
1014 for (i = 0; (in = ins[i]) >= 0; i++)
1016 lra_assert
1017 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
1018 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
1019 *curr_id->operand_loc[in] = new_in_reg;
1021 lra_update_dups (curr_id, ins);
1022 if (out < 0)
1023 return;
1024 /* See a comment for the input operand above. */
1025 narrow_reload_pseudo_class (out_rtx, goal_class);
1026 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
1028 start_sequence ();
1029 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
1030 emit_insn (*after);
1031 *after = get_insns ();
1032 end_sequence ();
1034 *curr_id->operand_loc[out] = new_out_reg;
1035 lra_update_dup (curr_id, out);
1038 /* Return register class which is union of all reg classes in insn
1039 constraint alternative string starting with P. */
1040 static enum reg_class
1041 reg_class_from_constraints (const char *p)
1043 int c, len;
1044 enum reg_class op_class = NO_REGS;
1047 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1049 case '#':
1050 case ',':
1051 return op_class;
1053 case 'g':
1054 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1055 break;
1057 default:
1058 enum constraint_num cn = lookup_constraint (p);
1059 enum reg_class cl = reg_class_for_constraint (cn);
1060 if (cl == NO_REGS)
1062 if (insn_extra_address_constraint (cn))
1063 op_class
1064 = (reg_class_subunion
1065 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1066 ADDRESS, SCRATCH)]);
1067 break;
1070 op_class = reg_class_subunion[op_class][cl];
1071 break;
1073 while ((p += len), c);
1074 return op_class;
1077 /* If OP is a register, return the class of the register as per
1078 get_reg_class, otherwise return NO_REGS. */
1079 static inline enum reg_class
1080 get_op_class (rtx op)
1082 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1085 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1086 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1087 SUBREG for VAL to make them equal. */
1088 static rtx_insn *
1089 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1091 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1093 /* Usually size of mem_pseudo is greater than val size but in
1094 rare cases it can be less as it can be defined by target
1095 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1096 if (! MEM_P (val))
1098 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1099 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1101 LRA_SUBREG_P (val) = 1;
1103 else
1105 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1106 LRA_SUBREG_P (mem_pseudo) = 1;
1109 return to_p ? gen_move_insn (mem_pseudo, val)
1110 : gen_move_insn (val, mem_pseudo);
1113 /* Process a special case insn (register move), return true if we
1114 don't need to process it anymore. INSN should be a single set
1115 insn. Set up that RTL was changed through CHANGE_P and macro
1116 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1117 SEC_MEM_P. */
1118 static bool
1119 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1121 int sregno, dregno;
1122 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1123 rtx_insn *before;
1124 enum reg_class dclass, sclass, secondary_class;
1125 secondary_reload_info sri;
1127 lra_assert (curr_insn_set != NULL_RTX);
1128 dreg = dest = SET_DEST (curr_insn_set);
1129 sreg = src = SET_SRC (curr_insn_set);
1130 if (GET_CODE (dest) == SUBREG)
1131 dreg = SUBREG_REG (dest);
1132 if (GET_CODE (src) == SUBREG)
1133 sreg = SUBREG_REG (src);
1134 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1135 return false;
1136 sclass = dclass = NO_REGS;
1137 if (REG_P (dreg))
1138 dclass = get_reg_class (REGNO (dreg));
1139 if (dclass == ALL_REGS)
1140 /* ALL_REGS is used for new pseudos created by transformations
1141 like reload of SUBREG_REG (see function
1142 simplify_operand_subreg). We don't know their class yet. We
1143 should figure out the class from processing the insn
1144 constraints not in this fast path function. Even if ALL_REGS
1145 were a right class for the pseudo, secondary_... hooks usually
1146 are not define for ALL_REGS. */
1147 return false;
1148 if (REG_P (sreg))
1149 sclass = get_reg_class (REGNO (sreg));
1150 if (sclass == ALL_REGS)
1151 /* See comments above. */
1152 return false;
1153 if (sclass == NO_REGS && dclass == NO_REGS)
1154 return false;
1155 #ifdef SECONDARY_MEMORY_NEEDED
1156 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1157 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1158 && ((sclass != NO_REGS && dclass != NO_REGS)
1159 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1160 #endif
1163 *sec_mem_p = true;
1164 return false;
1166 #endif
1167 if (! REG_P (dreg) || ! REG_P (sreg))
1168 return false;
1169 sri.prev_sri = NULL;
1170 sri.icode = CODE_FOR_nothing;
1171 sri.extra_cost = 0;
1172 secondary_class = NO_REGS;
1173 /* Set up hard register for a reload pseudo for hook
1174 secondary_reload because some targets just ignore unassigned
1175 pseudos in the hook. */
1176 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1178 dregno = REGNO (dreg);
1179 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1181 else
1182 dregno = -1;
1183 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1185 sregno = REGNO (sreg);
1186 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1188 else
1189 sregno = -1;
1190 if (sclass != NO_REGS)
1191 secondary_class
1192 = (enum reg_class) targetm.secondary_reload (false, dest,
1193 (reg_class_t) sclass,
1194 GET_MODE (src), &sri);
1195 if (sclass == NO_REGS
1196 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1197 && dclass != NO_REGS))
1199 enum reg_class old_sclass = secondary_class;
1200 secondary_reload_info old_sri = sri;
1202 sri.prev_sri = NULL;
1203 sri.icode = CODE_FOR_nothing;
1204 sri.extra_cost = 0;
1205 secondary_class
1206 = (enum reg_class) targetm.secondary_reload (true, src,
1207 (reg_class_t) dclass,
1208 GET_MODE (src), &sri);
1209 /* Check the target hook consistency. */
1210 lra_assert
1211 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1212 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1213 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1215 if (sregno >= 0)
1216 reg_renumber [sregno] = -1;
1217 if (dregno >= 0)
1218 reg_renumber [dregno] = -1;
1219 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1220 return false;
1221 *change_p = true;
1222 new_reg = NULL_RTX;
1223 if (secondary_class != NO_REGS)
1224 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1225 secondary_class,
1226 "secondary");
1227 start_sequence ();
1228 if (sri.icode == CODE_FOR_nothing)
1229 lra_emit_move (new_reg, src);
1230 else
1232 enum reg_class scratch_class;
1234 scratch_class = (reg_class_from_constraints
1235 (insn_data[sri.icode].operand[2].constraint));
1236 scratch_reg = (lra_create_new_reg_with_unique_value
1237 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1238 scratch_class, "scratch"));
1239 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1240 src, scratch_reg));
1242 before = get_insns ();
1243 end_sequence ();
1244 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1245 if (new_reg != NULL_RTX)
1246 SET_SRC (curr_insn_set) = new_reg;
1247 else
1249 if (lra_dump_file != NULL)
1251 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1252 dump_insn_slim (lra_dump_file, curr_insn);
1254 lra_set_insn_deleted (curr_insn);
1255 return true;
1257 return false;
1260 /* The following data describe the result of process_alt_operands.
1261 The data are used in curr_insn_transform to generate reloads. */
1263 /* The chosen reg classes which should be used for the corresponding
1264 operands. */
1265 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1266 /* True if the operand should be the same as another operand and that
1267 other operand does not need a reload. */
1268 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1269 /* True if the operand does not need a reload. */
1270 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1271 /* True if the operand can be offsetable memory. */
1272 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1273 /* The number of an operand to which given operand can be matched to. */
1274 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1275 /* The number of elements in the following array. */
1276 static int goal_alt_dont_inherit_ops_num;
1277 /* Numbers of operands whose reload pseudos should not be inherited. */
1278 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1279 /* True if the insn commutative operands should be swapped. */
1280 static bool goal_alt_swapped;
1281 /* The chosen insn alternative. */
1282 static int goal_alt_number;
1284 /* True if the corresponding operand is the result of an equivalence
1285 substitution. */
1286 static bool equiv_substition_p[MAX_RECOG_OPERANDS];
1288 /* The following five variables are used to choose the best insn
1289 alternative. They reflect final characteristics of the best
1290 alternative. */
1292 /* Number of necessary reloads and overall cost reflecting the
1293 previous value and other unpleasantness of the best alternative. */
1294 static int best_losers, best_overall;
1295 /* Overall number hard registers used for reloads. For example, on
1296 some targets we need 2 general registers to reload DFmode and only
1297 one floating point register. */
1298 static int best_reload_nregs;
1299 /* Overall number reflecting distances of previous reloading the same
1300 value. The distances are counted from the current BB start. It is
1301 used to improve inheritance chances. */
1302 static int best_reload_sum;
1304 /* True if the current insn should have no correspondingly input or
1305 output reloads. */
1306 static bool no_input_reloads_p, no_output_reloads_p;
1308 /* True if we swapped the commutative operands in the current
1309 insn. */
1310 static int curr_swapped;
1312 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1313 register of class CL. Add any input reloads to list BEFORE. AFTER
1314 is nonnull if *LOC is an automodified value; handle that case by
1315 adding the required output reloads to list AFTER. Return true if
1316 the RTL was changed.
1318 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1319 register. Return false if the address register is correct. */
1320 static bool
1321 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1322 enum reg_class cl)
1324 int regno;
1325 enum reg_class rclass, new_class;
1326 rtx reg;
1327 rtx new_reg;
1328 machine_mode mode;
1329 bool subreg_p, before_p = false;
1331 subreg_p = GET_CODE (*loc) == SUBREG;
1332 if (subreg_p)
1334 reg = SUBREG_REG (*loc);
1335 mode = GET_MODE (reg);
1337 /* For mode with size bigger than ptr_mode, there unlikely to be "mov"
1338 between two registers with different classes, but there normally will
1339 be "mov" which transfers element of vector register into the general
1340 register, and this normally will be a subreg which should be reloaded
1341 as a whole. This is particularly likely to be triggered when
1342 -fno-split-wide-types specified. */
1343 if (!REG_P (reg)
1344 || in_class_p (reg, cl, &new_class)
1345 || GET_MODE_SIZE (mode) <= GET_MODE_SIZE (ptr_mode))
1346 loc = &SUBREG_REG (*loc);
1349 reg = *loc;
1350 mode = GET_MODE (reg);
1351 if (! REG_P (reg))
1353 if (check_only_p)
1354 return true;
1355 /* Always reload memory in an address even if the target supports
1356 such addresses. */
1357 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1358 before_p = true;
1360 else
1362 regno = REGNO (reg);
1363 rclass = get_reg_class (regno);
1364 if (! check_only_p
1365 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1367 if (lra_dump_file != NULL)
1369 fprintf (lra_dump_file,
1370 "Changing pseudo %d in address of insn %u on equiv ",
1371 REGNO (reg), INSN_UID (curr_insn));
1372 dump_value_slim (lra_dump_file, *loc, 1);
1373 fprintf (lra_dump_file, "\n");
1375 *loc = copy_rtx (*loc);
1377 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1379 if (check_only_p)
1380 return true;
1381 reg = *loc;
1382 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1383 mode, reg, cl, subreg_p, "address", &new_reg))
1384 before_p = true;
1386 else if (new_class != NO_REGS && rclass != new_class)
1388 if (check_only_p)
1389 return true;
1390 lra_change_class (regno, new_class, " Change to", true);
1391 return false;
1393 else
1394 return false;
1396 if (before_p)
1398 push_to_sequence (*before);
1399 lra_emit_move (new_reg, reg);
1400 *before = get_insns ();
1401 end_sequence ();
1403 *loc = new_reg;
1404 if (after != NULL)
1406 start_sequence ();
1407 lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1408 emit_insn (*after);
1409 *after = get_insns ();
1410 end_sequence ();
1412 return true;
1415 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1416 the insn to be inserted before curr insn. AFTER returns the
1417 the insn to be inserted after curr insn. ORIGREG and NEWREG
1418 are the original reg and new reg for reload. */
1419 static void
1420 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1421 rtx newreg)
1423 if (before)
1425 push_to_sequence (*before);
1426 lra_emit_move (newreg, origreg);
1427 *before = get_insns ();
1428 end_sequence ();
1430 if (after)
1432 start_sequence ();
1433 lra_emit_move (origreg, newreg);
1434 emit_insn (*after);
1435 *after = get_insns ();
1436 end_sequence ();
1440 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1442 /* Make reloads for subreg in operand NOP with internal subreg mode
1443 REG_MODE, add new reloads for further processing. Return true if
1444 any change was done. */
1445 static bool
1446 simplify_operand_subreg (int nop, machine_mode reg_mode)
1448 int hard_regno;
1449 rtx_insn *before, *after;
1450 machine_mode mode, innermode;
1451 rtx reg, new_reg;
1452 rtx operand = *curr_id->operand_loc[nop];
1453 enum reg_class regclass;
1454 enum op_type type;
1456 before = after = NULL;
1458 if (GET_CODE (operand) != SUBREG)
1459 return false;
1461 mode = GET_MODE (operand);
1462 reg = SUBREG_REG (operand);
1463 innermode = GET_MODE (reg);
1464 type = curr_static_id->operand[nop].type;
1465 /* If we change address for paradoxical subreg of memory, the
1466 address might violate the necessary alignment or the access might
1467 be slow. So take this into consideration. We should not worry
1468 about access beyond allocated memory for paradoxical memory
1469 subregs as we don't substitute such equiv memory (see processing
1470 equivalences in function lra_constraints) and because for spilled
1471 pseudos we allocate stack memory enough for the biggest
1472 corresponding paradoxical subreg. */
1473 if (MEM_P (reg)
1474 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1475 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1477 rtx subst, old = *curr_id->operand_loc[nop];
1479 alter_subreg (curr_id->operand_loc[nop], false);
1480 subst = *curr_id->operand_loc[nop];
1481 lra_assert (MEM_P (subst));
1482 if (! valid_address_p (innermode, XEXP (reg, 0),
1483 MEM_ADDR_SPACE (reg))
1484 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1485 MEM_ADDR_SPACE (subst)))
1486 return true;
1487 else if ((get_constraint_type (lookup_constraint
1488 (curr_static_id->operand[nop].constraint))
1489 != CT_SPECIAL_MEMORY)
1490 /* We still can reload address and if the address is
1491 valid, we can remove subreg without reloading its
1492 inner memory. */
1493 && valid_address_p (GET_MODE (subst),
1494 regno_reg_rtx
1495 [ira_class_hard_regs
1496 [base_reg_class (GET_MODE (subst),
1497 MEM_ADDR_SPACE (subst),
1498 ADDRESS, SCRATCH)][0]],
1499 MEM_ADDR_SPACE (subst)))
1500 return true;
1502 /* If the address was valid and became invalid, prefer to reload
1503 the memory. Typical case is when the index scale should
1504 correspond the memory. */
1505 *curr_id->operand_loc[nop] = old;
1507 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1509 alter_subreg (curr_id->operand_loc[nop], false);
1510 return true;
1512 else if (CONSTANT_P (reg))
1514 /* Try to simplify subreg of constant. It is usually result of
1515 equivalence substitution. */
1516 if (innermode == VOIDmode
1517 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1518 innermode = curr_static_id->operand[nop].mode;
1519 if ((new_reg = simplify_subreg (mode, reg, innermode,
1520 SUBREG_BYTE (operand))) != NULL_RTX)
1522 *curr_id->operand_loc[nop] = new_reg;
1523 return true;
1526 /* Put constant into memory when we have mixed modes. It generates
1527 a better code in most cases as it does not need a secondary
1528 reload memory. It also prevents LRA looping when LRA is using
1529 secondary reload memory again and again. */
1530 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1531 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1533 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1534 alter_subreg (curr_id->operand_loc[nop], false);
1535 return true;
1537 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1538 if there may be a problem accessing OPERAND in the outer
1539 mode. */
1540 if ((REG_P (reg)
1541 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1542 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1543 /* Don't reload paradoxical subregs because we could be looping
1544 having repeatedly final regno out of hard regs range. */
1545 && (hard_regno_nregs[hard_regno][innermode]
1546 >= hard_regno_nregs[hard_regno][mode])
1547 && simplify_subreg_regno (hard_regno, innermode,
1548 SUBREG_BYTE (operand), mode) < 0
1549 /* Don't reload subreg for matching reload. It is actually
1550 valid subreg in LRA. */
1551 && ! LRA_SUBREG_P (operand))
1552 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1554 enum reg_class rclass;
1556 if (REG_P (reg))
1557 /* There is a big probability that we will get the same class
1558 for the new pseudo and we will get the same insn which
1559 means infinite looping. So spill the new pseudo. */
1560 rclass = NO_REGS;
1561 else
1562 /* The class will be defined later in curr_insn_transform. */
1563 rclass
1564 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1566 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1567 rclass, TRUE, "subreg reg", &new_reg))
1569 bool insert_before, insert_after;
1570 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1572 insert_before = (type != OP_OUT
1573 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1574 insert_after = (type != OP_IN);
1575 insert_move_for_subreg (insert_before ? &before : NULL,
1576 insert_after ? &after : NULL,
1577 reg, new_reg);
1579 SUBREG_REG (operand) = new_reg;
1580 lra_process_new_insns (curr_insn, before, after,
1581 "Inserting subreg reload");
1582 return true;
1584 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1585 IRA allocates hardreg to the inner pseudo reg according to its mode
1586 instead of the outermode, so the size of the hardreg may not be enough
1587 to contain the outermode operand, in that case we may need to insert
1588 reload for the reg. For the following two types of paradoxical subreg,
1589 we need to insert reload:
1590 1. If the op_type is OP_IN, and the hardreg could not be paired with
1591 other hardreg to contain the outermode operand
1592 (checked by in_hard_reg_set_p), we need to insert the reload.
1593 2. If the op_type is OP_OUT or OP_INOUT.
1595 Here is a paradoxical subreg example showing how the reload is generated:
1597 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1598 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1600 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1601 here, if reg107 is assigned to hardreg R15, because R15 is the last
1602 hardreg, compiler cannot find another hardreg to pair with R15 to
1603 contain TImode data. So we insert a TImode reload reg180 for it.
1604 After reload is inserted:
1606 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1607 (reg:DI 107 [ __comp ])) -1
1608 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1609 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1611 Two reload hard registers will be allocated to reg180 to save TImode data
1612 in LRA_assign. */
1613 else if (REG_P (reg)
1614 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1615 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1616 && (hard_regno_nregs[hard_regno][innermode]
1617 < hard_regno_nregs[hard_regno][mode])
1618 && (regclass = lra_get_allocno_class (REGNO (reg)))
1619 && (type != OP_IN
1620 || !in_hard_reg_set_p (reg_class_contents[regclass],
1621 mode, hard_regno)))
1623 /* The class will be defined later in curr_insn_transform. */
1624 enum reg_class rclass
1625 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1627 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1628 rclass, TRUE, "paradoxical subreg", &new_reg))
1630 rtx subreg;
1631 bool insert_before, insert_after;
1633 PUT_MODE (new_reg, mode);
1634 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1635 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1637 insert_before = (type != OP_OUT);
1638 insert_after = (type != OP_IN);
1639 insert_move_for_subreg (insert_before ? &before : NULL,
1640 insert_after ? &after : NULL,
1641 reg, subreg);
1643 SUBREG_REG (operand) = new_reg;
1644 lra_process_new_insns (curr_insn, before, after,
1645 "Inserting paradoxical subreg reload");
1646 return true;
1648 return false;
1651 /* Return TRUE if X refers for a hard register from SET. */
1652 static bool
1653 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1655 int i, j, x_hard_regno;
1656 machine_mode mode;
1657 const char *fmt;
1658 enum rtx_code code;
1660 if (x == NULL_RTX)
1661 return false;
1662 code = GET_CODE (x);
1663 mode = GET_MODE (x);
1664 if (code == SUBREG)
1666 x = SUBREG_REG (x);
1667 code = GET_CODE (x);
1668 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1669 mode = GET_MODE (x);
1672 if (REG_P (x))
1674 x_hard_regno = get_hard_regno (x);
1675 return (x_hard_regno >= 0
1676 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1678 if (MEM_P (x))
1680 struct address_info ad;
1682 decompose_mem_address (&ad, x);
1683 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1684 return true;
1685 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1686 return true;
1688 fmt = GET_RTX_FORMAT (code);
1689 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1691 if (fmt[i] == 'e')
1693 if (uses_hard_regs_p (XEXP (x, i), set))
1694 return true;
1696 else if (fmt[i] == 'E')
1698 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1699 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1700 return true;
1703 return false;
1706 /* Return true if OP is a spilled pseudo. */
1707 static inline bool
1708 spilled_pseudo_p (rtx op)
1710 return (REG_P (op)
1711 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1714 /* Return true if X is a general constant. */
1715 static inline bool
1716 general_constant_p (rtx x)
1718 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1721 static bool
1722 reg_in_class_p (rtx reg, enum reg_class cl)
1724 if (cl == NO_REGS)
1725 return get_reg_class (REGNO (reg)) == NO_REGS;
1726 return in_class_p (reg, cl, NULL);
1729 /* Return true if SET of RCLASS contains no hard regs which can be
1730 used in MODE. */
1731 static bool
1732 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1733 HARD_REG_SET &set,
1734 enum machine_mode mode)
1736 HARD_REG_SET temp;
1738 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1739 COPY_HARD_REG_SET (temp, set);
1740 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1741 return (hard_reg_set_subset_p
1742 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1745 /* Major function to choose the current insn alternative and what
1746 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1747 negative we should consider only this alternative. Return false if
1748 we can not choose the alternative or find how to reload the
1749 operands. */
1750 static bool
1751 process_alt_operands (int only_alternative)
1753 bool ok_p = false;
1754 int nop, overall, nalt;
1755 int n_alternatives = curr_static_id->n_alternatives;
1756 int n_operands = curr_static_id->n_operands;
1757 /* LOSERS counts the operands that don't fit this alternative and
1758 would require loading. */
1759 int losers;
1760 /* REJECT is a count of how undesirable this alternative says it is
1761 if any reloading is required. If the alternative matches exactly
1762 then REJECT is ignored, but otherwise it gets this much counted
1763 against it in addition to the reloading needed. */
1764 int reject;
1765 int op_reject;
1766 /* The number of elements in the following array. */
1767 int early_clobbered_regs_num;
1768 /* Numbers of operands which are early clobber registers. */
1769 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1770 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1771 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1772 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1773 bool curr_alt_win[MAX_RECOG_OPERANDS];
1774 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1775 int curr_alt_matches[MAX_RECOG_OPERANDS];
1776 /* The number of elements in the following array. */
1777 int curr_alt_dont_inherit_ops_num;
1778 /* Numbers of operands whose reload pseudos should not be inherited. */
1779 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1780 rtx op;
1781 /* The register when the operand is a subreg of register, otherwise the
1782 operand itself. */
1783 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1784 /* The register if the operand is a register or subreg of register,
1785 otherwise NULL. */
1786 rtx operand_reg[MAX_RECOG_OPERANDS];
1787 int hard_regno[MAX_RECOG_OPERANDS];
1788 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1789 int reload_nregs, reload_sum;
1790 bool costly_p;
1791 enum reg_class cl;
1793 /* Calculate some data common for all alternatives to speed up the
1794 function. */
1795 for (nop = 0; nop < n_operands; nop++)
1797 rtx reg;
1799 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1800 /* The real hard regno of the operand after the allocation. */
1801 hard_regno[nop] = get_hard_regno (op);
1803 operand_reg[nop] = reg = op;
1804 biggest_mode[nop] = GET_MODE (op);
1805 if (GET_CODE (op) == SUBREG)
1807 operand_reg[nop] = reg = SUBREG_REG (op);
1808 if (GET_MODE_SIZE (biggest_mode[nop])
1809 < GET_MODE_SIZE (GET_MODE (reg)))
1810 biggest_mode[nop] = GET_MODE (reg);
1812 if (! REG_P (reg))
1813 operand_reg[nop] = NULL_RTX;
1814 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1815 || ((int) REGNO (reg)
1816 == lra_get_elimination_hard_regno (REGNO (reg))))
1817 no_subreg_reg_operand[nop] = reg;
1818 else
1819 operand_reg[nop] = no_subreg_reg_operand[nop]
1820 /* Just use natural mode for elimination result. It should
1821 be enough for extra constraints hooks. */
1822 = regno_reg_rtx[hard_regno[nop]];
1825 /* The constraints are made of several alternatives. Each operand's
1826 constraint looks like foo,bar,... with commas separating the
1827 alternatives. The first alternatives for all operands go
1828 together, the second alternatives go together, etc.
1830 First loop over alternatives. */
1831 alternative_mask preferred = curr_id->preferred_alternatives;
1832 if (only_alternative >= 0)
1833 preferred &= ALTERNATIVE_BIT (only_alternative);
1835 for (nalt = 0; nalt < n_alternatives; nalt++)
1837 /* Loop over operands for one constraint alternative. */
1838 if (!TEST_BIT (preferred, nalt))
1839 continue;
1841 overall = losers = reject = reload_nregs = reload_sum = 0;
1842 for (nop = 0; nop < n_operands; nop++)
1844 int inc = (curr_static_id
1845 ->operand_alternative[nalt * n_operands + nop].reject);
1846 if (lra_dump_file != NULL && inc != 0)
1847 fprintf (lra_dump_file,
1848 " Staticly defined alt reject+=%d\n", inc);
1849 reject += inc;
1851 early_clobbered_regs_num = 0;
1853 for (nop = 0; nop < n_operands; nop++)
1855 const char *p;
1856 char *end;
1857 int len, c, m, i, opalt_num, this_alternative_matches;
1858 bool win, did_match, offmemok, early_clobber_p;
1859 /* false => this operand can be reloaded somehow for this
1860 alternative. */
1861 bool badop;
1862 /* true => this operand can be reloaded if the alternative
1863 allows regs. */
1864 bool winreg;
1865 /* True if a constant forced into memory would be OK for
1866 this operand. */
1867 bool constmemok;
1868 enum reg_class this_alternative, this_costly_alternative;
1869 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1870 bool this_alternative_match_win, this_alternative_win;
1871 bool this_alternative_offmemok;
1872 bool scratch_p;
1873 machine_mode mode;
1874 enum constraint_num cn;
1876 opalt_num = nalt * n_operands + nop;
1877 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1879 /* Fast track for no constraints at all. */
1880 curr_alt[nop] = NO_REGS;
1881 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1882 curr_alt_win[nop] = true;
1883 curr_alt_match_win[nop] = false;
1884 curr_alt_offmemok[nop] = false;
1885 curr_alt_matches[nop] = -1;
1886 continue;
1889 op = no_subreg_reg_operand[nop];
1890 mode = curr_operand_mode[nop];
1892 win = did_match = winreg = offmemok = constmemok = false;
1893 badop = true;
1895 early_clobber_p = false;
1896 p = curr_static_id->operand_alternative[opalt_num].constraint;
1898 this_costly_alternative = this_alternative = NO_REGS;
1899 /* We update set of possible hard regs besides its class
1900 because reg class might be inaccurate. For example,
1901 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1902 is translated in HI_REGS because classes are merged by
1903 pairs and there is no accurate intermediate class. */
1904 CLEAR_HARD_REG_SET (this_alternative_set);
1905 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1906 this_alternative_win = false;
1907 this_alternative_match_win = false;
1908 this_alternative_offmemok = false;
1909 this_alternative_matches = -1;
1911 /* An empty constraint should be excluded by the fast
1912 track. */
1913 lra_assert (*p != 0 && *p != ',');
1915 op_reject = 0;
1916 /* Scan this alternative's specs for this operand; set WIN
1917 if the operand fits any letter in this alternative.
1918 Otherwise, clear BADOP if this operand could fit some
1919 letter after reloads, or set WINREG if this operand could
1920 fit after reloads provided the constraint allows some
1921 registers. */
1922 costly_p = false;
1925 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1927 case '\0':
1928 len = 0;
1929 break;
1930 case ',':
1931 c = '\0';
1932 break;
1934 case '&':
1935 early_clobber_p = true;
1936 break;
1938 case '$':
1939 op_reject += LRA_MAX_REJECT;
1940 break;
1941 case '^':
1942 op_reject += LRA_LOSER_COST_FACTOR;
1943 break;
1945 case '#':
1946 /* Ignore rest of this alternative. */
1947 c = '\0';
1948 break;
1950 case '0': case '1': case '2': case '3': case '4':
1951 case '5': case '6': case '7': case '8': case '9':
1953 int m_hregno;
1954 bool match_p;
1956 m = strtoul (p, &end, 10);
1957 p = end;
1958 len = 0;
1959 lra_assert (nop > m);
1961 this_alternative_matches = m;
1962 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1963 /* We are supposed to match a previous operand.
1964 If we do, we win if that one did. If we do
1965 not, count both of the operands as losers.
1966 (This is too conservative, since most of the
1967 time only a single reload insn will be needed
1968 to make the two operands win. As a result,
1969 this alternative may be rejected when it is
1970 actually desirable.) */
1971 match_p = false;
1972 if (operands_match_p (*curr_id->operand_loc[nop],
1973 *curr_id->operand_loc[m], m_hregno))
1975 /* We should reject matching of an early
1976 clobber operand if the matching operand is
1977 not dying in the insn. */
1978 if (! curr_static_id->operand[m].early_clobber
1979 || operand_reg[nop] == NULL_RTX
1980 || (find_regno_note (curr_insn, REG_DEAD,
1981 REGNO (op))
1982 || REGNO (op) == REGNO (operand_reg[m])))
1983 match_p = true;
1985 if (match_p)
1987 /* If we are matching a non-offsettable
1988 address where an offsettable address was
1989 expected, then we must reject this
1990 combination, because we can't reload
1991 it. */
1992 if (curr_alt_offmemok[m]
1993 && MEM_P (*curr_id->operand_loc[m])
1994 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1995 continue;
1997 else
1999 /* Operands don't match. Both operands must
2000 allow a reload register, otherwise we
2001 cannot make them match. */
2002 if (curr_alt[m] == NO_REGS)
2003 break;
2004 /* Retroactively mark the operand we had to
2005 match as a loser, if it wasn't already and
2006 it wasn't matched to a register constraint
2007 (e.g it might be matched by memory). */
2008 if (curr_alt_win[m]
2009 && (operand_reg[m] == NULL_RTX
2010 || hard_regno[m] < 0))
2012 losers++;
2013 reload_nregs
2014 += (ira_reg_class_max_nregs[curr_alt[m]]
2015 [GET_MODE (*curr_id->operand_loc[m])]);
2018 /* Prefer matching earlyclobber alternative as
2019 it results in less hard regs required for
2020 the insn than a non-matching earlyclobber
2021 alternative. */
2022 if (curr_static_id->operand[m].early_clobber)
2024 if (lra_dump_file != NULL)
2025 fprintf
2026 (lra_dump_file,
2027 " %d Matching earlyclobber alt:"
2028 " reject--\n",
2029 nop);
2030 reject--;
2032 /* Otherwise we prefer no matching
2033 alternatives because it gives more freedom
2034 in RA. */
2035 else if (operand_reg[nop] == NULL_RTX
2036 || (find_regno_note (curr_insn, REG_DEAD,
2037 REGNO (operand_reg[nop]))
2038 == NULL_RTX))
2040 if (lra_dump_file != NULL)
2041 fprintf
2042 (lra_dump_file,
2043 " %d Matching alt: reject+=2\n",
2044 nop);
2045 reject += 2;
2048 /* If we have to reload this operand and some
2049 previous operand also had to match the same
2050 thing as this operand, we don't know how to do
2051 that. */
2052 if (!match_p || !curr_alt_win[m])
2054 for (i = 0; i < nop; i++)
2055 if (curr_alt_matches[i] == m)
2056 break;
2057 if (i < nop)
2058 break;
2060 else
2061 did_match = true;
2063 /* This can be fixed with reloads if the operand
2064 we are supposed to match can be fixed with
2065 reloads. */
2066 badop = false;
2067 this_alternative = curr_alt[m];
2068 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
2069 winreg = this_alternative != NO_REGS;
2070 break;
2073 case 'g':
2074 if (MEM_P (op)
2075 || general_constant_p (op)
2076 || spilled_pseudo_p (op))
2077 win = true;
2078 cl = GENERAL_REGS;
2079 goto reg;
2081 default:
2082 cn = lookup_constraint (p);
2083 switch (get_constraint_type (cn))
2085 case CT_REGISTER:
2086 cl = reg_class_for_constraint (cn);
2087 if (cl != NO_REGS)
2088 goto reg;
2089 break;
2091 case CT_CONST_INT:
2092 if (CONST_INT_P (op)
2093 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2094 win = true;
2095 break;
2097 case CT_MEMORY:
2098 if (MEM_P (op)
2099 && satisfies_memory_constraint_p (op, cn))
2100 win = true;
2101 else if (spilled_pseudo_p (op))
2102 win = true;
2104 /* If we didn't already win, we can reload constants
2105 via force_const_mem or put the pseudo value into
2106 memory, or make other memory by reloading the
2107 address like for 'o'. */
2108 if (CONST_POOL_OK_P (mode, op)
2109 || MEM_P (op) || REG_P (op)
2110 /* We can restore the equiv insn by a
2111 reload. */
2112 || equiv_substition_p[nop])
2113 badop = false;
2114 constmemok = true;
2115 offmemok = true;
2116 break;
2118 case CT_ADDRESS:
2119 /* If we didn't already win, we can reload the address
2120 into a base register. */
2121 if (satisfies_address_constraint_p (op, cn))
2122 win = true;
2123 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2124 ADDRESS, SCRATCH);
2125 badop = false;
2126 goto reg;
2128 case CT_FIXED_FORM:
2129 if (constraint_satisfied_p (op, cn))
2130 win = true;
2131 break;
2133 case CT_SPECIAL_MEMORY:
2134 if (MEM_P (op)
2135 && satisfies_memory_constraint_p (op, cn))
2136 win = true;
2137 else if (spilled_pseudo_p (op))
2138 win = true;
2139 break;
2141 break;
2143 reg:
2144 this_alternative = reg_class_subunion[this_alternative][cl];
2145 IOR_HARD_REG_SET (this_alternative_set,
2146 reg_class_contents[cl]);
2147 if (costly_p)
2149 this_costly_alternative
2150 = reg_class_subunion[this_costly_alternative][cl];
2151 IOR_HARD_REG_SET (this_costly_alternative_set,
2152 reg_class_contents[cl]);
2154 if (mode == BLKmode)
2155 break;
2156 winreg = true;
2157 if (REG_P (op))
2159 if (hard_regno[nop] >= 0
2160 && in_hard_reg_set_p (this_alternative_set,
2161 mode, hard_regno[nop]))
2162 win = true;
2163 else if (hard_regno[nop] < 0
2164 && in_class_p (op, this_alternative, NULL))
2165 win = true;
2167 break;
2169 if (c != ' ' && c != '\t')
2170 costly_p = c == '*';
2172 while ((p += len), c);
2174 scratch_p = (operand_reg[nop] != NULL_RTX
2175 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2176 /* Record which operands fit this alternative. */
2177 if (win)
2179 this_alternative_win = true;
2180 if (operand_reg[nop] != NULL_RTX)
2182 if (hard_regno[nop] >= 0)
2184 if (in_hard_reg_set_p (this_costly_alternative_set,
2185 mode, hard_regno[nop]))
2187 if (lra_dump_file != NULL)
2188 fprintf (lra_dump_file,
2189 " %d Costly set: reject++\n",
2190 nop);
2191 reject++;
2194 else
2196 /* Prefer won reg to spilled pseudo under other
2197 equal conditions for possibe inheritance. */
2198 if (! scratch_p)
2200 if (lra_dump_file != NULL)
2201 fprintf
2202 (lra_dump_file,
2203 " %d Non pseudo reload: reject++\n",
2204 nop);
2205 reject++;
2207 if (in_class_p (operand_reg[nop],
2208 this_costly_alternative, NULL))
2210 if (lra_dump_file != NULL)
2211 fprintf
2212 (lra_dump_file,
2213 " %d Non pseudo costly reload:"
2214 " reject++\n",
2215 nop);
2216 reject++;
2219 /* We simulate the behavior of old reload here.
2220 Although scratches need hard registers and it
2221 might result in spilling other pseudos, no reload
2222 insns are generated for the scratches. So it
2223 might cost something but probably less than old
2224 reload pass believes. */
2225 if (scratch_p)
2227 if (lra_dump_file != NULL)
2228 fprintf (lra_dump_file,
2229 " %d Scratch win: reject+=2\n",
2230 nop);
2231 reject += 2;
2235 else if (did_match)
2236 this_alternative_match_win = true;
2237 else
2239 int const_to_mem = 0;
2240 bool no_regs_p;
2242 reject += op_reject;
2243 /* Never do output reload of stack pointer. It makes
2244 impossible to do elimination when SP is changed in
2245 RTL. */
2246 if (op == stack_pointer_rtx && ! frame_pointer_needed
2247 && curr_static_id->operand[nop].type != OP_IN)
2248 goto fail;
2250 /* If this alternative asks for a specific reg class, see if there
2251 is at least one allocatable register in that class. */
2252 no_regs_p
2253 = (this_alternative == NO_REGS
2254 || (hard_reg_set_subset_p
2255 (reg_class_contents[this_alternative],
2256 lra_no_alloc_regs)));
2258 /* For asms, verify that the class for this alternative is possible
2259 for the mode that is specified. */
2260 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2262 int i;
2263 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2264 if (HARD_REGNO_MODE_OK (i, mode)
2265 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2266 mode, i))
2267 break;
2268 if (i == FIRST_PSEUDO_REGISTER)
2269 winreg = false;
2272 /* If this operand accepts a register, and if the
2273 register class has at least one allocatable register,
2274 then this operand can be reloaded. */
2275 if (winreg && !no_regs_p)
2276 badop = false;
2278 if (badop)
2280 if (lra_dump_file != NULL)
2281 fprintf (lra_dump_file,
2282 " alt=%d: Bad operand -- refuse\n",
2283 nalt);
2284 goto fail;
2287 if (this_alternative != NO_REGS)
2289 HARD_REG_SET available_regs;
2291 COPY_HARD_REG_SET (available_regs,
2292 reg_class_contents[this_alternative]);
2293 AND_COMPL_HARD_REG_SET
2294 (available_regs,
2295 ira_prohibited_class_mode_regs[this_alternative][mode]);
2296 AND_COMPL_HARD_REG_SET (available_regs, lra_no_alloc_regs);
2297 if (hard_reg_set_empty_p (available_regs))
2299 /* There are no hard regs holding a value of given
2300 mode. */
2301 if (offmemok)
2303 this_alternative = NO_REGS;
2304 if (lra_dump_file != NULL)
2305 fprintf (lra_dump_file,
2306 " %d Using memory because of"
2307 " a bad mode: reject+=2\n",
2308 nop);
2309 reject += 2;
2311 else
2313 if (lra_dump_file != NULL)
2314 fprintf (lra_dump_file,
2315 " alt=%d: Wrong mode -- refuse\n",
2316 nalt);
2317 goto fail;
2322 /* If not assigned pseudo has a class which a subset of
2323 required reg class, it is a less costly alternative
2324 as the pseudo still can get a hard reg of necessary
2325 class. */
2326 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2327 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2328 && ira_class_subset_p[this_alternative][cl])
2330 if (lra_dump_file != NULL)
2331 fprintf
2332 (lra_dump_file,
2333 " %d Super set class reg: reject-=3\n", nop);
2334 reject -= 3;
2337 this_alternative_offmemok = offmemok;
2338 if (this_costly_alternative != NO_REGS)
2340 if (lra_dump_file != NULL)
2341 fprintf (lra_dump_file,
2342 " %d Costly loser: reject++\n", nop);
2343 reject++;
2345 /* If the operand is dying, has a matching constraint,
2346 and satisfies constraints of the matched operand
2347 which failed to satisfy the own constraints, most probably
2348 the reload for this operand will be gone. */
2349 if (this_alternative_matches >= 0
2350 && !curr_alt_win[this_alternative_matches]
2351 && REG_P (op)
2352 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2353 && (hard_regno[nop] >= 0
2354 ? in_hard_reg_set_p (this_alternative_set,
2355 mode, hard_regno[nop])
2356 : in_class_p (op, this_alternative, NULL)))
2358 if (lra_dump_file != NULL)
2359 fprintf
2360 (lra_dump_file,
2361 " %d Dying matched operand reload: reject++\n",
2362 nop);
2363 reject++;
2365 else
2367 /* Strict_low_part requires to reload the register
2368 not the sub-register. In this case we should
2369 check that a final reload hard reg can hold the
2370 value mode. */
2371 if (curr_static_id->operand[nop].strict_low
2372 && REG_P (op)
2373 && hard_regno[nop] < 0
2374 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2375 && ira_class_hard_regs_num[this_alternative] > 0
2376 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2377 [this_alternative][0],
2378 GET_MODE
2379 (*curr_id->operand_loc[nop])))
2381 if (lra_dump_file != NULL)
2382 fprintf
2383 (lra_dump_file,
2384 " alt=%d: Strict low subreg reload -- refuse\n",
2385 nalt);
2386 goto fail;
2388 losers++;
2390 if (operand_reg[nop] != NULL_RTX
2391 /* Output operands and matched input operands are
2392 not inherited. The following conditions do not
2393 exactly describe the previous statement but they
2394 are pretty close. */
2395 && curr_static_id->operand[nop].type != OP_OUT
2396 && (this_alternative_matches < 0
2397 || curr_static_id->operand[nop].type != OP_IN))
2399 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2400 (operand_reg[nop])]
2401 .last_reload);
2403 /* The value of reload_sum has sense only if we
2404 process insns in their order. It happens only on
2405 the first constraints sub-pass when we do most of
2406 reload work. */
2407 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2408 reload_sum += last_reload - bb_reload_num;
2410 /* If this is a constant that is reloaded into the
2411 desired class by copying it to memory first, count
2412 that as another reload. This is consistent with
2413 other code and is required to avoid choosing another
2414 alternative when the constant is moved into memory.
2415 Note that the test here is precisely the same as in
2416 the code below that calls force_const_mem. */
2417 if (CONST_POOL_OK_P (mode, op)
2418 && ((targetm.preferred_reload_class
2419 (op, this_alternative) == NO_REGS)
2420 || no_input_reloads_p))
2422 const_to_mem = 1;
2423 if (! no_regs_p)
2424 losers++;
2427 /* Alternative loses if it requires a type of reload not
2428 permitted for this insn. We can always reload
2429 objects with a REG_UNUSED note. */
2430 if ((curr_static_id->operand[nop].type != OP_IN
2431 && no_output_reloads_p
2432 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2433 || (curr_static_id->operand[nop].type != OP_OUT
2434 && no_input_reloads_p && ! const_to_mem)
2435 || (this_alternative_matches >= 0
2436 && (no_input_reloads_p
2437 || (no_output_reloads_p
2438 && (curr_static_id->operand
2439 [this_alternative_matches].type != OP_IN)
2440 && ! find_reg_note (curr_insn, REG_UNUSED,
2441 no_subreg_reg_operand
2442 [this_alternative_matches])))))
2444 if (lra_dump_file != NULL)
2445 fprintf
2446 (lra_dump_file,
2447 " alt=%d: No input/otput reload -- refuse\n",
2448 nalt);
2449 goto fail;
2452 /* Alternative loses if it required class pseudo can not
2453 hold value of required mode. Such insns can be
2454 described by insn definitions with mode iterators. */
2455 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2456 && ! hard_reg_set_empty_p (this_alternative_set)
2457 /* It is common practice for constraints to use a
2458 class which does not have actually enough regs to
2459 hold the value (e.g. x86 AREG for mode requiring
2460 more one general reg). Therefore we have 2
2461 conditions to check that the reload pseudo can
2462 not hold the mode value. */
2463 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2464 [this_alternative][0],
2465 GET_MODE (*curr_id->operand_loc[nop]))
2466 /* The above condition is not enough as the first
2467 reg in ira_class_hard_regs can be not aligned for
2468 multi-words mode values. */
2469 && (prohibited_class_reg_set_mode_p
2470 (this_alternative, this_alternative_set,
2471 GET_MODE (*curr_id->operand_loc[nop]))))
2473 if (lra_dump_file != NULL)
2474 fprintf (lra_dump_file,
2475 " alt=%d: reload pseudo for op %d "
2476 " can not hold the mode value -- refuse\n",
2477 nalt, nop);
2478 goto fail;
2481 /* Check strong discouragement of reload of non-constant
2482 into class THIS_ALTERNATIVE. */
2483 if (! CONSTANT_P (op) && ! no_regs_p
2484 && (targetm.preferred_reload_class
2485 (op, this_alternative) == NO_REGS
2486 || (curr_static_id->operand[nop].type == OP_OUT
2487 && (targetm.preferred_output_reload_class
2488 (op, this_alternative) == NO_REGS))))
2490 if (lra_dump_file != NULL)
2491 fprintf (lra_dump_file,
2492 " %d Non-prefered reload: reject+=%d\n",
2493 nop, LRA_MAX_REJECT);
2494 reject += LRA_MAX_REJECT;
2497 if (! (MEM_P (op) && offmemok)
2498 && ! (const_to_mem && constmemok))
2500 /* We prefer to reload pseudos over reloading other
2501 things, since such reloads may be able to be
2502 eliminated later. So bump REJECT in other cases.
2503 Don't do this in the case where we are forcing a
2504 constant into memory and it will then win since
2505 we don't want to have a different alternative
2506 match then. */
2507 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2509 if (lra_dump_file != NULL)
2510 fprintf
2511 (lra_dump_file,
2512 " %d Non-pseudo reload: reject+=2\n",
2513 nop);
2514 reject += 2;
2517 if (! no_regs_p)
2518 reload_nregs
2519 += ira_reg_class_max_nregs[this_alternative][mode];
2521 if (SMALL_REGISTER_CLASS_P (this_alternative))
2523 if (lra_dump_file != NULL)
2524 fprintf
2525 (lra_dump_file,
2526 " %d Small class reload: reject+=%d\n",
2527 nop, LRA_LOSER_COST_FACTOR / 2);
2528 reject += LRA_LOSER_COST_FACTOR / 2;
2532 /* We are trying to spill pseudo into memory. It is
2533 usually more costly than moving to a hard register
2534 although it might takes the same number of
2535 reloads.
2537 Non-pseudo spill may happen also. Suppose a target allows both
2538 register and memory in the operand constraint alternatives,
2539 then it's typical that an eliminable register has a substition
2540 of "base + offset" which can either be reloaded by a simple
2541 "new_reg <= base + offset" which will match the register
2542 constraint, or a similar reg addition followed by further spill
2543 to and reload from memory which will match the memory
2544 constraint, but this memory spill will be much more costly
2545 usually.
2547 Code below increases the reject for both pseudo and non-pseudo
2548 spill. */
2549 if (no_regs_p
2550 && !(MEM_P (op) && offmemok)
2551 && !(REG_P (op) && hard_regno[nop] < 0))
2553 if (lra_dump_file != NULL)
2554 fprintf
2555 (lra_dump_file,
2556 " %d Spill %spseudo into memory: reject+=3\n",
2557 nop, REG_P (op) ? "" : "Non-");
2558 reject += 3;
2559 if (VECTOR_MODE_P (mode))
2561 /* Spilling vectors into memory is usually more
2562 costly as they contain big values. */
2563 if (lra_dump_file != NULL)
2564 fprintf
2565 (lra_dump_file,
2566 " %d Spill vector pseudo: reject+=2\n",
2567 nop);
2568 reject += 2;
2572 #ifdef SECONDARY_MEMORY_NEEDED
2573 /* If reload requires moving value through secondary
2574 memory, it will need one more insn at least. */
2575 if (this_alternative != NO_REGS
2576 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2577 && ((curr_static_id->operand[nop].type != OP_OUT
2578 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2579 GET_MODE (op)))
2580 || (curr_static_id->operand[nop].type != OP_IN
2581 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2582 GET_MODE (op)))))
2583 losers++;
2584 #endif
2585 /* Input reloads can be inherited more often than output
2586 reloads can be removed, so penalize output
2587 reloads. */
2588 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2590 if (lra_dump_file != NULL)
2591 fprintf
2592 (lra_dump_file,
2593 " %d Non input pseudo reload: reject++\n",
2594 nop);
2595 reject++;
2599 if (early_clobber_p && ! scratch_p)
2601 if (lra_dump_file != NULL)
2602 fprintf (lra_dump_file,
2603 " %d Early clobber: reject++\n", nop);
2604 reject++;
2606 /* ??? We check early clobbers after processing all operands
2607 (see loop below) and there we update the costs more.
2608 Should we update the cost (may be approximately) here
2609 because of early clobber register reloads or it is a rare
2610 or non-important thing to be worth to do it. */
2611 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2612 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2614 if (lra_dump_file != NULL)
2615 fprintf (lra_dump_file,
2616 " alt=%d,overall=%d,losers=%d -- refuse\n",
2617 nalt, overall, losers);
2618 goto fail;
2621 curr_alt[nop] = this_alternative;
2622 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2623 curr_alt_win[nop] = this_alternative_win;
2624 curr_alt_match_win[nop] = this_alternative_match_win;
2625 curr_alt_offmemok[nop] = this_alternative_offmemok;
2626 curr_alt_matches[nop] = this_alternative_matches;
2628 if (this_alternative_matches >= 0
2629 && !did_match && !this_alternative_win)
2630 curr_alt_win[this_alternative_matches] = false;
2632 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2633 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2635 if (curr_insn_set != NULL_RTX && n_operands == 2
2636 /* Prevent processing non-move insns. */
2637 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2638 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2639 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2640 && REG_P (no_subreg_reg_operand[0])
2641 && REG_P (no_subreg_reg_operand[1])
2642 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2643 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2644 || (! curr_alt_win[0] && curr_alt_win[1]
2645 && REG_P (no_subreg_reg_operand[1])
2646 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2647 || (curr_alt_win[0] && ! curr_alt_win[1]
2648 && REG_P (no_subreg_reg_operand[0])
2649 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2650 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2651 no_subreg_reg_operand[1])
2652 || (targetm.preferred_reload_class
2653 (no_subreg_reg_operand[1],
2654 (enum reg_class) curr_alt[1]) != NO_REGS))
2655 /* If it is a result of recent elimination in move
2656 insn we can transform it into an add still by
2657 using this alternative. */
2658 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2660 /* We have a move insn and a new reload insn will be similar
2661 to the current insn. We should avoid such situation as it
2662 results in LRA cycling. */
2663 overall += LRA_MAX_REJECT;
2665 ok_p = true;
2666 curr_alt_dont_inherit_ops_num = 0;
2667 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2669 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2670 HARD_REG_SET temp_set;
2672 i = early_clobbered_nops[nop];
2673 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2674 || hard_regno[i] < 0)
2675 continue;
2676 lra_assert (operand_reg[i] != NULL_RTX);
2677 clobbered_hard_regno = hard_regno[i];
2678 CLEAR_HARD_REG_SET (temp_set);
2679 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2680 first_conflict_j = last_conflict_j = -1;
2681 for (j = 0; j < n_operands; j++)
2682 if (j == i
2683 /* We don't want process insides of match_operator and
2684 match_parallel because otherwise we would process
2685 their operands once again generating a wrong
2686 code. */
2687 || curr_static_id->operand[j].is_operator)
2688 continue;
2689 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2690 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2691 continue;
2692 /* If we don't reload j-th operand, check conflicts. */
2693 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2694 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2696 if (first_conflict_j < 0)
2697 first_conflict_j = j;
2698 last_conflict_j = j;
2700 if (last_conflict_j < 0)
2701 continue;
2702 /* If earlyclobber operand conflicts with another
2703 non-matching operand which is actually the same register
2704 as the earlyclobber operand, it is better to reload the
2705 another operand as an operand matching the earlyclobber
2706 operand can be also the same. */
2707 if (first_conflict_j == last_conflict_j
2708 && operand_reg[last_conflict_j] != NULL_RTX
2709 && ! curr_alt_match_win[last_conflict_j]
2710 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2712 curr_alt_win[last_conflict_j] = false;
2713 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2714 = last_conflict_j;
2715 losers++;
2716 /* Early clobber was already reflected in REJECT. */
2717 lra_assert (reject > 0);
2718 if (lra_dump_file != NULL)
2719 fprintf
2720 (lra_dump_file,
2721 " %d Conflict early clobber reload: reject--\n",
2723 reject--;
2724 overall += LRA_LOSER_COST_FACTOR - 1;
2726 else
2728 /* We need to reload early clobbered register and the
2729 matched registers. */
2730 for (j = 0; j < n_operands; j++)
2731 if (curr_alt_matches[j] == i)
2733 curr_alt_match_win[j] = false;
2734 losers++;
2735 overall += LRA_LOSER_COST_FACTOR;
2737 if (! curr_alt_match_win[i])
2738 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2739 else
2741 /* Remember pseudos used for match reloads are never
2742 inherited. */
2743 lra_assert (curr_alt_matches[i] >= 0);
2744 curr_alt_win[curr_alt_matches[i]] = false;
2746 curr_alt_win[i] = curr_alt_match_win[i] = false;
2747 losers++;
2748 /* Early clobber was already reflected in REJECT. */
2749 lra_assert (reject > 0);
2750 if (lra_dump_file != NULL)
2751 fprintf
2752 (lra_dump_file,
2753 " %d Matched conflict early clobber reloads:"
2754 "reject--\n",
2756 reject--;
2757 overall += LRA_LOSER_COST_FACTOR - 1;
2760 if (lra_dump_file != NULL)
2761 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2762 nalt, overall, losers, reload_nregs);
2764 /* If this alternative can be made to work by reloading, and it
2765 needs less reloading than the others checked so far, record
2766 it as the chosen goal for reloading. */
2767 if ((best_losers != 0 && losers == 0)
2768 || (((best_losers == 0 && losers == 0)
2769 || (best_losers != 0 && losers != 0))
2770 && (best_overall > overall
2771 || (best_overall == overall
2772 /* If the cost of the reloads is the same,
2773 prefer alternative which requires minimal
2774 number of reload regs. */
2775 && (reload_nregs < best_reload_nregs
2776 || (reload_nregs == best_reload_nregs
2777 && (best_reload_sum < reload_sum
2778 || (best_reload_sum == reload_sum
2779 && nalt < goal_alt_number))))))))
2781 for (nop = 0; nop < n_operands; nop++)
2783 goal_alt_win[nop] = curr_alt_win[nop];
2784 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2785 goal_alt_matches[nop] = curr_alt_matches[nop];
2786 goal_alt[nop] = curr_alt[nop];
2787 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2789 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2790 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2791 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2792 goal_alt_swapped = curr_swapped;
2793 best_overall = overall;
2794 best_losers = losers;
2795 best_reload_nregs = reload_nregs;
2796 best_reload_sum = reload_sum;
2797 goal_alt_number = nalt;
2799 if (losers == 0)
2800 /* Everything is satisfied. Do not process alternatives
2801 anymore. */
2802 break;
2803 fail:
2806 return ok_p;
2809 /* Make reload base reg from address AD. */
2810 static rtx
2811 base_to_reg (struct address_info *ad)
2813 enum reg_class cl;
2814 int code = -1;
2815 rtx new_inner = NULL_RTX;
2816 rtx new_reg = NULL_RTX;
2817 rtx_insn *insn;
2818 rtx_insn *last_insn = get_last_insn();
2820 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2821 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2822 get_index_code (ad));
2823 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2824 cl, "base");
2825 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2826 ad->disp_term == NULL
2827 ? gen_int_mode (0, ad->mode)
2828 : *ad->disp_term);
2829 if (!valid_address_p (ad->mode, new_inner, ad->as))
2830 return NULL_RTX;
2831 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base_term));
2832 code = recog_memoized (insn);
2833 if (code < 0)
2835 delete_insns_since (last_insn);
2836 return NULL_RTX;
2839 return new_inner;
2842 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2843 static rtx
2844 base_plus_disp_to_reg (struct address_info *ad)
2846 enum reg_class cl;
2847 rtx new_reg;
2849 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2850 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2851 get_index_code (ad));
2852 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2853 cl, "base + disp");
2854 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2855 return new_reg;
2858 /* Make reload of index part of address AD. Return the new
2859 pseudo. */
2860 static rtx
2861 index_part_to_reg (struct address_info *ad)
2863 rtx new_reg;
2865 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2866 INDEX_REG_CLASS, "index term");
2867 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2868 GEN_INT (get_index_scale (ad)), new_reg, 1);
2869 return new_reg;
2872 /* Return true if we can add a displacement to address AD, even if that
2873 makes the address invalid. The fix-up code requires any new address
2874 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2875 static bool
2876 can_add_disp_p (struct address_info *ad)
2878 return (!ad->autoinc_p
2879 && ad->segment == NULL
2880 && ad->base == ad->base_term
2881 && ad->disp == ad->disp_term);
2884 /* Make equiv substitution in address AD. Return true if a substitution
2885 was made. */
2886 static bool
2887 equiv_address_substitution (struct address_info *ad)
2889 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2890 HOST_WIDE_INT disp, scale;
2891 bool change_p;
2893 base_term = strip_subreg (ad->base_term);
2894 if (base_term == NULL)
2895 base_reg = new_base_reg = NULL_RTX;
2896 else
2898 base_reg = *base_term;
2899 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2901 index_term = strip_subreg (ad->index_term);
2902 if (index_term == NULL)
2903 index_reg = new_index_reg = NULL_RTX;
2904 else
2906 index_reg = *index_term;
2907 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2909 if (base_reg == new_base_reg && index_reg == new_index_reg)
2910 return false;
2911 disp = 0;
2912 change_p = false;
2913 if (lra_dump_file != NULL)
2915 fprintf (lra_dump_file, "Changing address in insn %d ",
2916 INSN_UID (curr_insn));
2917 dump_value_slim (lra_dump_file, *ad->outer, 1);
2919 if (base_reg != new_base_reg)
2921 if (REG_P (new_base_reg))
2923 *base_term = new_base_reg;
2924 change_p = true;
2926 else if (GET_CODE (new_base_reg) == PLUS
2927 && REG_P (XEXP (new_base_reg, 0))
2928 && CONST_INT_P (XEXP (new_base_reg, 1))
2929 && can_add_disp_p (ad))
2931 disp += INTVAL (XEXP (new_base_reg, 1));
2932 *base_term = XEXP (new_base_reg, 0);
2933 change_p = true;
2935 if (ad->base_term2 != NULL)
2936 *ad->base_term2 = *ad->base_term;
2938 if (index_reg != new_index_reg)
2940 if (REG_P (new_index_reg))
2942 *index_term = new_index_reg;
2943 change_p = true;
2945 else if (GET_CODE (new_index_reg) == PLUS
2946 && REG_P (XEXP (new_index_reg, 0))
2947 && CONST_INT_P (XEXP (new_index_reg, 1))
2948 && can_add_disp_p (ad)
2949 && (scale = get_index_scale (ad)))
2951 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2952 *index_term = XEXP (new_index_reg, 0);
2953 change_p = true;
2956 if (disp != 0)
2958 if (ad->disp != NULL)
2959 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2960 else
2962 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2963 update_address (ad);
2965 change_p = true;
2967 if (lra_dump_file != NULL)
2969 if (! change_p)
2970 fprintf (lra_dump_file, " -- no change\n");
2971 else
2973 fprintf (lra_dump_file, " on equiv ");
2974 dump_value_slim (lra_dump_file, *ad->outer, 1);
2975 fprintf (lra_dump_file, "\n");
2978 return change_p;
2981 /* Major function to make reloads for an address in operand NOP or
2982 check its correctness (If CHECK_ONLY_P is true). The supported
2983 cases are:
2985 1) an address that existed before LRA started, at which point it
2986 must have been valid. These addresses are subject to elimination
2987 and may have become invalid due to the elimination offset being out
2988 of range.
2990 2) an address created by forcing a constant to memory
2991 (force_const_to_mem). The initial form of these addresses might
2992 not be valid, and it is this function's job to make them valid.
2994 3) a frame address formed from a register and a (possibly zero)
2995 constant offset. As above, these addresses might not be valid and
2996 this function must make them so.
2998 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2999 reloads to *AFTER because of inc/dec, {pre, post} modify in the
3000 address. Return true for any RTL change.
3002 The function is a helper function which does not produce all
3003 transformations (when CHECK_ONLY_P is false) which can be
3004 necessary. It does just basic steps. To do all necessary
3005 transformations use function process_address. */
3006 static bool
3007 process_address_1 (int nop, bool check_only_p,
3008 rtx_insn **before, rtx_insn **after)
3010 struct address_info ad;
3011 rtx new_reg;
3012 HOST_WIDE_INT scale;
3013 rtx op = *curr_id->operand_loc[nop];
3014 const char *constraint = curr_static_id->operand[nop].constraint;
3015 enum constraint_num cn = lookup_constraint (constraint);
3016 bool change_p = false;
3018 if (MEM_P (op)
3019 && GET_MODE (op) == BLKmode
3020 && GET_CODE (XEXP (op, 0)) == SCRATCH)
3021 return false;
3023 if (insn_extra_address_constraint (cn))
3024 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
3025 else if (MEM_P (op))
3026 decompose_mem_address (&ad, op);
3027 else if (GET_CODE (op) == SUBREG
3028 && MEM_P (SUBREG_REG (op)))
3029 decompose_mem_address (&ad, SUBREG_REG (op));
3030 else
3031 return false;
3032 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
3033 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
3034 when INDEX_REG_CLASS is a single register class. */
3035 if (ad.base_term != NULL
3036 && ad.index_term != NULL
3037 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
3038 && REG_P (*ad.base_term)
3039 && REG_P (*ad.index_term)
3040 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
3041 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
3043 std::swap (ad.base, ad.index);
3044 std::swap (ad.base_term, ad.index_term);
3046 if (! check_only_p)
3047 change_p = equiv_address_substitution (&ad);
3048 if (ad.base_term != NULL
3049 && (process_addr_reg
3050 (ad.base_term, check_only_p, before,
3051 (ad.autoinc_p
3052 && !(REG_P (*ad.base_term)
3053 && find_regno_note (curr_insn, REG_DEAD,
3054 REGNO (*ad.base_term)) != NULL_RTX)
3055 ? after : NULL),
3056 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3057 get_index_code (&ad)))))
3059 change_p = true;
3060 if (ad.base_term2 != NULL)
3061 *ad.base_term2 = *ad.base_term;
3063 if (ad.index_term != NULL
3064 && process_addr_reg (ad.index_term, check_only_p,
3065 before, NULL, INDEX_REG_CLASS))
3066 change_p = true;
3068 /* Target hooks sometimes don't treat extra-constraint addresses as
3069 legitimate address_operands, so handle them specially. */
3070 if (insn_extra_address_constraint (cn)
3071 && satisfies_address_constraint_p (&ad, cn))
3072 return change_p;
3074 if (check_only_p)
3075 return change_p;
3077 /* There are three cases where the shape of *AD.INNER may now be invalid:
3079 1) the original address was valid, but either elimination or
3080 equiv_address_substitution was applied and that made
3081 the address invalid.
3083 2) the address is an invalid symbolic address created by
3084 force_const_to_mem.
3086 3) the address is a frame address with an invalid offset.
3088 4) the address is a frame address with an invalid base.
3090 All these cases involve a non-autoinc address, so there is no
3091 point revalidating other types. */
3092 if (ad.autoinc_p || valid_address_p (&ad))
3093 return change_p;
3095 /* Any index existed before LRA started, so we can assume that the
3096 presence and shape of the index is valid. */
3097 push_to_sequence (*before);
3098 lra_assert (ad.disp == ad.disp_term);
3099 if (ad.base == NULL)
3101 if (ad.index == NULL)
3103 rtx_insn *insn;
3104 rtx_insn *last = get_last_insn ();
3105 int code = -1;
3106 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3107 SCRATCH, SCRATCH);
3108 rtx addr = *ad.inner;
3110 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3111 if (HAVE_lo_sum)
3113 /* addr => lo_sum (new_base, addr), case (2) above. */
3114 insn = emit_insn (gen_rtx_SET
3115 (new_reg,
3116 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
3117 code = recog_memoized (insn);
3118 if (code >= 0)
3120 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
3121 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
3123 /* Try to put lo_sum into register. */
3124 insn = emit_insn (gen_rtx_SET
3125 (new_reg,
3126 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
3127 code = recog_memoized (insn);
3128 if (code >= 0)
3130 *ad.inner = new_reg;
3131 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
3133 *ad.inner = addr;
3134 code = -1;
3140 if (code < 0)
3141 delete_insns_since (last);
3144 if (code < 0)
3146 /* addr => new_base, case (2) above. */
3147 lra_emit_move (new_reg, addr);
3149 for (insn = last == NULL_RTX ? get_insns () : NEXT_INSN (last);
3150 insn != NULL_RTX;
3151 insn = NEXT_INSN (insn))
3152 if (recog_memoized (insn) < 0)
3153 break;
3154 if (insn != NULL_RTX)
3156 /* Do nothing if we cannot generate right insns.
3157 This is analogous to reload pass behavior. */
3158 delete_insns_since (last);
3159 end_sequence ();
3160 return false;
3162 *ad.inner = new_reg;
3165 else
3167 /* index * scale + disp => new base + index * scale,
3168 case (1) above. */
3169 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3170 GET_CODE (*ad.index));
3172 lra_assert (INDEX_REG_CLASS != NO_REGS);
3173 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3174 lra_emit_move (new_reg, *ad.disp);
3175 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3176 new_reg, *ad.index);
3179 else if (ad.index == NULL)
3181 int regno;
3182 enum reg_class cl;
3183 rtx set;
3184 rtx_insn *insns, *last_insn;
3185 /* Try to reload base into register only if the base is invalid
3186 for the address but with valid offset, case (4) above. */
3187 start_sequence ();
3188 new_reg = base_to_reg (&ad);
3190 /* base + disp => new base, cases (1) and (3) above. */
3191 /* Another option would be to reload the displacement into an
3192 index register. However, postreload has code to optimize
3193 address reloads that have the same base and different
3194 displacements, so reloading into an index register would
3195 not necessarily be a win. */
3196 if (new_reg == NULL_RTX)
3197 new_reg = base_plus_disp_to_reg (&ad);
3198 insns = get_insns ();
3199 last_insn = get_last_insn ();
3200 /* If we generated at least two insns, try last insn source as
3201 an address. If we succeed, we generate one less insn. */
3202 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3203 && GET_CODE (SET_SRC (set)) == PLUS
3204 && REG_P (XEXP (SET_SRC (set), 0))
3205 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3207 *ad.inner = SET_SRC (set);
3208 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3210 *ad.base_term = XEXP (SET_SRC (set), 0);
3211 *ad.disp_term = XEXP (SET_SRC (set), 1);
3212 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3213 get_index_code (&ad));
3214 regno = REGNO (*ad.base_term);
3215 if (regno >= FIRST_PSEUDO_REGISTER
3216 && cl != lra_get_allocno_class (regno))
3217 lra_change_class (regno, cl, " Change to", true);
3218 new_reg = SET_SRC (set);
3219 delete_insns_since (PREV_INSN (last_insn));
3222 /* Try if target can split displacement into legitimite new disp
3223 and offset. If it's the case, we replace the last insn with
3224 insns for base + offset => new_reg and set new_reg + new disp
3225 to *ad.inner. */
3226 last_insn = get_last_insn ();
3227 if ((set = single_set (last_insn)) != NULL_RTX
3228 && GET_CODE (SET_SRC (set)) == PLUS
3229 && REG_P (XEXP (SET_SRC (set), 0))
3230 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3231 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3233 rtx addend, disp = XEXP (SET_SRC (set), 1);
3234 if (targetm.legitimize_address_displacement (&disp, &addend,
3235 ad.mode))
3237 rtx_insn *new_insns;
3238 start_sequence ();
3239 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3240 new_insns = get_insns ();
3241 end_sequence ();
3242 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3243 delete_insns_since (PREV_INSN (last_insn));
3244 add_insn (new_insns);
3245 insns = get_insns ();
3248 end_sequence ();
3249 emit_insn (insns);
3250 *ad.inner = new_reg;
3252 else if (ad.disp_term != NULL)
3254 /* base + scale * index + disp => new base + scale * index,
3255 case (1) above. */
3256 new_reg = base_plus_disp_to_reg (&ad);
3257 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3258 new_reg, *ad.index);
3260 else if ((scale = get_index_scale (&ad)) == 1)
3262 /* The last transformation to one reg will be made in
3263 curr_insn_transform function. */
3264 end_sequence ();
3265 return false;
3267 else if (scale != 0)
3269 /* base + scale * index => base + new_reg,
3270 case (1) above.
3271 Index part of address may become invalid. For example, we
3272 changed pseudo on the equivalent memory and a subreg of the
3273 pseudo onto the memory of different mode for which the scale is
3274 prohibitted. */
3275 new_reg = index_part_to_reg (&ad);
3276 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3277 *ad.base_term, new_reg);
3279 else
3281 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3282 SCRATCH, SCRATCH);
3283 rtx addr = *ad.inner;
3285 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3286 /* addr => new_base. */
3287 lra_emit_move (new_reg, addr);
3288 *ad.inner = new_reg;
3290 *before = get_insns ();
3291 end_sequence ();
3292 return true;
3295 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3296 Use process_address_1 as a helper function. Return true for any
3297 RTL changes.
3299 If CHECK_ONLY_P is true, just check address correctness. Return
3300 false if the address correct. */
3301 static bool
3302 process_address (int nop, bool check_only_p,
3303 rtx_insn **before, rtx_insn **after)
3305 bool res = false;
3307 while (process_address_1 (nop, check_only_p, before, after))
3309 if (check_only_p)
3310 return true;
3311 res = true;
3313 return res;
3316 /* Emit insns to reload VALUE into a new register. VALUE is an
3317 auto-increment or auto-decrement RTX whose operand is a register or
3318 memory location; so reloading involves incrementing that location.
3319 IN is either identical to VALUE, or some cheaper place to reload
3320 value being incremented/decremented from.
3322 INC_AMOUNT is the number to increment or decrement by (always
3323 positive and ignored for POST_MODIFY/PRE_MODIFY).
3325 Return pseudo containing the result. */
3326 static rtx
3327 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3329 /* REG or MEM to be copied and incremented. */
3330 rtx incloc = XEXP (value, 0);
3331 /* Nonzero if increment after copying. */
3332 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3333 || GET_CODE (value) == POST_MODIFY);
3334 rtx_insn *last;
3335 rtx inc;
3336 rtx_insn *add_insn;
3337 int code;
3338 rtx real_in = in == value ? incloc : in;
3339 rtx result;
3340 bool plus_p = true;
3342 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3344 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3345 || GET_CODE (XEXP (value, 1)) == MINUS);
3346 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3347 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3348 inc = XEXP (XEXP (value, 1), 1);
3350 else
3352 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3353 inc_amount = -inc_amount;
3355 inc = GEN_INT (inc_amount);
3358 if (! post && REG_P (incloc))
3359 result = incloc;
3360 else
3361 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3362 "INC/DEC result");
3364 if (real_in != result)
3366 /* First copy the location to the result register. */
3367 lra_assert (REG_P (result));
3368 emit_insn (gen_move_insn (result, real_in));
3371 /* We suppose that there are insns to add/sub with the constant
3372 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3373 old reload worked with this assumption. If the assumption
3374 becomes wrong, we should use approach in function
3375 base_plus_disp_to_reg. */
3376 if (in == value)
3378 /* See if we can directly increment INCLOC. */
3379 last = get_last_insn ();
3380 add_insn = emit_insn (plus_p
3381 ? gen_add2_insn (incloc, inc)
3382 : gen_sub2_insn (incloc, inc));
3384 code = recog_memoized (add_insn);
3385 if (code >= 0)
3387 if (! post && result != incloc)
3388 emit_insn (gen_move_insn (result, incloc));
3389 return result;
3391 delete_insns_since (last);
3394 /* If couldn't do the increment directly, must increment in RESULT.
3395 The way we do this depends on whether this is pre- or
3396 post-increment. For pre-increment, copy INCLOC to the reload
3397 register, increment it there, then save back. */
3398 if (! post)
3400 if (real_in != result)
3401 emit_insn (gen_move_insn (result, real_in));
3402 if (plus_p)
3403 emit_insn (gen_add2_insn (result, inc));
3404 else
3405 emit_insn (gen_sub2_insn (result, inc));
3406 if (result != incloc)
3407 emit_insn (gen_move_insn (incloc, result));
3409 else
3411 /* Post-increment.
3413 Because this might be a jump insn or a compare, and because
3414 RESULT may not be available after the insn in an input
3415 reload, we must do the incrementing before the insn being
3416 reloaded for.
3418 We have already copied IN to RESULT. Increment the copy in
3419 RESULT, save that back, then decrement RESULT so it has
3420 the original value. */
3421 if (plus_p)
3422 emit_insn (gen_add2_insn (result, inc));
3423 else
3424 emit_insn (gen_sub2_insn (result, inc));
3425 emit_insn (gen_move_insn (incloc, result));
3426 /* Restore non-modified value for the result. We prefer this
3427 way because it does not require an additional hard
3428 register. */
3429 if (plus_p)
3431 if (CONST_INT_P (inc))
3432 emit_insn (gen_add2_insn (result,
3433 gen_int_mode (-INTVAL (inc),
3434 GET_MODE (result))));
3435 else
3436 emit_insn (gen_sub2_insn (result, inc));
3438 else
3439 emit_insn (gen_add2_insn (result, inc));
3441 return result;
3444 /* Return true if the current move insn does not need processing as we
3445 already know that it satisfies its constraints. */
3446 static bool
3447 simple_move_p (void)
3449 rtx dest, src;
3450 enum reg_class dclass, sclass;
3452 lra_assert (curr_insn_set != NULL_RTX);
3453 dest = SET_DEST (curr_insn_set);
3454 src = SET_SRC (curr_insn_set);
3455 return ((dclass = get_op_class (dest)) != NO_REGS
3456 && (sclass = get_op_class (src)) != NO_REGS
3457 /* The backend guarantees that register moves of cost 2
3458 never need reloads. */
3459 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3462 /* Swap operands NOP and NOP + 1. */
3463 static inline void
3464 swap_operands (int nop)
3466 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3467 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3468 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3469 std::swap (equiv_substition_p[nop], equiv_substition_p[nop + 1]);
3470 /* Swap the duplicates too. */
3471 lra_update_dup (curr_id, nop);
3472 lra_update_dup (curr_id, nop + 1);
3475 /* Main entry point of the constraint code: search the body of the
3476 current insn to choose the best alternative. It is mimicking insn
3477 alternative cost calculation model of former reload pass. That is
3478 because machine descriptions were written to use this model. This
3479 model can be changed in future. Make commutative operand exchange
3480 if it is chosen.
3482 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3483 constraints. Return true if any change happened during function
3484 call.
3486 If CHECK_ONLY_P is true then don't do any transformation. Just
3487 check that the insn satisfies all constraints. If the insn does
3488 not satisfy any constraint, return true. */
3489 static bool
3490 curr_insn_transform (bool check_only_p)
3492 int i, j, k;
3493 int n_operands;
3494 int n_alternatives;
3495 int n_outputs;
3496 int commutative;
3497 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3498 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3499 signed char outputs[MAX_RECOG_OPERANDS + 1];
3500 rtx_insn *before, *after;
3501 bool alt_p = false;
3502 /* Flag that the insn has been changed through a transformation. */
3503 bool change_p;
3504 bool sec_mem_p;
3505 #ifdef SECONDARY_MEMORY_NEEDED
3506 bool use_sec_mem_p;
3507 #endif
3508 int max_regno_before;
3509 int reused_alternative_num;
3511 curr_insn_set = single_set (curr_insn);
3512 if (curr_insn_set != NULL_RTX && simple_move_p ())
3513 return false;
3515 no_input_reloads_p = no_output_reloads_p = false;
3516 goal_alt_number = -1;
3517 change_p = sec_mem_p = false;
3518 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3519 reloads; neither are insns that SET cc0. Insns that use CC0 are
3520 not allowed to have any input reloads. */
3521 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3522 no_output_reloads_p = true;
3524 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3525 no_input_reloads_p = true;
3526 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3527 no_output_reloads_p = true;
3529 n_operands = curr_static_id->n_operands;
3530 n_alternatives = curr_static_id->n_alternatives;
3532 /* Just return "no reloads" if insn has no operands with
3533 constraints. */
3534 if (n_operands == 0 || n_alternatives == 0)
3535 return false;
3537 max_regno_before = max_reg_num ();
3539 for (i = 0; i < n_operands; i++)
3541 goal_alt_matched[i][0] = -1;
3542 goal_alt_matches[i] = -1;
3545 commutative = curr_static_id->commutative;
3547 /* Now see what we need for pseudos that didn't get hard regs or got
3548 the wrong kind of hard reg. For this, we must consider all the
3549 operands together against the register constraints. */
3551 best_losers = best_overall = INT_MAX;
3552 best_reload_sum = 0;
3554 curr_swapped = false;
3555 goal_alt_swapped = false;
3557 if (! check_only_p)
3558 /* Make equivalence substitution and memory subreg elimination
3559 before address processing because an address legitimacy can
3560 depend on memory mode. */
3561 for (i = 0; i < n_operands; i++)
3563 rtx op, subst, old;
3564 bool op_change_p = false;
3566 if (curr_static_id->operand[i].is_operator)
3567 continue;
3569 old = op = *curr_id->operand_loc[i];
3570 if (GET_CODE (old) == SUBREG)
3571 old = SUBREG_REG (old);
3572 subst = get_equiv_with_elimination (old, curr_insn);
3573 original_subreg_reg_mode[i] = VOIDmode;
3574 equiv_substition_p[i] = false;
3575 if (subst != old)
3577 equiv_substition_p[i] = true;
3578 subst = copy_rtx (subst);
3579 lra_assert (REG_P (old));
3580 if (GET_CODE (op) != SUBREG)
3581 *curr_id->operand_loc[i] = subst;
3582 else
3584 SUBREG_REG (op) = subst;
3585 if (GET_MODE (subst) == VOIDmode)
3586 original_subreg_reg_mode[i] = GET_MODE (old);
3588 if (lra_dump_file != NULL)
3590 fprintf (lra_dump_file,
3591 "Changing pseudo %d in operand %i of insn %u on equiv ",
3592 REGNO (old), i, INSN_UID (curr_insn));
3593 dump_value_slim (lra_dump_file, subst, 1);
3594 fprintf (lra_dump_file, "\n");
3596 op_change_p = change_p = true;
3598 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3600 change_p = true;
3601 lra_update_dup (curr_id, i);
3605 /* Reload address registers and displacements. We do it before
3606 finding an alternative because of memory constraints. */
3607 before = after = NULL;
3608 for (i = 0; i < n_operands; i++)
3609 if (! curr_static_id->operand[i].is_operator
3610 && process_address (i, check_only_p, &before, &after))
3612 if (check_only_p)
3613 return true;
3614 change_p = true;
3615 lra_update_dup (curr_id, i);
3618 if (change_p)
3619 /* If we've changed the instruction then any alternative that
3620 we chose previously may no longer be valid. */
3621 lra_set_used_insn_alternative (curr_insn, -1);
3623 if (! check_only_p && curr_insn_set != NULL_RTX
3624 && check_and_process_move (&change_p, &sec_mem_p))
3625 return change_p;
3627 try_swapped:
3629 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3630 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3631 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3632 reused_alternative_num, INSN_UID (curr_insn));
3634 if (process_alt_operands (reused_alternative_num))
3635 alt_p = true;
3637 if (check_only_p)
3638 return ! alt_p || best_losers != 0;
3640 /* If insn is commutative (it's safe to exchange a certain pair of
3641 operands) then we need to try each alternative twice, the second
3642 time matching those two operands as if we had exchanged them. To
3643 do this, really exchange them in operands.
3645 If we have just tried the alternatives the second time, return
3646 operands to normal and drop through. */
3648 if (reused_alternative_num < 0 && commutative >= 0)
3650 curr_swapped = !curr_swapped;
3651 if (curr_swapped)
3653 swap_operands (commutative);
3654 goto try_swapped;
3656 else
3657 swap_operands (commutative);
3660 if (! alt_p && ! sec_mem_p)
3662 /* No alternative works with reloads?? */
3663 if (INSN_CODE (curr_insn) >= 0)
3664 fatal_insn ("unable to generate reloads for:", curr_insn);
3665 error_for_asm (curr_insn,
3666 "inconsistent operand constraints in an %<asm%>");
3667 /* Avoid further trouble with this insn. */
3668 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3669 lra_invalidate_insn_data (curr_insn);
3670 return true;
3673 /* If the best alternative is with operands 1 and 2 swapped, swap
3674 them. Update the operand numbers of any reloads already
3675 pushed. */
3677 if (goal_alt_swapped)
3679 if (lra_dump_file != NULL)
3680 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3681 INSN_UID (curr_insn));
3683 /* Swap the duplicates too. */
3684 swap_operands (commutative);
3685 change_p = true;
3688 #ifdef SECONDARY_MEMORY_NEEDED
3689 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3690 too conservatively. So we use the secondary memory only if there
3691 is no any alternative without reloads. */
3692 use_sec_mem_p = false;
3693 if (! alt_p)
3694 use_sec_mem_p = true;
3695 else if (sec_mem_p)
3697 for (i = 0; i < n_operands; i++)
3698 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3699 break;
3700 use_sec_mem_p = i < n_operands;
3703 if (use_sec_mem_p)
3705 int in = -1, out = -1;
3706 rtx new_reg, src, dest, rld;
3707 machine_mode sec_mode, rld_mode;
3709 lra_assert (curr_insn_set != NULL_RTX && sec_mem_p);
3710 dest = SET_DEST (curr_insn_set);
3711 src = SET_SRC (curr_insn_set);
3712 for (i = 0; i < n_operands; i++)
3713 if (*curr_id->operand_loc[i] == dest)
3714 out = i;
3715 else if (*curr_id->operand_loc[i] == src)
3716 in = i;
3717 for (i = 0; i < curr_static_id->n_dups; i++)
3718 if (out < 0 && *curr_id->dup_loc[i] == dest)
3719 out = curr_static_id->dup_num[i];
3720 else if (in < 0 && *curr_id->dup_loc[i] == src)
3721 in = curr_static_id->dup_num[i];
3722 lra_assert (out >= 0 && in >= 0
3723 && curr_static_id->operand[out].type == OP_OUT
3724 && curr_static_id->operand[in].type == OP_IN);
3725 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3726 ? dest : src);
3727 rld_mode = GET_MODE (rld);
3728 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3729 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3730 #else
3731 sec_mode = rld_mode;
3732 #endif
3733 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3734 NO_REGS, "secondary");
3735 /* If the mode is changed, it should be wider. */
3736 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3737 if (sec_mode != rld_mode)
3739 /* If the target says specifically to use another mode for
3740 secondary memory moves we can not reuse the original
3741 insn. */
3742 after = emit_spill_move (false, new_reg, dest);
3743 lra_process_new_insns (curr_insn, NULL, after,
3744 "Inserting the sec. move");
3745 /* We may have non null BEFORE here (e.g. after address
3746 processing. */
3747 push_to_sequence (before);
3748 before = emit_spill_move (true, new_reg, src);
3749 emit_insn (before);
3750 before = get_insns ();
3751 end_sequence ();
3752 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3753 lra_set_insn_deleted (curr_insn);
3755 else if (dest == rld)
3757 *curr_id->operand_loc[out] = new_reg;
3758 lra_update_dup (curr_id, out);
3759 after = emit_spill_move (false, new_reg, dest);
3760 lra_process_new_insns (curr_insn, NULL, after,
3761 "Inserting the sec. move");
3763 else
3765 *curr_id->operand_loc[in] = new_reg;
3766 lra_update_dup (curr_id, in);
3767 /* See comments above. */
3768 push_to_sequence (before);
3769 before = emit_spill_move (true, new_reg, src);
3770 emit_insn (before);
3771 before = get_insns ();
3772 end_sequence ();
3773 lra_process_new_insns (curr_insn, before, NULL,
3774 "Inserting the sec. move");
3776 lra_update_insn_regno_info (curr_insn);
3777 return true;
3779 #endif
3781 lra_assert (goal_alt_number >= 0);
3782 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3784 if (lra_dump_file != NULL)
3786 const char *p;
3788 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3789 goal_alt_number, INSN_UID (curr_insn));
3790 for (i = 0; i < n_operands; i++)
3792 p = (curr_static_id->operand_alternative
3793 [goal_alt_number * n_operands + i].constraint);
3794 if (*p == '\0')
3795 continue;
3796 fprintf (lra_dump_file, " (%d) ", i);
3797 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3798 fputc (*p, lra_dump_file);
3800 if (INSN_CODE (curr_insn) >= 0
3801 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3802 fprintf (lra_dump_file, " {%s}", p);
3803 if (curr_id->sp_offset != 0)
3804 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3805 curr_id->sp_offset);
3806 fprintf (lra_dump_file, "\n");
3809 /* Right now, for any pair of operands I and J that are required to
3810 match, with J < I, goal_alt_matches[I] is J. Add I to
3811 goal_alt_matched[J]. */
3813 for (i = 0; i < n_operands; i++)
3814 if ((j = goal_alt_matches[i]) >= 0)
3816 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3818 /* We allow matching one output operand and several input
3819 operands. */
3820 lra_assert (k == 0
3821 || (curr_static_id->operand[j].type == OP_OUT
3822 && curr_static_id->operand[i].type == OP_IN
3823 && (curr_static_id->operand
3824 [goal_alt_matched[j][0]].type == OP_IN)));
3825 goal_alt_matched[j][k] = i;
3826 goal_alt_matched[j][k + 1] = -1;
3829 for (i = 0; i < n_operands; i++)
3830 goal_alt_win[i] |= goal_alt_match_win[i];
3832 /* Any constants that aren't allowed and can't be reloaded into
3833 registers are here changed into memory references. */
3834 for (i = 0; i < n_operands; i++)
3835 if (goal_alt_win[i])
3837 int regno;
3838 enum reg_class new_class;
3839 rtx reg = *curr_id->operand_loc[i];
3841 if (GET_CODE (reg) == SUBREG)
3842 reg = SUBREG_REG (reg);
3844 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3846 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3848 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3850 lra_assert (ok_p);
3851 lra_change_class (regno, new_class, " Change to", true);
3855 else
3857 const char *constraint;
3858 char c;
3859 rtx op = *curr_id->operand_loc[i];
3860 rtx subreg = NULL_RTX;
3861 machine_mode mode = curr_operand_mode[i];
3863 if (GET_CODE (op) == SUBREG)
3865 subreg = op;
3866 op = SUBREG_REG (op);
3867 mode = GET_MODE (op);
3870 if (CONST_POOL_OK_P (mode, op)
3871 && ((targetm.preferred_reload_class
3872 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3873 || no_input_reloads_p))
3875 rtx tem = force_const_mem (mode, op);
3877 change_p = true;
3878 if (subreg != NULL_RTX)
3879 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3881 *curr_id->operand_loc[i] = tem;
3882 lra_update_dup (curr_id, i);
3883 process_address (i, false, &before, &after);
3885 /* If the alternative accepts constant pool refs directly
3886 there will be no reload needed at all. */
3887 if (subreg != NULL_RTX)
3888 continue;
3889 /* Skip alternatives before the one requested. */
3890 constraint = (curr_static_id->operand_alternative
3891 [goal_alt_number * n_operands + i].constraint);
3892 for (;
3893 (c = *constraint) && c != ',' && c != '#';
3894 constraint += CONSTRAINT_LEN (c, constraint))
3896 enum constraint_num cn = lookup_constraint (constraint);
3897 if ((insn_extra_memory_constraint (cn)
3898 || insn_extra_special_memory_constraint (cn))
3899 && satisfies_memory_constraint_p (tem, cn))
3900 break;
3902 if (c == '\0' || c == ',' || c == '#')
3903 continue;
3905 goal_alt_win[i] = true;
3909 n_outputs = 0;
3910 outputs[0] = -1;
3911 for (i = 0; i < n_operands; i++)
3913 int regno;
3914 bool optional_p = false;
3915 rtx old, new_reg;
3916 rtx op = *curr_id->operand_loc[i];
3918 if (goal_alt_win[i])
3920 if (goal_alt[i] == NO_REGS
3921 && REG_P (op)
3922 /* When we assign NO_REGS it means that we will not
3923 assign a hard register to the scratch pseudo by
3924 assigment pass and the scratch pseudo will be
3925 spilled. Spilled scratch pseudos are transformed
3926 back to scratches at the LRA end. */
3927 && lra_former_scratch_operand_p (curr_insn, i)
3928 && lra_former_scratch_p (REGNO (op)))
3930 int regno = REGNO (op);
3931 lra_change_class (regno, NO_REGS, " Change to", true);
3932 if (lra_get_regno_hard_regno (regno) >= 0)
3933 /* We don't have to mark all insn affected by the
3934 spilled pseudo as there is only one such insn, the
3935 current one. */
3936 reg_renumber[regno] = -1;
3937 lra_assert (bitmap_single_bit_set_p
3938 (&lra_reg_info[REGNO (op)].insn_bitmap));
3940 /* We can do an optional reload. If the pseudo got a hard
3941 reg, we might improve the code through inheritance. If
3942 it does not get a hard register we coalesce memory/memory
3943 moves later. Ignore move insns to avoid cycling. */
3944 if (! lra_simple_p
3945 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3946 && goal_alt[i] != NO_REGS && REG_P (op)
3947 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3948 && regno < new_regno_start
3949 && ! lra_former_scratch_p (regno)
3950 && reg_renumber[regno] < 0
3951 /* Check that the optional reload pseudo will be able to
3952 hold given mode value. */
3953 && ! (prohibited_class_reg_set_mode_p
3954 (goal_alt[i], reg_class_contents[goal_alt[i]],
3955 PSEUDO_REGNO_MODE (regno)))
3956 && (curr_insn_set == NULL_RTX
3957 || !((REG_P (SET_SRC (curr_insn_set))
3958 || MEM_P (SET_SRC (curr_insn_set))
3959 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3960 && (REG_P (SET_DEST (curr_insn_set))
3961 || MEM_P (SET_DEST (curr_insn_set))
3962 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3963 optional_p = true;
3964 else
3965 continue;
3968 /* Operands that match previous ones have already been handled. */
3969 if (goal_alt_matches[i] >= 0)
3970 continue;
3972 /* We should not have an operand with a non-offsettable address
3973 appearing where an offsettable address will do. It also may
3974 be a case when the address should be special in other words
3975 not a general one (e.g. it needs no index reg). */
3976 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3978 enum reg_class rclass;
3979 rtx *loc = &XEXP (op, 0);
3980 enum rtx_code code = GET_CODE (*loc);
3982 push_to_sequence (before);
3983 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3984 MEM, SCRATCH);
3985 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3986 new_reg = emit_inc (rclass, *loc, *loc,
3987 /* This value does not matter for MODIFY. */
3988 GET_MODE_SIZE (GET_MODE (op)));
3989 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3990 "offsetable address", &new_reg))
3991 lra_emit_move (new_reg, *loc);
3992 before = get_insns ();
3993 end_sequence ();
3994 *loc = new_reg;
3995 lra_update_dup (curr_id, i);
3997 else if (goal_alt_matched[i][0] == -1)
3999 machine_mode mode;
4000 rtx reg, *loc;
4001 int hard_regno, byte;
4002 enum op_type type = curr_static_id->operand[i].type;
4004 loc = curr_id->operand_loc[i];
4005 mode = curr_operand_mode[i];
4006 if (GET_CODE (*loc) == SUBREG)
4008 reg = SUBREG_REG (*loc);
4009 byte = SUBREG_BYTE (*loc);
4010 if (REG_P (reg)
4011 /* Strict_low_part requires reload the register not
4012 the sub-register. */
4013 && (curr_static_id->operand[i].strict_low
4014 || (GET_MODE_SIZE (mode)
4015 <= GET_MODE_SIZE (GET_MODE (reg))
4016 && (hard_regno
4017 = get_try_hard_regno (REGNO (reg))) >= 0
4018 && (simplify_subreg_regno
4019 (hard_regno,
4020 GET_MODE (reg), byte, mode) < 0)
4021 && (goal_alt[i] == NO_REGS
4022 || (simplify_subreg_regno
4023 (ira_class_hard_regs[goal_alt[i]][0],
4024 GET_MODE (reg), byte, mode) >= 0)))))
4026 if (type == OP_OUT)
4027 type = OP_INOUT;
4028 loc = &SUBREG_REG (*loc);
4029 mode = GET_MODE (*loc);
4032 old = *loc;
4033 if (get_reload_reg (type, mode, old, goal_alt[i],
4034 loc != curr_id->operand_loc[i], "", &new_reg)
4035 && type != OP_OUT)
4037 push_to_sequence (before);
4038 lra_emit_move (new_reg, old);
4039 before = get_insns ();
4040 end_sequence ();
4042 *loc = new_reg;
4043 if (type != OP_IN
4044 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
4046 start_sequence ();
4047 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
4048 emit_insn (after);
4049 after = get_insns ();
4050 end_sequence ();
4051 *loc = new_reg;
4053 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
4054 if (goal_alt_dont_inherit_ops[j] == i)
4056 lra_set_regno_unique_value (REGNO (new_reg));
4057 break;
4059 lra_update_dup (curr_id, i);
4061 else if (curr_static_id->operand[i].type == OP_IN
4062 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4063 == OP_OUT))
4065 /* generate reloads for input and matched outputs. */
4066 match_inputs[0] = i;
4067 match_inputs[1] = -1;
4068 match_reload (goal_alt_matched[i][0], match_inputs, outputs,
4069 goal_alt[i], &before, &after,
4070 curr_static_id->operand_alternative
4071 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
4072 .earlyclobber);
4074 else if (curr_static_id->operand[i].type == OP_OUT
4075 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4076 == OP_IN))
4077 /* Generate reloads for output and matched inputs. */
4078 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i], &before,
4079 &after, curr_static_id->operand_alternative
4080 [goal_alt_number * n_operands + i].earlyclobber);
4081 else if (curr_static_id->operand[i].type == OP_IN
4082 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4083 == OP_IN))
4085 /* Generate reloads for matched inputs. */
4086 match_inputs[0] = i;
4087 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
4088 match_inputs[j + 1] = k;
4089 match_inputs[j + 1] = -1;
4090 match_reload (-1, match_inputs, outputs, goal_alt[i], &before,
4091 &after, false);
4093 else
4094 /* We must generate code in any case when function
4095 process_alt_operands decides that it is possible. */
4096 gcc_unreachable ();
4098 /* Memorise processed outputs so that output remaining to be processed
4099 can avoid using the same register value (see match_reload). */
4100 if (curr_static_id->operand[i].type == OP_OUT)
4102 outputs[n_outputs++] = i;
4103 outputs[n_outputs] = -1;
4106 if (optional_p)
4108 lra_assert (REG_P (op));
4109 regno = REGNO (op);
4110 op = *curr_id->operand_loc[i]; /* Substitution. */
4111 if (GET_CODE (op) == SUBREG)
4112 op = SUBREG_REG (op);
4113 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
4114 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
4115 lra_reg_info[REGNO (op)].restore_regno = regno;
4116 if (lra_dump_file != NULL)
4117 fprintf (lra_dump_file,
4118 " Making reload reg %d for reg %d optional\n",
4119 REGNO (op), regno);
4122 if (before != NULL_RTX || after != NULL_RTX
4123 || max_regno_before != max_reg_num ())
4124 change_p = true;
4125 if (change_p)
4127 lra_update_operator_dups (curr_id);
4128 /* Something changes -- process the insn. */
4129 lra_update_insn_regno_info (curr_insn);
4131 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
4132 return change_p;
4135 /* Return true if INSN satisfies all constraints. In other words, no
4136 reload insns are needed. */
4137 bool
4138 lra_constrain_insn (rtx_insn *insn)
4140 int saved_new_regno_start = new_regno_start;
4141 int saved_new_insn_uid_start = new_insn_uid_start;
4142 bool change_p;
4144 curr_insn = insn;
4145 curr_id = lra_get_insn_recog_data (curr_insn);
4146 curr_static_id = curr_id->insn_static_data;
4147 new_insn_uid_start = get_max_uid ();
4148 new_regno_start = max_reg_num ();
4149 change_p = curr_insn_transform (true);
4150 new_regno_start = saved_new_regno_start;
4151 new_insn_uid_start = saved_new_insn_uid_start;
4152 return ! change_p;
4155 /* Return true if X is in LIST. */
4156 static bool
4157 in_list_p (rtx x, rtx list)
4159 for (; list != NULL_RTX; list = XEXP (list, 1))
4160 if (XEXP (list, 0) == x)
4161 return true;
4162 return false;
4165 /* Return true if X contains an allocatable hard register (if
4166 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
4167 static bool
4168 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
4170 int i, j;
4171 const char *fmt;
4172 enum rtx_code code;
4174 code = GET_CODE (x);
4175 if (REG_P (x))
4177 int regno = REGNO (x);
4178 HARD_REG_SET alloc_regs;
4180 if (hard_reg_p)
4182 if (regno >= FIRST_PSEUDO_REGISTER)
4183 regno = lra_get_regno_hard_regno (regno);
4184 if (regno < 0)
4185 return false;
4186 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
4187 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
4189 else
4191 if (regno < FIRST_PSEUDO_REGISTER)
4192 return false;
4193 if (! spilled_p)
4194 return true;
4195 return lra_get_regno_hard_regno (regno) < 0;
4198 fmt = GET_RTX_FORMAT (code);
4199 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4201 if (fmt[i] == 'e')
4203 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4204 return true;
4206 else if (fmt[i] == 'E')
4208 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4209 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4210 return true;
4213 return false;
4216 /* Process all regs in location *LOC and change them on equivalent
4217 substitution. Return true if any change was done. */
4218 static bool
4219 loc_equivalence_change_p (rtx *loc)
4221 rtx subst, reg, x = *loc;
4222 bool result = false;
4223 enum rtx_code code = GET_CODE (x);
4224 const char *fmt;
4225 int i, j;
4227 if (code == SUBREG)
4229 reg = SUBREG_REG (x);
4230 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4231 && GET_MODE (subst) == VOIDmode)
4233 /* We cannot reload debug location. Simplify subreg here
4234 while we know the inner mode. */
4235 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4236 GET_MODE (reg), SUBREG_BYTE (x));
4237 return true;
4240 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4242 *loc = subst;
4243 return true;
4246 /* Scan all the operand sub-expressions. */
4247 fmt = GET_RTX_FORMAT (code);
4248 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4250 if (fmt[i] == 'e')
4251 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4252 else if (fmt[i] == 'E')
4253 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4254 result
4255 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4257 return result;
4260 /* Similar to loc_equivalence_change_p, but for use as
4261 simplify_replace_fn_rtx callback. DATA is insn for which the
4262 elimination is done. If it null we don't do the elimination. */
4263 static rtx
4264 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4266 if (!REG_P (loc))
4267 return NULL_RTX;
4269 rtx subst = (data == NULL
4270 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4271 if (subst != loc)
4272 return subst;
4274 return NULL_RTX;
4277 /* Maximum number of generated reload insns per an insn. It is for
4278 preventing this pass cycling in a bug case. */
4279 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4281 /* The current iteration number of this LRA pass. */
4282 int lra_constraint_iter;
4284 /* True if we substituted equiv which needs checking register
4285 allocation correctness because the equivalent value contains
4286 allocatable hard registers or when we restore multi-register
4287 pseudo. */
4288 bool lra_risky_transformations_p;
4290 /* Return true if REGNO is referenced in more than one block. */
4291 static bool
4292 multi_block_pseudo_p (int regno)
4294 basic_block bb = NULL;
4295 unsigned int uid;
4296 bitmap_iterator bi;
4298 if (regno < FIRST_PSEUDO_REGISTER)
4299 return false;
4301 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4302 if (bb == NULL)
4303 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4304 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4305 return true;
4306 return false;
4309 /* Return true if LIST contains a deleted insn. */
4310 static bool
4311 contains_deleted_insn_p (rtx_insn_list *list)
4313 for (; list != NULL_RTX; list = list->next ())
4314 if (NOTE_P (list->insn ())
4315 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4316 return true;
4317 return false;
4320 /* Return true if X contains a pseudo dying in INSN. */
4321 static bool
4322 dead_pseudo_p (rtx x, rtx_insn *insn)
4324 int i, j;
4325 const char *fmt;
4326 enum rtx_code code;
4328 if (REG_P (x))
4329 return (insn != NULL_RTX
4330 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4331 code = GET_CODE (x);
4332 fmt = GET_RTX_FORMAT (code);
4333 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4335 if (fmt[i] == 'e')
4337 if (dead_pseudo_p (XEXP (x, i), insn))
4338 return true;
4340 else if (fmt[i] == 'E')
4342 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4343 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4344 return true;
4347 return false;
4350 /* Return true if INSN contains a dying pseudo in INSN right hand
4351 side. */
4352 static bool
4353 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4355 rtx set = single_set (insn);
4357 gcc_assert (set != NULL);
4358 return dead_pseudo_p (SET_SRC (set), insn);
4361 /* Return true if any init insn of REGNO contains a dying pseudo in
4362 insn right hand side. */
4363 static bool
4364 init_insn_rhs_dead_pseudo_p (int regno)
4366 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4368 if (insns == NULL)
4369 return false;
4370 for (; insns != NULL_RTX; insns = insns->next ())
4371 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4372 return true;
4373 return false;
4376 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4377 reverse only if we have one init insn with given REGNO as a
4378 source. */
4379 static bool
4380 reverse_equiv_p (int regno)
4382 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4383 rtx set;
4385 if (insns == NULL)
4386 return false;
4387 if (! INSN_P (insns->insn ())
4388 || insns->next () != NULL)
4389 return false;
4390 if ((set = single_set (insns->insn ())) == NULL_RTX)
4391 return false;
4392 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4395 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4396 call this function only for non-reverse equivalence. */
4397 static bool
4398 contains_reloaded_insn_p (int regno)
4400 rtx set;
4401 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4403 for (; list != NULL; list = list->next ())
4404 if ((set = single_set (list->insn ())) == NULL_RTX
4405 || ! REG_P (SET_DEST (set))
4406 || (int) REGNO (SET_DEST (set)) != regno)
4407 return true;
4408 return false;
4411 /* Entry function of LRA constraint pass. Return true if the
4412 constraint pass did change the code. */
4413 bool
4414 lra_constraints (bool first_p)
4416 bool changed_p;
4417 int i, hard_regno, new_insns_num;
4418 unsigned int min_len, new_min_len, uid;
4419 rtx set, x, reg, dest_reg;
4420 basic_block last_bb;
4421 bitmap_head equiv_insn_bitmap;
4422 bitmap_iterator bi;
4424 lra_constraint_iter++;
4425 if (lra_dump_file != NULL)
4426 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4427 lra_constraint_iter);
4428 changed_p = false;
4429 if (pic_offset_table_rtx
4430 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4431 lra_risky_transformations_p = true;
4432 else
4433 lra_risky_transformations_p = false;
4434 new_insn_uid_start = get_max_uid ();
4435 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4436 /* Mark used hard regs for target stack size calulations. */
4437 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4438 if (lra_reg_info[i].nrefs != 0
4439 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4441 int j, nregs;
4443 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4444 for (j = 0; j < nregs; j++)
4445 df_set_regs_ever_live (hard_regno + j, true);
4447 /* Do elimination before the equivalence processing as we can spill
4448 some pseudos during elimination. */
4449 lra_eliminate (false, first_p);
4450 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4451 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4452 if (lra_reg_info[i].nrefs != 0)
4454 ira_reg_equiv[i].profitable_p = true;
4455 reg = regno_reg_rtx[i];
4456 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4458 bool pseudo_p = contains_reg_p (x, false, false);
4460 /* After RTL transformation, we can not guarantee that
4461 pseudo in the substitution was not reloaded which might
4462 make equivalence invalid. For example, in reverse
4463 equiv of p0
4465 p0 <- ...
4467 equiv_mem <- p0
4469 the memory address register was reloaded before the 2nd
4470 insn. */
4471 if ((! first_p && pseudo_p)
4472 /* We don't use DF for compilation speed sake. So it
4473 is problematic to update live info when we use an
4474 equivalence containing pseudos in more than one
4475 BB. */
4476 || (pseudo_p && multi_block_pseudo_p (i))
4477 /* If an init insn was deleted for some reason, cancel
4478 the equiv. We could update the equiv insns after
4479 transformations including an equiv insn deletion
4480 but it is not worthy as such cases are extremely
4481 rare. */
4482 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4483 /* If it is not a reverse equivalence, we check that a
4484 pseudo in rhs of the init insn is not dying in the
4485 insn. Otherwise, the live info at the beginning of
4486 the corresponding BB might be wrong after we
4487 removed the insn. When the equiv can be a
4488 constant, the right hand side of the init insn can
4489 be a pseudo. */
4490 || (! reverse_equiv_p (i)
4491 && (init_insn_rhs_dead_pseudo_p (i)
4492 /* If we reloaded the pseudo in an equivalence
4493 init insn, we can not remove the equiv init
4494 insns and the init insns might write into
4495 const memory in this case. */
4496 || contains_reloaded_insn_p (i)))
4497 /* Prevent access beyond equivalent memory for
4498 paradoxical subregs. */
4499 || (MEM_P (x)
4500 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4501 > GET_MODE_SIZE (GET_MODE (x))))
4502 || (pic_offset_table_rtx
4503 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4504 && (targetm.preferred_reload_class
4505 (x, lra_get_allocno_class (i)) == NO_REGS))
4506 || contains_symbol_ref_p (x))))
4507 ira_reg_equiv[i].defined_p = false;
4508 if (contains_reg_p (x, false, true))
4509 ira_reg_equiv[i].profitable_p = false;
4510 if (get_equiv (reg) != reg)
4511 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4514 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4515 update_equiv (i);
4516 /* We should add all insns containing pseudos which should be
4517 substituted by their equivalences. */
4518 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4519 lra_push_insn_by_uid (uid);
4520 min_len = lra_insn_stack_length ();
4521 new_insns_num = 0;
4522 last_bb = NULL;
4523 changed_p = false;
4524 while ((new_min_len = lra_insn_stack_length ()) != 0)
4526 curr_insn = lra_pop_insn ();
4527 --new_min_len;
4528 curr_bb = BLOCK_FOR_INSN (curr_insn);
4529 if (curr_bb != last_bb)
4531 last_bb = curr_bb;
4532 bb_reload_num = lra_curr_reload_num;
4534 if (min_len > new_min_len)
4536 min_len = new_min_len;
4537 new_insns_num = 0;
4539 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4540 internal_error
4541 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4542 MAX_RELOAD_INSNS_NUMBER);
4543 new_insns_num++;
4544 if (DEBUG_INSN_P (curr_insn))
4546 /* We need to check equivalence in debug insn and change
4547 pseudo to the equivalent value if necessary. */
4548 curr_id = lra_get_insn_recog_data (curr_insn);
4549 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4551 rtx old = *curr_id->operand_loc[0];
4552 *curr_id->operand_loc[0]
4553 = simplify_replace_fn_rtx (old, NULL_RTX,
4554 loc_equivalence_callback, curr_insn);
4555 if (old != *curr_id->operand_loc[0])
4557 lra_update_insn_regno_info (curr_insn);
4558 changed_p = true;
4562 else if (INSN_P (curr_insn))
4564 if ((set = single_set (curr_insn)) != NULL_RTX)
4566 dest_reg = SET_DEST (set);
4567 /* The equivalence pseudo could be set up as SUBREG in a
4568 case when it is a call restore insn in a mode
4569 different from the pseudo mode. */
4570 if (GET_CODE (dest_reg) == SUBREG)
4571 dest_reg = SUBREG_REG (dest_reg);
4572 if ((REG_P (dest_reg)
4573 && (x = get_equiv (dest_reg)) != dest_reg
4574 /* Remove insns which set up a pseudo whose value
4575 can not be changed. Such insns might be not in
4576 init_insns because we don't update equiv data
4577 during insn transformations.
4579 As an example, let suppose that a pseudo got
4580 hard register and on the 1st pass was not
4581 changed to equivalent constant. We generate an
4582 additional insn setting up the pseudo because of
4583 secondary memory movement. Then the pseudo is
4584 spilled and we use the equiv constant. In this
4585 case we should remove the additional insn and
4586 this insn is not init_insns list. */
4587 && (! MEM_P (x) || MEM_READONLY_P (x)
4588 /* Check that this is actually an insn setting
4589 up the equivalence. */
4590 || in_list_p (curr_insn,
4591 ira_reg_equiv
4592 [REGNO (dest_reg)].init_insns)))
4593 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4594 && in_list_p (curr_insn,
4595 ira_reg_equiv
4596 [REGNO (SET_SRC (set))].init_insns)))
4598 /* This is equiv init insn of pseudo which did not get a
4599 hard register -- remove the insn. */
4600 if (lra_dump_file != NULL)
4602 fprintf (lra_dump_file,
4603 " Removing equiv init insn %i (freq=%d)\n",
4604 INSN_UID (curr_insn),
4605 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4606 dump_insn_slim (lra_dump_file, curr_insn);
4608 if (contains_reg_p (x, true, false))
4609 lra_risky_transformations_p = true;
4610 lra_set_insn_deleted (curr_insn);
4611 continue;
4614 curr_id = lra_get_insn_recog_data (curr_insn);
4615 curr_static_id = curr_id->insn_static_data;
4616 init_curr_insn_input_reloads ();
4617 init_curr_operand_mode ();
4618 if (curr_insn_transform (false))
4619 changed_p = true;
4620 /* Check non-transformed insns too for equiv change as USE
4621 or CLOBBER don't need reloads but can contain pseudos
4622 being changed on their equivalences. */
4623 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4624 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4626 lra_update_insn_regno_info (curr_insn);
4627 changed_p = true;
4631 bitmap_clear (&equiv_insn_bitmap);
4632 /* If we used a new hard regno, changed_p should be true because the
4633 hard reg is assigned to a new pseudo. */
4634 if (flag_checking && !changed_p)
4636 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4637 if (lra_reg_info[i].nrefs != 0
4638 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4640 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4642 for (j = 0; j < nregs; j++)
4643 lra_assert (df_regs_ever_live_p (hard_regno + j));
4646 return changed_p;
4649 /* Initiate the LRA constraint pass. It is done once per
4650 function. */
4651 void
4652 lra_constraints_init (void)
4656 /* Finalize the LRA constraint pass. It is done once per
4657 function. */
4658 void
4659 lra_constraints_finish (void)
4665 /* This page contains code to do inheritance/split
4666 transformations. */
4668 /* Number of reloads passed so far in current EBB. */
4669 static int reloads_num;
4671 /* Number of calls passed so far in current EBB. */
4672 static int calls_num;
4674 /* Current reload pseudo check for validity of elements in
4675 USAGE_INSNS. */
4676 static int curr_usage_insns_check;
4678 /* Info about last usage of registers in EBB to do inheritance/split
4679 transformation. Inheritance transformation is done from a spilled
4680 pseudo and split transformations from a hard register or a pseudo
4681 assigned to a hard register. */
4682 struct usage_insns
4684 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4685 value INSNS is valid. The insns is chain of optional debug insns
4686 and a finishing non-debug insn using the corresponding reg. The
4687 value is also used to mark the registers which are set up in the
4688 current insn. The negated insn uid is used for this. */
4689 int check;
4690 /* Value of global reloads_num at the last insn in INSNS. */
4691 int reloads_num;
4692 /* Value of global reloads_nums at the last insn in INSNS. */
4693 int calls_num;
4694 /* It can be true only for splitting. And it means that the restore
4695 insn should be put after insn given by the following member. */
4696 bool after_p;
4697 /* Next insns in the current EBB which use the original reg and the
4698 original reg value is not changed between the current insn and
4699 the next insns. In order words, e.g. for inheritance, if we need
4700 to use the original reg value again in the next insns we can try
4701 to use the value in a hard register from a reload insn of the
4702 current insn. */
4703 rtx insns;
4706 /* Map: regno -> corresponding pseudo usage insns. */
4707 static struct usage_insns *usage_insns;
4709 static void
4710 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4712 usage_insns[regno].check = curr_usage_insns_check;
4713 usage_insns[regno].insns = insn;
4714 usage_insns[regno].reloads_num = reloads_num;
4715 usage_insns[regno].calls_num = calls_num;
4716 usage_insns[regno].after_p = after_p;
4719 /* The function is used to form list REGNO usages which consists of
4720 optional debug insns finished by a non-debug insn using REGNO.
4721 RELOADS_NUM is current number of reload insns processed so far. */
4722 static void
4723 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
4725 rtx next_usage_insns;
4727 if (usage_insns[regno].check == curr_usage_insns_check
4728 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4729 && DEBUG_INSN_P (insn))
4731 /* Check that we did not add the debug insn yet. */
4732 if (next_usage_insns != insn
4733 && (GET_CODE (next_usage_insns) != INSN_LIST
4734 || XEXP (next_usage_insns, 0) != insn))
4735 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4736 next_usage_insns);
4738 else if (NONDEBUG_INSN_P (insn))
4739 setup_next_usage_insn (regno, insn, reloads_num, false);
4740 else
4741 usage_insns[regno].check = 0;
4744 /* Return first non-debug insn in list USAGE_INSNS. */
4745 static rtx_insn *
4746 skip_usage_debug_insns (rtx usage_insns)
4748 rtx insn;
4750 /* Skip debug insns. */
4751 for (insn = usage_insns;
4752 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4753 insn = XEXP (insn, 1))
4755 return safe_as_a <rtx_insn *> (insn);
4758 /* Return true if we need secondary memory moves for insn in
4759 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4760 into the insn. */
4761 static bool
4762 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4763 rtx usage_insns ATTRIBUTE_UNUSED)
4765 #ifndef SECONDARY_MEMORY_NEEDED
4766 return false;
4767 #else
4768 rtx_insn *insn;
4769 rtx set, dest;
4770 enum reg_class cl;
4772 if (inher_cl == ALL_REGS
4773 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4774 return false;
4775 lra_assert (INSN_P (insn));
4776 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4777 return false;
4778 dest = SET_DEST (set);
4779 if (! REG_P (dest))
4780 return false;
4781 lra_assert (inher_cl != NO_REGS);
4782 cl = get_reg_class (REGNO (dest));
4783 return (cl != NO_REGS && cl != ALL_REGS
4784 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4785 #endif
4788 /* Registers involved in inheritance/split in the current EBB
4789 (inheritance/split pseudos and original registers). */
4790 static bitmap_head check_only_regs;
4792 /* Do inheritance transformations for insn INSN, which defines (if
4793 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4794 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4795 form as the "insns" field of usage_insns. Return true if we
4796 succeed in such transformation.
4798 The transformations look like:
4800 p <- ... i <- ...
4801 ... p <- i (new insn)
4802 ... =>
4803 <- ... p ... <- ... i ...
4805 ... i <- p (new insn)
4806 <- ... p ... <- ... i ...
4807 ... =>
4808 <- ... p ... <- ... i ...
4809 where p is a spilled original pseudo and i is a new inheritance pseudo.
4812 The inheritance pseudo has the smallest class of two classes CL and
4813 class of ORIGINAL REGNO. */
4814 static bool
4815 inherit_reload_reg (bool def_p, int original_regno,
4816 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4818 if (optimize_function_for_size_p (cfun))
4819 return false;
4821 enum reg_class rclass = lra_get_allocno_class (original_regno);
4822 rtx original_reg = regno_reg_rtx[original_regno];
4823 rtx new_reg, usage_insn;
4824 rtx_insn *new_insns;
4826 lra_assert (! usage_insns[original_regno].after_p);
4827 if (lra_dump_file != NULL)
4828 fprintf (lra_dump_file,
4829 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4830 if (! ira_reg_classes_intersect_p[cl][rclass])
4832 if (lra_dump_file != NULL)
4834 fprintf (lra_dump_file,
4835 " Rejecting inheritance for %d "
4836 "because of disjoint classes %s and %s\n",
4837 original_regno, reg_class_names[cl],
4838 reg_class_names[rclass]);
4839 fprintf (lra_dump_file,
4840 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4842 return false;
4844 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4845 /* We don't use a subset of two classes because it can be
4846 NO_REGS. This transformation is still profitable in most
4847 cases even if the classes are not intersected as register
4848 move is probably cheaper than a memory load. */
4849 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4851 if (lra_dump_file != NULL)
4852 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4853 reg_class_names[cl], reg_class_names[rclass]);
4855 rclass = cl;
4857 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4859 /* Reject inheritance resulting in secondary memory moves.
4860 Otherwise, there is a danger in LRA cycling. Also such
4861 transformation will be unprofitable. */
4862 if (lra_dump_file != NULL)
4864 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4865 rtx set = single_set (insn);
4867 lra_assert (set != NULL_RTX);
4869 rtx dest = SET_DEST (set);
4871 lra_assert (REG_P (dest));
4872 fprintf (lra_dump_file,
4873 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4874 "as secondary mem is needed\n",
4875 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4876 original_regno, reg_class_names[rclass]);
4877 fprintf (lra_dump_file,
4878 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4880 return false;
4882 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4883 rclass, "inheritance");
4884 start_sequence ();
4885 if (def_p)
4886 lra_emit_move (original_reg, new_reg);
4887 else
4888 lra_emit_move (new_reg, original_reg);
4889 new_insns = get_insns ();
4890 end_sequence ();
4891 if (NEXT_INSN (new_insns) != NULL_RTX)
4893 if (lra_dump_file != NULL)
4895 fprintf (lra_dump_file,
4896 " Rejecting inheritance %d->%d "
4897 "as it results in 2 or more insns:\n",
4898 original_regno, REGNO (new_reg));
4899 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4900 fprintf (lra_dump_file,
4901 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4903 return false;
4905 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
4906 lra_update_insn_regno_info (insn);
4907 if (! def_p)
4908 /* We now have a new usage insn for original regno. */
4909 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4910 if (lra_dump_file != NULL)
4911 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4912 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4913 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4914 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4915 bitmap_set_bit (&check_only_regs, original_regno);
4916 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4917 if (def_p)
4918 lra_process_new_insns (insn, NULL, new_insns,
4919 "Add original<-inheritance");
4920 else
4921 lra_process_new_insns (insn, new_insns, NULL,
4922 "Add inheritance<-original");
4923 while (next_usage_insns != NULL_RTX)
4925 if (GET_CODE (next_usage_insns) != INSN_LIST)
4927 usage_insn = next_usage_insns;
4928 lra_assert (NONDEBUG_INSN_P (usage_insn));
4929 next_usage_insns = NULL;
4931 else
4933 usage_insn = XEXP (next_usage_insns, 0);
4934 lra_assert (DEBUG_INSN_P (usage_insn));
4935 next_usage_insns = XEXP (next_usage_insns, 1);
4937 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
4938 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4939 if (lra_dump_file != NULL)
4941 fprintf (lra_dump_file,
4942 " Inheritance reuse change %d->%d (bb%d):\n",
4943 original_regno, REGNO (new_reg),
4944 BLOCK_FOR_INSN (usage_insn)->index);
4945 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
4948 if (lra_dump_file != NULL)
4949 fprintf (lra_dump_file,
4950 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4951 return true;
4954 /* Return true if we need a caller save/restore for pseudo REGNO which
4955 was assigned to a hard register. */
4956 static inline bool
4957 need_for_call_save_p (int regno)
4959 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4960 return (usage_insns[regno].calls_num < calls_num
4961 && (overlaps_hard_reg_set_p
4962 ((flag_ipa_ra &&
4963 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4964 ? lra_reg_info[regno].actual_call_used_reg_set
4965 : call_used_reg_set,
4966 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4967 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4968 PSEUDO_REGNO_MODE (regno))));
4971 /* Global registers occurring in the current EBB. */
4972 static bitmap_head ebb_global_regs;
4974 /* Return true if we need a split for hard register REGNO or pseudo
4975 REGNO which was assigned to a hard register.
4976 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4977 used for reloads since the EBB end. It is an approximation of the
4978 used hard registers in the split range. The exact value would
4979 require expensive calculations. If we were aggressive with
4980 splitting because of the approximation, the split pseudo will save
4981 the same hard register assignment and will be removed in the undo
4982 pass. We still need the approximation because too aggressive
4983 splitting would result in too inaccurate cost calculation in the
4984 assignment pass because of too many generated moves which will be
4985 probably removed in the undo pass. */
4986 static inline bool
4987 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4989 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4991 lra_assert (hard_regno >= 0);
4992 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4993 /* Don't split eliminable hard registers, otherwise we can
4994 split hard registers like hard frame pointer, which
4995 lives on BB start/end according to DF-infrastructure,
4996 when there is a pseudo assigned to the register and
4997 living in the same BB. */
4998 && (regno >= FIRST_PSEUDO_REGISTER
4999 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
5000 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
5001 /* Don't split call clobbered hard regs living through
5002 calls, otherwise we might have a check problem in the
5003 assign sub-pass as in the most cases (exception is a
5004 situation when lra_risky_transformations_p value is
5005 true) the assign pass assumes that all pseudos living
5006 through calls are assigned to call saved hard regs. */
5007 && (regno >= FIRST_PSEUDO_REGISTER
5008 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
5009 || usage_insns[regno].calls_num == calls_num)
5010 /* We need at least 2 reloads to make pseudo splitting
5011 profitable. We should provide hard regno splitting in
5012 any case to solve 1st insn scheduling problem when
5013 moving hard register definition up might result in
5014 impossibility to find hard register for reload pseudo of
5015 small register class. */
5016 && (usage_insns[regno].reloads_num
5017 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
5018 && (regno < FIRST_PSEUDO_REGISTER
5019 /* For short living pseudos, spilling + inheritance can
5020 be considered a substitution for splitting.
5021 Therefore we do not splitting for local pseudos. It
5022 decreases also aggressiveness of splitting. The
5023 minimal number of references is chosen taking into
5024 account that for 2 references splitting has no sense
5025 as we can just spill the pseudo. */
5026 || (regno >= FIRST_PSEUDO_REGISTER
5027 && lra_reg_info[regno].nrefs > 3
5028 && bitmap_bit_p (&ebb_global_regs, regno))))
5029 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
5032 /* Return class for the split pseudo created from original pseudo with
5033 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
5034 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
5035 results in no secondary memory movements. */
5036 static enum reg_class
5037 choose_split_class (enum reg_class allocno_class,
5038 int hard_regno ATTRIBUTE_UNUSED,
5039 machine_mode mode ATTRIBUTE_UNUSED)
5041 #ifndef SECONDARY_MEMORY_NEEDED
5042 return allocno_class;
5043 #else
5044 int i;
5045 enum reg_class cl, best_cl = NO_REGS;
5046 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
5047 = REGNO_REG_CLASS (hard_regno);
5049 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
5050 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
5051 return allocno_class;
5052 for (i = 0;
5053 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
5054 i++)
5055 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
5056 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
5057 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
5058 && (best_cl == NO_REGS
5059 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
5060 best_cl = cl;
5061 return best_cl;
5062 #endif
5065 /* Do split transformations for insn INSN, which defines or uses
5066 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
5067 the EBB next uses ORIGINAL_REGNO; it has the same form as the
5068 "insns" field of usage_insns.
5070 The transformations look like:
5072 p <- ... p <- ...
5073 ... s <- p (new insn -- save)
5074 ... =>
5075 ... p <- s (new insn -- restore)
5076 <- ... p ... <- ... p ...
5078 <- ... p ... <- ... p ...
5079 ... s <- p (new insn -- save)
5080 ... =>
5081 ... p <- s (new insn -- restore)
5082 <- ... p ... <- ... p ...
5084 where p is an original pseudo got a hard register or a hard
5085 register and s is a new split pseudo. The save is put before INSN
5086 if BEFORE_P is true. Return true if we succeed in such
5087 transformation. */
5088 static bool
5089 split_reg (bool before_p, int original_regno, rtx_insn *insn,
5090 rtx next_usage_insns)
5092 enum reg_class rclass;
5093 rtx original_reg;
5094 int hard_regno, nregs;
5095 rtx new_reg, usage_insn;
5096 rtx_insn *restore, *save;
5097 bool after_p;
5098 bool call_save_p;
5099 machine_mode mode;
5101 if (original_regno < FIRST_PSEUDO_REGISTER)
5103 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
5104 hard_regno = original_regno;
5105 call_save_p = false;
5106 nregs = 1;
5107 mode = lra_reg_info[hard_regno].biggest_mode;
5108 machine_mode reg_rtx_mode = GET_MODE (regno_reg_rtx[hard_regno]);
5109 /* A reg can have a biggest_mode of VOIDmode if it was only ever seen
5110 as part of a multi-word register. In that case, or if the biggest
5111 mode was larger than a register, just use the reg_rtx. Otherwise,
5112 limit the size to that of the biggest access in the function. */
5113 if (mode == VOIDmode
5114 || GET_MODE_SIZE (mode) > GET_MODE_SIZE (reg_rtx_mode))
5116 original_reg = regno_reg_rtx[hard_regno];
5117 mode = reg_rtx_mode;
5119 else
5120 original_reg = gen_rtx_REG (mode, hard_regno);
5122 else
5124 mode = PSEUDO_REGNO_MODE (original_regno);
5125 hard_regno = reg_renumber[original_regno];
5126 nregs = hard_regno_nregs[hard_regno][mode];
5127 rclass = lra_get_allocno_class (original_regno);
5128 original_reg = regno_reg_rtx[original_regno];
5129 call_save_p = need_for_call_save_p (original_regno);
5131 lra_assert (hard_regno >= 0);
5132 if (lra_dump_file != NULL)
5133 fprintf (lra_dump_file,
5134 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
5136 if (call_save_p)
5138 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
5139 hard_regno_nregs[hard_regno][mode],
5140 mode);
5141 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
5143 else
5145 rclass = choose_split_class (rclass, hard_regno, mode);
5146 if (rclass == NO_REGS)
5148 if (lra_dump_file != NULL)
5150 fprintf (lra_dump_file,
5151 " Rejecting split of %d(%s): "
5152 "no good reg class for %d(%s)\n",
5153 original_regno,
5154 reg_class_names[lra_get_allocno_class (original_regno)],
5155 hard_regno,
5156 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
5157 fprintf
5158 (lra_dump_file,
5159 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5161 return false;
5163 new_reg = lra_create_new_reg (mode, original_reg, rclass, "split");
5164 reg_renumber[REGNO (new_reg)] = hard_regno;
5166 save = emit_spill_move (true, new_reg, original_reg);
5167 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
5169 if (lra_dump_file != NULL)
5171 fprintf
5172 (lra_dump_file,
5173 " Rejecting split %d->%d resulting in > 2 save insns:\n",
5174 original_regno, REGNO (new_reg));
5175 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
5176 fprintf (lra_dump_file,
5177 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5179 return false;
5181 restore = emit_spill_move (false, new_reg, original_reg);
5182 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
5184 if (lra_dump_file != NULL)
5186 fprintf (lra_dump_file,
5187 " Rejecting split %d->%d "
5188 "resulting in > 2 restore insns:\n",
5189 original_regno, REGNO (new_reg));
5190 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
5191 fprintf (lra_dump_file,
5192 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5194 return false;
5196 after_p = usage_insns[original_regno].after_p;
5197 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
5198 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5199 bitmap_set_bit (&check_only_regs, original_regno);
5200 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
5201 for (;;)
5203 if (GET_CODE (next_usage_insns) != INSN_LIST)
5205 usage_insn = next_usage_insns;
5206 break;
5208 usage_insn = XEXP (next_usage_insns, 0);
5209 lra_assert (DEBUG_INSN_P (usage_insn));
5210 next_usage_insns = XEXP (next_usage_insns, 1);
5211 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5212 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5213 if (lra_dump_file != NULL)
5215 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5216 original_regno, REGNO (new_reg));
5217 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5220 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5221 lra_assert (usage_insn != insn || (after_p && before_p));
5222 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5223 after_p ? NULL : restore,
5224 after_p ? restore : NULL,
5225 call_save_p
5226 ? "Add reg<-save" : "Add reg<-split");
5227 lra_process_new_insns (insn, before_p ? save : NULL,
5228 before_p ? NULL : save,
5229 call_save_p
5230 ? "Add save<-reg" : "Add split<-reg");
5231 if (nregs > 1)
5232 /* If we are trying to split multi-register. We should check
5233 conflicts on the next assignment sub-pass. IRA can allocate on
5234 sub-register levels, LRA do this on pseudos level right now and
5235 this discrepancy may create allocation conflicts after
5236 splitting. */
5237 lra_risky_transformations_p = true;
5238 if (lra_dump_file != NULL)
5239 fprintf (lra_dump_file,
5240 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5241 return true;
5244 /* Recognize that we need a split transformation for insn INSN, which
5245 defines or uses REGNO in its insn biggest MODE (we use it only if
5246 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5247 hard registers which might be used for reloads since the EBB end.
5248 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5249 uid before starting INSN processing. Return true if we succeed in
5250 such transformation. */
5251 static bool
5252 split_if_necessary (int regno, machine_mode mode,
5253 HARD_REG_SET potential_reload_hard_regs,
5254 bool before_p, rtx_insn *insn, int max_uid)
5256 bool res = false;
5257 int i, nregs = 1;
5258 rtx next_usage_insns;
5260 if (regno < FIRST_PSEUDO_REGISTER)
5261 nregs = hard_regno_nregs[regno][mode];
5262 for (i = 0; i < nregs; i++)
5263 if (usage_insns[regno + i].check == curr_usage_insns_check
5264 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5265 /* To avoid processing the register twice or more. */
5266 && ((GET_CODE (next_usage_insns) != INSN_LIST
5267 && INSN_UID (next_usage_insns) < max_uid)
5268 || (GET_CODE (next_usage_insns) == INSN_LIST
5269 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5270 && need_for_split_p (potential_reload_hard_regs, regno + i)
5271 && split_reg (before_p, regno + i, insn, next_usage_insns))
5272 res = true;
5273 return res;
5276 /* Check only registers living at the current program point in the
5277 current EBB. */
5278 static bitmap_head live_regs;
5280 /* Update live info in EBB given by its HEAD and TAIL insns after
5281 inheritance/split transformation. The function removes dead moves
5282 too. */
5283 static void
5284 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5286 unsigned int j;
5287 int i, regno;
5288 bool live_p;
5289 rtx_insn *prev_insn;
5290 rtx set;
5291 bool remove_p;
5292 basic_block last_bb, prev_bb, curr_bb;
5293 bitmap_iterator bi;
5294 struct lra_insn_reg *reg;
5295 edge e;
5296 edge_iterator ei;
5298 last_bb = BLOCK_FOR_INSN (tail);
5299 prev_bb = NULL;
5300 for (curr_insn = tail;
5301 curr_insn != PREV_INSN (head);
5302 curr_insn = prev_insn)
5304 prev_insn = PREV_INSN (curr_insn);
5305 /* We need to process empty blocks too. They contain
5306 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5307 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5308 continue;
5309 curr_bb = BLOCK_FOR_INSN (curr_insn);
5310 if (curr_bb != prev_bb)
5312 if (prev_bb != NULL)
5314 /* Update df_get_live_in (prev_bb): */
5315 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5316 if (bitmap_bit_p (&live_regs, j))
5317 bitmap_set_bit (df_get_live_in (prev_bb), j);
5318 else
5319 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5321 if (curr_bb != last_bb)
5323 /* Update df_get_live_out (curr_bb): */
5324 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5326 live_p = bitmap_bit_p (&live_regs, j);
5327 if (! live_p)
5328 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5329 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5331 live_p = true;
5332 break;
5334 if (live_p)
5335 bitmap_set_bit (df_get_live_out (curr_bb), j);
5336 else
5337 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5340 prev_bb = curr_bb;
5341 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5343 if (! NONDEBUG_INSN_P (curr_insn))
5344 continue;
5345 curr_id = lra_get_insn_recog_data (curr_insn);
5346 curr_static_id = curr_id->insn_static_data;
5347 remove_p = false;
5348 if ((set = single_set (curr_insn)) != NULL_RTX
5349 && REG_P (SET_DEST (set))
5350 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5351 && SET_DEST (set) != pic_offset_table_rtx
5352 && bitmap_bit_p (&check_only_regs, regno)
5353 && ! bitmap_bit_p (&live_regs, regno))
5354 remove_p = true;
5355 /* See which defined values die here. */
5356 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5357 if (reg->type == OP_OUT && ! reg->subreg_p)
5358 bitmap_clear_bit (&live_regs, reg->regno);
5359 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5360 if (reg->type == OP_OUT && ! reg->subreg_p)
5361 bitmap_clear_bit (&live_regs, reg->regno);
5362 if (curr_id->arg_hard_regs != NULL)
5363 /* Make clobbered argument hard registers die. */
5364 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5365 if (regno >= FIRST_PSEUDO_REGISTER)
5366 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
5367 /* Mark each used value as live. */
5368 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5369 if (reg->type != OP_OUT
5370 && bitmap_bit_p (&check_only_regs, reg->regno))
5371 bitmap_set_bit (&live_regs, reg->regno);
5372 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5373 if (reg->type != OP_OUT
5374 && bitmap_bit_p (&check_only_regs, reg->regno))
5375 bitmap_set_bit (&live_regs, reg->regno);
5376 if (curr_id->arg_hard_regs != NULL)
5377 /* Make used argument hard registers live. */
5378 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5379 if (regno < FIRST_PSEUDO_REGISTER
5380 && bitmap_bit_p (&check_only_regs, regno))
5381 bitmap_set_bit (&live_regs, regno);
5382 /* It is quite important to remove dead move insns because it
5383 means removing dead store. We don't need to process them for
5384 constraints. */
5385 if (remove_p)
5387 if (lra_dump_file != NULL)
5389 fprintf (lra_dump_file, " Removing dead insn:\n ");
5390 dump_insn_slim (lra_dump_file, curr_insn);
5392 lra_set_insn_deleted (curr_insn);
5397 /* The structure describes info to do an inheritance for the current
5398 insn. We need to collect such info first before doing the
5399 transformations because the transformations change the insn
5400 internal representation. */
5401 struct to_inherit
5403 /* Original regno. */
5404 int regno;
5405 /* Subsequent insns which can inherit original reg value. */
5406 rtx insns;
5409 /* Array containing all info for doing inheritance from the current
5410 insn. */
5411 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5413 /* Number elements in the previous array. */
5414 static int to_inherit_num;
5416 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5417 structure to_inherit. */
5418 static void
5419 add_to_inherit (int regno, rtx insns)
5421 int i;
5423 for (i = 0; i < to_inherit_num; i++)
5424 if (to_inherit[i].regno == regno)
5425 return;
5426 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5427 to_inherit[to_inherit_num].regno = regno;
5428 to_inherit[to_inherit_num++].insns = insns;
5431 /* Return the last non-debug insn in basic block BB, or the block begin
5432 note if none. */
5433 static rtx_insn *
5434 get_last_insertion_point (basic_block bb)
5436 rtx_insn *insn;
5438 FOR_BB_INSNS_REVERSE (bb, insn)
5439 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5440 return insn;
5441 gcc_unreachable ();
5444 /* Set up RES by registers living on edges FROM except the edge (FROM,
5445 TO) or by registers set up in a jump insn in BB FROM. */
5446 static void
5447 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5449 rtx_insn *last;
5450 struct lra_insn_reg *reg;
5451 edge e;
5452 edge_iterator ei;
5454 lra_assert (to != NULL);
5455 bitmap_clear (res);
5456 FOR_EACH_EDGE (e, ei, from->succs)
5457 if (e->dest != to)
5458 bitmap_ior_into (res, df_get_live_in (e->dest));
5459 last = get_last_insertion_point (from);
5460 if (! JUMP_P (last))
5461 return;
5462 curr_id = lra_get_insn_recog_data (last);
5463 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5464 if (reg->type != OP_IN)
5465 bitmap_set_bit (res, reg->regno);
5468 /* Used as a temporary results of some bitmap calculations. */
5469 static bitmap_head temp_bitmap;
5471 /* We split for reloads of small class of hard regs. The following
5472 defines how many hard regs the class should have to be qualified as
5473 small. The code is mostly oriented to x86/x86-64 architecture
5474 where some insns need to use only specific register or pair of
5475 registers and these register can live in RTL explicitly, e.g. for
5476 parameter passing. */
5477 static const int max_small_class_regs_num = 2;
5479 /* Do inheritance/split transformations in EBB starting with HEAD and
5480 finishing on TAIL. We process EBB insns in the reverse order.
5481 Return true if we did any inheritance/split transformation in the
5482 EBB.
5484 We should avoid excessive splitting which results in worse code
5485 because of inaccurate cost calculations for spilling new split
5486 pseudos in such case. To achieve this we do splitting only if
5487 register pressure is high in given basic block and there are reload
5488 pseudos requiring hard registers. We could do more register
5489 pressure calculations at any given program point to avoid necessary
5490 splitting even more but it is to expensive and the current approach
5491 works well enough. */
5492 static bool
5493 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5495 int i, src_regno, dst_regno, nregs;
5496 bool change_p, succ_p, update_reloads_num_p;
5497 rtx_insn *prev_insn, *last_insn;
5498 rtx next_usage_insns, set;
5499 enum reg_class cl;
5500 struct lra_insn_reg *reg;
5501 basic_block last_processed_bb, curr_bb = NULL;
5502 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5503 bitmap to_process;
5504 unsigned int j;
5505 bitmap_iterator bi;
5506 bool head_p, after_p;
5508 change_p = false;
5509 curr_usage_insns_check++;
5510 reloads_num = calls_num = 0;
5511 bitmap_clear (&check_only_regs);
5512 last_processed_bb = NULL;
5513 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5514 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5515 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5516 /* We don't process new insns generated in the loop. */
5517 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5519 prev_insn = PREV_INSN (curr_insn);
5520 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5521 curr_bb = BLOCK_FOR_INSN (curr_insn);
5522 if (last_processed_bb != curr_bb)
5524 /* We are at the end of BB. Add qualified living
5525 pseudos for potential splitting. */
5526 to_process = df_get_live_out (curr_bb);
5527 if (last_processed_bb != NULL)
5529 /* We are somewhere in the middle of EBB. */
5530 get_live_on_other_edges (curr_bb, last_processed_bb,
5531 &temp_bitmap);
5532 to_process = &temp_bitmap;
5534 last_processed_bb = curr_bb;
5535 last_insn = get_last_insertion_point (curr_bb);
5536 after_p = (! JUMP_P (last_insn)
5537 && (! CALL_P (last_insn)
5538 || (find_reg_note (last_insn,
5539 REG_NORETURN, NULL_RTX) == NULL_RTX
5540 && ! SIBLING_CALL_P (last_insn))));
5541 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5542 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5544 if ((int) j >= lra_constraint_new_regno_start)
5545 break;
5546 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5548 if (j < FIRST_PSEUDO_REGISTER)
5549 SET_HARD_REG_BIT (live_hard_regs, j);
5550 else
5551 add_to_hard_reg_set (&live_hard_regs,
5552 PSEUDO_REGNO_MODE (j),
5553 reg_renumber[j]);
5554 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5558 src_regno = dst_regno = -1;
5559 if (NONDEBUG_INSN_P (curr_insn)
5560 && (set = single_set (curr_insn)) != NULL_RTX
5561 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5563 src_regno = REGNO (SET_SRC (set));
5564 dst_regno = REGNO (SET_DEST (set));
5566 update_reloads_num_p = true;
5567 if (src_regno < lra_constraint_new_regno_start
5568 && src_regno >= FIRST_PSEUDO_REGISTER
5569 && reg_renumber[src_regno] < 0
5570 && dst_regno >= lra_constraint_new_regno_start
5571 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5573 /* 'reload_pseudo <- original_pseudo'. */
5574 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5575 reloads_num++;
5576 update_reloads_num_p = false;
5577 succ_p = false;
5578 if (usage_insns[src_regno].check == curr_usage_insns_check
5579 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5580 succ_p = inherit_reload_reg (false, src_regno, cl,
5581 curr_insn, next_usage_insns);
5582 if (succ_p)
5583 change_p = true;
5584 else
5585 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5586 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5587 IOR_HARD_REG_SET (potential_reload_hard_regs,
5588 reg_class_contents[cl]);
5590 else if (src_regno >= lra_constraint_new_regno_start
5591 && dst_regno < lra_constraint_new_regno_start
5592 && dst_regno >= FIRST_PSEUDO_REGISTER
5593 && reg_renumber[dst_regno] < 0
5594 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5595 && usage_insns[dst_regno].check == curr_usage_insns_check
5596 && (next_usage_insns
5597 = usage_insns[dst_regno].insns) != NULL_RTX)
5599 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5600 reloads_num++;
5601 update_reloads_num_p = false;
5602 /* 'original_pseudo <- reload_pseudo'. */
5603 if (! JUMP_P (curr_insn)
5604 && inherit_reload_reg (true, dst_regno, cl,
5605 curr_insn, next_usage_insns))
5606 change_p = true;
5607 /* Invalidate. */
5608 usage_insns[dst_regno].check = 0;
5609 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5610 IOR_HARD_REG_SET (potential_reload_hard_regs,
5611 reg_class_contents[cl]);
5613 else if (INSN_P (curr_insn))
5615 int iter;
5616 int max_uid = get_max_uid ();
5618 curr_id = lra_get_insn_recog_data (curr_insn);
5619 curr_static_id = curr_id->insn_static_data;
5620 to_inherit_num = 0;
5621 /* Process insn definitions. */
5622 for (iter = 0; iter < 2; iter++)
5623 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5624 reg != NULL;
5625 reg = reg->next)
5626 if (reg->type != OP_IN
5627 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5629 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5630 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5631 && usage_insns[dst_regno].check == curr_usage_insns_check
5632 && (next_usage_insns
5633 = usage_insns[dst_regno].insns) != NULL_RTX)
5635 struct lra_insn_reg *r;
5637 for (r = curr_id->regs; r != NULL; r = r->next)
5638 if (r->type != OP_OUT && r->regno == dst_regno)
5639 break;
5640 /* Don't do inheritance if the pseudo is also
5641 used in the insn. */
5642 if (r == NULL)
5643 /* We can not do inheritance right now
5644 because the current insn reg info (chain
5645 regs) can change after that. */
5646 add_to_inherit (dst_regno, next_usage_insns);
5648 /* We can not process one reg twice here because of
5649 usage_insns invalidation. */
5650 if ((dst_regno < FIRST_PSEUDO_REGISTER
5651 || reg_renumber[dst_regno] >= 0)
5652 && ! reg->subreg_p && reg->type != OP_IN)
5654 HARD_REG_SET s;
5656 if (split_if_necessary (dst_regno, reg->biggest_mode,
5657 potential_reload_hard_regs,
5658 false, curr_insn, max_uid))
5659 change_p = true;
5660 CLEAR_HARD_REG_SET (s);
5661 if (dst_regno < FIRST_PSEUDO_REGISTER)
5662 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5663 else
5664 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5665 reg_renumber[dst_regno]);
5666 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5668 /* We should invalidate potential inheritance or
5669 splitting for the current insn usages to the next
5670 usage insns (see code below) as the output pseudo
5671 prevents this. */
5672 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5673 && reg_renumber[dst_regno] < 0)
5674 || (reg->type == OP_OUT && ! reg->subreg_p
5675 && (dst_regno < FIRST_PSEUDO_REGISTER
5676 || reg_renumber[dst_regno] >= 0)))
5678 /* Invalidate and mark definitions. */
5679 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5680 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5681 else
5683 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5684 for (i = 0; i < nregs; i++)
5685 usage_insns[dst_regno + i].check
5686 = -(int) INSN_UID (curr_insn);
5690 /* Process clobbered call regs. */
5691 if (curr_id->arg_hard_regs != NULL)
5692 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5693 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5694 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
5695 = -(int) INSN_UID (curr_insn);
5696 if (! JUMP_P (curr_insn))
5697 for (i = 0; i < to_inherit_num; i++)
5698 if (inherit_reload_reg (true, to_inherit[i].regno,
5699 ALL_REGS, curr_insn,
5700 to_inherit[i].insns))
5701 change_p = true;
5702 if (CALL_P (curr_insn))
5704 rtx cheap, pat, dest;
5705 rtx_insn *restore;
5706 int regno, hard_regno;
5708 calls_num++;
5709 if ((cheap = find_reg_note (curr_insn,
5710 REG_RETURNED, NULL_RTX)) != NULL_RTX
5711 && ((cheap = XEXP (cheap, 0)), true)
5712 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5713 && (hard_regno = reg_renumber[regno]) >= 0
5714 /* If there are pending saves/restores, the
5715 optimization is not worth. */
5716 && usage_insns[regno].calls_num == calls_num - 1
5717 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5719 /* Restore the pseudo from the call result as
5720 REG_RETURNED note says that the pseudo value is
5721 in the call result and the pseudo is an argument
5722 of the call. */
5723 pat = PATTERN (curr_insn);
5724 if (GET_CODE (pat) == PARALLEL)
5725 pat = XVECEXP (pat, 0, 0);
5726 dest = SET_DEST (pat);
5727 /* For multiple return values dest is PARALLEL.
5728 Currently we handle only single return value case. */
5729 if (REG_P (dest))
5731 start_sequence ();
5732 emit_move_insn (cheap, copy_rtx (dest));
5733 restore = get_insns ();
5734 end_sequence ();
5735 lra_process_new_insns (curr_insn, NULL, restore,
5736 "Inserting call parameter restore");
5737 /* We don't need to save/restore of the pseudo from
5738 this call. */
5739 usage_insns[regno].calls_num = calls_num;
5740 bitmap_set_bit (&check_only_regs, regno);
5744 to_inherit_num = 0;
5745 /* Process insn usages. */
5746 for (iter = 0; iter < 2; iter++)
5747 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5748 reg != NULL;
5749 reg = reg->next)
5750 if ((reg->type != OP_OUT
5751 || (reg->type == OP_OUT && reg->subreg_p))
5752 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5754 if (src_regno >= FIRST_PSEUDO_REGISTER
5755 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5757 if (usage_insns[src_regno].check == curr_usage_insns_check
5758 && (next_usage_insns
5759 = usage_insns[src_regno].insns) != NULL_RTX
5760 && NONDEBUG_INSN_P (curr_insn))
5761 add_to_inherit (src_regno, next_usage_insns);
5762 else if (usage_insns[src_regno].check
5763 != -(int) INSN_UID (curr_insn))
5764 /* Add usages but only if the reg is not set up
5765 in the same insn. */
5766 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5768 else if (src_regno < FIRST_PSEUDO_REGISTER
5769 || reg_renumber[src_regno] >= 0)
5771 bool before_p;
5772 rtx_insn *use_insn = curr_insn;
5774 before_p = (JUMP_P (curr_insn)
5775 || (CALL_P (curr_insn) && reg->type == OP_IN));
5776 if (NONDEBUG_INSN_P (curr_insn)
5777 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5778 && split_if_necessary (src_regno, reg->biggest_mode,
5779 potential_reload_hard_regs,
5780 before_p, curr_insn, max_uid))
5782 if (reg->subreg_p)
5783 lra_risky_transformations_p = true;
5784 change_p = true;
5785 /* Invalidate. */
5786 usage_insns[src_regno].check = 0;
5787 if (before_p)
5788 use_insn = PREV_INSN (curr_insn);
5790 if (NONDEBUG_INSN_P (curr_insn))
5792 if (src_regno < FIRST_PSEUDO_REGISTER)
5793 add_to_hard_reg_set (&live_hard_regs,
5794 reg->biggest_mode, src_regno);
5795 else
5796 add_to_hard_reg_set (&live_hard_regs,
5797 PSEUDO_REGNO_MODE (src_regno),
5798 reg_renumber[src_regno]);
5800 add_next_usage_insn (src_regno, use_insn, reloads_num);
5803 /* Process used call regs. */
5804 if (curr_id->arg_hard_regs != NULL)
5805 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5806 if (src_regno < FIRST_PSEUDO_REGISTER)
5808 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5809 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5811 for (i = 0; i < to_inherit_num; i++)
5813 src_regno = to_inherit[i].regno;
5814 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5815 curr_insn, to_inherit[i].insns))
5816 change_p = true;
5817 else
5818 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5821 if (update_reloads_num_p
5822 && NONDEBUG_INSN_P (curr_insn)
5823 && (set = single_set (curr_insn)) != NULL_RTX)
5825 int regno = -1;
5826 if ((REG_P (SET_DEST (set))
5827 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5828 && reg_renumber[regno] < 0
5829 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5830 || (REG_P (SET_SRC (set))
5831 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5832 && reg_renumber[regno] < 0
5833 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5835 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5836 reloads_num++;
5837 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5838 IOR_HARD_REG_SET (potential_reload_hard_regs,
5839 reg_class_contents[cl]);
5842 /* We reached the start of the current basic block. */
5843 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5844 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5846 /* We reached the beginning of the current block -- do
5847 rest of spliting in the current BB. */
5848 to_process = df_get_live_in (curr_bb);
5849 if (BLOCK_FOR_INSN (head) != curr_bb)
5851 /* We are somewhere in the middle of EBB. */
5852 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5853 curr_bb, &temp_bitmap);
5854 to_process = &temp_bitmap;
5856 head_p = true;
5857 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5859 if ((int) j >= lra_constraint_new_regno_start)
5860 break;
5861 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5862 && usage_insns[j].check == curr_usage_insns_check
5863 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5865 if (need_for_split_p (potential_reload_hard_regs, j))
5867 if (lra_dump_file != NULL && head_p)
5869 fprintf (lra_dump_file,
5870 " ----------------------------------\n");
5871 head_p = false;
5873 if (split_reg (false, j, bb_note (curr_bb),
5874 next_usage_insns))
5875 change_p = true;
5877 usage_insns[j].check = 0;
5882 return change_p;
5885 /* This value affects EBB forming. If probability of edge from EBB to
5886 a BB is not greater than the following value, we don't add the BB
5887 to EBB. */
5888 #define EBB_PROBABILITY_CUTOFF \
5889 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
5891 /* Current number of inheritance/split iteration. */
5892 int lra_inheritance_iter;
5894 /* Entry function for inheritance/split pass. */
5895 void
5896 lra_inheritance (void)
5898 int i;
5899 basic_block bb, start_bb;
5900 edge e;
5902 lra_inheritance_iter++;
5903 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5904 return;
5905 timevar_push (TV_LRA_INHERITANCE);
5906 if (lra_dump_file != NULL)
5907 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5908 lra_inheritance_iter);
5909 curr_usage_insns_check = 0;
5910 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5911 for (i = 0; i < lra_constraint_new_regno_start; i++)
5912 usage_insns[i].check = 0;
5913 bitmap_initialize (&check_only_regs, &reg_obstack);
5914 bitmap_initialize (&live_regs, &reg_obstack);
5915 bitmap_initialize (&temp_bitmap, &reg_obstack);
5916 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5917 FOR_EACH_BB_FN (bb, cfun)
5919 start_bb = bb;
5920 if (lra_dump_file != NULL)
5921 fprintf (lra_dump_file, "EBB");
5922 /* Form a EBB starting with BB. */
5923 bitmap_clear (&ebb_global_regs);
5924 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5925 for (;;)
5927 if (lra_dump_file != NULL)
5928 fprintf (lra_dump_file, " %d", bb->index);
5929 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5930 || LABEL_P (BB_HEAD (bb->next_bb)))
5931 break;
5932 e = find_fallthru_edge (bb->succs);
5933 if (! e)
5934 break;
5935 if (e->probability < EBB_PROBABILITY_CUTOFF)
5936 break;
5937 bb = bb->next_bb;
5939 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5940 if (lra_dump_file != NULL)
5941 fprintf (lra_dump_file, "\n");
5942 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5943 /* Remember that the EBB head and tail can change in
5944 inherit_in_ebb. */
5945 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5947 bitmap_clear (&ebb_global_regs);
5948 bitmap_clear (&temp_bitmap);
5949 bitmap_clear (&live_regs);
5950 bitmap_clear (&check_only_regs);
5951 free (usage_insns);
5953 timevar_pop (TV_LRA_INHERITANCE);
5958 /* This page contains code to undo failed inheritance/split
5959 transformations. */
5961 /* Current number of iteration undoing inheritance/split. */
5962 int lra_undo_inheritance_iter;
5964 /* Fix BB live info LIVE after removing pseudos created on pass doing
5965 inheritance/split which are REMOVED_PSEUDOS. */
5966 static void
5967 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5969 unsigned int regno;
5970 bitmap_iterator bi;
5972 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5973 if (bitmap_clear_bit (live, regno))
5974 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5977 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5978 number. */
5979 static int
5980 get_regno (rtx reg)
5982 if (GET_CODE (reg) == SUBREG)
5983 reg = SUBREG_REG (reg);
5984 if (REG_P (reg))
5985 return REGNO (reg);
5986 return -1;
5989 /* Delete a move INSN with destination reg DREGNO and a previous
5990 clobber insn with the same regno. The inheritance/split code can
5991 generate moves with preceding clobber and when we delete such moves
5992 we should delete the clobber insn too to keep the correct life
5993 info. */
5994 static void
5995 delete_move_and_clobber (rtx_insn *insn, int dregno)
5997 rtx_insn *prev_insn = PREV_INSN (insn);
5999 lra_set_insn_deleted (insn);
6000 lra_assert (dregno >= 0);
6001 if (prev_insn != NULL && NONDEBUG_INSN_P (prev_insn)
6002 && GET_CODE (PATTERN (prev_insn)) == CLOBBER
6003 && dregno == get_regno (XEXP (PATTERN (prev_insn), 0)))
6004 lra_set_insn_deleted (prev_insn);
6007 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
6008 return true if we did any change. The undo transformations for
6009 inheritance looks like
6010 i <- i2
6011 p <- i => p <- i2
6012 or removing
6013 p <- i, i <- p, and i <- i3
6014 where p is original pseudo from which inheritance pseudo i was
6015 created, i and i3 are removed inheritance pseudos, i2 is another
6016 not removed inheritance pseudo. All split pseudos or other
6017 occurrences of removed inheritance pseudos are changed on the
6018 corresponding original pseudos.
6020 The function also schedules insns changed and created during
6021 inheritance/split pass for processing by the subsequent constraint
6022 pass. */
6023 static bool
6024 remove_inheritance_pseudos (bitmap remove_pseudos)
6026 basic_block bb;
6027 int regno, sregno, prev_sregno, dregno, restore_regno;
6028 rtx set, prev_set;
6029 rtx_insn *prev_insn;
6030 bool change_p, done_p;
6032 change_p = ! bitmap_empty_p (remove_pseudos);
6033 /* We can not finish the function right away if CHANGE_P is true
6034 because we need to marks insns affected by previous
6035 inheritance/split pass for processing by the subsequent
6036 constraint pass. */
6037 FOR_EACH_BB_FN (bb, cfun)
6039 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
6040 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
6041 FOR_BB_INSNS_REVERSE (bb, curr_insn)
6043 if (! INSN_P (curr_insn))
6044 continue;
6045 done_p = false;
6046 sregno = dregno = -1;
6047 if (change_p && NONDEBUG_INSN_P (curr_insn)
6048 && (set = single_set (curr_insn)) != NULL_RTX)
6050 dregno = get_regno (SET_DEST (set));
6051 sregno = get_regno (SET_SRC (set));
6054 if (sregno >= 0 && dregno >= 0)
6056 if ((bitmap_bit_p (remove_pseudos, sregno)
6057 && (lra_reg_info[sregno].restore_regno == dregno
6058 || (bitmap_bit_p (remove_pseudos, dregno)
6059 && (lra_reg_info[sregno].restore_regno
6060 == lra_reg_info[dregno].restore_regno))))
6061 || (bitmap_bit_p (remove_pseudos, dregno)
6062 && lra_reg_info[dregno].restore_regno == sregno))
6063 /* One of the following cases:
6064 original <- removed inheritance pseudo
6065 removed inherit pseudo <- another removed inherit pseudo
6066 removed inherit pseudo <- original pseudo
6068 removed_split_pseudo <- original_reg
6069 original_reg <- removed_split_pseudo */
6071 if (lra_dump_file != NULL)
6073 fprintf (lra_dump_file, " Removing %s:\n",
6074 bitmap_bit_p (&lra_split_regs, sregno)
6075 || bitmap_bit_p (&lra_split_regs, dregno)
6076 ? "split" : "inheritance");
6077 dump_insn_slim (lra_dump_file, curr_insn);
6079 delete_move_and_clobber (curr_insn, dregno);
6080 done_p = true;
6082 else if (bitmap_bit_p (remove_pseudos, sregno)
6083 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
6085 /* Search the following pattern:
6086 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
6087 original_pseudo <- inherit_or_split_pseudo1
6088 where the 2nd insn is the current insn and
6089 inherit_or_split_pseudo2 is not removed. If it is found,
6090 change the current insn onto:
6091 original_pseudo <- inherit_or_split_pseudo2. */
6092 for (prev_insn = PREV_INSN (curr_insn);
6093 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
6094 prev_insn = PREV_INSN (prev_insn))
6096 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
6097 && (prev_set = single_set (prev_insn)) != NULL_RTX
6098 /* There should be no subregs in insn we are
6099 searching because only the original reg might
6100 be in subreg when we changed the mode of
6101 load/store for splitting. */
6102 && REG_P (SET_DEST (prev_set))
6103 && REG_P (SET_SRC (prev_set))
6104 && (int) REGNO (SET_DEST (prev_set)) == sregno
6105 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
6106 >= FIRST_PSEUDO_REGISTER)
6107 /* As we consider chain of inheritance or
6108 splitting described in above comment we should
6109 check that sregno and prev_sregno were
6110 inheritance/split pseudos created from the
6111 same original regno. */
6112 && (lra_reg_info[sregno].restore_regno
6113 == lra_reg_info[prev_sregno].restore_regno)
6114 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
6116 lra_assert (GET_MODE (SET_SRC (prev_set))
6117 == GET_MODE (regno_reg_rtx[sregno]));
6118 if (GET_CODE (SET_SRC (set)) == SUBREG)
6119 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
6120 else
6121 SET_SRC (set) = SET_SRC (prev_set);
6122 /* As we are finishing with processing the insn
6123 here, check the destination too as it might
6124 inheritance pseudo for another pseudo. */
6125 if (bitmap_bit_p (remove_pseudos, dregno)
6126 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
6127 && (restore_regno
6128 = lra_reg_info[dregno].restore_regno) >= 0)
6130 if (GET_CODE (SET_DEST (set)) == SUBREG)
6131 SUBREG_REG (SET_DEST (set))
6132 = regno_reg_rtx[restore_regno];
6133 else
6134 SET_DEST (set) = regno_reg_rtx[restore_regno];
6136 lra_push_insn_and_update_insn_regno_info (curr_insn);
6137 lra_set_used_insn_alternative_by_uid
6138 (INSN_UID (curr_insn), -1);
6139 done_p = true;
6140 if (lra_dump_file != NULL)
6142 fprintf (lra_dump_file, " Change reload insn:\n");
6143 dump_insn_slim (lra_dump_file, curr_insn);
6148 if (! done_p)
6150 struct lra_insn_reg *reg;
6151 bool restored_regs_p = false;
6152 bool kept_regs_p = false;
6154 curr_id = lra_get_insn_recog_data (curr_insn);
6155 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6157 regno = reg->regno;
6158 restore_regno = lra_reg_info[regno].restore_regno;
6159 if (restore_regno >= 0)
6161 if (change_p && bitmap_bit_p (remove_pseudos, regno))
6163 lra_substitute_pseudo_within_insn
6164 (curr_insn, regno, regno_reg_rtx[restore_regno],
6165 false);
6166 restored_regs_p = true;
6168 else
6169 kept_regs_p = true;
6172 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
6174 /* The instruction has changed since the previous
6175 constraints pass. */
6176 lra_push_insn_and_update_insn_regno_info (curr_insn);
6177 lra_set_used_insn_alternative_by_uid
6178 (INSN_UID (curr_insn), -1);
6180 else if (restored_regs_p)
6181 /* The instruction has been restored to the form that
6182 it had during the previous constraints pass. */
6183 lra_update_insn_regno_info (curr_insn);
6184 if (restored_regs_p && lra_dump_file != NULL)
6186 fprintf (lra_dump_file, " Insn after restoring regs:\n");
6187 dump_insn_slim (lra_dump_file, curr_insn);
6192 return change_p;
6195 /* If optional reload pseudos failed to get a hard register or was not
6196 inherited, it is better to remove optional reloads. We do this
6197 transformation after undoing inheritance to figure out necessity to
6198 remove optional reloads easier. Return true if we do any
6199 change. */
6200 static bool
6201 undo_optional_reloads (void)
6203 bool change_p, keep_p;
6204 unsigned int regno, uid;
6205 bitmap_iterator bi, bi2;
6206 rtx_insn *insn;
6207 rtx set, src, dest;
6208 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
6210 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
6211 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
6212 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6214 keep_p = false;
6215 /* Keep optional reloads from previous subpasses. */
6216 if (lra_reg_info[regno].restore_regno < 0
6217 /* If the original pseudo changed its allocation, just
6218 removing the optional pseudo is dangerous as the original
6219 pseudo will have longer live range. */
6220 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
6221 keep_p = true;
6222 else if (reg_renumber[regno] >= 0)
6223 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
6225 insn = lra_insn_recog_data[uid]->insn;
6226 if ((set = single_set (insn)) == NULL_RTX)
6227 continue;
6228 src = SET_SRC (set);
6229 dest = SET_DEST (set);
6230 if (! REG_P (src) || ! REG_P (dest))
6231 continue;
6232 if (REGNO (dest) == regno
6233 /* Ignore insn for optional reloads itself. */
6234 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
6235 /* Check only inheritance on last inheritance pass. */
6236 && (int) REGNO (src) >= new_regno_start
6237 /* Check that the optional reload was inherited. */
6238 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6240 keep_p = true;
6241 break;
6244 if (keep_p)
6246 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6247 if (lra_dump_file != NULL)
6248 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6251 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6252 bitmap_initialize (&insn_bitmap, &reg_obstack);
6253 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6255 if (lra_dump_file != NULL)
6256 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6257 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6258 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6260 insn = lra_insn_recog_data[uid]->insn;
6261 if ((set = single_set (insn)) != NULL_RTX)
6263 src = SET_SRC (set);
6264 dest = SET_DEST (set);
6265 if (REG_P (src) && REG_P (dest)
6266 && ((REGNO (src) == regno
6267 && (lra_reg_info[regno].restore_regno
6268 == (int) REGNO (dest)))
6269 || (REGNO (dest) == regno
6270 && (lra_reg_info[regno].restore_regno
6271 == (int) REGNO (src)))))
6273 if (lra_dump_file != NULL)
6275 fprintf (lra_dump_file, " Deleting move %u\n",
6276 INSN_UID (insn));
6277 dump_insn_slim (lra_dump_file, insn);
6279 delete_move_and_clobber (insn, REGNO (dest));
6280 continue;
6282 /* We should not worry about generation memory-memory
6283 moves here as if the corresponding inheritance did
6284 not work (inheritance pseudo did not get a hard reg),
6285 we remove the inheritance pseudo and the optional
6286 reload. */
6288 lra_substitute_pseudo_within_insn
6289 (insn, regno, regno_reg_rtx[lra_reg_info[regno].restore_regno],
6290 false);
6291 lra_update_insn_regno_info (insn);
6292 if (lra_dump_file != NULL)
6294 fprintf (lra_dump_file,
6295 " Restoring original insn:\n");
6296 dump_insn_slim (lra_dump_file, insn);
6300 /* Clear restore_regnos. */
6301 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6302 lra_reg_info[regno].restore_regno = -1;
6303 bitmap_clear (&insn_bitmap);
6304 bitmap_clear (&removed_optional_reload_pseudos);
6305 return change_p;
6308 /* Entry function for undoing inheritance/split transformation. Return true
6309 if we did any RTL change in this pass. */
6310 bool
6311 lra_undo_inheritance (void)
6313 unsigned int regno;
6314 int restore_regno, hard_regno;
6315 int n_all_inherit, n_inherit, n_all_split, n_split;
6316 bitmap_head remove_pseudos;
6317 bitmap_iterator bi;
6318 bool change_p;
6320 lra_undo_inheritance_iter++;
6321 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6322 return false;
6323 if (lra_dump_file != NULL)
6324 fprintf (lra_dump_file,
6325 "\n********** Undoing inheritance #%d: **********\n\n",
6326 lra_undo_inheritance_iter);
6327 bitmap_initialize (&remove_pseudos, &reg_obstack);
6328 n_inherit = n_all_inherit = 0;
6329 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6330 if (lra_reg_info[regno].restore_regno >= 0)
6332 n_all_inherit++;
6333 if (reg_renumber[regno] < 0
6334 /* If the original pseudo changed its allocation, just
6335 removing inheritance is dangerous as for changing
6336 allocation we used shorter live-ranges. */
6337 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6338 bitmap_set_bit (&remove_pseudos, regno);
6339 else
6340 n_inherit++;
6342 if (lra_dump_file != NULL && n_all_inherit != 0)
6343 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6344 n_inherit, n_all_inherit,
6345 (double) n_inherit / n_all_inherit * 100);
6346 n_split = n_all_split = 0;
6347 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6348 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6350 n_all_split++;
6351 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6352 ? reg_renumber[restore_regno] : restore_regno);
6353 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6354 bitmap_set_bit (&remove_pseudos, regno);
6355 else
6357 n_split++;
6358 if (lra_dump_file != NULL)
6359 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6360 regno, restore_regno);
6363 if (lra_dump_file != NULL && n_all_split != 0)
6364 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6365 n_split, n_all_split,
6366 (double) n_split / n_all_split * 100);
6367 change_p = remove_inheritance_pseudos (&remove_pseudos);
6368 bitmap_clear (&remove_pseudos);
6369 /* Clear restore_regnos. */
6370 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6371 lra_reg_info[regno].restore_regno = -1;
6372 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6373 lra_reg_info[regno].restore_regno = -1;
6374 change_p = undo_optional_reloads () || change_p;
6375 return change_p;