Add qdf24xx base tuning support.
[official-gcc.git] / gcc / lra-constraints.c
blobbf08dce2e0a4c2ef4c339aedbda4dba47cba1645
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 && !(REG_P (op) && hard_regno[nop] < 0))
2493 if (lra_dump_file != NULL)
2494 fprintf
2495 (lra_dump_file,
2496 " %d Spill %spseudo into memory: reject+=3\n",
2497 nop, REG_P (op) ? "" : "Non-");
2498 reject += 3;
2499 if (VECTOR_MODE_P (mode))
2501 /* Spilling vectors into memory is usually more
2502 costly as they contain big values. */
2503 if (lra_dump_file != NULL)
2504 fprintf
2505 (lra_dump_file,
2506 " %d Spill vector pseudo: reject+=2\n",
2507 nop);
2508 reject += 2;
2512 #ifdef SECONDARY_MEMORY_NEEDED
2513 /* If reload requires moving value through secondary
2514 memory, it will need one more insn at least. */
2515 if (this_alternative != NO_REGS
2516 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2517 && ((curr_static_id->operand[nop].type != OP_OUT
2518 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2519 GET_MODE (op)))
2520 || (curr_static_id->operand[nop].type != OP_IN
2521 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2522 GET_MODE (op)))))
2523 losers++;
2524 #endif
2525 /* Input reloads can be inherited more often than output
2526 reloads can be removed, so penalize output
2527 reloads. */
2528 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2530 if (lra_dump_file != NULL)
2531 fprintf
2532 (lra_dump_file,
2533 " %d Non input pseudo reload: reject++\n",
2534 nop);
2535 reject++;
2539 if (early_clobber_p && ! scratch_p)
2541 if (lra_dump_file != NULL)
2542 fprintf (lra_dump_file,
2543 " %d Early clobber: reject++\n", nop);
2544 reject++;
2546 /* ??? We check early clobbers after processing all operands
2547 (see loop below) and there we update the costs more.
2548 Should we update the cost (may be approximately) here
2549 because of early clobber register reloads or it is a rare
2550 or non-important thing to be worth to do it. */
2551 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2552 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2554 if (lra_dump_file != NULL)
2555 fprintf (lra_dump_file,
2556 " alt=%d,overall=%d,losers=%d -- refuse\n",
2557 nalt, overall, losers);
2558 goto fail;
2561 curr_alt[nop] = this_alternative;
2562 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2563 curr_alt_win[nop] = this_alternative_win;
2564 curr_alt_match_win[nop] = this_alternative_match_win;
2565 curr_alt_offmemok[nop] = this_alternative_offmemok;
2566 curr_alt_matches[nop] = this_alternative_matches;
2568 if (this_alternative_matches >= 0
2569 && !did_match && !this_alternative_win)
2570 curr_alt_win[this_alternative_matches] = false;
2572 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2573 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2575 if (curr_insn_set != NULL_RTX && n_operands == 2
2576 /* Prevent processing non-move insns. */
2577 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2578 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2579 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2580 && REG_P (no_subreg_reg_operand[0])
2581 && REG_P (no_subreg_reg_operand[1])
2582 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2583 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2584 || (! curr_alt_win[0] && curr_alt_win[1]
2585 && REG_P (no_subreg_reg_operand[1])
2586 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2587 || (curr_alt_win[0] && ! curr_alt_win[1]
2588 && REG_P (no_subreg_reg_operand[0])
2589 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2590 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2591 no_subreg_reg_operand[1])
2592 || (targetm.preferred_reload_class
2593 (no_subreg_reg_operand[1],
2594 (enum reg_class) curr_alt[1]) != NO_REGS))
2595 /* If it is a result of recent elimination in move
2596 insn we can transform it into an add still by
2597 using this alternative. */
2598 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2600 /* We have a move insn and a new reload insn will be similar
2601 to the current insn. We should avoid such situation as it
2602 results in LRA cycling. */
2603 overall += LRA_MAX_REJECT;
2605 ok_p = true;
2606 curr_alt_dont_inherit_ops_num = 0;
2607 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2609 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2610 HARD_REG_SET temp_set;
2612 i = early_clobbered_nops[nop];
2613 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2614 || hard_regno[i] < 0)
2615 continue;
2616 lra_assert (operand_reg[i] != NULL_RTX);
2617 clobbered_hard_regno = hard_regno[i];
2618 CLEAR_HARD_REG_SET (temp_set);
2619 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2620 first_conflict_j = last_conflict_j = -1;
2621 for (j = 0; j < n_operands; j++)
2622 if (j == i
2623 /* We don't want process insides of match_operator and
2624 match_parallel because otherwise we would process
2625 their operands once again generating a wrong
2626 code. */
2627 || curr_static_id->operand[j].is_operator)
2628 continue;
2629 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2630 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2631 continue;
2632 /* If we don't reload j-th operand, check conflicts. */
2633 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2634 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2636 if (first_conflict_j < 0)
2637 first_conflict_j = j;
2638 last_conflict_j = j;
2640 if (last_conflict_j < 0)
2641 continue;
2642 /* If earlyclobber operand conflicts with another
2643 non-matching operand which is actually the same register
2644 as the earlyclobber operand, it is better to reload the
2645 another operand as an operand matching the earlyclobber
2646 operand can be also the same. */
2647 if (first_conflict_j == last_conflict_j
2648 && operand_reg[last_conflict_j] != NULL_RTX
2649 && ! curr_alt_match_win[last_conflict_j]
2650 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2652 curr_alt_win[last_conflict_j] = false;
2653 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2654 = last_conflict_j;
2655 losers++;
2656 /* Early clobber was already reflected in REJECT. */
2657 lra_assert (reject > 0);
2658 if (lra_dump_file != NULL)
2659 fprintf
2660 (lra_dump_file,
2661 " %d Conflict early clobber reload: reject--\n",
2663 reject--;
2664 overall += LRA_LOSER_COST_FACTOR - 1;
2666 else
2668 /* We need to reload early clobbered register and the
2669 matched registers. */
2670 for (j = 0; j < n_operands; j++)
2671 if (curr_alt_matches[j] == i)
2673 curr_alt_match_win[j] = false;
2674 losers++;
2675 overall += LRA_LOSER_COST_FACTOR;
2677 if (! curr_alt_match_win[i])
2678 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2679 else
2681 /* Remember pseudos used for match reloads are never
2682 inherited. */
2683 lra_assert (curr_alt_matches[i] >= 0);
2684 curr_alt_win[curr_alt_matches[i]] = false;
2686 curr_alt_win[i] = curr_alt_match_win[i] = false;
2687 losers++;
2688 /* Early clobber was already reflected in REJECT. */
2689 lra_assert (reject > 0);
2690 if (lra_dump_file != NULL)
2691 fprintf
2692 (lra_dump_file,
2693 " %d Matched conflict early clobber reloads:"
2694 "reject--\n",
2696 reject--;
2697 overall += LRA_LOSER_COST_FACTOR - 1;
2700 if (lra_dump_file != NULL)
2701 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2702 nalt, overall, losers, reload_nregs);
2704 /* If this alternative can be made to work by reloading, and it
2705 needs less reloading than the others checked so far, record
2706 it as the chosen goal for reloading. */
2707 if ((best_losers != 0 && losers == 0)
2708 || (((best_losers == 0 && losers == 0)
2709 || (best_losers != 0 && losers != 0))
2710 && (best_overall > overall
2711 || (best_overall == overall
2712 /* If the cost of the reloads is the same,
2713 prefer alternative which requires minimal
2714 number of reload regs. */
2715 && (reload_nregs < best_reload_nregs
2716 || (reload_nregs == best_reload_nregs
2717 && (best_reload_sum < reload_sum
2718 || (best_reload_sum == reload_sum
2719 && nalt < goal_alt_number))))))))
2721 for (nop = 0; nop < n_operands; nop++)
2723 goal_alt_win[nop] = curr_alt_win[nop];
2724 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2725 goal_alt_matches[nop] = curr_alt_matches[nop];
2726 goal_alt[nop] = curr_alt[nop];
2727 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2729 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2730 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2731 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2732 goal_alt_swapped = curr_swapped;
2733 best_overall = overall;
2734 best_losers = losers;
2735 best_reload_nregs = reload_nregs;
2736 best_reload_sum = reload_sum;
2737 goal_alt_number = nalt;
2739 if (losers == 0)
2740 /* Everything is satisfied. Do not process alternatives
2741 anymore. */
2742 break;
2743 fail:
2746 return ok_p;
2749 /* Make reload base reg from address AD. */
2750 static rtx
2751 base_to_reg (struct address_info *ad)
2753 enum reg_class cl;
2754 int code = -1;
2755 rtx new_inner = NULL_RTX;
2756 rtx new_reg = NULL_RTX;
2757 rtx_insn *insn;
2758 rtx_insn *last_insn = get_last_insn();
2760 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2761 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2762 get_index_code (ad));
2763 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2764 cl, "base");
2765 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2766 ad->disp_term == NULL
2767 ? gen_int_mode (0, ad->mode)
2768 : *ad->disp_term);
2769 if (!valid_address_p (ad->mode, new_inner, ad->as))
2770 return NULL_RTX;
2771 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base_term));
2772 code = recog_memoized (insn);
2773 if (code < 0)
2775 delete_insns_since (last_insn);
2776 return NULL_RTX;
2779 return new_inner;
2782 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2783 static rtx
2784 base_plus_disp_to_reg (struct address_info *ad)
2786 enum reg_class cl;
2787 rtx new_reg;
2789 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2790 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2791 get_index_code (ad));
2792 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2793 cl, "base + disp");
2794 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2795 return new_reg;
2798 /* Make reload of index part of address AD. Return the new
2799 pseudo. */
2800 static rtx
2801 index_part_to_reg (struct address_info *ad)
2803 rtx new_reg;
2805 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2806 INDEX_REG_CLASS, "index term");
2807 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2808 GEN_INT (get_index_scale (ad)), new_reg, 1);
2809 return new_reg;
2812 /* Return true if we can add a displacement to address AD, even if that
2813 makes the address invalid. The fix-up code requires any new address
2814 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2815 static bool
2816 can_add_disp_p (struct address_info *ad)
2818 return (!ad->autoinc_p
2819 && ad->segment == NULL
2820 && ad->base == ad->base_term
2821 && ad->disp == ad->disp_term);
2824 /* Make equiv substitution in address AD. Return true if a substitution
2825 was made. */
2826 static bool
2827 equiv_address_substitution (struct address_info *ad)
2829 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2830 HOST_WIDE_INT disp, scale;
2831 bool change_p;
2833 base_term = strip_subreg (ad->base_term);
2834 if (base_term == NULL)
2835 base_reg = new_base_reg = NULL_RTX;
2836 else
2838 base_reg = *base_term;
2839 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2841 index_term = strip_subreg (ad->index_term);
2842 if (index_term == NULL)
2843 index_reg = new_index_reg = NULL_RTX;
2844 else
2846 index_reg = *index_term;
2847 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2849 if (base_reg == new_base_reg && index_reg == new_index_reg)
2850 return false;
2851 disp = 0;
2852 change_p = false;
2853 if (lra_dump_file != NULL)
2855 fprintf (lra_dump_file, "Changing address in insn %d ",
2856 INSN_UID (curr_insn));
2857 dump_value_slim (lra_dump_file, *ad->outer, 1);
2859 if (base_reg != new_base_reg)
2861 if (REG_P (new_base_reg))
2863 *base_term = new_base_reg;
2864 change_p = true;
2866 else if (GET_CODE (new_base_reg) == PLUS
2867 && REG_P (XEXP (new_base_reg, 0))
2868 && CONST_INT_P (XEXP (new_base_reg, 1))
2869 && can_add_disp_p (ad))
2871 disp += INTVAL (XEXP (new_base_reg, 1));
2872 *base_term = XEXP (new_base_reg, 0);
2873 change_p = true;
2875 if (ad->base_term2 != NULL)
2876 *ad->base_term2 = *ad->base_term;
2878 if (index_reg != new_index_reg)
2880 if (REG_P (new_index_reg))
2882 *index_term = new_index_reg;
2883 change_p = true;
2885 else if (GET_CODE (new_index_reg) == PLUS
2886 && REG_P (XEXP (new_index_reg, 0))
2887 && CONST_INT_P (XEXP (new_index_reg, 1))
2888 && can_add_disp_p (ad)
2889 && (scale = get_index_scale (ad)))
2891 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2892 *index_term = XEXP (new_index_reg, 0);
2893 change_p = true;
2896 if (disp != 0)
2898 if (ad->disp != NULL)
2899 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2900 else
2902 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2903 update_address (ad);
2905 change_p = true;
2907 if (lra_dump_file != NULL)
2909 if (! change_p)
2910 fprintf (lra_dump_file, " -- no change\n");
2911 else
2913 fprintf (lra_dump_file, " on equiv ");
2914 dump_value_slim (lra_dump_file, *ad->outer, 1);
2915 fprintf (lra_dump_file, "\n");
2918 return change_p;
2921 /* Major function to make reloads for an address in operand NOP or
2922 check its correctness (If CHECK_ONLY_P is true). The supported
2923 cases are:
2925 1) an address that existed before LRA started, at which point it
2926 must have been valid. These addresses are subject to elimination
2927 and may have become invalid due to the elimination offset being out
2928 of range.
2930 2) an address created by forcing a constant to memory
2931 (force_const_to_mem). The initial form of these addresses might
2932 not be valid, and it is this function's job to make them valid.
2934 3) a frame address formed from a register and a (possibly zero)
2935 constant offset. As above, these addresses might not be valid and
2936 this function must make them so.
2938 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2939 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2940 address. Return true for any RTL change.
2942 The function is a helper function which does not produce all
2943 transformations (when CHECK_ONLY_P is false) which can be
2944 necessary. It does just basic steps. To do all necessary
2945 transformations use function process_address. */
2946 static bool
2947 process_address_1 (int nop, bool check_only_p,
2948 rtx_insn **before, rtx_insn **after)
2950 struct address_info ad;
2951 rtx new_reg;
2952 HOST_WIDE_INT scale;
2953 rtx op = *curr_id->operand_loc[nop];
2954 const char *constraint = curr_static_id->operand[nop].constraint;
2955 enum constraint_num cn = lookup_constraint (constraint);
2956 bool change_p = false;
2958 if (MEM_P (op)
2959 && GET_MODE (op) == BLKmode
2960 && GET_CODE (XEXP (op, 0)) == SCRATCH)
2961 return false;
2963 if (insn_extra_address_constraint (cn))
2964 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2965 else if (MEM_P (op))
2966 decompose_mem_address (&ad, op);
2967 else if (GET_CODE (op) == SUBREG
2968 && MEM_P (SUBREG_REG (op)))
2969 decompose_mem_address (&ad, SUBREG_REG (op));
2970 else
2971 return false;
2972 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
2973 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
2974 when INDEX_REG_CLASS is a single register class. */
2975 if (ad.base_term != NULL
2976 && ad.index_term != NULL
2977 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
2978 && REG_P (*ad.base_term)
2979 && REG_P (*ad.index_term)
2980 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
2981 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
2983 std::swap (ad.base, ad.index);
2984 std::swap (ad.base_term, ad.index_term);
2986 if (! check_only_p)
2987 change_p = equiv_address_substitution (&ad);
2988 if (ad.base_term != NULL
2989 && (process_addr_reg
2990 (ad.base_term, check_only_p, before,
2991 (ad.autoinc_p
2992 && !(REG_P (*ad.base_term)
2993 && find_regno_note (curr_insn, REG_DEAD,
2994 REGNO (*ad.base_term)) != NULL_RTX)
2995 ? after : NULL),
2996 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2997 get_index_code (&ad)))))
2999 change_p = true;
3000 if (ad.base_term2 != NULL)
3001 *ad.base_term2 = *ad.base_term;
3003 if (ad.index_term != NULL
3004 && process_addr_reg (ad.index_term, check_only_p,
3005 before, NULL, INDEX_REG_CLASS))
3006 change_p = true;
3008 /* Target hooks sometimes don't treat extra-constraint addresses as
3009 legitimate address_operands, so handle them specially. */
3010 if (insn_extra_address_constraint (cn)
3011 && satisfies_address_constraint_p (&ad, cn))
3012 return change_p;
3014 if (check_only_p)
3015 return change_p;
3017 /* There are three cases where the shape of *AD.INNER may now be invalid:
3019 1) the original address was valid, but either elimination or
3020 equiv_address_substitution was applied and that made
3021 the address invalid.
3023 2) the address is an invalid symbolic address created by
3024 force_const_to_mem.
3026 3) the address is a frame address with an invalid offset.
3028 4) the address is a frame address with an invalid base.
3030 All these cases involve a non-autoinc address, so there is no
3031 point revalidating other types. */
3032 if (ad.autoinc_p || valid_address_p (&ad))
3033 return change_p;
3035 /* Any index existed before LRA started, so we can assume that the
3036 presence and shape of the index is valid. */
3037 push_to_sequence (*before);
3038 lra_assert (ad.disp == ad.disp_term);
3039 if (ad.base == NULL)
3041 if (ad.index == NULL)
3043 rtx_insn *insn;
3044 rtx_insn *last = get_last_insn ();
3045 int code = -1;
3046 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3047 SCRATCH, SCRATCH);
3048 rtx addr = *ad.inner;
3050 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3051 if (HAVE_lo_sum)
3053 /* addr => lo_sum (new_base, addr), case (2) above. */
3054 insn = emit_insn (gen_rtx_SET
3055 (new_reg,
3056 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
3057 code = recog_memoized (insn);
3058 if (code >= 0)
3060 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
3061 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
3063 /* Try to put lo_sum into register. */
3064 insn = emit_insn (gen_rtx_SET
3065 (new_reg,
3066 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
3067 code = recog_memoized (insn);
3068 if (code >= 0)
3070 *ad.inner = new_reg;
3071 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
3073 *ad.inner = addr;
3074 code = -1;
3080 if (code < 0)
3081 delete_insns_since (last);
3084 if (code < 0)
3086 /* addr => new_base, case (2) above. */
3087 lra_emit_move (new_reg, addr);
3089 for (insn = last == NULL_RTX ? get_insns () : NEXT_INSN (last);
3090 insn != NULL_RTX;
3091 insn = NEXT_INSN (insn))
3092 if (recog_memoized (insn) < 0)
3093 break;
3094 if (insn != NULL_RTX)
3096 /* Do nothing if we cannot generate right insns.
3097 This is analogous to reload pass behavior. */
3098 delete_insns_since (last);
3099 end_sequence ();
3100 return false;
3102 *ad.inner = new_reg;
3105 else
3107 /* index * scale + disp => new base + index * scale,
3108 case (1) above. */
3109 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3110 GET_CODE (*ad.index));
3112 lra_assert (INDEX_REG_CLASS != NO_REGS);
3113 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3114 lra_emit_move (new_reg, *ad.disp);
3115 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3116 new_reg, *ad.index);
3119 else if (ad.index == NULL)
3121 int regno;
3122 enum reg_class cl;
3123 rtx set;
3124 rtx_insn *insns, *last_insn;
3125 /* Try to reload base into register only if the base is invalid
3126 for the address but with valid offset, case (4) above. */
3127 start_sequence ();
3128 new_reg = base_to_reg (&ad);
3130 /* base + disp => new base, cases (1) and (3) above. */
3131 /* Another option would be to reload the displacement into an
3132 index register. However, postreload has code to optimize
3133 address reloads that have the same base and different
3134 displacements, so reloading into an index register would
3135 not necessarily be a win. */
3136 if (new_reg == NULL_RTX)
3137 new_reg = base_plus_disp_to_reg (&ad);
3138 insns = get_insns ();
3139 last_insn = get_last_insn ();
3140 /* If we generated at least two insns, try last insn source as
3141 an address. If we succeed, we generate one less insn. */
3142 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3143 && GET_CODE (SET_SRC (set)) == PLUS
3144 && REG_P (XEXP (SET_SRC (set), 0))
3145 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3147 *ad.inner = SET_SRC (set);
3148 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3150 *ad.base_term = XEXP (SET_SRC (set), 0);
3151 *ad.disp_term = XEXP (SET_SRC (set), 1);
3152 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3153 get_index_code (&ad));
3154 regno = REGNO (*ad.base_term);
3155 if (regno >= FIRST_PSEUDO_REGISTER
3156 && cl != lra_get_allocno_class (regno))
3157 lra_change_class (regno, cl, " Change to", true);
3158 new_reg = SET_SRC (set);
3159 delete_insns_since (PREV_INSN (last_insn));
3162 /* Try if target can split displacement into legitimite new disp
3163 and offset. If it's the case, we replace the last insn with
3164 insns for base + offset => new_reg and set new_reg + new disp
3165 to *ad.inner. */
3166 last_insn = get_last_insn ();
3167 if ((set = single_set (last_insn)) != NULL_RTX
3168 && GET_CODE (SET_SRC (set)) == PLUS
3169 && REG_P (XEXP (SET_SRC (set), 0))
3170 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3171 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3173 rtx addend, disp = XEXP (SET_SRC (set), 1);
3174 if (targetm.legitimize_address_displacement (&disp, &addend,
3175 ad.mode))
3177 rtx_insn *new_insns;
3178 start_sequence ();
3179 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3180 new_insns = get_insns ();
3181 end_sequence ();
3182 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3183 delete_insns_since (PREV_INSN (last_insn));
3184 add_insn (new_insns);
3185 insns = get_insns ();
3188 end_sequence ();
3189 emit_insn (insns);
3190 *ad.inner = new_reg;
3192 else if (ad.disp_term != NULL)
3194 /* base + scale * index + disp => new base + scale * index,
3195 case (1) above. */
3196 new_reg = base_plus_disp_to_reg (&ad);
3197 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3198 new_reg, *ad.index);
3200 else if ((scale = get_index_scale (&ad)) == 1)
3202 /* The last transformation to one reg will be made in
3203 curr_insn_transform function. */
3204 end_sequence ();
3205 return false;
3207 else if (scale != 0)
3209 /* base + scale * index => base + new_reg,
3210 case (1) above.
3211 Index part of address may become invalid. For example, we
3212 changed pseudo on the equivalent memory and a subreg of the
3213 pseudo onto the memory of different mode for which the scale is
3214 prohibitted. */
3215 new_reg = index_part_to_reg (&ad);
3216 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3217 *ad.base_term, new_reg);
3219 else
3221 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3222 SCRATCH, SCRATCH);
3223 rtx addr = *ad.inner;
3225 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3226 /* addr => new_base. */
3227 lra_emit_move (new_reg, addr);
3228 *ad.inner = new_reg;
3230 *before = get_insns ();
3231 end_sequence ();
3232 return true;
3235 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3236 Use process_address_1 as a helper function. Return true for any
3237 RTL changes.
3239 If CHECK_ONLY_P is true, just check address correctness. Return
3240 false if the address correct. */
3241 static bool
3242 process_address (int nop, bool check_only_p,
3243 rtx_insn **before, rtx_insn **after)
3245 bool res = false;
3247 while (process_address_1 (nop, check_only_p, before, after))
3249 if (check_only_p)
3250 return true;
3251 res = true;
3253 return res;
3256 /* Emit insns to reload VALUE into a new register. VALUE is an
3257 auto-increment or auto-decrement RTX whose operand is a register or
3258 memory location; so reloading involves incrementing that location.
3259 IN is either identical to VALUE, or some cheaper place to reload
3260 value being incremented/decremented from.
3262 INC_AMOUNT is the number to increment or decrement by (always
3263 positive and ignored for POST_MODIFY/PRE_MODIFY).
3265 Return pseudo containing the result. */
3266 static rtx
3267 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3269 /* REG or MEM to be copied and incremented. */
3270 rtx incloc = XEXP (value, 0);
3271 /* Nonzero if increment after copying. */
3272 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3273 || GET_CODE (value) == POST_MODIFY);
3274 rtx_insn *last;
3275 rtx inc;
3276 rtx_insn *add_insn;
3277 int code;
3278 rtx real_in = in == value ? incloc : in;
3279 rtx result;
3280 bool plus_p = true;
3282 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3284 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3285 || GET_CODE (XEXP (value, 1)) == MINUS);
3286 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3287 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3288 inc = XEXP (XEXP (value, 1), 1);
3290 else
3292 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3293 inc_amount = -inc_amount;
3295 inc = GEN_INT (inc_amount);
3298 if (! post && REG_P (incloc))
3299 result = incloc;
3300 else
3301 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3302 "INC/DEC result");
3304 if (real_in != result)
3306 /* First copy the location to the result register. */
3307 lra_assert (REG_P (result));
3308 emit_insn (gen_move_insn (result, real_in));
3311 /* We suppose that there are insns to add/sub with the constant
3312 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3313 old reload worked with this assumption. If the assumption
3314 becomes wrong, we should use approach in function
3315 base_plus_disp_to_reg. */
3316 if (in == value)
3318 /* See if we can directly increment INCLOC. */
3319 last = get_last_insn ();
3320 add_insn = emit_insn (plus_p
3321 ? gen_add2_insn (incloc, inc)
3322 : gen_sub2_insn (incloc, inc));
3324 code = recog_memoized (add_insn);
3325 if (code >= 0)
3327 if (! post && result != incloc)
3328 emit_insn (gen_move_insn (result, incloc));
3329 return result;
3331 delete_insns_since (last);
3334 /* If couldn't do the increment directly, must increment in RESULT.
3335 The way we do this depends on whether this is pre- or
3336 post-increment. For pre-increment, copy INCLOC to the reload
3337 register, increment it there, then save back. */
3338 if (! post)
3340 if (real_in != result)
3341 emit_insn (gen_move_insn (result, real_in));
3342 if (plus_p)
3343 emit_insn (gen_add2_insn (result, inc));
3344 else
3345 emit_insn (gen_sub2_insn (result, inc));
3346 if (result != incloc)
3347 emit_insn (gen_move_insn (incloc, result));
3349 else
3351 /* Post-increment.
3353 Because this might be a jump insn or a compare, and because
3354 RESULT may not be available after the insn in an input
3355 reload, we must do the incrementing before the insn being
3356 reloaded for.
3358 We have already copied IN to RESULT. Increment the copy in
3359 RESULT, save that back, then decrement RESULT so it has
3360 the original value. */
3361 if (plus_p)
3362 emit_insn (gen_add2_insn (result, inc));
3363 else
3364 emit_insn (gen_sub2_insn (result, inc));
3365 emit_insn (gen_move_insn (incloc, result));
3366 /* Restore non-modified value for the result. We prefer this
3367 way because it does not require an additional hard
3368 register. */
3369 if (plus_p)
3371 if (CONST_INT_P (inc))
3372 emit_insn (gen_add2_insn (result,
3373 gen_int_mode (-INTVAL (inc),
3374 GET_MODE (result))));
3375 else
3376 emit_insn (gen_sub2_insn (result, inc));
3378 else
3379 emit_insn (gen_add2_insn (result, inc));
3381 return result;
3384 /* Return true if the current move insn does not need processing as we
3385 already know that it satisfies its constraints. */
3386 static bool
3387 simple_move_p (void)
3389 rtx dest, src;
3390 enum reg_class dclass, sclass;
3392 lra_assert (curr_insn_set != NULL_RTX);
3393 dest = SET_DEST (curr_insn_set);
3394 src = SET_SRC (curr_insn_set);
3395 return ((dclass = get_op_class (dest)) != NO_REGS
3396 && (sclass = get_op_class (src)) != NO_REGS
3397 /* The backend guarantees that register moves of cost 2
3398 never need reloads. */
3399 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3402 /* Swap operands NOP and NOP + 1. */
3403 static inline void
3404 swap_operands (int nop)
3406 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3407 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3408 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3409 std::swap (equiv_substition_p[nop], equiv_substition_p[nop + 1]);
3410 /* Swap the duplicates too. */
3411 lra_update_dup (curr_id, nop);
3412 lra_update_dup (curr_id, nop + 1);
3415 /* Main entry point of the constraint code: search the body of the
3416 current insn to choose the best alternative. It is mimicking insn
3417 alternative cost calculation model of former reload pass. That is
3418 because machine descriptions were written to use this model. This
3419 model can be changed in future. Make commutative operand exchange
3420 if it is chosen.
3422 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3423 constraints. Return true if any change happened during function
3424 call.
3426 If CHECK_ONLY_P is true then don't do any transformation. Just
3427 check that the insn satisfies all constraints. If the insn does
3428 not satisfy any constraint, return true. */
3429 static bool
3430 curr_insn_transform (bool check_only_p)
3432 int i, j, k;
3433 int n_operands;
3434 int n_alternatives;
3435 int commutative;
3436 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3437 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3438 rtx_insn *before, *after;
3439 bool alt_p = false;
3440 /* Flag that the insn has been changed through a transformation. */
3441 bool change_p;
3442 bool sec_mem_p;
3443 #ifdef SECONDARY_MEMORY_NEEDED
3444 bool use_sec_mem_p;
3445 #endif
3446 int max_regno_before;
3447 int reused_alternative_num;
3449 curr_insn_set = single_set (curr_insn);
3450 if (curr_insn_set != NULL_RTX && simple_move_p ())
3451 return false;
3453 no_input_reloads_p = no_output_reloads_p = false;
3454 goal_alt_number = -1;
3455 change_p = sec_mem_p = false;
3456 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3457 reloads; neither are insns that SET cc0. Insns that use CC0 are
3458 not allowed to have any input reloads. */
3459 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3460 no_output_reloads_p = true;
3462 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3463 no_input_reloads_p = true;
3464 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3465 no_output_reloads_p = true;
3467 n_operands = curr_static_id->n_operands;
3468 n_alternatives = curr_static_id->n_alternatives;
3470 /* Just return "no reloads" if insn has no operands with
3471 constraints. */
3472 if (n_operands == 0 || n_alternatives == 0)
3473 return false;
3475 max_regno_before = max_reg_num ();
3477 for (i = 0; i < n_operands; i++)
3479 goal_alt_matched[i][0] = -1;
3480 goal_alt_matches[i] = -1;
3483 commutative = curr_static_id->commutative;
3485 /* Now see what we need for pseudos that didn't get hard regs or got
3486 the wrong kind of hard reg. For this, we must consider all the
3487 operands together against the register constraints. */
3489 best_losers = best_overall = INT_MAX;
3490 best_reload_sum = 0;
3492 curr_swapped = false;
3493 goal_alt_swapped = false;
3495 if (! check_only_p)
3496 /* Make equivalence substitution and memory subreg elimination
3497 before address processing because an address legitimacy can
3498 depend on memory mode. */
3499 for (i = 0; i < n_operands; i++)
3501 rtx op, subst, old;
3502 bool op_change_p = false;
3504 if (curr_static_id->operand[i].is_operator)
3505 continue;
3507 old = op = *curr_id->operand_loc[i];
3508 if (GET_CODE (old) == SUBREG)
3509 old = SUBREG_REG (old);
3510 subst = get_equiv_with_elimination (old, curr_insn);
3511 original_subreg_reg_mode[i] = VOIDmode;
3512 equiv_substition_p[i] = false;
3513 if (subst != old)
3515 equiv_substition_p[i] = true;
3516 subst = copy_rtx (subst);
3517 lra_assert (REG_P (old));
3518 if (GET_CODE (op) != SUBREG)
3519 *curr_id->operand_loc[i] = subst;
3520 else
3522 SUBREG_REG (op) = subst;
3523 if (GET_MODE (subst) == VOIDmode)
3524 original_subreg_reg_mode[i] = GET_MODE (old);
3526 if (lra_dump_file != NULL)
3528 fprintf (lra_dump_file,
3529 "Changing pseudo %d in operand %i of insn %u on equiv ",
3530 REGNO (old), i, INSN_UID (curr_insn));
3531 dump_value_slim (lra_dump_file, subst, 1);
3532 fprintf (lra_dump_file, "\n");
3534 op_change_p = change_p = true;
3536 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3538 change_p = true;
3539 lra_update_dup (curr_id, i);
3543 /* Reload address registers and displacements. We do it before
3544 finding an alternative because of memory constraints. */
3545 before = after = NULL;
3546 for (i = 0; i < n_operands; i++)
3547 if (! curr_static_id->operand[i].is_operator
3548 && process_address (i, check_only_p, &before, &after))
3550 if (check_only_p)
3551 return true;
3552 change_p = true;
3553 lra_update_dup (curr_id, i);
3556 if (change_p)
3557 /* If we've changed the instruction then any alternative that
3558 we chose previously may no longer be valid. */
3559 lra_set_used_insn_alternative (curr_insn, -1);
3561 if (! check_only_p && curr_insn_set != NULL_RTX
3562 && check_and_process_move (&change_p, &sec_mem_p))
3563 return change_p;
3565 try_swapped:
3567 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3568 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3569 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3570 reused_alternative_num, INSN_UID (curr_insn));
3572 if (process_alt_operands (reused_alternative_num))
3573 alt_p = true;
3575 if (check_only_p)
3576 return ! alt_p || best_losers != 0;
3578 /* If insn is commutative (it's safe to exchange a certain pair of
3579 operands) then we need to try each alternative twice, the second
3580 time matching those two operands as if we had exchanged them. To
3581 do this, really exchange them in operands.
3583 If we have just tried the alternatives the second time, return
3584 operands to normal and drop through. */
3586 if (reused_alternative_num < 0 && commutative >= 0)
3588 curr_swapped = !curr_swapped;
3589 if (curr_swapped)
3591 swap_operands (commutative);
3592 goto try_swapped;
3594 else
3595 swap_operands (commutative);
3598 if (! alt_p && ! sec_mem_p)
3600 /* No alternative works with reloads?? */
3601 if (INSN_CODE (curr_insn) >= 0)
3602 fatal_insn ("unable to generate reloads for:", curr_insn);
3603 error_for_asm (curr_insn,
3604 "inconsistent operand constraints in an %<asm%>");
3605 /* Avoid further trouble with this insn. */
3606 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3607 lra_invalidate_insn_data (curr_insn);
3608 return true;
3611 /* If the best alternative is with operands 1 and 2 swapped, swap
3612 them. Update the operand numbers of any reloads already
3613 pushed. */
3615 if (goal_alt_swapped)
3617 if (lra_dump_file != NULL)
3618 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3619 INSN_UID (curr_insn));
3621 /* Swap the duplicates too. */
3622 swap_operands (commutative);
3623 change_p = true;
3626 #ifdef SECONDARY_MEMORY_NEEDED
3627 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3628 too conservatively. So we use the secondary memory only if there
3629 is no any alternative without reloads. */
3630 use_sec_mem_p = false;
3631 if (! alt_p)
3632 use_sec_mem_p = true;
3633 else if (sec_mem_p)
3635 for (i = 0; i < n_operands; i++)
3636 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3637 break;
3638 use_sec_mem_p = i < n_operands;
3641 if (use_sec_mem_p)
3643 int in = -1, out = -1;
3644 rtx new_reg, src, dest, rld;
3645 machine_mode sec_mode, rld_mode;
3647 lra_assert (curr_insn_set != NULL_RTX && sec_mem_p);
3648 dest = SET_DEST (curr_insn_set);
3649 src = SET_SRC (curr_insn_set);
3650 for (i = 0; i < n_operands; i++)
3651 if (*curr_id->operand_loc[i] == dest)
3652 out = i;
3653 else if (*curr_id->operand_loc[i] == src)
3654 in = i;
3655 for (i = 0; i < curr_static_id->n_dups; i++)
3656 if (out < 0 && *curr_id->dup_loc[i] == dest)
3657 out = curr_static_id->dup_num[i];
3658 else if (in < 0 && *curr_id->dup_loc[i] == src)
3659 in = curr_static_id->dup_num[i];
3660 lra_assert (out >= 0 && in >= 0
3661 && curr_static_id->operand[out].type == OP_OUT
3662 && curr_static_id->operand[in].type == OP_IN);
3663 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3664 ? dest : src);
3665 rld_mode = GET_MODE (rld);
3666 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3667 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3668 #else
3669 sec_mode = rld_mode;
3670 #endif
3671 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3672 NO_REGS, "secondary");
3673 /* If the mode is changed, it should be wider. */
3674 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3675 if (sec_mode != rld_mode)
3677 /* If the target says specifically to use another mode for
3678 secondary memory moves we can not reuse the original
3679 insn. */
3680 after = emit_spill_move (false, new_reg, dest);
3681 lra_process_new_insns (curr_insn, NULL, after,
3682 "Inserting the sec. move");
3683 /* We may have non null BEFORE here (e.g. after address
3684 processing. */
3685 push_to_sequence (before);
3686 before = emit_spill_move (true, new_reg, src);
3687 emit_insn (before);
3688 before = get_insns ();
3689 end_sequence ();
3690 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3691 lra_set_insn_deleted (curr_insn);
3693 else if (dest == rld)
3695 *curr_id->operand_loc[out] = new_reg;
3696 lra_update_dup (curr_id, out);
3697 after = emit_spill_move (false, new_reg, dest);
3698 lra_process_new_insns (curr_insn, NULL, after,
3699 "Inserting the sec. move");
3701 else
3703 *curr_id->operand_loc[in] = new_reg;
3704 lra_update_dup (curr_id, in);
3705 /* See comments above. */
3706 push_to_sequence (before);
3707 before = emit_spill_move (true, new_reg, src);
3708 emit_insn (before);
3709 before = get_insns ();
3710 end_sequence ();
3711 lra_process_new_insns (curr_insn, before, NULL,
3712 "Inserting the sec. move");
3714 lra_update_insn_regno_info (curr_insn);
3715 return true;
3717 #endif
3719 lra_assert (goal_alt_number >= 0);
3720 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3722 if (lra_dump_file != NULL)
3724 const char *p;
3726 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3727 goal_alt_number, INSN_UID (curr_insn));
3728 for (i = 0; i < n_operands; i++)
3730 p = (curr_static_id->operand_alternative
3731 [goal_alt_number * n_operands + i].constraint);
3732 if (*p == '\0')
3733 continue;
3734 fprintf (lra_dump_file, " (%d) ", i);
3735 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3736 fputc (*p, lra_dump_file);
3738 if (INSN_CODE (curr_insn) >= 0
3739 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3740 fprintf (lra_dump_file, " {%s}", p);
3741 if (curr_id->sp_offset != 0)
3742 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3743 curr_id->sp_offset);
3744 fprintf (lra_dump_file, "\n");
3747 /* Right now, for any pair of operands I and J that are required to
3748 match, with J < I, goal_alt_matches[I] is J. Add I to
3749 goal_alt_matched[J]. */
3751 for (i = 0; i < n_operands; i++)
3752 if ((j = goal_alt_matches[i]) >= 0)
3754 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3756 /* We allow matching one output operand and several input
3757 operands. */
3758 lra_assert (k == 0
3759 || (curr_static_id->operand[j].type == OP_OUT
3760 && curr_static_id->operand[i].type == OP_IN
3761 && (curr_static_id->operand
3762 [goal_alt_matched[j][0]].type == OP_IN)));
3763 goal_alt_matched[j][k] = i;
3764 goal_alt_matched[j][k + 1] = -1;
3767 for (i = 0; i < n_operands; i++)
3768 goal_alt_win[i] |= goal_alt_match_win[i];
3770 /* Any constants that aren't allowed and can't be reloaded into
3771 registers are here changed into memory references. */
3772 for (i = 0; i < n_operands; i++)
3773 if (goal_alt_win[i])
3775 int regno;
3776 enum reg_class new_class;
3777 rtx reg = *curr_id->operand_loc[i];
3779 if (GET_CODE (reg) == SUBREG)
3780 reg = SUBREG_REG (reg);
3782 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3784 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3786 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3788 lra_assert (ok_p);
3789 lra_change_class (regno, new_class, " Change to", true);
3793 else
3795 const char *constraint;
3796 char c;
3797 rtx op = *curr_id->operand_loc[i];
3798 rtx subreg = NULL_RTX;
3799 machine_mode mode = curr_operand_mode[i];
3801 if (GET_CODE (op) == SUBREG)
3803 subreg = op;
3804 op = SUBREG_REG (op);
3805 mode = GET_MODE (op);
3808 if (CONST_POOL_OK_P (mode, op)
3809 && ((targetm.preferred_reload_class
3810 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3811 || no_input_reloads_p))
3813 rtx tem = force_const_mem (mode, op);
3815 change_p = true;
3816 if (subreg != NULL_RTX)
3817 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3819 *curr_id->operand_loc[i] = tem;
3820 lra_update_dup (curr_id, i);
3821 process_address (i, false, &before, &after);
3823 /* If the alternative accepts constant pool refs directly
3824 there will be no reload needed at all. */
3825 if (subreg != NULL_RTX)
3826 continue;
3827 /* Skip alternatives before the one requested. */
3828 constraint = (curr_static_id->operand_alternative
3829 [goal_alt_number * n_operands + i].constraint);
3830 for (;
3831 (c = *constraint) && c != ',' && c != '#';
3832 constraint += CONSTRAINT_LEN (c, constraint))
3834 enum constraint_num cn = lookup_constraint (constraint);
3835 if ((insn_extra_memory_constraint (cn)
3836 || insn_extra_special_memory_constraint (cn))
3837 && satisfies_memory_constraint_p (tem, cn))
3838 break;
3840 if (c == '\0' || c == ',' || c == '#')
3841 continue;
3843 goal_alt_win[i] = true;
3847 for (i = 0; i < n_operands; i++)
3849 int regno;
3850 bool optional_p = false;
3851 rtx old, new_reg;
3852 rtx op = *curr_id->operand_loc[i];
3854 if (goal_alt_win[i])
3856 if (goal_alt[i] == NO_REGS
3857 && REG_P (op)
3858 /* When we assign NO_REGS it means that we will not
3859 assign a hard register to the scratch pseudo by
3860 assigment pass and the scratch pseudo will be
3861 spilled. Spilled scratch pseudos are transformed
3862 back to scratches at the LRA end. */
3863 && lra_former_scratch_operand_p (curr_insn, i)
3864 && lra_former_scratch_p (REGNO (op)))
3866 int regno = REGNO (op);
3867 lra_change_class (regno, NO_REGS, " Change to", true);
3868 if (lra_get_regno_hard_regno (regno) >= 0)
3869 /* We don't have to mark all insn affected by the
3870 spilled pseudo as there is only one such insn, the
3871 current one. */
3872 reg_renumber[regno] = -1;
3873 lra_assert (bitmap_single_bit_set_p
3874 (&lra_reg_info[REGNO (op)].insn_bitmap));
3876 /* We can do an optional reload. If the pseudo got a hard
3877 reg, we might improve the code through inheritance. If
3878 it does not get a hard register we coalesce memory/memory
3879 moves later. Ignore move insns to avoid cycling. */
3880 if (! lra_simple_p
3881 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3882 && goal_alt[i] != NO_REGS && REG_P (op)
3883 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3884 && regno < new_regno_start
3885 && ! lra_former_scratch_p (regno)
3886 && reg_renumber[regno] < 0
3887 /* Check that the optional reload pseudo will be able to
3888 hold given mode value. */
3889 && ! (prohibited_class_reg_set_mode_p
3890 (goal_alt[i], reg_class_contents[goal_alt[i]],
3891 PSEUDO_REGNO_MODE (regno)))
3892 && (curr_insn_set == NULL_RTX
3893 || !((REG_P (SET_SRC (curr_insn_set))
3894 || MEM_P (SET_SRC (curr_insn_set))
3895 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3896 && (REG_P (SET_DEST (curr_insn_set))
3897 || MEM_P (SET_DEST (curr_insn_set))
3898 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3899 optional_p = true;
3900 else
3901 continue;
3904 /* Operands that match previous ones have already been handled. */
3905 if (goal_alt_matches[i] >= 0)
3906 continue;
3908 /* We should not have an operand with a non-offsettable address
3909 appearing where an offsettable address will do. It also may
3910 be a case when the address should be special in other words
3911 not a general one (e.g. it needs no index reg). */
3912 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3914 enum reg_class rclass;
3915 rtx *loc = &XEXP (op, 0);
3916 enum rtx_code code = GET_CODE (*loc);
3918 push_to_sequence (before);
3919 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3920 MEM, SCRATCH);
3921 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3922 new_reg = emit_inc (rclass, *loc, *loc,
3923 /* This value does not matter for MODIFY. */
3924 GET_MODE_SIZE (GET_MODE (op)));
3925 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3926 "offsetable address", &new_reg))
3927 lra_emit_move (new_reg, *loc);
3928 before = get_insns ();
3929 end_sequence ();
3930 *loc = new_reg;
3931 lra_update_dup (curr_id, i);
3933 else if (goal_alt_matched[i][0] == -1)
3935 machine_mode mode;
3936 rtx reg, *loc;
3937 int hard_regno, byte;
3938 enum op_type type = curr_static_id->operand[i].type;
3940 loc = curr_id->operand_loc[i];
3941 mode = curr_operand_mode[i];
3942 if (GET_CODE (*loc) == SUBREG)
3944 reg = SUBREG_REG (*loc);
3945 byte = SUBREG_BYTE (*loc);
3946 if (REG_P (reg)
3947 /* Strict_low_part requires reload the register not
3948 the sub-register. */
3949 && (curr_static_id->operand[i].strict_low
3950 || (GET_MODE_SIZE (mode)
3951 <= GET_MODE_SIZE (GET_MODE (reg))
3952 && (hard_regno
3953 = get_try_hard_regno (REGNO (reg))) >= 0
3954 && (simplify_subreg_regno
3955 (hard_regno,
3956 GET_MODE (reg), byte, mode) < 0)
3957 && (goal_alt[i] == NO_REGS
3958 || (simplify_subreg_regno
3959 (ira_class_hard_regs[goal_alt[i]][0],
3960 GET_MODE (reg), byte, mode) >= 0)))))
3962 if (type == OP_OUT)
3963 type = OP_INOUT;
3964 loc = &SUBREG_REG (*loc);
3965 mode = GET_MODE (*loc);
3968 old = *loc;
3969 if (get_reload_reg (type, mode, old, goal_alt[i],
3970 loc != curr_id->operand_loc[i], "", &new_reg)
3971 && type != OP_OUT)
3973 push_to_sequence (before);
3974 lra_emit_move (new_reg, old);
3975 before = get_insns ();
3976 end_sequence ();
3978 *loc = new_reg;
3979 if (type != OP_IN
3980 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3982 start_sequence ();
3983 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3984 emit_insn (after);
3985 after = get_insns ();
3986 end_sequence ();
3987 *loc = new_reg;
3989 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3990 if (goal_alt_dont_inherit_ops[j] == i)
3992 lra_set_regno_unique_value (REGNO (new_reg));
3993 break;
3995 lra_update_dup (curr_id, i);
3997 else if (curr_static_id->operand[i].type == OP_IN
3998 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3999 == OP_OUT))
4001 /* generate reloads for input and matched outputs. */
4002 match_inputs[0] = i;
4003 match_inputs[1] = -1;
4004 match_reload (goal_alt_matched[i][0], match_inputs,
4005 goal_alt[i], &before, &after,
4006 curr_static_id->operand_alternative
4007 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
4008 .earlyclobber);
4010 else if (curr_static_id->operand[i].type == OP_OUT
4011 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4012 == OP_IN))
4013 /* Generate reloads for output and matched inputs. */
4014 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after,
4015 curr_static_id->operand_alternative
4016 [goal_alt_number * n_operands + i].earlyclobber);
4017 else if (curr_static_id->operand[i].type == OP_IN
4018 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4019 == OP_IN))
4021 /* Generate reloads for matched inputs. */
4022 match_inputs[0] = i;
4023 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
4024 match_inputs[j + 1] = k;
4025 match_inputs[j + 1] = -1;
4026 match_reload (-1, match_inputs, goal_alt[i], &before, &after, false);
4028 else
4029 /* We must generate code in any case when function
4030 process_alt_operands decides that it is possible. */
4031 gcc_unreachable ();
4032 if (optional_p)
4034 lra_assert (REG_P (op));
4035 regno = REGNO (op);
4036 op = *curr_id->operand_loc[i]; /* Substitution. */
4037 if (GET_CODE (op) == SUBREG)
4038 op = SUBREG_REG (op);
4039 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
4040 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
4041 lra_reg_info[REGNO (op)].restore_regno = regno;
4042 if (lra_dump_file != NULL)
4043 fprintf (lra_dump_file,
4044 " Making reload reg %d for reg %d optional\n",
4045 REGNO (op), regno);
4048 if (before != NULL_RTX || after != NULL_RTX
4049 || max_regno_before != max_reg_num ())
4050 change_p = true;
4051 if (change_p)
4053 lra_update_operator_dups (curr_id);
4054 /* Something changes -- process the insn. */
4055 lra_update_insn_regno_info (curr_insn);
4057 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
4058 return change_p;
4061 /* Return true if INSN satisfies all constraints. In other words, no
4062 reload insns are needed. */
4063 bool
4064 lra_constrain_insn (rtx_insn *insn)
4066 int saved_new_regno_start = new_regno_start;
4067 int saved_new_insn_uid_start = new_insn_uid_start;
4068 bool change_p;
4070 curr_insn = insn;
4071 curr_id = lra_get_insn_recog_data (curr_insn);
4072 curr_static_id = curr_id->insn_static_data;
4073 new_insn_uid_start = get_max_uid ();
4074 new_regno_start = max_reg_num ();
4075 change_p = curr_insn_transform (true);
4076 new_regno_start = saved_new_regno_start;
4077 new_insn_uid_start = saved_new_insn_uid_start;
4078 return ! change_p;
4081 /* Return true if X is in LIST. */
4082 static bool
4083 in_list_p (rtx x, rtx list)
4085 for (; list != NULL_RTX; list = XEXP (list, 1))
4086 if (XEXP (list, 0) == x)
4087 return true;
4088 return false;
4091 /* Return true if X contains an allocatable hard register (if
4092 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
4093 static bool
4094 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
4096 int i, j;
4097 const char *fmt;
4098 enum rtx_code code;
4100 code = GET_CODE (x);
4101 if (REG_P (x))
4103 int regno = REGNO (x);
4104 HARD_REG_SET alloc_regs;
4106 if (hard_reg_p)
4108 if (regno >= FIRST_PSEUDO_REGISTER)
4109 regno = lra_get_regno_hard_regno (regno);
4110 if (regno < 0)
4111 return false;
4112 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
4113 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
4115 else
4117 if (regno < FIRST_PSEUDO_REGISTER)
4118 return false;
4119 if (! spilled_p)
4120 return true;
4121 return lra_get_regno_hard_regno (regno) < 0;
4124 fmt = GET_RTX_FORMAT (code);
4125 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4127 if (fmt[i] == 'e')
4129 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4130 return true;
4132 else if (fmt[i] == 'E')
4134 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4135 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4136 return true;
4139 return false;
4142 /* Process all regs in location *LOC and change them on equivalent
4143 substitution. Return true if any change was done. */
4144 static bool
4145 loc_equivalence_change_p (rtx *loc)
4147 rtx subst, reg, x = *loc;
4148 bool result = false;
4149 enum rtx_code code = GET_CODE (x);
4150 const char *fmt;
4151 int i, j;
4153 if (code == SUBREG)
4155 reg = SUBREG_REG (x);
4156 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4157 && GET_MODE (subst) == VOIDmode)
4159 /* We cannot reload debug location. Simplify subreg here
4160 while we know the inner mode. */
4161 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4162 GET_MODE (reg), SUBREG_BYTE (x));
4163 return true;
4166 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4168 *loc = subst;
4169 return true;
4172 /* Scan all the operand sub-expressions. */
4173 fmt = GET_RTX_FORMAT (code);
4174 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4176 if (fmt[i] == 'e')
4177 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4178 else if (fmt[i] == 'E')
4179 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4180 result
4181 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4183 return result;
4186 /* Similar to loc_equivalence_change_p, but for use as
4187 simplify_replace_fn_rtx callback. DATA is insn for which the
4188 elimination is done. If it null we don't do the elimination. */
4189 static rtx
4190 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4192 if (!REG_P (loc))
4193 return NULL_RTX;
4195 rtx subst = (data == NULL
4196 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4197 if (subst != loc)
4198 return subst;
4200 return NULL_RTX;
4203 /* Maximum number of generated reload insns per an insn. It is for
4204 preventing this pass cycling in a bug case. */
4205 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4207 /* The current iteration number of this LRA pass. */
4208 int lra_constraint_iter;
4210 /* True if we substituted equiv which needs checking register
4211 allocation correctness because the equivalent value contains
4212 allocatable hard registers or when we restore multi-register
4213 pseudo. */
4214 bool lra_risky_transformations_p;
4216 /* Return true if REGNO is referenced in more than one block. */
4217 static bool
4218 multi_block_pseudo_p (int regno)
4220 basic_block bb = NULL;
4221 unsigned int uid;
4222 bitmap_iterator bi;
4224 if (regno < FIRST_PSEUDO_REGISTER)
4225 return false;
4227 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4228 if (bb == NULL)
4229 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4230 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4231 return true;
4232 return false;
4235 /* Return true if LIST contains a deleted insn. */
4236 static bool
4237 contains_deleted_insn_p (rtx_insn_list *list)
4239 for (; list != NULL_RTX; list = list->next ())
4240 if (NOTE_P (list->insn ())
4241 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4242 return true;
4243 return false;
4246 /* Return true if X contains a pseudo dying in INSN. */
4247 static bool
4248 dead_pseudo_p (rtx x, rtx_insn *insn)
4250 int i, j;
4251 const char *fmt;
4252 enum rtx_code code;
4254 if (REG_P (x))
4255 return (insn != NULL_RTX
4256 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4257 code = GET_CODE (x);
4258 fmt = GET_RTX_FORMAT (code);
4259 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4261 if (fmt[i] == 'e')
4263 if (dead_pseudo_p (XEXP (x, i), insn))
4264 return true;
4266 else if (fmt[i] == 'E')
4268 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4269 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4270 return true;
4273 return false;
4276 /* Return true if INSN contains a dying pseudo in INSN right hand
4277 side. */
4278 static bool
4279 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4281 rtx set = single_set (insn);
4283 gcc_assert (set != NULL);
4284 return dead_pseudo_p (SET_SRC (set), insn);
4287 /* Return true if any init insn of REGNO contains a dying pseudo in
4288 insn right hand side. */
4289 static bool
4290 init_insn_rhs_dead_pseudo_p (int regno)
4292 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4294 if (insns == NULL)
4295 return false;
4296 for (; insns != NULL_RTX; insns = insns->next ())
4297 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4298 return true;
4299 return false;
4302 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4303 reverse only if we have one init insn with given REGNO as a
4304 source. */
4305 static bool
4306 reverse_equiv_p (int regno)
4308 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4309 rtx set;
4311 if (insns == NULL)
4312 return false;
4313 if (! INSN_P (insns->insn ())
4314 || insns->next () != NULL)
4315 return false;
4316 if ((set = single_set (insns->insn ())) == NULL_RTX)
4317 return false;
4318 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4321 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4322 call this function only for non-reverse equivalence. */
4323 static bool
4324 contains_reloaded_insn_p (int regno)
4326 rtx set;
4327 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4329 for (; list != NULL; list = list->next ())
4330 if ((set = single_set (list->insn ())) == NULL_RTX
4331 || ! REG_P (SET_DEST (set))
4332 || (int) REGNO (SET_DEST (set)) != regno)
4333 return true;
4334 return false;
4337 /* Entry function of LRA constraint pass. Return true if the
4338 constraint pass did change the code. */
4339 bool
4340 lra_constraints (bool first_p)
4342 bool changed_p;
4343 int i, hard_regno, new_insns_num;
4344 unsigned int min_len, new_min_len, uid;
4345 rtx set, x, reg, dest_reg;
4346 basic_block last_bb;
4347 bitmap_head equiv_insn_bitmap;
4348 bitmap_iterator bi;
4350 lra_constraint_iter++;
4351 if (lra_dump_file != NULL)
4352 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4353 lra_constraint_iter);
4354 changed_p = false;
4355 if (pic_offset_table_rtx
4356 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4357 lra_risky_transformations_p = true;
4358 else
4359 lra_risky_transformations_p = false;
4360 new_insn_uid_start = get_max_uid ();
4361 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4362 /* Mark used hard regs for target stack size calulations. */
4363 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4364 if (lra_reg_info[i].nrefs != 0
4365 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4367 int j, nregs;
4369 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4370 for (j = 0; j < nregs; j++)
4371 df_set_regs_ever_live (hard_regno + j, true);
4373 /* Do elimination before the equivalence processing as we can spill
4374 some pseudos during elimination. */
4375 lra_eliminate (false, first_p);
4376 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4377 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4378 if (lra_reg_info[i].nrefs != 0)
4380 ira_reg_equiv[i].profitable_p = true;
4381 reg = regno_reg_rtx[i];
4382 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4384 bool pseudo_p = contains_reg_p (x, false, false);
4386 /* After RTL transformation, we can not guarantee that
4387 pseudo in the substitution was not reloaded which might
4388 make equivalence invalid. For example, in reverse
4389 equiv of p0
4391 p0 <- ...
4393 equiv_mem <- p0
4395 the memory address register was reloaded before the 2nd
4396 insn. */
4397 if ((! first_p && pseudo_p)
4398 /* We don't use DF for compilation speed sake. So it
4399 is problematic to update live info when we use an
4400 equivalence containing pseudos in more than one
4401 BB. */
4402 || (pseudo_p && multi_block_pseudo_p (i))
4403 /* If an init insn was deleted for some reason, cancel
4404 the equiv. We could update the equiv insns after
4405 transformations including an equiv insn deletion
4406 but it is not worthy as such cases are extremely
4407 rare. */
4408 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4409 /* If it is not a reverse equivalence, we check that a
4410 pseudo in rhs of the init insn is not dying in the
4411 insn. Otherwise, the live info at the beginning of
4412 the corresponding BB might be wrong after we
4413 removed the insn. When the equiv can be a
4414 constant, the right hand side of the init insn can
4415 be a pseudo. */
4416 || (! reverse_equiv_p (i)
4417 && (init_insn_rhs_dead_pseudo_p (i)
4418 /* If we reloaded the pseudo in an equivalence
4419 init insn, we can not remove the equiv init
4420 insns and the init insns might write into
4421 const memory in this case. */
4422 || contains_reloaded_insn_p (i)))
4423 /* Prevent access beyond equivalent memory for
4424 paradoxical subregs. */
4425 || (MEM_P (x)
4426 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4427 > GET_MODE_SIZE (GET_MODE (x))))
4428 || (pic_offset_table_rtx
4429 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4430 && (targetm.preferred_reload_class
4431 (x, lra_get_allocno_class (i)) == NO_REGS))
4432 || contains_symbol_ref_p (x))))
4433 ira_reg_equiv[i].defined_p = false;
4434 if (contains_reg_p (x, false, true))
4435 ira_reg_equiv[i].profitable_p = false;
4436 if (get_equiv (reg) != reg)
4437 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4440 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4441 update_equiv (i);
4442 /* We should add all insns containing pseudos which should be
4443 substituted by their equivalences. */
4444 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4445 lra_push_insn_by_uid (uid);
4446 min_len = lra_insn_stack_length ();
4447 new_insns_num = 0;
4448 last_bb = NULL;
4449 changed_p = false;
4450 while ((new_min_len = lra_insn_stack_length ()) != 0)
4452 curr_insn = lra_pop_insn ();
4453 --new_min_len;
4454 curr_bb = BLOCK_FOR_INSN (curr_insn);
4455 if (curr_bb != last_bb)
4457 last_bb = curr_bb;
4458 bb_reload_num = lra_curr_reload_num;
4460 if (min_len > new_min_len)
4462 min_len = new_min_len;
4463 new_insns_num = 0;
4465 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4466 internal_error
4467 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4468 MAX_RELOAD_INSNS_NUMBER);
4469 new_insns_num++;
4470 if (DEBUG_INSN_P (curr_insn))
4472 /* We need to check equivalence in debug insn and change
4473 pseudo to the equivalent value if necessary. */
4474 curr_id = lra_get_insn_recog_data (curr_insn);
4475 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4477 rtx old = *curr_id->operand_loc[0];
4478 *curr_id->operand_loc[0]
4479 = simplify_replace_fn_rtx (old, NULL_RTX,
4480 loc_equivalence_callback, curr_insn);
4481 if (old != *curr_id->operand_loc[0])
4483 lra_update_insn_regno_info (curr_insn);
4484 changed_p = true;
4488 else if (INSN_P (curr_insn))
4490 if ((set = single_set (curr_insn)) != NULL_RTX)
4492 dest_reg = SET_DEST (set);
4493 /* The equivalence pseudo could be set up as SUBREG in a
4494 case when it is a call restore insn in a mode
4495 different from the pseudo mode. */
4496 if (GET_CODE (dest_reg) == SUBREG)
4497 dest_reg = SUBREG_REG (dest_reg);
4498 if ((REG_P (dest_reg)
4499 && (x = get_equiv (dest_reg)) != dest_reg
4500 /* Remove insns which set up a pseudo whose value
4501 can not be changed. Such insns might be not in
4502 init_insns because we don't update equiv data
4503 during insn transformations.
4505 As an example, let suppose that a pseudo got
4506 hard register and on the 1st pass was not
4507 changed to equivalent constant. We generate an
4508 additional insn setting up the pseudo because of
4509 secondary memory movement. Then the pseudo is
4510 spilled and we use the equiv constant. In this
4511 case we should remove the additional insn and
4512 this insn is not init_insns list. */
4513 && (! MEM_P (x) || MEM_READONLY_P (x)
4514 /* Check that this is actually an insn setting
4515 up the equivalence. */
4516 || in_list_p (curr_insn,
4517 ira_reg_equiv
4518 [REGNO (dest_reg)].init_insns)))
4519 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4520 && in_list_p (curr_insn,
4521 ira_reg_equiv
4522 [REGNO (SET_SRC (set))].init_insns)))
4524 /* This is equiv init insn of pseudo which did not get a
4525 hard register -- remove the insn. */
4526 if (lra_dump_file != NULL)
4528 fprintf (lra_dump_file,
4529 " Removing equiv init insn %i (freq=%d)\n",
4530 INSN_UID (curr_insn),
4531 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4532 dump_insn_slim (lra_dump_file, curr_insn);
4534 if (contains_reg_p (x, true, false))
4535 lra_risky_transformations_p = true;
4536 lra_set_insn_deleted (curr_insn);
4537 continue;
4540 curr_id = lra_get_insn_recog_data (curr_insn);
4541 curr_static_id = curr_id->insn_static_data;
4542 init_curr_insn_input_reloads ();
4543 init_curr_operand_mode ();
4544 if (curr_insn_transform (false))
4545 changed_p = true;
4546 /* Check non-transformed insns too for equiv change as USE
4547 or CLOBBER don't need reloads but can contain pseudos
4548 being changed on their equivalences. */
4549 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4550 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4552 lra_update_insn_regno_info (curr_insn);
4553 changed_p = true;
4557 bitmap_clear (&equiv_insn_bitmap);
4558 /* If we used a new hard regno, changed_p should be true because the
4559 hard reg is assigned to a new pseudo. */
4560 if (flag_checking && !changed_p)
4562 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4563 if (lra_reg_info[i].nrefs != 0
4564 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4566 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4568 for (j = 0; j < nregs; j++)
4569 lra_assert (df_regs_ever_live_p (hard_regno + j));
4572 return changed_p;
4575 /* Initiate the LRA constraint pass. It is done once per
4576 function. */
4577 void
4578 lra_constraints_init (void)
4582 /* Finalize the LRA constraint pass. It is done once per
4583 function. */
4584 void
4585 lra_constraints_finish (void)
4591 /* This page contains code to do inheritance/split
4592 transformations. */
4594 /* Number of reloads passed so far in current EBB. */
4595 static int reloads_num;
4597 /* Number of calls passed so far in current EBB. */
4598 static int calls_num;
4600 /* Current reload pseudo check for validity of elements in
4601 USAGE_INSNS. */
4602 static int curr_usage_insns_check;
4604 /* Info about last usage of registers in EBB to do inheritance/split
4605 transformation. Inheritance transformation is done from a spilled
4606 pseudo and split transformations from a hard register or a pseudo
4607 assigned to a hard register. */
4608 struct usage_insns
4610 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4611 value INSNS is valid. The insns is chain of optional debug insns
4612 and a finishing non-debug insn using the corresponding reg. The
4613 value is also used to mark the registers which are set up in the
4614 current insn. The negated insn uid is used for this. */
4615 int check;
4616 /* Value of global reloads_num at the last insn in INSNS. */
4617 int reloads_num;
4618 /* Value of global reloads_nums at the last insn in INSNS. */
4619 int calls_num;
4620 /* It can be true only for splitting. And it means that the restore
4621 insn should be put after insn given by the following member. */
4622 bool after_p;
4623 /* Next insns in the current EBB which use the original reg and the
4624 original reg value is not changed between the current insn and
4625 the next insns. In order words, e.g. for inheritance, if we need
4626 to use the original reg value again in the next insns we can try
4627 to use the value in a hard register from a reload insn of the
4628 current insn. */
4629 rtx insns;
4632 /* Map: regno -> corresponding pseudo usage insns. */
4633 static struct usage_insns *usage_insns;
4635 static void
4636 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4638 usage_insns[regno].check = curr_usage_insns_check;
4639 usage_insns[regno].insns = insn;
4640 usage_insns[regno].reloads_num = reloads_num;
4641 usage_insns[regno].calls_num = calls_num;
4642 usage_insns[regno].after_p = after_p;
4645 /* The function is used to form list REGNO usages which consists of
4646 optional debug insns finished by a non-debug insn using REGNO.
4647 RELOADS_NUM is current number of reload insns processed so far. */
4648 static void
4649 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
4651 rtx next_usage_insns;
4653 if (usage_insns[regno].check == curr_usage_insns_check
4654 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4655 && DEBUG_INSN_P (insn))
4657 /* Check that we did not add the debug insn yet. */
4658 if (next_usage_insns != insn
4659 && (GET_CODE (next_usage_insns) != INSN_LIST
4660 || XEXP (next_usage_insns, 0) != insn))
4661 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4662 next_usage_insns);
4664 else if (NONDEBUG_INSN_P (insn))
4665 setup_next_usage_insn (regno, insn, reloads_num, false);
4666 else
4667 usage_insns[regno].check = 0;
4670 /* Return first non-debug insn in list USAGE_INSNS. */
4671 static rtx_insn *
4672 skip_usage_debug_insns (rtx usage_insns)
4674 rtx insn;
4676 /* Skip debug insns. */
4677 for (insn = usage_insns;
4678 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4679 insn = XEXP (insn, 1))
4681 return safe_as_a <rtx_insn *> (insn);
4684 /* Return true if we need secondary memory moves for insn in
4685 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4686 into the insn. */
4687 static bool
4688 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4689 rtx usage_insns ATTRIBUTE_UNUSED)
4691 #ifndef SECONDARY_MEMORY_NEEDED
4692 return false;
4693 #else
4694 rtx_insn *insn;
4695 rtx set, dest;
4696 enum reg_class cl;
4698 if (inher_cl == ALL_REGS
4699 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4700 return false;
4701 lra_assert (INSN_P (insn));
4702 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4703 return false;
4704 dest = SET_DEST (set);
4705 if (! REG_P (dest))
4706 return false;
4707 lra_assert (inher_cl != NO_REGS);
4708 cl = get_reg_class (REGNO (dest));
4709 return (cl != NO_REGS && cl != ALL_REGS
4710 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4711 #endif
4714 /* Registers involved in inheritance/split in the current EBB
4715 (inheritance/split pseudos and original registers). */
4716 static bitmap_head check_only_regs;
4718 /* Do inheritance transformations for insn INSN, which defines (if
4719 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4720 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4721 form as the "insns" field of usage_insns. Return true if we
4722 succeed in such transformation.
4724 The transformations look like:
4726 p <- ... i <- ...
4727 ... p <- i (new insn)
4728 ... =>
4729 <- ... p ... <- ... i ...
4731 ... i <- p (new insn)
4732 <- ... p ... <- ... i ...
4733 ... =>
4734 <- ... p ... <- ... i ...
4735 where p is a spilled original pseudo and i is a new inheritance pseudo.
4738 The inheritance pseudo has the smallest class of two classes CL and
4739 class of ORIGINAL REGNO. */
4740 static bool
4741 inherit_reload_reg (bool def_p, int original_regno,
4742 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4744 if (optimize_function_for_size_p (cfun))
4745 return false;
4747 enum reg_class rclass = lra_get_allocno_class (original_regno);
4748 rtx original_reg = regno_reg_rtx[original_regno];
4749 rtx new_reg, usage_insn;
4750 rtx_insn *new_insns;
4752 lra_assert (! usage_insns[original_regno].after_p);
4753 if (lra_dump_file != NULL)
4754 fprintf (lra_dump_file,
4755 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4756 if (! ira_reg_classes_intersect_p[cl][rclass])
4758 if (lra_dump_file != NULL)
4760 fprintf (lra_dump_file,
4761 " Rejecting inheritance for %d "
4762 "because of disjoint classes %s and %s\n",
4763 original_regno, reg_class_names[cl],
4764 reg_class_names[rclass]);
4765 fprintf (lra_dump_file,
4766 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4768 return false;
4770 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4771 /* We don't use a subset of two classes because it can be
4772 NO_REGS. This transformation is still profitable in most
4773 cases even if the classes are not intersected as register
4774 move is probably cheaper than a memory load. */
4775 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4777 if (lra_dump_file != NULL)
4778 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4779 reg_class_names[cl], reg_class_names[rclass]);
4781 rclass = cl;
4783 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4785 /* Reject inheritance resulting in secondary memory moves.
4786 Otherwise, there is a danger in LRA cycling. Also such
4787 transformation will be unprofitable. */
4788 if (lra_dump_file != NULL)
4790 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4791 rtx set = single_set (insn);
4793 lra_assert (set != NULL_RTX);
4795 rtx dest = SET_DEST (set);
4797 lra_assert (REG_P (dest));
4798 fprintf (lra_dump_file,
4799 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4800 "as secondary mem is needed\n",
4801 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4802 original_regno, reg_class_names[rclass]);
4803 fprintf (lra_dump_file,
4804 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4806 return false;
4808 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4809 rclass, "inheritance");
4810 start_sequence ();
4811 if (def_p)
4812 lra_emit_move (original_reg, new_reg);
4813 else
4814 lra_emit_move (new_reg, original_reg);
4815 new_insns = get_insns ();
4816 end_sequence ();
4817 if (NEXT_INSN (new_insns) != NULL_RTX)
4819 if (lra_dump_file != NULL)
4821 fprintf (lra_dump_file,
4822 " Rejecting inheritance %d->%d "
4823 "as it results in 2 or more insns:\n",
4824 original_regno, REGNO (new_reg));
4825 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4826 fprintf (lra_dump_file,
4827 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4829 return false;
4831 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
4832 lra_update_insn_regno_info (insn);
4833 if (! def_p)
4834 /* We now have a new usage insn for original regno. */
4835 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4836 if (lra_dump_file != NULL)
4837 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4838 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4839 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4840 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4841 bitmap_set_bit (&check_only_regs, original_regno);
4842 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4843 if (def_p)
4844 lra_process_new_insns (insn, NULL, new_insns,
4845 "Add original<-inheritance");
4846 else
4847 lra_process_new_insns (insn, new_insns, NULL,
4848 "Add inheritance<-original");
4849 while (next_usage_insns != NULL_RTX)
4851 if (GET_CODE (next_usage_insns) != INSN_LIST)
4853 usage_insn = next_usage_insns;
4854 lra_assert (NONDEBUG_INSN_P (usage_insn));
4855 next_usage_insns = NULL;
4857 else
4859 usage_insn = XEXP (next_usage_insns, 0);
4860 lra_assert (DEBUG_INSN_P (usage_insn));
4861 next_usage_insns = XEXP (next_usage_insns, 1);
4863 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
4864 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4865 if (lra_dump_file != NULL)
4867 fprintf (lra_dump_file,
4868 " Inheritance reuse change %d->%d (bb%d):\n",
4869 original_regno, REGNO (new_reg),
4870 BLOCK_FOR_INSN (usage_insn)->index);
4871 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
4874 if (lra_dump_file != NULL)
4875 fprintf (lra_dump_file,
4876 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4877 return true;
4880 /* Return true if we need a caller save/restore for pseudo REGNO which
4881 was assigned to a hard register. */
4882 static inline bool
4883 need_for_call_save_p (int regno)
4885 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4886 return (usage_insns[regno].calls_num < calls_num
4887 && (overlaps_hard_reg_set_p
4888 ((flag_ipa_ra &&
4889 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4890 ? lra_reg_info[regno].actual_call_used_reg_set
4891 : call_used_reg_set,
4892 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4893 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4894 PSEUDO_REGNO_MODE (regno))));
4897 /* Global registers occurring in the current EBB. */
4898 static bitmap_head ebb_global_regs;
4900 /* Return true if we need a split for hard register REGNO or pseudo
4901 REGNO which was assigned to a hard register.
4902 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4903 used for reloads since the EBB end. It is an approximation of the
4904 used hard registers in the split range. The exact value would
4905 require expensive calculations. If we were aggressive with
4906 splitting because of the approximation, the split pseudo will save
4907 the same hard register assignment and will be removed in the undo
4908 pass. We still need the approximation because too aggressive
4909 splitting would result in too inaccurate cost calculation in the
4910 assignment pass because of too many generated moves which will be
4911 probably removed in the undo pass. */
4912 static inline bool
4913 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4915 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4917 lra_assert (hard_regno >= 0);
4918 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4919 /* Don't split eliminable hard registers, otherwise we can
4920 split hard registers like hard frame pointer, which
4921 lives on BB start/end according to DF-infrastructure,
4922 when there is a pseudo assigned to the register and
4923 living in the same BB. */
4924 && (regno >= FIRST_PSEUDO_REGISTER
4925 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4926 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4927 /* Don't split call clobbered hard regs living through
4928 calls, otherwise we might have a check problem in the
4929 assign sub-pass as in the most cases (exception is a
4930 situation when lra_risky_transformations_p value is
4931 true) the assign pass assumes that all pseudos living
4932 through calls are assigned to call saved hard regs. */
4933 && (regno >= FIRST_PSEUDO_REGISTER
4934 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4935 || usage_insns[regno].calls_num == calls_num)
4936 /* We need at least 2 reloads to make pseudo splitting
4937 profitable. We should provide hard regno splitting in
4938 any case to solve 1st insn scheduling problem when
4939 moving hard register definition up might result in
4940 impossibility to find hard register for reload pseudo of
4941 small register class. */
4942 && (usage_insns[regno].reloads_num
4943 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4944 && (regno < FIRST_PSEUDO_REGISTER
4945 /* For short living pseudos, spilling + inheritance can
4946 be considered a substitution for splitting.
4947 Therefore we do not splitting for local pseudos. It
4948 decreases also aggressiveness of splitting. The
4949 minimal number of references is chosen taking into
4950 account that for 2 references splitting has no sense
4951 as we can just spill the pseudo. */
4952 || (regno >= FIRST_PSEUDO_REGISTER
4953 && lra_reg_info[regno].nrefs > 3
4954 && bitmap_bit_p (&ebb_global_regs, regno))))
4955 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4958 /* Return class for the split pseudo created from original pseudo with
4959 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4960 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4961 results in no secondary memory movements. */
4962 static enum reg_class
4963 choose_split_class (enum reg_class allocno_class,
4964 int hard_regno ATTRIBUTE_UNUSED,
4965 machine_mode mode ATTRIBUTE_UNUSED)
4967 #ifndef SECONDARY_MEMORY_NEEDED
4968 return allocno_class;
4969 #else
4970 int i;
4971 enum reg_class cl, best_cl = NO_REGS;
4972 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4973 = REGNO_REG_CLASS (hard_regno);
4975 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4976 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4977 return allocno_class;
4978 for (i = 0;
4979 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4980 i++)
4981 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4982 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4983 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4984 && (best_cl == NO_REGS
4985 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4986 best_cl = cl;
4987 return best_cl;
4988 #endif
4991 /* Do split transformations for insn INSN, which defines or uses
4992 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4993 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4994 "insns" field of usage_insns.
4996 The transformations look like:
4998 p <- ... p <- ...
4999 ... s <- p (new insn -- save)
5000 ... =>
5001 ... p <- s (new insn -- restore)
5002 <- ... p ... <- ... p ...
5004 <- ... p ... <- ... p ...
5005 ... s <- p (new insn -- save)
5006 ... =>
5007 ... p <- s (new insn -- restore)
5008 <- ... p ... <- ... p ...
5010 where p is an original pseudo got a hard register or a hard
5011 register and s is a new split pseudo. The save is put before INSN
5012 if BEFORE_P is true. Return true if we succeed in such
5013 transformation. */
5014 static bool
5015 split_reg (bool before_p, int original_regno, rtx_insn *insn,
5016 rtx next_usage_insns)
5018 enum reg_class rclass;
5019 rtx original_reg;
5020 int hard_regno, nregs;
5021 rtx new_reg, usage_insn;
5022 rtx_insn *restore, *save;
5023 bool after_p;
5024 bool call_save_p;
5025 machine_mode mode;
5027 if (original_regno < FIRST_PSEUDO_REGISTER)
5029 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
5030 hard_regno = original_regno;
5031 call_save_p = false;
5032 nregs = 1;
5033 mode = lra_reg_info[hard_regno].biggest_mode;
5034 machine_mode reg_rtx_mode = GET_MODE (regno_reg_rtx[hard_regno]);
5035 /* A reg can have a biggest_mode of VOIDmode if it was only ever seen
5036 as part of a multi-word register. In that case, or if the biggest
5037 mode was larger than a register, just use the reg_rtx. Otherwise,
5038 limit the size to that of the biggest access in the function. */
5039 if (mode == VOIDmode
5040 || GET_MODE_SIZE (mode) > GET_MODE_SIZE (reg_rtx_mode))
5042 original_reg = regno_reg_rtx[hard_regno];
5043 mode = reg_rtx_mode;
5045 else
5046 original_reg = gen_rtx_REG (mode, hard_regno);
5048 else
5050 mode = PSEUDO_REGNO_MODE (original_regno);
5051 hard_regno = reg_renumber[original_regno];
5052 nregs = hard_regno_nregs[hard_regno][mode];
5053 rclass = lra_get_allocno_class (original_regno);
5054 original_reg = regno_reg_rtx[original_regno];
5055 call_save_p = need_for_call_save_p (original_regno);
5057 lra_assert (hard_regno >= 0);
5058 if (lra_dump_file != NULL)
5059 fprintf (lra_dump_file,
5060 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
5062 if (call_save_p)
5064 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
5065 hard_regno_nregs[hard_regno][mode],
5066 mode);
5067 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
5069 else
5071 rclass = choose_split_class (rclass, hard_regno, mode);
5072 if (rclass == NO_REGS)
5074 if (lra_dump_file != NULL)
5076 fprintf (lra_dump_file,
5077 " Rejecting split of %d(%s): "
5078 "no good reg class for %d(%s)\n",
5079 original_regno,
5080 reg_class_names[lra_get_allocno_class (original_regno)],
5081 hard_regno,
5082 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
5083 fprintf
5084 (lra_dump_file,
5085 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5087 return false;
5089 new_reg = lra_create_new_reg (mode, original_reg, rclass, "split");
5090 reg_renumber[REGNO (new_reg)] = hard_regno;
5092 save = emit_spill_move (true, new_reg, original_reg);
5093 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
5095 if (lra_dump_file != NULL)
5097 fprintf
5098 (lra_dump_file,
5099 " Rejecting split %d->%d resulting in > 2 save insns:\n",
5100 original_regno, REGNO (new_reg));
5101 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
5102 fprintf (lra_dump_file,
5103 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5105 return false;
5107 restore = emit_spill_move (false, new_reg, original_reg);
5108 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
5110 if (lra_dump_file != NULL)
5112 fprintf (lra_dump_file,
5113 " Rejecting split %d->%d "
5114 "resulting in > 2 restore insns:\n",
5115 original_regno, REGNO (new_reg));
5116 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
5117 fprintf (lra_dump_file,
5118 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5120 return false;
5122 after_p = usage_insns[original_regno].after_p;
5123 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
5124 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5125 bitmap_set_bit (&check_only_regs, original_regno);
5126 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
5127 for (;;)
5129 if (GET_CODE (next_usage_insns) != INSN_LIST)
5131 usage_insn = next_usage_insns;
5132 break;
5134 usage_insn = XEXP (next_usage_insns, 0);
5135 lra_assert (DEBUG_INSN_P (usage_insn));
5136 next_usage_insns = XEXP (next_usage_insns, 1);
5137 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5138 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5139 if (lra_dump_file != NULL)
5141 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5142 original_regno, REGNO (new_reg));
5143 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5146 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5147 lra_assert (usage_insn != insn || (after_p && before_p));
5148 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5149 after_p ? NULL : restore,
5150 after_p ? restore : NULL,
5151 call_save_p
5152 ? "Add reg<-save" : "Add reg<-split");
5153 lra_process_new_insns (insn, before_p ? save : NULL,
5154 before_p ? NULL : save,
5155 call_save_p
5156 ? "Add save<-reg" : "Add split<-reg");
5157 if (nregs > 1)
5158 /* If we are trying to split multi-register. We should check
5159 conflicts on the next assignment sub-pass. IRA can allocate on
5160 sub-register levels, LRA do this on pseudos level right now and
5161 this discrepancy may create allocation conflicts after
5162 splitting. */
5163 lra_risky_transformations_p = true;
5164 if (lra_dump_file != NULL)
5165 fprintf (lra_dump_file,
5166 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5167 return true;
5170 /* Recognize that we need a split transformation for insn INSN, which
5171 defines or uses REGNO in its insn biggest MODE (we use it only if
5172 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5173 hard registers which might be used for reloads since the EBB end.
5174 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5175 uid before starting INSN processing. Return true if we succeed in
5176 such transformation. */
5177 static bool
5178 split_if_necessary (int regno, machine_mode mode,
5179 HARD_REG_SET potential_reload_hard_regs,
5180 bool before_p, rtx_insn *insn, int max_uid)
5182 bool res = false;
5183 int i, nregs = 1;
5184 rtx next_usage_insns;
5186 if (regno < FIRST_PSEUDO_REGISTER)
5187 nregs = hard_regno_nregs[regno][mode];
5188 for (i = 0; i < nregs; i++)
5189 if (usage_insns[regno + i].check == curr_usage_insns_check
5190 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5191 /* To avoid processing the register twice or more. */
5192 && ((GET_CODE (next_usage_insns) != INSN_LIST
5193 && INSN_UID (next_usage_insns) < max_uid)
5194 || (GET_CODE (next_usage_insns) == INSN_LIST
5195 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5196 && need_for_split_p (potential_reload_hard_regs, regno + i)
5197 && split_reg (before_p, regno + i, insn, next_usage_insns))
5198 res = true;
5199 return res;
5202 /* Check only registers living at the current program point in the
5203 current EBB. */
5204 static bitmap_head live_regs;
5206 /* Update live info in EBB given by its HEAD and TAIL insns after
5207 inheritance/split transformation. The function removes dead moves
5208 too. */
5209 static void
5210 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5212 unsigned int j;
5213 int i, regno;
5214 bool live_p;
5215 rtx_insn *prev_insn;
5216 rtx set;
5217 bool remove_p;
5218 basic_block last_bb, prev_bb, curr_bb;
5219 bitmap_iterator bi;
5220 struct lra_insn_reg *reg;
5221 edge e;
5222 edge_iterator ei;
5224 last_bb = BLOCK_FOR_INSN (tail);
5225 prev_bb = NULL;
5226 for (curr_insn = tail;
5227 curr_insn != PREV_INSN (head);
5228 curr_insn = prev_insn)
5230 prev_insn = PREV_INSN (curr_insn);
5231 /* We need to process empty blocks too. They contain
5232 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5233 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5234 continue;
5235 curr_bb = BLOCK_FOR_INSN (curr_insn);
5236 if (curr_bb != prev_bb)
5238 if (prev_bb != NULL)
5240 /* Update df_get_live_in (prev_bb): */
5241 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5242 if (bitmap_bit_p (&live_regs, j))
5243 bitmap_set_bit (df_get_live_in (prev_bb), j);
5244 else
5245 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5247 if (curr_bb != last_bb)
5249 /* Update df_get_live_out (curr_bb): */
5250 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5252 live_p = bitmap_bit_p (&live_regs, j);
5253 if (! live_p)
5254 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5255 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5257 live_p = true;
5258 break;
5260 if (live_p)
5261 bitmap_set_bit (df_get_live_out (curr_bb), j);
5262 else
5263 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5266 prev_bb = curr_bb;
5267 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5269 if (! NONDEBUG_INSN_P (curr_insn))
5270 continue;
5271 curr_id = lra_get_insn_recog_data (curr_insn);
5272 curr_static_id = curr_id->insn_static_data;
5273 remove_p = false;
5274 if ((set = single_set (curr_insn)) != NULL_RTX
5275 && REG_P (SET_DEST (set))
5276 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5277 && SET_DEST (set) != pic_offset_table_rtx
5278 && bitmap_bit_p (&check_only_regs, regno)
5279 && ! bitmap_bit_p (&live_regs, regno))
5280 remove_p = true;
5281 /* See which defined values die here. */
5282 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5283 if (reg->type == OP_OUT && ! reg->subreg_p)
5284 bitmap_clear_bit (&live_regs, reg->regno);
5285 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5286 if (reg->type == OP_OUT && ! reg->subreg_p)
5287 bitmap_clear_bit (&live_regs, reg->regno);
5288 if (curr_id->arg_hard_regs != NULL)
5289 /* Make clobbered argument hard registers die. */
5290 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5291 if (regno >= FIRST_PSEUDO_REGISTER)
5292 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
5293 /* Mark each used value as live. */
5294 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5295 if (reg->type != OP_OUT
5296 && bitmap_bit_p (&check_only_regs, reg->regno))
5297 bitmap_set_bit (&live_regs, reg->regno);
5298 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5299 if (reg->type != OP_OUT
5300 && bitmap_bit_p (&check_only_regs, reg->regno))
5301 bitmap_set_bit (&live_regs, reg->regno);
5302 if (curr_id->arg_hard_regs != NULL)
5303 /* Make used argument hard registers live. */
5304 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5305 if (regno < FIRST_PSEUDO_REGISTER
5306 && bitmap_bit_p (&check_only_regs, regno))
5307 bitmap_set_bit (&live_regs, regno);
5308 /* It is quite important to remove dead move insns because it
5309 means removing dead store. We don't need to process them for
5310 constraints. */
5311 if (remove_p)
5313 if (lra_dump_file != NULL)
5315 fprintf (lra_dump_file, " Removing dead insn:\n ");
5316 dump_insn_slim (lra_dump_file, curr_insn);
5318 lra_set_insn_deleted (curr_insn);
5323 /* The structure describes info to do an inheritance for the current
5324 insn. We need to collect such info first before doing the
5325 transformations because the transformations change the insn
5326 internal representation. */
5327 struct to_inherit
5329 /* Original regno. */
5330 int regno;
5331 /* Subsequent insns which can inherit original reg value. */
5332 rtx insns;
5335 /* Array containing all info for doing inheritance from the current
5336 insn. */
5337 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5339 /* Number elements in the previous array. */
5340 static int to_inherit_num;
5342 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5343 structure to_inherit. */
5344 static void
5345 add_to_inherit (int regno, rtx insns)
5347 int i;
5349 for (i = 0; i < to_inherit_num; i++)
5350 if (to_inherit[i].regno == regno)
5351 return;
5352 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5353 to_inherit[to_inherit_num].regno = regno;
5354 to_inherit[to_inherit_num++].insns = insns;
5357 /* Return the last non-debug insn in basic block BB, or the block begin
5358 note if none. */
5359 static rtx_insn *
5360 get_last_insertion_point (basic_block bb)
5362 rtx_insn *insn;
5364 FOR_BB_INSNS_REVERSE (bb, insn)
5365 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5366 return insn;
5367 gcc_unreachable ();
5370 /* Set up RES by registers living on edges FROM except the edge (FROM,
5371 TO) or by registers set up in a jump insn in BB FROM. */
5372 static void
5373 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5375 rtx_insn *last;
5376 struct lra_insn_reg *reg;
5377 edge e;
5378 edge_iterator ei;
5380 lra_assert (to != NULL);
5381 bitmap_clear (res);
5382 FOR_EACH_EDGE (e, ei, from->succs)
5383 if (e->dest != to)
5384 bitmap_ior_into (res, df_get_live_in (e->dest));
5385 last = get_last_insertion_point (from);
5386 if (! JUMP_P (last))
5387 return;
5388 curr_id = lra_get_insn_recog_data (last);
5389 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5390 if (reg->type != OP_IN)
5391 bitmap_set_bit (res, reg->regno);
5394 /* Used as a temporary results of some bitmap calculations. */
5395 static bitmap_head temp_bitmap;
5397 /* We split for reloads of small class of hard regs. The following
5398 defines how many hard regs the class should have to be qualified as
5399 small. The code is mostly oriented to x86/x86-64 architecture
5400 where some insns need to use only specific register or pair of
5401 registers and these register can live in RTL explicitly, e.g. for
5402 parameter passing. */
5403 static const int max_small_class_regs_num = 2;
5405 /* Do inheritance/split transformations in EBB starting with HEAD and
5406 finishing on TAIL. We process EBB insns in the reverse order.
5407 Return true if we did any inheritance/split transformation in the
5408 EBB.
5410 We should avoid excessive splitting which results in worse code
5411 because of inaccurate cost calculations for spilling new split
5412 pseudos in such case. To achieve this we do splitting only if
5413 register pressure is high in given basic block and there are reload
5414 pseudos requiring hard registers. We could do more register
5415 pressure calculations at any given program point to avoid necessary
5416 splitting even more but it is to expensive and the current approach
5417 works well enough. */
5418 static bool
5419 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5421 int i, src_regno, dst_regno, nregs;
5422 bool change_p, succ_p, update_reloads_num_p;
5423 rtx_insn *prev_insn, *last_insn;
5424 rtx next_usage_insns, set;
5425 enum reg_class cl;
5426 struct lra_insn_reg *reg;
5427 basic_block last_processed_bb, curr_bb = NULL;
5428 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5429 bitmap to_process;
5430 unsigned int j;
5431 bitmap_iterator bi;
5432 bool head_p, after_p;
5434 change_p = false;
5435 curr_usage_insns_check++;
5436 reloads_num = calls_num = 0;
5437 bitmap_clear (&check_only_regs);
5438 last_processed_bb = NULL;
5439 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5440 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5441 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5442 /* We don't process new insns generated in the loop. */
5443 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5445 prev_insn = PREV_INSN (curr_insn);
5446 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5447 curr_bb = BLOCK_FOR_INSN (curr_insn);
5448 if (last_processed_bb != curr_bb)
5450 /* We are at the end of BB. Add qualified living
5451 pseudos for potential splitting. */
5452 to_process = df_get_live_out (curr_bb);
5453 if (last_processed_bb != NULL)
5455 /* We are somewhere in the middle of EBB. */
5456 get_live_on_other_edges (curr_bb, last_processed_bb,
5457 &temp_bitmap);
5458 to_process = &temp_bitmap;
5460 last_processed_bb = curr_bb;
5461 last_insn = get_last_insertion_point (curr_bb);
5462 after_p = (! JUMP_P (last_insn)
5463 && (! CALL_P (last_insn)
5464 || (find_reg_note (last_insn,
5465 REG_NORETURN, NULL_RTX) == NULL_RTX
5466 && ! SIBLING_CALL_P (last_insn))));
5467 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5468 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5470 if ((int) j >= lra_constraint_new_regno_start)
5471 break;
5472 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5474 if (j < FIRST_PSEUDO_REGISTER)
5475 SET_HARD_REG_BIT (live_hard_regs, j);
5476 else
5477 add_to_hard_reg_set (&live_hard_regs,
5478 PSEUDO_REGNO_MODE (j),
5479 reg_renumber[j]);
5480 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5484 src_regno = dst_regno = -1;
5485 if (NONDEBUG_INSN_P (curr_insn)
5486 && (set = single_set (curr_insn)) != NULL_RTX
5487 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5489 src_regno = REGNO (SET_SRC (set));
5490 dst_regno = REGNO (SET_DEST (set));
5492 update_reloads_num_p = true;
5493 if (src_regno < lra_constraint_new_regno_start
5494 && src_regno >= FIRST_PSEUDO_REGISTER
5495 && reg_renumber[src_regno] < 0
5496 && dst_regno >= lra_constraint_new_regno_start
5497 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5499 /* 'reload_pseudo <- original_pseudo'. */
5500 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5501 reloads_num++;
5502 update_reloads_num_p = false;
5503 succ_p = false;
5504 if (usage_insns[src_regno].check == curr_usage_insns_check
5505 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5506 succ_p = inherit_reload_reg (false, src_regno, cl,
5507 curr_insn, next_usage_insns);
5508 if (succ_p)
5509 change_p = true;
5510 else
5511 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5512 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5513 IOR_HARD_REG_SET (potential_reload_hard_regs,
5514 reg_class_contents[cl]);
5516 else if (src_regno >= lra_constraint_new_regno_start
5517 && dst_regno < lra_constraint_new_regno_start
5518 && dst_regno >= FIRST_PSEUDO_REGISTER
5519 && reg_renumber[dst_regno] < 0
5520 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5521 && usage_insns[dst_regno].check == curr_usage_insns_check
5522 && (next_usage_insns
5523 = usage_insns[dst_regno].insns) != NULL_RTX)
5525 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5526 reloads_num++;
5527 update_reloads_num_p = false;
5528 /* 'original_pseudo <- reload_pseudo'. */
5529 if (! JUMP_P (curr_insn)
5530 && inherit_reload_reg (true, dst_regno, cl,
5531 curr_insn, next_usage_insns))
5532 change_p = true;
5533 /* Invalidate. */
5534 usage_insns[dst_regno].check = 0;
5535 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5536 IOR_HARD_REG_SET (potential_reload_hard_regs,
5537 reg_class_contents[cl]);
5539 else if (INSN_P (curr_insn))
5541 int iter;
5542 int max_uid = get_max_uid ();
5544 curr_id = lra_get_insn_recog_data (curr_insn);
5545 curr_static_id = curr_id->insn_static_data;
5546 to_inherit_num = 0;
5547 /* Process insn definitions. */
5548 for (iter = 0; iter < 2; iter++)
5549 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5550 reg != NULL;
5551 reg = reg->next)
5552 if (reg->type != OP_IN
5553 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5555 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5556 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5557 && usage_insns[dst_regno].check == curr_usage_insns_check
5558 && (next_usage_insns
5559 = usage_insns[dst_regno].insns) != NULL_RTX)
5561 struct lra_insn_reg *r;
5563 for (r = curr_id->regs; r != NULL; r = r->next)
5564 if (r->type != OP_OUT && r->regno == dst_regno)
5565 break;
5566 /* Don't do inheritance if the pseudo is also
5567 used in the insn. */
5568 if (r == NULL)
5569 /* We can not do inheritance right now
5570 because the current insn reg info (chain
5571 regs) can change after that. */
5572 add_to_inherit (dst_regno, next_usage_insns);
5574 /* We can not process one reg twice here because of
5575 usage_insns invalidation. */
5576 if ((dst_regno < FIRST_PSEUDO_REGISTER
5577 || reg_renumber[dst_regno] >= 0)
5578 && ! reg->subreg_p && reg->type != OP_IN)
5580 HARD_REG_SET s;
5582 if (split_if_necessary (dst_regno, reg->biggest_mode,
5583 potential_reload_hard_regs,
5584 false, curr_insn, max_uid))
5585 change_p = true;
5586 CLEAR_HARD_REG_SET (s);
5587 if (dst_regno < FIRST_PSEUDO_REGISTER)
5588 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5589 else
5590 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5591 reg_renumber[dst_regno]);
5592 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5594 /* We should invalidate potential inheritance or
5595 splitting for the current insn usages to the next
5596 usage insns (see code below) as the output pseudo
5597 prevents this. */
5598 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5599 && reg_renumber[dst_regno] < 0)
5600 || (reg->type == OP_OUT && ! reg->subreg_p
5601 && (dst_regno < FIRST_PSEUDO_REGISTER
5602 || reg_renumber[dst_regno] >= 0)))
5604 /* Invalidate and mark definitions. */
5605 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5606 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5607 else
5609 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5610 for (i = 0; i < nregs; i++)
5611 usage_insns[dst_regno + i].check
5612 = -(int) INSN_UID (curr_insn);
5616 /* Process clobbered call regs. */
5617 if (curr_id->arg_hard_regs != NULL)
5618 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5619 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5620 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
5621 = -(int) INSN_UID (curr_insn);
5622 if (! JUMP_P (curr_insn))
5623 for (i = 0; i < to_inherit_num; i++)
5624 if (inherit_reload_reg (true, to_inherit[i].regno,
5625 ALL_REGS, curr_insn,
5626 to_inherit[i].insns))
5627 change_p = true;
5628 if (CALL_P (curr_insn))
5630 rtx cheap, pat, dest;
5631 rtx_insn *restore;
5632 int regno, hard_regno;
5634 calls_num++;
5635 if ((cheap = find_reg_note (curr_insn,
5636 REG_RETURNED, NULL_RTX)) != NULL_RTX
5637 && ((cheap = XEXP (cheap, 0)), true)
5638 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5639 && (hard_regno = reg_renumber[regno]) >= 0
5640 /* If there are pending saves/restores, the
5641 optimization is not worth. */
5642 && usage_insns[regno].calls_num == calls_num - 1
5643 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5645 /* Restore the pseudo from the call result as
5646 REG_RETURNED note says that the pseudo value is
5647 in the call result and the pseudo is an argument
5648 of the call. */
5649 pat = PATTERN (curr_insn);
5650 if (GET_CODE (pat) == PARALLEL)
5651 pat = XVECEXP (pat, 0, 0);
5652 dest = SET_DEST (pat);
5653 /* For multiple return values dest is PARALLEL.
5654 Currently we handle only single return value case. */
5655 if (REG_P (dest))
5657 start_sequence ();
5658 emit_move_insn (cheap, copy_rtx (dest));
5659 restore = get_insns ();
5660 end_sequence ();
5661 lra_process_new_insns (curr_insn, NULL, restore,
5662 "Inserting call parameter restore");
5663 /* We don't need to save/restore of the pseudo from
5664 this call. */
5665 usage_insns[regno].calls_num = calls_num;
5666 bitmap_set_bit (&check_only_regs, regno);
5670 to_inherit_num = 0;
5671 /* Process insn usages. */
5672 for (iter = 0; iter < 2; iter++)
5673 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5674 reg != NULL;
5675 reg = reg->next)
5676 if ((reg->type != OP_OUT
5677 || (reg->type == OP_OUT && reg->subreg_p))
5678 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5680 if (src_regno >= FIRST_PSEUDO_REGISTER
5681 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5683 if (usage_insns[src_regno].check == curr_usage_insns_check
5684 && (next_usage_insns
5685 = usage_insns[src_regno].insns) != NULL_RTX
5686 && NONDEBUG_INSN_P (curr_insn))
5687 add_to_inherit (src_regno, next_usage_insns);
5688 else if (usage_insns[src_regno].check
5689 != -(int) INSN_UID (curr_insn))
5690 /* Add usages but only if the reg is not set up
5691 in the same insn. */
5692 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5694 else if (src_regno < FIRST_PSEUDO_REGISTER
5695 || reg_renumber[src_regno] >= 0)
5697 bool before_p;
5698 rtx_insn *use_insn = curr_insn;
5700 before_p = (JUMP_P (curr_insn)
5701 || (CALL_P (curr_insn) && reg->type == OP_IN));
5702 if (NONDEBUG_INSN_P (curr_insn)
5703 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5704 && split_if_necessary (src_regno, reg->biggest_mode,
5705 potential_reload_hard_regs,
5706 before_p, curr_insn, max_uid))
5708 if (reg->subreg_p)
5709 lra_risky_transformations_p = true;
5710 change_p = true;
5711 /* Invalidate. */
5712 usage_insns[src_regno].check = 0;
5713 if (before_p)
5714 use_insn = PREV_INSN (curr_insn);
5716 if (NONDEBUG_INSN_P (curr_insn))
5718 if (src_regno < FIRST_PSEUDO_REGISTER)
5719 add_to_hard_reg_set (&live_hard_regs,
5720 reg->biggest_mode, src_regno);
5721 else
5722 add_to_hard_reg_set (&live_hard_regs,
5723 PSEUDO_REGNO_MODE (src_regno),
5724 reg_renumber[src_regno]);
5726 add_next_usage_insn (src_regno, use_insn, reloads_num);
5729 /* Process used call regs. */
5730 if (curr_id->arg_hard_regs != NULL)
5731 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5732 if (src_regno < FIRST_PSEUDO_REGISTER)
5734 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5735 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5737 for (i = 0; i < to_inherit_num; i++)
5739 src_regno = to_inherit[i].regno;
5740 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5741 curr_insn, to_inherit[i].insns))
5742 change_p = true;
5743 else
5744 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5747 if (update_reloads_num_p
5748 && NONDEBUG_INSN_P (curr_insn)
5749 && (set = single_set (curr_insn)) != NULL_RTX)
5751 int regno = -1;
5752 if ((REG_P (SET_DEST (set))
5753 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5754 && reg_renumber[regno] < 0
5755 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5756 || (REG_P (SET_SRC (set))
5757 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5758 && reg_renumber[regno] < 0
5759 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5761 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5762 reloads_num++;
5763 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5764 IOR_HARD_REG_SET (potential_reload_hard_regs,
5765 reg_class_contents[cl]);
5768 /* We reached the start of the current basic block. */
5769 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5770 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5772 /* We reached the beginning of the current block -- do
5773 rest of spliting in the current BB. */
5774 to_process = df_get_live_in (curr_bb);
5775 if (BLOCK_FOR_INSN (head) != curr_bb)
5777 /* We are somewhere in the middle of EBB. */
5778 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5779 curr_bb, &temp_bitmap);
5780 to_process = &temp_bitmap;
5782 head_p = true;
5783 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5785 if ((int) j >= lra_constraint_new_regno_start)
5786 break;
5787 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5788 && usage_insns[j].check == curr_usage_insns_check
5789 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5791 if (need_for_split_p (potential_reload_hard_regs, j))
5793 if (lra_dump_file != NULL && head_p)
5795 fprintf (lra_dump_file,
5796 " ----------------------------------\n");
5797 head_p = false;
5799 if (split_reg (false, j, bb_note (curr_bb),
5800 next_usage_insns))
5801 change_p = true;
5803 usage_insns[j].check = 0;
5808 return change_p;
5811 /* This value affects EBB forming. If probability of edge from EBB to
5812 a BB is not greater than the following value, we don't add the BB
5813 to EBB. */
5814 #define EBB_PROBABILITY_CUTOFF \
5815 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
5817 /* Current number of inheritance/split iteration. */
5818 int lra_inheritance_iter;
5820 /* Entry function for inheritance/split pass. */
5821 void
5822 lra_inheritance (void)
5824 int i;
5825 basic_block bb, start_bb;
5826 edge e;
5828 lra_inheritance_iter++;
5829 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5830 return;
5831 timevar_push (TV_LRA_INHERITANCE);
5832 if (lra_dump_file != NULL)
5833 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5834 lra_inheritance_iter);
5835 curr_usage_insns_check = 0;
5836 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5837 for (i = 0; i < lra_constraint_new_regno_start; i++)
5838 usage_insns[i].check = 0;
5839 bitmap_initialize (&check_only_regs, &reg_obstack);
5840 bitmap_initialize (&live_regs, &reg_obstack);
5841 bitmap_initialize (&temp_bitmap, &reg_obstack);
5842 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5843 FOR_EACH_BB_FN (bb, cfun)
5845 start_bb = bb;
5846 if (lra_dump_file != NULL)
5847 fprintf (lra_dump_file, "EBB");
5848 /* Form a EBB starting with BB. */
5849 bitmap_clear (&ebb_global_regs);
5850 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5851 for (;;)
5853 if (lra_dump_file != NULL)
5854 fprintf (lra_dump_file, " %d", bb->index);
5855 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5856 || LABEL_P (BB_HEAD (bb->next_bb)))
5857 break;
5858 e = find_fallthru_edge (bb->succs);
5859 if (! e)
5860 break;
5861 if (e->probability < EBB_PROBABILITY_CUTOFF)
5862 break;
5863 bb = bb->next_bb;
5865 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5866 if (lra_dump_file != NULL)
5867 fprintf (lra_dump_file, "\n");
5868 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5869 /* Remember that the EBB head and tail can change in
5870 inherit_in_ebb. */
5871 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5873 bitmap_clear (&ebb_global_regs);
5874 bitmap_clear (&temp_bitmap);
5875 bitmap_clear (&live_regs);
5876 bitmap_clear (&check_only_regs);
5877 free (usage_insns);
5879 timevar_pop (TV_LRA_INHERITANCE);
5884 /* This page contains code to undo failed inheritance/split
5885 transformations. */
5887 /* Current number of iteration undoing inheritance/split. */
5888 int lra_undo_inheritance_iter;
5890 /* Fix BB live info LIVE after removing pseudos created on pass doing
5891 inheritance/split which are REMOVED_PSEUDOS. */
5892 static void
5893 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5895 unsigned int regno;
5896 bitmap_iterator bi;
5898 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5899 if (bitmap_clear_bit (live, regno))
5900 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5903 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5904 number. */
5905 static int
5906 get_regno (rtx reg)
5908 if (GET_CODE (reg) == SUBREG)
5909 reg = SUBREG_REG (reg);
5910 if (REG_P (reg))
5911 return REGNO (reg);
5912 return -1;
5915 /* Delete a move INSN with destination reg DREGNO and a previous
5916 clobber insn with the same regno. The inheritance/split code can
5917 generate moves with preceding clobber and when we delete such moves
5918 we should delete the clobber insn too to keep the correct life
5919 info. */
5920 static void
5921 delete_move_and_clobber (rtx_insn *insn, int dregno)
5923 rtx_insn *prev_insn = PREV_INSN (insn);
5925 lra_set_insn_deleted (insn);
5926 lra_assert (dregno >= 0);
5927 if (prev_insn != NULL && NONDEBUG_INSN_P (prev_insn)
5928 && GET_CODE (PATTERN (prev_insn)) == CLOBBER
5929 && dregno == get_regno (XEXP (PATTERN (prev_insn), 0)))
5930 lra_set_insn_deleted (prev_insn);
5933 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5934 return true if we did any change. The undo transformations for
5935 inheritance looks like
5936 i <- i2
5937 p <- i => p <- i2
5938 or removing
5939 p <- i, i <- p, and i <- i3
5940 where p is original pseudo from which inheritance pseudo i was
5941 created, i and i3 are removed inheritance pseudos, i2 is another
5942 not removed inheritance pseudo. All split pseudos or other
5943 occurrences of removed inheritance pseudos are changed on the
5944 corresponding original pseudos.
5946 The function also schedules insns changed and created during
5947 inheritance/split pass for processing by the subsequent constraint
5948 pass. */
5949 static bool
5950 remove_inheritance_pseudos (bitmap remove_pseudos)
5952 basic_block bb;
5953 int regno, sregno, prev_sregno, dregno, restore_regno;
5954 rtx set, prev_set;
5955 rtx_insn *prev_insn;
5956 bool change_p, done_p;
5958 change_p = ! bitmap_empty_p (remove_pseudos);
5959 /* We can not finish the function right away if CHANGE_P is true
5960 because we need to marks insns affected by previous
5961 inheritance/split pass for processing by the subsequent
5962 constraint pass. */
5963 FOR_EACH_BB_FN (bb, cfun)
5965 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5966 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5967 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5969 if (! INSN_P (curr_insn))
5970 continue;
5971 done_p = false;
5972 sregno = dregno = -1;
5973 if (change_p && NONDEBUG_INSN_P (curr_insn)
5974 && (set = single_set (curr_insn)) != NULL_RTX)
5976 dregno = get_regno (SET_DEST (set));
5977 sregno = get_regno (SET_SRC (set));
5980 if (sregno >= 0 && dregno >= 0)
5982 if ((bitmap_bit_p (remove_pseudos, sregno)
5983 && (lra_reg_info[sregno].restore_regno == dregno
5984 || (bitmap_bit_p (remove_pseudos, dregno)
5985 && (lra_reg_info[sregno].restore_regno
5986 == lra_reg_info[dregno].restore_regno))))
5987 || (bitmap_bit_p (remove_pseudos, dregno)
5988 && lra_reg_info[dregno].restore_regno == sregno))
5989 /* One of the following cases:
5990 original <- removed inheritance pseudo
5991 removed inherit pseudo <- another removed inherit pseudo
5992 removed inherit pseudo <- original pseudo
5994 removed_split_pseudo <- original_reg
5995 original_reg <- removed_split_pseudo */
5997 if (lra_dump_file != NULL)
5999 fprintf (lra_dump_file, " Removing %s:\n",
6000 bitmap_bit_p (&lra_split_regs, sregno)
6001 || bitmap_bit_p (&lra_split_regs, dregno)
6002 ? "split" : "inheritance");
6003 dump_insn_slim (lra_dump_file, curr_insn);
6005 delete_move_and_clobber (curr_insn, dregno);
6006 done_p = true;
6008 else if (bitmap_bit_p (remove_pseudos, sregno)
6009 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
6011 /* Search the following pattern:
6012 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
6013 original_pseudo <- inherit_or_split_pseudo1
6014 where the 2nd insn is the current insn and
6015 inherit_or_split_pseudo2 is not removed. If it is found,
6016 change the current insn onto:
6017 original_pseudo <- inherit_or_split_pseudo2. */
6018 for (prev_insn = PREV_INSN (curr_insn);
6019 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
6020 prev_insn = PREV_INSN (prev_insn))
6022 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
6023 && (prev_set = single_set (prev_insn)) != NULL_RTX
6024 /* There should be no subregs in insn we are
6025 searching because only the original reg might
6026 be in subreg when we changed the mode of
6027 load/store for splitting. */
6028 && REG_P (SET_DEST (prev_set))
6029 && REG_P (SET_SRC (prev_set))
6030 && (int) REGNO (SET_DEST (prev_set)) == sregno
6031 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
6032 >= FIRST_PSEUDO_REGISTER)
6033 /* As we consider chain of inheritance or
6034 splitting described in above comment we should
6035 check that sregno and prev_sregno were
6036 inheritance/split pseudos created from the
6037 same original regno. */
6038 && (lra_reg_info[sregno].restore_regno
6039 == lra_reg_info[prev_sregno].restore_regno)
6040 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
6042 lra_assert (GET_MODE (SET_SRC (prev_set))
6043 == GET_MODE (regno_reg_rtx[sregno]));
6044 if (GET_CODE (SET_SRC (set)) == SUBREG)
6045 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
6046 else
6047 SET_SRC (set) = SET_SRC (prev_set);
6048 /* As we are finishing with processing the insn
6049 here, check the destination too as it might
6050 inheritance pseudo for another pseudo. */
6051 if (bitmap_bit_p (remove_pseudos, dregno)
6052 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
6053 && (restore_regno
6054 = lra_reg_info[dregno].restore_regno) >= 0)
6056 if (GET_CODE (SET_DEST (set)) == SUBREG)
6057 SUBREG_REG (SET_DEST (set))
6058 = regno_reg_rtx[restore_regno];
6059 else
6060 SET_DEST (set) = regno_reg_rtx[restore_regno];
6062 lra_push_insn_and_update_insn_regno_info (curr_insn);
6063 lra_set_used_insn_alternative_by_uid
6064 (INSN_UID (curr_insn), -1);
6065 done_p = true;
6066 if (lra_dump_file != NULL)
6068 fprintf (lra_dump_file, " Change reload insn:\n");
6069 dump_insn_slim (lra_dump_file, curr_insn);
6074 if (! done_p)
6076 struct lra_insn_reg *reg;
6077 bool restored_regs_p = false;
6078 bool kept_regs_p = false;
6080 curr_id = lra_get_insn_recog_data (curr_insn);
6081 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6083 regno = reg->regno;
6084 restore_regno = lra_reg_info[regno].restore_regno;
6085 if (restore_regno >= 0)
6087 if (change_p && bitmap_bit_p (remove_pseudos, regno))
6089 lra_substitute_pseudo_within_insn
6090 (curr_insn, regno, regno_reg_rtx[restore_regno],
6091 false);
6092 restored_regs_p = true;
6094 else
6095 kept_regs_p = true;
6098 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
6100 /* The instruction has changed since the previous
6101 constraints pass. */
6102 lra_push_insn_and_update_insn_regno_info (curr_insn);
6103 lra_set_used_insn_alternative_by_uid
6104 (INSN_UID (curr_insn), -1);
6106 else if (restored_regs_p)
6107 /* The instruction has been restored to the form that
6108 it had during the previous constraints pass. */
6109 lra_update_insn_regno_info (curr_insn);
6110 if (restored_regs_p && lra_dump_file != NULL)
6112 fprintf (lra_dump_file, " Insn after restoring regs:\n");
6113 dump_insn_slim (lra_dump_file, curr_insn);
6118 return change_p;
6121 /* If optional reload pseudos failed to get a hard register or was not
6122 inherited, it is better to remove optional reloads. We do this
6123 transformation after undoing inheritance to figure out necessity to
6124 remove optional reloads easier. Return true if we do any
6125 change. */
6126 static bool
6127 undo_optional_reloads (void)
6129 bool change_p, keep_p;
6130 unsigned int regno, uid;
6131 bitmap_iterator bi, bi2;
6132 rtx_insn *insn;
6133 rtx set, src, dest;
6134 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
6136 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
6137 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
6138 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6140 keep_p = false;
6141 /* Keep optional reloads from previous subpasses. */
6142 if (lra_reg_info[regno].restore_regno < 0
6143 /* If the original pseudo changed its allocation, just
6144 removing the optional pseudo is dangerous as the original
6145 pseudo will have longer live range. */
6146 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
6147 keep_p = true;
6148 else if (reg_renumber[regno] >= 0)
6149 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
6151 insn = lra_insn_recog_data[uid]->insn;
6152 if ((set = single_set (insn)) == NULL_RTX)
6153 continue;
6154 src = SET_SRC (set);
6155 dest = SET_DEST (set);
6156 if (! REG_P (src) || ! REG_P (dest))
6157 continue;
6158 if (REGNO (dest) == regno
6159 /* Ignore insn for optional reloads itself. */
6160 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
6161 /* Check only inheritance on last inheritance pass. */
6162 && (int) REGNO (src) >= new_regno_start
6163 /* Check that the optional reload was inherited. */
6164 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6166 keep_p = true;
6167 break;
6170 if (keep_p)
6172 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6173 if (lra_dump_file != NULL)
6174 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6177 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6178 bitmap_initialize (&insn_bitmap, &reg_obstack);
6179 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6181 if (lra_dump_file != NULL)
6182 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6183 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6184 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6186 insn = lra_insn_recog_data[uid]->insn;
6187 if ((set = single_set (insn)) != NULL_RTX)
6189 src = SET_SRC (set);
6190 dest = SET_DEST (set);
6191 if (REG_P (src) && REG_P (dest)
6192 && ((REGNO (src) == regno
6193 && (lra_reg_info[regno].restore_regno
6194 == (int) REGNO (dest)))
6195 || (REGNO (dest) == regno
6196 && (lra_reg_info[regno].restore_regno
6197 == (int) REGNO (src)))))
6199 if (lra_dump_file != NULL)
6201 fprintf (lra_dump_file, " Deleting move %u\n",
6202 INSN_UID (insn));
6203 dump_insn_slim (lra_dump_file, insn);
6205 delete_move_and_clobber (insn, REGNO (dest));
6206 continue;
6208 /* We should not worry about generation memory-memory
6209 moves here as if the corresponding inheritance did
6210 not work (inheritance pseudo did not get a hard reg),
6211 we remove the inheritance pseudo and the optional
6212 reload. */
6214 lra_substitute_pseudo_within_insn
6215 (insn, regno, regno_reg_rtx[lra_reg_info[regno].restore_regno],
6216 false);
6217 lra_update_insn_regno_info (insn);
6218 if (lra_dump_file != NULL)
6220 fprintf (lra_dump_file,
6221 " Restoring original insn:\n");
6222 dump_insn_slim (lra_dump_file, insn);
6226 /* Clear restore_regnos. */
6227 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6228 lra_reg_info[regno].restore_regno = -1;
6229 bitmap_clear (&insn_bitmap);
6230 bitmap_clear (&removed_optional_reload_pseudos);
6231 return change_p;
6234 /* Entry function for undoing inheritance/split transformation. Return true
6235 if we did any RTL change in this pass. */
6236 bool
6237 lra_undo_inheritance (void)
6239 unsigned int regno;
6240 int restore_regno, hard_regno;
6241 int n_all_inherit, n_inherit, n_all_split, n_split;
6242 bitmap_head remove_pseudos;
6243 bitmap_iterator bi;
6244 bool change_p;
6246 lra_undo_inheritance_iter++;
6247 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6248 return false;
6249 if (lra_dump_file != NULL)
6250 fprintf (lra_dump_file,
6251 "\n********** Undoing inheritance #%d: **********\n\n",
6252 lra_undo_inheritance_iter);
6253 bitmap_initialize (&remove_pseudos, &reg_obstack);
6254 n_inherit = n_all_inherit = 0;
6255 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6256 if (lra_reg_info[regno].restore_regno >= 0)
6258 n_all_inherit++;
6259 if (reg_renumber[regno] < 0
6260 /* If the original pseudo changed its allocation, just
6261 removing inheritance is dangerous as for changing
6262 allocation we used shorter live-ranges. */
6263 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6264 bitmap_set_bit (&remove_pseudos, regno);
6265 else
6266 n_inherit++;
6268 if (lra_dump_file != NULL && n_all_inherit != 0)
6269 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6270 n_inherit, n_all_inherit,
6271 (double) n_inherit / n_all_inherit * 100);
6272 n_split = n_all_split = 0;
6273 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6274 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6276 n_all_split++;
6277 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6278 ? reg_renumber[restore_regno] : restore_regno);
6279 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6280 bitmap_set_bit (&remove_pseudos, regno);
6281 else
6283 n_split++;
6284 if (lra_dump_file != NULL)
6285 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6286 regno, restore_regno);
6289 if (lra_dump_file != NULL && n_all_split != 0)
6290 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6291 n_split, n_all_split,
6292 (double) n_split / n_all_split * 100);
6293 change_p = remove_inheritance_pseudos (&remove_pseudos);
6294 bitmap_clear (&remove_pseudos);
6295 /* Clear restore_regnos. */
6296 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6297 lra_reg_info[regno].restore_regno = -1;
6298 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6299 lra_reg_info[regno].restore_regno = -1;
6300 change_p = undo_optional_reloads () || change_p;
6301 return change_p;