Move some comparison simplifications to match.pd
[official-gcc.git] / gcc / lra-constraints.c
blobddb91dd49cf9aaf6a83c7ff62aa8848ba3865925
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 "backend.h"
113 #include "predict.h"
114 #include "tree.h"
115 #include "rtl.h"
116 #include "df.h"
117 #include "tm_p.h"
118 #include "regs.h"
119 #include "insn-config.h"
120 #include "insn-codes.h"
121 #include "recog.h"
122 #include "output.h"
123 #include "addresses.h"
124 #include "target.h"
125 #include "flags.h"
126 #include "alias.h"
127 #include "expmed.h"
128 #include "dojump.h"
129 #include "explow.h"
130 #include "calls.h"
131 #include "emit-rtl.h"
132 #include "varasm.h"
133 #include "stmt.h"
134 #include "expr.h"
135 #include "cfgrtl.h"
136 #include "except.h"
137 #include "optabs.h"
138 #include "ira.h"
139 #include "rtl-error.h"
140 #include "params.h"
141 #include "lra.h"
142 #include "insn-attr.h"
143 #include "lra-int.h"
145 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
146 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
147 reload insns. */
148 static int bb_reload_num;
150 /* The current insn being processed and corresponding its single set
151 (NULL otherwise), its data (basic block, the insn data, the insn
152 static data, and the mode of each operand). */
153 static rtx_insn *curr_insn;
154 static rtx curr_insn_set;
155 static basic_block curr_bb;
156 static lra_insn_recog_data_t curr_id;
157 static struct lra_static_insn_data *curr_static_id;
158 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
159 /* Mode of the register substituted by its equivalence with VOIDmode
160 (e.g. constant) and whose subreg is given operand of the current
161 insn. VOIDmode in all other cases. */
162 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
166 /* Start numbers for new registers and insns at the current constraints
167 pass start. */
168 static int new_regno_start;
169 static int new_insn_uid_start;
171 /* If LOC is nonnull, strip any outer subreg from it. */
172 static inline rtx *
173 strip_subreg (rtx *loc)
175 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
178 /* Return hard regno of REGNO or if it is was not assigned to a hard
179 register, use a hard register from its allocno class. */
180 static int
181 get_try_hard_regno (int regno)
183 int hard_regno;
184 enum reg_class rclass;
186 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
187 hard_regno = lra_get_regno_hard_regno (regno);
188 if (hard_regno >= 0)
189 return hard_regno;
190 rclass = lra_get_allocno_class (regno);
191 if (rclass == NO_REGS)
192 return -1;
193 return ira_class_hard_regs[rclass][0];
196 /* Return final hard regno (plus offset) which will be after
197 elimination. We do this for matching constraints because the final
198 hard regno could have a different class. */
199 static int
200 get_final_hard_regno (int hard_regno, int offset)
202 if (hard_regno < 0)
203 return hard_regno;
204 hard_regno = lra_get_elimination_hard_regno (hard_regno);
205 return hard_regno + offset;
208 /* Return hard regno of X after removing subreg and making
209 elimination. If X is not a register or subreg of register, return
210 -1. For pseudo use its assignment. */
211 static int
212 get_hard_regno (rtx x)
214 rtx reg;
215 int offset, hard_regno;
217 reg = x;
218 if (GET_CODE (x) == SUBREG)
219 reg = SUBREG_REG (x);
220 if (! REG_P (reg))
221 return -1;
222 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
223 hard_regno = lra_get_regno_hard_regno (hard_regno);
224 if (hard_regno < 0)
225 return -1;
226 offset = 0;
227 if (GET_CODE (x) == SUBREG)
228 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
229 SUBREG_BYTE (x), GET_MODE (x));
230 return get_final_hard_regno (hard_regno, offset);
233 /* If REGNO is a hard register or has been allocated a hard register,
234 return the class of that register. If REGNO is a reload pseudo
235 created by the current constraints pass, return its allocno class.
236 Return NO_REGS otherwise. */
237 static enum reg_class
238 get_reg_class (int regno)
240 int hard_regno;
242 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
243 hard_regno = lra_get_regno_hard_regno (regno);
244 if (hard_regno >= 0)
246 hard_regno = get_final_hard_regno (hard_regno, 0);
247 return REGNO_REG_CLASS (hard_regno);
249 if (regno >= new_regno_start)
250 return lra_get_allocno_class (regno);
251 return NO_REGS;
254 /* Return true if REG satisfies (or will satisfy) reg class constraint
255 CL. Use elimination first if REG is a hard register. If REG is a
256 reload pseudo created by this constraints pass, assume that it will
257 be allocated a hard register from its allocno class, but allow that
258 class to be narrowed to CL if it is currently a superset of CL.
260 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
261 REGNO (reg), or NO_REGS if no change in its class was needed. */
262 static bool
263 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
265 enum reg_class rclass, common_class;
266 machine_mode reg_mode;
267 int class_size, hard_regno, nregs, i, j;
268 int regno = REGNO (reg);
270 if (new_class != NULL)
271 *new_class = NO_REGS;
272 if (regno < FIRST_PSEUDO_REGISTER)
274 rtx final_reg = reg;
275 rtx *final_loc = &final_reg;
277 lra_eliminate_reg_if_possible (final_loc);
278 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
280 reg_mode = GET_MODE (reg);
281 rclass = get_reg_class (regno);
282 if (regno < new_regno_start
283 /* Do not allow the constraints for reload instructions to
284 influence the classes of new pseudos. These reloads are
285 typically moves that have many alternatives, and restricting
286 reload pseudos for one alternative may lead to situations
287 where other reload pseudos are no longer allocatable. */
288 || (INSN_UID (curr_insn) >= new_insn_uid_start
289 && curr_insn_set != NULL
290 && ((OBJECT_P (SET_SRC (curr_insn_set))
291 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
292 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
293 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
294 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
295 /* When we don't know what class will be used finally for reload
296 pseudos, we use ALL_REGS. */
297 return ((regno >= new_regno_start && rclass == ALL_REGS)
298 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
299 && ! hard_reg_set_subset_p (reg_class_contents[cl],
300 lra_no_alloc_regs)));
301 else
303 common_class = ira_reg_class_subset[rclass][cl];
304 if (new_class != NULL)
305 *new_class = common_class;
306 if (hard_reg_set_subset_p (reg_class_contents[common_class],
307 lra_no_alloc_regs))
308 return false;
309 /* Check that there are enough allocatable regs. */
310 class_size = ira_class_hard_regs_num[common_class];
311 for (i = 0; i < class_size; i++)
313 hard_regno = ira_class_hard_regs[common_class][i];
314 nregs = hard_regno_nregs[hard_regno][reg_mode];
315 if (nregs == 1)
316 return true;
317 for (j = 0; j < nregs; j++)
318 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
319 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
320 hard_regno + j))
321 break;
322 if (j >= nregs)
323 return true;
325 return false;
329 /* Return true if REGNO satisfies a memory constraint. */
330 static bool
331 in_mem_p (int regno)
333 return get_reg_class (regno) == NO_REGS;
336 /* Return 1 if ADDR is a valid memory address for mode MODE in address
337 space AS, and check that each pseudo has the proper kind of hard
338 reg. */
339 static int
340 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
341 rtx addr, addr_space_t as)
343 #ifdef GO_IF_LEGITIMATE_ADDRESS
344 lra_assert (ADDR_SPACE_GENERIC_P (as));
345 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
346 return 0;
348 win:
349 return 1;
350 #else
351 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
352 #endif
355 namespace {
356 /* Temporarily eliminates registers in an address (for the lifetime of
357 the object). */
358 class address_eliminator {
359 public:
360 address_eliminator (struct address_info *ad);
361 ~address_eliminator ();
363 private:
364 struct address_info *m_ad;
365 rtx *m_base_loc;
366 rtx m_base_reg;
367 rtx *m_index_loc;
368 rtx m_index_reg;
372 address_eliminator::address_eliminator (struct address_info *ad)
373 : m_ad (ad),
374 m_base_loc (strip_subreg (ad->base_term)),
375 m_base_reg (NULL_RTX),
376 m_index_loc (strip_subreg (ad->index_term)),
377 m_index_reg (NULL_RTX)
379 if (m_base_loc != NULL)
381 m_base_reg = *m_base_loc;
382 lra_eliminate_reg_if_possible (m_base_loc);
383 if (m_ad->base_term2 != NULL)
384 *m_ad->base_term2 = *m_ad->base_term;
386 if (m_index_loc != NULL)
388 m_index_reg = *m_index_loc;
389 lra_eliminate_reg_if_possible (m_index_loc);
393 address_eliminator::~address_eliminator ()
395 if (m_base_loc && *m_base_loc != m_base_reg)
397 *m_base_loc = m_base_reg;
398 if (m_ad->base_term2 != NULL)
399 *m_ad->base_term2 = *m_ad->base_term;
401 if (m_index_loc && *m_index_loc != m_index_reg)
402 *m_index_loc = m_index_reg;
405 /* Return true if the eliminated form of AD is a legitimate target address. */
406 static bool
407 valid_address_p (struct address_info *ad)
409 address_eliminator eliminator (ad);
410 return valid_address_p (ad->mode, *ad->outer, ad->as);
413 /* Return true if the eliminated form of memory reference OP satisfies
414 extra memory constraint CONSTRAINT. */
415 static bool
416 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
418 struct address_info ad;
420 decompose_mem_address (&ad, op);
421 address_eliminator eliminator (&ad);
422 return constraint_satisfied_p (op, constraint);
425 /* Return true if the eliminated form of address AD satisfies extra
426 address constraint CONSTRAINT. */
427 static bool
428 satisfies_address_constraint_p (struct address_info *ad,
429 enum constraint_num constraint)
431 address_eliminator eliminator (ad);
432 return constraint_satisfied_p (*ad->outer, constraint);
435 /* Return true if the eliminated form of address OP satisfies extra
436 address constraint CONSTRAINT. */
437 static bool
438 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
440 struct address_info ad;
442 decompose_lea_address (&ad, &op);
443 return satisfies_address_constraint_p (&ad, constraint);
446 /* Initiate equivalences for LRA. As we keep original equivalences
447 before any elimination, we need to make copies otherwise any change
448 in insns might change the equivalences. */
449 void
450 lra_init_equiv (void)
452 ira_expand_reg_equiv ();
453 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
455 rtx res;
457 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
458 ira_reg_equiv[i].memory = copy_rtx (res);
459 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
460 ira_reg_equiv[i].invariant = copy_rtx (res);
464 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
466 /* Update equivalence for REGNO. We need to this as the equivalence
467 might contain other pseudos which are changed by their
468 equivalences. */
469 static void
470 update_equiv (int regno)
472 rtx x;
474 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
475 ira_reg_equiv[regno].memory
476 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
477 NULL_RTX);
478 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
479 ira_reg_equiv[regno].invariant
480 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
481 NULL_RTX);
484 /* If we have decided to substitute X with another value, return that
485 value, otherwise return X. */
486 static rtx
487 get_equiv (rtx x)
489 int regno;
490 rtx res;
492 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
493 || ! ira_reg_equiv[regno].defined_p
494 || ! ira_reg_equiv[regno].profitable_p
495 || lra_get_regno_hard_regno (regno) >= 0)
496 return x;
497 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
499 if (targetm.cannot_substitute_mem_equiv_p (res))
500 return x;
501 return res;
503 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
504 return res;
505 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
506 return res;
507 gcc_unreachable ();
510 /* If we have decided to substitute X with the equivalent value,
511 return that value after elimination for INSN, otherwise return
512 X. */
513 static rtx
514 get_equiv_with_elimination (rtx x, rtx_insn *insn)
516 rtx res = get_equiv (x);
518 if (x == res || CONSTANT_P (res))
519 return res;
520 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
521 false, false, 0, true);
524 /* Set up curr_operand_mode. */
525 static void
526 init_curr_operand_mode (void)
528 int nop = curr_static_id->n_operands;
529 for (int i = 0; i < nop; i++)
531 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
532 if (mode == VOIDmode)
534 /* The .md mode for address operands is the mode of the
535 addressed value rather than the mode of the address itself. */
536 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
537 mode = Pmode;
538 else
539 mode = curr_static_id->operand[i].mode;
541 curr_operand_mode[i] = mode;
547 /* The page contains code to reuse input reloads. */
549 /* Structure describes input reload of the current insns. */
550 struct input_reload
552 /* Reloaded value. */
553 rtx input;
554 /* Reload pseudo used. */
555 rtx reg;
558 /* The number of elements in the following array. */
559 static int curr_insn_input_reloads_num;
560 /* Array containing info about input reloads. It is used to find the
561 same input reload and reuse the reload pseudo in this case. */
562 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
564 /* Initiate data concerning reuse of input reloads for the current
565 insn. */
566 static void
567 init_curr_insn_input_reloads (void)
569 curr_insn_input_reloads_num = 0;
572 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
573 created input reload pseudo (only if TYPE is not OP_OUT). Don't
574 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
575 wrapped up in SUBREG. The result pseudo is returned through
576 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
577 reused the already created input reload pseudo. Use TITLE to
578 describe new registers for debug purposes. */
579 static bool
580 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
581 enum reg_class rclass, bool in_subreg_p,
582 const char *title, rtx *result_reg)
584 int i, regno;
585 enum reg_class new_class;
587 if (type == OP_OUT)
589 *result_reg
590 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
591 return true;
593 /* Prevent reuse value of expression with side effects,
594 e.g. volatile memory. */
595 if (! side_effects_p (original))
596 for (i = 0; i < curr_insn_input_reloads_num; i++)
597 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
598 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
600 rtx reg = curr_insn_input_reloads[i].reg;
601 regno = REGNO (reg);
602 /* If input is equal to original and both are VOIDmode,
603 GET_MODE (reg) might be still different from mode.
604 Ensure we don't return *result_reg with wrong mode. */
605 if (GET_MODE (reg) != mode)
607 if (in_subreg_p)
608 continue;
609 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
610 continue;
611 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
612 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
613 continue;
615 *result_reg = reg;
616 if (lra_dump_file != NULL)
618 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
619 dump_value_slim (lra_dump_file, original, 1);
621 if (new_class != lra_get_allocno_class (regno))
622 lra_change_class (regno, new_class, ", change to", false);
623 if (lra_dump_file != NULL)
624 fprintf (lra_dump_file, "\n");
625 return false;
627 *result_reg = lra_create_new_reg (mode, original, rclass, title);
628 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
629 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
630 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
631 return true;
636 /* The page contains code to extract memory address parts. */
638 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
639 static inline bool
640 ok_for_index_p_nonstrict (rtx reg)
642 unsigned regno = REGNO (reg);
644 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
647 /* A version of regno_ok_for_base_p for use here, when all pseudos
648 should count as OK. Arguments as for regno_ok_for_base_p. */
649 static inline bool
650 ok_for_base_p_nonstrict (rtx reg, machine_mode mode, addr_space_t as,
651 enum rtx_code outer_code, enum rtx_code index_code)
653 unsigned regno = REGNO (reg);
655 if (regno >= FIRST_PSEUDO_REGISTER)
656 return true;
657 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
662 /* The page contains major code to choose the current insn alternative
663 and generate reloads for it. */
665 /* Return the offset from REGNO of the least significant register
666 in (reg:MODE REGNO).
668 This function is used to tell whether two registers satisfy
669 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
671 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
672 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
674 lra_constraint_offset (int regno, machine_mode mode)
676 lra_assert (regno < FIRST_PSEUDO_REGISTER);
677 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
678 && SCALAR_INT_MODE_P (mode))
679 return hard_regno_nregs[regno][mode] - 1;
680 return 0;
683 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
684 if they are the same hard reg, and has special hacks for
685 auto-increment and auto-decrement. This is specifically intended for
686 process_alt_operands to use in determining whether two operands
687 match. X is the operand whose number is the lower of the two.
689 It is supposed that X is the output operand and Y is the input
690 operand. Y_HARD_REGNO is the final hard regno of register Y or
691 register in subreg Y as we know it now. Otherwise, it is a
692 negative value. */
693 static bool
694 operands_match_p (rtx x, rtx y, int y_hard_regno)
696 int i;
697 RTX_CODE code = GET_CODE (x);
698 const char *fmt;
700 if (x == y)
701 return true;
702 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
703 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
705 int j;
707 i = get_hard_regno (x);
708 if (i < 0)
709 goto slow;
711 if ((j = y_hard_regno) < 0)
712 goto slow;
714 i += lra_constraint_offset (i, GET_MODE (x));
715 j += lra_constraint_offset (j, GET_MODE (y));
717 return i == j;
720 /* If two operands must match, because they are really a single
721 operand of an assembler insn, then two post-increments are invalid
722 because the assembler insn would increment only once. On the
723 other hand, a post-increment matches ordinary indexing if the
724 post-increment is the output operand. */
725 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
726 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
728 /* Two pre-increments are invalid because the assembler insn would
729 increment only once. On the other hand, a pre-increment matches
730 ordinary indexing if the pre-increment is the input operand. */
731 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
732 || GET_CODE (y) == PRE_MODIFY)
733 return operands_match_p (x, XEXP (y, 0), -1);
735 slow:
737 if (code == REG && REG_P (y))
738 return REGNO (x) == REGNO (y);
740 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
741 && x == SUBREG_REG (y))
742 return true;
743 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
744 && SUBREG_REG (x) == y)
745 return true;
747 /* Now we have disposed of all the cases in which different rtx
748 codes can match. */
749 if (code != GET_CODE (y))
750 return false;
752 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
753 if (GET_MODE (x) != GET_MODE (y))
754 return false;
756 switch (code)
758 CASE_CONST_UNIQUE:
759 return false;
761 case LABEL_REF:
762 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
763 case SYMBOL_REF:
764 return XSTR (x, 0) == XSTR (y, 0);
766 default:
767 break;
770 /* Compare the elements. If any pair of corresponding elements fail
771 to match, return false for the whole things. */
773 fmt = GET_RTX_FORMAT (code);
774 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
776 int val, j;
777 switch (fmt[i])
779 case 'w':
780 if (XWINT (x, i) != XWINT (y, i))
781 return false;
782 break;
784 case 'i':
785 if (XINT (x, i) != XINT (y, i))
786 return false;
787 break;
789 case 'e':
790 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
791 if (val == 0)
792 return false;
793 break;
795 case '0':
796 break;
798 case 'E':
799 if (XVECLEN (x, i) != XVECLEN (y, i))
800 return false;
801 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
803 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
804 if (val == 0)
805 return false;
807 break;
809 /* It is believed that rtx's at this level will never
810 contain anything but integers and other rtx's, except for
811 within LABEL_REFs and SYMBOL_REFs. */
812 default:
813 gcc_unreachable ();
816 return true;
819 /* True if X is a constant that can be forced into the constant pool.
820 MODE is the mode of the operand, or VOIDmode if not known. */
821 #define CONST_POOL_OK_P(MODE, X) \
822 ((MODE) != VOIDmode \
823 && CONSTANT_P (X) \
824 && GET_CODE (X) != HIGH \
825 && !targetm.cannot_force_const_mem (MODE, X))
827 /* True if C is a non-empty register class that has too few registers
828 to be safely used as a reload target class. */
829 #define SMALL_REGISTER_CLASS_P(C) \
830 (ira_class_hard_regs_num [(C)] == 1 \
831 || (ira_class_hard_regs_num [(C)] >= 1 \
832 && targetm.class_likely_spilled_p (C)))
834 /* If REG is a reload pseudo, try to make its class satisfying CL. */
835 static void
836 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
838 enum reg_class rclass;
840 /* Do not make more accurate class from reloads generated. They are
841 mostly moves with a lot of constraints. Making more accurate
842 class may results in very narrow class and impossibility of find
843 registers for several reloads of one insn. */
844 if (INSN_UID (curr_insn) >= new_insn_uid_start)
845 return;
846 if (GET_CODE (reg) == SUBREG)
847 reg = SUBREG_REG (reg);
848 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
849 return;
850 if (in_class_p (reg, cl, &rclass) && rclass != cl)
851 lra_change_class (REGNO (reg), rclass, " Change to", true);
854 /* Generate reloads for matching OUT and INS (array of input operand
855 numbers with end marker -1) with reg class GOAL_CLASS. Add input
856 and output reloads correspondingly to the lists *BEFORE and *AFTER.
857 OUT might be negative. In this case we generate input reloads for
858 matched input operands INS. */
859 static void
860 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
861 rtx_insn **before, rtx_insn **after)
863 int i, in;
864 rtx new_in_reg, new_out_reg, reg;
865 machine_mode inmode, outmode;
866 rtx in_rtx = *curr_id->operand_loc[ins[0]];
867 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
869 inmode = curr_operand_mode[ins[0]];
870 outmode = out < 0 ? inmode : curr_operand_mode[out];
871 push_to_sequence (*before);
872 if (inmode != outmode)
874 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
876 reg = new_in_reg
877 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
878 goal_class, "");
879 if (SCALAR_INT_MODE_P (inmode))
880 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
881 else
882 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
883 LRA_SUBREG_P (new_out_reg) = 1;
884 /* If the input reg is dying here, we can use the same hard
885 register for REG and IN_RTX. We do it only for original
886 pseudos as reload pseudos can die although original
887 pseudos still live where reload pseudos dies. */
888 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
889 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
890 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
892 else
894 reg = new_out_reg
895 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
896 goal_class, "");
897 if (SCALAR_INT_MODE_P (outmode))
898 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
899 else
900 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
901 /* NEW_IN_REG is non-paradoxical subreg. We don't want
902 NEW_OUT_REG living above. We add clobber clause for
903 this. This is just a temporary clobber. We can remove
904 it at the end of LRA work. */
905 rtx_insn *clobber = emit_clobber (new_out_reg);
906 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
907 LRA_SUBREG_P (new_in_reg) = 1;
908 if (GET_CODE (in_rtx) == SUBREG)
910 rtx subreg_reg = SUBREG_REG (in_rtx);
912 /* If SUBREG_REG is dying here and sub-registers IN_RTX
913 and NEW_IN_REG are similar, we can use the same hard
914 register for REG and SUBREG_REG. */
915 if (REG_P (subreg_reg)
916 && (int) REGNO (subreg_reg) < lra_new_regno_start
917 && GET_MODE (subreg_reg) == outmode
918 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
919 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
920 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
924 else
926 /* Pseudos have values -- see comments for lra_reg_info.
927 Different pseudos with the same value do not conflict even if
928 they live in the same place. When we create a pseudo we
929 assign value of original pseudo (if any) from which we
930 created the new pseudo. If we create the pseudo from the
931 input pseudo, the new pseudo will no conflict with the input
932 pseudo which is wrong when the input pseudo lives after the
933 insn and as the new pseudo value is changed by the insn
934 output. Therefore we create the new pseudo from the output.
936 We cannot reuse the current output register because we might
937 have a situation like "a <- a op b", where the constraints
938 force the second input operand ("b") to match the output
939 operand ("a"). "b" must then be copied into a new register
940 so that it doesn't clobber the current value of "a". */
942 new_in_reg = new_out_reg
943 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
944 goal_class, "");
946 /* In operand can be got from transformations before processing insn
947 constraints. One example of such transformations is subreg
948 reloading (see function simplify_operand_subreg). The new
949 pseudos created by the transformations might have inaccurate
950 class (ALL_REGS) and we should make their classes more
951 accurate. */
952 narrow_reload_pseudo_class (in_rtx, goal_class);
953 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
954 *before = get_insns ();
955 end_sequence ();
956 for (i = 0; (in = ins[i]) >= 0; i++)
958 lra_assert
959 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
960 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
961 *curr_id->operand_loc[in] = new_in_reg;
963 lra_update_dups (curr_id, ins);
964 if (out < 0)
965 return;
966 /* See a comment for the input operand above. */
967 narrow_reload_pseudo_class (out_rtx, goal_class);
968 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
970 start_sequence ();
971 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
972 emit_insn (*after);
973 *after = get_insns ();
974 end_sequence ();
976 *curr_id->operand_loc[out] = new_out_reg;
977 lra_update_dup (curr_id, out);
980 /* Return register class which is union of all reg classes in insn
981 constraint alternative string starting with P. */
982 static enum reg_class
983 reg_class_from_constraints (const char *p)
985 int c, len;
986 enum reg_class op_class = NO_REGS;
989 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
991 case '#':
992 case ',':
993 return op_class;
995 case 'g':
996 op_class = reg_class_subunion[op_class][GENERAL_REGS];
997 break;
999 default:
1000 enum constraint_num cn = lookup_constraint (p);
1001 enum reg_class cl = reg_class_for_constraint (cn);
1002 if (cl == NO_REGS)
1004 if (insn_extra_address_constraint (cn))
1005 op_class
1006 = (reg_class_subunion
1007 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1008 ADDRESS, SCRATCH)]);
1009 break;
1012 op_class = reg_class_subunion[op_class][cl];
1013 break;
1015 while ((p += len), c);
1016 return op_class;
1019 /* If OP is a register, return the class of the register as per
1020 get_reg_class, otherwise return NO_REGS. */
1021 static inline enum reg_class
1022 get_op_class (rtx op)
1024 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1027 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1028 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1029 SUBREG for VAL to make them equal. */
1030 static rtx_insn *
1031 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1033 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1035 /* Usually size of mem_pseudo is greater than val size but in
1036 rare cases it can be less as it can be defined by target
1037 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1038 if (! MEM_P (val))
1040 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1041 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1043 LRA_SUBREG_P (val) = 1;
1045 else
1047 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1048 LRA_SUBREG_P (mem_pseudo) = 1;
1051 return to_p ? gen_move_insn (mem_pseudo, val)
1052 : gen_move_insn (val, mem_pseudo);
1055 /* Process a special case insn (register move), return true if we
1056 don't need to process it anymore. INSN should be a single set
1057 insn. Set up that RTL was changed through CHANGE_P and macro
1058 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1059 SEC_MEM_P. */
1060 static bool
1061 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1063 int sregno, dregno;
1064 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1065 rtx_insn *before;
1066 enum reg_class dclass, sclass, secondary_class;
1067 secondary_reload_info sri;
1069 lra_assert (curr_insn_set != NULL_RTX);
1070 dreg = dest = SET_DEST (curr_insn_set);
1071 sreg = src = SET_SRC (curr_insn_set);
1072 if (GET_CODE (dest) == SUBREG)
1073 dreg = SUBREG_REG (dest);
1074 if (GET_CODE (src) == SUBREG)
1075 sreg = SUBREG_REG (src);
1076 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1077 return false;
1078 sclass = dclass = NO_REGS;
1079 if (REG_P (dreg))
1080 dclass = get_reg_class (REGNO (dreg));
1081 if (dclass == ALL_REGS)
1082 /* ALL_REGS is used for new pseudos created by transformations
1083 like reload of SUBREG_REG (see function
1084 simplify_operand_subreg). We don't know their class yet. We
1085 should figure out the class from processing the insn
1086 constraints not in this fast path function. Even if ALL_REGS
1087 were a right class for the pseudo, secondary_... hooks usually
1088 are not define for ALL_REGS. */
1089 return false;
1090 if (REG_P (sreg))
1091 sclass = get_reg_class (REGNO (sreg));
1092 if (sclass == ALL_REGS)
1093 /* See comments above. */
1094 return false;
1095 if (sclass == NO_REGS && dclass == NO_REGS)
1096 return false;
1097 #ifdef SECONDARY_MEMORY_NEEDED
1098 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1099 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1100 && ((sclass != NO_REGS && dclass != NO_REGS)
1101 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1102 #endif
1105 *sec_mem_p = true;
1106 return false;
1108 #endif
1109 if (! REG_P (dreg) || ! REG_P (sreg))
1110 return false;
1111 sri.prev_sri = NULL;
1112 sri.icode = CODE_FOR_nothing;
1113 sri.extra_cost = 0;
1114 secondary_class = NO_REGS;
1115 /* Set up hard register for a reload pseudo for hook
1116 secondary_reload because some targets just ignore unassigned
1117 pseudos in the hook. */
1118 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1120 dregno = REGNO (dreg);
1121 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1123 else
1124 dregno = -1;
1125 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1127 sregno = REGNO (sreg);
1128 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1130 else
1131 sregno = -1;
1132 if (sclass != NO_REGS)
1133 secondary_class
1134 = (enum reg_class) targetm.secondary_reload (false, dest,
1135 (reg_class_t) sclass,
1136 GET_MODE (src), &sri);
1137 if (sclass == NO_REGS
1138 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1139 && dclass != NO_REGS))
1141 enum reg_class old_sclass = secondary_class;
1142 secondary_reload_info old_sri = sri;
1144 sri.prev_sri = NULL;
1145 sri.icode = CODE_FOR_nothing;
1146 sri.extra_cost = 0;
1147 secondary_class
1148 = (enum reg_class) targetm.secondary_reload (true, src,
1149 (reg_class_t) dclass,
1150 GET_MODE (src), &sri);
1151 /* Check the target hook consistency. */
1152 lra_assert
1153 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1154 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1155 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1157 if (sregno >= 0)
1158 reg_renumber [sregno] = -1;
1159 if (dregno >= 0)
1160 reg_renumber [dregno] = -1;
1161 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1162 return false;
1163 *change_p = true;
1164 new_reg = NULL_RTX;
1165 if (secondary_class != NO_REGS)
1166 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1167 secondary_class,
1168 "secondary");
1169 start_sequence ();
1170 if (sri.icode == CODE_FOR_nothing)
1171 lra_emit_move (new_reg, src);
1172 else
1174 enum reg_class scratch_class;
1176 scratch_class = (reg_class_from_constraints
1177 (insn_data[sri.icode].operand[2].constraint));
1178 scratch_reg = (lra_create_new_reg_with_unique_value
1179 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1180 scratch_class, "scratch"));
1181 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1182 src, scratch_reg));
1184 before = get_insns ();
1185 end_sequence ();
1186 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1187 if (new_reg != NULL_RTX)
1188 SET_SRC (curr_insn_set) = new_reg;
1189 else
1191 if (lra_dump_file != NULL)
1193 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1194 dump_insn_slim (lra_dump_file, curr_insn);
1196 lra_set_insn_deleted (curr_insn);
1197 return true;
1199 return false;
1202 /* The following data describe the result of process_alt_operands.
1203 The data are used in curr_insn_transform to generate reloads. */
1205 /* The chosen reg classes which should be used for the corresponding
1206 operands. */
1207 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1208 /* True if the operand should be the same as another operand and that
1209 other operand does not need a reload. */
1210 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1211 /* True if the operand does not need a reload. */
1212 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1213 /* True if the operand can be offsetable memory. */
1214 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1215 /* The number of an operand to which given operand can be matched to. */
1216 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1217 /* The number of elements in the following array. */
1218 static int goal_alt_dont_inherit_ops_num;
1219 /* Numbers of operands whose reload pseudos should not be inherited. */
1220 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1221 /* True if the insn commutative operands should be swapped. */
1222 static bool goal_alt_swapped;
1223 /* The chosen insn alternative. */
1224 static int goal_alt_number;
1226 /* The following five variables are used to choose the best insn
1227 alternative. They reflect final characteristics of the best
1228 alternative. */
1230 /* Number of necessary reloads and overall cost reflecting the
1231 previous value and other unpleasantness of the best alternative. */
1232 static int best_losers, best_overall;
1233 /* Overall number hard registers used for reloads. For example, on
1234 some targets we need 2 general registers to reload DFmode and only
1235 one floating point register. */
1236 static int best_reload_nregs;
1237 /* Overall number reflecting distances of previous reloading the same
1238 value. The distances are counted from the current BB start. It is
1239 used to improve inheritance chances. */
1240 static int best_reload_sum;
1242 /* True if the current insn should have no correspondingly input or
1243 output reloads. */
1244 static bool no_input_reloads_p, no_output_reloads_p;
1246 /* True if we swapped the commutative operands in the current
1247 insn. */
1248 static int curr_swapped;
1250 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1251 register of class CL. Add any input reloads to list BEFORE. AFTER
1252 is nonnull if *LOC is an automodified value; handle that case by
1253 adding the required output reloads to list AFTER. Return true if
1254 the RTL was changed.
1256 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1257 register. Return false if the address register is correct. */
1258 static bool
1259 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1260 enum reg_class cl)
1262 int regno;
1263 enum reg_class rclass, new_class;
1264 rtx reg;
1265 rtx new_reg;
1266 machine_mode mode;
1267 bool subreg_p, before_p = false;
1269 subreg_p = GET_CODE (*loc) == SUBREG;
1270 if (subreg_p)
1271 loc = &SUBREG_REG (*loc);
1272 reg = *loc;
1273 mode = GET_MODE (reg);
1274 if (! REG_P (reg))
1276 if (check_only_p)
1277 return true;
1278 /* Always reload memory in an address even if the target supports
1279 such addresses. */
1280 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1281 before_p = true;
1283 else
1285 regno = REGNO (reg);
1286 rclass = get_reg_class (regno);
1287 if (! check_only_p
1288 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1290 if (lra_dump_file != NULL)
1292 fprintf (lra_dump_file,
1293 "Changing pseudo %d in address of insn %u on equiv ",
1294 REGNO (reg), INSN_UID (curr_insn));
1295 dump_value_slim (lra_dump_file, *loc, 1);
1296 fprintf (lra_dump_file, "\n");
1298 *loc = copy_rtx (*loc);
1300 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1302 if (check_only_p)
1303 return true;
1304 reg = *loc;
1305 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1306 mode, reg, cl, subreg_p, "address", &new_reg))
1307 before_p = true;
1309 else if (new_class != NO_REGS && rclass != new_class)
1311 if (check_only_p)
1312 return true;
1313 lra_change_class (regno, new_class, " Change to", true);
1314 return false;
1316 else
1317 return false;
1319 if (before_p)
1321 push_to_sequence (*before);
1322 lra_emit_move (new_reg, reg);
1323 *before = get_insns ();
1324 end_sequence ();
1326 *loc = new_reg;
1327 if (after != NULL)
1329 start_sequence ();
1330 lra_emit_move (reg, new_reg);
1331 emit_insn (*after);
1332 *after = get_insns ();
1333 end_sequence ();
1335 return true;
1338 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1339 the insn to be inserted before curr insn. AFTER returns the
1340 the insn to be inserted after curr insn. ORIGREG and NEWREG
1341 are the original reg and new reg for reload. */
1342 static void
1343 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1344 rtx newreg)
1346 if (before)
1348 push_to_sequence (*before);
1349 lra_emit_move (newreg, origreg);
1350 *before = get_insns ();
1351 end_sequence ();
1353 if (after)
1355 start_sequence ();
1356 lra_emit_move (origreg, newreg);
1357 emit_insn (*after);
1358 *after = get_insns ();
1359 end_sequence ();
1363 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1365 /* Make reloads for subreg in operand NOP with internal subreg mode
1366 REG_MODE, add new reloads for further processing. Return true if
1367 any change was done. */
1368 static bool
1369 simplify_operand_subreg (int nop, machine_mode reg_mode)
1371 int hard_regno;
1372 rtx_insn *before, *after;
1373 machine_mode mode, innermode;
1374 rtx reg, new_reg;
1375 rtx operand = *curr_id->operand_loc[nop];
1376 enum reg_class regclass;
1377 enum op_type type;
1379 before = after = NULL;
1381 if (GET_CODE (operand) != SUBREG)
1382 return false;
1384 mode = GET_MODE (operand);
1385 reg = SUBREG_REG (operand);
1386 innermode = GET_MODE (reg);
1387 type = curr_static_id->operand[nop].type;
1388 /* If we change address for paradoxical subreg of memory, the
1389 address might violate the necessary alignment or the access might
1390 be slow. So take this into consideration. We should not worry
1391 about access beyond allocated memory for paradoxical memory
1392 subregs as we don't substitute such equiv memory (see processing
1393 equivalences in function lra_constraints) and because for spilled
1394 pseudos we allocate stack memory enough for the biggest
1395 corresponding paradoxical subreg. */
1396 if (MEM_P (reg)
1397 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1398 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1400 rtx subst, old = *curr_id->operand_loc[nop];
1402 alter_subreg (curr_id->operand_loc[nop], false);
1403 subst = *curr_id->operand_loc[nop];
1404 lra_assert (MEM_P (subst));
1405 if (! valid_address_p (innermode, XEXP (reg, 0),
1406 MEM_ADDR_SPACE (reg))
1407 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1408 MEM_ADDR_SPACE (subst)))
1409 return true;
1410 /* If the address was valid and became invalid, prefer to reload
1411 the memory. Typical case is when the index scale should
1412 correspond the memory. */
1413 *curr_id->operand_loc[nop] = old;
1415 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1417 alter_subreg (curr_id->operand_loc[nop], false);
1418 return true;
1420 else if (CONSTANT_P (reg))
1422 /* Try to simplify subreg of constant. It is usually result of
1423 equivalence substitution. */
1424 if (innermode == VOIDmode
1425 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1426 innermode = curr_static_id->operand[nop].mode;
1427 if ((new_reg = simplify_subreg (mode, reg, innermode,
1428 SUBREG_BYTE (operand))) != NULL_RTX)
1430 *curr_id->operand_loc[nop] = new_reg;
1431 return true;
1434 /* Put constant into memory when we have mixed modes. It generates
1435 a better code in most cases as it does not need a secondary
1436 reload memory. It also prevents LRA looping when LRA is using
1437 secondary reload memory again and again. */
1438 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1439 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1441 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1442 alter_subreg (curr_id->operand_loc[nop], false);
1443 return true;
1445 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1446 if there may be a problem accessing OPERAND in the outer
1447 mode. */
1448 if ((REG_P (reg)
1449 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1450 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1451 /* Don't reload paradoxical subregs because we could be looping
1452 having repeatedly final regno out of hard regs range. */
1453 && (hard_regno_nregs[hard_regno][innermode]
1454 >= hard_regno_nregs[hard_regno][mode])
1455 && simplify_subreg_regno (hard_regno, innermode,
1456 SUBREG_BYTE (operand), mode) < 0
1457 /* Don't reload subreg for matching reload. It is actually
1458 valid subreg in LRA. */
1459 && ! LRA_SUBREG_P (operand))
1460 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1462 enum reg_class rclass;
1464 if (REG_P (reg))
1465 /* There is a big probability that we will get the same class
1466 for the new pseudo and we will get the same insn which
1467 means infinite looping. So spill the new pseudo. */
1468 rclass = NO_REGS;
1469 else
1470 /* The class will be defined later in curr_insn_transform. */
1471 rclass
1472 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1474 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1475 rclass, TRUE, "subreg reg", &new_reg))
1477 bool insert_before, insert_after;
1478 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1480 insert_before = (type != OP_OUT
1481 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1482 insert_after = (type != OP_IN);
1483 insert_move_for_subreg (insert_before ? &before : NULL,
1484 insert_after ? &after : NULL,
1485 reg, new_reg);
1487 SUBREG_REG (operand) = new_reg;
1488 lra_process_new_insns (curr_insn, before, after,
1489 "Inserting subreg reload");
1490 return true;
1492 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1493 IRA allocates hardreg to the inner pseudo reg according to its mode
1494 instead of the outermode, so the size of the hardreg may not be enough
1495 to contain the outermode operand, in that case we may need to insert
1496 reload for the reg. For the following two types of paradoxical subreg,
1497 we need to insert reload:
1498 1. If the op_type is OP_IN, and the hardreg could not be paired with
1499 other hardreg to contain the outermode operand
1500 (checked by in_hard_reg_set_p), we need to insert the reload.
1501 2. If the op_type is OP_OUT or OP_INOUT.
1503 Here is a paradoxical subreg example showing how the reload is generated:
1505 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1506 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1508 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1509 here, if reg107 is assigned to hardreg R15, because R15 is the last
1510 hardreg, compiler cannot find another hardreg to pair with R15 to
1511 contain TImode data. So we insert a TImode reload reg180 for it.
1512 After reload is inserted:
1514 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1515 (reg:DI 107 [ __comp ])) -1
1516 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1517 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1519 Two reload hard registers will be allocated to reg180 to save TImode data
1520 in LRA_assign. */
1521 else if (REG_P (reg)
1522 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1523 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1524 && (hard_regno_nregs[hard_regno][innermode]
1525 < hard_regno_nregs[hard_regno][mode])
1526 && (regclass = lra_get_allocno_class (REGNO (reg)))
1527 && (type != OP_IN
1528 || !in_hard_reg_set_p (reg_class_contents[regclass],
1529 mode, hard_regno)))
1531 /* The class will be defined later in curr_insn_transform. */
1532 enum reg_class rclass
1533 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1535 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1536 rclass, TRUE, "paradoxical subreg", &new_reg))
1538 rtx subreg;
1539 bool insert_before, insert_after;
1541 PUT_MODE (new_reg, mode);
1542 subreg = simplify_gen_subreg (innermode, new_reg, mode, 0);
1543 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1545 insert_before = (type != OP_OUT);
1546 insert_after = (type != OP_IN);
1547 insert_move_for_subreg (insert_before ? &before : NULL,
1548 insert_after ? &after : NULL,
1549 reg, subreg);
1551 SUBREG_REG (operand) = new_reg;
1552 lra_process_new_insns (curr_insn, before, after,
1553 "Inserting paradoxical subreg reload");
1554 return true;
1556 return false;
1559 /* Return TRUE if X refers for a hard register from SET. */
1560 static bool
1561 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1563 int i, j, x_hard_regno;
1564 machine_mode mode;
1565 const char *fmt;
1566 enum rtx_code code;
1568 if (x == NULL_RTX)
1569 return false;
1570 code = GET_CODE (x);
1571 mode = GET_MODE (x);
1572 if (code == SUBREG)
1574 x = SUBREG_REG (x);
1575 code = GET_CODE (x);
1576 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1577 mode = GET_MODE (x);
1580 if (REG_P (x))
1582 x_hard_regno = get_hard_regno (x);
1583 return (x_hard_regno >= 0
1584 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1586 if (MEM_P (x))
1588 struct address_info ad;
1590 decompose_mem_address (&ad, x);
1591 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1592 return true;
1593 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1594 return true;
1596 fmt = GET_RTX_FORMAT (code);
1597 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1599 if (fmt[i] == 'e')
1601 if (uses_hard_regs_p (XEXP (x, i), set))
1602 return true;
1604 else if (fmt[i] == 'E')
1606 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1607 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1608 return true;
1611 return false;
1614 /* Return true if OP is a spilled pseudo. */
1615 static inline bool
1616 spilled_pseudo_p (rtx op)
1618 return (REG_P (op)
1619 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1622 /* Return true if X is a general constant. */
1623 static inline bool
1624 general_constant_p (rtx x)
1626 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1629 static bool
1630 reg_in_class_p (rtx reg, enum reg_class cl)
1632 if (cl == NO_REGS)
1633 return get_reg_class (REGNO (reg)) == NO_REGS;
1634 return in_class_p (reg, cl, NULL);
1637 /* Return true if SET of RCLASS contains no hard regs which can be
1638 used in MODE. */
1639 static bool
1640 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1641 HARD_REG_SET &set,
1642 enum machine_mode mode)
1644 HARD_REG_SET temp;
1646 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1647 COPY_HARD_REG_SET (temp, set);
1648 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1649 return (hard_reg_set_subset_p
1650 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1653 /* Major function to choose the current insn alternative and what
1654 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1655 negative we should consider only this alternative. Return false if
1656 we can not choose the alternative or find how to reload the
1657 operands. */
1658 static bool
1659 process_alt_operands (int only_alternative)
1661 bool ok_p = false;
1662 int nop, overall, nalt;
1663 int n_alternatives = curr_static_id->n_alternatives;
1664 int n_operands = curr_static_id->n_operands;
1665 /* LOSERS counts the operands that don't fit this alternative and
1666 would require loading. */
1667 int losers;
1668 /* REJECT is a count of how undesirable this alternative says it is
1669 if any reloading is required. If the alternative matches exactly
1670 then REJECT is ignored, but otherwise it gets this much counted
1671 against it in addition to the reloading needed. */
1672 int reject;
1673 int op_reject;
1674 /* The number of elements in the following array. */
1675 int early_clobbered_regs_num;
1676 /* Numbers of operands which are early clobber registers. */
1677 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1678 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1679 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1680 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1681 bool curr_alt_win[MAX_RECOG_OPERANDS];
1682 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1683 int curr_alt_matches[MAX_RECOG_OPERANDS];
1684 /* The number of elements in the following array. */
1685 int curr_alt_dont_inherit_ops_num;
1686 /* Numbers of operands whose reload pseudos should not be inherited. */
1687 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1688 rtx op;
1689 /* The register when the operand is a subreg of register, otherwise the
1690 operand itself. */
1691 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1692 /* The register if the operand is a register or subreg of register,
1693 otherwise NULL. */
1694 rtx operand_reg[MAX_RECOG_OPERANDS];
1695 int hard_regno[MAX_RECOG_OPERANDS];
1696 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1697 int reload_nregs, reload_sum;
1698 bool costly_p;
1699 enum reg_class cl;
1701 /* Calculate some data common for all alternatives to speed up the
1702 function. */
1703 for (nop = 0; nop < n_operands; nop++)
1705 rtx reg;
1707 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1708 /* The real hard regno of the operand after the allocation. */
1709 hard_regno[nop] = get_hard_regno (op);
1711 operand_reg[nop] = reg = op;
1712 biggest_mode[nop] = GET_MODE (op);
1713 if (GET_CODE (op) == SUBREG)
1715 operand_reg[nop] = reg = SUBREG_REG (op);
1716 if (GET_MODE_SIZE (biggest_mode[nop])
1717 < GET_MODE_SIZE (GET_MODE (reg)))
1718 biggest_mode[nop] = GET_MODE (reg);
1720 if (! REG_P (reg))
1721 operand_reg[nop] = NULL_RTX;
1722 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1723 || ((int) REGNO (reg)
1724 == lra_get_elimination_hard_regno (REGNO (reg))))
1725 no_subreg_reg_operand[nop] = reg;
1726 else
1727 operand_reg[nop] = no_subreg_reg_operand[nop]
1728 /* Just use natural mode for elimination result. It should
1729 be enough for extra constraints hooks. */
1730 = regno_reg_rtx[hard_regno[nop]];
1733 /* The constraints are made of several alternatives. Each operand's
1734 constraint looks like foo,bar,... with commas separating the
1735 alternatives. The first alternatives for all operands go
1736 together, the second alternatives go together, etc.
1738 First loop over alternatives. */
1739 alternative_mask preferred = curr_id->preferred_alternatives;
1740 if (only_alternative >= 0)
1741 preferred &= ALTERNATIVE_BIT (only_alternative);
1743 for (nalt = 0; nalt < n_alternatives; nalt++)
1745 /* Loop over operands for one constraint alternative. */
1746 if (!TEST_BIT (preferred, nalt))
1747 continue;
1749 overall = losers = reject = reload_nregs = reload_sum = 0;
1750 for (nop = 0; nop < n_operands; nop++)
1752 int inc = (curr_static_id
1753 ->operand_alternative[nalt * n_operands + nop].reject);
1754 if (lra_dump_file != NULL && inc != 0)
1755 fprintf (lra_dump_file,
1756 " Staticly defined alt reject+=%d\n", inc);
1757 reject += inc;
1759 early_clobbered_regs_num = 0;
1761 for (nop = 0; nop < n_operands; nop++)
1763 const char *p;
1764 char *end;
1765 int len, c, m, i, opalt_num, this_alternative_matches;
1766 bool win, did_match, offmemok, early_clobber_p;
1767 /* false => this operand can be reloaded somehow for this
1768 alternative. */
1769 bool badop;
1770 /* true => this operand can be reloaded if the alternative
1771 allows regs. */
1772 bool winreg;
1773 /* True if a constant forced into memory would be OK for
1774 this operand. */
1775 bool constmemok;
1776 enum reg_class this_alternative, this_costly_alternative;
1777 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1778 bool this_alternative_match_win, this_alternative_win;
1779 bool this_alternative_offmemok;
1780 bool scratch_p;
1781 machine_mode mode;
1782 enum constraint_num cn;
1784 opalt_num = nalt * n_operands + nop;
1785 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1787 /* Fast track for no constraints at all. */
1788 curr_alt[nop] = NO_REGS;
1789 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1790 curr_alt_win[nop] = true;
1791 curr_alt_match_win[nop] = false;
1792 curr_alt_offmemok[nop] = false;
1793 curr_alt_matches[nop] = -1;
1794 continue;
1797 op = no_subreg_reg_operand[nop];
1798 mode = curr_operand_mode[nop];
1800 win = did_match = winreg = offmemok = constmemok = false;
1801 badop = true;
1803 early_clobber_p = false;
1804 p = curr_static_id->operand_alternative[opalt_num].constraint;
1806 this_costly_alternative = this_alternative = NO_REGS;
1807 /* We update set of possible hard regs besides its class
1808 because reg class might be inaccurate. For example,
1809 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1810 is translated in HI_REGS because classes are merged by
1811 pairs and there is no accurate intermediate class. */
1812 CLEAR_HARD_REG_SET (this_alternative_set);
1813 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1814 this_alternative_win = false;
1815 this_alternative_match_win = false;
1816 this_alternative_offmemok = false;
1817 this_alternative_matches = -1;
1819 /* An empty constraint should be excluded by the fast
1820 track. */
1821 lra_assert (*p != 0 && *p != ',');
1823 op_reject = 0;
1824 /* Scan this alternative's specs for this operand; set WIN
1825 if the operand fits any letter in this alternative.
1826 Otherwise, clear BADOP if this operand could fit some
1827 letter after reloads, or set WINREG if this operand could
1828 fit after reloads provided the constraint allows some
1829 registers. */
1830 costly_p = false;
1833 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1835 case '\0':
1836 len = 0;
1837 break;
1838 case ',':
1839 c = '\0';
1840 break;
1842 case '&':
1843 early_clobber_p = true;
1844 break;
1846 case '$':
1847 op_reject += LRA_MAX_REJECT;
1848 break;
1849 case '^':
1850 op_reject += LRA_LOSER_COST_FACTOR;
1851 break;
1853 case '#':
1854 /* Ignore rest of this alternative. */
1855 c = '\0';
1856 break;
1858 case '0': case '1': case '2': case '3': case '4':
1859 case '5': case '6': case '7': case '8': case '9':
1861 int m_hregno;
1862 bool match_p;
1864 m = strtoul (p, &end, 10);
1865 p = end;
1866 len = 0;
1867 lra_assert (nop > m);
1869 this_alternative_matches = m;
1870 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1871 /* We are supposed to match a previous operand.
1872 If we do, we win if that one did. If we do
1873 not, count both of the operands as losers.
1874 (This is too conservative, since most of the
1875 time only a single reload insn will be needed
1876 to make the two operands win. As a result,
1877 this alternative may be rejected when it is
1878 actually desirable.) */
1879 match_p = false;
1880 if (operands_match_p (*curr_id->operand_loc[nop],
1881 *curr_id->operand_loc[m], m_hregno))
1883 /* We should reject matching of an early
1884 clobber operand if the matching operand is
1885 not dying in the insn. */
1886 if (! curr_static_id->operand[m].early_clobber
1887 || operand_reg[nop] == NULL_RTX
1888 || (find_regno_note (curr_insn, REG_DEAD,
1889 REGNO (op))
1890 || REGNO (op) == REGNO (operand_reg[m])))
1891 match_p = true;
1893 if (match_p)
1895 /* If we are matching a non-offsettable
1896 address where an offsettable address was
1897 expected, then we must reject this
1898 combination, because we can't reload
1899 it. */
1900 if (curr_alt_offmemok[m]
1901 && MEM_P (*curr_id->operand_loc[m])
1902 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1903 continue;
1905 else
1907 /* Operands don't match. Both operands must
1908 allow a reload register, otherwise we
1909 cannot make them match. */
1910 if (curr_alt[m] == NO_REGS)
1911 break;
1912 /* Retroactively mark the operand we had to
1913 match as a loser, if it wasn't already and
1914 it wasn't matched to a register constraint
1915 (e.g it might be matched by memory). */
1916 if (curr_alt_win[m]
1917 && (operand_reg[m] == NULL_RTX
1918 || hard_regno[m] < 0))
1920 losers++;
1921 reload_nregs
1922 += (ira_reg_class_max_nregs[curr_alt[m]]
1923 [GET_MODE (*curr_id->operand_loc[m])]);
1926 /* Prefer matching earlyclobber alternative as
1927 it results in less hard regs required for
1928 the insn than a non-matching earlyclobber
1929 alternative. */
1930 if (curr_static_id->operand[m].early_clobber)
1932 if (lra_dump_file != NULL)
1933 fprintf
1934 (lra_dump_file,
1935 " %d Matching earlyclobber alt:"
1936 " reject--\n",
1937 nop);
1938 reject--;
1940 /* Otherwise we prefer no matching
1941 alternatives because it gives more freedom
1942 in RA. */
1943 else if (operand_reg[nop] == NULL_RTX
1944 || (find_regno_note (curr_insn, REG_DEAD,
1945 REGNO (operand_reg[nop]))
1946 == NULL_RTX))
1948 if (lra_dump_file != NULL)
1949 fprintf
1950 (lra_dump_file,
1951 " %d Matching alt: reject+=2\n",
1952 nop);
1953 reject += 2;
1956 /* If we have to reload this operand and some
1957 previous operand also had to match the same
1958 thing as this operand, we don't know how to do
1959 that. */
1960 if (!match_p || !curr_alt_win[m])
1962 for (i = 0; i < nop; i++)
1963 if (curr_alt_matches[i] == m)
1964 break;
1965 if (i < nop)
1966 break;
1968 else
1969 did_match = true;
1971 /* This can be fixed with reloads if the operand
1972 we are supposed to match can be fixed with
1973 reloads. */
1974 badop = false;
1975 this_alternative = curr_alt[m];
1976 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1977 winreg = this_alternative != NO_REGS;
1978 break;
1981 case 'g':
1982 if (MEM_P (op)
1983 || general_constant_p (op)
1984 || spilled_pseudo_p (op))
1985 win = true;
1986 cl = GENERAL_REGS;
1987 goto reg;
1989 default:
1990 cn = lookup_constraint (p);
1991 switch (get_constraint_type (cn))
1993 case CT_REGISTER:
1994 cl = reg_class_for_constraint (cn);
1995 if (cl != NO_REGS)
1996 goto reg;
1997 break;
1999 case CT_CONST_INT:
2000 if (CONST_INT_P (op)
2001 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2002 win = true;
2003 break;
2005 case CT_MEMORY:
2006 if (MEM_P (op)
2007 && satisfies_memory_constraint_p (op, cn))
2008 win = true;
2009 else if (spilled_pseudo_p (op))
2010 win = true;
2012 /* If we didn't already win, we can reload constants
2013 via force_const_mem or put the pseudo value into
2014 memory, or make other memory by reloading the
2015 address like for 'o'. */
2016 if (CONST_POOL_OK_P (mode, op)
2017 || MEM_P (op) || REG_P (op))
2018 badop = false;
2019 constmemok = true;
2020 offmemok = true;
2021 break;
2023 case CT_ADDRESS:
2024 /* If we didn't already win, we can reload the address
2025 into a base register. */
2026 if (satisfies_address_constraint_p (op, cn))
2027 win = true;
2028 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2029 ADDRESS, SCRATCH);
2030 badop = false;
2031 goto reg;
2033 case CT_FIXED_FORM:
2034 if (constraint_satisfied_p (op, cn))
2035 win = true;
2036 break;
2038 break;
2040 reg:
2041 this_alternative = reg_class_subunion[this_alternative][cl];
2042 IOR_HARD_REG_SET (this_alternative_set,
2043 reg_class_contents[cl]);
2044 if (costly_p)
2046 this_costly_alternative
2047 = reg_class_subunion[this_costly_alternative][cl];
2048 IOR_HARD_REG_SET (this_costly_alternative_set,
2049 reg_class_contents[cl]);
2051 if (mode == BLKmode)
2052 break;
2053 winreg = true;
2054 if (REG_P (op))
2056 if (hard_regno[nop] >= 0
2057 && in_hard_reg_set_p (this_alternative_set,
2058 mode, hard_regno[nop]))
2059 win = true;
2060 else if (hard_regno[nop] < 0
2061 && in_class_p (op, this_alternative, NULL))
2062 win = true;
2064 break;
2066 if (c != ' ' && c != '\t')
2067 costly_p = c == '*';
2069 while ((p += len), c);
2071 scratch_p = (operand_reg[nop] != NULL_RTX
2072 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2073 /* Record which operands fit this alternative. */
2074 if (win)
2076 this_alternative_win = true;
2077 if (operand_reg[nop] != NULL_RTX)
2079 if (hard_regno[nop] >= 0)
2081 if (in_hard_reg_set_p (this_costly_alternative_set,
2082 mode, hard_regno[nop]))
2084 if (lra_dump_file != NULL)
2085 fprintf (lra_dump_file,
2086 " %d Costly set: reject++\n",
2087 nop);
2088 reject++;
2091 else
2093 /* Prefer won reg to spilled pseudo under other
2094 equal conditions for possibe inheritance. */
2095 if (! scratch_p)
2097 if (lra_dump_file != NULL)
2098 fprintf
2099 (lra_dump_file,
2100 " %d Non pseudo reload: reject++\n",
2101 nop);
2102 reject++;
2104 if (in_class_p (operand_reg[nop],
2105 this_costly_alternative, NULL))
2107 if (lra_dump_file != NULL)
2108 fprintf
2109 (lra_dump_file,
2110 " %d Non pseudo costly reload:"
2111 " reject++\n",
2112 nop);
2113 reject++;
2116 /* We simulate the behaviour of old reload here.
2117 Although scratches need hard registers and it
2118 might result in spilling other pseudos, no reload
2119 insns are generated for the scratches. So it
2120 might cost something but probably less than old
2121 reload pass believes. */
2122 if (scratch_p)
2124 if (lra_dump_file != NULL)
2125 fprintf (lra_dump_file,
2126 " %d Scratch win: reject+=2\n",
2127 nop);
2128 reject += 2;
2132 else if (did_match)
2133 this_alternative_match_win = true;
2134 else
2136 int const_to_mem = 0;
2137 bool no_regs_p;
2139 reject += op_reject;
2140 /* Never do output reload of stack pointer. It makes
2141 impossible to do elimination when SP is changed in
2142 RTL. */
2143 if (op == stack_pointer_rtx && ! frame_pointer_needed
2144 && curr_static_id->operand[nop].type != OP_IN)
2145 goto fail;
2147 /* If this alternative asks for a specific reg class, see if there
2148 is at least one allocatable register in that class. */
2149 no_regs_p
2150 = (this_alternative == NO_REGS
2151 || (hard_reg_set_subset_p
2152 (reg_class_contents[this_alternative],
2153 lra_no_alloc_regs)));
2155 /* For asms, verify that the class for this alternative is possible
2156 for the mode that is specified. */
2157 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2159 int i;
2160 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2161 if (HARD_REGNO_MODE_OK (i, mode)
2162 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2163 mode, i))
2164 break;
2165 if (i == FIRST_PSEUDO_REGISTER)
2166 winreg = false;
2169 /* If this operand accepts a register, and if the
2170 register class has at least one allocatable register,
2171 then this operand can be reloaded. */
2172 if (winreg && !no_regs_p)
2173 badop = false;
2175 if (badop)
2177 if (lra_dump_file != NULL)
2178 fprintf (lra_dump_file,
2179 " alt=%d: Bad operand -- refuse\n",
2180 nalt);
2181 goto fail;
2184 /* If not assigned pseudo has a class which a subset of
2185 required reg class, it is a less costly alternative
2186 as the pseudo still can get a hard reg of necessary
2187 class. */
2188 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2189 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2190 && ira_class_subset_p[this_alternative][cl])
2192 if (lra_dump_file != NULL)
2193 fprintf
2194 (lra_dump_file,
2195 " %d Super set class reg: reject-=3\n", nop);
2196 reject -= 3;
2199 this_alternative_offmemok = offmemok;
2200 if (this_costly_alternative != NO_REGS)
2202 if (lra_dump_file != NULL)
2203 fprintf (lra_dump_file,
2204 " %d Costly loser: reject++\n", nop);
2205 reject++;
2207 /* If the operand is dying, has a matching constraint,
2208 and satisfies constraints of the matched operand
2209 which failed to satisfy the own constraints, most probably
2210 the reload for this operand will be gone. */
2211 if (this_alternative_matches >= 0
2212 && !curr_alt_win[this_alternative_matches]
2213 && REG_P (op)
2214 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2215 && (hard_regno[nop] >= 0
2216 ? in_hard_reg_set_p (this_alternative_set,
2217 mode, hard_regno[nop])
2218 : in_class_p (op, this_alternative, NULL)))
2220 if (lra_dump_file != NULL)
2221 fprintf
2222 (lra_dump_file,
2223 " %d Dying matched operand reload: reject++\n",
2224 nop);
2225 reject++;
2227 else
2229 /* Strict_low_part requires to reload the register
2230 not the sub-register. In this case we should
2231 check that a final reload hard reg can hold the
2232 value mode. */
2233 if (curr_static_id->operand[nop].strict_low
2234 && REG_P (op)
2235 && hard_regno[nop] < 0
2236 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2237 && ira_class_hard_regs_num[this_alternative] > 0
2238 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2239 [this_alternative][0],
2240 GET_MODE
2241 (*curr_id->operand_loc[nop])))
2243 if (lra_dump_file != NULL)
2244 fprintf
2245 (lra_dump_file,
2246 " alt=%d: Strict low subreg reload -- refuse\n",
2247 nalt);
2248 goto fail;
2250 losers++;
2252 if (operand_reg[nop] != NULL_RTX
2253 /* Output operands and matched input operands are
2254 not inherited. The following conditions do not
2255 exactly describe the previous statement but they
2256 are pretty close. */
2257 && curr_static_id->operand[nop].type != OP_OUT
2258 && (this_alternative_matches < 0
2259 || curr_static_id->operand[nop].type != OP_IN))
2261 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2262 (operand_reg[nop])]
2263 .last_reload);
2265 /* The value of reload_sum has sense only if we
2266 process insns in their order. It happens only on
2267 the first constraints sub-pass when we do most of
2268 reload work. */
2269 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2270 reload_sum += last_reload - bb_reload_num;
2272 /* If this is a constant that is reloaded into the
2273 desired class by copying it to memory first, count
2274 that as another reload. This is consistent with
2275 other code and is required to avoid choosing another
2276 alternative when the constant is moved into memory.
2277 Note that the test here is precisely the same as in
2278 the code below that calls force_const_mem. */
2279 if (CONST_POOL_OK_P (mode, op)
2280 && ((targetm.preferred_reload_class
2281 (op, this_alternative) == NO_REGS)
2282 || no_input_reloads_p))
2284 const_to_mem = 1;
2285 if (! no_regs_p)
2286 losers++;
2289 /* Alternative loses if it requires a type of reload not
2290 permitted for this insn. We can always reload
2291 objects with a REG_UNUSED note. */
2292 if ((curr_static_id->operand[nop].type != OP_IN
2293 && no_output_reloads_p
2294 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2295 || (curr_static_id->operand[nop].type != OP_OUT
2296 && no_input_reloads_p && ! const_to_mem)
2297 || (this_alternative_matches >= 0
2298 && (no_input_reloads_p
2299 || (no_output_reloads_p
2300 && (curr_static_id->operand
2301 [this_alternative_matches].type != OP_IN)
2302 && ! find_reg_note (curr_insn, REG_UNUSED,
2303 no_subreg_reg_operand
2304 [this_alternative_matches])))))
2306 if (lra_dump_file != NULL)
2307 fprintf
2308 (lra_dump_file,
2309 " alt=%d: No input/otput reload -- refuse\n",
2310 nalt);
2311 goto fail;
2314 /* Alternative loses if it required class pseudo can not
2315 hold value of required mode. Such insns can be
2316 described by insn definitions with mode iterators. */
2317 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2318 && ! hard_reg_set_empty_p (this_alternative_set)
2319 /* It is common practice for constraints to use a
2320 class which does not have actually enough regs to
2321 hold the value (e.g. x86 AREG for mode requiring
2322 more one general reg). Therefore we have 2
2323 conditions to check that the reload pseudo can
2324 not hold the mode value. */
2325 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2326 [this_alternative][0],
2327 GET_MODE (*curr_id->operand_loc[nop]))
2328 /* The above condition is not enough as the first
2329 reg in ira_class_hard_regs can be not aligned for
2330 multi-words mode values. */
2331 && (prohibited_class_reg_set_mode_p
2332 (this_alternative, this_alternative_set,
2333 GET_MODE (*curr_id->operand_loc[nop]))))
2335 if (lra_dump_file != NULL)
2336 fprintf (lra_dump_file,
2337 " alt=%d: reload pseudo for op %d "
2338 " can not hold the mode value -- refuse\n",
2339 nalt, nop);
2340 goto fail;
2343 /* Check strong discouragement of reload of non-constant
2344 into class THIS_ALTERNATIVE. */
2345 if (! CONSTANT_P (op) && ! no_regs_p
2346 && (targetm.preferred_reload_class
2347 (op, this_alternative) == NO_REGS
2348 || (curr_static_id->operand[nop].type == OP_OUT
2349 && (targetm.preferred_output_reload_class
2350 (op, this_alternative) == NO_REGS))))
2352 if (lra_dump_file != NULL)
2353 fprintf (lra_dump_file,
2354 " %d Non-prefered reload: reject+=%d\n",
2355 nop, LRA_MAX_REJECT);
2356 reject += LRA_MAX_REJECT;
2359 if (! (MEM_P (op) && offmemok)
2360 && ! (const_to_mem && constmemok))
2362 /* We prefer to reload pseudos over reloading other
2363 things, since such reloads may be able to be
2364 eliminated later. So bump REJECT in other cases.
2365 Don't do this in the case where we are forcing a
2366 constant into memory and it will then win since
2367 we don't want to have a different alternative
2368 match then. */
2369 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2371 if (lra_dump_file != NULL)
2372 fprintf
2373 (lra_dump_file,
2374 " %d Non-pseudo reload: reject+=2\n",
2375 nop);
2376 reject += 2;
2379 if (! no_regs_p)
2380 reload_nregs
2381 += ira_reg_class_max_nregs[this_alternative][mode];
2383 if (SMALL_REGISTER_CLASS_P (this_alternative))
2385 if (lra_dump_file != NULL)
2386 fprintf
2387 (lra_dump_file,
2388 " %d Small class reload: reject+=%d\n",
2389 nop, LRA_LOSER_COST_FACTOR / 2);
2390 reject += LRA_LOSER_COST_FACTOR / 2;
2394 /* We are trying to spill pseudo into memory. It is
2395 usually more costly than moving to a hard register
2396 although it might takes the same number of
2397 reloads. */
2398 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2400 if (lra_dump_file != NULL)
2401 fprintf
2402 (lra_dump_file,
2403 " %d Spill pseudo into memory: reject+=3\n",
2404 nop);
2405 reject += 3;
2406 if (VECTOR_MODE_P (mode))
2408 /* Spilling vectors into memory is usually more
2409 costly as they contain big values. */
2410 if (lra_dump_file != NULL)
2411 fprintf
2412 (lra_dump_file,
2413 " %d Spill vector pseudo: reject+=2\n",
2414 nop);
2415 reject += 2;
2419 #ifdef SECONDARY_MEMORY_NEEDED
2420 /* If reload requires moving value through secondary
2421 memory, it will need one more insn at least. */
2422 if (this_alternative != NO_REGS
2423 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2424 && ((curr_static_id->operand[nop].type != OP_OUT
2425 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2426 GET_MODE (op)))
2427 || (curr_static_id->operand[nop].type != OP_IN
2428 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2429 GET_MODE (op)))))
2430 losers++;
2431 #endif
2432 /* Input reloads can be inherited more often than output
2433 reloads can be removed, so penalize output
2434 reloads. */
2435 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2437 if (lra_dump_file != NULL)
2438 fprintf
2439 (lra_dump_file,
2440 " %d Non input pseudo reload: reject++\n",
2441 nop);
2442 reject++;
2446 if (early_clobber_p && ! scratch_p)
2448 if (lra_dump_file != NULL)
2449 fprintf (lra_dump_file,
2450 " %d Early clobber: reject++\n", nop);
2451 reject++;
2453 /* ??? We check early clobbers after processing all operands
2454 (see loop below) and there we update the costs more.
2455 Should we update the cost (may be approximately) here
2456 because of early clobber register reloads or it is a rare
2457 or non-important thing to be worth to do it. */
2458 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2459 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2461 if (lra_dump_file != NULL)
2462 fprintf (lra_dump_file,
2463 " alt=%d,overall=%d,losers=%d -- refuse\n",
2464 nalt, overall, losers);
2465 goto fail;
2468 curr_alt[nop] = this_alternative;
2469 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2470 curr_alt_win[nop] = this_alternative_win;
2471 curr_alt_match_win[nop] = this_alternative_match_win;
2472 curr_alt_offmemok[nop] = this_alternative_offmemok;
2473 curr_alt_matches[nop] = this_alternative_matches;
2475 if (this_alternative_matches >= 0
2476 && !did_match && !this_alternative_win)
2477 curr_alt_win[this_alternative_matches] = false;
2479 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2480 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2482 if (curr_insn_set != NULL_RTX && n_operands == 2
2483 /* Prevent processing non-move insns. */
2484 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2485 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2486 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2487 && REG_P (no_subreg_reg_operand[0])
2488 && REG_P (no_subreg_reg_operand[1])
2489 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2490 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2491 || (! curr_alt_win[0] && curr_alt_win[1]
2492 && REG_P (no_subreg_reg_operand[1])
2493 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2494 || (curr_alt_win[0] && ! curr_alt_win[1]
2495 && REG_P (no_subreg_reg_operand[0])
2496 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2497 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2498 no_subreg_reg_operand[1])
2499 || (targetm.preferred_reload_class
2500 (no_subreg_reg_operand[1],
2501 (enum reg_class) curr_alt[1]) != NO_REGS))
2502 /* If it is a result of recent elimination in move
2503 insn we can transform it into an add still by
2504 using this alternative. */
2505 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2507 /* We have a move insn and a new reload insn will be similar
2508 to the current insn. We should avoid such situation as it
2509 results in LRA cycling. */
2510 overall += LRA_MAX_REJECT;
2512 ok_p = true;
2513 curr_alt_dont_inherit_ops_num = 0;
2514 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2516 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2517 HARD_REG_SET temp_set;
2519 i = early_clobbered_nops[nop];
2520 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2521 || hard_regno[i] < 0)
2522 continue;
2523 lra_assert (operand_reg[i] != NULL_RTX);
2524 clobbered_hard_regno = hard_regno[i];
2525 CLEAR_HARD_REG_SET (temp_set);
2526 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2527 first_conflict_j = last_conflict_j = -1;
2528 for (j = 0; j < n_operands; j++)
2529 if (j == i
2530 /* We don't want process insides of match_operator and
2531 match_parallel because otherwise we would process
2532 their operands once again generating a wrong
2533 code. */
2534 || curr_static_id->operand[j].is_operator)
2535 continue;
2536 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2537 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2538 continue;
2539 /* If we don't reload j-th operand, check conflicts. */
2540 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2541 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2543 if (first_conflict_j < 0)
2544 first_conflict_j = j;
2545 last_conflict_j = j;
2547 if (last_conflict_j < 0)
2548 continue;
2549 /* If earlyclobber operand conflicts with another
2550 non-matching operand which is actually the same register
2551 as the earlyclobber operand, it is better to reload the
2552 another operand as an operand matching the earlyclobber
2553 operand can be also the same. */
2554 if (first_conflict_j == last_conflict_j
2555 && operand_reg[last_conflict_j]
2556 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2557 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2559 curr_alt_win[last_conflict_j] = false;
2560 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2561 = last_conflict_j;
2562 losers++;
2563 /* Early clobber was already reflected in REJECT. */
2564 lra_assert (reject > 0);
2565 if (lra_dump_file != NULL)
2566 fprintf
2567 (lra_dump_file,
2568 " %d Conflict early clobber reload: reject--\n",
2570 reject--;
2571 overall += LRA_LOSER_COST_FACTOR - 1;
2573 else
2575 /* We need to reload early clobbered register and the
2576 matched registers. */
2577 for (j = 0; j < n_operands; j++)
2578 if (curr_alt_matches[j] == i)
2580 curr_alt_match_win[j] = false;
2581 losers++;
2582 overall += LRA_LOSER_COST_FACTOR;
2584 if (! curr_alt_match_win[i])
2585 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2586 else
2588 /* Remember pseudos used for match reloads are never
2589 inherited. */
2590 lra_assert (curr_alt_matches[i] >= 0);
2591 curr_alt_win[curr_alt_matches[i]] = false;
2593 curr_alt_win[i] = curr_alt_match_win[i] = false;
2594 losers++;
2595 /* Early clobber was already reflected in REJECT. */
2596 lra_assert (reject > 0);
2597 if (lra_dump_file != NULL)
2598 fprintf
2599 (lra_dump_file,
2600 " %d Matched conflict early clobber reloads:"
2601 "reject--\n",
2603 reject--;
2604 overall += LRA_LOSER_COST_FACTOR - 1;
2607 if (lra_dump_file != NULL)
2608 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2609 nalt, overall, losers, reload_nregs);
2611 /* If this alternative can be made to work by reloading, and it
2612 needs less reloading than the others checked so far, record
2613 it as the chosen goal for reloading. */
2614 if ((best_losers != 0 && losers == 0)
2615 || (((best_losers == 0 && losers == 0)
2616 || (best_losers != 0 && losers != 0))
2617 && (best_overall > overall
2618 || (best_overall == overall
2619 /* If the cost of the reloads is the same,
2620 prefer alternative which requires minimal
2621 number of reload regs. */
2622 && (reload_nregs < best_reload_nregs
2623 || (reload_nregs == best_reload_nregs
2624 && (best_reload_sum < reload_sum
2625 || (best_reload_sum == reload_sum
2626 && nalt < goal_alt_number))))))))
2628 for (nop = 0; nop < n_operands; nop++)
2630 goal_alt_win[nop] = curr_alt_win[nop];
2631 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2632 goal_alt_matches[nop] = curr_alt_matches[nop];
2633 goal_alt[nop] = curr_alt[nop];
2634 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2636 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2637 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2638 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2639 goal_alt_swapped = curr_swapped;
2640 best_overall = overall;
2641 best_losers = losers;
2642 best_reload_nregs = reload_nregs;
2643 best_reload_sum = reload_sum;
2644 goal_alt_number = nalt;
2646 if (losers == 0)
2647 /* Everything is satisfied. Do not process alternatives
2648 anymore. */
2649 break;
2650 fail:
2653 return ok_p;
2656 /* Make reload base reg from address AD. */
2657 static rtx
2658 base_to_reg (struct address_info *ad)
2660 enum reg_class cl;
2661 int code = -1;
2662 rtx new_inner = NULL_RTX;
2663 rtx new_reg = NULL_RTX;
2664 rtx_insn *insn;
2665 rtx_insn *last_insn = get_last_insn();
2667 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2668 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2669 get_index_code (ad));
2670 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2671 cl, "base");
2672 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2673 ad->disp_term == NULL
2674 ? gen_int_mode (0, ad->mode)
2675 : *ad->disp_term);
2676 if (!valid_address_p (ad->mode, new_inner, ad->as))
2677 return NULL_RTX;
2678 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base_term));
2679 code = recog_memoized (insn);
2680 if (code < 0)
2682 delete_insns_since (last_insn);
2683 return NULL_RTX;
2686 return new_inner;
2689 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2690 static rtx
2691 base_plus_disp_to_reg (struct address_info *ad)
2693 enum reg_class cl;
2694 rtx new_reg;
2696 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2697 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2698 get_index_code (ad));
2699 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2700 cl, "base + disp");
2701 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2702 return new_reg;
2705 /* Make reload of index part of address AD. Return the new
2706 pseudo. */
2707 static rtx
2708 index_part_to_reg (struct address_info *ad)
2710 rtx new_reg;
2712 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2713 INDEX_REG_CLASS, "index term");
2714 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2715 GEN_INT (get_index_scale (ad)), new_reg, 1);
2716 return new_reg;
2719 /* Return true if we can add a displacement to address AD, even if that
2720 makes the address invalid. The fix-up code requires any new address
2721 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2722 static bool
2723 can_add_disp_p (struct address_info *ad)
2725 return (!ad->autoinc_p
2726 && ad->segment == NULL
2727 && ad->base == ad->base_term
2728 && ad->disp == ad->disp_term);
2731 /* Make equiv substitution in address AD. Return true if a substitution
2732 was made. */
2733 static bool
2734 equiv_address_substitution (struct address_info *ad)
2736 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2737 HOST_WIDE_INT disp, scale;
2738 bool change_p;
2740 base_term = strip_subreg (ad->base_term);
2741 if (base_term == NULL)
2742 base_reg = new_base_reg = NULL_RTX;
2743 else
2745 base_reg = *base_term;
2746 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2748 index_term = strip_subreg (ad->index_term);
2749 if (index_term == NULL)
2750 index_reg = new_index_reg = NULL_RTX;
2751 else
2753 index_reg = *index_term;
2754 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2756 if (base_reg == new_base_reg && index_reg == new_index_reg)
2757 return false;
2758 disp = 0;
2759 change_p = false;
2760 if (lra_dump_file != NULL)
2762 fprintf (lra_dump_file, "Changing address in insn %d ",
2763 INSN_UID (curr_insn));
2764 dump_value_slim (lra_dump_file, *ad->outer, 1);
2766 if (base_reg != new_base_reg)
2768 if (REG_P (new_base_reg))
2770 *base_term = new_base_reg;
2771 change_p = true;
2773 else if (GET_CODE (new_base_reg) == PLUS
2774 && REG_P (XEXP (new_base_reg, 0))
2775 && CONST_INT_P (XEXP (new_base_reg, 1))
2776 && can_add_disp_p (ad))
2778 disp += INTVAL (XEXP (new_base_reg, 1));
2779 *base_term = XEXP (new_base_reg, 0);
2780 change_p = true;
2782 if (ad->base_term2 != NULL)
2783 *ad->base_term2 = *ad->base_term;
2785 if (index_reg != new_index_reg)
2787 if (REG_P (new_index_reg))
2789 *index_term = new_index_reg;
2790 change_p = true;
2792 else if (GET_CODE (new_index_reg) == PLUS
2793 && REG_P (XEXP (new_index_reg, 0))
2794 && CONST_INT_P (XEXP (new_index_reg, 1))
2795 && can_add_disp_p (ad)
2796 && (scale = get_index_scale (ad)))
2798 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2799 *index_term = XEXP (new_index_reg, 0);
2800 change_p = true;
2803 if (disp != 0)
2805 if (ad->disp != NULL)
2806 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2807 else
2809 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2810 update_address (ad);
2812 change_p = true;
2814 if (lra_dump_file != NULL)
2816 if (! change_p)
2817 fprintf (lra_dump_file, " -- no change\n");
2818 else
2820 fprintf (lra_dump_file, " on equiv ");
2821 dump_value_slim (lra_dump_file, *ad->outer, 1);
2822 fprintf (lra_dump_file, "\n");
2825 return change_p;
2828 /* Major function to make reloads for an address in operand NOP or
2829 check its correctness (If CHECK_ONLY_P is true). The supported
2830 cases are:
2832 1) an address that existed before LRA started, at which point it
2833 must have been valid. These addresses are subject to elimination
2834 and may have become invalid due to the elimination offset being out
2835 of range.
2837 2) an address created by forcing a constant to memory
2838 (force_const_to_mem). The initial form of these addresses might
2839 not be valid, and it is this function's job to make them valid.
2841 3) a frame address formed from a register and a (possibly zero)
2842 constant offset. As above, these addresses might not be valid and
2843 this function must make them so.
2845 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2846 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2847 address. Return true for any RTL change.
2849 The function is a helper function which does not produce all
2850 transformations (when CHECK_ONLY_P is false) which can be
2851 necessary. It does just basic steps. To do all necessary
2852 transformations use function process_address. */
2853 static bool
2854 process_address_1 (int nop, bool check_only_p,
2855 rtx_insn **before, rtx_insn **after)
2857 struct address_info ad;
2858 rtx new_reg;
2859 rtx op = *curr_id->operand_loc[nop];
2860 const char *constraint = curr_static_id->operand[nop].constraint;
2861 enum constraint_num cn = lookup_constraint (constraint);
2862 bool change_p = false;
2864 if (insn_extra_address_constraint (cn))
2865 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2866 else if (MEM_P (op))
2867 decompose_mem_address (&ad, op);
2868 else if (GET_CODE (op) == SUBREG
2869 && MEM_P (SUBREG_REG (op)))
2870 decompose_mem_address (&ad, SUBREG_REG (op));
2871 else
2872 return false;
2873 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
2874 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
2875 when INDEX_REG_CLASS is a single register class. */
2876 if (ad.base_term != NULL
2877 && ad.index_term != NULL
2878 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
2879 && REG_P (*ad.base_term)
2880 && REG_P (*ad.index_term)
2881 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
2882 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
2884 std::swap (ad.base, ad.index);
2885 std::swap (ad.base_term, ad.index_term);
2887 if (! check_only_p)
2888 change_p = equiv_address_substitution (&ad);
2889 if (ad.base_term != NULL
2890 && (process_addr_reg
2891 (ad.base_term, check_only_p, before,
2892 (ad.autoinc_p
2893 && !(REG_P (*ad.base_term)
2894 && find_regno_note (curr_insn, REG_DEAD,
2895 REGNO (*ad.base_term)) != NULL_RTX)
2896 ? after : NULL),
2897 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2898 get_index_code (&ad)))))
2900 change_p = true;
2901 if (ad.base_term2 != NULL)
2902 *ad.base_term2 = *ad.base_term;
2904 if (ad.index_term != NULL
2905 && process_addr_reg (ad.index_term, check_only_p,
2906 before, NULL, INDEX_REG_CLASS))
2907 change_p = true;
2909 /* Target hooks sometimes don't treat extra-constraint addresses as
2910 legitimate address_operands, so handle them specially. */
2911 if (insn_extra_address_constraint (cn)
2912 && satisfies_address_constraint_p (&ad, cn))
2913 return change_p;
2915 if (check_only_p)
2916 return change_p;
2918 /* There are three cases where the shape of *AD.INNER may now be invalid:
2920 1) the original address was valid, but either elimination or
2921 equiv_address_substitution was applied and that made
2922 the address invalid.
2924 2) the address is an invalid symbolic address created by
2925 force_const_to_mem.
2927 3) the address is a frame address with an invalid offset.
2929 4) the address is a frame address with an invalid base.
2931 All these cases involve a non-autoinc address, so there is no
2932 point revalidating other types. */
2933 if (ad.autoinc_p || valid_address_p (&ad))
2934 return change_p;
2936 /* Any index existed before LRA started, so we can assume that the
2937 presence and shape of the index is valid. */
2938 push_to_sequence (*before);
2939 lra_assert (ad.disp == ad.disp_term);
2940 if (ad.base == NULL)
2942 if (ad.index == NULL)
2944 int code = -1;
2945 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2946 SCRATCH, SCRATCH);
2947 rtx addr = *ad.inner;
2949 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2950 if (HAVE_lo_sum)
2952 rtx_insn *insn;
2953 rtx_insn *last = get_last_insn ();
2955 /* addr => lo_sum (new_base, addr), case (2) above. */
2956 insn = emit_insn (gen_rtx_SET
2957 (new_reg,
2958 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2959 code = recog_memoized (insn);
2960 if (code >= 0)
2962 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2963 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2965 /* Try to put lo_sum into register. */
2966 insn = emit_insn (gen_rtx_SET
2967 (new_reg,
2968 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2969 code = recog_memoized (insn);
2970 if (code >= 0)
2972 *ad.inner = new_reg;
2973 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2975 *ad.inner = addr;
2976 code = -1;
2982 if (code < 0)
2983 delete_insns_since (last);
2986 if (code < 0)
2988 /* addr => new_base, case (2) above. */
2989 lra_emit_move (new_reg, addr);
2990 *ad.inner = new_reg;
2993 else
2995 /* index * scale + disp => new base + index * scale,
2996 case (1) above. */
2997 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
2998 GET_CODE (*ad.index));
3000 lra_assert (INDEX_REG_CLASS != NO_REGS);
3001 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3002 lra_emit_move (new_reg, *ad.disp);
3003 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3004 new_reg, *ad.index);
3007 else if (ad.index == NULL)
3009 int regno;
3010 enum reg_class cl;
3011 rtx set;
3012 rtx_insn *insns, *last_insn;
3013 /* Try to reload base into register only if the base is invalid
3014 for the address but with valid offset, case (4) above. */
3015 start_sequence ();
3016 new_reg = base_to_reg (&ad);
3018 /* base + disp => new base, cases (1) and (3) above. */
3019 /* Another option would be to reload the displacement into an
3020 index register. However, postreload has code to optimize
3021 address reloads that have the same base and different
3022 displacements, so reloading into an index register would
3023 not necessarily be a win. */
3024 if (new_reg == NULL_RTX)
3025 new_reg = base_plus_disp_to_reg (&ad);
3026 insns = get_insns ();
3027 last_insn = get_last_insn ();
3028 /* If we generated at least two insns, try last insn source as
3029 an address. If we succeed, we generate one less insn. */
3030 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3031 && GET_CODE (SET_SRC (set)) == PLUS
3032 && REG_P (XEXP (SET_SRC (set), 0))
3033 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3035 *ad.inner = SET_SRC (set);
3036 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3038 *ad.base_term = XEXP (SET_SRC (set), 0);
3039 *ad.disp_term = XEXP (SET_SRC (set), 1);
3040 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3041 get_index_code (&ad));
3042 regno = REGNO (*ad.base_term);
3043 if (regno >= FIRST_PSEUDO_REGISTER
3044 && cl != lra_get_allocno_class (regno))
3045 lra_change_class (regno, cl, " Change to", true);
3046 new_reg = SET_SRC (set);
3047 delete_insns_since (PREV_INSN (last_insn));
3050 /* Try if target can split displacement into legitimite new disp
3051 and offset. If it's the case, we replace the last insn with
3052 insns for base + offset => new_reg and set new_reg + new disp
3053 to *ad.inner. */
3054 last_insn = get_last_insn ();
3055 if ((set = single_set (last_insn)) != NULL_RTX
3056 && GET_CODE (SET_SRC (set)) == PLUS
3057 && REG_P (XEXP (SET_SRC (set), 0))
3058 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3059 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3061 rtx addend, disp = XEXP (SET_SRC (set), 1);
3062 if (targetm.legitimize_address_displacement (&disp, &addend,
3063 ad.mode))
3065 rtx_insn *new_insns;
3066 start_sequence ();
3067 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3068 new_insns = get_insns ();
3069 end_sequence ();
3070 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3071 delete_insns_since (PREV_INSN (last_insn));
3072 add_insn (new_insns);
3073 insns = get_insns ();
3076 end_sequence ();
3077 emit_insn (insns);
3078 *ad.inner = new_reg;
3080 else if (ad.disp_term != NULL)
3082 /* base + scale * index + disp => new base + scale * index,
3083 case (1) above. */
3084 new_reg = base_plus_disp_to_reg (&ad);
3085 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3086 new_reg, *ad.index);
3088 else if (get_index_scale (&ad) == 1)
3090 /* The last transformation to one reg will be made in
3091 curr_insn_transform function. */
3092 end_sequence ();
3093 return false;
3095 else
3097 /* base + scale * index => base + new_reg,
3098 case (1) above.
3099 Index part of address may become invalid. For example, we
3100 changed pseudo on the equivalent memory and a subreg of the
3101 pseudo onto the memory of different mode for which the scale is
3102 prohibitted. */
3103 new_reg = index_part_to_reg (&ad);
3104 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3105 *ad.base_term, new_reg);
3107 *before = get_insns ();
3108 end_sequence ();
3109 return true;
3112 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3113 Use process_address_1 as a helper function. Return true for any
3114 RTL changes.
3116 If CHECK_ONLY_P is true, just check address correctness. Return
3117 false if the address correct. */
3118 static bool
3119 process_address (int nop, bool check_only_p,
3120 rtx_insn **before, rtx_insn **after)
3122 bool res = false;
3124 while (process_address_1 (nop, check_only_p, before, after))
3126 if (check_only_p)
3127 return true;
3128 res = true;
3130 return res;
3133 /* Emit insns to reload VALUE into a new register. VALUE is an
3134 auto-increment or auto-decrement RTX whose operand is a register or
3135 memory location; so reloading involves incrementing that location.
3136 IN is either identical to VALUE, or some cheaper place to reload
3137 value being incremented/decremented from.
3139 INC_AMOUNT is the number to increment or decrement by (always
3140 positive and ignored for POST_MODIFY/PRE_MODIFY).
3142 Return pseudo containing the result. */
3143 static rtx
3144 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3146 /* REG or MEM to be copied and incremented. */
3147 rtx incloc = XEXP (value, 0);
3148 /* Nonzero if increment after copying. */
3149 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3150 || GET_CODE (value) == POST_MODIFY);
3151 rtx_insn *last;
3152 rtx inc;
3153 rtx_insn *add_insn;
3154 int code;
3155 rtx real_in = in == value ? incloc : in;
3156 rtx result;
3157 bool plus_p = true;
3159 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3161 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3162 || GET_CODE (XEXP (value, 1)) == MINUS);
3163 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3164 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3165 inc = XEXP (XEXP (value, 1), 1);
3167 else
3169 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3170 inc_amount = -inc_amount;
3172 inc = GEN_INT (inc_amount);
3175 if (! post && REG_P (incloc))
3176 result = incloc;
3177 else
3178 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3179 "INC/DEC result");
3181 if (real_in != result)
3183 /* First copy the location to the result register. */
3184 lra_assert (REG_P (result));
3185 emit_insn (gen_move_insn (result, real_in));
3188 /* We suppose that there are insns to add/sub with the constant
3189 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3190 old reload worked with this assumption. If the assumption
3191 becomes wrong, we should use approach in function
3192 base_plus_disp_to_reg. */
3193 if (in == value)
3195 /* See if we can directly increment INCLOC. */
3196 last = get_last_insn ();
3197 add_insn = emit_insn (plus_p
3198 ? gen_add2_insn (incloc, inc)
3199 : gen_sub2_insn (incloc, inc));
3201 code = recog_memoized (add_insn);
3202 if (code >= 0)
3204 if (! post && result != incloc)
3205 emit_insn (gen_move_insn (result, incloc));
3206 return result;
3208 delete_insns_since (last);
3211 /* If couldn't do the increment directly, must increment in RESULT.
3212 The way we do this depends on whether this is pre- or
3213 post-increment. For pre-increment, copy INCLOC to the reload
3214 register, increment it there, then save back. */
3215 if (! post)
3217 if (real_in != result)
3218 emit_insn (gen_move_insn (result, real_in));
3219 if (plus_p)
3220 emit_insn (gen_add2_insn (result, inc));
3221 else
3222 emit_insn (gen_sub2_insn (result, inc));
3223 if (result != incloc)
3224 emit_insn (gen_move_insn (incloc, result));
3226 else
3228 /* Post-increment.
3230 Because this might be a jump insn or a compare, and because
3231 RESULT may not be available after the insn in an input
3232 reload, we must do the incrementing before the insn being
3233 reloaded for.
3235 We have already copied IN to RESULT. Increment the copy in
3236 RESULT, save that back, then decrement RESULT so it has
3237 the original value. */
3238 if (plus_p)
3239 emit_insn (gen_add2_insn (result, inc));
3240 else
3241 emit_insn (gen_sub2_insn (result, inc));
3242 emit_insn (gen_move_insn (incloc, result));
3243 /* Restore non-modified value for the result. We prefer this
3244 way because it does not require an additional hard
3245 register. */
3246 if (plus_p)
3248 if (CONST_INT_P (inc))
3249 emit_insn (gen_add2_insn (result,
3250 gen_int_mode (-INTVAL (inc),
3251 GET_MODE (result))));
3252 else
3253 emit_insn (gen_sub2_insn (result, inc));
3255 else
3256 emit_insn (gen_add2_insn (result, inc));
3258 return result;
3261 /* Return true if the current move insn does not need processing as we
3262 already know that it satisfies its constraints. */
3263 static bool
3264 simple_move_p (void)
3266 rtx dest, src;
3267 enum reg_class dclass, sclass;
3269 lra_assert (curr_insn_set != NULL_RTX);
3270 dest = SET_DEST (curr_insn_set);
3271 src = SET_SRC (curr_insn_set);
3272 return ((dclass = get_op_class (dest)) != NO_REGS
3273 && (sclass = get_op_class (src)) != NO_REGS
3274 /* The backend guarantees that register moves of cost 2
3275 never need reloads. */
3276 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3279 /* Swap operands NOP and NOP + 1. */
3280 static inline void
3281 swap_operands (int nop)
3283 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3284 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3285 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3286 /* Swap the duplicates too. */
3287 lra_update_dup (curr_id, nop);
3288 lra_update_dup (curr_id, nop + 1);
3291 /* Main entry point of the constraint code: search the body of the
3292 current insn to choose the best alternative. It is mimicking insn
3293 alternative cost calculation model of former reload pass. That is
3294 because machine descriptions were written to use this model. This
3295 model can be changed in future. Make commutative operand exchange
3296 if it is chosen.
3298 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3299 constraints. Return true if any change happened during function
3300 call.
3302 If CHECK_ONLY_P is true then don't do any transformation. Just
3303 check that the insn satisfies all constraints. If the insn does
3304 not satisfy any constraint, return true. */
3305 static bool
3306 curr_insn_transform (bool check_only_p)
3308 int i, j, k;
3309 int n_operands;
3310 int n_alternatives;
3311 int commutative;
3312 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3313 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3314 rtx_insn *before, *after;
3315 bool alt_p = false;
3316 /* Flag that the insn has been changed through a transformation. */
3317 bool change_p;
3318 bool sec_mem_p;
3319 #ifdef SECONDARY_MEMORY_NEEDED
3320 bool use_sec_mem_p;
3321 #endif
3322 int max_regno_before;
3323 int reused_alternative_num;
3325 curr_insn_set = single_set (curr_insn);
3326 if (curr_insn_set != NULL_RTX && simple_move_p ())
3327 return false;
3329 no_input_reloads_p = no_output_reloads_p = false;
3330 goal_alt_number = -1;
3331 change_p = sec_mem_p = false;
3332 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3333 reloads; neither are insns that SET cc0. Insns that use CC0 are
3334 not allowed to have any input reloads. */
3335 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3336 no_output_reloads_p = true;
3338 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3339 no_input_reloads_p = true;
3340 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3341 no_output_reloads_p = true;
3343 n_operands = curr_static_id->n_operands;
3344 n_alternatives = curr_static_id->n_alternatives;
3346 /* Just return "no reloads" if insn has no operands with
3347 constraints. */
3348 if (n_operands == 0 || n_alternatives == 0)
3349 return false;
3351 max_regno_before = max_reg_num ();
3353 for (i = 0; i < n_operands; i++)
3355 goal_alt_matched[i][0] = -1;
3356 goal_alt_matches[i] = -1;
3359 commutative = curr_static_id->commutative;
3361 /* Now see what we need for pseudos that didn't get hard regs or got
3362 the wrong kind of hard reg. For this, we must consider all the
3363 operands together against the register constraints. */
3365 best_losers = best_overall = INT_MAX;
3366 best_reload_sum = 0;
3368 curr_swapped = false;
3369 goal_alt_swapped = false;
3371 if (! check_only_p)
3372 /* Make equivalence substitution and memory subreg elimination
3373 before address processing because an address legitimacy can
3374 depend on memory mode. */
3375 for (i = 0; i < n_operands; i++)
3377 rtx op = *curr_id->operand_loc[i];
3378 rtx subst, old = op;
3379 bool op_change_p = false;
3381 if (GET_CODE (old) == SUBREG)
3382 old = SUBREG_REG (old);
3383 subst = get_equiv_with_elimination (old, curr_insn);
3384 original_subreg_reg_mode[i] = VOIDmode;
3385 if (subst != old)
3387 subst = copy_rtx (subst);
3388 lra_assert (REG_P (old));
3389 if (GET_CODE (op) != SUBREG)
3390 *curr_id->operand_loc[i] = subst;
3391 else
3393 SUBREG_REG (op) = subst;
3394 if (GET_MODE (subst) == VOIDmode)
3395 original_subreg_reg_mode[i] = GET_MODE (old);
3397 if (lra_dump_file != NULL)
3399 fprintf (lra_dump_file,
3400 "Changing pseudo %d in operand %i of insn %u on equiv ",
3401 REGNO (old), i, INSN_UID (curr_insn));
3402 dump_value_slim (lra_dump_file, subst, 1);
3403 fprintf (lra_dump_file, "\n");
3405 op_change_p = change_p = true;
3407 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3409 change_p = true;
3410 lra_update_dup (curr_id, i);
3414 /* Reload address registers and displacements. We do it before
3415 finding an alternative because of memory constraints. */
3416 before = after = NULL;
3417 for (i = 0; i < n_operands; i++)
3418 if (! curr_static_id->operand[i].is_operator
3419 && process_address (i, check_only_p, &before, &after))
3421 if (check_only_p)
3422 return true;
3423 change_p = true;
3424 lra_update_dup (curr_id, i);
3427 if (change_p)
3428 /* If we've changed the instruction then any alternative that
3429 we chose previously may no longer be valid. */
3430 lra_set_used_insn_alternative (curr_insn, -1);
3432 if (! check_only_p && curr_insn_set != NULL_RTX
3433 && check_and_process_move (&change_p, &sec_mem_p))
3434 return change_p;
3436 try_swapped:
3438 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3439 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3440 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3441 reused_alternative_num, INSN_UID (curr_insn));
3443 if (process_alt_operands (reused_alternative_num))
3444 alt_p = true;
3446 if (check_only_p)
3447 return ! alt_p || best_losers != 0;
3449 /* If insn is commutative (it's safe to exchange a certain pair of
3450 operands) then we need to try each alternative twice, the second
3451 time matching those two operands as if we had exchanged them. To
3452 do this, really exchange them in operands.
3454 If we have just tried the alternatives the second time, return
3455 operands to normal and drop through. */
3457 if (reused_alternative_num < 0 && commutative >= 0)
3459 curr_swapped = !curr_swapped;
3460 if (curr_swapped)
3462 swap_operands (commutative);
3463 goto try_swapped;
3465 else
3466 swap_operands (commutative);
3469 if (! alt_p && ! sec_mem_p)
3471 /* No alternative works with reloads?? */
3472 if (INSN_CODE (curr_insn) >= 0)
3473 fatal_insn ("unable to generate reloads for:", curr_insn);
3474 error_for_asm (curr_insn,
3475 "inconsistent operand constraints in an %<asm%>");
3476 /* Avoid further trouble with this insn. */
3477 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3478 lra_invalidate_insn_data (curr_insn);
3479 return true;
3482 /* If the best alternative is with operands 1 and 2 swapped, swap
3483 them. Update the operand numbers of any reloads already
3484 pushed. */
3486 if (goal_alt_swapped)
3488 if (lra_dump_file != NULL)
3489 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3490 INSN_UID (curr_insn));
3492 /* Swap the duplicates too. */
3493 swap_operands (commutative);
3494 change_p = true;
3497 #ifdef SECONDARY_MEMORY_NEEDED
3498 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3499 too conservatively. So we use the secondary memory only if there
3500 is no any alternative without reloads. */
3501 use_sec_mem_p = false;
3502 if (! alt_p)
3503 use_sec_mem_p = true;
3504 else if (sec_mem_p)
3506 for (i = 0; i < n_operands; i++)
3507 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3508 break;
3509 use_sec_mem_p = i < n_operands;
3512 if (use_sec_mem_p)
3514 rtx new_reg, src, dest, rld;
3515 machine_mode sec_mode, rld_mode;
3517 lra_assert (sec_mem_p);
3518 lra_assert (curr_static_id->operand[0].type == OP_OUT
3519 && curr_static_id->operand[1].type == OP_IN);
3520 dest = *curr_id->operand_loc[0];
3521 src = *curr_id->operand_loc[1];
3522 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3523 ? dest : src);
3524 rld_mode = GET_MODE (rld);
3525 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3526 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3527 #else
3528 sec_mode = rld_mode;
3529 #endif
3530 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3531 NO_REGS, "secondary");
3532 /* If the mode is changed, it should be wider. */
3533 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3534 if (sec_mode != rld_mode)
3536 /* If the target says specifically to use another mode for
3537 secondary memory moves we can not reuse the original
3538 insn. */
3539 after = emit_spill_move (false, new_reg, dest);
3540 lra_process_new_insns (curr_insn, NULL, after,
3541 "Inserting the sec. move");
3542 /* We may have non null BEFORE here (e.g. after address
3543 processing. */
3544 push_to_sequence (before);
3545 before = emit_spill_move (true, new_reg, src);
3546 emit_insn (before);
3547 before = get_insns ();
3548 end_sequence ();
3549 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3550 lra_set_insn_deleted (curr_insn);
3552 else if (dest == rld)
3554 *curr_id->operand_loc[0] = new_reg;
3555 after = emit_spill_move (false, new_reg, dest);
3556 lra_process_new_insns (curr_insn, NULL, after,
3557 "Inserting the sec. move");
3559 else
3561 *curr_id->operand_loc[1] = new_reg;
3562 /* See comments above. */
3563 push_to_sequence (before);
3564 before = emit_spill_move (true, new_reg, src);
3565 emit_insn (before);
3566 before = get_insns ();
3567 end_sequence ();
3568 lra_process_new_insns (curr_insn, before, NULL,
3569 "Inserting the sec. move");
3571 lra_update_insn_regno_info (curr_insn);
3572 return true;
3574 #endif
3576 lra_assert (goal_alt_number >= 0);
3577 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3579 if (lra_dump_file != NULL)
3581 const char *p;
3583 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3584 goal_alt_number, INSN_UID (curr_insn));
3585 for (i = 0; i < n_operands; i++)
3587 p = (curr_static_id->operand_alternative
3588 [goal_alt_number * n_operands + i].constraint);
3589 if (*p == '\0')
3590 continue;
3591 fprintf (lra_dump_file, " (%d) ", i);
3592 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3593 fputc (*p, lra_dump_file);
3595 if (INSN_CODE (curr_insn) >= 0
3596 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3597 fprintf (lra_dump_file, " {%s}", p);
3598 if (curr_id->sp_offset != 0)
3599 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3600 curr_id->sp_offset);
3601 fprintf (lra_dump_file, "\n");
3604 /* Right now, for any pair of operands I and J that are required to
3605 match, with J < I, goal_alt_matches[I] is J. Add I to
3606 goal_alt_matched[J]. */
3608 for (i = 0; i < n_operands; i++)
3609 if ((j = goal_alt_matches[i]) >= 0)
3611 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3613 /* We allow matching one output operand and several input
3614 operands. */
3615 lra_assert (k == 0
3616 || (curr_static_id->operand[j].type == OP_OUT
3617 && curr_static_id->operand[i].type == OP_IN
3618 && (curr_static_id->operand
3619 [goal_alt_matched[j][0]].type == OP_IN)));
3620 goal_alt_matched[j][k] = i;
3621 goal_alt_matched[j][k + 1] = -1;
3624 for (i = 0; i < n_operands; i++)
3625 goal_alt_win[i] |= goal_alt_match_win[i];
3627 /* Any constants that aren't allowed and can't be reloaded into
3628 registers are here changed into memory references. */
3629 for (i = 0; i < n_operands; i++)
3630 if (goal_alt_win[i])
3632 int regno;
3633 enum reg_class new_class;
3634 rtx reg = *curr_id->operand_loc[i];
3636 if (GET_CODE (reg) == SUBREG)
3637 reg = SUBREG_REG (reg);
3639 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3641 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3643 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3645 lra_assert (ok_p);
3646 lra_change_class (regno, new_class, " Change to", true);
3650 else
3652 const char *constraint;
3653 char c;
3654 rtx op = *curr_id->operand_loc[i];
3655 rtx subreg = NULL_RTX;
3656 machine_mode mode = curr_operand_mode[i];
3658 if (GET_CODE (op) == SUBREG)
3660 subreg = op;
3661 op = SUBREG_REG (op);
3662 mode = GET_MODE (op);
3665 if (CONST_POOL_OK_P (mode, op)
3666 && ((targetm.preferred_reload_class
3667 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3668 || no_input_reloads_p))
3670 rtx tem = force_const_mem (mode, op);
3672 change_p = true;
3673 if (subreg != NULL_RTX)
3674 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3676 *curr_id->operand_loc[i] = tem;
3677 lra_update_dup (curr_id, i);
3678 process_address (i, false, &before, &after);
3680 /* If the alternative accepts constant pool refs directly
3681 there will be no reload needed at all. */
3682 if (subreg != NULL_RTX)
3683 continue;
3684 /* Skip alternatives before the one requested. */
3685 constraint = (curr_static_id->operand_alternative
3686 [goal_alt_number * n_operands + i].constraint);
3687 for (;
3688 (c = *constraint) && c != ',' && c != '#';
3689 constraint += CONSTRAINT_LEN (c, constraint))
3691 enum constraint_num cn = lookup_constraint (constraint);
3692 if (insn_extra_memory_constraint (cn)
3693 && satisfies_memory_constraint_p (tem, cn))
3694 break;
3696 if (c == '\0' || c == ',' || c == '#')
3697 continue;
3699 goal_alt_win[i] = true;
3703 for (i = 0; i < n_operands; i++)
3705 int regno;
3706 bool optional_p = false;
3707 rtx old, new_reg;
3708 rtx op = *curr_id->operand_loc[i];
3710 if (goal_alt_win[i])
3712 if (goal_alt[i] == NO_REGS
3713 && REG_P (op)
3714 /* When we assign NO_REGS it means that we will not
3715 assign a hard register to the scratch pseudo by
3716 assigment pass and the scratch pseudo will be
3717 spilled. Spilled scratch pseudos are transformed
3718 back to scratches at the LRA end. */
3719 && lra_former_scratch_operand_p (curr_insn, i))
3721 int regno = REGNO (op);
3722 lra_change_class (regno, NO_REGS, " Change to", true);
3723 if (lra_get_regno_hard_regno (regno) >= 0)
3724 /* We don't have to mark all insn affected by the
3725 spilled pseudo as there is only one such insn, the
3726 current one. */
3727 reg_renumber[regno] = -1;
3729 /* We can do an optional reload. If the pseudo got a hard
3730 reg, we might improve the code through inheritance. If
3731 it does not get a hard register we coalesce memory/memory
3732 moves later. Ignore move insns to avoid cycling. */
3733 if (! lra_simple_p
3734 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3735 && goal_alt[i] != NO_REGS && REG_P (op)
3736 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3737 && regno < new_regno_start
3738 && ! lra_former_scratch_p (regno)
3739 && reg_renumber[regno] < 0
3740 /* Check that the optional reload pseudo will be able to
3741 hold given mode value. */
3742 && ! (prohibited_class_reg_set_mode_p
3743 (goal_alt[i], reg_class_contents[goal_alt[i]],
3744 PSEUDO_REGNO_MODE (regno)))
3745 && (curr_insn_set == NULL_RTX
3746 || !((REG_P (SET_SRC (curr_insn_set))
3747 || MEM_P (SET_SRC (curr_insn_set))
3748 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3749 && (REG_P (SET_DEST (curr_insn_set))
3750 || MEM_P (SET_DEST (curr_insn_set))
3751 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3752 optional_p = true;
3753 else
3754 continue;
3757 /* Operands that match previous ones have already been handled. */
3758 if (goal_alt_matches[i] >= 0)
3759 continue;
3761 /* We should not have an operand with a non-offsettable address
3762 appearing where an offsettable address will do. It also may
3763 be a case when the address should be special in other words
3764 not a general one (e.g. it needs no index reg). */
3765 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3767 enum reg_class rclass;
3768 rtx *loc = &XEXP (op, 0);
3769 enum rtx_code code = GET_CODE (*loc);
3771 push_to_sequence (before);
3772 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3773 MEM, SCRATCH);
3774 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3775 new_reg = emit_inc (rclass, *loc, *loc,
3776 /* This value does not matter for MODIFY. */
3777 GET_MODE_SIZE (GET_MODE (op)));
3778 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3779 "offsetable address", &new_reg))
3780 lra_emit_move (new_reg, *loc);
3781 before = get_insns ();
3782 end_sequence ();
3783 *loc = new_reg;
3784 lra_update_dup (curr_id, i);
3786 else if (goal_alt_matched[i][0] == -1)
3788 machine_mode mode;
3789 rtx reg, *loc;
3790 int hard_regno, byte;
3791 enum op_type type = curr_static_id->operand[i].type;
3793 loc = curr_id->operand_loc[i];
3794 mode = curr_operand_mode[i];
3795 if (GET_CODE (*loc) == SUBREG)
3797 reg = SUBREG_REG (*loc);
3798 byte = SUBREG_BYTE (*loc);
3799 if (REG_P (reg)
3800 /* Strict_low_part requires reload the register not
3801 the sub-register. */
3802 && (curr_static_id->operand[i].strict_low
3803 || (GET_MODE_SIZE (mode)
3804 <= GET_MODE_SIZE (GET_MODE (reg))
3805 && (hard_regno
3806 = get_try_hard_regno (REGNO (reg))) >= 0
3807 && (simplify_subreg_regno
3808 (hard_regno,
3809 GET_MODE (reg), byte, mode) < 0)
3810 && (goal_alt[i] == NO_REGS
3811 || (simplify_subreg_regno
3812 (ira_class_hard_regs[goal_alt[i]][0],
3813 GET_MODE (reg), byte, mode) >= 0)))))
3815 if (type == OP_OUT)
3816 type = OP_INOUT;
3817 loc = &SUBREG_REG (*loc);
3818 mode = GET_MODE (*loc);
3821 old = *loc;
3822 if (get_reload_reg (type, mode, old, goal_alt[i],
3823 loc != curr_id->operand_loc[i], "", &new_reg)
3824 && type != OP_OUT)
3826 push_to_sequence (before);
3827 lra_emit_move (new_reg, old);
3828 before = get_insns ();
3829 end_sequence ();
3831 *loc = new_reg;
3832 if (type != OP_IN
3833 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3835 start_sequence ();
3836 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3837 emit_insn (after);
3838 after = get_insns ();
3839 end_sequence ();
3840 *loc = new_reg;
3842 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3843 if (goal_alt_dont_inherit_ops[j] == i)
3845 lra_set_regno_unique_value (REGNO (new_reg));
3846 break;
3848 lra_update_dup (curr_id, i);
3850 else if (curr_static_id->operand[i].type == OP_IN
3851 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3852 == OP_OUT))
3854 /* generate reloads for input and matched outputs. */
3855 match_inputs[0] = i;
3856 match_inputs[1] = -1;
3857 match_reload (goal_alt_matched[i][0], match_inputs,
3858 goal_alt[i], &before, &after);
3860 else if (curr_static_id->operand[i].type == OP_OUT
3861 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3862 == OP_IN))
3863 /* Generate reloads for output and matched inputs. */
3864 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3865 else if (curr_static_id->operand[i].type == OP_IN
3866 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3867 == OP_IN))
3869 /* Generate reloads for matched inputs. */
3870 match_inputs[0] = i;
3871 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3872 match_inputs[j + 1] = k;
3873 match_inputs[j + 1] = -1;
3874 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3876 else
3877 /* We must generate code in any case when function
3878 process_alt_operands decides that it is possible. */
3879 gcc_unreachable ();
3880 if (optional_p)
3882 lra_assert (REG_P (op));
3883 regno = REGNO (op);
3884 op = *curr_id->operand_loc[i]; /* Substitution. */
3885 if (GET_CODE (op) == SUBREG)
3886 op = SUBREG_REG (op);
3887 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3888 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3889 lra_reg_info[REGNO (op)].restore_regno = regno;
3890 if (lra_dump_file != NULL)
3891 fprintf (lra_dump_file,
3892 " Making reload reg %d for reg %d optional\n",
3893 REGNO (op), regno);
3896 if (before != NULL_RTX || after != NULL_RTX
3897 || max_regno_before != max_reg_num ())
3898 change_p = true;
3899 if (change_p)
3901 lra_update_operator_dups (curr_id);
3902 /* Something changes -- process the insn. */
3903 lra_update_insn_regno_info (curr_insn);
3905 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3906 return change_p;
3909 /* Return true if INSN satisfies all constraints. In other words, no
3910 reload insns are needed. */
3911 bool
3912 lra_constrain_insn (rtx_insn *insn)
3914 int saved_new_regno_start = new_regno_start;
3915 int saved_new_insn_uid_start = new_insn_uid_start;
3916 bool change_p;
3918 curr_insn = insn;
3919 curr_id = lra_get_insn_recog_data (curr_insn);
3920 curr_static_id = curr_id->insn_static_data;
3921 new_insn_uid_start = get_max_uid ();
3922 new_regno_start = max_reg_num ();
3923 change_p = curr_insn_transform (true);
3924 new_regno_start = saved_new_regno_start;
3925 new_insn_uid_start = saved_new_insn_uid_start;
3926 return ! change_p;
3929 /* Return true if X is in LIST. */
3930 static bool
3931 in_list_p (rtx x, rtx list)
3933 for (; list != NULL_RTX; list = XEXP (list, 1))
3934 if (XEXP (list, 0) == x)
3935 return true;
3936 return false;
3939 /* Return true if X contains an allocatable hard register (if
3940 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3941 static bool
3942 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3944 int i, j;
3945 const char *fmt;
3946 enum rtx_code code;
3948 code = GET_CODE (x);
3949 if (REG_P (x))
3951 int regno = REGNO (x);
3952 HARD_REG_SET alloc_regs;
3954 if (hard_reg_p)
3956 if (regno >= FIRST_PSEUDO_REGISTER)
3957 regno = lra_get_regno_hard_regno (regno);
3958 if (regno < 0)
3959 return false;
3960 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3961 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3963 else
3965 if (regno < FIRST_PSEUDO_REGISTER)
3966 return false;
3967 if (! spilled_p)
3968 return true;
3969 return lra_get_regno_hard_regno (regno) < 0;
3972 fmt = GET_RTX_FORMAT (code);
3973 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3975 if (fmt[i] == 'e')
3977 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3978 return true;
3980 else if (fmt[i] == 'E')
3982 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3983 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3984 return true;
3987 return false;
3990 /* Return true if X contains a symbol reg. */
3991 static bool
3992 contains_symbol_ref_p (rtx x)
3994 int i, j;
3995 const char *fmt;
3996 enum rtx_code code;
3998 code = GET_CODE (x);
3999 if (code == SYMBOL_REF)
4000 return true;
4001 fmt = GET_RTX_FORMAT (code);
4002 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4004 if (fmt[i] == 'e')
4006 if (contains_symbol_ref_p (XEXP (x, i)))
4007 return true;
4009 else if (fmt[i] == 'E')
4011 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4012 if (contains_symbol_ref_p (XVECEXP (x, i, j)))
4013 return true;
4016 return false;
4019 /* Process all regs in location *LOC and change them on equivalent
4020 substitution. Return true if any change was done. */
4021 static bool
4022 loc_equivalence_change_p (rtx *loc)
4024 rtx subst, reg, x = *loc;
4025 bool result = false;
4026 enum rtx_code code = GET_CODE (x);
4027 const char *fmt;
4028 int i, j;
4030 if (code == SUBREG)
4032 reg = SUBREG_REG (x);
4033 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4034 && GET_MODE (subst) == VOIDmode)
4036 /* We cannot reload debug location. Simplify subreg here
4037 while we know the inner mode. */
4038 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4039 GET_MODE (reg), SUBREG_BYTE (x));
4040 return true;
4043 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4045 *loc = subst;
4046 return true;
4049 /* Scan all the operand sub-expressions. */
4050 fmt = GET_RTX_FORMAT (code);
4051 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4053 if (fmt[i] == 'e')
4054 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4055 else if (fmt[i] == 'E')
4056 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4057 result
4058 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4060 return result;
4063 /* Similar to loc_equivalence_change_p, but for use as
4064 simplify_replace_fn_rtx callback. DATA is insn for which the
4065 elimination is done. If it null we don't do the elimination. */
4066 static rtx
4067 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4069 if (!REG_P (loc))
4070 return NULL_RTX;
4072 rtx subst = (data == NULL
4073 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4074 if (subst != loc)
4075 return subst;
4077 return NULL_RTX;
4080 /* Maximum number of generated reload insns per an insn. It is for
4081 preventing this pass cycling in a bug case. */
4082 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4084 /* The current iteration number of this LRA pass. */
4085 int lra_constraint_iter;
4087 /* True if we substituted equiv which needs checking register
4088 allocation correctness because the equivalent value contains
4089 allocatable hard registers or when we restore multi-register
4090 pseudo. */
4091 bool lra_risky_transformations_p;
4093 /* Return true if REGNO is referenced in more than one block. */
4094 static bool
4095 multi_block_pseudo_p (int regno)
4097 basic_block bb = NULL;
4098 unsigned int uid;
4099 bitmap_iterator bi;
4101 if (regno < FIRST_PSEUDO_REGISTER)
4102 return false;
4104 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4105 if (bb == NULL)
4106 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4107 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4108 return true;
4109 return false;
4112 /* Return true if LIST contains a deleted insn. */
4113 static bool
4114 contains_deleted_insn_p (rtx_insn_list *list)
4116 for (; list != NULL_RTX; list = list->next ())
4117 if (NOTE_P (list->insn ())
4118 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4119 return true;
4120 return false;
4123 /* Return true if X contains a pseudo dying in INSN. */
4124 static bool
4125 dead_pseudo_p (rtx x, rtx_insn *insn)
4127 int i, j;
4128 const char *fmt;
4129 enum rtx_code code;
4131 if (REG_P (x))
4132 return (insn != NULL_RTX
4133 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4134 code = GET_CODE (x);
4135 fmt = GET_RTX_FORMAT (code);
4136 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4138 if (fmt[i] == 'e')
4140 if (dead_pseudo_p (XEXP (x, i), insn))
4141 return true;
4143 else if (fmt[i] == 'E')
4145 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4146 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4147 return true;
4150 return false;
4153 /* Return true if INSN contains a dying pseudo in INSN right hand
4154 side. */
4155 static bool
4156 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4158 rtx set = single_set (insn);
4160 gcc_assert (set != NULL);
4161 return dead_pseudo_p (SET_SRC (set), insn);
4164 /* Return true if any init insn of REGNO contains a dying pseudo in
4165 insn right hand side. */
4166 static bool
4167 init_insn_rhs_dead_pseudo_p (int regno)
4169 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4171 if (insns == NULL)
4172 return false;
4173 for (; insns != NULL_RTX; insns = insns->next ())
4174 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4175 return true;
4176 return false;
4179 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4180 reverse only if we have one init insn with given REGNO as a
4181 source. */
4182 static bool
4183 reverse_equiv_p (int regno)
4185 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4186 rtx set;
4188 if (insns == NULL)
4189 return false;
4190 if (! INSN_P (insns->insn ())
4191 || insns->next () != NULL)
4192 return false;
4193 if ((set = single_set (insns->insn ())) == NULL_RTX)
4194 return false;
4195 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4198 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4199 call this function only for non-reverse equivalence. */
4200 static bool
4201 contains_reloaded_insn_p (int regno)
4203 rtx set;
4204 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4206 for (; list != NULL; list = list->next ())
4207 if ((set = single_set (list->insn ())) == NULL_RTX
4208 || ! REG_P (SET_DEST (set))
4209 || (int) REGNO (SET_DEST (set)) != regno)
4210 return true;
4211 return false;
4214 /* Entry function of LRA constraint pass. Return true if the
4215 constraint pass did change the code. */
4216 bool
4217 lra_constraints (bool first_p)
4219 bool changed_p;
4220 int i, hard_regno, new_insns_num;
4221 unsigned int min_len, new_min_len, uid;
4222 rtx set, x, reg, dest_reg;
4223 basic_block last_bb;
4224 bitmap_head equiv_insn_bitmap;
4225 bitmap_iterator bi;
4227 lra_constraint_iter++;
4228 if (lra_dump_file != NULL)
4229 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4230 lra_constraint_iter);
4231 changed_p = false;
4232 if (pic_offset_table_rtx
4233 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4234 lra_risky_transformations_p = true;
4235 else
4236 lra_risky_transformations_p = false;
4237 new_insn_uid_start = get_max_uid ();
4238 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4239 /* Mark used hard regs for target stack size calulations. */
4240 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4241 if (lra_reg_info[i].nrefs != 0
4242 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4244 int j, nregs;
4246 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4247 for (j = 0; j < nregs; j++)
4248 df_set_regs_ever_live (hard_regno + j, true);
4250 /* Do elimination before the equivalence processing as we can spill
4251 some pseudos during elimination. */
4252 lra_eliminate (false, first_p);
4253 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4254 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4255 if (lra_reg_info[i].nrefs != 0)
4257 ira_reg_equiv[i].profitable_p = true;
4258 reg = regno_reg_rtx[i];
4259 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4261 bool pseudo_p = contains_reg_p (x, false, false);
4263 /* After RTL transformation, we can not guarantee that
4264 pseudo in the substitution was not reloaded which might
4265 make equivalence invalid. For example, in reverse
4266 equiv of p0
4268 p0 <- ...
4270 equiv_mem <- p0
4272 the memory address register was reloaded before the 2nd
4273 insn. */
4274 if ((! first_p && pseudo_p)
4275 /* We don't use DF for compilation speed sake. So it
4276 is problematic to update live info when we use an
4277 equivalence containing pseudos in more than one
4278 BB. */
4279 || (pseudo_p && multi_block_pseudo_p (i))
4280 /* If an init insn was deleted for some reason, cancel
4281 the equiv. We could update the equiv insns after
4282 transformations including an equiv insn deletion
4283 but it is not worthy as such cases are extremely
4284 rare. */
4285 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4286 /* If it is not a reverse equivalence, we check that a
4287 pseudo in rhs of the init insn is not dying in the
4288 insn. Otherwise, the live info at the beginning of
4289 the corresponding BB might be wrong after we
4290 removed the insn. When the equiv can be a
4291 constant, the right hand side of the init insn can
4292 be a pseudo. */
4293 || (! reverse_equiv_p (i)
4294 && (init_insn_rhs_dead_pseudo_p (i)
4295 /* If we reloaded the pseudo in an equivalence
4296 init insn, we can not remove the equiv init
4297 insns and the init insns might write into
4298 const memory in this case. */
4299 || contains_reloaded_insn_p (i)))
4300 /* Prevent access beyond equivalent memory for
4301 paradoxical subregs. */
4302 || (MEM_P (x)
4303 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4304 > GET_MODE_SIZE (GET_MODE (x))))
4305 || (pic_offset_table_rtx
4306 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4307 && (targetm.preferred_reload_class
4308 (x, lra_get_allocno_class (i)) == NO_REGS))
4309 || contains_symbol_ref_p (x))))
4310 ira_reg_equiv[i].defined_p = false;
4311 if (contains_reg_p (x, false, true))
4312 ira_reg_equiv[i].profitable_p = false;
4313 if (get_equiv (reg) != reg)
4314 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4317 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4318 update_equiv (i);
4319 /* We should add all insns containing pseudos which should be
4320 substituted by their equivalences. */
4321 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4322 lra_push_insn_by_uid (uid);
4323 min_len = lra_insn_stack_length ();
4324 new_insns_num = 0;
4325 last_bb = NULL;
4326 changed_p = false;
4327 while ((new_min_len = lra_insn_stack_length ()) != 0)
4329 curr_insn = lra_pop_insn ();
4330 --new_min_len;
4331 curr_bb = BLOCK_FOR_INSN (curr_insn);
4332 if (curr_bb != last_bb)
4334 last_bb = curr_bb;
4335 bb_reload_num = lra_curr_reload_num;
4337 if (min_len > new_min_len)
4339 min_len = new_min_len;
4340 new_insns_num = 0;
4342 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4343 internal_error
4344 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4345 MAX_RELOAD_INSNS_NUMBER);
4346 new_insns_num++;
4347 if (DEBUG_INSN_P (curr_insn))
4349 /* We need to check equivalence in debug insn and change
4350 pseudo to the equivalent value if necessary. */
4351 curr_id = lra_get_insn_recog_data (curr_insn);
4352 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4354 rtx old = *curr_id->operand_loc[0];
4355 *curr_id->operand_loc[0]
4356 = simplify_replace_fn_rtx (old, NULL_RTX,
4357 loc_equivalence_callback, curr_insn);
4358 if (old != *curr_id->operand_loc[0])
4360 lra_update_insn_regno_info (curr_insn);
4361 changed_p = true;
4365 else if (INSN_P (curr_insn))
4367 if ((set = single_set (curr_insn)) != NULL_RTX)
4369 dest_reg = SET_DEST (set);
4370 /* The equivalence pseudo could be set up as SUBREG in a
4371 case when it is a call restore insn in a mode
4372 different from the pseudo mode. */
4373 if (GET_CODE (dest_reg) == SUBREG)
4374 dest_reg = SUBREG_REG (dest_reg);
4375 if ((REG_P (dest_reg)
4376 && (x = get_equiv (dest_reg)) != dest_reg
4377 /* Remove insns which set up a pseudo whose value
4378 can not be changed. Such insns might be not in
4379 init_insns because we don't update equiv data
4380 during insn transformations.
4382 As an example, let suppose that a pseudo got
4383 hard register and on the 1st pass was not
4384 changed to equivalent constant. We generate an
4385 additional insn setting up the pseudo because of
4386 secondary memory movement. Then the pseudo is
4387 spilled and we use the equiv constant. In this
4388 case we should remove the additional insn and
4389 this insn is not init_insns list. */
4390 && (! MEM_P (x) || MEM_READONLY_P (x)
4391 /* Check that this is actually an insn setting
4392 up the equivalence. */
4393 || in_list_p (curr_insn,
4394 ira_reg_equiv
4395 [REGNO (dest_reg)].init_insns)))
4396 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4397 && in_list_p (curr_insn,
4398 ira_reg_equiv
4399 [REGNO (SET_SRC (set))].init_insns)))
4401 /* This is equiv init insn of pseudo which did not get a
4402 hard register -- remove the insn. */
4403 if (lra_dump_file != NULL)
4405 fprintf (lra_dump_file,
4406 " Removing equiv init insn %i (freq=%d)\n",
4407 INSN_UID (curr_insn),
4408 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4409 dump_insn_slim (lra_dump_file, curr_insn);
4411 if (contains_reg_p (x, true, false))
4412 lra_risky_transformations_p = true;
4413 lra_set_insn_deleted (curr_insn);
4414 continue;
4417 curr_id = lra_get_insn_recog_data (curr_insn);
4418 curr_static_id = curr_id->insn_static_data;
4419 init_curr_insn_input_reloads ();
4420 init_curr_operand_mode ();
4421 if (curr_insn_transform (false))
4422 changed_p = true;
4423 /* Check non-transformed insns too for equiv change as USE
4424 or CLOBBER don't need reloads but can contain pseudos
4425 being changed on their equivalences. */
4426 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4427 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4429 lra_update_insn_regno_info (curr_insn);
4430 changed_p = true;
4434 bitmap_clear (&equiv_insn_bitmap);
4435 /* If we used a new hard regno, changed_p should be true because the
4436 hard reg is assigned to a new pseudo. */
4437 #ifdef ENABLE_CHECKING
4438 if (! changed_p)
4440 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4441 if (lra_reg_info[i].nrefs != 0
4442 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4444 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4446 for (j = 0; j < nregs; j++)
4447 lra_assert (df_regs_ever_live_p (hard_regno + j));
4450 #endif
4451 return changed_p;
4454 /* Initiate the LRA constraint pass. It is done once per
4455 function. */
4456 void
4457 lra_constraints_init (void)
4461 /* Finalize the LRA constraint pass. It is done once per
4462 function. */
4463 void
4464 lra_constraints_finish (void)
4470 /* This page contains code to do inheritance/split
4471 transformations. */
4473 /* Number of reloads passed so far in current EBB. */
4474 static int reloads_num;
4476 /* Number of calls passed so far in current EBB. */
4477 static int calls_num;
4479 /* Current reload pseudo check for validity of elements in
4480 USAGE_INSNS. */
4481 static int curr_usage_insns_check;
4483 /* Info about last usage of registers in EBB to do inheritance/split
4484 transformation. Inheritance transformation is done from a spilled
4485 pseudo and split transformations from a hard register or a pseudo
4486 assigned to a hard register. */
4487 struct usage_insns
4489 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4490 value INSNS is valid. The insns is chain of optional debug insns
4491 and a finishing non-debug insn using the corresponding reg. The
4492 value is also used to mark the registers which are set up in the
4493 current insn. The negated insn uid is used for this. */
4494 int check;
4495 /* Value of global reloads_num at the last insn in INSNS. */
4496 int reloads_num;
4497 /* Value of global reloads_nums at the last insn in INSNS. */
4498 int calls_num;
4499 /* It can be true only for splitting. And it means that the restore
4500 insn should be put after insn given by the following member. */
4501 bool after_p;
4502 /* Next insns in the current EBB which use the original reg and the
4503 original reg value is not changed between the current insn and
4504 the next insns. In order words, e.g. for inheritance, if we need
4505 to use the original reg value again in the next insns we can try
4506 to use the value in a hard register from a reload insn of the
4507 current insn. */
4508 rtx insns;
4511 /* Map: regno -> corresponding pseudo usage insns. */
4512 static struct usage_insns *usage_insns;
4514 static void
4515 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4517 usage_insns[regno].check = curr_usage_insns_check;
4518 usage_insns[regno].insns = insn;
4519 usage_insns[regno].reloads_num = reloads_num;
4520 usage_insns[regno].calls_num = calls_num;
4521 usage_insns[regno].after_p = after_p;
4524 /* The function is used to form list REGNO usages which consists of
4525 optional debug insns finished by a non-debug insn using REGNO.
4526 RELOADS_NUM is current number of reload insns processed so far. */
4527 static void
4528 add_next_usage_insn (int regno, rtx insn, int reloads_num)
4530 rtx next_usage_insns;
4532 if (usage_insns[regno].check == curr_usage_insns_check
4533 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4534 && DEBUG_INSN_P (insn))
4536 /* Check that we did not add the debug insn yet. */
4537 if (next_usage_insns != insn
4538 && (GET_CODE (next_usage_insns) != INSN_LIST
4539 || XEXP (next_usage_insns, 0) != insn))
4540 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4541 next_usage_insns);
4543 else if (NONDEBUG_INSN_P (insn))
4544 setup_next_usage_insn (regno, insn, reloads_num, false);
4545 else
4546 usage_insns[regno].check = 0;
4549 /* Return first non-debug insn in list USAGE_INSNS. */
4550 static rtx_insn *
4551 skip_usage_debug_insns (rtx usage_insns)
4553 rtx insn;
4555 /* Skip debug insns. */
4556 for (insn = usage_insns;
4557 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4558 insn = XEXP (insn, 1))
4560 return safe_as_a <rtx_insn *> (insn);
4563 /* Return true if we need secondary memory moves for insn in
4564 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4565 into the insn. */
4566 static bool
4567 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4568 rtx usage_insns ATTRIBUTE_UNUSED)
4570 #ifndef SECONDARY_MEMORY_NEEDED
4571 return false;
4572 #else
4573 rtx_insn *insn;
4574 rtx set, dest;
4575 enum reg_class cl;
4577 if (inher_cl == ALL_REGS
4578 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4579 return false;
4580 lra_assert (INSN_P (insn));
4581 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4582 return false;
4583 dest = SET_DEST (set);
4584 if (! REG_P (dest))
4585 return false;
4586 lra_assert (inher_cl != NO_REGS);
4587 cl = get_reg_class (REGNO (dest));
4588 return (cl != NO_REGS && cl != ALL_REGS
4589 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4590 #endif
4593 /* Registers involved in inheritance/split in the current EBB
4594 (inheritance/split pseudos and original registers). */
4595 static bitmap_head check_only_regs;
4597 /* Do inheritance transformations for insn INSN, which defines (if
4598 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4599 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4600 form as the "insns" field of usage_insns. Return true if we
4601 succeed in such transformation.
4603 The transformations look like:
4605 p <- ... i <- ...
4606 ... p <- i (new insn)
4607 ... =>
4608 <- ... p ... <- ... i ...
4610 ... i <- p (new insn)
4611 <- ... p ... <- ... i ...
4612 ... =>
4613 <- ... p ... <- ... i ...
4614 where p is a spilled original pseudo and i is a new inheritance pseudo.
4617 The inheritance pseudo has the smallest class of two classes CL and
4618 class of ORIGINAL REGNO. */
4619 static bool
4620 inherit_reload_reg (bool def_p, int original_regno,
4621 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4623 if (optimize_function_for_size_p (cfun))
4624 return false;
4626 enum reg_class rclass = lra_get_allocno_class (original_regno);
4627 rtx original_reg = regno_reg_rtx[original_regno];
4628 rtx new_reg, usage_insn;
4629 rtx_insn *new_insns;
4631 lra_assert (! usage_insns[original_regno].after_p);
4632 if (lra_dump_file != NULL)
4633 fprintf (lra_dump_file,
4634 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4635 if (! ira_reg_classes_intersect_p[cl][rclass])
4637 if (lra_dump_file != NULL)
4639 fprintf (lra_dump_file,
4640 " Rejecting inheritance for %d "
4641 "because of disjoint classes %s and %s\n",
4642 original_regno, reg_class_names[cl],
4643 reg_class_names[rclass]);
4644 fprintf (lra_dump_file,
4645 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4647 return false;
4649 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4650 /* We don't use a subset of two classes because it can be
4651 NO_REGS. This transformation is still profitable in most
4652 cases even if the classes are not intersected as register
4653 move is probably cheaper than a memory load. */
4654 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4656 if (lra_dump_file != NULL)
4657 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4658 reg_class_names[cl], reg_class_names[rclass]);
4660 rclass = cl;
4662 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4664 /* Reject inheritance resulting in secondary memory moves.
4665 Otherwise, there is a danger in LRA cycling. Also such
4666 transformation will be unprofitable. */
4667 if (lra_dump_file != NULL)
4669 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4670 rtx set = single_set (insn);
4672 lra_assert (set != NULL_RTX);
4674 rtx dest = SET_DEST (set);
4676 lra_assert (REG_P (dest));
4677 fprintf (lra_dump_file,
4678 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4679 "as secondary mem is needed\n",
4680 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4681 original_regno, reg_class_names[rclass]);
4682 fprintf (lra_dump_file,
4683 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4685 return false;
4687 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4688 rclass, "inheritance");
4689 start_sequence ();
4690 if (def_p)
4691 lra_emit_move (original_reg, new_reg);
4692 else
4693 lra_emit_move (new_reg, original_reg);
4694 new_insns = get_insns ();
4695 end_sequence ();
4696 if (NEXT_INSN (new_insns) != NULL_RTX)
4698 if (lra_dump_file != NULL)
4700 fprintf (lra_dump_file,
4701 " Rejecting inheritance %d->%d "
4702 "as it results in 2 or more insns:\n",
4703 original_regno, REGNO (new_reg));
4704 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4705 fprintf (lra_dump_file,
4706 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4708 return false;
4710 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
4711 lra_update_insn_regno_info (insn);
4712 if (! def_p)
4713 /* We now have a new usage insn for original regno. */
4714 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4715 if (lra_dump_file != NULL)
4716 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4717 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4718 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4719 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4720 bitmap_set_bit (&check_only_regs, original_regno);
4721 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4722 if (def_p)
4723 lra_process_new_insns (insn, NULL, new_insns,
4724 "Add original<-inheritance");
4725 else
4726 lra_process_new_insns (insn, new_insns, NULL,
4727 "Add inheritance<-original");
4728 while (next_usage_insns != NULL_RTX)
4730 if (GET_CODE (next_usage_insns) != INSN_LIST)
4732 usage_insn = next_usage_insns;
4733 lra_assert (NONDEBUG_INSN_P (usage_insn));
4734 next_usage_insns = NULL;
4736 else
4738 usage_insn = XEXP (next_usage_insns, 0);
4739 lra_assert (DEBUG_INSN_P (usage_insn));
4740 next_usage_insns = XEXP (next_usage_insns, 1);
4742 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
4743 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4744 if (lra_dump_file != NULL)
4746 fprintf (lra_dump_file,
4747 " Inheritance reuse change %d->%d (bb%d):\n",
4748 original_regno, REGNO (new_reg),
4749 BLOCK_FOR_INSN (usage_insn)->index);
4750 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
4753 if (lra_dump_file != NULL)
4754 fprintf (lra_dump_file,
4755 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4756 return true;
4759 /* Return true if we need a caller save/restore for pseudo REGNO which
4760 was assigned to a hard register. */
4761 static inline bool
4762 need_for_call_save_p (int regno)
4764 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4765 return (usage_insns[regno].calls_num < calls_num
4766 && (overlaps_hard_reg_set_p
4767 ((flag_ipa_ra &&
4768 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4769 ? lra_reg_info[regno].actual_call_used_reg_set
4770 : call_used_reg_set,
4771 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4772 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4773 PSEUDO_REGNO_MODE (regno))));
4776 /* Global registers occurring in the current EBB. */
4777 static bitmap_head ebb_global_regs;
4779 /* Return true if we need a split for hard register REGNO or pseudo
4780 REGNO which was assigned to a hard register.
4781 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4782 used for reloads since the EBB end. It is an approximation of the
4783 used hard registers in the split range. The exact value would
4784 require expensive calculations. If we were aggressive with
4785 splitting because of the approximation, the split pseudo will save
4786 the same hard register assignment and will be removed in the undo
4787 pass. We still need the approximation because too aggressive
4788 splitting would result in too inaccurate cost calculation in the
4789 assignment pass because of too many generated moves which will be
4790 probably removed in the undo pass. */
4791 static inline bool
4792 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4794 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4796 lra_assert (hard_regno >= 0);
4797 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4798 /* Don't split eliminable hard registers, otherwise we can
4799 split hard registers like hard frame pointer, which
4800 lives on BB start/end according to DF-infrastructure,
4801 when there is a pseudo assigned to the register and
4802 living in the same BB. */
4803 && (regno >= FIRST_PSEUDO_REGISTER
4804 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4805 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4806 /* Don't split call clobbered hard regs living through
4807 calls, otherwise we might have a check problem in the
4808 assign sub-pass as in the most cases (exception is a
4809 situation when lra_risky_transformations_p value is
4810 true) the assign pass assumes that all pseudos living
4811 through calls are assigned to call saved hard regs. */
4812 && (regno >= FIRST_PSEUDO_REGISTER
4813 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4814 || usage_insns[regno].calls_num == calls_num)
4815 /* We need at least 2 reloads to make pseudo splitting
4816 profitable. We should provide hard regno splitting in
4817 any case to solve 1st insn scheduling problem when
4818 moving hard register definition up might result in
4819 impossibility to find hard register for reload pseudo of
4820 small register class. */
4821 && (usage_insns[regno].reloads_num
4822 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4823 && (regno < FIRST_PSEUDO_REGISTER
4824 /* For short living pseudos, spilling + inheritance can
4825 be considered a substitution for splitting.
4826 Therefore we do not splitting for local pseudos. It
4827 decreases also aggressiveness of splitting. The
4828 minimal number of references is chosen taking into
4829 account that for 2 references splitting has no sense
4830 as we can just spill the pseudo. */
4831 || (regno >= FIRST_PSEUDO_REGISTER
4832 && lra_reg_info[regno].nrefs > 3
4833 && bitmap_bit_p (&ebb_global_regs, regno))))
4834 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4837 /* Return class for the split pseudo created from original pseudo with
4838 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4839 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4840 results in no secondary memory movements. */
4841 static enum reg_class
4842 choose_split_class (enum reg_class allocno_class,
4843 int hard_regno ATTRIBUTE_UNUSED,
4844 machine_mode mode ATTRIBUTE_UNUSED)
4846 #ifndef SECONDARY_MEMORY_NEEDED
4847 return allocno_class;
4848 #else
4849 int i;
4850 enum reg_class cl, best_cl = NO_REGS;
4851 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4852 = REGNO_REG_CLASS (hard_regno);
4854 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4855 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4856 return allocno_class;
4857 for (i = 0;
4858 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4859 i++)
4860 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4861 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4862 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4863 && (best_cl == NO_REGS
4864 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4865 best_cl = cl;
4866 return best_cl;
4867 #endif
4870 /* Do split transformations for insn INSN, which defines or uses
4871 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4872 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4873 "insns" field of usage_insns.
4875 The transformations look like:
4877 p <- ... p <- ...
4878 ... s <- p (new insn -- save)
4879 ... =>
4880 ... p <- s (new insn -- restore)
4881 <- ... p ... <- ... p ...
4883 <- ... p ... <- ... p ...
4884 ... s <- p (new insn -- save)
4885 ... =>
4886 ... p <- s (new insn -- restore)
4887 <- ... p ... <- ... p ...
4889 where p is an original pseudo got a hard register or a hard
4890 register and s is a new split pseudo. The save is put before INSN
4891 if BEFORE_P is true. Return true if we succeed in such
4892 transformation. */
4893 static bool
4894 split_reg (bool before_p, int original_regno, rtx_insn *insn,
4895 rtx next_usage_insns)
4897 enum reg_class rclass;
4898 rtx original_reg;
4899 int hard_regno, nregs;
4900 rtx new_reg, usage_insn;
4901 rtx_insn *restore, *save;
4902 bool after_p;
4903 bool call_save_p;
4905 if (original_regno < FIRST_PSEUDO_REGISTER)
4907 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4908 hard_regno = original_regno;
4909 call_save_p = false;
4910 nregs = 1;
4912 else
4914 hard_regno = reg_renumber[original_regno];
4915 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4916 rclass = lra_get_allocno_class (original_regno);
4917 original_reg = regno_reg_rtx[original_regno];
4918 call_save_p = need_for_call_save_p (original_regno);
4920 original_reg = regno_reg_rtx[original_regno];
4921 lra_assert (hard_regno >= 0);
4922 if (lra_dump_file != NULL)
4923 fprintf (lra_dump_file,
4924 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4925 if (call_save_p)
4927 machine_mode mode = GET_MODE (original_reg);
4929 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4930 hard_regno_nregs[hard_regno][mode],
4931 mode);
4932 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4934 else
4936 rclass = choose_split_class (rclass, hard_regno,
4937 GET_MODE (original_reg));
4938 if (rclass == NO_REGS)
4940 if (lra_dump_file != NULL)
4942 fprintf (lra_dump_file,
4943 " Rejecting split of %d(%s): "
4944 "no good reg class for %d(%s)\n",
4945 original_regno,
4946 reg_class_names[lra_get_allocno_class (original_regno)],
4947 hard_regno,
4948 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4949 fprintf
4950 (lra_dump_file,
4951 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4953 return false;
4955 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4956 rclass, "split");
4957 reg_renumber[REGNO (new_reg)] = hard_regno;
4959 save = emit_spill_move (true, new_reg, original_reg);
4960 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
4962 if (lra_dump_file != NULL)
4964 fprintf
4965 (lra_dump_file,
4966 " Rejecting split %d->%d resulting in > 2 save insns:\n",
4967 original_regno, REGNO (new_reg));
4968 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
4969 fprintf (lra_dump_file,
4970 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4972 return false;
4974 restore = emit_spill_move (false, new_reg, original_reg);
4975 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
4977 if (lra_dump_file != NULL)
4979 fprintf (lra_dump_file,
4980 " Rejecting split %d->%d "
4981 "resulting in > 2 restore insns:\n",
4982 original_regno, REGNO (new_reg));
4983 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
4984 fprintf (lra_dump_file,
4985 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4987 return false;
4989 after_p = usage_insns[original_regno].after_p;
4990 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4991 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4992 bitmap_set_bit (&check_only_regs, original_regno);
4993 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4994 for (;;)
4996 if (GET_CODE (next_usage_insns) != INSN_LIST)
4998 usage_insn = next_usage_insns;
4999 break;
5001 usage_insn = XEXP (next_usage_insns, 0);
5002 lra_assert (DEBUG_INSN_P (usage_insn));
5003 next_usage_insns = XEXP (next_usage_insns, 1);
5004 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5005 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5006 if (lra_dump_file != NULL)
5008 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5009 original_regno, REGNO (new_reg));
5010 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5013 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5014 lra_assert (usage_insn != insn || (after_p && before_p));
5015 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5016 after_p ? NULL : restore,
5017 after_p ? restore : NULL,
5018 call_save_p
5019 ? "Add reg<-save" : "Add reg<-split");
5020 lra_process_new_insns (insn, before_p ? save : NULL,
5021 before_p ? NULL : save,
5022 call_save_p
5023 ? "Add save<-reg" : "Add split<-reg");
5024 if (nregs > 1)
5025 /* If we are trying to split multi-register. We should check
5026 conflicts on the next assignment sub-pass. IRA can allocate on
5027 sub-register levels, LRA do this on pseudos level right now and
5028 this discrepancy may create allocation conflicts after
5029 splitting. */
5030 lra_risky_transformations_p = true;
5031 if (lra_dump_file != NULL)
5032 fprintf (lra_dump_file,
5033 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5034 return true;
5037 /* Recognize that we need a split transformation for insn INSN, which
5038 defines or uses REGNO in its insn biggest MODE (we use it only if
5039 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5040 hard registers which might be used for reloads since the EBB end.
5041 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5042 uid before starting INSN processing. Return true if we succeed in
5043 such transformation. */
5044 static bool
5045 split_if_necessary (int regno, machine_mode mode,
5046 HARD_REG_SET potential_reload_hard_regs,
5047 bool before_p, rtx_insn *insn, int max_uid)
5049 bool res = false;
5050 int i, nregs = 1;
5051 rtx next_usage_insns;
5053 if (regno < FIRST_PSEUDO_REGISTER)
5054 nregs = hard_regno_nregs[regno][mode];
5055 for (i = 0; i < nregs; i++)
5056 if (usage_insns[regno + i].check == curr_usage_insns_check
5057 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5058 /* To avoid processing the register twice or more. */
5059 && ((GET_CODE (next_usage_insns) != INSN_LIST
5060 && INSN_UID (next_usage_insns) < max_uid)
5061 || (GET_CODE (next_usage_insns) == INSN_LIST
5062 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5063 && need_for_split_p (potential_reload_hard_regs, regno + i)
5064 && split_reg (before_p, regno + i, insn, next_usage_insns))
5065 res = true;
5066 return res;
5069 /* Check only registers living at the current program point in the
5070 current EBB. */
5071 static bitmap_head live_regs;
5073 /* Update live info in EBB given by its HEAD and TAIL insns after
5074 inheritance/split transformation. The function removes dead moves
5075 too. */
5076 static void
5077 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5079 unsigned int j;
5080 int i, regno;
5081 bool live_p;
5082 rtx_insn *prev_insn;
5083 rtx set;
5084 bool remove_p;
5085 basic_block last_bb, prev_bb, curr_bb;
5086 bitmap_iterator bi;
5087 struct lra_insn_reg *reg;
5088 edge e;
5089 edge_iterator ei;
5091 last_bb = BLOCK_FOR_INSN (tail);
5092 prev_bb = NULL;
5093 for (curr_insn = tail;
5094 curr_insn != PREV_INSN (head);
5095 curr_insn = prev_insn)
5097 prev_insn = PREV_INSN (curr_insn);
5098 /* We need to process empty blocks too. They contain
5099 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5100 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5101 continue;
5102 curr_bb = BLOCK_FOR_INSN (curr_insn);
5103 if (curr_bb != prev_bb)
5105 if (prev_bb != NULL)
5107 /* Update df_get_live_in (prev_bb): */
5108 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5109 if (bitmap_bit_p (&live_regs, j))
5110 bitmap_set_bit (df_get_live_in (prev_bb), j);
5111 else
5112 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5114 if (curr_bb != last_bb)
5116 /* Update df_get_live_out (curr_bb): */
5117 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5119 live_p = bitmap_bit_p (&live_regs, j);
5120 if (! live_p)
5121 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5122 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5124 live_p = true;
5125 break;
5127 if (live_p)
5128 bitmap_set_bit (df_get_live_out (curr_bb), j);
5129 else
5130 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5133 prev_bb = curr_bb;
5134 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5136 if (! NONDEBUG_INSN_P (curr_insn))
5137 continue;
5138 curr_id = lra_get_insn_recog_data (curr_insn);
5139 curr_static_id = curr_id->insn_static_data;
5140 remove_p = false;
5141 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
5142 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5143 && bitmap_bit_p (&check_only_regs, regno)
5144 && ! bitmap_bit_p (&live_regs, regno))
5145 remove_p = true;
5146 /* See which defined values die here. */
5147 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5148 if (reg->type == OP_OUT && ! reg->subreg_p)
5149 bitmap_clear_bit (&live_regs, reg->regno);
5150 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5151 if (reg->type == OP_OUT && ! reg->subreg_p)
5152 bitmap_clear_bit (&live_regs, reg->regno);
5153 if (curr_id->arg_hard_regs != NULL)
5154 /* Make clobbered argument hard registers die. */
5155 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5156 if (regno >= FIRST_PSEUDO_REGISTER)
5157 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
5158 /* Mark each used value as live. */
5159 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5160 if (reg->type != OP_OUT
5161 && bitmap_bit_p (&check_only_regs, reg->regno))
5162 bitmap_set_bit (&live_regs, reg->regno);
5163 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5164 if (reg->type != OP_OUT
5165 && bitmap_bit_p (&check_only_regs, reg->regno))
5166 bitmap_set_bit (&live_regs, reg->regno);
5167 if (curr_id->arg_hard_regs != NULL)
5168 /* Make used argument hard registers live. */
5169 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5170 if (regno < FIRST_PSEUDO_REGISTER
5171 && bitmap_bit_p (&check_only_regs, regno))
5172 bitmap_set_bit (&live_regs, regno);
5173 /* It is quite important to remove dead move insns because it
5174 means removing dead store. We don't need to process them for
5175 constraints. */
5176 if (remove_p)
5178 if (lra_dump_file != NULL)
5180 fprintf (lra_dump_file, " Removing dead insn:\n ");
5181 dump_insn_slim (lra_dump_file, curr_insn);
5183 lra_set_insn_deleted (curr_insn);
5188 /* The structure describes info to do an inheritance for the current
5189 insn. We need to collect such info first before doing the
5190 transformations because the transformations change the insn
5191 internal representation. */
5192 struct to_inherit
5194 /* Original regno. */
5195 int regno;
5196 /* Subsequent insns which can inherit original reg value. */
5197 rtx insns;
5200 /* Array containing all info for doing inheritance from the current
5201 insn. */
5202 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5204 /* Number elements in the previous array. */
5205 static int to_inherit_num;
5207 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5208 structure to_inherit. */
5209 static void
5210 add_to_inherit (int regno, rtx insns)
5212 int i;
5214 for (i = 0; i < to_inherit_num; i++)
5215 if (to_inherit[i].regno == regno)
5216 return;
5217 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5218 to_inherit[to_inherit_num].regno = regno;
5219 to_inherit[to_inherit_num++].insns = insns;
5222 /* Return the last non-debug insn in basic block BB, or the block begin
5223 note if none. */
5224 static rtx_insn *
5225 get_last_insertion_point (basic_block bb)
5227 rtx_insn *insn;
5229 FOR_BB_INSNS_REVERSE (bb, insn)
5230 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5231 return insn;
5232 gcc_unreachable ();
5235 /* Set up RES by registers living on edges FROM except the edge (FROM,
5236 TO) or by registers set up in a jump insn in BB FROM. */
5237 static void
5238 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5240 rtx_insn *last;
5241 struct lra_insn_reg *reg;
5242 edge e;
5243 edge_iterator ei;
5245 lra_assert (to != NULL);
5246 bitmap_clear (res);
5247 FOR_EACH_EDGE (e, ei, from->succs)
5248 if (e->dest != to)
5249 bitmap_ior_into (res, df_get_live_in (e->dest));
5250 last = get_last_insertion_point (from);
5251 if (! JUMP_P (last))
5252 return;
5253 curr_id = lra_get_insn_recog_data (last);
5254 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5255 if (reg->type != OP_IN)
5256 bitmap_set_bit (res, reg->regno);
5259 /* Used as a temporary results of some bitmap calculations. */
5260 static bitmap_head temp_bitmap;
5262 /* We split for reloads of small class of hard regs. The following
5263 defines how many hard regs the class should have to be qualified as
5264 small. The code is mostly oriented to x86/x86-64 architecture
5265 where some insns need to use only specific register or pair of
5266 registers and these register can live in RTL explicitly, e.g. for
5267 parameter passing. */
5268 static const int max_small_class_regs_num = 2;
5270 /* Do inheritance/split transformations in EBB starting with HEAD and
5271 finishing on TAIL. We process EBB insns in the reverse order.
5272 Return true if we did any inheritance/split transformation in the
5273 EBB.
5275 We should avoid excessive splitting which results in worse code
5276 because of inaccurate cost calculations for spilling new split
5277 pseudos in such case. To achieve this we do splitting only if
5278 register pressure is high in given basic block and there are reload
5279 pseudos requiring hard registers. We could do more register
5280 pressure calculations at any given program point to avoid necessary
5281 splitting even more but it is to expensive and the current approach
5282 works well enough. */
5283 static bool
5284 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5286 int i, src_regno, dst_regno, nregs;
5287 bool change_p, succ_p, update_reloads_num_p;
5288 rtx_insn *prev_insn, *last_insn;
5289 rtx next_usage_insns, set;
5290 enum reg_class cl;
5291 struct lra_insn_reg *reg;
5292 basic_block last_processed_bb, curr_bb = NULL;
5293 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5294 bitmap to_process;
5295 unsigned int j;
5296 bitmap_iterator bi;
5297 bool head_p, after_p;
5299 change_p = false;
5300 curr_usage_insns_check++;
5301 reloads_num = calls_num = 0;
5302 bitmap_clear (&check_only_regs);
5303 last_processed_bb = NULL;
5304 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5305 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5306 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5307 /* We don't process new insns generated in the loop. */
5308 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5310 prev_insn = PREV_INSN (curr_insn);
5311 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5312 curr_bb = BLOCK_FOR_INSN (curr_insn);
5313 if (last_processed_bb != curr_bb)
5315 /* We are at the end of BB. Add qualified living
5316 pseudos for potential splitting. */
5317 to_process = df_get_live_out (curr_bb);
5318 if (last_processed_bb != NULL)
5320 /* We are somewhere in the middle of EBB. */
5321 get_live_on_other_edges (curr_bb, last_processed_bb,
5322 &temp_bitmap);
5323 to_process = &temp_bitmap;
5325 last_processed_bb = curr_bb;
5326 last_insn = get_last_insertion_point (curr_bb);
5327 after_p = (! JUMP_P (last_insn)
5328 && (! CALL_P (last_insn)
5329 || (find_reg_note (last_insn,
5330 REG_NORETURN, NULL_RTX) == NULL_RTX
5331 && ! SIBLING_CALL_P (last_insn))));
5332 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5333 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5335 if ((int) j >= lra_constraint_new_regno_start)
5336 break;
5337 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5339 if (j < FIRST_PSEUDO_REGISTER)
5340 SET_HARD_REG_BIT (live_hard_regs, j);
5341 else
5342 add_to_hard_reg_set (&live_hard_regs,
5343 PSEUDO_REGNO_MODE (j),
5344 reg_renumber[j]);
5345 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5349 src_regno = dst_regno = -1;
5350 if (NONDEBUG_INSN_P (curr_insn)
5351 && (set = single_set (curr_insn)) != NULL_RTX
5352 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5354 src_regno = REGNO (SET_SRC (set));
5355 dst_regno = REGNO (SET_DEST (set));
5357 update_reloads_num_p = true;
5358 if (src_regno < lra_constraint_new_regno_start
5359 && src_regno >= FIRST_PSEUDO_REGISTER
5360 && reg_renumber[src_regno] < 0
5361 && dst_regno >= lra_constraint_new_regno_start
5362 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5364 /* 'reload_pseudo <- original_pseudo'. */
5365 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5366 reloads_num++;
5367 update_reloads_num_p = false;
5368 succ_p = false;
5369 if (usage_insns[src_regno].check == curr_usage_insns_check
5370 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5371 succ_p = inherit_reload_reg (false, src_regno, cl,
5372 curr_insn, next_usage_insns);
5373 if (succ_p)
5374 change_p = true;
5375 else
5376 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5377 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5378 IOR_HARD_REG_SET (potential_reload_hard_regs,
5379 reg_class_contents[cl]);
5381 else if (src_regno >= lra_constraint_new_regno_start
5382 && dst_regno < lra_constraint_new_regno_start
5383 && dst_regno >= FIRST_PSEUDO_REGISTER
5384 && reg_renumber[dst_regno] < 0
5385 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5386 && usage_insns[dst_regno].check == curr_usage_insns_check
5387 && (next_usage_insns
5388 = usage_insns[dst_regno].insns) != NULL_RTX)
5390 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5391 reloads_num++;
5392 update_reloads_num_p = false;
5393 /* 'original_pseudo <- reload_pseudo'. */
5394 if (! JUMP_P (curr_insn)
5395 && inherit_reload_reg (true, dst_regno, cl,
5396 curr_insn, next_usage_insns))
5397 change_p = true;
5398 /* Invalidate. */
5399 usage_insns[dst_regno].check = 0;
5400 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5401 IOR_HARD_REG_SET (potential_reload_hard_regs,
5402 reg_class_contents[cl]);
5404 else if (INSN_P (curr_insn))
5406 int iter;
5407 int max_uid = get_max_uid ();
5409 curr_id = lra_get_insn_recog_data (curr_insn);
5410 curr_static_id = curr_id->insn_static_data;
5411 to_inherit_num = 0;
5412 /* Process insn definitions. */
5413 for (iter = 0; iter < 2; iter++)
5414 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5415 reg != NULL;
5416 reg = reg->next)
5417 if (reg->type != OP_IN
5418 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5420 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5421 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5422 && usage_insns[dst_regno].check == curr_usage_insns_check
5423 && (next_usage_insns
5424 = usage_insns[dst_regno].insns) != NULL_RTX)
5426 struct lra_insn_reg *r;
5428 for (r = curr_id->regs; r != NULL; r = r->next)
5429 if (r->type != OP_OUT && r->regno == dst_regno)
5430 break;
5431 /* Don't do inheritance if the pseudo is also
5432 used in the insn. */
5433 if (r == NULL)
5434 /* We can not do inheritance right now
5435 because the current insn reg info (chain
5436 regs) can change after that. */
5437 add_to_inherit (dst_regno, next_usage_insns);
5439 /* We can not process one reg twice here because of
5440 usage_insns invalidation. */
5441 if ((dst_regno < FIRST_PSEUDO_REGISTER
5442 || reg_renumber[dst_regno] >= 0)
5443 && ! reg->subreg_p && reg->type != OP_IN)
5445 HARD_REG_SET s;
5447 if (split_if_necessary (dst_regno, reg->biggest_mode,
5448 potential_reload_hard_regs,
5449 false, curr_insn, max_uid))
5450 change_p = true;
5451 CLEAR_HARD_REG_SET (s);
5452 if (dst_regno < FIRST_PSEUDO_REGISTER)
5453 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5454 else
5455 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5456 reg_renumber[dst_regno]);
5457 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5459 /* We should invalidate potential inheritance or
5460 splitting for the current insn usages to the next
5461 usage insns (see code below) as the output pseudo
5462 prevents this. */
5463 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5464 && reg_renumber[dst_regno] < 0)
5465 || (reg->type == OP_OUT && ! reg->subreg_p
5466 && (dst_regno < FIRST_PSEUDO_REGISTER
5467 || reg_renumber[dst_regno] >= 0)))
5469 /* Invalidate and mark definitions. */
5470 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5471 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5472 else
5474 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5475 for (i = 0; i < nregs; i++)
5476 usage_insns[dst_regno + i].check
5477 = -(int) INSN_UID (curr_insn);
5481 /* Process clobbered call regs. */
5482 if (curr_id->arg_hard_regs != NULL)
5483 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5484 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5485 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
5486 = -(int) INSN_UID (curr_insn);
5487 if (! JUMP_P (curr_insn))
5488 for (i = 0; i < to_inherit_num; i++)
5489 if (inherit_reload_reg (true, to_inherit[i].regno,
5490 ALL_REGS, curr_insn,
5491 to_inherit[i].insns))
5492 change_p = true;
5493 if (CALL_P (curr_insn))
5495 rtx cheap, pat, dest;
5496 rtx_insn *restore;
5497 int regno, hard_regno;
5499 calls_num++;
5500 if ((cheap = find_reg_note (curr_insn,
5501 REG_RETURNED, NULL_RTX)) != NULL_RTX
5502 && ((cheap = XEXP (cheap, 0)), true)
5503 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5504 && (hard_regno = reg_renumber[regno]) >= 0
5505 /* If there are pending saves/restores, the
5506 optimization is not worth. */
5507 && usage_insns[regno].calls_num == calls_num - 1
5508 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5510 /* Restore the pseudo from the call result as
5511 REG_RETURNED note says that the pseudo value is
5512 in the call result and the pseudo is an argument
5513 of the call. */
5514 pat = PATTERN (curr_insn);
5515 if (GET_CODE (pat) == PARALLEL)
5516 pat = XVECEXP (pat, 0, 0);
5517 dest = SET_DEST (pat);
5518 /* For multiple return values dest is PARALLEL.
5519 Currently we handle only single return value case. */
5520 if (REG_P (dest))
5522 start_sequence ();
5523 emit_move_insn (cheap, copy_rtx (dest));
5524 restore = get_insns ();
5525 end_sequence ();
5526 lra_process_new_insns (curr_insn, NULL, restore,
5527 "Inserting call parameter restore");
5528 /* We don't need to save/restore of the pseudo from
5529 this call. */
5530 usage_insns[regno].calls_num = calls_num;
5531 bitmap_set_bit (&check_only_regs, regno);
5535 to_inherit_num = 0;
5536 /* Process insn usages. */
5537 for (iter = 0; iter < 2; iter++)
5538 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5539 reg != NULL;
5540 reg = reg->next)
5541 if ((reg->type != OP_OUT
5542 || (reg->type == OP_OUT && reg->subreg_p))
5543 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5545 if (src_regno >= FIRST_PSEUDO_REGISTER
5546 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5548 if (usage_insns[src_regno].check == curr_usage_insns_check
5549 && (next_usage_insns
5550 = usage_insns[src_regno].insns) != NULL_RTX
5551 && NONDEBUG_INSN_P (curr_insn))
5552 add_to_inherit (src_regno, next_usage_insns);
5553 else if (usage_insns[src_regno].check
5554 != -(int) INSN_UID (curr_insn))
5555 /* Add usages but only if the reg is not set up
5556 in the same insn. */
5557 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5559 else if (src_regno < FIRST_PSEUDO_REGISTER
5560 || reg_renumber[src_regno] >= 0)
5562 bool before_p;
5563 rtx_insn *use_insn = curr_insn;
5565 before_p = (JUMP_P (curr_insn)
5566 || (CALL_P (curr_insn) && reg->type == OP_IN));
5567 if (NONDEBUG_INSN_P (curr_insn)
5568 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5569 && split_if_necessary (src_regno, reg->biggest_mode,
5570 potential_reload_hard_regs,
5571 before_p, curr_insn, max_uid))
5573 if (reg->subreg_p)
5574 lra_risky_transformations_p = true;
5575 change_p = true;
5576 /* Invalidate. */
5577 usage_insns[src_regno].check = 0;
5578 if (before_p)
5579 use_insn = PREV_INSN (curr_insn);
5581 if (NONDEBUG_INSN_P (curr_insn))
5583 if (src_regno < FIRST_PSEUDO_REGISTER)
5584 add_to_hard_reg_set (&live_hard_regs,
5585 reg->biggest_mode, src_regno);
5586 else
5587 add_to_hard_reg_set (&live_hard_regs,
5588 PSEUDO_REGNO_MODE (src_regno),
5589 reg_renumber[src_regno]);
5591 add_next_usage_insn (src_regno, use_insn, reloads_num);
5594 /* Process used call regs. */
5595 if (curr_id->arg_hard_regs != NULL)
5596 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5597 if (src_regno < FIRST_PSEUDO_REGISTER)
5599 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5600 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5602 for (i = 0; i < to_inherit_num; i++)
5604 src_regno = to_inherit[i].regno;
5605 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5606 curr_insn, to_inherit[i].insns))
5607 change_p = true;
5608 else
5609 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5612 if (update_reloads_num_p
5613 && NONDEBUG_INSN_P (curr_insn)
5614 && (set = single_set (curr_insn)) != NULL_RTX)
5616 int regno = -1;
5617 if ((REG_P (SET_DEST (set))
5618 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5619 && reg_renumber[regno] < 0
5620 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5621 || (REG_P (SET_SRC (set))
5622 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5623 && reg_renumber[regno] < 0
5624 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5626 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5627 reloads_num++;
5628 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5629 IOR_HARD_REG_SET (potential_reload_hard_regs,
5630 reg_class_contents[cl]);
5633 /* We reached the start of the current basic block. */
5634 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5635 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5637 /* We reached the beginning of the current block -- do
5638 rest of spliting in the current BB. */
5639 to_process = df_get_live_in (curr_bb);
5640 if (BLOCK_FOR_INSN (head) != curr_bb)
5642 /* We are somewhere in the middle of EBB. */
5643 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5644 curr_bb, &temp_bitmap);
5645 to_process = &temp_bitmap;
5647 head_p = true;
5648 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5650 if ((int) j >= lra_constraint_new_regno_start)
5651 break;
5652 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5653 && usage_insns[j].check == curr_usage_insns_check
5654 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5656 if (need_for_split_p (potential_reload_hard_regs, j))
5658 if (lra_dump_file != NULL && head_p)
5660 fprintf (lra_dump_file,
5661 " ----------------------------------\n");
5662 head_p = false;
5664 if (split_reg (false, j, bb_note (curr_bb),
5665 next_usage_insns))
5666 change_p = true;
5668 usage_insns[j].check = 0;
5673 return change_p;
5676 /* This value affects EBB forming. If probability of edge from EBB to
5677 a BB is not greater than the following value, we don't add the BB
5678 to EBB. */
5679 #define EBB_PROBABILITY_CUTOFF \
5680 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
5682 /* Current number of inheritance/split iteration. */
5683 int lra_inheritance_iter;
5685 /* Entry function for inheritance/split pass. */
5686 void
5687 lra_inheritance (void)
5689 int i;
5690 basic_block bb, start_bb;
5691 edge e;
5693 lra_inheritance_iter++;
5694 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5695 return;
5696 timevar_push (TV_LRA_INHERITANCE);
5697 if (lra_dump_file != NULL)
5698 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5699 lra_inheritance_iter);
5700 curr_usage_insns_check = 0;
5701 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5702 for (i = 0; i < lra_constraint_new_regno_start; i++)
5703 usage_insns[i].check = 0;
5704 bitmap_initialize (&check_only_regs, &reg_obstack);
5705 bitmap_initialize (&live_regs, &reg_obstack);
5706 bitmap_initialize (&temp_bitmap, &reg_obstack);
5707 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5708 FOR_EACH_BB_FN (bb, cfun)
5710 start_bb = bb;
5711 if (lra_dump_file != NULL)
5712 fprintf (lra_dump_file, "EBB");
5713 /* Form a EBB starting with BB. */
5714 bitmap_clear (&ebb_global_regs);
5715 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5716 for (;;)
5718 if (lra_dump_file != NULL)
5719 fprintf (lra_dump_file, " %d", bb->index);
5720 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5721 || LABEL_P (BB_HEAD (bb->next_bb)))
5722 break;
5723 e = find_fallthru_edge (bb->succs);
5724 if (! e)
5725 break;
5726 if (e->probability < EBB_PROBABILITY_CUTOFF)
5727 break;
5728 bb = bb->next_bb;
5730 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5731 if (lra_dump_file != NULL)
5732 fprintf (lra_dump_file, "\n");
5733 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5734 /* Remember that the EBB head and tail can change in
5735 inherit_in_ebb. */
5736 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5738 bitmap_clear (&ebb_global_regs);
5739 bitmap_clear (&temp_bitmap);
5740 bitmap_clear (&live_regs);
5741 bitmap_clear (&check_only_regs);
5742 free (usage_insns);
5744 timevar_pop (TV_LRA_INHERITANCE);
5749 /* This page contains code to undo failed inheritance/split
5750 transformations. */
5752 /* Current number of iteration undoing inheritance/split. */
5753 int lra_undo_inheritance_iter;
5755 /* Fix BB live info LIVE after removing pseudos created on pass doing
5756 inheritance/split which are REMOVED_PSEUDOS. */
5757 static void
5758 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5760 unsigned int regno;
5761 bitmap_iterator bi;
5763 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5764 if (bitmap_clear_bit (live, regno))
5765 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5768 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5769 number. */
5770 static int
5771 get_regno (rtx reg)
5773 if (GET_CODE (reg) == SUBREG)
5774 reg = SUBREG_REG (reg);
5775 if (REG_P (reg))
5776 return REGNO (reg);
5777 return -1;
5780 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5781 return true if we did any change. The undo transformations for
5782 inheritance looks like
5783 i <- i2
5784 p <- i => p <- i2
5785 or removing
5786 p <- i, i <- p, and i <- i3
5787 where p is original pseudo from which inheritance pseudo i was
5788 created, i and i3 are removed inheritance pseudos, i2 is another
5789 not removed inheritance pseudo. All split pseudos or other
5790 occurrences of removed inheritance pseudos are changed on the
5791 corresponding original pseudos.
5793 The function also schedules insns changed and created during
5794 inheritance/split pass for processing by the subsequent constraint
5795 pass. */
5796 static bool
5797 remove_inheritance_pseudos (bitmap remove_pseudos)
5799 basic_block bb;
5800 int regno, sregno, prev_sregno, dregno, restore_regno;
5801 rtx set, prev_set;
5802 rtx_insn *prev_insn;
5803 bool change_p, done_p;
5805 change_p = ! bitmap_empty_p (remove_pseudos);
5806 /* We can not finish the function right away if CHANGE_P is true
5807 because we need to marks insns affected by previous
5808 inheritance/split pass for processing by the subsequent
5809 constraint pass. */
5810 FOR_EACH_BB_FN (bb, cfun)
5812 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5813 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5814 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5816 if (! INSN_P (curr_insn))
5817 continue;
5818 done_p = false;
5819 sregno = dregno = -1;
5820 if (change_p && NONDEBUG_INSN_P (curr_insn)
5821 && (set = single_set (curr_insn)) != NULL_RTX)
5823 dregno = get_regno (SET_DEST (set));
5824 sregno = get_regno (SET_SRC (set));
5827 if (sregno >= 0 && dregno >= 0)
5829 if ((bitmap_bit_p (remove_pseudos, sregno)
5830 && (lra_reg_info[sregno].restore_regno == dregno
5831 || (bitmap_bit_p (remove_pseudos, dregno)
5832 && (lra_reg_info[sregno].restore_regno
5833 == lra_reg_info[dregno].restore_regno))))
5834 || (bitmap_bit_p (remove_pseudos, dregno)
5835 && lra_reg_info[dregno].restore_regno == sregno))
5836 /* One of the following cases:
5837 original <- removed inheritance pseudo
5838 removed inherit pseudo <- another removed inherit pseudo
5839 removed inherit pseudo <- original pseudo
5841 removed_split_pseudo <- original_reg
5842 original_reg <- removed_split_pseudo */
5844 if (lra_dump_file != NULL)
5846 fprintf (lra_dump_file, " Removing %s:\n",
5847 bitmap_bit_p (&lra_split_regs, sregno)
5848 || bitmap_bit_p (&lra_split_regs, dregno)
5849 ? "split" : "inheritance");
5850 dump_insn_slim (lra_dump_file, curr_insn);
5852 lra_set_insn_deleted (curr_insn);
5853 done_p = true;
5855 else if (bitmap_bit_p (remove_pseudos, sregno)
5856 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5858 /* Search the following pattern:
5859 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5860 original_pseudo <- inherit_or_split_pseudo1
5861 where the 2nd insn is the current insn and
5862 inherit_or_split_pseudo2 is not removed. If it is found,
5863 change the current insn onto:
5864 original_pseudo <- inherit_or_split_pseudo2. */
5865 for (prev_insn = PREV_INSN (curr_insn);
5866 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5867 prev_insn = PREV_INSN (prev_insn))
5869 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5870 && (prev_set = single_set (prev_insn)) != NULL_RTX
5871 /* There should be no subregs in insn we are
5872 searching because only the original reg might
5873 be in subreg when we changed the mode of
5874 load/store for splitting. */
5875 && REG_P (SET_DEST (prev_set))
5876 && REG_P (SET_SRC (prev_set))
5877 && (int) REGNO (SET_DEST (prev_set)) == sregno
5878 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5879 >= FIRST_PSEUDO_REGISTER)
5880 /* As we consider chain of inheritance or
5881 splitting described in above comment we should
5882 check that sregno and prev_sregno were
5883 inheritance/split pseudos created from the
5884 same original regno. */
5885 && (lra_reg_info[sregno].restore_regno
5886 == lra_reg_info[prev_sregno].restore_regno)
5887 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5889 lra_assert (GET_MODE (SET_SRC (prev_set))
5890 == GET_MODE (regno_reg_rtx[sregno]));
5891 if (GET_CODE (SET_SRC (set)) == SUBREG)
5892 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5893 else
5894 SET_SRC (set) = SET_SRC (prev_set);
5895 /* As we are finishing with processing the insn
5896 here, check the destination too as it might
5897 inheritance pseudo for another pseudo. */
5898 if (bitmap_bit_p (remove_pseudos, dregno)
5899 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
5900 && (restore_regno
5901 = lra_reg_info[dregno].restore_regno) >= 0)
5903 if (GET_CODE (SET_DEST (set)) == SUBREG)
5904 SUBREG_REG (SET_DEST (set))
5905 = regno_reg_rtx[restore_regno];
5906 else
5907 SET_DEST (set) = regno_reg_rtx[restore_regno];
5909 lra_push_insn_and_update_insn_regno_info (curr_insn);
5910 lra_set_used_insn_alternative_by_uid
5911 (INSN_UID (curr_insn), -1);
5912 done_p = true;
5913 if (lra_dump_file != NULL)
5915 fprintf (lra_dump_file, " Change reload insn:\n");
5916 dump_insn_slim (lra_dump_file, curr_insn);
5921 if (! done_p)
5923 struct lra_insn_reg *reg;
5924 bool restored_regs_p = false;
5925 bool kept_regs_p = false;
5927 curr_id = lra_get_insn_recog_data (curr_insn);
5928 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5930 regno = reg->regno;
5931 restore_regno = lra_reg_info[regno].restore_regno;
5932 if (restore_regno >= 0)
5934 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5936 lra_substitute_pseudo_within_insn
5937 (curr_insn, regno, regno_reg_rtx[restore_regno],
5938 false);
5939 restored_regs_p = true;
5941 else
5942 kept_regs_p = true;
5945 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5947 /* The instruction has changed since the previous
5948 constraints pass. */
5949 lra_push_insn_and_update_insn_regno_info (curr_insn);
5950 lra_set_used_insn_alternative_by_uid
5951 (INSN_UID (curr_insn), -1);
5953 else if (restored_regs_p)
5954 /* The instruction has been restored to the form that
5955 it had during the previous constraints pass. */
5956 lra_update_insn_regno_info (curr_insn);
5957 if (restored_regs_p && lra_dump_file != NULL)
5959 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5960 dump_insn_slim (lra_dump_file, curr_insn);
5965 return change_p;
5968 /* If optional reload pseudos failed to get a hard register or was not
5969 inherited, it is better to remove optional reloads. We do this
5970 transformation after undoing inheritance to figure out necessity to
5971 remove optional reloads easier. Return true if we do any
5972 change. */
5973 static bool
5974 undo_optional_reloads (void)
5976 bool change_p, keep_p;
5977 unsigned int regno, uid;
5978 bitmap_iterator bi, bi2;
5979 rtx_insn *insn;
5980 rtx set, src, dest;
5981 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5983 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5984 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5985 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5987 keep_p = false;
5988 /* Keep optional reloads from previous subpasses. */
5989 if (lra_reg_info[regno].restore_regno < 0
5990 /* If the original pseudo changed its allocation, just
5991 removing the optional pseudo is dangerous as the original
5992 pseudo will have longer live range. */
5993 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
5994 keep_p = true;
5995 else if (reg_renumber[regno] >= 0)
5996 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
5998 insn = lra_insn_recog_data[uid]->insn;
5999 if ((set = single_set (insn)) == NULL_RTX)
6000 continue;
6001 src = SET_SRC (set);
6002 dest = SET_DEST (set);
6003 if (! REG_P (src) || ! REG_P (dest))
6004 continue;
6005 if (REGNO (dest) == regno
6006 /* Ignore insn for optional reloads itself. */
6007 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
6008 /* Check only inheritance on last inheritance pass. */
6009 && (int) REGNO (src) >= new_regno_start
6010 /* Check that the optional reload was inherited. */
6011 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6013 keep_p = true;
6014 break;
6017 if (keep_p)
6019 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6020 if (lra_dump_file != NULL)
6021 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6024 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6025 bitmap_initialize (&insn_bitmap, &reg_obstack);
6026 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6028 if (lra_dump_file != NULL)
6029 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6030 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6031 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6033 insn = lra_insn_recog_data[uid]->insn;
6034 if ((set = single_set (insn)) != NULL_RTX)
6036 src = SET_SRC (set);
6037 dest = SET_DEST (set);
6038 if (REG_P (src) && REG_P (dest)
6039 && ((REGNO (src) == regno
6040 && (lra_reg_info[regno].restore_regno
6041 == (int) REGNO (dest)))
6042 || (REGNO (dest) == regno
6043 && (lra_reg_info[regno].restore_regno
6044 == (int) REGNO (src)))))
6046 if (lra_dump_file != NULL)
6048 fprintf (lra_dump_file, " Deleting move %u\n",
6049 INSN_UID (insn));
6050 dump_insn_slim (lra_dump_file, insn);
6052 lra_set_insn_deleted (insn);
6053 continue;
6055 /* We should not worry about generation memory-memory
6056 moves here as if the corresponding inheritance did
6057 not work (inheritance pseudo did not get a hard reg),
6058 we remove the inheritance pseudo and the optional
6059 reload. */
6061 lra_substitute_pseudo_within_insn
6062 (insn, regno, regno_reg_rtx[lra_reg_info[regno].restore_regno],
6063 false);
6064 lra_update_insn_regno_info (insn);
6065 if (lra_dump_file != NULL)
6067 fprintf (lra_dump_file,
6068 " Restoring original insn:\n");
6069 dump_insn_slim (lra_dump_file, insn);
6073 /* Clear restore_regnos. */
6074 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6075 lra_reg_info[regno].restore_regno = -1;
6076 bitmap_clear (&insn_bitmap);
6077 bitmap_clear (&removed_optional_reload_pseudos);
6078 return change_p;
6081 /* Entry function for undoing inheritance/split transformation. Return true
6082 if we did any RTL change in this pass. */
6083 bool
6084 lra_undo_inheritance (void)
6086 unsigned int regno;
6087 int restore_regno, hard_regno;
6088 int n_all_inherit, n_inherit, n_all_split, n_split;
6089 bitmap_head remove_pseudos;
6090 bitmap_iterator bi;
6091 bool change_p;
6093 lra_undo_inheritance_iter++;
6094 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6095 return false;
6096 if (lra_dump_file != NULL)
6097 fprintf (lra_dump_file,
6098 "\n********** Undoing inheritance #%d: **********\n\n",
6099 lra_undo_inheritance_iter);
6100 bitmap_initialize (&remove_pseudos, &reg_obstack);
6101 n_inherit = n_all_inherit = 0;
6102 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6103 if (lra_reg_info[regno].restore_regno >= 0)
6105 n_all_inherit++;
6106 if (reg_renumber[regno] < 0
6107 /* If the original pseudo changed its allocation, just
6108 removing inheritance is dangerous as for changing
6109 allocation we used shorter live-ranges. */
6110 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6111 bitmap_set_bit (&remove_pseudos, regno);
6112 else
6113 n_inherit++;
6115 if (lra_dump_file != NULL && n_all_inherit != 0)
6116 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6117 n_inherit, n_all_inherit,
6118 (double) n_inherit / n_all_inherit * 100);
6119 n_split = n_all_split = 0;
6120 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6121 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6123 n_all_split++;
6124 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6125 ? reg_renumber[restore_regno] : restore_regno);
6126 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6127 bitmap_set_bit (&remove_pseudos, regno);
6128 else
6130 n_split++;
6131 if (lra_dump_file != NULL)
6132 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6133 regno, restore_regno);
6136 if (lra_dump_file != NULL && n_all_split != 0)
6137 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6138 n_split, n_all_split,
6139 (double) n_split / n_all_split * 100);
6140 change_p = remove_inheritance_pseudos (&remove_pseudos);
6141 bitmap_clear (&remove_pseudos);
6142 /* Clear restore_regnos. */
6143 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6144 lra_reg_info[regno].restore_regno = -1;
6145 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6146 lra_reg_info[regno].restore_regno = -1;
6147 change_p = undo_optional_reloads () || change_p;
6148 return change_p;