* g++.dg/debug/pr71432.C: Fail on AIX.
[official-gcc.git] / gcc / lra-constraints.c
blobe9d3e43eacef4432dd4c3bbfedf180a10222f4f4
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2016 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains code for 3 passes: constraint pass,
23 inheritance/split pass, and pass for undoing failed inheritance and
24 split.
26 The major goal of constraint pass is to transform RTL to satisfy
27 insn and address constraints by:
28 o choosing insn alternatives;
29 o generating *reload insns* (or reloads in brief) and *reload
30 pseudos* which will get necessary hard registers later;
31 o substituting pseudos with equivalent values and removing the
32 instructions that initialized those pseudos.
34 The constraint pass has biggest and most complicated code in LRA.
35 There are a lot of important details like:
36 o reuse of input reload pseudos to simplify reload pseudo
37 allocations;
38 o some heuristics to choose insn alternative to improve the
39 inheritance;
40 o early clobbers etc.
42 The pass is mimicking former reload pass in alternative choosing
43 because the reload pass is oriented to current machine description
44 model. It might be changed if the machine description model is
45 changed.
47 There is special code for preventing all LRA and this pass cycling
48 in case of bugs.
50 On the first iteration of the pass we process every instruction and
51 choose an alternative for each one. On subsequent iterations we try
52 to avoid reprocessing instructions if we can be sure that the old
53 choice is still valid.
55 The inheritance/spilt pass is to transform code to achieve
56 ineheritance and live range splitting. It is done on backward
57 traversal of EBBs.
59 The inheritance optimization goal is to reuse values in hard
60 registers. There is analogous optimization in old reload pass. The
61 inheritance is achieved by following transformation:
63 reload_p1 <- p reload_p1 <- p
64 ... new_p <- reload_p1
65 ... => ...
66 reload_p2 <- p reload_p2 <- new_p
68 where p is spilled and not changed between the insns. Reload_p1 is
69 also called *original pseudo* and new_p is called *inheritance
70 pseudo*.
72 The subsequent assignment pass will try to assign the same (or
73 another if it is not possible) hard register to new_p as to
74 reload_p1 or reload_p2.
76 If the assignment pass fails to assign a hard register to new_p,
77 this file will undo the inheritance and restore the original code.
78 This is because implementing the above sequence with a spilled
79 new_p would make the code much worse. The inheritance is done in
80 EBB scope. The above is just a simplified example to get an idea
81 of the inheritance as the inheritance is also done for non-reload
82 insns.
84 Splitting (transformation) is also done in EBB scope on the same
85 pass as the inheritance:
87 r <- ... or ... <- r r <- ... or ... <- r
88 ... s <- r (new insn -- save)
89 ... =>
90 ... r <- s (new insn -- restore)
91 ... <- r ... <- r
93 The *split pseudo* s is assigned to the hard register of the
94 original pseudo or hard register r.
96 Splitting is done:
97 o In EBBs with high register pressure for global pseudos (living
98 in at least 2 BBs) and assigned to hard registers when there
99 are more one reloads needing the hard registers;
100 o for pseudos needing save/restore code around calls.
102 If the split pseudo still has the same hard register as the
103 original pseudo after the subsequent assignment pass or the
104 original pseudo was split, the opposite transformation is done on
105 the same pass for undoing inheritance. */
107 #undef REG_OK_STRICT
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "backend.h"
113 #include "target.h"
114 #include "rtl.h"
115 #include "tree.h"
116 #include "predict.h"
117 #include "df.h"
118 #include "tm_p.h"
119 #include "expmed.h"
120 #include "optabs.h"
121 #include "regs.h"
122 #include "ira.h"
123 #include "recog.h"
124 #include "output.h"
125 #include "addresses.h"
126 #include "expr.h"
127 #include "cfgrtl.h"
128 #include "rtl-error.h"
129 #include "params.h"
130 #include "lra.h"
131 #include "lra-int.h"
132 #include "print-rtl.h"
134 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
135 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
136 reload insns. */
137 static int bb_reload_num;
139 /* The current insn being processed and corresponding its single set
140 (NULL otherwise), its data (basic block, the insn data, the insn
141 static data, and the mode of each operand). */
142 static rtx_insn *curr_insn;
143 static rtx curr_insn_set;
144 static basic_block curr_bb;
145 static lra_insn_recog_data_t curr_id;
146 static struct lra_static_insn_data *curr_static_id;
147 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
148 /* Mode of the register substituted by its equivalence with VOIDmode
149 (e.g. constant) and whose subreg is given operand of the current
150 insn. VOIDmode in all other cases. */
151 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
155 /* Start numbers for new registers and insns at the current constraints
156 pass start. */
157 static int new_regno_start;
158 static int new_insn_uid_start;
160 /* If LOC is nonnull, strip any outer subreg from it. */
161 static inline rtx *
162 strip_subreg (rtx *loc)
164 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
167 /* Return hard regno of REGNO or if it is was not assigned to a hard
168 register, use a hard register from its allocno class. */
169 static int
170 get_try_hard_regno (int regno)
172 int hard_regno;
173 enum reg_class rclass;
175 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
176 hard_regno = lra_get_regno_hard_regno (regno);
177 if (hard_regno >= 0)
178 return hard_regno;
179 rclass = lra_get_allocno_class (regno);
180 if (rclass == NO_REGS)
181 return -1;
182 return ira_class_hard_regs[rclass][0];
185 /* Return final hard regno (plus offset) which will be after
186 elimination. We do this for matching constraints because the final
187 hard regno could have a different class. */
188 static int
189 get_final_hard_regno (int hard_regno, int offset)
191 if (hard_regno < 0)
192 return hard_regno;
193 hard_regno = lra_get_elimination_hard_regno (hard_regno);
194 return hard_regno + offset;
197 /* Return hard regno of X after removing subreg and making
198 elimination. If X is not a register or subreg of register, return
199 -1. For pseudo use its assignment. */
200 static int
201 get_hard_regno (rtx x)
203 rtx reg;
204 int offset, hard_regno;
206 reg = x;
207 if (GET_CODE (x) == SUBREG)
208 reg = SUBREG_REG (x);
209 if (! REG_P (reg))
210 return -1;
211 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
212 hard_regno = lra_get_regno_hard_regno (hard_regno);
213 if (hard_regno < 0)
214 return -1;
215 offset = 0;
216 if (GET_CODE (x) == SUBREG)
217 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
218 SUBREG_BYTE (x), GET_MODE (x));
219 return get_final_hard_regno (hard_regno, offset);
222 /* If REGNO is a hard register or has been allocated a hard register,
223 return the class of that register. If REGNO is a reload pseudo
224 created by the current constraints pass, return its allocno class.
225 Return NO_REGS otherwise. */
226 static enum reg_class
227 get_reg_class (int regno)
229 int hard_regno;
231 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
232 hard_regno = lra_get_regno_hard_regno (regno);
233 if (hard_regno >= 0)
235 hard_regno = get_final_hard_regno (hard_regno, 0);
236 return REGNO_REG_CLASS (hard_regno);
238 if (regno >= new_regno_start)
239 return lra_get_allocno_class (regno);
240 return NO_REGS;
243 /* Return true if REG satisfies (or will satisfy) reg class constraint
244 CL. Use elimination first if REG is a hard register. If REG is a
245 reload pseudo created by this constraints pass, assume that it will
246 be allocated a hard register from its allocno class, but allow that
247 class to be narrowed to CL if it is currently a superset of CL.
249 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
250 REGNO (reg), or NO_REGS if no change in its class was needed. */
251 static bool
252 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
254 enum reg_class rclass, common_class;
255 machine_mode reg_mode;
256 int class_size, hard_regno, nregs, i, j;
257 int regno = REGNO (reg);
259 if (new_class != NULL)
260 *new_class = NO_REGS;
261 if (regno < FIRST_PSEUDO_REGISTER)
263 rtx final_reg = reg;
264 rtx *final_loc = &final_reg;
266 lra_eliminate_reg_if_possible (final_loc);
267 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
269 reg_mode = GET_MODE (reg);
270 rclass = get_reg_class (regno);
271 if (regno < new_regno_start
272 /* Do not allow the constraints for reload instructions to
273 influence the classes of new pseudos. These reloads are
274 typically moves that have many alternatives, and restricting
275 reload pseudos for one alternative may lead to situations
276 where other reload pseudos are no longer allocatable. */
277 || (INSN_UID (curr_insn) >= new_insn_uid_start
278 && curr_insn_set != NULL
279 && ((OBJECT_P (SET_SRC (curr_insn_set))
280 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
281 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
282 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
283 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
284 /* When we don't know what class will be used finally for reload
285 pseudos, we use ALL_REGS. */
286 return ((regno >= new_regno_start && rclass == ALL_REGS)
287 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
288 && ! hard_reg_set_subset_p (reg_class_contents[cl],
289 lra_no_alloc_regs)));
290 else
292 common_class = ira_reg_class_subset[rclass][cl];
293 if (new_class != NULL)
294 *new_class = common_class;
295 if (hard_reg_set_subset_p (reg_class_contents[common_class],
296 lra_no_alloc_regs))
297 return false;
298 /* Check that there are enough allocatable regs. */
299 class_size = ira_class_hard_regs_num[common_class];
300 for (i = 0; i < class_size; i++)
302 hard_regno = ira_class_hard_regs[common_class][i];
303 nregs = hard_regno_nregs[hard_regno][reg_mode];
304 if (nregs == 1)
305 return true;
306 for (j = 0; j < nregs; j++)
307 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
308 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
309 hard_regno + j))
310 break;
311 if (j >= nregs)
312 return true;
314 return false;
318 /* Return true if REGNO satisfies a memory constraint. */
319 static bool
320 in_mem_p (int regno)
322 return get_reg_class (regno) == NO_REGS;
325 /* Return 1 if ADDR is a valid memory address for mode MODE in address
326 space AS, and check that each pseudo has the proper kind of hard
327 reg. */
328 static int
329 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
330 rtx addr, addr_space_t as)
332 #ifdef GO_IF_LEGITIMATE_ADDRESS
333 lra_assert (ADDR_SPACE_GENERIC_P (as));
334 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
335 return 0;
337 win:
338 return 1;
339 #else
340 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
341 #endif
344 namespace {
345 /* Temporarily eliminates registers in an address (for the lifetime of
346 the object). */
347 class address_eliminator {
348 public:
349 address_eliminator (struct address_info *ad);
350 ~address_eliminator ();
352 private:
353 struct address_info *m_ad;
354 rtx *m_base_loc;
355 rtx m_base_reg;
356 rtx *m_index_loc;
357 rtx m_index_reg;
361 address_eliminator::address_eliminator (struct address_info *ad)
362 : m_ad (ad),
363 m_base_loc (strip_subreg (ad->base_term)),
364 m_base_reg (NULL_RTX),
365 m_index_loc (strip_subreg (ad->index_term)),
366 m_index_reg (NULL_RTX)
368 if (m_base_loc != NULL)
370 m_base_reg = *m_base_loc;
371 lra_eliminate_reg_if_possible (m_base_loc);
372 if (m_ad->base_term2 != NULL)
373 *m_ad->base_term2 = *m_ad->base_term;
375 if (m_index_loc != NULL)
377 m_index_reg = *m_index_loc;
378 lra_eliminate_reg_if_possible (m_index_loc);
382 address_eliminator::~address_eliminator ()
384 if (m_base_loc && *m_base_loc != m_base_reg)
386 *m_base_loc = m_base_reg;
387 if (m_ad->base_term2 != NULL)
388 *m_ad->base_term2 = *m_ad->base_term;
390 if (m_index_loc && *m_index_loc != m_index_reg)
391 *m_index_loc = m_index_reg;
394 /* Return true if the eliminated form of AD is a legitimate target address. */
395 static bool
396 valid_address_p (struct address_info *ad)
398 address_eliminator eliminator (ad);
399 return valid_address_p (ad->mode, *ad->outer, ad->as);
402 /* Return true if the eliminated form of memory reference OP satisfies
403 extra (special) memory constraint CONSTRAINT. */
404 static bool
405 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
407 struct address_info ad;
409 decompose_mem_address (&ad, op);
410 address_eliminator eliminator (&ad);
411 return constraint_satisfied_p (op, constraint);
414 /* Return true if the eliminated form of address AD satisfies extra
415 address constraint CONSTRAINT. */
416 static bool
417 satisfies_address_constraint_p (struct address_info *ad,
418 enum constraint_num constraint)
420 address_eliminator eliminator (ad);
421 return constraint_satisfied_p (*ad->outer, constraint);
424 /* Return true if the eliminated form of address OP satisfies extra
425 address constraint CONSTRAINT. */
426 static bool
427 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
429 struct address_info ad;
431 decompose_lea_address (&ad, &op);
432 return satisfies_address_constraint_p (&ad, constraint);
435 /* Initiate equivalences for LRA. As we keep original equivalences
436 before any elimination, we need to make copies otherwise any change
437 in insns might change the equivalences. */
438 void
439 lra_init_equiv (void)
441 ira_expand_reg_equiv ();
442 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
444 rtx res;
446 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
447 ira_reg_equiv[i].memory = copy_rtx (res);
448 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
449 ira_reg_equiv[i].invariant = copy_rtx (res);
453 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
455 /* Update equivalence for REGNO. We need to this as the equivalence
456 might contain other pseudos which are changed by their
457 equivalences. */
458 static void
459 update_equiv (int regno)
461 rtx x;
463 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
464 ira_reg_equiv[regno].memory
465 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
466 NULL_RTX);
467 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
468 ira_reg_equiv[regno].invariant
469 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
470 NULL_RTX);
473 /* If we have decided to substitute X with another value, return that
474 value, otherwise return X. */
475 static rtx
476 get_equiv (rtx x)
478 int regno;
479 rtx res;
481 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
482 || ! ira_reg_equiv[regno].defined_p
483 || ! ira_reg_equiv[regno].profitable_p
484 || lra_get_regno_hard_regno (regno) >= 0)
485 return x;
486 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
488 if (targetm.cannot_substitute_mem_equiv_p (res))
489 return x;
490 return res;
492 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
493 return res;
494 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
495 return res;
496 gcc_unreachable ();
499 /* If we have decided to substitute X with the equivalent value,
500 return that value after elimination for INSN, otherwise return
501 X. */
502 static rtx
503 get_equiv_with_elimination (rtx x, rtx_insn *insn)
505 rtx res = get_equiv (x);
507 if (x == res || CONSTANT_P (res))
508 return res;
509 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
510 false, false, 0, true);
513 /* Set up curr_operand_mode. */
514 static void
515 init_curr_operand_mode (void)
517 int nop = curr_static_id->n_operands;
518 for (int i = 0; i < nop; i++)
520 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
521 if (mode == VOIDmode)
523 /* The .md mode for address operands is the mode of the
524 addressed value rather than the mode of the address itself. */
525 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
526 mode = Pmode;
527 else
528 mode = curr_static_id->operand[i].mode;
530 curr_operand_mode[i] = mode;
536 /* The page contains code to reuse input reloads. */
538 /* Structure describes input reload of the current insns. */
539 struct input_reload
541 /* Reloaded value. */
542 rtx input;
543 /* Reload pseudo used. */
544 rtx reg;
547 /* The number of elements in the following array. */
548 static int curr_insn_input_reloads_num;
549 /* Array containing info about input reloads. It is used to find the
550 same input reload and reuse the reload pseudo in this case. */
551 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
553 /* Initiate data concerning reuse of input reloads for the current
554 insn. */
555 static void
556 init_curr_insn_input_reloads (void)
558 curr_insn_input_reloads_num = 0;
561 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
562 created input reload pseudo (only if TYPE is not OP_OUT). Don't
563 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
564 wrapped up in SUBREG. The result pseudo is returned through
565 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
566 reused the already created input reload pseudo. Use TITLE to
567 describe new registers for debug purposes. */
568 static bool
569 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
570 enum reg_class rclass, bool in_subreg_p,
571 const char *title, rtx *result_reg)
573 int i, regno;
574 enum reg_class new_class;
576 if (type == OP_OUT)
578 *result_reg
579 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
580 return true;
582 /* Prevent reuse value of expression with side effects,
583 e.g. volatile memory. */
584 if (! side_effects_p (original))
585 for (i = 0; i < curr_insn_input_reloads_num; i++)
586 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
587 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
589 rtx reg = curr_insn_input_reloads[i].reg;
590 regno = REGNO (reg);
591 /* If input is equal to original and both are VOIDmode,
592 GET_MODE (reg) might be still different from mode.
593 Ensure we don't return *result_reg with wrong mode. */
594 if (GET_MODE (reg) != mode)
596 if (in_subreg_p)
597 continue;
598 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
599 continue;
600 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
601 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
602 continue;
604 *result_reg = reg;
605 if (lra_dump_file != NULL)
607 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
608 dump_value_slim (lra_dump_file, original, 1);
610 if (new_class != lra_get_allocno_class (regno))
611 lra_change_class (regno, new_class, ", change to", false);
612 if (lra_dump_file != NULL)
613 fprintf (lra_dump_file, "\n");
614 return false;
616 *result_reg = lra_create_new_reg (mode, original, rclass, title);
617 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
618 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
619 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
620 return true;
625 /* The page contains code to extract memory address parts. */
627 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
628 static inline bool
629 ok_for_index_p_nonstrict (rtx reg)
631 unsigned regno = REGNO (reg);
633 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
636 /* A version of regno_ok_for_base_p for use here, when all pseudos
637 should count as OK. Arguments as for regno_ok_for_base_p. */
638 static inline bool
639 ok_for_base_p_nonstrict (rtx reg, machine_mode mode, addr_space_t as,
640 enum rtx_code outer_code, enum rtx_code index_code)
642 unsigned regno = REGNO (reg);
644 if (regno >= FIRST_PSEUDO_REGISTER)
645 return true;
646 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
651 /* The page contains major code to choose the current insn alternative
652 and generate reloads for it. */
654 /* Return the offset from REGNO of the least significant register
655 in (reg:MODE REGNO).
657 This function is used to tell whether two registers satisfy
658 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
660 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
661 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
663 lra_constraint_offset (int regno, machine_mode mode)
665 lra_assert (regno < FIRST_PSEUDO_REGISTER);
666 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
667 && SCALAR_INT_MODE_P (mode))
668 return hard_regno_nregs[regno][mode] - 1;
669 return 0;
672 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
673 if they are the same hard reg, and has special hacks for
674 auto-increment and auto-decrement. This is specifically intended for
675 process_alt_operands to use in determining whether two operands
676 match. X is the operand whose number is the lower of the two.
678 It is supposed that X is the output operand and Y is the input
679 operand. Y_HARD_REGNO is the final hard regno of register Y or
680 register in subreg Y as we know it now. Otherwise, it is a
681 negative value. */
682 static bool
683 operands_match_p (rtx x, rtx y, int y_hard_regno)
685 int i;
686 RTX_CODE code = GET_CODE (x);
687 const char *fmt;
689 if (x == y)
690 return true;
691 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
692 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
694 int j;
696 i = get_hard_regno (x);
697 if (i < 0)
698 goto slow;
700 if ((j = y_hard_regno) < 0)
701 goto slow;
703 i += lra_constraint_offset (i, GET_MODE (x));
704 j += lra_constraint_offset (j, GET_MODE (y));
706 return i == j;
709 /* If two operands must match, because they are really a single
710 operand of an assembler insn, then two post-increments are invalid
711 because the assembler insn would increment only once. On the
712 other hand, a post-increment matches ordinary indexing if the
713 post-increment is the output operand. */
714 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
715 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
717 /* Two pre-increments are invalid because the assembler insn would
718 increment only once. On the other hand, a pre-increment matches
719 ordinary indexing if the pre-increment is the input operand. */
720 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
721 || GET_CODE (y) == PRE_MODIFY)
722 return operands_match_p (x, XEXP (y, 0), -1);
724 slow:
726 if (code == REG && REG_P (y))
727 return REGNO (x) == REGNO (y);
729 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
730 && x == SUBREG_REG (y))
731 return true;
732 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
733 && SUBREG_REG (x) == y)
734 return true;
736 /* Now we have disposed of all the cases in which different rtx
737 codes can match. */
738 if (code != GET_CODE (y))
739 return false;
741 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
742 if (GET_MODE (x) != GET_MODE (y))
743 return false;
745 switch (code)
747 CASE_CONST_UNIQUE:
748 return false;
750 case LABEL_REF:
751 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
752 case SYMBOL_REF:
753 return XSTR (x, 0) == XSTR (y, 0);
755 default:
756 break;
759 /* Compare the elements. If any pair of corresponding elements fail
760 to match, return false for the whole things. */
762 fmt = GET_RTX_FORMAT (code);
763 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
765 int val, j;
766 switch (fmt[i])
768 case 'w':
769 if (XWINT (x, i) != XWINT (y, i))
770 return false;
771 break;
773 case 'i':
774 if (XINT (x, i) != XINT (y, i))
775 return false;
776 break;
778 case 'e':
779 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
780 if (val == 0)
781 return false;
782 break;
784 case '0':
785 break;
787 case 'E':
788 if (XVECLEN (x, i) != XVECLEN (y, i))
789 return false;
790 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
792 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
793 if (val == 0)
794 return false;
796 break;
798 /* It is believed that rtx's at this level will never
799 contain anything but integers and other rtx's, except for
800 within LABEL_REFs and SYMBOL_REFs. */
801 default:
802 gcc_unreachable ();
805 return true;
808 /* True if X is a constant that can be forced into the constant pool.
809 MODE is the mode of the operand, or VOIDmode if not known. */
810 #define CONST_POOL_OK_P(MODE, X) \
811 ((MODE) != VOIDmode \
812 && CONSTANT_P (X) \
813 && GET_CODE (X) != HIGH \
814 && !targetm.cannot_force_const_mem (MODE, X))
816 /* True if C is a non-empty register class that has too few registers
817 to be safely used as a reload target class. */
818 #define SMALL_REGISTER_CLASS_P(C) \
819 (ira_class_hard_regs_num [(C)] == 1 \
820 || (ira_class_hard_regs_num [(C)] >= 1 \
821 && targetm.class_likely_spilled_p (C)))
823 /* If REG is a reload pseudo, try to make its class satisfying CL. */
824 static void
825 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
827 enum reg_class rclass;
829 /* Do not make more accurate class from reloads generated. They are
830 mostly moves with a lot of constraints. Making more accurate
831 class may results in very narrow class and impossibility of find
832 registers for several reloads of one insn. */
833 if (INSN_UID (curr_insn) >= new_insn_uid_start)
834 return;
835 if (GET_CODE (reg) == SUBREG)
836 reg = SUBREG_REG (reg);
837 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
838 return;
839 if (in_class_p (reg, cl, &rclass) && rclass != cl)
840 lra_change_class (REGNO (reg), rclass, " Change to", true);
843 /* Searches X for any reference to a reg with the same value as REGNO,
844 returning the rtx of the reference found if any. Otherwise,
845 returns NULL_RTX. */
846 static rtx
847 regno_val_use_in (unsigned int regno, rtx x)
849 const char *fmt;
850 int i, j;
851 rtx tem;
853 if (REG_P (x) && lra_reg_info[REGNO (x)].val == lra_reg_info[regno].val)
854 return x;
856 fmt = GET_RTX_FORMAT (GET_CODE (x));
857 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
859 if (fmt[i] == 'e')
861 if ((tem = regno_val_use_in (regno, XEXP (x, i))))
862 return tem;
864 else if (fmt[i] == 'E')
865 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
866 if ((tem = regno_val_use_in (regno , XVECEXP (x, i, j))))
867 return tem;
870 return NULL_RTX;
873 /* Generate reloads for matching OUT and INS (array of input operand
874 numbers with end marker -1) with reg class GOAL_CLASS. Add input
875 and output reloads correspondingly to the lists *BEFORE and *AFTER.
876 OUT might be negative. In this case we generate input reloads for
877 matched input operands INS. EARLY_CLOBBER_P is a flag that the
878 output operand is early clobbered for chosen alternative. */
879 static void
880 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
881 rtx_insn **before, rtx_insn **after, bool early_clobber_p)
883 int i, in;
884 rtx new_in_reg, new_out_reg, reg;
885 machine_mode inmode, outmode;
886 rtx in_rtx = *curr_id->operand_loc[ins[0]];
887 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
889 inmode = curr_operand_mode[ins[0]];
890 outmode = out < 0 ? inmode : curr_operand_mode[out];
891 push_to_sequence (*before);
892 if (inmode != outmode)
894 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
896 reg = new_in_reg
897 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
898 goal_class, "");
899 if (SCALAR_INT_MODE_P (inmode))
900 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
901 else
902 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
903 LRA_SUBREG_P (new_out_reg) = 1;
904 /* If the input reg is dying here, we can use the same hard
905 register for REG and IN_RTX. We do it only for original
906 pseudos as reload pseudos can die although original
907 pseudos still live where reload pseudos dies. */
908 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
909 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
910 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
912 else
914 reg = new_out_reg
915 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
916 goal_class, "");
917 if (SCALAR_INT_MODE_P (outmode))
918 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
919 else
920 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
921 /* NEW_IN_REG is non-paradoxical subreg. We don't want
922 NEW_OUT_REG living above. We add clobber clause for
923 this. This is just a temporary clobber. We can remove
924 it at the end of LRA work. */
925 rtx_insn *clobber = emit_clobber (new_out_reg);
926 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
927 LRA_SUBREG_P (new_in_reg) = 1;
928 if (GET_CODE (in_rtx) == SUBREG)
930 rtx subreg_reg = SUBREG_REG (in_rtx);
932 /* If SUBREG_REG is dying here and sub-registers IN_RTX
933 and NEW_IN_REG are similar, we can use the same hard
934 register for REG and SUBREG_REG. */
935 if (REG_P (subreg_reg)
936 && (int) REGNO (subreg_reg) < lra_new_regno_start
937 && GET_MODE (subreg_reg) == outmode
938 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
939 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
940 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
944 else
946 /* Pseudos have values -- see comments for lra_reg_info.
947 Different pseudos with the same value do not conflict even if
948 they live in the same place. When we create a pseudo we
949 assign value of original pseudo (if any) from which we
950 created the new pseudo. If we create the pseudo from the
951 input pseudo, the new pseudo will have no conflict with the
952 input pseudo which is wrong when the input pseudo lives after
953 the insn and as the new pseudo value is changed by the insn
954 output. Therefore we create the new pseudo from the output
955 except the case when we have single matched dying input
956 pseudo.
958 We cannot reuse the current output register because we might
959 have a situation like "a <- a op b", where the constraints
960 force the second input operand ("b") to match the output
961 operand ("a"). "b" must then be copied into a new register
962 so that it doesn't clobber the current value of "a".
964 We can not use the same value if the output pseudo is
965 early clobbered or the input pseudo is mentioned in the
966 output, e.g. as an address part in memory, because
967 output reload will actually extend the pseudo liveness.
968 We don't care about eliminable hard regs here as we are
969 interesting only in pseudos. */
971 new_in_reg = new_out_reg
972 = (! early_clobber_p && ins[1] < 0 && REG_P (in_rtx)
973 && (int) REGNO (in_rtx) < lra_new_regno_start
974 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
975 && (out < 0
976 || regno_val_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
977 ? lra_create_new_reg (inmode, in_rtx, goal_class, "")
978 : lra_create_new_reg_with_unique_value (outmode, out_rtx,
979 goal_class, ""));
981 /* In operand can be got from transformations before processing insn
982 constraints. One example of such transformations is subreg
983 reloading (see function simplify_operand_subreg). The new
984 pseudos created by the transformations might have inaccurate
985 class (ALL_REGS) and we should make their classes more
986 accurate. */
987 narrow_reload_pseudo_class (in_rtx, goal_class);
988 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
989 *before = get_insns ();
990 end_sequence ();
991 for (i = 0; (in = ins[i]) >= 0; i++)
993 lra_assert
994 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
995 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
996 *curr_id->operand_loc[in] = new_in_reg;
998 lra_update_dups (curr_id, ins);
999 if (out < 0)
1000 return;
1001 /* See a comment for the input operand above. */
1002 narrow_reload_pseudo_class (out_rtx, goal_class);
1003 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
1005 start_sequence ();
1006 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
1007 emit_insn (*after);
1008 *after = get_insns ();
1009 end_sequence ();
1011 *curr_id->operand_loc[out] = new_out_reg;
1012 lra_update_dup (curr_id, out);
1015 /* Return register class which is union of all reg classes in insn
1016 constraint alternative string starting with P. */
1017 static enum reg_class
1018 reg_class_from_constraints (const char *p)
1020 int c, len;
1021 enum reg_class op_class = NO_REGS;
1024 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1026 case '#':
1027 case ',':
1028 return op_class;
1030 case 'g':
1031 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1032 break;
1034 default:
1035 enum constraint_num cn = lookup_constraint (p);
1036 enum reg_class cl = reg_class_for_constraint (cn);
1037 if (cl == NO_REGS)
1039 if (insn_extra_address_constraint (cn))
1040 op_class
1041 = (reg_class_subunion
1042 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1043 ADDRESS, SCRATCH)]);
1044 break;
1047 op_class = reg_class_subunion[op_class][cl];
1048 break;
1050 while ((p += len), c);
1051 return op_class;
1054 /* If OP is a register, return the class of the register as per
1055 get_reg_class, otherwise return NO_REGS. */
1056 static inline enum reg_class
1057 get_op_class (rtx op)
1059 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1062 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1063 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1064 SUBREG for VAL to make them equal. */
1065 static rtx_insn *
1066 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1068 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1070 /* Usually size of mem_pseudo is greater than val size but in
1071 rare cases it can be less as it can be defined by target
1072 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1073 if (! MEM_P (val))
1075 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1076 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1078 LRA_SUBREG_P (val) = 1;
1080 else
1082 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1083 LRA_SUBREG_P (mem_pseudo) = 1;
1086 return to_p ? gen_move_insn (mem_pseudo, val)
1087 : gen_move_insn (val, mem_pseudo);
1090 /* Process a special case insn (register move), return true if we
1091 don't need to process it anymore. INSN should be a single set
1092 insn. Set up that RTL was changed through CHANGE_P and macro
1093 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1094 SEC_MEM_P. */
1095 static bool
1096 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1098 int sregno, dregno;
1099 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1100 rtx_insn *before;
1101 enum reg_class dclass, sclass, secondary_class;
1102 secondary_reload_info sri;
1104 lra_assert (curr_insn_set != NULL_RTX);
1105 dreg = dest = SET_DEST (curr_insn_set);
1106 sreg = src = SET_SRC (curr_insn_set);
1107 if (GET_CODE (dest) == SUBREG)
1108 dreg = SUBREG_REG (dest);
1109 if (GET_CODE (src) == SUBREG)
1110 sreg = SUBREG_REG (src);
1111 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1112 return false;
1113 sclass = dclass = NO_REGS;
1114 if (REG_P (dreg))
1115 dclass = get_reg_class (REGNO (dreg));
1116 if (dclass == ALL_REGS)
1117 /* ALL_REGS is used for new pseudos created by transformations
1118 like reload of SUBREG_REG (see function
1119 simplify_operand_subreg). We don't know their class yet. We
1120 should figure out the class from processing the insn
1121 constraints not in this fast path function. Even if ALL_REGS
1122 were a right class for the pseudo, secondary_... hooks usually
1123 are not define for ALL_REGS. */
1124 return false;
1125 if (REG_P (sreg))
1126 sclass = get_reg_class (REGNO (sreg));
1127 if (sclass == ALL_REGS)
1128 /* See comments above. */
1129 return false;
1130 if (sclass == NO_REGS && dclass == NO_REGS)
1131 return false;
1132 #ifdef SECONDARY_MEMORY_NEEDED
1133 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1134 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1135 && ((sclass != NO_REGS && dclass != NO_REGS)
1136 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1137 #endif
1140 *sec_mem_p = true;
1141 return false;
1143 #endif
1144 if (! REG_P (dreg) || ! REG_P (sreg))
1145 return false;
1146 sri.prev_sri = NULL;
1147 sri.icode = CODE_FOR_nothing;
1148 sri.extra_cost = 0;
1149 secondary_class = NO_REGS;
1150 /* Set up hard register for a reload pseudo for hook
1151 secondary_reload because some targets just ignore unassigned
1152 pseudos in the hook. */
1153 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1155 dregno = REGNO (dreg);
1156 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1158 else
1159 dregno = -1;
1160 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1162 sregno = REGNO (sreg);
1163 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1165 else
1166 sregno = -1;
1167 if (sclass != NO_REGS)
1168 secondary_class
1169 = (enum reg_class) targetm.secondary_reload (false, dest,
1170 (reg_class_t) sclass,
1171 GET_MODE (src), &sri);
1172 if (sclass == NO_REGS
1173 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1174 && dclass != NO_REGS))
1176 enum reg_class old_sclass = secondary_class;
1177 secondary_reload_info old_sri = sri;
1179 sri.prev_sri = NULL;
1180 sri.icode = CODE_FOR_nothing;
1181 sri.extra_cost = 0;
1182 secondary_class
1183 = (enum reg_class) targetm.secondary_reload (true, src,
1184 (reg_class_t) dclass,
1185 GET_MODE (src), &sri);
1186 /* Check the target hook consistency. */
1187 lra_assert
1188 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1189 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1190 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1192 if (sregno >= 0)
1193 reg_renumber [sregno] = -1;
1194 if (dregno >= 0)
1195 reg_renumber [dregno] = -1;
1196 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1197 return false;
1198 *change_p = true;
1199 new_reg = NULL_RTX;
1200 if (secondary_class != NO_REGS)
1201 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1202 secondary_class,
1203 "secondary");
1204 start_sequence ();
1205 if (sri.icode == CODE_FOR_nothing)
1206 lra_emit_move (new_reg, src);
1207 else
1209 enum reg_class scratch_class;
1211 scratch_class = (reg_class_from_constraints
1212 (insn_data[sri.icode].operand[2].constraint));
1213 scratch_reg = (lra_create_new_reg_with_unique_value
1214 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1215 scratch_class, "scratch"));
1216 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1217 src, scratch_reg));
1219 before = get_insns ();
1220 end_sequence ();
1221 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1222 if (new_reg != NULL_RTX)
1223 SET_SRC (curr_insn_set) = new_reg;
1224 else
1226 if (lra_dump_file != NULL)
1228 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1229 dump_insn_slim (lra_dump_file, curr_insn);
1231 lra_set_insn_deleted (curr_insn);
1232 return true;
1234 return false;
1237 /* The following data describe the result of process_alt_operands.
1238 The data are used in curr_insn_transform to generate reloads. */
1240 /* The chosen reg classes which should be used for the corresponding
1241 operands. */
1242 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1243 /* True if the operand should be the same as another operand and that
1244 other operand does not need a reload. */
1245 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1246 /* True if the operand does not need a reload. */
1247 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1248 /* True if the operand can be offsetable memory. */
1249 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1250 /* The number of an operand to which given operand can be matched to. */
1251 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1252 /* The number of elements in the following array. */
1253 static int goal_alt_dont_inherit_ops_num;
1254 /* Numbers of operands whose reload pseudos should not be inherited. */
1255 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1256 /* True if the insn commutative operands should be swapped. */
1257 static bool goal_alt_swapped;
1258 /* The chosen insn alternative. */
1259 static int goal_alt_number;
1261 /* True if the corresponding operand is the result of an equivalence
1262 substitution. */
1263 static bool equiv_substition_p[MAX_RECOG_OPERANDS];
1265 /* The following five variables are used to choose the best insn
1266 alternative. They reflect final characteristics of the best
1267 alternative. */
1269 /* Number of necessary reloads and overall cost reflecting the
1270 previous value and other unpleasantness of the best alternative. */
1271 static int best_losers, best_overall;
1272 /* Overall number hard registers used for reloads. For example, on
1273 some targets we need 2 general registers to reload DFmode and only
1274 one floating point register. */
1275 static int best_reload_nregs;
1276 /* Overall number reflecting distances of previous reloading the same
1277 value. The distances are counted from the current BB start. It is
1278 used to improve inheritance chances. */
1279 static int best_reload_sum;
1281 /* True if the current insn should have no correspondingly input or
1282 output reloads. */
1283 static bool no_input_reloads_p, no_output_reloads_p;
1285 /* True if we swapped the commutative operands in the current
1286 insn. */
1287 static int curr_swapped;
1289 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1290 register of class CL. Add any input reloads to list BEFORE. AFTER
1291 is nonnull if *LOC is an automodified value; handle that case by
1292 adding the required output reloads to list AFTER. Return true if
1293 the RTL was changed.
1295 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1296 register. Return false if the address register is correct. */
1297 static bool
1298 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1299 enum reg_class cl)
1301 int regno;
1302 enum reg_class rclass, new_class;
1303 rtx reg;
1304 rtx new_reg;
1305 machine_mode mode;
1306 bool subreg_p, before_p = false;
1308 subreg_p = GET_CODE (*loc) == SUBREG;
1309 if (subreg_p)
1311 reg = SUBREG_REG (*loc);
1312 mode = GET_MODE (reg);
1314 /* For mode with size bigger than ptr_mode, there unlikely to be "mov"
1315 between two registers with different classes, but there normally will
1316 be "mov" which transfers element of vector register into the general
1317 register, and this normally will be a subreg which should be reloaded
1318 as a whole. This is particularly likely to be triggered when
1319 -fno-split-wide-types specified. */
1320 if (!REG_P (reg)
1321 || in_class_p (reg, cl, &new_class)
1322 || GET_MODE_SIZE (mode) <= GET_MODE_SIZE (ptr_mode))
1323 loc = &SUBREG_REG (*loc);
1326 reg = *loc;
1327 mode = GET_MODE (reg);
1328 if (! REG_P (reg))
1330 if (check_only_p)
1331 return true;
1332 /* Always reload memory in an address even if the target supports
1333 such addresses. */
1334 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1335 before_p = true;
1337 else
1339 regno = REGNO (reg);
1340 rclass = get_reg_class (regno);
1341 if (! check_only_p
1342 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1344 if (lra_dump_file != NULL)
1346 fprintf (lra_dump_file,
1347 "Changing pseudo %d in address of insn %u on equiv ",
1348 REGNO (reg), INSN_UID (curr_insn));
1349 dump_value_slim (lra_dump_file, *loc, 1);
1350 fprintf (lra_dump_file, "\n");
1352 *loc = copy_rtx (*loc);
1354 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1356 if (check_only_p)
1357 return true;
1358 reg = *loc;
1359 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1360 mode, reg, cl, subreg_p, "address", &new_reg))
1361 before_p = true;
1363 else if (new_class != NO_REGS && rclass != new_class)
1365 if (check_only_p)
1366 return true;
1367 lra_change_class (regno, new_class, " Change to", true);
1368 return false;
1370 else
1371 return false;
1373 if (before_p)
1375 push_to_sequence (*before);
1376 lra_emit_move (new_reg, reg);
1377 *before = get_insns ();
1378 end_sequence ();
1380 *loc = new_reg;
1381 if (after != NULL)
1383 start_sequence ();
1384 lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1385 emit_insn (*after);
1386 *after = get_insns ();
1387 end_sequence ();
1389 return true;
1392 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1393 the insn to be inserted before curr insn. AFTER returns the
1394 the insn to be inserted after curr insn. ORIGREG and NEWREG
1395 are the original reg and new reg for reload. */
1396 static void
1397 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1398 rtx newreg)
1400 if (before)
1402 push_to_sequence (*before);
1403 lra_emit_move (newreg, origreg);
1404 *before = get_insns ();
1405 end_sequence ();
1407 if (after)
1409 start_sequence ();
1410 lra_emit_move (origreg, newreg);
1411 emit_insn (*after);
1412 *after = get_insns ();
1413 end_sequence ();
1417 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1419 /* Make reloads for subreg in operand NOP with internal subreg mode
1420 REG_MODE, add new reloads for further processing. Return true if
1421 any change was done. */
1422 static bool
1423 simplify_operand_subreg (int nop, machine_mode reg_mode)
1425 int hard_regno;
1426 rtx_insn *before, *after;
1427 machine_mode mode, innermode;
1428 rtx reg, new_reg;
1429 rtx operand = *curr_id->operand_loc[nop];
1430 enum reg_class regclass;
1431 enum op_type type;
1433 before = after = NULL;
1435 if (GET_CODE (operand) != SUBREG)
1436 return false;
1438 mode = GET_MODE (operand);
1439 reg = SUBREG_REG (operand);
1440 innermode = GET_MODE (reg);
1441 type = curr_static_id->operand[nop].type;
1442 /* If we change address for paradoxical subreg of memory, the
1443 address might violate the necessary alignment or the access might
1444 be slow. So take this into consideration. We should not worry
1445 about access beyond allocated memory for paradoxical memory
1446 subregs as we don't substitute such equiv memory (see processing
1447 equivalences in function lra_constraints) and because for spilled
1448 pseudos we allocate stack memory enough for the biggest
1449 corresponding paradoxical subreg. */
1450 if (MEM_P (reg)
1451 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1452 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1454 rtx subst, old = *curr_id->operand_loc[nop];
1456 alter_subreg (curr_id->operand_loc[nop], false);
1457 subst = *curr_id->operand_loc[nop];
1458 lra_assert (MEM_P (subst));
1459 if (! valid_address_p (innermode, XEXP (reg, 0),
1460 MEM_ADDR_SPACE (reg))
1461 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1462 MEM_ADDR_SPACE (subst)))
1463 return true;
1464 else if ((get_constraint_type (lookup_constraint
1465 (curr_static_id->operand[nop].constraint))
1466 != CT_SPECIAL_MEMORY)
1467 /* We still can reload address and if the address is
1468 valid, we can remove subreg without reloading its
1469 inner memory. */
1470 && valid_address_p (GET_MODE (subst),
1471 regno_reg_rtx
1472 [ira_class_hard_regs
1473 [base_reg_class (GET_MODE (subst),
1474 MEM_ADDR_SPACE (subst),
1475 ADDRESS, SCRATCH)][0]],
1476 MEM_ADDR_SPACE (subst)))
1477 return true;
1479 /* If the address was valid and became invalid, prefer to reload
1480 the memory. Typical case is when the index scale should
1481 correspond the memory. */
1482 *curr_id->operand_loc[nop] = old;
1484 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1486 alter_subreg (curr_id->operand_loc[nop], false);
1487 return true;
1489 else if (CONSTANT_P (reg))
1491 /* Try to simplify subreg of constant. It is usually result of
1492 equivalence substitution. */
1493 if (innermode == VOIDmode
1494 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1495 innermode = curr_static_id->operand[nop].mode;
1496 if ((new_reg = simplify_subreg (mode, reg, innermode,
1497 SUBREG_BYTE (operand))) != NULL_RTX)
1499 *curr_id->operand_loc[nop] = new_reg;
1500 return true;
1503 /* Put constant into memory when we have mixed modes. It generates
1504 a better code in most cases as it does not need a secondary
1505 reload memory. It also prevents LRA looping when LRA is using
1506 secondary reload memory again and again. */
1507 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1508 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1510 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1511 alter_subreg (curr_id->operand_loc[nop], false);
1512 return true;
1514 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1515 if there may be a problem accessing OPERAND in the outer
1516 mode. */
1517 if ((REG_P (reg)
1518 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1519 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1520 /* Don't reload paradoxical subregs because we could be looping
1521 having repeatedly final regno out of hard regs range. */
1522 && (hard_regno_nregs[hard_regno][innermode]
1523 >= hard_regno_nregs[hard_regno][mode])
1524 && simplify_subreg_regno (hard_regno, innermode,
1525 SUBREG_BYTE (operand), mode) < 0
1526 /* Don't reload subreg for matching reload. It is actually
1527 valid subreg in LRA. */
1528 && ! LRA_SUBREG_P (operand))
1529 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1531 enum reg_class rclass;
1533 if (REG_P (reg))
1534 /* There is a big probability that we will get the same class
1535 for the new pseudo and we will get the same insn which
1536 means infinite looping. So spill the new pseudo. */
1537 rclass = NO_REGS;
1538 else
1539 /* The class will be defined later in curr_insn_transform. */
1540 rclass
1541 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1543 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1544 rclass, TRUE, "subreg reg", &new_reg))
1546 bool insert_before, insert_after;
1547 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1549 insert_before = (type != OP_OUT
1550 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1551 insert_after = (type != OP_IN);
1552 insert_move_for_subreg (insert_before ? &before : NULL,
1553 insert_after ? &after : NULL,
1554 reg, new_reg);
1556 SUBREG_REG (operand) = new_reg;
1557 lra_process_new_insns (curr_insn, before, after,
1558 "Inserting subreg reload");
1559 return true;
1561 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1562 IRA allocates hardreg to the inner pseudo reg according to its mode
1563 instead of the outermode, so the size of the hardreg may not be enough
1564 to contain the outermode operand, in that case we may need to insert
1565 reload for the reg. For the following two types of paradoxical subreg,
1566 we need to insert reload:
1567 1. If the op_type is OP_IN, and the hardreg could not be paired with
1568 other hardreg to contain the outermode operand
1569 (checked by in_hard_reg_set_p), we need to insert the reload.
1570 2. If the op_type is OP_OUT or OP_INOUT.
1572 Here is a paradoxical subreg example showing how the reload is generated:
1574 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1575 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1577 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1578 here, if reg107 is assigned to hardreg R15, because R15 is the last
1579 hardreg, compiler cannot find another hardreg to pair with R15 to
1580 contain TImode data. So we insert a TImode reload reg180 for it.
1581 After reload is inserted:
1583 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1584 (reg:DI 107 [ __comp ])) -1
1585 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1586 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1588 Two reload hard registers will be allocated to reg180 to save TImode data
1589 in LRA_assign. */
1590 else if (REG_P (reg)
1591 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1592 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1593 && (hard_regno_nregs[hard_regno][innermode]
1594 < hard_regno_nregs[hard_regno][mode])
1595 && (regclass = lra_get_allocno_class (REGNO (reg)))
1596 && (type != OP_IN
1597 || !in_hard_reg_set_p (reg_class_contents[regclass],
1598 mode, hard_regno)))
1600 /* The class will be defined later in curr_insn_transform. */
1601 enum reg_class rclass
1602 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1604 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1605 rclass, TRUE, "paradoxical subreg", &new_reg))
1607 rtx subreg;
1608 bool insert_before, insert_after;
1610 PUT_MODE (new_reg, mode);
1611 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1612 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1614 insert_before = (type != OP_OUT);
1615 insert_after = (type != OP_IN);
1616 insert_move_for_subreg (insert_before ? &before : NULL,
1617 insert_after ? &after : NULL,
1618 reg, subreg);
1620 SUBREG_REG (operand) = new_reg;
1621 lra_process_new_insns (curr_insn, before, after,
1622 "Inserting paradoxical subreg reload");
1623 return true;
1625 return false;
1628 /* Return TRUE if X refers for a hard register from SET. */
1629 static bool
1630 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1632 int i, j, x_hard_regno;
1633 machine_mode mode;
1634 const char *fmt;
1635 enum rtx_code code;
1637 if (x == NULL_RTX)
1638 return false;
1639 code = GET_CODE (x);
1640 mode = GET_MODE (x);
1641 if (code == SUBREG)
1643 x = SUBREG_REG (x);
1644 code = GET_CODE (x);
1645 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1646 mode = GET_MODE (x);
1649 if (REG_P (x))
1651 x_hard_regno = get_hard_regno (x);
1652 return (x_hard_regno >= 0
1653 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1655 if (MEM_P (x))
1657 struct address_info ad;
1659 decompose_mem_address (&ad, x);
1660 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1661 return true;
1662 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1663 return true;
1665 fmt = GET_RTX_FORMAT (code);
1666 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1668 if (fmt[i] == 'e')
1670 if (uses_hard_regs_p (XEXP (x, i), set))
1671 return true;
1673 else if (fmt[i] == 'E')
1675 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1676 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1677 return true;
1680 return false;
1683 /* Return true if OP is a spilled pseudo. */
1684 static inline bool
1685 spilled_pseudo_p (rtx op)
1687 return (REG_P (op)
1688 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1691 /* Return true if X is a general constant. */
1692 static inline bool
1693 general_constant_p (rtx x)
1695 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1698 static bool
1699 reg_in_class_p (rtx reg, enum reg_class cl)
1701 if (cl == NO_REGS)
1702 return get_reg_class (REGNO (reg)) == NO_REGS;
1703 return in_class_p (reg, cl, NULL);
1706 /* Return true if SET of RCLASS contains no hard regs which can be
1707 used in MODE. */
1708 static bool
1709 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1710 HARD_REG_SET &set,
1711 enum machine_mode mode)
1713 HARD_REG_SET temp;
1715 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1716 COPY_HARD_REG_SET (temp, set);
1717 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1718 return (hard_reg_set_subset_p
1719 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1722 /* Major function to choose the current insn alternative and what
1723 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1724 negative we should consider only this alternative. Return false if
1725 we can not choose the alternative or find how to reload the
1726 operands. */
1727 static bool
1728 process_alt_operands (int only_alternative)
1730 bool ok_p = false;
1731 int nop, overall, nalt;
1732 int n_alternatives = curr_static_id->n_alternatives;
1733 int n_operands = curr_static_id->n_operands;
1734 /* LOSERS counts the operands that don't fit this alternative and
1735 would require loading. */
1736 int losers;
1737 /* REJECT is a count of how undesirable this alternative says it is
1738 if any reloading is required. If the alternative matches exactly
1739 then REJECT is ignored, but otherwise it gets this much counted
1740 against it in addition to the reloading needed. */
1741 int reject;
1742 int op_reject;
1743 /* The number of elements in the following array. */
1744 int early_clobbered_regs_num;
1745 /* Numbers of operands which are early clobber registers. */
1746 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1747 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1748 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1749 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1750 bool curr_alt_win[MAX_RECOG_OPERANDS];
1751 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1752 int curr_alt_matches[MAX_RECOG_OPERANDS];
1753 /* The number of elements in the following array. */
1754 int curr_alt_dont_inherit_ops_num;
1755 /* Numbers of operands whose reload pseudos should not be inherited. */
1756 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1757 rtx op;
1758 /* The register when the operand is a subreg of register, otherwise the
1759 operand itself. */
1760 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1761 /* The register if the operand is a register or subreg of register,
1762 otherwise NULL. */
1763 rtx operand_reg[MAX_RECOG_OPERANDS];
1764 int hard_regno[MAX_RECOG_OPERANDS];
1765 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1766 int reload_nregs, reload_sum;
1767 bool costly_p;
1768 enum reg_class cl;
1770 /* Calculate some data common for all alternatives to speed up the
1771 function. */
1772 for (nop = 0; nop < n_operands; nop++)
1774 rtx reg;
1776 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1777 /* The real hard regno of the operand after the allocation. */
1778 hard_regno[nop] = get_hard_regno (op);
1780 operand_reg[nop] = reg = op;
1781 biggest_mode[nop] = GET_MODE (op);
1782 if (GET_CODE (op) == SUBREG)
1784 operand_reg[nop] = reg = SUBREG_REG (op);
1785 if (GET_MODE_SIZE (biggest_mode[nop])
1786 < GET_MODE_SIZE (GET_MODE (reg)))
1787 biggest_mode[nop] = GET_MODE (reg);
1789 if (! REG_P (reg))
1790 operand_reg[nop] = NULL_RTX;
1791 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1792 || ((int) REGNO (reg)
1793 == lra_get_elimination_hard_regno (REGNO (reg))))
1794 no_subreg_reg_operand[nop] = reg;
1795 else
1796 operand_reg[nop] = no_subreg_reg_operand[nop]
1797 /* Just use natural mode for elimination result. It should
1798 be enough for extra constraints hooks. */
1799 = regno_reg_rtx[hard_regno[nop]];
1802 /* The constraints are made of several alternatives. Each operand's
1803 constraint looks like foo,bar,... with commas separating the
1804 alternatives. The first alternatives for all operands go
1805 together, the second alternatives go together, etc.
1807 First loop over alternatives. */
1808 alternative_mask preferred = curr_id->preferred_alternatives;
1809 if (only_alternative >= 0)
1810 preferred &= ALTERNATIVE_BIT (only_alternative);
1812 for (nalt = 0; nalt < n_alternatives; nalt++)
1814 /* Loop over operands for one constraint alternative. */
1815 if (!TEST_BIT (preferred, nalt))
1816 continue;
1818 overall = losers = reject = reload_nregs = reload_sum = 0;
1819 for (nop = 0; nop < n_operands; nop++)
1821 int inc = (curr_static_id
1822 ->operand_alternative[nalt * n_operands + nop].reject);
1823 if (lra_dump_file != NULL && inc != 0)
1824 fprintf (lra_dump_file,
1825 " Staticly defined alt reject+=%d\n", inc);
1826 reject += inc;
1828 early_clobbered_regs_num = 0;
1830 for (nop = 0; nop < n_operands; nop++)
1832 const char *p;
1833 char *end;
1834 int len, c, m, i, opalt_num, this_alternative_matches;
1835 bool win, did_match, offmemok, early_clobber_p;
1836 /* false => this operand can be reloaded somehow for this
1837 alternative. */
1838 bool badop;
1839 /* true => this operand can be reloaded if the alternative
1840 allows regs. */
1841 bool winreg;
1842 /* True if a constant forced into memory would be OK for
1843 this operand. */
1844 bool constmemok;
1845 enum reg_class this_alternative, this_costly_alternative;
1846 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1847 bool this_alternative_match_win, this_alternative_win;
1848 bool this_alternative_offmemok;
1849 bool scratch_p;
1850 machine_mode mode;
1851 enum constraint_num cn;
1853 opalt_num = nalt * n_operands + nop;
1854 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1856 /* Fast track for no constraints at all. */
1857 curr_alt[nop] = NO_REGS;
1858 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1859 curr_alt_win[nop] = true;
1860 curr_alt_match_win[nop] = false;
1861 curr_alt_offmemok[nop] = false;
1862 curr_alt_matches[nop] = -1;
1863 continue;
1866 op = no_subreg_reg_operand[nop];
1867 mode = curr_operand_mode[nop];
1869 win = did_match = winreg = offmemok = constmemok = false;
1870 badop = true;
1872 early_clobber_p = false;
1873 p = curr_static_id->operand_alternative[opalt_num].constraint;
1875 this_costly_alternative = this_alternative = NO_REGS;
1876 /* We update set of possible hard regs besides its class
1877 because reg class might be inaccurate. For example,
1878 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1879 is translated in HI_REGS because classes are merged by
1880 pairs and there is no accurate intermediate class. */
1881 CLEAR_HARD_REG_SET (this_alternative_set);
1882 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1883 this_alternative_win = false;
1884 this_alternative_match_win = false;
1885 this_alternative_offmemok = false;
1886 this_alternative_matches = -1;
1888 /* An empty constraint should be excluded by the fast
1889 track. */
1890 lra_assert (*p != 0 && *p != ',');
1892 op_reject = 0;
1893 /* Scan this alternative's specs for this operand; set WIN
1894 if the operand fits any letter in this alternative.
1895 Otherwise, clear BADOP if this operand could fit some
1896 letter after reloads, or set WINREG if this operand could
1897 fit after reloads provided the constraint allows some
1898 registers. */
1899 costly_p = false;
1902 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1904 case '\0':
1905 len = 0;
1906 break;
1907 case ',':
1908 c = '\0';
1909 break;
1911 case '&':
1912 early_clobber_p = true;
1913 break;
1915 case '$':
1916 op_reject += LRA_MAX_REJECT;
1917 break;
1918 case '^':
1919 op_reject += LRA_LOSER_COST_FACTOR;
1920 break;
1922 case '#':
1923 /* Ignore rest of this alternative. */
1924 c = '\0';
1925 break;
1927 case '0': case '1': case '2': case '3': case '4':
1928 case '5': case '6': case '7': case '8': case '9':
1930 int m_hregno;
1931 bool match_p;
1933 m = strtoul (p, &end, 10);
1934 p = end;
1935 len = 0;
1936 lra_assert (nop > m);
1938 this_alternative_matches = m;
1939 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1940 /* We are supposed to match a previous operand.
1941 If we do, we win if that one did. If we do
1942 not, count both of the operands as losers.
1943 (This is too conservative, since most of the
1944 time only a single reload insn will be needed
1945 to make the two operands win. As a result,
1946 this alternative may be rejected when it is
1947 actually desirable.) */
1948 match_p = false;
1949 if (operands_match_p (*curr_id->operand_loc[nop],
1950 *curr_id->operand_loc[m], m_hregno))
1952 /* We should reject matching of an early
1953 clobber operand if the matching operand is
1954 not dying in the insn. */
1955 if (! curr_static_id->operand[m].early_clobber
1956 || operand_reg[nop] == NULL_RTX
1957 || (find_regno_note (curr_insn, REG_DEAD,
1958 REGNO (op))
1959 || REGNO (op) == REGNO (operand_reg[m])))
1960 match_p = true;
1962 if (match_p)
1964 /* If we are matching a non-offsettable
1965 address where an offsettable address was
1966 expected, then we must reject this
1967 combination, because we can't reload
1968 it. */
1969 if (curr_alt_offmemok[m]
1970 && MEM_P (*curr_id->operand_loc[m])
1971 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1972 continue;
1974 else
1976 /* Operands don't match. Both operands must
1977 allow a reload register, otherwise we
1978 cannot make them match. */
1979 if (curr_alt[m] == NO_REGS)
1980 break;
1981 /* Retroactively mark the operand we had to
1982 match as a loser, if it wasn't already and
1983 it wasn't matched to a register constraint
1984 (e.g it might be matched by memory). */
1985 if (curr_alt_win[m]
1986 && (operand_reg[m] == NULL_RTX
1987 || hard_regno[m] < 0))
1989 losers++;
1990 reload_nregs
1991 += (ira_reg_class_max_nregs[curr_alt[m]]
1992 [GET_MODE (*curr_id->operand_loc[m])]);
1995 /* Prefer matching earlyclobber alternative as
1996 it results in less hard regs required for
1997 the insn than a non-matching earlyclobber
1998 alternative. */
1999 if (curr_static_id->operand[m].early_clobber)
2001 if (lra_dump_file != NULL)
2002 fprintf
2003 (lra_dump_file,
2004 " %d Matching earlyclobber alt:"
2005 " reject--\n",
2006 nop);
2007 reject--;
2009 /* Otherwise we prefer no matching
2010 alternatives because it gives more freedom
2011 in RA. */
2012 else if (operand_reg[nop] == NULL_RTX
2013 || (find_regno_note (curr_insn, REG_DEAD,
2014 REGNO (operand_reg[nop]))
2015 == NULL_RTX))
2017 if (lra_dump_file != NULL)
2018 fprintf
2019 (lra_dump_file,
2020 " %d Matching alt: reject+=2\n",
2021 nop);
2022 reject += 2;
2025 /* If we have to reload this operand and some
2026 previous operand also had to match the same
2027 thing as this operand, we don't know how to do
2028 that. */
2029 if (!match_p || !curr_alt_win[m])
2031 for (i = 0; i < nop; i++)
2032 if (curr_alt_matches[i] == m)
2033 break;
2034 if (i < nop)
2035 break;
2037 else
2038 did_match = true;
2040 /* This can be fixed with reloads if the operand
2041 we are supposed to match can be fixed with
2042 reloads. */
2043 badop = false;
2044 this_alternative = curr_alt[m];
2045 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
2046 winreg = this_alternative != NO_REGS;
2047 break;
2050 case 'g':
2051 if (MEM_P (op)
2052 || general_constant_p (op)
2053 || spilled_pseudo_p (op))
2054 win = true;
2055 cl = GENERAL_REGS;
2056 goto reg;
2058 default:
2059 cn = lookup_constraint (p);
2060 switch (get_constraint_type (cn))
2062 case CT_REGISTER:
2063 cl = reg_class_for_constraint (cn);
2064 if (cl != NO_REGS)
2065 goto reg;
2066 break;
2068 case CT_CONST_INT:
2069 if (CONST_INT_P (op)
2070 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2071 win = true;
2072 break;
2074 case CT_MEMORY:
2075 if (MEM_P (op)
2076 && satisfies_memory_constraint_p (op, cn))
2077 win = true;
2078 else if (spilled_pseudo_p (op))
2079 win = true;
2081 /* If we didn't already win, we can reload constants
2082 via force_const_mem or put the pseudo value into
2083 memory, or make other memory by reloading the
2084 address like for 'o'. */
2085 if (CONST_POOL_OK_P (mode, op)
2086 || MEM_P (op) || REG_P (op)
2087 /* We can restore the equiv insn by a
2088 reload. */
2089 || equiv_substition_p[nop])
2090 badop = false;
2091 constmemok = true;
2092 offmemok = true;
2093 break;
2095 case CT_ADDRESS:
2096 /* If we didn't already win, we can reload the address
2097 into a base register. */
2098 if (satisfies_address_constraint_p (op, cn))
2099 win = true;
2100 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2101 ADDRESS, SCRATCH);
2102 badop = false;
2103 goto reg;
2105 case CT_FIXED_FORM:
2106 if (constraint_satisfied_p (op, cn))
2107 win = true;
2108 break;
2110 case CT_SPECIAL_MEMORY:
2111 if (MEM_P (op)
2112 && satisfies_memory_constraint_p (op, cn))
2113 win = true;
2114 else if (spilled_pseudo_p (op))
2115 win = true;
2116 break;
2118 break;
2120 reg:
2121 this_alternative = reg_class_subunion[this_alternative][cl];
2122 IOR_HARD_REG_SET (this_alternative_set,
2123 reg_class_contents[cl]);
2124 if (costly_p)
2126 this_costly_alternative
2127 = reg_class_subunion[this_costly_alternative][cl];
2128 IOR_HARD_REG_SET (this_costly_alternative_set,
2129 reg_class_contents[cl]);
2131 if (mode == BLKmode)
2132 break;
2133 winreg = true;
2134 if (REG_P (op))
2136 if (hard_regno[nop] >= 0
2137 && in_hard_reg_set_p (this_alternative_set,
2138 mode, hard_regno[nop]))
2139 win = true;
2140 else if (hard_regno[nop] < 0
2141 && in_class_p (op, this_alternative, NULL))
2142 win = true;
2144 break;
2146 if (c != ' ' && c != '\t')
2147 costly_p = c == '*';
2149 while ((p += len), c);
2151 scratch_p = (operand_reg[nop] != NULL_RTX
2152 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2153 /* Record which operands fit this alternative. */
2154 if (win)
2156 this_alternative_win = true;
2157 if (operand_reg[nop] != NULL_RTX)
2159 if (hard_regno[nop] >= 0)
2161 if (in_hard_reg_set_p (this_costly_alternative_set,
2162 mode, hard_regno[nop]))
2164 if (lra_dump_file != NULL)
2165 fprintf (lra_dump_file,
2166 " %d Costly set: reject++\n",
2167 nop);
2168 reject++;
2171 else
2173 /* Prefer won reg to spilled pseudo under other
2174 equal conditions for possibe inheritance. */
2175 if (! scratch_p)
2177 if (lra_dump_file != NULL)
2178 fprintf
2179 (lra_dump_file,
2180 " %d Non pseudo reload: reject++\n",
2181 nop);
2182 reject++;
2184 if (in_class_p (operand_reg[nop],
2185 this_costly_alternative, NULL))
2187 if (lra_dump_file != NULL)
2188 fprintf
2189 (lra_dump_file,
2190 " %d Non pseudo costly reload:"
2191 " reject++\n",
2192 nop);
2193 reject++;
2196 /* We simulate the behavior of old reload here.
2197 Although scratches need hard registers and it
2198 might result in spilling other pseudos, no reload
2199 insns are generated for the scratches. So it
2200 might cost something but probably less than old
2201 reload pass believes. */
2202 if (scratch_p)
2204 if (lra_dump_file != NULL)
2205 fprintf (lra_dump_file,
2206 " %d Scratch win: reject+=2\n",
2207 nop);
2208 reject += 2;
2212 else if (did_match)
2213 this_alternative_match_win = true;
2214 else
2216 int const_to_mem = 0;
2217 bool no_regs_p;
2219 reject += op_reject;
2220 /* Never do output reload of stack pointer. It makes
2221 impossible to do elimination when SP is changed in
2222 RTL. */
2223 if (op == stack_pointer_rtx && ! frame_pointer_needed
2224 && curr_static_id->operand[nop].type != OP_IN)
2225 goto fail;
2227 /* If this alternative asks for a specific reg class, see if there
2228 is at least one allocatable register in that class. */
2229 no_regs_p
2230 = (this_alternative == NO_REGS
2231 || (hard_reg_set_subset_p
2232 (reg_class_contents[this_alternative],
2233 lra_no_alloc_regs)));
2235 /* For asms, verify that the class for this alternative is possible
2236 for the mode that is specified. */
2237 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2239 int i;
2240 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2241 if (HARD_REGNO_MODE_OK (i, mode)
2242 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2243 mode, i))
2244 break;
2245 if (i == FIRST_PSEUDO_REGISTER)
2246 winreg = false;
2249 /* If this operand accepts a register, and if the
2250 register class has at least one allocatable register,
2251 then this operand can be reloaded. */
2252 if (winreg && !no_regs_p)
2253 badop = false;
2255 if (badop)
2257 if (lra_dump_file != NULL)
2258 fprintf (lra_dump_file,
2259 " alt=%d: Bad operand -- refuse\n",
2260 nalt);
2261 goto fail;
2264 /* If not assigned pseudo has a class which a subset of
2265 required reg class, it is a less costly alternative
2266 as the pseudo still can get a hard reg of necessary
2267 class. */
2268 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2269 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2270 && ira_class_subset_p[this_alternative][cl])
2272 if (lra_dump_file != NULL)
2273 fprintf
2274 (lra_dump_file,
2275 " %d Super set class reg: reject-=3\n", nop);
2276 reject -= 3;
2279 this_alternative_offmemok = offmemok;
2280 if (this_costly_alternative != NO_REGS)
2282 if (lra_dump_file != NULL)
2283 fprintf (lra_dump_file,
2284 " %d Costly loser: reject++\n", nop);
2285 reject++;
2287 /* If the operand is dying, has a matching constraint,
2288 and satisfies constraints of the matched operand
2289 which failed to satisfy the own constraints, most probably
2290 the reload for this operand will be gone. */
2291 if (this_alternative_matches >= 0
2292 && !curr_alt_win[this_alternative_matches]
2293 && REG_P (op)
2294 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2295 && (hard_regno[nop] >= 0
2296 ? in_hard_reg_set_p (this_alternative_set,
2297 mode, hard_regno[nop])
2298 : in_class_p (op, this_alternative, NULL)))
2300 if (lra_dump_file != NULL)
2301 fprintf
2302 (lra_dump_file,
2303 " %d Dying matched operand reload: reject++\n",
2304 nop);
2305 reject++;
2307 else
2309 /* Strict_low_part requires to reload the register
2310 not the sub-register. In this case we should
2311 check that a final reload hard reg can hold the
2312 value mode. */
2313 if (curr_static_id->operand[nop].strict_low
2314 && REG_P (op)
2315 && hard_regno[nop] < 0
2316 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2317 && ira_class_hard_regs_num[this_alternative] > 0
2318 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2319 [this_alternative][0],
2320 GET_MODE
2321 (*curr_id->operand_loc[nop])))
2323 if (lra_dump_file != NULL)
2324 fprintf
2325 (lra_dump_file,
2326 " alt=%d: Strict low subreg reload -- refuse\n",
2327 nalt);
2328 goto fail;
2330 losers++;
2332 if (operand_reg[nop] != NULL_RTX
2333 /* Output operands and matched input operands are
2334 not inherited. The following conditions do not
2335 exactly describe the previous statement but they
2336 are pretty close. */
2337 && curr_static_id->operand[nop].type != OP_OUT
2338 && (this_alternative_matches < 0
2339 || curr_static_id->operand[nop].type != OP_IN))
2341 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2342 (operand_reg[nop])]
2343 .last_reload);
2345 /* The value of reload_sum has sense only if we
2346 process insns in their order. It happens only on
2347 the first constraints sub-pass when we do most of
2348 reload work. */
2349 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2350 reload_sum += last_reload - bb_reload_num;
2352 /* If this is a constant that is reloaded into the
2353 desired class by copying it to memory first, count
2354 that as another reload. This is consistent with
2355 other code and is required to avoid choosing another
2356 alternative when the constant is moved into memory.
2357 Note that the test here is precisely the same as in
2358 the code below that calls force_const_mem. */
2359 if (CONST_POOL_OK_P (mode, op)
2360 && ((targetm.preferred_reload_class
2361 (op, this_alternative) == NO_REGS)
2362 || no_input_reloads_p))
2364 const_to_mem = 1;
2365 if (! no_regs_p)
2366 losers++;
2369 /* Alternative loses if it requires a type of reload not
2370 permitted for this insn. We can always reload
2371 objects with a REG_UNUSED note. */
2372 if ((curr_static_id->operand[nop].type != OP_IN
2373 && no_output_reloads_p
2374 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2375 || (curr_static_id->operand[nop].type != OP_OUT
2376 && no_input_reloads_p && ! const_to_mem)
2377 || (this_alternative_matches >= 0
2378 && (no_input_reloads_p
2379 || (no_output_reloads_p
2380 && (curr_static_id->operand
2381 [this_alternative_matches].type != OP_IN)
2382 && ! find_reg_note (curr_insn, REG_UNUSED,
2383 no_subreg_reg_operand
2384 [this_alternative_matches])))))
2386 if (lra_dump_file != NULL)
2387 fprintf
2388 (lra_dump_file,
2389 " alt=%d: No input/otput reload -- refuse\n",
2390 nalt);
2391 goto fail;
2394 /* Alternative loses if it required class pseudo can not
2395 hold value of required mode. Such insns can be
2396 described by insn definitions with mode iterators. */
2397 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2398 && ! hard_reg_set_empty_p (this_alternative_set)
2399 /* It is common practice for constraints to use a
2400 class which does not have actually enough regs to
2401 hold the value (e.g. x86 AREG for mode requiring
2402 more one general reg). Therefore we have 2
2403 conditions to check that the reload pseudo can
2404 not hold the mode value. */
2405 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2406 [this_alternative][0],
2407 GET_MODE (*curr_id->operand_loc[nop]))
2408 /* The above condition is not enough as the first
2409 reg in ira_class_hard_regs can be not aligned for
2410 multi-words mode values. */
2411 && (prohibited_class_reg_set_mode_p
2412 (this_alternative, this_alternative_set,
2413 GET_MODE (*curr_id->operand_loc[nop]))))
2415 if (lra_dump_file != NULL)
2416 fprintf (lra_dump_file,
2417 " alt=%d: reload pseudo for op %d "
2418 " can not hold the mode value -- refuse\n",
2419 nalt, nop);
2420 goto fail;
2423 /* Check strong discouragement of reload of non-constant
2424 into class THIS_ALTERNATIVE. */
2425 if (! CONSTANT_P (op) && ! no_regs_p
2426 && (targetm.preferred_reload_class
2427 (op, this_alternative) == NO_REGS
2428 || (curr_static_id->operand[nop].type == OP_OUT
2429 && (targetm.preferred_output_reload_class
2430 (op, this_alternative) == NO_REGS))))
2432 if (lra_dump_file != NULL)
2433 fprintf (lra_dump_file,
2434 " %d Non-prefered reload: reject+=%d\n",
2435 nop, LRA_MAX_REJECT);
2436 reject += LRA_MAX_REJECT;
2439 if (! (MEM_P (op) && offmemok)
2440 && ! (const_to_mem && constmemok))
2442 /* We prefer to reload pseudos over reloading other
2443 things, since such reloads may be able to be
2444 eliminated later. So bump REJECT in other cases.
2445 Don't do this in the case where we are forcing a
2446 constant into memory and it will then win since
2447 we don't want to have a different alternative
2448 match then. */
2449 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2451 if (lra_dump_file != NULL)
2452 fprintf
2453 (lra_dump_file,
2454 " %d Non-pseudo reload: reject+=2\n",
2455 nop);
2456 reject += 2;
2459 if (! no_regs_p)
2460 reload_nregs
2461 += ira_reg_class_max_nregs[this_alternative][mode];
2463 if (SMALL_REGISTER_CLASS_P (this_alternative))
2465 if (lra_dump_file != NULL)
2466 fprintf
2467 (lra_dump_file,
2468 " %d Small class reload: reject+=%d\n",
2469 nop, LRA_LOSER_COST_FACTOR / 2);
2470 reject += LRA_LOSER_COST_FACTOR / 2;
2474 /* We are trying to spill pseudo into memory. It is
2475 usually more costly than moving to a hard register
2476 although it might takes the same number of
2477 reloads.
2479 Non-pseudo spill may happen also. Suppose a target allows both
2480 register and memory in the operand constraint alternatives,
2481 then it's typical that an eliminable register has a substition
2482 of "base + offset" which can either be reloaded by a simple
2483 "new_reg <= base + offset" which will match the register
2484 constraint, or a similar reg addition followed by further spill
2485 to and reload from memory which will match the memory
2486 constraint, but this memory spill will be much more costly
2487 usually.
2489 Code below increases the reject for both pseudo and non-pseudo
2490 spill. */
2491 if (no_regs_p
2492 && !(MEM_P (op) && offmemok)
2493 && !(REG_P (op) && hard_regno[nop] < 0))
2495 if (lra_dump_file != NULL)
2496 fprintf
2497 (lra_dump_file,
2498 " %d Spill %spseudo into memory: reject+=3\n",
2499 nop, REG_P (op) ? "" : "Non-");
2500 reject += 3;
2501 if (VECTOR_MODE_P (mode))
2503 /* Spilling vectors into memory is usually more
2504 costly as they contain big values. */
2505 if (lra_dump_file != NULL)
2506 fprintf
2507 (lra_dump_file,
2508 " %d Spill vector pseudo: reject+=2\n",
2509 nop);
2510 reject += 2;
2514 #ifdef SECONDARY_MEMORY_NEEDED
2515 /* If reload requires moving value through secondary
2516 memory, it will need one more insn at least. */
2517 if (this_alternative != NO_REGS
2518 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2519 && ((curr_static_id->operand[nop].type != OP_OUT
2520 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2521 GET_MODE (op)))
2522 || (curr_static_id->operand[nop].type != OP_IN
2523 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2524 GET_MODE (op)))))
2525 losers++;
2526 #endif
2527 /* Input reloads can be inherited more often than output
2528 reloads can be removed, so penalize output
2529 reloads. */
2530 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2532 if (lra_dump_file != NULL)
2533 fprintf
2534 (lra_dump_file,
2535 " %d Non input pseudo reload: reject++\n",
2536 nop);
2537 reject++;
2541 if (early_clobber_p && ! scratch_p)
2543 if (lra_dump_file != NULL)
2544 fprintf (lra_dump_file,
2545 " %d Early clobber: reject++\n", nop);
2546 reject++;
2548 /* ??? We check early clobbers after processing all operands
2549 (see loop below) and there we update the costs more.
2550 Should we update the cost (may be approximately) here
2551 because of early clobber register reloads or it is a rare
2552 or non-important thing to be worth to do it. */
2553 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2554 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2556 if (lra_dump_file != NULL)
2557 fprintf (lra_dump_file,
2558 " alt=%d,overall=%d,losers=%d -- refuse\n",
2559 nalt, overall, losers);
2560 goto fail;
2563 curr_alt[nop] = this_alternative;
2564 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2565 curr_alt_win[nop] = this_alternative_win;
2566 curr_alt_match_win[nop] = this_alternative_match_win;
2567 curr_alt_offmemok[nop] = this_alternative_offmemok;
2568 curr_alt_matches[nop] = this_alternative_matches;
2570 if (this_alternative_matches >= 0
2571 && !did_match && !this_alternative_win)
2572 curr_alt_win[this_alternative_matches] = false;
2574 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2575 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2577 if (curr_insn_set != NULL_RTX && n_operands == 2
2578 /* Prevent processing non-move insns. */
2579 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2580 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2581 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2582 && REG_P (no_subreg_reg_operand[0])
2583 && REG_P (no_subreg_reg_operand[1])
2584 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2585 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2586 || (! curr_alt_win[0] && curr_alt_win[1]
2587 && REG_P (no_subreg_reg_operand[1])
2588 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2589 || (curr_alt_win[0] && ! curr_alt_win[1]
2590 && REG_P (no_subreg_reg_operand[0])
2591 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2592 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2593 no_subreg_reg_operand[1])
2594 || (targetm.preferred_reload_class
2595 (no_subreg_reg_operand[1],
2596 (enum reg_class) curr_alt[1]) != NO_REGS))
2597 /* If it is a result of recent elimination in move
2598 insn we can transform it into an add still by
2599 using this alternative. */
2600 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2602 /* We have a move insn and a new reload insn will be similar
2603 to the current insn. We should avoid such situation as it
2604 results in LRA cycling. */
2605 overall += LRA_MAX_REJECT;
2607 ok_p = true;
2608 curr_alt_dont_inherit_ops_num = 0;
2609 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2611 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2612 HARD_REG_SET temp_set;
2614 i = early_clobbered_nops[nop];
2615 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2616 || hard_regno[i] < 0)
2617 continue;
2618 lra_assert (operand_reg[i] != NULL_RTX);
2619 clobbered_hard_regno = hard_regno[i];
2620 CLEAR_HARD_REG_SET (temp_set);
2621 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2622 first_conflict_j = last_conflict_j = -1;
2623 for (j = 0; j < n_operands; j++)
2624 if (j == i
2625 /* We don't want process insides of match_operator and
2626 match_parallel because otherwise we would process
2627 their operands once again generating a wrong
2628 code. */
2629 || curr_static_id->operand[j].is_operator)
2630 continue;
2631 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2632 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2633 continue;
2634 /* If we don't reload j-th operand, check conflicts. */
2635 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2636 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2638 if (first_conflict_j < 0)
2639 first_conflict_j = j;
2640 last_conflict_j = j;
2642 if (last_conflict_j < 0)
2643 continue;
2644 /* If earlyclobber operand conflicts with another
2645 non-matching operand which is actually the same register
2646 as the earlyclobber operand, it is better to reload the
2647 another operand as an operand matching the earlyclobber
2648 operand can be also the same. */
2649 if (first_conflict_j == last_conflict_j
2650 && operand_reg[last_conflict_j] != NULL_RTX
2651 && ! curr_alt_match_win[last_conflict_j]
2652 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2654 curr_alt_win[last_conflict_j] = false;
2655 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2656 = last_conflict_j;
2657 losers++;
2658 /* Early clobber was already reflected in REJECT. */
2659 lra_assert (reject > 0);
2660 if (lra_dump_file != NULL)
2661 fprintf
2662 (lra_dump_file,
2663 " %d Conflict early clobber reload: reject--\n",
2665 reject--;
2666 overall += LRA_LOSER_COST_FACTOR - 1;
2668 else
2670 /* We need to reload early clobbered register and the
2671 matched registers. */
2672 for (j = 0; j < n_operands; j++)
2673 if (curr_alt_matches[j] == i)
2675 curr_alt_match_win[j] = false;
2676 losers++;
2677 overall += LRA_LOSER_COST_FACTOR;
2679 if (! curr_alt_match_win[i])
2680 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2681 else
2683 /* Remember pseudos used for match reloads are never
2684 inherited. */
2685 lra_assert (curr_alt_matches[i] >= 0);
2686 curr_alt_win[curr_alt_matches[i]] = false;
2688 curr_alt_win[i] = curr_alt_match_win[i] = false;
2689 losers++;
2690 /* Early clobber was already reflected in REJECT. */
2691 lra_assert (reject > 0);
2692 if (lra_dump_file != NULL)
2693 fprintf
2694 (lra_dump_file,
2695 " %d Matched conflict early clobber reloads:"
2696 "reject--\n",
2698 reject--;
2699 overall += LRA_LOSER_COST_FACTOR - 1;
2702 if (lra_dump_file != NULL)
2703 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2704 nalt, overall, losers, reload_nregs);
2706 /* If this alternative can be made to work by reloading, and it
2707 needs less reloading than the others checked so far, record
2708 it as the chosen goal for reloading. */
2709 if ((best_losers != 0 && losers == 0)
2710 || (((best_losers == 0 && losers == 0)
2711 || (best_losers != 0 && losers != 0))
2712 && (best_overall > overall
2713 || (best_overall == overall
2714 /* If the cost of the reloads is the same,
2715 prefer alternative which requires minimal
2716 number of reload regs. */
2717 && (reload_nregs < best_reload_nregs
2718 || (reload_nregs == best_reload_nregs
2719 && (best_reload_sum < reload_sum
2720 || (best_reload_sum == reload_sum
2721 && nalt < goal_alt_number))))))))
2723 for (nop = 0; nop < n_operands; nop++)
2725 goal_alt_win[nop] = curr_alt_win[nop];
2726 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2727 goal_alt_matches[nop] = curr_alt_matches[nop];
2728 goal_alt[nop] = curr_alt[nop];
2729 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2731 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2732 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2733 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2734 goal_alt_swapped = curr_swapped;
2735 best_overall = overall;
2736 best_losers = losers;
2737 best_reload_nregs = reload_nregs;
2738 best_reload_sum = reload_sum;
2739 goal_alt_number = nalt;
2741 if (losers == 0)
2742 /* Everything is satisfied. Do not process alternatives
2743 anymore. */
2744 break;
2745 fail:
2748 return ok_p;
2751 /* Make reload base reg from address AD. */
2752 static rtx
2753 base_to_reg (struct address_info *ad)
2755 enum reg_class cl;
2756 int code = -1;
2757 rtx new_inner = NULL_RTX;
2758 rtx new_reg = NULL_RTX;
2759 rtx_insn *insn;
2760 rtx_insn *last_insn = get_last_insn();
2762 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2763 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2764 get_index_code (ad));
2765 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2766 cl, "base");
2767 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2768 ad->disp_term == NULL
2769 ? gen_int_mode (0, ad->mode)
2770 : *ad->disp_term);
2771 if (!valid_address_p (ad->mode, new_inner, ad->as))
2772 return NULL_RTX;
2773 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base_term));
2774 code = recog_memoized (insn);
2775 if (code < 0)
2777 delete_insns_since (last_insn);
2778 return NULL_RTX;
2781 return new_inner;
2784 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2785 static rtx
2786 base_plus_disp_to_reg (struct address_info *ad)
2788 enum reg_class cl;
2789 rtx new_reg;
2791 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2792 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2793 get_index_code (ad));
2794 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2795 cl, "base + disp");
2796 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2797 return new_reg;
2800 /* Make reload of index part of address AD. Return the new
2801 pseudo. */
2802 static rtx
2803 index_part_to_reg (struct address_info *ad)
2805 rtx new_reg;
2807 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2808 INDEX_REG_CLASS, "index term");
2809 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2810 GEN_INT (get_index_scale (ad)), new_reg, 1);
2811 return new_reg;
2814 /* Return true if we can add a displacement to address AD, even if that
2815 makes the address invalid. The fix-up code requires any new address
2816 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2817 static bool
2818 can_add_disp_p (struct address_info *ad)
2820 return (!ad->autoinc_p
2821 && ad->segment == NULL
2822 && ad->base == ad->base_term
2823 && ad->disp == ad->disp_term);
2826 /* Make equiv substitution in address AD. Return true if a substitution
2827 was made. */
2828 static bool
2829 equiv_address_substitution (struct address_info *ad)
2831 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2832 HOST_WIDE_INT disp, scale;
2833 bool change_p;
2835 base_term = strip_subreg (ad->base_term);
2836 if (base_term == NULL)
2837 base_reg = new_base_reg = NULL_RTX;
2838 else
2840 base_reg = *base_term;
2841 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2843 index_term = strip_subreg (ad->index_term);
2844 if (index_term == NULL)
2845 index_reg = new_index_reg = NULL_RTX;
2846 else
2848 index_reg = *index_term;
2849 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2851 if (base_reg == new_base_reg && index_reg == new_index_reg)
2852 return false;
2853 disp = 0;
2854 change_p = false;
2855 if (lra_dump_file != NULL)
2857 fprintf (lra_dump_file, "Changing address in insn %d ",
2858 INSN_UID (curr_insn));
2859 dump_value_slim (lra_dump_file, *ad->outer, 1);
2861 if (base_reg != new_base_reg)
2863 if (REG_P (new_base_reg))
2865 *base_term = new_base_reg;
2866 change_p = true;
2868 else if (GET_CODE (new_base_reg) == PLUS
2869 && REG_P (XEXP (new_base_reg, 0))
2870 && CONST_INT_P (XEXP (new_base_reg, 1))
2871 && can_add_disp_p (ad))
2873 disp += INTVAL (XEXP (new_base_reg, 1));
2874 *base_term = XEXP (new_base_reg, 0);
2875 change_p = true;
2877 if (ad->base_term2 != NULL)
2878 *ad->base_term2 = *ad->base_term;
2880 if (index_reg != new_index_reg)
2882 if (REG_P (new_index_reg))
2884 *index_term = new_index_reg;
2885 change_p = true;
2887 else if (GET_CODE (new_index_reg) == PLUS
2888 && REG_P (XEXP (new_index_reg, 0))
2889 && CONST_INT_P (XEXP (new_index_reg, 1))
2890 && can_add_disp_p (ad)
2891 && (scale = get_index_scale (ad)))
2893 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2894 *index_term = XEXP (new_index_reg, 0);
2895 change_p = true;
2898 if (disp != 0)
2900 if (ad->disp != NULL)
2901 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2902 else
2904 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2905 update_address (ad);
2907 change_p = true;
2909 if (lra_dump_file != NULL)
2911 if (! change_p)
2912 fprintf (lra_dump_file, " -- no change\n");
2913 else
2915 fprintf (lra_dump_file, " on equiv ");
2916 dump_value_slim (lra_dump_file, *ad->outer, 1);
2917 fprintf (lra_dump_file, "\n");
2920 return change_p;
2923 /* Major function to make reloads for an address in operand NOP or
2924 check its correctness (If CHECK_ONLY_P is true). The supported
2925 cases are:
2927 1) an address that existed before LRA started, at which point it
2928 must have been valid. These addresses are subject to elimination
2929 and may have become invalid due to the elimination offset being out
2930 of range.
2932 2) an address created by forcing a constant to memory
2933 (force_const_to_mem). The initial form of these addresses might
2934 not be valid, and it is this function's job to make them valid.
2936 3) a frame address formed from a register and a (possibly zero)
2937 constant offset. As above, these addresses might not be valid and
2938 this function must make them so.
2940 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2941 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2942 address. Return true for any RTL change.
2944 The function is a helper function which does not produce all
2945 transformations (when CHECK_ONLY_P is false) which can be
2946 necessary. It does just basic steps. To do all necessary
2947 transformations use function process_address. */
2948 static bool
2949 process_address_1 (int nop, bool check_only_p,
2950 rtx_insn **before, rtx_insn **after)
2952 struct address_info ad;
2953 rtx new_reg;
2954 HOST_WIDE_INT scale;
2955 rtx op = *curr_id->operand_loc[nop];
2956 const char *constraint = curr_static_id->operand[nop].constraint;
2957 enum constraint_num cn = lookup_constraint (constraint);
2958 bool change_p = false;
2960 if (MEM_P (op)
2961 && GET_MODE (op) == BLKmode
2962 && GET_CODE (XEXP (op, 0)) == SCRATCH)
2963 return false;
2965 if (insn_extra_address_constraint (cn))
2966 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2967 else if (MEM_P (op))
2968 decompose_mem_address (&ad, op);
2969 else if (GET_CODE (op) == SUBREG
2970 && MEM_P (SUBREG_REG (op)))
2971 decompose_mem_address (&ad, SUBREG_REG (op));
2972 else
2973 return false;
2974 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
2975 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
2976 when INDEX_REG_CLASS is a single register class. */
2977 if (ad.base_term != NULL
2978 && ad.index_term != NULL
2979 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
2980 && REG_P (*ad.base_term)
2981 && REG_P (*ad.index_term)
2982 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
2983 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
2985 std::swap (ad.base, ad.index);
2986 std::swap (ad.base_term, ad.index_term);
2988 if (! check_only_p)
2989 change_p = equiv_address_substitution (&ad);
2990 if (ad.base_term != NULL
2991 && (process_addr_reg
2992 (ad.base_term, check_only_p, before,
2993 (ad.autoinc_p
2994 && !(REG_P (*ad.base_term)
2995 && find_regno_note (curr_insn, REG_DEAD,
2996 REGNO (*ad.base_term)) != NULL_RTX)
2997 ? after : NULL),
2998 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2999 get_index_code (&ad)))))
3001 change_p = true;
3002 if (ad.base_term2 != NULL)
3003 *ad.base_term2 = *ad.base_term;
3005 if (ad.index_term != NULL
3006 && process_addr_reg (ad.index_term, check_only_p,
3007 before, NULL, INDEX_REG_CLASS))
3008 change_p = true;
3010 /* Target hooks sometimes don't treat extra-constraint addresses as
3011 legitimate address_operands, so handle them specially. */
3012 if (insn_extra_address_constraint (cn)
3013 && satisfies_address_constraint_p (&ad, cn))
3014 return change_p;
3016 if (check_only_p)
3017 return change_p;
3019 /* There are three cases where the shape of *AD.INNER may now be invalid:
3021 1) the original address was valid, but either elimination or
3022 equiv_address_substitution was applied and that made
3023 the address invalid.
3025 2) the address is an invalid symbolic address created by
3026 force_const_to_mem.
3028 3) the address is a frame address with an invalid offset.
3030 4) the address is a frame address with an invalid base.
3032 All these cases involve a non-autoinc address, so there is no
3033 point revalidating other types. */
3034 if (ad.autoinc_p || valid_address_p (&ad))
3035 return change_p;
3037 /* Any index existed before LRA started, so we can assume that the
3038 presence and shape of the index is valid. */
3039 push_to_sequence (*before);
3040 lra_assert (ad.disp == ad.disp_term);
3041 if (ad.base == NULL)
3043 if (ad.index == NULL)
3045 rtx_insn *insn;
3046 rtx_insn *last = get_last_insn ();
3047 int code = -1;
3048 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3049 SCRATCH, SCRATCH);
3050 rtx addr = *ad.inner;
3052 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3053 if (HAVE_lo_sum)
3055 /* addr => lo_sum (new_base, addr), case (2) above. */
3056 insn = emit_insn (gen_rtx_SET
3057 (new_reg,
3058 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
3059 code = recog_memoized (insn);
3060 if (code >= 0)
3062 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
3063 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
3065 /* Try to put lo_sum into register. */
3066 insn = emit_insn (gen_rtx_SET
3067 (new_reg,
3068 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
3069 code = recog_memoized (insn);
3070 if (code >= 0)
3072 *ad.inner = new_reg;
3073 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
3075 *ad.inner = addr;
3076 code = -1;
3082 if (code < 0)
3083 delete_insns_since (last);
3086 if (code < 0)
3088 /* addr => new_base, case (2) above. */
3089 lra_emit_move (new_reg, addr);
3091 for (insn = last == NULL_RTX ? get_insns () : NEXT_INSN (last);
3092 insn != NULL_RTX;
3093 insn = NEXT_INSN (insn))
3094 if (recog_memoized (insn) < 0)
3095 break;
3096 if (insn != NULL_RTX)
3098 /* Do nothing if we cannot generate right insns.
3099 This is analogous to reload pass behavior. */
3100 delete_insns_since (last);
3101 end_sequence ();
3102 return false;
3104 *ad.inner = new_reg;
3107 else
3109 /* index * scale + disp => new base + index * scale,
3110 case (1) above. */
3111 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3112 GET_CODE (*ad.index));
3114 lra_assert (INDEX_REG_CLASS != NO_REGS);
3115 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3116 lra_emit_move (new_reg, *ad.disp);
3117 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3118 new_reg, *ad.index);
3121 else if (ad.index == NULL)
3123 int regno;
3124 enum reg_class cl;
3125 rtx set;
3126 rtx_insn *insns, *last_insn;
3127 /* Try to reload base into register only if the base is invalid
3128 for the address but with valid offset, case (4) above. */
3129 start_sequence ();
3130 new_reg = base_to_reg (&ad);
3132 /* base + disp => new base, cases (1) and (3) above. */
3133 /* Another option would be to reload the displacement into an
3134 index register. However, postreload has code to optimize
3135 address reloads that have the same base and different
3136 displacements, so reloading into an index register would
3137 not necessarily be a win. */
3138 if (new_reg == NULL_RTX)
3139 new_reg = base_plus_disp_to_reg (&ad);
3140 insns = get_insns ();
3141 last_insn = get_last_insn ();
3142 /* If we generated at least two insns, try last insn source as
3143 an address. If we succeed, we generate one less insn. */
3144 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3145 && GET_CODE (SET_SRC (set)) == PLUS
3146 && REG_P (XEXP (SET_SRC (set), 0))
3147 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3149 *ad.inner = SET_SRC (set);
3150 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3152 *ad.base_term = XEXP (SET_SRC (set), 0);
3153 *ad.disp_term = XEXP (SET_SRC (set), 1);
3154 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3155 get_index_code (&ad));
3156 regno = REGNO (*ad.base_term);
3157 if (regno >= FIRST_PSEUDO_REGISTER
3158 && cl != lra_get_allocno_class (regno))
3159 lra_change_class (regno, cl, " Change to", true);
3160 new_reg = SET_SRC (set);
3161 delete_insns_since (PREV_INSN (last_insn));
3164 /* Try if target can split displacement into legitimite new disp
3165 and offset. If it's the case, we replace the last insn with
3166 insns for base + offset => new_reg and set new_reg + new disp
3167 to *ad.inner. */
3168 last_insn = get_last_insn ();
3169 if ((set = single_set (last_insn)) != NULL_RTX
3170 && GET_CODE (SET_SRC (set)) == PLUS
3171 && REG_P (XEXP (SET_SRC (set), 0))
3172 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3173 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3175 rtx addend, disp = XEXP (SET_SRC (set), 1);
3176 if (targetm.legitimize_address_displacement (&disp, &addend,
3177 ad.mode))
3179 rtx_insn *new_insns;
3180 start_sequence ();
3181 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3182 new_insns = get_insns ();
3183 end_sequence ();
3184 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3185 delete_insns_since (PREV_INSN (last_insn));
3186 add_insn (new_insns);
3187 insns = get_insns ();
3190 end_sequence ();
3191 emit_insn (insns);
3192 *ad.inner = new_reg;
3194 else if (ad.disp_term != NULL)
3196 /* base + scale * index + disp => new base + scale * index,
3197 case (1) above. */
3198 new_reg = base_plus_disp_to_reg (&ad);
3199 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3200 new_reg, *ad.index);
3202 else if ((scale = get_index_scale (&ad)) == 1)
3204 /* The last transformation to one reg will be made in
3205 curr_insn_transform function. */
3206 end_sequence ();
3207 return false;
3209 else if (scale != 0)
3211 /* base + scale * index => base + new_reg,
3212 case (1) above.
3213 Index part of address may become invalid. For example, we
3214 changed pseudo on the equivalent memory and a subreg of the
3215 pseudo onto the memory of different mode for which the scale is
3216 prohibitted. */
3217 new_reg = index_part_to_reg (&ad);
3218 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3219 *ad.base_term, new_reg);
3221 else
3223 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3224 SCRATCH, SCRATCH);
3225 rtx addr = *ad.inner;
3227 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3228 /* addr => new_base. */
3229 lra_emit_move (new_reg, addr);
3230 *ad.inner = new_reg;
3232 *before = get_insns ();
3233 end_sequence ();
3234 return true;
3237 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3238 Use process_address_1 as a helper function. Return true for any
3239 RTL changes.
3241 If CHECK_ONLY_P is true, just check address correctness. Return
3242 false if the address correct. */
3243 static bool
3244 process_address (int nop, bool check_only_p,
3245 rtx_insn **before, rtx_insn **after)
3247 bool res = false;
3249 while (process_address_1 (nop, check_only_p, before, after))
3251 if (check_only_p)
3252 return true;
3253 res = true;
3255 return res;
3258 /* Emit insns to reload VALUE into a new register. VALUE is an
3259 auto-increment or auto-decrement RTX whose operand is a register or
3260 memory location; so reloading involves incrementing that location.
3261 IN is either identical to VALUE, or some cheaper place to reload
3262 value being incremented/decremented from.
3264 INC_AMOUNT is the number to increment or decrement by (always
3265 positive and ignored for POST_MODIFY/PRE_MODIFY).
3267 Return pseudo containing the result. */
3268 static rtx
3269 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3271 /* REG or MEM to be copied and incremented. */
3272 rtx incloc = XEXP (value, 0);
3273 /* Nonzero if increment after copying. */
3274 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3275 || GET_CODE (value) == POST_MODIFY);
3276 rtx_insn *last;
3277 rtx inc;
3278 rtx_insn *add_insn;
3279 int code;
3280 rtx real_in = in == value ? incloc : in;
3281 rtx result;
3282 bool plus_p = true;
3284 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3286 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3287 || GET_CODE (XEXP (value, 1)) == MINUS);
3288 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3289 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3290 inc = XEXP (XEXP (value, 1), 1);
3292 else
3294 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3295 inc_amount = -inc_amount;
3297 inc = GEN_INT (inc_amount);
3300 if (! post && REG_P (incloc))
3301 result = incloc;
3302 else
3303 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3304 "INC/DEC result");
3306 if (real_in != result)
3308 /* First copy the location to the result register. */
3309 lra_assert (REG_P (result));
3310 emit_insn (gen_move_insn (result, real_in));
3313 /* We suppose that there are insns to add/sub with the constant
3314 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3315 old reload worked with this assumption. If the assumption
3316 becomes wrong, we should use approach in function
3317 base_plus_disp_to_reg. */
3318 if (in == value)
3320 /* See if we can directly increment INCLOC. */
3321 last = get_last_insn ();
3322 add_insn = emit_insn (plus_p
3323 ? gen_add2_insn (incloc, inc)
3324 : gen_sub2_insn (incloc, inc));
3326 code = recog_memoized (add_insn);
3327 if (code >= 0)
3329 if (! post && result != incloc)
3330 emit_insn (gen_move_insn (result, incloc));
3331 return result;
3333 delete_insns_since (last);
3336 /* If couldn't do the increment directly, must increment in RESULT.
3337 The way we do this depends on whether this is pre- or
3338 post-increment. For pre-increment, copy INCLOC to the reload
3339 register, increment it there, then save back. */
3340 if (! post)
3342 if (real_in != result)
3343 emit_insn (gen_move_insn (result, real_in));
3344 if (plus_p)
3345 emit_insn (gen_add2_insn (result, inc));
3346 else
3347 emit_insn (gen_sub2_insn (result, inc));
3348 if (result != incloc)
3349 emit_insn (gen_move_insn (incloc, result));
3351 else
3353 /* Post-increment.
3355 Because this might be a jump insn or a compare, and because
3356 RESULT may not be available after the insn in an input
3357 reload, we must do the incrementing before the insn being
3358 reloaded for.
3360 We have already copied IN to RESULT. Increment the copy in
3361 RESULT, save that back, then decrement RESULT so it has
3362 the original value. */
3363 if (plus_p)
3364 emit_insn (gen_add2_insn (result, inc));
3365 else
3366 emit_insn (gen_sub2_insn (result, inc));
3367 emit_insn (gen_move_insn (incloc, result));
3368 /* Restore non-modified value for the result. We prefer this
3369 way because it does not require an additional hard
3370 register. */
3371 if (plus_p)
3373 if (CONST_INT_P (inc))
3374 emit_insn (gen_add2_insn (result,
3375 gen_int_mode (-INTVAL (inc),
3376 GET_MODE (result))));
3377 else
3378 emit_insn (gen_sub2_insn (result, inc));
3380 else
3381 emit_insn (gen_add2_insn (result, inc));
3383 return result;
3386 /* Return true if the current move insn does not need processing as we
3387 already know that it satisfies its constraints. */
3388 static bool
3389 simple_move_p (void)
3391 rtx dest, src;
3392 enum reg_class dclass, sclass;
3394 lra_assert (curr_insn_set != NULL_RTX);
3395 dest = SET_DEST (curr_insn_set);
3396 src = SET_SRC (curr_insn_set);
3397 return ((dclass = get_op_class (dest)) != NO_REGS
3398 && (sclass = get_op_class (src)) != NO_REGS
3399 /* The backend guarantees that register moves of cost 2
3400 never need reloads. */
3401 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3404 /* Swap operands NOP and NOP + 1. */
3405 static inline void
3406 swap_operands (int nop)
3408 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3409 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3410 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3411 std::swap (equiv_substition_p[nop], equiv_substition_p[nop + 1]);
3412 /* Swap the duplicates too. */
3413 lra_update_dup (curr_id, nop);
3414 lra_update_dup (curr_id, nop + 1);
3417 /* Main entry point of the constraint code: search the body of the
3418 current insn to choose the best alternative. It is mimicking insn
3419 alternative cost calculation model of former reload pass. That is
3420 because machine descriptions were written to use this model. This
3421 model can be changed in future. Make commutative operand exchange
3422 if it is chosen.
3424 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3425 constraints. Return true if any change happened during function
3426 call.
3428 If CHECK_ONLY_P is true then don't do any transformation. Just
3429 check that the insn satisfies all constraints. If the insn does
3430 not satisfy any constraint, return true. */
3431 static bool
3432 curr_insn_transform (bool check_only_p)
3434 int i, j, k;
3435 int n_operands;
3436 int n_alternatives;
3437 int commutative;
3438 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3439 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3440 rtx_insn *before, *after;
3441 bool alt_p = false;
3442 /* Flag that the insn has been changed through a transformation. */
3443 bool change_p;
3444 bool sec_mem_p;
3445 #ifdef SECONDARY_MEMORY_NEEDED
3446 bool use_sec_mem_p;
3447 #endif
3448 int max_regno_before;
3449 int reused_alternative_num;
3451 curr_insn_set = single_set (curr_insn);
3452 if (curr_insn_set != NULL_RTX && simple_move_p ())
3453 return false;
3455 no_input_reloads_p = no_output_reloads_p = false;
3456 goal_alt_number = -1;
3457 change_p = sec_mem_p = false;
3458 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3459 reloads; neither are insns that SET cc0. Insns that use CC0 are
3460 not allowed to have any input reloads. */
3461 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3462 no_output_reloads_p = true;
3464 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3465 no_input_reloads_p = true;
3466 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3467 no_output_reloads_p = true;
3469 n_operands = curr_static_id->n_operands;
3470 n_alternatives = curr_static_id->n_alternatives;
3472 /* Just return "no reloads" if insn has no operands with
3473 constraints. */
3474 if (n_operands == 0 || n_alternatives == 0)
3475 return false;
3477 max_regno_before = max_reg_num ();
3479 for (i = 0; i < n_operands; i++)
3481 goal_alt_matched[i][0] = -1;
3482 goal_alt_matches[i] = -1;
3485 commutative = curr_static_id->commutative;
3487 /* Now see what we need for pseudos that didn't get hard regs or got
3488 the wrong kind of hard reg. For this, we must consider all the
3489 operands together against the register constraints. */
3491 best_losers = best_overall = INT_MAX;
3492 best_reload_sum = 0;
3494 curr_swapped = false;
3495 goal_alt_swapped = false;
3497 if (! check_only_p)
3498 /* Make equivalence substitution and memory subreg elimination
3499 before address processing because an address legitimacy can
3500 depend on memory mode. */
3501 for (i = 0; i < n_operands; i++)
3503 rtx op, subst, old;
3504 bool op_change_p = false;
3506 if (curr_static_id->operand[i].is_operator)
3507 continue;
3509 old = op = *curr_id->operand_loc[i];
3510 if (GET_CODE (old) == SUBREG)
3511 old = SUBREG_REG (old);
3512 subst = get_equiv_with_elimination (old, curr_insn);
3513 original_subreg_reg_mode[i] = VOIDmode;
3514 equiv_substition_p[i] = false;
3515 if (subst != old)
3517 equiv_substition_p[i] = true;
3518 subst = copy_rtx (subst);
3519 lra_assert (REG_P (old));
3520 if (GET_CODE (op) != SUBREG)
3521 *curr_id->operand_loc[i] = subst;
3522 else
3524 SUBREG_REG (op) = subst;
3525 if (GET_MODE (subst) == VOIDmode)
3526 original_subreg_reg_mode[i] = GET_MODE (old);
3528 if (lra_dump_file != NULL)
3530 fprintf (lra_dump_file,
3531 "Changing pseudo %d in operand %i of insn %u on equiv ",
3532 REGNO (old), i, INSN_UID (curr_insn));
3533 dump_value_slim (lra_dump_file, subst, 1);
3534 fprintf (lra_dump_file, "\n");
3536 op_change_p = change_p = true;
3538 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3540 change_p = true;
3541 lra_update_dup (curr_id, i);
3545 /* Reload address registers and displacements. We do it before
3546 finding an alternative because of memory constraints. */
3547 before = after = NULL;
3548 for (i = 0; i < n_operands; i++)
3549 if (! curr_static_id->operand[i].is_operator
3550 && process_address (i, check_only_p, &before, &after))
3552 if (check_only_p)
3553 return true;
3554 change_p = true;
3555 lra_update_dup (curr_id, i);
3558 if (change_p)
3559 /* If we've changed the instruction then any alternative that
3560 we chose previously may no longer be valid. */
3561 lra_set_used_insn_alternative (curr_insn, -1);
3563 if (! check_only_p && curr_insn_set != NULL_RTX
3564 && check_and_process_move (&change_p, &sec_mem_p))
3565 return change_p;
3567 try_swapped:
3569 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3570 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3571 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3572 reused_alternative_num, INSN_UID (curr_insn));
3574 if (process_alt_operands (reused_alternative_num))
3575 alt_p = true;
3577 if (check_only_p)
3578 return ! alt_p || best_losers != 0;
3580 /* If insn is commutative (it's safe to exchange a certain pair of
3581 operands) then we need to try each alternative twice, the second
3582 time matching those two operands as if we had exchanged them. To
3583 do this, really exchange them in operands.
3585 If we have just tried the alternatives the second time, return
3586 operands to normal and drop through. */
3588 if (reused_alternative_num < 0 && commutative >= 0)
3590 curr_swapped = !curr_swapped;
3591 if (curr_swapped)
3593 swap_operands (commutative);
3594 goto try_swapped;
3596 else
3597 swap_operands (commutative);
3600 if (! alt_p && ! sec_mem_p)
3602 /* No alternative works with reloads?? */
3603 if (INSN_CODE (curr_insn) >= 0)
3604 fatal_insn ("unable to generate reloads for:", curr_insn);
3605 error_for_asm (curr_insn,
3606 "inconsistent operand constraints in an %<asm%>");
3607 /* Avoid further trouble with this insn. */
3608 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3609 lra_invalidate_insn_data (curr_insn);
3610 return true;
3613 /* If the best alternative is with operands 1 and 2 swapped, swap
3614 them. Update the operand numbers of any reloads already
3615 pushed. */
3617 if (goal_alt_swapped)
3619 if (lra_dump_file != NULL)
3620 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3621 INSN_UID (curr_insn));
3623 /* Swap the duplicates too. */
3624 swap_operands (commutative);
3625 change_p = true;
3628 #ifdef SECONDARY_MEMORY_NEEDED
3629 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3630 too conservatively. So we use the secondary memory only if there
3631 is no any alternative without reloads. */
3632 use_sec_mem_p = false;
3633 if (! alt_p)
3634 use_sec_mem_p = true;
3635 else if (sec_mem_p)
3637 for (i = 0; i < n_operands; i++)
3638 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3639 break;
3640 use_sec_mem_p = i < n_operands;
3643 if (use_sec_mem_p)
3645 int in = -1, out = -1;
3646 rtx new_reg, src, dest, rld;
3647 machine_mode sec_mode, rld_mode;
3649 lra_assert (curr_insn_set != NULL_RTX && sec_mem_p);
3650 dest = SET_DEST (curr_insn_set);
3651 src = SET_SRC (curr_insn_set);
3652 for (i = 0; i < n_operands; i++)
3653 if (*curr_id->operand_loc[i] == dest)
3654 out = i;
3655 else if (*curr_id->operand_loc[i] == src)
3656 in = i;
3657 for (i = 0; i < curr_static_id->n_dups; i++)
3658 if (out < 0 && *curr_id->dup_loc[i] == dest)
3659 out = curr_static_id->dup_num[i];
3660 else if (in < 0 && *curr_id->dup_loc[i] == src)
3661 in = curr_static_id->dup_num[i];
3662 lra_assert (out >= 0 && in >= 0
3663 && curr_static_id->operand[out].type == OP_OUT
3664 && curr_static_id->operand[in].type == OP_IN);
3665 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3666 ? dest : src);
3667 rld_mode = GET_MODE (rld);
3668 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3669 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3670 #else
3671 sec_mode = rld_mode;
3672 #endif
3673 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3674 NO_REGS, "secondary");
3675 /* If the mode is changed, it should be wider. */
3676 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3677 if (sec_mode != rld_mode)
3679 /* If the target says specifically to use another mode for
3680 secondary memory moves we can not reuse the original
3681 insn. */
3682 after = emit_spill_move (false, new_reg, dest);
3683 lra_process_new_insns (curr_insn, NULL, after,
3684 "Inserting the sec. move");
3685 /* We may have non null BEFORE here (e.g. after address
3686 processing. */
3687 push_to_sequence (before);
3688 before = emit_spill_move (true, new_reg, src);
3689 emit_insn (before);
3690 before = get_insns ();
3691 end_sequence ();
3692 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3693 lra_set_insn_deleted (curr_insn);
3695 else if (dest == rld)
3697 *curr_id->operand_loc[out] = new_reg;
3698 lra_update_dup (curr_id, out);
3699 after = emit_spill_move (false, new_reg, dest);
3700 lra_process_new_insns (curr_insn, NULL, after,
3701 "Inserting the sec. move");
3703 else
3705 *curr_id->operand_loc[in] = new_reg;
3706 lra_update_dup (curr_id, in);
3707 /* See comments above. */
3708 push_to_sequence (before);
3709 before = emit_spill_move (true, new_reg, src);
3710 emit_insn (before);
3711 before = get_insns ();
3712 end_sequence ();
3713 lra_process_new_insns (curr_insn, before, NULL,
3714 "Inserting the sec. move");
3716 lra_update_insn_regno_info (curr_insn);
3717 return true;
3719 #endif
3721 lra_assert (goal_alt_number >= 0);
3722 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3724 if (lra_dump_file != NULL)
3726 const char *p;
3728 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3729 goal_alt_number, INSN_UID (curr_insn));
3730 for (i = 0; i < n_operands; i++)
3732 p = (curr_static_id->operand_alternative
3733 [goal_alt_number * n_operands + i].constraint);
3734 if (*p == '\0')
3735 continue;
3736 fprintf (lra_dump_file, " (%d) ", i);
3737 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3738 fputc (*p, lra_dump_file);
3740 if (INSN_CODE (curr_insn) >= 0
3741 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3742 fprintf (lra_dump_file, " {%s}", p);
3743 if (curr_id->sp_offset != 0)
3744 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3745 curr_id->sp_offset);
3746 fprintf (lra_dump_file, "\n");
3749 /* Right now, for any pair of operands I and J that are required to
3750 match, with J < I, goal_alt_matches[I] is J. Add I to
3751 goal_alt_matched[J]. */
3753 for (i = 0; i < n_operands; i++)
3754 if ((j = goal_alt_matches[i]) >= 0)
3756 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3758 /* We allow matching one output operand and several input
3759 operands. */
3760 lra_assert (k == 0
3761 || (curr_static_id->operand[j].type == OP_OUT
3762 && curr_static_id->operand[i].type == OP_IN
3763 && (curr_static_id->operand
3764 [goal_alt_matched[j][0]].type == OP_IN)));
3765 goal_alt_matched[j][k] = i;
3766 goal_alt_matched[j][k + 1] = -1;
3769 for (i = 0; i < n_operands; i++)
3770 goal_alt_win[i] |= goal_alt_match_win[i];
3772 /* Any constants that aren't allowed and can't be reloaded into
3773 registers are here changed into memory references. */
3774 for (i = 0; i < n_operands; i++)
3775 if (goal_alt_win[i])
3777 int regno;
3778 enum reg_class new_class;
3779 rtx reg = *curr_id->operand_loc[i];
3781 if (GET_CODE (reg) == SUBREG)
3782 reg = SUBREG_REG (reg);
3784 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3786 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3788 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3790 lra_assert (ok_p);
3791 lra_change_class (regno, new_class, " Change to", true);
3795 else
3797 const char *constraint;
3798 char c;
3799 rtx op = *curr_id->operand_loc[i];
3800 rtx subreg = NULL_RTX;
3801 machine_mode mode = curr_operand_mode[i];
3803 if (GET_CODE (op) == SUBREG)
3805 subreg = op;
3806 op = SUBREG_REG (op);
3807 mode = GET_MODE (op);
3810 if (CONST_POOL_OK_P (mode, op)
3811 && ((targetm.preferred_reload_class
3812 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3813 || no_input_reloads_p))
3815 rtx tem = force_const_mem (mode, op);
3817 change_p = true;
3818 if (subreg != NULL_RTX)
3819 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3821 *curr_id->operand_loc[i] = tem;
3822 lra_update_dup (curr_id, i);
3823 process_address (i, false, &before, &after);
3825 /* If the alternative accepts constant pool refs directly
3826 there will be no reload needed at all. */
3827 if (subreg != NULL_RTX)
3828 continue;
3829 /* Skip alternatives before the one requested. */
3830 constraint = (curr_static_id->operand_alternative
3831 [goal_alt_number * n_operands + i].constraint);
3832 for (;
3833 (c = *constraint) && c != ',' && c != '#';
3834 constraint += CONSTRAINT_LEN (c, constraint))
3836 enum constraint_num cn = lookup_constraint (constraint);
3837 if ((insn_extra_memory_constraint (cn)
3838 || insn_extra_special_memory_constraint (cn))
3839 && satisfies_memory_constraint_p (tem, cn))
3840 break;
3842 if (c == '\0' || c == ',' || c == '#')
3843 continue;
3845 goal_alt_win[i] = true;
3849 for (i = 0; i < n_operands; i++)
3851 int regno;
3852 bool optional_p = false;
3853 rtx old, new_reg;
3854 rtx op = *curr_id->operand_loc[i];
3856 if (goal_alt_win[i])
3858 if (goal_alt[i] == NO_REGS
3859 && REG_P (op)
3860 /* When we assign NO_REGS it means that we will not
3861 assign a hard register to the scratch pseudo by
3862 assigment pass and the scratch pseudo will be
3863 spilled. Spilled scratch pseudos are transformed
3864 back to scratches at the LRA end. */
3865 && lra_former_scratch_operand_p (curr_insn, i)
3866 && lra_former_scratch_p (REGNO (op)))
3868 int regno = REGNO (op);
3869 lra_change_class (regno, NO_REGS, " Change to", true);
3870 if (lra_get_regno_hard_regno (regno) >= 0)
3871 /* We don't have to mark all insn affected by the
3872 spilled pseudo as there is only one such insn, the
3873 current one. */
3874 reg_renumber[regno] = -1;
3875 lra_assert (bitmap_single_bit_set_p
3876 (&lra_reg_info[REGNO (op)].insn_bitmap));
3878 /* We can do an optional reload. If the pseudo got a hard
3879 reg, we might improve the code through inheritance. If
3880 it does not get a hard register we coalesce memory/memory
3881 moves later. Ignore move insns to avoid cycling. */
3882 if (! lra_simple_p
3883 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3884 && goal_alt[i] != NO_REGS && REG_P (op)
3885 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3886 && regno < new_regno_start
3887 && ! lra_former_scratch_p (regno)
3888 && reg_renumber[regno] < 0
3889 /* Check that the optional reload pseudo will be able to
3890 hold given mode value. */
3891 && ! (prohibited_class_reg_set_mode_p
3892 (goal_alt[i], reg_class_contents[goal_alt[i]],
3893 PSEUDO_REGNO_MODE (regno)))
3894 && (curr_insn_set == NULL_RTX
3895 || !((REG_P (SET_SRC (curr_insn_set))
3896 || MEM_P (SET_SRC (curr_insn_set))
3897 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3898 && (REG_P (SET_DEST (curr_insn_set))
3899 || MEM_P (SET_DEST (curr_insn_set))
3900 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3901 optional_p = true;
3902 else
3903 continue;
3906 /* Operands that match previous ones have already been handled. */
3907 if (goal_alt_matches[i] >= 0)
3908 continue;
3910 /* We should not have an operand with a non-offsettable address
3911 appearing where an offsettable address will do. It also may
3912 be a case when the address should be special in other words
3913 not a general one (e.g. it needs no index reg). */
3914 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3916 enum reg_class rclass;
3917 rtx *loc = &XEXP (op, 0);
3918 enum rtx_code code = GET_CODE (*loc);
3920 push_to_sequence (before);
3921 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3922 MEM, SCRATCH);
3923 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3924 new_reg = emit_inc (rclass, *loc, *loc,
3925 /* This value does not matter for MODIFY. */
3926 GET_MODE_SIZE (GET_MODE (op)));
3927 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3928 "offsetable address", &new_reg))
3929 lra_emit_move (new_reg, *loc);
3930 before = get_insns ();
3931 end_sequence ();
3932 *loc = new_reg;
3933 lra_update_dup (curr_id, i);
3935 else if (goal_alt_matched[i][0] == -1)
3937 machine_mode mode;
3938 rtx reg, *loc;
3939 int hard_regno, byte;
3940 enum op_type type = curr_static_id->operand[i].type;
3942 loc = curr_id->operand_loc[i];
3943 mode = curr_operand_mode[i];
3944 if (GET_CODE (*loc) == SUBREG)
3946 reg = SUBREG_REG (*loc);
3947 byte = SUBREG_BYTE (*loc);
3948 if (REG_P (reg)
3949 /* Strict_low_part requires reload the register not
3950 the sub-register. */
3951 && (curr_static_id->operand[i].strict_low
3952 || (GET_MODE_SIZE (mode)
3953 <= GET_MODE_SIZE (GET_MODE (reg))
3954 && (hard_regno
3955 = get_try_hard_regno (REGNO (reg))) >= 0
3956 && (simplify_subreg_regno
3957 (hard_regno,
3958 GET_MODE (reg), byte, mode) < 0)
3959 && (goal_alt[i] == NO_REGS
3960 || (simplify_subreg_regno
3961 (ira_class_hard_regs[goal_alt[i]][0],
3962 GET_MODE (reg), byte, mode) >= 0)))))
3964 if (type == OP_OUT)
3965 type = OP_INOUT;
3966 loc = &SUBREG_REG (*loc);
3967 mode = GET_MODE (*loc);
3970 old = *loc;
3971 if (get_reload_reg (type, mode, old, goal_alt[i],
3972 loc != curr_id->operand_loc[i], "", &new_reg)
3973 && type != OP_OUT)
3975 push_to_sequence (before);
3976 lra_emit_move (new_reg, old);
3977 before = get_insns ();
3978 end_sequence ();
3980 *loc = new_reg;
3981 if (type != OP_IN
3982 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3984 start_sequence ();
3985 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3986 emit_insn (after);
3987 after = get_insns ();
3988 end_sequence ();
3989 *loc = new_reg;
3991 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3992 if (goal_alt_dont_inherit_ops[j] == i)
3994 lra_set_regno_unique_value (REGNO (new_reg));
3995 break;
3997 lra_update_dup (curr_id, i);
3999 else if (curr_static_id->operand[i].type == OP_IN
4000 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4001 == OP_OUT))
4003 /* generate reloads for input and matched outputs. */
4004 match_inputs[0] = i;
4005 match_inputs[1] = -1;
4006 match_reload (goal_alt_matched[i][0], match_inputs,
4007 goal_alt[i], &before, &after,
4008 curr_static_id->operand_alternative
4009 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
4010 .earlyclobber);
4012 else if (curr_static_id->operand[i].type == OP_OUT
4013 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4014 == OP_IN))
4015 /* Generate reloads for output and matched inputs. */
4016 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after,
4017 curr_static_id->operand_alternative
4018 [goal_alt_number * n_operands + i].earlyclobber);
4019 else if (curr_static_id->operand[i].type == OP_IN
4020 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4021 == OP_IN))
4023 /* Generate reloads for matched inputs. */
4024 match_inputs[0] = i;
4025 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
4026 match_inputs[j + 1] = k;
4027 match_inputs[j + 1] = -1;
4028 match_reload (-1, match_inputs, goal_alt[i], &before, &after, false);
4030 else
4031 /* We must generate code in any case when function
4032 process_alt_operands decides that it is possible. */
4033 gcc_unreachable ();
4034 if (optional_p)
4036 lra_assert (REG_P (op));
4037 regno = REGNO (op);
4038 op = *curr_id->operand_loc[i]; /* Substitution. */
4039 if (GET_CODE (op) == SUBREG)
4040 op = SUBREG_REG (op);
4041 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
4042 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
4043 lra_reg_info[REGNO (op)].restore_regno = regno;
4044 if (lra_dump_file != NULL)
4045 fprintf (lra_dump_file,
4046 " Making reload reg %d for reg %d optional\n",
4047 REGNO (op), regno);
4050 if (before != NULL_RTX || after != NULL_RTX
4051 || max_regno_before != max_reg_num ())
4052 change_p = true;
4053 if (change_p)
4055 lra_update_operator_dups (curr_id);
4056 /* Something changes -- process the insn. */
4057 lra_update_insn_regno_info (curr_insn);
4059 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
4060 return change_p;
4063 /* Return true if INSN satisfies all constraints. In other words, no
4064 reload insns are needed. */
4065 bool
4066 lra_constrain_insn (rtx_insn *insn)
4068 int saved_new_regno_start = new_regno_start;
4069 int saved_new_insn_uid_start = new_insn_uid_start;
4070 bool change_p;
4072 curr_insn = insn;
4073 curr_id = lra_get_insn_recog_data (curr_insn);
4074 curr_static_id = curr_id->insn_static_data;
4075 new_insn_uid_start = get_max_uid ();
4076 new_regno_start = max_reg_num ();
4077 change_p = curr_insn_transform (true);
4078 new_regno_start = saved_new_regno_start;
4079 new_insn_uid_start = saved_new_insn_uid_start;
4080 return ! change_p;
4083 /* Return true if X is in LIST. */
4084 static bool
4085 in_list_p (rtx x, rtx list)
4087 for (; list != NULL_RTX; list = XEXP (list, 1))
4088 if (XEXP (list, 0) == x)
4089 return true;
4090 return false;
4093 /* Return true if X contains an allocatable hard register (if
4094 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
4095 static bool
4096 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
4098 int i, j;
4099 const char *fmt;
4100 enum rtx_code code;
4102 code = GET_CODE (x);
4103 if (REG_P (x))
4105 int regno = REGNO (x);
4106 HARD_REG_SET alloc_regs;
4108 if (hard_reg_p)
4110 if (regno >= FIRST_PSEUDO_REGISTER)
4111 regno = lra_get_regno_hard_regno (regno);
4112 if (regno < 0)
4113 return false;
4114 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
4115 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
4117 else
4119 if (regno < FIRST_PSEUDO_REGISTER)
4120 return false;
4121 if (! spilled_p)
4122 return true;
4123 return lra_get_regno_hard_regno (regno) < 0;
4126 fmt = GET_RTX_FORMAT (code);
4127 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4129 if (fmt[i] == 'e')
4131 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4132 return true;
4134 else if (fmt[i] == 'E')
4136 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4137 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4138 return true;
4141 return false;
4144 /* Process all regs in location *LOC and change them on equivalent
4145 substitution. Return true if any change was done. */
4146 static bool
4147 loc_equivalence_change_p (rtx *loc)
4149 rtx subst, reg, x = *loc;
4150 bool result = false;
4151 enum rtx_code code = GET_CODE (x);
4152 const char *fmt;
4153 int i, j;
4155 if (code == SUBREG)
4157 reg = SUBREG_REG (x);
4158 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4159 && GET_MODE (subst) == VOIDmode)
4161 /* We cannot reload debug location. Simplify subreg here
4162 while we know the inner mode. */
4163 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4164 GET_MODE (reg), SUBREG_BYTE (x));
4165 return true;
4168 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4170 *loc = subst;
4171 return true;
4174 /* Scan all the operand sub-expressions. */
4175 fmt = GET_RTX_FORMAT (code);
4176 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4178 if (fmt[i] == 'e')
4179 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4180 else if (fmt[i] == 'E')
4181 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4182 result
4183 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4185 return result;
4188 /* Similar to loc_equivalence_change_p, but for use as
4189 simplify_replace_fn_rtx callback. DATA is insn for which the
4190 elimination is done. If it null we don't do the elimination. */
4191 static rtx
4192 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4194 if (!REG_P (loc))
4195 return NULL_RTX;
4197 rtx subst = (data == NULL
4198 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4199 if (subst != loc)
4200 return subst;
4202 return NULL_RTX;
4205 /* Maximum number of generated reload insns per an insn. It is for
4206 preventing this pass cycling in a bug case. */
4207 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4209 /* The current iteration number of this LRA pass. */
4210 int lra_constraint_iter;
4212 /* True if we substituted equiv which needs checking register
4213 allocation correctness because the equivalent value contains
4214 allocatable hard registers or when we restore multi-register
4215 pseudo. */
4216 bool lra_risky_transformations_p;
4218 /* Return true if REGNO is referenced in more than one block. */
4219 static bool
4220 multi_block_pseudo_p (int regno)
4222 basic_block bb = NULL;
4223 unsigned int uid;
4224 bitmap_iterator bi;
4226 if (regno < FIRST_PSEUDO_REGISTER)
4227 return false;
4229 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4230 if (bb == NULL)
4231 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4232 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4233 return true;
4234 return false;
4237 /* Return true if LIST contains a deleted insn. */
4238 static bool
4239 contains_deleted_insn_p (rtx_insn_list *list)
4241 for (; list != NULL_RTX; list = list->next ())
4242 if (NOTE_P (list->insn ())
4243 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4244 return true;
4245 return false;
4248 /* Return true if X contains a pseudo dying in INSN. */
4249 static bool
4250 dead_pseudo_p (rtx x, rtx_insn *insn)
4252 int i, j;
4253 const char *fmt;
4254 enum rtx_code code;
4256 if (REG_P (x))
4257 return (insn != NULL_RTX
4258 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4259 code = GET_CODE (x);
4260 fmt = GET_RTX_FORMAT (code);
4261 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4263 if (fmt[i] == 'e')
4265 if (dead_pseudo_p (XEXP (x, i), insn))
4266 return true;
4268 else if (fmt[i] == 'E')
4270 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4271 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4272 return true;
4275 return false;
4278 /* Return true if INSN contains a dying pseudo in INSN right hand
4279 side. */
4280 static bool
4281 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4283 rtx set = single_set (insn);
4285 gcc_assert (set != NULL);
4286 return dead_pseudo_p (SET_SRC (set), insn);
4289 /* Return true if any init insn of REGNO contains a dying pseudo in
4290 insn right hand side. */
4291 static bool
4292 init_insn_rhs_dead_pseudo_p (int regno)
4294 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4296 if (insns == NULL)
4297 return false;
4298 for (; insns != NULL_RTX; insns = insns->next ())
4299 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4300 return true;
4301 return false;
4304 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4305 reverse only if we have one init insn with given REGNO as a
4306 source. */
4307 static bool
4308 reverse_equiv_p (int regno)
4310 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4311 rtx set;
4313 if (insns == NULL)
4314 return false;
4315 if (! INSN_P (insns->insn ())
4316 || insns->next () != NULL)
4317 return false;
4318 if ((set = single_set (insns->insn ())) == NULL_RTX)
4319 return false;
4320 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4323 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4324 call this function only for non-reverse equivalence. */
4325 static bool
4326 contains_reloaded_insn_p (int regno)
4328 rtx set;
4329 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4331 for (; list != NULL; list = list->next ())
4332 if ((set = single_set (list->insn ())) == NULL_RTX
4333 || ! REG_P (SET_DEST (set))
4334 || (int) REGNO (SET_DEST (set)) != regno)
4335 return true;
4336 return false;
4339 /* Entry function of LRA constraint pass. Return true if the
4340 constraint pass did change the code. */
4341 bool
4342 lra_constraints (bool first_p)
4344 bool changed_p;
4345 int i, hard_regno, new_insns_num;
4346 unsigned int min_len, new_min_len, uid;
4347 rtx set, x, reg, dest_reg;
4348 basic_block last_bb;
4349 bitmap_head equiv_insn_bitmap;
4350 bitmap_iterator bi;
4352 lra_constraint_iter++;
4353 if (lra_dump_file != NULL)
4354 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4355 lra_constraint_iter);
4356 changed_p = false;
4357 if (pic_offset_table_rtx
4358 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4359 lra_risky_transformations_p = true;
4360 else
4361 lra_risky_transformations_p = false;
4362 new_insn_uid_start = get_max_uid ();
4363 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4364 /* Mark used hard regs for target stack size calulations. */
4365 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4366 if (lra_reg_info[i].nrefs != 0
4367 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4369 int j, nregs;
4371 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4372 for (j = 0; j < nregs; j++)
4373 df_set_regs_ever_live (hard_regno + j, true);
4375 /* Do elimination before the equivalence processing as we can spill
4376 some pseudos during elimination. */
4377 lra_eliminate (false, first_p);
4378 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4379 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4380 if (lra_reg_info[i].nrefs != 0)
4382 ira_reg_equiv[i].profitable_p = true;
4383 reg = regno_reg_rtx[i];
4384 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4386 bool pseudo_p = contains_reg_p (x, false, false);
4388 /* After RTL transformation, we can not guarantee that
4389 pseudo in the substitution was not reloaded which might
4390 make equivalence invalid. For example, in reverse
4391 equiv of p0
4393 p0 <- ...
4395 equiv_mem <- p0
4397 the memory address register was reloaded before the 2nd
4398 insn. */
4399 if ((! first_p && pseudo_p)
4400 /* We don't use DF for compilation speed sake. So it
4401 is problematic to update live info when we use an
4402 equivalence containing pseudos in more than one
4403 BB. */
4404 || (pseudo_p && multi_block_pseudo_p (i))
4405 /* If an init insn was deleted for some reason, cancel
4406 the equiv. We could update the equiv insns after
4407 transformations including an equiv insn deletion
4408 but it is not worthy as such cases are extremely
4409 rare. */
4410 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4411 /* If it is not a reverse equivalence, we check that a
4412 pseudo in rhs of the init insn is not dying in the
4413 insn. Otherwise, the live info at the beginning of
4414 the corresponding BB might be wrong after we
4415 removed the insn. When the equiv can be a
4416 constant, the right hand side of the init insn can
4417 be a pseudo. */
4418 || (! reverse_equiv_p (i)
4419 && (init_insn_rhs_dead_pseudo_p (i)
4420 /* If we reloaded the pseudo in an equivalence
4421 init insn, we can not remove the equiv init
4422 insns and the init insns might write into
4423 const memory in this case. */
4424 || contains_reloaded_insn_p (i)))
4425 /* Prevent access beyond equivalent memory for
4426 paradoxical subregs. */
4427 || (MEM_P (x)
4428 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4429 > GET_MODE_SIZE (GET_MODE (x))))
4430 || (pic_offset_table_rtx
4431 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4432 && (targetm.preferred_reload_class
4433 (x, lra_get_allocno_class (i)) == NO_REGS))
4434 || contains_symbol_ref_p (x))))
4435 ira_reg_equiv[i].defined_p = false;
4436 if (contains_reg_p (x, false, true))
4437 ira_reg_equiv[i].profitable_p = false;
4438 if (get_equiv (reg) != reg)
4439 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4442 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4443 update_equiv (i);
4444 /* We should add all insns containing pseudos which should be
4445 substituted by their equivalences. */
4446 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4447 lra_push_insn_by_uid (uid);
4448 min_len = lra_insn_stack_length ();
4449 new_insns_num = 0;
4450 last_bb = NULL;
4451 changed_p = false;
4452 while ((new_min_len = lra_insn_stack_length ()) != 0)
4454 curr_insn = lra_pop_insn ();
4455 --new_min_len;
4456 curr_bb = BLOCK_FOR_INSN (curr_insn);
4457 if (curr_bb != last_bb)
4459 last_bb = curr_bb;
4460 bb_reload_num = lra_curr_reload_num;
4462 if (min_len > new_min_len)
4464 min_len = new_min_len;
4465 new_insns_num = 0;
4467 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4468 internal_error
4469 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4470 MAX_RELOAD_INSNS_NUMBER);
4471 new_insns_num++;
4472 if (DEBUG_INSN_P (curr_insn))
4474 /* We need to check equivalence in debug insn and change
4475 pseudo to the equivalent value if necessary. */
4476 curr_id = lra_get_insn_recog_data (curr_insn);
4477 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4479 rtx old = *curr_id->operand_loc[0];
4480 *curr_id->operand_loc[0]
4481 = simplify_replace_fn_rtx (old, NULL_RTX,
4482 loc_equivalence_callback, curr_insn);
4483 if (old != *curr_id->operand_loc[0])
4485 lra_update_insn_regno_info (curr_insn);
4486 changed_p = true;
4490 else if (INSN_P (curr_insn))
4492 if ((set = single_set (curr_insn)) != NULL_RTX)
4494 dest_reg = SET_DEST (set);
4495 /* The equivalence pseudo could be set up as SUBREG in a
4496 case when it is a call restore insn in a mode
4497 different from the pseudo mode. */
4498 if (GET_CODE (dest_reg) == SUBREG)
4499 dest_reg = SUBREG_REG (dest_reg);
4500 if ((REG_P (dest_reg)
4501 && (x = get_equiv (dest_reg)) != dest_reg
4502 /* Remove insns which set up a pseudo whose value
4503 can not be changed. Such insns might be not in
4504 init_insns because we don't update equiv data
4505 during insn transformations.
4507 As an example, let suppose that a pseudo got
4508 hard register and on the 1st pass was not
4509 changed to equivalent constant. We generate an
4510 additional insn setting up the pseudo because of
4511 secondary memory movement. Then the pseudo is
4512 spilled and we use the equiv constant. In this
4513 case we should remove the additional insn and
4514 this insn is not init_insns list. */
4515 && (! MEM_P (x) || MEM_READONLY_P (x)
4516 /* Check that this is actually an insn setting
4517 up the equivalence. */
4518 || in_list_p (curr_insn,
4519 ira_reg_equiv
4520 [REGNO (dest_reg)].init_insns)))
4521 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4522 && in_list_p (curr_insn,
4523 ira_reg_equiv
4524 [REGNO (SET_SRC (set))].init_insns)))
4526 /* This is equiv init insn of pseudo which did not get a
4527 hard register -- remove the insn. */
4528 if (lra_dump_file != NULL)
4530 fprintf (lra_dump_file,
4531 " Removing equiv init insn %i (freq=%d)\n",
4532 INSN_UID (curr_insn),
4533 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4534 dump_insn_slim (lra_dump_file, curr_insn);
4536 if (contains_reg_p (x, true, false))
4537 lra_risky_transformations_p = true;
4538 lra_set_insn_deleted (curr_insn);
4539 continue;
4542 curr_id = lra_get_insn_recog_data (curr_insn);
4543 curr_static_id = curr_id->insn_static_data;
4544 init_curr_insn_input_reloads ();
4545 init_curr_operand_mode ();
4546 if (curr_insn_transform (false))
4547 changed_p = true;
4548 /* Check non-transformed insns too for equiv change as USE
4549 or CLOBBER don't need reloads but can contain pseudos
4550 being changed on their equivalences. */
4551 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4552 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4554 lra_update_insn_regno_info (curr_insn);
4555 changed_p = true;
4559 bitmap_clear (&equiv_insn_bitmap);
4560 /* If we used a new hard regno, changed_p should be true because the
4561 hard reg is assigned to a new pseudo. */
4562 if (flag_checking && !changed_p)
4564 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4565 if (lra_reg_info[i].nrefs != 0
4566 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4568 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4570 for (j = 0; j < nregs; j++)
4571 lra_assert (df_regs_ever_live_p (hard_regno + j));
4574 return changed_p;
4577 /* Initiate the LRA constraint pass. It is done once per
4578 function. */
4579 void
4580 lra_constraints_init (void)
4584 /* Finalize the LRA constraint pass. It is done once per
4585 function. */
4586 void
4587 lra_constraints_finish (void)
4593 /* This page contains code to do inheritance/split
4594 transformations. */
4596 /* Number of reloads passed so far in current EBB. */
4597 static int reloads_num;
4599 /* Number of calls passed so far in current EBB. */
4600 static int calls_num;
4602 /* Current reload pseudo check for validity of elements in
4603 USAGE_INSNS. */
4604 static int curr_usage_insns_check;
4606 /* Info about last usage of registers in EBB to do inheritance/split
4607 transformation. Inheritance transformation is done from a spilled
4608 pseudo and split transformations from a hard register or a pseudo
4609 assigned to a hard register. */
4610 struct usage_insns
4612 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4613 value INSNS is valid. The insns is chain of optional debug insns
4614 and a finishing non-debug insn using the corresponding reg. The
4615 value is also used to mark the registers which are set up in the
4616 current insn. The negated insn uid is used for this. */
4617 int check;
4618 /* Value of global reloads_num at the last insn in INSNS. */
4619 int reloads_num;
4620 /* Value of global reloads_nums at the last insn in INSNS. */
4621 int calls_num;
4622 /* It can be true only for splitting. And it means that the restore
4623 insn should be put after insn given by the following member. */
4624 bool after_p;
4625 /* Next insns in the current EBB which use the original reg and the
4626 original reg value is not changed between the current insn and
4627 the next insns. In order words, e.g. for inheritance, if we need
4628 to use the original reg value again in the next insns we can try
4629 to use the value in a hard register from a reload insn of the
4630 current insn. */
4631 rtx insns;
4634 /* Map: regno -> corresponding pseudo usage insns. */
4635 static struct usage_insns *usage_insns;
4637 static void
4638 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4640 usage_insns[regno].check = curr_usage_insns_check;
4641 usage_insns[regno].insns = insn;
4642 usage_insns[regno].reloads_num = reloads_num;
4643 usage_insns[regno].calls_num = calls_num;
4644 usage_insns[regno].after_p = after_p;
4647 /* The function is used to form list REGNO usages which consists of
4648 optional debug insns finished by a non-debug insn using REGNO.
4649 RELOADS_NUM is current number of reload insns processed so far. */
4650 static void
4651 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
4653 rtx next_usage_insns;
4655 if (usage_insns[regno].check == curr_usage_insns_check
4656 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4657 && DEBUG_INSN_P (insn))
4659 /* Check that we did not add the debug insn yet. */
4660 if (next_usage_insns != insn
4661 && (GET_CODE (next_usage_insns) != INSN_LIST
4662 || XEXP (next_usage_insns, 0) != insn))
4663 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4664 next_usage_insns);
4666 else if (NONDEBUG_INSN_P (insn))
4667 setup_next_usage_insn (regno, insn, reloads_num, false);
4668 else
4669 usage_insns[regno].check = 0;
4672 /* Return first non-debug insn in list USAGE_INSNS. */
4673 static rtx_insn *
4674 skip_usage_debug_insns (rtx usage_insns)
4676 rtx insn;
4678 /* Skip debug insns. */
4679 for (insn = usage_insns;
4680 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4681 insn = XEXP (insn, 1))
4683 return safe_as_a <rtx_insn *> (insn);
4686 /* Return true if we need secondary memory moves for insn in
4687 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4688 into the insn. */
4689 static bool
4690 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4691 rtx usage_insns ATTRIBUTE_UNUSED)
4693 #ifndef SECONDARY_MEMORY_NEEDED
4694 return false;
4695 #else
4696 rtx_insn *insn;
4697 rtx set, dest;
4698 enum reg_class cl;
4700 if (inher_cl == ALL_REGS
4701 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4702 return false;
4703 lra_assert (INSN_P (insn));
4704 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4705 return false;
4706 dest = SET_DEST (set);
4707 if (! REG_P (dest))
4708 return false;
4709 lra_assert (inher_cl != NO_REGS);
4710 cl = get_reg_class (REGNO (dest));
4711 return (cl != NO_REGS && cl != ALL_REGS
4712 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4713 #endif
4716 /* Registers involved in inheritance/split in the current EBB
4717 (inheritance/split pseudos and original registers). */
4718 static bitmap_head check_only_regs;
4720 /* Do inheritance transformations for insn INSN, which defines (if
4721 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4722 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4723 form as the "insns" field of usage_insns. Return true if we
4724 succeed in such transformation.
4726 The transformations look like:
4728 p <- ... i <- ...
4729 ... p <- i (new insn)
4730 ... =>
4731 <- ... p ... <- ... i ...
4733 ... i <- p (new insn)
4734 <- ... p ... <- ... i ...
4735 ... =>
4736 <- ... p ... <- ... i ...
4737 where p is a spilled original pseudo and i is a new inheritance pseudo.
4740 The inheritance pseudo has the smallest class of two classes CL and
4741 class of ORIGINAL REGNO. */
4742 static bool
4743 inherit_reload_reg (bool def_p, int original_regno,
4744 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4746 if (optimize_function_for_size_p (cfun))
4747 return false;
4749 enum reg_class rclass = lra_get_allocno_class (original_regno);
4750 rtx original_reg = regno_reg_rtx[original_regno];
4751 rtx new_reg, usage_insn;
4752 rtx_insn *new_insns;
4754 lra_assert (! usage_insns[original_regno].after_p);
4755 if (lra_dump_file != NULL)
4756 fprintf (lra_dump_file,
4757 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4758 if (! ira_reg_classes_intersect_p[cl][rclass])
4760 if (lra_dump_file != NULL)
4762 fprintf (lra_dump_file,
4763 " Rejecting inheritance for %d "
4764 "because of disjoint classes %s and %s\n",
4765 original_regno, reg_class_names[cl],
4766 reg_class_names[rclass]);
4767 fprintf (lra_dump_file,
4768 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4770 return false;
4772 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4773 /* We don't use a subset of two classes because it can be
4774 NO_REGS. This transformation is still profitable in most
4775 cases even if the classes are not intersected as register
4776 move is probably cheaper than a memory load. */
4777 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4779 if (lra_dump_file != NULL)
4780 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4781 reg_class_names[cl], reg_class_names[rclass]);
4783 rclass = cl;
4785 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4787 /* Reject inheritance resulting in secondary memory moves.
4788 Otherwise, there is a danger in LRA cycling. Also such
4789 transformation will be unprofitable. */
4790 if (lra_dump_file != NULL)
4792 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4793 rtx set = single_set (insn);
4795 lra_assert (set != NULL_RTX);
4797 rtx dest = SET_DEST (set);
4799 lra_assert (REG_P (dest));
4800 fprintf (lra_dump_file,
4801 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4802 "as secondary mem is needed\n",
4803 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4804 original_regno, reg_class_names[rclass]);
4805 fprintf (lra_dump_file,
4806 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4808 return false;
4810 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4811 rclass, "inheritance");
4812 start_sequence ();
4813 if (def_p)
4814 lra_emit_move (original_reg, new_reg);
4815 else
4816 lra_emit_move (new_reg, original_reg);
4817 new_insns = get_insns ();
4818 end_sequence ();
4819 if (NEXT_INSN (new_insns) != NULL_RTX)
4821 if (lra_dump_file != NULL)
4823 fprintf (lra_dump_file,
4824 " Rejecting inheritance %d->%d "
4825 "as it results in 2 or more insns:\n",
4826 original_regno, REGNO (new_reg));
4827 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4828 fprintf (lra_dump_file,
4829 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4831 return false;
4833 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
4834 lra_update_insn_regno_info (insn);
4835 if (! def_p)
4836 /* We now have a new usage insn for original regno. */
4837 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4838 if (lra_dump_file != NULL)
4839 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4840 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4841 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4842 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4843 bitmap_set_bit (&check_only_regs, original_regno);
4844 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4845 if (def_p)
4846 lra_process_new_insns (insn, NULL, new_insns,
4847 "Add original<-inheritance");
4848 else
4849 lra_process_new_insns (insn, new_insns, NULL,
4850 "Add inheritance<-original");
4851 while (next_usage_insns != NULL_RTX)
4853 if (GET_CODE (next_usage_insns) != INSN_LIST)
4855 usage_insn = next_usage_insns;
4856 lra_assert (NONDEBUG_INSN_P (usage_insn));
4857 next_usage_insns = NULL;
4859 else
4861 usage_insn = XEXP (next_usage_insns, 0);
4862 lra_assert (DEBUG_INSN_P (usage_insn));
4863 next_usage_insns = XEXP (next_usage_insns, 1);
4865 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
4866 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4867 if (lra_dump_file != NULL)
4869 fprintf (lra_dump_file,
4870 " Inheritance reuse change %d->%d (bb%d):\n",
4871 original_regno, REGNO (new_reg),
4872 BLOCK_FOR_INSN (usage_insn)->index);
4873 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
4876 if (lra_dump_file != NULL)
4877 fprintf (lra_dump_file,
4878 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4879 return true;
4882 /* Return true if we need a caller save/restore for pseudo REGNO which
4883 was assigned to a hard register. */
4884 static inline bool
4885 need_for_call_save_p (int regno)
4887 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4888 return (usage_insns[regno].calls_num < calls_num
4889 && (overlaps_hard_reg_set_p
4890 ((flag_ipa_ra &&
4891 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4892 ? lra_reg_info[regno].actual_call_used_reg_set
4893 : call_used_reg_set,
4894 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4895 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4896 PSEUDO_REGNO_MODE (regno))));
4899 /* Global registers occurring in the current EBB. */
4900 static bitmap_head ebb_global_regs;
4902 /* Return true if we need a split for hard register REGNO or pseudo
4903 REGNO which was assigned to a hard register.
4904 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4905 used for reloads since the EBB end. It is an approximation of the
4906 used hard registers in the split range. The exact value would
4907 require expensive calculations. If we were aggressive with
4908 splitting because of the approximation, the split pseudo will save
4909 the same hard register assignment and will be removed in the undo
4910 pass. We still need the approximation because too aggressive
4911 splitting would result in too inaccurate cost calculation in the
4912 assignment pass because of too many generated moves which will be
4913 probably removed in the undo pass. */
4914 static inline bool
4915 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4917 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4919 lra_assert (hard_regno >= 0);
4920 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4921 /* Don't split eliminable hard registers, otherwise we can
4922 split hard registers like hard frame pointer, which
4923 lives on BB start/end according to DF-infrastructure,
4924 when there is a pseudo assigned to the register and
4925 living in the same BB. */
4926 && (regno >= FIRST_PSEUDO_REGISTER
4927 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4928 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4929 /* Don't split call clobbered hard regs living through
4930 calls, otherwise we might have a check problem in the
4931 assign sub-pass as in the most cases (exception is a
4932 situation when lra_risky_transformations_p value is
4933 true) the assign pass assumes that all pseudos living
4934 through calls are assigned to call saved hard regs. */
4935 && (regno >= FIRST_PSEUDO_REGISTER
4936 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4937 || usage_insns[regno].calls_num == calls_num)
4938 /* We need at least 2 reloads to make pseudo splitting
4939 profitable. We should provide hard regno splitting in
4940 any case to solve 1st insn scheduling problem when
4941 moving hard register definition up might result in
4942 impossibility to find hard register for reload pseudo of
4943 small register class. */
4944 && (usage_insns[regno].reloads_num
4945 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4946 && (regno < FIRST_PSEUDO_REGISTER
4947 /* For short living pseudos, spilling + inheritance can
4948 be considered a substitution for splitting.
4949 Therefore we do not splitting for local pseudos. It
4950 decreases also aggressiveness of splitting. The
4951 minimal number of references is chosen taking into
4952 account that for 2 references splitting has no sense
4953 as we can just spill the pseudo. */
4954 || (regno >= FIRST_PSEUDO_REGISTER
4955 && lra_reg_info[regno].nrefs > 3
4956 && bitmap_bit_p (&ebb_global_regs, regno))))
4957 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4960 /* Return class for the split pseudo created from original pseudo with
4961 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4962 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4963 results in no secondary memory movements. */
4964 static enum reg_class
4965 choose_split_class (enum reg_class allocno_class,
4966 int hard_regno ATTRIBUTE_UNUSED,
4967 machine_mode mode ATTRIBUTE_UNUSED)
4969 #ifndef SECONDARY_MEMORY_NEEDED
4970 return allocno_class;
4971 #else
4972 int i;
4973 enum reg_class cl, best_cl = NO_REGS;
4974 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4975 = REGNO_REG_CLASS (hard_regno);
4977 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4978 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4979 return allocno_class;
4980 for (i = 0;
4981 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4982 i++)
4983 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4984 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4985 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4986 && (best_cl == NO_REGS
4987 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4988 best_cl = cl;
4989 return best_cl;
4990 #endif
4993 /* Do split transformations for insn INSN, which defines or uses
4994 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4995 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4996 "insns" field of usage_insns.
4998 The transformations look like:
5000 p <- ... p <- ...
5001 ... s <- p (new insn -- save)
5002 ... =>
5003 ... p <- s (new insn -- restore)
5004 <- ... p ... <- ... p ...
5006 <- ... p ... <- ... p ...
5007 ... s <- p (new insn -- save)
5008 ... =>
5009 ... p <- s (new insn -- restore)
5010 <- ... p ... <- ... p ...
5012 where p is an original pseudo got a hard register or a hard
5013 register and s is a new split pseudo. The save is put before INSN
5014 if BEFORE_P is true. Return true if we succeed in such
5015 transformation. */
5016 static bool
5017 split_reg (bool before_p, int original_regno, rtx_insn *insn,
5018 rtx next_usage_insns)
5020 enum reg_class rclass;
5021 rtx original_reg;
5022 int hard_regno, nregs;
5023 rtx new_reg, usage_insn;
5024 rtx_insn *restore, *save;
5025 bool after_p;
5026 bool call_save_p;
5027 machine_mode mode;
5029 if (original_regno < FIRST_PSEUDO_REGISTER)
5031 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
5032 hard_regno = original_regno;
5033 call_save_p = false;
5034 nregs = 1;
5035 mode = lra_reg_info[hard_regno].biggest_mode;
5036 machine_mode reg_rtx_mode = GET_MODE (regno_reg_rtx[hard_regno]);
5037 /* A reg can have a biggest_mode of VOIDmode if it was only ever seen
5038 as part of a multi-word register. In that case, or if the biggest
5039 mode was larger than a register, just use the reg_rtx. Otherwise,
5040 limit the size to that of the biggest access in the function. */
5041 if (mode == VOIDmode
5042 || GET_MODE_SIZE (mode) > GET_MODE_SIZE (reg_rtx_mode))
5044 original_reg = regno_reg_rtx[hard_regno];
5045 mode = reg_rtx_mode;
5047 else
5048 original_reg = gen_rtx_REG (mode, hard_regno);
5050 else
5052 mode = PSEUDO_REGNO_MODE (original_regno);
5053 hard_regno = reg_renumber[original_regno];
5054 nregs = hard_regno_nregs[hard_regno][mode];
5055 rclass = lra_get_allocno_class (original_regno);
5056 original_reg = regno_reg_rtx[original_regno];
5057 call_save_p = need_for_call_save_p (original_regno);
5059 lra_assert (hard_regno >= 0);
5060 if (lra_dump_file != NULL)
5061 fprintf (lra_dump_file,
5062 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
5064 if (call_save_p)
5066 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
5067 hard_regno_nregs[hard_regno][mode],
5068 mode);
5069 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
5071 else
5073 rclass = choose_split_class (rclass, hard_regno, mode);
5074 if (rclass == NO_REGS)
5076 if (lra_dump_file != NULL)
5078 fprintf (lra_dump_file,
5079 " Rejecting split of %d(%s): "
5080 "no good reg class for %d(%s)\n",
5081 original_regno,
5082 reg_class_names[lra_get_allocno_class (original_regno)],
5083 hard_regno,
5084 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
5085 fprintf
5086 (lra_dump_file,
5087 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5089 return false;
5091 new_reg = lra_create_new_reg (mode, original_reg, rclass, "split");
5092 reg_renumber[REGNO (new_reg)] = hard_regno;
5094 save = emit_spill_move (true, new_reg, original_reg);
5095 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
5097 if (lra_dump_file != NULL)
5099 fprintf
5100 (lra_dump_file,
5101 " Rejecting split %d->%d resulting in > 2 save insns:\n",
5102 original_regno, REGNO (new_reg));
5103 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
5104 fprintf (lra_dump_file,
5105 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5107 return false;
5109 restore = emit_spill_move (false, new_reg, original_reg);
5110 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
5112 if (lra_dump_file != NULL)
5114 fprintf (lra_dump_file,
5115 " Rejecting split %d->%d "
5116 "resulting in > 2 restore insns:\n",
5117 original_regno, REGNO (new_reg));
5118 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
5119 fprintf (lra_dump_file,
5120 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5122 return false;
5124 after_p = usage_insns[original_regno].after_p;
5125 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
5126 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5127 bitmap_set_bit (&check_only_regs, original_regno);
5128 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
5129 for (;;)
5131 if (GET_CODE (next_usage_insns) != INSN_LIST)
5133 usage_insn = next_usage_insns;
5134 break;
5136 usage_insn = XEXP (next_usage_insns, 0);
5137 lra_assert (DEBUG_INSN_P (usage_insn));
5138 next_usage_insns = XEXP (next_usage_insns, 1);
5139 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5140 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5141 if (lra_dump_file != NULL)
5143 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5144 original_regno, REGNO (new_reg));
5145 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5148 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5149 lra_assert (usage_insn != insn || (after_p && before_p));
5150 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5151 after_p ? NULL : restore,
5152 after_p ? restore : NULL,
5153 call_save_p
5154 ? "Add reg<-save" : "Add reg<-split");
5155 lra_process_new_insns (insn, before_p ? save : NULL,
5156 before_p ? NULL : save,
5157 call_save_p
5158 ? "Add save<-reg" : "Add split<-reg");
5159 if (nregs > 1)
5160 /* If we are trying to split multi-register. We should check
5161 conflicts on the next assignment sub-pass. IRA can allocate on
5162 sub-register levels, LRA do this on pseudos level right now and
5163 this discrepancy may create allocation conflicts after
5164 splitting. */
5165 lra_risky_transformations_p = true;
5166 if (lra_dump_file != NULL)
5167 fprintf (lra_dump_file,
5168 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5169 return true;
5172 /* Recognize that we need a split transformation for insn INSN, which
5173 defines or uses REGNO in its insn biggest MODE (we use it only if
5174 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5175 hard registers which might be used for reloads since the EBB end.
5176 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5177 uid before starting INSN processing. Return true if we succeed in
5178 such transformation. */
5179 static bool
5180 split_if_necessary (int regno, machine_mode mode,
5181 HARD_REG_SET potential_reload_hard_regs,
5182 bool before_p, rtx_insn *insn, int max_uid)
5184 bool res = false;
5185 int i, nregs = 1;
5186 rtx next_usage_insns;
5188 if (regno < FIRST_PSEUDO_REGISTER)
5189 nregs = hard_regno_nregs[regno][mode];
5190 for (i = 0; i < nregs; i++)
5191 if (usage_insns[regno + i].check == curr_usage_insns_check
5192 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5193 /* To avoid processing the register twice or more. */
5194 && ((GET_CODE (next_usage_insns) != INSN_LIST
5195 && INSN_UID (next_usage_insns) < max_uid)
5196 || (GET_CODE (next_usage_insns) == INSN_LIST
5197 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5198 && need_for_split_p (potential_reload_hard_regs, regno + i)
5199 && split_reg (before_p, regno + i, insn, next_usage_insns))
5200 res = true;
5201 return res;
5204 /* Check only registers living at the current program point in the
5205 current EBB. */
5206 static bitmap_head live_regs;
5208 /* Update live info in EBB given by its HEAD and TAIL insns after
5209 inheritance/split transformation. The function removes dead moves
5210 too. */
5211 static void
5212 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5214 unsigned int j;
5215 int i, regno;
5216 bool live_p;
5217 rtx_insn *prev_insn;
5218 rtx set;
5219 bool remove_p;
5220 basic_block last_bb, prev_bb, curr_bb;
5221 bitmap_iterator bi;
5222 struct lra_insn_reg *reg;
5223 edge e;
5224 edge_iterator ei;
5226 last_bb = BLOCK_FOR_INSN (tail);
5227 prev_bb = NULL;
5228 for (curr_insn = tail;
5229 curr_insn != PREV_INSN (head);
5230 curr_insn = prev_insn)
5232 prev_insn = PREV_INSN (curr_insn);
5233 /* We need to process empty blocks too. They contain
5234 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5235 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5236 continue;
5237 curr_bb = BLOCK_FOR_INSN (curr_insn);
5238 if (curr_bb != prev_bb)
5240 if (prev_bb != NULL)
5242 /* Update df_get_live_in (prev_bb): */
5243 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5244 if (bitmap_bit_p (&live_regs, j))
5245 bitmap_set_bit (df_get_live_in (prev_bb), j);
5246 else
5247 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5249 if (curr_bb != last_bb)
5251 /* Update df_get_live_out (curr_bb): */
5252 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5254 live_p = bitmap_bit_p (&live_regs, j);
5255 if (! live_p)
5256 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5257 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5259 live_p = true;
5260 break;
5262 if (live_p)
5263 bitmap_set_bit (df_get_live_out (curr_bb), j);
5264 else
5265 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5268 prev_bb = curr_bb;
5269 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5271 if (! NONDEBUG_INSN_P (curr_insn))
5272 continue;
5273 curr_id = lra_get_insn_recog_data (curr_insn);
5274 curr_static_id = curr_id->insn_static_data;
5275 remove_p = false;
5276 if ((set = single_set (curr_insn)) != NULL_RTX
5277 && REG_P (SET_DEST (set))
5278 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5279 && SET_DEST (set) != pic_offset_table_rtx
5280 && bitmap_bit_p (&check_only_regs, regno)
5281 && ! bitmap_bit_p (&live_regs, regno))
5282 remove_p = true;
5283 /* See which defined values die here. */
5284 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5285 if (reg->type == OP_OUT && ! reg->subreg_p)
5286 bitmap_clear_bit (&live_regs, reg->regno);
5287 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5288 if (reg->type == OP_OUT && ! reg->subreg_p)
5289 bitmap_clear_bit (&live_regs, reg->regno);
5290 if (curr_id->arg_hard_regs != NULL)
5291 /* Make clobbered argument hard registers die. */
5292 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5293 if (regno >= FIRST_PSEUDO_REGISTER)
5294 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
5295 /* Mark each used value as live. */
5296 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5297 if (reg->type != OP_OUT
5298 && bitmap_bit_p (&check_only_regs, reg->regno))
5299 bitmap_set_bit (&live_regs, reg->regno);
5300 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5301 if (reg->type != OP_OUT
5302 && bitmap_bit_p (&check_only_regs, reg->regno))
5303 bitmap_set_bit (&live_regs, reg->regno);
5304 if (curr_id->arg_hard_regs != NULL)
5305 /* Make used argument hard registers live. */
5306 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5307 if (regno < FIRST_PSEUDO_REGISTER
5308 && bitmap_bit_p (&check_only_regs, regno))
5309 bitmap_set_bit (&live_regs, regno);
5310 /* It is quite important to remove dead move insns because it
5311 means removing dead store. We don't need to process them for
5312 constraints. */
5313 if (remove_p)
5315 if (lra_dump_file != NULL)
5317 fprintf (lra_dump_file, " Removing dead insn:\n ");
5318 dump_insn_slim (lra_dump_file, curr_insn);
5320 lra_set_insn_deleted (curr_insn);
5325 /* The structure describes info to do an inheritance for the current
5326 insn. We need to collect such info first before doing the
5327 transformations because the transformations change the insn
5328 internal representation. */
5329 struct to_inherit
5331 /* Original regno. */
5332 int regno;
5333 /* Subsequent insns which can inherit original reg value. */
5334 rtx insns;
5337 /* Array containing all info for doing inheritance from the current
5338 insn. */
5339 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5341 /* Number elements in the previous array. */
5342 static int to_inherit_num;
5344 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5345 structure to_inherit. */
5346 static void
5347 add_to_inherit (int regno, rtx insns)
5349 int i;
5351 for (i = 0; i < to_inherit_num; i++)
5352 if (to_inherit[i].regno == regno)
5353 return;
5354 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5355 to_inherit[to_inherit_num].regno = regno;
5356 to_inherit[to_inherit_num++].insns = insns;
5359 /* Return the last non-debug insn in basic block BB, or the block begin
5360 note if none. */
5361 static rtx_insn *
5362 get_last_insertion_point (basic_block bb)
5364 rtx_insn *insn;
5366 FOR_BB_INSNS_REVERSE (bb, insn)
5367 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5368 return insn;
5369 gcc_unreachable ();
5372 /* Set up RES by registers living on edges FROM except the edge (FROM,
5373 TO) or by registers set up in a jump insn in BB FROM. */
5374 static void
5375 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5377 rtx_insn *last;
5378 struct lra_insn_reg *reg;
5379 edge e;
5380 edge_iterator ei;
5382 lra_assert (to != NULL);
5383 bitmap_clear (res);
5384 FOR_EACH_EDGE (e, ei, from->succs)
5385 if (e->dest != to)
5386 bitmap_ior_into (res, df_get_live_in (e->dest));
5387 last = get_last_insertion_point (from);
5388 if (! JUMP_P (last))
5389 return;
5390 curr_id = lra_get_insn_recog_data (last);
5391 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5392 if (reg->type != OP_IN)
5393 bitmap_set_bit (res, reg->regno);
5396 /* Used as a temporary results of some bitmap calculations. */
5397 static bitmap_head temp_bitmap;
5399 /* We split for reloads of small class of hard regs. The following
5400 defines how many hard regs the class should have to be qualified as
5401 small. The code is mostly oriented to x86/x86-64 architecture
5402 where some insns need to use only specific register or pair of
5403 registers and these register can live in RTL explicitly, e.g. for
5404 parameter passing. */
5405 static const int max_small_class_regs_num = 2;
5407 /* Do inheritance/split transformations in EBB starting with HEAD and
5408 finishing on TAIL. We process EBB insns in the reverse order.
5409 Return true if we did any inheritance/split transformation in the
5410 EBB.
5412 We should avoid excessive splitting which results in worse code
5413 because of inaccurate cost calculations for spilling new split
5414 pseudos in such case. To achieve this we do splitting only if
5415 register pressure is high in given basic block and there are reload
5416 pseudos requiring hard registers. We could do more register
5417 pressure calculations at any given program point to avoid necessary
5418 splitting even more but it is to expensive and the current approach
5419 works well enough. */
5420 static bool
5421 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5423 int i, src_regno, dst_regno, nregs;
5424 bool change_p, succ_p, update_reloads_num_p;
5425 rtx_insn *prev_insn, *last_insn;
5426 rtx next_usage_insns, set;
5427 enum reg_class cl;
5428 struct lra_insn_reg *reg;
5429 basic_block last_processed_bb, curr_bb = NULL;
5430 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5431 bitmap to_process;
5432 unsigned int j;
5433 bitmap_iterator bi;
5434 bool head_p, after_p;
5436 change_p = false;
5437 curr_usage_insns_check++;
5438 reloads_num = calls_num = 0;
5439 bitmap_clear (&check_only_regs);
5440 last_processed_bb = NULL;
5441 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5442 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5443 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5444 /* We don't process new insns generated in the loop. */
5445 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5447 prev_insn = PREV_INSN (curr_insn);
5448 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5449 curr_bb = BLOCK_FOR_INSN (curr_insn);
5450 if (last_processed_bb != curr_bb)
5452 /* We are at the end of BB. Add qualified living
5453 pseudos for potential splitting. */
5454 to_process = df_get_live_out (curr_bb);
5455 if (last_processed_bb != NULL)
5457 /* We are somewhere in the middle of EBB. */
5458 get_live_on_other_edges (curr_bb, last_processed_bb,
5459 &temp_bitmap);
5460 to_process = &temp_bitmap;
5462 last_processed_bb = curr_bb;
5463 last_insn = get_last_insertion_point (curr_bb);
5464 after_p = (! JUMP_P (last_insn)
5465 && (! CALL_P (last_insn)
5466 || (find_reg_note (last_insn,
5467 REG_NORETURN, NULL_RTX) == NULL_RTX
5468 && ! SIBLING_CALL_P (last_insn))));
5469 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5470 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5472 if ((int) j >= lra_constraint_new_regno_start)
5473 break;
5474 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5476 if (j < FIRST_PSEUDO_REGISTER)
5477 SET_HARD_REG_BIT (live_hard_regs, j);
5478 else
5479 add_to_hard_reg_set (&live_hard_regs,
5480 PSEUDO_REGNO_MODE (j),
5481 reg_renumber[j]);
5482 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5486 src_regno = dst_regno = -1;
5487 if (NONDEBUG_INSN_P (curr_insn)
5488 && (set = single_set (curr_insn)) != NULL_RTX
5489 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5491 src_regno = REGNO (SET_SRC (set));
5492 dst_regno = REGNO (SET_DEST (set));
5494 update_reloads_num_p = true;
5495 if (src_regno < lra_constraint_new_regno_start
5496 && src_regno >= FIRST_PSEUDO_REGISTER
5497 && reg_renumber[src_regno] < 0
5498 && dst_regno >= lra_constraint_new_regno_start
5499 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5501 /* 'reload_pseudo <- original_pseudo'. */
5502 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5503 reloads_num++;
5504 update_reloads_num_p = false;
5505 succ_p = false;
5506 if (usage_insns[src_regno].check == curr_usage_insns_check
5507 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5508 succ_p = inherit_reload_reg (false, src_regno, cl,
5509 curr_insn, next_usage_insns);
5510 if (succ_p)
5511 change_p = true;
5512 else
5513 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5514 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5515 IOR_HARD_REG_SET (potential_reload_hard_regs,
5516 reg_class_contents[cl]);
5518 else if (src_regno >= lra_constraint_new_regno_start
5519 && dst_regno < lra_constraint_new_regno_start
5520 && dst_regno >= FIRST_PSEUDO_REGISTER
5521 && reg_renumber[dst_regno] < 0
5522 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5523 && usage_insns[dst_regno].check == curr_usage_insns_check
5524 && (next_usage_insns
5525 = usage_insns[dst_regno].insns) != NULL_RTX)
5527 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5528 reloads_num++;
5529 update_reloads_num_p = false;
5530 /* 'original_pseudo <- reload_pseudo'. */
5531 if (! JUMP_P (curr_insn)
5532 && inherit_reload_reg (true, dst_regno, cl,
5533 curr_insn, next_usage_insns))
5534 change_p = true;
5535 /* Invalidate. */
5536 usage_insns[dst_regno].check = 0;
5537 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5538 IOR_HARD_REG_SET (potential_reload_hard_regs,
5539 reg_class_contents[cl]);
5541 else if (INSN_P (curr_insn))
5543 int iter;
5544 int max_uid = get_max_uid ();
5546 curr_id = lra_get_insn_recog_data (curr_insn);
5547 curr_static_id = curr_id->insn_static_data;
5548 to_inherit_num = 0;
5549 /* Process insn definitions. */
5550 for (iter = 0; iter < 2; iter++)
5551 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5552 reg != NULL;
5553 reg = reg->next)
5554 if (reg->type != OP_IN
5555 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5557 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5558 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5559 && usage_insns[dst_regno].check == curr_usage_insns_check
5560 && (next_usage_insns
5561 = usage_insns[dst_regno].insns) != NULL_RTX)
5563 struct lra_insn_reg *r;
5565 for (r = curr_id->regs; r != NULL; r = r->next)
5566 if (r->type != OP_OUT && r->regno == dst_regno)
5567 break;
5568 /* Don't do inheritance if the pseudo is also
5569 used in the insn. */
5570 if (r == NULL)
5571 /* We can not do inheritance right now
5572 because the current insn reg info (chain
5573 regs) can change after that. */
5574 add_to_inherit (dst_regno, next_usage_insns);
5576 /* We can not process one reg twice here because of
5577 usage_insns invalidation. */
5578 if ((dst_regno < FIRST_PSEUDO_REGISTER
5579 || reg_renumber[dst_regno] >= 0)
5580 && ! reg->subreg_p && reg->type != OP_IN)
5582 HARD_REG_SET s;
5584 if (split_if_necessary (dst_regno, reg->biggest_mode,
5585 potential_reload_hard_regs,
5586 false, curr_insn, max_uid))
5587 change_p = true;
5588 CLEAR_HARD_REG_SET (s);
5589 if (dst_regno < FIRST_PSEUDO_REGISTER)
5590 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5591 else
5592 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5593 reg_renumber[dst_regno]);
5594 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5596 /* We should invalidate potential inheritance or
5597 splitting for the current insn usages to the next
5598 usage insns (see code below) as the output pseudo
5599 prevents this. */
5600 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5601 && reg_renumber[dst_regno] < 0)
5602 || (reg->type == OP_OUT && ! reg->subreg_p
5603 && (dst_regno < FIRST_PSEUDO_REGISTER
5604 || reg_renumber[dst_regno] >= 0)))
5606 /* Invalidate and mark definitions. */
5607 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5608 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5609 else
5611 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5612 for (i = 0; i < nregs; i++)
5613 usage_insns[dst_regno + i].check
5614 = -(int) INSN_UID (curr_insn);
5618 /* Process clobbered call regs. */
5619 if (curr_id->arg_hard_regs != NULL)
5620 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5621 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5622 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
5623 = -(int) INSN_UID (curr_insn);
5624 if (! JUMP_P (curr_insn))
5625 for (i = 0; i < to_inherit_num; i++)
5626 if (inherit_reload_reg (true, to_inherit[i].regno,
5627 ALL_REGS, curr_insn,
5628 to_inherit[i].insns))
5629 change_p = true;
5630 if (CALL_P (curr_insn))
5632 rtx cheap, pat, dest;
5633 rtx_insn *restore;
5634 int regno, hard_regno;
5636 calls_num++;
5637 if ((cheap = find_reg_note (curr_insn,
5638 REG_RETURNED, NULL_RTX)) != NULL_RTX
5639 && ((cheap = XEXP (cheap, 0)), true)
5640 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5641 && (hard_regno = reg_renumber[regno]) >= 0
5642 /* If there are pending saves/restores, the
5643 optimization is not worth. */
5644 && usage_insns[regno].calls_num == calls_num - 1
5645 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5647 /* Restore the pseudo from the call result as
5648 REG_RETURNED note says that the pseudo value is
5649 in the call result and the pseudo is an argument
5650 of the call. */
5651 pat = PATTERN (curr_insn);
5652 if (GET_CODE (pat) == PARALLEL)
5653 pat = XVECEXP (pat, 0, 0);
5654 dest = SET_DEST (pat);
5655 /* For multiple return values dest is PARALLEL.
5656 Currently we handle only single return value case. */
5657 if (REG_P (dest))
5659 start_sequence ();
5660 emit_move_insn (cheap, copy_rtx (dest));
5661 restore = get_insns ();
5662 end_sequence ();
5663 lra_process_new_insns (curr_insn, NULL, restore,
5664 "Inserting call parameter restore");
5665 /* We don't need to save/restore of the pseudo from
5666 this call. */
5667 usage_insns[regno].calls_num = calls_num;
5668 bitmap_set_bit (&check_only_regs, regno);
5672 to_inherit_num = 0;
5673 /* Process insn usages. */
5674 for (iter = 0; iter < 2; iter++)
5675 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5676 reg != NULL;
5677 reg = reg->next)
5678 if ((reg->type != OP_OUT
5679 || (reg->type == OP_OUT && reg->subreg_p))
5680 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5682 if (src_regno >= FIRST_PSEUDO_REGISTER
5683 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5685 if (usage_insns[src_regno].check == curr_usage_insns_check
5686 && (next_usage_insns
5687 = usage_insns[src_regno].insns) != NULL_RTX
5688 && NONDEBUG_INSN_P (curr_insn))
5689 add_to_inherit (src_regno, next_usage_insns);
5690 else if (usage_insns[src_regno].check
5691 != -(int) INSN_UID (curr_insn))
5692 /* Add usages but only if the reg is not set up
5693 in the same insn. */
5694 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5696 else if (src_regno < FIRST_PSEUDO_REGISTER
5697 || reg_renumber[src_regno] >= 0)
5699 bool before_p;
5700 rtx_insn *use_insn = curr_insn;
5702 before_p = (JUMP_P (curr_insn)
5703 || (CALL_P (curr_insn) && reg->type == OP_IN));
5704 if (NONDEBUG_INSN_P (curr_insn)
5705 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5706 && split_if_necessary (src_regno, reg->biggest_mode,
5707 potential_reload_hard_regs,
5708 before_p, curr_insn, max_uid))
5710 if (reg->subreg_p)
5711 lra_risky_transformations_p = true;
5712 change_p = true;
5713 /* Invalidate. */
5714 usage_insns[src_regno].check = 0;
5715 if (before_p)
5716 use_insn = PREV_INSN (curr_insn);
5718 if (NONDEBUG_INSN_P (curr_insn))
5720 if (src_regno < FIRST_PSEUDO_REGISTER)
5721 add_to_hard_reg_set (&live_hard_regs,
5722 reg->biggest_mode, src_regno);
5723 else
5724 add_to_hard_reg_set (&live_hard_regs,
5725 PSEUDO_REGNO_MODE (src_regno),
5726 reg_renumber[src_regno]);
5728 add_next_usage_insn (src_regno, use_insn, reloads_num);
5731 /* Process used call regs. */
5732 if (curr_id->arg_hard_regs != NULL)
5733 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5734 if (src_regno < FIRST_PSEUDO_REGISTER)
5736 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5737 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5739 for (i = 0; i < to_inherit_num; i++)
5741 src_regno = to_inherit[i].regno;
5742 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5743 curr_insn, to_inherit[i].insns))
5744 change_p = true;
5745 else
5746 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5749 if (update_reloads_num_p
5750 && NONDEBUG_INSN_P (curr_insn)
5751 && (set = single_set (curr_insn)) != NULL_RTX)
5753 int regno = -1;
5754 if ((REG_P (SET_DEST (set))
5755 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5756 && reg_renumber[regno] < 0
5757 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5758 || (REG_P (SET_SRC (set))
5759 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5760 && reg_renumber[regno] < 0
5761 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5763 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5764 reloads_num++;
5765 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5766 IOR_HARD_REG_SET (potential_reload_hard_regs,
5767 reg_class_contents[cl]);
5770 /* We reached the start of the current basic block. */
5771 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5772 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5774 /* We reached the beginning of the current block -- do
5775 rest of spliting in the current BB. */
5776 to_process = df_get_live_in (curr_bb);
5777 if (BLOCK_FOR_INSN (head) != curr_bb)
5779 /* We are somewhere in the middle of EBB. */
5780 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5781 curr_bb, &temp_bitmap);
5782 to_process = &temp_bitmap;
5784 head_p = true;
5785 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5787 if ((int) j >= lra_constraint_new_regno_start)
5788 break;
5789 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5790 && usage_insns[j].check == curr_usage_insns_check
5791 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5793 if (need_for_split_p (potential_reload_hard_regs, j))
5795 if (lra_dump_file != NULL && head_p)
5797 fprintf (lra_dump_file,
5798 " ----------------------------------\n");
5799 head_p = false;
5801 if (split_reg (false, j, bb_note (curr_bb),
5802 next_usage_insns))
5803 change_p = true;
5805 usage_insns[j].check = 0;
5810 return change_p;
5813 /* This value affects EBB forming. If probability of edge from EBB to
5814 a BB is not greater than the following value, we don't add the BB
5815 to EBB. */
5816 #define EBB_PROBABILITY_CUTOFF \
5817 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
5819 /* Current number of inheritance/split iteration. */
5820 int lra_inheritance_iter;
5822 /* Entry function for inheritance/split pass. */
5823 void
5824 lra_inheritance (void)
5826 int i;
5827 basic_block bb, start_bb;
5828 edge e;
5830 lra_inheritance_iter++;
5831 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5832 return;
5833 timevar_push (TV_LRA_INHERITANCE);
5834 if (lra_dump_file != NULL)
5835 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5836 lra_inheritance_iter);
5837 curr_usage_insns_check = 0;
5838 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5839 for (i = 0; i < lra_constraint_new_regno_start; i++)
5840 usage_insns[i].check = 0;
5841 bitmap_initialize (&check_only_regs, &reg_obstack);
5842 bitmap_initialize (&live_regs, &reg_obstack);
5843 bitmap_initialize (&temp_bitmap, &reg_obstack);
5844 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5845 FOR_EACH_BB_FN (bb, cfun)
5847 start_bb = bb;
5848 if (lra_dump_file != NULL)
5849 fprintf (lra_dump_file, "EBB");
5850 /* Form a EBB starting with BB. */
5851 bitmap_clear (&ebb_global_regs);
5852 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5853 for (;;)
5855 if (lra_dump_file != NULL)
5856 fprintf (lra_dump_file, " %d", bb->index);
5857 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5858 || LABEL_P (BB_HEAD (bb->next_bb)))
5859 break;
5860 e = find_fallthru_edge (bb->succs);
5861 if (! e)
5862 break;
5863 if (e->probability < EBB_PROBABILITY_CUTOFF)
5864 break;
5865 bb = bb->next_bb;
5867 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5868 if (lra_dump_file != NULL)
5869 fprintf (lra_dump_file, "\n");
5870 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5871 /* Remember that the EBB head and tail can change in
5872 inherit_in_ebb. */
5873 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5875 bitmap_clear (&ebb_global_regs);
5876 bitmap_clear (&temp_bitmap);
5877 bitmap_clear (&live_regs);
5878 bitmap_clear (&check_only_regs);
5879 free (usage_insns);
5881 timevar_pop (TV_LRA_INHERITANCE);
5886 /* This page contains code to undo failed inheritance/split
5887 transformations. */
5889 /* Current number of iteration undoing inheritance/split. */
5890 int lra_undo_inheritance_iter;
5892 /* Fix BB live info LIVE after removing pseudos created on pass doing
5893 inheritance/split which are REMOVED_PSEUDOS. */
5894 static void
5895 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5897 unsigned int regno;
5898 bitmap_iterator bi;
5900 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5901 if (bitmap_clear_bit (live, regno))
5902 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5905 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5906 number. */
5907 static int
5908 get_regno (rtx reg)
5910 if (GET_CODE (reg) == SUBREG)
5911 reg = SUBREG_REG (reg);
5912 if (REG_P (reg))
5913 return REGNO (reg);
5914 return -1;
5917 /* Delete a move INSN with destination reg DREGNO and a previous
5918 clobber insn with the same regno. The inheritance/split code can
5919 generate moves with preceding clobber and when we delete such moves
5920 we should delete the clobber insn too to keep the correct life
5921 info. */
5922 static void
5923 delete_move_and_clobber (rtx_insn *insn, int dregno)
5925 rtx_insn *prev_insn = PREV_INSN (insn);
5927 lra_set_insn_deleted (insn);
5928 lra_assert (dregno >= 0);
5929 if (prev_insn != NULL && NONDEBUG_INSN_P (prev_insn)
5930 && GET_CODE (PATTERN (prev_insn)) == CLOBBER
5931 && dregno == get_regno (XEXP (PATTERN (prev_insn), 0)))
5932 lra_set_insn_deleted (prev_insn);
5935 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5936 return true if we did any change. The undo transformations for
5937 inheritance looks like
5938 i <- i2
5939 p <- i => p <- i2
5940 or removing
5941 p <- i, i <- p, and i <- i3
5942 where p is original pseudo from which inheritance pseudo i was
5943 created, i and i3 are removed inheritance pseudos, i2 is another
5944 not removed inheritance pseudo. All split pseudos or other
5945 occurrences of removed inheritance pseudos are changed on the
5946 corresponding original pseudos.
5948 The function also schedules insns changed and created during
5949 inheritance/split pass for processing by the subsequent constraint
5950 pass. */
5951 static bool
5952 remove_inheritance_pseudos (bitmap remove_pseudos)
5954 basic_block bb;
5955 int regno, sregno, prev_sregno, dregno, restore_regno;
5956 rtx set, prev_set;
5957 rtx_insn *prev_insn;
5958 bool change_p, done_p;
5960 change_p = ! bitmap_empty_p (remove_pseudos);
5961 /* We can not finish the function right away if CHANGE_P is true
5962 because we need to marks insns affected by previous
5963 inheritance/split pass for processing by the subsequent
5964 constraint pass. */
5965 FOR_EACH_BB_FN (bb, cfun)
5967 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5968 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5969 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5971 if (! INSN_P (curr_insn))
5972 continue;
5973 done_p = false;
5974 sregno = dregno = -1;
5975 if (change_p && NONDEBUG_INSN_P (curr_insn)
5976 && (set = single_set (curr_insn)) != NULL_RTX)
5978 dregno = get_regno (SET_DEST (set));
5979 sregno = get_regno (SET_SRC (set));
5982 if (sregno >= 0 && dregno >= 0)
5984 if ((bitmap_bit_p (remove_pseudos, sregno)
5985 && (lra_reg_info[sregno].restore_regno == dregno
5986 || (bitmap_bit_p (remove_pseudos, dregno)
5987 && (lra_reg_info[sregno].restore_regno
5988 == lra_reg_info[dregno].restore_regno))))
5989 || (bitmap_bit_p (remove_pseudos, dregno)
5990 && lra_reg_info[dregno].restore_regno == sregno))
5991 /* One of the following cases:
5992 original <- removed inheritance pseudo
5993 removed inherit pseudo <- another removed inherit pseudo
5994 removed inherit pseudo <- original pseudo
5996 removed_split_pseudo <- original_reg
5997 original_reg <- removed_split_pseudo */
5999 if (lra_dump_file != NULL)
6001 fprintf (lra_dump_file, " Removing %s:\n",
6002 bitmap_bit_p (&lra_split_regs, sregno)
6003 || bitmap_bit_p (&lra_split_regs, dregno)
6004 ? "split" : "inheritance");
6005 dump_insn_slim (lra_dump_file, curr_insn);
6007 delete_move_and_clobber (curr_insn, dregno);
6008 done_p = true;
6010 else if (bitmap_bit_p (remove_pseudos, sregno)
6011 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
6013 /* Search the following pattern:
6014 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
6015 original_pseudo <- inherit_or_split_pseudo1
6016 where the 2nd insn is the current insn and
6017 inherit_or_split_pseudo2 is not removed. If it is found,
6018 change the current insn onto:
6019 original_pseudo <- inherit_or_split_pseudo2. */
6020 for (prev_insn = PREV_INSN (curr_insn);
6021 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
6022 prev_insn = PREV_INSN (prev_insn))
6024 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
6025 && (prev_set = single_set (prev_insn)) != NULL_RTX
6026 /* There should be no subregs in insn we are
6027 searching because only the original reg might
6028 be in subreg when we changed the mode of
6029 load/store for splitting. */
6030 && REG_P (SET_DEST (prev_set))
6031 && REG_P (SET_SRC (prev_set))
6032 && (int) REGNO (SET_DEST (prev_set)) == sregno
6033 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
6034 >= FIRST_PSEUDO_REGISTER)
6035 /* As we consider chain of inheritance or
6036 splitting described in above comment we should
6037 check that sregno and prev_sregno were
6038 inheritance/split pseudos created from the
6039 same original regno. */
6040 && (lra_reg_info[sregno].restore_regno
6041 == lra_reg_info[prev_sregno].restore_regno)
6042 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
6044 lra_assert (GET_MODE (SET_SRC (prev_set))
6045 == GET_MODE (regno_reg_rtx[sregno]));
6046 if (GET_CODE (SET_SRC (set)) == SUBREG)
6047 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
6048 else
6049 SET_SRC (set) = SET_SRC (prev_set);
6050 /* As we are finishing with processing the insn
6051 here, check the destination too as it might
6052 inheritance pseudo for another pseudo. */
6053 if (bitmap_bit_p (remove_pseudos, dregno)
6054 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
6055 && (restore_regno
6056 = lra_reg_info[dregno].restore_regno) >= 0)
6058 if (GET_CODE (SET_DEST (set)) == SUBREG)
6059 SUBREG_REG (SET_DEST (set))
6060 = regno_reg_rtx[restore_regno];
6061 else
6062 SET_DEST (set) = regno_reg_rtx[restore_regno];
6064 lra_push_insn_and_update_insn_regno_info (curr_insn);
6065 lra_set_used_insn_alternative_by_uid
6066 (INSN_UID (curr_insn), -1);
6067 done_p = true;
6068 if (lra_dump_file != NULL)
6070 fprintf (lra_dump_file, " Change reload insn:\n");
6071 dump_insn_slim (lra_dump_file, curr_insn);
6076 if (! done_p)
6078 struct lra_insn_reg *reg;
6079 bool restored_regs_p = false;
6080 bool kept_regs_p = false;
6082 curr_id = lra_get_insn_recog_data (curr_insn);
6083 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6085 regno = reg->regno;
6086 restore_regno = lra_reg_info[regno].restore_regno;
6087 if (restore_regno >= 0)
6089 if (change_p && bitmap_bit_p (remove_pseudos, regno))
6091 lra_substitute_pseudo_within_insn
6092 (curr_insn, regno, regno_reg_rtx[restore_regno],
6093 false);
6094 restored_regs_p = true;
6096 else
6097 kept_regs_p = true;
6100 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
6102 /* The instruction has changed since the previous
6103 constraints pass. */
6104 lra_push_insn_and_update_insn_regno_info (curr_insn);
6105 lra_set_used_insn_alternative_by_uid
6106 (INSN_UID (curr_insn), -1);
6108 else if (restored_regs_p)
6109 /* The instruction has been restored to the form that
6110 it had during the previous constraints pass. */
6111 lra_update_insn_regno_info (curr_insn);
6112 if (restored_regs_p && lra_dump_file != NULL)
6114 fprintf (lra_dump_file, " Insn after restoring regs:\n");
6115 dump_insn_slim (lra_dump_file, curr_insn);
6120 return change_p;
6123 /* If optional reload pseudos failed to get a hard register or was not
6124 inherited, it is better to remove optional reloads. We do this
6125 transformation after undoing inheritance to figure out necessity to
6126 remove optional reloads easier. Return true if we do any
6127 change. */
6128 static bool
6129 undo_optional_reloads (void)
6131 bool change_p, keep_p;
6132 unsigned int regno, uid;
6133 bitmap_iterator bi, bi2;
6134 rtx_insn *insn;
6135 rtx set, src, dest;
6136 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
6138 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
6139 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
6140 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6142 keep_p = false;
6143 /* Keep optional reloads from previous subpasses. */
6144 if (lra_reg_info[regno].restore_regno < 0
6145 /* If the original pseudo changed its allocation, just
6146 removing the optional pseudo is dangerous as the original
6147 pseudo will have longer live range. */
6148 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
6149 keep_p = true;
6150 else if (reg_renumber[regno] >= 0)
6151 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
6153 insn = lra_insn_recog_data[uid]->insn;
6154 if ((set = single_set (insn)) == NULL_RTX)
6155 continue;
6156 src = SET_SRC (set);
6157 dest = SET_DEST (set);
6158 if (! REG_P (src) || ! REG_P (dest))
6159 continue;
6160 if (REGNO (dest) == regno
6161 /* Ignore insn for optional reloads itself. */
6162 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
6163 /* Check only inheritance on last inheritance pass. */
6164 && (int) REGNO (src) >= new_regno_start
6165 /* Check that the optional reload was inherited. */
6166 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6168 keep_p = true;
6169 break;
6172 if (keep_p)
6174 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6175 if (lra_dump_file != NULL)
6176 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6179 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6180 bitmap_initialize (&insn_bitmap, &reg_obstack);
6181 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6183 if (lra_dump_file != NULL)
6184 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6185 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6186 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6188 insn = lra_insn_recog_data[uid]->insn;
6189 if ((set = single_set (insn)) != NULL_RTX)
6191 src = SET_SRC (set);
6192 dest = SET_DEST (set);
6193 if (REG_P (src) && REG_P (dest)
6194 && ((REGNO (src) == regno
6195 && (lra_reg_info[regno].restore_regno
6196 == (int) REGNO (dest)))
6197 || (REGNO (dest) == regno
6198 && (lra_reg_info[regno].restore_regno
6199 == (int) REGNO (src)))))
6201 if (lra_dump_file != NULL)
6203 fprintf (lra_dump_file, " Deleting move %u\n",
6204 INSN_UID (insn));
6205 dump_insn_slim (lra_dump_file, insn);
6207 delete_move_and_clobber (insn, REGNO (dest));
6208 continue;
6210 /* We should not worry about generation memory-memory
6211 moves here as if the corresponding inheritance did
6212 not work (inheritance pseudo did not get a hard reg),
6213 we remove the inheritance pseudo and the optional
6214 reload. */
6216 lra_substitute_pseudo_within_insn
6217 (insn, regno, regno_reg_rtx[lra_reg_info[regno].restore_regno],
6218 false);
6219 lra_update_insn_regno_info (insn);
6220 if (lra_dump_file != NULL)
6222 fprintf (lra_dump_file,
6223 " Restoring original insn:\n");
6224 dump_insn_slim (lra_dump_file, insn);
6228 /* Clear restore_regnos. */
6229 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6230 lra_reg_info[regno].restore_regno = -1;
6231 bitmap_clear (&insn_bitmap);
6232 bitmap_clear (&removed_optional_reload_pseudos);
6233 return change_p;
6236 /* Entry function for undoing inheritance/split transformation. Return true
6237 if we did any RTL change in this pass. */
6238 bool
6239 lra_undo_inheritance (void)
6241 unsigned int regno;
6242 int restore_regno, hard_regno;
6243 int n_all_inherit, n_inherit, n_all_split, n_split;
6244 bitmap_head remove_pseudos;
6245 bitmap_iterator bi;
6246 bool change_p;
6248 lra_undo_inheritance_iter++;
6249 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6250 return false;
6251 if (lra_dump_file != NULL)
6252 fprintf (lra_dump_file,
6253 "\n********** Undoing inheritance #%d: **********\n\n",
6254 lra_undo_inheritance_iter);
6255 bitmap_initialize (&remove_pseudos, &reg_obstack);
6256 n_inherit = n_all_inherit = 0;
6257 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6258 if (lra_reg_info[regno].restore_regno >= 0)
6260 n_all_inherit++;
6261 if (reg_renumber[regno] < 0
6262 /* If the original pseudo changed its allocation, just
6263 removing inheritance is dangerous as for changing
6264 allocation we used shorter live-ranges. */
6265 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6266 bitmap_set_bit (&remove_pseudos, regno);
6267 else
6268 n_inherit++;
6270 if (lra_dump_file != NULL && n_all_inherit != 0)
6271 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6272 n_inherit, n_all_inherit,
6273 (double) n_inherit / n_all_inherit * 100);
6274 n_split = n_all_split = 0;
6275 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6276 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6278 n_all_split++;
6279 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6280 ? reg_renumber[restore_regno] : restore_regno);
6281 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6282 bitmap_set_bit (&remove_pseudos, regno);
6283 else
6285 n_split++;
6286 if (lra_dump_file != NULL)
6287 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6288 regno, restore_regno);
6291 if (lra_dump_file != NULL && n_all_split != 0)
6292 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6293 n_split, n_all_split,
6294 (double) n_split / n_all_split * 100);
6295 change_p = remove_inheritance_pseudos (&remove_pseudos);
6296 bitmap_clear (&remove_pseudos);
6297 /* Clear restore_regnos. */
6298 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6299 lra_reg_info[regno].restore_regno = -1;
6300 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6301 lra_reg_info[regno].restore_regno = -1;
6302 change_p = undo_optional_reloads () || change_p;
6303 return change_p;