2015-01-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / lra-constraints.c
blob762889ce13ce12646faa1afb59a40d351d79d281
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];
176 /* Start numbers for new registers and insns at the current constraints
177 pass start. */
178 static int new_regno_start;
179 static int new_insn_uid_start;
181 /* If LOC is nonnull, strip any outer subreg from it. */
182 static inline rtx *
183 strip_subreg (rtx *loc)
185 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
188 /* Return hard regno of REGNO or if it is was not assigned to a hard
189 register, use a hard register from its allocno class. */
190 static int
191 get_try_hard_regno (int regno)
193 int hard_regno;
194 enum reg_class rclass;
196 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
197 hard_regno = lra_get_regno_hard_regno (regno);
198 if (hard_regno >= 0)
199 return hard_regno;
200 rclass = lra_get_allocno_class (regno);
201 if (rclass == NO_REGS)
202 return -1;
203 return ira_class_hard_regs[rclass][0];
206 /* Return final hard regno (plus offset) which will be after
207 elimination. We do this for matching constraints because the final
208 hard regno could have a different class. */
209 static int
210 get_final_hard_regno (int hard_regno, int offset)
212 if (hard_regno < 0)
213 return hard_regno;
214 hard_regno = lra_get_elimination_hard_regno (hard_regno);
215 return hard_regno + offset;
218 /* Return hard regno of X after removing subreg and making
219 elimination. If X is not a register or subreg of register, return
220 -1. For pseudo use its assignment. */
221 static int
222 get_hard_regno (rtx x)
224 rtx reg;
225 int offset, hard_regno;
227 reg = x;
228 if (GET_CODE (x) == SUBREG)
229 reg = SUBREG_REG (x);
230 if (! REG_P (reg))
231 return -1;
232 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
233 hard_regno = lra_get_regno_hard_regno (hard_regno);
234 if (hard_regno < 0)
235 return -1;
236 offset = 0;
237 if (GET_CODE (x) == SUBREG)
238 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
239 SUBREG_BYTE (x), GET_MODE (x));
240 return get_final_hard_regno (hard_regno, offset);
243 /* If REGNO is a hard register or has been allocated a hard register,
244 return the class of that register. If REGNO is a reload pseudo
245 created by the current constraints pass, return its allocno class.
246 Return NO_REGS otherwise. */
247 static enum reg_class
248 get_reg_class (int regno)
250 int hard_regno;
252 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
253 hard_regno = lra_get_regno_hard_regno (regno);
254 if (hard_regno >= 0)
256 hard_regno = get_final_hard_regno (hard_regno, 0);
257 return REGNO_REG_CLASS (hard_regno);
259 if (regno >= new_regno_start)
260 return lra_get_allocno_class (regno);
261 return NO_REGS;
264 /* Return true if REG satisfies (or will satisfy) reg class constraint
265 CL. Use elimination first if REG is a hard register. If REG is a
266 reload pseudo created by this constraints pass, assume that it will
267 be allocated a hard register from its allocno class, but allow that
268 class to be narrowed to CL if it is currently a superset of CL.
270 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
271 REGNO (reg), or NO_REGS if no change in its class was needed. */
272 static bool
273 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
275 enum reg_class rclass, common_class;
276 machine_mode reg_mode;
277 int class_size, hard_regno, nregs, i, j;
278 int regno = REGNO (reg);
280 if (new_class != NULL)
281 *new_class = NO_REGS;
282 if (regno < FIRST_PSEUDO_REGISTER)
284 rtx final_reg = reg;
285 rtx *final_loc = &final_reg;
287 lra_eliminate_reg_if_possible (final_loc);
288 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
290 reg_mode = GET_MODE (reg);
291 rclass = get_reg_class (regno);
292 if (regno < new_regno_start
293 /* Do not allow the constraints for reload instructions to
294 influence the classes of new pseudos. These reloads are
295 typically moves that have many alternatives, and restricting
296 reload pseudos for one alternative may lead to situations
297 where other reload pseudos are no longer allocatable. */
298 || (INSN_UID (curr_insn) >= new_insn_uid_start
299 && curr_insn_set != NULL
300 && ((OBJECT_P (SET_SRC (curr_insn_set))
301 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
302 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
303 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
304 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
305 /* When we don't know what class will be used finally for reload
306 pseudos, we use ALL_REGS. */
307 return ((regno >= new_regno_start && rclass == ALL_REGS)
308 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
309 && ! hard_reg_set_subset_p (reg_class_contents[cl],
310 lra_no_alloc_regs)));
311 else
313 common_class = ira_reg_class_subset[rclass][cl];
314 if (new_class != NULL)
315 *new_class = common_class;
316 if (hard_reg_set_subset_p (reg_class_contents[common_class],
317 lra_no_alloc_regs))
318 return false;
319 /* Check that there are enough allocatable regs. */
320 class_size = ira_class_hard_regs_num[common_class];
321 for (i = 0; i < class_size; i++)
323 hard_regno = ira_class_hard_regs[common_class][i];
324 nregs = hard_regno_nregs[hard_regno][reg_mode];
325 if (nregs == 1)
326 return true;
327 for (j = 0; j < nregs; j++)
328 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
329 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
330 hard_regno + j))
331 break;
332 if (j >= nregs)
333 return true;
335 return false;
339 /* Return true if REGNO satisfies a memory constraint. */
340 static bool
341 in_mem_p (int regno)
343 return get_reg_class (regno) == NO_REGS;
346 /* Return 1 if ADDR is a valid memory address for mode MODE in address
347 space AS, and check that each pseudo has the proper kind of hard
348 reg. */
349 static int
350 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
351 rtx addr, addr_space_t as)
353 #ifdef GO_IF_LEGITIMATE_ADDRESS
354 lra_assert (ADDR_SPACE_GENERIC_P (as));
355 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
356 return 0;
358 win:
359 return 1;
360 #else
361 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
362 #endif
365 namespace {
366 /* Temporarily eliminates registers in an address (for the lifetime of
367 the object). */
368 class address_eliminator {
369 public:
370 address_eliminator (struct address_info *ad);
371 ~address_eliminator ();
373 private:
374 struct address_info *m_ad;
375 rtx *m_base_loc;
376 rtx m_base_reg;
377 rtx *m_index_loc;
378 rtx m_index_reg;
382 address_eliminator::address_eliminator (struct address_info *ad)
383 : m_ad (ad),
384 m_base_loc (strip_subreg (ad->base_term)),
385 m_base_reg (NULL_RTX),
386 m_index_loc (strip_subreg (ad->index_term)),
387 m_index_reg (NULL_RTX)
389 if (m_base_loc != NULL)
391 m_base_reg = *m_base_loc;
392 lra_eliminate_reg_if_possible (m_base_loc);
393 if (m_ad->base_term2 != NULL)
394 *m_ad->base_term2 = *m_ad->base_term;
396 if (m_index_loc != NULL)
398 m_index_reg = *m_index_loc;
399 lra_eliminate_reg_if_possible (m_index_loc);
403 address_eliminator::~address_eliminator ()
405 if (m_base_loc && *m_base_loc != m_base_reg)
407 *m_base_loc = m_base_reg;
408 if (m_ad->base_term2 != NULL)
409 *m_ad->base_term2 = *m_ad->base_term;
411 if (m_index_loc && *m_index_loc != m_index_reg)
412 *m_index_loc = m_index_reg;
415 /* Return true if the eliminated form of AD is a legitimate target address. */
416 static bool
417 valid_address_p (struct address_info *ad)
419 address_eliminator eliminator (ad);
420 return valid_address_p (ad->mode, *ad->outer, ad->as);
423 /* Return true if the eliminated form of memory reference OP satisfies
424 extra memory constraint CONSTRAINT. */
425 static bool
426 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
428 struct address_info ad;
430 decompose_mem_address (&ad, op);
431 address_eliminator eliminator (&ad);
432 return constraint_satisfied_p (op, constraint);
435 /* Return true if the eliminated form of address AD satisfies extra
436 address constraint CONSTRAINT. */
437 static bool
438 satisfies_address_constraint_p (struct address_info *ad,
439 enum constraint_num constraint)
441 address_eliminator eliminator (ad);
442 return constraint_satisfied_p (*ad->outer, constraint);
445 /* Return true if the eliminated form of address OP satisfies extra
446 address constraint CONSTRAINT. */
447 static bool
448 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
450 struct address_info ad;
452 decompose_lea_address (&ad, &op);
453 return satisfies_address_constraint_p (&ad, constraint);
456 /* Initiate equivalences for LRA. As we keep original equivalences
457 before any elimination, we need to make copies otherwise any change
458 in insns might change the equivalences. */
459 void
460 lra_init_equiv (void)
462 ira_expand_reg_equiv ();
463 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
465 rtx res;
467 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
468 ira_reg_equiv[i].memory = copy_rtx (res);
469 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
470 ira_reg_equiv[i].invariant = copy_rtx (res);
474 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
476 /* Update equivalence for REGNO. We need to this as the equivalence
477 might contain other pseudos which are changed by their
478 equivalences. */
479 static void
480 update_equiv (int regno)
482 rtx x;
484 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
485 ira_reg_equiv[regno].memory
486 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
487 NULL_RTX);
488 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
489 ira_reg_equiv[regno].invariant
490 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
491 NULL_RTX);
494 /* If we have decided to substitute X with another value, return that
495 value, otherwise return X. */
496 static rtx
497 get_equiv (rtx x)
499 int regno;
500 rtx res;
502 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
503 || ! ira_reg_equiv[regno].defined_p
504 || ! ira_reg_equiv[regno].profitable_p
505 || lra_get_regno_hard_regno (regno) >= 0)
506 return x;
507 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
509 if (targetm.cannot_substitute_mem_equiv_p (res))
510 return x;
511 return res;
513 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
514 return res;
515 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
516 return res;
517 gcc_unreachable ();
520 /* If we have decided to substitute X with the equivalent value,
521 return that value after elimination for INSN, otherwise return
522 X. */
523 static rtx
524 get_equiv_with_elimination (rtx x, rtx_insn *insn)
526 rtx res = get_equiv (x);
528 if (x == res || CONSTANT_P (res))
529 return res;
530 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
531 0, false, false, true);
534 /* Set up curr_operand_mode. */
535 static void
536 init_curr_operand_mode (void)
538 int nop = curr_static_id->n_operands;
539 for (int i = 0; i < nop; i++)
541 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
542 if (mode == VOIDmode)
544 /* The .md mode for address operands is the mode of the
545 addressed value rather than the mode of the address itself. */
546 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
547 mode = Pmode;
548 else
549 mode = curr_static_id->operand[i].mode;
551 curr_operand_mode[i] = mode;
557 /* The page contains code to reuse input reloads. */
559 /* Structure describes input reload of the current insns. */
560 struct input_reload
562 /* Reloaded value. */
563 rtx input;
564 /* Reload pseudo used. */
565 rtx reg;
568 /* The number of elements in the following array. */
569 static int curr_insn_input_reloads_num;
570 /* Array containing info about input reloads. It is used to find the
571 same input reload and reuse the reload pseudo in this case. */
572 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
574 /* Initiate data concerning reuse of input reloads for the current
575 insn. */
576 static void
577 init_curr_insn_input_reloads (void)
579 curr_insn_input_reloads_num = 0;
582 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
583 created input reload pseudo (only if TYPE is not OP_OUT). Don't
584 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
585 wrapped up in SUBREG. The result pseudo is returned through
586 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
587 reused the already created input reload pseudo. Use TITLE to
588 describe new registers for debug purposes. */
589 static bool
590 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
591 enum reg_class rclass, bool in_subreg_p,
592 const char *title, rtx *result_reg)
594 int i, regno;
595 enum reg_class new_class;
597 if (type == OP_OUT)
599 *result_reg
600 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
601 return true;
603 /* Prevent reuse value of expression with side effects,
604 e.g. volatile memory. */
605 if (! side_effects_p (original))
606 for (i = 0; i < curr_insn_input_reloads_num; i++)
607 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
608 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
610 rtx reg = curr_insn_input_reloads[i].reg;
611 regno = REGNO (reg);
612 /* If input is equal to original and both are VOIDmode,
613 GET_MODE (reg) might be still different from mode.
614 Ensure we don't return *result_reg with wrong mode. */
615 if (GET_MODE (reg) != mode)
617 if (in_subreg_p)
618 continue;
619 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
620 continue;
621 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
622 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
623 continue;
625 *result_reg = reg;
626 if (lra_dump_file != NULL)
628 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
629 dump_value_slim (lra_dump_file, original, 1);
631 if (new_class != lra_get_allocno_class (regno))
632 lra_change_class (regno, new_class, ", change to", false);
633 if (lra_dump_file != NULL)
634 fprintf (lra_dump_file, "\n");
635 return false;
637 *result_reg = lra_create_new_reg (mode, original, rclass, title);
638 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
639 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
640 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
641 return true;
646 /* The page contains code to extract memory address parts. */
648 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
649 static inline bool
650 ok_for_index_p_nonstrict (rtx reg)
652 unsigned regno = REGNO (reg);
654 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
657 /* A version of regno_ok_for_base_p for use here, when all pseudos
658 should count as OK. Arguments as for regno_ok_for_base_p. */
659 static inline bool
660 ok_for_base_p_nonstrict (rtx reg, machine_mode mode, addr_space_t as,
661 enum rtx_code outer_code, enum rtx_code index_code)
663 unsigned regno = REGNO (reg);
665 if (regno >= FIRST_PSEUDO_REGISTER)
666 return true;
667 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
672 /* The page contains major code to choose the current insn alternative
673 and generate reloads for it. */
675 /* Return the offset from REGNO of the least significant register
676 in (reg:MODE REGNO).
678 This function is used to tell whether two registers satisfy
679 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
681 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
682 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
684 lra_constraint_offset (int regno, machine_mode mode)
686 lra_assert (regno < FIRST_PSEUDO_REGISTER);
687 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
688 && SCALAR_INT_MODE_P (mode))
689 return hard_regno_nregs[regno][mode] - 1;
690 return 0;
693 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
694 if they are the same hard reg, and has special hacks for
695 auto-increment and auto-decrement. This is specifically intended for
696 process_alt_operands to use in determining whether two operands
697 match. X is the operand whose number is the lower of the two.
699 It is supposed that X is the output operand and Y is the input
700 operand. Y_HARD_REGNO is the final hard regno of register Y or
701 register in subreg Y as we know it now. Otherwise, it is a
702 negative value. */
703 static bool
704 operands_match_p (rtx x, rtx y, int y_hard_regno)
706 int i;
707 RTX_CODE code = GET_CODE (x);
708 const char *fmt;
710 if (x == y)
711 return true;
712 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
713 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
715 int j;
717 i = get_hard_regno (x);
718 if (i < 0)
719 goto slow;
721 if ((j = y_hard_regno) < 0)
722 goto slow;
724 i += lra_constraint_offset (i, GET_MODE (x));
725 j += lra_constraint_offset (j, GET_MODE (y));
727 return i == j;
730 /* If two operands must match, because they are really a single
731 operand of an assembler insn, then two post-increments are invalid
732 because the assembler insn would increment only once. On the
733 other hand, a post-increment matches ordinary indexing if the
734 post-increment is the output operand. */
735 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
736 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
738 /* Two pre-increments are invalid because the assembler insn would
739 increment only once. On the other hand, a pre-increment matches
740 ordinary indexing if the pre-increment is the input operand. */
741 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
742 || GET_CODE (y) == PRE_MODIFY)
743 return operands_match_p (x, XEXP (y, 0), -1);
745 slow:
747 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
748 && x == SUBREG_REG (y))
749 return true;
750 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
751 && SUBREG_REG (x) == y)
752 return true;
754 /* Now we have disposed of all the cases in which different rtx
755 codes can match. */
756 if (code != GET_CODE (y))
757 return false;
759 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
760 if (GET_MODE (x) != GET_MODE (y))
761 return false;
763 switch (code)
765 CASE_CONST_UNIQUE:
766 return false;
768 case LABEL_REF:
769 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
770 case SYMBOL_REF:
771 return XSTR (x, 0) == XSTR (y, 0);
773 default:
774 break;
777 /* Compare the elements. If any pair of corresponding elements fail
778 to match, return false for the whole things. */
780 fmt = GET_RTX_FORMAT (code);
781 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
783 int val, j;
784 switch (fmt[i])
786 case 'w':
787 if (XWINT (x, i) != XWINT (y, i))
788 return false;
789 break;
791 case 'i':
792 if (XINT (x, i) != XINT (y, i))
793 return false;
794 break;
796 case 'e':
797 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
798 if (val == 0)
799 return false;
800 break;
802 case '0':
803 break;
805 case 'E':
806 if (XVECLEN (x, i) != XVECLEN (y, i))
807 return false;
808 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
810 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
811 if (val == 0)
812 return false;
814 break;
816 /* It is believed that rtx's at this level will never
817 contain anything but integers and other rtx's, except for
818 within LABEL_REFs and SYMBOL_REFs. */
819 default:
820 gcc_unreachable ();
823 return true;
826 /* True if X is a constant that can be forced into the constant pool.
827 MODE is the mode of the operand, or VOIDmode if not known. */
828 #define CONST_POOL_OK_P(MODE, X) \
829 ((MODE) != VOIDmode \
830 && CONSTANT_P (X) \
831 && GET_CODE (X) != HIGH \
832 && !targetm.cannot_force_const_mem (MODE, X))
834 /* True if C is a non-empty register class that has too few registers
835 to be safely used as a reload target class. */
836 #define SMALL_REGISTER_CLASS_P(C) \
837 (ira_class_hard_regs_num [(C)] == 1 \
838 || (ira_class_hard_regs_num [(C)] >= 1 \
839 && targetm.class_likely_spilled_p (C)))
841 /* If REG is a reload pseudo, try to make its class satisfying CL. */
842 static void
843 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
845 enum reg_class rclass;
847 /* Do not make more accurate class from reloads generated. They are
848 mostly moves with a lot of constraints. Making more accurate
849 class may results in very narrow class and impossibility of find
850 registers for several reloads of one insn. */
851 if (INSN_UID (curr_insn) >= new_insn_uid_start)
852 return;
853 if (GET_CODE (reg) == SUBREG)
854 reg = SUBREG_REG (reg);
855 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
856 return;
857 if (in_class_p (reg, cl, &rclass) && rclass != cl)
858 lra_change_class (REGNO (reg), rclass, " Change to", true);
861 /* Generate reloads for matching OUT and INS (array of input operand
862 numbers with end marker -1) with reg class GOAL_CLASS. Add input
863 and output reloads correspondingly to the lists *BEFORE and *AFTER.
864 OUT might be negative. In this case we generate input reloads for
865 matched input operands INS. */
866 static void
867 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
868 rtx_insn **before, rtx_insn **after)
870 int i, in;
871 rtx new_in_reg, new_out_reg, reg, clobber;
872 machine_mode inmode, outmode;
873 rtx in_rtx = *curr_id->operand_loc[ins[0]];
874 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
876 inmode = curr_operand_mode[ins[0]];
877 outmode = out < 0 ? inmode : curr_operand_mode[out];
878 push_to_sequence (*before);
879 if (inmode != outmode)
881 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
883 reg = new_in_reg
884 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
885 goal_class, "");
886 if (SCALAR_INT_MODE_P (inmode))
887 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
888 else
889 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
890 LRA_SUBREG_P (new_out_reg) = 1;
891 /* If the input reg is dying here, we can use the same hard
892 register for REG and IN_RTX. We do it only for original
893 pseudos as reload pseudos can die although original
894 pseudos still live where reload pseudos dies. */
895 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
896 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
897 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
899 else
901 reg = new_out_reg
902 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
903 goal_class, "");
904 if (SCALAR_INT_MODE_P (outmode))
905 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
906 else
907 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
908 /* NEW_IN_REG is non-paradoxical subreg. We don't want
909 NEW_OUT_REG living above. We add clobber clause for
910 this. This is just a temporary clobber. We can remove
911 it at the end of LRA work. */
912 clobber = emit_clobber (new_out_reg);
913 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
914 LRA_SUBREG_P (new_in_reg) = 1;
915 if (GET_CODE (in_rtx) == SUBREG)
917 rtx subreg_reg = SUBREG_REG (in_rtx);
919 /* If SUBREG_REG is dying here and sub-registers IN_RTX
920 and NEW_IN_REG are similar, we can use the same hard
921 register for REG and SUBREG_REG. */
922 if (REG_P (subreg_reg)
923 && (int) REGNO (subreg_reg) < lra_new_regno_start
924 && GET_MODE (subreg_reg) == outmode
925 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
926 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
927 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
931 else
933 /* Pseudos have values -- see comments for lra_reg_info.
934 Different pseudos with the same value do not conflict even if
935 they live in the same place. When we create a pseudo we
936 assign value of original pseudo (if any) from which we
937 created the new pseudo. If we create the pseudo from the
938 input pseudo, the new pseudo will no conflict with the input
939 pseudo which is wrong when the input pseudo lives after the
940 insn and as the new pseudo value is changed by the insn
941 output. Therefore we create the new pseudo from the output.
943 We cannot reuse the current output register because we might
944 have a situation like "a <- a op b", where the constraints
945 force the second input operand ("b") to match the output
946 operand ("a"). "b" must then be copied into a new register
947 so that it doesn't clobber the current value of "a". */
949 new_in_reg = new_out_reg
950 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
951 goal_class, "");
953 /* In operand can be got from transformations before processing insn
954 constraints. One example of such transformations is subreg
955 reloading (see function simplify_operand_subreg). The new
956 pseudos created by the transformations might have inaccurate
957 class (ALL_REGS) and we should make their classes more
958 accurate. */
959 narrow_reload_pseudo_class (in_rtx, goal_class);
960 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
961 *before = get_insns ();
962 end_sequence ();
963 for (i = 0; (in = ins[i]) >= 0; i++)
965 lra_assert
966 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
967 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
968 *curr_id->operand_loc[in] = new_in_reg;
970 lra_update_dups (curr_id, ins);
971 if (out < 0)
972 return;
973 /* See a comment for the input operand above. */
974 narrow_reload_pseudo_class (out_rtx, goal_class);
975 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
977 start_sequence ();
978 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
979 emit_insn (*after);
980 *after = get_insns ();
981 end_sequence ();
983 *curr_id->operand_loc[out] = new_out_reg;
984 lra_update_dup (curr_id, out);
987 /* Return register class which is union of all reg classes in insn
988 constraint alternative string starting with P. */
989 static enum reg_class
990 reg_class_from_constraints (const char *p)
992 int c, len;
993 enum reg_class op_class = NO_REGS;
996 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
998 case '#':
999 case ',':
1000 return op_class;
1002 case 'g':
1003 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1004 break;
1006 default:
1007 enum constraint_num cn = lookup_constraint (p);
1008 enum reg_class cl = reg_class_for_constraint (cn);
1009 if (cl == NO_REGS)
1011 if (insn_extra_address_constraint (cn))
1012 op_class
1013 = (reg_class_subunion
1014 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1015 ADDRESS, SCRATCH)]);
1016 break;
1019 op_class = reg_class_subunion[op_class][cl];
1020 break;
1022 while ((p += len), c);
1023 return op_class;
1026 /* If OP is a register, return the class of the register as per
1027 get_reg_class, otherwise return NO_REGS. */
1028 static inline enum reg_class
1029 get_op_class (rtx op)
1031 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1034 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1035 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1036 SUBREG for VAL to make them equal. */
1037 static rtx_insn *
1038 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1040 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1042 /* Usually size of mem_pseudo is greater than val size but in
1043 rare cases it can be less as it can be defined by target
1044 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1045 if (! MEM_P (val))
1047 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1048 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1050 LRA_SUBREG_P (val) = 1;
1052 else
1054 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1055 LRA_SUBREG_P (mem_pseudo) = 1;
1058 return as_a <rtx_insn *> (to_p
1059 ? gen_move_insn (mem_pseudo, val)
1060 : gen_move_insn (val, mem_pseudo));
1063 /* Process a special case insn (register move), return true if we
1064 don't need to process it anymore. INSN should be a single set
1065 insn. Set up that RTL was changed through CHANGE_P and macro
1066 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1067 SEC_MEM_P. */
1068 static bool
1069 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1071 int sregno, dregno;
1072 rtx dest, src, dreg, sreg, old_sreg, new_reg, scratch_reg;
1073 rtx_insn *before;
1074 enum reg_class dclass, sclass, secondary_class;
1075 machine_mode sreg_mode;
1076 secondary_reload_info sri;
1078 lra_assert (curr_insn_set != NULL_RTX);
1079 dreg = dest = SET_DEST (curr_insn_set);
1080 sreg = src = SET_SRC (curr_insn_set);
1081 if (GET_CODE (dest) == SUBREG)
1082 dreg = SUBREG_REG (dest);
1083 if (GET_CODE (src) == SUBREG)
1084 sreg = SUBREG_REG (src);
1085 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1086 return false;
1087 sclass = dclass = NO_REGS;
1088 if (REG_P (dreg))
1089 dclass = get_reg_class (REGNO (dreg));
1090 if (dclass == ALL_REGS)
1091 /* ALL_REGS is used for new pseudos created by transformations
1092 like reload of SUBREG_REG (see function
1093 simplify_operand_subreg). We don't know their class yet. We
1094 should figure out the class from processing the insn
1095 constraints not in this fast path function. Even if ALL_REGS
1096 were a right class for the pseudo, secondary_... hooks usually
1097 are not define for ALL_REGS. */
1098 return false;
1099 sreg_mode = GET_MODE (sreg);
1100 old_sreg = sreg;
1101 if (REG_P (sreg))
1102 sclass = get_reg_class (REGNO (sreg));
1103 if (sclass == ALL_REGS)
1104 /* See comments above. */
1105 return false;
1106 if (sclass == NO_REGS && dclass == NO_REGS)
1107 return false;
1108 #ifdef SECONDARY_MEMORY_NEEDED
1109 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1110 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1111 && ((sclass != NO_REGS && dclass != NO_REGS)
1112 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1113 #endif
1116 *sec_mem_p = true;
1117 return false;
1119 #endif
1120 if (! REG_P (dreg) || ! REG_P (sreg))
1121 return false;
1122 sri.prev_sri = NULL;
1123 sri.icode = CODE_FOR_nothing;
1124 sri.extra_cost = 0;
1125 secondary_class = NO_REGS;
1126 /* Set up hard register for a reload pseudo for hook
1127 secondary_reload because some targets just ignore unassigned
1128 pseudos in the hook. */
1129 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1131 dregno = REGNO (dreg);
1132 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1134 else
1135 dregno = -1;
1136 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1138 sregno = REGNO (sreg);
1139 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1141 else
1142 sregno = -1;
1143 if (sclass != NO_REGS)
1144 secondary_class
1145 = (enum reg_class) targetm.secondary_reload (false, dest,
1146 (reg_class_t) sclass,
1147 GET_MODE (src), &sri);
1148 if (sclass == NO_REGS
1149 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1150 && dclass != NO_REGS))
1152 enum reg_class old_sclass = secondary_class;
1153 secondary_reload_info old_sri = sri;
1155 sri.prev_sri = NULL;
1156 sri.icode = CODE_FOR_nothing;
1157 sri.extra_cost = 0;
1158 secondary_class
1159 = (enum reg_class) targetm.secondary_reload (true, sreg,
1160 (reg_class_t) dclass,
1161 sreg_mode, &sri);
1162 /* Check the target hook consistency. */
1163 lra_assert
1164 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1165 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1166 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1168 if (sregno >= 0)
1169 reg_renumber [sregno] = -1;
1170 if (dregno >= 0)
1171 reg_renumber [dregno] = -1;
1172 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1173 return false;
1174 *change_p = true;
1175 new_reg = NULL_RTX;
1176 if (secondary_class != NO_REGS)
1177 new_reg = lra_create_new_reg_with_unique_value (sreg_mode, NULL_RTX,
1178 secondary_class,
1179 "secondary");
1180 start_sequence ();
1181 if (old_sreg != sreg)
1182 sreg = copy_rtx (sreg);
1183 if (sri.icode == CODE_FOR_nothing)
1184 lra_emit_move (new_reg, sreg);
1185 else
1187 enum reg_class scratch_class;
1189 scratch_class = (reg_class_from_constraints
1190 (insn_data[sri.icode].operand[2].constraint));
1191 scratch_reg = (lra_create_new_reg_with_unique_value
1192 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1193 scratch_class, "scratch"));
1194 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1195 sreg, scratch_reg));
1197 before = get_insns ();
1198 end_sequence ();
1199 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1200 if (new_reg != NULL_RTX)
1202 if (GET_CODE (src) == SUBREG)
1203 SUBREG_REG (src) = new_reg;
1204 else
1205 SET_SRC (curr_insn_set) = new_reg;
1207 else
1209 if (lra_dump_file != NULL)
1211 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1212 dump_insn_slim (lra_dump_file, curr_insn);
1214 lra_set_insn_deleted (curr_insn);
1215 return true;
1217 return false;
1220 /* The following data describe the result of process_alt_operands.
1221 The data are used in curr_insn_transform to generate reloads. */
1223 /* The chosen reg classes which should be used for the corresponding
1224 operands. */
1225 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1226 /* True if the operand should be the same as another operand and that
1227 other operand does not need a reload. */
1228 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1229 /* True if the operand does not need a reload. */
1230 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1231 /* True if the operand can be offsetable memory. */
1232 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1233 /* The number of an operand to which given operand can be matched to. */
1234 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1235 /* The number of elements in the following array. */
1236 static int goal_alt_dont_inherit_ops_num;
1237 /* Numbers of operands whose reload pseudos should not be inherited. */
1238 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1239 /* True if the insn commutative operands should be swapped. */
1240 static bool goal_alt_swapped;
1241 /* The chosen insn alternative. */
1242 static int goal_alt_number;
1244 /* The following five variables are used to choose the best insn
1245 alternative. They reflect final characteristics of the best
1246 alternative. */
1248 /* Number of necessary reloads and overall cost reflecting the
1249 previous value and other unpleasantness of the best alternative. */
1250 static int best_losers, best_overall;
1251 /* Overall number hard registers used for reloads. For example, on
1252 some targets we need 2 general registers to reload DFmode and only
1253 one floating point register. */
1254 static int best_reload_nregs;
1255 /* Overall number reflecting distances of previous reloading the same
1256 value. The distances are counted from the current BB start. It is
1257 used to improve inheritance chances. */
1258 static int best_reload_sum;
1260 /* True if the current insn should have no correspondingly input or
1261 output reloads. */
1262 static bool no_input_reloads_p, no_output_reloads_p;
1264 /* True if we swapped the commutative operands in the current
1265 insn. */
1266 static int curr_swapped;
1268 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1269 register of class CL. Add any input reloads to list BEFORE. AFTER
1270 is nonnull if *LOC is an automodified value; handle that case by
1271 adding the required output reloads to list AFTER. Return true if
1272 the RTL was changed.
1274 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1275 register. Return false if the address register is correct. */
1276 static bool
1277 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1278 enum reg_class cl)
1280 int regno;
1281 enum reg_class rclass, new_class;
1282 rtx reg;
1283 rtx new_reg;
1284 machine_mode mode;
1285 bool subreg_p, before_p = false;
1287 subreg_p = GET_CODE (*loc) == SUBREG;
1288 if (subreg_p)
1289 loc = &SUBREG_REG (*loc);
1290 reg = *loc;
1291 mode = GET_MODE (reg);
1292 if (! REG_P (reg))
1294 if (check_only_p)
1295 return true;
1296 /* Always reload memory in an address even if the target supports
1297 such addresses. */
1298 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1299 before_p = true;
1301 else
1303 regno = REGNO (reg);
1304 rclass = get_reg_class (regno);
1305 if (! check_only_p
1306 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1308 if (lra_dump_file != NULL)
1310 fprintf (lra_dump_file,
1311 "Changing pseudo %d in address of insn %u on equiv ",
1312 REGNO (reg), INSN_UID (curr_insn));
1313 dump_value_slim (lra_dump_file, *loc, 1);
1314 fprintf (lra_dump_file, "\n");
1316 *loc = copy_rtx (*loc);
1318 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1320 if (check_only_p)
1321 return true;
1322 reg = *loc;
1323 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1324 mode, reg, cl, subreg_p, "address", &new_reg))
1325 before_p = true;
1327 else if (new_class != NO_REGS && rclass != new_class)
1329 if (check_only_p)
1330 return true;
1331 lra_change_class (regno, new_class, " Change to", true);
1332 return false;
1334 else
1335 return false;
1337 if (before_p)
1339 push_to_sequence (*before);
1340 lra_emit_move (new_reg, reg);
1341 *before = get_insns ();
1342 end_sequence ();
1344 *loc = new_reg;
1345 if (after != NULL)
1347 start_sequence ();
1348 lra_emit_move (reg, new_reg);
1349 emit_insn (*after);
1350 *after = get_insns ();
1351 end_sequence ();
1353 return true;
1356 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1357 the insn to be inserted before curr insn. AFTER returns the
1358 the insn to be inserted after curr insn. ORIGREG and NEWREG
1359 are the original reg and new reg for reload. */
1360 static void
1361 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1362 rtx newreg)
1364 if (before)
1366 push_to_sequence (*before);
1367 lra_emit_move (newreg, origreg);
1368 *before = get_insns ();
1369 end_sequence ();
1371 if (after)
1373 start_sequence ();
1374 lra_emit_move (origreg, newreg);
1375 emit_insn (*after);
1376 *after = get_insns ();
1377 end_sequence ();
1381 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1383 /* Make reloads for subreg in operand NOP with internal subreg mode
1384 REG_MODE, add new reloads for further processing. Return true if
1385 any reload was generated. */
1386 static bool
1387 simplify_operand_subreg (int nop, machine_mode reg_mode)
1389 int hard_regno;
1390 rtx_insn *before, *after;
1391 machine_mode mode;
1392 rtx reg, new_reg;
1393 rtx operand = *curr_id->operand_loc[nop];
1394 enum reg_class regclass;
1395 enum op_type type;
1397 before = after = NULL;
1399 if (GET_CODE (operand) != SUBREG)
1400 return false;
1402 mode = GET_MODE (operand);
1403 reg = SUBREG_REG (operand);
1404 type = curr_static_id->operand[nop].type;
1405 /* If we change address for paradoxical subreg of memory, the
1406 address might violate the necessary alignment or the access might
1407 be slow. So take this into consideration. We should not worry
1408 about access beyond allocated memory for paradoxical memory
1409 subregs as we don't substitute such equiv memory (see processing
1410 equivalences in function lra_constraints) and because for spilled
1411 pseudos we allocate stack memory enough for the biggest
1412 corresponding paradoxical subreg. */
1413 if (MEM_P (reg)
1414 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1415 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1417 rtx subst, old = *curr_id->operand_loc[nop];
1419 alter_subreg (curr_id->operand_loc[nop], false);
1420 subst = *curr_id->operand_loc[nop];
1421 lra_assert (MEM_P (subst));
1422 if (! valid_address_p (GET_MODE (reg), XEXP (reg, 0),
1423 MEM_ADDR_SPACE (reg))
1424 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1425 MEM_ADDR_SPACE (subst)))
1426 return true;
1427 /* If the address was valid and became invalid, prefer to reload
1428 the memory. Typical case is when the index scale should
1429 correspond the memory. */
1430 *curr_id->operand_loc[nop] = old;
1432 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1434 alter_subreg (curr_id->operand_loc[nop], false);
1435 return true;
1437 /* Put constant into memory when we have mixed modes. It generates
1438 a better code in most cases as it does not need a secondary
1439 reload memory. It also prevents LRA looping when LRA is using
1440 secondary reload memory again and again. */
1441 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1442 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1444 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1445 alter_subreg (curr_id->operand_loc[nop], false);
1446 return true;
1448 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1449 if there may be a problem accessing OPERAND in the outer
1450 mode. */
1451 if ((REG_P (reg)
1452 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1453 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1454 /* Don't reload paradoxical subregs because we could be looping
1455 having repeatedly final regno out of hard regs range. */
1456 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1457 >= hard_regno_nregs[hard_regno][mode])
1458 && simplify_subreg_regno (hard_regno, GET_MODE (reg),
1459 SUBREG_BYTE (operand), mode) < 0
1460 /* Don't reload subreg for matching reload. It is actually
1461 valid subreg in LRA. */
1462 && ! LRA_SUBREG_P (operand))
1463 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1465 enum reg_class rclass;
1467 if (REG_P (reg))
1468 /* There is a big probability that we will get the same class
1469 for the new pseudo and we will get the same insn which
1470 means infinite looping. So spill the new pseudo. */
1471 rclass = NO_REGS;
1472 else
1473 /* The class will be defined later in curr_insn_transform. */
1474 rclass
1475 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1477 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1478 rclass, TRUE, "subreg reg", &new_reg))
1480 bool insert_before, insert_after;
1481 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1483 insert_before = (type != OP_OUT
1484 || GET_MODE_SIZE (GET_MODE (reg)) > GET_MODE_SIZE (mode));
1485 insert_after = (type != OP_IN);
1486 insert_move_for_subreg (insert_before ? &before : NULL,
1487 insert_after ? &after : NULL,
1488 reg, new_reg);
1490 SUBREG_REG (operand) = new_reg;
1491 lra_process_new_insns (curr_insn, before, after,
1492 "Inserting subreg reload");
1493 return true;
1495 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1496 IRA allocates hardreg to the inner pseudo reg according to its mode
1497 instead of the outermode, so the size of the hardreg may not be enough
1498 to contain the outermode operand, in that case we may need to insert
1499 reload for the reg. For the following two types of paradoxical subreg,
1500 we need to insert reload:
1501 1. If the op_type is OP_IN, and the hardreg could not be paired with
1502 other hardreg to contain the outermode operand
1503 (checked by in_hard_reg_set_p), we need to insert the reload.
1504 2. If the op_type is OP_OUT or OP_INOUT.
1506 Here is a paradoxical subreg example showing how the reload is generated:
1508 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1509 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1511 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1512 here, if reg107 is assigned to hardreg R15, because R15 is the last
1513 hardreg, compiler cannot find another hardreg to pair with R15 to
1514 contain TImode data. So we insert a TImode reload reg180 for it.
1515 After reload is inserted:
1517 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1518 (reg:DI 107 [ __comp ])) -1
1519 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1520 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1522 Two reload hard registers will be allocated to reg180 to save TImode data
1523 in LRA_assign. */
1524 else if (REG_P (reg)
1525 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1526 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1527 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1528 < hard_regno_nregs[hard_regno][mode])
1529 && (regclass = lra_get_allocno_class (REGNO (reg)))
1530 && (type != OP_IN
1531 || !in_hard_reg_set_p (reg_class_contents[regclass],
1532 mode, hard_regno)))
1534 /* The class will be defined later in curr_insn_transform. */
1535 enum reg_class rclass
1536 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1538 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1539 rclass, TRUE, "paradoxical subreg", &new_reg))
1541 rtx subreg;
1542 bool insert_before, insert_after;
1544 PUT_MODE (new_reg, mode);
1545 subreg = simplify_gen_subreg (GET_MODE (reg), new_reg, mode, 0);
1546 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1548 insert_before = (type != OP_OUT);
1549 insert_after = (type != OP_IN);
1550 insert_move_for_subreg (insert_before ? &before : NULL,
1551 insert_after ? &after : NULL,
1552 reg, subreg);
1554 SUBREG_REG (operand) = new_reg;
1555 lra_process_new_insns (curr_insn, before, after,
1556 "Inserting paradoxical subreg reload");
1557 return true;
1559 return false;
1562 /* Return TRUE if X refers for a hard register from SET. */
1563 static bool
1564 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1566 int i, j, x_hard_regno;
1567 machine_mode mode;
1568 const char *fmt;
1569 enum rtx_code code;
1571 if (x == NULL_RTX)
1572 return false;
1573 code = GET_CODE (x);
1574 mode = GET_MODE (x);
1575 if (code == SUBREG)
1577 x = SUBREG_REG (x);
1578 code = GET_CODE (x);
1579 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1580 mode = GET_MODE (x);
1583 if (REG_P (x))
1585 x_hard_regno = get_hard_regno (x);
1586 return (x_hard_regno >= 0
1587 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1589 if (MEM_P (x))
1591 struct address_info ad;
1593 decompose_mem_address (&ad, x);
1594 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1595 return true;
1596 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1597 return true;
1599 fmt = GET_RTX_FORMAT (code);
1600 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1602 if (fmt[i] == 'e')
1604 if (uses_hard_regs_p (XEXP (x, i), set))
1605 return true;
1607 else if (fmt[i] == 'E')
1609 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1610 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1611 return true;
1614 return false;
1617 /* Return true if OP is a spilled pseudo. */
1618 static inline bool
1619 spilled_pseudo_p (rtx op)
1621 return (REG_P (op)
1622 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1625 /* Return true if X is a general constant. */
1626 static inline bool
1627 general_constant_p (rtx x)
1629 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1632 static bool
1633 reg_in_class_p (rtx reg, enum reg_class cl)
1635 if (cl == NO_REGS)
1636 return get_reg_class (REGNO (reg)) == NO_REGS;
1637 return in_class_p (reg, cl, NULL);
1640 /* Return true if SET of RCLASS contains no hard regs which can be
1641 used in MODE. */
1642 static bool
1643 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1644 HARD_REG_SET &set,
1645 enum machine_mode mode)
1647 HARD_REG_SET temp;
1649 lra_assert (hard_reg_set_subset_p (set, reg_class_contents[rclass]));
1650 COPY_HARD_REG_SET (temp, set);
1651 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1652 return (hard_reg_set_subset_p
1653 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1656 /* Major function to choose the current insn alternative and what
1657 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1658 negative we should consider only this alternative. Return false if
1659 we can not choose the alternative or find how to reload the
1660 operands. */
1661 static bool
1662 process_alt_operands (int only_alternative)
1664 bool ok_p = false;
1665 int nop, overall, nalt;
1666 int n_alternatives = curr_static_id->n_alternatives;
1667 int n_operands = curr_static_id->n_operands;
1668 /* LOSERS counts the operands that don't fit this alternative and
1669 would require loading. */
1670 int losers;
1671 /* REJECT is a count of how undesirable this alternative says it is
1672 if any reloading is required. If the alternative matches exactly
1673 then REJECT is ignored, but otherwise it gets this much counted
1674 against it in addition to the reloading needed. */
1675 int reject;
1676 int op_reject;
1677 /* The number of elements in the following array. */
1678 int early_clobbered_regs_num;
1679 /* Numbers of operands which are early clobber registers. */
1680 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1681 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1682 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1683 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1684 bool curr_alt_win[MAX_RECOG_OPERANDS];
1685 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1686 int curr_alt_matches[MAX_RECOG_OPERANDS];
1687 /* The number of elements in the following array. */
1688 int curr_alt_dont_inherit_ops_num;
1689 /* Numbers of operands whose reload pseudos should not be inherited. */
1690 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1691 rtx op;
1692 /* The register when the operand is a subreg of register, otherwise the
1693 operand itself. */
1694 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1695 /* The register if the operand is a register or subreg of register,
1696 otherwise NULL. */
1697 rtx operand_reg[MAX_RECOG_OPERANDS];
1698 int hard_regno[MAX_RECOG_OPERANDS];
1699 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1700 int reload_nregs, reload_sum;
1701 bool costly_p;
1702 enum reg_class cl;
1704 /* Calculate some data common for all alternatives to speed up the
1705 function. */
1706 for (nop = 0; nop < n_operands; nop++)
1708 rtx reg;
1710 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1711 /* The real hard regno of the operand after the allocation. */
1712 hard_regno[nop] = get_hard_regno (op);
1714 operand_reg[nop] = reg = op;
1715 biggest_mode[nop] = GET_MODE (op);
1716 if (GET_CODE (op) == SUBREG)
1718 operand_reg[nop] = reg = SUBREG_REG (op);
1719 if (GET_MODE_SIZE (biggest_mode[nop])
1720 < GET_MODE_SIZE (GET_MODE (reg)))
1721 biggest_mode[nop] = GET_MODE (reg);
1723 if (! REG_P (reg))
1724 operand_reg[nop] = NULL_RTX;
1725 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1726 || ((int) REGNO (reg)
1727 == lra_get_elimination_hard_regno (REGNO (reg))))
1728 no_subreg_reg_operand[nop] = reg;
1729 else
1730 operand_reg[nop] = no_subreg_reg_operand[nop]
1731 /* Just use natural mode for elimination result. It should
1732 be enough for extra constraints hooks. */
1733 = regno_reg_rtx[hard_regno[nop]];
1736 /* The constraints are made of several alternatives. Each operand's
1737 constraint looks like foo,bar,... with commas separating the
1738 alternatives. The first alternatives for all operands go
1739 together, the second alternatives go together, etc.
1741 First loop over alternatives. */
1742 alternative_mask preferred = curr_id->preferred_alternatives;
1743 if (only_alternative >= 0)
1744 preferred &= ALTERNATIVE_BIT (only_alternative);
1746 for (nalt = 0; nalt < n_alternatives; nalt++)
1748 /* Loop over operands for one constraint alternative. */
1749 if (!TEST_BIT (preferred, nalt))
1750 continue;
1752 overall = losers = reject = reload_nregs = reload_sum = 0;
1753 for (nop = 0; nop < n_operands; nop++)
1755 int inc = (curr_static_id
1756 ->operand_alternative[nalt * n_operands + nop].reject);
1757 if (lra_dump_file != NULL && inc != 0)
1758 fprintf (lra_dump_file,
1759 " Staticly defined alt reject+=%d\n", inc);
1760 reject += inc;
1762 early_clobbered_regs_num = 0;
1764 for (nop = 0; nop < n_operands; nop++)
1766 const char *p;
1767 char *end;
1768 int len, c, m, i, opalt_num, this_alternative_matches;
1769 bool win, did_match, offmemok, early_clobber_p;
1770 /* false => this operand can be reloaded somehow for this
1771 alternative. */
1772 bool badop;
1773 /* true => this operand can be reloaded if the alternative
1774 allows regs. */
1775 bool winreg;
1776 /* True if a constant forced into memory would be OK for
1777 this operand. */
1778 bool constmemok;
1779 enum reg_class this_alternative, this_costly_alternative;
1780 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1781 bool this_alternative_match_win, this_alternative_win;
1782 bool this_alternative_offmemok;
1783 bool scratch_p;
1784 machine_mode mode;
1785 enum constraint_num cn;
1787 opalt_num = nalt * n_operands + nop;
1788 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1790 /* Fast track for no constraints at all. */
1791 curr_alt[nop] = NO_REGS;
1792 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1793 curr_alt_win[nop] = true;
1794 curr_alt_match_win[nop] = false;
1795 curr_alt_offmemok[nop] = false;
1796 curr_alt_matches[nop] = -1;
1797 continue;
1800 op = no_subreg_reg_operand[nop];
1801 mode = curr_operand_mode[nop];
1803 win = did_match = winreg = offmemok = constmemok = false;
1804 badop = true;
1806 early_clobber_p = false;
1807 p = curr_static_id->operand_alternative[opalt_num].constraint;
1809 this_costly_alternative = this_alternative = NO_REGS;
1810 /* We update set of possible hard regs besides its class
1811 because reg class might be inaccurate. For example,
1812 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1813 is translated in HI_REGS because classes are merged by
1814 pairs and there is no accurate intermediate class. */
1815 CLEAR_HARD_REG_SET (this_alternative_set);
1816 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1817 this_alternative_win = false;
1818 this_alternative_match_win = false;
1819 this_alternative_offmemok = false;
1820 this_alternative_matches = -1;
1822 /* An empty constraint should be excluded by the fast
1823 track. */
1824 lra_assert (*p != 0 && *p != ',');
1826 op_reject = 0;
1827 /* Scan this alternative's specs for this operand; set WIN
1828 if the operand fits any letter in this alternative.
1829 Otherwise, clear BADOP if this operand could fit some
1830 letter after reloads, or set WINREG if this operand could
1831 fit after reloads provided the constraint allows some
1832 registers. */
1833 costly_p = false;
1836 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1838 case '\0':
1839 len = 0;
1840 break;
1841 case ',':
1842 c = '\0';
1843 break;
1845 case '&':
1846 early_clobber_p = true;
1847 break;
1849 case '$':
1850 op_reject += LRA_MAX_REJECT;
1851 break;
1852 case '^':
1853 op_reject += LRA_LOSER_COST_FACTOR;
1854 break;
1856 case '#':
1857 /* Ignore rest of this alternative. */
1858 c = '\0';
1859 break;
1861 case '0': case '1': case '2': case '3': case '4':
1862 case '5': case '6': case '7': case '8': case '9':
1864 int m_hregno;
1865 bool match_p;
1867 m = strtoul (p, &end, 10);
1868 p = end;
1869 len = 0;
1870 lra_assert (nop > m);
1872 this_alternative_matches = m;
1873 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1874 /* We are supposed to match a previous operand.
1875 If we do, we win if that one did. If we do
1876 not, count both of the operands as losers.
1877 (This is too conservative, since most of the
1878 time only a single reload insn will be needed
1879 to make the two operands win. As a result,
1880 this alternative may be rejected when it is
1881 actually desirable.) */
1882 match_p = false;
1883 if (operands_match_p (*curr_id->operand_loc[nop],
1884 *curr_id->operand_loc[m], m_hregno))
1886 /* We should reject matching of an early
1887 clobber operand if the matching operand is
1888 not dying in the insn. */
1889 if (! curr_static_id->operand[m].early_clobber
1890 || operand_reg[nop] == NULL_RTX
1891 || (find_regno_note (curr_insn, REG_DEAD,
1892 REGNO (op))
1893 || REGNO (op) == REGNO (operand_reg[m])))
1894 match_p = true;
1896 if (match_p)
1898 /* If we are matching a non-offsettable
1899 address where an offsettable address was
1900 expected, then we must reject this
1901 combination, because we can't reload
1902 it. */
1903 if (curr_alt_offmemok[m]
1904 && MEM_P (*curr_id->operand_loc[m])
1905 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1906 continue;
1908 else
1910 /* Operands don't match. Both operands must
1911 allow a reload register, otherwise we
1912 cannot make them match. */
1913 if (curr_alt[m] == NO_REGS)
1914 break;
1915 /* Retroactively mark the operand we had to
1916 match as a loser, if it wasn't already and
1917 it wasn't matched to a register constraint
1918 (e.g it might be matched by memory). */
1919 if (curr_alt_win[m]
1920 && (operand_reg[m] == NULL_RTX
1921 || hard_regno[m] < 0))
1923 losers++;
1924 reload_nregs
1925 += (ira_reg_class_max_nregs[curr_alt[m]]
1926 [GET_MODE (*curr_id->operand_loc[m])]);
1929 /* Prefer matching earlyclobber alternative as
1930 it results in less hard regs required for
1931 the insn than a non-matching earlyclobber
1932 alternative. */
1933 if (curr_static_id->operand[m].early_clobber)
1935 if (lra_dump_file != NULL)
1936 fprintf
1937 (lra_dump_file,
1938 " %d Matching earlyclobber alt:"
1939 " reject--\n",
1940 nop);
1941 reject--;
1943 /* Otherwise we prefer no matching
1944 alternatives because it gives more freedom
1945 in RA. */
1946 else if (operand_reg[nop] == NULL_RTX
1947 || (find_regno_note (curr_insn, REG_DEAD,
1948 REGNO (operand_reg[nop]))
1949 == NULL_RTX))
1951 if (lra_dump_file != NULL)
1952 fprintf
1953 (lra_dump_file,
1954 " %d Matching alt: reject+=2\n",
1955 nop);
1956 reject += 2;
1959 /* If we have to reload this operand and some
1960 previous operand also had to match the same
1961 thing as this operand, we don't know how to do
1962 that. */
1963 if (!match_p || !curr_alt_win[m])
1965 for (i = 0; i < nop; i++)
1966 if (curr_alt_matches[i] == m)
1967 break;
1968 if (i < nop)
1969 break;
1971 else
1972 did_match = true;
1974 /* This can be fixed with reloads if the operand
1975 we are supposed to match can be fixed with
1976 reloads. */
1977 badop = false;
1978 this_alternative = curr_alt[m];
1979 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1980 winreg = this_alternative != NO_REGS;
1981 break;
1984 case 'g':
1985 if (MEM_P (op)
1986 || general_constant_p (op)
1987 || spilled_pseudo_p (op))
1988 win = true;
1989 cl = GENERAL_REGS;
1990 goto reg;
1992 default:
1993 cn = lookup_constraint (p);
1994 switch (get_constraint_type (cn))
1996 case CT_REGISTER:
1997 cl = reg_class_for_constraint (cn);
1998 if (cl != NO_REGS)
1999 goto reg;
2000 break;
2002 case CT_CONST_INT:
2003 if (CONST_INT_P (op)
2004 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2005 win = true;
2006 break;
2008 case CT_MEMORY:
2009 if (MEM_P (op)
2010 && satisfies_memory_constraint_p (op, cn))
2011 win = true;
2012 else if (spilled_pseudo_p (op))
2013 win = true;
2015 /* If we didn't already win, we can reload constants
2016 via force_const_mem or put the pseudo value into
2017 memory, or make other memory by reloading the
2018 address like for 'o'. */
2019 if (CONST_POOL_OK_P (mode, op)
2020 || MEM_P (op) || REG_P (op))
2021 badop = false;
2022 constmemok = true;
2023 offmemok = true;
2024 break;
2026 case CT_ADDRESS:
2027 /* If we didn't already win, we can reload the address
2028 into a base register. */
2029 if (satisfies_address_constraint_p (op, cn))
2030 win = true;
2031 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2032 ADDRESS, SCRATCH);
2033 badop = false;
2034 goto reg;
2036 case CT_FIXED_FORM:
2037 if (constraint_satisfied_p (op, cn))
2038 win = true;
2039 break;
2041 break;
2043 reg:
2044 this_alternative = reg_class_subunion[this_alternative][cl];
2045 IOR_HARD_REG_SET (this_alternative_set,
2046 reg_class_contents[cl]);
2047 if (costly_p)
2049 this_costly_alternative
2050 = reg_class_subunion[this_costly_alternative][cl];
2051 IOR_HARD_REG_SET (this_costly_alternative_set,
2052 reg_class_contents[cl]);
2054 if (mode == BLKmode)
2055 break;
2056 winreg = true;
2057 if (REG_P (op))
2059 if (hard_regno[nop] >= 0
2060 && in_hard_reg_set_p (this_alternative_set,
2061 mode, hard_regno[nop]))
2062 win = true;
2063 else if (hard_regno[nop] < 0
2064 && in_class_p (op, this_alternative, NULL))
2065 win = true;
2067 break;
2069 if (c != ' ' && c != '\t')
2070 costly_p = c == '*';
2072 while ((p += len), c);
2074 scratch_p = (operand_reg[nop] != NULL_RTX
2075 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2076 /* Record which operands fit this alternative. */
2077 if (win)
2079 this_alternative_win = true;
2080 if (operand_reg[nop] != NULL_RTX)
2082 if (hard_regno[nop] >= 0)
2084 if (in_hard_reg_set_p (this_costly_alternative_set,
2085 mode, hard_regno[nop]))
2087 if (lra_dump_file != NULL)
2088 fprintf (lra_dump_file,
2089 " %d Costly set: reject++\n",
2090 nop);
2091 reject++;
2094 else
2096 /* Prefer won reg to spilled pseudo under other
2097 equal conditions for possibe inheritance. */
2098 if (! scratch_p)
2100 if (lra_dump_file != NULL)
2101 fprintf
2102 (lra_dump_file,
2103 " %d Non pseudo reload: reject++\n",
2104 nop);
2105 reject++;
2107 if (in_class_p (operand_reg[nop],
2108 this_costly_alternative, NULL))
2110 if (lra_dump_file != NULL)
2111 fprintf
2112 (lra_dump_file,
2113 " %d Non pseudo costly reload:"
2114 " reject++\n",
2115 nop);
2116 reject++;
2119 /* We simulate the behaviour of old reload here.
2120 Although scratches need hard registers and it
2121 might result in spilling other pseudos, no reload
2122 insns are generated for the scratches. So it
2123 might cost something but probably less than old
2124 reload pass believes. */
2125 if (scratch_p)
2127 if (lra_dump_file != NULL)
2128 fprintf (lra_dump_file,
2129 " %d Scratch win: reject+=2\n",
2130 nop);
2131 reject += 2;
2135 else if (did_match)
2136 this_alternative_match_win = true;
2137 else
2139 int const_to_mem = 0;
2140 bool no_regs_p;
2142 reject += op_reject;
2143 /* Never do output reload of stack pointer. It makes
2144 impossible to do elimination when SP is changed in
2145 RTL. */
2146 if (op == stack_pointer_rtx && ! frame_pointer_needed
2147 && curr_static_id->operand[nop].type != OP_IN)
2148 goto fail;
2150 /* If this alternative asks for a specific reg class, see if there
2151 is at least one allocatable register in that class. */
2152 no_regs_p
2153 = (this_alternative == NO_REGS
2154 || (hard_reg_set_subset_p
2155 (reg_class_contents[this_alternative],
2156 lra_no_alloc_regs)));
2158 /* For asms, verify that the class for this alternative is possible
2159 for the mode that is specified. */
2160 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2162 int i;
2163 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2164 if (HARD_REGNO_MODE_OK (i, mode)
2165 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2166 mode, i))
2167 break;
2168 if (i == FIRST_PSEUDO_REGISTER)
2169 winreg = false;
2172 /* If this operand accepts a register, and if the
2173 register class has at least one allocatable register,
2174 then this operand can be reloaded. */
2175 if (winreg && !no_regs_p)
2176 badop = false;
2178 if (badop)
2180 if (lra_dump_file != NULL)
2181 fprintf (lra_dump_file,
2182 " alt=%d: Bad operand -- refuse\n",
2183 nalt);
2184 goto fail;
2187 /* If not assigned pseudo has a class which a subset of
2188 required reg class, it is a less costly alternative
2189 as the pseudo still can get a hard reg of necessary
2190 class. */
2191 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2192 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2193 && ira_class_subset_p[this_alternative][cl])
2195 if (lra_dump_file != NULL)
2196 fprintf
2197 (lra_dump_file,
2198 " %d Super set class reg: reject-=3\n", nop);
2199 reject -= 3;
2202 this_alternative_offmemok = offmemok;
2203 if (this_costly_alternative != NO_REGS)
2205 if (lra_dump_file != NULL)
2206 fprintf (lra_dump_file,
2207 " %d Costly loser: reject++\n", nop);
2208 reject++;
2210 /* If the operand is dying, has a matching constraint,
2211 and satisfies constraints of the matched operand
2212 which failed to satisfy the own constraints, most probably
2213 the reload for this operand will be gone. */
2214 if (this_alternative_matches >= 0
2215 && !curr_alt_win[this_alternative_matches]
2216 && REG_P (op)
2217 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2218 && (hard_regno[nop] >= 0
2219 ? in_hard_reg_set_p (this_alternative_set,
2220 mode, hard_regno[nop])
2221 : in_class_p (op, this_alternative, NULL)))
2223 if (lra_dump_file != NULL)
2224 fprintf
2225 (lra_dump_file,
2226 " %d Dying matched operand reload: reject++\n",
2227 nop);
2228 reject++;
2230 else
2232 /* Strict_low_part requires to reload the register
2233 not the sub-register. In this case we should
2234 check that a final reload hard reg can hold the
2235 value mode. */
2236 if (curr_static_id->operand[nop].strict_low
2237 && REG_P (op)
2238 && hard_regno[nop] < 0
2239 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2240 && ira_class_hard_regs_num[this_alternative] > 0
2241 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2242 [this_alternative][0],
2243 GET_MODE
2244 (*curr_id->operand_loc[nop])))
2246 if (lra_dump_file != NULL)
2247 fprintf
2248 (lra_dump_file,
2249 " alt=%d: Strict low subreg reload -- refuse\n",
2250 nalt);
2251 goto fail;
2253 losers++;
2255 if (operand_reg[nop] != NULL_RTX
2256 /* Output operands and matched input operands are
2257 not inherited. The following conditions do not
2258 exactly describe the previous statement but they
2259 are pretty close. */
2260 && curr_static_id->operand[nop].type != OP_OUT
2261 && (this_alternative_matches < 0
2262 || curr_static_id->operand[nop].type != OP_IN))
2264 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2265 (operand_reg[nop])]
2266 .last_reload);
2268 /* The value of reload_sum has sense only if we
2269 process insns in their order. It happens only on
2270 the first constraints sub-pass when we do most of
2271 reload work. */
2272 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2273 reload_sum += last_reload - bb_reload_num;
2275 /* If this is a constant that is reloaded into the
2276 desired class by copying it to memory first, count
2277 that as another reload. This is consistent with
2278 other code and is required to avoid choosing another
2279 alternative when the constant is moved into memory.
2280 Note that the test here is precisely the same as in
2281 the code below that calls force_const_mem. */
2282 if (CONST_POOL_OK_P (mode, op)
2283 && ((targetm.preferred_reload_class
2284 (op, this_alternative) == NO_REGS)
2285 || no_input_reloads_p))
2287 const_to_mem = 1;
2288 if (! no_regs_p)
2289 losers++;
2292 /* Alternative loses if it requires a type of reload not
2293 permitted for this insn. We can always reload
2294 objects with a REG_UNUSED note. */
2295 if ((curr_static_id->operand[nop].type != OP_IN
2296 && no_output_reloads_p
2297 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2298 || (curr_static_id->operand[nop].type != OP_OUT
2299 && no_input_reloads_p && ! const_to_mem)
2300 || (this_alternative_matches >= 0
2301 && (no_input_reloads_p
2302 || (no_output_reloads_p
2303 && (curr_static_id->operand
2304 [this_alternative_matches].type != OP_IN)
2305 && ! find_reg_note (curr_insn, REG_UNUSED,
2306 no_subreg_reg_operand
2307 [this_alternative_matches])))))
2309 if (lra_dump_file != NULL)
2310 fprintf
2311 (lra_dump_file,
2312 " alt=%d: No input/otput reload -- refuse\n",
2313 nalt);
2314 goto fail;
2317 /* Alternative loses if it required class pseudo can not
2318 hold value of required mode. Such insns can be
2319 described by insn definitions with mode iterators. */
2320 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2321 && ! hard_reg_set_empty_p (this_alternative_set)
2322 /* It is common practice for constraints to use a
2323 class which does not have actually enough regs to
2324 hold the value (e.g. x86 AREG for mode requiring
2325 more one general reg). Therefore we have 2
2326 conditions to check that the reload pseudo can
2327 not hold the mode value. */
2328 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2329 [this_alternative][0],
2330 GET_MODE (*curr_id->operand_loc[nop]))
2331 /* The above condition is not enough as the first
2332 reg in ira_class_hard_regs can be not aligned for
2333 multi-words mode values. */
2334 && (prohibited_class_reg_set_mode_p
2335 (this_alternative, this_alternative_set,
2336 GET_MODE (*curr_id->operand_loc[nop]))))
2338 if (lra_dump_file != NULL)
2339 fprintf (lra_dump_file,
2340 " alt=%d: reload pseudo for op %d "
2341 " can not hold the mode value -- refuse\n",
2342 nalt, nop);
2343 goto fail;
2346 /* Check strong discouragement of reload of non-constant
2347 into class THIS_ALTERNATIVE. */
2348 if (! CONSTANT_P (op) && ! no_regs_p
2349 && (targetm.preferred_reload_class
2350 (op, this_alternative) == NO_REGS
2351 || (curr_static_id->operand[nop].type == OP_OUT
2352 && (targetm.preferred_output_reload_class
2353 (op, this_alternative) == NO_REGS))))
2355 if (lra_dump_file != NULL)
2356 fprintf (lra_dump_file,
2357 " %d Non-prefered reload: reject+=%d\n",
2358 nop, LRA_MAX_REJECT);
2359 reject += LRA_MAX_REJECT;
2362 if (! (MEM_P (op) && offmemok)
2363 && ! (const_to_mem && constmemok))
2365 /* We prefer to reload pseudos over reloading other
2366 things, since such reloads may be able to be
2367 eliminated later. So bump REJECT in other cases.
2368 Don't do this in the case where we are forcing a
2369 constant into memory and it will then win since
2370 we don't want to have a different alternative
2371 match then. */
2372 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2374 if (lra_dump_file != NULL)
2375 fprintf
2376 (lra_dump_file,
2377 " %d Non-pseudo reload: reject+=2\n",
2378 nop);
2379 reject += 2;
2382 if (! no_regs_p)
2383 reload_nregs
2384 += ira_reg_class_max_nregs[this_alternative][mode];
2386 if (SMALL_REGISTER_CLASS_P (this_alternative))
2388 if (lra_dump_file != NULL)
2389 fprintf
2390 (lra_dump_file,
2391 " %d Small class reload: reject+=%d\n",
2392 nop, LRA_LOSER_COST_FACTOR / 2);
2393 reject += LRA_LOSER_COST_FACTOR / 2;
2397 /* We are trying to spill pseudo into memory. It is
2398 usually more costly than moving to a hard register
2399 although it might takes the same number of
2400 reloads. */
2401 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2403 if (lra_dump_file != NULL)
2404 fprintf
2405 (lra_dump_file,
2406 " %d Spill pseudo into memory: reject+=3\n",
2407 nop);
2408 reject += 3;
2409 if (VECTOR_MODE_P (mode))
2411 /* Spilling vectors into memory is usually more
2412 costly as they contain big values. */
2413 if (lra_dump_file != NULL)
2414 fprintf
2415 (lra_dump_file,
2416 " %d Spill vector pseudo: reject+=2\n",
2417 nop);
2418 reject += 2;
2422 #ifdef SECONDARY_MEMORY_NEEDED
2423 /* If reload requires moving value through secondary
2424 memory, it will need one more insn at least. */
2425 if (this_alternative != NO_REGS
2426 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2427 && ((curr_static_id->operand[nop].type != OP_OUT
2428 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2429 GET_MODE (op)))
2430 || (curr_static_id->operand[nop].type != OP_IN
2431 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2432 GET_MODE (op)))))
2433 losers++;
2434 #endif
2435 /* Input reloads can be inherited more often than output
2436 reloads can be removed, so penalize output
2437 reloads. */
2438 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2440 if (lra_dump_file != NULL)
2441 fprintf
2442 (lra_dump_file,
2443 " %d Non input pseudo reload: reject++\n",
2444 nop);
2445 reject++;
2449 if (early_clobber_p && ! scratch_p)
2451 if (lra_dump_file != NULL)
2452 fprintf (lra_dump_file,
2453 " %d Early clobber: reject++\n", nop);
2454 reject++;
2456 /* ??? We check early clobbers after processing all operands
2457 (see loop below) and there we update the costs more.
2458 Should we update the cost (may be approximately) here
2459 because of early clobber register reloads or it is a rare
2460 or non-important thing to be worth to do it. */
2461 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2462 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2464 if (lra_dump_file != NULL)
2465 fprintf (lra_dump_file,
2466 " alt=%d,overall=%d,losers=%d -- refuse\n",
2467 nalt, overall, losers);
2468 goto fail;
2471 curr_alt[nop] = this_alternative;
2472 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2473 curr_alt_win[nop] = this_alternative_win;
2474 curr_alt_match_win[nop] = this_alternative_match_win;
2475 curr_alt_offmemok[nop] = this_alternative_offmemok;
2476 curr_alt_matches[nop] = this_alternative_matches;
2478 if (this_alternative_matches >= 0
2479 && !did_match && !this_alternative_win)
2480 curr_alt_win[this_alternative_matches] = false;
2482 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2483 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2485 if (curr_insn_set != NULL_RTX && n_operands == 2
2486 /* Prevent processing non-move insns. */
2487 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2488 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2489 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2490 && REG_P (no_subreg_reg_operand[0])
2491 && REG_P (no_subreg_reg_operand[1])
2492 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2493 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2494 || (! curr_alt_win[0] && curr_alt_win[1]
2495 && REG_P (no_subreg_reg_operand[1])
2496 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2497 || (curr_alt_win[0] && ! curr_alt_win[1]
2498 && REG_P (no_subreg_reg_operand[0])
2499 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2500 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2501 no_subreg_reg_operand[1])
2502 || (targetm.preferred_reload_class
2503 (no_subreg_reg_operand[1],
2504 (enum reg_class) curr_alt[1]) != NO_REGS))
2505 /* If it is a result of recent elimination in move
2506 insn we can transform it into an add still by
2507 using this alternative. */
2508 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2510 /* We have a move insn and a new reload insn will be similar
2511 to the current insn. We should avoid such situation as it
2512 results in LRA cycling. */
2513 overall += LRA_MAX_REJECT;
2515 ok_p = true;
2516 curr_alt_dont_inherit_ops_num = 0;
2517 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2519 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2520 HARD_REG_SET temp_set;
2522 i = early_clobbered_nops[nop];
2523 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2524 || hard_regno[i] < 0)
2525 continue;
2526 lra_assert (operand_reg[i] != NULL_RTX);
2527 clobbered_hard_regno = hard_regno[i];
2528 CLEAR_HARD_REG_SET (temp_set);
2529 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2530 first_conflict_j = last_conflict_j = -1;
2531 for (j = 0; j < n_operands; j++)
2532 if (j == i
2533 /* We don't want process insides of match_operator and
2534 match_parallel because otherwise we would process
2535 their operands once again generating a wrong
2536 code. */
2537 || curr_static_id->operand[j].is_operator)
2538 continue;
2539 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2540 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2541 continue;
2542 /* If we don't reload j-th operand, check conflicts. */
2543 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2544 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2546 if (first_conflict_j < 0)
2547 first_conflict_j = j;
2548 last_conflict_j = j;
2550 if (last_conflict_j < 0)
2551 continue;
2552 /* If earlyclobber operand conflicts with another
2553 non-matching operand which is actually the same register
2554 as the earlyclobber operand, it is better to reload the
2555 another operand as an operand matching the earlyclobber
2556 operand can be also the same. */
2557 if (first_conflict_j == last_conflict_j
2558 && operand_reg[last_conflict_j]
2559 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2560 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2562 curr_alt_win[last_conflict_j] = false;
2563 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2564 = last_conflict_j;
2565 losers++;
2566 /* Early clobber was already reflected in REJECT. */
2567 lra_assert (reject > 0);
2568 if (lra_dump_file != NULL)
2569 fprintf
2570 (lra_dump_file,
2571 " %d Conflict early clobber reload: reject--\n",
2573 reject--;
2574 overall += LRA_LOSER_COST_FACTOR - 1;
2576 else
2578 /* We need to reload early clobbered register and the
2579 matched registers. */
2580 for (j = 0; j < n_operands; j++)
2581 if (curr_alt_matches[j] == i)
2583 curr_alt_match_win[j] = false;
2584 losers++;
2585 overall += LRA_LOSER_COST_FACTOR;
2587 if (! curr_alt_match_win[i])
2588 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2589 else
2591 /* Remember pseudos used for match reloads are never
2592 inherited. */
2593 lra_assert (curr_alt_matches[i] >= 0);
2594 curr_alt_win[curr_alt_matches[i]] = false;
2596 curr_alt_win[i] = curr_alt_match_win[i] = false;
2597 losers++;
2598 /* Early clobber was already reflected in REJECT. */
2599 lra_assert (reject > 0);
2600 if (lra_dump_file != NULL)
2601 fprintf
2602 (lra_dump_file,
2603 " %d Matched conflict early clobber reloads:"
2604 "reject--\n",
2606 reject--;
2607 overall += LRA_LOSER_COST_FACTOR - 1;
2610 if (lra_dump_file != NULL)
2611 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2612 nalt, overall, losers, reload_nregs);
2614 /* If this alternative can be made to work by reloading, and it
2615 needs less reloading than the others checked so far, record
2616 it as the chosen goal for reloading. */
2617 if ((best_losers != 0 && losers == 0)
2618 || (((best_losers == 0 && losers == 0)
2619 || (best_losers != 0 && losers != 0))
2620 && (best_overall > overall
2621 || (best_overall == overall
2622 /* If the cost of the reloads is the same,
2623 prefer alternative which requires minimal
2624 number of reload regs. */
2625 && (reload_nregs < best_reload_nregs
2626 || (reload_nregs == best_reload_nregs
2627 && (best_reload_sum < reload_sum
2628 || (best_reload_sum == reload_sum
2629 && nalt < goal_alt_number))))))))
2631 for (nop = 0; nop < n_operands; nop++)
2633 goal_alt_win[nop] = curr_alt_win[nop];
2634 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2635 goal_alt_matches[nop] = curr_alt_matches[nop];
2636 goal_alt[nop] = curr_alt[nop];
2637 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2639 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2640 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2641 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2642 goal_alt_swapped = curr_swapped;
2643 best_overall = overall;
2644 best_losers = losers;
2645 best_reload_nregs = reload_nregs;
2646 best_reload_sum = reload_sum;
2647 goal_alt_number = nalt;
2649 if (losers == 0)
2650 /* Everything is satisfied. Do not process alternatives
2651 anymore. */
2652 break;
2653 fail:
2656 return ok_p;
2659 /* Make reload base reg from address AD. */
2660 static rtx
2661 base_to_reg (struct address_info *ad)
2663 enum reg_class cl;
2664 int code = -1;
2665 rtx new_inner = NULL_RTX;
2666 rtx new_reg = NULL_RTX;
2667 rtx_insn *insn;
2668 rtx_insn *last_insn = get_last_insn();
2670 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2671 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2672 get_index_code (ad));
2673 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2674 cl, "base");
2675 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2676 ad->disp_term == NULL
2677 ? gen_int_mode (0, ad->mode)
2678 : *ad->disp_term);
2679 if (!valid_address_p (ad->mode, new_inner, ad->as))
2680 return NULL_RTX;
2681 insn = emit_insn (gen_rtx_SET (ad->mode, new_reg, *ad->base_term));
2682 code = recog_memoized (insn);
2683 if (code < 0)
2685 delete_insns_since (last_insn);
2686 return NULL_RTX;
2689 return new_inner;
2692 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2693 static rtx
2694 base_plus_disp_to_reg (struct address_info *ad)
2696 enum reg_class cl;
2697 rtx new_reg;
2699 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2700 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2701 get_index_code (ad));
2702 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2703 cl, "base + disp");
2704 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2705 return new_reg;
2708 /* Make reload of index part of address AD. Return the new
2709 pseudo. */
2710 static rtx
2711 index_part_to_reg (struct address_info *ad)
2713 rtx new_reg;
2715 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2716 INDEX_REG_CLASS, "index term");
2717 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2718 GEN_INT (get_index_scale (ad)), new_reg, 1);
2719 return new_reg;
2722 /* Return true if we can add a displacement to address AD, even if that
2723 makes the address invalid. The fix-up code requires any new address
2724 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2725 static bool
2726 can_add_disp_p (struct address_info *ad)
2728 return (!ad->autoinc_p
2729 && ad->segment == NULL
2730 && ad->base == ad->base_term
2731 && ad->disp == ad->disp_term);
2734 /* Make equiv substitution in address AD. Return true if a substitution
2735 was made. */
2736 static bool
2737 equiv_address_substitution (struct address_info *ad)
2739 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2740 HOST_WIDE_INT disp, scale;
2741 bool change_p;
2743 base_term = strip_subreg (ad->base_term);
2744 if (base_term == NULL)
2745 base_reg = new_base_reg = NULL_RTX;
2746 else
2748 base_reg = *base_term;
2749 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2751 index_term = strip_subreg (ad->index_term);
2752 if (index_term == NULL)
2753 index_reg = new_index_reg = NULL_RTX;
2754 else
2756 index_reg = *index_term;
2757 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2759 if (base_reg == new_base_reg && index_reg == new_index_reg)
2760 return false;
2761 disp = 0;
2762 change_p = false;
2763 if (lra_dump_file != NULL)
2765 fprintf (lra_dump_file, "Changing address in insn %d ",
2766 INSN_UID (curr_insn));
2767 dump_value_slim (lra_dump_file, *ad->outer, 1);
2769 if (base_reg != new_base_reg)
2771 if (REG_P (new_base_reg))
2773 *base_term = new_base_reg;
2774 change_p = true;
2776 else if (GET_CODE (new_base_reg) == PLUS
2777 && REG_P (XEXP (new_base_reg, 0))
2778 && CONST_INT_P (XEXP (new_base_reg, 1))
2779 && can_add_disp_p (ad))
2781 disp += INTVAL (XEXP (new_base_reg, 1));
2782 *base_term = XEXP (new_base_reg, 0);
2783 change_p = true;
2785 if (ad->base_term2 != NULL)
2786 *ad->base_term2 = *ad->base_term;
2788 if (index_reg != new_index_reg)
2790 if (REG_P (new_index_reg))
2792 *index_term = new_index_reg;
2793 change_p = true;
2795 else if (GET_CODE (new_index_reg) == PLUS
2796 && REG_P (XEXP (new_index_reg, 0))
2797 && CONST_INT_P (XEXP (new_index_reg, 1))
2798 && can_add_disp_p (ad)
2799 && (scale = get_index_scale (ad)))
2801 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2802 *index_term = XEXP (new_index_reg, 0);
2803 change_p = true;
2806 if (disp != 0)
2808 if (ad->disp != NULL)
2809 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2810 else
2812 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2813 update_address (ad);
2815 change_p = true;
2817 if (lra_dump_file != NULL)
2819 if (! change_p)
2820 fprintf (lra_dump_file, " -- no change\n");
2821 else
2823 fprintf (lra_dump_file, " on equiv ");
2824 dump_value_slim (lra_dump_file, *ad->outer, 1);
2825 fprintf (lra_dump_file, "\n");
2828 return change_p;
2831 /* Major function to make reloads for an address in operand NOP or
2832 check its correctness (If CHECK_ONLY_P is true). The supported
2833 cases are:
2835 1) an address that existed before LRA started, at which point it
2836 must have been valid. These addresses are subject to elimination
2837 and may have become invalid due to the elimination offset being out
2838 of range.
2840 2) an address created by forcing a constant to memory
2841 (force_const_to_mem). The initial form of these addresses might
2842 not be valid, and it is this function's job to make them valid.
2844 3) a frame address formed from a register and a (possibly zero)
2845 constant offset. As above, these addresses might not be valid and
2846 this function must make them so.
2848 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2849 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2850 address. Return true for any RTL change.
2852 The function is a helper function which does not produce all
2853 transformations (when CHECK_ONLY_P is false) which can be
2854 necessary. It does just basic steps. To do all necessary
2855 transformations use function process_address. */
2856 static bool
2857 process_address_1 (int nop, bool check_only_p,
2858 rtx_insn **before, rtx_insn **after)
2860 struct address_info ad;
2861 rtx new_reg;
2862 rtx op = *curr_id->operand_loc[nop];
2863 const char *constraint = curr_static_id->operand[nop].constraint;
2864 enum constraint_num cn = lookup_constraint (constraint);
2865 bool change_p = false;
2867 if (insn_extra_address_constraint (cn))
2868 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2869 else if (MEM_P (op))
2870 decompose_mem_address (&ad, op);
2871 else if (GET_CODE (op) == SUBREG
2872 && MEM_P (SUBREG_REG (op)))
2873 decompose_mem_address (&ad, SUBREG_REG (op));
2874 else
2875 return false;
2876 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
2877 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
2878 when INDEX_REG_CLASS is a single register class. */
2879 if (ad.base_term != NULL
2880 && ad.index_term != NULL
2881 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
2882 && REG_P (*ad.base_term)
2883 && REG_P (*ad.index_term)
2884 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
2885 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
2887 std::swap (ad.base, ad.index);
2888 std::swap (ad.base_term, ad.index_term);
2890 if (! check_only_p)
2891 change_p = equiv_address_substitution (&ad);
2892 if (ad.base_term != NULL
2893 && (process_addr_reg
2894 (ad.base_term, check_only_p, before,
2895 (ad.autoinc_p
2896 && !(REG_P (*ad.base_term)
2897 && find_regno_note (curr_insn, REG_DEAD,
2898 REGNO (*ad.base_term)) != NULL_RTX)
2899 ? after : NULL),
2900 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2901 get_index_code (&ad)))))
2903 change_p = true;
2904 if (ad.base_term2 != NULL)
2905 *ad.base_term2 = *ad.base_term;
2907 if (ad.index_term != NULL
2908 && process_addr_reg (ad.index_term, check_only_p,
2909 before, NULL, INDEX_REG_CLASS))
2910 change_p = true;
2912 /* Target hooks sometimes don't treat extra-constraint addresses as
2913 legitimate address_operands, so handle them specially. */
2914 if (insn_extra_address_constraint (cn)
2915 && satisfies_address_constraint_p (&ad, cn))
2916 return change_p;
2918 if (check_only_p)
2919 return change_p;
2921 /* There are three cases where the shape of *AD.INNER may now be invalid:
2923 1) the original address was valid, but either elimination or
2924 equiv_address_substitution was applied and that made
2925 the address invalid.
2927 2) the address is an invalid symbolic address created by
2928 force_const_to_mem.
2930 3) the address is a frame address with an invalid offset.
2932 4) the address is a frame address with an invalid base.
2934 All these cases involve a non-autoinc address, so there is no
2935 point revalidating other types. */
2936 if (ad.autoinc_p || valid_address_p (&ad))
2937 return change_p;
2939 /* Any index existed before LRA started, so we can assume that the
2940 presence and shape of the index is valid. */
2941 push_to_sequence (*before);
2942 lra_assert (ad.disp == ad.disp_term);
2943 if (ad.base == NULL)
2945 if (ad.index == NULL)
2947 int code = -1;
2948 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2949 SCRATCH, SCRATCH);
2950 rtx addr = *ad.inner;
2952 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2953 #ifdef HAVE_lo_sum
2955 rtx_insn *insn;
2956 rtx_insn *last = get_last_insn ();
2958 /* addr => lo_sum (new_base, addr), case (2) above. */
2959 insn = emit_insn (gen_rtx_SET
2960 (VOIDmode, new_reg,
2961 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2962 code = recog_memoized (insn);
2963 if (code >= 0)
2965 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2966 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2968 /* Try to put lo_sum into register. */
2969 insn = emit_insn (gen_rtx_SET
2970 (VOIDmode, new_reg,
2971 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2972 code = recog_memoized (insn);
2973 if (code >= 0)
2975 *ad.inner = new_reg;
2976 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2978 *ad.inner = addr;
2979 code = -1;
2985 if (code < 0)
2986 delete_insns_since (last);
2988 #endif
2989 if (code < 0)
2991 /* addr => new_base, case (2) above. */
2992 lra_emit_move (new_reg, addr);
2993 *ad.inner = new_reg;
2996 else
2998 /* index * scale + disp => new base + index * scale,
2999 case (1) above. */
3000 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3001 GET_CODE (*ad.index));
3003 lra_assert (INDEX_REG_CLASS != NO_REGS);
3004 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3005 lra_emit_move (new_reg, *ad.disp);
3006 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3007 new_reg, *ad.index);
3010 else if (ad.index == NULL)
3012 int regno;
3013 enum reg_class cl;
3014 rtx set;
3015 rtx_insn *insns, *last_insn;
3016 /* Try to reload base into register only if the base is invalid
3017 for the address but with valid offset, case (4) above. */
3018 start_sequence ();
3019 new_reg = base_to_reg (&ad);
3021 /* base + disp => new base, cases (1) and (3) above. */
3022 /* Another option would be to reload the displacement into an
3023 index register. However, postreload has code to optimize
3024 address reloads that have the same base and different
3025 displacements, so reloading into an index register would
3026 not necessarily be a win. */
3027 if (new_reg == NULL_RTX)
3028 new_reg = base_plus_disp_to_reg (&ad);
3029 insns = get_insns ();
3030 last_insn = get_last_insn ();
3031 /* If we generated at least two insns, try last insn source as
3032 an address. If we succeed, we generate one less insn. */
3033 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3034 && GET_CODE (SET_SRC (set)) == PLUS
3035 && REG_P (XEXP (SET_SRC (set), 0))
3036 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3038 *ad.inner = SET_SRC (set);
3039 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3041 *ad.base_term = XEXP (SET_SRC (set), 0);
3042 *ad.disp_term = XEXP (SET_SRC (set), 1);
3043 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3044 get_index_code (&ad));
3045 regno = REGNO (*ad.base_term);
3046 if (regno >= FIRST_PSEUDO_REGISTER
3047 && cl != lra_get_allocno_class (regno))
3048 lra_change_class (regno, cl, " Change to", true);
3049 new_reg = SET_SRC (set);
3050 delete_insns_since (PREV_INSN (last_insn));
3053 /* Try if target can split displacement into legitimite new disp
3054 and offset. If it's the case, we replace the last insn with
3055 insns for base + offset => new_reg and set new_reg + new disp
3056 to *ad.inner. */
3057 last_insn = get_last_insn ();
3058 if ((set = single_set (last_insn)) != NULL_RTX
3059 && GET_CODE (SET_SRC (set)) == PLUS
3060 && REG_P (XEXP (SET_SRC (set), 0))
3061 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3062 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3064 rtx addend, disp = XEXP (SET_SRC (set), 1);
3065 if (targetm.legitimize_address_displacement (&disp, &addend,
3066 ad.mode))
3068 rtx_insn *new_insns;
3069 start_sequence ();
3070 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3071 new_insns = get_insns ();
3072 end_sequence ();
3073 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3074 delete_insns_since (PREV_INSN (last_insn));
3075 add_insn (new_insns);
3076 insns = get_insns ();
3079 end_sequence ();
3080 emit_insn (insns);
3081 *ad.inner = new_reg;
3083 else if (ad.disp_term != NULL)
3085 /* base + scale * index + disp => new base + scale * index,
3086 case (1) above. */
3087 new_reg = base_plus_disp_to_reg (&ad);
3088 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3089 new_reg, *ad.index);
3091 else if (get_index_scale (&ad) == 1)
3093 /* The last transformation to one reg will be made in
3094 curr_insn_transform function. */
3095 end_sequence ();
3096 return false;
3098 else
3100 /* base + scale * index => base + new_reg,
3101 case (1) above.
3102 Index part of address may become invalid. For example, we
3103 changed pseudo on the equivalent memory and a subreg of the
3104 pseudo onto the memory of different mode for which the scale is
3105 prohibitted. */
3106 new_reg = index_part_to_reg (&ad);
3107 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3108 *ad.base_term, new_reg);
3110 *before = get_insns ();
3111 end_sequence ();
3112 return true;
3115 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3116 Use process_address_1 as a helper function. Return true for any
3117 RTL changes.
3119 If CHECK_ONLY_P is true, just check address correctness. Return
3120 false if the address correct. */
3121 static bool
3122 process_address (int nop, bool check_only_p,
3123 rtx_insn **before, rtx_insn **after)
3125 bool res = false;
3127 while (process_address_1 (nop, check_only_p, before, after))
3129 if (check_only_p)
3130 return true;
3131 res = true;
3133 return res;
3136 /* Emit insns to reload VALUE into a new register. VALUE is an
3137 auto-increment or auto-decrement RTX whose operand is a register or
3138 memory location; so reloading involves incrementing that location.
3139 IN is either identical to VALUE, or some cheaper place to reload
3140 value being incremented/decremented from.
3142 INC_AMOUNT is the number to increment or decrement by (always
3143 positive and ignored for POST_MODIFY/PRE_MODIFY).
3145 Return pseudo containing the result. */
3146 static rtx
3147 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3149 /* REG or MEM to be copied and incremented. */
3150 rtx incloc = XEXP (value, 0);
3151 /* Nonzero if increment after copying. */
3152 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3153 || GET_CODE (value) == POST_MODIFY);
3154 rtx_insn *last;
3155 rtx inc;
3156 rtx_insn *add_insn;
3157 int code;
3158 rtx real_in = in == value ? incloc : in;
3159 rtx result;
3160 bool plus_p = true;
3162 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3164 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3165 || GET_CODE (XEXP (value, 1)) == MINUS);
3166 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3167 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3168 inc = XEXP (XEXP (value, 1), 1);
3170 else
3172 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3173 inc_amount = -inc_amount;
3175 inc = GEN_INT (inc_amount);
3178 if (! post && REG_P (incloc))
3179 result = incloc;
3180 else
3181 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3182 "INC/DEC result");
3184 if (real_in != result)
3186 /* First copy the location to the result register. */
3187 lra_assert (REG_P (result));
3188 emit_insn (gen_move_insn (result, real_in));
3191 /* We suppose that there are insns to add/sub with the constant
3192 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3193 old reload worked with this assumption. If the assumption
3194 becomes wrong, we should use approach in function
3195 base_plus_disp_to_reg. */
3196 if (in == value)
3198 /* See if we can directly increment INCLOC. */
3199 last = get_last_insn ();
3200 add_insn = emit_insn (plus_p
3201 ? gen_add2_insn (incloc, inc)
3202 : gen_sub2_insn (incloc, inc));
3204 code = recog_memoized (add_insn);
3205 if (code >= 0)
3207 if (! post && result != incloc)
3208 emit_insn (gen_move_insn (result, incloc));
3209 return result;
3211 delete_insns_since (last);
3214 /* If couldn't do the increment directly, must increment in RESULT.
3215 The way we do this depends on whether this is pre- or
3216 post-increment. For pre-increment, copy INCLOC to the reload
3217 register, increment it there, then save back. */
3218 if (! post)
3220 if (real_in != result)
3221 emit_insn (gen_move_insn (result, real_in));
3222 if (plus_p)
3223 emit_insn (gen_add2_insn (result, inc));
3224 else
3225 emit_insn (gen_sub2_insn (result, inc));
3226 if (result != incloc)
3227 emit_insn (gen_move_insn (incloc, result));
3229 else
3231 /* Post-increment.
3233 Because this might be a jump insn or a compare, and because
3234 RESULT may not be available after the insn in an input
3235 reload, we must do the incrementing before the insn being
3236 reloaded for.
3238 We have already copied IN to RESULT. Increment the copy in
3239 RESULT, save that back, then decrement RESULT so it has
3240 the original value. */
3241 if (plus_p)
3242 emit_insn (gen_add2_insn (result, inc));
3243 else
3244 emit_insn (gen_sub2_insn (result, inc));
3245 emit_insn (gen_move_insn (incloc, result));
3246 /* Restore non-modified value for the result. We prefer this
3247 way because it does not require an additional hard
3248 register. */
3249 if (plus_p)
3251 if (CONST_INT_P (inc))
3252 emit_insn (gen_add2_insn (result,
3253 gen_int_mode (-INTVAL (inc),
3254 GET_MODE (result))));
3255 else
3256 emit_insn (gen_sub2_insn (result, inc));
3258 else
3259 emit_insn (gen_add2_insn (result, inc));
3261 return result;
3264 /* Return true if the current move insn does not need processing as we
3265 already know that it satisfies its constraints. */
3266 static bool
3267 simple_move_p (void)
3269 rtx dest, src;
3270 enum reg_class dclass, sclass;
3272 lra_assert (curr_insn_set != NULL_RTX);
3273 dest = SET_DEST (curr_insn_set);
3274 src = SET_SRC (curr_insn_set);
3275 return ((dclass = get_op_class (dest)) != NO_REGS
3276 && (sclass = get_op_class (src)) != NO_REGS
3277 /* The backend guarantees that register moves of cost 2
3278 never need reloads. */
3279 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3282 /* Swap operands NOP and NOP + 1. */
3283 static inline void
3284 swap_operands (int nop)
3286 machine_mode mode = curr_operand_mode[nop];
3287 curr_operand_mode[nop] = curr_operand_mode[nop + 1];
3288 curr_operand_mode[nop + 1] = mode;
3289 rtx x = *curr_id->operand_loc[nop];
3290 *curr_id->operand_loc[nop] = *curr_id->operand_loc[nop + 1];
3291 *curr_id->operand_loc[nop + 1] = x;
3292 /* Swap the duplicates too. */
3293 lra_update_dup (curr_id, nop);
3294 lra_update_dup (curr_id, nop + 1);
3297 /* Main entry point of the constraint code: search the body of the
3298 current insn to choose the best alternative. It is mimicking insn
3299 alternative cost calculation model of former reload pass. That is
3300 because machine descriptions were written to use this model. This
3301 model can be changed in future. Make commutative operand exchange
3302 if it is chosen.
3304 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3305 constraints. Return true if any change happened during function
3306 call.
3308 If CHECK_ONLY_P is true then don't do any transformation. Just
3309 check that the insn satisfies all constraints. If the insn does
3310 not satisfy any constraint, return true. */
3311 static bool
3312 curr_insn_transform (bool check_only_p)
3314 int i, j, k;
3315 int n_operands;
3316 int n_alternatives;
3317 int commutative;
3318 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3319 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3320 rtx_insn *before, *after;
3321 bool alt_p = false;
3322 /* Flag that the insn has been changed through a transformation. */
3323 bool change_p;
3324 bool sec_mem_p;
3325 #ifdef SECONDARY_MEMORY_NEEDED
3326 bool use_sec_mem_p;
3327 #endif
3328 int max_regno_before;
3329 int reused_alternative_num;
3331 curr_insn_set = single_set (curr_insn);
3332 if (curr_insn_set != NULL_RTX && simple_move_p ())
3333 return false;
3335 no_input_reloads_p = no_output_reloads_p = false;
3336 goal_alt_number = -1;
3337 change_p = sec_mem_p = false;
3338 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3339 reloads; neither are insns that SET cc0. Insns that use CC0 are
3340 not allowed to have any input reloads. */
3341 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3342 no_output_reloads_p = true;
3344 #ifdef HAVE_cc0
3345 if (reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3346 no_input_reloads_p = true;
3347 if (reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3348 no_output_reloads_p = true;
3349 #endif
3351 n_operands = curr_static_id->n_operands;
3352 n_alternatives = curr_static_id->n_alternatives;
3354 /* Just return "no reloads" if insn has no operands with
3355 constraints. */
3356 if (n_operands == 0 || n_alternatives == 0)
3357 return false;
3359 max_regno_before = max_reg_num ();
3361 for (i = 0; i < n_operands; i++)
3363 goal_alt_matched[i][0] = -1;
3364 goal_alt_matches[i] = -1;
3367 commutative = curr_static_id->commutative;
3369 /* Now see what we need for pseudos that didn't get hard regs or got
3370 the wrong kind of hard reg. For this, we must consider all the
3371 operands together against the register constraints. */
3373 best_losers = best_overall = INT_MAX;
3374 best_reload_sum = 0;
3376 curr_swapped = false;
3377 goal_alt_swapped = false;
3379 if (! check_only_p)
3380 /* Make equivalence substitution and memory subreg elimination
3381 before address processing because an address legitimacy can
3382 depend on memory mode. */
3383 for (i = 0; i < n_operands; i++)
3385 rtx op = *curr_id->operand_loc[i];
3386 rtx subst, old = op;
3387 bool op_change_p = false;
3389 if (GET_CODE (old) == SUBREG)
3390 old = SUBREG_REG (old);
3391 subst = get_equiv_with_elimination (old, curr_insn);
3392 if (subst != old)
3394 subst = copy_rtx (subst);
3395 lra_assert (REG_P (old));
3396 if (GET_CODE (op) == SUBREG)
3397 SUBREG_REG (op) = subst;
3398 else
3399 *curr_id->operand_loc[i] = subst;
3400 if (lra_dump_file != NULL)
3402 fprintf (lra_dump_file,
3403 "Changing pseudo %d in operand %i of insn %u on equiv ",
3404 REGNO (old), i, INSN_UID (curr_insn));
3405 dump_value_slim (lra_dump_file, subst, 1);
3406 fprintf (lra_dump_file, "\n");
3408 op_change_p = change_p = true;
3410 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3412 change_p = true;
3413 lra_update_dup (curr_id, i);
3417 /* Reload address registers and displacements. We do it before
3418 finding an alternative because of memory constraints. */
3419 before = after = NULL;
3420 for (i = 0; i < n_operands; i++)
3421 if (! curr_static_id->operand[i].is_operator
3422 && process_address (i, check_only_p, &before, &after))
3424 if (check_only_p)
3425 return true;
3426 change_p = true;
3427 lra_update_dup (curr_id, i);
3430 if (change_p)
3431 /* If we've changed the instruction then any alternative that
3432 we chose previously may no longer be valid. */
3433 lra_set_used_insn_alternative (curr_insn, -1);
3435 if (! check_only_p && curr_insn_set != NULL_RTX
3436 && check_and_process_move (&change_p, &sec_mem_p))
3437 return change_p;
3439 try_swapped:
3441 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3442 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3443 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3444 reused_alternative_num, INSN_UID (curr_insn));
3446 if (process_alt_operands (reused_alternative_num))
3447 alt_p = true;
3449 if (check_only_p)
3450 return ! alt_p || best_losers != 0;
3452 /* If insn is commutative (it's safe to exchange a certain pair of
3453 operands) then we need to try each alternative twice, the second
3454 time matching those two operands as if we had exchanged them. To
3455 do this, really exchange them in operands.
3457 If we have just tried the alternatives the second time, return
3458 operands to normal and drop through. */
3460 if (reused_alternative_num < 0 && commutative >= 0)
3462 curr_swapped = !curr_swapped;
3463 if (curr_swapped)
3465 swap_operands (commutative);
3466 goto try_swapped;
3468 else
3469 swap_operands (commutative);
3472 if (! alt_p && ! sec_mem_p)
3474 /* No alternative works with reloads?? */
3475 if (INSN_CODE (curr_insn) >= 0)
3476 fatal_insn ("unable to generate reloads for:", curr_insn);
3477 error_for_asm (curr_insn,
3478 "inconsistent operand constraints in an %<asm%>");
3479 /* Avoid further trouble with this insn. */
3480 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3481 lra_invalidate_insn_data (curr_insn);
3482 return true;
3485 /* If the best alternative is with operands 1 and 2 swapped, swap
3486 them. Update the operand numbers of any reloads already
3487 pushed. */
3489 if (goal_alt_swapped)
3491 if (lra_dump_file != NULL)
3492 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3493 INSN_UID (curr_insn));
3495 /* Swap the duplicates too. */
3496 swap_operands (commutative);
3497 change_p = true;
3500 #ifdef SECONDARY_MEMORY_NEEDED
3501 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3502 too conservatively. So we use the secondary memory only if there
3503 is no any alternative without reloads. */
3504 use_sec_mem_p = false;
3505 if (! alt_p)
3506 use_sec_mem_p = true;
3507 else if (sec_mem_p)
3509 for (i = 0; i < n_operands; i++)
3510 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3511 break;
3512 use_sec_mem_p = i < n_operands;
3515 if (use_sec_mem_p)
3517 rtx new_reg, src, dest, rld;
3518 machine_mode sec_mode, rld_mode;
3520 lra_assert (sec_mem_p);
3521 lra_assert (curr_static_id->operand[0].type == OP_OUT
3522 && curr_static_id->operand[1].type == OP_IN);
3523 dest = *curr_id->operand_loc[0];
3524 src = *curr_id->operand_loc[1];
3525 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3526 ? dest : src);
3527 rld_mode = GET_MODE (rld);
3528 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3529 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3530 #else
3531 sec_mode = rld_mode;
3532 #endif
3533 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3534 NO_REGS, "secondary");
3535 /* If the mode is changed, it should be wider. */
3536 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3537 if (sec_mode != rld_mode)
3539 /* If the target says specifically to use another mode for
3540 secondary memory moves we can not reuse the original
3541 insn. */
3542 after = emit_spill_move (false, new_reg, dest);
3543 lra_process_new_insns (curr_insn, NULL, after,
3544 "Inserting the sec. move");
3545 /* We may have non null BEFORE here (e.g. after address
3546 processing. */
3547 push_to_sequence (before);
3548 before = emit_spill_move (true, new_reg, src);
3549 emit_insn (before);
3550 before = get_insns ();
3551 end_sequence ();
3552 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3553 lra_set_insn_deleted (curr_insn);
3555 else if (dest == rld)
3557 *curr_id->operand_loc[0] = new_reg;
3558 after = emit_spill_move (false, new_reg, dest);
3559 lra_process_new_insns (curr_insn, NULL, after,
3560 "Inserting the sec. move");
3562 else
3564 *curr_id->operand_loc[1] = new_reg;
3565 /* See comments above. */
3566 push_to_sequence (before);
3567 before = emit_spill_move (true, new_reg, src);
3568 emit_insn (before);
3569 before = get_insns ();
3570 end_sequence ();
3571 lra_process_new_insns (curr_insn, before, NULL,
3572 "Inserting the sec. move");
3574 lra_update_insn_regno_info (curr_insn);
3575 return true;
3577 #endif
3579 lra_assert (goal_alt_number >= 0);
3580 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3582 if (lra_dump_file != NULL)
3584 const char *p;
3586 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3587 goal_alt_number, INSN_UID (curr_insn));
3588 for (i = 0; i < n_operands; i++)
3590 p = (curr_static_id->operand_alternative
3591 [goal_alt_number * n_operands + i].constraint);
3592 if (*p == '\0')
3593 continue;
3594 fprintf (lra_dump_file, " (%d) ", i);
3595 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3596 fputc (*p, lra_dump_file);
3598 if (INSN_CODE (curr_insn) >= 0
3599 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3600 fprintf (lra_dump_file, " {%s}", p);
3601 if (curr_id->sp_offset != 0)
3602 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3603 curr_id->sp_offset);
3604 fprintf (lra_dump_file, "\n");
3607 /* Right now, for any pair of operands I and J that are required to
3608 match, with J < I, goal_alt_matches[I] is J. Add I to
3609 goal_alt_matched[J]. */
3611 for (i = 0; i < n_operands; i++)
3612 if ((j = goal_alt_matches[i]) >= 0)
3614 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3616 /* We allow matching one output operand and several input
3617 operands. */
3618 lra_assert (k == 0
3619 || (curr_static_id->operand[j].type == OP_OUT
3620 && curr_static_id->operand[i].type == OP_IN
3621 && (curr_static_id->operand
3622 [goal_alt_matched[j][0]].type == OP_IN)));
3623 goal_alt_matched[j][k] = i;
3624 goal_alt_matched[j][k + 1] = -1;
3627 for (i = 0; i < n_operands; i++)
3628 goal_alt_win[i] |= goal_alt_match_win[i];
3630 /* Any constants that aren't allowed and can't be reloaded into
3631 registers are here changed into memory references. */
3632 for (i = 0; i < n_operands; i++)
3633 if (goal_alt_win[i])
3635 int regno;
3636 enum reg_class new_class;
3637 rtx reg = *curr_id->operand_loc[i];
3639 if (GET_CODE (reg) == SUBREG)
3640 reg = SUBREG_REG (reg);
3642 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3644 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3646 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3648 lra_assert (ok_p);
3649 lra_change_class (regno, new_class, " Change to", true);
3653 else
3655 const char *constraint;
3656 char c;
3657 rtx op = *curr_id->operand_loc[i];
3658 rtx subreg = NULL_RTX;
3659 machine_mode mode = curr_operand_mode[i];
3661 if (GET_CODE (op) == SUBREG)
3663 subreg = op;
3664 op = SUBREG_REG (op);
3665 mode = GET_MODE (op);
3668 if (CONST_POOL_OK_P (mode, op)
3669 && ((targetm.preferred_reload_class
3670 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3671 || no_input_reloads_p))
3673 rtx tem = force_const_mem (mode, op);
3675 change_p = true;
3676 if (subreg != NULL_RTX)
3677 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3679 *curr_id->operand_loc[i] = tem;
3680 lra_update_dup (curr_id, i);
3681 process_address (i, false, &before, &after);
3683 /* If the alternative accepts constant pool refs directly
3684 there will be no reload needed at all. */
3685 if (subreg != NULL_RTX)
3686 continue;
3687 /* Skip alternatives before the one requested. */
3688 constraint = (curr_static_id->operand_alternative
3689 [goal_alt_number * n_operands + i].constraint);
3690 for (;
3691 (c = *constraint) && c != ',' && c != '#';
3692 constraint += CONSTRAINT_LEN (c, constraint))
3694 enum constraint_num cn = lookup_constraint (constraint);
3695 if (insn_extra_memory_constraint (cn)
3696 && satisfies_memory_constraint_p (tem, cn))
3697 break;
3699 if (c == '\0' || c == ',' || c == '#')
3700 continue;
3702 goal_alt_win[i] = true;
3706 for (i = 0; i < n_operands; i++)
3708 int regno;
3709 bool optional_p = false;
3710 rtx old, new_reg;
3711 rtx op = *curr_id->operand_loc[i];
3713 if (goal_alt_win[i])
3715 if (goal_alt[i] == NO_REGS
3716 && REG_P (op)
3717 /* When we assign NO_REGS it means that we will not
3718 assign a hard register to the scratch pseudo by
3719 assigment pass and the scratch pseudo will be
3720 spilled. Spilled scratch pseudos are transformed
3721 back to scratches at the LRA end. */
3722 && lra_former_scratch_operand_p (curr_insn, i))
3724 int regno = REGNO (op);
3725 lra_change_class (regno, NO_REGS, " Change to", true);
3726 if (lra_get_regno_hard_regno (regno) >= 0)
3727 /* We don't have to mark all insn affected by the
3728 spilled pseudo as there is only one such insn, the
3729 current one. */
3730 reg_renumber[regno] = -1;
3732 /* We can do an optional reload. If the pseudo got a hard
3733 reg, we might improve the code through inheritance. If
3734 it does not get a hard register we coalesce memory/memory
3735 moves later. Ignore move insns to avoid cycling. */
3736 if (! lra_simple_p
3737 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3738 && goal_alt[i] != NO_REGS && REG_P (op)
3739 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3740 && regno < new_regno_start
3741 && ! lra_former_scratch_p (regno)
3742 && reg_renumber[regno] < 0
3743 /* Check that the optional reload pseudo will be able to
3744 hold given mode value. */
3745 && ! (prohibited_class_reg_set_mode_p
3746 (goal_alt[i], reg_class_contents[goal_alt[i]],
3747 PSEUDO_REGNO_MODE (regno)))
3748 && (curr_insn_set == NULL_RTX
3749 || !((REG_P (SET_SRC (curr_insn_set))
3750 || MEM_P (SET_SRC (curr_insn_set))
3751 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3752 && (REG_P (SET_DEST (curr_insn_set))
3753 || MEM_P (SET_DEST (curr_insn_set))
3754 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3755 optional_p = true;
3756 else
3757 continue;
3760 /* Operands that match previous ones have already been handled. */
3761 if (goal_alt_matches[i] >= 0)
3762 continue;
3764 /* We should not have an operand with a non-offsettable address
3765 appearing where an offsettable address will do. It also may
3766 be a case when the address should be special in other words
3767 not a general one (e.g. it needs no index reg). */
3768 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3770 enum reg_class rclass;
3771 rtx *loc = &XEXP (op, 0);
3772 enum rtx_code code = GET_CODE (*loc);
3774 push_to_sequence (before);
3775 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3776 MEM, SCRATCH);
3777 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3778 new_reg = emit_inc (rclass, *loc, *loc,
3779 /* This value does not matter for MODIFY. */
3780 GET_MODE_SIZE (GET_MODE (op)));
3781 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3782 "offsetable address", &new_reg))
3783 lra_emit_move (new_reg, *loc);
3784 before = get_insns ();
3785 end_sequence ();
3786 *loc = new_reg;
3787 lra_update_dup (curr_id, i);
3789 else if (goal_alt_matched[i][0] == -1)
3791 machine_mode mode;
3792 rtx reg, *loc;
3793 int hard_regno, byte;
3794 enum op_type type = curr_static_id->operand[i].type;
3796 loc = curr_id->operand_loc[i];
3797 mode = curr_operand_mode[i];
3798 if (GET_CODE (*loc) == SUBREG)
3800 reg = SUBREG_REG (*loc);
3801 byte = SUBREG_BYTE (*loc);
3802 if (REG_P (reg)
3803 /* Strict_low_part requires reload the register not
3804 the sub-register. */
3805 && (curr_static_id->operand[i].strict_low
3806 || (GET_MODE_SIZE (mode)
3807 <= GET_MODE_SIZE (GET_MODE (reg))
3808 && (hard_regno
3809 = get_try_hard_regno (REGNO (reg))) >= 0
3810 && (simplify_subreg_regno
3811 (hard_regno,
3812 GET_MODE (reg), byte, mode) < 0)
3813 && (goal_alt[i] == NO_REGS
3814 || (simplify_subreg_regno
3815 (ira_class_hard_regs[goal_alt[i]][0],
3816 GET_MODE (reg), byte, mode) >= 0)))))
3818 if (type == OP_OUT)
3819 type = OP_INOUT;
3820 loc = &SUBREG_REG (*loc);
3821 mode = GET_MODE (*loc);
3824 old = *loc;
3825 if (get_reload_reg (type, mode, old, goal_alt[i],
3826 loc != curr_id->operand_loc[i], "", &new_reg)
3827 && type != OP_OUT)
3829 push_to_sequence (before);
3830 lra_emit_move (new_reg, old);
3831 before = get_insns ();
3832 end_sequence ();
3834 *loc = new_reg;
3835 if (type != OP_IN
3836 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3838 start_sequence ();
3839 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3840 emit_insn (after);
3841 after = get_insns ();
3842 end_sequence ();
3843 *loc = new_reg;
3845 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3846 if (goal_alt_dont_inherit_ops[j] == i)
3848 lra_set_regno_unique_value (REGNO (new_reg));
3849 break;
3851 lra_update_dup (curr_id, i);
3853 else if (curr_static_id->operand[i].type == OP_IN
3854 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3855 == OP_OUT))
3857 /* generate reloads for input and matched outputs. */
3858 match_inputs[0] = i;
3859 match_inputs[1] = -1;
3860 match_reload (goal_alt_matched[i][0], match_inputs,
3861 goal_alt[i], &before, &after);
3863 else if (curr_static_id->operand[i].type == OP_OUT
3864 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3865 == OP_IN))
3866 /* Generate reloads for output and matched inputs. */
3867 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3868 else if (curr_static_id->operand[i].type == OP_IN
3869 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3870 == OP_IN))
3872 /* Generate reloads for matched inputs. */
3873 match_inputs[0] = i;
3874 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3875 match_inputs[j + 1] = k;
3876 match_inputs[j + 1] = -1;
3877 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3879 else
3880 /* We must generate code in any case when function
3881 process_alt_operands decides that it is possible. */
3882 gcc_unreachable ();
3883 if (optional_p)
3885 lra_assert (REG_P (op));
3886 regno = REGNO (op);
3887 op = *curr_id->operand_loc[i]; /* Substitution. */
3888 if (GET_CODE (op) == SUBREG)
3889 op = SUBREG_REG (op);
3890 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3891 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3892 lra_reg_info[REGNO (op)].restore_regno = regno;
3893 if (lra_dump_file != NULL)
3894 fprintf (lra_dump_file,
3895 " Making reload reg %d for reg %d optional\n",
3896 REGNO (op), regno);
3899 if (before != NULL_RTX || after != NULL_RTX
3900 || max_regno_before != max_reg_num ())
3901 change_p = true;
3902 if (change_p)
3904 lra_update_operator_dups (curr_id);
3905 /* Something changes -- process the insn. */
3906 lra_update_insn_regno_info (curr_insn);
3908 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3909 return change_p;
3912 /* Return true if INSN satisfies all constraints. In other words, no
3913 reload insns are needed. */
3914 bool
3915 lra_constrain_insn (rtx_insn *insn)
3917 int saved_new_regno_start = new_regno_start;
3918 int saved_new_insn_uid_start = new_insn_uid_start;
3919 bool change_p;
3921 curr_insn = insn;
3922 curr_id = lra_get_insn_recog_data (curr_insn);
3923 curr_static_id = curr_id->insn_static_data;
3924 new_insn_uid_start = get_max_uid ();
3925 new_regno_start = max_reg_num ();
3926 change_p = curr_insn_transform (true);
3927 new_regno_start = saved_new_regno_start;
3928 new_insn_uid_start = saved_new_insn_uid_start;
3929 return ! change_p;
3932 /* Return true if X is in LIST. */
3933 static bool
3934 in_list_p (rtx x, rtx list)
3936 for (; list != NULL_RTX; list = XEXP (list, 1))
3937 if (XEXP (list, 0) == x)
3938 return true;
3939 return false;
3942 /* Return true if X contains an allocatable hard register (if
3943 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3944 static bool
3945 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3947 int i, j;
3948 const char *fmt;
3949 enum rtx_code code;
3951 code = GET_CODE (x);
3952 if (REG_P (x))
3954 int regno = REGNO (x);
3955 HARD_REG_SET alloc_regs;
3957 if (hard_reg_p)
3959 if (regno >= FIRST_PSEUDO_REGISTER)
3960 regno = lra_get_regno_hard_regno (regno);
3961 if (regno < 0)
3962 return false;
3963 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3964 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3966 else
3968 if (regno < FIRST_PSEUDO_REGISTER)
3969 return false;
3970 if (! spilled_p)
3971 return true;
3972 return lra_get_regno_hard_regno (regno) < 0;
3975 fmt = GET_RTX_FORMAT (code);
3976 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3978 if (fmt[i] == 'e')
3980 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3981 return true;
3983 else if (fmt[i] == 'E')
3985 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3986 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3987 return true;
3990 return false;
3993 /* Return true if X contains a symbol reg. */
3994 static bool
3995 contains_symbol_ref_p (rtx x)
3997 int i, j;
3998 const char *fmt;
3999 enum rtx_code code;
4001 code = GET_CODE (x);
4002 if (code == SYMBOL_REF)
4003 return true;
4004 fmt = GET_RTX_FORMAT (code);
4005 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4007 if (fmt[i] == 'e')
4009 if (contains_symbol_ref_p (XEXP (x, i)))
4010 return true;
4012 else if (fmt[i] == 'E')
4014 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4015 if (contains_symbol_ref_p (XVECEXP (x, i, j)))
4016 return true;
4019 return false;
4022 /* Process all regs in location *LOC and change them on equivalent
4023 substitution. Return true if any change was done. */
4024 static bool
4025 loc_equivalence_change_p (rtx *loc)
4027 rtx subst, reg, x = *loc;
4028 bool result = false;
4029 enum rtx_code code = GET_CODE (x);
4030 const char *fmt;
4031 int i, j;
4033 if (code == SUBREG)
4035 reg = SUBREG_REG (x);
4036 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4037 && GET_MODE (subst) == VOIDmode)
4039 /* We cannot reload debug location. Simplify subreg here
4040 while we know the inner mode. */
4041 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4042 GET_MODE (reg), SUBREG_BYTE (x));
4043 return true;
4046 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4048 *loc = subst;
4049 return true;
4052 /* Scan all the operand sub-expressions. */
4053 fmt = GET_RTX_FORMAT (code);
4054 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4056 if (fmt[i] == 'e')
4057 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4058 else if (fmt[i] == 'E')
4059 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4060 result
4061 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4063 return result;
4066 /* Similar to loc_equivalence_change_p, but for use as
4067 simplify_replace_fn_rtx callback. DATA is insn for which the
4068 elimination is done. If it null we don't do the elimination. */
4069 static rtx
4070 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4072 if (!REG_P (loc))
4073 return NULL_RTX;
4075 rtx subst = (data == NULL
4076 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4077 if (subst != loc)
4078 return subst;
4080 return NULL_RTX;
4083 /* Maximum number of generated reload insns per an insn. It is for
4084 preventing this pass cycling in a bug case. */
4085 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4087 /* The current iteration number of this LRA pass. */
4088 int lra_constraint_iter;
4090 /* True if we substituted equiv which needs checking register
4091 allocation correctness because the equivalent value contains
4092 allocatable hard registers or when we restore multi-register
4093 pseudo. */
4094 bool lra_risky_transformations_p;
4096 /* Return true if REGNO is referenced in more than one block. */
4097 static bool
4098 multi_block_pseudo_p (int regno)
4100 basic_block bb = NULL;
4101 unsigned int uid;
4102 bitmap_iterator bi;
4104 if (regno < FIRST_PSEUDO_REGISTER)
4105 return false;
4107 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4108 if (bb == NULL)
4109 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4110 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4111 return true;
4112 return false;
4115 /* Return true if LIST contains a deleted insn. */
4116 static bool
4117 contains_deleted_insn_p (rtx_insn_list *list)
4119 for (; list != NULL_RTX; list = list->next ())
4120 if (NOTE_P (list->insn ())
4121 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4122 return true;
4123 return false;
4126 /* Return true if X contains a pseudo dying in INSN. */
4127 static bool
4128 dead_pseudo_p (rtx x, rtx insn)
4130 int i, j;
4131 const char *fmt;
4132 enum rtx_code code;
4134 if (REG_P (x))
4135 return (insn != NULL_RTX
4136 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4137 code = GET_CODE (x);
4138 fmt = GET_RTX_FORMAT (code);
4139 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4141 if (fmt[i] == 'e')
4143 if (dead_pseudo_p (XEXP (x, i), insn))
4144 return true;
4146 else if (fmt[i] == 'E')
4148 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4149 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4150 return true;
4153 return false;
4156 /* Return true if INSN contains a dying pseudo in INSN right hand
4157 side. */
4158 static bool
4159 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4161 rtx set = single_set (insn);
4163 gcc_assert (set != NULL);
4164 return dead_pseudo_p (SET_SRC (set), insn);
4167 /* Return true if any init insn of REGNO contains a dying pseudo in
4168 insn right hand side. */
4169 static bool
4170 init_insn_rhs_dead_pseudo_p (int regno)
4172 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4174 if (insns == NULL)
4175 return false;
4176 for (; insns != NULL_RTX; insns = insns->next ())
4177 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4178 return true;
4179 return false;
4182 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4183 reverse only if we have one init insn with given REGNO as a
4184 source. */
4185 static bool
4186 reverse_equiv_p (int regno)
4188 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4189 rtx set;
4191 if (insns == NULL)
4192 return false;
4193 if (! INSN_P (insns->insn ())
4194 || insns->next () != NULL)
4195 return false;
4196 if ((set = single_set (insns->insn ())) == NULL_RTX)
4197 return false;
4198 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4201 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4202 call this function only for non-reverse equivalence. */
4203 static bool
4204 contains_reloaded_insn_p (int regno)
4206 rtx set;
4207 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4209 for (; list != NULL; list = list->next ())
4210 if ((set = single_set (list->insn ())) == NULL_RTX
4211 || ! REG_P (SET_DEST (set))
4212 || (int) REGNO (SET_DEST (set)) != regno)
4213 return true;
4214 return false;
4217 /* Entry function of LRA constraint pass. Return true if the
4218 constraint pass did change the code. */
4219 bool
4220 lra_constraints (bool first_p)
4222 bool changed_p;
4223 int i, hard_regno, new_insns_num;
4224 unsigned int min_len, new_min_len, uid;
4225 rtx set, x, reg, dest_reg;
4226 basic_block last_bb;
4227 bitmap_head equiv_insn_bitmap;
4228 bitmap_iterator bi;
4230 lra_constraint_iter++;
4231 if (lra_dump_file != NULL)
4232 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4233 lra_constraint_iter);
4234 changed_p = false;
4235 if (pic_offset_table_rtx
4236 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4237 lra_risky_transformations_p = true;
4238 else
4239 lra_risky_transformations_p = false;
4240 new_insn_uid_start = get_max_uid ();
4241 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4242 /* Mark used hard regs for target stack size calulations. */
4243 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4244 if (lra_reg_info[i].nrefs != 0
4245 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4247 int j, nregs;
4249 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4250 for (j = 0; j < nregs; j++)
4251 df_set_regs_ever_live (hard_regno + j, true);
4253 /* Do elimination before the equivalence processing as we can spill
4254 some pseudos during elimination. */
4255 lra_eliminate (false, first_p);
4256 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4257 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4258 if (lra_reg_info[i].nrefs != 0)
4260 ira_reg_equiv[i].profitable_p = true;
4261 reg = regno_reg_rtx[i];
4262 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4264 bool pseudo_p = contains_reg_p (x, false, false);
4266 /* After RTL transformation, we can not guarantee that
4267 pseudo in the substitution was not reloaded which might
4268 make equivalence invalid. For example, in reverse
4269 equiv of p0
4271 p0 <- ...
4273 equiv_mem <- p0
4275 the memory address register was reloaded before the 2nd
4276 insn. */
4277 if ((! first_p && pseudo_p)
4278 /* We don't use DF for compilation speed sake. So it
4279 is problematic to update live info when we use an
4280 equivalence containing pseudos in more than one
4281 BB. */
4282 || (pseudo_p && multi_block_pseudo_p (i))
4283 /* If an init insn was deleted for some reason, cancel
4284 the equiv. We could update the equiv insns after
4285 transformations including an equiv insn deletion
4286 but it is not worthy as such cases are extremely
4287 rare. */
4288 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4289 /* If it is not a reverse equivalence, we check that a
4290 pseudo in rhs of the init insn is not dying in the
4291 insn. Otherwise, the live info at the beginning of
4292 the corresponding BB might be wrong after we
4293 removed the insn. When the equiv can be a
4294 constant, the right hand side of the init insn can
4295 be a pseudo. */
4296 || (! reverse_equiv_p (i)
4297 && (init_insn_rhs_dead_pseudo_p (i)
4298 /* If we reloaded the pseudo in an equivalence
4299 init insn, we can not remove the equiv init
4300 insns and the init insns might write into
4301 const memory in this case. */
4302 || contains_reloaded_insn_p (i)))
4303 /* Prevent access beyond equivalent memory for
4304 paradoxical subregs. */
4305 || (MEM_P (x)
4306 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4307 > GET_MODE_SIZE (GET_MODE (x))))
4308 || (pic_offset_table_rtx
4309 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4310 && (targetm.preferred_reload_class
4311 (x, lra_get_allocno_class (i)) == NO_REGS))
4312 || contains_symbol_ref_p (x))))
4313 ira_reg_equiv[i].defined_p = false;
4314 if (contains_reg_p (x, false, true))
4315 ira_reg_equiv[i].profitable_p = false;
4316 if (get_equiv (reg) != reg)
4317 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4320 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4321 update_equiv (i);
4322 /* We should add all insns containing pseudos which should be
4323 substituted by their equivalences. */
4324 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4325 lra_push_insn_by_uid (uid);
4326 min_len = lra_insn_stack_length ();
4327 new_insns_num = 0;
4328 last_bb = NULL;
4329 changed_p = false;
4330 while ((new_min_len = lra_insn_stack_length ()) != 0)
4332 curr_insn = lra_pop_insn ();
4333 --new_min_len;
4334 curr_bb = BLOCK_FOR_INSN (curr_insn);
4335 if (curr_bb != last_bb)
4337 last_bb = curr_bb;
4338 bb_reload_num = lra_curr_reload_num;
4340 if (min_len > new_min_len)
4342 min_len = new_min_len;
4343 new_insns_num = 0;
4345 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4346 internal_error
4347 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4348 MAX_RELOAD_INSNS_NUMBER);
4349 new_insns_num++;
4350 if (DEBUG_INSN_P (curr_insn))
4352 /* We need to check equivalence in debug insn and change
4353 pseudo to the equivalent value if necessary. */
4354 curr_id = lra_get_insn_recog_data (curr_insn);
4355 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4357 rtx old = *curr_id->operand_loc[0];
4358 *curr_id->operand_loc[0]
4359 = simplify_replace_fn_rtx (old, NULL_RTX,
4360 loc_equivalence_callback, curr_insn);
4361 if (old != *curr_id->operand_loc[0])
4363 lra_update_insn_regno_info (curr_insn);
4364 changed_p = true;
4368 else if (INSN_P (curr_insn))
4370 if ((set = single_set (curr_insn)) != NULL_RTX)
4372 dest_reg = SET_DEST (set);
4373 /* The equivalence pseudo could be set up as SUBREG in a
4374 case when it is a call restore insn in a mode
4375 different from the pseudo mode. */
4376 if (GET_CODE (dest_reg) == SUBREG)
4377 dest_reg = SUBREG_REG (dest_reg);
4378 if ((REG_P (dest_reg)
4379 && (x = get_equiv (dest_reg)) != dest_reg
4380 /* Remove insns which set up a pseudo whose value
4381 can not be changed. Such insns might be not in
4382 init_insns because we don't update equiv data
4383 during insn transformations.
4385 As an example, let suppose that a pseudo got
4386 hard register and on the 1st pass was not
4387 changed to equivalent constant. We generate an
4388 additional insn setting up the pseudo because of
4389 secondary memory movement. Then the pseudo is
4390 spilled and we use the equiv constant. In this
4391 case we should remove the additional insn and
4392 this insn is not init_insns list. */
4393 && (! MEM_P (x) || MEM_READONLY_P (x)
4394 /* Check that this is actually an insn setting
4395 up the equivalence. */
4396 || in_list_p (curr_insn,
4397 ira_reg_equiv
4398 [REGNO (dest_reg)].init_insns)))
4399 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4400 && in_list_p (curr_insn,
4401 ira_reg_equiv
4402 [REGNO (SET_SRC (set))].init_insns)))
4404 /* This is equiv init insn of pseudo which did not get a
4405 hard register -- remove the insn. */
4406 if (lra_dump_file != NULL)
4408 fprintf (lra_dump_file,
4409 " Removing equiv init insn %i (freq=%d)\n",
4410 INSN_UID (curr_insn),
4411 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4412 dump_insn_slim (lra_dump_file, curr_insn);
4414 if (contains_reg_p (x, true, false))
4415 lra_risky_transformations_p = true;
4416 lra_set_insn_deleted (curr_insn);
4417 continue;
4420 curr_id = lra_get_insn_recog_data (curr_insn);
4421 curr_static_id = curr_id->insn_static_data;
4422 init_curr_insn_input_reloads ();
4423 init_curr_operand_mode ();
4424 if (curr_insn_transform (false))
4425 changed_p = true;
4426 /* Check non-transformed insns too for equiv change as USE
4427 or CLOBBER don't need reloads but can contain pseudos
4428 being changed on their equivalences. */
4429 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4430 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4432 lra_update_insn_regno_info (curr_insn);
4433 changed_p = true;
4437 bitmap_clear (&equiv_insn_bitmap);
4438 /* If we used a new hard regno, changed_p should be true because the
4439 hard reg is assigned to a new pseudo. */
4440 #ifdef ENABLE_CHECKING
4441 if (! changed_p)
4443 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4444 if (lra_reg_info[i].nrefs != 0
4445 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4447 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4449 for (j = 0; j < nregs; j++)
4450 lra_assert (df_regs_ever_live_p (hard_regno + j));
4453 #endif
4454 return changed_p;
4457 /* Initiate the LRA constraint pass. It is done once per
4458 function. */
4459 void
4460 lra_constraints_init (void)
4464 /* Finalize the LRA constraint pass. It is done once per
4465 function. */
4466 void
4467 lra_constraints_finish (void)
4473 /* This page contains code to do inheritance/split
4474 transformations. */
4476 /* Number of reloads passed so far in current EBB. */
4477 static int reloads_num;
4479 /* Number of calls passed so far in current EBB. */
4480 static int calls_num;
4482 /* Current reload pseudo check for validity of elements in
4483 USAGE_INSNS. */
4484 static int curr_usage_insns_check;
4486 /* Info about last usage of registers in EBB to do inheritance/split
4487 transformation. Inheritance transformation is done from a spilled
4488 pseudo and split transformations from a hard register or a pseudo
4489 assigned to a hard register. */
4490 struct usage_insns
4492 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4493 value INSNS is valid. The insns is chain of optional debug insns
4494 and a finishing non-debug insn using the corresponding reg. The
4495 value is also used to mark the registers which are set up in the
4496 current insn. The negated insn uid is used for this. */
4497 int check;
4498 /* Value of global reloads_num at the last insn in INSNS. */
4499 int reloads_num;
4500 /* Value of global reloads_nums at the last insn in INSNS. */
4501 int calls_num;
4502 /* It can be true only for splitting. And it means that the restore
4503 insn should be put after insn given by the following member. */
4504 bool after_p;
4505 /* Next insns in the current EBB which use the original reg and the
4506 original reg value is not changed between the current insn and
4507 the next insns. In order words, e.g. for inheritance, if we need
4508 to use the original reg value again in the next insns we can try
4509 to use the value in a hard register from a reload insn of the
4510 current insn. */
4511 rtx insns;
4514 /* Map: regno -> corresponding pseudo usage insns. */
4515 static struct usage_insns *usage_insns;
4517 static void
4518 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4520 usage_insns[regno].check = curr_usage_insns_check;
4521 usage_insns[regno].insns = insn;
4522 usage_insns[regno].reloads_num = reloads_num;
4523 usage_insns[regno].calls_num = calls_num;
4524 usage_insns[regno].after_p = after_p;
4527 /* The function is used to form list REGNO usages which consists of
4528 optional debug insns finished by a non-debug insn using REGNO.
4529 RELOADS_NUM is current number of reload insns processed so far. */
4530 static void
4531 add_next_usage_insn (int regno, rtx insn, int reloads_num)
4533 rtx next_usage_insns;
4535 if (usage_insns[regno].check == curr_usage_insns_check
4536 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4537 && DEBUG_INSN_P (insn))
4539 /* Check that we did not add the debug insn yet. */
4540 if (next_usage_insns != insn
4541 && (GET_CODE (next_usage_insns) != INSN_LIST
4542 || XEXP (next_usage_insns, 0) != insn))
4543 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4544 next_usage_insns);
4546 else if (NONDEBUG_INSN_P (insn))
4547 setup_next_usage_insn (regno, insn, reloads_num, false);
4548 else
4549 usage_insns[regno].check = 0;
4552 /* Return first non-debug insn in list USAGE_INSNS. */
4553 static rtx_insn *
4554 skip_usage_debug_insns (rtx usage_insns)
4556 rtx insn;
4558 /* Skip debug insns. */
4559 for (insn = usage_insns;
4560 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4561 insn = XEXP (insn, 1))
4563 return safe_as_a <rtx_insn *> (insn);
4566 /* Return true if we need secondary memory moves for insn in
4567 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4568 into the insn. */
4569 static bool
4570 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4571 rtx usage_insns ATTRIBUTE_UNUSED)
4573 #ifndef SECONDARY_MEMORY_NEEDED
4574 return false;
4575 #else
4576 rtx_insn *insn;
4577 rtx set, dest;
4578 enum reg_class cl;
4580 if (inher_cl == ALL_REGS
4581 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4582 return false;
4583 lra_assert (INSN_P (insn));
4584 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4585 return false;
4586 dest = SET_DEST (set);
4587 if (! REG_P (dest))
4588 return false;
4589 lra_assert (inher_cl != NO_REGS);
4590 cl = get_reg_class (REGNO (dest));
4591 return (cl != NO_REGS && cl != ALL_REGS
4592 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4593 #endif
4596 /* Registers involved in inheritance/split in the current EBB
4597 (inheritance/split pseudos and original registers). */
4598 static bitmap_head check_only_regs;
4600 /* Do inheritance transformations for insn INSN, which defines (if
4601 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4602 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4603 form as the "insns" field of usage_insns. Return true if we
4604 succeed in such transformation.
4606 The transformations look like:
4608 p <- ... i <- ...
4609 ... p <- i (new insn)
4610 ... =>
4611 <- ... p ... <- ... i ...
4613 ... i <- p (new insn)
4614 <- ... p ... <- ... i ...
4615 ... =>
4616 <- ... p ... <- ... i ...
4617 where p is a spilled original pseudo and i is a new inheritance pseudo.
4620 The inheritance pseudo has the smallest class of two classes CL and
4621 class of ORIGINAL REGNO. */
4622 static bool
4623 inherit_reload_reg (bool def_p, int original_regno,
4624 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4626 if (optimize_function_for_size_p (cfun))
4627 return false;
4629 enum reg_class rclass = lra_get_allocno_class (original_regno);
4630 rtx original_reg = regno_reg_rtx[original_regno];
4631 rtx new_reg, usage_insn;
4632 rtx_insn *new_insns;
4634 lra_assert (! usage_insns[original_regno].after_p);
4635 if (lra_dump_file != NULL)
4636 fprintf (lra_dump_file,
4637 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4638 if (! ira_reg_classes_intersect_p[cl][rclass])
4640 if (lra_dump_file != NULL)
4642 fprintf (lra_dump_file,
4643 " Rejecting inheritance for %d "
4644 "because of disjoint classes %s and %s\n",
4645 original_regno, reg_class_names[cl],
4646 reg_class_names[rclass]);
4647 fprintf (lra_dump_file,
4648 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4650 return false;
4652 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4653 /* We don't use a subset of two classes because it can be
4654 NO_REGS. This transformation is still profitable in most
4655 cases even if the classes are not intersected as register
4656 move is probably cheaper than a memory load. */
4657 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4659 if (lra_dump_file != NULL)
4660 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4661 reg_class_names[cl], reg_class_names[rclass]);
4663 rclass = cl;
4665 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4667 /* Reject inheritance resulting in secondary memory moves.
4668 Otherwise, there is a danger in LRA cycling. Also such
4669 transformation will be unprofitable. */
4670 if (lra_dump_file != NULL)
4672 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4673 rtx set = single_set (insn);
4675 lra_assert (set != NULL_RTX);
4677 rtx dest = SET_DEST (set);
4679 lra_assert (REG_P (dest));
4680 fprintf (lra_dump_file,
4681 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4682 "as secondary mem is needed\n",
4683 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4684 original_regno, reg_class_names[rclass]);
4685 fprintf (lra_dump_file,
4686 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4688 return false;
4690 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4691 rclass, "inheritance");
4692 start_sequence ();
4693 if (def_p)
4694 lra_emit_move (original_reg, new_reg);
4695 else
4696 lra_emit_move (new_reg, original_reg);
4697 new_insns = get_insns ();
4698 end_sequence ();
4699 if (NEXT_INSN (new_insns) != NULL_RTX)
4701 if (lra_dump_file != NULL)
4703 fprintf (lra_dump_file,
4704 " Rejecting inheritance %d->%d "
4705 "as it results in 2 or more insns:\n",
4706 original_regno, REGNO (new_reg));
4707 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4708 fprintf (lra_dump_file,
4709 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4711 return false;
4713 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg);
4714 lra_update_insn_regno_info (insn);
4715 if (! def_p)
4716 /* We now have a new usage insn for original regno. */
4717 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4718 if (lra_dump_file != NULL)
4719 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4720 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4721 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4722 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4723 bitmap_set_bit (&check_only_regs, original_regno);
4724 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4725 if (def_p)
4726 lra_process_new_insns (insn, NULL, new_insns,
4727 "Add original<-inheritance");
4728 else
4729 lra_process_new_insns (insn, new_insns, NULL,
4730 "Add inheritance<-original");
4731 while (next_usage_insns != NULL_RTX)
4733 if (GET_CODE (next_usage_insns) != INSN_LIST)
4735 usage_insn = next_usage_insns;
4736 lra_assert (NONDEBUG_INSN_P (usage_insn));
4737 next_usage_insns = NULL;
4739 else
4741 usage_insn = XEXP (next_usage_insns, 0);
4742 lra_assert (DEBUG_INSN_P (usage_insn));
4743 next_usage_insns = XEXP (next_usage_insns, 1);
4745 lra_substitute_pseudo (&usage_insn, original_regno, new_reg);
4746 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4747 if (lra_dump_file != NULL)
4749 fprintf (lra_dump_file,
4750 " Inheritance reuse change %d->%d (bb%d):\n",
4751 original_regno, REGNO (new_reg),
4752 BLOCK_FOR_INSN (usage_insn)->index);
4753 dump_insn_slim (lra_dump_file, usage_insn);
4756 if (lra_dump_file != NULL)
4757 fprintf (lra_dump_file,
4758 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4759 return true;
4762 /* Return true if we need a caller save/restore for pseudo REGNO which
4763 was assigned to a hard register. */
4764 static inline bool
4765 need_for_call_save_p (int regno)
4767 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4768 return (usage_insns[regno].calls_num < calls_num
4769 && (overlaps_hard_reg_set_p
4770 ((flag_ipa_ra &&
4771 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4772 ? lra_reg_info[regno].actual_call_used_reg_set
4773 : call_used_reg_set,
4774 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4775 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4776 PSEUDO_REGNO_MODE (regno))));
4779 /* Global registers occurring in the current EBB. */
4780 static bitmap_head ebb_global_regs;
4782 /* Return true if we need a split for hard register REGNO or pseudo
4783 REGNO which was assigned to a hard register.
4784 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4785 used for reloads since the EBB end. It is an approximation of the
4786 used hard registers in the split range. The exact value would
4787 require expensive calculations. If we were aggressive with
4788 splitting because of the approximation, the split pseudo will save
4789 the same hard register assignment and will be removed in the undo
4790 pass. We still need the approximation because too aggressive
4791 splitting would result in too inaccurate cost calculation in the
4792 assignment pass because of too many generated moves which will be
4793 probably removed in the undo pass. */
4794 static inline bool
4795 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4797 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4799 lra_assert (hard_regno >= 0);
4800 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4801 /* Don't split eliminable hard registers, otherwise we can
4802 split hard registers like hard frame pointer, which
4803 lives on BB start/end according to DF-infrastructure,
4804 when there is a pseudo assigned to the register and
4805 living in the same BB. */
4806 && (regno >= FIRST_PSEUDO_REGISTER
4807 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4808 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4809 /* Don't split call clobbered hard regs living through
4810 calls, otherwise we might have a check problem in the
4811 assign sub-pass as in the most cases (exception is a
4812 situation when lra_risky_transformations_p value is
4813 true) the assign pass assumes that all pseudos living
4814 through calls are assigned to call saved hard regs. */
4815 && (regno >= FIRST_PSEUDO_REGISTER
4816 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4817 || usage_insns[regno].calls_num == calls_num)
4818 /* We need at least 2 reloads to make pseudo splitting
4819 profitable. We should provide hard regno splitting in
4820 any case to solve 1st insn scheduling problem when
4821 moving hard register definition up might result in
4822 impossibility to find hard register for reload pseudo of
4823 small register class. */
4824 && (usage_insns[regno].reloads_num
4825 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4826 && (regno < FIRST_PSEUDO_REGISTER
4827 /* For short living pseudos, spilling + inheritance can
4828 be considered a substitution for splitting.
4829 Therefore we do not splitting for local pseudos. It
4830 decreases also aggressiveness of splitting. The
4831 minimal number of references is chosen taking into
4832 account that for 2 references splitting has no sense
4833 as we can just spill the pseudo. */
4834 || (regno >= FIRST_PSEUDO_REGISTER
4835 && lra_reg_info[regno].nrefs > 3
4836 && bitmap_bit_p (&ebb_global_regs, regno))))
4837 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4840 /* Return class for the split pseudo created from original pseudo with
4841 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4842 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4843 results in no secondary memory movements. */
4844 static enum reg_class
4845 choose_split_class (enum reg_class allocno_class,
4846 int hard_regno ATTRIBUTE_UNUSED,
4847 machine_mode mode ATTRIBUTE_UNUSED)
4849 #ifndef SECONDARY_MEMORY_NEEDED
4850 return allocno_class;
4851 #else
4852 int i;
4853 enum reg_class cl, best_cl = NO_REGS;
4854 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4855 = REGNO_REG_CLASS (hard_regno);
4857 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4858 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4859 return allocno_class;
4860 for (i = 0;
4861 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4862 i++)
4863 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4864 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4865 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4866 && (best_cl == NO_REGS
4867 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4868 best_cl = cl;
4869 return best_cl;
4870 #endif
4873 /* Do split transformations for insn INSN, which defines or uses
4874 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4875 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4876 "insns" field of usage_insns.
4878 The transformations look like:
4880 p <- ... p <- ...
4881 ... s <- p (new insn -- save)
4882 ... =>
4883 ... p <- s (new insn -- restore)
4884 <- ... p ... <- ... p ...
4886 <- ... p ... <- ... p ...
4887 ... s <- p (new insn -- save)
4888 ... =>
4889 ... p <- s (new insn -- restore)
4890 <- ... p ... <- ... p ...
4892 where p is an original pseudo got a hard register or a hard
4893 register and s is a new split pseudo. The save is put before INSN
4894 if BEFORE_P is true. Return true if we succeed in such
4895 transformation. */
4896 static bool
4897 split_reg (bool before_p, int original_regno, rtx_insn *insn,
4898 rtx next_usage_insns)
4900 enum reg_class rclass;
4901 rtx original_reg;
4902 int hard_regno, nregs;
4903 rtx new_reg, usage_insn;
4904 rtx_insn *restore, *save;
4905 bool after_p;
4906 bool call_save_p;
4908 if (original_regno < FIRST_PSEUDO_REGISTER)
4910 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4911 hard_regno = original_regno;
4912 call_save_p = false;
4913 nregs = 1;
4915 else
4917 hard_regno = reg_renumber[original_regno];
4918 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4919 rclass = lra_get_allocno_class (original_regno);
4920 original_reg = regno_reg_rtx[original_regno];
4921 call_save_p = need_for_call_save_p (original_regno);
4923 original_reg = regno_reg_rtx[original_regno];
4924 lra_assert (hard_regno >= 0);
4925 if (lra_dump_file != NULL)
4926 fprintf (lra_dump_file,
4927 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4928 if (call_save_p)
4930 machine_mode mode = GET_MODE (original_reg);
4932 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4933 hard_regno_nregs[hard_regno][mode],
4934 mode);
4935 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4937 else
4939 rclass = choose_split_class (rclass, hard_regno,
4940 GET_MODE (original_reg));
4941 if (rclass == NO_REGS)
4943 if (lra_dump_file != NULL)
4945 fprintf (lra_dump_file,
4946 " Rejecting split of %d(%s): "
4947 "no good reg class for %d(%s)\n",
4948 original_regno,
4949 reg_class_names[lra_get_allocno_class (original_regno)],
4950 hard_regno,
4951 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4952 fprintf
4953 (lra_dump_file,
4954 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4956 return false;
4958 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4959 rclass, "split");
4960 reg_renumber[REGNO (new_reg)] = hard_regno;
4962 save = emit_spill_move (true, new_reg, original_reg);
4963 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
4965 if (lra_dump_file != NULL)
4967 fprintf
4968 (lra_dump_file,
4969 " Rejecting split %d->%d resulting in > 2 save insns:\n",
4970 original_regno, REGNO (new_reg));
4971 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
4972 fprintf (lra_dump_file,
4973 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4975 return false;
4977 restore = emit_spill_move (false, new_reg, original_reg);
4978 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
4980 if (lra_dump_file != NULL)
4982 fprintf (lra_dump_file,
4983 " Rejecting split %d->%d "
4984 "resulting in > 2 restore insns:\n",
4985 original_regno, REGNO (new_reg));
4986 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
4987 fprintf (lra_dump_file,
4988 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4990 return false;
4992 after_p = usage_insns[original_regno].after_p;
4993 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4994 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4995 bitmap_set_bit (&check_only_regs, original_regno);
4996 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4997 for (;;)
4999 if (GET_CODE (next_usage_insns) != INSN_LIST)
5001 usage_insn = next_usage_insns;
5002 break;
5004 usage_insn = XEXP (next_usage_insns, 0);
5005 lra_assert (DEBUG_INSN_P (usage_insn));
5006 next_usage_insns = XEXP (next_usage_insns, 1);
5007 lra_substitute_pseudo (&usage_insn, original_regno, new_reg);
5008 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5009 if (lra_dump_file != NULL)
5011 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5012 original_regno, REGNO (new_reg));
5013 dump_insn_slim (lra_dump_file, usage_insn);
5016 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5017 lra_assert (usage_insn != insn || (after_p && before_p));
5018 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5019 after_p ? NULL : restore,
5020 after_p ? restore : NULL,
5021 call_save_p
5022 ? "Add reg<-save" : "Add reg<-split");
5023 lra_process_new_insns (insn, before_p ? save : NULL,
5024 before_p ? NULL : save,
5025 call_save_p
5026 ? "Add save<-reg" : "Add split<-reg");
5027 if (nregs > 1)
5028 /* If we are trying to split multi-register. We should check
5029 conflicts on the next assignment sub-pass. IRA can allocate on
5030 sub-register levels, LRA do this on pseudos level right now and
5031 this discrepancy may create allocation conflicts after
5032 splitting. */
5033 lra_risky_transformations_p = true;
5034 if (lra_dump_file != NULL)
5035 fprintf (lra_dump_file,
5036 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5037 return true;
5040 /* Recognize that we need a split transformation for insn INSN, which
5041 defines or uses REGNO in its insn biggest MODE (we use it only if
5042 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5043 hard registers which might be used for reloads since the EBB end.
5044 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5045 uid before starting INSN processing. Return true if we succeed in
5046 such transformation. */
5047 static bool
5048 split_if_necessary (int regno, machine_mode mode,
5049 HARD_REG_SET potential_reload_hard_regs,
5050 bool before_p, rtx_insn *insn, int max_uid)
5052 bool res = false;
5053 int i, nregs = 1;
5054 rtx next_usage_insns;
5056 if (regno < FIRST_PSEUDO_REGISTER)
5057 nregs = hard_regno_nregs[regno][mode];
5058 for (i = 0; i < nregs; i++)
5059 if (usage_insns[regno + i].check == curr_usage_insns_check
5060 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5061 /* To avoid processing the register twice or more. */
5062 && ((GET_CODE (next_usage_insns) != INSN_LIST
5063 && INSN_UID (next_usage_insns) < max_uid)
5064 || (GET_CODE (next_usage_insns) == INSN_LIST
5065 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5066 && need_for_split_p (potential_reload_hard_regs, regno + i)
5067 && split_reg (before_p, regno + i, insn, next_usage_insns))
5068 res = true;
5069 return res;
5072 /* Check only registers living at the current program point in the
5073 current EBB. */
5074 static bitmap_head live_regs;
5076 /* Update live info in EBB given by its HEAD and TAIL insns after
5077 inheritance/split transformation. The function removes dead moves
5078 too. */
5079 static void
5080 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5082 unsigned int j;
5083 int i, regno;
5084 bool live_p;
5085 rtx_insn *prev_insn;
5086 rtx set;
5087 bool remove_p;
5088 basic_block last_bb, prev_bb, curr_bb;
5089 bitmap_iterator bi;
5090 struct lra_insn_reg *reg;
5091 edge e;
5092 edge_iterator ei;
5094 last_bb = BLOCK_FOR_INSN (tail);
5095 prev_bb = NULL;
5096 for (curr_insn = tail;
5097 curr_insn != PREV_INSN (head);
5098 curr_insn = prev_insn)
5100 prev_insn = PREV_INSN (curr_insn);
5101 /* We need to process empty blocks too. They contain
5102 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5103 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5104 continue;
5105 curr_bb = BLOCK_FOR_INSN (curr_insn);
5106 if (curr_bb != prev_bb)
5108 if (prev_bb != NULL)
5110 /* Update df_get_live_in (prev_bb): */
5111 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5112 if (bitmap_bit_p (&live_regs, j))
5113 bitmap_set_bit (df_get_live_in (prev_bb), j);
5114 else
5115 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5117 if (curr_bb != last_bb)
5119 /* Update df_get_live_out (curr_bb): */
5120 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5122 live_p = bitmap_bit_p (&live_regs, j);
5123 if (! live_p)
5124 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5125 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5127 live_p = true;
5128 break;
5130 if (live_p)
5131 bitmap_set_bit (df_get_live_out (curr_bb), j);
5132 else
5133 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5136 prev_bb = curr_bb;
5137 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5139 if (! NONDEBUG_INSN_P (curr_insn))
5140 continue;
5141 curr_id = lra_get_insn_recog_data (curr_insn);
5142 curr_static_id = curr_id->insn_static_data;
5143 remove_p = false;
5144 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
5145 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5146 && bitmap_bit_p (&check_only_regs, regno)
5147 && ! bitmap_bit_p (&live_regs, regno))
5148 remove_p = true;
5149 /* See which defined values die here. */
5150 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5151 if (reg->type == OP_OUT && ! reg->subreg_p)
5152 bitmap_clear_bit (&live_regs, reg->regno);
5153 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5154 if (reg->type == OP_OUT && ! reg->subreg_p)
5155 bitmap_clear_bit (&live_regs, reg->regno);
5156 /* Mark each used value as live. */
5157 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5158 if (reg->type != OP_OUT
5159 && bitmap_bit_p (&check_only_regs, reg->regno))
5160 bitmap_set_bit (&live_regs, reg->regno);
5161 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5162 if (reg->type != OP_OUT
5163 && bitmap_bit_p (&check_only_regs, reg->regno))
5164 bitmap_set_bit (&live_regs, reg->regno);
5165 if (curr_id->arg_hard_regs != NULL)
5166 /* Make argument hard registers live. */
5167 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5168 if (bitmap_bit_p (&check_only_regs, regno))
5169 bitmap_set_bit (&live_regs, regno);
5170 /* It is quite important to remove dead move insns because it
5171 means removing dead store. We don't need to process them for
5172 constraints. */
5173 if (remove_p)
5175 if (lra_dump_file != NULL)
5177 fprintf (lra_dump_file, " Removing dead insn:\n ");
5178 dump_insn_slim (lra_dump_file, curr_insn);
5180 lra_set_insn_deleted (curr_insn);
5185 /* The structure describes info to do an inheritance for the current
5186 insn. We need to collect such info first before doing the
5187 transformations because the transformations change the insn
5188 internal representation. */
5189 struct to_inherit
5191 /* Original regno. */
5192 int regno;
5193 /* Subsequent insns which can inherit original reg value. */
5194 rtx insns;
5197 /* Array containing all info for doing inheritance from the current
5198 insn. */
5199 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5201 /* Number elements in the previous array. */
5202 static int to_inherit_num;
5204 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5205 structure to_inherit. */
5206 static void
5207 add_to_inherit (int regno, rtx insns)
5209 int i;
5211 for (i = 0; i < to_inherit_num; i++)
5212 if (to_inherit[i].regno == regno)
5213 return;
5214 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5215 to_inherit[to_inherit_num].regno = regno;
5216 to_inherit[to_inherit_num++].insns = insns;
5219 /* Return the last non-debug insn in basic block BB, or the block begin
5220 note if none. */
5221 static rtx_insn *
5222 get_last_insertion_point (basic_block bb)
5224 rtx_insn *insn;
5226 FOR_BB_INSNS_REVERSE (bb, insn)
5227 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5228 return insn;
5229 gcc_unreachable ();
5232 /* Set up RES by registers living on edges FROM except the edge (FROM,
5233 TO) or by registers set up in a jump insn in BB FROM. */
5234 static void
5235 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5237 rtx_insn *last;
5238 struct lra_insn_reg *reg;
5239 edge e;
5240 edge_iterator ei;
5242 lra_assert (to != NULL);
5243 bitmap_clear (res);
5244 FOR_EACH_EDGE (e, ei, from->succs)
5245 if (e->dest != to)
5246 bitmap_ior_into (res, df_get_live_in (e->dest));
5247 last = get_last_insertion_point (from);
5248 if (! JUMP_P (last))
5249 return;
5250 curr_id = lra_get_insn_recog_data (last);
5251 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5252 if (reg->type != OP_IN)
5253 bitmap_set_bit (res, reg->regno);
5256 /* Used as a temporary results of some bitmap calculations. */
5257 static bitmap_head temp_bitmap;
5259 /* We split for reloads of small class of hard regs. The following
5260 defines how many hard regs the class should have to be qualified as
5261 small. The code is mostly oriented to x86/x86-64 architecture
5262 where some insns need to use only specific register or pair of
5263 registers and these register can live in RTL explicitly, e.g. for
5264 parameter passing. */
5265 static const int max_small_class_regs_num = 2;
5267 /* Do inheritance/split transformations in EBB starting with HEAD and
5268 finishing on TAIL. We process EBB insns in the reverse order.
5269 Return true if we did any inheritance/split transformation in the
5270 EBB.
5272 We should avoid excessive splitting which results in worse code
5273 because of inaccurate cost calculations for spilling new split
5274 pseudos in such case. To achieve this we do splitting only if
5275 register pressure is high in given basic block and there are reload
5276 pseudos requiring hard registers. We could do more register
5277 pressure calculations at any given program point to avoid necessary
5278 splitting even more but it is to expensive and the current approach
5279 works well enough. */
5280 static bool
5281 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5283 int i, src_regno, dst_regno, nregs;
5284 bool change_p, succ_p, update_reloads_num_p;
5285 rtx_insn *prev_insn, *last_insn;
5286 rtx next_usage_insns, set;
5287 enum reg_class cl;
5288 struct lra_insn_reg *reg;
5289 basic_block last_processed_bb, curr_bb = NULL;
5290 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5291 bitmap to_process;
5292 unsigned int j;
5293 bitmap_iterator bi;
5294 bool head_p, after_p;
5296 change_p = false;
5297 curr_usage_insns_check++;
5298 reloads_num = calls_num = 0;
5299 bitmap_clear (&check_only_regs);
5300 last_processed_bb = NULL;
5301 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5302 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5303 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5304 /* We don't process new insns generated in the loop. */
5305 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5307 prev_insn = PREV_INSN (curr_insn);
5308 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5309 curr_bb = BLOCK_FOR_INSN (curr_insn);
5310 if (last_processed_bb != curr_bb)
5312 /* We are at the end of BB. Add qualified living
5313 pseudos for potential splitting. */
5314 to_process = df_get_live_out (curr_bb);
5315 if (last_processed_bb != NULL)
5317 /* We are somewhere in the middle of EBB. */
5318 get_live_on_other_edges (curr_bb, last_processed_bb,
5319 &temp_bitmap);
5320 to_process = &temp_bitmap;
5322 last_processed_bb = curr_bb;
5323 last_insn = get_last_insertion_point (curr_bb);
5324 after_p = (! JUMP_P (last_insn)
5325 && (! CALL_P (last_insn)
5326 || (find_reg_note (last_insn,
5327 REG_NORETURN, NULL_RTX) == NULL_RTX
5328 && ! SIBLING_CALL_P (last_insn))));
5329 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5330 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5332 if ((int) j >= lra_constraint_new_regno_start)
5333 break;
5334 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5336 if (j < FIRST_PSEUDO_REGISTER)
5337 SET_HARD_REG_BIT (live_hard_regs, j);
5338 else
5339 add_to_hard_reg_set (&live_hard_regs,
5340 PSEUDO_REGNO_MODE (j),
5341 reg_renumber[j]);
5342 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5346 src_regno = dst_regno = -1;
5347 if (NONDEBUG_INSN_P (curr_insn)
5348 && (set = single_set (curr_insn)) != NULL_RTX
5349 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5351 src_regno = REGNO (SET_SRC (set));
5352 dst_regno = REGNO (SET_DEST (set));
5354 update_reloads_num_p = true;
5355 if (src_regno < lra_constraint_new_regno_start
5356 && src_regno >= FIRST_PSEUDO_REGISTER
5357 && reg_renumber[src_regno] < 0
5358 && dst_regno >= lra_constraint_new_regno_start
5359 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5361 /* 'reload_pseudo <- original_pseudo'. */
5362 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5363 reloads_num++;
5364 update_reloads_num_p = false;
5365 succ_p = false;
5366 if (usage_insns[src_regno].check == curr_usage_insns_check
5367 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5368 succ_p = inherit_reload_reg (false, src_regno, cl,
5369 curr_insn, next_usage_insns);
5370 if (succ_p)
5371 change_p = true;
5372 else
5373 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5374 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5375 IOR_HARD_REG_SET (potential_reload_hard_regs,
5376 reg_class_contents[cl]);
5378 else if (src_regno >= lra_constraint_new_regno_start
5379 && dst_regno < lra_constraint_new_regno_start
5380 && dst_regno >= FIRST_PSEUDO_REGISTER
5381 && reg_renumber[dst_regno] < 0
5382 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5383 && usage_insns[dst_regno].check == curr_usage_insns_check
5384 && (next_usage_insns
5385 = usage_insns[dst_regno].insns) != NULL_RTX)
5387 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5388 reloads_num++;
5389 update_reloads_num_p = false;
5390 /* 'original_pseudo <- reload_pseudo'. */
5391 if (! JUMP_P (curr_insn)
5392 && inherit_reload_reg (true, dst_regno, cl,
5393 curr_insn, next_usage_insns))
5394 change_p = true;
5395 /* Invalidate. */
5396 usage_insns[dst_regno].check = 0;
5397 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5398 IOR_HARD_REG_SET (potential_reload_hard_regs,
5399 reg_class_contents[cl]);
5401 else if (INSN_P (curr_insn))
5403 int iter;
5404 int max_uid = get_max_uid ();
5406 curr_id = lra_get_insn_recog_data (curr_insn);
5407 curr_static_id = curr_id->insn_static_data;
5408 to_inherit_num = 0;
5409 /* Process insn definitions. */
5410 for (iter = 0; iter < 2; iter++)
5411 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5412 reg != NULL;
5413 reg = reg->next)
5414 if (reg->type != OP_IN
5415 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5417 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5418 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5419 && usage_insns[dst_regno].check == curr_usage_insns_check
5420 && (next_usage_insns
5421 = usage_insns[dst_regno].insns) != NULL_RTX)
5423 struct lra_insn_reg *r;
5425 for (r = curr_id->regs; r != NULL; r = r->next)
5426 if (r->type != OP_OUT && r->regno == dst_regno)
5427 break;
5428 /* Don't do inheritance if the pseudo is also
5429 used in the insn. */
5430 if (r == NULL)
5431 /* We can not do inheritance right now
5432 because the current insn reg info (chain
5433 regs) can change after that. */
5434 add_to_inherit (dst_regno, next_usage_insns);
5436 /* We can not process one reg twice here because of
5437 usage_insns invalidation. */
5438 if ((dst_regno < FIRST_PSEUDO_REGISTER
5439 || reg_renumber[dst_regno] >= 0)
5440 && ! reg->subreg_p && reg->type != OP_IN)
5442 HARD_REG_SET s;
5444 if (split_if_necessary (dst_regno, reg->biggest_mode,
5445 potential_reload_hard_regs,
5446 false, curr_insn, max_uid))
5447 change_p = true;
5448 CLEAR_HARD_REG_SET (s);
5449 if (dst_regno < FIRST_PSEUDO_REGISTER)
5450 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5451 else
5452 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5453 reg_renumber[dst_regno]);
5454 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5456 /* We should invalidate potential inheritance or
5457 splitting for the current insn usages to the next
5458 usage insns (see code below) as the output pseudo
5459 prevents this. */
5460 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5461 && reg_renumber[dst_regno] < 0)
5462 || (reg->type == OP_OUT && ! reg->subreg_p
5463 && (dst_regno < FIRST_PSEUDO_REGISTER
5464 || reg_renumber[dst_regno] >= 0)))
5466 /* Invalidate and mark definitions. */
5467 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5468 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5469 else
5471 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5472 for (i = 0; i < nregs; i++)
5473 usage_insns[dst_regno + i].check
5474 = -(int) INSN_UID (curr_insn);
5478 if (! JUMP_P (curr_insn))
5479 for (i = 0; i < to_inherit_num; i++)
5480 if (inherit_reload_reg (true, to_inherit[i].regno,
5481 ALL_REGS, curr_insn,
5482 to_inherit[i].insns))
5483 change_p = true;
5484 if (CALL_P (curr_insn))
5486 rtx cheap, pat, dest;
5487 rtx_insn *restore;
5488 int regno, hard_regno;
5490 calls_num++;
5491 if ((cheap = find_reg_note (curr_insn,
5492 REG_RETURNED, NULL_RTX)) != NULL_RTX
5493 && ((cheap = XEXP (cheap, 0)), true)
5494 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5495 && (hard_regno = reg_renumber[regno]) >= 0
5496 /* If there are pending saves/restores, the
5497 optimization is not worth. */
5498 && usage_insns[regno].calls_num == calls_num - 1
5499 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5501 /* Restore the pseudo from the call result as
5502 REG_RETURNED note says that the pseudo value is
5503 in the call result and the pseudo is an argument
5504 of the call. */
5505 pat = PATTERN (curr_insn);
5506 if (GET_CODE (pat) == PARALLEL)
5507 pat = XVECEXP (pat, 0, 0);
5508 dest = SET_DEST (pat);
5509 /* For multiple return values dest is PARALLEL.
5510 Currently we handle only single return value case. */
5511 if (REG_P (dest))
5513 start_sequence ();
5514 emit_move_insn (cheap, copy_rtx (dest));
5515 restore = get_insns ();
5516 end_sequence ();
5517 lra_process_new_insns (curr_insn, NULL, restore,
5518 "Inserting call parameter restore");
5519 /* We don't need to save/restore of the pseudo from
5520 this call. */
5521 usage_insns[regno].calls_num = calls_num;
5522 bitmap_set_bit (&check_only_regs, regno);
5526 to_inherit_num = 0;
5527 /* Process insn usages. */
5528 for (iter = 0; iter < 2; iter++)
5529 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5530 reg != NULL;
5531 reg = reg->next)
5532 if ((reg->type != OP_OUT
5533 || (reg->type == OP_OUT && reg->subreg_p))
5534 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5536 if (src_regno >= FIRST_PSEUDO_REGISTER
5537 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5539 if (usage_insns[src_regno].check == curr_usage_insns_check
5540 && (next_usage_insns
5541 = usage_insns[src_regno].insns) != NULL_RTX
5542 && NONDEBUG_INSN_P (curr_insn))
5543 add_to_inherit (src_regno, next_usage_insns);
5544 else if (usage_insns[src_regno].check
5545 != -(int) INSN_UID (curr_insn))
5546 /* Add usages but only if the reg is not set up
5547 in the same insn. */
5548 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5550 else if (src_regno < FIRST_PSEUDO_REGISTER
5551 || reg_renumber[src_regno] >= 0)
5553 bool before_p;
5554 rtx use_insn = curr_insn;
5556 before_p = (JUMP_P (curr_insn)
5557 || (CALL_P (curr_insn) && reg->type == OP_IN));
5558 if (NONDEBUG_INSN_P (curr_insn)
5559 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5560 && split_if_necessary (src_regno, reg->biggest_mode,
5561 potential_reload_hard_regs,
5562 before_p, curr_insn, max_uid))
5564 if (reg->subreg_p)
5565 lra_risky_transformations_p = true;
5566 change_p = true;
5567 /* Invalidate. */
5568 usage_insns[src_regno].check = 0;
5569 if (before_p)
5570 use_insn = PREV_INSN (curr_insn);
5572 if (NONDEBUG_INSN_P (curr_insn))
5574 if (src_regno < FIRST_PSEUDO_REGISTER)
5575 add_to_hard_reg_set (&live_hard_regs,
5576 reg->biggest_mode, src_regno);
5577 else
5578 add_to_hard_reg_set (&live_hard_regs,
5579 PSEUDO_REGNO_MODE (src_regno),
5580 reg_renumber[src_regno]);
5582 add_next_usage_insn (src_regno, use_insn, reloads_num);
5585 /* Process call args. */
5586 if (curr_id->arg_hard_regs != NULL)
5587 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5588 if (src_regno < FIRST_PSEUDO_REGISTER)
5590 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5591 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5593 for (i = 0; i < to_inherit_num; i++)
5595 src_regno = to_inherit[i].regno;
5596 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5597 curr_insn, to_inherit[i].insns))
5598 change_p = true;
5599 else
5600 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5603 if (update_reloads_num_p
5604 && NONDEBUG_INSN_P (curr_insn)
5605 && (set = single_set (curr_insn)) != NULL_RTX)
5607 int regno = -1;
5608 if ((REG_P (SET_DEST (set))
5609 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5610 && reg_renumber[regno] < 0
5611 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5612 || (REG_P (SET_SRC (set))
5613 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5614 && reg_renumber[regno] < 0
5615 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5617 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5618 reloads_num++;
5619 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5620 IOR_HARD_REG_SET (potential_reload_hard_regs,
5621 reg_class_contents[cl]);
5624 /* We reached the start of the current basic block. */
5625 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5626 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5628 /* We reached the beginning of the current block -- do
5629 rest of spliting in the current BB. */
5630 to_process = df_get_live_in (curr_bb);
5631 if (BLOCK_FOR_INSN (head) != curr_bb)
5633 /* We are somewhere in the middle of EBB. */
5634 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5635 curr_bb, &temp_bitmap);
5636 to_process = &temp_bitmap;
5638 head_p = true;
5639 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5641 if ((int) j >= lra_constraint_new_regno_start)
5642 break;
5643 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5644 && usage_insns[j].check == curr_usage_insns_check
5645 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5647 if (need_for_split_p (potential_reload_hard_regs, j))
5649 if (lra_dump_file != NULL && head_p)
5651 fprintf (lra_dump_file,
5652 " ----------------------------------\n");
5653 head_p = false;
5655 if (split_reg (false, j, bb_note (curr_bb),
5656 next_usage_insns))
5657 change_p = true;
5659 usage_insns[j].check = 0;
5664 return change_p;
5667 /* This value affects EBB forming. If probability of edge from EBB to
5668 a BB is not greater than the following value, we don't add the BB
5669 to EBB. */
5670 #define EBB_PROBABILITY_CUTOFF ((REG_BR_PROB_BASE * 50) / 100)
5672 /* Current number of inheritance/split iteration. */
5673 int lra_inheritance_iter;
5675 /* Entry function for inheritance/split pass. */
5676 void
5677 lra_inheritance (void)
5679 int i;
5680 basic_block bb, start_bb;
5681 edge e;
5683 lra_inheritance_iter++;
5684 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5685 return;
5686 timevar_push (TV_LRA_INHERITANCE);
5687 if (lra_dump_file != NULL)
5688 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5689 lra_inheritance_iter);
5690 curr_usage_insns_check = 0;
5691 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5692 for (i = 0; i < lra_constraint_new_regno_start; i++)
5693 usage_insns[i].check = 0;
5694 bitmap_initialize (&check_only_regs, &reg_obstack);
5695 bitmap_initialize (&live_regs, &reg_obstack);
5696 bitmap_initialize (&temp_bitmap, &reg_obstack);
5697 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5698 FOR_EACH_BB_FN (bb, cfun)
5700 start_bb = bb;
5701 if (lra_dump_file != NULL)
5702 fprintf (lra_dump_file, "EBB");
5703 /* Form a EBB starting with BB. */
5704 bitmap_clear (&ebb_global_regs);
5705 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5706 for (;;)
5708 if (lra_dump_file != NULL)
5709 fprintf (lra_dump_file, " %d", bb->index);
5710 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5711 || LABEL_P (BB_HEAD (bb->next_bb)))
5712 break;
5713 e = find_fallthru_edge (bb->succs);
5714 if (! e)
5715 break;
5716 if (e->probability <= EBB_PROBABILITY_CUTOFF)
5717 break;
5718 bb = bb->next_bb;
5720 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5721 if (lra_dump_file != NULL)
5722 fprintf (lra_dump_file, "\n");
5723 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5724 /* Remember that the EBB head and tail can change in
5725 inherit_in_ebb. */
5726 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5728 bitmap_clear (&ebb_global_regs);
5729 bitmap_clear (&temp_bitmap);
5730 bitmap_clear (&live_regs);
5731 bitmap_clear (&check_only_regs);
5732 free (usage_insns);
5734 timevar_pop (TV_LRA_INHERITANCE);
5739 /* This page contains code to undo failed inheritance/split
5740 transformations. */
5742 /* Current number of iteration undoing inheritance/split. */
5743 int lra_undo_inheritance_iter;
5745 /* Fix BB live info LIVE after removing pseudos created on pass doing
5746 inheritance/split which are REMOVED_PSEUDOS. */
5747 static void
5748 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5750 unsigned int regno;
5751 bitmap_iterator bi;
5753 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5754 if (bitmap_clear_bit (live, regno))
5755 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5758 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5759 number. */
5760 static int
5761 get_regno (rtx reg)
5763 if (GET_CODE (reg) == SUBREG)
5764 reg = SUBREG_REG (reg);
5765 if (REG_P (reg))
5766 return REGNO (reg);
5767 return -1;
5770 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5771 return true if we did any change. The undo transformations for
5772 inheritance looks like
5773 i <- i2
5774 p <- i => p <- i2
5775 or removing
5776 p <- i, i <- p, and i <- i3
5777 where p is original pseudo from which inheritance pseudo i was
5778 created, i and i3 are removed inheritance pseudos, i2 is another
5779 not removed inheritance pseudo. All split pseudos or other
5780 occurrences of removed inheritance pseudos are changed on the
5781 corresponding original pseudos.
5783 The function also schedules insns changed and created during
5784 inheritance/split pass for processing by the subsequent constraint
5785 pass. */
5786 static bool
5787 remove_inheritance_pseudos (bitmap remove_pseudos)
5789 basic_block bb;
5790 int regno, sregno, prev_sregno, dregno, restore_regno;
5791 rtx set, prev_set;
5792 rtx_insn *prev_insn;
5793 bool change_p, done_p;
5795 change_p = ! bitmap_empty_p (remove_pseudos);
5796 /* We can not finish the function right away if CHANGE_P is true
5797 because we need to marks insns affected by previous
5798 inheritance/split pass for processing by the subsequent
5799 constraint pass. */
5800 FOR_EACH_BB_FN (bb, cfun)
5802 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5803 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5804 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5806 if (! INSN_P (curr_insn))
5807 continue;
5808 done_p = false;
5809 sregno = dregno = -1;
5810 if (change_p && NONDEBUG_INSN_P (curr_insn)
5811 && (set = single_set (curr_insn)) != NULL_RTX)
5813 dregno = get_regno (SET_DEST (set));
5814 sregno = get_regno (SET_SRC (set));
5817 if (sregno >= 0 && dregno >= 0)
5819 if ((bitmap_bit_p (remove_pseudos, sregno)
5820 && (lra_reg_info[sregno].restore_regno == dregno
5821 || (bitmap_bit_p (remove_pseudos, dregno)
5822 && (lra_reg_info[sregno].restore_regno
5823 == lra_reg_info[dregno].restore_regno))))
5824 || (bitmap_bit_p (remove_pseudos, dregno)
5825 && lra_reg_info[dregno].restore_regno == sregno))
5826 /* One of the following cases:
5827 original <- removed inheritance pseudo
5828 removed inherit pseudo <- another removed inherit pseudo
5829 removed inherit pseudo <- original pseudo
5831 removed_split_pseudo <- original_reg
5832 original_reg <- removed_split_pseudo */
5834 if (lra_dump_file != NULL)
5836 fprintf (lra_dump_file, " Removing %s:\n",
5837 bitmap_bit_p (&lra_split_regs, sregno)
5838 || bitmap_bit_p (&lra_split_regs, dregno)
5839 ? "split" : "inheritance");
5840 dump_insn_slim (lra_dump_file, curr_insn);
5842 lra_set_insn_deleted (curr_insn);
5843 done_p = true;
5845 else if (bitmap_bit_p (remove_pseudos, sregno)
5846 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5848 /* Search the following pattern:
5849 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5850 original_pseudo <- inherit_or_split_pseudo1
5851 where the 2nd insn is the current insn and
5852 inherit_or_split_pseudo2 is not removed. If it is found,
5853 change the current insn onto:
5854 original_pseudo <- inherit_or_split_pseudo2. */
5855 for (prev_insn = PREV_INSN (curr_insn);
5856 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5857 prev_insn = PREV_INSN (prev_insn))
5859 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5860 && (prev_set = single_set (prev_insn)) != NULL_RTX
5861 /* There should be no subregs in insn we are
5862 searching because only the original reg might
5863 be in subreg when we changed the mode of
5864 load/store for splitting. */
5865 && REG_P (SET_DEST (prev_set))
5866 && REG_P (SET_SRC (prev_set))
5867 && (int) REGNO (SET_DEST (prev_set)) == sregno
5868 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5869 >= FIRST_PSEUDO_REGISTER)
5870 /* As we consider chain of inheritance or
5871 splitting described in above comment we should
5872 check that sregno and prev_sregno were
5873 inheritance/split pseudos created from the
5874 same original regno. */
5875 && (lra_reg_info[sregno].restore_regno
5876 == lra_reg_info[prev_sregno].restore_regno)
5877 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5879 lra_assert (GET_MODE (SET_SRC (prev_set))
5880 == GET_MODE (regno_reg_rtx[sregno]));
5881 if (GET_CODE (SET_SRC (set)) == SUBREG)
5882 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5883 else
5884 SET_SRC (set) = SET_SRC (prev_set);
5885 /* As we are finishing with processing the insn
5886 here, check the destination too as it might
5887 inheritance pseudo for another pseudo. */
5888 if (bitmap_bit_p (remove_pseudos, dregno)
5889 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
5890 && (restore_regno
5891 = lra_reg_info[dregno].restore_regno) >= 0)
5893 if (GET_CODE (SET_DEST (set)) == SUBREG)
5894 SUBREG_REG (SET_DEST (set))
5895 = regno_reg_rtx[restore_regno];
5896 else
5897 SET_DEST (set) = regno_reg_rtx[restore_regno];
5899 lra_push_insn_and_update_insn_regno_info (curr_insn);
5900 lra_set_used_insn_alternative_by_uid
5901 (INSN_UID (curr_insn), -1);
5902 done_p = true;
5903 if (lra_dump_file != NULL)
5905 fprintf (lra_dump_file, " Change reload insn:\n");
5906 dump_insn_slim (lra_dump_file, curr_insn);
5911 if (! done_p)
5913 struct lra_insn_reg *reg;
5914 bool restored_regs_p = false;
5915 bool kept_regs_p = false;
5917 curr_id = lra_get_insn_recog_data (curr_insn);
5918 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5920 regno = reg->regno;
5921 restore_regno = lra_reg_info[regno].restore_regno;
5922 if (restore_regno >= 0)
5924 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5926 lra_substitute_pseudo_within_insn (
5927 curr_insn, regno, regno_reg_rtx[restore_regno]);
5928 restored_regs_p = true;
5930 else
5931 kept_regs_p = true;
5934 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5936 /* The instruction has changed since the previous
5937 constraints pass. */
5938 lra_push_insn_and_update_insn_regno_info (curr_insn);
5939 lra_set_used_insn_alternative_by_uid
5940 (INSN_UID (curr_insn), -1);
5942 else if (restored_regs_p)
5943 /* The instruction has been restored to the form that
5944 it had during the previous constraints pass. */
5945 lra_update_insn_regno_info (curr_insn);
5946 if (restored_regs_p && lra_dump_file != NULL)
5948 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5949 dump_insn_slim (lra_dump_file, curr_insn);
5954 return change_p;
5957 /* If optional reload pseudos failed to get a hard register or was not
5958 inherited, it is better to remove optional reloads. We do this
5959 transformation after undoing inheritance to figure out necessity to
5960 remove optional reloads easier. Return true if we do any
5961 change. */
5962 static bool
5963 undo_optional_reloads (void)
5965 bool change_p, keep_p;
5966 unsigned int regno, uid;
5967 bitmap_iterator bi, bi2;
5968 rtx_insn *insn;
5969 rtx set, src, dest;
5970 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5972 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5973 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5974 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5976 keep_p = false;
5977 /* Keep optional reloads from previous subpasses. */
5978 if (lra_reg_info[regno].restore_regno < 0
5979 /* If the original pseudo changed its allocation, just
5980 removing the optional pseudo is dangerous as the original
5981 pseudo will have longer live range. */
5982 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
5983 keep_p = true;
5984 else if (reg_renumber[regno] >= 0)
5985 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
5987 insn = lra_insn_recog_data[uid]->insn;
5988 if ((set = single_set (insn)) == NULL_RTX)
5989 continue;
5990 src = SET_SRC (set);
5991 dest = SET_DEST (set);
5992 if (! REG_P (src) || ! REG_P (dest))
5993 continue;
5994 if (REGNO (dest) == regno
5995 /* Ignore insn for optional reloads itself. */
5996 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
5997 /* Check only inheritance on last inheritance pass. */
5998 && (int) REGNO (src) >= new_regno_start
5999 /* Check that the optional reload was inherited. */
6000 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6002 keep_p = true;
6003 break;
6006 if (keep_p)
6008 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6009 if (lra_dump_file != NULL)
6010 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6013 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6014 bitmap_initialize (&insn_bitmap, &reg_obstack);
6015 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6017 if (lra_dump_file != NULL)
6018 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6019 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6020 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6022 insn = lra_insn_recog_data[uid]->insn;
6023 if ((set = single_set (insn)) != NULL_RTX)
6025 src = SET_SRC (set);
6026 dest = SET_DEST (set);
6027 if (REG_P (src) && REG_P (dest)
6028 && ((REGNO (src) == regno
6029 && (lra_reg_info[regno].restore_regno
6030 == (int) REGNO (dest)))
6031 || (REGNO (dest) == regno
6032 && (lra_reg_info[regno].restore_regno
6033 == (int) REGNO (src)))))
6035 if (lra_dump_file != NULL)
6037 fprintf (lra_dump_file, " Deleting move %u\n",
6038 INSN_UID (insn));
6039 dump_insn_slim (lra_dump_file, insn);
6041 lra_set_insn_deleted (insn);
6042 continue;
6044 /* We should not worry about generation memory-memory
6045 moves here as if the corresponding inheritance did
6046 not work (inheritance pseudo did not get a hard reg),
6047 we remove the inheritance pseudo and the optional
6048 reload. */
6050 lra_substitute_pseudo_within_insn (
6051 insn, regno,
6052 regno_reg_rtx[lra_reg_info[regno].restore_regno]);
6053 lra_update_insn_regno_info (insn);
6054 if (lra_dump_file != NULL)
6056 fprintf (lra_dump_file,
6057 " Restoring original insn:\n");
6058 dump_insn_slim (lra_dump_file, insn);
6062 /* Clear restore_regnos. */
6063 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6064 lra_reg_info[regno].restore_regno = -1;
6065 bitmap_clear (&insn_bitmap);
6066 bitmap_clear (&removed_optional_reload_pseudos);
6067 return change_p;
6070 /* Entry function for undoing inheritance/split transformation. Return true
6071 if we did any RTL change in this pass. */
6072 bool
6073 lra_undo_inheritance (void)
6075 unsigned int regno;
6076 int restore_regno, hard_regno;
6077 int n_all_inherit, n_inherit, n_all_split, n_split;
6078 bitmap_head remove_pseudos;
6079 bitmap_iterator bi;
6080 bool change_p;
6082 lra_undo_inheritance_iter++;
6083 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6084 return false;
6085 if (lra_dump_file != NULL)
6086 fprintf (lra_dump_file,
6087 "\n********** Undoing inheritance #%d: **********\n\n",
6088 lra_undo_inheritance_iter);
6089 bitmap_initialize (&remove_pseudos, &reg_obstack);
6090 n_inherit = n_all_inherit = 0;
6091 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6092 if (lra_reg_info[regno].restore_regno >= 0)
6094 n_all_inherit++;
6095 if (reg_renumber[regno] < 0
6096 /* If the original pseudo changed its allocation, just
6097 removing inheritance is dangerous as for changing
6098 allocation we used shorter live-ranges. */
6099 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6100 bitmap_set_bit (&remove_pseudos, regno);
6101 else
6102 n_inherit++;
6104 if (lra_dump_file != NULL && n_all_inherit != 0)
6105 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6106 n_inherit, n_all_inherit,
6107 (double) n_inherit / n_all_inherit * 100);
6108 n_split = n_all_split = 0;
6109 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6110 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6112 n_all_split++;
6113 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6114 ? reg_renumber[restore_regno] : restore_regno);
6115 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6116 bitmap_set_bit (&remove_pseudos, regno);
6117 else
6119 n_split++;
6120 if (lra_dump_file != NULL)
6121 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6122 regno, restore_regno);
6125 if (lra_dump_file != NULL && n_all_split != 0)
6126 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6127 n_split, n_all_split,
6128 (double) n_split / n_all_split * 100);
6129 change_p = remove_inheritance_pseudos (&remove_pseudos);
6130 bitmap_clear (&remove_pseudos);
6131 /* Clear restore_regnos. */
6132 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6133 lra_reg_info[regno].restore_regno = -1;
6134 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6135 lra_reg_info[regno].restore_regno = -1;
6136 change_p = undo_optional_reloads () || change_p;
6137 return change_p;