Daily bump.
[official-gcc.git] / gcc / lra-constraints.c
blobf96fd458e232224a5bedcac0b8783b36c91cdb48
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2016 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains code for 3 passes: constraint pass,
23 inheritance/split pass, and pass for undoing failed inheritance and
24 split.
26 The major goal of constraint pass is to transform RTL to satisfy
27 insn and address constraints by:
28 o choosing insn alternatives;
29 o generating *reload insns* (or reloads in brief) and *reload
30 pseudos* which will get necessary hard registers later;
31 o substituting pseudos with equivalent values and removing the
32 instructions that initialized those pseudos.
34 The constraint pass has biggest and most complicated code in LRA.
35 There are a lot of important details like:
36 o reuse of input reload pseudos to simplify reload pseudo
37 allocations;
38 o some heuristics to choose insn alternative to improve the
39 inheritance;
40 o early clobbers etc.
42 The pass is mimicking former reload pass in alternative choosing
43 because the reload pass is oriented to current machine description
44 model. It might be changed if the machine description model is
45 changed.
47 There is special code for preventing all LRA and this pass cycling
48 in case of bugs.
50 On the first iteration of the pass we process every instruction and
51 choose an alternative for each one. On subsequent iterations we try
52 to avoid reprocessing instructions if we can be sure that the old
53 choice is still valid.
55 The inheritance/spilt pass is to transform code to achieve
56 ineheritance and live range splitting. It is done on backward
57 traversal of EBBs.
59 The inheritance optimization goal is to reuse values in hard
60 registers. There is analogous optimization in old reload pass. The
61 inheritance is achieved by following transformation:
63 reload_p1 <- p reload_p1 <- p
64 ... new_p <- reload_p1
65 ... => ...
66 reload_p2 <- p reload_p2 <- new_p
68 where p is spilled and not changed between the insns. Reload_p1 is
69 also called *original pseudo* and new_p is called *inheritance
70 pseudo*.
72 The subsequent assignment pass will try to assign the same (or
73 another if it is not possible) hard register to new_p as to
74 reload_p1 or reload_p2.
76 If the assignment pass fails to assign a hard register to new_p,
77 this file will undo the inheritance and restore the original code.
78 This is because implementing the above sequence with a spilled
79 new_p would make the code much worse. The inheritance is done in
80 EBB scope. The above is just a simplified example to get an idea
81 of the inheritance as the inheritance is also done for non-reload
82 insns.
84 Splitting (transformation) is also done in EBB scope on the same
85 pass as the inheritance:
87 r <- ... or ... <- r r <- ... or ... <- r
88 ... s <- r (new insn -- save)
89 ... =>
90 ... r <- s (new insn -- restore)
91 ... <- r ... <- r
93 The *split pseudo* s is assigned to the hard register of the
94 original pseudo or hard register r.
96 Splitting is done:
97 o In EBBs with high register pressure for global pseudos (living
98 in at least 2 BBs) and assigned to hard registers when there
99 are more one reloads needing the hard registers;
100 o for pseudos needing save/restore code around calls.
102 If the split pseudo still has the same hard register as the
103 original pseudo after the subsequent assignment pass or the
104 original pseudo was split, the opposite transformation is done on
105 the same pass for undoing inheritance. */
107 #undef REG_OK_STRICT
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "backend.h"
113 #include "target.h"
114 #include "rtl.h"
115 #include "tree.h"
116 #include "predict.h"
117 #include "df.h"
118 #include "tm_p.h"
119 #include "expmed.h"
120 #include "optabs.h"
121 #include "regs.h"
122 #include "ira.h"
123 #include "recog.h"
124 #include "output.h"
125 #include "addresses.h"
126 #include "expr.h"
127 #include "cfgrtl.h"
128 #include "rtl-error.h"
129 #include "params.h"
130 #include "lra.h"
131 #include "lra-int.h"
132 #include "print-rtl.h"
134 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
135 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
136 reload insns. */
137 static int bb_reload_num;
139 /* The current insn being processed and corresponding its single set
140 (NULL otherwise), its data (basic block, the insn data, the insn
141 static data, and the mode of each operand). */
142 static rtx_insn *curr_insn;
143 static rtx curr_insn_set;
144 static basic_block curr_bb;
145 static lra_insn_recog_data_t curr_id;
146 static struct lra_static_insn_data *curr_static_id;
147 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
148 /* Mode of the register substituted by its equivalence with VOIDmode
149 (e.g. constant) and whose subreg is given operand of the current
150 insn. VOIDmode in all other cases. */
151 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
155 /* Start numbers for new registers and insns at the current constraints
156 pass start. */
157 static int new_regno_start;
158 static int new_insn_uid_start;
160 /* If LOC is nonnull, strip any outer subreg from it. */
161 static inline rtx *
162 strip_subreg (rtx *loc)
164 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
167 /* Return hard regno of REGNO or if it is was not assigned to a hard
168 register, use a hard register from its allocno class. */
169 static int
170 get_try_hard_regno (int regno)
172 int hard_regno;
173 enum reg_class rclass;
175 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
176 hard_regno = lra_get_regno_hard_regno (regno);
177 if (hard_regno >= 0)
178 return hard_regno;
179 rclass = lra_get_allocno_class (regno);
180 if (rclass == NO_REGS)
181 return -1;
182 return ira_class_hard_regs[rclass][0];
185 /* Return final hard regno (plus offset) which will be after
186 elimination. We do this for matching constraints because the final
187 hard regno could have a different class. */
188 static int
189 get_final_hard_regno (int hard_regno, int offset)
191 if (hard_regno < 0)
192 return hard_regno;
193 hard_regno = lra_get_elimination_hard_regno (hard_regno);
194 return hard_regno + offset;
197 /* Return hard regno of X after removing subreg and making
198 elimination. If X is not a register or subreg of register, return
199 -1. For pseudo use its assignment. */
200 static int
201 get_hard_regno (rtx x)
203 rtx reg;
204 int offset, hard_regno;
206 reg = x;
207 if (GET_CODE (x) == SUBREG)
208 reg = SUBREG_REG (x);
209 if (! REG_P (reg))
210 return -1;
211 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
212 hard_regno = lra_get_regno_hard_regno (hard_regno);
213 if (hard_regno < 0)
214 return -1;
215 offset = 0;
216 if (GET_CODE (x) == SUBREG)
217 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
218 SUBREG_BYTE (x), GET_MODE (x));
219 return get_final_hard_regno (hard_regno, offset);
222 /* If REGNO is a hard register or has been allocated a hard register,
223 return the class of that register. If REGNO is a reload pseudo
224 created by the current constraints pass, return its allocno class.
225 Return NO_REGS otherwise. */
226 static enum reg_class
227 get_reg_class (int regno)
229 int hard_regno;
231 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
232 hard_regno = lra_get_regno_hard_regno (regno);
233 if (hard_regno >= 0)
235 hard_regno = get_final_hard_regno (hard_regno, 0);
236 return REGNO_REG_CLASS (hard_regno);
238 if (regno >= new_regno_start)
239 return lra_get_allocno_class (regno);
240 return NO_REGS;
243 /* Return true if REG satisfies (or will satisfy) reg class constraint
244 CL. Use elimination first if REG is a hard register. If REG is a
245 reload pseudo created by this constraints pass, assume that it will
246 be allocated a hard register from its allocno class, but allow that
247 class to be narrowed to CL if it is currently a superset of CL.
249 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
250 REGNO (reg), or NO_REGS if no change in its class was needed. */
251 static bool
252 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
254 enum reg_class rclass, common_class;
255 machine_mode reg_mode;
256 int class_size, hard_regno, nregs, i, j;
257 int regno = REGNO (reg);
259 if (new_class != NULL)
260 *new_class = NO_REGS;
261 if (regno < FIRST_PSEUDO_REGISTER)
263 rtx final_reg = reg;
264 rtx *final_loc = &final_reg;
266 lra_eliminate_reg_if_possible (final_loc);
267 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
269 reg_mode = GET_MODE (reg);
270 rclass = get_reg_class (regno);
271 if (regno < new_regno_start
272 /* Do not allow the constraints for reload instructions to
273 influence the classes of new pseudos. These reloads are
274 typically moves that have many alternatives, and restricting
275 reload pseudos for one alternative may lead to situations
276 where other reload pseudos are no longer allocatable. */
277 || (INSN_UID (curr_insn) >= new_insn_uid_start
278 && curr_insn_set != NULL
279 && ((OBJECT_P (SET_SRC (curr_insn_set))
280 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
281 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
282 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
283 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
284 /* When we don't know what class will be used finally for reload
285 pseudos, we use ALL_REGS. */
286 return ((regno >= new_regno_start && rclass == ALL_REGS)
287 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
288 && ! hard_reg_set_subset_p (reg_class_contents[cl],
289 lra_no_alloc_regs)));
290 else
292 common_class = ira_reg_class_subset[rclass][cl];
293 if (new_class != NULL)
294 *new_class = common_class;
295 if (hard_reg_set_subset_p (reg_class_contents[common_class],
296 lra_no_alloc_regs))
297 return false;
298 /* Check that there are enough allocatable regs. */
299 class_size = ira_class_hard_regs_num[common_class];
300 for (i = 0; i < class_size; i++)
302 hard_regno = ira_class_hard_regs[common_class][i];
303 nregs = hard_regno_nregs[hard_regno][reg_mode];
304 if (nregs == 1)
305 return true;
306 for (j = 0; j < nregs; j++)
307 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
308 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
309 hard_regno + j))
310 break;
311 if (j >= nregs)
312 return true;
314 return false;
318 /* Return true if REGNO satisfies a memory constraint. */
319 static bool
320 in_mem_p (int regno)
322 return get_reg_class (regno) == NO_REGS;
325 /* Return 1 if ADDR is a valid memory address for mode MODE in address
326 space AS, and check that each pseudo has the proper kind of hard
327 reg. */
328 static int
329 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
330 rtx addr, addr_space_t as)
332 #ifdef GO_IF_LEGITIMATE_ADDRESS
333 lra_assert (ADDR_SPACE_GENERIC_P (as));
334 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
335 return 0;
337 win:
338 return 1;
339 #else
340 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
341 #endif
344 namespace {
345 /* Temporarily eliminates registers in an address (for the lifetime of
346 the object). */
347 class address_eliminator {
348 public:
349 address_eliminator (struct address_info *ad);
350 ~address_eliminator ();
352 private:
353 struct address_info *m_ad;
354 rtx *m_base_loc;
355 rtx m_base_reg;
356 rtx *m_index_loc;
357 rtx m_index_reg;
361 address_eliminator::address_eliminator (struct address_info *ad)
362 : m_ad (ad),
363 m_base_loc (strip_subreg (ad->base_term)),
364 m_base_reg (NULL_RTX),
365 m_index_loc (strip_subreg (ad->index_term)),
366 m_index_reg (NULL_RTX)
368 if (m_base_loc != NULL)
370 m_base_reg = *m_base_loc;
371 lra_eliminate_reg_if_possible (m_base_loc);
372 if (m_ad->base_term2 != NULL)
373 *m_ad->base_term2 = *m_ad->base_term;
375 if (m_index_loc != NULL)
377 m_index_reg = *m_index_loc;
378 lra_eliminate_reg_if_possible (m_index_loc);
382 address_eliminator::~address_eliminator ()
384 if (m_base_loc && *m_base_loc != m_base_reg)
386 *m_base_loc = m_base_reg;
387 if (m_ad->base_term2 != NULL)
388 *m_ad->base_term2 = *m_ad->base_term;
390 if (m_index_loc && *m_index_loc != m_index_reg)
391 *m_index_loc = m_index_reg;
394 /* Return true if the eliminated form of AD is a legitimate target address. */
395 static bool
396 valid_address_p (struct address_info *ad)
398 address_eliminator eliminator (ad);
399 return valid_address_p (ad->mode, *ad->outer, ad->as);
402 /* Return true if the eliminated form of memory reference OP satisfies
403 extra (special) memory constraint CONSTRAINT. */
404 static bool
405 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
407 struct address_info ad;
409 decompose_mem_address (&ad, op);
410 address_eliminator eliminator (&ad);
411 return constraint_satisfied_p (op, constraint);
414 /* Return true if the eliminated form of address AD satisfies extra
415 address constraint CONSTRAINT. */
416 static bool
417 satisfies_address_constraint_p (struct address_info *ad,
418 enum constraint_num constraint)
420 address_eliminator eliminator (ad);
421 return constraint_satisfied_p (*ad->outer, constraint);
424 /* Return true if the eliminated form of address OP satisfies extra
425 address constraint CONSTRAINT. */
426 static bool
427 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
429 struct address_info ad;
431 decompose_lea_address (&ad, &op);
432 return satisfies_address_constraint_p (&ad, constraint);
435 /* Initiate equivalences for LRA. As we keep original equivalences
436 before any elimination, we need to make copies otherwise any change
437 in insns might change the equivalences. */
438 void
439 lra_init_equiv (void)
441 ira_expand_reg_equiv ();
442 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
444 rtx res;
446 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
447 ira_reg_equiv[i].memory = copy_rtx (res);
448 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
449 ira_reg_equiv[i].invariant = copy_rtx (res);
453 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
455 /* Update equivalence for REGNO. We need to this as the equivalence
456 might contain other pseudos which are changed by their
457 equivalences. */
458 static void
459 update_equiv (int regno)
461 rtx x;
463 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
464 ira_reg_equiv[regno].memory
465 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
466 NULL_RTX);
467 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
468 ira_reg_equiv[regno].invariant
469 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
470 NULL_RTX);
473 /* If we have decided to substitute X with another value, return that
474 value, otherwise return X. */
475 static rtx
476 get_equiv (rtx x)
478 int regno;
479 rtx res;
481 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
482 || ! ira_reg_equiv[regno].defined_p
483 || ! ira_reg_equiv[regno].profitable_p
484 || lra_get_regno_hard_regno (regno) >= 0)
485 return x;
486 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
488 if (targetm.cannot_substitute_mem_equiv_p (res))
489 return x;
490 return res;
492 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
493 return res;
494 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
495 return res;
496 gcc_unreachable ();
499 /* If we have decided to substitute X with the equivalent value,
500 return that value after elimination for INSN, otherwise return
501 X. */
502 static rtx
503 get_equiv_with_elimination (rtx x, rtx_insn *insn)
505 rtx res = get_equiv (x);
507 if (x == res || CONSTANT_P (res))
508 return res;
509 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
510 false, false, 0, true);
513 /* Set up curr_operand_mode. */
514 static void
515 init_curr_operand_mode (void)
517 int nop = curr_static_id->n_operands;
518 for (int i = 0; i < nop; i++)
520 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
521 if (mode == VOIDmode)
523 /* The .md mode for address operands is the mode of the
524 addressed value rather than the mode of the address itself. */
525 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
526 mode = Pmode;
527 else
528 mode = curr_static_id->operand[i].mode;
530 curr_operand_mode[i] = mode;
536 /* The page contains code to reuse input reloads. */
538 /* Structure describes input reload of the current insns. */
539 struct input_reload
541 /* Reloaded value. */
542 rtx input;
543 /* Reload pseudo used. */
544 rtx reg;
547 /* The number of elements in the following array. */
548 static int curr_insn_input_reloads_num;
549 /* Array containing info about input reloads. It is used to find the
550 same input reload and reuse the reload pseudo in this case. */
551 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
553 /* Initiate data concerning reuse of input reloads for the current
554 insn. */
555 static void
556 init_curr_insn_input_reloads (void)
558 curr_insn_input_reloads_num = 0;
561 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
562 created input reload pseudo (only if TYPE is not OP_OUT). Don't
563 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
564 wrapped up in SUBREG. The result pseudo is returned through
565 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
566 reused the already created input reload pseudo. Use TITLE to
567 describe new registers for debug purposes. */
568 static bool
569 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
570 enum reg_class rclass, bool in_subreg_p,
571 const char *title, rtx *result_reg)
573 int i, regno;
574 enum reg_class new_class;
576 if (type == OP_OUT)
578 *result_reg
579 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
580 return true;
582 /* Prevent reuse value of expression with side effects,
583 e.g. volatile memory. */
584 if (! side_effects_p (original))
585 for (i = 0; i < curr_insn_input_reloads_num; i++)
586 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
587 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
589 rtx reg = curr_insn_input_reloads[i].reg;
590 regno = REGNO (reg);
591 /* If input is equal to original and both are VOIDmode,
592 GET_MODE (reg) might be still different from mode.
593 Ensure we don't return *result_reg with wrong mode. */
594 if (GET_MODE (reg) != mode)
596 if (in_subreg_p)
597 continue;
598 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
599 continue;
600 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
601 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
602 continue;
604 *result_reg = reg;
605 if (lra_dump_file != NULL)
607 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
608 dump_value_slim (lra_dump_file, original, 1);
610 if (new_class != lra_get_allocno_class (regno))
611 lra_change_class (regno, new_class, ", change to", false);
612 if (lra_dump_file != NULL)
613 fprintf (lra_dump_file, "\n");
614 return false;
616 *result_reg = lra_create_new_reg (mode, original, rclass, title);
617 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
618 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
619 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
620 return true;
625 /* The page contains code to extract memory address parts. */
627 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
628 static inline bool
629 ok_for_index_p_nonstrict (rtx reg)
631 unsigned regno = REGNO (reg);
633 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
636 /* A version of regno_ok_for_base_p for use here, when all pseudos
637 should count as OK. Arguments as for regno_ok_for_base_p. */
638 static inline bool
639 ok_for_base_p_nonstrict (rtx reg, machine_mode mode, addr_space_t as,
640 enum rtx_code outer_code, enum rtx_code index_code)
642 unsigned regno = REGNO (reg);
644 if (regno >= FIRST_PSEUDO_REGISTER)
645 return true;
646 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
651 /* The page contains major code to choose the current insn alternative
652 and generate reloads for it. */
654 /* Return the offset from REGNO of the least significant register
655 in (reg:MODE REGNO).
657 This function is used to tell whether two registers satisfy
658 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
660 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
661 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
663 lra_constraint_offset (int regno, machine_mode mode)
665 lra_assert (regno < FIRST_PSEUDO_REGISTER);
666 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
667 && SCALAR_INT_MODE_P (mode))
668 return hard_regno_nregs[regno][mode] - 1;
669 return 0;
672 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
673 if they are the same hard reg, and has special hacks for
674 auto-increment and auto-decrement. This is specifically intended for
675 process_alt_operands to use in determining whether two operands
676 match. X is the operand whose number is the lower of the two.
678 It is supposed that X is the output operand and Y is the input
679 operand. Y_HARD_REGNO is the final hard regno of register Y or
680 register in subreg Y as we know it now. Otherwise, it is a
681 negative value. */
682 static bool
683 operands_match_p (rtx x, rtx y, int y_hard_regno)
685 int i;
686 RTX_CODE code = GET_CODE (x);
687 const char *fmt;
689 if (x == y)
690 return true;
691 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
692 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
694 int j;
696 i = get_hard_regno (x);
697 if (i < 0)
698 goto slow;
700 if ((j = y_hard_regno) < 0)
701 goto slow;
703 i += lra_constraint_offset (i, GET_MODE (x));
704 j += lra_constraint_offset (j, GET_MODE (y));
706 return i == j;
709 /* If two operands must match, because they are really a single
710 operand of an assembler insn, then two post-increments are invalid
711 because the assembler insn would increment only once. On the
712 other hand, a post-increment matches ordinary indexing if the
713 post-increment is the output operand. */
714 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
715 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
717 /* Two pre-increments are invalid because the assembler insn would
718 increment only once. On the other hand, a pre-increment matches
719 ordinary indexing if the pre-increment is the input operand. */
720 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
721 || GET_CODE (y) == PRE_MODIFY)
722 return operands_match_p (x, XEXP (y, 0), -1);
724 slow:
726 if (code == REG && REG_P (y))
727 return REGNO (x) == REGNO (y);
729 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
730 && x == SUBREG_REG (y))
731 return true;
732 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
733 && SUBREG_REG (x) == y)
734 return true;
736 /* Now we have disposed of all the cases in which different rtx
737 codes can match. */
738 if (code != GET_CODE (y))
739 return false;
741 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
742 if (GET_MODE (x) != GET_MODE (y))
743 return false;
745 switch (code)
747 CASE_CONST_UNIQUE:
748 return false;
750 case LABEL_REF:
751 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
752 case SYMBOL_REF:
753 return XSTR (x, 0) == XSTR (y, 0);
755 default:
756 break;
759 /* Compare the elements. If any pair of corresponding elements fail
760 to match, return false for the whole things. */
762 fmt = GET_RTX_FORMAT (code);
763 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
765 int val, j;
766 switch (fmt[i])
768 case 'w':
769 if (XWINT (x, i) != XWINT (y, i))
770 return false;
771 break;
773 case 'i':
774 if (XINT (x, i) != XINT (y, i))
775 return false;
776 break;
778 case 'e':
779 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
780 if (val == 0)
781 return false;
782 break;
784 case '0':
785 break;
787 case 'E':
788 if (XVECLEN (x, i) != XVECLEN (y, i))
789 return false;
790 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
792 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
793 if (val == 0)
794 return false;
796 break;
798 /* It is believed that rtx's at this level will never
799 contain anything but integers and other rtx's, except for
800 within LABEL_REFs and SYMBOL_REFs. */
801 default:
802 gcc_unreachable ();
805 return true;
808 /* True if X is a constant that can be forced into the constant pool.
809 MODE is the mode of the operand, or VOIDmode if not known. */
810 #define CONST_POOL_OK_P(MODE, X) \
811 ((MODE) != VOIDmode \
812 && CONSTANT_P (X) \
813 && GET_CODE (X) != HIGH \
814 && !targetm.cannot_force_const_mem (MODE, X))
816 /* True if C is a non-empty register class that has too few registers
817 to be safely used as a reload target class. */
818 #define SMALL_REGISTER_CLASS_P(C) \
819 (ira_class_hard_regs_num [(C)] == 1 \
820 || (ira_class_hard_regs_num [(C)] >= 1 \
821 && targetm.class_likely_spilled_p (C)))
823 /* If REG is a reload pseudo, try to make its class satisfying CL. */
824 static void
825 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
827 enum reg_class rclass;
829 /* Do not make more accurate class from reloads generated. They are
830 mostly moves with a lot of constraints. Making more accurate
831 class may results in very narrow class and impossibility of find
832 registers for several reloads of one insn. */
833 if (INSN_UID (curr_insn) >= new_insn_uid_start)
834 return;
835 if (GET_CODE (reg) == SUBREG)
836 reg = SUBREG_REG (reg);
837 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
838 return;
839 if (in_class_p (reg, cl, &rclass) && rclass != cl)
840 lra_change_class (REGNO (reg), rclass, " Change to", true);
843 /* Searches X for any reference to a reg with the same value as REGNO,
844 returning the rtx of the reference found if any. Otherwise,
845 returns NULL_RTX. */
846 static rtx
847 regno_val_use_in (unsigned int regno, rtx x)
849 const char *fmt;
850 int i, j;
851 rtx tem;
853 if (REG_P (x) && lra_reg_info[REGNO (x)].val == lra_reg_info[regno].val)
854 return x;
856 fmt = GET_RTX_FORMAT (GET_CODE (x));
857 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
859 if (fmt[i] == 'e')
861 if ((tem = regno_val_use_in (regno, XEXP (x, i))))
862 return tem;
864 else if (fmt[i] == 'E')
865 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
866 if ((tem = regno_val_use_in (regno , XVECEXP (x, i, j))))
867 return tem;
870 return NULL_RTX;
873 /* Generate reloads for matching OUT and INS (array of input operand
874 numbers with end marker -1) with reg class GOAL_CLASS, considering
875 output operands OUTS (similar array to INS) needing to be in different
876 registers. Add input and output reloads correspondingly to the lists
877 *BEFORE and *AFTER. OUT might be negative. In this case we generate
878 input reloads for matched input operands INS. EARLY_CLOBBER_P is a flag
879 that the output operand is early clobbered for chosen alternative. */
880 static void
881 match_reload (signed char out, signed char *ins, signed char *outs,
882 enum reg_class goal_class, rtx_insn **before,
883 rtx_insn **after, bool early_clobber_p)
885 bool out_conflict;
886 int i, in;
887 rtx new_in_reg, new_out_reg, reg;
888 machine_mode inmode, outmode;
889 rtx in_rtx = *curr_id->operand_loc[ins[0]];
890 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
892 inmode = curr_operand_mode[ins[0]];
893 outmode = out < 0 ? inmode : curr_operand_mode[out];
894 push_to_sequence (*before);
895 if (inmode != outmode)
897 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
899 reg = new_in_reg
900 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
901 goal_class, "");
902 if (SCALAR_INT_MODE_P (inmode))
903 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
904 else
905 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
906 LRA_SUBREG_P (new_out_reg) = 1;
907 /* If the input reg is dying here, we can use the same hard
908 register for REG and IN_RTX. We do it only for original
909 pseudos as reload pseudos can die although original
910 pseudos still live where reload pseudos dies. */
911 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
912 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
913 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
915 else
917 reg = new_out_reg
918 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
919 goal_class, "");
920 if (SCALAR_INT_MODE_P (outmode))
921 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
922 else
923 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
924 /* NEW_IN_REG is non-paradoxical subreg. We don't want
925 NEW_OUT_REG living above. We add clobber clause for
926 this. This is just a temporary clobber. We can remove
927 it at the end of LRA work. */
928 rtx_insn *clobber = emit_clobber (new_out_reg);
929 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
930 LRA_SUBREG_P (new_in_reg) = 1;
931 if (GET_CODE (in_rtx) == SUBREG)
933 rtx subreg_reg = SUBREG_REG (in_rtx);
935 /* If SUBREG_REG is dying here and sub-registers IN_RTX
936 and NEW_IN_REG are similar, we can use the same hard
937 register for REG and SUBREG_REG. */
938 if (REG_P (subreg_reg)
939 && (int) REGNO (subreg_reg) < lra_new_regno_start
940 && GET_MODE (subreg_reg) == outmode
941 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
942 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
943 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
947 else
949 /* Pseudos have values -- see comments for lra_reg_info.
950 Different pseudos with the same value do not conflict even if
951 they live in the same place. When we create a pseudo we
952 assign value of original pseudo (if any) from which we
953 created the new pseudo. If we create the pseudo from the
954 input pseudo, the new pseudo will have no conflict with the
955 input pseudo which is wrong when the input pseudo lives after
956 the insn and as the new pseudo value is changed by the insn
957 output. Therefore we create the new pseudo from the output
958 except the case when we have single matched dying input
959 pseudo.
961 We cannot reuse the current output register because we might
962 have a situation like "a <- a op b", where the constraints
963 force the second input operand ("b") to match the output
964 operand ("a"). "b" must then be copied into a new register
965 so that it doesn't clobber the current value of "a".
967 We can not use the same value if the output pseudo is
968 early clobbered or the input pseudo is mentioned in the
969 output, e.g. as an address part in memory, because
970 output reload will actually extend the pseudo liveness.
971 We don't care about eliminable hard regs here as we are
972 interesting only in pseudos. */
974 /* Matching input's register value is the same as one of the other
975 output operand. Output operands in a parallel insn must be in
976 different registers. */
977 out_conflict = false;
978 if (REG_P (in_rtx))
980 for (i = 0; outs[i] >= 0; i++)
982 rtx other_out_rtx = *curr_id->operand_loc[outs[i]];
983 if (REG_P (other_out_rtx)
984 && (regno_val_use_in (REGNO (in_rtx), other_out_rtx)
985 != NULL_RTX))
987 out_conflict = true;
988 break;
993 new_in_reg = new_out_reg
994 = (! early_clobber_p && ins[1] < 0 && REG_P (in_rtx)
995 && (int) REGNO (in_rtx) < lra_new_regno_start
996 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
997 && (out < 0
998 || regno_val_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
999 && !out_conflict
1000 ? lra_create_new_reg (inmode, in_rtx, goal_class, "")
1001 : lra_create_new_reg_with_unique_value (outmode, out_rtx,
1002 goal_class, ""));
1004 /* In operand can be got from transformations before processing insn
1005 constraints. One example of such transformations is subreg
1006 reloading (see function simplify_operand_subreg). The new
1007 pseudos created by the transformations might have inaccurate
1008 class (ALL_REGS) and we should make their classes more
1009 accurate. */
1010 narrow_reload_pseudo_class (in_rtx, goal_class);
1011 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
1012 *before = get_insns ();
1013 end_sequence ();
1014 for (i = 0; (in = ins[i]) >= 0; i++)
1016 lra_assert
1017 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
1018 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
1019 *curr_id->operand_loc[in] = new_in_reg;
1021 lra_update_dups (curr_id, ins);
1022 if (out < 0)
1023 return;
1024 /* See a comment for the input operand above. */
1025 narrow_reload_pseudo_class (out_rtx, goal_class);
1026 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
1028 start_sequence ();
1029 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
1030 emit_insn (*after);
1031 *after = get_insns ();
1032 end_sequence ();
1034 *curr_id->operand_loc[out] = new_out_reg;
1035 lra_update_dup (curr_id, out);
1038 /* Return register class which is union of all reg classes in insn
1039 constraint alternative string starting with P. */
1040 static enum reg_class
1041 reg_class_from_constraints (const char *p)
1043 int c, len;
1044 enum reg_class op_class = NO_REGS;
1047 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1049 case '#':
1050 case ',':
1051 return op_class;
1053 case 'g':
1054 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1055 break;
1057 default:
1058 enum constraint_num cn = lookup_constraint (p);
1059 enum reg_class cl = reg_class_for_constraint (cn);
1060 if (cl == NO_REGS)
1062 if (insn_extra_address_constraint (cn))
1063 op_class
1064 = (reg_class_subunion
1065 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1066 ADDRESS, SCRATCH)]);
1067 break;
1070 op_class = reg_class_subunion[op_class][cl];
1071 break;
1073 while ((p += len), c);
1074 return op_class;
1077 /* If OP is a register, return the class of the register as per
1078 get_reg_class, otherwise return NO_REGS. */
1079 static inline enum reg_class
1080 get_op_class (rtx op)
1082 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1085 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1086 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1087 SUBREG for VAL to make them equal. */
1088 static rtx_insn *
1089 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1091 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1093 /* Usually size of mem_pseudo is greater than val size but in
1094 rare cases it can be less as it can be defined by target
1095 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1096 if (! MEM_P (val))
1098 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1099 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1101 LRA_SUBREG_P (val) = 1;
1103 else
1105 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1106 LRA_SUBREG_P (mem_pseudo) = 1;
1109 return to_p ? gen_move_insn (mem_pseudo, val)
1110 : gen_move_insn (val, mem_pseudo);
1113 /* Process a special case insn (register move), return true if we
1114 don't need to process it anymore. INSN should be a single set
1115 insn. Set up that RTL was changed through CHANGE_P and macro
1116 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1117 SEC_MEM_P. */
1118 static bool
1119 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1121 int sregno, dregno;
1122 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1123 rtx_insn *before;
1124 enum reg_class dclass, sclass, secondary_class;
1125 secondary_reload_info sri;
1127 lra_assert (curr_insn_set != NULL_RTX);
1128 dreg = dest = SET_DEST (curr_insn_set);
1129 sreg = src = SET_SRC (curr_insn_set);
1130 if (GET_CODE (dest) == SUBREG)
1131 dreg = SUBREG_REG (dest);
1132 if (GET_CODE (src) == SUBREG)
1133 sreg = SUBREG_REG (src);
1134 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1135 return false;
1136 sclass = dclass = NO_REGS;
1137 if (REG_P (dreg))
1138 dclass = get_reg_class (REGNO (dreg));
1139 if (dclass == ALL_REGS)
1140 /* ALL_REGS is used for new pseudos created by transformations
1141 like reload of SUBREG_REG (see function
1142 simplify_operand_subreg). We don't know their class yet. We
1143 should figure out the class from processing the insn
1144 constraints not in this fast path function. Even if ALL_REGS
1145 were a right class for the pseudo, secondary_... hooks usually
1146 are not define for ALL_REGS. */
1147 return false;
1148 if (REG_P (sreg))
1149 sclass = get_reg_class (REGNO (sreg));
1150 if (sclass == ALL_REGS)
1151 /* See comments above. */
1152 return false;
1153 if (sclass == NO_REGS && dclass == NO_REGS)
1154 return false;
1155 #ifdef SECONDARY_MEMORY_NEEDED
1156 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1157 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1158 && ((sclass != NO_REGS && dclass != NO_REGS)
1159 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1160 #endif
1163 *sec_mem_p = true;
1164 return false;
1166 #endif
1167 if (! REG_P (dreg) || ! REG_P (sreg))
1168 return false;
1169 sri.prev_sri = NULL;
1170 sri.icode = CODE_FOR_nothing;
1171 sri.extra_cost = 0;
1172 secondary_class = NO_REGS;
1173 /* Set up hard register for a reload pseudo for hook
1174 secondary_reload because some targets just ignore unassigned
1175 pseudos in the hook. */
1176 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1178 dregno = REGNO (dreg);
1179 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1181 else
1182 dregno = -1;
1183 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1185 sregno = REGNO (sreg);
1186 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1188 else
1189 sregno = -1;
1190 if (sclass != NO_REGS)
1191 secondary_class
1192 = (enum reg_class) targetm.secondary_reload (false, dest,
1193 (reg_class_t) sclass,
1194 GET_MODE (src), &sri);
1195 if (sclass == NO_REGS
1196 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1197 && dclass != NO_REGS))
1199 enum reg_class old_sclass = secondary_class;
1200 secondary_reload_info old_sri = sri;
1202 sri.prev_sri = NULL;
1203 sri.icode = CODE_FOR_nothing;
1204 sri.extra_cost = 0;
1205 secondary_class
1206 = (enum reg_class) targetm.secondary_reload (true, src,
1207 (reg_class_t) dclass,
1208 GET_MODE (src), &sri);
1209 /* Check the target hook consistency. */
1210 lra_assert
1211 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1212 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1213 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1215 if (sregno >= 0)
1216 reg_renumber [sregno] = -1;
1217 if (dregno >= 0)
1218 reg_renumber [dregno] = -1;
1219 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1220 return false;
1221 *change_p = true;
1222 new_reg = NULL_RTX;
1223 if (secondary_class != NO_REGS)
1224 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1225 secondary_class,
1226 "secondary");
1227 start_sequence ();
1228 if (sri.icode == CODE_FOR_nothing)
1229 lra_emit_move (new_reg, src);
1230 else
1232 enum reg_class scratch_class;
1234 scratch_class = (reg_class_from_constraints
1235 (insn_data[sri.icode].operand[2].constraint));
1236 scratch_reg = (lra_create_new_reg_with_unique_value
1237 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1238 scratch_class, "scratch"));
1239 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1240 src, scratch_reg));
1242 before = get_insns ();
1243 end_sequence ();
1244 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1245 if (new_reg != NULL_RTX)
1246 SET_SRC (curr_insn_set) = new_reg;
1247 else
1249 if (lra_dump_file != NULL)
1251 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1252 dump_insn_slim (lra_dump_file, curr_insn);
1254 lra_set_insn_deleted (curr_insn);
1255 return true;
1257 return false;
1260 /* The following data describe the result of process_alt_operands.
1261 The data are used in curr_insn_transform to generate reloads. */
1263 /* The chosen reg classes which should be used for the corresponding
1264 operands. */
1265 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1266 /* True if the operand should be the same as another operand and that
1267 other operand does not need a reload. */
1268 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1269 /* True if the operand does not need a reload. */
1270 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1271 /* True if the operand can be offsetable memory. */
1272 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1273 /* The number of an operand to which given operand can be matched to. */
1274 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1275 /* The number of elements in the following array. */
1276 static int goal_alt_dont_inherit_ops_num;
1277 /* Numbers of operands whose reload pseudos should not be inherited. */
1278 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1279 /* True if the insn commutative operands should be swapped. */
1280 static bool goal_alt_swapped;
1281 /* The chosen insn alternative. */
1282 static int goal_alt_number;
1284 /* The following five variables are used to choose the best insn
1285 alternative. They reflect final characteristics of the best
1286 alternative. */
1288 /* Number of necessary reloads and overall cost reflecting the
1289 previous value and other unpleasantness of the best alternative. */
1290 static int best_losers, best_overall;
1291 /* Overall number hard registers used for reloads. For example, on
1292 some targets we need 2 general registers to reload DFmode and only
1293 one floating point register. */
1294 static int best_reload_nregs;
1295 /* Overall number reflecting distances of previous reloading the same
1296 value. The distances are counted from the current BB start. It is
1297 used to improve inheritance chances. */
1298 static int best_reload_sum;
1300 /* True if the current insn should have no correspondingly input or
1301 output reloads. */
1302 static bool no_input_reloads_p, no_output_reloads_p;
1304 /* True if we swapped the commutative operands in the current
1305 insn. */
1306 static int curr_swapped;
1308 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1309 register of class CL. Add any input reloads to list BEFORE. AFTER
1310 is nonnull if *LOC is an automodified value; handle that case by
1311 adding the required output reloads to list AFTER. Return true if
1312 the RTL was changed.
1314 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1315 register. Return false if the address register is correct. */
1316 static bool
1317 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1318 enum reg_class cl)
1320 int regno;
1321 enum reg_class rclass, new_class;
1322 rtx reg;
1323 rtx new_reg;
1324 machine_mode mode;
1325 bool subreg_p, before_p = false;
1327 subreg_p = GET_CODE (*loc) == SUBREG;
1328 if (subreg_p)
1329 loc = &SUBREG_REG (*loc);
1330 reg = *loc;
1331 mode = GET_MODE (reg);
1332 if (! REG_P (reg))
1334 if (check_only_p)
1335 return true;
1336 /* Always reload memory in an address even if the target supports
1337 such addresses. */
1338 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1339 before_p = true;
1341 else
1343 regno = REGNO (reg);
1344 rclass = get_reg_class (regno);
1345 if (! check_only_p
1346 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1348 if (lra_dump_file != NULL)
1350 fprintf (lra_dump_file,
1351 "Changing pseudo %d in address of insn %u on equiv ",
1352 REGNO (reg), INSN_UID (curr_insn));
1353 dump_value_slim (lra_dump_file, *loc, 1);
1354 fprintf (lra_dump_file, "\n");
1356 *loc = copy_rtx (*loc);
1358 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1360 if (check_only_p)
1361 return true;
1362 reg = *loc;
1363 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1364 mode, reg, cl, subreg_p, "address", &new_reg))
1365 before_p = true;
1367 else if (new_class != NO_REGS && rclass != new_class)
1369 if (check_only_p)
1370 return true;
1371 lra_change_class (regno, new_class, " Change to", true);
1372 return false;
1374 else
1375 return false;
1377 if (before_p)
1379 push_to_sequence (*before);
1380 lra_emit_move (new_reg, reg);
1381 *before = get_insns ();
1382 end_sequence ();
1384 *loc = new_reg;
1385 if (after != NULL)
1387 start_sequence ();
1388 lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1389 emit_insn (*after);
1390 *after = get_insns ();
1391 end_sequence ();
1393 return true;
1396 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1397 the insn to be inserted before curr insn. AFTER returns the
1398 the insn to be inserted after curr insn. ORIGREG and NEWREG
1399 are the original reg and new reg for reload. */
1400 static void
1401 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1402 rtx newreg)
1404 if (before)
1406 push_to_sequence (*before);
1407 lra_emit_move (newreg, origreg);
1408 *before = get_insns ();
1409 end_sequence ();
1411 if (after)
1413 start_sequence ();
1414 lra_emit_move (origreg, newreg);
1415 emit_insn (*after);
1416 *after = get_insns ();
1417 end_sequence ();
1421 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1423 /* Make reloads for subreg in operand NOP with internal subreg mode
1424 REG_MODE, add new reloads for further processing. Return true if
1425 any change was done. */
1426 static bool
1427 simplify_operand_subreg (int nop, machine_mode reg_mode)
1429 int hard_regno;
1430 rtx_insn *before, *after;
1431 machine_mode mode, innermode;
1432 rtx reg, new_reg;
1433 rtx operand = *curr_id->operand_loc[nop];
1434 enum reg_class regclass;
1435 enum op_type type;
1437 before = after = NULL;
1439 if (GET_CODE (operand) != SUBREG)
1440 return false;
1442 mode = GET_MODE (operand);
1443 reg = SUBREG_REG (operand);
1444 innermode = GET_MODE (reg);
1445 type = curr_static_id->operand[nop].type;
1446 /* If we change address for paradoxical subreg of memory, the
1447 address might violate the necessary alignment or the access might
1448 be slow. So take this into consideration. We should not worry
1449 about access beyond allocated memory for paradoxical memory
1450 subregs as we don't substitute such equiv memory (see processing
1451 equivalences in function lra_constraints) and because for spilled
1452 pseudos we allocate stack memory enough for the biggest
1453 corresponding paradoxical subreg. */
1454 if (MEM_P (reg)
1455 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1456 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1458 rtx subst, old = *curr_id->operand_loc[nop];
1460 alter_subreg (curr_id->operand_loc[nop], false);
1461 subst = *curr_id->operand_loc[nop];
1462 lra_assert (MEM_P (subst));
1463 if (! valid_address_p (innermode, XEXP (reg, 0),
1464 MEM_ADDR_SPACE (reg))
1465 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1466 MEM_ADDR_SPACE (subst)))
1467 return true;
1468 else if ((get_constraint_type (lookup_constraint
1469 (curr_static_id->operand[nop].constraint))
1470 != CT_SPECIAL_MEMORY)
1471 /* We still can reload address and if the address is
1472 valid, we can remove subreg without reloading its
1473 inner memory. */
1474 && valid_address_p (GET_MODE (subst),
1475 regno_reg_rtx
1476 [ira_class_hard_regs
1477 [base_reg_class (GET_MODE (subst),
1478 MEM_ADDR_SPACE (subst),
1479 ADDRESS, SCRATCH)][0]],
1480 MEM_ADDR_SPACE (subst)))
1481 return true;
1483 /* If the address was valid and became invalid, prefer to reload
1484 the memory. Typical case is when the index scale should
1485 correspond the memory. */
1486 *curr_id->operand_loc[nop] = old;
1488 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1490 alter_subreg (curr_id->operand_loc[nop], false);
1491 return true;
1493 else if (CONSTANT_P (reg))
1495 /* Try to simplify subreg of constant. It is usually result of
1496 equivalence substitution. */
1497 if (innermode == VOIDmode
1498 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1499 innermode = curr_static_id->operand[nop].mode;
1500 if ((new_reg = simplify_subreg (mode, reg, innermode,
1501 SUBREG_BYTE (operand))) != NULL_RTX)
1503 *curr_id->operand_loc[nop] = new_reg;
1504 return true;
1507 /* Put constant into memory when we have mixed modes. It generates
1508 a better code in most cases as it does not need a secondary
1509 reload memory. It also prevents LRA looping when LRA is using
1510 secondary reload memory again and again. */
1511 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1512 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1514 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1515 alter_subreg (curr_id->operand_loc[nop], false);
1516 return true;
1518 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1519 if there may be a problem accessing OPERAND in the outer
1520 mode. */
1521 if ((REG_P (reg)
1522 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1523 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1524 /* Don't reload paradoxical subregs because we could be looping
1525 having repeatedly final regno out of hard regs range. */
1526 && (hard_regno_nregs[hard_regno][innermode]
1527 >= hard_regno_nregs[hard_regno][mode])
1528 && simplify_subreg_regno (hard_regno, innermode,
1529 SUBREG_BYTE (operand), mode) < 0
1530 /* Don't reload subreg for matching reload. It is actually
1531 valid subreg in LRA. */
1532 && ! LRA_SUBREG_P (operand))
1533 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1535 enum reg_class rclass;
1537 if (REG_P (reg))
1538 /* There is a big probability that we will get the same class
1539 for the new pseudo and we will get the same insn which
1540 means infinite looping. So spill the new pseudo. */
1541 rclass = NO_REGS;
1542 else
1543 /* The class will be defined later in curr_insn_transform. */
1544 rclass
1545 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1547 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1548 rclass, TRUE, "subreg reg", &new_reg))
1550 bool insert_before, insert_after;
1551 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1553 insert_before = (type != OP_OUT
1554 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1555 insert_after = (type != OP_IN);
1556 insert_move_for_subreg (insert_before ? &before : NULL,
1557 insert_after ? &after : NULL,
1558 reg, new_reg);
1560 SUBREG_REG (operand) = new_reg;
1561 lra_process_new_insns (curr_insn, before, after,
1562 "Inserting subreg reload");
1563 return true;
1565 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1566 IRA allocates hardreg to the inner pseudo reg according to its mode
1567 instead of the outermode, so the size of the hardreg may not be enough
1568 to contain the outermode operand, in that case we may need to insert
1569 reload for the reg. For the following two types of paradoxical subreg,
1570 we need to insert reload:
1571 1. If the op_type is OP_IN, and the hardreg could not be paired with
1572 other hardreg to contain the outermode operand
1573 (checked by in_hard_reg_set_p), we need to insert the reload.
1574 2. If the op_type is OP_OUT or OP_INOUT.
1576 Here is a paradoxical subreg example showing how the reload is generated:
1578 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1579 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1581 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1582 here, if reg107 is assigned to hardreg R15, because R15 is the last
1583 hardreg, compiler cannot find another hardreg to pair with R15 to
1584 contain TImode data. So we insert a TImode reload reg180 for it.
1585 After reload is inserted:
1587 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1588 (reg:DI 107 [ __comp ])) -1
1589 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1590 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1592 Two reload hard registers will be allocated to reg180 to save TImode data
1593 in LRA_assign. */
1594 else if (REG_P (reg)
1595 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1596 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1597 && (hard_regno_nregs[hard_regno][innermode]
1598 < hard_regno_nregs[hard_regno][mode])
1599 && (regclass = lra_get_allocno_class (REGNO (reg)))
1600 && (type != OP_IN
1601 || !in_hard_reg_set_p (reg_class_contents[regclass],
1602 mode, hard_regno)))
1604 /* The class will be defined later in curr_insn_transform. */
1605 enum reg_class rclass
1606 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1608 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1609 rclass, TRUE, "paradoxical subreg", &new_reg))
1611 rtx subreg;
1612 bool insert_before, insert_after;
1614 PUT_MODE (new_reg, mode);
1615 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1616 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1618 insert_before = (type != OP_OUT);
1619 insert_after = (type != OP_IN);
1620 insert_move_for_subreg (insert_before ? &before : NULL,
1621 insert_after ? &after : NULL,
1622 reg, subreg);
1624 SUBREG_REG (operand) = new_reg;
1625 lra_process_new_insns (curr_insn, before, after,
1626 "Inserting paradoxical subreg reload");
1627 return true;
1629 return false;
1632 /* Return TRUE if X refers for a hard register from SET. */
1633 static bool
1634 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1636 int i, j, x_hard_regno;
1637 machine_mode mode;
1638 const char *fmt;
1639 enum rtx_code code;
1641 if (x == NULL_RTX)
1642 return false;
1643 code = GET_CODE (x);
1644 mode = GET_MODE (x);
1645 if (code == SUBREG)
1647 x = SUBREG_REG (x);
1648 code = GET_CODE (x);
1649 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1650 mode = GET_MODE (x);
1653 if (REG_P (x))
1655 x_hard_regno = get_hard_regno (x);
1656 return (x_hard_regno >= 0
1657 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1659 if (MEM_P (x))
1661 struct address_info ad;
1663 decompose_mem_address (&ad, x);
1664 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1665 return true;
1666 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1667 return true;
1669 fmt = GET_RTX_FORMAT (code);
1670 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1672 if (fmt[i] == 'e')
1674 if (uses_hard_regs_p (XEXP (x, i), set))
1675 return true;
1677 else if (fmt[i] == 'E')
1679 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1680 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1681 return true;
1684 return false;
1687 /* Return true if OP is a spilled pseudo. */
1688 static inline bool
1689 spilled_pseudo_p (rtx op)
1691 return (REG_P (op)
1692 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1695 /* Return true if X is a general constant. */
1696 static inline bool
1697 general_constant_p (rtx x)
1699 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1702 static bool
1703 reg_in_class_p (rtx reg, enum reg_class cl)
1705 if (cl == NO_REGS)
1706 return get_reg_class (REGNO (reg)) == NO_REGS;
1707 return in_class_p (reg, cl, NULL);
1710 /* Return true if SET of RCLASS contains no hard regs which can be
1711 used in MODE. */
1712 static bool
1713 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1714 HARD_REG_SET &set,
1715 enum machine_mode mode)
1717 HARD_REG_SET temp;
1719 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1720 COPY_HARD_REG_SET (temp, set);
1721 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1722 return (hard_reg_set_subset_p
1723 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1726 /* Major function to choose the current insn alternative and what
1727 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1728 negative we should consider only this alternative. Return false if
1729 we can not choose the alternative or find how to reload the
1730 operands. */
1731 static bool
1732 process_alt_operands (int only_alternative)
1734 bool ok_p = false;
1735 int nop, overall, nalt;
1736 int n_alternatives = curr_static_id->n_alternatives;
1737 int n_operands = curr_static_id->n_operands;
1738 /* LOSERS counts the operands that don't fit this alternative and
1739 would require loading. */
1740 int losers;
1741 /* REJECT is a count of how undesirable this alternative says it is
1742 if any reloading is required. If the alternative matches exactly
1743 then REJECT is ignored, but otherwise it gets this much counted
1744 against it in addition to the reloading needed. */
1745 int reject;
1746 int op_reject;
1747 /* The number of elements in the following array. */
1748 int early_clobbered_regs_num;
1749 /* Numbers of operands which are early clobber registers. */
1750 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1751 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1752 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1753 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1754 bool curr_alt_win[MAX_RECOG_OPERANDS];
1755 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1756 int curr_alt_matches[MAX_RECOG_OPERANDS];
1757 /* The number of elements in the following array. */
1758 int curr_alt_dont_inherit_ops_num;
1759 /* Numbers of operands whose reload pseudos should not be inherited. */
1760 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1761 rtx op;
1762 /* The register when the operand is a subreg of register, otherwise the
1763 operand itself. */
1764 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1765 /* The register if the operand is a register or subreg of register,
1766 otherwise NULL. */
1767 rtx operand_reg[MAX_RECOG_OPERANDS];
1768 int hard_regno[MAX_RECOG_OPERANDS];
1769 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1770 int reload_nregs, reload_sum;
1771 bool costly_p;
1772 enum reg_class cl;
1774 /* Calculate some data common for all alternatives to speed up the
1775 function. */
1776 for (nop = 0; nop < n_operands; nop++)
1778 rtx reg;
1780 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1781 /* The real hard regno of the operand after the allocation. */
1782 hard_regno[nop] = get_hard_regno (op);
1784 operand_reg[nop] = reg = op;
1785 biggest_mode[nop] = GET_MODE (op);
1786 if (GET_CODE (op) == SUBREG)
1788 operand_reg[nop] = reg = SUBREG_REG (op);
1789 if (GET_MODE_SIZE (biggest_mode[nop])
1790 < GET_MODE_SIZE (GET_MODE (reg)))
1791 biggest_mode[nop] = GET_MODE (reg);
1793 if (! REG_P (reg))
1794 operand_reg[nop] = NULL_RTX;
1795 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1796 || ((int) REGNO (reg)
1797 == lra_get_elimination_hard_regno (REGNO (reg))))
1798 no_subreg_reg_operand[nop] = reg;
1799 else
1800 operand_reg[nop] = no_subreg_reg_operand[nop]
1801 /* Just use natural mode for elimination result. It should
1802 be enough for extra constraints hooks. */
1803 = regno_reg_rtx[hard_regno[nop]];
1806 /* The constraints are made of several alternatives. Each operand's
1807 constraint looks like foo,bar,... with commas separating the
1808 alternatives. The first alternatives for all operands go
1809 together, the second alternatives go together, etc.
1811 First loop over alternatives. */
1812 alternative_mask preferred = curr_id->preferred_alternatives;
1813 if (only_alternative >= 0)
1814 preferred &= ALTERNATIVE_BIT (only_alternative);
1816 for (nalt = 0; nalt < n_alternatives; nalt++)
1818 /* Loop over operands for one constraint alternative. */
1819 if (!TEST_BIT (preferred, nalt))
1820 continue;
1822 overall = losers = reject = reload_nregs = reload_sum = 0;
1823 for (nop = 0; nop < n_operands; nop++)
1825 int inc = (curr_static_id
1826 ->operand_alternative[nalt * n_operands + nop].reject);
1827 if (lra_dump_file != NULL && inc != 0)
1828 fprintf (lra_dump_file,
1829 " Staticly defined alt reject+=%d\n", inc);
1830 reject += inc;
1832 early_clobbered_regs_num = 0;
1834 for (nop = 0; nop < n_operands; nop++)
1836 const char *p;
1837 char *end;
1838 int len, c, m, i, opalt_num, this_alternative_matches;
1839 bool win, did_match, offmemok, early_clobber_p;
1840 /* false => this operand can be reloaded somehow for this
1841 alternative. */
1842 bool badop;
1843 /* true => this operand can be reloaded if the alternative
1844 allows regs. */
1845 bool winreg;
1846 /* True if a constant forced into memory would be OK for
1847 this operand. */
1848 bool constmemok;
1849 enum reg_class this_alternative, this_costly_alternative;
1850 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1851 bool this_alternative_match_win, this_alternative_win;
1852 bool this_alternative_offmemok;
1853 bool scratch_p;
1854 machine_mode mode;
1855 enum constraint_num cn;
1857 opalt_num = nalt * n_operands + nop;
1858 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1860 /* Fast track for no constraints at all. */
1861 curr_alt[nop] = NO_REGS;
1862 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1863 curr_alt_win[nop] = true;
1864 curr_alt_match_win[nop] = false;
1865 curr_alt_offmemok[nop] = false;
1866 curr_alt_matches[nop] = -1;
1867 continue;
1870 op = no_subreg_reg_operand[nop];
1871 mode = curr_operand_mode[nop];
1873 win = did_match = winreg = offmemok = constmemok = false;
1874 badop = true;
1876 early_clobber_p = false;
1877 p = curr_static_id->operand_alternative[opalt_num].constraint;
1879 this_costly_alternative = this_alternative = NO_REGS;
1880 /* We update set of possible hard regs besides its class
1881 because reg class might be inaccurate. For example,
1882 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1883 is translated in HI_REGS because classes are merged by
1884 pairs and there is no accurate intermediate class. */
1885 CLEAR_HARD_REG_SET (this_alternative_set);
1886 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1887 this_alternative_win = false;
1888 this_alternative_match_win = false;
1889 this_alternative_offmemok = false;
1890 this_alternative_matches = -1;
1892 /* An empty constraint should be excluded by the fast
1893 track. */
1894 lra_assert (*p != 0 && *p != ',');
1896 op_reject = 0;
1897 /* Scan this alternative's specs for this operand; set WIN
1898 if the operand fits any letter in this alternative.
1899 Otherwise, clear BADOP if this operand could fit some
1900 letter after reloads, or set WINREG if this operand could
1901 fit after reloads provided the constraint allows some
1902 registers. */
1903 costly_p = false;
1906 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1908 case '\0':
1909 len = 0;
1910 break;
1911 case ',':
1912 c = '\0';
1913 break;
1915 case '&':
1916 early_clobber_p = true;
1917 break;
1919 case '$':
1920 op_reject += LRA_MAX_REJECT;
1921 break;
1922 case '^':
1923 op_reject += LRA_LOSER_COST_FACTOR;
1924 break;
1926 case '#':
1927 /* Ignore rest of this alternative. */
1928 c = '\0';
1929 break;
1931 case '0': case '1': case '2': case '3': case '4':
1932 case '5': case '6': case '7': case '8': case '9':
1934 int m_hregno;
1935 bool match_p;
1937 m = strtoul (p, &end, 10);
1938 p = end;
1939 len = 0;
1940 lra_assert (nop > m);
1942 this_alternative_matches = m;
1943 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1944 /* We are supposed to match a previous operand.
1945 If we do, we win if that one did. If we do
1946 not, count both of the operands as losers.
1947 (This is too conservative, since most of the
1948 time only a single reload insn will be needed
1949 to make the two operands win. As a result,
1950 this alternative may be rejected when it is
1951 actually desirable.) */
1952 match_p = false;
1953 if (operands_match_p (*curr_id->operand_loc[nop],
1954 *curr_id->operand_loc[m], m_hregno))
1956 /* We should reject matching of an early
1957 clobber operand if the matching operand is
1958 not dying in the insn. */
1959 if (! curr_static_id->operand[m].early_clobber
1960 || operand_reg[nop] == NULL_RTX
1961 || (find_regno_note (curr_insn, REG_DEAD,
1962 REGNO (op))
1963 || REGNO (op) == REGNO (operand_reg[m])))
1964 match_p = true;
1966 if (match_p)
1968 /* If we are matching a non-offsettable
1969 address where an offsettable address was
1970 expected, then we must reject this
1971 combination, because we can't reload
1972 it. */
1973 if (curr_alt_offmemok[m]
1974 && MEM_P (*curr_id->operand_loc[m])
1975 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1976 continue;
1978 else
1980 /* Operands don't match. Both operands must
1981 allow a reload register, otherwise we
1982 cannot make them match. */
1983 if (curr_alt[m] == NO_REGS)
1984 break;
1985 /* Retroactively mark the operand we had to
1986 match as a loser, if it wasn't already and
1987 it wasn't matched to a register constraint
1988 (e.g it might be matched by memory). */
1989 if (curr_alt_win[m]
1990 && (operand_reg[m] == NULL_RTX
1991 || hard_regno[m] < 0))
1993 losers++;
1994 reload_nregs
1995 += (ira_reg_class_max_nregs[curr_alt[m]]
1996 [GET_MODE (*curr_id->operand_loc[m])]);
1999 /* Prefer matching earlyclobber alternative as
2000 it results in less hard regs required for
2001 the insn than a non-matching earlyclobber
2002 alternative. */
2003 if (curr_static_id->operand[m].early_clobber)
2005 if (lra_dump_file != NULL)
2006 fprintf
2007 (lra_dump_file,
2008 " %d Matching earlyclobber alt:"
2009 " reject--\n",
2010 nop);
2011 reject--;
2013 /* Otherwise we prefer no matching
2014 alternatives because it gives more freedom
2015 in RA. */
2016 else if (operand_reg[nop] == NULL_RTX
2017 || (find_regno_note (curr_insn, REG_DEAD,
2018 REGNO (operand_reg[nop]))
2019 == NULL_RTX))
2021 if (lra_dump_file != NULL)
2022 fprintf
2023 (lra_dump_file,
2024 " %d Matching alt: reject+=2\n",
2025 nop);
2026 reject += 2;
2029 /* If we have to reload this operand and some
2030 previous operand also had to match the same
2031 thing as this operand, we don't know how to do
2032 that. */
2033 if (!match_p || !curr_alt_win[m])
2035 for (i = 0; i < nop; i++)
2036 if (curr_alt_matches[i] == m)
2037 break;
2038 if (i < nop)
2039 break;
2041 else
2042 did_match = true;
2044 /* This can be fixed with reloads if the operand
2045 we are supposed to match can be fixed with
2046 reloads. */
2047 badop = false;
2048 this_alternative = curr_alt[m];
2049 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
2050 winreg = this_alternative != NO_REGS;
2051 break;
2054 case 'g':
2055 if (MEM_P (op)
2056 || general_constant_p (op)
2057 || spilled_pseudo_p (op))
2058 win = true;
2059 cl = GENERAL_REGS;
2060 goto reg;
2062 default:
2063 cn = lookup_constraint (p);
2064 switch (get_constraint_type (cn))
2066 case CT_REGISTER:
2067 cl = reg_class_for_constraint (cn);
2068 if (cl != NO_REGS)
2069 goto reg;
2070 break;
2072 case CT_CONST_INT:
2073 if (CONST_INT_P (op)
2074 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2075 win = true;
2076 break;
2078 case CT_MEMORY:
2079 if (MEM_P (op)
2080 && satisfies_memory_constraint_p (op, cn))
2081 win = true;
2082 else if (spilled_pseudo_p (op))
2083 win = true;
2085 /* If we didn't already win, we can reload constants
2086 via force_const_mem or put the pseudo value into
2087 memory, or make other memory by reloading the
2088 address like for 'o'. */
2089 if (CONST_POOL_OK_P (mode, op)
2090 || MEM_P (op) || REG_P (op))
2091 badop = false;
2092 constmemok = true;
2093 offmemok = true;
2094 break;
2096 case CT_ADDRESS:
2097 /* If we didn't already win, we can reload the address
2098 into a base register. */
2099 if (satisfies_address_constraint_p (op, cn))
2100 win = true;
2101 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2102 ADDRESS, SCRATCH);
2103 badop = false;
2104 goto reg;
2106 case CT_FIXED_FORM:
2107 if (constraint_satisfied_p (op, cn))
2108 win = true;
2109 break;
2111 case CT_SPECIAL_MEMORY:
2112 if (MEM_P (op)
2113 && satisfies_memory_constraint_p (op, cn))
2114 win = true;
2115 else if (spilled_pseudo_p (op))
2116 win = true;
2117 break;
2119 break;
2121 reg:
2122 this_alternative = reg_class_subunion[this_alternative][cl];
2123 IOR_HARD_REG_SET (this_alternative_set,
2124 reg_class_contents[cl]);
2125 if (costly_p)
2127 this_costly_alternative
2128 = reg_class_subunion[this_costly_alternative][cl];
2129 IOR_HARD_REG_SET (this_costly_alternative_set,
2130 reg_class_contents[cl]);
2132 if (mode == BLKmode)
2133 break;
2134 winreg = true;
2135 if (REG_P (op))
2137 if (hard_regno[nop] >= 0
2138 && in_hard_reg_set_p (this_alternative_set,
2139 mode, hard_regno[nop]))
2140 win = true;
2141 else if (hard_regno[nop] < 0
2142 && in_class_p (op, this_alternative, NULL))
2143 win = true;
2145 break;
2147 if (c != ' ' && c != '\t')
2148 costly_p = c == '*';
2150 while ((p += len), c);
2152 scratch_p = (operand_reg[nop] != NULL_RTX
2153 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2154 /* Record which operands fit this alternative. */
2155 if (win)
2157 this_alternative_win = true;
2158 if (operand_reg[nop] != NULL_RTX)
2160 if (hard_regno[nop] >= 0)
2162 if (in_hard_reg_set_p (this_costly_alternative_set,
2163 mode, hard_regno[nop]))
2165 if (lra_dump_file != NULL)
2166 fprintf (lra_dump_file,
2167 " %d Costly set: reject++\n",
2168 nop);
2169 reject++;
2172 else
2174 /* Prefer won reg to spilled pseudo under other
2175 equal conditions for possibe inheritance. */
2176 if (! scratch_p)
2178 if (lra_dump_file != NULL)
2179 fprintf
2180 (lra_dump_file,
2181 " %d Non pseudo reload: reject++\n",
2182 nop);
2183 reject++;
2185 if (in_class_p (operand_reg[nop],
2186 this_costly_alternative, NULL))
2188 if (lra_dump_file != NULL)
2189 fprintf
2190 (lra_dump_file,
2191 " %d Non pseudo costly reload:"
2192 " reject++\n",
2193 nop);
2194 reject++;
2197 /* We simulate the behavior of old reload here.
2198 Although scratches need hard registers and it
2199 might result in spilling other pseudos, no reload
2200 insns are generated for the scratches. So it
2201 might cost something but probably less than old
2202 reload pass believes. */
2203 if (scratch_p)
2205 if (lra_dump_file != NULL)
2206 fprintf (lra_dump_file,
2207 " %d Scratch win: reject+=2\n",
2208 nop);
2209 reject += 2;
2213 else if (did_match)
2214 this_alternative_match_win = true;
2215 else
2217 int const_to_mem = 0;
2218 bool no_regs_p;
2220 reject += op_reject;
2221 /* Never do output reload of stack pointer. It makes
2222 impossible to do elimination when SP is changed in
2223 RTL. */
2224 if (op == stack_pointer_rtx && ! frame_pointer_needed
2225 && curr_static_id->operand[nop].type != OP_IN)
2226 goto fail;
2228 /* If this alternative asks for a specific reg class, see if there
2229 is at least one allocatable register in that class. */
2230 no_regs_p
2231 = (this_alternative == NO_REGS
2232 || (hard_reg_set_subset_p
2233 (reg_class_contents[this_alternative],
2234 lra_no_alloc_regs)));
2236 /* For asms, verify that the class for this alternative is possible
2237 for the mode that is specified. */
2238 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2240 int i;
2241 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2242 if (HARD_REGNO_MODE_OK (i, mode)
2243 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2244 mode, i))
2245 break;
2246 if (i == FIRST_PSEUDO_REGISTER)
2247 winreg = false;
2250 /* If this operand accepts a register, and if the
2251 register class has at least one allocatable register,
2252 then this operand can be reloaded. */
2253 if (winreg && !no_regs_p)
2254 badop = false;
2256 if (badop)
2258 if (lra_dump_file != NULL)
2259 fprintf (lra_dump_file,
2260 " alt=%d: Bad operand -- refuse\n",
2261 nalt);
2262 goto fail;
2265 /* If not assigned pseudo has a class which a subset of
2266 required reg class, it is a less costly alternative
2267 as the pseudo still can get a hard reg of necessary
2268 class. */
2269 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2270 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2271 && ira_class_subset_p[this_alternative][cl])
2273 if (lra_dump_file != NULL)
2274 fprintf
2275 (lra_dump_file,
2276 " %d Super set class reg: reject-=3\n", nop);
2277 reject -= 3;
2280 this_alternative_offmemok = offmemok;
2281 if (this_costly_alternative != NO_REGS)
2283 if (lra_dump_file != NULL)
2284 fprintf (lra_dump_file,
2285 " %d Costly loser: reject++\n", nop);
2286 reject++;
2288 /* If the operand is dying, has a matching constraint,
2289 and satisfies constraints of the matched operand
2290 which failed to satisfy the own constraints, most probably
2291 the reload for this operand will be gone. */
2292 if (this_alternative_matches >= 0
2293 && !curr_alt_win[this_alternative_matches]
2294 && REG_P (op)
2295 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2296 && (hard_regno[nop] >= 0
2297 ? in_hard_reg_set_p (this_alternative_set,
2298 mode, hard_regno[nop])
2299 : in_class_p (op, this_alternative, NULL)))
2301 if (lra_dump_file != NULL)
2302 fprintf
2303 (lra_dump_file,
2304 " %d Dying matched operand reload: reject++\n",
2305 nop);
2306 reject++;
2308 else
2310 /* Strict_low_part requires to reload the register
2311 not the sub-register. In this case we should
2312 check that a final reload hard reg can hold the
2313 value mode. */
2314 if (curr_static_id->operand[nop].strict_low
2315 && REG_P (op)
2316 && hard_regno[nop] < 0
2317 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2318 && ira_class_hard_regs_num[this_alternative] > 0
2319 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2320 [this_alternative][0],
2321 GET_MODE
2322 (*curr_id->operand_loc[nop])))
2324 if (lra_dump_file != NULL)
2325 fprintf
2326 (lra_dump_file,
2327 " alt=%d: Strict low subreg reload -- refuse\n",
2328 nalt);
2329 goto fail;
2331 losers++;
2333 if (operand_reg[nop] != NULL_RTX
2334 /* Output operands and matched input operands are
2335 not inherited. The following conditions do not
2336 exactly describe the previous statement but they
2337 are pretty close. */
2338 && curr_static_id->operand[nop].type != OP_OUT
2339 && (this_alternative_matches < 0
2340 || curr_static_id->operand[nop].type != OP_IN))
2342 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2343 (operand_reg[nop])]
2344 .last_reload);
2346 /* The value of reload_sum has sense only if we
2347 process insns in their order. It happens only on
2348 the first constraints sub-pass when we do most of
2349 reload work. */
2350 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2351 reload_sum += last_reload - bb_reload_num;
2353 /* If this is a constant that is reloaded into the
2354 desired class by copying it to memory first, count
2355 that as another reload. This is consistent with
2356 other code and is required to avoid choosing another
2357 alternative when the constant is moved into memory.
2358 Note that the test here is precisely the same as in
2359 the code below that calls force_const_mem. */
2360 if (CONST_POOL_OK_P (mode, op)
2361 && ((targetm.preferred_reload_class
2362 (op, this_alternative) == NO_REGS)
2363 || no_input_reloads_p))
2365 const_to_mem = 1;
2366 if (! no_regs_p)
2367 losers++;
2370 /* Alternative loses if it requires a type of reload not
2371 permitted for this insn. We can always reload
2372 objects with a REG_UNUSED note. */
2373 if ((curr_static_id->operand[nop].type != OP_IN
2374 && no_output_reloads_p
2375 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2376 || (curr_static_id->operand[nop].type != OP_OUT
2377 && no_input_reloads_p && ! const_to_mem)
2378 || (this_alternative_matches >= 0
2379 && (no_input_reloads_p
2380 || (no_output_reloads_p
2381 && (curr_static_id->operand
2382 [this_alternative_matches].type != OP_IN)
2383 && ! find_reg_note (curr_insn, REG_UNUSED,
2384 no_subreg_reg_operand
2385 [this_alternative_matches])))))
2387 if (lra_dump_file != NULL)
2388 fprintf
2389 (lra_dump_file,
2390 " alt=%d: No input/otput reload -- refuse\n",
2391 nalt);
2392 goto fail;
2395 /* Alternative loses if it required class pseudo can not
2396 hold value of required mode. Such insns can be
2397 described by insn definitions with mode iterators. */
2398 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2399 && ! hard_reg_set_empty_p (this_alternative_set)
2400 /* It is common practice for constraints to use a
2401 class which does not have actually enough regs to
2402 hold the value (e.g. x86 AREG for mode requiring
2403 more one general reg). Therefore we have 2
2404 conditions to check that the reload pseudo can
2405 not hold the mode value. */
2406 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2407 [this_alternative][0],
2408 GET_MODE (*curr_id->operand_loc[nop]))
2409 /* The above condition is not enough as the first
2410 reg in ira_class_hard_regs can be not aligned for
2411 multi-words mode values. */
2412 && (prohibited_class_reg_set_mode_p
2413 (this_alternative, this_alternative_set,
2414 GET_MODE (*curr_id->operand_loc[nop]))))
2416 if (lra_dump_file != NULL)
2417 fprintf (lra_dump_file,
2418 " alt=%d: reload pseudo for op %d "
2419 " can not hold the mode value -- refuse\n",
2420 nalt, nop);
2421 goto fail;
2424 /* Check strong discouragement of reload of non-constant
2425 into class THIS_ALTERNATIVE. */
2426 if (! CONSTANT_P (op) && ! no_regs_p
2427 && (targetm.preferred_reload_class
2428 (op, this_alternative) == NO_REGS
2429 || (curr_static_id->operand[nop].type == OP_OUT
2430 && (targetm.preferred_output_reload_class
2431 (op, this_alternative) == NO_REGS))))
2433 if (lra_dump_file != NULL)
2434 fprintf (lra_dump_file,
2435 " %d Non-prefered reload: reject+=%d\n",
2436 nop, LRA_MAX_REJECT);
2437 reject += LRA_MAX_REJECT;
2440 if (! (MEM_P (op) && offmemok)
2441 && ! (const_to_mem && constmemok))
2443 /* We prefer to reload pseudos over reloading other
2444 things, since such reloads may be able to be
2445 eliminated later. So bump REJECT in other cases.
2446 Don't do this in the case where we are forcing a
2447 constant into memory and it will then win since
2448 we don't want to have a different alternative
2449 match then. */
2450 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2452 if (lra_dump_file != NULL)
2453 fprintf
2454 (lra_dump_file,
2455 " %d Non-pseudo reload: reject+=2\n",
2456 nop);
2457 reject += 2;
2460 if (! no_regs_p)
2461 reload_nregs
2462 += ira_reg_class_max_nregs[this_alternative][mode];
2464 if (SMALL_REGISTER_CLASS_P (this_alternative))
2466 if (lra_dump_file != NULL)
2467 fprintf
2468 (lra_dump_file,
2469 " %d Small class reload: reject+=%d\n",
2470 nop, LRA_LOSER_COST_FACTOR / 2);
2471 reject += LRA_LOSER_COST_FACTOR / 2;
2475 /* We are trying to spill pseudo into memory. It is
2476 usually more costly than moving to a hard register
2477 although it might takes the same number of
2478 reloads. */
2479 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2481 if (lra_dump_file != NULL)
2482 fprintf
2483 (lra_dump_file,
2484 " %d Spill pseudo into memory: reject+=3\n",
2485 nop);
2486 reject += 3;
2487 if (VECTOR_MODE_P (mode))
2489 /* Spilling vectors into memory is usually more
2490 costly as they contain big values. */
2491 if (lra_dump_file != NULL)
2492 fprintf
2493 (lra_dump_file,
2494 " %d Spill vector pseudo: reject+=2\n",
2495 nop);
2496 reject += 2;
2500 #ifdef SECONDARY_MEMORY_NEEDED
2501 /* If reload requires moving value through secondary
2502 memory, it will need one more insn at least. */
2503 if (this_alternative != NO_REGS
2504 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2505 && ((curr_static_id->operand[nop].type != OP_OUT
2506 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2507 GET_MODE (op)))
2508 || (curr_static_id->operand[nop].type != OP_IN
2509 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2510 GET_MODE (op)))))
2511 losers++;
2512 #endif
2513 /* Input reloads can be inherited more often than output
2514 reloads can be removed, so penalize output
2515 reloads. */
2516 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2518 if (lra_dump_file != NULL)
2519 fprintf
2520 (lra_dump_file,
2521 " %d Non input pseudo reload: reject++\n",
2522 nop);
2523 reject++;
2527 if (early_clobber_p && ! scratch_p)
2529 if (lra_dump_file != NULL)
2530 fprintf (lra_dump_file,
2531 " %d Early clobber: reject++\n", nop);
2532 reject++;
2534 /* ??? We check early clobbers after processing all operands
2535 (see loop below) and there we update the costs more.
2536 Should we update the cost (may be approximately) here
2537 because of early clobber register reloads or it is a rare
2538 or non-important thing to be worth to do it. */
2539 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2540 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2542 if (lra_dump_file != NULL)
2543 fprintf (lra_dump_file,
2544 " alt=%d,overall=%d,losers=%d -- refuse\n",
2545 nalt, overall, losers);
2546 goto fail;
2549 curr_alt[nop] = this_alternative;
2550 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2551 curr_alt_win[nop] = this_alternative_win;
2552 curr_alt_match_win[nop] = this_alternative_match_win;
2553 curr_alt_offmemok[nop] = this_alternative_offmemok;
2554 curr_alt_matches[nop] = this_alternative_matches;
2556 if (this_alternative_matches >= 0
2557 && !did_match && !this_alternative_win)
2558 curr_alt_win[this_alternative_matches] = false;
2560 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2561 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2563 if (curr_insn_set != NULL_RTX && n_operands == 2
2564 /* Prevent processing non-move insns. */
2565 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2566 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2567 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2568 && REG_P (no_subreg_reg_operand[0])
2569 && REG_P (no_subreg_reg_operand[1])
2570 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2571 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2572 || (! curr_alt_win[0] && curr_alt_win[1]
2573 && REG_P (no_subreg_reg_operand[1])
2574 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2575 || (curr_alt_win[0] && ! curr_alt_win[1]
2576 && REG_P (no_subreg_reg_operand[0])
2577 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2578 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2579 no_subreg_reg_operand[1])
2580 || (targetm.preferred_reload_class
2581 (no_subreg_reg_operand[1],
2582 (enum reg_class) curr_alt[1]) != NO_REGS))
2583 /* If it is a result of recent elimination in move
2584 insn we can transform it into an add still by
2585 using this alternative. */
2586 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2588 /* We have a move insn and a new reload insn will be similar
2589 to the current insn. We should avoid such situation as it
2590 results in LRA cycling. */
2591 overall += LRA_MAX_REJECT;
2593 ok_p = true;
2594 curr_alt_dont_inherit_ops_num = 0;
2595 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2597 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2598 HARD_REG_SET temp_set;
2600 i = early_clobbered_nops[nop];
2601 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2602 || hard_regno[i] < 0)
2603 continue;
2604 lra_assert (operand_reg[i] != NULL_RTX);
2605 clobbered_hard_regno = hard_regno[i];
2606 CLEAR_HARD_REG_SET (temp_set);
2607 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2608 first_conflict_j = last_conflict_j = -1;
2609 for (j = 0; j < n_operands; j++)
2610 if (j == i
2611 /* We don't want process insides of match_operator and
2612 match_parallel because otherwise we would process
2613 their operands once again generating a wrong
2614 code. */
2615 || curr_static_id->operand[j].is_operator)
2616 continue;
2617 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2618 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2619 continue;
2620 /* If we don't reload j-th operand, check conflicts. */
2621 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2622 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2624 if (first_conflict_j < 0)
2625 first_conflict_j = j;
2626 last_conflict_j = j;
2628 if (last_conflict_j < 0)
2629 continue;
2630 /* If earlyclobber operand conflicts with another
2631 non-matching operand which is actually the same register
2632 as the earlyclobber operand, it is better to reload the
2633 another operand as an operand matching the earlyclobber
2634 operand can be also the same. */
2635 if (first_conflict_j == last_conflict_j
2636 && operand_reg[last_conflict_j] != NULL_RTX
2637 && ! curr_alt_match_win[last_conflict_j]
2638 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2640 curr_alt_win[last_conflict_j] = false;
2641 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2642 = last_conflict_j;
2643 losers++;
2644 /* Early clobber was already reflected in REJECT. */
2645 lra_assert (reject > 0);
2646 if (lra_dump_file != NULL)
2647 fprintf
2648 (lra_dump_file,
2649 " %d Conflict early clobber reload: reject--\n",
2651 reject--;
2652 overall += LRA_LOSER_COST_FACTOR - 1;
2654 else
2656 /* We need to reload early clobbered register and the
2657 matched registers. */
2658 for (j = 0; j < n_operands; j++)
2659 if (curr_alt_matches[j] == i)
2661 curr_alt_match_win[j] = false;
2662 losers++;
2663 overall += LRA_LOSER_COST_FACTOR;
2665 if (! curr_alt_match_win[i])
2666 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2667 else
2669 /* Remember pseudos used for match reloads are never
2670 inherited. */
2671 lra_assert (curr_alt_matches[i] >= 0);
2672 curr_alt_win[curr_alt_matches[i]] = false;
2674 curr_alt_win[i] = curr_alt_match_win[i] = false;
2675 losers++;
2676 /* Early clobber was already reflected in REJECT. */
2677 lra_assert (reject > 0);
2678 if (lra_dump_file != NULL)
2679 fprintf
2680 (lra_dump_file,
2681 " %d Matched conflict early clobber reloads:"
2682 "reject--\n",
2684 reject--;
2685 overall += LRA_LOSER_COST_FACTOR - 1;
2688 if (lra_dump_file != NULL)
2689 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2690 nalt, overall, losers, reload_nregs);
2692 /* If this alternative can be made to work by reloading, and it
2693 needs less reloading than the others checked so far, record
2694 it as the chosen goal for reloading. */
2695 if ((best_losers != 0 && losers == 0)
2696 || (((best_losers == 0 && losers == 0)
2697 || (best_losers != 0 && losers != 0))
2698 && (best_overall > overall
2699 || (best_overall == overall
2700 /* If the cost of the reloads is the same,
2701 prefer alternative which requires minimal
2702 number of reload regs. */
2703 && (reload_nregs < best_reload_nregs
2704 || (reload_nregs == best_reload_nregs
2705 && (best_reload_sum < reload_sum
2706 || (best_reload_sum == reload_sum
2707 && nalt < goal_alt_number))))))))
2709 for (nop = 0; nop < n_operands; nop++)
2711 goal_alt_win[nop] = curr_alt_win[nop];
2712 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2713 goal_alt_matches[nop] = curr_alt_matches[nop];
2714 goal_alt[nop] = curr_alt[nop];
2715 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2717 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2718 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2719 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2720 goal_alt_swapped = curr_swapped;
2721 best_overall = overall;
2722 best_losers = losers;
2723 best_reload_nregs = reload_nregs;
2724 best_reload_sum = reload_sum;
2725 goal_alt_number = nalt;
2727 if (losers == 0)
2728 /* Everything is satisfied. Do not process alternatives
2729 anymore. */
2730 break;
2731 fail:
2734 return ok_p;
2737 /* Make reload base reg from address AD. */
2738 static rtx
2739 base_to_reg (struct address_info *ad)
2741 enum reg_class cl;
2742 int code = -1;
2743 rtx new_inner = NULL_RTX;
2744 rtx new_reg = NULL_RTX;
2745 rtx_insn *insn;
2746 rtx_insn *last_insn = get_last_insn();
2748 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2749 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2750 get_index_code (ad));
2751 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2752 cl, "base");
2753 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2754 ad->disp_term == NULL
2755 ? gen_int_mode (0, ad->mode)
2756 : *ad->disp_term);
2757 if (!valid_address_p (ad->mode, new_inner, ad->as))
2758 return NULL_RTX;
2759 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base_term));
2760 code = recog_memoized (insn);
2761 if (code < 0)
2763 delete_insns_since (last_insn);
2764 return NULL_RTX;
2767 return new_inner;
2770 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2771 static rtx
2772 base_plus_disp_to_reg (struct address_info *ad)
2774 enum reg_class cl;
2775 rtx new_reg;
2777 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2778 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2779 get_index_code (ad));
2780 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2781 cl, "base + disp");
2782 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2783 return new_reg;
2786 /* Make reload of index part of address AD. Return the new
2787 pseudo. */
2788 static rtx
2789 index_part_to_reg (struct address_info *ad)
2791 rtx new_reg;
2793 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2794 INDEX_REG_CLASS, "index term");
2795 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2796 GEN_INT (get_index_scale (ad)), new_reg, 1);
2797 return new_reg;
2800 /* Return true if we can add a displacement to address AD, even if that
2801 makes the address invalid. The fix-up code requires any new address
2802 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2803 static bool
2804 can_add_disp_p (struct address_info *ad)
2806 return (!ad->autoinc_p
2807 && ad->segment == NULL
2808 && ad->base == ad->base_term
2809 && ad->disp == ad->disp_term);
2812 /* Make equiv substitution in address AD. Return true if a substitution
2813 was made. */
2814 static bool
2815 equiv_address_substitution (struct address_info *ad)
2817 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2818 HOST_WIDE_INT disp, scale;
2819 bool change_p;
2821 base_term = strip_subreg (ad->base_term);
2822 if (base_term == NULL)
2823 base_reg = new_base_reg = NULL_RTX;
2824 else
2826 base_reg = *base_term;
2827 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2829 index_term = strip_subreg (ad->index_term);
2830 if (index_term == NULL)
2831 index_reg = new_index_reg = NULL_RTX;
2832 else
2834 index_reg = *index_term;
2835 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2837 if (base_reg == new_base_reg && index_reg == new_index_reg)
2838 return false;
2839 disp = 0;
2840 change_p = false;
2841 if (lra_dump_file != NULL)
2843 fprintf (lra_dump_file, "Changing address in insn %d ",
2844 INSN_UID (curr_insn));
2845 dump_value_slim (lra_dump_file, *ad->outer, 1);
2847 if (base_reg != new_base_reg)
2849 if (REG_P (new_base_reg))
2851 *base_term = new_base_reg;
2852 change_p = true;
2854 else if (GET_CODE (new_base_reg) == PLUS
2855 && REG_P (XEXP (new_base_reg, 0))
2856 && CONST_INT_P (XEXP (new_base_reg, 1))
2857 && can_add_disp_p (ad))
2859 disp += INTVAL (XEXP (new_base_reg, 1));
2860 *base_term = XEXP (new_base_reg, 0);
2861 change_p = true;
2863 if (ad->base_term2 != NULL)
2864 *ad->base_term2 = *ad->base_term;
2866 if (index_reg != new_index_reg)
2868 if (REG_P (new_index_reg))
2870 *index_term = new_index_reg;
2871 change_p = true;
2873 else if (GET_CODE (new_index_reg) == PLUS
2874 && REG_P (XEXP (new_index_reg, 0))
2875 && CONST_INT_P (XEXP (new_index_reg, 1))
2876 && can_add_disp_p (ad)
2877 && (scale = get_index_scale (ad)))
2879 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2880 *index_term = XEXP (new_index_reg, 0);
2881 change_p = true;
2884 if (disp != 0)
2886 if (ad->disp != NULL)
2887 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2888 else
2890 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2891 update_address (ad);
2893 change_p = true;
2895 if (lra_dump_file != NULL)
2897 if (! change_p)
2898 fprintf (lra_dump_file, " -- no change\n");
2899 else
2901 fprintf (lra_dump_file, " on equiv ");
2902 dump_value_slim (lra_dump_file, *ad->outer, 1);
2903 fprintf (lra_dump_file, "\n");
2906 return change_p;
2909 /* Major function to make reloads for an address in operand NOP or
2910 check its correctness (If CHECK_ONLY_P is true). The supported
2911 cases are:
2913 1) an address that existed before LRA started, at which point it
2914 must have been valid. These addresses are subject to elimination
2915 and may have become invalid due to the elimination offset being out
2916 of range.
2918 2) an address created by forcing a constant to memory
2919 (force_const_to_mem). The initial form of these addresses might
2920 not be valid, and it is this function's job to make them valid.
2922 3) a frame address formed from a register and a (possibly zero)
2923 constant offset. As above, these addresses might not be valid and
2924 this function must make them so.
2926 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2927 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2928 address. Return true for any RTL change.
2930 The function is a helper function which does not produce all
2931 transformations (when CHECK_ONLY_P is false) which can be
2932 necessary. It does just basic steps. To do all necessary
2933 transformations use function process_address. */
2934 static bool
2935 process_address_1 (int nop, bool check_only_p,
2936 rtx_insn **before, rtx_insn **after)
2938 struct address_info ad;
2939 rtx new_reg;
2940 HOST_WIDE_INT scale;
2941 rtx op = *curr_id->operand_loc[nop];
2942 const char *constraint = curr_static_id->operand[nop].constraint;
2943 enum constraint_num cn = lookup_constraint (constraint);
2944 bool change_p = false;
2946 if (MEM_P (op)
2947 && GET_MODE (op) == BLKmode
2948 && GET_CODE (XEXP (op, 0)) == SCRATCH)
2949 return false;
2951 if (insn_extra_address_constraint (cn))
2952 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2953 else if (MEM_P (op))
2954 decompose_mem_address (&ad, op);
2955 else if (GET_CODE (op) == SUBREG
2956 && MEM_P (SUBREG_REG (op)))
2957 decompose_mem_address (&ad, SUBREG_REG (op));
2958 else
2959 return false;
2960 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
2961 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
2962 when INDEX_REG_CLASS is a single register class. */
2963 if (ad.base_term != NULL
2964 && ad.index_term != NULL
2965 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
2966 && REG_P (*ad.base_term)
2967 && REG_P (*ad.index_term)
2968 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
2969 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
2971 std::swap (ad.base, ad.index);
2972 std::swap (ad.base_term, ad.index_term);
2974 if (! check_only_p)
2975 change_p = equiv_address_substitution (&ad);
2976 if (ad.base_term != NULL
2977 && (process_addr_reg
2978 (ad.base_term, check_only_p, before,
2979 (ad.autoinc_p
2980 && !(REG_P (*ad.base_term)
2981 && find_regno_note (curr_insn, REG_DEAD,
2982 REGNO (*ad.base_term)) != NULL_RTX)
2983 ? after : NULL),
2984 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2985 get_index_code (&ad)))))
2987 change_p = true;
2988 if (ad.base_term2 != NULL)
2989 *ad.base_term2 = *ad.base_term;
2991 if (ad.index_term != NULL
2992 && process_addr_reg (ad.index_term, check_only_p,
2993 before, NULL, INDEX_REG_CLASS))
2994 change_p = true;
2996 /* Target hooks sometimes don't treat extra-constraint addresses as
2997 legitimate address_operands, so handle them specially. */
2998 if (insn_extra_address_constraint (cn)
2999 && satisfies_address_constraint_p (&ad, cn))
3000 return change_p;
3002 if (check_only_p)
3003 return change_p;
3005 /* There are three cases where the shape of *AD.INNER may now be invalid:
3007 1) the original address was valid, but either elimination or
3008 equiv_address_substitution was applied and that made
3009 the address invalid.
3011 2) the address is an invalid symbolic address created by
3012 force_const_to_mem.
3014 3) the address is a frame address with an invalid offset.
3016 4) the address is a frame address with an invalid base.
3018 All these cases involve a non-autoinc address, so there is no
3019 point revalidating other types. */
3020 if (ad.autoinc_p || valid_address_p (&ad))
3021 return change_p;
3023 /* Any index existed before LRA started, so we can assume that the
3024 presence and shape of the index is valid. */
3025 push_to_sequence (*before);
3026 lra_assert (ad.disp == ad.disp_term);
3027 if (ad.base == NULL)
3029 if (ad.index == NULL)
3031 rtx_insn *insn;
3032 rtx_insn *last = get_last_insn ();
3033 int code = -1;
3034 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3035 SCRATCH, SCRATCH);
3036 rtx addr = *ad.inner;
3038 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3039 if (HAVE_lo_sum)
3041 /* addr => lo_sum (new_base, addr), case (2) above. */
3042 insn = emit_insn (gen_rtx_SET
3043 (new_reg,
3044 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
3045 code = recog_memoized (insn);
3046 if (code >= 0)
3048 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
3049 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
3051 /* Try to put lo_sum into register. */
3052 insn = emit_insn (gen_rtx_SET
3053 (new_reg,
3054 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
3055 code = recog_memoized (insn);
3056 if (code >= 0)
3058 *ad.inner = new_reg;
3059 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
3061 *ad.inner = addr;
3062 code = -1;
3068 if (code < 0)
3069 delete_insns_since (last);
3072 if (code < 0)
3074 /* addr => new_base, case (2) above. */
3075 lra_emit_move (new_reg, addr);
3077 for (insn = last == NULL_RTX ? get_insns () : NEXT_INSN (last);
3078 insn != NULL_RTX;
3079 insn = NEXT_INSN (insn))
3080 if (recog_memoized (insn) < 0)
3081 break;
3082 if (insn != NULL_RTX)
3084 /* Do nothing if we cannot generate right insns.
3085 This is analogous to reload pass behavior. */
3086 delete_insns_since (last);
3087 end_sequence ();
3088 return false;
3090 *ad.inner = new_reg;
3093 else
3095 /* index * scale + disp => new base + index * scale,
3096 case (1) above. */
3097 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3098 GET_CODE (*ad.index));
3100 lra_assert (INDEX_REG_CLASS != NO_REGS);
3101 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3102 lra_emit_move (new_reg, *ad.disp);
3103 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3104 new_reg, *ad.index);
3107 else if (ad.index == NULL)
3109 int regno;
3110 enum reg_class cl;
3111 rtx set;
3112 rtx_insn *insns, *last_insn;
3113 /* Try to reload base into register only if the base is invalid
3114 for the address but with valid offset, case (4) above. */
3115 start_sequence ();
3116 new_reg = base_to_reg (&ad);
3118 /* base + disp => new base, cases (1) and (3) above. */
3119 /* Another option would be to reload the displacement into an
3120 index register. However, postreload has code to optimize
3121 address reloads that have the same base and different
3122 displacements, so reloading into an index register would
3123 not necessarily be a win. */
3124 if (new_reg == NULL_RTX)
3125 new_reg = base_plus_disp_to_reg (&ad);
3126 insns = get_insns ();
3127 last_insn = get_last_insn ();
3128 /* If we generated at least two insns, try last insn source as
3129 an address. If we succeed, we generate one less insn. */
3130 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3131 && GET_CODE (SET_SRC (set)) == PLUS
3132 && REG_P (XEXP (SET_SRC (set), 0))
3133 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3135 *ad.inner = SET_SRC (set);
3136 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3138 *ad.base_term = XEXP (SET_SRC (set), 0);
3139 *ad.disp_term = XEXP (SET_SRC (set), 1);
3140 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3141 get_index_code (&ad));
3142 regno = REGNO (*ad.base_term);
3143 if (regno >= FIRST_PSEUDO_REGISTER
3144 && cl != lra_get_allocno_class (regno))
3145 lra_change_class (regno, cl, " Change to", true);
3146 new_reg = SET_SRC (set);
3147 delete_insns_since (PREV_INSN (last_insn));
3150 /* Try if target can split displacement into legitimite new disp
3151 and offset. If it's the case, we replace the last insn with
3152 insns for base + offset => new_reg and set new_reg + new disp
3153 to *ad.inner. */
3154 last_insn = get_last_insn ();
3155 if ((set = single_set (last_insn)) != NULL_RTX
3156 && GET_CODE (SET_SRC (set)) == PLUS
3157 && REG_P (XEXP (SET_SRC (set), 0))
3158 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3159 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3161 rtx addend, disp = XEXP (SET_SRC (set), 1);
3162 if (targetm.legitimize_address_displacement (&disp, &addend,
3163 ad.mode))
3165 rtx_insn *new_insns;
3166 start_sequence ();
3167 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3168 new_insns = get_insns ();
3169 end_sequence ();
3170 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3171 delete_insns_since (PREV_INSN (last_insn));
3172 add_insn (new_insns);
3173 insns = get_insns ();
3176 end_sequence ();
3177 emit_insn (insns);
3178 *ad.inner = new_reg;
3180 else if (ad.disp_term != NULL)
3182 /* base + scale * index + disp => new base + scale * index,
3183 case (1) above. */
3184 new_reg = base_plus_disp_to_reg (&ad);
3185 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3186 new_reg, *ad.index);
3188 else if ((scale = get_index_scale (&ad)) == 1)
3190 /* The last transformation to one reg will be made in
3191 curr_insn_transform function. */
3192 end_sequence ();
3193 return false;
3195 else if (scale != 0)
3197 /* base + scale * index => base + new_reg,
3198 case (1) above.
3199 Index part of address may become invalid. For example, we
3200 changed pseudo on the equivalent memory and a subreg of the
3201 pseudo onto the memory of different mode for which the scale is
3202 prohibitted. */
3203 new_reg = index_part_to_reg (&ad);
3204 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3205 *ad.base_term, new_reg);
3207 else
3209 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3210 SCRATCH, SCRATCH);
3211 rtx addr = *ad.inner;
3213 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3214 /* addr => new_base. */
3215 lra_emit_move (new_reg, addr);
3216 *ad.inner = new_reg;
3218 *before = get_insns ();
3219 end_sequence ();
3220 return true;
3223 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3224 Use process_address_1 as a helper function. Return true for any
3225 RTL changes.
3227 If CHECK_ONLY_P is true, just check address correctness. Return
3228 false if the address correct. */
3229 static bool
3230 process_address (int nop, bool check_only_p,
3231 rtx_insn **before, rtx_insn **after)
3233 bool res = false;
3235 while (process_address_1 (nop, check_only_p, before, after))
3237 if (check_only_p)
3238 return true;
3239 res = true;
3241 return res;
3244 /* Emit insns to reload VALUE into a new register. VALUE is an
3245 auto-increment or auto-decrement RTX whose operand is a register or
3246 memory location; so reloading involves incrementing that location.
3247 IN is either identical to VALUE, or some cheaper place to reload
3248 value being incremented/decremented from.
3250 INC_AMOUNT is the number to increment or decrement by (always
3251 positive and ignored for POST_MODIFY/PRE_MODIFY).
3253 Return pseudo containing the result. */
3254 static rtx
3255 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3257 /* REG or MEM to be copied and incremented. */
3258 rtx incloc = XEXP (value, 0);
3259 /* Nonzero if increment after copying. */
3260 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3261 || GET_CODE (value) == POST_MODIFY);
3262 rtx_insn *last;
3263 rtx inc;
3264 rtx_insn *add_insn;
3265 int code;
3266 rtx real_in = in == value ? incloc : in;
3267 rtx result;
3268 bool plus_p = true;
3270 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3272 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3273 || GET_CODE (XEXP (value, 1)) == MINUS);
3274 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3275 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3276 inc = XEXP (XEXP (value, 1), 1);
3278 else
3280 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3281 inc_amount = -inc_amount;
3283 inc = GEN_INT (inc_amount);
3286 if (! post && REG_P (incloc))
3287 result = incloc;
3288 else
3289 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3290 "INC/DEC result");
3292 if (real_in != result)
3294 /* First copy the location to the result register. */
3295 lra_assert (REG_P (result));
3296 emit_insn (gen_move_insn (result, real_in));
3299 /* We suppose that there are insns to add/sub with the constant
3300 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3301 old reload worked with this assumption. If the assumption
3302 becomes wrong, we should use approach in function
3303 base_plus_disp_to_reg. */
3304 if (in == value)
3306 /* See if we can directly increment INCLOC. */
3307 last = get_last_insn ();
3308 add_insn = emit_insn (plus_p
3309 ? gen_add2_insn (incloc, inc)
3310 : gen_sub2_insn (incloc, inc));
3312 code = recog_memoized (add_insn);
3313 if (code >= 0)
3315 if (! post && result != incloc)
3316 emit_insn (gen_move_insn (result, incloc));
3317 return result;
3319 delete_insns_since (last);
3322 /* If couldn't do the increment directly, must increment in RESULT.
3323 The way we do this depends on whether this is pre- or
3324 post-increment. For pre-increment, copy INCLOC to the reload
3325 register, increment it there, then save back. */
3326 if (! post)
3328 if (real_in != result)
3329 emit_insn (gen_move_insn (result, real_in));
3330 if (plus_p)
3331 emit_insn (gen_add2_insn (result, inc));
3332 else
3333 emit_insn (gen_sub2_insn (result, inc));
3334 if (result != incloc)
3335 emit_insn (gen_move_insn (incloc, result));
3337 else
3339 /* Post-increment.
3341 Because this might be a jump insn or a compare, and because
3342 RESULT may not be available after the insn in an input
3343 reload, we must do the incrementing before the insn being
3344 reloaded for.
3346 We have already copied IN to RESULT. Increment the copy in
3347 RESULT, save that back, then decrement RESULT so it has
3348 the original value. */
3349 if (plus_p)
3350 emit_insn (gen_add2_insn (result, inc));
3351 else
3352 emit_insn (gen_sub2_insn (result, inc));
3353 emit_insn (gen_move_insn (incloc, result));
3354 /* Restore non-modified value for the result. We prefer this
3355 way because it does not require an additional hard
3356 register. */
3357 if (plus_p)
3359 if (CONST_INT_P (inc))
3360 emit_insn (gen_add2_insn (result,
3361 gen_int_mode (-INTVAL (inc),
3362 GET_MODE (result))));
3363 else
3364 emit_insn (gen_sub2_insn (result, inc));
3366 else
3367 emit_insn (gen_add2_insn (result, inc));
3369 return result;
3372 /* Return true if the current move insn does not need processing as we
3373 already know that it satisfies its constraints. */
3374 static bool
3375 simple_move_p (void)
3377 rtx dest, src;
3378 enum reg_class dclass, sclass;
3380 lra_assert (curr_insn_set != NULL_RTX);
3381 dest = SET_DEST (curr_insn_set);
3382 src = SET_SRC (curr_insn_set);
3383 return ((dclass = get_op_class (dest)) != NO_REGS
3384 && (sclass = get_op_class (src)) != NO_REGS
3385 /* The backend guarantees that register moves of cost 2
3386 never need reloads. */
3387 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3390 /* Swap operands NOP and NOP + 1. */
3391 static inline void
3392 swap_operands (int nop)
3394 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3395 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3396 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3397 /* Swap the duplicates too. */
3398 lra_update_dup (curr_id, nop);
3399 lra_update_dup (curr_id, nop + 1);
3402 /* Main entry point of the constraint code: search the body of the
3403 current insn to choose the best alternative. It is mimicking insn
3404 alternative cost calculation model of former reload pass. That is
3405 because machine descriptions were written to use this model. This
3406 model can be changed in future. Make commutative operand exchange
3407 if it is chosen.
3409 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3410 constraints. Return true if any change happened during function
3411 call.
3413 If CHECK_ONLY_P is true then don't do any transformation. Just
3414 check that the insn satisfies all constraints. If the insn does
3415 not satisfy any constraint, return true. */
3416 static bool
3417 curr_insn_transform (bool check_only_p)
3419 int i, j, k;
3420 int n_operands;
3421 int n_alternatives;
3422 int n_outputs;
3423 int commutative;
3424 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3425 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3426 signed char outputs[MAX_RECOG_OPERANDS + 1];
3427 rtx_insn *before, *after;
3428 bool alt_p = false;
3429 /* Flag that the insn has been changed through a transformation. */
3430 bool change_p;
3431 bool sec_mem_p;
3432 #ifdef SECONDARY_MEMORY_NEEDED
3433 bool use_sec_mem_p;
3434 #endif
3435 int max_regno_before;
3436 int reused_alternative_num;
3438 curr_insn_set = single_set (curr_insn);
3439 if (curr_insn_set != NULL_RTX && simple_move_p ())
3440 return false;
3442 no_input_reloads_p = no_output_reloads_p = false;
3443 goal_alt_number = -1;
3444 change_p = sec_mem_p = false;
3445 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3446 reloads; neither are insns that SET cc0. Insns that use CC0 are
3447 not allowed to have any input reloads. */
3448 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3449 no_output_reloads_p = true;
3451 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3452 no_input_reloads_p = true;
3453 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3454 no_output_reloads_p = true;
3456 n_operands = curr_static_id->n_operands;
3457 n_alternatives = curr_static_id->n_alternatives;
3459 /* Just return "no reloads" if insn has no operands with
3460 constraints. */
3461 if (n_operands == 0 || n_alternatives == 0)
3462 return false;
3464 max_regno_before = max_reg_num ();
3466 for (i = 0; i < n_operands; i++)
3468 goal_alt_matched[i][0] = -1;
3469 goal_alt_matches[i] = -1;
3472 commutative = curr_static_id->commutative;
3474 /* Now see what we need for pseudos that didn't get hard regs or got
3475 the wrong kind of hard reg. For this, we must consider all the
3476 operands together against the register constraints. */
3478 best_losers = best_overall = INT_MAX;
3479 best_reload_sum = 0;
3481 curr_swapped = false;
3482 goal_alt_swapped = false;
3484 if (! check_only_p)
3485 /* Make equivalence substitution and memory subreg elimination
3486 before address processing because an address legitimacy can
3487 depend on memory mode. */
3488 for (i = 0; i < n_operands; i++)
3490 rtx op, subst, old;
3491 bool op_change_p = false;
3493 if (curr_static_id->operand[i].is_operator)
3494 continue;
3496 old = op = *curr_id->operand_loc[i];
3497 if (GET_CODE (old) == SUBREG)
3498 old = SUBREG_REG (old);
3499 subst = get_equiv_with_elimination (old, curr_insn);
3500 original_subreg_reg_mode[i] = VOIDmode;
3501 if (subst != old)
3503 subst = copy_rtx (subst);
3504 lra_assert (REG_P (old));
3505 if (GET_CODE (op) != SUBREG)
3506 *curr_id->operand_loc[i] = subst;
3507 else
3509 SUBREG_REG (op) = subst;
3510 if (GET_MODE (subst) == VOIDmode)
3511 original_subreg_reg_mode[i] = GET_MODE (old);
3513 if (lra_dump_file != NULL)
3515 fprintf (lra_dump_file,
3516 "Changing pseudo %d in operand %i of insn %u on equiv ",
3517 REGNO (old), i, INSN_UID (curr_insn));
3518 dump_value_slim (lra_dump_file, subst, 1);
3519 fprintf (lra_dump_file, "\n");
3521 op_change_p = change_p = true;
3523 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3525 change_p = true;
3526 lra_update_dup (curr_id, i);
3530 /* Reload address registers and displacements. We do it before
3531 finding an alternative because of memory constraints. */
3532 before = after = NULL;
3533 for (i = 0; i < n_operands; i++)
3534 if (! curr_static_id->operand[i].is_operator
3535 && process_address (i, check_only_p, &before, &after))
3537 if (check_only_p)
3538 return true;
3539 change_p = true;
3540 lra_update_dup (curr_id, i);
3543 if (change_p)
3544 /* If we've changed the instruction then any alternative that
3545 we chose previously may no longer be valid. */
3546 lra_set_used_insn_alternative (curr_insn, -1);
3548 if (! check_only_p && curr_insn_set != NULL_RTX
3549 && check_and_process_move (&change_p, &sec_mem_p))
3550 return change_p;
3552 try_swapped:
3554 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3555 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3556 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3557 reused_alternative_num, INSN_UID (curr_insn));
3559 if (process_alt_operands (reused_alternative_num))
3560 alt_p = true;
3562 if (check_only_p)
3563 return ! alt_p || best_losers != 0;
3565 /* If insn is commutative (it's safe to exchange a certain pair of
3566 operands) then we need to try each alternative twice, the second
3567 time matching those two operands as if we had exchanged them. To
3568 do this, really exchange them in operands.
3570 If we have just tried the alternatives the second time, return
3571 operands to normal and drop through. */
3573 if (reused_alternative_num < 0 && commutative >= 0)
3575 curr_swapped = !curr_swapped;
3576 if (curr_swapped)
3578 swap_operands (commutative);
3579 goto try_swapped;
3581 else
3582 swap_operands (commutative);
3585 if (! alt_p && ! sec_mem_p)
3587 /* No alternative works with reloads?? */
3588 if (INSN_CODE (curr_insn) >= 0)
3589 fatal_insn ("unable to generate reloads for:", curr_insn);
3590 error_for_asm (curr_insn,
3591 "inconsistent operand constraints in an %<asm%>");
3592 /* Avoid further trouble with this insn. */
3593 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3594 lra_invalidate_insn_data (curr_insn);
3595 return true;
3598 /* If the best alternative is with operands 1 and 2 swapped, swap
3599 them. Update the operand numbers of any reloads already
3600 pushed. */
3602 if (goal_alt_swapped)
3604 if (lra_dump_file != NULL)
3605 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3606 INSN_UID (curr_insn));
3608 /* Swap the duplicates too. */
3609 swap_operands (commutative);
3610 change_p = true;
3613 #ifdef SECONDARY_MEMORY_NEEDED
3614 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3615 too conservatively. So we use the secondary memory only if there
3616 is no any alternative without reloads. */
3617 use_sec_mem_p = false;
3618 if (! alt_p)
3619 use_sec_mem_p = true;
3620 else if (sec_mem_p)
3622 for (i = 0; i < n_operands; i++)
3623 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3624 break;
3625 use_sec_mem_p = i < n_operands;
3628 if (use_sec_mem_p)
3630 int in = -1, out = -1;
3631 rtx new_reg, src, dest, rld;
3632 machine_mode sec_mode, rld_mode;
3634 lra_assert (curr_insn_set != NULL_RTX && sec_mem_p);
3635 dest = SET_DEST (curr_insn_set);
3636 src = SET_SRC (curr_insn_set);
3637 for (i = 0; i < n_operands; i++)
3638 if (*curr_id->operand_loc[i] == dest)
3639 out = i;
3640 else if (*curr_id->operand_loc[i] == src)
3641 in = i;
3642 for (i = 0; i < curr_static_id->n_dups; i++)
3643 if (out < 0 && *curr_id->dup_loc[i] == dest)
3644 out = curr_static_id->dup_num[i];
3645 else if (in < 0 && *curr_id->dup_loc[i] == src)
3646 in = curr_static_id->dup_num[i];
3647 lra_assert (out >= 0 && in >= 0
3648 && curr_static_id->operand[out].type == OP_OUT
3649 && curr_static_id->operand[in].type == OP_IN);
3650 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3651 ? dest : src);
3652 rld_mode = GET_MODE (rld);
3653 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3654 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3655 #else
3656 sec_mode = rld_mode;
3657 #endif
3658 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3659 NO_REGS, "secondary");
3660 /* If the mode is changed, it should be wider. */
3661 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3662 if (sec_mode != rld_mode)
3664 /* If the target says specifically to use another mode for
3665 secondary memory moves we can not reuse the original
3666 insn. */
3667 after = emit_spill_move (false, new_reg, dest);
3668 lra_process_new_insns (curr_insn, NULL, after,
3669 "Inserting the sec. move");
3670 /* We may have non null BEFORE here (e.g. after address
3671 processing. */
3672 push_to_sequence (before);
3673 before = emit_spill_move (true, new_reg, src);
3674 emit_insn (before);
3675 before = get_insns ();
3676 end_sequence ();
3677 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3678 lra_set_insn_deleted (curr_insn);
3680 else if (dest == rld)
3682 *curr_id->operand_loc[out] = new_reg;
3683 lra_update_dup (curr_id, out);
3684 after = emit_spill_move (false, new_reg, dest);
3685 lra_process_new_insns (curr_insn, NULL, after,
3686 "Inserting the sec. move");
3688 else
3690 *curr_id->operand_loc[in] = new_reg;
3691 lra_update_dup (curr_id, in);
3692 /* See comments above. */
3693 push_to_sequence (before);
3694 before = emit_spill_move (true, new_reg, src);
3695 emit_insn (before);
3696 before = get_insns ();
3697 end_sequence ();
3698 lra_process_new_insns (curr_insn, before, NULL,
3699 "Inserting the sec. move");
3701 lra_update_insn_regno_info (curr_insn);
3702 return true;
3704 #endif
3706 lra_assert (goal_alt_number >= 0);
3707 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3709 if (lra_dump_file != NULL)
3711 const char *p;
3713 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3714 goal_alt_number, INSN_UID (curr_insn));
3715 for (i = 0; i < n_operands; i++)
3717 p = (curr_static_id->operand_alternative
3718 [goal_alt_number * n_operands + i].constraint);
3719 if (*p == '\0')
3720 continue;
3721 fprintf (lra_dump_file, " (%d) ", i);
3722 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3723 fputc (*p, lra_dump_file);
3725 if (INSN_CODE (curr_insn) >= 0
3726 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3727 fprintf (lra_dump_file, " {%s}", p);
3728 if (curr_id->sp_offset != 0)
3729 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3730 curr_id->sp_offset);
3731 fprintf (lra_dump_file, "\n");
3734 /* Right now, for any pair of operands I and J that are required to
3735 match, with J < I, goal_alt_matches[I] is J. Add I to
3736 goal_alt_matched[J]. */
3738 for (i = 0; i < n_operands; i++)
3739 if ((j = goal_alt_matches[i]) >= 0)
3741 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3743 /* We allow matching one output operand and several input
3744 operands. */
3745 lra_assert (k == 0
3746 || (curr_static_id->operand[j].type == OP_OUT
3747 && curr_static_id->operand[i].type == OP_IN
3748 && (curr_static_id->operand
3749 [goal_alt_matched[j][0]].type == OP_IN)));
3750 goal_alt_matched[j][k] = i;
3751 goal_alt_matched[j][k + 1] = -1;
3754 for (i = 0; i < n_operands; i++)
3755 goal_alt_win[i] |= goal_alt_match_win[i];
3757 /* Any constants that aren't allowed and can't be reloaded into
3758 registers are here changed into memory references. */
3759 for (i = 0; i < n_operands; i++)
3760 if (goal_alt_win[i])
3762 int regno;
3763 enum reg_class new_class;
3764 rtx reg = *curr_id->operand_loc[i];
3766 if (GET_CODE (reg) == SUBREG)
3767 reg = SUBREG_REG (reg);
3769 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3771 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3773 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3775 lra_assert (ok_p);
3776 lra_change_class (regno, new_class, " Change to", true);
3780 else
3782 const char *constraint;
3783 char c;
3784 rtx op = *curr_id->operand_loc[i];
3785 rtx subreg = NULL_RTX;
3786 machine_mode mode = curr_operand_mode[i];
3788 if (GET_CODE (op) == SUBREG)
3790 subreg = op;
3791 op = SUBREG_REG (op);
3792 mode = GET_MODE (op);
3795 if (CONST_POOL_OK_P (mode, op)
3796 && ((targetm.preferred_reload_class
3797 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3798 || no_input_reloads_p))
3800 rtx tem = force_const_mem (mode, op);
3802 change_p = true;
3803 if (subreg != NULL_RTX)
3804 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3806 *curr_id->operand_loc[i] = tem;
3807 lra_update_dup (curr_id, i);
3808 process_address (i, false, &before, &after);
3810 /* If the alternative accepts constant pool refs directly
3811 there will be no reload needed at all. */
3812 if (subreg != NULL_RTX)
3813 continue;
3814 /* Skip alternatives before the one requested. */
3815 constraint = (curr_static_id->operand_alternative
3816 [goal_alt_number * n_operands + i].constraint);
3817 for (;
3818 (c = *constraint) && c != ',' && c != '#';
3819 constraint += CONSTRAINT_LEN (c, constraint))
3821 enum constraint_num cn = lookup_constraint (constraint);
3822 if ((insn_extra_memory_constraint (cn)
3823 || insn_extra_special_memory_constraint (cn))
3824 && satisfies_memory_constraint_p (tem, cn))
3825 break;
3827 if (c == '\0' || c == ',' || c == '#')
3828 continue;
3830 goal_alt_win[i] = true;
3834 n_outputs = 0;
3835 outputs[0] = -1;
3836 for (i = 0; i < n_operands; i++)
3838 int regno;
3839 bool optional_p = false;
3840 rtx old, new_reg;
3841 rtx op = *curr_id->operand_loc[i];
3843 if (goal_alt_win[i])
3845 if (goal_alt[i] == NO_REGS
3846 && REG_P (op)
3847 /* When we assign NO_REGS it means that we will not
3848 assign a hard register to the scratch pseudo by
3849 assigment pass and the scratch pseudo will be
3850 spilled. Spilled scratch pseudos are transformed
3851 back to scratches at the LRA end. */
3852 && lra_former_scratch_operand_p (curr_insn, i)
3853 && lra_former_scratch_p (REGNO (op)))
3855 int regno = REGNO (op);
3856 lra_change_class (regno, NO_REGS, " Change to", true);
3857 if (lra_get_regno_hard_regno (regno) >= 0)
3858 /* We don't have to mark all insn affected by the
3859 spilled pseudo as there is only one such insn, the
3860 current one. */
3861 reg_renumber[regno] = -1;
3862 lra_assert (bitmap_single_bit_set_p
3863 (&lra_reg_info[REGNO (op)].insn_bitmap));
3865 /* We can do an optional reload. If the pseudo got a hard
3866 reg, we might improve the code through inheritance. If
3867 it does not get a hard register we coalesce memory/memory
3868 moves later. Ignore move insns to avoid cycling. */
3869 if (! lra_simple_p
3870 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3871 && goal_alt[i] != NO_REGS && REG_P (op)
3872 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3873 && regno < new_regno_start
3874 && ! lra_former_scratch_p (regno)
3875 && reg_renumber[regno] < 0
3876 /* Check that the optional reload pseudo will be able to
3877 hold given mode value. */
3878 && ! (prohibited_class_reg_set_mode_p
3879 (goal_alt[i], reg_class_contents[goal_alt[i]],
3880 PSEUDO_REGNO_MODE (regno)))
3881 && (curr_insn_set == NULL_RTX
3882 || !((REG_P (SET_SRC (curr_insn_set))
3883 || MEM_P (SET_SRC (curr_insn_set))
3884 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3885 && (REG_P (SET_DEST (curr_insn_set))
3886 || MEM_P (SET_DEST (curr_insn_set))
3887 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3888 optional_p = true;
3889 else
3890 continue;
3893 /* Operands that match previous ones have already been handled. */
3894 if (goal_alt_matches[i] >= 0)
3895 continue;
3897 /* We should not have an operand with a non-offsettable address
3898 appearing where an offsettable address will do. It also may
3899 be a case when the address should be special in other words
3900 not a general one (e.g. it needs no index reg). */
3901 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3903 enum reg_class rclass;
3904 rtx *loc = &XEXP (op, 0);
3905 enum rtx_code code = GET_CODE (*loc);
3907 push_to_sequence (before);
3908 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3909 MEM, SCRATCH);
3910 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3911 new_reg = emit_inc (rclass, *loc, *loc,
3912 /* This value does not matter for MODIFY. */
3913 GET_MODE_SIZE (GET_MODE (op)));
3914 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3915 "offsetable address", &new_reg))
3916 lra_emit_move (new_reg, *loc);
3917 before = get_insns ();
3918 end_sequence ();
3919 *loc = new_reg;
3920 lra_update_dup (curr_id, i);
3922 else if (goal_alt_matched[i][0] == -1)
3924 machine_mode mode;
3925 rtx reg, *loc;
3926 int hard_regno, byte;
3927 enum op_type type = curr_static_id->operand[i].type;
3929 loc = curr_id->operand_loc[i];
3930 mode = curr_operand_mode[i];
3931 if (GET_CODE (*loc) == SUBREG)
3933 reg = SUBREG_REG (*loc);
3934 byte = SUBREG_BYTE (*loc);
3935 if (REG_P (reg)
3936 /* Strict_low_part requires reload the register not
3937 the sub-register. */
3938 && (curr_static_id->operand[i].strict_low
3939 || (GET_MODE_SIZE (mode)
3940 <= GET_MODE_SIZE (GET_MODE (reg))
3941 && (hard_regno
3942 = get_try_hard_regno (REGNO (reg))) >= 0
3943 && (simplify_subreg_regno
3944 (hard_regno,
3945 GET_MODE (reg), byte, mode) < 0)
3946 && (goal_alt[i] == NO_REGS
3947 || (simplify_subreg_regno
3948 (ira_class_hard_regs[goal_alt[i]][0],
3949 GET_MODE (reg), byte, mode) >= 0)))))
3951 if (type == OP_OUT)
3952 type = OP_INOUT;
3953 loc = &SUBREG_REG (*loc);
3954 mode = GET_MODE (*loc);
3957 old = *loc;
3958 if (get_reload_reg (type, mode, old, goal_alt[i],
3959 loc != curr_id->operand_loc[i], "", &new_reg)
3960 && type != OP_OUT)
3962 push_to_sequence (before);
3963 lra_emit_move (new_reg, old);
3964 before = get_insns ();
3965 end_sequence ();
3967 *loc = new_reg;
3968 if (type != OP_IN
3969 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3971 start_sequence ();
3972 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3973 emit_insn (after);
3974 after = get_insns ();
3975 end_sequence ();
3976 *loc = new_reg;
3978 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3979 if (goal_alt_dont_inherit_ops[j] == i)
3981 lra_set_regno_unique_value (REGNO (new_reg));
3982 break;
3984 lra_update_dup (curr_id, i);
3986 else if (curr_static_id->operand[i].type == OP_IN
3987 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3988 == OP_OUT))
3990 /* generate reloads for input and matched outputs. */
3991 match_inputs[0] = i;
3992 match_inputs[1] = -1;
3993 match_reload (goal_alt_matched[i][0], match_inputs, outputs,
3994 goal_alt[i], &before, &after,
3995 curr_static_id->operand_alternative
3996 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
3997 .earlyclobber);
3999 else if (curr_static_id->operand[i].type == OP_OUT
4000 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4001 == OP_IN))
4002 /* Generate reloads for output and matched inputs. */
4003 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i], &before,
4004 &after, curr_static_id->operand_alternative
4005 [goal_alt_number * n_operands + i].earlyclobber);
4006 else if (curr_static_id->operand[i].type == OP_IN
4007 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4008 == OP_IN))
4010 /* Generate reloads for matched inputs. */
4011 match_inputs[0] = i;
4012 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
4013 match_inputs[j + 1] = k;
4014 match_inputs[j + 1] = -1;
4015 match_reload (-1, match_inputs, outputs, goal_alt[i], &before,
4016 &after, false);
4018 else
4019 /* We must generate code in any case when function
4020 process_alt_operands decides that it is possible. */
4021 gcc_unreachable ();
4023 /* Memorise processed outputs so that output remaining to be processed
4024 can avoid using the same register value (see match_reload). */
4025 if (curr_static_id->operand[i].type == OP_OUT)
4027 outputs[n_outputs++] = i;
4028 outputs[n_outputs] = -1;
4031 if (optional_p)
4033 lra_assert (REG_P (op));
4034 regno = REGNO (op);
4035 op = *curr_id->operand_loc[i]; /* Substitution. */
4036 if (GET_CODE (op) == SUBREG)
4037 op = SUBREG_REG (op);
4038 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
4039 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
4040 lra_reg_info[REGNO (op)].restore_regno = regno;
4041 if (lra_dump_file != NULL)
4042 fprintf (lra_dump_file,
4043 " Making reload reg %d for reg %d optional\n",
4044 REGNO (op), regno);
4047 if (before != NULL_RTX || after != NULL_RTX
4048 || max_regno_before != max_reg_num ())
4049 change_p = true;
4050 if (change_p)
4052 lra_update_operator_dups (curr_id);
4053 /* Something changes -- process the insn. */
4054 lra_update_insn_regno_info (curr_insn);
4056 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
4057 return change_p;
4060 /* Return true if INSN satisfies all constraints. In other words, no
4061 reload insns are needed. */
4062 bool
4063 lra_constrain_insn (rtx_insn *insn)
4065 int saved_new_regno_start = new_regno_start;
4066 int saved_new_insn_uid_start = new_insn_uid_start;
4067 bool change_p;
4069 curr_insn = insn;
4070 curr_id = lra_get_insn_recog_data (curr_insn);
4071 curr_static_id = curr_id->insn_static_data;
4072 new_insn_uid_start = get_max_uid ();
4073 new_regno_start = max_reg_num ();
4074 change_p = curr_insn_transform (true);
4075 new_regno_start = saved_new_regno_start;
4076 new_insn_uid_start = saved_new_insn_uid_start;
4077 return ! change_p;
4080 /* Return true if X is in LIST. */
4081 static bool
4082 in_list_p (rtx x, rtx list)
4084 for (; list != NULL_RTX; list = XEXP (list, 1))
4085 if (XEXP (list, 0) == x)
4086 return true;
4087 return false;
4090 /* Return true if X contains an allocatable hard register (if
4091 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
4092 static bool
4093 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
4095 int i, j;
4096 const char *fmt;
4097 enum rtx_code code;
4099 code = GET_CODE (x);
4100 if (REG_P (x))
4102 int regno = REGNO (x);
4103 HARD_REG_SET alloc_regs;
4105 if (hard_reg_p)
4107 if (regno >= FIRST_PSEUDO_REGISTER)
4108 regno = lra_get_regno_hard_regno (regno);
4109 if (regno < 0)
4110 return false;
4111 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
4112 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
4114 else
4116 if (regno < FIRST_PSEUDO_REGISTER)
4117 return false;
4118 if (! spilled_p)
4119 return true;
4120 return lra_get_regno_hard_regno (regno) < 0;
4123 fmt = GET_RTX_FORMAT (code);
4124 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4126 if (fmt[i] == 'e')
4128 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4129 return true;
4131 else if (fmt[i] == 'E')
4133 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4134 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4135 return true;
4138 return false;
4141 /* Process all regs in location *LOC and change them on equivalent
4142 substitution. Return true if any change was done. */
4143 static bool
4144 loc_equivalence_change_p (rtx *loc)
4146 rtx subst, reg, x = *loc;
4147 bool result = false;
4148 enum rtx_code code = GET_CODE (x);
4149 const char *fmt;
4150 int i, j;
4152 if (code == SUBREG)
4154 reg = SUBREG_REG (x);
4155 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4156 && GET_MODE (subst) == VOIDmode)
4158 /* We cannot reload debug location. Simplify subreg here
4159 while we know the inner mode. */
4160 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4161 GET_MODE (reg), SUBREG_BYTE (x));
4162 return true;
4165 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4167 *loc = subst;
4168 return true;
4171 /* Scan all the operand sub-expressions. */
4172 fmt = GET_RTX_FORMAT (code);
4173 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4175 if (fmt[i] == 'e')
4176 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4177 else if (fmt[i] == 'E')
4178 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4179 result
4180 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4182 return result;
4185 /* Similar to loc_equivalence_change_p, but for use as
4186 simplify_replace_fn_rtx callback. DATA is insn for which the
4187 elimination is done. If it null we don't do the elimination. */
4188 static rtx
4189 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4191 if (!REG_P (loc))
4192 return NULL_RTX;
4194 rtx subst = (data == NULL
4195 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4196 if (subst != loc)
4197 return subst;
4199 return NULL_RTX;
4202 /* Maximum number of generated reload insns per an insn. It is for
4203 preventing this pass cycling in a bug case. */
4204 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4206 /* The current iteration number of this LRA pass. */
4207 int lra_constraint_iter;
4209 /* True if we substituted equiv which needs checking register
4210 allocation correctness because the equivalent value contains
4211 allocatable hard registers or when we restore multi-register
4212 pseudo. */
4213 bool lra_risky_transformations_p;
4215 /* Return true if REGNO is referenced in more than one block. */
4216 static bool
4217 multi_block_pseudo_p (int regno)
4219 basic_block bb = NULL;
4220 unsigned int uid;
4221 bitmap_iterator bi;
4223 if (regno < FIRST_PSEUDO_REGISTER)
4224 return false;
4226 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4227 if (bb == NULL)
4228 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4229 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4230 return true;
4231 return false;
4234 /* Return true if LIST contains a deleted insn. */
4235 static bool
4236 contains_deleted_insn_p (rtx_insn_list *list)
4238 for (; list != NULL_RTX; list = list->next ())
4239 if (NOTE_P (list->insn ())
4240 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4241 return true;
4242 return false;
4245 /* Return true if X contains a pseudo dying in INSN. */
4246 static bool
4247 dead_pseudo_p (rtx x, rtx_insn *insn)
4249 int i, j;
4250 const char *fmt;
4251 enum rtx_code code;
4253 if (REG_P (x))
4254 return (insn != NULL_RTX
4255 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4256 code = GET_CODE (x);
4257 fmt = GET_RTX_FORMAT (code);
4258 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4260 if (fmt[i] == 'e')
4262 if (dead_pseudo_p (XEXP (x, i), insn))
4263 return true;
4265 else if (fmt[i] == 'E')
4267 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4268 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4269 return true;
4272 return false;
4275 /* Return true if INSN contains a dying pseudo in INSN right hand
4276 side. */
4277 static bool
4278 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4280 rtx set = single_set (insn);
4282 gcc_assert (set != NULL);
4283 return dead_pseudo_p (SET_SRC (set), insn);
4286 /* Return true if any init insn of REGNO contains a dying pseudo in
4287 insn right hand side. */
4288 static bool
4289 init_insn_rhs_dead_pseudo_p (int regno)
4291 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4293 if (insns == NULL)
4294 return false;
4295 for (; insns != NULL_RTX; insns = insns->next ())
4296 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4297 return true;
4298 return false;
4301 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4302 reverse only if we have one init insn with given REGNO as a
4303 source. */
4304 static bool
4305 reverse_equiv_p (int regno)
4307 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4308 rtx set;
4310 if (insns == NULL)
4311 return false;
4312 if (! INSN_P (insns->insn ())
4313 || insns->next () != NULL)
4314 return false;
4315 if ((set = single_set (insns->insn ())) == NULL_RTX)
4316 return false;
4317 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4320 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4321 call this function only for non-reverse equivalence. */
4322 static bool
4323 contains_reloaded_insn_p (int regno)
4325 rtx set;
4326 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4328 for (; list != NULL; list = list->next ())
4329 if ((set = single_set (list->insn ())) == NULL_RTX
4330 || ! REG_P (SET_DEST (set))
4331 || (int) REGNO (SET_DEST (set)) != regno)
4332 return true;
4333 return false;
4336 /* Entry function of LRA constraint pass. Return true if the
4337 constraint pass did change the code. */
4338 bool
4339 lra_constraints (bool first_p)
4341 bool changed_p;
4342 int i, hard_regno, new_insns_num;
4343 unsigned int min_len, new_min_len, uid;
4344 rtx set, x, reg, dest_reg;
4345 basic_block last_bb;
4346 bitmap_head equiv_insn_bitmap;
4347 bitmap_iterator bi;
4349 lra_constraint_iter++;
4350 if (lra_dump_file != NULL)
4351 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4352 lra_constraint_iter);
4353 changed_p = false;
4354 if (pic_offset_table_rtx
4355 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4356 lra_risky_transformations_p = true;
4357 else
4358 lra_risky_transformations_p = false;
4359 new_insn_uid_start = get_max_uid ();
4360 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4361 /* Mark used hard regs for target stack size calulations. */
4362 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4363 if (lra_reg_info[i].nrefs != 0
4364 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4366 int j, nregs;
4368 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4369 for (j = 0; j < nregs; j++)
4370 df_set_regs_ever_live (hard_regno + j, true);
4372 /* Do elimination before the equivalence processing as we can spill
4373 some pseudos during elimination. */
4374 lra_eliminate (false, first_p);
4375 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4376 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4377 if (lra_reg_info[i].nrefs != 0)
4379 ira_reg_equiv[i].profitable_p = true;
4380 reg = regno_reg_rtx[i];
4381 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4383 bool pseudo_p = contains_reg_p (x, false, false);
4385 /* After RTL transformation, we can not guarantee that
4386 pseudo in the substitution was not reloaded which might
4387 make equivalence invalid. For example, in reverse
4388 equiv of p0
4390 p0 <- ...
4392 equiv_mem <- p0
4394 the memory address register was reloaded before the 2nd
4395 insn. */
4396 if ((! first_p && pseudo_p)
4397 /* We don't use DF for compilation speed sake. So it
4398 is problematic to update live info when we use an
4399 equivalence containing pseudos in more than one
4400 BB. */
4401 || (pseudo_p && multi_block_pseudo_p (i))
4402 /* If an init insn was deleted for some reason, cancel
4403 the equiv. We could update the equiv insns after
4404 transformations including an equiv insn deletion
4405 but it is not worthy as such cases are extremely
4406 rare. */
4407 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4408 /* If it is not a reverse equivalence, we check that a
4409 pseudo in rhs of the init insn is not dying in the
4410 insn. Otherwise, the live info at the beginning of
4411 the corresponding BB might be wrong after we
4412 removed the insn. When the equiv can be a
4413 constant, the right hand side of the init insn can
4414 be a pseudo. */
4415 || (! reverse_equiv_p (i)
4416 && (init_insn_rhs_dead_pseudo_p (i)
4417 /* If we reloaded the pseudo in an equivalence
4418 init insn, we can not remove the equiv init
4419 insns and the init insns might write into
4420 const memory in this case. */
4421 || contains_reloaded_insn_p (i)))
4422 /* Prevent access beyond equivalent memory for
4423 paradoxical subregs. */
4424 || (MEM_P (x)
4425 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4426 > GET_MODE_SIZE (GET_MODE (x))))
4427 || (pic_offset_table_rtx
4428 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4429 && (targetm.preferred_reload_class
4430 (x, lra_get_allocno_class (i)) == NO_REGS))
4431 || contains_symbol_ref_p (x))))
4432 ira_reg_equiv[i].defined_p = false;
4433 if (contains_reg_p (x, false, true))
4434 ira_reg_equiv[i].profitable_p = false;
4435 if (get_equiv (reg) != reg)
4436 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4439 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4440 update_equiv (i);
4441 /* We should add all insns containing pseudos which should be
4442 substituted by their equivalences. */
4443 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4444 lra_push_insn_by_uid (uid);
4445 min_len = lra_insn_stack_length ();
4446 new_insns_num = 0;
4447 last_bb = NULL;
4448 changed_p = false;
4449 while ((new_min_len = lra_insn_stack_length ()) != 0)
4451 curr_insn = lra_pop_insn ();
4452 --new_min_len;
4453 curr_bb = BLOCK_FOR_INSN (curr_insn);
4454 if (curr_bb != last_bb)
4456 last_bb = curr_bb;
4457 bb_reload_num = lra_curr_reload_num;
4459 if (min_len > new_min_len)
4461 min_len = new_min_len;
4462 new_insns_num = 0;
4464 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4465 internal_error
4466 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4467 MAX_RELOAD_INSNS_NUMBER);
4468 new_insns_num++;
4469 if (DEBUG_INSN_P (curr_insn))
4471 /* We need to check equivalence in debug insn and change
4472 pseudo to the equivalent value if necessary. */
4473 curr_id = lra_get_insn_recog_data (curr_insn);
4474 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4476 rtx old = *curr_id->operand_loc[0];
4477 *curr_id->operand_loc[0]
4478 = simplify_replace_fn_rtx (old, NULL_RTX,
4479 loc_equivalence_callback, curr_insn);
4480 if (old != *curr_id->operand_loc[0])
4482 lra_update_insn_regno_info (curr_insn);
4483 changed_p = true;
4487 else if (INSN_P (curr_insn))
4489 if ((set = single_set (curr_insn)) != NULL_RTX)
4491 dest_reg = SET_DEST (set);
4492 /* The equivalence pseudo could be set up as SUBREG in a
4493 case when it is a call restore insn in a mode
4494 different from the pseudo mode. */
4495 if (GET_CODE (dest_reg) == SUBREG)
4496 dest_reg = SUBREG_REG (dest_reg);
4497 if ((REG_P (dest_reg)
4498 && (x = get_equiv (dest_reg)) != dest_reg
4499 /* Remove insns which set up a pseudo whose value
4500 can not be changed. Such insns might be not in
4501 init_insns because we don't update equiv data
4502 during insn transformations.
4504 As an example, let suppose that a pseudo got
4505 hard register and on the 1st pass was not
4506 changed to equivalent constant. We generate an
4507 additional insn setting up the pseudo because of
4508 secondary memory movement. Then the pseudo is
4509 spilled and we use the equiv constant. In this
4510 case we should remove the additional insn and
4511 this insn is not init_insns list. */
4512 && (! MEM_P (x) || MEM_READONLY_P (x)
4513 /* Check that this is actually an insn setting
4514 up the equivalence. */
4515 || in_list_p (curr_insn,
4516 ira_reg_equiv
4517 [REGNO (dest_reg)].init_insns)))
4518 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4519 && in_list_p (curr_insn,
4520 ira_reg_equiv
4521 [REGNO (SET_SRC (set))].init_insns)))
4523 /* This is equiv init insn of pseudo which did not get a
4524 hard register -- remove the insn. */
4525 if (lra_dump_file != NULL)
4527 fprintf (lra_dump_file,
4528 " Removing equiv init insn %i (freq=%d)\n",
4529 INSN_UID (curr_insn),
4530 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4531 dump_insn_slim (lra_dump_file, curr_insn);
4533 if (contains_reg_p (x, true, false))
4534 lra_risky_transformations_p = true;
4535 lra_set_insn_deleted (curr_insn);
4536 continue;
4539 curr_id = lra_get_insn_recog_data (curr_insn);
4540 curr_static_id = curr_id->insn_static_data;
4541 init_curr_insn_input_reloads ();
4542 init_curr_operand_mode ();
4543 if (curr_insn_transform (false))
4544 changed_p = true;
4545 /* Check non-transformed insns too for equiv change as USE
4546 or CLOBBER don't need reloads but can contain pseudos
4547 being changed on their equivalences. */
4548 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4549 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4551 lra_update_insn_regno_info (curr_insn);
4552 changed_p = true;
4556 bitmap_clear (&equiv_insn_bitmap);
4557 /* If we used a new hard regno, changed_p should be true because the
4558 hard reg is assigned to a new pseudo. */
4559 if (flag_checking && !changed_p)
4561 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4562 if (lra_reg_info[i].nrefs != 0
4563 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4565 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4567 for (j = 0; j < nregs; j++)
4568 lra_assert (df_regs_ever_live_p (hard_regno + j));
4571 return changed_p;
4574 /* Initiate the LRA constraint pass. It is done once per
4575 function. */
4576 void
4577 lra_constraints_init (void)
4581 /* Finalize the LRA constraint pass. It is done once per
4582 function. */
4583 void
4584 lra_constraints_finish (void)
4590 /* This page contains code to do inheritance/split
4591 transformations. */
4593 /* Number of reloads passed so far in current EBB. */
4594 static int reloads_num;
4596 /* Number of calls passed so far in current EBB. */
4597 static int calls_num;
4599 /* Current reload pseudo check for validity of elements in
4600 USAGE_INSNS. */
4601 static int curr_usage_insns_check;
4603 /* Info about last usage of registers in EBB to do inheritance/split
4604 transformation. Inheritance transformation is done from a spilled
4605 pseudo and split transformations from a hard register or a pseudo
4606 assigned to a hard register. */
4607 struct usage_insns
4609 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4610 value INSNS is valid. The insns is chain of optional debug insns
4611 and a finishing non-debug insn using the corresponding reg. The
4612 value is also used to mark the registers which are set up in the
4613 current insn. The negated insn uid is used for this. */
4614 int check;
4615 /* Value of global reloads_num at the last insn in INSNS. */
4616 int reloads_num;
4617 /* Value of global reloads_nums at the last insn in INSNS. */
4618 int calls_num;
4619 /* It can be true only for splitting. And it means that the restore
4620 insn should be put after insn given by the following member. */
4621 bool after_p;
4622 /* Next insns in the current EBB which use the original reg and the
4623 original reg value is not changed between the current insn and
4624 the next insns. In order words, e.g. for inheritance, if we need
4625 to use the original reg value again in the next insns we can try
4626 to use the value in a hard register from a reload insn of the
4627 current insn. */
4628 rtx insns;
4631 /* Map: regno -> corresponding pseudo usage insns. */
4632 static struct usage_insns *usage_insns;
4634 static void
4635 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4637 usage_insns[regno].check = curr_usage_insns_check;
4638 usage_insns[regno].insns = insn;
4639 usage_insns[regno].reloads_num = reloads_num;
4640 usage_insns[regno].calls_num = calls_num;
4641 usage_insns[regno].after_p = after_p;
4644 /* The function is used to form list REGNO usages which consists of
4645 optional debug insns finished by a non-debug insn using REGNO.
4646 RELOADS_NUM is current number of reload insns processed so far. */
4647 static void
4648 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
4650 rtx next_usage_insns;
4652 if (usage_insns[regno].check == curr_usage_insns_check
4653 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4654 && DEBUG_INSN_P (insn))
4656 /* Check that we did not add the debug insn yet. */
4657 if (next_usage_insns != insn
4658 && (GET_CODE (next_usage_insns) != INSN_LIST
4659 || XEXP (next_usage_insns, 0) != insn))
4660 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4661 next_usage_insns);
4663 else if (NONDEBUG_INSN_P (insn))
4664 setup_next_usage_insn (regno, insn, reloads_num, false);
4665 else
4666 usage_insns[regno].check = 0;
4669 /* Return first non-debug insn in list USAGE_INSNS. */
4670 static rtx_insn *
4671 skip_usage_debug_insns (rtx usage_insns)
4673 rtx insn;
4675 /* Skip debug insns. */
4676 for (insn = usage_insns;
4677 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4678 insn = XEXP (insn, 1))
4680 return safe_as_a <rtx_insn *> (insn);
4683 /* Return true if we need secondary memory moves for insn in
4684 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4685 into the insn. */
4686 static bool
4687 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4688 rtx usage_insns ATTRIBUTE_UNUSED)
4690 #ifndef SECONDARY_MEMORY_NEEDED
4691 return false;
4692 #else
4693 rtx_insn *insn;
4694 rtx set, dest;
4695 enum reg_class cl;
4697 if (inher_cl == ALL_REGS
4698 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4699 return false;
4700 lra_assert (INSN_P (insn));
4701 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4702 return false;
4703 dest = SET_DEST (set);
4704 if (! REG_P (dest))
4705 return false;
4706 lra_assert (inher_cl != NO_REGS);
4707 cl = get_reg_class (REGNO (dest));
4708 return (cl != NO_REGS && cl != ALL_REGS
4709 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4710 #endif
4713 /* Registers involved in inheritance/split in the current EBB
4714 (inheritance/split pseudos and original registers). */
4715 static bitmap_head check_only_regs;
4717 /* Do inheritance transformations for insn INSN, which defines (if
4718 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4719 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4720 form as the "insns" field of usage_insns. Return true if we
4721 succeed in such transformation.
4723 The transformations look like:
4725 p <- ... i <- ...
4726 ... p <- i (new insn)
4727 ... =>
4728 <- ... p ... <- ... i ...
4730 ... i <- p (new insn)
4731 <- ... p ... <- ... i ...
4732 ... =>
4733 <- ... p ... <- ... i ...
4734 where p is a spilled original pseudo and i is a new inheritance pseudo.
4737 The inheritance pseudo has the smallest class of two classes CL and
4738 class of ORIGINAL REGNO. */
4739 static bool
4740 inherit_reload_reg (bool def_p, int original_regno,
4741 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4743 if (optimize_function_for_size_p (cfun))
4744 return false;
4746 enum reg_class rclass = lra_get_allocno_class (original_regno);
4747 rtx original_reg = regno_reg_rtx[original_regno];
4748 rtx new_reg, usage_insn;
4749 rtx_insn *new_insns;
4751 lra_assert (! usage_insns[original_regno].after_p);
4752 if (lra_dump_file != NULL)
4753 fprintf (lra_dump_file,
4754 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4755 if (! ira_reg_classes_intersect_p[cl][rclass])
4757 if (lra_dump_file != NULL)
4759 fprintf (lra_dump_file,
4760 " Rejecting inheritance for %d "
4761 "because of disjoint classes %s and %s\n",
4762 original_regno, reg_class_names[cl],
4763 reg_class_names[rclass]);
4764 fprintf (lra_dump_file,
4765 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4767 return false;
4769 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4770 /* We don't use a subset of two classes because it can be
4771 NO_REGS. This transformation is still profitable in most
4772 cases even if the classes are not intersected as register
4773 move is probably cheaper than a memory load. */
4774 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4776 if (lra_dump_file != NULL)
4777 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4778 reg_class_names[cl], reg_class_names[rclass]);
4780 rclass = cl;
4782 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4784 /* Reject inheritance resulting in secondary memory moves.
4785 Otherwise, there is a danger in LRA cycling. Also such
4786 transformation will be unprofitable. */
4787 if (lra_dump_file != NULL)
4789 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4790 rtx set = single_set (insn);
4792 lra_assert (set != NULL_RTX);
4794 rtx dest = SET_DEST (set);
4796 lra_assert (REG_P (dest));
4797 fprintf (lra_dump_file,
4798 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4799 "as secondary mem is needed\n",
4800 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4801 original_regno, reg_class_names[rclass]);
4802 fprintf (lra_dump_file,
4803 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4805 return false;
4807 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4808 rclass, "inheritance");
4809 start_sequence ();
4810 if (def_p)
4811 lra_emit_move (original_reg, new_reg);
4812 else
4813 lra_emit_move (new_reg, original_reg);
4814 new_insns = get_insns ();
4815 end_sequence ();
4816 if (NEXT_INSN (new_insns) != NULL_RTX)
4818 if (lra_dump_file != NULL)
4820 fprintf (lra_dump_file,
4821 " Rejecting inheritance %d->%d "
4822 "as it results in 2 or more insns:\n",
4823 original_regno, REGNO (new_reg));
4824 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4825 fprintf (lra_dump_file,
4826 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4828 return false;
4830 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
4831 lra_update_insn_regno_info (insn);
4832 if (! def_p)
4833 /* We now have a new usage insn for original regno. */
4834 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4835 if (lra_dump_file != NULL)
4836 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4837 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4838 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4839 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4840 bitmap_set_bit (&check_only_regs, original_regno);
4841 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4842 if (def_p)
4843 lra_process_new_insns (insn, NULL, new_insns,
4844 "Add original<-inheritance");
4845 else
4846 lra_process_new_insns (insn, new_insns, NULL,
4847 "Add inheritance<-original");
4848 while (next_usage_insns != NULL_RTX)
4850 if (GET_CODE (next_usage_insns) != INSN_LIST)
4852 usage_insn = next_usage_insns;
4853 lra_assert (NONDEBUG_INSN_P (usage_insn));
4854 next_usage_insns = NULL;
4856 else
4858 usage_insn = XEXP (next_usage_insns, 0);
4859 lra_assert (DEBUG_INSN_P (usage_insn));
4860 next_usage_insns = XEXP (next_usage_insns, 1);
4862 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
4863 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4864 if (lra_dump_file != NULL)
4866 fprintf (lra_dump_file,
4867 " Inheritance reuse change %d->%d (bb%d):\n",
4868 original_regno, REGNO (new_reg),
4869 BLOCK_FOR_INSN (usage_insn)->index);
4870 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
4873 if (lra_dump_file != NULL)
4874 fprintf (lra_dump_file,
4875 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4876 return true;
4879 /* Return true if we need a caller save/restore for pseudo REGNO which
4880 was assigned to a hard register. */
4881 static inline bool
4882 need_for_call_save_p (int regno)
4884 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4885 return (usage_insns[regno].calls_num < calls_num
4886 && (overlaps_hard_reg_set_p
4887 ((flag_ipa_ra &&
4888 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4889 ? lra_reg_info[regno].actual_call_used_reg_set
4890 : call_used_reg_set,
4891 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4892 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4893 PSEUDO_REGNO_MODE (regno))));
4896 /* Global registers occurring in the current EBB. */
4897 static bitmap_head ebb_global_regs;
4899 /* Return true if we need a split for hard register REGNO or pseudo
4900 REGNO which was assigned to a hard register.
4901 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4902 used for reloads since the EBB end. It is an approximation of the
4903 used hard registers in the split range. The exact value would
4904 require expensive calculations. If we were aggressive with
4905 splitting because of the approximation, the split pseudo will save
4906 the same hard register assignment and will be removed in the undo
4907 pass. We still need the approximation because too aggressive
4908 splitting would result in too inaccurate cost calculation in the
4909 assignment pass because of too many generated moves which will be
4910 probably removed in the undo pass. */
4911 static inline bool
4912 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4914 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4916 lra_assert (hard_regno >= 0);
4917 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4918 /* Don't split eliminable hard registers, otherwise we can
4919 split hard registers like hard frame pointer, which
4920 lives on BB start/end according to DF-infrastructure,
4921 when there is a pseudo assigned to the register and
4922 living in the same BB. */
4923 && (regno >= FIRST_PSEUDO_REGISTER
4924 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4925 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4926 /* Don't split call clobbered hard regs living through
4927 calls, otherwise we might have a check problem in the
4928 assign sub-pass as in the most cases (exception is a
4929 situation when lra_risky_transformations_p value is
4930 true) the assign pass assumes that all pseudos living
4931 through calls are assigned to call saved hard regs. */
4932 && (regno >= FIRST_PSEUDO_REGISTER
4933 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4934 || usage_insns[regno].calls_num == calls_num)
4935 /* We need at least 2 reloads to make pseudo splitting
4936 profitable. We should provide hard regno splitting in
4937 any case to solve 1st insn scheduling problem when
4938 moving hard register definition up might result in
4939 impossibility to find hard register for reload pseudo of
4940 small register class. */
4941 && (usage_insns[regno].reloads_num
4942 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4943 && (regno < FIRST_PSEUDO_REGISTER
4944 /* For short living pseudos, spilling + inheritance can
4945 be considered a substitution for splitting.
4946 Therefore we do not splitting for local pseudos. It
4947 decreases also aggressiveness of splitting. The
4948 minimal number of references is chosen taking into
4949 account that for 2 references splitting has no sense
4950 as we can just spill the pseudo. */
4951 || (regno >= FIRST_PSEUDO_REGISTER
4952 && lra_reg_info[regno].nrefs > 3
4953 && bitmap_bit_p (&ebb_global_regs, regno))))
4954 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4957 /* Return class for the split pseudo created from original pseudo with
4958 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4959 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4960 results in no secondary memory movements. */
4961 static enum reg_class
4962 choose_split_class (enum reg_class allocno_class,
4963 int hard_regno ATTRIBUTE_UNUSED,
4964 machine_mode mode ATTRIBUTE_UNUSED)
4966 #ifndef SECONDARY_MEMORY_NEEDED
4967 return allocno_class;
4968 #else
4969 int i;
4970 enum reg_class cl, best_cl = NO_REGS;
4971 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4972 = REGNO_REG_CLASS (hard_regno);
4974 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4975 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4976 return allocno_class;
4977 for (i = 0;
4978 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4979 i++)
4980 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4981 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4982 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4983 && (best_cl == NO_REGS
4984 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4985 best_cl = cl;
4986 return best_cl;
4987 #endif
4990 /* Do split transformations for insn INSN, which defines or uses
4991 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4992 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4993 "insns" field of usage_insns.
4995 The transformations look like:
4997 p <- ... p <- ...
4998 ... s <- p (new insn -- save)
4999 ... =>
5000 ... p <- s (new insn -- restore)
5001 <- ... p ... <- ... p ...
5003 <- ... p ... <- ... p ...
5004 ... s <- p (new insn -- save)
5005 ... =>
5006 ... p <- s (new insn -- restore)
5007 <- ... p ... <- ... p ...
5009 where p is an original pseudo got a hard register or a hard
5010 register and s is a new split pseudo. The save is put before INSN
5011 if BEFORE_P is true. Return true if we succeed in such
5012 transformation. */
5013 static bool
5014 split_reg (bool before_p, int original_regno, rtx_insn *insn,
5015 rtx next_usage_insns)
5017 enum reg_class rclass;
5018 rtx original_reg;
5019 int hard_regno, nregs;
5020 rtx new_reg, usage_insn;
5021 rtx_insn *restore, *save;
5022 bool after_p;
5023 bool call_save_p;
5024 machine_mode mode;
5026 if (original_regno < FIRST_PSEUDO_REGISTER)
5028 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
5029 hard_regno = original_regno;
5030 call_save_p = false;
5031 nregs = 1;
5032 mode = lra_reg_info[hard_regno].biggest_mode;
5033 machine_mode reg_rtx_mode = GET_MODE (regno_reg_rtx[hard_regno]);
5034 /* A reg can have a biggest_mode of VOIDmode if it was only ever seen
5035 as part of a multi-word register. In that case, or if the biggest
5036 mode was larger than a register, just use the reg_rtx. Otherwise,
5037 limit the size to that of the biggest access in the function. */
5038 if (mode == VOIDmode
5039 || GET_MODE_SIZE (mode) > GET_MODE_SIZE (reg_rtx_mode))
5041 original_reg = regno_reg_rtx[hard_regno];
5042 mode = reg_rtx_mode;
5044 else
5045 original_reg = gen_rtx_REG (mode, hard_regno);
5047 else
5049 mode = PSEUDO_REGNO_MODE (original_regno);
5050 hard_regno = reg_renumber[original_regno];
5051 nregs = hard_regno_nregs[hard_regno][mode];
5052 rclass = lra_get_allocno_class (original_regno);
5053 original_reg = regno_reg_rtx[original_regno];
5054 call_save_p = need_for_call_save_p (original_regno);
5056 lra_assert (hard_regno >= 0);
5057 if (lra_dump_file != NULL)
5058 fprintf (lra_dump_file,
5059 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
5061 if (call_save_p)
5063 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
5064 hard_regno_nregs[hard_regno][mode],
5065 mode);
5066 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
5068 else
5070 rclass = choose_split_class (rclass, hard_regno, mode);
5071 if (rclass == NO_REGS)
5073 if (lra_dump_file != NULL)
5075 fprintf (lra_dump_file,
5076 " Rejecting split of %d(%s): "
5077 "no good reg class for %d(%s)\n",
5078 original_regno,
5079 reg_class_names[lra_get_allocno_class (original_regno)],
5080 hard_regno,
5081 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
5082 fprintf
5083 (lra_dump_file,
5084 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5086 return false;
5088 new_reg = lra_create_new_reg (mode, original_reg, rclass, "split");
5089 reg_renumber[REGNO (new_reg)] = hard_regno;
5091 save = emit_spill_move (true, new_reg, original_reg);
5092 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
5094 if (lra_dump_file != NULL)
5096 fprintf
5097 (lra_dump_file,
5098 " Rejecting split %d->%d resulting in > 2 save insns:\n",
5099 original_regno, REGNO (new_reg));
5100 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
5101 fprintf (lra_dump_file,
5102 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5104 return false;
5106 restore = emit_spill_move (false, new_reg, original_reg);
5107 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
5109 if (lra_dump_file != NULL)
5111 fprintf (lra_dump_file,
5112 " Rejecting split %d->%d "
5113 "resulting in > 2 restore insns:\n",
5114 original_regno, REGNO (new_reg));
5115 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
5116 fprintf (lra_dump_file,
5117 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5119 return false;
5121 after_p = usage_insns[original_regno].after_p;
5122 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
5123 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5124 bitmap_set_bit (&check_only_regs, original_regno);
5125 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
5126 for (;;)
5128 if (GET_CODE (next_usage_insns) != INSN_LIST)
5130 usage_insn = next_usage_insns;
5131 break;
5133 usage_insn = XEXP (next_usage_insns, 0);
5134 lra_assert (DEBUG_INSN_P (usage_insn));
5135 next_usage_insns = XEXP (next_usage_insns, 1);
5136 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5137 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5138 if (lra_dump_file != NULL)
5140 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5141 original_regno, REGNO (new_reg));
5142 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5145 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5146 lra_assert (usage_insn != insn || (after_p && before_p));
5147 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5148 after_p ? NULL : restore,
5149 after_p ? restore : NULL,
5150 call_save_p
5151 ? "Add reg<-save" : "Add reg<-split");
5152 lra_process_new_insns (insn, before_p ? save : NULL,
5153 before_p ? NULL : save,
5154 call_save_p
5155 ? "Add save<-reg" : "Add split<-reg");
5156 if (nregs > 1)
5157 /* If we are trying to split multi-register. We should check
5158 conflicts on the next assignment sub-pass. IRA can allocate on
5159 sub-register levels, LRA do this on pseudos level right now and
5160 this discrepancy may create allocation conflicts after
5161 splitting. */
5162 lra_risky_transformations_p = true;
5163 if (lra_dump_file != NULL)
5164 fprintf (lra_dump_file,
5165 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5166 return true;
5169 /* Recognize that we need a split transformation for insn INSN, which
5170 defines or uses REGNO in its insn biggest MODE (we use it only if
5171 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5172 hard registers which might be used for reloads since the EBB end.
5173 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5174 uid before starting INSN processing. Return true if we succeed in
5175 such transformation. */
5176 static bool
5177 split_if_necessary (int regno, machine_mode mode,
5178 HARD_REG_SET potential_reload_hard_regs,
5179 bool before_p, rtx_insn *insn, int max_uid)
5181 bool res = false;
5182 int i, nregs = 1;
5183 rtx next_usage_insns;
5185 if (regno < FIRST_PSEUDO_REGISTER)
5186 nregs = hard_regno_nregs[regno][mode];
5187 for (i = 0; i < nregs; i++)
5188 if (usage_insns[regno + i].check == curr_usage_insns_check
5189 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5190 /* To avoid processing the register twice or more. */
5191 && ((GET_CODE (next_usage_insns) != INSN_LIST
5192 && INSN_UID (next_usage_insns) < max_uid)
5193 || (GET_CODE (next_usage_insns) == INSN_LIST
5194 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5195 && need_for_split_p (potential_reload_hard_regs, regno + i)
5196 && split_reg (before_p, regno + i, insn, next_usage_insns))
5197 res = true;
5198 return res;
5201 /* Check only registers living at the current program point in the
5202 current EBB. */
5203 static bitmap_head live_regs;
5205 /* Update live info in EBB given by its HEAD and TAIL insns after
5206 inheritance/split transformation. The function removes dead moves
5207 too. */
5208 static void
5209 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5211 unsigned int j;
5212 int i, regno;
5213 bool live_p;
5214 rtx_insn *prev_insn;
5215 rtx set;
5216 bool remove_p;
5217 basic_block last_bb, prev_bb, curr_bb;
5218 bitmap_iterator bi;
5219 struct lra_insn_reg *reg;
5220 edge e;
5221 edge_iterator ei;
5223 last_bb = BLOCK_FOR_INSN (tail);
5224 prev_bb = NULL;
5225 for (curr_insn = tail;
5226 curr_insn != PREV_INSN (head);
5227 curr_insn = prev_insn)
5229 prev_insn = PREV_INSN (curr_insn);
5230 /* We need to process empty blocks too. They contain
5231 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5232 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5233 continue;
5234 curr_bb = BLOCK_FOR_INSN (curr_insn);
5235 if (curr_bb != prev_bb)
5237 if (prev_bb != NULL)
5239 /* Update df_get_live_in (prev_bb): */
5240 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5241 if (bitmap_bit_p (&live_regs, j))
5242 bitmap_set_bit (df_get_live_in (prev_bb), j);
5243 else
5244 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5246 if (curr_bb != last_bb)
5248 /* Update df_get_live_out (curr_bb): */
5249 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5251 live_p = bitmap_bit_p (&live_regs, j);
5252 if (! live_p)
5253 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5254 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5256 live_p = true;
5257 break;
5259 if (live_p)
5260 bitmap_set_bit (df_get_live_out (curr_bb), j);
5261 else
5262 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5265 prev_bb = curr_bb;
5266 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5268 if (! NONDEBUG_INSN_P (curr_insn))
5269 continue;
5270 curr_id = lra_get_insn_recog_data (curr_insn);
5271 curr_static_id = curr_id->insn_static_data;
5272 remove_p = false;
5273 if ((set = single_set (curr_insn)) != NULL_RTX
5274 && REG_P (SET_DEST (set))
5275 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5276 && SET_DEST (set) != pic_offset_table_rtx
5277 && bitmap_bit_p (&check_only_regs, regno)
5278 && ! bitmap_bit_p (&live_regs, regno))
5279 remove_p = true;
5280 /* See which defined values die here. */
5281 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5282 if (reg->type == OP_OUT && ! reg->subreg_p)
5283 bitmap_clear_bit (&live_regs, reg->regno);
5284 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5285 if (reg->type == OP_OUT && ! reg->subreg_p)
5286 bitmap_clear_bit (&live_regs, reg->regno);
5287 if (curr_id->arg_hard_regs != NULL)
5288 /* Make clobbered argument hard registers die. */
5289 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5290 if (regno >= FIRST_PSEUDO_REGISTER)
5291 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
5292 /* Mark each used value as live. */
5293 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5294 if (reg->type != OP_OUT
5295 && bitmap_bit_p (&check_only_regs, reg->regno))
5296 bitmap_set_bit (&live_regs, reg->regno);
5297 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5298 if (reg->type != OP_OUT
5299 && bitmap_bit_p (&check_only_regs, reg->regno))
5300 bitmap_set_bit (&live_regs, reg->regno);
5301 if (curr_id->arg_hard_regs != NULL)
5302 /* Make used argument hard registers live. */
5303 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5304 if (regno < FIRST_PSEUDO_REGISTER
5305 && bitmap_bit_p (&check_only_regs, regno))
5306 bitmap_set_bit (&live_regs, regno);
5307 /* It is quite important to remove dead move insns because it
5308 means removing dead store. We don't need to process them for
5309 constraints. */
5310 if (remove_p)
5312 if (lra_dump_file != NULL)
5314 fprintf (lra_dump_file, " Removing dead insn:\n ");
5315 dump_insn_slim (lra_dump_file, curr_insn);
5317 lra_set_insn_deleted (curr_insn);
5322 /* The structure describes info to do an inheritance for the current
5323 insn. We need to collect such info first before doing the
5324 transformations because the transformations change the insn
5325 internal representation. */
5326 struct to_inherit
5328 /* Original regno. */
5329 int regno;
5330 /* Subsequent insns which can inherit original reg value. */
5331 rtx insns;
5334 /* Array containing all info for doing inheritance from the current
5335 insn. */
5336 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5338 /* Number elements in the previous array. */
5339 static int to_inherit_num;
5341 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5342 structure to_inherit. */
5343 static void
5344 add_to_inherit (int regno, rtx insns)
5346 int i;
5348 for (i = 0; i < to_inherit_num; i++)
5349 if (to_inherit[i].regno == regno)
5350 return;
5351 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5352 to_inherit[to_inherit_num].regno = regno;
5353 to_inherit[to_inherit_num++].insns = insns;
5356 /* Return the last non-debug insn in basic block BB, or the block begin
5357 note if none. */
5358 static rtx_insn *
5359 get_last_insertion_point (basic_block bb)
5361 rtx_insn *insn;
5363 FOR_BB_INSNS_REVERSE (bb, insn)
5364 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5365 return insn;
5366 gcc_unreachable ();
5369 /* Set up RES by registers living on edges FROM except the edge (FROM,
5370 TO) or by registers set up in a jump insn in BB FROM. */
5371 static void
5372 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5374 rtx_insn *last;
5375 struct lra_insn_reg *reg;
5376 edge e;
5377 edge_iterator ei;
5379 lra_assert (to != NULL);
5380 bitmap_clear (res);
5381 FOR_EACH_EDGE (e, ei, from->succs)
5382 if (e->dest != to)
5383 bitmap_ior_into (res, df_get_live_in (e->dest));
5384 last = get_last_insertion_point (from);
5385 if (! JUMP_P (last))
5386 return;
5387 curr_id = lra_get_insn_recog_data (last);
5388 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5389 if (reg->type != OP_IN)
5390 bitmap_set_bit (res, reg->regno);
5393 /* Used as a temporary results of some bitmap calculations. */
5394 static bitmap_head temp_bitmap;
5396 /* We split for reloads of small class of hard regs. The following
5397 defines how many hard regs the class should have to be qualified as
5398 small. The code is mostly oriented to x86/x86-64 architecture
5399 where some insns need to use only specific register or pair of
5400 registers and these register can live in RTL explicitly, e.g. for
5401 parameter passing. */
5402 static const int max_small_class_regs_num = 2;
5404 /* Do inheritance/split transformations in EBB starting with HEAD and
5405 finishing on TAIL. We process EBB insns in the reverse order.
5406 Return true if we did any inheritance/split transformation in the
5407 EBB.
5409 We should avoid excessive splitting which results in worse code
5410 because of inaccurate cost calculations for spilling new split
5411 pseudos in such case. To achieve this we do splitting only if
5412 register pressure is high in given basic block and there are reload
5413 pseudos requiring hard registers. We could do more register
5414 pressure calculations at any given program point to avoid necessary
5415 splitting even more but it is to expensive and the current approach
5416 works well enough. */
5417 static bool
5418 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5420 int i, src_regno, dst_regno, nregs;
5421 bool change_p, succ_p, update_reloads_num_p;
5422 rtx_insn *prev_insn, *last_insn;
5423 rtx next_usage_insns, set;
5424 enum reg_class cl;
5425 struct lra_insn_reg *reg;
5426 basic_block last_processed_bb, curr_bb = NULL;
5427 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5428 bitmap to_process;
5429 unsigned int j;
5430 bitmap_iterator bi;
5431 bool head_p, after_p;
5433 change_p = false;
5434 curr_usage_insns_check++;
5435 reloads_num = calls_num = 0;
5436 bitmap_clear (&check_only_regs);
5437 last_processed_bb = NULL;
5438 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5439 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5440 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5441 /* We don't process new insns generated in the loop. */
5442 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5444 prev_insn = PREV_INSN (curr_insn);
5445 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5446 curr_bb = BLOCK_FOR_INSN (curr_insn);
5447 if (last_processed_bb != curr_bb)
5449 /* We are at the end of BB. Add qualified living
5450 pseudos for potential splitting. */
5451 to_process = df_get_live_out (curr_bb);
5452 if (last_processed_bb != NULL)
5454 /* We are somewhere in the middle of EBB. */
5455 get_live_on_other_edges (curr_bb, last_processed_bb,
5456 &temp_bitmap);
5457 to_process = &temp_bitmap;
5459 last_processed_bb = curr_bb;
5460 last_insn = get_last_insertion_point (curr_bb);
5461 after_p = (! JUMP_P (last_insn)
5462 && (! CALL_P (last_insn)
5463 || (find_reg_note (last_insn,
5464 REG_NORETURN, NULL_RTX) == NULL_RTX
5465 && ! SIBLING_CALL_P (last_insn))));
5466 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5467 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5469 if ((int) j >= lra_constraint_new_regno_start)
5470 break;
5471 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5473 if (j < FIRST_PSEUDO_REGISTER)
5474 SET_HARD_REG_BIT (live_hard_regs, j);
5475 else
5476 add_to_hard_reg_set (&live_hard_regs,
5477 PSEUDO_REGNO_MODE (j),
5478 reg_renumber[j]);
5479 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5483 src_regno = dst_regno = -1;
5484 if (NONDEBUG_INSN_P (curr_insn)
5485 && (set = single_set (curr_insn)) != NULL_RTX
5486 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5488 src_regno = REGNO (SET_SRC (set));
5489 dst_regno = REGNO (SET_DEST (set));
5491 update_reloads_num_p = true;
5492 if (src_regno < lra_constraint_new_regno_start
5493 && src_regno >= FIRST_PSEUDO_REGISTER
5494 && reg_renumber[src_regno] < 0
5495 && dst_regno >= lra_constraint_new_regno_start
5496 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5498 /* 'reload_pseudo <- original_pseudo'. */
5499 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5500 reloads_num++;
5501 update_reloads_num_p = false;
5502 succ_p = false;
5503 if (usage_insns[src_regno].check == curr_usage_insns_check
5504 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5505 succ_p = inherit_reload_reg (false, src_regno, cl,
5506 curr_insn, next_usage_insns);
5507 if (succ_p)
5508 change_p = true;
5509 else
5510 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5511 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5512 IOR_HARD_REG_SET (potential_reload_hard_regs,
5513 reg_class_contents[cl]);
5515 else if (src_regno >= lra_constraint_new_regno_start
5516 && dst_regno < lra_constraint_new_regno_start
5517 && dst_regno >= FIRST_PSEUDO_REGISTER
5518 && reg_renumber[dst_regno] < 0
5519 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5520 && usage_insns[dst_regno].check == curr_usage_insns_check
5521 && (next_usage_insns
5522 = usage_insns[dst_regno].insns) != NULL_RTX)
5524 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5525 reloads_num++;
5526 update_reloads_num_p = false;
5527 /* 'original_pseudo <- reload_pseudo'. */
5528 if (! JUMP_P (curr_insn)
5529 && inherit_reload_reg (true, dst_regno, cl,
5530 curr_insn, next_usage_insns))
5531 change_p = true;
5532 /* Invalidate. */
5533 usage_insns[dst_regno].check = 0;
5534 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5535 IOR_HARD_REG_SET (potential_reload_hard_regs,
5536 reg_class_contents[cl]);
5538 else if (INSN_P (curr_insn))
5540 int iter;
5541 int max_uid = get_max_uid ();
5543 curr_id = lra_get_insn_recog_data (curr_insn);
5544 curr_static_id = curr_id->insn_static_data;
5545 to_inherit_num = 0;
5546 /* Process insn definitions. */
5547 for (iter = 0; iter < 2; iter++)
5548 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5549 reg != NULL;
5550 reg = reg->next)
5551 if (reg->type != OP_IN
5552 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5554 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5555 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5556 && usage_insns[dst_regno].check == curr_usage_insns_check
5557 && (next_usage_insns
5558 = usage_insns[dst_regno].insns) != NULL_RTX)
5560 struct lra_insn_reg *r;
5562 for (r = curr_id->regs; r != NULL; r = r->next)
5563 if (r->type != OP_OUT && r->regno == dst_regno)
5564 break;
5565 /* Don't do inheritance if the pseudo is also
5566 used in the insn. */
5567 if (r == NULL)
5568 /* We can not do inheritance right now
5569 because the current insn reg info (chain
5570 regs) can change after that. */
5571 add_to_inherit (dst_regno, next_usage_insns);
5573 /* We can not process one reg twice here because of
5574 usage_insns invalidation. */
5575 if ((dst_regno < FIRST_PSEUDO_REGISTER
5576 || reg_renumber[dst_regno] >= 0)
5577 && ! reg->subreg_p && reg->type != OP_IN)
5579 HARD_REG_SET s;
5581 if (split_if_necessary (dst_regno, reg->biggest_mode,
5582 potential_reload_hard_regs,
5583 false, curr_insn, max_uid))
5584 change_p = true;
5585 CLEAR_HARD_REG_SET (s);
5586 if (dst_regno < FIRST_PSEUDO_REGISTER)
5587 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5588 else
5589 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5590 reg_renumber[dst_regno]);
5591 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5593 /* We should invalidate potential inheritance or
5594 splitting for the current insn usages to the next
5595 usage insns (see code below) as the output pseudo
5596 prevents this. */
5597 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5598 && reg_renumber[dst_regno] < 0)
5599 || (reg->type == OP_OUT && ! reg->subreg_p
5600 && (dst_regno < FIRST_PSEUDO_REGISTER
5601 || reg_renumber[dst_regno] >= 0)))
5603 /* Invalidate and mark definitions. */
5604 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5605 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5606 else
5608 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5609 for (i = 0; i < nregs; i++)
5610 usage_insns[dst_regno + i].check
5611 = -(int) INSN_UID (curr_insn);
5615 /* Process clobbered call regs. */
5616 if (curr_id->arg_hard_regs != NULL)
5617 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5618 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5619 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
5620 = -(int) INSN_UID (curr_insn);
5621 if (! JUMP_P (curr_insn))
5622 for (i = 0; i < to_inherit_num; i++)
5623 if (inherit_reload_reg (true, to_inherit[i].regno,
5624 ALL_REGS, curr_insn,
5625 to_inherit[i].insns))
5626 change_p = true;
5627 if (CALL_P (curr_insn))
5629 rtx cheap, pat, dest;
5630 rtx_insn *restore;
5631 int regno, hard_regno;
5633 calls_num++;
5634 if ((cheap = find_reg_note (curr_insn,
5635 REG_RETURNED, NULL_RTX)) != NULL_RTX
5636 && ((cheap = XEXP (cheap, 0)), true)
5637 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5638 && (hard_regno = reg_renumber[regno]) >= 0
5639 /* If there are pending saves/restores, the
5640 optimization is not worth. */
5641 && usage_insns[regno].calls_num == calls_num - 1
5642 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5644 /* Restore the pseudo from the call result as
5645 REG_RETURNED note says that the pseudo value is
5646 in the call result and the pseudo is an argument
5647 of the call. */
5648 pat = PATTERN (curr_insn);
5649 if (GET_CODE (pat) == PARALLEL)
5650 pat = XVECEXP (pat, 0, 0);
5651 dest = SET_DEST (pat);
5652 /* For multiple return values dest is PARALLEL.
5653 Currently we handle only single return value case. */
5654 if (REG_P (dest))
5656 start_sequence ();
5657 emit_move_insn (cheap, copy_rtx (dest));
5658 restore = get_insns ();
5659 end_sequence ();
5660 lra_process_new_insns (curr_insn, NULL, restore,
5661 "Inserting call parameter restore");
5662 /* We don't need to save/restore of the pseudo from
5663 this call. */
5664 usage_insns[regno].calls_num = calls_num;
5665 bitmap_set_bit (&check_only_regs, regno);
5669 to_inherit_num = 0;
5670 /* Process insn usages. */
5671 for (iter = 0; iter < 2; iter++)
5672 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5673 reg != NULL;
5674 reg = reg->next)
5675 if ((reg->type != OP_OUT
5676 || (reg->type == OP_OUT && reg->subreg_p))
5677 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5679 if (src_regno >= FIRST_PSEUDO_REGISTER
5680 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5682 if (usage_insns[src_regno].check == curr_usage_insns_check
5683 && (next_usage_insns
5684 = usage_insns[src_regno].insns) != NULL_RTX
5685 && NONDEBUG_INSN_P (curr_insn))
5686 add_to_inherit (src_regno, next_usage_insns);
5687 else if (usage_insns[src_regno].check
5688 != -(int) INSN_UID (curr_insn))
5689 /* Add usages but only if the reg is not set up
5690 in the same insn. */
5691 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5693 else if (src_regno < FIRST_PSEUDO_REGISTER
5694 || reg_renumber[src_regno] >= 0)
5696 bool before_p;
5697 rtx_insn *use_insn = curr_insn;
5699 before_p = (JUMP_P (curr_insn)
5700 || (CALL_P (curr_insn) && reg->type == OP_IN));
5701 if (NONDEBUG_INSN_P (curr_insn)
5702 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5703 && split_if_necessary (src_regno, reg->biggest_mode,
5704 potential_reload_hard_regs,
5705 before_p, curr_insn, max_uid))
5707 if (reg->subreg_p)
5708 lra_risky_transformations_p = true;
5709 change_p = true;
5710 /* Invalidate. */
5711 usage_insns[src_regno].check = 0;
5712 if (before_p)
5713 use_insn = PREV_INSN (curr_insn);
5715 if (NONDEBUG_INSN_P (curr_insn))
5717 if (src_regno < FIRST_PSEUDO_REGISTER)
5718 add_to_hard_reg_set (&live_hard_regs,
5719 reg->biggest_mode, src_regno);
5720 else
5721 add_to_hard_reg_set (&live_hard_regs,
5722 PSEUDO_REGNO_MODE (src_regno),
5723 reg_renumber[src_regno]);
5725 add_next_usage_insn (src_regno, use_insn, reloads_num);
5728 /* Process used call regs. */
5729 if (curr_id->arg_hard_regs != NULL)
5730 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5731 if (src_regno < FIRST_PSEUDO_REGISTER)
5733 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5734 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5736 for (i = 0; i < to_inherit_num; i++)
5738 src_regno = to_inherit[i].regno;
5739 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5740 curr_insn, to_inherit[i].insns))
5741 change_p = true;
5742 else
5743 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5746 if (update_reloads_num_p
5747 && NONDEBUG_INSN_P (curr_insn)
5748 && (set = single_set (curr_insn)) != NULL_RTX)
5750 int regno = -1;
5751 if ((REG_P (SET_DEST (set))
5752 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5753 && reg_renumber[regno] < 0
5754 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5755 || (REG_P (SET_SRC (set))
5756 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5757 && reg_renumber[regno] < 0
5758 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5760 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5761 reloads_num++;
5762 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5763 IOR_HARD_REG_SET (potential_reload_hard_regs,
5764 reg_class_contents[cl]);
5767 /* We reached the start of the current basic block. */
5768 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5769 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5771 /* We reached the beginning of the current block -- do
5772 rest of spliting in the current BB. */
5773 to_process = df_get_live_in (curr_bb);
5774 if (BLOCK_FOR_INSN (head) != curr_bb)
5776 /* We are somewhere in the middle of EBB. */
5777 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5778 curr_bb, &temp_bitmap);
5779 to_process = &temp_bitmap;
5781 head_p = true;
5782 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5784 if ((int) j >= lra_constraint_new_regno_start)
5785 break;
5786 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5787 && usage_insns[j].check == curr_usage_insns_check
5788 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5790 if (need_for_split_p (potential_reload_hard_regs, j))
5792 if (lra_dump_file != NULL && head_p)
5794 fprintf (lra_dump_file,
5795 " ----------------------------------\n");
5796 head_p = false;
5798 if (split_reg (false, j, bb_note (curr_bb),
5799 next_usage_insns))
5800 change_p = true;
5802 usage_insns[j].check = 0;
5807 return change_p;
5810 /* This value affects EBB forming. If probability of edge from EBB to
5811 a BB is not greater than the following value, we don't add the BB
5812 to EBB. */
5813 #define EBB_PROBABILITY_CUTOFF \
5814 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
5816 /* Current number of inheritance/split iteration. */
5817 int lra_inheritance_iter;
5819 /* Entry function for inheritance/split pass. */
5820 void
5821 lra_inheritance (void)
5823 int i;
5824 basic_block bb, start_bb;
5825 edge e;
5827 lra_inheritance_iter++;
5828 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5829 return;
5830 timevar_push (TV_LRA_INHERITANCE);
5831 if (lra_dump_file != NULL)
5832 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5833 lra_inheritance_iter);
5834 curr_usage_insns_check = 0;
5835 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5836 for (i = 0; i < lra_constraint_new_regno_start; i++)
5837 usage_insns[i].check = 0;
5838 bitmap_initialize (&check_only_regs, &reg_obstack);
5839 bitmap_initialize (&live_regs, &reg_obstack);
5840 bitmap_initialize (&temp_bitmap, &reg_obstack);
5841 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5842 FOR_EACH_BB_FN (bb, cfun)
5844 start_bb = bb;
5845 if (lra_dump_file != NULL)
5846 fprintf (lra_dump_file, "EBB");
5847 /* Form a EBB starting with BB. */
5848 bitmap_clear (&ebb_global_regs);
5849 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5850 for (;;)
5852 if (lra_dump_file != NULL)
5853 fprintf (lra_dump_file, " %d", bb->index);
5854 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5855 || LABEL_P (BB_HEAD (bb->next_bb)))
5856 break;
5857 e = find_fallthru_edge (bb->succs);
5858 if (! e)
5859 break;
5860 if (e->probability < EBB_PROBABILITY_CUTOFF)
5861 break;
5862 bb = bb->next_bb;
5864 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5865 if (lra_dump_file != NULL)
5866 fprintf (lra_dump_file, "\n");
5867 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5868 /* Remember that the EBB head and tail can change in
5869 inherit_in_ebb. */
5870 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5872 bitmap_clear (&ebb_global_regs);
5873 bitmap_clear (&temp_bitmap);
5874 bitmap_clear (&live_regs);
5875 bitmap_clear (&check_only_regs);
5876 free (usage_insns);
5878 timevar_pop (TV_LRA_INHERITANCE);
5883 /* This page contains code to undo failed inheritance/split
5884 transformations. */
5886 /* Current number of iteration undoing inheritance/split. */
5887 int lra_undo_inheritance_iter;
5889 /* Fix BB live info LIVE after removing pseudos created on pass doing
5890 inheritance/split which are REMOVED_PSEUDOS. */
5891 static void
5892 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5894 unsigned int regno;
5895 bitmap_iterator bi;
5897 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5898 if (bitmap_clear_bit (live, regno))
5899 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5902 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5903 number. */
5904 static int
5905 get_regno (rtx reg)
5907 if (GET_CODE (reg) == SUBREG)
5908 reg = SUBREG_REG (reg);
5909 if (REG_P (reg))
5910 return REGNO (reg);
5911 return -1;
5914 /* Delete a move INSN with destination reg DREGNO and a previous
5915 clobber insn with the same regno. The inheritance/split code can
5916 generate moves with preceding clobber and when we delete such moves
5917 we should delete the clobber insn too to keep the correct life
5918 info. */
5919 static void
5920 delete_move_and_clobber (rtx_insn *insn, int dregno)
5922 rtx_insn *prev_insn = PREV_INSN (insn);
5924 lra_set_insn_deleted (insn);
5925 lra_assert (dregno >= 0);
5926 if (prev_insn != NULL && NONDEBUG_INSN_P (prev_insn)
5927 && GET_CODE (PATTERN (prev_insn)) == CLOBBER
5928 && dregno == get_regno (XEXP (PATTERN (prev_insn), 0)))
5929 lra_set_insn_deleted (prev_insn);
5932 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5933 return true if we did any change. The undo transformations for
5934 inheritance looks like
5935 i <- i2
5936 p <- i => p <- i2
5937 or removing
5938 p <- i, i <- p, and i <- i3
5939 where p is original pseudo from which inheritance pseudo i was
5940 created, i and i3 are removed inheritance pseudos, i2 is another
5941 not removed inheritance pseudo. All split pseudos or other
5942 occurrences of removed inheritance pseudos are changed on the
5943 corresponding original pseudos.
5945 The function also schedules insns changed and created during
5946 inheritance/split pass for processing by the subsequent constraint
5947 pass. */
5948 static bool
5949 remove_inheritance_pseudos (bitmap remove_pseudos)
5951 basic_block bb;
5952 int regno, sregno, prev_sregno, dregno, restore_regno;
5953 rtx set, prev_set;
5954 rtx_insn *prev_insn;
5955 bool change_p, done_p;
5957 change_p = ! bitmap_empty_p (remove_pseudos);
5958 /* We can not finish the function right away if CHANGE_P is true
5959 because we need to marks insns affected by previous
5960 inheritance/split pass for processing by the subsequent
5961 constraint pass. */
5962 FOR_EACH_BB_FN (bb, cfun)
5964 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5965 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5966 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5968 if (! INSN_P (curr_insn))
5969 continue;
5970 done_p = false;
5971 sregno = dregno = -1;
5972 if (change_p && NONDEBUG_INSN_P (curr_insn)
5973 && (set = single_set (curr_insn)) != NULL_RTX)
5975 dregno = get_regno (SET_DEST (set));
5976 sregno = get_regno (SET_SRC (set));
5979 if (sregno >= 0 && dregno >= 0)
5981 if ((bitmap_bit_p (remove_pseudos, sregno)
5982 && (lra_reg_info[sregno].restore_regno == dregno
5983 || (bitmap_bit_p (remove_pseudos, dregno)
5984 && (lra_reg_info[sregno].restore_regno
5985 == lra_reg_info[dregno].restore_regno))))
5986 || (bitmap_bit_p (remove_pseudos, dregno)
5987 && lra_reg_info[dregno].restore_regno == sregno))
5988 /* One of the following cases:
5989 original <- removed inheritance pseudo
5990 removed inherit pseudo <- another removed inherit pseudo
5991 removed inherit pseudo <- original pseudo
5993 removed_split_pseudo <- original_reg
5994 original_reg <- removed_split_pseudo */
5996 if (lra_dump_file != NULL)
5998 fprintf (lra_dump_file, " Removing %s:\n",
5999 bitmap_bit_p (&lra_split_regs, sregno)
6000 || bitmap_bit_p (&lra_split_regs, dregno)
6001 ? "split" : "inheritance");
6002 dump_insn_slim (lra_dump_file, curr_insn);
6004 delete_move_and_clobber (curr_insn, dregno);
6005 done_p = true;
6007 else if (bitmap_bit_p (remove_pseudos, sregno)
6008 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
6010 /* Search the following pattern:
6011 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
6012 original_pseudo <- inherit_or_split_pseudo1
6013 where the 2nd insn is the current insn and
6014 inherit_or_split_pseudo2 is not removed. If it is found,
6015 change the current insn onto:
6016 original_pseudo <- inherit_or_split_pseudo2. */
6017 for (prev_insn = PREV_INSN (curr_insn);
6018 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
6019 prev_insn = PREV_INSN (prev_insn))
6021 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
6022 && (prev_set = single_set (prev_insn)) != NULL_RTX
6023 /* There should be no subregs in insn we are
6024 searching because only the original reg might
6025 be in subreg when we changed the mode of
6026 load/store for splitting. */
6027 && REG_P (SET_DEST (prev_set))
6028 && REG_P (SET_SRC (prev_set))
6029 && (int) REGNO (SET_DEST (prev_set)) == sregno
6030 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
6031 >= FIRST_PSEUDO_REGISTER)
6032 /* As we consider chain of inheritance or
6033 splitting described in above comment we should
6034 check that sregno and prev_sregno were
6035 inheritance/split pseudos created from the
6036 same original regno. */
6037 && (lra_reg_info[sregno].restore_regno
6038 == lra_reg_info[prev_sregno].restore_regno)
6039 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
6041 lra_assert (GET_MODE (SET_SRC (prev_set))
6042 == GET_MODE (regno_reg_rtx[sregno]));
6043 if (GET_CODE (SET_SRC (set)) == SUBREG)
6044 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
6045 else
6046 SET_SRC (set) = SET_SRC (prev_set);
6047 /* As we are finishing with processing the insn
6048 here, check the destination too as it might
6049 inheritance pseudo for another pseudo. */
6050 if (bitmap_bit_p (remove_pseudos, dregno)
6051 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
6052 && (restore_regno
6053 = lra_reg_info[dregno].restore_regno) >= 0)
6055 if (GET_CODE (SET_DEST (set)) == SUBREG)
6056 SUBREG_REG (SET_DEST (set))
6057 = regno_reg_rtx[restore_regno];
6058 else
6059 SET_DEST (set) = regno_reg_rtx[restore_regno];
6061 lra_push_insn_and_update_insn_regno_info (curr_insn);
6062 lra_set_used_insn_alternative_by_uid
6063 (INSN_UID (curr_insn), -1);
6064 done_p = true;
6065 if (lra_dump_file != NULL)
6067 fprintf (lra_dump_file, " Change reload insn:\n");
6068 dump_insn_slim (lra_dump_file, curr_insn);
6073 if (! done_p)
6075 struct lra_insn_reg *reg;
6076 bool restored_regs_p = false;
6077 bool kept_regs_p = false;
6079 curr_id = lra_get_insn_recog_data (curr_insn);
6080 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6082 regno = reg->regno;
6083 restore_regno = lra_reg_info[regno].restore_regno;
6084 if (restore_regno >= 0)
6086 if (change_p && bitmap_bit_p (remove_pseudos, regno))
6088 lra_substitute_pseudo_within_insn
6089 (curr_insn, regno, regno_reg_rtx[restore_regno],
6090 false);
6091 restored_regs_p = true;
6093 else
6094 kept_regs_p = true;
6097 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
6099 /* The instruction has changed since the previous
6100 constraints pass. */
6101 lra_push_insn_and_update_insn_regno_info (curr_insn);
6102 lra_set_used_insn_alternative_by_uid
6103 (INSN_UID (curr_insn), -1);
6105 else if (restored_regs_p)
6106 /* The instruction has been restored to the form that
6107 it had during the previous constraints pass. */
6108 lra_update_insn_regno_info (curr_insn);
6109 if (restored_regs_p && lra_dump_file != NULL)
6111 fprintf (lra_dump_file, " Insn after restoring regs:\n");
6112 dump_insn_slim (lra_dump_file, curr_insn);
6117 return change_p;
6120 /* If optional reload pseudos failed to get a hard register or was not
6121 inherited, it is better to remove optional reloads. We do this
6122 transformation after undoing inheritance to figure out necessity to
6123 remove optional reloads easier. Return true if we do any
6124 change. */
6125 static bool
6126 undo_optional_reloads (void)
6128 bool change_p, keep_p;
6129 unsigned int regno, uid;
6130 bitmap_iterator bi, bi2;
6131 rtx_insn *insn;
6132 rtx set, src, dest;
6133 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
6135 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
6136 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
6137 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6139 keep_p = false;
6140 /* Keep optional reloads from previous subpasses. */
6141 if (lra_reg_info[regno].restore_regno < 0
6142 /* If the original pseudo changed its allocation, just
6143 removing the optional pseudo is dangerous as the original
6144 pseudo will have longer live range. */
6145 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
6146 keep_p = true;
6147 else if (reg_renumber[regno] >= 0)
6148 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
6150 insn = lra_insn_recog_data[uid]->insn;
6151 if ((set = single_set (insn)) == NULL_RTX)
6152 continue;
6153 src = SET_SRC (set);
6154 dest = SET_DEST (set);
6155 if (! REG_P (src) || ! REG_P (dest))
6156 continue;
6157 if (REGNO (dest) == regno
6158 /* Ignore insn for optional reloads itself. */
6159 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
6160 /* Check only inheritance on last inheritance pass. */
6161 && (int) REGNO (src) >= new_regno_start
6162 /* Check that the optional reload was inherited. */
6163 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6165 keep_p = true;
6166 break;
6169 if (keep_p)
6171 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6172 if (lra_dump_file != NULL)
6173 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6176 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6177 bitmap_initialize (&insn_bitmap, &reg_obstack);
6178 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6180 if (lra_dump_file != NULL)
6181 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6182 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6183 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6185 insn = lra_insn_recog_data[uid]->insn;
6186 if ((set = single_set (insn)) != NULL_RTX)
6188 src = SET_SRC (set);
6189 dest = SET_DEST (set);
6190 if (REG_P (src) && REG_P (dest)
6191 && ((REGNO (src) == regno
6192 && (lra_reg_info[regno].restore_regno
6193 == (int) REGNO (dest)))
6194 || (REGNO (dest) == regno
6195 && (lra_reg_info[regno].restore_regno
6196 == (int) REGNO (src)))))
6198 if (lra_dump_file != NULL)
6200 fprintf (lra_dump_file, " Deleting move %u\n",
6201 INSN_UID (insn));
6202 dump_insn_slim (lra_dump_file, insn);
6204 delete_move_and_clobber (insn, REGNO (dest));
6205 continue;
6207 /* We should not worry about generation memory-memory
6208 moves here as if the corresponding inheritance did
6209 not work (inheritance pseudo did not get a hard reg),
6210 we remove the inheritance pseudo and the optional
6211 reload. */
6213 lra_substitute_pseudo_within_insn
6214 (insn, regno, regno_reg_rtx[lra_reg_info[regno].restore_regno],
6215 false);
6216 lra_update_insn_regno_info (insn);
6217 if (lra_dump_file != NULL)
6219 fprintf (lra_dump_file,
6220 " Restoring original insn:\n");
6221 dump_insn_slim (lra_dump_file, insn);
6225 /* Clear restore_regnos. */
6226 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6227 lra_reg_info[regno].restore_regno = -1;
6228 bitmap_clear (&insn_bitmap);
6229 bitmap_clear (&removed_optional_reload_pseudos);
6230 return change_p;
6233 /* Entry function for undoing inheritance/split transformation. Return true
6234 if we did any RTL change in this pass. */
6235 bool
6236 lra_undo_inheritance (void)
6238 unsigned int regno;
6239 int restore_regno, hard_regno;
6240 int n_all_inherit, n_inherit, n_all_split, n_split;
6241 bitmap_head remove_pseudos;
6242 bitmap_iterator bi;
6243 bool change_p;
6245 lra_undo_inheritance_iter++;
6246 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6247 return false;
6248 if (lra_dump_file != NULL)
6249 fprintf (lra_dump_file,
6250 "\n********** Undoing inheritance #%d: **********\n\n",
6251 lra_undo_inheritance_iter);
6252 bitmap_initialize (&remove_pseudos, &reg_obstack);
6253 n_inherit = n_all_inherit = 0;
6254 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6255 if (lra_reg_info[regno].restore_regno >= 0)
6257 n_all_inherit++;
6258 if (reg_renumber[regno] < 0
6259 /* If the original pseudo changed its allocation, just
6260 removing inheritance is dangerous as for changing
6261 allocation we used shorter live-ranges. */
6262 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6263 bitmap_set_bit (&remove_pseudos, regno);
6264 else
6265 n_inherit++;
6267 if (lra_dump_file != NULL && n_all_inherit != 0)
6268 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6269 n_inherit, n_all_inherit,
6270 (double) n_inherit / n_all_inherit * 100);
6271 n_split = n_all_split = 0;
6272 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6273 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6275 n_all_split++;
6276 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6277 ? reg_renumber[restore_regno] : restore_regno);
6278 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6279 bitmap_set_bit (&remove_pseudos, regno);
6280 else
6282 n_split++;
6283 if (lra_dump_file != NULL)
6284 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6285 regno, restore_regno);
6288 if (lra_dump_file != NULL && n_all_split != 0)
6289 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6290 n_split, n_all_split,
6291 (double) n_split / n_all_split * 100);
6292 change_p = remove_inheritance_pseudos (&remove_pseudos);
6293 bitmap_clear (&remove_pseudos);
6294 /* Clear restore_regnos. */
6295 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6296 lra_reg_info[regno].restore_regno = -1;
6297 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6298 lra_reg_info[regno].restore_regno = -1;
6299 change_p = undo_optional_reloads () || change_p;
6300 return change_p;