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