max(INT_MIN, x) -> x
[official-gcc.git] / gcc / lra-constraints.c
blob14d5f1dc538896d01748bbbd78bb4850f110602a
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)
1310 loc = &SUBREG_REG (*loc);
1311 reg = *loc;
1312 mode = GET_MODE (reg);
1313 if (! REG_P (reg))
1315 if (check_only_p)
1316 return true;
1317 /* Always reload memory in an address even if the target supports
1318 such addresses. */
1319 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1320 before_p = true;
1322 else
1324 regno = REGNO (reg);
1325 rclass = get_reg_class (regno);
1326 if (! check_only_p
1327 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1329 if (lra_dump_file != NULL)
1331 fprintf (lra_dump_file,
1332 "Changing pseudo %d in address of insn %u on equiv ",
1333 REGNO (reg), INSN_UID (curr_insn));
1334 dump_value_slim (lra_dump_file, *loc, 1);
1335 fprintf (lra_dump_file, "\n");
1337 *loc = copy_rtx (*loc);
1339 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1341 if (check_only_p)
1342 return true;
1343 reg = *loc;
1344 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1345 mode, reg, cl, subreg_p, "address", &new_reg))
1346 before_p = true;
1348 else if (new_class != NO_REGS && rclass != new_class)
1350 if (check_only_p)
1351 return true;
1352 lra_change_class (regno, new_class, " Change to", true);
1353 return false;
1355 else
1356 return false;
1358 if (before_p)
1360 push_to_sequence (*before);
1361 lra_emit_move (new_reg, reg);
1362 *before = get_insns ();
1363 end_sequence ();
1365 *loc = new_reg;
1366 if (after != NULL)
1368 start_sequence ();
1369 lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1370 emit_insn (*after);
1371 *after = get_insns ();
1372 end_sequence ();
1374 return true;
1377 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1378 the insn to be inserted before curr insn. AFTER returns the
1379 the insn to be inserted after curr insn. ORIGREG and NEWREG
1380 are the original reg and new reg for reload. */
1381 static void
1382 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1383 rtx newreg)
1385 if (before)
1387 push_to_sequence (*before);
1388 lra_emit_move (newreg, origreg);
1389 *before = get_insns ();
1390 end_sequence ();
1392 if (after)
1394 start_sequence ();
1395 lra_emit_move (origreg, newreg);
1396 emit_insn (*after);
1397 *after = get_insns ();
1398 end_sequence ();
1402 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1404 /* Make reloads for subreg in operand NOP with internal subreg mode
1405 REG_MODE, add new reloads for further processing. Return true if
1406 any change was done. */
1407 static bool
1408 simplify_operand_subreg (int nop, machine_mode reg_mode)
1410 int hard_regno;
1411 rtx_insn *before, *after;
1412 machine_mode mode, innermode;
1413 rtx reg, new_reg;
1414 rtx operand = *curr_id->operand_loc[nop];
1415 enum reg_class regclass;
1416 enum op_type type;
1418 before = after = NULL;
1420 if (GET_CODE (operand) != SUBREG)
1421 return false;
1423 mode = GET_MODE (operand);
1424 reg = SUBREG_REG (operand);
1425 innermode = GET_MODE (reg);
1426 type = curr_static_id->operand[nop].type;
1427 /* If we change address for paradoxical subreg of memory, the
1428 address might violate the necessary alignment or the access might
1429 be slow. So take this into consideration. We should not worry
1430 about access beyond allocated memory for paradoxical memory
1431 subregs as we don't substitute such equiv memory (see processing
1432 equivalences in function lra_constraints) and because for spilled
1433 pseudos we allocate stack memory enough for the biggest
1434 corresponding paradoxical subreg. */
1435 if (MEM_P (reg)
1436 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1437 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1439 rtx subst, old = *curr_id->operand_loc[nop];
1441 alter_subreg (curr_id->operand_loc[nop], false);
1442 subst = *curr_id->operand_loc[nop];
1443 lra_assert (MEM_P (subst));
1444 if (! valid_address_p (innermode, XEXP (reg, 0),
1445 MEM_ADDR_SPACE (reg))
1446 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1447 MEM_ADDR_SPACE (subst)))
1448 return true;
1449 else if ((get_constraint_type (lookup_constraint
1450 (curr_static_id->operand[nop].constraint))
1451 != CT_SPECIAL_MEMORY)
1452 /* We still can reload address and if the address is
1453 valid, we can remove subreg without reloading its
1454 inner memory. */
1455 && valid_address_p (GET_MODE (subst),
1456 regno_reg_rtx
1457 [ira_class_hard_regs
1458 [base_reg_class (GET_MODE (subst),
1459 MEM_ADDR_SPACE (subst),
1460 ADDRESS, SCRATCH)][0]],
1461 MEM_ADDR_SPACE (subst)))
1462 return true;
1464 /* If the address was valid and became invalid, prefer to reload
1465 the memory. Typical case is when the index scale should
1466 correspond the memory. */
1467 *curr_id->operand_loc[nop] = old;
1469 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1471 alter_subreg (curr_id->operand_loc[nop], false);
1472 return true;
1474 else if (CONSTANT_P (reg))
1476 /* Try to simplify subreg of constant. It is usually result of
1477 equivalence substitution. */
1478 if (innermode == VOIDmode
1479 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1480 innermode = curr_static_id->operand[nop].mode;
1481 if ((new_reg = simplify_subreg (mode, reg, innermode,
1482 SUBREG_BYTE (operand))) != NULL_RTX)
1484 *curr_id->operand_loc[nop] = new_reg;
1485 return true;
1488 /* Put constant into memory when we have mixed modes. It generates
1489 a better code in most cases as it does not need a secondary
1490 reload memory. It also prevents LRA looping when LRA is using
1491 secondary reload memory again and again. */
1492 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1493 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1495 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1496 alter_subreg (curr_id->operand_loc[nop], false);
1497 return true;
1499 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1500 if there may be a problem accessing OPERAND in the outer
1501 mode. */
1502 if ((REG_P (reg)
1503 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1504 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1505 /* Don't reload paradoxical subregs because we could be looping
1506 having repeatedly final regno out of hard regs range. */
1507 && (hard_regno_nregs[hard_regno][innermode]
1508 >= hard_regno_nregs[hard_regno][mode])
1509 && simplify_subreg_regno (hard_regno, innermode,
1510 SUBREG_BYTE (operand), mode) < 0
1511 /* Don't reload subreg for matching reload. It is actually
1512 valid subreg in LRA. */
1513 && ! LRA_SUBREG_P (operand))
1514 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1516 enum reg_class rclass;
1518 if (REG_P (reg))
1519 /* There is a big probability that we will get the same class
1520 for the new pseudo and we will get the same insn which
1521 means infinite looping. So spill the new pseudo. */
1522 rclass = NO_REGS;
1523 else
1524 /* The class will be defined later in curr_insn_transform. */
1525 rclass
1526 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1528 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1529 rclass, TRUE, "subreg reg", &new_reg))
1531 bool insert_before, insert_after;
1532 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1534 insert_before = (type != OP_OUT
1535 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1536 insert_after = (type != OP_IN);
1537 insert_move_for_subreg (insert_before ? &before : NULL,
1538 insert_after ? &after : NULL,
1539 reg, new_reg);
1541 SUBREG_REG (operand) = new_reg;
1542 lra_process_new_insns (curr_insn, before, after,
1543 "Inserting subreg reload");
1544 return true;
1546 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1547 IRA allocates hardreg to the inner pseudo reg according to its mode
1548 instead of the outermode, so the size of the hardreg may not be enough
1549 to contain the outermode operand, in that case we may need to insert
1550 reload for the reg. For the following two types of paradoxical subreg,
1551 we need to insert reload:
1552 1. If the op_type is OP_IN, and the hardreg could not be paired with
1553 other hardreg to contain the outermode operand
1554 (checked by in_hard_reg_set_p), we need to insert the reload.
1555 2. If the op_type is OP_OUT or OP_INOUT.
1557 Here is a paradoxical subreg example showing how the reload is generated:
1559 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1560 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1562 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1563 here, if reg107 is assigned to hardreg R15, because R15 is the last
1564 hardreg, compiler cannot find another hardreg to pair with R15 to
1565 contain TImode data. So we insert a TImode reload reg180 for it.
1566 After reload is inserted:
1568 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1569 (reg:DI 107 [ __comp ])) -1
1570 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1571 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1573 Two reload hard registers will be allocated to reg180 to save TImode data
1574 in LRA_assign. */
1575 else if (REG_P (reg)
1576 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1577 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1578 && (hard_regno_nregs[hard_regno][innermode]
1579 < hard_regno_nregs[hard_regno][mode])
1580 && (regclass = lra_get_allocno_class (REGNO (reg)))
1581 && (type != OP_IN
1582 || !in_hard_reg_set_p (reg_class_contents[regclass],
1583 mode, hard_regno)))
1585 /* The class will be defined later in curr_insn_transform. */
1586 enum reg_class rclass
1587 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1589 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1590 rclass, TRUE, "paradoxical subreg", &new_reg))
1592 rtx subreg;
1593 bool insert_before, insert_after;
1595 PUT_MODE (new_reg, mode);
1596 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1597 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1599 insert_before = (type != OP_OUT);
1600 insert_after = (type != OP_IN);
1601 insert_move_for_subreg (insert_before ? &before : NULL,
1602 insert_after ? &after : NULL,
1603 reg, subreg);
1605 SUBREG_REG (operand) = new_reg;
1606 lra_process_new_insns (curr_insn, before, after,
1607 "Inserting paradoxical subreg reload");
1608 return true;
1610 return false;
1613 /* Return TRUE if X refers for a hard register from SET. */
1614 static bool
1615 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1617 int i, j, x_hard_regno;
1618 machine_mode mode;
1619 const char *fmt;
1620 enum rtx_code code;
1622 if (x == NULL_RTX)
1623 return false;
1624 code = GET_CODE (x);
1625 mode = GET_MODE (x);
1626 if (code == SUBREG)
1628 x = SUBREG_REG (x);
1629 code = GET_CODE (x);
1630 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1631 mode = GET_MODE (x);
1634 if (REG_P (x))
1636 x_hard_regno = get_hard_regno (x);
1637 return (x_hard_regno >= 0
1638 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1640 if (MEM_P (x))
1642 struct address_info ad;
1644 decompose_mem_address (&ad, x);
1645 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1646 return true;
1647 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1648 return true;
1650 fmt = GET_RTX_FORMAT (code);
1651 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1653 if (fmt[i] == 'e')
1655 if (uses_hard_regs_p (XEXP (x, i), set))
1656 return true;
1658 else if (fmt[i] == 'E')
1660 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1661 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1662 return true;
1665 return false;
1668 /* Return true if OP is a spilled pseudo. */
1669 static inline bool
1670 spilled_pseudo_p (rtx op)
1672 return (REG_P (op)
1673 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1676 /* Return true if X is a general constant. */
1677 static inline bool
1678 general_constant_p (rtx x)
1680 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1683 static bool
1684 reg_in_class_p (rtx reg, enum reg_class cl)
1686 if (cl == NO_REGS)
1687 return get_reg_class (REGNO (reg)) == NO_REGS;
1688 return in_class_p (reg, cl, NULL);
1691 /* Return true if SET of RCLASS contains no hard regs which can be
1692 used in MODE. */
1693 static bool
1694 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1695 HARD_REG_SET &set,
1696 enum machine_mode mode)
1698 HARD_REG_SET temp;
1700 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1701 COPY_HARD_REG_SET (temp, set);
1702 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1703 return (hard_reg_set_subset_p
1704 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1707 /* Major function to choose the current insn alternative and what
1708 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1709 negative we should consider only this alternative. Return false if
1710 we can not choose the alternative or find how to reload the
1711 operands. */
1712 static bool
1713 process_alt_operands (int only_alternative)
1715 bool ok_p = false;
1716 int nop, overall, nalt;
1717 int n_alternatives = curr_static_id->n_alternatives;
1718 int n_operands = curr_static_id->n_operands;
1719 /* LOSERS counts the operands that don't fit this alternative and
1720 would require loading. */
1721 int losers;
1722 /* REJECT is a count of how undesirable this alternative says it is
1723 if any reloading is required. If the alternative matches exactly
1724 then REJECT is ignored, but otherwise it gets this much counted
1725 against it in addition to the reloading needed. */
1726 int reject;
1727 int op_reject;
1728 /* The number of elements in the following array. */
1729 int early_clobbered_regs_num;
1730 /* Numbers of operands which are early clobber registers. */
1731 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1732 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1733 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1734 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1735 bool curr_alt_win[MAX_RECOG_OPERANDS];
1736 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1737 int curr_alt_matches[MAX_RECOG_OPERANDS];
1738 /* The number of elements in the following array. */
1739 int curr_alt_dont_inherit_ops_num;
1740 /* Numbers of operands whose reload pseudos should not be inherited. */
1741 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1742 rtx op;
1743 /* The register when the operand is a subreg of register, otherwise the
1744 operand itself. */
1745 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1746 /* The register if the operand is a register or subreg of register,
1747 otherwise NULL. */
1748 rtx operand_reg[MAX_RECOG_OPERANDS];
1749 int hard_regno[MAX_RECOG_OPERANDS];
1750 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1751 int reload_nregs, reload_sum;
1752 bool costly_p;
1753 enum reg_class cl;
1755 /* Calculate some data common for all alternatives to speed up the
1756 function. */
1757 for (nop = 0; nop < n_operands; nop++)
1759 rtx reg;
1761 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1762 /* The real hard regno of the operand after the allocation. */
1763 hard_regno[nop] = get_hard_regno (op);
1765 operand_reg[nop] = reg = op;
1766 biggest_mode[nop] = GET_MODE (op);
1767 if (GET_CODE (op) == SUBREG)
1769 operand_reg[nop] = reg = SUBREG_REG (op);
1770 if (GET_MODE_SIZE (biggest_mode[nop])
1771 < GET_MODE_SIZE (GET_MODE (reg)))
1772 biggest_mode[nop] = GET_MODE (reg);
1774 if (! REG_P (reg))
1775 operand_reg[nop] = NULL_RTX;
1776 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1777 || ((int) REGNO (reg)
1778 == lra_get_elimination_hard_regno (REGNO (reg))))
1779 no_subreg_reg_operand[nop] = reg;
1780 else
1781 operand_reg[nop] = no_subreg_reg_operand[nop]
1782 /* Just use natural mode for elimination result. It should
1783 be enough for extra constraints hooks. */
1784 = regno_reg_rtx[hard_regno[nop]];
1787 /* The constraints are made of several alternatives. Each operand's
1788 constraint looks like foo,bar,... with commas separating the
1789 alternatives. The first alternatives for all operands go
1790 together, the second alternatives go together, etc.
1792 First loop over alternatives. */
1793 alternative_mask preferred = curr_id->preferred_alternatives;
1794 if (only_alternative >= 0)
1795 preferred &= ALTERNATIVE_BIT (only_alternative);
1797 for (nalt = 0; nalt < n_alternatives; nalt++)
1799 /* Loop over operands for one constraint alternative. */
1800 if (!TEST_BIT (preferred, nalt))
1801 continue;
1803 overall = losers = reject = reload_nregs = reload_sum = 0;
1804 for (nop = 0; nop < n_operands; nop++)
1806 int inc = (curr_static_id
1807 ->operand_alternative[nalt * n_operands + nop].reject);
1808 if (lra_dump_file != NULL && inc != 0)
1809 fprintf (lra_dump_file,
1810 " Staticly defined alt reject+=%d\n", inc);
1811 reject += inc;
1813 early_clobbered_regs_num = 0;
1815 for (nop = 0; nop < n_operands; nop++)
1817 const char *p;
1818 char *end;
1819 int len, c, m, i, opalt_num, this_alternative_matches;
1820 bool win, did_match, offmemok, early_clobber_p;
1821 /* false => this operand can be reloaded somehow for this
1822 alternative. */
1823 bool badop;
1824 /* true => this operand can be reloaded if the alternative
1825 allows regs. */
1826 bool winreg;
1827 /* True if a constant forced into memory would be OK for
1828 this operand. */
1829 bool constmemok;
1830 enum reg_class this_alternative, this_costly_alternative;
1831 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1832 bool this_alternative_match_win, this_alternative_win;
1833 bool this_alternative_offmemok;
1834 bool scratch_p;
1835 machine_mode mode;
1836 enum constraint_num cn;
1838 opalt_num = nalt * n_operands + nop;
1839 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1841 /* Fast track for no constraints at all. */
1842 curr_alt[nop] = NO_REGS;
1843 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1844 curr_alt_win[nop] = true;
1845 curr_alt_match_win[nop] = false;
1846 curr_alt_offmemok[nop] = false;
1847 curr_alt_matches[nop] = -1;
1848 continue;
1851 op = no_subreg_reg_operand[nop];
1852 mode = curr_operand_mode[nop];
1854 win = did_match = winreg = offmemok = constmemok = false;
1855 badop = true;
1857 early_clobber_p = false;
1858 p = curr_static_id->operand_alternative[opalt_num].constraint;
1860 this_costly_alternative = this_alternative = NO_REGS;
1861 /* We update set of possible hard regs besides its class
1862 because reg class might be inaccurate. For example,
1863 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1864 is translated in HI_REGS because classes are merged by
1865 pairs and there is no accurate intermediate class. */
1866 CLEAR_HARD_REG_SET (this_alternative_set);
1867 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1868 this_alternative_win = false;
1869 this_alternative_match_win = false;
1870 this_alternative_offmemok = false;
1871 this_alternative_matches = -1;
1873 /* An empty constraint should be excluded by the fast
1874 track. */
1875 lra_assert (*p != 0 && *p != ',');
1877 op_reject = 0;
1878 /* Scan this alternative's specs for this operand; set WIN
1879 if the operand fits any letter in this alternative.
1880 Otherwise, clear BADOP if this operand could fit some
1881 letter after reloads, or set WINREG if this operand could
1882 fit after reloads provided the constraint allows some
1883 registers. */
1884 costly_p = false;
1887 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1889 case '\0':
1890 len = 0;
1891 break;
1892 case ',':
1893 c = '\0';
1894 break;
1896 case '&':
1897 early_clobber_p = true;
1898 break;
1900 case '$':
1901 op_reject += LRA_MAX_REJECT;
1902 break;
1903 case '^':
1904 op_reject += LRA_LOSER_COST_FACTOR;
1905 break;
1907 case '#':
1908 /* Ignore rest of this alternative. */
1909 c = '\0';
1910 break;
1912 case '0': case '1': case '2': case '3': case '4':
1913 case '5': case '6': case '7': case '8': case '9':
1915 int m_hregno;
1916 bool match_p;
1918 m = strtoul (p, &end, 10);
1919 p = end;
1920 len = 0;
1921 lra_assert (nop > m);
1923 this_alternative_matches = m;
1924 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1925 /* We are supposed to match a previous operand.
1926 If we do, we win if that one did. If we do
1927 not, count both of the operands as losers.
1928 (This is too conservative, since most of the
1929 time only a single reload insn will be needed
1930 to make the two operands win. As a result,
1931 this alternative may be rejected when it is
1932 actually desirable.) */
1933 match_p = false;
1934 if (operands_match_p (*curr_id->operand_loc[nop],
1935 *curr_id->operand_loc[m], m_hregno))
1937 /* We should reject matching of an early
1938 clobber operand if the matching operand is
1939 not dying in the insn. */
1940 if (! curr_static_id->operand[m].early_clobber
1941 || operand_reg[nop] == NULL_RTX
1942 || (find_regno_note (curr_insn, REG_DEAD,
1943 REGNO (op))
1944 || REGNO (op) == REGNO (operand_reg[m])))
1945 match_p = true;
1947 if (match_p)
1949 /* If we are matching a non-offsettable
1950 address where an offsettable address was
1951 expected, then we must reject this
1952 combination, because we can't reload
1953 it. */
1954 if (curr_alt_offmemok[m]
1955 && MEM_P (*curr_id->operand_loc[m])
1956 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1957 continue;
1959 else
1961 /* Operands don't match. Both operands must
1962 allow a reload register, otherwise we
1963 cannot make them match. */
1964 if (curr_alt[m] == NO_REGS)
1965 break;
1966 /* Retroactively mark the operand we had to
1967 match as a loser, if it wasn't already and
1968 it wasn't matched to a register constraint
1969 (e.g it might be matched by memory). */
1970 if (curr_alt_win[m]
1971 && (operand_reg[m] == NULL_RTX
1972 || hard_regno[m] < 0))
1974 losers++;
1975 reload_nregs
1976 += (ira_reg_class_max_nregs[curr_alt[m]]
1977 [GET_MODE (*curr_id->operand_loc[m])]);
1980 /* Prefer matching earlyclobber alternative as
1981 it results in less hard regs required for
1982 the insn than a non-matching earlyclobber
1983 alternative. */
1984 if (curr_static_id->operand[m].early_clobber)
1986 if (lra_dump_file != NULL)
1987 fprintf
1988 (lra_dump_file,
1989 " %d Matching earlyclobber alt:"
1990 " reject--\n",
1991 nop);
1992 reject--;
1994 /* Otherwise we prefer no matching
1995 alternatives because it gives more freedom
1996 in RA. */
1997 else if (operand_reg[nop] == NULL_RTX
1998 || (find_regno_note (curr_insn, REG_DEAD,
1999 REGNO (operand_reg[nop]))
2000 == NULL_RTX))
2002 if (lra_dump_file != NULL)
2003 fprintf
2004 (lra_dump_file,
2005 " %d Matching alt: reject+=2\n",
2006 nop);
2007 reject += 2;
2010 /* If we have to reload this operand and some
2011 previous operand also had to match the same
2012 thing as this operand, we don't know how to do
2013 that. */
2014 if (!match_p || !curr_alt_win[m])
2016 for (i = 0; i < nop; i++)
2017 if (curr_alt_matches[i] == m)
2018 break;
2019 if (i < nop)
2020 break;
2022 else
2023 did_match = true;
2025 /* This can be fixed with reloads if the operand
2026 we are supposed to match can be fixed with
2027 reloads. */
2028 badop = false;
2029 this_alternative = curr_alt[m];
2030 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
2031 winreg = this_alternative != NO_REGS;
2032 break;
2035 case 'g':
2036 if (MEM_P (op)
2037 || general_constant_p (op)
2038 || spilled_pseudo_p (op))
2039 win = true;
2040 cl = GENERAL_REGS;
2041 goto reg;
2043 default:
2044 cn = lookup_constraint (p);
2045 switch (get_constraint_type (cn))
2047 case CT_REGISTER:
2048 cl = reg_class_for_constraint (cn);
2049 if (cl != NO_REGS)
2050 goto reg;
2051 break;
2053 case CT_CONST_INT:
2054 if (CONST_INT_P (op)
2055 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2056 win = true;
2057 break;
2059 case CT_MEMORY:
2060 if (MEM_P (op)
2061 && satisfies_memory_constraint_p (op, cn))
2062 win = true;
2063 else if (spilled_pseudo_p (op))
2064 win = true;
2066 /* If we didn't already win, we can reload constants
2067 via force_const_mem or put the pseudo value into
2068 memory, or make other memory by reloading the
2069 address like for 'o'. */
2070 if (CONST_POOL_OK_P (mode, op)
2071 || MEM_P (op) || REG_P (op)
2072 /* We can restore the equiv insn by a
2073 reload. */
2074 || equiv_substition_p[nop])
2075 badop = false;
2076 constmemok = true;
2077 offmemok = true;
2078 break;
2080 case CT_ADDRESS:
2081 /* If we didn't already win, we can reload the address
2082 into a base register. */
2083 if (satisfies_address_constraint_p (op, cn))
2084 win = true;
2085 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2086 ADDRESS, SCRATCH);
2087 badop = false;
2088 goto reg;
2090 case CT_FIXED_FORM:
2091 if (constraint_satisfied_p (op, cn))
2092 win = true;
2093 break;
2095 case CT_SPECIAL_MEMORY:
2096 if (MEM_P (op)
2097 && satisfies_memory_constraint_p (op, cn))
2098 win = true;
2099 else if (spilled_pseudo_p (op))
2100 win = true;
2101 break;
2103 break;
2105 reg:
2106 this_alternative = reg_class_subunion[this_alternative][cl];
2107 IOR_HARD_REG_SET (this_alternative_set,
2108 reg_class_contents[cl]);
2109 if (costly_p)
2111 this_costly_alternative
2112 = reg_class_subunion[this_costly_alternative][cl];
2113 IOR_HARD_REG_SET (this_costly_alternative_set,
2114 reg_class_contents[cl]);
2116 if (mode == BLKmode)
2117 break;
2118 winreg = true;
2119 if (REG_P (op))
2121 if (hard_regno[nop] >= 0
2122 && in_hard_reg_set_p (this_alternative_set,
2123 mode, hard_regno[nop]))
2124 win = true;
2125 else if (hard_regno[nop] < 0
2126 && in_class_p (op, this_alternative, NULL))
2127 win = true;
2129 break;
2131 if (c != ' ' && c != '\t')
2132 costly_p = c == '*';
2134 while ((p += len), c);
2136 scratch_p = (operand_reg[nop] != NULL_RTX
2137 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2138 /* Record which operands fit this alternative. */
2139 if (win)
2141 this_alternative_win = true;
2142 if (operand_reg[nop] != NULL_RTX)
2144 if (hard_regno[nop] >= 0)
2146 if (in_hard_reg_set_p (this_costly_alternative_set,
2147 mode, hard_regno[nop]))
2149 if (lra_dump_file != NULL)
2150 fprintf (lra_dump_file,
2151 " %d Costly set: reject++\n",
2152 nop);
2153 reject++;
2156 else
2158 /* Prefer won reg to spilled pseudo under other
2159 equal conditions for possibe inheritance. */
2160 if (! scratch_p)
2162 if (lra_dump_file != NULL)
2163 fprintf
2164 (lra_dump_file,
2165 " %d Non pseudo reload: reject++\n",
2166 nop);
2167 reject++;
2169 if (in_class_p (operand_reg[nop],
2170 this_costly_alternative, NULL))
2172 if (lra_dump_file != NULL)
2173 fprintf
2174 (lra_dump_file,
2175 " %d Non pseudo costly reload:"
2176 " reject++\n",
2177 nop);
2178 reject++;
2181 /* We simulate the behavior of old reload here.
2182 Although scratches need hard registers and it
2183 might result in spilling other pseudos, no reload
2184 insns are generated for the scratches. So it
2185 might cost something but probably less than old
2186 reload pass believes. */
2187 if (scratch_p)
2189 if (lra_dump_file != NULL)
2190 fprintf (lra_dump_file,
2191 " %d Scratch win: reject+=2\n",
2192 nop);
2193 reject += 2;
2197 else if (did_match)
2198 this_alternative_match_win = true;
2199 else
2201 int const_to_mem = 0;
2202 bool no_regs_p;
2204 reject += op_reject;
2205 /* Never do output reload of stack pointer. It makes
2206 impossible to do elimination when SP is changed in
2207 RTL. */
2208 if (op == stack_pointer_rtx && ! frame_pointer_needed
2209 && curr_static_id->operand[nop].type != OP_IN)
2210 goto fail;
2212 /* If this alternative asks for a specific reg class, see if there
2213 is at least one allocatable register in that class. */
2214 no_regs_p
2215 = (this_alternative == NO_REGS
2216 || (hard_reg_set_subset_p
2217 (reg_class_contents[this_alternative],
2218 lra_no_alloc_regs)));
2220 /* For asms, verify that the class for this alternative is possible
2221 for the mode that is specified. */
2222 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2224 int i;
2225 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2226 if (HARD_REGNO_MODE_OK (i, mode)
2227 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2228 mode, i))
2229 break;
2230 if (i == FIRST_PSEUDO_REGISTER)
2231 winreg = false;
2234 /* If this operand accepts a register, and if the
2235 register class has at least one allocatable register,
2236 then this operand can be reloaded. */
2237 if (winreg && !no_regs_p)
2238 badop = false;
2240 if (badop)
2242 if (lra_dump_file != NULL)
2243 fprintf (lra_dump_file,
2244 " alt=%d: Bad operand -- refuse\n",
2245 nalt);
2246 goto fail;
2249 /* If not assigned pseudo has a class which a subset of
2250 required reg class, it is a less costly alternative
2251 as the pseudo still can get a hard reg of necessary
2252 class. */
2253 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2254 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2255 && ira_class_subset_p[this_alternative][cl])
2257 if (lra_dump_file != NULL)
2258 fprintf
2259 (lra_dump_file,
2260 " %d Super set class reg: reject-=3\n", nop);
2261 reject -= 3;
2264 this_alternative_offmemok = offmemok;
2265 if (this_costly_alternative != NO_REGS)
2267 if (lra_dump_file != NULL)
2268 fprintf (lra_dump_file,
2269 " %d Costly loser: reject++\n", nop);
2270 reject++;
2272 /* If the operand is dying, has a matching constraint,
2273 and satisfies constraints of the matched operand
2274 which failed to satisfy the own constraints, most probably
2275 the reload for this operand will be gone. */
2276 if (this_alternative_matches >= 0
2277 && !curr_alt_win[this_alternative_matches]
2278 && REG_P (op)
2279 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2280 && (hard_regno[nop] >= 0
2281 ? in_hard_reg_set_p (this_alternative_set,
2282 mode, hard_regno[nop])
2283 : in_class_p (op, this_alternative, NULL)))
2285 if (lra_dump_file != NULL)
2286 fprintf
2287 (lra_dump_file,
2288 " %d Dying matched operand reload: reject++\n",
2289 nop);
2290 reject++;
2292 else
2294 /* Strict_low_part requires to reload the register
2295 not the sub-register. In this case we should
2296 check that a final reload hard reg can hold the
2297 value mode. */
2298 if (curr_static_id->operand[nop].strict_low
2299 && REG_P (op)
2300 && hard_regno[nop] < 0
2301 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2302 && ira_class_hard_regs_num[this_alternative] > 0
2303 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2304 [this_alternative][0],
2305 GET_MODE
2306 (*curr_id->operand_loc[nop])))
2308 if (lra_dump_file != NULL)
2309 fprintf
2310 (lra_dump_file,
2311 " alt=%d: Strict low subreg reload -- refuse\n",
2312 nalt);
2313 goto fail;
2315 losers++;
2317 if (operand_reg[nop] != NULL_RTX
2318 /* Output operands and matched input operands are
2319 not inherited. The following conditions do not
2320 exactly describe the previous statement but they
2321 are pretty close. */
2322 && curr_static_id->operand[nop].type != OP_OUT
2323 && (this_alternative_matches < 0
2324 || curr_static_id->operand[nop].type != OP_IN))
2326 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2327 (operand_reg[nop])]
2328 .last_reload);
2330 /* The value of reload_sum has sense only if we
2331 process insns in their order. It happens only on
2332 the first constraints sub-pass when we do most of
2333 reload work. */
2334 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2335 reload_sum += last_reload - bb_reload_num;
2337 /* If this is a constant that is reloaded into the
2338 desired class by copying it to memory first, count
2339 that as another reload. This is consistent with
2340 other code and is required to avoid choosing another
2341 alternative when the constant is moved into memory.
2342 Note that the test here is precisely the same as in
2343 the code below that calls force_const_mem. */
2344 if (CONST_POOL_OK_P (mode, op)
2345 && ((targetm.preferred_reload_class
2346 (op, this_alternative) == NO_REGS)
2347 || no_input_reloads_p))
2349 const_to_mem = 1;
2350 if (! no_regs_p)
2351 losers++;
2354 /* Alternative loses if it requires a type of reload not
2355 permitted for this insn. We can always reload
2356 objects with a REG_UNUSED note. */
2357 if ((curr_static_id->operand[nop].type != OP_IN
2358 && no_output_reloads_p
2359 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2360 || (curr_static_id->operand[nop].type != OP_OUT
2361 && no_input_reloads_p && ! const_to_mem)
2362 || (this_alternative_matches >= 0
2363 && (no_input_reloads_p
2364 || (no_output_reloads_p
2365 && (curr_static_id->operand
2366 [this_alternative_matches].type != OP_IN)
2367 && ! find_reg_note (curr_insn, REG_UNUSED,
2368 no_subreg_reg_operand
2369 [this_alternative_matches])))))
2371 if (lra_dump_file != NULL)
2372 fprintf
2373 (lra_dump_file,
2374 " alt=%d: No input/otput reload -- refuse\n",
2375 nalt);
2376 goto fail;
2379 /* Alternative loses if it required class pseudo can not
2380 hold value of required mode. Such insns can be
2381 described by insn definitions with mode iterators. */
2382 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2383 && ! hard_reg_set_empty_p (this_alternative_set)
2384 /* It is common practice for constraints to use a
2385 class which does not have actually enough regs to
2386 hold the value (e.g. x86 AREG for mode requiring
2387 more one general reg). Therefore we have 2
2388 conditions to check that the reload pseudo can
2389 not hold the mode value. */
2390 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2391 [this_alternative][0],
2392 GET_MODE (*curr_id->operand_loc[nop]))
2393 /* The above condition is not enough as the first
2394 reg in ira_class_hard_regs can be not aligned for
2395 multi-words mode values. */
2396 && (prohibited_class_reg_set_mode_p
2397 (this_alternative, this_alternative_set,
2398 GET_MODE (*curr_id->operand_loc[nop]))))
2400 if (lra_dump_file != NULL)
2401 fprintf (lra_dump_file,
2402 " alt=%d: reload pseudo for op %d "
2403 " can not hold the mode value -- refuse\n",
2404 nalt, nop);
2405 goto fail;
2408 /* Check strong discouragement of reload of non-constant
2409 into class THIS_ALTERNATIVE. */
2410 if (! CONSTANT_P (op) && ! no_regs_p
2411 && (targetm.preferred_reload_class
2412 (op, this_alternative) == NO_REGS
2413 || (curr_static_id->operand[nop].type == OP_OUT
2414 && (targetm.preferred_output_reload_class
2415 (op, this_alternative) == NO_REGS))))
2417 if (lra_dump_file != NULL)
2418 fprintf (lra_dump_file,
2419 " %d Non-prefered reload: reject+=%d\n",
2420 nop, LRA_MAX_REJECT);
2421 reject += LRA_MAX_REJECT;
2424 if (! (MEM_P (op) && offmemok)
2425 && ! (const_to_mem && constmemok))
2427 /* We prefer to reload pseudos over reloading other
2428 things, since such reloads may be able to be
2429 eliminated later. So bump REJECT in other cases.
2430 Don't do this in the case where we are forcing a
2431 constant into memory and it will then win since
2432 we don't want to have a different alternative
2433 match then. */
2434 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2436 if (lra_dump_file != NULL)
2437 fprintf
2438 (lra_dump_file,
2439 " %d Non-pseudo reload: reject+=2\n",
2440 nop);
2441 reject += 2;
2444 if (! no_regs_p)
2445 reload_nregs
2446 += ira_reg_class_max_nregs[this_alternative][mode];
2448 if (SMALL_REGISTER_CLASS_P (this_alternative))
2450 if (lra_dump_file != NULL)
2451 fprintf
2452 (lra_dump_file,
2453 " %d Small class reload: reject+=%d\n",
2454 nop, LRA_LOSER_COST_FACTOR / 2);
2455 reject += LRA_LOSER_COST_FACTOR / 2;
2459 /* We are trying to spill pseudo into memory. It is
2460 usually more costly than moving to a hard register
2461 although it might takes the same number of
2462 reloads. */
2463 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2465 if (lra_dump_file != NULL)
2466 fprintf
2467 (lra_dump_file,
2468 " %d Spill pseudo into memory: reject+=3\n",
2469 nop);
2470 reject += 3;
2471 if (VECTOR_MODE_P (mode))
2473 /* Spilling vectors into memory is usually more
2474 costly as they contain big values. */
2475 if (lra_dump_file != NULL)
2476 fprintf
2477 (lra_dump_file,
2478 " %d Spill vector pseudo: reject+=2\n",
2479 nop);
2480 reject += 2;
2484 #ifdef SECONDARY_MEMORY_NEEDED
2485 /* If reload requires moving value through secondary
2486 memory, it will need one more insn at least. */
2487 if (this_alternative != NO_REGS
2488 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2489 && ((curr_static_id->operand[nop].type != OP_OUT
2490 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2491 GET_MODE (op)))
2492 || (curr_static_id->operand[nop].type != OP_IN
2493 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2494 GET_MODE (op)))))
2495 losers++;
2496 #endif
2497 /* Input reloads can be inherited more often than output
2498 reloads can be removed, so penalize output
2499 reloads. */
2500 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2502 if (lra_dump_file != NULL)
2503 fprintf
2504 (lra_dump_file,
2505 " %d Non input pseudo reload: reject++\n",
2506 nop);
2507 reject++;
2511 if (early_clobber_p && ! scratch_p)
2513 if (lra_dump_file != NULL)
2514 fprintf (lra_dump_file,
2515 " %d Early clobber: reject++\n", nop);
2516 reject++;
2518 /* ??? We check early clobbers after processing all operands
2519 (see loop below) and there we update the costs more.
2520 Should we update the cost (may be approximately) here
2521 because of early clobber register reloads or it is a rare
2522 or non-important thing to be worth to do it. */
2523 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2524 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2526 if (lra_dump_file != NULL)
2527 fprintf (lra_dump_file,
2528 " alt=%d,overall=%d,losers=%d -- refuse\n",
2529 nalt, overall, losers);
2530 goto fail;
2533 curr_alt[nop] = this_alternative;
2534 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2535 curr_alt_win[nop] = this_alternative_win;
2536 curr_alt_match_win[nop] = this_alternative_match_win;
2537 curr_alt_offmemok[nop] = this_alternative_offmemok;
2538 curr_alt_matches[nop] = this_alternative_matches;
2540 if (this_alternative_matches >= 0
2541 && !did_match && !this_alternative_win)
2542 curr_alt_win[this_alternative_matches] = false;
2544 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2545 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2547 if (curr_insn_set != NULL_RTX && n_operands == 2
2548 /* Prevent processing non-move insns. */
2549 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2550 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2551 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2552 && REG_P (no_subreg_reg_operand[0])
2553 && REG_P (no_subreg_reg_operand[1])
2554 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2555 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2556 || (! curr_alt_win[0] && curr_alt_win[1]
2557 && REG_P (no_subreg_reg_operand[1])
2558 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2559 || (curr_alt_win[0] && ! curr_alt_win[1]
2560 && REG_P (no_subreg_reg_operand[0])
2561 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2562 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2563 no_subreg_reg_operand[1])
2564 || (targetm.preferred_reload_class
2565 (no_subreg_reg_operand[1],
2566 (enum reg_class) curr_alt[1]) != NO_REGS))
2567 /* If it is a result of recent elimination in move
2568 insn we can transform it into an add still by
2569 using this alternative. */
2570 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2572 /* We have a move insn and a new reload insn will be similar
2573 to the current insn. We should avoid such situation as it
2574 results in LRA cycling. */
2575 overall += LRA_MAX_REJECT;
2577 ok_p = true;
2578 curr_alt_dont_inherit_ops_num = 0;
2579 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2581 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2582 HARD_REG_SET temp_set;
2584 i = early_clobbered_nops[nop];
2585 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2586 || hard_regno[i] < 0)
2587 continue;
2588 lra_assert (operand_reg[i] != NULL_RTX);
2589 clobbered_hard_regno = hard_regno[i];
2590 CLEAR_HARD_REG_SET (temp_set);
2591 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2592 first_conflict_j = last_conflict_j = -1;
2593 for (j = 0; j < n_operands; j++)
2594 if (j == i
2595 /* We don't want process insides of match_operator and
2596 match_parallel because otherwise we would process
2597 their operands once again generating a wrong
2598 code. */
2599 || curr_static_id->operand[j].is_operator)
2600 continue;
2601 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2602 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2603 continue;
2604 /* If we don't reload j-th operand, check conflicts. */
2605 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2606 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2608 if (first_conflict_j < 0)
2609 first_conflict_j = j;
2610 last_conflict_j = j;
2612 if (last_conflict_j < 0)
2613 continue;
2614 /* If earlyclobber operand conflicts with another
2615 non-matching operand which is actually the same register
2616 as the earlyclobber operand, it is better to reload the
2617 another operand as an operand matching the earlyclobber
2618 operand can be also the same. */
2619 if (first_conflict_j == last_conflict_j
2620 && operand_reg[last_conflict_j] != NULL_RTX
2621 && ! curr_alt_match_win[last_conflict_j]
2622 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2624 curr_alt_win[last_conflict_j] = false;
2625 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2626 = last_conflict_j;
2627 losers++;
2628 /* Early clobber was already reflected in REJECT. */
2629 lra_assert (reject > 0);
2630 if (lra_dump_file != NULL)
2631 fprintf
2632 (lra_dump_file,
2633 " %d Conflict early clobber reload: reject--\n",
2635 reject--;
2636 overall += LRA_LOSER_COST_FACTOR - 1;
2638 else
2640 /* We need to reload early clobbered register and the
2641 matched registers. */
2642 for (j = 0; j < n_operands; j++)
2643 if (curr_alt_matches[j] == i)
2645 curr_alt_match_win[j] = false;
2646 losers++;
2647 overall += LRA_LOSER_COST_FACTOR;
2649 if (! curr_alt_match_win[i])
2650 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2651 else
2653 /* Remember pseudos used for match reloads are never
2654 inherited. */
2655 lra_assert (curr_alt_matches[i] >= 0);
2656 curr_alt_win[curr_alt_matches[i]] = false;
2658 curr_alt_win[i] = curr_alt_match_win[i] = false;
2659 losers++;
2660 /* Early clobber was already reflected in REJECT. */
2661 lra_assert (reject > 0);
2662 if (lra_dump_file != NULL)
2663 fprintf
2664 (lra_dump_file,
2665 " %d Matched conflict early clobber reloads:"
2666 "reject--\n",
2668 reject--;
2669 overall += LRA_LOSER_COST_FACTOR - 1;
2672 if (lra_dump_file != NULL)
2673 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2674 nalt, overall, losers, reload_nregs);
2676 /* If this alternative can be made to work by reloading, and it
2677 needs less reloading than the others checked so far, record
2678 it as the chosen goal for reloading. */
2679 if ((best_losers != 0 && losers == 0)
2680 || (((best_losers == 0 && losers == 0)
2681 || (best_losers != 0 && losers != 0))
2682 && (best_overall > overall
2683 || (best_overall == overall
2684 /* If the cost of the reloads is the same,
2685 prefer alternative which requires minimal
2686 number of reload regs. */
2687 && (reload_nregs < best_reload_nregs
2688 || (reload_nregs == best_reload_nregs
2689 && (best_reload_sum < reload_sum
2690 || (best_reload_sum == reload_sum
2691 && nalt < goal_alt_number))))))))
2693 for (nop = 0; nop < n_operands; nop++)
2695 goal_alt_win[nop] = curr_alt_win[nop];
2696 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2697 goal_alt_matches[nop] = curr_alt_matches[nop];
2698 goal_alt[nop] = curr_alt[nop];
2699 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2701 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2702 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2703 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2704 goal_alt_swapped = curr_swapped;
2705 best_overall = overall;
2706 best_losers = losers;
2707 best_reload_nregs = reload_nregs;
2708 best_reload_sum = reload_sum;
2709 goal_alt_number = nalt;
2711 if (losers == 0)
2712 /* Everything is satisfied. Do not process alternatives
2713 anymore. */
2714 break;
2715 fail:
2718 return ok_p;
2721 /* Make reload base reg from address AD. */
2722 static rtx
2723 base_to_reg (struct address_info *ad)
2725 enum reg_class cl;
2726 int code = -1;
2727 rtx new_inner = NULL_RTX;
2728 rtx new_reg = NULL_RTX;
2729 rtx_insn *insn;
2730 rtx_insn *last_insn = get_last_insn();
2732 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2733 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2734 get_index_code (ad));
2735 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2736 cl, "base");
2737 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2738 ad->disp_term == NULL
2739 ? gen_int_mode (0, ad->mode)
2740 : *ad->disp_term);
2741 if (!valid_address_p (ad->mode, new_inner, ad->as))
2742 return NULL_RTX;
2743 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base_term));
2744 code = recog_memoized (insn);
2745 if (code < 0)
2747 delete_insns_since (last_insn);
2748 return NULL_RTX;
2751 return new_inner;
2754 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2755 static rtx
2756 base_plus_disp_to_reg (struct address_info *ad)
2758 enum reg_class cl;
2759 rtx new_reg;
2761 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2762 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2763 get_index_code (ad));
2764 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2765 cl, "base + disp");
2766 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2767 return new_reg;
2770 /* Make reload of index part of address AD. Return the new
2771 pseudo. */
2772 static rtx
2773 index_part_to_reg (struct address_info *ad)
2775 rtx new_reg;
2777 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2778 INDEX_REG_CLASS, "index term");
2779 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2780 GEN_INT (get_index_scale (ad)), new_reg, 1);
2781 return new_reg;
2784 /* Return true if we can add a displacement to address AD, even if that
2785 makes the address invalid. The fix-up code requires any new address
2786 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2787 static bool
2788 can_add_disp_p (struct address_info *ad)
2790 return (!ad->autoinc_p
2791 && ad->segment == NULL
2792 && ad->base == ad->base_term
2793 && ad->disp == ad->disp_term);
2796 /* Make equiv substitution in address AD. Return true if a substitution
2797 was made. */
2798 static bool
2799 equiv_address_substitution (struct address_info *ad)
2801 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2802 HOST_WIDE_INT disp, scale;
2803 bool change_p;
2805 base_term = strip_subreg (ad->base_term);
2806 if (base_term == NULL)
2807 base_reg = new_base_reg = NULL_RTX;
2808 else
2810 base_reg = *base_term;
2811 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2813 index_term = strip_subreg (ad->index_term);
2814 if (index_term == NULL)
2815 index_reg = new_index_reg = NULL_RTX;
2816 else
2818 index_reg = *index_term;
2819 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2821 if (base_reg == new_base_reg && index_reg == new_index_reg)
2822 return false;
2823 disp = 0;
2824 change_p = false;
2825 if (lra_dump_file != NULL)
2827 fprintf (lra_dump_file, "Changing address in insn %d ",
2828 INSN_UID (curr_insn));
2829 dump_value_slim (lra_dump_file, *ad->outer, 1);
2831 if (base_reg != new_base_reg)
2833 if (REG_P (new_base_reg))
2835 *base_term = new_base_reg;
2836 change_p = true;
2838 else if (GET_CODE (new_base_reg) == PLUS
2839 && REG_P (XEXP (new_base_reg, 0))
2840 && CONST_INT_P (XEXP (new_base_reg, 1))
2841 && can_add_disp_p (ad))
2843 disp += INTVAL (XEXP (new_base_reg, 1));
2844 *base_term = XEXP (new_base_reg, 0);
2845 change_p = true;
2847 if (ad->base_term2 != NULL)
2848 *ad->base_term2 = *ad->base_term;
2850 if (index_reg != new_index_reg)
2852 if (REG_P (new_index_reg))
2854 *index_term = new_index_reg;
2855 change_p = true;
2857 else if (GET_CODE (new_index_reg) == PLUS
2858 && REG_P (XEXP (new_index_reg, 0))
2859 && CONST_INT_P (XEXP (new_index_reg, 1))
2860 && can_add_disp_p (ad)
2861 && (scale = get_index_scale (ad)))
2863 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2864 *index_term = XEXP (new_index_reg, 0);
2865 change_p = true;
2868 if (disp != 0)
2870 if (ad->disp != NULL)
2871 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2872 else
2874 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2875 update_address (ad);
2877 change_p = true;
2879 if (lra_dump_file != NULL)
2881 if (! change_p)
2882 fprintf (lra_dump_file, " -- no change\n");
2883 else
2885 fprintf (lra_dump_file, " on equiv ");
2886 dump_value_slim (lra_dump_file, *ad->outer, 1);
2887 fprintf (lra_dump_file, "\n");
2890 return change_p;
2893 /* Major function to make reloads for an address in operand NOP or
2894 check its correctness (If CHECK_ONLY_P is true). The supported
2895 cases are:
2897 1) an address that existed before LRA started, at which point it
2898 must have been valid. These addresses are subject to elimination
2899 and may have become invalid due to the elimination offset being out
2900 of range.
2902 2) an address created by forcing a constant to memory
2903 (force_const_to_mem). The initial form of these addresses might
2904 not be valid, and it is this function's job to make them valid.
2906 3) a frame address formed from a register and a (possibly zero)
2907 constant offset. As above, these addresses might not be valid and
2908 this function must make them so.
2910 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2911 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2912 address. Return true for any RTL change.
2914 The function is a helper function which does not produce all
2915 transformations (when CHECK_ONLY_P is false) which can be
2916 necessary. It does just basic steps. To do all necessary
2917 transformations use function process_address. */
2918 static bool
2919 process_address_1 (int nop, bool check_only_p,
2920 rtx_insn **before, rtx_insn **after)
2922 struct address_info ad;
2923 rtx new_reg;
2924 HOST_WIDE_INT scale;
2925 rtx op = *curr_id->operand_loc[nop];
2926 const char *constraint = curr_static_id->operand[nop].constraint;
2927 enum constraint_num cn = lookup_constraint (constraint);
2928 bool change_p = false;
2930 if (MEM_P (op)
2931 && GET_MODE (op) == BLKmode
2932 && GET_CODE (XEXP (op, 0)) == SCRATCH)
2933 return false;
2935 if (insn_extra_address_constraint (cn))
2936 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2937 else if (MEM_P (op))
2938 decompose_mem_address (&ad, op);
2939 else if (GET_CODE (op) == SUBREG
2940 && MEM_P (SUBREG_REG (op)))
2941 decompose_mem_address (&ad, SUBREG_REG (op));
2942 else
2943 return false;
2944 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
2945 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
2946 when INDEX_REG_CLASS is a single register class. */
2947 if (ad.base_term != NULL
2948 && ad.index_term != NULL
2949 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
2950 && REG_P (*ad.base_term)
2951 && REG_P (*ad.index_term)
2952 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
2953 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
2955 std::swap (ad.base, ad.index);
2956 std::swap (ad.base_term, ad.index_term);
2958 if (! check_only_p)
2959 change_p = equiv_address_substitution (&ad);
2960 if (ad.base_term != NULL
2961 && (process_addr_reg
2962 (ad.base_term, check_only_p, before,
2963 (ad.autoinc_p
2964 && !(REG_P (*ad.base_term)
2965 && find_regno_note (curr_insn, REG_DEAD,
2966 REGNO (*ad.base_term)) != NULL_RTX)
2967 ? after : NULL),
2968 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2969 get_index_code (&ad)))))
2971 change_p = true;
2972 if (ad.base_term2 != NULL)
2973 *ad.base_term2 = *ad.base_term;
2975 if (ad.index_term != NULL
2976 && process_addr_reg (ad.index_term, check_only_p,
2977 before, NULL, INDEX_REG_CLASS))
2978 change_p = true;
2980 /* Target hooks sometimes don't treat extra-constraint addresses as
2981 legitimate address_operands, so handle them specially. */
2982 if (insn_extra_address_constraint (cn)
2983 && satisfies_address_constraint_p (&ad, cn))
2984 return change_p;
2986 if (check_only_p)
2987 return change_p;
2989 /* There are three cases where the shape of *AD.INNER may now be invalid:
2991 1) the original address was valid, but either elimination or
2992 equiv_address_substitution was applied and that made
2993 the address invalid.
2995 2) the address is an invalid symbolic address created by
2996 force_const_to_mem.
2998 3) the address is a frame address with an invalid offset.
3000 4) the address is a frame address with an invalid base.
3002 All these cases involve a non-autoinc address, so there is no
3003 point revalidating other types. */
3004 if (ad.autoinc_p || valid_address_p (&ad))
3005 return change_p;
3007 /* Any index existed before LRA started, so we can assume that the
3008 presence and shape of the index is valid. */
3009 push_to_sequence (*before);
3010 lra_assert (ad.disp == ad.disp_term);
3011 if (ad.base == NULL)
3013 if (ad.index == NULL)
3015 rtx_insn *insn;
3016 rtx_insn *last = get_last_insn ();
3017 int code = -1;
3018 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3019 SCRATCH, SCRATCH);
3020 rtx addr = *ad.inner;
3022 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3023 if (HAVE_lo_sum)
3025 /* addr => lo_sum (new_base, addr), case (2) above. */
3026 insn = emit_insn (gen_rtx_SET
3027 (new_reg,
3028 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
3029 code = recog_memoized (insn);
3030 if (code >= 0)
3032 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
3033 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
3035 /* Try to put lo_sum into register. */
3036 insn = emit_insn (gen_rtx_SET
3037 (new_reg,
3038 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
3039 code = recog_memoized (insn);
3040 if (code >= 0)
3042 *ad.inner = new_reg;
3043 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
3045 *ad.inner = addr;
3046 code = -1;
3052 if (code < 0)
3053 delete_insns_since (last);
3056 if (code < 0)
3058 /* addr => new_base, case (2) above. */
3059 lra_emit_move (new_reg, addr);
3061 for (insn = last == NULL_RTX ? get_insns () : NEXT_INSN (last);
3062 insn != NULL_RTX;
3063 insn = NEXT_INSN (insn))
3064 if (recog_memoized (insn) < 0)
3065 break;
3066 if (insn != NULL_RTX)
3068 /* Do nothing if we cannot generate right insns.
3069 This is analogous to reload pass behavior. */
3070 delete_insns_since (last);
3071 end_sequence ();
3072 return false;
3074 *ad.inner = new_reg;
3077 else
3079 /* index * scale + disp => new base + index * scale,
3080 case (1) above. */
3081 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3082 GET_CODE (*ad.index));
3084 lra_assert (INDEX_REG_CLASS != NO_REGS);
3085 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3086 lra_emit_move (new_reg, *ad.disp);
3087 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3088 new_reg, *ad.index);
3091 else if (ad.index == NULL)
3093 int regno;
3094 enum reg_class cl;
3095 rtx set;
3096 rtx_insn *insns, *last_insn;
3097 /* Try to reload base into register only if the base is invalid
3098 for the address but with valid offset, case (4) above. */
3099 start_sequence ();
3100 new_reg = base_to_reg (&ad);
3102 /* base + disp => new base, cases (1) and (3) above. */
3103 /* Another option would be to reload the displacement into an
3104 index register. However, postreload has code to optimize
3105 address reloads that have the same base and different
3106 displacements, so reloading into an index register would
3107 not necessarily be a win. */
3108 if (new_reg == NULL_RTX)
3109 new_reg = base_plus_disp_to_reg (&ad);
3110 insns = get_insns ();
3111 last_insn = get_last_insn ();
3112 /* If we generated at least two insns, try last insn source as
3113 an address. If we succeed, we generate one less insn. */
3114 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3115 && GET_CODE (SET_SRC (set)) == PLUS
3116 && REG_P (XEXP (SET_SRC (set), 0))
3117 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3119 *ad.inner = SET_SRC (set);
3120 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3122 *ad.base_term = XEXP (SET_SRC (set), 0);
3123 *ad.disp_term = XEXP (SET_SRC (set), 1);
3124 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3125 get_index_code (&ad));
3126 regno = REGNO (*ad.base_term);
3127 if (regno >= FIRST_PSEUDO_REGISTER
3128 && cl != lra_get_allocno_class (regno))
3129 lra_change_class (regno, cl, " Change to", true);
3130 new_reg = SET_SRC (set);
3131 delete_insns_since (PREV_INSN (last_insn));
3134 /* Try if target can split displacement into legitimite new disp
3135 and offset. If it's the case, we replace the last insn with
3136 insns for base + offset => new_reg and set new_reg + new disp
3137 to *ad.inner. */
3138 last_insn = get_last_insn ();
3139 if ((set = single_set (last_insn)) != NULL_RTX
3140 && GET_CODE (SET_SRC (set)) == PLUS
3141 && REG_P (XEXP (SET_SRC (set), 0))
3142 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3143 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3145 rtx addend, disp = XEXP (SET_SRC (set), 1);
3146 if (targetm.legitimize_address_displacement (&disp, &addend,
3147 ad.mode))
3149 rtx_insn *new_insns;
3150 start_sequence ();
3151 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3152 new_insns = get_insns ();
3153 end_sequence ();
3154 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3155 delete_insns_since (PREV_INSN (last_insn));
3156 add_insn (new_insns);
3157 insns = get_insns ();
3160 end_sequence ();
3161 emit_insn (insns);
3162 *ad.inner = new_reg;
3164 else if (ad.disp_term != NULL)
3166 /* base + scale * index + disp => new base + scale * index,
3167 case (1) above. */
3168 new_reg = base_plus_disp_to_reg (&ad);
3169 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3170 new_reg, *ad.index);
3172 else if ((scale = get_index_scale (&ad)) == 1)
3174 /* The last transformation to one reg will be made in
3175 curr_insn_transform function. */
3176 end_sequence ();
3177 return false;
3179 else if (scale != 0)
3181 /* base + scale * index => base + new_reg,
3182 case (1) above.
3183 Index part of address may become invalid. For example, we
3184 changed pseudo on the equivalent memory and a subreg of the
3185 pseudo onto the memory of different mode for which the scale is
3186 prohibitted. */
3187 new_reg = index_part_to_reg (&ad);
3188 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3189 *ad.base_term, new_reg);
3191 else
3193 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3194 SCRATCH, SCRATCH);
3195 rtx addr = *ad.inner;
3197 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3198 /* addr => new_base. */
3199 lra_emit_move (new_reg, addr);
3200 *ad.inner = new_reg;
3202 *before = get_insns ();
3203 end_sequence ();
3204 return true;
3207 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3208 Use process_address_1 as a helper function. Return true for any
3209 RTL changes.
3211 If CHECK_ONLY_P is true, just check address correctness. Return
3212 false if the address correct. */
3213 static bool
3214 process_address (int nop, bool check_only_p,
3215 rtx_insn **before, rtx_insn **after)
3217 bool res = false;
3219 while (process_address_1 (nop, check_only_p, before, after))
3221 if (check_only_p)
3222 return true;
3223 res = true;
3225 return res;
3228 /* Emit insns to reload VALUE into a new register. VALUE is an
3229 auto-increment or auto-decrement RTX whose operand is a register or
3230 memory location; so reloading involves incrementing that location.
3231 IN is either identical to VALUE, or some cheaper place to reload
3232 value being incremented/decremented from.
3234 INC_AMOUNT is the number to increment or decrement by (always
3235 positive and ignored for POST_MODIFY/PRE_MODIFY).
3237 Return pseudo containing the result. */
3238 static rtx
3239 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3241 /* REG or MEM to be copied and incremented. */
3242 rtx incloc = XEXP (value, 0);
3243 /* Nonzero if increment after copying. */
3244 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3245 || GET_CODE (value) == POST_MODIFY);
3246 rtx_insn *last;
3247 rtx inc;
3248 rtx_insn *add_insn;
3249 int code;
3250 rtx real_in = in == value ? incloc : in;
3251 rtx result;
3252 bool plus_p = true;
3254 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3256 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3257 || GET_CODE (XEXP (value, 1)) == MINUS);
3258 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3259 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3260 inc = XEXP (XEXP (value, 1), 1);
3262 else
3264 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3265 inc_amount = -inc_amount;
3267 inc = GEN_INT (inc_amount);
3270 if (! post && REG_P (incloc))
3271 result = incloc;
3272 else
3273 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3274 "INC/DEC result");
3276 if (real_in != result)
3278 /* First copy the location to the result register. */
3279 lra_assert (REG_P (result));
3280 emit_insn (gen_move_insn (result, real_in));
3283 /* We suppose that there are insns to add/sub with the constant
3284 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3285 old reload worked with this assumption. If the assumption
3286 becomes wrong, we should use approach in function
3287 base_plus_disp_to_reg. */
3288 if (in == value)
3290 /* See if we can directly increment INCLOC. */
3291 last = get_last_insn ();
3292 add_insn = emit_insn (plus_p
3293 ? gen_add2_insn (incloc, inc)
3294 : gen_sub2_insn (incloc, inc));
3296 code = recog_memoized (add_insn);
3297 if (code >= 0)
3299 if (! post && result != incloc)
3300 emit_insn (gen_move_insn (result, incloc));
3301 return result;
3303 delete_insns_since (last);
3306 /* If couldn't do the increment directly, must increment in RESULT.
3307 The way we do this depends on whether this is pre- or
3308 post-increment. For pre-increment, copy INCLOC to the reload
3309 register, increment it there, then save back. */
3310 if (! post)
3312 if (real_in != result)
3313 emit_insn (gen_move_insn (result, real_in));
3314 if (plus_p)
3315 emit_insn (gen_add2_insn (result, inc));
3316 else
3317 emit_insn (gen_sub2_insn (result, inc));
3318 if (result != incloc)
3319 emit_insn (gen_move_insn (incloc, result));
3321 else
3323 /* Post-increment.
3325 Because this might be a jump insn or a compare, and because
3326 RESULT may not be available after the insn in an input
3327 reload, we must do the incrementing before the insn being
3328 reloaded for.
3330 We have already copied IN to RESULT. Increment the copy in
3331 RESULT, save that back, then decrement RESULT so it has
3332 the original value. */
3333 if (plus_p)
3334 emit_insn (gen_add2_insn (result, inc));
3335 else
3336 emit_insn (gen_sub2_insn (result, inc));
3337 emit_insn (gen_move_insn (incloc, result));
3338 /* Restore non-modified value for the result. We prefer this
3339 way because it does not require an additional hard
3340 register. */
3341 if (plus_p)
3343 if (CONST_INT_P (inc))
3344 emit_insn (gen_add2_insn (result,
3345 gen_int_mode (-INTVAL (inc),
3346 GET_MODE (result))));
3347 else
3348 emit_insn (gen_sub2_insn (result, inc));
3350 else
3351 emit_insn (gen_add2_insn (result, inc));
3353 return result;
3356 /* Return true if the current move insn does not need processing as we
3357 already know that it satisfies its constraints. */
3358 static bool
3359 simple_move_p (void)
3361 rtx dest, src;
3362 enum reg_class dclass, sclass;
3364 lra_assert (curr_insn_set != NULL_RTX);
3365 dest = SET_DEST (curr_insn_set);
3366 src = SET_SRC (curr_insn_set);
3367 return ((dclass = get_op_class (dest)) != NO_REGS
3368 && (sclass = get_op_class (src)) != NO_REGS
3369 /* The backend guarantees that register moves of cost 2
3370 never need reloads. */
3371 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3374 /* Swap operands NOP and NOP + 1. */
3375 static inline void
3376 swap_operands (int nop)
3378 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3379 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3380 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3381 std::swap (equiv_substition_p[nop], equiv_substition_p[nop + 1]);
3382 /* Swap the duplicates too. */
3383 lra_update_dup (curr_id, nop);
3384 lra_update_dup (curr_id, nop + 1);
3387 /* Main entry point of the constraint code: search the body of the
3388 current insn to choose the best alternative. It is mimicking insn
3389 alternative cost calculation model of former reload pass. That is
3390 because machine descriptions were written to use this model. This
3391 model can be changed in future. Make commutative operand exchange
3392 if it is chosen.
3394 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3395 constraints. Return true if any change happened during function
3396 call.
3398 If CHECK_ONLY_P is true then don't do any transformation. Just
3399 check that the insn satisfies all constraints. If the insn does
3400 not satisfy any constraint, return true. */
3401 static bool
3402 curr_insn_transform (bool check_only_p)
3404 int i, j, k;
3405 int n_operands;
3406 int n_alternatives;
3407 int commutative;
3408 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3409 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3410 rtx_insn *before, *after;
3411 bool alt_p = false;
3412 /* Flag that the insn has been changed through a transformation. */
3413 bool change_p;
3414 bool sec_mem_p;
3415 #ifdef SECONDARY_MEMORY_NEEDED
3416 bool use_sec_mem_p;
3417 #endif
3418 int max_regno_before;
3419 int reused_alternative_num;
3421 curr_insn_set = single_set (curr_insn);
3422 if (curr_insn_set != NULL_RTX && simple_move_p ())
3423 return false;
3425 no_input_reloads_p = no_output_reloads_p = false;
3426 goal_alt_number = -1;
3427 change_p = sec_mem_p = false;
3428 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3429 reloads; neither are insns that SET cc0. Insns that use CC0 are
3430 not allowed to have any input reloads. */
3431 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3432 no_output_reloads_p = true;
3434 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3435 no_input_reloads_p = true;
3436 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3437 no_output_reloads_p = true;
3439 n_operands = curr_static_id->n_operands;
3440 n_alternatives = curr_static_id->n_alternatives;
3442 /* Just return "no reloads" if insn has no operands with
3443 constraints. */
3444 if (n_operands == 0 || n_alternatives == 0)
3445 return false;
3447 max_regno_before = max_reg_num ();
3449 for (i = 0; i < n_operands; i++)
3451 goal_alt_matched[i][0] = -1;
3452 goal_alt_matches[i] = -1;
3455 commutative = curr_static_id->commutative;
3457 /* Now see what we need for pseudos that didn't get hard regs or got
3458 the wrong kind of hard reg. For this, we must consider all the
3459 operands together against the register constraints. */
3461 best_losers = best_overall = INT_MAX;
3462 best_reload_sum = 0;
3464 curr_swapped = false;
3465 goal_alt_swapped = false;
3467 if (! check_only_p)
3468 /* Make equivalence substitution and memory subreg elimination
3469 before address processing because an address legitimacy can
3470 depend on memory mode. */
3471 for (i = 0; i < n_operands; i++)
3473 rtx op, subst, old;
3474 bool op_change_p = false;
3476 if (curr_static_id->operand[i].is_operator)
3477 continue;
3479 old = op = *curr_id->operand_loc[i];
3480 if (GET_CODE (old) == SUBREG)
3481 old = SUBREG_REG (old);
3482 subst = get_equiv_with_elimination (old, curr_insn);
3483 original_subreg_reg_mode[i] = VOIDmode;
3484 equiv_substition_p[i] = false;
3485 if (subst != old)
3487 equiv_substition_p[i] = true;
3488 subst = copy_rtx (subst);
3489 lra_assert (REG_P (old));
3490 if (GET_CODE (op) != SUBREG)
3491 *curr_id->operand_loc[i] = subst;
3492 else
3494 SUBREG_REG (op) = subst;
3495 if (GET_MODE (subst) == VOIDmode)
3496 original_subreg_reg_mode[i] = GET_MODE (old);
3498 if (lra_dump_file != NULL)
3500 fprintf (lra_dump_file,
3501 "Changing pseudo %d in operand %i of insn %u on equiv ",
3502 REGNO (old), i, INSN_UID (curr_insn));
3503 dump_value_slim (lra_dump_file, subst, 1);
3504 fprintf (lra_dump_file, "\n");
3506 op_change_p = change_p = true;
3508 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3510 change_p = true;
3511 lra_update_dup (curr_id, i);
3515 /* Reload address registers and displacements. We do it before
3516 finding an alternative because of memory constraints. */
3517 before = after = NULL;
3518 for (i = 0; i < n_operands; i++)
3519 if (! curr_static_id->operand[i].is_operator
3520 && process_address (i, check_only_p, &before, &after))
3522 if (check_only_p)
3523 return true;
3524 change_p = true;
3525 lra_update_dup (curr_id, i);
3528 if (change_p)
3529 /* If we've changed the instruction then any alternative that
3530 we chose previously may no longer be valid. */
3531 lra_set_used_insn_alternative (curr_insn, -1);
3533 if (! check_only_p && curr_insn_set != NULL_RTX
3534 && check_and_process_move (&change_p, &sec_mem_p))
3535 return change_p;
3537 try_swapped:
3539 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3540 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3541 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3542 reused_alternative_num, INSN_UID (curr_insn));
3544 if (process_alt_operands (reused_alternative_num))
3545 alt_p = true;
3547 if (check_only_p)
3548 return ! alt_p || best_losers != 0;
3550 /* If insn is commutative (it's safe to exchange a certain pair of
3551 operands) then we need to try each alternative twice, the second
3552 time matching those two operands as if we had exchanged them. To
3553 do this, really exchange them in operands.
3555 If we have just tried the alternatives the second time, return
3556 operands to normal and drop through. */
3558 if (reused_alternative_num < 0 && commutative >= 0)
3560 curr_swapped = !curr_swapped;
3561 if (curr_swapped)
3563 swap_operands (commutative);
3564 goto try_swapped;
3566 else
3567 swap_operands (commutative);
3570 if (! alt_p && ! sec_mem_p)
3572 /* No alternative works with reloads?? */
3573 if (INSN_CODE (curr_insn) >= 0)
3574 fatal_insn ("unable to generate reloads for:", curr_insn);
3575 error_for_asm (curr_insn,
3576 "inconsistent operand constraints in an %<asm%>");
3577 /* Avoid further trouble with this insn. */
3578 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3579 lra_invalidate_insn_data (curr_insn);
3580 return true;
3583 /* If the best alternative is with operands 1 and 2 swapped, swap
3584 them. Update the operand numbers of any reloads already
3585 pushed. */
3587 if (goal_alt_swapped)
3589 if (lra_dump_file != NULL)
3590 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3591 INSN_UID (curr_insn));
3593 /* Swap the duplicates too. */
3594 swap_operands (commutative);
3595 change_p = true;
3598 #ifdef SECONDARY_MEMORY_NEEDED
3599 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3600 too conservatively. So we use the secondary memory only if there
3601 is no any alternative without reloads. */
3602 use_sec_mem_p = false;
3603 if (! alt_p)
3604 use_sec_mem_p = true;
3605 else if (sec_mem_p)
3607 for (i = 0; i < n_operands; i++)
3608 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3609 break;
3610 use_sec_mem_p = i < n_operands;
3613 if (use_sec_mem_p)
3615 int in = -1, out = -1;
3616 rtx new_reg, src, dest, rld;
3617 machine_mode sec_mode, rld_mode;
3619 lra_assert (curr_insn_set != NULL_RTX && sec_mem_p);
3620 dest = SET_DEST (curr_insn_set);
3621 src = SET_SRC (curr_insn_set);
3622 for (i = 0; i < n_operands; i++)
3623 if (*curr_id->operand_loc[i] == dest)
3624 out = i;
3625 else if (*curr_id->operand_loc[i] == src)
3626 in = i;
3627 for (i = 0; i < curr_static_id->n_dups; i++)
3628 if (out < 0 && *curr_id->dup_loc[i] == dest)
3629 out = curr_static_id->dup_num[i];
3630 else if (in < 0 && *curr_id->dup_loc[i] == src)
3631 in = curr_static_id->dup_num[i];
3632 lra_assert (out >= 0 && in >= 0
3633 && curr_static_id->operand[out].type == OP_OUT
3634 && curr_static_id->operand[in].type == OP_IN);
3635 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3636 ? dest : src);
3637 rld_mode = GET_MODE (rld);
3638 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3639 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3640 #else
3641 sec_mode = rld_mode;
3642 #endif
3643 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3644 NO_REGS, "secondary");
3645 /* If the mode is changed, it should be wider. */
3646 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3647 if (sec_mode != rld_mode)
3649 /* If the target says specifically to use another mode for
3650 secondary memory moves we can not reuse the original
3651 insn. */
3652 after = emit_spill_move (false, new_reg, dest);
3653 lra_process_new_insns (curr_insn, NULL, after,
3654 "Inserting the sec. move");
3655 /* We may have non null BEFORE here (e.g. after address
3656 processing. */
3657 push_to_sequence (before);
3658 before = emit_spill_move (true, new_reg, src);
3659 emit_insn (before);
3660 before = get_insns ();
3661 end_sequence ();
3662 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3663 lra_set_insn_deleted (curr_insn);
3665 else if (dest == rld)
3667 *curr_id->operand_loc[out] = new_reg;
3668 lra_update_dup (curr_id, out);
3669 after = emit_spill_move (false, new_reg, dest);
3670 lra_process_new_insns (curr_insn, NULL, after,
3671 "Inserting the sec. move");
3673 else
3675 *curr_id->operand_loc[in] = new_reg;
3676 lra_update_dup (curr_id, in);
3677 /* See comments above. */
3678 push_to_sequence (before);
3679 before = emit_spill_move (true, new_reg, src);
3680 emit_insn (before);
3681 before = get_insns ();
3682 end_sequence ();
3683 lra_process_new_insns (curr_insn, before, NULL,
3684 "Inserting the sec. move");
3686 lra_update_insn_regno_info (curr_insn);
3687 return true;
3689 #endif
3691 lra_assert (goal_alt_number >= 0);
3692 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3694 if (lra_dump_file != NULL)
3696 const char *p;
3698 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3699 goal_alt_number, INSN_UID (curr_insn));
3700 for (i = 0; i < n_operands; i++)
3702 p = (curr_static_id->operand_alternative
3703 [goal_alt_number * n_operands + i].constraint);
3704 if (*p == '\0')
3705 continue;
3706 fprintf (lra_dump_file, " (%d) ", i);
3707 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3708 fputc (*p, lra_dump_file);
3710 if (INSN_CODE (curr_insn) >= 0
3711 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3712 fprintf (lra_dump_file, " {%s}", p);
3713 if (curr_id->sp_offset != 0)
3714 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3715 curr_id->sp_offset);
3716 fprintf (lra_dump_file, "\n");
3719 /* Right now, for any pair of operands I and J that are required to
3720 match, with J < I, goal_alt_matches[I] is J. Add I to
3721 goal_alt_matched[J]. */
3723 for (i = 0; i < n_operands; i++)
3724 if ((j = goal_alt_matches[i]) >= 0)
3726 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3728 /* We allow matching one output operand and several input
3729 operands. */
3730 lra_assert (k == 0
3731 || (curr_static_id->operand[j].type == OP_OUT
3732 && curr_static_id->operand[i].type == OP_IN
3733 && (curr_static_id->operand
3734 [goal_alt_matched[j][0]].type == OP_IN)));
3735 goal_alt_matched[j][k] = i;
3736 goal_alt_matched[j][k + 1] = -1;
3739 for (i = 0; i < n_operands; i++)
3740 goal_alt_win[i] |= goal_alt_match_win[i];
3742 /* Any constants that aren't allowed and can't be reloaded into
3743 registers are here changed into memory references. */
3744 for (i = 0; i < n_operands; i++)
3745 if (goal_alt_win[i])
3747 int regno;
3748 enum reg_class new_class;
3749 rtx reg = *curr_id->operand_loc[i];
3751 if (GET_CODE (reg) == SUBREG)
3752 reg = SUBREG_REG (reg);
3754 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3756 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3758 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3760 lra_assert (ok_p);
3761 lra_change_class (regno, new_class, " Change to", true);
3765 else
3767 const char *constraint;
3768 char c;
3769 rtx op = *curr_id->operand_loc[i];
3770 rtx subreg = NULL_RTX;
3771 machine_mode mode = curr_operand_mode[i];
3773 if (GET_CODE (op) == SUBREG)
3775 subreg = op;
3776 op = SUBREG_REG (op);
3777 mode = GET_MODE (op);
3780 if (CONST_POOL_OK_P (mode, op)
3781 && ((targetm.preferred_reload_class
3782 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3783 || no_input_reloads_p))
3785 rtx tem = force_const_mem (mode, op);
3787 change_p = true;
3788 if (subreg != NULL_RTX)
3789 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3791 *curr_id->operand_loc[i] = tem;
3792 lra_update_dup (curr_id, i);
3793 process_address (i, false, &before, &after);
3795 /* If the alternative accepts constant pool refs directly
3796 there will be no reload needed at all. */
3797 if (subreg != NULL_RTX)
3798 continue;
3799 /* Skip alternatives before the one requested. */
3800 constraint = (curr_static_id->operand_alternative
3801 [goal_alt_number * n_operands + i].constraint);
3802 for (;
3803 (c = *constraint) && c != ',' && c != '#';
3804 constraint += CONSTRAINT_LEN (c, constraint))
3806 enum constraint_num cn = lookup_constraint (constraint);
3807 if ((insn_extra_memory_constraint (cn)
3808 || insn_extra_special_memory_constraint (cn))
3809 && satisfies_memory_constraint_p (tem, cn))
3810 break;
3812 if (c == '\0' || c == ',' || c == '#')
3813 continue;
3815 goal_alt_win[i] = true;
3819 for (i = 0; i < n_operands; i++)
3821 int regno;
3822 bool optional_p = false;
3823 rtx old, new_reg;
3824 rtx op = *curr_id->operand_loc[i];
3826 if (goal_alt_win[i])
3828 if (goal_alt[i] == NO_REGS
3829 && REG_P (op)
3830 /* When we assign NO_REGS it means that we will not
3831 assign a hard register to the scratch pseudo by
3832 assigment pass and the scratch pseudo will be
3833 spilled. Spilled scratch pseudos are transformed
3834 back to scratches at the LRA end. */
3835 && lra_former_scratch_operand_p (curr_insn, i)
3836 && lra_former_scratch_p (REGNO (op)))
3838 int regno = REGNO (op);
3839 lra_change_class (regno, NO_REGS, " Change to", true);
3840 if (lra_get_regno_hard_regno (regno) >= 0)
3841 /* We don't have to mark all insn affected by the
3842 spilled pseudo as there is only one such insn, the
3843 current one. */
3844 reg_renumber[regno] = -1;
3845 lra_assert (bitmap_single_bit_set_p
3846 (&lra_reg_info[REGNO (op)].insn_bitmap));
3848 /* We can do an optional reload. If the pseudo got a hard
3849 reg, we might improve the code through inheritance. If
3850 it does not get a hard register we coalesce memory/memory
3851 moves later. Ignore move insns to avoid cycling. */
3852 if (! lra_simple_p
3853 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3854 && goal_alt[i] != NO_REGS && REG_P (op)
3855 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3856 && regno < new_regno_start
3857 && ! lra_former_scratch_p (regno)
3858 && reg_renumber[regno] < 0
3859 /* Check that the optional reload pseudo will be able to
3860 hold given mode value. */
3861 && ! (prohibited_class_reg_set_mode_p
3862 (goal_alt[i], reg_class_contents[goal_alt[i]],
3863 PSEUDO_REGNO_MODE (regno)))
3864 && (curr_insn_set == NULL_RTX
3865 || !((REG_P (SET_SRC (curr_insn_set))
3866 || MEM_P (SET_SRC (curr_insn_set))
3867 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3868 && (REG_P (SET_DEST (curr_insn_set))
3869 || MEM_P (SET_DEST (curr_insn_set))
3870 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3871 optional_p = true;
3872 else
3873 continue;
3876 /* Operands that match previous ones have already been handled. */
3877 if (goal_alt_matches[i] >= 0)
3878 continue;
3880 /* We should not have an operand with a non-offsettable address
3881 appearing where an offsettable address will do. It also may
3882 be a case when the address should be special in other words
3883 not a general one (e.g. it needs no index reg). */
3884 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3886 enum reg_class rclass;
3887 rtx *loc = &XEXP (op, 0);
3888 enum rtx_code code = GET_CODE (*loc);
3890 push_to_sequence (before);
3891 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3892 MEM, SCRATCH);
3893 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3894 new_reg = emit_inc (rclass, *loc, *loc,
3895 /* This value does not matter for MODIFY. */
3896 GET_MODE_SIZE (GET_MODE (op)));
3897 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3898 "offsetable address", &new_reg))
3899 lra_emit_move (new_reg, *loc);
3900 before = get_insns ();
3901 end_sequence ();
3902 *loc = new_reg;
3903 lra_update_dup (curr_id, i);
3905 else if (goal_alt_matched[i][0] == -1)
3907 machine_mode mode;
3908 rtx reg, *loc;
3909 int hard_regno, byte;
3910 enum op_type type = curr_static_id->operand[i].type;
3912 loc = curr_id->operand_loc[i];
3913 mode = curr_operand_mode[i];
3914 if (GET_CODE (*loc) == SUBREG)
3916 reg = SUBREG_REG (*loc);
3917 byte = SUBREG_BYTE (*loc);
3918 if (REG_P (reg)
3919 /* Strict_low_part requires reload the register not
3920 the sub-register. */
3921 && (curr_static_id->operand[i].strict_low
3922 || (GET_MODE_SIZE (mode)
3923 <= GET_MODE_SIZE (GET_MODE (reg))
3924 && (hard_regno
3925 = get_try_hard_regno (REGNO (reg))) >= 0
3926 && (simplify_subreg_regno
3927 (hard_regno,
3928 GET_MODE (reg), byte, mode) < 0)
3929 && (goal_alt[i] == NO_REGS
3930 || (simplify_subreg_regno
3931 (ira_class_hard_regs[goal_alt[i]][0],
3932 GET_MODE (reg), byte, mode) >= 0)))))
3934 if (type == OP_OUT)
3935 type = OP_INOUT;
3936 loc = &SUBREG_REG (*loc);
3937 mode = GET_MODE (*loc);
3940 old = *loc;
3941 if (get_reload_reg (type, mode, old, goal_alt[i],
3942 loc != curr_id->operand_loc[i], "", &new_reg)
3943 && type != OP_OUT)
3945 push_to_sequence (before);
3946 lra_emit_move (new_reg, old);
3947 before = get_insns ();
3948 end_sequence ();
3950 *loc = new_reg;
3951 if (type != OP_IN
3952 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3954 start_sequence ();
3955 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3956 emit_insn (after);
3957 after = get_insns ();
3958 end_sequence ();
3959 *loc = new_reg;
3961 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3962 if (goal_alt_dont_inherit_ops[j] == i)
3964 lra_set_regno_unique_value (REGNO (new_reg));
3965 break;
3967 lra_update_dup (curr_id, i);
3969 else if (curr_static_id->operand[i].type == OP_IN
3970 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3971 == OP_OUT))
3973 /* generate reloads for input and matched outputs. */
3974 match_inputs[0] = i;
3975 match_inputs[1] = -1;
3976 match_reload (goal_alt_matched[i][0], match_inputs,
3977 goal_alt[i], &before, &after,
3978 curr_static_id->operand_alternative
3979 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
3980 .earlyclobber);
3982 else if (curr_static_id->operand[i].type == OP_OUT
3983 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3984 == OP_IN))
3985 /* Generate reloads for output and matched inputs. */
3986 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after,
3987 curr_static_id->operand_alternative
3988 [goal_alt_number * n_operands + i].earlyclobber);
3989 else if (curr_static_id->operand[i].type == OP_IN
3990 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3991 == OP_IN))
3993 /* Generate reloads for matched inputs. */
3994 match_inputs[0] = i;
3995 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3996 match_inputs[j + 1] = k;
3997 match_inputs[j + 1] = -1;
3998 match_reload (-1, match_inputs, goal_alt[i], &before, &after, false);
4000 else
4001 /* We must generate code in any case when function
4002 process_alt_operands decides that it is possible. */
4003 gcc_unreachable ();
4004 if (optional_p)
4006 lra_assert (REG_P (op));
4007 regno = REGNO (op);
4008 op = *curr_id->operand_loc[i]; /* Substitution. */
4009 if (GET_CODE (op) == SUBREG)
4010 op = SUBREG_REG (op);
4011 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
4012 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
4013 lra_reg_info[REGNO (op)].restore_regno = regno;
4014 if (lra_dump_file != NULL)
4015 fprintf (lra_dump_file,
4016 " Making reload reg %d for reg %d optional\n",
4017 REGNO (op), regno);
4020 if (before != NULL_RTX || after != NULL_RTX
4021 || max_regno_before != max_reg_num ())
4022 change_p = true;
4023 if (change_p)
4025 lra_update_operator_dups (curr_id);
4026 /* Something changes -- process the insn. */
4027 lra_update_insn_regno_info (curr_insn);
4029 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
4030 return change_p;
4033 /* Return true if INSN satisfies all constraints. In other words, no
4034 reload insns are needed. */
4035 bool
4036 lra_constrain_insn (rtx_insn *insn)
4038 int saved_new_regno_start = new_regno_start;
4039 int saved_new_insn_uid_start = new_insn_uid_start;
4040 bool change_p;
4042 curr_insn = insn;
4043 curr_id = lra_get_insn_recog_data (curr_insn);
4044 curr_static_id = curr_id->insn_static_data;
4045 new_insn_uid_start = get_max_uid ();
4046 new_regno_start = max_reg_num ();
4047 change_p = curr_insn_transform (true);
4048 new_regno_start = saved_new_regno_start;
4049 new_insn_uid_start = saved_new_insn_uid_start;
4050 return ! change_p;
4053 /* Return true if X is in LIST. */
4054 static bool
4055 in_list_p (rtx x, rtx list)
4057 for (; list != NULL_RTX; list = XEXP (list, 1))
4058 if (XEXP (list, 0) == x)
4059 return true;
4060 return false;
4063 /* Return true if X contains an allocatable hard register (if
4064 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
4065 static bool
4066 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
4068 int i, j;
4069 const char *fmt;
4070 enum rtx_code code;
4072 code = GET_CODE (x);
4073 if (REG_P (x))
4075 int regno = REGNO (x);
4076 HARD_REG_SET alloc_regs;
4078 if (hard_reg_p)
4080 if (regno >= FIRST_PSEUDO_REGISTER)
4081 regno = lra_get_regno_hard_regno (regno);
4082 if (regno < 0)
4083 return false;
4084 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
4085 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
4087 else
4089 if (regno < FIRST_PSEUDO_REGISTER)
4090 return false;
4091 if (! spilled_p)
4092 return true;
4093 return lra_get_regno_hard_regno (regno) < 0;
4096 fmt = GET_RTX_FORMAT (code);
4097 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4099 if (fmt[i] == 'e')
4101 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4102 return true;
4104 else if (fmt[i] == 'E')
4106 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4107 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4108 return true;
4111 return false;
4114 /* Process all regs in location *LOC and change them on equivalent
4115 substitution. Return true if any change was done. */
4116 static bool
4117 loc_equivalence_change_p (rtx *loc)
4119 rtx subst, reg, x = *loc;
4120 bool result = false;
4121 enum rtx_code code = GET_CODE (x);
4122 const char *fmt;
4123 int i, j;
4125 if (code == SUBREG)
4127 reg = SUBREG_REG (x);
4128 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4129 && GET_MODE (subst) == VOIDmode)
4131 /* We cannot reload debug location. Simplify subreg here
4132 while we know the inner mode. */
4133 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4134 GET_MODE (reg), SUBREG_BYTE (x));
4135 return true;
4138 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4140 *loc = subst;
4141 return true;
4144 /* Scan all the operand sub-expressions. */
4145 fmt = GET_RTX_FORMAT (code);
4146 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4148 if (fmt[i] == 'e')
4149 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4150 else if (fmt[i] == 'E')
4151 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4152 result
4153 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4155 return result;
4158 /* Similar to loc_equivalence_change_p, but for use as
4159 simplify_replace_fn_rtx callback. DATA is insn for which the
4160 elimination is done. If it null we don't do the elimination. */
4161 static rtx
4162 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4164 if (!REG_P (loc))
4165 return NULL_RTX;
4167 rtx subst = (data == NULL
4168 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4169 if (subst != loc)
4170 return subst;
4172 return NULL_RTX;
4175 /* Maximum number of generated reload insns per an insn. It is for
4176 preventing this pass cycling in a bug case. */
4177 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4179 /* The current iteration number of this LRA pass. */
4180 int lra_constraint_iter;
4182 /* True if we substituted equiv which needs checking register
4183 allocation correctness because the equivalent value contains
4184 allocatable hard registers or when we restore multi-register
4185 pseudo. */
4186 bool lra_risky_transformations_p;
4188 /* Return true if REGNO is referenced in more than one block. */
4189 static bool
4190 multi_block_pseudo_p (int regno)
4192 basic_block bb = NULL;
4193 unsigned int uid;
4194 bitmap_iterator bi;
4196 if (regno < FIRST_PSEUDO_REGISTER)
4197 return false;
4199 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4200 if (bb == NULL)
4201 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4202 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4203 return true;
4204 return false;
4207 /* Return true if LIST contains a deleted insn. */
4208 static bool
4209 contains_deleted_insn_p (rtx_insn_list *list)
4211 for (; list != NULL_RTX; list = list->next ())
4212 if (NOTE_P (list->insn ())
4213 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4214 return true;
4215 return false;
4218 /* Return true if X contains a pseudo dying in INSN. */
4219 static bool
4220 dead_pseudo_p (rtx x, rtx_insn *insn)
4222 int i, j;
4223 const char *fmt;
4224 enum rtx_code code;
4226 if (REG_P (x))
4227 return (insn != NULL_RTX
4228 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4229 code = GET_CODE (x);
4230 fmt = GET_RTX_FORMAT (code);
4231 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4233 if (fmt[i] == 'e')
4235 if (dead_pseudo_p (XEXP (x, i), insn))
4236 return true;
4238 else if (fmt[i] == 'E')
4240 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4241 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4242 return true;
4245 return false;
4248 /* Return true if INSN contains a dying pseudo in INSN right hand
4249 side. */
4250 static bool
4251 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4253 rtx set = single_set (insn);
4255 gcc_assert (set != NULL);
4256 return dead_pseudo_p (SET_SRC (set), insn);
4259 /* Return true if any init insn of REGNO contains a dying pseudo in
4260 insn right hand side. */
4261 static bool
4262 init_insn_rhs_dead_pseudo_p (int regno)
4264 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4266 if (insns == NULL)
4267 return false;
4268 for (; insns != NULL_RTX; insns = insns->next ())
4269 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4270 return true;
4271 return false;
4274 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4275 reverse only if we have one init insn with given REGNO as a
4276 source. */
4277 static bool
4278 reverse_equiv_p (int regno)
4280 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4281 rtx set;
4283 if (insns == NULL)
4284 return false;
4285 if (! INSN_P (insns->insn ())
4286 || insns->next () != NULL)
4287 return false;
4288 if ((set = single_set (insns->insn ())) == NULL_RTX)
4289 return false;
4290 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4293 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4294 call this function only for non-reverse equivalence. */
4295 static bool
4296 contains_reloaded_insn_p (int regno)
4298 rtx set;
4299 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4301 for (; list != NULL; list = list->next ())
4302 if ((set = single_set (list->insn ())) == NULL_RTX
4303 || ! REG_P (SET_DEST (set))
4304 || (int) REGNO (SET_DEST (set)) != regno)
4305 return true;
4306 return false;
4309 /* Entry function of LRA constraint pass. Return true if the
4310 constraint pass did change the code. */
4311 bool
4312 lra_constraints (bool first_p)
4314 bool changed_p;
4315 int i, hard_regno, new_insns_num;
4316 unsigned int min_len, new_min_len, uid;
4317 rtx set, x, reg, dest_reg;
4318 basic_block last_bb;
4319 bitmap_head equiv_insn_bitmap;
4320 bitmap_iterator bi;
4322 lra_constraint_iter++;
4323 if (lra_dump_file != NULL)
4324 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4325 lra_constraint_iter);
4326 changed_p = false;
4327 if (pic_offset_table_rtx
4328 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4329 lra_risky_transformations_p = true;
4330 else
4331 lra_risky_transformations_p = false;
4332 new_insn_uid_start = get_max_uid ();
4333 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4334 /* Mark used hard regs for target stack size calulations. */
4335 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4336 if (lra_reg_info[i].nrefs != 0
4337 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4339 int j, nregs;
4341 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4342 for (j = 0; j < nregs; j++)
4343 df_set_regs_ever_live (hard_regno + j, true);
4345 /* Do elimination before the equivalence processing as we can spill
4346 some pseudos during elimination. */
4347 lra_eliminate (false, first_p);
4348 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4349 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4350 if (lra_reg_info[i].nrefs != 0)
4352 ira_reg_equiv[i].profitable_p = true;
4353 reg = regno_reg_rtx[i];
4354 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4356 bool pseudo_p = contains_reg_p (x, false, false);
4358 /* After RTL transformation, we can not guarantee that
4359 pseudo in the substitution was not reloaded which might
4360 make equivalence invalid. For example, in reverse
4361 equiv of p0
4363 p0 <- ...
4365 equiv_mem <- p0
4367 the memory address register was reloaded before the 2nd
4368 insn. */
4369 if ((! first_p && pseudo_p)
4370 /* We don't use DF for compilation speed sake. So it
4371 is problematic to update live info when we use an
4372 equivalence containing pseudos in more than one
4373 BB. */
4374 || (pseudo_p && multi_block_pseudo_p (i))
4375 /* If an init insn was deleted for some reason, cancel
4376 the equiv. We could update the equiv insns after
4377 transformations including an equiv insn deletion
4378 but it is not worthy as such cases are extremely
4379 rare. */
4380 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4381 /* If it is not a reverse equivalence, we check that a
4382 pseudo in rhs of the init insn is not dying in the
4383 insn. Otherwise, the live info at the beginning of
4384 the corresponding BB might be wrong after we
4385 removed the insn. When the equiv can be a
4386 constant, the right hand side of the init insn can
4387 be a pseudo. */
4388 || (! reverse_equiv_p (i)
4389 && (init_insn_rhs_dead_pseudo_p (i)
4390 /* If we reloaded the pseudo in an equivalence
4391 init insn, we can not remove the equiv init
4392 insns and the init insns might write into
4393 const memory in this case. */
4394 || contains_reloaded_insn_p (i)))
4395 /* Prevent access beyond equivalent memory for
4396 paradoxical subregs. */
4397 || (MEM_P (x)
4398 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4399 > GET_MODE_SIZE (GET_MODE (x))))
4400 || (pic_offset_table_rtx
4401 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4402 && (targetm.preferred_reload_class
4403 (x, lra_get_allocno_class (i)) == NO_REGS))
4404 || contains_symbol_ref_p (x))))
4405 ira_reg_equiv[i].defined_p = false;
4406 if (contains_reg_p (x, false, true))
4407 ira_reg_equiv[i].profitable_p = false;
4408 if (get_equiv (reg) != reg)
4409 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4412 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4413 update_equiv (i);
4414 /* We should add all insns containing pseudos which should be
4415 substituted by their equivalences. */
4416 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4417 lra_push_insn_by_uid (uid);
4418 min_len = lra_insn_stack_length ();
4419 new_insns_num = 0;
4420 last_bb = NULL;
4421 changed_p = false;
4422 while ((new_min_len = lra_insn_stack_length ()) != 0)
4424 curr_insn = lra_pop_insn ();
4425 --new_min_len;
4426 curr_bb = BLOCK_FOR_INSN (curr_insn);
4427 if (curr_bb != last_bb)
4429 last_bb = curr_bb;
4430 bb_reload_num = lra_curr_reload_num;
4432 if (min_len > new_min_len)
4434 min_len = new_min_len;
4435 new_insns_num = 0;
4437 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4438 internal_error
4439 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4440 MAX_RELOAD_INSNS_NUMBER);
4441 new_insns_num++;
4442 if (DEBUG_INSN_P (curr_insn))
4444 /* We need to check equivalence in debug insn and change
4445 pseudo to the equivalent value if necessary. */
4446 curr_id = lra_get_insn_recog_data (curr_insn);
4447 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4449 rtx old = *curr_id->operand_loc[0];
4450 *curr_id->operand_loc[0]
4451 = simplify_replace_fn_rtx (old, NULL_RTX,
4452 loc_equivalence_callback, curr_insn);
4453 if (old != *curr_id->operand_loc[0])
4455 lra_update_insn_regno_info (curr_insn);
4456 changed_p = true;
4460 else if (INSN_P (curr_insn))
4462 if ((set = single_set (curr_insn)) != NULL_RTX)
4464 dest_reg = SET_DEST (set);
4465 /* The equivalence pseudo could be set up as SUBREG in a
4466 case when it is a call restore insn in a mode
4467 different from the pseudo mode. */
4468 if (GET_CODE (dest_reg) == SUBREG)
4469 dest_reg = SUBREG_REG (dest_reg);
4470 if ((REG_P (dest_reg)
4471 && (x = get_equiv (dest_reg)) != dest_reg
4472 /* Remove insns which set up a pseudo whose value
4473 can not be changed. Such insns might be not in
4474 init_insns because we don't update equiv data
4475 during insn transformations.
4477 As an example, let suppose that a pseudo got
4478 hard register and on the 1st pass was not
4479 changed to equivalent constant. We generate an
4480 additional insn setting up the pseudo because of
4481 secondary memory movement. Then the pseudo is
4482 spilled and we use the equiv constant. In this
4483 case we should remove the additional insn and
4484 this insn is not init_insns list. */
4485 && (! MEM_P (x) || MEM_READONLY_P (x)
4486 /* Check that this is actually an insn setting
4487 up the equivalence. */
4488 || in_list_p (curr_insn,
4489 ira_reg_equiv
4490 [REGNO (dest_reg)].init_insns)))
4491 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4492 && in_list_p (curr_insn,
4493 ira_reg_equiv
4494 [REGNO (SET_SRC (set))].init_insns)))
4496 /* This is equiv init insn of pseudo which did not get a
4497 hard register -- remove the insn. */
4498 if (lra_dump_file != NULL)
4500 fprintf (lra_dump_file,
4501 " Removing equiv init insn %i (freq=%d)\n",
4502 INSN_UID (curr_insn),
4503 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4504 dump_insn_slim (lra_dump_file, curr_insn);
4506 if (contains_reg_p (x, true, false))
4507 lra_risky_transformations_p = true;
4508 lra_set_insn_deleted (curr_insn);
4509 continue;
4512 curr_id = lra_get_insn_recog_data (curr_insn);
4513 curr_static_id = curr_id->insn_static_data;
4514 init_curr_insn_input_reloads ();
4515 init_curr_operand_mode ();
4516 if (curr_insn_transform (false))
4517 changed_p = true;
4518 /* Check non-transformed insns too for equiv change as USE
4519 or CLOBBER don't need reloads but can contain pseudos
4520 being changed on their equivalences. */
4521 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4522 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4524 lra_update_insn_regno_info (curr_insn);
4525 changed_p = true;
4529 bitmap_clear (&equiv_insn_bitmap);
4530 /* If we used a new hard regno, changed_p should be true because the
4531 hard reg is assigned to a new pseudo. */
4532 if (flag_checking && !changed_p)
4534 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4535 if (lra_reg_info[i].nrefs != 0
4536 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4538 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4540 for (j = 0; j < nregs; j++)
4541 lra_assert (df_regs_ever_live_p (hard_regno + j));
4544 return changed_p;
4547 /* Initiate the LRA constraint pass. It is done once per
4548 function. */
4549 void
4550 lra_constraints_init (void)
4554 /* Finalize the LRA constraint pass. It is done once per
4555 function. */
4556 void
4557 lra_constraints_finish (void)
4563 /* This page contains code to do inheritance/split
4564 transformations. */
4566 /* Number of reloads passed so far in current EBB. */
4567 static int reloads_num;
4569 /* Number of calls passed so far in current EBB. */
4570 static int calls_num;
4572 /* Current reload pseudo check for validity of elements in
4573 USAGE_INSNS. */
4574 static int curr_usage_insns_check;
4576 /* Info about last usage of registers in EBB to do inheritance/split
4577 transformation. Inheritance transformation is done from a spilled
4578 pseudo and split transformations from a hard register or a pseudo
4579 assigned to a hard register. */
4580 struct usage_insns
4582 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4583 value INSNS is valid. The insns is chain of optional debug insns
4584 and a finishing non-debug insn using the corresponding reg. The
4585 value is also used to mark the registers which are set up in the
4586 current insn. The negated insn uid is used for this. */
4587 int check;
4588 /* Value of global reloads_num at the last insn in INSNS. */
4589 int reloads_num;
4590 /* Value of global reloads_nums at the last insn in INSNS. */
4591 int calls_num;
4592 /* It can be true only for splitting. And it means that the restore
4593 insn should be put after insn given by the following member. */
4594 bool after_p;
4595 /* Next insns in the current EBB which use the original reg and the
4596 original reg value is not changed between the current insn and
4597 the next insns. In order words, e.g. for inheritance, if we need
4598 to use the original reg value again in the next insns we can try
4599 to use the value in a hard register from a reload insn of the
4600 current insn. */
4601 rtx insns;
4604 /* Map: regno -> corresponding pseudo usage insns. */
4605 static struct usage_insns *usage_insns;
4607 static void
4608 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4610 usage_insns[regno].check = curr_usage_insns_check;
4611 usage_insns[regno].insns = insn;
4612 usage_insns[regno].reloads_num = reloads_num;
4613 usage_insns[regno].calls_num = calls_num;
4614 usage_insns[regno].after_p = after_p;
4617 /* The function is used to form list REGNO usages which consists of
4618 optional debug insns finished by a non-debug insn using REGNO.
4619 RELOADS_NUM is current number of reload insns processed so far. */
4620 static void
4621 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
4623 rtx next_usage_insns;
4625 if (usage_insns[regno].check == curr_usage_insns_check
4626 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4627 && DEBUG_INSN_P (insn))
4629 /* Check that we did not add the debug insn yet. */
4630 if (next_usage_insns != insn
4631 && (GET_CODE (next_usage_insns) != INSN_LIST
4632 || XEXP (next_usage_insns, 0) != insn))
4633 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4634 next_usage_insns);
4636 else if (NONDEBUG_INSN_P (insn))
4637 setup_next_usage_insn (regno, insn, reloads_num, false);
4638 else
4639 usage_insns[regno].check = 0;
4642 /* Return first non-debug insn in list USAGE_INSNS. */
4643 static rtx_insn *
4644 skip_usage_debug_insns (rtx usage_insns)
4646 rtx insn;
4648 /* Skip debug insns. */
4649 for (insn = usage_insns;
4650 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4651 insn = XEXP (insn, 1))
4653 return safe_as_a <rtx_insn *> (insn);
4656 /* Return true if we need secondary memory moves for insn in
4657 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4658 into the insn. */
4659 static bool
4660 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4661 rtx usage_insns ATTRIBUTE_UNUSED)
4663 #ifndef SECONDARY_MEMORY_NEEDED
4664 return false;
4665 #else
4666 rtx_insn *insn;
4667 rtx set, dest;
4668 enum reg_class cl;
4670 if (inher_cl == ALL_REGS
4671 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4672 return false;
4673 lra_assert (INSN_P (insn));
4674 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4675 return false;
4676 dest = SET_DEST (set);
4677 if (! REG_P (dest))
4678 return false;
4679 lra_assert (inher_cl != NO_REGS);
4680 cl = get_reg_class (REGNO (dest));
4681 return (cl != NO_REGS && cl != ALL_REGS
4682 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4683 #endif
4686 /* Registers involved in inheritance/split in the current EBB
4687 (inheritance/split pseudos and original registers). */
4688 static bitmap_head check_only_regs;
4690 /* Do inheritance transformations for insn INSN, which defines (if
4691 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4692 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4693 form as the "insns" field of usage_insns. Return true if we
4694 succeed in such transformation.
4696 The transformations look like:
4698 p <- ... i <- ...
4699 ... p <- i (new insn)
4700 ... =>
4701 <- ... p ... <- ... i ...
4703 ... i <- p (new insn)
4704 <- ... p ... <- ... i ...
4705 ... =>
4706 <- ... p ... <- ... i ...
4707 where p is a spilled original pseudo and i is a new inheritance pseudo.
4710 The inheritance pseudo has the smallest class of two classes CL and
4711 class of ORIGINAL REGNO. */
4712 static bool
4713 inherit_reload_reg (bool def_p, int original_regno,
4714 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4716 if (optimize_function_for_size_p (cfun))
4717 return false;
4719 enum reg_class rclass = lra_get_allocno_class (original_regno);
4720 rtx original_reg = regno_reg_rtx[original_regno];
4721 rtx new_reg, usage_insn;
4722 rtx_insn *new_insns;
4724 lra_assert (! usage_insns[original_regno].after_p);
4725 if (lra_dump_file != NULL)
4726 fprintf (lra_dump_file,
4727 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4728 if (! ira_reg_classes_intersect_p[cl][rclass])
4730 if (lra_dump_file != NULL)
4732 fprintf (lra_dump_file,
4733 " Rejecting inheritance for %d "
4734 "because of disjoint classes %s and %s\n",
4735 original_regno, reg_class_names[cl],
4736 reg_class_names[rclass]);
4737 fprintf (lra_dump_file,
4738 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4740 return false;
4742 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4743 /* We don't use a subset of two classes because it can be
4744 NO_REGS. This transformation is still profitable in most
4745 cases even if the classes are not intersected as register
4746 move is probably cheaper than a memory load. */
4747 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4749 if (lra_dump_file != NULL)
4750 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4751 reg_class_names[cl], reg_class_names[rclass]);
4753 rclass = cl;
4755 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4757 /* Reject inheritance resulting in secondary memory moves.
4758 Otherwise, there is a danger in LRA cycling. Also such
4759 transformation will be unprofitable. */
4760 if (lra_dump_file != NULL)
4762 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4763 rtx set = single_set (insn);
4765 lra_assert (set != NULL_RTX);
4767 rtx dest = SET_DEST (set);
4769 lra_assert (REG_P (dest));
4770 fprintf (lra_dump_file,
4771 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4772 "as secondary mem is needed\n",
4773 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4774 original_regno, reg_class_names[rclass]);
4775 fprintf (lra_dump_file,
4776 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4778 return false;
4780 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4781 rclass, "inheritance");
4782 start_sequence ();
4783 if (def_p)
4784 lra_emit_move (original_reg, new_reg);
4785 else
4786 lra_emit_move (new_reg, original_reg);
4787 new_insns = get_insns ();
4788 end_sequence ();
4789 if (NEXT_INSN (new_insns) != NULL_RTX)
4791 if (lra_dump_file != NULL)
4793 fprintf (lra_dump_file,
4794 " Rejecting inheritance %d->%d "
4795 "as it results in 2 or more insns:\n",
4796 original_regno, REGNO (new_reg));
4797 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4798 fprintf (lra_dump_file,
4799 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4801 return false;
4803 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
4804 lra_update_insn_regno_info (insn);
4805 if (! def_p)
4806 /* We now have a new usage insn for original regno. */
4807 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4808 if (lra_dump_file != NULL)
4809 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4810 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4811 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4812 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4813 bitmap_set_bit (&check_only_regs, original_regno);
4814 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4815 if (def_p)
4816 lra_process_new_insns (insn, NULL, new_insns,
4817 "Add original<-inheritance");
4818 else
4819 lra_process_new_insns (insn, new_insns, NULL,
4820 "Add inheritance<-original");
4821 while (next_usage_insns != NULL_RTX)
4823 if (GET_CODE (next_usage_insns) != INSN_LIST)
4825 usage_insn = next_usage_insns;
4826 lra_assert (NONDEBUG_INSN_P (usage_insn));
4827 next_usage_insns = NULL;
4829 else
4831 usage_insn = XEXP (next_usage_insns, 0);
4832 lra_assert (DEBUG_INSN_P (usage_insn));
4833 next_usage_insns = XEXP (next_usage_insns, 1);
4835 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
4836 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4837 if (lra_dump_file != NULL)
4839 fprintf (lra_dump_file,
4840 " Inheritance reuse change %d->%d (bb%d):\n",
4841 original_regno, REGNO (new_reg),
4842 BLOCK_FOR_INSN (usage_insn)->index);
4843 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
4846 if (lra_dump_file != NULL)
4847 fprintf (lra_dump_file,
4848 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4849 return true;
4852 /* Return true if we need a caller save/restore for pseudo REGNO which
4853 was assigned to a hard register. */
4854 static inline bool
4855 need_for_call_save_p (int regno)
4857 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4858 return (usage_insns[regno].calls_num < calls_num
4859 && (overlaps_hard_reg_set_p
4860 ((flag_ipa_ra &&
4861 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4862 ? lra_reg_info[regno].actual_call_used_reg_set
4863 : call_used_reg_set,
4864 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4865 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4866 PSEUDO_REGNO_MODE (regno))));
4869 /* Global registers occurring in the current EBB. */
4870 static bitmap_head ebb_global_regs;
4872 /* Return true if we need a split for hard register REGNO or pseudo
4873 REGNO which was assigned to a hard register.
4874 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4875 used for reloads since the EBB end. It is an approximation of the
4876 used hard registers in the split range. The exact value would
4877 require expensive calculations. If we were aggressive with
4878 splitting because of the approximation, the split pseudo will save
4879 the same hard register assignment and will be removed in the undo
4880 pass. We still need the approximation because too aggressive
4881 splitting would result in too inaccurate cost calculation in the
4882 assignment pass because of too many generated moves which will be
4883 probably removed in the undo pass. */
4884 static inline bool
4885 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4887 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4889 lra_assert (hard_regno >= 0);
4890 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4891 /* Don't split eliminable hard registers, otherwise we can
4892 split hard registers like hard frame pointer, which
4893 lives on BB start/end according to DF-infrastructure,
4894 when there is a pseudo assigned to the register and
4895 living in the same BB. */
4896 && (regno >= FIRST_PSEUDO_REGISTER
4897 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4898 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4899 /* Don't split call clobbered hard regs living through
4900 calls, otherwise we might have a check problem in the
4901 assign sub-pass as in the most cases (exception is a
4902 situation when lra_risky_transformations_p value is
4903 true) the assign pass assumes that all pseudos living
4904 through calls are assigned to call saved hard regs. */
4905 && (regno >= FIRST_PSEUDO_REGISTER
4906 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4907 || usage_insns[regno].calls_num == calls_num)
4908 /* We need at least 2 reloads to make pseudo splitting
4909 profitable. We should provide hard regno splitting in
4910 any case to solve 1st insn scheduling problem when
4911 moving hard register definition up might result in
4912 impossibility to find hard register for reload pseudo of
4913 small register class. */
4914 && (usage_insns[regno].reloads_num
4915 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4916 && (regno < FIRST_PSEUDO_REGISTER
4917 /* For short living pseudos, spilling + inheritance can
4918 be considered a substitution for splitting.
4919 Therefore we do not splitting for local pseudos. It
4920 decreases also aggressiveness of splitting. The
4921 minimal number of references is chosen taking into
4922 account that for 2 references splitting has no sense
4923 as we can just spill the pseudo. */
4924 || (regno >= FIRST_PSEUDO_REGISTER
4925 && lra_reg_info[regno].nrefs > 3
4926 && bitmap_bit_p (&ebb_global_regs, regno))))
4927 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4930 /* Return class for the split pseudo created from original pseudo with
4931 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4932 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4933 results in no secondary memory movements. */
4934 static enum reg_class
4935 choose_split_class (enum reg_class allocno_class,
4936 int hard_regno ATTRIBUTE_UNUSED,
4937 machine_mode mode ATTRIBUTE_UNUSED)
4939 #ifndef SECONDARY_MEMORY_NEEDED
4940 return allocno_class;
4941 #else
4942 int i;
4943 enum reg_class cl, best_cl = NO_REGS;
4944 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4945 = REGNO_REG_CLASS (hard_regno);
4947 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4948 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4949 return allocno_class;
4950 for (i = 0;
4951 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4952 i++)
4953 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4954 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4955 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4956 && (best_cl == NO_REGS
4957 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4958 best_cl = cl;
4959 return best_cl;
4960 #endif
4963 /* Do split transformations for insn INSN, which defines or uses
4964 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4965 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4966 "insns" field of usage_insns.
4968 The transformations look like:
4970 p <- ... p <- ...
4971 ... s <- p (new insn -- save)
4972 ... =>
4973 ... p <- s (new insn -- restore)
4974 <- ... p ... <- ... p ...
4976 <- ... p ... <- ... p ...
4977 ... s <- p (new insn -- save)
4978 ... =>
4979 ... p <- s (new insn -- restore)
4980 <- ... p ... <- ... p ...
4982 where p is an original pseudo got a hard register or a hard
4983 register and s is a new split pseudo. The save is put before INSN
4984 if BEFORE_P is true. Return true if we succeed in such
4985 transformation. */
4986 static bool
4987 split_reg (bool before_p, int original_regno, rtx_insn *insn,
4988 rtx next_usage_insns)
4990 enum reg_class rclass;
4991 rtx original_reg;
4992 int hard_regno, nregs;
4993 rtx new_reg, usage_insn;
4994 rtx_insn *restore, *save;
4995 bool after_p;
4996 bool call_save_p;
4997 machine_mode mode;
4999 if (original_regno < FIRST_PSEUDO_REGISTER)
5001 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
5002 hard_regno = original_regno;
5003 call_save_p = false;
5004 nregs = 1;
5005 mode = lra_reg_info[hard_regno].biggest_mode;
5006 machine_mode reg_rtx_mode = GET_MODE (regno_reg_rtx[hard_regno]);
5007 /* A reg can have a biggest_mode of VOIDmode if it was only ever seen
5008 as part of a multi-word register. In that case, or if the biggest
5009 mode was larger than a register, just use the reg_rtx. Otherwise,
5010 limit the size to that of the biggest access in the function. */
5011 if (mode == VOIDmode
5012 || GET_MODE_SIZE (mode) > GET_MODE_SIZE (reg_rtx_mode))
5014 original_reg = regno_reg_rtx[hard_regno];
5015 mode = reg_rtx_mode;
5017 else
5018 original_reg = gen_rtx_REG (mode, hard_regno);
5020 else
5022 mode = PSEUDO_REGNO_MODE (original_regno);
5023 hard_regno = reg_renumber[original_regno];
5024 nregs = hard_regno_nregs[hard_regno][mode];
5025 rclass = lra_get_allocno_class (original_regno);
5026 original_reg = regno_reg_rtx[original_regno];
5027 call_save_p = need_for_call_save_p (original_regno);
5029 lra_assert (hard_regno >= 0);
5030 if (lra_dump_file != NULL)
5031 fprintf (lra_dump_file,
5032 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
5034 if (call_save_p)
5036 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
5037 hard_regno_nregs[hard_regno][mode],
5038 mode);
5039 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
5041 else
5043 rclass = choose_split_class (rclass, hard_regno, mode);
5044 if (rclass == NO_REGS)
5046 if (lra_dump_file != NULL)
5048 fprintf (lra_dump_file,
5049 " Rejecting split of %d(%s): "
5050 "no good reg class for %d(%s)\n",
5051 original_regno,
5052 reg_class_names[lra_get_allocno_class (original_regno)],
5053 hard_regno,
5054 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
5055 fprintf
5056 (lra_dump_file,
5057 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5059 return false;
5061 new_reg = lra_create_new_reg (mode, original_reg, rclass, "split");
5062 reg_renumber[REGNO (new_reg)] = hard_regno;
5064 save = emit_spill_move (true, new_reg, original_reg);
5065 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
5067 if (lra_dump_file != NULL)
5069 fprintf
5070 (lra_dump_file,
5071 " Rejecting split %d->%d resulting in > 2 save insns:\n",
5072 original_regno, REGNO (new_reg));
5073 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
5074 fprintf (lra_dump_file,
5075 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5077 return false;
5079 restore = emit_spill_move (false, new_reg, original_reg);
5080 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
5082 if (lra_dump_file != NULL)
5084 fprintf (lra_dump_file,
5085 " Rejecting split %d->%d "
5086 "resulting in > 2 restore insns:\n",
5087 original_regno, REGNO (new_reg));
5088 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
5089 fprintf (lra_dump_file,
5090 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5092 return false;
5094 after_p = usage_insns[original_regno].after_p;
5095 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
5096 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5097 bitmap_set_bit (&check_only_regs, original_regno);
5098 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
5099 for (;;)
5101 if (GET_CODE (next_usage_insns) != INSN_LIST)
5103 usage_insn = next_usage_insns;
5104 break;
5106 usage_insn = XEXP (next_usage_insns, 0);
5107 lra_assert (DEBUG_INSN_P (usage_insn));
5108 next_usage_insns = XEXP (next_usage_insns, 1);
5109 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5110 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5111 if (lra_dump_file != NULL)
5113 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5114 original_regno, REGNO (new_reg));
5115 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5118 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5119 lra_assert (usage_insn != insn || (after_p && before_p));
5120 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5121 after_p ? NULL : restore,
5122 after_p ? restore : NULL,
5123 call_save_p
5124 ? "Add reg<-save" : "Add reg<-split");
5125 lra_process_new_insns (insn, before_p ? save : NULL,
5126 before_p ? NULL : save,
5127 call_save_p
5128 ? "Add save<-reg" : "Add split<-reg");
5129 if (nregs > 1)
5130 /* If we are trying to split multi-register. We should check
5131 conflicts on the next assignment sub-pass. IRA can allocate on
5132 sub-register levels, LRA do this on pseudos level right now and
5133 this discrepancy may create allocation conflicts after
5134 splitting. */
5135 lra_risky_transformations_p = true;
5136 if (lra_dump_file != NULL)
5137 fprintf (lra_dump_file,
5138 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5139 return true;
5142 /* Recognize that we need a split transformation for insn INSN, which
5143 defines or uses REGNO in its insn biggest MODE (we use it only if
5144 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5145 hard registers which might be used for reloads since the EBB end.
5146 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5147 uid before starting INSN processing. Return true if we succeed in
5148 such transformation. */
5149 static bool
5150 split_if_necessary (int regno, machine_mode mode,
5151 HARD_REG_SET potential_reload_hard_regs,
5152 bool before_p, rtx_insn *insn, int max_uid)
5154 bool res = false;
5155 int i, nregs = 1;
5156 rtx next_usage_insns;
5158 if (regno < FIRST_PSEUDO_REGISTER)
5159 nregs = hard_regno_nregs[regno][mode];
5160 for (i = 0; i < nregs; i++)
5161 if (usage_insns[regno + i].check == curr_usage_insns_check
5162 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5163 /* To avoid processing the register twice or more. */
5164 && ((GET_CODE (next_usage_insns) != INSN_LIST
5165 && INSN_UID (next_usage_insns) < max_uid)
5166 || (GET_CODE (next_usage_insns) == INSN_LIST
5167 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5168 && need_for_split_p (potential_reload_hard_regs, regno + i)
5169 && split_reg (before_p, regno + i, insn, next_usage_insns))
5170 res = true;
5171 return res;
5174 /* Check only registers living at the current program point in the
5175 current EBB. */
5176 static bitmap_head live_regs;
5178 /* Update live info in EBB given by its HEAD and TAIL insns after
5179 inheritance/split transformation. The function removes dead moves
5180 too. */
5181 static void
5182 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5184 unsigned int j;
5185 int i, regno;
5186 bool live_p;
5187 rtx_insn *prev_insn;
5188 rtx set;
5189 bool remove_p;
5190 basic_block last_bb, prev_bb, curr_bb;
5191 bitmap_iterator bi;
5192 struct lra_insn_reg *reg;
5193 edge e;
5194 edge_iterator ei;
5196 last_bb = BLOCK_FOR_INSN (tail);
5197 prev_bb = NULL;
5198 for (curr_insn = tail;
5199 curr_insn != PREV_INSN (head);
5200 curr_insn = prev_insn)
5202 prev_insn = PREV_INSN (curr_insn);
5203 /* We need to process empty blocks too. They contain
5204 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5205 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5206 continue;
5207 curr_bb = BLOCK_FOR_INSN (curr_insn);
5208 if (curr_bb != prev_bb)
5210 if (prev_bb != NULL)
5212 /* Update df_get_live_in (prev_bb): */
5213 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5214 if (bitmap_bit_p (&live_regs, j))
5215 bitmap_set_bit (df_get_live_in (prev_bb), j);
5216 else
5217 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5219 if (curr_bb != last_bb)
5221 /* Update df_get_live_out (curr_bb): */
5222 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5224 live_p = bitmap_bit_p (&live_regs, j);
5225 if (! live_p)
5226 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5227 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5229 live_p = true;
5230 break;
5232 if (live_p)
5233 bitmap_set_bit (df_get_live_out (curr_bb), j);
5234 else
5235 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5238 prev_bb = curr_bb;
5239 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5241 if (! NONDEBUG_INSN_P (curr_insn))
5242 continue;
5243 curr_id = lra_get_insn_recog_data (curr_insn);
5244 curr_static_id = curr_id->insn_static_data;
5245 remove_p = false;
5246 if ((set = single_set (curr_insn)) != NULL_RTX
5247 && REG_P (SET_DEST (set))
5248 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5249 && SET_DEST (set) != pic_offset_table_rtx
5250 && bitmap_bit_p (&check_only_regs, regno)
5251 && ! bitmap_bit_p (&live_regs, regno))
5252 remove_p = true;
5253 /* See which defined values die here. */
5254 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5255 if (reg->type == OP_OUT && ! reg->subreg_p)
5256 bitmap_clear_bit (&live_regs, reg->regno);
5257 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5258 if (reg->type == OP_OUT && ! reg->subreg_p)
5259 bitmap_clear_bit (&live_regs, reg->regno);
5260 if (curr_id->arg_hard_regs != NULL)
5261 /* Make clobbered argument hard registers die. */
5262 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5263 if (regno >= FIRST_PSEUDO_REGISTER)
5264 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
5265 /* Mark each used value as live. */
5266 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5267 if (reg->type != OP_OUT
5268 && bitmap_bit_p (&check_only_regs, reg->regno))
5269 bitmap_set_bit (&live_regs, reg->regno);
5270 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5271 if (reg->type != OP_OUT
5272 && bitmap_bit_p (&check_only_regs, reg->regno))
5273 bitmap_set_bit (&live_regs, reg->regno);
5274 if (curr_id->arg_hard_regs != NULL)
5275 /* Make used argument hard registers live. */
5276 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5277 if (regno < FIRST_PSEUDO_REGISTER
5278 && bitmap_bit_p (&check_only_regs, regno))
5279 bitmap_set_bit (&live_regs, regno);
5280 /* It is quite important to remove dead move insns because it
5281 means removing dead store. We don't need to process them for
5282 constraints. */
5283 if (remove_p)
5285 if (lra_dump_file != NULL)
5287 fprintf (lra_dump_file, " Removing dead insn:\n ");
5288 dump_insn_slim (lra_dump_file, curr_insn);
5290 lra_set_insn_deleted (curr_insn);
5295 /* The structure describes info to do an inheritance for the current
5296 insn. We need to collect such info first before doing the
5297 transformations because the transformations change the insn
5298 internal representation. */
5299 struct to_inherit
5301 /* Original regno. */
5302 int regno;
5303 /* Subsequent insns which can inherit original reg value. */
5304 rtx insns;
5307 /* Array containing all info for doing inheritance from the current
5308 insn. */
5309 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5311 /* Number elements in the previous array. */
5312 static int to_inherit_num;
5314 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5315 structure to_inherit. */
5316 static void
5317 add_to_inherit (int regno, rtx insns)
5319 int i;
5321 for (i = 0; i < to_inherit_num; i++)
5322 if (to_inherit[i].regno == regno)
5323 return;
5324 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5325 to_inherit[to_inherit_num].regno = regno;
5326 to_inherit[to_inherit_num++].insns = insns;
5329 /* Return the last non-debug insn in basic block BB, or the block begin
5330 note if none. */
5331 static rtx_insn *
5332 get_last_insertion_point (basic_block bb)
5334 rtx_insn *insn;
5336 FOR_BB_INSNS_REVERSE (bb, insn)
5337 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5338 return insn;
5339 gcc_unreachable ();
5342 /* Set up RES by registers living on edges FROM except the edge (FROM,
5343 TO) or by registers set up in a jump insn in BB FROM. */
5344 static void
5345 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5347 rtx_insn *last;
5348 struct lra_insn_reg *reg;
5349 edge e;
5350 edge_iterator ei;
5352 lra_assert (to != NULL);
5353 bitmap_clear (res);
5354 FOR_EACH_EDGE (e, ei, from->succs)
5355 if (e->dest != to)
5356 bitmap_ior_into (res, df_get_live_in (e->dest));
5357 last = get_last_insertion_point (from);
5358 if (! JUMP_P (last))
5359 return;
5360 curr_id = lra_get_insn_recog_data (last);
5361 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5362 if (reg->type != OP_IN)
5363 bitmap_set_bit (res, reg->regno);
5366 /* Used as a temporary results of some bitmap calculations. */
5367 static bitmap_head temp_bitmap;
5369 /* We split for reloads of small class of hard regs. The following
5370 defines how many hard regs the class should have to be qualified as
5371 small. The code is mostly oriented to x86/x86-64 architecture
5372 where some insns need to use only specific register or pair of
5373 registers and these register can live in RTL explicitly, e.g. for
5374 parameter passing. */
5375 static const int max_small_class_regs_num = 2;
5377 /* Do inheritance/split transformations in EBB starting with HEAD and
5378 finishing on TAIL. We process EBB insns in the reverse order.
5379 Return true if we did any inheritance/split transformation in the
5380 EBB.
5382 We should avoid excessive splitting which results in worse code
5383 because of inaccurate cost calculations for spilling new split
5384 pseudos in such case. To achieve this we do splitting only if
5385 register pressure is high in given basic block and there are reload
5386 pseudos requiring hard registers. We could do more register
5387 pressure calculations at any given program point to avoid necessary
5388 splitting even more but it is to expensive and the current approach
5389 works well enough. */
5390 static bool
5391 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5393 int i, src_regno, dst_regno, nregs;
5394 bool change_p, succ_p, update_reloads_num_p;
5395 rtx_insn *prev_insn, *last_insn;
5396 rtx next_usage_insns, set;
5397 enum reg_class cl;
5398 struct lra_insn_reg *reg;
5399 basic_block last_processed_bb, curr_bb = NULL;
5400 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5401 bitmap to_process;
5402 unsigned int j;
5403 bitmap_iterator bi;
5404 bool head_p, after_p;
5406 change_p = false;
5407 curr_usage_insns_check++;
5408 reloads_num = calls_num = 0;
5409 bitmap_clear (&check_only_regs);
5410 last_processed_bb = NULL;
5411 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5412 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5413 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5414 /* We don't process new insns generated in the loop. */
5415 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5417 prev_insn = PREV_INSN (curr_insn);
5418 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5419 curr_bb = BLOCK_FOR_INSN (curr_insn);
5420 if (last_processed_bb != curr_bb)
5422 /* We are at the end of BB. Add qualified living
5423 pseudos for potential splitting. */
5424 to_process = df_get_live_out (curr_bb);
5425 if (last_processed_bb != NULL)
5427 /* We are somewhere in the middle of EBB. */
5428 get_live_on_other_edges (curr_bb, last_processed_bb,
5429 &temp_bitmap);
5430 to_process = &temp_bitmap;
5432 last_processed_bb = curr_bb;
5433 last_insn = get_last_insertion_point (curr_bb);
5434 after_p = (! JUMP_P (last_insn)
5435 && (! CALL_P (last_insn)
5436 || (find_reg_note (last_insn,
5437 REG_NORETURN, NULL_RTX) == NULL_RTX
5438 && ! SIBLING_CALL_P (last_insn))));
5439 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5440 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5442 if ((int) j >= lra_constraint_new_regno_start)
5443 break;
5444 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5446 if (j < FIRST_PSEUDO_REGISTER)
5447 SET_HARD_REG_BIT (live_hard_regs, j);
5448 else
5449 add_to_hard_reg_set (&live_hard_regs,
5450 PSEUDO_REGNO_MODE (j),
5451 reg_renumber[j]);
5452 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5456 src_regno = dst_regno = -1;
5457 if (NONDEBUG_INSN_P (curr_insn)
5458 && (set = single_set (curr_insn)) != NULL_RTX
5459 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5461 src_regno = REGNO (SET_SRC (set));
5462 dst_regno = REGNO (SET_DEST (set));
5464 update_reloads_num_p = true;
5465 if (src_regno < lra_constraint_new_regno_start
5466 && src_regno >= FIRST_PSEUDO_REGISTER
5467 && reg_renumber[src_regno] < 0
5468 && dst_regno >= lra_constraint_new_regno_start
5469 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5471 /* 'reload_pseudo <- original_pseudo'. */
5472 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5473 reloads_num++;
5474 update_reloads_num_p = false;
5475 succ_p = false;
5476 if (usage_insns[src_regno].check == curr_usage_insns_check
5477 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5478 succ_p = inherit_reload_reg (false, src_regno, cl,
5479 curr_insn, next_usage_insns);
5480 if (succ_p)
5481 change_p = true;
5482 else
5483 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5484 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5485 IOR_HARD_REG_SET (potential_reload_hard_regs,
5486 reg_class_contents[cl]);
5488 else if (src_regno >= lra_constraint_new_regno_start
5489 && dst_regno < lra_constraint_new_regno_start
5490 && dst_regno >= FIRST_PSEUDO_REGISTER
5491 && reg_renumber[dst_regno] < 0
5492 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5493 && usage_insns[dst_regno].check == curr_usage_insns_check
5494 && (next_usage_insns
5495 = usage_insns[dst_regno].insns) != NULL_RTX)
5497 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5498 reloads_num++;
5499 update_reloads_num_p = false;
5500 /* 'original_pseudo <- reload_pseudo'. */
5501 if (! JUMP_P (curr_insn)
5502 && inherit_reload_reg (true, dst_regno, cl,
5503 curr_insn, next_usage_insns))
5504 change_p = true;
5505 /* Invalidate. */
5506 usage_insns[dst_regno].check = 0;
5507 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5508 IOR_HARD_REG_SET (potential_reload_hard_regs,
5509 reg_class_contents[cl]);
5511 else if (INSN_P (curr_insn))
5513 int iter;
5514 int max_uid = get_max_uid ();
5516 curr_id = lra_get_insn_recog_data (curr_insn);
5517 curr_static_id = curr_id->insn_static_data;
5518 to_inherit_num = 0;
5519 /* Process insn definitions. */
5520 for (iter = 0; iter < 2; iter++)
5521 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5522 reg != NULL;
5523 reg = reg->next)
5524 if (reg->type != OP_IN
5525 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5527 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5528 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5529 && usage_insns[dst_regno].check == curr_usage_insns_check
5530 && (next_usage_insns
5531 = usage_insns[dst_regno].insns) != NULL_RTX)
5533 struct lra_insn_reg *r;
5535 for (r = curr_id->regs; r != NULL; r = r->next)
5536 if (r->type != OP_OUT && r->regno == dst_regno)
5537 break;
5538 /* Don't do inheritance if the pseudo is also
5539 used in the insn. */
5540 if (r == NULL)
5541 /* We can not do inheritance right now
5542 because the current insn reg info (chain
5543 regs) can change after that. */
5544 add_to_inherit (dst_regno, next_usage_insns);
5546 /* We can not process one reg twice here because of
5547 usage_insns invalidation. */
5548 if ((dst_regno < FIRST_PSEUDO_REGISTER
5549 || reg_renumber[dst_regno] >= 0)
5550 && ! reg->subreg_p && reg->type != OP_IN)
5552 HARD_REG_SET s;
5554 if (split_if_necessary (dst_regno, reg->biggest_mode,
5555 potential_reload_hard_regs,
5556 false, curr_insn, max_uid))
5557 change_p = true;
5558 CLEAR_HARD_REG_SET (s);
5559 if (dst_regno < FIRST_PSEUDO_REGISTER)
5560 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5561 else
5562 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5563 reg_renumber[dst_regno]);
5564 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5566 /* We should invalidate potential inheritance or
5567 splitting for the current insn usages to the next
5568 usage insns (see code below) as the output pseudo
5569 prevents this. */
5570 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5571 && reg_renumber[dst_regno] < 0)
5572 || (reg->type == OP_OUT && ! reg->subreg_p
5573 && (dst_regno < FIRST_PSEUDO_REGISTER
5574 || reg_renumber[dst_regno] >= 0)))
5576 /* Invalidate and mark definitions. */
5577 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5578 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5579 else
5581 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5582 for (i = 0; i < nregs; i++)
5583 usage_insns[dst_regno + i].check
5584 = -(int) INSN_UID (curr_insn);
5588 /* Process clobbered call regs. */
5589 if (curr_id->arg_hard_regs != NULL)
5590 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5591 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5592 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
5593 = -(int) INSN_UID (curr_insn);
5594 if (! JUMP_P (curr_insn))
5595 for (i = 0; i < to_inherit_num; i++)
5596 if (inherit_reload_reg (true, to_inherit[i].regno,
5597 ALL_REGS, curr_insn,
5598 to_inherit[i].insns))
5599 change_p = true;
5600 if (CALL_P (curr_insn))
5602 rtx cheap, pat, dest;
5603 rtx_insn *restore;
5604 int regno, hard_regno;
5606 calls_num++;
5607 if ((cheap = find_reg_note (curr_insn,
5608 REG_RETURNED, NULL_RTX)) != NULL_RTX
5609 && ((cheap = XEXP (cheap, 0)), true)
5610 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5611 && (hard_regno = reg_renumber[regno]) >= 0
5612 /* If there are pending saves/restores, the
5613 optimization is not worth. */
5614 && usage_insns[regno].calls_num == calls_num - 1
5615 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5617 /* Restore the pseudo from the call result as
5618 REG_RETURNED note says that the pseudo value is
5619 in the call result and the pseudo is an argument
5620 of the call. */
5621 pat = PATTERN (curr_insn);
5622 if (GET_CODE (pat) == PARALLEL)
5623 pat = XVECEXP (pat, 0, 0);
5624 dest = SET_DEST (pat);
5625 /* For multiple return values dest is PARALLEL.
5626 Currently we handle only single return value case. */
5627 if (REG_P (dest))
5629 start_sequence ();
5630 emit_move_insn (cheap, copy_rtx (dest));
5631 restore = get_insns ();
5632 end_sequence ();
5633 lra_process_new_insns (curr_insn, NULL, restore,
5634 "Inserting call parameter restore");
5635 /* We don't need to save/restore of the pseudo from
5636 this call. */
5637 usage_insns[regno].calls_num = calls_num;
5638 bitmap_set_bit (&check_only_regs, regno);
5642 to_inherit_num = 0;
5643 /* Process insn usages. */
5644 for (iter = 0; iter < 2; iter++)
5645 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5646 reg != NULL;
5647 reg = reg->next)
5648 if ((reg->type != OP_OUT
5649 || (reg->type == OP_OUT && reg->subreg_p))
5650 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5652 if (src_regno >= FIRST_PSEUDO_REGISTER
5653 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5655 if (usage_insns[src_regno].check == curr_usage_insns_check
5656 && (next_usage_insns
5657 = usage_insns[src_regno].insns) != NULL_RTX
5658 && NONDEBUG_INSN_P (curr_insn))
5659 add_to_inherit (src_regno, next_usage_insns);
5660 else if (usage_insns[src_regno].check
5661 != -(int) INSN_UID (curr_insn))
5662 /* Add usages but only if the reg is not set up
5663 in the same insn. */
5664 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5666 else if (src_regno < FIRST_PSEUDO_REGISTER
5667 || reg_renumber[src_regno] >= 0)
5669 bool before_p;
5670 rtx_insn *use_insn = curr_insn;
5672 before_p = (JUMP_P (curr_insn)
5673 || (CALL_P (curr_insn) && reg->type == OP_IN));
5674 if (NONDEBUG_INSN_P (curr_insn)
5675 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5676 && split_if_necessary (src_regno, reg->biggest_mode,
5677 potential_reload_hard_regs,
5678 before_p, curr_insn, max_uid))
5680 if (reg->subreg_p)
5681 lra_risky_transformations_p = true;
5682 change_p = true;
5683 /* Invalidate. */
5684 usage_insns[src_regno].check = 0;
5685 if (before_p)
5686 use_insn = PREV_INSN (curr_insn);
5688 if (NONDEBUG_INSN_P (curr_insn))
5690 if (src_regno < FIRST_PSEUDO_REGISTER)
5691 add_to_hard_reg_set (&live_hard_regs,
5692 reg->biggest_mode, src_regno);
5693 else
5694 add_to_hard_reg_set (&live_hard_regs,
5695 PSEUDO_REGNO_MODE (src_regno),
5696 reg_renumber[src_regno]);
5698 add_next_usage_insn (src_regno, use_insn, reloads_num);
5701 /* Process used call regs. */
5702 if (curr_id->arg_hard_regs != NULL)
5703 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5704 if (src_regno < FIRST_PSEUDO_REGISTER)
5706 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5707 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5709 for (i = 0; i < to_inherit_num; i++)
5711 src_regno = to_inherit[i].regno;
5712 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5713 curr_insn, to_inherit[i].insns))
5714 change_p = true;
5715 else
5716 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5719 if (update_reloads_num_p
5720 && NONDEBUG_INSN_P (curr_insn)
5721 && (set = single_set (curr_insn)) != NULL_RTX)
5723 int regno = -1;
5724 if ((REG_P (SET_DEST (set))
5725 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5726 && reg_renumber[regno] < 0
5727 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5728 || (REG_P (SET_SRC (set))
5729 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5730 && reg_renumber[regno] < 0
5731 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5733 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5734 reloads_num++;
5735 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5736 IOR_HARD_REG_SET (potential_reload_hard_regs,
5737 reg_class_contents[cl]);
5740 /* We reached the start of the current basic block. */
5741 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5742 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5744 /* We reached the beginning of the current block -- do
5745 rest of spliting in the current BB. */
5746 to_process = df_get_live_in (curr_bb);
5747 if (BLOCK_FOR_INSN (head) != curr_bb)
5749 /* We are somewhere in the middle of EBB. */
5750 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5751 curr_bb, &temp_bitmap);
5752 to_process = &temp_bitmap;
5754 head_p = true;
5755 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5757 if ((int) j >= lra_constraint_new_regno_start)
5758 break;
5759 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5760 && usage_insns[j].check == curr_usage_insns_check
5761 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5763 if (need_for_split_p (potential_reload_hard_regs, j))
5765 if (lra_dump_file != NULL && head_p)
5767 fprintf (lra_dump_file,
5768 " ----------------------------------\n");
5769 head_p = false;
5771 if (split_reg (false, j, bb_note (curr_bb),
5772 next_usage_insns))
5773 change_p = true;
5775 usage_insns[j].check = 0;
5780 return change_p;
5783 /* This value affects EBB forming. If probability of edge from EBB to
5784 a BB is not greater than the following value, we don't add the BB
5785 to EBB. */
5786 #define EBB_PROBABILITY_CUTOFF \
5787 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
5789 /* Current number of inheritance/split iteration. */
5790 int lra_inheritance_iter;
5792 /* Entry function for inheritance/split pass. */
5793 void
5794 lra_inheritance (void)
5796 int i;
5797 basic_block bb, start_bb;
5798 edge e;
5800 lra_inheritance_iter++;
5801 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5802 return;
5803 timevar_push (TV_LRA_INHERITANCE);
5804 if (lra_dump_file != NULL)
5805 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5806 lra_inheritance_iter);
5807 curr_usage_insns_check = 0;
5808 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5809 for (i = 0; i < lra_constraint_new_regno_start; i++)
5810 usage_insns[i].check = 0;
5811 bitmap_initialize (&check_only_regs, &reg_obstack);
5812 bitmap_initialize (&live_regs, &reg_obstack);
5813 bitmap_initialize (&temp_bitmap, &reg_obstack);
5814 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5815 FOR_EACH_BB_FN (bb, cfun)
5817 start_bb = bb;
5818 if (lra_dump_file != NULL)
5819 fprintf (lra_dump_file, "EBB");
5820 /* Form a EBB starting with BB. */
5821 bitmap_clear (&ebb_global_regs);
5822 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5823 for (;;)
5825 if (lra_dump_file != NULL)
5826 fprintf (lra_dump_file, " %d", bb->index);
5827 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5828 || LABEL_P (BB_HEAD (bb->next_bb)))
5829 break;
5830 e = find_fallthru_edge (bb->succs);
5831 if (! e)
5832 break;
5833 if (e->probability < EBB_PROBABILITY_CUTOFF)
5834 break;
5835 bb = bb->next_bb;
5837 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5838 if (lra_dump_file != NULL)
5839 fprintf (lra_dump_file, "\n");
5840 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5841 /* Remember that the EBB head and tail can change in
5842 inherit_in_ebb. */
5843 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5845 bitmap_clear (&ebb_global_regs);
5846 bitmap_clear (&temp_bitmap);
5847 bitmap_clear (&live_regs);
5848 bitmap_clear (&check_only_regs);
5849 free (usage_insns);
5851 timevar_pop (TV_LRA_INHERITANCE);
5856 /* This page contains code to undo failed inheritance/split
5857 transformations. */
5859 /* Current number of iteration undoing inheritance/split. */
5860 int lra_undo_inheritance_iter;
5862 /* Fix BB live info LIVE after removing pseudos created on pass doing
5863 inheritance/split which are REMOVED_PSEUDOS. */
5864 static void
5865 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5867 unsigned int regno;
5868 bitmap_iterator bi;
5870 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5871 if (bitmap_clear_bit (live, regno))
5872 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5875 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5876 number. */
5877 static int
5878 get_regno (rtx reg)
5880 if (GET_CODE (reg) == SUBREG)
5881 reg = SUBREG_REG (reg);
5882 if (REG_P (reg))
5883 return REGNO (reg);
5884 return -1;
5887 /* Delete a move INSN with destination reg DREGNO and a previous
5888 clobber insn with the same regno. The inheritance/split code can
5889 generate moves with preceding clobber and when we delete such moves
5890 we should delete the clobber insn too to keep the correct life
5891 info. */
5892 static void
5893 delete_move_and_clobber (rtx_insn *insn, int dregno)
5895 rtx_insn *prev_insn = PREV_INSN (insn);
5897 lra_set_insn_deleted (insn);
5898 lra_assert (dregno >= 0);
5899 if (prev_insn != NULL && NONDEBUG_INSN_P (prev_insn)
5900 && GET_CODE (PATTERN (prev_insn)) == CLOBBER
5901 && dregno == get_regno (XEXP (PATTERN (prev_insn), 0)))
5902 lra_set_insn_deleted (prev_insn);
5905 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5906 return true if we did any change. The undo transformations for
5907 inheritance looks like
5908 i <- i2
5909 p <- i => p <- i2
5910 or removing
5911 p <- i, i <- p, and i <- i3
5912 where p is original pseudo from which inheritance pseudo i was
5913 created, i and i3 are removed inheritance pseudos, i2 is another
5914 not removed inheritance pseudo. All split pseudos or other
5915 occurrences of removed inheritance pseudos are changed on the
5916 corresponding original pseudos.
5918 The function also schedules insns changed and created during
5919 inheritance/split pass for processing by the subsequent constraint
5920 pass. */
5921 static bool
5922 remove_inheritance_pseudos (bitmap remove_pseudos)
5924 basic_block bb;
5925 int regno, sregno, prev_sregno, dregno, restore_regno;
5926 rtx set, prev_set;
5927 rtx_insn *prev_insn;
5928 bool change_p, done_p;
5930 change_p = ! bitmap_empty_p (remove_pseudos);
5931 /* We can not finish the function right away if CHANGE_P is true
5932 because we need to marks insns affected by previous
5933 inheritance/split pass for processing by the subsequent
5934 constraint pass. */
5935 FOR_EACH_BB_FN (bb, cfun)
5937 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5938 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5939 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5941 if (! INSN_P (curr_insn))
5942 continue;
5943 done_p = false;
5944 sregno = dregno = -1;
5945 if (change_p && NONDEBUG_INSN_P (curr_insn)
5946 && (set = single_set (curr_insn)) != NULL_RTX)
5948 dregno = get_regno (SET_DEST (set));
5949 sregno = get_regno (SET_SRC (set));
5952 if (sregno >= 0 && dregno >= 0)
5954 if ((bitmap_bit_p (remove_pseudos, sregno)
5955 && (lra_reg_info[sregno].restore_regno == dregno
5956 || (bitmap_bit_p (remove_pseudos, dregno)
5957 && (lra_reg_info[sregno].restore_regno
5958 == lra_reg_info[dregno].restore_regno))))
5959 || (bitmap_bit_p (remove_pseudos, dregno)
5960 && lra_reg_info[dregno].restore_regno == sregno))
5961 /* One of the following cases:
5962 original <- removed inheritance pseudo
5963 removed inherit pseudo <- another removed inherit pseudo
5964 removed inherit pseudo <- original pseudo
5966 removed_split_pseudo <- original_reg
5967 original_reg <- removed_split_pseudo */
5969 if (lra_dump_file != NULL)
5971 fprintf (lra_dump_file, " Removing %s:\n",
5972 bitmap_bit_p (&lra_split_regs, sregno)
5973 || bitmap_bit_p (&lra_split_regs, dregno)
5974 ? "split" : "inheritance");
5975 dump_insn_slim (lra_dump_file, curr_insn);
5977 delete_move_and_clobber (curr_insn, dregno);
5978 done_p = true;
5980 else if (bitmap_bit_p (remove_pseudos, sregno)
5981 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5983 /* Search the following pattern:
5984 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5985 original_pseudo <- inherit_or_split_pseudo1
5986 where the 2nd insn is the current insn and
5987 inherit_or_split_pseudo2 is not removed. If it is found,
5988 change the current insn onto:
5989 original_pseudo <- inherit_or_split_pseudo2. */
5990 for (prev_insn = PREV_INSN (curr_insn);
5991 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5992 prev_insn = PREV_INSN (prev_insn))
5994 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5995 && (prev_set = single_set (prev_insn)) != NULL_RTX
5996 /* There should be no subregs in insn we are
5997 searching because only the original reg might
5998 be in subreg when we changed the mode of
5999 load/store for splitting. */
6000 && REG_P (SET_DEST (prev_set))
6001 && REG_P (SET_SRC (prev_set))
6002 && (int) REGNO (SET_DEST (prev_set)) == sregno
6003 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
6004 >= FIRST_PSEUDO_REGISTER)
6005 /* As we consider chain of inheritance or
6006 splitting described in above comment we should
6007 check that sregno and prev_sregno were
6008 inheritance/split pseudos created from the
6009 same original regno. */
6010 && (lra_reg_info[sregno].restore_regno
6011 == lra_reg_info[prev_sregno].restore_regno)
6012 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
6014 lra_assert (GET_MODE (SET_SRC (prev_set))
6015 == GET_MODE (regno_reg_rtx[sregno]));
6016 if (GET_CODE (SET_SRC (set)) == SUBREG)
6017 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
6018 else
6019 SET_SRC (set) = SET_SRC (prev_set);
6020 /* As we are finishing with processing the insn
6021 here, check the destination too as it might
6022 inheritance pseudo for another pseudo. */
6023 if (bitmap_bit_p (remove_pseudos, dregno)
6024 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
6025 && (restore_regno
6026 = lra_reg_info[dregno].restore_regno) >= 0)
6028 if (GET_CODE (SET_DEST (set)) == SUBREG)
6029 SUBREG_REG (SET_DEST (set))
6030 = regno_reg_rtx[restore_regno];
6031 else
6032 SET_DEST (set) = regno_reg_rtx[restore_regno];
6034 lra_push_insn_and_update_insn_regno_info (curr_insn);
6035 lra_set_used_insn_alternative_by_uid
6036 (INSN_UID (curr_insn), -1);
6037 done_p = true;
6038 if (lra_dump_file != NULL)
6040 fprintf (lra_dump_file, " Change reload insn:\n");
6041 dump_insn_slim (lra_dump_file, curr_insn);
6046 if (! done_p)
6048 struct lra_insn_reg *reg;
6049 bool restored_regs_p = false;
6050 bool kept_regs_p = false;
6052 curr_id = lra_get_insn_recog_data (curr_insn);
6053 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6055 regno = reg->regno;
6056 restore_regno = lra_reg_info[regno].restore_regno;
6057 if (restore_regno >= 0)
6059 if (change_p && bitmap_bit_p (remove_pseudos, regno))
6061 lra_substitute_pseudo_within_insn
6062 (curr_insn, regno, regno_reg_rtx[restore_regno],
6063 false);
6064 restored_regs_p = true;
6066 else
6067 kept_regs_p = true;
6070 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
6072 /* The instruction has changed since the previous
6073 constraints pass. */
6074 lra_push_insn_and_update_insn_regno_info (curr_insn);
6075 lra_set_used_insn_alternative_by_uid
6076 (INSN_UID (curr_insn), -1);
6078 else if (restored_regs_p)
6079 /* The instruction has been restored to the form that
6080 it had during the previous constraints pass. */
6081 lra_update_insn_regno_info (curr_insn);
6082 if (restored_regs_p && lra_dump_file != NULL)
6084 fprintf (lra_dump_file, " Insn after restoring regs:\n");
6085 dump_insn_slim (lra_dump_file, curr_insn);
6090 return change_p;
6093 /* If optional reload pseudos failed to get a hard register or was not
6094 inherited, it is better to remove optional reloads. We do this
6095 transformation after undoing inheritance to figure out necessity to
6096 remove optional reloads easier. Return true if we do any
6097 change. */
6098 static bool
6099 undo_optional_reloads (void)
6101 bool change_p, keep_p;
6102 unsigned int regno, uid;
6103 bitmap_iterator bi, bi2;
6104 rtx_insn *insn;
6105 rtx set, src, dest;
6106 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
6108 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
6109 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
6110 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6112 keep_p = false;
6113 /* Keep optional reloads from previous subpasses. */
6114 if (lra_reg_info[regno].restore_regno < 0
6115 /* If the original pseudo changed its allocation, just
6116 removing the optional pseudo is dangerous as the original
6117 pseudo will have longer live range. */
6118 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
6119 keep_p = true;
6120 else if (reg_renumber[regno] >= 0)
6121 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
6123 insn = lra_insn_recog_data[uid]->insn;
6124 if ((set = single_set (insn)) == NULL_RTX)
6125 continue;
6126 src = SET_SRC (set);
6127 dest = SET_DEST (set);
6128 if (! REG_P (src) || ! REG_P (dest))
6129 continue;
6130 if (REGNO (dest) == regno
6131 /* Ignore insn for optional reloads itself. */
6132 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
6133 /* Check only inheritance on last inheritance pass. */
6134 && (int) REGNO (src) >= new_regno_start
6135 /* Check that the optional reload was inherited. */
6136 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6138 keep_p = true;
6139 break;
6142 if (keep_p)
6144 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6145 if (lra_dump_file != NULL)
6146 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6149 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6150 bitmap_initialize (&insn_bitmap, &reg_obstack);
6151 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6153 if (lra_dump_file != NULL)
6154 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6155 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6156 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6158 insn = lra_insn_recog_data[uid]->insn;
6159 if ((set = single_set (insn)) != NULL_RTX)
6161 src = SET_SRC (set);
6162 dest = SET_DEST (set);
6163 if (REG_P (src) && REG_P (dest)
6164 && ((REGNO (src) == regno
6165 && (lra_reg_info[regno].restore_regno
6166 == (int) REGNO (dest)))
6167 || (REGNO (dest) == regno
6168 && (lra_reg_info[regno].restore_regno
6169 == (int) REGNO (src)))))
6171 if (lra_dump_file != NULL)
6173 fprintf (lra_dump_file, " Deleting move %u\n",
6174 INSN_UID (insn));
6175 dump_insn_slim (lra_dump_file, insn);
6177 delete_move_and_clobber (insn, REGNO (dest));
6178 continue;
6180 /* We should not worry about generation memory-memory
6181 moves here as if the corresponding inheritance did
6182 not work (inheritance pseudo did not get a hard reg),
6183 we remove the inheritance pseudo and the optional
6184 reload. */
6186 lra_substitute_pseudo_within_insn
6187 (insn, regno, regno_reg_rtx[lra_reg_info[regno].restore_regno],
6188 false);
6189 lra_update_insn_regno_info (insn);
6190 if (lra_dump_file != NULL)
6192 fprintf (lra_dump_file,
6193 " Restoring original insn:\n");
6194 dump_insn_slim (lra_dump_file, insn);
6198 /* Clear restore_regnos. */
6199 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6200 lra_reg_info[regno].restore_regno = -1;
6201 bitmap_clear (&insn_bitmap);
6202 bitmap_clear (&removed_optional_reload_pseudos);
6203 return change_p;
6206 /* Entry function for undoing inheritance/split transformation. Return true
6207 if we did any RTL change in this pass. */
6208 bool
6209 lra_undo_inheritance (void)
6211 unsigned int regno;
6212 int restore_regno, hard_regno;
6213 int n_all_inherit, n_inherit, n_all_split, n_split;
6214 bitmap_head remove_pseudos;
6215 bitmap_iterator bi;
6216 bool change_p;
6218 lra_undo_inheritance_iter++;
6219 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6220 return false;
6221 if (lra_dump_file != NULL)
6222 fprintf (lra_dump_file,
6223 "\n********** Undoing inheritance #%d: **********\n\n",
6224 lra_undo_inheritance_iter);
6225 bitmap_initialize (&remove_pseudos, &reg_obstack);
6226 n_inherit = n_all_inherit = 0;
6227 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6228 if (lra_reg_info[regno].restore_regno >= 0)
6230 n_all_inherit++;
6231 if (reg_renumber[regno] < 0
6232 /* If the original pseudo changed its allocation, just
6233 removing inheritance is dangerous as for changing
6234 allocation we used shorter live-ranges. */
6235 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6236 bitmap_set_bit (&remove_pseudos, regno);
6237 else
6238 n_inherit++;
6240 if (lra_dump_file != NULL && n_all_inherit != 0)
6241 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6242 n_inherit, n_all_inherit,
6243 (double) n_inherit / n_all_inherit * 100);
6244 n_split = n_all_split = 0;
6245 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6246 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6248 n_all_split++;
6249 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6250 ? reg_renumber[restore_regno] : restore_regno);
6251 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6252 bitmap_set_bit (&remove_pseudos, regno);
6253 else
6255 n_split++;
6256 if (lra_dump_file != NULL)
6257 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6258 regno, restore_regno);
6261 if (lra_dump_file != NULL && n_all_split != 0)
6262 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6263 n_split, n_all_split,
6264 (double) n_split / n_all_split * 100);
6265 change_p = remove_inheritance_pseudos (&remove_pseudos);
6266 bitmap_clear (&remove_pseudos);
6267 /* Clear restore_regnos. */
6268 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6269 lra_reg_info[regno].restore_regno = -1;
6270 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6271 lra_reg_info[regno].restore_regno = -1;
6272 change_p = undo_optional_reloads () || change_p;
6273 return change_p;