* Add TARGET_LEGITIMIZE_ADDRESS_DISPLACEMENT target macro.
[official-gcc.git] / gcc / lra-constraints.c
blob382281c8dc802262330c53c69506c85d4dda9d55
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2014 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 "tm.h"
113 #include "hard-reg-set.h"
114 #include "rtl.h"
115 #include "tm_p.h"
116 #include "regs.h"
117 #include "insn-config.h"
118 #include "insn-codes.h"
119 #include "recog.h"
120 #include "output.h"
121 #include "addresses.h"
122 #include "target.h"
123 #include "hashtab.h"
124 #include "hash-set.h"
125 #include "vec.h"
126 #include "machmode.h"
127 #include "input.h"
128 #include "function.h"
129 #include "expr.h"
130 #include "predict.h"
131 #include "dominance.h"
132 #include "cfg.h"
133 #include "cfgrtl.h"
134 #include "basic-block.h"
135 #include "except.h"
136 #include "optabs.h"
137 #include "df.h"
138 #include "ira.h"
139 #include "rtl-error.h"
140 #include "lra-int.h"
142 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
143 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
144 reload insns. */
145 static int bb_reload_num;
147 /* The current insn being processed and corresponding its single set
148 (NULL otherwise), its data (basic block, the insn data, the insn
149 static data, and the mode of each operand). */
150 static rtx_insn *curr_insn;
151 static rtx curr_insn_set;
152 static basic_block curr_bb;
153 static lra_insn_recog_data_t curr_id;
154 static struct lra_static_insn_data *curr_static_id;
155 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
159 /* Start numbers for new registers and insns at the current constraints
160 pass start. */
161 static int new_regno_start;
162 static int new_insn_uid_start;
164 /* If LOC is nonnull, strip any outer subreg from it. */
165 static inline rtx *
166 strip_subreg (rtx *loc)
168 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
171 /* Return hard regno of REGNO or if it is was not assigned to a hard
172 register, use a hard register from its allocno class. */
173 static int
174 get_try_hard_regno (int regno)
176 int hard_regno;
177 enum reg_class rclass;
179 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
180 hard_regno = lra_get_regno_hard_regno (regno);
181 if (hard_regno >= 0)
182 return hard_regno;
183 rclass = lra_get_allocno_class (regno);
184 if (rclass == NO_REGS)
185 return -1;
186 return ira_class_hard_regs[rclass][0];
189 /* Return final hard regno (plus offset) which will be after
190 elimination. We do this for matching constraints because the final
191 hard regno could have a different class. */
192 static int
193 get_final_hard_regno (int hard_regno, int offset)
195 if (hard_regno < 0)
196 return hard_regno;
197 hard_regno = lra_get_elimination_hard_regno (hard_regno);
198 return hard_regno + offset;
201 /* Return hard regno of X after removing subreg and making
202 elimination. If X is not a register or subreg of register, return
203 -1. For pseudo use its assignment. */
204 static int
205 get_hard_regno (rtx x)
207 rtx reg;
208 int offset, hard_regno;
210 reg = x;
211 if (GET_CODE (x) == SUBREG)
212 reg = SUBREG_REG (x);
213 if (! REG_P (reg))
214 return -1;
215 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
216 hard_regno = lra_get_regno_hard_regno (hard_regno);
217 if (hard_regno < 0)
218 return -1;
219 offset = 0;
220 if (GET_CODE (x) == SUBREG)
221 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
222 SUBREG_BYTE (x), GET_MODE (x));
223 return get_final_hard_regno (hard_regno, offset);
226 /* If REGNO is a hard register or has been allocated a hard register,
227 return the class of that register. If REGNO is a reload pseudo
228 created by the current constraints pass, return its allocno class.
229 Return NO_REGS otherwise. */
230 static enum reg_class
231 get_reg_class (int regno)
233 int hard_regno;
235 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
236 hard_regno = lra_get_regno_hard_regno (regno);
237 if (hard_regno >= 0)
239 hard_regno = get_final_hard_regno (hard_regno, 0);
240 return REGNO_REG_CLASS (hard_regno);
242 if (regno >= new_regno_start)
243 return lra_get_allocno_class (regno);
244 return NO_REGS;
247 /* Return true if REG satisfies (or will satisfy) reg class constraint
248 CL. Use elimination first if REG is a hard register. If REG is a
249 reload pseudo created by this constraints pass, assume that it will
250 be allocated a hard register from its allocno class, but allow that
251 class to be narrowed to CL if it is currently a superset of CL.
253 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
254 REGNO (reg), or NO_REGS if no change in its class was needed. */
255 static bool
256 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
258 enum reg_class rclass, common_class;
259 machine_mode reg_mode;
260 int class_size, hard_regno, nregs, i, j;
261 int regno = REGNO (reg);
263 if (new_class != NULL)
264 *new_class = NO_REGS;
265 if (regno < FIRST_PSEUDO_REGISTER)
267 rtx final_reg = reg;
268 rtx *final_loc = &final_reg;
270 lra_eliminate_reg_if_possible (final_loc);
271 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
273 reg_mode = GET_MODE (reg);
274 rclass = get_reg_class (regno);
275 if (regno < new_regno_start
276 /* Do not allow the constraints for reload instructions to
277 influence the classes of new pseudos. These reloads are
278 typically moves that have many alternatives, and restricting
279 reload pseudos for one alternative may lead to situations
280 where other reload pseudos are no longer allocatable. */
281 || (INSN_UID (curr_insn) >= new_insn_uid_start
282 && curr_insn_set != NULL
283 && ((OBJECT_P (SET_SRC (curr_insn_set))
284 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
285 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
286 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
287 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
288 /* When we don't know what class will be used finally for reload
289 pseudos, we use ALL_REGS. */
290 return ((regno >= new_regno_start && rclass == ALL_REGS)
291 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
292 && ! hard_reg_set_subset_p (reg_class_contents[cl],
293 lra_no_alloc_regs)));
294 else
296 common_class = ira_reg_class_subset[rclass][cl];
297 if (new_class != NULL)
298 *new_class = common_class;
299 if (hard_reg_set_subset_p (reg_class_contents[common_class],
300 lra_no_alloc_regs))
301 return false;
302 /* Check that there are enough allocatable regs. */
303 class_size = ira_class_hard_regs_num[common_class];
304 for (i = 0; i < class_size; i++)
306 hard_regno = ira_class_hard_regs[common_class][i];
307 nregs = hard_regno_nregs[hard_regno][reg_mode];
308 if (nregs == 1)
309 return true;
310 for (j = 0; j < nregs; j++)
311 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
312 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
313 hard_regno + j))
314 break;
315 if (j >= nregs)
316 return true;
318 return false;
322 /* Return true if REGNO satisfies a memory constraint. */
323 static bool
324 in_mem_p (int regno)
326 return get_reg_class (regno) == NO_REGS;
329 /* Return 1 if ADDR is a valid memory address for mode MODE in address
330 space AS, and check that each pseudo has the proper kind of hard
331 reg. */
332 static int
333 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
334 rtx addr, addr_space_t as)
336 #ifdef GO_IF_LEGITIMATE_ADDRESS
337 lra_assert (ADDR_SPACE_GENERIC_P (as));
338 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
339 return 0;
341 win:
342 return 1;
343 #else
344 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
345 #endif
348 namespace {
349 /* Temporarily eliminates registers in an address (for the lifetime of
350 the object). */
351 class address_eliminator {
352 public:
353 address_eliminator (struct address_info *ad);
354 ~address_eliminator ();
356 private:
357 struct address_info *m_ad;
358 rtx *m_base_loc;
359 rtx m_base_reg;
360 rtx *m_index_loc;
361 rtx m_index_reg;
365 address_eliminator::address_eliminator (struct address_info *ad)
366 : m_ad (ad),
367 m_base_loc (strip_subreg (ad->base_term)),
368 m_base_reg (NULL_RTX),
369 m_index_loc (strip_subreg (ad->index_term)),
370 m_index_reg (NULL_RTX)
372 if (m_base_loc != NULL)
374 m_base_reg = *m_base_loc;
375 lra_eliminate_reg_if_possible (m_base_loc);
376 if (m_ad->base_term2 != NULL)
377 *m_ad->base_term2 = *m_ad->base_term;
379 if (m_index_loc != NULL)
381 m_index_reg = *m_index_loc;
382 lra_eliminate_reg_if_possible (m_index_loc);
386 address_eliminator::~address_eliminator ()
388 if (m_base_loc && *m_base_loc != m_base_reg)
390 *m_base_loc = m_base_reg;
391 if (m_ad->base_term2 != NULL)
392 *m_ad->base_term2 = *m_ad->base_term;
394 if (m_index_loc && *m_index_loc != m_index_reg)
395 *m_index_loc = m_index_reg;
398 /* Return true if the eliminated form of AD is a legitimate target address. */
399 static bool
400 valid_address_p (struct address_info *ad)
402 address_eliminator eliminator (ad);
403 return valid_address_p (ad->mode, *ad->outer, ad->as);
406 /* Return true if the eliminated form of memory reference OP satisfies
407 extra memory constraint CONSTRAINT. */
408 static bool
409 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
411 struct address_info ad;
413 decompose_mem_address (&ad, op);
414 address_eliminator eliminator (&ad);
415 return constraint_satisfied_p (op, constraint);
418 /* Return true if the eliminated form of address AD satisfies extra
419 address constraint CONSTRAINT. */
420 static bool
421 satisfies_address_constraint_p (struct address_info *ad,
422 enum constraint_num constraint)
424 address_eliminator eliminator (ad);
425 return constraint_satisfied_p (*ad->outer, constraint);
428 /* Return true if the eliminated form of address OP satisfies extra
429 address constraint CONSTRAINT. */
430 static bool
431 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
433 struct address_info ad;
435 decompose_lea_address (&ad, &op);
436 return satisfies_address_constraint_p (&ad, constraint);
439 /* Initiate equivalences for LRA. As we keep original equivalences
440 before any elimination, we need to make copies otherwise any change
441 in insns might change the equivalences. */
442 void
443 lra_init_equiv (void)
445 ira_expand_reg_equiv ();
446 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
448 rtx res;
450 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
451 ira_reg_equiv[i].memory = copy_rtx (res);
452 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
453 ira_reg_equiv[i].invariant = copy_rtx (res);
457 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
459 /* Update equivalence for REGNO. We need to this as the equivalence
460 might contain other pseudos which are changed by their
461 equivalences. */
462 static void
463 update_equiv (int regno)
465 rtx x;
467 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
468 ira_reg_equiv[regno].memory
469 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
470 NULL_RTX);
471 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
472 ira_reg_equiv[regno].invariant
473 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
474 NULL_RTX);
477 /* If we have decided to substitute X with another value, return that
478 value, otherwise return X. */
479 static rtx
480 get_equiv (rtx x)
482 int regno;
483 rtx res;
485 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
486 || ! ira_reg_equiv[regno].defined_p
487 || ! ira_reg_equiv[regno].profitable_p
488 || lra_get_regno_hard_regno (regno) >= 0)
489 return x;
490 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
492 if (targetm.cannot_substitute_mem_equiv_p (res))
493 return x;
494 return res;
496 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
497 return res;
498 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
499 return res;
500 gcc_unreachable ();
503 /* If we have decided to substitute X with the equivalent value,
504 return that value after elimination for INSN, otherwise return
505 X. */
506 static rtx
507 get_equiv_with_elimination (rtx x, rtx_insn *insn)
509 rtx res = get_equiv (x);
511 if (x == res || CONSTANT_P (res))
512 return res;
513 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
514 0, false, false, true);
517 /* Set up curr_operand_mode. */
518 static void
519 init_curr_operand_mode (void)
521 int nop = curr_static_id->n_operands;
522 for (int i = 0; i < nop; i++)
524 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
525 if (mode == VOIDmode)
527 /* The .md mode for address operands is the mode of the
528 addressed value rather than the mode of the address itself. */
529 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
530 mode = Pmode;
531 else
532 mode = curr_static_id->operand[i].mode;
534 curr_operand_mode[i] = mode;
540 /* The page contains code to reuse input reloads. */
542 /* Structure describes input reload of the current insns. */
543 struct input_reload
545 /* Reloaded value. */
546 rtx input;
547 /* Reload pseudo used. */
548 rtx reg;
551 /* The number of elements in the following array. */
552 static int curr_insn_input_reloads_num;
553 /* Array containing info about input reloads. It is used to find the
554 same input reload and reuse the reload pseudo in this case. */
555 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
557 /* Initiate data concerning reuse of input reloads for the current
558 insn. */
559 static void
560 init_curr_insn_input_reloads (void)
562 curr_insn_input_reloads_num = 0;
565 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
566 created input reload pseudo (only if TYPE is not OP_OUT). Don't
567 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
568 wrapped up in SUBREG. The result pseudo is returned through
569 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
570 reused the already created input reload pseudo. Use TITLE to
571 describe new registers for debug purposes. */
572 static bool
573 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
574 enum reg_class rclass, bool in_subreg_p,
575 const char *title, rtx *result_reg)
577 int i, regno;
578 enum reg_class new_class;
580 if (type == OP_OUT)
582 *result_reg
583 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
584 return true;
586 /* Prevent reuse value of expression with side effects,
587 e.g. volatile memory. */
588 if (! side_effects_p (original))
589 for (i = 0; i < curr_insn_input_reloads_num; i++)
590 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
591 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
593 rtx reg = curr_insn_input_reloads[i].reg;
594 regno = REGNO (reg);
595 /* If input is equal to original and both are VOIDmode,
596 GET_MODE (reg) might be still different from mode.
597 Ensure we don't return *result_reg with wrong mode. */
598 if (GET_MODE (reg) != mode)
600 if (in_subreg_p)
601 continue;
602 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
603 continue;
604 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
605 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
606 continue;
608 *result_reg = reg;
609 if (lra_dump_file != NULL)
611 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
612 dump_value_slim (lra_dump_file, original, 1);
614 if (new_class != lra_get_allocno_class (regno))
615 lra_change_class (regno, new_class, ", change to", false);
616 if (lra_dump_file != NULL)
617 fprintf (lra_dump_file, "\n");
618 return false;
620 *result_reg = lra_create_new_reg (mode, original, rclass, title);
621 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
622 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
623 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
624 return true;
629 /* The page contains code to extract memory address parts. */
631 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
632 static inline bool
633 ok_for_index_p_nonstrict (rtx reg)
635 unsigned regno = REGNO (reg);
637 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
640 /* A version of regno_ok_for_base_p for use here, when all pseudos
641 should count as OK. Arguments as for regno_ok_for_base_p. */
642 static inline bool
643 ok_for_base_p_nonstrict (rtx reg, machine_mode mode, addr_space_t as,
644 enum rtx_code outer_code, enum rtx_code index_code)
646 unsigned regno = REGNO (reg);
648 if (regno >= FIRST_PSEUDO_REGISTER)
649 return true;
650 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
655 /* The page contains major code to choose the current insn alternative
656 and generate reloads for it. */
658 /* Return the offset from REGNO of the least significant register
659 in (reg:MODE REGNO).
661 This function is used to tell whether two registers satisfy
662 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
664 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
665 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
667 lra_constraint_offset (int regno, machine_mode mode)
669 lra_assert (regno < FIRST_PSEUDO_REGISTER);
670 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
671 && SCALAR_INT_MODE_P (mode))
672 return hard_regno_nregs[regno][mode] - 1;
673 return 0;
676 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
677 if they are the same hard reg, and has special hacks for
678 auto-increment and auto-decrement. This is specifically intended for
679 process_alt_operands to use in determining whether two operands
680 match. X is the operand whose number is the lower of the two.
682 It is supposed that X is the output operand and Y is the input
683 operand. Y_HARD_REGNO is the final hard regno of register Y or
684 register in subreg Y as we know it now. Otherwise, it is a
685 negative value. */
686 static bool
687 operands_match_p (rtx x, rtx y, int y_hard_regno)
689 int i;
690 RTX_CODE code = GET_CODE (x);
691 const char *fmt;
693 if (x == y)
694 return true;
695 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
696 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
698 int j;
700 i = get_hard_regno (x);
701 if (i < 0)
702 goto slow;
704 if ((j = y_hard_regno) < 0)
705 goto slow;
707 i += lra_constraint_offset (i, GET_MODE (x));
708 j += lra_constraint_offset (j, GET_MODE (y));
710 return i == j;
713 /* If two operands must match, because they are really a single
714 operand of an assembler insn, then two post-increments are invalid
715 because the assembler insn would increment only once. On the
716 other hand, a post-increment matches ordinary indexing if the
717 post-increment is the output operand. */
718 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
719 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
721 /* Two pre-increments are invalid because the assembler insn would
722 increment only once. On the other hand, a pre-increment matches
723 ordinary indexing if the pre-increment is the input operand. */
724 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
725 || GET_CODE (y) == PRE_MODIFY)
726 return operands_match_p (x, XEXP (y, 0), -1);
728 slow:
730 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
731 && x == SUBREG_REG (y))
732 return true;
733 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
734 && SUBREG_REG (x) == y)
735 return true;
737 /* Now we have disposed of all the cases in which different rtx
738 codes can match. */
739 if (code != GET_CODE (y))
740 return false;
742 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
743 if (GET_MODE (x) != GET_MODE (y))
744 return false;
746 switch (code)
748 CASE_CONST_UNIQUE:
749 return false;
751 case LABEL_REF:
752 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
753 case SYMBOL_REF:
754 return XSTR (x, 0) == XSTR (y, 0);
756 default:
757 break;
760 /* Compare the elements. If any pair of corresponding elements fail
761 to match, return false for the whole things. */
763 fmt = GET_RTX_FORMAT (code);
764 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
766 int val, j;
767 switch (fmt[i])
769 case 'w':
770 if (XWINT (x, i) != XWINT (y, i))
771 return false;
772 break;
774 case 'i':
775 if (XINT (x, i) != XINT (y, i))
776 return false;
777 break;
779 case 'e':
780 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
781 if (val == 0)
782 return false;
783 break;
785 case '0':
786 break;
788 case 'E':
789 if (XVECLEN (x, i) != XVECLEN (y, i))
790 return false;
791 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
793 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
794 if (val == 0)
795 return false;
797 break;
799 /* It is believed that rtx's at this level will never
800 contain anything but integers and other rtx's, except for
801 within LABEL_REFs and SYMBOL_REFs. */
802 default:
803 gcc_unreachable ();
806 return true;
809 /* True if X is a constant that can be forced into the constant pool.
810 MODE is the mode of the operand, or VOIDmode if not known. */
811 #define CONST_POOL_OK_P(MODE, X) \
812 ((MODE) != VOIDmode \
813 && CONSTANT_P (X) \
814 && GET_CODE (X) != HIGH \
815 && !targetm.cannot_force_const_mem (MODE, X))
817 /* True if C is a non-empty register class that has too few registers
818 to be safely used as a reload target class. */
819 #define SMALL_REGISTER_CLASS_P(C) \
820 (ira_class_hard_regs_num [(C)] == 1 \
821 || (ira_class_hard_regs_num [(C)] >= 1 \
822 && targetm.class_likely_spilled_p (C)))
824 /* If REG is a reload pseudo, try to make its class satisfying CL. */
825 static void
826 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
828 enum reg_class rclass;
830 /* Do not make more accurate class from reloads generated. They are
831 mostly moves with a lot of constraints. Making more accurate
832 class may results in very narrow class and impossibility of find
833 registers for several reloads of one insn. */
834 if (INSN_UID (curr_insn) >= new_insn_uid_start)
835 return;
836 if (GET_CODE (reg) == SUBREG)
837 reg = SUBREG_REG (reg);
838 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
839 return;
840 if (in_class_p (reg, cl, &rclass) && rclass != cl)
841 lra_change_class (REGNO (reg), rclass, " Change to", true);
844 /* Generate reloads for matching OUT and INS (array of input operand
845 numbers with end marker -1) with reg class GOAL_CLASS. Add input
846 and output reloads correspondingly to the lists *BEFORE and *AFTER.
847 OUT might be negative. In this case we generate input reloads for
848 matched input operands INS. */
849 static void
850 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
851 rtx_insn **before, rtx_insn **after)
853 int i, in;
854 rtx new_in_reg, new_out_reg, reg, clobber;
855 machine_mode inmode, outmode;
856 rtx in_rtx = *curr_id->operand_loc[ins[0]];
857 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
859 inmode = curr_operand_mode[ins[0]];
860 outmode = out < 0 ? inmode : curr_operand_mode[out];
861 push_to_sequence (*before);
862 if (inmode != outmode)
864 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
866 reg = new_in_reg
867 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
868 goal_class, "");
869 if (SCALAR_INT_MODE_P (inmode))
870 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
871 else
872 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
873 LRA_SUBREG_P (new_out_reg) = 1;
874 /* If the input reg is dying here, we can use the same hard
875 register for REG and IN_RTX. We do it only for original
876 pseudos as reload pseudos can die although original
877 pseudos still live where reload pseudos dies. */
878 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
879 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
880 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
882 else
884 reg = new_out_reg
885 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
886 goal_class, "");
887 if (SCALAR_INT_MODE_P (outmode))
888 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
889 else
890 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
891 /* NEW_IN_REG is non-paradoxical subreg. We don't want
892 NEW_OUT_REG living above. We add clobber clause for
893 this. This is just a temporary clobber. We can remove
894 it at the end of LRA work. */
895 clobber = emit_clobber (new_out_reg);
896 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
897 LRA_SUBREG_P (new_in_reg) = 1;
898 if (GET_CODE (in_rtx) == SUBREG)
900 rtx subreg_reg = SUBREG_REG (in_rtx);
902 /* If SUBREG_REG is dying here and sub-registers IN_RTX
903 and NEW_IN_REG are similar, we can use the same hard
904 register for REG and SUBREG_REG. */
905 if (REG_P (subreg_reg)
906 && (int) REGNO (subreg_reg) < lra_new_regno_start
907 && GET_MODE (subreg_reg) == outmode
908 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
909 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
910 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
914 else
916 /* Pseudos have values -- see comments for lra_reg_info.
917 Different pseudos with the same value do not conflict even if
918 they live in the same place. When we create a pseudo we
919 assign value of original pseudo (if any) from which we
920 created the new pseudo. If we create the pseudo from the
921 input pseudo, the new pseudo will no conflict with the input
922 pseudo which is wrong when the input pseudo lives after the
923 insn and as the new pseudo value is changed by the insn
924 output. Therefore we create the new pseudo from the output.
926 We cannot reuse the current output register because we might
927 have a situation like "a <- a op b", where the constraints
928 force the second input operand ("b") to match the output
929 operand ("a"). "b" must then be copied into a new register
930 so that it doesn't clobber the current value of "a". */
932 new_in_reg = new_out_reg
933 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
934 goal_class, "");
936 /* In operand can be got from transformations before processing insn
937 constraints. One example of such transformations is subreg
938 reloading (see function simplify_operand_subreg). The new
939 pseudos created by the transformations might have inaccurate
940 class (ALL_REGS) and we should make their classes more
941 accurate. */
942 narrow_reload_pseudo_class (in_rtx, goal_class);
943 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
944 *before = get_insns ();
945 end_sequence ();
946 for (i = 0; (in = ins[i]) >= 0; i++)
948 lra_assert
949 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
950 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
951 *curr_id->operand_loc[in] = new_in_reg;
953 lra_update_dups (curr_id, ins);
954 if (out < 0)
955 return;
956 /* See a comment for the input operand above. */
957 narrow_reload_pseudo_class (out_rtx, goal_class);
958 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
960 start_sequence ();
961 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
962 emit_insn (*after);
963 *after = get_insns ();
964 end_sequence ();
966 *curr_id->operand_loc[out] = new_out_reg;
967 lra_update_dup (curr_id, out);
970 /* Return register class which is union of all reg classes in insn
971 constraint alternative string starting with P. */
972 static enum reg_class
973 reg_class_from_constraints (const char *p)
975 int c, len;
976 enum reg_class op_class = NO_REGS;
979 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
981 case '#':
982 case ',':
983 return op_class;
985 case 'g':
986 op_class = reg_class_subunion[op_class][GENERAL_REGS];
987 break;
989 default:
990 enum constraint_num cn = lookup_constraint (p);
991 enum reg_class cl = reg_class_for_constraint (cn);
992 if (cl == NO_REGS)
994 if (insn_extra_address_constraint (cn))
995 op_class
996 = (reg_class_subunion
997 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
998 ADDRESS, SCRATCH)]);
999 break;
1002 op_class = reg_class_subunion[op_class][cl];
1003 break;
1005 while ((p += len), c);
1006 return op_class;
1009 /* If OP is a register, return the class of the register as per
1010 get_reg_class, otherwise return NO_REGS. */
1011 static inline enum reg_class
1012 get_op_class (rtx op)
1014 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1017 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1018 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1019 SUBREG for VAL to make them equal. */
1020 static rtx_insn *
1021 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1023 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1025 /* Usually size of mem_pseudo is greater than val size but in
1026 rare cases it can be less as it can be defined by target
1027 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1028 if (! MEM_P (val))
1030 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1031 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1033 LRA_SUBREG_P (val) = 1;
1035 else
1037 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1038 LRA_SUBREG_P (mem_pseudo) = 1;
1041 return as_a <rtx_insn *> (to_p
1042 ? gen_move_insn (mem_pseudo, val)
1043 : gen_move_insn (val, mem_pseudo));
1046 /* Process a special case insn (register move), return true if we
1047 don't need to process it anymore. INSN should be a single set
1048 insn. Set up that RTL was changed through CHANGE_P and macro
1049 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1050 SEC_MEM_P. */
1051 static bool
1052 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1054 int sregno, dregno;
1055 rtx dest, src, dreg, sreg, old_sreg, new_reg, scratch_reg;
1056 rtx_insn *before;
1057 enum reg_class dclass, sclass, secondary_class;
1058 machine_mode sreg_mode;
1059 secondary_reload_info sri;
1061 lra_assert (curr_insn_set != NULL_RTX);
1062 dreg = dest = SET_DEST (curr_insn_set);
1063 sreg = src = SET_SRC (curr_insn_set);
1064 if (GET_CODE (dest) == SUBREG)
1065 dreg = SUBREG_REG (dest);
1066 if (GET_CODE (src) == SUBREG)
1067 sreg = SUBREG_REG (src);
1068 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1069 return false;
1070 sclass = dclass = NO_REGS;
1071 if (REG_P (dreg))
1072 dclass = get_reg_class (REGNO (dreg));
1073 if (dclass == ALL_REGS)
1074 /* ALL_REGS is used for new pseudos created by transformations
1075 like reload of SUBREG_REG (see function
1076 simplify_operand_subreg). We don't know their class yet. We
1077 should figure out the class from processing the insn
1078 constraints not in this fast path function. Even if ALL_REGS
1079 were a right class for the pseudo, secondary_... hooks usually
1080 are not define for ALL_REGS. */
1081 return false;
1082 sreg_mode = GET_MODE (sreg);
1083 old_sreg = sreg;
1084 if (REG_P (sreg))
1085 sclass = get_reg_class (REGNO (sreg));
1086 if (sclass == ALL_REGS)
1087 /* See comments above. */
1088 return false;
1089 if (sclass == NO_REGS && dclass == NO_REGS)
1090 return false;
1091 #ifdef SECONDARY_MEMORY_NEEDED
1092 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1093 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1094 && ((sclass != NO_REGS && dclass != NO_REGS)
1095 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1096 #endif
1099 *sec_mem_p = true;
1100 return false;
1102 #endif
1103 if (! REG_P (dreg) || ! REG_P (sreg))
1104 return false;
1105 sri.prev_sri = NULL;
1106 sri.icode = CODE_FOR_nothing;
1107 sri.extra_cost = 0;
1108 secondary_class = NO_REGS;
1109 /* Set up hard register for a reload pseudo for hook
1110 secondary_reload because some targets just ignore unassigned
1111 pseudos in the hook. */
1112 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1114 dregno = REGNO (dreg);
1115 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1117 else
1118 dregno = -1;
1119 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1121 sregno = REGNO (sreg);
1122 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1124 else
1125 sregno = -1;
1126 if (sclass != NO_REGS)
1127 secondary_class
1128 = (enum reg_class) targetm.secondary_reload (false, dest,
1129 (reg_class_t) sclass,
1130 GET_MODE (src), &sri);
1131 if (sclass == NO_REGS
1132 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1133 && dclass != NO_REGS))
1135 enum reg_class old_sclass = secondary_class;
1136 secondary_reload_info old_sri = sri;
1138 sri.prev_sri = NULL;
1139 sri.icode = CODE_FOR_nothing;
1140 sri.extra_cost = 0;
1141 secondary_class
1142 = (enum reg_class) targetm.secondary_reload (true, sreg,
1143 (reg_class_t) dclass,
1144 sreg_mode, &sri);
1145 /* Check the target hook consistency. */
1146 lra_assert
1147 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1148 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1149 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1151 if (sregno >= 0)
1152 reg_renumber [sregno] = -1;
1153 if (dregno >= 0)
1154 reg_renumber [dregno] = -1;
1155 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1156 return false;
1157 *change_p = true;
1158 new_reg = NULL_RTX;
1159 if (secondary_class != NO_REGS)
1160 new_reg = lra_create_new_reg_with_unique_value (sreg_mode, NULL_RTX,
1161 secondary_class,
1162 "secondary");
1163 start_sequence ();
1164 if (old_sreg != sreg)
1165 sreg = copy_rtx (sreg);
1166 if (sri.icode == CODE_FOR_nothing)
1167 lra_emit_move (new_reg, sreg);
1168 else
1170 enum reg_class scratch_class;
1172 scratch_class = (reg_class_from_constraints
1173 (insn_data[sri.icode].operand[2].constraint));
1174 scratch_reg = (lra_create_new_reg_with_unique_value
1175 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1176 scratch_class, "scratch"));
1177 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1178 sreg, scratch_reg));
1180 before = get_insns ();
1181 end_sequence ();
1182 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1183 if (new_reg != NULL_RTX)
1185 if (GET_CODE (src) == SUBREG)
1186 SUBREG_REG (src) = new_reg;
1187 else
1188 SET_SRC (curr_insn_set) = new_reg;
1190 else
1192 if (lra_dump_file != NULL)
1194 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1195 dump_insn_slim (lra_dump_file, curr_insn);
1197 lra_set_insn_deleted (curr_insn);
1198 return true;
1200 return false;
1203 /* The following data describe the result of process_alt_operands.
1204 The data are used in curr_insn_transform to generate reloads. */
1206 /* The chosen reg classes which should be used for the corresponding
1207 operands. */
1208 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1209 /* True if the operand should be the same as another operand and that
1210 other operand does not need a reload. */
1211 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1212 /* True if the operand does not need a reload. */
1213 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1214 /* True if the operand can be offsetable memory. */
1215 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1216 /* The number of an operand to which given operand can be matched to. */
1217 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1218 /* The number of elements in the following array. */
1219 static int goal_alt_dont_inherit_ops_num;
1220 /* Numbers of operands whose reload pseudos should not be inherited. */
1221 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1222 /* True if the insn commutative operands should be swapped. */
1223 static bool goal_alt_swapped;
1224 /* The chosen insn alternative. */
1225 static int goal_alt_number;
1227 /* The following five variables are used to choose the best insn
1228 alternative. They reflect final characteristics of the best
1229 alternative. */
1231 /* Number of necessary reloads and overall cost reflecting the
1232 previous value and other unpleasantness of the best alternative. */
1233 static int best_losers, best_overall;
1234 /* Overall number hard registers used for reloads. For example, on
1235 some targets we need 2 general registers to reload DFmode and only
1236 one floating point register. */
1237 static int best_reload_nregs;
1238 /* Overall number reflecting distances of previous reloading the same
1239 value. The distances are counted from the current BB start. It is
1240 used to improve inheritance chances. */
1241 static int best_reload_sum;
1243 /* True if the current insn should have no correspondingly input or
1244 output reloads. */
1245 static bool no_input_reloads_p, no_output_reloads_p;
1247 /* True if we swapped the commutative operands in the current
1248 insn. */
1249 static int curr_swapped;
1251 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1252 register of class CL. Add any input reloads to list BEFORE. AFTER
1253 is nonnull if *LOC is an automodified value; handle that case by
1254 adding the required output reloads to list AFTER. Return true if
1255 the RTL was changed.
1257 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1258 register. Return false if the address register is correct. */
1259 static bool
1260 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1261 enum reg_class cl)
1263 int regno;
1264 enum reg_class rclass, new_class;
1265 rtx reg;
1266 rtx new_reg;
1267 machine_mode mode;
1268 bool subreg_p, before_p = false;
1270 subreg_p = GET_CODE (*loc) == SUBREG;
1271 if (subreg_p)
1272 loc = &SUBREG_REG (*loc);
1273 reg = *loc;
1274 mode = GET_MODE (reg);
1275 if (! REG_P (reg))
1277 if (check_only_p)
1278 return true;
1279 /* Always reload memory in an address even if the target supports
1280 such addresses. */
1281 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1282 before_p = true;
1284 else
1286 regno = REGNO (reg);
1287 rclass = get_reg_class (regno);
1288 if (! check_only_p
1289 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1291 if (lra_dump_file != NULL)
1293 fprintf (lra_dump_file,
1294 "Changing pseudo %d in address of insn %u on equiv ",
1295 REGNO (reg), INSN_UID (curr_insn));
1296 dump_value_slim (lra_dump_file, *loc, 1);
1297 fprintf (lra_dump_file, "\n");
1299 *loc = copy_rtx (*loc);
1301 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1303 if (check_only_p)
1304 return true;
1305 reg = *loc;
1306 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1307 mode, reg, cl, subreg_p, "address", &new_reg))
1308 before_p = true;
1310 else if (new_class != NO_REGS && rclass != new_class)
1312 if (check_only_p)
1313 return true;
1314 lra_change_class (regno, new_class, " Change to", true);
1315 return false;
1317 else
1318 return false;
1320 if (before_p)
1322 push_to_sequence (*before);
1323 lra_emit_move (new_reg, reg);
1324 *before = get_insns ();
1325 end_sequence ();
1327 *loc = new_reg;
1328 if (after != NULL)
1330 start_sequence ();
1331 lra_emit_move (reg, new_reg);
1332 emit_insn (*after);
1333 *after = get_insns ();
1334 end_sequence ();
1336 return true;
1339 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1340 the insn to be inserted before curr insn. AFTER returns the
1341 the insn to be inserted after curr insn. ORIGREG and NEWREG
1342 are the original reg and new reg for reload. */
1343 static void
1344 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1345 rtx newreg)
1347 if (before)
1349 push_to_sequence (*before);
1350 lra_emit_move (newreg, origreg);
1351 *before = get_insns ();
1352 end_sequence ();
1354 if (after)
1356 start_sequence ();
1357 lra_emit_move (origreg, newreg);
1358 emit_insn (*after);
1359 *after = get_insns ();
1360 end_sequence ();
1364 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1366 /* Make reloads for subreg in operand NOP with internal subreg mode
1367 REG_MODE, add new reloads for further processing. Return true if
1368 any reload was generated. */
1369 static bool
1370 simplify_operand_subreg (int nop, machine_mode reg_mode)
1372 int hard_regno;
1373 rtx_insn *before, *after;
1374 machine_mode mode;
1375 rtx reg, new_reg;
1376 rtx operand = *curr_id->operand_loc[nop];
1377 enum reg_class regclass;
1378 enum op_type type;
1380 before = after = NULL;
1382 if (GET_CODE (operand) != SUBREG)
1383 return false;
1385 mode = GET_MODE (operand);
1386 reg = SUBREG_REG (operand);
1387 type = curr_static_id->operand[nop].type;
1388 /* If we change address for paradoxical subreg of memory, the
1389 address might violate the necessary alignment or the access might
1390 be slow. So take this into consideration. We should not worry
1391 about access beyond allocated memory for paradoxical memory
1392 subregs as we don't substitute such equiv memory (see processing
1393 equivalences in function lra_constraints) and because for spilled
1394 pseudos we allocate stack memory enough for the biggest
1395 corresponding paradoxical subreg. */
1396 if (MEM_P (reg)
1397 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1398 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1400 rtx subst, old = *curr_id->operand_loc[nop];
1402 alter_subreg (curr_id->operand_loc[nop], false);
1403 subst = *curr_id->operand_loc[nop];
1404 lra_assert (MEM_P (subst));
1405 if (! valid_address_p (GET_MODE (reg), XEXP (reg, 0),
1406 MEM_ADDR_SPACE (reg))
1407 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1408 MEM_ADDR_SPACE (subst)))
1409 return true;
1410 /* If the address was valid and became invalid, prefer to reload
1411 the memory. Typical case is when the index scale should
1412 correspond the memory. */
1413 *curr_id->operand_loc[nop] = old;
1415 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1417 alter_subreg (curr_id->operand_loc[nop], false);
1418 return true;
1420 /* Put constant into memory when we have mixed modes. It generates
1421 a better code in most cases as it does not need a secondary
1422 reload memory. It also prevents LRA looping when LRA is using
1423 secondary reload memory again and again. */
1424 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1425 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1427 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1428 alter_subreg (curr_id->operand_loc[nop], false);
1429 return true;
1431 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1432 if there may be a problem accessing OPERAND in the outer
1433 mode. */
1434 if ((REG_P (reg)
1435 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1436 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1437 /* Don't reload paradoxical subregs because we could be looping
1438 having repeatedly final regno out of hard regs range. */
1439 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1440 >= hard_regno_nregs[hard_regno][mode])
1441 && simplify_subreg_regno (hard_regno, GET_MODE (reg),
1442 SUBREG_BYTE (operand), mode) < 0
1443 /* Don't reload subreg for matching reload. It is actually
1444 valid subreg in LRA. */
1445 && ! LRA_SUBREG_P (operand))
1446 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1448 enum reg_class rclass;
1450 if (REG_P (reg))
1451 /* There is a big probability that we will get the same class
1452 for the new pseudo and we will get the same insn which
1453 means infinite looping. So spill the new pseudo. */
1454 rclass = NO_REGS;
1455 else
1456 /* The class will be defined later in curr_insn_transform. */
1457 rclass
1458 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1460 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1461 rclass, TRUE, "subreg reg", &new_reg))
1463 bool insert_before, insert_after;
1464 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1466 insert_before = (type != OP_OUT
1467 || GET_MODE_SIZE (GET_MODE (reg)) > GET_MODE_SIZE (mode));
1468 insert_after = (type != OP_IN);
1469 insert_move_for_subreg (insert_before ? &before : NULL,
1470 insert_after ? &after : NULL,
1471 reg, new_reg);
1473 SUBREG_REG (operand) = new_reg;
1474 lra_process_new_insns (curr_insn, before, after,
1475 "Inserting subreg reload");
1476 return true;
1478 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1479 IRA allocates hardreg to the inner pseudo reg according to its mode
1480 instead of the outermode, so the size of the hardreg may not be enough
1481 to contain the outermode operand, in that case we may need to insert
1482 reload for the reg. For the following two types of paradoxical subreg,
1483 we need to insert reload:
1484 1. If the op_type is OP_IN, and the hardreg could not be paired with
1485 other hardreg to contain the outermode operand
1486 (checked by in_hard_reg_set_p), we need to insert the reload.
1487 2. If the op_type is OP_OUT or OP_INOUT.
1489 Here is a paradoxical subreg example showing how the reload is generated:
1491 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1492 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1494 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1495 here, if reg107 is assigned to hardreg R15, because R15 is the last
1496 hardreg, compiler cannot find another hardreg to pair with R15 to
1497 contain TImode data. So we insert a TImode reload reg180 for it.
1498 After reload is inserted:
1500 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1501 (reg:DI 107 [ __comp ])) -1
1502 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1503 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1505 Two reload hard registers will be allocated to reg180 to save TImode data
1506 in LRA_assign. */
1507 else if (REG_P (reg)
1508 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1509 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1510 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1511 < hard_regno_nregs[hard_regno][mode])
1512 && (regclass = lra_get_allocno_class (REGNO (reg)))
1513 && (type != OP_IN
1514 || !in_hard_reg_set_p (reg_class_contents[regclass],
1515 mode, hard_regno)))
1517 /* The class will be defined later in curr_insn_transform. */
1518 enum reg_class rclass
1519 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1521 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1522 rclass, TRUE, "paradoxical subreg", &new_reg))
1524 rtx subreg;
1525 bool insert_before, insert_after;
1527 PUT_MODE (new_reg, mode);
1528 subreg = simplify_gen_subreg (GET_MODE (reg), new_reg, mode, 0);
1529 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1531 insert_before = (type != OP_OUT);
1532 insert_after = (type != OP_IN);
1533 insert_move_for_subreg (insert_before ? &before : NULL,
1534 insert_after ? &after : NULL,
1535 reg, subreg);
1537 SUBREG_REG (operand) = new_reg;
1538 lra_process_new_insns (curr_insn, before, after,
1539 "Inserting paradoxical subreg reload");
1540 return true;
1542 return false;
1545 /* Return TRUE if X refers for a hard register from SET. */
1546 static bool
1547 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1549 int i, j, x_hard_regno;
1550 machine_mode mode;
1551 const char *fmt;
1552 enum rtx_code code;
1554 if (x == NULL_RTX)
1555 return false;
1556 code = GET_CODE (x);
1557 mode = GET_MODE (x);
1558 if (code == SUBREG)
1560 x = SUBREG_REG (x);
1561 code = GET_CODE (x);
1562 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1563 mode = GET_MODE (x);
1566 if (REG_P (x))
1568 x_hard_regno = get_hard_regno (x);
1569 return (x_hard_regno >= 0
1570 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1572 if (MEM_P (x))
1574 struct address_info ad;
1576 decompose_mem_address (&ad, x);
1577 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1578 return true;
1579 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1580 return true;
1582 fmt = GET_RTX_FORMAT (code);
1583 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1585 if (fmt[i] == 'e')
1587 if (uses_hard_regs_p (XEXP (x, i), set))
1588 return true;
1590 else if (fmt[i] == 'E')
1592 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1593 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1594 return true;
1597 return false;
1600 /* Return true if OP is a spilled pseudo. */
1601 static inline bool
1602 spilled_pseudo_p (rtx op)
1604 return (REG_P (op)
1605 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1608 /* Return true if X is a general constant. */
1609 static inline bool
1610 general_constant_p (rtx x)
1612 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1615 static bool
1616 reg_in_class_p (rtx reg, enum reg_class cl)
1618 if (cl == NO_REGS)
1619 return get_reg_class (REGNO (reg)) == NO_REGS;
1620 return in_class_p (reg, cl, NULL);
1623 /* Major function to choose the current insn alternative and what
1624 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1625 negative we should consider only this alternative. Return false if
1626 we can not choose the alternative or find how to reload the
1627 operands. */
1628 static bool
1629 process_alt_operands (int only_alternative)
1631 bool ok_p = false;
1632 int nop, overall, nalt;
1633 int n_alternatives = curr_static_id->n_alternatives;
1634 int n_operands = curr_static_id->n_operands;
1635 /* LOSERS counts the operands that don't fit this alternative and
1636 would require loading. */
1637 int losers;
1638 /* REJECT is a count of how undesirable this alternative says it is
1639 if any reloading is required. If the alternative matches exactly
1640 then REJECT is ignored, but otherwise it gets this much counted
1641 against it in addition to the reloading needed. */
1642 int reject;
1643 /* The number of elements in the following array. */
1644 int early_clobbered_regs_num;
1645 /* Numbers of operands which are early clobber registers. */
1646 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1647 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1648 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1649 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1650 bool curr_alt_win[MAX_RECOG_OPERANDS];
1651 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1652 int curr_alt_matches[MAX_RECOG_OPERANDS];
1653 /* The number of elements in the following array. */
1654 int curr_alt_dont_inherit_ops_num;
1655 /* Numbers of operands whose reload pseudos should not be inherited. */
1656 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1657 rtx op;
1658 /* The register when the operand is a subreg of register, otherwise the
1659 operand itself. */
1660 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1661 /* The register if the operand is a register or subreg of register,
1662 otherwise NULL. */
1663 rtx operand_reg[MAX_RECOG_OPERANDS];
1664 int hard_regno[MAX_RECOG_OPERANDS];
1665 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1666 int reload_nregs, reload_sum;
1667 bool costly_p;
1668 enum reg_class cl;
1670 /* Calculate some data common for all alternatives to speed up the
1671 function. */
1672 for (nop = 0; nop < n_operands; nop++)
1674 rtx reg;
1676 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1677 /* The real hard regno of the operand after the allocation. */
1678 hard_regno[nop] = get_hard_regno (op);
1680 operand_reg[nop] = reg = op;
1681 biggest_mode[nop] = GET_MODE (op);
1682 if (GET_CODE (op) == SUBREG)
1684 operand_reg[nop] = reg = SUBREG_REG (op);
1685 if (GET_MODE_SIZE (biggest_mode[nop])
1686 < GET_MODE_SIZE (GET_MODE (reg)))
1687 biggest_mode[nop] = GET_MODE (reg);
1689 if (! REG_P (reg))
1690 operand_reg[nop] = NULL_RTX;
1691 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1692 || ((int) REGNO (reg)
1693 == lra_get_elimination_hard_regno (REGNO (reg))))
1694 no_subreg_reg_operand[nop] = reg;
1695 else
1696 operand_reg[nop] = no_subreg_reg_operand[nop]
1697 /* Just use natural mode for elimination result. It should
1698 be enough for extra constraints hooks. */
1699 = regno_reg_rtx[hard_regno[nop]];
1702 /* The constraints are made of several alternatives. Each operand's
1703 constraint looks like foo,bar,... with commas separating the
1704 alternatives. The first alternatives for all operands go
1705 together, the second alternatives go together, etc.
1707 First loop over alternatives. */
1708 alternative_mask preferred = curr_id->preferred_alternatives;
1709 if (only_alternative >= 0)
1710 preferred &= ALTERNATIVE_BIT (only_alternative);
1712 for (nalt = 0; nalt < n_alternatives; nalt++)
1714 /* Loop over operands for one constraint alternative. */
1715 if (!TEST_BIT (preferred, nalt))
1716 continue;
1718 overall = losers = reject = reload_nregs = reload_sum = 0;
1719 for (nop = 0; nop < n_operands; nop++)
1721 int inc = (curr_static_id
1722 ->operand_alternative[nalt * n_operands + nop].reject);
1723 if (lra_dump_file != NULL && inc != 0)
1724 fprintf (lra_dump_file,
1725 " Staticly defined alt reject+=%d\n", inc);
1726 reject += inc;
1728 early_clobbered_regs_num = 0;
1730 for (nop = 0; nop < n_operands; nop++)
1732 const char *p;
1733 char *end;
1734 int len, c, m, i, opalt_num, this_alternative_matches;
1735 bool win, did_match, offmemok, early_clobber_p;
1736 /* false => this operand can be reloaded somehow for this
1737 alternative. */
1738 bool badop;
1739 /* true => this operand can be reloaded if the alternative
1740 allows regs. */
1741 bool winreg;
1742 /* True if a constant forced into memory would be OK for
1743 this operand. */
1744 bool constmemok;
1745 enum reg_class this_alternative, this_costly_alternative;
1746 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1747 bool this_alternative_match_win, this_alternative_win;
1748 bool this_alternative_offmemok;
1749 bool scratch_p;
1750 machine_mode mode;
1751 enum constraint_num cn;
1753 opalt_num = nalt * n_operands + nop;
1754 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1756 /* Fast track for no constraints at all. */
1757 curr_alt[nop] = NO_REGS;
1758 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1759 curr_alt_win[nop] = true;
1760 curr_alt_match_win[nop] = false;
1761 curr_alt_offmemok[nop] = false;
1762 curr_alt_matches[nop] = -1;
1763 continue;
1766 op = no_subreg_reg_operand[nop];
1767 mode = curr_operand_mode[nop];
1769 win = did_match = winreg = offmemok = constmemok = false;
1770 badop = true;
1772 early_clobber_p = false;
1773 p = curr_static_id->operand_alternative[opalt_num].constraint;
1775 this_costly_alternative = this_alternative = NO_REGS;
1776 /* We update set of possible hard regs besides its class
1777 because reg class might be inaccurate. For example,
1778 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1779 is translated in HI_REGS because classes are merged by
1780 pairs and there is no accurate intermediate class. */
1781 CLEAR_HARD_REG_SET (this_alternative_set);
1782 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1783 this_alternative_win = false;
1784 this_alternative_match_win = false;
1785 this_alternative_offmemok = false;
1786 this_alternative_matches = -1;
1788 /* An empty constraint should be excluded by the fast
1789 track. */
1790 lra_assert (*p != 0 && *p != ',');
1792 /* Scan this alternative's specs for this operand; set WIN
1793 if the operand fits any letter in this alternative.
1794 Otherwise, clear BADOP if this operand could fit some
1795 letter after reloads, or set WINREG if this operand could
1796 fit after reloads provided the constraint allows some
1797 registers. */
1798 costly_p = false;
1801 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1803 case '\0':
1804 len = 0;
1805 break;
1806 case ',':
1807 c = '\0';
1808 break;
1810 case '&':
1811 early_clobber_p = true;
1812 break;
1814 case '#':
1815 /* Ignore rest of this alternative. */
1816 c = '\0';
1817 break;
1819 case '0': case '1': case '2': case '3': case '4':
1820 case '5': case '6': case '7': case '8': case '9':
1822 int m_hregno;
1823 bool match_p;
1825 m = strtoul (p, &end, 10);
1826 p = end;
1827 len = 0;
1828 lra_assert (nop > m);
1830 this_alternative_matches = m;
1831 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1832 /* We are supposed to match a previous operand.
1833 If we do, we win if that one did. If we do
1834 not, count both of the operands as losers.
1835 (This is too conservative, since most of the
1836 time only a single reload insn will be needed
1837 to make the two operands win. As a result,
1838 this alternative may be rejected when it is
1839 actually desirable.) */
1840 match_p = false;
1841 if (operands_match_p (*curr_id->operand_loc[nop],
1842 *curr_id->operand_loc[m], m_hregno))
1844 /* We should reject matching of an early
1845 clobber operand if the matching operand is
1846 not dying in the insn. */
1847 if (! curr_static_id->operand[m].early_clobber
1848 || operand_reg[nop] == NULL_RTX
1849 || (find_regno_note (curr_insn, REG_DEAD,
1850 REGNO (op))
1851 || REGNO (op) == REGNO (operand_reg[m])))
1852 match_p = true;
1854 if (match_p)
1856 /* If we are matching a non-offsettable
1857 address where an offsettable address was
1858 expected, then we must reject this
1859 combination, because we can't reload
1860 it. */
1861 if (curr_alt_offmemok[m]
1862 && MEM_P (*curr_id->operand_loc[m])
1863 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1864 continue;
1866 else
1868 /* Operands don't match. Both operands must
1869 allow a reload register, otherwise we
1870 cannot make them match. */
1871 if (curr_alt[m] == NO_REGS)
1872 break;
1873 /* Retroactively mark the operand we had to
1874 match as a loser, if it wasn't already and
1875 it wasn't matched to a register constraint
1876 (e.g it might be matched by memory). */
1877 if (curr_alt_win[m]
1878 && (operand_reg[m] == NULL_RTX
1879 || hard_regno[m] < 0))
1881 losers++;
1882 reload_nregs
1883 += (ira_reg_class_max_nregs[curr_alt[m]]
1884 [GET_MODE (*curr_id->operand_loc[m])]);
1887 /* Prefer matching earlyclobber alternative as
1888 it results in less hard regs required for
1889 the insn than a non-matching earlyclobber
1890 alternative. */
1891 if (curr_static_id->operand[m].early_clobber)
1893 if (lra_dump_file != NULL)
1894 fprintf
1895 (lra_dump_file,
1896 " %d Matching earlyclobber alt:"
1897 " reject--\n",
1898 nop);
1899 reject--;
1901 /* Otherwise we prefer no matching
1902 alternatives because it gives more freedom
1903 in RA. */
1904 else if (operand_reg[nop] == NULL_RTX
1905 || (find_regno_note (curr_insn, REG_DEAD,
1906 REGNO (operand_reg[nop]))
1907 == NULL_RTX))
1909 if (lra_dump_file != NULL)
1910 fprintf
1911 (lra_dump_file,
1912 " %d Matching alt: reject+=2\n",
1913 nop);
1914 reject += 2;
1917 /* If we have to reload this operand and some
1918 previous operand also had to match the same
1919 thing as this operand, we don't know how to do
1920 that. */
1921 if (!match_p || !curr_alt_win[m])
1923 for (i = 0; i < nop; i++)
1924 if (curr_alt_matches[i] == m)
1925 break;
1926 if (i < nop)
1927 break;
1929 else
1930 did_match = true;
1932 /* This can be fixed with reloads if the operand
1933 we are supposed to match can be fixed with
1934 reloads. */
1935 badop = false;
1936 this_alternative = curr_alt[m];
1937 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1938 winreg = this_alternative != NO_REGS;
1939 break;
1942 case 'g':
1943 if (MEM_P (op)
1944 || general_constant_p (op)
1945 || spilled_pseudo_p (op))
1946 win = true;
1947 cl = GENERAL_REGS;
1948 goto reg;
1950 default:
1951 cn = lookup_constraint (p);
1952 switch (get_constraint_type (cn))
1954 case CT_REGISTER:
1955 cl = reg_class_for_constraint (cn);
1956 if (cl != NO_REGS)
1957 goto reg;
1958 break;
1960 case CT_CONST_INT:
1961 if (CONST_INT_P (op)
1962 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
1963 win = true;
1964 break;
1966 case CT_MEMORY:
1967 if (MEM_P (op)
1968 && satisfies_memory_constraint_p (op, cn))
1969 win = true;
1970 else if (spilled_pseudo_p (op))
1971 win = true;
1973 /* If we didn't already win, we can reload constants
1974 via force_const_mem or put the pseudo value into
1975 memory, or make other memory by reloading the
1976 address like for 'o'. */
1977 if (CONST_POOL_OK_P (mode, op)
1978 || MEM_P (op) || REG_P (op))
1979 badop = false;
1980 constmemok = true;
1981 offmemok = true;
1982 break;
1984 case CT_ADDRESS:
1985 /* If we didn't already win, we can reload the address
1986 into a base register. */
1987 if (satisfies_address_constraint_p (op, cn))
1988 win = true;
1989 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1990 ADDRESS, SCRATCH);
1991 badop = false;
1992 goto reg;
1994 case CT_FIXED_FORM:
1995 if (constraint_satisfied_p (op, cn))
1996 win = true;
1997 break;
1999 break;
2001 reg:
2002 this_alternative = reg_class_subunion[this_alternative][cl];
2003 IOR_HARD_REG_SET (this_alternative_set,
2004 reg_class_contents[cl]);
2005 if (costly_p)
2007 this_costly_alternative
2008 = reg_class_subunion[this_costly_alternative][cl];
2009 IOR_HARD_REG_SET (this_costly_alternative_set,
2010 reg_class_contents[cl]);
2012 if (mode == BLKmode)
2013 break;
2014 winreg = true;
2015 if (REG_P (op))
2017 if (hard_regno[nop] >= 0
2018 && in_hard_reg_set_p (this_alternative_set,
2019 mode, hard_regno[nop]))
2020 win = true;
2021 else if (hard_regno[nop] < 0
2022 && in_class_p (op, this_alternative, NULL))
2023 win = true;
2025 break;
2027 if (c != ' ' && c != '\t')
2028 costly_p = c == '*';
2030 while ((p += len), c);
2032 scratch_p = (operand_reg[nop] != NULL_RTX
2033 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2034 /* Record which operands fit this alternative. */
2035 if (win)
2037 this_alternative_win = true;
2038 if (operand_reg[nop] != NULL_RTX)
2040 if (hard_regno[nop] >= 0)
2042 if (in_hard_reg_set_p (this_costly_alternative_set,
2043 mode, hard_regno[nop]))
2045 if (lra_dump_file != NULL)
2046 fprintf (lra_dump_file,
2047 " %d Costly set: reject++\n",
2048 nop);
2049 reject++;
2052 else
2054 /* Prefer won reg to spilled pseudo under other
2055 equal conditions for possibe inheritance. */
2056 if (! scratch_p)
2058 if (lra_dump_file != NULL)
2059 fprintf
2060 (lra_dump_file,
2061 " %d Non pseudo reload: reject++\n",
2062 nop);
2063 reject++;
2065 if (in_class_p (operand_reg[nop],
2066 this_costly_alternative, NULL))
2068 if (lra_dump_file != NULL)
2069 fprintf
2070 (lra_dump_file,
2071 " %d Non pseudo costly reload:"
2072 " reject++\n",
2073 nop);
2074 reject++;
2077 /* We simulate the behaviour of old reload here.
2078 Although scratches need hard registers and it
2079 might result in spilling other pseudos, no reload
2080 insns are generated for the scratches. So it
2081 might cost something but probably less than old
2082 reload pass believes. */
2083 if (scratch_p)
2085 if (lra_dump_file != NULL)
2086 fprintf (lra_dump_file,
2087 " %d Scratch win: reject+=2\n",
2088 nop);
2089 reject += 2;
2093 else if (did_match)
2094 this_alternative_match_win = true;
2095 else
2097 int const_to_mem = 0;
2098 bool no_regs_p;
2100 /* Never do output reload of stack pointer. It makes
2101 impossible to do elimination when SP is changed in
2102 RTL. */
2103 if (op == stack_pointer_rtx && ! frame_pointer_needed
2104 && curr_static_id->operand[nop].type != OP_IN)
2105 goto fail;
2107 /* If this alternative asks for a specific reg class, see if there
2108 is at least one allocatable register in that class. */
2109 no_regs_p
2110 = (this_alternative == NO_REGS
2111 || (hard_reg_set_subset_p
2112 (reg_class_contents[this_alternative],
2113 lra_no_alloc_regs)));
2115 /* For asms, verify that the class for this alternative is possible
2116 for the mode that is specified. */
2117 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2119 int i;
2120 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2121 if (HARD_REGNO_MODE_OK (i, mode)
2122 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2123 mode, i))
2124 break;
2125 if (i == FIRST_PSEUDO_REGISTER)
2126 winreg = false;
2129 /* If this operand accepts a register, and if the
2130 register class has at least one allocatable register,
2131 then this operand can be reloaded. */
2132 if (winreg && !no_regs_p)
2133 badop = false;
2135 if (badop)
2137 if (lra_dump_file != NULL)
2138 fprintf (lra_dump_file,
2139 " alt=%d: Bad operand -- refuse\n",
2140 nalt);
2141 goto fail;
2144 /* If not assigned pseudo has a class which a subset of
2145 required reg class, it is a less costly alternative
2146 as the pseudo still can get a hard reg of necessary
2147 class. */
2148 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2149 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2150 && ira_class_subset_p[this_alternative][cl])
2152 if (lra_dump_file != NULL)
2153 fprintf
2154 (lra_dump_file,
2155 " %d Super set class reg: reject-=3\n", nop);
2156 reject -= 3;
2159 this_alternative_offmemok = offmemok;
2160 if (this_costly_alternative != NO_REGS)
2162 if (lra_dump_file != NULL)
2163 fprintf (lra_dump_file,
2164 " %d Costly loser: reject++\n", nop);
2165 reject++;
2167 /* If the operand is dying, has a matching constraint,
2168 and satisfies constraints of the matched operand
2169 which failed to satisfy the own constraints, most probably
2170 the reload for this operand will be gone. */
2171 if (this_alternative_matches >= 0
2172 && !curr_alt_win[this_alternative_matches]
2173 && REG_P (op)
2174 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2175 && (hard_regno[nop] >= 0
2176 ? in_hard_reg_set_p (this_alternative_set,
2177 mode, hard_regno[nop])
2178 : in_class_p (op, this_alternative, NULL)))
2180 if (lra_dump_file != NULL)
2181 fprintf
2182 (lra_dump_file,
2183 " %d Dying matched operand reload: reject++\n",
2184 nop);
2185 reject++;
2187 else
2189 /* Strict_low_part requires to reload the register
2190 not the sub-register. In this case we should
2191 check that a final reload hard reg can hold the
2192 value mode. */
2193 if (curr_static_id->operand[nop].strict_low
2194 && REG_P (op)
2195 && hard_regno[nop] < 0
2196 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2197 && ira_class_hard_regs_num[this_alternative] > 0
2198 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2199 [this_alternative][0],
2200 GET_MODE
2201 (*curr_id->operand_loc[nop])))
2203 if (lra_dump_file != NULL)
2204 fprintf
2205 (lra_dump_file,
2206 " alt=%d: Strict low subreg reload -- refuse\n",
2207 nalt);
2208 goto fail;
2210 losers++;
2212 if (operand_reg[nop] != NULL_RTX
2213 /* Output operands and matched input operands are
2214 not inherited. The following conditions do not
2215 exactly describe the previous statement but they
2216 are pretty close. */
2217 && curr_static_id->operand[nop].type != OP_OUT
2218 && (this_alternative_matches < 0
2219 || curr_static_id->operand[nop].type != OP_IN))
2221 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2222 (operand_reg[nop])]
2223 .last_reload);
2225 /* The value of reload_sum has sense only if we
2226 process insns in their order. It happens only on
2227 the first constraints sub-pass when we do most of
2228 reload work. */
2229 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2230 reload_sum += last_reload - bb_reload_num;
2232 /* If this is a constant that is reloaded into the
2233 desired class by copying it to memory first, count
2234 that as another reload. This is consistent with
2235 other code and is required to avoid choosing another
2236 alternative when the constant is moved into memory.
2237 Note that the test here is precisely the same as in
2238 the code below that calls force_const_mem. */
2239 if (CONST_POOL_OK_P (mode, op)
2240 && ((targetm.preferred_reload_class
2241 (op, this_alternative) == NO_REGS)
2242 || no_input_reloads_p))
2244 const_to_mem = 1;
2245 if (! no_regs_p)
2246 losers++;
2249 /* Alternative loses if it requires a type of reload not
2250 permitted for this insn. We can always reload
2251 objects with a REG_UNUSED note. */
2252 if ((curr_static_id->operand[nop].type != OP_IN
2253 && no_output_reloads_p
2254 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2255 || (curr_static_id->operand[nop].type != OP_OUT
2256 && no_input_reloads_p && ! const_to_mem)
2257 || (this_alternative_matches >= 0
2258 && (no_input_reloads_p
2259 || (no_output_reloads_p
2260 && (curr_static_id->operand
2261 [this_alternative_matches].type != OP_IN)
2262 && ! find_reg_note (curr_insn, REG_UNUSED,
2263 no_subreg_reg_operand
2264 [this_alternative_matches])))))
2266 if (lra_dump_file != NULL)
2267 fprintf
2268 (lra_dump_file,
2269 " alt=%d: No input/otput reload -- refuse\n",
2270 nalt);
2271 goto fail;
2274 /* Alternative loses if it required class pseudo can not
2275 hold value of required mode. Such insns can be
2276 described by insn definitions with mode iterators. */
2277 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2278 && ! hard_reg_set_empty_p (this_alternative_set)
2279 /* It is common practice for constraints to use a
2280 class which does not have actually enough regs to
2281 hold the value (e.g. x86 AREG for mode requiring
2282 more one general reg). Therefore we have 2
2283 conditions to check that the reload pseudo can
2284 not hold the mode value. */
2285 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2286 [this_alternative][0],
2287 GET_MODE (*curr_id->operand_loc[nop])))
2289 HARD_REG_SET temp;
2291 COPY_HARD_REG_SET (temp, this_alternative_set);
2292 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
2293 /* The above condition is not enough as the first
2294 reg in ira_class_hard_regs can be not aligned for
2295 multi-words mode values. */
2296 if (hard_reg_set_subset_p (temp,
2297 ira_prohibited_class_mode_regs
2298 [this_alternative]
2299 [GET_MODE (*curr_id->operand_loc[nop])]))
2301 if (lra_dump_file != NULL)
2302 fprintf
2303 (lra_dump_file,
2304 " alt=%d: reload pseudo for op %d "
2305 " can not hold the mode value -- refuse\n",
2306 nalt, nop);
2307 goto fail;
2311 /* Check strong discouragement of reload of non-constant
2312 into class THIS_ALTERNATIVE. */
2313 if (! CONSTANT_P (op) && ! no_regs_p
2314 && (targetm.preferred_reload_class
2315 (op, this_alternative) == NO_REGS
2316 || (curr_static_id->operand[nop].type == OP_OUT
2317 && (targetm.preferred_output_reload_class
2318 (op, this_alternative) == NO_REGS))))
2320 if (lra_dump_file != NULL)
2321 fprintf (lra_dump_file,
2322 " %d Non-prefered reload: reject+=%d\n",
2323 nop, LRA_MAX_REJECT);
2324 reject += LRA_MAX_REJECT;
2327 if (! (MEM_P (op) && offmemok)
2328 && ! (const_to_mem && constmemok))
2330 /* We prefer to reload pseudos over reloading other
2331 things, since such reloads may be able to be
2332 eliminated later. So bump REJECT in other cases.
2333 Don't do this in the case where we are forcing a
2334 constant into memory and it will then win since
2335 we don't want to have a different alternative
2336 match then. */
2337 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2339 if (lra_dump_file != NULL)
2340 fprintf
2341 (lra_dump_file,
2342 " %d Non-pseudo reload: reject+=2\n",
2343 nop);
2344 reject += 2;
2347 if (! no_regs_p)
2348 reload_nregs
2349 += ira_reg_class_max_nregs[this_alternative][mode];
2351 if (SMALL_REGISTER_CLASS_P (this_alternative))
2353 if (lra_dump_file != NULL)
2354 fprintf
2355 (lra_dump_file,
2356 " %d Small class reload: reject+=%d\n",
2357 nop, LRA_LOSER_COST_FACTOR / 2);
2358 reject += LRA_LOSER_COST_FACTOR / 2;
2362 /* We are trying to spill pseudo into memory. It is
2363 usually more costly than moving to a hard register
2364 although it might takes the same number of
2365 reloads. */
2366 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2368 if (lra_dump_file != NULL)
2369 fprintf
2370 (lra_dump_file,
2371 " %d Spill pseudo into memory: reject+=3\n",
2372 nop);
2373 reject += 3;
2374 if (VECTOR_MODE_P (mode))
2376 /* Spilling vectors into memory is usually more
2377 costly as they contain big values. */
2378 if (lra_dump_file != NULL)
2379 fprintf
2380 (lra_dump_file,
2381 " %d Spill vector pseudo: reject+=2\n",
2382 nop);
2383 reject += 2;
2387 #ifdef SECONDARY_MEMORY_NEEDED
2388 /* If reload requires moving value through secondary
2389 memory, it will need one more insn at least. */
2390 if (this_alternative != NO_REGS
2391 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2392 && ((curr_static_id->operand[nop].type != OP_OUT
2393 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2394 GET_MODE (op)))
2395 || (curr_static_id->operand[nop].type != OP_IN
2396 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2397 GET_MODE (op)))))
2398 losers++;
2399 #endif
2400 /* Input reloads can be inherited more often than output
2401 reloads can be removed, so penalize output
2402 reloads. */
2403 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2405 if (lra_dump_file != NULL)
2406 fprintf
2407 (lra_dump_file,
2408 " %d Non input pseudo reload: reject++\n",
2409 nop);
2410 reject++;
2414 if (early_clobber_p && ! scratch_p)
2416 if (lra_dump_file != NULL)
2417 fprintf (lra_dump_file,
2418 " %d Early clobber: reject++\n", nop);
2419 reject++;
2421 /* ??? We check early clobbers after processing all operands
2422 (see loop below) and there we update the costs more.
2423 Should we update the cost (may be approximately) here
2424 because of early clobber register reloads or it is a rare
2425 or non-important thing to be worth to do it. */
2426 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2427 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2429 if (lra_dump_file != NULL)
2430 fprintf (lra_dump_file,
2431 " alt=%d,overall=%d,losers=%d -- refuse\n",
2432 nalt, overall, losers);
2433 goto fail;
2436 curr_alt[nop] = this_alternative;
2437 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2438 curr_alt_win[nop] = this_alternative_win;
2439 curr_alt_match_win[nop] = this_alternative_match_win;
2440 curr_alt_offmemok[nop] = this_alternative_offmemok;
2441 curr_alt_matches[nop] = this_alternative_matches;
2443 if (this_alternative_matches >= 0
2444 && !did_match && !this_alternative_win)
2445 curr_alt_win[this_alternative_matches] = false;
2447 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2448 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2450 if (curr_insn_set != NULL_RTX && n_operands == 2
2451 /* Prevent processing non-move insns. */
2452 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2453 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2454 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2455 && REG_P (no_subreg_reg_operand[0])
2456 && REG_P (no_subreg_reg_operand[1])
2457 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2458 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2459 || (! curr_alt_win[0] && curr_alt_win[1]
2460 && REG_P (no_subreg_reg_operand[1])
2461 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2462 || (curr_alt_win[0] && ! curr_alt_win[1]
2463 && REG_P (no_subreg_reg_operand[0])
2464 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2465 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2466 no_subreg_reg_operand[1])
2467 || (targetm.preferred_reload_class
2468 (no_subreg_reg_operand[1],
2469 (enum reg_class) curr_alt[1]) != NO_REGS))
2470 /* If it is a result of recent elimination in move
2471 insn we can transform it into an add still by
2472 using this alternative. */
2473 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2475 /* We have a move insn and a new reload insn will be similar
2476 to the current insn. We should avoid such situation as it
2477 results in LRA cycling. */
2478 overall += LRA_MAX_REJECT;
2480 ok_p = true;
2481 curr_alt_dont_inherit_ops_num = 0;
2482 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2484 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2485 HARD_REG_SET temp_set;
2487 i = early_clobbered_nops[nop];
2488 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2489 || hard_regno[i] < 0)
2490 continue;
2491 lra_assert (operand_reg[i] != NULL_RTX);
2492 clobbered_hard_regno = hard_regno[i];
2493 CLEAR_HARD_REG_SET (temp_set);
2494 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2495 first_conflict_j = last_conflict_j = -1;
2496 for (j = 0; j < n_operands; j++)
2497 if (j == i
2498 /* We don't want process insides of match_operator and
2499 match_parallel because otherwise we would process
2500 their operands once again generating a wrong
2501 code. */
2502 || curr_static_id->operand[j].is_operator)
2503 continue;
2504 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2505 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2506 continue;
2507 /* If we don't reload j-th operand, check conflicts. */
2508 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2509 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2511 if (first_conflict_j < 0)
2512 first_conflict_j = j;
2513 last_conflict_j = j;
2515 if (last_conflict_j < 0)
2516 continue;
2517 /* If earlyclobber operand conflicts with another
2518 non-matching operand which is actually the same register
2519 as the earlyclobber operand, it is better to reload the
2520 another operand as an operand matching the earlyclobber
2521 operand can be also the same. */
2522 if (first_conflict_j == last_conflict_j
2523 && operand_reg[last_conflict_j]
2524 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2525 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2527 curr_alt_win[last_conflict_j] = false;
2528 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2529 = last_conflict_j;
2530 losers++;
2531 /* Early clobber was already reflected in REJECT. */
2532 lra_assert (reject > 0);
2533 if (lra_dump_file != NULL)
2534 fprintf
2535 (lra_dump_file,
2536 " %d Conflict early clobber reload: reject--\n",
2538 reject--;
2539 overall += LRA_LOSER_COST_FACTOR - 1;
2541 else
2543 /* We need to reload early clobbered register and the
2544 matched registers. */
2545 for (j = 0; j < n_operands; j++)
2546 if (curr_alt_matches[j] == i)
2548 curr_alt_match_win[j] = false;
2549 losers++;
2550 overall += LRA_LOSER_COST_FACTOR;
2552 if (! curr_alt_match_win[i])
2553 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2554 else
2556 /* Remember pseudos used for match reloads are never
2557 inherited. */
2558 lra_assert (curr_alt_matches[i] >= 0);
2559 curr_alt_win[curr_alt_matches[i]] = false;
2561 curr_alt_win[i] = curr_alt_match_win[i] = false;
2562 losers++;
2563 /* Early clobber was already reflected in REJECT. */
2564 lra_assert (reject > 0);
2565 if (lra_dump_file != NULL)
2566 fprintf
2567 (lra_dump_file,
2568 " %d Matched conflict early clobber reloads:"
2569 "reject--\n",
2571 reject--;
2572 overall += LRA_LOSER_COST_FACTOR - 1;
2575 if (lra_dump_file != NULL)
2576 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2577 nalt, overall, losers, reload_nregs);
2579 /* If this alternative can be made to work by reloading, and it
2580 needs less reloading than the others checked so far, record
2581 it as the chosen goal for reloading. */
2582 if ((best_losers != 0 && losers == 0)
2583 || (((best_losers == 0 && losers == 0)
2584 || (best_losers != 0 && losers != 0))
2585 && (best_overall > overall
2586 || (best_overall == overall
2587 /* If the cost of the reloads is the same,
2588 prefer alternative which requires minimal
2589 number of reload regs. */
2590 && (reload_nregs < best_reload_nregs
2591 || (reload_nregs == best_reload_nregs
2592 && (best_reload_sum < reload_sum
2593 || (best_reload_sum == reload_sum
2594 && nalt < goal_alt_number))))))))
2596 for (nop = 0; nop < n_operands; nop++)
2598 goal_alt_win[nop] = curr_alt_win[nop];
2599 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2600 goal_alt_matches[nop] = curr_alt_matches[nop];
2601 goal_alt[nop] = curr_alt[nop];
2602 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2604 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2605 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2606 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2607 goal_alt_swapped = curr_swapped;
2608 best_overall = overall;
2609 best_losers = losers;
2610 best_reload_nregs = reload_nregs;
2611 best_reload_sum = reload_sum;
2612 goal_alt_number = nalt;
2614 if (losers == 0)
2615 /* Everything is satisfied. Do not process alternatives
2616 anymore. */
2617 break;
2618 fail:
2621 return ok_p;
2624 /* Make reload base reg from address AD. */
2625 static rtx
2626 base_to_reg (struct address_info *ad)
2628 enum reg_class cl;
2629 int code = -1;
2630 rtx new_inner = NULL_RTX;
2631 rtx new_reg = NULL_RTX;
2632 rtx_insn *insn;
2633 rtx_insn *last_insn = get_last_insn();
2635 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2636 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2637 get_index_code (ad));
2638 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2639 cl, "base");
2640 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2641 ad->disp_term == NULL
2642 ? gen_int_mode (0, ad->mode)
2643 : *ad->disp_term);
2644 if (!valid_address_p (ad->mode, new_inner, ad->as))
2645 return NULL_RTX;
2646 insn = emit_insn (gen_rtx_SET (ad->mode, new_reg, *ad->base_term));
2647 code = recog_memoized (insn);
2648 if (code < 0)
2650 delete_insns_since (last_insn);
2651 return NULL_RTX;
2654 return new_inner;
2657 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2658 static rtx
2659 base_plus_disp_to_reg (struct address_info *ad)
2661 enum reg_class cl;
2662 rtx new_reg;
2664 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2665 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2666 get_index_code (ad));
2667 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2668 cl, "base + disp");
2669 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2670 return new_reg;
2673 /* Make reload of index part of address AD. Return the new
2674 pseudo. */
2675 static rtx
2676 index_part_to_reg (struct address_info *ad)
2678 rtx new_reg;
2680 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2681 INDEX_REG_CLASS, "index term");
2682 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2683 GEN_INT (get_index_scale (ad)), new_reg, 1);
2684 return new_reg;
2687 /* Return true if we can add a displacement to address AD, even if that
2688 makes the address invalid. The fix-up code requires any new address
2689 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2690 static bool
2691 can_add_disp_p (struct address_info *ad)
2693 return (!ad->autoinc_p
2694 && ad->segment == NULL
2695 && ad->base == ad->base_term
2696 && ad->disp == ad->disp_term);
2699 /* Make equiv substitution in address AD. Return true if a substitution
2700 was made. */
2701 static bool
2702 equiv_address_substitution (struct address_info *ad)
2704 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2705 HOST_WIDE_INT disp, scale;
2706 bool change_p;
2708 base_term = strip_subreg (ad->base_term);
2709 if (base_term == NULL)
2710 base_reg = new_base_reg = NULL_RTX;
2711 else
2713 base_reg = *base_term;
2714 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2716 index_term = strip_subreg (ad->index_term);
2717 if (index_term == NULL)
2718 index_reg = new_index_reg = NULL_RTX;
2719 else
2721 index_reg = *index_term;
2722 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2724 if (base_reg == new_base_reg && index_reg == new_index_reg)
2725 return false;
2726 disp = 0;
2727 change_p = false;
2728 if (lra_dump_file != NULL)
2730 fprintf (lra_dump_file, "Changing address in insn %d ",
2731 INSN_UID (curr_insn));
2732 dump_value_slim (lra_dump_file, *ad->outer, 1);
2734 if (base_reg != new_base_reg)
2736 if (REG_P (new_base_reg))
2738 *base_term = new_base_reg;
2739 change_p = true;
2741 else if (GET_CODE (new_base_reg) == PLUS
2742 && REG_P (XEXP (new_base_reg, 0))
2743 && CONST_INT_P (XEXP (new_base_reg, 1))
2744 && can_add_disp_p (ad))
2746 disp += INTVAL (XEXP (new_base_reg, 1));
2747 *base_term = XEXP (new_base_reg, 0);
2748 change_p = true;
2750 if (ad->base_term2 != NULL)
2751 *ad->base_term2 = *ad->base_term;
2753 if (index_reg != new_index_reg)
2755 if (REG_P (new_index_reg))
2757 *index_term = new_index_reg;
2758 change_p = true;
2760 else if (GET_CODE (new_index_reg) == PLUS
2761 && REG_P (XEXP (new_index_reg, 0))
2762 && CONST_INT_P (XEXP (new_index_reg, 1))
2763 && can_add_disp_p (ad)
2764 && (scale = get_index_scale (ad)))
2766 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2767 *index_term = XEXP (new_index_reg, 0);
2768 change_p = true;
2771 if (disp != 0)
2773 if (ad->disp != NULL)
2774 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2775 else
2777 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2778 update_address (ad);
2780 change_p = true;
2782 if (lra_dump_file != NULL)
2784 if (! change_p)
2785 fprintf (lra_dump_file, " -- no change\n");
2786 else
2788 fprintf (lra_dump_file, " on equiv ");
2789 dump_value_slim (lra_dump_file, *ad->outer, 1);
2790 fprintf (lra_dump_file, "\n");
2793 return change_p;
2796 /* Major function to make reloads for an address in operand NOP or
2797 check its correctness (If CHECK_ONLY_P is true). The supported
2798 cases are:
2800 1) an address that existed before LRA started, at which point it
2801 must have been valid. These addresses are subject to elimination
2802 and may have become invalid due to the elimination offset being out
2803 of range.
2805 2) an address created by forcing a constant to memory
2806 (force_const_to_mem). The initial form of these addresses might
2807 not be valid, and it is this function's job to make them valid.
2809 3) a frame address formed from a register and a (possibly zero)
2810 constant offset. As above, these addresses might not be valid and
2811 this function must make them so.
2813 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2814 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2815 address. Return true for any RTL change.
2817 The function is a helper function which does not produce all
2818 transformations (when CHECK_ONLY_P is false) which can be
2819 necessary. It does just basic steps. To do all necessary
2820 transformations use function process_address. */
2821 static bool
2822 process_address_1 (int nop, bool check_only_p,
2823 rtx_insn **before, rtx_insn **after)
2825 struct address_info ad;
2826 rtx new_reg;
2827 rtx op = *curr_id->operand_loc[nop];
2828 const char *constraint = curr_static_id->operand[nop].constraint;
2829 enum constraint_num cn = lookup_constraint (constraint);
2830 bool change_p = false;
2832 if (insn_extra_address_constraint (cn))
2833 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2834 else if (MEM_P (op))
2835 decompose_mem_address (&ad, op);
2836 else if (GET_CODE (op) == SUBREG
2837 && MEM_P (SUBREG_REG (op)))
2838 decompose_mem_address (&ad, SUBREG_REG (op));
2839 else
2840 return false;
2841 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
2842 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
2843 when INDEX_REG_CLASS is a single register class. */
2844 if (ad.base_term != NULL
2845 && ad.index_term != NULL
2846 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
2847 && REG_P (*ad.base_term)
2848 && REG_P (*ad.index_term)
2849 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
2850 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
2852 std::swap (ad.base, ad.index);
2853 std::swap (ad.base_term, ad.index_term);
2855 if (! check_only_p)
2856 change_p = equiv_address_substitution (&ad);
2857 if (ad.base_term != NULL
2858 && (process_addr_reg
2859 (ad.base_term, check_only_p, before,
2860 (ad.autoinc_p
2861 && !(REG_P (*ad.base_term)
2862 && find_regno_note (curr_insn, REG_DEAD,
2863 REGNO (*ad.base_term)) != NULL_RTX)
2864 ? after : NULL),
2865 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2866 get_index_code (&ad)))))
2868 change_p = true;
2869 if (ad.base_term2 != NULL)
2870 *ad.base_term2 = *ad.base_term;
2872 if (ad.index_term != NULL
2873 && process_addr_reg (ad.index_term, check_only_p,
2874 before, NULL, INDEX_REG_CLASS))
2875 change_p = true;
2877 /* Target hooks sometimes don't treat extra-constraint addresses as
2878 legitimate address_operands, so handle them specially. */
2879 if (insn_extra_address_constraint (cn)
2880 && satisfies_address_constraint_p (&ad, cn))
2881 return change_p;
2883 if (check_only_p)
2884 return change_p;
2886 /* There are three cases where the shape of *AD.INNER may now be invalid:
2888 1) the original address was valid, but either elimination or
2889 equiv_address_substitution was applied and that made
2890 the address invalid.
2892 2) the address is an invalid symbolic address created by
2893 force_const_to_mem.
2895 3) the address is a frame address with an invalid offset.
2897 4) the address is a frame address with an invalid base.
2899 All these cases involve a non-autoinc address, so there is no
2900 point revalidating other types. */
2901 if (ad.autoinc_p || valid_address_p (&ad))
2902 return change_p;
2904 /* Any index existed before LRA started, so we can assume that the
2905 presence and shape of the index is valid. */
2906 push_to_sequence (*before);
2907 lra_assert (ad.disp == ad.disp_term);
2908 if (ad.base == NULL)
2910 if (ad.index == NULL)
2912 int code = -1;
2913 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2914 SCRATCH, SCRATCH);
2915 rtx addr = *ad.inner;
2917 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2918 #ifdef HAVE_lo_sum
2920 rtx_insn *insn;
2921 rtx_insn *last = get_last_insn ();
2923 /* addr => lo_sum (new_base, addr), case (2) above. */
2924 insn = emit_insn (gen_rtx_SET
2925 (VOIDmode, new_reg,
2926 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2927 code = recog_memoized (insn);
2928 if (code >= 0)
2930 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2931 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2933 /* Try to put lo_sum into register. */
2934 insn = emit_insn (gen_rtx_SET
2935 (VOIDmode, new_reg,
2936 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2937 code = recog_memoized (insn);
2938 if (code >= 0)
2940 *ad.inner = new_reg;
2941 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2943 *ad.inner = addr;
2944 code = -1;
2950 if (code < 0)
2951 delete_insns_since (last);
2953 #endif
2954 if (code < 0)
2956 /* addr => new_base, case (2) above. */
2957 lra_emit_move (new_reg, addr);
2958 *ad.inner = new_reg;
2961 else
2963 /* index * scale + disp => new base + index * scale,
2964 case (1) above. */
2965 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
2966 GET_CODE (*ad.index));
2968 lra_assert (INDEX_REG_CLASS != NO_REGS);
2969 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2970 lra_emit_move (new_reg, *ad.disp);
2971 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2972 new_reg, *ad.index);
2975 else if (ad.index == NULL)
2977 int regno;
2978 enum reg_class cl;
2979 rtx set;
2980 rtx_insn *insns, *last_insn;
2981 /* Try to reload base into register only if the base is invalid
2982 for the address but with valid offset, case (4) above. */
2983 start_sequence ();
2984 new_reg = base_to_reg (&ad);
2986 /* base + disp => new base, cases (1) and (3) above. */
2987 /* Another option would be to reload the displacement into an
2988 index register. However, postreload has code to optimize
2989 address reloads that have the same base and different
2990 displacements, so reloading into an index register would
2991 not necessarily be a win. */
2992 if (new_reg == NULL_RTX)
2993 new_reg = base_plus_disp_to_reg (&ad);
2994 insns = get_insns ();
2995 last_insn = get_last_insn ();
2996 /* If we generated at least two insns, try last insn source as
2997 an address. If we succeed, we generate one less insn. */
2998 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
2999 && GET_CODE (SET_SRC (set)) == PLUS
3000 && REG_P (XEXP (SET_SRC (set), 0))
3001 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3003 *ad.inner = SET_SRC (set);
3004 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3006 *ad.base_term = XEXP (SET_SRC (set), 0);
3007 *ad.disp_term = XEXP (SET_SRC (set), 1);
3008 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3009 get_index_code (&ad));
3010 regno = REGNO (*ad.base_term);
3011 if (regno >= FIRST_PSEUDO_REGISTER
3012 && cl != lra_get_allocno_class (regno))
3013 lra_change_class (regno, cl, " Change to", true);
3014 new_reg = SET_SRC (set);
3015 delete_insns_since (PREV_INSN (last_insn));
3018 /* Try if target can split displacement into legitimite new disp
3019 and offset. If it's the case, we replace the last insn with
3020 insns for base + offset => new_reg and set new_reg + new disp
3021 to *ad.inner. */
3022 last_insn = get_last_insn ();
3023 if ((set = single_set (last_insn)) != NULL_RTX
3024 && GET_CODE (SET_SRC (set)) == PLUS
3025 && REG_P (XEXP (SET_SRC (set), 0))
3026 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3027 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3029 rtx addend, disp = XEXP (SET_SRC (set), 1);
3030 if (targetm.legitimize_address_displacement (&disp, &addend,
3031 ad.mode))
3033 rtx_insn *new_insns;
3034 start_sequence ();
3035 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3036 new_insns = get_insns ();
3037 end_sequence ();
3038 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3039 delete_insns_since (PREV_INSN (last_insn));
3040 add_insn (new_insns);
3041 insns = get_insns ();
3044 end_sequence ();
3045 emit_insn (insns);
3046 *ad.inner = new_reg;
3048 else if (ad.disp_term != NULL)
3050 /* base + scale * index + disp => new base + scale * index,
3051 case (1) above. */
3052 new_reg = base_plus_disp_to_reg (&ad);
3053 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3054 new_reg, *ad.index);
3056 else if (get_index_scale (&ad) == 1)
3058 /* The last transformation to one reg will be made in
3059 curr_insn_transform function. */
3060 end_sequence ();
3061 return false;
3063 else
3065 /* base + scale * index => base + new_reg,
3066 case (1) above.
3067 Index part of address may become invalid. For example, we
3068 changed pseudo on the equivalent memory and a subreg of the
3069 pseudo onto the memory of different mode for which the scale is
3070 prohibitted. */
3071 new_reg = index_part_to_reg (&ad);
3072 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3073 *ad.base_term, new_reg);
3075 *before = get_insns ();
3076 end_sequence ();
3077 return true;
3080 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3081 Use process_address_1 as a helper function. Return true for any
3082 RTL changes.
3084 If CHECK_ONLY_P is true, just check address correctness. Return
3085 false if the address correct. */
3086 static bool
3087 process_address (int nop, bool check_only_p,
3088 rtx_insn **before, rtx_insn **after)
3090 bool res = false;
3092 while (process_address_1 (nop, check_only_p, before, after))
3094 if (check_only_p)
3095 return true;
3096 res = true;
3098 return res;
3101 /* Emit insns to reload VALUE into a new register. VALUE is an
3102 auto-increment or auto-decrement RTX whose operand is a register or
3103 memory location; so reloading involves incrementing that location.
3104 IN is either identical to VALUE, or some cheaper place to reload
3105 value being incremented/decremented from.
3107 INC_AMOUNT is the number to increment or decrement by (always
3108 positive and ignored for POST_MODIFY/PRE_MODIFY).
3110 Return pseudo containing the result. */
3111 static rtx
3112 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3114 /* REG or MEM to be copied and incremented. */
3115 rtx incloc = XEXP (value, 0);
3116 /* Nonzero if increment after copying. */
3117 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3118 || GET_CODE (value) == POST_MODIFY);
3119 rtx_insn *last;
3120 rtx inc;
3121 rtx_insn *add_insn;
3122 int code;
3123 rtx real_in = in == value ? incloc : in;
3124 rtx result;
3125 bool plus_p = true;
3127 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3129 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3130 || GET_CODE (XEXP (value, 1)) == MINUS);
3131 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3132 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3133 inc = XEXP (XEXP (value, 1), 1);
3135 else
3137 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3138 inc_amount = -inc_amount;
3140 inc = GEN_INT (inc_amount);
3143 if (! post && REG_P (incloc))
3144 result = incloc;
3145 else
3146 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3147 "INC/DEC result");
3149 if (real_in != result)
3151 /* First copy the location to the result register. */
3152 lra_assert (REG_P (result));
3153 emit_insn (gen_move_insn (result, real_in));
3156 /* We suppose that there are insns to add/sub with the constant
3157 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3158 old reload worked with this assumption. If the assumption
3159 becomes wrong, we should use approach in function
3160 base_plus_disp_to_reg. */
3161 if (in == value)
3163 /* See if we can directly increment INCLOC. */
3164 last = get_last_insn ();
3165 add_insn = emit_insn (plus_p
3166 ? gen_add2_insn (incloc, inc)
3167 : gen_sub2_insn (incloc, inc));
3169 code = recog_memoized (add_insn);
3170 if (code >= 0)
3172 if (! post && result != incloc)
3173 emit_insn (gen_move_insn (result, incloc));
3174 return result;
3176 delete_insns_since (last);
3179 /* If couldn't do the increment directly, must increment in RESULT.
3180 The way we do this depends on whether this is pre- or
3181 post-increment. For pre-increment, copy INCLOC to the reload
3182 register, increment it there, then save back. */
3183 if (! post)
3185 if (real_in != result)
3186 emit_insn (gen_move_insn (result, real_in));
3187 if (plus_p)
3188 emit_insn (gen_add2_insn (result, inc));
3189 else
3190 emit_insn (gen_sub2_insn (result, inc));
3191 if (result != incloc)
3192 emit_insn (gen_move_insn (incloc, result));
3194 else
3196 /* Post-increment.
3198 Because this might be a jump insn or a compare, and because
3199 RESULT may not be available after the insn in an input
3200 reload, we must do the incrementing before the insn being
3201 reloaded for.
3203 We have already copied IN to RESULT. Increment the copy in
3204 RESULT, save that back, then decrement RESULT so it has
3205 the original value. */
3206 if (plus_p)
3207 emit_insn (gen_add2_insn (result, inc));
3208 else
3209 emit_insn (gen_sub2_insn (result, inc));
3210 emit_insn (gen_move_insn (incloc, result));
3211 /* Restore non-modified value for the result. We prefer this
3212 way because it does not require an additional hard
3213 register. */
3214 if (plus_p)
3216 if (CONST_INT_P (inc))
3217 emit_insn (gen_add2_insn (result,
3218 gen_int_mode (-INTVAL (inc),
3219 GET_MODE (result))));
3220 else
3221 emit_insn (gen_sub2_insn (result, inc));
3223 else
3224 emit_insn (gen_add2_insn (result, inc));
3226 return result;
3229 /* Return true if the current move insn does not need processing as we
3230 already know that it satisfies its constraints. */
3231 static bool
3232 simple_move_p (void)
3234 rtx dest, src;
3235 enum reg_class dclass, sclass;
3237 lra_assert (curr_insn_set != NULL_RTX);
3238 dest = SET_DEST (curr_insn_set);
3239 src = SET_SRC (curr_insn_set);
3240 return ((dclass = get_op_class (dest)) != NO_REGS
3241 && (sclass = get_op_class (src)) != NO_REGS
3242 /* The backend guarantees that register moves of cost 2
3243 never need reloads. */
3244 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3247 /* Swap operands NOP and NOP + 1. */
3248 static inline void
3249 swap_operands (int nop)
3251 machine_mode mode = curr_operand_mode[nop];
3252 curr_operand_mode[nop] = curr_operand_mode[nop + 1];
3253 curr_operand_mode[nop + 1] = mode;
3254 rtx x = *curr_id->operand_loc[nop];
3255 *curr_id->operand_loc[nop] = *curr_id->operand_loc[nop + 1];
3256 *curr_id->operand_loc[nop + 1] = x;
3257 /* Swap the duplicates too. */
3258 lra_update_dup (curr_id, nop);
3259 lra_update_dup (curr_id, nop + 1);
3262 /* Main entry point of the constraint code: search the body of the
3263 current insn to choose the best alternative. It is mimicking insn
3264 alternative cost calculation model of former reload pass. That is
3265 because machine descriptions were written to use this model. This
3266 model can be changed in future. Make commutative operand exchange
3267 if it is chosen.
3269 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3270 constraints. Return true if any change happened during function
3271 call.
3273 If CHECK_ONLY_P is true then don't do any transformation. Just
3274 check that the insn satisfies all constraints. If the insn does
3275 not satisfy any constraint, return true. */
3276 static bool
3277 curr_insn_transform (bool check_only_p)
3279 int i, j, k;
3280 int n_operands;
3281 int n_alternatives;
3282 int commutative;
3283 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3284 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3285 rtx_insn *before, *after;
3286 bool alt_p = false;
3287 /* Flag that the insn has been changed through a transformation. */
3288 bool change_p;
3289 bool sec_mem_p;
3290 #ifdef SECONDARY_MEMORY_NEEDED
3291 bool use_sec_mem_p;
3292 #endif
3293 int max_regno_before;
3294 int reused_alternative_num;
3296 curr_insn_set = single_set (curr_insn);
3297 if (curr_insn_set != NULL_RTX && simple_move_p ())
3298 return false;
3300 no_input_reloads_p = no_output_reloads_p = false;
3301 goal_alt_number = -1;
3302 change_p = sec_mem_p = false;
3303 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3304 reloads; neither are insns that SET cc0. Insns that use CC0 are
3305 not allowed to have any input reloads. */
3306 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3307 no_output_reloads_p = true;
3309 #ifdef HAVE_cc0
3310 if (reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3311 no_input_reloads_p = true;
3312 if (reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3313 no_output_reloads_p = true;
3314 #endif
3316 n_operands = curr_static_id->n_operands;
3317 n_alternatives = curr_static_id->n_alternatives;
3319 /* Just return "no reloads" if insn has no operands with
3320 constraints. */
3321 if (n_operands == 0 || n_alternatives == 0)
3322 return false;
3324 max_regno_before = max_reg_num ();
3326 for (i = 0; i < n_operands; i++)
3328 goal_alt_matched[i][0] = -1;
3329 goal_alt_matches[i] = -1;
3332 commutative = curr_static_id->commutative;
3334 /* Now see what we need for pseudos that didn't get hard regs or got
3335 the wrong kind of hard reg. For this, we must consider all the
3336 operands together against the register constraints. */
3338 best_losers = best_overall = INT_MAX;
3339 best_reload_sum = 0;
3341 curr_swapped = false;
3342 goal_alt_swapped = false;
3344 if (! check_only_p)
3345 /* Make equivalence substitution and memory subreg elimination
3346 before address processing because an address legitimacy can
3347 depend on memory mode. */
3348 for (i = 0; i < n_operands; i++)
3350 rtx op = *curr_id->operand_loc[i];
3351 rtx subst, old = op;
3352 bool op_change_p = false;
3354 if (GET_CODE (old) == SUBREG)
3355 old = SUBREG_REG (old);
3356 subst = get_equiv_with_elimination (old, curr_insn);
3357 if (subst != old)
3359 subst = copy_rtx (subst);
3360 lra_assert (REG_P (old));
3361 if (GET_CODE (op) == SUBREG)
3362 SUBREG_REG (op) = subst;
3363 else
3364 *curr_id->operand_loc[i] = subst;
3365 if (lra_dump_file != NULL)
3367 fprintf (lra_dump_file,
3368 "Changing pseudo %d in operand %i of insn %u on equiv ",
3369 REGNO (old), i, INSN_UID (curr_insn));
3370 dump_value_slim (lra_dump_file, subst, 1);
3371 fprintf (lra_dump_file, "\n");
3373 op_change_p = change_p = true;
3375 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3377 change_p = true;
3378 lra_update_dup (curr_id, i);
3382 /* Reload address registers and displacements. We do it before
3383 finding an alternative because of memory constraints. */
3384 before = after = NULL;
3385 for (i = 0; i < n_operands; i++)
3386 if (! curr_static_id->operand[i].is_operator
3387 && process_address (i, check_only_p, &before, &after))
3389 if (check_only_p)
3390 return true;
3391 change_p = true;
3392 lra_update_dup (curr_id, i);
3395 if (change_p)
3396 /* If we've changed the instruction then any alternative that
3397 we chose previously may no longer be valid. */
3398 lra_set_used_insn_alternative (curr_insn, -1);
3400 if (! check_only_p && curr_insn_set != NULL_RTX
3401 && check_and_process_move (&change_p, &sec_mem_p))
3402 return change_p;
3404 try_swapped:
3406 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3407 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3408 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3409 reused_alternative_num, INSN_UID (curr_insn));
3411 if (process_alt_operands (reused_alternative_num))
3412 alt_p = true;
3414 if (check_only_p)
3415 return ! alt_p || best_losers != 0;
3417 /* If insn is commutative (it's safe to exchange a certain pair of
3418 operands) then we need to try each alternative twice, the second
3419 time matching those two operands as if we had exchanged them. To
3420 do this, really exchange them in operands.
3422 If we have just tried the alternatives the second time, return
3423 operands to normal and drop through. */
3425 if (reused_alternative_num < 0 && commutative >= 0)
3427 curr_swapped = !curr_swapped;
3428 if (curr_swapped)
3430 swap_operands (commutative);
3431 goto try_swapped;
3433 else
3434 swap_operands (commutative);
3437 if (! alt_p && ! sec_mem_p)
3439 /* No alternative works with reloads?? */
3440 if (INSN_CODE (curr_insn) >= 0)
3441 fatal_insn ("unable to generate reloads for:", curr_insn);
3442 error_for_asm (curr_insn,
3443 "inconsistent operand constraints in an %<asm%>");
3444 /* Avoid further trouble with this insn. */
3445 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3446 lra_invalidate_insn_data (curr_insn);
3447 return true;
3450 /* If the best alternative is with operands 1 and 2 swapped, swap
3451 them. Update the operand numbers of any reloads already
3452 pushed. */
3454 if (goal_alt_swapped)
3456 if (lra_dump_file != NULL)
3457 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3458 INSN_UID (curr_insn));
3460 /* Swap the duplicates too. */
3461 swap_operands (commutative);
3462 change_p = true;
3465 #ifdef SECONDARY_MEMORY_NEEDED
3466 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3467 too conservatively. So we use the secondary memory only if there
3468 is no any alternative without reloads. */
3469 use_sec_mem_p = false;
3470 if (! alt_p)
3471 use_sec_mem_p = true;
3472 else if (sec_mem_p)
3474 for (i = 0; i < n_operands; i++)
3475 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3476 break;
3477 use_sec_mem_p = i < n_operands;
3480 if (use_sec_mem_p)
3482 rtx new_reg, src, dest, rld;
3483 machine_mode sec_mode, rld_mode;
3485 lra_assert (sec_mem_p);
3486 lra_assert (curr_static_id->operand[0].type == OP_OUT
3487 && curr_static_id->operand[1].type == OP_IN);
3488 dest = *curr_id->operand_loc[0];
3489 src = *curr_id->operand_loc[1];
3490 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3491 ? dest : src);
3492 rld_mode = GET_MODE (rld);
3493 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3494 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3495 #else
3496 sec_mode = rld_mode;
3497 #endif
3498 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3499 NO_REGS, "secondary");
3500 /* If the mode is changed, it should be wider. */
3501 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3502 if (sec_mode != rld_mode)
3504 /* If the target says specifically to use another mode for
3505 secondary memory moves we can not reuse the original
3506 insn. */
3507 after = emit_spill_move (false, new_reg, dest);
3508 lra_process_new_insns (curr_insn, NULL, after,
3509 "Inserting the sec. move");
3510 /* We may have non null BEFORE here (e.g. after address
3511 processing. */
3512 push_to_sequence (before);
3513 before = emit_spill_move (true, new_reg, src);
3514 emit_insn (before);
3515 before = get_insns ();
3516 end_sequence ();
3517 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3518 lra_set_insn_deleted (curr_insn);
3520 else if (dest == rld)
3522 *curr_id->operand_loc[0] = new_reg;
3523 after = emit_spill_move (false, new_reg, dest);
3524 lra_process_new_insns (curr_insn, NULL, after,
3525 "Inserting the sec. move");
3527 else
3529 *curr_id->operand_loc[1] = new_reg;
3530 /* See comments above. */
3531 push_to_sequence (before);
3532 before = emit_spill_move (true, new_reg, src);
3533 emit_insn (before);
3534 before = get_insns ();
3535 end_sequence ();
3536 lra_process_new_insns (curr_insn, before, NULL,
3537 "Inserting the sec. move");
3539 lra_update_insn_regno_info (curr_insn);
3540 return true;
3542 #endif
3544 lra_assert (goal_alt_number >= 0);
3545 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3547 if (lra_dump_file != NULL)
3549 const char *p;
3551 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3552 goal_alt_number, INSN_UID (curr_insn));
3553 for (i = 0; i < n_operands; i++)
3555 p = (curr_static_id->operand_alternative
3556 [goal_alt_number * n_operands + i].constraint);
3557 if (*p == '\0')
3558 continue;
3559 fprintf (lra_dump_file, " (%d) ", i);
3560 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3561 fputc (*p, lra_dump_file);
3563 if (INSN_CODE (curr_insn) >= 0
3564 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3565 fprintf (lra_dump_file, " {%s}", p);
3566 if (curr_id->sp_offset != 0)
3567 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3568 curr_id->sp_offset);
3569 fprintf (lra_dump_file, "\n");
3572 /* Right now, for any pair of operands I and J that are required to
3573 match, with J < I, goal_alt_matches[I] is J. Add I to
3574 goal_alt_matched[J]. */
3576 for (i = 0; i < n_operands; i++)
3577 if ((j = goal_alt_matches[i]) >= 0)
3579 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3581 /* We allow matching one output operand and several input
3582 operands. */
3583 lra_assert (k == 0
3584 || (curr_static_id->operand[j].type == OP_OUT
3585 && curr_static_id->operand[i].type == OP_IN
3586 && (curr_static_id->operand
3587 [goal_alt_matched[j][0]].type == OP_IN)));
3588 goal_alt_matched[j][k] = i;
3589 goal_alt_matched[j][k + 1] = -1;
3592 for (i = 0; i < n_operands; i++)
3593 goal_alt_win[i] |= goal_alt_match_win[i];
3595 /* Any constants that aren't allowed and can't be reloaded into
3596 registers are here changed into memory references. */
3597 for (i = 0; i < n_operands; i++)
3598 if (goal_alt_win[i])
3600 int regno;
3601 enum reg_class new_class;
3602 rtx reg = *curr_id->operand_loc[i];
3604 if (GET_CODE (reg) == SUBREG)
3605 reg = SUBREG_REG (reg);
3607 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3609 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3611 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3613 lra_assert (ok_p);
3614 lra_change_class (regno, new_class, " Change to", true);
3618 else
3620 const char *constraint;
3621 char c;
3622 rtx op = *curr_id->operand_loc[i];
3623 rtx subreg = NULL_RTX;
3624 machine_mode mode = curr_operand_mode[i];
3626 if (GET_CODE (op) == SUBREG)
3628 subreg = op;
3629 op = SUBREG_REG (op);
3630 mode = GET_MODE (op);
3633 if (CONST_POOL_OK_P (mode, op)
3634 && ((targetm.preferred_reload_class
3635 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3636 || no_input_reloads_p))
3638 rtx tem = force_const_mem (mode, op);
3640 change_p = true;
3641 if (subreg != NULL_RTX)
3642 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3644 *curr_id->operand_loc[i] = tem;
3645 lra_update_dup (curr_id, i);
3646 process_address (i, false, &before, &after);
3648 /* If the alternative accepts constant pool refs directly
3649 there will be no reload needed at all. */
3650 if (subreg != NULL_RTX)
3651 continue;
3652 /* Skip alternatives before the one requested. */
3653 constraint = (curr_static_id->operand_alternative
3654 [goal_alt_number * n_operands + i].constraint);
3655 for (;
3656 (c = *constraint) && c != ',' && c != '#';
3657 constraint += CONSTRAINT_LEN (c, constraint))
3659 enum constraint_num cn = lookup_constraint (constraint);
3660 if (insn_extra_memory_constraint (cn)
3661 && satisfies_memory_constraint_p (tem, cn))
3662 break;
3664 if (c == '\0' || c == ',' || c == '#')
3665 continue;
3667 goal_alt_win[i] = true;
3671 for (i = 0; i < n_operands; i++)
3673 int regno;
3674 bool optional_p = false;
3675 rtx old, new_reg;
3676 rtx op = *curr_id->operand_loc[i];
3678 if (goal_alt_win[i])
3680 if (goal_alt[i] == NO_REGS
3681 && REG_P (op)
3682 /* When we assign NO_REGS it means that we will not
3683 assign a hard register to the scratch pseudo by
3684 assigment pass and the scratch pseudo will be
3685 spilled. Spilled scratch pseudos are transformed
3686 back to scratches at the LRA end. */
3687 && lra_former_scratch_operand_p (curr_insn, i))
3689 int regno = REGNO (op);
3690 lra_change_class (regno, NO_REGS, " Change to", true);
3691 if (lra_get_regno_hard_regno (regno) >= 0)
3692 /* We don't have to mark all insn affected by the
3693 spilled pseudo as there is only one such insn, the
3694 current one. */
3695 reg_renumber[regno] = -1;
3697 /* We can do an optional reload. If the pseudo got a hard
3698 reg, we might improve the code through inheritance. If
3699 it does not get a hard register we coalesce memory/memory
3700 moves later. Ignore move insns to avoid cycling. */
3701 if (! lra_simple_p
3702 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3703 && goal_alt[i] != NO_REGS && REG_P (op)
3704 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3705 && regno < new_regno_start
3706 && ! lra_former_scratch_p (regno)
3707 && reg_renumber[regno] < 0
3708 && (curr_insn_set == NULL_RTX
3709 || !((REG_P (SET_SRC (curr_insn_set))
3710 || MEM_P (SET_SRC (curr_insn_set))
3711 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3712 && (REG_P (SET_DEST (curr_insn_set))
3713 || MEM_P (SET_DEST (curr_insn_set))
3714 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3715 optional_p = true;
3716 else
3717 continue;
3720 /* Operands that match previous ones have already been handled. */
3721 if (goal_alt_matches[i] >= 0)
3722 continue;
3724 /* We should not have an operand with a non-offsettable address
3725 appearing where an offsettable address will do. It also may
3726 be a case when the address should be special in other words
3727 not a general one (e.g. it needs no index reg). */
3728 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3730 enum reg_class rclass;
3731 rtx *loc = &XEXP (op, 0);
3732 enum rtx_code code = GET_CODE (*loc);
3734 push_to_sequence (before);
3735 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3736 MEM, SCRATCH);
3737 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3738 new_reg = emit_inc (rclass, *loc, *loc,
3739 /* This value does not matter for MODIFY. */
3740 GET_MODE_SIZE (GET_MODE (op)));
3741 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3742 "offsetable address", &new_reg))
3743 lra_emit_move (new_reg, *loc);
3744 before = get_insns ();
3745 end_sequence ();
3746 *loc = new_reg;
3747 lra_update_dup (curr_id, i);
3749 else if (goal_alt_matched[i][0] == -1)
3751 machine_mode mode;
3752 rtx reg, *loc;
3753 int hard_regno, byte;
3754 enum op_type type = curr_static_id->operand[i].type;
3756 loc = curr_id->operand_loc[i];
3757 mode = curr_operand_mode[i];
3758 if (GET_CODE (*loc) == SUBREG)
3760 reg = SUBREG_REG (*loc);
3761 byte = SUBREG_BYTE (*loc);
3762 if (REG_P (reg)
3763 /* Strict_low_part requires reload the register not
3764 the sub-register. */
3765 && (curr_static_id->operand[i].strict_low
3766 || (GET_MODE_SIZE (mode)
3767 <= GET_MODE_SIZE (GET_MODE (reg))
3768 && (hard_regno
3769 = get_try_hard_regno (REGNO (reg))) >= 0
3770 && (simplify_subreg_regno
3771 (hard_regno,
3772 GET_MODE (reg), byte, mode) < 0)
3773 && (goal_alt[i] == NO_REGS
3774 || (simplify_subreg_regno
3775 (ira_class_hard_regs[goal_alt[i]][0],
3776 GET_MODE (reg), byte, mode) >= 0)))))
3778 loc = &SUBREG_REG (*loc);
3779 mode = GET_MODE (*loc);
3782 old = *loc;
3783 if (get_reload_reg (type, mode, old, goal_alt[i],
3784 loc != curr_id->operand_loc[i], "", &new_reg)
3785 && type != OP_OUT)
3787 push_to_sequence (before);
3788 lra_emit_move (new_reg, old);
3789 before = get_insns ();
3790 end_sequence ();
3792 *loc = new_reg;
3793 if (type != OP_IN
3794 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3796 start_sequence ();
3797 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3798 emit_insn (after);
3799 after = get_insns ();
3800 end_sequence ();
3801 *loc = new_reg;
3803 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3804 if (goal_alt_dont_inherit_ops[j] == i)
3806 lra_set_regno_unique_value (REGNO (new_reg));
3807 break;
3809 lra_update_dup (curr_id, i);
3811 else if (curr_static_id->operand[i].type == OP_IN
3812 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3813 == OP_OUT))
3815 /* generate reloads for input and matched outputs. */
3816 match_inputs[0] = i;
3817 match_inputs[1] = -1;
3818 match_reload (goal_alt_matched[i][0], match_inputs,
3819 goal_alt[i], &before, &after);
3821 else if (curr_static_id->operand[i].type == OP_OUT
3822 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3823 == OP_IN))
3824 /* Generate reloads for output and matched inputs. */
3825 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3826 else if (curr_static_id->operand[i].type == OP_IN
3827 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3828 == OP_IN))
3830 /* Generate reloads for matched inputs. */
3831 match_inputs[0] = i;
3832 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3833 match_inputs[j + 1] = k;
3834 match_inputs[j + 1] = -1;
3835 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3837 else
3838 /* We must generate code in any case when function
3839 process_alt_operands decides that it is possible. */
3840 gcc_unreachable ();
3841 if (optional_p)
3843 lra_assert (REG_P (op));
3844 regno = REGNO (op);
3845 op = *curr_id->operand_loc[i]; /* Substitution. */
3846 if (GET_CODE (op) == SUBREG)
3847 op = SUBREG_REG (op);
3848 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3849 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3850 lra_reg_info[REGNO (op)].restore_regno = regno;
3851 if (lra_dump_file != NULL)
3852 fprintf (lra_dump_file,
3853 " Making reload reg %d for reg %d optional\n",
3854 REGNO (op), regno);
3857 if (before != NULL_RTX || after != NULL_RTX
3858 || max_regno_before != max_reg_num ())
3859 change_p = true;
3860 if (change_p)
3862 lra_update_operator_dups (curr_id);
3863 /* Something changes -- process the insn. */
3864 lra_update_insn_regno_info (curr_insn);
3866 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3867 return change_p;
3870 /* Return true if INSN satisfies all constraints. In other words, no
3871 reload insns are needed. */
3872 bool
3873 lra_constrain_insn (rtx_insn *insn)
3875 int saved_new_regno_start = new_regno_start;
3876 int saved_new_insn_uid_start = new_insn_uid_start;
3877 bool change_p;
3879 curr_insn = insn;
3880 curr_id = lra_get_insn_recog_data (curr_insn);
3881 curr_static_id = curr_id->insn_static_data;
3882 new_insn_uid_start = get_max_uid ();
3883 new_regno_start = max_reg_num ();
3884 change_p = curr_insn_transform (true);
3885 new_regno_start = saved_new_regno_start;
3886 new_insn_uid_start = saved_new_insn_uid_start;
3887 return ! change_p;
3890 /* Return true if X is in LIST. */
3891 static bool
3892 in_list_p (rtx x, rtx list)
3894 for (; list != NULL_RTX; list = XEXP (list, 1))
3895 if (XEXP (list, 0) == x)
3896 return true;
3897 return false;
3900 /* Return true if X contains an allocatable hard register (if
3901 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3902 static bool
3903 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3905 int i, j;
3906 const char *fmt;
3907 enum rtx_code code;
3909 code = GET_CODE (x);
3910 if (REG_P (x))
3912 int regno = REGNO (x);
3913 HARD_REG_SET alloc_regs;
3915 if (hard_reg_p)
3917 if (regno >= FIRST_PSEUDO_REGISTER)
3918 regno = lra_get_regno_hard_regno (regno);
3919 if (regno < 0)
3920 return false;
3921 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3922 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3924 else
3926 if (regno < FIRST_PSEUDO_REGISTER)
3927 return false;
3928 if (! spilled_p)
3929 return true;
3930 return lra_get_regno_hard_regno (regno) < 0;
3933 fmt = GET_RTX_FORMAT (code);
3934 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3936 if (fmt[i] == 'e')
3938 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3939 return true;
3941 else if (fmt[i] == 'E')
3943 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3944 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3945 return true;
3948 return false;
3951 /* Return true if X contains a symbol reg. */
3952 static bool
3953 contains_symbol_ref_p (rtx x)
3955 int i, j;
3956 const char *fmt;
3957 enum rtx_code code;
3959 code = GET_CODE (x);
3960 if (code == SYMBOL_REF)
3961 return true;
3962 fmt = GET_RTX_FORMAT (code);
3963 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3965 if (fmt[i] == 'e')
3967 if (contains_symbol_ref_p (XEXP (x, i)))
3968 return true;
3970 else if (fmt[i] == 'E')
3972 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3973 if (contains_symbol_ref_p (XVECEXP (x, i, j)))
3974 return true;
3977 return false;
3980 /* Process all regs in location *LOC and change them on equivalent
3981 substitution. Return true if any change was done. */
3982 static bool
3983 loc_equivalence_change_p (rtx *loc)
3985 rtx subst, reg, x = *loc;
3986 bool result = false;
3987 enum rtx_code code = GET_CODE (x);
3988 const char *fmt;
3989 int i, j;
3991 if (code == SUBREG)
3993 reg = SUBREG_REG (x);
3994 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
3995 && GET_MODE (subst) == VOIDmode)
3997 /* We cannot reload debug location. Simplify subreg here
3998 while we know the inner mode. */
3999 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4000 GET_MODE (reg), SUBREG_BYTE (x));
4001 return true;
4004 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4006 *loc = subst;
4007 return true;
4010 /* Scan all the operand sub-expressions. */
4011 fmt = GET_RTX_FORMAT (code);
4012 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4014 if (fmt[i] == 'e')
4015 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4016 else if (fmt[i] == 'E')
4017 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4018 result
4019 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4021 return result;
4024 /* Similar to loc_equivalence_change_p, but for use as
4025 simplify_replace_fn_rtx callback. DATA is insn for which the
4026 elimination is done. If it null we don't do the elimination. */
4027 static rtx
4028 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4030 if (!REG_P (loc))
4031 return NULL_RTX;
4033 rtx subst = (data == NULL
4034 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4035 if (subst != loc)
4036 return subst;
4038 return NULL_RTX;
4041 /* Maximum number of generated reload insns per an insn. It is for
4042 preventing this pass cycling in a bug case. */
4043 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4045 /* The current iteration number of this LRA pass. */
4046 int lra_constraint_iter;
4048 /* True if we substituted equiv which needs checking register
4049 allocation correctness because the equivalent value contains
4050 allocatable hard registers or when we restore multi-register
4051 pseudo. */
4052 bool lra_risky_transformations_p;
4054 /* Return true if REGNO is referenced in more than one block. */
4055 static bool
4056 multi_block_pseudo_p (int regno)
4058 basic_block bb = NULL;
4059 unsigned int uid;
4060 bitmap_iterator bi;
4062 if (regno < FIRST_PSEUDO_REGISTER)
4063 return false;
4065 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4066 if (bb == NULL)
4067 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4068 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4069 return true;
4070 return false;
4073 /* Return true if LIST contains a deleted insn. */
4074 static bool
4075 contains_deleted_insn_p (rtx_insn_list *list)
4077 for (; list != NULL_RTX; list = list->next ())
4078 if (NOTE_P (list->insn ())
4079 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4080 return true;
4081 return false;
4084 /* Return true if X contains a pseudo dying in INSN. */
4085 static bool
4086 dead_pseudo_p (rtx x, rtx insn)
4088 int i, j;
4089 const char *fmt;
4090 enum rtx_code code;
4092 if (REG_P (x))
4093 return (insn != NULL_RTX
4094 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4095 code = GET_CODE (x);
4096 fmt = GET_RTX_FORMAT (code);
4097 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4099 if (fmt[i] == 'e')
4101 if (dead_pseudo_p (XEXP (x, i), insn))
4102 return true;
4104 else if (fmt[i] == 'E')
4106 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4107 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4108 return true;
4111 return false;
4114 /* Return true if INSN contains a dying pseudo in INSN right hand
4115 side. */
4116 static bool
4117 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4119 rtx set = single_set (insn);
4121 gcc_assert (set != NULL);
4122 return dead_pseudo_p (SET_SRC (set), insn);
4125 /* Return true if any init insn of REGNO contains a dying pseudo in
4126 insn right hand side. */
4127 static bool
4128 init_insn_rhs_dead_pseudo_p (int regno)
4130 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4132 if (insns == NULL)
4133 return false;
4134 for (; insns != NULL_RTX; insns = insns->next ())
4135 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4136 return true;
4137 return false;
4140 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4141 reverse only if we have one init insn with given REGNO as a
4142 source. */
4143 static bool
4144 reverse_equiv_p (int regno)
4146 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4147 rtx set;
4149 if (insns == NULL)
4150 return false;
4151 if (! INSN_P (insns->insn ())
4152 || insns->next () != NULL)
4153 return false;
4154 if ((set = single_set (insns->insn ())) == NULL_RTX)
4155 return false;
4156 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4159 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4160 call this function only for non-reverse equivalence. */
4161 static bool
4162 contains_reloaded_insn_p (int regno)
4164 rtx set;
4165 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4167 for (; list != NULL; list = list->next ())
4168 if ((set = single_set (list->insn ())) == NULL_RTX
4169 || ! REG_P (SET_DEST (set))
4170 || (int) REGNO (SET_DEST (set)) != regno)
4171 return true;
4172 return false;
4175 /* Entry function of LRA constraint pass. Return true if the
4176 constraint pass did change the code. */
4177 bool
4178 lra_constraints (bool first_p)
4180 bool changed_p;
4181 int i, hard_regno, new_insns_num;
4182 unsigned int min_len, new_min_len, uid;
4183 rtx set, x, reg, dest_reg;
4184 basic_block last_bb;
4185 bitmap_head equiv_insn_bitmap;
4186 bitmap_iterator bi;
4188 lra_constraint_iter++;
4189 if (lra_dump_file != NULL)
4190 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4191 lra_constraint_iter);
4192 changed_p = false;
4193 if (pic_offset_table_rtx
4194 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4195 lra_risky_transformations_p = true;
4196 else
4197 lra_risky_transformations_p = false;
4198 new_insn_uid_start = get_max_uid ();
4199 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4200 /* Mark used hard regs for target stack size calulations. */
4201 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4202 if (lra_reg_info[i].nrefs != 0
4203 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4205 int j, nregs;
4207 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4208 for (j = 0; j < nregs; j++)
4209 df_set_regs_ever_live (hard_regno + j, true);
4211 /* Do elimination before the equivalence processing as we can spill
4212 some pseudos during elimination. */
4213 lra_eliminate (false, first_p);
4214 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4215 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4216 if (lra_reg_info[i].nrefs != 0)
4218 ira_reg_equiv[i].profitable_p = true;
4219 reg = regno_reg_rtx[i];
4220 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4222 bool pseudo_p = contains_reg_p (x, false, false);
4224 /* After RTL transformation, we can not guarantee that
4225 pseudo in the substitution was not reloaded which might
4226 make equivalence invalid. For example, in reverse
4227 equiv of p0
4229 p0 <- ...
4231 equiv_mem <- p0
4233 the memory address register was reloaded before the 2nd
4234 insn. */
4235 if ((! first_p && pseudo_p)
4236 /* We don't use DF for compilation speed sake. So it
4237 is problematic to update live info when we use an
4238 equivalence containing pseudos in more than one
4239 BB. */
4240 || (pseudo_p && multi_block_pseudo_p (i))
4241 /* If an init insn was deleted for some reason, cancel
4242 the equiv. We could update the equiv insns after
4243 transformations including an equiv insn deletion
4244 but it is not worthy as such cases are extremely
4245 rare. */
4246 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4247 /* If it is not a reverse equivalence, we check that a
4248 pseudo in rhs of the init insn is not dying in the
4249 insn. Otherwise, the live info at the beginning of
4250 the corresponding BB might be wrong after we
4251 removed the insn. When the equiv can be a
4252 constant, the right hand side of the init insn can
4253 be a pseudo. */
4254 || (! reverse_equiv_p (i)
4255 && (init_insn_rhs_dead_pseudo_p (i)
4256 /* If we reloaded the pseudo in an equivalence
4257 init insn, we can not remove the equiv init
4258 insns and the init insns might write into
4259 const memory in this case. */
4260 || contains_reloaded_insn_p (i)))
4261 /* Prevent access beyond equivalent memory for
4262 paradoxical subregs. */
4263 || (MEM_P (x)
4264 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4265 > GET_MODE_SIZE (GET_MODE (x))))
4266 || (pic_offset_table_rtx
4267 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4268 && (targetm.preferred_reload_class
4269 (x, lra_get_allocno_class (i)) == NO_REGS))
4270 || contains_symbol_ref_p (x))))
4271 ira_reg_equiv[i].defined_p = false;
4272 if (contains_reg_p (x, false, true))
4273 ira_reg_equiv[i].profitable_p = false;
4274 if (get_equiv (reg) != reg)
4275 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4278 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4279 update_equiv (i);
4280 /* We should add all insns containing pseudos which should be
4281 substituted by their equivalences. */
4282 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4283 lra_push_insn_by_uid (uid);
4284 min_len = lra_insn_stack_length ();
4285 new_insns_num = 0;
4286 last_bb = NULL;
4287 changed_p = false;
4288 while ((new_min_len = lra_insn_stack_length ()) != 0)
4290 curr_insn = lra_pop_insn ();
4291 --new_min_len;
4292 curr_bb = BLOCK_FOR_INSN (curr_insn);
4293 if (curr_bb != last_bb)
4295 last_bb = curr_bb;
4296 bb_reload_num = lra_curr_reload_num;
4298 if (min_len > new_min_len)
4300 min_len = new_min_len;
4301 new_insns_num = 0;
4303 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4304 internal_error
4305 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4306 MAX_RELOAD_INSNS_NUMBER);
4307 new_insns_num++;
4308 if (DEBUG_INSN_P (curr_insn))
4310 /* We need to check equivalence in debug insn and change
4311 pseudo to the equivalent value if necessary. */
4312 curr_id = lra_get_insn_recog_data (curr_insn);
4313 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4315 rtx old = *curr_id->operand_loc[0];
4316 *curr_id->operand_loc[0]
4317 = simplify_replace_fn_rtx (old, NULL_RTX,
4318 loc_equivalence_callback, curr_insn);
4319 if (old != *curr_id->operand_loc[0])
4321 lra_update_insn_regno_info (curr_insn);
4322 changed_p = true;
4326 else if (INSN_P (curr_insn))
4328 if ((set = single_set (curr_insn)) != NULL_RTX)
4330 dest_reg = SET_DEST (set);
4331 /* The equivalence pseudo could be set up as SUBREG in a
4332 case when it is a call restore insn in a mode
4333 different from the pseudo mode. */
4334 if (GET_CODE (dest_reg) == SUBREG)
4335 dest_reg = SUBREG_REG (dest_reg);
4336 if ((REG_P (dest_reg)
4337 && (x = get_equiv (dest_reg)) != dest_reg
4338 /* Remove insns which set up a pseudo whose value
4339 can not be changed. Such insns might be not in
4340 init_insns because we don't update equiv data
4341 during insn transformations.
4343 As an example, let suppose that a pseudo got
4344 hard register and on the 1st pass was not
4345 changed to equivalent constant. We generate an
4346 additional insn setting up the pseudo because of
4347 secondary memory movement. Then the pseudo is
4348 spilled and we use the equiv constant. In this
4349 case we should remove the additional insn and
4350 this insn is not init_insns list. */
4351 && (! MEM_P (x) || MEM_READONLY_P (x)
4352 /* Check that this is actually an insn setting
4353 up the equivalence. */
4354 || in_list_p (curr_insn,
4355 ira_reg_equiv
4356 [REGNO (dest_reg)].init_insns)))
4357 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4358 && in_list_p (curr_insn,
4359 ira_reg_equiv
4360 [REGNO (SET_SRC (set))].init_insns)))
4362 /* This is equiv init insn of pseudo which did not get a
4363 hard register -- remove the insn. */
4364 if (lra_dump_file != NULL)
4366 fprintf (lra_dump_file,
4367 " Removing equiv init insn %i (freq=%d)\n",
4368 INSN_UID (curr_insn),
4369 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4370 dump_insn_slim (lra_dump_file, curr_insn);
4372 if (contains_reg_p (x, true, false))
4373 lra_risky_transformations_p = true;
4374 lra_set_insn_deleted (curr_insn);
4375 continue;
4378 curr_id = lra_get_insn_recog_data (curr_insn);
4379 curr_static_id = curr_id->insn_static_data;
4380 init_curr_insn_input_reloads ();
4381 init_curr_operand_mode ();
4382 if (curr_insn_transform (false))
4383 changed_p = true;
4384 /* Check non-transformed insns too for equiv change as USE
4385 or CLOBBER don't need reloads but can contain pseudos
4386 being changed on their equivalences. */
4387 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4388 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4390 lra_update_insn_regno_info (curr_insn);
4391 changed_p = true;
4395 bitmap_clear (&equiv_insn_bitmap);
4396 /* If we used a new hard regno, changed_p should be true because the
4397 hard reg is assigned to a new pseudo. */
4398 #ifdef ENABLE_CHECKING
4399 if (! changed_p)
4401 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4402 if (lra_reg_info[i].nrefs != 0
4403 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4405 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4407 for (j = 0; j < nregs; j++)
4408 lra_assert (df_regs_ever_live_p (hard_regno + j));
4411 #endif
4412 return changed_p;
4415 /* Initiate the LRA constraint pass. It is done once per
4416 function. */
4417 void
4418 lra_constraints_init (void)
4422 /* Finalize the LRA constraint pass. It is done once per
4423 function. */
4424 void
4425 lra_constraints_finish (void)
4431 /* This page contains code to do inheritance/split
4432 transformations. */
4434 /* Number of reloads passed so far in current EBB. */
4435 static int reloads_num;
4437 /* Number of calls passed so far in current EBB. */
4438 static int calls_num;
4440 /* Current reload pseudo check for validity of elements in
4441 USAGE_INSNS. */
4442 static int curr_usage_insns_check;
4444 /* Info about last usage of registers in EBB to do inheritance/split
4445 transformation. Inheritance transformation is done from a spilled
4446 pseudo and split transformations from a hard register or a pseudo
4447 assigned to a hard register. */
4448 struct usage_insns
4450 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4451 value INSNS is valid. The insns is chain of optional debug insns
4452 and a finishing non-debug insn using the corresponding reg. The
4453 value is also used to mark the registers which are set up in the
4454 current insn. The negated insn uid is used for this. */
4455 int check;
4456 /* Value of global reloads_num at the last insn in INSNS. */
4457 int reloads_num;
4458 /* Value of global reloads_nums at the last insn in INSNS. */
4459 int calls_num;
4460 /* It can be true only for splitting. And it means that the restore
4461 insn should be put after insn given by the following member. */
4462 bool after_p;
4463 /* Next insns in the current EBB which use the original reg and the
4464 original reg value is not changed between the current insn and
4465 the next insns. In order words, e.g. for inheritance, if we need
4466 to use the original reg value again in the next insns we can try
4467 to use the value in a hard register from a reload insn of the
4468 current insn. */
4469 rtx insns;
4472 /* Map: regno -> corresponding pseudo usage insns. */
4473 static struct usage_insns *usage_insns;
4475 static void
4476 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4478 usage_insns[regno].check = curr_usage_insns_check;
4479 usage_insns[regno].insns = insn;
4480 usage_insns[regno].reloads_num = reloads_num;
4481 usage_insns[regno].calls_num = calls_num;
4482 usage_insns[regno].after_p = after_p;
4485 /* The function is used to form list REGNO usages which consists of
4486 optional debug insns finished by a non-debug insn using REGNO.
4487 RELOADS_NUM is current number of reload insns processed so far. */
4488 static void
4489 add_next_usage_insn (int regno, rtx insn, int reloads_num)
4491 rtx next_usage_insns;
4493 if (usage_insns[regno].check == curr_usage_insns_check
4494 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4495 && DEBUG_INSN_P (insn))
4497 /* Check that we did not add the debug insn yet. */
4498 if (next_usage_insns != insn
4499 && (GET_CODE (next_usage_insns) != INSN_LIST
4500 || XEXP (next_usage_insns, 0) != insn))
4501 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4502 next_usage_insns);
4504 else if (NONDEBUG_INSN_P (insn))
4505 setup_next_usage_insn (regno, insn, reloads_num, false);
4506 else
4507 usage_insns[regno].check = 0;
4510 /* Return first non-debug insn in list USAGE_INSNS. */
4511 static rtx_insn *
4512 skip_usage_debug_insns (rtx usage_insns)
4514 rtx insn;
4516 /* Skip debug insns. */
4517 for (insn = usage_insns;
4518 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4519 insn = XEXP (insn, 1))
4521 return safe_as_a <rtx_insn *> (insn);
4524 /* Return true if we need secondary memory moves for insn in
4525 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4526 into the insn. */
4527 static bool
4528 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4529 rtx usage_insns ATTRIBUTE_UNUSED)
4531 #ifndef SECONDARY_MEMORY_NEEDED
4532 return false;
4533 #else
4534 rtx_insn *insn;
4535 rtx set, dest;
4536 enum reg_class cl;
4538 if (inher_cl == ALL_REGS
4539 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4540 return false;
4541 lra_assert (INSN_P (insn));
4542 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4543 return false;
4544 dest = SET_DEST (set);
4545 if (! REG_P (dest))
4546 return false;
4547 lra_assert (inher_cl != NO_REGS);
4548 cl = get_reg_class (REGNO (dest));
4549 return (cl != NO_REGS && cl != ALL_REGS
4550 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4551 #endif
4554 /* Registers involved in inheritance/split in the current EBB
4555 (inheritance/split pseudos and original registers). */
4556 static bitmap_head check_only_regs;
4558 /* Do inheritance transformations for insn INSN, which defines (if
4559 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4560 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4561 form as the "insns" field of usage_insns. Return true if we
4562 succeed in such transformation.
4564 The transformations look like:
4566 p <- ... i <- ...
4567 ... p <- i (new insn)
4568 ... =>
4569 <- ... p ... <- ... i ...
4571 ... i <- p (new insn)
4572 <- ... p ... <- ... i ...
4573 ... =>
4574 <- ... p ... <- ... i ...
4575 where p is a spilled original pseudo and i is a new inheritance pseudo.
4578 The inheritance pseudo has the smallest class of two classes CL and
4579 class of ORIGINAL REGNO. */
4580 static bool
4581 inherit_reload_reg (bool def_p, int original_regno,
4582 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4584 if (optimize_function_for_size_p (cfun))
4585 return false;
4587 enum reg_class rclass = lra_get_allocno_class (original_regno);
4588 rtx original_reg = regno_reg_rtx[original_regno];
4589 rtx new_reg, usage_insn;
4590 rtx_insn *new_insns;
4592 lra_assert (! usage_insns[original_regno].after_p);
4593 if (lra_dump_file != NULL)
4594 fprintf (lra_dump_file,
4595 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4596 if (! ira_reg_classes_intersect_p[cl][rclass])
4598 if (lra_dump_file != NULL)
4600 fprintf (lra_dump_file,
4601 " Rejecting inheritance for %d "
4602 "because of disjoint classes %s and %s\n",
4603 original_regno, reg_class_names[cl],
4604 reg_class_names[rclass]);
4605 fprintf (lra_dump_file,
4606 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4608 return false;
4610 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4611 /* We don't use a subset of two classes because it can be
4612 NO_REGS. This transformation is still profitable in most
4613 cases even if the classes are not intersected as register
4614 move is probably cheaper than a memory load. */
4615 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4617 if (lra_dump_file != NULL)
4618 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4619 reg_class_names[cl], reg_class_names[rclass]);
4621 rclass = cl;
4623 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4625 /* Reject inheritance resulting in secondary memory moves.
4626 Otherwise, there is a danger in LRA cycling. Also such
4627 transformation will be unprofitable. */
4628 if (lra_dump_file != NULL)
4630 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4631 rtx set = single_set (insn);
4633 lra_assert (set != NULL_RTX);
4635 rtx dest = SET_DEST (set);
4637 lra_assert (REG_P (dest));
4638 fprintf (lra_dump_file,
4639 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4640 "as secondary mem is needed\n",
4641 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4642 original_regno, reg_class_names[rclass]);
4643 fprintf (lra_dump_file,
4644 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4646 return false;
4648 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4649 rclass, "inheritance");
4650 start_sequence ();
4651 if (def_p)
4652 lra_emit_move (original_reg, new_reg);
4653 else
4654 lra_emit_move (new_reg, original_reg);
4655 new_insns = get_insns ();
4656 end_sequence ();
4657 if (NEXT_INSN (new_insns) != NULL_RTX)
4659 if (lra_dump_file != NULL)
4661 fprintf (lra_dump_file,
4662 " Rejecting inheritance %d->%d "
4663 "as it results in 2 or more insns:\n",
4664 original_regno, REGNO (new_reg));
4665 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4666 fprintf (lra_dump_file,
4667 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4669 return false;
4671 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg);
4672 lra_update_insn_regno_info (insn);
4673 if (! def_p)
4674 /* We now have a new usage insn for original regno. */
4675 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4676 if (lra_dump_file != NULL)
4677 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4678 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4679 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4680 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4681 bitmap_set_bit (&check_only_regs, original_regno);
4682 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4683 if (def_p)
4684 lra_process_new_insns (insn, NULL, new_insns,
4685 "Add original<-inheritance");
4686 else
4687 lra_process_new_insns (insn, new_insns, NULL,
4688 "Add inheritance<-original");
4689 while (next_usage_insns != NULL_RTX)
4691 if (GET_CODE (next_usage_insns) != INSN_LIST)
4693 usage_insn = next_usage_insns;
4694 lra_assert (NONDEBUG_INSN_P (usage_insn));
4695 next_usage_insns = NULL;
4697 else
4699 usage_insn = XEXP (next_usage_insns, 0);
4700 lra_assert (DEBUG_INSN_P (usage_insn));
4701 next_usage_insns = XEXP (next_usage_insns, 1);
4703 lra_substitute_pseudo (&usage_insn, original_regno, new_reg);
4704 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4705 if (lra_dump_file != NULL)
4707 fprintf (lra_dump_file,
4708 " Inheritance reuse change %d->%d (bb%d):\n",
4709 original_regno, REGNO (new_reg),
4710 BLOCK_FOR_INSN (usage_insn)->index);
4711 dump_insn_slim (lra_dump_file, usage_insn);
4714 if (lra_dump_file != NULL)
4715 fprintf (lra_dump_file,
4716 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4717 return true;
4720 /* Return true if we need a caller save/restore for pseudo REGNO which
4721 was assigned to a hard register. */
4722 static inline bool
4723 need_for_call_save_p (int regno)
4725 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4726 return (usage_insns[regno].calls_num < calls_num
4727 && (overlaps_hard_reg_set_p
4728 ((flag_ipa_ra &&
4729 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4730 ? lra_reg_info[regno].actual_call_used_reg_set
4731 : call_used_reg_set,
4732 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4733 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4734 PSEUDO_REGNO_MODE (regno))));
4737 /* Global registers occurring in the current EBB. */
4738 static bitmap_head ebb_global_regs;
4740 /* Return true if we need a split for hard register REGNO or pseudo
4741 REGNO which was assigned to a hard register.
4742 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4743 used for reloads since the EBB end. It is an approximation of the
4744 used hard registers in the split range. The exact value would
4745 require expensive calculations. If we were aggressive with
4746 splitting because of the approximation, the split pseudo will save
4747 the same hard register assignment and will be removed in the undo
4748 pass. We still need the approximation because too aggressive
4749 splitting would result in too inaccurate cost calculation in the
4750 assignment pass because of too many generated moves which will be
4751 probably removed in the undo pass. */
4752 static inline bool
4753 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4755 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4757 lra_assert (hard_regno >= 0);
4758 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4759 /* Don't split eliminable hard registers, otherwise we can
4760 split hard registers like hard frame pointer, which
4761 lives on BB start/end according to DF-infrastructure,
4762 when there is a pseudo assigned to the register and
4763 living in the same BB. */
4764 && (regno >= FIRST_PSEUDO_REGISTER
4765 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4766 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4767 /* Don't split call clobbered hard regs living through
4768 calls, otherwise we might have a check problem in the
4769 assign sub-pass as in the most cases (exception is a
4770 situation when lra_risky_transformations_p value is
4771 true) the assign pass assumes that all pseudos living
4772 through calls are assigned to call saved hard regs. */
4773 && (regno >= FIRST_PSEUDO_REGISTER
4774 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4775 || usage_insns[regno].calls_num == calls_num)
4776 /* We need at least 2 reloads to make pseudo splitting
4777 profitable. We should provide hard regno splitting in
4778 any case to solve 1st insn scheduling problem when
4779 moving hard register definition up might result in
4780 impossibility to find hard register for reload pseudo of
4781 small register class. */
4782 && (usage_insns[regno].reloads_num
4783 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4784 && (regno < FIRST_PSEUDO_REGISTER
4785 /* For short living pseudos, spilling + inheritance can
4786 be considered a substitution for splitting.
4787 Therefore we do not splitting for local pseudos. It
4788 decreases also aggressiveness of splitting. The
4789 minimal number of references is chosen taking into
4790 account that for 2 references splitting has no sense
4791 as we can just spill the pseudo. */
4792 || (regno >= FIRST_PSEUDO_REGISTER
4793 && lra_reg_info[regno].nrefs > 3
4794 && bitmap_bit_p (&ebb_global_regs, regno))))
4795 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4798 /* Return class for the split pseudo created from original pseudo with
4799 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4800 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4801 results in no secondary memory movements. */
4802 static enum reg_class
4803 choose_split_class (enum reg_class allocno_class,
4804 int hard_regno ATTRIBUTE_UNUSED,
4805 machine_mode mode ATTRIBUTE_UNUSED)
4807 #ifndef SECONDARY_MEMORY_NEEDED
4808 return allocno_class;
4809 #else
4810 int i;
4811 enum reg_class cl, best_cl = NO_REGS;
4812 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4813 = REGNO_REG_CLASS (hard_regno);
4815 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4816 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4817 return allocno_class;
4818 for (i = 0;
4819 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4820 i++)
4821 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4822 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4823 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4824 && (best_cl == NO_REGS
4825 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4826 best_cl = cl;
4827 return best_cl;
4828 #endif
4831 /* Do split transformations for insn INSN, which defines or uses
4832 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4833 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4834 "insns" field of usage_insns.
4836 The transformations look like:
4838 p <- ... p <- ...
4839 ... s <- p (new insn -- save)
4840 ... =>
4841 ... p <- s (new insn -- restore)
4842 <- ... p ... <- ... p ...
4844 <- ... p ... <- ... p ...
4845 ... s <- p (new insn -- save)
4846 ... =>
4847 ... p <- s (new insn -- restore)
4848 <- ... p ... <- ... p ...
4850 where p is an original pseudo got a hard register or a hard
4851 register and s is a new split pseudo. The save is put before INSN
4852 if BEFORE_P is true. Return true if we succeed in such
4853 transformation. */
4854 static bool
4855 split_reg (bool before_p, int original_regno, rtx_insn *insn,
4856 rtx next_usage_insns)
4858 enum reg_class rclass;
4859 rtx original_reg;
4860 int hard_regno, nregs;
4861 rtx new_reg, usage_insn;
4862 rtx_insn *restore, *save;
4863 bool after_p;
4864 bool call_save_p;
4866 if (original_regno < FIRST_PSEUDO_REGISTER)
4868 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4869 hard_regno = original_regno;
4870 call_save_p = false;
4871 nregs = 1;
4873 else
4875 hard_regno = reg_renumber[original_regno];
4876 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4877 rclass = lra_get_allocno_class (original_regno);
4878 original_reg = regno_reg_rtx[original_regno];
4879 call_save_p = need_for_call_save_p (original_regno);
4881 original_reg = regno_reg_rtx[original_regno];
4882 lra_assert (hard_regno >= 0);
4883 if (lra_dump_file != NULL)
4884 fprintf (lra_dump_file,
4885 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4886 if (call_save_p)
4888 machine_mode mode = GET_MODE (original_reg);
4890 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4891 hard_regno_nregs[hard_regno][mode],
4892 mode);
4893 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4895 else
4897 rclass = choose_split_class (rclass, hard_regno,
4898 GET_MODE (original_reg));
4899 if (rclass == NO_REGS)
4901 if (lra_dump_file != NULL)
4903 fprintf (lra_dump_file,
4904 " Rejecting split of %d(%s): "
4905 "no good reg class for %d(%s)\n",
4906 original_regno,
4907 reg_class_names[lra_get_allocno_class (original_regno)],
4908 hard_regno,
4909 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4910 fprintf
4911 (lra_dump_file,
4912 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4914 return false;
4916 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4917 rclass, "split");
4918 reg_renumber[REGNO (new_reg)] = hard_regno;
4920 save = emit_spill_move (true, new_reg, original_reg);
4921 if (NEXT_INSN (save) != NULL_RTX)
4923 lra_assert (! call_save_p);
4924 if (lra_dump_file != NULL)
4926 fprintf
4927 (lra_dump_file,
4928 " Rejecting split %d->%d resulting in > 2 %s save insns:\n",
4929 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4930 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
4931 fprintf (lra_dump_file,
4932 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4934 return false;
4936 restore = emit_spill_move (false, new_reg, original_reg);
4937 if (NEXT_INSN (restore) != NULL_RTX)
4939 lra_assert (! call_save_p);
4940 if (lra_dump_file != NULL)
4942 fprintf (lra_dump_file,
4943 " Rejecting split %d->%d "
4944 "resulting in > 2 %s restore insns:\n",
4945 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4946 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
4947 fprintf (lra_dump_file,
4948 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4950 return false;
4952 after_p = usage_insns[original_regno].after_p;
4953 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4954 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4955 bitmap_set_bit (&check_only_regs, original_regno);
4956 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4957 for (;;)
4959 if (GET_CODE (next_usage_insns) != INSN_LIST)
4961 usage_insn = next_usage_insns;
4962 break;
4964 usage_insn = XEXP (next_usage_insns, 0);
4965 lra_assert (DEBUG_INSN_P (usage_insn));
4966 next_usage_insns = XEXP (next_usage_insns, 1);
4967 lra_substitute_pseudo (&usage_insn, original_regno, new_reg);
4968 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4969 if (lra_dump_file != NULL)
4971 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
4972 original_regno, REGNO (new_reg));
4973 dump_insn_slim (lra_dump_file, usage_insn);
4976 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
4977 lra_assert (usage_insn != insn || (after_p && before_p));
4978 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
4979 after_p ? NULL : restore,
4980 after_p ? restore : NULL,
4981 call_save_p
4982 ? "Add reg<-save" : "Add reg<-split");
4983 lra_process_new_insns (insn, before_p ? save : NULL,
4984 before_p ? NULL : save,
4985 call_save_p
4986 ? "Add save<-reg" : "Add split<-reg");
4987 if (nregs > 1)
4988 /* If we are trying to split multi-register. We should check
4989 conflicts on the next assignment sub-pass. IRA can allocate on
4990 sub-register levels, LRA do this on pseudos level right now and
4991 this discrepancy may create allocation conflicts after
4992 splitting. */
4993 lra_risky_transformations_p = true;
4994 if (lra_dump_file != NULL)
4995 fprintf (lra_dump_file,
4996 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4997 return true;
5000 /* Recognize that we need a split transformation for insn INSN, which
5001 defines or uses REGNO in its insn biggest MODE (we use it only if
5002 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5003 hard registers which might be used for reloads since the EBB end.
5004 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5005 uid before starting INSN processing. Return true if we succeed in
5006 such transformation. */
5007 static bool
5008 split_if_necessary (int regno, machine_mode mode,
5009 HARD_REG_SET potential_reload_hard_regs,
5010 bool before_p, rtx_insn *insn, int max_uid)
5012 bool res = false;
5013 int i, nregs = 1;
5014 rtx next_usage_insns;
5016 if (regno < FIRST_PSEUDO_REGISTER)
5017 nregs = hard_regno_nregs[regno][mode];
5018 for (i = 0; i < nregs; i++)
5019 if (usage_insns[regno + i].check == curr_usage_insns_check
5020 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5021 /* To avoid processing the register twice or more. */
5022 && ((GET_CODE (next_usage_insns) != INSN_LIST
5023 && INSN_UID (next_usage_insns) < max_uid)
5024 || (GET_CODE (next_usage_insns) == INSN_LIST
5025 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5026 && need_for_split_p (potential_reload_hard_regs, regno + i)
5027 && split_reg (before_p, regno + i, insn, next_usage_insns))
5028 res = true;
5029 return res;
5032 /* Check only registers living at the current program point in the
5033 current EBB. */
5034 static bitmap_head live_regs;
5036 /* Update live info in EBB given by its HEAD and TAIL insns after
5037 inheritance/split transformation. The function removes dead moves
5038 too. */
5039 static void
5040 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5042 unsigned int j;
5043 int i, regno;
5044 bool live_p;
5045 rtx_insn *prev_insn;
5046 rtx set;
5047 bool remove_p;
5048 basic_block last_bb, prev_bb, curr_bb;
5049 bitmap_iterator bi;
5050 struct lra_insn_reg *reg;
5051 edge e;
5052 edge_iterator ei;
5054 last_bb = BLOCK_FOR_INSN (tail);
5055 prev_bb = NULL;
5056 for (curr_insn = tail;
5057 curr_insn != PREV_INSN (head);
5058 curr_insn = prev_insn)
5060 prev_insn = PREV_INSN (curr_insn);
5061 /* We need to process empty blocks too. They contain
5062 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5063 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5064 continue;
5065 curr_bb = BLOCK_FOR_INSN (curr_insn);
5066 if (curr_bb != prev_bb)
5068 if (prev_bb != NULL)
5070 /* Update df_get_live_in (prev_bb): */
5071 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5072 if (bitmap_bit_p (&live_regs, j))
5073 bitmap_set_bit (df_get_live_in (prev_bb), j);
5074 else
5075 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5077 if (curr_bb != last_bb)
5079 /* Update df_get_live_out (curr_bb): */
5080 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5082 live_p = bitmap_bit_p (&live_regs, j);
5083 if (! live_p)
5084 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5085 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5087 live_p = true;
5088 break;
5090 if (live_p)
5091 bitmap_set_bit (df_get_live_out (curr_bb), j);
5092 else
5093 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5096 prev_bb = curr_bb;
5097 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5099 if (! NONDEBUG_INSN_P (curr_insn))
5100 continue;
5101 curr_id = lra_get_insn_recog_data (curr_insn);
5102 curr_static_id = curr_id->insn_static_data;
5103 remove_p = false;
5104 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
5105 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5106 && bitmap_bit_p (&check_only_regs, regno)
5107 && ! bitmap_bit_p (&live_regs, regno))
5108 remove_p = true;
5109 /* See which defined values die here. */
5110 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5111 if (reg->type == OP_OUT && ! reg->subreg_p)
5112 bitmap_clear_bit (&live_regs, reg->regno);
5113 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5114 if (reg->type == OP_OUT && ! reg->subreg_p)
5115 bitmap_clear_bit (&live_regs, reg->regno);
5116 /* Mark each used value as live. */
5117 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5118 if (reg->type != OP_OUT
5119 && bitmap_bit_p (&check_only_regs, reg->regno))
5120 bitmap_set_bit (&live_regs, reg->regno);
5121 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5122 if (reg->type != OP_OUT
5123 && bitmap_bit_p (&check_only_regs, reg->regno))
5124 bitmap_set_bit (&live_regs, reg->regno);
5125 if (curr_id->arg_hard_regs != NULL)
5126 /* Make argument hard registers live. */
5127 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5128 if (bitmap_bit_p (&check_only_regs, regno))
5129 bitmap_set_bit (&live_regs, regno);
5130 /* It is quite important to remove dead move insns because it
5131 means removing dead store. We don't need to process them for
5132 constraints. */
5133 if (remove_p)
5135 if (lra_dump_file != NULL)
5137 fprintf (lra_dump_file, " Removing dead insn:\n ");
5138 dump_insn_slim (lra_dump_file, curr_insn);
5140 lra_set_insn_deleted (curr_insn);
5145 /* The structure describes info to do an inheritance for the current
5146 insn. We need to collect such info first before doing the
5147 transformations because the transformations change the insn
5148 internal representation. */
5149 struct to_inherit
5151 /* Original regno. */
5152 int regno;
5153 /* Subsequent insns which can inherit original reg value. */
5154 rtx insns;
5157 /* Array containing all info for doing inheritance from the current
5158 insn. */
5159 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5161 /* Number elements in the previous array. */
5162 static int to_inherit_num;
5164 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5165 structure to_inherit. */
5166 static void
5167 add_to_inherit (int regno, rtx insns)
5169 int i;
5171 for (i = 0; i < to_inherit_num; i++)
5172 if (to_inherit[i].regno == regno)
5173 return;
5174 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5175 to_inherit[to_inherit_num].regno = regno;
5176 to_inherit[to_inherit_num++].insns = insns;
5179 /* Return the last non-debug insn in basic block BB, or the block begin
5180 note if none. */
5181 static rtx_insn *
5182 get_last_insertion_point (basic_block bb)
5184 rtx_insn *insn;
5186 FOR_BB_INSNS_REVERSE (bb, insn)
5187 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5188 return insn;
5189 gcc_unreachable ();
5192 /* Set up RES by registers living on edges FROM except the edge (FROM,
5193 TO) or by registers set up in a jump insn in BB FROM. */
5194 static void
5195 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5197 rtx_insn *last;
5198 struct lra_insn_reg *reg;
5199 edge e;
5200 edge_iterator ei;
5202 lra_assert (to != NULL);
5203 bitmap_clear (res);
5204 FOR_EACH_EDGE (e, ei, from->succs)
5205 if (e->dest != to)
5206 bitmap_ior_into (res, df_get_live_in (e->dest));
5207 last = get_last_insertion_point (from);
5208 if (! JUMP_P (last))
5209 return;
5210 curr_id = lra_get_insn_recog_data (last);
5211 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5212 if (reg->type != OP_IN)
5213 bitmap_set_bit (res, reg->regno);
5216 /* Used as a temporary results of some bitmap calculations. */
5217 static bitmap_head temp_bitmap;
5219 /* We split for reloads of small class of hard regs. The following
5220 defines how many hard regs the class should have to be qualified as
5221 small. The code is mostly oriented to x86/x86-64 architecture
5222 where some insns need to use only specific register or pair of
5223 registers and these register can live in RTL explicitly, e.g. for
5224 parameter passing. */
5225 static const int max_small_class_regs_num = 2;
5227 /* Do inheritance/split transformations in EBB starting with HEAD and
5228 finishing on TAIL. We process EBB insns in the reverse order.
5229 Return true if we did any inheritance/split transformation in the
5230 EBB.
5232 We should avoid excessive splitting which results in worse code
5233 because of inaccurate cost calculations for spilling new split
5234 pseudos in such case. To achieve this we do splitting only if
5235 register pressure is high in given basic block and there are reload
5236 pseudos requiring hard registers. We could do more register
5237 pressure calculations at any given program point to avoid necessary
5238 splitting even more but it is to expensive and the current approach
5239 works well enough. */
5240 static bool
5241 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5243 int i, src_regno, dst_regno, nregs;
5244 bool change_p, succ_p, update_reloads_num_p;
5245 rtx_insn *prev_insn, *last_insn;
5246 rtx next_usage_insns, set;
5247 enum reg_class cl;
5248 struct lra_insn_reg *reg;
5249 basic_block last_processed_bb, curr_bb = NULL;
5250 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5251 bitmap to_process;
5252 unsigned int j;
5253 bitmap_iterator bi;
5254 bool head_p, after_p;
5256 change_p = false;
5257 curr_usage_insns_check++;
5258 reloads_num = calls_num = 0;
5259 bitmap_clear (&check_only_regs);
5260 last_processed_bb = NULL;
5261 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5262 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5263 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5264 /* We don't process new insns generated in the loop. */
5265 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5267 prev_insn = PREV_INSN (curr_insn);
5268 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5269 curr_bb = BLOCK_FOR_INSN (curr_insn);
5270 if (last_processed_bb != curr_bb)
5272 /* We are at the end of BB. Add qualified living
5273 pseudos for potential splitting. */
5274 to_process = df_get_live_out (curr_bb);
5275 if (last_processed_bb != NULL)
5277 /* We are somewhere in the middle of EBB. */
5278 get_live_on_other_edges (curr_bb, last_processed_bb,
5279 &temp_bitmap);
5280 to_process = &temp_bitmap;
5282 last_processed_bb = curr_bb;
5283 last_insn = get_last_insertion_point (curr_bb);
5284 after_p = (! JUMP_P (last_insn)
5285 && (! CALL_P (last_insn)
5286 || (find_reg_note (last_insn,
5287 REG_NORETURN, NULL_RTX) == NULL_RTX
5288 && ! SIBLING_CALL_P (last_insn))));
5289 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5290 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5292 if ((int) j >= lra_constraint_new_regno_start)
5293 break;
5294 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5296 if (j < FIRST_PSEUDO_REGISTER)
5297 SET_HARD_REG_BIT (live_hard_regs, j);
5298 else
5299 add_to_hard_reg_set (&live_hard_regs,
5300 PSEUDO_REGNO_MODE (j),
5301 reg_renumber[j]);
5302 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5306 src_regno = dst_regno = -1;
5307 if (NONDEBUG_INSN_P (curr_insn)
5308 && (set = single_set (curr_insn)) != NULL_RTX
5309 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5311 src_regno = REGNO (SET_SRC (set));
5312 dst_regno = REGNO (SET_DEST (set));
5314 update_reloads_num_p = true;
5315 if (src_regno < lra_constraint_new_regno_start
5316 && src_regno >= FIRST_PSEUDO_REGISTER
5317 && reg_renumber[src_regno] < 0
5318 && dst_regno >= lra_constraint_new_regno_start
5319 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5321 /* 'reload_pseudo <- original_pseudo'. */
5322 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5323 reloads_num++;
5324 update_reloads_num_p = false;
5325 succ_p = false;
5326 if (usage_insns[src_regno].check == curr_usage_insns_check
5327 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5328 succ_p = inherit_reload_reg (false, src_regno, cl,
5329 curr_insn, next_usage_insns);
5330 if (succ_p)
5331 change_p = true;
5332 else
5333 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5334 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5335 IOR_HARD_REG_SET (potential_reload_hard_regs,
5336 reg_class_contents[cl]);
5338 else if (src_regno >= lra_constraint_new_regno_start
5339 && dst_regno < lra_constraint_new_regno_start
5340 && dst_regno >= FIRST_PSEUDO_REGISTER
5341 && reg_renumber[dst_regno] < 0
5342 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5343 && usage_insns[dst_regno].check == curr_usage_insns_check
5344 && (next_usage_insns
5345 = usage_insns[dst_regno].insns) != NULL_RTX)
5347 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5348 reloads_num++;
5349 update_reloads_num_p = false;
5350 /* 'original_pseudo <- reload_pseudo'. */
5351 if (! JUMP_P (curr_insn)
5352 && inherit_reload_reg (true, dst_regno, cl,
5353 curr_insn, next_usage_insns))
5354 change_p = true;
5355 /* Invalidate. */
5356 usage_insns[dst_regno].check = 0;
5357 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5358 IOR_HARD_REG_SET (potential_reload_hard_regs,
5359 reg_class_contents[cl]);
5361 else if (INSN_P (curr_insn))
5363 int iter;
5364 int max_uid = get_max_uid ();
5366 curr_id = lra_get_insn_recog_data (curr_insn);
5367 curr_static_id = curr_id->insn_static_data;
5368 to_inherit_num = 0;
5369 /* Process insn definitions. */
5370 for (iter = 0; iter < 2; iter++)
5371 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5372 reg != NULL;
5373 reg = reg->next)
5374 if (reg->type != OP_IN
5375 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5377 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5378 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5379 && usage_insns[dst_regno].check == curr_usage_insns_check
5380 && (next_usage_insns
5381 = usage_insns[dst_regno].insns) != NULL_RTX)
5383 struct lra_insn_reg *r;
5385 for (r = curr_id->regs; r != NULL; r = r->next)
5386 if (r->type != OP_OUT && r->regno == dst_regno)
5387 break;
5388 /* Don't do inheritance if the pseudo is also
5389 used in the insn. */
5390 if (r == NULL)
5391 /* We can not do inheritance right now
5392 because the current insn reg info (chain
5393 regs) can change after that. */
5394 add_to_inherit (dst_regno, next_usage_insns);
5396 /* We can not process one reg twice here because of
5397 usage_insns invalidation. */
5398 if ((dst_regno < FIRST_PSEUDO_REGISTER
5399 || reg_renumber[dst_regno] >= 0)
5400 && ! reg->subreg_p && reg->type != OP_IN)
5402 HARD_REG_SET s;
5404 if (split_if_necessary (dst_regno, reg->biggest_mode,
5405 potential_reload_hard_regs,
5406 false, curr_insn, max_uid))
5407 change_p = true;
5408 CLEAR_HARD_REG_SET (s);
5409 if (dst_regno < FIRST_PSEUDO_REGISTER)
5410 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5411 else
5412 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5413 reg_renumber[dst_regno]);
5414 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5416 /* We should invalidate potential inheritance or
5417 splitting for the current insn usages to the next
5418 usage insns (see code below) as the output pseudo
5419 prevents this. */
5420 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5421 && reg_renumber[dst_regno] < 0)
5422 || (reg->type == OP_OUT && ! reg->subreg_p
5423 && (dst_regno < FIRST_PSEUDO_REGISTER
5424 || reg_renumber[dst_regno] >= 0)))
5426 /* Invalidate and mark definitions. */
5427 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5428 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5429 else
5431 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5432 for (i = 0; i < nregs; i++)
5433 usage_insns[dst_regno + i].check
5434 = -(int) INSN_UID (curr_insn);
5438 if (! JUMP_P (curr_insn))
5439 for (i = 0; i < to_inherit_num; i++)
5440 if (inherit_reload_reg (true, to_inherit[i].regno,
5441 ALL_REGS, curr_insn,
5442 to_inherit[i].insns))
5443 change_p = true;
5444 if (CALL_P (curr_insn))
5446 rtx cheap, pat, dest;
5447 rtx_insn *restore;
5448 int regno, hard_regno;
5450 calls_num++;
5451 if ((cheap = find_reg_note (curr_insn,
5452 REG_RETURNED, NULL_RTX)) != NULL_RTX
5453 && ((cheap = XEXP (cheap, 0)), true)
5454 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5455 && (hard_regno = reg_renumber[regno]) >= 0
5456 /* If there are pending saves/restores, the
5457 optimization is not worth. */
5458 && usage_insns[regno].calls_num == calls_num - 1
5459 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5461 /* Restore the pseudo from the call result as
5462 REG_RETURNED note says that the pseudo value is
5463 in the call result and the pseudo is an argument
5464 of the call. */
5465 pat = PATTERN (curr_insn);
5466 if (GET_CODE (pat) == PARALLEL)
5467 pat = XVECEXP (pat, 0, 0);
5468 dest = SET_DEST (pat);
5469 /* For multiple return values dest is PARALLEL.
5470 Currently we handle only single return value case. */
5471 if (REG_P (dest))
5473 start_sequence ();
5474 emit_move_insn (cheap, copy_rtx (dest));
5475 restore = get_insns ();
5476 end_sequence ();
5477 lra_process_new_insns (curr_insn, NULL, restore,
5478 "Inserting call parameter restore");
5479 /* We don't need to save/restore of the pseudo from
5480 this call. */
5481 usage_insns[regno].calls_num = calls_num;
5482 bitmap_set_bit (&check_only_regs, regno);
5486 to_inherit_num = 0;
5487 /* Process insn usages. */
5488 for (iter = 0; iter < 2; iter++)
5489 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5490 reg != NULL;
5491 reg = reg->next)
5492 if ((reg->type != OP_OUT
5493 || (reg->type == OP_OUT && reg->subreg_p))
5494 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5496 if (src_regno >= FIRST_PSEUDO_REGISTER
5497 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5499 if (usage_insns[src_regno].check == curr_usage_insns_check
5500 && (next_usage_insns
5501 = usage_insns[src_regno].insns) != NULL_RTX
5502 && NONDEBUG_INSN_P (curr_insn))
5503 add_to_inherit (src_regno, next_usage_insns);
5504 else if (usage_insns[src_regno].check
5505 != -(int) INSN_UID (curr_insn))
5506 /* Add usages but only if the reg is not set up
5507 in the same insn. */
5508 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5510 else if (src_regno < FIRST_PSEUDO_REGISTER
5511 || reg_renumber[src_regno] >= 0)
5513 bool before_p;
5514 rtx use_insn = curr_insn;
5516 before_p = (JUMP_P (curr_insn)
5517 || (CALL_P (curr_insn) && reg->type == OP_IN));
5518 if (NONDEBUG_INSN_P (curr_insn)
5519 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5520 && split_if_necessary (src_regno, reg->biggest_mode,
5521 potential_reload_hard_regs,
5522 before_p, curr_insn, max_uid))
5524 if (reg->subreg_p)
5525 lra_risky_transformations_p = true;
5526 change_p = true;
5527 /* Invalidate. */
5528 usage_insns[src_regno].check = 0;
5529 if (before_p)
5530 use_insn = PREV_INSN (curr_insn);
5532 if (NONDEBUG_INSN_P (curr_insn))
5534 if (src_regno < FIRST_PSEUDO_REGISTER)
5535 add_to_hard_reg_set (&live_hard_regs,
5536 reg->biggest_mode, src_regno);
5537 else
5538 add_to_hard_reg_set (&live_hard_regs,
5539 PSEUDO_REGNO_MODE (src_regno),
5540 reg_renumber[src_regno]);
5542 add_next_usage_insn (src_regno, use_insn, reloads_num);
5545 /* Process call args. */
5546 if (curr_id->arg_hard_regs != NULL)
5547 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5548 if (src_regno < FIRST_PSEUDO_REGISTER)
5550 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5551 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5553 for (i = 0; i < to_inherit_num; i++)
5555 src_regno = to_inherit[i].regno;
5556 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5557 curr_insn, to_inherit[i].insns))
5558 change_p = true;
5559 else
5560 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5563 if (update_reloads_num_p
5564 && NONDEBUG_INSN_P (curr_insn)
5565 && (set = single_set (curr_insn)) != NULL_RTX)
5567 int regno = -1;
5568 if ((REG_P (SET_DEST (set))
5569 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5570 && reg_renumber[regno] < 0
5571 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5572 || (REG_P (SET_SRC (set))
5573 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5574 && reg_renumber[regno] < 0
5575 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5577 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5578 reloads_num++;
5579 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5580 IOR_HARD_REG_SET (potential_reload_hard_regs,
5581 reg_class_contents[cl]);
5584 /* We reached the start of the current basic block. */
5585 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5586 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5588 /* We reached the beginning of the current block -- do
5589 rest of spliting in the current BB. */
5590 to_process = df_get_live_in (curr_bb);
5591 if (BLOCK_FOR_INSN (head) != curr_bb)
5593 /* We are somewhere in the middle of EBB. */
5594 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5595 curr_bb, &temp_bitmap);
5596 to_process = &temp_bitmap;
5598 head_p = true;
5599 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5601 if ((int) j >= lra_constraint_new_regno_start)
5602 break;
5603 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5604 && usage_insns[j].check == curr_usage_insns_check
5605 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5607 if (need_for_split_p (potential_reload_hard_regs, j))
5609 if (lra_dump_file != NULL && head_p)
5611 fprintf (lra_dump_file,
5612 " ----------------------------------\n");
5613 head_p = false;
5615 if (split_reg (false, j, bb_note (curr_bb),
5616 next_usage_insns))
5617 change_p = true;
5619 usage_insns[j].check = 0;
5624 return change_p;
5627 /* This value affects EBB forming. If probability of edge from EBB to
5628 a BB is not greater than the following value, we don't add the BB
5629 to EBB. */
5630 #define EBB_PROBABILITY_CUTOFF ((REG_BR_PROB_BASE * 50) / 100)
5632 /* Current number of inheritance/split iteration. */
5633 int lra_inheritance_iter;
5635 /* Entry function for inheritance/split pass. */
5636 void
5637 lra_inheritance (void)
5639 int i;
5640 basic_block bb, start_bb;
5641 edge e;
5643 lra_inheritance_iter++;
5644 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5645 return;
5646 timevar_push (TV_LRA_INHERITANCE);
5647 if (lra_dump_file != NULL)
5648 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5649 lra_inheritance_iter);
5650 curr_usage_insns_check = 0;
5651 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5652 for (i = 0; i < lra_constraint_new_regno_start; i++)
5653 usage_insns[i].check = 0;
5654 bitmap_initialize (&check_only_regs, &reg_obstack);
5655 bitmap_initialize (&live_regs, &reg_obstack);
5656 bitmap_initialize (&temp_bitmap, &reg_obstack);
5657 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5658 FOR_EACH_BB_FN (bb, cfun)
5660 start_bb = bb;
5661 if (lra_dump_file != NULL)
5662 fprintf (lra_dump_file, "EBB");
5663 /* Form a EBB starting with BB. */
5664 bitmap_clear (&ebb_global_regs);
5665 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5666 for (;;)
5668 if (lra_dump_file != NULL)
5669 fprintf (lra_dump_file, " %d", bb->index);
5670 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5671 || LABEL_P (BB_HEAD (bb->next_bb)))
5672 break;
5673 e = find_fallthru_edge (bb->succs);
5674 if (! e)
5675 break;
5676 if (e->probability <= EBB_PROBABILITY_CUTOFF)
5677 break;
5678 bb = bb->next_bb;
5680 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5681 if (lra_dump_file != NULL)
5682 fprintf (lra_dump_file, "\n");
5683 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5684 /* Remember that the EBB head and tail can change in
5685 inherit_in_ebb. */
5686 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5688 bitmap_clear (&ebb_global_regs);
5689 bitmap_clear (&temp_bitmap);
5690 bitmap_clear (&live_regs);
5691 bitmap_clear (&check_only_regs);
5692 free (usage_insns);
5694 timevar_pop (TV_LRA_INHERITANCE);
5699 /* This page contains code to undo failed inheritance/split
5700 transformations. */
5702 /* Current number of iteration undoing inheritance/split. */
5703 int lra_undo_inheritance_iter;
5705 /* Fix BB live info LIVE after removing pseudos created on pass doing
5706 inheritance/split which are REMOVED_PSEUDOS. */
5707 static void
5708 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5710 unsigned int regno;
5711 bitmap_iterator bi;
5713 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5714 if (bitmap_clear_bit (live, regno))
5715 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5718 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5719 number. */
5720 static int
5721 get_regno (rtx reg)
5723 if (GET_CODE (reg) == SUBREG)
5724 reg = SUBREG_REG (reg);
5725 if (REG_P (reg))
5726 return REGNO (reg);
5727 return -1;
5730 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5731 return true if we did any change. The undo transformations for
5732 inheritance looks like
5733 i <- i2
5734 p <- i => p <- i2
5735 or removing
5736 p <- i, i <- p, and i <- i3
5737 where p is original pseudo from which inheritance pseudo i was
5738 created, i and i3 are removed inheritance pseudos, i2 is another
5739 not removed inheritance pseudo. All split pseudos or other
5740 occurrences of removed inheritance pseudos are changed on the
5741 corresponding original pseudos.
5743 The function also schedules insns changed and created during
5744 inheritance/split pass for processing by the subsequent constraint
5745 pass. */
5746 static bool
5747 remove_inheritance_pseudos (bitmap remove_pseudos)
5749 basic_block bb;
5750 int regno, sregno, prev_sregno, dregno, restore_regno;
5751 rtx set, prev_set;
5752 rtx_insn *prev_insn;
5753 bool change_p, done_p;
5755 change_p = ! bitmap_empty_p (remove_pseudos);
5756 /* We can not finish the function right away if CHANGE_P is true
5757 because we need to marks insns affected by previous
5758 inheritance/split pass for processing by the subsequent
5759 constraint pass. */
5760 FOR_EACH_BB_FN (bb, cfun)
5762 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5763 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5764 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5766 if (! INSN_P (curr_insn))
5767 continue;
5768 done_p = false;
5769 sregno = dregno = -1;
5770 if (change_p && NONDEBUG_INSN_P (curr_insn)
5771 && (set = single_set (curr_insn)) != NULL_RTX)
5773 dregno = get_regno (SET_DEST (set));
5774 sregno = get_regno (SET_SRC (set));
5777 if (sregno >= 0 && dregno >= 0)
5779 if ((bitmap_bit_p (remove_pseudos, sregno)
5780 && (lra_reg_info[sregno].restore_regno == dregno
5781 || (bitmap_bit_p (remove_pseudos, dregno)
5782 && (lra_reg_info[sregno].restore_regno
5783 == lra_reg_info[dregno].restore_regno))))
5784 || (bitmap_bit_p (remove_pseudos, dregno)
5785 && lra_reg_info[dregno].restore_regno == sregno))
5786 /* One of the following cases:
5787 original <- removed inheritance pseudo
5788 removed inherit pseudo <- another removed inherit pseudo
5789 removed inherit pseudo <- original pseudo
5791 removed_split_pseudo <- original_reg
5792 original_reg <- removed_split_pseudo */
5794 if (lra_dump_file != NULL)
5796 fprintf (lra_dump_file, " Removing %s:\n",
5797 bitmap_bit_p (&lra_split_regs, sregno)
5798 || bitmap_bit_p (&lra_split_regs, dregno)
5799 ? "split" : "inheritance");
5800 dump_insn_slim (lra_dump_file, curr_insn);
5802 lra_set_insn_deleted (curr_insn);
5803 done_p = true;
5805 else if (bitmap_bit_p (remove_pseudos, sregno)
5806 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5808 /* Search the following pattern:
5809 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5810 original_pseudo <- inherit_or_split_pseudo1
5811 where the 2nd insn is the current insn and
5812 inherit_or_split_pseudo2 is not removed. If it is found,
5813 change the current insn onto:
5814 original_pseudo <- inherit_or_split_pseudo2. */
5815 for (prev_insn = PREV_INSN (curr_insn);
5816 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5817 prev_insn = PREV_INSN (prev_insn))
5819 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5820 && (prev_set = single_set (prev_insn)) != NULL_RTX
5821 /* There should be no subregs in insn we are
5822 searching because only the original reg might
5823 be in subreg when we changed the mode of
5824 load/store for splitting. */
5825 && REG_P (SET_DEST (prev_set))
5826 && REG_P (SET_SRC (prev_set))
5827 && (int) REGNO (SET_DEST (prev_set)) == sregno
5828 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5829 >= FIRST_PSEUDO_REGISTER)
5830 /* As we consider chain of inheritance or
5831 splitting described in above comment we should
5832 check that sregno and prev_sregno were
5833 inheritance/split pseudos created from the
5834 same original regno. */
5835 && (lra_reg_info[sregno].restore_regno
5836 == lra_reg_info[prev_sregno].restore_regno)
5837 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5839 lra_assert (GET_MODE (SET_SRC (prev_set))
5840 == GET_MODE (regno_reg_rtx[sregno]));
5841 if (GET_CODE (SET_SRC (set)) == SUBREG)
5842 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5843 else
5844 SET_SRC (set) = SET_SRC (prev_set);
5845 /* As we are finishing with processing the insn
5846 here, check the destination too as it might
5847 inheritance pseudo for another pseudo. */
5848 if (bitmap_bit_p (remove_pseudos, dregno)
5849 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
5850 && (restore_regno
5851 = lra_reg_info[dregno].restore_regno) >= 0)
5853 if (GET_CODE (SET_DEST (set)) == SUBREG)
5854 SUBREG_REG (SET_DEST (set))
5855 = regno_reg_rtx[restore_regno];
5856 else
5857 SET_DEST (set) = regno_reg_rtx[restore_regno];
5859 lra_push_insn_and_update_insn_regno_info (curr_insn);
5860 lra_set_used_insn_alternative_by_uid
5861 (INSN_UID (curr_insn), -1);
5862 done_p = true;
5863 if (lra_dump_file != NULL)
5865 fprintf (lra_dump_file, " Change reload insn:\n");
5866 dump_insn_slim (lra_dump_file, curr_insn);
5871 if (! done_p)
5873 struct lra_insn_reg *reg;
5874 bool restored_regs_p = false;
5875 bool kept_regs_p = false;
5877 curr_id = lra_get_insn_recog_data (curr_insn);
5878 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5880 regno = reg->regno;
5881 restore_regno = lra_reg_info[regno].restore_regno;
5882 if (restore_regno >= 0)
5884 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5886 lra_substitute_pseudo_within_insn (
5887 curr_insn, regno, regno_reg_rtx[restore_regno]);
5888 restored_regs_p = true;
5890 else
5891 kept_regs_p = true;
5894 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5896 /* The instruction has changed since the previous
5897 constraints pass. */
5898 lra_push_insn_and_update_insn_regno_info (curr_insn);
5899 lra_set_used_insn_alternative_by_uid
5900 (INSN_UID (curr_insn), -1);
5902 else if (restored_regs_p)
5903 /* The instruction has been restored to the form that
5904 it had during the previous constraints pass. */
5905 lra_update_insn_regno_info (curr_insn);
5906 if (restored_regs_p && lra_dump_file != NULL)
5908 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5909 dump_insn_slim (lra_dump_file, curr_insn);
5914 return change_p;
5917 /* If optional reload pseudos failed to get a hard register or was not
5918 inherited, it is better to remove optional reloads. We do this
5919 transformation after undoing inheritance to figure out necessity to
5920 remove optional reloads easier. Return true if we do any
5921 change. */
5922 static bool
5923 undo_optional_reloads (void)
5925 bool change_p, keep_p;
5926 unsigned int regno, uid;
5927 bitmap_iterator bi, bi2;
5928 rtx_insn *insn;
5929 rtx set, src, dest;
5930 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5932 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5933 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5934 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5936 keep_p = false;
5937 /* Keep optional reloads from previous subpasses. */
5938 if (lra_reg_info[regno].restore_regno < 0
5939 /* If the original pseudo changed its allocation, just
5940 removing the optional pseudo is dangerous as the original
5941 pseudo will have longer live range. */
5942 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
5943 keep_p = true;
5944 else if (reg_renumber[regno] >= 0)
5945 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
5947 insn = lra_insn_recog_data[uid]->insn;
5948 if ((set = single_set (insn)) == NULL_RTX)
5949 continue;
5950 src = SET_SRC (set);
5951 dest = SET_DEST (set);
5952 if (! REG_P (src) || ! REG_P (dest))
5953 continue;
5954 if (REGNO (dest) == regno
5955 /* Ignore insn for optional reloads itself. */
5956 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
5957 /* Check only inheritance on last inheritance pass. */
5958 && (int) REGNO (src) >= new_regno_start
5959 /* Check that the optional reload was inherited. */
5960 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
5962 keep_p = true;
5963 break;
5966 if (keep_p)
5968 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
5969 if (lra_dump_file != NULL)
5970 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
5973 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
5974 bitmap_initialize (&insn_bitmap, &reg_obstack);
5975 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
5977 if (lra_dump_file != NULL)
5978 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
5979 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
5980 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
5982 insn = lra_insn_recog_data[uid]->insn;
5983 if ((set = single_set (insn)) != NULL_RTX)
5985 src = SET_SRC (set);
5986 dest = SET_DEST (set);
5987 if (REG_P (src) && REG_P (dest)
5988 && ((REGNO (src) == regno
5989 && (lra_reg_info[regno].restore_regno
5990 == (int) REGNO (dest)))
5991 || (REGNO (dest) == regno
5992 && (lra_reg_info[regno].restore_regno
5993 == (int) REGNO (src)))))
5995 if (lra_dump_file != NULL)
5997 fprintf (lra_dump_file, " Deleting move %u\n",
5998 INSN_UID (insn));
5999 dump_insn_slim (lra_dump_file, insn);
6001 lra_set_insn_deleted (insn);
6002 continue;
6004 /* We should not worry about generation memory-memory
6005 moves here as if the corresponding inheritance did
6006 not work (inheritance pseudo did not get a hard reg),
6007 we remove the inheritance pseudo and the optional
6008 reload. */
6010 lra_substitute_pseudo_within_insn (
6011 insn, regno,
6012 regno_reg_rtx[lra_reg_info[regno].restore_regno]);
6013 lra_update_insn_regno_info (insn);
6014 if (lra_dump_file != NULL)
6016 fprintf (lra_dump_file,
6017 " Restoring original insn:\n");
6018 dump_insn_slim (lra_dump_file, insn);
6022 /* Clear restore_regnos. */
6023 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6024 lra_reg_info[regno].restore_regno = -1;
6025 bitmap_clear (&insn_bitmap);
6026 bitmap_clear (&removed_optional_reload_pseudos);
6027 return change_p;
6030 /* Entry function for undoing inheritance/split transformation. Return true
6031 if we did any RTL change in this pass. */
6032 bool
6033 lra_undo_inheritance (void)
6035 unsigned int regno;
6036 int restore_regno, hard_regno;
6037 int n_all_inherit, n_inherit, n_all_split, n_split;
6038 bitmap_head remove_pseudos;
6039 bitmap_iterator bi;
6040 bool change_p;
6042 lra_undo_inheritance_iter++;
6043 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6044 return false;
6045 if (lra_dump_file != NULL)
6046 fprintf (lra_dump_file,
6047 "\n********** Undoing inheritance #%d: **********\n\n",
6048 lra_undo_inheritance_iter);
6049 bitmap_initialize (&remove_pseudos, &reg_obstack);
6050 n_inherit = n_all_inherit = 0;
6051 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6052 if (lra_reg_info[regno].restore_regno >= 0)
6054 n_all_inherit++;
6055 if (reg_renumber[regno] < 0
6056 /* If the original pseudo changed its allocation, just
6057 removing inheritance is dangerous as for changing
6058 allocation we used shorter live-ranges. */
6059 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6060 bitmap_set_bit (&remove_pseudos, regno);
6061 else
6062 n_inherit++;
6064 if (lra_dump_file != NULL && n_all_inherit != 0)
6065 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6066 n_inherit, n_all_inherit,
6067 (double) n_inherit / n_all_inherit * 100);
6068 n_split = n_all_split = 0;
6069 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6070 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6072 n_all_split++;
6073 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6074 ? reg_renumber[restore_regno] : restore_regno);
6075 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6076 bitmap_set_bit (&remove_pseudos, regno);
6077 else
6079 n_split++;
6080 if (lra_dump_file != NULL)
6081 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6082 regno, restore_regno);
6085 if (lra_dump_file != NULL && n_all_split != 0)
6086 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6087 n_split, n_all_split,
6088 (double) n_split / n_all_split * 100);
6089 change_p = remove_inheritance_pseudos (&remove_pseudos);
6090 bitmap_clear (&remove_pseudos);
6091 /* Clear restore_regnos. */
6092 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6093 lra_reg_info[regno].restore_regno = -1;
6094 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6095 lra_reg_info[regno].restore_regno = -1;
6096 change_p = undo_optional_reloads () || change_p;
6097 return change_p;