Simplify X / X, 0 / X and X % X
[official-gcc.git] / gcc / lra-constraints.c
blob56b65ef81af79e4ccea6d9072ee4e9bc2fe24b21
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 (!(MEM_ALIGN (reg) < GET_MODE_ALIGNMENT (mode)
1490 && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg)))
1491 || (MEM_ALIGN (reg) < GET_MODE_ALIGNMENT (innermode)
1492 && SLOW_UNALIGNED_ACCESS (innermode, MEM_ALIGN (reg))))
1493 return true;
1495 /* INNERMODE is fast, MODE slow. Reload the mem in INNERMODE. */
1496 enum reg_class rclass
1497 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1498 if (get_reload_reg (curr_static_id->operand[nop].type, innermode, reg,
1499 rclass, TRUE, "slow mem", &new_reg))
1501 bool insert_before, insert_after;
1502 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1504 insert_before = (type != OP_OUT
1505 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1506 insert_after = type != OP_IN;
1507 insert_move_for_subreg (insert_before ? &before : NULL,
1508 insert_after ? &after : NULL,
1509 reg, new_reg);
1511 *curr_id->operand_loc[nop] = operand;
1512 SUBREG_REG (operand) = new_reg;
1514 /* Convert to MODE. */
1515 reg = operand;
1516 rclass = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1517 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1518 rclass, TRUE, "slow mem", &new_reg))
1520 bool insert_before, insert_after;
1521 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1523 insert_before = type != OP_OUT;
1524 insert_after = type != OP_IN;
1525 insert_move_for_subreg (insert_before ? &before : NULL,
1526 insert_after ? &after : NULL,
1527 reg, new_reg);
1529 *curr_id->operand_loc[nop] = new_reg;
1530 lra_process_new_insns (curr_insn, before, after,
1531 "Inserting slow mem reload");
1532 return true;
1535 /* If the address was valid and became invalid, prefer to reload
1536 the memory. Typical case is when the index scale should
1537 correspond the memory. */
1538 *curr_id->operand_loc[nop] = operand;
1540 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1542 alter_subreg (curr_id->operand_loc[nop], false);
1543 return true;
1545 else if (CONSTANT_P (reg))
1547 /* Try to simplify subreg of constant. It is usually result of
1548 equivalence substitution. */
1549 if (innermode == VOIDmode
1550 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1551 innermode = curr_static_id->operand[nop].mode;
1552 if ((new_reg = simplify_subreg (mode, reg, innermode,
1553 SUBREG_BYTE (operand))) != NULL_RTX)
1555 *curr_id->operand_loc[nop] = new_reg;
1556 return true;
1559 /* Put constant into memory when we have mixed modes. It generates
1560 a better code in most cases as it does not need a secondary
1561 reload memory. It also prevents LRA looping when LRA is using
1562 secondary reload memory again and again. */
1563 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1564 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1566 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1567 alter_subreg (curr_id->operand_loc[nop], false);
1568 return true;
1570 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1571 if there may be a problem accessing OPERAND in the outer
1572 mode. */
1573 if ((REG_P (reg)
1574 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1575 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1576 /* Don't reload paradoxical subregs because we could be looping
1577 having repeatedly final regno out of hard regs range. */
1578 && (hard_regno_nregs[hard_regno][innermode]
1579 >= hard_regno_nregs[hard_regno][mode])
1580 && simplify_subreg_regno (hard_regno, innermode,
1581 SUBREG_BYTE (operand), mode) < 0
1582 /* Don't reload subreg for matching reload. It is actually
1583 valid subreg in LRA. */
1584 && ! LRA_SUBREG_P (operand))
1585 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1587 enum reg_class rclass;
1589 if (REG_P (reg))
1590 /* There is a big probability that we will get the same class
1591 for the new pseudo and we will get the same insn which
1592 means infinite looping. So spill the new pseudo. */
1593 rclass = NO_REGS;
1594 else
1595 /* The class will be defined later in curr_insn_transform. */
1596 rclass
1597 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1599 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1600 rclass, TRUE, "subreg reg", &new_reg))
1602 bool insert_before, insert_after;
1603 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1605 insert_before = (type != OP_OUT
1606 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1607 insert_after = (type != OP_IN);
1608 insert_move_for_subreg (insert_before ? &before : NULL,
1609 insert_after ? &after : NULL,
1610 reg, new_reg);
1612 SUBREG_REG (operand) = new_reg;
1613 lra_process_new_insns (curr_insn, before, after,
1614 "Inserting subreg reload");
1615 return true;
1617 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1618 IRA allocates hardreg to the inner pseudo reg according to its mode
1619 instead of the outermode, so the size of the hardreg may not be enough
1620 to contain the outermode operand, in that case we may need to insert
1621 reload for the reg. For the following two types of paradoxical subreg,
1622 we need to insert reload:
1623 1. If the op_type is OP_IN, and the hardreg could not be paired with
1624 other hardreg to contain the outermode operand
1625 (checked by in_hard_reg_set_p), we need to insert the reload.
1626 2. If the op_type is OP_OUT or OP_INOUT.
1628 Here is a paradoxical subreg example showing how the reload is generated:
1630 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1631 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1633 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1634 here, if reg107 is assigned to hardreg R15, because R15 is the last
1635 hardreg, compiler cannot find another hardreg to pair with R15 to
1636 contain TImode data. So we insert a TImode reload reg180 for it.
1637 After reload is inserted:
1639 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1640 (reg:DI 107 [ __comp ])) -1
1641 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1642 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1644 Two reload hard registers will be allocated to reg180 to save TImode data
1645 in LRA_assign. */
1646 else if (REG_P (reg)
1647 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1648 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1649 && (hard_regno_nregs[hard_regno][innermode]
1650 < hard_regno_nregs[hard_regno][mode])
1651 && (regclass = lra_get_allocno_class (REGNO (reg)))
1652 && (type != OP_IN
1653 || !in_hard_reg_set_p (reg_class_contents[regclass],
1654 mode, hard_regno)))
1656 /* The class will be defined later in curr_insn_transform. */
1657 enum reg_class rclass
1658 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1660 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1661 rclass, TRUE, "paradoxical subreg", &new_reg))
1663 rtx subreg;
1664 bool insert_before, insert_after;
1666 PUT_MODE (new_reg, mode);
1667 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1668 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1670 insert_before = (type != OP_OUT);
1671 insert_after = (type != OP_IN);
1672 insert_move_for_subreg (insert_before ? &before : NULL,
1673 insert_after ? &after : NULL,
1674 reg, subreg);
1676 SUBREG_REG (operand) = new_reg;
1677 lra_process_new_insns (curr_insn, before, after,
1678 "Inserting paradoxical subreg reload");
1679 return true;
1681 return false;
1684 /* Return TRUE if X refers for a hard register from SET. */
1685 static bool
1686 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1688 int i, j, x_hard_regno;
1689 machine_mode mode;
1690 const char *fmt;
1691 enum rtx_code code;
1693 if (x == NULL_RTX)
1694 return false;
1695 code = GET_CODE (x);
1696 mode = GET_MODE (x);
1697 if (code == SUBREG)
1699 x = SUBREG_REG (x);
1700 code = GET_CODE (x);
1701 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1702 mode = GET_MODE (x);
1705 if (REG_P (x))
1707 x_hard_regno = get_hard_regno (x, true);
1708 return (x_hard_regno >= 0
1709 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1711 if (MEM_P (x))
1713 struct address_info ad;
1715 decompose_mem_address (&ad, x);
1716 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1717 return true;
1718 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1719 return true;
1721 fmt = GET_RTX_FORMAT (code);
1722 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1724 if (fmt[i] == 'e')
1726 if (uses_hard_regs_p (XEXP (x, i), set))
1727 return true;
1729 else if (fmt[i] == 'E')
1731 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1732 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1733 return true;
1736 return false;
1739 /* Return true if OP is a spilled pseudo. */
1740 static inline bool
1741 spilled_pseudo_p (rtx op)
1743 return (REG_P (op)
1744 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1747 /* Return true if X is a general constant. */
1748 static inline bool
1749 general_constant_p (rtx x)
1751 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1754 static bool
1755 reg_in_class_p (rtx reg, enum reg_class cl)
1757 if (cl == NO_REGS)
1758 return get_reg_class (REGNO (reg)) == NO_REGS;
1759 return in_class_p (reg, cl, NULL);
1762 /* Return true if SET of RCLASS contains no hard regs which can be
1763 used in MODE. */
1764 static bool
1765 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1766 HARD_REG_SET &set,
1767 enum machine_mode mode)
1769 HARD_REG_SET temp;
1771 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1772 COPY_HARD_REG_SET (temp, set);
1773 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1774 return (hard_reg_set_subset_p
1775 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1778 /* Major function to choose the current insn alternative and what
1779 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1780 negative we should consider only this alternative. Return false if
1781 we can not choose the alternative or find how to reload the
1782 operands. */
1783 static bool
1784 process_alt_operands (int only_alternative)
1786 bool ok_p = false;
1787 int nop, overall, nalt;
1788 int n_alternatives = curr_static_id->n_alternatives;
1789 int n_operands = curr_static_id->n_operands;
1790 /* LOSERS counts the operands that don't fit this alternative and
1791 would require loading. */
1792 int losers;
1793 /* REJECT is a count of how undesirable this alternative says it is
1794 if any reloading is required. If the alternative matches exactly
1795 then REJECT is ignored, but otherwise it gets this much counted
1796 against it in addition to the reloading needed. */
1797 int reject;
1798 int op_reject;
1799 /* The number of elements in the following array. */
1800 int early_clobbered_regs_num;
1801 /* Numbers of operands which are early clobber registers. */
1802 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1803 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1804 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1805 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1806 bool curr_alt_win[MAX_RECOG_OPERANDS];
1807 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1808 int curr_alt_matches[MAX_RECOG_OPERANDS];
1809 /* The number of elements in the following array. */
1810 int curr_alt_dont_inherit_ops_num;
1811 /* Numbers of operands whose reload pseudos should not be inherited. */
1812 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1813 rtx op;
1814 /* The register when the operand is a subreg of register, otherwise the
1815 operand itself. */
1816 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1817 /* The register if the operand is a register or subreg of register,
1818 otherwise NULL. */
1819 rtx operand_reg[MAX_RECOG_OPERANDS];
1820 int hard_regno[MAX_RECOG_OPERANDS];
1821 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1822 int reload_nregs, reload_sum;
1823 bool costly_p;
1824 enum reg_class cl;
1826 /* Calculate some data common for all alternatives to speed up the
1827 function. */
1828 for (nop = 0; nop < n_operands; nop++)
1830 rtx reg;
1832 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1833 /* The real hard regno of the operand after the allocation. */
1834 hard_regno[nop] = get_hard_regno (op, true);
1836 operand_reg[nop] = reg = op;
1837 biggest_mode[nop] = GET_MODE (op);
1838 if (GET_CODE (op) == SUBREG)
1840 operand_reg[nop] = reg = SUBREG_REG (op);
1841 if (GET_MODE_SIZE (biggest_mode[nop])
1842 < GET_MODE_SIZE (GET_MODE (reg)))
1843 biggest_mode[nop] = GET_MODE (reg);
1845 if (! REG_P (reg))
1846 operand_reg[nop] = NULL_RTX;
1847 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1848 || ((int) REGNO (reg)
1849 == lra_get_elimination_hard_regno (REGNO (reg))))
1850 no_subreg_reg_operand[nop] = reg;
1851 else
1852 operand_reg[nop] = no_subreg_reg_operand[nop]
1853 /* Just use natural mode for elimination result. It should
1854 be enough for extra constraints hooks. */
1855 = regno_reg_rtx[hard_regno[nop]];
1858 /* The constraints are made of several alternatives. Each operand's
1859 constraint looks like foo,bar,... with commas separating the
1860 alternatives. The first alternatives for all operands go
1861 together, the second alternatives go together, etc.
1863 First loop over alternatives. */
1864 alternative_mask preferred = curr_id->preferred_alternatives;
1865 if (only_alternative >= 0)
1866 preferred &= ALTERNATIVE_BIT (only_alternative);
1868 for (nalt = 0; nalt < n_alternatives; nalt++)
1870 /* Loop over operands for one constraint alternative. */
1871 if (!TEST_BIT (preferred, nalt))
1872 continue;
1874 overall = losers = reject = reload_nregs = reload_sum = 0;
1875 for (nop = 0; nop < n_operands; nop++)
1877 int inc = (curr_static_id
1878 ->operand_alternative[nalt * n_operands + nop].reject);
1879 if (lra_dump_file != NULL && inc != 0)
1880 fprintf (lra_dump_file,
1881 " Staticly defined alt reject+=%d\n", inc);
1882 reject += inc;
1884 early_clobbered_regs_num = 0;
1886 for (nop = 0; nop < n_operands; nop++)
1888 const char *p;
1889 char *end;
1890 int len, c, m, i, opalt_num, this_alternative_matches;
1891 bool win, did_match, offmemok, early_clobber_p;
1892 /* false => this operand can be reloaded somehow for this
1893 alternative. */
1894 bool badop;
1895 /* true => this operand can be reloaded if the alternative
1896 allows regs. */
1897 bool winreg;
1898 /* True if a constant forced into memory would be OK for
1899 this operand. */
1900 bool constmemok;
1901 enum reg_class this_alternative, this_costly_alternative;
1902 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1903 bool this_alternative_match_win, this_alternative_win;
1904 bool this_alternative_offmemok;
1905 bool scratch_p;
1906 machine_mode mode;
1907 enum constraint_num cn;
1909 opalt_num = nalt * n_operands + nop;
1910 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1912 /* Fast track for no constraints at all. */
1913 curr_alt[nop] = NO_REGS;
1914 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1915 curr_alt_win[nop] = true;
1916 curr_alt_match_win[nop] = false;
1917 curr_alt_offmemok[nop] = false;
1918 curr_alt_matches[nop] = -1;
1919 continue;
1922 op = no_subreg_reg_operand[nop];
1923 mode = curr_operand_mode[nop];
1925 win = did_match = winreg = offmemok = constmemok = false;
1926 badop = true;
1928 early_clobber_p = false;
1929 p = curr_static_id->operand_alternative[opalt_num].constraint;
1931 this_costly_alternative = this_alternative = NO_REGS;
1932 /* We update set of possible hard regs besides its class
1933 because reg class might be inaccurate. For example,
1934 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1935 is translated in HI_REGS because classes are merged by
1936 pairs and there is no accurate intermediate class. */
1937 CLEAR_HARD_REG_SET (this_alternative_set);
1938 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1939 this_alternative_win = false;
1940 this_alternative_match_win = false;
1941 this_alternative_offmemok = false;
1942 this_alternative_matches = -1;
1944 /* An empty constraint should be excluded by the fast
1945 track. */
1946 lra_assert (*p != 0 && *p != ',');
1948 op_reject = 0;
1949 /* Scan this alternative's specs for this operand; set WIN
1950 if the operand fits any letter in this alternative.
1951 Otherwise, clear BADOP if this operand could fit some
1952 letter after reloads, or set WINREG if this operand could
1953 fit after reloads provided the constraint allows some
1954 registers. */
1955 costly_p = false;
1958 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1960 case '\0':
1961 len = 0;
1962 break;
1963 case ',':
1964 c = '\0';
1965 break;
1967 case '&':
1968 early_clobber_p = true;
1969 break;
1971 case '$':
1972 op_reject += LRA_MAX_REJECT;
1973 break;
1974 case '^':
1975 op_reject += LRA_LOSER_COST_FACTOR;
1976 break;
1978 case '#':
1979 /* Ignore rest of this alternative. */
1980 c = '\0';
1981 break;
1983 case '0': case '1': case '2': case '3': case '4':
1984 case '5': case '6': case '7': case '8': case '9':
1986 int m_hregno;
1987 bool match_p;
1989 m = strtoul (p, &end, 10);
1990 p = end;
1991 len = 0;
1992 lra_assert (nop > m);
1994 this_alternative_matches = m;
1995 m_hregno = get_hard_regno (*curr_id->operand_loc[m], false);
1996 /* We are supposed to match a previous operand.
1997 If we do, we win if that one did. If we do
1998 not, count both of the operands as losers.
1999 (This is too conservative, since most of the
2000 time only a single reload insn will be needed
2001 to make the two operands win. As a result,
2002 this alternative may be rejected when it is
2003 actually desirable.) */
2004 match_p = false;
2005 if (operands_match_p (*curr_id->operand_loc[nop],
2006 *curr_id->operand_loc[m], m_hregno))
2008 /* We should reject matching of an early
2009 clobber operand if the matching operand is
2010 not dying in the insn. */
2011 if (! curr_static_id->operand[m].early_clobber
2012 || operand_reg[nop] == NULL_RTX
2013 || (find_regno_note (curr_insn, REG_DEAD,
2014 REGNO (op))
2015 || REGNO (op) == REGNO (operand_reg[m])))
2016 match_p = true;
2018 if (match_p)
2020 /* If we are matching a non-offsettable
2021 address where an offsettable address was
2022 expected, then we must reject this
2023 combination, because we can't reload
2024 it. */
2025 if (curr_alt_offmemok[m]
2026 && MEM_P (*curr_id->operand_loc[m])
2027 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
2028 continue;
2030 else
2032 /* Operands don't match. Both operands must
2033 allow a reload register, otherwise we
2034 cannot make them match. */
2035 if (curr_alt[m] == NO_REGS)
2036 break;
2037 /* Retroactively mark the operand we had to
2038 match as a loser, if it wasn't already and
2039 it wasn't matched to a register constraint
2040 (e.g it might be matched by memory). */
2041 if (curr_alt_win[m]
2042 && (operand_reg[m] == NULL_RTX
2043 || hard_regno[m] < 0))
2045 losers++;
2046 reload_nregs
2047 += (ira_reg_class_max_nregs[curr_alt[m]]
2048 [GET_MODE (*curr_id->operand_loc[m])]);
2051 /* Prefer matching earlyclobber alternative as
2052 it results in less hard regs required for
2053 the insn than a non-matching earlyclobber
2054 alternative. */
2055 if (curr_static_id->operand[m].early_clobber)
2057 if (lra_dump_file != NULL)
2058 fprintf
2059 (lra_dump_file,
2060 " %d Matching earlyclobber alt:"
2061 " reject--\n",
2062 nop);
2063 reject--;
2065 /* Otherwise we prefer no matching
2066 alternatives because it gives more freedom
2067 in RA. */
2068 else if (operand_reg[nop] == NULL_RTX
2069 || (find_regno_note (curr_insn, REG_DEAD,
2070 REGNO (operand_reg[nop]))
2071 == NULL_RTX))
2073 if (lra_dump_file != NULL)
2074 fprintf
2075 (lra_dump_file,
2076 " %d Matching alt: reject+=2\n",
2077 nop);
2078 reject += 2;
2081 /* If we have to reload this operand and some
2082 previous operand also had to match the same
2083 thing as this operand, we don't know how to do
2084 that. */
2085 if (!match_p || !curr_alt_win[m])
2087 for (i = 0; i < nop; i++)
2088 if (curr_alt_matches[i] == m)
2089 break;
2090 if (i < nop)
2091 break;
2093 else
2094 did_match = true;
2096 /* This can be fixed with reloads if the operand
2097 we are supposed to match can be fixed with
2098 reloads. */
2099 badop = false;
2100 this_alternative = curr_alt[m];
2101 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
2102 winreg = this_alternative != NO_REGS;
2103 break;
2106 case 'g':
2107 if (MEM_P (op)
2108 || general_constant_p (op)
2109 || spilled_pseudo_p (op))
2110 win = true;
2111 cl = GENERAL_REGS;
2112 goto reg;
2114 default:
2115 cn = lookup_constraint (p);
2116 switch (get_constraint_type (cn))
2118 case CT_REGISTER:
2119 cl = reg_class_for_constraint (cn);
2120 if (cl != NO_REGS)
2121 goto reg;
2122 break;
2124 case CT_CONST_INT:
2125 if (CONST_INT_P (op)
2126 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2127 win = true;
2128 break;
2130 case CT_MEMORY:
2131 if (MEM_P (op)
2132 && satisfies_memory_constraint_p (op, cn))
2133 win = true;
2134 else if (spilled_pseudo_p (op))
2135 win = true;
2137 /* If we didn't already win, we can reload constants
2138 via force_const_mem or put the pseudo value into
2139 memory, or make other memory by reloading the
2140 address like for 'o'. */
2141 if (CONST_POOL_OK_P (mode, op)
2142 || MEM_P (op) || REG_P (op)
2143 /* We can restore the equiv insn by a
2144 reload. */
2145 || equiv_substition_p[nop])
2146 badop = false;
2147 constmemok = true;
2148 offmemok = true;
2149 break;
2151 case CT_ADDRESS:
2152 /* If we didn't already win, we can reload the address
2153 into a base register. */
2154 if (satisfies_address_constraint_p (op, cn))
2155 win = true;
2156 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2157 ADDRESS, SCRATCH);
2158 badop = false;
2159 goto reg;
2161 case CT_FIXED_FORM:
2162 if (constraint_satisfied_p (op, cn))
2163 win = true;
2164 break;
2166 case CT_SPECIAL_MEMORY:
2167 if (MEM_P (op)
2168 && satisfies_memory_constraint_p (op, cn))
2169 win = true;
2170 else if (spilled_pseudo_p (op))
2171 win = true;
2172 break;
2174 break;
2176 reg:
2177 this_alternative = reg_class_subunion[this_alternative][cl];
2178 IOR_HARD_REG_SET (this_alternative_set,
2179 reg_class_contents[cl]);
2180 if (costly_p)
2182 this_costly_alternative
2183 = reg_class_subunion[this_costly_alternative][cl];
2184 IOR_HARD_REG_SET (this_costly_alternative_set,
2185 reg_class_contents[cl]);
2187 if (mode == BLKmode)
2188 break;
2189 winreg = true;
2190 if (REG_P (op))
2192 if (hard_regno[nop] >= 0
2193 && in_hard_reg_set_p (this_alternative_set,
2194 mode, hard_regno[nop]))
2195 win = true;
2196 else if (hard_regno[nop] < 0
2197 && in_class_p (op, this_alternative, NULL))
2198 win = true;
2200 break;
2202 if (c != ' ' && c != '\t')
2203 costly_p = c == '*';
2205 while ((p += len), c);
2207 scratch_p = (operand_reg[nop] != NULL_RTX
2208 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2209 /* Record which operands fit this alternative. */
2210 if (win)
2212 this_alternative_win = true;
2213 if (operand_reg[nop] != NULL_RTX)
2215 if (hard_regno[nop] >= 0)
2217 if (in_hard_reg_set_p (this_costly_alternative_set,
2218 mode, hard_regno[nop]))
2220 if (lra_dump_file != NULL)
2221 fprintf (lra_dump_file,
2222 " %d Costly set: reject++\n",
2223 nop);
2224 reject++;
2227 else
2229 /* Prefer won reg to spilled pseudo under other
2230 equal conditions for possibe inheritance. */
2231 if (! scratch_p)
2233 if (lra_dump_file != NULL)
2234 fprintf
2235 (lra_dump_file,
2236 " %d Non pseudo reload: reject++\n",
2237 nop);
2238 reject++;
2240 if (in_class_p (operand_reg[nop],
2241 this_costly_alternative, NULL))
2243 if (lra_dump_file != NULL)
2244 fprintf
2245 (lra_dump_file,
2246 " %d Non pseudo costly reload:"
2247 " reject++\n",
2248 nop);
2249 reject++;
2252 /* We simulate the behavior of old reload here.
2253 Although scratches need hard registers and it
2254 might result in spilling other pseudos, no reload
2255 insns are generated for the scratches. So it
2256 might cost something but probably less than old
2257 reload pass believes. */
2258 if (scratch_p)
2260 if (lra_dump_file != NULL)
2261 fprintf (lra_dump_file,
2262 " %d Scratch win: reject+=2\n",
2263 nop);
2264 reject += 2;
2268 else if (did_match)
2269 this_alternative_match_win = true;
2270 else
2272 int const_to_mem = 0;
2273 bool no_regs_p;
2275 reject += op_reject;
2276 /* Never do output reload of stack pointer. It makes
2277 impossible to do elimination when SP is changed in
2278 RTL. */
2279 if (op == stack_pointer_rtx && ! frame_pointer_needed
2280 && curr_static_id->operand[nop].type != OP_IN)
2281 goto fail;
2283 /* If this alternative asks for a specific reg class, see if there
2284 is at least one allocatable register in that class. */
2285 no_regs_p
2286 = (this_alternative == NO_REGS
2287 || (hard_reg_set_subset_p
2288 (reg_class_contents[this_alternative],
2289 lra_no_alloc_regs)));
2291 /* For asms, verify that the class for this alternative is possible
2292 for the mode that is specified. */
2293 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2295 int i;
2296 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2297 if (HARD_REGNO_MODE_OK (i, mode)
2298 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2299 mode, i))
2300 break;
2301 if (i == FIRST_PSEUDO_REGISTER)
2302 winreg = false;
2305 /* If this operand accepts a register, and if the
2306 register class has at least one allocatable register,
2307 then this operand can be reloaded. */
2308 if (winreg && !no_regs_p)
2309 badop = false;
2311 if (badop)
2313 if (lra_dump_file != NULL)
2314 fprintf (lra_dump_file,
2315 " alt=%d: Bad operand -- refuse\n",
2316 nalt);
2317 goto fail;
2320 if (this_alternative != NO_REGS)
2322 HARD_REG_SET available_regs;
2324 COPY_HARD_REG_SET (available_regs,
2325 reg_class_contents[this_alternative]);
2326 AND_COMPL_HARD_REG_SET
2327 (available_regs,
2328 ira_prohibited_class_mode_regs[this_alternative][mode]);
2329 AND_COMPL_HARD_REG_SET (available_regs, lra_no_alloc_regs);
2330 if (hard_reg_set_empty_p (available_regs))
2332 /* There are no hard regs holding a value of given
2333 mode. */
2334 if (offmemok)
2336 this_alternative = NO_REGS;
2337 if (lra_dump_file != NULL)
2338 fprintf (lra_dump_file,
2339 " %d Using memory because of"
2340 " a bad mode: reject+=2\n",
2341 nop);
2342 reject += 2;
2344 else
2346 if (lra_dump_file != NULL)
2347 fprintf (lra_dump_file,
2348 " alt=%d: Wrong mode -- refuse\n",
2349 nalt);
2350 goto fail;
2355 /* If not assigned pseudo has a class which a subset of
2356 required reg class, it is a less costly alternative
2357 as the pseudo still can get a hard reg of necessary
2358 class. */
2359 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2360 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2361 && ira_class_subset_p[this_alternative][cl])
2363 if (lra_dump_file != NULL)
2364 fprintf
2365 (lra_dump_file,
2366 " %d Super set class reg: reject-=3\n", nop);
2367 reject -= 3;
2370 this_alternative_offmemok = offmemok;
2371 if (this_costly_alternative != NO_REGS)
2373 if (lra_dump_file != NULL)
2374 fprintf (lra_dump_file,
2375 " %d Costly loser: reject++\n", nop);
2376 reject++;
2378 /* If the operand is dying, has a matching constraint,
2379 and satisfies constraints of the matched operand
2380 which failed to satisfy the own constraints, most probably
2381 the reload for this operand will be gone. */
2382 if (this_alternative_matches >= 0
2383 && !curr_alt_win[this_alternative_matches]
2384 && REG_P (op)
2385 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2386 && (hard_regno[nop] >= 0
2387 ? in_hard_reg_set_p (this_alternative_set,
2388 mode, hard_regno[nop])
2389 : in_class_p (op, this_alternative, NULL)))
2391 if (lra_dump_file != NULL)
2392 fprintf
2393 (lra_dump_file,
2394 " %d Dying matched operand reload: reject++\n",
2395 nop);
2396 reject++;
2398 else
2400 /* Strict_low_part requires to reload the register
2401 not the sub-register. In this case we should
2402 check that a final reload hard reg can hold the
2403 value mode. */
2404 if (curr_static_id->operand[nop].strict_low
2405 && REG_P (op)
2406 && hard_regno[nop] < 0
2407 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2408 && ira_class_hard_regs_num[this_alternative] > 0
2409 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2410 [this_alternative][0],
2411 GET_MODE
2412 (*curr_id->operand_loc[nop])))
2414 if (lra_dump_file != NULL)
2415 fprintf
2416 (lra_dump_file,
2417 " alt=%d: Strict low subreg reload -- refuse\n",
2418 nalt);
2419 goto fail;
2421 losers++;
2423 if (operand_reg[nop] != NULL_RTX
2424 /* Output operands and matched input operands are
2425 not inherited. The following conditions do not
2426 exactly describe the previous statement but they
2427 are pretty close. */
2428 && curr_static_id->operand[nop].type != OP_OUT
2429 && (this_alternative_matches < 0
2430 || curr_static_id->operand[nop].type != OP_IN))
2432 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2433 (operand_reg[nop])]
2434 .last_reload);
2436 /* The value of reload_sum has sense only if we
2437 process insns in their order. It happens only on
2438 the first constraints sub-pass when we do most of
2439 reload work. */
2440 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2441 reload_sum += last_reload - bb_reload_num;
2443 /* If this is a constant that is reloaded into the
2444 desired class by copying it to memory first, count
2445 that as another reload. This is consistent with
2446 other code and is required to avoid choosing another
2447 alternative when the constant is moved into memory.
2448 Note that the test here is precisely the same as in
2449 the code below that calls force_const_mem. */
2450 if (CONST_POOL_OK_P (mode, op)
2451 && ((targetm.preferred_reload_class
2452 (op, this_alternative) == NO_REGS)
2453 || no_input_reloads_p))
2455 const_to_mem = 1;
2456 if (! no_regs_p)
2457 losers++;
2460 /* Alternative loses if it requires a type of reload not
2461 permitted for this insn. We can always reload
2462 objects with a REG_UNUSED note. */
2463 if ((curr_static_id->operand[nop].type != OP_IN
2464 && no_output_reloads_p
2465 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2466 || (curr_static_id->operand[nop].type != OP_OUT
2467 && no_input_reloads_p && ! const_to_mem)
2468 || (this_alternative_matches >= 0
2469 && (no_input_reloads_p
2470 || (no_output_reloads_p
2471 && (curr_static_id->operand
2472 [this_alternative_matches].type != OP_IN)
2473 && ! find_reg_note (curr_insn, REG_UNUSED,
2474 no_subreg_reg_operand
2475 [this_alternative_matches])))))
2477 if (lra_dump_file != NULL)
2478 fprintf
2479 (lra_dump_file,
2480 " alt=%d: No input/otput reload -- refuse\n",
2481 nalt);
2482 goto fail;
2485 /* Alternative loses if it required class pseudo can not
2486 hold value of required mode. Such insns can be
2487 described by insn definitions with mode iterators. */
2488 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2489 && ! hard_reg_set_empty_p (this_alternative_set)
2490 /* It is common practice for constraints to use a
2491 class which does not have actually enough regs to
2492 hold the value (e.g. x86 AREG for mode requiring
2493 more one general reg). Therefore we have 2
2494 conditions to check that the reload pseudo can
2495 not hold the mode value. */
2496 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2497 [this_alternative][0],
2498 GET_MODE (*curr_id->operand_loc[nop]))
2499 /* The above condition is not enough as the first
2500 reg in ira_class_hard_regs can be not aligned for
2501 multi-words mode values. */
2502 && (prohibited_class_reg_set_mode_p
2503 (this_alternative, this_alternative_set,
2504 GET_MODE (*curr_id->operand_loc[nop]))))
2506 if (lra_dump_file != NULL)
2507 fprintf (lra_dump_file,
2508 " alt=%d: reload pseudo for op %d "
2509 " can not hold the mode value -- refuse\n",
2510 nalt, nop);
2511 goto fail;
2514 /* Check strong discouragement of reload of non-constant
2515 into class THIS_ALTERNATIVE. */
2516 if (! CONSTANT_P (op) && ! no_regs_p
2517 && (targetm.preferred_reload_class
2518 (op, this_alternative) == NO_REGS
2519 || (curr_static_id->operand[nop].type == OP_OUT
2520 && (targetm.preferred_output_reload_class
2521 (op, this_alternative) == NO_REGS))))
2523 if (lra_dump_file != NULL)
2524 fprintf (lra_dump_file,
2525 " %d Non-prefered reload: reject+=%d\n",
2526 nop, LRA_MAX_REJECT);
2527 reject += LRA_MAX_REJECT;
2530 if (! (MEM_P (op) && offmemok)
2531 && ! (const_to_mem && constmemok))
2533 /* We prefer to reload pseudos over reloading other
2534 things, since such reloads may be able to be
2535 eliminated later. So bump REJECT in other cases.
2536 Don't do this in the case where we are forcing a
2537 constant into memory and it will then win since
2538 we don't want to have a different alternative
2539 match then. */
2540 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2542 if (lra_dump_file != NULL)
2543 fprintf
2544 (lra_dump_file,
2545 " %d Non-pseudo reload: reject+=2\n",
2546 nop);
2547 reject += 2;
2550 if (! no_regs_p)
2551 reload_nregs
2552 += ira_reg_class_max_nregs[this_alternative][mode];
2554 if (SMALL_REGISTER_CLASS_P (this_alternative))
2556 if (lra_dump_file != NULL)
2557 fprintf
2558 (lra_dump_file,
2559 " %d Small class reload: reject+=%d\n",
2560 nop, LRA_LOSER_COST_FACTOR / 2);
2561 reject += LRA_LOSER_COST_FACTOR / 2;
2565 /* We are trying to spill pseudo into memory. It is
2566 usually more costly than moving to a hard register
2567 although it might takes the same number of
2568 reloads.
2570 Non-pseudo spill may happen also. Suppose a target allows both
2571 register and memory in the operand constraint alternatives,
2572 then it's typical that an eliminable register has a substition
2573 of "base + offset" which can either be reloaded by a simple
2574 "new_reg <= base + offset" which will match the register
2575 constraint, or a similar reg addition followed by further spill
2576 to and reload from memory which will match the memory
2577 constraint, but this memory spill will be much more costly
2578 usually.
2580 Code below increases the reject for both pseudo and non-pseudo
2581 spill. */
2582 if (no_regs_p
2583 && !(MEM_P (op) && offmemok)
2584 && !(REG_P (op) && hard_regno[nop] < 0))
2586 if (lra_dump_file != NULL)
2587 fprintf
2588 (lra_dump_file,
2589 " %d Spill %spseudo into memory: reject+=3\n",
2590 nop, REG_P (op) ? "" : "Non-");
2591 reject += 3;
2592 if (VECTOR_MODE_P (mode))
2594 /* Spilling vectors into memory is usually more
2595 costly as they contain big values. */
2596 if (lra_dump_file != NULL)
2597 fprintf
2598 (lra_dump_file,
2599 " %d Spill vector pseudo: reject+=2\n",
2600 nop);
2601 reject += 2;
2605 #ifdef SECONDARY_MEMORY_NEEDED
2606 /* If reload requires moving value through secondary
2607 memory, it will need one more insn at least. */
2608 if (this_alternative != NO_REGS
2609 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2610 && ((curr_static_id->operand[nop].type != OP_OUT
2611 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2612 GET_MODE (op)))
2613 || (curr_static_id->operand[nop].type != OP_IN
2614 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2615 GET_MODE (op)))))
2616 losers++;
2617 #endif
2618 /* Input reloads can be inherited more often than output
2619 reloads can be removed, so penalize output
2620 reloads. */
2621 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2623 if (lra_dump_file != NULL)
2624 fprintf
2625 (lra_dump_file,
2626 " %d Non input pseudo reload: reject++\n",
2627 nop);
2628 reject++;
2632 if (early_clobber_p && ! scratch_p)
2634 if (lra_dump_file != NULL)
2635 fprintf (lra_dump_file,
2636 " %d Early clobber: reject++\n", nop);
2637 reject++;
2639 /* ??? We check early clobbers after processing all operands
2640 (see loop below) and there we update the costs more.
2641 Should we update the cost (may be approximately) here
2642 because of early clobber register reloads or it is a rare
2643 or non-important thing to be worth to do it. */
2644 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2645 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2647 if (lra_dump_file != NULL)
2648 fprintf (lra_dump_file,
2649 " alt=%d,overall=%d,losers=%d -- refuse\n",
2650 nalt, overall, losers);
2651 goto fail;
2654 curr_alt[nop] = this_alternative;
2655 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2656 curr_alt_win[nop] = this_alternative_win;
2657 curr_alt_match_win[nop] = this_alternative_match_win;
2658 curr_alt_offmemok[nop] = this_alternative_offmemok;
2659 curr_alt_matches[nop] = this_alternative_matches;
2661 if (this_alternative_matches >= 0
2662 && !did_match && !this_alternative_win)
2663 curr_alt_win[this_alternative_matches] = false;
2665 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2666 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2668 if (curr_insn_set != NULL_RTX && n_operands == 2
2669 /* Prevent processing non-move insns. */
2670 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2671 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2672 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2673 && REG_P (no_subreg_reg_operand[0])
2674 && REG_P (no_subreg_reg_operand[1])
2675 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2676 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2677 || (! curr_alt_win[0] && curr_alt_win[1]
2678 && REG_P (no_subreg_reg_operand[1])
2679 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2680 || (curr_alt_win[0] && ! curr_alt_win[1]
2681 && REG_P (no_subreg_reg_operand[0])
2682 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2683 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2684 no_subreg_reg_operand[1])
2685 || (targetm.preferred_reload_class
2686 (no_subreg_reg_operand[1],
2687 (enum reg_class) curr_alt[1]) != NO_REGS))
2688 /* If it is a result of recent elimination in move
2689 insn we can transform it into an add still by
2690 using this alternative. */
2691 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2693 /* We have a move insn and a new reload insn will be similar
2694 to the current insn. We should avoid such situation as it
2695 results in LRA cycling. */
2696 overall += LRA_MAX_REJECT;
2698 ok_p = true;
2699 curr_alt_dont_inherit_ops_num = 0;
2700 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2702 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2703 HARD_REG_SET temp_set;
2705 i = early_clobbered_nops[nop];
2706 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2707 || hard_regno[i] < 0)
2708 continue;
2709 lra_assert (operand_reg[i] != NULL_RTX);
2710 clobbered_hard_regno = hard_regno[i];
2711 CLEAR_HARD_REG_SET (temp_set);
2712 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2713 first_conflict_j = last_conflict_j = -1;
2714 for (j = 0; j < n_operands; j++)
2715 if (j == i
2716 /* We don't want process insides of match_operator and
2717 match_parallel because otherwise we would process
2718 their operands once again generating a wrong
2719 code. */
2720 || curr_static_id->operand[j].is_operator)
2721 continue;
2722 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2723 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2724 continue;
2725 /* If we don't reload j-th operand, check conflicts. */
2726 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2727 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2729 if (first_conflict_j < 0)
2730 first_conflict_j = j;
2731 last_conflict_j = j;
2733 if (last_conflict_j < 0)
2734 continue;
2735 /* If earlyclobber operand conflicts with another
2736 non-matching operand which is actually the same register
2737 as the earlyclobber operand, it is better to reload the
2738 another operand as an operand matching the earlyclobber
2739 operand can be also the same. */
2740 if (first_conflict_j == last_conflict_j
2741 && operand_reg[last_conflict_j] != NULL_RTX
2742 && ! curr_alt_match_win[last_conflict_j]
2743 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2745 curr_alt_win[last_conflict_j] = false;
2746 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2747 = last_conflict_j;
2748 losers++;
2749 /* Early clobber was already reflected in REJECT. */
2750 lra_assert (reject > 0);
2751 if (lra_dump_file != NULL)
2752 fprintf
2753 (lra_dump_file,
2754 " %d Conflict early clobber reload: reject--\n",
2756 reject--;
2757 overall += LRA_LOSER_COST_FACTOR - 1;
2759 else
2761 /* We need to reload early clobbered register and the
2762 matched registers. */
2763 for (j = 0; j < n_operands; j++)
2764 if (curr_alt_matches[j] == i)
2766 curr_alt_match_win[j] = false;
2767 losers++;
2768 overall += LRA_LOSER_COST_FACTOR;
2770 if (! curr_alt_match_win[i])
2771 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2772 else
2774 /* Remember pseudos used for match reloads are never
2775 inherited. */
2776 lra_assert (curr_alt_matches[i] >= 0);
2777 curr_alt_win[curr_alt_matches[i]] = false;
2779 curr_alt_win[i] = curr_alt_match_win[i] = false;
2780 losers++;
2781 /* Early clobber was already reflected in REJECT. */
2782 lra_assert (reject > 0);
2783 if (lra_dump_file != NULL)
2784 fprintf
2785 (lra_dump_file,
2786 " %d Matched conflict early clobber reloads:"
2787 "reject--\n",
2789 reject--;
2790 overall += LRA_LOSER_COST_FACTOR - 1;
2793 if (lra_dump_file != NULL)
2794 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2795 nalt, overall, losers, reload_nregs);
2797 /* If this alternative can be made to work by reloading, and it
2798 needs less reloading than the others checked so far, record
2799 it as the chosen goal for reloading. */
2800 if ((best_losers != 0 && losers == 0)
2801 || (((best_losers == 0 && losers == 0)
2802 || (best_losers != 0 && losers != 0))
2803 && (best_overall > overall
2804 || (best_overall == overall
2805 /* If the cost of the reloads is the same,
2806 prefer alternative which requires minimal
2807 number of reload regs. */
2808 && (reload_nregs < best_reload_nregs
2809 || (reload_nregs == best_reload_nregs
2810 && (best_reload_sum < reload_sum
2811 || (best_reload_sum == reload_sum
2812 && nalt < goal_alt_number))))))))
2814 for (nop = 0; nop < n_operands; nop++)
2816 goal_alt_win[nop] = curr_alt_win[nop];
2817 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2818 goal_alt_matches[nop] = curr_alt_matches[nop];
2819 goal_alt[nop] = curr_alt[nop];
2820 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2822 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2823 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2824 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2825 goal_alt_swapped = curr_swapped;
2826 best_overall = overall;
2827 best_losers = losers;
2828 best_reload_nregs = reload_nregs;
2829 best_reload_sum = reload_sum;
2830 goal_alt_number = nalt;
2832 if (losers == 0)
2833 /* Everything is satisfied. Do not process alternatives
2834 anymore. */
2835 break;
2836 fail:
2839 return ok_p;
2842 /* Make reload base reg from address AD. */
2843 static rtx
2844 base_to_reg (struct address_info *ad)
2846 enum reg_class cl;
2847 int code = -1;
2848 rtx new_inner = NULL_RTX;
2849 rtx new_reg = NULL_RTX;
2850 rtx_insn *insn;
2851 rtx_insn *last_insn = get_last_insn();
2853 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2854 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2855 get_index_code (ad));
2856 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2857 cl, "base");
2858 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2859 ad->disp_term == NULL
2860 ? gen_int_mode (0, ad->mode)
2861 : *ad->disp_term);
2862 if (!valid_address_p (ad->mode, new_inner, ad->as))
2863 return NULL_RTX;
2864 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base_term));
2865 code = recog_memoized (insn);
2866 if (code < 0)
2868 delete_insns_since (last_insn);
2869 return NULL_RTX;
2872 return new_inner;
2875 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2876 static rtx
2877 base_plus_disp_to_reg (struct address_info *ad)
2879 enum reg_class cl;
2880 rtx new_reg;
2882 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2883 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2884 get_index_code (ad));
2885 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2886 cl, "base + disp");
2887 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2888 return new_reg;
2891 /* Make reload of index part of address AD. Return the new
2892 pseudo. */
2893 static rtx
2894 index_part_to_reg (struct address_info *ad)
2896 rtx new_reg;
2898 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2899 INDEX_REG_CLASS, "index term");
2900 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2901 GEN_INT (get_index_scale (ad)), new_reg, 1);
2902 return new_reg;
2905 /* Return true if we can add a displacement to address AD, even if that
2906 makes the address invalid. The fix-up code requires any new address
2907 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2908 static bool
2909 can_add_disp_p (struct address_info *ad)
2911 return (!ad->autoinc_p
2912 && ad->segment == NULL
2913 && ad->base == ad->base_term
2914 && ad->disp == ad->disp_term);
2917 /* Make equiv substitution in address AD. Return true if a substitution
2918 was made. */
2919 static bool
2920 equiv_address_substitution (struct address_info *ad)
2922 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2923 HOST_WIDE_INT disp, scale;
2924 bool change_p;
2926 base_term = strip_subreg (ad->base_term);
2927 if (base_term == NULL)
2928 base_reg = new_base_reg = NULL_RTX;
2929 else
2931 base_reg = *base_term;
2932 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2934 index_term = strip_subreg (ad->index_term);
2935 if (index_term == NULL)
2936 index_reg = new_index_reg = NULL_RTX;
2937 else
2939 index_reg = *index_term;
2940 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2942 if (base_reg == new_base_reg && index_reg == new_index_reg)
2943 return false;
2944 disp = 0;
2945 change_p = false;
2946 if (lra_dump_file != NULL)
2948 fprintf (lra_dump_file, "Changing address in insn %d ",
2949 INSN_UID (curr_insn));
2950 dump_value_slim (lra_dump_file, *ad->outer, 1);
2952 if (base_reg != new_base_reg)
2954 if (REG_P (new_base_reg))
2956 *base_term = new_base_reg;
2957 change_p = true;
2959 else if (GET_CODE (new_base_reg) == PLUS
2960 && REG_P (XEXP (new_base_reg, 0))
2961 && CONST_INT_P (XEXP (new_base_reg, 1))
2962 && can_add_disp_p (ad))
2964 disp += INTVAL (XEXP (new_base_reg, 1));
2965 *base_term = XEXP (new_base_reg, 0);
2966 change_p = true;
2968 if (ad->base_term2 != NULL)
2969 *ad->base_term2 = *ad->base_term;
2971 if (index_reg != new_index_reg)
2973 if (REG_P (new_index_reg))
2975 *index_term = new_index_reg;
2976 change_p = true;
2978 else if (GET_CODE (new_index_reg) == PLUS
2979 && REG_P (XEXP (new_index_reg, 0))
2980 && CONST_INT_P (XEXP (new_index_reg, 1))
2981 && can_add_disp_p (ad)
2982 && (scale = get_index_scale (ad)))
2984 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2985 *index_term = XEXP (new_index_reg, 0);
2986 change_p = true;
2989 if (disp != 0)
2991 if (ad->disp != NULL)
2992 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2993 else
2995 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2996 update_address (ad);
2998 change_p = true;
3000 if (lra_dump_file != NULL)
3002 if (! change_p)
3003 fprintf (lra_dump_file, " -- no change\n");
3004 else
3006 fprintf (lra_dump_file, " on equiv ");
3007 dump_value_slim (lra_dump_file, *ad->outer, 1);
3008 fprintf (lra_dump_file, "\n");
3011 return change_p;
3014 /* Major function to make reloads for an address in operand NOP or
3015 check its correctness (If CHECK_ONLY_P is true). The supported
3016 cases are:
3018 1) an address that existed before LRA started, at which point it
3019 must have been valid. These addresses are subject to elimination
3020 and may have become invalid due to the elimination offset being out
3021 of range.
3023 2) an address created by forcing a constant to memory
3024 (force_const_to_mem). The initial form of these addresses might
3025 not be valid, and it is this function's job to make them valid.
3027 3) a frame address formed from a register and a (possibly zero)
3028 constant offset. As above, these addresses might not be valid and
3029 this function must make them so.
3031 Add reloads to the lists *BEFORE and *AFTER. We might need to add
3032 reloads to *AFTER because of inc/dec, {pre, post} modify in the
3033 address. Return true for any RTL change.
3035 The function is a helper function which does not produce all
3036 transformations (when CHECK_ONLY_P is false) which can be
3037 necessary. It does just basic steps. To do all necessary
3038 transformations use function process_address. */
3039 static bool
3040 process_address_1 (int nop, bool check_only_p,
3041 rtx_insn **before, rtx_insn **after)
3043 struct address_info ad;
3044 rtx new_reg;
3045 HOST_WIDE_INT scale;
3046 rtx op = *curr_id->operand_loc[nop];
3047 const char *constraint = curr_static_id->operand[nop].constraint;
3048 enum constraint_num cn = lookup_constraint (constraint);
3049 bool change_p = false;
3051 if (MEM_P (op)
3052 && GET_MODE (op) == BLKmode
3053 && GET_CODE (XEXP (op, 0)) == SCRATCH)
3054 return false;
3056 if (insn_extra_address_constraint (cn))
3057 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
3058 else if (MEM_P (op))
3059 decompose_mem_address (&ad, op);
3060 else if (GET_CODE (op) == SUBREG
3061 && MEM_P (SUBREG_REG (op)))
3062 decompose_mem_address (&ad, SUBREG_REG (op));
3063 else
3064 return false;
3065 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
3066 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
3067 when INDEX_REG_CLASS is a single register class. */
3068 if (ad.base_term != NULL
3069 && ad.index_term != NULL
3070 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
3071 && REG_P (*ad.base_term)
3072 && REG_P (*ad.index_term)
3073 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
3074 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
3076 std::swap (ad.base, ad.index);
3077 std::swap (ad.base_term, ad.index_term);
3079 if (! check_only_p)
3080 change_p = equiv_address_substitution (&ad);
3081 if (ad.base_term != NULL
3082 && (process_addr_reg
3083 (ad.base_term, check_only_p, before,
3084 (ad.autoinc_p
3085 && !(REG_P (*ad.base_term)
3086 && find_regno_note (curr_insn, REG_DEAD,
3087 REGNO (*ad.base_term)) != NULL_RTX)
3088 ? after : NULL),
3089 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3090 get_index_code (&ad)))))
3092 change_p = true;
3093 if (ad.base_term2 != NULL)
3094 *ad.base_term2 = *ad.base_term;
3096 if (ad.index_term != NULL
3097 && process_addr_reg (ad.index_term, check_only_p,
3098 before, NULL, INDEX_REG_CLASS))
3099 change_p = true;
3101 /* Target hooks sometimes don't treat extra-constraint addresses as
3102 legitimate address_operands, so handle them specially. */
3103 if (insn_extra_address_constraint (cn)
3104 && satisfies_address_constraint_p (&ad, cn))
3105 return change_p;
3107 if (check_only_p)
3108 return change_p;
3110 /* There are three cases where the shape of *AD.INNER may now be invalid:
3112 1) the original address was valid, but either elimination or
3113 equiv_address_substitution was applied and that made
3114 the address invalid.
3116 2) the address is an invalid symbolic address created by
3117 force_const_to_mem.
3119 3) the address is a frame address with an invalid offset.
3121 4) the address is a frame address with an invalid base.
3123 All these cases involve a non-autoinc address, so there is no
3124 point revalidating other types. */
3125 if (ad.autoinc_p || valid_address_p (&ad))
3126 return change_p;
3128 /* Any index existed before LRA started, so we can assume that the
3129 presence and shape of the index is valid. */
3130 push_to_sequence (*before);
3131 lra_assert (ad.disp == ad.disp_term);
3132 if (ad.base == NULL)
3134 if (ad.index == NULL)
3136 rtx_insn *insn;
3137 rtx_insn *last = get_last_insn ();
3138 int code = -1;
3139 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3140 SCRATCH, SCRATCH);
3141 rtx addr = *ad.inner;
3143 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3144 if (HAVE_lo_sum)
3146 /* addr => lo_sum (new_base, addr), case (2) above. */
3147 insn = emit_insn (gen_rtx_SET
3148 (new_reg,
3149 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
3150 code = recog_memoized (insn);
3151 if (code >= 0)
3153 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
3154 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
3156 /* Try to put lo_sum into register. */
3157 insn = emit_insn (gen_rtx_SET
3158 (new_reg,
3159 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
3160 code = recog_memoized (insn);
3161 if (code >= 0)
3163 *ad.inner = new_reg;
3164 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
3166 *ad.inner = addr;
3167 code = -1;
3173 if (code < 0)
3174 delete_insns_since (last);
3177 if (code < 0)
3179 /* addr => new_base, case (2) above. */
3180 lra_emit_move (new_reg, addr);
3182 for (insn = last == NULL_RTX ? get_insns () : NEXT_INSN (last);
3183 insn != NULL_RTX;
3184 insn = NEXT_INSN (insn))
3185 if (recog_memoized (insn) < 0)
3186 break;
3187 if (insn != NULL_RTX)
3189 /* Do nothing if we cannot generate right insns.
3190 This is analogous to reload pass behavior. */
3191 delete_insns_since (last);
3192 end_sequence ();
3193 return false;
3195 *ad.inner = new_reg;
3198 else
3200 /* index * scale + disp => new base + index * scale,
3201 case (1) above. */
3202 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3203 GET_CODE (*ad.index));
3205 lra_assert (INDEX_REG_CLASS != NO_REGS);
3206 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3207 lra_emit_move (new_reg, *ad.disp);
3208 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3209 new_reg, *ad.index);
3212 else if (ad.index == NULL)
3214 int regno;
3215 enum reg_class cl;
3216 rtx set;
3217 rtx_insn *insns, *last_insn;
3218 /* Try to reload base into register only if the base is invalid
3219 for the address but with valid offset, case (4) above. */
3220 start_sequence ();
3221 new_reg = base_to_reg (&ad);
3223 /* base + disp => new base, cases (1) and (3) above. */
3224 /* Another option would be to reload the displacement into an
3225 index register. However, postreload has code to optimize
3226 address reloads that have the same base and different
3227 displacements, so reloading into an index register would
3228 not necessarily be a win. */
3229 if (new_reg == NULL_RTX)
3230 new_reg = base_plus_disp_to_reg (&ad);
3231 insns = get_insns ();
3232 last_insn = get_last_insn ();
3233 /* If we generated at least two insns, try last insn source as
3234 an address. If we succeed, we generate one less insn. */
3235 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3236 && GET_CODE (SET_SRC (set)) == PLUS
3237 && REG_P (XEXP (SET_SRC (set), 0))
3238 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3240 *ad.inner = SET_SRC (set);
3241 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3243 *ad.base_term = XEXP (SET_SRC (set), 0);
3244 *ad.disp_term = XEXP (SET_SRC (set), 1);
3245 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3246 get_index_code (&ad));
3247 regno = REGNO (*ad.base_term);
3248 if (regno >= FIRST_PSEUDO_REGISTER
3249 && cl != lra_get_allocno_class (regno))
3250 lra_change_class (regno, cl, " Change to", true);
3251 new_reg = SET_SRC (set);
3252 delete_insns_since (PREV_INSN (last_insn));
3255 /* Try if target can split displacement into legitimite new disp
3256 and offset. If it's the case, we replace the last insn with
3257 insns for base + offset => new_reg and set new_reg + new disp
3258 to *ad.inner. */
3259 last_insn = get_last_insn ();
3260 if ((set = single_set (last_insn)) != NULL_RTX
3261 && GET_CODE (SET_SRC (set)) == PLUS
3262 && REG_P (XEXP (SET_SRC (set), 0))
3263 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3264 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3266 rtx addend, disp = XEXP (SET_SRC (set), 1);
3267 if (targetm.legitimize_address_displacement (&disp, &addend,
3268 ad.mode))
3270 rtx_insn *new_insns;
3271 start_sequence ();
3272 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3273 new_insns = get_insns ();
3274 end_sequence ();
3275 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3276 delete_insns_since (PREV_INSN (last_insn));
3277 add_insn (new_insns);
3278 insns = get_insns ();
3281 end_sequence ();
3282 emit_insn (insns);
3283 *ad.inner = new_reg;
3285 else if (ad.disp_term != NULL)
3287 /* base + scale * index + disp => new base + scale * index,
3288 case (1) above. */
3289 new_reg = base_plus_disp_to_reg (&ad);
3290 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3291 new_reg, *ad.index);
3293 else if ((scale = get_index_scale (&ad)) == 1)
3295 /* The last transformation to one reg will be made in
3296 curr_insn_transform function. */
3297 end_sequence ();
3298 return false;
3300 else if (scale != 0)
3302 /* base + scale * index => base + new_reg,
3303 case (1) above.
3304 Index part of address may become invalid. For example, we
3305 changed pseudo on the equivalent memory and a subreg of the
3306 pseudo onto the memory of different mode for which the scale is
3307 prohibitted. */
3308 new_reg = index_part_to_reg (&ad);
3309 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3310 *ad.base_term, new_reg);
3312 else
3314 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3315 SCRATCH, SCRATCH);
3316 rtx addr = *ad.inner;
3318 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3319 /* addr => new_base. */
3320 lra_emit_move (new_reg, addr);
3321 *ad.inner = new_reg;
3323 *before = get_insns ();
3324 end_sequence ();
3325 return true;
3328 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3329 Use process_address_1 as a helper function. Return true for any
3330 RTL changes.
3332 If CHECK_ONLY_P is true, just check address correctness. Return
3333 false if the address correct. */
3334 static bool
3335 process_address (int nop, bool check_only_p,
3336 rtx_insn **before, rtx_insn **after)
3338 bool res = false;
3340 while (process_address_1 (nop, check_only_p, before, after))
3342 if (check_only_p)
3343 return true;
3344 res = true;
3346 return res;
3349 /* Emit insns to reload VALUE into a new register. VALUE is an
3350 auto-increment or auto-decrement RTX whose operand is a register or
3351 memory location; so reloading involves incrementing that location.
3352 IN is either identical to VALUE, or some cheaper place to reload
3353 value being incremented/decremented from.
3355 INC_AMOUNT is the number to increment or decrement by (always
3356 positive and ignored for POST_MODIFY/PRE_MODIFY).
3358 Return pseudo containing the result. */
3359 static rtx
3360 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3362 /* REG or MEM to be copied and incremented. */
3363 rtx incloc = XEXP (value, 0);
3364 /* Nonzero if increment after copying. */
3365 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3366 || GET_CODE (value) == POST_MODIFY);
3367 rtx_insn *last;
3368 rtx inc;
3369 rtx_insn *add_insn;
3370 int code;
3371 rtx real_in = in == value ? incloc : in;
3372 rtx result;
3373 bool plus_p = true;
3375 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3377 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3378 || GET_CODE (XEXP (value, 1)) == MINUS);
3379 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3380 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3381 inc = XEXP (XEXP (value, 1), 1);
3383 else
3385 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3386 inc_amount = -inc_amount;
3388 inc = GEN_INT (inc_amount);
3391 if (! post && REG_P (incloc))
3392 result = incloc;
3393 else
3394 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3395 "INC/DEC result");
3397 if (real_in != result)
3399 /* First copy the location to the result register. */
3400 lra_assert (REG_P (result));
3401 emit_insn (gen_move_insn (result, real_in));
3404 /* We suppose that there are insns to add/sub with the constant
3405 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3406 old reload worked with this assumption. If the assumption
3407 becomes wrong, we should use approach in function
3408 base_plus_disp_to_reg. */
3409 if (in == value)
3411 /* See if we can directly increment INCLOC. */
3412 last = get_last_insn ();
3413 add_insn = emit_insn (plus_p
3414 ? gen_add2_insn (incloc, inc)
3415 : gen_sub2_insn (incloc, inc));
3417 code = recog_memoized (add_insn);
3418 if (code >= 0)
3420 if (! post && result != incloc)
3421 emit_insn (gen_move_insn (result, incloc));
3422 return result;
3424 delete_insns_since (last);
3427 /* If couldn't do the increment directly, must increment in RESULT.
3428 The way we do this depends on whether this is pre- or
3429 post-increment. For pre-increment, copy INCLOC to the reload
3430 register, increment it there, then save back. */
3431 if (! post)
3433 if (real_in != result)
3434 emit_insn (gen_move_insn (result, real_in));
3435 if (plus_p)
3436 emit_insn (gen_add2_insn (result, inc));
3437 else
3438 emit_insn (gen_sub2_insn (result, inc));
3439 if (result != incloc)
3440 emit_insn (gen_move_insn (incloc, result));
3442 else
3444 /* Post-increment.
3446 Because this might be a jump insn or a compare, and because
3447 RESULT may not be available after the insn in an input
3448 reload, we must do the incrementing before the insn being
3449 reloaded for.
3451 We have already copied IN to RESULT. Increment the copy in
3452 RESULT, save that back, then decrement RESULT so it has
3453 the original value. */
3454 if (plus_p)
3455 emit_insn (gen_add2_insn (result, inc));
3456 else
3457 emit_insn (gen_sub2_insn (result, inc));
3458 emit_insn (gen_move_insn (incloc, result));
3459 /* Restore non-modified value for the result. We prefer this
3460 way because it does not require an additional hard
3461 register. */
3462 if (plus_p)
3464 if (CONST_INT_P (inc))
3465 emit_insn (gen_add2_insn (result,
3466 gen_int_mode (-INTVAL (inc),
3467 GET_MODE (result))));
3468 else
3469 emit_insn (gen_sub2_insn (result, inc));
3471 else
3472 emit_insn (gen_add2_insn (result, inc));
3474 return result;
3477 /* Return true if the current move insn does not need processing as we
3478 already know that it satisfies its constraints. */
3479 static bool
3480 simple_move_p (void)
3482 rtx dest, src;
3483 enum reg_class dclass, sclass;
3485 lra_assert (curr_insn_set != NULL_RTX);
3486 dest = SET_DEST (curr_insn_set);
3487 src = SET_SRC (curr_insn_set);
3489 /* If the instruction has multiple sets we need to process it even if it
3490 is single_set. This can happen if one or more of the SETs are dead.
3491 See PR73650. */
3492 if (multiple_sets (curr_insn))
3493 return false;
3495 return ((dclass = get_op_class (dest)) != NO_REGS
3496 && (sclass = get_op_class (src)) != NO_REGS
3497 /* The backend guarantees that register moves of cost 2
3498 never need reloads. */
3499 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3502 /* Swap operands NOP and NOP + 1. */
3503 static inline void
3504 swap_operands (int nop)
3506 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3507 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3508 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3509 std::swap (equiv_substition_p[nop], equiv_substition_p[nop + 1]);
3510 /* Swap the duplicates too. */
3511 lra_update_dup (curr_id, nop);
3512 lra_update_dup (curr_id, nop + 1);
3515 /* Main entry point of the constraint code: search the body of the
3516 current insn to choose the best alternative. It is mimicking insn
3517 alternative cost calculation model of former reload pass. That is
3518 because machine descriptions were written to use this model. This
3519 model can be changed in future. Make commutative operand exchange
3520 if it is chosen.
3522 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3523 constraints. Return true if any change happened during function
3524 call.
3526 If CHECK_ONLY_P is true then don't do any transformation. Just
3527 check that the insn satisfies all constraints. If the insn does
3528 not satisfy any constraint, return true. */
3529 static bool
3530 curr_insn_transform (bool check_only_p)
3532 int i, j, k;
3533 int n_operands;
3534 int n_alternatives;
3535 int n_outputs;
3536 int commutative;
3537 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3538 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3539 signed char outputs[MAX_RECOG_OPERANDS + 1];
3540 rtx_insn *before, *after;
3541 bool alt_p = false;
3542 /* Flag that the insn has been changed through a transformation. */
3543 bool change_p;
3544 bool sec_mem_p;
3545 #ifdef SECONDARY_MEMORY_NEEDED
3546 bool use_sec_mem_p;
3547 #endif
3548 int max_regno_before;
3549 int reused_alternative_num;
3551 curr_insn_set = single_set (curr_insn);
3552 if (curr_insn_set != NULL_RTX && simple_move_p ())
3553 return false;
3555 no_input_reloads_p = no_output_reloads_p = false;
3556 goal_alt_number = -1;
3557 change_p = sec_mem_p = false;
3558 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3559 reloads; neither are insns that SET cc0. Insns that use CC0 are
3560 not allowed to have any input reloads. */
3561 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3562 no_output_reloads_p = true;
3564 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3565 no_input_reloads_p = true;
3566 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3567 no_output_reloads_p = true;
3569 n_operands = curr_static_id->n_operands;
3570 n_alternatives = curr_static_id->n_alternatives;
3572 /* Just return "no reloads" if insn has no operands with
3573 constraints. */
3574 if (n_operands == 0 || n_alternatives == 0)
3575 return false;
3577 max_regno_before = max_reg_num ();
3579 for (i = 0; i < n_operands; i++)
3581 goal_alt_matched[i][0] = -1;
3582 goal_alt_matches[i] = -1;
3585 commutative = curr_static_id->commutative;
3587 /* Now see what we need for pseudos that didn't get hard regs or got
3588 the wrong kind of hard reg. For this, we must consider all the
3589 operands together against the register constraints. */
3591 best_losers = best_overall = INT_MAX;
3592 best_reload_sum = 0;
3594 curr_swapped = false;
3595 goal_alt_swapped = false;
3597 if (! check_only_p)
3598 /* Make equivalence substitution and memory subreg elimination
3599 before address processing because an address legitimacy can
3600 depend on memory mode. */
3601 for (i = 0; i < n_operands; i++)
3603 rtx op, subst, old;
3604 bool op_change_p = false;
3606 if (curr_static_id->operand[i].is_operator)
3607 continue;
3609 old = op = *curr_id->operand_loc[i];
3610 if (GET_CODE (old) == SUBREG)
3611 old = SUBREG_REG (old);
3612 subst = get_equiv_with_elimination (old, curr_insn);
3613 original_subreg_reg_mode[i] = VOIDmode;
3614 equiv_substition_p[i] = false;
3615 if (subst != old)
3617 equiv_substition_p[i] = true;
3618 subst = copy_rtx (subst);
3619 lra_assert (REG_P (old));
3620 if (GET_CODE (op) != SUBREG)
3621 *curr_id->operand_loc[i] = subst;
3622 else
3624 SUBREG_REG (op) = subst;
3625 if (GET_MODE (subst) == VOIDmode)
3626 original_subreg_reg_mode[i] = GET_MODE (old);
3628 if (lra_dump_file != NULL)
3630 fprintf (lra_dump_file,
3631 "Changing pseudo %d in operand %i of insn %u on equiv ",
3632 REGNO (old), i, INSN_UID (curr_insn));
3633 dump_value_slim (lra_dump_file, subst, 1);
3634 fprintf (lra_dump_file, "\n");
3636 op_change_p = change_p = true;
3638 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3640 change_p = true;
3641 lra_update_dup (curr_id, i);
3645 /* Reload address registers and displacements. We do it before
3646 finding an alternative because of memory constraints. */
3647 before = after = NULL;
3648 for (i = 0; i < n_operands; i++)
3649 if (! curr_static_id->operand[i].is_operator
3650 && process_address (i, check_only_p, &before, &after))
3652 if (check_only_p)
3653 return true;
3654 change_p = true;
3655 lra_update_dup (curr_id, i);
3658 if (change_p)
3659 /* If we've changed the instruction then any alternative that
3660 we chose previously may no longer be valid. */
3661 lra_set_used_insn_alternative (curr_insn, -1);
3663 if (! check_only_p && curr_insn_set != NULL_RTX
3664 && check_and_process_move (&change_p, &sec_mem_p))
3665 return change_p;
3667 try_swapped:
3669 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3670 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3671 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3672 reused_alternative_num, INSN_UID (curr_insn));
3674 if (process_alt_operands (reused_alternative_num))
3675 alt_p = true;
3677 if (check_only_p)
3678 return ! alt_p || best_losers != 0;
3680 /* If insn is commutative (it's safe to exchange a certain pair of
3681 operands) then we need to try each alternative twice, the second
3682 time matching those two operands as if we had exchanged them. To
3683 do this, really exchange them in operands.
3685 If we have just tried the alternatives the second time, return
3686 operands to normal and drop through. */
3688 if (reused_alternative_num < 0 && commutative >= 0)
3690 curr_swapped = !curr_swapped;
3691 if (curr_swapped)
3693 swap_operands (commutative);
3694 goto try_swapped;
3696 else
3697 swap_operands (commutative);
3700 if (! alt_p && ! sec_mem_p)
3702 /* No alternative works with reloads?? */
3703 if (INSN_CODE (curr_insn) >= 0)
3704 fatal_insn ("unable to generate reloads for:", curr_insn);
3705 error_for_asm (curr_insn,
3706 "inconsistent operand constraints in an %<asm%>");
3707 /* Avoid further trouble with this insn. */
3708 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3709 lra_invalidate_insn_data (curr_insn);
3710 return true;
3713 /* If the best alternative is with operands 1 and 2 swapped, swap
3714 them. Update the operand numbers of any reloads already
3715 pushed. */
3717 if (goal_alt_swapped)
3719 if (lra_dump_file != NULL)
3720 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3721 INSN_UID (curr_insn));
3723 /* Swap the duplicates too. */
3724 swap_operands (commutative);
3725 change_p = true;
3728 #ifdef SECONDARY_MEMORY_NEEDED
3729 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3730 too conservatively. So we use the secondary memory only if there
3731 is no any alternative without reloads. */
3732 use_sec_mem_p = false;
3733 if (! alt_p)
3734 use_sec_mem_p = true;
3735 else if (sec_mem_p)
3737 for (i = 0; i < n_operands; i++)
3738 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3739 break;
3740 use_sec_mem_p = i < n_operands;
3743 if (use_sec_mem_p)
3745 int in = -1, out = -1;
3746 rtx new_reg, src, dest, rld;
3747 machine_mode sec_mode, rld_mode;
3749 lra_assert (curr_insn_set != NULL_RTX && sec_mem_p);
3750 dest = SET_DEST (curr_insn_set);
3751 src = SET_SRC (curr_insn_set);
3752 for (i = 0; i < n_operands; i++)
3753 if (*curr_id->operand_loc[i] == dest)
3754 out = i;
3755 else if (*curr_id->operand_loc[i] == src)
3756 in = i;
3757 for (i = 0; i < curr_static_id->n_dups; i++)
3758 if (out < 0 && *curr_id->dup_loc[i] == dest)
3759 out = curr_static_id->dup_num[i];
3760 else if (in < 0 && *curr_id->dup_loc[i] == src)
3761 in = curr_static_id->dup_num[i];
3762 lra_assert (out >= 0 && in >= 0
3763 && curr_static_id->operand[out].type == OP_OUT
3764 && curr_static_id->operand[in].type == OP_IN);
3765 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3766 ? dest : src);
3767 rld_mode = GET_MODE (rld);
3768 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3769 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3770 #else
3771 sec_mode = rld_mode;
3772 #endif
3773 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3774 NO_REGS, "secondary");
3775 /* If the mode is changed, it should be wider. */
3776 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3777 if (sec_mode != rld_mode)
3779 /* If the target says specifically to use another mode for
3780 secondary memory moves we can not reuse the original
3781 insn. */
3782 after = emit_spill_move (false, new_reg, dest);
3783 lra_process_new_insns (curr_insn, NULL, after,
3784 "Inserting the sec. move");
3785 /* We may have non null BEFORE here (e.g. after address
3786 processing. */
3787 push_to_sequence (before);
3788 before = emit_spill_move (true, new_reg, src);
3789 emit_insn (before);
3790 before = get_insns ();
3791 end_sequence ();
3792 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3793 lra_set_insn_deleted (curr_insn);
3795 else if (dest == rld)
3797 *curr_id->operand_loc[out] = new_reg;
3798 lra_update_dup (curr_id, out);
3799 after = emit_spill_move (false, new_reg, dest);
3800 lra_process_new_insns (curr_insn, NULL, after,
3801 "Inserting the sec. move");
3803 else
3805 *curr_id->operand_loc[in] = new_reg;
3806 lra_update_dup (curr_id, in);
3807 /* See comments above. */
3808 push_to_sequence (before);
3809 before = emit_spill_move (true, new_reg, src);
3810 emit_insn (before);
3811 before = get_insns ();
3812 end_sequence ();
3813 lra_process_new_insns (curr_insn, before, NULL,
3814 "Inserting the sec. move");
3816 lra_update_insn_regno_info (curr_insn);
3817 return true;
3819 #endif
3821 lra_assert (goal_alt_number >= 0);
3822 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3824 if (lra_dump_file != NULL)
3826 const char *p;
3828 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3829 goal_alt_number, INSN_UID (curr_insn));
3830 for (i = 0; i < n_operands; i++)
3832 p = (curr_static_id->operand_alternative
3833 [goal_alt_number * n_operands + i].constraint);
3834 if (*p == '\0')
3835 continue;
3836 fprintf (lra_dump_file, " (%d) ", i);
3837 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3838 fputc (*p, lra_dump_file);
3840 if (INSN_CODE (curr_insn) >= 0
3841 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3842 fprintf (lra_dump_file, " {%s}", p);
3843 if (curr_id->sp_offset != 0)
3844 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3845 curr_id->sp_offset);
3846 fprintf (lra_dump_file, "\n");
3849 /* Right now, for any pair of operands I and J that are required to
3850 match, with J < I, goal_alt_matches[I] is J. Add I to
3851 goal_alt_matched[J]. */
3853 for (i = 0; i < n_operands; i++)
3854 if ((j = goal_alt_matches[i]) >= 0)
3856 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3858 /* We allow matching one output operand and several input
3859 operands. */
3860 lra_assert (k == 0
3861 || (curr_static_id->operand[j].type == OP_OUT
3862 && curr_static_id->operand[i].type == OP_IN
3863 && (curr_static_id->operand
3864 [goal_alt_matched[j][0]].type == OP_IN)));
3865 goal_alt_matched[j][k] = i;
3866 goal_alt_matched[j][k + 1] = -1;
3869 for (i = 0; i < n_operands; i++)
3870 goal_alt_win[i] |= goal_alt_match_win[i];
3872 /* Any constants that aren't allowed and can't be reloaded into
3873 registers are here changed into memory references. */
3874 for (i = 0; i < n_operands; i++)
3875 if (goal_alt_win[i])
3877 int regno;
3878 enum reg_class new_class;
3879 rtx reg = *curr_id->operand_loc[i];
3881 if (GET_CODE (reg) == SUBREG)
3882 reg = SUBREG_REG (reg);
3884 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3886 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3888 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3890 lra_assert (ok_p);
3891 lra_change_class (regno, new_class, " Change to", true);
3895 else
3897 const char *constraint;
3898 char c;
3899 rtx op = *curr_id->operand_loc[i];
3900 rtx subreg = NULL_RTX;
3901 machine_mode mode = curr_operand_mode[i];
3903 if (GET_CODE (op) == SUBREG)
3905 subreg = op;
3906 op = SUBREG_REG (op);
3907 mode = GET_MODE (op);
3910 if (CONST_POOL_OK_P (mode, op)
3911 && ((targetm.preferred_reload_class
3912 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3913 || no_input_reloads_p))
3915 rtx tem = force_const_mem (mode, op);
3917 change_p = true;
3918 if (subreg != NULL_RTX)
3919 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3921 *curr_id->operand_loc[i] = tem;
3922 lra_update_dup (curr_id, i);
3923 process_address (i, false, &before, &after);
3925 /* If the alternative accepts constant pool refs directly
3926 there will be no reload needed at all. */
3927 if (subreg != NULL_RTX)
3928 continue;
3929 /* Skip alternatives before the one requested. */
3930 constraint = (curr_static_id->operand_alternative
3931 [goal_alt_number * n_operands + i].constraint);
3932 for (;
3933 (c = *constraint) && c != ',' && c != '#';
3934 constraint += CONSTRAINT_LEN (c, constraint))
3936 enum constraint_num cn = lookup_constraint (constraint);
3937 if ((insn_extra_memory_constraint (cn)
3938 || insn_extra_special_memory_constraint (cn))
3939 && satisfies_memory_constraint_p (tem, cn))
3940 break;
3942 if (c == '\0' || c == ',' || c == '#')
3943 continue;
3945 goal_alt_win[i] = true;
3949 n_outputs = 0;
3950 outputs[0] = -1;
3951 for (i = 0; i < n_operands; i++)
3953 int regno;
3954 bool optional_p = false;
3955 rtx old, new_reg;
3956 rtx op = *curr_id->operand_loc[i];
3958 if (goal_alt_win[i])
3960 if (goal_alt[i] == NO_REGS
3961 && REG_P (op)
3962 /* When we assign NO_REGS it means that we will not
3963 assign a hard register to the scratch pseudo by
3964 assigment pass and the scratch pseudo will be
3965 spilled. Spilled scratch pseudos are transformed
3966 back to scratches at the LRA end. */
3967 && lra_former_scratch_operand_p (curr_insn, i)
3968 && lra_former_scratch_p (REGNO (op)))
3970 int regno = REGNO (op);
3971 lra_change_class (regno, NO_REGS, " Change to", true);
3972 if (lra_get_regno_hard_regno (regno) >= 0)
3973 /* We don't have to mark all insn affected by the
3974 spilled pseudo as there is only one such insn, the
3975 current one. */
3976 reg_renumber[regno] = -1;
3977 lra_assert (bitmap_single_bit_set_p
3978 (&lra_reg_info[REGNO (op)].insn_bitmap));
3980 /* We can do an optional reload. If the pseudo got a hard
3981 reg, we might improve the code through inheritance. If
3982 it does not get a hard register we coalesce memory/memory
3983 moves later. Ignore move insns to avoid cycling. */
3984 if (! lra_simple_p
3985 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3986 && goal_alt[i] != NO_REGS && REG_P (op)
3987 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3988 && regno < new_regno_start
3989 && ! lra_former_scratch_p (regno)
3990 && reg_renumber[regno] < 0
3991 /* Check that the optional reload pseudo will be able to
3992 hold given mode value. */
3993 && ! (prohibited_class_reg_set_mode_p
3994 (goal_alt[i], reg_class_contents[goal_alt[i]],
3995 PSEUDO_REGNO_MODE (regno)))
3996 && (curr_insn_set == NULL_RTX
3997 || !((REG_P (SET_SRC (curr_insn_set))
3998 || MEM_P (SET_SRC (curr_insn_set))
3999 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
4000 && (REG_P (SET_DEST (curr_insn_set))
4001 || MEM_P (SET_DEST (curr_insn_set))
4002 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
4003 optional_p = true;
4004 else
4005 continue;
4008 /* Operands that match previous ones have already been handled. */
4009 if (goal_alt_matches[i] >= 0)
4010 continue;
4012 /* We should not have an operand with a non-offsettable address
4013 appearing where an offsettable address will do. It also may
4014 be a case when the address should be special in other words
4015 not a general one (e.g. it needs no index reg). */
4016 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
4018 enum reg_class rclass;
4019 rtx *loc = &XEXP (op, 0);
4020 enum rtx_code code = GET_CODE (*loc);
4022 push_to_sequence (before);
4023 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
4024 MEM, SCRATCH);
4025 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
4026 new_reg = emit_inc (rclass, *loc, *loc,
4027 /* This value does not matter for MODIFY. */
4028 GET_MODE_SIZE (GET_MODE (op)));
4029 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
4030 "offsetable address", &new_reg))
4031 lra_emit_move (new_reg, *loc);
4032 before = get_insns ();
4033 end_sequence ();
4034 *loc = new_reg;
4035 lra_update_dup (curr_id, i);
4037 else if (goal_alt_matched[i][0] == -1)
4039 machine_mode mode;
4040 rtx reg, *loc;
4041 int hard_regno, byte;
4042 enum op_type type = curr_static_id->operand[i].type;
4044 loc = curr_id->operand_loc[i];
4045 mode = curr_operand_mode[i];
4046 if (GET_CODE (*loc) == SUBREG)
4048 reg = SUBREG_REG (*loc);
4049 byte = SUBREG_BYTE (*loc);
4050 if (REG_P (reg)
4051 /* Strict_low_part requires reload the register not
4052 the sub-register. */
4053 && (curr_static_id->operand[i].strict_low
4054 || (GET_MODE_SIZE (mode)
4055 <= GET_MODE_SIZE (GET_MODE (reg))
4056 && (hard_regno
4057 = get_try_hard_regno (REGNO (reg))) >= 0
4058 && (simplify_subreg_regno
4059 (hard_regno,
4060 GET_MODE (reg), byte, mode) < 0)
4061 && (goal_alt[i] == NO_REGS
4062 || (simplify_subreg_regno
4063 (ira_class_hard_regs[goal_alt[i]][0],
4064 GET_MODE (reg), byte, mode) >= 0)))))
4066 if (type == OP_OUT)
4067 type = OP_INOUT;
4068 loc = &SUBREG_REG (*loc);
4069 mode = GET_MODE (*loc);
4072 old = *loc;
4073 if (get_reload_reg (type, mode, old, goal_alt[i],
4074 loc != curr_id->operand_loc[i], "", &new_reg)
4075 && type != OP_OUT)
4077 push_to_sequence (before);
4078 lra_emit_move (new_reg, old);
4079 before = get_insns ();
4080 end_sequence ();
4082 *loc = new_reg;
4083 if (type != OP_IN
4084 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
4086 start_sequence ();
4087 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
4088 emit_insn (after);
4089 after = get_insns ();
4090 end_sequence ();
4091 *loc = new_reg;
4093 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
4094 if (goal_alt_dont_inherit_ops[j] == i)
4096 lra_set_regno_unique_value (REGNO (new_reg));
4097 break;
4099 lra_update_dup (curr_id, i);
4101 else if (curr_static_id->operand[i].type == OP_IN
4102 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4103 == OP_OUT))
4105 /* generate reloads for input and matched outputs. */
4106 match_inputs[0] = i;
4107 match_inputs[1] = -1;
4108 match_reload (goal_alt_matched[i][0], match_inputs, outputs,
4109 goal_alt[i], &before, &after,
4110 curr_static_id->operand_alternative
4111 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
4112 .earlyclobber);
4114 else if (curr_static_id->operand[i].type == OP_OUT
4115 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4116 == OP_IN))
4117 /* Generate reloads for output and matched inputs. */
4118 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i], &before,
4119 &after, curr_static_id->operand_alternative
4120 [goal_alt_number * n_operands + i].earlyclobber);
4121 else if (curr_static_id->operand[i].type == OP_IN
4122 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4123 == OP_IN))
4125 /* Generate reloads for matched inputs. */
4126 match_inputs[0] = i;
4127 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
4128 match_inputs[j + 1] = k;
4129 match_inputs[j + 1] = -1;
4130 match_reload (-1, match_inputs, outputs, goal_alt[i], &before,
4131 &after, false);
4133 else
4134 /* We must generate code in any case when function
4135 process_alt_operands decides that it is possible. */
4136 gcc_unreachable ();
4138 /* Memorise processed outputs so that output remaining to be processed
4139 can avoid using the same register value (see match_reload). */
4140 if (curr_static_id->operand[i].type == OP_OUT)
4142 outputs[n_outputs++] = i;
4143 outputs[n_outputs] = -1;
4146 if (optional_p)
4148 rtx reg = op;
4150 lra_assert (REG_P (reg));
4151 regno = REGNO (reg);
4152 op = *curr_id->operand_loc[i]; /* Substitution. */
4153 if (GET_CODE (op) == SUBREG)
4154 op = SUBREG_REG (op);
4155 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
4156 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
4157 lra_reg_info[REGNO (op)].restore_rtx = reg;
4158 if (lra_dump_file != NULL)
4159 fprintf (lra_dump_file,
4160 " Making reload reg %d for reg %d optional\n",
4161 REGNO (op), regno);
4164 if (before != NULL_RTX || after != NULL_RTX
4165 || max_regno_before != max_reg_num ())
4166 change_p = true;
4167 if (change_p)
4169 lra_update_operator_dups (curr_id);
4170 /* Something changes -- process the insn. */
4171 lra_update_insn_regno_info (curr_insn);
4173 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
4174 return change_p;
4177 /* Return true if INSN satisfies all constraints. In other words, no
4178 reload insns are needed. */
4179 bool
4180 lra_constrain_insn (rtx_insn *insn)
4182 int saved_new_regno_start = new_regno_start;
4183 int saved_new_insn_uid_start = new_insn_uid_start;
4184 bool change_p;
4186 curr_insn = insn;
4187 curr_id = lra_get_insn_recog_data (curr_insn);
4188 curr_static_id = curr_id->insn_static_data;
4189 new_insn_uid_start = get_max_uid ();
4190 new_regno_start = max_reg_num ();
4191 change_p = curr_insn_transform (true);
4192 new_regno_start = saved_new_regno_start;
4193 new_insn_uid_start = saved_new_insn_uid_start;
4194 return ! change_p;
4197 /* Return true if X is in LIST. */
4198 static bool
4199 in_list_p (rtx x, rtx list)
4201 for (; list != NULL_RTX; list = XEXP (list, 1))
4202 if (XEXP (list, 0) == x)
4203 return true;
4204 return false;
4207 /* Return true if X contains an allocatable hard register (if
4208 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
4209 static bool
4210 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
4212 int i, j;
4213 const char *fmt;
4214 enum rtx_code code;
4216 code = GET_CODE (x);
4217 if (REG_P (x))
4219 int regno = REGNO (x);
4220 HARD_REG_SET alloc_regs;
4222 if (hard_reg_p)
4224 if (regno >= FIRST_PSEUDO_REGISTER)
4225 regno = lra_get_regno_hard_regno (regno);
4226 if (regno < 0)
4227 return false;
4228 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
4229 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
4231 else
4233 if (regno < FIRST_PSEUDO_REGISTER)
4234 return false;
4235 if (! spilled_p)
4236 return true;
4237 return lra_get_regno_hard_regno (regno) < 0;
4240 fmt = GET_RTX_FORMAT (code);
4241 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4243 if (fmt[i] == 'e')
4245 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4246 return true;
4248 else if (fmt[i] == 'E')
4250 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4251 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4252 return true;
4255 return false;
4258 /* Process all regs in location *LOC and change them on equivalent
4259 substitution. Return true if any change was done. */
4260 static bool
4261 loc_equivalence_change_p (rtx *loc)
4263 rtx subst, reg, x = *loc;
4264 bool result = false;
4265 enum rtx_code code = GET_CODE (x);
4266 const char *fmt;
4267 int i, j;
4269 if (code == SUBREG)
4271 reg = SUBREG_REG (x);
4272 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4273 && GET_MODE (subst) == VOIDmode)
4275 /* We cannot reload debug location. Simplify subreg here
4276 while we know the inner mode. */
4277 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4278 GET_MODE (reg), SUBREG_BYTE (x));
4279 return true;
4282 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4284 *loc = subst;
4285 return true;
4288 /* Scan all the operand sub-expressions. */
4289 fmt = GET_RTX_FORMAT (code);
4290 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4292 if (fmt[i] == 'e')
4293 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4294 else if (fmt[i] == 'E')
4295 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4296 result
4297 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4299 return result;
4302 /* Similar to loc_equivalence_change_p, but for use as
4303 simplify_replace_fn_rtx callback. DATA is insn for which the
4304 elimination is done. If it null we don't do the elimination. */
4305 static rtx
4306 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4308 if (!REG_P (loc))
4309 return NULL_RTX;
4311 rtx subst = (data == NULL
4312 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4313 if (subst != loc)
4314 return subst;
4316 return NULL_RTX;
4319 /* Maximum number of generated reload insns per an insn. It is for
4320 preventing this pass cycling in a bug case. */
4321 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4323 /* The current iteration number of this LRA pass. */
4324 int lra_constraint_iter;
4326 /* True if we substituted equiv which needs checking register
4327 allocation correctness because the equivalent value contains
4328 allocatable hard registers or when we restore multi-register
4329 pseudo. */
4330 bool lra_risky_transformations_p;
4332 /* Return true if REGNO is referenced in more than one block. */
4333 static bool
4334 multi_block_pseudo_p (int regno)
4336 basic_block bb = NULL;
4337 unsigned int uid;
4338 bitmap_iterator bi;
4340 if (regno < FIRST_PSEUDO_REGISTER)
4341 return false;
4343 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4344 if (bb == NULL)
4345 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4346 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4347 return true;
4348 return false;
4351 /* Return true if LIST contains a deleted insn. */
4352 static bool
4353 contains_deleted_insn_p (rtx_insn_list *list)
4355 for (; list != NULL_RTX; list = list->next ())
4356 if (NOTE_P (list->insn ())
4357 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4358 return true;
4359 return false;
4362 /* Return true if X contains a pseudo dying in INSN. */
4363 static bool
4364 dead_pseudo_p (rtx x, rtx_insn *insn)
4366 int i, j;
4367 const char *fmt;
4368 enum rtx_code code;
4370 if (REG_P (x))
4371 return (insn != NULL_RTX
4372 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4373 code = GET_CODE (x);
4374 fmt = GET_RTX_FORMAT (code);
4375 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4377 if (fmt[i] == 'e')
4379 if (dead_pseudo_p (XEXP (x, i), insn))
4380 return true;
4382 else if (fmt[i] == 'E')
4384 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4385 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4386 return true;
4389 return false;
4392 /* Return true if INSN contains a dying pseudo in INSN right hand
4393 side. */
4394 static bool
4395 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4397 rtx set = single_set (insn);
4399 gcc_assert (set != NULL);
4400 return dead_pseudo_p (SET_SRC (set), insn);
4403 /* Return true if any init insn of REGNO contains a dying pseudo in
4404 insn right hand side. */
4405 static bool
4406 init_insn_rhs_dead_pseudo_p (int regno)
4408 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4410 if (insns == NULL)
4411 return false;
4412 for (; insns != NULL_RTX; insns = insns->next ())
4413 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4414 return true;
4415 return false;
4418 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4419 reverse only if we have one init insn with given REGNO as a
4420 source. */
4421 static bool
4422 reverse_equiv_p (int regno)
4424 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4425 rtx set;
4427 if (insns == NULL)
4428 return false;
4429 if (! INSN_P (insns->insn ())
4430 || insns->next () != NULL)
4431 return false;
4432 if ((set = single_set (insns->insn ())) == NULL_RTX)
4433 return false;
4434 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4437 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4438 call this function only for non-reverse equivalence. */
4439 static bool
4440 contains_reloaded_insn_p (int regno)
4442 rtx set;
4443 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4445 for (; list != NULL; list = list->next ())
4446 if ((set = single_set (list->insn ())) == NULL_RTX
4447 || ! REG_P (SET_DEST (set))
4448 || (int) REGNO (SET_DEST (set)) != regno)
4449 return true;
4450 return false;
4453 /* Entry function of LRA constraint pass. Return true if the
4454 constraint pass did change the code. */
4455 bool
4456 lra_constraints (bool first_p)
4458 bool changed_p;
4459 int i, hard_regno, new_insns_num;
4460 unsigned int min_len, new_min_len, uid;
4461 rtx set, x, reg, dest_reg;
4462 basic_block last_bb;
4463 bitmap_head equiv_insn_bitmap;
4464 bitmap_iterator bi;
4466 lra_constraint_iter++;
4467 if (lra_dump_file != NULL)
4468 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4469 lra_constraint_iter);
4470 changed_p = false;
4471 if (pic_offset_table_rtx
4472 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4473 lra_risky_transformations_p = true;
4474 else
4475 lra_risky_transformations_p = false;
4476 new_insn_uid_start = get_max_uid ();
4477 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4478 /* Mark used hard regs for target stack size calulations. */
4479 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4480 if (lra_reg_info[i].nrefs != 0
4481 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4483 int j, nregs;
4485 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4486 for (j = 0; j < nregs; j++)
4487 df_set_regs_ever_live (hard_regno + j, true);
4489 /* Do elimination before the equivalence processing as we can spill
4490 some pseudos during elimination. */
4491 lra_eliminate (false, first_p);
4492 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4493 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4494 if (lra_reg_info[i].nrefs != 0)
4496 ira_reg_equiv[i].profitable_p = true;
4497 reg = regno_reg_rtx[i];
4498 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4500 bool pseudo_p = contains_reg_p (x, false, false);
4502 /* After RTL transformation, we can not guarantee that
4503 pseudo in the substitution was not reloaded which might
4504 make equivalence invalid. For example, in reverse
4505 equiv of p0
4507 p0 <- ...
4509 equiv_mem <- p0
4511 the memory address register was reloaded before the 2nd
4512 insn. */
4513 if ((! first_p && pseudo_p)
4514 /* We don't use DF for compilation speed sake. So it
4515 is problematic to update live info when we use an
4516 equivalence containing pseudos in more than one
4517 BB. */
4518 || (pseudo_p && multi_block_pseudo_p (i))
4519 /* If an init insn was deleted for some reason, cancel
4520 the equiv. We could update the equiv insns after
4521 transformations including an equiv insn deletion
4522 but it is not worthy as such cases are extremely
4523 rare. */
4524 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4525 /* If it is not a reverse equivalence, we check that a
4526 pseudo in rhs of the init insn is not dying in the
4527 insn. Otherwise, the live info at the beginning of
4528 the corresponding BB might be wrong after we
4529 removed the insn. When the equiv can be a
4530 constant, the right hand side of the init insn can
4531 be a pseudo. */
4532 || (! reverse_equiv_p (i)
4533 && (init_insn_rhs_dead_pseudo_p (i)
4534 /* If we reloaded the pseudo in an equivalence
4535 init insn, we can not remove the equiv init
4536 insns and the init insns might write into
4537 const memory in this case. */
4538 || contains_reloaded_insn_p (i)))
4539 /* Prevent access beyond equivalent memory for
4540 paradoxical subregs. */
4541 || (MEM_P (x)
4542 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4543 > GET_MODE_SIZE (GET_MODE (x))))
4544 || (pic_offset_table_rtx
4545 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4546 && (targetm.preferred_reload_class
4547 (x, lra_get_allocno_class (i)) == NO_REGS))
4548 || contains_symbol_ref_p (x))))
4549 ira_reg_equiv[i].defined_p = false;
4550 if (contains_reg_p (x, false, true))
4551 ira_reg_equiv[i].profitable_p = false;
4552 if (get_equiv (reg) != reg)
4553 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4556 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4557 update_equiv (i);
4558 /* We should add all insns containing pseudos which should be
4559 substituted by their equivalences. */
4560 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4561 lra_push_insn_by_uid (uid);
4562 min_len = lra_insn_stack_length ();
4563 new_insns_num = 0;
4564 last_bb = NULL;
4565 changed_p = false;
4566 while ((new_min_len = lra_insn_stack_length ()) != 0)
4568 curr_insn = lra_pop_insn ();
4569 --new_min_len;
4570 curr_bb = BLOCK_FOR_INSN (curr_insn);
4571 if (curr_bb != last_bb)
4573 last_bb = curr_bb;
4574 bb_reload_num = lra_curr_reload_num;
4576 if (min_len > new_min_len)
4578 min_len = new_min_len;
4579 new_insns_num = 0;
4581 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4582 internal_error
4583 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4584 MAX_RELOAD_INSNS_NUMBER);
4585 new_insns_num++;
4586 if (DEBUG_INSN_P (curr_insn))
4588 /* We need to check equivalence in debug insn and change
4589 pseudo to the equivalent value if necessary. */
4590 curr_id = lra_get_insn_recog_data (curr_insn);
4591 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4593 rtx old = *curr_id->operand_loc[0];
4594 *curr_id->operand_loc[0]
4595 = simplify_replace_fn_rtx (old, NULL_RTX,
4596 loc_equivalence_callback, curr_insn);
4597 if (old != *curr_id->operand_loc[0])
4599 lra_update_insn_regno_info (curr_insn);
4600 changed_p = true;
4604 else if (INSN_P (curr_insn))
4606 if ((set = single_set (curr_insn)) != NULL_RTX)
4608 dest_reg = SET_DEST (set);
4609 /* The equivalence pseudo could be set up as SUBREG in a
4610 case when it is a call restore insn in a mode
4611 different from the pseudo mode. */
4612 if (GET_CODE (dest_reg) == SUBREG)
4613 dest_reg = SUBREG_REG (dest_reg);
4614 if ((REG_P (dest_reg)
4615 && (x = get_equiv (dest_reg)) != dest_reg
4616 /* Remove insns which set up a pseudo whose value
4617 can not be changed. Such insns might be not in
4618 init_insns because we don't update equiv data
4619 during insn transformations.
4621 As an example, let suppose that a pseudo got
4622 hard register and on the 1st pass was not
4623 changed to equivalent constant. We generate an
4624 additional insn setting up the pseudo because of
4625 secondary memory movement. Then the pseudo is
4626 spilled and we use the equiv constant. In this
4627 case we should remove the additional insn and
4628 this insn is not init_insns list. */
4629 && (! MEM_P (x) || MEM_READONLY_P (x)
4630 /* Check that this is actually an insn setting
4631 up the equivalence. */
4632 || in_list_p (curr_insn,
4633 ira_reg_equiv
4634 [REGNO (dest_reg)].init_insns)))
4635 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4636 && in_list_p (curr_insn,
4637 ira_reg_equiv
4638 [REGNO (SET_SRC (set))].init_insns)))
4640 /* This is equiv init insn of pseudo which did not get a
4641 hard register -- remove the insn. */
4642 if (lra_dump_file != NULL)
4644 fprintf (lra_dump_file,
4645 " Removing equiv init insn %i (freq=%d)\n",
4646 INSN_UID (curr_insn),
4647 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4648 dump_insn_slim (lra_dump_file, curr_insn);
4650 if (contains_reg_p (x, true, false))
4651 lra_risky_transformations_p = true;
4652 lra_set_insn_deleted (curr_insn);
4653 continue;
4656 curr_id = lra_get_insn_recog_data (curr_insn);
4657 curr_static_id = curr_id->insn_static_data;
4658 init_curr_insn_input_reloads ();
4659 init_curr_operand_mode ();
4660 if (curr_insn_transform (false))
4661 changed_p = true;
4662 /* Check non-transformed insns too for equiv change as USE
4663 or CLOBBER don't need reloads but can contain pseudos
4664 being changed on their equivalences. */
4665 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4666 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4668 lra_update_insn_regno_info (curr_insn);
4669 changed_p = true;
4673 bitmap_clear (&equiv_insn_bitmap);
4674 /* If we used a new hard regno, changed_p should be true because the
4675 hard reg is assigned to a new pseudo. */
4676 if (flag_checking && !changed_p)
4678 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4679 if (lra_reg_info[i].nrefs != 0
4680 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4682 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4684 for (j = 0; j < nregs; j++)
4685 lra_assert (df_regs_ever_live_p (hard_regno + j));
4688 return changed_p;
4691 static void initiate_invariants (void);
4692 static void finish_invariants (void);
4694 /* Initiate the LRA constraint pass. It is done once per
4695 function. */
4696 void
4697 lra_constraints_init (void)
4699 initiate_invariants ();
4702 /* Finalize the LRA constraint pass. It is done once per
4703 function. */
4704 void
4705 lra_constraints_finish (void)
4707 finish_invariants ();
4712 /* Structure describes invariants for ineheritance. */
4713 struct invariant
4715 /* The order number of the invariant. */
4716 int num;
4717 /* The invariant RTX. */
4718 rtx invariant_rtx;
4719 /* The origin insn of the invariant. */
4720 rtx_insn *insn;
4723 typedef struct invariant invariant_t;
4724 typedef invariant_t *invariant_ptr_t;
4725 typedef const invariant_t *const_invariant_ptr_t;
4727 /* Pointer to the inheritance invariants. */
4728 static vec<invariant_ptr_t> invariants;
4730 /* Allocation pool for the invariants. */
4731 static object_allocator<struct invariant> *invariants_pool;
4733 /* Hash table for the invariants. */
4734 static htab_t invariant_table;
4736 /* Hash function for INVARIANT. */
4737 static hashval_t
4738 invariant_hash (const void *invariant)
4740 rtx inv = ((const_invariant_ptr_t) invariant)->invariant_rtx;
4741 return lra_rtx_hash (inv);
4744 /* Equal function for invariants INVARIANT1 and INVARIANT2. */
4745 static int
4746 invariant_eq_p (const void *invariant1, const void *invariant2)
4748 rtx inv1 = ((const_invariant_ptr_t) invariant1)->invariant_rtx;
4749 rtx inv2 = ((const_invariant_ptr_t) invariant2)->invariant_rtx;
4751 return rtx_equal_p (inv1, inv2);
4754 /* Insert INVARIANT_RTX into the table if it is not there yet. Return
4755 invariant which is in the table. */
4756 static invariant_ptr_t
4757 insert_invariant (rtx invariant_rtx)
4759 void **entry_ptr;
4760 invariant_t invariant;
4761 invariant_ptr_t invariant_ptr;
4763 invariant.invariant_rtx = invariant_rtx;
4764 entry_ptr = htab_find_slot (invariant_table, &invariant, INSERT);
4765 if (*entry_ptr == NULL)
4767 invariant_ptr = invariants_pool->allocate ();
4768 invariant_ptr->invariant_rtx = invariant_rtx;
4769 invariant_ptr->insn = NULL;
4770 invariants.safe_push (invariant_ptr);
4771 *entry_ptr = (void *) invariant_ptr;
4773 return (invariant_ptr_t) *entry_ptr;
4776 /* Initiate the invariant table. */
4777 static void
4778 initiate_invariants (void)
4780 invariants.create (100);
4781 invariants_pool = new object_allocator<struct invariant> ("Inheritance invariants");
4782 invariant_table = htab_create (100, invariant_hash, invariant_eq_p, NULL);
4785 /* Finish the invariant table. */
4786 static void
4787 finish_invariants (void)
4789 htab_delete (invariant_table);
4790 delete invariants_pool;
4791 invariants.release ();
4794 /* Make the invariant table empty. */
4795 static void
4796 clear_invariants (void)
4798 htab_empty (invariant_table);
4799 invariants_pool->release ();
4800 invariants.truncate (0);
4805 /* This page contains code to do inheritance/split
4806 transformations. */
4808 /* Number of reloads passed so far in current EBB. */
4809 static int reloads_num;
4811 /* Number of calls passed so far in current EBB. */
4812 static int calls_num;
4814 /* Current reload pseudo check for validity of elements in
4815 USAGE_INSNS. */
4816 static int curr_usage_insns_check;
4818 /* Info about last usage of registers in EBB to do inheritance/split
4819 transformation. Inheritance transformation is done from a spilled
4820 pseudo and split transformations from a hard register or a pseudo
4821 assigned to a hard register. */
4822 struct usage_insns
4824 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4825 value INSNS is valid. The insns is chain of optional debug insns
4826 and a finishing non-debug insn using the corresponding reg. The
4827 value is also used to mark the registers which are set up in the
4828 current insn. The negated insn uid is used for this. */
4829 int check;
4830 /* Value of global reloads_num at the last insn in INSNS. */
4831 int reloads_num;
4832 /* Value of global reloads_nums at the last insn in INSNS. */
4833 int calls_num;
4834 /* It can be true only for splitting. And it means that the restore
4835 insn should be put after insn given by the following member. */
4836 bool after_p;
4837 /* Next insns in the current EBB which use the original reg and the
4838 original reg value is not changed between the current insn and
4839 the next insns. In order words, e.g. for inheritance, if we need
4840 to use the original reg value again in the next insns we can try
4841 to use the value in a hard register from a reload insn of the
4842 current insn. */
4843 rtx insns;
4846 /* Map: regno -> corresponding pseudo usage insns. */
4847 static struct usage_insns *usage_insns;
4849 static void
4850 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4852 usage_insns[regno].check = curr_usage_insns_check;
4853 usage_insns[regno].insns = insn;
4854 usage_insns[regno].reloads_num = reloads_num;
4855 usage_insns[regno].calls_num = calls_num;
4856 usage_insns[regno].after_p = after_p;
4859 /* The function is used to form list REGNO usages which consists of
4860 optional debug insns finished by a non-debug insn using REGNO.
4861 RELOADS_NUM is current number of reload insns processed so far. */
4862 static void
4863 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
4865 rtx next_usage_insns;
4867 if (usage_insns[regno].check == curr_usage_insns_check
4868 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4869 && DEBUG_INSN_P (insn))
4871 /* Check that we did not add the debug insn yet. */
4872 if (next_usage_insns != insn
4873 && (GET_CODE (next_usage_insns) != INSN_LIST
4874 || XEXP (next_usage_insns, 0) != insn))
4875 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4876 next_usage_insns);
4878 else if (NONDEBUG_INSN_P (insn))
4879 setup_next_usage_insn (regno, insn, reloads_num, false);
4880 else
4881 usage_insns[regno].check = 0;
4884 /* Return first non-debug insn in list USAGE_INSNS. */
4885 static rtx_insn *
4886 skip_usage_debug_insns (rtx usage_insns)
4888 rtx insn;
4890 /* Skip debug insns. */
4891 for (insn = usage_insns;
4892 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4893 insn = XEXP (insn, 1))
4895 return safe_as_a <rtx_insn *> (insn);
4898 /* Return true if we need secondary memory moves for insn in
4899 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4900 into the insn. */
4901 static bool
4902 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4903 rtx usage_insns ATTRIBUTE_UNUSED)
4905 #ifndef SECONDARY_MEMORY_NEEDED
4906 return false;
4907 #else
4908 rtx_insn *insn;
4909 rtx set, dest;
4910 enum reg_class cl;
4912 if (inher_cl == ALL_REGS
4913 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4914 return false;
4915 lra_assert (INSN_P (insn));
4916 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4917 return false;
4918 dest = SET_DEST (set);
4919 if (! REG_P (dest))
4920 return false;
4921 lra_assert (inher_cl != NO_REGS);
4922 cl = get_reg_class (REGNO (dest));
4923 return (cl != NO_REGS && cl != ALL_REGS
4924 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4925 #endif
4928 /* Registers involved in inheritance/split in the current EBB
4929 (inheritance/split pseudos and original registers). */
4930 static bitmap_head check_only_regs;
4932 /* Reload pseudos can not be involded in invariant inheritance in the
4933 current EBB. */
4934 static bitmap_head invalid_invariant_regs;
4936 /* Do inheritance transformations for insn INSN, which defines (if
4937 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4938 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4939 form as the "insns" field of usage_insns. Return true if we
4940 succeed in such transformation.
4942 The transformations look like:
4944 p <- ... i <- ...
4945 ... p <- i (new insn)
4946 ... =>
4947 <- ... p ... <- ... i ...
4949 ... i <- p (new insn)
4950 <- ... p ... <- ... i ...
4951 ... =>
4952 <- ... p ... <- ... i ...
4953 where p is a spilled original pseudo and i is a new inheritance pseudo.
4956 The inheritance pseudo has the smallest class of two classes CL and
4957 class of ORIGINAL REGNO. */
4958 static bool
4959 inherit_reload_reg (bool def_p, int original_regno,
4960 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4962 if (optimize_function_for_size_p (cfun))
4963 return false;
4965 enum reg_class rclass = lra_get_allocno_class (original_regno);
4966 rtx original_reg = regno_reg_rtx[original_regno];
4967 rtx new_reg, usage_insn;
4968 rtx_insn *new_insns;
4970 lra_assert (! usage_insns[original_regno].after_p);
4971 if (lra_dump_file != NULL)
4972 fprintf (lra_dump_file,
4973 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4974 if (! ira_reg_classes_intersect_p[cl][rclass])
4976 if (lra_dump_file != NULL)
4978 fprintf (lra_dump_file,
4979 " Rejecting inheritance for %d "
4980 "because of disjoint classes %s and %s\n",
4981 original_regno, reg_class_names[cl],
4982 reg_class_names[rclass]);
4983 fprintf (lra_dump_file,
4984 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4986 return false;
4988 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4989 /* We don't use a subset of two classes because it can be
4990 NO_REGS. This transformation is still profitable in most
4991 cases even if the classes are not intersected as register
4992 move is probably cheaper than a memory load. */
4993 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4995 if (lra_dump_file != NULL)
4996 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4997 reg_class_names[cl], reg_class_names[rclass]);
4999 rclass = cl;
5001 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
5003 /* Reject inheritance resulting in secondary memory moves.
5004 Otherwise, there is a danger in LRA cycling. Also such
5005 transformation will be unprofitable. */
5006 if (lra_dump_file != NULL)
5008 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
5009 rtx set = single_set (insn);
5011 lra_assert (set != NULL_RTX);
5013 rtx dest = SET_DEST (set);
5015 lra_assert (REG_P (dest));
5016 fprintf (lra_dump_file,
5017 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
5018 "as secondary mem is needed\n",
5019 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
5020 original_regno, reg_class_names[rclass]);
5021 fprintf (lra_dump_file,
5022 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5024 return false;
5026 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
5027 rclass, "inheritance");
5028 start_sequence ();
5029 if (def_p)
5030 lra_emit_move (original_reg, new_reg);
5031 else
5032 lra_emit_move (new_reg, original_reg);
5033 new_insns = get_insns ();
5034 end_sequence ();
5035 if (NEXT_INSN (new_insns) != NULL_RTX)
5037 if (lra_dump_file != NULL)
5039 fprintf (lra_dump_file,
5040 " Rejecting inheritance %d->%d "
5041 "as it results in 2 or more insns:\n",
5042 original_regno, REGNO (new_reg));
5043 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
5044 fprintf (lra_dump_file,
5045 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5047 return false;
5049 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
5050 lra_update_insn_regno_info (insn);
5051 if (! def_p)
5052 /* We now have a new usage insn for original regno. */
5053 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
5054 if (lra_dump_file != NULL)
5055 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
5056 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
5057 lra_reg_info[REGNO (new_reg)].restore_rtx = regno_reg_rtx[original_regno];
5058 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5059 bitmap_set_bit (&check_only_regs, original_regno);
5060 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
5061 if (def_p)
5062 lra_process_new_insns (insn, NULL, new_insns,
5063 "Add original<-inheritance");
5064 else
5065 lra_process_new_insns (insn, new_insns, NULL,
5066 "Add inheritance<-original");
5067 while (next_usage_insns != NULL_RTX)
5069 if (GET_CODE (next_usage_insns) != INSN_LIST)
5071 usage_insn = next_usage_insns;
5072 lra_assert (NONDEBUG_INSN_P (usage_insn));
5073 next_usage_insns = NULL;
5075 else
5077 usage_insn = XEXP (next_usage_insns, 0);
5078 lra_assert (DEBUG_INSN_P (usage_insn));
5079 next_usage_insns = XEXP (next_usage_insns, 1);
5081 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5082 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5083 if (lra_dump_file != NULL)
5085 fprintf (lra_dump_file,
5086 " Inheritance reuse change %d->%d (bb%d):\n",
5087 original_regno, REGNO (new_reg),
5088 BLOCK_FOR_INSN (usage_insn)->index);
5089 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5092 if (lra_dump_file != NULL)
5093 fprintf (lra_dump_file,
5094 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5095 return true;
5098 /* Return true if we need a caller save/restore for pseudo REGNO which
5099 was assigned to a hard register. */
5100 static inline bool
5101 need_for_call_save_p (int regno)
5103 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
5104 return (usage_insns[regno].calls_num < calls_num
5105 && (overlaps_hard_reg_set_p
5106 ((flag_ipa_ra &&
5107 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
5108 ? lra_reg_info[regno].actual_call_used_reg_set
5109 : call_used_reg_set,
5110 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
5111 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
5112 PSEUDO_REGNO_MODE (regno))));
5115 /* Global registers occurring in the current EBB. */
5116 static bitmap_head ebb_global_regs;
5118 /* Return true if we need a split for hard register REGNO or pseudo
5119 REGNO which was assigned to a hard register.
5120 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
5121 used for reloads since the EBB end. It is an approximation of the
5122 used hard registers in the split range. The exact value would
5123 require expensive calculations. If we were aggressive with
5124 splitting because of the approximation, the split pseudo will save
5125 the same hard register assignment and will be removed in the undo
5126 pass. We still need the approximation because too aggressive
5127 splitting would result in too inaccurate cost calculation in the
5128 assignment pass because of too many generated moves which will be
5129 probably removed in the undo pass. */
5130 static inline bool
5131 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
5133 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
5135 lra_assert (hard_regno >= 0);
5136 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
5137 /* Don't split eliminable hard registers, otherwise we can
5138 split hard registers like hard frame pointer, which
5139 lives on BB start/end according to DF-infrastructure,
5140 when there is a pseudo assigned to the register and
5141 living in the same BB. */
5142 && (regno >= FIRST_PSEUDO_REGISTER
5143 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
5144 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
5145 /* Don't split call clobbered hard regs living through
5146 calls, otherwise we might have a check problem in the
5147 assign sub-pass as in the most cases (exception is a
5148 situation when lra_risky_transformations_p value is
5149 true) the assign pass assumes that all pseudos living
5150 through calls are assigned to call saved hard regs. */
5151 && (regno >= FIRST_PSEUDO_REGISTER
5152 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
5153 || usage_insns[regno].calls_num == calls_num)
5154 /* We need at least 2 reloads to make pseudo splitting
5155 profitable. We should provide hard regno splitting in
5156 any case to solve 1st insn scheduling problem when
5157 moving hard register definition up might result in
5158 impossibility to find hard register for reload pseudo of
5159 small register class. */
5160 && (usage_insns[regno].reloads_num
5161 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
5162 && (regno < FIRST_PSEUDO_REGISTER
5163 /* For short living pseudos, spilling + inheritance can
5164 be considered a substitution for splitting.
5165 Therefore we do not splitting for local pseudos. It
5166 decreases also aggressiveness of splitting. The
5167 minimal number of references is chosen taking into
5168 account that for 2 references splitting has no sense
5169 as we can just spill the pseudo. */
5170 || (regno >= FIRST_PSEUDO_REGISTER
5171 && lra_reg_info[regno].nrefs > 3
5172 && bitmap_bit_p (&ebb_global_regs, regno))))
5173 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
5176 /* Return class for the split pseudo created from original pseudo with
5177 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
5178 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
5179 results in no secondary memory movements. */
5180 static enum reg_class
5181 choose_split_class (enum reg_class allocno_class,
5182 int hard_regno ATTRIBUTE_UNUSED,
5183 machine_mode mode ATTRIBUTE_UNUSED)
5185 #ifndef SECONDARY_MEMORY_NEEDED
5186 return allocno_class;
5187 #else
5188 int i;
5189 enum reg_class cl, best_cl = NO_REGS;
5190 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
5191 = REGNO_REG_CLASS (hard_regno);
5193 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
5194 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
5195 return allocno_class;
5196 for (i = 0;
5197 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
5198 i++)
5199 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
5200 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
5201 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
5202 && (best_cl == NO_REGS
5203 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
5204 best_cl = cl;
5205 return best_cl;
5206 #endif
5209 /* Do split transformations for insn INSN, which defines or uses
5210 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
5211 the EBB next uses ORIGINAL_REGNO; it has the same form as the
5212 "insns" field of usage_insns.
5214 The transformations look like:
5216 p <- ... p <- ...
5217 ... s <- p (new insn -- save)
5218 ... =>
5219 ... p <- s (new insn -- restore)
5220 <- ... p ... <- ... p ...
5222 <- ... p ... <- ... p ...
5223 ... s <- p (new insn -- save)
5224 ... =>
5225 ... p <- s (new insn -- restore)
5226 <- ... p ... <- ... p ...
5228 where p is an original pseudo got a hard register or a hard
5229 register and s is a new split pseudo. The save is put before INSN
5230 if BEFORE_P is true. Return true if we succeed in such
5231 transformation. */
5232 static bool
5233 split_reg (bool before_p, int original_regno, rtx_insn *insn,
5234 rtx next_usage_insns)
5236 enum reg_class rclass;
5237 rtx original_reg;
5238 int hard_regno, nregs;
5239 rtx new_reg, usage_insn;
5240 rtx_insn *restore, *save;
5241 bool after_p;
5242 bool call_save_p;
5243 machine_mode mode;
5245 if (original_regno < FIRST_PSEUDO_REGISTER)
5247 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
5248 hard_regno = original_regno;
5249 call_save_p = false;
5250 nregs = 1;
5251 mode = lra_reg_info[hard_regno].biggest_mode;
5252 machine_mode reg_rtx_mode = GET_MODE (regno_reg_rtx[hard_regno]);
5253 /* A reg can have a biggest_mode of VOIDmode if it was only ever seen
5254 as part of a multi-word register. In that case, or if the biggest
5255 mode was larger than a register, just use the reg_rtx. Otherwise,
5256 limit the size to that of the biggest access in the function. */
5257 if (mode == VOIDmode
5258 || GET_MODE_SIZE (mode) > GET_MODE_SIZE (reg_rtx_mode))
5260 original_reg = regno_reg_rtx[hard_regno];
5261 mode = reg_rtx_mode;
5263 else
5264 original_reg = gen_rtx_REG (mode, hard_regno);
5266 else
5268 mode = PSEUDO_REGNO_MODE (original_regno);
5269 hard_regno = reg_renumber[original_regno];
5270 nregs = hard_regno_nregs[hard_regno][mode];
5271 rclass = lra_get_allocno_class (original_regno);
5272 original_reg = regno_reg_rtx[original_regno];
5273 call_save_p = need_for_call_save_p (original_regno);
5275 lra_assert (hard_regno >= 0);
5276 if (lra_dump_file != NULL)
5277 fprintf (lra_dump_file,
5278 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
5280 if (call_save_p)
5282 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
5283 hard_regno_nregs[hard_regno][mode],
5284 mode);
5285 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
5287 else
5289 rclass = choose_split_class (rclass, hard_regno, mode);
5290 if (rclass == NO_REGS)
5292 if (lra_dump_file != NULL)
5294 fprintf (lra_dump_file,
5295 " Rejecting split of %d(%s): "
5296 "no good reg class for %d(%s)\n",
5297 original_regno,
5298 reg_class_names[lra_get_allocno_class (original_regno)],
5299 hard_regno,
5300 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
5301 fprintf
5302 (lra_dump_file,
5303 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5305 return false;
5307 new_reg = lra_create_new_reg (mode, original_reg, rclass, "split");
5308 reg_renumber[REGNO (new_reg)] = hard_regno;
5310 save = emit_spill_move (true, new_reg, original_reg);
5311 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
5313 if (lra_dump_file != NULL)
5315 fprintf
5316 (lra_dump_file,
5317 " Rejecting split %d->%d resulting in > 2 save insns:\n",
5318 original_regno, REGNO (new_reg));
5319 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
5320 fprintf (lra_dump_file,
5321 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5323 return false;
5325 restore = emit_spill_move (false, new_reg, original_reg);
5326 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
5328 if (lra_dump_file != NULL)
5330 fprintf (lra_dump_file,
5331 " Rejecting split %d->%d "
5332 "resulting in > 2 restore insns:\n",
5333 original_regno, REGNO (new_reg));
5334 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
5335 fprintf (lra_dump_file,
5336 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5338 return false;
5340 after_p = usage_insns[original_regno].after_p;
5341 lra_reg_info[REGNO (new_reg)].restore_rtx = regno_reg_rtx[original_regno];
5342 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5343 bitmap_set_bit (&check_only_regs, original_regno);
5344 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
5345 for (;;)
5347 if (GET_CODE (next_usage_insns) != INSN_LIST)
5349 usage_insn = next_usage_insns;
5350 break;
5352 usage_insn = XEXP (next_usage_insns, 0);
5353 lra_assert (DEBUG_INSN_P (usage_insn));
5354 next_usage_insns = XEXP (next_usage_insns, 1);
5355 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5356 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5357 if (lra_dump_file != NULL)
5359 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5360 original_regno, REGNO (new_reg));
5361 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5364 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5365 lra_assert (usage_insn != insn || (after_p && before_p));
5366 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5367 after_p ? NULL : restore,
5368 after_p ? restore : NULL,
5369 call_save_p
5370 ? "Add reg<-save" : "Add reg<-split");
5371 lra_process_new_insns (insn, before_p ? save : NULL,
5372 before_p ? NULL : save,
5373 call_save_p
5374 ? "Add save<-reg" : "Add split<-reg");
5375 if (nregs > 1)
5376 /* If we are trying to split multi-register. We should check
5377 conflicts on the next assignment sub-pass. IRA can allocate on
5378 sub-register levels, LRA do this on pseudos level right now and
5379 this discrepancy may create allocation conflicts after
5380 splitting. */
5381 lra_risky_transformations_p = true;
5382 if (lra_dump_file != NULL)
5383 fprintf (lra_dump_file,
5384 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5385 return true;
5388 /* Recognize that we need a split transformation for insn INSN, which
5389 defines or uses REGNO in its insn biggest MODE (we use it only if
5390 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5391 hard registers which might be used for reloads since the EBB end.
5392 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5393 uid before starting INSN processing. Return true if we succeed in
5394 such transformation. */
5395 static bool
5396 split_if_necessary (int regno, machine_mode mode,
5397 HARD_REG_SET potential_reload_hard_regs,
5398 bool before_p, rtx_insn *insn, int max_uid)
5400 bool res = false;
5401 int i, nregs = 1;
5402 rtx next_usage_insns;
5404 if (regno < FIRST_PSEUDO_REGISTER)
5405 nregs = hard_regno_nregs[regno][mode];
5406 for (i = 0; i < nregs; i++)
5407 if (usage_insns[regno + i].check == curr_usage_insns_check
5408 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5409 /* To avoid processing the register twice or more. */
5410 && ((GET_CODE (next_usage_insns) != INSN_LIST
5411 && INSN_UID (next_usage_insns) < max_uid)
5412 || (GET_CODE (next_usage_insns) == INSN_LIST
5413 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5414 && need_for_split_p (potential_reload_hard_regs, regno + i)
5415 && split_reg (before_p, regno + i, insn, next_usage_insns))
5416 res = true;
5417 return res;
5420 /* Return TRUE if rtx X is considered as an invariant for
5421 inheritance. */
5422 static bool
5423 invariant_p (const_rtx x)
5425 machine_mode mode;
5426 const char *fmt;
5427 enum rtx_code code;
5428 int i, j;
5430 code = GET_CODE (x);
5431 mode = GET_MODE (x);
5432 if (code == SUBREG)
5434 x = SUBREG_REG (x);
5435 code = GET_CODE (x);
5436 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
5437 mode = GET_MODE (x);
5440 if (MEM_P (x))
5441 return false;
5443 if (REG_P (x))
5445 int i, nregs, regno = REGNO (x);
5447 if (regno >= FIRST_PSEUDO_REGISTER || regno == STACK_POINTER_REGNUM
5448 || TEST_HARD_REG_BIT (eliminable_regset, regno)
5449 || GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
5450 return false;
5451 nregs = hard_regno_nregs[regno][mode];
5452 for (i = 0; i < nregs; i++)
5453 if (! fixed_regs[regno + i]
5454 /* A hard register may be clobbered in the current insn
5455 but we can ignore this case because if the hard
5456 register is used it should be set somewhere after the
5457 clobber. */
5458 || bitmap_bit_p (&invalid_invariant_regs, regno + i))
5459 return false;
5461 fmt = GET_RTX_FORMAT (code);
5462 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5464 if (fmt[i] == 'e')
5466 if (! invariant_p (XEXP (x, i)))
5467 return false;
5469 else if (fmt[i] == 'E')
5471 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5472 if (! invariant_p (XVECEXP (x, i, j)))
5473 return false;
5476 return true;
5479 /* We have 'dest_reg <- invariant'. Let us try to make an invariant
5480 inheritance transformation (using dest_reg instead invariant in a
5481 subsequent insn). */
5482 static bool
5483 process_invariant_for_inheritance (rtx dst_reg, rtx invariant_rtx)
5485 invariant_ptr_t invariant_ptr;
5486 rtx_insn *insn, *new_insns;
5487 rtx insn_set, insn_reg, new_reg;
5488 int insn_regno;
5489 bool succ_p = false;
5490 int dst_regno = REGNO (dst_reg);
5491 enum machine_mode dst_mode = GET_MODE (dst_reg);
5492 enum reg_class cl = lra_get_allocno_class (dst_regno), insn_reg_cl;
5494 invariant_ptr = insert_invariant (invariant_rtx);
5495 if ((insn = invariant_ptr->insn) != NULL_RTX)
5497 /* We have a subsequent insn using the invariant. */
5498 insn_set = single_set (insn);
5499 lra_assert (insn_set != NULL);
5500 insn_reg = SET_DEST (insn_set);
5501 lra_assert (REG_P (insn_reg));
5502 insn_regno = REGNO (insn_reg);
5503 insn_reg_cl = lra_get_allocno_class (insn_regno);
5505 if (dst_mode == GET_MODE (insn_reg)
5506 /* We should consider only result move reg insns which are
5507 cheap. */
5508 && targetm.register_move_cost (dst_mode, cl, insn_reg_cl) == 2
5509 && targetm.register_move_cost (dst_mode, cl, cl) == 2)
5511 if (lra_dump_file != NULL)
5512 fprintf (lra_dump_file,
5513 " [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[\n");
5514 new_reg = lra_create_new_reg (dst_mode, dst_reg,
5515 cl, "invariant inheritance");
5516 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
5517 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5518 lra_reg_info[REGNO (new_reg)].restore_rtx = PATTERN (insn);
5519 start_sequence ();
5520 lra_emit_move (new_reg, dst_reg);
5521 new_insns = get_insns ();
5522 end_sequence ();
5523 lra_process_new_insns (curr_insn, NULL, new_insns,
5524 "Add invariant inheritance<-original");
5525 start_sequence ();
5526 lra_emit_move (SET_DEST (insn_set), new_reg);
5527 new_insns = get_insns ();
5528 end_sequence ();
5529 lra_process_new_insns (insn, NULL, new_insns,
5530 "Changing reload<-inheritance");
5531 lra_set_insn_deleted (insn);
5532 succ_p = true;
5533 if (lra_dump_file != NULL)
5535 fprintf (lra_dump_file,
5536 " Invariant inheritance reuse change %d (bb%d):\n",
5537 REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
5538 dump_insn_slim (lra_dump_file, insn);
5539 fprintf (lra_dump_file,
5540 " ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]\n");
5544 invariant_ptr->insn = curr_insn;
5545 return succ_p;
5548 /* Check only registers living at the current program point in the
5549 current EBB. */
5550 static bitmap_head live_regs;
5552 /* Update live info in EBB given by its HEAD and TAIL insns after
5553 inheritance/split transformation. The function removes dead moves
5554 too. */
5555 static void
5556 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5558 unsigned int j;
5559 int i, regno;
5560 bool live_p;
5561 rtx_insn *prev_insn;
5562 rtx set;
5563 bool remove_p;
5564 basic_block last_bb, prev_bb, curr_bb;
5565 bitmap_iterator bi;
5566 struct lra_insn_reg *reg;
5567 edge e;
5568 edge_iterator ei;
5570 last_bb = BLOCK_FOR_INSN (tail);
5571 prev_bb = NULL;
5572 for (curr_insn = tail;
5573 curr_insn != PREV_INSN (head);
5574 curr_insn = prev_insn)
5576 prev_insn = PREV_INSN (curr_insn);
5577 /* We need to process empty blocks too. They contain
5578 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5579 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5580 continue;
5581 curr_bb = BLOCK_FOR_INSN (curr_insn);
5582 if (curr_bb != prev_bb)
5584 if (prev_bb != NULL)
5586 /* Update df_get_live_in (prev_bb): */
5587 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5588 if (bitmap_bit_p (&live_regs, j))
5589 bitmap_set_bit (df_get_live_in (prev_bb), j);
5590 else
5591 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5593 if (curr_bb != last_bb)
5595 /* Update df_get_live_out (curr_bb): */
5596 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5598 live_p = bitmap_bit_p (&live_regs, j);
5599 if (! live_p)
5600 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5601 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5603 live_p = true;
5604 break;
5606 if (live_p)
5607 bitmap_set_bit (df_get_live_out (curr_bb), j);
5608 else
5609 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5612 prev_bb = curr_bb;
5613 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5615 if (! NONDEBUG_INSN_P (curr_insn))
5616 continue;
5617 curr_id = lra_get_insn_recog_data (curr_insn);
5618 curr_static_id = curr_id->insn_static_data;
5619 remove_p = false;
5620 if ((set = single_set (curr_insn)) != NULL_RTX
5621 && REG_P (SET_DEST (set))
5622 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5623 && SET_DEST (set) != pic_offset_table_rtx
5624 && bitmap_bit_p (&check_only_regs, regno)
5625 && ! bitmap_bit_p (&live_regs, regno))
5626 remove_p = true;
5627 /* See which defined values die here. */
5628 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5629 if (reg->type == OP_OUT && ! reg->subreg_p)
5630 bitmap_clear_bit (&live_regs, reg->regno);
5631 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5632 if (reg->type == OP_OUT && ! reg->subreg_p)
5633 bitmap_clear_bit (&live_regs, reg->regno);
5634 if (curr_id->arg_hard_regs != NULL)
5635 /* Make clobbered argument hard registers die. */
5636 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5637 if (regno >= FIRST_PSEUDO_REGISTER)
5638 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
5639 /* Mark each used value as live. */
5640 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5641 if (reg->type != OP_OUT
5642 && bitmap_bit_p (&check_only_regs, reg->regno))
5643 bitmap_set_bit (&live_regs, reg->regno);
5644 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5645 if (reg->type != OP_OUT
5646 && bitmap_bit_p (&check_only_regs, reg->regno))
5647 bitmap_set_bit (&live_regs, reg->regno);
5648 if (curr_id->arg_hard_regs != NULL)
5649 /* Make used argument hard registers live. */
5650 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5651 if (regno < FIRST_PSEUDO_REGISTER
5652 && bitmap_bit_p (&check_only_regs, regno))
5653 bitmap_set_bit (&live_regs, regno);
5654 /* It is quite important to remove dead move insns because it
5655 means removing dead store. We don't need to process them for
5656 constraints. */
5657 if (remove_p)
5659 if (lra_dump_file != NULL)
5661 fprintf (lra_dump_file, " Removing dead insn:\n ");
5662 dump_insn_slim (lra_dump_file, curr_insn);
5664 lra_set_insn_deleted (curr_insn);
5669 /* The structure describes info to do an inheritance for the current
5670 insn. We need to collect such info first before doing the
5671 transformations because the transformations change the insn
5672 internal representation. */
5673 struct to_inherit
5675 /* Original regno. */
5676 int regno;
5677 /* Subsequent insns which can inherit original reg value. */
5678 rtx insns;
5681 /* Array containing all info for doing inheritance from the current
5682 insn. */
5683 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5685 /* Number elements in the previous array. */
5686 static int to_inherit_num;
5688 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5689 structure to_inherit. */
5690 static void
5691 add_to_inherit (int regno, rtx insns)
5693 int i;
5695 for (i = 0; i < to_inherit_num; i++)
5696 if (to_inherit[i].regno == regno)
5697 return;
5698 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5699 to_inherit[to_inherit_num].regno = regno;
5700 to_inherit[to_inherit_num++].insns = insns;
5703 /* Return the last non-debug insn in basic block BB, or the block begin
5704 note if none. */
5705 static rtx_insn *
5706 get_last_insertion_point (basic_block bb)
5708 rtx_insn *insn;
5710 FOR_BB_INSNS_REVERSE (bb, insn)
5711 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5712 return insn;
5713 gcc_unreachable ();
5716 /* Set up RES by registers living on edges FROM except the edge (FROM,
5717 TO) or by registers set up in a jump insn in BB FROM. */
5718 static void
5719 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5721 rtx_insn *last;
5722 struct lra_insn_reg *reg;
5723 edge e;
5724 edge_iterator ei;
5726 lra_assert (to != NULL);
5727 bitmap_clear (res);
5728 FOR_EACH_EDGE (e, ei, from->succs)
5729 if (e->dest != to)
5730 bitmap_ior_into (res, df_get_live_in (e->dest));
5731 last = get_last_insertion_point (from);
5732 if (! JUMP_P (last))
5733 return;
5734 curr_id = lra_get_insn_recog_data (last);
5735 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5736 if (reg->type != OP_IN)
5737 bitmap_set_bit (res, reg->regno);
5740 /* Used as a temporary results of some bitmap calculations. */
5741 static bitmap_head temp_bitmap;
5743 /* We split for reloads of small class of hard regs. The following
5744 defines how many hard regs the class should have to be qualified as
5745 small. The code is mostly oriented to x86/x86-64 architecture
5746 where some insns need to use only specific register or pair of
5747 registers and these register can live in RTL explicitly, e.g. for
5748 parameter passing. */
5749 static const int max_small_class_regs_num = 2;
5751 /* Do inheritance/split transformations in EBB starting with HEAD and
5752 finishing on TAIL. We process EBB insns in the reverse order.
5753 Return true if we did any inheritance/split transformation in the
5754 EBB.
5756 We should avoid excessive splitting which results in worse code
5757 because of inaccurate cost calculations for spilling new split
5758 pseudos in such case. To achieve this we do splitting only if
5759 register pressure is high in given basic block and there are reload
5760 pseudos requiring hard registers. We could do more register
5761 pressure calculations at any given program point to avoid necessary
5762 splitting even more but it is to expensive and the current approach
5763 works well enough. */
5764 static bool
5765 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5767 int i, src_regno, dst_regno, nregs;
5768 bool change_p, succ_p, update_reloads_num_p;
5769 rtx_insn *prev_insn, *last_insn;
5770 rtx next_usage_insns, curr_set;
5771 enum reg_class cl;
5772 struct lra_insn_reg *reg;
5773 basic_block last_processed_bb, curr_bb = NULL;
5774 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5775 bitmap to_process;
5776 unsigned int j;
5777 bitmap_iterator bi;
5778 bool head_p, after_p;
5780 change_p = false;
5781 curr_usage_insns_check++;
5782 clear_invariants ();
5783 reloads_num = calls_num = 0;
5784 bitmap_clear (&check_only_regs);
5785 bitmap_clear (&invalid_invariant_regs);
5786 last_processed_bb = NULL;
5787 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5788 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5789 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5790 /* We don't process new insns generated in the loop. */
5791 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5793 prev_insn = PREV_INSN (curr_insn);
5794 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5795 curr_bb = BLOCK_FOR_INSN (curr_insn);
5796 if (last_processed_bb != curr_bb)
5798 /* We are at the end of BB. Add qualified living
5799 pseudos for potential splitting. */
5800 to_process = df_get_live_out (curr_bb);
5801 if (last_processed_bb != NULL)
5803 /* We are somewhere in the middle of EBB. */
5804 get_live_on_other_edges (curr_bb, last_processed_bb,
5805 &temp_bitmap);
5806 to_process = &temp_bitmap;
5808 last_processed_bb = curr_bb;
5809 last_insn = get_last_insertion_point (curr_bb);
5810 after_p = (! JUMP_P (last_insn)
5811 && (! CALL_P (last_insn)
5812 || (find_reg_note (last_insn,
5813 REG_NORETURN, NULL_RTX) == NULL_RTX
5814 && ! SIBLING_CALL_P (last_insn))));
5815 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5816 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5818 if ((int) j >= lra_constraint_new_regno_start)
5819 break;
5820 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5822 if (j < FIRST_PSEUDO_REGISTER)
5823 SET_HARD_REG_BIT (live_hard_regs, j);
5824 else
5825 add_to_hard_reg_set (&live_hard_regs,
5826 PSEUDO_REGNO_MODE (j),
5827 reg_renumber[j]);
5828 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5832 src_regno = dst_regno = -1;
5833 curr_set = single_set (curr_insn);
5834 if (curr_set != NULL_RTX && REG_P (SET_DEST (curr_set)))
5835 dst_regno = REGNO (SET_DEST (curr_set));
5836 if (curr_set != NULL_RTX && REG_P (SET_SRC (curr_set)))
5837 src_regno = REGNO (SET_SRC (curr_set));
5838 update_reloads_num_p = true;
5839 if (src_regno < lra_constraint_new_regno_start
5840 && src_regno >= FIRST_PSEUDO_REGISTER
5841 && reg_renumber[src_regno] < 0
5842 && dst_regno >= lra_constraint_new_regno_start
5843 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5845 /* 'reload_pseudo <- original_pseudo'. */
5846 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5847 reloads_num++;
5848 update_reloads_num_p = false;
5849 succ_p = false;
5850 if (usage_insns[src_regno].check == curr_usage_insns_check
5851 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5852 succ_p = inherit_reload_reg (false, src_regno, cl,
5853 curr_insn, next_usage_insns);
5854 if (succ_p)
5855 change_p = true;
5856 else
5857 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5858 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5859 IOR_HARD_REG_SET (potential_reload_hard_regs,
5860 reg_class_contents[cl]);
5862 else if (src_regno < 0
5863 && dst_regno >= lra_constraint_new_regno_start
5864 && invariant_p (SET_SRC (curr_set))
5865 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS
5866 && ! bitmap_bit_p (&invalid_invariant_regs, dst_regno))
5868 /* 'reload_pseudo <- invariant'. */
5869 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5870 reloads_num++;
5871 update_reloads_num_p = false;
5872 if (process_invariant_for_inheritance (SET_DEST (curr_set), SET_SRC (curr_set)))
5873 change_p = true;
5874 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5875 IOR_HARD_REG_SET (potential_reload_hard_regs,
5876 reg_class_contents[cl]);
5878 else if (src_regno >= lra_constraint_new_regno_start
5879 && dst_regno < lra_constraint_new_regno_start
5880 && dst_regno >= FIRST_PSEUDO_REGISTER
5881 && reg_renumber[dst_regno] < 0
5882 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5883 && usage_insns[dst_regno].check == curr_usage_insns_check
5884 && (next_usage_insns
5885 = usage_insns[dst_regno].insns) != NULL_RTX)
5887 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5888 reloads_num++;
5889 update_reloads_num_p = false;
5890 /* 'original_pseudo <- reload_pseudo'. */
5891 if (! JUMP_P (curr_insn)
5892 && inherit_reload_reg (true, dst_regno, cl,
5893 curr_insn, next_usage_insns))
5894 change_p = true;
5895 /* Invalidate. */
5896 usage_insns[dst_regno].check = 0;
5897 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5898 IOR_HARD_REG_SET (potential_reload_hard_regs,
5899 reg_class_contents[cl]);
5901 else if (INSN_P (curr_insn))
5903 int iter;
5904 int max_uid = get_max_uid ();
5906 curr_id = lra_get_insn_recog_data (curr_insn);
5907 curr_static_id = curr_id->insn_static_data;
5908 to_inherit_num = 0;
5909 /* Process insn definitions. */
5910 for (iter = 0; iter < 2; iter++)
5911 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5912 reg != NULL;
5913 reg = reg->next)
5914 if (reg->type != OP_IN
5915 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5917 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5918 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5919 && usage_insns[dst_regno].check == curr_usage_insns_check
5920 && (next_usage_insns
5921 = usage_insns[dst_regno].insns) != NULL_RTX)
5923 struct lra_insn_reg *r;
5925 for (r = curr_id->regs; r != NULL; r = r->next)
5926 if (r->type != OP_OUT && r->regno == dst_regno)
5927 break;
5928 /* Don't do inheritance if the pseudo is also
5929 used in the insn. */
5930 if (r == NULL)
5931 /* We can not do inheritance right now
5932 because the current insn reg info (chain
5933 regs) can change after that. */
5934 add_to_inherit (dst_regno, next_usage_insns);
5936 /* We can not process one reg twice here because of
5937 usage_insns invalidation. */
5938 if ((dst_regno < FIRST_PSEUDO_REGISTER
5939 || reg_renumber[dst_regno] >= 0)
5940 && ! reg->subreg_p && reg->type != OP_IN)
5942 HARD_REG_SET s;
5944 if (split_if_necessary (dst_regno, reg->biggest_mode,
5945 potential_reload_hard_regs,
5946 false, curr_insn, max_uid))
5947 change_p = true;
5948 CLEAR_HARD_REG_SET (s);
5949 if (dst_regno < FIRST_PSEUDO_REGISTER)
5950 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5951 else
5952 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5953 reg_renumber[dst_regno]);
5954 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5956 /* We should invalidate potential inheritance or
5957 splitting for the current insn usages to the next
5958 usage insns (see code below) as the output pseudo
5959 prevents this. */
5960 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5961 && reg_renumber[dst_regno] < 0)
5962 || (reg->type == OP_OUT && ! reg->subreg_p
5963 && (dst_regno < FIRST_PSEUDO_REGISTER
5964 || reg_renumber[dst_regno] >= 0)))
5966 /* Invalidate and mark definitions. */
5967 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5968 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5969 else
5971 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5972 for (i = 0; i < nregs; i++)
5973 usage_insns[dst_regno + i].check
5974 = -(int) INSN_UID (curr_insn);
5978 /* Process clobbered call regs. */
5979 if (curr_id->arg_hard_regs != NULL)
5980 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5981 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5982 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
5983 = -(int) INSN_UID (curr_insn);
5984 if (! JUMP_P (curr_insn))
5985 for (i = 0; i < to_inherit_num; i++)
5986 if (inherit_reload_reg (true, to_inherit[i].regno,
5987 ALL_REGS, curr_insn,
5988 to_inherit[i].insns))
5989 change_p = true;
5990 if (CALL_P (curr_insn))
5992 rtx cheap, pat, dest;
5993 rtx_insn *restore;
5994 int regno, hard_regno;
5996 calls_num++;
5997 if ((cheap = find_reg_note (curr_insn,
5998 REG_RETURNED, NULL_RTX)) != NULL_RTX
5999 && ((cheap = XEXP (cheap, 0)), true)
6000 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
6001 && (hard_regno = reg_renumber[regno]) >= 0
6002 /* If there are pending saves/restores, the
6003 optimization is not worth. */
6004 && usage_insns[regno].calls_num == calls_num - 1
6005 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
6007 /* Restore the pseudo from the call result as
6008 REG_RETURNED note says that the pseudo value is
6009 in the call result and the pseudo is an argument
6010 of the call. */
6011 pat = PATTERN (curr_insn);
6012 if (GET_CODE (pat) == PARALLEL)
6013 pat = XVECEXP (pat, 0, 0);
6014 dest = SET_DEST (pat);
6015 /* For multiple return values dest is PARALLEL.
6016 Currently we handle only single return value case. */
6017 if (REG_P (dest))
6019 start_sequence ();
6020 emit_move_insn (cheap, copy_rtx (dest));
6021 restore = get_insns ();
6022 end_sequence ();
6023 lra_process_new_insns (curr_insn, NULL, restore,
6024 "Inserting call parameter restore");
6025 /* We don't need to save/restore of the pseudo from
6026 this call. */
6027 usage_insns[regno].calls_num = calls_num;
6028 bitmap_set_bit (&check_only_regs, regno);
6032 to_inherit_num = 0;
6033 /* Process insn usages. */
6034 for (iter = 0; iter < 2; iter++)
6035 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6036 reg != NULL;
6037 reg = reg->next)
6038 if ((reg->type != OP_OUT
6039 || (reg->type == OP_OUT && reg->subreg_p))
6040 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
6042 if (src_regno >= FIRST_PSEUDO_REGISTER
6043 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
6045 if (usage_insns[src_regno].check == curr_usage_insns_check
6046 && (next_usage_insns
6047 = usage_insns[src_regno].insns) != NULL_RTX
6048 && NONDEBUG_INSN_P (curr_insn))
6049 add_to_inherit (src_regno, next_usage_insns);
6050 else if (usage_insns[src_regno].check
6051 != -(int) INSN_UID (curr_insn))
6052 /* Add usages but only if the reg is not set up
6053 in the same insn. */
6054 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6056 else if (src_regno < FIRST_PSEUDO_REGISTER
6057 || reg_renumber[src_regno] >= 0)
6059 bool before_p;
6060 rtx_insn *use_insn = curr_insn;
6062 before_p = (JUMP_P (curr_insn)
6063 || (CALL_P (curr_insn) && reg->type == OP_IN));
6064 if (NONDEBUG_INSN_P (curr_insn)
6065 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
6066 && split_if_necessary (src_regno, reg->biggest_mode,
6067 potential_reload_hard_regs,
6068 before_p, curr_insn, max_uid))
6070 if (reg->subreg_p)
6071 lra_risky_transformations_p = true;
6072 change_p = true;
6073 /* Invalidate. */
6074 usage_insns[src_regno].check = 0;
6075 if (before_p)
6076 use_insn = PREV_INSN (curr_insn);
6078 if (NONDEBUG_INSN_P (curr_insn))
6080 if (src_regno < FIRST_PSEUDO_REGISTER)
6081 add_to_hard_reg_set (&live_hard_regs,
6082 reg->biggest_mode, src_regno);
6083 else
6084 add_to_hard_reg_set (&live_hard_regs,
6085 PSEUDO_REGNO_MODE (src_regno),
6086 reg_renumber[src_regno]);
6088 add_next_usage_insn (src_regno, use_insn, reloads_num);
6091 /* Process used call regs. */
6092 if (curr_id->arg_hard_regs != NULL)
6093 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6094 if (src_regno < FIRST_PSEUDO_REGISTER)
6096 SET_HARD_REG_BIT (live_hard_regs, src_regno);
6097 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6099 for (i = 0; i < to_inherit_num; i++)
6101 src_regno = to_inherit[i].regno;
6102 if (inherit_reload_reg (false, src_regno, ALL_REGS,
6103 curr_insn, to_inherit[i].insns))
6104 change_p = true;
6105 else
6106 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6109 if (update_reloads_num_p
6110 && NONDEBUG_INSN_P (curr_insn) && curr_set != NULL_RTX)
6112 int regno = -1;
6113 if ((REG_P (SET_DEST (curr_set))
6114 && (regno = REGNO (SET_DEST (curr_set))) >= lra_constraint_new_regno_start
6115 && reg_renumber[regno] < 0
6116 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
6117 || (REG_P (SET_SRC (curr_set))
6118 && (regno = REGNO (SET_SRC (curr_set))) >= lra_constraint_new_regno_start
6119 && reg_renumber[regno] < 0
6120 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
6122 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6123 reloads_num++;
6124 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6125 IOR_HARD_REG_SET (potential_reload_hard_regs,
6126 reg_class_contents[cl]);
6129 if (NONDEBUG_INSN_P (curr_insn))
6131 int regno;
6133 /* Invalidate invariants with changed regs. */
6134 curr_id = lra_get_insn_recog_data (curr_insn);
6135 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6136 if (reg->type != OP_IN)
6137 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6138 curr_static_id = curr_id->insn_static_data;
6139 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6140 if (reg->type != OP_IN)
6141 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6142 if (curr_id->arg_hard_regs != NULL)
6143 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6144 bitmap_set_bit (&invalid_invariant_regs,
6145 regno >= FIRST_PSEUDO_REGISTER
6146 ? regno : regno - FIRST_PSEUDO_REGISTER);
6148 /* We reached the start of the current basic block. */
6149 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
6150 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
6152 /* We reached the beginning of the current block -- do
6153 rest of spliting in the current BB. */
6154 to_process = df_get_live_in (curr_bb);
6155 if (BLOCK_FOR_INSN (head) != curr_bb)
6157 /* We are somewhere in the middle of EBB. */
6158 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
6159 curr_bb, &temp_bitmap);
6160 to_process = &temp_bitmap;
6162 head_p = true;
6163 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
6165 if ((int) j >= lra_constraint_new_regno_start)
6166 break;
6167 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
6168 && usage_insns[j].check == curr_usage_insns_check
6169 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
6171 if (need_for_split_p (potential_reload_hard_regs, j))
6173 if (lra_dump_file != NULL && head_p)
6175 fprintf (lra_dump_file,
6176 " ----------------------------------\n");
6177 head_p = false;
6179 if (split_reg (false, j, bb_note (curr_bb),
6180 next_usage_insns))
6181 change_p = true;
6183 usage_insns[j].check = 0;
6188 return change_p;
6191 /* This value affects EBB forming. If probability of edge from EBB to
6192 a BB is not greater than the following value, we don't add the BB
6193 to EBB. */
6194 #define EBB_PROBABILITY_CUTOFF \
6195 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
6197 /* Current number of inheritance/split iteration. */
6198 int lra_inheritance_iter;
6200 /* Entry function for inheritance/split pass. */
6201 void
6202 lra_inheritance (void)
6204 int i;
6205 basic_block bb, start_bb;
6206 edge e;
6208 lra_inheritance_iter++;
6209 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6210 return;
6211 timevar_push (TV_LRA_INHERITANCE);
6212 if (lra_dump_file != NULL)
6213 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
6214 lra_inheritance_iter);
6215 curr_usage_insns_check = 0;
6216 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
6217 for (i = 0; i < lra_constraint_new_regno_start; i++)
6218 usage_insns[i].check = 0;
6219 bitmap_initialize (&check_only_regs, &reg_obstack);
6220 bitmap_initialize (&invalid_invariant_regs, &reg_obstack);
6221 bitmap_initialize (&live_regs, &reg_obstack);
6222 bitmap_initialize (&temp_bitmap, &reg_obstack);
6223 bitmap_initialize (&ebb_global_regs, &reg_obstack);
6224 FOR_EACH_BB_FN (bb, cfun)
6226 start_bb = bb;
6227 if (lra_dump_file != NULL)
6228 fprintf (lra_dump_file, "EBB");
6229 /* Form a EBB starting with BB. */
6230 bitmap_clear (&ebb_global_regs);
6231 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
6232 for (;;)
6234 if (lra_dump_file != NULL)
6235 fprintf (lra_dump_file, " %d", bb->index);
6236 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
6237 || LABEL_P (BB_HEAD (bb->next_bb)))
6238 break;
6239 e = find_fallthru_edge (bb->succs);
6240 if (! e)
6241 break;
6242 if (e->probability < EBB_PROBABILITY_CUTOFF)
6243 break;
6244 bb = bb->next_bb;
6246 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
6247 if (lra_dump_file != NULL)
6248 fprintf (lra_dump_file, "\n");
6249 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
6250 /* Remember that the EBB head and tail can change in
6251 inherit_in_ebb. */
6252 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
6254 bitmap_clear (&ebb_global_regs);
6255 bitmap_clear (&temp_bitmap);
6256 bitmap_clear (&live_regs);
6257 bitmap_clear (&invalid_invariant_regs);
6258 bitmap_clear (&check_only_regs);
6259 free (usage_insns);
6261 timevar_pop (TV_LRA_INHERITANCE);
6266 /* This page contains code to undo failed inheritance/split
6267 transformations. */
6269 /* Current number of iteration undoing inheritance/split. */
6270 int lra_undo_inheritance_iter;
6272 /* Fix BB live info LIVE after removing pseudos created on pass doing
6273 inheritance/split which are REMOVED_PSEUDOS. */
6274 static void
6275 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
6277 unsigned int regno;
6278 bitmap_iterator bi;
6280 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
6281 if (bitmap_clear_bit (live, regno)
6282 && REG_P (lra_reg_info[regno].restore_rtx))
6283 bitmap_set_bit (live, REGNO (lra_reg_info[regno].restore_rtx));
6286 /* Return regno of the (subreg of) REG. Otherwise, return a negative
6287 number. */
6288 static int
6289 get_regno (rtx reg)
6291 if (GET_CODE (reg) == SUBREG)
6292 reg = SUBREG_REG (reg);
6293 if (REG_P (reg))
6294 return REGNO (reg);
6295 return -1;
6298 /* Delete a move INSN with destination reg DREGNO and a previous
6299 clobber insn with the same regno. The inheritance/split code can
6300 generate moves with preceding clobber and when we delete such moves
6301 we should delete the clobber insn too to keep the correct life
6302 info. */
6303 static void
6304 delete_move_and_clobber (rtx_insn *insn, int dregno)
6306 rtx_insn *prev_insn = PREV_INSN (insn);
6308 lra_set_insn_deleted (insn);
6309 lra_assert (dregno >= 0);
6310 if (prev_insn != NULL && NONDEBUG_INSN_P (prev_insn)
6311 && GET_CODE (PATTERN (prev_insn)) == CLOBBER
6312 && dregno == get_regno (XEXP (PATTERN (prev_insn), 0)))
6313 lra_set_insn_deleted (prev_insn);
6316 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
6317 return true if we did any change. The undo transformations for
6318 inheritance looks like
6319 i <- i2
6320 p <- i => p <- i2
6321 or removing
6322 p <- i, i <- p, and i <- i3
6323 where p is original pseudo from which inheritance pseudo i was
6324 created, i and i3 are removed inheritance pseudos, i2 is another
6325 not removed inheritance pseudo. All split pseudos or other
6326 occurrences of removed inheritance pseudos are changed on the
6327 corresponding original pseudos.
6329 The function also schedules insns changed and created during
6330 inheritance/split pass for processing by the subsequent constraint
6331 pass. */
6332 static bool
6333 remove_inheritance_pseudos (bitmap remove_pseudos)
6335 basic_block bb;
6336 int regno, sregno, prev_sregno, dregno;
6337 rtx restore_rtx;
6338 rtx set, prev_set;
6339 rtx_insn *prev_insn;
6340 bool change_p, done_p;
6342 change_p = ! bitmap_empty_p (remove_pseudos);
6343 /* We can not finish the function right away if CHANGE_P is true
6344 because we need to marks insns affected by previous
6345 inheritance/split pass for processing by the subsequent
6346 constraint pass. */
6347 FOR_EACH_BB_FN (bb, cfun)
6349 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
6350 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
6351 FOR_BB_INSNS_REVERSE (bb, curr_insn)
6353 if (! INSN_P (curr_insn))
6354 continue;
6355 done_p = false;
6356 sregno = dregno = -1;
6357 if (change_p && NONDEBUG_INSN_P (curr_insn)
6358 && (set = single_set (curr_insn)) != NULL_RTX)
6360 dregno = get_regno (SET_DEST (set));
6361 sregno = get_regno (SET_SRC (set));
6364 if (sregno >= 0 && dregno >= 0)
6366 if (bitmap_bit_p (remove_pseudos, dregno)
6367 && ! REG_P (lra_reg_info[dregno].restore_rtx))
6369 /* invariant inheritance pseudo <- original pseudo */
6370 if (lra_dump_file != NULL)
6372 fprintf (lra_dump_file, " Removing invariant inheritance:\n");
6373 dump_insn_slim (lra_dump_file, curr_insn);
6374 fprintf (lra_dump_file, "\n");
6376 delete_move_and_clobber (curr_insn, dregno);
6377 done_p = true;
6379 else if (bitmap_bit_p (remove_pseudos, sregno)
6380 && ! REG_P (lra_reg_info[sregno].restore_rtx))
6382 /* reload pseudo <- invariant inheritance pseudo */
6383 start_sequence ();
6384 /* We can not just change the source. It might be
6385 an insn different from the move. */
6386 emit_insn (lra_reg_info[sregno].restore_rtx);
6387 rtx_insn *new_insns = get_insns ();
6388 end_sequence ();
6389 lra_assert (single_set (new_insns) != NULL
6390 && SET_DEST (set) == SET_DEST (single_set (new_insns)));
6391 lra_process_new_insns (curr_insn, NULL, new_insns,
6392 "Changing reload<-invariant inheritance");
6393 delete_move_and_clobber (curr_insn, dregno);
6394 done_p = true;
6396 else if ((bitmap_bit_p (remove_pseudos, sregno)
6397 && (get_regno (lra_reg_info[sregno].restore_rtx) == dregno
6398 || (bitmap_bit_p (remove_pseudos, dregno)
6399 && get_regno (lra_reg_info[sregno].restore_rtx) >= 0
6400 && (get_regno (lra_reg_info[sregno].restore_rtx)
6401 == get_regno (lra_reg_info[dregno].restore_rtx)))))
6402 || (bitmap_bit_p (remove_pseudos, dregno)
6403 && get_regno (lra_reg_info[dregno].restore_rtx) == sregno))
6404 /* One of the following cases:
6405 original <- removed inheritance pseudo
6406 removed inherit pseudo <- another removed inherit pseudo
6407 removed inherit pseudo <- original pseudo
6409 removed_split_pseudo <- original_reg
6410 original_reg <- removed_split_pseudo */
6412 if (lra_dump_file != NULL)
6414 fprintf (lra_dump_file, " Removing %s:\n",
6415 bitmap_bit_p (&lra_split_regs, sregno)
6416 || bitmap_bit_p (&lra_split_regs, dregno)
6417 ? "split" : "inheritance");
6418 dump_insn_slim (lra_dump_file, curr_insn);
6420 delete_move_and_clobber (curr_insn, dregno);
6421 done_p = true;
6423 else if (bitmap_bit_p (remove_pseudos, sregno)
6424 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
6426 /* Search the following pattern:
6427 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
6428 original_pseudo <- inherit_or_split_pseudo1
6429 where the 2nd insn is the current insn and
6430 inherit_or_split_pseudo2 is not removed. If it is found,
6431 change the current insn onto:
6432 original_pseudo <- inherit_or_split_pseudo2. */
6433 for (prev_insn = PREV_INSN (curr_insn);
6434 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
6435 prev_insn = PREV_INSN (prev_insn))
6437 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
6438 && (prev_set = single_set (prev_insn)) != NULL_RTX
6439 /* There should be no subregs in insn we are
6440 searching because only the original reg might
6441 be in subreg when we changed the mode of
6442 load/store for splitting. */
6443 && REG_P (SET_DEST (prev_set))
6444 && REG_P (SET_SRC (prev_set))
6445 && (int) REGNO (SET_DEST (prev_set)) == sregno
6446 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
6447 >= FIRST_PSEUDO_REGISTER)
6448 && (lra_reg_info[prev_sregno].restore_rtx == NULL_RTX
6450 /* As we consider chain of inheritance or
6451 splitting described in above comment we should
6452 check that sregno and prev_sregno were
6453 inheritance/split pseudos created from the
6454 same original regno. */
6455 (get_regno (lra_reg_info[sregno].restore_rtx) >= 0
6456 && (get_regno (lra_reg_info[sregno].restore_rtx)
6457 == get_regno (lra_reg_info[prev_sregno].restore_rtx))))
6458 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
6460 lra_assert (GET_MODE (SET_SRC (prev_set))
6461 == GET_MODE (regno_reg_rtx[sregno]));
6462 if (GET_CODE (SET_SRC (set)) == SUBREG)
6463 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
6464 else
6465 SET_SRC (set) = SET_SRC (prev_set);
6466 /* As we are finishing with processing the insn
6467 here, check the destination too as it might
6468 inheritance pseudo for another pseudo. */
6469 if (bitmap_bit_p (remove_pseudos, dregno)
6470 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
6471 && (restore_rtx
6472 = lra_reg_info[dregno].restore_rtx) != NULL_RTX)
6474 if (GET_CODE (SET_DEST (set)) == SUBREG)
6475 SUBREG_REG (SET_DEST (set)) = restore_rtx;
6476 else
6477 SET_DEST (set) = restore_rtx;
6479 lra_push_insn_and_update_insn_regno_info (curr_insn);
6480 lra_set_used_insn_alternative_by_uid
6481 (INSN_UID (curr_insn), -1);
6482 done_p = true;
6483 if (lra_dump_file != NULL)
6485 fprintf (lra_dump_file, " Change reload insn:\n");
6486 dump_insn_slim (lra_dump_file, curr_insn);
6491 if (! done_p)
6493 struct lra_insn_reg *reg;
6494 bool restored_regs_p = false;
6495 bool kept_regs_p = false;
6497 curr_id = lra_get_insn_recog_data (curr_insn);
6498 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6500 regno = reg->regno;
6501 restore_rtx = lra_reg_info[regno].restore_rtx;
6502 if (restore_rtx != NULL_RTX)
6504 if (change_p && bitmap_bit_p (remove_pseudos, regno))
6506 lra_substitute_pseudo_within_insn
6507 (curr_insn, regno, restore_rtx, false);
6508 restored_regs_p = true;
6510 else
6511 kept_regs_p = true;
6514 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
6516 /* The instruction has changed since the previous
6517 constraints pass. */
6518 lra_push_insn_and_update_insn_regno_info (curr_insn);
6519 lra_set_used_insn_alternative_by_uid
6520 (INSN_UID (curr_insn), -1);
6522 else if (restored_regs_p)
6523 /* The instruction has been restored to the form that
6524 it had during the previous constraints pass. */
6525 lra_update_insn_regno_info (curr_insn);
6526 if (restored_regs_p && lra_dump_file != NULL)
6528 fprintf (lra_dump_file, " Insn after restoring regs:\n");
6529 dump_insn_slim (lra_dump_file, curr_insn);
6534 return change_p;
6537 /* If optional reload pseudos failed to get a hard register or was not
6538 inherited, it is better to remove optional reloads. We do this
6539 transformation after undoing inheritance to figure out necessity to
6540 remove optional reloads easier. Return true if we do any
6541 change. */
6542 static bool
6543 undo_optional_reloads (void)
6545 bool change_p, keep_p;
6546 unsigned int regno, uid;
6547 bitmap_iterator bi, bi2;
6548 rtx_insn *insn;
6549 rtx set, src, dest;
6550 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
6552 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
6553 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
6554 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6556 keep_p = false;
6557 /* Keep optional reloads from previous subpasses. */
6558 if (lra_reg_info[regno].restore_rtx == NULL_RTX
6559 /* If the original pseudo changed its allocation, just
6560 removing the optional pseudo is dangerous as the original
6561 pseudo will have longer live range. */
6562 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] >= 0)
6563 keep_p = true;
6564 else if (reg_renumber[regno] >= 0)
6565 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
6567 insn = lra_insn_recog_data[uid]->insn;
6568 if ((set = single_set (insn)) == NULL_RTX)
6569 continue;
6570 src = SET_SRC (set);
6571 dest = SET_DEST (set);
6572 if (! REG_P (src) || ! REG_P (dest))
6573 continue;
6574 if (REGNO (dest) == regno
6575 /* Ignore insn for optional reloads itself. */
6576 && REGNO (lra_reg_info[regno].restore_rtx) != REGNO (src)
6577 /* Check only inheritance on last inheritance pass. */
6578 && (int) REGNO (src) >= new_regno_start
6579 /* Check that the optional reload was inherited. */
6580 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6582 keep_p = true;
6583 break;
6586 if (keep_p)
6588 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6589 if (lra_dump_file != NULL)
6590 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6593 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6594 bitmap_initialize (&insn_bitmap, &reg_obstack);
6595 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6597 if (lra_dump_file != NULL)
6598 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6599 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6600 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6602 insn = lra_insn_recog_data[uid]->insn;
6603 if ((set = single_set (insn)) != NULL_RTX)
6605 src = SET_SRC (set);
6606 dest = SET_DEST (set);
6607 if (REG_P (src) && REG_P (dest)
6608 && ((REGNO (src) == regno
6609 && (REGNO (lra_reg_info[regno].restore_rtx)
6610 == REGNO (dest)))
6611 || (REGNO (dest) == regno
6612 && (REGNO (lra_reg_info[regno].restore_rtx)
6613 == REGNO (src)))))
6615 if (lra_dump_file != NULL)
6617 fprintf (lra_dump_file, " Deleting move %u\n",
6618 INSN_UID (insn));
6619 dump_insn_slim (lra_dump_file, insn);
6621 delete_move_and_clobber (insn, REGNO (dest));
6622 continue;
6624 /* We should not worry about generation memory-memory
6625 moves here as if the corresponding inheritance did
6626 not work (inheritance pseudo did not get a hard reg),
6627 we remove the inheritance pseudo and the optional
6628 reload. */
6630 lra_substitute_pseudo_within_insn
6631 (insn, regno, lra_reg_info[regno].restore_rtx, false);
6632 lra_update_insn_regno_info (insn);
6633 if (lra_dump_file != NULL)
6635 fprintf (lra_dump_file,
6636 " Restoring original insn:\n");
6637 dump_insn_slim (lra_dump_file, insn);
6641 /* Clear restore_regnos. */
6642 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6643 lra_reg_info[regno].restore_rtx = NULL_RTX;
6644 bitmap_clear (&insn_bitmap);
6645 bitmap_clear (&removed_optional_reload_pseudos);
6646 return change_p;
6649 /* Entry function for undoing inheritance/split transformation. Return true
6650 if we did any RTL change in this pass. */
6651 bool
6652 lra_undo_inheritance (void)
6654 unsigned int regno;
6655 int hard_regno;
6656 int n_all_inherit, n_inherit, n_all_split, n_split;
6657 rtx restore_rtx;
6658 bitmap_head remove_pseudos;
6659 bitmap_iterator bi;
6660 bool change_p;
6662 lra_undo_inheritance_iter++;
6663 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6664 return false;
6665 if (lra_dump_file != NULL)
6666 fprintf (lra_dump_file,
6667 "\n********** Undoing inheritance #%d: **********\n\n",
6668 lra_undo_inheritance_iter);
6669 bitmap_initialize (&remove_pseudos, &reg_obstack);
6670 n_inherit = n_all_inherit = 0;
6671 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6672 if (lra_reg_info[regno].restore_rtx != NULL_RTX)
6674 n_all_inherit++;
6675 if (reg_renumber[regno] < 0
6676 /* If the original pseudo changed its allocation, just
6677 removing inheritance is dangerous as for changing
6678 allocation we used shorter live-ranges. */
6679 && (! REG_P (lra_reg_info[regno].restore_rtx)
6680 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] < 0))
6681 bitmap_set_bit (&remove_pseudos, regno);
6682 else
6683 n_inherit++;
6685 if (lra_dump_file != NULL && n_all_inherit != 0)
6686 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6687 n_inherit, n_all_inherit,
6688 (double) n_inherit / n_all_inherit * 100);
6689 n_split = n_all_split = 0;
6690 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6691 if ((restore_rtx = lra_reg_info[regno].restore_rtx) != NULL_RTX)
6693 int restore_regno = REGNO (restore_rtx);
6695 n_all_split++;
6696 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6697 ? reg_renumber[restore_regno] : restore_regno);
6698 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6699 bitmap_set_bit (&remove_pseudos, regno);
6700 else
6702 n_split++;
6703 if (lra_dump_file != NULL)
6704 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6705 regno, restore_regno);
6708 if (lra_dump_file != NULL && n_all_split != 0)
6709 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6710 n_split, n_all_split,
6711 (double) n_split / n_all_split * 100);
6712 change_p = remove_inheritance_pseudos (&remove_pseudos);
6713 bitmap_clear (&remove_pseudos);
6714 /* Clear restore_regnos. */
6715 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6716 lra_reg_info[regno].restore_rtx = NULL_RTX;
6717 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6718 lra_reg_info[regno].restore_rtx = NULL_RTX;
6719 change_p = undo_optional_reloads () || change_p;
6720 return change_p;