2016-09-26 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / lra-constraints.c
blobbf5b521f51cd444e6e24064706aac59951db5312
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2016 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains code for 3 passes: constraint pass,
23 inheritance/split pass, and pass for undoing failed inheritance and
24 split.
26 The major goal of constraint pass is to transform RTL to satisfy
27 insn and address constraints by:
28 o choosing insn alternatives;
29 o generating *reload insns* (or reloads in brief) and *reload
30 pseudos* which will get necessary hard registers later;
31 o substituting pseudos with equivalent values and removing the
32 instructions that initialized those pseudos.
34 The constraint pass has biggest and most complicated code in LRA.
35 There are a lot of important details like:
36 o reuse of input reload pseudos to simplify reload pseudo
37 allocations;
38 o some heuristics to choose insn alternative to improve the
39 inheritance;
40 o early clobbers etc.
42 The pass is mimicking former reload pass in alternative choosing
43 because the reload pass is oriented to current machine description
44 model. It might be changed if the machine description model is
45 changed.
47 There is special code for preventing all LRA and this pass cycling
48 in case of bugs.
50 On the first iteration of the pass we process every instruction and
51 choose an alternative for each one. On subsequent iterations we try
52 to avoid reprocessing instructions if we can be sure that the old
53 choice is still valid.
55 The inheritance/spilt pass is to transform code to achieve
56 ineheritance and live range splitting. It is done on backward
57 traversal of EBBs.
59 The inheritance optimization goal is to reuse values in hard
60 registers. There is analogous optimization in old reload pass. The
61 inheritance is achieved by following transformation:
63 reload_p1 <- p reload_p1 <- p
64 ... new_p <- reload_p1
65 ... => ...
66 reload_p2 <- p reload_p2 <- new_p
68 where p is spilled and not changed between the insns. Reload_p1 is
69 also called *original pseudo* and new_p is called *inheritance
70 pseudo*.
72 The subsequent assignment pass will try to assign the same (or
73 another if it is not possible) hard register to new_p as to
74 reload_p1 or reload_p2.
76 If the assignment pass fails to assign a hard register to new_p,
77 this file will undo the inheritance and restore the original code.
78 This is because implementing the above sequence with a spilled
79 new_p would make the code much worse. The inheritance is done in
80 EBB scope. The above is just a simplified example to get an idea
81 of the inheritance as the inheritance is also done for non-reload
82 insns.
84 Splitting (transformation) is also done in EBB scope on the same
85 pass as the inheritance:
87 r <- ... or ... <- r r <- ... or ... <- r
88 ... s <- r (new insn -- save)
89 ... =>
90 ... r <- s (new insn -- restore)
91 ... <- r ... <- r
93 The *split pseudo* s is assigned to the hard register of the
94 original pseudo or hard register r.
96 Splitting is done:
97 o In EBBs with high register pressure for global pseudos (living
98 in at least 2 BBs) and assigned to hard registers when there
99 are more one reloads needing the hard registers;
100 o for pseudos needing save/restore code around calls.
102 If the split pseudo still has the same hard register as the
103 original pseudo after the subsequent assignment pass or the
104 original pseudo was split, the opposite transformation is done on
105 the same pass for undoing inheritance. */
107 #undef REG_OK_STRICT
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "backend.h"
113 #include "target.h"
114 #include "rtl.h"
115 #include "tree.h"
116 #include "predict.h"
117 #include "df.h"
118 #include "tm_p.h"
119 #include "expmed.h"
120 #include "optabs.h"
121 #include "regs.h"
122 #include "ira.h"
123 #include "recog.h"
124 #include "output.h"
125 #include "addresses.h"
126 #include "expr.h"
127 #include "cfgrtl.h"
128 #include "rtl-error.h"
129 #include "params.h"
130 #include "lra.h"
131 #include "lra-int.h"
132 #include "print-rtl.h"
134 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
135 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
136 reload insns. */
137 static int bb_reload_num;
139 /* The current insn being processed and corresponding its single set
140 (NULL otherwise), its data (basic block, the insn data, the insn
141 static data, and the mode of each operand). */
142 static rtx_insn *curr_insn;
143 static rtx curr_insn_set;
144 static basic_block curr_bb;
145 static lra_insn_recog_data_t curr_id;
146 static struct lra_static_insn_data *curr_static_id;
147 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
148 /* Mode of the register substituted by its equivalence with VOIDmode
149 (e.g. constant) and whose subreg is given operand of the current
150 insn. VOIDmode in all other cases. */
151 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
155 /* Start numbers for new registers and insns at the current constraints
156 pass start. */
157 static int new_regno_start;
158 static int new_insn_uid_start;
160 /* If LOC is nonnull, strip any outer subreg from it. */
161 static inline rtx *
162 strip_subreg (rtx *loc)
164 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
167 /* Return hard regno of REGNO or if it is was not assigned to a hard
168 register, use a hard register from its allocno class. */
169 static int
170 get_try_hard_regno (int regno)
172 int hard_regno;
173 enum reg_class rclass;
175 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
176 hard_regno = lra_get_regno_hard_regno (regno);
177 if (hard_regno >= 0)
178 return hard_regno;
179 rclass = lra_get_allocno_class (regno);
180 if (rclass == NO_REGS)
181 return -1;
182 return ira_class_hard_regs[rclass][0];
185 /* Return the hard regno of X after removing its subreg. If X is not
186 a register or a subreg of a register, return -1. If X is a pseudo,
187 use its assignment. If FINAL_P return the final hard regno which will
188 be after elimination. */
189 static int
190 get_hard_regno (rtx x, bool final_p)
192 rtx reg;
193 int hard_regno;
195 reg = x;
196 if (SUBREG_P (x))
197 reg = SUBREG_REG (x);
198 if (! REG_P (reg))
199 return -1;
200 if (! HARD_REGISTER_NUM_P (hard_regno = REGNO (reg)))
201 hard_regno = lra_get_regno_hard_regno (hard_regno);
202 if (hard_regno < 0)
203 return -1;
204 if (final_p)
205 hard_regno = lra_get_elimination_hard_regno (hard_regno);
206 if (SUBREG_P (x))
207 hard_regno += subreg_regno_offset (hard_regno, GET_MODE (reg),
208 SUBREG_BYTE (x), GET_MODE (x));
209 return hard_regno;
212 /* If REGNO is a hard register or has been allocated a hard register,
213 return the class of that register. If REGNO is a reload pseudo
214 created by the current constraints pass, return its allocno class.
215 Return NO_REGS otherwise. */
216 static enum reg_class
217 get_reg_class (int regno)
219 int hard_regno;
221 if (! HARD_REGISTER_NUM_P (hard_regno = regno))
222 hard_regno = lra_get_regno_hard_regno (regno);
223 if (hard_regno >= 0)
225 hard_regno = lra_get_elimination_hard_regno (hard_regno);
226 return REGNO_REG_CLASS (hard_regno);
228 if (regno >= new_regno_start)
229 return lra_get_allocno_class (regno);
230 return NO_REGS;
233 /* Return true if REG satisfies (or will satisfy) reg class constraint
234 CL. Use elimination first if REG is a hard register. If REG is a
235 reload pseudo created by this constraints pass, assume that it will
236 be allocated a hard register from its allocno class, but allow that
237 class to be narrowed to CL if it is currently a superset of CL.
239 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
240 REGNO (reg), or NO_REGS if no change in its class was needed. */
241 static bool
242 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
244 enum reg_class rclass, common_class;
245 machine_mode reg_mode;
246 int class_size, hard_regno, nregs, i, j;
247 int regno = REGNO (reg);
249 if (new_class != NULL)
250 *new_class = NO_REGS;
251 if (regno < FIRST_PSEUDO_REGISTER)
253 rtx final_reg = reg;
254 rtx *final_loc = &final_reg;
256 lra_eliminate_reg_if_possible (final_loc);
257 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
259 reg_mode = GET_MODE (reg);
260 rclass = get_reg_class (regno);
261 if (regno < new_regno_start
262 /* Do not allow the constraints for reload instructions to
263 influence the classes of new pseudos. These reloads are
264 typically moves that have many alternatives, and restricting
265 reload pseudos for one alternative may lead to situations
266 where other reload pseudos are no longer allocatable. */
267 || (INSN_UID (curr_insn) >= new_insn_uid_start
268 && curr_insn_set != NULL
269 && ((OBJECT_P (SET_SRC (curr_insn_set))
270 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
271 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
272 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
273 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
274 /* When we don't know what class will be used finally for reload
275 pseudos, we use ALL_REGS. */
276 return ((regno >= new_regno_start && rclass == ALL_REGS)
277 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
278 && ! hard_reg_set_subset_p (reg_class_contents[cl],
279 lra_no_alloc_regs)));
280 else
282 common_class = ira_reg_class_subset[rclass][cl];
283 if (new_class != NULL)
284 *new_class = common_class;
285 if (hard_reg_set_subset_p (reg_class_contents[common_class],
286 lra_no_alloc_regs))
287 return false;
288 /* Check that there are enough allocatable regs. */
289 class_size = ira_class_hard_regs_num[common_class];
290 for (i = 0; i < class_size; i++)
292 hard_regno = ira_class_hard_regs[common_class][i];
293 nregs = hard_regno_nregs[hard_regno][reg_mode];
294 if (nregs == 1)
295 return true;
296 for (j = 0; j < nregs; j++)
297 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
298 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
299 hard_regno + j))
300 break;
301 if (j >= nregs)
302 return true;
304 return false;
308 /* Return true if REGNO satisfies a memory constraint. */
309 static bool
310 in_mem_p (int regno)
312 return get_reg_class (regno) == NO_REGS;
315 /* Return 1 if ADDR is a valid memory address for mode MODE in address
316 space AS, and check that each pseudo has the proper kind of hard
317 reg. */
318 static int
319 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
320 rtx addr, addr_space_t as)
322 #ifdef GO_IF_LEGITIMATE_ADDRESS
323 lra_assert (ADDR_SPACE_GENERIC_P (as));
324 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
325 return 0;
327 win:
328 return 1;
329 #else
330 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
331 #endif
334 namespace {
335 /* Temporarily eliminates registers in an address (for the lifetime of
336 the object). */
337 class address_eliminator {
338 public:
339 address_eliminator (struct address_info *ad);
340 ~address_eliminator ();
342 private:
343 struct address_info *m_ad;
344 rtx *m_base_loc;
345 rtx m_base_reg;
346 rtx *m_index_loc;
347 rtx m_index_reg;
351 address_eliminator::address_eliminator (struct address_info *ad)
352 : m_ad (ad),
353 m_base_loc (strip_subreg (ad->base_term)),
354 m_base_reg (NULL_RTX),
355 m_index_loc (strip_subreg (ad->index_term)),
356 m_index_reg (NULL_RTX)
358 if (m_base_loc != NULL)
360 m_base_reg = *m_base_loc;
361 lra_eliminate_reg_if_possible (m_base_loc);
362 if (m_ad->base_term2 != NULL)
363 *m_ad->base_term2 = *m_ad->base_term;
365 if (m_index_loc != NULL)
367 m_index_reg = *m_index_loc;
368 lra_eliminate_reg_if_possible (m_index_loc);
372 address_eliminator::~address_eliminator ()
374 if (m_base_loc && *m_base_loc != m_base_reg)
376 *m_base_loc = m_base_reg;
377 if (m_ad->base_term2 != NULL)
378 *m_ad->base_term2 = *m_ad->base_term;
380 if (m_index_loc && *m_index_loc != m_index_reg)
381 *m_index_loc = m_index_reg;
384 /* Return true if the eliminated form of AD is a legitimate target address. */
385 static bool
386 valid_address_p (struct address_info *ad)
388 address_eliminator eliminator (ad);
389 return valid_address_p (ad->mode, *ad->outer, ad->as);
392 /* Return true if the eliminated form of memory reference OP satisfies
393 extra (special) memory constraint CONSTRAINT. */
394 static bool
395 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
397 struct address_info ad;
399 decompose_mem_address (&ad, op);
400 address_eliminator eliminator (&ad);
401 return constraint_satisfied_p (op, constraint);
404 /* Return true if the eliminated form of address AD satisfies extra
405 address constraint CONSTRAINT. */
406 static bool
407 satisfies_address_constraint_p (struct address_info *ad,
408 enum constraint_num constraint)
410 address_eliminator eliminator (ad);
411 return constraint_satisfied_p (*ad->outer, constraint);
414 /* Return true if the eliminated form of address OP satisfies extra
415 address constraint CONSTRAINT. */
416 static bool
417 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
419 struct address_info ad;
421 decompose_lea_address (&ad, &op);
422 return satisfies_address_constraint_p (&ad, constraint);
425 /* Initiate equivalences for LRA. As we keep original equivalences
426 before any elimination, we need to make copies otherwise any change
427 in insns might change the equivalences. */
428 void
429 lra_init_equiv (void)
431 ira_expand_reg_equiv ();
432 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
434 rtx res;
436 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
437 ira_reg_equiv[i].memory = copy_rtx (res);
438 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
439 ira_reg_equiv[i].invariant = copy_rtx (res);
443 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
445 /* Update equivalence for REGNO. We need to this as the equivalence
446 might contain other pseudos which are changed by their
447 equivalences. */
448 static void
449 update_equiv (int regno)
451 rtx x;
453 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
454 ira_reg_equiv[regno].memory
455 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
456 NULL_RTX);
457 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
458 ira_reg_equiv[regno].invariant
459 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
460 NULL_RTX);
463 /* If we have decided to substitute X with another value, return that
464 value, otherwise return X. */
465 static rtx
466 get_equiv (rtx x)
468 int regno;
469 rtx res;
471 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
472 || ! ira_reg_equiv[regno].defined_p
473 || ! ira_reg_equiv[regno].profitable_p
474 || lra_get_regno_hard_regno (regno) >= 0)
475 return x;
476 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
478 if (targetm.cannot_substitute_mem_equiv_p (res))
479 return x;
480 return res;
482 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
483 return res;
484 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
485 return res;
486 gcc_unreachable ();
489 /* If we have decided to substitute X with the equivalent value,
490 return that value after elimination for INSN, otherwise return
491 X. */
492 static rtx
493 get_equiv_with_elimination (rtx x, rtx_insn *insn)
495 rtx res = get_equiv (x);
497 if (x == res || CONSTANT_P (res))
498 return res;
499 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
500 false, false, 0, true);
503 /* Set up curr_operand_mode. */
504 static void
505 init_curr_operand_mode (void)
507 int nop = curr_static_id->n_operands;
508 for (int i = 0; i < nop; i++)
510 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
511 if (mode == VOIDmode)
513 /* The .md mode for address operands is the mode of the
514 addressed value rather than the mode of the address itself. */
515 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
516 mode = Pmode;
517 else
518 mode = curr_static_id->operand[i].mode;
520 curr_operand_mode[i] = mode;
526 /* The page contains code to reuse input reloads. */
528 /* Structure describes input reload of the current insns. */
529 struct input_reload
531 /* Reloaded value. */
532 rtx input;
533 /* Reload pseudo used. */
534 rtx reg;
537 /* The number of elements in the following array. */
538 static int curr_insn_input_reloads_num;
539 /* Array containing info about input reloads. It is used to find the
540 same input reload and reuse the reload pseudo in this case. */
541 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
543 /* Initiate data concerning reuse of input reloads for the current
544 insn. */
545 static void
546 init_curr_insn_input_reloads (void)
548 curr_insn_input_reloads_num = 0;
551 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
552 created input reload pseudo (only if TYPE is not OP_OUT). Don't
553 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
554 wrapped up in SUBREG. The result pseudo is returned through
555 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
556 reused the already created input reload pseudo. Use TITLE to
557 describe new registers for debug purposes. */
558 static bool
559 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
560 enum reg_class rclass, bool in_subreg_p,
561 const char *title, rtx *result_reg)
563 int i, regno;
564 enum reg_class new_class;
566 if (type == OP_OUT)
568 *result_reg
569 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
570 return true;
572 /* Prevent reuse value of expression with side effects,
573 e.g. volatile memory. */
574 if (! side_effects_p (original))
575 for (i = 0; i < curr_insn_input_reloads_num; i++)
576 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
577 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
579 rtx reg = curr_insn_input_reloads[i].reg;
580 regno = REGNO (reg);
581 /* If input is equal to original and both are VOIDmode,
582 GET_MODE (reg) might be still different from mode.
583 Ensure we don't return *result_reg with wrong mode. */
584 if (GET_MODE (reg) != mode)
586 if (in_subreg_p)
587 continue;
588 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
589 continue;
590 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
591 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
592 continue;
594 *result_reg = reg;
595 if (lra_dump_file != NULL)
597 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
598 dump_value_slim (lra_dump_file, original, 1);
600 if (new_class != lra_get_allocno_class (regno))
601 lra_change_class (regno, new_class, ", change to", false);
602 if (lra_dump_file != NULL)
603 fprintf (lra_dump_file, "\n");
604 return false;
606 *result_reg = lra_create_new_reg (mode, original, rclass, title);
607 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
608 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
609 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
610 return true;
615 /* The page contains code to extract memory address parts. */
617 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
618 static inline bool
619 ok_for_index_p_nonstrict (rtx reg)
621 unsigned regno = REGNO (reg);
623 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
626 /* A version of regno_ok_for_base_p for use here, when all pseudos
627 should count as OK. Arguments as for regno_ok_for_base_p. */
628 static inline bool
629 ok_for_base_p_nonstrict (rtx reg, machine_mode mode, addr_space_t as,
630 enum rtx_code outer_code, enum rtx_code index_code)
632 unsigned regno = REGNO (reg);
634 if (regno >= FIRST_PSEUDO_REGISTER)
635 return true;
636 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
641 /* The page contains major code to choose the current insn alternative
642 and generate reloads for it. */
644 /* Return the offset from REGNO of the least significant register
645 in (reg:MODE REGNO).
647 This function is used to tell whether two registers satisfy
648 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
650 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
651 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
653 lra_constraint_offset (int regno, machine_mode mode)
655 lra_assert (regno < FIRST_PSEUDO_REGISTER);
656 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
657 && SCALAR_INT_MODE_P (mode))
658 return hard_regno_nregs[regno][mode] - 1;
659 return 0;
662 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
663 if they are the same hard reg, and has special hacks for
664 auto-increment and auto-decrement. This is specifically intended for
665 process_alt_operands to use in determining whether two operands
666 match. X is the operand whose number is the lower of the two.
668 It is supposed that X is the output operand and Y is the input
669 operand. Y_HARD_REGNO is the final hard regno of register Y or
670 register in subreg Y as we know it now. Otherwise, it is a
671 negative value. */
672 static bool
673 operands_match_p (rtx x, rtx y, int y_hard_regno)
675 int i;
676 RTX_CODE code = GET_CODE (x);
677 const char *fmt;
679 if (x == y)
680 return true;
681 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
682 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
684 int j;
686 i = get_hard_regno (x, false);
687 if (i < 0)
688 goto slow;
690 if ((j = y_hard_regno) < 0)
691 goto slow;
693 i += lra_constraint_offset (i, GET_MODE (x));
694 j += lra_constraint_offset (j, GET_MODE (y));
696 return i == j;
699 /* If two operands must match, because they are really a single
700 operand of an assembler insn, then two post-increments are invalid
701 because the assembler insn would increment only once. On the
702 other hand, a post-increment matches ordinary indexing if the
703 post-increment is the output operand. */
704 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
705 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
707 /* Two pre-increments are invalid because the assembler insn would
708 increment only once. On the other hand, a pre-increment matches
709 ordinary indexing if the pre-increment is the input operand. */
710 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
711 || GET_CODE (y) == PRE_MODIFY)
712 return operands_match_p (x, XEXP (y, 0), -1);
714 slow:
716 if (code == REG && REG_P (y))
717 return REGNO (x) == REGNO (y);
719 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
720 && x == SUBREG_REG (y))
721 return true;
722 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
723 && SUBREG_REG (x) == y)
724 return true;
726 /* Now we have disposed of all the cases in which different rtx
727 codes can match. */
728 if (code != GET_CODE (y))
729 return false;
731 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
732 if (GET_MODE (x) != GET_MODE (y))
733 return false;
735 switch (code)
737 CASE_CONST_UNIQUE:
738 return false;
740 case LABEL_REF:
741 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
742 case SYMBOL_REF:
743 return XSTR (x, 0) == XSTR (y, 0);
745 default:
746 break;
749 /* Compare the elements. If any pair of corresponding elements fail
750 to match, return false for the whole things. */
752 fmt = GET_RTX_FORMAT (code);
753 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
755 int val, j;
756 switch (fmt[i])
758 case 'w':
759 if (XWINT (x, i) != XWINT (y, i))
760 return false;
761 break;
763 case 'i':
764 if (XINT (x, i) != XINT (y, i))
765 return false;
766 break;
768 case 'e':
769 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
770 if (val == 0)
771 return false;
772 break;
774 case '0':
775 break;
777 case 'E':
778 if (XVECLEN (x, i) != XVECLEN (y, i))
779 return false;
780 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
782 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
783 if (val == 0)
784 return false;
786 break;
788 /* It is believed that rtx's at this level will never
789 contain anything but integers and other rtx's, except for
790 within LABEL_REFs and SYMBOL_REFs. */
791 default:
792 gcc_unreachable ();
795 return true;
798 /* True if X is a constant that can be forced into the constant pool.
799 MODE is the mode of the operand, or VOIDmode if not known. */
800 #define CONST_POOL_OK_P(MODE, X) \
801 ((MODE) != VOIDmode \
802 && CONSTANT_P (X) \
803 && GET_CODE (X) != HIGH \
804 && !targetm.cannot_force_const_mem (MODE, X))
806 /* True if C is a non-empty register class that has too few registers
807 to be safely used as a reload target class. */
808 #define SMALL_REGISTER_CLASS_P(C) \
809 (ira_class_hard_regs_num [(C)] == 1 \
810 || (ira_class_hard_regs_num [(C)] >= 1 \
811 && targetm.class_likely_spilled_p (C)))
813 /* If REG is a reload pseudo, try to make its class satisfying CL. */
814 static void
815 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
817 enum reg_class rclass;
819 /* Do not make more accurate class from reloads generated. They are
820 mostly moves with a lot of constraints. Making more accurate
821 class may results in very narrow class and impossibility of find
822 registers for several reloads of one insn. */
823 if (INSN_UID (curr_insn) >= new_insn_uid_start)
824 return;
825 if (GET_CODE (reg) == SUBREG)
826 reg = SUBREG_REG (reg);
827 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
828 return;
829 if (in_class_p (reg, cl, &rclass) && rclass != cl)
830 lra_change_class (REGNO (reg), rclass, " Change to", true);
833 /* Searches X for any reference to a reg with the same value as REGNO,
834 returning the rtx of the reference found if any. Otherwise,
835 returns NULL_RTX. */
836 static rtx
837 regno_val_use_in (unsigned int regno, rtx x)
839 const char *fmt;
840 int i, j;
841 rtx tem;
843 if (REG_P (x) && lra_reg_info[REGNO (x)].val == lra_reg_info[regno].val)
844 return x;
846 fmt = GET_RTX_FORMAT (GET_CODE (x));
847 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
849 if (fmt[i] == 'e')
851 if ((tem = regno_val_use_in (regno, XEXP (x, i))))
852 return tem;
854 else if (fmt[i] == 'E')
855 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
856 if ((tem = regno_val_use_in (regno , XVECEXP (x, i, j))))
857 return tem;
860 return NULL_RTX;
863 /* Generate reloads for matching OUT and INS (array of input operand
864 numbers with end marker -1) with reg class GOAL_CLASS, considering
865 output operands OUTS (similar array to INS) needing to be in different
866 registers. Add input and output reloads correspondingly to the lists
867 *BEFORE and *AFTER. OUT might be negative. In this case we generate
868 input reloads for matched input operands INS. EARLY_CLOBBER_P is a flag
869 that the output operand is early clobbered for chosen alternative. */
870 static void
871 match_reload (signed char out, signed char *ins, signed char *outs,
872 enum reg_class goal_class, rtx_insn **before,
873 rtx_insn **after, bool early_clobber_p)
875 bool out_conflict;
876 int i, in;
877 rtx new_in_reg, new_out_reg, reg;
878 machine_mode inmode, outmode;
879 rtx in_rtx = *curr_id->operand_loc[ins[0]];
880 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
882 inmode = curr_operand_mode[ins[0]];
883 outmode = out < 0 ? inmode : curr_operand_mode[out];
884 push_to_sequence (*before);
885 if (inmode != outmode)
887 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
889 reg = new_in_reg
890 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
891 goal_class, "");
892 if (SCALAR_INT_MODE_P (inmode))
893 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
894 else
895 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
896 LRA_SUBREG_P (new_out_reg) = 1;
897 /* If the input reg is dying here, we can use the same hard
898 register for REG and IN_RTX. We do it only for original
899 pseudos as reload pseudos can die although original
900 pseudos still live where reload pseudos dies. */
901 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
902 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
903 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
905 else
907 reg = new_out_reg
908 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
909 goal_class, "");
910 if (SCALAR_INT_MODE_P (outmode))
911 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
912 else
913 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
914 /* NEW_IN_REG is non-paradoxical subreg. We don't want
915 NEW_OUT_REG living above. We add clobber clause for
916 this. This is just a temporary clobber. We can remove
917 it at the end of LRA work. */
918 rtx_insn *clobber = emit_clobber (new_out_reg);
919 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
920 LRA_SUBREG_P (new_in_reg) = 1;
921 if (GET_CODE (in_rtx) == SUBREG)
923 rtx subreg_reg = SUBREG_REG (in_rtx);
925 /* If SUBREG_REG is dying here and sub-registers IN_RTX
926 and NEW_IN_REG are similar, we can use the same hard
927 register for REG and SUBREG_REG. */
928 if (REG_P (subreg_reg)
929 && (int) REGNO (subreg_reg) < lra_new_regno_start
930 && GET_MODE (subreg_reg) == outmode
931 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
932 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
933 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
937 else
939 /* Pseudos have values -- see comments for lra_reg_info.
940 Different pseudos with the same value do not conflict even if
941 they live in the same place. When we create a pseudo we
942 assign value of original pseudo (if any) from which we
943 created the new pseudo. If we create the pseudo from the
944 input pseudo, the new pseudo will have no conflict with the
945 input pseudo which is wrong when the input pseudo lives after
946 the insn and as the new pseudo value is changed by the insn
947 output. Therefore we create the new pseudo from the output
948 except the case when we have single matched dying input
949 pseudo.
951 We cannot reuse the current output register because we might
952 have a situation like "a <- a op b", where the constraints
953 force the second input operand ("b") to match the output
954 operand ("a"). "b" must then be copied into a new register
955 so that it doesn't clobber the current value of "a".
957 We can not use the same value if the output pseudo is
958 early clobbered or the input pseudo is mentioned in the
959 output, e.g. as an address part in memory, because
960 output reload will actually extend the pseudo liveness.
961 We don't care about eliminable hard regs here as we are
962 interesting only in pseudos. */
964 /* Matching input's register value is the same as one of the other
965 output operand. Output operands in a parallel insn must be in
966 different registers. */
967 out_conflict = false;
968 if (REG_P (in_rtx))
970 for (i = 0; outs[i] >= 0; i++)
972 rtx other_out_rtx = *curr_id->operand_loc[outs[i]];
973 if (REG_P (other_out_rtx)
974 && (regno_val_use_in (REGNO (in_rtx), other_out_rtx)
975 != NULL_RTX))
977 out_conflict = true;
978 break;
983 new_in_reg = new_out_reg
984 = (! early_clobber_p && ins[1] < 0 && REG_P (in_rtx)
985 && (int) REGNO (in_rtx) < lra_new_regno_start
986 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
987 && (out < 0
988 || regno_val_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
989 && !out_conflict
990 ? lra_create_new_reg (inmode, in_rtx, goal_class, "")
991 : lra_create_new_reg_with_unique_value (outmode, out_rtx,
992 goal_class, ""));
994 /* In operand can be got from transformations before processing insn
995 constraints. One example of such transformations is subreg
996 reloading (see function simplify_operand_subreg). The new
997 pseudos created by the transformations might have inaccurate
998 class (ALL_REGS) and we should make their classes more
999 accurate. */
1000 narrow_reload_pseudo_class (in_rtx, goal_class);
1001 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
1002 *before = get_insns ();
1003 end_sequence ();
1004 for (i = 0; (in = ins[i]) >= 0; i++)
1006 lra_assert
1007 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
1008 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
1009 *curr_id->operand_loc[in] = new_in_reg;
1011 lra_update_dups (curr_id, ins);
1012 if (out < 0)
1013 return;
1014 /* See a comment for the input operand above. */
1015 narrow_reload_pseudo_class (out_rtx, goal_class);
1016 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
1018 start_sequence ();
1019 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
1020 emit_insn (*after);
1021 *after = get_insns ();
1022 end_sequence ();
1024 *curr_id->operand_loc[out] = new_out_reg;
1025 lra_update_dup (curr_id, out);
1028 /* Return register class which is union of all reg classes in insn
1029 constraint alternative string starting with P. */
1030 static enum reg_class
1031 reg_class_from_constraints (const char *p)
1033 int c, len;
1034 enum reg_class op_class = NO_REGS;
1037 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1039 case '#':
1040 case ',':
1041 return op_class;
1043 case 'g':
1044 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1045 break;
1047 default:
1048 enum constraint_num cn = lookup_constraint (p);
1049 enum reg_class cl = reg_class_for_constraint (cn);
1050 if (cl == NO_REGS)
1052 if (insn_extra_address_constraint (cn))
1053 op_class
1054 = (reg_class_subunion
1055 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1056 ADDRESS, SCRATCH)]);
1057 break;
1060 op_class = reg_class_subunion[op_class][cl];
1061 break;
1063 while ((p += len), c);
1064 return op_class;
1067 /* If OP is a register, return the class of the register as per
1068 get_reg_class, otherwise return NO_REGS. */
1069 static inline enum reg_class
1070 get_op_class (rtx op)
1072 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1075 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1076 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1077 SUBREG for VAL to make them equal. */
1078 static rtx_insn *
1079 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1081 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1083 /* Usually size of mem_pseudo is greater than val size but in
1084 rare cases it can be less as it can be defined by target
1085 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1086 if (! MEM_P (val))
1088 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1089 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1091 LRA_SUBREG_P (val) = 1;
1093 else
1095 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1096 LRA_SUBREG_P (mem_pseudo) = 1;
1099 return to_p ? gen_move_insn (mem_pseudo, val)
1100 : gen_move_insn (val, mem_pseudo);
1103 /* Process a special case insn (register move), return true if we
1104 don't need to process it anymore. INSN should be a single set
1105 insn. Set up that RTL was changed through CHANGE_P and macro
1106 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1107 SEC_MEM_P. */
1108 static bool
1109 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1111 int sregno, dregno;
1112 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1113 rtx_insn *before;
1114 enum reg_class dclass, sclass, secondary_class;
1115 secondary_reload_info sri;
1117 lra_assert (curr_insn_set != NULL_RTX);
1118 dreg = dest = SET_DEST (curr_insn_set);
1119 sreg = src = SET_SRC (curr_insn_set);
1120 if (GET_CODE (dest) == SUBREG)
1121 dreg = SUBREG_REG (dest);
1122 if (GET_CODE (src) == SUBREG)
1123 sreg = SUBREG_REG (src);
1124 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1125 return false;
1126 sclass = dclass = NO_REGS;
1127 if (REG_P (dreg))
1128 dclass = get_reg_class (REGNO (dreg));
1129 if (dclass == ALL_REGS)
1130 /* ALL_REGS is used for new pseudos created by transformations
1131 like reload of SUBREG_REG (see function
1132 simplify_operand_subreg). We don't know their class yet. We
1133 should figure out the class from processing the insn
1134 constraints not in this fast path function. Even if ALL_REGS
1135 were a right class for the pseudo, secondary_... hooks usually
1136 are not define for ALL_REGS. */
1137 return false;
1138 if (REG_P (sreg))
1139 sclass = get_reg_class (REGNO (sreg));
1140 if (sclass == ALL_REGS)
1141 /* See comments above. */
1142 return false;
1143 if (sclass == NO_REGS && dclass == NO_REGS)
1144 return false;
1145 #ifdef SECONDARY_MEMORY_NEEDED
1146 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1147 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1148 && ((sclass != NO_REGS && dclass != NO_REGS)
1149 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1150 #endif
1153 *sec_mem_p = true;
1154 return false;
1156 #endif
1157 if (! REG_P (dreg) || ! REG_P (sreg))
1158 return false;
1159 sri.prev_sri = NULL;
1160 sri.icode = CODE_FOR_nothing;
1161 sri.extra_cost = 0;
1162 secondary_class = NO_REGS;
1163 /* Set up hard register for a reload pseudo for hook
1164 secondary_reload because some targets just ignore unassigned
1165 pseudos in the hook. */
1166 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1168 dregno = REGNO (dreg);
1169 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1171 else
1172 dregno = -1;
1173 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1175 sregno = REGNO (sreg);
1176 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1178 else
1179 sregno = -1;
1180 if (sclass != NO_REGS)
1181 secondary_class
1182 = (enum reg_class) targetm.secondary_reload (false, dest,
1183 (reg_class_t) sclass,
1184 GET_MODE (src), &sri);
1185 if (sclass == NO_REGS
1186 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1187 && dclass != NO_REGS))
1189 enum reg_class old_sclass = secondary_class;
1190 secondary_reload_info old_sri = sri;
1192 sri.prev_sri = NULL;
1193 sri.icode = CODE_FOR_nothing;
1194 sri.extra_cost = 0;
1195 secondary_class
1196 = (enum reg_class) targetm.secondary_reload (true, src,
1197 (reg_class_t) dclass,
1198 GET_MODE (src), &sri);
1199 /* Check the target hook consistency. */
1200 lra_assert
1201 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1202 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1203 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1205 if (sregno >= 0)
1206 reg_renumber [sregno] = -1;
1207 if (dregno >= 0)
1208 reg_renumber [dregno] = -1;
1209 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1210 return false;
1211 *change_p = true;
1212 new_reg = NULL_RTX;
1213 if (secondary_class != NO_REGS)
1214 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1215 secondary_class,
1216 "secondary");
1217 start_sequence ();
1218 if (sri.icode == CODE_FOR_nothing)
1219 lra_emit_move (new_reg, src);
1220 else
1222 enum reg_class scratch_class;
1224 scratch_class = (reg_class_from_constraints
1225 (insn_data[sri.icode].operand[2].constraint));
1226 scratch_reg = (lra_create_new_reg_with_unique_value
1227 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1228 scratch_class, "scratch"));
1229 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1230 src, scratch_reg));
1232 before = get_insns ();
1233 end_sequence ();
1234 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1235 if (new_reg != NULL_RTX)
1236 SET_SRC (curr_insn_set) = new_reg;
1237 else
1239 if (lra_dump_file != NULL)
1241 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1242 dump_insn_slim (lra_dump_file, curr_insn);
1244 lra_set_insn_deleted (curr_insn);
1245 return true;
1247 return false;
1250 /* The following data describe the result of process_alt_operands.
1251 The data are used in curr_insn_transform to generate reloads. */
1253 /* The chosen reg classes which should be used for the corresponding
1254 operands. */
1255 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1256 /* True if the operand should be the same as another operand and that
1257 other operand does not need a reload. */
1258 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1259 /* True if the operand does not need a reload. */
1260 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1261 /* True if the operand can be offsetable memory. */
1262 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1263 /* The number of an operand to which given operand can be matched to. */
1264 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1265 /* The number of elements in the following array. */
1266 static int goal_alt_dont_inherit_ops_num;
1267 /* Numbers of operands whose reload pseudos should not be inherited. */
1268 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1269 /* True if the insn commutative operands should be swapped. */
1270 static bool goal_alt_swapped;
1271 /* The chosen insn alternative. */
1272 static int goal_alt_number;
1274 /* True if the corresponding operand is the result of an equivalence
1275 substitution. */
1276 static bool equiv_substition_p[MAX_RECOG_OPERANDS];
1278 /* The following five variables are used to choose the best insn
1279 alternative. They reflect final characteristics of the best
1280 alternative. */
1282 /* Number of necessary reloads and overall cost reflecting the
1283 previous value and other unpleasantness of the best alternative. */
1284 static int best_losers, best_overall;
1285 /* Overall number hard registers used for reloads. For example, on
1286 some targets we need 2 general registers to reload DFmode and only
1287 one floating point register. */
1288 static int best_reload_nregs;
1289 /* Overall number reflecting distances of previous reloading the same
1290 value. The distances are counted from the current BB start. It is
1291 used to improve inheritance chances. */
1292 static int best_reload_sum;
1294 /* True if the current insn should have no correspondingly input or
1295 output reloads. */
1296 static bool no_input_reloads_p, no_output_reloads_p;
1298 /* True if we swapped the commutative operands in the current
1299 insn. */
1300 static int curr_swapped;
1302 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1303 register of class CL. Add any input reloads to list BEFORE. AFTER
1304 is nonnull if *LOC is an automodified value; handle that case by
1305 adding the required output reloads to list AFTER. Return true if
1306 the RTL was changed.
1308 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1309 register. Return false if the address register is correct. */
1310 static bool
1311 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1312 enum reg_class cl)
1314 int regno;
1315 enum reg_class rclass, new_class;
1316 rtx reg;
1317 rtx new_reg;
1318 machine_mode mode;
1319 bool subreg_p, before_p = false;
1321 subreg_p = GET_CODE (*loc) == SUBREG;
1322 if (subreg_p)
1324 reg = SUBREG_REG (*loc);
1325 mode = GET_MODE (reg);
1327 /* For mode with size bigger than ptr_mode, there unlikely to be "mov"
1328 between two registers with different classes, but there normally will
1329 be "mov" which transfers element of vector register into the general
1330 register, and this normally will be a subreg which should be reloaded
1331 as a whole. This is particularly likely to be triggered when
1332 -fno-split-wide-types specified. */
1333 if (!REG_P (reg)
1334 || in_class_p (reg, cl, &new_class)
1335 || GET_MODE_SIZE (mode) <= GET_MODE_SIZE (ptr_mode))
1336 loc = &SUBREG_REG (*loc);
1339 reg = *loc;
1340 mode = GET_MODE (reg);
1341 if (! REG_P (reg))
1343 if (check_only_p)
1344 return true;
1345 /* Always reload memory in an address even if the target supports
1346 such addresses. */
1347 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1348 before_p = true;
1350 else
1352 regno = REGNO (reg);
1353 rclass = get_reg_class (regno);
1354 if (! check_only_p
1355 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1357 if (lra_dump_file != NULL)
1359 fprintf (lra_dump_file,
1360 "Changing pseudo %d in address of insn %u on equiv ",
1361 REGNO (reg), INSN_UID (curr_insn));
1362 dump_value_slim (lra_dump_file, *loc, 1);
1363 fprintf (lra_dump_file, "\n");
1365 *loc = copy_rtx (*loc);
1367 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1369 if (check_only_p)
1370 return true;
1371 reg = *loc;
1372 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1373 mode, reg, cl, subreg_p, "address", &new_reg))
1374 before_p = true;
1376 else if (new_class != NO_REGS && rclass != new_class)
1378 if (check_only_p)
1379 return true;
1380 lra_change_class (regno, new_class, " Change to", true);
1381 return false;
1383 else
1384 return false;
1386 if (before_p)
1388 push_to_sequence (*before);
1389 lra_emit_move (new_reg, reg);
1390 *before = get_insns ();
1391 end_sequence ();
1393 *loc = new_reg;
1394 if (after != NULL)
1396 start_sequence ();
1397 lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1398 emit_insn (*after);
1399 *after = get_insns ();
1400 end_sequence ();
1402 return true;
1405 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1406 the insn to be inserted before curr insn. AFTER returns the
1407 the insn to be inserted after curr insn. ORIGREG and NEWREG
1408 are the original reg and new reg for reload. */
1409 static void
1410 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1411 rtx newreg)
1413 if (before)
1415 push_to_sequence (*before);
1416 lra_emit_move (newreg, origreg);
1417 *before = get_insns ();
1418 end_sequence ();
1420 if (after)
1422 start_sequence ();
1423 lra_emit_move (origreg, newreg);
1424 emit_insn (*after);
1425 *after = get_insns ();
1426 end_sequence ();
1430 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1432 /* Make reloads for subreg in operand NOP with internal subreg mode
1433 REG_MODE, add new reloads for further processing. Return true if
1434 any change was done. */
1435 static bool
1436 simplify_operand_subreg (int nop, machine_mode reg_mode)
1438 int hard_regno;
1439 rtx_insn *before, *after;
1440 machine_mode mode, innermode;
1441 rtx reg, new_reg;
1442 rtx operand = *curr_id->operand_loc[nop];
1443 enum reg_class regclass;
1444 enum op_type type;
1446 before = after = NULL;
1448 if (GET_CODE (operand) != SUBREG)
1449 return false;
1451 mode = GET_MODE (operand);
1452 reg = SUBREG_REG (operand);
1453 innermode = GET_MODE (reg);
1454 type = curr_static_id->operand[nop].type;
1455 if (MEM_P (reg))
1457 rtx subst;
1459 alter_subreg (curr_id->operand_loc[nop], false);
1460 subst = *curr_id->operand_loc[nop];
1461 lra_assert (MEM_P (subst));
1462 if (! valid_address_p (innermode, XEXP (reg, 0),
1463 MEM_ADDR_SPACE (reg))
1464 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1465 MEM_ADDR_SPACE (subst))
1466 || ((get_constraint_type (lookup_constraint
1467 (curr_static_id->operand[nop].constraint))
1468 != CT_SPECIAL_MEMORY)
1469 /* We still can reload address and if the address is
1470 valid, we can remove subreg without reloading its
1471 inner memory. */
1472 && valid_address_p (GET_MODE (subst),
1473 regno_reg_rtx
1474 [ira_class_hard_regs
1475 [base_reg_class (GET_MODE (subst),
1476 MEM_ADDR_SPACE (subst),
1477 ADDRESS, SCRATCH)][0]],
1478 MEM_ADDR_SPACE (subst))))
1480 /* If we change address for paradoxical subreg of memory, the
1481 address might violate the necessary alignment or the access might
1482 be slow. So take this into consideration. We should not worry
1483 about access beyond allocated memory for paradoxical memory
1484 subregs as we don't substitute such equiv memory (see processing
1485 equivalences in function lra_constraints) and because for spilled
1486 pseudos we allocate stack memory enough for the biggest
1487 corresponding paradoxical subreg. */
1488 if (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1489 || SLOW_UNALIGNED_ACCESS (innermode, MEM_ALIGN (reg))
1490 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode))
1491 return true;
1493 /* INNERMODE is fast, MODE slow. Reload the mem in INNERMODE. */
1494 enum reg_class rclass
1495 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1496 if (get_reload_reg (curr_static_id->operand[nop].type, innermode, reg,
1497 rclass, TRUE, "slow mem", &new_reg))
1499 bool insert_before, insert_after;
1500 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1502 insert_before = (type != OP_OUT
1503 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1504 insert_after = type != OP_IN;
1505 insert_move_for_subreg (insert_before ? &before : NULL,
1506 insert_after ? &after : NULL,
1507 reg, new_reg);
1509 *curr_id->operand_loc[nop] = operand;
1510 SUBREG_REG (operand) = new_reg;
1512 /* Convert to MODE. */
1513 reg = operand;
1514 rclass = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1515 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1516 rclass, TRUE, "slow mem", &new_reg))
1518 bool insert_before, insert_after;
1519 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1521 insert_before = type != OP_OUT;
1522 insert_after = type != OP_IN;
1523 insert_move_for_subreg (insert_before ? &before : NULL,
1524 insert_after ? &after : NULL,
1525 reg, new_reg);
1527 *curr_id->operand_loc[nop] = new_reg;
1528 lra_process_new_insns (curr_insn, before, after,
1529 "Inserting slow mem reload");
1530 return true;
1533 /* If the address was valid and became invalid, prefer to reload
1534 the memory. Typical case is when the index scale should
1535 correspond the memory. */
1536 *curr_id->operand_loc[nop] = operand;
1538 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1540 alter_subreg (curr_id->operand_loc[nop], false);
1541 return true;
1543 else if (CONSTANT_P (reg))
1545 /* Try to simplify subreg of constant. It is usually result of
1546 equivalence substitution. */
1547 if (innermode == VOIDmode
1548 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1549 innermode = curr_static_id->operand[nop].mode;
1550 if ((new_reg = simplify_subreg (mode, reg, innermode,
1551 SUBREG_BYTE (operand))) != NULL_RTX)
1553 *curr_id->operand_loc[nop] = new_reg;
1554 return true;
1557 /* Put constant into memory when we have mixed modes. It generates
1558 a better code in most cases as it does not need a secondary
1559 reload memory. It also prevents LRA looping when LRA is using
1560 secondary reload memory again and again. */
1561 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1562 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1564 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1565 alter_subreg (curr_id->operand_loc[nop], false);
1566 return true;
1568 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1569 if there may be a problem accessing OPERAND in the outer
1570 mode. */
1571 if ((REG_P (reg)
1572 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1573 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1574 /* Don't reload paradoxical subregs because we could be looping
1575 having repeatedly final regno out of hard regs range. */
1576 && (hard_regno_nregs[hard_regno][innermode]
1577 >= hard_regno_nregs[hard_regno][mode])
1578 && simplify_subreg_regno (hard_regno, innermode,
1579 SUBREG_BYTE (operand), mode) < 0
1580 /* Don't reload subreg for matching reload. It is actually
1581 valid subreg in LRA. */
1582 && ! LRA_SUBREG_P (operand))
1583 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1585 enum reg_class rclass;
1587 if (REG_P (reg))
1588 /* There is a big probability that we will get the same class
1589 for the new pseudo and we will get the same insn which
1590 means infinite looping. So spill the new pseudo. */
1591 rclass = NO_REGS;
1592 else
1593 /* The class will be defined later in curr_insn_transform. */
1594 rclass
1595 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1597 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1598 rclass, TRUE, "subreg reg", &new_reg))
1600 bool insert_before, insert_after;
1601 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1603 insert_before = (type != OP_OUT
1604 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1605 insert_after = (type != OP_IN);
1606 insert_move_for_subreg (insert_before ? &before : NULL,
1607 insert_after ? &after : NULL,
1608 reg, new_reg);
1610 SUBREG_REG (operand) = new_reg;
1611 lra_process_new_insns (curr_insn, before, after,
1612 "Inserting subreg reload");
1613 return true;
1615 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1616 IRA allocates hardreg to the inner pseudo reg according to its mode
1617 instead of the outermode, so the size of the hardreg may not be enough
1618 to contain the outermode operand, in that case we may need to insert
1619 reload for the reg. For the following two types of paradoxical subreg,
1620 we need to insert reload:
1621 1. If the op_type is OP_IN, and the hardreg could not be paired with
1622 other hardreg to contain the outermode operand
1623 (checked by in_hard_reg_set_p), we need to insert the reload.
1624 2. If the op_type is OP_OUT or OP_INOUT.
1626 Here is a paradoxical subreg example showing how the reload is generated:
1628 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1629 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1631 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1632 here, if reg107 is assigned to hardreg R15, because R15 is the last
1633 hardreg, compiler cannot find another hardreg to pair with R15 to
1634 contain TImode data. So we insert a TImode reload reg180 for it.
1635 After reload is inserted:
1637 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1638 (reg:DI 107 [ __comp ])) -1
1639 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1640 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1642 Two reload hard registers will be allocated to reg180 to save TImode data
1643 in LRA_assign. */
1644 else if (REG_P (reg)
1645 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1646 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1647 && (hard_regno_nregs[hard_regno][innermode]
1648 < hard_regno_nregs[hard_regno][mode])
1649 && (regclass = lra_get_allocno_class (REGNO (reg)))
1650 && (type != OP_IN
1651 || !in_hard_reg_set_p (reg_class_contents[regclass],
1652 mode, hard_regno)))
1654 /* The class will be defined later in curr_insn_transform. */
1655 enum reg_class rclass
1656 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1658 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1659 rclass, TRUE, "paradoxical subreg", &new_reg))
1661 rtx subreg;
1662 bool insert_before, insert_after;
1664 PUT_MODE (new_reg, mode);
1665 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1666 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1668 insert_before = (type != OP_OUT);
1669 insert_after = (type != OP_IN);
1670 insert_move_for_subreg (insert_before ? &before : NULL,
1671 insert_after ? &after : NULL,
1672 reg, subreg);
1674 SUBREG_REG (operand) = new_reg;
1675 lra_process_new_insns (curr_insn, before, after,
1676 "Inserting paradoxical subreg reload");
1677 return true;
1679 return false;
1682 /* Return TRUE if X refers for a hard register from SET. */
1683 static bool
1684 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1686 int i, j, x_hard_regno;
1687 machine_mode mode;
1688 const char *fmt;
1689 enum rtx_code code;
1691 if (x == NULL_RTX)
1692 return false;
1693 code = GET_CODE (x);
1694 mode = GET_MODE (x);
1695 if (code == SUBREG)
1697 x = SUBREG_REG (x);
1698 code = GET_CODE (x);
1699 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1700 mode = GET_MODE (x);
1703 if (REG_P (x))
1705 x_hard_regno = get_hard_regno (x, true);
1706 return (x_hard_regno >= 0
1707 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1709 if (MEM_P (x))
1711 struct address_info ad;
1713 decompose_mem_address (&ad, x);
1714 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1715 return true;
1716 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1717 return true;
1719 fmt = GET_RTX_FORMAT (code);
1720 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1722 if (fmt[i] == 'e')
1724 if (uses_hard_regs_p (XEXP (x, i), set))
1725 return true;
1727 else if (fmt[i] == 'E')
1729 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1730 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1731 return true;
1734 return false;
1737 /* Return true if OP is a spilled pseudo. */
1738 static inline bool
1739 spilled_pseudo_p (rtx op)
1741 return (REG_P (op)
1742 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1745 /* Return true if X is a general constant. */
1746 static inline bool
1747 general_constant_p (rtx x)
1749 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1752 static bool
1753 reg_in_class_p (rtx reg, enum reg_class cl)
1755 if (cl == NO_REGS)
1756 return get_reg_class (REGNO (reg)) == NO_REGS;
1757 return in_class_p (reg, cl, NULL);
1760 /* Return true if SET of RCLASS contains no hard regs which can be
1761 used in MODE. */
1762 static bool
1763 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1764 HARD_REG_SET &set,
1765 enum machine_mode mode)
1767 HARD_REG_SET temp;
1769 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1770 COPY_HARD_REG_SET (temp, set);
1771 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1772 return (hard_reg_set_subset_p
1773 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1776 /* Major function to choose the current insn alternative and what
1777 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1778 negative we should consider only this alternative. Return false if
1779 we can not choose the alternative or find how to reload the
1780 operands. */
1781 static bool
1782 process_alt_operands (int only_alternative)
1784 bool ok_p = false;
1785 int nop, overall, nalt;
1786 int n_alternatives = curr_static_id->n_alternatives;
1787 int n_operands = curr_static_id->n_operands;
1788 /* LOSERS counts the operands that don't fit this alternative and
1789 would require loading. */
1790 int losers;
1791 /* REJECT is a count of how undesirable this alternative says it is
1792 if any reloading is required. If the alternative matches exactly
1793 then REJECT is ignored, but otherwise it gets this much counted
1794 against it in addition to the reloading needed. */
1795 int reject;
1796 int op_reject;
1797 /* The number of elements in the following array. */
1798 int early_clobbered_regs_num;
1799 /* Numbers of operands which are early clobber registers. */
1800 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1801 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1802 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1803 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1804 bool curr_alt_win[MAX_RECOG_OPERANDS];
1805 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1806 int curr_alt_matches[MAX_RECOG_OPERANDS];
1807 /* The number of elements in the following array. */
1808 int curr_alt_dont_inherit_ops_num;
1809 /* Numbers of operands whose reload pseudos should not be inherited. */
1810 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1811 rtx op;
1812 /* The register when the operand is a subreg of register, otherwise the
1813 operand itself. */
1814 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1815 /* The register if the operand is a register or subreg of register,
1816 otherwise NULL. */
1817 rtx operand_reg[MAX_RECOG_OPERANDS];
1818 int hard_regno[MAX_RECOG_OPERANDS];
1819 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1820 int reload_nregs, reload_sum;
1821 bool costly_p;
1822 enum reg_class cl;
1824 /* Calculate some data common for all alternatives to speed up the
1825 function. */
1826 for (nop = 0; nop < n_operands; nop++)
1828 rtx reg;
1830 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1831 /* The real hard regno of the operand after the allocation. */
1832 hard_regno[nop] = get_hard_regno (op, true);
1834 operand_reg[nop] = reg = op;
1835 biggest_mode[nop] = GET_MODE (op);
1836 if (GET_CODE (op) == SUBREG)
1838 operand_reg[nop] = reg = SUBREG_REG (op);
1839 if (GET_MODE_SIZE (biggest_mode[nop])
1840 < GET_MODE_SIZE (GET_MODE (reg)))
1841 biggest_mode[nop] = GET_MODE (reg);
1843 if (! REG_P (reg))
1844 operand_reg[nop] = NULL_RTX;
1845 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1846 || ((int) REGNO (reg)
1847 == lra_get_elimination_hard_regno (REGNO (reg))))
1848 no_subreg_reg_operand[nop] = reg;
1849 else
1850 operand_reg[nop] = no_subreg_reg_operand[nop]
1851 /* Just use natural mode for elimination result. It should
1852 be enough for extra constraints hooks. */
1853 = regno_reg_rtx[hard_regno[nop]];
1856 /* The constraints are made of several alternatives. Each operand's
1857 constraint looks like foo,bar,... with commas separating the
1858 alternatives. The first alternatives for all operands go
1859 together, the second alternatives go together, etc.
1861 First loop over alternatives. */
1862 alternative_mask preferred = curr_id->preferred_alternatives;
1863 if (only_alternative >= 0)
1864 preferred &= ALTERNATIVE_BIT (only_alternative);
1866 for (nalt = 0; nalt < n_alternatives; nalt++)
1868 /* Loop over operands for one constraint alternative. */
1869 if (!TEST_BIT (preferred, nalt))
1870 continue;
1872 overall = losers = reject = reload_nregs = reload_sum = 0;
1873 for (nop = 0; nop < n_operands; nop++)
1875 int inc = (curr_static_id
1876 ->operand_alternative[nalt * n_operands + nop].reject);
1877 if (lra_dump_file != NULL && inc != 0)
1878 fprintf (lra_dump_file,
1879 " Staticly defined alt reject+=%d\n", inc);
1880 reject += inc;
1882 early_clobbered_regs_num = 0;
1884 for (nop = 0; nop < n_operands; nop++)
1886 const char *p;
1887 char *end;
1888 int len, c, m, i, opalt_num, this_alternative_matches;
1889 bool win, did_match, offmemok, early_clobber_p;
1890 /* false => this operand can be reloaded somehow for this
1891 alternative. */
1892 bool badop;
1893 /* true => this operand can be reloaded if the alternative
1894 allows regs. */
1895 bool winreg;
1896 /* True if a constant forced into memory would be OK for
1897 this operand. */
1898 bool constmemok;
1899 enum reg_class this_alternative, this_costly_alternative;
1900 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1901 bool this_alternative_match_win, this_alternative_win;
1902 bool this_alternative_offmemok;
1903 bool scratch_p;
1904 machine_mode mode;
1905 enum constraint_num cn;
1907 opalt_num = nalt * n_operands + nop;
1908 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1910 /* Fast track for no constraints at all. */
1911 curr_alt[nop] = NO_REGS;
1912 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1913 curr_alt_win[nop] = true;
1914 curr_alt_match_win[nop] = false;
1915 curr_alt_offmemok[nop] = false;
1916 curr_alt_matches[nop] = -1;
1917 continue;
1920 op = no_subreg_reg_operand[nop];
1921 mode = curr_operand_mode[nop];
1923 win = did_match = winreg = offmemok = constmemok = false;
1924 badop = true;
1926 early_clobber_p = false;
1927 p = curr_static_id->operand_alternative[opalt_num].constraint;
1929 this_costly_alternative = this_alternative = NO_REGS;
1930 /* We update set of possible hard regs besides its class
1931 because reg class might be inaccurate. For example,
1932 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1933 is translated in HI_REGS because classes are merged by
1934 pairs and there is no accurate intermediate class. */
1935 CLEAR_HARD_REG_SET (this_alternative_set);
1936 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1937 this_alternative_win = false;
1938 this_alternative_match_win = false;
1939 this_alternative_offmemok = false;
1940 this_alternative_matches = -1;
1942 /* An empty constraint should be excluded by the fast
1943 track. */
1944 lra_assert (*p != 0 && *p != ',');
1946 op_reject = 0;
1947 /* Scan this alternative's specs for this operand; set WIN
1948 if the operand fits any letter in this alternative.
1949 Otherwise, clear BADOP if this operand could fit some
1950 letter after reloads, or set WINREG if this operand could
1951 fit after reloads provided the constraint allows some
1952 registers. */
1953 costly_p = false;
1956 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1958 case '\0':
1959 len = 0;
1960 break;
1961 case ',':
1962 c = '\0';
1963 break;
1965 case '&':
1966 early_clobber_p = true;
1967 break;
1969 case '$':
1970 op_reject += LRA_MAX_REJECT;
1971 break;
1972 case '^':
1973 op_reject += LRA_LOSER_COST_FACTOR;
1974 break;
1976 case '#':
1977 /* Ignore rest of this alternative. */
1978 c = '\0';
1979 break;
1981 case '0': case '1': case '2': case '3': case '4':
1982 case '5': case '6': case '7': case '8': case '9':
1984 int m_hregno;
1985 bool match_p;
1987 m = strtoul (p, &end, 10);
1988 p = end;
1989 len = 0;
1990 lra_assert (nop > m);
1992 this_alternative_matches = m;
1993 m_hregno = get_hard_regno (*curr_id->operand_loc[m], false);
1994 /* We are supposed to match a previous operand.
1995 If we do, we win if that one did. If we do
1996 not, count both of the operands as losers.
1997 (This is too conservative, since most of the
1998 time only a single reload insn will be needed
1999 to make the two operands win. As a result,
2000 this alternative may be rejected when it is
2001 actually desirable.) */
2002 match_p = false;
2003 if (operands_match_p (*curr_id->operand_loc[nop],
2004 *curr_id->operand_loc[m], m_hregno))
2006 /* We should reject matching of an early
2007 clobber operand if the matching operand is
2008 not dying in the insn. */
2009 if (! curr_static_id->operand[m].early_clobber
2010 || operand_reg[nop] == NULL_RTX
2011 || (find_regno_note (curr_insn, REG_DEAD,
2012 REGNO (op))
2013 || REGNO (op) == REGNO (operand_reg[m])))
2014 match_p = true;
2016 if (match_p)
2018 /* If we are matching a non-offsettable
2019 address where an offsettable address was
2020 expected, then we must reject this
2021 combination, because we can't reload
2022 it. */
2023 if (curr_alt_offmemok[m]
2024 && MEM_P (*curr_id->operand_loc[m])
2025 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
2026 continue;
2028 else
2030 /* Operands don't match. Both operands must
2031 allow a reload register, otherwise we
2032 cannot make them match. */
2033 if (curr_alt[m] == NO_REGS)
2034 break;
2035 /* Retroactively mark the operand we had to
2036 match as a loser, if it wasn't already and
2037 it wasn't matched to a register constraint
2038 (e.g it might be matched by memory). */
2039 if (curr_alt_win[m]
2040 && (operand_reg[m] == NULL_RTX
2041 || hard_regno[m] < 0))
2043 losers++;
2044 reload_nregs
2045 += (ira_reg_class_max_nregs[curr_alt[m]]
2046 [GET_MODE (*curr_id->operand_loc[m])]);
2049 /* Prefer matching earlyclobber alternative as
2050 it results in less hard regs required for
2051 the insn than a non-matching earlyclobber
2052 alternative. */
2053 if (curr_static_id->operand[m].early_clobber)
2055 if (lra_dump_file != NULL)
2056 fprintf
2057 (lra_dump_file,
2058 " %d Matching earlyclobber alt:"
2059 " reject--\n",
2060 nop);
2061 reject--;
2063 /* Otherwise we prefer no matching
2064 alternatives because it gives more freedom
2065 in RA. */
2066 else if (operand_reg[nop] == NULL_RTX
2067 || (find_regno_note (curr_insn, REG_DEAD,
2068 REGNO (operand_reg[nop]))
2069 == NULL_RTX))
2071 if (lra_dump_file != NULL)
2072 fprintf
2073 (lra_dump_file,
2074 " %d Matching alt: reject+=2\n",
2075 nop);
2076 reject += 2;
2079 /* If we have to reload this operand and some
2080 previous operand also had to match the same
2081 thing as this operand, we don't know how to do
2082 that. */
2083 if (!match_p || !curr_alt_win[m])
2085 for (i = 0; i < nop; i++)
2086 if (curr_alt_matches[i] == m)
2087 break;
2088 if (i < nop)
2089 break;
2091 else
2092 did_match = true;
2094 /* This can be fixed with reloads if the operand
2095 we are supposed to match can be fixed with
2096 reloads. */
2097 badop = false;
2098 this_alternative = curr_alt[m];
2099 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
2100 winreg = this_alternative != NO_REGS;
2101 break;
2104 case 'g':
2105 if (MEM_P (op)
2106 || general_constant_p (op)
2107 || spilled_pseudo_p (op))
2108 win = true;
2109 cl = GENERAL_REGS;
2110 goto reg;
2112 default:
2113 cn = lookup_constraint (p);
2114 switch (get_constraint_type (cn))
2116 case CT_REGISTER:
2117 cl = reg_class_for_constraint (cn);
2118 if (cl != NO_REGS)
2119 goto reg;
2120 break;
2122 case CT_CONST_INT:
2123 if (CONST_INT_P (op)
2124 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2125 win = true;
2126 break;
2128 case CT_MEMORY:
2129 if (MEM_P (op)
2130 && satisfies_memory_constraint_p (op, cn))
2131 win = true;
2132 else if (spilled_pseudo_p (op))
2133 win = true;
2135 /* If we didn't already win, we can reload constants
2136 via force_const_mem or put the pseudo value into
2137 memory, or make other memory by reloading the
2138 address like for 'o'. */
2139 if (CONST_POOL_OK_P (mode, op)
2140 || MEM_P (op) || REG_P (op)
2141 /* We can restore the equiv insn by a
2142 reload. */
2143 || equiv_substition_p[nop])
2144 badop = false;
2145 constmemok = true;
2146 offmemok = true;
2147 break;
2149 case CT_ADDRESS:
2150 /* If we didn't already win, we can reload the address
2151 into a base register. */
2152 if (satisfies_address_constraint_p (op, cn))
2153 win = true;
2154 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2155 ADDRESS, SCRATCH);
2156 badop = false;
2157 goto reg;
2159 case CT_FIXED_FORM:
2160 if (constraint_satisfied_p (op, cn))
2161 win = true;
2162 break;
2164 case CT_SPECIAL_MEMORY:
2165 if (MEM_P (op)
2166 && satisfies_memory_constraint_p (op, cn))
2167 win = true;
2168 else if (spilled_pseudo_p (op))
2169 win = true;
2170 break;
2172 break;
2174 reg:
2175 this_alternative = reg_class_subunion[this_alternative][cl];
2176 IOR_HARD_REG_SET (this_alternative_set,
2177 reg_class_contents[cl]);
2178 if (costly_p)
2180 this_costly_alternative
2181 = reg_class_subunion[this_costly_alternative][cl];
2182 IOR_HARD_REG_SET (this_costly_alternative_set,
2183 reg_class_contents[cl]);
2185 if (mode == BLKmode)
2186 break;
2187 winreg = true;
2188 if (REG_P (op))
2190 if (hard_regno[nop] >= 0
2191 && in_hard_reg_set_p (this_alternative_set,
2192 mode, hard_regno[nop]))
2193 win = true;
2194 else if (hard_regno[nop] < 0
2195 && in_class_p (op, this_alternative, NULL))
2196 win = true;
2198 break;
2200 if (c != ' ' && c != '\t')
2201 costly_p = c == '*';
2203 while ((p += len), c);
2205 scratch_p = (operand_reg[nop] != NULL_RTX
2206 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2207 /* Record which operands fit this alternative. */
2208 if (win)
2210 this_alternative_win = true;
2211 if (operand_reg[nop] != NULL_RTX)
2213 if (hard_regno[nop] >= 0)
2215 if (in_hard_reg_set_p (this_costly_alternative_set,
2216 mode, hard_regno[nop]))
2218 if (lra_dump_file != NULL)
2219 fprintf (lra_dump_file,
2220 " %d Costly set: reject++\n",
2221 nop);
2222 reject++;
2225 else
2227 /* Prefer won reg to spilled pseudo under other
2228 equal conditions for possibe inheritance. */
2229 if (! scratch_p)
2231 if (lra_dump_file != NULL)
2232 fprintf
2233 (lra_dump_file,
2234 " %d Non pseudo reload: reject++\n",
2235 nop);
2236 reject++;
2238 if (in_class_p (operand_reg[nop],
2239 this_costly_alternative, NULL))
2241 if (lra_dump_file != NULL)
2242 fprintf
2243 (lra_dump_file,
2244 " %d Non pseudo costly reload:"
2245 " reject++\n",
2246 nop);
2247 reject++;
2250 /* We simulate the behavior of old reload here.
2251 Although scratches need hard registers and it
2252 might result in spilling other pseudos, no reload
2253 insns are generated for the scratches. So it
2254 might cost something but probably less than old
2255 reload pass believes. */
2256 if (scratch_p)
2258 if (lra_dump_file != NULL)
2259 fprintf (lra_dump_file,
2260 " %d Scratch win: reject+=2\n",
2261 nop);
2262 reject += 2;
2266 else if (did_match)
2267 this_alternative_match_win = true;
2268 else
2270 int const_to_mem = 0;
2271 bool no_regs_p;
2273 reject += op_reject;
2274 /* Never do output reload of stack pointer. It makes
2275 impossible to do elimination when SP is changed in
2276 RTL. */
2277 if (op == stack_pointer_rtx && ! frame_pointer_needed
2278 && curr_static_id->operand[nop].type != OP_IN)
2279 goto fail;
2281 /* If this alternative asks for a specific reg class, see if there
2282 is at least one allocatable register in that class. */
2283 no_regs_p
2284 = (this_alternative == NO_REGS
2285 || (hard_reg_set_subset_p
2286 (reg_class_contents[this_alternative],
2287 lra_no_alloc_regs)));
2289 /* For asms, verify that the class for this alternative is possible
2290 for the mode that is specified. */
2291 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2293 int i;
2294 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2295 if (HARD_REGNO_MODE_OK (i, mode)
2296 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2297 mode, i))
2298 break;
2299 if (i == FIRST_PSEUDO_REGISTER)
2300 winreg = false;
2303 /* If this operand accepts a register, and if the
2304 register class has at least one allocatable register,
2305 then this operand can be reloaded. */
2306 if (winreg && !no_regs_p)
2307 badop = false;
2309 if (badop)
2311 if (lra_dump_file != NULL)
2312 fprintf (lra_dump_file,
2313 " alt=%d: Bad operand -- refuse\n",
2314 nalt);
2315 goto fail;
2318 if (this_alternative != NO_REGS)
2320 HARD_REG_SET available_regs;
2322 COPY_HARD_REG_SET (available_regs,
2323 reg_class_contents[this_alternative]);
2324 AND_COMPL_HARD_REG_SET
2325 (available_regs,
2326 ira_prohibited_class_mode_regs[this_alternative][mode]);
2327 AND_COMPL_HARD_REG_SET (available_regs, lra_no_alloc_regs);
2328 if (hard_reg_set_empty_p (available_regs))
2330 /* There are no hard regs holding a value of given
2331 mode. */
2332 if (offmemok)
2334 this_alternative = NO_REGS;
2335 if (lra_dump_file != NULL)
2336 fprintf (lra_dump_file,
2337 " %d Using memory because of"
2338 " a bad mode: reject+=2\n",
2339 nop);
2340 reject += 2;
2342 else
2344 if (lra_dump_file != NULL)
2345 fprintf (lra_dump_file,
2346 " alt=%d: Wrong mode -- refuse\n",
2347 nalt);
2348 goto fail;
2353 /* If not assigned pseudo has a class which a subset of
2354 required reg class, it is a less costly alternative
2355 as the pseudo still can get a hard reg of necessary
2356 class. */
2357 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2358 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2359 && ira_class_subset_p[this_alternative][cl])
2361 if (lra_dump_file != NULL)
2362 fprintf
2363 (lra_dump_file,
2364 " %d Super set class reg: reject-=3\n", nop);
2365 reject -= 3;
2368 this_alternative_offmemok = offmemok;
2369 if (this_costly_alternative != NO_REGS)
2371 if (lra_dump_file != NULL)
2372 fprintf (lra_dump_file,
2373 " %d Costly loser: reject++\n", nop);
2374 reject++;
2376 /* If the operand is dying, has a matching constraint,
2377 and satisfies constraints of the matched operand
2378 which failed to satisfy the own constraints, most probably
2379 the reload for this operand will be gone. */
2380 if (this_alternative_matches >= 0
2381 && !curr_alt_win[this_alternative_matches]
2382 && REG_P (op)
2383 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2384 && (hard_regno[nop] >= 0
2385 ? in_hard_reg_set_p (this_alternative_set,
2386 mode, hard_regno[nop])
2387 : in_class_p (op, this_alternative, NULL)))
2389 if (lra_dump_file != NULL)
2390 fprintf
2391 (lra_dump_file,
2392 " %d Dying matched operand reload: reject++\n",
2393 nop);
2394 reject++;
2396 else
2398 /* Strict_low_part requires to reload the register
2399 not the sub-register. In this case we should
2400 check that a final reload hard reg can hold the
2401 value mode. */
2402 if (curr_static_id->operand[nop].strict_low
2403 && REG_P (op)
2404 && hard_regno[nop] < 0
2405 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2406 && ira_class_hard_regs_num[this_alternative] > 0
2407 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2408 [this_alternative][0],
2409 GET_MODE
2410 (*curr_id->operand_loc[nop])))
2412 if (lra_dump_file != NULL)
2413 fprintf
2414 (lra_dump_file,
2415 " alt=%d: Strict low subreg reload -- refuse\n",
2416 nalt);
2417 goto fail;
2419 losers++;
2421 if (operand_reg[nop] != NULL_RTX
2422 /* Output operands and matched input operands are
2423 not inherited. The following conditions do not
2424 exactly describe the previous statement but they
2425 are pretty close. */
2426 && curr_static_id->operand[nop].type != OP_OUT
2427 && (this_alternative_matches < 0
2428 || curr_static_id->operand[nop].type != OP_IN))
2430 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2431 (operand_reg[nop])]
2432 .last_reload);
2434 /* The value of reload_sum has sense only if we
2435 process insns in their order. It happens only on
2436 the first constraints sub-pass when we do most of
2437 reload work. */
2438 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2439 reload_sum += last_reload - bb_reload_num;
2441 /* If this is a constant that is reloaded into the
2442 desired class by copying it to memory first, count
2443 that as another reload. This is consistent with
2444 other code and is required to avoid choosing another
2445 alternative when the constant is moved into memory.
2446 Note that the test here is precisely the same as in
2447 the code below that calls force_const_mem. */
2448 if (CONST_POOL_OK_P (mode, op)
2449 && ((targetm.preferred_reload_class
2450 (op, this_alternative) == NO_REGS)
2451 || no_input_reloads_p))
2453 const_to_mem = 1;
2454 if (! no_regs_p)
2455 losers++;
2458 /* Alternative loses if it requires a type of reload not
2459 permitted for this insn. We can always reload
2460 objects with a REG_UNUSED note. */
2461 if ((curr_static_id->operand[nop].type != OP_IN
2462 && no_output_reloads_p
2463 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2464 || (curr_static_id->operand[nop].type != OP_OUT
2465 && no_input_reloads_p && ! const_to_mem)
2466 || (this_alternative_matches >= 0
2467 && (no_input_reloads_p
2468 || (no_output_reloads_p
2469 && (curr_static_id->operand
2470 [this_alternative_matches].type != OP_IN)
2471 && ! find_reg_note (curr_insn, REG_UNUSED,
2472 no_subreg_reg_operand
2473 [this_alternative_matches])))))
2475 if (lra_dump_file != NULL)
2476 fprintf
2477 (lra_dump_file,
2478 " alt=%d: No input/otput reload -- refuse\n",
2479 nalt);
2480 goto fail;
2483 /* Alternative loses if it required class pseudo can not
2484 hold value of required mode. Such insns can be
2485 described by insn definitions with mode iterators. */
2486 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2487 && ! hard_reg_set_empty_p (this_alternative_set)
2488 /* It is common practice for constraints to use a
2489 class which does not have actually enough regs to
2490 hold the value (e.g. x86 AREG for mode requiring
2491 more one general reg). Therefore we have 2
2492 conditions to check that the reload pseudo can
2493 not hold the mode value. */
2494 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2495 [this_alternative][0],
2496 GET_MODE (*curr_id->operand_loc[nop]))
2497 /* The above condition is not enough as the first
2498 reg in ira_class_hard_regs can be not aligned for
2499 multi-words mode values. */
2500 && (prohibited_class_reg_set_mode_p
2501 (this_alternative, this_alternative_set,
2502 GET_MODE (*curr_id->operand_loc[nop]))))
2504 if (lra_dump_file != NULL)
2505 fprintf (lra_dump_file,
2506 " alt=%d: reload pseudo for op %d "
2507 " can not hold the mode value -- refuse\n",
2508 nalt, nop);
2509 goto fail;
2512 /* Check strong discouragement of reload of non-constant
2513 into class THIS_ALTERNATIVE. */
2514 if (! CONSTANT_P (op) && ! no_regs_p
2515 && (targetm.preferred_reload_class
2516 (op, this_alternative) == NO_REGS
2517 || (curr_static_id->operand[nop].type == OP_OUT
2518 && (targetm.preferred_output_reload_class
2519 (op, this_alternative) == NO_REGS))))
2521 if (lra_dump_file != NULL)
2522 fprintf (lra_dump_file,
2523 " %d Non-prefered reload: reject+=%d\n",
2524 nop, LRA_MAX_REJECT);
2525 reject += LRA_MAX_REJECT;
2528 if (! (MEM_P (op) && offmemok)
2529 && ! (const_to_mem && constmemok))
2531 /* We prefer to reload pseudos over reloading other
2532 things, since such reloads may be able to be
2533 eliminated later. So bump REJECT in other cases.
2534 Don't do this in the case where we are forcing a
2535 constant into memory and it will then win since
2536 we don't want to have a different alternative
2537 match then. */
2538 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2540 if (lra_dump_file != NULL)
2541 fprintf
2542 (lra_dump_file,
2543 " %d Non-pseudo reload: reject+=2\n",
2544 nop);
2545 reject += 2;
2548 if (! no_regs_p)
2549 reload_nregs
2550 += ira_reg_class_max_nregs[this_alternative][mode];
2552 if (SMALL_REGISTER_CLASS_P (this_alternative))
2554 if (lra_dump_file != NULL)
2555 fprintf
2556 (lra_dump_file,
2557 " %d Small class reload: reject+=%d\n",
2558 nop, LRA_LOSER_COST_FACTOR / 2);
2559 reject += LRA_LOSER_COST_FACTOR / 2;
2563 /* We are trying to spill pseudo into memory. It is
2564 usually more costly than moving to a hard register
2565 although it might takes the same number of
2566 reloads.
2568 Non-pseudo spill may happen also. Suppose a target allows both
2569 register and memory in the operand constraint alternatives,
2570 then it's typical that an eliminable register has a substition
2571 of "base + offset" which can either be reloaded by a simple
2572 "new_reg <= base + offset" which will match the register
2573 constraint, or a similar reg addition followed by further spill
2574 to and reload from memory which will match the memory
2575 constraint, but this memory spill will be much more costly
2576 usually.
2578 Code below increases the reject for both pseudo and non-pseudo
2579 spill. */
2580 if (no_regs_p
2581 && !(MEM_P (op) && offmemok)
2582 && !(REG_P (op) && hard_regno[nop] < 0))
2584 if (lra_dump_file != NULL)
2585 fprintf
2586 (lra_dump_file,
2587 " %d Spill %spseudo into memory: reject+=3\n",
2588 nop, REG_P (op) ? "" : "Non-");
2589 reject += 3;
2590 if (VECTOR_MODE_P (mode))
2592 /* Spilling vectors into memory is usually more
2593 costly as they contain big values. */
2594 if (lra_dump_file != NULL)
2595 fprintf
2596 (lra_dump_file,
2597 " %d Spill vector pseudo: reject+=2\n",
2598 nop);
2599 reject += 2;
2603 #ifdef SECONDARY_MEMORY_NEEDED
2604 /* If reload requires moving value through secondary
2605 memory, it will need one more insn at least. */
2606 if (this_alternative != NO_REGS
2607 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2608 && ((curr_static_id->operand[nop].type != OP_OUT
2609 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2610 GET_MODE (op)))
2611 || (curr_static_id->operand[nop].type != OP_IN
2612 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2613 GET_MODE (op)))))
2614 losers++;
2615 #endif
2616 /* Input reloads can be inherited more often than output
2617 reloads can be removed, so penalize output
2618 reloads. */
2619 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2621 if (lra_dump_file != NULL)
2622 fprintf
2623 (lra_dump_file,
2624 " %d Non input pseudo reload: reject++\n",
2625 nop);
2626 reject++;
2630 if (early_clobber_p && ! scratch_p)
2632 if (lra_dump_file != NULL)
2633 fprintf (lra_dump_file,
2634 " %d Early clobber: reject++\n", nop);
2635 reject++;
2637 /* ??? We check early clobbers after processing all operands
2638 (see loop below) and there we update the costs more.
2639 Should we update the cost (may be approximately) here
2640 because of early clobber register reloads or it is a rare
2641 or non-important thing to be worth to do it. */
2642 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2643 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2645 if (lra_dump_file != NULL)
2646 fprintf (lra_dump_file,
2647 " alt=%d,overall=%d,losers=%d -- refuse\n",
2648 nalt, overall, losers);
2649 goto fail;
2652 curr_alt[nop] = this_alternative;
2653 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2654 curr_alt_win[nop] = this_alternative_win;
2655 curr_alt_match_win[nop] = this_alternative_match_win;
2656 curr_alt_offmemok[nop] = this_alternative_offmemok;
2657 curr_alt_matches[nop] = this_alternative_matches;
2659 if (this_alternative_matches >= 0
2660 && !did_match && !this_alternative_win)
2661 curr_alt_win[this_alternative_matches] = false;
2663 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2664 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2666 if (curr_insn_set != NULL_RTX && n_operands == 2
2667 /* Prevent processing non-move insns. */
2668 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2669 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2670 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2671 && REG_P (no_subreg_reg_operand[0])
2672 && REG_P (no_subreg_reg_operand[1])
2673 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2674 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2675 || (! curr_alt_win[0] && curr_alt_win[1]
2676 && REG_P (no_subreg_reg_operand[1])
2677 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2678 || (curr_alt_win[0] && ! curr_alt_win[1]
2679 && REG_P (no_subreg_reg_operand[0])
2680 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2681 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2682 no_subreg_reg_operand[1])
2683 || (targetm.preferred_reload_class
2684 (no_subreg_reg_operand[1],
2685 (enum reg_class) curr_alt[1]) != NO_REGS))
2686 /* If it is a result of recent elimination in move
2687 insn we can transform it into an add still by
2688 using this alternative. */
2689 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2691 /* We have a move insn and a new reload insn will be similar
2692 to the current insn. We should avoid such situation as it
2693 results in LRA cycling. */
2694 overall += LRA_MAX_REJECT;
2696 ok_p = true;
2697 curr_alt_dont_inherit_ops_num = 0;
2698 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2700 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2701 HARD_REG_SET temp_set;
2703 i = early_clobbered_nops[nop];
2704 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2705 || hard_regno[i] < 0)
2706 continue;
2707 lra_assert (operand_reg[i] != NULL_RTX);
2708 clobbered_hard_regno = hard_regno[i];
2709 CLEAR_HARD_REG_SET (temp_set);
2710 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2711 first_conflict_j = last_conflict_j = -1;
2712 for (j = 0; j < n_operands; j++)
2713 if (j == i
2714 /* We don't want process insides of match_operator and
2715 match_parallel because otherwise we would process
2716 their operands once again generating a wrong
2717 code. */
2718 || curr_static_id->operand[j].is_operator)
2719 continue;
2720 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2721 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2722 continue;
2723 /* If we don't reload j-th operand, check conflicts. */
2724 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2725 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2727 if (first_conflict_j < 0)
2728 first_conflict_j = j;
2729 last_conflict_j = j;
2731 if (last_conflict_j < 0)
2732 continue;
2733 /* If earlyclobber operand conflicts with another
2734 non-matching operand which is actually the same register
2735 as the earlyclobber operand, it is better to reload the
2736 another operand as an operand matching the earlyclobber
2737 operand can be also the same. */
2738 if (first_conflict_j == last_conflict_j
2739 && operand_reg[last_conflict_j] != NULL_RTX
2740 && ! curr_alt_match_win[last_conflict_j]
2741 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2743 curr_alt_win[last_conflict_j] = false;
2744 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2745 = last_conflict_j;
2746 losers++;
2747 /* Early clobber was already reflected in REJECT. */
2748 lra_assert (reject > 0);
2749 if (lra_dump_file != NULL)
2750 fprintf
2751 (lra_dump_file,
2752 " %d Conflict early clobber reload: reject--\n",
2754 reject--;
2755 overall += LRA_LOSER_COST_FACTOR - 1;
2757 else
2759 /* We need to reload early clobbered register and the
2760 matched registers. */
2761 for (j = 0; j < n_operands; j++)
2762 if (curr_alt_matches[j] == i)
2764 curr_alt_match_win[j] = false;
2765 losers++;
2766 overall += LRA_LOSER_COST_FACTOR;
2768 if (! curr_alt_match_win[i])
2769 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2770 else
2772 /* Remember pseudos used for match reloads are never
2773 inherited. */
2774 lra_assert (curr_alt_matches[i] >= 0);
2775 curr_alt_win[curr_alt_matches[i]] = false;
2777 curr_alt_win[i] = curr_alt_match_win[i] = false;
2778 losers++;
2779 /* Early clobber was already reflected in REJECT. */
2780 lra_assert (reject > 0);
2781 if (lra_dump_file != NULL)
2782 fprintf
2783 (lra_dump_file,
2784 " %d Matched conflict early clobber reloads:"
2785 "reject--\n",
2787 reject--;
2788 overall += LRA_LOSER_COST_FACTOR - 1;
2791 if (lra_dump_file != NULL)
2792 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2793 nalt, overall, losers, reload_nregs);
2795 /* If this alternative can be made to work by reloading, and it
2796 needs less reloading than the others checked so far, record
2797 it as the chosen goal for reloading. */
2798 if ((best_losers != 0 && losers == 0)
2799 || (((best_losers == 0 && losers == 0)
2800 || (best_losers != 0 && losers != 0))
2801 && (best_overall > overall
2802 || (best_overall == overall
2803 /* If the cost of the reloads is the same,
2804 prefer alternative which requires minimal
2805 number of reload regs. */
2806 && (reload_nregs < best_reload_nregs
2807 || (reload_nregs == best_reload_nregs
2808 && (best_reload_sum < reload_sum
2809 || (best_reload_sum == reload_sum
2810 && nalt < goal_alt_number))))))))
2812 for (nop = 0; nop < n_operands; nop++)
2814 goal_alt_win[nop] = curr_alt_win[nop];
2815 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2816 goal_alt_matches[nop] = curr_alt_matches[nop];
2817 goal_alt[nop] = curr_alt[nop];
2818 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2820 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2821 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2822 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2823 goal_alt_swapped = curr_swapped;
2824 best_overall = overall;
2825 best_losers = losers;
2826 best_reload_nregs = reload_nregs;
2827 best_reload_sum = reload_sum;
2828 goal_alt_number = nalt;
2830 if (losers == 0)
2831 /* Everything is satisfied. Do not process alternatives
2832 anymore. */
2833 break;
2834 fail:
2837 return ok_p;
2840 /* Make reload base reg from address AD. */
2841 static rtx
2842 base_to_reg (struct address_info *ad)
2844 enum reg_class cl;
2845 int code = -1;
2846 rtx new_inner = NULL_RTX;
2847 rtx new_reg = NULL_RTX;
2848 rtx_insn *insn;
2849 rtx_insn *last_insn = get_last_insn();
2851 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2852 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2853 get_index_code (ad));
2854 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2855 cl, "base");
2856 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2857 ad->disp_term == NULL
2858 ? gen_int_mode (0, ad->mode)
2859 : *ad->disp_term);
2860 if (!valid_address_p (ad->mode, new_inner, ad->as))
2861 return NULL_RTX;
2862 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base_term));
2863 code = recog_memoized (insn);
2864 if (code < 0)
2866 delete_insns_since (last_insn);
2867 return NULL_RTX;
2870 return new_inner;
2873 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2874 static rtx
2875 base_plus_disp_to_reg (struct address_info *ad)
2877 enum reg_class cl;
2878 rtx new_reg;
2880 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2881 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2882 get_index_code (ad));
2883 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2884 cl, "base + disp");
2885 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2886 return new_reg;
2889 /* Make reload of index part of address AD. Return the new
2890 pseudo. */
2891 static rtx
2892 index_part_to_reg (struct address_info *ad)
2894 rtx new_reg;
2896 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2897 INDEX_REG_CLASS, "index term");
2898 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2899 GEN_INT (get_index_scale (ad)), new_reg, 1);
2900 return new_reg;
2903 /* Return true if we can add a displacement to address AD, even if that
2904 makes the address invalid. The fix-up code requires any new address
2905 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2906 static bool
2907 can_add_disp_p (struct address_info *ad)
2909 return (!ad->autoinc_p
2910 && ad->segment == NULL
2911 && ad->base == ad->base_term
2912 && ad->disp == ad->disp_term);
2915 /* Make equiv substitution in address AD. Return true if a substitution
2916 was made. */
2917 static bool
2918 equiv_address_substitution (struct address_info *ad)
2920 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2921 HOST_WIDE_INT disp, scale;
2922 bool change_p;
2924 base_term = strip_subreg (ad->base_term);
2925 if (base_term == NULL)
2926 base_reg = new_base_reg = NULL_RTX;
2927 else
2929 base_reg = *base_term;
2930 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2932 index_term = strip_subreg (ad->index_term);
2933 if (index_term == NULL)
2934 index_reg = new_index_reg = NULL_RTX;
2935 else
2937 index_reg = *index_term;
2938 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2940 if (base_reg == new_base_reg && index_reg == new_index_reg)
2941 return false;
2942 disp = 0;
2943 change_p = false;
2944 if (lra_dump_file != NULL)
2946 fprintf (lra_dump_file, "Changing address in insn %d ",
2947 INSN_UID (curr_insn));
2948 dump_value_slim (lra_dump_file, *ad->outer, 1);
2950 if (base_reg != new_base_reg)
2952 if (REG_P (new_base_reg))
2954 *base_term = new_base_reg;
2955 change_p = true;
2957 else if (GET_CODE (new_base_reg) == PLUS
2958 && REG_P (XEXP (new_base_reg, 0))
2959 && CONST_INT_P (XEXP (new_base_reg, 1))
2960 && can_add_disp_p (ad))
2962 disp += INTVAL (XEXP (new_base_reg, 1));
2963 *base_term = XEXP (new_base_reg, 0);
2964 change_p = true;
2966 if (ad->base_term2 != NULL)
2967 *ad->base_term2 = *ad->base_term;
2969 if (index_reg != new_index_reg)
2971 if (REG_P (new_index_reg))
2973 *index_term = new_index_reg;
2974 change_p = true;
2976 else if (GET_CODE (new_index_reg) == PLUS
2977 && REG_P (XEXP (new_index_reg, 0))
2978 && CONST_INT_P (XEXP (new_index_reg, 1))
2979 && can_add_disp_p (ad)
2980 && (scale = get_index_scale (ad)))
2982 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2983 *index_term = XEXP (new_index_reg, 0);
2984 change_p = true;
2987 if (disp != 0)
2989 if (ad->disp != NULL)
2990 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2991 else
2993 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2994 update_address (ad);
2996 change_p = true;
2998 if (lra_dump_file != NULL)
3000 if (! change_p)
3001 fprintf (lra_dump_file, " -- no change\n");
3002 else
3004 fprintf (lra_dump_file, " on equiv ");
3005 dump_value_slim (lra_dump_file, *ad->outer, 1);
3006 fprintf (lra_dump_file, "\n");
3009 return change_p;
3012 /* Major function to make reloads for an address in operand NOP or
3013 check its correctness (If CHECK_ONLY_P is true). The supported
3014 cases are:
3016 1) an address that existed before LRA started, at which point it
3017 must have been valid. These addresses are subject to elimination
3018 and may have become invalid due to the elimination offset being out
3019 of range.
3021 2) an address created by forcing a constant to memory
3022 (force_const_to_mem). The initial form of these addresses might
3023 not be valid, and it is this function's job to make them valid.
3025 3) a frame address formed from a register and a (possibly zero)
3026 constant offset. As above, these addresses might not be valid and
3027 this function must make them so.
3029 Add reloads to the lists *BEFORE and *AFTER. We might need to add
3030 reloads to *AFTER because of inc/dec, {pre, post} modify in the
3031 address. Return true for any RTL change.
3033 The function is a helper function which does not produce all
3034 transformations (when CHECK_ONLY_P is false) which can be
3035 necessary. It does just basic steps. To do all necessary
3036 transformations use function process_address. */
3037 static bool
3038 process_address_1 (int nop, bool check_only_p,
3039 rtx_insn **before, rtx_insn **after)
3041 struct address_info ad;
3042 rtx new_reg;
3043 HOST_WIDE_INT scale;
3044 rtx op = *curr_id->operand_loc[nop];
3045 const char *constraint = curr_static_id->operand[nop].constraint;
3046 enum constraint_num cn = lookup_constraint (constraint);
3047 bool change_p = false;
3049 if (MEM_P (op)
3050 && GET_MODE (op) == BLKmode
3051 && GET_CODE (XEXP (op, 0)) == SCRATCH)
3052 return false;
3054 if (insn_extra_address_constraint (cn))
3055 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
3056 else if (MEM_P (op))
3057 decompose_mem_address (&ad, op);
3058 else if (GET_CODE (op) == SUBREG
3059 && MEM_P (SUBREG_REG (op)))
3060 decompose_mem_address (&ad, SUBREG_REG (op));
3061 else
3062 return false;
3063 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
3064 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
3065 when INDEX_REG_CLASS is a single register class. */
3066 if (ad.base_term != NULL
3067 && ad.index_term != NULL
3068 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
3069 && REG_P (*ad.base_term)
3070 && REG_P (*ad.index_term)
3071 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
3072 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
3074 std::swap (ad.base, ad.index);
3075 std::swap (ad.base_term, ad.index_term);
3077 if (! check_only_p)
3078 change_p = equiv_address_substitution (&ad);
3079 if (ad.base_term != NULL
3080 && (process_addr_reg
3081 (ad.base_term, check_only_p, before,
3082 (ad.autoinc_p
3083 && !(REG_P (*ad.base_term)
3084 && find_regno_note (curr_insn, REG_DEAD,
3085 REGNO (*ad.base_term)) != NULL_RTX)
3086 ? after : NULL),
3087 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3088 get_index_code (&ad)))))
3090 change_p = true;
3091 if (ad.base_term2 != NULL)
3092 *ad.base_term2 = *ad.base_term;
3094 if (ad.index_term != NULL
3095 && process_addr_reg (ad.index_term, check_only_p,
3096 before, NULL, INDEX_REG_CLASS))
3097 change_p = true;
3099 /* Target hooks sometimes don't treat extra-constraint addresses as
3100 legitimate address_operands, so handle them specially. */
3101 if (insn_extra_address_constraint (cn)
3102 && satisfies_address_constraint_p (&ad, cn))
3103 return change_p;
3105 if (check_only_p)
3106 return change_p;
3108 /* There are three cases where the shape of *AD.INNER may now be invalid:
3110 1) the original address was valid, but either elimination or
3111 equiv_address_substitution was applied and that made
3112 the address invalid.
3114 2) the address is an invalid symbolic address created by
3115 force_const_to_mem.
3117 3) the address is a frame address with an invalid offset.
3119 4) the address is a frame address with an invalid base.
3121 All these cases involve a non-autoinc address, so there is no
3122 point revalidating other types. */
3123 if (ad.autoinc_p || valid_address_p (&ad))
3124 return change_p;
3126 /* Any index existed before LRA started, so we can assume that the
3127 presence and shape of the index is valid. */
3128 push_to_sequence (*before);
3129 lra_assert (ad.disp == ad.disp_term);
3130 if (ad.base == NULL)
3132 if (ad.index == NULL)
3134 rtx_insn *insn;
3135 rtx_insn *last = get_last_insn ();
3136 int code = -1;
3137 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3138 SCRATCH, SCRATCH);
3139 rtx addr = *ad.inner;
3141 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3142 if (HAVE_lo_sum)
3144 /* addr => lo_sum (new_base, addr), case (2) above. */
3145 insn = emit_insn (gen_rtx_SET
3146 (new_reg,
3147 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
3148 code = recog_memoized (insn);
3149 if (code >= 0)
3151 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
3152 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
3154 /* Try to put lo_sum into register. */
3155 insn = emit_insn (gen_rtx_SET
3156 (new_reg,
3157 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
3158 code = recog_memoized (insn);
3159 if (code >= 0)
3161 *ad.inner = new_reg;
3162 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
3164 *ad.inner = addr;
3165 code = -1;
3171 if (code < 0)
3172 delete_insns_since (last);
3175 if (code < 0)
3177 /* addr => new_base, case (2) above. */
3178 lra_emit_move (new_reg, addr);
3180 for (insn = last == NULL_RTX ? get_insns () : NEXT_INSN (last);
3181 insn != NULL_RTX;
3182 insn = NEXT_INSN (insn))
3183 if (recog_memoized (insn) < 0)
3184 break;
3185 if (insn != NULL_RTX)
3187 /* Do nothing if we cannot generate right insns.
3188 This is analogous to reload pass behavior. */
3189 delete_insns_since (last);
3190 end_sequence ();
3191 return false;
3193 *ad.inner = new_reg;
3196 else
3198 /* index * scale + disp => new base + index * scale,
3199 case (1) above. */
3200 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3201 GET_CODE (*ad.index));
3203 lra_assert (INDEX_REG_CLASS != NO_REGS);
3204 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3205 lra_emit_move (new_reg, *ad.disp);
3206 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3207 new_reg, *ad.index);
3210 else if (ad.index == NULL)
3212 int regno;
3213 enum reg_class cl;
3214 rtx set;
3215 rtx_insn *insns, *last_insn;
3216 /* Try to reload base into register only if the base is invalid
3217 for the address but with valid offset, case (4) above. */
3218 start_sequence ();
3219 new_reg = base_to_reg (&ad);
3221 /* base + disp => new base, cases (1) and (3) above. */
3222 /* Another option would be to reload the displacement into an
3223 index register. However, postreload has code to optimize
3224 address reloads that have the same base and different
3225 displacements, so reloading into an index register would
3226 not necessarily be a win. */
3227 if (new_reg == NULL_RTX)
3228 new_reg = base_plus_disp_to_reg (&ad);
3229 insns = get_insns ();
3230 last_insn = get_last_insn ();
3231 /* If we generated at least two insns, try last insn source as
3232 an address. If we succeed, we generate one less insn. */
3233 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3234 && GET_CODE (SET_SRC (set)) == PLUS
3235 && REG_P (XEXP (SET_SRC (set), 0))
3236 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3238 *ad.inner = SET_SRC (set);
3239 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3241 *ad.base_term = XEXP (SET_SRC (set), 0);
3242 *ad.disp_term = XEXP (SET_SRC (set), 1);
3243 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3244 get_index_code (&ad));
3245 regno = REGNO (*ad.base_term);
3246 if (regno >= FIRST_PSEUDO_REGISTER
3247 && cl != lra_get_allocno_class (regno))
3248 lra_change_class (regno, cl, " Change to", true);
3249 new_reg = SET_SRC (set);
3250 delete_insns_since (PREV_INSN (last_insn));
3253 /* Try if target can split displacement into legitimite new disp
3254 and offset. If it's the case, we replace the last insn with
3255 insns for base + offset => new_reg and set new_reg + new disp
3256 to *ad.inner. */
3257 last_insn = get_last_insn ();
3258 if ((set = single_set (last_insn)) != NULL_RTX
3259 && GET_CODE (SET_SRC (set)) == PLUS
3260 && REG_P (XEXP (SET_SRC (set), 0))
3261 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3262 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3264 rtx addend, disp = XEXP (SET_SRC (set), 1);
3265 if (targetm.legitimize_address_displacement (&disp, &addend,
3266 ad.mode))
3268 rtx_insn *new_insns;
3269 start_sequence ();
3270 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3271 new_insns = get_insns ();
3272 end_sequence ();
3273 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3274 delete_insns_since (PREV_INSN (last_insn));
3275 add_insn (new_insns);
3276 insns = get_insns ();
3279 end_sequence ();
3280 emit_insn (insns);
3281 *ad.inner = new_reg;
3283 else if (ad.disp_term != NULL)
3285 /* base + scale * index + disp => new base + scale * index,
3286 case (1) above. */
3287 new_reg = base_plus_disp_to_reg (&ad);
3288 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3289 new_reg, *ad.index);
3291 else if ((scale = get_index_scale (&ad)) == 1)
3293 /* The last transformation to one reg will be made in
3294 curr_insn_transform function. */
3295 end_sequence ();
3296 return false;
3298 else if (scale != 0)
3300 /* base + scale * index => base + new_reg,
3301 case (1) above.
3302 Index part of address may become invalid. For example, we
3303 changed pseudo on the equivalent memory and a subreg of the
3304 pseudo onto the memory of different mode for which the scale is
3305 prohibitted. */
3306 new_reg = index_part_to_reg (&ad);
3307 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3308 *ad.base_term, new_reg);
3310 else
3312 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3313 SCRATCH, SCRATCH);
3314 rtx addr = *ad.inner;
3316 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3317 /* addr => new_base. */
3318 lra_emit_move (new_reg, addr);
3319 *ad.inner = new_reg;
3321 *before = get_insns ();
3322 end_sequence ();
3323 return true;
3326 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3327 Use process_address_1 as a helper function. Return true for any
3328 RTL changes.
3330 If CHECK_ONLY_P is true, just check address correctness. Return
3331 false if the address correct. */
3332 static bool
3333 process_address (int nop, bool check_only_p,
3334 rtx_insn **before, rtx_insn **after)
3336 bool res = false;
3338 while (process_address_1 (nop, check_only_p, before, after))
3340 if (check_only_p)
3341 return true;
3342 res = true;
3344 return res;
3347 /* Emit insns to reload VALUE into a new register. VALUE is an
3348 auto-increment or auto-decrement RTX whose operand is a register or
3349 memory location; so reloading involves incrementing that location.
3350 IN is either identical to VALUE, or some cheaper place to reload
3351 value being incremented/decremented from.
3353 INC_AMOUNT is the number to increment or decrement by (always
3354 positive and ignored for POST_MODIFY/PRE_MODIFY).
3356 Return pseudo containing the result. */
3357 static rtx
3358 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3360 /* REG or MEM to be copied and incremented. */
3361 rtx incloc = XEXP (value, 0);
3362 /* Nonzero if increment after copying. */
3363 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3364 || GET_CODE (value) == POST_MODIFY);
3365 rtx_insn *last;
3366 rtx inc;
3367 rtx_insn *add_insn;
3368 int code;
3369 rtx real_in = in == value ? incloc : in;
3370 rtx result;
3371 bool plus_p = true;
3373 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3375 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3376 || GET_CODE (XEXP (value, 1)) == MINUS);
3377 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3378 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3379 inc = XEXP (XEXP (value, 1), 1);
3381 else
3383 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3384 inc_amount = -inc_amount;
3386 inc = GEN_INT (inc_amount);
3389 if (! post && REG_P (incloc))
3390 result = incloc;
3391 else
3392 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3393 "INC/DEC result");
3395 if (real_in != result)
3397 /* First copy the location to the result register. */
3398 lra_assert (REG_P (result));
3399 emit_insn (gen_move_insn (result, real_in));
3402 /* We suppose that there are insns to add/sub with the constant
3403 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3404 old reload worked with this assumption. If the assumption
3405 becomes wrong, we should use approach in function
3406 base_plus_disp_to_reg. */
3407 if (in == value)
3409 /* See if we can directly increment INCLOC. */
3410 last = get_last_insn ();
3411 add_insn = emit_insn (plus_p
3412 ? gen_add2_insn (incloc, inc)
3413 : gen_sub2_insn (incloc, inc));
3415 code = recog_memoized (add_insn);
3416 if (code >= 0)
3418 if (! post && result != incloc)
3419 emit_insn (gen_move_insn (result, incloc));
3420 return result;
3422 delete_insns_since (last);
3425 /* If couldn't do the increment directly, must increment in RESULT.
3426 The way we do this depends on whether this is pre- or
3427 post-increment. For pre-increment, copy INCLOC to the reload
3428 register, increment it there, then save back. */
3429 if (! post)
3431 if (real_in != result)
3432 emit_insn (gen_move_insn (result, real_in));
3433 if (plus_p)
3434 emit_insn (gen_add2_insn (result, inc));
3435 else
3436 emit_insn (gen_sub2_insn (result, inc));
3437 if (result != incloc)
3438 emit_insn (gen_move_insn (incloc, result));
3440 else
3442 /* Post-increment.
3444 Because this might be a jump insn or a compare, and because
3445 RESULT may not be available after the insn in an input
3446 reload, we must do the incrementing before the insn being
3447 reloaded for.
3449 We have already copied IN to RESULT. Increment the copy in
3450 RESULT, save that back, then decrement RESULT so it has
3451 the original value. */
3452 if (plus_p)
3453 emit_insn (gen_add2_insn (result, inc));
3454 else
3455 emit_insn (gen_sub2_insn (result, inc));
3456 emit_insn (gen_move_insn (incloc, result));
3457 /* Restore non-modified value for the result. We prefer this
3458 way because it does not require an additional hard
3459 register. */
3460 if (plus_p)
3462 if (CONST_INT_P (inc))
3463 emit_insn (gen_add2_insn (result,
3464 gen_int_mode (-INTVAL (inc),
3465 GET_MODE (result))));
3466 else
3467 emit_insn (gen_sub2_insn (result, inc));
3469 else
3470 emit_insn (gen_add2_insn (result, inc));
3472 return result;
3475 /* Return true if the current move insn does not need processing as we
3476 already know that it satisfies its constraints. */
3477 static bool
3478 simple_move_p (void)
3480 rtx dest, src;
3481 enum reg_class dclass, sclass;
3483 lra_assert (curr_insn_set != NULL_RTX);
3484 dest = SET_DEST (curr_insn_set);
3485 src = SET_SRC (curr_insn_set);
3487 /* If the instruction has multiple sets we need to process it even if it
3488 is single_set. This can happen if one or more of the SETs are dead.
3489 See PR73650. */
3490 if (multiple_sets (curr_insn))
3491 return false;
3493 return ((dclass = get_op_class (dest)) != NO_REGS
3494 && (sclass = get_op_class (src)) != NO_REGS
3495 /* The backend guarantees that register moves of cost 2
3496 never need reloads. */
3497 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3500 /* Swap operands NOP and NOP + 1. */
3501 static inline void
3502 swap_operands (int nop)
3504 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3505 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3506 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3507 std::swap (equiv_substition_p[nop], equiv_substition_p[nop + 1]);
3508 /* Swap the duplicates too. */
3509 lra_update_dup (curr_id, nop);
3510 lra_update_dup (curr_id, nop + 1);
3513 /* Main entry point of the constraint code: search the body of the
3514 current insn to choose the best alternative. It is mimicking insn
3515 alternative cost calculation model of former reload pass. That is
3516 because machine descriptions were written to use this model. This
3517 model can be changed in future. Make commutative operand exchange
3518 if it is chosen.
3520 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3521 constraints. Return true if any change happened during function
3522 call.
3524 If CHECK_ONLY_P is true then don't do any transformation. Just
3525 check that the insn satisfies all constraints. If the insn does
3526 not satisfy any constraint, return true. */
3527 static bool
3528 curr_insn_transform (bool check_only_p)
3530 int i, j, k;
3531 int n_operands;
3532 int n_alternatives;
3533 int n_outputs;
3534 int commutative;
3535 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3536 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3537 signed char outputs[MAX_RECOG_OPERANDS + 1];
3538 rtx_insn *before, *after;
3539 bool alt_p = false;
3540 /* Flag that the insn has been changed through a transformation. */
3541 bool change_p;
3542 bool sec_mem_p;
3543 #ifdef SECONDARY_MEMORY_NEEDED
3544 bool use_sec_mem_p;
3545 #endif
3546 int max_regno_before;
3547 int reused_alternative_num;
3549 curr_insn_set = single_set (curr_insn);
3550 if (curr_insn_set != NULL_RTX && simple_move_p ())
3551 return false;
3553 no_input_reloads_p = no_output_reloads_p = false;
3554 goal_alt_number = -1;
3555 change_p = sec_mem_p = false;
3556 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3557 reloads; neither are insns that SET cc0. Insns that use CC0 are
3558 not allowed to have any input reloads. */
3559 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3560 no_output_reloads_p = true;
3562 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3563 no_input_reloads_p = true;
3564 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3565 no_output_reloads_p = true;
3567 n_operands = curr_static_id->n_operands;
3568 n_alternatives = curr_static_id->n_alternatives;
3570 /* Just return "no reloads" if insn has no operands with
3571 constraints. */
3572 if (n_operands == 0 || n_alternatives == 0)
3573 return false;
3575 max_regno_before = max_reg_num ();
3577 for (i = 0; i < n_operands; i++)
3579 goal_alt_matched[i][0] = -1;
3580 goal_alt_matches[i] = -1;
3583 commutative = curr_static_id->commutative;
3585 /* Now see what we need for pseudos that didn't get hard regs or got
3586 the wrong kind of hard reg. For this, we must consider all the
3587 operands together against the register constraints. */
3589 best_losers = best_overall = INT_MAX;
3590 best_reload_sum = 0;
3592 curr_swapped = false;
3593 goal_alt_swapped = false;
3595 if (! check_only_p)
3596 /* Make equivalence substitution and memory subreg elimination
3597 before address processing because an address legitimacy can
3598 depend on memory mode. */
3599 for (i = 0; i < n_operands; i++)
3601 rtx op, subst, old;
3602 bool op_change_p = false;
3604 if (curr_static_id->operand[i].is_operator)
3605 continue;
3607 old = op = *curr_id->operand_loc[i];
3608 if (GET_CODE (old) == SUBREG)
3609 old = SUBREG_REG (old);
3610 subst = get_equiv_with_elimination (old, curr_insn);
3611 original_subreg_reg_mode[i] = VOIDmode;
3612 equiv_substition_p[i] = false;
3613 if (subst != old)
3615 equiv_substition_p[i] = true;
3616 subst = copy_rtx (subst);
3617 lra_assert (REG_P (old));
3618 if (GET_CODE (op) != SUBREG)
3619 *curr_id->operand_loc[i] = subst;
3620 else
3622 SUBREG_REG (op) = subst;
3623 if (GET_MODE (subst) == VOIDmode)
3624 original_subreg_reg_mode[i] = GET_MODE (old);
3626 if (lra_dump_file != NULL)
3628 fprintf (lra_dump_file,
3629 "Changing pseudo %d in operand %i of insn %u on equiv ",
3630 REGNO (old), i, INSN_UID (curr_insn));
3631 dump_value_slim (lra_dump_file, subst, 1);
3632 fprintf (lra_dump_file, "\n");
3634 op_change_p = change_p = true;
3636 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3638 change_p = true;
3639 lra_update_dup (curr_id, i);
3643 /* Reload address registers and displacements. We do it before
3644 finding an alternative because of memory constraints. */
3645 before = after = NULL;
3646 for (i = 0; i < n_operands; i++)
3647 if (! curr_static_id->operand[i].is_operator
3648 && process_address (i, check_only_p, &before, &after))
3650 if (check_only_p)
3651 return true;
3652 change_p = true;
3653 lra_update_dup (curr_id, i);
3656 if (change_p)
3657 /* If we've changed the instruction then any alternative that
3658 we chose previously may no longer be valid. */
3659 lra_set_used_insn_alternative (curr_insn, -1);
3661 if (! check_only_p && curr_insn_set != NULL_RTX
3662 && check_and_process_move (&change_p, &sec_mem_p))
3663 return change_p;
3665 try_swapped:
3667 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3668 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3669 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3670 reused_alternative_num, INSN_UID (curr_insn));
3672 if (process_alt_operands (reused_alternative_num))
3673 alt_p = true;
3675 if (check_only_p)
3676 return ! alt_p || best_losers != 0;
3678 /* If insn is commutative (it's safe to exchange a certain pair of
3679 operands) then we need to try each alternative twice, the second
3680 time matching those two operands as if we had exchanged them. To
3681 do this, really exchange them in operands.
3683 If we have just tried the alternatives the second time, return
3684 operands to normal and drop through. */
3686 if (reused_alternative_num < 0 && commutative >= 0)
3688 curr_swapped = !curr_swapped;
3689 if (curr_swapped)
3691 swap_operands (commutative);
3692 goto try_swapped;
3694 else
3695 swap_operands (commutative);
3698 if (! alt_p && ! sec_mem_p)
3700 /* No alternative works with reloads?? */
3701 if (INSN_CODE (curr_insn) >= 0)
3702 fatal_insn ("unable to generate reloads for:", curr_insn);
3703 error_for_asm (curr_insn,
3704 "inconsistent operand constraints in an %<asm%>");
3705 /* Avoid further trouble with this insn. */
3706 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3707 lra_invalidate_insn_data (curr_insn);
3708 return true;
3711 /* If the best alternative is with operands 1 and 2 swapped, swap
3712 them. Update the operand numbers of any reloads already
3713 pushed. */
3715 if (goal_alt_swapped)
3717 if (lra_dump_file != NULL)
3718 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3719 INSN_UID (curr_insn));
3721 /* Swap the duplicates too. */
3722 swap_operands (commutative);
3723 change_p = true;
3726 #ifdef SECONDARY_MEMORY_NEEDED
3727 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3728 too conservatively. So we use the secondary memory only if there
3729 is no any alternative without reloads. */
3730 use_sec_mem_p = false;
3731 if (! alt_p)
3732 use_sec_mem_p = true;
3733 else if (sec_mem_p)
3735 for (i = 0; i < n_operands; i++)
3736 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3737 break;
3738 use_sec_mem_p = i < n_operands;
3741 if (use_sec_mem_p)
3743 int in = -1, out = -1;
3744 rtx new_reg, src, dest, rld;
3745 machine_mode sec_mode, rld_mode;
3747 lra_assert (curr_insn_set != NULL_RTX && sec_mem_p);
3748 dest = SET_DEST (curr_insn_set);
3749 src = SET_SRC (curr_insn_set);
3750 for (i = 0; i < n_operands; i++)
3751 if (*curr_id->operand_loc[i] == dest)
3752 out = i;
3753 else if (*curr_id->operand_loc[i] == src)
3754 in = i;
3755 for (i = 0; i < curr_static_id->n_dups; i++)
3756 if (out < 0 && *curr_id->dup_loc[i] == dest)
3757 out = curr_static_id->dup_num[i];
3758 else if (in < 0 && *curr_id->dup_loc[i] == src)
3759 in = curr_static_id->dup_num[i];
3760 lra_assert (out >= 0 && in >= 0
3761 && curr_static_id->operand[out].type == OP_OUT
3762 && curr_static_id->operand[in].type == OP_IN);
3763 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3764 ? dest : src);
3765 rld_mode = GET_MODE (rld);
3766 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3767 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3768 #else
3769 sec_mode = rld_mode;
3770 #endif
3771 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3772 NO_REGS, "secondary");
3773 /* If the mode is changed, it should be wider. */
3774 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3775 if (sec_mode != rld_mode)
3777 /* If the target says specifically to use another mode for
3778 secondary memory moves we can not reuse the original
3779 insn. */
3780 after = emit_spill_move (false, new_reg, dest);
3781 lra_process_new_insns (curr_insn, NULL, after,
3782 "Inserting the sec. move");
3783 /* We may have non null BEFORE here (e.g. after address
3784 processing. */
3785 push_to_sequence (before);
3786 before = emit_spill_move (true, new_reg, src);
3787 emit_insn (before);
3788 before = get_insns ();
3789 end_sequence ();
3790 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3791 lra_set_insn_deleted (curr_insn);
3793 else if (dest == rld)
3795 *curr_id->operand_loc[out] = new_reg;
3796 lra_update_dup (curr_id, out);
3797 after = emit_spill_move (false, new_reg, dest);
3798 lra_process_new_insns (curr_insn, NULL, after,
3799 "Inserting the sec. move");
3801 else
3803 *curr_id->operand_loc[in] = new_reg;
3804 lra_update_dup (curr_id, in);
3805 /* See comments above. */
3806 push_to_sequence (before);
3807 before = emit_spill_move (true, new_reg, src);
3808 emit_insn (before);
3809 before = get_insns ();
3810 end_sequence ();
3811 lra_process_new_insns (curr_insn, before, NULL,
3812 "Inserting the sec. move");
3814 lra_update_insn_regno_info (curr_insn);
3815 return true;
3817 #endif
3819 lra_assert (goal_alt_number >= 0);
3820 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3822 if (lra_dump_file != NULL)
3824 const char *p;
3826 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3827 goal_alt_number, INSN_UID (curr_insn));
3828 for (i = 0; i < n_operands; i++)
3830 p = (curr_static_id->operand_alternative
3831 [goal_alt_number * n_operands + i].constraint);
3832 if (*p == '\0')
3833 continue;
3834 fprintf (lra_dump_file, " (%d) ", i);
3835 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3836 fputc (*p, lra_dump_file);
3838 if (INSN_CODE (curr_insn) >= 0
3839 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3840 fprintf (lra_dump_file, " {%s}", p);
3841 if (curr_id->sp_offset != 0)
3842 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3843 curr_id->sp_offset);
3844 fprintf (lra_dump_file, "\n");
3847 /* Right now, for any pair of operands I and J that are required to
3848 match, with J < I, goal_alt_matches[I] is J. Add I to
3849 goal_alt_matched[J]. */
3851 for (i = 0; i < n_operands; i++)
3852 if ((j = goal_alt_matches[i]) >= 0)
3854 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3856 /* We allow matching one output operand and several input
3857 operands. */
3858 lra_assert (k == 0
3859 || (curr_static_id->operand[j].type == OP_OUT
3860 && curr_static_id->operand[i].type == OP_IN
3861 && (curr_static_id->operand
3862 [goal_alt_matched[j][0]].type == OP_IN)));
3863 goal_alt_matched[j][k] = i;
3864 goal_alt_matched[j][k + 1] = -1;
3867 for (i = 0; i < n_operands; i++)
3868 goal_alt_win[i] |= goal_alt_match_win[i];
3870 /* Any constants that aren't allowed and can't be reloaded into
3871 registers are here changed into memory references. */
3872 for (i = 0; i < n_operands; i++)
3873 if (goal_alt_win[i])
3875 int regno;
3876 enum reg_class new_class;
3877 rtx reg = *curr_id->operand_loc[i];
3879 if (GET_CODE (reg) == SUBREG)
3880 reg = SUBREG_REG (reg);
3882 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3884 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3886 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3888 lra_assert (ok_p);
3889 lra_change_class (regno, new_class, " Change to", true);
3893 else
3895 const char *constraint;
3896 char c;
3897 rtx op = *curr_id->operand_loc[i];
3898 rtx subreg = NULL_RTX;
3899 machine_mode mode = curr_operand_mode[i];
3901 if (GET_CODE (op) == SUBREG)
3903 subreg = op;
3904 op = SUBREG_REG (op);
3905 mode = GET_MODE (op);
3908 if (CONST_POOL_OK_P (mode, op)
3909 && ((targetm.preferred_reload_class
3910 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3911 || no_input_reloads_p))
3913 rtx tem = force_const_mem (mode, op);
3915 change_p = true;
3916 if (subreg != NULL_RTX)
3917 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3919 *curr_id->operand_loc[i] = tem;
3920 lra_update_dup (curr_id, i);
3921 process_address (i, false, &before, &after);
3923 /* If the alternative accepts constant pool refs directly
3924 there will be no reload needed at all. */
3925 if (subreg != NULL_RTX)
3926 continue;
3927 /* Skip alternatives before the one requested. */
3928 constraint = (curr_static_id->operand_alternative
3929 [goal_alt_number * n_operands + i].constraint);
3930 for (;
3931 (c = *constraint) && c != ',' && c != '#';
3932 constraint += CONSTRAINT_LEN (c, constraint))
3934 enum constraint_num cn = lookup_constraint (constraint);
3935 if ((insn_extra_memory_constraint (cn)
3936 || insn_extra_special_memory_constraint (cn))
3937 && satisfies_memory_constraint_p (tem, cn))
3938 break;
3940 if (c == '\0' || c == ',' || c == '#')
3941 continue;
3943 goal_alt_win[i] = true;
3947 n_outputs = 0;
3948 outputs[0] = -1;
3949 for (i = 0; i < n_operands; i++)
3951 int regno;
3952 bool optional_p = false;
3953 rtx old, new_reg;
3954 rtx op = *curr_id->operand_loc[i];
3956 if (goal_alt_win[i])
3958 if (goal_alt[i] == NO_REGS
3959 && REG_P (op)
3960 /* When we assign NO_REGS it means that we will not
3961 assign a hard register to the scratch pseudo by
3962 assigment pass and the scratch pseudo will be
3963 spilled. Spilled scratch pseudos are transformed
3964 back to scratches at the LRA end. */
3965 && lra_former_scratch_operand_p (curr_insn, i)
3966 && lra_former_scratch_p (REGNO (op)))
3968 int regno = REGNO (op);
3969 lra_change_class (regno, NO_REGS, " Change to", true);
3970 if (lra_get_regno_hard_regno (regno) >= 0)
3971 /* We don't have to mark all insn affected by the
3972 spilled pseudo as there is only one such insn, the
3973 current one. */
3974 reg_renumber[regno] = -1;
3975 lra_assert (bitmap_single_bit_set_p
3976 (&lra_reg_info[REGNO (op)].insn_bitmap));
3978 /* We can do an optional reload. If the pseudo got a hard
3979 reg, we might improve the code through inheritance. If
3980 it does not get a hard register we coalesce memory/memory
3981 moves later. Ignore move insns to avoid cycling. */
3982 if (! lra_simple_p
3983 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3984 && goal_alt[i] != NO_REGS && REG_P (op)
3985 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3986 && regno < new_regno_start
3987 && ! lra_former_scratch_p (regno)
3988 && reg_renumber[regno] < 0
3989 /* Check that the optional reload pseudo will be able to
3990 hold given mode value. */
3991 && ! (prohibited_class_reg_set_mode_p
3992 (goal_alt[i], reg_class_contents[goal_alt[i]],
3993 PSEUDO_REGNO_MODE (regno)))
3994 && (curr_insn_set == NULL_RTX
3995 || !((REG_P (SET_SRC (curr_insn_set))
3996 || MEM_P (SET_SRC (curr_insn_set))
3997 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3998 && (REG_P (SET_DEST (curr_insn_set))
3999 || MEM_P (SET_DEST (curr_insn_set))
4000 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
4001 optional_p = true;
4002 else
4003 continue;
4006 /* Operands that match previous ones have already been handled. */
4007 if (goal_alt_matches[i] >= 0)
4008 continue;
4010 /* We should not have an operand with a non-offsettable address
4011 appearing where an offsettable address will do. It also may
4012 be a case when the address should be special in other words
4013 not a general one (e.g. it needs no index reg). */
4014 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
4016 enum reg_class rclass;
4017 rtx *loc = &XEXP (op, 0);
4018 enum rtx_code code = GET_CODE (*loc);
4020 push_to_sequence (before);
4021 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
4022 MEM, SCRATCH);
4023 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
4024 new_reg = emit_inc (rclass, *loc, *loc,
4025 /* This value does not matter for MODIFY. */
4026 GET_MODE_SIZE (GET_MODE (op)));
4027 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
4028 "offsetable address", &new_reg))
4029 lra_emit_move (new_reg, *loc);
4030 before = get_insns ();
4031 end_sequence ();
4032 *loc = new_reg;
4033 lra_update_dup (curr_id, i);
4035 else if (goal_alt_matched[i][0] == -1)
4037 machine_mode mode;
4038 rtx reg, *loc;
4039 int hard_regno, byte;
4040 enum op_type type = curr_static_id->operand[i].type;
4042 loc = curr_id->operand_loc[i];
4043 mode = curr_operand_mode[i];
4044 if (GET_CODE (*loc) == SUBREG)
4046 reg = SUBREG_REG (*loc);
4047 byte = SUBREG_BYTE (*loc);
4048 if (REG_P (reg)
4049 /* Strict_low_part requires reload the register not
4050 the sub-register. */
4051 && (curr_static_id->operand[i].strict_low
4052 || (GET_MODE_SIZE (mode)
4053 <= GET_MODE_SIZE (GET_MODE (reg))
4054 && (hard_regno
4055 = get_try_hard_regno (REGNO (reg))) >= 0
4056 && (simplify_subreg_regno
4057 (hard_regno,
4058 GET_MODE (reg), byte, mode) < 0)
4059 && (goal_alt[i] == NO_REGS
4060 || (simplify_subreg_regno
4061 (ira_class_hard_regs[goal_alt[i]][0],
4062 GET_MODE (reg), byte, mode) >= 0)))))
4064 if (type == OP_OUT)
4065 type = OP_INOUT;
4066 loc = &SUBREG_REG (*loc);
4067 mode = GET_MODE (*loc);
4070 old = *loc;
4071 if (get_reload_reg (type, mode, old, goal_alt[i],
4072 loc != curr_id->operand_loc[i], "", &new_reg)
4073 && type != OP_OUT)
4075 push_to_sequence (before);
4076 lra_emit_move (new_reg, old);
4077 before = get_insns ();
4078 end_sequence ();
4080 *loc = new_reg;
4081 if (type != OP_IN
4082 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
4084 start_sequence ();
4085 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
4086 emit_insn (after);
4087 after = get_insns ();
4088 end_sequence ();
4089 *loc = new_reg;
4091 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
4092 if (goal_alt_dont_inherit_ops[j] == i)
4094 lra_set_regno_unique_value (REGNO (new_reg));
4095 break;
4097 lra_update_dup (curr_id, i);
4099 else if (curr_static_id->operand[i].type == OP_IN
4100 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4101 == OP_OUT))
4103 /* generate reloads for input and matched outputs. */
4104 match_inputs[0] = i;
4105 match_inputs[1] = -1;
4106 match_reload (goal_alt_matched[i][0], match_inputs, outputs,
4107 goal_alt[i], &before, &after,
4108 curr_static_id->operand_alternative
4109 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
4110 .earlyclobber);
4112 else if (curr_static_id->operand[i].type == OP_OUT
4113 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4114 == OP_IN))
4115 /* Generate reloads for output and matched inputs. */
4116 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i], &before,
4117 &after, curr_static_id->operand_alternative
4118 [goal_alt_number * n_operands + i].earlyclobber);
4119 else if (curr_static_id->operand[i].type == OP_IN
4120 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4121 == OP_IN))
4123 /* Generate reloads for matched inputs. */
4124 match_inputs[0] = i;
4125 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
4126 match_inputs[j + 1] = k;
4127 match_inputs[j + 1] = -1;
4128 match_reload (-1, match_inputs, outputs, goal_alt[i], &before,
4129 &after, false);
4131 else
4132 /* We must generate code in any case when function
4133 process_alt_operands decides that it is possible. */
4134 gcc_unreachable ();
4136 /* Memorise processed outputs so that output remaining to be processed
4137 can avoid using the same register value (see match_reload). */
4138 if (curr_static_id->operand[i].type == OP_OUT)
4140 outputs[n_outputs++] = i;
4141 outputs[n_outputs] = -1;
4144 if (optional_p)
4146 rtx reg = op;
4148 lra_assert (REG_P (reg));
4149 regno = REGNO (reg);
4150 op = *curr_id->operand_loc[i]; /* Substitution. */
4151 if (GET_CODE (op) == SUBREG)
4152 op = SUBREG_REG (op);
4153 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
4154 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
4155 lra_reg_info[REGNO (op)].restore_rtx = reg;
4156 if (lra_dump_file != NULL)
4157 fprintf (lra_dump_file,
4158 " Making reload reg %d for reg %d optional\n",
4159 REGNO (op), regno);
4162 if (before != NULL_RTX || after != NULL_RTX
4163 || max_regno_before != max_reg_num ())
4164 change_p = true;
4165 if (change_p)
4167 lra_update_operator_dups (curr_id);
4168 /* Something changes -- process the insn. */
4169 lra_update_insn_regno_info (curr_insn);
4171 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
4172 return change_p;
4175 /* Return true if INSN satisfies all constraints. In other words, no
4176 reload insns are needed. */
4177 bool
4178 lra_constrain_insn (rtx_insn *insn)
4180 int saved_new_regno_start = new_regno_start;
4181 int saved_new_insn_uid_start = new_insn_uid_start;
4182 bool change_p;
4184 curr_insn = insn;
4185 curr_id = lra_get_insn_recog_data (curr_insn);
4186 curr_static_id = curr_id->insn_static_data;
4187 new_insn_uid_start = get_max_uid ();
4188 new_regno_start = max_reg_num ();
4189 change_p = curr_insn_transform (true);
4190 new_regno_start = saved_new_regno_start;
4191 new_insn_uid_start = saved_new_insn_uid_start;
4192 return ! change_p;
4195 /* Return true if X is in LIST. */
4196 static bool
4197 in_list_p (rtx x, rtx list)
4199 for (; list != NULL_RTX; list = XEXP (list, 1))
4200 if (XEXP (list, 0) == x)
4201 return true;
4202 return false;
4205 /* Return true if X contains an allocatable hard register (if
4206 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
4207 static bool
4208 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
4210 int i, j;
4211 const char *fmt;
4212 enum rtx_code code;
4214 code = GET_CODE (x);
4215 if (REG_P (x))
4217 int regno = REGNO (x);
4218 HARD_REG_SET alloc_regs;
4220 if (hard_reg_p)
4222 if (regno >= FIRST_PSEUDO_REGISTER)
4223 regno = lra_get_regno_hard_regno (regno);
4224 if (regno < 0)
4225 return false;
4226 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
4227 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
4229 else
4231 if (regno < FIRST_PSEUDO_REGISTER)
4232 return false;
4233 if (! spilled_p)
4234 return true;
4235 return lra_get_regno_hard_regno (regno) < 0;
4238 fmt = GET_RTX_FORMAT (code);
4239 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4241 if (fmt[i] == 'e')
4243 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4244 return true;
4246 else if (fmt[i] == 'E')
4248 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4249 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4250 return true;
4253 return false;
4256 /* Process all regs in location *LOC and change them on equivalent
4257 substitution. Return true if any change was done. */
4258 static bool
4259 loc_equivalence_change_p (rtx *loc)
4261 rtx subst, reg, x = *loc;
4262 bool result = false;
4263 enum rtx_code code = GET_CODE (x);
4264 const char *fmt;
4265 int i, j;
4267 if (code == SUBREG)
4269 reg = SUBREG_REG (x);
4270 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4271 && GET_MODE (subst) == VOIDmode)
4273 /* We cannot reload debug location. Simplify subreg here
4274 while we know the inner mode. */
4275 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4276 GET_MODE (reg), SUBREG_BYTE (x));
4277 return true;
4280 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4282 *loc = subst;
4283 return true;
4286 /* Scan all the operand sub-expressions. */
4287 fmt = GET_RTX_FORMAT (code);
4288 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4290 if (fmt[i] == 'e')
4291 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4292 else if (fmt[i] == 'E')
4293 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4294 result
4295 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4297 return result;
4300 /* Similar to loc_equivalence_change_p, but for use as
4301 simplify_replace_fn_rtx callback. DATA is insn for which the
4302 elimination is done. If it null we don't do the elimination. */
4303 static rtx
4304 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4306 if (!REG_P (loc))
4307 return NULL_RTX;
4309 rtx subst = (data == NULL
4310 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4311 if (subst != loc)
4312 return subst;
4314 return NULL_RTX;
4317 /* Maximum number of generated reload insns per an insn. It is for
4318 preventing this pass cycling in a bug case. */
4319 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4321 /* The current iteration number of this LRA pass. */
4322 int lra_constraint_iter;
4324 /* True if we substituted equiv which needs checking register
4325 allocation correctness because the equivalent value contains
4326 allocatable hard registers or when we restore multi-register
4327 pseudo. */
4328 bool lra_risky_transformations_p;
4330 /* Return true if REGNO is referenced in more than one block. */
4331 static bool
4332 multi_block_pseudo_p (int regno)
4334 basic_block bb = NULL;
4335 unsigned int uid;
4336 bitmap_iterator bi;
4338 if (regno < FIRST_PSEUDO_REGISTER)
4339 return false;
4341 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4342 if (bb == NULL)
4343 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4344 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4345 return true;
4346 return false;
4349 /* Return true if LIST contains a deleted insn. */
4350 static bool
4351 contains_deleted_insn_p (rtx_insn_list *list)
4353 for (; list != NULL_RTX; list = list->next ())
4354 if (NOTE_P (list->insn ())
4355 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4356 return true;
4357 return false;
4360 /* Return true if X contains a pseudo dying in INSN. */
4361 static bool
4362 dead_pseudo_p (rtx x, rtx_insn *insn)
4364 int i, j;
4365 const char *fmt;
4366 enum rtx_code code;
4368 if (REG_P (x))
4369 return (insn != NULL_RTX
4370 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4371 code = GET_CODE (x);
4372 fmt = GET_RTX_FORMAT (code);
4373 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4375 if (fmt[i] == 'e')
4377 if (dead_pseudo_p (XEXP (x, i), insn))
4378 return true;
4380 else if (fmt[i] == 'E')
4382 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4383 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4384 return true;
4387 return false;
4390 /* Return true if INSN contains a dying pseudo in INSN right hand
4391 side. */
4392 static bool
4393 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4395 rtx set = single_set (insn);
4397 gcc_assert (set != NULL);
4398 return dead_pseudo_p (SET_SRC (set), insn);
4401 /* Return true if any init insn of REGNO contains a dying pseudo in
4402 insn right hand side. */
4403 static bool
4404 init_insn_rhs_dead_pseudo_p (int regno)
4406 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4408 if (insns == NULL)
4409 return false;
4410 for (; insns != NULL_RTX; insns = insns->next ())
4411 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4412 return true;
4413 return false;
4416 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4417 reverse only if we have one init insn with given REGNO as a
4418 source. */
4419 static bool
4420 reverse_equiv_p (int regno)
4422 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4423 rtx set;
4425 if (insns == NULL)
4426 return false;
4427 if (! INSN_P (insns->insn ())
4428 || insns->next () != NULL)
4429 return false;
4430 if ((set = single_set (insns->insn ())) == NULL_RTX)
4431 return false;
4432 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4435 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4436 call this function only for non-reverse equivalence. */
4437 static bool
4438 contains_reloaded_insn_p (int regno)
4440 rtx set;
4441 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4443 for (; list != NULL; list = list->next ())
4444 if ((set = single_set (list->insn ())) == NULL_RTX
4445 || ! REG_P (SET_DEST (set))
4446 || (int) REGNO (SET_DEST (set)) != regno)
4447 return true;
4448 return false;
4451 /* Entry function of LRA constraint pass. Return true if the
4452 constraint pass did change the code. */
4453 bool
4454 lra_constraints (bool first_p)
4456 bool changed_p;
4457 int i, hard_regno, new_insns_num;
4458 unsigned int min_len, new_min_len, uid;
4459 rtx set, x, reg, dest_reg;
4460 basic_block last_bb;
4461 bitmap_head equiv_insn_bitmap;
4462 bitmap_iterator bi;
4464 lra_constraint_iter++;
4465 if (lra_dump_file != NULL)
4466 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4467 lra_constraint_iter);
4468 changed_p = false;
4469 if (pic_offset_table_rtx
4470 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4471 lra_risky_transformations_p = true;
4472 else
4473 lra_risky_transformations_p = false;
4474 new_insn_uid_start = get_max_uid ();
4475 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4476 /* Mark used hard regs for target stack size calulations. */
4477 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4478 if (lra_reg_info[i].nrefs != 0
4479 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4481 int j, nregs;
4483 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4484 for (j = 0; j < nregs; j++)
4485 df_set_regs_ever_live (hard_regno + j, true);
4487 /* Do elimination before the equivalence processing as we can spill
4488 some pseudos during elimination. */
4489 lra_eliminate (false, first_p);
4490 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4491 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4492 if (lra_reg_info[i].nrefs != 0)
4494 ira_reg_equiv[i].profitable_p = true;
4495 reg = regno_reg_rtx[i];
4496 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4498 bool pseudo_p = contains_reg_p (x, false, false);
4500 /* After RTL transformation, we can not guarantee that
4501 pseudo in the substitution was not reloaded which might
4502 make equivalence invalid. For example, in reverse
4503 equiv of p0
4505 p0 <- ...
4507 equiv_mem <- p0
4509 the memory address register was reloaded before the 2nd
4510 insn. */
4511 if ((! first_p && pseudo_p)
4512 /* We don't use DF for compilation speed sake. So it
4513 is problematic to update live info when we use an
4514 equivalence containing pseudos in more than one
4515 BB. */
4516 || (pseudo_p && multi_block_pseudo_p (i))
4517 /* If an init insn was deleted for some reason, cancel
4518 the equiv. We could update the equiv insns after
4519 transformations including an equiv insn deletion
4520 but it is not worthy as such cases are extremely
4521 rare. */
4522 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4523 /* If it is not a reverse equivalence, we check that a
4524 pseudo in rhs of the init insn is not dying in the
4525 insn. Otherwise, the live info at the beginning of
4526 the corresponding BB might be wrong after we
4527 removed the insn. When the equiv can be a
4528 constant, the right hand side of the init insn can
4529 be a pseudo. */
4530 || (! reverse_equiv_p (i)
4531 && (init_insn_rhs_dead_pseudo_p (i)
4532 /* If we reloaded the pseudo in an equivalence
4533 init insn, we can not remove the equiv init
4534 insns and the init insns might write into
4535 const memory in this case. */
4536 || contains_reloaded_insn_p (i)))
4537 /* Prevent access beyond equivalent memory for
4538 paradoxical subregs. */
4539 || (MEM_P (x)
4540 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4541 > GET_MODE_SIZE (GET_MODE (x))))
4542 || (pic_offset_table_rtx
4543 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4544 && (targetm.preferred_reload_class
4545 (x, lra_get_allocno_class (i)) == NO_REGS))
4546 || contains_symbol_ref_p (x))))
4547 ira_reg_equiv[i].defined_p = false;
4548 if (contains_reg_p (x, false, true))
4549 ira_reg_equiv[i].profitable_p = false;
4550 if (get_equiv (reg) != reg)
4551 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4554 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4555 update_equiv (i);
4556 /* We should add all insns containing pseudos which should be
4557 substituted by their equivalences. */
4558 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4559 lra_push_insn_by_uid (uid);
4560 min_len = lra_insn_stack_length ();
4561 new_insns_num = 0;
4562 last_bb = NULL;
4563 changed_p = false;
4564 while ((new_min_len = lra_insn_stack_length ()) != 0)
4566 curr_insn = lra_pop_insn ();
4567 --new_min_len;
4568 curr_bb = BLOCK_FOR_INSN (curr_insn);
4569 if (curr_bb != last_bb)
4571 last_bb = curr_bb;
4572 bb_reload_num = lra_curr_reload_num;
4574 if (min_len > new_min_len)
4576 min_len = new_min_len;
4577 new_insns_num = 0;
4579 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4580 internal_error
4581 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4582 MAX_RELOAD_INSNS_NUMBER);
4583 new_insns_num++;
4584 if (DEBUG_INSN_P (curr_insn))
4586 /* We need to check equivalence in debug insn and change
4587 pseudo to the equivalent value if necessary. */
4588 curr_id = lra_get_insn_recog_data (curr_insn);
4589 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4591 rtx old = *curr_id->operand_loc[0];
4592 *curr_id->operand_loc[0]
4593 = simplify_replace_fn_rtx (old, NULL_RTX,
4594 loc_equivalence_callback, curr_insn);
4595 if (old != *curr_id->operand_loc[0])
4597 lra_update_insn_regno_info (curr_insn);
4598 changed_p = true;
4602 else if (INSN_P (curr_insn))
4604 if ((set = single_set (curr_insn)) != NULL_RTX)
4606 dest_reg = SET_DEST (set);
4607 /* The equivalence pseudo could be set up as SUBREG in a
4608 case when it is a call restore insn in a mode
4609 different from the pseudo mode. */
4610 if (GET_CODE (dest_reg) == SUBREG)
4611 dest_reg = SUBREG_REG (dest_reg);
4612 if ((REG_P (dest_reg)
4613 && (x = get_equiv (dest_reg)) != dest_reg
4614 /* Remove insns which set up a pseudo whose value
4615 can not be changed. Such insns might be not in
4616 init_insns because we don't update equiv data
4617 during insn transformations.
4619 As an example, let suppose that a pseudo got
4620 hard register and on the 1st pass was not
4621 changed to equivalent constant. We generate an
4622 additional insn setting up the pseudo because of
4623 secondary memory movement. Then the pseudo is
4624 spilled and we use the equiv constant. In this
4625 case we should remove the additional insn and
4626 this insn is not init_insns list. */
4627 && (! MEM_P (x) || MEM_READONLY_P (x)
4628 /* Check that this is actually an insn setting
4629 up the equivalence. */
4630 || in_list_p (curr_insn,
4631 ira_reg_equiv
4632 [REGNO (dest_reg)].init_insns)))
4633 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4634 && in_list_p (curr_insn,
4635 ira_reg_equiv
4636 [REGNO (SET_SRC (set))].init_insns)))
4638 /* This is equiv init insn of pseudo which did not get a
4639 hard register -- remove the insn. */
4640 if (lra_dump_file != NULL)
4642 fprintf (lra_dump_file,
4643 " Removing equiv init insn %i (freq=%d)\n",
4644 INSN_UID (curr_insn),
4645 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4646 dump_insn_slim (lra_dump_file, curr_insn);
4648 if (contains_reg_p (x, true, false))
4649 lra_risky_transformations_p = true;
4650 lra_set_insn_deleted (curr_insn);
4651 continue;
4654 curr_id = lra_get_insn_recog_data (curr_insn);
4655 curr_static_id = curr_id->insn_static_data;
4656 init_curr_insn_input_reloads ();
4657 init_curr_operand_mode ();
4658 if (curr_insn_transform (false))
4659 changed_p = true;
4660 /* Check non-transformed insns too for equiv change as USE
4661 or CLOBBER don't need reloads but can contain pseudos
4662 being changed on their equivalences. */
4663 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4664 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4666 lra_update_insn_regno_info (curr_insn);
4667 changed_p = true;
4671 bitmap_clear (&equiv_insn_bitmap);
4672 /* If we used a new hard regno, changed_p should be true because the
4673 hard reg is assigned to a new pseudo. */
4674 if (flag_checking && !changed_p)
4676 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4677 if (lra_reg_info[i].nrefs != 0
4678 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4680 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4682 for (j = 0; j < nregs; j++)
4683 lra_assert (df_regs_ever_live_p (hard_regno + j));
4686 return changed_p;
4689 static void initiate_invariants (void);
4690 static void finish_invariants (void);
4692 /* Initiate the LRA constraint pass. It is done once per
4693 function. */
4694 void
4695 lra_constraints_init (void)
4697 initiate_invariants ();
4700 /* Finalize the LRA constraint pass. It is done once per
4701 function. */
4702 void
4703 lra_constraints_finish (void)
4705 finish_invariants ();
4710 /* Structure describes invariants for ineheritance. */
4711 struct invariant
4713 /* The order number of the invariant. */
4714 int num;
4715 /* The invariant RTX. */
4716 rtx invariant_rtx;
4717 /* The origin insn of the invariant. */
4718 rtx_insn *insn;
4721 typedef struct invariant invariant_t;
4722 typedef invariant_t *invariant_ptr_t;
4723 typedef const invariant_t *const_invariant_ptr_t;
4725 /* Pointer to the inheritance invariants. */
4726 static vec<invariant_ptr_t> invariants;
4728 /* Allocation pool for the invariants. */
4729 static object_allocator<struct invariant> *invariants_pool;
4731 /* Hash table for the invariants. */
4732 static htab_t invariant_table;
4734 /* Hash function for INVARIANT. */
4735 static hashval_t
4736 invariant_hash (const void *invariant)
4738 rtx inv = ((const_invariant_ptr_t) invariant)->invariant_rtx;
4739 return lra_rtx_hash (inv);
4742 /* Equal function for invariants INVARIANT1 and INVARIANT2. */
4743 static int
4744 invariant_eq_p (const void *invariant1, const void *invariant2)
4746 rtx inv1 = ((const_invariant_ptr_t) invariant1)->invariant_rtx;
4747 rtx inv2 = ((const_invariant_ptr_t) invariant2)->invariant_rtx;
4749 return rtx_equal_p (inv1, inv2);
4752 /* Insert INVARIANT_RTX into the table if it is not there yet. Return
4753 invariant which is in the table. */
4754 static invariant_ptr_t
4755 insert_invariant (rtx invariant_rtx)
4757 void **entry_ptr;
4758 invariant_t invariant;
4759 invariant_ptr_t invariant_ptr;
4761 invariant.invariant_rtx = invariant_rtx;
4762 entry_ptr = htab_find_slot (invariant_table, &invariant, INSERT);
4763 if (*entry_ptr == NULL)
4765 invariant_ptr = invariants_pool->allocate ();
4766 invariant_ptr->invariant_rtx = invariant_rtx;
4767 invariant_ptr->insn = NULL;
4768 invariants.safe_push (invariant_ptr);
4769 *entry_ptr = (void *) invariant_ptr;
4771 return (invariant_ptr_t) *entry_ptr;
4774 /* Initiate the invariant table. */
4775 static void
4776 initiate_invariants (void)
4778 invariants.create (100);
4779 invariants_pool = new object_allocator<struct invariant> ("Inheritance invariants");
4780 invariant_table = htab_create (100, invariant_hash, invariant_eq_p, NULL);
4783 /* Finish the invariant table. */
4784 static void
4785 finish_invariants (void)
4787 htab_delete (invariant_table);
4788 delete invariants_pool;
4789 invariants.release ();
4792 /* Make the invariant table empty. */
4793 static void
4794 clear_invariants (void)
4796 htab_empty (invariant_table);
4797 invariants_pool->release ();
4798 invariants.truncate (0);
4803 /* This page contains code to do inheritance/split
4804 transformations. */
4806 /* Number of reloads passed so far in current EBB. */
4807 static int reloads_num;
4809 /* Number of calls passed so far in current EBB. */
4810 static int calls_num;
4812 /* Current reload pseudo check for validity of elements in
4813 USAGE_INSNS. */
4814 static int curr_usage_insns_check;
4816 /* Info about last usage of registers in EBB to do inheritance/split
4817 transformation. Inheritance transformation is done from a spilled
4818 pseudo and split transformations from a hard register or a pseudo
4819 assigned to a hard register. */
4820 struct usage_insns
4822 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4823 value INSNS is valid. The insns is chain of optional debug insns
4824 and a finishing non-debug insn using the corresponding reg. The
4825 value is also used to mark the registers which are set up in the
4826 current insn. The negated insn uid is used for this. */
4827 int check;
4828 /* Value of global reloads_num at the last insn in INSNS. */
4829 int reloads_num;
4830 /* Value of global reloads_nums at the last insn in INSNS. */
4831 int calls_num;
4832 /* It can be true only for splitting. And it means that the restore
4833 insn should be put after insn given by the following member. */
4834 bool after_p;
4835 /* Next insns in the current EBB which use the original reg and the
4836 original reg value is not changed between the current insn and
4837 the next insns. In order words, e.g. for inheritance, if we need
4838 to use the original reg value again in the next insns we can try
4839 to use the value in a hard register from a reload insn of the
4840 current insn. */
4841 rtx insns;
4844 /* Map: regno -> corresponding pseudo usage insns. */
4845 static struct usage_insns *usage_insns;
4847 static void
4848 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4850 usage_insns[regno].check = curr_usage_insns_check;
4851 usage_insns[regno].insns = insn;
4852 usage_insns[regno].reloads_num = reloads_num;
4853 usage_insns[regno].calls_num = calls_num;
4854 usage_insns[regno].after_p = after_p;
4857 /* The function is used to form list REGNO usages which consists of
4858 optional debug insns finished by a non-debug insn using REGNO.
4859 RELOADS_NUM is current number of reload insns processed so far. */
4860 static void
4861 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
4863 rtx next_usage_insns;
4865 if (usage_insns[regno].check == curr_usage_insns_check
4866 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4867 && DEBUG_INSN_P (insn))
4869 /* Check that we did not add the debug insn yet. */
4870 if (next_usage_insns != insn
4871 && (GET_CODE (next_usage_insns) != INSN_LIST
4872 || XEXP (next_usage_insns, 0) != insn))
4873 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4874 next_usage_insns);
4876 else if (NONDEBUG_INSN_P (insn))
4877 setup_next_usage_insn (regno, insn, reloads_num, false);
4878 else
4879 usage_insns[regno].check = 0;
4882 /* Return first non-debug insn in list USAGE_INSNS. */
4883 static rtx_insn *
4884 skip_usage_debug_insns (rtx usage_insns)
4886 rtx insn;
4888 /* Skip debug insns. */
4889 for (insn = usage_insns;
4890 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4891 insn = XEXP (insn, 1))
4893 return safe_as_a <rtx_insn *> (insn);
4896 /* Return true if we need secondary memory moves for insn in
4897 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4898 into the insn. */
4899 static bool
4900 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4901 rtx usage_insns ATTRIBUTE_UNUSED)
4903 #ifndef SECONDARY_MEMORY_NEEDED
4904 return false;
4905 #else
4906 rtx_insn *insn;
4907 rtx set, dest;
4908 enum reg_class cl;
4910 if (inher_cl == ALL_REGS
4911 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4912 return false;
4913 lra_assert (INSN_P (insn));
4914 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4915 return false;
4916 dest = SET_DEST (set);
4917 if (! REG_P (dest))
4918 return false;
4919 lra_assert (inher_cl != NO_REGS);
4920 cl = get_reg_class (REGNO (dest));
4921 return (cl != NO_REGS && cl != ALL_REGS
4922 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4923 #endif
4926 /* Registers involved in inheritance/split in the current EBB
4927 (inheritance/split pseudos and original registers). */
4928 static bitmap_head check_only_regs;
4930 /* Reload pseudos can not be involded in invariant inheritance in the
4931 current EBB. */
4932 static bitmap_head invalid_invariant_regs;
4934 /* Do inheritance transformations for insn INSN, which defines (if
4935 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4936 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4937 form as the "insns" field of usage_insns. Return true if we
4938 succeed in such transformation.
4940 The transformations look like:
4942 p <- ... i <- ...
4943 ... p <- i (new insn)
4944 ... =>
4945 <- ... p ... <- ... i ...
4947 ... i <- p (new insn)
4948 <- ... p ... <- ... i ...
4949 ... =>
4950 <- ... p ... <- ... i ...
4951 where p is a spilled original pseudo and i is a new inheritance pseudo.
4954 The inheritance pseudo has the smallest class of two classes CL and
4955 class of ORIGINAL REGNO. */
4956 static bool
4957 inherit_reload_reg (bool def_p, int original_regno,
4958 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4960 if (optimize_function_for_size_p (cfun))
4961 return false;
4963 enum reg_class rclass = lra_get_allocno_class (original_regno);
4964 rtx original_reg = regno_reg_rtx[original_regno];
4965 rtx new_reg, usage_insn;
4966 rtx_insn *new_insns;
4968 lra_assert (! usage_insns[original_regno].after_p);
4969 if (lra_dump_file != NULL)
4970 fprintf (lra_dump_file,
4971 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4972 if (! ira_reg_classes_intersect_p[cl][rclass])
4974 if (lra_dump_file != NULL)
4976 fprintf (lra_dump_file,
4977 " Rejecting inheritance for %d "
4978 "because of disjoint classes %s and %s\n",
4979 original_regno, reg_class_names[cl],
4980 reg_class_names[rclass]);
4981 fprintf (lra_dump_file,
4982 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4984 return false;
4986 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4987 /* We don't use a subset of two classes because it can be
4988 NO_REGS. This transformation is still profitable in most
4989 cases even if the classes are not intersected as register
4990 move is probably cheaper than a memory load. */
4991 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4993 if (lra_dump_file != NULL)
4994 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4995 reg_class_names[cl], reg_class_names[rclass]);
4997 rclass = cl;
4999 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
5001 /* Reject inheritance resulting in secondary memory moves.
5002 Otherwise, there is a danger in LRA cycling. Also such
5003 transformation will be unprofitable. */
5004 if (lra_dump_file != NULL)
5006 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
5007 rtx set = single_set (insn);
5009 lra_assert (set != NULL_RTX);
5011 rtx dest = SET_DEST (set);
5013 lra_assert (REG_P (dest));
5014 fprintf (lra_dump_file,
5015 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
5016 "as secondary mem is needed\n",
5017 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
5018 original_regno, reg_class_names[rclass]);
5019 fprintf (lra_dump_file,
5020 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5022 return false;
5024 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
5025 rclass, "inheritance");
5026 start_sequence ();
5027 if (def_p)
5028 lra_emit_move (original_reg, new_reg);
5029 else
5030 lra_emit_move (new_reg, original_reg);
5031 new_insns = get_insns ();
5032 end_sequence ();
5033 if (NEXT_INSN (new_insns) != NULL_RTX)
5035 if (lra_dump_file != NULL)
5037 fprintf (lra_dump_file,
5038 " Rejecting inheritance %d->%d "
5039 "as it results in 2 or more insns:\n",
5040 original_regno, REGNO (new_reg));
5041 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
5042 fprintf (lra_dump_file,
5043 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5045 return false;
5047 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
5048 lra_update_insn_regno_info (insn);
5049 if (! def_p)
5050 /* We now have a new usage insn for original regno. */
5051 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
5052 if (lra_dump_file != NULL)
5053 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
5054 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
5055 lra_reg_info[REGNO (new_reg)].restore_rtx = regno_reg_rtx[original_regno];
5056 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5057 bitmap_set_bit (&check_only_regs, original_regno);
5058 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
5059 if (def_p)
5060 lra_process_new_insns (insn, NULL, new_insns,
5061 "Add original<-inheritance");
5062 else
5063 lra_process_new_insns (insn, new_insns, NULL,
5064 "Add inheritance<-original");
5065 while (next_usage_insns != NULL_RTX)
5067 if (GET_CODE (next_usage_insns) != INSN_LIST)
5069 usage_insn = next_usage_insns;
5070 lra_assert (NONDEBUG_INSN_P (usage_insn));
5071 next_usage_insns = NULL;
5073 else
5075 usage_insn = XEXP (next_usage_insns, 0);
5076 lra_assert (DEBUG_INSN_P (usage_insn));
5077 next_usage_insns = XEXP (next_usage_insns, 1);
5079 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5080 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5081 if (lra_dump_file != NULL)
5083 fprintf (lra_dump_file,
5084 " Inheritance reuse change %d->%d (bb%d):\n",
5085 original_regno, REGNO (new_reg),
5086 BLOCK_FOR_INSN (usage_insn)->index);
5087 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5090 if (lra_dump_file != NULL)
5091 fprintf (lra_dump_file,
5092 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5093 return true;
5096 /* Return true if we need a caller save/restore for pseudo REGNO which
5097 was assigned to a hard register. */
5098 static inline bool
5099 need_for_call_save_p (int regno)
5101 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
5102 return (usage_insns[regno].calls_num < calls_num
5103 && (overlaps_hard_reg_set_p
5104 ((flag_ipa_ra &&
5105 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
5106 ? lra_reg_info[regno].actual_call_used_reg_set
5107 : call_used_reg_set,
5108 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
5109 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
5110 PSEUDO_REGNO_MODE (regno))));
5113 /* Global registers occurring in the current EBB. */
5114 static bitmap_head ebb_global_regs;
5116 /* Return true if we need a split for hard register REGNO or pseudo
5117 REGNO which was assigned to a hard register.
5118 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
5119 used for reloads since the EBB end. It is an approximation of the
5120 used hard registers in the split range. The exact value would
5121 require expensive calculations. If we were aggressive with
5122 splitting because of the approximation, the split pseudo will save
5123 the same hard register assignment and will be removed in the undo
5124 pass. We still need the approximation because too aggressive
5125 splitting would result in too inaccurate cost calculation in the
5126 assignment pass because of too many generated moves which will be
5127 probably removed in the undo pass. */
5128 static inline bool
5129 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
5131 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
5133 lra_assert (hard_regno >= 0);
5134 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
5135 /* Don't split eliminable hard registers, otherwise we can
5136 split hard registers like hard frame pointer, which
5137 lives on BB start/end according to DF-infrastructure,
5138 when there is a pseudo assigned to the register and
5139 living in the same BB. */
5140 && (regno >= FIRST_PSEUDO_REGISTER
5141 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
5142 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
5143 /* Don't split call clobbered hard regs living through
5144 calls, otherwise we might have a check problem in the
5145 assign sub-pass as in the most cases (exception is a
5146 situation when lra_risky_transformations_p value is
5147 true) the assign pass assumes that all pseudos living
5148 through calls are assigned to call saved hard regs. */
5149 && (regno >= FIRST_PSEUDO_REGISTER
5150 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
5151 || usage_insns[regno].calls_num == calls_num)
5152 /* We need at least 2 reloads to make pseudo splitting
5153 profitable. We should provide hard regno splitting in
5154 any case to solve 1st insn scheduling problem when
5155 moving hard register definition up might result in
5156 impossibility to find hard register for reload pseudo of
5157 small register class. */
5158 && (usage_insns[regno].reloads_num
5159 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
5160 && (regno < FIRST_PSEUDO_REGISTER
5161 /* For short living pseudos, spilling + inheritance can
5162 be considered a substitution for splitting.
5163 Therefore we do not splitting for local pseudos. It
5164 decreases also aggressiveness of splitting. The
5165 minimal number of references is chosen taking into
5166 account that for 2 references splitting has no sense
5167 as we can just spill the pseudo. */
5168 || (regno >= FIRST_PSEUDO_REGISTER
5169 && lra_reg_info[regno].nrefs > 3
5170 && bitmap_bit_p (&ebb_global_regs, regno))))
5171 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
5174 /* Return class for the split pseudo created from original pseudo with
5175 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
5176 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
5177 results in no secondary memory movements. */
5178 static enum reg_class
5179 choose_split_class (enum reg_class allocno_class,
5180 int hard_regno ATTRIBUTE_UNUSED,
5181 machine_mode mode ATTRIBUTE_UNUSED)
5183 #ifndef SECONDARY_MEMORY_NEEDED
5184 return allocno_class;
5185 #else
5186 int i;
5187 enum reg_class cl, best_cl = NO_REGS;
5188 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
5189 = REGNO_REG_CLASS (hard_regno);
5191 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
5192 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
5193 return allocno_class;
5194 for (i = 0;
5195 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
5196 i++)
5197 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
5198 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
5199 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
5200 && (best_cl == NO_REGS
5201 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
5202 best_cl = cl;
5203 return best_cl;
5204 #endif
5207 /* Do split transformations for insn INSN, which defines or uses
5208 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
5209 the EBB next uses ORIGINAL_REGNO; it has the same form as the
5210 "insns" field of usage_insns.
5212 The transformations look like:
5214 p <- ... p <- ...
5215 ... s <- p (new insn -- save)
5216 ... =>
5217 ... p <- s (new insn -- restore)
5218 <- ... p ... <- ... p ...
5220 <- ... p ... <- ... p ...
5221 ... s <- p (new insn -- save)
5222 ... =>
5223 ... p <- s (new insn -- restore)
5224 <- ... p ... <- ... p ...
5226 where p is an original pseudo got a hard register or a hard
5227 register and s is a new split pseudo. The save is put before INSN
5228 if BEFORE_P is true. Return true if we succeed in such
5229 transformation. */
5230 static bool
5231 split_reg (bool before_p, int original_regno, rtx_insn *insn,
5232 rtx next_usage_insns)
5234 enum reg_class rclass;
5235 rtx original_reg;
5236 int hard_regno, nregs;
5237 rtx new_reg, usage_insn;
5238 rtx_insn *restore, *save;
5239 bool after_p;
5240 bool call_save_p;
5241 machine_mode mode;
5243 if (original_regno < FIRST_PSEUDO_REGISTER)
5245 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
5246 hard_regno = original_regno;
5247 call_save_p = false;
5248 nregs = 1;
5249 mode = lra_reg_info[hard_regno].biggest_mode;
5250 machine_mode reg_rtx_mode = GET_MODE (regno_reg_rtx[hard_regno]);
5251 /* A reg can have a biggest_mode of VOIDmode if it was only ever seen
5252 as part of a multi-word register. In that case, or if the biggest
5253 mode was larger than a register, just use the reg_rtx. Otherwise,
5254 limit the size to that of the biggest access in the function. */
5255 if (mode == VOIDmode
5256 || GET_MODE_SIZE (mode) > GET_MODE_SIZE (reg_rtx_mode))
5258 original_reg = regno_reg_rtx[hard_regno];
5259 mode = reg_rtx_mode;
5261 else
5262 original_reg = gen_rtx_REG (mode, hard_regno);
5264 else
5266 mode = PSEUDO_REGNO_MODE (original_regno);
5267 hard_regno = reg_renumber[original_regno];
5268 nregs = hard_regno_nregs[hard_regno][mode];
5269 rclass = lra_get_allocno_class (original_regno);
5270 original_reg = regno_reg_rtx[original_regno];
5271 call_save_p = need_for_call_save_p (original_regno);
5273 lra_assert (hard_regno >= 0);
5274 if (lra_dump_file != NULL)
5275 fprintf (lra_dump_file,
5276 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
5278 if (call_save_p)
5280 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
5281 hard_regno_nregs[hard_regno][mode],
5282 mode);
5283 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
5285 else
5287 rclass = choose_split_class (rclass, hard_regno, mode);
5288 if (rclass == NO_REGS)
5290 if (lra_dump_file != NULL)
5292 fprintf (lra_dump_file,
5293 " Rejecting split of %d(%s): "
5294 "no good reg class for %d(%s)\n",
5295 original_regno,
5296 reg_class_names[lra_get_allocno_class (original_regno)],
5297 hard_regno,
5298 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
5299 fprintf
5300 (lra_dump_file,
5301 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5303 return false;
5305 new_reg = lra_create_new_reg (mode, original_reg, rclass, "split");
5306 reg_renumber[REGNO (new_reg)] = hard_regno;
5308 save = emit_spill_move (true, new_reg, original_reg);
5309 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
5311 if (lra_dump_file != NULL)
5313 fprintf
5314 (lra_dump_file,
5315 " Rejecting split %d->%d resulting in > 2 save insns:\n",
5316 original_regno, REGNO (new_reg));
5317 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
5318 fprintf (lra_dump_file,
5319 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5321 return false;
5323 restore = emit_spill_move (false, new_reg, original_reg);
5324 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
5326 if (lra_dump_file != NULL)
5328 fprintf (lra_dump_file,
5329 " Rejecting split %d->%d "
5330 "resulting in > 2 restore insns:\n",
5331 original_regno, REGNO (new_reg));
5332 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
5333 fprintf (lra_dump_file,
5334 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5336 return false;
5338 after_p = usage_insns[original_regno].after_p;
5339 lra_reg_info[REGNO (new_reg)].restore_rtx = regno_reg_rtx[original_regno];
5340 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5341 bitmap_set_bit (&check_only_regs, original_regno);
5342 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
5343 for (;;)
5345 if (GET_CODE (next_usage_insns) != INSN_LIST)
5347 usage_insn = next_usage_insns;
5348 break;
5350 usage_insn = XEXP (next_usage_insns, 0);
5351 lra_assert (DEBUG_INSN_P (usage_insn));
5352 next_usage_insns = XEXP (next_usage_insns, 1);
5353 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5354 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5355 if (lra_dump_file != NULL)
5357 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5358 original_regno, REGNO (new_reg));
5359 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5362 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5363 lra_assert (usage_insn != insn || (after_p && before_p));
5364 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5365 after_p ? NULL : restore,
5366 after_p ? restore : NULL,
5367 call_save_p
5368 ? "Add reg<-save" : "Add reg<-split");
5369 lra_process_new_insns (insn, before_p ? save : NULL,
5370 before_p ? NULL : save,
5371 call_save_p
5372 ? "Add save<-reg" : "Add split<-reg");
5373 if (nregs > 1)
5374 /* If we are trying to split multi-register. We should check
5375 conflicts on the next assignment sub-pass. IRA can allocate on
5376 sub-register levels, LRA do this on pseudos level right now and
5377 this discrepancy may create allocation conflicts after
5378 splitting. */
5379 lra_risky_transformations_p = true;
5380 if (lra_dump_file != NULL)
5381 fprintf (lra_dump_file,
5382 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5383 return true;
5386 /* Recognize that we need a split transformation for insn INSN, which
5387 defines or uses REGNO in its insn biggest MODE (we use it only if
5388 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5389 hard registers which might be used for reloads since the EBB end.
5390 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5391 uid before starting INSN processing. Return true if we succeed in
5392 such transformation. */
5393 static bool
5394 split_if_necessary (int regno, machine_mode mode,
5395 HARD_REG_SET potential_reload_hard_regs,
5396 bool before_p, rtx_insn *insn, int max_uid)
5398 bool res = false;
5399 int i, nregs = 1;
5400 rtx next_usage_insns;
5402 if (regno < FIRST_PSEUDO_REGISTER)
5403 nregs = hard_regno_nregs[regno][mode];
5404 for (i = 0; i < nregs; i++)
5405 if (usage_insns[regno + i].check == curr_usage_insns_check
5406 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5407 /* To avoid processing the register twice or more. */
5408 && ((GET_CODE (next_usage_insns) != INSN_LIST
5409 && INSN_UID (next_usage_insns) < max_uid)
5410 || (GET_CODE (next_usage_insns) == INSN_LIST
5411 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5412 && need_for_split_p (potential_reload_hard_regs, regno + i)
5413 && split_reg (before_p, regno + i, insn, next_usage_insns))
5414 res = true;
5415 return res;
5418 /* Return TRUE if rtx X is considered as an invariant for
5419 inheritance. */
5420 static bool
5421 invariant_p (const_rtx x)
5423 machine_mode mode;
5424 const char *fmt;
5425 enum rtx_code code;
5426 int i, j;
5428 code = GET_CODE (x);
5429 mode = GET_MODE (x);
5430 if (code == SUBREG)
5432 x = SUBREG_REG (x);
5433 code = GET_CODE (x);
5434 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
5435 mode = GET_MODE (x);
5438 if (MEM_P (x))
5439 return false;
5441 if (REG_P (x))
5443 int i, nregs, regno = REGNO (x);
5445 if (regno >= FIRST_PSEUDO_REGISTER || regno == STACK_POINTER_REGNUM
5446 || TEST_HARD_REG_BIT (eliminable_regset, regno)
5447 || GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
5448 return false;
5449 nregs = hard_regno_nregs[regno][mode];
5450 for (i = 0; i < nregs; i++)
5451 if (! fixed_regs[regno + i]
5452 /* A hard register may be clobbered in the current insn
5453 but we can ignore this case because if the hard
5454 register is used it should be set somewhere after the
5455 clobber. */
5456 || bitmap_bit_p (&invalid_invariant_regs, regno + i))
5457 return false;
5459 fmt = GET_RTX_FORMAT (code);
5460 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5462 if (fmt[i] == 'e')
5464 if (! invariant_p (XEXP (x, i)))
5465 return false;
5467 else if (fmt[i] == 'E')
5469 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5470 if (! invariant_p (XVECEXP (x, i, j)))
5471 return false;
5474 return true;
5477 /* We have 'dest_reg <- invariant'. Let us try to make an invariant
5478 inheritance transformation (using dest_reg instead invariant in a
5479 subsequent insn). */
5480 static bool
5481 process_invariant_for_inheritance (rtx dst_reg, rtx invariant_rtx)
5483 invariant_ptr_t invariant_ptr;
5484 rtx_insn *insn, *new_insns;
5485 rtx insn_set, insn_reg, new_reg;
5486 int insn_regno;
5487 bool succ_p = false;
5488 int dst_regno = REGNO (dst_reg);
5489 enum machine_mode dst_mode = GET_MODE (dst_reg);
5490 enum reg_class cl = lra_get_allocno_class (dst_regno), insn_reg_cl;
5492 invariant_ptr = insert_invariant (invariant_rtx);
5493 if ((insn = invariant_ptr->insn) != NULL_RTX)
5495 /* We have a subsequent insn using the invariant. */
5496 insn_set = single_set (insn);
5497 lra_assert (insn_set != NULL);
5498 insn_reg = SET_DEST (insn_set);
5499 lra_assert (REG_P (insn_reg));
5500 insn_regno = REGNO (insn_reg);
5501 insn_reg_cl = lra_get_allocno_class (insn_regno);
5503 if (dst_mode == GET_MODE (insn_reg)
5504 /* We should consider only result move reg insns which are
5505 cheap. */
5506 && targetm.register_move_cost (dst_mode, cl, insn_reg_cl) == 2
5507 && targetm.register_move_cost (dst_mode, cl, cl) == 2)
5509 if (lra_dump_file != NULL)
5510 fprintf (lra_dump_file,
5511 " [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[\n");
5512 new_reg = lra_create_new_reg (dst_mode, dst_reg,
5513 cl, "invariant inheritance");
5514 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
5515 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5516 lra_reg_info[REGNO (new_reg)].restore_rtx = PATTERN (insn);
5517 start_sequence ();
5518 lra_emit_move (new_reg, dst_reg);
5519 new_insns = get_insns ();
5520 end_sequence ();
5521 lra_process_new_insns (curr_insn, NULL, new_insns,
5522 "Add invariant inheritance<-original");
5523 start_sequence ();
5524 lra_emit_move (SET_DEST (insn_set), new_reg);
5525 new_insns = get_insns ();
5526 end_sequence ();
5527 lra_process_new_insns (insn, NULL, new_insns,
5528 "Changing reload<-inheritance");
5529 lra_set_insn_deleted (insn);
5530 succ_p = true;
5531 if (lra_dump_file != NULL)
5533 fprintf (lra_dump_file,
5534 " Invariant inheritance reuse change %d (bb%d):\n",
5535 REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
5536 dump_insn_slim (lra_dump_file, insn);
5537 fprintf (lra_dump_file,
5538 " ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]\n");
5542 invariant_ptr->insn = curr_insn;
5543 return succ_p;
5546 /* Check only registers living at the current program point in the
5547 current EBB. */
5548 static bitmap_head live_regs;
5550 /* Update live info in EBB given by its HEAD and TAIL insns after
5551 inheritance/split transformation. The function removes dead moves
5552 too. */
5553 static void
5554 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5556 unsigned int j;
5557 int i, regno;
5558 bool live_p;
5559 rtx_insn *prev_insn;
5560 rtx set;
5561 bool remove_p;
5562 basic_block last_bb, prev_bb, curr_bb;
5563 bitmap_iterator bi;
5564 struct lra_insn_reg *reg;
5565 edge e;
5566 edge_iterator ei;
5568 last_bb = BLOCK_FOR_INSN (tail);
5569 prev_bb = NULL;
5570 for (curr_insn = tail;
5571 curr_insn != PREV_INSN (head);
5572 curr_insn = prev_insn)
5574 prev_insn = PREV_INSN (curr_insn);
5575 /* We need to process empty blocks too. They contain
5576 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5577 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5578 continue;
5579 curr_bb = BLOCK_FOR_INSN (curr_insn);
5580 if (curr_bb != prev_bb)
5582 if (prev_bb != NULL)
5584 /* Update df_get_live_in (prev_bb): */
5585 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5586 if (bitmap_bit_p (&live_regs, j))
5587 bitmap_set_bit (df_get_live_in (prev_bb), j);
5588 else
5589 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5591 if (curr_bb != last_bb)
5593 /* Update df_get_live_out (curr_bb): */
5594 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5596 live_p = bitmap_bit_p (&live_regs, j);
5597 if (! live_p)
5598 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5599 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5601 live_p = true;
5602 break;
5604 if (live_p)
5605 bitmap_set_bit (df_get_live_out (curr_bb), j);
5606 else
5607 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5610 prev_bb = curr_bb;
5611 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5613 if (! NONDEBUG_INSN_P (curr_insn))
5614 continue;
5615 curr_id = lra_get_insn_recog_data (curr_insn);
5616 curr_static_id = curr_id->insn_static_data;
5617 remove_p = false;
5618 if ((set = single_set (curr_insn)) != NULL_RTX
5619 && REG_P (SET_DEST (set))
5620 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5621 && SET_DEST (set) != pic_offset_table_rtx
5622 && bitmap_bit_p (&check_only_regs, regno)
5623 && ! bitmap_bit_p (&live_regs, regno))
5624 remove_p = true;
5625 /* See which defined values die here. */
5626 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5627 if (reg->type == OP_OUT && ! reg->subreg_p)
5628 bitmap_clear_bit (&live_regs, reg->regno);
5629 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5630 if (reg->type == OP_OUT && ! reg->subreg_p)
5631 bitmap_clear_bit (&live_regs, reg->regno);
5632 if (curr_id->arg_hard_regs != NULL)
5633 /* Make clobbered argument hard registers die. */
5634 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5635 if (regno >= FIRST_PSEUDO_REGISTER)
5636 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
5637 /* Mark each used value as live. */
5638 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5639 if (reg->type != OP_OUT
5640 && bitmap_bit_p (&check_only_regs, reg->regno))
5641 bitmap_set_bit (&live_regs, reg->regno);
5642 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5643 if (reg->type != OP_OUT
5644 && bitmap_bit_p (&check_only_regs, reg->regno))
5645 bitmap_set_bit (&live_regs, reg->regno);
5646 if (curr_id->arg_hard_regs != NULL)
5647 /* Make used argument hard registers live. */
5648 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5649 if (regno < FIRST_PSEUDO_REGISTER
5650 && bitmap_bit_p (&check_only_regs, regno))
5651 bitmap_set_bit (&live_regs, regno);
5652 /* It is quite important to remove dead move insns because it
5653 means removing dead store. We don't need to process them for
5654 constraints. */
5655 if (remove_p)
5657 if (lra_dump_file != NULL)
5659 fprintf (lra_dump_file, " Removing dead insn:\n ");
5660 dump_insn_slim (lra_dump_file, curr_insn);
5662 lra_set_insn_deleted (curr_insn);
5667 /* The structure describes info to do an inheritance for the current
5668 insn. We need to collect such info first before doing the
5669 transformations because the transformations change the insn
5670 internal representation. */
5671 struct to_inherit
5673 /* Original regno. */
5674 int regno;
5675 /* Subsequent insns which can inherit original reg value. */
5676 rtx insns;
5679 /* Array containing all info for doing inheritance from the current
5680 insn. */
5681 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5683 /* Number elements in the previous array. */
5684 static int to_inherit_num;
5686 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5687 structure to_inherit. */
5688 static void
5689 add_to_inherit (int regno, rtx insns)
5691 int i;
5693 for (i = 0; i < to_inherit_num; i++)
5694 if (to_inherit[i].regno == regno)
5695 return;
5696 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5697 to_inherit[to_inherit_num].regno = regno;
5698 to_inherit[to_inherit_num++].insns = insns;
5701 /* Return the last non-debug insn in basic block BB, or the block begin
5702 note if none. */
5703 static rtx_insn *
5704 get_last_insertion_point (basic_block bb)
5706 rtx_insn *insn;
5708 FOR_BB_INSNS_REVERSE (bb, insn)
5709 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5710 return insn;
5711 gcc_unreachable ();
5714 /* Set up RES by registers living on edges FROM except the edge (FROM,
5715 TO) or by registers set up in a jump insn in BB FROM. */
5716 static void
5717 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5719 rtx_insn *last;
5720 struct lra_insn_reg *reg;
5721 edge e;
5722 edge_iterator ei;
5724 lra_assert (to != NULL);
5725 bitmap_clear (res);
5726 FOR_EACH_EDGE (e, ei, from->succs)
5727 if (e->dest != to)
5728 bitmap_ior_into (res, df_get_live_in (e->dest));
5729 last = get_last_insertion_point (from);
5730 if (! JUMP_P (last))
5731 return;
5732 curr_id = lra_get_insn_recog_data (last);
5733 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5734 if (reg->type != OP_IN)
5735 bitmap_set_bit (res, reg->regno);
5738 /* Used as a temporary results of some bitmap calculations. */
5739 static bitmap_head temp_bitmap;
5741 /* We split for reloads of small class of hard regs. The following
5742 defines how many hard regs the class should have to be qualified as
5743 small. The code is mostly oriented to x86/x86-64 architecture
5744 where some insns need to use only specific register or pair of
5745 registers and these register can live in RTL explicitly, e.g. for
5746 parameter passing. */
5747 static const int max_small_class_regs_num = 2;
5749 /* Do inheritance/split transformations in EBB starting with HEAD and
5750 finishing on TAIL. We process EBB insns in the reverse order.
5751 Return true if we did any inheritance/split transformation in the
5752 EBB.
5754 We should avoid excessive splitting which results in worse code
5755 because of inaccurate cost calculations for spilling new split
5756 pseudos in such case. To achieve this we do splitting only if
5757 register pressure is high in given basic block and there are reload
5758 pseudos requiring hard registers. We could do more register
5759 pressure calculations at any given program point to avoid necessary
5760 splitting even more but it is to expensive and the current approach
5761 works well enough. */
5762 static bool
5763 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5765 int i, src_regno, dst_regno, nregs;
5766 bool change_p, succ_p, update_reloads_num_p;
5767 rtx_insn *prev_insn, *last_insn;
5768 rtx next_usage_insns, curr_set;
5769 enum reg_class cl;
5770 struct lra_insn_reg *reg;
5771 basic_block last_processed_bb, curr_bb = NULL;
5772 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5773 bitmap to_process;
5774 unsigned int j;
5775 bitmap_iterator bi;
5776 bool head_p, after_p;
5778 change_p = false;
5779 curr_usage_insns_check++;
5780 clear_invariants ();
5781 reloads_num = calls_num = 0;
5782 bitmap_clear (&check_only_regs);
5783 bitmap_clear (&invalid_invariant_regs);
5784 last_processed_bb = NULL;
5785 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5786 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5787 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5788 /* We don't process new insns generated in the loop. */
5789 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5791 prev_insn = PREV_INSN (curr_insn);
5792 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5793 curr_bb = BLOCK_FOR_INSN (curr_insn);
5794 if (last_processed_bb != curr_bb)
5796 /* We are at the end of BB. Add qualified living
5797 pseudos for potential splitting. */
5798 to_process = df_get_live_out (curr_bb);
5799 if (last_processed_bb != NULL)
5801 /* We are somewhere in the middle of EBB. */
5802 get_live_on_other_edges (curr_bb, last_processed_bb,
5803 &temp_bitmap);
5804 to_process = &temp_bitmap;
5806 last_processed_bb = curr_bb;
5807 last_insn = get_last_insertion_point (curr_bb);
5808 after_p = (! JUMP_P (last_insn)
5809 && (! CALL_P (last_insn)
5810 || (find_reg_note (last_insn,
5811 REG_NORETURN, NULL_RTX) == NULL_RTX
5812 && ! SIBLING_CALL_P (last_insn))));
5813 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5814 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5816 if ((int) j >= lra_constraint_new_regno_start)
5817 break;
5818 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5820 if (j < FIRST_PSEUDO_REGISTER)
5821 SET_HARD_REG_BIT (live_hard_regs, j);
5822 else
5823 add_to_hard_reg_set (&live_hard_regs,
5824 PSEUDO_REGNO_MODE (j),
5825 reg_renumber[j]);
5826 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5830 src_regno = dst_regno = -1;
5831 curr_set = single_set (curr_insn);
5832 if (curr_set != NULL_RTX && REG_P (SET_DEST (curr_set)))
5833 dst_regno = REGNO (SET_DEST (curr_set));
5834 if (curr_set != NULL_RTX && REG_P (SET_SRC (curr_set)))
5835 src_regno = REGNO (SET_SRC (curr_set));
5836 update_reloads_num_p = true;
5837 if (src_regno < lra_constraint_new_regno_start
5838 && src_regno >= FIRST_PSEUDO_REGISTER
5839 && reg_renumber[src_regno] < 0
5840 && dst_regno >= lra_constraint_new_regno_start
5841 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5843 /* 'reload_pseudo <- original_pseudo'. */
5844 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5845 reloads_num++;
5846 update_reloads_num_p = false;
5847 succ_p = false;
5848 if (usage_insns[src_regno].check == curr_usage_insns_check
5849 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5850 succ_p = inherit_reload_reg (false, src_regno, cl,
5851 curr_insn, next_usage_insns);
5852 if (succ_p)
5853 change_p = true;
5854 else
5855 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5856 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5857 IOR_HARD_REG_SET (potential_reload_hard_regs,
5858 reg_class_contents[cl]);
5860 else if (src_regno < 0
5861 && dst_regno >= lra_constraint_new_regno_start
5862 && invariant_p (SET_SRC (curr_set))
5863 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS
5864 && ! bitmap_bit_p (&invalid_invariant_regs, dst_regno))
5866 /* 'reload_pseudo <- invariant'. */
5867 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5868 reloads_num++;
5869 update_reloads_num_p = false;
5870 if (process_invariant_for_inheritance (SET_DEST (curr_set), SET_SRC (curr_set)))
5871 change_p = true;
5872 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5873 IOR_HARD_REG_SET (potential_reload_hard_regs,
5874 reg_class_contents[cl]);
5876 else if (src_regno >= lra_constraint_new_regno_start
5877 && dst_regno < lra_constraint_new_regno_start
5878 && dst_regno >= FIRST_PSEUDO_REGISTER
5879 && reg_renumber[dst_regno] < 0
5880 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5881 && usage_insns[dst_regno].check == curr_usage_insns_check
5882 && (next_usage_insns
5883 = usage_insns[dst_regno].insns) != NULL_RTX)
5885 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5886 reloads_num++;
5887 update_reloads_num_p = false;
5888 /* 'original_pseudo <- reload_pseudo'. */
5889 if (! JUMP_P (curr_insn)
5890 && inherit_reload_reg (true, dst_regno, cl,
5891 curr_insn, next_usage_insns))
5892 change_p = true;
5893 /* Invalidate. */
5894 usage_insns[dst_regno].check = 0;
5895 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5896 IOR_HARD_REG_SET (potential_reload_hard_regs,
5897 reg_class_contents[cl]);
5899 else if (INSN_P (curr_insn))
5901 int iter;
5902 int max_uid = get_max_uid ();
5904 curr_id = lra_get_insn_recog_data (curr_insn);
5905 curr_static_id = curr_id->insn_static_data;
5906 to_inherit_num = 0;
5907 /* Process insn definitions. */
5908 for (iter = 0; iter < 2; iter++)
5909 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5910 reg != NULL;
5911 reg = reg->next)
5912 if (reg->type != OP_IN
5913 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5915 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5916 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5917 && usage_insns[dst_regno].check == curr_usage_insns_check
5918 && (next_usage_insns
5919 = usage_insns[dst_regno].insns) != NULL_RTX)
5921 struct lra_insn_reg *r;
5923 for (r = curr_id->regs; r != NULL; r = r->next)
5924 if (r->type != OP_OUT && r->regno == dst_regno)
5925 break;
5926 /* Don't do inheritance if the pseudo is also
5927 used in the insn. */
5928 if (r == NULL)
5929 /* We can not do inheritance right now
5930 because the current insn reg info (chain
5931 regs) can change after that. */
5932 add_to_inherit (dst_regno, next_usage_insns);
5934 /* We can not process one reg twice here because of
5935 usage_insns invalidation. */
5936 if ((dst_regno < FIRST_PSEUDO_REGISTER
5937 || reg_renumber[dst_regno] >= 0)
5938 && ! reg->subreg_p && reg->type != OP_IN)
5940 HARD_REG_SET s;
5942 if (split_if_necessary (dst_regno, reg->biggest_mode,
5943 potential_reload_hard_regs,
5944 false, curr_insn, max_uid))
5945 change_p = true;
5946 CLEAR_HARD_REG_SET (s);
5947 if (dst_regno < FIRST_PSEUDO_REGISTER)
5948 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5949 else
5950 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5951 reg_renumber[dst_regno]);
5952 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5954 /* We should invalidate potential inheritance or
5955 splitting for the current insn usages to the next
5956 usage insns (see code below) as the output pseudo
5957 prevents this. */
5958 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5959 && reg_renumber[dst_regno] < 0)
5960 || (reg->type == OP_OUT && ! reg->subreg_p
5961 && (dst_regno < FIRST_PSEUDO_REGISTER
5962 || reg_renumber[dst_regno] >= 0)))
5964 /* Invalidate and mark definitions. */
5965 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5966 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5967 else
5969 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5970 for (i = 0; i < nregs; i++)
5971 usage_insns[dst_regno + i].check
5972 = -(int) INSN_UID (curr_insn);
5976 /* Process clobbered call regs. */
5977 if (curr_id->arg_hard_regs != NULL)
5978 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5979 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5980 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
5981 = -(int) INSN_UID (curr_insn);
5982 if (! JUMP_P (curr_insn))
5983 for (i = 0; i < to_inherit_num; i++)
5984 if (inherit_reload_reg (true, to_inherit[i].regno,
5985 ALL_REGS, curr_insn,
5986 to_inherit[i].insns))
5987 change_p = true;
5988 if (CALL_P (curr_insn))
5990 rtx cheap, pat, dest;
5991 rtx_insn *restore;
5992 int regno, hard_regno;
5994 calls_num++;
5995 if ((cheap = find_reg_note (curr_insn,
5996 REG_RETURNED, NULL_RTX)) != NULL_RTX
5997 && ((cheap = XEXP (cheap, 0)), true)
5998 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5999 && (hard_regno = reg_renumber[regno]) >= 0
6000 /* If there are pending saves/restores, the
6001 optimization is not worth. */
6002 && usage_insns[regno].calls_num == calls_num - 1
6003 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
6005 /* Restore the pseudo from the call result as
6006 REG_RETURNED note says that the pseudo value is
6007 in the call result and the pseudo is an argument
6008 of the call. */
6009 pat = PATTERN (curr_insn);
6010 if (GET_CODE (pat) == PARALLEL)
6011 pat = XVECEXP (pat, 0, 0);
6012 dest = SET_DEST (pat);
6013 /* For multiple return values dest is PARALLEL.
6014 Currently we handle only single return value case. */
6015 if (REG_P (dest))
6017 start_sequence ();
6018 emit_move_insn (cheap, copy_rtx (dest));
6019 restore = get_insns ();
6020 end_sequence ();
6021 lra_process_new_insns (curr_insn, NULL, restore,
6022 "Inserting call parameter restore");
6023 /* We don't need to save/restore of the pseudo from
6024 this call. */
6025 usage_insns[regno].calls_num = calls_num;
6026 bitmap_set_bit (&check_only_regs, regno);
6030 to_inherit_num = 0;
6031 /* Process insn usages. */
6032 for (iter = 0; iter < 2; iter++)
6033 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6034 reg != NULL;
6035 reg = reg->next)
6036 if ((reg->type != OP_OUT
6037 || (reg->type == OP_OUT && reg->subreg_p))
6038 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
6040 if (src_regno >= FIRST_PSEUDO_REGISTER
6041 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
6043 if (usage_insns[src_regno].check == curr_usage_insns_check
6044 && (next_usage_insns
6045 = usage_insns[src_regno].insns) != NULL_RTX
6046 && NONDEBUG_INSN_P (curr_insn))
6047 add_to_inherit (src_regno, next_usage_insns);
6048 else if (usage_insns[src_regno].check
6049 != -(int) INSN_UID (curr_insn))
6050 /* Add usages but only if the reg is not set up
6051 in the same insn. */
6052 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6054 else if (src_regno < FIRST_PSEUDO_REGISTER
6055 || reg_renumber[src_regno] >= 0)
6057 bool before_p;
6058 rtx_insn *use_insn = curr_insn;
6060 before_p = (JUMP_P (curr_insn)
6061 || (CALL_P (curr_insn) && reg->type == OP_IN));
6062 if (NONDEBUG_INSN_P (curr_insn)
6063 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
6064 && split_if_necessary (src_regno, reg->biggest_mode,
6065 potential_reload_hard_regs,
6066 before_p, curr_insn, max_uid))
6068 if (reg->subreg_p)
6069 lra_risky_transformations_p = true;
6070 change_p = true;
6071 /* Invalidate. */
6072 usage_insns[src_regno].check = 0;
6073 if (before_p)
6074 use_insn = PREV_INSN (curr_insn);
6076 if (NONDEBUG_INSN_P (curr_insn))
6078 if (src_regno < FIRST_PSEUDO_REGISTER)
6079 add_to_hard_reg_set (&live_hard_regs,
6080 reg->biggest_mode, src_regno);
6081 else
6082 add_to_hard_reg_set (&live_hard_regs,
6083 PSEUDO_REGNO_MODE (src_regno),
6084 reg_renumber[src_regno]);
6086 add_next_usage_insn (src_regno, use_insn, reloads_num);
6089 /* Process used call regs. */
6090 if (curr_id->arg_hard_regs != NULL)
6091 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6092 if (src_regno < FIRST_PSEUDO_REGISTER)
6094 SET_HARD_REG_BIT (live_hard_regs, src_regno);
6095 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6097 for (i = 0; i < to_inherit_num; i++)
6099 src_regno = to_inherit[i].regno;
6100 if (inherit_reload_reg (false, src_regno, ALL_REGS,
6101 curr_insn, to_inherit[i].insns))
6102 change_p = true;
6103 else
6104 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6107 if (update_reloads_num_p
6108 && NONDEBUG_INSN_P (curr_insn) && curr_set != NULL_RTX)
6110 int regno = -1;
6111 if ((REG_P (SET_DEST (curr_set))
6112 && (regno = REGNO (SET_DEST (curr_set))) >= lra_constraint_new_regno_start
6113 && reg_renumber[regno] < 0
6114 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
6115 || (REG_P (SET_SRC (curr_set))
6116 && (regno = REGNO (SET_SRC (curr_set))) >= lra_constraint_new_regno_start
6117 && reg_renumber[regno] < 0
6118 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
6120 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6121 reloads_num++;
6122 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6123 IOR_HARD_REG_SET (potential_reload_hard_regs,
6124 reg_class_contents[cl]);
6127 if (NONDEBUG_INSN_P (curr_insn))
6129 int regno;
6131 /* Invalidate invariants with changed regs. */
6132 curr_id = lra_get_insn_recog_data (curr_insn);
6133 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6134 if (reg->type != OP_IN)
6135 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6136 curr_static_id = curr_id->insn_static_data;
6137 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6138 if (reg->type != OP_IN)
6139 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6140 if (curr_id->arg_hard_regs != NULL)
6141 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6142 bitmap_set_bit (&invalid_invariant_regs,
6143 regno >= FIRST_PSEUDO_REGISTER
6144 ? regno : regno - FIRST_PSEUDO_REGISTER);
6146 /* We reached the start of the current basic block. */
6147 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
6148 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
6150 /* We reached the beginning of the current block -- do
6151 rest of spliting in the current BB. */
6152 to_process = df_get_live_in (curr_bb);
6153 if (BLOCK_FOR_INSN (head) != curr_bb)
6155 /* We are somewhere in the middle of EBB. */
6156 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
6157 curr_bb, &temp_bitmap);
6158 to_process = &temp_bitmap;
6160 head_p = true;
6161 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
6163 if ((int) j >= lra_constraint_new_regno_start)
6164 break;
6165 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
6166 && usage_insns[j].check == curr_usage_insns_check
6167 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
6169 if (need_for_split_p (potential_reload_hard_regs, j))
6171 if (lra_dump_file != NULL && head_p)
6173 fprintf (lra_dump_file,
6174 " ----------------------------------\n");
6175 head_p = false;
6177 if (split_reg (false, j, bb_note (curr_bb),
6178 next_usage_insns))
6179 change_p = true;
6181 usage_insns[j].check = 0;
6186 return change_p;
6189 /* This value affects EBB forming. If probability of edge from EBB to
6190 a BB is not greater than the following value, we don't add the BB
6191 to EBB. */
6192 #define EBB_PROBABILITY_CUTOFF \
6193 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
6195 /* Current number of inheritance/split iteration. */
6196 int lra_inheritance_iter;
6198 /* Entry function for inheritance/split pass. */
6199 void
6200 lra_inheritance (void)
6202 int i;
6203 basic_block bb, start_bb;
6204 edge e;
6206 lra_inheritance_iter++;
6207 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6208 return;
6209 timevar_push (TV_LRA_INHERITANCE);
6210 if (lra_dump_file != NULL)
6211 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
6212 lra_inheritance_iter);
6213 curr_usage_insns_check = 0;
6214 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
6215 for (i = 0; i < lra_constraint_new_regno_start; i++)
6216 usage_insns[i].check = 0;
6217 bitmap_initialize (&check_only_regs, &reg_obstack);
6218 bitmap_initialize (&invalid_invariant_regs, &reg_obstack);
6219 bitmap_initialize (&live_regs, &reg_obstack);
6220 bitmap_initialize (&temp_bitmap, &reg_obstack);
6221 bitmap_initialize (&ebb_global_regs, &reg_obstack);
6222 FOR_EACH_BB_FN (bb, cfun)
6224 start_bb = bb;
6225 if (lra_dump_file != NULL)
6226 fprintf (lra_dump_file, "EBB");
6227 /* Form a EBB starting with BB. */
6228 bitmap_clear (&ebb_global_regs);
6229 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
6230 for (;;)
6232 if (lra_dump_file != NULL)
6233 fprintf (lra_dump_file, " %d", bb->index);
6234 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
6235 || LABEL_P (BB_HEAD (bb->next_bb)))
6236 break;
6237 e = find_fallthru_edge (bb->succs);
6238 if (! e)
6239 break;
6240 if (e->probability < EBB_PROBABILITY_CUTOFF)
6241 break;
6242 bb = bb->next_bb;
6244 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
6245 if (lra_dump_file != NULL)
6246 fprintf (lra_dump_file, "\n");
6247 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
6248 /* Remember that the EBB head and tail can change in
6249 inherit_in_ebb. */
6250 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
6252 bitmap_clear (&ebb_global_regs);
6253 bitmap_clear (&temp_bitmap);
6254 bitmap_clear (&live_regs);
6255 bitmap_clear (&invalid_invariant_regs);
6256 bitmap_clear (&check_only_regs);
6257 free (usage_insns);
6259 timevar_pop (TV_LRA_INHERITANCE);
6264 /* This page contains code to undo failed inheritance/split
6265 transformations. */
6267 /* Current number of iteration undoing inheritance/split. */
6268 int lra_undo_inheritance_iter;
6270 /* Fix BB live info LIVE after removing pseudos created on pass doing
6271 inheritance/split which are REMOVED_PSEUDOS. */
6272 static void
6273 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
6275 unsigned int regno;
6276 bitmap_iterator bi;
6278 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
6279 if (bitmap_clear_bit (live, regno)
6280 && REG_P (lra_reg_info[regno].restore_rtx))
6281 bitmap_set_bit (live, REGNO (lra_reg_info[regno].restore_rtx));
6284 /* Return regno of the (subreg of) REG. Otherwise, return a negative
6285 number. */
6286 static int
6287 get_regno (rtx reg)
6289 if (GET_CODE (reg) == SUBREG)
6290 reg = SUBREG_REG (reg);
6291 if (REG_P (reg))
6292 return REGNO (reg);
6293 return -1;
6296 /* Delete a move INSN with destination reg DREGNO and a previous
6297 clobber insn with the same regno. The inheritance/split code can
6298 generate moves with preceding clobber and when we delete such moves
6299 we should delete the clobber insn too to keep the correct life
6300 info. */
6301 static void
6302 delete_move_and_clobber (rtx_insn *insn, int dregno)
6304 rtx_insn *prev_insn = PREV_INSN (insn);
6306 lra_set_insn_deleted (insn);
6307 lra_assert (dregno >= 0);
6308 if (prev_insn != NULL && NONDEBUG_INSN_P (prev_insn)
6309 && GET_CODE (PATTERN (prev_insn)) == CLOBBER
6310 && dregno == get_regno (XEXP (PATTERN (prev_insn), 0)))
6311 lra_set_insn_deleted (prev_insn);
6314 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
6315 return true if we did any change. The undo transformations for
6316 inheritance looks like
6317 i <- i2
6318 p <- i => p <- i2
6319 or removing
6320 p <- i, i <- p, and i <- i3
6321 where p is original pseudo from which inheritance pseudo i was
6322 created, i and i3 are removed inheritance pseudos, i2 is another
6323 not removed inheritance pseudo. All split pseudos or other
6324 occurrences of removed inheritance pseudos are changed on the
6325 corresponding original pseudos.
6327 The function also schedules insns changed and created during
6328 inheritance/split pass for processing by the subsequent constraint
6329 pass. */
6330 static bool
6331 remove_inheritance_pseudos (bitmap remove_pseudos)
6333 basic_block bb;
6334 int regno, sregno, prev_sregno, dregno;
6335 rtx restore_rtx;
6336 rtx set, prev_set;
6337 rtx_insn *prev_insn;
6338 bool change_p, done_p;
6340 change_p = ! bitmap_empty_p (remove_pseudos);
6341 /* We can not finish the function right away if CHANGE_P is true
6342 because we need to marks insns affected by previous
6343 inheritance/split pass for processing by the subsequent
6344 constraint pass. */
6345 FOR_EACH_BB_FN (bb, cfun)
6347 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
6348 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
6349 FOR_BB_INSNS_REVERSE (bb, curr_insn)
6351 if (! INSN_P (curr_insn))
6352 continue;
6353 done_p = false;
6354 sregno = dregno = -1;
6355 if (change_p && NONDEBUG_INSN_P (curr_insn)
6356 && (set = single_set (curr_insn)) != NULL_RTX)
6358 dregno = get_regno (SET_DEST (set));
6359 sregno = get_regno (SET_SRC (set));
6362 if (sregno >= 0 && dregno >= 0)
6364 if (bitmap_bit_p (remove_pseudos, dregno)
6365 && ! REG_P (lra_reg_info[dregno].restore_rtx))
6367 /* invariant inheritance pseudo <- original pseudo */
6368 if (lra_dump_file != NULL)
6370 fprintf (lra_dump_file, " Removing invariant inheritance:\n");
6371 dump_insn_slim (lra_dump_file, curr_insn);
6372 fprintf (lra_dump_file, "\n");
6374 delete_move_and_clobber (curr_insn, dregno);
6375 done_p = true;
6377 else if (bitmap_bit_p (remove_pseudos, sregno)
6378 && ! REG_P (lra_reg_info[sregno].restore_rtx))
6380 /* reload pseudo <- invariant inheritance pseudo */
6381 start_sequence ();
6382 /* We can not just change the source. It might be
6383 an insn different from the move. */
6384 emit_insn (lra_reg_info[sregno].restore_rtx);
6385 rtx_insn *new_insns = get_insns ();
6386 end_sequence ();
6387 lra_assert (single_set (new_insns) != NULL
6388 && SET_DEST (set) == SET_DEST (single_set (new_insns)));
6389 lra_process_new_insns (curr_insn, NULL, new_insns,
6390 "Changing reload<-invariant inheritance");
6391 delete_move_and_clobber (curr_insn, dregno);
6392 done_p = true;
6394 else if ((bitmap_bit_p (remove_pseudos, sregno)
6395 && (get_regno (lra_reg_info[sregno].restore_rtx) == dregno
6396 || (bitmap_bit_p (remove_pseudos, dregno)
6397 && get_regno (lra_reg_info[sregno].restore_rtx) >= 0
6398 && (get_regno (lra_reg_info[sregno].restore_rtx)
6399 == get_regno (lra_reg_info[dregno].restore_rtx)))))
6400 || (bitmap_bit_p (remove_pseudos, dregno)
6401 && get_regno (lra_reg_info[dregno].restore_rtx) == sregno))
6402 /* One of the following cases:
6403 original <- removed inheritance pseudo
6404 removed inherit pseudo <- another removed inherit pseudo
6405 removed inherit pseudo <- original pseudo
6407 removed_split_pseudo <- original_reg
6408 original_reg <- removed_split_pseudo */
6410 if (lra_dump_file != NULL)
6412 fprintf (lra_dump_file, " Removing %s:\n",
6413 bitmap_bit_p (&lra_split_regs, sregno)
6414 || bitmap_bit_p (&lra_split_regs, dregno)
6415 ? "split" : "inheritance");
6416 dump_insn_slim (lra_dump_file, curr_insn);
6418 delete_move_and_clobber (curr_insn, dregno);
6419 done_p = true;
6421 else if (bitmap_bit_p (remove_pseudos, sregno)
6422 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
6424 /* Search the following pattern:
6425 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
6426 original_pseudo <- inherit_or_split_pseudo1
6427 where the 2nd insn is the current insn and
6428 inherit_or_split_pseudo2 is not removed. If it is found,
6429 change the current insn onto:
6430 original_pseudo <- inherit_or_split_pseudo2. */
6431 for (prev_insn = PREV_INSN (curr_insn);
6432 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
6433 prev_insn = PREV_INSN (prev_insn))
6435 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
6436 && (prev_set = single_set (prev_insn)) != NULL_RTX
6437 /* There should be no subregs in insn we are
6438 searching because only the original reg might
6439 be in subreg when we changed the mode of
6440 load/store for splitting. */
6441 && REG_P (SET_DEST (prev_set))
6442 && REG_P (SET_SRC (prev_set))
6443 && (int) REGNO (SET_DEST (prev_set)) == sregno
6444 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
6445 >= FIRST_PSEUDO_REGISTER)
6446 && (lra_reg_info[prev_sregno].restore_rtx == NULL_RTX
6448 /* As we consider chain of inheritance or
6449 splitting described in above comment we should
6450 check that sregno and prev_sregno were
6451 inheritance/split pseudos created from the
6452 same original regno. */
6453 (get_regno (lra_reg_info[sregno].restore_rtx) >= 0
6454 && (get_regno (lra_reg_info[sregno].restore_rtx)
6455 == get_regno (lra_reg_info[prev_sregno].restore_rtx))))
6456 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
6458 lra_assert (GET_MODE (SET_SRC (prev_set))
6459 == GET_MODE (regno_reg_rtx[sregno]));
6460 if (GET_CODE (SET_SRC (set)) == SUBREG)
6461 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
6462 else
6463 SET_SRC (set) = SET_SRC (prev_set);
6464 /* As we are finishing with processing the insn
6465 here, check the destination too as it might
6466 inheritance pseudo for another pseudo. */
6467 if (bitmap_bit_p (remove_pseudos, dregno)
6468 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
6469 && (restore_rtx
6470 = lra_reg_info[dregno].restore_rtx) != NULL_RTX)
6472 if (GET_CODE (SET_DEST (set)) == SUBREG)
6473 SUBREG_REG (SET_DEST (set)) = restore_rtx;
6474 else
6475 SET_DEST (set) = restore_rtx;
6477 lra_push_insn_and_update_insn_regno_info (curr_insn);
6478 lra_set_used_insn_alternative_by_uid
6479 (INSN_UID (curr_insn), -1);
6480 done_p = true;
6481 if (lra_dump_file != NULL)
6483 fprintf (lra_dump_file, " Change reload insn:\n");
6484 dump_insn_slim (lra_dump_file, curr_insn);
6489 if (! done_p)
6491 struct lra_insn_reg *reg;
6492 bool restored_regs_p = false;
6493 bool kept_regs_p = false;
6495 curr_id = lra_get_insn_recog_data (curr_insn);
6496 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6498 regno = reg->regno;
6499 restore_rtx = lra_reg_info[regno].restore_rtx;
6500 if (restore_rtx != NULL_RTX)
6502 if (change_p && bitmap_bit_p (remove_pseudos, regno))
6504 lra_substitute_pseudo_within_insn
6505 (curr_insn, regno, restore_rtx, false);
6506 restored_regs_p = true;
6508 else
6509 kept_regs_p = true;
6512 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
6514 /* The instruction has changed since the previous
6515 constraints pass. */
6516 lra_push_insn_and_update_insn_regno_info (curr_insn);
6517 lra_set_used_insn_alternative_by_uid
6518 (INSN_UID (curr_insn), -1);
6520 else if (restored_regs_p)
6521 /* The instruction has been restored to the form that
6522 it had during the previous constraints pass. */
6523 lra_update_insn_regno_info (curr_insn);
6524 if (restored_regs_p && lra_dump_file != NULL)
6526 fprintf (lra_dump_file, " Insn after restoring regs:\n");
6527 dump_insn_slim (lra_dump_file, curr_insn);
6532 return change_p;
6535 /* If optional reload pseudos failed to get a hard register or was not
6536 inherited, it is better to remove optional reloads. We do this
6537 transformation after undoing inheritance to figure out necessity to
6538 remove optional reloads easier. Return true if we do any
6539 change. */
6540 static bool
6541 undo_optional_reloads (void)
6543 bool change_p, keep_p;
6544 unsigned int regno, uid;
6545 bitmap_iterator bi, bi2;
6546 rtx_insn *insn;
6547 rtx set, src, dest;
6548 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
6550 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
6551 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
6552 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6554 keep_p = false;
6555 /* Keep optional reloads from previous subpasses. */
6556 if (lra_reg_info[regno].restore_rtx == NULL_RTX
6557 /* If the original pseudo changed its allocation, just
6558 removing the optional pseudo is dangerous as the original
6559 pseudo will have longer live range. */
6560 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] >= 0)
6561 keep_p = true;
6562 else if (reg_renumber[regno] >= 0)
6563 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
6565 insn = lra_insn_recog_data[uid]->insn;
6566 if ((set = single_set (insn)) == NULL_RTX)
6567 continue;
6568 src = SET_SRC (set);
6569 dest = SET_DEST (set);
6570 if (! REG_P (src) || ! REG_P (dest))
6571 continue;
6572 if (REGNO (dest) == regno
6573 /* Ignore insn for optional reloads itself. */
6574 && REGNO (lra_reg_info[regno].restore_rtx) != REGNO (src)
6575 /* Check only inheritance on last inheritance pass. */
6576 && (int) REGNO (src) >= new_regno_start
6577 /* Check that the optional reload was inherited. */
6578 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6580 keep_p = true;
6581 break;
6584 if (keep_p)
6586 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6587 if (lra_dump_file != NULL)
6588 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6591 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6592 bitmap_initialize (&insn_bitmap, &reg_obstack);
6593 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6595 if (lra_dump_file != NULL)
6596 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6597 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6598 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6600 insn = lra_insn_recog_data[uid]->insn;
6601 if ((set = single_set (insn)) != NULL_RTX)
6603 src = SET_SRC (set);
6604 dest = SET_DEST (set);
6605 if (REG_P (src) && REG_P (dest)
6606 && ((REGNO (src) == regno
6607 && (REGNO (lra_reg_info[regno].restore_rtx)
6608 == REGNO (dest)))
6609 || (REGNO (dest) == regno
6610 && (REGNO (lra_reg_info[regno].restore_rtx)
6611 == REGNO (src)))))
6613 if (lra_dump_file != NULL)
6615 fprintf (lra_dump_file, " Deleting move %u\n",
6616 INSN_UID (insn));
6617 dump_insn_slim (lra_dump_file, insn);
6619 delete_move_and_clobber (insn, REGNO (dest));
6620 continue;
6622 /* We should not worry about generation memory-memory
6623 moves here as if the corresponding inheritance did
6624 not work (inheritance pseudo did not get a hard reg),
6625 we remove the inheritance pseudo and the optional
6626 reload. */
6628 lra_substitute_pseudo_within_insn
6629 (insn, regno, lra_reg_info[regno].restore_rtx, false);
6630 lra_update_insn_regno_info (insn);
6631 if (lra_dump_file != NULL)
6633 fprintf (lra_dump_file,
6634 " Restoring original insn:\n");
6635 dump_insn_slim (lra_dump_file, insn);
6639 /* Clear restore_regnos. */
6640 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6641 lra_reg_info[regno].restore_rtx = NULL_RTX;
6642 bitmap_clear (&insn_bitmap);
6643 bitmap_clear (&removed_optional_reload_pseudos);
6644 return change_p;
6647 /* Entry function for undoing inheritance/split transformation. Return true
6648 if we did any RTL change in this pass. */
6649 bool
6650 lra_undo_inheritance (void)
6652 unsigned int regno;
6653 int hard_regno;
6654 int n_all_inherit, n_inherit, n_all_split, n_split;
6655 rtx restore_rtx;
6656 bitmap_head remove_pseudos;
6657 bitmap_iterator bi;
6658 bool change_p;
6660 lra_undo_inheritance_iter++;
6661 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6662 return false;
6663 if (lra_dump_file != NULL)
6664 fprintf (lra_dump_file,
6665 "\n********** Undoing inheritance #%d: **********\n\n",
6666 lra_undo_inheritance_iter);
6667 bitmap_initialize (&remove_pseudos, &reg_obstack);
6668 n_inherit = n_all_inherit = 0;
6669 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6670 if (lra_reg_info[regno].restore_rtx != NULL_RTX)
6672 n_all_inherit++;
6673 if (reg_renumber[regno] < 0
6674 /* If the original pseudo changed its allocation, just
6675 removing inheritance is dangerous as for changing
6676 allocation we used shorter live-ranges. */
6677 && (! REG_P (lra_reg_info[regno].restore_rtx)
6678 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] < 0))
6679 bitmap_set_bit (&remove_pseudos, regno);
6680 else
6681 n_inherit++;
6683 if (lra_dump_file != NULL && n_all_inherit != 0)
6684 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6685 n_inherit, n_all_inherit,
6686 (double) n_inherit / n_all_inherit * 100);
6687 n_split = n_all_split = 0;
6688 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6689 if ((restore_rtx = lra_reg_info[regno].restore_rtx) != NULL_RTX)
6691 int restore_regno = REGNO (restore_rtx);
6693 n_all_split++;
6694 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6695 ? reg_renumber[restore_regno] : restore_regno);
6696 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6697 bitmap_set_bit (&remove_pseudos, regno);
6698 else
6700 n_split++;
6701 if (lra_dump_file != NULL)
6702 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6703 regno, restore_regno);
6706 if (lra_dump_file != NULL && n_all_split != 0)
6707 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6708 n_split, n_all_split,
6709 (double) n_split / n_all_split * 100);
6710 change_p = remove_inheritance_pseudos (&remove_pseudos);
6711 bitmap_clear (&remove_pseudos);
6712 /* Clear restore_regnos. */
6713 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6714 lra_reg_info[regno].restore_rtx = NULL_RTX;
6715 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6716 lra_reg_info[regno].restore_rtx = NULL_RTX;
6717 change_p = undo_optional_reloads () || change_p;
6718 return change_p;