2015-09-25 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / lra-constraints.c
blobc6afa7a94f5e4db6045b2efc4463fc0ac4c67526
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2015 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains code for 3 passes: constraint pass,
23 inheritance/split pass, and pass for undoing failed inheritance and
24 split.
26 The major goal of constraint pass is to transform RTL to satisfy
27 insn and address constraints by:
28 o choosing insn alternatives;
29 o generating *reload insns* (or reloads in brief) and *reload
30 pseudos* which will get necessary hard registers later;
31 o substituting pseudos with equivalent values and removing the
32 instructions that initialized those pseudos.
34 The constraint pass has biggest and most complicated code in LRA.
35 There are a lot of important details like:
36 o reuse of input reload pseudos to simplify reload pseudo
37 allocations;
38 o some heuristics to choose insn alternative to improve the
39 inheritance;
40 o early clobbers etc.
42 The pass is mimicking former reload pass in alternative choosing
43 because the reload pass is oriented to current machine description
44 model. It might be changed if the machine description model is
45 changed.
47 There is special code for preventing all LRA and this pass cycling
48 in case of bugs.
50 On the first iteration of the pass we process every instruction and
51 choose an alternative for each one. On subsequent iterations we try
52 to avoid reprocessing instructions if we can be sure that the old
53 choice is still valid.
55 The inheritance/spilt pass is to transform code to achieve
56 ineheritance and live range splitting. It is done on backward
57 traversal of EBBs.
59 The inheritance optimization goal is to reuse values in hard
60 registers. There is analogous optimization in old reload pass. The
61 inheritance is achieved by following transformation:
63 reload_p1 <- p reload_p1 <- p
64 ... new_p <- reload_p1
65 ... => ...
66 reload_p2 <- p reload_p2 <- new_p
68 where p is spilled and not changed between the insns. Reload_p1 is
69 also called *original pseudo* and new_p is called *inheritance
70 pseudo*.
72 The subsequent assignment pass will try to assign the same (or
73 another if it is not possible) hard register to new_p as to
74 reload_p1 or reload_p2.
76 If the assignment pass fails to assign a hard register to new_p,
77 this file will undo the inheritance and restore the original code.
78 This is because implementing the above sequence with a spilled
79 new_p would make the code much worse. The inheritance is done in
80 EBB scope. The above is just a simplified example to get an idea
81 of the inheritance as the inheritance is also done for non-reload
82 insns.
84 Splitting (transformation) is also done in EBB scope on the same
85 pass as the inheritance:
87 r <- ... or ... <- r r <- ... or ... <- r
88 ... s <- r (new insn -- save)
89 ... =>
90 ... r <- s (new insn -- restore)
91 ... <- r ... <- r
93 The *split pseudo* s is assigned to the hard register of the
94 original pseudo or hard register r.
96 Splitting is done:
97 o In EBBs with high register pressure for global pseudos (living
98 in at least 2 BBs) and assigned to hard registers when there
99 are more one reloads needing the hard registers;
100 o for pseudos needing save/restore code around calls.
102 If the split pseudo still has the same hard register as the
103 original pseudo after the subsequent assignment pass or the
104 original pseudo was split, the opposite transformation is done on
105 the same pass for undoing inheritance. */
107 #undef REG_OK_STRICT
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "backend.h"
113 #include "predict.h"
114 #include "tree.h"
115 #include "rtl.h"
116 #include "df.h"
117 #include "tm_p.h"
118 #include "regs.h"
119 #include "insn-config.h"
120 #include "insn-codes.h"
121 #include "recog.h"
122 #include "output.h"
123 #include "addresses.h"
124 #include "target.h"
125 #include "flags.h"
126 #include "alias.h"
127 #include "expmed.h"
128 #include "dojump.h"
129 #include "explow.h"
130 #include "calls.h"
131 #include "emit-rtl.h"
132 #include "varasm.h"
133 #include "stmt.h"
134 #include "expr.h"
135 #include "cfgrtl.h"
136 #include "except.h"
137 #include "optabs.h"
138 #include "ira.h"
139 #include "rtl-error.h"
140 #include "params.h"
141 #include "lra.h"
142 #include "insn-attr.h"
143 #include "lra-int.h"
145 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
146 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
147 reload insns. */
148 static int bb_reload_num;
150 /* The current insn being processed and corresponding its single set
151 (NULL otherwise), its data (basic block, the insn data, the insn
152 static data, and the mode of each operand). */
153 static rtx_insn *curr_insn;
154 static rtx curr_insn_set;
155 static basic_block curr_bb;
156 static lra_insn_recog_data_t curr_id;
157 static struct lra_static_insn_data *curr_static_id;
158 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
159 /* Mode of the register substituted by its equivalence with VOIDmode
160 (e.g. constant) and whose subreg is given operand of the current
161 insn. VOIDmode in all other cases. */
162 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
166 /* Start numbers for new registers and insns at the current constraints
167 pass start. */
168 static int new_regno_start;
169 static int new_insn_uid_start;
171 /* If LOC is nonnull, strip any outer subreg from it. */
172 static inline rtx *
173 strip_subreg (rtx *loc)
175 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
178 /* Return hard regno of REGNO or if it is was not assigned to a hard
179 register, use a hard register from its allocno class. */
180 static int
181 get_try_hard_regno (int regno)
183 int hard_regno;
184 enum reg_class rclass;
186 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
187 hard_regno = lra_get_regno_hard_regno (regno);
188 if (hard_regno >= 0)
189 return hard_regno;
190 rclass = lra_get_allocno_class (regno);
191 if (rclass == NO_REGS)
192 return -1;
193 return ira_class_hard_regs[rclass][0];
196 /* Return final hard regno (plus offset) which will be after
197 elimination. We do this for matching constraints because the final
198 hard regno could have a different class. */
199 static int
200 get_final_hard_regno (int hard_regno, int offset)
202 if (hard_regno < 0)
203 return hard_regno;
204 hard_regno = lra_get_elimination_hard_regno (hard_regno);
205 return hard_regno + offset;
208 /* Return hard regno of X after removing subreg and making
209 elimination. If X is not a register or subreg of register, return
210 -1. For pseudo use its assignment. */
211 static int
212 get_hard_regno (rtx x)
214 rtx reg;
215 int offset, hard_regno;
217 reg = x;
218 if (GET_CODE (x) == SUBREG)
219 reg = SUBREG_REG (x);
220 if (! REG_P (reg))
221 return -1;
222 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
223 hard_regno = lra_get_regno_hard_regno (hard_regno);
224 if (hard_regno < 0)
225 return -1;
226 offset = 0;
227 if (GET_CODE (x) == SUBREG)
228 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
229 SUBREG_BYTE (x), GET_MODE (x));
230 return get_final_hard_regno (hard_regno, offset);
233 /* If REGNO is a hard register or has been allocated a hard register,
234 return the class of that register. If REGNO is a reload pseudo
235 created by the current constraints pass, return its allocno class.
236 Return NO_REGS otherwise. */
237 static enum reg_class
238 get_reg_class (int regno)
240 int hard_regno;
242 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
243 hard_regno = lra_get_regno_hard_regno (regno);
244 if (hard_regno >= 0)
246 hard_regno = get_final_hard_regno (hard_regno, 0);
247 return REGNO_REG_CLASS (hard_regno);
249 if (regno >= new_regno_start)
250 return lra_get_allocno_class (regno);
251 return NO_REGS;
254 /* Return true if REG satisfies (or will satisfy) reg class constraint
255 CL. Use elimination first if REG is a hard register. If REG is a
256 reload pseudo created by this constraints pass, assume that it will
257 be allocated a hard register from its allocno class, but allow that
258 class to be narrowed to CL if it is currently a superset of CL.
260 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
261 REGNO (reg), or NO_REGS if no change in its class was needed. */
262 static bool
263 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
265 enum reg_class rclass, common_class;
266 machine_mode reg_mode;
267 int class_size, hard_regno, nregs, i, j;
268 int regno = REGNO (reg);
270 if (new_class != NULL)
271 *new_class = NO_REGS;
272 if (regno < FIRST_PSEUDO_REGISTER)
274 rtx final_reg = reg;
275 rtx *final_loc = &final_reg;
277 lra_eliminate_reg_if_possible (final_loc);
278 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
280 reg_mode = GET_MODE (reg);
281 rclass = get_reg_class (regno);
282 if (regno < new_regno_start
283 /* Do not allow the constraints for reload instructions to
284 influence the classes of new pseudos. These reloads are
285 typically moves that have many alternatives, and restricting
286 reload pseudos for one alternative may lead to situations
287 where other reload pseudos are no longer allocatable. */
288 || (INSN_UID (curr_insn) >= new_insn_uid_start
289 && curr_insn_set != NULL
290 && ((OBJECT_P (SET_SRC (curr_insn_set))
291 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
292 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
293 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
294 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
295 /* When we don't know what class will be used finally for reload
296 pseudos, we use ALL_REGS. */
297 return ((regno >= new_regno_start && rclass == ALL_REGS)
298 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
299 && ! hard_reg_set_subset_p (reg_class_contents[cl],
300 lra_no_alloc_regs)));
301 else
303 common_class = ira_reg_class_subset[rclass][cl];
304 if (new_class != NULL)
305 *new_class = common_class;
306 if (hard_reg_set_subset_p (reg_class_contents[common_class],
307 lra_no_alloc_regs))
308 return false;
309 /* Check that there are enough allocatable regs. */
310 class_size = ira_class_hard_regs_num[common_class];
311 for (i = 0; i < class_size; i++)
313 hard_regno = ira_class_hard_regs[common_class][i];
314 nregs = hard_regno_nregs[hard_regno][reg_mode];
315 if (nregs == 1)
316 return true;
317 for (j = 0; j < nregs; j++)
318 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
319 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
320 hard_regno + j))
321 break;
322 if (j >= nregs)
323 return true;
325 return false;
329 /* Return true if REGNO satisfies a memory constraint. */
330 static bool
331 in_mem_p (int regno)
333 return get_reg_class (regno) == NO_REGS;
336 /* Return 1 if ADDR is a valid memory address for mode MODE in address
337 space AS, and check that each pseudo has the proper kind of hard
338 reg. */
339 static int
340 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
341 rtx addr, addr_space_t as)
343 #ifdef GO_IF_LEGITIMATE_ADDRESS
344 lra_assert (ADDR_SPACE_GENERIC_P (as));
345 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
346 return 0;
348 win:
349 return 1;
350 #else
351 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
352 #endif
355 namespace {
356 /* Temporarily eliminates registers in an address (for the lifetime of
357 the object). */
358 class address_eliminator {
359 public:
360 address_eliminator (struct address_info *ad);
361 ~address_eliminator ();
363 private:
364 struct address_info *m_ad;
365 rtx *m_base_loc;
366 rtx m_base_reg;
367 rtx *m_index_loc;
368 rtx m_index_reg;
372 address_eliminator::address_eliminator (struct address_info *ad)
373 : m_ad (ad),
374 m_base_loc (strip_subreg (ad->base_term)),
375 m_base_reg (NULL_RTX),
376 m_index_loc (strip_subreg (ad->index_term)),
377 m_index_reg (NULL_RTX)
379 if (m_base_loc != NULL)
381 m_base_reg = *m_base_loc;
382 lra_eliminate_reg_if_possible (m_base_loc);
383 if (m_ad->base_term2 != NULL)
384 *m_ad->base_term2 = *m_ad->base_term;
386 if (m_index_loc != NULL)
388 m_index_reg = *m_index_loc;
389 lra_eliminate_reg_if_possible (m_index_loc);
393 address_eliminator::~address_eliminator ()
395 if (m_base_loc && *m_base_loc != m_base_reg)
397 *m_base_loc = m_base_reg;
398 if (m_ad->base_term2 != NULL)
399 *m_ad->base_term2 = *m_ad->base_term;
401 if (m_index_loc && *m_index_loc != m_index_reg)
402 *m_index_loc = m_index_reg;
405 /* Return true if the eliminated form of AD is a legitimate target address. */
406 static bool
407 valid_address_p (struct address_info *ad)
409 address_eliminator eliminator (ad);
410 return valid_address_p (ad->mode, *ad->outer, ad->as);
413 /* Return true if the eliminated form of memory reference OP satisfies
414 extra memory constraint CONSTRAINT. */
415 static bool
416 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
418 struct address_info ad;
420 decompose_mem_address (&ad, op);
421 address_eliminator eliminator (&ad);
422 return constraint_satisfied_p (op, constraint);
425 /* Return true if the eliminated form of address AD satisfies extra
426 address constraint CONSTRAINT. */
427 static bool
428 satisfies_address_constraint_p (struct address_info *ad,
429 enum constraint_num constraint)
431 address_eliminator eliminator (ad);
432 return constraint_satisfied_p (*ad->outer, constraint);
435 /* Return true if the eliminated form of address OP satisfies extra
436 address constraint CONSTRAINT. */
437 static bool
438 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
440 struct address_info ad;
442 decompose_lea_address (&ad, &op);
443 return satisfies_address_constraint_p (&ad, constraint);
446 /* Initiate equivalences for LRA. As we keep original equivalences
447 before any elimination, we need to make copies otherwise any change
448 in insns might change the equivalences. */
449 void
450 lra_init_equiv (void)
452 ira_expand_reg_equiv ();
453 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
455 rtx res;
457 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
458 ira_reg_equiv[i].memory = copy_rtx (res);
459 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
460 ira_reg_equiv[i].invariant = copy_rtx (res);
464 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
466 /* Update equivalence for REGNO. We need to this as the equivalence
467 might contain other pseudos which are changed by their
468 equivalences. */
469 static void
470 update_equiv (int regno)
472 rtx x;
474 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
475 ira_reg_equiv[regno].memory
476 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
477 NULL_RTX);
478 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
479 ira_reg_equiv[regno].invariant
480 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
481 NULL_RTX);
484 /* If we have decided to substitute X with another value, return that
485 value, otherwise return X. */
486 static rtx
487 get_equiv (rtx x)
489 int regno;
490 rtx res;
492 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
493 || ! ira_reg_equiv[regno].defined_p
494 || ! ira_reg_equiv[regno].profitable_p
495 || lra_get_regno_hard_regno (regno) >= 0)
496 return x;
497 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
499 if (targetm.cannot_substitute_mem_equiv_p (res))
500 return x;
501 return res;
503 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
504 return res;
505 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
506 return res;
507 gcc_unreachable ();
510 /* If we have decided to substitute X with the equivalent value,
511 return that value after elimination for INSN, otherwise return
512 X. */
513 static rtx
514 get_equiv_with_elimination (rtx x, rtx_insn *insn)
516 rtx res = get_equiv (x);
518 if (x == res || CONSTANT_P (res))
519 return res;
520 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
521 false, false, 0, true);
524 /* Set up curr_operand_mode. */
525 static void
526 init_curr_operand_mode (void)
528 int nop = curr_static_id->n_operands;
529 for (int i = 0; i < nop; i++)
531 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
532 if (mode == VOIDmode)
534 /* The .md mode for address operands is the mode of the
535 addressed value rather than the mode of the address itself. */
536 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
537 mode = Pmode;
538 else
539 mode = curr_static_id->operand[i].mode;
541 curr_operand_mode[i] = mode;
547 /* The page contains code to reuse input reloads. */
549 /* Structure describes input reload of the current insns. */
550 struct input_reload
552 /* Reloaded value. */
553 rtx input;
554 /* Reload pseudo used. */
555 rtx reg;
558 /* The number of elements in the following array. */
559 static int curr_insn_input_reloads_num;
560 /* Array containing info about input reloads. It is used to find the
561 same input reload and reuse the reload pseudo in this case. */
562 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
564 /* Initiate data concerning reuse of input reloads for the current
565 insn. */
566 static void
567 init_curr_insn_input_reloads (void)
569 curr_insn_input_reloads_num = 0;
572 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
573 created input reload pseudo (only if TYPE is not OP_OUT). Don't
574 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
575 wrapped up in SUBREG. The result pseudo is returned through
576 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
577 reused the already created input reload pseudo. Use TITLE to
578 describe new registers for debug purposes. */
579 static bool
580 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
581 enum reg_class rclass, bool in_subreg_p,
582 const char *title, rtx *result_reg)
584 int i, regno;
585 enum reg_class new_class;
587 if (type == OP_OUT)
589 *result_reg
590 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
591 return true;
593 /* Prevent reuse value of expression with side effects,
594 e.g. volatile memory. */
595 if (! side_effects_p (original))
596 for (i = 0; i < curr_insn_input_reloads_num; i++)
597 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
598 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
600 rtx reg = curr_insn_input_reloads[i].reg;
601 regno = REGNO (reg);
602 /* If input is equal to original and both are VOIDmode,
603 GET_MODE (reg) might be still different from mode.
604 Ensure we don't return *result_reg with wrong mode. */
605 if (GET_MODE (reg) != mode)
607 if (in_subreg_p)
608 continue;
609 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
610 continue;
611 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
612 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
613 continue;
615 *result_reg = reg;
616 if (lra_dump_file != NULL)
618 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
619 dump_value_slim (lra_dump_file, original, 1);
621 if (new_class != lra_get_allocno_class (regno))
622 lra_change_class (regno, new_class, ", change to", false);
623 if (lra_dump_file != NULL)
624 fprintf (lra_dump_file, "\n");
625 return false;
627 *result_reg = lra_create_new_reg (mode, original, rclass, title);
628 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
629 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
630 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
631 return true;
636 /* The page contains code to extract memory address parts. */
638 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
639 static inline bool
640 ok_for_index_p_nonstrict (rtx reg)
642 unsigned regno = REGNO (reg);
644 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
647 /* A version of regno_ok_for_base_p for use here, when all pseudos
648 should count as OK. Arguments as for regno_ok_for_base_p. */
649 static inline bool
650 ok_for_base_p_nonstrict (rtx reg, machine_mode mode, addr_space_t as,
651 enum rtx_code outer_code, enum rtx_code index_code)
653 unsigned regno = REGNO (reg);
655 if (regno >= FIRST_PSEUDO_REGISTER)
656 return true;
657 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
662 /* The page contains major code to choose the current insn alternative
663 and generate reloads for it. */
665 /* Return the offset from REGNO of the least significant register
666 in (reg:MODE REGNO).
668 This function is used to tell whether two registers satisfy
669 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
671 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
672 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
674 lra_constraint_offset (int regno, machine_mode mode)
676 lra_assert (regno < FIRST_PSEUDO_REGISTER);
677 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
678 && SCALAR_INT_MODE_P (mode))
679 return hard_regno_nregs[regno][mode] - 1;
680 return 0;
683 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
684 if they are the same hard reg, and has special hacks for
685 auto-increment and auto-decrement. This is specifically intended for
686 process_alt_operands to use in determining whether two operands
687 match. X is the operand whose number is the lower of the two.
689 It is supposed that X is the output operand and Y is the input
690 operand. Y_HARD_REGNO is the final hard regno of register Y or
691 register in subreg Y as we know it now. Otherwise, it is a
692 negative value. */
693 static bool
694 operands_match_p (rtx x, rtx y, int y_hard_regno)
696 int i;
697 RTX_CODE code = GET_CODE (x);
698 const char *fmt;
700 if (x == y)
701 return true;
702 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
703 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
705 int j;
707 i = get_hard_regno (x);
708 if (i < 0)
709 goto slow;
711 if ((j = y_hard_regno) < 0)
712 goto slow;
714 i += lra_constraint_offset (i, GET_MODE (x));
715 j += lra_constraint_offset (j, GET_MODE (y));
717 return i == j;
720 /* If two operands must match, because they are really a single
721 operand of an assembler insn, then two post-increments are invalid
722 because the assembler insn would increment only once. On the
723 other hand, a post-increment matches ordinary indexing if the
724 post-increment is the output operand. */
725 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
726 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
728 /* Two pre-increments are invalid because the assembler insn would
729 increment only once. On the other hand, a pre-increment matches
730 ordinary indexing if the pre-increment is the input operand. */
731 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
732 || GET_CODE (y) == PRE_MODIFY)
733 return operands_match_p (x, XEXP (y, 0), -1);
735 slow:
737 if (code == REG && REG_P (y))
738 return REGNO (x) == REGNO (y);
740 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
741 && x == SUBREG_REG (y))
742 return true;
743 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
744 && SUBREG_REG (x) == y)
745 return true;
747 /* Now we have disposed of all the cases in which different rtx
748 codes can match. */
749 if (code != GET_CODE (y))
750 return false;
752 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
753 if (GET_MODE (x) != GET_MODE (y))
754 return false;
756 switch (code)
758 CASE_CONST_UNIQUE:
759 return false;
761 case LABEL_REF:
762 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
763 case SYMBOL_REF:
764 return XSTR (x, 0) == XSTR (y, 0);
766 default:
767 break;
770 /* Compare the elements. If any pair of corresponding elements fail
771 to match, return false for the whole things. */
773 fmt = GET_RTX_FORMAT (code);
774 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
776 int val, j;
777 switch (fmt[i])
779 case 'w':
780 if (XWINT (x, i) != XWINT (y, i))
781 return false;
782 break;
784 case 'i':
785 if (XINT (x, i) != XINT (y, i))
786 return false;
787 break;
789 case 'e':
790 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
791 if (val == 0)
792 return false;
793 break;
795 case '0':
796 break;
798 case 'E':
799 if (XVECLEN (x, i) != XVECLEN (y, i))
800 return false;
801 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
803 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
804 if (val == 0)
805 return false;
807 break;
809 /* It is believed that rtx's at this level will never
810 contain anything but integers and other rtx's, except for
811 within LABEL_REFs and SYMBOL_REFs. */
812 default:
813 gcc_unreachable ();
816 return true;
819 /* True if X is a constant that can be forced into the constant pool.
820 MODE is the mode of the operand, or VOIDmode if not known. */
821 #define CONST_POOL_OK_P(MODE, X) \
822 ((MODE) != VOIDmode \
823 && CONSTANT_P (X) \
824 && GET_CODE (X) != HIGH \
825 && !targetm.cannot_force_const_mem (MODE, X))
827 /* True if C is a non-empty register class that has too few registers
828 to be safely used as a reload target class. */
829 #define SMALL_REGISTER_CLASS_P(C) \
830 (ira_class_hard_regs_num [(C)] == 1 \
831 || (ira_class_hard_regs_num [(C)] >= 1 \
832 && targetm.class_likely_spilled_p (C)))
834 /* If REG is a reload pseudo, try to make its class satisfying CL. */
835 static void
836 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
838 enum reg_class rclass;
840 /* Do not make more accurate class from reloads generated. They are
841 mostly moves with a lot of constraints. Making more accurate
842 class may results in very narrow class and impossibility of find
843 registers for several reloads of one insn. */
844 if (INSN_UID (curr_insn) >= new_insn_uid_start)
845 return;
846 if (GET_CODE (reg) == SUBREG)
847 reg = SUBREG_REG (reg);
848 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
849 return;
850 if (in_class_p (reg, cl, &rclass) && rclass != cl)
851 lra_change_class (REGNO (reg), rclass, " Change to", true);
854 /* Generate reloads for matching OUT and INS (array of input operand
855 numbers with end marker -1) with reg class GOAL_CLASS. Add input
856 and output reloads correspondingly to the lists *BEFORE and *AFTER.
857 OUT might be negative. In this case we generate input reloads for
858 matched input operands INS. */
859 static void
860 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
861 rtx_insn **before, rtx_insn **after)
863 int i, in;
864 rtx new_in_reg, new_out_reg, reg;
865 machine_mode inmode, outmode;
866 rtx in_rtx = *curr_id->operand_loc[ins[0]];
867 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
869 inmode = curr_operand_mode[ins[0]];
870 outmode = out < 0 ? inmode : curr_operand_mode[out];
871 push_to_sequence (*before);
872 if (inmode != outmode)
874 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
876 reg = new_in_reg
877 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
878 goal_class, "");
879 if (SCALAR_INT_MODE_P (inmode))
880 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
881 else
882 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
883 LRA_SUBREG_P (new_out_reg) = 1;
884 /* If the input reg is dying here, we can use the same hard
885 register for REG and IN_RTX. We do it only for original
886 pseudos as reload pseudos can die although original
887 pseudos still live where reload pseudos dies. */
888 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
889 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
890 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
892 else
894 reg = new_out_reg
895 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
896 goal_class, "");
897 if (SCALAR_INT_MODE_P (outmode))
898 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
899 else
900 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
901 /* NEW_IN_REG is non-paradoxical subreg. We don't want
902 NEW_OUT_REG living above. We add clobber clause for
903 this. This is just a temporary clobber. We can remove
904 it at the end of LRA work. */
905 rtx_insn *clobber = emit_clobber (new_out_reg);
906 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
907 LRA_SUBREG_P (new_in_reg) = 1;
908 if (GET_CODE (in_rtx) == SUBREG)
910 rtx subreg_reg = SUBREG_REG (in_rtx);
912 /* If SUBREG_REG is dying here and sub-registers IN_RTX
913 and NEW_IN_REG are similar, we can use the same hard
914 register for REG and SUBREG_REG. */
915 if (REG_P (subreg_reg)
916 && (int) REGNO (subreg_reg) < lra_new_regno_start
917 && GET_MODE (subreg_reg) == outmode
918 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
919 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
920 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
924 else
926 /* Pseudos have values -- see comments for lra_reg_info.
927 Different pseudos with the same value do not conflict even if
928 they live in the same place. When we create a pseudo we
929 assign value of original pseudo (if any) from which we
930 created the new pseudo. If we create the pseudo from the
931 input pseudo, the new pseudo will have no conflict with the
932 input pseudo which is wrong when the input pseudo lives after
933 the insn and as the new pseudo value is changed by the insn
934 output. Therefore we create the new pseudo from the output
935 except the case when we have single matched dying input
936 pseudo.
938 We cannot reuse the current output register because we might
939 have a situation like "a <- a op b", where the constraints
940 force the second input operand ("b") to match the output
941 operand ("a"). "b" must then be copied into a new register
942 so that it doesn't clobber the current value of "a". */
944 new_in_reg = new_out_reg
945 = (ins[1] < 0 && REG_P (in_rtx)
946 && (int) REGNO (in_rtx) < lra_new_regno_start
947 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
948 /* We can not use the same value if the pseudo is mentioned
949 in the output, e.g. as an address part in memory,
950 becuase output reload will actually extend the pseudo
951 liveness. We don't care about eliminable hard regs here
952 as we are interesting only in pseudos. */
953 && (out < 0 || regno_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
954 ? lra_create_new_reg (inmode, in_rtx, goal_class, "")
955 : lra_create_new_reg_with_unique_value (outmode, out_rtx,
956 goal_class, ""));
958 /* In operand can be got from transformations before processing insn
959 constraints. One example of such transformations is subreg
960 reloading (see function simplify_operand_subreg). The new
961 pseudos created by the transformations might have inaccurate
962 class (ALL_REGS) and we should make their classes more
963 accurate. */
964 narrow_reload_pseudo_class (in_rtx, goal_class);
965 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
966 *before = get_insns ();
967 end_sequence ();
968 for (i = 0; (in = ins[i]) >= 0; i++)
970 lra_assert
971 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
972 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
973 *curr_id->operand_loc[in] = new_in_reg;
975 lra_update_dups (curr_id, ins);
976 if (out < 0)
977 return;
978 /* See a comment for the input operand above. */
979 narrow_reload_pseudo_class (out_rtx, goal_class);
980 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
982 start_sequence ();
983 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
984 emit_insn (*after);
985 *after = get_insns ();
986 end_sequence ();
988 *curr_id->operand_loc[out] = new_out_reg;
989 lra_update_dup (curr_id, out);
992 /* Return register class which is union of all reg classes in insn
993 constraint alternative string starting with P. */
994 static enum reg_class
995 reg_class_from_constraints (const char *p)
997 int c, len;
998 enum reg_class op_class = NO_REGS;
1001 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1003 case '#':
1004 case ',':
1005 return op_class;
1007 case 'g':
1008 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1009 break;
1011 default:
1012 enum constraint_num cn = lookup_constraint (p);
1013 enum reg_class cl = reg_class_for_constraint (cn);
1014 if (cl == NO_REGS)
1016 if (insn_extra_address_constraint (cn))
1017 op_class
1018 = (reg_class_subunion
1019 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1020 ADDRESS, SCRATCH)]);
1021 break;
1024 op_class = reg_class_subunion[op_class][cl];
1025 break;
1027 while ((p += len), c);
1028 return op_class;
1031 /* If OP is a register, return the class of the register as per
1032 get_reg_class, otherwise return NO_REGS. */
1033 static inline enum reg_class
1034 get_op_class (rtx op)
1036 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1039 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1040 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1041 SUBREG for VAL to make them equal. */
1042 static rtx_insn *
1043 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1045 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1047 /* Usually size of mem_pseudo is greater than val size but in
1048 rare cases it can be less as it can be defined by target
1049 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1050 if (! MEM_P (val))
1052 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1053 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1055 LRA_SUBREG_P (val) = 1;
1057 else
1059 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1060 LRA_SUBREG_P (mem_pseudo) = 1;
1063 return to_p ? gen_move_insn (mem_pseudo, val)
1064 : gen_move_insn (val, mem_pseudo);
1067 /* Process a special case insn (register move), return true if we
1068 don't need to process it anymore. INSN should be a single set
1069 insn. Set up that RTL was changed through CHANGE_P and macro
1070 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1071 SEC_MEM_P. */
1072 static bool
1073 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1075 int sregno, dregno;
1076 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1077 rtx_insn *before;
1078 enum reg_class dclass, sclass, secondary_class;
1079 secondary_reload_info sri;
1081 lra_assert (curr_insn_set != NULL_RTX);
1082 dreg = dest = SET_DEST (curr_insn_set);
1083 sreg = src = SET_SRC (curr_insn_set);
1084 if (GET_CODE (dest) == SUBREG)
1085 dreg = SUBREG_REG (dest);
1086 if (GET_CODE (src) == SUBREG)
1087 sreg = SUBREG_REG (src);
1088 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1089 return false;
1090 sclass = dclass = NO_REGS;
1091 if (REG_P (dreg))
1092 dclass = get_reg_class (REGNO (dreg));
1093 if (dclass == ALL_REGS)
1094 /* ALL_REGS is used for new pseudos created by transformations
1095 like reload of SUBREG_REG (see function
1096 simplify_operand_subreg). We don't know their class yet. We
1097 should figure out the class from processing the insn
1098 constraints not in this fast path function. Even if ALL_REGS
1099 were a right class for the pseudo, secondary_... hooks usually
1100 are not define for ALL_REGS. */
1101 return false;
1102 if (REG_P (sreg))
1103 sclass = get_reg_class (REGNO (sreg));
1104 if (sclass == ALL_REGS)
1105 /* See comments above. */
1106 return false;
1107 if (sclass == NO_REGS && dclass == NO_REGS)
1108 return false;
1109 #ifdef SECONDARY_MEMORY_NEEDED
1110 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1111 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1112 && ((sclass != NO_REGS && dclass != NO_REGS)
1113 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1114 #endif
1117 *sec_mem_p = true;
1118 return false;
1120 #endif
1121 if (! REG_P (dreg) || ! REG_P (sreg))
1122 return false;
1123 sri.prev_sri = NULL;
1124 sri.icode = CODE_FOR_nothing;
1125 sri.extra_cost = 0;
1126 secondary_class = NO_REGS;
1127 /* Set up hard register for a reload pseudo for hook
1128 secondary_reload because some targets just ignore unassigned
1129 pseudos in the hook. */
1130 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1132 dregno = REGNO (dreg);
1133 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1135 else
1136 dregno = -1;
1137 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1139 sregno = REGNO (sreg);
1140 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1142 else
1143 sregno = -1;
1144 if (sclass != NO_REGS)
1145 secondary_class
1146 = (enum reg_class) targetm.secondary_reload (false, dest,
1147 (reg_class_t) sclass,
1148 GET_MODE (src), &sri);
1149 if (sclass == NO_REGS
1150 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1151 && dclass != NO_REGS))
1153 enum reg_class old_sclass = secondary_class;
1154 secondary_reload_info old_sri = sri;
1156 sri.prev_sri = NULL;
1157 sri.icode = CODE_FOR_nothing;
1158 sri.extra_cost = 0;
1159 secondary_class
1160 = (enum reg_class) targetm.secondary_reload (true, src,
1161 (reg_class_t) dclass,
1162 GET_MODE (src), &sri);
1163 /* Check the target hook consistency. */
1164 lra_assert
1165 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1166 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1167 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1169 if (sregno >= 0)
1170 reg_renumber [sregno] = -1;
1171 if (dregno >= 0)
1172 reg_renumber [dregno] = -1;
1173 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1174 return false;
1175 *change_p = true;
1176 new_reg = NULL_RTX;
1177 if (secondary_class != NO_REGS)
1178 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1179 secondary_class,
1180 "secondary");
1181 start_sequence ();
1182 if (sri.icode == CODE_FOR_nothing)
1183 lra_emit_move (new_reg, src);
1184 else
1186 enum reg_class scratch_class;
1188 scratch_class = (reg_class_from_constraints
1189 (insn_data[sri.icode].operand[2].constraint));
1190 scratch_reg = (lra_create_new_reg_with_unique_value
1191 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1192 scratch_class, "scratch"));
1193 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1194 src, scratch_reg));
1196 before = get_insns ();
1197 end_sequence ();
1198 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1199 if (new_reg != NULL_RTX)
1200 SET_SRC (curr_insn_set) = new_reg;
1201 else
1203 if (lra_dump_file != NULL)
1205 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1206 dump_insn_slim (lra_dump_file, curr_insn);
1208 lra_set_insn_deleted (curr_insn);
1209 return true;
1211 return false;
1214 /* The following data describe the result of process_alt_operands.
1215 The data are used in curr_insn_transform to generate reloads. */
1217 /* The chosen reg classes which should be used for the corresponding
1218 operands. */
1219 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1220 /* True if the operand should be the same as another operand and that
1221 other operand does not need a reload. */
1222 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1223 /* True if the operand does not need a reload. */
1224 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1225 /* True if the operand can be offsetable memory. */
1226 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1227 /* The number of an operand to which given operand can be matched to. */
1228 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1229 /* The number of elements in the following array. */
1230 static int goal_alt_dont_inherit_ops_num;
1231 /* Numbers of operands whose reload pseudos should not be inherited. */
1232 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1233 /* True if the insn commutative operands should be swapped. */
1234 static bool goal_alt_swapped;
1235 /* The chosen insn alternative. */
1236 static int goal_alt_number;
1238 /* The following five variables are used to choose the best insn
1239 alternative. They reflect final characteristics of the best
1240 alternative. */
1242 /* Number of necessary reloads and overall cost reflecting the
1243 previous value and other unpleasantness of the best alternative. */
1244 static int best_losers, best_overall;
1245 /* Overall number hard registers used for reloads. For example, on
1246 some targets we need 2 general registers to reload DFmode and only
1247 one floating point register. */
1248 static int best_reload_nregs;
1249 /* Overall number reflecting distances of previous reloading the same
1250 value. The distances are counted from the current BB start. It is
1251 used to improve inheritance chances. */
1252 static int best_reload_sum;
1254 /* True if the current insn should have no correspondingly input or
1255 output reloads. */
1256 static bool no_input_reloads_p, no_output_reloads_p;
1258 /* True if we swapped the commutative operands in the current
1259 insn. */
1260 static int curr_swapped;
1262 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1263 register of class CL. Add any input reloads to list BEFORE. AFTER
1264 is nonnull if *LOC is an automodified value; handle that case by
1265 adding the required output reloads to list AFTER. Return true if
1266 the RTL was changed.
1268 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1269 register. Return false if the address register is correct. */
1270 static bool
1271 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1272 enum reg_class cl)
1274 int regno;
1275 enum reg_class rclass, new_class;
1276 rtx reg;
1277 rtx new_reg;
1278 machine_mode mode;
1279 bool subreg_p, before_p = false;
1281 subreg_p = GET_CODE (*loc) == SUBREG;
1282 if (subreg_p)
1283 loc = &SUBREG_REG (*loc);
1284 reg = *loc;
1285 mode = GET_MODE (reg);
1286 if (! REG_P (reg))
1288 if (check_only_p)
1289 return true;
1290 /* Always reload memory in an address even if the target supports
1291 such addresses. */
1292 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1293 before_p = true;
1295 else
1297 regno = REGNO (reg);
1298 rclass = get_reg_class (regno);
1299 if (! check_only_p
1300 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1302 if (lra_dump_file != NULL)
1304 fprintf (lra_dump_file,
1305 "Changing pseudo %d in address of insn %u on equiv ",
1306 REGNO (reg), INSN_UID (curr_insn));
1307 dump_value_slim (lra_dump_file, *loc, 1);
1308 fprintf (lra_dump_file, "\n");
1310 *loc = copy_rtx (*loc);
1312 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1314 if (check_only_p)
1315 return true;
1316 reg = *loc;
1317 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1318 mode, reg, cl, subreg_p, "address", &new_reg))
1319 before_p = true;
1321 else if (new_class != NO_REGS && rclass != new_class)
1323 if (check_only_p)
1324 return true;
1325 lra_change_class (regno, new_class, " Change to", true);
1326 return false;
1328 else
1329 return false;
1331 if (before_p)
1333 push_to_sequence (*before);
1334 lra_emit_move (new_reg, reg);
1335 *before = get_insns ();
1336 end_sequence ();
1338 *loc = new_reg;
1339 if (after != NULL)
1341 start_sequence ();
1342 lra_emit_move (reg, new_reg);
1343 emit_insn (*after);
1344 *after = get_insns ();
1345 end_sequence ();
1347 return true;
1350 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1351 the insn to be inserted before curr insn. AFTER returns the
1352 the insn to be inserted after curr insn. ORIGREG and NEWREG
1353 are the original reg and new reg for reload. */
1354 static void
1355 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1356 rtx newreg)
1358 if (before)
1360 push_to_sequence (*before);
1361 lra_emit_move (newreg, origreg);
1362 *before = get_insns ();
1363 end_sequence ();
1365 if (after)
1367 start_sequence ();
1368 lra_emit_move (origreg, newreg);
1369 emit_insn (*after);
1370 *after = get_insns ();
1371 end_sequence ();
1375 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1377 /* Make reloads for subreg in operand NOP with internal subreg mode
1378 REG_MODE, add new reloads for further processing. Return true if
1379 any change was done. */
1380 static bool
1381 simplify_operand_subreg (int nop, machine_mode reg_mode)
1383 int hard_regno;
1384 rtx_insn *before, *after;
1385 machine_mode mode, innermode;
1386 rtx reg, new_reg;
1387 rtx operand = *curr_id->operand_loc[nop];
1388 enum reg_class regclass;
1389 enum op_type type;
1391 before = after = NULL;
1393 if (GET_CODE (operand) != SUBREG)
1394 return false;
1396 mode = GET_MODE (operand);
1397 reg = SUBREG_REG (operand);
1398 innermode = GET_MODE (reg);
1399 type = curr_static_id->operand[nop].type;
1400 /* If we change address for paradoxical subreg of memory, the
1401 address might violate the necessary alignment or the access might
1402 be slow. So take this into consideration. We should not worry
1403 about access beyond allocated memory for paradoxical memory
1404 subregs as we don't substitute such equiv memory (see processing
1405 equivalences in function lra_constraints) and because for spilled
1406 pseudos we allocate stack memory enough for the biggest
1407 corresponding paradoxical subreg. */
1408 if (MEM_P (reg)
1409 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1410 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1412 rtx subst, old = *curr_id->operand_loc[nop];
1414 alter_subreg (curr_id->operand_loc[nop], false);
1415 subst = *curr_id->operand_loc[nop];
1416 lra_assert (MEM_P (subst));
1417 if (! valid_address_p (innermode, XEXP (reg, 0),
1418 MEM_ADDR_SPACE (reg))
1419 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1420 MEM_ADDR_SPACE (subst)))
1421 return true;
1422 /* If the address was valid and became invalid, prefer to reload
1423 the memory. Typical case is when the index scale should
1424 correspond the memory. */
1425 *curr_id->operand_loc[nop] = old;
1427 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1429 alter_subreg (curr_id->operand_loc[nop], false);
1430 return true;
1432 else if (CONSTANT_P (reg))
1434 /* Try to simplify subreg of constant. It is usually result of
1435 equivalence substitution. */
1436 if (innermode == VOIDmode
1437 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1438 innermode = curr_static_id->operand[nop].mode;
1439 if ((new_reg = simplify_subreg (mode, reg, innermode,
1440 SUBREG_BYTE (operand))) != NULL_RTX)
1442 *curr_id->operand_loc[nop] = new_reg;
1443 return true;
1446 /* Put constant into memory when we have mixed modes. It generates
1447 a better code in most cases as it does not need a secondary
1448 reload memory. It also prevents LRA looping when LRA is using
1449 secondary reload memory again and again. */
1450 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1451 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1453 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1454 alter_subreg (curr_id->operand_loc[nop], false);
1455 return true;
1457 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1458 if there may be a problem accessing OPERAND in the outer
1459 mode. */
1460 if ((REG_P (reg)
1461 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1462 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1463 /* Don't reload paradoxical subregs because we could be looping
1464 having repeatedly final regno out of hard regs range. */
1465 && (hard_regno_nregs[hard_regno][innermode]
1466 >= hard_regno_nregs[hard_regno][mode])
1467 && simplify_subreg_regno (hard_regno, innermode,
1468 SUBREG_BYTE (operand), mode) < 0
1469 /* Don't reload subreg for matching reload. It is actually
1470 valid subreg in LRA. */
1471 && ! LRA_SUBREG_P (operand))
1472 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1474 enum reg_class rclass;
1476 if (REG_P (reg))
1477 /* There is a big probability that we will get the same class
1478 for the new pseudo and we will get the same insn which
1479 means infinite looping. So spill the new pseudo. */
1480 rclass = NO_REGS;
1481 else
1482 /* The class will be defined later in curr_insn_transform. */
1483 rclass
1484 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1486 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1487 rclass, TRUE, "subreg reg", &new_reg))
1489 bool insert_before, insert_after;
1490 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1492 insert_before = (type != OP_OUT
1493 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1494 insert_after = (type != OP_IN);
1495 insert_move_for_subreg (insert_before ? &before : NULL,
1496 insert_after ? &after : NULL,
1497 reg, new_reg);
1499 SUBREG_REG (operand) = new_reg;
1500 lra_process_new_insns (curr_insn, before, after,
1501 "Inserting subreg reload");
1502 return true;
1504 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1505 IRA allocates hardreg to the inner pseudo reg according to its mode
1506 instead of the outermode, so the size of the hardreg may not be enough
1507 to contain the outermode operand, in that case we may need to insert
1508 reload for the reg. For the following two types of paradoxical subreg,
1509 we need to insert reload:
1510 1. If the op_type is OP_IN, and the hardreg could not be paired with
1511 other hardreg to contain the outermode operand
1512 (checked by in_hard_reg_set_p), we need to insert the reload.
1513 2. If the op_type is OP_OUT or OP_INOUT.
1515 Here is a paradoxical subreg example showing how the reload is generated:
1517 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1518 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1520 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1521 here, if reg107 is assigned to hardreg R15, because R15 is the last
1522 hardreg, compiler cannot find another hardreg to pair with R15 to
1523 contain TImode data. So we insert a TImode reload reg180 for it.
1524 After reload is inserted:
1526 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1527 (reg:DI 107 [ __comp ])) -1
1528 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1529 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1531 Two reload hard registers will be allocated to reg180 to save TImode data
1532 in LRA_assign. */
1533 else if (REG_P (reg)
1534 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1535 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1536 && (hard_regno_nregs[hard_regno][innermode]
1537 < hard_regno_nregs[hard_regno][mode])
1538 && (regclass = lra_get_allocno_class (REGNO (reg)))
1539 && (type != OP_IN
1540 || !in_hard_reg_set_p (reg_class_contents[regclass],
1541 mode, hard_regno)))
1543 /* The class will be defined later in curr_insn_transform. */
1544 enum reg_class rclass
1545 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1547 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1548 rclass, TRUE, "paradoxical subreg", &new_reg))
1550 rtx subreg;
1551 bool insert_before, insert_after;
1553 PUT_MODE (new_reg, mode);
1554 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1555 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1557 insert_before = (type != OP_OUT);
1558 insert_after = (type != OP_IN);
1559 insert_move_for_subreg (insert_before ? &before : NULL,
1560 insert_after ? &after : NULL,
1561 reg, subreg);
1563 SUBREG_REG (operand) = new_reg;
1564 lra_process_new_insns (curr_insn, before, after,
1565 "Inserting paradoxical subreg reload");
1566 return true;
1568 return false;
1571 /* Return TRUE if X refers for a hard register from SET. */
1572 static bool
1573 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1575 int i, j, x_hard_regno;
1576 machine_mode mode;
1577 const char *fmt;
1578 enum rtx_code code;
1580 if (x == NULL_RTX)
1581 return false;
1582 code = GET_CODE (x);
1583 mode = GET_MODE (x);
1584 if (code == SUBREG)
1586 x = SUBREG_REG (x);
1587 code = GET_CODE (x);
1588 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1589 mode = GET_MODE (x);
1592 if (REG_P (x))
1594 x_hard_regno = get_hard_regno (x);
1595 return (x_hard_regno >= 0
1596 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1598 if (MEM_P (x))
1600 struct address_info ad;
1602 decompose_mem_address (&ad, x);
1603 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1604 return true;
1605 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1606 return true;
1608 fmt = GET_RTX_FORMAT (code);
1609 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1611 if (fmt[i] == 'e')
1613 if (uses_hard_regs_p (XEXP (x, i), set))
1614 return true;
1616 else if (fmt[i] == 'E')
1618 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1619 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1620 return true;
1623 return false;
1626 /* Return true if OP is a spilled pseudo. */
1627 static inline bool
1628 spilled_pseudo_p (rtx op)
1630 return (REG_P (op)
1631 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1634 /* Return true if X is a general constant. */
1635 static inline bool
1636 general_constant_p (rtx x)
1638 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1641 static bool
1642 reg_in_class_p (rtx reg, enum reg_class cl)
1644 if (cl == NO_REGS)
1645 return get_reg_class (REGNO (reg)) == NO_REGS;
1646 return in_class_p (reg, cl, NULL);
1649 /* Return true if SET of RCLASS contains no hard regs which can be
1650 used in MODE. */
1651 static bool
1652 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1653 HARD_REG_SET &set,
1654 enum machine_mode mode)
1656 HARD_REG_SET temp;
1658 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1659 COPY_HARD_REG_SET (temp, set);
1660 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1661 return (hard_reg_set_subset_p
1662 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1665 /* Major function to choose the current insn alternative and what
1666 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1667 negative we should consider only this alternative. Return false if
1668 we can not choose the alternative or find how to reload the
1669 operands. */
1670 static bool
1671 process_alt_operands (int only_alternative)
1673 bool ok_p = false;
1674 int nop, overall, nalt;
1675 int n_alternatives = curr_static_id->n_alternatives;
1676 int n_operands = curr_static_id->n_operands;
1677 /* LOSERS counts the operands that don't fit this alternative and
1678 would require loading. */
1679 int losers;
1680 /* REJECT is a count of how undesirable this alternative says it is
1681 if any reloading is required. If the alternative matches exactly
1682 then REJECT is ignored, but otherwise it gets this much counted
1683 against it in addition to the reloading needed. */
1684 int reject;
1685 int op_reject;
1686 /* The number of elements in the following array. */
1687 int early_clobbered_regs_num;
1688 /* Numbers of operands which are early clobber registers. */
1689 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1690 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1691 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1692 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1693 bool curr_alt_win[MAX_RECOG_OPERANDS];
1694 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1695 int curr_alt_matches[MAX_RECOG_OPERANDS];
1696 /* The number of elements in the following array. */
1697 int curr_alt_dont_inherit_ops_num;
1698 /* Numbers of operands whose reload pseudos should not be inherited. */
1699 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1700 rtx op;
1701 /* The register when the operand is a subreg of register, otherwise the
1702 operand itself. */
1703 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1704 /* The register if the operand is a register or subreg of register,
1705 otherwise NULL. */
1706 rtx operand_reg[MAX_RECOG_OPERANDS];
1707 int hard_regno[MAX_RECOG_OPERANDS];
1708 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1709 int reload_nregs, reload_sum;
1710 bool costly_p;
1711 enum reg_class cl;
1713 /* Calculate some data common for all alternatives to speed up the
1714 function. */
1715 for (nop = 0; nop < n_operands; nop++)
1717 rtx reg;
1719 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1720 /* The real hard regno of the operand after the allocation. */
1721 hard_regno[nop] = get_hard_regno (op);
1723 operand_reg[nop] = reg = op;
1724 biggest_mode[nop] = GET_MODE (op);
1725 if (GET_CODE (op) == SUBREG)
1727 operand_reg[nop] = reg = SUBREG_REG (op);
1728 if (GET_MODE_SIZE (biggest_mode[nop])
1729 < GET_MODE_SIZE (GET_MODE (reg)))
1730 biggest_mode[nop] = GET_MODE (reg);
1732 if (! REG_P (reg))
1733 operand_reg[nop] = NULL_RTX;
1734 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1735 || ((int) REGNO (reg)
1736 == lra_get_elimination_hard_regno (REGNO (reg))))
1737 no_subreg_reg_operand[nop] = reg;
1738 else
1739 operand_reg[nop] = no_subreg_reg_operand[nop]
1740 /* Just use natural mode for elimination result. It should
1741 be enough for extra constraints hooks. */
1742 = regno_reg_rtx[hard_regno[nop]];
1745 /* The constraints are made of several alternatives. Each operand's
1746 constraint looks like foo,bar,... with commas separating the
1747 alternatives. The first alternatives for all operands go
1748 together, the second alternatives go together, etc.
1750 First loop over alternatives. */
1751 alternative_mask preferred = curr_id->preferred_alternatives;
1752 if (only_alternative >= 0)
1753 preferred &= ALTERNATIVE_BIT (only_alternative);
1755 for (nalt = 0; nalt < n_alternatives; nalt++)
1757 /* Loop over operands for one constraint alternative. */
1758 if (!TEST_BIT (preferred, nalt))
1759 continue;
1761 overall = losers = reject = reload_nregs = reload_sum = 0;
1762 for (nop = 0; nop < n_operands; nop++)
1764 int inc = (curr_static_id
1765 ->operand_alternative[nalt * n_operands + nop].reject);
1766 if (lra_dump_file != NULL && inc != 0)
1767 fprintf (lra_dump_file,
1768 " Staticly defined alt reject+=%d\n", inc);
1769 reject += inc;
1771 early_clobbered_regs_num = 0;
1773 for (nop = 0; nop < n_operands; nop++)
1775 const char *p;
1776 char *end;
1777 int len, c, m, i, opalt_num, this_alternative_matches;
1778 bool win, did_match, offmemok, early_clobber_p;
1779 /* false => this operand can be reloaded somehow for this
1780 alternative. */
1781 bool badop;
1782 /* true => this operand can be reloaded if the alternative
1783 allows regs. */
1784 bool winreg;
1785 /* True if a constant forced into memory would be OK for
1786 this operand. */
1787 bool constmemok;
1788 enum reg_class this_alternative, this_costly_alternative;
1789 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1790 bool this_alternative_match_win, this_alternative_win;
1791 bool this_alternative_offmemok;
1792 bool scratch_p;
1793 machine_mode mode;
1794 enum constraint_num cn;
1796 opalt_num = nalt * n_operands + nop;
1797 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1799 /* Fast track for no constraints at all. */
1800 curr_alt[nop] = NO_REGS;
1801 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1802 curr_alt_win[nop] = true;
1803 curr_alt_match_win[nop] = false;
1804 curr_alt_offmemok[nop] = false;
1805 curr_alt_matches[nop] = -1;
1806 continue;
1809 op = no_subreg_reg_operand[nop];
1810 mode = curr_operand_mode[nop];
1812 win = did_match = winreg = offmemok = constmemok = false;
1813 badop = true;
1815 early_clobber_p = false;
1816 p = curr_static_id->operand_alternative[opalt_num].constraint;
1818 this_costly_alternative = this_alternative = NO_REGS;
1819 /* We update set of possible hard regs besides its class
1820 because reg class might be inaccurate. For example,
1821 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1822 is translated in HI_REGS because classes are merged by
1823 pairs and there is no accurate intermediate class. */
1824 CLEAR_HARD_REG_SET (this_alternative_set);
1825 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1826 this_alternative_win = false;
1827 this_alternative_match_win = false;
1828 this_alternative_offmemok = false;
1829 this_alternative_matches = -1;
1831 /* An empty constraint should be excluded by the fast
1832 track. */
1833 lra_assert (*p != 0 && *p != ',');
1835 op_reject = 0;
1836 /* Scan this alternative's specs for this operand; set WIN
1837 if the operand fits any letter in this alternative.
1838 Otherwise, clear BADOP if this operand could fit some
1839 letter after reloads, or set WINREG if this operand could
1840 fit after reloads provided the constraint allows some
1841 registers. */
1842 costly_p = false;
1845 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1847 case '\0':
1848 len = 0;
1849 break;
1850 case ',':
1851 c = '\0';
1852 break;
1854 case '&':
1855 early_clobber_p = true;
1856 break;
1858 case '$':
1859 op_reject += LRA_MAX_REJECT;
1860 break;
1861 case '^':
1862 op_reject += LRA_LOSER_COST_FACTOR;
1863 break;
1865 case '#':
1866 /* Ignore rest of this alternative. */
1867 c = '\0';
1868 break;
1870 case '0': case '1': case '2': case '3': case '4':
1871 case '5': case '6': case '7': case '8': case '9':
1873 int m_hregno;
1874 bool match_p;
1876 m = strtoul (p, &end, 10);
1877 p = end;
1878 len = 0;
1879 lra_assert (nop > m);
1881 this_alternative_matches = m;
1882 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1883 /* We are supposed to match a previous operand.
1884 If we do, we win if that one did. If we do
1885 not, count both of the operands as losers.
1886 (This is too conservative, since most of the
1887 time only a single reload insn will be needed
1888 to make the two operands win. As a result,
1889 this alternative may be rejected when it is
1890 actually desirable.) */
1891 match_p = false;
1892 if (operands_match_p (*curr_id->operand_loc[nop],
1893 *curr_id->operand_loc[m], m_hregno))
1895 /* We should reject matching of an early
1896 clobber operand if the matching operand is
1897 not dying in the insn. */
1898 if (! curr_static_id->operand[m].early_clobber
1899 || operand_reg[nop] == NULL_RTX
1900 || (find_regno_note (curr_insn, REG_DEAD,
1901 REGNO (op))
1902 || REGNO (op) == REGNO (operand_reg[m])))
1903 match_p = true;
1905 if (match_p)
1907 /* If we are matching a non-offsettable
1908 address where an offsettable address was
1909 expected, then we must reject this
1910 combination, because we can't reload
1911 it. */
1912 if (curr_alt_offmemok[m]
1913 && MEM_P (*curr_id->operand_loc[m])
1914 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1915 continue;
1917 else
1919 /* Operands don't match. Both operands must
1920 allow a reload register, otherwise we
1921 cannot make them match. */
1922 if (curr_alt[m] == NO_REGS)
1923 break;
1924 /* Retroactively mark the operand we had to
1925 match as a loser, if it wasn't already and
1926 it wasn't matched to a register constraint
1927 (e.g it might be matched by memory). */
1928 if (curr_alt_win[m]
1929 && (operand_reg[m] == NULL_RTX
1930 || hard_regno[m] < 0))
1932 losers++;
1933 reload_nregs
1934 += (ira_reg_class_max_nregs[curr_alt[m]]
1935 [GET_MODE (*curr_id->operand_loc[m])]);
1938 /* Prefer matching earlyclobber alternative as
1939 it results in less hard regs required for
1940 the insn than a non-matching earlyclobber
1941 alternative. */
1942 if (curr_static_id->operand[m].early_clobber)
1944 if (lra_dump_file != NULL)
1945 fprintf
1946 (lra_dump_file,
1947 " %d Matching earlyclobber alt:"
1948 " reject--\n",
1949 nop);
1950 reject--;
1952 /* Otherwise we prefer no matching
1953 alternatives because it gives more freedom
1954 in RA. */
1955 else if (operand_reg[nop] == NULL_RTX
1956 || (find_regno_note (curr_insn, REG_DEAD,
1957 REGNO (operand_reg[nop]))
1958 == NULL_RTX))
1960 if (lra_dump_file != NULL)
1961 fprintf
1962 (lra_dump_file,
1963 " %d Matching alt: reject+=2\n",
1964 nop);
1965 reject += 2;
1968 /* If we have to reload this operand and some
1969 previous operand also had to match the same
1970 thing as this operand, we don't know how to do
1971 that. */
1972 if (!match_p || !curr_alt_win[m])
1974 for (i = 0; i < nop; i++)
1975 if (curr_alt_matches[i] == m)
1976 break;
1977 if (i < nop)
1978 break;
1980 else
1981 did_match = true;
1983 /* This can be fixed with reloads if the operand
1984 we are supposed to match can be fixed with
1985 reloads. */
1986 badop = false;
1987 this_alternative = curr_alt[m];
1988 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1989 winreg = this_alternative != NO_REGS;
1990 break;
1993 case 'g':
1994 if (MEM_P (op)
1995 || general_constant_p (op)
1996 || spilled_pseudo_p (op))
1997 win = true;
1998 cl = GENERAL_REGS;
1999 goto reg;
2001 default:
2002 cn = lookup_constraint (p);
2003 switch (get_constraint_type (cn))
2005 case CT_REGISTER:
2006 cl = reg_class_for_constraint (cn);
2007 if (cl != NO_REGS)
2008 goto reg;
2009 break;
2011 case CT_CONST_INT:
2012 if (CONST_INT_P (op)
2013 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2014 win = true;
2015 break;
2017 case CT_MEMORY:
2018 if (MEM_P (op)
2019 && satisfies_memory_constraint_p (op, cn))
2020 win = true;
2021 else if (spilled_pseudo_p (op))
2022 win = true;
2024 /* If we didn't already win, we can reload constants
2025 via force_const_mem or put the pseudo value into
2026 memory, or make other memory by reloading the
2027 address like for 'o'. */
2028 if (CONST_POOL_OK_P (mode, op)
2029 || MEM_P (op) || REG_P (op))
2030 badop = false;
2031 constmemok = true;
2032 offmemok = true;
2033 break;
2035 case CT_ADDRESS:
2036 /* If we didn't already win, we can reload the address
2037 into a base register. */
2038 if (satisfies_address_constraint_p (op, cn))
2039 win = true;
2040 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2041 ADDRESS, SCRATCH);
2042 badop = false;
2043 goto reg;
2045 case CT_FIXED_FORM:
2046 if (constraint_satisfied_p (op, cn))
2047 win = true;
2048 break;
2050 break;
2052 reg:
2053 this_alternative = reg_class_subunion[this_alternative][cl];
2054 IOR_HARD_REG_SET (this_alternative_set,
2055 reg_class_contents[cl]);
2056 if (costly_p)
2058 this_costly_alternative
2059 = reg_class_subunion[this_costly_alternative][cl];
2060 IOR_HARD_REG_SET (this_costly_alternative_set,
2061 reg_class_contents[cl]);
2063 if (mode == BLKmode)
2064 break;
2065 winreg = true;
2066 if (REG_P (op))
2068 if (hard_regno[nop] >= 0
2069 && in_hard_reg_set_p (this_alternative_set,
2070 mode, hard_regno[nop]))
2071 win = true;
2072 else if (hard_regno[nop] < 0
2073 && in_class_p (op, this_alternative, NULL))
2074 win = true;
2076 break;
2078 if (c != ' ' && c != '\t')
2079 costly_p = c == '*';
2081 while ((p += len), c);
2083 scratch_p = (operand_reg[nop] != NULL_RTX
2084 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2085 /* Record which operands fit this alternative. */
2086 if (win)
2088 this_alternative_win = true;
2089 if (operand_reg[nop] != NULL_RTX)
2091 if (hard_regno[nop] >= 0)
2093 if (in_hard_reg_set_p (this_costly_alternative_set,
2094 mode, hard_regno[nop]))
2096 if (lra_dump_file != NULL)
2097 fprintf (lra_dump_file,
2098 " %d Costly set: reject++\n",
2099 nop);
2100 reject++;
2103 else
2105 /* Prefer won reg to spilled pseudo under other
2106 equal conditions for possibe inheritance. */
2107 if (! scratch_p)
2109 if (lra_dump_file != NULL)
2110 fprintf
2111 (lra_dump_file,
2112 " %d Non pseudo reload: reject++\n",
2113 nop);
2114 reject++;
2116 if (in_class_p (operand_reg[nop],
2117 this_costly_alternative, NULL))
2119 if (lra_dump_file != NULL)
2120 fprintf
2121 (lra_dump_file,
2122 " %d Non pseudo costly reload:"
2123 " reject++\n",
2124 nop);
2125 reject++;
2128 /* We simulate the behaviour of old reload here.
2129 Although scratches need hard registers and it
2130 might result in spilling other pseudos, no reload
2131 insns are generated for the scratches. So it
2132 might cost something but probably less than old
2133 reload pass believes. */
2134 if (scratch_p)
2136 if (lra_dump_file != NULL)
2137 fprintf (lra_dump_file,
2138 " %d Scratch win: reject+=2\n",
2139 nop);
2140 reject += 2;
2144 else if (did_match)
2145 this_alternative_match_win = true;
2146 else
2148 int const_to_mem = 0;
2149 bool no_regs_p;
2151 reject += op_reject;
2152 /* Never do output reload of stack pointer. It makes
2153 impossible to do elimination when SP is changed in
2154 RTL. */
2155 if (op == stack_pointer_rtx && ! frame_pointer_needed
2156 && curr_static_id->operand[nop].type != OP_IN)
2157 goto fail;
2159 /* If this alternative asks for a specific reg class, see if there
2160 is at least one allocatable register in that class. */
2161 no_regs_p
2162 = (this_alternative == NO_REGS
2163 || (hard_reg_set_subset_p
2164 (reg_class_contents[this_alternative],
2165 lra_no_alloc_regs)));
2167 /* For asms, verify that the class for this alternative is possible
2168 for the mode that is specified. */
2169 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2171 int i;
2172 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2173 if (HARD_REGNO_MODE_OK (i, mode)
2174 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2175 mode, i))
2176 break;
2177 if (i == FIRST_PSEUDO_REGISTER)
2178 winreg = false;
2181 /* If this operand accepts a register, and if the
2182 register class has at least one allocatable register,
2183 then this operand can be reloaded. */
2184 if (winreg && !no_regs_p)
2185 badop = false;
2187 if (badop)
2189 if (lra_dump_file != NULL)
2190 fprintf (lra_dump_file,
2191 " alt=%d: Bad operand -- refuse\n",
2192 nalt);
2193 goto fail;
2196 /* If not assigned pseudo has a class which a subset of
2197 required reg class, it is a less costly alternative
2198 as the pseudo still can get a hard reg of necessary
2199 class. */
2200 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2201 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2202 && ira_class_subset_p[this_alternative][cl])
2204 if (lra_dump_file != NULL)
2205 fprintf
2206 (lra_dump_file,
2207 " %d Super set class reg: reject-=3\n", nop);
2208 reject -= 3;
2211 this_alternative_offmemok = offmemok;
2212 if (this_costly_alternative != NO_REGS)
2214 if (lra_dump_file != NULL)
2215 fprintf (lra_dump_file,
2216 " %d Costly loser: reject++\n", nop);
2217 reject++;
2219 /* If the operand is dying, has a matching constraint,
2220 and satisfies constraints of the matched operand
2221 which failed to satisfy the own constraints, most probably
2222 the reload for this operand will be gone. */
2223 if (this_alternative_matches >= 0
2224 && !curr_alt_win[this_alternative_matches]
2225 && REG_P (op)
2226 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2227 && (hard_regno[nop] >= 0
2228 ? in_hard_reg_set_p (this_alternative_set,
2229 mode, hard_regno[nop])
2230 : in_class_p (op, this_alternative, NULL)))
2232 if (lra_dump_file != NULL)
2233 fprintf
2234 (lra_dump_file,
2235 " %d Dying matched operand reload: reject++\n",
2236 nop);
2237 reject++;
2239 else
2241 /* Strict_low_part requires to reload the register
2242 not the sub-register. In this case we should
2243 check that a final reload hard reg can hold the
2244 value mode. */
2245 if (curr_static_id->operand[nop].strict_low
2246 && REG_P (op)
2247 && hard_regno[nop] < 0
2248 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2249 && ira_class_hard_regs_num[this_alternative] > 0
2250 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2251 [this_alternative][0],
2252 GET_MODE
2253 (*curr_id->operand_loc[nop])))
2255 if (lra_dump_file != NULL)
2256 fprintf
2257 (lra_dump_file,
2258 " alt=%d: Strict low subreg reload -- refuse\n",
2259 nalt);
2260 goto fail;
2262 losers++;
2264 if (operand_reg[nop] != NULL_RTX
2265 /* Output operands and matched input operands are
2266 not inherited. The following conditions do not
2267 exactly describe the previous statement but they
2268 are pretty close. */
2269 && curr_static_id->operand[nop].type != OP_OUT
2270 && (this_alternative_matches < 0
2271 || curr_static_id->operand[nop].type != OP_IN))
2273 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2274 (operand_reg[nop])]
2275 .last_reload);
2277 /* The value of reload_sum has sense only if we
2278 process insns in their order. It happens only on
2279 the first constraints sub-pass when we do most of
2280 reload work. */
2281 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2282 reload_sum += last_reload - bb_reload_num;
2284 /* If this is a constant that is reloaded into the
2285 desired class by copying it to memory first, count
2286 that as another reload. This is consistent with
2287 other code and is required to avoid choosing another
2288 alternative when the constant is moved into memory.
2289 Note that the test here is precisely the same as in
2290 the code below that calls force_const_mem. */
2291 if (CONST_POOL_OK_P (mode, op)
2292 && ((targetm.preferred_reload_class
2293 (op, this_alternative) == NO_REGS)
2294 || no_input_reloads_p))
2296 const_to_mem = 1;
2297 if (! no_regs_p)
2298 losers++;
2301 /* Alternative loses if it requires a type of reload not
2302 permitted for this insn. We can always reload
2303 objects with a REG_UNUSED note. */
2304 if ((curr_static_id->operand[nop].type != OP_IN
2305 && no_output_reloads_p
2306 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2307 || (curr_static_id->operand[nop].type != OP_OUT
2308 && no_input_reloads_p && ! const_to_mem)
2309 || (this_alternative_matches >= 0
2310 && (no_input_reloads_p
2311 || (no_output_reloads_p
2312 && (curr_static_id->operand
2313 [this_alternative_matches].type != OP_IN)
2314 && ! find_reg_note (curr_insn, REG_UNUSED,
2315 no_subreg_reg_operand
2316 [this_alternative_matches])))))
2318 if (lra_dump_file != NULL)
2319 fprintf
2320 (lra_dump_file,
2321 " alt=%d: No input/otput reload -- refuse\n",
2322 nalt);
2323 goto fail;
2326 /* Alternative loses if it required class pseudo can not
2327 hold value of required mode. Such insns can be
2328 described by insn definitions with mode iterators. */
2329 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2330 && ! hard_reg_set_empty_p (this_alternative_set)
2331 /* It is common practice for constraints to use a
2332 class which does not have actually enough regs to
2333 hold the value (e.g. x86 AREG for mode requiring
2334 more one general reg). Therefore we have 2
2335 conditions to check that the reload pseudo can
2336 not hold the mode value. */
2337 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2338 [this_alternative][0],
2339 GET_MODE (*curr_id->operand_loc[nop]))
2340 /* The above condition is not enough as the first
2341 reg in ira_class_hard_regs can be not aligned for
2342 multi-words mode values. */
2343 && (prohibited_class_reg_set_mode_p
2344 (this_alternative, this_alternative_set,
2345 GET_MODE (*curr_id->operand_loc[nop]))))
2347 if (lra_dump_file != NULL)
2348 fprintf (lra_dump_file,
2349 " alt=%d: reload pseudo for op %d "
2350 " can not hold the mode value -- refuse\n",
2351 nalt, nop);
2352 goto fail;
2355 /* Check strong discouragement of reload of non-constant
2356 into class THIS_ALTERNATIVE. */
2357 if (! CONSTANT_P (op) && ! no_regs_p
2358 && (targetm.preferred_reload_class
2359 (op, this_alternative) == NO_REGS
2360 || (curr_static_id->operand[nop].type == OP_OUT
2361 && (targetm.preferred_output_reload_class
2362 (op, this_alternative) == NO_REGS))))
2364 if (lra_dump_file != NULL)
2365 fprintf (lra_dump_file,
2366 " %d Non-prefered reload: reject+=%d\n",
2367 nop, LRA_MAX_REJECT);
2368 reject += LRA_MAX_REJECT;
2371 if (! (MEM_P (op) && offmemok)
2372 && ! (const_to_mem && constmemok))
2374 /* We prefer to reload pseudos over reloading other
2375 things, since such reloads may be able to be
2376 eliminated later. So bump REJECT in other cases.
2377 Don't do this in the case where we are forcing a
2378 constant into memory and it will then win since
2379 we don't want to have a different alternative
2380 match then. */
2381 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2383 if (lra_dump_file != NULL)
2384 fprintf
2385 (lra_dump_file,
2386 " %d Non-pseudo reload: reject+=2\n",
2387 nop);
2388 reject += 2;
2391 if (! no_regs_p)
2392 reload_nregs
2393 += ira_reg_class_max_nregs[this_alternative][mode];
2395 if (SMALL_REGISTER_CLASS_P (this_alternative))
2397 if (lra_dump_file != NULL)
2398 fprintf
2399 (lra_dump_file,
2400 " %d Small class reload: reject+=%d\n",
2401 nop, LRA_LOSER_COST_FACTOR / 2);
2402 reject += LRA_LOSER_COST_FACTOR / 2;
2406 /* We are trying to spill pseudo into memory. It is
2407 usually more costly than moving to a hard register
2408 although it might takes the same number of
2409 reloads. */
2410 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2412 if (lra_dump_file != NULL)
2413 fprintf
2414 (lra_dump_file,
2415 " %d Spill pseudo into memory: reject+=3\n",
2416 nop);
2417 reject += 3;
2418 if (VECTOR_MODE_P (mode))
2420 /* Spilling vectors into memory is usually more
2421 costly as they contain big values. */
2422 if (lra_dump_file != NULL)
2423 fprintf
2424 (lra_dump_file,
2425 " %d Spill vector pseudo: reject+=2\n",
2426 nop);
2427 reject += 2;
2431 #ifdef SECONDARY_MEMORY_NEEDED
2432 /* If reload requires moving value through secondary
2433 memory, it will need one more insn at least. */
2434 if (this_alternative != NO_REGS
2435 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2436 && ((curr_static_id->operand[nop].type != OP_OUT
2437 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2438 GET_MODE (op)))
2439 || (curr_static_id->operand[nop].type != OP_IN
2440 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2441 GET_MODE (op)))))
2442 losers++;
2443 #endif
2444 /* Input reloads can be inherited more often than output
2445 reloads can be removed, so penalize output
2446 reloads. */
2447 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2449 if (lra_dump_file != NULL)
2450 fprintf
2451 (lra_dump_file,
2452 " %d Non input pseudo reload: reject++\n",
2453 nop);
2454 reject++;
2458 if (early_clobber_p && ! scratch_p)
2460 if (lra_dump_file != NULL)
2461 fprintf (lra_dump_file,
2462 " %d Early clobber: reject++\n", nop);
2463 reject++;
2465 /* ??? We check early clobbers after processing all operands
2466 (see loop below) and there we update the costs more.
2467 Should we update the cost (may be approximately) here
2468 because of early clobber register reloads or it is a rare
2469 or non-important thing to be worth to do it. */
2470 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2471 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2473 if (lra_dump_file != NULL)
2474 fprintf (lra_dump_file,
2475 " alt=%d,overall=%d,losers=%d -- refuse\n",
2476 nalt, overall, losers);
2477 goto fail;
2480 curr_alt[nop] = this_alternative;
2481 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2482 curr_alt_win[nop] = this_alternative_win;
2483 curr_alt_match_win[nop] = this_alternative_match_win;
2484 curr_alt_offmemok[nop] = this_alternative_offmemok;
2485 curr_alt_matches[nop] = this_alternative_matches;
2487 if (this_alternative_matches >= 0
2488 && !did_match && !this_alternative_win)
2489 curr_alt_win[this_alternative_matches] = false;
2491 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2492 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2494 if (curr_insn_set != NULL_RTX && n_operands == 2
2495 /* Prevent processing non-move insns. */
2496 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2497 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2498 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2499 && REG_P (no_subreg_reg_operand[0])
2500 && REG_P (no_subreg_reg_operand[1])
2501 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2502 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2503 || (! curr_alt_win[0] && curr_alt_win[1]
2504 && REG_P (no_subreg_reg_operand[1])
2505 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2506 || (curr_alt_win[0] && ! curr_alt_win[1]
2507 && REG_P (no_subreg_reg_operand[0])
2508 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2509 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2510 no_subreg_reg_operand[1])
2511 || (targetm.preferred_reload_class
2512 (no_subreg_reg_operand[1],
2513 (enum reg_class) curr_alt[1]) != NO_REGS))
2514 /* If it is a result of recent elimination in move
2515 insn we can transform it into an add still by
2516 using this alternative. */
2517 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2519 /* We have a move insn and a new reload insn will be similar
2520 to the current insn. We should avoid such situation as it
2521 results in LRA cycling. */
2522 overall += LRA_MAX_REJECT;
2524 ok_p = true;
2525 curr_alt_dont_inherit_ops_num = 0;
2526 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2528 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2529 HARD_REG_SET temp_set;
2531 i = early_clobbered_nops[nop];
2532 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2533 || hard_regno[i] < 0)
2534 continue;
2535 lra_assert (operand_reg[i] != NULL_RTX);
2536 clobbered_hard_regno = hard_regno[i];
2537 CLEAR_HARD_REG_SET (temp_set);
2538 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2539 first_conflict_j = last_conflict_j = -1;
2540 for (j = 0; j < n_operands; j++)
2541 if (j == i
2542 /* We don't want process insides of match_operator and
2543 match_parallel because otherwise we would process
2544 their operands once again generating a wrong
2545 code. */
2546 || curr_static_id->operand[j].is_operator)
2547 continue;
2548 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2549 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2550 continue;
2551 /* If we don't reload j-th operand, check conflicts. */
2552 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2553 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2555 if (first_conflict_j < 0)
2556 first_conflict_j = j;
2557 last_conflict_j = j;
2559 if (last_conflict_j < 0)
2560 continue;
2561 /* If earlyclobber operand conflicts with another
2562 non-matching operand which is actually the same register
2563 as the earlyclobber operand, it is better to reload the
2564 another operand as an operand matching the earlyclobber
2565 operand can be also the same. */
2566 if (first_conflict_j == last_conflict_j
2567 && operand_reg[last_conflict_j]
2568 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2569 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2571 curr_alt_win[last_conflict_j] = false;
2572 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2573 = last_conflict_j;
2574 losers++;
2575 /* Early clobber was already reflected in REJECT. */
2576 lra_assert (reject > 0);
2577 if (lra_dump_file != NULL)
2578 fprintf
2579 (lra_dump_file,
2580 " %d Conflict early clobber reload: reject--\n",
2582 reject--;
2583 overall += LRA_LOSER_COST_FACTOR - 1;
2585 else
2587 /* We need to reload early clobbered register and the
2588 matched registers. */
2589 for (j = 0; j < n_operands; j++)
2590 if (curr_alt_matches[j] == i)
2592 curr_alt_match_win[j] = false;
2593 losers++;
2594 overall += LRA_LOSER_COST_FACTOR;
2596 if (! curr_alt_match_win[i])
2597 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2598 else
2600 /* Remember pseudos used for match reloads are never
2601 inherited. */
2602 lra_assert (curr_alt_matches[i] >= 0);
2603 curr_alt_win[curr_alt_matches[i]] = false;
2605 curr_alt_win[i] = curr_alt_match_win[i] = false;
2606 losers++;
2607 /* Early clobber was already reflected in REJECT. */
2608 lra_assert (reject > 0);
2609 if (lra_dump_file != NULL)
2610 fprintf
2611 (lra_dump_file,
2612 " %d Matched conflict early clobber reloads:"
2613 "reject--\n",
2615 reject--;
2616 overall += LRA_LOSER_COST_FACTOR - 1;
2619 if (lra_dump_file != NULL)
2620 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2621 nalt, overall, losers, reload_nregs);
2623 /* If this alternative can be made to work by reloading, and it
2624 needs less reloading than the others checked so far, record
2625 it as the chosen goal for reloading. */
2626 if ((best_losers != 0 && losers == 0)
2627 || (((best_losers == 0 && losers == 0)
2628 || (best_losers != 0 && losers != 0))
2629 && (best_overall > overall
2630 || (best_overall == overall
2631 /* If the cost of the reloads is the same,
2632 prefer alternative which requires minimal
2633 number of reload regs. */
2634 && (reload_nregs < best_reload_nregs
2635 || (reload_nregs == best_reload_nregs
2636 && (best_reload_sum < reload_sum
2637 || (best_reload_sum == reload_sum
2638 && nalt < goal_alt_number))))))))
2640 for (nop = 0; nop < n_operands; nop++)
2642 goal_alt_win[nop] = curr_alt_win[nop];
2643 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2644 goal_alt_matches[nop] = curr_alt_matches[nop];
2645 goal_alt[nop] = curr_alt[nop];
2646 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2648 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2649 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2650 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2651 goal_alt_swapped = curr_swapped;
2652 best_overall = overall;
2653 best_losers = losers;
2654 best_reload_nregs = reload_nregs;
2655 best_reload_sum = reload_sum;
2656 goal_alt_number = nalt;
2658 if (losers == 0)
2659 /* Everything is satisfied. Do not process alternatives
2660 anymore. */
2661 break;
2662 fail:
2665 return ok_p;
2668 /* Make reload base reg from address AD. */
2669 static rtx
2670 base_to_reg (struct address_info *ad)
2672 enum reg_class cl;
2673 int code = -1;
2674 rtx new_inner = NULL_RTX;
2675 rtx new_reg = NULL_RTX;
2676 rtx_insn *insn;
2677 rtx_insn *last_insn = get_last_insn();
2679 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2680 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2681 get_index_code (ad));
2682 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2683 cl, "base");
2684 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2685 ad->disp_term == NULL
2686 ? gen_int_mode (0, ad->mode)
2687 : *ad->disp_term);
2688 if (!valid_address_p (ad->mode, new_inner, ad->as))
2689 return NULL_RTX;
2690 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base_term));
2691 code = recog_memoized (insn);
2692 if (code < 0)
2694 delete_insns_since (last_insn);
2695 return NULL_RTX;
2698 return new_inner;
2701 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2702 static rtx
2703 base_plus_disp_to_reg (struct address_info *ad)
2705 enum reg_class cl;
2706 rtx new_reg;
2708 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2709 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2710 get_index_code (ad));
2711 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2712 cl, "base + disp");
2713 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2714 return new_reg;
2717 /* Make reload of index part of address AD. Return the new
2718 pseudo. */
2719 static rtx
2720 index_part_to_reg (struct address_info *ad)
2722 rtx new_reg;
2724 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2725 INDEX_REG_CLASS, "index term");
2726 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2727 GEN_INT (get_index_scale (ad)), new_reg, 1);
2728 return new_reg;
2731 /* Return true if we can add a displacement to address AD, even if that
2732 makes the address invalid. The fix-up code requires any new address
2733 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2734 static bool
2735 can_add_disp_p (struct address_info *ad)
2737 return (!ad->autoinc_p
2738 && ad->segment == NULL
2739 && ad->base == ad->base_term
2740 && ad->disp == ad->disp_term);
2743 /* Make equiv substitution in address AD. Return true if a substitution
2744 was made. */
2745 static bool
2746 equiv_address_substitution (struct address_info *ad)
2748 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2749 HOST_WIDE_INT disp, scale;
2750 bool change_p;
2752 base_term = strip_subreg (ad->base_term);
2753 if (base_term == NULL)
2754 base_reg = new_base_reg = NULL_RTX;
2755 else
2757 base_reg = *base_term;
2758 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2760 index_term = strip_subreg (ad->index_term);
2761 if (index_term == NULL)
2762 index_reg = new_index_reg = NULL_RTX;
2763 else
2765 index_reg = *index_term;
2766 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2768 if (base_reg == new_base_reg && index_reg == new_index_reg)
2769 return false;
2770 disp = 0;
2771 change_p = false;
2772 if (lra_dump_file != NULL)
2774 fprintf (lra_dump_file, "Changing address in insn %d ",
2775 INSN_UID (curr_insn));
2776 dump_value_slim (lra_dump_file, *ad->outer, 1);
2778 if (base_reg != new_base_reg)
2780 if (REG_P (new_base_reg))
2782 *base_term = new_base_reg;
2783 change_p = true;
2785 else if (GET_CODE (new_base_reg) == PLUS
2786 && REG_P (XEXP (new_base_reg, 0))
2787 && CONST_INT_P (XEXP (new_base_reg, 1))
2788 && can_add_disp_p (ad))
2790 disp += INTVAL (XEXP (new_base_reg, 1));
2791 *base_term = XEXP (new_base_reg, 0);
2792 change_p = true;
2794 if (ad->base_term2 != NULL)
2795 *ad->base_term2 = *ad->base_term;
2797 if (index_reg != new_index_reg)
2799 if (REG_P (new_index_reg))
2801 *index_term = new_index_reg;
2802 change_p = true;
2804 else if (GET_CODE (new_index_reg) == PLUS
2805 && REG_P (XEXP (new_index_reg, 0))
2806 && CONST_INT_P (XEXP (new_index_reg, 1))
2807 && can_add_disp_p (ad)
2808 && (scale = get_index_scale (ad)))
2810 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2811 *index_term = XEXP (new_index_reg, 0);
2812 change_p = true;
2815 if (disp != 0)
2817 if (ad->disp != NULL)
2818 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2819 else
2821 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2822 update_address (ad);
2824 change_p = true;
2826 if (lra_dump_file != NULL)
2828 if (! change_p)
2829 fprintf (lra_dump_file, " -- no change\n");
2830 else
2832 fprintf (lra_dump_file, " on equiv ");
2833 dump_value_slim (lra_dump_file, *ad->outer, 1);
2834 fprintf (lra_dump_file, "\n");
2837 return change_p;
2840 /* Major function to make reloads for an address in operand NOP or
2841 check its correctness (If CHECK_ONLY_P is true). The supported
2842 cases are:
2844 1) an address that existed before LRA started, at which point it
2845 must have been valid. These addresses are subject to elimination
2846 and may have become invalid due to the elimination offset being out
2847 of range.
2849 2) an address created by forcing a constant to memory
2850 (force_const_to_mem). The initial form of these addresses might
2851 not be valid, and it is this function's job to make them valid.
2853 3) a frame address formed from a register and a (possibly zero)
2854 constant offset. As above, these addresses might not be valid and
2855 this function must make them so.
2857 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2858 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2859 address. Return true for any RTL change.
2861 The function is a helper function which does not produce all
2862 transformations (when CHECK_ONLY_P is false) which can be
2863 necessary. It does just basic steps. To do all necessary
2864 transformations use function process_address. */
2865 static bool
2866 process_address_1 (int nop, bool check_only_p,
2867 rtx_insn **before, rtx_insn **after)
2869 struct address_info ad;
2870 rtx new_reg;
2871 rtx op = *curr_id->operand_loc[nop];
2872 const char *constraint = curr_static_id->operand[nop].constraint;
2873 enum constraint_num cn = lookup_constraint (constraint);
2874 bool change_p = false;
2876 if (insn_extra_address_constraint (cn))
2877 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2878 else if (MEM_P (op))
2879 decompose_mem_address (&ad, op);
2880 else if (GET_CODE (op) == SUBREG
2881 && MEM_P (SUBREG_REG (op)))
2882 decompose_mem_address (&ad, SUBREG_REG (op));
2883 else
2884 return false;
2885 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
2886 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
2887 when INDEX_REG_CLASS is a single register class. */
2888 if (ad.base_term != NULL
2889 && ad.index_term != NULL
2890 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
2891 && REG_P (*ad.base_term)
2892 && REG_P (*ad.index_term)
2893 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
2894 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
2896 std::swap (ad.base, ad.index);
2897 std::swap (ad.base_term, ad.index_term);
2899 if (! check_only_p)
2900 change_p = equiv_address_substitution (&ad);
2901 if (ad.base_term != NULL
2902 && (process_addr_reg
2903 (ad.base_term, check_only_p, before,
2904 (ad.autoinc_p
2905 && !(REG_P (*ad.base_term)
2906 && find_regno_note (curr_insn, REG_DEAD,
2907 REGNO (*ad.base_term)) != NULL_RTX)
2908 ? after : NULL),
2909 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2910 get_index_code (&ad)))))
2912 change_p = true;
2913 if (ad.base_term2 != NULL)
2914 *ad.base_term2 = *ad.base_term;
2916 if (ad.index_term != NULL
2917 && process_addr_reg (ad.index_term, check_only_p,
2918 before, NULL, INDEX_REG_CLASS))
2919 change_p = true;
2921 /* Target hooks sometimes don't treat extra-constraint addresses as
2922 legitimate address_operands, so handle them specially. */
2923 if (insn_extra_address_constraint (cn)
2924 && satisfies_address_constraint_p (&ad, cn))
2925 return change_p;
2927 if (check_only_p)
2928 return change_p;
2930 /* There are three cases where the shape of *AD.INNER may now be invalid:
2932 1) the original address was valid, but either elimination or
2933 equiv_address_substitution was applied and that made
2934 the address invalid.
2936 2) the address is an invalid symbolic address created by
2937 force_const_to_mem.
2939 3) the address is a frame address with an invalid offset.
2941 4) the address is a frame address with an invalid base.
2943 All these cases involve a non-autoinc address, so there is no
2944 point revalidating other types. */
2945 if (ad.autoinc_p || valid_address_p (&ad))
2946 return change_p;
2948 /* Any index existed before LRA started, so we can assume that the
2949 presence and shape of the index is valid. */
2950 push_to_sequence (*before);
2951 lra_assert (ad.disp == ad.disp_term);
2952 if (ad.base == NULL)
2954 if (ad.index == NULL)
2956 int code = -1;
2957 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2958 SCRATCH, SCRATCH);
2959 rtx addr = *ad.inner;
2961 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2962 if (HAVE_lo_sum)
2964 rtx_insn *insn;
2965 rtx_insn *last = get_last_insn ();
2967 /* addr => lo_sum (new_base, addr), case (2) above. */
2968 insn = emit_insn (gen_rtx_SET
2969 (new_reg,
2970 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2971 code = recog_memoized (insn);
2972 if (code >= 0)
2974 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2975 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2977 /* Try to put lo_sum into register. */
2978 insn = emit_insn (gen_rtx_SET
2979 (new_reg,
2980 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2981 code = recog_memoized (insn);
2982 if (code >= 0)
2984 *ad.inner = new_reg;
2985 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2987 *ad.inner = addr;
2988 code = -1;
2994 if (code < 0)
2995 delete_insns_since (last);
2998 if (code < 0)
3000 /* addr => new_base, case (2) above. */
3001 lra_emit_move (new_reg, addr);
3002 *ad.inner = new_reg;
3005 else
3007 /* index * scale + disp => new base + index * scale,
3008 case (1) above. */
3009 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3010 GET_CODE (*ad.index));
3012 lra_assert (INDEX_REG_CLASS != NO_REGS);
3013 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3014 lra_emit_move (new_reg, *ad.disp);
3015 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3016 new_reg, *ad.index);
3019 else if (ad.index == NULL)
3021 int regno;
3022 enum reg_class cl;
3023 rtx set;
3024 rtx_insn *insns, *last_insn;
3025 /* Try to reload base into register only if the base is invalid
3026 for the address but with valid offset, case (4) above. */
3027 start_sequence ();
3028 new_reg = base_to_reg (&ad);
3030 /* base + disp => new base, cases (1) and (3) above. */
3031 /* Another option would be to reload the displacement into an
3032 index register. However, postreload has code to optimize
3033 address reloads that have the same base and different
3034 displacements, so reloading into an index register would
3035 not necessarily be a win. */
3036 if (new_reg == NULL_RTX)
3037 new_reg = base_plus_disp_to_reg (&ad);
3038 insns = get_insns ();
3039 last_insn = get_last_insn ();
3040 /* If we generated at least two insns, try last insn source as
3041 an address. If we succeed, we generate one less insn. */
3042 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3043 && GET_CODE (SET_SRC (set)) == PLUS
3044 && REG_P (XEXP (SET_SRC (set), 0))
3045 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3047 *ad.inner = SET_SRC (set);
3048 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3050 *ad.base_term = XEXP (SET_SRC (set), 0);
3051 *ad.disp_term = XEXP (SET_SRC (set), 1);
3052 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3053 get_index_code (&ad));
3054 regno = REGNO (*ad.base_term);
3055 if (regno >= FIRST_PSEUDO_REGISTER
3056 && cl != lra_get_allocno_class (regno))
3057 lra_change_class (regno, cl, " Change to", true);
3058 new_reg = SET_SRC (set);
3059 delete_insns_since (PREV_INSN (last_insn));
3062 /* Try if target can split displacement into legitimite new disp
3063 and offset. If it's the case, we replace the last insn with
3064 insns for base + offset => new_reg and set new_reg + new disp
3065 to *ad.inner. */
3066 last_insn = get_last_insn ();
3067 if ((set = single_set (last_insn)) != NULL_RTX
3068 && GET_CODE (SET_SRC (set)) == PLUS
3069 && REG_P (XEXP (SET_SRC (set), 0))
3070 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3071 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3073 rtx addend, disp = XEXP (SET_SRC (set), 1);
3074 if (targetm.legitimize_address_displacement (&disp, &addend,
3075 ad.mode))
3077 rtx_insn *new_insns;
3078 start_sequence ();
3079 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3080 new_insns = get_insns ();
3081 end_sequence ();
3082 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3083 delete_insns_since (PREV_INSN (last_insn));
3084 add_insn (new_insns);
3085 insns = get_insns ();
3088 end_sequence ();
3089 emit_insn (insns);
3090 *ad.inner = new_reg;
3092 else if (ad.disp_term != NULL)
3094 /* base + scale * index + disp => new base + scale * index,
3095 case (1) above. */
3096 new_reg = base_plus_disp_to_reg (&ad);
3097 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3098 new_reg, *ad.index);
3100 else if (get_index_scale (&ad) == 1)
3102 /* The last transformation to one reg will be made in
3103 curr_insn_transform function. */
3104 end_sequence ();
3105 return false;
3107 else
3109 /* base + scale * index => base + new_reg,
3110 case (1) above.
3111 Index part of address may become invalid. For example, we
3112 changed pseudo on the equivalent memory and a subreg of the
3113 pseudo onto the memory of different mode for which the scale is
3114 prohibitted. */
3115 new_reg = index_part_to_reg (&ad);
3116 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3117 *ad.base_term, new_reg);
3119 *before = get_insns ();
3120 end_sequence ();
3121 return true;
3124 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3125 Use process_address_1 as a helper function. Return true for any
3126 RTL changes.
3128 If CHECK_ONLY_P is true, just check address correctness. Return
3129 false if the address correct. */
3130 static bool
3131 process_address (int nop, bool check_only_p,
3132 rtx_insn **before, rtx_insn **after)
3134 bool res = false;
3136 while (process_address_1 (nop, check_only_p, before, after))
3138 if (check_only_p)
3139 return true;
3140 res = true;
3142 return res;
3145 /* Emit insns to reload VALUE into a new register. VALUE is an
3146 auto-increment or auto-decrement RTX whose operand is a register or
3147 memory location; so reloading involves incrementing that location.
3148 IN is either identical to VALUE, or some cheaper place to reload
3149 value being incremented/decremented from.
3151 INC_AMOUNT is the number to increment or decrement by (always
3152 positive and ignored for POST_MODIFY/PRE_MODIFY).
3154 Return pseudo containing the result. */
3155 static rtx
3156 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3158 /* REG or MEM to be copied and incremented. */
3159 rtx incloc = XEXP (value, 0);
3160 /* Nonzero if increment after copying. */
3161 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3162 || GET_CODE (value) == POST_MODIFY);
3163 rtx_insn *last;
3164 rtx inc;
3165 rtx_insn *add_insn;
3166 int code;
3167 rtx real_in = in == value ? incloc : in;
3168 rtx result;
3169 bool plus_p = true;
3171 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3173 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3174 || GET_CODE (XEXP (value, 1)) == MINUS);
3175 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3176 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3177 inc = XEXP (XEXP (value, 1), 1);
3179 else
3181 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3182 inc_amount = -inc_amount;
3184 inc = GEN_INT (inc_amount);
3187 if (! post && REG_P (incloc))
3188 result = incloc;
3189 else
3190 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3191 "INC/DEC result");
3193 if (real_in != result)
3195 /* First copy the location to the result register. */
3196 lra_assert (REG_P (result));
3197 emit_insn (gen_move_insn (result, real_in));
3200 /* We suppose that there are insns to add/sub with the constant
3201 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3202 old reload worked with this assumption. If the assumption
3203 becomes wrong, we should use approach in function
3204 base_plus_disp_to_reg. */
3205 if (in == value)
3207 /* See if we can directly increment INCLOC. */
3208 last = get_last_insn ();
3209 add_insn = emit_insn (plus_p
3210 ? gen_add2_insn (incloc, inc)
3211 : gen_sub2_insn (incloc, inc));
3213 code = recog_memoized (add_insn);
3214 if (code >= 0)
3216 if (! post && result != incloc)
3217 emit_insn (gen_move_insn (result, incloc));
3218 return result;
3220 delete_insns_since (last);
3223 /* If couldn't do the increment directly, must increment in RESULT.
3224 The way we do this depends on whether this is pre- or
3225 post-increment. For pre-increment, copy INCLOC to the reload
3226 register, increment it there, then save back. */
3227 if (! post)
3229 if (real_in != result)
3230 emit_insn (gen_move_insn (result, real_in));
3231 if (plus_p)
3232 emit_insn (gen_add2_insn (result, inc));
3233 else
3234 emit_insn (gen_sub2_insn (result, inc));
3235 if (result != incloc)
3236 emit_insn (gen_move_insn (incloc, result));
3238 else
3240 /* Post-increment.
3242 Because this might be a jump insn or a compare, and because
3243 RESULT may not be available after the insn in an input
3244 reload, we must do the incrementing before the insn being
3245 reloaded for.
3247 We have already copied IN to RESULT. Increment the copy in
3248 RESULT, save that back, then decrement RESULT so it has
3249 the original value. */
3250 if (plus_p)
3251 emit_insn (gen_add2_insn (result, inc));
3252 else
3253 emit_insn (gen_sub2_insn (result, inc));
3254 emit_insn (gen_move_insn (incloc, result));
3255 /* Restore non-modified value for the result. We prefer this
3256 way because it does not require an additional hard
3257 register. */
3258 if (plus_p)
3260 if (CONST_INT_P (inc))
3261 emit_insn (gen_add2_insn (result,
3262 gen_int_mode (-INTVAL (inc),
3263 GET_MODE (result))));
3264 else
3265 emit_insn (gen_sub2_insn (result, inc));
3267 else
3268 emit_insn (gen_add2_insn (result, inc));
3270 return result;
3273 /* Return true if the current move insn does not need processing as we
3274 already know that it satisfies its constraints. */
3275 static bool
3276 simple_move_p (void)
3278 rtx dest, src;
3279 enum reg_class dclass, sclass;
3281 lra_assert (curr_insn_set != NULL_RTX);
3282 dest = SET_DEST (curr_insn_set);
3283 src = SET_SRC (curr_insn_set);
3284 return ((dclass = get_op_class (dest)) != NO_REGS
3285 && (sclass = get_op_class (src)) != NO_REGS
3286 /* The backend guarantees that register moves of cost 2
3287 never need reloads. */
3288 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3291 /* Swap operands NOP and NOP + 1. */
3292 static inline void
3293 swap_operands (int nop)
3295 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3296 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3297 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3298 /* Swap the duplicates too. */
3299 lra_update_dup (curr_id, nop);
3300 lra_update_dup (curr_id, nop + 1);
3303 /* Main entry point of the constraint code: search the body of the
3304 current insn to choose the best alternative. It is mimicking insn
3305 alternative cost calculation model of former reload pass. That is
3306 because machine descriptions were written to use this model. This
3307 model can be changed in future. Make commutative operand exchange
3308 if it is chosen.
3310 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3311 constraints. Return true if any change happened during function
3312 call.
3314 If CHECK_ONLY_P is true then don't do any transformation. Just
3315 check that the insn satisfies all constraints. If the insn does
3316 not satisfy any constraint, return true. */
3317 static bool
3318 curr_insn_transform (bool check_only_p)
3320 int i, j, k;
3321 int n_operands;
3322 int n_alternatives;
3323 int commutative;
3324 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3325 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3326 rtx_insn *before, *after;
3327 bool alt_p = false;
3328 /* Flag that the insn has been changed through a transformation. */
3329 bool change_p;
3330 bool sec_mem_p;
3331 #ifdef SECONDARY_MEMORY_NEEDED
3332 bool use_sec_mem_p;
3333 #endif
3334 int max_regno_before;
3335 int reused_alternative_num;
3337 curr_insn_set = single_set (curr_insn);
3338 if (curr_insn_set != NULL_RTX && simple_move_p ())
3339 return false;
3341 no_input_reloads_p = no_output_reloads_p = false;
3342 goal_alt_number = -1;
3343 change_p = sec_mem_p = false;
3344 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3345 reloads; neither are insns that SET cc0. Insns that use CC0 are
3346 not allowed to have any input reloads. */
3347 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3348 no_output_reloads_p = true;
3350 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3351 no_input_reloads_p = true;
3352 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3353 no_output_reloads_p = true;
3355 n_operands = curr_static_id->n_operands;
3356 n_alternatives = curr_static_id->n_alternatives;
3358 /* Just return "no reloads" if insn has no operands with
3359 constraints. */
3360 if (n_operands == 0 || n_alternatives == 0)
3361 return false;
3363 max_regno_before = max_reg_num ();
3365 for (i = 0; i < n_operands; i++)
3367 goal_alt_matched[i][0] = -1;
3368 goal_alt_matches[i] = -1;
3371 commutative = curr_static_id->commutative;
3373 /* Now see what we need for pseudos that didn't get hard regs or got
3374 the wrong kind of hard reg. For this, we must consider all the
3375 operands together against the register constraints. */
3377 best_losers = best_overall = INT_MAX;
3378 best_reload_sum = 0;
3380 curr_swapped = false;
3381 goal_alt_swapped = false;
3383 if (! check_only_p)
3384 /* Make equivalence substitution and memory subreg elimination
3385 before address processing because an address legitimacy can
3386 depend on memory mode. */
3387 for (i = 0; i < n_operands; i++)
3389 rtx op = *curr_id->operand_loc[i];
3390 rtx subst, old = op;
3391 bool op_change_p = false;
3393 if (GET_CODE (old) == SUBREG)
3394 old = SUBREG_REG (old);
3395 subst = get_equiv_with_elimination (old, curr_insn);
3396 original_subreg_reg_mode[i] = VOIDmode;
3397 if (subst != old)
3399 subst = copy_rtx (subst);
3400 lra_assert (REG_P (old));
3401 if (GET_CODE (op) != SUBREG)
3402 *curr_id->operand_loc[i] = subst;
3403 else
3405 SUBREG_REG (op) = subst;
3406 if (GET_MODE (subst) == VOIDmode)
3407 original_subreg_reg_mode[i] = GET_MODE (old);
3409 if (lra_dump_file != NULL)
3411 fprintf (lra_dump_file,
3412 "Changing pseudo %d in operand %i of insn %u on equiv ",
3413 REGNO (old), i, INSN_UID (curr_insn));
3414 dump_value_slim (lra_dump_file, subst, 1);
3415 fprintf (lra_dump_file, "\n");
3417 op_change_p = change_p = true;
3419 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3421 change_p = true;
3422 lra_update_dup (curr_id, i);
3426 /* Reload address registers and displacements. We do it before
3427 finding an alternative because of memory constraints. */
3428 before = after = NULL;
3429 for (i = 0; i < n_operands; i++)
3430 if (! curr_static_id->operand[i].is_operator
3431 && process_address (i, check_only_p, &before, &after))
3433 if (check_only_p)
3434 return true;
3435 change_p = true;
3436 lra_update_dup (curr_id, i);
3439 if (change_p)
3440 /* If we've changed the instruction then any alternative that
3441 we chose previously may no longer be valid. */
3442 lra_set_used_insn_alternative (curr_insn, -1);
3444 if (! check_only_p && curr_insn_set != NULL_RTX
3445 && check_and_process_move (&change_p, &sec_mem_p))
3446 return change_p;
3448 try_swapped:
3450 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3451 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3452 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3453 reused_alternative_num, INSN_UID (curr_insn));
3455 if (process_alt_operands (reused_alternative_num))
3456 alt_p = true;
3458 if (check_only_p)
3459 return ! alt_p || best_losers != 0;
3461 /* If insn is commutative (it's safe to exchange a certain pair of
3462 operands) then we need to try each alternative twice, the second
3463 time matching those two operands as if we had exchanged them. To
3464 do this, really exchange them in operands.
3466 If we have just tried the alternatives the second time, return
3467 operands to normal and drop through. */
3469 if (reused_alternative_num < 0 && commutative >= 0)
3471 curr_swapped = !curr_swapped;
3472 if (curr_swapped)
3474 swap_operands (commutative);
3475 goto try_swapped;
3477 else
3478 swap_operands (commutative);
3481 if (! alt_p && ! sec_mem_p)
3483 /* No alternative works with reloads?? */
3484 if (INSN_CODE (curr_insn) >= 0)
3485 fatal_insn ("unable to generate reloads for:", curr_insn);
3486 error_for_asm (curr_insn,
3487 "inconsistent operand constraints in an %<asm%>");
3488 /* Avoid further trouble with this insn. */
3489 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3490 lra_invalidate_insn_data (curr_insn);
3491 return true;
3494 /* If the best alternative is with operands 1 and 2 swapped, swap
3495 them. Update the operand numbers of any reloads already
3496 pushed. */
3498 if (goal_alt_swapped)
3500 if (lra_dump_file != NULL)
3501 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3502 INSN_UID (curr_insn));
3504 /* Swap the duplicates too. */
3505 swap_operands (commutative);
3506 change_p = true;
3509 #ifdef SECONDARY_MEMORY_NEEDED
3510 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3511 too conservatively. So we use the secondary memory only if there
3512 is no any alternative without reloads. */
3513 use_sec_mem_p = false;
3514 if (! alt_p)
3515 use_sec_mem_p = true;
3516 else if (sec_mem_p)
3518 for (i = 0; i < n_operands; i++)
3519 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3520 break;
3521 use_sec_mem_p = i < n_operands;
3524 if (use_sec_mem_p)
3526 rtx new_reg, src, dest, rld;
3527 machine_mode sec_mode, rld_mode;
3529 lra_assert (sec_mem_p);
3530 lra_assert (curr_static_id->operand[0].type == OP_OUT
3531 && curr_static_id->operand[1].type == OP_IN);
3532 dest = *curr_id->operand_loc[0];
3533 src = *curr_id->operand_loc[1];
3534 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3535 ? dest : src);
3536 rld_mode = GET_MODE (rld);
3537 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3538 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3539 #else
3540 sec_mode = rld_mode;
3541 #endif
3542 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3543 NO_REGS, "secondary");
3544 /* If the mode is changed, it should be wider. */
3545 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3546 if (sec_mode != rld_mode)
3548 /* If the target says specifically to use another mode for
3549 secondary memory moves we can not reuse the original
3550 insn. */
3551 after = emit_spill_move (false, new_reg, dest);
3552 lra_process_new_insns (curr_insn, NULL, after,
3553 "Inserting the sec. move");
3554 /* We may have non null BEFORE here (e.g. after address
3555 processing. */
3556 push_to_sequence (before);
3557 before = emit_spill_move (true, new_reg, src);
3558 emit_insn (before);
3559 before = get_insns ();
3560 end_sequence ();
3561 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3562 lra_set_insn_deleted (curr_insn);
3564 else if (dest == rld)
3566 *curr_id->operand_loc[0] = new_reg;
3567 after = emit_spill_move (false, new_reg, dest);
3568 lra_process_new_insns (curr_insn, NULL, after,
3569 "Inserting the sec. move");
3571 else
3573 *curr_id->operand_loc[1] = new_reg;
3574 /* See comments above. */
3575 push_to_sequence (before);
3576 before = emit_spill_move (true, new_reg, src);
3577 emit_insn (before);
3578 before = get_insns ();
3579 end_sequence ();
3580 lra_process_new_insns (curr_insn, before, NULL,
3581 "Inserting the sec. move");
3583 lra_update_insn_regno_info (curr_insn);
3584 return true;
3586 #endif
3588 lra_assert (goal_alt_number >= 0);
3589 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3591 if (lra_dump_file != NULL)
3593 const char *p;
3595 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3596 goal_alt_number, INSN_UID (curr_insn));
3597 for (i = 0; i < n_operands; i++)
3599 p = (curr_static_id->operand_alternative
3600 [goal_alt_number * n_operands + i].constraint);
3601 if (*p == '\0')
3602 continue;
3603 fprintf (lra_dump_file, " (%d) ", i);
3604 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3605 fputc (*p, lra_dump_file);
3607 if (INSN_CODE (curr_insn) >= 0
3608 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3609 fprintf (lra_dump_file, " {%s}", p);
3610 if (curr_id->sp_offset != 0)
3611 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3612 curr_id->sp_offset);
3613 fprintf (lra_dump_file, "\n");
3616 /* Right now, for any pair of operands I and J that are required to
3617 match, with J < I, goal_alt_matches[I] is J. Add I to
3618 goal_alt_matched[J]. */
3620 for (i = 0; i < n_operands; i++)
3621 if ((j = goal_alt_matches[i]) >= 0)
3623 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3625 /* We allow matching one output operand and several input
3626 operands. */
3627 lra_assert (k == 0
3628 || (curr_static_id->operand[j].type == OP_OUT
3629 && curr_static_id->operand[i].type == OP_IN
3630 && (curr_static_id->operand
3631 [goal_alt_matched[j][0]].type == OP_IN)));
3632 goal_alt_matched[j][k] = i;
3633 goal_alt_matched[j][k + 1] = -1;
3636 for (i = 0; i < n_operands; i++)
3637 goal_alt_win[i] |= goal_alt_match_win[i];
3639 /* Any constants that aren't allowed and can't be reloaded into
3640 registers are here changed into memory references. */
3641 for (i = 0; i < n_operands; i++)
3642 if (goal_alt_win[i])
3644 int regno;
3645 enum reg_class new_class;
3646 rtx reg = *curr_id->operand_loc[i];
3648 if (GET_CODE (reg) == SUBREG)
3649 reg = SUBREG_REG (reg);
3651 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3653 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3655 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3657 lra_assert (ok_p);
3658 lra_change_class (regno, new_class, " Change to", true);
3662 else
3664 const char *constraint;
3665 char c;
3666 rtx op = *curr_id->operand_loc[i];
3667 rtx subreg = NULL_RTX;
3668 machine_mode mode = curr_operand_mode[i];
3670 if (GET_CODE (op) == SUBREG)
3672 subreg = op;
3673 op = SUBREG_REG (op);
3674 mode = GET_MODE (op);
3677 if (CONST_POOL_OK_P (mode, op)
3678 && ((targetm.preferred_reload_class
3679 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3680 || no_input_reloads_p))
3682 rtx tem = force_const_mem (mode, op);
3684 change_p = true;
3685 if (subreg != NULL_RTX)
3686 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3688 *curr_id->operand_loc[i] = tem;
3689 lra_update_dup (curr_id, i);
3690 process_address (i, false, &before, &after);
3692 /* If the alternative accepts constant pool refs directly
3693 there will be no reload needed at all. */
3694 if (subreg != NULL_RTX)
3695 continue;
3696 /* Skip alternatives before the one requested. */
3697 constraint = (curr_static_id->operand_alternative
3698 [goal_alt_number * n_operands + i].constraint);
3699 for (;
3700 (c = *constraint) && c != ',' && c != '#';
3701 constraint += CONSTRAINT_LEN (c, constraint))
3703 enum constraint_num cn = lookup_constraint (constraint);
3704 if (insn_extra_memory_constraint (cn)
3705 && satisfies_memory_constraint_p (tem, cn))
3706 break;
3708 if (c == '\0' || c == ',' || c == '#')
3709 continue;
3711 goal_alt_win[i] = true;
3715 for (i = 0; i < n_operands; i++)
3717 int regno;
3718 bool optional_p = false;
3719 rtx old, new_reg;
3720 rtx op = *curr_id->operand_loc[i];
3722 if (goal_alt_win[i])
3724 if (goal_alt[i] == NO_REGS
3725 && REG_P (op)
3726 /* When we assign NO_REGS it means that we will not
3727 assign a hard register to the scratch pseudo by
3728 assigment pass and the scratch pseudo will be
3729 spilled. Spilled scratch pseudos are transformed
3730 back to scratches at the LRA end. */
3731 && lra_former_scratch_operand_p (curr_insn, i))
3733 int regno = REGNO (op);
3734 lra_change_class (regno, NO_REGS, " Change to", true);
3735 if (lra_get_regno_hard_regno (regno) >= 0)
3736 /* We don't have to mark all insn affected by the
3737 spilled pseudo as there is only one such insn, the
3738 current one. */
3739 reg_renumber[regno] = -1;
3741 /* We can do an optional reload. If the pseudo got a hard
3742 reg, we might improve the code through inheritance. If
3743 it does not get a hard register we coalesce memory/memory
3744 moves later. Ignore move insns to avoid cycling. */
3745 if (! lra_simple_p
3746 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3747 && goal_alt[i] != NO_REGS && REG_P (op)
3748 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3749 && regno < new_regno_start
3750 && ! lra_former_scratch_p (regno)
3751 && reg_renumber[regno] < 0
3752 /* Check that the optional reload pseudo will be able to
3753 hold given mode value. */
3754 && ! (prohibited_class_reg_set_mode_p
3755 (goal_alt[i], reg_class_contents[goal_alt[i]],
3756 PSEUDO_REGNO_MODE (regno)))
3757 && (curr_insn_set == NULL_RTX
3758 || !((REG_P (SET_SRC (curr_insn_set))
3759 || MEM_P (SET_SRC (curr_insn_set))
3760 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3761 && (REG_P (SET_DEST (curr_insn_set))
3762 || MEM_P (SET_DEST (curr_insn_set))
3763 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3764 optional_p = true;
3765 else
3766 continue;
3769 /* Operands that match previous ones have already been handled. */
3770 if (goal_alt_matches[i] >= 0)
3771 continue;
3773 /* We should not have an operand with a non-offsettable address
3774 appearing where an offsettable address will do. It also may
3775 be a case when the address should be special in other words
3776 not a general one (e.g. it needs no index reg). */
3777 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3779 enum reg_class rclass;
3780 rtx *loc = &XEXP (op, 0);
3781 enum rtx_code code = GET_CODE (*loc);
3783 push_to_sequence (before);
3784 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3785 MEM, SCRATCH);
3786 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3787 new_reg = emit_inc (rclass, *loc, *loc,
3788 /* This value does not matter for MODIFY. */
3789 GET_MODE_SIZE (GET_MODE (op)));
3790 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3791 "offsetable address", &new_reg))
3792 lra_emit_move (new_reg, *loc);
3793 before = get_insns ();
3794 end_sequence ();
3795 *loc = new_reg;
3796 lra_update_dup (curr_id, i);
3798 else if (goal_alt_matched[i][0] == -1)
3800 machine_mode mode;
3801 rtx reg, *loc;
3802 int hard_regno, byte;
3803 enum op_type type = curr_static_id->operand[i].type;
3805 loc = curr_id->operand_loc[i];
3806 mode = curr_operand_mode[i];
3807 if (GET_CODE (*loc) == SUBREG)
3809 reg = SUBREG_REG (*loc);
3810 byte = SUBREG_BYTE (*loc);
3811 if (REG_P (reg)
3812 /* Strict_low_part requires reload the register not
3813 the sub-register. */
3814 && (curr_static_id->operand[i].strict_low
3815 || (GET_MODE_SIZE (mode)
3816 <= GET_MODE_SIZE (GET_MODE (reg))
3817 && (hard_regno
3818 = get_try_hard_regno (REGNO (reg))) >= 0
3819 && (simplify_subreg_regno
3820 (hard_regno,
3821 GET_MODE (reg), byte, mode) < 0)
3822 && (goal_alt[i] == NO_REGS
3823 || (simplify_subreg_regno
3824 (ira_class_hard_regs[goal_alt[i]][0],
3825 GET_MODE (reg), byte, mode) >= 0)))))
3827 if (type == OP_OUT)
3828 type = OP_INOUT;
3829 loc = &SUBREG_REG (*loc);
3830 mode = GET_MODE (*loc);
3833 old = *loc;
3834 if (get_reload_reg (type, mode, old, goal_alt[i],
3835 loc != curr_id->operand_loc[i], "", &new_reg)
3836 && type != OP_OUT)
3838 push_to_sequence (before);
3839 lra_emit_move (new_reg, old);
3840 before = get_insns ();
3841 end_sequence ();
3843 *loc = new_reg;
3844 if (type != OP_IN
3845 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3847 start_sequence ();
3848 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3849 emit_insn (after);
3850 after = get_insns ();
3851 end_sequence ();
3852 *loc = new_reg;
3854 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3855 if (goal_alt_dont_inherit_ops[j] == i)
3857 lra_set_regno_unique_value (REGNO (new_reg));
3858 break;
3860 lra_update_dup (curr_id, i);
3862 else if (curr_static_id->operand[i].type == OP_IN
3863 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3864 == OP_OUT))
3866 /* generate reloads for input and matched outputs. */
3867 match_inputs[0] = i;
3868 match_inputs[1] = -1;
3869 match_reload (goal_alt_matched[i][0], match_inputs,
3870 goal_alt[i], &before, &after);
3872 else if (curr_static_id->operand[i].type == OP_OUT
3873 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3874 == OP_IN))
3875 /* Generate reloads for output and matched inputs. */
3876 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3877 else if (curr_static_id->operand[i].type == OP_IN
3878 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3879 == OP_IN))
3881 /* Generate reloads for matched inputs. */
3882 match_inputs[0] = i;
3883 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3884 match_inputs[j + 1] = k;
3885 match_inputs[j + 1] = -1;
3886 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3888 else
3889 /* We must generate code in any case when function
3890 process_alt_operands decides that it is possible. */
3891 gcc_unreachable ();
3892 if (optional_p)
3894 lra_assert (REG_P (op));
3895 regno = REGNO (op);
3896 op = *curr_id->operand_loc[i]; /* Substitution. */
3897 if (GET_CODE (op) == SUBREG)
3898 op = SUBREG_REG (op);
3899 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3900 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3901 lra_reg_info[REGNO (op)].restore_regno = regno;
3902 if (lra_dump_file != NULL)
3903 fprintf (lra_dump_file,
3904 " Making reload reg %d for reg %d optional\n",
3905 REGNO (op), regno);
3908 if (before != NULL_RTX || after != NULL_RTX
3909 || max_regno_before != max_reg_num ())
3910 change_p = true;
3911 if (change_p)
3913 lra_update_operator_dups (curr_id);
3914 /* Something changes -- process the insn. */
3915 lra_update_insn_regno_info (curr_insn);
3917 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3918 return change_p;
3921 /* Return true if INSN satisfies all constraints. In other words, no
3922 reload insns are needed. */
3923 bool
3924 lra_constrain_insn (rtx_insn *insn)
3926 int saved_new_regno_start = new_regno_start;
3927 int saved_new_insn_uid_start = new_insn_uid_start;
3928 bool change_p;
3930 curr_insn = insn;
3931 curr_id = lra_get_insn_recog_data (curr_insn);
3932 curr_static_id = curr_id->insn_static_data;
3933 new_insn_uid_start = get_max_uid ();
3934 new_regno_start = max_reg_num ();
3935 change_p = curr_insn_transform (true);
3936 new_regno_start = saved_new_regno_start;
3937 new_insn_uid_start = saved_new_insn_uid_start;
3938 return ! change_p;
3941 /* Return true if X is in LIST. */
3942 static bool
3943 in_list_p (rtx x, rtx list)
3945 for (; list != NULL_RTX; list = XEXP (list, 1))
3946 if (XEXP (list, 0) == x)
3947 return true;
3948 return false;
3951 /* Return true if X contains an allocatable hard register (if
3952 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3953 static bool
3954 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3956 int i, j;
3957 const char *fmt;
3958 enum rtx_code code;
3960 code = GET_CODE (x);
3961 if (REG_P (x))
3963 int regno = REGNO (x);
3964 HARD_REG_SET alloc_regs;
3966 if (hard_reg_p)
3968 if (regno >= FIRST_PSEUDO_REGISTER)
3969 regno = lra_get_regno_hard_regno (regno);
3970 if (regno < 0)
3971 return false;
3972 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3973 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3975 else
3977 if (regno < FIRST_PSEUDO_REGISTER)
3978 return false;
3979 if (! spilled_p)
3980 return true;
3981 return lra_get_regno_hard_regno (regno) < 0;
3984 fmt = GET_RTX_FORMAT (code);
3985 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3987 if (fmt[i] == 'e')
3989 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3990 return true;
3992 else if (fmt[i] == 'E')
3994 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3995 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3996 return true;
3999 return false;
4002 /* Return true if X contains a symbol reg. */
4003 static bool
4004 contains_symbol_ref_p (rtx x)
4006 int i, j;
4007 const char *fmt;
4008 enum rtx_code code;
4010 code = GET_CODE (x);
4011 if (code == SYMBOL_REF)
4012 return true;
4013 fmt = GET_RTX_FORMAT (code);
4014 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4016 if (fmt[i] == 'e')
4018 if (contains_symbol_ref_p (XEXP (x, i)))
4019 return true;
4021 else if (fmt[i] == 'E')
4023 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4024 if (contains_symbol_ref_p (XVECEXP (x, i, j)))
4025 return true;
4028 return false;
4031 /* Process all regs in location *LOC and change them on equivalent
4032 substitution. Return true if any change was done. */
4033 static bool
4034 loc_equivalence_change_p (rtx *loc)
4036 rtx subst, reg, x = *loc;
4037 bool result = false;
4038 enum rtx_code code = GET_CODE (x);
4039 const char *fmt;
4040 int i, j;
4042 if (code == SUBREG)
4044 reg = SUBREG_REG (x);
4045 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4046 && GET_MODE (subst) == VOIDmode)
4048 /* We cannot reload debug location. Simplify subreg here
4049 while we know the inner mode. */
4050 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4051 GET_MODE (reg), SUBREG_BYTE (x));
4052 return true;
4055 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4057 *loc = subst;
4058 return true;
4061 /* Scan all the operand sub-expressions. */
4062 fmt = GET_RTX_FORMAT (code);
4063 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4065 if (fmt[i] == 'e')
4066 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4067 else if (fmt[i] == 'E')
4068 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4069 result
4070 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4072 return result;
4075 /* Similar to loc_equivalence_change_p, but for use as
4076 simplify_replace_fn_rtx callback. DATA is insn for which the
4077 elimination is done. If it null we don't do the elimination. */
4078 static rtx
4079 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4081 if (!REG_P (loc))
4082 return NULL_RTX;
4084 rtx subst = (data == NULL
4085 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4086 if (subst != loc)
4087 return subst;
4089 return NULL_RTX;
4092 /* Maximum number of generated reload insns per an insn. It is for
4093 preventing this pass cycling in a bug case. */
4094 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4096 /* The current iteration number of this LRA pass. */
4097 int lra_constraint_iter;
4099 /* True if we substituted equiv which needs checking register
4100 allocation correctness because the equivalent value contains
4101 allocatable hard registers or when we restore multi-register
4102 pseudo. */
4103 bool lra_risky_transformations_p;
4105 /* Return true if REGNO is referenced in more than one block. */
4106 static bool
4107 multi_block_pseudo_p (int regno)
4109 basic_block bb = NULL;
4110 unsigned int uid;
4111 bitmap_iterator bi;
4113 if (regno < FIRST_PSEUDO_REGISTER)
4114 return false;
4116 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4117 if (bb == NULL)
4118 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4119 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4120 return true;
4121 return false;
4124 /* Return true if LIST contains a deleted insn. */
4125 static bool
4126 contains_deleted_insn_p (rtx_insn_list *list)
4128 for (; list != NULL_RTX; list = list->next ())
4129 if (NOTE_P (list->insn ())
4130 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4131 return true;
4132 return false;
4135 /* Return true if X contains a pseudo dying in INSN. */
4136 static bool
4137 dead_pseudo_p (rtx x, rtx_insn *insn)
4139 int i, j;
4140 const char *fmt;
4141 enum rtx_code code;
4143 if (REG_P (x))
4144 return (insn != NULL_RTX
4145 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4146 code = GET_CODE (x);
4147 fmt = GET_RTX_FORMAT (code);
4148 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4150 if (fmt[i] == 'e')
4152 if (dead_pseudo_p (XEXP (x, i), insn))
4153 return true;
4155 else if (fmt[i] == 'E')
4157 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4158 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4159 return true;
4162 return false;
4165 /* Return true if INSN contains a dying pseudo in INSN right hand
4166 side. */
4167 static bool
4168 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4170 rtx set = single_set (insn);
4172 gcc_assert (set != NULL);
4173 return dead_pseudo_p (SET_SRC (set), insn);
4176 /* Return true if any init insn of REGNO contains a dying pseudo in
4177 insn right hand side. */
4178 static bool
4179 init_insn_rhs_dead_pseudo_p (int regno)
4181 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4183 if (insns == NULL)
4184 return false;
4185 for (; insns != NULL_RTX; insns = insns->next ())
4186 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4187 return true;
4188 return false;
4191 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4192 reverse only if we have one init insn with given REGNO as a
4193 source. */
4194 static bool
4195 reverse_equiv_p (int regno)
4197 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4198 rtx set;
4200 if (insns == NULL)
4201 return false;
4202 if (! INSN_P (insns->insn ())
4203 || insns->next () != NULL)
4204 return false;
4205 if ((set = single_set (insns->insn ())) == NULL_RTX)
4206 return false;
4207 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4210 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4211 call this function only for non-reverse equivalence. */
4212 static bool
4213 contains_reloaded_insn_p (int regno)
4215 rtx set;
4216 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4218 for (; list != NULL; list = list->next ())
4219 if ((set = single_set (list->insn ())) == NULL_RTX
4220 || ! REG_P (SET_DEST (set))
4221 || (int) REGNO (SET_DEST (set)) != regno)
4222 return true;
4223 return false;
4226 /* Entry function of LRA constraint pass. Return true if the
4227 constraint pass did change the code. */
4228 bool
4229 lra_constraints (bool first_p)
4231 bool changed_p;
4232 int i, hard_regno, new_insns_num;
4233 unsigned int min_len, new_min_len, uid;
4234 rtx set, x, reg, dest_reg;
4235 basic_block last_bb;
4236 bitmap_head equiv_insn_bitmap;
4237 bitmap_iterator bi;
4239 lra_constraint_iter++;
4240 if (lra_dump_file != NULL)
4241 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4242 lra_constraint_iter);
4243 changed_p = false;
4244 if (pic_offset_table_rtx
4245 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4246 lra_risky_transformations_p = true;
4247 else
4248 lra_risky_transformations_p = false;
4249 new_insn_uid_start = get_max_uid ();
4250 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4251 /* Mark used hard regs for target stack size calulations. */
4252 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4253 if (lra_reg_info[i].nrefs != 0
4254 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4256 int j, nregs;
4258 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4259 for (j = 0; j < nregs; j++)
4260 df_set_regs_ever_live (hard_regno + j, true);
4262 /* Do elimination before the equivalence processing as we can spill
4263 some pseudos during elimination. */
4264 lra_eliminate (false, first_p);
4265 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4266 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4267 if (lra_reg_info[i].nrefs != 0)
4269 ira_reg_equiv[i].profitable_p = true;
4270 reg = regno_reg_rtx[i];
4271 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4273 bool pseudo_p = contains_reg_p (x, false, false);
4275 /* After RTL transformation, we can not guarantee that
4276 pseudo in the substitution was not reloaded which might
4277 make equivalence invalid. For example, in reverse
4278 equiv of p0
4280 p0 <- ...
4282 equiv_mem <- p0
4284 the memory address register was reloaded before the 2nd
4285 insn. */
4286 if ((! first_p && pseudo_p)
4287 /* We don't use DF for compilation speed sake. So it
4288 is problematic to update live info when we use an
4289 equivalence containing pseudos in more than one
4290 BB. */
4291 || (pseudo_p && multi_block_pseudo_p (i))
4292 /* If an init insn was deleted for some reason, cancel
4293 the equiv. We could update the equiv insns after
4294 transformations including an equiv insn deletion
4295 but it is not worthy as such cases are extremely
4296 rare. */
4297 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4298 /* If it is not a reverse equivalence, we check that a
4299 pseudo in rhs of the init insn is not dying in the
4300 insn. Otherwise, the live info at the beginning of
4301 the corresponding BB might be wrong after we
4302 removed the insn. When the equiv can be a
4303 constant, the right hand side of the init insn can
4304 be a pseudo. */
4305 || (! reverse_equiv_p (i)
4306 && (init_insn_rhs_dead_pseudo_p (i)
4307 /* If we reloaded the pseudo in an equivalence
4308 init insn, we can not remove the equiv init
4309 insns and the init insns might write into
4310 const memory in this case. */
4311 || contains_reloaded_insn_p (i)))
4312 /* Prevent access beyond equivalent memory for
4313 paradoxical subregs. */
4314 || (MEM_P (x)
4315 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4316 > GET_MODE_SIZE (GET_MODE (x))))
4317 || (pic_offset_table_rtx
4318 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4319 && (targetm.preferred_reload_class
4320 (x, lra_get_allocno_class (i)) == NO_REGS))
4321 || contains_symbol_ref_p (x))))
4322 ira_reg_equiv[i].defined_p = false;
4323 if (contains_reg_p (x, false, true))
4324 ira_reg_equiv[i].profitable_p = false;
4325 if (get_equiv (reg) != reg)
4326 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4329 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4330 update_equiv (i);
4331 /* We should add all insns containing pseudos which should be
4332 substituted by their equivalences. */
4333 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4334 lra_push_insn_by_uid (uid);
4335 min_len = lra_insn_stack_length ();
4336 new_insns_num = 0;
4337 last_bb = NULL;
4338 changed_p = false;
4339 while ((new_min_len = lra_insn_stack_length ()) != 0)
4341 curr_insn = lra_pop_insn ();
4342 --new_min_len;
4343 curr_bb = BLOCK_FOR_INSN (curr_insn);
4344 if (curr_bb != last_bb)
4346 last_bb = curr_bb;
4347 bb_reload_num = lra_curr_reload_num;
4349 if (min_len > new_min_len)
4351 min_len = new_min_len;
4352 new_insns_num = 0;
4354 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4355 internal_error
4356 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4357 MAX_RELOAD_INSNS_NUMBER);
4358 new_insns_num++;
4359 if (DEBUG_INSN_P (curr_insn))
4361 /* We need to check equivalence in debug insn and change
4362 pseudo to the equivalent value if necessary. */
4363 curr_id = lra_get_insn_recog_data (curr_insn);
4364 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4366 rtx old = *curr_id->operand_loc[0];
4367 *curr_id->operand_loc[0]
4368 = simplify_replace_fn_rtx (old, NULL_RTX,
4369 loc_equivalence_callback, curr_insn);
4370 if (old != *curr_id->operand_loc[0])
4372 lra_update_insn_regno_info (curr_insn);
4373 changed_p = true;
4377 else if (INSN_P (curr_insn))
4379 if ((set = single_set (curr_insn)) != NULL_RTX)
4381 dest_reg = SET_DEST (set);
4382 /* The equivalence pseudo could be set up as SUBREG in a
4383 case when it is a call restore insn in a mode
4384 different from the pseudo mode. */
4385 if (GET_CODE (dest_reg) == SUBREG)
4386 dest_reg = SUBREG_REG (dest_reg);
4387 if ((REG_P (dest_reg)
4388 && (x = get_equiv (dest_reg)) != dest_reg
4389 /* Remove insns which set up a pseudo whose value
4390 can not be changed. Such insns might be not in
4391 init_insns because we don't update equiv data
4392 during insn transformations.
4394 As an example, let suppose that a pseudo got
4395 hard register and on the 1st pass was not
4396 changed to equivalent constant. We generate an
4397 additional insn setting up the pseudo because of
4398 secondary memory movement. Then the pseudo is
4399 spilled and we use the equiv constant. In this
4400 case we should remove the additional insn and
4401 this insn is not init_insns list. */
4402 && (! MEM_P (x) || MEM_READONLY_P (x)
4403 /* Check that this is actually an insn setting
4404 up the equivalence. */
4405 || in_list_p (curr_insn,
4406 ira_reg_equiv
4407 [REGNO (dest_reg)].init_insns)))
4408 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4409 && in_list_p (curr_insn,
4410 ira_reg_equiv
4411 [REGNO (SET_SRC (set))].init_insns)))
4413 /* This is equiv init insn of pseudo which did not get a
4414 hard register -- remove the insn. */
4415 if (lra_dump_file != NULL)
4417 fprintf (lra_dump_file,
4418 " Removing equiv init insn %i (freq=%d)\n",
4419 INSN_UID (curr_insn),
4420 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4421 dump_insn_slim (lra_dump_file, curr_insn);
4423 if (contains_reg_p (x, true, false))
4424 lra_risky_transformations_p = true;
4425 lra_set_insn_deleted (curr_insn);
4426 continue;
4429 curr_id = lra_get_insn_recog_data (curr_insn);
4430 curr_static_id = curr_id->insn_static_data;
4431 init_curr_insn_input_reloads ();
4432 init_curr_operand_mode ();
4433 if (curr_insn_transform (false))
4434 changed_p = true;
4435 /* Check non-transformed insns too for equiv change as USE
4436 or CLOBBER don't need reloads but can contain pseudos
4437 being changed on their equivalences. */
4438 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4439 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4441 lra_update_insn_regno_info (curr_insn);
4442 changed_p = true;
4446 bitmap_clear (&equiv_insn_bitmap);
4447 /* If we used a new hard regno, changed_p should be true because the
4448 hard reg is assigned to a new pseudo. */
4449 #ifdef ENABLE_CHECKING
4450 if (! changed_p)
4452 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4453 if (lra_reg_info[i].nrefs != 0
4454 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4456 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4458 for (j = 0; j < nregs; j++)
4459 lra_assert (df_regs_ever_live_p (hard_regno + j));
4462 #endif
4463 return changed_p;
4466 /* Initiate the LRA constraint pass. It is done once per
4467 function. */
4468 void
4469 lra_constraints_init (void)
4473 /* Finalize the LRA constraint pass. It is done once per
4474 function. */
4475 void
4476 lra_constraints_finish (void)
4482 /* This page contains code to do inheritance/split
4483 transformations. */
4485 /* Number of reloads passed so far in current EBB. */
4486 static int reloads_num;
4488 /* Number of calls passed so far in current EBB. */
4489 static int calls_num;
4491 /* Current reload pseudo check for validity of elements in
4492 USAGE_INSNS. */
4493 static int curr_usage_insns_check;
4495 /* Info about last usage of registers in EBB to do inheritance/split
4496 transformation. Inheritance transformation is done from a spilled
4497 pseudo and split transformations from a hard register or a pseudo
4498 assigned to a hard register. */
4499 struct usage_insns
4501 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4502 value INSNS is valid. The insns is chain of optional debug insns
4503 and a finishing non-debug insn using the corresponding reg. The
4504 value is also used to mark the registers which are set up in the
4505 current insn. The negated insn uid is used for this. */
4506 int check;
4507 /* Value of global reloads_num at the last insn in INSNS. */
4508 int reloads_num;
4509 /* Value of global reloads_nums at the last insn in INSNS. */
4510 int calls_num;
4511 /* It can be true only for splitting. And it means that the restore
4512 insn should be put after insn given by the following member. */
4513 bool after_p;
4514 /* Next insns in the current EBB which use the original reg and the
4515 original reg value is not changed between the current insn and
4516 the next insns. In order words, e.g. for inheritance, if we need
4517 to use the original reg value again in the next insns we can try
4518 to use the value in a hard register from a reload insn of the
4519 current insn. */
4520 rtx insns;
4523 /* Map: regno -> corresponding pseudo usage insns. */
4524 static struct usage_insns *usage_insns;
4526 static void
4527 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4529 usage_insns[regno].check = curr_usage_insns_check;
4530 usage_insns[regno].insns = insn;
4531 usage_insns[regno].reloads_num = reloads_num;
4532 usage_insns[regno].calls_num = calls_num;
4533 usage_insns[regno].after_p = after_p;
4536 /* The function is used to form list REGNO usages which consists of
4537 optional debug insns finished by a non-debug insn using REGNO.
4538 RELOADS_NUM is current number of reload insns processed so far. */
4539 static void
4540 add_next_usage_insn (int regno, rtx insn, int reloads_num)
4542 rtx next_usage_insns;
4544 if (usage_insns[regno].check == curr_usage_insns_check
4545 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4546 && DEBUG_INSN_P (insn))
4548 /* Check that we did not add the debug insn yet. */
4549 if (next_usage_insns != insn
4550 && (GET_CODE (next_usage_insns) != INSN_LIST
4551 || XEXP (next_usage_insns, 0) != insn))
4552 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4553 next_usage_insns);
4555 else if (NONDEBUG_INSN_P (insn))
4556 setup_next_usage_insn (regno, insn, reloads_num, false);
4557 else
4558 usage_insns[regno].check = 0;
4561 /* Return first non-debug insn in list USAGE_INSNS. */
4562 static rtx_insn *
4563 skip_usage_debug_insns (rtx usage_insns)
4565 rtx insn;
4567 /* Skip debug insns. */
4568 for (insn = usage_insns;
4569 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4570 insn = XEXP (insn, 1))
4572 return safe_as_a <rtx_insn *> (insn);
4575 /* Return true if we need secondary memory moves for insn in
4576 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4577 into the insn. */
4578 static bool
4579 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4580 rtx usage_insns ATTRIBUTE_UNUSED)
4582 #ifndef SECONDARY_MEMORY_NEEDED
4583 return false;
4584 #else
4585 rtx_insn *insn;
4586 rtx set, dest;
4587 enum reg_class cl;
4589 if (inher_cl == ALL_REGS
4590 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4591 return false;
4592 lra_assert (INSN_P (insn));
4593 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4594 return false;
4595 dest = SET_DEST (set);
4596 if (! REG_P (dest))
4597 return false;
4598 lra_assert (inher_cl != NO_REGS);
4599 cl = get_reg_class (REGNO (dest));
4600 return (cl != NO_REGS && cl != ALL_REGS
4601 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4602 #endif
4605 /* Registers involved in inheritance/split in the current EBB
4606 (inheritance/split pseudos and original registers). */
4607 static bitmap_head check_only_regs;
4609 /* Do inheritance transformations for insn INSN, which defines (if
4610 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4611 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4612 form as the "insns" field of usage_insns. Return true if we
4613 succeed in such transformation.
4615 The transformations look like:
4617 p <- ... i <- ...
4618 ... p <- i (new insn)
4619 ... =>
4620 <- ... p ... <- ... i ...
4622 ... i <- p (new insn)
4623 <- ... p ... <- ... i ...
4624 ... =>
4625 <- ... p ... <- ... i ...
4626 where p is a spilled original pseudo and i is a new inheritance pseudo.
4629 The inheritance pseudo has the smallest class of two classes CL and
4630 class of ORIGINAL REGNO. */
4631 static bool
4632 inherit_reload_reg (bool def_p, int original_regno,
4633 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4635 if (optimize_function_for_size_p (cfun))
4636 return false;
4638 enum reg_class rclass = lra_get_allocno_class (original_regno);
4639 rtx original_reg = regno_reg_rtx[original_regno];
4640 rtx new_reg, usage_insn;
4641 rtx_insn *new_insns;
4643 lra_assert (! usage_insns[original_regno].after_p);
4644 if (lra_dump_file != NULL)
4645 fprintf (lra_dump_file,
4646 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4647 if (! ira_reg_classes_intersect_p[cl][rclass])
4649 if (lra_dump_file != NULL)
4651 fprintf (lra_dump_file,
4652 " Rejecting inheritance for %d "
4653 "because of disjoint classes %s and %s\n",
4654 original_regno, reg_class_names[cl],
4655 reg_class_names[rclass]);
4656 fprintf (lra_dump_file,
4657 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4659 return false;
4661 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4662 /* We don't use a subset of two classes because it can be
4663 NO_REGS. This transformation is still profitable in most
4664 cases even if the classes are not intersected as register
4665 move is probably cheaper than a memory load. */
4666 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4668 if (lra_dump_file != NULL)
4669 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4670 reg_class_names[cl], reg_class_names[rclass]);
4672 rclass = cl;
4674 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4676 /* Reject inheritance resulting in secondary memory moves.
4677 Otherwise, there is a danger in LRA cycling. Also such
4678 transformation will be unprofitable. */
4679 if (lra_dump_file != NULL)
4681 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4682 rtx set = single_set (insn);
4684 lra_assert (set != NULL_RTX);
4686 rtx dest = SET_DEST (set);
4688 lra_assert (REG_P (dest));
4689 fprintf (lra_dump_file,
4690 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4691 "as secondary mem is needed\n",
4692 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4693 original_regno, reg_class_names[rclass]);
4694 fprintf (lra_dump_file,
4695 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4697 return false;
4699 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4700 rclass, "inheritance");
4701 start_sequence ();
4702 if (def_p)
4703 lra_emit_move (original_reg, new_reg);
4704 else
4705 lra_emit_move (new_reg, original_reg);
4706 new_insns = get_insns ();
4707 end_sequence ();
4708 if (NEXT_INSN (new_insns) != NULL_RTX)
4710 if (lra_dump_file != NULL)
4712 fprintf (lra_dump_file,
4713 " Rejecting inheritance %d->%d "
4714 "as it results in 2 or more insns:\n",
4715 original_regno, REGNO (new_reg));
4716 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4717 fprintf (lra_dump_file,
4718 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4720 return false;
4722 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
4723 lra_update_insn_regno_info (insn);
4724 if (! def_p)
4725 /* We now have a new usage insn for original regno. */
4726 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4727 if (lra_dump_file != NULL)
4728 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4729 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4730 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4731 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4732 bitmap_set_bit (&check_only_regs, original_regno);
4733 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4734 if (def_p)
4735 lra_process_new_insns (insn, NULL, new_insns,
4736 "Add original<-inheritance");
4737 else
4738 lra_process_new_insns (insn, new_insns, NULL,
4739 "Add inheritance<-original");
4740 while (next_usage_insns != NULL_RTX)
4742 if (GET_CODE (next_usage_insns) != INSN_LIST)
4744 usage_insn = next_usage_insns;
4745 lra_assert (NONDEBUG_INSN_P (usage_insn));
4746 next_usage_insns = NULL;
4748 else
4750 usage_insn = XEXP (next_usage_insns, 0);
4751 lra_assert (DEBUG_INSN_P (usage_insn));
4752 next_usage_insns = XEXP (next_usage_insns, 1);
4754 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
4755 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4756 if (lra_dump_file != NULL)
4758 fprintf (lra_dump_file,
4759 " Inheritance reuse change %d->%d (bb%d):\n",
4760 original_regno, REGNO (new_reg),
4761 BLOCK_FOR_INSN (usage_insn)->index);
4762 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
4765 if (lra_dump_file != NULL)
4766 fprintf (lra_dump_file,
4767 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4768 return true;
4771 /* Return true if we need a caller save/restore for pseudo REGNO which
4772 was assigned to a hard register. */
4773 static inline bool
4774 need_for_call_save_p (int regno)
4776 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4777 return (usage_insns[regno].calls_num < calls_num
4778 && (overlaps_hard_reg_set_p
4779 ((flag_ipa_ra &&
4780 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4781 ? lra_reg_info[regno].actual_call_used_reg_set
4782 : call_used_reg_set,
4783 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4784 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4785 PSEUDO_REGNO_MODE (regno))));
4788 /* Global registers occurring in the current EBB. */
4789 static bitmap_head ebb_global_regs;
4791 /* Return true if we need a split for hard register REGNO or pseudo
4792 REGNO which was assigned to a hard register.
4793 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4794 used for reloads since the EBB end. It is an approximation of the
4795 used hard registers in the split range. The exact value would
4796 require expensive calculations. If we were aggressive with
4797 splitting because of the approximation, the split pseudo will save
4798 the same hard register assignment and will be removed in the undo
4799 pass. We still need the approximation because too aggressive
4800 splitting would result in too inaccurate cost calculation in the
4801 assignment pass because of too many generated moves which will be
4802 probably removed in the undo pass. */
4803 static inline bool
4804 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4806 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4808 lra_assert (hard_regno >= 0);
4809 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4810 /* Don't split eliminable hard registers, otherwise we can
4811 split hard registers like hard frame pointer, which
4812 lives on BB start/end according to DF-infrastructure,
4813 when there is a pseudo assigned to the register and
4814 living in the same BB. */
4815 && (regno >= FIRST_PSEUDO_REGISTER
4816 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4817 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4818 /* Don't split call clobbered hard regs living through
4819 calls, otherwise we might have a check problem in the
4820 assign sub-pass as in the most cases (exception is a
4821 situation when lra_risky_transformations_p value is
4822 true) the assign pass assumes that all pseudos living
4823 through calls are assigned to call saved hard regs. */
4824 && (regno >= FIRST_PSEUDO_REGISTER
4825 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4826 || usage_insns[regno].calls_num == calls_num)
4827 /* We need at least 2 reloads to make pseudo splitting
4828 profitable. We should provide hard regno splitting in
4829 any case to solve 1st insn scheduling problem when
4830 moving hard register definition up might result in
4831 impossibility to find hard register for reload pseudo of
4832 small register class. */
4833 && (usage_insns[regno].reloads_num
4834 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4835 && (regno < FIRST_PSEUDO_REGISTER
4836 /* For short living pseudos, spilling + inheritance can
4837 be considered a substitution for splitting.
4838 Therefore we do not splitting for local pseudos. It
4839 decreases also aggressiveness of splitting. The
4840 minimal number of references is chosen taking into
4841 account that for 2 references splitting has no sense
4842 as we can just spill the pseudo. */
4843 || (regno >= FIRST_PSEUDO_REGISTER
4844 && lra_reg_info[regno].nrefs > 3
4845 && bitmap_bit_p (&ebb_global_regs, regno))))
4846 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4849 /* Return class for the split pseudo created from original pseudo with
4850 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4851 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4852 results in no secondary memory movements. */
4853 static enum reg_class
4854 choose_split_class (enum reg_class allocno_class,
4855 int hard_regno ATTRIBUTE_UNUSED,
4856 machine_mode mode ATTRIBUTE_UNUSED)
4858 #ifndef SECONDARY_MEMORY_NEEDED
4859 return allocno_class;
4860 #else
4861 int i;
4862 enum reg_class cl, best_cl = NO_REGS;
4863 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4864 = REGNO_REG_CLASS (hard_regno);
4866 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4867 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4868 return allocno_class;
4869 for (i = 0;
4870 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4871 i++)
4872 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4873 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4874 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4875 && (best_cl == NO_REGS
4876 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4877 best_cl = cl;
4878 return best_cl;
4879 #endif
4882 /* Do split transformations for insn INSN, which defines or uses
4883 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4884 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4885 "insns" field of usage_insns.
4887 The transformations look like:
4889 p <- ... p <- ...
4890 ... s <- p (new insn -- save)
4891 ... =>
4892 ... p <- s (new insn -- restore)
4893 <- ... p ... <- ... p ...
4895 <- ... p ... <- ... p ...
4896 ... s <- p (new insn -- save)
4897 ... =>
4898 ... p <- s (new insn -- restore)
4899 <- ... p ... <- ... p ...
4901 where p is an original pseudo got a hard register or a hard
4902 register and s is a new split pseudo. The save is put before INSN
4903 if BEFORE_P is true. Return true if we succeed in such
4904 transformation. */
4905 static bool
4906 split_reg (bool before_p, int original_regno, rtx_insn *insn,
4907 rtx next_usage_insns)
4909 enum reg_class rclass;
4910 rtx original_reg;
4911 int hard_regno, nregs;
4912 rtx new_reg, usage_insn;
4913 rtx_insn *restore, *save;
4914 bool after_p;
4915 bool call_save_p;
4917 if (original_regno < FIRST_PSEUDO_REGISTER)
4919 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4920 hard_regno = original_regno;
4921 call_save_p = false;
4922 nregs = 1;
4924 else
4926 hard_regno = reg_renumber[original_regno];
4927 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4928 rclass = lra_get_allocno_class (original_regno);
4929 original_reg = regno_reg_rtx[original_regno];
4930 call_save_p = need_for_call_save_p (original_regno);
4932 original_reg = regno_reg_rtx[original_regno];
4933 lra_assert (hard_regno >= 0);
4934 if (lra_dump_file != NULL)
4935 fprintf (lra_dump_file,
4936 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4937 if (call_save_p)
4939 machine_mode mode = GET_MODE (original_reg);
4941 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4942 hard_regno_nregs[hard_regno][mode],
4943 mode);
4944 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4946 else
4948 rclass = choose_split_class (rclass, hard_regno,
4949 GET_MODE (original_reg));
4950 if (rclass == NO_REGS)
4952 if (lra_dump_file != NULL)
4954 fprintf (lra_dump_file,
4955 " Rejecting split of %d(%s): "
4956 "no good reg class for %d(%s)\n",
4957 original_regno,
4958 reg_class_names[lra_get_allocno_class (original_regno)],
4959 hard_regno,
4960 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4961 fprintf
4962 (lra_dump_file,
4963 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4965 return false;
4967 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4968 rclass, "split");
4969 reg_renumber[REGNO (new_reg)] = hard_regno;
4971 save = emit_spill_move (true, new_reg, original_reg);
4972 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
4974 if (lra_dump_file != NULL)
4976 fprintf
4977 (lra_dump_file,
4978 " Rejecting split %d->%d resulting in > 2 save insns:\n",
4979 original_regno, REGNO (new_reg));
4980 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
4981 fprintf (lra_dump_file,
4982 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4984 return false;
4986 restore = emit_spill_move (false, new_reg, original_reg);
4987 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
4989 if (lra_dump_file != NULL)
4991 fprintf (lra_dump_file,
4992 " Rejecting split %d->%d "
4993 "resulting in > 2 restore insns:\n",
4994 original_regno, REGNO (new_reg));
4995 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
4996 fprintf (lra_dump_file,
4997 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4999 return false;
5001 after_p = usage_insns[original_regno].after_p;
5002 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
5003 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5004 bitmap_set_bit (&check_only_regs, original_regno);
5005 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
5006 for (;;)
5008 if (GET_CODE (next_usage_insns) != INSN_LIST)
5010 usage_insn = next_usage_insns;
5011 break;
5013 usage_insn = XEXP (next_usage_insns, 0);
5014 lra_assert (DEBUG_INSN_P (usage_insn));
5015 next_usage_insns = XEXP (next_usage_insns, 1);
5016 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5017 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5018 if (lra_dump_file != NULL)
5020 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5021 original_regno, REGNO (new_reg));
5022 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5025 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5026 lra_assert (usage_insn != insn || (after_p && before_p));
5027 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5028 after_p ? NULL : restore,
5029 after_p ? restore : NULL,
5030 call_save_p
5031 ? "Add reg<-save" : "Add reg<-split");
5032 lra_process_new_insns (insn, before_p ? save : NULL,
5033 before_p ? NULL : save,
5034 call_save_p
5035 ? "Add save<-reg" : "Add split<-reg");
5036 if (nregs > 1)
5037 /* If we are trying to split multi-register. We should check
5038 conflicts on the next assignment sub-pass. IRA can allocate on
5039 sub-register levels, LRA do this on pseudos level right now and
5040 this discrepancy may create allocation conflicts after
5041 splitting. */
5042 lra_risky_transformations_p = true;
5043 if (lra_dump_file != NULL)
5044 fprintf (lra_dump_file,
5045 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5046 return true;
5049 /* Recognize that we need a split transformation for insn INSN, which
5050 defines or uses REGNO in its insn biggest MODE (we use it only if
5051 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5052 hard registers which might be used for reloads since the EBB end.
5053 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5054 uid before starting INSN processing. Return true if we succeed in
5055 such transformation. */
5056 static bool
5057 split_if_necessary (int regno, machine_mode mode,
5058 HARD_REG_SET potential_reload_hard_regs,
5059 bool before_p, rtx_insn *insn, int max_uid)
5061 bool res = false;
5062 int i, nregs = 1;
5063 rtx next_usage_insns;
5065 if (regno < FIRST_PSEUDO_REGISTER)
5066 nregs = hard_regno_nregs[regno][mode];
5067 for (i = 0; i < nregs; i++)
5068 if (usage_insns[regno + i].check == curr_usage_insns_check
5069 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5070 /* To avoid processing the register twice or more. */
5071 && ((GET_CODE (next_usage_insns) != INSN_LIST
5072 && INSN_UID (next_usage_insns) < max_uid)
5073 || (GET_CODE (next_usage_insns) == INSN_LIST
5074 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5075 && need_for_split_p (potential_reload_hard_regs, regno + i)
5076 && split_reg (before_p, regno + i, insn, next_usage_insns))
5077 res = true;
5078 return res;
5081 /* Check only registers living at the current program point in the
5082 current EBB. */
5083 static bitmap_head live_regs;
5085 /* Update live info in EBB given by its HEAD and TAIL insns after
5086 inheritance/split transformation. The function removes dead moves
5087 too. */
5088 static void
5089 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5091 unsigned int j;
5092 int i, regno;
5093 bool live_p;
5094 rtx_insn *prev_insn;
5095 rtx set;
5096 bool remove_p;
5097 basic_block last_bb, prev_bb, curr_bb;
5098 bitmap_iterator bi;
5099 struct lra_insn_reg *reg;
5100 edge e;
5101 edge_iterator ei;
5103 last_bb = BLOCK_FOR_INSN (tail);
5104 prev_bb = NULL;
5105 for (curr_insn = tail;
5106 curr_insn != PREV_INSN (head);
5107 curr_insn = prev_insn)
5109 prev_insn = PREV_INSN (curr_insn);
5110 /* We need to process empty blocks too. They contain
5111 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5112 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5113 continue;
5114 curr_bb = BLOCK_FOR_INSN (curr_insn);
5115 if (curr_bb != prev_bb)
5117 if (prev_bb != NULL)
5119 /* Update df_get_live_in (prev_bb): */
5120 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5121 if (bitmap_bit_p (&live_regs, j))
5122 bitmap_set_bit (df_get_live_in (prev_bb), j);
5123 else
5124 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5126 if (curr_bb != last_bb)
5128 /* Update df_get_live_out (curr_bb): */
5129 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5131 live_p = bitmap_bit_p (&live_regs, j);
5132 if (! live_p)
5133 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5134 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5136 live_p = true;
5137 break;
5139 if (live_p)
5140 bitmap_set_bit (df_get_live_out (curr_bb), j);
5141 else
5142 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5145 prev_bb = curr_bb;
5146 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5148 if (! NONDEBUG_INSN_P (curr_insn))
5149 continue;
5150 curr_id = lra_get_insn_recog_data (curr_insn);
5151 curr_static_id = curr_id->insn_static_data;
5152 remove_p = false;
5153 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
5154 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5155 && bitmap_bit_p (&check_only_regs, regno)
5156 && ! bitmap_bit_p (&live_regs, regno))
5157 remove_p = true;
5158 /* See which defined values die here. */
5159 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5160 if (reg->type == OP_OUT && ! reg->subreg_p)
5161 bitmap_clear_bit (&live_regs, reg->regno);
5162 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5163 if (reg->type == OP_OUT && ! reg->subreg_p)
5164 bitmap_clear_bit (&live_regs, reg->regno);
5165 if (curr_id->arg_hard_regs != NULL)
5166 /* Make clobbered argument hard registers die. */
5167 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5168 if (regno >= FIRST_PSEUDO_REGISTER)
5169 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
5170 /* Mark each used value as live. */
5171 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5172 if (reg->type != OP_OUT
5173 && bitmap_bit_p (&check_only_regs, reg->regno))
5174 bitmap_set_bit (&live_regs, reg->regno);
5175 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5176 if (reg->type != OP_OUT
5177 && bitmap_bit_p (&check_only_regs, reg->regno))
5178 bitmap_set_bit (&live_regs, reg->regno);
5179 if (curr_id->arg_hard_regs != NULL)
5180 /* Make used argument hard registers live. */
5181 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5182 if (regno < FIRST_PSEUDO_REGISTER
5183 && bitmap_bit_p (&check_only_regs, regno))
5184 bitmap_set_bit (&live_regs, regno);
5185 /* It is quite important to remove dead move insns because it
5186 means removing dead store. We don't need to process them for
5187 constraints. */
5188 if (remove_p)
5190 if (lra_dump_file != NULL)
5192 fprintf (lra_dump_file, " Removing dead insn:\n ");
5193 dump_insn_slim (lra_dump_file, curr_insn);
5195 lra_set_insn_deleted (curr_insn);
5200 /* The structure describes info to do an inheritance for the current
5201 insn. We need to collect such info first before doing the
5202 transformations because the transformations change the insn
5203 internal representation. */
5204 struct to_inherit
5206 /* Original regno. */
5207 int regno;
5208 /* Subsequent insns which can inherit original reg value. */
5209 rtx insns;
5212 /* Array containing all info for doing inheritance from the current
5213 insn. */
5214 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5216 /* Number elements in the previous array. */
5217 static int to_inherit_num;
5219 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5220 structure to_inherit. */
5221 static void
5222 add_to_inherit (int regno, rtx insns)
5224 int i;
5226 for (i = 0; i < to_inherit_num; i++)
5227 if (to_inherit[i].regno == regno)
5228 return;
5229 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5230 to_inherit[to_inherit_num].regno = regno;
5231 to_inherit[to_inherit_num++].insns = insns;
5234 /* Return the last non-debug insn in basic block BB, or the block begin
5235 note if none. */
5236 static rtx_insn *
5237 get_last_insertion_point (basic_block bb)
5239 rtx_insn *insn;
5241 FOR_BB_INSNS_REVERSE (bb, insn)
5242 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5243 return insn;
5244 gcc_unreachable ();
5247 /* Set up RES by registers living on edges FROM except the edge (FROM,
5248 TO) or by registers set up in a jump insn in BB FROM. */
5249 static void
5250 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5252 rtx_insn *last;
5253 struct lra_insn_reg *reg;
5254 edge e;
5255 edge_iterator ei;
5257 lra_assert (to != NULL);
5258 bitmap_clear (res);
5259 FOR_EACH_EDGE (e, ei, from->succs)
5260 if (e->dest != to)
5261 bitmap_ior_into (res, df_get_live_in (e->dest));
5262 last = get_last_insertion_point (from);
5263 if (! JUMP_P (last))
5264 return;
5265 curr_id = lra_get_insn_recog_data (last);
5266 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5267 if (reg->type != OP_IN)
5268 bitmap_set_bit (res, reg->regno);
5271 /* Used as a temporary results of some bitmap calculations. */
5272 static bitmap_head temp_bitmap;
5274 /* We split for reloads of small class of hard regs. The following
5275 defines how many hard regs the class should have to be qualified as
5276 small. The code is mostly oriented to x86/x86-64 architecture
5277 where some insns need to use only specific register or pair of
5278 registers and these register can live in RTL explicitly, e.g. for
5279 parameter passing. */
5280 static const int max_small_class_regs_num = 2;
5282 /* Do inheritance/split transformations in EBB starting with HEAD and
5283 finishing on TAIL. We process EBB insns in the reverse order.
5284 Return true if we did any inheritance/split transformation in the
5285 EBB.
5287 We should avoid excessive splitting which results in worse code
5288 because of inaccurate cost calculations for spilling new split
5289 pseudos in such case. To achieve this we do splitting only if
5290 register pressure is high in given basic block and there are reload
5291 pseudos requiring hard registers. We could do more register
5292 pressure calculations at any given program point to avoid necessary
5293 splitting even more but it is to expensive and the current approach
5294 works well enough. */
5295 static bool
5296 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5298 int i, src_regno, dst_regno, nregs;
5299 bool change_p, succ_p, update_reloads_num_p;
5300 rtx_insn *prev_insn, *last_insn;
5301 rtx next_usage_insns, set;
5302 enum reg_class cl;
5303 struct lra_insn_reg *reg;
5304 basic_block last_processed_bb, curr_bb = NULL;
5305 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5306 bitmap to_process;
5307 unsigned int j;
5308 bitmap_iterator bi;
5309 bool head_p, after_p;
5311 change_p = false;
5312 curr_usage_insns_check++;
5313 reloads_num = calls_num = 0;
5314 bitmap_clear (&check_only_regs);
5315 last_processed_bb = NULL;
5316 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5317 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5318 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5319 /* We don't process new insns generated in the loop. */
5320 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5322 prev_insn = PREV_INSN (curr_insn);
5323 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5324 curr_bb = BLOCK_FOR_INSN (curr_insn);
5325 if (last_processed_bb != curr_bb)
5327 /* We are at the end of BB. Add qualified living
5328 pseudos for potential splitting. */
5329 to_process = df_get_live_out (curr_bb);
5330 if (last_processed_bb != NULL)
5332 /* We are somewhere in the middle of EBB. */
5333 get_live_on_other_edges (curr_bb, last_processed_bb,
5334 &temp_bitmap);
5335 to_process = &temp_bitmap;
5337 last_processed_bb = curr_bb;
5338 last_insn = get_last_insertion_point (curr_bb);
5339 after_p = (! JUMP_P (last_insn)
5340 && (! CALL_P (last_insn)
5341 || (find_reg_note (last_insn,
5342 REG_NORETURN, NULL_RTX) == NULL_RTX
5343 && ! SIBLING_CALL_P (last_insn))));
5344 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5345 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5347 if ((int) j >= lra_constraint_new_regno_start)
5348 break;
5349 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5351 if (j < FIRST_PSEUDO_REGISTER)
5352 SET_HARD_REG_BIT (live_hard_regs, j);
5353 else
5354 add_to_hard_reg_set (&live_hard_regs,
5355 PSEUDO_REGNO_MODE (j),
5356 reg_renumber[j]);
5357 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5361 src_regno = dst_regno = -1;
5362 if (NONDEBUG_INSN_P (curr_insn)
5363 && (set = single_set (curr_insn)) != NULL_RTX
5364 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5366 src_regno = REGNO (SET_SRC (set));
5367 dst_regno = REGNO (SET_DEST (set));
5369 update_reloads_num_p = true;
5370 if (src_regno < lra_constraint_new_regno_start
5371 && src_regno >= FIRST_PSEUDO_REGISTER
5372 && reg_renumber[src_regno] < 0
5373 && dst_regno >= lra_constraint_new_regno_start
5374 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5376 /* 'reload_pseudo <- original_pseudo'. */
5377 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5378 reloads_num++;
5379 update_reloads_num_p = false;
5380 succ_p = false;
5381 if (usage_insns[src_regno].check == curr_usage_insns_check
5382 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5383 succ_p = inherit_reload_reg (false, src_regno, cl,
5384 curr_insn, next_usage_insns);
5385 if (succ_p)
5386 change_p = true;
5387 else
5388 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5389 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5390 IOR_HARD_REG_SET (potential_reload_hard_regs,
5391 reg_class_contents[cl]);
5393 else if (src_regno >= lra_constraint_new_regno_start
5394 && dst_regno < lra_constraint_new_regno_start
5395 && dst_regno >= FIRST_PSEUDO_REGISTER
5396 && reg_renumber[dst_regno] < 0
5397 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5398 && usage_insns[dst_regno].check == curr_usage_insns_check
5399 && (next_usage_insns
5400 = usage_insns[dst_regno].insns) != NULL_RTX)
5402 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5403 reloads_num++;
5404 update_reloads_num_p = false;
5405 /* 'original_pseudo <- reload_pseudo'. */
5406 if (! JUMP_P (curr_insn)
5407 && inherit_reload_reg (true, dst_regno, cl,
5408 curr_insn, next_usage_insns))
5409 change_p = true;
5410 /* Invalidate. */
5411 usage_insns[dst_regno].check = 0;
5412 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5413 IOR_HARD_REG_SET (potential_reload_hard_regs,
5414 reg_class_contents[cl]);
5416 else if (INSN_P (curr_insn))
5418 int iter;
5419 int max_uid = get_max_uid ();
5421 curr_id = lra_get_insn_recog_data (curr_insn);
5422 curr_static_id = curr_id->insn_static_data;
5423 to_inherit_num = 0;
5424 /* Process insn definitions. */
5425 for (iter = 0; iter < 2; iter++)
5426 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5427 reg != NULL;
5428 reg = reg->next)
5429 if (reg->type != OP_IN
5430 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5432 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5433 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5434 && usage_insns[dst_regno].check == curr_usage_insns_check
5435 && (next_usage_insns
5436 = usage_insns[dst_regno].insns) != NULL_RTX)
5438 struct lra_insn_reg *r;
5440 for (r = curr_id->regs; r != NULL; r = r->next)
5441 if (r->type != OP_OUT && r->regno == dst_regno)
5442 break;
5443 /* Don't do inheritance if the pseudo is also
5444 used in the insn. */
5445 if (r == NULL)
5446 /* We can not do inheritance right now
5447 because the current insn reg info (chain
5448 regs) can change after that. */
5449 add_to_inherit (dst_regno, next_usage_insns);
5451 /* We can not process one reg twice here because of
5452 usage_insns invalidation. */
5453 if ((dst_regno < FIRST_PSEUDO_REGISTER
5454 || reg_renumber[dst_regno] >= 0)
5455 && ! reg->subreg_p && reg->type != OP_IN)
5457 HARD_REG_SET s;
5459 if (split_if_necessary (dst_regno, reg->biggest_mode,
5460 potential_reload_hard_regs,
5461 false, curr_insn, max_uid))
5462 change_p = true;
5463 CLEAR_HARD_REG_SET (s);
5464 if (dst_regno < FIRST_PSEUDO_REGISTER)
5465 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5466 else
5467 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5468 reg_renumber[dst_regno]);
5469 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5471 /* We should invalidate potential inheritance or
5472 splitting for the current insn usages to the next
5473 usage insns (see code below) as the output pseudo
5474 prevents this. */
5475 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5476 && reg_renumber[dst_regno] < 0)
5477 || (reg->type == OP_OUT && ! reg->subreg_p
5478 && (dst_regno < FIRST_PSEUDO_REGISTER
5479 || reg_renumber[dst_regno] >= 0)))
5481 /* Invalidate and mark definitions. */
5482 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5483 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5484 else
5486 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5487 for (i = 0; i < nregs; i++)
5488 usage_insns[dst_regno + i].check
5489 = -(int) INSN_UID (curr_insn);
5493 /* Process clobbered call regs. */
5494 if (curr_id->arg_hard_regs != NULL)
5495 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5496 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5497 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
5498 = -(int) INSN_UID (curr_insn);
5499 if (! JUMP_P (curr_insn))
5500 for (i = 0; i < to_inherit_num; i++)
5501 if (inherit_reload_reg (true, to_inherit[i].regno,
5502 ALL_REGS, curr_insn,
5503 to_inherit[i].insns))
5504 change_p = true;
5505 if (CALL_P (curr_insn))
5507 rtx cheap, pat, dest;
5508 rtx_insn *restore;
5509 int regno, hard_regno;
5511 calls_num++;
5512 if ((cheap = find_reg_note (curr_insn,
5513 REG_RETURNED, NULL_RTX)) != NULL_RTX
5514 && ((cheap = XEXP (cheap, 0)), true)
5515 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5516 && (hard_regno = reg_renumber[regno]) >= 0
5517 /* If there are pending saves/restores, the
5518 optimization is not worth. */
5519 && usage_insns[regno].calls_num == calls_num - 1
5520 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5522 /* Restore the pseudo from the call result as
5523 REG_RETURNED note says that the pseudo value is
5524 in the call result and the pseudo is an argument
5525 of the call. */
5526 pat = PATTERN (curr_insn);
5527 if (GET_CODE (pat) == PARALLEL)
5528 pat = XVECEXP (pat, 0, 0);
5529 dest = SET_DEST (pat);
5530 /* For multiple return values dest is PARALLEL.
5531 Currently we handle only single return value case. */
5532 if (REG_P (dest))
5534 start_sequence ();
5535 emit_move_insn (cheap, copy_rtx (dest));
5536 restore = get_insns ();
5537 end_sequence ();
5538 lra_process_new_insns (curr_insn, NULL, restore,
5539 "Inserting call parameter restore");
5540 /* We don't need to save/restore of the pseudo from
5541 this call. */
5542 usage_insns[regno].calls_num = calls_num;
5543 bitmap_set_bit (&check_only_regs, regno);
5547 to_inherit_num = 0;
5548 /* Process insn usages. */
5549 for (iter = 0; iter < 2; iter++)
5550 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5551 reg != NULL;
5552 reg = reg->next)
5553 if ((reg->type != OP_OUT
5554 || (reg->type == OP_OUT && reg->subreg_p))
5555 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5557 if (src_regno >= FIRST_PSEUDO_REGISTER
5558 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5560 if (usage_insns[src_regno].check == curr_usage_insns_check
5561 && (next_usage_insns
5562 = usage_insns[src_regno].insns) != NULL_RTX
5563 && NONDEBUG_INSN_P (curr_insn))
5564 add_to_inherit (src_regno, next_usage_insns);
5565 else if (usage_insns[src_regno].check
5566 != -(int) INSN_UID (curr_insn))
5567 /* Add usages but only if the reg is not set up
5568 in the same insn. */
5569 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5571 else if (src_regno < FIRST_PSEUDO_REGISTER
5572 || reg_renumber[src_regno] >= 0)
5574 bool before_p;
5575 rtx_insn *use_insn = curr_insn;
5577 before_p = (JUMP_P (curr_insn)
5578 || (CALL_P (curr_insn) && reg->type == OP_IN));
5579 if (NONDEBUG_INSN_P (curr_insn)
5580 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5581 && split_if_necessary (src_regno, reg->biggest_mode,
5582 potential_reload_hard_regs,
5583 before_p, curr_insn, max_uid))
5585 if (reg->subreg_p)
5586 lra_risky_transformations_p = true;
5587 change_p = true;
5588 /* Invalidate. */
5589 usage_insns[src_regno].check = 0;
5590 if (before_p)
5591 use_insn = PREV_INSN (curr_insn);
5593 if (NONDEBUG_INSN_P (curr_insn))
5595 if (src_regno < FIRST_PSEUDO_REGISTER)
5596 add_to_hard_reg_set (&live_hard_regs,
5597 reg->biggest_mode, src_regno);
5598 else
5599 add_to_hard_reg_set (&live_hard_regs,
5600 PSEUDO_REGNO_MODE (src_regno),
5601 reg_renumber[src_regno]);
5603 add_next_usage_insn (src_regno, use_insn, reloads_num);
5606 /* Process used call regs. */
5607 if (curr_id->arg_hard_regs != NULL)
5608 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5609 if (src_regno < FIRST_PSEUDO_REGISTER)
5611 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5612 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5614 for (i = 0; i < to_inherit_num; i++)
5616 src_regno = to_inherit[i].regno;
5617 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5618 curr_insn, to_inherit[i].insns))
5619 change_p = true;
5620 else
5621 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5624 if (update_reloads_num_p
5625 && NONDEBUG_INSN_P (curr_insn)
5626 && (set = single_set (curr_insn)) != NULL_RTX)
5628 int regno = -1;
5629 if ((REG_P (SET_DEST (set))
5630 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5631 && reg_renumber[regno] < 0
5632 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5633 || (REG_P (SET_SRC (set))
5634 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5635 && reg_renumber[regno] < 0
5636 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5638 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5639 reloads_num++;
5640 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5641 IOR_HARD_REG_SET (potential_reload_hard_regs,
5642 reg_class_contents[cl]);
5645 /* We reached the start of the current basic block. */
5646 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5647 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5649 /* We reached the beginning of the current block -- do
5650 rest of spliting in the current BB. */
5651 to_process = df_get_live_in (curr_bb);
5652 if (BLOCK_FOR_INSN (head) != curr_bb)
5654 /* We are somewhere in the middle of EBB. */
5655 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5656 curr_bb, &temp_bitmap);
5657 to_process = &temp_bitmap;
5659 head_p = true;
5660 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5662 if ((int) j >= lra_constraint_new_regno_start)
5663 break;
5664 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5665 && usage_insns[j].check == curr_usage_insns_check
5666 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5668 if (need_for_split_p (potential_reload_hard_regs, j))
5670 if (lra_dump_file != NULL && head_p)
5672 fprintf (lra_dump_file,
5673 " ----------------------------------\n");
5674 head_p = false;
5676 if (split_reg (false, j, bb_note (curr_bb),
5677 next_usage_insns))
5678 change_p = true;
5680 usage_insns[j].check = 0;
5685 return change_p;
5688 /* This value affects EBB forming. If probability of edge from EBB to
5689 a BB is not greater than the following value, we don't add the BB
5690 to EBB. */
5691 #define EBB_PROBABILITY_CUTOFF \
5692 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
5694 /* Current number of inheritance/split iteration. */
5695 int lra_inheritance_iter;
5697 /* Entry function for inheritance/split pass. */
5698 void
5699 lra_inheritance (void)
5701 int i;
5702 basic_block bb, start_bb;
5703 edge e;
5705 lra_inheritance_iter++;
5706 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5707 return;
5708 timevar_push (TV_LRA_INHERITANCE);
5709 if (lra_dump_file != NULL)
5710 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5711 lra_inheritance_iter);
5712 curr_usage_insns_check = 0;
5713 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5714 for (i = 0; i < lra_constraint_new_regno_start; i++)
5715 usage_insns[i].check = 0;
5716 bitmap_initialize (&check_only_regs, &reg_obstack);
5717 bitmap_initialize (&live_regs, &reg_obstack);
5718 bitmap_initialize (&temp_bitmap, &reg_obstack);
5719 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5720 FOR_EACH_BB_FN (bb, cfun)
5722 start_bb = bb;
5723 if (lra_dump_file != NULL)
5724 fprintf (lra_dump_file, "EBB");
5725 /* Form a EBB starting with BB. */
5726 bitmap_clear (&ebb_global_regs);
5727 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5728 for (;;)
5730 if (lra_dump_file != NULL)
5731 fprintf (lra_dump_file, " %d", bb->index);
5732 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5733 || LABEL_P (BB_HEAD (bb->next_bb)))
5734 break;
5735 e = find_fallthru_edge (bb->succs);
5736 if (! e)
5737 break;
5738 if (e->probability < EBB_PROBABILITY_CUTOFF)
5739 break;
5740 bb = bb->next_bb;
5742 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5743 if (lra_dump_file != NULL)
5744 fprintf (lra_dump_file, "\n");
5745 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5746 /* Remember that the EBB head and tail can change in
5747 inherit_in_ebb. */
5748 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5750 bitmap_clear (&ebb_global_regs);
5751 bitmap_clear (&temp_bitmap);
5752 bitmap_clear (&live_regs);
5753 bitmap_clear (&check_only_regs);
5754 free (usage_insns);
5756 timevar_pop (TV_LRA_INHERITANCE);
5761 /* This page contains code to undo failed inheritance/split
5762 transformations. */
5764 /* Current number of iteration undoing inheritance/split. */
5765 int lra_undo_inheritance_iter;
5767 /* Fix BB live info LIVE after removing pseudos created on pass doing
5768 inheritance/split which are REMOVED_PSEUDOS. */
5769 static void
5770 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5772 unsigned int regno;
5773 bitmap_iterator bi;
5775 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5776 if (bitmap_clear_bit (live, regno))
5777 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5780 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5781 number. */
5782 static int
5783 get_regno (rtx reg)
5785 if (GET_CODE (reg) == SUBREG)
5786 reg = SUBREG_REG (reg);
5787 if (REG_P (reg))
5788 return REGNO (reg);
5789 return -1;
5792 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5793 return true if we did any change. The undo transformations for
5794 inheritance looks like
5795 i <- i2
5796 p <- i => p <- i2
5797 or removing
5798 p <- i, i <- p, and i <- i3
5799 where p is original pseudo from which inheritance pseudo i was
5800 created, i and i3 are removed inheritance pseudos, i2 is another
5801 not removed inheritance pseudo. All split pseudos or other
5802 occurrences of removed inheritance pseudos are changed on the
5803 corresponding original pseudos.
5805 The function also schedules insns changed and created during
5806 inheritance/split pass for processing by the subsequent constraint
5807 pass. */
5808 static bool
5809 remove_inheritance_pseudos (bitmap remove_pseudos)
5811 basic_block bb;
5812 int regno, sregno, prev_sregno, dregno, restore_regno;
5813 rtx set, prev_set;
5814 rtx_insn *prev_insn;
5815 bool change_p, done_p;
5817 change_p = ! bitmap_empty_p (remove_pseudos);
5818 /* We can not finish the function right away if CHANGE_P is true
5819 because we need to marks insns affected by previous
5820 inheritance/split pass for processing by the subsequent
5821 constraint pass. */
5822 FOR_EACH_BB_FN (bb, cfun)
5824 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5825 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5826 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5828 if (! INSN_P (curr_insn))
5829 continue;
5830 done_p = false;
5831 sregno = dregno = -1;
5832 if (change_p && NONDEBUG_INSN_P (curr_insn)
5833 && (set = single_set (curr_insn)) != NULL_RTX)
5835 dregno = get_regno (SET_DEST (set));
5836 sregno = get_regno (SET_SRC (set));
5839 if (sregno >= 0 && dregno >= 0)
5841 if ((bitmap_bit_p (remove_pseudos, sregno)
5842 && (lra_reg_info[sregno].restore_regno == dregno
5843 || (bitmap_bit_p (remove_pseudos, dregno)
5844 && (lra_reg_info[sregno].restore_regno
5845 == lra_reg_info[dregno].restore_regno))))
5846 || (bitmap_bit_p (remove_pseudos, dregno)
5847 && lra_reg_info[dregno].restore_regno == sregno))
5848 /* One of the following cases:
5849 original <- removed inheritance pseudo
5850 removed inherit pseudo <- another removed inherit pseudo
5851 removed inherit pseudo <- original pseudo
5853 removed_split_pseudo <- original_reg
5854 original_reg <- removed_split_pseudo */
5856 if (lra_dump_file != NULL)
5858 fprintf (lra_dump_file, " Removing %s:\n",
5859 bitmap_bit_p (&lra_split_regs, sregno)
5860 || bitmap_bit_p (&lra_split_regs, dregno)
5861 ? "split" : "inheritance");
5862 dump_insn_slim (lra_dump_file, curr_insn);
5864 lra_set_insn_deleted (curr_insn);
5865 done_p = true;
5867 else if (bitmap_bit_p (remove_pseudos, sregno)
5868 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5870 /* Search the following pattern:
5871 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5872 original_pseudo <- inherit_or_split_pseudo1
5873 where the 2nd insn is the current insn and
5874 inherit_or_split_pseudo2 is not removed. If it is found,
5875 change the current insn onto:
5876 original_pseudo <- inherit_or_split_pseudo2. */
5877 for (prev_insn = PREV_INSN (curr_insn);
5878 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5879 prev_insn = PREV_INSN (prev_insn))
5881 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5882 && (prev_set = single_set (prev_insn)) != NULL_RTX
5883 /* There should be no subregs in insn we are
5884 searching because only the original reg might
5885 be in subreg when we changed the mode of
5886 load/store for splitting. */
5887 && REG_P (SET_DEST (prev_set))
5888 && REG_P (SET_SRC (prev_set))
5889 && (int) REGNO (SET_DEST (prev_set)) == sregno
5890 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5891 >= FIRST_PSEUDO_REGISTER)
5892 /* As we consider chain of inheritance or
5893 splitting described in above comment we should
5894 check that sregno and prev_sregno were
5895 inheritance/split pseudos created from the
5896 same original regno. */
5897 && (lra_reg_info[sregno].restore_regno
5898 == lra_reg_info[prev_sregno].restore_regno)
5899 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5901 lra_assert (GET_MODE (SET_SRC (prev_set))
5902 == GET_MODE (regno_reg_rtx[sregno]));
5903 if (GET_CODE (SET_SRC (set)) == SUBREG)
5904 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5905 else
5906 SET_SRC (set) = SET_SRC (prev_set);
5907 /* As we are finishing with processing the insn
5908 here, check the destination too as it might
5909 inheritance pseudo for another pseudo. */
5910 if (bitmap_bit_p (remove_pseudos, dregno)
5911 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
5912 && (restore_regno
5913 = lra_reg_info[dregno].restore_regno) >= 0)
5915 if (GET_CODE (SET_DEST (set)) == SUBREG)
5916 SUBREG_REG (SET_DEST (set))
5917 = regno_reg_rtx[restore_regno];
5918 else
5919 SET_DEST (set) = regno_reg_rtx[restore_regno];
5921 lra_push_insn_and_update_insn_regno_info (curr_insn);
5922 lra_set_used_insn_alternative_by_uid
5923 (INSN_UID (curr_insn), -1);
5924 done_p = true;
5925 if (lra_dump_file != NULL)
5927 fprintf (lra_dump_file, " Change reload insn:\n");
5928 dump_insn_slim (lra_dump_file, curr_insn);
5933 if (! done_p)
5935 struct lra_insn_reg *reg;
5936 bool restored_regs_p = false;
5937 bool kept_regs_p = false;
5939 curr_id = lra_get_insn_recog_data (curr_insn);
5940 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5942 regno = reg->regno;
5943 restore_regno = lra_reg_info[regno].restore_regno;
5944 if (restore_regno >= 0)
5946 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5948 lra_substitute_pseudo_within_insn
5949 (curr_insn, regno, regno_reg_rtx[restore_regno],
5950 false);
5951 restored_regs_p = true;
5953 else
5954 kept_regs_p = true;
5957 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5959 /* The instruction has changed since the previous
5960 constraints pass. */
5961 lra_push_insn_and_update_insn_regno_info (curr_insn);
5962 lra_set_used_insn_alternative_by_uid
5963 (INSN_UID (curr_insn), -1);
5965 else if (restored_regs_p)
5966 /* The instruction has been restored to the form that
5967 it had during the previous constraints pass. */
5968 lra_update_insn_regno_info (curr_insn);
5969 if (restored_regs_p && lra_dump_file != NULL)
5971 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5972 dump_insn_slim (lra_dump_file, curr_insn);
5977 return change_p;
5980 /* If optional reload pseudos failed to get a hard register or was not
5981 inherited, it is better to remove optional reloads. We do this
5982 transformation after undoing inheritance to figure out necessity to
5983 remove optional reloads easier. Return true if we do any
5984 change. */
5985 static bool
5986 undo_optional_reloads (void)
5988 bool change_p, keep_p;
5989 unsigned int regno, uid;
5990 bitmap_iterator bi, bi2;
5991 rtx_insn *insn;
5992 rtx set, src, dest;
5993 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5995 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5996 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5997 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5999 keep_p = false;
6000 /* Keep optional reloads from previous subpasses. */
6001 if (lra_reg_info[regno].restore_regno < 0
6002 /* If the original pseudo changed its allocation, just
6003 removing the optional pseudo is dangerous as the original
6004 pseudo will have longer live range. */
6005 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
6006 keep_p = true;
6007 else if (reg_renumber[regno] >= 0)
6008 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
6010 insn = lra_insn_recog_data[uid]->insn;
6011 if ((set = single_set (insn)) == NULL_RTX)
6012 continue;
6013 src = SET_SRC (set);
6014 dest = SET_DEST (set);
6015 if (! REG_P (src) || ! REG_P (dest))
6016 continue;
6017 if (REGNO (dest) == regno
6018 /* Ignore insn for optional reloads itself. */
6019 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
6020 /* Check only inheritance on last inheritance pass. */
6021 && (int) REGNO (src) >= new_regno_start
6022 /* Check that the optional reload was inherited. */
6023 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6025 keep_p = true;
6026 break;
6029 if (keep_p)
6031 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6032 if (lra_dump_file != NULL)
6033 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6036 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6037 bitmap_initialize (&insn_bitmap, &reg_obstack);
6038 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6040 if (lra_dump_file != NULL)
6041 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6042 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6043 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6045 insn = lra_insn_recog_data[uid]->insn;
6046 if ((set = single_set (insn)) != NULL_RTX)
6048 src = SET_SRC (set);
6049 dest = SET_DEST (set);
6050 if (REG_P (src) && REG_P (dest)
6051 && ((REGNO (src) == regno
6052 && (lra_reg_info[regno].restore_regno
6053 == (int) REGNO (dest)))
6054 || (REGNO (dest) == regno
6055 && (lra_reg_info[regno].restore_regno
6056 == (int) REGNO (src)))))
6058 if (lra_dump_file != NULL)
6060 fprintf (lra_dump_file, " Deleting move %u\n",
6061 INSN_UID (insn));
6062 dump_insn_slim (lra_dump_file, insn);
6064 lra_set_insn_deleted (insn);
6065 continue;
6067 /* We should not worry about generation memory-memory
6068 moves here as if the corresponding inheritance did
6069 not work (inheritance pseudo did not get a hard reg),
6070 we remove the inheritance pseudo and the optional
6071 reload. */
6073 lra_substitute_pseudo_within_insn
6074 (insn, regno, regno_reg_rtx[lra_reg_info[regno].restore_regno],
6075 false);
6076 lra_update_insn_regno_info (insn);
6077 if (lra_dump_file != NULL)
6079 fprintf (lra_dump_file,
6080 " Restoring original insn:\n");
6081 dump_insn_slim (lra_dump_file, insn);
6085 /* Clear restore_regnos. */
6086 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6087 lra_reg_info[regno].restore_regno = -1;
6088 bitmap_clear (&insn_bitmap);
6089 bitmap_clear (&removed_optional_reload_pseudos);
6090 return change_p;
6093 /* Entry function for undoing inheritance/split transformation. Return true
6094 if we did any RTL change in this pass. */
6095 bool
6096 lra_undo_inheritance (void)
6098 unsigned int regno;
6099 int restore_regno, hard_regno;
6100 int n_all_inherit, n_inherit, n_all_split, n_split;
6101 bitmap_head remove_pseudos;
6102 bitmap_iterator bi;
6103 bool change_p;
6105 lra_undo_inheritance_iter++;
6106 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6107 return false;
6108 if (lra_dump_file != NULL)
6109 fprintf (lra_dump_file,
6110 "\n********** Undoing inheritance #%d: **********\n\n",
6111 lra_undo_inheritance_iter);
6112 bitmap_initialize (&remove_pseudos, &reg_obstack);
6113 n_inherit = n_all_inherit = 0;
6114 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6115 if (lra_reg_info[regno].restore_regno >= 0)
6117 n_all_inherit++;
6118 if (reg_renumber[regno] < 0
6119 /* If the original pseudo changed its allocation, just
6120 removing inheritance is dangerous as for changing
6121 allocation we used shorter live-ranges. */
6122 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6123 bitmap_set_bit (&remove_pseudos, regno);
6124 else
6125 n_inherit++;
6127 if (lra_dump_file != NULL && n_all_inherit != 0)
6128 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6129 n_inherit, n_all_inherit,
6130 (double) n_inherit / n_all_inherit * 100);
6131 n_split = n_all_split = 0;
6132 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6133 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6135 n_all_split++;
6136 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6137 ? reg_renumber[restore_regno] : restore_regno);
6138 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6139 bitmap_set_bit (&remove_pseudos, regno);
6140 else
6142 n_split++;
6143 if (lra_dump_file != NULL)
6144 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6145 regno, restore_regno);
6148 if (lra_dump_file != NULL && n_all_split != 0)
6149 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6150 n_split, n_all_split,
6151 (double) n_split / n_all_split * 100);
6152 change_p = remove_inheritance_pseudos (&remove_pseudos);
6153 bitmap_clear (&remove_pseudos);
6154 /* Clear restore_regnos. */
6155 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6156 lra_reg_info[regno].restore_regno = -1;
6157 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6158 lra_reg_info[regno].restore_regno = -1;
6159 change_p = undo_optional_reloads () || change_p;
6160 return change_p;