2016-08-05 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / lra-constraints.c
blob880eee0890d7e7d9e69e713f2eb475942430d185
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 rtx reg = op;
4110 lra_assert (REG_P (reg));
4111 regno = REGNO (reg);
4112 op = *curr_id->operand_loc[i]; /* Substitution. */
4113 if (GET_CODE (op) == SUBREG)
4114 op = SUBREG_REG (op);
4115 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
4116 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
4117 lra_reg_info[REGNO (op)].restore_rtx = reg;
4118 if (lra_dump_file != NULL)
4119 fprintf (lra_dump_file,
4120 " Making reload reg %d for reg %d optional\n",
4121 REGNO (op), regno);
4124 if (before != NULL_RTX || after != NULL_RTX
4125 || max_regno_before != max_reg_num ())
4126 change_p = true;
4127 if (change_p)
4129 lra_update_operator_dups (curr_id);
4130 /* Something changes -- process the insn. */
4131 lra_update_insn_regno_info (curr_insn);
4133 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
4134 return change_p;
4137 /* Return true if INSN satisfies all constraints. In other words, no
4138 reload insns are needed. */
4139 bool
4140 lra_constrain_insn (rtx_insn *insn)
4142 int saved_new_regno_start = new_regno_start;
4143 int saved_new_insn_uid_start = new_insn_uid_start;
4144 bool change_p;
4146 curr_insn = insn;
4147 curr_id = lra_get_insn_recog_data (curr_insn);
4148 curr_static_id = curr_id->insn_static_data;
4149 new_insn_uid_start = get_max_uid ();
4150 new_regno_start = max_reg_num ();
4151 change_p = curr_insn_transform (true);
4152 new_regno_start = saved_new_regno_start;
4153 new_insn_uid_start = saved_new_insn_uid_start;
4154 return ! change_p;
4157 /* Return true if X is in LIST. */
4158 static bool
4159 in_list_p (rtx x, rtx list)
4161 for (; list != NULL_RTX; list = XEXP (list, 1))
4162 if (XEXP (list, 0) == x)
4163 return true;
4164 return false;
4167 /* Return true if X contains an allocatable hard register (if
4168 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
4169 static bool
4170 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
4172 int i, j;
4173 const char *fmt;
4174 enum rtx_code code;
4176 code = GET_CODE (x);
4177 if (REG_P (x))
4179 int regno = REGNO (x);
4180 HARD_REG_SET alloc_regs;
4182 if (hard_reg_p)
4184 if (regno >= FIRST_PSEUDO_REGISTER)
4185 regno = lra_get_regno_hard_regno (regno);
4186 if (regno < 0)
4187 return false;
4188 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
4189 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
4191 else
4193 if (regno < FIRST_PSEUDO_REGISTER)
4194 return false;
4195 if (! spilled_p)
4196 return true;
4197 return lra_get_regno_hard_regno (regno) < 0;
4200 fmt = GET_RTX_FORMAT (code);
4201 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4203 if (fmt[i] == 'e')
4205 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4206 return true;
4208 else if (fmt[i] == 'E')
4210 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4211 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4212 return true;
4215 return false;
4218 /* Process all regs in location *LOC and change them on equivalent
4219 substitution. Return true if any change was done. */
4220 static bool
4221 loc_equivalence_change_p (rtx *loc)
4223 rtx subst, reg, x = *loc;
4224 bool result = false;
4225 enum rtx_code code = GET_CODE (x);
4226 const char *fmt;
4227 int i, j;
4229 if (code == SUBREG)
4231 reg = SUBREG_REG (x);
4232 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4233 && GET_MODE (subst) == VOIDmode)
4235 /* We cannot reload debug location. Simplify subreg here
4236 while we know the inner mode. */
4237 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4238 GET_MODE (reg), SUBREG_BYTE (x));
4239 return true;
4242 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4244 *loc = subst;
4245 return true;
4248 /* Scan all the operand sub-expressions. */
4249 fmt = GET_RTX_FORMAT (code);
4250 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4252 if (fmt[i] == 'e')
4253 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4254 else if (fmt[i] == 'E')
4255 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4256 result
4257 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4259 return result;
4262 /* Similar to loc_equivalence_change_p, but for use as
4263 simplify_replace_fn_rtx callback. DATA is insn for which the
4264 elimination is done. If it null we don't do the elimination. */
4265 static rtx
4266 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4268 if (!REG_P (loc))
4269 return NULL_RTX;
4271 rtx subst = (data == NULL
4272 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4273 if (subst != loc)
4274 return subst;
4276 return NULL_RTX;
4279 /* Maximum number of generated reload insns per an insn. It is for
4280 preventing this pass cycling in a bug case. */
4281 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4283 /* The current iteration number of this LRA pass. */
4284 int lra_constraint_iter;
4286 /* True if we substituted equiv which needs checking register
4287 allocation correctness because the equivalent value contains
4288 allocatable hard registers or when we restore multi-register
4289 pseudo. */
4290 bool lra_risky_transformations_p;
4292 /* Return true if REGNO is referenced in more than one block. */
4293 static bool
4294 multi_block_pseudo_p (int regno)
4296 basic_block bb = NULL;
4297 unsigned int uid;
4298 bitmap_iterator bi;
4300 if (regno < FIRST_PSEUDO_REGISTER)
4301 return false;
4303 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4304 if (bb == NULL)
4305 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4306 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4307 return true;
4308 return false;
4311 /* Return true if LIST contains a deleted insn. */
4312 static bool
4313 contains_deleted_insn_p (rtx_insn_list *list)
4315 for (; list != NULL_RTX; list = list->next ())
4316 if (NOTE_P (list->insn ())
4317 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4318 return true;
4319 return false;
4322 /* Return true if X contains a pseudo dying in INSN. */
4323 static bool
4324 dead_pseudo_p (rtx x, rtx_insn *insn)
4326 int i, j;
4327 const char *fmt;
4328 enum rtx_code code;
4330 if (REG_P (x))
4331 return (insn != NULL_RTX
4332 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4333 code = GET_CODE (x);
4334 fmt = GET_RTX_FORMAT (code);
4335 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4337 if (fmt[i] == 'e')
4339 if (dead_pseudo_p (XEXP (x, i), insn))
4340 return true;
4342 else if (fmt[i] == 'E')
4344 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4345 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4346 return true;
4349 return false;
4352 /* Return true if INSN contains a dying pseudo in INSN right hand
4353 side. */
4354 static bool
4355 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4357 rtx set = single_set (insn);
4359 gcc_assert (set != NULL);
4360 return dead_pseudo_p (SET_SRC (set), insn);
4363 /* Return true if any init insn of REGNO contains a dying pseudo in
4364 insn right hand side. */
4365 static bool
4366 init_insn_rhs_dead_pseudo_p (int regno)
4368 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4370 if (insns == NULL)
4371 return false;
4372 for (; insns != NULL_RTX; insns = insns->next ())
4373 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4374 return true;
4375 return false;
4378 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4379 reverse only if we have one init insn with given REGNO as a
4380 source. */
4381 static bool
4382 reverse_equiv_p (int regno)
4384 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4385 rtx set;
4387 if (insns == NULL)
4388 return false;
4389 if (! INSN_P (insns->insn ())
4390 || insns->next () != NULL)
4391 return false;
4392 if ((set = single_set (insns->insn ())) == NULL_RTX)
4393 return false;
4394 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4397 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4398 call this function only for non-reverse equivalence. */
4399 static bool
4400 contains_reloaded_insn_p (int regno)
4402 rtx set;
4403 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4405 for (; list != NULL; list = list->next ())
4406 if ((set = single_set (list->insn ())) == NULL_RTX
4407 || ! REG_P (SET_DEST (set))
4408 || (int) REGNO (SET_DEST (set)) != regno)
4409 return true;
4410 return false;
4413 /* Entry function of LRA constraint pass. Return true if the
4414 constraint pass did change the code. */
4415 bool
4416 lra_constraints (bool first_p)
4418 bool changed_p;
4419 int i, hard_regno, new_insns_num;
4420 unsigned int min_len, new_min_len, uid;
4421 rtx set, x, reg, dest_reg;
4422 basic_block last_bb;
4423 bitmap_head equiv_insn_bitmap;
4424 bitmap_iterator bi;
4426 lra_constraint_iter++;
4427 if (lra_dump_file != NULL)
4428 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4429 lra_constraint_iter);
4430 changed_p = false;
4431 if (pic_offset_table_rtx
4432 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4433 lra_risky_transformations_p = true;
4434 else
4435 lra_risky_transformations_p = false;
4436 new_insn_uid_start = get_max_uid ();
4437 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4438 /* Mark used hard regs for target stack size calulations. */
4439 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4440 if (lra_reg_info[i].nrefs != 0
4441 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4443 int j, nregs;
4445 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4446 for (j = 0; j < nregs; j++)
4447 df_set_regs_ever_live (hard_regno + j, true);
4449 /* Do elimination before the equivalence processing as we can spill
4450 some pseudos during elimination. */
4451 lra_eliminate (false, first_p);
4452 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4453 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4454 if (lra_reg_info[i].nrefs != 0)
4456 ira_reg_equiv[i].profitable_p = true;
4457 reg = regno_reg_rtx[i];
4458 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4460 bool pseudo_p = contains_reg_p (x, false, false);
4462 /* After RTL transformation, we can not guarantee that
4463 pseudo in the substitution was not reloaded which might
4464 make equivalence invalid. For example, in reverse
4465 equiv of p0
4467 p0 <- ...
4469 equiv_mem <- p0
4471 the memory address register was reloaded before the 2nd
4472 insn. */
4473 if ((! first_p && pseudo_p)
4474 /* We don't use DF for compilation speed sake. So it
4475 is problematic to update live info when we use an
4476 equivalence containing pseudos in more than one
4477 BB. */
4478 || (pseudo_p && multi_block_pseudo_p (i))
4479 /* If an init insn was deleted for some reason, cancel
4480 the equiv. We could update the equiv insns after
4481 transformations including an equiv insn deletion
4482 but it is not worthy as such cases are extremely
4483 rare. */
4484 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4485 /* If it is not a reverse equivalence, we check that a
4486 pseudo in rhs of the init insn is not dying in the
4487 insn. Otherwise, the live info at the beginning of
4488 the corresponding BB might be wrong after we
4489 removed the insn. When the equiv can be a
4490 constant, the right hand side of the init insn can
4491 be a pseudo. */
4492 || (! reverse_equiv_p (i)
4493 && (init_insn_rhs_dead_pseudo_p (i)
4494 /* If we reloaded the pseudo in an equivalence
4495 init insn, we can not remove the equiv init
4496 insns and the init insns might write into
4497 const memory in this case. */
4498 || contains_reloaded_insn_p (i)))
4499 /* Prevent access beyond equivalent memory for
4500 paradoxical subregs. */
4501 || (MEM_P (x)
4502 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4503 > GET_MODE_SIZE (GET_MODE (x))))
4504 || (pic_offset_table_rtx
4505 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4506 && (targetm.preferred_reload_class
4507 (x, lra_get_allocno_class (i)) == NO_REGS))
4508 || contains_symbol_ref_p (x))))
4509 ira_reg_equiv[i].defined_p = false;
4510 if (contains_reg_p (x, false, true))
4511 ira_reg_equiv[i].profitable_p = false;
4512 if (get_equiv (reg) != reg)
4513 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4516 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4517 update_equiv (i);
4518 /* We should add all insns containing pseudos which should be
4519 substituted by their equivalences. */
4520 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4521 lra_push_insn_by_uid (uid);
4522 min_len = lra_insn_stack_length ();
4523 new_insns_num = 0;
4524 last_bb = NULL;
4525 changed_p = false;
4526 while ((new_min_len = lra_insn_stack_length ()) != 0)
4528 curr_insn = lra_pop_insn ();
4529 --new_min_len;
4530 curr_bb = BLOCK_FOR_INSN (curr_insn);
4531 if (curr_bb != last_bb)
4533 last_bb = curr_bb;
4534 bb_reload_num = lra_curr_reload_num;
4536 if (min_len > new_min_len)
4538 min_len = new_min_len;
4539 new_insns_num = 0;
4541 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4542 internal_error
4543 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4544 MAX_RELOAD_INSNS_NUMBER);
4545 new_insns_num++;
4546 if (DEBUG_INSN_P (curr_insn))
4548 /* We need to check equivalence in debug insn and change
4549 pseudo to the equivalent value if necessary. */
4550 curr_id = lra_get_insn_recog_data (curr_insn);
4551 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4553 rtx old = *curr_id->operand_loc[0];
4554 *curr_id->operand_loc[0]
4555 = simplify_replace_fn_rtx (old, NULL_RTX,
4556 loc_equivalence_callback, curr_insn);
4557 if (old != *curr_id->operand_loc[0])
4559 lra_update_insn_regno_info (curr_insn);
4560 changed_p = true;
4564 else if (INSN_P (curr_insn))
4566 if ((set = single_set (curr_insn)) != NULL_RTX)
4568 dest_reg = SET_DEST (set);
4569 /* The equivalence pseudo could be set up as SUBREG in a
4570 case when it is a call restore insn in a mode
4571 different from the pseudo mode. */
4572 if (GET_CODE (dest_reg) == SUBREG)
4573 dest_reg = SUBREG_REG (dest_reg);
4574 if ((REG_P (dest_reg)
4575 && (x = get_equiv (dest_reg)) != dest_reg
4576 /* Remove insns which set up a pseudo whose value
4577 can not be changed. Such insns might be not in
4578 init_insns because we don't update equiv data
4579 during insn transformations.
4581 As an example, let suppose that a pseudo got
4582 hard register and on the 1st pass was not
4583 changed to equivalent constant. We generate an
4584 additional insn setting up the pseudo because of
4585 secondary memory movement. Then the pseudo is
4586 spilled and we use the equiv constant. In this
4587 case we should remove the additional insn and
4588 this insn is not init_insns list. */
4589 && (! MEM_P (x) || MEM_READONLY_P (x)
4590 /* Check that this is actually an insn setting
4591 up the equivalence. */
4592 || in_list_p (curr_insn,
4593 ira_reg_equiv
4594 [REGNO (dest_reg)].init_insns)))
4595 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4596 && in_list_p (curr_insn,
4597 ira_reg_equiv
4598 [REGNO (SET_SRC (set))].init_insns)))
4600 /* This is equiv init insn of pseudo which did not get a
4601 hard register -- remove the insn. */
4602 if (lra_dump_file != NULL)
4604 fprintf (lra_dump_file,
4605 " Removing equiv init insn %i (freq=%d)\n",
4606 INSN_UID (curr_insn),
4607 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4608 dump_insn_slim (lra_dump_file, curr_insn);
4610 if (contains_reg_p (x, true, false))
4611 lra_risky_transformations_p = true;
4612 lra_set_insn_deleted (curr_insn);
4613 continue;
4616 curr_id = lra_get_insn_recog_data (curr_insn);
4617 curr_static_id = curr_id->insn_static_data;
4618 init_curr_insn_input_reloads ();
4619 init_curr_operand_mode ();
4620 if (curr_insn_transform (false))
4621 changed_p = true;
4622 /* Check non-transformed insns too for equiv change as USE
4623 or CLOBBER don't need reloads but can contain pseudos
4624 being changed on their equivalences. */
4625 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4626 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4628 lra_update_insn_regno_info (curr_insn);
4629 changed_p = true;
4633 bitmap_clear (&equiv_insn_bitmap);
4634 /* If we used a new hard regno, changed_p should be true because the
4635 hard reg is assigned to a new pseudo. */
4636 if (flag_checking && !changed_p)
4638 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4639 if (lra_reg_info[i].nrefs != 0
4640 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4642 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4644 for (j = 0; j < nregs; j++)
4645 lra_assert (df_regs_ever_live_p (hard_regno + j));
4648 return changed_p;
4651 static void initiate_invariants (void);
4652 static void finish_invariants (void);
4654 /* Initiate the LRA constraint pass. It is done once per
4655 function. */
4656 void
4657 lra_constraints_init (void)
4659 initiate_invariants ();
4662 /* Finalize the LRA constraint pass. It is done once per
4663 function. */
4664 void
4665 lra_constraints_finish (void)
4667 finish_invariants ();
4672 /* Structure describes invariants for ineheritance. */
4673 struct invariant
4675 /* The order number of the invariant. */
4676 int num;
4677 /* The invariant RTX. */
4678 rtx invariant_rtx;
4679 /* The origin insn of the invariant. */
4680 rtx_insn *insn;
4683 typedef struct invariant invariant_t;
4684 typedef invariant_t *invariant_ptr_t;
4685 typedef const invariant_t *const_invariant_ptr_t;
4687 /* Pointer to the inheritance invariants. */
4688 static vec<invariant_ptr_t> invariants;
4690 /* Allocation pool for the invariants. */
4691 static object_allocator<struct invariant> *invariants_pool;
4693 /* Hash table for the invariants. */
4694 static htab_t invariant_table;
4696 /* Hash function for INVARIANT. */
4697 static hashval_t
4698 invariant_hash (const void *invariant)
4700 rtx inv = ((const_invariant_ptr_t) invariant)->invariant_rtx;
4701 return lra_rtx_hash (inv);
4704 /* Equal function for invariants INVARIANT1 and INVARIANT2. */
4705 static int
4706 invariant_eq_p (const void *invariant1, const void *invariant2)
4708 rtx inv1 = ((const_invariant_ptr_t) invariant1)->invariant_rtx;
4709 rtx inv2 = ((const_invariant_ptr_t) invariant2)->invariant_rtx;
4711 return rtx_equal_p (inv1, inv2);
4714 /* Insert INVARIANT_RTX into the table if it is not there yet. Return
4715 invariant which is in the table. */
4716 static invariant_ptr_t
4717 insert_invariant (rtx invariant_rtx)
4719 void **entry_ptr;
4720 invariant_t invariant;
4721 invariant_ptr_t invariant_ptr;
4723 invariant.invariant_rtx = invariant_rtx;
4724 entry_ptr = htab_find_slot (invariant_table, &invariant, INSERT);
4725 if (*entry_ptr == NULL)
4727 invariant_ptr = invariants_pool->allocate ();
4728 invariant_ptr->invariant_rtx = invariant_rtx;
4729 invariant_ptr->insn = NULL;
4730 invariants.safe_push (invariant_ptr);
4731 *entry_ptr = (void *) invariant_ptr;
4733 return (invariant_ptr_t) *entry_ptr;
4736 /* Initiate the invariant table. */
4737 static void
4738 initiate_invariants (void)
4740 invariants.create (100);
4741 invariants_pool = new object_allocator<struct invariant> ("Inheritance invariants");
4742 invariant_table = htab_create (100, invariant_hash, invariant_eq_p, NULL);
4745 /* Finish the invariant table. */
4746 static void
4747 finish_invariants (void)
4749 htab_delete (invariant_table);
4750 delete invariants_pool;
4751 invariants.release ();
4754 /* Make the invariant table empty. */
4755 static void
4756 clear_invariants (void)
4758 htab_empty (invariant_table);
4759 invariants_pool->release ();
4760 invariants.truncate (0);
4765 /* This page contains code to do inheritance/split
4766 transformations. */
4768 /* Number of reloads passed so far in current EBB. */
4769 static int reloads_num;
4771 /* Number of calls passed so far in current EBB. */
4772 static int calls_num;
4774 /* Current reload pseudo check for validity of elements in
4775 USAGE_INSNS. */
4776 static int curr_usage_insns_check;
4778 /* Info about last usage of registers in EBB to do inheritance/split
4779 transformation. Inheritance transformation is done from a spilled
4780 pseudo and split transformations from a hard register or a pseudo
4781 assigned to a hard register. */
4782 struct usage_insns
4784 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4785 value INSNS is valid. The insns is chain of optional debug insns
4786 and a finishing non-debug insn using the corresponding reg. The
4787 value is also used to mark the registers which are set up in the
4788 current insn. The negated insn uid is used for this. */
4789 int check;
4790 /* Value of global reloads_num at the last insn in INSNS. */
4791 int reloads_num;
4792 /* Value of global reloads_nums at the last insn in INSNS. */
4793 int calls_num;
4794 /* It can be true only for splitting. And it means that the restore
4795 insn should be put after insn given by the following member. */
4796 bool after_p;
4797 /* Next insns in the current EBB which use the original reg and the
4798 original reg value is not changed between the current insn and
4799 the next insns. In order words, e.g. for inheritance, if we need
4800 to use the original reg value again in the next insns we can try
4801 to use the value in a hard register from a reload insn of the
4802 current insn. */
4803 rtx insns;
4806 /* Map: regno -> corresponding pseudo usage insns. */
4807 static struct usage_insns *usage_insns;
4809 static void
4810 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4812 usage_insns[regno].check = curr_usage_insns_check;
4813 usage_insns[regno].insns = insn;
4814 usage_insns[regno].reloads_num = reloads_num;
4815 usage_insns[regno].calls_num = calls_num;
4816 usage_insns[regno].after_p = after_p;
4819 /* The function is used to form list REGNO usages which consists of
4820 optional debug insns finished by a non-debug insn using REGNO.
4821 RELOADS_NUM is current number of reload insns processed so far. */
4822 static void
4823 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
4825 rtx next_usage_insns;
4827 if (usage_insns[regno].check == curr_usage_insns_check
4828 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4829 && DEBUG_INSN_P (insn))
4831 /* Check that we did not add the debug insn yet. */
4832 if (next_usage_insns != insn
4833 && (GET_CODE (next_usage_insns) != INSN_LIST
4834 || XEXP (next_usage_insns, 0) != insn))
4835 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4836 next_usage_insns);
4838 else if (NONDEBUG_INSN_P (insn))
4839 setup_next_usage_insn (regno, insn, reloads_num, false);
4840 else
4841 usage_insns[regno].check = 0;
4844 /* Return first non-debug insn in list USAGE_INSNS. */
4845 static rtx_insn *
4846 skip_usage_debug_insns (rtx usage_insns)
4848 rtx insn;
4850 /* Skip debug insns. */
4851 for (insn = usage_insns;
4852 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4853 insn = XEXP (insn, 1))
4855 return safe_as_a <rtx_insn *> (insn);
4858 /* Return true if we need secondary memory moves for insn in
4859 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4860 into the insn. */
4861 static bool
4862 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4863 rtx usage_insns ATTRIBUTE_UNUSED)
4865 #ifndef SECONDARY_MEMORY_NEEDED
4866 return false;
4867 #else
4868 rtx_insn *insn;
4869 rtx set, dest;
4870 enum reg_class cl;
4872 if (inher_cl == ALL_REGS
4873 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4874 return false;
4875 lra_assert (INSN_P (insn));
4876 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4877 return false;
4878 dest = SET_DEST (set);
4879 if (! REG_P (dest))
4880 return false;
4881 lra_assert (inher_cl != NO_REGS);
4882 cl = get_reg_class (REGNO (dest));
4883 return (cl != NO_REGS && cl != ALL_REGS
4884 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4885 #endif
4888 /* Registers involved in inheritance/split in the current EBB
4889 (inheritance/split pseudos and original registers). */
4890 static bitmap_head check_only_regs;
4892 /* Reload pseudos can not be involded in invariant inheritance in the
4893 current EBB. */
4894 static bitmap_head invalid_invariant_regs;
4896 /* Do inheritance transformations for insn INSN, which defines (if
4897 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4898 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4899 form as the "insns" field of usage_insns. Return true if we
4900 succeed in such transformation.
4902 The transformations look like:
4904 p <- ... i <- ...
4905 ... p <- i (new insn)
4906 ... =>
4907 <- ... p ... <- ... i ...
4909 ... i <- p (new insn)
4910 <- ... p ... <- ... i ...
4911 ... =>
4912 <- ... p ... <- ... i ...
4913 where p is a spilled original pseudo and i is a new inheritance pseudo.
4916 The inheritance pseudo has the smallest class of two classes CL and
4917 class of ORIGINAL REGNO. */
4918 static bool
4919 inherit_reload_reg (bool def_p, int original_regno,
4920 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4922 if (optimize_function_for_size_p (cfun))
4923 return false;
4925 enum reg_class rclass = lra_get_allocno_class (original_regno);
4926 rtx original_reg = regno_reg_rtx[original_regno];
4927 rtx new_reg, usage_insn;
4928 rtx_insn *new_insns;
4930 lra_assert (! usage_insns[original_regno].after_p);
4931 if (lra_dump_file != NULL)
4932 fprintf (lra_dump_file,
4933 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4934 if (! ira_reg_classes_intersect_p[cl][rclass])
4936 if (lra_dump_file != NULL)
4938 fprintf (lra_dump_file,
4939 " Rejecting inheritance for %d "
4940 "because of disjoint classes %s and %s\n",
4941 original_regno, reg_class_names[cl],
4942 reg_class_names[rclass]);
4943 fprintf (lra_dump_file,
4944 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4946 return false;
4948 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4949 /* We don't use a subset of two classes because it can be
4950 NO_REGS. This transformation is still profitable in most
4951 cases even if the classes are not intersected as register
4952 move is probably cheaper than a memory load. */
4953 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4955 if (lra_dump_file != NULL)
4956 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4957 reg_class_names[cl], reg_class_names[rclass]);
4959 rclass = cl;
4961 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4963 /* Reject inheritance resulting in secondary memory moves.
4964 Otherwise, there is a danger in LRA cycling. Also such
4965 transformation will be unprofitable. */
4966 if (lra_dump_file != NULL)
4968 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4969 rtx set = single_set (insn);
4971 lra_assert (set != NULL_RTX);
4973 rtx dest = SET_DEST (set);
4975 lra_assert (REG_P (dest));
4976 fprintf (lra_dump_file,
4977 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4978 "as secondary mem is needed\n",
4979 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4980 original_regno, reg_class_names[rclass]);
4981 fprintf (lra_dump_file,
4982 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4984 return false;
4986 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4987 rclass, "inheritance");
4988 start_sequence ();
4989 if (def_p)
4990 lra_emit_move (original_reg, new_reg);
4991 else
4992 lra_emit_move (new_reg, original_reg);
4993 new_insns = get_insns ();
4994 end_sequence ();
4995 if (NEXT_INSN (new_insns) != NULL_RTX)
4997 if (lra_dump_file != NULL)
4999 fprintf (lra_dump_file,
5000 " Rejecting inheritance %d->%d "
5001 "as it results in 2 or more insns:\n",
5002 original_regno, REGNO (new_reg));
5003 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
5004 fprintf (lra_dump_file,
5005 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5007 return false;
5009 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
5010 lra_update_insn_regno_info (insn);
5011 if (! def_p)
5012 /* We now have a new usage insn for original regno. */
5013 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
5014 if (lra_dump_file != NULL)
5015 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
5016 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
5017 lra_reg_info[REGNO (new_reg)].restore_rtx = regno_reg_rtx[original_regno];
5018 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5019 bitmap_set_bit (&check_only_regs, original_regno);
5020 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
5021 if (def_p)
5022 lra_process_new_insns (insn, NULL, new_insns,
5023 "Add original<-inheritance");
5024 else
5025 lra_process_new_insns (insn, new_insns, NULL,
5026 "Add inheritance<-original");
5027 while (next_usage_insns != NULL_RTX)
5029 if (GET_CODE (next_usage_insns) != INSN_LIST)
5031 usage_insn = next_usage_insns;
5032 lra_assert (NONDEBUG_INSN_P (usage_insn));
5033 next_usage_insns = NULL;
5035 else
5037 usage_insn = XEXP (next_usage_insns, 0);
5038 lra_assert (DEBUG_INSN_P (usage_insn));
5039 next_usage_insns = XEXP (next_usage_insns, 1);
5041 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5042 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5043 if (lra_dump_file != NULL)
5045 fprintf (lra_dump_file,
5046 " Inheritance reuse change %d->%d (bb%d):\n",
5047 original_regno, REGNO (new_reg),
5048 BLOCK_FOR_INSN (usage_insn)->index);
5049 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5052 if (lra_dump_file != NULL)
5053 fprintf (lra_dump_file,
5054 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5055 return true;
5058 /* Return true if we need a caller save/restore for pseudo REGNO which
5059 was assigned to a hard register. */
5060 static inline bool
5061 need_for_call_save_p (int regno)
5063 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
5064 return (usage_insns[regno].calls_num < calls_num
5065 && (overlaps_hard_reg_set_p
5066 ((flag_ipa_ra &&
5067 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
5068 ? lra_reg_info[regno].actual_call_used_reg_set
5069 : call_used_reg_set,
5070 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
5071 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
5072 PSEUDO_REGNO_MODE (regno))));
5075 /* Global registers occurring in the current EBB. */
5076 static bitmap_head ebb_global_regs;
5078 /* Return true if we need a split for hard register REGNO or pseudo
5079 REGNO which was assigned to a hard register.
5080 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
5081 used for reloads since the EBB end. It is an approximation of the
5082 used hard registers in the split range. The exact value would
5083 require expensive calculations. If we were aggressive with
5084 splitting because of the approximation, the split pseudo will save
5085 the same hard register assignment and will be removed in the undo
5086 pass. We still need the approximation because too aggressive
5087 splitting would result in too inaccurate cost calculation in the
5088 assignment pass because of too many generated moves which will be
5089 probably removed in the undo pass. */
5090 static inline bool
5091 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
5093 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
5095 lra_assert (hard_regno >= 0);
5096 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
5097 /* Don't split eliminable hard registers, otherwise we can
5098 split hard registers like hard frame pointer, which
5099 lives on BB start/end according to DF-infrastructure,
5100 when there is a pseudo assigned to the register and
5101 living in the same BB. */
5102 && (regno >= FIRST_PSEUDO_REGISTER
5103 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
5104 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
5105 /* Don't split call clobbered hard regs living through
5106 calls, otherwise we might have a check problem in the
5107 assign sub-pass as in the most cases (exception is a
5108 situation when lra_risky_transformations_p value is
5109 true) the assign pass assumes that all pseudos living
5110 through calls are assigned to call saved hard regs. */
5111 && (regno >= FIRST_PSEUDO_REGISTER
5112 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
5113 || usage_insns[regno].calls_num == calls_num)
5114 /* We need at least 2 reloads to make pseudo splitting
5115 profitable. We should provide hard regno splitting in
5116 any case to solve 1st insn scheduling problem when
5117 moving hard register definition up might result in
5118 impossibility to find hard register for reload pseudo of
5119 small register class. */
5120 && (usage_insns[regno].reloads_num
5121 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
5122 && (regno < FIRST_PSEUDO_REGISTER
5123 /* For short living pseudos, spilling + inheritance can
5124 be considered a substitution for splitting.
5125 Therefore we do not splitting for local pseudos. It
5126 decreases also aggressiveness of splitting. The
5127 minimal number of references is chosen taking into
5128 account that for 2 references splitting has no sense
5129 as we can just spill the pseudo. */
5130 || (regno >= FIRST_PSEUDO_REGISTER
5131 && lra_reg_info[regno].nrefs > 3
5132 && bitmap_bit_p (&ebb_global_regs, regno))))
5133 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
5136 /* Return class for the split pseudo created from original pseudo with
5137 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
5138 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
5139 results in no secondary memory movements. */
5140 static enum reg_class
5141 choose_split_class (enum reg_class allocno_class,
5142 int hard_regno ATTRIBUTE_UNUSED,
5143 machine_mode mode ATTRIBUTE_UNUSED)
5145 #ifndef SECONDARY_MEMORY_NEEDED
5146 return allocno_class;
5147 #else
5148 int i;
5149 enum reg_class cl, best_cl = NO_REGS;
5150 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
5151 = REGNO_REG_CLASS (hard_regno);
5153 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
5154 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
5155 return allocno_class;
5156 for (i = 0;
5157 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
5158 i++)
5159 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
5160 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
5161 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
5162 && (best_cl == NO_REGS
5163 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
5164 best_cl = cl;
5165 return best_cl;
5166 #endif
5169 /* Do split transformations for insn INSN, which defines or uses
5170 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
5171 the EBB next uses ORIGINAL_REGNO; it has the same form as the
5172 "insns" field of usage_insns.
5174 The transformations look like:
5176 p <- ... p <- ...
5177 ... s <- p (new insn -- save)
5178 ... =>
5179 ... p <- s (new insn -- restore)
5180 <- ... p ... <- ... p ...
5182 <- ... p ... <- ... p ...
5183 ... s <- p (new insn -- save)
5184 ... =>
5185 ... p <- s (new insn -- restore)
5186 <- ... p ... <- ... p ...
5188 where p is an original pseudo got a hard register or a hard
5189 register and s is a new split pseudo. The save is put before INSN
5190 if BEFORE_P is true. Return true if we succeed in such
5191 transformation. */
5192 static bool
5193 split_reg (bool before_p, int original_regno, rtx_insn *insn,
5194 rtx next_usage_insns)
5196 enum reg_class rclass;
5197 rtx original_reg;
5198 int hard_regno, nregs;
5199 rtx new_reg, usage_insn;
5200 rtx_insn *restore, *save;
5201 bool after_p;
5202 bool call_save_p;
5203 machine_mode mode;
5205 if (original_regno < FIRST_PSEUDO_REGISTER)
5207 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
5208 hard_regno = original_regno;
5209 call_save_p = false;
5210 nregs = 1;
5211 mode = lra_reg_info[hard_regno].biggest_mode;
5212 machine_mode reg_rtx_mode = GET_MODE (regno_reg_rtx[hard_regno]);
5213 /* A reg can have a biggest_mode of VOIDmode if it was only ever seen
5214 as part of a multi-word register. In that case, or if the biggest
5215 mode was larger than a register, just use the reg_rtx. Otherwise,
5216 limit the size to that of the biggest access in the function. */
5217 if (mode == VOIDmode
5218 || GET_MODE_SIZE (mode) > GET_MODE_SIZE (reg_rtx_mode))
5220 original_reg = regno_reg_rtx[hard_regno];
5221 mode = reg_rtx_mode;
5223 else
5224 original_reg = gen_rtx_REG (mode, hard_regno);
5226 else
5228 mode = PSEUDO_REGNO_MODE (original_regno);
5229 hard_regno = reg_renumber[original_regno];
5230 nregs = hard_regno_nregs[hard_regno][mode];
5231 rclass = lra_get_allocno_class (original_regno);
5232 original_reg = regno_reg_rtx[original_regno];
5233 call_save_p = need_for_call_save_p (original_regno);
5235 lra_assert (hard_regno >= 0);
5236 if (lra_dump_file != NULL)
5237 fprintf (lra_dump_file,
5238 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
5240 if (call_save_p)
5242 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
5243 hard_regno_nregs[hard_regno][mode],
5244 mode);
5245 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
5247 else
5249 rclass = choose_split_class (rclass, hard_regno, mode);
5250 if (rclass == NO_REGS)
5252 if (lra_dump_file != NULL)
5254 fprintf (lra_dump_file,
5255 " Rejecting split of %d(%s): "
5256 "no good reg class for %d(%s)\n",
5257 original_regno,
5258 reg_class_names[lra_get_allocno_class (original_regno)],
5259 hard_regno,
5260 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
5261 fprintf
5262 (lra_dump_file,
5263 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5265 return false;
5267 new_reg = lra_create_new_reg (mode, original_reg, rclass, "split");
5268 reg_renumber[REGNO (new_reg)] = hard_regno;
5270 save = emit_spill_move (true, new_reg, original_reg);
5271 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
5273 if (lra_dump_file != NULL)
5275 fprintf
5276 (lra_dump_file,
5277 " Rejecting split %d->%d resulting in > 2 save insns:\n",
5278 original_regno, REGNO (new_reg));
5279 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
5280 fprintf (lra_dump_file,
5281 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5283 return false;
5285 restore = emit_spill_move (false, new_reg, original_reg);
5286 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
5288 if (lra_dump_file != NULL)
5290 fprintf (lra_dump_file,
5291 " Rejecting split %d->%d "
5292 "resulting in > 2 restore insns:\n",
5293 original_regno, REGNO (new_reg));
5294 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
5295 fprintf (lra_dump_file,
5296 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5298 return false;
5300 after_p = usage_insns[original_regno].after_p;
5301 lra_reg_info[REGNO (new_reg)].restore_rtx = regno_reg_rtx[original_regno];
5302 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5303 bitmap_set_bit (&check_only_regs, original_regno);
5304 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
5305 for (;;)
5307 if (GET_CODE (next_usage_insns) != INSN_LIST)
5309 usage_insn = next_usage_insns;
5310 break;
5312 usage_insn = XEXP (next_usage_insns, 0);
5313 lra_assert (DEBUG_INSN_P (usage_insn));
5314 next_usage_insns = XEXP (next_usage_insns, 1);
5315 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5316 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5317 if (lra_dump_file != NULL)
5319 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5320 original_regno, REGNO (new_reg));
5321 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5324 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5325 lra_assert (usage_insn != insn || (after_p && before_p));
5326 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5327 after_p ? NULL : restore,
5328 after_p ? restore : NULL,
5329 call_save_p
5330 ? "Add reg<-save" : "Add reg<-split");
5331 lra_process_new_insns (insn, before_p ? save : NULL,
5332 before_p ? NULL : save,
5333 call_save_p
5334 ? "Add save<-reg" : "Add split<-reg");
5335 if (nregs > 1)
5336 /* If we are trying to split multi-register. We should check
5337 conflicts on the next assignment sub-pass. IRA can allocate on
5338 sub-register levels, LRA do this on pseudos level right now and
5339 this discrepancy may create allocation conflicts after
5340 splitting. */
5341 lra_risky_transformations_p = true;
5342 if (lra_dump_file != NULL)
5343 fprintf (lra_dump_file,
5344 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5345 return true;
5348 /* Recognize that we need a split transformation for insn INSN, which
5349 defines or uses REGNO in its insn biggest MODE (we use it only if
5350 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5351 hard registers which might be used for reloads since the EBB end.
5352 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5353 uid before starting INSN processing. Return true if we succeed in
5354 such transformation. */
5355 static bool
5356 split_if_necessary (int regno, machine_mode mode,
5357 HARD_REG_SET potential_reload_hard_regs,
5358 bool before_p, rtx_insn *insn, int max_uid)
5360 bool res = false;
5361 int i, nregs = 1;
5362 rtx next_usage_insns;
5364 if (regno < FIRST_PSEUDO_REGISTER)
5365 nregs = hard_regno_nregs[regno][mode];
5366 for (i = 0; i < nregs; i++)
5367 if (usage_insns[regno + i].check == curr_usage_insns_check
5368 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5369 /* To avoid processing the register twice or more. */
5370 && ((GET_CODE (next_usage_insns) != INSN_LIST
5371 && INSN_UID (next_usage_insns) < max_uid)
5372 || (GET_CODE (next_usage_insns) == INSN_LIST
5373 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5374 && need_for_split_p (potential_reload_hard_regs, regno + i)
5375 && split_reg (before_p, regno + i, insn, next_usage_insns))
5376 res = true;
5377 return res;
5380 /* Return TRUE if rtx X is considered as an invariant for
5381 inheritance. */
5382 static bool
5383 invariant_p (const_rtx x)
5385 machine_mode mode;
5386 const char *fmt;
5387 enum rtx_code code;
5388 int i, j;
5390 code = GET_CODE (x);
5391 mode = GET_MODE (x);
5392 if (code == SUBREG)
5394 x = SUBREG_REG (x);
5395 code = GET_CODE (x);
5396 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
5397 mode = GET_MODE (x);
5400 if (MEM_P (x))
5401 return false;
5403 if (REG_P (x))
5405 int i, nregs, regno = REGNO (x);
5407 if (regno >= FIRST_PSEUDO_REGISTER || regno == STACK_POINTER_REGNUM
5408 || TEST_HARD_REG_BIT (eliminable_regset, regno)
5409 || GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
5410 return false;
5411 nregs = hard_regno_nregs[regno][mode];
5412 for (i = 0; i < nregs; i++)
5413 if (! fixed_regs[regno + i]
5414 /* A hard register may be clobbered in the current insn
5415 but we can ignore this case because if the hard
5416 register is used it should be set somewhere after the
5417 clobber. */
5418 || bitmap_bit_p (&invalid_invariant_regs, regno + i))
5419 return false;
5421 fmt = GET_RTX_FORMAT (code);
5422 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5424 if (fmt[i] == 'e')
5426 if (! invariant_p (XEXP (x, i)))
5427 return false;
5429 else if (fmt[i] == 'E')
5431 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5432 if (! invariant_p (XVECEXP (x, i, j)))
5433 return false;
5436 return true;
5439 /* We have 'dest_reg <- invariant'. Let us try to make an invariant
5440 inheritance transformation (using dest_reg instead invariant in a
5441 subsequent insn). */
5442 static bool
5443 process_invariant_for_inheritance (rtx dst_reg, rtx invariant_rtx)
5445 invariant_ptr_t invariant_ptr;
5446 rtx_insn *insn, *new_insns;
5447 rtx insn_set, insn_reg, new_reg;
5448 int insn_regno;
5449 bool succ_p = false;
5450 int dst_regno = REGNO (dst_reg);
5451 enum machine_mode dst_mode = GET_MODE (dst_reg);
5452 enum reg_class cl = lra_get_allocno_class (dst_regno), insn_reg_cl;
5454 invariant_ptr = insert_invariant (invariant_rtx);
5455 if ((insn = invariant_ptr->insn) != NULL_RTX)
5457 /* We have a subsequent insn using the invariant. */
5458 insn_set = single_set (insn);
5459 lra_assert (insn_set != NULL);
5460 insn_reg = SET_DEST (insn_set);
5461 lra_assert (REG_P (insn_reg));
5462 insn_regno = REGNO (insn_reg);
5463 insn_reg_cl = lra_get_allocno_class (insn_regno);
5465 if (dst_mode == GET_MODE (insn_reg)
5466 /* We should consider only result move reg insns which are
5467 cheap. */
5468 && targetm.register_move_cost (dst_mode, cl, insn_reg_cl) == 2
5469 && targetm.register_move_cost (dst_mode, cl, cl) == 2)
5471 if (lra_dump_file != NULL)
5472 fprintf (lra_dump_file,
5473 " [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[\n");
5474 new_reg = lra_create_new_reg (dst_mode, dst_reg,
5475 cl, "invariant inheritance");
5476 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
5477 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5478 lra_reg_info[REGNO (new_reg)].restore_rtx = PATTERN (insn);
5479 start_sequence ();
5480 lra_emit_move (new_reg, dst_reg);
5481 new_insns = get_insns ();
5482 end_sequence ();
5483 lra_process_new_insns (curr_insn, NULL, new_insns,
5484 "Add invariant inheritance<-original");
5485 start_sequence ();
5486 lra_emit_move (SET_DEST (insn_set), new_reg);
5487 new_insns = get_insns ();
5488 end_sequence ();
5489 lra_process_new_insns (insn, NULL, new_insns,
5490 "Changing reload<-inheritance");
5491 lra_set_insn_deleted (insn);
5492 succ_p = true;
5493 if (lra_dump_file != NULL)
5495 fprintf (lra_dump_file,
5496 " Invariant inheritance reuse change %d (bb%d):\n",
5497 REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
5498 dump_insn_slim (lra_dump_file, insn);
5499 fprintf (lra_dump_file,
5500 " ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]\n");
5504 invariant_ptr->insn = curr_insn;
5505 return succ_p;
5508 /* Check only registers living at the current program point in the
5509 current EBB. */
5510 static bitmap_head live_regs;
5512 /* Update live info in EBB given by its HEAD and TAIL insns after
5513 inheritance/split transformation. The function removes dead moves
5514 too. */
5515 static void
5516 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5518 unsigned int j;
5519 int i, regno;
5520 bool live_p;
5521 rtx_insn *prev_insn;
5522 rtx set;
5523 bool remove_p;
5524 basic_block last_bb, prev_bb, curr_bb;
5525 bitmap_iterator bi;
5526 struct lra_insn_reg *reg;
5527 edge e;
5528 edge_iterator ei;
5530 last_bb = BLOCK_FOR_INSN (tail);
5531 prev_bb = NULL;
5532 for (curr_insn = tail;
5533 curr_insn != PREV_INSN (head);
5534 curr_insn = prev_insn)
5536 prev_insn = PREV_INSN (curr_insn);
5537 /* We need to process empty blocks too. They contain
5538 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5539 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5540 continue;
5541 curr_bb = BLOCK_FOR_INSN (curr_insn);
5542 if (curr_bb != prev_bb)
5544 if (prev_bb != NULL)
5546 /* Update df_get_live_in (prev_bb): */
5547 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5548 if (bitmap_bit_p (&live_regs, j))
5549 bitmap_set_bit (df_get_live_in (prev_bb), j);
5550 else
5551 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5553 if (curr_bb != last_bb)
5555 /* Update df_get_live_out (curr_bb): */
5556 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5558 live_p = bitmap_bit_p (&live_regs, j);
5559 if (! live_p)
5560 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5561 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5563 live_p = true;
5564 break;
5566 if (live_p)
5567 bitmap_set_bit (df_get_live_out (curr_bb), j);
5568 else
5569 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5572 prev_bb = curr_bb;
5573 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5575 if (! NONDEBUG_INSN_P (curr_insn))
5576 continue;
5577 curr_id = lra_get_insn_recog_data (curr_insn);
5578 curr_static_id = curr_id->insn_static_data;
5579 remove_p = false;
5580 if ((set = single_set (curr_insn)) != NULL_RTX
5581 && REG_P (SET_DEST (set))
5582 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5583 && SET_DEST (set) != pic_offset_table_rtx
5584 && bitmap_bit_p (&check_only_regs, regno)
5585 && ! bitmap_bit_p (&live_regs, regno))
5586 remove_p = true;
5587 /* See which defined values die here. */
5588 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5589 if (reg->type == OP_OUT && ! reg->subreg_p)
5590 bitmap_clear_bit (&live_regs, reg->regno);
5591 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5592 if (reg->type == OP_OUT && ! reg->subreg_p)
5593 bitmap_clear_bit (&live_regs, reg->regno);
5594 if (curr_id->arg_hard_regs != NULL)
5595 /* Make clobbered argument hard registers die. */
5596 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5597 if (regno >= FIRST_PSEUDO_REGISTER)
5598 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
5599 /* Mark each used value as live. */
5600 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5601 if (reg->type != OP_OUT
5602 && bitmap_bit_p (&check_only_regs, reg->regno))
5603 bitmap_set_bit (&live_regs, reg->regno);
5604 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5605 if (reg->type != OP_OUT
5606 && bitmap_bit_p (&check_only_regs, reg->regno))
5607 bitmap_set_bit (&live_regs, reg->regno);
5608 if (curr_id->arg_hard_regs != NULL)
5609 /* Make used argument hard registers live. */
5610 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5611 if (regno < FIRST_PSEUDO_REGISTER
5612 && bitmap_bit_p (&check_only_regs, regno))
5613 bitmap_set_bit (&live_regs, regno);
5614 /* It is quite important to remove dead move insns because it
5615 means removing dead store. We don't need to process them for
5616 constraints. */
5617 if (remove_p)
5619 if (lra_dump_file != NULL)
5621 fprintf (lra_dump_file, " Removing dead insn:\n ");
5622 dump_insn_slim (lra_dump_file, curr_insn);
5624 lra_set_insn_deleted (curr_insn);
5629 /* The structure describes info to do an inheritance for the current
5630 insn. We need to collect such info first before doing the
5631 transformations because the transformations change the insn
5632 internal representation. */
5633 struct to_inherit
5635 /* Original regno. */
5636 int regno;
5637 /* Subsequent insns which can inherit original reg value. */
5638 rtx insns;
5641 /* Array containing all info for doing inheritance from the current
5642 insn. */
5643 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5645 /* Number elements in the previous array. */
5646 static int to_inherit_num;
5648 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5649 structure to_inherit. */
5650 static void
5651 add_to_inherit (int regno, rtx insns)
5653 int i;
5655 for (i = 0; i < to_inherit_num; i++)
5656 if (to_inherit[i].regno == regno)
5657 return;
5658 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5659 to_inherit[to_inherit_num].regno = regno;
5660 to_inherit[to_inherit_num++].insns = insns;
5663 /* Return the last non-debug insn in basic block BB, or the block begin
5664 note if none. */
5665 static rtx_insn *
5666 get_last_insertion_point (basic_block bb)
5668 rtx_insn *insn;
5670 FOR_BB_INSNS_REVERSE (bb, insn)
5671 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5672 return insn;
5673 gcc_unreachable ();
5676 /* Set up RES by registers living on edges FROM except the edge (FROM,
5677 TO) or by registers set up in a jump insn in BB FROM. */
5678 static void
5679 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5681 rtx_insn *last;
5682 struct lra_insn_reg *reg;
5683 edge e;
5684 edge_iterator ei;
5686 lra_assert (to != NULL);
5687 bitmap_clear (res);
5688 FOR_EACH_EDGE (e, ei, from->succs)
5689 if (e->dest != to)
5690 bitmap_ior_into (res, df_get_live_in (e->dest));
5691 last = get_last_insertion_point (from);
5692 if (! JUMP_P (last))
5693 return;
5694 curr_id = lra_get_insn_recog_data (last);
5695 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5696 if (reg->type != OP_IN)
5697 bitmap_set_bit (res, reg->regno);
5700 /* Used as a temporary results of some bitmap calculations. */
5701 static bitmap_head temp_bitmap;
5703 /* We split for reloads of small class of hard regs. The following
5704 defines how many hard regs the class should have to be qualified as
5705 small. The code is mostly oriented to x86/x86-64 architecture
5706 where some insns need to use only specific register or pair of
5707 registers and these register can live in RTL explicitly, e.g. for
5708 parameter passing. */
5709 static const int max_small_class_regs_num = 2;
5711 /* Do inheritance/split transformations in EBB starting with HEAD and
5712 finishing on TAIL. We process EBB insns in the reverse order.
5713 Return true if we did any inheritance/split transformation in the
5714 EBB.
5716 We should avoid excessive splitting which results in worse code
5717 because of inaccurate cost calculations for spilling new split
5718 pseudos in such case. To achieve this we do splitting only if
5719 register pressure is high in given basic block and there are reload
5720 pseudos requiring hard registers. We could do more register
5721 pressure calculations at any given program point to avoid necessary
5722 splitting even more but it is to expensive and the current approach
5723 works well enough. */
5724 static bool
5725 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5727 int i, src_regno, dst_regno, nregs;
5728 bool change_p, succ_p, update_reloads_num_p;
5729 rtx_insn *prev_insn, *last_insn;
5730 rtx next_usage_insns, curr_set;
5731 enum reg_class cl;
5732 struct lra_insn_reg *reg;
5733 basic_block last_processed_bb, curr_bb = NULL;
5734 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5735 bitmap to_process;
5736 unsigned int j;
5737 bitmap_iterator bi;
5738 bool head_p, after_p;
5740 change_p = false;
5741 curr_usage_insns_check++;
5742 clear_invariants ();
5743 reloads_num = calls_num = 0;
5744 bitmap_clear (&check_only_regs);
5745 bitmap_clear (&invalid_invariant_regs);
5746 last_processed_bb = NULL;
5747 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5748 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5749 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5750 /* We don't process new insns generated in the loop. */
5751 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5753 prev_insn = PREV_INSN (curr_insn);
5754 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5755 curr_bb = BLOCK_FOR_INSN (curr_insn);
5756 if (last_processed_bb != curr_bb)
5758 /* We are at the end of BB. Add qualified living
5759 pseudos for potential splitting. */
5760 to_process = df_get_live_out (curr_bb);
5761 if (last_processed_bb != NULL)
5763 /* We are somewhere in the middle of EBB. */
5764 get_live_on_other_edges (curr_bb, last_processed_bb,
5765 &temp_bitmap);
5766 to_process = &temp_bitmap;
5768 last_processed_bb = curr_bb;
5769 last_insn = get_last_insertion_point (curr_bb);
5770 after_p = (! JUMP_P (last_insn)
5771 && (! CALL_P (last_insn)
5772 || (find_reg_note (last_insn,
5773 REG_NORETURN, NULL_RTX) == NULL_RTX
5774 && ! SIBLING_CALL_P (last_insn))));
5775 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5776 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5778 if ((int) j >= lra_constraint_new_regno_start)
5779 break;
5780 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5782 if (j < FIRST_PSEUDO_REGISTER)
5783 SET_HARD_REG_BIT (live_hard_regs, j);
5784 else
5785 add_to_hard_reg_set (&live_hard_regs,
5786 PSEUDO_REGNO_MODE (j),
5787 reg_renumber[j]);
5788 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5792 src_regno = dst_regno = -1;
5793 curr_set = single_set (curr_insn);
5794 if (curr_set != NULL_RTX && REG_P (SET_DEST (curr_set)))
5795 dst_regno = REGNO (SET_DEST (curr_set));
5796 if (curr_set != NULL_RTX && REG_P (SET_SRC (curr_set)))
5797 src_regno = REGNO (SET_SRC (curr_set));
5798 update_reloads_num_p = true;
5799 if (src_regno < lra_constraint_new_regno_start
5800 && src_regno >= FIRST_PSEUDO_REGISTER
5801 && reg_renumber[src_regno] < 0
5802 && dst_regno >= lra_constraint_new_regno_start
5803 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5805 /* 'reload_pseudo <- original_pseudo'. */
5806 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5807 reloads_num++;
5808 update_reloads_num_p = false;
5809 succ_p = false;
5810 if (usage_insns[src_regno].check == curr_usage_insns_check
5811 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5812 succ_p = inherit_reload_reg (false, src_regno, cl,
5813 curr_insn, next_usage_insns);
5814 if (succ_p)
5815 change_p = true;
5816 else
5817 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5818 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5819 IOR_HARD_REG_SET (potential_reload_hard_regs,
5820 reg_class_contents[cl]);
5822 else if (src_regno < 0
5823 && dst_regno >= lra_constraint_new_regno_start
5824 && invariant_p (SET_SRC (curr_set))
5825 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS
5826 && ! bitmap_bit_p (&invalid_invariant_regs, dst_regno))
5828 /* 'reload_pseudo <- invariant'. */
5829 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5830 reloads_num++;
5831 update_reloads_num_p = false;
5832 if (process_invariant_for_inheritance (SET_DEST (curr_set), SET_SRC (curr_set)))
5833 change_p = true;
5834 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5835 IOR_HARD_REG_SET (potential_reload_hard_regs,
5836 reg_class_contents[cl]);
5838 else if (src_regno >= lra_constraint_new_regno_start
5839 && dst_regno < lra_constraint_new_regno_start
5840 && dst_regno >= FIRST_PSEUDO_REGISTER
5841 && reg_renumber[dst_regno] < 0
5842 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5843 && usage_insns[dst_regno].check == curr_usage_insns_check
5844 && (next_usage_insns
5845 = usage_insns[dst_regno].insns) != NULL_RTX)
5847 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5848 reloads_num++;
5849 update_reloads_num_p = false;
5850 /* 'original_pseudo <- reload_pseudo'. */
5851 if (! JUMP_P (curr_insn)
5852 && inherit_reload_reg (true, dst_regno, cl,
5853 curr_insn, next_usage_insns))
5854 change_p = true;
5855 /* Invalidate. */
5856 usage_insns[dst_regno].check = 0;
5857 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5858 IOR_HARD_REG_SET (potential_reload_hard_regs,
5859 reg_class_contents[cl]);
5861 else if (INSN_P (curr_insn))
5863 int iter;
5864 int max_uid = get_max_uid ();
5866 curr_id = lra_get_insn_recog_data (curr_insn);
5867 curr_static_id = curr_id->insn_static_data;
5868 to_inherit_num = 0;
5869 /* Process insn definitions. */
5870 for (iter = 0; iter < 2; iter++)
5871 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5872 reg != NULL;
5873 reg = reg->next)
5874 if (reg->type != OP_IN
5875 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5877 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5878 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5879 && usage_insns[dst_regno].check == curr_usage_insns_check
5880 && (next_usage_insns
5881 = usage_insns[dst_regno].insns) != NULL_RTX)
5883 struct lra_insn_reg *r;
5885 for (r = curr_id->regs; r != NULL; r = r->next)
5886 if (r->type != OP_OUT && r->regno == dst_regno)
5887 break;
5888 /* Don't do inheritance if the pseudo is also
5889 used in the insn. */
5890 if (r == NULL)
5891 /* We can not do inheritance right now
5892 because the current insn reg info (chain
5893 regs) can change after that. */
5894 add_to_inherit (dst_regno, next_usage_insns);
5896 /* We can not process one reg twice here because of
5897 usage_insns invalidation. */
5898 if ((dst_regno < FIRST_PSEUDO_REGISTER
5899 || reg_renumber[dst_regno] >= 0)
5900 && ! reg->subreg_p && reg->type != OP_IN)
5902 HARD_REG_SET s;
5904 if (split_if_necessary (dst_regno, reg->biggest_mode,
5905 potential_reload_hard_regs,
5906 false, curr_insn, max_uid))
5907 change_p = true;
5908 CLEAR_HARD_REG_SET (s);
5909 if (dst_regno < FIRST_PSEUDO_REGISTER)
5910 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5911 else
5912 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5913 reg_renumber[dst_regno]);
5914 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5916 /* We should invalidate potential inheritance or
5917 splitting for the current insn usages to the next
5918 usage insns (see code below) as the output pseudo
5919 prevents this. */
5920 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5921 && reg_renumber[dst_regno] < 0)
5922 || (reg->type == OP_OUT && ! reg->subreg_p
5923 && (dst_regno < FIRST_PSEUDO_REGISTER
5924 || reg_renumber[dst_regno] >= 0)))
5926 /* Invalidate and mark definitions. */
5927 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5928 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5929 else
5931 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5932 for (i = 0; i < nregs; i++)
5933 usage_insns[dst_regno + i].check
5934 = -(int) INSN_UID (curr_insn);
5938 /* Process clobbered call regs. */
5939 if (curr_id->arg_hard_regs != NULL)
5940 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5941 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5942 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
5943 = -(int) INSN_UID (curr_insn);
5944 if (! JUMP_P (curr_insn))
5945 for (i = 0; i < to_inherit_num; i++)
5946 if (inherit_reload_reg (true, to_inherit[i].regno,
5947 ALL_REGS, curr_insn,
5948 to_inherit[i].insns))
5949 change_p = true;
5950 if (CALL_P (curr_insn))
5952 rtx cheap, pat, dest;
5953 rtx_insn *restore;
5954 int regno, hard_regno;
5956 calls_num++;
5957 if ((cheap = find_reg_note (curr_insn,
5958 REG_RETURNED, NULL_RTX)) != NULL_RTX
5959 && ((cheap = XEXP (cheap, 0)), true)
5960 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5961 && (hard_regno = reg_renumber[regno]) >= 0
5962 /* If there are pending saves/restores, the
5963 optimization is not worth. */
5964 && usage_insns[regno].calls_num == calls_num - 1
5965 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5967 /* Restore the pseudo from the call result as
5968 REG_RETURNED note says that the pseudo value is
5969 in the call result and the pseudo is an argument
5970 of the call. */
5971 pat = PATTERN (curr_insn);
5972 if (GET_CODE (pat) == PARALLEL)
5973 pat = XVECEXP (pat, 0, 0);
5974 dest = SET_DEST (pat);
5975 /* For multiple return values dest is PARALLEL.
5976 Currently we handle only single return value case. */
5977 if (REG_P (dest))
5979 start_sequence ();
5980 emit_move_insn (cheap, copy_rtx (dest));
5981 restore = get_insns ();
5982 end_sequence ();
5983 lra_process_new_insns (curr_insn, NULL, restore,
5984 "Inserting call parameter restore");
5985 /* We don't need to save/restore of the pseudo from
5986 this call. */
5987 usage_insns[regno].calls_num = calls_num;
5988 bitmap_set_bit (&check_only_regs, regno);
5992 to_inherit_num = 0;
5993 /* Process insn usages. */
5994 for (iter = 0; iter < 2; iter++)
5995 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5996 reg != NULL;
5997 reg = reg->next)
5998 if ((reg->type != OP_OUT
5999 || (reg->type == OP_OUT && reg->subreg_p))
6000 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
6002 if (src_regno >= FIRST_PSEUDO_REGISTER
6003 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
6005 if (usage_insns[src_regno].check == curr_usage_insns_check
6006 && (next_usage_insns
6007 = usage_insns[src_regno].insns) != NULL_RTX
6008 && NONDEBUG_INSN_P (curr_insn))
6009 add_to_inherit (src_regno, next_usage_insns);
6010 else if (usage_insns[src_regno].check
6011 != -(int) INSN_UID (curr_insn))
6012 /* Add usages but only if the reg is not set up
6013 in the same insn. */
6014 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6016 else if (src_regno < FIRST_PSEUDO_REGISTER
6017 || reg_renumber[src_regno] >= 0)
6019 bool before_p;
6020 rtx_insn *use_insn = curr_insn;
6022 before_p = (JUMP_P (curr_insn)
6023 || (CALL_P (curr_insn) && reg->type == OP_IN));
6024 if (NONDEBUG_INSN_P (curr_insn)
6025 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
6026 && split_if_necessary (src_regno, reg->biggest_mode,
6027 potential_reload_hard_regs,
6028 before_p, curr_insn, max_uid))
6030 if (reg->subreg_p)
6031 lra_risky_transformations_p = true;
6032 change_p = true;
6033 /* Invalidate. */
6034 usage_insns[src_regno].check = 0;
6035 if (before_p)
6036 use_insn = PREV_INSN (curr_insn);
6038 if (NONDEBUG_INSN_P (curr_insn))
6040 if (src_regno < FIRST_PSEUDO_REGISTER)
6041 add_to_hard_reg_set (&live_hard_regs,
6042 reg->biggest_mode, src_regno);
6043 else
6044 add_to_hard_reg_set (&live_hard_regs,
6045 PSEUDO_REGNO_MODE (src_regno),
6046 reg_renumber[src_regno]);
6048 add_next_usage_insn (src_regno, use_insn, reloads_num);
6051 /* Process used call regs. */
6052 if (curr_id->arg_hard_regs != NULL)
6053 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6054 if (src_regno < FIRST_PSEUDO_REGISTER)
6056 SET_HARD_REG_BIT (live_hard_regs, src_regno);
6057 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6059 for (i = 0; i < to_inherit_num; i++)
6061 src_regno = to_inherit[i].regno;
6062 if (inherit_reload_reg (false, src_regno, ALL_REGS,
6063 curr_insn, to_inherit[i].insns))
6064 change_p = true;
6065 else
6066 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6069 if (update_reloads_num_p
6070 && NONDEBUG_INSN_P (curr_insn) && curr_set != NULL_RTX)
6072 int regno = -1;
6073 if ((REG_P (SET_DEST (curr_set))
6074 && (regno = REGNO (SET_DEST (curr_set))) >= lra_constraint_new_regno_start
6075 && reg_renumber[regno] < 0
6076 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
6077 || (REG_P (SET_SRC (curr_set))
6078 && (regno = REGNO (SET_SRC (curr_set))) >= lra_constraint_new_regno_start
6079 && reg_renumber[regno] < 0
6080 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
6082 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6083 reloads_num++;
6084 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6085 IOR_HARD_REG_SET (potential_reload_hard_regs,
6086 reg_class_contents[cl]);
6089 if (NONDEBUG_INSN_P (curr_insn))
6091 int regno;
6093 /* Invalidate invariants with changed regs. */
6094 curr_id = lra_get_insn_recog_data (curr_insn);
6095 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6096 if (reg->type != OP_IN)
6097 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6098 curr_static_id = curr_id->insn_static_data;
6099 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6100 if (reg->type != OP_IN)
6101 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6102 if (curr_id->arg_hard_regs != NULL)
6103 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6104 bitmap_set_bit (&invalid_invariant_regs,
6105 regno >= FIRST_PSEUDO_REGISTER
6106 ? regno : regno - FIRST_PSEUDO_REGISTER);
6108 /* We reached the start of the current basic block. */
6109 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
6110 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
6112 /* We reached the beginning of the current block -- do
6113 rest of spliting in the current BB. */
6114 to_process = df_get_live_in (curr_bb);
6115 if (BLOCK_FOR_INSN (head) != curr_bb)
6117 /* We are somewhere in the middle of EBB. */
6118 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
6119 curr_bb, &temp_bitmap);
6120 to_process = &temp_bitmap;
6122 head_p = true;
6123 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
6125 if ((int) j >= lra_constraint_new_regno_start)
6126 break;
6127 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
6128 && usage_insns[j].check == curr_usage_insns_check
6129 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
6131 if (need_for_split_p (potential_reload_hard_regs, j))
6133 if (lra_dump_file != NULL && head_p)
6135 fprintf (lra_dump_file,
6136 " ----------------------------------\n");
6137 head_p = false;
6139 if (split_reg (false, j, bb_note (curr_bb),
6140 next_usage_insns))
6141 change_p = true;
6143 usage_insns[j].check = 0;
6148 return change_p;
6151 /* This value affects EBB forming. If probability of edge from EBB to
6152 a BB is not greater than the following value, we don't add the BB
6153 to EBB. */
6154 #define EBB_PROBABILITY_CUTOFF \
6155 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
6157 /* Current number of inheritance/split iteration. */
6158 int lra_inheritance_iter;
6160 /* Entry function for inheritance/split pass. */
6161 void
6162 lra_inheritance (void)
6164 int i;
6165 basic_block bb, start_bb;
6166 edge e;
6168 lra_inheritance_iter++;
6169 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6170 return;
6171 timevar_push (TV_LRA_INHERITANCE);
6172 if (lra_dump_file != NULL)
6173 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
6174 lra_inheritance_iter);
6175 curr_usage_insns_check = 0;
6176 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
6177 for (i = 0; i < lra_constraint_new_regno_start; i++)
6178 usage_insns[i].check = 0;
6179 bitmap_initialize (&check_only_regs, &reg_obstack);
6180 bitmap_initialize (&invalid_invariant_regs, &reg_obstack);
6181 bitmap_initialize (&live_regs, &reg_obstack);
6182 bitmap_initialize (&temp_bitmap, &reg_obstack);
6183 bitmap_initialize (&ebb_global_regs, &reg_obstack);
6184 FOR_EACH_BB_FN (bb, cfun)
6186 start_bb = bb;
6187 if (lra_dump_file != NULL)
6188 fprintf (lra_dump_file, "EBB");
6189 /* Form a EBB starting with BB. */
6190 bitmap_clear (&ebb_global_regs);
6191 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
6192 for (;;)
6194 if (lra_dump_file != NULL)
6195 fprintf (lra_dump_file, " %d", bb->index);
6196 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
6197 || LABEL_P (BB_HEAD (bb->next_bb)))
6198 break;
6199 e = find_fallthru_edge (bb->succs);
6200 if (! e)
6201 break;
6202 if (e->probability < EBB_PROBABILITY_CUTOFF)
6203 break;
6204 bb = bb->next_bb;
6206 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
6207 if (lra_dump_file != NULL)
6208 fprintf (lra_dump_file, "\n");
6209 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
6210 /* Remember that the EBB head and tail can change in
6211 inherit_in_ebb. */
6212 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
6214 bitmap_clear (&ebb_global_regs);
6215 bitmap_clear (&temp_bitmap);
6216 bitmap_clear (&live_regs);
6217 bitmap_clear (&invalid_invariant_regs);
6218 bitmap_clear (&check_only_regs);
6219 free (usage_insns);
6221 timevar_pop (TV_LRA_INHERITANCE);
6226 /* This page contains code to undo failed inheritance/split
6227 transformations. */
6229 /* Current number of iteration undoing inheritance/split. */
6230 int lra_undo_inheritance_iter;
6232 /* Fix BB live info LIVE after removing pseudos created on pass doing
6233 inheritance/split which are REMOVED_PSEUDOS. */
6234 static void
6235 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
6237 unsigned int regno;
6238 bitmap_iterator bi;
6240 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
6241 if (bitmap_clear_bit (live, regno)
6242 && REG_P (lra_reg_info[regno].restore_rtx))
6243 bitmap_set_bit (live, REGNO (lra_reg_info[regno].restore_rtx));
6246 /* Return regno of the (subreg of) REG. Otherwise, return a negative
6247 number. */
6248 static int
6249 get_regno (rtx reg)
6251 if (GET_CODE (reg) == SUBREG)
6252 reg = SUBREG_REG (reg);
6253 if (REG_P (reg))
6254 return REGNO (reg);
6255 return -1;
6258 /* Delete a move INSN with destination reg DREGNO and a previous
6259 clobber insn with the same regno. The inheritance/split code can
6260 generate moves with preceding clobber and when we delete such moves
6261 we should delete the clobber insn too to keep the correct life
6262 info. */
6263 static void
6264 delete_move_and_clobber (rtx_insn *insn, int dregno)
6266 rtx_insn *prev_insn = PREV_INSN (insn);
6268 lra_set_insn_deleted (insn);
6269 lra_assert (dregno >= 0);
6270 if (prev_insn != NULL && NONDEBUG_INSN_P (prev_insn)
6271 && GET_CODE (PATTERN (prev_insn)) == CLOBBER
6272 && dregno == get_regno (XEXP (PATTERN (prev_insn), 0)))
6273 lra_set_insn_deleted (prev_insn);
6276 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
6277 return true if we did any change. The undo transformations for
6278 inheritance looks like
6279 i <- i2
6280 p <- i => p <- i2
6281 or removing
6282 p <- i, i <- p, and i <- i3
6283 where p is original pseudo from which inheritance pseudo i was
6284 created, i and i3 are removed inheritance pseudos, i2 is another
6285 not removed inheritance pseudo. All split pseudos or other
6286 occurrences of removed inheritance pseudos are changed on the
6287 corresponding original pseudos.
6289 The function also schedules insns changed and created during
6290 inheritance/split pass for processing by the subsequent constraint
6291 pass. */
6292 static bool
6293 remove_inheritance_pseudos (bitmap remove_pseudos)
6295 basic_block bb;
6296 int regno, sregno, prev_sregno, dregno;
6297 rtx restore_rtx;
6298 rtx set, prev_set;
6299 rtx_insn *prev_insn;
6300 bool change_p, done_p;
6302 change_p = ! bitmap_empty_p (remove_pseudos);
6303 /* We can not finish the function right away if CHANGE_P is true
6304 because we need to marks insns affected by previous
6305 inheritance/split pass for processing by the subsequent
6306 constraint pass. */
6307 FOR_EACH_BB_FN (bb, cfun)
6309 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
6310 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
6311 FOR_BB_INSNS_REVERSE (bb, curr_insn)
6313 if (! INSN_P (curr_insn))
6314 continue;
6315 done_p = false;
6316 sregno = dregno = -1;
6317 if (change_p && NONDEBUG_INSN_P (curr_insn)
6318 && (set = single_set (curr_insn)) != NULL_RTX)
6320 dregno = get_regno (SET_DEST (set));
6321 sregno = get_regno (SET_SRC (set));
6324 if (sregno >= 0 && dregno >= 0)
6326 if (bitmap_bit_p (remove_pseudos, dregno)
6327 && ! REG_P (lra_reg_info[dregno].restore_rtx))
6329 /* invariant inheritance pseudo <- original pseudo */
6330 if (lra_dump_file != NULL)
6332 fprintf (lra_dump_file, " Removing invariant inheritance:\n");
6333 dump_insn_slim (lra_dump_file, curr_insn);
6334 fprintf (lra_dump_file, "\n");
6336 delete_move_and_clobber (curr_insn, dregno);
6337 done_p = true;
6339 else if (bitmap_bit_p (remove_pseudos, sregno)
6340 && ! REG_P (lra_reg_info[sregno].restore_rtx))
6342 /* reload pseudo <- invariant inheritance pseudo */
6343 start_sequence ();
6344 /* We can not just change the source. It might be
6345 an insn different from the move. */
6346 emit_insn (lra_reg_info[sregno].restore_rtx);
6347 rtx_insn *new_insns = get_insns ();
6348 end_sequence ();
6349 lra_assert (single_set (new_insns) != NULL
6350 && SET_DEST (set) == SET_DEST (single_set (new_insns)));
6351 lra_process_new_insns (curr_insn, NULL, new_insns,
6352 "Changing reload<-invariant inheritance");
6353 delete_move_and_clobber (curr_insn, dregno);
6354 done_p = true;
6356 else if ((bitmap_bit_p (remove_pseudos, sregno)
6357 && (get_regno (lra_reg_info[sregno].restore_rtx) == dregno
6358 || (bitmap_bit_p (remove_pseudos, dregno)
6359 && get_regno (lra_reg_info[sregno].restore_rtx) >= 0
6360 && (get_regno (lra_reg_info[sregno].restore_rtx)
6361 == get_regno (lra_reg_info[dregno].restore_rtx)))))
6362 || (bitmap_bit_p (remove_pseudos, dregno)
6363 && get_regno (lra_reg_info[dregno].restore_rtx) == sregno))
6364 /* One of the following cases:
6365 original <- removed inheritance pseudo
6366 removed inherit pseudo <- another removed inherit pseudo
6367 removed inherit pseudo <- original pseudo
6369 removed_split_pseudo <- original_reg
6370 original_reg <- removed_split_pseudo */
6372 if (lra_dump_file != NULL)
6374 fprintf (lra_dump_file, " Removing %s:\n",
6375 bitmap_bit_p (&lra_split_regs, sregno)
6376 || bitmap_bit_p (&lra_split_regs, dregno)
6377 ? "split" : "inheritance");
6378 dump_insn_slim (lra_dump_file, curr_insn);
6380 delete_move_and_clobber (curr_insn, dregno);
6381 done_p = true;
6383 else if (bitmap_bit_p (remove_pseudos, sregno)
6384 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
6386 /* Search the following pattern:
6387 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
6388 original_pseudo <- inherit_or_split_pseudo1
6389 where the 2nd insn is the current insn and
6390 inherit_or_split_pseudo2 is not removed. If it is found,
6391 change the current insn onto:
6392 original_pseudo <- inherit_or_split_pseudo2. */
6393 for (prev_insn = PREV_INSN (curr_insn);
6394 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
6395 prev_insn = PREV_INSN (prev_insn))
6397 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
6398 && (prev_set = single_set (prev_insn)) != NULL_RTX
6399 /* There should be no subregs in insn we are
6400 searching because only the original reg might
6401 be in subreg when we changed the mode of
6402 load/store for splitting. */
6403 && REG_P (SET_DEST (prev_set))
6404 && REG_P (SET_SRC (prev_set))
6405 && (int) REGNO (SET_DEST (prev_set)) == sregno
6406 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
6407 >= FIRST_PSEUDO_REGISTER)
6408 && (lra_reg_info[prev_sregno].restore_rtx == NULL_RTX
6410 /* As we consider chain of inheritance or
6411 splitting described in above comment we should
6412 check that sregno and prev_sregno were
6413 inheritance/split pseudos created from the
6414 same original regno. */
6415 (get_regno (lra_reg_info[sregno].restore_rtx) >= 0
6416 && (get_regno (lra_reg_info[sregno].restore_rtx)
6417 == get_regno (lra_reg_info[prev_sregno].restore_rtx))))
6418 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
6420 lra_assert (GET_MODE (SET_SRC (prev_set))
6421 == GET_MODE (regno_reg_rtx[sregno]));
6422 if (GET_CODE (SET_SRC (set)) == SUBREG)
6423 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
6424 else
6425 SET_SRC (set) = SET_SRC (prev_set);
6426 /* As we are finishing with processing the insn
6427 here, check the destination too as it might
6428 inheritance pseudo for another pseudo. */
6429 if (bitmap_bit_p (remove_pseudos, dregno)
6430 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
6431 && (restore_rtx
6432 = lra_reg_info[dregno].restore_rtx) != NULL_RTX)
6434 if (GET_CODE (SET_DEST (set)) == SUBREG)
6435 SUBREG_REG (SET_DEST (set)) = restore_rtx;
6436 else
6437 SET_DEST (set) = restore_rtx;
6439 lra_push_insn_and_update_insn_regno_info (curr_insn);
6440 lra_set_used_insn_alternative_by_uid
6441 (INSN_UID (curr_insn), -1);
6442 done_p = true;
6443 if (lra_dump_file != NULL)
6445 fprintf (lra_dump_file, " Change reload insn:\n");
6446 dump_insn_slim (lra_dump_file, curr_insn);
6451 if (! done_p)
6453 struct lra_insn_reg *reg;
6454 bool restored_regs_p = false;
6455 bool kept_regs_p = false;
6457 curr_id = lra_get_insn_recog_data (curr_insn);
6458 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6460 regno = reg->regno;
6461 restore_rtx = lra_reg_info[regno].restore_rtx;
6462 if (restore_rtx != NULL_RTX)
6464 if (change_p && bitmap_bit_p (remove_pseudos, regno))
6466 lra_substitute_pseudo_within_insn
6467 (curr_insn, regno, restore_rtx, false);
6468 restored_regs_p = true;
6470 else
6471 kept_regs_p = true;
6474 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
6476 /* The instruction has changed since the previous
6477 constraints pass. */
6478 lra_push_insn_and_update_insn_regno_info (curr_insn);
6479 lra_set_used_insn_alternative_by_uid
6480 (INSN_UID (curr_insn), -1);
6482 else if (restored_regs_p)
6483 /* The instruction has been restored to the form that
6484 it had during the previous constraints pass. */
6485 lra_update_insn_regno_info (curr_insn);
6486 if (restored_regs_p && lra_dump_file != NULL)
6488 fprintf (lra_dump_file, " Insn after restoring regs:\n");
6489 dump_insn_slim (lra_dump_file, curr_insn);
6494 return change_p;
6497 /* If optional reload pseudos failed to get a hard register or was not
6498 inherited, it is better to remove optional reloads. We do this
6499 transformation after undoing inheritance to figure out necessity to
6500 remove optional reloads easier. Return true if we do any
6501 change. */
6502 static bool
6503 undo_optional_reloads (void)
6505 bool change_p, keep_p;
6506 unsigned int regno, uid;
6507 bitmap_iterator bi, bi2;
6508 rtx_insn *insn;
6509 rtx set, src, dest;
6510 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
6512 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
6513 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
6514 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6516 keep_p = false;
6517 /* Keep optional reloads from previous subpasses. */
6518 if (lra_reg_info[regno].restore_rtx == NULL_RTX
6519 /* If the original pseudo changed its allocation, just
6520 removing the optional pseudo is dangerous as the original
6521 pseudo will have longer live range. */
6522 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] >= 0)
6523 keep_p = true;
6524 else if (reg_renumber[regno] >= 0)
6525 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
6527 insn = lra_insn_recog_data[uid]->insn;
6528 if ((set = single_set (insn)) == NULL_RTX)
6529 continue;
6530 src = SET_SRC (set);
6531 dest = SET_DEST (set);
6532 if (! REG_P (src) || ! REG_P (dest))
6533 continue;
6534 if (REGNO (dest) == regno
6535 /* Ignore insn for optional reloads itself. */
6536 && REGNO (lra_reg_info[regno].restore_rtx) != REGNO (src)
6537 /* Check only inheritance on last inheritance pass. */
6538 && (int) REGNO (src) >= new_regno_start
6539 /* Check that the optional reload was inherited. */
6540 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6542 keep_p = true;
6543 break;
6546 if (keep_p)
6548 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6549 if (lra_dump_file != NULL)
6550 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6553 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6554 bitmap_initialize (&insn_bitmap, &reg_obstack);
6555 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6557 if (lra_dump_file != NULL)
6558 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6559 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6560 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6562 insn = lra_insn_recog_data[uid]->insn;
6563 if ((set = single_set (insn)) != NULL_RTX)
6565 src = SET_SRC (set);
6566 dest = SET_DEST (set);
6567 if (REG_P (src) && REG_P (dest)
6568 && ((REGNO (src) == regno
6569 && (REGNO (lra_reg_info[regno].restore_rtx)
6570 == REGNO (dest)))
6571 || (REGNO (dest) == regno
6572 && (REGNO (lra_reg_info[regno].restore_rtx)
6573 == REGNO (src)))))
6575 if (lra_dump_file != NULL)
6577 fprintf (lra_dump_file, " Deleting move %u\n",
6578 INSN_UID (insn));
6579 dump_insn_slim (lra_dump_file, insn);
6581 delete_move_and_clobber (insn, REGNO (dest));
6582 continue;
6584 /* We should not worry about generation memory-memory
6585 moves here as if the corresponding inheritance did
6586 not work (inheritance pseudo did not get a hard reg),
6587 we remove the inheritance pseudo and the optional
6588 reload. */
6590 lra_substitute_pseudo_within_insn
6591 (insn, regno, lra_reg_info[regno].restore_rtx, false);
6592 lra_update_insn_regno_info (insn);
6593 if (lra_dump_file != NULL)
6595 fprintf (lra_dump_file,
6596 " Restoring original insn:\n");
6597 dump_insn_slim (lra_dump_file, insn);
6601 /* Clear restore_regnos. */
6602 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6603 lra_reg_info[regno].restore_rtx = NULL_RTX;
6604 bitmap_clear (&insn_bitmap);
6605 bitmap_clear (&removed_optional_reload_pseudos);
6606 return change_p;
6609 /* Entry function for undoing inheritance/split transformation. Return true
6610 if we did any RTL change in this pass. */
6611 bool
6612 lra_undo_inheritance (void)
6614 unsigned int regno;
6615 int hard_regno;
6616 int n_all_inherit, n_inherit, n_all_split, n_split;
6617 rtx restore_rtx;
6618 bitmap_head remove_pseudos;
6619 bitmap_iterator bi;
6620 bool change_p;
6622 lra_undo_inheritance_iter++;
6623 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6624 return false;
6625 if (lra_dump_file != NULL)
6626 fprintf (lra_dump_file,
6627 "\n********** Undoing inheritance #%d: **********\n\n",
6628 lra_undo_inheritance_iter);
6629 bitmap_initialize (&remove_pseudos, &reg_obstack);
6630 n_inherit = n_all_inherit = 0;
6631 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6632 if (lra_reg_info[regno].restore_rtx != NULL_RTX)
6634 n_all_inherit++;
6635 if (reg_renumber[regno] < 0
6636 /* If the original pseudo changed its allocation, just
6637 removing inheritance is dangerous as for changing
6638 allocation we used shorter live-ranges. */
6639 && (! REG_P (lra_reg_info[regno].restore_rtx)
6640 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] < 0))
6641 bitmap_set_bit (&remove_pseudos, regno);
6642 else
6643 n_inherit++;
6645 if (lra_dump_file != NULL && n_all_inherit != 0)
6646 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6647 n_inherit, n_all_inherit,
6648 (double) n_inherit / n_all_inherit * 100);
6649 n_split = n_all_split = 0;
6650 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6651 if ((restore_rtx = lra_reg_info[regno].restore_rtx) != NULL_RTX)
6653 int restore_regno = REGNO (restore_rtx);
6655 n_all_split++;
6656 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6657 ? reg_renumber[restore_regno] : restore_regno);
6658 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6659 bitmap_set_bit (&remove_pseudos, regno);
6660 else
6662 n_split++;
6663 if (lra_dump_file != NULL)
6664 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6665 regno, restore_regno);
6668 if (lra_dump_file != NULL && n_all_split != 0)
6669 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6670 n_split, n_all_split,
6671 (double) n_split / n_all_split * 100);
6672 change_p = remove_inheritance_pseudos (&remove_pseudos);
6673 bitmap_clear (&remove_pseudos);
6674 /* Clear restore_regnos. */
6675 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6676 lra_reg_info[regno].restore_rtx = NULL_RTX;
6677 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6678 lra_reg_info[regno].restore_rtx = NULL_RTX;
6679 change_p = undo_optional_reloads () || change_p;
6680 return change_p;