2016-11-09 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / lra-constraints.c
blobb592168f00104a5b9649e3eb47e4544e78a59f8e
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2016 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains code for 3 passes: constraint pass,
23 inheritance/split pass, and pass for undoing failed inheritance and
24 split.
26 The major goal of constraint pass is to transform RTL to satisfy
27 insn and address constraints by:
28 o choosing insn alternatives;
29 o generating *reload insns* (or reloads in brief) and *reload
30 pseudos* which will get necessary hard registers later;
31 o substituting pseudos with equivalent values and removing the
32 instructions that initialized those pseudos.
34 The constraint pass has biggest and most complicated code in LRA.
35 There are a lot of important details like:
36 o reuse of input reload pseudos to simplify reload pseudo
37 allocations;
38 o some heuristics to choose insn alternative to improve the
39 inheritance;
40 o early clobbers etc.
42 The pass is mimicking former reload pass in alternative choosing
43 because the reload pass is oriented to current machine description
44 model. It might be changed if the machine description model is
45 changed.
47 There is special code for preventing all LRA and this pass cycling
48 in case of bugs.
50 On the first iteration of the pass we process every instruction and
51 choose an alternative for each one. On subsequent iterations we try
52 to avoid reprocessing instructions if we can be sure that the old
53 choice is still valid.
55 The inheritance/spilt pass is to transform code to achieve
56 ineheritance and live range splitting. It is done on backward
57 traversal of EBBs.
59 The inheritance optimization goal is to reuse values in hard
60 registers. There is analogous optimization in old reload pass. The
61 inheritance is achieved by following transformation:
63 reload_p1 <- p reload_p1 <- p
64 ... new_p <- reload_p1
65 ... => ...
66 reload_p2 <- p reload_p2 <- new_p
68 where p is spilled and not changed between the insns. Reload_p1 is
69 also called *original pseudo* and new_p is called *inheritance
70 pseudo*.
72 The subsequent assignment pass will try to assign the same (or
73 another if it is not possible) hard register to new_p as to
74 reload_p1 or reload_p2.
76 If the assignment pass fails to assign a hard register to new_p,
77 this file will undo the inheritance and restore the original code.
78 This is because implementing the above sequence with a spilled
79 new_p would make the code much worse. The inheritance is done in
80 EBB scope. The above is just a simplified example to get an idea
81 of the inheritance as the inheritance is also done for non-reload
82 insns.
84 Splitting (transformation) is also done in EBB scope on the same
85 pass as the inheritance:
87 r <- ... or ... <- r r <- ... or ... <- r
88 ... s <- r (new insn -- save)
89 ... =>
90 ... r <- s (new insn -- restore)
91 ... <- r ... <- r
93 The *split pseudo* s is assigned to the hard register of the
94 original pseudo or hard register r.
96 Splitting is done:
97 o In EBBs with high register pressure for global pseudos (living
98 in at least 2 BBs) and assigned to hard registers when there
99 are more one reloads needing the hard registers;
100 o for pseudos needing save/restore code around calls.
102 If the split pseudo still has the same hard register as the
103 original pseudo after the subsequent assignment pass or the
104 original pseudo was split, the opposite transformation is done on
105 the same pass for undoing inheritance. */
107 #undef REG_OK_STRICT
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "backend.h"
113 #include "target.h"
114 #include "rtl.h"
115 #include "tree.h"
116 #include "predict.h"
117 #include "df.h"
118 #include "memmodel.h"
119 #include "tm_p.h"
120 #include "expmed.h"
121 #include "optabs.h"
122 #include "regs.h"
123 #include "ira.h"
124 #include "recog.h"
125 #include "output.h"
126 #include "addresses.h"
127 #include "expr.h"
128 #include "cfgrtl.h"
129 #include "rtl-error.h"
130 #include "params.h"
131 #include "lra.h"
132 #include "lra-int.h"
133 #include "print-rtl.h"
135 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
136 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
137 reload insns. */
138 static int bb_reload_num;
140 /* The current insn being processed and corresponding its single set
141 (NULL otherwise), its data (basic block, the insn data, the insn
142 static data, and the mode of each operand). */
143 static rtx_insn *curr_insn;
144 static rtx curr_insn_set;
145 static basic_block curr_bb;
146 static lra_insn_recog_data_t curr_id;
147 static struct lra_static_insn_data *curr_static_id;
148 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
149 /* Mode of the register substituted by its equivalence with VOIDmode
150 (e.g. constant) and whose subreg is given operand of the current
151 insn. VOIDmode in all other cases. */
152 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
156 /* Start numbers for new registers and insns at the current constraints
157 pass start. */
158 static int new_regno_start;
159 static int new_insn_uid_start;
161 /* If LOC is nonnull, strip any outer subreg from it. */
162 static inline rtx *
163 strip_subreg (rtx *loc)
165 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
168 /* Return hard regno of REGNO or if it is was not assigned to a hard
169 register, use a hard register from its allocno class. */
170 static int
171 get_try_hard_regno (int regno)
173 int hard_regno;
174 enum reg_class rclass;
176 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
177 hard_regno = lra_get_regno_hard_regno (regno);
178 if (hard_regno >= 0)
179 return hard_regno;
180 rclass = lra_get_allocno_class (regno);
181 if (rclass == NO_REGS)
182 return -1;
183 return ira_class_hard_regs[rclass][0];
186 /* Return the hard regno of X after removing its subreg. If X is not
187 a register or a subreg of a register, return -1. If X is a pseudo,
188 use its assignment. If FINAL_P return the final hard regno which will
189 be after elimination. */
190 static int
191 get_hard_regno (rtx x, bool final_p)
193 rtx reg;
194 int hard_regno;
196 reg = x;
197 if (SUBREG_P (x))
198 reg = SUBREG_REG (x);
199 if (! REG_P (reg))
200 return -1;
201 if (! HARD_REGISTER_NUM_P (hard_regno = REGNO (reg)))
202 hard_regno = lra_get_regno_hard_regno (hard_regno);
203 if (hard_regno < 0)
204 return -1;
205 if (final_p)
206 hard_regno = lra_get_elimination_hard_regno (hard_regno);
207 if (SUBREG_P (x))
208 hard_regno += subreg_regno_offset (hard_regno, GET_MODE (reg),
209 SUBREG_BYTE (x), GET_MODE (x));
210 return hard_regno;
213 /* If REGNO is a hard register or has been allocated a hard register,
214 return the class of that register. If REGNO is a reload pseudo
215 created by the current constraints pass, return its allocno class.
216 Return NO_REGS otherwise. */
217 static enum reg_class
218 get_reg_class (int regno)
220 int hard_regno;
222 if (! HARD_REGISTER_NUM_P (hard_regno = regno))
223 hard_regno = lra_get_regno_hard_regno (regno);
224 if (hard_regno >= 0)
226 hard_regno = lra_get_elimination_hard_regno (hard_regno);
227 return REGNO_REG_CLASS (hard_regno);
229 if (regno >= new_regno_start)
230 return lra_get_allocno_class (regno);
231 return NO_REGS;
234 /* Return true if REG satisfies (or will satisfy) reg class constraint
235 CL. Use elimination first if REG is a hard register. If REG is a
236 reload pseudo created by this constraints pass, assume that it will
237 be allocated a hard register from its allocno class, but allow that
238 class to be narrowed to CL if it is currently a superset of CL.
240 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
241 REGNO (reg), or NO_REGS if no change in its class was needed. */
242 static bool
243 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
245 enum reg_class rclass, common_class;
246 machine_mode reg_mode;
247 int class_size, hard_regno, nregs, i, j;
248 int regno = REGNO (reg);
250 if (new_class != NULL)
251 *new_class = NO_REGS;
252 if (regno < FIRST_PSEUDO_REGISTER)
254 rtx final_reg = reg;
255 rtx *final_loc = &final_reg;
257 lra_eliminate_reg_if_possible (final_loc);
258 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
260 reg_mode = GET_MODE (reg);
261 rclass = get_reg_class (regno);
262 if (regno < new_regno_start
263 /* Do not allow the constraints for reload instructions to
264 influence the classes of new pseudos. These reloads are
265 typically moves that have many alternatives, and restricting
266 reload pseudos for one alternative may lead to situations
267 where other reload pseudos are no longer allocatable. */
268 || (INSN_UID (curr_insn) >= new_insn_uid_start
269 && curr_insn_set != NULL
270 && ((OBJECT_P (SET_SRC (curr_insn_set))
271 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
272 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
273 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
274 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
275 /* When we don't know what class will be used finally for reload
276 pseudos, we use ALL_REGS. */
277 return ((regno >= new_regno_start && rclass == ALL_REGS)
278 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
279 && ! hard_reg_set_subset_p (reg_class_contents[cl],
280 lra_no_alloc_regs)));
281 else
283 common_class = ira_reg_class_subset[rclass][cl];
284 if (new_class != NULL)
285 *new_class = common_class;
286 if (hard_reg_set_subset_p (reg_class_contents[common_class],
287 lra_no_alloc_regs))
288 return false;
289 /* Check that there are enough allocatable regs. */
290 class_size = ira_class_hard_regs_num[common_class];
291 for (i = 0; i < class_size; i++)
293 hard_regno = ira_class_hard_regs[common_class][i];
294 nregs = hard_regno_nregs[hard_regno][reg_mode];
295 if (nregs == 1)
296 return true;
297 for (j = 0; j < nregs; j++)
298 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
299 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
300 hard_regno + j))
301 break;
302 if (j >= nregs)
303 return true;
305 return false;
309 /* Return true if REGNO satisfies a memory constraint. */
310 static bool
311 in_mem_p (int regno)
313 return get_reg_class (regno) == NO_REGS;
316 /* Return 1 if ADDR is a valid memory address for mode MODE in address
317 space AS, and check that each pseudo has the proper kind of hard
318 reg. */
319 static int
320 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
321 rtx addr, addr_space_t as)
323 #ifdef GO_IF_LEGITIMATE_ADDRESS
324 lra_assert (ADDR_SPACE_GENERIC_P (as));
325 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
326 return 0;
328 win:
329 return 1;
330 #else
331 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
332 #endif
335 namespace {
336 /* Temporarily eliminates registers in an address (for the lifetime of
337 the object). */
338 class address_eliminator {
339 public:
340 address_eliminator (struct address_info *ad);
341 ~address_eliminator ();
343 private:
344 struct address_info *m_ad;
345 rtx *m_base_loc;
346 rtx m_base_reg;
347 rtx *m_index_loc;
348 rtx m_index_reg;
352 address_eliminator::address_eliminator (struct address_info *ad)
353 : m_ad (ad),
354 m_base_loc (strip_subreg (ad->base_term)),
355 m_base_reg (NULL_RTX),
356 m_index_loc (strip_subreg (ad->index_term)),
357 m_index_reg (NULL_RTX)
359 if (m_base_loc != NULL)
361 m_base_reg = *m_base_loc;
362 lra_eliminate_reg_if_possible (m_base_loc);
363 if (m_ad->base_term2 != NULL)
364 *m_ad->base_term2 = *m_ad->base_term;
366 if (m_index_loc != NULL)
368 m_index_reg = *m_index_loc;
369 lra_eliminate_reg_if_possible (m_index_loc);
373 address_eliminator::~address_eliminator ()
375 if (m_base_loc && *m_base_loc != m_base_reg)
377 *m_base_loc = m_base_reg;
378 if (m_ad->base_term2 != NULL)
379 *m_ad->base_term2 = *m_ad->base_term;
381 if (m_index_loc && *m_index_loc != m_index_reg)
382 *m_index_loc = m_index_reg;
385 /* Return true if the eliminated form of AD is a legitimate target address. */
386 static bool
387 valid_address_p (struct address_info *ad)
389 address_eliminator eliminator (ad);
390 return valid_address_p (ad->mode, *ad->outer, ad->as);
393 /* Return true if the eliminated form of memory reference OP satisfies
394 extra (special) memory constraint CONSTRAINT. */
395 static bool
396 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
398 struct address_info ad;
400 decompose_mem_address (&ad, op);
401 address_eliminator eliminator (&ad);
402 return constraint_satisfied_p (op, constraint);
405 /* Return true if the eliminated form of address AD satisfies extra
406 address constraint CONSTRAINT. */
407 static bool
408 satisfies_address_constraint_p (struct address_info *ad,
409 enum constraint_num constraint)
411 address_eliminator eliminator (ad);
412 return constraint_satisfied_p (*ad->outer, constraint);
415 /* Return true if the eliminated form of address OP satisfies extra
416 address constraint CONSTRAINT. */
417 static bool
418 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
420 struct address_info ad;
422 decompose_lea_address (&ad, &op);
423 return satisfies_address_constraint_p (&ad, constraint);
426 /* Initiate equivalences for LRA. As we keep original equivalences
427 before any elimination, we need to make copies otherwise any change
428 in insns might change the equivalences. */
429 void
430 lra_init_equiv (void)
432 ira_expand_reg_equiv ();
433 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
435 rtx res;
437 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
438 ira_reg_equiv[i].memory = copy_rtx (res);
439 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
440 ira_reg_equiv[i].invariant = copy_rtx (res);
444 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
446 /* Update equivalence for REGNO. We need to this as the equivalence
447 might contain other pseudos which are changed by their
448 equivalences. */
449 static void
450 update_equiv (int regno)
452 rtx x;
454 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
455 ira_reg_equiv[regno].memory
456 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
457 NULL_RTX);
458 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
459 ira_reg_equiv[regno].invariant
460 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
461 NULL_RTX);
464 /* If we have decided to substitute X with another value, return that
465 value, otherwise return X. */
466 static rtx
467 get_equiv (rtx x)
469 int regno;
470 rtx res;
472 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
473 || ! ira_reg_equiv[regno].defined_p
474 || ! ira_reg_equiv[regno].profitable_p
475 || lra_get_regno_hard_regno (regno) >= 0)
476 return x;
477 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
479 if (targetm.cannot_substitute_mem_equiv_p (res))
480 return x;
481 return res;
483 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
484 return res;
485 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
486 return res;
487 gcc_unreachable ();
490 /* If we have decided to substitute X with the equivalent value,
491 return that value after elimination for INSN, otherwise return
492 X. */
493 static rtx
494 get_equiv_with_elimination (rtx x, rtx_insn *insn)
496 rtx res = get_equiv (x);
498 if (x == res || CONSTANT_P (res))
499 return res;
500 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
501 false, false, 0, true);
504 /* Set up curr_operand_mode. */
505 static void
506 init_curr_operand_mode (void)
508 int nop = curr_static_id->n_operands;
509 for (int i = 0; i < nop; i++)
511 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
512 if (mode == VOIDmode)
514 /* The .md mode for address operands is the mode of the
515 addressed value rather than the mode of the address itself. */
516 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
517 mode = Pmode;
518 else
519 mode = curr_static_id->operand[i].mode;
521 curr_operand_mode[i] = mode;
527 /* The page contains code to reuse input reloads. */
529 /* Structure describes input reload of the current insns. */
530 struct input_reload
532 /* Reloaded value. */
533 rtx input;
534 /* Reload pseudo used. */
535 rtx reg;
538 /* The number of elements in the following array. */
539 static int curr_insn_input_reloads_num;
540 /* Array containing info about input reloads. It is used to find the
541 same input reload and reuse the reload pseudo in this case. */
542 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
544 /* Initiate data concerning reuse of input reloads for the current
545 insn. */
546 static void
547 init_curr_insn_input_reloads (void)
549 curr_insn_input_reloads_num = 0;
552 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
553 created input reload pseudo (only if TYPE is not OP_OUT). Don't
554 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
555 wrapped up in SUBREG. The result pseudo is returned through
556 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
557 reused the already created input reload pseudo. Use TITLE to
558 describe new registers for debug purposes. */
559 static bool
560 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
561 enum reg_class rclass, bool in_subreg_p,
562 const char *title, rtx *result_reg)
564 int i, regno;
565 enum reg_class new_class;
567 if (type == OP_OUT)
569 *result_reg
570 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
571 return true;
573 /* Prevent reuse value of expression with side effects,
574 e.g. volatile memory. */
575 if (! side_effects_p (original))
576 for (i = 0; i < curr_insn_input_reloads_num; i++)
577 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
578 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
580 rtx reg = curr_insn_input_reloads[i].reg;
581 regno = REGNO (reg);
582 /* If input is equal to original and both are VOIDmode,
583 GET_MODE (reg) might be still different from mode.
584 Ensure we don't return *result_reg with wrong mode. */
585 if (GET_MODE (reg) != mode)
587 if (in_subreg_p)
588 continue;
589 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
590 continue;
591 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
592 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
593 continue;
595 *result_reg = reg;
596 if (lra_dump_file != NULL)
598 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
599 dump_value_slim (lra_dump_file, original, 1);
601 if (new_class != lra_get_allocno_class (regno))
602 lra_change_class (regno, new_class, ", change to", false);
603 if (lra_dump_file != NULL)
604 fprintf (lra_dump_file, "\n");
605 return false;
607 *result_reg = lra_create_new_reg (mode, original, rclass, title);
608 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
609 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
610 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
611 return true;
616 /* The page contains code to extract memory address parts. */
618 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
619 static inline bool
620 ok_for_index_p_nonstrict (rtx reg)
622 unsigned regno = REGNO (reg);
624 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
627 /* A version of regno_ok_for_base_p for use here, when all pseudos
628 should count as OK. Arguments as for regno_ok_for_base_p. */
629 static inline bool
630 ok_for_base_p_nonstrict (rtx reg, machine_mode mode, addr_space_t as,
631 enum rtx_code outer_code, enum rtx_code index_code)
633 unsigned regno = REGNO (reg);
635 if (regno >= FIRST_PSEUDO_REGISTER)
636 return true;
637 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
642 /* The page contains major code to choose the current insn alternative
643 and generate reloads for it. */
645 /* Return the offset from REGNO of the least significant register
646 in (reg:MODE REGNO).
648 This function is used to tell whether two registers satisfy
649 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
651 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
652 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
654 lra_constraint_offset (int regno, machine_mode mode)
656 lra_assert (regno < FIRST_PSEUDO_REGISTER);
657 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
658 && SCALAR_INT_MODE_P (mode))
659 return hard_regno_nregs[regno][mode] - 1;
660 return 0;
663 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
664 if they are the same hard reg, and has special hacks for
665 auto-increment and auto-decrement. This is specifically intended for
666 process_alt_operands to use in determining whether two operands
667 match. X is the operand whose number is the lower of the two.
669 It is supposed that X is the output operand and Y is the input
670 operand. Y_HARD_REGNO is the final hard regno of register Y or
671 register in subreg Y as we know it now. Otherwise, it is a
672 negative value. */
673 static bool
674 operands_match_p (rtx x, rtx y, int y_hard_regno)
676 int i;
677 RTX_CODE code = GET_CODE (x);
678 const char *fmt;
680 if (x == y)
681 return true;
682 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
683 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
685 int j;
687 i = get_hard_regno (x, false);
688 if (i < 0)
689 goto slow;
691 if ((j = y_hard_regno) < 0)
692 goto slow;
694 i += lra_constraint_offset (i, GET_MODE (x));
695 j += lra_constraint_offset (j, GET_MODE (y));
697 return i == j;
700 /* If two operands must match, because they are really a single
701 operand of an assembler insn, then two post-increments are invalid
702 because the assembler insn would increment only once. On the
703 other hand, a post-increment matches ordinary indexing if the
704 post-increment is the output operand. */
705 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
706 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
708 /* Two pre-increments are invalid because the assembler insn would
709 increment only once. On the other hand, a pre-increment matches
710 ordinary indexing if the pre-increment is the input operand. */
711 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
712 || GET_CODE (y) == PRE_MODIFY)
713 return operands_match_p (x, XEXP (y, 0), -1);
715 slow:
717 if (code == REG && REG_P (y))
718 return REGNO (x) == REGNO (y);
720 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
721 && x == SUBREG_REG (y))
722 return true;
723 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
724 && SUBREG_REG (x) == y)
725 return true;
727 /* Now we have disposed of all the cases in which different rtx
728 codes can match. */
729 if (code != GET_CODE (y))
730 return false;
732 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
733 if (GET_MODE (x) != GET_MODE (y))
734 return false;
736 switch (code)
738 CASE_CONST_UNIQUE:
739 return false;
741 case LABEL_REF:
742 return label_ref_label (x) == label_ref_label (y);
743 case SYMBOL_REF:
744 return XSTR (x, 0) == XSTR (y, 0);
746 default:
747 break;
750 /* Compare the elements. If any pair of corresponding elements fail
751 to match, return false for the whole things. */
753 fmt = GET_RTX_FORMAT (code);
754 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
756 int val, j;
757 switch (fmt[i])
759 case 'w':
760 if (XWINT (x, i) != XWINT (y, i))
761 return false;
762 break;
764 case 'i':
765 if (XINT (x, i) != XINT (y, i))
766 return false;
767 break;
769 case 'e':
770 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
771 if (val == 0)
772 return false;
773 break;
775 case '0':
776 break;
778 case 'E':
779 if (XVECLEN (x, i) != XVECLEN (y, i))
780 return false;
781 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
783 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
784 if (val == 0)
785 return false;
787 break;
789 /* It is believed that rtx's at this level will never
790 contain anything but integers and other rtx's, except for
791 within LABEL_REFs and SYMBOL_REFs. */
792 default:
793 gcc_unreachable ();
796 return true;
799 /* True if X is a constant that can be forced into the constant pool.
800 MODE is the mode of the operand, or VOIDmode if not known. */
801 #define CONST_POOL_OK_P(MODE, X) \
802 ((MODE) != VOIDmode \
803 && CONSTANT_P (X) \
804 && GET_CODE (X) != HIGH \
805 && !targetm.cannot_force_const_mem (MODE, X))
807 /* True if C is a non-empty register class that has too few registers
808 to be safely used as a reload target class. */
809 #define SMALL_REGISTER_CLASS_P(C) \
810 (ira_class_hard_regs_num [(C)] == 1 \
811 || (ira_class_hard_regs_num [(C)] >= 1 \
812 && targetm.class_likely_spilled_p (C)))
814 /* If REG is a reload pseudo, try to make its class satisfying CL. */
815 static void
816 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
818 enum reg_class rclass;
820 /* Do not make more accurate class from reloads generated. They are
821 mostly moves with a lot of constraints. Making more accurate
822 class may results in very narrow class and impossibility of find
823 registers for several reloads of one insn. */
824 if (INSN_UID (curr_insn) >= new_insn_uid_start)
825 return;
826 if (GET_CODE (reg) == SUBREG)
827 reg = SUBREG_REG (reg);
828 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
829 return;
830 if (in_class_p (reg, cl, &rclass) && rclass != cl)
831 lra_change_class (REGNO (reg), rclass, " Change to", true);
834 /* Searches X for any reference to a reg with the same value as REGNO,
835 returning the rtx of the reference found if any. Otherwise,
836 returns NULL_RTX. */
837 static rtx
838 regno_val_use_in (unsigned int regno, rtx x)
840 const char *fmt;
841 int i, j;
842 rtx tem;
844 if (REG_P (x) && lra_reg_info[REGNO (x)].val == lra_reg_info[regno].val)
845 return x;
847 fmt = GET_RTX_FORMAT (GET_CODE (x));
848 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
850 if (fmt[i] == 'e')
852 if ((tem = regno_val_use_in (regno, XEXP (x, i))))
853 return tem;
855 else if (fmt[i] == 'E')
856 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
857 if ((tem = regno_val_use_in (regno , XVECEXP (x, i, j))))
858 return tem;
861 return NULL_RTX;
864 /* Generate reloads for matching OUT and INS (array of input operand
865 numbers with end marker -1) with reg class GOAL_CLASS, considering
866 output operands OUTS (similar array to INS) needing to be in different
867 registers. Add input and output reloads correspondingly to the lists
868 *BEFORE and *AFTER. OUT might be negative. In this case we generate
869 input reloads for matched input operands INS. EARLY_CLOBBER_P is a flag
870 that the output operand is early clobbered for chosen alternative. */
871 static void
872 match_reload (signed char out, signed char *ins, signed char *outs,
873 enum reg_class goal_class, rtx_insn **before,
874 rtx_insn **after, bool early_clobber_p)
876 bool out_conflict;
877 int i, in;
878 rtx new_in_reg, new_out_reg, reg;
879 machine_mode inmode, outmode;
880 rtx in_rtx = *curr_id->operand_loc[ins[0]];
881 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
883 inmode = curr_operand_mode[ins[0]];
884 outmode = out < 0 ? inmode : curr_operand_mode[out];
885 push_to_sequence (*before);
886 if (inmode != outmode)
888 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
890 reg = new_in_reg
891 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
892 goal_class, "");
893 if (SCALAR_INT_MODE_P (inmode))
894 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
895 else
896 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
897 LRA_SUBREG_P (new_out_reg) = 1;
898 /* If the input reg is dying here, we can use the same hard
899 register for REG and IN_RTX. We do it only for original
900 pseudos as reload pseudos can die although original
901 pseudos still live where reload pseudos dies. */
902 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
903 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
904 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
906 else
908 reg = new_out_reg
909 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
910 goal_class, "");
911 if (SCALAR_INT_MODE_P (outmode))
912 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
913 else
914 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
915 /* NEW_IN_REG is non-paradoxical subreg. We don't want
916 NEW_OUT_REG living above. We add clobber clause for
917 this. This is just a temporary clobber. We can remove
918 it at the end of LRA work. */
919 rtx_insn *clobber = emit_clobber (new_out_reg);
920 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
921 LRA_SUBREG_P (new_in_reg) = 1;
922 if (GET_CODE (in_rtx) == SUBREG)
924 rtx subreg_reg = SUBREG_REG (in_rtx);
926 /* If SUBREG_REG is dying here and sub-registers IN_RTX
927 and NEW_IN_REG are similar, we can use the same hard
928 register for REG and SUBREG_REG. */
929 if (REG_P (subreg_reg)
930 && (int) REGNO (subreg_reg) < lra_new_regno_start
931 && GET_MODE (subreg_reg) == outmode
932 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
933 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
934 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
938 else
940 /* Pseudos have values -- see comments for lra_reg_info.
941 Different pseudos with the same value do not conflict even if
942 they live in the same place. When we create a pseudo we
943 assign value of original pseudo (if any) from which we
944 created the new pseudo. If we create the pseudo from the
945 input pseudo, the new pseudo will have no conflict with the
946 input pseudo which is wrong when the input pseudo lives after
947 the insn and as the new pseudo value is changed by the insn
948 output. Therefore we create the new pseudo from the output
949 except the case when we have single matched dying input
950 pseudo.
952 We cannot reuse the current output register because we might
953 have a situation like "a <- a op b", where the constraints
954 force the second input operand ("b") to match the output
955 operand ("a"). "b" must then be copied into a new register
956 so that it doesn't clobber the current value of "a".
958 We can not use the same value if the output pseudo is
959 early clobbered or the input pseudo is mentioned in the
960 output, e.g. as an address part in memory, because
961 output reload will actually extend the pseudo liveness.
962 We don't care about eliminable hard regs here as we are
963 interesting only in pseudos. */
965 /* Matching input's register value is the same as one of the other
966 output operand. Output operands in a parallel insn must be in
967 different registers. */
968 out_conflict = false;
969 if (REG_P (in_rtx))
971 for (i = 0; outs[i] >= 0; i++)
973 rtx other_out_rtx = *curr_id->operand_loc[outs[i]];
974 if (REG_P (other_out_rtx)
975 && (regno_val_use_in (REGNO (in_rtx), other_out_rtx)
976 != NULL_RTX))
978 out_conflict = true;
979 break;
984 new_in_reg = new_out_reg
985 = (! early_clobber_p && ins[1] < 0 && REG_P (in_rtx)
986 && (int) REGNO (in_rtx) < lra_new_regno_start
987 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
988 && (out < 0
989 || regno_val_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
990 && !out_conflict
991 ? lra_create_new_reg (inmode, in_rtx, goal_class, "")
992 : lra_create_new_reg_with_unique_value (outmode, out_rtx,
993 goal_class, ""));
995 /* In operand can be got from transformations before processing insn
996 constraints. One example of such transformations is subreg
997 reloading (see function simplify_operand_subreg). The new
998 pseudos created by the transformations might have inaccurate
999 class (ALL_REGS) and we should make their classes more
1000 accurate. */
1001 narrow_reload_pseudo_class (in_rtx, goal_class);
1002 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
1003 *before = get_insns ();
1004 end_sequence ();
1005 for (i = 0; (in = ins[i]) >= 0; i++)
1007 lra_assert
1008 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
1009 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
1010 *curr_id->operand_loc[in] = new_in_reg;
1012 lra_update_dups (curr_id, ins);
1013 if (out < 0)
1014 return;
1015 /* See a comment for the input operand above. */
1016 narrow_reload_pseudo_class (out_rtx, goal_class);
1017 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
1019 start_sequence ();
1020 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
1021 emit_insn (*after);
1022 *after = get_insns ();
1023 end_sequence ();
1025 *curr_id->operand_loc[out] = new_out_reg;
1026 lra_update_dup (curr_id, out);
1029 /* Return register class which is union of all reg classes in insn
1030 constraint alternative string starting with P. */
1031 static enum reg_class
1032 reg_class_from_constraints (const char *p)
1034 int c, len;
1035 enum reg_class op_class = NO_REGS;
1038 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1040 case '#':
1041 case ',':
1042 return op_class;
1044 case 'g':
1045 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1046 break;
1048 default:
1049 enum constraint_num cn = lookup_constraint (p);
1050 enum reg_class cl = reg_class_for_constraint (cn);
1051 if (cl == NO_REGS)
1053 if (insn_extra_address_constraint (cn))
1054 op_class
1055 = (reg_class_subunion
1056 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1057 ADDRESS, SCRATCH)]);
1058 break;
1061 op_class = reg_class_subunion[op_class][cl];
1062 break;
1064 while ((p += len), c);
1065 return op_class;
1068 /* If OP is a register, return the class of the register as per
1069 get_reg_class, otherwise return NO_REGS. */
1070 static inline enum reg_class
1071 get_op_class (rtx op)
1073 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1076 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1077 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1078 SUBREG for VAL to make them equal. */
1079 static rtx_insn *
1080 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1082 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1084 /* Usually size of mem_pseudo is greater than val size but in
1085 rare cases it can be less as it can be defined by target
1086 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1087 if (! MEM_P (val))
1089 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1090 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1092 LRA_SUBREG_P (val) = 1;
1094 else
1096 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1097 LRA_SUBREG_P (mem_pseudo) = 1;
1100 return to_p ? gen_move_insn (mem_pseudo, val)
1101 : gen_move_insn (val, mem_pseudo);
1104 /* Process a special case insn (register move), return true if we
1105 don't need to process it anymore. INSN should be a single set
1106 insn. Set up that RTL was changed through CHANGE_P and macro
1107 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1108 SEC_MEM_P. */
1109 static bool
1110 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1112 int sregno, dregno;
1113 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1114 rtx_insn *before;
1115 enum reg_class dclass, sclass, secondary_class;
1116 secondary_reload_info sri;
1118 lra_assert (curr_insn_set != NULL_RTX);
1119 dreg = dest = SET_DEST (curr_insn_set);
1120 sreg = src = SET_SRC (curr_insn_set);
1121 if (GET_CODE (dest) == SUBREG)
1122 dreg = SUBREG_REG (dest);
1123 if (GET_CODE (src) == SUBREG)
1124 sreg = SUBREG_REG (src);
1125 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1126 return false;
1127 sclass = dclass = NO_REGS;
1128 if (REG_P (dreg))
1129 dclass = get_reg_class (REGNO (dreg));
1130 if (dclass == ALL_REGS)
1131 /* ALL_REGS is used for new pseudos created by transformations
1132 like reload of SUBREG_REG (see function
1133 simplify_operand_subreg). We don't know their class yet. We
1134 should figure out the class from processing the insn
1135 constraints not in this fast path function. Even if ALL_REGS
1136 were a right class for the pseudo, secondary_... hooks usually
1137 are not define for ALL_REGS. */
1138 return false;
1139 if (REG_P (sreg))
1140 sclass = get_reg_class (REGNO (sreg));
1141 if (sclass == ALL_REGS)
1142 /* See comments above. */
1143 return false;
1144 if (sclass == NO_REGS && dclass == NO_REGS)
1145 return false;
1146 #ifdef SECONDARY_MEMORY_NEEDED
1147 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1148 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1149 && ((sclass != NO_REGS && dclass != NO_REGS)
1150 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1151 #endif
1154 *sec_mem_p = true;
1155 return false;
1157 #endif
1158 if (! REG_P (dreg) || ! REG_P (sreg))
1159 return false;
1160 sri.prev_sri = NULL;
1161 sri.icode = CODE_FOR_nothing;
1162 sri.extra_cost = 0;
1163 secondary_class = NO_REGS;
1164 /* Set up hard register for a reload pseudo for hook
1165 secondary_reload because some targets just ignore unassigned
1166 pseudos in the hook. */
1167 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1169 dregno = REGNO (dreg);
1170 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1172 else
1173 dregno = -1;
1174 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1176 sregno = REGNO (sreg);
1177 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1179 else
1180 sregno = -1;
1181 if (sclass != NO_REGS)
1182 secondary_class
1183 = (enum reg_class) targetm.secondary_reload (false, dest,
1184 (reg_class_t) sclass,
1185 GET_MODE (src), &sri);
1186 if (sclass == NO_REGS
1187 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1188 && dclass != NO_REGS))
1190 enum reg_class old_sclass = secondary_class;
1191 secondary_reload_info old_sri = sri;
1193 sri.prev_sri = NULL;
1194 sri.icode = CODE_FOR_nothing;
1195 sri.extra_cost = 0;
1196 secondary_class
1197 = (enum reg_class) targetm.secondary_reload (true, src,
1198 (reg_class_t) dclass,
1199 GET_MODE (src), &sri);
1200 /* Check the target hook consistency. */
1201 lra_assert
1202 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1203 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1204 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1206 if (sregno >= 0)
1207 reg_renumber [sregno] = -1;
1208 if (dregno >= 0)
1209 reg_renumber [dregno] = -1;
1210 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1211 return false;
1212 *change_p = true;
1213 new_reg = NULL_RTX;
1214 if (secondary_class != NO_REGS)
1215 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1216 secondary_class,
1217 "secondary");
1218 start_sequence ();
1219 if (sri.icode == CODE_FOR_nothing)
1220 lra_emit_move (new_reg, src);
1221 else
1223 enum reg_class scratch_class;
1225 scratch_class = (reg_class_from_constraints
1226 (insn_data[sri.icode].operand[2].constraint));
1227 scratch_reg = (lra_create_new_reg_with_unique_value
1228 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1229 scratch_class, "scratch"));
1230 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1231 src, scratch_reg));
1233 before = get_insns ();
1234 end_sequence ();
1235 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1236 if (new_reg != NULL_RTX)
1237 SET_SRC (curr_insn_set) = new_reg;
1238 else
1240 if (lra_dump_file != NULL)
1242 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1243 dump_insn_slim (lra_dump_file, curr_insn);
1245 lra_set_insn_deleted (curr_insn);
1246 return true;
1248 return false;
1251 /* The following data describe the result of process_alt_operands.
1252 The data are used in curr_insn_transform to generate reloads. */
1254 /* The chosen reg classes which should be used for the corresponding
1255 operands. */
1256 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1257 /* True if the operand should be the same as another operand and that
1258 other operand does not need a reload. */
1259 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1260 /* True if the operand does not need a reload. */
1261 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1262 /* True if the operand can be offsetable memory. */
1263 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1264 /* The number of an operand to which given operand can be matched to. */
1265 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1266 /* The number of elements in the following array. */
1267 static int goal_alt_dont_inherit_ops_num;
1268 /* Numbers of operands whose reload pseudos should not be inherited. */
1269 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1270 /* True if the insn commutative operands should be swapped. */
1271 static bool goal_alt_swapped;
1272 /* The chosen insn alternative. */
1273 static int goal_alt_number;
1275 /* True if the corresponding operand is the result of an equivalence
1276 substitution. */
1277 static bool equiv_substition_p[MAX_RECOG_OPERANDS];
1279 /* The following five variables are used to choose the best insn
1280 alternative. They reflect final characteristics of the best
1281 alternative. */
1283 /* Number of necessary reloads and overall cost reflecting the
1284 previous value and other unpleasantness of the best alternative. */
1285 static int best_losers, best_overall;
1286 /* Overall number hard registers used for reloads. For example, on
1287 some targets we need 2 general registers to reload DFmode and only
1288 one floating point register. */
1289 static int best_reload_nregs;
1290 /* Overall number reflecting distances of previous reloading the same
1291 value. The distances are counted from the current BB start. It is
1292 used to improve inheritance chances. */
1293 static int best_reload_sum;
1295 /* True if the current insn should have no correspondingly input or
1296 output reloads. */
1297 static bool no_input_reloads_p, no_output_reloads_p;
1299 /* True if we swapped the commutative operands in the current
1300 insn. */
1301 static int curr_swapped;
1303 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1304 register of class CL. Add any input reloads to list BEFORE. AFTER
1305 is nonnull if *LOC is an automodified value; handle that case by
1306 adding the required output reloads to list AFTER. Return true if
1307 the RTL was changed.
1309 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1310 register. Return false if the address register is correct. */
1311 static bool
1312 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1313 enum reg_class cl)
1315 int regno;
1316 enum reg_class rclass, new_class;
1317 rtx reg;
1318 rtx new_reg;
1319 machine_mode mode;
1320 bool subreg_p, before_p = false;
1322 subreg_p = GET_CODE (*loc) == SUBREG;
1323 if (subreg_p)
1325 reg = SUBREG_REG (*loc);
1326 mode = GET_MODE (reg);
1328 /* For mode with size bigger than ptr_mode, there unlikely to be "mov"
1329 between two registers with different classes, but there normally will
1330 be "mov" which transfers element of vector register into the general
1331 register, and this normally will be a subreg which should be reloaded
1332 as a whole. This is particularly likely to be triggered when
1333 -fno-split-wide-types specified. */
1334 if (!REG_P (reg)
1335 || in_class_p (reg, cl, &new_class)
1336 || GET_MODE_SIZE (mode) <= GET_MODE_SIZE (ptr_mode))
1337 loc = &SUBREG_REG (*loc);
1340 reg = *loc;
1341 mode = GET_MODE (reg);
1342 if (! REG_P (reg))
1344 if (check_only_p)
1345 return true;
1346 /* Always reload memory in an address even if the target supports
1347 such addresses. */
1348 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1349 before_p = true;
1351 else
1353 regno = REGNO (reg);
1354 rclass = get_reg_class (regno);
1355 if (! check_only_p
1356 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1358 if (lra_dump_file != NULL)
1360 fprintf (lra_dump_file,
1361 "Changing pseudo %d in address of insn %u on equiv ",
1362 REGNO (reg), INSN_UID (curr_insn));
1363 dump_value_slim (lra_dump_file, *loc, 1);
1364 fprintf (lra_dump_file, "\n");
1366 *loc = copy_rtx (*loc);
1368 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1370 if (check_only_p)
1371 return true;
1372 reg = *loc;
1373 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1374 mode, reg, cl, subreg_p, "address", &new_reg))
1375 before_p = true;
1377 else if (new_class != NO_REGS && rclass != new_class)
1379 if (check_only_p)
1380 return true;
1381 lra_change_class (regno, new_class, " Change to", true);
1382 return false;
1384 else
1385 return false;
1387 if (before_p)
1389 push_to_sequence (*before);
1390 lra_emit_move (new_reg, reg);
1391 *before = get_insns ();
1392 end_sequence ();
1394 *loc = new_reg;
1395 if (after != NULL)
1397 start_sequence ();
1398 lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1399 emit_insn (*after);
1400 *after = get_insns ();
1401 end_sequence ();
1403 return true;
1406 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1407 the insn to be inserted before curr insn. AFTER returns the
1408 the insn to be inserted after curr insn. ORIGREG and NEWREG
1409 are the original reg and new reg for reload. */
1410 static void
1411 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1412 rtx newreg)
1414 if (before)
1416 push_to_sequence (*before);
1417 lra_emit_move (newreg, origreg);
1418 *before = get_insns ();
1419 end_sequence ();
1421 if (after)
1423 start_sequence ();
1424 lra_emit_move (origreg, newreg);
1425 emit_insn (*after);
1426 *after = get_insns ();
1427 end_sequence ();
1431 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1433 /* Make reloads for subreg in operand NOP with internal subreg mode
1434 REG_MODE, add new reloads for further processing. Return true if
1435 any change was done. */
1436 static bool
1437 simplify_operand_subreg (int nop, machine_mode reg_mode)
1439 int hard_regno;
1440 rtx_insn *before, *after;
1441 machine_mode mode, innermode;
1442 rtx reg, new_reg;
1443 rtx operand = *curr_id->operand_loc[nop];
1444 enum reg_class regclass;
1445 enum op_type type;
1447 before = after = NULL;
1449 if (GET_CODE (operand) != SUBREG)
1450 return false;
1452 mode = GET_MODE (operand);
1453 reg = SUBREG_REG (operand);
1454 innermode = GET_MODE (reg);
1455 type = curr_static_id->operand[nop].type;
1456 if (MEM_P (reg))
1458 rtx subst;
1460 alter_subreg (curr_id->operand_loc[nop], false);
1461 subst = *curr_id->operand_loc[nop];
1462 lra_assert (MEM_P (subst));
1463 if (! valid_address_p (innermode, XEXP (reg, 0),
1464 MEM_ADDR_SPACE (reg))
1465 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1466 MEM_ADDR_SPACE (subst))
1467 || ((get_constraint_type (lookup_constraint
1468 (curr_static_id->operand[nop].constraint))
1469 != CT_SPECIAL_MEMORY)
1470 /* We still can reload address and if the address is
1471 valid, we can remove subreg without reloading its
1472 inner memory. */
1473 && valid_address_p (GET_MODE (subst),
1474 regno_reg_rtx
1475 [ira_class_hard_regs
1476 [base_reg_class (GET_MODE (subst),
1477 MEM_ADDR_SPACE (subst),
1478 ADDRESS, SCRATCH)][0]],
1479 MEM_ADDR_SPACE (subst))))
1481 /* If we change address for paradoxical subreg of memory, the
1482 address might violate the necessary alignment or the access might
1483 be slow. So take this into consideration. We should not worry
1484 about access beyond allocated memory for paradoxical memory
1485 subregs as we don't substitute such equiv memory (see processing
1486 equivalences in function lra_constraints) and because for spilled
1487 pseudos we allocate stack memory enough for the biggest
1488 corresponding paradoxical subreg. */
1489 if (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1490 || SLOW_UNALIGNED_ACCESS (innermode, MEM_ALIGN (reg))
1491 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode))
1492 return true;
1494 /* INNERMODE is fast, MODE slow. Reload the mem in INNERMODE. */
1495 enum reg_class rclass
1496 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1497 if (get_reload_reg (curr_static_id->operand[nop].type, innermode, reg,
1498 rclass, TRUE, "slow mem", &new_reg))
1500 bool insert_before, insert_after;
1501 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1503 insert_before = (type != OP_OUT
1504 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1505 insert_after = type != OP_IN;
1506 insert_move_for_subreg (insert_before ? &before : NULL,
1507 insert_after ? &after : NULL,
1508 reg, new_reg);
1510 *curr_id->operand_loc[nop] = operand;
1511 SUBREG_REG (operand) = new_reg;
1513 /* Convert to MODE. */
1514 reg = operand;
1515 rclass = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1516 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1517 rclass, TRUE, "slow mem", &new_reg))
1519 bool insert_before, insert_after;
1520 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1522 insert_before = type != OP_OUT;
1523 insert_after = type != OP_IN;
1524 insert_move_for_subreg (insert_before ? &before : NULL,
1525 insert_after ? &after : NULL,
1526 reg, new_reg);
1528 *curr_id->operand_loc[nop] = new_reg;
1529 lra_process_new_insns (curr_insn, before, after,
1530 "Inserting slow mem reload");
1531 return true;
1534 /* If the address was valid and became invalid, prefer to reload
1535 the memory. Typical case is when the index scale should
1536 correspond the memory. */
1537 *curr_id->operand_loc[nop] = operand;
1539 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1541 alter_subreg (curr_id->operand_loc[nop], false);
1542 return true;
1544 else if (CONSTANT_P (reg))
1546 /* Try to simplify subreg of constant. It is usually result of
1547 equivalence substitution. */
1548 if (innermode == VOIDmode
1549 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1550 innermode = curr_static_id->operand[nop].mode;
1551 if ((new_reg = simplify_subreg (mode, reg, innermode,
1552 SUBREG_BYTE (operand))) != NULL_RTX)
1554 *curr_id->operand_loc[nop] = new_reg;
1555 return true;
1558 /* Put constant into memory when we have mixed modes. It generates
1559 a better code in most cases as it does not need a secondary
1560 reload memory. It also prevents LRA looping when LRA is using
1561 secondary reload memory again and again. */
1562 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1563 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1565 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1566 alter_subreg (curr_id->operand_loc[nop], false);
1567 return true;
1569 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1570 if there may be a problem accessing OPERAND in the outer
1571 mode. */
1572 if ((REG_P (reg)
1573 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1574 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1575 /* Don't reload paradoxical subregs because we could be looping
1576 having repeatedly final regno out of hard regs range. */
1577 && (hard_regno_nregs[hard_regno][innermode]
1578 >= hard_regno_nregs[hard_regno][mode])
1579 && simplify_subreg_regno (hard_regno, innermode,
1580 SUBREG_BYTE (operand), mode) < 0
1581 /* Don't reload subreg for matching reload. It is actually
1582 valid subreg in LRA. */
1583 && ! LRA_SUBREG_P (operand))
1584 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1586 enum reg_class rclass;
1588 if (REG_P (reg))
1589 /* There is a big probability that we will get the same class
1590 for the new pseudo and we will get the same insn which
1591 means infinite looping. So spill the new pseudo. */
1592 rclass = NO_REGS;
1593 else
1594 /* The class will be defined later in curr_insn_transform. */
1595 rclass
1596 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1598 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1599 rclass, TRUE, "subreg reg", &new_reg))
1601 bool insert_before, insert_after;
1602 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1604 insert_before = (type != OP_OUT
1605 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1606 insert_after = (type != OP_IN);
1607 insert_move_for_subreg (insert_before ? &before : NULL,
1608 insert_after ? &after : NULL,
1609 reg, new_reg);
1611 SUBREG_REG (operand) = new_reg;
1612 lra_process_new_insns (curr_insn, before, after,
1613 "Inserting subreg reload");
1614 return true;
1616 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1617 IRA allocates hardreg to the inner pseudo reg according to its mode
1618 instead of the outermode, so the size of the hardreg may not be enough
1619 to contain the outermode operand, in that case we may need to insert
1620 reload for the reg. For the following two types of paradoxical subreg,
1621 we need to insert reload:
1622 1. If the op_type is OP_IN, and the hardreg could not be paired with
1623 other hardreg to contain the outermode operand
1624 (checked by in_hard_reg_set_p), we need to insert the reload.
1625 2. If the op_type is OP_OUT or OP_INOUT.
1627 Here is a paradoxical subreg example showing how the reload is generated:
1629 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1630 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1632 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1633 here, if reg107 is assigned to hardreg R15, because R15 is the last
1634 hardreg, compiler cannot find another hardreg to pair with R15 to
1635 contain TImode data. So we insert a TImode reload reg180 for it.
1636 After reload is inserted:
1638 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1639 (reg:DI 107 [ __comp ])) -1
1640 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1641 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1643 Two reload hard registers will be allocated to reg180 to save TImode data
1644 in LRA_assign. */
1645 else if (REG_P (reg)
1646 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1647 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1648 && (hard_regno_nregs[hard_regno][innermode]
1649 < hard_regno_nregs[hard_regno][mode])
1650 && (regclass = lra_get_allocno_class (REGNO (reg)))
1651 && (type != OP_IN
1652 || !in_hard_reg_set_p (reg_class_contents[regclass],
1653 mode, hard_regno)))
1655 /* The class will be defined later in curr_insn_transform. */
1656 enum reg_class rclass
1657 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1659 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1660 rclass, TRUE, "paradoxical subreg", &new_reg))
1662 rtx subreg;
1663 bool insert_before, insert_after;
1665 PUT_MODE (new_reg, mode);
1666 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1667 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1669 insert_before = (type != OP_OUT);
1670 insert_after = (type != OP_IN);
1671 insert_move_for_subreg (insert_before ? &before : NULL,
1672 insert_after ? &after : NULL,
1673 reg, subreg);
1675 SUBREG_REG (operand) = new_reg;
1676 lra_process_new_insns (curr_insn, before, after,
1677 "Inserting paradoxical subreg reload");
1678 return true;
1680 return false;
1683 /* Return TRUE if X refers for a hard register from SET. */
1684 static bool
1685 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1687 int i, j, x_hard_regno;
1688 machine_mode mode;
1689 const char *fmt;
1690 enum rtx_code code;
1692 if (x == NULL_RTX)
1693 return false;
1694 code = GET_CODE (x);
1695 mode = GET_MODE (x);
1696 if (code == SUBREG)
1698 x = SUBREG_REG (x);
1699 code = GET_CODE (x);
1700 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1701 mode = GET_MODE (x);
1704 if (REG_P (x))
1706 x_hard_regno = get_hard_regno (x, true);
1707 return (x_hard_regno >= 0
1708 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1710 if (MEM_P (x))
1712 struct address_info ad;
1714 decompose_mem_address (&ad, x);
1715 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1716 return true;
1717 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1718 return true;
1720 fmt = GET_RTX_FORMAT (code);
1721 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1723 if (fmt[i] == 'e')
1725 if (uses_hard_regs_p (XEXP (x, i), set))
1726 return true;
1728 else if (fmt[i] == 'E')
1730 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1731 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1732 return true;
1735 return false;
1738 /* Return true if OP is a spilled pseudo. */
1739 static inline bool
1740 spilled_pseudo_p (rtx op)
1742 return (REG_P (op)
1743 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1746 /* Return true if X is a general constant. */
1747 static inline bool
1748 general_constant_p (rtx x)
1750 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1753 static bool
1754 reg_in_class_p (rtx reg, enum reg_class cl)
1756 if (cl == NO_REGS)
1757 return get_reg_class (REGNO (reg)) == NO_REGS;
1758 return in_class_p (reg, cl, NULL);
1761 /* Return true if SET of RCLASS contains no hard regs which can be
1762 used in MODE. */
1763 static bool
1764 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1765 HARD_REG_SET &set,
1766 enum machine_mode mode)
1768 HARD_REG_SET temp;
1770 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1771 COPY_HARD_REG_SET (temp, set);
1772 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1773 return (hard_reg_set_subset_p
1774 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1777 /* Major function to choose the current insn alternative and what
1778 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1779 negative we should consider only this alternative. Return false if
1780 we can not choose the alternative or find how to reload the
1781 operands. */
1782 static bool
1783 process_alt_operands (int only_alternative)
1785 bool ok_p = false;
1786 int nop, overall, nalt;
1787 int n_alternatives = curr_static_id->n_alternatives;
1788 int n_operands = curr_static_id->n_operands;
1789 /* LOSERS counts the operands that don't fit this alternative and
1790 would require loading. */
1791 int losers;
1792 /* REJECT is a count of how undesirable this alternative says it is
1793 if any reloading is required. If the alternative matches exactly
1794 then REJECT is ignored, but otherwise it gets this much counted
1795 against it in addition to the reloading needed. */
1796 int reject;
1797 int op_reject;
1798 /* The number of elements in the following array. */
1799 int early_clobbered_regs_num;
1800 /* Numbers of operands which are early clobber registers. */
1801 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1802 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1803 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1804 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1805 bool curr_alt_win[MAX_RECOG_OPERANDS];
1806 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1807 int curr_alt_matches[MAX_RECOG_OPERANDS];
1808 /* The number of elements in the following array. */
1809 int curr_alt_dont_inherit_ops_num;
1810 /* Numbers of operands whose reload pseudos should not be inherited. */
1811 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1812 rtx op;
1813 /* The register when the operand is a subreg of register, otherwise the
1814 operand itself. */
1815 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1816 /* The register if the operand is a register or subreg of register,
1817 otherwise NULL. */
1818 rtx operand_reg[MAX_RECOG_OPERANDS];
1819 int hard_regno[MAX_RECOG_OPERANDS];
1820 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1821 int reload_nregs, reload_sum;
1822 bool costly_p;
1823 enum reg_class cl;
1825 /* Calculate some data common for all alternatives to speed up the
1826 function. */
1827 for (nop = 0; nop < n_operands; nop++)
1829 rtx reg;
1831 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1832 /* The real hard regno of the operand after the allocation. */
1833 hard_regno[nop] = get_hard_regno (op, true);
1835 operand_reg[nop] = reg = op;
1836 biggest_mode[nop] = GET_MODE (op);
1837 if (GET_CODE (op) == SUBREG)
1839 operand_reg[nop] = reg = SUBREG_REG (op);
1840 if (GET_MODE_SIZE (biggest_mode[nop])
1841 < GET_MODE_SIZE (GET_MODE (reg)))
1842 biggest_mode[nop] = GET_MODE (reg);
1844 if (! REG_P (reg))
1845 operand_reg[nop] = NULL_RTX;
1846 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1847 || ((int) REGNO (reg)
1848 == lra_get_elimination_hard_regno (REGNO (reg))))
1849 no_subreg_reg_operand[nop] = reg;
1850 else
1851 operand_reg[nop] = no_subreg_reg_operand[nop]
1852 /* Just use natural mode for elimination result. It should
1853 be enough for extra constraints hooks. */
1854 = regno_reg_rtx[hard_regno[nop]];
1857 /* The constraints are made of several alternatives. Each operand's
1858 constraint looks like foo,bar,... with commas separating the
1859 alternatives. The first alternatives for all operands go
1860 together, the second alternatives go together, etc.
1862 First loop over alternatives. */
1863 alternative_mask preferred = curr_id->preferred_alternatives;
1864 if (only_alternative >= 0)
1865 preferred &= ALTERNATIVE_BIT (only_alternative);
1867 for (nalt = 0; nalt < n_alternatives; nalt++)
1869 /* Loop over operands for one constraint alternative. */
1870 if (!TEST_BIT (preferred, nalt))
1871 continue;
1873 overall = losers = reject = reload_nregs = reload_sum = 0;
1874 for (nop = 0; nop < n_operands; nop++)
1876 int inc = (curr_static_id
1877 ->operand_alternative[nalt * n_operands + nop].reject);
1878 if (lra_dump_file != NULL && inc != 0)
1879 fprintf (lra_dump_file,
1880 " Staticly defined alt reject+=%d\n", inc);
1881 reject += inc;
1883 early_clobbered_regs_num = 0;
1885 for (nop = 0; nop < n_operands; nop++)
1887 const char *p;
1888 char *end;
1889 int len, c, m, i, opalt_num, this_alternative_matches;
1890 bool win, did_match, offmemok, early_clobber_p;
1891 /* false => this operand can be reloaded somehow for this
1892 alternative. */
1893 bool badop;
1894 /* true => this operand can be reloaded if the alternative
1895 allows regs. */
1896 bool winreg;
1897 /* True if a constant forced into memory would be OK for
1898 this operand. */
1899 bool constmemok;
1900 enum reg_class this_alternative, this_costly_alternative;
1901 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1902 bool this_alternative_match_win, this_alternative_win;
1903 bool this_alternative_offmemok;
1904 bool scratch_p;
1905 machine_mode mode;
1906 enum constraint_num cn;
1908 opalt_num = nalt * n_operands + nop;
1909 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1911 /* Fast track for no constraints at all. */
1912 curr_alt[nop] = NO_REGS;
1913 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1914 curr_alt_win[nop] = true;
1915 curr_alt_match_win[nop] = false;
1916 curr_alt_offmemok[nop] = false;
1917 curr_alt_matches[nop] = -1;
1918 continue;
1921 op = no_subreg_reg_operand[nop];
1922 mode = curr_operand_mode[nop];
1924 win = did_match = winreg = offmemok = constmemok = false;
1925 badop = true;
1927 early_clobber_p = false;
1928 p = curr_static_id->operand_alternative[opalt_num].constraint;
1930 this_costly_alternative = this_alternative = NO_REGS;
1931 /* We update set of possible hard regs besides its class
1932 because reg class might be inaccurate. For example,
1933 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1934 is translated in HI_REGS because classes are merged by
1935 pairs and there is no accurate intermediate class. */
1936 CLEAR_HARD_REG_SET (this_alternative_set);
1937 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1938 this_alternative_win = false;
1939 this_alternative_match_win = false;
1940 this_alternative_offmemok = false;
1941 this_alternative_matches = -1;
1943 /* An empty constraint should be excluded by the fast
1944 track. */
1945 lra_assert (*p != 0 && *p != ',');
1947 op_reject = 0;
1948 /* Scan this alternative's specs for this operand; set WIN
1949 if the operand fits any letter in this alternative.
1950 Otherwise, clear BADOP if this operand could fit some
1951 letter after reloads, or set WINREG if this operand could
1952 fit after reloads provided the constraint allows some
1953 registers. */
1954 costly_p = false;
1957 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1959 case '\0':
1960 len = 0;
1961 break;
1962 case ',':
1963 c = '\0';
1964 break;
1966 case '&':
1967 early_clobber_p = true;
1968 break;
1970 case '$':
1971 op_reject += LRA_MAX_REJECT;
1972 break;
1973 case '^':
1974 op_reject += LRA_LOSER_COST_FACTOR;
1975 break;
1977 case '#':
1978 /* Ignore rest of this alternative. */
1979 c = '\0';
1980 break;
1982 case '0': case '1': case '2': case '3': case '4':
1983 case '5': case '6': case '7': case '8': case '9':
1985 int m_hregno;
1986 bool match_p;
1988 m = strtoul (p, &end, 10);
1989 p = end;
1990 len = 0;
1991 lra_assert (nop > m);
1993 this_alternative_matches = m;
1994 m_hregno = get_hard_regno (*curr_id->operand_loc[m], false);
1995 /* We are supposed to match a previous operand.
1996 If we do, we win if that one did. If we do
1997 not, count both of the operands as losers.
1998 (This is too conservative, since most of the
1999 time only a single reload insn will be needed
2000 to make the two operands win. As a result,
2001 this alternative may be rejected when it is
2002 actually desirable.) */
2003 match_p = false;
2004 if (operands_match_p (*curr_id->operand_loc[nop],
2005 *curr_id->operand_loc[m], m_hregno))
2007 /* We should reject matching of an early
2008 clobber operand if the matching operand is
2009 not dying in the insn. */
2010 if (! curr_static_id->operand[m].early_clobber
2011 || operand_reg[nop] == NULL_RTX
2012 || (find_regno_note (curr_insn, REG_DEAD,
2013 REGNO (op))
2014 || REGNO (op) == REGNO (operand_reg[m])))
2015 match_p = true;
2017 if (match_p)
2019 /* If we are matching a non-offsettable
2020 address where an offsettable address was
2021 expected, then we must reject this
2022 combination, because we can't reload
2023 it. */
2024 if (curr_alt_offmemok[m]
2025 && MEM_P (*curr_id->operand_loc[m])
2026 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
2027 continue;
2029 else
2031 /* Operands don't match. Both operands must
2032 allow a reload register, otherwise we
2033 cannot make them match. */
2034 if (curr_alt[m] == NO_REGS)
2035 break;
2036 /* Retroactively mark the operand we had to
2037 match as a loser, if it wasn't already and
2038 it wasn't matched to a register constraint
2039 (e.g it might be matched by memory). */
2040 if (curr_alt_win[m]
2041 && (operand_reg[m] == NULL_RTX
2042 || hard_regno[m] < 0))
2044 losers++;
2045 reload_nregs
2046 += (ira_reg_class_max_nregs[curr_alt[m]]
2047 [GET_MODE (*curr_id->operand_loc[m])]);
2050 /* Prefer matching earlyclobber alternative as
2051 it results in less hard regs required for
2052 the insn than a non-matching earlyclobber
2053 alternative. */
2054 if (curr_static_id->operand[m].early_clobber)
2056 if (lra_dump_file != NULL)
2057 fprintf
2058 (lra_dump_file,
2059 " %d Matching earlyclobber alt:"
2060 " reject--\n",
2061 nop);
2062 reject--;
2064 /* Otherwise we prefer no matching
2065 alternatives because it gives more freedom
2066 in RA. */
2067 else if (operand_reg[nop] == NULL_RTX
2068 || (find_regno_note (curr_insn, REG_DEAD,
2069 REGNO (operand_reg[nop]))
2070 == NULL_RTX))
2072 if (lra_dump_file != NULL)
2073 fprintf
2074 (lra_dump_file,
2075 " %d Matching alt: reject+=2\n",
2076 nop);
2077 reject += 2;
2080 /* If we have to reload this operand and some
2081 previous operand also had to match the same
2082 thing as this operand, we don't know how to do
2083 that. */
2084 if (!match_p || !curr_alt_win[m])
2086 for (i = 0; i < nop; i++)
2087 if (curr_alt_matches[i] == m)
2088 break;
2089 if (i < nop)
2090 break;
2092 else
2093 did_match = true;
2095 /* This can be fixed with reloads if the operand
2096 we are supposed to match can be fixed with
2097 reloads. */
2098 badop = false;
2099 this_alternative = curr_alt[m];
2100 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
2101 winreg = this_alternative != NO_REGS;
2102 break;
2105 case 'g':
2106 if (MEM_P (op)
2107 || general_constant_p (op)
2108 || spilled_pseudo_p (op))
2109 win = true;
2110 cl = GENERAL_REGS;
2111 goto reg;
2113 default:
2114 cn = lookup_constraint (p);
2115 switch (get_constraint_type (cn))
2117 case CT_REGISTER:
2118 cl = reg_class_for_constraint (cn);
2119 if (cl != NO_REGS)
2120 goto reg;
2121 break;
2123 case CT_CONST_INT:
2124 if (CONST_INT_P (op)
2125 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2126 win = true;
2127 break;
2129 case CT_MEMORY:
2130 if (MEM_P (op)
2131 && satisfies_memory_constraint_p (op, cn))
2132 win = true;
2133 else if (spilled_pseudo_p (op))
2134 win = true;
2136 /* If we didn't already win, we can reload constants
2137 via force_const_mem or put the pseudo value into
2138 memory, or make other memory by reloading the
2139 address like for 'o'. */
2140 if (CONST_POOL_OK_P (mode, op)
2141 || MEM_P (op) || REG_P (op)
2142 /* We can restore the equiv insn by a
2143 reload. */
2144 || equiv_substition_p[nop])
2145 badop = false;
2146 constmemok = true;
2147 offmemok = true;
2148 break;
2150 case CT_ADDRESS:
2151 /* If we didn't already win, we can reload the address
2152 into a base register. */
2153 if (satisfies_address_constraint_p (op, cn))
2154 win = true;
2155 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2156 ADDRESS, SCRATCH);
2157 badop = false;
2158 goto reg;
2160 case CT_FIXED_FORM:
2161 if (constraint_satisfied_p (op, cn))
2162 win = true;
2163 break;
2165 case CT_SPECIAL_MEMORY:
2166 if (MEM_P (op)
2167 && satisfies_memory_constraint_p (op, cn))
2168 win = true;
2169 else if (spilled_pseudo_p (op))
2170 win = true;
2171 break;
2173 break;
2175 reg:
2176 this_alternative = reg_class_subunion[this_alternative][cl];
2177 IOR_HARD_REG_SET (this_alternative_set,
2178 reg_class_contents[cl]);
2179 if (costly_p)
2181 this_costly_alternative
2182 = reg_class_subunion[this_costly_alternative][cl];
2183 IOR_HARD_REG_SET (this_costly_alternative_set,
2184 reg_class_contents[cl]);
2186 if (mode == BLKmode)
2187 break;
2188 winreg = true;
2189 if (REG_P (op))
2191 if (hard_regno[nop] >= 0
2192 && in_hard_reg_set_p (this_alternative_set,
2193 mode, hard_regno[nop]))
2194 win = true;
2195 else if (hard_regno[nop] < 0
2196 && in_class_p (op, this_alternative, NULL))
2197 win = true;
2199 break;
2201 if (c != ' ' && c != '\t')
2202 costly_p = c == '*';
2204 while ((p += len), c);
2206 scratch_p = (operand_reg[nop] != NULL_RTX
2207 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2208 /* Record which operands fit this alternative. */
2209 if (win)
2211 this_alternative_win = true;
2212 if (operand_reg[nop] != NULL_RTX)
2214 if (hard_regno[nop] >= 0)
2216 if (in_hard_reg_set_p (this_costly_alternative_set,
2217 mode, hard_regno[nop]))
2219 if (lra_dump_file != NULL)
2220 fprintf (lra_dump_file,
2221 " %d Costly set: reject++\n",
2222 nop);
2223 reject++;
2226 else
2228 /* Prefer won reg to spilled pseudo under other
2229 equal conditions for possibe inheritance. */
2230 if (! scratch_p)
2232 if (lra_dump_file != NULL)
2233 fprintf
2234 (lra_dump_file,
2235 " %d Non pseudo reload: reject++\n",
2236 nop);
2237 reject++;
2239 if (in_class_p (operand_reg[nop],
2240 this_costly_alternative, NULL))
2242 if (lra_dump_file != NULL)
2243 fprintf
2244 (lra_dump_file,
2245 " %d Non pseudo costly reload:"
2246 " reject++\n",
2247 nop);
2248 reject++;
2251 /* We simulate the behavior of old reload here.
2252 Although scratches need hard registers and it
2253 might result in spilling other pseudos, no reload
2254 insns are generated for the scratches. So it
2255 might cost something but probably less than old
2256 reload pass believes. */
2257 if (scratch_p)
2259 if (lra_dump_file != NULL)
2260 fprintf (lra_dump_file,
2261 " %d Scratch win: reject+=2\n",
2262 nop);
2263 reject += 2;
2267 else if (did_match)
2268 this_alternative_match_win = true;
2269 else
2271 int const_to_mem = 0;
2272 bool no_regs_p;
2274 reject += op_reject;
2275 /* Never do output reload of stack pointer. It makes
2276 impossible to do elimination when SP is changed in
2277 RTL. */
2278 if (op == stack_pointer_rtx && ! frame_pointer_needed
2279 && curr_static_id->operand[nop].type != OP_IN)
2280 goto fail;
2282 /* If this alternative asks for a specific reg class, see if there
2283 is at least one allocatable register in that class. */
2284 no_regs_p
2285 = (this_alternative == NO_REGS
2286 || (hard_reg_set_subset_p
2287 (reg_class_contents[this_alternative],
2288 lra_no_alloc_regs)));
2290 /* For asms, verify that the class for this alternative is possible
2291 for the mode that is specified. */
2292 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2294 int i;
2295 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2296 if (HARD_REGNO_MODE_OK (i, mode)
2297 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2298 mode, i))
2299 break;
2300 if (i == FIRST_PSEUDO_REGISTER)
2301 winreg = false;
2304 /* If this operand accepts a register, and if the
2305 register class has at least one allocatable register,
2306 then this operand can be reloaded. */
2307 if (winreg && !no_regs_p)
2308 badop = false;
2310 if (badop)
2312 if (lra_dump_file != NULL)
2313 fprintf (lra_dump_file,
2314 " alt=%d: Bad operand -- refuse\n",
2315 nalt);
2316 goto fail;
2319 if (this_alternative != NO_REGS)
2321 HARD_REG_SET available_regs;
2323 COPY_HARD_REG_SET (available_regs,
2324 reg_class_contents[this_alternative]);
2325 AND_COMPL_HARD_REG_SET
2326 (available_regs,
2327 ira_prohibited_class_mode_regs[this_alternative][mode]);
2328 AND_COMPL_HARD_REG_SET (available_regs, lra_no_alloc_regs);
2329 if (hard_reg_set_empty_p (available_regs))
2331 /* There are no hard regs holding a value of given
2332 mode. */
2333 if (offmemok)
2335 this_alternative = NO_REGS;
2336 if (lra_dump_file != NULL)
2337 fprintf (lra_dump_file,
2338 " %d Using memory because of"
2339 " a bad mode: reject+=2\n",
2340 nop);
2341 reject += 2;
2343 else
2345 if (lra_dump_file != NULL)
2346 fprintf (lra_dump_file,
2347 " alt=%d: Wrong mode -- refuse\n",
2348 nalt);
2349 goto fail;
2354 /* If not assigned pseudo has a class which a subset of
2355 required reg class, it is a less costly alternative
2356 as the pseudo still can get a hard reg of necessary
2357 class. */
2358 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2359 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2360 && ira_class_subset_p[this_alternative][cl])
2362 if (lra_dump_file != NULL)
2363 fprintf
2364 (lra_dump_file,
2365 " %d Super set class reg: reject-=3\n", nop);
2366 reject -= 3;
2369 this_alternative_offmemok = offmemok;
2370 if (this_costly_alternative != NO_REGS)
2372 if (lra_dump_file != NULL)
2373 fprintf (lra_dump_file,
2374 " %d Costly loser: reject++\n", nop);
2375 reject++;
2377 /* If the operand is dying, has a matching constraint,
2378 and satisfies constraints of the matched operand
2379 which failed to satisfy the own constraints, most probably
2380 the reload for this operand will be gone. */
2381 if (this_alternative_matches >= 0
2382 && !curr_alt_win[this_alternative_matches]
2383 && REG_P (op)
2384 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2385 && (hard_regno[nop] >= 0
2386 ? in_hard_reg_set_p (this_alternative_set,
2387 mode, hard_regno[nop])
2388 : in_class_p (op, this_alternative, NULL)))
2390 if (lra_dump_file != NULL)
2391 fprintf
2392 (lra_dump_file,
2393 " %d Dying matched operand reload: reject++\n",
2394 nop);
2395 reject++;
2397 else
2399 /* Strict_low_part requires to reload the register
2400 not the sub-register. In this case we should
2401 check that a final reload hard reg can hold the
2402 value mode. */
2403 if (curr_static_id->operand[nop].strict_low
2404 && REG_P (op)
2405 && hard_regno[nop] < 0
2406 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2407 && ira_class_hard_regs_num[this_alternative] > 0
2408 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2409 [this_alternative][0],
2410 GET_MODE
2411 (*curr_id->operand_loc[nop])))
2413 if (lra_dump_file != NULL)
2414 fprintf
2415 (lra_dump_file,
2416 " alt=%d: Strict low subreg reload -- refuse\n",
2417 nalt);
2418 goto fail;
2420 losers++;
2422 if (operand_reg[nop] != NULL_RTX
2423 /* Output operands and matched input operands are
2424 not inherited. The following conditions do not
2425 exactly describe the previous statement but they
2426 are pretty close. */
2427 && curr_static_id->operand[nop].type != OP_OUT
2428 && (this_alternative_matches < 0
2429 || curr_static_id->operand[nop].type != OP_IN))
2431 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2432 (operand_reg[nop])]
2433 .last_reload);
2435 /* The value of reload_sum has sense only if we
2436 process insns in their order. It happens only on
2437 the first constraints sub-pass when we do most of
2438 reload work. */
2439 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2440 reload_sum += last_reload - bb_reload_num;
2442 /* If this is a constant that is reloaded into the
2443 desired class by copying it to memory first, count
2444 that as another reload. This is consistent with
2445 other code and is required to avoid choosing another
2446 alternative when the constant is moved into memory.
2447 Note that the test here is precisely the same as in
2448 the code below that calls force_const_mem. */
2449 if (CONST_POOL_OK_P (mode, op)
2450 && ((targetm.preferred_reload_class
2451 (op, this_alternative) == NO_REGS)
2452 || no_input_reloads_p))
2454 const_to_mem = 1;
2455 if (! no_regs_p)
2456 losers++;
2459 /* Alternative loses if it requires a type of reload not
2460 permitted for this insn. We can always reload
2461 objects with a REG_UNUSED note. */
2462 if ((curr_static_id->operand[nop].type != OP_IN
2463 && no_output_reloads_p
2464 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2465 || (curr_static_id->operand[nop].type != OP_OUT
2466 && no_input_reloads_p && ! const_to_mem)
2467 || (this_alternative_matches >= 0
2468 && (no_input_reloads_p
2469 || (no_output_reloads_p
2470 && (curr_static_id->operand
2471 [this_alternative_matches].type != OP_IN)
2472 && ! find_reg_note (curr_insn, REG_UNUSED,
2473 no_subreg_reg_operand
2474 [this_alternative_matches])))))
2476 if (lra_dump_file != NULL)
2477 fprintf
2478 (lra_dump_file,
2479 " alt=%d: No input/otput reload -- refuse\n",
2480 nalt);
2481 goto fail;
2484 /* Alternative loses if it required class pseudo can not
2485 hold value of required mode. Such insns can be
2486 described by insn definitions with mode iterators. */
2487 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2488 && ! hard_reg_set_empty_p (this_alternative_set)
2489 /* It is common practice for constraints to use a
2490 class which does not have actually enough regs to
2491 hold the value (e.g. x86 AREG for mode requiring
2492 more one general reg). Therefore we have 2
2493 conditions to check that the reload pseudo can
2494 not hold the mode value. */
2495 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2496 [this_alternative][0],
2497 GET_MODE (*curr_id->operand_loc[nop]))
2498 /* The above condition is not enough as the first
2499 reg in ira_class_hard_regs can be not aligned for
2500 multi-words mode values. */
2501 && (prohibited_class_reg_set_mode_p
2502 (this_alternative, this_alternative_set,
2503 GET_MODE (*curr_id->operand_loc[nop]))))
2505 if (lra_dump_file != NULL)
2506 fprintf (lra_dump_file,
2507 " alt=%d: reload pseudo for op %d "
2508 " can not hold the mode value -- refuse\n",
2509 nalt, nop);
2510 goto fail;
2513 /* Check strong discouragement of reload of non-constant
2514 into class THIS_ALTERNATIVE. */
2515 if (! CONSTANT_P (op) && ! no_regs_p
2516 && (targetm.preferred_reload_class
2517 (op, this_alternative) == NO_REGS
2518 || (curr_static_id->operand[nop].type == OP_OUT
2519 && (targetm.preferred_output_reload_class
2520 (op, this_alternative) == NO_REGS))))
2522 if (lra_dump_file != NULL)
2523 fprintf (lra_dump_file,
2524 " %d Non-prefered reload: reject+=%d\n",
2525 nop, LRA_MAX_REJECT);
2526 reject += LRA_MAX_REJECT;
2529 if (! (MEM_P (op) && offmemok)
2530 && ! (const_to_mem && constmemok))
2532 /* We prefer to reload pseudos over reloading other
2533 things, since such reloads may be able to be
2534 eliminated later. So bump REJECT in other cases.
2535 Don't do this in the case where we are forcing a
2536 constant into memory and it will then win since
2537 we don't want to have a different alternative
2538 match then. */
2539 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2541 if (lra_dump_file != NULL)
2542 fprintf
2543 (lra_dump_file,
2544 " %d Non-pseudo reload: reject+=2\n",
2545 nop);
2546 reject += 2;
2549 if (! no_regs_p)
2550 reload_nregs
2551 += ira_reg_class_max_nregs[this_alternative][mode];
2553 if (SMALL_REGISTER_CLASS_P (this_alternative))
2555 if (lra_dump_file != NULL)
2556 fprintf
2557 (lra_dump_file,
2558 " %d Small class reload: reject+=%d\n",
2559 nop, LRA_LOSER_COST_FACTOR / 2);
2560 reject += LRA_LOSER_COST_FACTOR / 2;
2564 /* We are trying to spill pseudo into memory. It is
2565 usually more costly than moving to a hard register
2566 although it might takes the same number of
2567 reloads.
2569 Non-pseudo spill may happen also. Suppose a target allows both
2570 register and memory in the operand constraint alternatives,
2571 then it's typical that an eliminable register has a substition
2572 of "base + offset" which can either be reloaded by a simple
2573 "new_reg <= base + offset" which will match the register
2574 constraint, or a similar reg addition followed by further spill
2575 to and reload from memory which will match the memory
2576 constraint, but this memory spill will be much more costly
2577 usually.
2579 Code below increases the reject for both pseudo and non-pseudo
2580 spill. */
2581 if (no_regs_p
2582 && !(MEM_P (op) && offmemok)
2583 && !(REG_P (op) && hard_regno[nop] < 0))
2585 if (lra_dump_file != NULL)
2586 fprintf
2587 (lra_dump_file,
2588 " %d Spill %spseudo into memory: reject+=3\n",
2589 nop, REG_P (op) ? "" : "Non-");
2590 reject += 3;
2591 if (VECTOR_MODE_P (mode))
2593 /* Spilling vectors into memory is usually more
2594 costly as they contain big values. */
2595 if (lra_dump_file != NULL)
2596 fprintf
2597 (lra_dump_file,
2598 " %d Spill vector pseudo: reject+=2\n",
2599 nop);
2600 reject += 2;
2604 #ifdef SECONDARY_MEMORY_NEEDED
2605 /* If reload requires moving value through secondary
2606 memory, it will need one more insn at least. */
2607 if (this_alternative != NO_REGS
2608 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2609 && ((curr_static_id->operand[nop].type != OP_OUT
2610 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2611 GET_MODE (op)))
2612 || (curr_static_id->operand[nop].type != OP_IN
2613 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2614 GET_MODE (op)))))
2615 losers++;
2616 #endif
2617 /* Input reloads can be inherited more often than output
2618 reloads can be removed, so penalize output
2619 reloads. */
2620 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2622 if (lra_dump_file != NULL)
2623 fprintf
2624 (lra_dump_file,
2625 " %d Non input pseudo reload: reject++\n",
2626 nop);
2627 reject++;
2631 if (early_clobber_p && ! scratch_p)
2633 if (lra_dump_file != NULL)
2634 fprintf (lra_dump_file,
2635 " %d Early clobber: reject++\n", nop);
2636 reject++;
2638 /* ??? We check early clobbers after processing all operands
2639 (see loop below) and there we update the costs more.
2640 Should we update the cost (may be approximately) here
2641 because of early clobber register reloads or it is a rare
2642 or non-important thing to be worth to do it. */
2643 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2644 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2646 if (lra_dump_file != NULL)
2647 fprintf (lra_dump_file,
2648 " alt=%d,overall=%d,losers=%d -- refuse\n",
2649 nalt, overall, losers);
2650 goto fail;
2653 curr_alt[nop] = this_alternative;
2654 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2655 curr_alt_win[nop] = this_alternative_win;
2656 curr_alt_match_win[nop] = this_alternative_match_win;
2657 curr_alt_offmemok[nop] = this_alternative_offmemok;
2658 curr_alt_matches[nop] = this_alternative_matches;
2660 if (this_alternative_matches >= 0
2661 && !did_match && !this_alternative_win)
2662 curr_alt_win[this_alternative_matches] = false;
2664 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2665 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2667 if (curr_insn_set != NULL_RTX && n_operands == 2
2668 /* Prevent processing non-move insns. */
2669 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2670 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2671 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2672 && REG_P (no_subreg_reg_operand[0])
2673 && REG_P (no_subreg_reg_operand[1])
2674 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2675 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2676 || (! curr_alt_win[0] && curr_alt_win[1]
2677 && REG_P (no_subreg_reg_operand[1])
2678 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2679 || (curr_alt_win[0] && ! curr_alt_win[1]
2680 && REG_P (no_subreg_reg_operand[0])
2681 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2682 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2683 no_subreg_reg_operand[1])
2684 || (targetm.preferred_reload_class
2685 (no_subreg_reg_operand[1],
2686 (enum reg_class) curr_alt[1]) != NO_REGS))
2687 /* If it is a result of recent elimination in move
2688 insn we can transform it into an add still by
2689 using this alternative. */
2690 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2692 /* We have a move insn and a new reload insn will be similar
2693 to the current insn. We should avoid such situation as it
2694 results in LRA cycling. */
2695 overall += LRA_MAX_REJECT;
2697 ok_p = true;
2698 curr_alt_dont_inherit_ops_num = 0;
2699 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2701 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2702 HARD_REG_SET temp_set;
2704 i = early_clobbered_nops[nop];
2705 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2706 || hard_regno[i] < 0)
2707 continue;
2708 lra_assert (operand_reg[i] != NULL_RTX);
2709 clobbered_hard_regno = hard_regno[i];
2710 CLEAR_HARD_REG_SET (temp_set);
2711 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2712 first_conflict_j = last_conflict_j = -1;
2713 for (j = 0; j < n_operands; j++)
2714 if (j == i
2715 /* We don't want process insides of match_operator and
2716 match_parallel because otherwise we would process
2717 their operands once again generating a wrong
2718 code. */
2719 || curr_static_id->operand[j].is_operator)
2720 continue;
2721 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2722 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2723 continue;
2724 /* If we don't reload j-th operand, check conflicts. */
2725 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2726 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2728 if (first_conflict_j < 0)
2729 first_conflict_j = j;
2730 last_conflict_j = j;
2732 if (last_conflict_j < 0)
2733 continue;
2734 /* If earlyclobber operand conflicts with another
2735 non-matching operand which is actually the same register
2736 as the earlyclobber operand, it is better to reload the
2737 another operand as an operand matching the earlyclobber
2738 operand can be also the same. */
2739 if (first_conflict_j == last_conflict_j
2740 && operand_reg[last_conflict_j] != NULL_RTX
2741 && ! curr_alt_match_win[last_conflict_j]
2742 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2744 curr_alt_win[last_conflict_j] = false;
2745 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2746 = last_conflict_j;
2747 losers++;
2748 /* Early clobber was already reflected in REJECT. */
2749 lra_assert (reject > 0);
2750 if (lra_dump_file != NULL)
2751 fprintf
2752 (lra_dump_file,
2753 " %d Conflict early clobber reload: reject--\n",
2755 reject--;
2756 overall += LRA_LOSER_COST_FACTOR - 1;
2758 else
2760 /* We need to reload early clobbered register and the
2761 matched registers. */
2762 for (j = 0; j < n_operands; j++)
2763 if (curr_alt_matches[j] == i)
2765 curr_alt_match_win[j] = false;
2766 losers++;
2767 overall += LRA_LOSER_COST_FACTOR;
2769 if (! curr_alt_match_win[i])
2770 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2771 else
2773 /* Remember pseudos used for match reloads are never
2774 inherited. */
2775 lra_assert (curr_alt_matches[i] >= 0);
2776 curr_alt_win[curr_alt_matches[i]] = false;
2778 curr_alt_win[i] = curr_alt_match_win[i] = false;
2779 losers++;
2780 /* Early clobber was already reflected in REJECT. */
2781 lra_assert (reject > 0);
2782 if (lra_dump_file != NULL)
2783 fprintf
2784 (lra_dump_file,
2785 " %d Matched conflict early clobber reloads:"
2786 "reject--\n",
2788 reject--;
2789 overall += LRA_LOSER_COST_FACTOR - 1;
2792 if (lra_dump_file != NULL)
2793 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2794 nalt, overall, losers, reload_nregs);
2796 /* If this alternative can be made to work by reloading, and it
2797 needs less reloading than the others checked so far, record
2798 it as the chosen goal for reloading. */
2799 if ((best_losers != 0 && losers == 0)
2800 || (((best_losers == 0 && losers == 0)
2801 || (best_losers != 0 && losers != 0))
2802 && (best_overall > overall
2803 || (best_overall == overall
2804 /* If the cost of the reloads is the same,
2805 prefer alternative which requires minimal
2806 number of reload regs. */
2807 && (reload_nregs < best_reload_nregs
2808 || (reload_nregs == best_reload_nregs
2809 && (best_reload_sum < reload_sum
2810 || (best_reload_sum == reload_sum
2811 && nalt < goal_alt_number))))))))
2813 for (nop = 0; nop < n_operands; nop++)
2815 goal_alt_win[nop] = curr_alt_win[nop];
2816 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2817 goal_alt_matches[nop] = curr_alt_matches[nop];
2818 goal_alt[nop] = curr_alt[nop];
2819 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2821 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2822 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2823 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2824 goal_alt_swapped = curr_swapped;
2825 best_overall = overall;
2826 best_losers = losers;
2827 best_reload_nregs = reload_nregs;
2828 best_reload_sum = reload_sum;
2829 goal_alt_number = nalt;
2831 if (losers == 0)
2832 /* Everything is satisfied. Do not process alternatives
2833 anymore. */
2834 break;
2835 fail:
2838 return ok_p;
2841 /* Make reload base reg from address AD. */
2842 static rtx
2843 base_to_reg (struct address_info *ad)
2845 enum reg_class cl;
2846 int code = -1;
2847 rtx new_inner = NULL_RTX;
2848 rtx new_reg = NULL_RTX;
2849 rtx_insn *insn;
2850 rtx_insn *last_insn = get_last_insn();
2852 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2853 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2854 get_index_code (ad));
2855 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2856 cl, "base");
2857 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2858 ad->disp_term == NULL
2859 ? gen_int_mode (0, ad->mode)
2860 : *ad->disp_term);
2861 if (!valid_address_p (ad->mode, new_inner, ad->as))
2862 return NULL_RTX;
2863 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base_term));
2864 code = recog_memoized (insn);
2865 if (code < 0)
2867 delete_insns_since (last_insn);
2868 return NULL_RTX;
2871 return new_inner;
2874 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2875 static rtx
2876 base_plus_disp_to_reg (struct address_info *ad)
2878 enum reg_class cl;
2879 rtx new_reg;
2881 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2882 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2883 get_index_code (ad));
2884 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2885 cl, "base + disp");
2886 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2887 return new_reg;
2890 /* Make reload of index part of address AD. Return the new
2891 pseudo. */
2892 static rtx
2893 index_part_to_reg (struct address_info *ad)
2895 rtx new_reg;
2897 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2898 INDEX_REG_CLASS, "index term");
2899 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2900 GEN_INT (get_index_scale (ad)), new_reg, 1);
2901 return new_reg;
2904 /* Return true if we can add a displacement to address AD, even if that
2905 makes the address invalid. The fix-up code requires any new address
2906 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2907 static bool
2908 can_add_disp_p (struct address_info *ad)
2910 return (!ad->autoinc_p
2911 && ad->segment == NULL
2912 && ad->base == ad->base_term
2913 && ad->disp == ad->disp_term);
2916 /* Make equiv substitution in address AD. Return true if a substitution
2917 was made. */
2918 static bool
2919 equiv_address_substitution (struct address_info *ad)
2921 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2922 HOST_WIDE_INT disp, scale;
2923 bool change_p;
2925 base_term = strip_subreg (ad->base_term);
2926 if (base_term == NULL)
2927 base_reg = new_base_reg = NULL_RTX;
2928 else
2930 base_reg = *base_term;
2931 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2933 index_term = strip_subreg (ad->index_term);
2934 if (index_term == NULL)
2935 index_reg = new_index_reg = NULL_RTX;
2936 else
2938 index_reg = *index_term;
2939 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2941 if (base_reg == new_base_reg && index_reg == new_index_reg)
2942 return false;
2943 disp = 0;
2944 change_p = false;
2945 if (lra_dump_file != NULL)
2947 fprintf (lra_dump_file, "Changing address in insn %d ",
2948 INSN_UID (curr_insn));
2949 dump_value_slim (lra_dump_file, *ad->outer, 1);
2951 if (base_reg != new_base_reg)
2953 if (REG_P (new_base_reg))
2955 *base_term = new_base_reg;
2956 change_p = true;
2958 else if (GET_CODE (new_base_reg) == PLUS
2959 && REG_P (XEXP (new_base_reg, 0))
2960 && CONST_INT_P (XEXP (new_base_reg, 1))
2961 && can_add_disp_p (ad))
2963 disp += INTVAL (XEXP (new_base_reg, 1));
2964 *base_term = XEXP (new_base_reg, 0);
2965 change_p = true;
2967 if (ad->base_term2 != NULL)
2968 *ad->base_term2 = *ad->base_term;
2970 if (index_reg != new_index_reg)
2972 if (REG_P (new_index_reg))
2974 *index_term = new_index_reg;
2975 change_p = true;
2977 else if (GET_CODE (new_index_reg) == PLUS
2978 && REG_P (XEXP (new_index_reg, 0))
2979 && CONST_INT_P (XEXP (new_index_reg, 1))
2980 && can_add_disp_p (ad)
2981 && (scale = get_index_scale (ad)))
2983 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2984 *index_term = XEXP (new_index_reg, 0);
2985 change_p = true;
2988 if (disp != 0)
2990 if (ad->disp != NULL)
2991 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2992 else
2994 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2995 update_address (ad);
2997 change_p = true;
2999 if (lra_dump_file != NULL)
3001 if (! change_p)
3002 fprintf (lra_dump_file, " -- no change\n");
3003 else
3005 fprintf (lra_dump_file, " on equiv ");
3006 dump_value_slim (lra_dump_file, *ad->outer, 1);
3007 fprintf (lra_dump_file, "\n");
3010 return change_p;
3013 /* Major function to make reloads for an address in operand NOP or
3014 check its correctness (If CHECK_ONLY_P is true). The supported
3015 cases are:
3017 1) an address that existed before LRA started, at which point it
3018 must have been valid. These addresses are subject to elimination
3019 and may have become invalid due to the elimination offset being out
3020 of range.
3022 2) an address created by forcing a constant to memory
3023 (force_const_to_mem). The initial form of these addresses might
3024 not be valid, and it is this function's job to make them valid.
3026 3) a frame address formed from a register and a (possibly zero)
3027 constant offset. As above, these addresses might not be valid and
3028 this function must make them so.
3030 Add reloads to the lists *BEFORE and *AFTER. We might need to add
3031 reloads to *AFTER because of inc/dec, {pre, post} modify in the
3032 address. Return true for any RTL change.
3034 The function is a helper function which does not produce all
3035 transformations (when CHECK_ONLY_P is false) which can be
3036 necessary. It does just basic steps. To do all necessary
3037 transformations use function process_address. */
3038 static bool
3039 process_address_1 (int nop, bool check_only_p,
3040 rtx_insn **before, rtx_insn **after)
3042 struct address_info ad;
3043 rtx new_reg;
3044 HOST_WIDE_INT scale;
3045 rtx op = *curr_id->operand_loc[nop];
3046 const char *constraint = curr_static_id->operand[nop].constraint;
3047 enum constraint_num cn = lookup_constraint (constraint);
3048 bool change_p = false;
3050 if (MEM_P (op)
3051 && GET_MODE (op) == BLKmode
3052 && GET_CODE (XEXP (op, 0)) == SCRATCH)
3053 return false;
3055 if (insn_extra_address_constraint (cn))
3056 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
3057 else if (MEM_P (op))
3058 decompose_mem_address (&ad, op);
3059 else if (GET_CODE (op) == SUBREG
3060 && MEM_P (SUBREG_REG (op)))
3061 decompose_mem_address (&ad, SUBREG_REG (op));
3062 else
3063 return false;
3064 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
3065 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
3066 when INDEX_REG_CLASS is a single register class. */
3067 if (ad.base_term != NULL
3068 && ad.index_term != NULL
3069 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
3070 && REG_P (*ad.base_term)
3071 && REG_P (*ad.index_term)
3072 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
3073 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
3075 std::swap (ad.base, ad.index);
3076 std::swap (ad.base_term, ad.index_term);
3078 if (! check_only_p)
3079 change_p = equiv_address_substitution (&ad);
3080 if (ad.base_term != NULL
3081 && (process_addr_reg
3082 (ad.base_term, check_only_p, before,
3083 (ad.autoinc_p
3084 && !(REG_P (*ad.base_term)
3085 && find_regno_note (curr_insn, REG_DEAD,
3086 REGNO (*ad.base_term)) != NULL_RTX)
3087 ? after : NULL),
3088 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3089 get_index_code (&ad)))))
3091 change_p = true;
3092 if (ad.base_term2 != NULL)
3093 *ad.base_term2 = *ad.base_term;
3095 if (ad.index_term != NULL
3096 && process_addr_reg (ad.index_term, check_only_p,
3097 before, NULL, INDEX_REG_CLASS))
3098 change_p = true;
3100 /* Target hooks sometimes don't treat extra-constraint addresses as
3101 legitimate address_operands, so handle them specially. */
3102 if (insn_extra_address_constraint (cn)
3103 && satisfies_address_constraint_p (&ad, cn))
3104 return change_p;
3106 if (check_only_p)
3107 return change_p;
3109 /* There are three cases where the shape of *AD.INNER may now be invalid:
3111 1) the original address was valid, but either elimination or
3112 equiv_address_substitution was applied and that made
3113 the address invalid.
3115 2) the address is an invalid symbolic address created by
3116 force_const_to_mem.
3118 3) the address is a frame address with an invalid offset.
3120 4) the address is a frame address with an invalid base.
3122 All these cases involve a non-autoinc address, so there is no
3123 point revalidating other types. */
3124 if (ad.autoinc_p || valid_address_p (&ad))
3125 return change_p;
3127 /* Any index existed before LRA started, so we can assume that the
3128 presence and shape of the index is valid. */
3129 push_to_sequence (*before);
3130 lra_assert (ad.disp == ad.disp_term);
3131 if (ad.base == NULL)
3133 if (ad.index == NULL)
3135 rtx_insn *insn;
3136 rtx_insn *last = get_last_insn ();
3137 int code = -1;
3138 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3139 SCRATCH, SCRATCH);
3140 rtx addr = *ad.inner;
3142 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3143 if (HAVE_lo_sum)
3145 /* addr => lo_sum (new_base, addr), case (2) above. */
3146 insn = emit_insn (gen_rtx_SET
3147 (new_reg,
3148 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
3149 code = recog_memoized (insn);
3150 if (code >= 0)
3152 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
3153 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
3155 /* Try to put lo_sum into register. */
3156 insn = emit_insn (gen_rtx_SET
3157 (new_reg,
3158 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
3159 code = recog_memoized (insn);
3160 if (code >= 0)
3162 *ad.inner = new_reg;
3163 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
3165 *ad.inner = addr;
3166 code = -1;
3172 if (code < 0)
3173 delete_insns_since (last);
3176 if (code < 0)
3178 /* addr => new_base, case (2) above. */
3179 lra_emit_move (new_reg, addr);
3181 for (insn = last == NULL_RTX ? get_insns () : NEXT_INSN (last);
3182 insn != NULL_RTX;
3183 insn = NEXT_INSN (insn))
3184 if (recog_memoized (insn) < 0)
3185 break;
3186 if (insn != NULL_RTX)
3188 /* Do nothing if we cannot generate right insns.
3189 This is analogous to reload pass behavior. */
3190 delete_insns_since (last);
3191 end_sequence ();
3192 return false;
3194 *ad.inner = new_reg;
3197 else
3199 /* index * scale + disp => new base + index * scale,
3200 case (1) above. */
3201 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3202 GET_CODE (*ad.index));
3204 lra_assert (INDEX_REG_CLASS != NO_REGS);
3205 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3206 lra_emit_move (new_reg, *ad.disp);
3207 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3208 new_reg, *ad.index);
3211 else if (ad.index == NULL)
3213 int regno;
3214 enum reg_class cl;
3215 rtx set;
3216 rtx_insn *insns, *last_insn;
3217 /* Try to reload base into register only if the base is invalid
3218 for the address but with valid offset, case (4) above. */
3219 start_sequence ();
3220 new_reg = base_to_reg (&ad);
3222 /* base + disp => new base, cases (1) and (3) above. */
3223 /* Another option would be to reload the displacement into an
3224 index register. However, postreload has code to optimize
3225 address reloads that have the same base and different
3226 displacements, so reloading into an index register would
3227 not necessarily be a win. */
3228 if (new_reg == NULL_RTX)
3229 new_reg = base_plus_disp_to_reg (&ad);
3230 insns = get_insns ();
3231 last_insn = get_last_insn ();
3232 /* If we generated at least two insns, try last insn source as
3233 an address. If we succeed, we generate one less insn. */
3234 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3235 && GET_CODE (SET_SRC (set)) == PLUS
3236 && REG_P (XEXP (SET_SRC (set), 0))
3237 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3239 *ad.inner = SET_SRC (set);
3240 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3242 *ad.base_term = XEXP (SET_SRC (set), 0);
3243 *ad.disp_term = XEXP (SET_SRC (set), 1);
3244 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3245 get_index_code (&ad));
3246 regno = REGNO (*ad.base_term);
3247 if (regno >= FIRST_PSEUDO_REGISTER
3248 && cl != lra_get_allocno_class (regno))
3249 lra_change_class (regno, cl, " Change to", true);
3250 new_reg = SET_SRC (set);
3251 delete_insns_since (PREV_INSN (last_insn));
3254 /* Try if target can split displacement into legitimite new disp
3255 and offset. If it's the case, we replace the last insn with
3256 insns for base + offset => new_reg and set new_reg + new disp
3257 to *ad.inner. */
3258 last_insn = get_last_insn ();
3259 if ((set = single_set (last_insn)) != NULL_RTX
3260 && GET_CODE (SET_SRC (set)) == PLUS
3261 && REG_P (XEXP (SET_SRC (set), 0))
3262 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3263 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3265 rtx addend, disp = XEXP (SET_SRC (set), 1);
3266 if (targetm.legitimize_address_displacement (&disp, &addend,
3267 ad.mode))
3269 rtx_insn *new_insns;
3270 start_sequence ();
3271 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3272 new_insns = get_insns ();
3273 end_sequence ();
3274 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3275 delete_insns_since (PREV_INSN (last_insn));
3276 add_insn (new_insns);
3277 insns = get_insns ();
3280 end_sequence ();
3281 emit_insn (insns);
3282 *ad.inner = new_reg;
3284 else if (ad.disp_term != NULL)
3286 /* base + scale * index + disp => new base + scale * index,
3287 case (1) above. */
3288 new_reg = base_plus_disp_to_reg (&ad);
3289 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3290 new_reg, *ad.index);
3292 else if ((scale = get_index_scale (&ad)) == 1)
3294 /* The last transformation to one reg will be made in
3295 curr_insn_transform function. */
3296 end_sequence ();
3297 return false;
3299 else if (scale != 0)
3301 /* base + scale * index => base + new_reg,
3302 case (1) above.
3303 Index part of address may become invalid. For example, we
3304 changed pseudo on the equivalent memory and a subreg of the
3305 pseudo onto the memory of different mode for which the scale is
3306 prohibitted. */
3307 new_reg = index_part_to_reg (&ad);
3308 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3309 *ad.base_term, new_reg);
3311 else
3313 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3314 SCRATCH, SCRATCH);
3315 rtx addr = *ad.inner;
3317 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3318 /* addr => new_base. */
3319 lra_emit_move (new_reg, addr);
3320 *ad.inner = new_reg;
3322 *before = get_insns ();
3323 end_sequence ();
3324 return true;
3327 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3328 Use process_address_1 as a helper function. Return true for any
3329 RTL changes.
3331 If CHECK_ONLY_P is true, just check address correctness. Return
3332 false if the address correct. */
3333 static bool
3334 process_address (int nop, bool check_only_p,
3335 rtx_insn **before, rtx_insn **after)
3337 bool res = false;
3339 while (process_address_1 (nop, check_only_p, before, after))
3341 if (check_only_p)
3342 return true;
3343 res = true;
3345 return res;
3348 /* Emit insns to reload VALUE into a new register. VALUE is an
3349 auto-increment or auto-decrement RTX whose operand is a register or
3350 memory location; so reloading involves incrementing that location.
3351 IN is either identical to VALUE, or some cheaper place to reload
3352 value being incremented/decremented from.
3354 INC_AMOUNT is the number to increment or decrement by (always
3355 positive and ignored for POST_MODIFY/PRE_MODIFY).
3357 Return pseudo containing the result. */
3358 static rtx
3359 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3361 /* REG or MEM to be copied and incremented. */
3362 rtx incloc = XEXP (value, 0);
3363 /* Nonzero if increment after copying. */
3364 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3365 || GET_CODE (value) == POST_MODIFY);
3366 rtx_insn *last;
3367 rtx inc;
3368 rtx_insn *add_insn;
3369 int code;
3370 rtx real_in = in == value ? incloc : in;
3371 rtx result;
3372 bool plus_p = true;
3374 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3376 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3377 || GET_CODE (XEXP (value, 1)) == MINUS);
3378 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3379 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3380 inc = XEXP (XEXP (value, 1), 1);
3382 else
3384 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3385 inc_amount = -inc_amount;
3387 inc = GEN_INT (inc_amount);
3390 if (! post && REG_P (incloc))
3391 result = incloc;
3392 else
3393 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3394 "INC/DEC result");
3396 if (real_in != result)
3398 /* First copy the location to the result register. */
3399 lra_assert (REG_P (result));
3400 emit_insn (gen_move_insn (result, real_in));
3403 /* We suppose that there are insns to add/sub with the constant
3404 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3405 old reload worked with this assumption. If the assumption
3406 becomes wrong, we should use approach in function
3407 base_plus_disp_to_reg. */
3408 if (in == value)
3410 /* See if we can directly increment INCLOC. */
3411 last = get_last_insn ();
3412 add_insn = emit_insn (plus_p
3413 ? gen_add2_insn (incloc, inc)
3414 : gen_sub2_insn (incloc, inc));
3416 code = recog_memoized (add_insn);
3417 if (code >= 0)
3419 if (! post && result != incloc)
3420 emit_insn (gen_move_insn (result, incloc));
3421 return result;
3423 delete_insns_since (last);
3426 /* If couldn't do the increment directly, must increment in RESULT.
3427 The way we do this depends on whether this is pre- or
3428 post-increment. For pre-increment, copy INCLOC to the reload
3429 register, increment it there, then save back. */
3430 if (! post)
3432 if (real_in != result)
3433 emit_insn (gen_move_insn (result, real_in));
3434 if (plus_p)
3435 emit_insn (gen_add2_insn (result, inc));
3436 else
3437 emit_insn (gen_sub2_insn (result, inc));
3438 if (result != incloc)
3439 emit_insn (gen_move_insn (incloc, result));
3441 else
3443 /* Post-increment.
3445 Because this might be a jump insn or a compare, and because
3446 RESULT may not be available after the insn in an input
3447 reload, we must do the incrementing before the insn being
3448 reloaded for.
3450 We have already copied IN to RESULT. Increment the copy in
3451 RESULT, save that back, then decrement RESULT so it has
3452 the original value. */
3453 if (plus_p)
3454 emit_insn (gen_add2_insn (result, inc));
3455 else
3456 emit_insn (gen_sub2_insn (result, inc));
3457 emit_insn (gen_move_insn (incloc, result));
3458 /* Restore non-modified value for the result. We prefer this
3459 way because it does not require an additional hard
3460 register. */
3461 if (plus_p)
3463 if (CONST_INT_P (inc))
3464 emit_insn (gen_add2_insn (result,
3465 gen_int_mode (-INTVAL (inc),
3466 GET_MODE (result))));
3467 else
3468 emit_insn (gen_sub2_insn (result, inc));
3470 else
3471 emit_insn (gen_add2_insn (result, inc));
3473 return result;
3476 /* Return true if the current move insn does not need processing as we
3477 already know that it satisfies its constraints. */
3478 static bool
3479 simple_move_p (void)
3481 rtx dest, src;
3482 enum reg_class dclass, sclass;
3484 lra_assert (curr_insn_set != NULL_RTX);
3485 dest = SET_DEST (curr_insn_set);
3486 src = SET_SRC (curr_insn_set);
3488 /* If the instruction has multiple sets we need to process it even if it
3489 is single_set. This can happen if one or more of the SETs are dead.
3490 See PR73650. */
3491 if (multiple_sets (curr_insn))
3492 return false;
3494 return ((dclass = get_op_class (dest)) != NO_REGS
3495 && (sclass = get_op_class (src)) != NO_REGS
3496 /* The backend guarantees that register moves of cost 2
3497 never need reloads. */
3498 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3501 /* Swap operands NOP and NOP + 1. */
3502 static inline void
3503 swap_operands (int nop)
3505 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3506 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3507 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3508 std::swap (equiv_substition_p[nop], equiv_substition_p[nop + 1]);
3509 /* Swap the duplicates too. */
3510 lra_update_dup (curr_id, nop);
3511 lra_update_dup (curr_id, nop + 1);
3514 /* Main entry point of the constraint code: search the body of the
3515 current insn to choose the best alternative. It is mimicking insn
3516 alternative cost calculation model of former reload pass. That is
3517 because machine descriptions were written to use this model. This
3518 model can be changed in future. Make commutative operand exchange
3519 if it is chosen.
3521 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3522 constraints. Return true if any change happened during function
3523 call.
3525 If CHECK_ONLY_P is true then don't do any transformation. Just
3526 check that the insn satisfies all constraints. If the insn does
3527 not satisfy any constraint, return true. */
3528 static bool
3529 curr_insn_transform (bool check_only_p)
3531 int i, j, k;
3532 int n_operands;
3533 int n_alternatives;
3534 int n_outputs;
3535 int commutative;
3536 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3537 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3538 signed char outputs[MAX_RECOG_OPERANDS + 1];
3539 rtx_insn *before, *after;
3540 bool alt_p = false;
3541 /* Flag that the insn has been changed through a transformation. */
3542 bool change_p;
3543 bool sec_mem_p;
3544 #ifdef SECONDARY_MEMORY_NEEDED
3545 bool use_sec_mem_p;
3546 #endif
3547 int max_regno_before;
3548 int reused_alternative_num;
3550 curr_insn_set = single_set (curr_insn);
3551 if (curr_insn_set != NULL_RTX && simple_move_p ())
3552 return false;
3554 no_input_reloads_p = no_output_reloads_p = false;
3555 goal_alt_number = -1;
3556 change_p = sec_mem_p = false;
3557 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3558 reloads; neither are insns that SET cc0. Insns that use CC0 are
3559 not allowed to have any input reloads. */
3560 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3561 no_output_reloads_p = true;
3563 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3564 no_input_reloads_p = true;
3565 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3566 no_output_reloads_p = true;
3568 n_operands = curr_static_id->n_operands;
3569 n_alternatives = curr_static_id->n_alternatives;
3571 /* Just return "no reloads" if insn has no operands with
3572 constraints. */
3573 if (n_operands == 0 || n_alternatives == 0)
3574 return false;
3576 max_regno_before = max_reg_num ();
3578 for (i = 0; i < n_operands; i++)
3580 goal_alt_matched[i][0] = -1;
3581 goal_alt_matches[i] = -1;
3584 commutative = curr_static_id->commutative;
3586 /* Now see what we need for pseudos that didn't get hard regs or got
3587 the wrong kind of hard reg. For this, we must consider all the
3588 operands together against the register constraints. */
3590 best_losers = best_overall = INT_MAX;
3591 best_reload_sum = 0;
3593 curr_swapped = false;
3594 goal_alt_swapped = false;
3596 if (! check_only_p)
3597 /* Make equivalence substitution and memory subreg elimination
3598 before address processing because an address legitimacy can
3599 depend on memory mode. */
3600 for (i = 0; i < n_operands; i++)
3602 rtx op, subst, old;
3603 bool op_change_p = false;
3605 if (curr_static_id->operand[i].is_operator)
3606 continue;
3608 old = op = *curr_id->operand_loc[i];
3609 if (GET_CODE (old) == SUBREG)
3610 old = SUBREG_REG (old);
3611 subst = get_equiv_with_elimination (old, curr_insn);
3612 original_subreg_reg_mode[i] = VOIDmode;
3613 equiv_substition_p[i] = false;
3614 if (subst != old)
3616 equiv_substition_p[i] = true;
3617 subst = copy_rtx (subst);
3618 lra_assert (REG_P (old));
3619 if (GET_CODE (op) != SUBREG)
3620 *curr_id->operand_loc[i] = subst;
3621 else
3623 SUBREG_REG (op) = subst;
3624 if (GET_MODE (subst) == VOIDmode)
3625 original_subreg_reg_mode[i] = GET_MODE (old);
3627 if (lra_dump_file != NULL)
3629 fprintf (lra_dump_file,
3630 "Changing pseudo %d in operand %i of insn %u on equiv ",
3631 REGNO (old), i, INSN_UID (curr_insn));
3632 dump_value_slim (lra_dump_file, subst, 1);
3633 fprintf (lra_dump_file, "\n");
3635 op_change_p = change_p = true;
3637 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3639 change_p = true;
3640 lra_update_dup (curr_id, i);
3644 /* Reload address registers and displacements. We do it before
3645 finding an alternative because of memory constraints. */
3646 before = after = NULL;
3647 for (i = 0; i < n_operands; i++)
3648 if (! curr_static_id->operand[i].is_operator
3649 && process_address (i, check_only_p, &before, &after))
3651 if (check_only_p)
3652 return true;
3653 change_p = true;
3654 lra_update_dup (curr_id, i);
3657 if (change_p)
3658 /* If we've changed the instruction then any alternative that
3659 we chose previously may no longer be valid. */
3660 lra_set_used_insn_alternative (curr_insn, -1);
3662 if (! check_only_p && curr_insn_set != NULL_RTX
3663 && check_and_process_move (&change_p, &sec_mem_p))
3664 return change_p;
3666 try_swapped:
3668 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3669 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3670 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3671 reused_alternative_num, INSN_UID (curr_insn));
3673 if (process_alt_operands (reused_alternative_num))
3674 alt_p = true;
3676 if (check_only_p)
3677 return ! alt_p || best_losers != 0;
3679 /* If insn is commutative (it's safe to exchange a certain pair of
3680 operands) then we need to try each alternative twice, the second
3681 time matching those two operands as if we had exchanged them. To
3682 do this, really exchange them in operands.
3684 If we have just tried the alternatives the second time, return
3685 operands to normal and drop through. */
3687 if (reused_alternative_num < 0 && commutative >= 0)
3689 curr_swapped = !curr_swapped;
3690 if (curr_swapped)
3692 swap_operands (commutative);
3693 goto try_swapped;
3695 else
3696 swap_operands (commutative);
3699 if (! alt_p && ! sec_mem_p)
3701 /* No alternative works with reloads?? */
3702 if (INSN_CODE (curr_insn) >= 0)
3703 fatal_insn ("unable to generate reloads for:", curr_insn);
3704 error_for_asm (curr_insn,
3705 "inconsistent operand constraints in an %<asm%>");
3706 /* Avoid further trouble with this insn. */
3707 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3708 lra_invalidate_insn_data (curr_insn);
3709 return true;
3712 /* If the best alternative is with operands 1 and 2 swapped, swap
3713 them. Update the operand numbers of any reloads already
3714 pushed. */
3716 if (goal_alt_swapped)
3718 if (lra_dump_file != NULL)
3719 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3720 INSN_UID (curr_insn));
3722 /* Swap the duplicates too. */
3723 swap_operands (commutative);
3724 change_p = true;
3727 #ifdef SECONDARY_MEMORY_NEEDED
3728 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3729 too conservatively. So we use the secondary memory only if there
3730 is no any alternative without reloads. */
3731 use_sec_mem_p = false;
3732 if (! alt_p)
3733 use_sec_mem_p = true;
3734 else if (sec_mem_p)
3736 for (i = 0; i < n_operands; i++)
3737 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3738 break;
3739 use_sec_mem_p = i < n_operands;
3742 if (use_sec_mem_p)
3744 int in = -1, out = -1;
3745 rtx new_reg, src, dest, rld;
3746 machine_mode sec_mode, rld_mode;
3748 lra_assert (curr_insn_set != NULL_RTX && sec_mem_p);
3749 dest = SET_DEST (curr_insn_set);
3750 src = SET_SRC (curr_insn_set);
3751 for (i = 0; i < n_operands; i++)
3752 if (*curr_id->operand_loc[i] == dest)
3753 out = i;
3754 else if (*curr_id->operand_loc[i] == src)
3755 in = i;
3756 for (i = 0; i < curr_static_id->n_dups; i++)
3757 if (out < 0 && *curr_id->dup_loc[i] == dest)
3758 out = curr_static_id->dup_num[i];
3759 else if (in < 0 && *curr_id->dup_loc[i] == src)
3760 in = curr_static_id->dup_num[i];
3761 lra_assert (out >= 0 && in >= 0
3762 && curr_static_id->operand[out].type == OP_OUT
3763 && curr_static_id->operand[in].type == OP_IN);
3764 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3765 ? dest : src);
3766 rld_mode = GET_MODE (rld);
3767 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3768 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3769 #else
3770 sec_mode = rld_mode;
3771 #endif
3772 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3773 NO_REGS, "secondary");
3774 /* If the mode is changed, it should be wider. */
3775 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3776 if (sec_mode != rld_mode)
3778 /* If the target says specifically to use another mode for
3779 secondary memory moves we can not reuse the original
3780 insn. */
3781 after = emit_spill_move (false, new_reg, dest);
3782 lra_process_new_insns (curr_insn, NULL, after,
3783 "Inserting the sec. move");
3784 /* We may have non null BEFORE here (e.g. after address
3785 processing. */
3786 push_to_sequence (before);
3787 before = emit_spill_move (true, new_reg, src);
3788 emit_insn (before);
3789 before = get_insns ();
3790 end_sequence ();
3791 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3792 lra_set_insn_deleted (curr_insn);
3794 else if (dest == rld)
3796 *curr_id->operand_loc[out] = new_reg;
3797 lra_update_dup (curr_id, out);
3798 after = emit_spill_move (false, new_reg, dest);
3799 lra_process_new_insns (curr_insn, NULL, after,
3800 "Inserting the sec. move");
3802 else
3804 *curr_id->operand_loc[in] = new_reg;
3805 lra_update_dup (curr_id, in);
3806 /* See comments above. */
3807 push_to_sequence (before);
3808 before = emit_spill_move (true, new_reg, src);
3809 emit_insn (before);
3810 before = get_insns ();
3811 end_sequence ();
3812 lra_process_new_insns (curr_insn, before, NULL,
3813 "Inserting the sec. move");
3815 lra_update_insn_regno_info (curr_insn);
3816 return true;
3818 #endif
3820 lra_assert (goal_alt_number >= 0);
3821 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3823 if (lra_dump_file != NULL)
3825 const char *p;
3827 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3828 goal_alt_number, INSN_UID (curr_insn));
3829 for (i = 0; i < n_operands; i++)
3831 p = (curr_static_id->operand_alternative
3832 [goal_alt_number * n_operands + i].constraint);
3833 if (*p == '\0')
3834 continue;
3835 fprintf (lra_dump_file, " (%d) ", i);
3836 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3837 fputc (*p, lra_dump_file);
3839 if (INSN_CODE (curr_insn) >= 0
3840 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3841 fprintf (lra_dump_file, " {%s}", p);
3842 if (curr_id->sp_offset != 0)
3843 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3844 curr_id->sp_offset);
3845 fprintf (lra_dump_file, "\n");
3848 /* Right now, for any pair of operands I and J that are required to
3849 match, with J < I, goal_alt_matches[I] is J. Add I to
3850 goal_alt_matched[J]. */
3852 for (i = 0; i < n_operands; i++)
3853 if ((j = goal_alt_matches[i]) >= 0)
3855 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3857 /* We allow matching one output operand and several input
3858 operands. */
3859 lra_assert (k == 0
3860 || (curr_static_id->operand[j].type == OP_OUT
3861 && curr_static_id->operand[i].type == OP_IN
3862 && (curr_static_id->operand
3863 [goal_alt_matched[j][0]].type == OP_IN)));
3864 goal_alt_matched[j][k] = i;
3865 goal_alt_matched[j][k + 1] = -1;
3868 for (i = 0; i < n_operands; i++)
3869 goal_alt_win[i] |= goal_alt_match_win[i];
3871 /* Any constants that aren't allowed and can't be reloaded into
3872 registers are here changed into memory references. */
3873 for (i = 0; i < n_operands; i++)
3874 if (goal_alt_win[i])
3876 int regno;
3877 enum reg_class new_class;
3878 rtx reg = *curr_id->operand_loc[i];
3880 if (GET_CODE (reg) == SUBREG)
3881 reg = SUBREG_REG (reg);
3883 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3885 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3887 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3889 lra_assert (ok_p);
3890 lra_change_class (regno, new_class, " Change to", true);
3894 else
3896 const char *constraint;
3897 char c;
3898 rtx op = *curr_id->operand_loc[i];
3899 rtx subreg = NULL_RTX;
3900 machine_mode mode = curr_operand_mode[i];
3902 if (GET_CODE (op) == SUBREG)
3904 subreg = op;
3905 op = SUBREG_REG (op);
3906 mode = GET_MODE (op);
3909 if (CONST_POOL_OK_P (mode, op)
3910 && ((targetm.preferred_reload_class
3911 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3912 || no_input_reloads_p))
3914 rtx tem = force_const_mem (mode, op);
3916 change_p = true;
3917 if (subreg != NULL_RTX)
3918 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3920 *curr_id->operand_loc[i] = tem;
3921 lra_update_dup (curr_id, i);
3922 process_address (i, false, &before, &after);
3924 /* If the alternative accepts constant pool refs directly
3925 there will be no reload needed at all. */
3926 if (subreg != NULL_RTX)
3927 continue;
3928 /* Skip alternatives before the one requested. */
3929 constraint = (curr_static_id->operand_alternative
3930 [goal_alt_number * n_operands + i].constraint);
3931 for (;
3932 (c = *constraint) && c != ',' && c != '#';
3933 constraint += CONSTRAINT_LEN (c, constraint))
3935 enum constraint_num cn = lookup_constraint (constraint);
3936 if ((insn_extra_memory_constraint (cn)
3937 || insn_extra_special_memory_constraint (cn))
3938 && satisfies_memory_constraint_p (tem, cn))
3939 break;
3941 if (c == '\0' || c == ',' || c == '#')
3942 continue;
3944 goal_alt_win[i] = true;
3948 n_outputs = 0;
3949 outputs[0] = -1;
3950 for (i = 0; i < n_operands; i++)
3952 int regno;
3953 bool optional_p = false;
3954 rtx old, new_reg;
3955 rtx op = *curr_id->operand_loc[i];
3957 if (goal_alt_win[i])
3959 if (goal_alt[i] == NO_REGS
3960 && REG_P (op)
3961 /* When we assign NO_REGS it means that we will not
3962 assign a hard register to the scratch pseudo by
3963 assigment pass and the scratch pseudo will be
3964 spilled. Spilled scratch pseudos are transformed
3965 back to scratches at the LRA end. */
3966 && lra_former_scratch_operand_p (curr_insn, i)
3967 && lra_former_scratch_p (REGNO (op)))
3969 int regno = REGNO (op);
3970 lra_change_class (regno, NO_REGS, " Change to", true);
3971 if (lra_get_regno_hard_regno (regno) >= 0)
3972 /* We don't have to mark all insn affected by the
3973 spilled pseudo as there is only one such insn, the
3974 current one. */
3975 reg_renumber[regno] = -1;
3976 lra_assert (bitmap_single_bit_set_p
3977 (&lra_reg_info[REGNO (op)].insn_bitmap));
3979 /* We can do an optional reload. If the pseudo got a hard
3980 reg, we might improve the code through inheritance. If
3981 it does not get a hard register we coalesce memory/memory
3982 moves later. Ignore move insns to avoid cycling. */
3983 if (! lra_simple_p
3984 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3985 && goal_alt[i] != NO_REGS && REG_P (op)
3986 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3987 && regno < new_regno_start
3988 && ! lra_former_scratch_p (regno)
3989 && reg_renumber[regno] < 0
3990 /* Check that the optional reload pseudo will be able to
3991 hold given mode value. */
3992 && ! (prohibited_class_reg_set_mode_p
3993 (goal_alt[i], reg_class_contents[goal_alt[i]],
3994 PSEUDO_REGNO_MODE (regno)))
3995 && (curr_insn_set == NULL_RTX
3996 || !((REG_P (SET_SRC (curr_insn_set))
3997 || MEM_P (SET_SRC (curr_insn_set))
3998 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3999 && (REG_P (SET_DEST (curr_insn_set))
4000 || MEM_P (SET_DEST (curr_insn_set))
4001 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
4002 optional_p = true;
4003 else
4004 continue;
4007 /* Operands that match previous ones have already been handled. */
4008 if (goal_alt_matches[i] >= 0)
4009 continue;
4011 /* We should not have an operand with a non-offsettable address
4012 appearing where an offsettable address will do. It also may
4013 be a case when the address should be special in other words
4014 not a general one (e.g. it needs no index reg). */
4015 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
4017 enum reg_class rclass;
4018 rtx *loc = &XEXP (op, 0);
4019 enum rtx_code code = GET_CODE (*loc);
4021 push_to_sequence (before);
4022 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
4023 MEM, SCRATCH);
4024 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
4025 new_reg = emit_inc (rclass, *loc, *loc,
4026 /* This value does not matter for MODIFY. */
4027 GET_MODE_SIZE (GET_MODE (op)));
4028 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
4029 "offsetable address", &new_reg))
4030 lra_emit_move (new_reg, *loc);
4031 before = get_insns ();
4032 end_sequence ();
4033 *loc = new_reg;
4034 lra_update_dup (curr_id, i);
4036 else if (goal_alt_matched[i][0] == -1)
4038 machine_mode mode;
4039 rtx reg, *loc;
4040 int hard_regno, byte;
4041 enum op_type type = curr_static_id->operand[i].type;
4043 loc = curr_id->operand_loc[i];
4044 mode = curr_operand_mode[i];
4045 if (GET_CODE (*loc) == SUBREG)
4047 reg = SUBREG_REG (*loc);
4048 byte = SUBREG_BYTE (*loc);
4049 if (REG_P (reg)
4050 /* Strict_low_part requires reload the register not
4051 the sub-register. */
4052 && (curr_static_id->operand[i].strict_low
4053 || (GET_MODE_SIZE (mode)
4054 <= GET_MODE_SIZE (GET_MODE (reg))
4055 && (hard_regno
4056 = get_try_hard_regno (REGNO (reg))) >= 0
4057 && (simplify_subreg_regno
4058 (hard_regno,
4059 GET_MODE (reg), byte, mode) < 0)
4060 && (goal_alt[i] == NO_REGS
4061 || (simplify_subreg_regno
4062 (ira_class_hard_regs[goal_alt[i]][0],
4063 GET_MODE (reg), byte, mode) >= 0)))))
4065 if (type == OP_OUT)
4066 type = OP_INOUT;
4067 loc = &SUBREG_REG (*loc);
4068 mode = GET_MODE (*loc);
4071 old = *loc;
4072 if (get_reload_reg (type, mode, old, goal_alt[i],
4073 loc != curr_id->operand_loc[i], "", &new_reg)
4074 && type != OP_OUT)
4076 push_to_sequence (before);
4077 lra_emit_move (new_reg, old);
4078 before = get_insns ();
4079 end_sequence ();
4081 *loc = new_reg;
4082 if (type != OP_IN
4083 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
4085 start_sequence ();
4086 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
4087 emit_insn (after);
4088 after = get_insns ();
4089 end_sequence ();
4090 *loc = new_reg;
4092 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
4093 if (goal_alt_dont_inherit_ops[j] == i)
4095 lra_set_regno_unique_value (REGNO (new_reg));
4096 break;
4098 lra_update_dup (curr_id, i);
4100 else if (curr_static_id->operand[i].type == OP_IN
4101 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4102 == OP_OUT))
4104 /* generate reloads for input and matched outputs. */
4105 match_inputs[0] = i;
4106 match_inputs[1] = -1;
4107 match_reload (goal_alt_matched[i][0], match_inputs, outputs,
4108 goal_alt[i], &before, &after,
4109 curr_static_id->operand_alternative
4110 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
4111 .earlyclobber);
4113 else if (curr_static_id->operand[i].type == OP_OUT
4114 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4115 == OP_IN))
4116 /* Generate reloads for output and matched inputs. */
4117 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i], &before,
4118 &after, curr_static_id->operand_alternative
4119 [goal_alt_number * n_operands + i].earlyclobber);
4120 else if (curr_static_id->operand[i].type == OP_IN
4121 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4122 == OP_IN))
4124 /* Generate reloads for matched inputs. */
4125 match_inputs[0] = i;
4126 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
4127 match_inputs[j + 1] = k;
4128 match_inputs[j + 1] = -1;
4129 match_reload (-1, match_inputs, outputs, goal_alt[i], &before,
4130 &after, false);
4132 else
4133 /* We must generate code in any case when function
4134 process_alt_operands decides that it is possible. */
4135 gcc_unreachable ();
4137 /* Memorise processed outputs so that output remaining to be processed
4138 can avoid using the same register value (see match_reload). */
4139 if (curr_static_id->operand[i].type == OP_OUT)
4141 outputs[n_outputs++] = i;
4142 outputs[n_outputs] = -1;
4145 if (optional_p)
4147 rtx reg = op;
4149 lra_assert (REG_P (reg));
4150 regno = REGNO (reg);
4151 op = *curr_id->operand_loc[i]; /* Substitution. */
4152 if (GET_CODE (op) == SUBREG)
4153 op = SUBREG_REG (op);
4154 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
4155 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
4156 lra_reg_info[REGNO (op)].restore_rtx = reg;
4157 if (lra_dump_file != NULL)
4158 fprintf (lra_dump_file,
4159 " Making reload reg %d for reg %d optional\n",
4160 REGNO (op), regno);
4163 if (before != NULL_RTX || after != NULL_RTX
4164 || max_regno_before != max_reg_num ())
4165 change_p = true;
4166 if (change_p)
4168 lra_update_operator_dups (curr_id);
4169 /* Something changes -- process the insn. */
4170 lra_update_insn_regno_info (curr_insn);
4172 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
4173 return change_p;
4176 /* Return true if INSN satisfies all constraints. In other words, no
4177 reload insns are needed. */
4178 bool
4179 lra_constrain_insn (rtx_insn *insn)
4181 int saved_new_regno_start = new_regno_start;
4182 int saved_new_insn_uid_start = new_insn_uid_start;
4183 bool change_p;
4185 curr_insn = insn;
4186 curr_id = lra_get_insn_recog_data (curr_insn);
4187 curr_static_id = curr_id->insn_static_data;
4188 new_insn_uid_start = get_max_uid ();
4189 new_regno_start = max_reg_num ();
4190 change_p = curr_insn_transform (true);
4191 new_regno_start = saved_new_regno_start;
4192 new_insn_uid_start = saved_new_insn_uid_start;
4193 return ! change_p;
4196 /* Return true if X is in LIST. */
4197 static bool
4198 in_list_p (rtx x, rtx list)
4200 for (; list != NULL_RTX; list = XEXP (list, 1))
4201 if (XEXP (list, 0) == x)
4202 return true;
4203 return false;
4206 /* Return true if X contains an allocatable hard register (if
4207 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
4208 static bool
4209 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
4211 int i, j;
4212 const char *fmt;
4213 enum rtx_code code;
4215 code = GET_CODE (x);
4216 if (REG_P (x))
4218 int regno = REGNO (x);
4219 HARD_REG_SET alloc_regs;
4221 if (hard_reg_p)
4223 if (regno >= FIRST_PSEUDO_REGISTER)
4224 regno = lra_get_regno_hard_regno (regno);
4225 if (regno < 0)
4226 return false;
4227 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
4228 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
4230 else
4232 if (regno < FIRST_PSEUDO_REGISTER)
4233 return false;
4234 if (! spilled_p)
4235 return true;
4236 return lra_get_regno_hard_regno (regno) < 0;
4239 fmt = GET_RTX_FORMAT (code);
4240 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4242 if (fmt[i] == 'e')
4244 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4245 return true;
4247 else if (fmt[i] == 'E')
4249 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4250 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4251 return true;
4254 return false;
4257 /* Process all regs in location *LOC and change them on equivalent
4258 substitution. Return true if any change was done. */
4259 static bool
4260 loc_equivalence_change_p (rtx *loc)
4262 rtx subst, reg, x = *loc;
4263 bool result = false;
4264 enum rtx_code code = GET_CODE (x);
4265 const char *fmt;
4266 int i, j;
4268 if (code == SUBREG)
4270 reg = SUBREG_REG (x);
4271 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4272 && GET_MODE (subst) == VOIDmode)
4274 /* We cannot reload debug location. Simplify subreg here
4275 while we know the inner mode. */
4276 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4277 GET_MODE (reg), SUBREG_BYTE (x));
4278 return true;
4281 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4283 *loc = subst;
4284 return true;
4287 /* Scan all the operand sub-expressions. */
4288 fmt = GET_RTX_FORMAT (code);
4289 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4291 if (fmt[i] == 'e')
4292 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4293 else if (fmt[i] == 'E')
4294 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4295 result
4296 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4298 return result;
4301 /* Similar to loc_equivalence_change_p, but for use as
4302 simplify_replace_fn_rtx callback. DATA is insn for which the
4303 elimination is done. If it null we don't do the elimination. */
4304 static rtx
4305 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4307 if (!REG_P (loc))
4308 return NULL_RTX;
4310 rtx subst = (data == NULL
4311 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4312 if (subst != loc)
4313 return subst;
4315 return NULL_RTX;
4318 /* Maximum number of generated reload insns per an insn. It is for
4319 preventing this pass cycling in a bug case. */
4320 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4322 /* The current iteration number of this LRA pass. */
4323 int lra_constraint_iter;
4325 /* True if we substituted equiv which needs checking register
4326 allocation correctness because the equivalent value contains
4327 allocatable hard registers or when we restore multi-register
4328 pseudo. */
4329 bool lra_risky_transformations_p;
4331 /* Return true if REGNO is referenced in more than one block. */
4332 static bool
4333 multi_block_pseudo_p (int regno)
4335 basic_block bb = NULL;
4336 unsigned int uid;
4337 bitmap_iterator bi;
4339 if (regno < FIRST_PSEUDO_REGISTER)
4340 return false;
4342 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4343 if (bb == NULL)
4344 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4345 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4346 return true;
4347 return false;
4350 /* Return true if LIST contains a deleted insn. */
4351 static bool
4352 contains_deleted_insn_p (rtx_insn_list *list)
4354 for (; list != NULL_RTX; list = list->next ())
4355 if (NOTE_P (list->insn ())
4356 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4357 return true;
4358 return false;
4361 /* Return true if X contains a pseudo dying in INSN. */
4362 static bool
4363 dead_pseudo_p (rtx x, rtx_insn *insn)
4365 int i, j;
4366 const char *fmt;
4367 enum rtx_code code;
4369 if (REG_P (x))
4370 return (insn != NULL_RTX
4371 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4372 code = GET_CODE (x);
4373 fmt = GET_RTX_FORMAT (code);
4374 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4376 if (fmt[i] == 'e')
4378 if (dead_pseudo_p (XEXP (x, i), insn))
4379 return true;
4381 else if (fmt[i] == 'E')
4383 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4384 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4385 return true;
4388 return false;
4391 /* Return true if INSN contains a dying pseudo in INSN right hand
4392 side. */
4393 static bool
4394 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4396 rtx set = single_set (insn);
4398 gcc_assert (set != NULL);
4399 return dead_pseudo_p (SET_SRC (set), insn);
4402 /* Return true if any init insn of REGNO contains a dying pseudo in
4403 insn right hand side. */
4404 static bool
4405 init_insn_rhs_dead_pseudo_p (int regno)
4407 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4409 if (insns == NULL)
4410 return false;
4411 for (; insns != NULL_RTX; insns = insns->next ())
4412 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4413 return true;
4414 return false;
4417 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4418 reverse only if we have one init insn with given REGNO as a
4419 source. */
4420 static bool
4421 reverse_equiv_p (int regno)
4423 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4424 rtx set;
4426 if (insns == NULL)
4427 return false;
4428 if (! INSN_P (insns->insn ())
4429 || insns->next () != NULL)
4430 return false;
4431 if ((set = single_set (insns->insn ())) == NULL_RTX)
4432 return false;
4433 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4436 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4437 call this function only for non-reverse equivalence. */
4438 static bool
4439 contains_reloaded_insn_p (int regno)
4441 rtx set;
4442 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4444 for (; list != NULL; list = list->next ())
4445 if ((set = single_set (list->insn ())) == NULL_RTX
4446 || ! REG_P (SET_DEST (set))
4447 || (int) REGNO (SET_DEST (set)) != regno)
4448 return true;
4449 return false;
4452 /* Entry function of LRA constraint pass. Return true if the
4453 constraint pass did change the code. */
4454 bool
4455 lra_constraints (bool first_p)
4457 bool changed_p;
4458 int i, hard_regno, new_insns_num;
4459 unsigned int min_len, new_min_len, uid;
4460 rtx set, x, reg, dest_reg;
4461 basic_block last_bb;
4462 bitmap_head equiv_insn_bitmap;
4463 bitmap_iterator bi;
4465 lra_constraint_iter++;
4466 if (lra_dump_file != NULL)
4467 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4468 lra_constraint_iter);
4469 changed_p = false;
4470 if (pic_offset_table_rtx
4471 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4472 lra_risky_transformations_p = true;
4473 else
4474 lra_risky_transformations_p = false;
4475 new_insn_uid_start = get_max_uid ();
4476 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4477 /* Mark used hard regs for target stack size calulations. */
4478 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4479 if (lra_reg_info[i].nrefs != 0
4480 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4482 int j, nregs;
4484 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4485 for (j = 0; j < nregs; j++)
4486 df_set_regs_ever_live (hard_regno + j, true);
4488 /* Do elimination before the equivalence processing as we can spill
4489 some pseudos during elimination. */
4490 lra_eliminate (false, first_p);
4491 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4492 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4493 if (lra_reg_info[i].nrefs != 0)
4495 ira_reg_equiv[i].profitable_p = true;
4496 reg = regno_reg_rtx[i];
4497 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4499 bool pseudo_p = contains_reg_p (x, false, false);
4501 /* After RTL transformation, we can not guarantee that
4502 pseudo in the substitution was not reloaded which might
4503 make equivalence invalid. For example, in reverse
4504 equiv of p0
4506 p0 <- ...
4508 equiv_mem <- p0
4510 the memory address register was reloaded before the 2nd
4511 insn. */
4512 if ((! first_p && pseudo_p)
4513 /* We don't use DF for compilation speed sake. So it
4514 is problematic to update live info when we use an
4515 equivalence containing pseudos in more than one
4516 BB. */
4517 || (pseudo_p && multi_block_pseudo_p (i))
4518 /* If an init insn was deleted for some reason, cancel
4519 the equiv. We could update the equiv insns after
4520 transformations including an equiv insn deletion
4521 but it is not worthy as such cases are extremely
4522 rare. */
4523 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4524 /* If it is not a reverse equivalence, we check that a
4525 pseudo in rhs of the init insn is not dying in the
4526 insn. Otherwise, the live info at the beginning of
4527 the corresponding BB might be wrong after we
4528 removed the insn. When the equiv can be a
4529 constant, the right hand side of the init insn can
4530 be a pseudo. */
4531 || (! reverse_equiv_p (i)
4532 && (init_insn_rhs_dead_pseudo_p (i)
4533 /* If we reloaded the pseudo in an equivalence
4534 init insn, we can not remove the equiv init
4535 insns and the init insns might write into
4536 const memory in this case. */
4537 || contains_reloaded_insn_p (i)))
4538 /* Prevent access beyond equivalent memory for
4539 paradoxical subregs. */
4540 || (MEM_P (x)
4541 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4542 > GET_MODE_SIZE (GET_MODE (x))))
4543 || (pic_offset_table_rtx
4544 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4545 && (targetm.preferred_reload_class
4546 (x, lra_get_allocno_class (i)) == NO_REGS))
4547 || contains_symbol_ref_p (x))))
4548 ira_reg_equiv[i].defined_p = false;
4549 if (contains_reg_p (x, false, true))
4550 ira_reg_equiv[i].profitable_p = false;
4551 if (get_equiv (reg) != reg)
4552 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4555 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4556 update_equiv (i);
4557 /* We should add all insns containing pseudos which should be
4558 substituted by their equivalences. */
4559 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4560 lra_push_insn_by_uid (uid);
4561 min_len = lra_insn_stack_length ();
4562 new_insns_num = 0;
4563 last_bb = NULL;
4564 changed_p = false;
4565 while ((new_min_len = lra_insn_stack_length ()) != 0)
4567 curr_insn = lra_pop_insn ();
4568 --new_min_len;
4569 curr_bb = BLOCK_FOR_INSN (curr_insn);
4570 if (curr_bb != last_bb)
4572 last_bb = curr_bb;
4573 bb_reload_num = lra_curr_reload_num;
4575 if (min_len > new_min_len)
4577 min_len = new_min_len;
4578 new_insns_num = 0;
4580 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4581 internal_error
4582 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4583 MAX_RELOAD_INSNS_NUMBER);
4584 new_insns_num++;
4585 if (DEBUG_INSN_P (curr_insn))
4587 /* We need to check equivalence in debug insn and change
4588 pseudo to the equivalent value if necessary. */
4589 curr_id = lra_get_insn_recog_data (curr_insn);
4590 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4592 rtx old = *curr_id->operand_loc[0];
4593 *curr_id->operand_loc[0]
4594 = simplify_replace_fn_rtx (old, NULL_RTX,
4595 loc_equivalence_callback, curr_insn);
4596 if (old != *curr_id->operand_loc[0])
4598 lra_update_insn_regno_info (curr_insn);
4599 changed_p = true;
4603 else if (INSN_P (curr_insn))
4605 if ((set = single_set (curr_insn)) != NULL_RTX)
4607 dest_reg = SET_DEST (set);
4608 /* The equivalence pseudo could be set up as SUBREG in a
4609 case when it is a call restore insn in a mode
4610 different from the pseudo mode. */
4611 if (GET_CODE (dest_reg) == SUBREG)
4612 dest_reg = SUBREG_REG (dest_reg);
4613 if ((REG_P (dest_reg)
4614 && (x = get_equiv (dest_reg)) != dest_reg
4615 /* Remove insns which set up a pseudo whose value
4616 can not be changed. Such insns might be not in
4617 init_insns because we don't update equiv data
4618 during insn transformations.
4620 As an example, let suppose that a pseudo got
4621 hard register and on the 1st pass was not
4622 changed to equivalent constant. We generate an
4623 additional insn setting up the pseudo because of
4624 secondary memory movement. Then the pseudo is
4625 spilled and we use the equiv constant. In this
4626 case we should remove the additional insn and
4627 this insn is not init_insns list. */
4628 && (! MEM_P (x) || MEM_READONLY_P (x)
4629 /* Check that this is actually an insn setting
4630 up the equivalence. */
4631 || in_list_p (curr_insn,
4632 ira_reg_equiv
4633 [REGNO (dest_reg)].init_insns)))
4634 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4635 && in_list_p (curr_insn,
4636 ira_reg_equiv
4637 [REGNO (SET_SRC (set))].init_insns)))
4639 /* This is equiv init insn of pseudo which did not get a
4640 hard register -- remove the insn. */
4641 if (lra_dump_file != NULL)
4643 fprintf (lra_dump_file,
4644 " Removing equiv init insn %i (freq=%d)\n",
4645 INSN_UID (curr_insn),
4646 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4647 dump_insn_slim (lra_dump_file, curr_insn);
4649 if (contains_reg_p (x, true, false))
4650 lra_risky_transformations_p = true;
4651 lra_set_insn_deleted (curr_insn);
4652 continue;
4655 curr_id = lra_get_insn_recog_data (curr_insn);
4656 curr_static_id = curr_id->insn_static_data;
4657 init_curr_insn_input_reloads ();
4658 init_curr_operand_mode ();
4659 if (curr_insn_transform (false))
4660 changed_p = true;
4661 /* Check non-transformed insns too for equiv change as USE
4662 or CLOBBER don't need reloads but can contain pseudos
4663 being changed on their equivalences. */
4664 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4665 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4667 lra_update_insn_regno_info (curr_insn);
4668 changed_p = true;
4672 bitmap_clear (&equiv_insn_bitmap);
4673 /* If we used a new hard regno, changed_p should be true because the
4674 hard reg is assigned to a new pseudo. */
4675 if (flag_checking && !changed_p)
4677 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4678 if (lra_reg_info[i].nrefs != 0
4679 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4681 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4683 for (j = 0; j < nregs; j++)
4684 lra_assert (df_regs_ever_live_p (hard_regno + j));
4687 return changed_p;
4690 static void initiate_invariants (void);
4691 static void finish_invariants (void);
4693 /* Initiate the LRA constraint pass. It is done once per
4694 function. */
4695 void
4696 lra_constraints_init (void)
4698 initiate_invariants ();
4701 /* Finalize the LRA constraint pass. It is done once per
4702 function. */
4703 void
4704 lra_constraints_finish (void)
4706 finish_invariants ();
4711 /* Structure describes invariants for ineheritance. */
4712 struct invariant
4714 /* The order number of the invariant. */
4715 int num;
4716 /* The invariant RTX. */
4717 rtx invariant_rtx;
4718 /* The origin insn of the invariant. */
4719 rtx_insn *insn;
4722 typedef struct invariant invariant_t;
4723 typedef invariant_t *invariant_ptr_t;
4724 typedef const invariant_t *const_invariant_ptr_t;
4726 /* Pointer to the inheritance invariants. */
4727 static vec<invariant_ptr_t> invariants;
4729 /* Allocation pool for the invariants. */
4730 static object_allocator<struct invariant> *invariants_pool;
4732 /* Hash table for the invariants. */
4733 static htab_t invariant_table;
4735 /* Hash function for INVARIANT. */
4736 static hashval_t
4737 invariant_hash (const void *invariant)
4739 rtx inv = ((const_invariant_ptr_t) invariant)->invariant_rtx;
4740 return lra_rtx_hash (inv);
4743 /* Equal function for invariants INVARIANT1 and INVARIANT2. */
4744 static int
4745 invariant_eq_p (const void *invariant1, const void *invariant2)
4747 rtx inv1 = ((const_invariant_ptr_t) invariant1)->invariant_rtx;
4748 rtx inv2 = ((const_invariant_ptr_t) invariant2)->invariant_rtx;
4750 return rtx_equal_p (inv1, inv2);
4753 /* Insert INVARIANT_RTX into the table if it is not there yet. Return
4754 invariant which is in the table. */
4755 static invariant_ptr_t
4756 insert_invariant (rtx invariant_rtx)
4758 void **entry_ptr;
4759 invariant_t invariant;
4760 invariant_ptr_t invariant_ptr;
4762 invariant.invariant_rtx = invariant_rtx;
4763 entry_ptr = htab_find_slot (invariant_table, &invariant, INSERT);
4764 if (*entry_ptr == NULL)
4766 invariant_ptr = invariants_pool->allocate ();
4767 invariant_ptr->invariant_rtx = invariant_rtx;
4768 invariant_ptr->insn = NULL;
4769 invariants.safe_push (invariant_ptr);
4770 *entry_ptr = (void *) invariant_ptr;
4772 return (invariant_ptr_t) *entry_ptr;
4775 /* Initiate the invariant table. */
4776 static void
4777 initiate_invariants (void)
4779 invariants.create (100);
4780 invariants_pool = new object_allocator<struct invariant> ("Inheritance invariants");
4781 invariant_table = htab_create (100, invariant_hash, invariant_eq_p, NULL);
4784 /* Finish the invariant table. */
4785 static void
4786 finish_invariants (void)
4788 htab_delete (invariant_table);
4789 delete invariants_pool;
4790 invariants.release ();
4793 /* Make the invariant table empty. */
4794 static void
4795 clear_invariants (void)
4797 htab_empty (invariant_table);
4798 invariants_pool->release ();
4799 invariants.truncate (0);
4804 /* This page contains code to do inheritance/split
4805 transformations. */
4807 /* Number of reloads passed so far in current EBB. */
4808 static int reloads_num;
4810 /* Number of calls passed so far in current EBB. */
4811 static int calls_num;
4813 /* Current reload pseudo check for validity of elements in
4814 USAGE_INSNS. */
4815 static int curr_usage_insns_check;
4817 /* Info about last usage of registers in EBB to do inheritance/split
4818 transformation. Inheritance transformation is done from a spilled
4819 pseudo and split transformations from a hard register or a pseudo
4820 assigned to a hard register. */
4821 struct usage_insns
4823 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4824 value INSNS is valid. The insns is chain of optional debug insns
4825 and a finishing non-debug insn using the corresponding reg. The
4826 value is also used to mark the registers which are set up in the
4827 current insn. The negated insn uid is used for this. */
4828 int check;
4829 /* Value of global reloads_num at the last insn in INSNS. */
4830 int reloads_num;
4831 /* Value of global reloads_nums at the last insn in INSNS. */
4832 int calls_num;
4833 /* It can be true only for splitting. And it means that the restore
4834 insn should be put after insn given by the following member. */
4835 bool after_p;
4836 /* Next insns in the current EBB which use the original reg and the
4837 original reg value is not changed between the current insn and
4838 the next insns. In order words, e.g. for inheritance, if we need
4839 to use the original reg value again in the next insns we can try
4840 to use the value in a hard register from a reload insn of the
4841 current insn. */
4842 rtx insns;
4845 /* Map: regno -> corresponding pseudo usage insns. */
4846 static struct usage_insns *usage_insns;
4848 static void
4849 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4851 usage_insns[regno].check = curr_usage_insns_check;
4852 usage_insns[regno].insns = insn;
4853 usage_insns[regno].reloads_num = reloads_num;
4854 usage_insns[regno].calls_num = calls_num;
4855 usage_insns[regno].after_p = after_p;
4858 /* The function is used to form list REGNO usages which consists of
4859 optional debug insns finished by a non-debug insn using REGNO.
4860 RELOADS_NUM is current number of reload insns processed so far. */
4861 static void
4862 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
4864 rtx next_usage_insns;
4866 if (usage_insns[regno].check == curr_usage_insns_check
4867 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4868 && DEBUG_INSN_P (insn))
4870 /* Check that we did not add the debug insn yet. */
4871 if (next_usage_insns != insn
4872 && (GET_CODE (next_usage_insns) != INSN_LIST
4873 || XEXP (next_usage_insns, 0) != insn))
4874 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4875 next_usage_insns);
4877 else if (NONDEBUG_INSN_P (insn))
4878 setup_next_usage_insn (regno, insn, reloads_num, false);
4879 else
4880 usage_insns[regno].check = 0;
4883 /* Return first non-debug insn in list USAGE_INSNS. */
4884 static rtx_insn *
4885 skip_usage_debug_insns (rtx usage_insns)
4887 rtx insn;
4889 /* Skip debug insns. */
4890 for (insn = usage_insns;
4891 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4892 insn = XEXP (insn, 1))
4894 return safe_as_a <rtx_insn *> (insn);
4897 /* Return true if we need secondary memory moves for insn in
4898 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4899 into the insn. */
4900 static bool
4901 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4902 rtx usage_insns ATTRIBUTE_UNUSED)
4904 #ifndef SECONDARY_MEMORY_NEEDED
4905 return false;
4906 #else
4907 rtx_insn *insn;
4908 rtx set, dest;
4909 enum reg_class cl;
4911 if (inher_cl == ALL_REGS
4912 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4913 return false;
4914 lra_assert (INSN_P (insn));
4915 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4916 return false;
4917 dest = SET_DEST (set);
4918 if (! REG_P (dest))
4919 return false;
4920 lra_assert (inher_cl != NO_REGS);
4921 cl = get_reg_class (REGNO (dest));
4922 return (cl != NO_REGS && cl != ALL_REGS
4923 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4924 #endif
4927 /* Registers involved in inheritance/split in the current EBB
4928 (inheritance/split pseudos and original registers). */
4929 static bitmap_head check_only_regs;
4931 /* Reload pseudos can not be involded in invariant inheritance in the
4932 current EBB. */
4933 static bitmap_head invalid_invariant_regs;
4935 /* Do inheritance transformations for insn INSN, which defines (if
4936 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4937 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4938 form as the "insns" field of usage_insns. Return true if we
4939 succeed in such transformation.
4941 The transformations look like:
4943 p <- ... i <- ...
4944 ... p <- i (new insn)
4945 ... =>
4946 <- ... p ... <- ... i ...
4948 ... i <- p (new insn)
4949 <- ... p ... <- ... i ...
4950 ... =>
4951 <- ... p ... <- ... i ...
4952 where p is a spilled original pseudo and i is a new inheritance pseudo.
4955 The inheritance pseudo has the smallest class of two classes CL and
4956 class of ORIGINAL REGNO. */
4957 static bool
4958 inherit_reload_reg (bool def_p, int original_regno,
4959 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4961 if (optimize_function_for_size_p (cfun))
4962 return false;
4964 enum reg_class rclass = lra_get_allocno_class (original_regno);
4965 rtx original_reg = regno_reg_rtx[original_regno];
4966 rtx new_reg, usage_insn;
4967 rtx_insn *new_insns;
4969 lra_assert (! usage_insns[original_regno].after_p);
4970 if (lra_dump_file != NULL)
4971 fprintf (lra_dump_file,
4972 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4973 if (! ira_reg_classes_intersect_p[cl][rclass])
4975 if (lra_dump_file != NULL)
4977 fprintf (lra_dump_file,
4978 " Rejecting inheritance for %d "
4979 "because of disjoint classes %s and %s\n",
4980 original_regno, reg_class_names[cl],
4981 reg_class_names[rclass]);
4982 fprintf (lra_dump_file,
4983 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4985 return false;
4987 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4988 /* We don't use a subset of two classes because it can be
4989 NO_REGS. This transformation is still profitable in most
4990 cases even if the classes are not intersected as register
4991 move is probably cheaper than a memory load. */
4992 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4994 if (lra_dump_file != NULL)
4995 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4996 reg_class_names[cl], reg_class_names[rclass]);
4998 rclass = cl;
5000 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
5002 /* Reject inheritance resulting in secondary memory moves.
5003 Otherwise, there is a danger in LRA cycling. Also such
5004 transformation will be unprofitable. */
5005 if (lra_dump_file != NULL)
5007 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
5008 rtx set = single_set (insn);
5010 lra_assert (set != NULL_RTX);
5012 rtx dest = SET_DEST (set);
5014 lra_assert (REG_P (dest));
5015 fprintf (lra_dump_file,
5016 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
5017 "as secondary mem is needed\n",
5018 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
5019 original_regno, reg_class_names[rclass]);
5020 fprintf (lra_dump_file,
5021 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5023 return false;
5025 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
5026 rclass, "inheritance");
5027 start_sequence ();
5028 if (def_p)
5029 lra_emit_move (original_reg, new_reg);
5030 else
5031 lra_emit_move (new_reg, original_reg);
5032 new_insns = get_insns ();
5033 end_sequence ();
5034 if (NEXT_INSN (new_insns) != NULL_RTX)
5036 if (lra_dump_file != NULL)
5038 fprintf (lra_dump_file,
5039 " Rejecting inheritance %d->%d "
5040 "as it results in 2 or more insns:\n",
5041 original_regno, REGNO (new_reg));
5042 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
5043 fprintf (lra_dump_file,
5044 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5046 return false;
5048 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
5049 lra_update_insn_regno_info (insn);
5050 if (! def_p)
5051 /* We now have a new usage insn for original regno. */
5052 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
5053 if (lra_dump_file != NULL)
5054 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
5055 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
5056 lra_reg_info[REGNO (new_reg)].restore_rtx = regno_reg_rtx[original_regno];
5057 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5058 bitmap_set_bit (&check_only_regs, original_regno);
5059 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
5060 if (def_p)
5061 lra_process_new_insns (insn, NULL, new_insns,
5062 "Add original<-inheritance");
5063 else
5064 lra_process_new_insns (insn, new_insns, NULL,
5065 "Add inheritance<-original");
5066 while (next_usage_insns != NULL_RTX)
5068 if (GET_CODE (next_usage_insns) != INSN_LIST)
5070 usage_insn = next_usage_insns;
5071 lra_assert (NONDEBUG_INSN_P (usage_insn));
5072 next_usage_insns = NULL;
5074 else
5076 usage_insn = XEXP (next_usage_insns, 0);
5077 lra_assert (DEBUG_INSN_P (usage_insn));
5078 next_usage_insns = XEXP (next_usage_insns, 1);
5080 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5081 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5082 if (lra_dump_file != NULL)
5084 fprintf (lra_dump_file,
5085 " Inheritance reuse change %d->%d (bb%d):\n",
5086 original_regno, REGNO (new_reg),
5087 BLOCK_FOR_INSN (usage_insn)->index);
5088 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5091 if (lra_dump_file != NULL)
5092 fprintf (lra_dump_file,
5093 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5094 return true;
5097 /* Return true if we need a caller save/restore for pseudo REGNO which
5098 was assigned to a hard register. */
5099 static inline bool
5100 need_for_call_save_p (int regno)
5102 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
5103 return (usage_insns[regno].calls_num < calls_num
5104 && (overlaps_hard_reg_set_p
5105 ((flag_ipa_ra &&
5106 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
5107 ? lra_reg_info[regno].actual_call_used_reg_set
5108 : call_used_reg_set,
5109 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
5110 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
5111 PSEUDO_REGNO_MODE (regno))));
5114 /* Global registers occurring in the current EBB. */
5115 static bitmap_head ebb_global_regs;
5117 /* Return true if we need a split for hard register REGNO or pseudo
5118 REGNO which was assigned to a hard register.
5119 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
5120 used for reloads since the EBB end. It is an approximation of the
5121 used hard registers in the split range. The exact value would
5122 require expensive calculations. If we were aggressive with
5123 splitting because of the approximation, the split pseudo will save
5124 the same hard register assignment and will be removed in the undo
5125 pass. We still need the approximation because too aggressive
5126 splitting would result in too inaccurate cost calculation in the
5127 assignment pass because of too many generated moves which will be
5128 probably removed in the undo pass. */
5129 static inline bool
5130 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
5132 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
5134 lra_assert (hard_regno >= 0);
5135 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
5136 /* Don't split eliminable hard registers, otherwise we can
5137 split hard registers like hard frame pointer, which
5138 lives on BB start/end according to DF-infrastructure,
5139 when there is a pseudo assigned to the register and
5140 living in the same BB. */
5141 && (regno >= FIRST_PSEUDO_REGISTER
5142 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
5143 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
5144 /* Don't split call clobbered hard regs living through
5145 calls, otherwise we might have a check problem in the
5146 assign sub-pass as in the most cases (exception is a
5147 situation when lra_risky_transformations_p value is
5148 true) the assign pass assumes that all pseudos living
5149 through calls are assigned to call saved hard regs. */
5150 && (regno >= FIRST_PSEUDO_REGISTER
5151 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
5152 || usage_insns[regno].calls_num == calls_num)
5153 /* We need at least 2 reloads to make pseudo splitting
5154 profitable. We should provide hard regno splitting in
5155 any case to solve 1st insn scheduling problem when
5156 moving hard register definition up might result in
5157 impossibility to find hard register for reload pseudo of
5158 small register class. */
5159 && (usage_insns[regno].reloads_num
5160 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
5161 && (regno < FIRST_PSEUDO_REGISTER
5162 /* For short living pseudos, spilling + inheritance can
5163 be considered a substitution for splitting.
5164 Therefore we do not splitting for local pseudos. It
5165 decreases also aggressiveness of splitting. The
5166 minimal number of references is chosen taking into
5167 account that for 2 references splitting has no sense
5168 as we can just spill the pseudo. */
5169 || (regno >= FIRST_PSEUDO_REGISTER
5170 && lra_reg_info[regno].nrefs > 3
5171 && bitmap_bit_p (&ebb_global_regs, regno))))
5172 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
5175 /* Return class for the split pseudo created from original pseudo with
5176 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
5177 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
5178 results in no secondary memory movements. */
5179 static enum reg_class
5180 choose_split_class (enum reg_class allocno_class,
5181 int hard_regno ATTRIBUTE_UNUSED,
5182 machine_mode mode ATTRIBUTE_UNUSED)
5184 #ifndef SECONDARY_MEMORY_NEEDED
5185 return allocno_class;
5186 #else
5187 int i;
5188 enum reg_class cl, best_cl = NO_REGS;
5189 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
5190 = REGNO_REG_CLASS (hard_regno);
5192 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
5193 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
5194 return allocno_class;
5195 for (i = 0;
5196 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
5197 i++)
5198 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
5199 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
5200 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
5201 && (best_cl == NO_REGS
5202 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
5203 best_cl = cl;
5204 return best_cl;
5205 #endif
5208 /* Do split transformations for insn INSN, which defines or uses
5209 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
5210 the EBB next uses ORIGINAL_REGNO; it has the same form as the
5211 "insns" field of usage_insns.
5213 The transformations look like:
5215 p <- ... p <- ...
5216 ... s <- p (new insn -- save)
5217 ... =>
5218 ... p <- s (new insn -- restore)
5219 <- ... p ... <- ... p ...
5221 <- ... p ... <- ... p ...
5222 ... s <- p (new insn -- save)
5223 ... =>
5224 ... p <- s (new insn -- restore)
5225 <- ... p ... <- ... p ...
5227 where p is an original pseudo got a hard register or a hard
5228 register and s is a new split pseudo. The save is put before INSN
5229 if BEFORE_P is true. Return true if we succeed in such
5230 transformation. */
5231 static bool
5232 split_reg (bool before_p, int original_regno, rtx_insn *insn,
5233 rtx next_usage_insns)
5235 enum reg_class rclass;
5236 rtx original_reg;
5237 int hard_regno, nregs;
5238 rtx new_reg, usage_insn;
5239 rtx_insn *restore, *save;
5240 bool after_p;
5241 bool call_save_p;
5242 machine_mode mode;
5244 if (original_regno < FIRST_PSEUDO_REGISTER)
5246 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
5247 hard_regno = original_regno;
5248 call_save_p = false;
5249 nregs = 1;
5250 mode = lra_reg_info[hard_regno].biggest_mode;
5251 machine_mode reg_rtx_mode = GET_MODE (regno_reg_rtx[hard_regno]);
5252 /* A reg can have a biggest_mode of VOIDmode if it was only ever seen
5253 as part of a multi-word register. In that case, or if the biggest
5254 mode was larger than a register, just use the reg_rtx. Otherwise,
5255 limit the size to that of the biggest access in the function. */
5256 if (mode == VOIDmode
5257 || GET_MODE_SIZE (mode) > GET_MODE_SIZE (reg_rtx_mode))
5259 original_reg = regno_reg_rtx[hard_regno];
5260 mode = reg_rtx_mode;
5262 else
5263 original_reg = gen_rtx_REG (mode, hard_regno);
5265 else
5267 mode = PSEUDO_REGNO_MODE (original_regno);
5268 hard_regno = reg_renumber[original_regno];
5269 nregs = hard_regno_nregs[hard_regno][mode];
5270 rclass = lra_get_allocno_class (original_regno);
5271 original_reg = regno_reg_rtx[original_regno];
5272 call_save_p = need_for_call_save_p (original_regno);
5274 lra_assert (hard_regno >= 0);
5275 if (lra_dump_file != NULL)
5276 fprintf (lra_dump_file,
5277 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
5279 if (call_save_p)
5281 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
5282 hard_regno_nregs[hard_regno][mode],
5283 mode);
5284 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
5286 else
5288 rclass = choose_split_class (rclass, hard_regno, mode);
5289 if (rclass == NO_REGS)
5291 if (lra_dump_file != NULL)
5293 fprintf (lra_dump_file,
5294 " Rejecting split of %d(%s): "
5295 "no good reg class for %d(%s)\n",
5296 original_regno,
5297 reg_class_names[lra_get_allocno_class (original_regno)],
5298 hard_regno,
5299 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
5300 fprintf
5301 (lra_dump_file,
5302 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5304 return false;
5306 new_reg = lra_create_new_reg (mode, original_reg, rclass, "split");
5307 reg_renumber[REGNO (new_reg)] = hard_regno;
5309 save = emit_spill_move (true, new_reg, original_reg);
5310 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
5312 if (lra_dump_file != NULL)
5314 fprintf
5315 (lra_dump_file,
5316 " Rejecting split %d->%d resulting in > 2 save insns:\n",
5317 original_regno, REGNO (new_reg));
5318 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
5319 fprintf (lra_dump_file,
5320 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5322 return false;
5324 restore = emit_spill_move (false, new_reg, original_reg);
5325 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
5327 if (lra_dump_file != NULL)
5329 fprintf (lra_dump_file,
5330 " Rejecting split %d->%d "
5331 "resulting in > 2 restore insns:\n",
5332 original_regno, REGNO (new_reg));
5333 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
5334 fprintf (lra_dump_file,
5335 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5337 return false;
5339 after_p = usage_insns[original_regno].after_p;
5340 lra_reg_info[REGNO (new_reg)].restore_rtx = regno_reg_rtx[original_regno];
5341 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5342 bitmap_set_bit (&check_only_regs, original_regno);
5343 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
5344 for (;;)
5346 if (GET_CODE (next_usage_insns) != INSN_LIST)
5348 usage_insn = next_usage_insns;
5349 break;
5351 usage_insn = XEXP (next_usage_insns, 0);
5352 lra_assert (DEBUG_INSN_P (usage_insn));
5353 next_usage_insns = XEXP (next_usage_insns, 1);
5354 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5355 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5356 if (lra_dump_file != NULL)
5358 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5359 original_regno, REGNO (new_reg));
5360 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5363 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5364 lra_assert (usage_insn != insn || (after_p && before_p));
5365 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5366 after_p ? NULL : restore,
5367 after_p ? restore : NULL,
5368 call_save_p
5369 ? "Add reg<-save" : "Add reg<-split");
5370 lra_process_new_insns (insn, before_p ? save : NULL,
5371 before_p ? NULL : save,
5372 call_save_p
5373 ? "Add save<-reg" : "Add split<-reg");
5374 if (nregs > 1)
5375 /* If we are trying to split multi-register. We should check
5376 conflicts on the next assignment sub-pass. IRA can allocate on
5377 sub-register levels, LRA do this on pseudos level right now and
5378 this discrepancy may create allocation conflicts after
5379 splitting. */
5380 lra_risky_transformations_p = true;
5381 if (lra_dump_file != NULL)
5382 fprintf (lra_dump_file,
5383 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5384 return true;
5387 /* Recognize that we need a split transformation for insn INSN, which
5388 defines or uses REGNO in its insn biggest MODE (we use it only if
5389 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5390 hard registers which might be used for reloads since the EBB end.
5391 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5392 uid before starting INSN processing. Return true if we succeed in
5393 such transformation. */
5394 static bool
5395 split_if_necessary (int regno, machine_mode mode,
5396 HARD_REG_SET potential_reload_hard_regs,
5397 bool before_p, rtx_insn *insn, int max_uid)
5399 bool res = false;
5400 int i, nregs = 1;
5401 rtx next_usage_insns;
5403 if (regno < FIRST_PSEUDO_REGISTER)
5404 nregs = hard_regno_nregs[regno][mode];
5405 for (i = 0; i < nregs; i++)
5406 if (usage_insns[regno + i].check == curr_usage_insns_check
5407 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5408 /* To avoid processing the register twice or more. */
5409 && ((GET_CODE (next_usage_insns) != INSN_LIST
5410 && INSN_UID (next_usage_insns) < max_uid)
5411 || (GET_CODE (next_usage_insns) == INSN_LIST
5412 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5413 && need_for_split_p (potential_reload_hard_regs, regno + i)
5414 && split_reg (before_p, regno + i, insn, next_usage_insns))
5415 res = true;
5416 return res;
5419 /* Return TRUE if rtx X is considered as an invariant for
5420 inheritance. */
5421 static bool
5422 invariant_p (const_rtx x)
5424 machine_mode mode;
5425 const char *fmt;
5426 enum rtx_code code;
5427 int i, j;
5429 code = GET_CODE (x);
5430 mode = GET_MODE (x);
5431 if (code == SUBREG)
5433 x = SUBREG_REG (x);
5434 code = GET_CODE (x);
5435 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
5436 mode = GET_MODE (x);
5439 if (MEM_P (x))
5440 return false;
5442 if (REG_P (x))
5444 int i, nregs, regno = REGNO (x);
5446 if (regno >= FIRST_PSEUDO_REGISTER || regno == STACK_POINTER_REGNUM
5447 || TEST_HARD_REG_BIT (eliminable_regset, regno)
5448 || GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
5449 return false;
5450 nregs = hard_regno_nregs[regno][mode];
5451 for (i = 0; i < nregs; i++)
5452 if (! fixed_regs[regno + i]
5453 /* A hard register may be clobbered in the current insn
5454 but we can ignore this case because if the hard
5455 register is used it should be set somewhere after the
5456 clobber. */
5457 || bitmap_bit_p (&invalid_invariant_regs, regno + i))
5458 return false;
5460 fmt = GET_RTX_FORMAT (code);
5461 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5463 if (fmt[i] == 'e')
5465 if (! invariant_p (XEXP (x, i)))
5466 return false;
5468 else if (fmt[i] == 'E')
5470 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5471 if (! invariant_p (XVECEXP (x, i, j)))
5472 return false;
5475 return true;
5478 /* We have 'dest_reg <- invariant'. Let us try to make an invariant
5479 inheritance transformation (using dest_reg instead invariant in a
5480 subsequent insn). */
5481 static bool
5482 process_invariant_for_inheritance (rtx dst_reg, rtx invariant_rtx)
5484 invariant_ptr_t invariant_ptr;
5485 rtx_insn *insn, *new_insns;
5486 rtx insn_set, insn_reg, new_reg;
5487 int insn_regno;
5488 bool succ_p = false;
5489 int dst_regno = REGNO (dst_reg);
5490 enum machine_mode dst_mode = GET_MODE (dst_reg);
5491 enum reg_class cl = lra_get_allocno_class (dst_regno), insn_reg_cl;
5493 invariant_ptr = insert_invariant (invariant_rtx);
5494 if ((insn = invariant_ptr->insn) != NULL_RTX)
5496 /* We have a subsequent insn using the invariant. */
5497 insn_set = single_set (insn);
5498 lra_assert (insn_set != NULL);
5499 insn_reg = SET_DEST (insn_set);
5500 lra_assert (REG_P (insn_reg));
5501 insn_regno = REGNO (insn_reg);
5502 insn_reg_cl = lra_get_allocno_class (insn_regno);
5504 if (dst_mode == GET_MODE (insn_reg)
5505 /* We should consider only result move reg insns which are
5506 cheap. */
5507 && targetm.register_move_cost (dst_mode, cl, insn_reg_cl) == 2
5508 && targetm.register_move_cost (dst_mode, cl, cl) == 2)
5510 if (lra_dump_file != NULL)
5511 fprintf (lra_dump_file,
5512 " [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[\n");
5513 new_reg = lra_create_new_reg (dst_mode, dst_reg,
5514 cl, "invariant inheritance");
5515 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
5516 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5517 lra_reg_info[REGNO (new_reg)].restore_rtx = PATTERN (insn);
5518 start_sequence ();
5519 lra_emit_move (new_reg, dst_reg);
5520 new_insns = get_insns ();
5521 end_sequence ();
5522 lra_process_new_insns (curr_insn, NULL, new_insns,
5523 "Add invariant inheritance<-original");
5524 start_sequence ();
5525 lra_emit_move (SET_DEST (insn_set), new_reg);
5526 new_insns = get_insns ();
5527 end_sequence ();
5528 lra_process_new_insns (insn, NULL, new_insns,
5529 "Changing reload<-inheritance");
5530 lra_set_insn_deleted (insn);
5531 succ_p = true;
5532 if (lra_dump_file != NULL)
5534 fprintf (lra_dump_file,
5535 " Invariant inheritance reuse change %d (bb%d):\n",
5536 REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
5537 dump_insn_slim (lra_dump_file, insn);
5538 fprintf (lra_dump_file,
5539 " ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]\n");
5543 invariant_ptr->insn = curr_insn;
5544 return succ_p;
5547 /* Check only registers living at the current program point in the
5548 current EBB. */
5549 static bitmap_head live_regs;
5551 /* Update live info in EBB given by its HEAD and TAIL insns after
5552 inheritance/split transformation. The function removes dead moves
5553 too. */
5554 static void
5555 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5557 unsigned int j;
5558 int i, regno;
5559 bool live_p;
5560 rtx_insn *prev_insn;
5561 rtx set;
5562 bool remove_p;
5563 basic_block last_bb, prev_bb, curr_bb;
5564 bitmap_iterator bi;
5565 struct lra_insn_reg *reg;
5566 edge e;
5567 edge_iterator ei;
5569 last_bb = BLOCK_FOR_INSN (tail);
5570 prev_bb = NULL;
5571 for (curr_insn = tail;
5572 curr_insn != PREV_INSN (head);
5573 curr_insn = prev_insn)
5575 prev_insn = PREV_INSN (curr_insn);
5576 /* We need to process empty blocks too. They contain
5577 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5578 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5579 continue;
5580 curr_bb = BLOCK_FOR_INSN (curr_insn);
5581 if (curr_bb != prev_bb)
5583 if (prev_bb != NULL)
5585 /* Update df_get_live_in (prev_bb): */
5586 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5587 if (bitmap_bit_p (&live_regs, j))
5588 bitmap_set_bit (df_get_live_in (prev_bb), j);
5589 else
5590 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5592 if (curr_bb != last_bb)
5594 /* Update df_get_live_out (curr_bb): */
5595 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5597 live_p = bitmap_bit_p (&live_regs, j);
5598 if (! live_p)
5599 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5600 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5602 live_p = true;
5603 break;
5605 if (live_p)
5606 bitmap_set_bit (df_get_live_out (curr_bb), j);
5607 else
5608 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5611 prev_bb = curr_bb;
5612 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5614 if (! NONDEBUG_INSN_P (curr_insn))
5615 continue;
5616 curr_id = lra_get_insn_recog_data (curr_insn);
5617 curr_static_id = curr_id->insn_static_data;
5618 remove_p = false;
5619 if ((set = single_set (curr_insn)) != NULL_RTX
5620 && REG_P (SET_DEST (set))
5621 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5622 && SET_DEST (set) != pic_offset_table_rtx
5623 && bitmap_bit_p (&check_only_regs, regno)
5624 && ! bitmap_bit_p (&live_regs, regno))
5625 remove_p = true;
5626 /* See which defined values die here. */
5627 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5628 if (reg->type == OP_OUT && ! reg->subreg_p)
5629 bitmap_clear_bit (&live_regs, reg->regno);
5630 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5631 if (reg->type == OP_OUT && ! reg->subreg_p)
5632 bitmap_clear_bit (&live_regs, reg->regno);
5633 if (curr_id->arg_hard_regs != NULL)
5634 /* Make clobbered argument hard registers die. */
5635 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5636 if (regno >= FIRST_PSEUDO_REGISTER)
5637 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
5638 /* Mark each used value as live. */
5639 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5640 if (reg->type != OP_OUT
5641 && bitmap_bit_p (&check_only_regs, reg->regno))
5642 bitmap_set_bit (&live_regs, reg->regno);
5643 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5644 if (reg->type != OP_OUT
5645 && bitmap_bit_p (&check_only_regs, reg->regno))
5646 bitmap_set_bit (&live_regs, reg->regno);
5647 if (curr_id->arg_hard_regs != NULL)
5648 /* Make used argument hard registers live. */
5649 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5650 if (regno < FIRST_PSEUDO_REGISTER
5651 && bitmap_bit_p (&check_only_regs, regno))
5652 bitmap_set_bit (&live_regs, regno);
5653 /* It is quite important to remove dead move insns because it
5654 means removing dead store. We don't need to process them for
5655 constraints. */
5656 if (remove_p)
5658 if (lra_dump_file != NULL)
5660 fprintf (lra_dump_file, " Removing dead insn:\n ");
5661 dump_insn_slim (lra_dump_file, curr_insn);
5663 lra_set_insn_deleted (curr_insn);
5668 /* The structure describes info to do an inheritance for the current
5669 insn. We need to collect such info first before doing the
5670 transformations because the transformations change the insn
5671 internal representation. */
5672 struct to_inherit
5674 /* Original regno. */
5675 int regno;
5676 /* Subsequent insns which can inherit original reg value. */
5677 rtx insns;
5680 /* Array containing all info for doing inheritance from the current
5681 insn. */
5682 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5684 /* Number elements in the previous array. */
5685 static int to_inherit_num;
5687 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5688 structure to_inherit. */
5689 static void
5690 add_to_inherit (int regno, rtx insns)
5692 int i;
5694 for (i = 0; i < to_inherit_num; i++)
5695 if (to_inherit[i].regno == regno)
5696 return;
5697 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5698 to_inherit[to_inherit_num].regno = regno;
5699 to_inherit[to_inherit_num++].insns = insns;
5702 /* Return the last non-debug insn in basic block BB, or the block begin
5703 note if none. */
5704 static rtx_insn *
5705 get_last_insertion_point (basic_block bb)
5707 rtx_insn *insn;
5709 FOR_BB_INSNS_REVERSE (bb, insn)
5710 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5711 return insn;
5712 gcc_unreachable ();
5715 /* Set up RES by registers living on edges FROM except the edge (FROM,
5716 TO) or by registers set up in a jump insn in BB FROM. */
5717 static void
5718 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5720 rtx_insn *last;
5721 struct lra_insn_reg *reg;
5722 edge e;
5723 edge_iterator ei;
5725 lra_assert (to != NULL);
5726 bitmap_clear (res);
5727 FOR_EACH_EDGE (e, ei, from->succs)
5728 if (e->dest != to)
5729 bitmap_ior_into (res, df_get_live_in (e->dest));
5730 last = get_last_insertion_point (from);
5731 if (! JUMP_P (last))
5732 return;
5733 curr_id = lra_get_insn_recog_data (last);
5734 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5735 if (reg->type != OP_IN)
5736 bitmap_set_bit (res, reg->regno);
5739 /* Used as a temporary results of some bitmap calculations. */
5740 static bitmap_head temp_bitmap;
5742 /* We split for reloads of small class of hard regs. The following
5743 defines how many hard regs the class should have to be qualified as
5744 small. The code is mostly oriented to x86/x86-64 architecture
5745 where some insns need to use only specific register or pair of
5746 registers and these register can live in RTL explicitly, e.g. for
5747 parameter passing. */
5748 static const int max_small_class_regs_num = 2;
5750 /* Do inheritance/split transformations in EBB starting with HEAD and
5751 finishing on TAIL. We process EBB insns in the reverse order.
5752 Return true if we did any inheritance/split transformation in the
5753 EBB.
5755 We should avoid excessive splitting which results in worse code
5756 because of inaccurate cost calculations for spilling new split
5757 pseudos in such case. To achieve this we do splitting only if
5758 register pressure is high in given basic block and there are reload
5759 pseudos requiring hard registers. We could do more register
5760 pressure calculations at any given program point to avoid necessary
5761 splitting even more but it is to expensive and the current approach
5762 works well enough. */
5763 static bool
5764 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5766 int i, src_regno, dst_regno, nregs;
5767 bool change_p, succ_p, update_reloads_num_p;
5768 rtx_insn *prev_insn, *last_insn;
5769 rtx next_usage_insns, curr_set;
5770 enum reg_class cl;
5771 struct lra_insn_reg *reg;
5772 basic_block last_processed_bb, curr_bb = NULL;
5773 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5774 bitmap to_process;
5775 unsigned int j;
5776 bitmap_iterator bi;
5777 bool head_p, after_p;
5779 change_p = false;
5780 curr_usage_insns_check++;
5781 clear_invariants ();
5782 reloads_num = calls_num = 0;
5783 bitmap_clear (&check_only_regs);
5784 bitmap_clear (&invalid_invariant_regs);
5785 last_processed_bb = NULL;
5786 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5787 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5788 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5789 /* We don't process new insns generated in the loop. */
5790 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5792 prev_insn = PREV_INSN (curr_insn);
5793 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5794 curr_bb = BLOCK_FOR_INSN (curr_insn);
5795 if (last_processed_bb != curr_bb)
5797 /* We are at the end of BB. Add qualified living
5798 pseudos for potential splitting. */
5799 to_process = df_get_live_out (curr_bb);
5800 if (last_processed_bb != NULL)
5802 /* We are somewhere in the middle of EBB. */
5803 get_live_on_other_edges (curr_bb, last_processed_bb,
5804 &temp_bitmap);
5805 to_process = &temp_bitmap;
5807 last_processed_bb = curr_bb;
5808 last_insn = get_last_insertion_point (curr_bb);
5809 after_p = (! JUMP_P (last_insn)
5810 && (! CALL_P (last_insn)
5811 || (find_reg_note (last_insn,
5812 REG_NORETURN, NULL_RTX) == NULL_RTX
5813 && ! SIBLING_CALL_P (last_insn))));
5814 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5815 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5817 if ((int) j >= lra_constraint_new_regno_start)
5818 break;
5819 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5821 if (j < FIRST_PSEUDO_REGISTER)
5822 SET_HARD_REG_BIT (live_hard_regs, j);
5823 else
5824 add_to_hard_reg_set (&live_hard_regs,
5825 PSEUDO_REGNO_MODE (j),
5826 reg_renumber[j]);
5827 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5831 src_regno = dst_regno = -1;
5832 curr_set = single_set (curr_insn);
5833 if (curr_set != NULL_RTX && REG_P (SET_DEST (curr_set)))
5834 dst_regno = REGNO (SET_DEST (curr_set));
5835 if (curr_set != NULL_RTX && REG_P (SET_SRC (curr_set)))
5836 src_regno = REGNO (SET_SRC (curr_set));
5837 update_reloads_num_p = true;
5838 if (src_regno < lra_constraint_new_regno_start
5839 && src_regno >= FIRST_PSEUDO_REGISTER
5840 && reg_renumber[src_regno] < 0
5841 && dst_regno >= lra_constraint_new_regno_start
5842 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5844 /* 'reload_pseudo <- original_pseudo'. */
5845 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5846 reloads_num++;
5847 update_reloads_num_p = false;
5848 succ_p = false;
5849 if (usage_insns[src_regno].check == curr_usage_insns_check
5850 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5851 succ_p = inherit_reload_reg (false, src_regno, cl,
5852 curr_insn, next_usage_insns);
5853 if (succ_p)
5854 change_p = true;
5855 else
5856 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5857 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5858 IOR_HARD_REG_SET (potential_reload_hard_regs,
5859 reg_class_contents[cl]);
5861 else if (src_regno < 0
5862 && dst_regno >= lra_constraint_new_regno_start
5863 && invariant_p (SET_SRC (curr_set))
5864 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS
5865 && ! bitmap_bit_p (&invalid_invariant_regs, dst_regno))
5867 /* 'reload_pseudo <- invariant'. */
5868 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5869 reloads_num++;
5870 update_reloads_num_p = false;
5871 if (process_invariant_for_inheritance (SET_DEST (curr_set), SET_SRC (curr_set)))
5872 change_p = true;
5873 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5874 IOR_HARD_REG_SET (potential_reload_hard_regs,
5875 reg_class_contents[cl]);
5877 else if (src_regno >= lra_constraint_new_regno_start
5878 && dst_regno < lra_constraint_new_regno_start
5879 && dst_regno >= FIRST_PSEUDO_REGISTER
5880 && reg_renumber[dst_regno] < 0
5881 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5882 && usage_insns[dst_regno].check == curr_usage_insns_check
5883 && (next_usage_insns
5884 = usage_insns[dst_regno].insns) != NULL_RTX)
5886 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5887 reloads_num++;
5888 update_reloads_num_p = false;
5889 /* 'original_pseudo <- reload_pseudo'. */
5890 if (! JUMP_P (curr_insn)
5891 && inherit_reload_reg (true, dst_regno, cl,
5892 curr_insn, next_usage_insns))
5893 change_p = true;
5894 /* Invalidate. */
5895 usage_insns[dst_regno].check = 0;
5896 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5897 IOR_HARD_REG_SET (potential_reload_hard_regs,
5898 reg_class_contents[cl]);
5900 else if (INSN_P (curr_insn))
5902 int iter;
5903 int max_uid = get_max_uid ();
5905 curr_id = lra_get_insn_recog_data (curr_insn);
5906 curr_static_id = curr_id->insn_static_data;
5907 to_inherit_num = 0;
5908 /* Process insn definitions. */
5909 for (iter = 0; iter < 2; iter++)
5910 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5911 reg != NULL;
5912 reg = reg->next)
5913 if (reg->type != OP_IN
5914 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5916 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5917 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5918 && usage_insns[dst_regno].check == curr_usage_insns_check
5919 && (next_usage_insns
5920 = usage_insns[dst_regno].insns) != NULL_RTX)
5922 struct lra_insn_reg *r;
5924 for (r = curr_id->regs; r != NULL; r = r->next)
5925 if (r->type != OP_OUT && r->regno == dst_regno)
5926 break;
5927 /* Don't do inheritance if the pseudo is also
5928 used in the insn. */
5929 if (r == NULL)
5930 /* We can not do inheritance right now
5931 because the current insn reg info (chain
5932 regs) can change after that. */
5933 add_to_inherit (dst_regno, next_usage_insns);
5935 /* We can not process one reg twice here because of
5936 usage_insns invalidation. */
5937 if ((dst_regno < FIRST_PSEUDO_REGISTER
5938 || reg_renumber[dst_regno] >= 0)
5939 && ! reg->subreg_p && reg->type != OP_IN)
5941 HARD_REG_SET s;
5943 if (split_if_necessary (dst_regno, reg->biggest_mode,
5944 potential_reload_hard_regs,
5945 false, curr_insn, max_uid))
5946 change_p = true;
5947 CLEAR_HARD_REG_SET (s);
5948 if (dst_regno < FIRST_PSEUDO_REGISTER)
5949 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5950 else
5951 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5952 reg_renumber[dst_regno]);
5953 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5955 /* We should invalidate potential inheritance or
5956 splitting for the current insn usages to the next
5957 usage insns (see code below) as the output pseudo
5958 prevents this. */
5959 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5960 && reg_renumber[dst_regno] < 0)
5961 || (reg->type == OP_OUT && ! reg->subreg_p
5962 && (dst_regno < FIRST_PSEUDO_REGISTER
5963 || reg_renumber[dst_regno] >= 0)))
5965 /* Invalidate and mark definitions. */
5966 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5967 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5968 else
5970 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5971 for (i = 0; i < nregs; i++)
5972 usage_insns[dst_regno + i].check
5973 = -(int) INSN_UID (curr_insn);
5977 /* Process clobbered call regs. */
5978 if (curr_id->arg_hard_regs != NULL)
5979 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5980 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5981 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
5982 = -(int) INSN_UID (curr_insn);
5983 if (! JUMP_P (curr_insn))
5984 for (i = 0; i < to_inherit_num; i++)
5985 if (inherit_reload_reg (true, to_inherit[i].regno,
5986 ALL_REGS, curr_insn,
5987 to_inherit[i].insns))
5988 change_p = true;
5989 if (CALL_P (curr_insn))
5991 rtx cheap, pat, dest;
5992 rtx_insn *restore;
5993 int regno, hard_regno;
5995 calls_num++;
5996 if ((cheap = find_reg_note (curr_insn,
5997 REG_RETURNED, NULL_RTX)) != NULL_RTX
5998 && ((cheap = XEXP (cheap, 0)), true)
5999 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
6000 && (hard_regno = reg_renumber[regno]) >= 0
6001 /* If there are pending saves/restores, the
6002 optimization is not worth. */
6003 && usage_insns[regno].calls_num == calls_num - 1
6004 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
6006 /* Restore the pseudo from the call result as
6007 REG_RETURNED note says that the pseudo value is
6008 in the call result and the pseudo is an argument
6009 of the call. */
6010 pat = PATTERN (curr_insn);
6011 if (GET_CODE (pat) == PARALLEL)
6012 pat = XVECEXP (pat, 0, 0);
6013 dest = SET_DEST (pat);
6014 /* For multiple return values dest is PARALLEL.
6015 Currently we handle only single return value case. */
6016 if (REG_P (dest))
6018 start_sequence ();
6019 emit_move_insn (cheap, copy_rtx (dest));
6020 restore = get_insns ();
6021 end_sequence ();
6022 lra_process_new_insns (curr_insn, NULL, restore,
6023 "Inserting call parameter restore");
6024 /* We don't need to save/restore of the pseudo from
6025 this call. */
6026 usage_insns[regno].calls_num = calls_num;
6027 bitmap_set_bit (&check_only_regs, regno);
6031 to_inherit_num = 0;
6032 /* Process insn usages. */
6033 for (iter = 0; iter < 2; iter++)
6034 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6035 reg != NULL;
6036 reg = reg->next)
6037 if ((reg->type != OP_OUT
6038 || (reg->type == OP_OUT && reg->subreg_p))
6039 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
6041 if (src_regno >= FIRST_PSEUDO_REGISTER
6042 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
6044 if (usage_insns[src_regno].check == curr_usage_insns_check
6045 && (next_usage_insns
6046 = usage_insns[src_regno].insns) != NULL_RTX
6047 && NONDEBUG_INSN_P (curr_insn))
6048 add_to_inherit (src_regno, next_usage_insns);
6049 else if (usage_insns[src_regno].check
6050 != -(int) INSN_UID (curr_insn))
6051 /* Add usages but only if the reg is not set up
6052 in the same insn. */
6053 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6055 else if (src_regno < FIRST_PSEUDO_REGISTER
6056 || reg_renumber[src_regno] >= 0)
6058 bool before_p;
6059 rtx_insn *use_insn = curr_insn;
6061 before_p = (JUMP_P (curr_insn)
6062 || (CALL_P (curr_insn) && reg->type == OP_IN));
6063 if (NONDEBUG_INSN_P (curr_insn)
6064 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
6065 && split_if_necessary (src_regno, reg->biggest_mode,
6066 potential_reload_hard_regs,
6067 before_p, curr_insn, max_uid))
6069 if (reg->subreg_p)
6070 lra_risky_transformations_p = true;
6071 change_p = true;
6072 /* Invalidate. */
6073 usage_insns[src_regno].check = 0;
6074 if (before_p)
6075 use_insn = PREV_INSN (curr_insn);
6077 if (NONDEBUG_INSN_P (curr_insn))
6079 if (src_regno < FIRST_PSEUDO_REGISTER)
6080 add_to_hard_reg_set (&live_hard_regs,
6081 reg->biggest_mode, src_regno);
6082 else
6083 add_to_hard_reg_set (&live_hard_regs,
6084 PSEUDO_REGNO_MODE (src_regno),
6085 reg_renumber[src_regno]);
6087 add_next_usage_insn (src_regno, use_insn, reloads_num);
6090 /* Process used call regs. */
6091 if (curr_id->arg_hard_regs != NULL)
6092 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6093 if (src_regno < FIRST_PSEUDO_REGISTER)
6095 SET_HARD_REG_BIT (live_hard_regs, src_regno);
6096 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6098 for (i = 0; i < to_inherit_num; i++)
6100 src_regno = to_inherit[i].regno;
6101 if (inherit_reload_reg (false, src_regno, ALL_REGS,
6102 curr_insn, to_inherit[i].insns))
6103 change_p = true;
6104 else
6105 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6108 if (update_reloads_num_p
6109 && NONDEBUG_INSN_P (curr_insn) && curr_set != NULL_RTX)
6111 int regno = -1;
6112 if ((REG_P (SET_DEST (curr_set))
6113 && (regno = REGNO (SET_DEST (curr_set))) >= lra_constraint_new_regno_start
6114 && reg_renumber[regno] < 0
6115 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
6116 || (REG_P (SET_SRC (curr_set))
6117 && (regno = REGNO (SET_SRC (curr_set))) >= lra_constraint_new_regno_start
6118 && reg_renumber[regno] < 0
6119 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
6121 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6122 reloads_num++;
6123 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6124 IOR_HARD_REG_SET (potential_reload_hard_regs,
6125 reg_class_contents[cl]);
6128 if (NONDEBUG_INSN_P (curr_insn))
6130 int regno;
6132 /* Invalidate invariants with changed regs. */
6133 curr_id = lra_get_insn_recog_data (curr_insn);
6134 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6135 if (reg->type != OP_IN)
6136 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6137 curr_static_id = curr_id->insn_static_data;
6138 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6139 if (reg->type != OP_IN)
6140 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6141 if (curr_id->arg_hard_regs != NULL)
6142 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6143 bitmap_set_bit (&invalid_invariant_regs,
6144 regno >= FIRST_PSEUDO_REGISTER
6145 ? regno : regno - FIRST_PSEUDO_REGISTER);
6147 /* We reached the start of the current basic block. */
6148 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
6149 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
6151 /* We reached the beginning of the current block -- do
6152 rest of spliting in the current BB. */
6153 to_process = df_get_live_in (curr_bb);
6154 if (BLOCK_FOR_INSN (head) != curr_bb)
6156 /* We are somewhere in the middle of EBB. */
6157 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
6158 curr_bb, &temp_bitmap);
6159 to_process = &temp_bitmap;
6161 head_p = true;
6162 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
6164 if ((int) j >= lra_constraint_new_regno_start)
6165 break;
6166 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
6167 && usage_insns[j].check == curr_usage_insns_check
6168 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
6170 if (need_for_split_p (potential_reload_hard_regs, j))
6172 if (lra_dump_file != NULL && head_p)
6174 fprintf (lra_dump_file,
6175 " ----------------------------------\n");
6176 head_p = false;
6178 if (split_reg (false, j, bb_note (curr_bb),
6179 next_usage_insns))
6180 change_p = true;
6182 usage_insns[j].check = 0;
6187 return change_p;
6190 /* This value affects EBB forming. If probability of edge from EBB to
6191 a BB is not greater than the following value, we don't add the BB
6192 to EBB. */
6193 #define EBB_PROBABILITY_CUTOFF \
6194 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
6196 /* Current number of inheritance/split iteration. */
6197 int lra_inheritance_iter;
6199 /* Entry function for inheritance/split pass. */
6200 void
6201 lra_inheritance (void)
6203 int i;
6204 basic_block bb, start_bb;
6205 edge e;
6207 lra_inheritance_iter++;
6208 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6209 return;
6210 timevar_push (TV_LRA_INHERITANCE);
6211 if (lra_dump_file != NULL)
6212 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
6213 lra_inheritance_iter);
6214 curr_usage_insns_check = 0;
6215 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
6216 for (i = 0; i < lra_constraint_new_regno_start; i++)
6217 usage_insns[i].check = 0;
6218 bitmap_initialize (&check_only_regs, &reg_obstack);
6219 bitmap_initialize (&invalid_invariant_regs, &reg_obstack);
6220 bitmap_initialize (&live_regs, &reg_obstack);
6221 bitmap_initialize (&temp_bitmap, &reg_obstack);
6222 bitmap_initialize (&ebb_global_regs, &reg_obstack);
6223 FOR_EACH_BB_FN (bb, cfun)
6225 start_bb = bb;
6226 if (lra_dump_file != NULL)
6227 fprintf (lra_dump_file, "EBB");
6228 /* Form a EBB starting with BB. */
6229 bitmap_clear (&ebb_global_regs);
6230 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
6231 for (;;)
6233 if (lra_dump_file != NULL)
6234 fprintf (lra_dump_file, " %d", bb->index);
6235 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
6236 || LABEL_P (BB_HEAD (bb->next_bb)))
6237 break;
6238 e = find_fallthru_edge (bb->succs);
6239 if (! e)
6240 break;
6241 if (e->probability < EBB_PROBABILITY_CUTOFF)
6242 break;
6243 bb = bb->next_bb;
6245 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
6246 if (lra_dump_file != NULL)
6247 fprintf (lra_dump_file, "\n");
6248 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
6249 /* Remember that the EBB head and tail can change in
6250 inherit_in_ebb. */
6251 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
6253 bitmap_clear (&ebb_global_regs);
6254 bitmap_clear (&temp_bitmap);
6255 bitmap_clear (&live_regs);
6256 bitmap_clear (&invalid_invariant_regs);
6257 bitmap_clear (&check_only_regs);
6258 free (usage_insns);
6260 timevar_pop (TV_LRA_INHERITANCE);
6265 /* This page contains code to undo failed inheritance/split
6266 transformations. */
6268 /* Current number of iteration undoing inheritance/split. */
6269 int lra_undo_inheritance_iter;
6271 /* Fix BB live info LIVE after removing pseudos created on pass doing
6272 inheritance/split which are REMOVED_PSEUDOS. */
6273 static void
6274 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
6276 unsigned int regno;
6277 bitmap_iterator bi;
6279 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
6280 if (bitmap_clear_bit (live, regno)
6281 && REG_P (lra_reg_info[regno].restore_rtx))
6282 bitmap_set_bit (live, REGNO (lra_reg_info[regno].restore_rtx));
6285 /* Return regno of the (subreg of) REG. Otherwise, return a negative
6286 number. */
6287 static int
6288 get_regno (rtx reg)
6290 if (GET_CODE (reg) == SUBREG)
6291 reg = SUBREG_REG (reg);
6292 if (REG_P (reg))
6293 return REGNO (reg);
6294 return -1;
6297 /* Delete a move INSN with destination reg DREGNO and a previous
6298 clobber insn with the same regno. The inheritance/split code can
6299 generate moves with preceding clobber and when we delete such moves
6300 we should delete the clobber insn too to keep the correct life
6301 info. */
6302 static void
6303 delete_move_and_clobber (rtx_insn *insn, int dregno)
6305 rtx_insn *prev_insn = PREV_INSN (insn);
6307 lra_set_insn_deleted (insn);
6308 lra_assert (dregno >= 0);
6309 if (prev_insn != NULL && NONDEBUG_INSN_P (prev_insn)
6310 && GET_CODE (PATTERN (prev_insn)) == CLOBBER
6311 && dregno == get_regno (XEXP (PATTERN (prev_insn), 0)))
6312 lra_set_insn_deleted (prev_insn);
6315 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
6316 return true if we did any change. The undo transformations for
6317 inheritance looks like
6318 i <- i2
6319 p <- i => p <- i2
6320 or removing
6321 p <- i, i <- p, and i <- i3
6322 where p is original pseudo from which inheritance pseudo i was
6323 created, i and i3 are removed inheritance pseudos, i2 is another
6324 not removed inheritance pseudo. All split pseudos or other
6325 occurrences of removed inheritance pseudos are changed on the
6326 corresponding original pseudos.
6328 The function also schedules insns changed and created during
6329 inheritance/split pass for processing by the subsequent constraint
6330 pass. */
6331 static bool
6332 remove_inheritance_pseudos (bitmap remove_pseudos)
6334 basic_block bb;
6335 int regno, sregno, prev_sregno, dregno;
6336 rtx restore_rtx;
6337 rtx set, prev_set;
6338 rtx_insn *prev_insn;
6339 bool change_p, done_p;
6341 change_p = ! bitmap_empty_p (remove_pseudos);
6342 /* We can not finish the function right away if CHANGE_P is true
6343 because we need to marks insns affected by previous
6344 inheritance/split pass for processing by the subsequent
6345 constraint pass. */
6346 FOR_EACH_BB_FN (bb, cfun)
6348 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
6349 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
6350 FOR_BB_INSNS_REVERSE (bb, curr_insn)
6352 if (! INSN_P (curr_insn))
6353 continue;
6354 done_p = false;
6355 sregno = dregno = -1;
6356 if (change_p && NONDEBUG_INSN_P (curr_insn)
6357 && (set = single_set (curr_insn)) != NULL_RTX)
6359 dregno = get_regno (SET_DEST (set));
6360 sregno = get_regno (SET_SRC (set));
6363 if (sregno >= 0 && dregno >= 0)
6365 if (bitmap_bit_p (remove_pseudos, dregno)
6366 && ! REG_P (lra_reg_info[dregno].restore_rtx))
6368 /* invariant inheritance pseudo <- original pseudo */
6369 if (lra_dump_file != NULL)
6371 fprintf (lra_dump_file, " Removing invariant inheritance:\n");
6372 dump_insn_slim (lra_dump_file, curr_insn);
6373 fprintf (lra_dump_file, "\n");
6375 delete_move_and_clobber (curr_insn, dregno);
6376 done_p = true;
6378 else if (bitmap_bit_p (remove_pseudos, sregno)
6379 && ! REG_P (lra_reg_info[sregno].restore_rtx))
6381 /* reload pseudo <- invariant inheritance pseudo */
6382 start_sequence ();
6383 /* We can not just change the source. It might be
6384 an insn different from the move. */
6385 emit_insn (lra_reg_info[sregno].restore_rtx);
6386 rtx_insn *new_insns = get_insns ();
6387 end_sequence ();
6388 lra_assert (single_set (new_insns) != NULL
6389 && SET_DEST (set) == SET_DEST (single_set (new_insns)));
6390 lra_process_new_insns (curr_insn, NULL, new_insns,
6391 "Changing reload<-invariant inheritance");
6392 delete_move_and_clobber (curr_insn, dregno);
6393 done_p = true;
6395 else if ((bitmap_bit_p (remove_pseudos, sregno)
6396 && (get_regno (lra_reg_info[sregno].restore_rtx) == dregno
6397 || (bitmap_bit_p (remove_pseudos, dregno)
6398 && get_regno (lra_reg_info[sregno].restore_rtx) >= 0
6399 && (get_regno (lra_reg_info[sregno].restore_rtx)
6400 == get_regno (lra_reg_info[dregno].restore_rtx)))))
6401 || (bitmap_bit_p (remove_pseudos, dregno)
6402 && get_regno (lra_reg_info[dregno].restore_rtx) == sregno))
6403 /* One of the following cases:
6404 original <- removed inheritance pseudo
6405 removed inherit pseudo <- another removed inherit pseudo
6406 removed inherit pseudo <- original pseudo
6408 removed_split_pseudo <- original_reg
6409 original_reg <- removed_split_pseudo */
6411 if (lra_dump_file != NULL)
6413 fprintf (lra_dump_file, " Removing %s:\n",
6414 bitmap_bit_p (&lra_split_regs, sregno)
6415 || bitmap_bit_p (&lra_split_regs, dregno)
6416 ? "split" : "inheritance");
6417 dump_insn_slim (lra_dump_file, curr_insn);
6419 delete_move_and_clobber (curr_insn, dregno);
6420 done_p = true;
6422 else if (bitmap_bit_p (remove_pseudos, sregno)
6423 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
6425 /* Search the following pattern:
6426 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
6427 original_pseudo <- inherit_or_split_pseudo1
6428 where the 2nd insn is the current insn and
6429 inherit_or_split_pseudo2 is not removed. If it is found,
6430 change the current insn onto:
6431 original_pseudo <- inherit_or_split_pseudo2. */
6432 for (prev_insn = PREV_INSN (curr_insn);
6433 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
6434 prev_insn = PREV_INSN (prev_insn))
6436 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
6437 && (prev_set = single_set (prev_insn)) != NULL_RTX
6438 /* There should be no subregs in insn we are
6439 searching because only the original reg might
6440 be in subreg when we changed the mode of
6441 load/store for splitting. */
6442 && REG_P (SET_DEST (prev_set))
6443 && REG_P (SET_SRC (prev_set))
6444 && (int) REGNO (SET_DEST (prev_set)) == sregno
6445 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
6446 >= FIRST_PSEUDO_REGISTER)
6447 && (lra_reg_info[prev_sregno].restore_rtx == NULL_RTX
6449 /* As we consider chain of inheritance or
6450 splitting described in above comment we should
6451 check that sregno and prev_sregno were
6452 inheritance/split pseudos created from the
6453 same original regno. */
6454 (get_regno (lra_reg_info[sregno].restore_rtx) >= 0
6455 && (get_regno (lra_reg_info[sregno].restore_rtx)
6456 == get_regno (lra_reg_info[prev_sregno].restore_rtx))))
6457 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
6459 lra_assert (GET_MODE (SET_SRC (prev_set))
6460 == GET_MODE (regno_reg_rtx[sregno]));
6461 if (GET_CODE (SET_SRC (set)) == SUBREG)
6462 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
6463 else
6464 SET_SRC (set) = SET_SRC (prev_set);
6465 /* As we are finishing with processing the insn
6466 here, check the destination too as it might
6467 inheritance pseudo for another pseudo. */
6468 if (bitmap_bit_p (remove_pseudos, dregno)
6469 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
6470 && (restore_rtx
6471 = lra_reg_info[dregno].restore_rtx) != NULL_RTX)
6473 if (GET_CODE (SET_DEST (set)) == SUBREG)
6474 SUBREG_REG (SET_DEST (set)) = restore_rtx;
6475 else
6476 SET_DEST (set) = restore_rtx;
6478 lra_push_insn_and_update_insn_regno_info (curr_insn);
6479 lra_set_used_insn_alternative_by_uid
6480 (INSN_UID (curr_insn), -1);
6481 done_p = true;
6482 if (lra_dump_file != NULL)
6484 fprintf (lra_dump_file, " Change reload insn:\n");
6485 dump_insn_slim (lra_dump_file, curr_insn);
6490 if (! done_p)
6492 struct lra_insn_reg *reg;
6493 bool restored_regs_p = false;
6494 bool kept_regs_p = false;
6496 curr_id = lra_get_insn_recog_data (curr_insn);
6497 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6499 regno = reg->regno;
6500 restore_rtx = lra_reg_info[regno].restore_rtx;
6501 if (restore_rtx != NULL_RTX)
6503 if (change_p && bitmap_bit_p (remove_pseudos, regno))
6505 lra_substitute_pseudo_within_insn
6506 (curr_insn, regno, restore_rtx, false);
6507 restored_regs_p = true;
6509 else
6510 kept_regs_p = true;
6513 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
6515 /* The instruction has changed since the previous
6516 constraints pass. */
6517 lra_push_insn_and_update_insn_regno_info (curr_insn);
6518 lra_set_used_insn_alternative_by_uid
6519 (INSN_UID (curr_insn), -1);
6521 else if (restored_regs_p)
6522 /* The instruction has been restored to the form that
6523 it had during the previous constraints pass. */
6524 lra_update_insn_regno_info (curr_insn);
6525 if (restored_regs_p && lra_dump_file != NULL)
6527 fprintf (lra_dump_file, " Insn after restoring regs:\n");
6528 dump_insn_slim (lra_dump_file, curr_insn);
6533 return change_p;
6536 /* If optional reload pseudos failed to get a hard register or was not
6537 inherited, it is better to remove optional reloads. We do this
6538 transformation after undoing inheritance to figure out necessity to
6539 remove optional reloads easier. Return true if we do any
6540 change. */
6541 static bool
6542 undo_optional_reloads (void)
6544 bool change_p, keep_p;
6545 unsigned int regno, uid;
6546 bitmap_iterator bi, bi2;
6547 rtx_insn *insn;
6548 rtx set, src, dest;
6549 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
6551 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
6552 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
6553 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6555 keep_p = false;
6556 /* Keep optional reloads from previous subpasses. */
6557 if (lra_reg_info[regno].restore_rtx == NULL_RTX
6558 /* If the original pseudo changed its allocation, just
6559 removing the optional pseudo is dangerous as the original
6560 pseudo will have longer live range. */
6561 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] >= 0)
6562 keep_p = true;
6563 else if (reg_renumber[regno] >= 0)
6564 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
6566 insn = lra_insn_recog_data[uid]->insn;
6567 if ((set = single_set (insn)) == NULL_RTX)
6568 continue;
6569 src = SET_SRC (set);
6570 dest = SET_DEST (set);
6571 if (! REG_P (src) || ! REG_P (dest))
6572 continue;
6573 if (REGNO (dest) == regno
6574 /* Ignore insn for optional reloads itself. */
6575 && REGNO (lra_reg_info[regno].restore_rtx) != REGNO (src)
6576 /* Check only inheritance on last inheritance pass. */
6577 && (int) REGNO (src) >= new_regno_start
6578 /* Check that the optional reload was inherited. */
6579 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6581 keep_p = true;
6582 break;
6585 if (keep_p)
6587 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6588 if (lra_dump_file != NULL)
6589 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6592 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6593 bitmap_initialize (&insn_bitmap, &reg_obstack);
6594 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6596 if (lra_dump_file != NULL)
6597 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6598 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6599 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6601 insn = lra_insn_recog_data[uid]->insn;
6602 if ((set = single_set (insn)) != NULL_RTX)
6604 src = SET_SRC (set);
6605 dest = SET_DEST (set);
6606 if (REG_P (src) && REG_P (dest)
6607 && ((REGNO (src) == regno
6608 && (REGNO (lra_reg_info[regno].restore_rtx)
6609 == REGNO (dest)))
6610 || (REGNO (dest) == regno
6611 && (REGNO (lra_reg_info[regno].restore_rtx)
6612 == REGNO (src)))))
6614 if (lra_dump_file != NULL)
6616 fprintf (lra_dump_file, " Deleting move %u\n",
6617 INSN_UID (insn));
6618 dump_insn_slim (lra_dump_file, insn);
6620 delete_move_and_clobber (insn, REGNO (dest));
6621 continue;
6623 /* We should not worry about generation memory-memory
6624 moves here as if the corresponding inheritance did
6625 not work (inheritance pseudo did not get a hard reg),
6626 we remove the inheritance pseudo and the optional
6627 reload. */
6629 lra_substitute_pseudo_within_insn
6630 (insn, regno, lra_reg_info[regno].restore_rtx, false);
6631 lra_update_insn_regno_info (insn);
6632 if (lra_dump_file != NULL)
6634 fprintf (lra_dump_file,
6635 " Restoring original insn:\n");
6636 dump_insn_slim (lra_dump_file, insn);
6640 /* Clear restore_regnos. */
6641 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6642 lra_reg_info[regno].restore_rtx = NULL_RTX;
6643 bitmap_clear (&insn_bitmap);
6644 bitmap_clear (&removed_optional_reload_pseudos);
6645 return change_p;
6648 /* Entry function for undoing inheritance/split transformation. Return true
6649 if we did any RTL change in this pass. */
6650 bool
6651 lra_undo_inheritance (void)
6653 unsigned int regno;
6654 int hard_regno;
6655 int n_all_inherit, n_inherit, n_all_split, n_split;
6656 rtx restore_rtx;
6657 bitmap_head remove_pseudos;
6658 bitmap_iterator bi;
6659 bool change_p;
6661 lra_undo_inheritance_iter++;
6662 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6663 return false;
6664 if (lra_dump_file != NULL)
6665 fprintf (lra_dump_file,
6666 "\n********** Undoing inheritance #%d: **********\n\n",
6667 lra_undo_inheritance_iter);
6668 bitmap_initialize (&remove_pseudos, &reg_obstack);
6669 n_inherit = n_all_inherit = 0;
6670 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6671 if (lra_reg_info[regno].restore_rtx != NULL_RTX)
6673 n_all_inherit++;
6674 if (reg_renumber[regno] < 0
6675 /* If the original pseudo changed its allocation, just
6676 removing inheritance is dangerous as for changing
6677 allocation we used shorter live-ranges. */
6678 && (! REG_P (lra_reg_info[regno].restore_rtx)
6679 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] < 0))
6680 bitmap_set_bit (&remove_pseudos, regno);
6681 else
6682 n_inherit++;
6684 if (lra_dump_file != NULL && n_all_inherit != 0)
6685 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6686 n_inherit, n_all_inherit,
6687 (double) n_inherit / n_all_inherit * 100);
6688 n_split = n_all_split = 0;
6689 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6690 if ((restore_rtx = lra_reg_info[regno].restore_rtx) != NULL_RTX)
6692 int restore_regno = REGNO (restore_rtx);
6694 n_all_split++;
6695 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6696 ? reg_renumber[restore_regno] : restore_regno);
6697 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6698 bitmap_set_bit (&remove_pseudos, regno);
6699 else
6701 n_split++;
6702 if (lra_dump_file != NULL)
6703 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6704 regno, restore_regno);
6707 if (lra_dump_file != NULL && n_all_split != 0)
6708 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6709 n_split, n_all_split,
6710 (double) n_split / n_all_split * 100);
6711 change_p = remove_inheritance_pseudos (&remove_pseudos);
6712 bitmap_clear (&remove_pseudos);
6713 /* Clear restore_regnos. */
6714 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6715 lra_reg_info[regno].restore_rtx = NULL_RTX;
6716 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6717 lra_reg_info[regno].restore_rtx = NULL_RTX;
6718 change_p = undo_optional_reloads () || change_p;
6719 return change_p;