* lra-constraints.c (process_address_1): Swap base_term and index_term
[official-gcc.git] / gcc / lra-constraints.c
blob8fc2cb77a5d913d015e67b470bc602bfe99cdec5
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)
491 return res;
492 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
493 return res;
494 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
495 return res;
496 gcc_unreachable ();
499 /* If we have decided to substitute X with the equivalent value,
500 return that value after elimination for INSN, otherwise return
501 X. */
502 static rtx
503 get_equiv_with_elimination (rtx x, rtx_insn *insn)
505 rtx res = get_equiv (x);
507 if (x == res || CONSTANT_P (res))
508 return res;
509 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
510 0, false, false, true);
513 /* Set up curr_operand_mode. */
514 static void
515 init_curr_operand_mode (void)
517 int nop = curr_static_id->n_operands;
518 for (int i = 0; i < nop; i++)
520 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
521 if (mode == VOIDmode)
523 /* The .md mode for address operands is the mode of the
524 addressed value rather than the mode of the address itself. */
525 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
526 mode = Pmode;
527 else
528 mode = curr_static_id->operand[i].mode;
530 curr_operand_mode[i] = mode;
536 /* The page contains code to reuse input reloads. */
538 /* Structure describes input reload of the current insns. */
539 struct input_reload
541 /* Reloaded value. */
542 rtx input;
543 /* Reload pseudo used. */
544 rtx reg;
547 /* The number of elements in the following array. */
548 static int curr_insn_input_reloads_num;
549 /* Array containing info about input reloads. It is used to find the
550 same input reload and reuse the reload pseudo in this case. */
551 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
553 /* Initiate data concerning reuse of input reloads for the current
554 insn. */
555 static void
556 init_curr_insn_input_reloads (void)
558 curr_insn_input_reloads_num = 0;
561 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
562 created input reload pseudo (only if TYPE is not OP_OUT). Don't
563 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
564 wrapped up in SUBREG. The result pseudo is returned through
565 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
566 reused the already created input reload pseudo. Use TITLE to
567 describe new registers for debug purposes. */
568 static bool
569 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
570 enum reg_class rclass, bool in_subreg_p,
571 const char *title, rtx *result_reg)
573 int i, regno;
574 enum reg_class new_class;
576 if (type == OP_OUT)
578 *result_reg
579 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
580 return true;
582 /* Prevent reuse value of expression with side effects,
583 e.g. volatile memory. */
584 if (! side_effects_p (original))
585 for (i = 0; i < curr_insn_input_reloads_num; i++)
586 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
587 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
589 rtx reg = curr_insn_input_reloads[i].reg;
590 regno = REGNO (reg);
591 /* If input is equal to original and both are VOIDmode,
592 GET_MODE (reg) might be still different from mode.
593 Ensure we don't return *result_reg with wrong mode. */
594 if (GET_MODE (reg) != mode)
596 if (in_subreg_p)
597 continue;
598 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
599 continue;
600 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
601 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
602 continue;
604 *result_reg = reg;
605 if (lra_dump_file != NULL)
607 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
608 dump_value_slim (lra_dump_file, original, 1);
610 if (new_class != lra_get_allocno_class (regno))
611 lra_change_class (regno, new_class, ", change to", false);
612 if (lra_dump_file != NULL)
613 fprintf (lra_dump_file, "\n");
614 return false;
616 *result_reg = lra_create_new_reg (mode, original, rclass, title);
617 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
618 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
619 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
620 return true;
625 /* The page contains code to extract memory address parts. */
627 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
628 static inline bool
629 ok_for_index_p_nonstrict (rtx reg)
631 unsigned regno = REGNO (reg);
633 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
636 /* A version of regno_ok_for_base_p for use here, when all pseudos
637 should count as OK. Arguments as for regno_ok_for_base_p. */
638 static inline bool
639 ok_for_base_p_nonstrict (rtx reg, machine_mode mode, addr_space_t as,
640 enum rtx_code outer_code, enum rtx_code index_code)
642 unsigned regno = REGNO (reg);
644 if (regno >= FIRST_PSEUDO_REGISTER)
645 return true;
646 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
651 /* The page contains major code to choose the current insn alternative
652 and generate reloads for it. */
654 /* Return the offset from REGNO of the least significant register
655 in (reg:MODE REGNO).
657 This function is used to tell whether two registers satisfy
658 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
660 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
661 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
663 lra_constraint_offset (int regno, machine_mode mode)
665 lra_assert (regno < FIRST_PSEUDO_REGISTER);
666 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
667 && SCALAR_INT_MODE_P (mode))
668 return hard_regno_nregs[regno][mode] - 1;
669 return 0;
672 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
673 if they are the same hard reg, and has special hacks for
674 auto-increment and auto-decrement. This is specifically intended for
675 process_alt_operands to use in determining whether two operands
676 match. X is the operand whose number is the lower of the two.
678 It is supposed that X is the output operand and Y is the input
679 operand. Y_HARD_REGNO is the final hard regno of register Y or
680 register in subreg Y as we know it now. Otherwise, it is a
681 negative value. */
682 static bool
683 operands_match_p (rtx x, rtx y, int y_hard_regno)
685 int i;
686 RTX_CODE code = GET_CODE (x);
687 const char *fmt;
689 if (x == y)
690 return true;
691 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
692 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
694 int j;
696 i = get_hard_regno (x);
697 if (i < 0)
698 goto slow;
700 if ((j = y_hard_regno) < 0)
701 goto slow;
703 i += lra_constraint_offset (i, GET_MODE (x));
704 j += lra_constraint_offset (j, GET_MODE (y));
706 return i == j;
709 /* If two operands must match, because they are really a single
710 operand of an assembler insn, then two post-increments are invalid
711 because the assembler insn would increment only once. On the
712 other hand, a post-increment matches ordinary indexing if the
713 post-increment is the output operand. */
714 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
715 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
717 /* Two pre-increments are invalid because the assembler insn would
718 increment only once. On the other hand, a pre-increment matches
719 ordinary indexing if the pre-increment is the input operand. */
720 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
721 || GET_CODE (y) == PRE_MODIFY)
722 return operands_match_p (x, XEXP (y, 0), -1);
724 slow:
726 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
727 && x == SUBREG_REG (y))
728 return true;
729 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
730 && SUBREG_REG (x) == y)
731 return true;
733 /* Now we have disposed of all the cases in which different rtx
734 codes can match. */
735 if (code != GET_CODE (y))
736 return false;
738 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
739 if (GET_MODE (x) != GET_MODE (y))
740 return false;
742 switch (code)
744 CASE_CONST_UNIQUE:
745 return false;
747 case LABEL_REF:
748 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
749 case SYMBOL_REF:
750 return XSTR (x, 0) == XSTR (y, 0);
752 default:
753 break;
756 /* Compare the elements. If any pair of corresponding elements fail
757 to match, return false for the whole things. */
759 fmt = GET_RTX_FORMAT (code);
760 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
762 int val, j;
763 switch (fmt[i])
765 case 'w':
766 if (XWINT (x, i) != XWINT (y, i))
767 return false;
768 break;
770 case 'i':
771 if (XINT (x, i) != XINT (y, i))
772 return false;
773 break;
775 case 'e':
776 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
777 if (val == 0)
778 return false;
779 break;
781 case '0':
782 break;
784 case 'E':
785 if (XVECLEN (x, i) != XVECLEN (y, i))
786 return false;
787 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
789 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
790 if (val == 0)
791 return false;
793 break;
795 /* It is believed that rtx's at this level will never
796 contain anything but integers and other rtx's, except for
797 within LABEL_REFs and SYMBOL_REFs. */
798 default:
799 gcc_unreachable ();
802 return true;
805 /* True if X is a constant that can be forced into the constant pool.
806 MODE is the mode of the operand, or VOIDmode if not known. */
807 #define CONST_POOL_OK_P(MODE, X) \
808 ((MODE) != VOIDmode \
809 && CONSTANT_P (X) \
810 && GET_CODE (X) != HIGH \
811 && !targetm.cannot_force_const_mem (MODE, X))
813 /* True if C is a non-empty register class that has too few registers
814 to be safely used as a reload target class. */
815 #define SMALL_REGISTER_CLASS_P(C) \
816 (ira_class_hard_regs_num [(C)] == 1 \
817 || (ira_class_hard_regs_num [(C)] >= 1 \
818 && targetm.class_likely_spilled_p (C)))
820 /* If REG is a reload pseudo, try to make its class satisfying CL. */
821 static void
822 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
824 enum reg_class rclass;
826 /* Do not make more accurate class from reloads generated. They are
827 mostly moves with a lot of constraints. Making more accurate
828 class may results in very narrow class and impossibility of find
829 registers for several reloads of one insn. */
830 if (INSN_UID (curr_insn) >= new_insn_uid_start)
831 return;
832 if (GET_CODE (reg) == SUBREG)
833 reg = SUBREG_REG (reg);
834 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
835 return;
836 if (in_class_p (reg, cl, &rclass) && rclass != cl)
837 lra_change_class (REGNO (reg), rclass, " Change to", true);
840 /* Generate reloads for matching OUT and INS (array of input operand
841 numbers with end marker -1) with reg class GOAL_CLASS. Add input
842 and output reloads correspondingly to the lists *BEFORE and *AFTER.
843 OUT might be negative. In this case we generate input reloads for
844 matched input operands INS. */
845 static void
846 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
847 rtx_insn **before, rtx_insn **after)
849 int i, in;
850 rtx new_in_reg, new_out_reg, reg, clobber;
851 machine_mode inmode, outmode;
852 rtx in_rtx = *curr_id->operand_loc[ins[0]];
853 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
855 inmode = curr_operand_mode[ins[0]];
856 outmode = out < 0 ? inmode : curr_operand_mode[out];
857 push_to_sequence (*before);
858 if (inmode != outmode)
860 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
862 reg = new_in_reg
863 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
864 goal_class, "");
865 if (SCALAR_INT_MODE_P (inmode))
866 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
867 else
868 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
869 LRA_SUBREG_P (new_out_reg) = 1;
870 /* If the input reg is dying here, we can use the same hard
871 register for REG and IN_RTX. We do it only for original
872 pseudos as reload pseudos can die although original
873 pseudos still live where reload pseudos dies. */
874 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
875 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
876 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
878 else
880 reg = new_out_reg
881 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
882 goal_class, "");
883 if (SCALAR_INT_MODE_P (outmode))
884 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
885 else
886 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
887 /* NEW_IN_REG is non-paradoxical subreg. We don't want
888 NEW_OUT_REG living above. We add clobber clause for
889 this. This is just a temporary clobber. We can remove
890 it at the end of LRA work. */
891 clobber = emit_clobber (new_out_reg);
892 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
893 LRA_SUBREG_P (new_in_reg) = 1;
894 if (GET_CODE (in_rtx) == SUBREG)
896 rtx subreg_reg = SUBREG_REG (in_rtx);
898 /* If SUBREG_REG is dying here and sub-registers IN_RTX
899 and NEW_IN_REG are similar, we can use the same hard
900 register for REG and SUBREG_REG. */
901 if (REG_P (subreg_reg)
902 && (int) REGNO (subreg_reg) < lra_new_regno_start
903 && GET_MODE (subreg_reg) == outmode
904 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
905 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
906 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
910 else
912 /* Pseudos have values -- see comments for lra_reg_info.
913 Different pseudos with the same value do not conflict even if
914 they live in the same place. When we create a pseudo we
915 assign value of original pseudo (if any) from which we
916 created the new pseudo. If we create the pseudo from the
917 input pseudo, the new pseudo will no conflict with the input
918 pseudo which is wrong when the input pseudo lives after the
919 insn and as the new pseudo value is changed by the insn
920 output. Therefore we create the new pseudo from the output.
922 We cannot reuse the current output register because we might
923 have a situation like "a <- a op b", where the constraints
924 force the second input operand ("b") to match the output
925 operand ("a"). "b" must then be copied into a new register
926 so that it doesn't clobber the current value of "a". */
928 new_in_reg = new_out_reg
929 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
930 goal_class, "");
932 /* In operand can be got from transformations before processing insn
933 constraints. One example of such transformations is subreg
934 reloading (see function simplify_operand_subreg). The new
935 pseudos created by the transformations might have inaccurate
936 class (ALL_REGS) and we should make their classes more
937 accurate. */
938 narrow_reload_pseudo_class (in_rtx, goal_class);
939 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
940 *before = get_insns ();
941 end_sequence ();
942 for (i = 0; (in = ins[i]) >= 0; i++)
944 lra_assert
945 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
946 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
947 *curr_id->operand_loc[in] = new_in_reg;
949 lra_update_dups (curr_id, ins);
950 if (out < 0)
951 return;
952 /* See a comment for the input operand above. */
953 narrow_reload_pseudo_class (out_rtx, goal_class);
954 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
956 start_sequence ();
957 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
958 emit_insn (*after);
959 *after = get_insns ();
960 end_sequence ();
962 *curr_id->operand_loc[out] = new_out_reg;
963 lra_update_dup (curr_id, out);
966 /* Return register class which is union of all reg classes in insn
967 constraint alternative string starting with P. */
968 static enum reg_class
969 reg_class_from_constraints (const char *p)
971 int c, len;
972 enum reg_class op_class = NO_REGS;
975 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
977 case '#':
978 case ',':
979 return op_class;
981 case 'g':
982 op_class = reg_class_subunion[op_class][GENERAL_REGS];
983 break;
985 default:
986 enum constraint_num cn = lookup_constraint (p);
987 enum reg_class cl = reg_class_for_constraint (cn);
988 if (cl == NO_REGS)
990 if (insn_extra_address_constraint (cn))
991 op_class
992 = (reg_class_subunion
993 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
994 ADDRESS, SCRATCH)]);
995 break;
998 op_class = reg_class_subunion[op_class][cl];
999 break;
1001 while ((p += len), c);
1002 return op_class;
1005 /* If OP is a register, return the class of the register as per
1006 get_reg_class, otherwise return NO_REGS. */
1007 static inline enum reg_class
1008 get_op_class (rtx op)
1010 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1013 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1014 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1015 SUBREG for VAL to make them equal. */
1016 static rtx_insn *
1017 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1019 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1021 /* Usually size of mem_pseudo is greater than val size but in
1022 rare cases it can be less as it can be defined by target
1023 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1024 if (! MEM_P (val))
1026 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1027 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1029 LRA_SUBREG_P (val) = 1;
1031 else
1033 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1034 LRA_SUBREG_P (mem_pseudo) = 1;
1037 return as_a <rtx_insn *> (to_p
1038 ? gen_move_insn (mem_pseudo, val)
1039 : gen_move_insn (val, mem_pseudo));
1042 /* Process a special case insn (register move), return true if we
1043 don't need to process it anymore. INSN should be a single set
1044 insn. Set up that RTL was changed through CHANGE_P and macro
1045 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1046 SEC_MEM_P. */
1047 static bool
1048 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1050 int sregno, dregno;
1051 rtx dest, src, dreg, sreg, old_sreg, new_reg, scratch_reg;
1052 rtx_insn *before;
1053 enum reg_class dclass, sclass, secondary_class;
1054 machine_mode sreg_mode;
1055 secondary_reload_info sri;
1057 lra_assert (curr_insn_set != NULL_RTX);
1058 dreg = dest = SET_DEST (curr_insn_set);
1059 sreg = src = SET_SRC (curr_insn_set);
1060 if (GET_CODE (dest) == SUBREG)
1061 dreg = SUBREG_REG (dest);
1062 if (GET_CODE (src) == SUBREG)
1063 sreg = SUBREG_REG (src);
1064 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1065 return false;
1066 sclass = dclass = NO_REGS;
1067 if (REG_P (dreg))
1068 dclass = get_reg_class (REGNO (dreg));
1069 if (dclass == ALL_REGS)
1070 /* ALL_REGS is used for new pseudos created by transformations
1071 like reload of SUBREG_REG (see function
1072 simplify_operand_subreg). We don't know their class yet. We
1073 should figure out the class from processing the insn
1074 constraints not in this fast path function. Even if ALL_REGS
1075 were a right class for the pseudo, secondary_... hooks usually
1076 are not define for ALL_REGS. */
1077 return false;
1078 sreg_mode = GET_MODE (sreg);
1079 old_sreg = sreg;
1080 if (REG_P (sreg))
1081 sclass = get_reg_class (REGNO (sreg));
1082 if (sclass == ALL_REGS)
1083 /* See comments above. */
1084 return false;
1085 if (sclass == NO_REGS && dclass == NO_REGS)
1086 return false;
1087 #ifdef SECONDARY_MEMORY_NEEDED
1088 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1089 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1090 && ((sclass != NO_REGS && dclass != NO_REGS)
1091 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1092 #endif
1095 *sec_mem_p = true;
1096 return false;
1098 #endif
1099 if (! REG_P (dreg) || ! REG_P (sreg))
1100 return false;
1101 sri.prev_sri = NULL;
1102 sri.icode = CODE_FOR_nothing;
1103 sri.extra_cost = 0;
1104 secondary_class = NO_REGS;
1105 /* Set up hard register for a reload pseudo for hook
1106 secondary_reload because some targets just ignore unassigned
1107 pseudos in the hook. */
1108 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1110 dregno = REGNO (dreg);
1111 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1113 else
1114 dregno = -1;
1115 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1117 sregno = REGNO (sreg);
1118 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1120 else
1121 sregno = -1;
1122 if (sclass != NO_REGS)
1123 secondary_class
1124 = (enum reg_class) targetm.secondary_reload (false, dest,
1125 (reg_class_t) sclass,
1126 GET_MODE (src), &sri);
1127 if (sclass == NO_REGS
1128 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1129 && dclass != NO_REGS))
1131 enum reg_class old_sclass = secondary_class;
1132 secondary_reload_info old_sri = sri;
1134 sri.prev_sri = NULL;
1135 sri.icode = CODE_FOR_nothing;
1136 sri.extra_cost = 0;
1137 secondary_class
1138 = (enum reg_class) targetm.secondary_reload (true, sreg,
1139 (reg_class_t) dclass,
1140 sreg_mode, &sri);
1141 /* Check the target hook consistency. */
1142 lra_assert
1143 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1144 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1145 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1147 if (sregno >= 0)
1148 reg_renumber [sregno] = -1;
1149 if (dregno >= 0)
1150 reg_renumber [dregno] = -1;
1151 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1152 return false;
1153 *change_p = true;
1154 new_reg = NULL_RTX;
1155 if (secondary_class != NO_REGS)
1156 new_reg = lra_create_new_reg_with_unique_value (sreg_mode, NULL_RTX,
1157 secondary_class,
1158 "secondary");
1159 start_sequence ();
1160 if (old_sreg != sreg)
1161 sreg = copy_rtx (sreg);
1162 if (sri.icode == CODE_FOR_nothing)
1163 lra_emit_move (new_reg, sreg);
1164 else
1166 enum reg_class scratch_class;
1168 scratch_class = (reg_class_from_constraints
1169 (insn_data[sri.icode].operand[2].constraint));
1170 scratch_reg = (lra_create_new_reg_with_unique_value
1171 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1172 scratch_class, "scratch"));
1173 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1174 sreg, scratch_reg));
1176 before = get_insns ();
1177 end_sequence ();
1178 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1179 if (new_reg != NULL_RTX)
1181 if (GET_CODE (src) == SUBREG)
1182 SUBREG_REG (src) = new_reg;
1183 else
1184 SET_SRC (curr_insn_set) = new_reg;
1186 else
1188 if (lra_dump_file != NULL)
1190 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1191 dump_insn_slim (lra_dump_file, curr_insn);
1193 lra_set_insn_deleted (curr_insn);
1194 return true;
1196 return false;
1199 /* The following data describe the result of process_alt_operands.
1200 The data are used in curr_insn_transform to generate reloads. */
1202 /* The chosen reg classes which should be used for the corresponding
1203 operands. */
1204 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1205 /* True if the operand should be the same as another operand and that
1206 other operand does not need a reload. */
1207 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1208 /* True if the operand does not need a reload. */
1209 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1210 /* True if the operand can be offsetable memory. */
1211 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1212 /* The number of an operand to which given operand can be matched to. */
1213 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1214 /* The number of elements in the following array. */
1215 static int goal_alt_dont_inherit_ops_num;
1216 /* Numbers of operands whose reload pseudos should not be inherited. */
1217 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1218 /* True if the insn commutative operands should be swapped. */
1219 static bool goal_alt_swapped;
1220 /* The chosen insn alternative. */
1221 static int goal_alt_number;
1223 /* The following five variables are used to choose the best insn
1224 alternative. They reflect final characteristics of the best
1225 alternative. */
1227 /* Number of necessary reloads and overall cost reflecting the
1228 previous value and other unpleasantness of the best alternative. */
1229 static int best_losers, best_overall;
1230 /* Overall number hard registers used for reloads. For example, on
1231 some targets we need 2 general registers to reload DFmode and only
1232 one floating point register. */
1233 static int best_reload_nregs;
1234 /* Overall number reflecting distances of previous reloading the same
1235 value. The distances are counted from the current BB start. It is
1236 used to improve inheritance chances. */
1237 static int best_reload_sum;
1239 /* True if the current insn should have no correspondingly input or
1240 output reloads. */
1241 static bool no_input_reloads_p, no_output_reloads_p;
1243 /* True if we swapped the commutative operands in the current
1244 insn. */
1245 static int curr_swapped;
1247 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1248 register of class CL. Add any input reloads to list BEFORE. AFTER
1249 is nonnull if *LOC is an automodified value; handle that case by
1250 adding the required output reloads to list AFTER. Return true if
1251 the RTL was changed.
1253 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1254 register. Return false if the address register is correct. */
1255 static bool
1256 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1257 enum reg_class cl)
1259 int regno;
1260 enum reg_class rclass, new_class;
1261 rtx reg;
1262 rtx new_reg;
1263 machine_mode mode;
1264 bool subreg_p, before_p = false;
1266 subreg_p = GET_CODE (*loc) == SUBREG;
1267 if (subreg_p)
1268 loc = &SUBREG_REG (*loc);
1269 reg = *loc;
1270 mode = GET_MODE (reg);
1271 if (! REG_P (reg))
1273 if (check_only_p)
1274 return true;
1275 /* Always reload memory in an address even if the target supports
1276 such addresses. */
1277 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1278 before_p = true;
1280 else
1282 regno = REGNO (reg);
1283 rclass = get_reg_class (regno);
1284 if (! check_only_p
1285 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1287 if (lra_dump_file != NULL)
1289 fprintf (lra_dump_file,
1290 "Changing pseudo %d in address of insn %u on equiv ",
1291 REGNO (reg), INSN_UID (curr_insn));
1292 dump_value_slim (lra_dump_file, *loc, 1);
1293 fprintf (lra_dump_file, "\n");
1295 *loc = copy_rtx (*loc);
1297 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1299 if (check_only_p)
1300 return true;
1301 reg = *loc;
1302 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1303 mode, reg, cl, subreg_p, "address", &new_reg))
1304 before_p = true;
1306 else if (new_class != NO_REGS && rclass != new_class)
1308 if (check_only_p)
1309 return true;
1310 lra_change_class (regno, new_class, " Change to", true);
1311 return false;
1313 else
1314 return false;
1316 if (before_p)
1318 push_to_sequence (*before);
1319 lra_emit_move (new_reg, reg);
1320 *before = get_insns ();
1321 end_sequence ();
1323 *loc = new_reg;
1324 if (after != NULL)
1326 start_sequence ();
1327 lra_emit_move (reg, new_reg);
1328 emit_insn (*after);
1329 *after = get_insns ();
1330 end_sequence ();
1332 return true;
1335 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1336 the insn to be inserted before curr insn. AFTER returns the
1337 the insn to be inserted after curr insn. ORIGREG and NEWREG
1338 are the original reg and new reg for reload. */
1339 static void
1340 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1341 rtx newreg)
1343 if (before)
1345 push_to_sequence (*before);
1346 lra_emit_move (newreg, origreg);
1347 *before = get_insns ();
1348 end_sequence ();
1350 if (after)
1352 start_sequence ();
1353 lra_emit_move (origreg, newreg);
1354 emit_insn (*after);
1355 *after = get_insns ();
1356 end_sequence ();
1360 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1362 /* Make reloads for subreg in operand NOP with internal subreg mode
1363 REG_MODE, add new reloads for further processing. Return true if
1364 any reload was generated. */
1365 static bool
1366 simplify_operand_subreg (int nop, machine_mode reg_mode)
1368 int hard_regno;
1369 rtx_insn *before, *after;
1370 machine_mode mode;
1371 rtx reg, new_reg;
1372 rtx operand = *curr_id->operand_loc[nop];
1373 enum reg_class regclass;
1374 enum op_type type;
1376 before = after = NULL;
1378 if (GET_CODE (operand) != SUBREG)
1379 return false;
1381 mode = GET_MODE (operand);
1382 reg = SUBREG_REG (operand);
1383 type = curr_static_id->operand[nop].type;
1384 /* If we change address for paradoxical subreg of memory, the
1385 address might violate the necessary alignment or the access might
1386 be slow. So take this into consideration. We should not worry
1387 about access beyond allocated memory for paradoxical memory
1388 subregs as we don't substitute such equiv memory (see processing
1389 equivalences in function lra_constraints) and because for spilled
1390 pseudos we allocate stack memory enough for the biggest
1391 corresponding paradoxical subreg. */
1392 if (MEM_P (reg)
1393 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1394 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1396 rtx subst, old = *curr_id->operand_loc[nop];
1398 alter_subreg (curr_id->operand_loc[nop], false);
1399 subst = *curr_id->operand_loc[nop];
1400 lra_assert (MEM_P (subst));
1401 if (! valid_address_p (GET_MODE (reg), XEXP (reg, 0),
1402 MEM_ADDR_SPACE (reg))
1403 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1404 MEM_ADDR_SPACE (subst)))
1405 return true;
1406 /* If the address was valid and became invalid, prefer to reload
1407 the memory. Typical case is when the index scale should
1408 correspond the memory. */
1409 *curr_id->operand_loc[nop] = old;
1411 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1413 alter_subreg (curr_id->operand_loc[nop], false);
1414 return true;
1416 /* Put constant into memory when we have mixed modes. It generates
1417 a better code in most cases as it does not need a secondary
1418 reload memory. It also prevents LRA looping when LRA is using
1419 secondary reload memory again and again. */
1420 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1421 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1423 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1424 alter_subreg (curr_id->operand_loc[nop], false);
1425 return true;
1427 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1428 if there may be a problem accessing OPERAND in the outer
1429 mode. */
1430 if ((REG_P (reg)
1431 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1432 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1433 /* Don't reload paradoxical subregs because we could be looping
1434 having repeatedly final regno out of hard regs range. */
1435 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1436 >= hard_regno_nregs[hard_regno][mode])
1437 && simplify_subreg_regno (hard_regno, GET_MODE (reg),
1438 SUBREG_BYTE (operand), mode) < 0
1439 /* Don't reload subreg for matching reload. It is actually
1440 valid subreg in LRA. */
1441 && ! LRA_SUBREG_P (operand))
1442 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1444 enum reg_class rclass;
1446 if (REG_P (reg))
1447 /* There is a big probability that we will get the same class
1448 for the new pseudo and we will get the same insn which
1449 means infinite looping. So spill the new pseudo. */
1450 rclass = NO_REGS;
1451 else
1452 /* The class will be defined later in curr_insn_transform. */
1453 rclass
1454 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1456 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1457 rclass, TRUE, "subreg reg", &new_reg))
1459 bool insert_before, insert_after;
1460 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1462 insert_before = (type != OP_OUT
1463 || GET_MODE_SIZE (GET_MODE (reg)) > GET_MODE_SIZE (mode));
1464 insert_after = (type != OP_IN);
1465 insert_move_for_subreg (insert_before ? &before : NULL,
1466 insert_after ? &after : NULL,
1467 reg, new_reg);
1469 SUBREG_REG (operand) = new_reg;
1470 lra_process_new_insns (curr_insn, before, after,
1471 "Inserting subreg reload");
1472 return true;
1474 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1475 IRA allocates hardreg to the inner pseudo reg according to its mode
1476 instead of the outermode, so the size of the hardreg may not be enough
1477 to contain the outermode operand, in that case we may need to insert
1478 reload for the reg. For the following two types of paradoxical subreg,
1479 we need to insert reload:
1480 1. If the op_type is OP_IN, and the hardreg could not be paired with
1481 other hardreg to contain the outermode operand
1482 (checked by in_hard_reg_set_p), we need to insert the reload.
1483 2. If the op_type is OP_OUT or OP_INOUT.
1485 Here is a paradoxical subreg example showing how the reload is generated:
1487 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1488 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1490 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1491 here, if reg107 is assigned to hardreg R15, because R15 is the last
1492 hardreg, compiler cannot find another hardreg to pair with R15 to
1493 contain TImode data. So we insert a TImode reload reg180 for it.
1494 After reload is inserted:
1496 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1497 (reg:DI 107 [ __comp ])) -1
1498 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1499 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1501 Two reload hard registers will be allocated to reg180 to save TImode data
1502 in LRA_assign. */
1503 else if (REG_P (reg)
1504 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1505 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1506 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1507 < hard_regno_nregs[hard_regno][mode])
1508 && (regclass = lra_get_allocno_class (REGNO (reg)))
1509 && (type != OP_IN
1510 || !in_hard_reg_set_p (reg_class_contents[regclass],
1511 mode, hard_regno)))
1513 /* The class will be defined later in curr_insn_transform. */
1514 enum reg_class rclass
1515 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1517 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1518 rclass, TRUE, "paradoxical subreg", &new_reg))
1520 rtx subreg;
1521 bool insert_before, insert_after;
1523 PUT_MODE (new_reg, mode);
1524 subreg = simplify_gen_subreg (GET_MODE (reg), new_reg, mode, 0);
1525 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1527 insert_before = (type != OP_OUT);
1528 insert_after = (type != OP_IN);
1529 insert_move_for_subreg (insert_before ? &before : NULL,
1530 insert_after ? &after : NULL,
1531 reg, subreg);
1533 SUBREG_REG (operand) = new_reg;
1534 lra_process_new_insns (curr_insn, before, after,
1535 "Inserting paradoxical subreg reload");
1536 return true;
1538 return false;
1541 /* Return TRUE if X refers for a hard register from SET. */
1542 static bool
1543 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1545 int i, j, x_hard_regno;
1546 machine_mode mode;
1547 const char *fmt;
1548 enum rtx_code code;
1550 if (x == NULL_RTX)
1551 return false;
1552 code = GET_CODE (x);
1553 mode = GET_MODE (x);
1554 if (code == SUBREG)
1556 x = SUBREG_REG (x);
1557 code = GET_CODE (x);
1558 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1559 mode = GET_MODE (x);
1562 if (REG_P (x))
1564 x_hard_regno = get_hard_regno (x);
1565 return (x_hard_regno >= 0
1566 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1568 if (MEM_P (x))
1570 struct address_info ad;
1572 decompose_mem_address (&ad, x);
1573 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1574 return true;
1575 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1576 return true;
1578 fmt = GET_RTX_FORMAT (code);
1579 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1581 if (fmt[i] == 'e')
1583 if (uses_hard_regs_p (XEXP (x, i), set))
1584 return true;
1586 else if (fmt[i] == 'E')
1588 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1589 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1590 return true;
1593 return false;
1596 /* Return true if OP is a spilled pseudo. */
1597 static inline bool
1598 spilled_pseudo_p (rtx op)
1600 return (REG_P (op)
1601 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1604 /* Return true if X is a general constant. */
1605 static inline bool
1606 general_constant_p (rtx x)
1608 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1611 static bool
1612 reg_in_class_p (rtx reg, enum reg_class cl)
1614 if (cl == NO_REGS)
1615 return get_reg_class (REGNO (reg)) == NO_REGS;
1616 return in_class_p (reg, cl, NULL);
1619 /* Major function to choose the current insn alternative and what
1620 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1621 negative we should consider only this alternative. Return false if
1622 we can not choose the alternative or find how to reload the
1623 operands. */
1624 static bool
1625 process_alt_operands (int only_alternative)
1627 bool ok_p = false;
1628 int nop, overall, nalt;
1629 int n_alternatives = curr_static_id->n_alternatives;
1630 int n_operands = curr_static_id->n_operands;
1631 /* LOSERS counts the operands that don't fit this alternative and
1632 would require loading. */
1633 int losers;
1634 /* REJECT is a count of how undesirable this alternative says it is
1635 if any reloading is required. If the alternative matches exactly
1636 then REJECT is ignored, but otherwise it gets this much counted
1637 against it in addition to the reloading needed. */
1638 int reject;
1639 /* The number of elements in the following array. */
1640 int early_clobbered_regs_num;
1641 /* Numbers of operands which are early clobber registers. */
1642 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1643 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1644 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1645 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1646 bool curr_alt_win[MAX_RECOG_OPERANDS];
1647 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1648 int curr_alt_matches[MAX_RECOG_OPERANDS];
1649 /* The number of elements in the following array. */
1650 int curr_alt_dont_inherit_ops_num;
1651 /* Numbers of operands whose reload pseudos should not be inherited. */
1652 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1653 rtx op;
1654 /* The register when the operand is a subreg of register, otherwise the
1655 operand itself. */
1656 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1657 /* The register if the operand is a register or subreg of register,
1658 otherwise NULL. */
1659 rtx operand_reg[MAX_RECOG_OPERANDS];
1660 int hard_regno[MAX_RECOG_OPERANDS];
1661 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1662 int reload_nregs, reload_sum;
1663 bool costly_p;
1664 enum reg_class cl;
1666 /* Calculate some data common for all alternatives to speed up the
1667 function. */
1668 for (nop = 0; nop < n_operands; nop++)
1670 rtx reg;
1672 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1673 /* The real hard regno of the operand after the allocation. */
1674 hard_regno[nop] = get_hard_regno (op);
1676 operand_reg[nop] = reg = op;
1677 biggest_mode[nop] = GET_MODE (op);
1678 if (GET_CODE (op) == SUBREG)
1680 operand_reg[nop] = reg = SUBREG_REG (op);
1681 if (GET_MODE_SIZE (biggest_mode[nop])
1682 < GET_MODE_SIZE (GET_MODE (reg)))
1683 biggest_mode[nop] = GET_MODE (reg);
1685 if (! REG_P (reg))
1686 operand_reg[nop] = NULL_RTX;
1687 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1688 || ((int) REGNO (reg)
1689 == lra_get_elimination_hard_regno (REGNO (reg))))
1690 no_subreg_reg_operand[nop] = reg;
1691 else
1692 operand_reg[nop] = no_subreg_reg_operand[nop]
1693 /* Just use natural mode for elimination result. It should
1694 be enough for extra constraints hooks. */
1695 = regno_reg_rtx[hard_regno[nop]];
1698 /* The constraints are made of several alternatives. Each operand's
1699 constraint looks like foo,bar,... with commas separating the
1700 alternatives. The first alternatives for all operands go
1701 together, the second alternatives go together, etc.
1703 First loop over alternatives. */
1704 alternative_mask preferred = curr_id->preferred_alternatives;
1705 if (only_alternative >= 0)
1706 preferred &= ALTERNATIVE_BIT (only_alternative);
1708 for (nalt = 0; nalt < n_alternatives; nalt++)
1710 /* Loop over operands for one constraint alternative. */
1711 if (!TEST_BIT (preferred, nalt))
1712 continue;
1714 overall = losers = reject = reload_nregs = reload_sum = 0;
1715 for (nop = 0; nop < n_operands; nop++)
1717 int inc = (curr_static_id
1718 ->operand_alternative[nalt * n_operands + nop].reject);
1719 if (lra_dump_file != NULL && inc != 0)
1720 fprintf (lra_dump_file,
1721 " Staticly defined alt reject+=%d\n", inc);
1722 reject += inc;
1724 early_clobbered_regs_num = 0;
1726 for (nop = 0; nop < n_operands; nop++)
1728 const char *p;
1729 char *end;
1730 int len, c, m, i, opalt_num, this_alternative_matches;
1731 bool win, did_match, offmemok, early_clobber_p;
1732 /* false => this operand can be reloaded somehow for this
1733 alternative. */
1734 bool badop;
1735 /* true => this operand can be reloaded if the alternative
1736 allows regs. */
1737 bool winreg;
1738 /* True if a constant forced into memory would be OK for
1739 this operand. */
1740 bool constmemok;
1741 enum reg_class this_alternative, this_costly_alternative;
1742 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1743 bool this_alternative_match_win, this_alternative_win;
1744 bool this_alternative_offmemok;
1745 bool scratch_p;
1746 machine_mode mode;
1747 enum constraint_num cn;
1749 opalt_num = nalt * n_operands + nop;
1750 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1752 /* Fast track for no constraints at all. */
1753 curr_alt[nop] = NO_REGS;
1754 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1755 curr_alt_win[nop] = true;
1756 curr_alt_match_win[nop] = false;
1757 curr_alt_offmemok[nop] = false;
1758 curr_alt_matches[nop] = -1;
1759 continue;
1762 op = no_subreg_reg_operand[nop];
1763 mode = curr_operand_mode[nop];
1765 win = did_match = winreg = offmemok = constmemok = false;
1766 badop = true;
1768 early_clobber_p = false;
1769 p = curr_static_id->operand_alternative[opalt_num].constraint;
1771 this_costly_alternative = this_alternative = NO_REGS;
1772 /* We update set of possible hard regs besides its class
1773 because reg class might be inaccurate. For example,
1774 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1775 is translated in HI_REGS because classes are merged by
1776 pairs and there is no accurate intermediate class. */
1777 CLEAR_HARD_REG_SET (this_alternative_set);
1778 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1779 this_alternative_win = false;
1780 this_alternative_match_win = false;
1781 this_alternative_offmemok = false;
1782 this_alternative_matches = -1;
1784 /* An empty constraint should be excluded by the fast
1785 track. */
1786 lra_assert (*p != 0 && *p != ',');
1788 /* Scan this alternative's specs for this operand; set WIN
1789 if the operand fits any letter in this alternative.
1790 Otherwise, clear BADOP if this operand could fit some
1791 letter after reloads, or set WINREG if this operand could
1792 fit after reloads provided the constraint allows some
1793 registers. */
1794 costly_p = false;
1797 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1799 case '\0':
1800 len = 0;
1801 break;
1802 case ',':
1803 c = '\0';
1804 break;
1806 case '&':
1807 early_clobber_p = true;
1808 break;
1810 case '#':
1811 /* Ignore rest of this alternative. */
1812 c = '\0';
1813 break;
1815 case '0': case '1': case '2': case '3': case '4':
1816 case '5': case '6': case '7': case '8': case '9':
1818 int m_hregno;
1819 bool match_p;
1821 m = strtoul (p, &end, 10);
1822 p = end;
1823 len = 0;
1824 lra_assert (nop > m);
1826 this_alternative_matches = m;
1827 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1828 /* We are supposed to match a previous operand.
1829 If we do, we win if that one did. If we do
1830 not, count both of the operands as losers.
1831 (This is too conservative, since most of the
1832 time only a single reload insn will be needed
1833 to make the two operands win. As a result,
1834 this alternative may be rejected when it is
1835 actually desirable.) */
1836 match_p = false;
1837 if (operands_match_p (*curr_id->operand_loc[nop],
1838 *curr_id->operand_loc[m], m_hregno))
1840 /* We should reject matching of an early
1841 clobber operand if the matching operand is
1842 not dying in the insn. */
1843 if (! curr_static_id->operand[m].early_clobber
1844 || operand_reg[nop] == NULL_RTX
1845 || (find_regno_note (curr_insn, REG_DEAD,
1846 REGNO (op))
1847 || REGNO (op) == REGNO (operand_reg[m])))
1848 match_p = true;
1850 if (match_p)
1852 /* If we are matching a non-offsettable
1853 address where an offsettable address was
1854 expected, then we must reject this
1855 combination, because we can't reload
1856 it. */
1857 if (curr_alt_offmemok[m]
1858 && MEM_P (*curr_id->operand_loc[m])
1859 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1860 continue;
1862 else
1864 /* Operands don't match. Both operands must
1865 allow a reload register, otherwise we
1866 cannot make them match. */
1867 if (curr_alt[m] == NO_REGS)
1868 break;
1869 /* Retroactively mark the operand we had to
1870 match as a loser, if it wasn't already and
1871 it wasn't matched to a register constraint
1872 (e.g it might be matched by memory). */
1873 if (curr_alt_win[m]
1874 && (operand_reg[m] == NULL_RTX
1875 || hard_regno[m] < 0))
1877 losers++;
1878 reload_nregs
1879 += (ira_reg_class_max_nregs[curr_alt[m]]
1880 [GET_MODE (*curr_id->operand_loc[m])]);
1883 /* Prefer matching earlyclobber alternative as
1884 it results in less hard regs required for
1885 the insn than a non-matching earlyclobber
1886 alternative. */
1887 if (curr_static_id->operand[m].early_clobber)
1889 if (lra_dump_file != NULL)
1890 fprintf
1891 (lra_dump_file,
1892 " %d Matching earlyclobber alt:"
1893 " reject--\n",
1894 nop);
1895 reject--;
1897 /* Otherwise we prefer no matching
1898 alternatives because it gives more freedom
1899 in RA. */
1900 else if (operand_reg[nop] == NULL_RTX
1901 || (find_regno_note (curr_insn, REG_DEAD,
1902 REGNO (operand_reg[nop]))
1903 == NULL_RTX))
1905 if (lra_dump_file != NULL)
1906 fprintf
1907 (lra_dump_file,
1908 " %d Matching alt: reject+=2\n",
1909 nop);
1910 reject += 2;
1913 /* If we have to reload this operand and some
1914 previous operand also had to match the same
1915 thing as this operand, we don't know how to do
1916 that. */
1917 if (!match_p || !curr_alt_win[m])
1919 for (i = 0; i < nop; i++)
1920 if (curr_alt_matches[i] == m)
1921 break;
1922 if (i < nop)
1923 break;
1925 else
1926 did_match = true;
1928 /* This can be fixed with reloads if the operand
1929 we are supposed to match can be fixed with
1930 reloads. */
1931 badop = false;
1932 this_alternative = curr_alt[m];
1933 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1934 winreg = this_alternative != NO_REGS;
1935 break;
1938 case 'g':
1939 if (MEM_P (op)
1940 || general_constant_p (op)
1941 || spilled_pseudo_p (op))
1942 win = true;
1943 cl = GENERAL_REGS;
1944 goto reg;
1946 default:
1947 cn = lookup_constraint (p);
1948 switch (get_constraint_type (cn))
1950 case CT_REGISTER:
1951 cl = reg_class_for_constraint (cn);
1952 if (cl != NO_REGS)
1953 goto reg;
1954 break;
1956 case CT_CONST_INT:
1957 if (CONST_INT_P (op)
1958 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
1959 win = true;
1960 break;
1962 case CT_MEMORY:
1963 if (MEM_P (op)
1964 && satisfies_memory_constraint_p (op, cn))
1965 win = true;
1966 else if (spilled_pseudo_p (op))
1967 win = true;
1969 /* If we didn't already win, we can reload constants
1970 via force_const_mem or put the pseudo value into
1971 memory, or make other memory by reloading the
1972 address like for 'o'. */
1973 if (CONST_POOL_OK_P (mode, op)
1974 || MEM_P (op) || REG_P (op))
1975 badop = false;
1976 constmemok = true;
1977 offmemok = true;
1978 break;
1980 case CT_ADDRESS:
1981 /* If we didn't already win, we can reload the address
1982 into a base register. */
1983 if (satisfies_address_constraint_p (op, cn))
1984 win = true;
1985 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1986 ADDRESS, SCRATCH);
1987 badop = false;
1988 goto reg;
1990 case CT_FIXED_FORM:
1991 if (constraint_satisfied_p (op, cn))
1992 win = true;
1993 break;
1995 break;
1997 reg:
1998 this_alternative = reg_class_subunion[this_alternative][cl];
1999 IOR_HARD_REG_SET (this_alternative_set,
2000 reg_class_contents[cl]);
2001 if (costly_p)
2003 this_costly_alternative
2004 = reg_class_subunion[this_costly_alternative][cl];
2005 IOR_HARD_REG_SET (this_costly_alternative_set,
2006 reg_class_contents[cl]);
2008 if (mode == BLKmode)
2009 break;
2010 winreg = true;
2011 if (REG_P (op))
2013 if (hard_regno[nop] >= 0
2014 && in_hard_reg_set_p (this_alternative_set,
2015 mode, hard_regno[nop]))
2016 win = true;
2017 else if (hard_regno[nop] < 0
2018 && in_class_p (op, this_alternative, NULL))
2019 win = true;
2021 break;
2023 if (c != ' ' && c != '\t')
2024 costly_p = c == '*';
2026 while ((p += len), c);
2028 scratch_p = (operand_reg[nop] != NULL_RTX
2029 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2030 /* Record which operands fit this alternative. */
2031 if (win)
2033 this_alternative_win = true;
2034 if (operand_reg[nop] != NULL_RTX)
2036 if (hard_regno[nop] >= 0)
2038 if (in_hard_reg_set_p (this_costly_alternative_set,
2039 mode, hard_regno[nop]))
2041 if (lra_dump_file != NULL)
2042 fprintf (lra_dump_file,
2043 " %d Costly set: reject++\n",
2044 nop);
2045 reject++;
2048 else
2050 /* Prefer won reg to spilled pseudo under other
2051 equal conditions for possibe inheritance. */
2052 if (! scratch_p)
2054 if (lra_dump_file != NULL)
2055 fprintf
2056 (lra_dump_file,
2057 " %d Non pseudo reload: reject++\n",
2058 nop);
2059 reject++;
2061 if (in_class_p (operand_reg[nop],
2062 this_costly_alternative, NULL))
2064 if (lra_dump_file != NULL)
2065 fprintf
2066 (lra_dump_file,
2067 " %d Non pseudo costly reload:"
2068 " reject++\n",
2069 nop);
2070 reject++;
2073 /* We simulate the behaviour of old reload here.
2074 Although scratches need hard registers and it
2075 might result in spilling other pseudos, no reload
2076 insns are generated for the scratches. So it
2077 might cost something but probably less than old
2078 reload pass believes. */
2079 if (scratch_p)
2081 if (lra_dump_file != NULL)
2082 fprintf (lra_dump_file,
2083 " %d Scratch win: reject+=2\n",
2084 nop);
2085 reject += 2;
2089 else if (did_match)
2090 this_alternative_match_win = true;
2091 else
2093 int const_to_mem = 0;
2094 bool no_regs_p;
2096 /* Never do output reload of stack pointer. It makes
2097 impossible to do elimination when SP is changed in
2098 RTL. */
2099 if (op == stack_pointer_rtx && ! frame_pointer_needed
2100 && curr_static_id->operand[nop].type != OP_IN)
2101 goto fail;
2103 /* If this alternative asks for a specific reg class, see if there
2104 is at least one allocatable register in that class. */
2105 no_regs_p
2106 = (this_alternative == NO_REGS
2107 || (hard_reg_set_subset_p
2108 (reg_class_contents[this_alternative],
2109 lra_no_alloc_regs)));
2111 /* For asms, verify that the class for this alternative is possible
2112 for the mode that is specified. */
2113 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2115 int i;
2116 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2117 if (HARD_REGNO_MODE_OK (i, mode)
2118 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2119 mode, i))
2120 break;
2121 if (i == FIRST_PSEUDO_REGISTER)
2122 winreg = false;
2125 /* If this operand accepts a register, and if the
2126 register class has at least one allocatable register,
2127 then this operand can be reloaded. */
2128 if (winreg && !no_regs_p)
2129 badop = false;
2131 if (badop)
2133 if (lra_dump_file != NULL)
2134 fprintf (lra_dump_file,
2135 " alt=%d: Bad operand -- refuse\n",
2136 nalt);
2137 goto fail;
2140 /* If not assigned pseudo has a class which a subset of
2141 required reg class, it is a less costly alternative
2142 as the pseudo still can get a hard reg of necessary
2143 class. */
2144 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2145 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2146 && ira_class_subset_p[this_alternative][cl])
2148 if (lra_dump_file != NULL)
2149 fprintf
2150 (lra_dump_file,
2151 " %d Super set class reg: reject-=3\n", nop);
2152 reject -= 3;
2155 this_alternative_offmemok = offmemok;
2156 if (this_costly_alternative != NO_REGS)
2158 if (lra_dump_file != NULL)
2159 fprintf (lra_dump_file,
2160 " %d Costly loser: reject++\n", nop);
2161 reject++;
2163 /* If the operand is dying, has a matching constraint,
2164 and satisfies constraints of the matched operand
2165 which failed to satisfy the own constraints, most probably
2166 the reload for this operand will be gone. */
2167 if (this_alternative_matches >= 0
2168 && !curr_alt_win[this_alternative_matches]
2169 && REG_P (op)
2170 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2171 && (hard_regno[nop] >= 0
2172 ? in_hard_reg_set_p (this_alternative_set,
2173 mode, hard_regno[nop])
2174 : in_class_p (op, this_alternative, NULL)))
2176 if (lra_dump_file != NULL)
2177 fprintf
2178 (lra_dump_file,
2179 " %d Dying matched operand reload: reject++\n",
2180 nop);
2181 reject++;
2183 else
2185 /* Strict_low_part requires to reload the register
2186 not the sub-register. In this case we should
2187 check that a final reload hard reg can hold the
2188 value mode. */
2189 if (curr_static_id->operand[nop].strict_low
2190 && REG_P (op)
2191 && hard_regno[nop] < 0
2192 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2193 && ira_class_hard_regs_num[this_alternative] > 0
2194 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2195 [this_alternative][0],
2196 GET_MODE
2197 (*curr_id->operand_loc[nop])))
2199 if (lra_dump_file != NULL)
2200 fprintf
2201 (lra_dump_file,
2202 " alt=%d: Strict low subreg reload -- refuse\n",
2203 nalt);
2204 goto fail;
2206 losers++;
2208 if (operand_reg[nop] != NULL_RTX
2209 /* Output operands and matched input operands are
2210 not inherited. The following conditions do not
2211 exactly describe the previous statement but they
2212 are pretty close. */
2213 && curr_static_id->operand[nop].type != OP_OUT
2214 && (this_alternative_matches < 0
2215 || curr_static_id->operand[nop].type != OP_IN))
2217 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2218 (operand_reg[nop])]
2219 .last_reload);
2221 /* The value of reload_sum has sense only if we
2222 process insns in their order. It happens only on
2223 the first constraints sub-pass when we do most of
2224 reload work. */
2225 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2226 reload_sum += last_reload - bb_reload_num;
2228 /* If this is a constant that is reloaded into the
2229 desired class by copying it to memory first, count
2230 that as another reload. This is consistent with
2231 other code and is required to avoid choosing another
2232 alternative when the constant is moved into memory.
2233 Note that the test here is precisely the same as in
2234 the code below that calls force_const_mem. */
2235 if (CONST_POOL_OK_P (mode, op)
2236 && ((targetm.preferred_reload_class
2237 (op, this_alternative) == NO_REGS)
2238 || no_input_reloads_p))
2240 const_to_mem = 1;
2241 if (! no_regs_p)
2242 losers++;
2245 /* Alternative loses if it requires a type of reload not
2246 permitted for this insn. We can always reload
2247 objects with a REG_UNUSED note. */
2248 if ((curr_static_id->operand[nop].type != OP_IN
2249 && no_output_reloads_p
2250 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2251 || (curr_static_id->operand[nop].type != OP_OUT
2252 && no_input_reloads_p && ! const_to_mem)
2253 || (this_alternative_matches >= 0
2254 && (no_input_reloads_p
2255 || (no_output_reloads_p
2256 && (curr_static_id->operand
2257 [this_alternative_matches].type != OP_IN)
2258 && ! find_reg_note (curr_insn, REG_UNUSED,
2259 no_subreg_reg_operand
2260 [this_alternative_matches])))))
2262 if (lra_dump_file != NULL)
2263 fprintf
2264 (lra_dump_file,
2265 " alt=%d: No input/otput reload -- refuse\n",
2266 nalt);
2267 goto fail;
2270 /* Alternative loses if it required class pseudo can not
2271 hold value of required mode. Such insns can be
2272 described by insn definitions with mode iterators. */
2273 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2274 && ! hard_reg_set_empty_p (this_alternative_set)
2275 /* It is common practice for constraints to use a
2276 class which does not have actually enough regs to
2277 hold the value (e.g. x86 AREG for mode requiring
2278 more one general reg). Therefore we have 2
2279 conditions to check that the reload pseudo can
2280 not hold the mode value. */
2281 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2282 [this_alternative][0],
2283 GET_MODE (*curr_id->operand_loc[nop])))
2285 HARD_REG_SET temp;
2287 COPY_HARD_REG_SET (temp, this_alternative_set);
2288 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
2289 /* The above condition is not enough as the first
2290 reg in ira_class_hard_regs can be not aligned for
2291 multi-words mode values. */
2292 if (hard_reg_set_subset_p (temp,
2293 ira_prohibited_class_mode_regs
2294 [this_alternative]
2295 [GET_MODE (*curr_id->operand_loc[nop])]))
2297 if (lra_dump_file != NULL)
2298 fprintf
2299 (lra_dump_file,
2300 " alt=%d: reload pseudo for op %d "
2301 " can not hold the mode value -- refuse\n",
2302 nalt, nop);
2303 goto fail;
2307 /* Check strong discouragement of reload of non-constant
2308 into class THIS_ALTERNATIVE. */
2309 if (! CONSTANT_P (op) && ! no_regs_p
2310 && (targetm.preferred_reload_class
2311 (op, this_alternative) == NO_REGS
2312 || (curr_static_id->operand[nop].type == OP_OUT
2313 && (targetm.preferred_output_reload_class
2314 (op, this_alternative) == NO_REGS))))
2316 if (lra_dump_file != NULL)
2317 fprintf (lra_dump_file,
2318 " %d Non-prefered reload: reject+=%d\n",
2319 nop, LRA_MAX_REJECT);
2320 reject += LRA_MAX_REJECT;
2323 if (! (MEM_P (op) && offmemok)
2324 && ! (const_to_mem && constmemok))
2326 /* We prefer to reload pseudos over reloading other
2327 things, since such reloads may be able to be
2328 eliminated later. So bump REJECT in other cases.
2329 Don't do this in the case where we are forcing a
2330 constant into memory and it will then win since
2331 we don't want to have a different alternative
2332 match then. */
2333 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2335 if (lra_dump_file != NULL)
2336 fprintf
2337 (lra_dump_file,
2338 " %d Non-pseudo reload: reject+=2\n",
2339 nop);
2340 reject += 2;
2343 if (! no_regs_p)
2344 reload_nregs
2345 += ira_reg_class_max_nregs[this_alternative][mode];
2347 if (SMALL_REGISTER_CLASS_P (this_alternative))
2349 if (lra_dump_file != NULL)
2350 fprintf
2351 (lra_dump_file,
2352 " %d Small class reload: reject+=%d\n",
2353 nop, LRA_LOSER_COST_FACTOR / 2);
2354 reject += LRA_LOSER_COST_FACTOR / 2;
2358 /* We are trying to spill pseudo into memory. It is
2359 usually more costly than moving to a hard register
2360 although it might takes the same number of
2361 reloads. */
2362 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2364 if (lra_dump_file != NULL)
2365 fprintf
2366 (lra_dump_file,
2367 " %d Spill pseudo into memory: reject+=3\n",
2368 nop);
2369 reject += 3;
2370 if (VECTOR_MODE_P (mode))
2372 /* Spilling vectors into memory is usually more
2373 costly as they contain big values. */
2374 if (lra_dump_file != NULL)
2375 fprintf
2376 (lra_dump_file,
2377 " %d Spill vector pseudo: reject+=2\n",
2378 nop);
2379 reject += 2;
2383 #ifdef SECONDARY_MEMORY_NEEDED
2384 /* If reload requires moving value through secondary
2385 memory, it will need one more insn at least. */
2386 if (this_alternative != NO_REGS
2387 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2388 && ((curr_static_id->operand[nop].type != OP_OUT
2389 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2390 GET_MODE (op)))
2391 || (curr_static_id->operand[nop].type != OP_IN
2392 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2393 GET_MODE (op)))))
2394 losers++;
2395 #endif
2396 /* Input reloads can be inherited more often than output
2397 reloads can be removed, so penalize output
2398 reloads. */
2399 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2401 if (lra_dump_file != NULL)
2402 fprintf
2403 (lra_dump_file,
2404 " %d Non input pseudo reload: reject++\n",
2405 nop);
2406 reject++;
2410 if (early_clobber_p && ! scratch_p)
2412 if (lra_dump_file != NULL)
2413 fprintf (lra_dump_file,
2414 " %d Early clobber: reject++\n", nop);
2415 reject++;
2417 /* ??? We check early clobbers after processing all operands
2418 (see loop below) and there we update the costs more.
2419 Should we update the cost (may be approximately) here
2420 because of early clobber register reloads or it is a rare
2421 or non-important thing to be worth to do it. */
2422 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2423 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2425 if (lra_dump_file != NULL)
2426 fprintf (lra_dump_file,
2427 " alt=%d,overall=%d,losers=%d -- refuse\n",
2428 nalt, overall, losers);
2429 goto fail;
2432 curr_alt[nop] = this_alternative;
2433 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2434 curr_alt_win[nop] = this_alternative_win;
2435 curr_alt_match_win[nop] = this_alternative_match_win;
2436 curr_alt_offmemok[nop] = this_alternative_offmemok;
2437 curr_alt_matches[nop] = this_alternative_matches;
2439 if (this_alternative_matches >= 0
2440 && !did_match && !this_alternative_win)
2441 curr_alt_win[this_alternative_matches] = false;
2443 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2444 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2446 if (curr_insn_set != NULL_RTX && n_operands == 2
2447 /* Prevent processing non-move insns. */
2448 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2449 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2450 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2451 && REG_P (no_subreg_reg_operand[0])
2452 && REG_P (no_subreg_reg_operand[1])
2453 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2454 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2455 || (! curr_alt_win[0] && curr_alt_win[1]
2456 && REG_P (no_subreg_reg_operand[1])
2457 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2458 || (curr_alt_win[0] && ! curr_alt_win[1]
2459 && REG_P (no_subreg_reg_operand[0])
2460 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2461 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2462 no_subreg_reg_operand[1])
2463 || (targetm.preferred_reload_class
2464 (no_subreg_reg_operand[1],
2465 (enum reg_class) curr_alt[1]) != NO_REGS))
2466 /* If it is a result of recent elimination in move
2467 insn we can transform it into an add still by
2468 using this alternative. */
2469 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2471 /* We have a move insn and a new reload insn will be similar
2472 to the current insn. We should avoid such situation as it
2473 results in LRA cycling. */
2474 overall += LRA_MAX_REJECT;
2476 ok_p = true;
2477 curr_alt_dont_inherit_ops_num = 0;
2478 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2480 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2481 HARD_REG_SET temp_set;
2483 i = early_clobbered_nops[nop];
2484 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2485 || hard_regno[i] < 0)
2486 continue;
2487 lra_assert (operand_reg[i] != NULL_RTX);
2488 clobbered_hard_regno = hard_regno[i];
2489 CLEAR_HARD_REG_SET (temp_set);
2490 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2491 first_conflict_j = last_conflict_j = -1;
2492 for (j = 0; j < n_operands; j++)
2493 if (j == i
2494 /* We don't want process insides of match_operator and
2495 match_parallel because otherwise we would process
2496 their operands once again generating a wrong
2497 code. */
2498 || curr_static_id->operand[j].is_operator)
2499 continue;
2500 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2501 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2502 continue;
2503 /* If we don't reload j-th operand, check conflicts. */
2504 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2505 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2507 if (first_conflict_j < 0)
2508 first_conflict_j = j;
2509 last_conflict_j = j;
2511 if (last_conflict_j < 0)
2512 continue;
2513 /* If earlyclobber operand conflicts with another
2514 non-matching operand which is actually the same register
2515 as the earlyclobber operand, it is better to reload the
2516 another operand as an operand matching the earlyclobber
2517 operand can be also the same. */
2518 if (first_conflict_j == last_conflict_j
2519 && operand_reg[last_conflict_j]
2520 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2521 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2523 curr_alt_win[last_conflict_j] = false;
2524 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2525 = last_conflict_j;
2526 losers++;
2527 /* Early clobber was already reflected in REJECT. */
2528 lra_assert (reject > 0);
2529 if (lra_dump_file != NULL)
2530 fprintf
2531 (lra_dump_file,
2532 " %d Conflict early clobber reload: reject--\n",
2534 reject--;
2535 overall += LRA_LOSER_COST_FACTOR - 1;
2537 else
2539 /* We need to reload early clobbered register and the
2540 matched registers. */
2541 for (j = 0; j < n_operands; j++)
2542 if (curr_alt_matches[j] == i)
2544 curr_alt_match_win[j] = false;
2545 losers++;
2546 overall += LRA_LOSER_COST_FACTOR;
2548 if (! curr_alt_match_win[i])
2549 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2550 else
2552 /* Remember pseudos used for match reloads are never
2553 inherited. */
2554 lra_assert (curr_alt_matches[i] >= 0);
2555 curr_alt_win[curr_alt_matches[i]] = false;
2557 curr_alt_win[i] = curr_alt_match_win[i] = false;
2558 losers++;
2559 /* Early clobber was already reflected in REJECT. */
2560 lra_assert (reject > 0);
2561 if (lra_dump_file != NULL)
2562 fprintf
2563 (lra_dump_file,
2564 " %d Matched conflict early clobber reloads:"
2565 "reject--\n",
2567 reject--;
2568 overall += LRA_LOSER_COST_FACTOR - 1;
2571 if (lra_dump_file != NULL)
2572 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2573 nalt, overall, losers, reload_nregs);
2575 /* If this alternative can be made to work by reloading, and it
2576 needs less reloading than the others checked so far, record
2577 it as the chosen goal for reloading. */
2578 if ((best_losers != 0 && losers == 0)
2579 || (((best_losers == 0 && losers == 0)
2580 || (best_losers != 0 && losers != 0))
2581 && (best_overall > overall
2582 || (best_overall == overall
2583 /* If the cost of the reloads is the same,
2584 prefer alternative which requires minimal
2585 number of reload regs. */
2586 && (reload_nregs < best_reload_nregs
2587 || (reload_nregs == best_reload_nregs
2588 && (best_reload_sum < reload_sum
2589 || (best_reload_sum == reload_sum
2590 && nalt < goal_alt_number))))))))
2592 for (nop = 0; nop < n_operands; nop++)
2594 goal_alt_win[nop] = curr_alt_win[nop];
2595 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2596 goal_alt_matches[nop] = curr_alt_matches[nop];
2597 goal_alt[nop] = curr_alt[nop];
2598 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2600 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2601 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2602 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2603 goal_alt_swapped = curr_swapped;
2604 best_overall = overall;
2605 best_losers = losers;
2606 best_reload_nregs = reload_nregs;
2607 best_reload_sum = reload_sum;
2608 goal_alt_number = nalt;
2610 if (losers == 0)
2611 /* Everything is satisfied. Do not process alternatives
2612 anymore. */
2613 break;
2614 fail:
2617 return ok_p;
2620 /* Make reload base reg from address AD. */
2621 static rtx
2622 base_to_reg (struct address_info *ad)
2624 enum reg_class cl;
2625 int code = -1;
2626 rtx new_inner = NULL_RTX;
2627 rtx new_reg = NULL_RTX;
2628 rtx_insn *insn;
2629 rtx_insn *last_insn = get_last_insn();
2631 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2632 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2633 get_index_code (ad));
2634 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2635 cl, "base");
2636 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2637 ad->disp_term == NULL
2638 ? gen_int_mode (0, ad->mode)
2639 : *ad->disp_term);
2640 if (!valid_address_p (ad->mode, new_inner, ad->as))
2641 return NULL_RTX;
2642 insn = emit_insn (gen_rtx_SET (ad->mode, new_reg, *ad->base_term));
2643 code = recog_memoized (insn);
2644 if (code < 0)
2646 delete_insns_since (last_insn);
2647 return NULL_RTX;
2650 return new_inner;
2653 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2654 static rtx
2655 base_plus_disp_to_reg (struct address_info *ad)
2657 enum reg_class cl;
2658 rtx new_reg;
2660 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2661 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2662 get_index_code (ad));
2663 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2664 cl, "base + disp");
2665 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2666 return new_reg;
2669 /* Make reload of index part of address AD. Return the new
2670 pseudo. */
2671 static rtx
2672 index_part_to_reg (struct address_info *ad)
2674 rtx new_reg;
2676 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2677 INDEX_REG_CLASS, "index term");
2678 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2679 GEN_INT (get_index_scale (ad)), new_reg, 1);
2680 return new_reg;
2683 /* Return true if we can add a displacement to address AD, even if that
2684 makes the address invalid. The fix-up code requires any new address
2685 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2686 static bool
2687 can_add_disp_p (struct address_info *ad)
2689 return (!ad->autoinc_p
2690 && ad->segment == NULL
2691 && ad->base == ad->base_term
2692 && ad->disp == ad->disp_term);
2695 /* Make equiv substitution in address AD. Return true if a substitution
2696 was made. */
2697 static bool
2698 equiv_address_substitution (struct address_info *ad)
2700 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2701 HOST_WIDE_INT disp, scale;
2702 bool change_p;
2704 base_term = strip_subreg (ad->base_term);
2705 if (base_term == NULL)
2706 base_reg = new_base_reg = NULL_RTX;
2707 else
2709 base_reg = *base_term;
2710 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2712 index_term = strip_subreg (ad->index_term);
2713 if (index_term == NULL)
2714 index_reg = new_index_reg = NULL_RTX;
2715 else
2717 index_reg = *index_term;
2718 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2720 if (base_reg == new_base_reg && index_reg == new_index_reg)
2721 return false;
2722 disp = 0;
2723 change_p = false;
2724 if (lra_dump_file != NULL)
2726 fprintf (lra_dump_file, "Changing address in insn %d ",
2727 INSN_UID (curr_insn));
2728 dump_value_slim (lra_dump_file, *ad->outer, 1);
2730 if (base_reg != new_base_reg)
2732 if (REG_P (new_base_reg))
2734 *base_term = new_base_reg;
2735 change_p = true;
2737 else if (GET_CODE (new_base_reg) == PLUS
2738 && REG_P (XEXP (new_base_reg, 0))
2739 && CONST_INT_P (XEXP (new_base_reg, 1))
2740 && can_add_disp_p (ad))
2742 disp += INTVAL (XEXP (new_base_reg, 1));
2743 *base_term = XEXP (new_base_reg, 0);
2744 change_p = true;
2746 if (ad->base_term2 != NULL)
2747 *ad->base_term2 = *ad->base_term;
2749 if (index_reg != new_index_reg)
2751 if (REG_P (new_index_reg))
2753 *index_term = new_index_reg;
2754 change_p = true;
2756 else if (GET_CODE (new_index_reg) == PLUS
2757 && REG_P (XEXP (new_index_reg, 0))
2758 && CONST_INT_P (XEXP (new_index_reg, 1))
2759 && can_add_disp_p (ad)
2760 && (scale = get_index_scale (ad)))
2762 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2763 *index_term = XEXP (new_index_reg, 0);
2764 change_p = true;
2767 if (disp != 0)
2769 if (ad->disp != NULL)
2770 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2771 else
2773 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2774 update_address (ad);
2776 change_p = true;
2778 if (lra_dump_file != NULL)
2780 if (! change_p)
2781 fprintf (lra_dump_file, " -- no change\n");
2782 else
2784 fprintf (lra_dump_file, " on equiv ");
2785 dump_value_slim (lra_dump_file, *ad->outer, 1);
2786 fprintf (lra_dump_file, "\n");
2789 return change_p;
2792 /* Major function to make reloads for an address in operand NOP or
2793 check its correctness (If CHECK_ONLY_P is true). The supported
2794 cases are:
2796 1) an address that existed before LRA started, at which point it
2797 must have been valid. These addresses are subject to elimination
2798 and may have become invalid due to the elimination offset being out
2799 of range.
2801 2) an address created by forcing a constant to memory
2802 (force_const_to_mem). The initial form of these addresses might
2803 not be valid, and it is this function's job to make them valid.
2805 3) a frame address formed from a register and a (possibly zero)
2806 constant offset. As above, these addresses might not be valid and
2807 this function must make them so.
2809 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2810 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2811 address. Return true for any RTL change.
2813 The function is a helper function which does not produce all
2814 transformations (when CHECK_ONLY_P is false) which can be
2815 necessary. It does just basic steps. To do all necessary
2816 transformations use function process_address. */
2817 static bool
2818 process_address_1 (int nop, bool check_only_p,
2819 rtx_insn **before, rtx_insn **after)
2821 struct address_info ad;
2822 rtx new_reg;
2823 rtx op = *curr_id->operand_loc[nop];
2824 const char *constraint = curr_static_id->operand[nop].constraint;
2825 enum constraint_num cn = lookup_constraint (constraint);
2826 bool change_p = false;
2828 if (insn_extra_address_constraint (cn))
2829 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2830 else if (MEM_P (op))
2831 decompose_mem_address (&ad, op);
2832 else if (GET_CODE (op) == SUBREG
2833 && MEM_P (SUBREG_REG (op)))
2834 decompose_mem_address (&ad, SUBREG_REG (op));
2835 else
2836 return false;
2837 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
2838 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
2839 when INDEX_REG_CLASS is a single register class. */
2840 if (ad.base_term != NULL
2841 && ad.index_term != NULL
2842 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
2843 && REG_P (*ad.base_term)
2844 && REG_P (*ad.index_term)
2845 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
2846 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
2848 std::swap (ad.base, ad.index);
2849 std::swap (ad.base_term, ad.index_term);
2851 if (! check_only_p)
2852 change_p = equiv_address_substitution (&ad);
2853 if (ad.base_term != NULL
2854 && (process_addr_reg
2855 (ad.base_term, check_only_p, before,
2856 (ad.autoinc_p
2857 && !(REG_P (*ad.base_term)
2858 && find_regno_note (curr_insn, REG_DEAD,
2859 REGNO (*ad.base_term)) != NULL_RTX)
2860 ? after : NULL),
2861 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2862 get_index_code (&ad)))))
2864 change_p = true;
2865 if (ad.base_term2 != NULL)
2866 *ad.base_term2 = *ad.base_term;
2868 if (ad.index_term != NULL
2869 && process_addr_reg (ad.index_term, check_only_p,
2870 before, NULL, INDEX_REG_CLASS))
2871 change_p = true;
2873 /* Target hooks sometimes don't treat extra-constraint addresses as
2874 legitimate address_operands, so handle them specially. */
2875 if (insn_extra_address_constraint (cn)
2876 && satisfies_address_constraint_p (&ad, cn))
2877 return change_p;
2879 if (check_only_p)
2880 return change_p;
2882 /* There are three cases where the shape of *AD.INNER may now be invalid:
2884 1) the original address was valid, but either elimination or
2885 equiv_address_substitution was applied and that made
2886 the address invalid.
2888 2) the address is an invalid symbolic address created by
2889 force_const_to_mem.
2891 3) the address is a frame address with an invalid offset.
2893 4) the address is a frame address with an invalid base.
2895 All these cases involve a non-autoinc address, so there is no
2896 point revalidating other types. */
2897 if (ad.autoinc_p || valid_address_p (&ad))
2898 return change_p;
2900 /* Any index existed before LRA started, so we can assume that the
2901 presence and shape of the index is valid. */
2902 push_to_sequence (*before);
2903 lra_assert (ad.disp == ad.disp_term);
2904 if (ad.base == NULL)
2906 if (ad.index == NULL)
2908 int code = -1;
2909 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2910 SCRATCH, SCRATCH);
2911 rtx addr = *ad.inner;
2913 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2914 #ifdef HAVE_lo_sum
2916 rtx_insn *insn;
2917 rtx_insn *last = get_last_insn ();
2919 /* addr => lo_sum (new_base, addr), case (2) above. */
2920 insn = emit_insn (gen_rtx_SET
2921 (VOIDmode, new_reg,
2922 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2923 code = recog_memoized (insn);
2924 if (code >= 0)
2926 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2927 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2929 /* Try to put lo_sum into register. */
2930 insn = emit_insn (gen_rtx_SET
2931 (VOIDmode, new_reg,
2932 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2933 code = recog_memoized (insn);
2934 if (code >= 0)
2936 *ad.inner = new_reg;
2937 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2939 *ad.inner = addr;
2940 code = -1;
2946 if (code < 0)
2947 delete_insns_since (last);
2949 #endif
2950 if (code < 0)
2952 /* addr => new_base, case (2) above. */
2953 lra_emit_move (new_reg, addr);
2954 *ad.inner = new_reg;
2957 else
2959 /* index * scale + disp => new base + index * scale,
2960 case (1) above. */
2961 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
2962 GET_CODE (*ad.index));
2964 lra_assert (INDEX_REG_CLASS != NO_REGS);
2965 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2966 lra_emit_move (new_reg, *ad.disp);
2967 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2968 new_reg, *ad.index);
2971 else if (ad.index == NULL)
2973 int regno;
2974 enum reg_class cl;
2975 rtx set;
2976 rtx_insn *insns, *last_insn;
2977 /* Try to reload base into register only if the base is invalid
2978 for the address but with valid offset, case (4) above. */
2979 start_sequence ();
2980 new_reg = base_to_reg (&ad);
2982 /* base + disp => new base, cases (1) and (3) above. */
2983 /* Another option would be to reload the displacement into an
2984 index register. However, postreload has code to optimize
2985 address reloads that have the same base and different
2986 displacements, so reloading into an index register would
2987 not necessarily be a win. */
2988 if (new_reg == NULL_RTX)
2989 new_reg = base_plus_disp_to_reg (&ad);
2990 insns = get_insns ();
2991 last_insn = get_last_insn ();
2992 /* If we generated at least two insns, try last insn source as
2993 an address. If we succeed, we generate one less insn. */
2994 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
2995 && GET_CODE (SET_SRC (set)) == PLUS
2996 && REG_P (XEXP (SET_SRC (set), 0))
2997 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
2999 *ad.inner = SET_SRC (set);
3000 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3002 *ad.base_term = XEXP (SET_SRC (set), 0);
3003 *ad.disp_term = XEXP (SET_SRC (set), 1);
3004 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3005 get_index_code (&ad));
3006 regno = REGNO (*ad.base_term);
3007 if (regno >= FIRST_PSEUDO_REGISTER
3008 && cl != lra_get_allocno_class (regno))
3009 lra_change_class (regno, cl, " Change to", true);
3010 new_reg = SET_SRC (set);
3011 delete_insns_since (PREV_INSN (last_insn));
3014 end_sequence ();
3015 emit_insn (insns);
3016 *ad.inner = new_reg;
3018 else if (ad.disp_term != NULL)
3020 /* base + scale * index + disp => new base + scale * index,
3021 case (1) above. */
3022 new_reg = base_plus_disp_to_reg (&ad);
3023 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3024 new_reg, *ad.index);
3026 else if (get_index_scale (&ad) == 1)
3028 /* The last transformation to one reg will be made in
3029 curr_insn_transform function. */
3030 end_sequence ();
3031 return false;
3033 else
3035 /* base + scale * index => base + new_reg,
3036 case (1) above.
3037 Index part of address may become invalid. For example, we
3038 changed pseudo on the equivalent memory and a subreg of the
3039 pseudo onto the memory of different mode for which the scale is
3040 prohibitted. */
3041 new_reg = index_part_to_reg (&ad);
3042 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3043 *ad.base_term, new_reg);
3045 *before = get_insns ();
3046 end_sequence ();
3047 return true;
3050 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3051 Use process_address_1 as a helper function. Return true for any
3052 RTL changes.
3054 If CHECK_ONLY_P is true, just check address correctness. Return
3055 false if the address correct. */
3056 static bool
3057 process_address (int nop, bool check_only_p,
3058 rtx_insn **before, rtx_insn **after)
3060 bool res = false;
3062 while (process_address_1 (nop, check_only_p, before, after))
3064 if (check_only_p)
3065 return true;
3066 res = true;
3068 return res;
3071 /* Emit insns to reload VALUE into a new register. VALUE is an
3072 auto-increment or auto-decrement RTX whose operand is a register or
3073 memory location; so reloading involves incrementing that location.
3074 IN is either identical to VALUE, or some cheaper place to reload
3075 value being incremented/decremented from.
3077 INC_AMOUNT is the number to increment or decrement by (always
3078 positive and ignored for POST_MODIFY/PRE_MODIFY).
3080 Return pseudo containing the result. */
3081 static rtx
3082 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3084 /* REG or MEM to be copied and incremented. */
3085 rtx incloc = XEXP (value, 0);
3086 /* Nonzero if increment after copying. */
3087 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3088 || GET_CODE (value) == POST_MODIFY);
3089 rtx_insn *last;
3090 rtx inc;
3091 rtx_insn *add_insn;
3092 int code;
3093 rtx real_in = in == value ? incloc : in;
3094 rtx result;
3095 bool plus_p = true;
3097 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3099 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3100 || GET_CODE (XEXP (value, 1)) == MINUS);
3101 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3102 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3103 inc = XEXP (XEXP (value, 1), 1);
3105 else
3107 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3108 inc_amount = -inc_amount;
3110 inc = GEN_INT (inc_amount);
3113 if (! post && REG_P (incloc))
3114 result = incloc;
3115 else
3116 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3117 "INC/DEC result");
3119 if (real_in != result)
3121 /* First copy the location to the result register. */
3122 lra_assert (REG_P (result));
3123 emit_insn (gen_move_insn (result, real_in));
3126 /* We suppose that there are insns to add/sub with the constant
3127 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3128 old reload worked with this assumption. If the assumption
3129 becomes wrong, we should use approach in function
3130 base_plus_disp_to_reg. */
3131 if (in == value)
3133 /* See if we can directly increment INCLOC. */
3134 last = get_last_insn ();
3135 add_insn = emit_insn (plus_p
3136 ? gen_add2_insn (incloc, inc)
3137 : gen_sub2_insn (incloc, inc));
3139 code = recog_memoized (add_insn);
3140 if (code >= 0)
3142 if (! post && result != incloc)
3143 emit_insn (gen_move_insn (result, incloc));
3144 return result;
3146 delete_insns_since (last);
3149 /* If couldn't do the increment directly, must increment in RESULT.
3150 The way we do this depends on whether this is pre- or
3151 post-increment. For pre-increment, copy INCLOC to the reload
3152 register, increment it there, then save back. */
3153 if (! post)
3155 if (real_in != result)
3156 emit_insn (gen_move_insn (result, real_in));
3157 if (plus_p)
3158 emit_insn (gen_add2_insn (result, inc));
3159 else
3160 emit_insn (gen_sub2_insn (result, inc));
3161 if (result != incloc)
3162 emit_insn (gen_move_insn (incloc, result));
3164 else
3166 /* Post-increment.
3168 Because this might be a jump insn or a compare, and because
3169 RESULT may not be available after the insn in an input
3170 reload, we must do the incrementing before the insn being
3171 reloaded for.
3173 We have already copied IN to RESULT. Increment the copy in
3174 RESULT, save that back, then decrement RESULT so it has
3175 the original value. */
3176 if (plus_p)
3177 emit_insn (gen_add2_insn (result, inc));
3178 else
3179 emit_insn (gen_sub2_insn (result, inc));
3180 emit_insn (gen_move_insn (incloc, result));
3181 /* Restore non-modified value for the result. We prefer this
3182 way because it does not require an additional hard
3183 register. */
3184 if (plus_p)
3186 if (CONST_INT_P (inc))
3187 emit_insn (gen_add2_insn (result,
3188 gen_int_mode (-INTVAL (inc),
3189 GET_MODE (result))));
3190 else
3191 emit_insn (gen_sub2_insn (result, inc));
3193 else
3194 emit_insn (gen_add2_insn (result, inc));
3196 return result;
3199 /* Return true if the current move insn does not need processing as we
3200 already know that it satisfies its constraints. */
3201 static bool
3202 simple_move_p (void)
3204 rtx dest, src;
3205 enum reg_class dclass, sclass;
3207 lra_assert (curr_insn_set != NULL_RTX);
3208 dest = SET_DEST (curr_insn_set);
3209 src = SET_SRC (curr_insn_set);
3210 return ((dclass = get_op_class (dest)) != NO_REGS
3211 && (sclass = get_op_class (src)) != NO_REGS
3212 /* The backend guarantees that register moves of cost 2
3213 never need reloads. */
3214 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3217 /* Swap operands NOP and NOP + 1. */
3218 static inline void
3219 swap_operands (int nop)
3221 machine_mode mode = curr_operand_mode[nop];
3222 curr_operand_mode[nop] = curr_operand_mode[nop + 1];
3223 curr_operand_mode[nop + 1] = mode;
3224 rtx x = *curr_id->operand_loc[nop];
3225 *curr_id->operand_loc[nop] = *curr_id->operand_loc[nop + 1];
3226 *curr_id->operand_loc[nop + 1] = x;
3227 /* Swap the duplicates too. */
3228 lra_update_dup (curr_id, nop);
3229 lra_update_dup (curr_id, nop + 1);
3232 /* Main entry point of the constraint code: search the body of the
3233 current insn to choose the best alternative. It is mimicking insn
3234 alternative cost calculation model of former reload pass. That is
3235 because machine descriptions were written to use this model. This
3236 model can be changed in future. Make commutative operand exchange
3237 if it is chosen.
3239 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3240 constraints. Return true if any change happened during function
3241 call.
3243 If CHECK_ONLY_P is true then don't do any transformation. Just
3244 check that the insn satisfies all constraints. If the insn does
3245 not satisfy any constraint, return true. */
3246 static bool
3247 curr_insn_transform (bool check_only_p)
3249 int i, j, k;
3250 int n_operands;
3251 int n_alternatives;
3252 int commutative;
3253 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3254 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3255 rtx_insn *before, *after;
3256 bool alt_p = false;
3257 /* Flag that the insn has been changed through a transformation. */
3258 bool change_p;
3259 bool sec_mem_p;
3260 #ifdef SECONDARY_MEMORY_NEEDED
3261 bool use_sec_mem_p;
3262 #endif
3263 int max_regno_before;
3264 int reused_alternative_num;
3266 curr_insn_set = single_set (curr_insn);
3267 if (curr_insn_set != NULL_RTX && simple_move_p ())
3268 return false;
3270 no_input_reloads_p = no_output_reloads_p = false;
3271 goal_alt_number = -1;
3272 change_p = sec_mem_p = false;
3273 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3274 reloads; neither are insns that SET cc0. Insns that use CC0 are
3275 not allowed to have any input reloads. */
3276 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3277 no_output_reloads_p = true;
3279 #ifdef HAVE_cc0
3280 if (reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3281 no_input_reloads_p = true;
3282 if (reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3283 no_output_reloads_p = true;
3284 #endif
3286 n_operands = curr_static_id->n_operands;
3287 n_alternatives = curr_static_id->n_alternatives;
3289 /* Just return "no reloads" if insn has no operands with
3290 constraints. */
3291 if (n_operands == 0 || n_alternatives == 0)
3292 return false;
3294 max_regno_before = max_reg_num ();
3296 for (i = 0; i < n_operands; i++)
3298 goal_alt_matched[i][0] = -1;
3299 goal_alt_matches[i] = -1;
3302 commutative = curr_static_id->commutative;
3304 /* Now see what we need for pseudos that didn't get hard regs or got
3305 the wrong kind of hard reg. For this, we must consider all the
3306 operands together against the register constraints. */
3308 best_losers = best_overall = INT_MAX;
3309 best_reload_sum = 0;
3311 curr_swapped = false;
3312 goal_alt_swapped = false;
3314 if (! check_only_p)
3315 /* Make equivalence substitution and memory subreg elimination
3316 before address processing because an address legitimacy can
3317 depend on memory mode. */
3318 for (i = 0; i < n_operands; i++)
3320 rtx op = *curr_id->operand_loc[i];
3321 rtx subst, old = op;
3322 bool op_change_p = false;
3324 if (GET_CODE (old) == SUBREG)
3325 old = SUBREG_REG (old);
3326 subst = get_equiv_with_elimination (old, curr_insn);
3327 if (subst != old)
3329 subst = copy_rtx (subst);
3330 lra_assert (REG_P (old));
3331 if (GET_CODE (op) == SUBREG)
3332 SUBREG_REG (op) = subst;
3333 else
3334 *curr_id->operand_loc[i] = subst;
3335 if (lra_dump_file != NULL)
3337 fprintf (lra_dump_file,
3338 "Changing pseudo %d in operand %i of insn %u on equiv ",
3339 REGNO (old), i, INSN_UID (curr_insn));
3340 dump_value_slim (lra_dump_file, subst, 1);
3341 fprintf (lra_dump_file, "\n");
3343 op_change_p = change_p = true;
3345 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3347 change_p = true;
3348 lra_update_dup (curr_id, i);
3352 /* Reload address registers and displacements. We do it before
3353 finding an alternative because of memory constraints. */
3354 before = after = NULL;
3355 for (i = 0; i < n_operands; i++)
3356 if (! curr_static_id->operand[i].is_operator
3357 && process_address (i, check_only_p, &before, &after))
3359 if (check_only_p)
3360 return true;
3361 change_p = true;
3362 lra_update_dup (curr_id, i);
3365 if (change_p)
3366 /* If we've changed the instruction then any alternative that
3367 we chose previously may no longer be valid. */
3368 lra_set_used_insn_alternative (curr_insn, -1);
3370 if (! check_only_p && curr_insn_set != NULL_RTX
3371 && check_and_process_move (&change_p, &sec_mem_p))
3372 return change_p;
3374 try_swapped:
3376 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3377 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3378 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3379 reused_alternative_num, INSN_UID (curr_insn));
3381 if (process_alt_operands (reused_alternative_num))
3382 alt_p = true;
3384 if (check_only_p)
3385 return ! alt_p || best_losers != 0;
3387 /* If insn is commutative (it's safe to exchange a certain pair of
3388 operands) then we need to try each alternative twice, the second
3389 time matching those two operands as if we had exchanged them. To
3390 do this, really exchange them in operands.
3392 If we have just tried the alternatives the second time, return
3393 operands to normal and drop through. */
3395 if (reused_alternative_num < 0 && commutative >= 0)
3397 curr_swapped = !curr_swapped;
3398 if (curr_swapped)
3400 swap_operands (commutative);
3401 goto try_swapped;
3403 else
3404 swap_operands (commutative);
3407 if (! alt_p && ! sec_mem_p)
3409 /* No alternative works with reloads?? */
3410 if (INSN_CODE (curr_insn) >= 0)
3411 fatal_insn ("unable to generate reloads for:", curr_insn);
3412 error_for_asm (curr_insn,
3413 "inconsistent operand constraints in an %<asm%>");
3414 /* Avoid further trouble with this insn. */
3415 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3416 lra_invalidate_insn_data (curr_insn);
3417 return true;
3420 /* If the best alternative is with operands 1 and 2 swapped, swap
3421 them. Update the operand numbers of any reloads already
3422 pushed. */
3424 if (goal_alt_swapped)
3426 if (lra_dump_file != NULL)
3427 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3428 INSN_UID (curr_insn));
3430 /* Swap the duplicates too. */
3431 swap_operands (commutative);
3432 change_p = true;
3435 #ifdef SECONDARY_MEMORY_NEEDED
3436 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3437 too conservatively. So we use the secondary memory only if there
3438 is no any alternative without reloads. */
3439 use_sec_mem_p = false;
3440 if (! alt_p)
3441 use_sec_mem_p = true;
3442 else if (sec_mem_p)
3444 for (i = 0; i < n_operands; i++)
3445 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3446 break;
3447 use_sec_mem_p = i < n_operands;
3450 if (use_sec_mem_p)
3452 rtx new_reg, src, dest, rld;
3453 machine_mode sec_mode, rld_mode;
3455 lra_assert (sec_mem_p);
3456 lra_assert (curr_static_id->operand[0].type == OP_OUT
3457 && curr_static_id->operand[1].type == OP_IN);
3458 dest = *curr_id->operand_loc[0];
3459 src = *curr_id->operand_loc[1];
3460 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3461 ? dest : src);
3462 rld_mode = GET_MODE (rld);
3463 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3464 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3465 #else
3466 sec_mode = rld_mode;
3467 #endif
3468 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3469 NO_REGS, "secondary");
3470 /* If the mode is changed, it should be wider. */
3471 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3472 if (sec_mode != rld_mode)
3474 /* If the target says specifically to use another mode for
3475 secondary memory moves we can not reuse the original
3476 insn. */
3477 after = emit_spill_move (false, new_reg, dest);
3478 lra_process_new_insns (curr_insn, NULL, after,
3479 "Inserting the sec. move");
3480 /* We may have non null BEFORE here (e.g. after address
3481 processing. */
3482 push_to_sequence (before);
3483 before = emit_spill_move (true, new_reg, src);
3484 emit_insn (before);
3485 before = get_insns ();
3486 end_sequence ();
3487 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3488 lra_set_insn_deleted (curr_insn);
3490 else if (dest == rld)
3492 *curr_id->operand_loc[0] = new_reg;
3493 after = emit_spill_move (false, new_reg, dest);
3494 lra_process_new_insns (curr_insn, NULL, after,
3495 "Inserting the sec. move");
3497 else
3499 *curr_id->operand_loc[1] = new_reg;
3500 /* See comments above. */
3501 push_to_sequence (before);
3502 before = emit_spill_move (true, new_reg, src);
3503 emit_insn (before);
3504 before = get_insns ();
3505 end_sequence ();
3506 lra_process_new_insns (curr_insn, before, NULL,
3507 "Inserting the sec. move");
3509 lra_update_insn_regno_info (curr_insn);
3510 return true;
3512 #endif
3514 lra_assert (goal_alt_number >= 0);
3515 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3517 if (lra_dump_file != NULL)
3519 const char *p;
3521 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3522 goal_alt_number, INSN_UID (curr_insn));
3523 for (i = 0; i < n_operands; i++)
3525 p = (curr_static_id->operand_alternative
3526 [goal_alt_number * n_operands + i].constraint);
3527 if (*p == '\0')
3528 continue;
3529 fprintf (lra_dump_file, " (%d) ", i);
3530 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3531 fputc (*p, lra_dump_file);
3533 if (INSN_CODE (curr_insn) >= 0
3534 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3535 fprintf (lra_dump_file, " {%s}", p);
3536 if (curr_id->sp_offset != 0)
3537 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3538 curr_id->sp_offset);
3539 fprintf (lra_dump_file, "\n");
3542 /* Right now, for any pair of operands I and J that are required to
3543 match, with J < I, goal_alt_matches[I] is J. Add I to
3544 goal_alt_matched[J]. */
3546 for (i = 0; i < n_operands; i++)
3547 if ((j = goal_alt_matches[i]) >= 0)
3549 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3551 /* We allow matching one output operand and several input
3552 operands. */
3553 lra_assert (k == 0
3554 || (curr_static_id->operand[j].type == OP_OUT
3555 && curr_static_id->operand[i].type == OP_IN
3556 && (curr_static_id->operand
3557 [goal_alt_matched[j][0]].type == OP_IN)));
3558 goal_alt_matched[j][k] = i;
3559 goal_alt_matched[j][k + 1] = -1;
3562 for (i = 0; i < n_operands; i++)
3563 goal_alt_win[i] |= goal_alt_match_win[i];
3565 /* Any constants that aren't allowed and can't be reloaded into
3566 registers are here changed into memory references. */
3567 for (i = 0; i < n_operands; i++)
3568 if (goal_alt_win[i])
3570 int regno;
3571 enum reg_class new_class;
3572 rtx reg = *curr_id->operand_loc[i];
3574 if (GET_CODE (reg) == SUBREG)
3575 reg = SUBREG_REG (reg);
3577 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3579 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3581 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3583 lra_assert (ok_p);
3584 lra_change_class (regno, new_class, " Change to", true);
3588 else
3590 const char *constraint;
3591 char c;
3592 rtx op = *curr_id->operand_loc[i];
3593 rtx subreg = NULL_RTX;
3594 machine_mode mode = curr_operand_mode[i];
3596 if (GET_CODE (op) == SUBREG)
3598 subreg = op;
3599 op = SUBREG_REG (op);
3600 mode = GET_MODE (op);
3603 if (CONST_POOL_OK_P (mode, op)
3604 && ((targetm.preferred_reload_class
3605 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3606 || no_input_reloads_p))
3608 rtx tem = force_const_mem (mode, op);
3610 change_p = true;
3611 if (subreg != NULL_RTX)
3612 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3614 *curr_id->operand_loc[i] = tem;
3615 lra_update_dup (curr_id, i);
3616 process_address (i, false, &before, &after);
3618 /* If the alternative accepts constant pool refs directly
3619 there will be no reload needed at all. */
3620 if (subreg != NULL_RTX)
3621 continue;
3622 /* Skip alternatives before the one requested. */
3623 constraint = (curr_static_id->operand_alternative
3624 [goal_alt_number * n_operands + i].constraint);
3625 for (;
3626 (c = *constraint) && c != ',' && c != '#';
3627 constraint += CONSTRAINT_LEN (c, constraint))
3629 enum constraint_num cn = lookup_constraint (constraint);
3630 if (insn_extra_memory_constraint (cn)
3631 && satisfies_memory_constraint_p (tem, cn))
3632 break;
3634 if (c == '\0' || c == ',' || c == '#')
3635 continue;
3637 goal_alt_win[i] = true;
3641 for (i = 0; i < n_operands; i++)
3643 int regno;
3644 bool optional_p = false;
3645 rtx old, new_reg;
3646 rtx op = *curr_id->operand_loc[i];
3648 if (goal_alt_win[i])
3650 if (goal_alt[i] == NO_REGS
3651 && REG_P (op)
3652 /* When we assign NO_REGS it means that we will not
3653 assign a hard register to the scratch pseudo by
3654 assigment pass and the scratch pseudo will be
3655 spilled. Spilled scratch pseudos are transformed
3656 back to scratches at the LRA end. */
3657 && lra_former_scratch_operand_p (curr_insn, i))
3659 int regno = REGNO (op);
3660 lra_change_class (regno, NO_REGS, " Change to", true);
3661 if (lra_get_regno_hard_regno (regno) >= 0)
3662 /* We don't have to mark all insn affected by the
3663 spilled pseudo as there is only one such insn, the
3664 current one. */
3665 reg_renumber[regno] = -1;
3667 /* We can do an optional reload. If the pseudo got a hard
3668 reg, we might improve the code through inheritance. If
3669 it does not get a hard register we coalesce memory/memory
3670 moves later. Ignore move insns to avoid cycling. */
3671 if (! lra_simple_p
3672 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3673 && goal_alt[i] != NO_REGS && REG_P (op)
3674 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3675 && regno < new_regno_start
3676 && ! lra_former_scratch_p (regno)
3677 && reg_renumber[regno] < 0
3678 && (curr_insn_set == NULL_RTX
3679 || !((REG_P (SET_SRC (curr_insn_set))
3680 || MEM_P (SET_SRC (curr_insn_set))
3681 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3682 && (REG_P (SET_DEST (curr_insn_set))
3683 || MEM_P (SET_DEST (curr_insn_set))
3684 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3685 optional_p = true;
3686 else
3687 continue;
3690 /* Operands that match previous ones have already been handled. */
3691 if (goal_alt_matches[i] >= 0)
3692 continue;
3694 /* We should not have an operand with a non-offsettable address
3695 appearing where an offsettable address will do. It also may
3696 be a case when the address should be special in other words
3697 not a general one (e.g. it needs no index reg). */
3698 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3700 enum reg_class rclass;
3701 rtx *loc = &XEXP (op, 0);
3702 enum rtx_code code = GET_CODE (*loc);
3704 push_to_sequence (before);
3705 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3706 MEM, SCRATCH);
3707 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3708 new_reg = emit_inc (rclass, *loc, *loc,
3709 /* This value does not matter for MODIFY. */
3710 GET_MODE_SIZE (GET_MODE (op)));
3711 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3712 "offsetable address", &new_reg))
3713 lra_emit_move (new_reg, *loc);
3714 before = get_insns ();
3715 end_sequence ();
3716 *loc = new_reg;
3717 lra_update_dup (curr_id, i);
3719 else if (goal_alt_matched[i][0] == -1)
3721 machine_mode mode;
3722 rtx reg, *loc;
3723 int hard_regno, byte;
3724 enum op_type type = curr_static_id->operand[i].type;
3726 loc = curr_id->operand_loc[i];
3727 mode = curr_operand_mode[i];
3728 if (GET_CODE (*loc) == SUBREG)
3730 reg = SUBREG_REG (*loc);
3731 byte = SUBREG_BYTE (*loc);
3732 if (REG_P (reg)
3733 /* Strict_low_part requires reload the register not
3734 the sub-register. */
3735 && (curr_static_id->operand[i].strict_low
3736 || (GET_MODE_SIZE (mode)
3737 <= GET_MODE_SIZE (GET_MODE (reg))
3738 && (hard_regno
3739 = get_try_hard_regno (REGNO (reg))) >= 0
3740 && (simplify_subreg_regno
3741 (hard_regno,
3742 GET_MODE (reg), byte, mode) < 0)
3743 && (goal_alt[i] == NO_REGS
3744 || (simplify_subreg_regno
3745 (ira_class_hard_regs[goal_alt[i]][0],
3746 GET_MODE (reg), byte, mode) >= 0)))))
3748 loc = &SUBREG_REG (*loc);
3749 mode = GET_MODE (*loc);
3752 old = *loc;
3753 if (get_reload_reg (type, mode, old, goal_alt[i],
3754 loc != curr_id->operand_loc[i], "", &new_reg)
3755 && type != OP_OUT)
3757 push_to_sequence (before);
3758 lra_emit_move (new_reg, old);
3759 before = get_insns ();
3760 end_sequence ();
3762 *loc = new_reg;
3763 if (type != OP_IN
3764 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3766 start_sequence ();
3767 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3768 emit_insn (after);
3769 after = get_insns ();
3770 end_sequence ();
3771 *loc = new_reg;
3773 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3774 if (goal_alt_dont_inherit_ops[j] == i)
3776 lra_set_regno_unique_value (REGNO (new_reg));
3777 break;
3779 lra_update_dup (curr_id, i);
3781 else if (curr_static_id->operand[i].type == OP_IN
3782 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3783 == OP_OUT))
3785 /* generate reloads for input and matched outputs. */
3786 match_inputs[0] = i;
3787 match_inputs[1] = -1;
3788 match_reload (goal_alt_matched[i][0], match_inputs,
3789 goal_alt[i], &before, &after);
3791 else if (curr_static_id->operand[i].type == OP_OUT
3792 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3793 == OP_IN))
3794 /* Generate reloads for output and matched inputs. */
3795 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3796 else if (curr_static_id->operand[i].type == OP_IN
3797 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3798 == OP_IN))
3800 /* Generate reloads for matched inputs. */
3801 match_inputs[0] = i;
3802 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3803 match_inputs[j + 1] = k;
3804 match_inputs[j + 1] = -1;
3805 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3807 else
3808 /* We must generate code in any case when function
3809 process_alt_operands decides that it is possible. */
3810 gcc_unreachable ();
3811 if (optional_p)
3813 lra_assert (REG_P (op));
3814 regno = REGNO (op);
3815 op = *curr_id->operand_loc[i]; /* Substitution. */
3816 if (GET_CODE (op) == SUBREG)
3817 op = SUBREG_REG (op);
3818 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3819 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3820 lra_reg_info[REGNO (op)].restore_regno = regno;
3821 if (lra_dump_file != NULL)
3822 fprintf (lra_dump_file,
3823 " Making reload reg %d for reg %d optional\n",
3824 REGNO (op), regno);
3827 if (before != NULL_RTX || after != NULL_RTX
3828 || max_regno_before != max_reg_num ())
3829 change_p = true;
3830 if (change_p)
3832 lra_update_operator_dups (curr_id);
3833 /* Something changes -- process the insn. */
3834 lra_update_insn_regno_info (curr_insn);
3836 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3837 return change_p;
3840 /* Return true if INSN satisfies all constraints. In other words, no
3841 reload insns are needed. */
3842 bool
3843 lra_constrain_insn (rtx_insn *insn)
3845 int saved_new_regno_start = new_regno_start;
3846 int saved_new_insn_uid_start = new_insn_uid_start;
3847 bool change_p;
3849 curr_insn = insn;
3850 curr_id = lra_get_insn_recog_data (curr_insn);
3851 curr_static_id = curr_id->insn_static_data;
3852 new_insn_uid_start = get_max_uid ();
3853 new_regno_start = max_reg_num ();
3854 change_p = curr_insn_transform (true);
3855 new_regno_start = saved_new_regno_start;
3856 new_insn_uid_start = saved_new_insn_uid_start;
3857 return ! change_p;
3860 /* Return true if X is in LIST. */
3861 static bool
3862 in_list_p (rtx x, rtx list)
3864 for (; list != NULL_RTX; list = XEXP (list, 1))
3865 if (XEXP (list, 0) == x)
3866 return true;
3867 return false;
3870 /* Return true if X contains an allocatable hard register (if
3871 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3872 static bool
3873 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3875 int i, j;
3876 const char *fmt;
3877 enum rtx_code code;
3879 code = GET_CODE (x);
3880 if (REG_P (x))
3882 int regno = REGNO (x);
3883 HARD_REG_SET alloc_regs;
3885 if (hard_reg_p)
3887 if (regno >= FIRST_PSEUDO_REGISTER)
3888 regno = lra_get_regno_hard_regno (regno);
3889 if (regno < 0)
3890 return false;
3891 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3892 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3894 else
3896 if (regno < FIRST_PSEUDO_REGISTER)
3897 return false;
3898 if (! spilled_p)
3899 return true;
3900 return lra_get_regno_hard_regno (regno) < 0;
3903 fmt = GET_RTX_FORMAT (code);
3904 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3906 if (fmt[i] == 'e')
3908 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3909 return true;
3911 else if (fmt[i] == 'E')
3913 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3914 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3915 return true;
3918 return false;
3921 /* Return true if X contains a symbol reg. */
3922 static bool
3923 contains_symbol_ref_p (rtx x)
3925 int i, j;
3926 const char *fmt;
3927 enum rtx_code code;
3929 code = GET_CODE (x);
3930 if (code == SYMBOL_REF)
3931 return true;
3932 fmt = GET_RTX_FORMAT (code);
3933 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3935 if (fmt[i] == 'e')
3937 if (contains_symbol_ref_p (XEXP (x, i)))
3938 return true;
3940 else if (fmt[i] == 'E')
3942 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3943 if (contains_symbol_ref_p (XVECEXP (x, i, j)))
3944 return true;
3947 return false;
3950 /* Process all regs in location *LOC and change them on equivalent
3951 substitution. Return true if any change was done. */
3952 static bool
3953 loc_equivalence_change_p (rtx *loc)
3955 rtx subst, reg, x = *loc;
3956 bool result = false;
3957 enum rtx_code code = GET_CODE (x);
3958 const char *fmt;
3959 int i, j;
3961 if (code == SUBREG)
3963 reg = SUBREG_REG (x);
3964 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
3965 && GET_MODE (subst) == VOIDmode)
3967 /* We cannot reload debug location. Simplify subreg here
3968 while we know the inner mode. */
3969 *loc = simplify_gen_subreg (GET_MODE (x), subst,
3970 GET_MODE (reg), SUBREG_BYTE (x));
3971 return true;
3974 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
3976 *loc = subst;
3977 return true;
3980 /* Scan all the operand sub-expressions. */
3981 fmt = GET_RTX_FORMAT (code);
3982 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3984 if (fmt[i] == 'e')
3985 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
3986 else if (fmt[i] == 'E')
3987 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3988 result
3989 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
3991 return result;
3994 /* Similar to loc_equivalence_change_p, but for use as
3995 simplify_replace_fn_rtx callback. DATA is insn for which the
3996 elimination is done. If it null we don't do the elimination. */
3997 static rtx
3998 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4000 if (!REG_P (loc))
4001 return NULL_RTX;
4003 rtx subst = (data == NULL
4004 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4005 if (subst != loc)
4006 return subst;
4008 return NULL_RTX;
4011 /* Maximum number of generated reload insns per an insn. It is for
4012 preventing this pass cycling in a bug case. */
4013 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4015 /* The current iteration number of this LRA pass. */
4016 int lra_constraint_iter;
4018 /* True if we substituted equiv which needs checking register
4019 allocation correctness because the equivalent value contains
4020 allocatable hard registers or when we restore multi-register
4021 pseudo. */
4022 bool lra_risky_transformations_p;
4024 /* Return true if REGNO is referenced in more than one block. */
4025 static bool
4026 multi_block_pseudo_p (int regno)
4028 basic_block bb = NULL;
4029 unsigned int uid;
4030 bitmap_iterator bi;
4032 if (regno < FIRST_PSEUDO_REGISTER)
4033 return false;
4035 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4036 if (bb == NULL)
4037 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4038 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4039 return true;
4040 return false;
4043 /* Return true if LIST contains a deleted insn. */
4044 static bool
4045 contains_deleted_insn_p (rtx_insn_list *list)
4047 for (; list != NULL_RTX; list = list->next ())
4048 if (NOTE_P (list->insn ())
4049 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4050 return true;
4051 return false;
4054 /* Return true if X contains a pseudo dying in INSN. */
4055 static bool
4056 dead_pseudo_p (rtx x, rtx insn)
4058 int i, j;
4059 const char *fmt;
4060 enum rtx_code code;
4062 if (REG_P (x))
4063 return (insn != NULL_RTX
4064 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4065 code = GET_CODE (x);
4066 fmt = GET_RTX_FORMAT (code);
4067 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4069 if (fmt[i] == 'e')
4071 if (dead_pseudo_p (XEXP (x, i), insn))
4072 return true;
4074 else if (fmt[i] == 'E')
4076 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4077 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4078 return true;
4081 return false;
4084 /* Return true if INSN contains a dying pseudo in INSN right hand
4085 side. */
4086 static bool
4087 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4089 rtx set = single_set (insn);
4091 gcc_assert (set != NULL);
4092 return dead_pseudo_p (SET_SRC (set), insn);
4095 /* Return true if any init insn of REGNO contains a dying pseudo in
4096 insn right hand side. */
4097 static bool
4098 init_insn_rhs_dead_pseudo_p (int regno)
4100 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4102 if (insns == NULL)
4103 return false;
4104 for (; insns != NULL_RTX; insns = insns->next ())
4105 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4106 return true;
4107 return false;
4110 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4111 reverse only if we have one init insn with given REGNO as a
4112 source. */
4113 static bool
4114 reverse_equiv_p (int regno)
4116 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4117 rtx set;
4119 if (insns == NULL)
4120 return false;
4121 if (! INSN_P (insns->insn ())
4122 || insns->next () != NULL)
4123 return false;
4124 if ((set = single_set (insns->insn ())) == NULL_RTX)
4125 return false;
4126 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4129 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4130 call this function only for non-reverse equivalence. */
4131 static bool
4132 contains_reloaded_insn_p (int regno)
4134 rtx set;
4135 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4137 for (; list != NULL; list = list->next ())
4138 if ((set = single_set (list->insn ())) == NULL_RTX
4139 || ! REG_P (SET_DEST (set))
4140 || (int) REGNO (SET_DEST (set)) != regno)
4141 return true;
4142 return false;
4145 /* Entry function of LRA constraint pass. Return true if the
4146 constraint pass did change the code. */
4147 bool
4148 lra_constraints (bool first_p)
4150 bool changed_p;
4151 int i, hard_regno, new_insns_num;
4152 unsigned int min_len, new_min_len, uid;
4153 rtx set, x, reg, dest_reg;
4154 basic_block last_bb;
4155 bitmap_head equiv_insn_bitmap;
4156 bitmap_iterator bi;
4158 lra_constraint_iter++;
4159 if (lra_dump_file != NULL)
4160 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4161 lra_constraint_iter);
4162 changed_p = false;
4163 if (pic_offset_table_rtx
4164 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4165 lra_risky_transformations_p = true;
4166 else
4167 lra_risky_transformations_p = false;
4168 new_insn_uid_start = get_max_uid ();
4169 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4170 /* Mark used hard regs for target stack size calulations. */
4171 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4172 if (lra_reg_info[i].nrefs != 0
4173 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4175 int j, nregs;
4177 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4178 for (j = 0; j < nregs; j++)
4179 df_set_regs_ever_live (hard_regno + j, true);
4181 /* Do elimination before the equivalence processing as we can spill
4182 some pseudos during elimination. */
4183 lra_eliminate (false, first_p);
4184 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4185 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4186 if (lra_reg_info[i].nrefs != 0)
4188 ira_reg_equiv[i].profitable_p = true;
4189 reg = regno_reg_rtx[i];
4190 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4192 bool pseudo_p = contains_reg_p (x, false, false);
4194 /* After RTL transformation, we can not guarantee that
4195 pseudo in the substitution was not reloaded which might
4196 make equivalence invalid. For example, in reverse
4197 equiv of p0
4199 p0 <- ...
4201 equiv_mem <- p0
4203 the memory address register was reloaded before the 2nd
4204 insn. */
4205 if ((! first_p && pseudo_p)
4206 /* We don't use DF for compilation speed sake. So it
4207 is problematic to update live info when we use an
4208 equivalence containing pseudos in more than one
4209 BB. */
4210 || (pseudo_p && multi_block_pseudo_p (i))
4211 /* If an init insn was deleted for some reason, cancel
4212 the equiv. We could update the equiv insns after
4213 transformations including an equiv insn deletion
4214 but it is not worthy as such cases are extremely
4215 rare. */
4216 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4217 /* If it is not a reverse equivalence, we check that a
4218 pseudo in rhs of the init insn is not dying in the
4219 insn. Otherwise, the live info at the beginning of
4220 the corresponding BB might be wrong after we
4221 removed the insn. When the equiv can be a
4222 constant, the right hand side of the init insn can
4223 be a pseudo. */
4224 || (! reverse_equiv_p (i)
4225 && (init_insn_rhs_dead_pseudo_p (i)
4226 /* If we reloaded the pseudo in an equivalence
4227 init insn, we can not remove the equiv init
4228 insns and the init insns might write into
4229 const memory in this case. */
4230 || contains_reloaded_insn_p (i)))
4231 /* Prevent access beyond equivalent memory for
4232 paradoxical subregs. */
4233 || (MEM_P (x)
4234 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4235 > GET_MODE_SIZE (GET_MODE (x))))
4236 || (pic_offset_table_rtx
4237 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4238 && (targetm.preferred_reload_class
4239 (x, lra_get_allocno_class (i)) == NO_REGS))
4240 || contains_symbol_ref_p (x))))
4241 ira_reg_equiv[i].defined_p = false;
4242 if (contains_reg_p (x, false, true))
4243 ira_reg_equiv[i].profitable_p = false;
4244 if (get_equiv (reg) != reg)
4245 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4248 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4249 update_equiv (i);
4250 /* We should add all insns containing pseudos which should be
4251 substituted by their equivalences. */
4252 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4253 lra_push_insn_by_uid (uid);
4254 min_len = lra_insn_stack_length ();
4255 new_insns_num = 0;
4256 last_bb = NULL;
4257 changed_p = false;
4258 while ((new_min_len = lra_insn_stack_length ()) != 0)
4260 curr_insn = lra_pop_insn ();
4261 --new_min_len;
4262 curr_bb = BLOCK_FOR_INSN (curr_insn);
4263 if (curr_bb != last_bb)
4265 last_bb = curr_bb;
4266 bb_reload_num = lra_curr_reload_num;
4268 if (min_len > new_min_len)
4270 min_len = new_min_len;
4271 new_insns_num = 0;
4273 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4274 internal_error
4275 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4276 MAX_RELOAD_INSNS_NUMBER);
4277 new_insns_num++;
4278 if (DEBUG_INSN_P (curr_insn))
4280 /* We need to check equivalence in debug insn and change
4281 pseudo to the equivalent value if necessary. */
4282 curr_id = lra_get_insn_recog_data (curr_insn);
4283 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4285 rtx old = *curr_id->operand_loc[0];
4286 *curr_id->operand_loc[0]
4287 = simplify_replace_fn_rtx (old, NULL_RTX,
4288 loc_equivalence_callback, curr_insn);
4289 if (old != *curr_id->operand_loc[0])
4291 lra_update_insn_regno_info (curr_insn);
4292 changed_p = true;
4296 else if (INSN_P (curr_insn))
4298 if ((set = single_set (curr_insn)) != NULL_RTX)
4300 dest_reg = SET_DEST (set);
4301 /* The equivalence pseudo could be set up as SUBREG in a
4302 case when it is a call restore insn in a mode
4303 different from the pseudo mode. */
4304 if (GET_CODE (dest_reg) == SUBREG)
4305 dest_reg = SUBREG_REG (dest_reg);
4306 if ((REG_P (dest_reg)
4307 && (x = get_equiv (dest_reg)) != dest_reg
4308 /* Remove insns which set up a pseudo whose value
4309 can not be changed. Such insns might be not in
4310 init_insns because we don't update equiv data
4311 during insn transformations.
4313 As an example, let suppose that a pseudo got
4314 hard register and on the 1st pass was not
4315 changed to equivalent constant. We generate an
4316 additional insn setting up the pseudo because of
4317 secondary memory movement. Then the pseudo is
4318 spilled and we use the equiv constant. In this
4319 case we should remove the additional insn and
4320 this insn is not init_insns list. */
4321 && (! MEM_P (x) || MEM_READONLY_P (x)
4322 /* Check that this is actually an insn setting
4323 up the equivalence. */
4324 || in_list_p (curr_insn,
4325 ira_reg_equiv
4326 [REGNO (dest_reg)].init_insns)))
4327 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4328 && in_list_p (curr_insn,
4329 ira_reg_equiv
4330 [REGNO (SET_SRC (set))].init_insns)))
4332 /* This is equiv init insn of pseudo which did not get a
4333 hard register -- remove the insn. */
4334 if (lra_dump_file != NULL)
4336 fprintf (lra_dump_file,
4337 " Removing equiv init insn %i (freq=%d)\n",
4338 INSN_UID (curr_insn),
4339 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4340 dump_insn_slim (lra_dump_file, curr_insn);
4342 if (contains_reg_p (x, true, false))
4343 lra_risky_transformations_p = true;
4344 lra_set_insn_deleted (curr_insn);
4345 continue;
4348 curr_id = lra_get_insn_recog_data (curr_insn);
4349 curr_static_id = curr_id->insn_static_data;
4350 init_curr_insn_input_reloads ();
4351 init_curr_operand_mode ();
4352 if (curr_insn_transform (false))
4353 changed_p = true;
4354 /* Check non-transformed insns too for equiv change as USE
4355 or CLOBBER don't need reloads but can contain pseudos
4356 being changed on their equivalences. */
4357 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4358 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4360 lra_update_insn_regno_info (curr_insn);
4361 changed_p = true;
4365 bitmap_clear (&equiv_insn_bitmap);
4366 /* If we used a new hard regno, changed_p should be true because the
4367 hard reg is assigned to a new pseudo. */
4368 #ifdef ENABLE_CHECKING
4369 if (! changed_p)
4371 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4372 if (lra_reg_info[i].nrefs != 0
4373 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4375 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4377 for (j = 0; j < nregs; j++)
4378 lra_assert (df_regs_ever_live_p (hard_regno + j));
4381 #endif
4382 return changed_p;
4385 /* Initiate the LRA constraint pass. It is done once per
4386 function. */
4387 void
4388 lra_constraints_init (void)
4392 /* Finalize the LRA constraint pass. It is done once per
4393 function. */
4394 void
4395 lra_constraints_finish (void)
4401 /* This page contains code to do inheritance/split
4402 transformations. */
4404 /* Number of reloads passed so far in current EBB. */
4405 static int reloads_num;
4407 /* Number of calls passed so far in current EBB. */
4408 static int calls_num;
4410 /* Current reload pseudo check for validity of elements in
4411 USAGE_INSNS. */
4412 static int curr_usage_insns_check;
4414 /* Info about last usage of registers in EBB to do inheritance/split
4415 transformation. Inheritance transformation is done from a spilled
4416 pseudo and split transformations from a hard register or a pseudo
4417 assigned to a hard register. */
4418 struct usage_insns
4420 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4421 value INSNS is valid. The insns is chain of optional debug insns
4422 and a finishing non-debug insn using the corresponding reg. The
4423 value is also used to mark the registers which are set up in the
4424 current insn. The negated insn uid is used for this. */
4425 int check;
4426 /* Value of global reloads_num at the last insn in INSNS. */
4427 int reloads_num;
4428 /* Value of global reloads_nums at the last insn in INSNS. */
4429 int calls_num;
4430 /* It can be true only for splitting. And it means that the restore
4431 insn should be put after insn given by the following member. */
4432 bool after_p;
4433 /* Next insns in the current EBB which use the original reg and the
4434 original reg value is not changed between the current insn and
4435 the next insns. In order words, e.g. for inheritance, if we need
4436 to use the original reg value again in the next insns we can try
4437 to use the value in a hard register from a reload insn of the
4438 current insn. */
4439 rtx insns;
4442 /* Map: regno -> corresponding pseudo usage insns. */
4443 static struct usage_insns *usage_insns;
4445 static void
4446 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4448 usage_insns[regno].check = curr_usage_insns_check;
4449 usage_insns[regno].insns = insn;
4450 usage_insns[regno].reloads_num = reloads_num;
4451 usage_insns[regno].calls_num = calls_num;
4452 usage_insns[regno].after_p = after_p;
4455 /* The function is used to form list REGNO usages which consists of
4456 optional debug insns finished by a non-debug insn using REGNO.
4457 RELOADS_NUM is current number of reload insns processed so far. */
4458 static void
4459 add_next_usage_insn (int regno, rtx insn, int reloads_num)
4461 rtx next_usage_insns;
4463 if (usage_insns[regno].check == curr_usage_insns_check
4464 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4465 && DEBUG_INSN_P (insn))
4467 /* Check that we did not add the debug insn yet. */
4468 if (next_usage_insns != insn
4469 && (GET_CODE (next_usage_insns) != INSN_LIST
4470 || XEXP (next_usage_insns, 0) != insn))
4471 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4472 next_usage_insns);
4474 else if (NONDEBUG_INSN_P (insn))
4475 setup_next_usage_insn (regno, insn, reloads_num, false);
4476 else
4477 usage_insns[regno].check = 0;
4480 /* Return first non-debug insn in list USAGE_INSNS. */
4481 static rtx_insn *
4482 skip_usage_debug_insns (rtx usage_insns)
4484 rtx insn;
4486 /* Skip debug insns. */
4487 for (insn = usage_insns;
4488 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4489 insn = XEXP (insn, 1))
4491 return safe_as_a <rtx_insn *> (insn);
4494 /* Return true if we need secondary memory moves for insn in
4495 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4496 into the insn. */
4497 static bool
4498 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4499 rtx usage_insns ATTRIBUTE_UNUSED)
4501 #ifndef SECONDARY_MEMORY_NEEDED
4502 return false;
4503 #else
4504 rtx_insn *insn;
4505 rtx set, dest;
4506 enum reg_class cl;
4508 if (inher_cl == ALL_REGS
4509 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4510 return false;
4511 lra_assert (INSN_P (insn));
4512 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4513 return false;
4514 dest = SET_DEST (set);
4515 if (! REG_P (dest))
4516 return false;
4517 lra_assert (inher_cl != NO_REGS);
4518 cl = get_reg_class (REGNO (dest));
4519 return (cl != NO_REGS && cl != ALL_REGS
4520 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4521 #endif
4524 /* Registers involved in inheritance/split in the current EBB
4525 (inheritance/split pseudos and original registers). */
4526 static bitmap_head check_only_regs;
4528 /* Do inheritance transformations for insn INSN, which defines (if
4529 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4530 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4531 form as the "insns" field of usage_insns. Return true if we
4532 succeed in such transformation.
4534 The transformations look like:
4536 p <- ... i <- ...
4537 ... p <- i (new insn)
4538 ... =>
4539 <- ... p ... <- ... i ...
4541 ... i <- p (new insn)
4542 <- ... p ... <- ... i ...
4543 ... =>
4544 <- ... p ... <- ... i ...
4545 where p is a spilled original pseudo and i is a new inheritance pseudo.
4548 The inheritance pseudo has the smallest class of two classes CL and
4549 class of ORIGINAL REGNO. */
4550 static bool
4551 inherit_reload_reg (bool def_p, int original_regno,
4552 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4554 if (optimize_function_for_size_p (cfun))
4555 return false;
4557 enum reg_class rclass = lra_get_allocno_class (original_regno);
4558 rtx original_reg = regno_reg_rtx[original_regno];
4559 rtx new_reg, usage_insn;
4560 rtx_insn *new_insns;
4562 lra_assert (! usage_insns[original_regno].after_p);
4563 if (lra_dump_file != NULL)
4564 fprintf (lra_dump_file,
4565 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4566 if (! ira_reg_classes_intersect_p[cl][rclass])
4568 if (lra_dump_file != NULL)
4570 fprintf (lra_dump_file,
4571 " Rejecting inheritance for %d "
4572 "because of disjoint classes %s and %s\n",
4573 original_regno, reg_class_names[cl],
4574 reg_class_names[rclass]);
4575 fprintf (lra_dump_file,
4576 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4578 return false;
4580 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4581 /* We don't use a subset of two classes because it can be
4582 NO_REGS. This transformation is still profitable in most
4583 cases even if the classes are not intersected as register
4584 move is probably cheaper than a memory load. */
4585 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4587 if (lra_dump_file != NULL)
4588 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4589 reg_class_names[cl], reg_class_names[rclass]);
4591 rclass = cl;
4593 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4595 /* Reject inheritance resulting in secondary memory moves.
4596 Otherwise, there is a danger in LRA cycling. Also such
4597 transformation will be unprofitable. */
4598 if (lra_dump_file != NULL)
4600 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4601 rtx set = single_set (insn);
4603 lra_assert (set != NULL_RTX);
4605 rtx dest = SET_DEST (set);
4607 lra_assert (REG_P (dest));
4608 fprintf (lra_dump_file,
4609 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4610 "as secondary mem is needed\n",
4611 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4612 original_regno, reg_class_names[rclass]);
4613 fprintf (lra_dump_file,
4614 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4616 return false;
4618 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4619 rclass, "inheritance");
4620 start_sequence ();
4621 if (def_p)
4622 lra_emit_move (original_reg, new_reg);
4623 else
4624 lra_emit_move (new_reg, original_reg);
4625 new_insns = get_insns ();
4626 end_sequence ();
4627 if (NEXT_INSN (new_insns) != NULL_RTX)
4629 if (lra_dump_file != NULL)
4631 fprintf (lra_dump_file,
4632 " Rejecting inheritance %d->%d "
4633 "as it results in 2 or more insns:\n",
4634 original_regno, REGNO (new_reg));
4635 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4636 fprintf (lra_dump_file,
4637 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4639 return false;
4641 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg);
4642 lra_update_insn_regno_info (insn);
4643 if (! def_p)
4644 /* We now have a new usage insn for original regno. */
4645 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4646 if (lra_dump_file != NULL)
4647 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4648 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4649 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4650 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4651 bitmap_set_bit (&check_only_regs, original_regno);
4652 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4653 if (def_p)
4654 lra_process_new_insns (insn, NULL, new_insns,
4655 "Add original<-inheritance");
4656 else
4657 lra_process_new_insns (insn, new_insns, NULL,
4658 "Add inheritance<-original");
4659 while (next_usage_insns != NULL_RTX)
4661 if (GET_CODE (next_usage_insns) != INSN_LIST)
4663 usage_insn = next_usage_insns;
4664 lra_assert (NONDEBUG_INSN_P (usage_insn));
4665 next_usage_insns = NULL;
4667 else
4669 usage_insn = XEXP (next_usage_insns, 0);
4670 lra_assert (DEBUG_INSN_P (usage_insn));
4671 next_usage_insns = XEXP (next_usage_insns, 1);
4673 lra_substitute_pseudo (&usage_insn, original_regno, new_reg);
4674 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4675 if (lra_dump_file != NULL)
4677 fprintf (lra_dump_file,
4678 " Inheritance reuse change %d->%d (bb%d):\n",
4679 original_regno, REGNO (new_reg),
4680 BLOCK_FOR_INSN (usage_insn)->index);
4681 dump_insn_slim (lra_dump_file, usage_insn);
4684 if (lra_dump_file != NULL)
4685 fprintf (lra_dump_file,
4686 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4687 return true;
4690 /* Return true if we need a caller save/restore for pseudo REGNO which
4691 was assigned to a hard register. */
4692 static inline bool
4693 need_for_call_save_p (int regno)
4695 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4696 return (usage_insns[regno].calls_num < calls_num
4697 && (overlaps_hard_reg_set_p
4698 ((flag_ipa_ra &&
4699 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4700 ? lra_reg_info[regno].actual_call_used_reg_set
4701 : call_used_reg_set,
4702 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4703 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4704 PSEUDO_REGNO_MODE (regno))));
4707 /* Global registers occurring in the current EBB. */
4708 static bitmap_head ebb_global_regs;
4710 /* Return true if we need a split for hard register REGNO or pseudo
4711 REGNO which was assigned to a hard register.
4712 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4713 used for reloads since the EBB end. It is an approximation of the
4714 used hard registers in the split range. The exact value would
4715 require expensive calculations. If we were aggressive with
4716 splitting because of the approximation, the split pseudo will save
4717 the same hard register assignment and will be removed in the undo
4718 pass. We still need the approximation because too aggressive
4719 splitting would result in too inaccurate cost calculation in the
4720 assignment pass because of too many generated moves which will be
4721 probably removed in the undo pass. */
4722 static inline bool
4723 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4725 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4727 lra_assert (hard_regno >= 0);
4728 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4729 /* Don't split eliminable hard registers, otherwise we can
4730 split hard registers like hard frame pointer, which
4731 lives on BB start/end according to DF-infrastructure,
4732 when there is a pseudo assigned to the register and
4733 living in the same BB. */
4734 && (regno >= FIRST_PSEUDO_REGISTER
4735 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4736 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4737 /* Don't split call clobbered hard regs living through
4738 calls, otherwise we might have a check problem in the
4739 assign sub-pass as in the most cases (exception is a
4740 situation when lra_risky_transformations_p value is
4741 true) the assign pass assumes that all pseudos living
4742 through calls are assigned to call saved hard regs. */
4743 && (regno >= FIRST_PSEUDO_REGISTER
4744 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4745 || usage_insns[regno].calls_num == calls_num)
4746 /* We need at least 2 reloads to make pseudo splitting
4747 profitable. We should provide hard regno splitting in
4748 any case to solve 1st insn scheduling problem when
4749 moving hard register definition up might result in
4750 impossibility to find hard register for reload pseudo of
4751 small register class. */
4752 && (usage_insns[regno].reloads_num
4753 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4754 && (regno < FIRST_PSEUDO_REGISTER
4755 /* For short living pseudos, spilling + inheritance can
4756 be considered a substitution for splitting.
4757 Therefore we do not splitting for local pseudos. It
4758 decreases also aggressiveness of splitting. The
4759 minimal number of references is chosen taking into
4760 account that for 2 references splitting has no sense
4761 as we can just spill the pseudo. */
4762 || (regno >= FIRST_PSEUDO_REGISTER
4763 && lra_reg_info[regno].nrefs > 3
4764 && bitmap_bit_p (&ebb_global_regs, regno))))
4765 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4768 /* Return class for the split pseudo created from original pseudo with
4769 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4770 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4771 results in no secondary memory movements. */
4772 static enum reg_class
4773 choose_split_class (enum reg_class allocno_class,
4774 int hard_regno ATTRIBUTE_UNUSED,
4775 machine_mode mode ATTRIBUTE_UNUSED)
4777 #ifndef SECONDARY_MEMORY_NEEDED
4778 return allocno_class;
4779 #else
4780 int i;
4781 enum reg_class cl, best_cl = NO_REGS;
4782 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4783 = REGNO_REG_CLASS (hard_regno);
4785 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4786 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4787 return allocno_class;
4788 for (i = 0;
4789 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4790 i++)
4791 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4792 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4793 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4794 && (best_cl == NO_REGS
4795 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4796 best_cl = cl;
4797 return best_cl;
4798 #endif
4801 /* Do split transformations for insn INSN, which defines or uses
4802 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4803 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4804 "insns" field of usage_insns.
4806 The transformations look like:
4808 p <- ... p <- ...
4809 ... s <- p (new insn -- save)
4810 ... =>
4811 ... p <- s (new insn -- restore)
4812 <- ... p ... <- ... p ...
4814 <- ... p ... <- ... p ...
4815 ... s <- p (new insn -- save)
4816 ... =>
4817 ... p <- s (new insn -- restore)
4818 <- ... p ... <- ... p ...
4820 where p is an original pseudo got a hard register or a hard
4821 register and s is a new split pseudo. The save is put before INSN
4822 if BEFORE_P is true. Return true if we succeed in such
4823 transformation. */
4824 static bool
4825 split_reg (bool before_p, int original_regno, rtx_insn *insn,
4826 rtx next_usage_insns)
4828 enum reg_class rclass;
4829 rtx original_reg;
4830 int hard_regno, nregs;
4831 rtx new_reg, usage_insn;
4832 rtx_insn *restore, *save;
4833 bool after_p;
4834 bool call_save_p;
4836 if (original_regno < FIRST_PSEUDO_REGISTER)
4838 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4839 hard_regno = original_regno;
4840 call_save_p = false;
4841 nregs = 1;
4843 else
4845 hard_regno = reg_renumber[original_regno];
4846 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4847 rclass = lra_get_allocno_class (original_regno);
4848 original_reg = regno_reg_rtx[original_regno];
4849 call_save_p = need_for_call_save_p (original_regno);
4851 original_reg = regno_reg_rtx[original_regno];
4852 lra_assert (hard_regno >= 0);
4853 if (lra_dump_file != NULL)
4854 fprintf (lra_dump_file,
4855 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4856 if (call_save_p)
4858 machine_mode mode = GET_MODE (original_reg);
4860 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4861 hard_regno_nregs[hard_regno][mode],
4862 mode);
4863 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4865 else
4867 rclass = choose_split_class (rclass, hard_regno,
4868 GET_MODE (original_reg));
4869 if (rclass == NO_REGS)
4871 if (lra_dump_file != NULL)
4873 fprintf (lra_dump_file,
4874 " Rejecting split of %d(%s): "
4875 "no good reg class for %d(%s)\n",
4876 original_regno,
4877 reg_class_names[lra_get_allocno_class (original_regno)],
4878 hard_regno,
4879 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4880 fprintf
4881 (lra_dump_file,
4882 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4884 return false;
4886 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4887 rclass, "split");
4888 reg_renumber[REGNO (new_reg)] = hard_regno;
4890 save = emit_spill_move (true, new_reg, original_reg);
4891 if (NEXT_INSN (save) != NULL_RTX)
4893 lra_assert (! call_save_p);
4894 if (lra_dump_file != NULL)
4896 fprintf
4897 (lra_dump_file,
4898 " Rejecting split %d->%d resulting in > 2 %s save insns:\n",
4899 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4900 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
4901 fprintf (lra_dump_file,
4902 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4904 return false;
4906 restore = emit_spill_move (false, new_reg, original_reg);
4907 if (NEXT_INSN (restore) != NULL_RTX)
4909 lra_assert (! call_save_p);
4910 if (lra_dump_file != NULL)
4912 fprintf (lra_dump_file,
4913 " Rejecting split %d->%d "
4914 "resulting in > 2 %s restore insns:\n",
4915 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4916 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
4917 fprintf (lra_dump_file,
4918 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4920 return false;
4922 after_p = usage_insns[original_regno].after_p;
4923 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4924 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4925 bitmap_set_bit (&check_only_regs, original_regno);
4926 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4927 for (;;)
4929 if (GET_CODE (next_usage_insns) != INSN_LIST)
4931 usage_insn = next_usage_insns;
4932 break;
4934 usage_insn = XEXP (next_usage_insns, 0);
4935 lra_assert (DEBUG_INSN_P (usage_insn));
4936 next_usage_insns = XEXP (next_usage_insns, 1);
4937 lra_substitute_pseudo (&usage_insn, original_regno, new_reg);
4938 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4939 if (lra_dump_file != NULL)
4941 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
4942 original_regno, REGNO (new_reg));
4943 dump_insn_slim (lra_dump_file, usage_insn);
4946 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
4947 lra_assert (usage_insn != insn || (after_p && before_p));
4948 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
4949 after_p ? NULL : restore,
4950 after_p ? restore : NULL,
4951 call_save_p
4952 ? "Add reg<-save" : "Add reg<-split");
4953 lra_process_new_insns (insn, before_p ? save : NULL,
4954 before_p ? NULL : save,
4955 call_save_p
4956 ? "Add save<-reg" : "Add split<-reg");
4957 if (nregs > 1)
4958 /* If we are trying to split multi-register. We should check
4959 conflicts on the next assignment sub-pass. IRA can allocate on
4960 sub-register levels, LRA do this on pseudos level right now and
4961 this discrepancy may create allocation conflicts after
4962 splitting. */
4963 lra_risky_transformations_p = true;
4964 if (lra_dump_file != NULL)
4965 fprintf (lra_dump_file,
4966 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4967 return true;
4970 /* Recognize that we need a split transformation for insn INSN, which
4971 defines or uses REGNO in its insn biggest MODE (we use it only if
4972 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
4973 hard registers which might be used for reloads since the EBB end.
4974 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
4975 uid before starting INSN processing. Return true if we succeed in
4976 such transformation. */
4977 static bool
4978 split_if_necessary (int regno, machine_mode mode,
4979 HARD_REG_SET potential_reload_hard_regs,
4980 bool before_p, rtx_insn *insn, int max_uid)
4982 bool res = false;
4983 int i, nregs = 1;
4984 rtx next_usage_insns;
4986 if (regno < FIRST_PSEUDO_REGISTER)
4987 nregs = hard_regno_nregs[regno][mode];
4988 for (i = 0; i < nregs; i++)
4989 if (usage_insns[regno + i].check == curr_usage_insns_check
4990 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
4991 /* To avoid processing the register twice or more. */
4992 && ((GET_CODE (next_usage_insns) != INSN_LIST
4993 && INSN_UID (next_usage_insns) < max_uid)
4994 || (GET_CODE (next_usage_insns) == INSN_LIST
4995 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
4996 && need_for_split_p (potential_reload_hard_regs, regno + i)
4997 && split_reg (before_p, regno + i, insn, next_usage_insns))
4998 res = true;
4999 return res;
5002 /* Check only registers living at the current program point in the
5003 current EBB. */
5004 static bitmap_head live_regs;
5006 /* Update live info in EBB given by its HEAD and TAIL insns after
5007 inheritance/split transformation. The function removes dead moves
5008 too. */
5009 static void
5010 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5012 unsigned int j;
5013 int i, regno;
5014 bool live_p;
5015 rtx_insn *prev_insn;
5016 rtx set;
5017 bool remove_p;
5018 basic_block last_bb, prev_bb, curr_bb;
5019 bitmap_iterator bi;
5020 struct lra_insn_reg *reg;
5021 edge e;
5022 edge_iterator ei;
5024 last_bb = BLOCK_FOR_INSN (tail);
5025 prev_bb = NULL;
5026 for (curr_insn = tail;
5027 curr_insn != PREV_INSN (head);
5028 curr_insn = prev_insn)
5030 prev_insn = PREV_INSN (curr_insn);
5031 /* We need to process empty blocks too. They contain
5032 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5033 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5034 continue;
5035 curr_bb = BLOCK_FOR_INSN (curr_insn);
5036 if (curr_bb != prev_bb)
5038 if (prev_bb != NULL)
5040 /* Update df_get_live_in (prev_bb): */
5041 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5042 if (bitmap_bit_p (&live_regs, j))
5043 bitmap_set_bit (df_get_live_in (prev_bb), j);
5044 else
5045 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5047 if (curr_bb != last_bb)
5049 /* Update df_get_live_out (curr_bb): */
5050 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5052 live_p = bitmap_bit_p (&live_regs, j);
5053 if (! live_p)
5054 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5055 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5057 live_p = true;
5058 break;
5060 if (live_p)
5061 bitmap_set_bit (df_get_live_out (curr_bb), j);
5062 else
5063 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5066 prev_bb = curr_bb;
5067 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5069 if (! NONDEBUG_INSN_P (curr_insn))
5070 continue;
5071 curr_id = lra_get_insn_recog_data (curr_insn);
5072 curr_static_id = curr_id->insn_static_data;
5073 remove_p = false;
5074 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
5075 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5076 && bitmap_bit_p (&check_only_regs, regno)
5077 && ! bitmap_bit_p (&live_regs, regno))
5078 remove_p = true;
5079 /* See which defined values die here. */
5080 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5081 if (reg->type == OP_OUT && ! reg->subreg_p)
5082 bitmap_clear_bit (&live_regs, reg->regno);
5083 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5084 if (reg->type == OP_OUT && ! reg->subreg_p)
5085 bitmap_clear_bit (&live_regs, reg->regno);
5086 /* Mark each used value as live. */
5087 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5088 if (reg->type != OP_OUT
5089 && bitmap_bit_p (&check_only_regs, reg->regno))
5090 bitmap_set_bit (&live_regs, reg->regno);
5091 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5092 if (reg->type != OP_OUT
5093 && bitmap_bit_p (&check_only_regs, reg->regno))
5094 bitmap_set_bit (&live_regs, reg->regno);
5095 if (curr_id->arg_hard_regs != NULL)
5096 /* Make argument hard registers live. */
5097 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5098 if (bitmap_bit_p (&check_only_regs, regno))
5099 bitmap_set_bit (&live_regs, regno);
5100 /* It is quite important to remove dead move insns because it
5101 means removing dead store. We don't need to process them for
5102 constraints. */
5103 if (remove_p)
5105 if (lra_dump_file != NULL)
5107 fprintf (lra_dump_file, " Removing dead insn:\n ");
5108 dump_insn_slim (lra_dump_file, curr_insn);
5110 lra_set_insn_deleted (curr_insn);
5115 /* The structure describes info to do an inheritance for the current
5116 insn. We need to collect such info first before doing the
5117 transformations because the transformations change the insn
5118 internal representation. */
5119 struct to_inherit
5121 /* Original regno. */
5122 int regno;
5123 /* Subsequent insns which can inherit original reg value. */
5124 rtx insns;
5127 /* Array containing all info for doing inheritance from the current
5128 insn. */
5129 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5131 /* Number elements in the previous array. */
5132 static int to_inherit_num;
5134 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5135 structure to_inherit. */
5136 static void
5137 add_to_inherit (int regno, rtx insns)
5139 int i;
5141 for (i = 0; i < to_inherit_num; i++)
5142 if (to_inherit[i].regno == regno)
5143 return;
5144 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5145 to_inherit[to_inherit_num].regno = regno;
5146 to_inherit[to_inherit_num++].insns = insns;
5149 /* Return the last non-debug insn in basic block BB, or the block begin
5150 note if none. */
5151 static rtx_insn *
5152 get_last_insertion_point (basic_block bb)
5154 rtx_insn *insn;
5156 FOR_BB_INSNS_REVERSE (bb, insn)
5157 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5158 return insn;
5159 gcc_unreachable ();
5162 /* Set up RES by registers living on edges FROM except the edge (FROM,
5163 TO) or by registers set up in a jump insn in BB FROM. */
5164 static void
5165 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5167 rtx_insn *last;
5168 struct lra_insn_reg *reg;
5169 edge e;
5170 edge_iterator ei;
5172 lra_assert (to != NULL);
5173 bitmap_clear (res);
5174 FOR_EACH_EDGE (e, ei, from->succs)
5175 if (e->dest != to)
5176 bitmap_ior_into (res, df_get_live_in (e->dest));
5177 last = get_last_insertion_point (from);
5178 if (! JUMP_P (last))
5179 return;
5180 curr_id = lra_get_insn_recog_data (last);
5181 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5182 if (reg->type != OP_IN)
5183 bitmap_set_bit (res, reg->regno);
5186 /* Used as a temporary results of some bitmap calculations. */
5187 static bitmap_head temp_bitmap;
5189 /* We split for reloads of small class of hard regs. The following
5190 defines how many hard regs the class should have to be qualified as
5191 small. The code is mostly oriented to x86/x86-64 architecture
5192 where some insns need to use only specific register or pair of
5193 registers and these register can live in RTL explicitly, e.g. for
5194 parameter passing. */
5195 static const int max_small_class_regs_num = 2;
5197 /* Do inheritance/split transformations in EBB starting with HEAD and
5198 finishing on TAIL. We process EBB insns in the reverse order.
5199 Return true if we did any inheritance/split transformation in the
5200 EBB.
5202 We should avoid excessive splitting which results in worse code
5203 because of inaccurate cost calculations for spilling new split
5204 pseudos in such case. To achieve this we do splitting only if
5205 register pressure is high in given basic block and there are reload
5206 pseudos requiring hard registers. We could do more register
5207 pressure calculations at any given program point to avoid necessary
5208 splitting even more but it is to expensive and the current approach
5209 works well enough. */
5210 static bool
5211 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5213 int i, src_regno, dst_regno, nregs;
5214 bool change_p, succ_p, update_reloads_num_p;
5215 rtx_insn *prev_insn, *last_insn;
5216 rtx next_usage_insns, set;
5217 enum reg_class cl;
5218 struct lra_insn_reg *reg;
5219 basic_block last_processed_bb, curr_bb = NULL;
5220 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5221 bitmap to_process;
5222 unsigned int j;
5223 bitmap_iterator bi;
5224 bool head_p, after_p;
5226 change_p = false;
5227 curr_usage_insns_check++;
5228 reloads_num = calls_num = 0;
5229 bitmap_clear (&check_only_regs);
5230 last_processed_bb = NULL;
5231 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5232 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5233 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5234 /* We don't process new insns generated in the loop. */
5235 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5237 prev_insn = PREV_INSN (curr_insn);
5238 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5239 curr_bb = BLOCK_FOR_INSN (curr_insn);
5240 if (last_processed_bb != curr_bb)
5242 /* We are at the end of BB. Add qualified living
5243 pseudos for potential splitting. */
5244 to_process = df_get_live_out (curr_bb);
5245 if (last_processed_bb != NULL)
5247 /* We are somewhere in the middle of EBB. */
5248 get_live_on_other_edges (curr_bb, last_processed_bb,
5249 &temp_bitmap);
5250 to_process = &temp_bitmap;
5252 last_processed_bb = curr_bb;
5253 last_insn = get_last_insertion_point (curr_bb);
5254 after_p = (! JUMP_P (last_insn)
5255 && (! CALL_P (last_insn)
5256 || (find_reg_note (last_insn,
5257 REG_NORETURN, NULL_RTX) == NULL_RTX
5258 && ! SIBLING_CALL_P (last_insn))));
5259 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5260 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5262 if ((int) j >= lra_constraint_new_regno_start)
5263 break;
5264 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5266 if (j < FIRST_PSEUDO_REGISTER)
5267 SET_HARD_REG_BIT (live_hard_regs, j);
5268 else
5269 add_to_hard_reg_set (&live_hard_regs,
5270 PSEUDO_REGNO_MODE (j),
5271 reg_renumber[j]);
5272 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5276 src_regno = dst_regno = -1;
5277 if (NONDEBUG_INSN_P (curr_insn)
5278 && (set = single_set (curr_insn)) != NULL_RTX
5279 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5281 src_regno = REGNO (SET_SRC (set));
5282 dst_regno = REGNO (SET_DEST (set));
5284 update_reloads_num_p = true;
5285 if (src_regno < lra_constraint_new_regno_start
5286 && src_regno >= FIRST_PSEUDO_REGISTER
5287 && reg_renumber[src_regno] < 0
5288 && dst_regno >= lra_constraint_new_regno_start
5289 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5291 /* 'reload_pseudo <- original_pseudo'. */
5292 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5293 reloads_num++;
5294 update_reloads_num_p = false;
5295 succ_p = false;
5296 if (usage_insns[src_regno].check == curr_usage_insns_check
5297 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5298 succ_p = inherit_reload_reg (false, src_regno, cl,
5299 curr_insn, next_usage_insns);
5300 if (succ_p)
5301 change_p = true;
5302 else
5303 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5304 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5305 IOR_HARD_REG_SET (potential_reload_hard_regs,
5306 reg_class_contents[cl]);
5308 else if (src_regno >= lra_constraint_new_regno_start
5309 && dst_regno < lra_constraint_new_regno_start
5310 && dst_regno >= FIRST_PSEUDO_REGISTER
5311 && reg_renumber[dst_regno] < 0
5312 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5313 && usage_insns[dst_regno].check == curr_usage_insns_check
5314 && (next_usage_insns
5315 = usage_insns[dst_regno].insns) != NULL_RTX)
5317 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5318 reloads_num++;
5319 update_reloads_num_p = false;
5320 /* 'original_pseudo <- reload_pseudo'. */
5321 if (! JUMP_P (curr_insn)
5322 && inherit_reload_reg (true, dst_regno, cl,
5323 curr_insn, next_usage_insns))
5324 change_p = true;
5325 /* Invalidate. */
5326 usage_insns[dst_regno].check = 0;
5327 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5328 IOR_HARD_REG_SET (potential_reload_hard_regs,
5329 reg_class_contents[cl]);
5331 else if (INSN_P (curr_insn))
5333 int iter;
5334 int max_uid = get_max_uid ();
5336 curr_id = lra_get_insn_recog_data (curr_insn);
5337 curr_static_id = curr_id->insn_static_data;
5338 to_inherit_num = 0;
5339 /* Process insn definitions. */
5340 for (iter = 0; iter < 2; iter++)
5341 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5342 reg != NULL;
5343 reg = reg->next)
5344 if (reg->type != OP_IN
5345 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5347 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5348 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5349 && usage_insns[dst_regno].check == curr_usage_insns_check
5350 && (next_usage_insns
5351 = usage_insns[dst_regno].insns) != NULL_RTX)
5353 struct lra_insn_reg *r;
5355 for (r = curr_id->regs; r != NULL; r = r->next)
5356 if (r->type != OP_OUT && r->regno == dst_regno)
5357 break;
5358 /* Don't do inheritance if the pseudo is also
5359 used in the insn. */
5360 if (r == NULL)
5361 /* We can not do inheritance right now
5362 because the current insn reg info (chain
5363 regs) can change after that. */
5364 add_to_inherit (dst_regno, next_usage_insns);
5366 /* We can not process one reg twice here because of
5367 usage_insns invalidation. */
5368 if ((dst_regno < FIRST_PSEUDO_REGISTER
5369 || reg_renumber[dst_regno] >= 0)
5370 && ! reg->subreg_p && reg->type != OP_IN)
5372 HARD_REG_SET s;
5374 if (split_if_necessary (dst_regno, reg->biggest_mode,
5375 potential_reload_hard_regs,
5376 false, curr_insn, max_uid))
5377 change_p = true;
5378 CLEAR_HARD_REG_SET (s);
5379 if (dst_regno < FIRST_PSEUDO_REGISTER)
5380 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5381 else
5382 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5383 reg_renumber[dst_regno]);
5384 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5386 /* We should invalidate potential inheritance or
5387 splitting for the current insn usages to the next
5388 usage insns (see code below) as the output pseudo
5389 prevents this. */
5390 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5391 && reg_renumber[dst_regno] < 0)
5392 || (reg->type == OP_OUT && ! reg->subreg_p
5393 && (dst_regno < FIRST_PSEUDO_REGISTER
5394 || reg_renumber[dst_regno] >= 0)))
5396 /* Invalidate and mark definitions. */
5397 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5398 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5399 else
5401 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5402 for (i = 0; i < nregs; i++)
5403 usage_insns[dst_regno + i].check
5404 = -(int) INSN_UID (curr_insn);
5408 if (! JUMP_P (curr_insn))
5409 for (i = 0; i < to_inherit_num; i++)
5410 if (inherit_reload_reg (true, to_inherit[i].regno,
5411 ALL_REGS, curr_insn,
5412 to_inherit[i].insns))
5413 change_p = true;
5414 if (CALL_P (curr_insn))
5416 rtx cheap, pat, dest;
5417 rtx_insn *restore;
5418 int regno, hard_regno;
5420 calls_num++;
5421 if ((cheap = find_reg_note (curr_insn,
5422 REG_RETURNED, NULL_RTX)) != NULL_RTX
5423 && ((cheap = XEXP (cheap, 0)), true)
5424 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5425 && (hard_regno = reg_renumber[regno]) >= 0
5426 /* If there are pending saves/restores, the
5427 optimization is not worth. */
5428 && usage_insns[regno].calls_num == calls_num - 1
5429 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5431 /* Restore the pseudo from the call result as
5432 REG_RETURNED note says that the pseudo value is
5433 in the call result and the pseudo is an argument
5434 of the call. */
5435 pat = PATTERN (curr_insn);
5436 if (GET_CODE (pat) == PARALLEL)
5437 pat = XVECEXP (pat, 0, 0);
5438 dest = SET_DEST (pat);
5439 /* For multiple return values dest is PARALLEL.
5440 Currently we handle only single return value case. */
5441 if (REG_P (dest))
5443 start_sequence ();
5444 emit_move_insn (cheap, copy_rtx (dest));
5445 restore = get_insns ();
5446 end_sequence ();
5447 lra_process_new_insns (curr_insn, NULL, restore,
5448 "Inserting call parameter restore");
5449 /* We don't need to save/restore of the pseudo from
5450 this call. */
5451 usage_insns[regno].calls_num = calls_num;
5452 bitmap_set_bit (&check_only_regs, regno);
5456 to_inherit_num = 0;
5457 /* Process insn usages. */
5458 for (iter = 0; iter < 2; iter++)
5459 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5460 reg != NULL;
5461 reg = reg->next)
5462 if ((reg->type != OP_OUT
5463 || (reg->type == OP_OUT && reg->subreg_p))
5464 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5466 if (src_regno >= FIRST_PSEUDO_REGISTER
5467 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5469 if (usage_insns[src_regno].check == curr_usage_insns_check
5470 && (next_usage_insns
5471 = usage_insns[src_regno].insns) != NULL_RTX
5472 && NONDEBUG_INSN_P (curr_insn))
5473 add_to_inherit (src_regno, next_usage_insns);
5474 else if (usage_insns[src_regno].check
5475 != -(int) INSN_UID (curr_insn))
5476 /* Add usages but only if the reg is not set up
5477 in the same insn. */
5478 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5480 else if (src_regno < FIRST_PSEUDO_REGISTER
5481 || reg_renumber[src_regno] >= 0)
5483 bool before_p;
5484 rtx use_insn = curr_insn;
5486 before_p = (JUMP_P (curr_insn)
5487 || (CALL_P (curr_insn) && reg->type == OP_IN));
5488 if (NONDEBUG_INSN_P (curr_insn)
5489 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5490 && split_if_necessary (src_regno, reg->biggest_mode,
5491 potential_reload_hard_regs,
5492 before_p, curr_insn, max_uid))
5494 if (reg->subreg_p)
5495 lra_risky_transformations_p = true;
5496 change_p = true;
5497 /* Invalidate. */
5498 usage_insns[src_regno].check = 0;
5499 if (before_p)
5500 use_insn = PREV_INSN (curr_insn);
5502 if (NONDEBUG_INSN_P (curr_insn))
5504 if (src_regno < FIRST_PSEUDO_REGISTER)
5505 add_to_hard_reg_set (&live_hard_regs,
5506 reg->biggest_mode, src_regno);
5507 else
5508 add_to_hard_reg_set (&live_hard_regs,
5509 PSEUDO_REGNO_MODE (src_regno),
5510 reg_renumber[src_regno]);
5512 add_next_usage_insn (src_regno, use_insn, reloads_num);
5515 /* Process call args. */
5516 if (curr_id->arg_hard_regs != NULL)
5517 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5518 if (src_regno < FIRST_PSEUDO_REGISTER)
5520 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5521 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5523 for (i = 0; i < to_inherit_num; i++)
5525 src_regno = to_inherit[i].regno;
5526 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5527 curr_insn, to_inherit[i].insns))
5528 change_p = true;
5529 else
5530 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5533 if (update_reloads_num_p
5534 && NONDEBUG_INSN_P (curr_insn)
5535 && (set = single_set (curr_insn)) != NULL_RTX)
5537 int regno = -1;
5538 if ((REG_P (SET_DEST (set))
5539 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5540 && reg_renumber[regno] < 0
5541 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5542 || (REG_P (SET_SRC (set))
5543 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5544 && reg_renumber[regno] < 0
5545 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5547 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5548 reloads_num++;
5549 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5550 IOR_HARD_REG_SET (potential_reload_hard_regs,
5551 reg_class_contents[cl]);
5554 /* We reached the start of the current basic block. */
5555 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5556 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5558 /* We reached the beginning of the current block -- do
5559 rest of spliting in the current BB. */
5560 to_process = df_get_live_in (curr_bb);
5561 if (BLOCK_FOR_INSN (head) != curr_bb)
5563 /* We are somewhere in the middle of EBB. */
5564 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5565 curr_bb, &temp_bitmap);
5566 to_process = &temp_bitmap;
5568 head_p = true;
5569 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5571 if ((int) j >= lra_constraint_new_regno_start)
5572 break;
5573 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5574 && usage_insns[j].check == curr_usage_insns_check
5575 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5577 if (need_for_split_p (potential_reload_hard_regs, j))
5579 if (lra_dump_file != NULL && head_p)
5581 fprintf (lra_dump_file,
5582 " ----------------------------------\n");
5583 head_p = false;
5585 if (split_reg (false, j, bb_note (curr_bb),
5586 next_usage_insns))
5587 change_p = true;
5589 usage_insns[j].check = 0;
5594 return change_p;
5597 /* This value affects EBB forming. If probability of edge from EBB to
5598 a BB is not greater than the following value, we don't add the BB
5599 to EBB. */
5600 #define EBB_PROBABILITY_CUTOFF ((REG_BR_PROB_BASE * 50) / 100)
5602 /* Current number of inheritance/split iteration. */
5603 int lra_inheritance_iter;
5605 /* Entry function for inheritance/split pass. */
5606 void
5607 lra_inheritance (void)
5609 int i;
5610 basic_block bb, start_bb;
5611 edge e;
5613 lra_inheritance_iter++;
5614 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5615 return;
5616 timevar_push (TV_LRA_INHERITANCE);
5617 if (lra_dump_file != NULL)
5618 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5619 lra_inheritance_iter);
5620 curr_usage_insns_check = 0;
5621 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5622 for (i = 0; i < lra_constraint_new_regno_start; i++)
5623 usage_insns[i].check = 0;
5624 bitmap_initialize (&check_only_regs, &reg_obstack);
5625 bitmap_initialize (&live_regs, &reg_obstack);
5626 bitmap_initialize (&temp_bitmap, &reg_obstack);
5627 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5628 FOR_EACH_BB_FN (bb, cfun)
5630 start_bb = bb;
5631 if (lra_dump_file != NULL)
5632 fprintf (lra_dump_file, "EBB");
5633 /* Form a EBB starting with BB. */
5634 bitmap_clear (&ebb_global_regs);
5635 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5636 for (;;)
5638 if (lra_dump_file != NULL)
5639 fprintf (lra_dump_file, " %d", bb->index);
5640 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5641 || LABEL_P (BB_HEAD (bb->next_bb)))
5642 break;
5643 e = find_fallthru_edge (bb->succs);
5644 if (! e)
5645 break;
5646 if (e->probability <= EBB_PROBABILITY_CUTOFF)
5647 break;
5648 bb = bb->next_bb;
5650 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5651 if (lra_dump_file != NULL)
5652 fprintf (lra_dump_file, "\n");
5653 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5654 /* Remember that the EBB head and tail can change in
5655 inherit_in_ebb. */
5656 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5658 bitmap_clear (&ebb_global_regs);
5659 bitmap_clear (&temp_bitmap);
5660 bitmap_clear (&live_regs);
5661 bitmap_clear (&check_only_regs);
5662 free (usage_insns);
5664 timevar_pop (TV_LRA_INHERITANCE);
5669 /* This page contains code to undo failed inheritance/split
5670 transformations. */
5672 /* Current number of iteration undoing inheritance/split. */
5673 int lra_undo_inheritance_iter;
5675 /* Fix BB live info LIVE after removing pseudos created on pass doing
5676 inheritance/split which are REMOVED_PSEUDOS. */
5677 static void
5678 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5680 unsigned int regno;
5681 bitmap_iterator bi;
5683 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5684 if (bitmap_clear_bit (live, regno))
5685 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5688 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5689 number. */
5690 static int
5691 get_regno (rtx reg)
5693 if (GET_CODE (reg) == SUBREG)
5694 reg = SUBREG_REG (reg);
5695 if (REG_P (reg))
5696 return REGNO (reg);
5697 return -1;
5700 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5701 return true if we did any change. The undo transformations for
5702 inheritance looks like
5703 i <- i2
5704 p <- i => p <- i2
5705 or removing
5706 p <- i, i <- p, and i <- i3
5707 where p is original pseudo from which inheritance pseudo i was
5708 created, i and i3 are removed inheritance pseudos, i2 is another
5709 not removed inheritance pseudo. All split pseudos or other
5710 occurrences of removed inheritance pseudos are changed on the
5711 corresponding original pseudos.
5713 The function also schedules insns changed and created during
5714 inheritance/split pass for processing by the subsequent constraint
5715 pass. */
5716 static bool
5717 remove_inheritance_pseudos (bitmap remove_pseudos)
5719 basic_block bb;
5720 int regno, sregno, prev_sregno, dregno, restore_regno;
5721 rtx set, prev_set;
5722 rtx_insn *prev_insn;
5723 bool change_p, done_p;
5725 change_p = ! bitmap_empty_p (remove_pseudos);
5726 /* We can not finish the function right away if CHANGE_P is true
5727 because we need to marks insns affected by previous
5728 inheritance/split pass for processing by the subsequent
5729 constraint pass. */
5730 FOR_EACH_BB_FN (bb, cfun)
5732 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5733 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5734 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5736 if (! INSN_P (curr_insn))
5737 continue;
5738 done_p = false;
5739 sregno = dregno = -1;
5740 if (change_p && NONDEBUG_INSN_P (curr_insn)
5741 && (set = single_set (curr_insn)) != NULL_RTX)
5743 dregno = get_regno (SET_DEST (set));
5744 sregno = get_regno (SET_SRC (set));
5747 if (sregno >= 0 && dregno >= 0)
5749 if ((bitmap_bit_p (remove_pseudos, sregno)
5750 && (lra_reg_info[sregno].restore_regno == dregno
5751 || (bitmap_bit_p (remove_pseudos, dregno)
5752 && (lra_reg_info[sregno].restore_regno
5753 == lra_reg_info[dregno].restore_regno))))
5754 || (bitmap_bit_p (remove_pseudos, dregno)
5755 && lra_reg_info[dregno].restore_regno == sregno))
5756 /* One of the following cases:
5757 original <- removed inheritance pseudo
5758 removed inherit pseudo <- another removed inherit pseudo
5759 removed inherit pseudo <- original pseudo
5761 removed_split_pseudo <- original_reg
5762 original_reg <- removed_split_pseudo */
5764 if (lra_dump_file != NULL)
5766 fprintf (lra_dump_file, " Removing %s:\n",
5767 bitmap_bit_p (&lra_split_regs, sregno)
5768 || bitmap_bit_p (&lra_split_regs, dregno)
5769 ? "split" : "inheritance");
5770 dump_insn_slim (lra_dump_file, curr_insn);
5772 lra_set_insn_deleted (curr_insn);
5773 done_p = true;
5775 else if (bitmap_bit_p (remove_pseudos, sregno)
5776 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5778 /* Search the following pattern:
5779 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5780 original_pseudo <- inherit_or_split_pseudo1
5781 where the 2nd insn is the current insn and
5782 inherit_or_split_pseudo2 is not removed. If it is found,
5783 change the current insn onto:
5784 original_pseudo <- inherit_or_split_pseudo2. */
5785 for (prev_insn = PREV_INSN (curr_insn);
5786 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5787 prev_insn = PREV_INSN (prev_insn))
5789 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5790 && (prev_set = single_set (prev_insn)) != NULL_RTX
5791 /* There should be no subregs in insn we are
5792 searching because only the original reg might
5793 be in subreg when we changed the mode of
5794 load/store for splitting. */
5795 && REG_P (SET_DEST (prev_set))
5796 && REG_P (SET_SRC (prev_set))
5797 && (int) REGNO (SET_DEST (prev_set)) == sregno
5798 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5799 >= FIRST_PSEUDO_REGISTER)
5800 /* As we consider chain of inheritance or
5801 splitting described in above comment we should
5802 check that sregno and prev_sregno were
5803 inheritance/split pseudos created from the
5804 same original regno. */
5805 && (lra_reg_info[sregno].restore_regno
5806 == lra_reg_info[prev_sregno].restore_regno)
5807 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5809 lra_assert (GET_MODE (SET_SRC (prev_set))
5810 == GET_MODE (regno_reg_rtx[sregno]));
5811 if (GET_CODE (SET_SRC (set)) == SUBREG)
5812 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5813 else
5814 SET_SRC (set) = SET_SRC (prev_set);
5815 /* As we are finishing with processing the insn
5816 here, check the destination too as it might
5817 inheritance pseudo for another pseudo. */
5818 if (bitmap_bit_p (remove_pseudos, dregno)
5819 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
5820 && (restore_regno
5821 = lra_reg_info[dregno].restore_regno) >= 0)
5823 if (GET_CODE (SET_DEST (set)) == SUBREG)
5824 SUBREG_REG (SET_DEST (set))
5825 = regno_reg_rtx[restore_regno];
5826 else
5827 SET_DEST (set) = regno_reg_rtx[restore_regno];
5829 lra_push_insn_and_update_insn_regno_info (curr_insn);
5830 lra_set_used_insn_alternative_by_uid
5831 (INSN_UID (curr_insn), -1);
5832 done_p = true;
5833 if (lra_dump_file != NULL)
5835 fprintf (lra_dump_file, " Change reload insn:\n");
5836 dump_insn_slim (lra_dump_file, curr_insn);
5841 if (! done_p)
5843 struct lra_insn_reg *reg;
5844 bool restored_regs_p = false;
5845 bool kept_regs_p = false;
5847 curr_id = lra_get_insn_recog_data (curr_insn);
5848 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5850 regno = reg->regno;
5851 restore_regno = lra_reg_info[regno].restore_regno;
5852 if (restore_regno >= 0)
5854 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5856 lra_substitute_pseudo_within_insn (
5857 curr_insn, regno, regno_reg_rtx[restore_regno]);
5858 restored_regs_p = true;
5860 else
5861 kept_regs_p = true;
5864 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5866 /* The instruction has changed since the previous
5867 constraints pass. */
5868 lra_push_insn_and_update_insn_regno_info (curr_insn);
5869 lra_set_used_insn_alternative_by_uid
5870 (INSN_UID (curr_insn), -1);
5872 else if (restored_regs_p)
5873 /* The instruction has been restored to the form that
5874 it had during the previous constraints pass. */
5875 lra_update_insn_regno_info (curr_insn);
5876 if (restored_regs_p && lra_dump_file != NULL)
5878 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5879 dump_insn_slim (lra_dump_file, curr_insn);
5884 return change_p;
5887 /* If optional reload pseudos failed to get a hard register or was not
5888 inherited, it is better to remove optional reloads. We do this
5889 transformation after undoing inheritance to figure out necessity to
5890 remove optional reloads easier. Return true if we do any
5891 change. */
5892 static bool
5893 undo_optional_reloads (void)
5895 bool change_p, keep_p;
5896 unsigned int regno, uid;
5897 bitmap_iterator bi, bi2;
5898 rtx_insn *insn;
5899 rtx set, src, dest;
5900 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5902 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5903 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5904 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5906 keep_p = false;
5907 /* Keep optional reloads from previous subpasses. */
5908 if (lra_reg_info[regno].restore_regno < 0
5909 /* If the original pseudo changed its allocation, just
5910 removing the optional pseudo is dangerous as the original
5911 pseudo will have longer live range. */
5912 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
5913 keep_p = true;
5914 else if (reg_renumber[regno] >= 0)
5915 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
5917 insn = lra_insn_recog_data[uid]->insn;
5918 if ((set = single_set (insn)) == NULL_RTX)
5919 continue;
5920 src = SET_SRC (set);
5921 dest = SET_DEST (set);
5922 if (! REG_P (src) || ! REG_P (dest))
5923 continue;
5924 if (REGNO (dest) == regno
5925 /* Ignore insn for optional reloads itself. */
5926 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
5927 /* Check only inheritance on last inheritance pass. */
5928 && (int) REGNO (src) >= new_regno_start
5929 /* Check that the optional reload was inherited. */
5930 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
5932 keep_p = true;
5933 break;
5936 if (keep_p)
5938 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
5939 if (lra_dump_file != NULL)
5940 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
5943 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
5944 bitmap_initialize (&insn_bitmap, &reg_obstack);
5945 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
5947 if (lra_dump_file != NULL)
5948 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
5949 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
5950 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
5952 insn = lra_insn_recog_data[uid]->insn;
5953 if ((set = single_set (insn)) != NULL_RTX)
5955 src = SET_SRC (set);
5956 dest = SET_DEST (set);
5957 if (REG_P (src) && REG_P (dest)
5958 && ((REGNO (src) == regno
5959 && (lra_reg_info[regno].restore_regno
5960 == (int) REGNO (dest)))
5961 || (REGNO (dest) == regno
5962 && (lra_reg_info[regno].restore_regno
5963 == (int) REGNO (src)))))
5965 if (lra_dump_file != NULL)
5967 fprintf (lra_dump_file, " Deleting move %u\n",
5968 INSN_UID (insn));
5969 dump_insn_slim (lra_dump_file, insn);
5971 lra_set_insn_deleted (insn);
5972 continue;
5974 /* We should not worry about generation memory-memory
5975 moves here as if the corresponding inheritance did
5976 not work (inheritance pseudo did not get a hard reg),
5977 we remove the inheritance pseudo and the optional
5978 reload. */
5980 lra_substitute_pseudo_within_insn (
5981 insn, regno,
5982 regno_reg_rtx[lra_reg_info[regno].restore_regno]);
5983 lra_update_insn_regno_info (insn);
5984 if (lra_dump_file != NULL)
5986 fprintf (lra_dump_file,
5987 " Restoring original insn:\n");
5988 dump_insn_slim (lra_dump_file, insn);
5992 /* Clear restore_regnos. */
5993 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5994 lra_reg_info[regno].restore_regno = -1;
5995 bitmap_clear (&insn_bitmap);
5996 bitmap_clear (&removed_optional_reload_pseudos);
5997 return change_p;
6000 /* Entry function for undoing inheritance/split transformation. Return true
6001 if we did any RTL change in this pass. */
6002 bool
6003 lra_undo_inheritance (void)
6005 unsigned int regno;
6006 int restore_regno, hard_regno;
6007 int n_all_inherit, n_inherit, n_all_split, n_split;
6008 bitmap_head remove_pseudos;
6009 bitmap_iterator bi;
6010 bool change_p;
6012 lra_undo_inheritance_iter++;
6013 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6014 return false;
6015 if (lra_dump_file != NULL)
6016 fprintf (lra_dump_file,
6017 "\n********** Undoing inheritance #%d: **********\n\n",
6018 lra_undo_inheritance_iter);
6019 bitmap_initialize (&remove_pseudos, &reg_obstack);
6020 n_inherit = n_all_inherit = 0;
6021 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6022 if (lra_reg_info[regno].restore_regno >= 0)
6024 n_all_inherit++;
6025 if (reg_renumber[regno] < 0
6026 /* If the original pseudo changed its allocation, just
6027 removing inheritance is dangerous as for changing
6028 allocation we used shorter live-ranges. */
6029 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6030 bitmap_set_bit (&remove_pseudos, regno);
6031 else
6032 n_inherit++;
6034 if (lra_dump_file != NULL && n_all_inherit != 0)
6035 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6036 n_inherit, n_all_inherit,
6037 (double) n_inherit / n_all_inherit * 100);
6038 n_split = n_all_split = 0;
6039 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6040 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6042 n_all_split++;
6043 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6044 ? reg_renumber[restore_regno] : restore_regno);
6045 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6046 bitmap_set_bit (&remove_pseudos, regno);
6047 else
6049 n_split++;
6050 if (lra_dump_file != NULL)
6051 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6052 regno, restore_regno);
6055 if (lra_dump_file != NULL && n_all_split != 0)
6056 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6057 n_split, n_all_split,
6058 (double) n_split / n_all_split * 100);
6059 change_p = remove_inheritance_pseudos (&remove_pseudos);
6060 bitmap_clear (&remove_pseudos);
6061 /* Clear restore_regnos. */
6062 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6063 lra_reg_info[regno].restore_regno = -1;
6064 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6065 lra_reg_info[regno].restore_regno = -1;
6066 change_p = undo_optional_reloads () || change_p;
6067 return change_p;