Daily bump.
[official-gcc.git] / gcc / lra-constraints.c
blob75c2a77b3dca76700b5c08ea94863ed39ba04ec2
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2015 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 "function.h"
124 #include "symtab.h"
125 #include "flags.h"
126 #include "alias.h"
127 #include "tree.h"
128 #include "expmed.h"
129 #include "dojump.h"
130 #include "explow.h"
131 #include "calls.h"
132 #include "emit-rtl.h"
133 #include "varasm.h"
134 #include "stmt.h"
135 #include "expr.h"
136 #include "predict.h"
137 #include "dominance.h"
138 #include "cfg.h"
139 #include "cfgrtl.h"
140 #include "basic-block.h"
141 #include "except.h"
142 #include "optabs.h"
143 #include "df.h"
144 #include "ira.h"
145 #include "rtl-error.h"
146 #include "params.h"
147 #include "lra-int.h"
149 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
150 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
151 reload insns. */
152 static int bb_reload_num;
154 /* The current insn being processed and corresponding its single set
155 (NULL otherwise), its data (basic block, the insn data, the insn
156 static data, and the mode of each operand). */
157 static rtx_insn *curr_insn;
158 static rtx curr_insn_set;
159 static basic_block curr_bb;
160 static lra_insn_recog_data_t curr_id;
161 static struct lra_static_insn_data *curr_static_id;
162 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
163 /* Mode of the register substituted by its equivalence with VOIDmode
164 (e.g. constant) and whose subreg is given operand of the current
165 insn. VOIDmode in all other cases. */
166 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
170 /* Start numbers for new registers and insns at the current constraints
171 pass start. */
172 static int new_regno_start;
173 static int new_insn_uid_start;
175 /* If LOC is nonnull, strip any outer subreg from it. */
176 static inline rtx *
177 strip_subreg (rtx *loc)
179 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
182 /* Return hard regno of REGNO or if it is was not assigned to a hard
183 register, use a hard register from its allocno class. */
184 static int
185 get_try_hard_regno (int regno)
187 int hard_regno;
188 enum reg_class rclass;
190 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
191 hard_regno = lra_get_regno_hard_regno (regno);
192 if (hard_regno >= 0)
193 return hard_regno;
194 rclass = lra_get_allocno_class (regno);
195 if (rclass == NO_REGS)
196 return -1;
197 return ira_class_hard_regs[rclass][0];
200 /* Return final hard regno (plus offset) which will be after
201 elimination. We do this for matching constraints because the final
202 hard regno could have a different class. */
203 static int
204 get_final_hard_regno (int hard_regno, int offset)
206 if (hard_regno < 0)
207 return hard_regno;
208 hard_regno = lra_get_elimination_hard_regno (hard_regno);
209 return hard_regno + offset;
212 /* Return hard regno of X after removing subreg and making
213 elimination. If X is not a register or subreg of register, return
214 -1. For pseudo use its assignment. */
215 static int
216 get_hard_regno (rtx x)
218 rtx reg;
219 int offset, hard_regno;
221 reg = x;
222 if (GET_CODE (x) == SUBREG)
223 reg = SUBREG_REG (x);
224 if (! REG_P (reg))
225 return -1;
226 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
227 hard_regno = lra_get_regno_hard_regno (hard_regno);
228 if (hard_regno < 0)
229 return -1;
230 offset = 0;
231 if (GET_CODE (x) == SUBREG)
232 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
233 SUBREG_BYTE (x), GET_MODE (x));
234 return get_final_hard_regno (hard_regno, offset);
237 /* If REGNO is a hard register or has been allocated a hard register,
238 return the class of that register. If REGNO is a reload pseudo
239 created by the current constraints pass, return its allocno class.
240 Return NO_REGS otherwise. */
241 static enum reg_class
242 get_reg_class (int regno)
244 int hard_regno;
246 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
247 hard_regno = lra_get_regno_hard_regno (regno);
248 if (hard_regno >= 0)
250 hard_regno = get_final_hard_regno (hard_regno, 0);
251 return REGNO_REG_CLASS (hard_regno);
253 if (regno >= new_regno_start)
254 return lra_get_allocno_class (regno);
255 return NO_REGS;
258 /* Return true if REG satisfies (or will satisfy) reg class constraint
259 CL. Use elimination first if REG is a hard register. If REG is a
260 reload pseudo created by this constraints pass, assume that it will
261 be allocated a hard register from its allocno class, but allow that
262 class to be narrowed to CL if it is currently a superset of CL.
264 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
265 REGNO (reg), or NO_REGS if no change in its class was needed. */
266 static bool
267 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
269 enum reg_class rclass, common_class;
270 machine_mode reg_mode;
271 int class_size, hard_regno, nregs, i, j;
272 int regno = REGNO (reg);
274 if (new_class != NULL)
275 *new_class = NO_REGS;
276 if (regno < FIRST_PSEUDO_REGISTER)
278 rtx final_reg = reg;
279 rtx *final_loc = &final_reg;
281 lra_eliminate_reg_if_possible (final_loc);
282 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
284 reg_mode = GET_MODE (reg);
285 rclass = get_reg_class (regno);
286 if (regno < new_regno_start
287 /* Do not allow the constraints for reload instructions to
288 influence the classes of new pseudos. These reloads are
289 typically moves that have many alternatives, and restricting
290 reload pseudos for one alternative may lead to situations
291 where other reload pseudos are no longer allocatable. */
292 || (INSN_UID (curr_insn) >= new_insn_uid_start
293 && curr_insn_set != NULL
294 && ((OBJECT_P (SET_SRC (curr_insn_set))
295 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
296 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
297 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
298 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
299 /* When we don't know what class will be used finally for reload
300 pseudos, we use ALL_REGS. */
301 return ((regno >= new_regno_start && rclass == ALL_REGS)
302 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
303 && ! hard_reg_set_subset_p (reg_class_contents[cl],
304 lra_no_alloc_regs)));
305 else
307 common_class = ira_reg_class_subset[rclass][cl];
308 if (new_class != NULL)
309 *new_class = common_class;
310 if (hard_reg_set_subset_p (reg_class_contents[common_class],
311 lra_no_alloc_regs))
312 return false;
313 /* Check that there are enough allocatable regs. */
314 class_size = ira_class_hard_regs_num[common_class];
315 for (i = 0; i < class_size; i++)
317 hard_regno = ira_class_hard_regs[common_class][i];
318 nregs = hard_regno_nregs[hard_regno][reg_mode];
319 if (nregs == 1)
320 return true;
321 for (j = 0; j < nregs; j++)
322 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
323 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
324 hard_regno + j))
325 break;
326 if (j >= nregs)
327 return true;
329 return false;
333 /* Return true if REGNO satisfies a memory constraint. */
334 static bool
335 in_mem_p (int regno)
337 return get_reg_class (regno) == NO_REGS;
340 /* Return 1 if ADDR is a valid memory address for mode MODE in address
341 space AS, and check that each pseudo has the proper kind of hard
342 reg. */
343 static int
344 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
345 rtx addr, addr_space_t as)
347 #ifdef GO_IF_LEGITIMATE_ADDRESS
348 lra_assert (ADDR_SPACE_GENERIC_P (as));
349 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
350 return 0;
352 win:
353 return 1;
354 #else
355 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
356 #endif
359 namespace {
360 /* Temporarily eliminates registers in an address (for the lifetime of
361 the object). */
362 class address_eliminator {
363 public:
364 address_eliminator (struct address_info *ad);
365 ~address_eliminator ();
367 private:
368 struct address_info *m_ad;
369 rtx *m_base_loc;
370 rtx m_base_reg;
371 rtx *m_index_loc;
372 rtx m_index_reg;
376 address_eliminator::address_eliminator (struct address_info *ad)
377 : m_ad (ad),
378 m_base_loc (strip_subreg (ad->base_term)),
379 m_base_reg (NULL_RTX),
380 m_index_loc (strip_subreg (ad->index_term)),
381 m_index_reg (NULL_RTX)
383 if (m_base_loc != NULL)
385 m_base_reg = *m_base_loc;
386 lra_eliminate_reg_if_possible (m_base_loc);
387 if (m_ad->base_term2 != NULL)
388 *m_ad->base_term2 = *m_ad->base_term;
390 if (m_index_loc != NULL)
392 m_index_reg = *m_index_loc;
393 lra_eliminate_reg_if_possible (m_index_loc);
397 address_eliminator::~address_eliminator ()
399 if (m_base_loc && *m_base_loc != m_base_reg)
401 *m_base_loc = m_base_reg;
402 if (m_ad->base_term2 != NULL)
403 *m_ad->base_term2 = *m_ad->base_term;
405 if (m_index_loc && *m_index_loc != m_index_reg)
406 *m_index_loc = m_index_reg;
409 /* Return true if the eliminated form of AD is a legitimate target address. */
410 static bool
411 valid_address_p (struct address_info *ad)
413 address_eliminator eliminator (ad);
414 return valid_address_p (ad->mode, *ad->outer, ad->as);
417 /* Return true if the eliminated form of memory reference OP satisfies
418 extra memory constraint CONSTRAINT. */
419 static bool
420 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
422 struct address_info ad;
424 decompose_mem_address (&ad, op);
425 address_eliminator eliminator (&ad);
426 return constraint_satisfied_p (op, constraint);
429 /* Return true if the eliminated form of address AD satisfies extra
430 address constraint CONSTRAINT. */
431 static bool
432 satisfies_address_constraint_p (struct address_info *ad,
433 enum constraint_num constraint)
435 address_eliminator eliminator (ad);
436 return constraint_satisfied_p (*ad->outer, constraint);
439 /* Return true if the eliminated form of address OP satisfies extra
440 address constraint CONSTRAINT. */
441 static bool
442 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
444 struct address_info ad;
446 decompose_lea_address (&ad, &op);
447 return satisfies_address_constraint_p (&ad, constraint);
450 /* Initiate equivalences for LRA. As we keep original equivalences
451 before any elimination, we need to make copies otherwise any change
452 in insns might change the equivalences. */
453 void
454 lra_init_equiv (void)
456 ira_expand_reg_equiv ();
457 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
459 rtx res;
461 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
462 ira_reg_equiv[i].memory = copy_rtx (res);
463 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
464 ira_reg_equiv[i].invariant = copy_rtx (res);
468 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
470 /* Update equivalence for REGNO. We need to this as the equivalence
471 might contain other pseudos which are changed by their
472 equivalences. */
473 static void
474 update_equiv (int regno)
476 rtx x;
478 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
479 ira_reg_equiv[regno].memory
480 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
481 NULL_RTX);
482 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
483 ira_reg_equiv[regno].invariant
484 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
485 NULL_RTX);
488 /* If we have decided to substitute X with another value, return that
489 value, otherwise return X. */
490 static rtx
491 get_equiv (rtx x)
493 int regno;
494 rtx res;
496 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
497 || ! ira_reg_equiv[regno].defined_p
498 || ! ira_reg_equiv[regno].profitable_p
499 || lra_get_regno_hard_regno (regno) >= 0)
500 return x;
501 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
503 if (targetm.cannot_substitute_mem_equiv_p (res))
504 return x;
505 return res;
507 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
508 return res;
509 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
510 return res;
511 gcc_unreachable ();
514 /* If we have decided to substitute X with the equivalent value,
515 return that value after elimination for INSN, otherwise return
516 X. */
517 static rtx
518 get_equiv_with_elimination (rtx x, rtx_insn *insn)
520 rtx res = get_equiv (x);
522 if (x == res || CONSTANT_P (res))
523 return res;
524 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
525 false, false, 0, true);
528 /* Set up curr_operand_mode. */
529 static void
530 init_curr_operand_mode (void)
532 int nop = curr_static_id->n_operands;
533 for (int i = 0; i < nop; i++)
535 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
536 if (mode == VOIDmode)
538 /* The .md mode for address operands is the mode of the
539 addressed value rather than the mode of the address itself. */
540 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
541 mode = Pmode;
542 else
543 mode = curr_static_id->operand[i].mode;
545 curr_operand_mode[i] = mode;
551 /* The page contains code to reuse input reloads. */
553 /* Structure describes input reload of the current insns. */
554 struct input_reload
556 /* Reloaded value. */
557 rtx input;
558 /* Reload pseudo used. */
559 rtx reg;
562 /* The number of elements in the following array. */
563 static int curr_insn_input_reloads_num;
564 /* Array containing info about input reloads. It is used to find the
565 same input reload and reuse the reload pseudo in this case. */
566 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
568 /* Initiate data concerning reuse of input reloads for the current
569 insn. */
570 static void
571 init_curr_insn_input_reloads (void)
573 curr_insn_input_reloads_num = 0;
576 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
577 created input reload pseudo (only if TYPE is not OP_OUT). Don't
578 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
579 wrapped up in SUBREG. The result pseudo is returned through
580 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
581 reused the already created input reload pseudo. Use TITLE to
582 describe new registers for debug purposes. */
583 static bool
584 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
585 enum reg_class rclass, bool in_subreg_p,
586 const char *title, rtx *result_reg)
588 int i, regno;
589 enum reg_class new_class;
591 if (type == OP_OUT)
593 *result_reg
594 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
595 return true;
597 /* Prevent reuse value of expression with side effects,
598 e.g. volatile memory. */
599 if (! side_effects_p (original))
600 for (i = 0; i < curr_insn_input_reloads_num; i++)
601 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
602 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
604 rtx reg = curr_insn_input_reloads[i].reg;
605 regno = REGNO (reg);
606 /* If input is equal to original and both are VOIDmode,
607 GET_MODE (reg) might be still different from mode.
608 Ensure we don't return *result_reg with wrong mode. */
609 if (GET_MODE (reg) != mode)
611 if (in_subreg_p)
612 continue;
613 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
614 continue;
615 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
616 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
617 continue;
619 *result_reg = reg;
620 if (lra_dump_file != NULL)
622 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
623 dump_value_slim (lra_dump_file, original, 1);
625 if (new_class != lra_get_allocno_class (regno))
626 lra_change_class (regno, new_class, ", change to", false);
627 if (lra_dump_file != NULL)
628 fprintf (lra_dump_file, "\n");
629 return false;
631 *result_reg = lra_create_new_reg (mode, original, rclass, title);
632 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
633 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
634 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
635 return true;
640 /* The page contains code to extract memory address parts. */
642 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
643 static inline bool
644 ok_for_index_p_nonstrict (rtx reg)
646 unsigned regno = REGNO (reg);
648 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
651 /* A version of regno_ok_for_base_p for use here, when all pseudos
652 should count as OK. Arguments as for regno_ok_for_base_p. */
653 static inline bool
654 ok_for_base_p_nonstrict (rtx reg, machine_mode mode, addr_space_t as,
655 enum rtx_code outer_code, enum rtx_code index_code)
657 unsigned regno = REGNO (reg);
659 if (regno >= FIRST_PSEUDO_REGISTER)
660 return true;
661 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
666 /* The page contains major code to choose the current insn alternative
667 and generate reloads for it. */
669 /* Return the offset from REGNO of the least significant register
670 in (reg:MODE REGNO).
672 This function is used to tell whether two registers satisfy
673 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
675 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
676 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
678 lra_constraint_offset (int regno, machine_mode mode)
680 lra_assert (regno < FIRST_PSEUDO_REGISTER);
681 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
682 && SCALAR_INT_MODE_P (mode))
683 return hard_regno_nregs[regno][mode] - 1;
684 return 0;
687 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
688 if they are the same hard reg, and has special hacks for
689 auto-increment and auto-decrement. This is specifically intended for
690 process_alt_operands to use in determining whether two operands
691 match. X is the operand whose number is the lower of the two.
693 It is supposed that X is the output operand and Y is the input
694 operand. Y_HARD_REGNO is the final hard regno of register Y or
695 register in subreg Y as we know it now. Otherwise, it is a
696 negative value. */
697 static bool
698 operands_match_p (rtx x, rtx y, int y_hard_regno)
700 int i;
701 RTX_CODE code = GET_CODE (x);
702 const char *fmt;
704 if (x == y)
705 return true;
706 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
707 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
709 int j;
711 i = get_hard_regno (x);
712 if (i < 0)
713 goto slow;
715 if ((j = y_hard_regno) < 0)
716 goto slow;
718 i += lra_constraint_offset (i, GET_MODE (x));
719 j += lra_constraint_offset (j, GET_MODE (y));
721 return i == j;
724 /* If two operands must match, because they are really a single
725 operand of an assembler insn, then two post-increments are invalid
726 because the assembler insn would increment only once. On the
727 other hand, a post-increment matches ordinary indexing if the
728 post-increment is the output operand. */
729 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
730 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
732 /* Two pre-increments are invalid because the assembler insn would
733 increment only once. On the other hand, a pre-increment matches
734 ordinary indexing if the pre-increment is the input operand. */
735 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
736 || GET_CODE (y) == PRE_MODIFY)
737 return operands_match_p (x, XEXP (y, 0), -1);
739 slow:
741 if (code == REG && REG_P (y))
742 return REGNO (x) == REGNO (y);
744 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
745 && x == SUBREG_REG (y))
746 return true;
747 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
748 && SUBREG_REG (x) == y)
749 return true;
751 /* Now we have disposed of all the cases in which different rtx
752 codes can match. */
753 if (code != GET_CODE (y))
754 return false;
756 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
757 if (GET_MODE (x) != GET_MODE (y))
758 return false;
760 switch (code)
762 CASE_CONST_UNIQUE:
763 return false;
765 case LABEL_REF:
766 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
767 case SYMBOL_REF:
768 return XSTR (x, 0) == XSTR (y, 0);
770 default:
771 break;
774 /* Compare the elements. If any pair of corresponding elements fail
775 to match, return false for the whole things. */
777 fmt = GET_RTX_FORMAT (code);
778 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
780 int val, j;
781 switch (fmt[i])
783 case 'w':
784 if (XWINT (x, i) != XWINT (y, i))
785 return false;
786 break;
788 case 'i':
789 if (XINT (x, i) != XINT (y, i))
790 return false;
791 break;
793 case 'e':
794 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
795 if (val == 0)
796 return false;
797 break;
799 case '0':
800 break;
802 case 'E':
803 if (XVECLEN (x, i) != XVECLEN (y, i))
804 return false;
805 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
807 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
808 if (val == 0)
809 return false;
811 break;
813 /* It is believed that rtx's at this level will never
814 contain anything but integers and other rtx's, except for
815 within LABEL_REFs and SYMBOL_REFs. */
816 default:
817 gcc_unreachable ();
820 return true;
823 /* True if X is a constant that can be forced into the constant pool.
824 MODE is the mode of the operand, or VOIDmode if not known. */
825 #define CONST_POOL_OK_P(MODE, X) \
826 ((MODE) != VOIDmode \
827 && CONSTANT_P (X) \
828 && GET_CODE (X) != HIGH \
829 && !targetm.cannot_force_const_mem (MODE, X))
831 /* True if C is a non-empty register class that has too few registers
832 to be safely used as a reload target class. */
833 #define SMALL_REGISTER_CLASS_P(C) \
834 (ira_class_hard_regs_num [(C)] == 1 \
835 || (ira_class_hard_regs_num [(C)] >= 1 \
836 && targetm.class_likely_spilled_p (C)))
838 /* If REG is a reload pseudo, try to make its class satisfying CL. */
839 static void
840 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
842 enum reg_class rclass;
844 /* Do not make more accurate class from reloads generated. They are
845 mostly moves with a lot of constraints. Making more accurate
846 class may results in very narrow class and impossibility of find
847 registers for several reloads of one insn. */
848 if (INSN_UID (curr_insn) >= new_insn_uid_start)
849 return;
850 if (GET_CODE (reg) == SUBREG)
851 reg = SUBREG_REG (reg);
852 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
853 return;
854 if (in_class_p (reg, cl, &rclass) && rclass != cl)
855 lra_change_class (REGNO (reg), rclass, " Change to", true);
858 /* Generate reloads for matching OUT and INS (array of input operand
859 numbers with end marker -1) with reg class GOAL_CLASS. Add input
860 and output reloads correspondingly to the lists *BEFORE and *AFTER.
861 OUT might be negative. In this case we generate input reloads for
862 matched input operands INS. */
863 static void
864 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
865 rtx_insn **before, rtx_insn **after)
867 int i, in;
868 rtx new_in_reg, new_out_reg, reg;
869 machine_mode inmode, outmode;
870 rtx in_rtx = *curr_id->operand_loc[ins[0]];
871 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
873 inmode = curr_operand_mode[ins[0]];
874 outmode = out < 0 ? inmode : curr_operand_mode[out];
875 push_to_sequence (*before);
876 if (inmode != outmode)
878 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
880 reg = new_in_reg
881 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
882 goal_class, "");
883 if (SCALAR_INT_MODE_P (inmode))
884 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
885 else
886 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
887 LRA_SUBREG_P (new_out_reg) = 1;
888 /* If the input reg is dying here, we can use the same hard
889 register for REG and IN_RTX. We do it only for original
890 pseudos as reload pseudos can die although original
891 pseudos still live where reload pseudos dies. */
892 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
893 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
894 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
896 else
898 reg = new_out_reg
899 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
900 goal_class, "");
901 if (SCALAR_INT_MODE_P (outmode))
902 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
903 else
904 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
905 /* NEW_IN_REG is non-paradoxical subreg. We don't want
906 NEW_OUT_REG living above. We add clobber clause for
907 this. This is just a temporary clobber. We can remove
908 it at the end of LRA work. */
909 rtx_insn *clobber = emit_clobber (new_out_reg);
910 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
911 LRA_SUBREG_P (new_in_reg) = 1;
912 if (GET_CODE (in_rtx) == SUBREG)
914 rtx subreg_reg = SUBREG_REG (in_rtx);
916 /* If SUBREG_REG is dying here and sub-registers IN_RTX
917 and NEW_IN_REG are similar, we can use the same hard
918 register for REG and SUBREG_REG. */
919 if (REG_P (subreg_reg)
920 && (int) REGNO (subreg_reg) < lra_new_regno_start
921 && GET_MODE (subreg_reg) == outmode
922 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
923 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
924 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
928 else
930 /* Pseudos have values -- see comments for lra_reg_info.
931 Different pseudos with the same value do not conflict even if
932 they live in the same place. When we create a pseudo we
933 assign value of original pseudo (if any) from which we
934 created the new pseudo. If we create the pseudo from the
935 input pseudo, the new pseudo will no conflict with the input
936 pseudo which is wrong when the input pseudo lives after the
937 insn and as the new pseudo value is changed by the insn
938 output. Therefore we create the new pseudo from the output.
940 We cannot reuse the current output register because we might
941 have a situation like "a <- a op b", where the constraints
942 force the second input operand ("b") to match the output
943 operand ("a"). "b" must then be copied into a new register
944 so that it doesn't clobber the current value of "a". */
946 new_in_reg = new_out_reg
947 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
948 goal_class, "");
950 /* In operand can be got from transformations before processing insn
951 constraints. One example of such transformations is subreg
952 reloading (see function simplify_operand_subreg). The new
953 pseudos created by the transformations might have inaccurate
954 class (ALL_REGS) and we should make their classes more
955 accurate. */
956 narrow_reload_pseudo_class (in_rtx, goal_class);
957 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
958 *before = get_insns ();
959 end_sequence ();
960 for (i = 0; (in = ins[i]) >= 0; i++)
962 lra_assert
963 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
964 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
965 *curr_id->operand_loc[in] = new_in_reg;
967 lra_update_dups (curr_id, ins);
968 if (out < 0)
969 return;
970 /* See a comment for the input operand above. */
971 narrow_reload_pseudo_class (out_rtx, goal_class);
972 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
974 start_sequence ();
975 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
976 emit_insn (*after);
977 *after = get_insns ();
978 end_sequence ();
980 *curr_id->operand_loc[out] = new_out_reg;
981 lra_update_dup (curr_id, out);
984 /* Return register class which is union of all reg classes in insn
985 constraint alternative string starting with P. */
986 static enum reg_class
987 reg_class_from_constraints (const char *p)
989 int c, len;
990 enum reg_class op_class = NO_REGS;
993 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
995 case '#':
996 case ',':
997 return op_class;
999 case 'g':
1000 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1001 break;
1003 default:
1004 enum constraint_num cn = lookup_constraint (p);
1005 enum reg_class cl = reg_class_for_constraint (cn);
1006 if (cl == NO_REGS)
1008 if (insn_extra_address_constraint (cn))
1009 op_class
1010 = (reg_class_subunion
1011 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1012 ADDRESS, SCRATCH)]);
1013 break;
1016 op_class = reg_class_subunion[op_class][cl];
1017 break;
1019 while ((p += len), c);
1020 return op_class;
1023 /* If OP is a register, return the class of the register as per
1024 get_reg_class, otherwise return NO_REGS. */
1025 static inline enum reg_class
1026 get_op_class (rtx op)
1028 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1031 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1032 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1033 SUBREG for VAL to make them equal. */
1034 static rtx_insn *
1035 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1037 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1039 /* Usually size of mem_pseudo is greater than val size but in
1040 rare cases it can be less as it can be defined by target
1041 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1042 if (! MEM_P (val))
1044 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1045 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1047 LRA_SUBREG_P (val) = 1;
1049 else
1051 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1052 LRA_SUBREG_P (mem_pseudo) = 1;
1055 return to_p ? gen_move_insn (mem_pseudo, val)
1056 : gen_move_insn (val, mem_pseudo);
1059 /* Process a special case insn (register move), return true if we
1060 don't need to process it anymore. INSN should be a single set
1061 insn. Set up that RTL was changed through CHANGE_P and macro
1062 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1063 SEC_MEM_P. */
1064 static bool
1065 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1067 int sregno, dregno;
1068 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1069 rtx_insn *before;
1070 enum reg_class dclass, sclass, secondary_class;
1071 secondary_reload_info sri;
1073 lra_assert (curr_insn_set != NULL_RTX);
1074 dreg = dest = SET_DEST (curr_insn_set);
1075 sreg = src = SET_SRC (curr_insn_set);
1076 if (GET_CODE (dest) == SUBREG)
1077 dreg = SUBREG_REG (dest);
1078 if (GET_CODE (src) == SUBREG)
1079 sreg = SUBREG_REG (src);
1080 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1081 return false;
1082 sclass = dclass = NO_REGS;
1083 if (REG_P (dreg))
1084 dclass = get_reg_class (REGNO (dreg));
1085 if (dclass == ALL_REGS)
1086 /* ALL_REGS is used for new pseudos created by transformations
1087 like reload of SUBREG_REG (see function
1088 simplify_operand_subreg). We don't know their class yet. We
1089 should figure out the class from processing the insn
1090 constraints not in this fast path function. Even if ALL_REGS
1091 were a right class for the pseudo, secondary_... hooks usually
1092 are not define for ALL_REGS. */
1093 return false;
1094 if (REG_P (sreg))
1095 sclass = get_reg_class (REGNO (sreg));
1096 if (sclass == ALL_REGS)
1097 /* See comments above. */
1098 return false;
1099 if (sclass == NO_REGS && dclass == NO_REGS)
1100 return false;
1101 #ifdef SECONDARY_MEMORY_NEEDED
1102 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1103 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1104 && ((sclass != NO_REGS && dclass != NO_REGS)
1105 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1106 #endif
1109 *sec_mem_p = true;
1110 return false;
1112 #endif
1113 if (! REG_P (dreg) || ! REG_P (sreg))
1114 return false;
1115 sri.prev_sri = NULL;
1116 sri.icode = CODE_FOR_nothing;
1117 sri.extra_cost = 0;
1118 secondary_class = NO_REGS;
1119 /* Set up hard register for a reload pseudo for hook
1120 secondary_reload because some targets just ignore unassigned
1121 pseudos in the hook. */
1122 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1124 dregno = REGNO (dreg);
1125 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1127 else
1128 dregno = -1;
1129 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1131 sregno = REGNO (sreg);
1132 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1134 else
1135 sregno = -1;
1136 if (sclass != NO_REGS)
1137 secondary_class
1138 = (enum reg_class) targetm.secondary_reload (false, dest,
1139 (reg_class_t) sclass,
1140 GET_MODE (src), &sri);
1141 if (sclass == NO_REGS
1142 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1143 && dclass != NO_REGS))
1145 enum reg_class old_sclass = secondary_class;
1146 secondary_reload_info old_sri = sri;
1148 sri.prev_sri = NULL;
1149 sri.icode = CODE_FOR_nothing;
1150 sri.extra_cost = 0;
1151 secondary_class
1152 = (enum reg_class) targetm.secondary_reload (true, src,
1153 (reg_class_t) dclass,
1154 GET_MODE (src), &sri);
1155 /* Check the target hook consistency. */
1156 lra_assert
1157 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1158 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1159 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1161 if (sregno >= 0)
1162 reg_renumber [sregno] = -1;
1163 if (dregno >= 0)
1164 reg_renumber [dregno] = -1;
1165 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1166 return false;
1167 *change_p = true;
1168 new_reg = NULL_RTX;
1169 if (secondary_class != NO_REGS)
1170 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1171 secondary_class,
1172 "secondary");
1173 start_sequence ();
1174 if (sri.icode == CODE_FOR_nothing)
1175 lra_emit_move (new_reg, src);
1176 else
1178 enum reg_class scratch_class;
1180 scratch_class = (reg_class_from_constraints
1181 (insn_data[sri.icode].operand[2].constraint));
1182 scratch_reg = (lra_create_new_reg_with_unique_value
1183 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1184 scratch_class, "scratch"));
1185 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1186 src, scratch_reg));
1188 before = get_insns ();
1189 end_sequence ();
1190 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1191 if (new_reg != NULL_RTX)
1192 SET_SRC (curr_insn_set) = new_reg;
1193 else
1195 if (lra_dump_file != NULL)
1197 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1198 dump_insn_slim (lra_dump_file, curr_insn);
1200 lra_set_insn_deleted (curr_insn);
1201 return true;
1203 return false;
1206 /* The following data describe the result of process_alt_operands.
1207 The data are used in curr_insn_transform to generate reloads. */
1209 /* The chosen reg classes which should be used for the corresponding
1210 operands. */
1211 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1212 /* True if the operand should be the same as another operand and that
1213 other operand does not need a reload. */
1214 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1215 /* True if the operand does not need a reload. */
1216 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1217 /* True if the operand can be offsetable memory. */
1218 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1219 /* The number of an operand to which given operand can be matched to. */
1220 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1221 /* The number of elements in the following array. */
1222 static int goal_alt_dont_inherit_ops_num;
1223 /* Numbers of operands whose reload pseudos should not be inherited. */
1224 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1225 /* True if the insn commutative operands should be swapped. */
1226 static bool goal_alt_swapped;
1227 /* The chosen insn alternative. */
1228 static int goal_alt_number;
1230 /* The following five variables are used to choose the best insn
1231 alternative. They reflect final characteristics of the best
1232 alternative. */
1234 /* Number of necessary reloads and overall cost reflecting the
1235 previous value and other unpleasantness of the best alternative. */
1236 static int best_losers, best_overall;
1237 /* Overall number hard registers used for reloads. For example, on
1238 some targets we need 2 general registers to reload DFmode and only
1239 one floating point register. */
1240 static int best_reload_nregs;
1241 /* Overall number reflecting distances of previous reloading the same
1242 value. The distances are counted from the current BB start. It is
1243 used to improve inheritance chances. */
1244 static int best_reload_sum;
1246 /* True if the current insn should have no correspondingly input or
1247 output reloads. */
1248 static bool no_input_reloads_p, no_output_reloads_p;
1250 /* True if we swapped the commutative operands in the current
1251 insn. */
1252 static int curr_swapped;
1254 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1255 register of class CL. Add any input reloads to list BEFORE. AFTER
1256 is nonnull if *LOC is an automodified value; handle that case by
1257 adding the required output reloads to list AFTER. Return true if
1258 the RTL was changed.
1260 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1261 register. Return false if the address register is correct. */
1262 static bool
1263 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1264 enum reg_class cl)
1266 int regno;
1267 enum reg_class rclass, new_class;
1268 rtx reg;
1269 rtx new_reg;
1270 machine_mode mode;
1271 bool subreg_p, before_p = false;
1273 subreg_p = GET_CODE (*loc) == SUBREG;
1274 if (subreg_p)
1275 loc = &SUBREG_REG (*loc);
1276 reg = *loc;
1277 mode = GET_MODE (reg);
1278 if (! REG_P (reg))
1280 if (check_only_p)
1281 return true;
1282 /* Always reload memory in an address even if the target supports
1283 such addresses. */
1284 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1285 before_p = true;
1287 else
1289 regno = REGNO (reg);
1290 rclass = get_reg_class (regno);
1291 if (! check_only_p
1292 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1294 if (lra_dump_file != NULL)
1296 fprintf (lra_dump_file,
1297 "Changing pseudo %d in address of insn %u on equiv ",
1298 REGNO (reg), INSN_UID (curr_insn));
1299 dump_value_slim (lra_dump_file, *loc, 1);
1300 fprintf (lra_dump_file, "\n");
1302 *loc = copy_rtx (*loc);
1304 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1306 if (check_only_p)
1307 return true;
1308 reg = *loc;
1309 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1310 mode, reg, cl, subreg_p, "address", &new_reg))
1311 before_p = true;
1313 else if (new_class != NO_REGS && rclass != new_class)
1315 if (check_only_p)
1316 return true;
1317 lra_change_class (regno, new_class, " Change to", true);
1318 return false;
1320 else
1321 return false;
1323 if (before_p)
1325 push_to_sequence (*before);
1326 lra_emit_move (new_reg, reg);
1327 *before = get_insns ();
1328 end_sequence ();
1330 *loc = new_reg;
1331 if (after != NULL)
1333 start_sequence ();
1334 lra_emit_move (reg, new_reg);
1335 emit_insn (*after);
1336 *after = get_insns ();
1337 end_sequence ();
1339 return true;
1342 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1343 the insn to be inserted before curr insn. AFTER returns the
1344 the insn to be inserted after curr insn. ORIGREG and NEWREG
1345 are the original reg and new reg for reload. */
1346 static void
1347 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1348 rtx newreg)
1350 if (before)
1352 push_to_sequence (*before);
1353 lra_emit_move (newreg, origreg);
1354 *before = get_insns ();
1355 end_sequence ();
1357 if (after)
1359 start_sequence ();
1360 lra_emit_move (origreg, newreg);
1361 emit_insn (*after);
1362 *after = get_insns ();
1363 end_sequence ();
1367 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1369 /* Make reloads for subreg in operand NOP with internal subreg mode
1370 REG_MODE, add new reloads for further processing. Return true if
1371 any change was done. */
1372 static bool
1373 simplify_operand_subreg (int nop, machine_mode reg_mode)
1375 int hard_regno;
1376 rtx_insn *before, *after;
1377 machine_mode mode, innermode;
1378 rtx reg, new_reg;
1379 rtx operand = *curr_id->operand_loc[nop];
1380 enum reg_class regclass;
1381 enum op_type type;
1383 before = after = NULL;
1385 if (GET_CODE (operand) != SUBREG)
1386 return false;
1388 mode = GET_MODE (operand);
1389 reg = SUBREG_REG (operand);
1390 innermode = GET_MODE (reg);
1391 type = curr_static_id->operand[nop].type;
1392 /* If we change address for paradoxical subreg of memory, the
1393 address might violate the necessary alignment or the access might
1394 be slow. So take this into consideration. We should not worry
1395 about access beyond allocated memory for paradoxical memory
1396 subregs as we don't substitute such equiv memory (see processing
1397 equivalences in function lra_constraints) and because for spilled
1398 pseudos we allocate stack memory enough for the biggest
1399 corresponding paradoxical subreg. */
1400 if (MEM_P (reg)
1401 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1402 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1404 rtx subst, old = *curr_id->operand_loc[nop];
1406 alter_subreg (curr_id->operand_loc[nop], false);
1407 subst = *curr_id->operand_loc[nop];
1408 lra_assert (MEM_P (subst));
1409 if (! valid_address_p (innermode, XEXP (reg, 0),
1410 MEM_ADDR_SPACE (reg))
1411 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1412 MEM_ADDR_SPACE (subst)))
1413 return true;
1414 /* If the address was valid and became invalid, prefer to reload
1415 the memory. Typical case is when the index scale should
1416 correspond the memory. */
1417 *curr_id->operand_loc[nop] = old;
1419 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1421 alter_subreg (curr_id->operand_loc[nop], false);
1422 return true;
1424 else if (CONSTANT_P (reg))
1426 /* Try to simplify subreg of constant. It is usually result of
1427 equivalence substitution. */
1428 if (innermode == VOIDmode
1429 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1430 innermode = curr_static_id->operand[nop].mode;
1431 if ((new_reg = simplify_subreg (mode, reg, innermode,
1432 SUBREG_BYTE (operand))) != NULL_RTX)
1434 *curr_id->operand_loc[nop] = new_reg;
1435 return true;
1438 /* Put constant into memory when we have mixed modes. It generates
1439 a better code in most cases as it does not need a secondary
1440 reload memory. It also prevents LRA looping when LRA is using
1441 secondary reload memory again and again. */
1442 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1443 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1445 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1446 alter_subreg (curr_id->operand_loc[nop], false);
1447 return true;
1449 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1450 if there may be a problem accessing OPERAND in the outer
1451 mode. */
1452 if ((REG_P (reg)
1453 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1454 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1455 /* Don't reload paradoxical subregs because we could be looping
1456 having repeatedly final regno out of hard regs range. */
1457 && (hard_regno_nregs[hard_regno][innermode]
1458 >= hard_regno_nregs[hard_regno][mode])
1459 && simplify_subreg_regno (hard_regno, innermode,
1460 SUBREG_BYTE (operand), mode) < 0
1461 /* Don't reload subreg for matching reload. It is actually
1462 valid subreg in LRA. */
1463 && ! LRA_SUBREG_P (operand))
1464 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1466 enum reg_class rclass;
1468 if (REG_P (reg))
1469 /* There is a big probability that we will get the same class
1470 for the new pseudo and we will get the same insn which
1471 means infinite looping. So spill the new pseudo. */
1472 rclass = NO_REGS;
1473 else
1474 /* The class will be defined later in curr_insn_transform. */
1475 rclass
1476 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1478 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1479 rclass, TRUE, "subreg reg", &new_reg))
1481 bool insert_before, insert_after;
1482 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1484 insert_before = (type != OP_OUT
1485 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1486 insert_after = (type != OP_IN);
1487 insert_move_for_subreg (insert_before ? &before : NULL,
1488 insert_after ? &after : NULL,
1489 reg, new_reg);
1491 SUBREG_REG (operand) = new_reg;
1492 lra_process_new_insns (curr_insn, before, after,
1493 "Inserting subreg reload");
1494 return true;
1496 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1497 IRA allocates hardreg to the inner pseudo reg according to its mode
1498 instead of the outermode, so the size of the hardreg may not be enough
1499 to contain the outermode operand, in that case we may need to insert
1500 reload for the reg. For the following two types of paradoxical subreg,
1501 we need to insert reload:
1502 1. If the op_type is OP_IN, and the hardreg could not be paired with
1503 other hardreg to contain the outermode operand
1504 (checked by in_hard_reg_set_p), we need to insert the reload.
1505 2. If the op_type is OP_OUT or OP_INOUT.
1507 Here is a paradoxical subreg example showing how the reload is generated:
1509 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1510 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1512 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1513 here, if reg107 is assigned to hardreg R15, because R15 is the last
1514 hardreg, compiler cannot find another hardreg to pair with R15 to
1515 contain TImode data. So we insert a TImode reload reg180 for it.
1516 After reload is inserted:
1518 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1519 (reg:DI 107 [ __comp ])) -1
1520 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1521 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1523 Two reload hard registers will be allocated to reg180 to save TImode data
1524 in LRA_assign. */
1525 else if (REG_P (reg)
1526 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1527 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1528 && (hard_regno_nregs[hard_regno][innermode]
1529 < hard_regno_nregs[hard_regno][mode])
1530 && (regclass = lra_get_allocno_class (REGNO (reg)))
1531 && (type != OP_IN
1532 || !in_hard_reg_set_p (reg_class_contents[regclass],
1533 mode, hard_regno)))
1535 /* The class will be defined later in curr_insn_transform. */
1536 enum reg_class rclass
1537 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1539 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1540 rclass, TRUE, "paradoxical subreg", &new_reg))
1542 rtx subreg;
1543 bool insert_before, insert_after;
1545 PUT_MODE (new_reg, mode);
1546 subreg = simplify_gen_subreg (innermode, new_reg, mode, 0);
1547 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1549 insert_before = (type != OP_OUT);
1550 insert_after = (type != OP_IN);
1551 insert_move_for_subreg (insert_before ? &before : NULL,
1552 insert_after ? &after : NULL,
1553 reg, subreg);
1555 SUBREG_REG (operand) = new_reg;
1556 lra_process_new_insns (curr_insn, before, after,
1557 "Inserting paradoxical subreg reload");
1558 return true;
1560 return false;
1563 /* Return TRUE if X refers for a hard register from SET. */
1564 static bool
1565 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1567 int i, j, x_hard_regno;
1568 machine_mode mode;
1569 const char *fmt;
1570 enum rtx_code code;
1572 if (x == NULL_RTX)
1573 return false;
1574 code = GET_CODE (x);
1575 mode = GET_MODE (x);
1576 if (code == SUBREG)
1578 x = SUBREG_REG (x);
1579 code = GET_CODE (x);
1580 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1581 mode = GET_MODE (x);
1584 if (REG_P (x))
1586 x_hard_regno = get_hard_regno (x);
1587 return (x_hard_regno >= 0
1588 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1590 if (MEM_P (x))
1592 struct address_info ad;
1594 decompose_mem_address (&ad, x);
1595 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1596 return true;
1597 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1598 return true;
1600 fmt = GET_RTX_FORMAT (code);
1601 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1603 if (fmt[i] == 'e')
1605 if (uses_hard_regs_p (XEXP (x, i), set))
1606 return true;
1608 else if (fmt[i] == 'E')
1610 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1611 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1612 return true;
1615 return false;
1618 /* Return true if OP is a spilled pseudo. */
1619 static inline bool
1620 spilled_pseudo_p (rtx op)
1622 return (REG_P (op)
1623 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1626 /* Return true if X is a general constant. */
1627 static inline bool
1628 general_constant_p (rtx x)
1630 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1633 static bool
1634 reg_in_class_p (rtx reg, enum reg_class cl)
1636 if (cl == NO_REGS)
1637 return get_reg_class (REGNO (reg)) == NO_REGS;
1638 return in_class_p (reg, cl, NULL);
1641 /* Return true if SET of RCLASS contains no hard regs which can be
1642 used in MODE. */
1643 static bool
1644 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1645 HARD_REG_SET &set,
1646 enum machine_mode mode)
1648 HARD_REG_SET temp;
1650 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1651 COPY_HARD_REG_SET (temp, set);
1652 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1653 return (hard_reg_set_subset_p
1654 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1657 /* Major function to choose the current insn alternative and what
1658 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1659 negative we should consider only this alternative. Return false if
1660 we can not choose the alternative or find how to reload the
1661 operands. */
1662 static bool
1663 process_alt_operands (int only_alternative)
1665 bool ok_p = false;
1666 int nop, overall, nalt;
1667 int n_alternatives = curr_static_id->n_alternatives;
1668 int n_operands = curr_static_id->n_operands;
1669 /* LOSERS counts the operands that don't fit this alternative and
1670 would require loading. */
1671 int losers;
1672 /* REJECT is a count of how undesirable this alternative says it is
1673 if any reloading is required. If the alternative matches exactly
1674 then REJECT is ignored, but otherwise it gets this much counted
1675 against it in addition to the reloading needed. */
1676 int reject;
1677 int op_reject;
1678 /* The number of elements in the following array. */
1679 int early_clobbered_regs_num;
1680 /* Numbers of operands which are early clobber registers. */
1681 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1682 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1683 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1684 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1685 bool curr_alt_win[MAX_RECOG_OPERANDS];
1686 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1687 int curr_alt_matches[MAX_RECOG_OPERANDS];
1688 /* The number of elements in the following array. */
1689 int curr_alt_dont_inherit_ops_num;
1690 /* Numbers of operands whose reload pseudos should not be inherited. */
1691 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1692 rtx op;
1693 /* The register when the operand is a subreg of register, otherwise the
1694 operand itself. */
1695 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1696 /* The register if the operand is a register or subreg of register,
1697 otherwise NULL. */
1698 rtx operand_reg[MAX_RECOG_OPERANDS];
1699 int hard_regno[MAX_RECOG_OPERANDS];
1700 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1701 int reload_nregs, reload_sum;
1702 bool costly_p;
1703 enum reg_class cl;
1705 /* Calculate some data common for all alternatives to speed up the
1706 function. */
1707 for (nop = 0; nop < n_operands; nop++)
1709 rtx reg;
1711 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1712 /* The real hard regno of the operand after the allocation. */
1713 hard_regno[nop] = get_hard_regno (op);
1715 operand_reg[nop] = reg = op;
1716 biggest_mode[nop] = GET_MODE (op);
1717 if (GET_CODE (op) == SUBREG)
1719 operand_reg[nop] = reg = SUBREG_REG (op);
1720 if (GET_MODE_SIZE (biggest_mode[nop])
1721 < GET_MODE_SIZE (GET_MODE (reg)))
1722 biggest_mode[nop] = GET_MODE (reg);
1724 if (! REG_P (reg))
1725 operand_reg[nop] = NULL_RTX;
1726 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1727 || ((int) REGNO (reg)
1728 == lra_get_elimination_hard_regno (REGNO (reg))))
1729 no_subreg_reg_operand[nop] = reg;
1730 else
1731 operand_reg[nop] = no_subreg_reg_operand[nop]
1732 /* Just use natural mode for elimination result. It should
1733 be enough for extra constraints hooks. */
1734 = regno_reg_rtx[hard_regno[nop]];
1737 /* The constraints are made of several alternatives. Each operand's
1738 constraint looks like foo,bar,... with commas separating the
1739 alternatives. The first alternatives for all operands go
1740 together, the second alternatives go together, etc.
1742 First loop over alternatives. */
1743 alternative_mask preferred = curr_id->preferred_alternatives;
1744 if (only_alternative >= 0)
1745 preferred &= ALTERNATIVE_BIT (only_alternative);
1747 for (nalt = 0; nalt < n_alternatives; nalt++)
1749 /* Loop over operands for one constraint alternative. */
1750 if (!TEST_BIT (preferred, nalt))
1751 continue;
1753 overall = losers = reject = reload_nregs = reload_sum = 0;
1754 for (nop = 0; nop < n_operands; nop++)
1756 int inc = (curr_static_id
1757 ->operand_alternative[nalt * n_operands + nop].reject);
1758 if (lra_dump_file != NULL && inc != 0)
1759 fprintf (lra_dump_file,
1760 " Staticly defined alt reject+=%d\n", inc);
1761 reject += inc;
1763 early_clobbered_regs_num = 0;
1765 for (nop = 0; nop < n_operands; nop++)
1767 const char *p;
1768 char *end;
1769 int len, c, m, i, opalt_num, this_alternative_matches;
1770 bool win, did_match, offmemok, early_clobber_p;
1771 /* false => this operand can be reloaded somehow for this
1772 alternative. */
1773 bool badop;
1774 /* true => this operand can be reloaded if the alternative
1775 allows regs. */
1776 bool winreg;
1777 /* True if a constant forced into memory would be OK for
1778 this operand. */
1779 bool constmemok;
1780 enum reg_class this_alternative, this_costly_alternative;
1781 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1782 bool this_alternative_match_win, this_alternative_win;
1783 bool this_alternative_offmemok;
1784 bool scratch_p;
1785 machine_mode mode;
1786 enum constraint_num cn;
1788 opalt_num = nalt * n_operands + nop;
1789 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1791 /* Fast track for no constraints at all. */
1792 curr_alt[nop] = NO_REGS;
1793 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1794 curr_alt_win[nop] = true;
1795 curr_alt_match_win[nop] = false;
1796 curr_alt_offmemok[nop] = false;
1797 curr_alt_matches[nop] = -1;
1798 continue;
1801 op = no_subreg_reg_operand[nop];
1802 mode = curr_operand_mode[nop];
1804 win = did_match = winreg = offmemok = constmemok = false;
1805 badop = true;
1807 early_clobber_p = false;
1808 p = curr_static_id->operand_alternative[opalt_num].constraint;
1810 this_costly_alternative = this_alternative = NO_REGS;
1811 /* We update set of possible hard regs besides its class
1812 because reg class might be inaccurate. For example,
1813 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1814 is translated in HI_REGS because classes are merged by
1815 pairs and there is no accurate intermediate class. */
1816 CLEAR_HARD_REG_SET (this_alternative_set);
1817 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1818 this_alternative_win = false;
1819 this_alternative_match_win = false;
1820 this_alternative_offmemok = false;
1821 this_alternative_matches = -1;
1823 /* An empty constraint should be excluded by the fast
1824 track. */
1825 lra_assert (*p != 0 && *p != ',');
1827 op_reject = 0;
1828 /* Scan this alternative's specs for this operand; set WIN
1829 if the operand fits any letter in this alternative.
1830 Otherwise, clear BADOP if this operand could fit some
1831 letter after reloads, or set WINREG if this operand could
1832 fit after reloads provided the constraint allows some
1833 registers. */
1834 costly_p = false;
1837 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1839 case '\0':
1840 len = 0;
1841 break;
1842 case ',':
1843 c = '\0';
1844 break;
1846 case '&':
1847 early_clobber_p = true;
1848 break;
1850 case '$':
1851 op_reject += LRA_MAX_REJECT;
1852 break;
1853 case '^':
1854 op_reject += LRA_LOSER_COST_FACTOR;
1855 break;
1857 case '#':
1858 /* Ignore rest of this alternative. */
1859 c = '\0';
1860 break;
1862 case '0': case '1': case '2': case '3': case '4':
1863 case '5': case '6': case '7': case '8': case '9':
1865 int m_hregno;
1866 bool match_p;
1868 m = strtoul (p, &end, 10);
1869 p = end;
1870 len = 0;
1871 lra_assert (nop > m);
1873 this_alternative_matches = m;
1874 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1875 /* We are supposed to match a previous operand.
1876 If we do, we win if that one did. If we do
1877 not, count both of the operands as losers.
1878 (This is too conservative, since most of the
1879 time only a single reload insn will be needed
1880 to make the two operands win. As a result,
1881 this alternative may be rejected when it is
1882 actually desirable.) */
1883 match_p = false;
1884 if (operands_match_p (*curr_id->operand_loc[nop],
1885 *curr_id->operand_loc[m], m_hregno))
1887 /* We should reject matching of an early
1888 clobber operand if the matching operand is
1889 not dying in the insn. */
1890 if (! curr_static_id->operand[m].early_clobber
1891 || operand_reg[nop] == NULL_RTX
1892 || (find_regno_note (curr_insn, REG_DEAD,
1893 REGNO (op))
1894 || REGNO (op) == REGNO (operand_reg[m])))
1895 match_p = true;
1897 if (match_p)
1899 /* If we are matching a non-offsettable
1900 address where an offsettable address was
1901 expected, then we must reject this
1902 combination, because we can't reload
1903 it. */
1904 if (curr_alt_offmemok[m]
1905 && MEM_P (*curr_id->operand_loc[m])
1906 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1907 continue;
1909 else
1911 /* Operands don't match. Both operands must
1912 allow a reload register, otherwise we
1913 cannot make them match. */
1914 if (curr_alt[m] == NO_REGS)
1915 break;
1916 /* Retroactively mark the operand we had to
1917 match as a loser, if it wasn't already and
1918 it wasn't matched to a register constraint
1919 (e.g it might be matched by memory). */
1920 if (curr_alt_win[m]
1921 && (operand_reg[m] == NULL_RTX
1922 || hard_regno[m] < 0))
1924 losers++;
1925 reload_nregs
1926 += (ira_reg_class_max_nregs[curr_alt[m]]
1927 [GET_MODE (*curr_id->operand_loc[m])]);
1930 /* Prefer matching earlyclobber alternative as
1931 it results in less hard regs required for
1932 the insn than a non-matching earlyclobber
1933 alternative. */
1934 if (curr_static_id->operand[m].early_clobber)
1936 if (lra_dump_file != NULL)
1937 fprintf
1938 (lra_dump_file,
1939 " %d Matching earlyclobber alt:"
1940 " reject--\n",
1941 nop);
1942 reject--;
1944 /* Otherwise we prefer no matching
1945 alternatives because it gives more freedom
1946 in RA. */
1947 else if (operand_reg[nop] == NULL_RTX
1948 || (find_regno_note (curr_insn, REG_DEAD,
1949 REGNO (operand_reg[nop]))
1950 == NULL_RTX))
1952 if (lra_dump_file != NULL)
1953 fprintf
1954 (lra_dump_file,
1955 " %d Matching alt: reject+=2\n",
1956 nop);
1957 reject += 2;
1960 /* If we have to reload this operand and some
1961 previous operand also had to match the same
1962 thing as this operand, we don't know how to do
1963 that. */
1964 if (!match_p || !curr_alt_win[m])
1966 for (i = 0; i < nop; i++)
1967 if (curr_alt_matches[i] == m)
1968 break;
1969 if (i < nop)
1970 break;
1972 else
1973 did_match = true;
1975 /* This can be fixed with reloads if the operand
1976 we are supposed to match can be fixed with
1977 reloads. */
1978 badop = false;
1979 this_alternative = curr_alt[m];
1980 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1981 winreg = this_alternative != NO_REGS;
1982 break;
1985 case 'g':
1986 if (MEM_P (op)
1987 || general_constant_p (op)
1988 || spilled_pseudo_p (op))
1989 win = true;
1990 cl = GENERAL_REGS;
1991 goto reg;
1993 default:
1994 cn = lookup_constraint (p);
1995 switch (get_constraint_type (cn))
1997 case CT_REGISTER:
1998 cl = reg_class_for_constraint (cn);
1999 if (cl != NO_REGS)
2000 goto reg;
2001 break;
2003 case CT_CONST_INT:
2004 if (CONST_INT_P (op)
2005 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2006 win = true;
2007 break;
2009 case CT_MEMORY:
2010 if (MEM_P (op)
2011 && satisfies_memory_constraint_p (op, cn))
2012 win = true;
2013 else if (spilled_pseudo_p (op))
2014 win = true;
2016 /* If we didn't already win, we can reload constants
2017 via force_const_mem or put the pseudo value into
2018 memory, or make other memory by reloading the
2019 address like for 'o'. */
2020 if (CONST_POOL_OK_P (mode, op)
2021 || MEM_P (op) || REG_P (op))
2022 badop = false;
2023 constmemok = true;
2024 offmemok = true;
2025 break;
2027 case CT_ADDRESS:
2028 /* If we didn't already win, we can reload the address
2029 into a base register. */
2030 if (satisfies_address_constraint_p (op, cn))
2031 win = true;
2032 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2033 ADDRESS, SCRATCH);
2034 badop = false;
2035 goto reg;
2037 case CT_FIXED_FORM:
2038 if (constraint_satisfied_p (op, cn))
2039 win = true;
2040 break;
2042 break;
2044 reg:
2045 this_alternative = reg_class_subunion[this_alternative][cl];
2046 IOR_HARD_REG_SET (this_alternative_set,
2047 reg_class_contents[cl]);
2048 if (costly_p)
2050 this_costly_alternative
2051 = reg_class_subunion[this_costly_alternative][cl];
2052 IOR_HARD_REG_SET (this_costly_alternative_set,
2053 reg_class_contents[cl]);
2055 if (mode == BLKmode)
2056 break;
2057 winreg = true;
2058 if (REG_P (op))
2060 if (hard_regno[nop] >= 0
2061 && in_hard_reg_set_p (this_alternative_set,
2062 mode, hard_regno[nop]))
2063 win = true;
2064 else if (hard_regno[nop] < 0
2065 && in_class_p (op, this_alternative, NULL))
2066 win = true;
2068 break;
2070 if (c != ' ' && c != '\t')
2071 costly_p = c == '*';
2073 while ((p += len), c);
2075 scratch_p = (operand_reg[nop] != NULL_RTX
2076 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2077 /* Record which operands fit this alternative. */
2078 if (win)
2080 this_alternative_win = true;
2081 if (operand_reg[nop] != NULL_RTX)
2083 if (hard_regno[nop] >= 0)
2085 if (in_hard_reg_set_p (this_costly_alternative_set,
2086 mode, hard_regno[nop]))
2088 if (lra_dump_file != NULL)
2089 fprintf (lra_dump_file,
2090 " %d Costly set: reject++\n",
2091 nop);
2092 reject++;
2095 else
2097 /* Prefer won reg to spilled pseudo under other
2098 equal conditions for possibe inheritance. */
2099 if (! scratch_p)
2101 if (lra_dump_file != NULL)
2102 fprintf
2103 (lra_dump_file,
2104 " %d Non pseudo reload: reject++\n",
2105 nop);
2106 reject++;
2108 if (in_class_p (operand_reg[nop],
2109 this_costly_alternative, NULL))
2111 if (lra_dump_file != NULL)
2112 fprintf
2113 (lra_dump_file,
2114 " %d Non pseudo costly reload:"
2115 " reject++\n",
2116 nop);
2117 reject++;
2120 /* We simulate the behaviour of old reload here.
2121 Although scratches need hard registers and it
2122 might result in spilling other pseudos, no reload
2123 insns are generated for the scratches. So it
2124 might cost something but probably less than old
2125 reload pass believes. */
2126 if (scratch_p)
2128 if (lra_dump_file != NULL)
2129 fprintf (lra_dump_file,
2130 " %d Scratch win: reject+=2\n",
2131 nop);
2132 reject += 2;
2136 else if (did_match)
2137 this_alternative_match_win = true;
2138 else
2140 int const_to_mem = 0;
2141 bool no_regs_p;
2143 reject += op_reject;
2144 /* Never do output reload of stack pointer. It makes
2145 impossible to do elimination when SP is changed in
2146 RTL. */
2147 if (op == stack_pointer_rtx && ! frame_pointer_needed
2148 && curr_static_id->operand[nop].type != OP_IN)
2149 goto fail;
2151 /* If this alternative asks for a specific reg class, see if there
2152 is at least one allocatable register in that class. */
2153 no_regs_p
2154 = (this_alternative == NO_REGS
2155 || (hard_reg_set_subset_p
2156 (reg_class_contents[this_alternative],
2157 lra_no_alloc_regs)));
2159 /* For asms, verify that the class for this alternative is possible
2160 for the mode that is specified. */
2161 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2163 int i;
2164 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2165 if (HARD_REGNO_MODE_OK (i, mode)
2166 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2167 mode, i))
2168 break;
2169 if (i == FIRST_PSEUDO_REGISTER)
2170 winreg = false;
2173 /* If this operand accepts a register, and if the
2174 register class has at least one allocatable register,
2175 then this operand can be reloaded. */
2176 if (winreg && !no_regs_p)
2177 badop = false;
2179 if (badop)
2181 if (lra_dump_file != NULL)
2182 fprintf (lra_dump_file,
2183 " alt=%d: Bad operand -- refuse\n",
2184 nalt);
2185 goto fail;
2188 /* If not assigned pseudo has a class which a subset of
2189 required reg class, it is a less costly alternative
2190 as the pseudo still can get a hard reg of necessary
2191 class. */
2192 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2193 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2194 && ira_class_subset_p[this_alternative][cl])
2196 if (lra_dump_file != NULL)
2197 fprintf
2198 (lra_dump_file,
2199 " %d Super set class reg: reject-=3\n", nop);
2200 reject -= 3;
2203 this_alternative_offmemok = offmemok;
2204 if (this_costly_alternative != NO_REGS)
2206 if (lra_dump_file != NULL)
2207 fprintf (lra_dump_file,
2208 " %d Costly loser: reject++\n", nop);
2209 reject++;
2211 /* If the operand is dying, has a matching constraint,
2212 and satisfies constraints of the matched operand
2213 which failed to satisfy the own constraints, most probably
2214 the reload for this operand will be gone. */
2215 if (this_alternative_matches >= 0
2216 && !curr_alt_win[this_alternative_matches]
2217 && REG_P (op)
2218 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2219 && (hard_regno[nop] >= 0
2220 ? in_hard_reg_set_p (this_alternative_set,
2221 mode, hard_regno[nop])
2222 : in_class_p (op, this_alternative, NULL)))
2224 if (lra_dump_file != NULL)
2225 fprintf
2226 (lra_dump_file,
2227 " %d Dying matched operand reload: reject++\n",
2228 nop);
2229 reject++;
2231 else
2233 /* Strict_low_part requires to reload the register
2234 not the sub-register. In this case we should
2235 check that a final reload hard reg can hold the
2236 value mode. */
2237 if (curr_static_id->operand[nop].strict_low
2238 && REG_P (op)
2239 && hard_regno[nop] < 0
2240 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2241 && ira_class_hard_regs_num[this_alternative] > 0
2242 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2243 [this_alternative][0],
2244 GET_MODE
2245 (*curr_id->operand_loc[nop])))
2247 if (lra_dump_file != NULL)
2248 fprintf
2249 (lra_dump_file,
2250 " alt=%d: Strict low subreg reload -- refuse\n",
2251 nalt);
2252 goto fail;
2254 losers++;
2256 if (operand_reg[nop] != NULL_RTX
2257 /* Output operands and matched input operands are
2258 not inherited. The following conditions do not
2259 exactly describe the previous statement but they
2260 are pretty close. */
2261 && curr_static_id->operand[nop].type != OP_OUT
2262 && (this_alternative_matches < 0
2263 || curr_static_id->operand[nop].type != OP_IN))
2265 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2266 (operand_reg[nop])]
2267 .last_reload);
2269 /* The value of reload_sum has sense only if we
2270 process insns in their order. It happens only on
2271 the first constraints sub-pass when we do most of
2272 reload work. */
2273 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2274 reload_sum += last_reload - bb_reload_num;
2276 /* If this is a constant that is reloaded into the
2277 desired class by copying it to memory first, count
2278 that as another reload. This is consistent with
2279 other code and is required to avoid choosing another
2280 alternative when the constant is moved into memory.
2281 Note that the test here is precisely the same as in
2282 the code below that calls force_const_mem. */
2283 if (CONST_POOL_OK_P (mode, op)
2284 && ((targetm.preferred_reload_class
2285 (op, this_alternative) == NO_REGS)
2286 || no_input_reloads_p))
2288 const_to_mem = 1;
2289 if (! no_regs_p)
2290 losers++;
2293 /* Alternative loses if it requires a type of reload not
2294 permitted for this insn. We can always reload
2295 objects with a REG_UNUSED note. */
2296 if ((curr_static_id->operand[nop].type != OP_IN
2297 && no_output_reloads_p
2298 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2299 || (curr_static_id->operand[nop].type != OP_OUT
2300 && no_input_reloads_p && ! const_to_mem)
2301 || (this_alternative_matches >= 0
2302 && (no_input_reloads_p
2303 || (no_output_reloads_p
2304 && (curr_static_id->operand
2305 [this_alternative_matches].type != OP_IN)
2306 && ! find_reg_note (curr_insn, REG_UNUSED,
2307 no_subreg_reg_operand
2308 [this_alternative_matches])))))
2310 if (lra_dump_file != NULL)
2311 fprintf
2312 (lra_dump_file,
2313 " alt=%d: No input/otput reload -- refuse\n",
2314 nalt);
2315 goto fail;
2318 /* Alternative loses if it required class pseudo can not
2319 hold value of required mode. Such insns can be
2320 described by insn definitions with mode iterators. */
2321 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2322 && ! hard_reg_set_empty_p (this_alternative_set)
2323 /* It is common practice for constraints to use a
2324 class which does not have actually enough regs to
2325 hold the value (e.g. x86 AREG for mode requiring
2326 more one general reg). Therefore we have 2
2327 conditions to check that the reload pseudo can
2328 not hold the mode value. */
2329 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2330 [this_alternative][0],
2331 GET_MODE (*curr_id->operand_loc[nop]))
2332 /* The above condition is not enough as the first
2333 reg in ira_class_hard_regs can be not aligned for
2334 multi-words mode values. */
2335 && (prohibited_class_reg_set_mode_p
2336 (this_alternative, this_alternative_set,
2337 GET_MODE (*curr_id->operand_loc[nop]))))
2339 if (lra_dump_file != NULL)
2340 fprintf (lra_dump_file,
2341 " alt=%d: reload pseudo for op %d "
2342 " can not hold the mode value -- refuse\n",
2343 nalt, nop);
2344 goto fail;
2347 /* Check strong discouragement of reload of non-constant
2348 into class THIS_ALTERNATIVE. */
2349 if (! CONSTANT_P (op) && ! no_regs_p
2350 && (targetm.preferred_reload_class
2351 (op, this_alternative) == NO_REGS
2352 || (curr_static_id->operand[nop].type == OP_OUT
2353 && (targetm.preferred_output_reload_class
2354 (op, this_alternative) == NO_REGS))))
2356 if (lra_dump_file != NULL)
2357 fprintf (lra_dump_file,
2358 " %d Non-prefered reload: reject+=%d\n",
2359 nop, LRA_MAX_REJECT);
2360 reject += LRA_MAX_REJECT;
2363 if (! (MEM_P (op) && offmemok)
2364 && ! (const_to_mem && constmemok))
2366 /* We prefer to reload pseudos over reloading other
2367 things, since such reloads may be able to be
2368 eliminated later. So bump REJECT in other cases.
2369 Don't do this in the case where we are forcing a
2370 constant into memory and it will then win since
2371 we don't want to have a different alternative
2372 match then. */
2373 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2375 if (lra_dump_file != NULL)
2376 fprintf
2377 (lra_dump_file,
2378 " %d Non-pseudo reload: reject+=2\n",
2379 nop);
2380 reject += 2;
2383 if (! no_regs_p)
2384 reload_nregs
2385 += ira_reg_class_max_nregs[this_alternative][mode];
2387 if (SMALL_REGISTER_CLASS_P (this_alternative))
2389 if (lra_dump_file != NULL)
2390 fprintf
2391 (lra_dump_file,
2392 " %d Small class reload: reject+=%d\n",
2393 nop, LRA_LOSER_COST_FACTOR / 2);
2394 reject += LRA_LOSER_COST_FACTOR / 2;
2398 /* We are trying to spill pseudo into memory. It is
2399 usually more costly than moving to a hard register
2400 although it might takes the same number of
2401 reloads. */
2402 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2404 if (lra_dump_file != NULL)
2405 fprintf
2406 (lra_dump_file,
2407 " %d Spill pseudo into memory: reject+=3\n",
2408 nop);
2409 reject += 3;
2410 if (VECTOR_MODE_P (mode))
2412 /* Spilling vectors into memory is usually more
2413 costly as they contain big values. */
2414 if (lra_dump_file != NULL)
2415 fprintf
2416 (lra_dump_file,
2417 " %d Spill vector pseudo: reject+=2\n",
2418 nop);
2419 reject += 2;
2423 #ifdef SECONDARY_MEMORY_NEEDED
2424 /* If reload requires moving value through secondary
2425 memory, it will need one more insn at least. */
2426 if (this_alternative != NO_REGS
2427 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2428 && ((curr_static_id->operand[nop].type != OP_OUT
2429 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2430 GET_MODE (op)))
2431 || (curr_static_id->operand[nop].type != OP_IN
2432 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2433 GET_MODE (op)))))
2434 losers++;
2435 #endif
2436 /* Input reloads can be inherited more often than output
2437 reloads can be removed, so penalize output
2438 reloads. */
2439 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2441 if (lra_dump_file != NULL)
2442 fprintf
2443 (lra_dump_file,
2444 " %d Non input pseudo reload: reject++\n",
2445 nop);
2446 reject++;
2450 if (early_clobber_p && ! scratch_p)
2452 if (lra_dump_file != NULL)
2453 fprintf (lra_dump_file,
2454 " %d Early clobber: reject++\n", nop);
2455 reject++;
2457 /* ??? We check early clobbers after processing all operands
2458 (see loop below) and there we update the costs more.
2459 Should we update the cost (may be approximately) here
2460 because of early clobber register reloads or it is a rare
2461 or non-important thing to be worth to do it. */
2462 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2463 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2465 if (lra_dump_file != NULL)
2466 fprintf (lra_dump_file,
2467 " alt=%d,overall=%d,losers=%d -- refuse\n",
2468 nalt, overall, losers);
2469 goto fail;
2472 curr_alt[nop] = this_alternative;
2473 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2474 curr_alt_win[nop] = this_alternative_win;
2475 curr_alt_match_win[nop] = this_alternative_match_win;
2476 curr_alt_offmemok[nop] = this_alternative_offmemok;
2477 curr_alt_matches[nop] = this_alternative_matches;
2479 if (this_alternative_matches >= 0
2480 && !did_match && !this_alternative_win)
2481 curr_alt_win[this_alternative_matches] = false;
2483 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2484 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2486 if (curr_insn_set != NULL_RTX && n_operands == 2
2487 /* Prevent processing non-move insns. */
2488 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2489 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2490 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2491 && REG_P (no_subreg_reg_operand[0])
2492 && REG_P (no_subreg_reg_operand[1])
2493 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2494 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2495 || (! curr_alt_win[0] && curr_alt_win[1]
2496 && REG_P (no_subreg_reg_operand[1])
2497 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2498 || (curr_alt_win[0] && ! curr_alt_win[1]
2499 && REG_P (no_subreg_reg_operand[0])
2500 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2501 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2502 no_subreg_reg_operand[1])
2503 || (targetm.preferred_reload_class
2504 (no_subreg_reg_operand[1],
2505 (enum reg_class) curr_alt[1]) != NO_REGS))
2506 /* If it is a result of recent elimination in move
2507 insn we can transform it into an add still by
2508 using this alternative. */
2509 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2511 /* We have a move insn and a new reload insn will be similar
2512 to the current insn. We should avoid such situation as it
2513 results in LRA cycling. */
2514 overall += LRA_MAX_REJECT;
2516 ok_p = true;
2517 curr_alt_dont_inherit_ops_num = 0;
2518 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2520 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2521 HARD_REG_SET temp_set;
2523 i = early_clobbered_nops[nop];
2524 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2525 || hard_regno[i] < 0)
2526 continue;
2527 lra_assert (operand_reg[i] != NULL_RTX);
2528 clobbered_hard_regno = hard_regno[i];
2529 CLEAR_HARD_REG_SET (temp_set);
2530 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2531 first_conflict_j = last_conflict_j = -1;
2532 for (j = 0; j < n_operands; j++)
2533 if (j == i
2534 /* We don't want process insides of match_operator and
2535 match_parallel because otherwise we would process
2536 their operands once again generating a wrong
2537 code. */
2538 || curr_static_id->operand[j].is_operator)
2539 continue;
2540 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2541 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2542 continue;
2543 /* If we don't reload j-th operand, check conflicts. */
2544 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2545 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2547 if (first_conflict_j < 0)
2548 first_conflict_j = j;
2549 last_conflict_j = j;
2551 if (last_conflict_j < 0)
2552 continue;
2553 /* If earlyclobber operand conflicts with another
2554 non-matching operand which is actually the same register
2555 as the earlyclobber operand, it is better to reload the
2556 another operand as an operand matching the earlyclobber
2557 operand can be also the same. */
2558 if (first_conflict_j == last_conflict_j
2559 && operand_reg[last_conflict_j]
2560 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2561 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2563 curr_alt_win[last_conflict_j] = false;
2564 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2565 = last_conflict_j;
2566 losers++;
2567 /* Early clobber was already reflected in REJECT. */
2568 lra_assert (reject > 0);
2569 if (lra_dump_file != NULL)
2570 fprintf
2571 (lra_dump_file,
2572 " %d Conflict early clobber reload: reject--\n",
2574 reject--;
2575 overall += LRA_LOSER_COST_FACTOR - 1;
2577 else
2579 /* We need to reload early clobbered register and the
2580 matched registers. */
2581 for (j = 0; j < n_operands; j++)
2582 if (curr_alt_matches[j] == i)
2584 curr_alt_match_win[j] = false;
2585 losers++;
2586 overall += LRA_LOSER_COST_FACTOR;
2588 if (! curr_alt_match_win[i])
2589 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2590 else
2592 /* Remember pseudos used for match reloads are never
2593 inherited. */
2594 lra_assert (curr_alt_matches[i] >= 0);
2595 curr_alt_win[curr_alt_matches[i]] = false;
2597 curr_alt_win[i] = curr_alt_match_win[i] = false;
2598 losers++;
2599 /* Early clobber was already reflected in REJECT. */
2600 lra_assert (reject > 0);
2601 if (lra_dump_file != NULL)
2602 fprintf
2603 (lra_dump_file,
2604 " %d Matched conflict early clobber reloads:"
2605 "reject--\n",
2607 reject--;
2608 overall += LRA_LOSER_COST_FACTOR - 1;
2611 if (lra_dump_file != NULL)
2612 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2613 nalt, overall, losers, reload_nregs);
2615 /* If this alternative can be made to work by reloading, and it
2616 needs less reloading than the others checked so far, record
2617 it as the chosen goal for reloading. */
2618 if ((best_losers != 0 && losers == 0)
2619 || (((best_losers == 0 && losers == 0)
2620 || (best_losers != 0 && losers != 0))
2621 && (best_overall > overall
2622 || (best_overall == overall
2623 /* If the cost of the reloads is the same,
2624 prefer alternative which requires minimal
2625 number of reload regs. */
2626 && (reload_nregs < best_reload_nregs
2627 || (reload_nregs == best_reload_nregs
2628 && (best_reload_sum < reload_sum
2629 || (best_reload_sum == reload_sum
2630 && nalt < goal_alt_number))))))))
2632 for (nop = 0; nop < n_operands; nop++)
2634 goal_alt_win[nop] = curr_alt_win[nop];
2635 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2636 goal_alt_matches[nop] = curr_alt_matches[nop];
2637 goal_alt[nop] = curr_alt[nop];
2638 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2640 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2641 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2642 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2643 goal_alt_swapped = curr_swapped;
2644 best_overall = overall;
2645 best_losers = losers;
2646 best_reload_nregs = reload_nregs;
2647 best_reload_sum = reload_sum;
2648 goal_alt_number = nalt;
2650 if (losers == 0)
2651 /* Everything is satisfied. Do not process alternatives
2652 anymore. */
2653 break;
2654 fail:
2657 return ok_p;
2660 /* Make reload base reg from address AD. */
2661 static rtx
2662 base_to_reg (struct address_info *ad)
2664 enum reg_class cl;
2665 int code = -1;
2666 rtx new_inner = NULL_RTX;
2667 rtx new_reg = NULL_RTX;
2668 rtx_insn *insn;
2669 rtx_insn *last_insn = get_last_insn();
2671 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2672 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2673 get_index_code (ad));
2674 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2675 cl, "base");
2676 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2677 ad->disp_term == NULL
2678 ? gen_int_mode (0, ad->mode)
2679 : *ad->disp_term);
2680 if (!valid_address_p (ad->mode, new_inner, ad->as))
2681 return NULL_RTX;
2682 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base_term));
2683 code = recog_memoized (insn);
2684 if (code < 0)
2686 delete_insns_since (last_insn);
2687 return NULL_RTX;
2690 return new_inner;
2693 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2694 static rtx
2695 base_plus_disp_to_reg (struct address_info *ad)
2697 enum reg_class cl;
2698 rtx new_reg;
2700 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2701 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2702 get_index_code (ad));
2703 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2704 cl, "base + disp");
2705 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2706 return new_reg;
2709 /* Make reload of index part of address AD. Return the new
2710 pseudo. */
2711 static rtx
2712 index_part_to_reg (struct address_info *ad)
2714 rtx new_reg;
2716 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2717 INDEX_REG_CLASS, "index term");
2718 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2719 GEN_INT (get_index_scale (ad)), new_reg, 1);
2720 return new_reg;
2723 /* Return true if we can add a displacement to address AD, even if that
2724 makes the address invalid. The fix-up code requires any new address
2725 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2726 static bool
2727 can_add_disp_p (struct address_info *ad)
2729 return (!ad->autoinc_p
2730 && ad->segment == NULL
2731 && ad->base == ad->base_term
2732 && ad->disp == ad->disp_term);
2735 /* Make equiv substitution in address AD. Return true if a substitution
2736 was made. */
2737 static bool
2738 equiv_address_substitution (struct address_info *ad)
2740 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2741 HOST_WIDE_INT disp, scale;
2742 bool change_p;
2744 base_term = strip_subreg (ad->base_term);
2745 if (base_term == NULL)
2746 base_reg = new_base_reg = NULL_RTX;
2747 else
2749 base_reg = *base_term;
2750 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2752 index_term = strip_subreg (ad->index_term);
2753 if (index_term == NULL)
2754 index_reg = new_index_reg = NULL_RTX;
2755 else
2757 index_reg = *index_term;
2758 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2760 if (base_reg == new_base_reg && index_reg == new_index_reg)
2761 return false;
2762 disp = 0;
2763 change_p = false;
2764 if (lra_dump_file != NULL)
2766 fprintf (lra_dump_file, "Changing address in insn %d ",
2767 INSN_UID (curr_insn));
2768 dump_value_slim (lra_dump_file, *ad->outer, 1);
2770 if (base_reg != new_base_reg)
2772 if (REG_P (new_base_reg))
2774 *base_term = new_base_reg;
2775 change_p = true;
2777 else if (GET_CODE (new_base_reg) == PLUS
2778 && REG_P (XEXP (new_base_reg, 0))
2779 && CONST_INT_P (XEXP (new_base_reg, 1))
2780 && can_add_disp_p (ad))
2782 disp += INTVAL (XEXP (new_base_reg, 1));
2783 *base_term = XEXP (new_base_reg, 0);
2784 change_p = true;
2786 if (ad->base_term2 != NULL)
2787 *ad->base_term2 = *ad->base_term;
2789 if (index_reg != new_index_reg)
2791 if (REG_P (new_index_reg))
2793 *index_term = new_index_reg;
2794 change_p = true;
2796 else if (GET_CODE (new_index_reg) == PLUS
2797 && REG_P (XEXP (new_index_reg, 0))
2798 && CONST_INT_P (XEXP (new_index_reg, 1))
2799 && can_add_disp_p (ad)
2800 && (scale = get_index_scale (ad)))
2802 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2803 *index_term = XEXP (new_index_reg, 0);
2804 change_p = true;
2807 if (disp != 0)
2809 if (ad->disp != NULL)
2810 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2811 else
2813 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2814 update_address (ad);
2816 change_p = true;
2818 if (lra_dump_file != NULL)
2820 if (! change_p)
2821 fprintf (lra_dump_file, " -- no change\n");
2822 else
2824 fprintf (lra_dump_file, " on equiv ");
2825 dump_value_slim (lra_dump_file, *ad->outer, 1);
2826 fprintf (lra_dump_file, "\n");
2829 return change_p;
2832 /* Major function to make reloads for an address in operand NOP or
2833 check its correctness (If CHECK_ONLY_P is true). The supported
2834 cases are:
2836 1) an address that existed before LRA started, at which point it
2837 must have been valid. These addresses are subject to elimination
2838 and may have become invalid due to the elimination offset being out
2839 of range.
2841 2) an address created by forcing a constant to memory
2842 (force_const_to_mem). The initial form of these addresses might
2843 not be valid, and it is this function's job to make them valid.
2845 3) a frame address formed from a register and a (possibly zero)
2846 constant offset. As above, these addresses might not be valid and
2847 this function must make them so.
2849 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2850 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2851 address. Return true for any RTL change.
2853 The function is a helper function which does not produce all
2854 transformations (when CHECK_ONLY_P is false) which can be
2855 necessary. It does just basic steps. To do all necessary
2856 transformations use function process_address. */
2857 static bool
2858 process_address_1 (int nop, bool check_only_p,
2859 rtx_insn **before, rtx_insn **after)
2861 struct address_info ad;
2862 rtx new_reg;
2863 rtx op = *curr_id->operand_loc[nop];
2864 const char *constraint = curr_static_id->operand[nop].constraint;
2865 enum constraint_num cn = lookup_constraint (constraint);
2866 bool change_p = false;
2868 if (insn_extra_address_constraint (cn))
2869 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2870 else if (MEM_P (op))
2871 decompose_mem_address (&ad, op);
2872 else if (GET_CODE (op) == SUBREG
2873 && MEM_P (SUBREG_REG (op)))
2874 decompose_mem_address (&ad, SUBREG_REG (op));
2875 else
2876 return false;
2877 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
2878 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
2879 when INDEX_REG_CLASS is a single register class. */
2880 if (ad.base_term != NULL
2881 && ad.index_term != NULL
2882 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
2883 && REG_P (*ad.base_term)
2884 && REG_P (*ad.index_term)
2885 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
2886 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
2888 std::swap (ad.base, ad.index);
2889 std::swap (ad.base_term, ad.index_term);
2891 if (! check_only_p)
2892 change_p = equiv_address_substitution (&ad);
2893 if (ad.base_term != NULL
2894 && (process_addr_reg
2895 (ad.base_term, check_only_p, before,
2896 (ad.autoinc_p
2897 && !(REG_P (*ad.base_term)
2898 && find_regno_note (curr_insn, REG_DEAD,
2899 REGNO (*ad.base_term)) != NULL_RTX)
2900 ? after : NULL),
2901 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2902 get_index_code (&ad)))))
2904 change_p = true;
2905 if (ad.base_term2 != NULL)
2906 *ad.base_term2 = *ad.base_term;
2908 if (ad.index_term != NULL
2909 && process_addr_reg (ad.index_term, check_only_p,
2910 before, NULL, INDEX_REG_CLASS))
2911 change_p = true;
2913 /* Target hooks sometimes don't treat extra-constraint addresses as
2914 legitimate address_operands, so handle them specially. */
2915 if (insn_extra_address_constraint (cn)
2916 && satisfies_address_constraint_p (&ad, cn))
2917 return change_p;
2919 if (check_only_p)
2920 return change_p;
2922 /* There are three cases where the shape of *AD.INNER may now be invalid:
2924 1) the original address was valid, but either elimination or
2925 equiv_address_substitution was applied and that made
2926 the address invalid.
2928 2) the address is an invalid symbolic address created by
2929 force_const_to_mem.
2931 3) the address is a frame address with an invalid offset.
2933 4) the address is a frame address with an invalid base.
2935 All these cases involve a non-autoinc address, so there is no
2936 point revalidating other types. */
2937 if (ad.autoinc_p || valid_address_p (&ad))
2938 return change_p;
2940 /* Any index existed before LRA started, so we can assume that the
2941 presence and shape of the index is valid. */
2942 push_to_sequence (*before);
2943 lra_assert (ad.disp == ad.disp_term);
2944 if (ad.base == NULL)
2946 if (ad.index == NULL)
2948 int code = -1;
2949 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2950 SCRATCH, SCRATCH);
2951 rtx addr = *ad.inner;
2953 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2954 if (HAVE_lo_sum)
2956 rtx_insn *insn;
2957 rtx_insn *last = get_last_insn ();
2959 /* addr => lo_sum (new_base, addr), case (2) above. */
2960 insn = emit_insn (gen_rtx_SET
2961 (new_reg,
2962 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2963 code = recog_memoized (insn);
2964 if (code >= 0)
2966 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2967 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2969 /* Try to put lo_sum into register. */
2970 insn = emit_insn (gen_rtx_SET
2971 (new_reg,
2972 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2973 code = recog_memoized (insn);
2974 if (code >= 0)
2976 *ad.inner = new_reg;
2977 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2979 *ad.inner = addr;
2980 code = -1;
2986 if (code < 0)
2987 delete_insns_since (last);
2990 if (code < 0)
2992 /* addr => new_base, case (2) above. */
2993 lra_emit_move (new_reg, addr);
2994 *ad.inner = new_reg;
2997 else
2999 /* index * scale + disp => new base + index * scale,
3000 case (1) above. */
3001 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3002 GET_CODE (*ad.index));
3004 lra_assert (INDEX_REG_CLASS != NO_REGS);
3005 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3006 lra_emit_move (new_reg, *ad.disp);
3007 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3008 new_reg, *ad.index);
3011 else if (ad.index == NULL)
3013 int regno;
3014 enum reg_class cl;
3015 rtx set;
3016 rtx_insn *insns, *last_insn;
3017 /* Try to reload base into register only if the base is invalid
3018 for the address but with valid offset, case (4) above. */
3019 start_sequence ();
3020 new_reg = base_to_reg (&ad);
3022 /* base + disp => new base, cases (1) and (3) above. */
3023 /* Another option would be to reload the displacement into an
3024 index register. However, postreload has code to optimize
3025 address reloads that have the same base and different
3026 displacements, so reloading into an index register would
3027 not necessarily be a win. */
3028 if (new_reg == NULL_RTX)
3029 new_reg = base_plus_disp_to_reg (&ad);
3030 insns = get_insns ();
3031 last_insn = get_last_insn ();
3032 /* If we generated at least two insns, try last insn source as
3033 an address. If we succeed, we generate one less insn. */
3034 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3035 && GET_CODE (SET_SRC (set)) == PLUS
3036 && REG_P (XEXP (SET_SRC (set), 0))
3037 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3039 *ad.inner = SET_SRC (set);
3040 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3042 *ad.base_term = XEXP (SET_SRC (set), 0);
3043 *ad.disp_term = XEXP (SET_SRC (set), 1);
3044 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3045 get_index_code (&ad));
3046 regno = REGNO (*ad.base_term);
3047 if (regno >= FIRST_PSEUDO_REGISTER
3048 && cl != lra_get_allocno_class (regno))
3049 lra_change_class (regno, cl, " Change to", true);
3050 new_reg = SET_SRC (set);
3051 delete_insns_since (PREV_INSN (last_insn));
3054 /* Try if target can split displacement into legitimite new disp
3055 and offset. If it's the case, we replace the last insn with
3056 insns for base + offset => new_reg and set new_reg + new disp
3057 to *ad.inner. */
3058 last_insn = get_last_insn ();
3059 if ((set = single_set (last_insn)) != NULL_RTX
3060 && GET_CODE (SET_SRC (set)) == PLUS
3061 && REG_P (XEXP (SET_SRC (set), 0))
3062 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3063 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3065 rtx addend, disp = XEXP (SET_SRC (set), 1);
3066 if (targetm.legitimize_address_displacement (&disp, &addend,
3067 ad.mode))
3069 rtx_insn *new_insns;
3070 start_sequence ();
3071 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3072 new_insns = get_insns ();
3073 end_sequence ();
3074 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3075 delete_insns_since (PREV_INSN (last_insn));
3076 add_insn (new_insns);
3077 insns = get_insns ();
3080 end_sequence ();
3081 emit_insn (insns);
3082 *ad.inner = new_reg;
3084 else if (ad.disp_term != NULL)
3086 /* base + scale * index + disp => new base + scale * index,
3087 case (1) above. */
3088 new_reg = base_plus_disp_to_reg (&ad);
3089 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3090 new_reg, *ad.index);
3092 else if (get_index_scale (&ad) == 1)
3094 /* The last transformation to one reg will be made in
3095 curr_insn_transform function. */
3096 end_sequence ();
3097 return false;
3099 else
3101 /* base + scale * index => base + new_reg,
3102 case (1) above.
3103 Index part of address may become invalid. For example, we
3104 changed pseudo on the equivalent memory and a subreg of the
3105 pseudo onto the memory of different mode for which the scale is
3106 prohibitted. */
3107 new_reg = index_part_to_reg (&ad);
3108 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3109 *ad.base_term, new_reg);
3111 *before = get_insns ();
3112 end_sequence ();
3113 return true;
3116 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3117 Use process_address_1 as a helper function. Return true for any
3118 RTL changes.
3120 If CHECK_ONLY_P is true, just check address correctness. Return
3121 false if the address correct. */
3122 static bool
3123 process_address (int nop, bool check_only_p,
3124 rtx_insn **before, rtx_insn **after)
3126 bool res = false;
3128 while (process_address_1 (nop, check_only_p, before, after))
3130 if (check_only_p)
3131 return true;
3132 res = true;
3134 return res;
3137 /* Emit insns to reload VALUE into a new register. VALUE is an
3138 auto-increment or auto-decrement RTX whose operand is a register or
3139 memory location; so reloading involves incrementing that location.
3140 IN is either identical to VALUE, or some cheaper place to reload
3141 value being incremented/decremented from.
3143 INC_AMOUNT is the number to increment or decrement by (always
3144 positive and ignored for POST_MODIFY/PRE_MODIFY).
3146 Return pseudo containing the result. */
3147 static rtx
3148 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3150 /* REG or MEM to be copied and incremented. */
3151 rtx incloc = XEXP (value, 0);
3152 /* Nonzero if increment after copying. */
3153 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3154 || GET_CODE (value) == POST_MODIFY);
3155 rtx_insn *last;
3156 rtx inc;
3157 rtx_insn *add_insn;
3158 int code;
3159 rtx real_in = in == value ? incloc : in;
3160 rtx result;
3161 bool plus_p = true;
3163 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3165 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3166 || GET_CODE (XEXP (value, 1)) == MINUS);
3167 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3168 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3169 inc = XEXP (XEXP (value, 1), 1);
3171 else
3173 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3174 inc_amount = -inc_amount;
3176 inc = GEN_INT (inc_amount);
3179 if (! post && REG_P (incloc))
3180 result = incloc;
3181 else
3182 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3183 "INC/DEC result");
3185 if (real_in != result)
3187 /* First copy the location to the result register. */
3188 lra_assert (REG_P (result));
3189 emit_insn (gen_move_insn (result, real_in));
3192 /* We suppose that there are insns to add/sub with the constant
3193 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3194 old reload worked with this assumption. If the assumption
3195 becomes wrong, we should use approach in function
3196 base_plus_disp_to_reg. */
3197 if (in == value)
3199 /* See if we can directly increment INCLOC. */
3200 last = get_last_insn ();
3201 add_insn = emit_insn (plus_p
3202 ? gen_add2_insn (incloc, inc)
3203 : gen_sub2_insn (incloc, inc));
3205 code = recog_memoized (add_insn);
3206 if (code >= 0)
3208 if (! post && result != incloc)
3209 emit_insn (gen_move_insn (result, incloc));
3210 return result;
3212 delete_insns_since (last);
3215 /* If couldn't do the increment directly, must increment in RESULT.
3216 The way we do this depends on whether this is pre- or
3217 post-increment. For pre-increment, copy INCLOC to the reload
3218 register, increment it there, then save back. */
3219 if (! post)
3221 if (real_in != result)
3222 emit_insn (gen_move_insn (result, real_in));
3223 if (plus_p)
3224 emit_insn (gen_add2_insn (result, inc));
3225 else
3226 emit_insn (gen_sub2_insn (result, inc));
3227 if (result != incloc)
3228 emit_insn (gen_move_insn (incloc, result));
3230 else
3232 /* Post-increment.
3234 Because this might be a jump insn or a compare, and because
3235 RESULT may not be available after the insn in an input
3236 reload, we must do the incrementing before the insn being
3237 reloaded for.
3239 We have already copied IN to RESULT. Increment the copy in
3240 RESULT, save that back, then decrement RESULT so it has
3241 the original value. */
3242 if (plus_p)
3243 emit_insn (gen_add2_insn (result, inc));
3244 else
3245 emit_insn (gen_sub2_insn (result, inc));
3246 emit_insn (gen_move_insn (incloc, result));
3247 /* Restore non-modified value for the result. We prefer this
3248 way because it does not require an additional hard
3249 register. */
3250 if (plus_p)
3252 if (CONST_INT_P (inc))
3253 emit_insn (gen_add2_insn (result,
3254 gen_int_mode (-INTVAL (inc),
3255 GET_MODE (result))));
3256 else
3257 emit_insn (gen_sub2_insn (result, inc));
3259 else
3260 emit_insn (gen_add2_insn (result, inc));
3262 return result;
3265 /* Return true if the current move insn does not need processing as we
3266 already know that it satisfies its constraints. */
3267 static bool
3268 simple_move_p (void)
3270 rtx dest, src;
3271 enum reg_class dclass, sclass;
3273 lra_assert (curr_insn_set != NULL_RTX);
3274 dest = SET_DEST (curr_insn_set);
3275 src = SET_SRC (curr_insn_set);
3276 return ((dclass = get_op_class (dest)) != NO_REGS
3277 && (sclass = get_op_class (src)) != NO_REGS
3278 /* The backend guarantees that register moves of cost 2
3279 never need reloads. */
3280 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3283 /* Swap operands NOP and NOP + 1. */
3284 static inline void
3285 swap_operands (int nop)
3287 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3288 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3289 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3290 /* Swap the duplicates too. */
3291 lra_update_dup (curr_id, nop);
3292 lra_update_dup (curr_id, nop + 1);
3295 /* Main entry point of the constraint code: search the body of the
3296 current insn to choose the best alternative. It is mimicking insn
3297 alternative cost calculation model of former reload pass. That is
3298 because machine descriptions were written to use this model. This
3299 model can be changed in future. Make commutative operand exchange
3300 if it is chosen.
3302 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3303 constraints. Return true if any change happened during function
3304 call.
3306 If CHECK_ONLY_P is true then don't do any transformation. Just
3307 check that the insn satisfies all constraints. If the insn does
3308 not satisfy any constraint, return true. */
3309 static bool
3310 curr_insn_transform (bool check_only_p)
3312 int i, j, k;
3313 int n_operands;
3314 int n_alternatives;
3315 int commutative;
3316 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3317 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3318 rtx_insn *before, *after;
3319 bool alt_p = false;
3320 /* Flag that the insn has been changed through a transformation. */
3321 bool change_p;
3322 bool sec_mem_p;
3323 #ifdef SECONDARY_MEMORY_NEEDED
3324 bool use_sec_mem_p;
3325 #endif
3326 int max_regno_before;
3327 int reused_alternative_num;
3329 curr_insn_set = single_set (curr_insn);
3330 if (curr_insn_set != NULL_RTX && simple_move_p ())
3331 return false;
3333 no_input_reloads_p = no_output_reloads_p = false;
3334 goal_alt_number = -1;
3335 change_p = sec_mem_p = false;
3336 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3337 reloads; neither are insns that SET cc0. Insns that use CC0 are
3338 not allowed to have any input reloads. */
3339 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3340 no_output_reloads_p = true;
3342 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3343 no_input_reloads_p = true;
3344 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3345 no_output_reloads_p = true;
3347 n_operands = curr_static_id->n_operands;
3348 n_alternatives = curr_static_id->n_alternatives;
3350 /* Just return "no reloads" if insn has no operands with
3351 constraints. */
3352 if (n_operands == 0 || n_alternatives == 0)
3353 return false;
3355 max_regno_before = max_reg_num ();
3357 for (i = 0; i < n_operands; i++)
3359 goal_alt_matched[i][0] = -1;
3360 goal_alt_matches[i] = -1;
3363 commutative = curr_static_id->commutative;
3365 /* Now see what we need for pseudos that didn't get hard regs or got
3366 the wrong kind of hard reg. For this, we must consider all the
3367 operands together against the register constraints. */
3369 best_losers = best_overall = INT_MAX;
3370 best_reload_sum = 0;
3372 curr_swapped = false;
3373 goal_alt_swapped = false;
3375 if (! check_only_p)
3376 /* Make equivalence substitution and memory subreg elimination
3377 before address processing because an address legitimacy can
3378 depend on memory mode. */
3379 for (i = 0; i < n_operands; i++)
3381 rtx op = *curr_id->operand_loc[i];
3382 rtx subst, old = op;
3383 bool op_change_p = false;
3385 if (GET_CODE (old) == SUBREG)
3386 old = SUBREG_REG (old);
3387 subst = get_equiv_with_elimination (old, curr_insn);
3388 original_subreg_reg_mode[i] = VOIDmode;
3389 if (subst != old)
3391 subst = copy_rtx (subst);
3392 lra_assert (REG_P (old));
3393 if (GET_CODE (op) != SUBREG)
3394 *curr_id->operand_loc[i] = subst;
3395 else
3397 SUBREG_REG (op) = subst;
3398 if (GET_MODE (subst) == VOIDmode)
3399 original_subreg_reg_mode[i] = GET_MODE (old);
3401 if (lra_dump_file != NULL)
3403 fprintf (lra_dump_file,
3404 "Changing pseudo %d in operand %i of insn %u on equiv ",
3405 REGNO (old), i, INSN_UID (curr_insn));
3406 dump_value_slim (lra_dump_file, subst, 1);
3407 fprintf (lra_dump_file, "\n");
3409 op_change_p = change_p = true;
3411 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3413 change_p = true;
3414 lra_update_dup (curr_id, i);
3418 /* Reload address registers and displacements. We do it before
3419 finding an alternative because of memory constraints. */
3420 before = after = NULL;
3421 for (i = 0; i < n_operands; i++)
3422 if (! curr_static_id->operand[i].is_operator
3423 && process_address (i, check_only_p, &before, &after))
3425 if (check_only_p)
3426 return true;
3427 change_p = true;
3428 lra_update_dup (curr_id, i);
3431 if (change_p)
3432 /* If we've changed the instruction then any alternative that
3433 we chose previously may no longer be valid. */
3434 lra_set_used_insn_alternative (curr_insn, -1);
3436 if (! check_only_p && curr_insn_set != NULL_RTX
3437 && check_and_process_move (&change_p, &sec_mem_p))
3438 return change_p;
3440 try_swapped:
3442 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3443 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3444 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3445 reused_alternative_num, INSN_UID (curr_insn));
3447 if (process_alt_operands (reused_alternative_num))
3448 alt_p = true;
3450 if (check_only_p)
3451 return ! alt_p || best_losers != 0;
3453 /* If insn is commutative (it's safe to exchange a certain pair of
3454 operands) then we need to try each alternative twice, the second
3455 time matching those two operands as if we had exchanged them. To
3456 do this, really exchange them in operands.
3458 If we have just tried the alternatives the second time, return
3459 operands to normal and drop through. */
3461 if (reused_alternative_num < 0 && commutative >= 0)
3463 curr_swapped = !curr_swapped;
3464 if (curr_swapped)
3466 swap_operands (commutative);
3467 goto try_swapped;
3469 else
3470 swap_operands (commutative);
3473 if (! alt_p && ! sec_mem_p)
3475 /* No alternative works with reloads?? */
3476 if (INSN_CODE (curr_insn) >= 0)
3477 fatal_insn ("unable to generate reloads for:", curr_insn);
3478 error_for_asm (curr_insn,
3479 "inconsistent operand constraints in an %<asm%>");
3480 /* Avoid further trouble with this insn. */
3481 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3482 lra_invalidate_insn_data (curr_insn);
3483 return true;
3486 /* If the best alternative is with operands 1 and 2 swapped, swap
3487 them. Update the operand numbers of any reloads already
3488 pushed. */
3490 if (goal_alt_swapped)
3492 if (lra_dump_file != NULL)
3493 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3494 INSN_UID (curr_insn));
3496 /* Swap the duplicates too. */
3497 swap_operands (commutative);
3498 change_p = true;
3501 #ifdef SECONDARY_MEMORY_NEEDED
3502 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3503 too conservatively. So we use the secondary memory only if there
3504 is no any alternative without reloads. */
3505 use_sec_mem_p = false;
3506 if (! alt_p)
3507 use_sec_mem_p = true;
3508 else if (sec_mem_p)
3510 for (i = 0; i < n_operands; i++)
3511 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3512 break;
3513 use_sec_mem_p = i < n_operands;
3516 if (use_sec_mem_p)
3518 rtx new_reg, src, dest, rld;
3519 machine_mode sec_mode, rld_mode;
3521 lra_assert (sec_mem_p);
3522 lra_assert (curr_static_id->operand[0].type == OP_OUT
3523 && curr_static_id->operand[1].type == OP_IN);
3524 dest = *curr_id->operand_loc[0];
3525 src = *curr_id->operand_loc[1];
3526 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3527 ? dest : src);
3528 rld_mode = GET_MODE (rld);
3529 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3530 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3531 #else
3532 sec_mode = rld_mode;
3533 #endif
3534 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3535 NO_REGS, "secondary");
3536 /* If the mode is changed, it should be wider. */
3537 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3538 if (sec_mode != rld_mode)
3540 /* If the target says specifically to use another mode for
3541 secondary memory moves we can not reuse the original
3542 insn. */
3543 after = emit_spill_move (false, new_reg, dest);
3544 lra_process_new_insns (curr_insn, NULL, after,
3545 "Inserting the sec. move");
3546 /* We may have non null BEFORE here (e.g. after address
3547 processing. */
3548 push_to_sequence (before);
3549 before = emit_spill_move (true, new_reg, src);
3550 emit_insn (before);
3551 before = get_insns ();
3552 end_sequence ();
3553 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3554 lra_set_insn_deleted (curr_insn);
3556 else if (dest == rld)
3558 *curr_id->operand_loc[0] = new_reg;
3559 after = emit_spill_move (false, new_reg, dest);
3560 lra_process_new_insns (curr_insn, NULL, after,
3561 "Inserting the sec. move");
3563 else
3565 *curr_id->operand_loc[1] = new_reg;
3566 /* See comments above. */
3567 push_to_sequence (before);
3568 before = emit_spill_move (true, new_reg, src);
3569 emit_insn (before);
3570 before = get_insns ();
3571 end_sequence ();
3572 lra_process_new_insns (curr_insn, before, NULL,
3573 "Inserting the sec. move");
3575 lra_update_insn_regno_info (curr_insn);
3576 return true;
3578 #endif
3580 lra_assert (goal_alt_number >= 0);
3581 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3583 if (lra_dump_file != NULL)
3585 const char *p;
3587 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3588 goal_alt_number, INSN_UID (curr_insn));
3589 for (i = 0; i < n_operands; i++)
3591 p = (curr_static_id->operand_alternative
3592 [goal_alt_number * n_operands + i].constraint);
3593 if (*p == '\0')
3594 continue;
3595 fprintf (lra_dump_file, " (%d) ", i);
3596 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3597 fputc (*p, lra_dump_file);
3599 if (INSN_CODE (curr_insn) >= 0
3600 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3601 fprintf (lra_dump_file, " {%s}", p);
3602 if (curr_id->sp_offset != 0)
3603 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3604 curr_id->sp_offset);
3605 fprintf (lra_dump_file, "\n");
3608 /* Right now, for any pair of operands I and J that are required to
3609 match, with J < I, goal_alt_matches[I] is J. Add I to
3610 goal_alt_matched[J]. */
3612 for (i = 0; i < n_operands; i++)
3613 if ((j = goal_alt_matches[i]) >= 0)
3615 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3617 /* We allow matching one output operand and several input
3618 operands. */
3619 lra_assert (k == 0
3620 || (curr_static_id->operand[j].type == OP_OUT
3621 && curr_static_id->operand[i].type == OP_IN
3622 && (curr_static_id->operand
3623 [goal_alt_matched[j][0]].type == OP_IN)));
3624 goal_alt_matched[j][k] = i;
3625 goal_alt_matched[j][k + 1] = -1;
3628 for (i = 0; i < n_operands; i++)
3629 goal_alt_win[i] |= goal_alt_match_win[i];
3631 /* Any constants that aren't allowed and can't be reloaded into
3632 registers are here changed into memory references. */
3633 for (i = 0; i < n_operands; i++)
3634 if (goal_alt_win[i])
3636 int regno;
3637 enum reg_class new_class;
3638 rtx reg = *curr_id->operand_loc[i];
3640 if (GET_CODE (reg) == SUBREG)
3641 reg = SUBREG_REG (reg);
3643 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3645 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3647 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3649 lra_assert (ok_p);
3650 lra_change_class (regno, new_class, " Change to", true);
3654 else
3656 const char *constraint;
3657 char c;
3658 rtx op = *curr_id->operand_loc[i];
3659 rtx subreg = NULL_RTX;
3660 machine_mode mode = curr_operand_mode[i];
3662 if (GET_CODE (op) == SUBREG)
3664 subreg = op;
3665 op = SUBREG_REG (op);
3666 mode = GET_MODE (op);
3669 if (CONST_POOL_OK_P (mode, op)
3670 && ((targetm.preferred_reload_class
3671 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3672 || no_input_reloads_p))
3674 rtx tem = force_const_mem (mode, op);
3676 change_p = true;
3677 if (subreg != NULL_RTX)
3678 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3680 *curr_id->operand_loc[i] = tem;
3681 lra_update_dup (curr_id, i);
3682 process_address (i, false, &before, &after);
3684 /* If the alternative accepts constant pool refs directly
3685 there will be no reload needed at all. */
3686 if (subreg != NULL_RTX)
3687 continue;
3688 /* Skip alternatives before the one requested. */
3689 constraint = (curr_static_id->operand_alternative
3690 [goal_alt_number * n_operands + i].constraint);
3691 for (;
3692 (c = *constraint) && c != ',' && c != '#';
3693 constraint += CONSTRAINT_LEN (c, constraint))
3695 enum constraint_num cn = lookup_constraint (constraint);
3696 if (insn_extra_memory_constraint (cn)
3697 && satisfies_memory_constraint_p (tem, cn))
3698 break;
3700 if (c == '\0' || c == ',' || c == '#')
3701 continue;
3703 goal_alt_win[i] = true;
3707 for (i = 0; i < n_operands; i++)
3709 int regno;
3710 bool optional_p = false;
3711 rtx old, new_reg;
3712 rtx op = *curr_id->operand_loc[i];
3714 if (goal_alt_win[i])
3716 if (goal_alt[i] == NO_REGS
3717 && REG_P (op)
3718 /* When we assign NO_REGS it means that we will not
3719 assign a hard register to the scratch pseudo by
3720 assigment pass and the scratch pseudo will be
3721 spilled. Spilled scratch pseudos are transformed
3722 back to scratches at the LRA end. */
3723 && lra_former_scratch_operand_p (curr_insn, i))
3725 int regno = REGNO (op);
3726 lra_change_class (regno, NO_REGS, " Change to", true);
3727 if (lra_get_regno_hard_regno (regno) >= 0)
3728 /* We don't have to mark all insn affected by the
3729 spilled pseudo as there is only one such insn, the
3730 current one. */
3731 reg_renumber[regno] = -1;
3733 /* We can do an optional reload. If the pseudo got a hard
3734 reg, we might improve the code through inheritance. If
3735 it does not get a hard register we coalesce memory/memory
3736 moves later. Ignore move insns to avoid cycling. */
3737 if (! lra_simple_p
3738 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3739 && goal_alt[i] != NO_REGS && REG_P (op)
3740 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3741 && regno < new_regno_start
3742 && ! lra_former_scratch_p (regno)
3743 && reg_renumber[regno] < 0
3744 /* Check that the optional reload pseudo will be able to
3745 hold given mode value. */
3746 && ! (prohibited_class_reg_set_mode_p
3747 (goal_alt[i], reg_class_contents[goal_alt[i]],
3748 PSEUDO_REGNO_MODE (regno)))
3749 && (curr_insn_set == NULL_RTX
3750 || !((REG_P (SET_SRC (curr_insn_set))
3751 || MEM_P (SET_SRC (curr_insn_set))
3752 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3753 && (REG_P (SET_DEST (curr_insn_set))
3754 || MEM_P (SET_DEST (curr_insn_set))
3755 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3756 optional_p = true;
3757 else
3758 continue;
3761 /* Operands that match previous ones have already been handled. */
3762 if (goal_alt_matches[i] >= 0)
3763 continue;
3765 /* We should not have an operand with a non-offsettable address
3766 appearing where an offsettable address will do. It also may
3767 be a case when the address should be special in other words
3768 not a general one (e.g. it needs no index reg). */
3769 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3771 enum reg_class rclass;
3772 rtx *loc = &XEXP (op, 0);
3773 enum rtx_code code = GET_CODE (*loc);
3775 push_to_sequence (before);
3776 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3777 MEM, SCRATCH);
3778 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3779 new_reg = emit_inc (rclass, *loc, *loc,
3780 /* This value does not matter for MODIFY. */
3781 GET_MODE_SIZE (GET_MODE (op)));
3782 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3783 "offsetable address", &new_reg))
3784 lra_emit_move (new_reg, *loc);
3785 before = get_insns ();
3786 end_sequence ();
3787 *loc = new_reg;
3788 lra_update_dup (curr_id, i);
3790 else if (goal_alt_matched[i][0] == -1)
3792 machine_mode mode;
3793 rtx reg, *loc;
3794 int hard_regno, byte;
3795 enum op_type type = curr_static_id->operand[i].type;
3797 loc = curr_id->operand_loc[i];
3798 mode = curr_operand_mode[i];
3799 if (GET_CODE (*loc) == SUBREG)
3801 reg = SUBREG_REG (*loc);
3802 byte = SUBREG_BYTE (*loc);
3803 if (REG_P (reg)
3804 /* Strict_low_part requires reload the register not
3805 the sub-register. */
3806 && (curr_static_id->operand[i].strict_low
3807 || (GET_MODE_SIZE (mode)
3808 <= GET_MODE_SIZE (GET_MODE (reg))
3809 && (hard_regno
3810 = get_try_hard_regno (REGNO (reg))) >= 0
3811 && (simplify_subreg_regno
3812 (hard_regno,
3813 GET_MODE (reg), byte, mode) < 0)
3814 && (goal_alt[i] == NO_REGS
3815 || (simplify_subreg_regno
3816 (ira_class_hard_regs[goal_alt[i]][0],
3817 GET_MODE (reg), byte, mode) >= 0)))))
3819 if (type == OP_OUT)
3820 type = OP_INOUT;
3821 loc = &SUBREG_REG (*loc);
3822 mode = GET_MODE (*loc);
3825 old = *loc;
3826 if (get_reload_reg (type, mode, old, goal_alt[i],
3827 loc != curr_id->operand_loc[i], "", &new_reg)
3828 && type != OP_OUT)
3830 push_to_sequence (before);
3831 lra_emit_move (new_reg, old);
3832 before = get_insns ();
3833 end_sequence ();
3835 *loc = new_reg;
3836 if (type != OP_IN
3837 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3839 start_sequence ();
3840 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3841 emit_insn (after);
3842 after = get_insns ();
3843 end_sequence ();
3844 *loc = new_reg;
3846 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3847 if (goal_alt_dont_inherit_ops[j] == i)
3849 lra_set_regno_unique_value (REGNO (new_reg));
3850 break;
3852 lra_update_dup (curr_id, i);
3854 else if (curr_static_id->operand[i].type == OP_IN
3855 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3856 == OP_OUT))
3858 /* generate reloads for input and matched outputs. */
3859 match_inputs[0] = i;
3860 match_inputs[1] = -1;
3861 match_reload (goal_alt_matched[i][0], match_inputs,
3862 goal_alt[i], &before, &after);
3864 else if (curr_static_id->operand[i].type == OP_OUT
3865 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3866 == OP_IN))
3867 /* Generate reloads for output and matched inputs. */
3868 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3869 else if (curr_static_id->operand[i].type == OP_IN
3870 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3871 == OP_IN))
3873 /* Generate reloads for matched inputs. */
3874 match_inputs[0] = i;
3875 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3876 match_inputs[j + 1] = k;
3877 match_inputs[j + 1] = -1;
3878 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3880 else
3881 /* We must generate code in any case when function
3882 process_alt_operands decides that it is possible. */
3883 gcc_unreachable ();
3884 if (optional_p)
3886 lra_assert (REG_P (op));
3887 regno = REGNO (op);
3888 op = *curr_id->operand_loc[i]; /* Substitution. */
3889 if (GET_CODE (op) == SUBREG)
3890 op = SUBREG_REG (op);
3891 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3892 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3893 lra_reg_info[REGNO (op)].restore_regno = regno;
3894 if (lra_dump_file != NULL)
3895 fprintf (lra_dump_file,
3896 " Making reload reg %d for reg %d optional\n",
3897 REGNO (op), regno);
3900 if (before != NULL_RTX || after != NULL_RTX
3901 || max_regno_before != max_reg_num ())
3902 change_p = true;
3903 if (change_p)
3905 lra_update_operator_dups (curr_id);
3906 /* Something changes -- process the insn. */
3907 lra_update_insn_regno_info (curr_insn);
3909 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3910 return change_p;
3913 /* Return true if INSN satisfies all constraints. In other words, no
3914 reload insns are needed. */
3915 bool
3916 lra_constrain_insn (rtx_insn *insn)
3918 int saved_new_regno_start = new_regno_start;
3919 int saved_new_insn_uid_start = new_insn_uid_start;
3920 bool change_p;
3922 curr_insn = insn;
3923 curr_id = lra_get_insn_recog_data (curr_insn);
3924 curr_static_id = curr_id->insn_static_data;
3925 new_insn_uid_start = get_max_uid ();
3926 new_regno_start = max_reg_num ();
3927 change_p = curr_insn_transform (true);
3928 new_regno_start = saved_new_regno_start;
3929 new_insn_uid_start = saved_new_insn_uid_start;
3930 return ! change_p;
3933 /* Return true if X is in LIST. */
3934 static bool
3935 in_list_p (rtx x, rtx list)
3937 for (; list != NULL_RTX; list = XEXP (list, 1))
3938 if (XEXP (list, 0) == x)
3939 return true;
3940 return false;
3943 /* Return true if X contains an allocatable hard register (if
3944 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3945 static bool
3946 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3948 int i, j;
3949 const char *fmt;
3950 enum rtx_code code;
3952 code = GET_CODE (x);
3953 if (REG_P (x))
3955 int regno = REGNO (x);
3956 HARD_REG_SET alloc_regs;
3958 if (hard_reg_p)
3960 if (regno >= FIRST_PSEUDO_REGISTER)
3961 regno = lra_get_regno_hard_regno (regno);
3962 if (regno < 0)
3963 return false;
3964 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3965 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3967 else
3969 if (regno < FIRST_PSEUDO_REGISTER)
3970 return false;
3971 if (! spilled_p)
3972 return true;
3973 return lra_get_regno_hard_regno (regno) < 0;
3976 fmt = GET_RTX_FORMAT (code);
3977 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3979 if (fmt[i] == 'e')
3981 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3982 return true;
3984 else if (fmt[i] == 'E')
3986 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3987 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3988 return true;
3991 return false;
3994 /* Return true if X contains a symbol reg. */
3995 static bool
3996 contains_symbol_ref_p (rtx x)
3998 int i, j;
3999 const char *fmt;
4000 enum rtx_code code;
4002 code = GET_CODE (x);
4003 if (code == SYMBOL_REF)
4004 return true;
4005 fmt = GET_RTX_FORMAT (code);
4006 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4008 if (fmt[i] == 'e')
4010 if (contains_symbol_ref_p (XEXP (x, i)))
4011 return true;
4013 else if (fmt[i] == 'E')
4015 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4016 if (contains_symbol_ref_p (XVECEXP (x, i, j)))
4017 return true;
4020 return false;
4023 /* Process all regs in location *LOC and change them on equivalent
4024 substitution. Return true if any change was done. */
4025 static bool
4026 loc_equivalence_change_p (rtx *loc)
4028 rtx subst, reg, x = *loc;
4029 bool result = false;
4030 enum rtx_code code = GET_CODE (x);
4031 const char *fmt;
4032 int i, j;
4034 if (code == SUBREG)
4036 reg = SUBREG_REG (x);
4037 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4038 && GET_MODE (subst) == VOIDmode)
4040 /* We cannot reload debug location. Simplify subreg here
4041 while we know the inner mode. */
4042 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4043 GET_MODE (reg), SUBREG_BYTE (x));
4044 return true;
4047 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4049 *loc = subst;
4050 return true;
4053 /* Scan all the operand sub-expressions. */
4054 fmt = GET_RTX_FORMAT (code);
4055 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4057 if (fmt[i] == 'e')
4058 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4059 else if (fmt[i] == 'E')
4060 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4061 result
4062 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4064 return result;
4067 /* Similar to loc_equivalence_change_p, but for use as
4068 simplify_replace_fn_rtx callback. DATA is insn for which the
4069 elimination is done. If it null we don't do the elimination. */
4070 static rtx
4071 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4073 if (!REG_P (loc))
4074 return NULL_RTX;
4076 rtx subst = (data == NULL
4077 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4078 if (subst != loc)
4079 return subst;
4081 return NULL_RTX;
4084 /* Maximum number of generated reload insns per an insn. It is for
4085 preventing this pass cycling in a bug case. */
4086 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4088 /* The current iteration number of this LRA pass. */
4089 int lra_constraint_iter;
4091 /* True if we substituted equiv which needs checking register
4092 allocation correctness because the equivalent value contains
4093 allocatable hard registers or when we restore multi-register
4094 pseudo. */
4095 bool lra_risky_transformations_p;
4097 /* Return true if REGNO is referenced in more than one block. */
4098 static bool
4099 multi_block_pseudo_p (int regno)
4101 basic_block bb = NULL;
4102 unsigned int uid;
4103 bitmap_iterator bi;
4105 if (regno < FIRST_PSEUDO_REGISTER)
4106 return false;
4108 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4109 if (bb == NULL)
4110 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4111 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4112 return true;
4113 return false;
4116 /* Return true if LIST contains a deleted insn. */
4117 static bool
4118 contains_deleted_insn_p (rtx_insn_list *list)
4120 for (; list != NULL_RTX; list = list->next ())
4121 if (NOTE_P (list->insn ())
4122 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4123 return true;
4124 return false;
4127 /* Return true if X contains a pseudo dying in INSN. */
4128 static bool
4129 dead_pseudo_p (rtx x, rtx_insn *insn)
4131 int i, j;
4132 const char *fmt;
4133 enum rtx_code code;
4135 if (REG_P (x))
4136 return (insn != NULL_RTX
4137 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4138 code = GET_CODE (x);
4139 fmt = GET_RTX_FORMAT (code);
4140 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4142 if (fmt[i] == 'e')
4144 if (dead_pseudo_p (XEXP (x, i), insn))
4145 return true;
4147 else if (fmt[i] == 'E')
4149 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4150 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4151 return true;
4154 return false;
4157 /* Return true if INSN contains a dying pseudo in INSN right hand
4158 side. */
4159 static bool
4160 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4162 rtx set = single_set (insn);
4164 gcc_assert (set != NULL);
4165 return dead_pseudo_p (SET_SRC (set), insn);
4168 /* Return true if any init insn of REGNO contains a dying pseudo in
4169 insn right hand side. */
4170 static bool
4171 init_insn_rhs_dead_pseudo_p (int regno)
4173 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4175 if (insns == NULL)
4176 return false;
4177 for (; insns != NULL_RTX; insns = insns->next ())
4178 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4179 return true;
4180 return false;
4183 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4184 reverse only if we have one init insn with given REGNO as a
4185 source. */
4186 static bool
4187 reverse_equiv_p (int regno)
4189 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4190 rtx set;
4192 if (insns == NULL)
4193 return false;
4194 if (! INSN_P (insns->insn ())
4195 || insns->next () != NULL)
4196 return false;
4197 if ((set = single_set (insns->insn ())) == NULL_RTX)
4198 return false;
4199 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4202 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4203 call this function only for non-reverse equivalence. */
4204 static bool
4205 contains_reloaded_insn_p (int regno)
4207 rtx set;
4208 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4210 for (; list != NULL; list = list->next ())
4211 if ((set = single_set (list->insn ())) == NULL_RTX
4212 || ! REG_P (SET_DEST (set))
4213 || (int) REGNO (SET_DEST (set)) != regno)
4214 return true;
4215 return false;
4218 /* Entry function of LRA constraint pass. Return true if the
4219 constraint pass did change the code. */
4220 bool
4221 lra_constraints (bool first_p)
4223 bool changed_p;
4224 int i, hard_regno, new_insns_num;
4225 unsigned int min_len, new_min_len, uid;
4226 rtx set, x, reg, dest_reg;
4227 basic_block last_bb;
4228 bitmap_head equiv_insn_bitmap;
4229 bitmap_iterator bi;
4231 lra_constraint_iter++;
4232 if (lra_dump_file != NULL)
4233 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4234 lra_constraint_iter);
4235 changed_p = false;
4236 if (pic_offset_table_rtx
4237 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4238 lra_risky_transformations_p = true;
4239 else
4240 lra_risky_transformations_p = false;
4241 new_insn_uid_start = get_max_uid ();
4242 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4243 /* Mark used hard regs for target stack size calulations. */
4244 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4245 if (lra_reg_info[i].nrefs != 0
4246 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4248 int j, nregs;
4250 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4251 for (j = 0; j < nregs; j++)
4252 df_set_regs_ever_live (hard_regno + j, true);
4254 /* Do elimination before the equivalence processing as we can spill
4255 some pseudos during elimination. */
4256 lra_eliminate (false, first_p);
4257 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4258 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4259 if (lra_reg_info[i].nrefs != 0)
4261 ira_reg_equiv[i].profitable_p = true;
4262 reg = regno_reg_rtx[i];
4263 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4265 bool pseudo_p = contains_reg_p (x, false, false);
4267 /* After RTL transformation, we can not guarantee that
4268 pseudo in the substitution was not reloaded which might
4269 make equivalence invalid. For example, in reverse
4270 equiv of p0
4272 p0 <- ...
4274 equiv_mem <- p0
4276 the memory address register was reloaded before the 2nd
4277 insn. */
4278 if ((! first_p && pseudo_p)
4279 /* We don't use DF for compilation speed sake. So it
4280 is problematic to update live info when we use an
4281 equivalence containing pseudos in more than one
4282 BB. */
4283 || (pseudo_p && multi_block_pseudo_p (i))
4284 /* If an init insn was deleted for some reason, cancel
4285 the equiv. We could update the equiv insns after
4286 transformations including an equiv insn deletion
4287 but it is not worthy as such cases are extremely
4288 rare. */
4289 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4290 /* If it is not a reverse equivalence, we check that a
4291 pseudo in rhs of the init insn is not dying in the
4292 insn. Otherwise, the live info at the beginning of
4293 the corresponding BB might be wrong after we
4294 removed the insn. When the equiv can be a
4295 constant, the right hand side of the init insn can
4296 be a pseudo. */
4297 || (! reverse_equiv_p (i)
4298 && (init_insn_rhs_dead_pseudo_p (i)
4299 /* If we reloaded the pseudo in an equivalence
4300 init insn, we can not remove the equiv init
4301 insns and the init insns might write into
4302 const memory in this case. */
4303 || contains_reloaded_insn_p (i)))
4304 /* Prevent access beyond equivalent memory for
4305 paradoxical subregs. */
4306 || (MEM_P (x)
4307 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4308 > GET_MODE_SIZE (GET_MODE (x))))
4309 || (pic_offset_table_rtx
4310 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4311 && (targetm.preferred_reload_class
4312 (x, lra_get_allocno_class (i)) == NO_REGS))
4313 || contains_symbol_ref_p (x))))
4314 ira_reg_equiv[i].defined_p = false;
4315 if (contains_reg_p (x, false, true))
4316 ira_reg_equiv[i].profitable_p = false;
4317 if (get_equiv (reg) != reg)
4318 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4321 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4322 update_equiv (i);
4323 /* We should add all insns containing pseudos which should be
4324 substituted by their equivalences. */
4325 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4326 lra_push_insn_by_uid (uid);
4327 min_len = lra_insn_stack_length ();
4328 new_insns_num = 0;
4329 last_bb = NULL;
4330 changed_p = false;
4331 while ((new_min_len = lra_insn_stack_length ()) != 0)
4333 curr_insn = lra_pop_insn ();
4334 --new_min_len;
4335 curr_bb = BLOCK_FOR_INSN (curr_insn);
4336 if (curr_bb != last_bb)
4338 last_bb = curr_bb;
4339 bb_reload_num = lra_curr_reload_num;
4341 if (min_len > new_min_len)
4343 min_len = new_min_len;
4344 new_insns_num = 0;
4346 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4347 internal_error
4348 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4349 MAX_RELOAD_INSNS_NUMBER);
4350 new_insns_num++;
4351 if (DEBUG_INSN_P (curr_insn))
4353 /* We need to check equivalence in debug insn and change
4354 pseudo to the equivalent value if necessary. */
4355 curr_id = lra_get_insn_recog_data (curr_insn);
4356 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4358 rtx old = *curr_id->operand_loc[0];
4359 *curr_id->operand_loc[0]
4360 = simplify_replace_fn_rtx (old, NULL_RTX,
4361 loc_equivalence_callback, curr_insn);
4362 if (old != *curr_id->operand_loc[0])
4364 lra_update_insn_regno_info (curr_insn);
4365 changed_p = true;
4369 else if (INSN_P (curr_insn))
4371 if ((set = single_set (curr_insn)) != NULL_RTX)
4373 dest_reg = SET_DEST (set);
4374 /* The equivalence pseudo could be set up as SUBREG in a
4375 case when it is a call restore insn in a mode
4376 different from the pseudo mode. */
4377 if (GET_CODE (dest_reg) == SUBREG)
4378 dest_reg = SUBREG_REG (dest_reg);
4379 if ((REG_P (dest_reg)
4380 && (x = get_equiv (dest_reg)) != dest_reg
4381 /* Remove insns which set up a pseudo whose value
4382 can not be changed. Such insns might be not in
4383 init_insns because we don't update equiv data
4384 during insn transformations.
4386 As an example, let suppose that a pseudo got
4387 hard register and on the 1st pass was not
4388 changed to equivalent constant. We generate an
4389 additional insn setting up the pseudo because of
4390 secondary memory movement. Then the pseudo is
4391 spilled and we use the equiv constant. In this
4392 case we should remove the additional insn and
4393 this insn is not init_insns list. */
4394 && (! MEM_P (x) || MEM_READONLY_P (x)
4395 /* Check that this is actually an insn setting
4396 up the equivalence. */
4397 || in_list_p (curr_insn,
4398 ira_reg_equiv
4399 [REGNO (dest_reg)].init_insns)))
4400 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4401 && in_list_p (curr_insn,
4402 ira_reg_equiv
4403 [REGNO (SET_SRC (set))].init_insns)))
4405 /* This is equiv init insn of pseudo which did not get a
4406 hard register -- remove the insn. */
4407 if (lra_dump_file != NULL)
4409 fprintf (lra_dump_file,
4410 " Removing equiv init insn %i (freq=%d)\n",
4411 INSN_UID (curr_insn),
4412 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4413 dump_insn_slim (lra_dump_file, curr_insn);
4415 if (contains_reg_p (x, true, false))
4416 lra_risky_transformations_p = true;
4417 lra_set_insn_deleted (curr_insn);
4418 continue;
4421 curr_id = lra_get_insn_recog_data (curr_insn);
4422 curr_static_id = curr_id->insn_static_data;
4423 init_curr_insn_input_reloads ();
4424 init_curr_operand_mode ();
4425 if (curr_insn_transform (false))
4426 changed_p = true;
4427 /* Check non-transformed insns too for equiv change as USE
4428 or CLOBBER don't need reloads but can contain pseudos
4429 being changed on their equivalences. */
4430 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4431 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4433 lra_update_insn_regno_info (curr_insn);
4434 changed_p = true;
4438 bitmap_clear (&equiv_insn_bitmap);
4439 /* If we used a new hard regno, changed_p should be true because the
4440 hard reg is assigned to a new pseudo. */
4441 #ifdef ENABLE_CHECKING
4442 if (! changed_p)
4444 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4445 if (lra_reg_info[i].nrefs != 0
4446 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4448 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4450 for (j = 0; j < nregs; j++)
4451 lra_assert (df_regs_ever_live_p (hard_regno + j));
4454 #endif
4455 return changed_p;
4458 /* Initiate the LRA constraint pass. It is done once per
4459 function. */
4460 void
4461 lra_constraints_init (void)
4465 /* Finalize the LRA constraint pass. It is done once per
4466 function. */
4467 void
4468 lra_constraints_finish (void)
4474 /* This page contains code to do inheritance/split
4475 transformations. */
4477 /* Number of reloads passed so far in current EBB. */
4478 static int reloads_num;
4480 /* Number of calls passed so far in current EBB. */
4481 static int calls_num;
4483 /* Current reload pseudo check for validity of elements in
4484 USAGE_INSNS. */
4485 static int curr_usage_insns_check;
4487 /* Info about last usage of registers in EBB to do inheritance/split
4488 transformation. Inheritance transformation is done from a spilled
4489 pseudo and split transformations from a hard register or a pseudo
4490 assigned to a hard register. */
4491 struct usage_insns
4493 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4494 value INSNS is valid. The insns is chain of optional debug insns
4495 and a finishing non-debug insn using the corresponding reg. The
4496 value is also used to mark the registers which are set up in the
4497 current insn. The negated insn uid is used for this. */
4498 int check;
4499 /* Value of global reloads_num at the last insn in INSNS. */
4500 int reloads_num;
4501 /* Value of global reloads_nums at the last insn in INSNS. */
4502 int calls_num;
4503 /* It can be true only for splitting. And it means that the restore
4504 insn should be put after insn given by the following member. */
4505 bool after_p;
4506 /* Next insns in the current EBB which use the original reg and the
4507 original reg value is not changed between the current insn and
4508 the next insns. In order words, e.g. for inheritance, if we need
4509 to use the original reg value again in the next insns we can try
4510 to use the value in a hard register from a reload insn of the
4511 current insn. */
4512 rtx insns;
4515 /* Map: regno -> corresponding pseudo usage insns. */
4516 static struct usage_insns *usage_insns;
4518 static void
4519 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4521 usage_insns[regno].check = curr_usage_insns_check;
4522 usage_insns[regno].insns = insn;
4523 usage_insns[regno].reloads_num = reloads_num;
4524 usage_insns[regno].calls_num = calls_num;
4525 usage_insns[regno].after_p = after_p;
4528 /* The function is used to form list REGNO usages which consists of
4529 optional debug insns finished by a non-debug insn using REGNO.
4530 RELOADS_NUM is current number of reload insns processed so far. */
4531 static void
4532 add_next_usage_insn (int regno, rtx insn, int reloads_num)
4534 rtx next_usage_insns;
4536 if (usage_insns[regno].check == curr_usage_insns_check
4537 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4538 && DEBUG_INSN_P (insn))
4540 /* Check that we did not add the debug insn yet. */
4541 if (next_usage_insns != insn
4542 && (GET_CODE (next_usage_insns) != INSN_LIST
4543 || XEXP (next_usage_insns, 0) != insn))
4544 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4545 next_usage_insns);
4547 else if (NONDEBUG_INSN_P (insn))
4548 setup_next_usage_insn (regno, insn, reloads_num, false);
4549 else
4550 usage_insns[regno].check = 0;
4553 /* Return first non-debug insn in list USAGE_INSNS. */
4554 static rtx_insn *
4555 skip_usage_debug_insns (rtx usage_insns)
4557 rtx insn;
4559 /* Skip debug insns. */
4560 for (insn = usage_insns;
4561 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4562 insn = XEXP (insn, 1))
4564 return safe_as_a <rtx_insn *> (insn);
4567 /* Return true if we need secondary memory moves for insn in
4568 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4569 into the insn. */
4570 static bool
4571 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4572 rtx usage_insns ATTRIBUTE_UNUSED)
4574 #ifndef SECONDARY_MEMORY_NEEDED
4575 return false;
4576 #else
4577 rtx_insn *insn;
4578 rtx set, dest;
4579 enum reg_class cl;
4581 if (inher_cl == ALL_REGS
4582 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4583 return false;
4584 lra_assert (INSN_P (insn));
4585 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4586 return false;
4587 dest = SET_DEST (set);
4588 if (! REG_P (dest))
4589 return false;
4590 lra_assert (inher_cl != NO_REGS);
4591 cl = get_reg_class (REGNO (dest));
4592 return (cl != NO_REGS && cl != ALL_REGS
4593 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4594 #endif
4597 /* Registers involved in inheritance/split in the current EBB
4598 (inheritance/split pseudos and original registers). */
4599 static bitmap_head check_only_regs;
4601 /* Do inheritance transformations for insn INSN, which defines (if
4602 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4603 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4604 form as the "insns" field of usage_insns. Return true if we
4605 succeed in such transformation.
4607 The transformations look like:
4609 p <- ... i <- ...
4610 ... p <- i (new insn)
4611 ... =>
4612 <- ... p ... <- ... i ...
4614 ... i <- p (new insn)
4615 <- ... p ... <- ... i ...
4616 ... =>
4617 <- ... p ... <- ... i ...
4618 where p is a spilled original pseudo and i is a new inheritance pseudo.
4621 The inheritance pseudo has the smallest class of two classes CL and
4622 class of ORIGINAL REGNO. */
4623 static bool
4624 inherit_reload_reg (bool def_p, int original_regno,
4625 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4627 if (optimize_function_for_size_p (cfun))
4628 return false;
4630 enum reg_class rclass = lra_get_allocno_class (original_regno);
4631 rtx original_reg = regno_reg_rtx[original_regno];
4632 rtx new_reg, usage_insn;
4633 rtx_insn *new_insns;
4635 lra_assert (! usage_insns[original_regno].after_p);
4636 if (lra_dump_file != NULL)
4637 fprintf (lra_dump_file,
4638 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4639 if (! ira_reg_classes_intersect_p[cl][rclass])
4641 if (lra_dump_file != NULL)
4643 fprintf (lra_dump_file,
4644 " Rejecting inheritance for %d "
4645 "because of disjoint classes %s and %s\n",
4646 original_regno, reg_class_names[cl],
4647 reg_class_names[rclass]);
4648 fprintf (lra_dump_file,
4649 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4651 return false;
4653 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4654 /* We don't use a subset of two classes because it can be
4655 NO_REGS. This transformation is still profitable in most
4656 cases even if the classes are not intersected as register
4657 move is probably cheaper than a memory load. */
4658 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4660 if (lra_dump_file != NULL)
4661 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4662 reg_class_names[cl], reg_class_names[rclass]);
4664 rclass = cl;
4666 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4668 /* Reject inheritance resulting in secondary memory moves.
4669 Otherwise, there is a danger in LRA cycling. Also such
4670 transformation will be unprofitable. */
4671 if (lra_dump_file != NULL)
4673 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4674 rtx set = single_set (insn);
4676 lra_assert (set != NULL_RTX);
4678 rtx dest = SET_DEST (set);
4680 lra_assert (REG_P (dest));
4681 fprintf (lra_dump_file,
4682 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4683 "as secondary mem is needed\n",
4684 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4685 original_regno, reg_class_names[rclass]);
4686 fprintf (lra_dump_file,
4687 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4689 return false;
4691 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4692 rclass, "inheritance");
4693 start_sequence ();
4694 if (def_p)
4695 lra_emit_move (original_reg, new_reg);
4696 else
4697 lra_emit_move (new_reg, original_reg);
4698 new_insns = get_insns ();
4699 end_sequence ();
4700 if (NEXT_INSN (new_insns) != NULL_RTX)
4702 if (lra_dump_file != NULL)
4704 fprintf (lra_dump_file,
4705 " Rejecting inheritance %d->%d "
4706 "as it results in 2 or more insns:\n",
4707 original_regno, REGNO (new_reg));
4708 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4709 fprintf (lra_dump_file,
4710 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4712 return false;
4714 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
4715 lra_update_insn_regno_info (insn);
4716 if (! def_p)
4717 /* We now have a new usage insn for original regno. */
4718 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4719 if (lra_dump_file != NULL)
4720 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4721 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4722 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4723 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4724 bitmap_set_bit (&check_only_regs, original_regno);
4725 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4726 if (def_p)
4727 lra_process_new_insns (insn, NULL, new_insns,
4728 "Add original<-inheritance");
4729 else
4730 lra_process_new_insns (insn, new_insns, NULL,
4731 "Add inheritance<-original");
4732 while (next_usage_insns != NULL_RTX)
4734 if (GET_CODE (next_usage_insns) != INSN_LIST)
4736 usage_insn = next_usage_insns;
4737 lra_assert (NONDEBUG_INSN_P (usage_insn));
4738 next_usage_insns = NULL;
4740 else
4742 usage_insn = XEXP (next_usage_insns, 0);
4743 lra_assert (DEBUG_INSN_P (usage_insn));
4744 next_usage_insns = XEXP (next_usage_insns, 1);
4746 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
4747 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4748 if (lra_dump_file != NULL)
4750 fprintf (lra_dump_file,
4751 " Inheritance reuse change %d->%d (bb%d):\n",
4752 original_regno, REGNO (new_reg),
4753 BLOCK_FOR_INSN (usage_insn)->index);
4754 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
4757 if (lra_dump_file != NULL)
4758 fprintf (lra_dump_file,
4759 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4760 return true;
4763 /* Return true if we need a caller save/restore for pseudo REGNO which
4764 was assigned to a hard register. */
4765 static inline bool
4766 need_for_call_save_p (int regno)
4768 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4769 return (usage_insns[regno].calls_num < calls_num
4770 && (overlaps_hard_reg_set_p
4771 ((flag_ipa_ra &&
4772 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4773 ? lra_reg_info[regno].actual_call_used_reg_set
4774 : call_used_reg_set,
4775 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4776 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4777 PSEUDO_REGNO_MODE (regno))));
4780 /* Global registers occurring in the current EBB. */
4781 static bitmap_head ebb_global_regs;
4783 /* Return true if we need a split for hard register REGNO or pseudo
4784 REGNO which was assigned to a hard register.
4785 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4786 used for reloads since the EBB end. It is an approximation of the
4787 used hard registers in the split range. The exact value would
4788 require expensive calculations. If we were aggressive with
4789 splitting because of the approximation, the split pseudo will save
4790 the same hard register assignment and will be removed in the undo
4791 pass. We still need the approximation because too aggressive
4792 splitting would result in too inaccurate cost calculation in the
4793 assignment pass because of too many generated moves which will be
4794 probably removed in the undo pass. */
4795 static inline bool
4796 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4798 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4800 lra_assert (hard_regno >= 0);
4801 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4802 /* Don't split eliminable hard registers, otherwise we can
4803 split hard registers like hard frame pointer, which
4804 lives on BB start/end according to DF-infrastructure,
4805 when there is a pseudo assigned to the register and
4806 living in the same BB. */
4807 && (regno >= FIRST_PSEUDO_REGISTER
4808 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4809 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4810 /* Don't split call clobbered hard regs living through
4811 calls, otherwise we might have a check problem in the
4812 assign sub-pass as in the most cases (exception is a
4813 situation when lra_risky_transformations_p value is
4814 true) the assign pass assumes that all pseudos living
4815 through calls are assigned to call saved hard regs. */
4816 && (regno >= FIRST_PSEUDO_REGISTER
4817 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4818 || usage_insns[regno].calls_num == calls_num)
4819 /* We need at least 2 reloads to make pseudo splitting
4820 profitable. We should provide hard regno splitting in
4821 any case to solve 1st insn scheduling problem when
4822 moving hard register definition up might result in
4823 impossibility to find hard register for reload pseudo of
4824 small register class. */
4825 && (usage_insns[regno].reloads_num
4826 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4827 && (regno < FIRST_PSEUDO_REGISTER
4828 /* For short living pseudos, spilling + inheritance can
4829 be considered a substitution for splitting.
4830 Therefore we do not splitting for local pseudos. It
4831 decreases also aggressiveness of splitting. The
4832 minimal number of references is chosen taking into
4833 account that for 2 references splitting has no sense
4834 as we can just spill the pseudo. */
4835 || (regno >= FIRST_PSEUDO_REGISTER
4836 && lra_reg_info[regno].nrefs > 3
4837 && bitmap_bit_p (&ebb_global_regs, regno))))
4838 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4841 /* Return class for the split pseudo created from original pseudo with
4842 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4843 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4844 results in no secondary memory movements. */
4845 static enum reg_class
4846 choose_split_class (enum reg_class allocno_class,
4847 int hard_regno ATTRIBUTE_UNUSED,
4848 machine_mode mode ATTRIBUTE_UNUSED)
4850 #ifndef SECONDARY_MEMORY_NEEDED
4851 return allocno_class;
4852 #else
4853 int i;
4854 enum reg_class cl, best_cl = NO_REGS;
4855 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4856 = REGNO_REG_CLASS (hard_regno);
4858 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4859 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4860 return allocno_class;
4861 for (i = 0;
4862 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4863 i++)
4864 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4865 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4866 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4867 && (best_cl == NO_REGS
4868 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4869 best_cl = cl;
4870 return best_cl;
4871 #endif
4874 /* Do split transformations for insn INSN, which defines or uses
4875 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4876 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4877 "insns" field of usage_insns.
4879 The transformations look like:
4881 p <- ... p <- ...
4882 ... s <- p (new insn -- save)
4883 ... =>
4884 ... p <- s (new insn -- restore)
4885 <- ... p ... <- ... p ...
4887 <- ... p ... <- ... p ...
4888 ... s <- p (new insn -- save)
4889 ... =>
4890 ... p <- s (new insn -- restore)
4891 <- ... p ... <- ... p ...
4893 where p is an original pseudo got a hard register or a hard
4894 register and s is a new split pseudo. The save is put before INSN
4895 if BEFORE_P is true. Return true if we succeed in such
4896 transformation. */
4897 static bool
4898 split_reg (bool before_p, int original_regno, rtx_insn *insn,
4899 rtx next_usage_insns)
4901 enum reg_class rclass;
4902 rtx original_reg;
4903 int hard_regno, nregs;
4904 rtx new_reg, usage_insn;
4905 rtx_insn *restore, *save;
4906 bool after_p;
4907 bool call_save_p;
4909 if (original_regno < FIRST_PSEUDO_REGISTER)
4911 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4912 hard_regno = original_regno;
4913 call_save_p = false;
4914 nregs = 1;
4916 else
4918 hard_regno = reg_renumber[original_regno];
4919 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4920 rclass = lra_get_allocno_class (original_regno);
4921 original_reg = regno_reg_rtx[original_regno];
4922 call_save_p = need_for_call_save_p (original_regno);
4924 original_reg = regno_reg_rtx[original_regno];
4925 lra_assert (hard_regno >= 0);
4926 if (lra_dump_file != NULL)
4927 fprintf (lra_dump_file,
4928 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4929 if (call_save_p)
4931 machine_mode mode = GET_MODE (original_reg);
4933 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4934 hard_regno_nregs[hard_regno][mode],
4935 mode);
4936 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4938 else
4940 rclass = choose_split_class (rclass, hard_regno,
4941 GET_MODE (original_reg));
4942 if (rclass == NO_REGS)
4944 if (lra_dump_file != NULL)
4946 fprintf (lra_dump_file,
4947 " Rejecting split of %d(%s): "
4948 "no good reg class for %d(%s)\n",
4949 original_regno,
4950 reg_class_names[lra_get_allocno_class (original_regno)],
4951 hard_regno,
4952 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4953 fprintf
4954 (lra_dump_file,
4955 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4957 return false;
4959 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4960 rclass, "split");
4961 reg_renumber[REGNO (new_reg)] = hard_regno;
4963 save = emit_spill_move (true, new_reg, original_reg);
4964 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
4966 if (lra_dump_file != NULL)
4968 fprintf
4969 (lra_dump_file,
4970 " Rejecting split %d->%d resulting in > 2 save insns:\n",
4971 original_regno, REGNO (new_reg));
4972 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
4973 fprintf (lra_dump_file,
4974 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4976 return false;
4978 restore = emit_spill_move (false, new_reg, original_reg);
4979 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
4981 if (lra_dump_file != NULL)
4983 fprintf (lra_dump_file,
4984 " Rejecting split %d->%d "
4985 "resulting in > 2 restore insns:\n",
4986 original_regno, REGNO (new_reg));
4987 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
4988 fprintf (lra_dump_file,
4989 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4991 return false;
4993 after_p = usage_insns[original_regno].after_p;
4994 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4995 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4996 bitmap_set_bit (&check_only_regs, original_regno);
4997 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4998 for (;;)
5000 if (GET_CODE (next_usage_insns) != INSN_LIST)
5002 usage_insn = next_usage_insns;
5003 break;
5005 usage_insn = XEXP (next_usage_insns, 0);
5006 lra_assert (DEBUG_INSN_P (usage_insn));
5007 next_usage_insns = XEXP (next_usage_insns, 1);
5008 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5009 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5010 if (lra_dump_file != NULL)
5012 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5013 original_regno, REGNO (new_reg));
5014 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5017 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5018 lra_assert (usage_insn != insn || (after_p && before_p));
5019 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5020 after_p ? NULL : restore,
5021 after_p ? restore : NULL,
5022 call_save_p
5023 ? "Add reg<-save" : "Add reg<-split");
5024 lra_process_new_insns (insn, before_p ? save : NULL,
5025 before_p ? NULL : save,
5026 call_save_p
5027 ? "Add save<-reg" : "Add split<-reg");
5028 if (nregs > 1)
5029 /* If we are trying to split multi-register. We should check
5030 conflicts on the next assignment sub-pass. IRA can allocate on
5031 sub-register levels, LRA do this on pseudos level right now and
5032 this discrepancy may create allocation conflicts after
5033 splitting. */
5034 lra_risky_transformations_p = true;
5035 if (lra_dump_file != NULL)
5036 fprintf (lra_dump_file,
5037 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5038 return true;
5041 /* Recognize that we need a split transformation for insn INSN, which
5042 defines or uses REGNO in its insn biggest MODE (we use it only if
5043 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5044 hard registers which might be used for reloads since the EBB end.
5045 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5046 uid before starting INSN processing. Return true if we succeed in
5047 such transformation. */
5048 static bool
5049 split_if_necessary (int regno, machine_mode mode,
5050 HARD_REG_SET potential_reload_hard_regs,
5051 bool before_p, rtx_insn *insn, int max_uid)
5053 bool res = false;
5054 int i, nregs = 1;
5055 rtx next_usage_insns;
5057 if (regno < FIRST_PSEUDO_REGISTER)
5058 nregs = hard_regno_nregs[regno][mode];
5059 for (i = 0; i < nregs; i++)
5060 if (usage_insns[regno + i].check == curr_usage_insns_check
5061 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5062 /* To avoid processing the register twice or more. */
5063 && ((GET_CODE (next_usage_insns) != INSN_LIST
5064 && INSN_UID (next_usage_insns) < max_uid)
5065 || (GET_CODE (next_usage_insns) == INSN_LIST
5066 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5067 && need_for_split_p (potential_reload_hard_regs, regno + i)
5068 && split_reg (before_p, regno + i, insn, next_usage_insns))
5069 res = true;
5070 return res;
5073 /* Check only registers living at the current program point in the
5074 current EBB. */
5075 static bitmap_head live_regs;
5077 /* Update live info in EBB given by its HEAD and TAIL insns after
5078 inheritance/split transformation. The function removes dead moves
5079 too. */
5080 static void
5081 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5083 unsigned int j;
5084 int i, regno;
5085 bool live_p;
5086 rtx_insn *prev_insn;
5087 rtx set;
5088 bool remove_p;
5089 basic_block last_bb, prev_bb, curr_bb;
5090 bitmap_iterator bi;
5091 struct lra_insn_reg *reg;
5092 edge e;
5093 edge_iterator ei;
5095 last_bb = BLOCK_FOR_INSN (tail);
5096 prev_bb = NULL;
5097 for (curr_insn = tail;
5098 curr_insn != PREV_INSN (head);
5099 curr_insn = prev_insn)
5101 prev_insn = PREV_INSN (curr_insn);
5102 /* We need to process empty blocks too. They contain
5103 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5104 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5105 continue;
5106 curr_bb = BLOCK_FOR_INSN (curr_insn);
5107 if (curr_bb != prev_bb)
5109 if (prev_bb != NULL)
5111 /* Update df_get_live_in (prev_bb): */
5112 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5113 if (bitmap_bit_p (&live_regs, j))
5114 bitmap_set_bit (df_get_live_in (prev_bb), j);
5115 else
5116 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5118 if (curr_bb != last_bb)
5120 /* Update df_get_live_out (curr_bb): */
5121 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5123 live_p = bitmap_bit_p (&live_regs, j);
5124 if (! live_p)
5125 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5126 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5128 live_p = true;
5129 break;
5131 if (live_p)
5132 bitmap_set_bit (df_get_live_out (curr_bb), j);
5133 else
5134 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5137 prev_bb = curr_bb;
5138 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5140 if (! NONDEBUG_INSN_P (curr_insn))
5141 continue;
5142 curr_id = lra_get_insn_recog_data (curr_insn);
5143 curr_static_id = curr_id->insn_static_data;
5144 remove_p = false;
5145 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
5146 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5147 && bitmap_bit_p (&check_only_regs, regno)
5148 && ! bitmap_bit_p (&live_regs, regno))
5149 remove_p = true;
5150 /* See which defined values die here. */
5151 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5152 if (reg->type == OP_OUT && ! reg->subreg_p)
5153 bitmap_clear_bit (&live_regs, reg->regno);
5154 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5155 if (reg->type == OP_OUT && ! reg->subreg_p)
5156 bitmap_clear_bit (&live_regs, reg->regno);
5157 /* Mark each used value as live. */
5158 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5159 if (reg->type != OP_OUT
5160 && bitmap_bit_p (&check_only_regs, reg->regno))
5161 bitmap_set_bit (&live_regs, reg->regno);
5162 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5163 if (reg->type != OP_OUT
5164 && bitmap_bit_p (&check_only_regs, reg->regno))
5165 bitmap_set_bit (&live_regs, reg->regno);
5166 if (curr_id->arg_hard_regs != NULL)
5167 /* Make argument hard registers live. */
5168 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5169 if (bitmap_bit_p (&check_only_regs, regno))
5170 bitmap_set_bit (&live_regs, regno);
5171 /* It is quite important to remove dead move insns because it
5172 means removing dead store. We don't need to process them for
5173 constraints. */
5174 if (remove_p)
5176 if (lra_dump_file != NULL)
5178 fprintf (lra_dump_file, " Removing dead insn:\n ");
5179 dump_insn_slim (lra_dump_file, curr_insn);
5181 lra_set_insn_deleted (curr_insn);
5186 /* The structure describes info to do an inheritance for the current
5187 insn. We need to collect such info first before doing the
5188 transformations because the transformations change the insn
5189 internal representation. */
5190 struct to_inherit
5192 /* Original regno. */
5193 int regno;
5194 /* Subsequent insns which can inherit original reg value. */
5195 rtx insns;
5198 /* Array containing all info for doing inheritance from the current
5199 insn. */
5200 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5202 /* Number elements in the previous array. */
5203 static int to_inherit_num;
5205 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5206 structure to_inherit. */
5207 static void
5208 add_to_inherit (int regno, rtx insns)
5210 int i;
5212 for (i = 0; i < to_inherit_num; i++)
5213 if (to_inherit[i].regno == regno)
5214 return;
5215 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5216 to_inherit[to_inherit_num].regno = regno;
5217 to_inherit[to_inherit_num++].insns = insns;
5220 /* Return the last non-debug insn in basic block BB, or the block begin
5221 note if none. */
5222 static rtx_insn *
5223 get_last_insertion_point (basic_block bb)
5225 rtx_insn *insn;
5227 FOR_BB_INSNS_REVERSE (bb, insn)
5228 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5229 return insn;
5230 gcc_unreachable ();
5233 /* Set up RES by registers living on edges FROM except the edge (FROM,
5234 TO) or by registers set up in a jump insn in BB FROM. */
5235 static void
5236 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5238 rtx_insn *last;
5239 struct lra_insn_reg *reg;
5240 edge e;
5241 edge_iterator ei;
5243 lra_assert (to != NULL);
5244 bitmap_clear (res);
5245 FOR_EACH_EDGE (e, ei, from->succs)
5246 if (e->dest != to)
5247 bitmap_ior_into (res, df_get_live_in (e->dest));
5248 last = get_last_insertion_point (from);
5249 if (! JUMP_P (last))
5250 return;
5251 curr_id = lra_get_insn_recog_data (last);
5252 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5253 if (reg->type != OP_IN)
5254 bitmap_set_bit (res, reg->regno);
5257 /* Used as a temporary results of some bitmap calculations. */
5258 static bitmap_head temp_bitmap;
5260 /* We split for reloads of small class of hard regs. The following
5261 defines how many hard regs the class should have to be qualified as
5262 small. The code is mostly oriented to x86/x86-64 architecture
5263 where some insns need to use only specific register or pair of
5264 registers and these register can live in RTL explicitly, e.g. for
5265 parameter passing. */
5266 static const int max_small_class_regs_num = 2;
5268 /* Do inheritance/split transformations in EBB starting with HEAD and
5269 finishing on TAIL. We process EBB insns in the reverse order.
5270 Return true if we did any inheritance/split transformation in the
5271 EBB.
5273 We should avoid excessive splitting which results in worse code
5274 because of inaccurate cost calculations for spilling new split
5275 pseudos in such case. To achieve this we do splitting only if
5276 register pressure is high in given basic block and there are reload
5277 pseudos requiring hard registers. We could do more register
5278 pressure calculations at any given program point to avoid necessary
5279 splitting even more but it is to expensive and the current approach
5280 works well enough. */
5281 static bool
5282 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5284 int i, src_regno, dst_regno, nregs;
5285 bool change_p, succ_p, update_reloads_num_p;
5286 rtx_insn *prev_insn, *last_insn;
5287 rtx next_usage_insns, set;
5288 enum reg_class cl;
5289 struct lra_insn_reg *reg;
5290 basic_block last_processed_bb, curr_bb = NULL;
5291 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5292 bitmap to_process;
5293 unsigned int j;
5294 bitmap_iterator bi;
5295 bool head_p, after_p;
5297 change_p = false;
5298 curr_usage_insns_check++;
5299 reloads_num = calls_num = 0;
5300 bitmap_clear (&check_only_regs);
5301 last_processed_bb = NULL;
5302 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5303 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5304 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5305 /* We don't process new insns generated in the loop. */
5306 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5308 prev_insn = PREV_INSN (curr_insn);
5309 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5310 curr_bb = BLOCK_FOR_INSN (curr_insn);
5311 if (last_processed_bb != curr_bb)
5313 /* We are at the end of BB. Add qualified living
5314 pseudos for potential splitting. */
5315 to_process = df_get_live_out (curr_bb);
5316 if (last_processed_bb != NULL)
5318 /* We are somewhere in the middle of EBB. */
5319 get_live_on_other_edges (curr_bb, last_processed_bb,
5320 &temp_bitmap);
5321 to_process = &temp_bitmap;
5323 last_processed_bb = curr_bb;
5324 last_insn = get_last_insertion_point (curr_bb);
5325 after_p = (! JUMP_P (last_insn)
5326 && (! CALL_P (last_insn)
5327 || (find_reg_note (last_insn,
5328 REG_NORETURN, NULL_RTX) == NULL_RTX
5329 && ! SIBLING_CALL_P (last_insn))));
5330 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5331 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5333 if ((int) j >= lra_constraint_new_regno_start)
5334 break;
5335 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5337 if (j < FIRST_PSEUDO_REGISTER)
5338 SET_HARD_REG_BIT (live_hard_regs, j);
5339 else
5340 add_to_hard_reg_set (&live_hard_regs,
5341 PSEUDO_REGNO_MODE (j),
5342 reg_renumber[j]);
5343 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5347 src_regno = dst_regno = -1;
5348 if (NONDEBUG_INSN_P (curr_insn)
5349 && (set = single_set (curr_insn)) != NULL_RTX
5350 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5352 src_regno = REGNO (SET_SRC (set));
5353 dst_regno = REGNO (SET_DEST (set));
5355 update_reloads_num_p = true;
5356 if (src_regno < lra_constraint_new_regno_start
5357 && src_regno >= FIRST_PSEUDO_REGISTER
5358 && reg_renumber[src_regno] < 0
5359 && dst_regno >= lra_constraint_new_regno_start
5360 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5362 /* 'reload_pseudo <- original_pseudo'. */
5363 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5364 reloads_num++;
5365 update_reloads_num_p = false;
5366 succ_p = false;
5367 if (usage_insns[src_regno].check == curr_usage_insns_check
5368 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5369 succ_p = inherit_reload_reg (false, src_regno, cl,
5370 curr_insn, next_usage_insns);
5371 if (succ_p)
5372 change_p = true;
5373 else
5374 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5375 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5376 IOR_HARD_REG_SET (potential_reload_hard_regs,
5377 reg_class_contents[cl]);
5379 else if (src_regno >= lra_constraint_new_regno_start
5380 && dst_regno < lra_constraint_new_regno_start
5381 && dst_regno >= FIRST_PSEUDO_REGISTER
5382 && reg_renumber[dst_regno] < 0
5383 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5384 && usage_insns[dst_regno].check == curr_usage_insns_check
5385 && (next_usage_insns
5386 = usage_insns[dst_regno].insns) != NULL_RTX)
5388 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5389 reloads_num++;
5390 update_reloads_num_p = false;
5391 /* 'original_pseudo <- reload_pseudo'. */
5392 if (! JUMP_P (curr_insn)
5393 && inherit_reload_reg (true, dst_regno, cl,
5394 curr_insn, next_usage_insns))
5395 change_p = true;
5396 /* Invalidate. */
5397 usage_insns[dst_regno].check = 0;
5398 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5399 IOR_HARD_REG_SET (potential_reload_hard_regs,
5400 reg_class_contents[cl]);
5402 else if (INSN_P (curr_insn))
5404 int iter;
5405 int max_uid = get_max_uid ();
5407 curr_id = lra_get_insn_recog_data (curr_insn);
5408 curr_static_id = curr_id->insn_static_data;
5409 to_inherit_num = 0;
5410 /* Process insn definitions. */
5411 for (iter = 0; iter < 2; iter++)
5412 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5413 reg != NULL;
5414 reg = reg->next)
5415 if (reg->type != OP_IN
5416 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5418 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5419 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5420 && usage_insns[dst_regno].check == curr_usage_insns_check
5421 && (next_usage_insns
5422 = usage_insns[dst_regno].insns) != NULL_RTX)
5424 struct lra_insn_reg *r;
5426 for (r = curr_id->regs; r != NULL; r = r->next)
5427 if (r->type != OP_OUT && r->regno == dst_regno)
5428 break;
5429 /* Don't do inheritance if the pseudo is also
5430 used in the insn. */
5431 if (r == NULL)
5432 /* We can not do inheritance right now
5433 because the current insn reg info (chain
5434 regs) can change after that. */
5435 add_to_inherit (dst_regno, next_usage_insns);
5437 /* We can not process one reg twice here because of
5438 usage_insns invalidation. */
5439 if ((dst_regno < FIRST_PSEUDO_REGISTER
5440 || reg_renumber[dst_regno] >= 0)
5441 && ! reg->subreg_p && reg->type != OP_IN)
5443 HARD_REG_SET s;
5445 if (split_if_necessary (dst_regno, reg->biggest_mode,
5446 potential_reload_hard_regs,
5447 false, curr_insn, max_uid))
5448 change_p = true;
5449 CLEAR_HARD_REG_SET (s);
5450 if (dst_regno < FIRST_PSEUDO_REGISTER)
5451 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5452 else
5453 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5454 reg_renumber[dst_regno]);
5455 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5457 /* We should invalidate potential inheritance or
5458 splitting for the current insn usages to the next
5459 usage insns (see code below) as the output pseudo
5460 prevents this. */
5461 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5462 && reg_renumber[dst_regno] < 0)
5463 || (reg->type == OP_OUT && ! reg->subreg_p
5464 && (dst_regno < FIRST_PSEUDO_REGISTER
5465 || reg_renumber[dst_regno] >= 0)))
5467 /* Invalidate and mark definitions. */
5468 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5469 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5470 else
5472 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5473 for (i = 0; i < nregs; i++)
5474 usage_insns[dst_regno + i].check
5475 = -(int) INSN_UID (curr_insn);
5479 if (! JUMP_P (curr_insn))
5480 for (i = 0; i < to_inherit_num; i++)
5481 if (inherit_reload_reg (true, to_inherit[i].regno,
5482 ALL_REGS, curr_insn,
5483 to_inherit[i].insns))
5484 change_p = true;
5485 if (CALL_P (curr_insn))
5487 rtx cheap, pat, dest;
5488 rtx_insn *restore;
5489 int regno, hard_regno;
5491 calls_num++;
5492 if ((cheap = find_reg_note (curr_insn,
5493 REG_RETURNED, NULL_RTX)) != NULL_RTX
5494 && ((cheap = XEXP (cheap, 0)), true)
5495 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5496 && (hard_regno = reg_renumber[regno]) >= 0
5497 /* If there are pending saves/restores, the
5498 optimization is not worth. */
5499 && usage_insns[regno].calls_num == calls_num - 1
5500 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5502 /* Restore the pseudo from the call result as
5503 REG_RETURNED note says that the pseudo value is
5504 in the call result and the pseudo is an argument
5505 of the call. */
5506 pat = PATTERN (curr_insn);
5507 if (GET_CODE (pat) == PARALLEL)
5508 pat = XVECEXP (pat, 0, 0);
5509 dest = SET_DEST (pat);
5510 /* For multiple return values dest is PARALLEL.
5511 Currently we handle only single return value case. */
5512 if (REG_P (dest))
5514 start_sequence ();
5515 emit_move_insn (cheap, copy_rtx (dest));
5516 restore = get_insns ();
5517 end_sequence ();
5518 lra_process_new_insns (curr_insn, NULL, restore,
5519 "Inserting call parameter restore");
5520 /* We don't need to save/restore of the pseudo from
5521 this call. */
5522 usage_insns[regno].calls_num = calls_num;
5523 bitmap_set_bit (&check_only_regs, regno);
5527 to_inherit_num = 0;
5528 /* Process insn usages. */
5529 for (iter = 0; iter < 2; iter++)
5530 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5531 reg != NULL;
5532 reg = reg->next)
5533 if ((reg->type != OP_OUT
5534 || (reg->type == OP_OUT && reg->subreg_p))
5535 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5537 if (src_regno >= FIRST_PSEUDO_REGISTER
5538 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5540 if (usage_insns[src_regno].check == curr_usage_insns_check
5541 && (next_usage_insns
5542 = usage_insns[src_regno].insns) != NULL_RTX
5543 && NONDEBUG_INSN_P (curr_insn))
5544 add_to_inherit (src_regno, next_usage_insns);
5545 else if (usage_insns[src_regno].check
5546 != -(int) INSN_UID (curr_insn))
5547 /* Add usages but only if the reg is not set up
5548 in the same insn. */
5549 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5551 else if (src_regno < FIRST_PSEUDO_REGISTER
5552 || reg_renumber[src_regno] >= 0)
5554 bool before_p;
5555 rtx_insn *use_insn = curr_insn;
5557 before_p = (JUMP_P (curr_insn)
5558 || (CALL_P (curr_insn) && reg->type == OP_IN));
5559 if (NONDEBUG_INSN_P (curr_insn)
5560 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5561 && split_if_necessary (src_regno, reg->biggest_mode,
5562 potential_reload_hard_regs,
5563 before_p, curr_insn, max_uid))
5565 if (reg->subreg_p)
5566 lra_risky_transformations_p = true;
5567 change_p = true;
5568 /* Invalidate. */
5569 usage_insns[src_regno].check = 0;
5570 if (before_p)
5571 use_insn = PREV_INSN (curr_insn);
5573 if (NONDEBUG_INSN_P (curr_insn))
5575 if (src_regno < FIRST_PSEUDO_REGISTER)
5576 add_to_hard_reg_set (&live_hard_regs,
5577 reg->biggest_mode, src_regno);
5578 else
5579 add_to_hard_reg_set (&live_hard_regs,
5580 PSEUDO_REGNO_MODE (src_regno),
5581 reg_renumber[src_regno]);
5583 add_next_usage_insn (src_regno, use_insn, reloads_num);
5586 /* Process call args. */
5587 if (curr_id->arg_hard_regs != NULL)
5588 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5589 if (src_regno < FIRST_PSEUDO_REGISTER)
5591 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5592 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5594 for (i = 0; i < to_inherit_num; i++)
5596 src_regno = to_inherit[i].regno;
5597 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5598 curr_insn, to_inherit[i].insns))
5599 change_p = true;
5600 else
5601 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5604 if (update_reloads_num_p
5605 && NONDEBUG_INSN_P (curr_insn)
5606 && (set = single_set (curr_insn)) != NULL_RTX)
5608 int regno = -1;
5609 if ((REG_P (SET_DEST (set))
5610 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5611 && reg_renumber[regno] < 0
5612 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5613 || (REG_P (SET_SRC (set))
5614 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5615 && reg_renumber[regno] < 0
5616 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5618 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5619 reloads_num++;
5620 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5621 IOR_HARD_REG_SET (potential_reload_hard_regs,
5622 reg_class_contents[cl]);
5625 /* We reached the start of the current basic block. */
5626 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5627 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5629 /* We reached the beginning of the current block -- do
5630 rest of spliting in the current BB. */
5631 to_process = df_get_live_in (curr_bb);
5632 if (BLOCK_FOR_INSN (head) != curr_bb)
5634 /* We are somewhere in the middle of EBB. */
5635 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5636 curr_bb, &temp_bitmap);
5637 to_process = &temp_bitmap;
5639 head_p = true;
5640 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5642 if ((int) j >= lra_constraint_new_regno_start)
5643 break;
5644 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5645 && usage_insns[j].check == curr_usage_insns_check
5646 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5648 if (need_for_split_p (potential_reload_hard_regs, j))
5650 if (lra_dump_file != NULL && head_p)
5652 fprintf (lra_dump_file,
5653 " ----------------------------------\n");
5654 head_p = false;
5656 if (split_reg (false, j, bb_note (curr_bb),
5657 next_usage_insns))
5658 change_p = true;
5660 usage_insns[j].check = 0;
5665 return change_p;
5668 /* This value affects EBB forming. If probability of edge from EBB to
5669 a BB is not greater than the following value, we don't add the BB
5670 to EBB. */
5671 #define EBB_PROBABILITY_CUTOFF \
5672 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
5674 /* Current number of inheritance/split iteration. */
5675 int lra_inheritance_iter;
5677 /* Entry function for inheritance/split pass. */
5678 void
5679 lra_inheritance (void)
5681 int i;
5682 basic_block bb, start_bb;
5683 edge e;
5685 lra_inheritance_iter++;
5686 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5687 return;
5688 timevar_push (TV_LRA_INHERITANCE);
5689 if (lra_dump_file != NULL)
5690 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5691 lra_inheritance_iter);
5692 curr_usage_insns_check = 0;
5693 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5694 for (i = 0; i < lra_constraint_new_regno_start; i++)
5695 usage_insns[i].check = 0;
5696 bitmap_initialize (&check_only_regs, &reg_obstack);
5697 bitmap_initialize (&live_regs, &reg_obstack);
5698 bitmap_initialize (&temp_bitmap, &reg_obstack);
5699 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5700 FOR_EACH_BB_FN (bb, cfun)
5702 start_bb = bb;
5703 if (lra_dump_file != NULL)
5704 fprintf (lra_dump_file, "EBB");
5705 /* Form a EBB starting with BB. */
5706 bitmap_clear (&ebb_global_regs);
5707 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5708 for (;;)
5710 if (lra_dump_file != NULL)
5711 fprintf (lra_dump_file, " %d", bb->index);
5712 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5713 || LABEL_P (BB_HEAD (bb->next_bb)))
5714 break;
5715 e = find_fallthru_edge (bb->succs);
5716 if (! e)
5717 break;
5718 if (e->probability < EBB_PROBABILITY_CUTOFF)
5719 break;
5720 bb = bb->next_bb;
5722 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5723 if (lra_dump_file != NULL)
5724 fprintf (lra_dump_file, "\n");
5725 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5726 /* Remember that the EBB head and tail can change in
5727 inherit_in_ebb. */
5728 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5730 bitmap_clear (&ebb_global_regs);
5731 bitmap_clear (&temp_bitmap);
5732 bitmap_clear (&live_regs);
5733 bitmap_clear (&check_only_regs);
5734 free (usage_insns);
5736 timevar_pop (TV_LRA_INHERITANCE);
5741 /* This page contains code to undo failed inheritance/split
5742 transformations. */
5744 /* Current number of iteration undoing inheritance/split. */
5745 int lra_undo_inheritance_iter;
5747 /* Fix BB live info LIVE after removing pseudos created on pass doing
5748 inheritance/split which are REMOVED_PSEUDOS. */
5749 static void
5750 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5752 unsigned int regno;
5753 bitmap_iterator bi;
5755 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5756 if (bitmap_clear_bit (live, regno))
5757 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5760 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5761 number. */
5762 static int
5763 get_regno (rtx reg)
5765 if (GET_CODE (reg) == SUBREG)
5766 reg = SUBREG_REG (reg);
5767 if (REG_P (reg))
5768 return REGNO (reg);
5769 return -1;
5772 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5773 return true if we did any change. The undo transformations for
5774 inheritance looks like
5775 i <- i2
5776 p <- i => p <- i2
5777 or removing
5778 p <- i, i <- p, and i <- i3
5779 where p is original pseudo from which inheritance pseudo i was
5780 created, i and i3 are removed inheritance pseudos, i2 is another
5781 not removed inheritance pseudo. All split pseudos or other
5782 occurrences of removed inheritance pseudos are changed on the
5783 corresponding original pseudos.
5785 The function also schedules insns changed and created during
5786 inheritance/split pass for processing by the subsequent constraint
5787 pass. */
5788 static bool
5789 remove_inheritance_pseudos (bitmap remove_pseudos)
5791 basic_block bb;
5792 int regno, sregno, prev_sregno, dregno, restore_regno;
5793 rtx set, prev_set;
5794 rtx_insn *prev_insn;
5795 bool change_p, done_p;
5797 change_p = ! bitmap_empty_p (remove_pseudos);
5798 /* We can not finish the function right away if CHANGE_P is true
5799 because we need to marks insns affected by previous
5800 inheritance/split pass for processing by the subsequent
5801 constraint pass. */
5802 FOR_EACH_BB_FN (bb, cfun)
5804 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5805 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5806 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5808 if (! INSN_P (curr_insn))
5809 continue;
5810 done_p = false;
5811 sregno = dregno = -1;
5812 if (change_p && NONDEBUG_INSN_P (curr_insn)
5813 && (set = single_set (curr_insn)) != NULL_RTX)
5815 dregno = get_regno (SET_DEST (set));
5816 sregno = get_regno (SET_SRC (set));
5819 if (sregno >= 0 && dregno >= 0)
5821 if ((bitmap_bit_p (remove_pseudos, sregno)
5822 && (lra_reg_info[sregno].restore_regno == dregno
5823 || (bitmap_bit_p (remove_pseudos, dregno)
5824 && (lra_reg_info[sregno].restore_regno
5825 == lra_reg_info[dregno].restore_regno))))
5826 || (bitmap_bit_p (remove_pseudos, dregno)
5827 && lra_reg_info[dregno].restore_regno == sregno))
5828 /* One of the following cases:
5829 original <- removed inheritance pseudo
5830 removed inherit pseudo <- another removed inherit pseudo
5831 removed inherit pseudo <- original pseudo
5833 removed_split_pseudo <- original_reg
5834 original_reg <- removed_split_pseudo */
5836 if (lra_dump_file != NULL)
5838 fprintf (lra_dump_file, " Removing %s:\n",
5839 bitmap_bit_p (&lra_split_regs, sregno)
5840 || bitmap_bit_p (&lra_split_regs, dregno)
5841 ? "split" : "inheritance");
5842 dump_insn_slim (lra_dump_file, curr_insn);
5844 lra_set_insn_deleted (curr_insn);
5845 done_p = true;
5847 else if (bitmap_bit_p (remove_pseudos, sregno)
5848 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5850 /* Search the following pattern:
5851 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5852 original_pseudo <- inherit_or_split_pseudo1
5853 where the 2nd insn is the current insn and
5854 inherit_or_split_pseudo2 is not removed. If it is found,
5855 change the current insn onto:
5856 original_pseudo <- inherit_or_split_pseudo2. */
5857 for (prev_insn = PREV_INSN (curr_insn);
5858 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5859 prev_insn = PREV_INSN (prev_insn))
5861 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5862 && (prev_set = single_set (prev_insn)) != NULL_RTX
5863 /* There should be no subregs in insn we are
5864 searching because only the original reg might
5865 be in subreg when we changed the mode of
5866 load/store for splitting. */
5867 && REG_P (SET_DEST (prev_set))
5868 && REG_P (SET_SRC (prev_set))
5869 && (int) REGNO (SET_DEST (prev_set)) == sregno
5870 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5871 >= FIRST_PSEUDO_REGISTER)
5872 /* As we consider chain of inheritance or
5873 splitting described in above comment we should
5874 check that sregno and prev_sregno were
5875 inheritance/split pseudos created from the
5876 same original regno. */
5877 && (lra_reg_info[sregno].restore_regno
5878 == lra_reg_info[prev_sregno].restore_regno)
5879 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5881 lra_assert (GET_MODE (SET_SRC (prev_set))
5882 == GET_MODE (regno_reg_rtx[sregno]));
5883 if (GET_CODE (SET_SRC (set)) == SUBREG)
5884 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5885 else
5886 SET_SRC (set) = SET_SRC (prev_set);
5887 /* As we are finishing with processing the insn
5888 here, check the destination too as it might
5889 inheritance pseudo for another pseudo. */
5890 if (bitmap_bit_p (remove_pseudos, dregno)
5891 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
5892 && (restore_regno
5893 = lra_reg_info[dregno].restore_regno) >= 0)
5895 if (GET_CODE (SET_DEST (set)) == SUBREG)
5896 SUBREG_REG (SET_DEST (set))
5897 = regno_reg_rtx[restore_regno];
5898 else
5899 SET_DEST (set) = regno_reg_rtx[restore_regno];
5901 lra_push_insn_and_update_insn_regno_info (curr_insn);
5902 lra_set_used_insn_alternative_by_uid
5903 (INSN_UID (curr_insn), -1);
5904 done_p = true;
5905 if (lra_dump_file != NULL)
5907 fprintf (lra_dump_file, " Change reload insn:\n");
5908 dump_insn_slim (lra_dump_file, curr_insn);
5913 if (! done_p)
5915 struct lra_insn_reg *reg;
5916 bool restored_regs_p = false;
5917 bool kept_regs_p = false;
5919 curr_id = lra_get_insn_recog_data (curr_insn);
5920 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5922 regno = reg->regno;
5923 restore_regno = lra_reg_info[regno].restore_regno;
5924 if (restore_regno >= 0)
5926 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5928 lra_substitute_pseudo_within_insn
5929 (curr_insn, regno, regno_reg_rtx[restore_regno],
5930 false);
5931 restored_regs_p = true;
5933 else
5934 kept_regs_p = true;
5937 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5939 /* The instruction has changed since the previous
5940 constraints pass. */
5941 lra_push_insn_and_update_insn_regno_info (curr_insn);
5942 lra_set_used_insn_alternative_by_uid
5943 (INSN_UID (curr_insn), -1);
5945 else if (restored_regs_p)
5946 /* The instruction has been restored to the form that
5947 it had during the previous constraints pass. */
5948 lra_update_insn_regno_info (curr_insn);
5949 if (restored_regs_p && lra_dump_file != NULL)
5951 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5952 dump_insn_slim (lra_dump_file, curr_insn);
5957 return change_p;
5960 /* If optional reload pseudos failed to get a hard register or was not
5961 inherited, it is better to remove optional reloads. We do this
5962 transformation after undoing inheritance to figure out necessity to
5963 remove optional reloads easier. Return true if we do any
5964 change. */
5965 static bool
5966 undo_optional_reloads (void)
5968 bool change_p, keep_p;
5969 unsigned int regno, uid;
5970 bitmap_iterator bi, bi2;
5971 rtx_insn *insn;
5972 rtx set, src, dest;
5973 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5975 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5976 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5977 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5979 keep_p = false;
5980 /* Keep optional reloads from previous subpasses. */
5981 if (lra_reg_info[regno].restore_regno < 0
5982 /* If the original pseudo changed its allocation, just
5983 removing the optional pseudo is dangerous as the original
5984 pseudo will have longer live range. */
5985 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
5986 keep_p = true;
5987 else if (reg_renumber[regno] >= 0)
5988 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
5990 insn = lra_insn_recog_data[uid]->insn;
5991 if ((set = single_set (insn)) == NULL_RTX)
5992 continue;
5993 src = SET_SRC (set);
5994 dest = SET_DEST (set);
5995 if (! REG_P (src) || ! REG_P (dest))
5996 continue;
5997 if (REGNO (dest) == regno
5998 /* Ignore insn for optional reloads itself. */
5999 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
6000 /* Check only inheritance on last inheritance pass. */
6001 && (int) REGNO (src) >= new_regno_start
6002 /* Check that the optional reload was inherited. */
6003 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6005 keep_p = true;
6006 break;
6009 if (keep_p)
6011 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6012 if (lra_dump_file != NULL)
6013 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6016 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6017 bitmap_initialize (&insn_bitmap, &reg_obstack);
6018 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6020 if (lra_dump_file != NULL)
6021 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6022 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6023 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6025 insn = lra_insn_recog_data[uid]->insn;
6026 if ((set = single_set (insn)) != NULL_RTX)
6028 src = SET_SRC (set);
6029 dest = SET_DEST (set);
6030 if (REG_P (src) && REG_P (dest)
6031 && ((REGNO (src) == regno
6032 && (lra_reg_info[regno].restore_regno
6033 == (int) REGNO (dest)))
6034 || (REGNO (dest) == regno
6035 && (lra_reg_info[regno].restore_regno
6036 == (int) REGNO (src)))))
6038 if (lra_dump_file != NULL)
6040 fprintf (lra_dump_file, " Deleting move %u\n",
6041 INSN_UID (insn));
6042 dump_insn_slim (lra_dump_file, insn);
6044 lra_set_insn_deleted (insn);
6045 continue;
6047 /* We should not worry about generation memory-memory
6048 moves here as if the corresponding inheritance did
6049 not work (inheritance pseudo did not get a hard reg),
6050 we remove the inheritance pseudo and the optional
6051 reload. */
6053 lra_substitute_pseudo_within_insn
6054 (insn, regno, regno_reg_rtx[lra_reg_info[regno].restore_regno],
6055 false);
6056 lra_update_insn_regno_info (insn);
6057 if (lra_dump_file != NULL)
6059 fprintf (lra_dump_file,
6060 " Restoring original insn:\n");
6061 dump_insn_slim (lra_dump_file, insn);
6065 /* Clear restore_regnos. */
6066 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6067 lra_reg_info[regno].restore_regno = -1;
6068 bitmap_clear (&insn_bitmap);
6069 bitmap_clear (&removed_optional_reload_pseudos);
6070 return change_p;
6073 /* Entry function for undoing inheritance/split transformation. Return true
6074 if we did any RTL change in this pass. */
6075 bool
6076 lra_undo_inheritance (void)
6078 unsigned int regno;
6079 int restore_regno, hard_regno;
6080 int n_all_inherit, n_inherit, n_all_split, n_split;
6081 bitmap_head remove_pseudos;
6082 bitmap_iterator bi;
6083 bool change_p;
6085 lra_undo_inheritance_iter++;
6086 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6087 return false;
6088 if (lra_dump_file != NULL)
6089 fprintf (lra_dump_file,
6090 "\n********** Undoing inheritance #%d: **********\n\n",
6091 lra_undo_inheritance_iter);
6092 bitmap_initialize (&remove_pseudos, &reg_obstack);
6093 n_inherit = n_all_inherit = 0;
6094 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6095 if (lra_reg_info[regno].restore_regno >= 0)
6097 n_all_inherit++;
6098 if (reg_renumber[regno] < 0
6099 /* If the original pseudo changed its allocation, just
6100 removing inheritance is dangerous as for changing
6101 allocation we used shorter live-ranges. */
6102 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6103 bitmap_set_bit (&remove_pseudos, regno);
6104 else
6105 n_inherit++;
6107 if (lra_dump_file != NULL && n_all_inherit != 0)
6108 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6109 n_inherit, n_all_inherit,
6110 (double) n_inherit / n_all_inherit * 100);
6111 n_split = n_all_split = 0;
6112 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6113 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6115 n_all_split++;
6116 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6117 ? reg_renumber[restore_regno] : restore_regno);
6118 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6119 bitmap_set_bit (&remove_pseudos, regno);
6120 else
6122 n_split++;
6123 if (lra_dump_file != NULL)
6124 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6125 regno, restore_regno);
6128 if (lra_dump_file != NULL && n_all_split != 0)
6129 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6130 n_split, n_all_split,
6131 (double) n_split / n_all_split * 100);
6132 change_p = remove_inheritance_pseudos (&remove_pseudos);
6133 bitmap_clear (&remove_pseudos);
6134 /* Clear restore_regnos. */
6135 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6136 lra_reg_info[regno].restore_regno = -1;
6137 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6138 lra_reg_info[regno].restore_regno = -1;
6139 change_p = undo_optional_reloads () || change_p;
6140 return change_p;