2015-01-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / lra-constraints.c
blob827c453b0fb111c866f79386f6f8b98ad00275e6
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2015 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains code for 3 passes: constraint pass,
23 inheritance/split pass, and pass for undoing failed inheritance and
24 split.
26 The major goal of constraint pass is to transform RTL to satisfy
27 insn and address constraints by:
28 o choosing insn alternatives;
29 o generating *reload insns* (or reloads in brief) and *reload
30 pseudos* which will get necessary hard registers later;
31 o substituting pseudos with equivalent values and removing the
32 instructions that initialized those pseudos.
34 The constraint pass has biggest and most complicated code in LRA.
35 There are a lot of important details like:
36 o reuse of input reload pseudos to simplify reload pseudo
37 allocations;
38 o some heuristics to choose insn alternative to improve the
39 inheritance;
40 o early clobbers etc.
42 The pass is mimicking former reload pass in alternative choosing
43 because the reload pass is oriented to current machine description
44 model. It might be changed if the machine description model is
45 changed.
47 There is special code for preventing all LRA and this pass cycling
48 in case of bugs.
50 On the first iteration of the pass we process every instruction and
51 choose an alternative for each one. On subsequent iterations we try
52 to avoid reprocessing instructions if we can be sure that the old
53 choice is still valid.
55 The inheritance/spilt pass is to transform code to achieve
56 ineheritance and live range splitting. It is done on backward
57 traversal of EBBs.
59 The inheritance optimization goal is to reuse values in hard
60 registers. There is analogous optimization in old reload pass. The
61 inheritance is achieved by following transformation:
63 reload_p1 <- p reload_p1 <- p
64 ... new_p <- reload_p1
65 ... => ...
66 reload_p2 <- p reload_p2 <- new_p
68 where p is spilled and not changed between the insns. Reload_p1 is
69 also called *original pseudo* and new_p is called *inheritance
70 pseudo*.
72 The subsequent assignment pass will try to assign the same (or
73 another if it is not possible) hard register to new_p as to
74 reload_p1 or reload_p2.
76 If the assignment pass fails to assign a hard register to new_p,
77 this file will undo the inheritance and restore the original code.
78 This is because implementing the above sequence with a spilled
79 new_p would make the code much worse. The inheritance is done in
80 EBB scope. The above is just a simplified example to get an idea
81 of the inheritance as the inheritance is also done for non-reload
82 insns.
84 Splitting (transformation) is also done in EBB scope on the same
85 pass as the inheritance:
87 r <- ... or ... <- r r <- ... or ... <- r
88 ... s <- r (new insn -- save)
89 ... =>
90 ... r <- s (new insn -- restore)
91 ... <- r ... <- r
93 The *split pseudo* s is assigned to the hard register of the
94 original pseudo or hard register r.
96 Splitting is done:
97 o In EBBs with high register pressure for global pseudos (living
98 in at least 2 BBs) and assigned to hard registers when there
99 are more one reloads needing the hard registers;
100 o for pseudos needing save/restore code around calls.
102 If the split pseudo still has the same hard register as the
103 original pseudo after the subsequent assignment pass or the
104 original pseudo was split, the opposite transformation is done on
105 the same pass for undoing inheritance. */
107 #undef REG_OK_STRICT
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "tm.h"
113 #include "hard-reg-set.h"
114 #include "rtl.h"
115 #include "tm_p.h"
116 #include "regs.h"
117 #include "insn-config.h"
118 #include "insn-codes.h"
119 #include "recog.h"
120 #include "output.h"
121 #include "addresses.h"
122 #include "target.h"
123 #include "hashtab.h"
124 #include "hash-set.h"
125 #include "vec.h"
126 #include "machmode.h"
127 #include "input.h"
128 #include "function.h"
129 #include "symtab.h"
130 #include "flags.h"
131 #include "statistics.h"
132 #include "double-int.h"
133 #include "real.h"
134 #include "fixed-value.h"
135 #include "alias.h"
136 #include "wide-int.h"
137 #include "inchash.h"
138 #include "tree.h"
139 #include "expmed.h"
140 #include "dojump.h"
141 #include "explow.h"
142 #include "calls.h"
143 #include "emit-rtl.h"
144 #include "varasm.h"
145 #include "stmt.h"
146 #include "expr.h"
147 #include "predict.h"
148 #include "dominance.h"
149 #include "cfg.h"
150 #include "cfgrtl.h"
151 #include "basic-block.h"
152 #include "except.h"
153 #include "optabs.h"
154 #include "df.h"
155 #include "ira.h"
156 #include "rtl-error.h"
157 #include "lra-int.h"
159 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
160 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
161 reload insns. */
162 static int bb_reload_num;
164 /* The current insn being processed and corresponding its single set
165 (NULL otherwise), its data (basic block, the insn data, the insn
166 static data, and the mode of each operand). */
167 static rtx_insn *curr_insn;
168 static rtx curr_insn_set;
169 static basic_block curr_bb;
170 static lra_insn_recog_data_t curr_id;
171 static struct lra_static_insn_data *curr_static_id;
172 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
173 /* Mode of the register substituted by its equivalence with VOIDmode
174 (e.g. constant) and whose subreg is given operand of the current
175 insn. VOIDmode in all other cases. */
176 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
180 /* Start numbers for new registers and insns at the current constraints
181 pass start. */
182 static int new_regno_start;
183 static int new_insn_uid_start;
185 /* If LOC is nonnull, strip any outer subreg from it. */
186 static inline rtx *
187 strip_subreg (rtx *loc)
189 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
192 /* Return hard regno of REGNO or if it is was not assigned to a hard
193 register, use a hard register from its allocno class. */
194 static int
195 get_try_hard_regno (int regno)
197 int hard_regno;
198 enum reg_class rclass;
200 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
201 hard_regno = lra_get_regno_hard_regno (regno);
202 if (hard_regno >= 0)
203 return hard_regno;
204 rclass = lra_get_allocno_class (regno);
205 if (rclass == NO_REGS)
206 return -1;
207 return ira_class_hard_regs[rclass][0];
210 /* Return final hard regno (plus offset) which will be after
211 elimination. We do this for matching constraints because the final
212 hard regno could have a different class. */
213 static int
214 get_final_hard_regno (int hard_regno, int offset)
216 if (hard_regno < 0)
217 return hard_regno;
218 hard_regno = lra_get_elimination_hard_regno (hard_regno);
219 return hard_regno + offset;
222 /* Return hard regno of X after removing subreg and making
223 elimination. If X is not a register or subreg of register, return
224 -1. For pseudo use its assignment. */
225 static int
226 get_hard_regno (rtx x)
228 rtx reg;
229 int offset, hard_regno;
231 reg = x;
232 if (GET_CODE (x) == SUBREG)
233 reg = SUBREG_REG (x);
234 if (! REG_P (reg))
235 return -1;
236 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
237 hard_regno = lra_get_regno_hard_regno (hard_regno);
238 if (hard_regno < 0)
239 return -1;
240 offset = 0;
241 if (GET_CODE (x) == SUBREG)
242 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
243 SUBREG_BYTE (x), GET_MODE (x));
244 return get_final_hard_regno (hard_regno, offset);
247 /* If REGNO is a hard register or has been allocated a hard register,
248 return the class of that register. If REGNO is a reload pseudo
249 created by the current constraints pass, return its allocno class.
250 Return NO_REGS otherwise. */
251 static enum reg_class
252 get_reg_class (int regno)
254 int hard_regno;
256 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
257 hard_regno = lra_get_regno_hard_regno (regno);
258 if (hard_regno >= 0)
260 hard_regno = get_final_hard_regno (hard_regno, 0);
261 return REGNO_REG_CLASS (hard_regno);
263 if (regno >= new_regno_start)
264 return lra_get_allocno_class (regno);
265 return NO_REGS;
268 /* Return true if REG satisfies (or will satisfy) reg class constraint
269 CL. Use elimination first if REG is a hard register. If REG is a
270 reload pseudo created by this constraints pass, assume that it will
271 be allocated a hard register from its allocno class, but allow that
272 class to be narrowed to CL if it is currently a superset of CL.
274 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
275 REGNO (reg), or NO_REGS if no change in its class was needed. */
276 static bool
277 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
279 enum reg_class rclass, common_class;
280 machine_mode reg_mode;
281 int class_size, hard_regno, nregs, i, j;
282 int regno = REGNO (reg);
284 if (new_class != NULL)
285 *new_class = NO_REGS;
286 if (regno < FIRST_PSEUDO_REGISTER)
288 rtx final_reg = reg;
289 rtx *final_loc = &final_reg;
291 lra_eliminate_reg_if_possible (final_loc);
292 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
294 reg_mode = GET_MODE (reg);
295 rclass = get_reg_class (regno);
296 if (regno < new_regno_start
297 /* Do not allow the constraints for reload instructions to
298 influence the classes of new pseudos. These reloads are
299 typically moves that have many alternatives, and restricting
300 reload pseudos for one alternative may lead to situations
301 where other reload pseudos are no longer allocatable. */
302 || (INSN_UID (curr_insn) >= new_insn_uid_start
303 && curr_insn_set != NULL
304 && ((OBJECT_P (SET_SRC (curr_insn_set))
305 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
306 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
307 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
308 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
309 /* When we don't know what class will be used finally for reload
310 pseudos, we use ALL_REGS. */
311 return ((regno >= new_regno_start && rclass == ALL_REGS)
312 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
313 && ! hard_reg_set_subset_p (reg_class_contents[cl],
314 lra_no_alloc_regs)));
315 else
317 common_class = ira_reg_class_subset[rclass][cl];
318 if (new_class != NULL)
319 *new_class = common_class;
320 if (hard_reg_set_subset_p (reg_class_contents[common_class],
321 lra_no_alloc_regs))
322 return false;
323 /* Check that there are enough allocatable regs. */
324 class_size = ira_class_hard_regs_num[common_class];
325 for (i = 0; i < class_size; i++)
327 hard_regno = ira_class_hard_regs[common_class][i];
328 nregs = hard_regno_nregs[hard_regno][reg_mode];
329 if (nregs == 1)
330 return true;
331 for (j = 0; j < nregs; j++)
332 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
333 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
334 hard_regno + j))
335 break;
336 if (j >= nregs)
337 return true;
339 return false;
343 /* Return true if REGNO satisfies a memory constraint. */
344 static bool
345 in_mem_p (int regno)
347 return get_reg_class (regno) == NO_REGS;
350 /* Return 1 if ADDR is a valid memory address for mode MODE in address
351 space AS, and check that each pseudo has the proper kind of hard
352 reg. */
353 static int
354 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
355 rtx addr, addr_space_t as)
357 #ifdef GO_IF_LEGITIMATE_ADDRESS
358 lra_assert (ADDR_SPACE_GENERIC_P (as));
359 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
360 return 0;
362 win:
363 return 1;
364 #else
365 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
366 #endif
369 namespace {
370 /* Temporarily eliminates registers in an address (for the lifetime of
371 the object). */
372 class address_eliminator {
373 public:
374 address_eliminator (struct address_info *ad);
375 ~address_eliminator ();
377 private:
378 struct address_info *m_ad;
379 rtx *m_base_loc;
380 rtx m_base_reg;
381 rtx *m_index_loc;
382 rtx m_index_reg;
386 address_eliminator::address_eliminator (struct address_info *ad)
387 : m_ad (ad),
388 m_base_loc (strip_subreg (ad->base_term)),
389 m_base_reg (NULL_RTX),
390 m_index_loc (strip_subreg (ad->index_term)),
391 m_index_reg (NULL_RTX)
393 if (m_base_loc != NULL)
395 m_base_reg = *m_base_loc;
396 lra_eliminate_reg_if_possible (m_base_loc);
397 if (m_ad->base_term2 != NULL)
398 *m_ad->base_term2 = *m_ad->base_term;
400 if (m_index_loc != NULL)
402 m_index_reg = *m_index_loc;
403 lra_eliminate_reg_if_possible (m_index_loc);
407 address_eliminator::~address_eliminator ()
409 if (m_base_loc && *m_base_loc != m_base_reg)
411 *m_base_loc = m_base_reg;
412 if (m_ad->base_term2 != NULL)
413 *m_ad->base_term2 = *m_ad->base_term;
415 if (m_index_loc && *m_index_loc != m_index_reg)
416 *m_index_loc = m_index_reg;
419 /* Return true if the eliminated form of AD is a legitimate target address. */
420 static bool
421 valid_address_p (struct address_info *ad)
423 address_eliminator eliminator (ad);
424 return valid_address_p (ad->mode, *ad->outer, ad->as);
427 /* Return true if the eliminated form of memory reference OP satisfies
428 extra memory constraint CONSTRAINT. */
429 static bool
430 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
432 struct address_info ad;
434 decompose_mem_address (&ad, op);
435 address_eliminator eliminator (&ad);
436 return constraint_satisfied_p (op, constraint);
439 /* Return true if the eliminated form of address AD satisfies extra
440 address constraint CONSTRAINT. */
441 static bool
442 satisfies_address_constraint_p (struct address_info *ad,
443 enum constraint_num constraint)
445 address_eliminator eliminator (ad);
446 return constraint_satisfied_p (*ad->outer, constraint);
449 /* Return true if the eliminated form of address OP satisfies extra
450 address constraint CONSTRAINT. */
451 static bool
452 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
454 struct address_info ad;
456 decompose_lea_address (&ad, &op);
457 return satisfies_address_constraint_p (&ad, constraint);
460 /* Initiate equivalences for LRA. As we keep original equivalences
461 before any elimination, we need to make copies otherwise any change
462 in insns might change the equivalences. */
463 void
464 lra_init_equiv (void)
466 ira_expand_reg_equiv ();
467 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
469 rtx res;
471 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
472 ira_reg_equiv[i].memory = copy_rtx (res);
473 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
474 ira_reg_equiv[i].invariant = copy_rtx (res);
478 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
480 /* Update equivalence for REGNO. We need to this as the equivalence
481 might contain other pseudos which are changed by their
482 equivalences. */
483 static void
484 update_equiv (int regno)
486 rtx x;
488 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
489 ira_reg_equiv[regno].memory
490 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
491 NULL_RTX);
492 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
493 ira_reg_equiv[regno].invariant
494 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
495 NULL_RTX);
498 /* If we have decided to substitute X with another value, return that
499 value, otherwise return X. */
500 static rtx
501 get_equiv (rtx x)
503 int regno;
504 rtx res;
506 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
507 || ! ira_reg_equiv[regno].defined_p
508 || ! ira_reg_equiv[regno].profitable_p
509 || lra_get_regno_hard_regno (regno) >= 0)
510 return x;
511 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
513 if (targetm.cannot_substitute_mem_equiv_p (res))
514 return x;
515 return res;
517 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
518 return res;
519 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
520 return res;
521 gcc_unreachable ();
524 /* If we have decided to substitute X with the equivalent value,
525 return that value after elimination for INSN, otherwise return
526 X. */
527 static rtx
528 get_equiv_with_elimination (rtx x, rtx_insn *insn)
530 rtx res = get_equiv (x);
532 if (x == res || CONSTANT_P (res))
533 return res;
534 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
535 0, false, false, true);
538 /* Set up curr_operand_mode. */
539 static void
540 init_curr_operand_mode (void)
542 int nop = curr_static_id->n_operands;
543 for (int i = 0; i < nop; i++)
545 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
546 if (mode == VOIDmode)
548 /* The .md mode for address operands is the mode of the
549 addressed value rather than the mode of the address itself. */
550 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
551 mode = Pmode;
552 else
553 mode = curr_static_id->operand[i].mode;
555 curr_operand_mode[i] = mode;
561 /* The page contains code to reuse input reloads. */
563 /* Structure describes input reload of the current insns. */
564 struct input_reload
566 /* Reloaded value. */
567 rtx input;
568 /* Reload pseudo used. */
569 rtx reg;
572 /* The number of elements in the following array. */
573 static int curr_insn_input_reloads_num;
574 /* Array containing info about input reloads. It is used to find the
575 same input reload and reuse the reload pseudo in this case. */
576 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
578 /* Initiate data concerning reuse of input reloads for the current
579 insn. */
580 static void
581 init_curr_insn_input_reloads (void)
583 curr_insn_input_reloads_num = 0;
586 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
587 created input reload pseudo (only if TYPE is not OP_OUT). Don't
588 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
589 wrapped up in SUBREG. The result pseudo is returned through
590 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
591 reused the already created input reload pseudo. Use TITLE to
592 describe new registers for debug purposes. */
593 static bool
594 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
595 enum reg_class rclass, bool in_subreg_p,
596 const char *title, rtx *result_reg)
598 int i, regno;
599 enum reg_class new_class;
601 if (type == OP_OUT)
603 *result_reg
604 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
605 return true;
607 /* Prevent reuse value of expression with side effects,
608 e.g. volatile memory. */
609 if (! side_effects_p (original))
610 for (i = 0; i < curr_insn_input_reloads_num; i++)
611 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
612 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
614 rtx reg = curr_insn_input_reloads[i].reg;
615 regno = REGNO (reg);
616 /* If input is equal to original and both are VOIDmode,
617 GET_MODE (reg) might be still different from mode.
618 Ensure we don't return *result_reg with wrong mode. */
619 if (GET_MODE (reg) != mode)
621 if (in_subreg_p)
622 continue;
623 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
624 continue;
625 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
626 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
627 continue;
629 *result_reg = reg;
630 if (lra_dump_file != NULL)
632 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
633 dump_value_slim (lra_dump_file, original, 1);
635 if (new_class != lra_get_allocno_class (regno))
636 lra_change_class (regno, new_class, ", change to", false);
637 if (lra_dump_file != NULL)
638 fprintf (lra_dump_file, "\n");
639 return false;
641 *result_reg = lra_create_new_reg (mode, original, rclass, title);
642 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
643 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
644 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
645 return true;
650 /* The page contains code to extract memory address parts. */
652 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
653 static inline bool
654 ok_for_index_p_nonstrict (rtx reg)
656 unsigned regno = REGNO (reg);
658 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
661 /* A version of regno_ok_for_base_p for use here, when all pseudos
662 should count as OK. Arguments as for regno_ok_for_base_p. */
663 static inline bool
664 ok_for_base_p_nonstrict (rtx reg, machine_mode mode, addr_space_t as,
665 enum rtx_code outer_code, enum rtx_code index_code)
667 unsigned regno = REGNO (reg);
669 if (regno >= FIRST_PSEUDO_REGISTER)
670 return true;
671 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
676 /* The page contains major code to choose the current insn alternative
677 and generate reloads for it. */
679 /* Return the offset from REGNO of the least significant register
680 in (reg:MODE REGNO).
682 This function is used to tell whether two registers satisfy
683 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
685 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
686 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
688 lra_constraint_offset (int regno, machine_mode mode)
690 lra_assert (regno < FIRST_PSEUDO_REGISTER);
691 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
692 && SCALAR_INT_MODE_P (mode))
693 return hard_regno_nregs[regno][mode] - 1;
694 return 0;
697 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
698 if they are the same hard reg, and has special hacks for
699 auto-increment and auto-decrement. This is specifically intended for
700 process_alt_operands to use in determining whether two operands
701 match. X is the operand whose number is the lower of the two.
703 It is supposed that X is the output operand and Y is the input
704 operand. Y_HARD_REGNO is the final hard regno of register Y or
705 register in subreg Y as we know it now. Otherwise, it is a
706 negative value. */
707 static bool
708 operands_match_p (rtx x, rtx y, int y_hard_regno)
710 int i;
711 RTX_CODE code = GET_CODE (x);
712 const char *fmt;
714 if (x == y)
715 return true;
716 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
717 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
719 int j;
721 i = get_hard_regno (x);
722 if (i < 0)
723 goto slow;
725 if ((j = y_hard_regno) < 0)
726 goto slow;
728 i += lra_constraint_offset (i, GET_MODE (x));
729 j += lra_constraint_offset (j, GET_MODE (y));
731 return i == j;
734 /* If two operands must match, because they are really a single
735 operand of an assembler insn, then two post-increments are invalid
736 because the assembler insn would increment only once. On the
737 other hand, a post-increment matches ordinary indexing if the
738 post-increment is the output operand. */
739 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
740 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
742 /* Two pre-increments are invalid because the assembler insn would
743 increment only once. On the other hand, a pre-increment matches
744 ordinary indexing if the pre-increment is the input operand. */
745 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
746 || GET_CODE (y) == PRE_MODIFY)
747 return operands_match_p (x, XEXP (y, 0), -1);
749 slow:
751 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
752 && x == SUBREG_REG (y))
753 return true;
754 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
755 && SUBREG_REG (x) == y)
756 return true;
758 /* Now we have disposed of all the cases in which different rtx
759 codes can match. */
760 if (code != GET_CODE (y))
761 return false;
763 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
764 if (GET_MODE (x) != GET_MODE (y))
765 return false;
767 switch (code)
769 CASE_CONST_UNIQUE:
770 return false;
772 case LABEL_REF:
773 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
774 case SYMBOL_REF:
775 return XSTR (x, 0) == XSTR (y, 0);
777 default:
778 break;
781 /* Compare the elements. If any pair of corresponding elements fail
782 to match, return false for the whole things. */
784 fmt = GET_RTX_FORMAT (code);
785 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
787 int val, j;
788 switch (fmt[i])
790 case 'w':
791 if (XWINT (x, i) != XWINT (y, i))
792 return false;
793 break;
795 case 'i':
796 if (XINT (x, i) != XINT (y, i))
797 return false;
798 break;
800 case 'e':
801 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
802 if (val == 0)
803 return false;
804 break;
806 case '0':
807 break;
809 case 'E':
810 if (XVECLEN (x, i) != XVECLEN (y, i))
811 return false;
812 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
814 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
815 if (val == 0)
816 return false;
818 break;
820 /* It is believed that rtx's at this level will never
821 contain anything but integers and other rtx's, except for
822 within LABEL_REFs and SYMBOL_REFs. */
823 default:
824 gcc_unreachable ();
827 return true;
830 /* True if X is a constant that can be forced into the constant pool.
831 MODE is the mode of the operand, or VOIDmode if not known. */
832 #define CONST_POOL_OK_P(MODE, X) \
833 ((MODE) != VOIDmode \
834 && CONSTANT_P (X) \
835 && GET_CODE (X) != HIGH \
836 && !targetm.cannot_force_const_mem (MODE, X))
838 /* True if C is a non-empty register class that has too few registers
839 to be safely used as a reload target class. */
840 #define SMALL_REGISTER_CLASS_P(C) \
841 (ira_class_hard_regs_num [(C)] == 1 \
842 || (ira_class_hard_regs_num [(C)] >= 1 \
843 && targetm.class_likely_spilled_p (C)))
845 /* If REG is a reload pseudo, try to make its class satisfying CL. */
846 static void
847 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
849 enum reg_class rclass;
851 /* Do not make more accurate class from reloads generated. They are
852 mostly moves with a lot of constraints. Making more accurate
853 class may results in very narrow class and impossibility of find
854 registers for several reloads of one insn. */
855 if (INSN_UID (curr_insn) >= new_insn_uid_start)
856 return;
857 if (GET_CODE (reg) == SUBREG)
858 reg = SUBREG_REG (reg);
859 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
860 return;
861 if (in_class_p (reg, cl, &rclass) && rclass != cl)
862 lra_change_class (REGNO (reg), rclass, " Change to", true);
865 /* Generate reloads for matching OUT and INS (array of input operand
866 numbers with end marker -1) with reg class GOAL_CLASS. Add input
867 and output reloads correspondingly to the lists *BEFORE and *AFTER.
868 OUT might be negative. In this case we generate input reloads for
869 matched input operands INS. */
870 static void
871 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
872 rtx_insn **before, rtx_insn **after)
874 int i, in;
875 rtx new_in_reg, new_out_reg, reg, clobber;
876 machine_mode inmode, outmode;
877 rtx in_rtx = *curr_id->operand_loc[ins[0]];
878 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
880 inmode = curr_operand_mode[ins[0]];
881 outmode = out < 0 ? inmode : curr_operand_mode[out];
882 push_to_sequence (*before);
883 if (inmode != outmode)
885 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
887 reg = new_in_reg
888 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
889 goal_class, "");
890 if (SCALAR_INT_MODE_P (inmode))
891 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
892 else
893 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
894 LRA_SUBREG_P (new_out_reg) = 1;
895 /* If the input reg is dying here, we can use the same hard
896 register for REG and IN_RTX. We do it only for original
897 pseudos as reload pseudos can die although original
898 pseudos still live where reload pseudos dies. */
899 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
900 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
901 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
903 else
905 reg = new_out_reg
906 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
907 goal_class, "");
908 if (SCALAR_INT_MODE_P (outmode))
909 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
910 else
911 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
912 /* NEW_IN_REG is non-paradoxical subreg. We don't want
913 NEW_OUT_REG living above. We add clobber clause for
914 this. This is just a temporary clobber. We can remove
915 it at the end of LRA work. */
916 clobber = emit_clobber (new_out_reg);
917 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
918 LRA_SUBREG_P (new_in_reg) = 1;
919 if (GET_CODE (in_rtx) == SUBREG)
921 rtx subreg_reg = SUBREG_REG (in_rtx);
923 /* If SUBREG_REG is dying here and sub-registers IN_RTX
924 and NEW_IN_REG are similar, we can use the same hard
925 register for REG and SUBREG_REG. */
926 if (REG_P (subreg_reg)
927 && (int) REGNO (subreg_reg) < lra_new_regno_start
928 && GET_MODE (subreg_reg) == outmode
929 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
930 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
931 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
935 else
937 /* Pseudos have values -- see comments for lra_reg_info.
938 Different pseudos with the same value do not conflict even if
939 they live in the same place. When we create a pseudo we
940 assign value of original pseudo (if any) from which we
941 created the new pseudo. If we create the pseudo from the
942 input pseudo, the new pseudo will no conflict with the input
943 pseudo which is wrong when the input pseudo lives after the
944 insn and as the new pseudo value is changed by the insn
945 output. Therefore we create the new pseudo from the output.
947 We cannot reuse the current output register because we might
948 have a situation like "a <- a op b", where the constraints
949 force the second input operand ("b") to match the output
950 operand ("a"). "b" must then be copied into a new register
951 so that it doesn't clobber the current value of "a". */
953 new_in_reg = new_out_reg
954 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
955 goal_class, "");
957 /* In operand can be got from transformations before processing insn
958 constraints. One example of such transformations is subreg
959 reloading (see function simplify_operand_subreg). The new
960 pseudos created by the transformations might have inaccurate
961 class (ALL_REGS) and we should make their classes more
962 accurate. */
963 narrow_reload_pseudo_class (in_rtx, goal_class);
964 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
965 *before = get_insns ();
966 end_sequence ();
967 for (i = 0; (in = ins[i]) >= 0; i++)
969 lra_assert
970 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
971 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
972 *curr_id->operand_loc[in] = new_in_reg;
974 lra_update_dups (curr_id, ins);
975 if (out < 0)
976 return;
977 /* See a comment for the input operand above. */
978 narrow_reload_pseudo_class (out_rtx, goal_class);
979 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
981 start_sequence ();
982 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
983 emit_insn (*after);
984 *after = get_insns ();
985 end_sequence ();
987 *curr_id->operand_loc[out] = new_out_reg;
988 lra_update_dup (curr_id, out);
991 /* Return register class which is union of all reg classes in insn
992 constraint alternative string starting with P. */
993 static enum reg_class
994 reg_class_from_constraints (const char *p)
996 int c, len;
997 enum reg_class op_class = NO_REGS;
1000 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1002 case '#':
1003 case ',':
1004 return op_class;
1006 case 'g':
1007 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1008 break;
1010 default:
1011 enum constraint_num cn = lookup_constraint (p);
1012 enum reg_class cl = reg_class_for_constraint (cn);
1013 if (cl == NO_REGS)
1015 if (insn_extra_address_constraint (cn))
1016 op_class
1017 = (reg_class_subunion
1018 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1019 ADDRESS, SCRATCH)]);
1020 break;
1023 op_class = reg_class_subunion[op_class][cl];
1024 break;
1026 while ((p += len), c);
1027 return op_class;
1030 /* If OP is a register, return the class of the register as per
1031 get_reg_class, otherwise return NO_REGS. */
1032 static inline enum reg_class
1033 get_op_class (rtx op)
1035 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1038 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1039 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1040 SUBREG for VAL to make them equal. */
1041 static rtx_insn *
1042 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1044 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1046 /* Usually size of mem_pseudo is greater than val size but in
1047 rare cases it can be less as it can be defined by target
1048 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1049 if (! MEM_P (val))
1051 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1052 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1054 LRA_SUBREG_P (val) = 1;
1056 else
1058 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1059 LRA_SUBREG_P (mem_pseudo) = 1;
1062 return as_a <rtx_insn *> (to_p
1063 ? gen_move_insn (mem_pseudo, val)
1064 : gen_move_insn (val, mem_pseudo));
1067 /* Process a special case insn (register move), return true if we
1068 don't need to process it anymore. INSN should be a single set
1069 insn. Set up that RTL was changed through CHANGE_P and macro
1070 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1071 SEC_MEM_P. */
1072 static bool
1073 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1075 int sregno, dregno;
1076 rtx dest, src, dreg, sreg, old_sreg, new_reg, scratch_reg;
1077 rtx_insn *before;
1078 enum reg_class dclass, sclass, secondary_class;
1079 machine_mode sreg_mode;
1080 secondary_reload_info sri;
1082 lra_assert (curr_insn_set != NULL_RTX);
1083 dreg = dest = SET_DEST (curr_insn_set);
1084 sreg = src = SET_SRC (curr_insn_set);
1085 if (GET_CODE (dest) == SUBREG)
1086 dreg = SUBREG_REG (dest);
1087 if (GET_CODE (src) == SUBREG)
1088 sreg = SUBREG_REG (src);
1089 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1090 return false;
1091 sclass = dclass = NO_REGS;
1092 if (REG_P (dreg))
1093 dclass = get_reg_class (REGNO (dreg));
1094 if (dclass == ALL_REGS)
1095 /* ALL_REGS is used for new pseudos created by transformations
1096 like reload of SUBREG_REG (see function
1097 simplify_operand_subreg). We don't know their class yet. We
1098 should figure out the class from processing the insn
1099 constraints not in this fast path function. Even if ALL_REGS
1100 were a right class for the pseudo, secondary_... hooks usually
1101 are not define for ALL_REGS. */
1102 return false;
1103 sreg_mode = GET_MODE (sreg);
1104 old_sreg = sreg;
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, sreg,
1164 (reg_class_t) dclass,
1165 sreg_mode, &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 (sreg_mode, NULL_RTX,
1182 secondary_class,
1183 "secondary");
1184 start_sequence ();
1185 if (old_sreg != sreg)
1186 sreg = copy_rtx (sreg);
1187 if (sri.icode == CODE_FOR_nothing)
1188 lra_emit_move (new_reg, sreg);
1189 else
1191 enum reg_class scratch_class;
1193 scratch_class = (reg_class_from_constraints
1194 (insn_data[sri.icode].operand[2].constraint));
1195 scratch_reg = (lra_create_new_reg_with_unique_value
1196 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1197 scratch_class, "scratch"));
1198 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1199 sreg, scratch_reg));
1201 before = get_insns ();
1202 end_sequence ();
1203 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1204 if (new_reg != NULL_RTX)
1206 if (GET_CODE (src) == SUBREG)
1207 SUBREG_REG (src) = new_reg;
1208 else
1209 SET_SRC (curr_insn_set) = new_reg;
1211 else
1213 if (lra_dump_file != NULL)
1215 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1216 dump_insn_slim (lra_dump_file, curr_insn);
1218 lra_set_insn_deleted (curr_insn);
1219 return true;
1221 return false;
1224 /* The following data describe the result of process_alt_operands.
1225 The data are used in curr_insn_transform to generate reloads. */
1227 /* The chosen reg classes which should be used for the corresponding
1228 operands. */
1229 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1230 /* True if the operand should be the same as another operand and that
1231 other operand does not need a reload. */
1232 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1233 /* True if the operand does not need a reload. */
1234 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1235 /* True if the operand can be offsetable memory. */
1236 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1237 /* The number of an operand to which given operand can be matched to. */
1238 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1239 /* The number of elements in the following array. */
1240 static int goal_alt_dont_inherit_ops_num;
1241 /* Numbers of operands whose reload pseudos should not be inherited. */
1242 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1243 /* True if the insn commutative operands should be swapped. */
1244 static bool goal_alt_swapped;
1245 /* The chosen insn alternative. */
1246 static int goal_alt_number;
1248 /* The following five variables are used to choose the best insn
1249 alternative. They reflect final characteristics of the best
1250 alternative. */
1252 /* Number of necessary reloads and overall cost reflecting the
1253 previous value and other unpleasantness of the best alternative. */
1254 static int best_losers, best_overall;
1255 /* Overall number hard registers used for reloads. For example, on
1256 some targets we need 2 general registers to reload DFmode and only
1257 one floating point register. */
1258 static int best_reload_nregs;
1259 /* Overall number reflecting distances of previous reloading the same
1260 value. The distances are counted from the current BB start. It is
1261 used to improve inheritance chances. */
1262 static int best_reload_sum;
1264 /* True if the current insn should have no correspondingly input or
1265 output reloads. */
1266 static bool no_input_reloads_p, no_output_reloads_p;
1268 /* True if we swapped the commutative operands in the current
1269 insn. */
1270 static int curr_swapped;
1272 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1273 register of class CL. Add any input reloads to list BEFORE. AFTER
1274 is nonnull if *LOC is an automodified value; handle that case by
1275 adding the required output reloads to list AFTER. Return true if
1276 the RTL was changed.
1278 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1279 register. Return false if the address register is correct. */
1280 static bool
1281 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1282 enum reg_class cl)
1284 int regno;
1285 enum reg_class rclass, new_class;
1286 rtx reg;
1287 rtx new_reg;
1288 machine_mode mode;
1289 bool subreg_p, before_p = false;
1291 subreg_p = GET_CODE (*loc) == SUBREG;
1292 if (subreg_p)
1293 loc = &SUBREG_REG (*loc);
1294 reg = *loc;
1295 mode = GET_MODE (reg);
1296 if (! REG_P (reg))
1298 if (check_only_p)
1299 return true;
1300 /* Always reload memory in an address even if the target supports
1301 such addresses. */
1302 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1303 before_p = true;
1305 else
1307 regno = REGNO (reg);
1308 rclass = get_reg_class (regno);
1309 if (! check_only_p
1310 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1312 if (lra_dump_file != NULL)
1314 fprintf (lra_dump_file,
1315 "Changing pseudo %d in address of insn %u on equiv ",
1316 REGNO (reg), INSN_UID (curr_insn));
1317 dump_value_slim (lra_dump_file, *loc, 1);
1318 fprintf (lra_dump_file, "\n");
1320 *loc = copy_rtx (*loc);
1322 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1324 if (check_only_p)
1325 return true;
1326 reg = *loc;
1327 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1328 mode, reg, cl, subreg_p, "address", &new_reg))
1329 before_p = true;
1331 else if (new_class != NO_REGS && rclass != new_class)
1333 if (check_only_p)
1334 return true;
1335 lra_change_class (regno, new_class, " Change to", true);
1336 return false;
1338 else
1339 return false;
1341 if (before_p)
1343 push_to_sequence (*before);
1344 lra_emit_move (new_reg, reg);
1345 *before = get_insns ();
1346 end_sequence ();
1348 *loc = new_reg;
1349 if (after != NULL)
1351 start_sequence ();
1352 lra_emit_move (reg, new_reg);
1353 emit_insn (*after);
1354 *after = get_insns ();
1355 end_sequence ();
1357 return true;
1360 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1361 the insn to be inserted before curr insn. AFTER returns the
1362 the insn to be inserted after curr insn. ORIGREG and NEWREG
1363 are the original reg and new reg for reload. */
1364 static void
1365 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1366 rtx newreg)
1368 if (before)
1370 push_to_sequence (*before);
1371 lra_emit_move (newreg, origreg);
1372 *before = get_insns ();
1373 end_sequence ();
1375 if (after)
1377 start_sequence ();
1378 lra_emit_move (origreg, newreg);
1379 emit_insn (*after);
1380 *after = get_insns ();
1381 end_sequence ();
1385 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1387 /* Make reloads for subreg in operand NOP with internal subreg mode
1388 REG_MODE, add new reloads for further processing. Return true if
1389 any change was done. */
1390 static bool
1391 simplify_operand_subreg (int nop, machine_mode reg_mode)
1393 int hard_regno;
1394 rtx_insn *before, *after;
1395 machine_mode mode, innermode;
1396 rtx reg, new_reg;
1397 rtx operand = *curr_id->operand_loc[nop];
1398 enum reg_class regclass;
1399 enum op_type type;
1401 before = after = NULL;
1403 if (GET_CODE (operand) != SUBREG)
1404 return false;
1406 mode = GET_MODE (operand);
1407 reg = SUBREG_REG (operand);
1408 innermode = GET_MODE (reg);
1409 type = curr_static_id->operand[nop].type;
1410 /* If we change address for paradoxical subreg of memory, the
1411 address might violate the necessary alignment or the access might
1412 be slow. So take this into consideration. We should not worry
1413 about access beyond allocated memory for paradoxical memory
1414 subregs as we don't substitute such equiv memory (see processing
1415 equivalences in function lra_constraints) and because for spilled
1416 pseudos we allocate stack memory enough for the biggest
1417 corresponding paradoxical subreg. */
1418 if (MEM_P (reg)
1419 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1420 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1422 rtx subst, old = *curr_id->operand_loc[nop];
1424 alter_subreg (curr_id->operand_loc[nop], false);
1425 subst = *curr_id->operand_loc[nop];
1426 lra_assert (MEM_P (subst));
1427 if (! valid_address_p (innermode, XEXP (reg, 0),
1428 MEM_ADDR_SPACE (reg))
1429 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1430 MEM_ADDR_SPACE (subst)))
1431 return true;
1432 /* If the address was valid and became invalid, prefer to reload
1433 the memory. Typical case is when the index scale should
1434 correspond the memory. */
1435 *curr_id->operand_loc[nop] = old;
1437 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1439 alter_subreg (curr_id->operand_loc[nop], false);
1440 return true;
1442 else if (CONSTANT_P (reg))
1444 /* Try to simplify subreg of constant. It is usually result of
1445 equivalence substitution. */
1446 if (innermode == VOIDmode
1447 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1448 innermode = curr_static_id->operand[nop].mode;
1449 if ((new_reg = simplify_subreg (mode, reg, innermode,
1450 SUBREG_BYTE (operand))) != NULL_RTX)
1452 *curr_id->operand_loc[nop] = new_reg;
1453 return true;
1456 /* Put constant into memory when we have mixed modes. It generates
1457 a better code in most cases as it does not need a secondary
1458 reload memory. It also prevents LRA looping when LRA is using
1459 secondary reload memory again and again. */
1460 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1461 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1463 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1464 alter_subreg (curr_id->operand_loc[nop], false);
1465 return true;
1467 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1468 if there may be a problem accessing OPERAND in the outer
1469 mode. */
1470 if ((REG_P (reg)
1471 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1472 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1473 /* Don't reload paradoxical subregs because we could be looping
1474 having repeatedly final regno out of hard regs range. */
1475 && (hard_regno_nregs[hard_regno][innermode]
1476 >= hard_regno_nregs[hard_regno][mode])
1477 && simplify_subreg_regno (hard_regno, innermode,
1478 SUBREG_BYTE (operand), mode) < 0
1479 /* Don't reload subreg for matching reload. It is actually
1480 valid subreg in LRA. */
1481 && ! LRA_SUBREG_P (operand))
1482 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1484 enum reg_class rclass;
1486 if (REG_P (reg))
1487 /* There is a big probability that we will get the same class
1488 for the new pseudo and we will get the same insn which
1489 means infinite looping. So spill the new pseudo. */
1490 rclass = NO_REGS;
1491 else
1492 /* The class will be defined later in curr_insn_transform. */
1493 rclass
1494 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1496 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1497 rclass, TRUE, "subreg reg", &new_reg))
1499 bool insert_before, insert_after;
1500 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1502 insert_before = (type != OP_OUT
1503 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1504 insert_after = (type != OP_IN);
1505 insert_move_for_subreg (insert_before ? &before : NULL,
1506 insert_after ? &after : NULL,
1507 reg, new_reg);
1509 SUBREG_REG (operand) = new_reg;
1510 lra_process_new_insns (curr_insn, before, after,
1511 "Inserting subreg reload");
1512 return true;
1514 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1515 IRA allocates hardreg to the inner pseudo reg according to its mode
1516 instead of the outermode, so the size of the hardreg may not be enough
1517 to contain the outermode operand, in that case we may need to insert
1518 reload for the reg. For the following two types of paradoxical subreg,
1519 we need to insert reload:
1520 1. If the op_type is OP_IN, and the hardreg could not be paired with
1521 other hardreg to contain the outermode operand
1522 (checked by in_hard_reg_set_p), we need to insert the reload.
1523 2. If the op_type is OP_OUT or OP_INOUT.
1525 Here is a paradoxical subreg example showing how the reload is generated:
1527 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1528 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1530 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1531 here, if reg107 is assigned to hardreg R15, because R15 is the last
1532 hardreg, compiler cannot find another hardreg to pair with R15 to
1533 contain TImode data. So we insert a TImode reload reg180 for it.
1534 After reload is inserted:
1536 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1537 (reg:DI 107 [ __comp ])) -1
1538 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1539 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1541 Two reload hard registers will be allocated to reg180 to save TImode data
1542 in LRA_assign. */
1543 else if (REG_P (reg)
1544 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1545 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1546 && (hard_regno_nregs[hard_regno][innermode]
1547 < hard_regno_nregs[hard_regno][mode])
1548 && (regclass = lra_get_allocno_class (REGNO (reg)))
1549 && (type != OP_IN
1550 || !in_hard_reg_set_p (reg_class_contents[regclass],
1551 mode, hard_regno)))
1553 /* The class will be defined later in curr_insn_transform. */
1554 enum reg_class rclass
1555 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1557 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1558 rclass, TRUE, "paradoxical subreg", &new_reg))
1560 rtx subreg;
1561 bool insert_before, insert_after;
1563 PUT_MODE (new_reg, mode);
1564 subreg = simplify_gen_subreg (innermode, new_reg, mode, 0);
1565 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1567 insert_before = (type != OP_OUT);
1568 insert_after = (type != OP_IN);
1569 insert_move_for_subreg (insert_before ? &before : NULL,
1570 insert_after ? &after : NULL,
1571 reg, subreg);
1573 SUBREG_REG (operand) = new_reg;
1574 lra_process_new_insns (curr_insn, before, after,
1575 "Inserting paradoxical subreg reload");
1576 return true;
1578 return false;
1581 /* Return TRUE if X refers for a hard register from SET. */
1582 static bool
1583 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1585 int i, j, x_hard_regno;
1586 machine_mode mode;
1587 const char *fmt;
1588 enum rtx_code code;
1590 if (x == NULL_RTX)
1591 return false;
1592 code = GET_CODE (x);
1593 mode = GET_MODE (x);
1594 if (code == SUBREG)
1596 x = SUBREG_REG (x);
1597 code = GET_CODE (x);
1598 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1599 mode = GET_MODE (x);
1602 if (REG_P (x))
1604 x_hard_regno = get_hard_regno (x);
1605 return (x_hard_regno >= 0
1606 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1608 if (MEM_P (x))
1610 struct address_info ad;
1612 decompose_mem_address (&ad, x);
1613 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1614 return true;
1615 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1616 return true;
1618 fmt = GET_RTX_FORMAT (code);
1619 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1621 if (fmt[i] == 'e')
1623 if (uses_hard_regs_p (XEXP (x, i), set))
1624 return true;
1626 else if (fmt[i] == 'E')
1628 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1629 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1630 return true;
1633 return false;
1636 /* Return true if OP is a spilled pseudo. */
1637 static inline bool
1638 spilled_pseudo_p (rtx op)
1640 return (REG_P (op)
1641 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1644 /* Return true if X is a general constant. */
1645 static inline bool
1646 general_constant_p (rtx x)
1648 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1651 static bool
1652 reg_in_class_p (rtx reg, enum reg_class cl)
1654 if (cl == NO_REGS)
1655 return get_reg_class (REGNO (reg)) == NO_REGS;
1656 return in_class_p (reg, cl, NULL);
1659 /* Return true if SET of RCLASS contains no hard regs which can be
1660 used in MODE. */
1661 static bool
1662 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1663 HARD_REG_SET &set,
1664 enum machine_mode mode)
1666 HARD_REG_SET temp;
1668 lra_assert (hard_reg_set_subset_p (set, reg_class_contents[rclass]));
1669 COPY_HARD_REG_SET (temp, set);
1670 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1671 return (hard_reg_set_subset_p
1672 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1675 /* Major function to choose the current insn alternative and what
1676 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1677 negative we should consider only this alternative. Return false if
1678 we can not choose the alternative or find how to reload the
1679 operands. */
1680 static bool
1681 process_alt_operands (int only_alternative)
1683 bool ok_p = false;
1684 int nop, overall, nalt;
1685 int n_alternatives = curr_static_id->n_alternatives;
1686 int n_operands = curr_static_id->n_operands;
1687 /* LOSERS counts the operands that don't fit this alternative and
1688 would require loading. */
1689 int losers;
1690 /* REJECT is a count of how undesirable this alternative says it is
1691 if any reloading is required. If the alternative matches exactly
1692 then REJECT is ignored, but otherwise it gets this much counted
1693 against it in addition to the reloading needed. */
1694 int reject;
1695 int op_reject;
1696 /* The number of elements in the following array. */
1697 int early_clobbered_regs_num;
1698 /* Numbers of operands which are early clobber registers. */
1699 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1700 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1701 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1702 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1703 bool curr_alt_win[MAX_RECOG_OPERANDS];
1704 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1705 int curr_alt_matches[MAX_RECOG_OPERANDS];
1706 /* The number of elements in the following array. */
1707 int curr_alt_dont_inherit_ops_num;
1708 /* Numbers of operands whose reload pseudos should not be inherited. */
1709 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1710 rtx op;
1711 /* The register when the operand is a subreg of register, otherwise the
1712 operand itself. */
1713 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1714 /* The register if the operand is a register or subreg of register,
1715 otherwise NULL. */
1716 rtx operand_reg[MAX_RECOG_OPERANDS];
1717 int hard_regno[MAX_RECOG_OPERANDS];
1718 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1719 int reload_nregs, reload_sum;
1720 bool costly_p;
1721 enum reg_class cl;
1723 /* Calculate some data common for all alternatives to speed up the
1724 function. */
1725 for (nop = 0; nop < n_operands; nop++)
1727 rtx reg;
1729 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1730 /* The real hard regno of the operand after the allocation. */
1731 hard_regno[nop] = get_hard_regno (op);
1733 operand_reg[nop] = reg = op;
1734 biggest_mode[nop] = GET_MODE (op);
1735 if (GET_CODE (op) == SUBREG)
1737 operand_reg[nop] = reg = SUBREG_REG (op);
1738 if (GET_MODE_SIZE (biggest_mode[nop])
1739 < GET_MODE_SIZE (GET_MODE (reg)))
1740 biggest_mode[nop] = GET_MODE (reg);
1742 if (! REG_P (reg))
1743 operand_reg[nop] = NULL_RTX;
1744 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1745 || ((int) REGNO (reg)
1746 == lra_get_elimination_hard_regno (REGNO (reg))))
1747 no_subreg_reg_operand[nop] = reg;
1748 else
1749 operand_reg[nop] = no_subreg_reg_operand[nop]
1750 /* Just use natural mode for elimination result. It should
1751 be enough for extra constraints hooks. */
1752 = regno_reg_rtx[hard_regno[nop]];
1755 /* The constraints are made of several alternatives. Each operand's
1756 constraint looks like foo,bar,... with commas separating the
1757 alternatives. The first alternatives for all operands go
1758 together, the second alternatives go together, etc.
1760 First loop over alternatives. */
1761 alternative_mask preferred = curr_id->preferred_alternatives;
1762 if (only_alternative >= 0)
1763 preferred &= ALTERNATIVE_BIT (only_alternative);
1765 for (nalt = 0; nalt < n_alternatives; nalt++)
1767 /* Loop over operands for one constraint alternative. */
1768 if (!TEST_BIT (preferred, nalt))
1769 continue;
1771 overall = losers = reject = reload_nregs = reload_sum = 0;
1772 for (nop = 0; nop < n_operands; nop++)
1774 int inc = (curr_static_id
1775 ->operand_alternative[nalt * n_operands + nop].reject);
1776 if (lra_dump_file != NULL && inc != 0)
1777 fprintf (lra_dump_file,
1778 " Staticly defined alt reject+=%d\n", inc);
1779 reject += inc;
1781 early_clobbered_regs_num = 0;
1783 for (nop = 0; nop < n_operands; nop++)
1785 const char *p;
1786 char *end;
1787 int len, c, m, i, opalt_num, this_alternative_matches;
1788 bool win, did_match, offmemok, early_clobber_p;
1789 /* false => this operand can be reloaded somehow for this
1790 alternative. */
1791 bool badop;
1792 /* true => this operand can be reloaded if the alternative
1793 allows regs. */
1794 bool winreg;
1795 /* True if a constant forced into memory would be OK for
1796 this operand. */
1797 bool constmemok;
1798 enum reg_class this_alternative, this_costly_alternative;
1799 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1800 bool this_alternative_match_win, this_alternative_win;
1801 bool this_alternative_offmemok;
1802 bool scratch_p;
1803 machine_mode mode;
1804 enum constraint_num cn;
1806 opalt_num = nalt * n_operands + nop;
1807 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1809 /* Fast track for no constraints at all. */
1810 curr_alt[nop] = NO_REGS;
1811 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1812 curr_alt_win[nop] = true;
1813 curr_alt_match_win[nop] = false;
1814 curr_alt_offmemok[nop] = false;
1815 curr_alt_matches[nop] = -1;
1816 continue;
1819 op = no_subreg_reg_operand[nop];
1820 mode = curr_operand_mode[nop];
1822 win = did_match = winreg = offmemok = constmemok = false;
1823 badop = true;
1825 early_clobber_p = false;
1826 p = curr_static_id->operand_alternative[opalt_num].constraint;
1828 this_costly_alternative = this_alternative = NO_REGS;
1829 /* We update set of possible hard regs besides its class
1830 because reg class might be inaccurate. For example,
1831 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1832 is translated in HI_REGS because classes are merged by
1833 pairs and there is no accurate intermediate class. */
1834 CLEAR_HARD_REG_SET (this_alternative_set);
1835 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1836 this_alternative_win = false;
1837 this_alternative_match_win = false;
1838 this_alternative_offmemok = false;
1839 this_alternative_matches = -1;
1841 /* An empty constraint should be excluded by the fast
1842 track. */
1843 lra_assert (*p != 0 && *p != ',');
1845 op_reject = 0;
1846 /* Scan this alternative's specs for this operand; set WIN
1847 if the operand fits any letter in this alternative.
1848 Otherwise, clear BADOP if this operand could fit some
1849 letter after reloads, or set WINREG if this operand could
1850 fit after reloads provided the constraint allows some
1851 registers. */
1852 costly_p = false;
1855 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1857 case '\0':
1858 len = 0;
1859 break;
1860 case ',':
1861 c = '\0';
1862 break;
1864 case '&':
1865 early_clobber_p = true;
1866 break;
1868 case '$':
1869 op_reject += LRA_MAX_REJECT;
1870 break;
1871 case '^':
1872 op_reject += LRA_LOSER_COST_FACTOR;
1873 break;
1875 case '#':
1876 /* Ignore rest of this alternative. */
1877 c = '\0';
1878 break;
1880 case '0': case '1': case '2': case '3': case '4':
1881 case '5': case '6': case '7': case '8': case '9':
1883 int m_hregno;
1884 bool match_p;
1886 m = strtoul (p, &end, 10);
1887 p = end;
1888 len = 0;
1889 lra_assert (nop > m);
1891 this_alternative_matches = m;
1892 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1893 /* We are supposed to match a previous operand.
1894 If we do, we win if that one did. If we do
1895 not, count both of the operands as losers.
1896 (This is too conservative, since most of the
1897 time only a single reload insn will be needed
1898 to make the two operands win. As a result,
1899 this alternative may be rejected when it is
1900 actually desirable.) */
1901 match_p = false;
1902 if (operands_match_p (*curr_id->operand_loc[nop],
1903 *curr_id->operand_loc[m], m_hregno))
1905 /* We should reject matching of an early
1906 clobber operand if the matching operand is
1907 not dying in the insn. */
1908 if (! curr_static_id->operand[m].early_clobber
1909 || operand_reg[nop] == NULL_RTX
1910 || (find_regno_note (curr_insn, REG_DEAD,
1911 REGNO (op))
1912 || REGNO (op) == REGNO (operand_reg[m])))
1913 match_p = true;
1915 if (match_p)
1917 /* If we are matching a non-offsettable
1918 address where an offsettable address was
1919 expected, then we must reject this
1920 combination, because we can't reload
1921 it. */
1922 if (curr_alt_offmemok[m]
1923 && MEM_P (*curr_id->operand_loc[m])
1924 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1925 continue;
1927 else
1929 /* Operands don't match. Both operands must
1930 allow a reload register, otherwise we
1931 cannot make them match. */
1932 if (curr_alt[m] == NO_REGS)
1933 break;
1934 /* Retroactively mark the operand we had to
1935 match as a loser, if it wasn't already and
1936 it wasn't matched to a register constraint
1937 (e.g it might be matched by memory). */
1938 if (curr_alt_win[m]
1939 && (operand_reg[m] == NULL_RTX
1940 || hard_regno[m] < 0))
1942 losers++;
1943 reload_nregs
1944 += (ira_reg_class_max_nregs[curr_alt[m]]
1945 [GET_MODE (*curr_id->operand_loc[m])]);
1948 /* Prefer matching earlyclobber alternative as
1949 it results in less hard regs required for
1950 the insn than a non-matching earlyclobber
1951 alternative. */
1952 if (curr_static_id->operand[m].early_clobber)
1954 if (lra_dump_file != NULL)
1955 fprintf
1956 (lra_dump_file,
1957 " %d Matching earlyclobber alt:"
1958 " reject--\n",
1959 nop);
1960 reject--;
1962 /* Otherwise we prefer no matching
1963 alternatives because it gives more freedom
1964 in RA. */
1965 else if (operand_reg[nop] == NULL_RTX
1966 || (find_regno_note (curr_insn, REG_DEAD,
1967 REGNO (operand_reg[nop]))
1968 == NULL_RTX))
1970 if (lra_dump_file != NULL)
1971 fprintf
1972 (lra_dump_file,
1973 " %d Matching alt: reject+=2\n",
1974 nop);
1975 reject += 2;
1978 /* If we have to reload this operand and some
1979 previous operand also had to match the same
1980 thing as this operand, we don't know how to do
1981 that. */
1982 if (!match_p || !curr_alt_win[m])
1984 for (i = 0; i < nop; i++)
1985 if (curr_alt_matches[i] == m)
1986 break;
1987 if (i < nop)
1988 break;
1990 else
1991 did_match = true;
1993 /* This can be fixed with reloads if the operand
1994 we are supposed to match can be fixed with
1995 reloads. */
1996 badop = false;
1997 this_alternative = curr_alt[m];
1998 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1999 winreg = this_alternative != NO_REGS;
2000 break;
2003 case 'g':
2004 if (MEM_P (op)
2005 || general_constant_p (op)
2006 || spilled_pseudo_p (op))
2007 win = true;
2008 cl = GENERAL_REGS;
2009 goto reg;
2011 default:
2012 cn = lookup_constraint (p);
2013 switch (get_constraint_type (cn))
2015 case CT_REGISTER:
2016 cl = reg_class_for_constraint (cn);
2017 if (cl != NO_REGS)
2018 goto reg;
2019 break;
2021 case CT_CONST_INT:
2022 if (CONST_INT_P (op)
2023 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2024 win = true;
2025 break;
2027 case CT_MEMORY:
2028 if (MEM_P (op)
2029 && satisfies_memory_constraint_p (op, cn))
2030 win = true;
2031 else if (spilled_pseudo_p (op))
2032 win = true;
2034 /* If we didn't already win, we can reload constants
2035 via force_const_mem or put the pseudo value into
2036 memory, or make other memory by reloading the
2037 address like for 'o'. */
2038 if (CONST_POOL_OK_P (mode, op)
2039 || MEM_P (op) || REG_P (op))
2040 badop = false;
2041 constmemok = true;
2042 offmemok = true;
2043 break;
2045 case CT_ADDRESS:
2046 /* If we didn't already win, we can reload the address
2047 into a base register. */
2048 if (satisfies_address_constraint_p (op, cn))
2049 win = true;
2050 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2051 ADDRESS, SCRATCH);
2052 badop = false;
2053 goto reg;
2055 case CT_FIXED_FORM:
2056 if (constraint_satisfied_p (op, cn))
2057 win = true;
2058 break;
2060 break;
2062 reg:
2063 this_alternative = reg_class_subunion[this_alternative][cl];
2064 IOR_HARD_REG_SET (this_alternative_set,
2065 reg_class_contents[cl]);
2066 if (costly_p)
2068 this_costly_alternative
2069 = reg_class_subunion[this_costly_alternative][cl];
2070 IOR_HARD_REG_SET (this_costly_alternative_set,
2071 reg_class_contents[cl]);
2073 if (mode == BLKmode)
2074 break;
2075 winreg = true;
2076 if (REG_P (op))
2078 if (hard_regno[nop] >= 0
2079 && in_hard_reg_set_p (this_alternative_set,
2080 mode, hard_regno[nop]))
2081 win = true;
2082 else if (hard_regno[nop] < 0
2083 && in_class_p (op, this_alternative, NULL))
2084 win = true;
2086 break;
2088 if (c != ' ' && c != '\t')
2089 costly_p = c == '*';
2091 while ((p += len), c);
2093 scratch_p = (operand_reg[nop] != NULL_RTX
2094 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2095 /* Record which operands fit this alternative. */
2096 if (win)
2098 this_alternative_win = true;
2099 if (operand_reg[nop] != NULL_RTX)
2101 if (hard_regno[nop] >= 0)
2103 if (in_hard_reg_set_p (this_costly_alternative_set,
2104 mode, hard_regno[nop]))
2106 if (lra_dump_file != NULL)
2107 fprintf (lra_dump_file,
2108 " %d Costly set: reject++\n",
2109 nop);
2110 reject++;
2113 else
2115 /* Prefer won reg to spilled pseudo under other
2116 equal conditions for possibe inheritance. */
2117 if (! scratch_p)
2119 if (lra_dump_file != NULL)
2120 fprintf
2121 (lra_dump_file,
2122 " %d Non pseudo reload: reject++\n",
2123 nop);
2124 reject++;
2126 if (in_class_p (operand_reg[nop],
2127 this_costly_alternative, NULL))
2129 if (lra_dump_file != NULL)
2130 fprintf
2131 (lra_dump_file,
2132 " %d Non pseudo costly reload:"
2133 " reject++\n",
2134 nop);
2135 reject++;
2138 /* We simulate the behaviour of old reload here.
2139 Although scratches need hard registers and it
2140 might result in spilling other pseudos, no reload
2141 insns are generated for the scratches. So it
2142 might cost something but probably less than old
2143 reload pass believes. */
2144 if (scratch_p)
2146 if (lra_dump_file != NULL)
2147 fprintf (lra_dump_file,
2148 " %d Scratch win: reject+=2\n",
2149 nop);
2150 reject += 2;
2154 else if (did_match)
2155 this_alternative_match_win = true;
2156 else
2158 int const_to_mem = 0;
2159 bool no_regs_p;
2161 reject += op_reject;
2162 /* Never do output reload of stack pointer. It makes
2163 impossible to do elimination when SP is changed in
2164 RTL. */
2165 if (op == stack_pointer_rtx && ! frame_pointer_needed
2166 && curr_static_id->operand[nop].type != OP_IN)
2167 goto fail;
2169 /* If this alternative asks for a specific reg class, see if there
2170 is at least one allocatable register in that class. */
2171 no_regs_p
2172 = (this_alternative == NO_REGS
2173 || (hard_reg_set_subset_p
2174 (reg_class_contents[this_alternative],
2175 lra_no_alloc_regs)));
2177 /* For asms, verify that the class for this alternative is possible
2178 for the mode that is specified. */
2179 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2181 int i;
2182 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2183 if (HARD_REGNO_MODE_OK (i, mode)
2184 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2185 mode, i))
2186 break;
2187 if (i == FIRST_PSEUDO_REGISTER)
2188 winreg = false;
2191 /* If this operand accepts a register, and if the
2192 register class has at least one allocatable register,
2193 then this operand can be reloaded. */
2194 if (winreg && !no_regs_p)
2195 badop = false;
2197 if (badop)
2199 if (lra_dump_file != NULL)
2200 fprintf (lra_dump_file,
2201 " alt=%d: Bad operand -- refuse\n",
2202 nalt);
2203 goto fail;
2206 /* If not assigned pseudo has a class which a subset of
2207 required reg class, it is a less costly alternative
2208 as the pseudo still can get a hard reg of necessary
2209 class. */
2210 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2211 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2212 && ira_class_subset_p[this_alternative][cl])
2214 if (lra_dump_file != NULL)
2215 fprintf
2216 (lra_dump_file,
2217 " %d Super set class reg: reject-=3\n", nop);
2218 reject -= 3;
2221 this_alternative_offmemok = offmemok;
2222 if (this_costly_alternative != NO_REGS)
2224 if (lra_dump_file != NULL)
2225 fprintf (lra_dump_file,
2226 " %d Costly loser: reject++\n", nop);
2227 reject++;
2229 /* If the operand is dying, has a matching constraint,
2230 and satisfies constraints of the matched operand
2231 which failed to satisfy the own constraints, most probably
2232 the reload for this operand will be gone. */
2233 if (this_alternative_matches >= 0
2234 && !curr_alt_win[this_alternative_matches]
2235 && REG_P (op)
2236 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2237 && (hard_regno[nop] >= 0
2238 ? in_hard_reg_set_p (this_alternative_set,
2239 mode, hard_regno[nop])
2240 : in_class_p (op, this_alternative, NULL)))
2242 if (lra_dump_file != NULL)
2243 fprintf
2244 (lra_dump_file,
2245 " %d Dying matched operand reload: reject++\n",
2246 nop);
2247 reject++;
2249 else
2251 /* Strict_low_part requires to reload the register
2252 not the sub-register. In this case we should
2253 check that a final reload hard reg can hold the
2254 value mode. */
2255 if (curr_static_id->operand[nop].strict_low
2256 && REG_P (op)
2257 && hard_regno[nop] < 0
2258 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2259 && ira_class_hard_regs_num[this_alternative] > 0
2260 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2261 [this_alternative][0],
2262 GET_MODE
2263 (*curr_id->operand_loc[nop])))
2265 if (lra_dump_file != NULL)
2266 fprintf
2267 (lra_dump_file,
2268 " alt=%d: Strict low subreg reload -- refuse\n",
2269 nalt);
2270 goto fail;
2272 losers++;
2274 if (operand_reg[nop] != NULL_RTX
2275 /* Output operands and matched input operands are
2276 not inherited. The following conditions do not
2277 exactly describe the previous statement but they
2278 are pretty close. */
2279 && curr_static_id->operand[nop].type != OP_OUT
2280 && (this_alternative_matches < 0
2281 || curr_static_id->operand[nop].type != OP_IN))
2283 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2284 (operand_reg[nop])]
2285 .last_reload);
2287 /* The value of reload_sum has sense only if we
2288 process insns in their order. It happens only on
2289 the first constraints sub-pass when we do most of
2290 reload work. */
2291 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2292 reload_sum += last_reload - bb_reload_num;
2294 /* If this is a constant that is reloaded into the
2295 desired class by copying it to memory first, count
2296 that as another reload. This is consistent with
2297 other code and is required to avoid choosing another
2298 alternative when the constant is moved into memory.
2299 Note that the test here is precisely the same as in
2300 the code below that calls force_const_mem. */
2301 if (CONST_POOL_OK_P (mode, op)
2302 && ((targetm.preferred_reload_class
2303 (op, this_alternative) == NO_REGS)
2304 || no_input_reloads_p))
2306 const_to_mem = 1;
2307 if (! no_regs_p)
2308 losers++;
2311 /* Alternative loses if it requires a type of reload not
2312 permitted for this insn. We can always reload
2313 objects with a REG_UNUSED note. */
2314 if ((curr_static_id->operand[nop].type != OP_IN
2315 && no_output_reloads_p
2316 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2317 || (curr_static_id->operand[nop].type != OP_OUT
2318 && no_input_reloads_p && ! const_to_mem)
2319 || (this_alternative_matches >= 0
2320 && (no_input_reloads_p
2321 || (no_output_reloads_p
2322 && (curr_static_id->operand
2323 [this_alternative_matches].type != OP_IN)
2324 && ! find_reg_note (curr_insn, REG_UNUSED,
2325 no_subreg_reg_operand
2326 [this_alternative_matches])))))
2328 if (lra_dump_file != NULL)
2329 fprintf
2330 (lra_dump_file,
2331 " alt=%d: No input/otput reload -- refuse\n",
2332 nalt);
2333 goto fail;
2336 /* Alternative loses if it required class pseudo can not
2337 hold value of required mode. Such insns can be
2338 described by insn definitions with mode iterators. */
2339 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2340 && ! hard_reg_set_empty_p (this_alternative_set)
2341 /* It is common practice for constraints to use a
2342 class which does not have actually enough regs to
2343 hold the value (e.g. x86 AREG for mode requiring
2344 more one general reg). Therefore we have 2
2345 conditions to check that the reload pseudo can
2346 not hold the mode value. */
2347 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2348 [this_alternative][0],
2349 GET_MODE (*curr_id->operand_loc[nop]))
2350 /* The above condition is not enough as the first
2351 reg in ira_class_hard_regs can be not aligned for
2352 multi-words mode values. */
2353 && (prohibited_class_reg_set_mode_p
2354 (this_alternative, this_alternative_set,
2355 GET_MODE (*curr_id->operand_loc[nop]))))
2357 if (lra_dump_file != NULL)
2358 fprintf (lra_dump_file,
2359 " alt=%d: reload pseudo for op %d "
2360 " can not hold the mode value -- refuse\n",
2361 nalt, nop);
2362 goto fail;
2365 /* Check strong discouragement of reload of non-constant
2366 into class THIS_ALTERNATIVE. */
2367 if (! CONSTANT_P (op) && ! no_regs_p
2368 && (targetm.preferred_reload_class
2369 (op, this_alternative) == NO_REGS
2370 || (curr_static_id->operand[nop].type == OP_OUT
2371 && (targetm.preferred_output_reload_class
2372 (op, this_alternative) == NO_REGS))))
2374 if (lra_dump_file != NULL)
2375 fprintf (lra_dump_file,
2376 " %d Non-prefered reload: reject+=%d\n",
2377 nop, LRA_MAX_REJECT);
2378 reject += LRA_MAX_REJECT;
2381 if (! (MEM_P (op) && offmemok)
2382 && ! (const_to_mem && constmemok))
2384 /* We prefer to reload pseudos over reloading other
2385 things, since such reloads may be able to be
2386 eliminated later. So bump REJECT in other cases.
2387 Don't do this in the case where we are forcing a
2388 constant into memory and it will then win since
2389 we don't want to have a different alternative
2390 match then. */
2391 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2393 if (lra_dump_file != NULL)
2394 fprintf
2395 (lra_dump_file,
2396 " %d Non-pseudo reload: reject+=2\n",
2397 nop);
2398 reject += 2;
2401 if (! no_regs_p)
2402 reload_nregs
2403 += ira_reg_class_max_nregs[this_alternative][mode];
2405 if (SMALL_REGISTER_CLASS_P (this_alternative))
2407 if (lra_dump_file != NULL)
2408 fprintf
2409 (lra_dump_file,
2410 " %d Small class reload: reject+=%d\n",
2411 nop, LRA_LOSER_COST_FACTOR / 2);
2412 reject += LRA_LOSER_COST_FACTOR / 2;
2416 /* We are trying to spill pseudo into memory. It is
2417 usually more costly than moving to a hard register
2418 although it might takes the same number of
2419 reloads. */
2420 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2422 if (lra_dump_file != NULL)
2423 fprintf
2424 (lra_dump_file,
2425 " %d Spill pseudo into memory: reject+=3\n",
2426 nop);
2427 reject += 3;
2428 if (VECTOR_MODE_P (mode))
2430 /* Spilling vectors into memory is usually more
2431 costly as they contain big values. */
2432 if (lra_dump_file != NULL)
2433 fprintf
2434 (lra_dump_file,
2435 " %d Spill vector pseudo: reject+=2\n",
2436 nop);
2437 reject += 2;
2441 #ifdef SECONDARY_MEMORY_NEEDED
2442 /* If reload requires moving value through secondary
2443 memory, it will need one more insn at least. */
2444 if (this_alternative != NO_REGS
2445 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2446 && ((curr_static_id->operand[nop].type != OP_OUT
2447 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2448 GET_MODE (op)))
2449 || (curr_static_id->operand[nop].type != OP_IN
2450 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2451 GET_MODE (op)))))
2452 losers++;
2453 #endif
2454 /* Input reloads can be inherited more often than output
2455 reloads can be removed, so penalize output
2456 reloads. */
2457 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2459 if (lra_dump_file != NULL)
2460 fprintf
2461 (lra_dump_file,
2462 " %d Non input pseudo reload: reject++\n",
2463 nop);
2464 reject++;
2468 if (early_clobber_p && ! scratch_p)
2470 if (lra_dump_file != NULL)
2471 fprintf (lra_dump_file,
2472 " %d Early clobber: reject++\n", nop);
2473 reject++;
2475 /* ??? We check early clobbers after processing all operands
2476 (see loop below) and there we update the costs more.
2477 Should we update the cost (may be approximately) here
2478 because of early clobber register reloads or it is a rare
2479 or non-important thing to be worth to do it. */
2480 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2481 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2483 if (lra_dump_file != NULL)
2484 fprintf (lra_dump_file,
2485 " alt=%d,overall=%d,losers=%d -- refuse\n",
2486 nalt, overall, losers);
2487 goto fail;
2490 curr_alt[nop] = this_alternative;
2491 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2492 curr_alt_win[nop] = this_alternative_win;
2493 curr_alt_match_win[nop] = this_alternative_match_win;
2494 curr_alt_offmemok[nop] = this_alternative_offmemok;
2495 curr_alt_matches[nop] = this_alternative_matches;
2497 if (this_alternative_matches >= 0
2498 && !did_match && !this_alternative_win)
2499 curr_alt_win[this_alternative_matches] = false;
2501 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2502 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2504 if (curr_insn_set != NULL_RTX && n_operands == 2
2505 /* Prevent processing non-move insns. */
2506 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2507 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2508 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2509 && REG_P (no_subreg_reg_operand[0])
2510 && REG_P (no_subreg_reg_operand[1])
2511 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2512 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2513 || (! curr_alt_win[0] && curr_alt_win[1]
2514 && REG_P (no_subreg_reg_operand[1])
2515 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2516 || (curr_alt_win[0] && ! curr_alt_win[1]
2517 && REG_P (no_subreg_reg_operand[0])
2518 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2519 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2520 no_subreg_reg_operand[1])
2521 || (targetm.preferred_reload_class
2522 (no_subreg_reg_operand[1],
2523 (enum reg_class) curr_alt[1]) != NO_REGS))
2524 /* If it is a result of recent elimination in move
2525 insn we can transform it into an add still by
2526 using this alternative. */
2527 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2529 /* We have a move insn and a new reload insn will be similar
2530 to the current insn. We should avoid such situation as it
2531 results in LRA cycling. */
2532 overall += LRA_MAX_REJECT;
2534 ok_p = true;
2535 curr_alt_dont_inherit_ops_num = 0;
2536 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2538 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2539 HARD_REG_SET temp_set;
2541 i = early_clobbered_nops[nop];
2542 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2543 || hard_regno[i] < 0)
2544 continue;
2545 lra_assert (operand_reg[i] != NULL_RTX);
2546 clobbered_hard_regno = hard_regno[i];
2547 CLEAR_HARD_REG_SET (temp_set);
2548 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2549 first_conflict_j = last_conflict_j = -1;
2550 for (j = 0; j < n_operands; j++)
2551 if (j == i
2552 /* We don't want process insides of match_operator and
2553 match_parallel because otherwise we would process
2554 their operands once again generating a wrong
2555 code. */
2556 || curr_static_id->operand[j].is_operator)
2557 continue;
2558 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2559 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2560 continue;
2561 /* If we don't reload j-th operand, check conflicts. */
2562 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2563 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2565 if (first_conflict_j < 0)
2566 first_conflict_j = j;
2567 last_conflict_j = j;
2569 if (last_conflict_j < 0)
2570 continue;
2571 /* If earlyclobber operand conflicts with another
2572 non-matching operand which is actually the same register
2573 as the earlyclobber operand, it is better to reload the
2574 another operand as an operand matching the earlyclobber
2575 operand can be also the same. */
2576 if (first_conflict_j == last_conflict_j
2577 && operand_reg[last_conflict_j]
2578 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2579 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2581 curr_alt_win[last_conflict_j] = false;
2582 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2583 = last_conflict_j;
2584 losers++;
2585 /* Early clobber was already reflected in REJECT. */
2586 lra_assert (reject > 0);
2587 if (lra_dump_file != NULL)
2588 fprintf
2589 (lra_dump_file,
2590 " %d Conflict early clobber reload: reject--\n",
2592 reject--;
2593 overall += LRA_LOSER_COST_FACTOR - 1;
2595 else
2597 /* We need to reload early clobbered register and the
2598 matched registers. */
2599 for (j = 0; j < n_operands; j++)
2600 if (curr_alt_matches[j] == i)
2602 curr_alt_match_win[j] = false;
2603 losers++;
2604 overall += LRA_LOSER_COST_FACTOR;
2606 if (! curr_alt_match_win[i])
2607 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2608 else
2610 /* Remember pseudos used for match reloads are never
2611 inherited. */
2612 lra_assert (curr_alt_matches[i] >= 0);
2613 curr_alt_win[curr_alt_matches[i]] = false;
2615 curr_alt_win[i] = curr_alt_match_win[i] = false;
2616 losers++;
2617 /* Early clobber was already reflected in REJECT. */
2618 lra_assert (reject > 0);
2619 if (lra_dump_file != NULL)
2620 fprintf
2621 (lra_dump_file,
2622 " %d Matched conflict early clobber reloads:"
2623 "reject--\n",
2625 reject--;
2626 overall += LRA_LOSER_COST_FACTOR - 1;
2629 if (lra_dump_file != NULL)
2630 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2631 nalt, overall, losers, reload_nregs);
2633 /* If this alternative can be made to work by reloading, and it
2634 needs less reloading than the others checked so far, record
2635 it as the chosen goal for reloading. */
2636 if ((best_losers != 0 && losers == 0)
2637 || (((best_losers == 0 && losers == 0)
2638 || (best_losers != 0 && losers != 0))
2639 && (best_overall > overall
2640 || (best_overall == overall
2641 /* If the cost of the reloads is the same,
2642 prefer alternative which requires minimal
2643 number of reload regs. */
2644 && (reload_nregs < best_reload_nregs
2645 || (reload_nregs == best_reload_nregs
2646 && (best_reload_sum < reload_sum
2647 || (best_reload_sum == reload_sum
2648 && nalt < goal_alt_number))))))))
2650 for (nop = 0; nop < n_operands; nop++)
2652 goal_alt_win[nop] = curr_alt_win[nop];
2653 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2654 goal_alt_matches[nop] = curr_alt_matches[nop];
2655 goal_alt[nop] = curr_alt[nop];
2656 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2658 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2659 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2660 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2661 goal_alt_swapped = curr_swapped;
2662 best_overall = overall;
2663 best_losers = losers;
2664 best_reload_nregs = reload_nregs;
2665 best_reload_sum = reload_sum;
2666 goal_alt_number = nalt;
2668 if (losers == 0)
2669 /* Everything is satisfied. Do not process alternatives
2670 anymore. */
2671 break;
2672 fail:
2675 return ok_p;
2678 /* Make reload base reg from address AD. */
2679 static rtx
2680 base_to_reg (struct address_info *ad)
2682 enum reg_class cl;
2683 int code = -1;
2684 rtx new_inner = NULL_RTX;
2685 rtx new_reg = NULL_RTX;
2686 rtx_insn *insn;
2687 rtx_insn *last_insn = get_last_insn();
2689 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2690 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2691 get_index_code (ad));
2692 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2693 cl, "base");
2694 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2695 ad->disp_term == NULL
2696 ? gen_int_mode (0, ad->mode)
2697 : *ad->disp_term);
2698 if (!valid_address_p (ad->mode, new_inner, ad->as))
2699 return NULL_RTX;
2700 insn = emit_insn (gen_rtx_SET (ad->mode, new_reg, *ad->base_term));
2701 code = recog_memoized (insn);
2702 if (code < 0)
2704 delete_insns_since (last_insn);
2705 return NULL_RTX;
2708 return new_inner;
2711 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2712 static rtx
2713 base_plus_disp_to_reg (struct address_info *ad)
2715 enum reg_class cl;
2716 rtx new_reg;
2718 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2719 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2720 get_index_code (ad));
2721 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2722 cl, "base + disp");
2723 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2724 return new_reg;
2727 /* Make reload of index part of address AD. Return the new
2728 pseudo. */
2729 static rtx
2730 index_part_to_reg (struct address_info *ad)
2732 rtx new_reg;
2734 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2735 INDEX_REG_CLASS, "index term");
2736 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2737 GEN_INT (get_index_scale (ad)), new_reg, 1);
2738 return new_reg;
2741 /* Return true if we can add a displacement to address AD, even if that
2742 makes the address invalid. The fix-up code requires any new address
2743 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2744 static bool
2745 can_add_disp_p (struct address_info *ad)
2747 return (!ad->autoinc_p
2748 && ad->segment == NULL
2749 && ad->base == ad->base_term
2750 && ad->disp == ad->disp_term);
2753 /* Make equiv substitution in address AD. Return true if a substitution
2754 was made. */
2755 static bool
2756 equiv_address_substitution (struct address_info *ad)
2758 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2759 HOST_WIDE_INT disp, scale;
2760 bool change_p;
2762 base_term = strip_subreg (ad->base_term);
2763 if (base_term == NULL)
2764 base_reg = new_base_reg = NULL_RTX;
2765 else
2767 base_reg = *base_term;
2768 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2770 index_term = strip_subreg (ad->index_term);
2771 if (index_term == NULL)
2772 index_reg = new_index_reg = NULL_RTX;
2773 else
2775 index_reg = *index_term;
2776 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2778 if (base_reg == new_base_reg && index_reg == new_index_reg)
2779 return false;
2780 disp = 0;
2781 change_p = false;
2782 if (lra_dump_file != NULL)
2784 fprintf (lra_dump_file, "Changing address in insn %d ",
2785 INSN_UID (curr_insn));
2786 dump_value_slim (lra_dump_file, *ad->outer, 1);
2788 if (base_reg != new_base_reg)
2790 if (REG_P (new_base_reg))
2792 *base_term = new_base_reg;
2793 change_p = true;
2795 else if (GET_CODE (new_base_reg) == PLUS
2796 && REG_P (XEXP (new_base_reg, 0))
2797 && CONST_INT_P (XEXP (new_base_reg, 1))
2798 && can_add_disp_p (ad))
2800 disp += INTVAL (XEXP (new_base_reg, 1));
2801 *base_term = XEXP (new_base_reg, 0);
2802 change_p = true;
2804 if (ad->base_term2 != NULL)
2805 *ad->base_term2 = *ad->base_term;
2807 if (index_reg != new_index_reg)
2809 if (REG_P (new_index_reg))
2811 *index_term = new_index_reg;
2812 change_p = true;
2814 else if (GET_CODE (new_index_reg) == PLUS
2815 && REG_P (XEXP (new_index_reg, 0))
2816 && CONST_INT_P (XEXP (new_index_reg, 1))
2817 && can_add_disp_p (ad)
2818 && (scale = get_index_scale (ad)))
2820 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2821 *index_term = XEXP (new_index_reg, 0);
2822 change_p = true;
2825 if (disp != 0)
2827 if (ad->disp != NULL)
2828 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2829 else
2831 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2832 update_address (ad);
2834 change_p = true;
2836 if (lra_dump_file != NULL)
2838 if (! change_p)
2839 fprintf (lra_dump_file, " -- no change\n");
2840 else
2842 fprintf (lra_dump_file, " on equiv ");
2843 dump_value_slim (lra_dump_file, *ad->outer, 1);
2844 fprintf (lra_dump_file, "\n");
2847 return change_p;
2850 /* Major function to make reloads for an address in operand NOP or
2851 check its correctness (If CHECK_ONLY_P is true). The supported
2852 cases are:
2854 1) an address that existed before LRA started, at which point it
2855 must have been valid. These addresses are subject to elimination
2856 and may have become invalid due to the elimination offset being out
2857 of range.
2859 2) an address created by forcing a constant to memory
2860 (force_const_to_mem). The initial form of these addresses might
2861 not be valid, and it is this function's job to make them valid.
2863 3) a frame address formed from a register and a (possibly zero)
2864 constant offset. As above, these addresses might not be valid and
2865 this function must make them so.
2867 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2868 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2869 address. Return true for any RTL change.
2871 The function is a helper function which does not produce all
2872 transformations (when CHECK_ONLY_P is false) which can be
2873 necessary. It does just basic steps. To do all necessary
2874 transformations use function process_address. */
2875 static bool
2876 process_address_1 (int nop, bool check_only_p,
2877 rtx_insn **before, rtx_insn **after)
2879 struct address_info ad;
2880 rtx new_reg;
2881 rtx op = *curr_id->operand_loc[nop];
2882 const char *constraint = curr_static_id->operand[nop].constraint;
2883 enum constraint_num cn = lookup_constraint (constraint);
2884 bool change_p = false;
2886 if (insn_extra_address_constraint (cn))
2887 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2888 else if (MEM_P (op))
2889 decompose_mem_address (&ad, op);
2890 else if (GET_CODE (op) == SUBREG
2891 && MEM_P (SUBREG_REG (op)))
2892 decompose_mem_address (&ad, SUBREG_REG (op));
2893 else
2894 return false;
2895 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
2896 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
2897 when INDEX_REG_CLASS is a single register class. */
2898 if (ad.base_term != NULL
2899 && ad.index_term != NULL
2900 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
2901 && REG_P (*ad.base_term)
2902 && REG_P (*ad.index_term)
2903 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
2904 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
2906 std::swap (ad.base, ad.index);
2907 std::swap (ad.base_term, ad.index_term);
2909 if (! check_only_p)
2910 change_p = equiv_address_substitution (&ad);
2911 if (ad.base_term != NULL
2912 && (process_addr_reg
2913 (ad.base_term, check_only_p, before,
2914 (ad.autoinc_p
2915 && !(REG_P (*ad.base_term)
2916 && find_regno_note (curr_insn, REG_DEAD,
2917 REGNO (*ad.base_term)) != NULL_RTX)
2918 ? after : NULL),
2919 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2920 get_index_code (&ad)))))
2922 change_p = true;
2923 if (ad.base_term2 != NULL)
2924 *ad.base_term2 = *ad.base_term;
2926 if (ad.index_term != NULL
2927 && process_addr_reg (ad.index_term, check_only_p,
2928 before, NULL, INDEX_REG_CLASS))
2929 change_p = true;
2931 /* Target hooks sometimes don't treat extra-constraint addresses as
2932 legitimate address_operands, so handle them specially. */
2933 if (insn_extra_address_constraint (cn)
2934 && satisfies_address_constraint_p (&ad, cn))
2935 return change_p;
2937 if (check_only_p)
2938 return change_p;
2940 /* There are three cases where the shape of *AD.INNER may now be invalid:
2942 1) the original address was valid, but either elimination or
2943 equiv_address_substitution was applied and that made
2944 the address invalid.
2946 2) the address is an invalid symbolic address created by
2947 force_const_to_mem.
2949 3) the address is a frame address with an invalid offset.
2951 4) the address is a frame address with an invalid base.
2953 All these cases involve a non-autoinc address, so there is no
2954 point revalidating other types. */
2955 if (ad.autoinc_p || valid_address_p (&ad))
2956 return change_p;
2958 /* Any index existed before LRA started, so we can assume that the
2959 presence and shape of the index is valid. */
2960 push_to_sequence (*before);
2961 lra_assert (ad.disp == ad.disp_term);
2962 if (ad.base == NULL)
2964 if (ad.index == NULL)
2966 int code = -1;
2967 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2968 SCRATCH, SCRATCH);
2969 rtx addr = *ad.inner;
2971 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2972 #ifdef HAVE_lo_sum
2974 rtx_insn *insn;
2975 rtx_insn *last = get_last_insn ();
2977 /* addr => lo_sum (new_base, addr), case (2) above. */
2978 insn = emit_insn (gen_rtx_SET
2979 (VOIDmode, new_reg,
2980 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2981 code = recog_memoized (insn);
2982 if (code >= 0)
2984 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2985 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2987 /* Try to put lo_sum into register. */
2988 insn = emit_insn (gen_rtx_SET
2989 (VOIDmode, new_reg,
2990 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2991 code = recog_memoized (insn);
2992 if (code >= 0)
2994 *ad.inner = new_reg;
2995 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2997 *ad.inner = addr;
2998 code = -1;
3004 if (code < 0)
3005 delete_insns_since (last);
3007 #endif
3008 if (code < 0)
3010 /* addr => new_base, case (2) above. */
3011 lra_emit_move (new_reg, addr);
3012 *ad.inner = new_reg;
3015 else
3017 /* index * scale + disp => new base + index * scale,
3018 case (1) above. */
3019 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3020 GET_CODE (*ad.index));
3022 lra_assert (INDEX_REG_CLASS != NO_REGS);
3023 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3024 lra_emit_move (new_reg, *ad.disp);
3025 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3026 new_reg, *ad.index);
3029 else if (ad.index == NULL)
3031 int regno;
3032 enum reg_class cl;
3033 rtx set;
3034 rtx_insn *insns, *last_insn;
3035 /* Try to reload base into register only if the base is invalid
3036 for the address but with valid offset, case (4) above. */
3037 start_sequence ();
3038 new_reg = base_to_reg (&ad);
3040 /* base + disp => new base, cases (1) and (3) above. */
3041 /* Another option would be to reload the displacement into an
3042 index register. However, postreload has code to optimize
3043 address reloads that have the same base and different
3044 displacements, so reloading into an index register would
3045 not necessarily be a win. */
3046 if (new_reg == NULL_RTX)
3047 new_reg = base_plus_disp_to_reg (&ad);
3048 insns = get_insns ();
3049 last_insn = get_last_insn ();
3050 /* If we generated at least two insns, try last insn source as
3051 an address. If we succeed, we generate one less insn. */
3052 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3053 && GET_CODE (SET_SRC (set)) == PLUS
3054 && REG_P (XEXP (SET_SRC (set), 0))
3055 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3057 *ad.inner = SET_SRC (set);
3058 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3060 *ad.base_term = XEXP (SET_SRC (set), 0);
3061 *ad.disp_term = XEXP (SET_SRC (set), 1);
3062 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3063 get_index_code (&ad));
3064 regno = REGNO (*ad.base_term);
3065 if (regno >= FIRST_PSEUDO_REGISTER
3066 && cl != lra_get_allocno_class (regno))
3067 lra_change_class (regno, cl, " Change to", true);
3068 new_reg = SET_SRC (set);
3069 delete_insns_since (PREV_INSN (last_insn));
3072 /* Try if target can split displacement into legitimite new disp
3073 and offset. If it's the case, we replace the last insn with
3074 insns for base + offset => new_reg and set new_reg + new disp
3075 to *ad.inner. */
3076 last_insn = get_last_insn ();
3077 if ((set = single_set (last_insn)) != NULL_RTX
3078 && GET_CODE (SET_SRC (set)) == PLUS
3079 && REG_P (XEXP (SET_SRC (set), 0))
3080 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3081 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3083 rtx addend, disp = XEXP (SET_SRC (set), 1);
3084 if (targetm.legitimize_address_displacement (&disp, &addend,
3085 ad.mode))
3087 rtx_insn *new_insns;
3088 start_sequence ();
3089 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3090 new_insns = get_insns ();
3091 end_sequence ();
3092 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3093 delete_insns_since (PREV_INSN (last_insn));
3094 add_insn (new_insns);
3095 insns = get_insns ();
3098 end_sequence ();
3099 emit_insn (insns);
3100 *ad.inner = new_reg;
3102 else if (ad.disp_term != NULL)
3104 /* base + scale * index + disp => new base + scale * index,
3105 case (1) above. */
3106 new_reg = base_plus_disp_to_reg (&ad);
3107 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3108 new_reg, *ad.index);
3110 else if (get_index_scale (&ad) == 1)
3112 /* The last transformation to one reg will be made in
3113 curr_insn_transform function. */
3114 end_sequence ();
3115 return false;
3117 else
3119 /* base + scale * index => base + new_reg,
3120 case (1) above.
3121 Index part of address may become invalid. For example, we
3122 changed pseudo on the equivalent memory and a subreg of the
3123 pseudo onto the memory of different mode for which the scale is
3124 prohibitted. */
3125 new_reg = index_part_to_reg (&ad);
3126 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3127 *ad.base_term, new_reg);
3129 *before = get_insns ();
3130 end_sequence ();
3131 return true;
3134 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3135 Use process_address_1 as a helper function. Return true for any
3136 RTL changes.
3138 If CHECK_ONLY_P is true, just check address correctness. Return
3139 false if the address correct. */
3140 static bool
3141 process_address (int nop, bool check_only_p,
3142 rtx_insn **before, rtx_insn **after)
3144 bool res = false;
3146 while (process_address_1 (nop, check_only_p, before, after))
3148 if (check_only_p)
3149 return true;
3150 res = true;
3152 return res;
3155 /* Emit insns to reload VALUE into a new register. VALUE is an
3156 auto-increment or auto-decrement RTX whose operand is a register or
3157 memory location; so reloading involves incrementing that location.
3158 IN is either identical to VALUE, or some cheaper place to reload
3159 value being incremented/decremented from.
3161 INC_AMOUNT is the number to increment or decrement by (always
3162 positive and ignored for POST_MODIFY/PRE_MODIFY).
3164 Return pseudo containing the result. */
3165 static rtx
3166 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3168 /* REG or MEM to be copied and incremented. */
3169 rtx incloc = XEXP (value, 0);
3170 /* Nonzero if increment after copying. */
3171 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3172 || GET_CODE (value) == POST_MODIFY);
3173 rtx_insn *last;
3174 rtx inc;
3175 rtx_insn *add_insn;
3176 int code;
3177 rtx real_in = in == value ? incloc : in;
3178 rtx result;
3179 bool plus_p = true;
3181 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3183 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3184 || GET_CODE (XEXP (value, 1)) == MINUS);
3185 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3186 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3187 inc = XEXP (XEXP (value, 1), 1);
3189 else
3191 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3192 inc_amount = -inc_amount;
3194 inc = GEN_INT (inc_amount);
3197 if (! post && REG_P (incloc))
3198 result = incloc;
3199 else
3200 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3201 "INC/DEC result");
3203 if (real_in != result)
3205 /* First copy the location to the result register. */
3206 lra_assert (REG_P (result));
3207 emit_insn (gen_move_insn (result, real_in));
3210 /* We suppose that there are insns to add/sub with the constant
3211 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3212 old reload worked with this assumption. If the assumption
3213 becomes wrong, we should use approach in function
3214 base_plus_disp_to_reg. */
3215 if (in == value)
3217 /* See if we can directly increment INCLOC. */
3218 last = get_last_insn ();
3219 add_insn = emit_insn (plus_p
3220 ? gen_add2_insn (incloc, inc)
3221 : gen_sub2_insn (incloc, inc));
3223 code = recog_memoized (add_insn);
3224 if (code >= 0)
3226 if (! post && result != incloc)
3227 emit_insn (gen_move_insn (result, incloc));
3228 return result;
3230 delete_insns_since (last);
3233 /* If couldn't do the increment directly, must increment in RESULT.
3234 The way we do this depends on whether this is pre- or
3235 post-increment. For pre-increment, copy INCLOC to the reload
3236 register, increment it there, then save back. */
3237 if (! post)
3239 if (real_in != result)
3240 emit_insn (gen_move_insn (result, real_in));
3241 if (plus_p)
3242 emit_insn (gen_add2_insn (result, inc));
3243 else
3244 emit_insn (gen_sub2_insn (result, inc));
3245 if (result != incloc)
3246 emit_insn (gen_move_insn (incloc, result));
3248 else
3250 /* Post-increment.
3252 Because this might be a jump insn or a compare, and because
3253 RESULT may not be available after the insn in an input
3254 reload, we must do the incrementing before the insn being
3255 reloaded for.
3257 We have already copied IN to RESULT. Increment the copy in
3258 RESULT, save that back, then decrement RESULT so it has
3259 the original value. */
3260 if (plus_p)
3261 emit_insn (gen_add2_insn (result, inc));
3262 else
3263 emit_insn (gen_sub2_insn (result, inc));
3264 emit_insn (gen_move_insn (incloc, result));
3265 /* Restore non-modified value for the result. We prefer this
3266 way because it does not require an additional hard
3267 register. */
3268 if (plus_p)
3270 if (CONST_INT_P (inc))
3271 emit_insn (gen_add2_insn (result,
3272 gen_int_mode (-INTVAL (inc),
3273 GET_MODE (result))));
3274 else
3275 emit_insn (gen_sub2_insn (result, inc));
3277 else
3278 emit_insn (gen_add2_insn (result, inc));
3280 return result;
3283 /* Return true if the current move insn does not need processing as we
3284 already know that it satisfies its constraints. */
3285 static bool
3286 simple_move_p (void)
3288 rtx dest, src;
3289 enum reg_class dclass, sclass;
3291 lra_assert (curr_insn_set != NULL_RTX);
3292 dest = SET_DEST (curr_insn_set);
3293 src = SET_SRC (curr_insn_set);
3294 return ((dclass = get_op_class (dest)) != NO_REGS
3295 && (sclass = get_op_class (src)) != NO_REGS
3296 /* The backend guarantees that register moves of cost 2
3297 never need reloads. */
3298 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3301 /* Swap operands NOP and NOP + 1. */
3302 static inline void
3303 swap_operands (int nop)
3305 machine_mode mode = curr_operand_mode[nop];
3306 curr_operand_mode[nop] = curr_operand_mode[nop + 1];
3307 curr_operand_mode[nop + 1] = mode;
3308 mode = original_subreg_reg_mode[nop];
3309 original_subreg_reg_mode[nop] = original_subreg_reg_mode[nop + 1];
3310 original_subreg_reg_mode[nop + 1] = mode;
3311 rtx x = *curr_id->operand_loc[nop];
3312 *curr_id->operand_loc[nop] = *curr_id->operand_loc[nop + 1];
3313 *curr_id->operand_loc[nop + 1] = x;
3314 /* Swap the duplicates too. */
3315 lra_update_dup (curr_id, nop);
3316 lra_update_dup (curr_id, nop + 1);
3319 /* Main entry point of the constraint code: search the body of the
3320 current insn to choose the best alternative. It is mimicking insn
3321 alternative cost calculation model of former reload pass. That is
3322 because machine descriptions were written to use this model. This
3323 model can be changed in future. Make commutative operand exchange
3324 if it is chosen.
3326 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3327 constraints. Return true if any change happened during function
3328 call.
3330 If CHECK_ONLY_P is true then don't do any transformation. Just
3331 check that the insn satisfies all constraints. If the insn does
3332 not satisfy any constraint, return true. */
3333 static bool
3334 curr_insn_transform (bool check_only_p)
3336 int i, j, k;
3337 int n_operands;
3338 int n_alternatives;
3339 int commutative;
3340 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3341 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3342 rtx_insn *before, *after;
3343 bool alt_p = false;
3344 /* Flag that the insn has been changed through a transformation. */
3345 bool change_p;
3346 bool sec_mem_p;
3347 #ifdef SECONDARY_MEMORY_NEEDED
3348 bool use_sec_mem_p;
3349 #endif
3350 int max_regno_before;
3351 int reused_alternative_num;
3353 curr_insn_set = single_set (curr_insn);
3354 if (curr_insn_set != NULL_RTX && simple_move_p ())
3355 return false;
3357 no_input_reloads_p = no_output_reloads_p = false;
3358 goal_alt_number = -1;
3359 change_p = sec_mem_p = false;
3360 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3361 reloads; neither are insns that SET cc0. Insns that use CC0 are
3362 not allowed to have any input reloads. */
3363 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3364 no_output_reloads_p = true;
3366 #ifdef HAVE_cc0
3367 if (reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3368 no_input_reloads_p = true;
3369 if (reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3370 no_output_reloads_p = true;
3371 #endif
3373 n_operands = curr_static_id->n_operands;
3374 n_alternatives = curr_static_id->n_alternatives;
3376 /* Just return "no reloads" if insn has no operands with
3377 constraints. */
3378 if (n_operands == 0 || n_alternatives == 0)
3379 return false;
3381 max_regno_before = max_reg_num ();
3383 for (i = 0; i < n_operands; i++)
3385 goal_alt_matched[i][0] = -1;
3386 goal_alt_matches[i] = -1;
3389 commutative = curr_static_id->commutative;
3391 /* Now see what we need for pseudos that didn't get hard regs or got
3392 the wrong kind of hard reg. For this, we must consider all the
3393 operands together against the register constraints. */
3395 best_losers = best_overall = INT_MAX;
3396 best_reload_sum = 0;
3398 curr_swapped = false;
3399 goal_alt_swapped = false;
3401 if (! check_only_p)
3402 /* Make equivalence substitution and memory subreg elimination
3403 before address processing because an address legitimacy can
3404 depend on memory mode. */
3405 for (i = 0; i < n_operands; i++)
3407 rtx op = *curr_id->operand_loc[i];
3408 rtx subst, old = op;
3409 bool op_change_p = false;
3411 if (GET_CODE (old) == SUBREG)
3412 old = SUBREG_REG (old);
3413 subst = get_equiv_with_elimination (old, curr_insn);
3414 original_subreg_reg_mode[i] = VOIDmode;
3415 if (subst != old)
3417 subst = copy_rtx (subst);
3418 lra_assert (REG_P (old));
3419 if (GET_CODE (op) != SUBREG)
3420 *curr_id->operand_loc[i] = subst;
3421 else
3423 SUBREG_REG (op) = subst;
3424 if (GET_MODE (subst) == VOIDmode)
3425 original_subreg_reg_mode[i] = GET_MODE (old);
3427 if (lra_dump_file != NULL)
3429 fprintf (lra_dump_file,
3430 "Changing pseudo %d in operand %i of insn %u on equiv ",
3431 REGNO (old), i, INSN_UID (curr_insn));
3432 dump_value_slim (lra_dump_file, subst, 1);
3433 fprintf (lra_dump_file, "\n");
3435 op_change_p = change_p = true;
3437 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3439 change_p = true;
3440 lra_update_dup (curr_id, i);
3444 /* Reload address registers and displacements. We do it before
3445 finding an alternative because of memory constraints. */
3446 before = after = NULL;
3447 for (i = 0; i < n_operands; i++)
3448 if (! curr_static_id->operand[i].is_operator
3449 && process_address (i, check_only_p, &before, &after))
3451 if (check_only_p)
3452 return true;
3453 change_p = true;
3454 lra_update_dup (curr_id, i);
3457 if (change_p)
3458 /* If we've changed the instruction then any alternative that
3459 we chose previously may no longer be valid. */
3460 lra_set_used_insn_alternative (curr_insn, -1);
3462 if (! check_only_p && curr_insn_set != NULL_RTX
3463 && check_and_process_move (&change_p, &sec_mem_p))
3464 return change_p;
3466 try_swapped:
3468 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3469 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3470 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3471 reused_alternative_num, INSN_UID (curr_insn));
3473 if (process_alt_operands (reused_alternative_num))
3474 alt_p = true;
3476 if (check_only_p)
3477 return ! alt_p || best_losers != 0;
3479 /* If insn is commutative (it's safe to exchange a certain pair of
3480 operands) then we need to try each alternative twice, the second
3481 time matching those two operands as if we had exchanged them. To
3482 do this, really exchange them in operands.
3484 If we have just tried the alternatives the second time, return
3485 operands to normal and drop through. */
3487 if (reused_alternative_num < 0 && commutative >= 0)
3489 curr_swapped = !curr_swapped;
3490 if (curr_swapped)
3492 swap_operands (commutative);
3493 goto try_swapped;
3495 else
3496 swap_operands (commutative);
3499 if (! alt_p && ! sec_mem_p)
3501 /* No alternative works with reloads?? */
3502 if (INSN_CODE (curr_insn) >= 0)
3503 fatal_insn ("unable to generate reloads for:", curr_insn);
3504 error_for_asm (curr_insn,
3505 "inconsistent operand constraints in an %<asm%>");
3506 /* Avoid further trouble with this insn. */
3507 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3508 lra_invalidate_insn_data (curr_insn);
3509 return true;
3512 /* If the best alternative is with operands 1 and 2 swapped, swap
3513 them. Update the operand numbers of any reloads already
3514 pushed. */
3516 if (goal_alt_swapped)
3518 if (lra_dump_file != NULL)
3519 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3520 INSN_UID (curr_insn));
3522 /* Swap the duplicates too. */
3523 swap_operands (commutative);
3524 change_p = true;
3527 #ifdef SECONDARY_MEMORY_NEEDED
3528 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3529 too conservatively. So we use the secondary memory only if there
3530 is no any alternative without reloads. */
3531 use_sec_mem_p = false;
3532 if (! alt_p)
3533 use_sec_mem_p = true;
3534 else if (sec_mem_p)
3536 for (i = 0; i < n_operands; i++)
3537 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3538 break;
3539 use_sec_mem_p = i < n_operands;
3542 if (use_sec_mem_p)
3544 rtx new_reg, src, dest, rld;
3545 machine_mode sec_mode, rld_mode;
3547 lra_assert (sec_mem_p);
3548 lra_assert (curr_static_id->operand[0].type == OP_OUT
3549 && curr_static_id->operand[1].type == OP_IN);
3550 dest = *curr_id->operand_loc[0];
3551 src = *curr_id->operand_loc[1];
3552 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3553 ? dest : src);
3554 rld_mode = GET_MODE (rld);
3555 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3556 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3557 #else
3558 sec_mode = rld_mode;
3559 #endif
3560 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3561 NO_REGS, "secondary");
3562 /* If the mode is changed, it should be wider. */
3563 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3564 if (sec_mode != rld_mode)
3566 /* If the target says specifically to use another mode for
3567 secondary memory moves we can not reuse the original
3568 insn. */
3569 after = emit_spill_move (false, new_reg, dest);
3570 lra_process_new_insns (curr_insn, NULL, after,
3571 "Inserting the sec. move");
3572 /* We may have non null BEFORE here (e.g. after address
3573 processing. */
3574 push_to_sequence (before);
3575 before = emit_spill_move (true, new_reg, src);
3576 emit_insn (before);
3577 before = get_insns ();
3578 end_sequence ();
3579 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3580 lra_set_insn_deleted (curr_insn);
3582 else if (dest == rld)
3584 *curr_id->operand_loc[0] = new_reg;
3585 after = emit_spill_move (false, new_reg, dest);
3586 lra_process_new_insns (curr_insn, NULL, after,
3587 "Inserting the sec. move");
3589 else
3591 *curr_id->operand_loc[1] = new_reg;
3592 /* See comments above. */
3593 push_to_sequence (before);
3594 before = emit_spill_move (true, new_reg, src);
3595 emit_insn (before);
3596 before = get_insns ();
3597 end_sequence ();
3598 lra_process_new_insns (curr_insn, before, NULL,
3599 "Inserting the sec. move");
3601 lra_update_insn_regno_info (curr_insn);
3602 return true;
3604 #endif
3606 lra_assert (goal_alt_number >= 0);
3607 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3609 if (lra_dump_file != NULL)
3611 const char *p;
3613 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3614 goal_alt_number, INSN_UID (curr_insn));
3615 for (i = 0; i < n_operands; i++)
3617 p = (curr_static_id->operand_alternative
3618 [goal_alt_number * n_operands + i].constraint);
3619 if (*p == '\0')
3620 continue;
3621 fprintf (lra_dump_file, " (%d) ", i);
3622 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3623 fputc (*p, lra_dump_file);
3625 if (INSN_CODE (curr_insn) >= 0
3626 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3627 fprintf (lra_dump_file, " {%s}", p);
3628 if (curr_id->sp_offset != 0)
3629 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3630 curr_id->sp_offset);
3631 fprintf (lra_dump_file, "\n");
3634 /* Right now, for any pair of operands I and J that are required to
3635 match, with J < I, goal_alt_matches[I] is J. Add I to
3636 goal_alt_matched[J]. */
3638 for (i = 0; i < n_operands; i++)
3639 if ((j = goal_alt_matches[i]) >= 0)
3641 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3643 /* We allow matching one output operand and several input
3644 operands. */
3645 lra_assert (k == 0
3646 || (curr_static_id->operand[j].type == OP_OUT
3647 && curr_static_id->operand[i].type == OP_IN
3648 && (curr_static_id->operand
3649 [goal_alt_matched[j][0]].type == OP_IN)));
3650 goal_alt_matched[j][k] = i;
3651 goal_alt_matched[j][k + 1] = -1;
3654 for (i = 0; i < n_operands; i++)
3655 goal_alt_win[i] |= goal_alt_match_win[i];
3657 /* Any constants that aren't allowed and can't be reloaded into
3658 registers are here changed into memory references. */
3659 for (i = 0; i < n_operands; i++)
3660 if (goal_alt_win[i])
3662 int regno;
3663 enum reg_class new_class;
3664 rtx reg = *curr_id->operand_loc[i];
3666 if (GET_CODE (reg) == SUBREG)
3667 reg = SUBREG_REG (reg);
3669 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3671 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3673 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3675 lra_assert (ok_p);
3676 lra_change_class (regno, new_class, " Change to", true);
3680 else
3682 const char *constraint;
3683 char c;
3684 rtx op = *curr_id->operand_loc[i];
3685 rtx subreg = NULL_RTX;
3686 machine_mode mode = curr_operand_mode[i];
3688 if (GET_CODE (op) == SUBREG)
3690 subreg = op;
3691 op = SUBREG_REG (op);
3692 mode = GET_MODE (op);
3695 if (CONST_POOL_OK_P (mode, op)
3696 && ((targetm.preferred_reload_class
3697 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3698 || no_input_reloads_p))
3700 rtx tem = force_const_mem (mode, op);
3702 change_p = true;
3703 if (subreg != NULL_RTX)
3704 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3706 *curr_id->operand_loc[i] = tem;
3707 lra_update_dup (curr_id, i);
3708 process_address (i, false, &before, &after);
3710 /* If the alternative accepts constant pool refs directly
3711 there will be no reload needed at all. */
3712 if (subreg != NULL_RTX)
3713 continue;
3714 /* Skip alternatives before the one requested. */
3715 constraint = (curr_static_id->operand_alternative
3716 [goal_alt_number * n_operands + i].constraint);
3717 for (;
3718 (c = *constraint) && c != ',' && c != '#';
3719 constraint += CONSTRAINT_LEN (c, constraint))
3721 enum constraint_num cn = lookup_constraint (constraint);
3722 if (insn_extra_memory_constraint (cn)
3723 && satisfies_memory_constraint_p (tem, cn))
3724 break;
3726 if (c == '\0' || c == ',' || c == '#')
3727 continue;
3729 goal_alt_win[i] = true;
3733 for (i = 0; i < n_operands; i++)
3735 int regno;
3736 bool optional_p = false;
3737 rtx old, new_reg;
3738 rtx op = *curr_id->operand_loc[i];
3740 if (goal_alt_win[i])
3742 if (goal_alt[i] == NO_REGS
3743 && REG_P (op)
3744 /* When we assign NO_REGS it means that we will not
3745 assign a hard register to the scratch pseudo by
3746 assigment pass and the scratch pseudo will be
3747 spilled. Spilled scratch pseudos are transformed
3748 back to scratches at the LRA end. */
3749 && lra_former_scratch_operand_p (curr_insn, i))
3751 int regno = REGNO (op);
3752 lra_change_class (regno, NO_REGS, " Change to", true);
3753 if (lra_get_regno_hard_regno (regno) >= 0)
3754 /* We don't have to mark all insn affected by the
3755 spilled pseudo as there is only one such insn, the
3756 current one. */
3757 reg_renumber[regno] = -1;
3759 /* We can do an optional reload. If the pseudo got a hard
3760 reg, we might improve the code through inheritance. If
3761 it does not get a hard register we coalesce memory/memory
3762 moves later. Ignore move insns to avoid cycling. */
3763 if (! lra_simple_p
3764 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3765 && goal_alt[i] != NO_REGS && REG_P (op)
3766 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3767 && regno < new_regno_start
3768 && ! lra_former_scratch_p (regno)
3769 && reg_renumber[regno] < 0
3770 /* Check that the optional reload pseudo will be able to
3771 hold given mode value. */
3772 && ! (prohibited_class_reg_set_mode_p
3773 (goal_alt[i], reg_class_contents[goal_alt[i]],
3774 PSEUDO_REGNO_MODE (regno)))
3775 && (curr_insn_set == NULL_RTX
3776 || !((REG_P (SET_SRC (curr_insn_set))
3777 || MEM_P (SET_SRC (curr_insn_set))
3778 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3779 && (REG_P (SET_DEST (curr_insn_set))
3780 || MEM_P (SET_DEST (curr_insn_set))
3781 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3782 optional_p = true;
3783 else
3784 continue;
3787 /* Operands that match previous ones have already been handled. */
3788 if (goal_alt_matches[i] >= 0)
3789 continue;
3791 /* We should not have an operand with a non-offsettable address
3792 appearing where an offsettable address will do. It also may
3793 be a case when the address should be special in other words
3794 not a general one (e.g. it needs no index reg). */
3795 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3797 enum reg_class rclass;
3798 rtx *loc = &XEXP (op, 0);
3799 enum rtx_code code = GET_CODE (*loc);
3801 push_to_sequence (before);
3802 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3803 MEM, SCRATCH);
3804 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3805 new_reg = emit_inc (rclass, *loc, *loc,
3806 /* This value does not matter for MODIFY. */
3807 GET_MODE_SIZE (GET_MODE (op)));
3808 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3809 "offsetable address", &new_reg))
3810 lra_emit_move (new_reg, *loc);
3811 before = get_insns ();
3812 end_sequence ();
3813 *loc = new_reg;
3814 lra_update_dup (curr_id, i);
3816 else if (goal_alt_matched[i][0] == -1)
3818 machine_mode mode;
3819 rtx reg, *loc;
3820 int hard_regno, byte;
3821 enum op_type type = curr_static_id->operand[i].type;
3823 loc = curr_id->operand_loc[i];
3824 mode = curr_operand_mode[i];
3825 if (GET_CODE (*loc) == SUBREG)
3827 reg = SUBREG_REG (*loc);
3828 byte = SUBREG_BYTE (*loc);
3829 if (REG_P (reg)
3830 /* Strict_low_part requires reload the register not
3831 the sub-register. */
3832 && (curr_static_id->operand[i].strict_low
3833 || (GET_MODE_SIZE (mode)
3834 <= GET_MODE_SIZE (GET_MODE (reg))
3835 && (hard_regno
3836 = get_try_hard_regno (REGNO (reg))) >= 0
3837 && (simplify_subreg_regno
3838 (hard_regno,
3839 GET_MODE (reg), byte, mode) < 0)
3840 && (goal_alt[i] == NO_REGS
3841 || (simplify_subreg_regno
3842 (ira_class_hard_regs[goal_alt[i]][0],
3843 GET_MODE (reg), byte, mode) >= 0)))))
3845 if (type == OP_OUT)
3846 type = OP_INOUT;
3847 loc = &SUBREG_REG (*loc);
3848 mode = GET_MODE (*loc);
3851 old = *loc;
3852 if (get_reload_reg (type, mode, old, goal_alt[i],
3853 loc != curr_id->operand_loc[i], "", &new_reg)
3854 && type != OP_OUT)
3856 push_to_sequence (before);
3857 lra_emit_move (new_reg, old);
3858 before = get_insns ();
3859 end_sequence ();
3861 *loc = new_reg;
3862 if (type != OP_IN
3863 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3865 start_sequence ();
3866 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3867 emit_insn (after);
3868 after = get_insns ();
3869 end_sequence ();
3870 *loc = new_reg;
3872 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3873 if (goal_alt_dont_inherit_ops[j] == i)
3875 lra_set_regno_unique_value (REGNO (new_reg));
3876 break;
3878 lra_update_dup (curr_id, i);
3880 else if (curr_static_id->operand[i].type == OP_IN
3881 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3882 == OP_OUT))
3884 /* generate reloads for input and matched outputs. */
3885 match_inputs[0] = i;
3886 match_inputs[1] = -1;
3887 match_reload (goal_alt_matched[i][0], match_inputs,
3888 goal_alt[i], &before, &after);
3890 else if (curr_static_id->operand[i].type == OP_OUT
3891 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3892 == OP_IN))
3893 /* Generate reloads for output and matched inputs. */
3894 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3895 else if (curr_static_id->operand[i].type == OP_IN
3896 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3897 == OP_IN))
3899 /* Generate reloads for matched inputs. */
3900 match_inputs[0] = i;
3901 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3902 match_inputs[j + 1] = k;
3903 match_inputs[j + 1] = -1;
3904 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3906 else
3907 /* We must generate code in any case when function
3908 process_alt_operands decides that it is possible. */
3909 gcc_unreachable ();
3910 if (optional_p)
3912 lra_assert (REG_P (op));
3913 regno = REGNO (op);
3914 op = *curr_id->operand_loc[i]; /* Substitution. */
3915 if (GET_CODE (op) == SUBREG)
3916 op = SUBREG_REG (op);
3917 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3918 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3919 lra_reg_info[REGNO (op)].restore_regno = regno;
3920 if (lra_dump_file != NULL)
3921 fprintf (lra_dump_file,
3922 " Making reload reg %d for reg %d optional\n",
3923 REGNO (op), regno);
3926 if (before != NULL_RTX || after != NULL_RTX
3927 || max_regno_before != max_reg_num ())
3928 change_p = true;
3929 if (change_p)
3931 lra_update_operator_dups (curr_id);
3932 /* Something changes -- process the insn. */
3933 lra_update_insn_regno_info (curr_insn);
3935 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3936 return change_p;
3939 /* Return true if INSN satisfies all constraints. In other words, no
3940 reload insns are needed. */
3941 bool
3942 lra_constrain_insn (rtx_insn *insn)
3944 int saved_new_regno_start = new_regno_start;
3945 int saved_new_insn_uid_start = new_insn_uid_start;
3946 bool change_p;
3948 curr_insn = insn;
3949 curr_id = lra_get_insn_recog_data (curr_insn);
3950 curr_static_id = curr_id->insn_static_data;
3951 new_insn_uid_start = get_max_uid ();
3952 new_regno_start = max_reg_num ();
3953 change_p = curr_insn_transform (true);
3954 new_regno_start = saved_new_regno_start;
3955 new_insn_uid_start = saved_new_insn_uid_start;
3956 return ! change_p;
3959 /* Return true if X is in LIST. */
3960 static bool
3961 in_list_p (rtx x, rtx list)
3963 for (; list != NULL_RTX; list = XEXP (list, 1))
3964 if (XEXP (list, 0) == x)
3965 return true;
3966 return false;
3969 /* Return true if X contains an allocatable hard register (if
3970 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3971 static bool
3972 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3974 int i, j;
3975 const char *fmt;
3976 enum rtx_code code;
3978 code = GET_CODE (x);
3979 if (REG_P (x))
3981 int regno = REGNO (x);
3982 HARD_REG_SET alloc_regs;
3984 if (hard_reg_p)
3986 if (regno >= FIRST_PSEUDO_REGISTER)
3987 regno = lra_get_regno_hard_regno (regno);
3988 if (regno < 0)
3989 return false;
3990 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3991 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3993 else
3995 if (regno < FIRST_PSEUDO_REGISTER)
3996 return false;
3997 if (! spilled_p)
3998 return true;
3999 return lra_get_regno_hard_regno (regno) < 0;
4002 fmt = GET_RTX_FORMAT (code);
4003 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4005 if (fmt[i] == 'e')
4007 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4008 return true;
4010 else if (fmt[i] == 'E')
4012 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4013 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4014 return true;
4017 return false;
4020 /* Return true if X contains a symbol reg. */
4021 static bool
4022 contains_symbol_ref_p (rtx x)
4024 int i, j;
4025 const char *fmt;
4026 enum rtx_code code;
4028 code = GET_CODE (x);
4029 if (code == SYMBOL_REF)
4030 return true;
4031 fmt = GET_RTX_FORMAT (code);
4032 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4034 if (fmt[i] == 'e')
4036 if (contains_symbol_ref_p (XEXP (x, i)))
4037 return true;
4039 else if (fmt[i] == 'E')
4041 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4042 if (contains_symbol_ref_p (XVECEXP (x, i, j)))
4043 return true;
4046 return false;
4049 /* Process all regs in location *LOC and change them on equivalent
4050 substitution. Return true if any change was done. */
4051 static bool
4052 loc_equivalence_change_p (rtx *loc)
4054 rtx subst, reg, x = *loc;
4055 bool result = false;
4056 enum rtx_code code = GET_CODE (x);
4057 const char *fmt;
4058 int i, j;
4060 if (code == SUBREG)
4062 reg = SUBREG_REG (x);
4063 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4064 && GET_MODE (subst) == VOIDmode)
4066 /* We cannot reload debug location. Simplify subreg here
4067 while we know the inner mode. */
4068 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4069 GET_MODE (reg), SUBREG_BYTE (x));
4070 return true;
4073 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4075 *loc = subst;
4076 return true;
4079 /* Scan all the operand sub-expressions. */
4080 fmt = GET_RTX_FORMAT (code);
4081 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4083 if (fmt[i] == 'e')
4084 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4085 else if (fmt[i] == 'E')
4086 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4087 result
4088 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4090 return result;
4093 /* Similar to loc_equivalence_change_p, but for use as
4094 simplify_replace_fn_rtx callback. DATA is insn for which the
4095 elimination is done. If it null we don't do the elimination. */
4096 static rtx
4097 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4099 if (!REG_P (loc))
4100 return NULL_RTX;
4102 rtx subst = (data == NULL
4103 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4104 if (subst != loc)
4105 return subst;
4107 return NULL_RTX;
4110 /* Maximum number of generated reload insns per an insn. It is for
4111 preventing this pass cycling in a bug case. */
4112 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4114 /* The current iteration number of this LRA pass. */
4115 int lra_constraint_iter;
4117 /* True if we substituted equiv which needs checking register
4118 allocation correctness because the equivalent value contains
4119 allocatable hard registers or when we restore multi-register
4120 pseudo. */
4121 bool lra_risky_transformations_p;
4123 /* Return true if REGNO is referenced in more than one block. */
4124 static bool
4125 multi_block_pseudo_p (int regno)
4127 basic_block bb = NULL;
4128 unsigned int uid;
4129 bitmap_iterator bi;
4131 if (regno < FIRST_PSEUDO_REGISTER)
4132 return false;
4134 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4135 if (bb == NULL)
4136 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4137 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4138 return true;
4139 return false;
4142 /* Return true if LIST contains a deleted insn. */
4143 static bool
4144 contains_deleted_insn_p (rtx_insn_list *list)
4146 for (; list != NULL_RTX; list = list->next ())
4147 if (NOTE_P (list->insn ())
4148 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4149 return true;
4150 return false;
4153 /* Return true if X contains a pseudo dying in INSN. */
4154 static bool
4155 dead_pseudo_p (rtx x, rtx insn)
4157 int i, j;
4158 const char *fmt;
4159 enum rtx_code code;
4161 if (REG_P (x))
4162 return (insn != NULL_RTX
4163 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4164 code = GET_CODE (x);
4165 fmt = GET_RTX_FORMAT (code);
4166 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4168 if (fmt[i] == 'e')
4170 if (dead_pseudo_p (XEXP (x, i), insn))
4171 return true;
4173 else if (fmt[i] == 'E')
4175 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4176 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4177 return true;
4180 return false;
4183 /* Return true if INSN contains a dying pseudo in INSN right hand
4184 side. */
4185 static bool
4186 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4188 rtx set = single_set (insn);
4190 gcc_assert (set != NULL);
4191 return dead_pseudo_p (SET_SRC (set), insn);
4194 /* Return true if any init insn of REGNO contains a dying pseudo in
4195 insn right hand side. */
4196 static bool
4197 init_insn_rhs_dead_pseudo_p (int regno)
4199 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4201 if (insns == NULL)
4202 return false;
4203 for (; insns != NULL_RTX; insns = insns->next ())
4204 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4205 return true;
4206 return false;
4209 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4210 reverse only if we have one init insn with given REGNO as a
4211 source. */
4212 static bool
4213 reverse_equiv_p (int regno)
4215 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4216 rtx set;
4218 if (insns == NULL)
4219 return false;
4220 if (! INSN_P (insns->insn ())
4221 || insns->next () != NULL)
4222 return false;
4223 if ((set = single_set (insns->insn ())) == NULL_RTX)
4224 return false;
4225 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4228 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4229 call this function only for non-reverse equivalence. */
4230 static bool
4231 contains_reloaded_insn_p (int regno)
4233 rtx set;
4234 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4236 for (; list != NULL; list = list->next ())
4237 if ((set = single_set (list->insn ())) == NULL_RTX
4238 || ! REG_P (SET_DEST (set))
4239 || (int) REGNO (SET_DEST (set)) != regno)
4240 return true;
4241 return false;
4244 /* Entry function of LRA constraint pass. Return true if the
4245 constraint pass did change the code. */
4246 bool
4247 lra_constraints (bool first_p)
4249 bool changed_p;
4250 int i, hard_regno, new_insns_num;
4251 unsigned int min_len, new_min_len, uid;
4252 rtx set, x, reg, dest_reg;
4253 basic_block last_bb;
4254 bitmap_head equiv_insn_bitmap;
4255 bitmap_iterator bi;
4257 lra_constraint_iter++;
4258 if (lra_dump_file != NULL)
4259 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4260 lra_constraint_iter);
4261 changed_p = false;
4262 if (pic_offset_table_rtx
4263 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4264 lra_risky_transformations_p = true;
4265 else
4266 lra_risky_transformations_p = false;
4267 new_insn_uid_start = get_max_uid ();
4268 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4269 /* Mark used hard regs for target stack size calulations. */
4270 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4271 if (lra_reg_info[i].nrefs != 0
4272 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4274 int j, nregs;
4276 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4277 for (j = 0; j < nregs; j++)
4278 df_set_regs_ever_live (hard_regno + j, true);
4280 /* Do elimination before the equivalence processing as we can spill
4281 some pseudos during elimination. */
4282 lra_eliminate (false, first_p);
4283 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4284 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4285 if (lra_reg_info[i].nrefs != 0)
4287 ira_reg_equiv[i].profitable_p = true;
4288 reg = regno_reg_rtx[i];
4289 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4291 bool pseudo_p = contains_reg_p (x, false, false);
4293 /* After RTL transformation, we can not guarantee that
4294 pseudo in the substitution was not reloaded which might
4295 make equivalence invalid. For example, in reverse
4296 equiv of p0
4298 p0 <- ...
4300 equiv_mem <- p0
4302 the memory address register was reloaded before the 2nd
4303 insn. */
4304 if ((! first_p && pseudo_p)
4305 /* We don't use DF for compilation speed sake. So it
4306 is problematic to update live info when we use an
4307 equivalence containing pseudos in more than one
4308 BB. */
4309 || (pseudo_p && multi_block_pseudo_p (i))
4310 /* If an init insn was deleted for some reason, cancel
4311 the equiv. We could update the equiv insns after
4312 transformations including an equiv insn deletion
4313 but it is not worthy as such cases are extremely
4314 rare. */
4315 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4316 /* If it is not a reverse equivalence, we check that a
4317 pseudo in rhs of the init insn is not dying in the
4318 insn. Otherwise, the live info at the beginning of
4319 the corresponding BB might be wrong after we
4320 removed the insn. When the equiv can be a
4321 constant, the right hand side of the init insn can
4322 be a pseudo. */
4323 || (! reverse_equiv_p (i)
4324 && (init_insn_rhs_dead_pseudo_p (i)
4325 /* If we reloaded the pseudo in an equivalence
4326 init insn, we can not remove the equiv init
4327 insns and the init insns might write into
4328 const memory in this case. */
4329 || contains_reloaded_insn_p (i)))
4330 /* Prevent access beyond equivalent memory for
4331 paradoxical subregs. */
4332 || (MEM_P (x)
4333 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4334 > GET_MODE_SIZE (GET_MODE (x))))
4335 || (pic_offset_table_rtx
4336 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4337 && (targetm.preferred_reload_class
4338 (x, lra_get_allocno_class (i)) == NO_REGS))
4339 || contains_symbol_ref_p (x))))
4340 ira_reg_equiv[i].defined_p = false;
4341 if (contains_reg_p (x, false, true))
4342 ira_reg_equiv[i].profitable_p = false;
4343 if (get_equiv (reg) != reg)
4344 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4347 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4348 update_equiv (i);
4349 /* We should add all insns containing pseudos which should be
4350 substituted by their equivalences. */
4351 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4352 lra_push_insn_by_uid (uid);
4353 min_len = lra_insn_stack_length ();
4354 new_insns_num = 0;
4355 last_bb = NULL;
4356 changed_p = false;
4357 while ((new_min_len = lra_insn_stack_length ()) != 0)
4359 curr_insn = lra_pop_insn ();
4360 --new_min_len;
4361 curr_bb = BLOCK_FOR_INSN (curr_insn);
4362 if (curr_bb != last_bb)
4364 last_bb = curr_bb;
4365 bb_reload_num = lra_curr_reload_num;
4367 if (min_len > new_min_len)
4369 min_len = new_min_len;
4370 new_insns_num = 0;
4372 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4373 internal_error
4374 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4375 MAX_RELOAD_INSNS_NUMBER);
4376 new_insns_num++;
4377 if (DEBUG_INSN_P (curr_insn))
4379 /* We need to check equivalence in debug insn and change
4380 pseudo to the equivalent value if necessary. */
4381 curr_id = lra_get_insn_recog_data (curr_insn);
4382 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4384 rtx old = *curr_id->operand_loc[0];
4385 *curr_id->operand_loc[0]
4386 = simplify_replace_fn_rtx (old, NULL_RTX,
4387 loc_equivalence_callback, curr_insn);
4388 if (old != *curr_id->operand_loc[0])
4390 lra_update_insn_regno_info (curr_insn);
4391 changed_p = true;
4395 else if (INSN_P (curr_insn))
4397 if ((set = single_set (curr_insn)) != NULL_RTX)
4399 dest_reg = SET_DEST (set);
4400 /* The equivalence pseudo could be set up as SUBREG in a
4401 case when it is a call restore insn in a mode
4402 different from the pseudo mode. */
4403 if (GET_CODE (dest_reg) == SUBREG)
4404 dest_reg = SUBREG_REG (dest_reg);
4405 if ((REG_P (dest_reg)
4406 && (x = get_equiv (dest_reg)) != dest_reg
4407 /* Remove insns which set up a pseudo whose value
4408 can not be changed. Such insns might be not in
4409 init_insns because we don't update equiv data
4410 during insn transformations.
4412 As an example, let suppose that a pseudo got
4413 hard register and on the 1st pass was not
4414 changed to equivalent constant. We generate an
4415 additional insn setting up the pseudo because of
4416 secondary memory movement. Then the pseudo is
4417 spilled and we use the equiv constant. In this
4418 case we should remove the additional insn and
4419 this insn is not init_insns list. */
4420 && (! MEM_P (x) || MEM_READONLY_P (x)
4421 /* Check that this is actually an insn setting
4422 up the equivalence. */
4423 || in_list_p (curr_insn,
4424 ira_reg_equiv
4425 [REGNO (dest_reg)].init_insns)))
4426 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4427 && in_list_p (curr_insn,
4428 ira_reg_equiv
4429 [REGNO (SET_SRC (set))].init_insns)))
4431 /* This is equiv init insn of pseudo which did not get a
4432 hard register -- remove the insn. */
4433 if (lra_dump_file != NULL)
4435 fprintf (lra_dump_file,
4436 " Removing equiv init insn %i (freq=%d)\n",
4437 INSN_UID (curr_insn),
4438 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4439 dump_insn_slim (lra_dump_file, curr_insn);
4441 if (contains_reg_p (x, true, false))
4442 lra_risky_transformations_p = true;
4443 lra_set_insn_deleted (curr_insn);
4444 continue;
4447 curr_id = lra_get_insn_recog_data (curr_insn);
4448 curr_static_id = curr_id->insn_static_data;
4449 init_curr_insn_input_reloads ();
4450 init_curr_operand_mode ();
4451 if (curr_insn_transform (false))
4452 changed_p = true;
4453 /* Check non-transformed insns too for equiv change as USE
4454 or CLOBBER don't need reloads but can contain pseudos
4455 being changed on their equivalences. */
4456 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4457 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4459 lra_update_insn_regno_info (curr_insn);
4460 changed_p = true;
4464 bitmap_clear (&equiv_insn_bitmap);
4465 /* If we used a new hard regno, changed_p should be true because the
4466 hard reg is assigned to a new pseudo. */
4467 #ifdef ENABLE_CHECKING
4468 if (! changed_p)
4470 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4471 if (lra_reg_info[i].nrefs != 0
4472 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4474 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4476 for (j = 0; j < nregs; j++)
4477 lra_assert (df_regs_ever_live_p (hard_regno + j));
4480 #endif
4481 return changed_p;
4484 /* Initiate the LRA constraint pass. It is done once per
4485 function. */
4486 void
4487 lra_constraints_init (void)
4491 /* Finalize the LRA constraint pass. It is done once per
4492 function. */
4493 void
4494 lra_constraints_finish (void)
4500 /* This page contains code to do inheritance/split
4501 transformations. */
4503 /* Number of reloads passed so far in current EBB. */
4504 static int reloads_num;
4506 /* Number of calls passed so far in current EBB. */
4507 static int calls_num;
4509 /* Current reload pseudo check for validity of elements in
4510 USAGE_INSNS. */
4511 static int curr_usage_insns_check;
4513 /* Info about last usage of registers in EBB to do inheritance/split
4514 transformation. Inheritance transformation is done from a spilled
4515 pseudo and split transformations from a hard register or a pseudo
4516 assigned to a hard register. */
4517 struct usage_insns
4519 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4520 value INSNS is valid. The insns is chain of optional debug insns
4521 and a finishing non-debug insn using the corresponding reg. The
4522 value is also used to mark the registers which are set up in the
4523 current insn. The negated insn uid is used for this. */
4524 int check;
4525 /* Value of global reloads_num at the last insn in INSNS. */
4526 int reloads_num;
4527 /* Value of global reloads_nums at the last insn in INSNS. */
4528 int calls_num;
4529 /* It can be true only for splitting. And it means that the restore
4530 insn should be put after insn given by the following member. */
4531 bool after_p;
4532 /* Next insns in the current EBB which use the original reg and the
4533 original reg value is not changed between the current insn and
4534 the next insns. In order words, e.g. for inheritance, if we need
4535 to use the original reg value again in the next insns we can try
4536 to use the value in a hard register from a reload insn of the
4537 current insn. */
4538 rtx insns;
4541 /* Map: regno -> corresponding pseudo usage insns. */
4542 static struct usage_insns *usage_insns;
4544 static void
4545 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4547 usage_insns[regno].check = curr_usage_insns_check;
4548 usage_insns[regno].insns = insn;
4549 usage_insns[regno].reloads_num = reloads_num;
4550 usage_insns[regno].calls_num = calls_num;
4551 usage_insns[regno].after_p = after_p;
4554 /* The function is used to form list REGNO usages which consists of
4555 optional debug insns finished by a non-debug insn using REGNO.
4556 RELOADS_NUM is current number of reload insns processed so far. */
4557 static void
4558 add_next_usage_insn (int regno, rtx insn, int reloads_num)
4560 rtx next_usage_insns;
4562 if (usage_insns[regno].check == curr_usage_insns_check
4563 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4564 && DEBUG_INSN_P (insn))
4566 /* Check that we did not add the debug insn yet. */
4567 if (next_usage_insns != insn
4568 && (GET_CODE (next_usage_insns) != INSN_LIST
4569 || XEXP (next_usage_insns, 0) != insn))
4570 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4571 next_usage_insns);
4573 else if (NONDEBUG_INSN_P (insn))
4574 setup_next_usage_insn (regno, insn, reloads_num, false);
4575 else
4576 usage_insns[regno].check = 0;
4579 /* Return first non-debug insn in list USAGE_INSNS. */
4580 static rtx_insn *
4581 skip_usage_debug_insns (rtx usage_insns)
4583 rtx insn;
4585 /* Skip debug insns. */
4586 for (insn = usage_insns;
4587 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4588 insn = XEXP (insn, 1))
4590 return safe_as_a <rtx_insn *> (insn);
4593 /* Return true if we need secondary memory moves for insn in
4594 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4595 into the insn. */
4596 static bool
4597 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4598 rtx usage_insns ATTRIBUTE_UNUSED)
4600 #ifndef SECONDARY_MEMORY_NEEDED
4601 return false;
4602 #else
4603 rtx_insn *insn;
4604 rtx set, dest;
4605 enum reg_class cl;
4607 if (inher_cl == ALL_REGS
4608 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4609 return false;
4610 lra_assert (INSN_P (insn));
4611 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4612 return false;
4613 dest = SET_DEST (set);
4614 if (! REG_P (dest))
4615 return false;
4616 lra_assert (inher_cl != NO_REGS);
4617 cl = get_reg_class (REGNO (dest));
4618 return (cl != NO_REGS && cl != ALL_REGS
4619 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4620 #endif
4623 /* Registers involved in inheritance/split in the current EBB
4624 (inheritance/split pseudos and original registers). */
4625 static bitmap_head check_only_regs;
4627 /* Do inheritance transformations for insn INSN, which defines (if
4628 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4629 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4630 form as the "insns" field of usage_insns. Return true if we
4631 succeed in such transformation.
4633 The transformations look like:
4635 p <- ... i <- ...
4636 ... p <- i (new insn)
4637 ... =>
4638 <- ... p ... <- ... i ...
4640 ... i <- p (new insn)
4641 <- ... p ... <- ... i ...
4642 ... =>
4643 <- ... p ... <- ... i ...
4644 where p is a spilled original pseudo and i is a new inheritance pseudo.
4647 The inheritance pseudo has the smallest class of two classes CL and
4648 class of ORIGINAL REGNO. */
4649 static bool
4650 inherit_reload_reg (bool def_p, int original_regno,
4651 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4653 if (optimize_function_for_size_p (cfun))
4654 return false;
4656 enum reg_class rclass = lra_get_allocno_class (original_regno);
4657 rtx original_reg = regno_reg_rtx[original_regno];
4658 rtx new_reg, usage_insn;
4659 rtx_insn *new_insns;
4661 lra_assert (! usage_insns[original_regno].after_p);
4662 if (lra_dump_file != NULL)
4663 fprintf (lra_dump_file,
4664 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4665 if (! ira_reg_classes_intersect_p[cl][rclass])
4667 if (lra_dump_file != NULL)
4669 fprintf (lra_dump_file,
4670 " Rejecting inheritance for %d "
4671 "because of disjoint classes %s and %s\n",
4672 original_regno, reg_class_names[cl],
4673 reg_class_names[rclass]);
4674 fprintf (lra_dump_file,
4675 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4677 return false;
4679 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4680 /* We don't use a subset of two classes because it can be
4681 NO_REGS. This transformation is still profitable in most
4682 cases even if the classes are not intersected as register
4683 move is probably cheaper than a memory load. */
4684 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4686 if (lra_dump_file != NULL)
4687 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4688 reg_class_names[cl], reg_class_names[rclass]);
4690 rclass = cl;
4692 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4694 /* Reject inheritance resulting in secondary memory moves.
4695 Otherwise, there is a danger in LRA cycling. Also such
4696 transformation will be unprofitable. */
4697 if (lra_dump_file != NULL)
4699 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4700 rtx set = single_set (insn);
4702 lra_assert (set != NULL_RTX);
4704 rtx dest = SET_DEST (set);
4706 lra_assert (REG_P (dest));
4707 fprintf (lra_dump_file,
4708 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4709 "as secondary mem is needed\n",
4710 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4711 original_regno, reg_class_names[rclass]);
4712 fprintf (lra_dump_file,
4713 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4715 return false;
4717 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4718 rclass, "inheritance");
4719 start_sequence ();
4720 if (def_p)
4721 lra_emit_move (original_reg, new_reg);
4722 else
4723 lra_emit_move (new_reg, original_reg);
4724 new_insns = get_insns ();
4725 end_sequence ();
4726 if (NEXT_INSN (new_insns) != NULL_RTX)
4728 if (lra_dump_file != NULL)
4730 fprintf (lra_dump_file,
4731 " Rejecting inheritance %d->%d "
4732 "as it results in 2 or more insns:\n",
4733 original_regno, REGNO (new_reg));
4734 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4735 fprintf (lra_dump_file,
4736 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4738 return false;
4740 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg);
4741 lra_update_insn_regno_info (insn);
4742 if (! def_p)
4743 /* We now have a new usage insn for original regno. */
4744 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4745 if (lra_dump_file != NULL)
4746 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4747 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4748 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4749 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4750 bitmap_set_bit (&check_only_regs, original_regno);
4751 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4752 if (def_p)
4753 lra_process_new_insns (insn, NULL, new_insns,
4754 "Add original<-inheritance");
4755 else
4756 lra_process_new_insns (insn, new_insns, NULL,
4757 "Add inheritance<-original");
4758 while (next_usage_insns != NULL_RTX)
4760 if (GET_CODE (next_usage_insns) != INSN_LIST)
4762 usage_insn = next_usage_insns;
4763 lra_assert (NONDEBUG_INSN_P (usage_insn));
4764 next_usage_insns = NULL;
4766 else
4768 usage_insn = XEXP (next_usage_insns, 0);
4769 lra_assert (DEBUG_INSN_P (usage_insn));
4770 next_usage_insns = XEXP (next_usage_insns, 1);
4772 lra_substitute_pseudo (&usage_insn, original_regno, new_reg);
4773 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4774 if (lra_dump_file != NULL)
4776 fprintf (lra_dump_file,
4777 " Inheritance reuse change %d->%d (bb%d):\n",
4778 original_regno, REGNO (new_reg),
4779 BLOCK_FOR_INSN (usage_insn)->index);
4780 dump_insn_slim (lra_dump_file, usage_insn);
4783 if (lra_dump_file != NULL)
4784 fprintf (lra_dump_file,
4785 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4786 return true;
4789 /* Return true if we need a caller save/restore for pseudo REGNO which
4790 was assigned to a hard register. */
4791 static inline bool
4792 need_for_call_save_p (int regno)
4794 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4795 return (usage_insns[regno].calls_num < calls_num
4796 && (overlaps_hard_reg_set_p
4797 ((flag_ipa_ra &&
4798 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4799 ? lra_reg_info[regno].actual_call_used_reg_set
4800 : call_used_reg_set,
4801 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4802 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4803 PSEUDO_REGNO_MODE (regno))));
4806 /* Global registers occurring in the current EBB. */
4807 static bitmap_head ebb_global_regs;
4809 /* Return true if we need a split for hard register REGNO or pseudo
4810 REGNO which was assigned to a hard register.
4811 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4812 used for reloads since the EBB end. It is an approximation of the
4813 used hard registers in the split range. The exact value would
4814 require expensive calculations. If we were aggressive with
4815 splitting because of the approximation, the split pseudo will save
4816 the same hard register assignment and will be removed in the undo
4817 pass. We still need the approximation because too aggressive
4818 splitting would result in too inaccurate cost calculation in the
4819 assignment pass because of too many generated moves which will be
4820 probably removed in the undo pass. */
4821 static inline bool
4822 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4824 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4826 lra_assert (hard_regno >= 0);
4827 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4828 /* Don't split eliminable hard registers, otherwise we can
4829 split hard registers like hard frame pointer, which
4830 lives on BB start/end according to DF-infrastructure,
4831 when there is a pseudo assigned to the register and
4832 living in the same BB. */
4833 && (regno >= FIRST_PSEUDO_REGISTER
4834 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4835 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4836 /* Don't split call clobbered hard regs living through
4837 calls, otherwise we might have a check problem in the
4838 assign sub-pass as in the most cases (exception is a
4839 situation when lra_risky_transformations_p value is
4840 true) the assign pass assumes that all pseudos living
4841 through calls are assigned to call saved hard regs. */
4842 && (regno >= FIRST_PSEUDO_REGISTER
4843 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4844 || usage_insns[regno].calls_num == calls_num)
4845 /* We need at least 2 reloads to make pseudo splitting
4846 profitable. We should provide hard regno splitting in
4847 any case to solve 1st insn scheduling problem when
4848 moving hard register definition up might result in
4849 impossibility to find hard register for reload pseudo of
4850 small register class. */
4851 && (usage_insns[regno].reloads_num
4852 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4853 && (regno < FIRST_PSEUDO_REGISTER
4854 /* For short living pseudos, spilling + inheritance can
4855 be considered a substitution for splitting.
4856 Therefore we do not splitting for local pseudos. It
4857 decreases also aggressiveness of splitting. The
4858 minimal number of references is chosen taking into
4859 account that for 2 references splitting has no sense
4860 as we can just spill the pseudo. */
4861 || (regno >= FIRST_PSEUDO_REGISTER
4862 && lra_reg_info[regno].nrefs > 3
4863 && bitmap_bit_p (&ebb_global_regs, regno))))
4864 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4867 /* Return class for the split pseudo created from original pseudo with
4868 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4869 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4870 results in no secondary memory movements. */
4871 static enum reg_class
4872 choose_split_class (enum reg_class allocno_class,
4873 int hard_regno ATTRIBUTE_UNUSED,
4874 machine_mode mode ATTRIBUTE_UNUSED)
4876 #ifndef SECONDARY_MEMORY_NEEDED
4877 return allocno_class;
4878 #else
4879 int i;
4880 enum reg_class cl, best_cl = NO_REGS;
4881 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4882 = REGNO_REG_CLASS (hard_regno);
4884 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4885 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4886 return allocno_class;
4887 for (i = 0;
4888 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4889 i++)
4890 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4891 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4892 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4893 && (best_cl == NO_REGS
4894 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4895 best_cl = cl;
4896 return best_cl;
4897 #endif
4900 /* Do split transformations for insn INSN, which defines or uses
4901 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4902 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4903 "insns" field of usage_insns.
4905 The transformations look like:
4907 p <- ... p <- ...
4908 ... s <- p (new insn -- save)
4909 ... =>
4910 ... p <- s (new insn -- restore)
4911 <- ... p ... <- ... p ...
4913 <- ... p ... <- ... p ...
4914 ... s <- p (new insn -- save)
4915 ... =>
4916 ... p <- s (new insn -- restore)
4917 <- ... p ... <- ... p ...
4919 where p is an original pseudo got a hard register or a hard
4920 register and s is a new split pseudo. The save is put before INSN
4921 if BEFORE_P is true. Return true if we succeed in such
4922 transformation. */
4923 static bool
4924 split_reg (bool before_p, int original_regno, rtx_insn *insn,
4925 rtx next_usage_insns)
4927 enum reg_class rclass;
4928 rtx original_reg;
4929 int hard_regno, nregs;
4930 rtx new_reg, usage_insn;
4931 rtx_insn *restore, *save;
4932 bool after_p;
4933 bool call_save_p;
4935 if (original_regno < FIRST_PSEUDO_REGISTER)
4937 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4938 hard_regno = original_regno;
4939 call_save_p = false;
4940 nregs = 1;
4942 else
4944 hard_regno = reg_renumber[original_regno];
4945 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4946 rclass = lra_get_allocno_class (original_regno);
4947 original_reg = regno_reg_rtx[original_regno];
4948 call_save_p = need_for_call_save_p (original_regno);
4950 original_reg = regno_reg_rtx[original_regno];
4951 lra_assert (hard_regno >= 0);
4952 if (lra_dump_file != NULL)
4953 fprintf (lra_dump_file,
4954 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4955 if (call_save_p)
4957 machine_mode mode = GET_MODE (original_reg);
4959 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4960 hard_regno_nregs[hard_regno][mode],
4961 mode);
4962 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4964 else
4966 rclass = choose_split_class (rclass, hard_regno,
4967 GET_MODE (original_reg));
4968 if (rclass == NO_REGS)
4970 if (lra_dump_file != NULL)
4972 fprintf (lra_dump_file,
4973 " Rejecting split of %d(%s): "
4974 "no good reg class for %d(%s)\n",
4975 original_regno,
4976 reg_class_names[lra_get_allocno_class (original_regno)],
4977 hard_regno,
4978 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4979 fprintf
4980 (lra_dump_file,
4981 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4983 return false;
4985 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4986 rclass, "split");
4987 reg_renumber[REGNO (new_reg)] = hard_regno;
4989 save = emit_spill_move (true, new_reg, original_reg);
4990 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
4992 if (lra_dump_file != NULL)
4994 fprintf
4995 (lra_dump_file,
4996 " Rejecting split %d->%d resulting in > 2 save insns:\n",
4997 original_regno, REGNO (new_reg));
4998 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
4999 fprintf (lra_dump_file,
5000 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5002 return false;
5004 restore = emit_spill_move (false, new_reg, original_reg);
5005 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
5007 if (lra_dump_file != NULL)
5009 fprintf (lra_dump_file,
5010 " Rejecting split %d->%d "
5011 "resulting in > 2 restore insns:\n",
5012 original_regno, REGNO (new_reg));
5013 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
5014 fprintf (lra_dump_file,
5015 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5017 return false;
5019 after_p = usage_insns[original_regno].after_p;
5020 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
5021 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5022 bitmap_set_bit (&check_only_regs, original_regno);
5023 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
5024 for (;;)
5026 if (GET_CODE (next_usage_insns) != INSN_LIST)
5028 usage_insn = next_usage_insns;
5029 break;
5031 usage_insn = XEXP (next_usage_insns, 0);
5032 lra_assert (DEBUG_INSN_P (usage_insn));
5033 next_usage_insns = XEXP (next_usage_insns, 1);
5034 lra_substitute_pseudo (&usage_insn, original_regno, new_reg);
5035 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5036 if (lra_dump_file != NULL)
5038 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5039 original_regno, REGNO (new_reg));
5040 dump_insn_slim (lra_dump_file, usage_insn);
5043 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5044 lra_assert (usage_insn != insn || (after_p && before_p));
5045 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5046 after_p ? NULL : restore,
5047 after_p ? restore : NULL,
5048 call_save_p
5049 ? "Add reg<-save" : "Add reg<-split");
5050 lra_process_new_insns (insn, before_p ? save : NULL,
5051 before_p ? NULL : save,
5052 call_save_p
5053 ? "Add save<-reg" : "Add split<-reg");
5054 if (nregs > 1)
5055 /* If we are trying to split multi-register. We should check
5056 conflicts on the next assignment sub-pass. IRA can allocate on
5057 sub-register levels, LRA do this on pseudos level right now and
5058 this discrepancy may create allocation conflicts after
5059 splitting. */
5060 lra_risky_transformations_p = true;
5061 if (lra_dump_file != NULL)
5062 fprintf (lra_dump_file,
5063 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5064 return true;
5067 /* Recognize that we need a split transformation for insn INSN, which
5068 defines or uses REGNO in its insn biggest MODE (we use it only if
5069 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5070 hard registers which might be used for reloads since the EBB end.
5071 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5072 uid before starting INSN processing. Return true if we succeed in
5073 such transformation. */
5074 static bool
5075 split_if_necessary (int regno, machine_mode mode,
5076 HARD_REG_SET potential_reload_hard_regs,
5077 bool before_p, rtx_insn *insn, int max_uid)
5079 bool res = false;
5080 int i, nregs = 1;
5081 rtx next_usage_insns;
5083 if (regno < FIRST_PSEUDO_REGISTER)
5084 nregs = hard_regno_nregs[regno][mode];
5085 for (i = 0; i < nregs; i++)
5086 if (usage_insns[regno + i].check == curr_usage_insns_check
5087 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5088 /* To avoid processing the register twice or more. */
5089 && ((GET_CODE (next_usage_insns) != INSN_LIST
5090 && INSN_UID (next_usage_insns) < max_uid)
5091 || (GET_CODE (next_usage_insns) == INSN_LIST
5092 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5093 && need_for_split_p (potential_reload_hard_regs, regno + i)
5094 && split_reg (before_p, regno + i, insn, next_usage_insns))
5095 res = true;
5096 return res;
5099 /* Check only registers living at the current program point in the
5100 current EBB. */
5101 static bitmap_head live_regs;
5103 /* Update live info in EBB given by its HEAD and TAIL insns after
5104 inheritance/split transformation. The function removes dead moves
5105 too. */
5106 static void
5107 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5109 unsigned int j;
5110 int i, regno;
5111 bool live_p;
5112 rtx_insn *prev_insn;
5113 rtx set;
5114 bool remove_p;
5115 basic_block last_bb, prev_bb, curr_bb;
5116 bitmap_iterator bi;
5117 struct lra_insn_reg *reg;
5118 edge e;
5119 edge_iterator ei;
5121 last_bb = BLOCK_FOR_INSN (tail);
5122 prev_bb = NULL;
5123 for (curr_insn = tail;
5124 curr_insn != PREV_INSN (head);
5125 curr_insn = prev_insn)
5127 prev_insn = PREV_INSN (curr_insn);
5128 /* We need to process empty blocks too. They contain
5129 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5130 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5131 continue;
5132 curr_bb = BLOCK_FOR_INSN (curr_insn);
5133 if (curr_bb != prev_bb)
5135 if (prev_bb != NULL)
5137 /* Update df_get_live_in (prev_bb): */
5138 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5139 if (bitmap_bit_p (&live_regs, j))
5140 bitmap_set_bit (df_get_live_in (prev_bb), j);
5141 else
5142 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5144 if (curr_bb != last_bb)
5146 /* Update df_get_live_out (curr_bb): */
5147 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5149 live_p = bitmap_bit_p (&live_regs, j);
5150 if (! live_p)
5151 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5152 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5154 live_p = true;
5155 break;
5157 if (live_p)
5158 bitmap_set_bit (df_get_live_out (curr_bb), j);
5159 else
5160 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5163 prev_bb = curr_bb;
5164 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5166 if (! NONDEBUG_INSN_P (curr_insn))
5167 continue;
5168 curr_id = lra_get_insn_recog_data (curr_insn);
5169 curr_static_id = curr_id->insn_static_data;
5170 remove_p = false;
5171 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
5172 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5173 && bitmap_bit_p (&check_only_regs, regno)
5174 && ! bitmap_bit_p (&live_regs, regno))
5175 remove_p = true;
5176 /* See which defined values die here. */
5177 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5178 if (reg->type == OP_OUT && ! reg->subreg_p)
5179 bitmap_clear_bit (&live_regs, reg->regno);
5180 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5181 if (reg->type == OP_OUT && ! reg->subreg_p)
5182 bitmap_clear_bit (&live_regs, reg->regno);
5183 /* Mark each used value as live. */
5184 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5185 if (reg->type != OP_OUT
5186 && bitmap_bit_p (&check_only_regs, reg->regno))
5187 bitmap_set_bit (&live_regs, reg->regno);
5188 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5189 if (reg->type != OP_OUT
5190 && bitmap_bit_p (&check_only_regs, reg->regno))
5191 bitmap_set_bit (&live_regs, reg->regno);
5192 if (curr_id->arg_hard_regs != NULL)
5193 /* Make argument hard registers live. */
5194 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5195 if (bitmap_bit_p (&check_only_regs, regno))
5196 bitmap_set_bit (&live_regs, regno);
5197 /* It is quite important to remove dead move insns because it
5198 means removing dead store. We don't need to process them for
5199 constraints. */
5200 if (remove_p)
5202 if (lra_dump_file != NULL)
5204 fprintf (lra_dump_file, " Removing dead insn:\n ");
5205 dump_insn_slim (lra_dump_file, curr_insn);
5207 lra_set_insn_deleted (curr_insn);
5212 /* The structure describes info to do an inheritance for the current
5213 insn. We need to collect such info first before doing the
5214 transformations because the transformations change the insn
5215 internal representation. */
5216 struct to_inherit
5218 /* Original regno. */
5219 int regno;
5220 /* Subsequent insns which can inherit original reg value. */
5221 rtx insns;
5224 /* Array containing all info for doing inheritance from the current
5225 insn. */
5226 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5228 /* Number elements in the previous array. */
5229 static int to_inherit_num;
5231 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5232 structure to_inherit. */
5233 static void
5234 add_to_inherit (int regno, rtx insns)
5236 int i;
5238 for (i = 0; i < to_inherit_num; i++)
5239 if (to_inherit[i].regno == regno)
5240 return;
5241 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5242 to_inherit[to_inherit_num].regno = regno;
5243 to_inherit[to_inherit_num++].insns = insns;
5246 /* Return the last non-debug insn in basic block BB, or the block begin
5247 note if none. */
5248 static rtx_insn *
5249 get_last_insertion_point (basic_block bb)
5251 rtx_insn *insn;
5253 FOR_BB_INSNS_REVERSE (bb, insn)
5254 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5255 return insn;
5256 gcc_unreachable ();
5259 /* Set up RES by registers living on edges FROM except the edge (FROM,
5260 TO) or by registers set up in a jump insn in BB FROM. */
5261 static void
5262 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5264 rtx_insn *last;
5265 struct lra_insn_reg *reg;
5266 edge e;
5267 edge_iterator ei;
5269 lra_assert (to != NULL);
5270 bitmap_clear (res);
5271 FOR_EACH_EDGE (e, ei, from->succs)
5272 if (e->dest != to)
5273 bitmap_ior_into (res, df_get_live_in (e->dest));
5274 last = get_last_insertion_point (from);
5275 if (! JUMP_P (last))
5276 return;
5277 curr_id = lra_get_insn_recog_data (last);
5278 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5279 if (reg->type != OP_IN)
5280 bitmap_set_bit (res, reg->regno);
5283 /* Used as a temporary results of some bitmap calculations. */
5284 static bitmap_head temp_bitmap;
5286 /* We split for reloads of small class of hard regs. The following
5287 defines how many hard regs the class should have to be qualified as
5288 small. The code is mostly oriented to x86/x86-64 architecture
5289 where some insns need to use only specific register or pair of
5290 registers and these register can live in RTL explicitly, e.g. for
5291 parameter passing. */
5292 static const int max_small_class_regs_num = 2;
5294 /* Do inheritance/split transformations in EBB starting with HEAD and
5295 finishing on TAIL. We process EBB insns in the reverse order.
5296 Return true if we did any inheritance/split transformation in the
5297 EBB.
5299 We should avoid excessive splitting which results in worse code
5300 because of inaccurate cost calculations for spilling new split
5301 pseudos in such case. To achieve this we do splitting only if
5302 register pressure is high in given basic block and there are reload
5303 pseudos requiring hard registers. We could do more register
5304 pressure calculations at any given program point to avoid necessary
5305 splitting even more but it is to expensive and the current approach
5306 works well enough. */
5307 static bool
5308 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5310 int i, src_regno, dst_regno, nregs;
5311 bool change_p, succ_p, update_reloads_num_p;
5312 rtx_insn *prev_insn, *last_insn;
5313 rtx next_usage_insns, set;
5314 enum reg_class cl;
5315 struct lra_insn_reg *reg;
5316 basic_block last_processed_bb, curr_bb = NULL;
5317 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5318 bitmap to_process;
5319 unsigned int j;
5320 bitmap_iterator bi;
5321 bool head_p, after_p;
5323 change_p = false;
5324 curr_usage_insns_check++;
5325 reloads_num = calls_num = 0;
5326 bitmap_clear (&check_only_regs);
5327 last_processed_bb = NULL;
5328 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5329 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5330 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5331 /* We don't process new insns generated in the loop. */
5332 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5334 prev_insn = PREV_INSN (curr_insn);
5335 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5336 curr_bb = BLOCK_FOR_INSN (curr_insn);
5337 if (last_processed_bb != curr_bb)
5339 /* We are at the end of BB. Add qualified living
5340 pseudos for potential splitting. */
5341 to_process = df_get_live_out (curr_bb);
5342 if (last_processed_bb != NULL)
5344 /* We are somewhere in the middle of EBB. */
5345 get_live_on_other_edges (curr_bb, last_processed_bb,
5346 &temp_bitmap);
5347 to_process = &temp_bitmap;
5349 last_processed_bb = curr_bb;
5350 last_insn = get_last_insertion_point (curr_bb);
5351 after_p = (! JUMP_P (last_insn)
5352 && (! CALL_P (last_insn)
5353 || (find_reg_note (last_insn,
5354 REG_NORETURN, NULL_RTX) == NULL_RTX
5355 && ! SIBLING_CALL_P (last_insn))));
5356 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5357 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5359 if ((int) j >= lra_constraint_new_regno_start)
5360 break;
5361 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5363 if (j < FIRST_PSEUDO_REGISTER)
5364 SET_HARD_REG_BIT (live_hard_regs, j);
5365 else
5366 add_to_hard_reg_set (&live_hard_regs,
5367 PSEUDO_REGNO_MODE (j),
5368 reg_renumber[j]);
5369 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5373 src_regno = dst_regno = -1;
5374 if (NONDEBUG_INSN_P (curr_insn)
5375 && (set = single_set (curr_insn)) != NULL_RTX
5376 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5378 src_regno = REGNO (SET_SRC (set));
5379 dst_regno = REGNO (SET_DEST (set));
5381 update_reloads_num_p = true;
5382 if (src_regno < lra_constraint_new_regno_start
5383 && src_regno >= FIRST_PSEUDO_REGISTER
5384 && reg_renumber[src_regno] < 0
5385 && dst_regno >= lra_constraint_new_regno_start
5386 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5388 /* 'reload_pseudo <- original_pseudo'. */
5389 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5390 reloads_num++;
5391 update_reloads_num_p = false;
5392 succ_p = false;
5393 if (usage_insns[src_regno].check == curr_usage_insns_check
5394 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5395 succ_p = inherit_reload_reg (false, src_regno, cl,
5396 curr_insn, next_usage_insns);
5397 if (succ_p)
5398 change_p = true;
5399 else
5400 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5401 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5402 IOR_HARD_REG_SET (potential_reload_hard_regs,
5403 reg_class_contents[cl]);
5405 else if (src_regno >= lra_constraint_new_regno_start
5406 && dst_regno < lra_constraint_new_regno_start
5407 && dst_regno >= FIRST_PSEUDO_REGISTER
5408 && reg_renumber[dst_regno] < 0
5409 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5410 && usage_insns[dst_regno].check == curr_usage_insns_check
5411 && (next_usage_insns
5412 = usage_insns[dst_regno].insns) != NULL_RTX)
5414 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5415 reloads_num++;
5416 update_reloads_num_p = false;
5417 /* 'original_pseudo <- reload_pseudo'. */
5418 if (! JUMP_P (curr_insn)
5419 && inherit_reload_reg (true, dst_regno, cl,
5420 curr_insn, next_usage_insns))
5421 change_p = true;
5422 /* Invalidate. */
5423 usage_insns[dst_regno].check = 0;
5424 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5425 IOR_HARD_REG_SET (potential_reload_hard_regs,
5426 reg_class_contents[cl]);
5428 else if (INSN_P (curr_insn))
5430 int iter;
5431 int max_uid = get_max_uid ();
5433 curr_id = lra_get_insn_recog_data (curr_insn);
5434 curr_static_id = curr_id->insn_static_data;
5435 to_inherit_num = 0;
5436 /* Process insn definitions. */
5437 for (iter = 0; iter < 2; iter++)
5438 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5439 reg != NULL;
5440 reg = reg->next)
5441 if (reg->type != OP_IN
5442 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5444 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5445 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5446 && usage_insns[dst_regno].check == curr_usage_insns_check
5447 && (next_usage_insns
5448 = usage_insns[dst_regno].insns) != NULL_RTX)
5450 struct lra_insn_reg *r;
5452 for (r = curr_id->regs; r != NULL; r = r->next)
5453 if (r->type != OP_OUT && r->regno == dst_regno)
5454 break;
5455 /* Don't do inheritance if the pseudo is also
5456 used in the insn. */
5457 if (r == NULL)
5458 /* We can not do inheritance right now
5459 because the current insn reg info (chain
5460 regs) can change after that. */
5461 add_to_inherit (dst_regno, next_usage_insns);
5463 /* We can not process one reg twice here because of
5464 usage_insns invalidation. */
5465 if ((dst_regno < FIRST_PSEUDO_REGISTER
5466 || reg_renumber[dst_regno] >= 0)
5467 && ! reg->subreg_p && reg->type != OP_IN)
5469 HARD_REG_SET s;
5471 if (split_if_necessary (dst_regno, reg->biggest_mode,
5472 potential_reload_hard_regs,
5473 false, curr_insn, max_uid))
5474 change_p = true;
5475 CLEAR_HARD_REG_SET (s);
5476 if (dst_regno < FIRST_PSEUDO_REGISTER)
5477 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5478 else
5479 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5480 reg_renumber[dst_regno]);
5481 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5483 /* We should invalidate potential inheritance or
5484 splitting for the current insn usages to the next
5485 usage insns (see code below) as the output pseudo
5486 prevents this. */
5487 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5488 && reg_renumber[dst_regno] < 0)
5489 || (reg->type == OP_OUT && ! reg->subreg_p
5490 && (dst_regno < FIRST_PSEUDO_REGISTER
5491 || reg_renumber[dst_regno] >= 0)))
5493 /* Invalidate and mark definitions. */
5494 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5495 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5496 else
5498 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5499 for (i = 0; i < nregs; i++)
5500 usage_insns[dst_regno + i].check
5501 = -(int) INSN_UID (curr_insn);
5505 if (! JUMP_P (curr_insn))
5506 for (i = 0; i < to_inherit_num; i++)
5507 if (inherit_reload_reg (true, to_inherit[i].regno,
5508 ALL_REGS, curr_insn,
5509 to_inherit[i].insns))
5510 change_p = true;
5511 if (CALL_P (curr_insn))
5513 rtx cheap, pat, dest;
5514 rtx_insn *restore;
5515 int regno, hard_regno;
5517 calls_num++;
5518 if ((cheap = find_reg_note (curr_insn,
5519 REG_RETURNED, NULL_RTX)) != NULL_RTX
5520 && ((cheap = XEXP (cheap, 0)), true)
5521 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5522 && (hard_regno = reg_renumber[regno]) >= 0
5523 /* If there are pending saves/restores, the
5524 optimization is not worth. */
5525 && usage_insns[regno].calls_num == calls_num - 1
5526 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5528 /* Restore the pseudo from the call result as
5529 REG_RETURNED note says that the pseudo value is
5530 in the call result and the pseudo is an argument
5531 of the call. */
5532 pat = PATTERN (curr_insn);
5533 if (GET_CODE (pat) == PARALLEL)
5534 pat = XVECEXP (pat, 0, 0);
5535 dest = SET_DEST (pat);
5536 /* For multiple return values dest is PARALLEL.
5537 Currently we handle only single return value case. */
5538 if (REG_P (dest))
5540 start_sequence ();
5541 emit_move_insn (cheap, copy_rtx (dest));
5542 restore = get_insns ();
5543 end_sequence ();
5544 lra_process_new_insns (curr_insn, NULL, restore,
5545 "Inserting call parameter restore");
5546 /* We don't need to save/restore of the pseudo from
5547 this call. */
5548 usage_insns[regno].calls_num = calls_num;
5549 bitmap_set_bit (&check_only_regs, regno);
5553 to_inherit_num = 0;
5554 /* Process insn usages. */
5555 for (iter = 0; iter < 2; iter++)
5556 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5557 reg != NULL;
5558 reg = reg->next)
5559 if ((reg->type != OP_OUT
5560 || (reg->type == OP_OUT && reg->subreg_p))
5561 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5563 if (src_regno >= FIRST_PSEUDO_REGISTER
5564 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5566 if (usage_insns[src_regno].check == curr_usage_insns_check
5567 && (next_usage_insns
5568 = usage_insns[src_regno].insns) != NULL_RTX
5569 && NONDEBUG_INSN_P (curr_insn))
5570 add_to_inherit (src_regno, next_usage_insns);
5571 else if (usage_insns[src_regno].check
5572 != -(int) INSN_UID (curr_insn))
5573 /* Add usages but only if the reg is not set up
5574 in the same insn. */
5575 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5577 else if (src_regno < FIRST_PSEUDO_REGISTER
5578 || reg_renumber[src_regno] >= 0)
5580 bool before_p;
5581 rtx use_insn = curr_insn;
5583 before_p = (JUMP_P (curr_insn)
5584 || (CALL_P (curr_insn) && reg->type == OP_IN));
5585 if (NONDEBUG_INSN_P (curr_insn)
5586 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5587 && split_if_necessary (src_regno, reg->biggest_mode,
5588 potential_reload_hard_regs,
5589 before_p, curr_insn, max_uid))
5591 if (reg->subreg_p)
5592 lra_risky_transformations_p = true;
5593 change_p = true;
5594 /* Invalidate. */
5595 usage_insns[src_regno].check = 0;
5596 if (before_p)
5597 use_insn = PREV_INSN (curr_insn);
5599 if (NONDEBUG_INSN_P (curr_insn))
5601 if (src_regno < FIRST_PSEUDO_REGISTER)
5602 add_to_hard_reg_set (&live_hard_regs,
5603 reg->biggest_mode, src_regno);
5604 else
5605 add_to_hard_reg_set (&live_hard_regs,
5606 PSEUDO_REGNO_MODE (src_regno),
5607 reg_renumber[src_regno]);
5609 add_next_usage_insn (src_regno, use_insn, reloads_num);
5612 /* Process call args. */
5613 if (curr_id->arg_hard_regs != NULL)
5614 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5615 if (src_regno < FIRST_PSEUDO_REGISTER)
5617 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5618 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5620 for (i = 0; i < to_inherit_num; i++)
5622 src_regno = to_inherit[i].regno;
5623 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5624 curr_insn, to_inherit[i].insns))
5625 change_p = true;
5626 else
5627 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5630 if (update_reloads_num_p
5631 && NONDEBUG_INSN_P (curr_insn)
5632 && (set = single_set (curr_insn)) != NULL_RTX)
5634 int regno = -1;
5635 if ((REG_P (SET_DEST (set))
5636 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5637 && reg_renumber[regno] < 0
5638 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5639 || (REG_P (SET_SRC (set))
5640 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5641 && reg_renumber[regno] < 0
5642 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5644 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5645 reloads_num++;
5646 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5647 IOR_HARD_REG_SET (potential_reload_hard_regs,
5648 reg_class_contents[cl]);
5651 /* We reached the start of the current basic block. */
5652 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5653 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5655 /* We reached the beginning of the current block -- do
5656 rest of spliting in the current BB. */
5657 to_process = df_get_live_in (curr_bb);
5658 if (BLOCK_FOR_INSN (head) != curr_bb)
5660 /* We are somewhere in the middle of EBB. */
5661 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5662 curr_bb, &temp_bitmap);
5663 to_process = &temp_bitmap;
5665 head_p = true;
5666 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5668 if ((int) j >= lra_constraint_new_regno_start)
5669 break;
5670 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5671 && usage_insns[j].check == curr_usage_insns_check
5672 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5674 if (need_for_split_p (potential_reload_hard_regs, j))
5676 if (lra_dump_file != NULL && head_p)
5678 fprintf (lra_dump_file,
5679 " ----------------------------------\n");
5680 head_p = false;
5682 if (split_reg (false, j, bb_note (curr_bb),
5683 next_usage_insns))
5684 change_p = true;
5686 usage_insns[j].check = 0;
5691 return change_p;
5694 /* This value affects EBB forming. If probability of edge from EBB to
5695 a BB is not greater than the following value, we don't add the BB
5696 to EBB. */
5697 #define EBB_PROBABILITY_CUTOFF ((REG_BR_PROB_BASE * 50) / 100)
5699 /* Current number of inheritance/split iteration. */
5700 int lra_inheritance_iter;
5702 /* Entry function for inheritance/split pass. */
5703 void
5704 lra_inheritance (void)
5706 int i;
5707 basic_block bb, start_bb;
5708 edge e;
5710 lra_inheritance_iter++;
5711 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5712 return;
5713 timevar_push (TV_LRA_INHERITANCE);
5714 if (lra_dump_file != NULL)
5715 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5716 lra_inheritance_iter);
5717 curr_usage_insns_check = 0;
5718 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5719 for (i = 0; i < lra_constraint_new_regno_start; i++)
5720 usage_insns[i].check = 0;
5721 bitmap_initialize (&check_only_regs, &reg_obstack);
5722 bitmap_initialize (&live_regs, &reg_obstack);
5723 bitmap_initialize (&temp_bitmap, &reg_obstack);
5724 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5725 FOR_EACH_BB_FN (bb, cfun)
5727 start_bb = bb;
5728 if (lra_dump_file != NULL)
5729 fprintf (lra_dump_file, "EBB");
5730 /* Form a EBB starting with BB. */
5731 bitmap_clear (&ebb_global_regs);
5732 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5733 for (;;)
5735 if (lra_dump_file != NULL)
5736 fprintf (lra_dump_file, " %d", bb->index);
5737 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5738 || LABEL_P (BB_HEAD (bb->next_bb)))
5739 break;
5740 e = find_fallthru_edge (bb->succs);
5741 if (! e)
5742 break;
5743 if (e->probability <= EBB_PROBABILITY_CUTOFF)
5744 break;
5745 bb = bb->next_bb;
5747 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5748 if (lra_dump_file != NULL)
5749 fprintf (lra_dump_file, "\n");
5750 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5751 /* Remember that the EBB head and tail can change in
5752 inherit_in_ebb. */
5753 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5755 bitmap_clear (&ebb_global_regs);
5756 bitmap_clear (&temp_bitmap);
5757 bitmap_clear (&live_regs);
5758 bitmap_clear (&check_only_regs);
5759 free (usage_insns);
5761 timevar_pop (TV_LRA_INHERITANCE);
5766 /* This page contains code to undo failed inheritance/split
5767 transformations. */
5769 /* Current number of iteration undoing inheritance/split. */
5770 int lra_undo_inheritance_iter;
5772 /* Fix BB live info LIVE after removing pseudos created on pass doing
5773 inheritance/split which are REMOVED_PSEUDOS. */
5774 static void
5775 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5777 unsigned int regno;
5778 bitmap_iterator bi;
5780 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5781 if (bitmap_clear_bit (live, regno))
5782 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5785 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5786 number. */
5787 static int
5788 get_regno (rtx reg)
5790 if (GET_CODE (reg) == SUBREG)
5791 reg = SUBREG_REG (reg);
5792 if (REG_P (reg))
5793 return REGNO (reg);
5794 return -1;
5797 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5798 return true if we did any change. The undo transformations for
5799 inheritance looks like
5800 i <- i2
5801 p <- i => p <- i2
5802 or removing
5803 p <- i, i <- p, and i <- i3
5804 where p is original pseudo from which inheritance pseudo i was
5805 created, i and i3 are removed inheritance pseudos, i2 is another
5806 not removed inheritance pseudo. All split pseudos or other
5807 occurrences of removed inheritance pseudos are changed on the
5808 corresponding original pseudos.
5810 The function also schedules insns changed and created during
5811 inheritance/split pass for processing by the subsequent constraint
5812 pass. */
5813 static bool
5814 remove_inheritance_pseudos (bitmap remove_pseudos)
5816 basic_block bb;
5817 int regno, sregno, prev_sregno, dregno, restore_regno;
5818 rtx set, prev_set;
5819 rtx_insn *prev_insn;
5820 bool change_p, done_p;
5822 change_p = ! bitmap_empty_p (remove_pseudos);
5823 /* We can not finish the function right away if CHANGE_P is true
5824 because we need to marks insns affected by previous
5825 inheritance/split pass for processing by the subsequent
5826 constraint pass. */
5827 FOR_EACH_BB_FN (bb, cfun)
5829 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5830 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5831 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5833 if (! INSN_P (curr_insn))
5834 continue;
5835 done_p = false;
5836 sregno = dregno = -1;
5837 if (change_p && NONDEBUG_INSN_P (curr_insn)
5838 && (set = single_set (curr_insn)) != NULL_RTX)
5840 dregno = get_regno (SET_DEST (set));
5841 sregno = get_regno (SET_SRC (set));
5844 if (sregno >= 0 && dregno >= 0)
5846 if ((bitmap_bit_p (remove_pseudos, sregno)
5847 && (lra_reg_info[sregno].restore_regno == dregno
5848 || (bitmap_bit_p (remove_pseudos, dregno)
5849 && (lra_reg_info[sregno].restore_regno
5850 == lra_reg_info[dregno].restore_regno))))
5851 || (bitmap_bit_p (remove_pseudos, dregno)
5852 && lra_reg_info[dregno].restore_regno == sregno))
5853 /* One of the following cases:
5854 original <- removed inheritance pseudo
5855 removed inherit pseudo <- another removed inherit pseudo
5856 removed inherit pseudo <- original pseudo
5858 removed_split_pseudo <- original_reg
5859 original_reg <- removed_split_pseudo */
5861 if (lra_dump_file != NULL)
5863 fprintf (lra_dump_file, " Removing %s:\n",
5864 bitmap_bit_p (&lra_split_regs, sregno)
5865 || bitmap_bit_p (&lra_split_regs, dregno)
5866 ? "split" : "inheritance");
5867 dump_insn_slim (lra_dump_file, curr_insn);
5869 lra_set_insn_deleted (curr_insn);
5870 done_p = true;
5872 else if (bitmap_bit_p (remove_pseudos, sregno)
5873 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5875 /* Search the following pattern:
5876 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5877 original_pseudo <- inherit_or_split_pseudo1
5878 where the 2nd insn is the current insn and
5879 inherit_or_split_pseudo2 is not removed. If it is found,
5880 change the current insn onto:
5881 original_pseudo <- inherit_or_split_pseudo2. */
5882 for (prev_insn = PREV_INSN (curr_insn);
5883 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5884 prev_insn = PREV_INSN (prev_insn))
5886 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5887 && (prev_set = single_set (prev_insn)) != NULL_RTX
5888 /* There should be no subregs in insn we are
5889 searching because only the original reg might
5890 be in subreg when we changed the mode of
5891 load/store for splitting. */
5892 && REG_P (SET_DEST (prev_set))
5893 && REG_P (SET_SRC (prev_set))
5894 && (int) REGNO (SET_DEST (prev_set)) == sregno
5895 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5896 >= FIRST_PSEUDO_REGISTER)
5897 /* As we consider chain of inheritance or
5898 splitting described in above comment we should
5899 check that sregno and prev_sregno were
5900 inheritance/split pseudos created from the
5901 same original regno. */
5902 && (lra_reg_info[sregno].restore_regno
5903 == lra_reg_info[prev_sregno].restore_regno)
5904 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5906 lra_assert (GET_MODE (SET_SRC (prev_set))
5907 == GET_MODE (regno_reg_rtx[sregno]));
5908 if (GET_CODE (SET_SRC (set)) == SUBREG)
5909 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5910 else
5911 SET_SRC (set) = SET_SRC (prev_set);
5912 /* As we are finishing with processing the insn
5913 here, check the destination too as it might
5914 inheritance pseudo for another pseudo. */
5915 if (bitmap_bit_p (remove_pseudos, dregno)
5916 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
5917 && (restore_regno
5918 = lra_reg_info[dregno].restore_regno) >= 0)
5920 if (GET_CODE (SET_DEST (set)) == SUBREG)
5921 SUBREG_REG (SET_DEST (set))
5922 = regno_reg_rtx[restore_regno];
5923 else
5924 SET_DEST (set) = regno_reg_rtx[restore_regno];
5926 lra_push_insn_and_update_insn_regno_info (curr_insn);
5927 lra_set_used_insn_alternative_by_uid
5928 (INSN_UID (curr_insn), -1);
5929 done_p = true;
5930 if (lra_dump_file != NULL)
5932 fprintf (lra_dump_file, " Change reload insn:\n");
5933 dump_insn_slim (lra_dump_file, curr_insn);
5938 if (! done_p)
5940 struct lra_insn_reg *reg;
5941 bool restored_regs_p = false;
5942 bool kept_regs_p = false;
5944 curr_id = lra_get_insn_recog_data (curr_insn);
5945 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5947 regno = reg->regno;
5948 restore_regno = lra_reg_info[regno].restore_regno;
5949 if (restore_regno >= 0)
5951 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5953 lra_substitute_pseudo_within_insn (
5954 curr_insn, regno, regno_reg_rtx[restore_regno]);
5955 restored_regs_p = true;
5957 else
5958 kept_regs_p = true;
5961 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5963 /* The instruction has changed since the previous
5964 constraints pass. */
5965 lra_push_insn_and_update_insn_regno_info (curr_insn);
5966 lra_set_used_insn_alternative_by_uid
5967 (INSN_UID (curr_insn), -1);
5969 else if (restored_regs_p)
5970 /* The instruction has been restored to the form that
5971 it had during the previous constraints pass. */
5972 lra_update_insn_regno_info (curr_insn);
5973 if (restored_regs_p && lra_dump_file != NULL)
5975 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5976 dump_insn_slim (lra_dump_file, curr_insn);
5981 return change_p;
5984 /* If optional reload pseudos failed to get a hard register or was not
5985 inherited, it is better to remove optional reloads. We do this
5986 transformation after undoing inheritance to figure out necessity to
5987 remove optional reloads easier. Return true if we do any
5988 change. */
5989 static bool
5990 undo_optional_reloads (void)
5992 bool change_p, keep_p;
5993 unsigned int regno, uid;
5994 bitmap_iterator bi, bi2;
5995 rtx_insn *insn;
5996 rtx set, src, dest;
5997 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5999 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
6000 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
6001 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6003 keep_p = false;
6004 /* Keep optional reloads from previous subpasses. */
6005 if (lra_reg_info[regno].restore_regno < 0
6006 /* If the original pseudo changed its allocation, just
6007 removing the optional pseudo is dangerous as the original
6008 pseudo will have longer live range. */
6009 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
6010 keep_p = true;
6011 else if (reg_renumber[regno] >= 0)
6012 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
6014 insn = lra_insn_recog_data[uid]->insn;
6015 if ((set = single_set (insn)) == NULL_RTX)
6016 continue;
6017 src = SET_SRC (set);
6018 dest = SET_DEST (set);
6019 if (! REG_P (src) || ! REG_P (dest))
6020 continue;
6021 if (REGNO (dest) == regno
6022 /* Ignore insn for optional reloads itself. */
6023 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
6024 /* Check only inheritance on last inheritance pass. */
6025 && (int) REGNO (src) >= new_regno_start
6026 /* Check that the optional reload was inherited. */
6027 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6029 keep_p = true;
6030 break;
6033 if (keep_p)
6035 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6036 if (lra_dump_file != NULL)
6037 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6040 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6041 bitmap_initialize (&insn_bitmap, &reg_obstack);
6042 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6044 if (lra_dump_file != NULL)
6045 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6046 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6047 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6049 insn = lra_insn_recog_data[uid]->insn;
6050 if ((set = single_set (insn)) != NULL_RTX)
6052 src = SET_SRC (set);
6053 dest = SET_DEST (set);
6054 if (REG_P (src) && REG_P (dest)
6055 && ((REGNO (src) == regno
6056 && (lra_reg_info[regno].restore_regno
6057 == (int) REGNO (dest)))
6058 || (REGNO (dest) == regno
6059 && (lra_reg_info[regno].restore_regno
6060 == (int) REGNO (src)))))
6062 if (lra_dump_file != NULL)
6064 fprintf (lra_dump_file, " Deleting move %u\n",
6065 INSN_UID (insn));
6066 dump_insn_slim (lra_dump_file, insn);
6068 lra_set_insn_deleted (insn);
6069 continue;
6071 /* We should not worry about generation memory-memory
6072 moves here as if the corresponding inheritance did
6073 not work (inheritance pseudo did not get a hard reg),
6074 we remove the inheritance pseudo and the optional
6075 reload. */
6077 lra_substitute_pseudo_within_insn (
6078 insn, regno,
6079 regno_reg_rtx[lra_reg_info[regno].restore_regno]);
6080 lra_update_insn_regno_info (insn);
6081 if (lra_dump_file != NULL)
6083 fprintf (lra_dump_file,
6084 " Restoring original insn:\n");
6085 dump_insn_slim (lra_dump_file, insn);
6089 /* Clear restore_regnos. */
6090 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6091 lra_reg_info[regno].restore_regno = -1;
6092 bitmap_clear (&insn_bitmap);
6093 bitmap_clear (&removed_optional_reload_pseudos);
6094 return change_p;
6097 /* Entry function for undoing inheritance/split transformation. Return true
6098 if we did any RTL change in this pass. */
6099 bool
6100 lra_undo_inheritance (void)
6102 unsigned int regno;
6103 int restore_regno, hard_regno;
6104 int n_all_inherit, n_inherit, n_all_split, n_split;
6105 bitmap_head remove_pseudos;
6106 bitmap_iterator bi;
6107 bool change_p;
6109 lra_undo_inheritance_iter++;
6110 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6111 return false;
6112 if (lra_dump_file != NULL)
6113 fprintf (lra_dump_file,
6114 "\n********** Undoing inheritance #%d: **********\n\n",
6115 lra_undo_inheritance_iter);
6116 bitmap_initialize (&remove_pseudos, &reg_obstack);
6117 n_inherit = n_all_inherit = 0;
6118 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6119 if (lra_reg_info[regno].restore_regno >= 0)
6121 n_all_inherit++;
6122 if (reg_renumber[regno] < 0
6123 /* If the original pseudo changed its allocation, just
6124 removing inheritance is dangerous as for changing
6125 allocation we used shorter live-ranges. */
6126 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6127 bitmap_set_bit (&remove_pseudos, regno);
6128 else
6129 n_inherit++;
6131 if (lra_dump_file != NULL && n_all_inherit != 0)
6132 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6133 n_inherit, n_all_inherit,
6134 (double) n_inherit / n_all_inherit * 100);
6135 n_split = n_all_split = 0;
6136 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6137 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6139 n_all_split++;
6140 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6141 ? reg_renumber[restore_regno] : restore_regno);
6142 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6143 bitmap_set_bit (&remove_pseudos, regno);
6144 else
6146 n_split++;
6147 if (lra_dump_file != NULL)
6148 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6149 regno, restore_regno);
6152 if (lra_dump_file != NULL && n_all_split != 0)
6153 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6154 n_split, n_all_split,
6155 (double) n_split / n_all_split * 100);
6156 change_p = remove_inheritance_pseudos (&remove_pseudos);
6157 bitmap_clear (&remove_pseudos);
6158 /* Clear restore_regnos. */
6159 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6160 lra_reg_info[regno].restore_regno = -1;
6161 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6162 lra_reg_info[regno].restore_regno = -1;
6163 change_p = undo_optional_reloads () || change_p;
6164 return change_p;