2015-10-18 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / lra-constraints.c
blobf8fb36e61e1f6a899d85aa9e34a09425b87f8511
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2015 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains code for 3 passes: constraint pass,
23 inheritance/split pass, and pass for undoing failed inheritance and
24 split.
26 The major goal of constraint pass is to transform RTL to satisfy
27 insn and address constraints by:
28 o choosing insn alternatives;
29 o generating *reload insns* (or reloads in brief) and *reload
30 pseudos* which will get necessary hard registers later;
31 o substituting pseudos with equivalent values and removing the
32 instructions that initialized those pseudos.
34 The constraint pass has biggest and most complicated code in LRA.
35 There are a lot of important details like:
36 o reuse of input reload pseudos to simplify reload pseudo
37 allocations;
38 o some heuristics to choose insn alternative to improve the
39 inheritance;
40 o early clobbers etc.
42 The pass is mimicking former reload pass in alternative choosing
43 because the reload pass is oriented to current machine description
44 model. It might be changed if the machine description model is
45 changed.
47 There is special code for preventing all LRA and this pass cycling
48 in case of bugs.
50 On the first iteration of the pass we process every instruction and
51 choose an alternative for each one. On subsequent iterations we try
52 to avoid reprocessing instructions if we can be sure that the old
53 choice is still valid.
55 The inheritance/spilt pass is to transform code to achieve
56 ineheritance and live range splitting. It is done on backward
57 traversal of EBBs.
59 The inheritance optimization goal is to reuse values in hard
60 registers. There is analogous optimization in old reload pass. The
61 inheritance is achieved by following transformation:
63 reload_p1 <- p reload_p1 <- p
64 ... new_p <- reload_p1
65 ... => ...
66 reload_p2 <- p reload_p2 <- new_p
68 where p is spilled and not changed between the insns. Reload_p1 is
69 also called *original pseudo* and new_p is called *inheritance
70 pseudo*.
72 The subsequent assignment pass will try to assign the same (or
73 another if it is not possible) hard register to new_p as to
74 reload_p1 or reload_p2.
76 If the assignment pass fails to assign a hard register to new_p,
77 this file will undo the inheritance and restore the original code.
78 This is because implementing the above sequence with a spilled
79 new_p would make the code much worse. The inheritance is done in
80 EBB scope. The above is just a simplified example to get an idea
81 of the inheritance as the inheritance is also done for non-reload
82 insns.
84 Splitting (transformation) is also done in EBB scope on the same
85 pass as the inheritance:
87 r <- ... or ... <- r r <- ... or ... <- r
88 ... s <- r (new insn -- save)
89 ... =>
90 ... r <- s (new insn -- restore)
91 ... <- r ... <- r
93 The *split pseudo* s is assigned to the hard register of the
94 original pseudo or hard register r.
96 Splitting is done:
97 o In EBBs with high register pressure for global pseudos (living
98 in at least 2 BBs) and assigned to hard registers when there
99 are more one reloads needing the hard registers;
100 o for pseudos needing save/restore code around calls.
102 If the split pseudo still has the same hard register as the
103 original pseudo after the subsequent assignment pass or the
104 original pseudo was split, the opposite transformation is done on
105 the same pass for undoing inheritance. */
107 #undef REG_OK_STRICT
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "backend.h"
113 #include "predict.h"
114 #include "tree.h"
115 #include "rtl.h"
116 #include "df.h"
117 #include "tm_p.h"
118 #include "regs.h"
119 #include "insn-config.h"
120 #include "insn-codes.h"
121 #include "recog.h"
122 #include "output.h"
123 #include "addresses.h"
124 #include "target.h"
125 #include "flags.h"
126 #include "alias.h"
127 #include "expmed.h"
128 #include "dojump.h"
129 #include "explow.h"
130 #include "calls.h"
131 #include "emit-rtl.h"
132 #include "varasm.h"
133 #include "stmt.h"
134 #include "expr.h"
135 #include "cfgrtl.h"
136 #include "except.h"
137 #include "optabs.h"
138 #include "ira.h"
139 #include "rtl-error.h"
140 #include "params.h"
141 #include "lra.h"
142 #include "insn-attr.h"
143 #include "lra-int.h"
145 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
146 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
147 reload insns. */
148 static int bb_reload_num;
150 /* The current insn being processed and corresponding its single set
151 (NULL otherwise), its data (basic block, the insn data, the insn
152 static data, and the mode of each operand). */
153 static rtx_insn *curr_insn;
154 static rtx curr_insn_set;
155 static basic_block curr_bb;
156 static lra_insn_recog_data_t curr_id;
157 static struct lra_static_insn_data *curr_static_id;
158 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
159 /* Mode of the register substituted by its equivalence with VOIDmode
160 (e.g. constant) and whose subreg is given operand of the current
161 insn. VOIDmode in all other cases. */
162 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
166 /* Start numbers for new registers and insns at the current constraints
167 pass start. */
168 static int new_regno_start;
169 static int new_insn_uid_start;
171 /* If LOC is nonnull, strip any outer subreg from it. */
172 static inline rtx *
173 strip_subreg (rtx *loc)
175 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
178 /* Return hard regno of REGNO or if it is was not assigned to a hard
179 register, use a hard register from its allocno class. */
180 static int
181 get_try_hard_regno (int regno)
183 int hard_regno;
184 enum reg_class rclass;
186 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
187 hard_regno = lra_get_regno_hard_regno (regno);
188 if (hard_regno >= 0)
189 return hard_regno;
190 rclass = lra_get_allocno_class (regno);
191 if (rclass == NO_REGS)
192 return -1;
193 return ira_class_hard_regs[rclass][0];
196 /* Return final hard regno (plus offset) which will be after
197 elimination. We do this for matching constraints because the final
198 hard regno could have a different class. */
199 static int
200 get_final_hard_regno (int hard_regno, int offset)
202 if (hard_regno < 0)
203 return hard_regno;
204 hard_regno = lra_get_elimination_hard_regno (hard_regno);
205 return hard_regno + offset;
208 /* Return hard regno of X after removing subreg and making
209 elimination. If X is not a register or subreg of register, return
210 -1. For pseudo use its assignment. */
211 static int
212 get_hard_regno (rtx x)
214 rtx reg;
215 int offset, hard_regno;
217 reg = x;
218 if (GET_CODE (x) == SUBREG)
219 reg = SUBREG_REG (x);
220 if (! REG_P (reg))
221 return -1;
222 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
223 hard_regno = lra_get_regno_hard_regno (hard_regno);
224 if (hard_regno < 0)
225 return -1;
226 offset = 0;
227 if (GET_CODE (x) == SUBREG)
228 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
229 SUBREG_BYTE (x), GET_MODE (x));
230 return get_final_hard_regno (hard_regno, offset);
233 /* If REGNO is a hard register or has been allocated a hard register,
234 return the class of that register. If REGNO is a reload pseudo
235 created by the current constraints pass, return its allocno class.
236 Return NO_REGS otherwise. */
237 static enum reg_class
238 get_reg_class (int regno)
240 int hard_regno;
242 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
243 hard_regno = lra_get_regno_hard_regno (regno);
244 if (hard_regno >= 0)
246 hard_regno = get_final_hard_regno (hard_regno, 0);
247 return REGNO_REG_CLASS (hard_regno);
249 if (regno >= new_regno_start)
250 return lra_get_allocno_class (regno);
251 return NO_REGS;
254 /* Return true if REG satisfies (or will satisfy) reg class constraint
255 CL. Use elimination first if REG is a hard register. If REG is a
256 reload pseudo created by this constraints pass, assume that it will
257 be allocated a hard register from its allocno class, but allow that
258 class to be narrowed to CL if it is currently a superset of CL.
260 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
261 REGNO (reg), or NO_REGS if no change in its class was needed. */
262 static bool
263 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
265 enum reg_class rclass, common_class;
266 machine_mode reg_mode;
267 int class_size, hard_regno, nregs, i, j;
268 int regno = REGNO (reg);
270 if (new_class != NULL)
271 *new_class = NO_REGS;
272 if (regno < FIRST_PSEUDO_REGISTER)
274 rtx final_reg = reg;
275 rtx *final_loc = &final_reg;
277 lra_eliminate_reg_if_possible (final_loc);
278 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
280 reg_mode = GET_MODE (reg);
281 rclass = get_reg_class (regno);
282 if (regno < new_regno_start
283 /* Do not allow the constraints for reload instructions to
284 influence the classes of new pseudos. These reloads are
285 typically moves that have many alternatives, and restricting
286 reload pseudos for one alternative may lead to situations
287 where other reload pseudos are no longer allocatable. */
288 || (INSN_UID (curr_insn) >= new_insn_uid_start
289 && curr_insn_set != NULL
290 && ((OBJECT_P (SET_SRC (curr_insn_set))
291 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
292 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
293 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
294 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
295 /* When we don't know what class will be used finally for reload
296 pseudos, we use ALL_REGS. */
297 return ((regno >= new_regno_start && rclass == ALL_REGS)
298 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
299 && ! hard_reg_set_subset_p (reg_class_contents[cl],
300 lra_no_alloc_regs)));
301 else
303 common_class = ira_reg_class_subset[rclass][cl];
304 if (new_class != NULL)
305 *new_class = common_class;
306 if (hard_reg_set_subset_p (reg_class_contents[common_class],
307 lra_no_alloc_regs))
308 return false;
309 /* Check that there are enough allocatable regs. */
310 class_size = ira_class_hard_regs_num[common_class];
311 for (i = 0; i < class_size; i++)
313 hard_regno = ira_class_hard_regs[common_class][i];
314 nregs = hard_regno_nregs[hard_regno][reg_mode];
315 if (nregs == 1)
316 return true;
317 for (j = 0; j < nregs; j++)
318 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
319 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
320 hard_regno + j))
321 break;
322 if (j >= nregs)
323 return true;
325 return false;
329 /* Return true if REGNO satisfies a memory constraint. */
330 static bool
331 in_mem_p (int regno)
333 return get_reg_class (regno) == NO_REGS;
336 /* Return 1 if ADDR is a valid memory address for mode MODE in address
337 space AS, and check that each pseudo has the proper kind of hard
338 reg. */
339 static int
340 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
341 rtx addr, addr_space_t as)
343 #ifdef GO_IF_LEGITIMATE_ADDRESS
344 lra_assert (ADDR_SPACE_GENERIC_P (as));
345 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
346 return 0;
348 win:
349 return 1;
350 #else
351 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
352 #endif
355 namespace {
356 /* Temporarily eliminates registers in an address (for the lifetime of
357 the object). */
358 class address_eliminator {
359 public:
360 address_eliminator (struct address_info *ad);
361 ~address_eliminator ();
363 private:
364 struct address_info *m_ad;
365 rtx *m_base_loc;
366 rtx m_base_reg;
367 rtx *m_index_loc;
368 rtx m_index_reg;
372 address_eliminator::address_eliminator (struct address_info *ad)
373 : m_ad (ad),
374 m_base_loc (strip_subreg (ad->base_term)),
375 m_base_reg (NULL_RTX),
376 m_index_loc (strip_subreg (ad->index_term)),
377 m_index_reg (NULL_RTX)
379 if (m_base_loc != NULL)
381 m_base_reg = *m_base_loc;
382 lra_eliminate_reg_if_possible (m_base_loc);
383 if (m_ad->base_term2 != NULL)
384 *m_ad->base_term2 = *m_ad->base_term;
386 if (m_index_loc != NULL)
388 m_index_reg = *m_index_loc;
389 lra_eliminate_reg_if_possible (m_index_loc);
393 address_eliminator::~address_eliminator ()
395 if (m_base_loc && *m_base_loc != m_base_reg)
397 *m_base_loc = m_base_reg;
398 if (m_ad->base_term2 != NULL)
399 *m_ad->base_term2 = *m_ad->base_term;
401 if (m_index_loc && *m_index_loc != m_index_reg)
402 *m_index_loc = m_index_reg;
405 /* Return true if the eliminated form of AD is a legitimate target address. */
406 static bool
407 valid_address_p (struct address_info *ad)
409 address_eliminator eliminator (ad);
410 return valid_address_p (ad->mode, *ad->outer, ad->as);
413 /* Return true if the eliminated form of memory reference OP satisfies
414 extra memory constraint CONSTRAINT. */
415 static bool
416 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
418 struct address_info ad;
420 decompose_mem_address (&ad, op);
421 address_eliminator eliminator (&ad);
422 return constraint_satisfied_p (op, constraint);
425 /* Return true if the eliminated form of address AD satisfies extra
426 address constraint CONSTRAINT. */
427 static bool
428 satisfies_address_constraint_p (struct address_info *ad,
429 enum constraint_num constraint)
431 address_eliminator eliminator (ad);
432 return constraint_satisfied_p (*ad->outer, constraint);
435 /* Return true if the eliminated form of address OP satisfies extra
436 address constraint CONSTRAINT. */
437 static bool
438 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
440 struct address_info ad;
442 decompose_lea_address (&ad, &op);
443 return satisfies_address_constraint_p (&ad, constraint);
446 /* Initiate equivalences for LRA. As we keep original equivalences
447 before any elimination, we need to make copies otherwise any change
448 in insns might change the equivalences. */
449 void
450 lra_init_equiv (void)
452 ira_expand_reg_equiv ();
453 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
455 rtx res;
457 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
458 ira_reg_equiv[i].memory = copy_rtx (res);
459 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
460 ira_reg_equiv[i].invariant = copy_rtx (res);
464 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
466 /* Update equivalence for REGNO. We need to this as the equivalence
467 might contain other pseudos which are changed by their
468 equivalences. */
469 static void
470 update_equiv (int regno)
472 rtx x;
474 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
475 ira_reg_equiv[regno].memory
476 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
477 NULL_RTX);
478 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
479 ira_reg_equiv[regno].invariant
480 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
481 NULL_RTX);
484 /* If we have decided to substitute X with another value, return that
485 value, otherwise return X. */
486 static rtx
487 get_equiv (rtx x)
489 int regno;
490 rtx res;
492 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
493 || ! ira_reg_equiv[regno].defined_p
494 || ! ira_reg_equiv[regno].profitable_p
495 || lra_get_regno_hard_regno (regno) >= 0)
496 return x;
497 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
499 if (targetm.cannot_substitute_mem_equiv_p (res))
500 return x;
501 return res;
503 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
504 return res;
505 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
506 return res;
507 gcc_unreachable ();
510 /* If we have decided to substitute X with the equivalent value,
511 return that value after elimination for INSN, otherwise return
512 X. */
513 static rtx
514 get_equiv_with_elimination (rtx x, rtx_insn *insn)
516 rtx res = get_equiv (x);
518 if (x == res || CONSTANT_P (res))
519 return res;
520 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
521 false, false, 0, true);
524 /* Set up curr_operand_mode. */
525 static void
526 init_curr_operand_mode (void)
528 int nop = curr_static_id->n_operands;
529 for (int i = 0; i < nop; i++)
531 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
532 if (mode == VOIDmode)
534 /* The .md mode for address operands is the mode of the
535 addressed value rather than the mode of the address itself. */
536 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
537 mode = Pmode;
538 else
539 mode = curr_static_id->operand[i].mode;
541 curr_operand_mode[i] = mode;
547 /* The page contains code to reuse input reloads. */
549 /* Structure describes input reload of the current insns. */
550 struct input_reload
552 /* Reloaded value. */
553 rtx input;
554 /* Reload pseudo used. */
555 rtx reg;
558 /* The number of elements in the following array. */
559 static int curr_insn_input_reloads_num;
560 /* Array containing info about input reloads. It is used to find the
561 same input reload and reuse the reload pseudo in this case. */
562 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
564 /* Initiate data concerning reuse of input reloads for the current
565 insn. */
566 static void
567 init_curr_insn_input_reloads (void)
569 curr_insn_input_reloads_num = 0;
572 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
573 created input reload pseudo (only if TYPE is not OP_OUT). Don't
574 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
575 wrapped up in SUBREG. The result pseudo is returned through
576 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
577 reused the already created input reload pseudo. Use TITLE to
578 describe new registers for debug purposes. */
579 static bool
580 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
581 enum reg_class rclass, bool in_subreg_p,
582 const char *title, rtx *result_reg)
584 int i, regno;
585 enum reg_class new_class;
587 if (type == OP_OUT)
589 *result_reg
590 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
591 return true;
593 /* Prevent reuse value of expression with side effects,
594 e.g. volatile memory. */
595 if (! side_effects_p (original))
596 for (i = 0; i < curr_insn_input_reloads_num; i++)
597 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
598 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
600 rtx reg = curr_insn_input_reloads[i].reg;
601 regno = REGNO (reg);
602 /* If input is equal to original and both are VOIDmode,
603 GET_MODE (reg) might be still different from mode.
604 Ensure we don't return *result_reg with wrong mode. */
605 if (GET_MODE (reg) != mode)
607 if (in_subreg_p)
608 continue;
609 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
610 continue;
611 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
612 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
613 continue;
615 *result_reg = reg;
616 if (lra_dump_file != NULL)
618 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
619 dump_value_slim (lra_dump_file, original, 1);
621 if (new_class != lra_get_allocno_class (regno))
622 lra_change_class (regno, new_class, ", change to", false);
623 if (lra_dump_file != NULL)
624 fprintf (lra_dump_file, "\n");
625 return false;
627 *result_reg = lra_create_new_reg (mode, original, rclass, title);
628 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
629 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
630 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
631 return true;
636 /* The page contains code to extract memory address parts. */
638 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
639 static inline bool
640 ok_for_index_p_nonstrict (rtx reg)
642 unsigned regno = REGNO (reg);
644 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
647 /* A version of regno_ok_for_base_p for use here, when all pseudos
648 should count as OK. Arguments as for regno_ok_for_base_p. */
649 static inline bool
650 ok_for_base_p_nonstrict (rtx reg, machine_mode mode, addr_space_t as,
651 enum rtx_code outer_code, enum rtx_code index_code)
653 unsigned regno = REGNO (reg);
655 if (regno >= FIRST_PSEUDO_REGISTER)
656 return true;
657 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
662 /* The page contains major code to choose the current insn alternative
663 and generate reloads for it. */
665 /* Return the offset from REGNO of the least significant register
666 in (reg:MODE REGNO).
668 This function is used to tell whether two registers satisfy
669 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
671 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
672 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
674 lra_constraint_offset (int regno, machine_mode mode)
676 lra_assert (regno < FIRST_PSEUDO_REGISTER);
677 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
678 && SCALAR_INT_MODE_P (mode))
679 return hard_regno_nregs[regno][mode] - 1;
680 return 0;
683 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
684 if they are the same hard reg, and has special hacks for
685 auto-increment and auto-decrement. This is specifically intended for
686 process_alt_operands to use in determining whether two operands
687 match. X is the operand whose number is the lower of the two.
689 It is supposed that X is the output operand and Y is the input
690 operand. Y_HARD_REGNO is the final hard regno of register Y or
691 register in subreg Y as we know it now. Otherwise, it is a
692 negative value. */
693 static bool
694 operands_match_p (rtx x, rtx y, int y_hard_regno)
696 int i;
697 RTX_CODE code = GET_CODE (x);
698 const char *fmt;
700 if (x == y)
701 return true;
702 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
703 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
705 int j;
707 i = get_hard_regno (x);
708 if (i < 0)
709 goto slow;
711 if ((j = y_hard_regno) < 0)
712 goto slow;
714 i += lra_constraint_offset (i, GET_MODE (x));
715 j += lra_constraint_offset (j, GET_MODE (y));
717 return i == j;
720 /* If two operands must match, because they are really a single
721 operand of an assembler insn, then two post-increments are invalid
722 because the assembler insn would increment only once. On the
723 other hand, a post-increment matches ordinary indexing if the
724 post-increment is the output operand. */
725 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
726 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
728 /* Two pre-increments are invalid because the assembler insn would
729 increment only once. On the other hand, a pre-increment matches
730 ordinary indexing if the pre-increment is the input operand. */
731 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
732 || GET_CODE (y) == PRE_MODIFY)
733 return operands_match_p (x, XEXP (y, 0), -1);
735 slow:
737 if (code == REG && REG_P (y))
738 return REGNO (x) == REGNO (y);
740 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
741 && x == SUBREG_REG (y))
742 return true;
743 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
744 && SUBREG_REG (x) == y)
745 return true;
747 /* Now we have disposed of all the cases in which different rtx
748 codes can match. */
749 if (code != GET_CODE (y))
750 return false;
752 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
753 if (GET_MODE (x) != GET_MODE (y))
754 return false;
756 switch (code)
758 CASE_CONST_UNIQUE:
759 return false;
761 case LABEL_REF:
762 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
763 case SYMBOL_REF:
764 return XSTR (x, 0) == XSTR (y, 0);
766 default:
767 break;
770 /* Compare the elements. If any pair of corresponding elements fail
771 to match, return false for the whole things. */
773 fmt = GET_RTX_FORMAT (code);
774 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
776 int val, j;
777 switch (fmt[i])
779 case 'w':
780 if (XWINT (x, i) != XWINT (y, i))
781 return false;
782 break;
784 case 'i':
785 if (XINT (x, i) != XINT (y, i))
786 return false;
787 break;
789 case 'e':
790 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
791 if (val == 0)
792 return false;
793 break;
795 case '0':
796 break;
798 case 'E':
799 if (XVECLEN (x, i) != XVECLEN (y, i))
800 return false;
801 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
803 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
804 if (val == 0)
805 return false;
807 break;
809 /* It is believed that rtx's at this level will never
810 contain anything but integers and other rtx's, except for
811 within LABEL_REFs and SYMBOL_REFs. */
812 default:
813 gcc_unreachable ();
816 return true;
819 /* True if X is a constant that can be forced into the constant pool.
820 MODE is the mode of the operand, or VOIDmode if not known. */
821 #define CONST_POOL_OK_P(MODE, X) \
822 ((MODE) != VOIDmode \
823 && CONSTANT_P (X) \
824 && GET_CODE (X) != HIGH \
825 && !targetm.cannot_force_const_mem (MODE, X))
827 /* True if C is a non-empty register class that has too few registers
828 to be safely used as a reload target class. */
829 #define SMALL_REGISTER_CLASS_P(C) \
830 (ira_class_hard_regs_num [(C)] == 1 \
831 || (ira_class_hard_regs_num [(C)] >= 1 \
832 && targetm.class_likely_spilled_p (C)))
834 /* If REG is a reload pseudo, try to make its class satisfying CL. */
835 static void
836 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
838 enum reg_class rclass;
840 /* Do not make more accurate class from reloads generated. They are
841 mostly moves with a lot of constraints. Making more accurate
842 class may results in very narrow class and impossibility of find
843 registers for several reloads of one insn. */
844 if (INSN_UID (curr_insn) >= new_insn_uid_start)
845 return;
846 if (GET_CODE (reg) == SUBREG)
847 reg = SUBREG_REG (reg);
848 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
849 return;
850 if (in_class_p (reg, cl, &rclass) && rclass != cl)
851 lra_change_class (REGNO (reg), rclass, " Change to", true);
854 /* Generate reloads for matching OUT and INS (array of input operand
855 numbers with end marker -1) with reg class GOAL_CLASS. Add input
856 and output reloads correspondingly to the lists *BEFORE and *AFTER.
857 OUT might be negative. In this case we generate input reloads for
858 matched input operands INS. EARLY_CLOBBER_P is a flag that the
859 output operand is early clobbered for chosen alternative. */
860 static void
861 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
862 rtx_insn **before, rtx_insn **after, bool early_clobber_p)
864 int i, in;
865 rtx new_in_reg, new_out_reg, reg;
866 machine_mode inmode, outmode;
867 rtx in_rtx = *curr_id->operand_loc[ins[0]];
868 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
870 inmode = curr_operand_mode[ins[0]];
871 outmode = out < 0 ? inmode : curr_operand_mode[out];
872 push_to_sequence (*before);
873 if (inmode != outmode)
875 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
877 reg = new_in_reg
878 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
879 goal_class, "");
880 if (SCALAR_INT_MODE_P (inmode))
881 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
882 else
883 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
884 LRA_SUBREG_P (new_out_reg) = 1;
885 /* If the input reg is dying here, we can use the same hard
886 register for REG and IN_RTX. We do it only for original
887 pseudos as reload pseudos can die although original
888 pseudos still live where reload pseudos dies. */
889 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
890 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
891 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
893 else
895 reg = new_out_reg
896 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
897 goal_class, "");
898 if (SCALAR_INT_MODE_P (outmode))
899 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
900 else
901 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
902 /* NEW_IN_REG is non-paradoxical subreg. We don't want
903 NEW_OUT_REG living above. We add clobber clause for
904 this. This is just a temporary clobber. We can remove
905 it at the end of LRA work. */
906 rtx_insn *clobber = emit_clobber (new_out_reg);
907 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
908 LRA_SUBREG_P (new_in_reg) = 1;
909 if (GET_CODE (in_rtx) == SUBREG)
911 rtx subreg_reg = SUBREG_REG (in_rtx);
913 /* If SUBREG_REG is dying here and sub-registers IN_RTX
914 and NEW_IN_REG are similar, we can use the same hard
915 register for REG and SUBREG_REG. */
916 if (REG_P (subreg_reg)
917 && (int) REGNO (subreg_reg) < lra_new_regno_start
918 && GET_MODE (subreg_reg) == outmode
919 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
920 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
921 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
925 else
927 /* Pseudos have values -- see comments for lra_reg_info.
928 Different pseudos with the same value do not conflict even if
929 they live in the same place. When we create a pseudo we
930 assign value of original pseudo (if any) from which we
931 created the new pseudo. If we create the pseudo from the
932 input pseudo, the new pseudo will have no conflict with the
933 input pseudo which is wrong when the input pseudo lives after
934 the insn and as the new pseudo value is changed by the insn
935 output. Therefore we create the new pseudo from the output
936 except the case when we have single matched dying input
937 pseudo.
939 We cannot reuse the current output register because we might
940 have a situation like "a <- a op b", where the constraints
941 force the second input operand ("b") to match the output
942 operand ("a"). "b" must then be copied into a new register
943 so that it doesn't clobber the current value of "a".
945 We can not use the same value if the output pseudo is
946 early clobbered or the input pseudo is mentioned in the
947 output, e.g. as an address part in memory, because
948 output reload will actually extend the pseudo liveness.
949 We don't care about eliminable hard regs here as we are
950 interesting only in pseudos. */
952 new_in_reg = new_out_reg
953 = (! early_clobber_p && ins[1] < 0 && REG_P (in_rtx)
954 && (int) REGNO (in_rtx) < lra_new_regno_start
955 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
956 && (out < 0 || regno_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
957 ? lra_create_new_reg (inmode, in_rtx, goal_class, "")
958 : lra_create_new_reg_with_unique_value (outmode, out_rtx,
959 goal_class, ""));
961 /* In operand can be got from transformations before processing insn
962 constraints. One example of such transformations is subreg
963 reloading (see function simplify_operand_subreg). The new
964 pseudos created by the transformations might have inaccurate
965 class (ALL_REGS) and we should make their classes more
966 accurate. */
967 narrow_reload_pseudo_class (in_rtx, goal_class);
968 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
969 *before = get_insns ();
970 end_sequence ();
971 for (i = 0; (in = ins[i]) >= 0; i++)
973 lra_assert
974 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
975 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
976 *curr_id->operand_loc[in] = new_in_reg;
978 lra_update_dups (curr_id, ins);
979 if (out < 0)
980 return;
981 /* See a comment for the input operand above. */
982 narrow_reload_pseudo_class (out_rtx, goal_class);
983 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
985 start_sequence ();
986 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
987 emit_insn (*after);
988 *after = get_insns ();
989 end_sequence ();
991 *curr_id->operand_loc[out] = new_out_reg;
992 lra_update_dup (curr_id, out);
995 /* Return register class which is union of all reg classes in insn
996 constraint alternative string starting with P. */
997 static enum reg_class
998 reg_class_from_constraints (const char *p)
1000 int c, len;
1001 enum reg_class op_class = NO_REGS;
1004 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1006 case '#':
1007 case ',':
1008 return op_class;
1010 case 'g':
1011 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1012 break;
1014 default:
1015 enum constraint_num cn = lookup_constraint (p);
1016 enum reg_class cl = reg_class_for_constraint (cn);
1017 if (cl == NO_REGS)
1019 if (insn_extra_address_constraint (cn))
1020 op_class
1021 = (reg_class_subunion
1022 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1023 ADDRESS, SCRATCH)]);
1024 break;
1027 op_class = reg_class_subunion[op_class][cl];
1028 break;
1030 while ((p += len), c);
1031 return op_class;
1034 /* If OP is a register, return the class of the register as per
1035 get_reg_class, otherwise return NO_REGS. */
1036 static inline enum reg_class
1037 get_op_class (rtx op)
1039 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1042 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1043 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1044 SUBREG for VAL to make them equal. */
1045 static rtx_insn *
1046 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1048 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1050 /* Usually size of mem_pseudo is greater than val size but in
1051 rare cases it can be less as it can be defined by target
1052 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1053 if (! MEM_P (val))
1055 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1056 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1058 LRA_SUBREG_P (val) = 1;
1060 else
1062 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1063 LRA_SUBREG_P (mem_pseudo) = 1;
1066 return to_p ? gen_move_insn (mem_pseudo, val)
1067 : gen_move_insn (val, mem_pseudo);
1070 /* Process a special case insn (register move), return true if we
1071 don't need to process it anymore. INSN should be a single set
1072 insn. Set up that RTL was changed through CHANGE_P and macro
1073 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1074 SEC_MEM_P. */
1075 static bool
1076 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1078 int sregno, dregno;
1079 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1080 rtx_insn *before;
1081 enum reg_class dclass, sclass, secondary_class;
1082 secondary_reload_info sri;
1084 lra_assert (curr_insn_set != NULL_RTX);
1085 dreg = dest = SET_DEST (curr_insn_set);
1086 sreg = src = SET_SRC (curr_insn_set);
1087 if (GET_CODE (dest) == SUBREG)
1088 dreg = SUBREG_REG (dest);
1089 if (GET_CODE (src) == SUBREG)
1090 sreg = SUBREG_REG (src);
1091 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1092 return false;
1093 sclass = dclass = NO_REGS;
1094 if (REG_P (dreg))
1095 dclass = get_reg_class (REGNO (dreg));
1096 if (dclass == ALL_REGS)
1097 /* ALL_REGS is used for new pseudos created by transformations
1098 like reload of SUBREG_REG (see function
1099 simplify_operand_subreg). We don't know their class yet. We
1100 should figure out the class from processing the insn
1101 constraints not in this fast path function. Even if ALL_REGS
1102 were a right class for the pseudo, secondary_... hooks usually
1103 are not define for ALL_REGS. */
1104 return false;
1105 if (REG_P (sreg))
1106 sclass = get_reg_class (REGNO (sreg));
1107 if (sclass == ALL_REGS)
1108 /* See comments above. */
1109 return false;
1110 if (sclass == NO_REGS && dclass == NO_REGS)
1111 return false;
1112 #ifdef SECONDARY_MEMORY_NEEDED
1113 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1114 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1115 && ((sclass != NO_REGS && dclass != NO_REGS)
1116 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1117 #endif
1120 *sec_mem_p = true;
1121 return false;
1123 #endif
1124 if (! REG_P (dreg) || ! REG_P (sreg))
1125 return false;
1126 sri.prev_sri = NULL;
1127 sri.icode = CODE_FOR_nothing;
1128 sri.extra_cost = 0;
1129 secondary_class = NO_REGS;
1130 /* Set up hard register for a reload pseudo for hook
1131 secondary_reload because some targets just ignore unassigned
1132 pseudos in the hook. */
1133 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1135 dregno = REGNO (dreg);
1136 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1138 else
1139 dregno = -1;
1140 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1142 sregno = REGNO (sreg);
1143 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1145 else
1146 sregno = -1;
1147 if (sclass != NO_REGS)
1148 secondary_class
1149 = (enum reg_class) targetm.secondary_reload (false, dest,
1150 (reg_class_t) sclass,
1151 GET_MODE (src), &sri);
1152 if (sclass == NO_REGS
1153 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1154 && dclass != NO_REGS))
1156 enum reg_class old_sclass = secondary_class;
1157 secondary_reload_info old_sri = sri;
1159 sri.prev_sri = NULL;
1160 sri.icode = CODE_FOR_nothing;
1161 sri.extra_cost = 0;
1162 secondary_class
1163 = (enum reg_class) targetm.secondary_reload (true, src,
1164 (reg_class_t) dclass,
1165 GET_MODE (src), &sri);
1166 /* Check the target hook consistency. */
1167 lra_assert
1168 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1169 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1170 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1172 if (sregno >= 0)
1173 reg_renumber [sregno] = -1;
1174 if (dregno >= 0)
1175 reg_renumber [dregno] = -1;
1176 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1177 return false;
1178 *change_p = true;
1179 new_reg = NULL_RTX;
1180 if (secondary_class != NO_REGS)
1181 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1182 secondary_class,
1183 "secondary");
1184 start_sequence ();
1185 if (sri.icode == CODE_FOR_nothing)
1186 lra_emit_move (new_reg, src);
1187 else
1189 enum reg_class scratch_class;
1191 scratch_class = (reg_class_from_constraints
1192 (insn_data[sri.icode].operand[2].constraint));
1193 scratch_reg = (lra_create_new_reg_with_unique_value
1194 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1195 scratch_class, "scratch"));
1196 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1197 src, scratch_reg));
1199 before = get_insns ();
1200 end_sequence ();
1201 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1202 if (new_reg != NULL_RTX)
1203 SET_SRC (curr_insn_set) = new_reg;
1204 else
1206 if (lra_dump_file != NULL)
1208 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1209 dump_insn_slim (lra_dump_file, curr_insn);
1211 lra_set_insn_deleted (curr_insn);
1212 return true;
1214 return false;
1217 /* The following data describe the result of process_alt_operands.
1218 The data are used in curr_insn_transform to generate reloads. */
1220 /* The chosen reg classes which should be used for the corresponding
1221 operands. */
1222 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1223 /* True if the operand should be the same as another operand and that
1224 other operand does not need a reload. */
1225 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1226 /* True if the operand does not need a reload. */
1227 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1228 /* True if the operand can be offsetable memory. */
1229 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1230 /* The number of an operand to which given operand can be matched to. */
1231 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1232 /* The number of elements in the following array. */
1233 static int goal_alt_dont_inherit_ops_num;
1234 /* Numbers of operands whose reload pseudos should not be inherited. */
1235 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1236 /* True if the insn commutative operands should be swapped. */
1237 static bool goal_alt_swapped;
1238 /* The chosen insn alternative. */
1239 static int goal_alt_number;
1241 /* The following five variables are used to choose the best insn
1242 alternative. They reflect final characteristics of the best
1243 alternative. */
1245 /* Number of necessary reloads and overall cost reflecting the
1246 previous value and other unpleasantness of the best alternative. */
1247 static int best_losers, best_overall;
1248 /* Overall number hard registers used for reloads. For example, on
1249 some targets we need 2 general registers to reload DFmode and only
1250 one floating point register. */
1251 static int best_reload_nregs;
1252 /* Overall number reflecting distances of previous reloading the same
1253 value. The distances are counted from the current BB start. It is
1254 used to improve inheritance chances. */
1255 static int best_reload_sum;
1257 /* True if the current insn should have no correspondingly input or
1258 output reloads. */
1259 static bool no_input_reloads_p, no_output_reloads_p;
1261 /* True if we swapped the commutative operands in the current
1262 insn. */
1263 static int curr_swapped;
1265 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1266 register of class CL. Add any input reloads to list BEFORE. AFTER
1267 is nonnull if *LOC is an automodified value; handle that case by
1268 adding the required output reloads to list AFTER. Return true if
1269 the RTL was changed.
1271 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1272 register. Return false if the address register is correct. */
1273 static bool
1274 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1275 enum reg_class cl)
1277 int regno;
1278 enum reg_class rclass, new_class;
1279 rtx reg;
1280 rtx new_reg;
1281 machine_mode mode;
1282 bool subreg_p, before_p = false;
1284 subreg_p = GET_CODE (*loc) == SUBREG;
1285 if (subreg_p)
1286 loc = &SUBREG_REG (*loc);
1287 reg = *loc;
1288 mode = GET_MODE (reg);
1289 if (! REG_P (reg))
1291 if (check_only_p)
1292 return true;
1293 /* Always reload memory in an address even if the target supports
1294 such addresses. */
1295 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1296 before_p = true;
1298 else
1300 regno = REGNO (reg);
1301 rclass = get_reg_class (regno);
1302 if (! check_only_p
1303 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1305 if (lra_dump_file != NULL)
1307 fprintf (lra_dump_file,
1308 "Changing pseudo %d in address of insn %u on equiv ",
1309 REGNO (reg), INSN_UID (curr_insn));
1310 dump_value_slim (lra_dump_file, *loc, 1);
1311 fprintf (lra_dump_file, "\n");
1313 *loc = copy_rtx (*loc);
1315 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1317 if (check_only_p)
1318 return true;
1319 reg = *loc;
1320 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1321 mode, reg, cl, subreg_p, "address", &new_reg))
1322 before_p = true;
1324 else if (new_class != NO_REGS && rclass != new_class)
1326 if (check_only_p)
1327 return true;
1328 lra_change_class (regno, new_class, " Change to", true);
1329 return false;
1331 else
1332 return false;
1334 if (before_p)
1336 push_to_sequence (*before);
1337 lra_emit_move (new_reg, reg);
1338 *before = get_insns ();
1339 end_sequence ();
1341 *loc = new_reg;
1342 if (after != NULL)
1344 start_sequence ();
1345 lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1346 emit_insn (*after);
1347 *after = get_insns ();
1348 end_sequence ();
1350 return true;
1353 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1354 the insn to be inserted before curr insn. AFTER returns the
1355 the insn to be inserted after curr insn. ORIGREG and NEWREG
1356 are the original reg and new reg for reload. */
1357 static void
1358 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1359 rtx newreg)
1361 if (before)
1363 push_to_sequence (*before);
1364 lra_emit_move (newreg, origreg);
1365 *before = get_insns ();
1366 end_sequence ();
1368 if (after)
1370 start_sequence ();
1371 lra_emit_move (origreg, newreg);
1372 emit_insn (*after);
1373 *after = get_insns ();
1374 end_sequence ();
1378 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1380 /* Make reloads for subreg in operand NOP with internal subreg mode
1381 REG_MODE, add new reloads for further processing. Return true if
1382 any change was done. */
1383 static bool
1384 simplify_operand_subreg (int nop, machine_mode reg_mode)
1386 int hard_regno;
1387 rtx_insn *before, *after;
1388 machine_mode mode, innermode;
1389 rtx reg, new_reg;
1390 rtx operand = *curr_id->operand_loc[nop];
1391 enum reg_class regclass;
1392 enum op_type type;
1394 before = after = NULL;
1396 if (GET_CODE (operand) != SUBREG)
1397 return false;
1399 mode = GET_MODE (operand);
1400 reg = SUBREG_REG (operand);
1401 innermode = GET_MODE (reg);
1402 type = curr_static_id->operand[nop].type;
1403 /* If we change address for paradoxical subreg of memory, the
1404 address might violate the necessary alignment or the access might
1405 be slow. So take this into consideration. We should not worry
1406 about access beyond allocated memory for paradoxical memory
1407 subregs as we don't substitute such equiv memory (see processing
1408 equivalences in function lra_constraints) and because for spilled
1409 pseudos we allocate stack memory enough for the biggest
1410 corresponding paradoxical subreg. */
1411 if (MEM_P (reg)
1412 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1413 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1415 rtx subst, old = *curr_id->operand_loc[nop];
1417 alter_subreg (curr_id->operand_loc[nop], false);
1418 subst = *curr_id->operand_loc[nop];
1419 lra_assert (MEM_P (subst));
1420 if (! valid_address_p (innermode, XEXP (reg, 0),
1421 MEM_ADDR_SPACE (reg))
1422 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1423 MEM_ADDR_SPACE (subst)))
1424 return true;
1425 /* If the address was valid and became invalid, prefer to reload
1426 the memory. Typical case is when the index scale should
1427 correspond the memory. */
1428 *curr_id->operand_loc[nop] = old;
1430 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1432 alter_subreg (curr_id->operand_loc[nop], false);
1433 return true;
1435 else if (CONSTANT_P (reg))
1437 /* Try to simplify subreg of constant. It is usually result of
1438 equivalence substitution. */
1439 if (innermode == VOIDmode
1440 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1441 innermode = curr_static_id->operand[nop].mode;
1442 if ((new_reg = simplify_subreg (mode, reg, innermode,
1443 SUBREG_BYTE (operand))) != NULL_RTX)
1445 *curr_id->operand_loc[nop] = new_reg;
1446 return true;
1449 /* Put constant into memory when we have mixed modes. It generates
1450 a better code in most cases as it does not need a secondary
1451 reload memory. It also prevents LRA looping when LRA is using
1452 secondary reload memory again and again. */
1453 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1454 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1456 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1457 alter_subreg (curr_id->operand_loc[nop], false);
1458 return true;
1460 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1461 if there may be a problem accessing OPERAND in the outer
1462 mode. */
1463 if ((REG_P (reg)
1464 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1465 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1466 /* Don't reload paradoxical subregs because we could be looping
1467 having repeatedly final regno out of hard regs range. */
1468 && (hard_regno_nregs[hard_regno][innermode]
1469 >= hard_regno_nregs[hard_regno][mode])
1470 && simplify_subreg_regno (hard_regno, innermode,
1471 SUBREG_BYTE (operand), mode) < 0
1472 /* Don't reload subreg for matching reload. It is actually
1473 valid subreg in LRA. */
1474 && ! LRA_SUBREG_P (operand))
1475 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1477 enum reg_class rclass;
1479 if (REG_P (reg))
1480 /* There is a big probability that we will get the same class
1481 for the new pseudo and we will get the same insn which
1482 means infinite looping. So spill the new pseudo. */
1483 rclass = NO_REGS;
1484 else
1485 /* The class will be defined later in curr_insn_transform. */
1486 rclass
1487 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1489 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1490 rclass, TRUE, "subreg reg", &new_reg))
1492 bool insert_before, insert_after;
1493 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1495 insert_before = (type != OP_OUT
1496 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1497 insert_after = (type != OP_IN);
1498 insert_move_for_subreg (insert_before ? &before : NULL,
1499 insert_after ? &after : NULL,
1500 reg, new_reg);
1502 SUBREG_REG (operand) = new_reg;
1503 lra_process_new_insns (curr_insn, before, after,
1504 "Inserting subreg reload");
1505 return true;
1507 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1508 IRA allocates hardreg to the inner pseudo reg according to its mode
1509 instead of the outermode, so the size of the hardreg may not be enough
1510 to contain the outermode operand, in that case we may need to insert
1511 reload for the reg. For the following two types of paradoxical subreg,
1512 we need to insert reload:
1513 1. If the op_type is OP_IN, and the hardreg could not be paired with
1514 other hardreg to contain the outermode operand
1515 (checked by in_hard_reg_set_p), we need to insert the reload.
1516 2. If the op_type is OP_OUT or OP_INOUT.
1518 Here is a paradoxical subreg example showing how the reload is generated:
1520 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1521 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1523 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1524 here, if reg107 is assigned to hardreg R15, because R15 is the last
1525 hardreg, compiler cannot find another hardreg to pair with R15 to
1526 contain TImode data. So we insert a TImode reload reg180 for it.
1527 After reload is inserted:
1529 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1530 (reg:DI 107 [ __comp ])) -1
1531 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1532 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1534 Two reload hard registers will be allocated to reg180 to save TImode data
1535 in LRA_assign. */
1536 else if (REG_P (reg)
1537 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1538 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1539 && (hard_regno_nregs[hard_regno][innermode]
1540 < hard_regno_nregs[hard_regno][mode])
1541 && (regclass = lra_get_allocno_class (REGNO (reg)))
1542 && (type != OP_IN
1543 || !in_hard_reg_set_p (reg_class_contents[regclass],
1544 mode, hard_regno)))
1546 /* The class will be defined later in curr_insn_transform. */
1547 enum reg_class rclass
1548 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1550 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1551 rclass, TRUE, "paradoxical subreg", &new_reg))
1553 rtx subreg;
1554 bool insert_before, insert_after;
1556 PUT_MODE (new_reg, mode);
1557 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1558 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1560 insert_before = (type != OP_OUT);
1561 insert_after = (type != OP_IN);
1562 insert_move_for_subreg (insert_before ? &before : NULL,
1563 insert_after ? &after : NULL,
1564 reg, subreg);
1566 SUBREG_REG (operand) = new_reg;
1567 lra_process_new_insns (curr_insn, before, after,
1568 "Inserting paradoxical subreg reload");
1569 return true;
1571 return false;
1574 /* Return TRUE if X refers for a hard register from SET. */
1575 static bool
1576 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1578 int i, j, x_hard_regno;
1579 machine_mode mode;
1580 const char *fmt;
1581 enum rtx_code code;
1583 if (x == NULL_RTX)
1584 return false;
1585 code = GET_CODE (x);
1586 mode = GET_MODE (x);
1587 if (code == SUBREG)
1589 x = SUBREG_REG (x);
1590 code = GET_CODE (x);
1591 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1592 mode = GET_MODE (x);
1595 if (REG_P (x))
1597 x_hard_regno = get_hard_regno (x);
1598 return (x_hard_regno >= 0
1599 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1601 if (MEM_P (x))
1603 struct address_info ad;
1605 decompose_mem_address (&ad, x);
1606 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1607 return true;
1608 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1609 return true;
1611 fmt = GET_RTX_FORMAT (code);
1612 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1614 if (fmt[i] == 'e')
1616 if (uses_hard_regs_p (XEXP (x, i), set))
1617 return true;
1619 else if (fmt[i] == 'E')
1621 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1622 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1623 return true;
1626 return false;
1629 /* Return true if OP is a spilled pseudo. */
1630 static inline bool
1631 spilled_pseudo_p (rtx op)
1633 return (REG_P (op)
1634 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1637 /* Return true if X is a general constant. */
1638 static inline bool
1639 general_constant_p (rtx x)
1641 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1644 static bool
1645 reg_in_class_p (rtx reg, enum reg_class cl)
1647 if (cl == NO_REGS)
1648 return get_reg_class (REGNO (reg)) == NO_REGS;
1649 return in_class_p (reg, cl, NULL);
1652 /* Return true if SET of RCLASS contains no hard regs which can be
1653 used in MODE. */
1654 static bool
1655 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1656 HARD_REG_SET &set,
1657 enum machine_mode mode)
1659 HARD_REG_SET temp;
1661 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1662 COPY_HARD_REG_SET (temp, set);
1663 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1664 return (hard_reg_set_subset_p
1665 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1668 /* Major function to choose the current insn alternative and what
1669 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1670 negative we should consider only this alternative. Return false if
1671 we can not choose the alternative or find how to reload the
1672 operands. */
1673 static bool
1674 process_alt_operands (int only_alternative)
1676 bool ok_p = false;
1677 int nop, overall, nalt;
1678 int n_alternatives = curr_static_id->n_alternatives;
1679 int n_operands = curr_static_id->n_operands;
1680 /* LOSERS counts the operands that don't fit this alternative and
1681 would require loading. */
1682 int losers;
1683 /* REJECT is a count of how undesirable this alternative says it is
1684 if any reloading is required. If the alternative matches exactly
1685 then REJECT is ignored, but otherwise it gets this much counted
1686 against it in addition to the reloading needed. */
1687 int reject;
1688 int op_reject;
1689 /* The number of elements in the following array. */
1690 int early_clobbered_regs_num;
1691 /* Numbers of operands which are early clobber registers. */
1692 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1693 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1694 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1695 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1696 bool curr_alt_win[MAX_RECOG_OPERANDS];
1697 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1698 int curr_alt_matches[MAX_RECOG_OPERANDS];
1699 /* The number of elements in the following array. */
1700 int curr_alt_dont_inherit_ops_num;
1701 /* Numbers of operands whose reload pseudos should not be inherited. */
1702 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1703 rtx op;
1704 /* The register when the operand is a subreg of register, otherwise the
1705 operand itself. */
1706 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1707 /* The register if the operand is a register or subreg of register,
1708 otherwise NULL. */
1709 rtx operand_reg[MAX_RECOG_OPERANDS];
1710 int hard_regno[MAX_RECOG_OPERANDS];
1711 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1712 int reload_nregs, reload_sum;
1713 bool costly_p;
1714 enum reg_class cl;
1716 /* Calculate some data common for all alternatives to speed up the
1717 function. */
1718 for (nop = 0; nop < n_operands; nop++)
1720 rtx reg;
1722 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1723 /* The real hard regno of the operand after the allocation. */
1724 hard_regno[nop] = get_hard_regno (op);
1726 operand_reg[nop] = reg = op;
1727 biggest_mode[nop] = GET_MODE (op);
1728 if (GET_CODE (op) == SUBREG)
1730 operand_reg[nop] = reg = SUBREG_REG (op);
1731 if (GET_MODE_SIZE (biggest_mode[nop])
1732 < GET_MODE_SIZE (GET_MODE (reg)))
1733 biggest_mode[nop] = GET_MODE (reg);
1735 if (! REG_P (reg))
1736 operand_reg[nop] = NULL_RTX;
1737 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1738 || ((int) REGNO (reg)
1739 == lra_get_elimination_hard_regno (REGNO (reg))))
1740 no_subreg_reg_operand[nop] = reg;
1741 else
1742 operand_reg[nop] = no_subreg_reg_operand[nop]
1743 /* Just use natural mode for elimination result. It should
1744 be enough for extra constraints hooks. */
1745 = regno_reg_rtx[hard_regno[nop]];
1748 /* The constraints are made of several alternatives. Each operand's
1749 constraint looks like foo,bar,... with commas separating the
1750 alternatives. The first alternatives for all operands go
1751 together, the second alternatives go together, etc.
1753 First loop over alternatives. */
1754 alternative_mask preferred = curr_id->preferred_alternatives;
1755 if (only_alternative >= 0)
1756 preferred &= ALTERNATIVE_BIT (only_alternative);
1758 for (nalt = 0; nalt < n_alternatives; nalt++)
1760 /* Loop over operands for one constraint alternative. */
1761 if (!TEST_BIT (preferred, nalt))
1762 continue;
1764 overall = losers = reject = reload_nregs = reload_sum = 0;
1765 for (nop = 0; nop < n_operands; nop++)
1767 int inc = (curr_static_id
1768 ->operand_alternative[nalt * n_operands + nop].reject);
1769 if (lra_dump_file != NULL && inc != 0)
1770 fprintf (lra_dump_file,
1771 " Staticly defined alt reject+=%d\n", inc);
1772 reject += inc;
1774 early_clobbered_regs_num = 0;
1776 for (nop = 0; nop < n_operands; nop++)
1778 const char *p;
1779 char *end;
1780 int len, c, m, i, opalt_num, this_alternative_matches;
1781 bool win, did_match, offmemok, early_clobber_p;
1782 /* false => this operand can be reloaded somehow for this
1783 alternative. */
1784 bool badop;
1785 /* true => this operand can be reloaded if the alternative
1786 allows regs. */
1787 bool winreg;
1788 /* True if a constant forced into memory would be OK for
1789 this operand. */
1790 bool constmemok;
1791 enum reg_class this_alternative, this_costly_alternative;
1792 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1793 bool this_alternative_match_win, this_alternative_win;
1794 bool this_alternative_offmemok;
1795 bool scratch_p;
1796 machine_mode mode;
1797 enum constraint_num cn;
1799 opalt_num = nalt * n_operands + nop;
1800 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1802 /* Fast track for no constraints at all. */
1803 curr_alt[nop] = NO_REGS;
1804 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1805 curr_alt_win[nop] = true;
1806 curr_alt_match_win[nop] = false;
1807 curr_alt_offmemok[nop] = false;
1808 curr_alt_matches[nop] = -1;
1809 continue;
1812 op = no_subreg_reg_operand[nop];
1813 mode = curr_operand_mode[nop];
1815 win = did_match = winreg = offmemok = constmemok = false;
1816 badop = true;
1818 early_clobber_p = false;
1819 p = curr_static_id->operand_alternative[opalt_num].constraint;
1821 this_costly_alternative = this_alternative = NO_REGS;
1822 /* We update set of possible hard regs besides its class
1823 because reg class might be inaccurate. For example,
1824 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1825 is translated in HI_REGS because classes are merged by
1826 pairs and there is no accurate intermediate class. */
1827 CLEAR_HARD_REG_SET (this_alternative_set);
1828 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1829 this_alternative_win = false;
1830 this_alternative_match_win = false;
1831 this_alternative_offmemok = false;
1832 this_alternative_matches = -1;
1834 /* An empty constraint should be excluded by the fast
1835 track. */
1836 lra_assert (*p != 0 && *p != ',');
1838 op_reject = 0;
1839 /* Scan this alternative's specs for this operand; set WIN
1840 if the operand fits any letter in this alternative.
1841 Otherwise, clear BADOP if this operand could fit some
1842 letter after reloads, or set WINREG if this operand could
1843 fit after reloads provided the constraint allows some
1844 registers. */
1845 costly_p = false;
1848 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1850 case '\0':
1851 len = 0;
1852 break;
1853 case ',':
1854 c = '\0';
1855 break;
1857 case '&':
1858 early_clobber_p = true;
1859 break;
1861 case '$':
1862 op_reject += LRA_MAX_REJECT;
1863 break;
1864 case '^':
1865 op_reject += LRA_LOSER_COST_FACTOR;
1866 break;
1868 case '#':
1869 /* Ignore rest of this alternative. */
1870 c = '\0';
1871 break;
1873 case '0': case '1': case '2': case '3': case '4':
1874 case '5': case '6': case '7': case '8': case '9':
1876 int m_hregno;
1877 bool match_p;
1879 m = strtoul (p, &end, 10);
1880 p = end;
1881 len = 0;
1882 lra_assert (nop > m);
1884 this_alternative_matches = m;
1885 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1886 /* We are supposed to match a previous operand.
1887 If we do, we win if that one did. If we do
1888 not, count both of the operands as losers.
1889 (This is too conservative, since most of the
1890 time only a single reload insn will be needed
1891 to make the two operands win. As a result,
1892 this alternative may be rejected when it is
1893 actually desirable.) */
1894 match_p = false;
1895 if (operands_match_p (*curr_id->operand_loc[nop],
1896 *curr_id->operand_loc[m], m_hregno))
1898 /* We should reject matching of an early
1899 clobber operand if the matching operand is
1900 not dying in the insn. */
1901 if (! curr_static_id->operand[m].early_clobber
1902 || operand_reg[nop] == NULL_RTX
1903 || (find_regno_note (curr_insn, REG_DEAD,
1904 REGNO (op))
1905 || REGNO (op) == REGNO (operand_reg[m])))
1906 match_p = true;
1908 if (match_p)
1910 /* If we are matching a non-offsettable
1911 address where an offsettable address was
1912 expected, then we must reject this
1913 combination, because we can't reload
1914 it. */
1915 if (curr_alt_offmemok[m]
1916 && MEM_P (*curr_id->operand_loc[m])
1917 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1918 continue;
1920 else
1922 /* Operands don't match. Both operands must
1923 allow a reload register, otherwise we
1924 cannot make them match. */
1925 if (curr_alt[m] == NO_REGS)
1926 break;
1927 /* Retroactively mark the operand we had to
1928 match as a loser, if it wasn't already and
1929 it wasn't matched to a register constraint
1930 (e.g it might be matched by memory). */
1931 if (curr_alt_win[m]
1932 && (operand_reg[m] == NULL_RTX
1933 || hard_regno[m] < 0))
1935 losers++;
1936 reload_nregs
1937 += (ira_reg_class_max_nregs[curr_alt[m]]
1938 [GET_MODE (*curr_id->operand_loc[m])]);
1941 /* Prefer matching earlyclobber alternative as
1942 it results in less hard regs required for
1943 the insn than a non-matching earlyclobber
1944 alternative. */
1945 if (curr_static_id->operand[m].early_clobber)
1947 if (lra_dump_file != NULL)
1948 fprintf
1949 (lra_dump_file,
1950 " %d Matching earlyclobber alt:"
1951 " reject--\n",
1952 nop);
1953 reject--;
1955 /* Otherwise we prefer no matching
1956 alternatives because it gives more freedom
1957 in RA. */
1958 else if (operand_reg[nop] == NULL_RTX
1959 || (find_regno_note (curr_insn, REG_DEAD,
1960 REGNO (operand_reg[nop]))
1961 == NULL_RTX))
1963 if (lra_dump_file != NULL)
1964 fprintf
1965 (lra_dump_file,
1966 " %d Matching alt: reject+=2\n",
1967 nop);
1968 reject += 2;
1971 /* If we have to reload this operand and some
1972 previous operand also had to match the same
1973 thing as this operand, we don't know how to do
1974 that. */
1975 if (!match_p || !curr_alt_win[m])
1977 for (i = 0; i < nop; i++)
1978 if (curr_alt_matches[i] == m)
1979 break;
1980 if (i < nop)
1981 break;
1983 else
1984 did_match = true;
1986 /* This can be fixed with reloads if the operand
1987 we are supposed to match can be fixed with
1988 reloads. */
1989 badop = false;
1990 this_alternative = curr_alt[m];
1991 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1992 winreg = this_alternative != NO_REGS;
1993 break;
1996 case 'g':
1997 if (MEM_P (op)
1998 || general_constant_p (op)
1999 || spilled_pseudo_p (op))
2000 win = true;
2001 cl = GENERAL_REGS;
2002 goto reg;
2004 default:
2005 cn = lookup_constraint (p);
2006 switch (get_constraint_type (cn))
2008 case CT_REGISTER:
2009 cl = reg_class_for_constraint (cn);
2010 if (cl != NO_REGS)
2011 goto reg;
2012 break;
2014 case CT_CONST_INT:
2015 if (CONST_INT_P (op)
2016 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2017 win = true;
2018 break;
2020 case CT_MEMORY:
2021 if (MEM_P (op)
2022 && satisfies_memory_constraint_p (op, cn))
2023 win = true;
2024 else if (spilled_pseudo_p (op))
2025 win = true;
2027 /* If we didn't already win, we can reload constants
2028 via force_const_mem or put the pseudo value into
2029 memory, or make other memory by reloading the
2030 address like for 'o'. */
2031 if (CONST_POOL_OK_P (mode, op)
2032 || MEM_P (op) || REG_P (op))
2033 badop = false;
2034 constmemok = true;
2035 offmemok = true;
2036 break;
2038 case CT_ADDRESS:
2039 /* If we didn't already win, we can reload the address
2040 into a base register. */
2041 if (satisfies_address_constraint_p (op, cn))
2042 win = true;
2043 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2044 ADDRESS, SCRATCH);
2045 badop = false;
2046 goto reg;
2048 case CT_FIXED_FORM:
2049 if (constraint_satisfied_p (op, cn))
2050 win = true;
2051 break;
2053 break;
2055 reg:
2056 this_alternative = reg_class_subunion[this_alternative][cl];
2057 IOR_HARD_REG_SET (this_alternative_set,
2058 reg_class_contents[cl]);
2059 if (costly_p)
2061 this_costly_alternative
2062 = reg_class_subunion[this_costly_alternative][cl];
2063 IOR_HARD_REG_SET (this_costly_alternative_set,
2064 reg_class_contents[cl]);
2066 if (mode == BLKmode)
2067 break;
2068 winreg = true;
2069 if (REG_P (op))
2071 if (hard_regno[nop] >= 0
2072 && in_hard_reg_set_p (this_alternative_set,
2073 mode, hard_regno[nop]))
2074 win = true;
2075 else if (hard_regno[nop] < 0
2076 && in_class_p (op, this_alternative, NULL))
2077 win = true;
2079 break;
2081 if (c != ' ' && c != '\t')
2082 costly_p = c == '*';
2084 while ((p += len), c);
2086 scratch_p = (operand_reg[nop] != NULL_RTX
2087 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2088 /* Record which operands fit this alternative. */
2089 if (win)
2091 this_alternative_win = true;
2092 if (operand_reg[nop] != NULL_RTX)
2094 if (hard_regno[nop] >= 0)
2096 if (in_hard_reg_set_p (this_costly_alternative_set,
2097 mode, hard_regno[nop]))
2099 if (lra_dump_file != NULL)
2100 fprintf (lra_dump_file,
2101 " %d Costly set: reject++\n",
2102 nop);
2103 reject++;
2106 else
2108 /* Prefer won reg to spilled pseudo under other
2109 equal conditions for possibe inheritance. */
2110 if (! scratch_p)
2112 if (lra_dump_file != NULL)
2113 fprintf
2114 (lra_dump_file,
2115 " %d Non pseudo reload: reject++\n",
2116 nop);
2117 reject++;
2119 if (in_class_p (operand_reg[nop],
2120 this_costly_alternative, NULL))
2122 if (lra_dump_file != NULL)
2123 fprintf
2124 (lra_dump_file,
2125 " %d Non pseudo costly reload:"
2126 " reject++\n",
2127 nop);
2128 reject++;
2131 /* We simulate the behaviour of old reload here.
2132 Although scratches need hard registers and it
2133 might result in spilling other pseudos, no reload
2134 insns are generated for the scratches. So it
2135 might cost something but probably less than old
2136 reload pass believes. */
2137 if (scratch_p)
2139 if (lra_dump_file != NULL)
2140 fprintf (lra_dump_file,
2141 " %d Scratch win: reject+=2\n",
2142 nop);
2143 reject += 2;
2147 else if (did_match)
2148 this_alternative_match_win = true;
2149 else
2151 int const_to_mem = 0;
2152 bool no_regs_p;
2154 reject += op_reject;
2155 /* Never do output reload of stack pointer. It makes
2156 impossible to do elimination when SP is changed in
2157 RTL. */
2158 if (op == stack_pointer_rtx && ! frame_pointer_needed
2159 && curr_static_id->operand[nop].type != OP_IN)
2160 goto fail;
2162 /* If this alternative asks for a specific reg class, see if there
2163 is at least one allocatable register in that class. */
2164 no_regs_p
2165 = (this_alternative == NO_REGS
2166 || (hard_reg_set_subset_p
2167 (reg_class_contents[this_alternative],
2168 lra_no_alloc_regs)));
2170 /* For asms, verify that the class for this alternative is possible
2171 for the mode that is specified. */
2172 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2174 int i;
2175 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2176 if (HARD_REGNO_MODE_OK (i, mode)
2177 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2178 mode, i))
2179 break;
2180 if (i == FIRST_PSEUDO_REGISTER)
2181 winreg = false;
2184 /* If this operand accepts a register, and if the
2185 register class has at least one allocatable register,
2186 then this operand can be reloaded. */
2187 if (winreg && !no_regs_p)
2188 badop = false;
2190 if (badop)
2192 if (lra_dump_file != NULL)
2193 fprintf (lra_dump_file,
2194 " alt=%d: Bad operand -- refuse\n",
2195 nalt);
2196 goto fail;
2199 /* If not assigned pseudo has a class which a subset of
2200 required reg class, it is a less costly alternative
2201 as the pseudo still can get a hard reg of necessary
2202 class. */
2203 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2204 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2205 && ira_class_subset_p[this_alternative][cl])
2207 if (lra_dump_file != NULL)
2208 fprintf
2209 (lra_dump_file,
2210 " %d Super set class reg: reject-=3\n", nop);
2211 reject -= 3;
2214 this_alternative_offmemok = offmemok;
2215 if (this_costly_alternative != NO_REGS)
2217 if (lra_dump_file != NULL)
2218 fprintf (lra_dump_file,
2219 " %d Costly loser: reject++\n", nop);
2220 reject++;
2222 /* If the operand is dying, has a matching constraint,
2223 and satisfies constraints of the matched operand
2224 which failed to satisfy the own constraints, most probably
2225 the reload for this operand will be gone. */
2226 if (this_alternative_matches >= 0
2227 && !curr_alt_win[this_alternative_matches]
2228 && REG_P (op)
2229 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2230 && (hard_regno[nop] >= 0
2231 ? in_hard_reg_set_p (this_alternative_set,
2232 mode, hard_regno[nop])
2233 : in_class_p (op, this_alternative, NULL)))
2235 if (lra_dump_file != NULL)
2236 fprintf
2237 (lra_dump_file,
2238 " %d Dying matched operand reload: reject++\n",
2239 nop);
2240 reject++;
2242 else
2244 /* Strict_low_part requires to reload the register
2245 not the sub-register. In this case we should
2246 check that a final reload hard reg can hold the
2247 value mode. */
2248 if (curr_static_id->operand[nop].strict_low
2249 && REG_P (op)
2250 && hard_regno[nop] < 0
2251 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2252 && ira_class_hard_regs_num[this_alternative] > 0
2253 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2254 [this_alternative][0],
2255 GET_MODE
2256 (*curr_id->operand_loc[nop])))
2258 if (lra_dump_file != NULL)
2259 fprintf
2260 (lra_dump_file,
2261 " alt=%d: Strict low subreg reload -- refuse\n",
2262 nalt);
2263 goto fail;
2265 losers++;
2267 if (operand_reg[nop] != NULL_RTX
2268 /* Output operands and matched input operands are
2269 not inherited. The following conditions do not
2270 exactly describe the previous statement but they
2271 are pretty close. */
2272 && curr_static_id->operand[nop].type != OP_OUT
2273 && (this_alternative_matches < 0
2274 || curr_static_id->operand[nop].type != OP_IN))
2276 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2277 (operand_reg[nop])]
2278 .last_reload);
2280 /* The value of reload_sum has sense only if we
2281 process insns in their order. It happens only on
2282 the first constraints sub-pass when we do most of
2283 reload work. */
2284 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2285 reload_sum += last_reload - bb_reload_num;
2287 /* If this is a constant that is reloaded into the
2288 desired class by copying it to memory first, count
2289 that as another reload. This is consistent with
2290 other code and is required to avoid choosing another
2291 alternative when the constant is moved into memory.
2292 Note that the test here is precisely the same as in
2293 the code below that calls force_const_mem. */
2294 if (CONST_POOL_OK_P (mode, op)
2295 && ((targetm.preferred_reload_class
2296 (op, this_alternative) == NO_REGS)
2297 || no_input_reloads_p))
2299 const_to_mem = 1;
2300 if (! no_regs_p)
2301 losers++;
2304 /* Alternative loses if it requires a type of reload not
2305 permitted for this insn. We can always reload
2306 objects with a REG_UNUSED note. */
2307 if ((curr_static_id->operand[nop].type != OP_IN
2308 && no_output_reloads_p
2309 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2310 || (curr_static_id->operand[nop].type != OP_OUT
2311 && no_input_reloads_p && ! const_to_mem)
2312 || (this_alternative_matches >= 0
2313 && (no_input_reloads_p
2314 || (no_output_reloads_p
2315 && (curr_static_id->operand
2316 [this_alternative_matches].type != OP_IN)
2317 && ! find_reg_note (curr_insn, REG_UNUSED,
2318 no_subreg_reg_operand
2319 [this_alternative_matches])))))
2321 if (lra_dump_file != NULL)
2322 fprintf
2323 (lra_dump_file,
2324 " alt=%d: No input/otput reload -- refuse\n",
2325 nalt);
2326 goto fail;
2329 /* Alternative loses if it required class pseudo can not
2330 hold value of required mode. Such insns can be
2331 described by insn definitions with mode iterators. */
2332 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2333 && ! hard_reg_set_empty_p (this_alternative_set)
2334 /* It is common practice for constraints to use a
2335 class which does not have actually enough regs to
2336 hold the value (e.g. x86 AREG for mode requiring
2337 more one general reg). Therefore we have 2
2338 conditions to check that the reload pseudo can
2339 not hold the mode value. */
2340 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2341 [this_alternative][0],
2342 GET_MODE (*curr_id->operand_loc[nop]))
2343 /* The above condition is not enough as the first
2344 reg in ira_class_hard_regs can be not aligned for
2345 multi-words mode values. */
2346 && (prohibited_class_reg_set_mode_p
2347 (this_alternative, this_alternative_set,
2348 GET_MODE (*curr_id->operand_loc[nop]))))
2350 if (lra_dump_file != NULL)
2351 fprintf (lra_dump_file,
2352 " alt=%d: reload pseudo for op %d "
2353 " can not hold the mode value -- refuse\n",
2354 nalt, nop);
2355 goto fail;
2358 /* Check strong discouragement of reload of non-constant
2359 into class THIS_ALTERNATIVE. */
2360 if (! CONSTANT_P (op) && ! no_regs_p
2361 && (targetm.preferred_reload_class
2362 (op, this_alternative) == NO_REGS
2363 || (curr_static_id->operand[nop].type == OP_OUT
2364 && (targetm.preferred_output_reload_class
2365 (op, this_alternative) == NO_REGS))))
2367 if (lra_dump_file != NULL)
2368 fprintf (lra_dump_file,
2369 " %d Non-prefered reload: reject+=%d\n",
2370 nop, LRA_MAX_REJECT);
2371 reject += LRA_MAX_REJECT;
2374 if (! (MEM_P (op) && offmemok)
2375 && ! (const_to_mem && constmemok))
2377 /* We prefer to reload pseudos over reloading other
2378 things, since such reloads may be able to be
2379 eliminated later. So bump REJECT in other cases.
2380 Don't do this in the case where we are forcing a
2381 constant into memory and it will then win since
2382 we don't want to have a different alternative
2383 match then. */
2384 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2386 if (lra_dump_file != NULL)
2387 fprintf
2388 (lra_dump_file,
2389 " %d Non-pseudo reload: reject+=2\n",
2390 nop);
2391 reject += 2;
2394 if (! no_regs_p)
2395 reload_nregs
2396 += ira_reg_class_max_nregs[this_alternative][mode];
2398 if (SMALL_REGISTER_CLASS_P (this_alternative))
2400 if (lra_dump_file != NULL)
2401 fprintf
2402 (lra_dump_file,
2403 " %d Small class reload: reject+=%d\n",
2404 nop, LRA_LOSER_COST_FACTOR / 2);
2405 reject += LRA_LOSER_COST_FACTOR / 2;
2409 /* We are trying to spill pseudo into memory. It is
2410 usually more costly than moving to a hard register
2411 although it might takes the same number of
2412 reloads. */
2413 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2415 if (lra_dump_file != NULL)
2416 fprintf
2417 (lra_dump_file,
2418 " %d Spill pseudo into memory: reject+=3\n",
2419 nop);
2420 reject += 3;
2421 if (VECTOR_MODE_P (mode))
2423 /* Spilling vectors into memory is usually more
2424 costly as they contain big values. */
2425 if (lra_dump_file != NULL)
2426 fprintf
2427 (lra_dump_file,
2428 " %d Spill vector pseudo: reject+=2\n",
2429 nop);
2430 reject += 2;
2434 #ifdef SECONDARY_MEMORY_NEEDED
2435 /* If reload requires moving value through secondary
2436 memory, it will need one more insn at least. */
2437 if (this_alternative != NO_REGS
2438 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2439 && ((curr_static_id->operand[nop].type != OP_OUT
2440 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2441 GET_MODE (op)))
2442 || (curr_static_id->operand[nop].type != OP_IN
2443 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2444 GET_MODE (op)))))
2445 losers++;
2446 #endif
2447 /* Input reloads can be inherited more often than output
2448 reloads can be removed, so penalize output
2449 reloads. */
2450 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2452 if (lra_dump_file != NULL)
2453 fprintf
2454 (lra_dump_file,
2455 " %d Non input pseudo reload: reject++\n",
2456 nop);
2457 reject++;
2461 if (early_clobber_p && ! scratch_p)
2463 if (lra_dump_file != NULL)
2464 fprintf (lra_dump_file,
2465 " %d Early clobber: reject++\n", nop);
2466 reject++;
2468 /* ??? We check early clobbers after processing all operands
2469 (see loop below) and there we update the costs more.
2470 Should we update the cost (may be approximately) here
2471 because of early clobber register reloads or it is a rare
2472 or non-important thing to be worth to do it. */
2473 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2474 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2476 if (lra_dump_file != NULL)
2477 fprintf (lra_dump_file,
2478 " alt=%d,overall=%d,losers=%d -- refuse\n",
2479 nalt, overall, losers);
2480 goto fail;
2483 curr_alt[nop] = this_alternative;
2484 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2485 curr_alt_win[nop] = this_alternative_win;
2486 curr_alt_match_win[nop] = this_alternative_match_win;
2487 curr_alt_offmemok[nop] = this_alternative_offmemok;
2488 curr_alt_matches[nop] = this_alternative_matches;
2490 if (this_alternative_matches >= 0
2491 && !did_match && !this_alternative_win)
2492 curr_alt_win[this_alternative_matches] = false;
2494 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2495 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2497 if (curr_insn_set != NULL_RTX && n_operands == 2
2498 /* Prevent processing non-move insns. */
2499 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2500 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2501 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2502 && REG_P (no_subreg_reg_operand[0])
2503 && REG_P (no_subreg_reg_operand[1])
2504 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2505 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2506 || (! curr_alt_win[0] && curr_alt_win[1]
2507 && REG_P (no_subreg_reg_operand[1])
2508 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2509 || (curr_alt_win[0] && ! curr_alt_win[1]
2510 && REG_P (no_subreg_reg_operand[0])
2511 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2512 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2513 no_subreg_reg_operand[1])
2514 || (targetm.preferred_reload_class
2515 (no_subreg_reg_operand[1],
2516 (enum reg_class) curr_alt[1]) != NO_REGS))
2517 /* If it is a result of recent elimination in move
2518 insn we can transform it into an add still by
2519 using this alternative. */
2520 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2522 /* We have a move insn and a new reload insn will be similar
2523 to the current insn. We should avoid such situation as it
2524 results in LRA cycling. */
2525 overall += LRA_MAX_REJECT;
2527 ok_p = true;
2528 curr_alt_dont_inherit_ops_num = 0;
2529 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2531 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2532 HARD_REG_SET temp_set;
2534 i = early_clobbered_nops[nop];
2535 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2536 || hard_regno[i] < 0)
2537 continue;
2538 lra_assert (operand_reg[i] != NULL_RTX);
2539 clobbered_hard_regno = hard_regno[i];
2540 CLEAR_HARD_REG_SET (temp_set);
2541 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2542 first_conflict_j = last_conflict_j = -1;
2543 for (j = 0; j < n_operands; j++)
2544 if (j == i
2545 /* We don't want process insides of match_operator and
2546 match_parallel because otherwise we would process
2547 their operands once again generating a wrong
2548 code. */
2549 || curr_static_id->operand[j].is_operator)
2550 continue;
2551 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2552 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2553 continue;
2554 /* If we don't reload j-th operand, check conflicts. */
2555 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2556 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2558 if (first_conflict_j < 0)
2559 first_conflict_j = j;
2560 last_conflict_j = j;
2562 if (last_conflict_j < 0)
2563 continue;
2564 /* If earlyclobber operand conflicts with another
2565 non-matching operand which is actually the same register
2566 as the earlyclobber operand, it is better to reload the
2567 another operand as an operand matching the earlyclobber
2568 operand can be also the same. */
2569 if (first_conflict_j == last_conflict_j
2570 && operand_reg[last_conflict_j]
2571 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2572 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2574 curr_alt_win[last_conflict_j] = false;
2575 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2576 = last_conflict_j;
2577 losers++;
2578 /* Early clobber was already reflected in REJECT. */
2579 lra_assert (reject > 0);
2580 if (lra_dump_file != NULL)
2581 fprintf
2582 (lra_dump_file,
2583 " %d Conflict early clobber reload: reject--\n",
2585 reject--;
2586 overall += LRA_LOSER_COST_FACTOR - 1;
2588 else
2590 /* We need to reload early clobbered register and the
2591 matched registers. */
2592 for (j = 0; j < n_operands; j++)
2593 if (curr_alt_matches[j] == i)
2595 curr_alt_match_win[j] = false;
2596 losers++;
2597 overall += LRA_LOSER_COST_FACTOR;
2599 if (! curr_alt_match_win[i])
2600 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2601 else
2603 /* Remember pseudos used for match reloads are never
2604 inherited. */
2605 lra_assert (curr_alt_matches[i] >= 0);
2606 curr_alt_win[curr_alt_matches[i]] = false;
2608 curr_alt_win[i] = curr_alt_match_win[i] = false;
2609 losers++;
2610 /* Early clobber was already reflected in REJECT. */
2611 lra_assert (reject > 0);
2612 if (lra_dump_file != NULL)
2613 fprintf
2614 (lra_dump_file,
2615 " %d Matched conflict early clobber reloads:"
2616 "reject--\n",
2618 reject--;
2619 overall += LRA_LOSER_COST_FACTOR - 1;
2622 if (lra_dump_file != NULL)
2623 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2624 nalt, overall, losers, reload_nregs);
2626 /* If this alternative can be made to work by reloading, and it
2627 needs less reloading than the others checked so far, record
2628 it as the chosen goal for reloading. */
2629 if ((best_losers != 0 && losers == 0)
2630 || (((best_losers == 0 && losers == 0)
2631 || (best_losers != 0 && losers != 0))
2632 && (best_overall > overall
2633 || (best_overall == overall
2634 /* If the cost of the reloads is the same,
2635 prefer alternative which requires minimal
2636 number of reload regs. */
2637 && (reload_nregs < best_reload_nregs
2638 || (reload_nregs == best_reload_nregs
2639 && (best_reload_sum < reload_sum
2640 || (best_reload_sum == reload_sum
2641 && nalt < goal_alt_number))))))))
2643 for (nop = 0; nop < n_operands; nop++)
2645 goal_alt_win[nop] = curr_alt_win[nop];
2646 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2647 goal_alt_matches[nop] = curr_alt_matches[nop];
2648 goal_alt[nop] = curr_alt[nop];
2649 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2651 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2652 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2653 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2654 goal_alt_swapped = curr_swapped;
2655 best_overall = overall;
2656 best_losers = losers;
2657 best_reload_nregs = reload_nregs;
2658 best_reload_sum = reload_sum;
2659 goal_alt_number = nalt;
2661 if (losers == 0)
2662 /* Everything is satisfied. Do not process alternatives
2663 anymore. */
2664 break;
2665 fail:
2668 return ok_p;
2671 /* Make reload base reg from address AD. */
2672 static rtx
2673 base_to_reg (struct address_info *ad)
2675 enum reg_class cl;
2676 int code = -1;
2677 rtx new_inner = NULL_RTX;
2678 rtx new_reg = NULL_RTX;
2679 rtx_insn *insn;
2680 rtx_insn *last_insn = get_last_insn();
2682 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2683 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2684 get_index_code (ad));
2685 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2686 cl, "base");
2687 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2688 ad->disp_term == NULL
2689 ? gen_int_mode (0, ad->mode)
2690 : *ad->disp_term);
2691 if (!valid_address_p (ad->mode, new_inner, ad->as))
2692 return NULL_RTX;
2693 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base_term));
2694 code = recog_memoized (insn);
2695 if (code < 0)
2697 delete_insns_since (last_insn);
2698 return NULL_RTX;
2701 return new_inner;
2704 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2705 static rtx
2706 base_plus_disp_to_reg (struct address_info *ad)
2708 enum reg_class cl;
2709 rtx new_reg;
2711 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2712 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2713 get_index_code (ad));
2714 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2715 cl, "base + disp");
2716 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2717 return new_reg;
2720 /* Make reload of index part of address AD. Return the new
2721 pseudo. */
2722 static rtx
2723 index_part_to_reg (struct address_info *ad)
2725 rtx new_reg;
2727 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2728 INDEX_REG_CLASS, "index term");
2729 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2730 GEN_INT (get_index_scale (ad)), new_reg, 1);
2731 return new_reg;
2734 /* Return true if we can add a displacement to address AD, even if that
2735 makes the address invalid. The fix-up code requires any new address
2736 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2737 static bool
2738 can_add_disp_p (struct address_info *ad)
2740 return (!ad->autoinc_p
2741 && ad->segment == NULL
2742 && ad->base == ad->base_term
2743 && ad->disp == ad->disp_term);
2746 /* Make equiv substitution in address AD. Return true if a substitution
2747 was made. */
2748 static bool
2749 equiv_address_substitution (struct address_info *ad)
2751 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2752 HOST_WIDE_INT disp, scale;
2753 bool change_p;
2755 base_term = strip_subreg (ad->base_term);
2756 if (base_term == NULL)
2757 base_reg = new_base_reg = NULL_RTX;
2758 else
2760 base_reg = *base_term;
2761 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2763 index_term = strip_subreg (ad->index_term);
2764 if (index_term == NULL)
2765 index_reg = new_index_reg = NULL_RTX;
2766 else
2768 index_reg = *index_term;
2769 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2771 if (base_reg == new_base_reg && index_reg == new_index_reg)
2772 return false;
2773 disp = 0;
2774 change_p = false;
2775 if (lra_dump_file != NULL)
2777 fprintf (lra_dump_file, "Changing address in insn %d ",
2778 INSN_UID (curr_insn));
2779 dump_value_slim (lra_dump_file, *ad->outer, 1);
2781 if (base_reg != new_base_reg)
2783 if (REG_P (new_base_reg))
2785 *base_term = new_base_reg;
2786 change_p = true;
2788 else if (GET_CODE (new_base_reg) == PLUS
2789 && REG_P (XEXP (new_base_reg, 0))
2790 && CONST_INT_P (XEXP (new_base_reg, 1))
2791 && can_add_disp_p (ad))
2793 disp += INTVAL (XEXP (new_base_reg, 1));
2794 *base_term = XEXP (new_base_reg, 0);
2795 change_p = true;
2797 if (ad->base_term2 != NULL)
2798 *ad->base_term2 = *ad->base_term;
2800 if (index_reg != new_index_reg)
2802 if (REG_P (new_index_reg))
2804 *index_term = new_index_reg;
2805 change_p = true;
2807 else if (GET_CODE (new_index_reg) == PLUS
2808 && REG_P (XEXP (new_index_reg, 0))
2809 && CONST_INT_P (XEXP (new_index_reg, 1))
2810 && can_add_disp_p (ad)
2811 && (scale = get_index_scale (ad)))
2813 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2814 *index_term = XEXP (new_index_reg, 0);
2815 change_p = true;
2818 if (disp != 0)
2820 if (ad->disp != NULL)
2821 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2822 else
2824 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2825 update_address (ad);
2827 change_p = true;
2829 if (lra_dump_file != NULL)
2831 if (! change_p)
2832 fprintf (lra_dump_file, " -- no change\n");
2833 else
2835 fprintf (lra_dump_file, " on equiv ");
2836 dump_value_slim (lra_dump_file, *ad->outer, 1);
2837 fprintf (lra_dump_file, "\n");
2840 return change_p;
2843 /* Major function to make reloads for an address in operand NOP or
2844 check its correctness (If CHECK_ONLY_P is true). The supported
2845 cases are:
2847 1) an address that existed before LRA started, at which point it
2848 must have been valid. These addresses are subject to elimination
2849 and may have become invalid due to the elimination offset being out
2850 of range.
2852 2) an address created by forcing a constant to memory
2853 (force_const_to_mem). The initial form of these addresses might
2854 not be valid, and it is this function's job to make them valid.
2856 3) a frame address formed from a register and a (possibly zero)
2857 constant offset. As above, these addresses might not be valid and
2858 this function must make them so.
2860 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2861 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2862 address. Return true for any RTL change.
2864 The function is a helper function which does not produce all
2865 transformations (when CHECK_ONLY_P is false) which can be
2866 necessary. It does just basic steps. To do all necessary
2867 transformations use function process_address. */
2868 static bool
2869 process_address_1 (int nop, bool check_only_p,
2870 rtx_insn **before, rtx_insn **after)
2872 struct address_info ad;
2873 rtx new_reg;
2874 rtx op = *curr_id->operand_loc[nop];
2875 const char *constraint = curr_static_id->operand[nop].constraint;
2876 enum constraint_num cn = lookup_constraint (constraint);
2877 bool change_p = false;
2879 if (insn_extra_address_constraint (cn))
2880 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2881 else if (MEM_P (op))
2882 decompose_mem_address (&ad, op);
2883 else if (GET_CODE (op) == SUBREG
2884 && MEM_P (SUBREG_REG (op)))
2885 decompose_mem_address (&ad, SUBREG_REG (op));
2886 else
2887 return false;
2888 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
2889 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
2890 when INDEX_REG_CLASS is a single register class. */
2891 if (ad.base_term != NULL
2892 && ad.index_term != NULL
2893 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
2894 && REG_P (*ad.base_term)
2895 && REG_P (*ad.index_term)
2896 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
2897 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
2899 std::swap (ad.base, ad.index);
2900 std::swap (ad.base_term, ad.index_term);
2902 if (! check_only_p)
2903 change_p = equiv_address_substitution (&ad);
2904 if (ad.base_term != NULL
2905 && (process_addr_reg
2906 (ad.base_term, check_only_p, before,
2907 (ad.autoinc_p
2908 && !(REG_P (*ad.base_term)
2909 && find_regno_note (curr_insn, REG_DEAD,
2910 REGNO (*ad.base_term)) != NULL_RTX)
2911 ? after : NULL),
2912 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2913 get_index_code (&ad)))))
2915 change_p = true;
2916 if (ad.base_term2 != NULL)
2917 *ad.base_term2 = *ad.base_term;
2919 if (ad.index_term != NULL
2920 && process_addr_reg (ad.index_term, check_only_p,
2921 before, NULL, INDEX_REG_CLASS))
2922 change_p = true;
2924 /* Target hooks sometimes don't treat extra-constraint addresses as
2925 legitimate address_operands, so handle them specially. */
2926 if (insn_extra_address_constraint (cn)
2927 && satisfies_address_constraint_p (&ad, cn))
2928 return change_p;
2930 if (check_only_p)
2931 return change_p;
2933 /* There are three cases where the shape of *AD.INNER may now be invalid:
2935 1) the original address was valid, but either elimination or
2936 equiv_address_substitution was applied and that made
2937 the address invalid.
2939 2) the address is an invalid symbolic address created by
2940 force_const_to_mem.
2942 3) the address is a frame address with an invalid offset.
2944 4) the address is a frame address with an invalid base.
2946 All these cases involve a non-autoinc address, so there is no
2947 point revalidating other types. */
2948 if (ad.autoinc_p || valid_address_p (&ad))
2949 return change_p;
2951 /* Any index existed before LRA started, so we can assume that the
2952 presence and shape of the index is valid. */
2953 push_to_sequence (*before);
2954 lra_assert (ad.disp == ad.disp_term);
2955 if (ad.base == NULL)
2957 if (ad.index == NULL)
2959 int code = -1;
2960 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2961 SCRATCH, SCRATCH);
2962 rtx addr = *ad.inner;
2964 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2965 if (HAVE_lo_sum)
2967 rtx_insn *insn;
2968 rtx_insn *last = get_last_insn ();
2970 /* addr => lo_sum (new_base, addr), case (2) above. */
2971 insn = emit_insn (gen_rtx_SET
2972 (new_reg,
2973 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2974 code = recog_memoized (insn);
2975 if (code >= 0)
2977 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2978 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2980 /* Try to put lo_sum into register. */
2981 insn = emit_insn (gen_rtx_SET
2982 (new_reg,
2983 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2984 code = recog_memoized (insn);
2985 if (code >= 0)
2987 *ad.inner = new_reg;
2988 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2990 *ad.inner = addr;
2991 code = -1;
2997 if (code < 0)
2998 delete_insns_since (last);
3001 if (code < 0)
3003 /* addr => new_base, case (2) above. */
3004 lra_emit_move (new_reg, addr);
3005 *ad.inner = new_reg;
3008 else
3010 /* index * scale + disp => new base + index * scale,
3011 case (1) above. */
3012 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3013 GET_CODE (*ad.index));
3015 lra_assert (INDEX_REG_CLASS != NO_REGS);
3016 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3017 lra_emit_move (new_reg, *ad.disp);
3018 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3019 new_reg, *ad.index);
3022 else if (ad.index == NULL)
3024 int regno;
3025 enum reg_class cl;
3026 rtx set;
3027 rtx_insn *insns, *last_insn;
3028 /* Try to reload base into register only if the base is invalid
3029 for the address but with valid offset, case (4) above. */
3030 start_sequence ();
3031 new_reg = base_to_reg (&ad);
3033 /* base + disp => new base, cases (1) and (3) above. */
3034 /* Another option would be to reload the displacement into an
3035 index register. However, postreload has code to optimize
3036 address reloads that have the same base and different
3037 displacements, so reloading into an index register would
3038 not necessarily be a win. */
3039 if (new_reg == NULL_RTX)
3040 new_reg = base_plus_disp_to_reg (&ad);
3041 insns = get_insns ();
3042 last_insn = get_last_insn ();
3043 /* If we generated at least two insns, try last insn source as
3044 an address. If we succeed, we generate one less insn. */
3045 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3046 && GET_CODE (SET_SRC (set)) == PLUS
3047 && REG_P (XEXP (SET_SRC (set), 0))
3048 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3050 *ad.inner = SET_SRC (set);
3051 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3053 *ad.base_term = XEXP (SET_SRC (set), 0);
3054 *ad.disp_term = XEXP (SET_SRC (set), 1);
3055 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3056 get_index_code (&ad));
3057 regno = REGNO (*ad.base_term);
3058 if (regno >= FIRST_PSEUDO_REGISTER
3059 && cl != lra_get_allocno_class (regno))
3060 lra_change_class (regno, cl, " Change to", true);
3061 new_reg = SET_SRC (set);
3062 delete_insns_since (PREV_INSN (last_insn));
3065 /* Try if target can split displacement into legitimite new disp
3066 and offset. If it's the case, we replace the last insn with
3067 insns for base + offset => new_reg and set new_reg + new disp
3068 to *ad.inner. */
3069 last_insn = get_last_insn ();
3070 if ((set = single_set (last_insn)) != NULL_RTX
3071 && GET_CODE (SET_SRC (set)) == PLUS
3072 && REG_P (XEXP (SET_SRC (set), 0))
3073 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3074 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3076 rtx addend, disp = XEXP (SET_SRC (set), 1);
3077 if (targetm.legitimize_address_displacement (&disp, &addend,
3078 ad.mode))
3080 rtx_insn *new_insns;
3081 start_sequence ();
3082 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3083 new_insns = get_insns ();
3084 end_sequence ();
3085 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3086 delete_insns_since (PREV_INSN (last_insn));
3087 add_insn (new_insns);
3088 insns = get_insns ();
3091 end_sequence ();
3092 emit_insn (insns);
3093 *ad.inner = new_reg;
3095 else if (ad.disp_term != NULL)
3097 /* base + scale * index + disp => new base + scale * index,
3098 case (1) above. */
3099 new_reg = base_plus_disp_to_reg (&ad);
3100 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3101 new_reg, *ad.index);
3103 else if (get_index_scale (&ad) == 1)
3105 /* The last transformation to one reg will be made in
3106 curr_insn_transform function. */
3107 end_sequence ();
3108 return false;
3110 else
3112 /* base + scale * index => base + new_reg,
3113 case (1) above.
3114 Index part of address may become invalid. For example, we
3115 changed pseudo on the equivalent memory and a subreg of the
3116 pseudo onto the memory of different mode for which the scale is
3117 prohibitted. */
3118 new_reg = index_part_to_reg (&ad);
3119 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3120 *ad.base_term, new_reg);
3122 *before = get_insns ();
3123 end_sequence ();
3124 return true;
3127 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3128 Use process_address_1 as a helper function. Return true for any
3129 RTL changes.
3131 If CHECK_ONLY_P is true, just check address correctness. Return
3132 false if the address correct. */
3133 static bool
3134 process_address (int nop, bool check_only_p,
3135 rtx_insn **before, rtx_insn **after)
3137 bool res = false;
3139 while (process_address_1 (nop, check_only_p, before, after))
3141 if (check_only_p)
3142 return true;
3143 res = true;
3145 return res;
3148 /* Emit insns to reload VALUE into a new register. VALUE is an
3149 auto-increment or auto-decrement RTX whose operand is a register or
3150 memory location; so reloading involves incrementing that location.
3151 IN is either identical to VALUE, or some cheaper place to reload
3152 value being incremented/decremented from.
3154 INC_AMOUNT is the number to increment or decrement by (always
3155 positive and ignored for POST_MODIFY/PRE_MODIFY).
3157 Return pseudo containing the result. */
3158 static rtx
3159 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3161 /* REG or MEM to be copied and incremented. */
3162 rtx incloc = XEXP (value, 0);
3163 /* Nonzero if increment after copying. */
3164 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3165 || GET_CODE (value) == POST_MODIFY);
3166 rtx_insn *last;
3167 rtx inc;
3168 rtx_insn *add_insn;
3169 int code;
3170 rtx real_in = in == value ? incloc : in;
3171 rtx result;
3172 bool plus_p = true;
3174 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3176 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3177 || GET_CODE (XEXP (value, 1)) == MINUS);
3178 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3179 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3180 inc = XEXP (XEXP (value, 1), 1);
3182 else
3184 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3185 inc_amount = -inc_amount;
3187 inc = GEN_INT (inc_amount);
3190 if (! post && REG_P (incloc))
3191 result = incloc;
3192 else
3193 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3194 "INC/DEC result");
3196 if (real_in != result)
3198 /* First copy the location to the result register. */
3199 lra_assert (REG_P (result));
3200 emit_insn (gen_move_insn (result, real_in));
3203 /* We suppose that there are insns to add/sub with the constant
3204 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3205 old reload worked with this assumption. If the assumption
3206 becomes wrong, we should use approach in function
3207 base_plus_disp_to_reg. */
3208 if (in == value)
3210 /* See if we can directly increment INCLOC. */
3211 last = get_last_insn ();
3212 add_insn = emit_insn (plus_p
3213 ? gen_add2_insn (incloc, inc)
3214 : gen_sub2_insn (incloc, inc));
3216 code = recog_memoized (add_insn);
3217 if (code >= 0)
3219 if (! post && result != incloc)
3220 emit_insn (gen_move_insn (result, incloc));
3221 return result;
3223 delete_insns_since (last);
3226 /* If couldn't do the increment directly, must increment in RESULT.
3227 The way we do this depends on whether this is pre- or
3228 post-increment. For pre-increment, copy INCLOC to the reload
3229 register, increment it there, then save back. */
3230 if (! post)
3232 if (real_in != result)
3233 emit_insn (gen_move_insn (result, real_in));
3234 if (plus_p)
3235 emit_insn (gen_add2_insn (result, inc));
3236 else
3237 emit_insn (gen_sub2_insn (result, inc));
3238 if (result != incloc)
3239 emit_insn (gen_move_insn (incloc, result));
3241 else
3243 /* Post-increment.
3245 Because this might be a jump insn or a compare, and because
3246 RESULT may not be available after the insn in an input
3247 reload, we must do the incrementing before the insn being
3248 reloaded for.
3250 We have already copied IN to RESULT. Increment the copy in
3251 RESULT, save that back, then decrement RESULT so it has
3252 the original value. */
3253 if (plus_p)
3254 emit_insn (gen_add2_insn (result, inc));
3255 else
3256 emit_insn (gen_sub2_insn (result, inc));
3257 emit_insn (gen_move_insn (incloc, result));
3258 /* Restore non-modified value for the result. We prefer this
3259 way because it does not require an additional hard
3260 register. */
3261 if (plus_p)
3263 if (CONST_INT_P (inc))
3264 emit_insn (gen_add2_insn (result,
3265 gen_int_mode (-INTVAL (inc),
3266 GET_MODE (result))));
3267 else
3268 emit_insn (gen_sub2_insn (result, inc));
3270 else
3271 emit_insn (gen_add2_insn (result, inc));
3273 return result;
3276 /* Return true if the current move insn does not need processing as we
3277 already know that it satisfies its constraints. */
3278 static bool
3279 simple_move_p (void)
3281 rtx dest, src;
3282 enum reg_class dclass, sclass;
3284 lra_assert (curr_insn_set != NULL_RTX);
3285 dest = SET_DEST (curr_insn_set);
3286 src = SET_SRC (curr_insn_set);
3287 return ((dclass = get_op_class (dest)) != NO_REGS
3288 && (sclass = get_op_class (src)) != NO_REGS
3289 /* The backend guarantees that register moves of cost 2
3290 never need reloads. */
3291 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3294 /* Swap operands NOP and NOP + 1. */
3295 static inline void
3296 swap_operands (int nop)
3298 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3299 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3300 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3301 /* Swap the duplicates too. */
3302 lra_update_dup (curr_id, nop);
3303 lra_update_dup (curr_id, nop + 1);
3306 /* Main entry point of the constraint code: search the body of the
3307 current insn to choose the best alternative. It is mimicking insn
3308 alternative cost calculation model of former reload pass. That is
3309 because machine descriptions were written to use this model. This
3310 model can be changed in future. Make commutative operand exchange
3311 if it is chosen.
3313 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3314 constraints. Return true if any change happened during function
3315 call.
3317 If CHECK_ONLY_P is true then don't do any transformation. Just
3318 check that the insn satisfies all constraints. If the insn does
3319 not satisfy any constraint, return true. */
3320 static bool
3321 curr_insn_transform (bool check_only_p)
3323 int i, j, k;
3324 int n_operands;
3325 int n_alternatives;
3326 int commutative;
3327 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3328 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3329 rtx_insn *before, *after;
3330 bool alt_p = false;
3331 /* Flag that the insn has been changed through a transformation. */
3332 bool change_p;
3333 bool sec_mem_p;
3334 #ifdef SECONDARY_MEMORY_NEEDED
3335 bool use_sec_mem_p;
3336 #endif
3337 int max_regno_before;
3338 int reused_alternative_num;
3340 curr_insn_set = single_set (curr_insn);
3341 if (curr_insn_set != NULL_RTX && simple_move_p ())
3342 return false;
3344 no_input_reloads_p = no_output_reloads_p = false;
3345 goal_alt_number = -1;
3346 change_p = sec_mem_p = false;
3347 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3348 reloads; neither are insns that SET cc0. Insns that use CC0 are
3349 not allowed to have any input reloads. */
3350 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3351 no_output_reloads_p = true;
3353 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3354 no_input_reloads_p = true;
3355 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3356 no_output_reloads_p = true;
3358 n_operands = curr_static_id->n_operands;
3359 n_alternatives = curr_static_id->n_alternatives;
3361 /* Just return "no reloads" if insn has no operands with
3362 constraints. */
3363 if (n_operands == 0 || n_alternatives == 0)
3364 return false;
3366 max_regno_before = max_reg_num ();
3368 for (i = 0; i < n_operands; i++)
3370 goal_alt_matched[i][0] = -1;
3371 goal_alt_matches[i] = -1;
3374 commutative = curr_static_id->commutative;
3376 /* Now see what we need for pseudos that didn't get hard regs or got
3377 the wrong kind of hard reg. For this, we must consider all the
3378 operands together against the register constraints. */
3380 best_losers = best_overall = INT_MAX;
3381 best_reload_sum = 0;
3383 curr_swapped = false;
3384 goal_alt_swapped = false;
3386 if (! check_only_p)
3387 /* Make equivalence substitution and memory subreg elimination
3388 before address processing because an address legitimacy can
3389 depend on memory mode. */
3390 for (i = 0; i < n_operands; i++)
3392 rtx op = *curr_id->operand_loc[i];
3393 rtx subst, old = op;
3394 bool op_change_p = false;
3396 if (GET_CODE (old) == SUBREG)
3397 old = SUBREG_REG (old);
3398 subst = get_equiv_with_elimination (old, curr_insn);
3399 original_subreg_reg_mode[i] = VOIDmode;
3400 if (subst != old)
3402 subst = copy_rtx (subst);
3403 lra_assert (REG_P (old));
3404 if (GET_CODE (op) != SUBREG)
3405 *curr_id->operand_loc[i] = subst;
3406 else
3408 SUBREG_REG (op) = subst;
3409 if (GET_MODE (subst) == VOIDmode)
3410 original_subreg_reg_mode[i] = GET_MODE (old);
3412 if (lra_dump_file != NULL)
3414 fprintf (lra_dump_file,
3415 "Changing pseudo %d in operand %i of insn %u on equiv ",
3416 REGNO (old), i, INSN_UID (curr_insn));
3417 dump_value_slim (lra_dump_file, subst, 1);
3418 fprintf (lra_dump_file, "\n");
3420 op_change_p = change_p = true;
3422 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3424 change_p = true;
3425 lra_update_dup (curr_id, i);
3429 /* Reload address registers and displacements. We do it before
3430 finding an alternative because of memory constraints. */
3431 before = after = NULL;
3432 for (i = 0; i < n_operands; i++)
3433 if (! curr_static_id->operand[i].is_operator
3434 && process_address (i, check_only_p, &before, &after))
3436 if (check_only_p)
3437 return true;
3438 change_p = true;
3439 lra_update_dup (curr_id, i);
3442 if (change_p)
3443 /* If we've changed the instruction then any alternative that
3444 we chose previously may no longer be valid. */
3445 lra_set_used_insn_alternative (curr_insn, -1);
3447 if (! check_only_p && curr_insn_set != NULL_RTX
3448 && check_and_process_move (&change_p, &sec_mem_p))
3449 return change_p;
3451 try_swapped:
3453 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3454 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3455 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3456 reused_alternative_num, INSN_UID (curr_insn));
3458 if (process_alt_operands (reused_alternative_num))
3459 alt_p = true;
3461 if (check_only_p)
3462 return ! alt_p || best_losers != 0;
3464 /* If insn is commutative (it's safe to exchange a certain pair of
3465 operands) then we need to try each alternative twice, the second
3466 time matching those two operands as if we had exchanged them. To
3467 do this, really exchange them in operands.
3469 If we have just tried the alternatives the second time, return
3470 operands to normal and drop through. */
3472 if (reused_alternative_num < 0 && commutative >= 0)
3474 curr_swapped = !curr_swapped;
3475 if (curr_swapped)
3477 swap_operands (commutative);
3478 goto try_swapped;
3480 else
3481 swap_operands (commutative);
3484 if (! alt_p && ! sec_mem_p)
3486 /* No alternative works with reloads?? */
3487 if (INSN_CODE (curr_insn) >= 0)
3488 fatal_insn ("unable to generate reloads for:", curr_insn);
3489 error_for_asm (curr_insn,
3490 "inconsistent operand constraints in an %<asm%>");
3491 /* Avoid further trouble with this insn. */
3492 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3493 lra_invalidate_insn_data (curr_insn);
3494 return true;
3497 /* If the best alternative is with operands 1 and 2 swapped, swap
3498 them. Update the operand numbers of any reloads already
3499 pushed. */
3501 if (goal_alt_swapped)
3503 if (lra_dump_file != NULL)
3504 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3505 INSN_UID (curr_insn));
3507 /* Swap the duplicates too. */
3508 swap_operands (commutative);
3509 change_p = true;
3512 #ifdef SECONDARY_MEMORY_NEEDED
3513 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3514 too conservatively. So we use the secondary memory only if there
3515 is no any alternative without reloads. */
3516 use_sec_mem_p = false;
3517 if (! alt_p)
3518 use_sec_mem_p = true;
3519 else if (sec_mem_p)
3521 for (i = 0; i < n_operands; i++)
3522 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3523 break;
3524 use_sec_mem_p = i < n_operands;
3527 if (use_sec_mem_p)
3529 rtx new_reg, src, dest, rld;
3530 machine_mode sec_mode, rld_mode;
3532 lra_assert (sec_mem_p);
3533 lra_assert (curr_static_id->operand[0].type == OP_OUT
3534 && curr_static_id->operand[1].type == OP_IN);
3535 dest = *curr_id->operand_loc[0];
3536 src = *curr_id->operand_loc[1];
3537 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3538 ? dest : src);
3539 rld_mode = GET_MODE (rld);
3540 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3541 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3542 #else
3543 sec_mode = rld_mode;
3544 #endif
3545 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3546 NO_REGS, "secondary");
3547 /* If the mode is changed, it should be wider. */
3548 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3549 if (sec_mode != rld_mode)
3551 /* If the target says specifically to use another mode for
3552 secondary memory moves we can not reuse the original
3553 insn. */
3554 after = emit_spill_move (false, new_reg, dest);
3555 lra_process_new_insns (curr_insn, NULL, after,
3556 "Inserting the sec. move");
3557 /* We may have non null BEFORE here (e.g. after address
3558 processing. */
3559 push_to_sequence (before);
3560 before = emit_spill_move (true, new_reg, src);
3561 emit_insn (before);
3562 before = get_insns ();
3563 end_sequence ();
3564 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3565 lra_set_insn_deleted (curr_insn);
3567 else if (dest == rld)
3569 *curr_id->operand_loc[0] = new_reg;
3570 after = emit_spill_move (false, new_reg, dest);
3571 lra_process_new_insns (curr_insn, NULL, after,
3572 "Inserting the sec. move");
3574 else
3576 *curr_id->operand_loc[1] = new_reg;
3577 /* See comments above. */
3578 push_to_sequence (before);
3579 before = emit_spill_move (true, new_reg, src);
3580 emit_insn (before);
3581 before = get_insns ();
3582 end_sequence ();
3583 lra_process_new_insns (curr_insn, before, NULL,
3584 "Inserting the sec. move");
3586 lra_update_insn_regno_info (curr_insn);
3587 return true;
3589 #endif
3591 lra_assert (goal_alt_number >= 0);
3592 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3594 if (lra_dump_file != NULL)
3596 const char *p;
3598 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3599 goal_alt_number, INSN_UID (curr_insn));
3600 for (i = 0; i < n_operands; i++)
3602 p = (curr_static_id->operand_alternative
3603 [goal_alt_number * n_operands + i].constraint);
3604 if (*p == '\0')
3605 continue;
3606 fprintf (lra_dump_file, " (%d) ", i);
3607 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3608 fputc (*p, lra_dump_file);
3610 if (INSN_CODE (curr_insn) >= 0
3611 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3612 fprintf (lra_dump_file, " {%s}", p);
3613 if (curr_id->sp_offset != 0)
3614 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3615 curr_id->sp_offset);
3616 fprintf (lra_dump_file, "\n");
3619 /* Right now, for any pair of operands I and J that are required to
3620 match, with J < I, goal_alt_matches[I] is J. Add I to
3621 goal_alt_matched[J]. */
3623 for (i = 0; i < n_operands; i++)
3624 if ((j = goal_alt_matches[i]) >= 0)
3626 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3628 /* We allow matching one output operand and several input
3629 operands. */
3630 lra_assert (k == 0
3631 || (curr_static_id->operand[j].type == OP_OUT
3632 && curr_static_id->operand[i].type == OP_IN
3633 && (curr_static_id->operand
3634 [goal_alt_matched[j][0]].type == OP_IN)));
3635 goal_alt_matched[j][k] = i;
3636 goal_alt_matched[j][k + 1] = -1;
3639 for (i = 0; i < n_operands; i++)
3640 goal_alt_win[i] |= goal_alt_match_win[i];
3642 /* Any constants that aren't allowed and can't be reloaded into
3643 registers are here changed into memory references. */
3644 for (i = 0; i < n_operands; i++)
3645 if (goal_alt_win[i])
3647 int regno;
3648 enum reg_class new_class;
3649 rtx reg = *curr_id->operand_loc[i];
3651 if (GET_CODE (reg) == SUBREG)
3652 reg = SUBREG_REG (reg);
3654 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3656 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3658 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3660 lra_assert (ok_p);
3661 lra_change_class (regno, new_class, " Change to", true);
3665 else
3667 const char *constraint;
3668 char c;
3669 rtx op = *curr_id->operand_loc[i];
3670 rtx subreg = NULL_RTX;
3671 machine_mode mode = curr_operand_mode[i];
3673 if (GET_CODE (op) == SUBREG)
3675 subreg = op;
3676 op = SUBREG_REG (op);
3677 mode = GET_MODE (op);
3680 if (CONST_POOL_OK_P (mode, op)
3681 && ((targetm.preferred_reload_class
3682 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3683 || no_input_reloads_p))
3685 rtx tem = force_const_mem (mode, op);
3687 change_p = true;
3688 if (subreg != NULL_RTX)
3689 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3691 *curr_id->operand_loc[i] = tem;
3692 lra_update_dup (curr_id, i);
3693 process_address (i, false, &before, &after);
3695 /* If the alternative accepts constant pool refs directly
3696 there will be no reload needed at all. */
3697 if (subreg != NULL_RTX)
3698 continue;
3699 /* Skip alternatives before the one requested. */
3700 constraint = (curr_static_id->operand_alternative
3701 [goal_alt_number * n_operands + i].constraint);
3702 for (;
3703 (c = *constraint) && c != ',' && c != '#';
3704 constraint += CONSTRAINT_LEN (c, constraint))
3706 enum constraint_num cn = lookup_constraint (constraint);
3707 if (insn_extra_memory_constraint (cn)
3708 && satisfies_memory_constraint_p (tem, cn))
3709 break;
3711 if (c == '\0' || c == ',' || c == '#')
3712 continue;
3714 goal_alt_win[i] = true;
3718 for (i = 0; i < n_operands; i++)
3720 int regno;
3721 bool optional_p = false;
3722 rtx old, new_reg;
3723 rtx op = *curr_id->operand_loc[i];
3725 if (goal_alt_win[i])
3727 if (goal_alt[i] == NO_REGS
3728 && REG_P (op)
3729 /* When we assign NO_REGS it means that we will not
3730 assign a hard register to the scratch pseudo by
3731 assigment pass and the scratch pseudo will be
3732 spilled. Spilled scratch pseudos are transformed
3733 back to scratches at the LRA end. */
3734 && lra_former_scratch_operand_p (curr_insn, i))
3736 int regno = REGNO (op);
3737 lra_change_class (regno, NO_REGS, " Change to", true);
3738 if (lra_get_regno_hard_regno (regno) >= 0)
3739 /* We don't have to mark all insn affected by the
3740 spilled pseudo as there is only one such insn, the
3741 current one. */
3742 reg_renumber[regno] = -1;
3744 /* We can do an optional reload. If the pseudo got a hard
3745 reg, we might improve the code through inheritance. If
3746 it does not get a hard register we coalesce memory/memory
3747 moves later. Ignore move insns to avoid cycling. */
3748 if (! lra_simple_p
3749 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3750 && goal_alt[i] != NO_REGS && REG_P (op)
3751 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3752 && regno < new_regno_start
3753 && ! lra_former_scratch_p (regno)
3754 && reg_renumber[regno] < 0
3755 /* Check that the optional reload pseudo will be able to
3756 hold given mode value. */
3757 && ! (prohibited_class_reg_set_mode_p
3758 (goal_alt[i], reg_class_contents[goal_alt[i]],
3759 PSEUDO_REGNO_MODE (regno)))
3760 && (curr_insn_set == NULL_RTX
3761 || !((REG_P (SET_SRC (curr_insn_set))
3762 || MEM_P (SET_SRC (curr_insn_set))
3763 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3764 && (REG_P (SET_DEST (curr_insn_set))
3765 || MEM_P (SET_DEST (curr_insn_set))
3766 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3767 optional_p = true;
3768 else
3769 continue;
3772 /* Operands that match previous ones have already been handled. */
3773 if (goal_alt_matches[i] >= 0)
3774 continue;
3776 /* We should not have an operand with a non-offsettable address
3777 appearing where an offsettable address will do. It also may
3778 be a case when the address should be special in other words
3779 not a general one (e.g. it needs no index reg). */
3780 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3782 enum reg_class rclass;
3783 rtx *loc = &XEXP (op, 0);
3784 enum rtx_code code = GET_CODE (*loc);
3786 push_to_sequence (before);
3787 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3788 MEM, SCRATCH);
3789 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3790 new_reg = emit_inc (rclass, *loc, *loc,
3791 /* This value does not matter for MODIFY. */
3792 GET_MODE_SIZE (GET_MODE (op)));
3793 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3794 "offsetable address", &new_reg))
3795 lra_emit_move (new_reg, *loc);
3796 before = get_insns ();
3797 end_sequence ();
3798 *loc = new_reg;
3799 lra_update_dup (curr_id, i);
3801 else if (goal_alt_matched[i][0] == -1)
3803 machine_mode mode;
3804 rtx reg, *loc;
3805 int hard_regno, byte;
3806 enum op_type type = curr_static_id->operand[i].type;
3808 loc = curr_id->operand_loc[i];
3809 mode = curr_operand_mode[i];
3810 if (GET_CODE (*loc) == SUBREG)
3812 reg = SUBREG_REG (*loc);
3813 byte = SUBREG_BYTE (*loc);
3814 if (REG_P (reg)
3815 /* Strict_low_part requires reload the register not
3816 the sub-register. */
3817 && (curr_static_id->operand[i].strict_low
3818 || (GET_MODE_SIZE (mode)
3819 <= GET_MODE_SIZE (GET_MODE (reg))
3820 && (hard_regno
3821 = get_try_hard_regno (REGNO (reg))) >= 0
3822 && (simplify_subreg_regno
3823 (hard_regno,
3824 GET_MODE (reg), byte, mode) < 0)
3825 && (goal_alt[i] == NO_REGS
3826 || (simplify_subreg_regno
3827 (ira_class_hard_regs[goal_alt[i]][0],
3828 GET_MODE (reg), byte, mode) >= 0)))))
3830 if (type == OP_OUT)
3831 type = OP_INOUT;
3832 loc = &SUBREG_REG (*loc);
3833 mode = GET_MODE (*loc);
3836 old = *loc;
3837 if (get_reload_reg (type, mode, old, goal_alt[i],
3838 loc != curr_id->operand_loc[i], "", &new_reg)
3839 && type != OP_OUT)
3841 push_to_sequence (before);
3842 lra_emit_move (new_reg, old);
3843 before = get_insns ();
3844 end_sequence ();
3846 *loc = new_reg;
3847 if (type != OP_IN
3848 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3850 start_sequence ();
3851 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3852 emit_insn (after);
3853 after = get_insns ();
3854 end_sequence ();
3855 *loc = new_reg;
3857 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3858 if (goal_alt_dont_inherit_ops[j] == i)
3860 lra_set_regno_unique_value (REGNO (new_reg));
3861 break;
3863 lra_update_dup (curr_id, i);
3865 else if (curr_static_id->operand[i].type == OP_IN
3866 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3867 == OP_OUT))
3869 /* generate reloads for input and matched outputs. */
3870 match_inputs[0] = i;
3871 match_inputs[1] = -1;
3872 match_reload (goal_alt_matched[i][0], match_inputs,
3873 goal_alt[i], &before, &after,
3874 curr_static_id->operand_alternative
3875 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
3876 .earlyclobber);
3878 else if (curr_static_id->operand[i].type == OP_OUT
3879 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3880 == OP_IN))
3881 /* Generate reloads for output and matched inputs. */
3882 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after,
3883 curr_static_id->operand_alternative
3884 [goal_alt_number * n_operands + i].earlyclobber);
3885 else if (curr_static_id->operand[i].type == OP_IN
3886 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3887 == OP_IN))
3889 /* Generate reloads for matched inputs. */
3890 match_inputs[0] = i;
3891 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3892 match_inputs[j + 1] = k;
3893 match_inputs[j + 1] = -1;
3894 match_reload (-1, match_inputs, goal_alt[i], &before, &after, false);
3896 else
3897 /* We must generate code in any case when function
3898 process_alt_operands decides that it is possible. */
3899 gcc_unreachable ();
3900 if (optional_p)
3902 lra_assert (REG_P (op));
3903 regno = REGNO (op);
3904 op = *curr_id->operand_loc[i]; /* Substitution. */
3905 if (GET_CODE (op) == SUBREG)
3906 op = SUBREG_REG (op);
3907 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3908 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3909 lra_reg_info[REGNO (op)].restore_regno = regno;
3910 if (lra_dump_file != NULL)
3911 fprintf (lra_dump_file,
3912 " Making reload reg %d for reg %d optional\n",
3913 REGNO (op), regno);
3916 if (before != NULL_RTX || after != NULL_RTX
3917 || max_regno_before != max_reg_num ())
3918 change_p = true;
3919 if (change_p)
3921 lra_update_operator_dups (curr_id);
3922 /* Something changes -- process the insn. */
3923 lra_update_insn_regno_info (curr_insn);
3925 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3926 return change_p;
3929 /* Return true if INSN satisfies all constraints. In other words, no
3930 reload insns are needed. */
3931 bool
3932 lra_constrain_insn (rtx_insn *insn)
3934 int saved_new_regno_start = new_regno_start;
3935 int saved_new_insn_uid_start = new_insn_uid_start;
3936 bool change_p;
3938 curr_insn = insn;
3939 curr_id = lra_get_insn_recog_data (curr_insn);
3940 curr_static_id = curr_id->insn_static_data;
3941 new_insn_uid_start = get_max_uid ();
3942 new_regno_start = max_reg_num ();
3943 change_p = curr_insn_transform (true);
3944 new_regno_start = saved_new_regno_start;
3945 new_insn_uid_start = saved_new_insn_uid_start;
3946 return ! change_p;
3949 /* Return true if X is in LIST. */
3950 static bool
3951 in_list_p (rtx x, rtx list)
3953 for (; list != NULL_RTX; list = XEXP (list, 1))
3954 if (XEXP (list, 0) == x)
3955 return true;
3956 return false;
3959 /* Return true if X contains an allocatable hard register (if
3960 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3961 static bool
3962 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3964 int i, j;
3965 const char *fmt;
3966 enum rtx_code code;
3968 code = GET_CODE (x);
3969 if (REG_P (x))
3971 int regno = REGNO (x);
3972 HARD_REG_SET alloc_regs;
3974 if (hard_reg_p)
3976 if (regno >= FIRST_PSEUDO_REGISTER)
3977 regno = lra_get_regno_hard_regno (regno);
3978 if (regno < 0)
3979 return false;
3980 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3981 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3983 else
3985 if (regno < FIRST_PSEUDO_REGISTER)
3986 return false;
3987 if (! spilled_p)
3988 return true;
3989 return lra_get_regno_hard_regno (regno) < 0;
3992 fmt = GET_RTX_FORMAT (code);
3993 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3995 if (fmt[i] == 'e')
3997 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3998 return true;
4000 else if (fmt[i] == 'E')
4002 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4003 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4004 return true;
4007 return false;
4010 /* Return true if X contains a symbol reg. */
4011 static bool
4012 contains_symbol_ref_p (rtx x)
4014 int i, j;
4015 const char *fmt;
4016 enum rtx_code code;
4018 code = GET_CODE (x);
4019 if (code == SYMBOL_REF)
4020 return true;
4021 fmt = GET_RTX_FORMAT (code);
4022 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4024 if (fmt[i] == 'e')
4026 if (contains_symbol_ref_p (XEXP (x, i)))
4027 return true;
4029 else if (fmt[i] == 'E')
4031 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4032 if (contains_symbol_ref_p (XVECEXP (x, i, j)))
4033 return true;
4036 return false;
4039 /* Process all regs in location *LOC and change them on equivalent
4040 substitution. Return true if any change was done. */
4041 static bool
4042 loc_equivalence_change_p (rtx *loc)
4044 rtx subst, reg, x = *loc;
4045 bool result = false;
4046 enum rtx_code code = GET_CODE (x);
4047 const char *fmt;
4048 int i, j;
4050 if (code == SUBREG)
4052 reg = SUBREG_REG (x);
4053 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4054 && GET_MODE (subst) == VOIDmode)
4056 /* We cannot reload debug location. Simplify subreg here
4057 while we know the inner mode. */
4058 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4059 GET_MODE (reg), SUBREG_BYTE (x));
4060 return true;
4063 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4065 *loc = subst;
4066 return true;
4069 /* Scan all the operand sub-expressions. */
4070 fmt = GET_RTX_FORMAT (code);
4071 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4073 if (fmt[i] == 'e')
4074 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4075 else if (fmt[i] == 'E')
4076 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4077 result
4078 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4080 return result;
4083 /* Similar to loc_equivalence_change_p, but for use as
4084 simplify_replace_fn_rtx callback. DATA is insn for which the
4085 elimination is done. If it null we don't do the elimination. */
4086 static rtx
4087 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4089 if (!REG_P (loc))
4090 return NULL_RTX;
4092 rtx subst = (data == NULL
4093 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4094 if (subst != loc)
4095 return subst;
4097 return NULL_RTX;
4100 /* Maximum number of generated reload insns per an insn. It is for
4101 preventing this pass cycling in a bug case. */
4102 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4104 /* The current iteration number of this LRA pass. */
4105 int lra_constraint_iter;
4107 /* True if we substituted equiv which needs checking register
4108 allocation correctness because the equivalent value contains
4109 allocatable hard registers or when we restore multi-register
4110 pseudo. */
4111 bool lra_risky_transformations_p;
4113 /* Return true if REGNO is referenced in more than one block. */
4114 static bool
4115 multi_block_pseudo_p (int regno)
4117 basic_block bb = NULL;
4118 unsigned int uid;
4119 bitmap_iterator bi;
4121 if (regno < FIRST_PSEUDO_REGISTER)
4122 return false;
4124 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4125 if (bb == NULL)
4126 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4127 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4128 return true;
4129 return false;
4132 /* Return true if LIST contains a deleted insn. */
4133 static bool
4134 contains_deleted_insn_p (rtx_insn_list *list)
4136 for (; list != NULL_RTX; list = list->next ())
4137 if (NOTE_P (list->insn ())
4138 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4139 return true;
4140 return false;
4143 /* Return true if X contains a pseudo dying in INSN. */
4144 static bool
4145 dead_pseudo_p (rtx x, rtx_insn *insn)
4147 int i, j;
4148 const char *fmt;
4149 enum rtx_code code;
4151 if (REG_P (x))
4152 return (insn != NULL_RTX
4153 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4154 code = GET_CODE (x);
4155 fmt = GET_RTX_FORMAT (code);
4156 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4158 if (fmt[i] == 'e')
4160 if (dead_pseudo_p (XEXP (x, i), insn))
4161 return true;
4163 else if (fmt[i] == 'E')
4165 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4166 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4167 return true;
4170 return false;
4173 /* Return true if INSN contains a dying pseudo in INSN right hand
4174 side. */
4175 static bool
4176 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4178 rtx set = single_set (insn);
4180 gcc_assert (set != NULL);
4181 return dead_pseudo_p (SET_SRC (set), insn);
4184 /* Return true if any init insn of REGNO contains a dying pseudo in
4185 insn right hand side. */
4186 static bool
4187 init_insn_rhs_dead_pseudo_p (int regno)
4189 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4191 if (insns == NULL)
4192 return false;
4193 for (; insns != NULL_RTX; insns = insns->next ())
4194 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4195 return true;
4196 return false;
4199 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4200 reverse only if we have one init insn with given REGNO as a
4201 source. */
4202 static bool
4203 reverse_equiv_p (int regno)
4205 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4206 rtx set;
4208 if (insns == NULL)
4209 return false;
4210 if (! INSN_P (insns->insn ())
4211 || insns->next () != NULL)
4212 return false;
4213 if ((set = single_set (insns->insn ())) == NULL_RTX)
4214 return false;
4215 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4218 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4219 call this function only for non-reverse equivalence. */
4220 static bool
4221 contains_reloaded_insn_p (int regno)
4223 rtx set;
4224 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4226 for (; list != NULL; list = list->next ())
4227 if ((set = single_set (list->insn ())) == NULL_RTX
4228 || ! REG_P (SET_DEST (set))
4229 || (int) REGNO (SET_DEST (set)) != regno)
4230 return true;
4231 return false;
4234 /* Entry function of LRA constraint pass. Return true if the
4235 constraint pass did change the code. */
4236 bool
4237 lra_constraints (bool first_p)
4239 bool changed_p;
4240 int i, hard_regno, new_insns_num;
4241 unsigned int min_len, new_min_len, uid;
4242 rtx set, x, reg, dest_reg;
4243 basic_block last_bb;
4244 bitmap_head equiv_insn_bitmap;
4245 bitmap_iterator bi;
4247 lra_constraint_iter++;
4248 if (lra_dump_file != NULL)
4249 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4250 lra_constraint_iter);
4251 changed_p = false;
4252 if (pic_offset_table_rtx
4253 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4254 lra_risky_transformations_p = true;
4255 else
4256 lra_risky_transformations_p = false;
4257 new_insn_uid_start = get_max_uid ();
4258 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4259 /* Mark used hard regs for target stack size calulations. */
4260 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4261 if (lra_reg_info[i].nrefs != 0
4262 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4264 int j, nregs;
4266 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4267 for (j = 0; j < nregs; j++)
4268 df_set_regs_ever_live (hard_regno + j, true);
4270 /* Do elimination before the equivalence processing as we can spill
4271 some pseudos during elimination. */
4272 lra_eliminate (false, first_p);
4273 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4274 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4275 if (lra_reg_info[i].nrefs != 0)
4277 ira_reg_equiv[i].profitable_p = true;
4278 reg = regno_reg_rtx[i];
4279 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4281 bool pseudo_p = contains_reg_p (x, false, false);
4283 /* After RTL transformation, we can not guarantee that
4284 pseudo in the substitution was not reloaded which might
4285 make equivalence invalid. For example, in reverse
4286 equiv of p0
4288 p0 <- ...
4290 equiv_mem <- p0
4292 the memory address register was reloaded before the 2nd
4293 insn. */
4294 if ((! first_p && pseudo_p)
4295 /* We don't use DF for compilation speed sake. So it
4296 is problematic to update live info when we use an
4297 equivalence containing pseudos in more than one
4298 BB. */
4299 || (pseudo_p && multi_block_pseudo_p (i))
4300 /* If an init insn was deleted for some reason, cancel
4301 the equiv. We could update the equiv insns after
4302 transformations including an equiv insn deletion
4303 but it is not worthy as such cases are extremely
4304 rare. */
4305 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4306 /* If it is not a reverse equivalence, we check that a
4307 pseudo in rhs of the init insn is not dying in the
4308 insn. Otherwise, the live info at the beginning of
4309 the corresponding BB might be wrong after we
4310 removed the insn. When the equiv can be a
4311 constant, the right hand side of the init insn can
4312 be a pseudo. */
4313 || (! reverse_equiv_p (i)
4314 && (init_insn_rhs_dead_pseudo_p (i)
4315 /* If we reloaded the pseudo in an equivalence
4316 init insn, we can not remove the equiv init
4317 insns and the init insns might write into
4318 const memory in this case. */
4319 || contains_reloaded_insn_p (i)))
4320 /* Prevent access beyond equivalent memory for
4321 paradoxical subregs. */
4322 || (MEM_P (x)
4323 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4324 > GET_MODE_SIZE (GET_MODE (x))))
4325 || (pic_offset_table_rtx
4326 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4327 && (targetm.preferred_reload_class
4328 (x, lra_get_allocno_class (i)) == NO_REGS))
4329 || contains_symbol_ref_p (x))))
4330 ira_reg_equiv[i].defined_p = false;
4331 if (contains_reg_p (x, false, true))
4332 ira_reg_equiv[i].profitable_p = false;
4333 if (get_equiv (reg) != reg)
4334 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4337 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4338 update_equiv (i);
4339 /* We should add all insns containing pseudos which should be
4340 substituted by their equivalences. */
4341 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4342 lra_push_insn_by_uid (uid);
4343 min_len = lra_insn_stack_length ();
4344 new_insns_num = 0;
4345 last_bb = NULL;
4346 changed_p = false;
4347 while ((new_min_len = lra_insn_stack_length ()) != 0)
4349 curr_insn = lra_pop_insn ();
4350 --new_min_len;
4351 curr_bb = BLOCK_FOR_INSN (curr_insn);
4352 if (curr_bb != last_bb)
4354 last_bb = curr_bb;
4355 bb_reload_num = lra_curr_reload_num;
4357 if (min_len > new_min_len)
4359 min_len = new_min_len;
4360 new_insns_num = 0;
4362 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4363 internal_error
4364 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4365 MAX_RELOAD_INSNS_NUMBER);
4366 new_insns_num++;
4367 if (DEBUG_INSN_P (curr_insn))
4369 /* We need to check equivalence in debug insn and change
4370 pseudo to the equivalent value if necessary. */
4371 curr_id = lra_get_insn_recog_data (curr_insn);
4372 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4374 rtx old = *curr_id->operand_loc[0];
4375 *curr_id->operand_loc[0]
4376 = simplify_replace_fn_rtx (old, NULL_RTX,
4377 loc_equivalence_callback, curr_insn);
4378 if (old != *curr_id->operand_loc[0])
4380 lra_update_insn_regno_info (curr_insn);
4381 changed_p = true;
4385 else if (INSN_P (curr_insn))
4387 if ((set = single_set (curr_insn)) != NULL_RTX)
4389 dest_reg = SET_DEST (set);
4390 /* The equivalence pseudo could be set up as SUBREG in a
4391 case when it is a call restore insn in a mode
4392 different from the pseudo mode. */
4393 if (GET_CODE (dest_reg) == SUBREG)
4394 dest_reg = SUBREG_REG (dest_reg);
4395 if ((REG_P (dest_reg)
4396 && (x = get_equiv (dest_reg)) != dest_reg
4397 /* Remove insns which set up a pseudo whose value
4398 can not be changed. Such insns might be not in
4399 init_insns because we don't update equiv data
4400 during insn transformations.
4402 As an example, let suppose that a pseudo got
4403 hard register and on the 1st pass was not
4404 changed to equivalent constant. We generate an
4405 additional insn setting up the pseudo because of
4406 secondary memory movement. Then the pseudo is
4407 spilled and we use the equiv constant. In this
4408 case we should remove the additional insn and
4409 this insn is not init_insns list. */
4410 && (! MEM_P (x) || MEM_READONLY_P (x)
4411 /* Check that this is actually an insn setting
4412 up the equivalence. */
4413 || in_list_p (curr_insn,
4414 ira_reg_equiv
4415 [REGNO (dest_reg)].init_insns)))
4416 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4417 && in_list_p (curr_insn,
4418 ira_reg_equiv
4419 [REGNO (SET_SRC (set))].init_insns)))
4421 /* This is equiv init insn of pseudo which did not get a
4422 hard register -- remove the insn. */
4423 if (lra_dump_file != NULL)
4425 fprintf (lra_dump_file,
4426 " Removing equiv init insn %i (freq=%d)\n",
4427 INSN_UID (curr_insn),
4428 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4429 dump_insn_slim (lra_dump_file, curr_insn);
4431 if (contains_reg_p (x, true, false))
4432 lra_risky_transformations_p = true;
4433 lra_set_insn_deleted (curr_insn);
4434 continue;
4437 curr_id = lra_get_insn_recog_data (curr_insn);
4438 curr_static_id = curr_id->insn_static_data;
4439 init_curr_insn_input_reloads ();
4440 init_curr_operand_mode ();
4441 if (curr_insn_transform (false))
4442 changed_p = true;
4443 /* Check non-transformed insns too for equiv change as USE
4444 or CLOBBER don't need reloads but can contain pseudos
4445 being changed on their equivalences. */
4446 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4447 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4449 lra_update_insn_regno_info (curr_insn);
4450 changed_p = true;
4454 bitmap_clear (&equiv_insn_bitmap);
4455 /* If we used a new hard regno, changed_p should be true because the
4456 hard reg is assigned to a new pseudo. */
4457 #ifdef ENABLE_CHECKING
4458 if (! changed_p)
4460 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4461 if (lra_reg_info[i].nrefs != 0
4462 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4464 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4466 for (j = 0; j < nregs; j++)
4467 lra_assert (df_regs_ever_live_p (hard_regno + j));
4470 #endif
4471 return changed_p;
4474 /* Initiate the LRA constraint pass. It is done once per
4475 function. */
4476 void
4477 lra_constraints_init (void)
4481 /* Finalize the LRA constraint pass. It is done once per
4482 function. */
4483 void
4484 lra_constraints_finish (void)
4490 /* This page contains code to do inheritance/split
4491 transformations. */
4493 /* Number of reloads passed so far in current EBB. */
4494 static int reloads_num;
4496 /* Number of calls passed so far in current EBB. */
4497 static int calls_num;
4499 /* Current reload pseudo check for validity of elements in
4500 USAGE_INSNS. */
4501 static int curr_usage_insns_check;
4503 /* Info about last usage of registers in EBB to do inheritance/split
4504 transformation. Inheritance transformation is done from a spilled
4505 pseudo and split transformations from a hard register or a pseudo
4506 assigned to a hard register. */
4507 struct usage_insns
4509 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4510 value INSNS is valid. The insns is chain of optional debug insns
4511 and a finishing non-debug insn using the corresponding reg. The
4512 value is also used to mark the registers which are set up in the
4513 current insn. The negated insn uid is used for this. */
4514 int check;
4515 /* Value of global reloads_num at the last insn in INSNS. */
4516 int reloads_num;
4517 /* Value of global reloads_nums at the last insn in INSNS. */
4518 int calls_num;
4519 /* It can be true only for splitting. And it means that the restore
4520 insn should be put after insn given by the following member. */
4521 bool after_p;
4522 /* Next insns in the current EBB which use the original reg and the
4523 original reg value is not changed between the current insn and
4524 the next insns. In order words, e.g. for inheritance, if we need
4525 to use the original reg value again in the next insns we can try
4526 to use the value in a hard register from a reload insn of the
4527 current insn. */
4528 rtx insns;
4531 /* Map: regno -> corresponding pseudo usage insns. */
4532 static struct usage_insns *usage_insns;
4534 static void
4535 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4537 usage_insns[regno].check = curr_usage_insns_check;
4538 usage_insns[regno].insns = insn;
4539 usage_insns[regno].reloads_num = reloads_num;
4540 usage_insns[regno].calls_num = calls_num;
4541 usage_insns[regno].after_p = after_p;
4544 /* The function is used to form list REGNO usages which consists of
4545 optional debug insns finished by a non-debug insn using REGNO.
4546 RELOADS_NUM is current number of reload insns processed so far. */
4547 static void
4548 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
4550 rtx next_usage_insns;
4552 if (usage_insns[regno].check == curr_usage_insns_check
4553 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4554 && DEBUG_INSN_P (insn))
4556 /* Check that we did not add the debug insn yet. */
4557 if (next_usage_insns != insn
4558 && (GET_CODE (next_usage_insns) != INSN_LIST
4559 || XEXP (next_usage_insns, 0) != insn))
4560 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4561 next_usage_insns);
4563 else if (NONDEBUG_INSN_P (insn))
4564 setup_next_usage_insn (regno, insn, reloads_num, false);
4565 else
4566 usage_insns[regno].check = 0;
4569 /* Return first non-debug insn in list USAGE_INSNS. */
4570 static rtx_insn *
4571 skip_usage_debug_insns (rtx usage_insns)
4573 rtx insn;
4575 /* Skip debug insns. */
4576 for (insn = usage_insns;
4577 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4578 insn = XEXP (insn, 1))
4580 return safe_as_a <rtx_insn *> (insn);
4583 /* Return true if we need secondary memory moves for insn in
4584 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4585 into the insn. */
4586 static bool
4587 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4588 rtx usage_insns ATTRIBUTE_UNUSED)
4590 #ifndef SECONDARY_MEMORY_NEEDED
4591 return false;
4592 #else
4593 rtx_insn *insn;
4594 rtx set, dest;
4595 enum reg_class cl;
4597 if (inher_cl == ALL_REGS
4598 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4599 return false;
4600 lra_assert (INSN_P (insn));
4601 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4602 return false;
4603 dest = SET_DEST (set);
4604 if (! REG_P (dest))
4605 return false;
4606 lra_assert (inher_cl != NO_REGS);
4607 cl = get_reg_class (REGNO (dest));
4608 return (cl != NO_REGS && cl != ALL_REGS
4609 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4610 #endif
4613 /* Registers involved in inheritance/split in the current EBB
4614 (inheritance/split pseudos and original registers). */
4615 static bitmap_head check_only_regs;
4617 /* Do inheritance transformations for insn INSN, which defines (if
4618 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4619 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4620 form as the "insns" field of usage_insns. Return true if we
4621 succeed in such transformation.
4623 The transformations look like:
4625 p <- ... i <- ...
4626 ... p <- i (new insn)
4627 ... =>
4628 <- ... p ... <- ... i ...
4630 ... i <- p (new insn)
4631 <- ... p ... <- ... i ...
4632 ... =>
4633 <- ... p ... <- ... i ...
4634 where p is a spilled original pseudo and i is a new inheritance pseudo.
4637 The inheritance pseudo has the smallest class of two classes CL and
4638 class of ORIGINAL REGNO. */
4639 static bool
4640 inherit_reload_reg (bool def_p, int original_regno,
4641 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4643 if (optimize_function_for_size_p (cfun))
4644 return false;
4646 enum reg_class rclass = lra_get_allocno_class (original_regno);
4647 rtx original_reg = regno_reg_rtx[original_regno];
4648 rtx new_reg, usage_insn;
4649 rtx_insn *new_insns;
4651 lra_assert (! usage_insns[original_regno].after_p);
4652 if (lra_dump_file != NULL)
4653 fprintf (lra_dump_file,
4654 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4655 if (! ira_reg_classes_intersect_p[cl][rclass])
4657 if (lra_dump_file != NULL)
4659 fprintf (lra_dump_file,
4660 " Rejecting inheritance for %d "
4661 "because of disjoint classes %s and %s\n",
4662 original_regno, reg_class_names[cl],
4663 reg_class_names[rclass]);
4664 fprintf (lra_dump_file,
4665 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4667 return false;
4669 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4670 /* We don't use a subset of two classes because it can be
4671 NO_REGS. This transformation is still profitable in most
4672 cases even if the classes are not intersected as register
4673 move is probably cheaper than a memory load. */
4674 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4676 if (lra_dump_file != NULL)
4677 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4678 reg_class_names[cl], reg_class_names[rclass]);
4680 rclass = cl;
4682 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4684 /* Reject inheritance resulting in secondary memory moves.
4685 Otherwise, there is a danger in LRA cycling. Also such
4686 transformation will be unprofitable. */
4687 if (lra_dump_file != NULL)
4689 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4690 rtx set = single_set (insn);
4692 lra_assert (set != NULL_RTX);
4694 rtx dest = SET_DEST (set);
4696 lra_assert (REG_P (dest));
4697 fprintf (lra_dump_file,
4698 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4699 "as secondary mem is needed\n",
4700 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4701 original_regno, reg_class_names[rclass]);
4702 fprintf (lra_dump_file,
4703 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4705 return false;
4707 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4708 rclass, "inheritance");
4709 start_sequence ();
4710 if (def_p)
4711 lra_emit_move (original_reg, new_reg);
4712 else
4713 lra_emit_move (new_reg, original_reg);
4714 new_insns = get_insns ();
4715 end_sequence ();
4716 if (NEXT_INSN (new_insns) != NULL_RTX)
4718 if (lra_dump_file != NULL)
4720 fprintf (lra_dump_file,
4721 " Rejecting inheritance %d->%d "
4722 "as it results in 2 or more insns:\n",
4723 original_regno, REGNO (new_reg));
4724 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4725 fprintf (lra_dump_file,
4726 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4728 return false;
4730 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
4731 lra_update_insn_regno_info (insn);
4732 if (! def_p)
4733 /* We now have a new usage insn for original regno. */
4734 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4735 if (lra_dump_file != NULL)
4736 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4737 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4738 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4739 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4740 bitmap_set_bit (&check_only_regs, original_regno);
4741 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4742 if (def_p)
4743 lra_process_new_insns (insn, NULL, new_insns,
4744 "Add original<-inheritance");
4745 else
4746 lra_process_new_insns (insn, new_insns, NULL,
4747 "Add inheritance<-original");
4748 while (next_usage_insns != NULL_RTX)
4750 if (GET_CODE (next_usage_insns) != INSN_LIST)
4752 usage_insn = next_usage_insns;
4753 lra_assert (NONDEBUG_INSN_P (usage_insn));
4754 next_usage_insns = NULL;
4756 else
4758 usage_insn = XEXP (next_usage_insns, 0);
4759 lra_assert (DEBUG_INSN_P (usage_insn));
4760 next_usage_insns = XEXP (next_usage_insns, 1);
4762 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
4763 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4764 if (lra_dump_file != NULL)
4766 fprintf (lra_dump_file,
4767 " Inheritance reuse change %d->%d (bb%d):\n",
4768 original_regno, REGNO (new_reg),
4769 BLOCK_FOR_INSN (usage_insn)->index);
4770 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
4773 if (lra_dump_file != NULL)
4774 fprintf (lra_dump_file,
4775 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4776 return true;
4779 /* Return true if we need a caller save/restore for pseudo REGNO which
4780 was assigned to a hard register. */
4781 static inline bool
4782 need_for_call_save_p (int regno)
4784 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4785 return (usage_insns[regno].calls_num < calls_num
4786 && (overlaps_hard_reg_set_p
4787 ((flag_ipa_ra &&
4788 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4789 ? lra_reg_info[regno].actual_call_used_reg_set
4790 : call_used_reg_set,
4791 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4792 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4793 PSEUDO_REGNO_MODE (regno))));
4796 /* Global registers occurring in the current EBB. */
4797 static bitmap_head ebb_global_regs;
4799 /* Return true if we need a split for hard register REGNO or pseudo
4800 REGNO which was assigned to a hard register.
4801 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4802 used for reloads since the EBB end. It is an approximation of the
4803 used hard registers in the split range. The exact value would
4804 require expensive calculations. If we were aggressive with
4805 splitting because of the approximation, the split pseudo will save
4806 the same hard register assignment and will be removed in the undo
4807 pass. We still need the approximation because too aggressive
4808 splitting would result in too inaccurate cost calculation in the
4809 assignment pass because of too many generated moves which will be
4810 probably removed in the undo pass. */
4811 static inline bool
4812 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4814 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4816 lra_assert (hard_regno >= 0);
4817 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4818 /* Don't split eliminable hard registers, otherwise we can
4819 split hard registers like hard frame pointer, which
4820 lives on BB start/end according to DF-infrastructure,
4821 when there is a pseudo assigned to the register and
4822 living in the same BB. */
4823 && (regno >= FIRST_PSEUDO_REGISTER
4824 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4825 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4826 /* Don't split call clobbered hard regs living through
4827 calls, otherwise we might have a check problem in the
4828 assign sub-pass as in the most cases (exception is a
4829 situation when lra_risky_transformations_p value is
4830 true) the assign pass assumes that all pseudos living
4831 through calls are assigned to call saved hard regs. */
4832 && (regno >= FIRST_PSEUDO_REGISTER
4833 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4834 || usage_insns[regno].calls_num == calls_num)
4835 /* We need at least 2 reloads to make pseudo splitting
4836 profitable. We should provide hard regno splitting in
4837 any case to solve 1st insn scheduling problem when
4838 moving hard register definition up might result in
4839 impossibility to find hard register for reload pseudo of
4840 small register class. */
4841 && (usage_insns[regno].reloads_num
4842 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4843 && (regno < FIRST_PSEUDO_REGISTER
4844 /* For short living pseudos, spilling + inheritance can
4845 be considered a substitution for splitting.
4846 Therefore we do not splitting for local pseudos. It
4847 decreases also aggressiveness of splitting. The
4848 minimal number of references is chosen taking into
4849 account that for 2 references splitting has no sense
4850 as we can just spill the pseudo. */
4851 || (regno >= FIRST_PSEUDO_REGISTER
4852 && lra_reg_info[regno].nrefs > 3
4853 && bitmap_bit_p (&ebb_global_regs, regno))))
4854 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4857 /* Return class for the split pseudo created from original pseudo with
4858 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4859 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4860 results in no secondary memory movements. */
4861 static enum reg_class
4862 choose_split_class (enum reg_class allocno_class,
4863 int hard_regno ATTRIBUTE_UNUSED,
4864 machine_mode mode ATTRIBUTE_UNUSED)
4866 #ifndef SECONDARY_MEMORY_NEEDED
4867 return allocno_class;
4868 #else
4869 int i;
4870 enum reg_class cl, best_cl = NO_REGS;
4871 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4872 = REGNO_REG_CLASS (hard_regno);
4874 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4875 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4876 return allocno_class;
4877 for (i = 0;
4878 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4879 i++)
4880 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4881 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4882 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4883 && (best_cl == NO_REGS
4884 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4885 best_cl = cl;
4886 return best_cl;
4887 #endif
4890 /* Do split transformations for insn INSN, which defines or uses
4891 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4892 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4893 "insns" field of usage_insns.
4895 The transformations look like:
4897 p <- ... p <- ...
4898 ... s <- p (new insn -- save)
4899 ... =>
4900 ... p <- s (new insn -- restore)
4901 <- ... p ... <- ... p ...
4903 <- ... p ... <- ... p ...
4904 ... s <- p (new insn -- save)
4905 ... =>
4906 ... p <- s (new insn -- restore)
4907 <- ... p ... <- ... p ...
4909 where p is an original pseudo got a hard register or a hard
4910 register and s is a new split pseudo. The save is put before INSN
4911 if BEFORE_P is true. Return true if we succeed in such
4912 transformation. */
4913 static bool
4914 split_reg (bool before_p, int original_regno, rtx_insn *insn,
4915 rtx next_usage_insns)
4917 enum reg_class rclass;
4918 rtx original_reg;
4919 int hard_regno, nregs;
4920 rtx new_reg, usage_insn;
4921 rtx_insn *restore, *save;
4922 bool after_p;
4923 bool call_save_p;
4925 if (original_regno < FIRST_PSEUDO_REGISTER)
4927 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4928 hard_regno = original_regno;
4929 call_save_p = false;
4930 nregs = 1;
4932 else
4934 hard_regno = reg_renumber[original_regno];
4935 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4936 rclass = lra_get_allocno_class (original_regno);
4937 original_reg = regno_reg_rtx[original_regno];
4938 call_save_p = need_for_call_save_p (original_regno);
4940 original_reg = regno_reg_rtx[original_regno];
4941 lra_assert (hard_regno >= 0);
4942 if (lra_dump_file != NULL)
4943 fprintf (lra_dump_file,
4944 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4945 if (call_save_p)
4947 machine_mode mode = GET_MODE (original_reg);
4949 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4950 hard_regno_nregs[hard_regno][mode],
4951 mode);
4952 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4954 else
4956 rclass = choose_split_class (rclass, hard_regno,
4957 GET_MODE (original_reg));
4958 if (rclass == NO_REGS)
4960 if (lra_dump_file != NULL)
4962 fprintf (lra_dump_file,
4963 " Rejecting split of %d(%s): "
4964 "no good reg class for %d(%s)\n",
4965 original_regno,
4966 reg_class_names[lra_get_allocno_class (original_regno)],
4967 hard_regno,
4968 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4969 fprintf
4970 (lra_dump_file,
4971 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4973 return false;
4975 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4976 rclass, "split");
4977 reg_renumber[REGNO (new_reg)] = hard_regno;
4979 save = emit_spill_move (true, new_reg, original_reg);
4980 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
4982 if (lra_dump_file != NULL)
4984 fprintf
4985 (lra_dump_file,
4986 " Rejecting split %d->%d resulting in > 2 save insns:\n",
4987 original_regno, REGNO (new_reg));
4988 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
4989 fprintf (lra_dump_file,
4990 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4992 return false;
4994 restore = emit_spill_move (false, new_reg, original_reg);
4995 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
4997 if (lra_dump_file != NULL)
4999 fprintf (lra_dump_file,
5000 " Rejecting split %d->%d "
5001 "resulting in > 2 restore insns:\n",
5002 original_regno, REGNO (new_reg));
5003 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
5004 fprintf (lra_dump_file,
5005 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5007 return false;
5009 after_p = usage_insns[original_regno].after_p;
5010 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
5011 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5012 bitmap_set_bit (&check_only_regs, original_regno);
5013 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
5014 for (;;)
5016 if (GET_CODE (next_usage_insns) != INSN_LIST)
5018 usage_insn = next_usage_insns;
5019 break;
5021 usage_insn = XEXP (next_usage_insns, 0);
5022 lra_assert (DEBUG_INSN_P (usage_insn));
5023 next_usage_insns = XEXP (next_usage_insns, 1);
5024 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5025 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5026 if (lra_dump_file != NULL)
5028 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5029 original_regno, REGNO (new_reg));
5030 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5033 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5034 lra_assert (usage_insn != insn || (after_p && before_p));
5035 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5036 after_p ? NULL : restore,
5037 after_p ? restore : NULL,
5038 call_save_p
5039 ? "Add reg<-save" : "Add reg<-split");
5040 lra_process_new_insns (insn, before_p ? save : NULL,
5041 before_p ? NULL : save,
5042 call_save_p
5043 ? "Add save<-reg" : "Add split<-reg");
5044 if (nregs > 1)
5045 /* If we are trying to split multi-register. We should check
5046 conflicts on the next assignment sub-pass. IRA can allocate on
5047 sub-register levels, LRA do this on pseudos level right now and
5048 this discrepancy may create allocation conflicts after
5049 splitting. */
5050 lra_risky_transformations_p = true;
5051 if (lra_dump_file != NULL)
5052 fprintf (lra_dump_file,
5053 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5054 return true;
5057 /* Recognize that we need a split transformation for insn INSN, which
5058 defines or uses REGNO in its insn biggest MODE (we use it only if
5059 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5060 hard registers which might be used for reloads since the EBB end.
5061 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5062 uid before starting INSN processing. Return true if we succeed in
5063 such transformation. */
5064 static bool
5065 split_if_necessary (int regno, machine_mode mode,
5066 HARD_REG_SET potential_reload_hard_regs,
5067 bool before_p, rtx_insn *insn, int max_uid)
5069 bool res = false;
5070 int i, nregs = 1;
5071 rtx next_usage_insns;
5073 if (regno < FIRST_PSEUDO_REGISTER)
5074 nregs = hard_regno_nregs[regno][mode];
5075 for (i = 0; i < nregs; i++)
5076 if (usage_insns[regno + i].check == curr_usage_insns_check
5077 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5078 /* To avoid processing the register twice or more. */
5079 && ((GET_CODE (next_usage_insns) != INSN_LIST
5080 && INSN_UID (next_usage_insns) < max_uid)
5081 || (GET_CODE (next_usage_insns) == INSN_LIST
5082 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5083 && need_for_split_p (potential_reload_hard_regs, regno + i)
5084 && split_reg (before_p, regno + i, insn, next_usage_insns))
5085 res = true;
5086 return res;
5089 /* Check only registers living at the current program point in the
5090 current EBB. */
5091 static bitmap_head live_regs;
5093 /* Update live info in EBB given by its HEAD and TAIL insns after
5094 inheritance/split transformation. The function removes dead moves
5095 too. */
5096 static void
5097 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5099 unsigned int j;
5100 int i, regno;
5101 bool live_p;
5102 rtx_insn *prev_insn;
5103 rtx set;
5104 bool remove_p;
5105 basic_block last_bb, prev_bb, curr_bb;
5106 bitmap_iterator bi;
5107 struct lra_insn_reg *reg;
5108 edge e;
5109 edge_iterator ei;
5111 last_bb = BLOCK_FOR_INSN (tail);
5112 prev_bb = NULL;
5113 for (curr_insn = tail;
5114 curr_insn != PREV_INSN (head);
5115 curr_insn = prev_insn)
5117 prev_insn = PREV_INSN (curr_insn);
5118 /* We need to process empty blocks too. They contain
5119 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5120 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5121 continue;
5122 curr_bb = BLOCK_FOR_INSN (curr_insn);
5123 if (curr_bb != prev_bb)
5125 if (prev_bb != NULL)
5127 /* Update df_get_live_in (prev_bb): */
5128 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5129 if (bitmap_bit_p (&live_regs, j))
5130 bitmap_set_bit (df_get_live_in (prev_bb), j);
5131 else
5132 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5134 if (curr_bb != last_bb)
5136 /* Update df_get_live_out (curr_bb): */
5137 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5139 live_p = bitmap_bit_p (&live_regs, j);
5140 if (! live_p)
5141 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5142 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5144 live_p = true;
5145 break;
5147 if (live_p)
5148 bitmap_set_bit (df_get_live_out (curr_bb), j);
5149 else
5150 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5153 prev_bb = curr_bb;
5154 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5156 if (! NONDEBUG_INSN_P (curr_insn))
5157 continue;
5158 curr_id = lra_get_insn_recog_data (curr_insn);
5159 curr_static_id = curr_id->insn_static_data;
5160 remove_p = false;
5161 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
5162 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5163 && bitmap_bit_p (&check_only_regs, regno)
5164 && ! bitmap_bit_p (&live_regs, regno))
5165 remove_p = true;
5166 /* See which defined values die here. */
5167 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5168 if (reg->type == OP_OUT && ! reg->subreg_p)
5169 bitmap_clear_bit (&live_regs, reg->regno);
5170 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5171 if (reg->type == OP_OUT && ! reg->subreg_p)
5172 bitmap_clear_bit (&live_regs, reg->regno);
5173 if (curr_id->arg_hard_regs != NULL)
5174 /* Make clobbered argument hard registers die. */
5175 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5176 if (regno >= FIRST_PSEUDO_REGISTER)
5177 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
5178 /* Mark each used value as live. */
5179 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5180 if (reg->type != OP_OUT
5181 && bitmap_bit_p (&check_only_regs, reg->regno))
5182 bitmap_set_bit (&live_regs, reg->regno);
5183 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5184 if (reg->type != OP_OUT
5185 && bitmap_bit_p (&check_only_regs, reg->regno))
5186 bitmap_set_bit (&live_regs, reg->regno);
5187 if (curr_id->arg_hard_regs != NULL)
5188 /* Make used argument hard registers live. */
5189 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5190 if (regno < FIRST_PSEUDO_REGISTER
5191 && bitmap_bit_p (&check_only_regs, regno))
5192 bitmap_set_bit (&live_regs, regno);
5193 /* It is quite important to remove dead move insns because it
5194 means removing dead store. We don't need to process them for
5195 constraints. */
5196 if (remove_p)
5198 if (lra_dump_file != NULL)
5200 fprintf (lra_dump_file, " Removing dead insn:\n ");
5201 dump_insn_slim (lra_dump_file, curr_insn);
5203 lra_set_insn_deleted (curr_insn);
5208 /* The structure describes info to do an inheritance for the current
5209 insn. We need to collect such info first before doing the
5210 transformations because the transformations change the insn
5211 internal representation. */
5212 struct to_inherit
5214 /* Original regno. */
5215 int regno;
5216 /* Subsequent insns which can inherit original reg value. */
5217 rtx insns;
5220 /* Array containing all info for doing inheritance from the current
5221 insn. */
5222 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5224 /* Number elements in the previous array. */
5225 static int to_inherit_num;
5227 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5228 structure to_inherit. */
5229 static void
5230 add_to_inherit (int regno, rtx insns)
5232 int i;
5234 for (i = 0; i < to_inherit_num; i++)
5235 if (to_inherit[i].regno == regno)
5236 return;
5237 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5238 to_inherit[to_inherit_num].regno = regno;
5239 to_inherit[to_inherit_num++].insns = insns;
5242 /* Return the last non-debug insn in basic block BB, or the block begin
5243 note if none. */
5244 static rtx_insn *
5245 get_last_insertion_point (basic_block bb)
5247 rtx_insn *insn;
5249 FOR_BB_INSNS_REVERSE (bb, insn)
5250 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5251 return insn;
5252 gcc_unreachable ();
5255 /* Set up RES by registers living on edges FROM except the edge (FROM,
5256 TO) or by registers set up in a jump insn in BB FROM. */
5257 static void
5258 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5260 rtx_insn *last;
5261 struct lra_insn_reg *reg;
5262 edge e;
5263 edge_iterator ei;
5265 lra_assert (to != NULL);
5266 bitmap_clear (res);
5267 FOR_EACH_EDGE (e, ei, from->succs)
5268 if (e->dest != to)
5269 bitmap_ior_into (res, df_get_live_in (e->dest));
5270 last = get_last_insertion_point (from);
5271 if (! JUMP_P (last))
5272 return;
5273 curr_id = lra_get_insn_recog_data (last);
5274 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5275 if (reg->type != OP_IN)
5276 bitmap_set_bit (res, reg->regno);
5279 /* Used as a temporary results of some bitmap calculations. */
5280 static bitmap_head temp_bitmap;
5282 /* We split for reloads of small class of hard regs. The following
5283 defines how many hard regs the class should have to be qualified as
5284 small. The code is mostly oriented to x86/x86-64 architecture
5285 where some insns need to use only specific register or pair of
5286 registers and these register can live in RTL explicitly, e.g. for
5287 parameter passing. */
5288 static const int max_small_class_regs_num = 2;
5290 /* Do inheritance/split transformations in EBB starting with HEAD and
5291 finishing on TAIL. We process EBB insns in the reverse order.
5292 Return true if we did any inheritance/split transformation in the
5293 EBB.
5295 We should avoid excessive splitting which results in worse code
5296 because of inaccurate cost calculations for spilling new split
5297 pseudos in such case. To achieve this we do splitting only if
5298 register pressure is high in given basic block and there are reload
5299 pseudos requiring hard registers. We could do more register
5300 pressure calculations at any given program point to avoid necessary
5301 splitting even more but it is to expensive and the current approach
5302 works well enough. */
5303 static bool
5304 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5306 int i, src_regno, dst_regno, nregs;
5307 bool change_p, succ_p, update_reloads_num_p;
5308 rtx_insn *prev_insn, *last_insn;
5309 rtx next_usage_insns, set;
5310 enum reg_class cl;
5311 struct lra_insn_reg *reg;
5312 basic_block last_processed_bb, curr_bb = NULL;
5313 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5314 bitmap to_process;
5315 unsigned int j;
5316 bitmap_iterator bi;
5317 bool head_p, after_p;
5319 change_p = false;
5320 curr_usage_insns_check++;
5321 reloads_num = calls_num = 0;
5322 bitmap_clear (&check_only_regs);
5323 last_processed_bb = NULL;
5324 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5325 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5326 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5327 /* We don't process new insns generated in the loop. */
5328 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5330 prev_insn = PREV_INSN (curr_insn);
5331 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5332 curr_bb = BLOCK_FOR_INSN (curr_insn);
5333 if (last_processed_bb != curr_bb)
5335 /* We are at the end of BB. Add qualified living
5336 pseudos for potential splitting. */
5337 to_process = df_get_live_out (curr_bb);
5338 if (last_processed_bb != NULL)
5340 /* We are somewhere in the middle of EBB. */
5341 get_live_on_other_edges (curr_bb, last_processed_bb,
5342 &temp_bitmap);
5343 to_process = &temp_bitmap;
5345 last_processed_bb = curr_bb;
5346 last_insn = get_last_insertion_point (curr_bb);
5347 after_p = (! JUMP_P (last_insn)
5348 && (! CALL_P (last_insn)
5349 || (find_reg_note (last_insn,
5350 REG_NORETURN, NULL_RTX) == NULL_RTX
5351 && ! SIBLING_CALL_P (last_insn))));
5352 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5353 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5355 if ((int) j >= lra_constraint_new_regno_start)
5356 break;
5357 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5359 if (j < FIRST_PSEUDO_REGISTER)
5360 SET_HARD_REG_BIT (live_hard_regs, j);
5361 else
5362 add_to_hard_reg_set (&live_hard_regs,
5363 PSEUDO_REGNO_MODE (j),
5364 reg_renumber[j]);
5365 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5369 src_regno = dst_regno = -1;
5370 if (NONDEBUG_INSN_P (curr_insn)
5371 && (set = single_set (curr_insn)) != NULL_RTX
5372 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5374 src_regno = REGNO (SET_SRC (set));
5375 dst_regno = REGNO (SET_DEST (set));
5377 update_reloads_num_p = true;
5378 if (src_regno < lra_constraint_new_regno_start
5379 && src_regno >= FIRST_PSEUDO_REGISTER
5380 && reg_renumber[src_regno] < 0
5381 && dst_regno >= lra_constraint_new_regno_start
5382 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5384 /* 'reload_pseudo <- original_pseudo'. */
5385 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5386 reloads_num++;
5387 update_reloads_num_p = false;
5388 succ_p = false;
5389 if (usage_insns[src_regno].check == curr_usage_insns_check
5390 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5391 succ_p = inherit_reload_reg (false, src_regno, cl,
5392 curr_insn, next_usage_insns);
5393 if (succ_p)
5394 change_p = true;
5395 else
5396 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5397 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5398 IOR_HARD_REG_SET (potential_reload_hard_regs,
5399 reg_class_contents[cl]);
5401 else if (src_regno >= lra_constraint_new_regno_start
5402 && dst_regno < lra_constraint_new_regno_start
5403 && dst_regno >= FIRST_PSEUDO_REGISTER
5404 && reg_renumber[dst_regno] < 0
5405 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5406 && usage_insns[dst_regno].check == curr_usage_insns_check
5407 && (next_usage_insns
5408 = usage_insns[dst_regno].insns) != NULL_RTX)
5410 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5411 reloads_num++;
5412 update_reloads_num_p = false;
5413 /* 'original_pseudo <- reload_pseudo'. */
5414 if (! JUMP_P (curr_insn)
5415 && inherit_reload_reg (true, dst_regno, cl,
5416 curr_insn, next_usage_insns))
5417 change_p = true;
5418 /* Invalidate. */
5419 usage_insns[dst_regno].check = 0;
5420 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5421 IOR_HARD_REG_SET (potential_reload_hard_regs,
5422 reg_class_contents[cl]);
5424 else if (INSN_P (curr_insn))
5426 int iter;
5427 int max_uid = get_max_uid ();
5429 curr_id = lra_get_insn_recog_data (curr_insn);
5430 curr_static_id = curr_id->insn_static_data;
5431 to_inherit_num = 0;
5432 /* Process insn definitions. */
5433 for (iter = 0; iter < 2; iter++)
5434 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5435 reg != NULL;
5436 reg = reg->next)
5437 if (reg->type != OP_IN
5438 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5440 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5441 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5442 && usage_insns[dst_regno].check == curr_usage_insns_check
5443 && (next_usage_insns
5444 = usage_insns[dst_regno].insns) != NULL_RTX)
5446 struct lra_insn_reg *r;
5448 for (r = curr_id->regs; r != NULL; r = r->next)
5449 if (r->type != OP_OUT && r->regno == dst_regno)
5450 break;
5451 /* Don't do inheritance if the pseudo is also
5452 used in the insn. */
5453 if (r == NULL)
5454 /* We can not do inheritance right now
5455 because the current insn reg info (chain
5456 regs) can change after that. */
5457 add_to_inherit (dst_regno, next_usage_insns);
5459 /* We can not process one reg twice here because of
5460 usage_insns invalidation. */
5461 if ((dst_regno < FIRST_PSEUDO_REGISTER
5462 || reg_renumber[dst_regno] >= 0)
5463 && ! reg->subreg_p && reg->type != OP_IN)
5465 HARD_REG_SET s;
5467 if (split_if_necessary (dst_regno, reg->biggest_mode,
5468 potential_reload_hard_regs,
5469 false, curr_insn, max_uid))
5470 change_p = true;
5471 CLEAR_HARD_REG_SET (s);
5472 if (dst_regno < FIRST_PSEUDO_REGISTER)
5473 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5474 else
5475 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5476 reg_renumber[dst_regno]);
5477 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5479 /* We should invalidate potential inheritance or
5480 splitting for the current insn usages to the next
5481 usage insns (see code below) as the output pseudo
5482 prevents this. */
5483 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5484 && reg_renumber[dst_regno] < 0)
5485 || (reg->type == OP_OUT && ! reg->subreg_p
5486 && (dst_regno < FIRST_PSEUDO_REGISTER
5487 || reg_renumber[dst_regno] >= 0)))
5489 /* Invalidate and mark definitions. */
5490 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5491 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5492 else
5494 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5495 for (i = 0; i < nregs; i++)
5496 usage_insns[dst_regno + i].check
5497 = -(int) INSN_UID (curr_insn);
5501 /* Process clobbered call regs. */
5502 if (curr_id->arg_hard_regs != NULL)
5503 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5504 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5505 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
5506 = -(int) INSN_UID (curr_insn);
5507 if (! JUMP_P (curr_insn))
5508 for (i = 0; i < to_inherit_num; i++)
5509 if (inherit_reload_reg (true, to_inherit[i].regno,
5510 ALL_REGS, curr_insn,
5511 to_inherit[i].insns))
5512 change_p = true;
5513 if (CALL_P (curr_insn))
5515 rtx cheap, pat, dest;
5516 rtx_insn *restore;
5517 int regno, hard_regno;
5519 calls_num++;
5520 if ((cheap = find_reg_note (curr_insn,
5521 REG_RETURNED, NULL_RTX)) != NULL_RTX
5522 && ((cheap = XEXP (cheap, 0)), true)
5523 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5524 && (hard_regno = reg_renumber[regno]) >= 0
5525 /* If there are pending saves/restores, the
5526 optimization is not worth. */
5527 && usage_insns[regno].calls_num == calls_num - 1
5528 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5530 /* Restore the pseudo from the call result as
5531 REG_RETURNED note says that the pseudo value is
5532 in the call result and the pseudo is an argument
5533 of the call. */
5534 pat = PATTERN (curr_insn);
5535 if (GET_CODE (pat) == PARALLEL)
5536 pat = XVECEXP (pat, 0, 0);
5537 dest = SET_DEST (pat);
5538 /* For multiple return values dest is PARALLEL.
5539 Currently we handle only single return value case. */
5540 if (REG_P (dest))
5542 start_sequence ();
5543 emit_move_insn (cheap, copy_rtx (dest));
5544 restore = get_insns ();
5545 end_sequence ();
5546 lra_process_new_insns (curr_insn, NULL, restore,
5547 "Inserting call parameter restore");
5548 /* We don't need to save/restore of the pseudo from
5549 this call. */
5550 usage_insns[regno].calls_num = calls_num;
5551 bitmap_set_bit (&check_only_regs, regno);
5555 to_inherit_num = 0;
5556 /* Process insn usages. */
5557 for (iter = 0; iter < 2; iter++)
5558 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5559 reg != NULL;
5560 reg = reg->next)
5561 if ((reg->type != OP_OUT
5562 || (reg->type == OP_OUT && reg->subreg_p))
5563 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5565 if (src_regno >= FIRST_PSEUDO_REGISTER
5566 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5568 if (usage_insns[src_regno].check == curr_usage_insns_check
5569 && (next_usage_insns
5570 = usage_insns[src_regno].insns) != NULL_RTX
5571 && NONDEBUG_INSN_P (curr_insn))
5572 add_to_inherit (src_regno, next_usage_insns);
5573 else if (usage_insns[src_regno].check
5574 != -(int) INSN_UID (curr_insn))
5575 /* Add usages but only if the reg is not set up
5576 in the same insn. */
5577 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5579 else if (src_regno < FIRST_PSEUDO_REGISTER
5580 || reg_renumber[src_regno] >= 0)
5582 bool before_p;
5583 rtx_insn *use_insn = curr_insn;
5585 before_p = (JUMP_P (curr_insn)
5586 || (CALL_P (curr_insn) && reg->type == OP_IN));
5587 if (NONDEBUG_INSN_P (curr_insn)
5588 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5589 && split_if_necessary (src_regno, reg->biggest_mode,
5590 potential_reload_hard_regs,
5591 before_p, curr_insn, max_uid))
5593 if (reg->subreg_p)
5594 lra_risky_transformations_p = true;
5595 change_p = true;
5596 /* Invalidate. */
5597 usage_insns[src_regno].check = 0;
5598 if (before_p)
5599 use_insn = PREV_INSN (curr_insn);
5601 if (NONDEBUG_INSN_P (curr_insn))
5603 if (src_regno < FIRST_PSEUDO_REGISTER)
5604 add_to_hard_reg_set (&live_hard_regs,
5605 reg->biggest_mode, src_regno);
5606 else
5607 add_to_hard_reg_set (&live_hard_regs,
5608 PSEUDO_REGNO_MODE (src_regno),
5609 reg_renumber[src_regno]);
5611 add_next_usage_insn (src_regno, use_insn, reloads_num);
5614 /* Process used call regs. */
5615 if (curr_id->arg_hard_regs != NULL)
5616 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5617 if (src_regno < FIRST_PSEUDO_REGISTER)
5619 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5620 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5622 for (i = 0; i < to_inherit_num; i++)
5624 src_regno = to_inherit[i].regno;
5625 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5626 curr_insn, to_inherit[i].insns))
5627 change_p = true;
5628 else
5629 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5632 if (update_reloads_num_p
5633 && NONDEBUG_INSN_P (curr_insn)
5634 && (set = single_set (curr_insn)) != NULL_RTX)
5636 int regno = -1;
5637 if ((REG_P (SET_DEST (set))
5638 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5639 && reg_renumber[regno] < 0
5640 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5641 || (REG_P (SET_SRC (set))
5642 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5643 && reg_renumber[regno] < 0
5644 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5646 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5647 reloads_num++;
5648 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5649 IOR_HARD_REG_SET (potential_reload_hard_regs,
5650 reg_class_contents[cl]);
5653 /* We reached the start of the current basic block. */
5654 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5655 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5657 /* We reached the beginning of the current block -- do
5658 rest of spliting in the current BB. */
5659 to_process = df_get_live_in (curr_bb);
5660 if (BLOCK_FOR_INSN (head) != curr_bb)
5662 /* We are somewhere in the middle of EBB. */
5663 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5664 curr_bb, &temp_bitmap);
5665 to_process = &temp_bitmap;
5667 head_p = true;
5668 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5670 if ((int) j >= lra_constraint_new_regno_start)
5671 break;
5672 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5673 && usage_insns[j].check == curr_usage_insns_check
5674 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5676 if (need_for_split_p (potential_reload_hard_regs, j))
5678 if (lra_dump_file != NULL && head_p)
5680 fprintf (lra_dump_file,
5681 " ----------------------------------\n");
5682 head_p = false;
5684 if (split_reg (false, j, bb_note (curr_bb),
5685 next_usage_insns))
5686 change_p = true;
5688 usage_insns[j].check = 0;
5693 return change_p;
5696 /* This value affects EBB forming. If probability of edge from EBB to
5697 a BB is not greater than the following value, we don't add the BB
5698 to EBB. */
5699 #define EBB_PROBABILITY_CUTOFF \
5700 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
5702 /* Current number of inheritance/split iteration. */
5703 int lra_inheritance_iter;
5705 /* Entry function for inheritance/split pass. */
5706 void
5707 lra_inheritance (void)
5709 int i;
5710 basic_block bb, start_bb;
5711 edge e;
5713 lra_inheritance_iter++;
5714 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5715 return;
5716 timevar_push (TV_LRA_INHERITANCE);
5717 if (lra_dump_file != NULL)
5718 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5719 lra_inheritance_iter);
5720 curr_usage_insns_check = 0;
5721 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5722 for (i = 0; i < lra_constraint_new_regno_start; i++)
5723 usage_insns[i].check = 0;
5724 bitmap_initialize (&check_only_regs, &reg_obstack);
5725 bitmap_initialize (&live_regs, &reg_obstack);
5726 bitmap_initialize (&temp_bitmap, &reg_obstack);
5727 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5728 FOR_EACH_BB_FN (bb, cfun)
5730 start_bb = bb;
5731 if (lra_dump_file != NULL)
5732 fprintf (lra_dump_file, "EBB");
5733 /* Form a EBB starting with BB. */
5734 bitmap_clear (&ebb_global_regs);
5735 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5736 for (;;)
5738 if (lra_dump_file != NULL)
5739 fprintf (lra_dump_file, " %d", bb->index);
5740 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5741 || LABEL_P (BB_HEAD (bb->next_bb)))
5742 break;
5743 e = find_fallthru_edge (bb->succs);
5744 if (! e)
5745 break;
5746 if (e->probability < EBB_PROBABILITY_CUTOFF)
5747 break;
5748 bb = bb->next_bb;
5750 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5751 if (lra_dump_file != NULL)
5752 fprintf (lra_dump_file, "\n");
5753 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5754 /* Remember that the EBB head and tail can change in
5755 inherit_in_ebb. */
5756 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5758 bitmap_clear (&ebb_global_regs);
5759 bitmap_clear (&temp_bitmap);
5760 bitmap_clear (&live_regs);
5761 bitmap_clear (&check_only_regs);
5762 free (usage_insns);
5764 timevar_pop (TV_LRA_INHERITANCE);
5769 /* This page contains code to undo failed inheritance/split
5770 transformations. */
5772 /* Current number of iteration undoing inheritance/split. */
5773 int lra_undo_inheritance_iter;
5775 /* Fix BB live info LIVE after removing pseudos created on pass doing
5776 inheritance/split which are REMOVED_PSEUDOS. */
5777 static void
5778 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5780 unsigned int regno;
5781 bitmap_iterator bi;
5783 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5784 if (bitmap_clear_bit (live, regno))
5785 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5788 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5789 number. */
5790 static int
5791 get_regno (rtx reg)
5793 if (GET_CODE (reg) == SUBREG)
5794 reg = SUBREG_REG (reg);
5795 if (REG_P (reg))
5796 return REGNO (reg);
5797 return -1;
5800 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5801 return true if we did any change. The undo transformations for
5802 inheritance looks like
5803 i <- i2
5804 p <- i => p <- i2
5805 or removing
5806 p <- i, i <- p, and i <- i3
5807 where p is original pseudo from which inheritance pseudo i was
5808 created, i and i3 are removed inheritance pseudos, i2 is another
5809 not removed inheritance pseudo. All split pseudos or other
5810 occurrences of removed inheritance pseudos are changed on the
5811 corresponding original pseudos.
5813 The function also schedules insns changed and created during
5814 inheritance/split pass for processing by the subsequent constraint
5815 pass. */
5816 static bool
5817 remove_inheritance_pseudos (bitmap remove_pseudos)
5819 basic_block bb;
5820 int regno, sregno, prev_sregno, dregno, restore_regno;
5821 rtx set, prev_set;
5822 rtx_insn *prev_insn;
5823 bool change_p, done_p;
5825 change_p = ! bitmap_empty_p (remove_pseudos);
5826 /* We can not finish the function right away if CHANGE_P is true
5827 because we need to marks insns affected by previous
5828 inheritance/split pass for processing by the subsequent
5829 constraint pass. */
5830 FOR_EACH_BB_FN (bb, cfun)
5832 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5833 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5834 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5836 if (! INSN_P (curr_insn))
5837 continue;
5838 done_p = false;
5839 sregno = dregno = -1;
5840 if (change_p && NONDEBUG_INSN_P (curr_insn)
5841 && (set = single_set (curr_insn)) != NULL_RTX)
5843 dregno = get_regno (SET_DEST (set));
5844 sregno = get_regno (SET_SRC (set));
5847 if (sregno >= 0 && dregno >= 0)
5849 if ((bitmap_bit_p (remove_pseudos, sregno)
5850 && (lra_reg_info[sregno].restore_regno == dregno
5851 || (bitmap_bit_p (remove_pseudos, dregno)
5852 && (lra_reg_info[sregno].restore_regno
5853 == lra_reg_info[dregno].restore_regno))))
5854 || (bitmap_bit_p (remove_pseudos, dregno)
5855 && lra_reg_info[dregno].restore_regno == sregno))
5856 /* One of the following cases:
5857 original <- removed inheritance pseudo
5858 removed inherit pseudo <- another removed inherit pseudo
5859 removed inherit pseudo <- original pseudo
5861 removed_split_pseudo <- original_reg
5862 original_reg <- removed_split_pseudo */
5864 if (lra_dump_file != NULL)
5866 fprintf (lra_dump_file, " Removing %s:\n",
5867 bitmap_bit_p (&lra_split_regs, sregno)
5868 || bitmap_bit_p (&lra_split_regs, dregno)
5869 ? "split" : "inheritance");
5870 dump_insn_slim (lra_dump_file, curr_insn);
5872 lra_set_insn_deleted (curr_insn);
5873 done_p = true;
5875 else if (bitmap_bit_p (remove_pseudos, sregno)
5876 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5878 /* Search the following pattern:
5879 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5880 original_pseudo <- inherit_or_split_pseudo1
5881 where the 2nd insn is the current insn and
5882 inherit_or_split_pseudo2 is not removed. If it is found,
5883 change the current insn onto:
5884 original_pseudo <- inherit_or_split_pseudo2. */
5885 for (prev_insn = PREV_INSN (curr_insn);
5886 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5887 prev_insn = PREV_INSN (prev_insn))
5889 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5890 && (prev_set = single_set (prev_insn)) != NULL_RTX
5891 /* There should be no subregs in insn we are
5892 searching because only the original reg might
5893 be in subreg when we changed the mode of
5894 load/store for splitting. */
5895 && REG_P (SET_DEST (prev_set))
5896 && REG_P (SET_SRC (prev_set))
5897 && (int) REGNO (SET_DEST (prev_set)) == sregno
5898 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5899 >= FIRST_PSEUDO_REGISTER)
5900 /* As we consider chain of inheritance or
5901 splitting described in above comment we should
5902 check that sregno and prev_sregno were
5903 inheritance/split pseudos created from the
5904 same original regno. */
5905 && (lra_reg_info[sregno].restore_regno
5906 == lra_reg_info[prev_sregno].restore_regno)
5907 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5909 lra_assert (GET_MODE (SET_SRC (prev_set))
5910 == GET_MODE (regno_reg_rtx[sregno]));
5911 if (GET_CODE (SET_SRC (set)) == SUBREG)
5912 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5913 else
5914 SET_SRC (set) = SET_SRC (prev_set);
5915 /* As we are finishing with processing the insn
5916 here, check the destination too as it might
5917 inheritance pseudo for another pseudo. */
5918 if (bitmap_bit_p (remove_pseudos, dregno)
5919 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
5920 && (restore_regno
5921 = lra_reg_info[dregno].restore_regno) >= 0)
5923 if (GET_CODE (SET_DEST (set)) == SUBREG)
5924 SUBREG_REG (SET_DEST (set))
5925 = regno_reg_rtx[restore_regno];
5926 else
5927 SET_DEST (set) = regno_reg_rtx[restore_regno];
5929 lra_push_insn_and_update_insn_regno_info (curr_insn);
5930 lra_set_used_insn_alternative_by_uid
5931 (INSN_UID (curr_insn), -1);
5932 done_p = true;
5933 if (lra_dump_file != NULL)
5935 fprintf (lra_dump_file, " Change reload insn:\n");
5936 dump_insn_slim (lra_dump_file, curr_insn);
5941 if (! done_p)
5943 struct lra_insn_reg *reg;
5944 bool restored_regs_p = false;
5945 bool kept_regs_p = false;
5947 curr_id = lra_get_insn_recog_data (curr_insn);
5948 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5950 regno = reg->regno;
5951 restore_regno = lra_reg_info[regno].restore_regno;
5952 if (restore_regno >= 0)
5954 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5956 lra_substitute_pseudo_within_insn
5957 (curr_insn, regno, regno_reg_rtx[restore_regno],
5958 false);
5959 restored_regs_p = true;
5961 else
5962 kept_regs_p = true;
5965 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5967 /* The instruction has changed since the previous
5968 constraints pass. */
5969 lra_push_insn_and_update_insn_regno_info (curr_insn);
5970 lra_set_used_insn_alternative_by_uid
5971 (INSN_UID (curr_insn), -1);
5973 else if (restored_regs_p)
5974 /* The instruction has been restored to the form that
5975 it had during the previous constraints pass. */
5976 lra_update_insn_regno_info (curr_insn);
5977 if (restored_regs_p && lra_dump_file != NULL)
5979 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5980 dump_insn_slim (lra_dump_file, curr_insn);
5985 return change_p;
5988 /* If optional reload pseudos failed to get a hard register or was not
5989 inherited, it is better to remove optional reloads. We do this
5990 transformation after undoing inheritance to figure out necessity to
5991 remove optional reloads easier. Return true if we do any
5992 change. */
5993 static bool
5994 undo_optional_reloads (void)
5996 bool change_p, keep_p;
5997 unsigned int regno, uid;
5998 bitmap_iterator bi, bi2;
5999 rtx_insn *insn;
6000 rtx set, src, dest;
6001 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
6003 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
6004 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
6005 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6007 keep_p = false;
6008 /* Keep optional reloads from previous subpasses. */
6009 if (lra_reg_info[regno].restore_regno < 0
6010 /* If the original pseudo changed its allocation, just
6011 removing the optional pseudo is dangerous as the original
6012 pseudo will have longer live range. */
6013 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
6014 keep_p = true;
6015 else if (reg_renumber[regno] >= 0)
6016 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
6018 insn = lra_insn_recog_data[uid]->insn;
6019 if ((set = single_set (insn)) == NULL_RTX)
6020 continue;
6021 src = SET_SRC (set);
6022 dest = SET_DEST (set);
6023 if (! REG_P (src) || ! REG_P (dest))
6024 continue;
6025 if (REGNO (dest) == regno
6026 /* Ignore insn for optional reloads itself. */
6027 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
6028 /* Check only inheritance on last inheritance pass. */
6029 && (int) REGNO (src) >= new_regno_start
6030 /* Check that the optional reload was inherited. */
6031 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6033 keep_p = true;
6034 break;
6037 if (keep_p)
6039 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6040 if (lra_dump_file != NULL)
6041 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6044 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6045 bitmap_initialize (&insn_bitmap, &reg_obstack);
6046 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6048 if (lra_dump_file != NULL)
6049 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6050 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6051 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6053 insn = lra_insn_recog_data[uid]->insn;
6054 if ((set = single_set (insn)) != NULL_RTX)
6056 src = SET_SRC (set);
6057 dest = SET_DEST (set);
6058 if (REG_P (src) && REG_P (dest)
6059 && ((REGNO (src) == regno
6060 && (lra_reg_info[regno].restore_regno
6061 == (int) REGNO (dest)))
6062 || (REGNO (dest) == regno
6063 && (lra_reg_info[regno].restore_regno
6064 == (int) REGNO (src)))))
6066 if (lra_dump_file != NULL)
6068 fprintf (lra_dump_file, " Deleting move %u\n",
6069 INSN_UID (insn));
6070 dump_insn_slim (lra_dump_file, insn);
6072 lra_set_insn_deleted (insn);
6073 continue;
6075 /* We should not worry about generation memory-memory
6076 moves here as if the corresponding inheritance did
6077 not work (inheritance pseudo did not get a hard reg),
6078 we remove the inheritance pseudo and the optional
6079 reload. */
6081 lra_substitute_pseudo_within_insn
6082 (insn, regno, regno_reg_rtx[lra_reg_info[regno].restore_regno],
6083 false);
6084 lra_update_insn_regno_info (insn);
6085 if (lra_dump_file != NULL)
6087 fprintf (lra_dump_file,
6088 " Restoring original insn:\n");
6089 dump_insn_slim (lra_dump_file, insn);
6093 /* Clear restore_regnos. */
6094 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6095 lra_reg_info[regno].restore_regno = -1;
6096 bitmap_clear (&insn_bitmap);
6097 bitmap_clear (&removed_optional_reload_pseudos);
6098 return change_p;
6101 /* Entry function for undoing inheritance/split transformation. Return true
6102 if we did any RTL change in this pass. */
6103 bool
6104 lra_undo_inheritance (void)
6106 unsigned int regno;
6107 int restore_regno, hard_regno;
6108 int n_all_inherit, n_inherit, n_all_split, n_split;
6109 bitmap_head remove_pseudos;
6110 bitmap_iterator bi;
6111 bool change_p;
6113 lra_undo_inheritance_iter++;
6114 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6115 return false;
6116 if (lra_dump_file != NULL)
6117 fprintf (lra_dump_file,
6118 "\n********** Undoing inheritance #%d: **********\n\n",
6119 lra_undo_inheritance_iter);
6120 bitmap_initialize (&remove_pseudos, &reg_obstack);
6121 n_inherit = n_all_inherit = 0;
6122 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6123 if (lra_reg_info[regno].restore_regno >= 0)
6125 n_all_inherit++;
6126 if (reg_renumber[regno] < 0
6127 /* If the original pseudo changed its allocation, just
6128 removing inheritance is dangerous as for changing
6129 allocation we used shorter live-ranges. */
6130 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6131 bitmap_set_bit (&remove_pseudos, regno);
6132 else
6133 n_inherit++;
6135 if (lra_dump_file != NULL && n_all_inherit != 0)
6136 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6137 n_inherit, n_all_inherit,
6138 (double) n_inherit / n_all_inherit * 100);
6139 n_split = n_all_split = 0;
6140 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6141 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6143 n_all_split++;
6144 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6145 ? reg_renumber[restore_regno] : restore_regno);
6146 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6147 bitmap_set_bit (&remove_pseudos, regno);
6148 else
6150 n_split++;
6151 if (lra_dump_file != NULL)
6152 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6153 regno, restore_regno);
6156 if (lra_dump_file != NULL && n_all_split != 0)
6157 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6158 n_split, n_all_split,
6159 (double) n_split / n_all_split * 100);
6160 change_p = remove_inheritance_pseudos (&remove_pseudos);
6161 bitmap_clear (&remove_pseudos);
6162 /* Clear restore_regnos. */
6163 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6164 lra_reg_info[regno].restore_regno = -1;
6165 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6166 lra_reg_info[regno].restore_regno = -1;
6167 change_p = undo_optional_reloads () || change_p;
6168 return change_p;