2015-10-28 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / lra-constraints.c
blobbc7a29290f289285e6ceb51b12aaec6c38d6bb74
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2015 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains code for 3 passes: constraint pass,
23 inheritance/split pass, and pass for undoing failed inheritance and
24 split.
26 The major goal of constraint pass is to transform RTL to satisfy
27 insn and address constraints by:
28 o choosing insn alternatives;
29 o generating *reload insns* (or reloads in brief) and *reload
30 pseudos* which will get necessary hard registers later;
31 o substituting pseudos with equivalent values and removing the
32 instructions that initialized those pseudos.
34 The constraint pass has biggest and most complicated code in LRA.
35 There are a lot of important details like:
36 o reuse of input reload pseudos to simplify reload pseudo
37 allocations;
38 o some heuristics to choose insn alternative to improve the
39 inheritance;
40 o early clobbers etc.
42 The pass is mimicking former reload pass in alternative choosing
43 because the reload pass is oriented to current machine description
44 model. It might be changed if the machine description model is
45 changed.
47 There is special code for preventing all LRA and this pass cycling
48 in case of bugs.
50 On the first iteration of the pass we process every instruction and
51 choose an alternative for each one. On subsequent iterations we try
52 to avoid reprocessing instructions if we can be sure that the old
53 choice is still valid.
55 The inheritance/spilt pass is to transform code to achieve
56 ineheritance and live range splitting. It is done on backward
57 traversal of EBBs.
59 The inheritance optimization goal is to reuse values in hard
60 registers. There is analogous optimization in old reload pass. The
61 inheritance is achieved by following transformation:
63 reload_p1 <- p reload_p1 <- p
64 ... new_p <- reload_p1
65 ... => ...
66 reload_p2 <- p reload_p2 <- new_p
68 where p is spilled and not changed between the insns. Reload_p1 is
69 also called *original pseudo* and new_p is called *inheritance
70 pseudo*.
72 The subsequent assignment pass will try to assign the same (or
73 another if it is not possible) hard register to new_p as to
74 reload_p1 or reload_p2.
76 If the assignment pass fails to assign a hard register to new_p,
77 this file will undo the inheritance and restore the original code.
78 This is because implementing the above sequence with a spilled
79 new_p would make the code much worse. The inheritance is done in
80 EBB scope. The above is just a simplified example to get an idea
81 of the inheritance as the inheritance is also done for non-reload
82 insns.
84 Splitting (transformation) is also done in EBB scope on the same
85 pass as the inheritance:
87 r <- ... or ... <- r r <- ... or ... <- r
88 ... s <- r (new insn -- save)
89 ... =>
90 ... r <- s (new insn -- restore)
91 ... <- r ... <- r
93 The *split pseudo* s is assigned to the hard register of the
94 original pseudo or hard register r.
96 Splitting is done:
97 o In EBBs with high register pressure for global pseudos (living
98 in at least 2 BBs) and assigned to hard registers when there
99 are more one reloads needing the hard registers;
100 o for pseudos needing save/restore code around calls.
102 If the split pseudo still has the same hard register as the
103 original pseudo after the subsequent assignment pass or the
104 original pseudo was split, the opposite transformation is done on
105 the same pass for undoing inheritance. */
107 #undef REG_OK_STRICT
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "backend.h"
113 #include "predict.h"
114 #include "tree.h"
115 #include "rtl.h"
116 #include "df.h"
117 #include "tm_p.h"
118 #include "regs.h"
119 #include "insn-config.h"
120 #include "insn-codes.h"
121 #include "recog.h"
122 #include "output.h"
123 #include "addresses.h"
124 #include "target.h"
125 #include "flags.h"
126 #include "alias.h"
127 #include "expmed.h"
128 #include "dojump.h"
129 #include "explow.h"
130 #include "calls.h"
131 #include "emit-rtl.h"
132 #include "varasm.h"
133 #include "stmt.h"
134 #include "expr.h"
135 #include "cfgrtl.h"
136 #include "except.h"
137 #include "optabs.h"
138 #include "ira.h"
139 #include "rtl-error.h"
140 #include "params.h"
141 #include "lra.h"
142 #include "insn-attr.h"
143 #include "lra-int.h"
144 #include "print-rtl.h"
146 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
147 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
148 reload insns. */
149 static int bb_reload_num;
151 /* The current insn being processed and corresponding its single set
152 (NULL otherwise), its data (basic block, the insn data, the insn
153 static data, and the mode of each operand). */
154 static rtx_insn *curr_insn;
155 static rtx curr_insn_set;
156 static basic_block curr_bb;
157 static lra_insn_recog_data_t curr_id;
158 static struct lra_static_insn_data *curr_static_id;
159 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
160 /* Mode of the register substituted by its equivalence with VOIDmode
161 (e.g. constant) and whose subreg is given operand of the current
162 insn. VOIDmode in all other cases. */
163 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
167 /* Start numbers for new registers and insns at the current constraints
168 pass start. */
169 static int new_regno_start;
170 static int new_insn_uid_start;
172 /* If LOC is nonnull, strip any outer subreg from it. */
173 static inline rtx *
174 strip_subreg (rtx *loc)
176 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
179 /* Return hard regno of REGNO or if it is was not assigned to a hard
180 register, use a hard register from its allocno class. */
181 static int
182 get_try_hard_regno (int regno)
184 int hard_regno;
185 enum reg_class rclass;
187 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
188 hard_regno = lra_get_regno_hard_regno (regno);
189 if (hard_regno >= 0)
190 return hard_regno;
191 rclass = lra_get_allocno_class (regno);
192 if (rclass == NO_REGS)
193 return -1;
194 return ira_class_hard_regs[rclass][0];
197 /* Return final hard regno (plus offset) which will be after
198 elimination. We do this for matching constraints because the final
199 hard regno could have a different class. */
200 static int
201 get_final_hard_regno (int hard_regno, int offset)
203 if (hard_regno < 0)
204 return hard_regno;
205 hard_regno = lra_get_elimination_hard_regno (hard_regno);
206 return hard_regno + offset;
209 /* Return hard regno of X after removing subreg and making
210 elimination. If X is not a register or subreg of register, return
211 -1. For pseudo use its assignment. */
212 static int
213 get_hard_regno (rtx x)
215 rtx reg;
216 int offset, hard_regno;
218 reg = x;
219 if (GET_CODE (x) == SUBREG)
220 reg = SUBREG_REG (x);
221 if (! REG_P (reg))
222 return -1;
223 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
224 hard_regno = lra_get_regno_hard_regno (hard_regno);
225 if (hard_regno < 0)
226 return -1;
227 offset = 0;
228 if (GET_CODE (x) == SUBREG)
229 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
230 SUBREG_BYTE (x), GET_MODE (x));
231 return get_final_hard_regno (hard_regno, offset);
234 /* If REGNO is a hard register or has been allocated a hard register,
235 return the class of that register. If REGNO is a reload pseudo
236 created by the current constraints pass, return its allocno class.
237 Return NO_REGS otherwise. */
238 static enum reg_class
239 get_reg_class (int regno)
241 int hard_regno;
243 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
244 hard_regno = lra_get_regno_hard_regno (regno);
245 if (hard_regno >= 0)
247 hard_regno = get_final_hard_regno (hard_regno, 0);
248 return REGNO_REG_CLASS (hard_regno);
250 if (regno >= new_regno_start)
251 return lra_get_allocno_class (regno);
252 return NO_REGS;
255 /* Return true if REG satisfies (or will satisfy) reg class constraint
256 CL. Use elimination first if REG is a hard register. If REG is a
257 reload pseudo created by this constraints pass, assume that it will
258 be allocated a hard register from its allocno class, but allow that
259 class to be narrowed to CL if it is currently a superset of CL.
261 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
262 REGNO (reg), or NO_REGS if no change in its class was needed. */
263 static bool
264 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
266 enum reg_class rclass, common_class;
267 machine_mode reg_mode;
268 int class_size, hard_regno, nregs, i, j;
269 int regno = REGNO (reg);
271 if (new_class != NULL)
272 *new_class = NO_REGS;
273 if (regno < FIRST_PSEUDO_REGISTER)
275 rtx final_reg = reg;
276 rtx *final_loc = &final_reg;
278 lra_eliminate_reg_if_possible (final_loc);
279 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
281 reg_mode = GET_MODE (reg);
282 rclass = get_reg_class (regno);
283 if (regno < new_regno_start
284 /* Do not allow the constraints for reload instructions to
285 influence the classes of new pseudos. These reloads are
286 typically moves that have many alternatives, and restricting
287 reload pseudos for one alternative may lead to situations
288 where other reload pseudos are no longer allocatable. */
289 || (INSN_UID (curr_insn) >= new_insn_uid_start
290 && curr_insn_set != NULL
291 && ((OBJECT_P (SET_SRC (curr_insn_set))
292 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
293 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
294 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
295 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
296 /* When we don't know what class will be used finally for reload
297 pseudos, we use ALL_REGS. */
298 return ((regno >= new_regno_start && rclass == ALL_REGS)
299 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
300 && ! hard_reg_set_subset_p (reg_class_contents[cl],
301 lra_no_alloc_regs)));
302 else
304 common_class = ira_reg_class_subset[rclass][cl];
305 if (new_class != NULL)
306 *new_class = common_class;
307 if (hard_reg_set_subset_p (reg_class_contents[common_class],
308 lra_no_alloc_regs))
309 return false;
310 /* Check that there are enough allocatable regs. */
311 class_size = ira_class_hard_regs_num[common_class];
312 for (i = 0; i < class_size; i++)
314 hard_regno = ira_class_hard_regs[common_class][i];
315 nregs = hard_regno_nregs[hard_regno][reg_mode];
316 if (nregs == 1)
317 return true;
318 for (j = 0; j < nregs; j++)
319 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
320 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
321 hard_regno + j))
322 break;
323 if (j >= nregs)
324 return true;
326 return false;
330 /* Return true if REGNO satisfies a memory constraint. */
331 static bool
332 in_mem_p (int regno)
334 return get_reg_class (regno) == NO_REGS;
337 /* Return 1 if ADDR is a valid memory address for mode MODE in address
338 space AS, and check that each pseudo has the proper kind of hard
339 reg. */
340 static int
341 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
342 rtx addr, addr_space_t as)
344 #ifdef GO_IF_LEGITIMATE_ADDRESS
345 lra_assert (ADDR_SPACE_GENERIC_P (as));
346 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
347 return 0;
349 win:
350 return 1;
351 #else
352 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
353 #endif
356 namespace {
357 /* Temporarily eliminates registers in an address (for the lifetime of
358 the object). */
359 class address_eliminator {
360 public:
361 address_eliminator (struct address_info *ad);
362 ~address_eliminator ();
364 private:
365 struct address_info *m_ad;
366 rtx *m_base_loc;
367 rtx m_base_reg;
368 rtx *m_index_loc;
369 rtx m_index_reg;
373 address_eliminator::address_eliminator (struct address_info *ad)
374 : m_ad (ad),
375 m_base_loc (strip_subreg (ad->base_term)),
376 m_base_reg (NULL_RTX),
377 m_index_loc (strip_subreg (ad->index_term)),
378 m_index_reg (NULL_RTX)
380 if (m_base_loc != NULL)
382 m_base_reg = *m_base_loc;
383 lra_eliminate_reg_if_possible (m_base_loc);
384 if (m_ad->base_term2 != NULL)
385 *m_ad->base_term2 = *m_ad->base_term;
387 if (m_index_loc != NULL)
389 m_index_reg = *m_index_loc;
390 lra_eliminate_reg_if_possible (m_index_loc);
394 address_eliminator::~address_eliminator ()
396 if (m_base_loc && *m_base_loc != m_base_reg)
398 *m_base_loc = m_base_reg;
399 if (m_ad->base_term2 != NULL)
400 *m_ad->base_term2 = *m_ad->base_term;
402 if (m_index_loc && *m_index_loc != m_index_reg)
403 *m_index_loc = m_index_reg;
406 /* Return true if the eliminated form of AD is a legitimate target address. */
407 static bool
408 valid_address_p (struct address_info *ad)
410 address_eliminator eliminator (ad);
411 return valid_address_p (ad->mode, *ad->outer, ad->as);
414 /* Return true if the eliminated form of memory reference OP satisfies
415 extra memory constraint CONSTRAINT. */
416 static bool
417 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
419 struct address_info ad;
421 decompose_mem_address (&ad, op);
422 address_eliminator eliminator (&ad);
423 return constraint_satisfied_p (op, constraint);
426 /* Return true if the eliminated form of address AD satisfies extra
427 address constraint CONSTRAINT. */
428 static bool
429 satisfies_address_constraint_p (struct address_info *ad,
430 enum constraint_num constraint)
432 address_eliminator eliminator (ad);
433 return constraint_satisfied_p (*ad->outer, constraint);
436 /* Return true if the eliminated form of address OP satisfies extra
437 address constraint CONSTRAINT. */
438 static bool
439 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
441 struct address_info ad;
443 decompose_lea_address (&ad, &op);
444 return satisfies_address_constraint_p (&ad, constraint);
447 /* Initiate equivalences for LRA. As we keep original equivalences
448 before any elimination, we need to make copies otherwise any change
449 in insns might change the equivalences. */
450 void
451 lra_init_equiv (void)
453 ira_expand_reg_equiv ();
454 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
456 rtx res;
458 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
459 ira_reg_equiv[i].memory = copy_rtx (res);
460 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
461 ira_reg_equiv[i].invariant = copy_rtx (res);
465 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
467 /* Update equivalence for REGNO. We need to this as the equivalence
468 might contain other pseudos which are changed by their
469 equivalences. */
470 static void
471 update_equiv (int regno)
473 rtx x;
475 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
476 ira_reg_equiv[regno].memory
477 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
478 NULL_RTX);
479 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
480 ira_reg_equiv[regno].invariant
481 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
482 NULL_RTX);
485 /* If we have decided to substitute X with another value, return that
486 value, otherwise return X. */
487 static rtx
488 get_equiv (rtx x)
490 int regno;
491 rtx res;
493 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
494 || ! ira_reg_equiv[regno].defined_p
495 || ! ira_reg_equiv[regno].profitable_p
496 || lra_get_regno_hard_regno (regno) >= 0)
497 return x;
498 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
500 if (targetm.cannot_substitute_mem_equiv_p (res))
501 return x;
502 return res;
504 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
505 return res;
506 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
507 return res;
508 gcc_unreachable ();
511 /* If we have decided to substitute X with the equivalent value,
512 return that value after elimination for INSN, otherwise return
513 X. */
514 static rtx
515 get_equiv_with_elimination (rtx x, rtx_insn *insn)
517 rtx res = get_equiv (x);
519 if (x == res || CONSTANT_P (res))
520 return res;
521 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
522 false, false, 0, true);
525 /* Set up curr_operand_mode. */
526 static void
527 init_curr_operand_mode (void)
529 int nop = curr_static_id->n_operands;
530 for (int i = 0; i < nop; i++)
532 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
533 if (mode == VOIDmode)
535 /* The .md mode for address operands is the mode of the
536 addressed value rather than the mode of the address itself. */
537 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
538 mode = Pmode;
539 else
540 mode = curr_static_id->operand[i].mode;
542 curr_operand_mode[i] = mode;
548 /* The page contains code to reuse input reloads. */
550 /* Structure describes input reload of the current insns. */
551 struct input_reload
553 /* Reloaded value. */
554 rtx input;
555 /* Reload pseudo used. */
556 rtx reg;
559 /* The number of elements in the following array. */
560 static int curr_insn_input_reloads_num;
561 /* Array containing info about input reloads. It is used to find the
562 same input reload and reuse the reload pseudo in this case. */
563 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
565 /* Initiate data concerning reuse of input reloads for the current
566 insn. */
567 static void
568 init_curr_insn_input_reloads (void)
570 curr_insn_input_reloads_num = 0;
573 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
574 created input reload pseudo (only if TYPE is not OP_OUT). Don't
575 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
576 wrapped up in SUBREG. The result pseudo is returned through
577 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
578 reused the already created input reload pseudo. Use TITLE to
579 describe new registers for debug purposes. */
580 static bool
581 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
582 enum reg_class rclass, bool in_subreg_p,
583 const char *title, rtx *result_reg)
585 int i, regno;
586 enum reg_class new_class;
588 if (type == OP_OUT)
590 *result_reg
591 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
592 return true;
594 /* Prevent reuse value of expression with side effects,
595 e.g. volatile memory. */
596 if (! side_effects_p (original))
597 for (i = 0; i < curr_insn_input_reloads_num; i++)
598 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
599 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
601 rtx reg = curr_insn_input_reloads[i].reg;
602 regno = REGNO (reg);
603 /* If input is equal to original and both are VOIDmode,
604 GET_MODE (reg) might be still different from mode.
605 Ensure we don't return *result_reg with wrong mode. */
606 if (GET_MODE (reg) != mode)
608 if (in_subreg_p)
609 continue;
610 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
611 continue;
612 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
613 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
614 continue;
616 *result_reg = reg;
617 if (lra_dump_file != NULL)
619 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
620 dump_value_slim (lra_dump_file, original, 1);
622 if (new_class != lra_get_allocno_class (regno))
623 lra_change_class (regno, new_class, ", change to", false);
624 if (lra_dump_file != NULL)
625 fprintf (lra_dump_file, "\n");
626 return false;
628 *result_reg = lra_create_new_reg (mode, original, rclass, title);
629 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
630 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
631 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
632 return true;
637 /* The page contains code to extract memory address parts. */
639 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
640 static inline bool
641 ok_for_index_p_nonstrict (rtx reg)
643 unsigned regno = REGNO (reg);
645 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
648 /* A version of regno_ok_for_base_p for use here, when all pseudos
649 should count as OK. Arguments as for regno_ok_for_base_p. */
650 static inline bool
651 ok_for_base_p_nonstrict (rtx reg, machine_mode mode, addr_space_t as,
652 enum rtx_code outer_code, enum rtx_code index_code)
654 unsigned regno = REGNO (reg);
656 if (regno >= FIRST_PSEUDO_REGISTER)
657 return true;
658 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
663 /* The page contains major code to choose the current insn alternative
664 and generate reloads for it. */
666 /* Return the offset from REGNO of the least significant register
667 in (reg:MODE REGNO).
669 This function is used to tell whether two registers satisfy
670 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
672 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
673 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
675 lra_constraint_offset (int regno, machine_mode mode)
677 lra_assert (regno < FIRST_PSEUDO_REGISTER);
678 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
679 && SCALAR_INT_MODE_P (mode))
680 return hard_regno_nregs[regno][mode] - 1;
681 return 0;
684 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
685 if they are the same hard reg, and has special hacks for
686 auto-increment and auto-decrement. This is specifically intended for
687 process_alt_operands to use in determining whether two operands
688 match. X is the operand whose number is the lower of the two.
690 It is supposed that X is the output operand and Y is the input
691 operand. Y_HARD_REGNO is the final hard regno of register Y or
692 register in subreg Y as we know it now. Otherwise, it is a
693 negative value. */
694 static bool
695 operands_match_p (rtx x, rtx y, int y_hard_regno)
697 int i;
698 RTX_CODE code = GET_CODE (x);
699 const char *fmt;
701 if (x == y)
702 return true;
703 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
704 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
706 int j;
708 i = get_hard_regno (x);
709 if (i < 0)
710 goto slow;
712 if ((j = y_hard_regno) < 0)
713 goto slow;
715 i += lra_constraint_offset (i, GET_MODE (x));
716 j += lra_constraint_offset (j, GET_MODE (y));
718 return i == j;
721 /* If two operands must match, because they are really a single
722 operand of an assembler insn, then two post-increments are invalid
723 because the assembler insn would increment only once. On the
724 other hand, a post-increment matches ordinary indexing if the
725 post-increment is the output operand. */
726 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
727 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
729 /* Two pre-increments are invalid because the assembler insn would
730 increment only once. On the other hand, a pre-increment matches
731 ordinary indexing if the pre-increment is the input operand. */
732 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
733 || GET_CODE (y) == PRE_MODIFY)
734 return operands_match_p (x, XEXP (y, 0), -1);
736 slow:
738 if (code == REG && REG_P (y))
739 return REGNO (x) == REGNO (y);
741 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
742 && x == SUBREG_REG (y))
743 return true;
744 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
745 && SUBREG_REG (x) == y)
746 return true;
748 /* Now we have disposed of all the cases in which different rtx
749 codes can match. */
750 if (code != GET_CODE (y))
751 return false;
753 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
754 if (GET_MODE (x) != GET_MODE (y))
755 return false;
757 switch (code)
759 CASE_CONST_UNIQUE:
760 return false;
762 case LABEL_REF:
763 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
764 case SYMBOL_REF:
765 return XSTR (x, 0) == XSTR (y, 0);
767 default:
768 break;
771 /* Compare the elements. If any pair of corresponding elements fail
772 to match, return false for the whole things. */
774 fmt = GET_RTX_FORMAT (code);
775 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
777 int val, j;
778 switch (fmt[i])
780 case 'w':
781 if (XWINT (x, i) != XWINT (y, i))
782 return false;
783 break;
785 case 'i':
786 if (XINT (x, i) != XINT (y, i))
787 return false;
788 break;
790 case 'e':
791 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
792 if (val == 0)
793 return false;
794 break;
796 case '0':
797 break;
799 case 'E':
800 if (XVECLEN (x, i) != XVECLEN (y, i))
801 return false;
802 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
804 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
805 if (val == 0)
806 return false;
808 break;
810 /* It is believed that rtx's at this level will never
811 contain anything but integers and other rtx's, except for
812 within LABEL_REFs and SYMBOL_REFs. */
813 default:
814 gcc_unreachable ();
817 return true;
820 /* True if X is a constant that can be forced into the constant pool.
821 MODE is the mode of the operand, or VOIDmode if not known. */
822 #define CONST_POOL_OK_P(MODE, X) \
823 ((MODE) != VOIDmode \
824 && CONSTANT_P (X) \
825 && GET_CODE (X) != HIGH \
826 && !targetm.cannot_force_const_mem (MODE, X))
828 /* True if C is a non-empty register class that has too few registers
829 to be safely used as a reload target class. */
830 #define SMALL_REGISTER_CLASS_P(C) \
831 (ira_class_hard_regs_num [(C)] == 1 \
832 || (ira_class_hard_regs_num [(C)] >= 1 \
833 && targetm.class_likely_spilled_p (C)))
835 /* If REG is a reload pseudo, try to make its class satisfying CL. */
836 static void
837 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
839 enum reg_class rclass;
841 /* Do not make more accurate class from reloads generated. They are
842 mostly moves with a lot of constraints. Making more accurate
843 class may results in very narrow class and impossibility of find
844 registers for several reloads of one insn. */
845 if (INSN_UID (curr_insn) >= new_insn_uid_start)
846 return;
847 if (GET_CODE (reg) == SUBREG)
848 reg = SUBREG_REG (reg);
849 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
850 return;
851 if (in_class_p (reg, cl, &rclass) && rclass != cl)
852 lra_change_class (REGNO (reg), rclass, " Change to", true);
855 /* Generate reloads for matching OUT and INS (array of input operand
856 numbers with end marker -1) with reg class GOAL_CLASS. Add input
857 and output reloads correspondingly to the lists *BEFORE and *AFTER.
858 OUT might be negative. In this case we generate input reloads for
859 matched input operands INS. EARLY_CLOBBER_P is a flag that the
860 output operand is early clobbered for chosen alternative. */
861 static void
862 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
863 rtx_insn **before, rtx_insn **after, bool early_clobber_p)
865 int i, in;
866 rtx new_in_reg, new_out_reg, reg;
867 machine_mode inmode, outmode;
868 rtx in_rtx = *curr_id->operand_loc[ins[0]];
869 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
871 inmode = curr_operand_mode[ins[0]];
872 outmode = out < 0 ? inmode : curr_operand_mode[out];
873 push_to_sequence (*before);
874 if (inmode != outmode)
876 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
878 reg = new_in_reg
879 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
880 goal_class, "");
881 if (SCALAR_INT_MODE_P (inmode))
882 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
883 else
884 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
885 LRA_SUBREG_P (new_out_reg) = 1;
886 /* If the input reg is dying here, we can use the same hard
887 register for REG and IN_RTX. We do it only for original
888 pseudos as reload pseudos can die although original
889 pseudos still live where reload pseudos dies. */
890 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
891 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
892 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
894 else
896 reg = new_out_reg
897 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
898 goal_class, "");
899 if (SCALAR_INT_MODE_P (outmode))
900 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
901 else
902 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
903 /* NEW_IN_REG is non-paradoxical subreg. We don't want
904 NEW_OUT_REG living above. We add clobber clause for
905 this. This is just a temporary clobber. We can remove
906 it at the end of LRA work. */
907 rtx_insn *clobber = emit_clobber (new_out_reg);
908 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
909 LRA_SUBREG_P (new_in_reg) = 1;
910 if (GET_CODE (in_rtx) == SUBREG)
912 rtx subreg_reg = SUBREG_REG (in_rtx);
914 /* If SUBREG_REG is dying here and sub-registers IN_RTX
915 and NEW_IN_REG are similar, we can use the same hard
916 register for REG and SUBREG_REG. */
917 if (REG_P (subreg_reg)
918 && (int) REGNO (subreg_reg) < lra_new_regno_start
919 && GET_MODE (subreg_reg) == outmode
920 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
921 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
922 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
926 else
928 /* Pseudos have values -- see comments for lra_reg_info.
929 Different pseudos with the same value do not conflict even if
930 they live in the same place. When we create a pseudo we
931 assign value of original pseudo (if any) from which we
932 created the new pseudo. If we create the pseudo from the
933 input pseudo, the new pseudo will have no conflict with the
934 input pseudo which is wrong when the input pseudo lives after
935 the insn and as the new pseudo value is changed by the insn
936 output. Therefore we create the new pseudo from the output
937 except the case when we have single matched dying input
938 pseudo.
940 We cannot reuse the current output register because we might
941 have a situation like "a <- a op b", where the constraints
942 force the second input operand ("b") to match the output
943 operand ("a"). "b" must then be copied into a new register
944 so that it doesn't clobber the current value of "a".
946 We can not use the same value if the output pseudo is
947 early clobbered or the input pseudo is mentioned in the
948 output, e.g. as an address part in memory, because
949 output reload will actually extend the pseudo liveness.
950 We don't care about eliminable hard regs here as we are
951 interesting only in pseudos. */
953 new_in_reg = new_out_reg
954 = (! early_clobber_p && ins[1] < 0 && REG_P (in_rtx)
955 && (int) REGNO (in_rtx) < lra_new_regno_start
956 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
957 && (out < 0 || regno_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
958 ? lra_create_new_reg (inmode, in_rtx, goal_class, "")
959 : lra_create_new_reg_with_unique_value (outmode, out_rtx,
960 goal_class, ""));
962 /* In operand can be got from transformations before processing insn
963 constraints. One example of such transformations is subreg
964 reloading (see function simplify_operand_subreg). The new
965 pseudos created by the transformations might have inaccurate
966 class (ALL_REGS) and we should make their classes more
967 accurate. */
968 narrow_reload_pseudo_class (in_rtx, goal_class);
969 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
970 *before = get_insns ();
971 end_sequence ();
972 for (i = 0; (in = ins[i]) >= 0; i++)
974 lra_assert
975 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
976 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
977 *curr_id->operand_loc[in] = new_in_reg;
979 lra_update_dups (curr_id, ins);
980 if (out < 0)
981 return;
982 /* See a comment for the input operand above. */
983 narrow_reload_pseudo_class (out_rtx, goal_class);
984 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
986 start_sequence ();
987 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
988 emit_insn (*after);
989 *after = get_insns ();
990 end_sequence ();
992 *curr_id->operand_loc[out] = new_out_reg;
993 lra_update_dup (curr_id, out);
996 /* Return register class which is union of all reg classes in insn
997 constraint alternative string starting with P. */
998 static enum reg_class
999 reg_class_from_constraints (const char *p)
1001 int c, len;
1002 enum reg_class op_class = NO_REGS;
1005 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1007 case '#':
1008 case ',':
1009 return op_class;
1011 case 'g':
1012 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1013 break;
1015 default:
1016 enum constraint_num cn = lookup_constraint (p);
1017 enum reg_class cl = reg_class_for_constraint (cn);
1018 if (cl == NO_REGS)
1020 if (insn_extra_address_constraint (cn))
1021 op_class
1022 = (reg_class_subunion
1023 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1024 ADDRESS, SCRATCH)]);
1025 break;
1028 op_class = reg_class_subunion[op_class][cl];
1029 break;
1031 while ((p += len), c);
1032 return op_class;
1035 /* If OP is a register, return the class of the register as per
1036 get_reg_class, otherwise return NO_REGS. */
1037 static inline enum reg_class
1038 get_op_class (rtx op)
1040 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1043 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1044 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1045 SUBREG for VAL to make them equal. */
1046 static rtx_insn *
1047 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1049 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1051 /* Usually size of mem_pseudo is greater than val size but in
1052 rare cases it can be less as it can be defined by target
1053 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1054 if (! MEM_P (val))
1056 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1057 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1059 LRA_SUBREG_P (val) = 1;
1061 else
1063 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1064 LRA_SUBREG_P (mem_pseudo) = 1;
1067 return to_p ? gen_move_insn (mem_pseudo, val)
1068 : gen_move_insn (val, mem_pseudo);
1071 /* Process a special case insn (register move), return true if we
1072 don't need to process it anymore. INSN should be a single set
1073 insn. Set up that RTL was changed through CHANGE_P and macro
1074 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1075 SEC_MEM_P. */
1076 static bool
1077 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1079 int sregno, dregno;
1080 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1081 rtx_insn *before;
1082 enum reg_class dclass, sclass, secondary_class;
1083 secondary_reload_info sri;
1085 lra_assert (curr_insn_set != NULL_RTX);
1086 dreg = dest = SET_DEST (curr_insn_set);
1087 sreg = src = SET_SRC (curr_insn_set);
1088 if (GET_CODE (dest) == SUBREG)
1089 dreg = SUBREG_REG (dest);
1090 if (GET_CODE (src) == SUBREG)
1091 sreg = SUBREG_REG (src);
1092 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1093 return false;
1094 sclass = dclass = NO_REGS;
1095 if (REG_P (dreg))
1096 dclass = get_reg_class (REGNO (dreg));
1097 if (dclass == ALL_REGS)
1098 /* ALL_REGS is used for new pseudos created by transformations
1099 like reload of SUBREG_REG (see function
1100 simplify_operand_subreg). We don't know their class yet. We
1101 should figure out the class from processing the insn
1102 constraints not in this fast path function. Even if ALL_REGS
1103 were a right class for the pseudo, secondary_... hooks usually
1104 are not define for ALL_REGS. */
1105 return false;
1106 if (REG_P (sreg))
1107 sclass = get_reg_class (REGNO (sreg));
1108 if (sclass == ALL_REGS)
1109 /* See comments above. */
1110 return false;
1111 if (sclass == NO_REGS && dclass == NO_REGS)
1112 return false;
1113 #ifdef SECONDARY_MEMORY_NEEDED
1114 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1115 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1116 && ((sclass != NO_REGS && dclass != NO_REGS)
1117 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1118 #endif
1121 *sec_mem_p = true;
1122 return false;
1124 #endif
1125 if (! REG_P (dreg) || ! REG_P (sreg))
1126 return false;
1127 sri.prev_sri = NULL;
1128 sri.icode = CODE_FOR_nothing;
1129 sri.extra_cost = 0;
1130 secondary_class = NO_REGS;
1131 /* Set up hard register for a reload pseudo for hook
1132 secondary_reload because some targets just ignore unassigned
1133 pseudos in the hook. */
1134 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1136 dregno = REGNO (dreg);
1137 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1139 else
1140 dregno = -1;
1141 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1143 sregno = REGNO (sreg);
1144 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1146 else
1147 sregno = -1;
1148 if (sclass != NO_REGS)
1149 secondary_class
1150 = (enum reg_class) targetm.secondary_reload (false, dest,
1151 (reg_class_t) sclass,
1152 GET_MODE (src), &sri);
1153 if (sclass == NO_REGS
1154 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1155 && dclass != NO_REGS))
1157 enum reg_class old_sclass = secondary_class;
1158 secondary_reload_info old_sri = sri;
1160 sri.prev_sri = NULL;
1161 sri.icode = CODE_FOR_nothing;
1162 sri.extra_cost = 0;
1163 secondary_class
1164 = (enum reg_class) targetm.secondary_reload (true, src,
1165 (reg_class_t) dclass,
1166 GET_MODE (src), &sri);
1167 /* Check the target hook consistency. */
1168 lra_assert
1169 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1170 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1171 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1173 if (sregno >= 0)
1174 reg_renumber [sregno] = -1;
1175 if (dregno >= 0)
1176 reg_renumber [dregno] = -1;
1177 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1178 return false;
1179 *change_p = true;
1180 new_reg = NULL_RTX;
1181 if (secondary_class != NO_REGS)
1182 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1183 secondary_class,
1184 "secondary");
1185 start_sequence ();
1186 if (sri.icode == CODE_FOR_nothing)
1187 lra_emit_move (new_reg, src);
1188 else
1190 enum reg_class scratch_class;
1192 scratch_class = (reg_class_from_constraints
1193 (insn_data[sri.icode].operand[2].constraint));
1194 scratch_reg = (lra_create_new_reg_with_unique_value
1195 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1196 scratch_class, "scratch"));
1197 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1198 src, scratch_reg));
1200 before = get_insns ();
1201 end_sequence ();
1202 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1203 if (new_reg != NULL_RTX)
1204 SET_SRC (curr_insn_set) = new_reg;
1205 else
1207 if (lra_dump_file != NULL)
1209 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1210 dump_insn_slim (lra_dump_file, curr_insn);
1212 lra_set_insn_deleted (curr_insn);
1213 return true;
1215 return false;
1218 /* The following data describe the result of process_alt_operands.
1219 The data are used in curr_insn_transform to generate reloads. */
1221 /* The chosen reg classes which should be used for the corresponding
1222 operands. */
1223 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1224 /* True if the operand should be the same as another operand and that
1225 other operand does not need a reload. */
1226 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1227 /* True if the operand does not need a reload. */
1228 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1229 /* True if the operand can be offsetable memory. */
1230 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1231 /* The number of an operand to which given operand can be matched to. */
1232 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1233 /* The number of elements in the following array. */
1234 static int goal_alt_dont_inherit_ops_num;
1235 /* Numbers of operands whose reload pseudos should not be inherited. */
1236 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1237 /* True if the insn commutative operands should be swapped. */
1238 static bool goal_alt_swapped;
1239 /* The chosen insn alternative. */
1240 static int goal_alt_number;
1242 /* The following five variables are used to choose the best insn
1243 alternative. They reflect final characteristics of the best
1244 alternative. */
1246 /* Number of necessary reloads and overall cost reflecting the
1247 previous value and other unpleasantness of the best alternative. */
1248 static int best_losers, best_overall;
1249 /* Overall number hard registers used for reloads. For example, on
1250 some targets we need 2 general registers to reload DFmode and only
1251 one floating point register. */
1252 static int best_reload_nregs;
1253 /* Overall number reflecting distances of previous reloading the same
1254 value. The distances are counted from the current BB start. It is
1255 used to improve inheritance chances. */
1256 static int best_reload_sum;
1258 /* True if the current insn should have no correspondingly input or
1259 output reloads. */
1260 static bool no_input_reloads_p, no_output_reloads_p;
1262 /* True if we swapped the commutative operands in the current
1263 insn. */
1264 static int curr_swapped;
1266 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1267 register of class CL. Add any input reloads to list BEFORE. AFTER
1268 is nonnull if *LOC is an automodified value; handle that case by
1269 adding the required output reloads to list AFTER. Return true if
1270 the RTL was changed.
1272 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1273 register. Return false if the address register is correct. */
1274 static bool
1275 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1276 enum reg_class cl)
1278 int regno;
1279 enum reg_class rclass, new_class;
1280 rtx reg;
1281 rtx new_reg;
1282 machine_mode mode;
1283 bool subreg_p, before_p = false;
1285 subreg_p = GET_CODE (*loc) == SUBREG;
1286 if (subreg_p)
1287 loc = &SUBREG_REG (*loc);
1288 reg = *loc;
1289 mode = GET_MODE (reg);
1290 if (! REG_P (reg))
1292 if (check_only_p)
1293 return true;
1294 /* Always reload memory in an address even if the target supports
1295 such addresses. */
1296 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1297 before_p = true;
1299 else
1301 regno = REGNO (reg);
1302 rclass = get_reg_class (regno);
1303 if (! check_only_p
1304 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1306 if (lra_dump_file != NULL)
1308 fprintf (lra_dump_file,
1309 "Changing pseudo %d in address of insn %u on equiv ",
1310 REGNO (reg), INSN_UID (curr_insn));
1311 dump_value_slim (lra_dump_file, *loc, 1);
1312 fprintf (lra_dump_file, "\n");
1314 *loc = copy_rtx (*loc);
1316 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1318 if (check_only_p)
1319 return true;
1320 reg = *loc;
1321 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1322 mode, reg, cl, subreg_p, "address", &new_reg))
1323 before_p = true;
1325 else if (new_class != NO_REGS && rclass != new_class)
1327 if (check_only_p)
1328 return true;
1329 lra_change_class (regno, new_class, " Change to", true);
1330 return false;
1332 else
1333 return false;
1335 if (before_p)
1337 push_to_sequence (*before);
1338 lra_emit_move (new_reg, reg);
1339 *before = get_insns ();
1340 end_sequence ();
1342 *loc = new_reg;
1343 if (after != NULL)
1345 start_sequence ();
1346 lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1347 emit_insn (*after);
1348 *after = get_insns ();
1349 end_sequence ();
1351 return true;
1354 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1355 the insn to be inserted before curr insn. AFTER returns the
1356 the insn to be inserted after curr insn. ORIGREG and NEWREG
1357 are the original reg and new reg for reload. */
1358 static void
1359 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1360 rtx newreg)
1362 if (before)
1364 push_to_sequence (*before);
1365 lra_emit_move (newreg, origreg);
1366 *before = get_insns ();
1367 end_sequence ();
1369 if (after)
1371 start_sequence ();
1372 lra_emit_move (origreg, newreg);
1373 emit_insn (*after);
1374 *after = get_insns ();
1375 end_sequence ();
1379 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1381 /* Make reloads for subreg in operand NOP with internal subreg mode
1382 REG_MODE, add new reloads for further processing. Return true if
1383 any change was done. */
1384 static bool
1385 simplify_operand_subreg (int nop, machine_mode reg_mode)
1387 int hard_regno;
1388 rtx_insn *before, *after;
1389 machine_mode mode, innermode;
1390 rtx reg, new_reg;
1391 rtx operand = *curr_id->operand_loc[nop];
1392 enum reg_class regclass;
1393 enum op_type type;
1395 before = after = NULL;
1397 if (GET_CODE (operand) != SUBREG)
1398 return false;
1400 mode = GET_MODE (operand);
1401 reg = SUBREG_REG (operand);
1402 innermode = GET_MODE (reg);
1403 type = curr_static_id->operand[nop].type;
1404 /* If we change address for paradoxical subreg of memory, the
1405 address might violate the necessary alignment or the access might
1406 be slow. So take this into consideration. We should not worry
1407 about access beyond allocated memory for paradoxical memory
1408 subregs as we don't substitute such equiv memory (see processing
1409 equivalences in function lra_constraints) and because for spilled
1410 pseudos we allocate stack memory enough for the biggest
1411 corresponding paradoxical subreg. */
1412 if (MEM_P (reg)
1413 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1414 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1416 rtx subst, old = *curr_id->operand_loc[nop];
1418 alter_subreg (curr_id->operand_loc[nop], false);
1419 subst = *curr_id->operand_loc[nop];
1420 lra_assert (MEM_P (subst));
1421 if (! valid_address_p (innermode, XEXP (reg, 0),
1422 MEM_ADDR_SPACE (reg))
1423 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1424 MEM_ADDR_SPACE (subst)))
1425 return true;
1426 /* If the address was valid and became invalid, prefer to reload
1427 the memory. Typical case is when the index scale should
1428 correspond the memory. */
1429 *curr_id->operand_loc[nop] = old;
1431 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1433 alter_subreg (curr_id->operand_loc[nop], false);
1434 return true;
1436 else if (CONSTANT_P (reg))
1438 /* Try to simplify subreg of constant. It is usually result of
1439 equivalence substitution. */
1440 if (innermode == VOIDmode
1441 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1442 innermode = curr_static_id->operand[nop].mode;
1443 if ((new_reg = simplify_subreg (mode, reg, innermode,
1444 SUBREG_BYTE (operand))) != NULL_RTX)
1446 *curr_id->operand_loc[nop] = new_reg;
1447 return true;
1450 /* Put constant into memory when we have mixed modes. It generates
1451 a better code in most cases as it does not need a secondary
1452 reload memory. It also prevents LRA looping when LRA is using
1453 secondary reload memory again and again. */
1454 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1455 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1457 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1458 alter_subreg (curr_id->operand_loc[nop], false);
1459 return true;
1461 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1462 if there may be a problem accessing OPERAND in the outer
1463 mode. */
1464 if ((REG_P (reg)
1465 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1466 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1467 /* Don't reload paradoxical subregs because we could be looping
1468 having repeatedly final regno out of hard regs range. */
1469 && (hard_regno_nregs[hard_regno][innermode]
1470 >= hard_regno_nregs[hard_regno][mode])
1471 && simplify_subreg_regno (hard_regno, innermode,
1472 SUBREG_BYTE (operand), mode) < 0
1473 /* Don't reload subreg for matching reload. It is actually
1474 valid subreg in LRA. */
1475 && ! LRA_SUBREG_P (operand))
1476 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1478 enum reg_class rclass;
1480 if (REG_P (reg))
1481 /* There is a big probability that we will get the same class
1482 for the new pseudo and we will get the same insn which
1483 means infinite looping. So spill the new pseudo. */
1484 rclass = NO_REGS;
1485 else
1486 /* The class will be defined later in curr_insn_transform. */
1487 rclass
1488 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1490 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1491 rclass, TRUE, "subreg reg", &new_reg))
1493 bool insert_before, insert_after;
1494 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1496 insert_before = (type != OP_OUT
1497 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1498 insert_after = (type != OP_IN);
1499 insert_move_for_subreg (insert_before ? &before : NULL,
1500 insert_after ? &after : NULL,
1501 reg, new_reg);
1503 SUBREG_REG (operand) = new_reg;
1504 lra_process_new_insns (curr_insn, before, after,
1505 "Inserting subreg reload");
1506 return true;
1508 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1509 IRA allocates hardreg to the inner pseudo reg according to its mode
1510 instead of the outermode, so the size of the hardreg may not be enough
1511 to contain the outermode operand, in that case we may need to insert
1512 reload for the reg. For the following two types of paradoxical subreg,
1513 we need to insert reload:
1514 1. If the op_type is OP_IN, and the hardreg could not be paired with
1515 other hardreg to contain the outermode operand
1516 (checked by in_hard_reg_set_p), we need to insert the reload.
1517 2. If the op_type is OP_OUT or OP_INOUT.
1519 Here is a paradoxical subreg example showing how the reload is generated:
1521 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1522 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1524 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1525 here, if reg107 is assigned to hardreg R15, because R15 is the last
1526 hardreg, compiler cannot find another hardreg to pair with R15 to
1527 contain TImode data. So we insert a TImode reload reg180 for it.
1528 After reload is inserted:
1530 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1531 (reg:DI 107 [ __comp ])) -1
1532 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1533 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1535 Two reload hard registers will be allocated to reg180 to save TImode data
1536 in LRA_assign. */
1537 else if (REG_P (reg)
1538 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1539 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1540 && (hard_regno_nregs[hard_regno][innermode]
1541 < hard_regno_nregs[hard_regno][mode])
1542 && (regclass = lra_get_allocno_class (REGNO (reg)))
1543 && (type != OP_IN
1544 || !in_hard_reg_set_p (reg_class_contents[regclass],
1545 mode, hard_regno)))
1547 /* The class will be defined later in curr_insn_transform. */
1548 enum reg_class rclass
1549 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1551 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1552 rclass, TRUE, "paradoxical subreg", &new_reg))
1554 rtx subreg;
1555 bool insert_before, insert_after;
1557 PUT_MODE (new_reg, mode);
1558 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1559 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1561 insert_before = (type != OP_OUT);
1562 insert_after = (type != OP_IN);
1563 insert_move_for_subreg (insert_before ? &before : NULL,
1564 insert_after ? &after : NULL,
1565 reg, subreg);
1567 SUBREG_REG (operand) = new_reg;
1568 lra_process_new_insns (curr_insn, before, after,
1569 "Inserting paradoxical subreg reload");
1570 return true;
1572 return false;
1575 /* Return TRUE if X refers for a hard register from SET. */
1576 static bool
1577 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1579 int i, j, x_hard_regno;
1580 machine_mode mode;
1581 const char *fmt;
1582 enum rtx_code code;
1584 if (x == NULL_RTX)
1585 return false;
1586 code = GET_CODE (x);
1587 mode = GET_MODE (x);
1588 if (code == SUBREG)
1590 x = SUBREG_REG (x);
1591 code = GET_CODE (x);
1592 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1593 mode = GET_MODE (x);
1596 if (REG_P (x))
1598 x_hard_regno = get_hard_regno (x);
1599 return (x_hard_regno >= 0
1600 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1602 if (MEM_P (x))
1604 struct address_info ad;
1606 decompose_mem_address (&ad, x);
1607 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1608 return true;
1609 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1610 return true;
1612 fmt = GET_RTX_FORMAT (code);
1613 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1615 if (fmt[i] == 'e')
1617 if (uses_hard_regs_p (XEXP (x, i), set))
1618 return true;
1620 else if (fmt[i] == 'E')
1622 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1623 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1624 return true;
1627 return false;
1630 /* Return true if OP is a spilled pseudo. */
1631 static inline bool
1632 spilled_pseudo_p (rtx op)
1634 return (REG_P (op)
1635 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1638 /* Return true if X is a general constant. */
1639 static inline bool
1640 general_constant_p (rtx x)
1642 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1645 static bool
1646 reg_in_class_p (rtx reg, enum reg_class cl)
1648 if (cl == NO_REGS)
1649 return get_reg_class (REGNO (reg)) == NO_REGS;
1650 return in_class_p (reg, cl, NULL);
1653 /* Return true if SET of RCLASS contains no hard regs which can be
1654 used in MODE. */
1655 static bool
1656 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1657 HARD_REG_SET &set,
1658 enum machine_mode mode)
1660 HARD_REG_SET temp;
1662 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1663 COPY_HARD_REG_SET (temp, set);
1664 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1665 return (hard_reg_set_subset_p
1666 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1669 /* Major function to choose the current insn alternative and what
1670 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1671 negative we should consider only this alternative. Return false if
1672 we can not choose the alternative or find how to reload the
1673 operands. */
1674 static bool
1675 process_alt_operands (int only_alternative)
1677 bool ok_p = false;
1678 int nop, overall, nalt;
1679 int n_alternatives = curr_static_id->n_alternatives;
1680 int n_operands = curr_static_id->n_operands;
1681 /* LOSERS counts the operands that don't fit this alternative and
1682 would require loading. */
1683 int losers;
1684 /* REJECT is a count of how undesirable this alternative says it is
1685 if any reloading is required. If the alternative matches exactly
1686 then REJECT is ignored, but otherwise it gets this much counted
1687 against it in addition to the reloading needed. */
1688 int reject;
1689 int op_reject;
1690 /* The number of elements in the following array. */
1691 int early_clobbered_regs_num;
1692 /* Numbers of operands which are early clobber registers. */
1693 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1694 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1695 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1696 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1697 bool curr_alt_win[MAX_RECOG_OPERANDS];
1698 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1699 int curr_alt_matches[MAX_RECOG_OPERANDS];
1700 /* The number of elements in the following array. */
1701 int curr_alt_dont_inherit_ops_num;
1702 /* Numbers of operands whose reload pseudos should not be inherited. */
1703 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1704 rtx op;
1705 /* The register when the operand is a subreg of register, otherwise the
1706 operand itself. */
1707 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1708 /* The register if the operand is a register or subreg of register,
1709 otherwise NULL. */
1710 rtx operand_reg[MAX_RECOG_OPERANDS];
1711 int hard_regno[MAX_RECOG_OPERANDS];
1712 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1713 int reload_nregs, reload_sum;
1714 bool costly_p;
1715 enum reg_class cl;
1717 /* Calculate some data common for all alternatives to speed up the
1718 function. */
1719 for (nop = 0; nop < n_operands; nop++)
1721 rtx reg;
1723 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1724 /* The real hard regno of the operand after the allocation. */
1725 hard_regno[nop] = get_hard_regno (op);
1727 operand_reg[nop] = reg = op;
1728 biggest_mode[nop] = GET_MODE (op);
1729 if (GET_CODE (op) == SUBREG)
1731 operand_reg[nop] = reg = SUBREG_REG (op);
1732 if (GET_MODE_SIZE (biggest_mode[nop])
1733 < GET_MODE_SIZE (GET_MODE (reg)))
1734 biggest_mode[nop] = GET_MODE (reg);
1736 if (! REG_P (reg))
1737 operand_reg[nop] = NULL_RTX;
1738 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1739 || ((int) REGNO (reg)
1740 == lra_get_elimination_hard_regno (REGNO (reg))))
1741 no_subreg_reg_operand[nop] = reg;
1742 else
1743 operand_reg[nop] = no_subreg_reg_operand[nop]
1744 /* Just use natural mode for elimination result. It should
1745 be enough for extra constraints hooks. */
1746 = regno_reg_rtx[hard_regno[nop]];
1749 /* The constraints are made of several alternatives. Each operand's
1750 constraint looks like foo,bar,... with commas separating the
1751 alternatives. The first alternatives for all operands go
1752 together, the second alternatives go together, etc.
1754 First loop over alternatives. */
1755 alternative_mask preferred = curr_id->preferred_alternatives;
1756 if (only_alternative >= 0)
1757 preferred &= ALTERNATIVE_BIT (only_alternative);
1759 for (nalt = 0; nalt < n_alternatives; nalt++)
1761 /* Loop over operands for one constraint alternative. */
1762 if (!TEST_BIT (preferred, nalt))
1763 continue;
1765 overall = losers = reject = reload_nregs = reload_sum = 0;
1766 for (nop = 0; nop < n_operands; nop++)
1768 int inc = (curr_static_id
1769 ->operand_alternative[nalt * n_operands + nop].reject);
1770 if (lra_dump_file != NULL && inc != 0)
1771 fprintf (lra_dump_file,
1772 " Staticly defined alt reject+=%d\n", inc);
1773 reject += inc;
1775 early_clobbered_regs_num = 0;
1777 for (nop = 0; nop < n_operands; nop++)
1779 const char *p;
1780 char *end;
1781 int len, c, m, i, opalt_num, this_alternative_matches;
1782 bool win, did_match, offmemok, early_clobber_p;
1783 /* false => this operand can be reloaded somehow for this
1784 alternative. */
1785 bool badop;
1786 /* true => this operand can be reloaded if the alternative
1787 allows regs. */
1788 bool winreg;
1789 /* True if a constant forced into memory would be OK for
1790 this operand. */
1791 bool constmemok;
1792 enum reg_class this_alternative, this_costly_alternative;
1793 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1794 bool this_alternative_match_win, this_alternative_win;
1795 bool this_alternative_offmemok;
1796 bool scratch_p;
1797 machine_mode mode;
1798 enum constraint_num cn;
1800 opalt_num = nalt * n_operands + nop;
1801 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1803 /* Fast track for no constraints at all. */
1804 curr_alt[nop] = NO_REGS;
1805 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1806 curr_alt_win[nop] = true;
1807 curr_alt_match_win[nop] = false;
1808 curr_alt_offmemok[nop] = false;
1809 curr_alt_matches[nop] = -1;
1810 continue;
1813 op = no_subreg_reg_operand[nop];
1814 mode = curr_operand_mode[nop];
1816 win = did_match = winreg = offmemok = constmemok = false;
1817 badop = true;
1819 early_clobber_p = false;
1820 p = curr_static_id->operand_alternative[opalt_num].constraint;
1822 this_costly_alternative = this_alternative = NO_REGS;
1823 /* We update set of possible hard regs besides its class
1824 because reg class might be inaccurate. For example,
1825 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1826 is translated in HI_REGS because classes are merged by
1827 pairs and there is no accurate intermediate class. */
1828 CLEAR_HARD_REG_SET (this_alternative_set);
1829 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1830 this_alternative_win = false;
1831 this_alternative_match_win = false;
1832 this_alternative_offmemok = false;
1833 this_alternative_matches = -1;
1835 /* An empty constraint should be excluded by the fast
1836 track. */
1837 lra_assert (*p != 0 && *p != ',');
1839 op_reject = 0;
1840 /* Scan this alternative's specs for this operand; set WIN
1841 if the operand fits any letter in this alternative.
1842 Otherwise, clear BADOP if this operand could fit some
1843 letter after reloads, or set WINREG if this operand could
1844 fit after reloads provided the constraint allows some
1845 registers. */
1846 costly_p = false;
1849 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1851 case '\0':
1852 len = 0;
1853 break;
1854 case ',':
1855 c = '\0';
1856 break;
1858 case '&':
1859 early_clobber_p = true;
1860 break;
1862 case '$':
1863 op_reject += LRA_MAX_REJECT;
1864 break;
1865 case '^':
1866 op_reject += LRA_LOSER_COST_FACTOR;
1867 break;
1869 case '#':
1870 /* Ignore rest of this alternative. */
1871 c = '\0';
1872 break;
1874 case '0': case '1': case '2': case '3': case '4':
1875 case '5': case '6': case '7': case '8': case '9':
1877 int m_hregno;
1878 bool match_p;
1880 m = strtoul (p, &end, 10);
1881 p = end;
1882 len = 0;
1883 lra_assert (nop > m);
1885 this_alternative_matches = m;
1886 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1887 /* We are supposed to match a previous operand.
1888 If we do, we win if that one did. If we do
1889 not, count both of the operands as losers.
1890 (This is too conservative, since most of the
1891 time only a single reload insn will be needed
1892 to make the two operands win. As a result,
1893 this alternative may be rejected when it is
1894 actually desirable.) */
1895 match_p = false;
1896 if (operands_match_p (*curr_id->operand_loc[nop],
1897 *curr_id->operand_loc[m], m_hregno))
1899 /* We should reject matching of an early
1900 clobber operand if the matching operand is
1901 not dying in the insn. */
1902 if (! curr_static_id->operand[m].early_clobber
1903 || operand_reg[nop] == NULL_RTX
1904 || (find_regno_note (curr_insn, REG_DEAD,
1905 REGNO (op))
1906 || REGNO (op) == REGNO (operand_reg[m])))
1907 match_p = true;
1909 if (match_p)
1911 /* If we are matching a non-offsettable
1912 address where an offsettable address was
1913 expected, then we must reject this
1914 combination, because we can't reload
1915 it. */
1916 if (curr_alt_offmemok[m]
1917 && MEM_P (*curr_id->operand_loc[m])
1918 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1919 continue;
1921 else
1923 /* Operands don't match. Both operands must
1924 allow a reload register, otherwise we
1925 cannot make them match. */
1926 if (curr_alt[m] == NO_REGS)
1927 break;
1928 /* Retroactively mark the operand we had to
1929 match as a loser, if it wasn't already and
1930 it wasn't matched to a register constraint
1931 (e.g it might be matched by memory). */
1932 if (curr_alt_win[m]
1933 && (operand_reg[m] == NULL_RTX
1934 || hard_regno[m] < 0))
1936 losers++;
1937 reload_nregs
1938 += (ira_reg_class_max_nregs[curr_alt[m]]
1939 [GET_MODE (*curr_id->operand_loc[m])]);
1942 /* Prefer matching earlyclobber alternative as
1943 it results in less hard regs required for
1944 the insn than a non-matching earlyclobber
1945 alternative. */
1946 if (curr_static_id->operand[m].early_clobber)
1948 if (lra_dump_file != NULL)
1949 fprintf
1950 (lra_dump_file,
1951 " %d Matching earlyclobber alt:"
1952 " reject--\n",
1953 nop);
1954 reject--;
1956 /* Otherwise we prefer no matching
1957 alternatives because it gives more freedom
1958 in RA. */
1959 else if (operand_reg[nop] == NULL_RTX
1960 || (find_regno_note (curr_insn, REG_DEAD,
1961 REGNO (operand_reg[nop]))
1962 == NULL_RTX))
1964 if (lra_dump_file != NULL)
1965 fprintf
1966 (lra_dump_file,
1967 " %d Matching alt: reject+=2\n",
1968 nop);
1969 reject += 2;
1972 /* If we have to reload this operand and some
1973 previous operand also had to match the same
1974 thing as this operand, we don't know how to do
1975 that. */
1976 if (!match_p || !curr_alt_win[m])
1978 for (i = 0; i < nop; i++)
1979 if (curr_alt_matches[i] == m)
1980 break;
1981 if (i < nop)
1982 break;
1984 else
1985 did_match = true;
1987 /* This can be fixed with reloads if the operand
1988 we are supposed to match can be fixed with
1989 reloads. */
1990 badop = false;
1991 this_alternative = curr_alt[m];
1992 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1993 winreg = this_alternative != NO_REGS;
1994 break;
1997 case 'g':
1998 if (MEM_P (op)
1999 || general_constant_p (op)
2000 || spilled_pseudo_p (op))
2001 win = true;
2002 cl = GENERAL_REGS;
2003 goto reg;
2005 default:
2006 cn = lookup_constraint (p);
2007 switch (get_constraint_type (cn))
2009 case CT_REGISTER:
2010 cl = reg_class_for_constraint (cn);
2011 if (cl != NO_REGS)
2012 goto reg;
2013 break;
2015 case CT_CONST_INT:
2016 if (CONST_INT_P (op)
2017 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2018 win = true;
2019 break;
2021 case CT_MEMORY:
2022 if (MEM_P (op)
2023 && satisfies_memory_constraint_p (op, cn))
2024 win = true;
2025 else if (spilled_pseudo_p (op))
2026 win = true;
2028 /* If we didn't already win, we can reload constants
2029 via force_const_mem or put the pseudo value into
2030 memory, or make other memory by reloading the
2031 address like for 'o'. */
2032 if (CONST_POOL_OK_P (mode, op)
2033 || MEM_P (op) || REG_P (op))
2034 badop = false;
2035 constmemok = true;
2036 offmemok = true;
2037 break;
2039 case CT_ADDRESS:
2040 /* If we didn't already win, we can reload the address
2041 into a base register. */
2042 if (satisfies_address_constraint_p (op, cn))
2043 win = true;
2044 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2045 ADDRESS, SCRATCH);
2046 badop = false;
2047 goto reg;
2049 case CT_FIXED_FORM:
2050 if (constraint_satisfied_p (op, cn))
2051 win = true;
2052 break;
2054 break;
2056 reg:
2057 this_alternative = reg_class_subunion[this_alternative][cl];
2058 IOR_HARD_REG_SET (this_alternative_set,
2059 reg_class_contents[cl]);
2060 if (costly_p)
2062 this_costly_alternative
2063 = reg_class_subunion[this_costly_alternative][cl];
2064 IOR_HARD_REG_SET (this_costly_alternative_set,
2065 reg_class_contents[cl]);
2067 if (mode == BLKmode)
2068 break;
2069 winreg = true;
2070 if (REG_P (op))
2072 if (hard_regno[nop] >= 0
2073 && in_hard_reg_set_p (this_alternative_set,
2074 mode, hard_regno[nop]))
2075 win = true;
2076 else if (hard_regno[nop] < 0
2077 && in_class_p (op, this_alternative, NULL))
2078 win = true;
2080 break;
2082 if (c != ' ' && c != '\t')
2083 costly_p = c == '*';
2085 while ((p += len), c);
2087 scratch_p = (operand_reg[nop] != NULL_RTX
2088 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2089 /* Record which operands fit this alternative. */
2090 if (win)
2092 this_alternative_win = true;
2093 if (operand_reg[nop] != NULL_RTX)
2095 if (hard_regno[nop] >= 0)
2097 if (in_hard_reg_set_p (this_costly_alternative_set,
2098 mode, hard_regno[nop]))
2100 if (lra_dump_file != NULL)
2101 fprintf (lra_dump_file,
2102 " %d Costly set: reject++\n",
2103 nop);
2104 reject++;
2107 else
2109 /* Prefer won reg to spilled pseudo under other
2110 equal conditions for possibe inheritance. */
2111 if (! scratch_p)
2113 if (lra_dump_file != NULL)
2114 fprintf
2115 (lra_dump_file,
2116 " %d Non pseudo reload: reject++\n",
2117 nop);
2118 reject++;
2120 if (in_class_p (operand_reg[nop],
2121 this_costly_alternative, NULL))
2123 if (lra_dump_file != NULL)
2124 fprintf
2125 (lra_dump_file,
2126 " %d Non pseudo costly reload:"
2127 " reject++\n",
2128 nop);
2129 reject++;
2132 /* We simulate the behaviour of old reload here.
2133 Although scratches need hard registers and it
2134 might result in spilling other pseudos, no reload
2135 insns are generated for the scratches. So it
2136 might cost something but probably less than old
2137 reload pass believes. */
2138 if (scratch_p)
2140 if (lra_dump_file != NULL)
2141 fprintf (lra_dump_file,
2142 " %d Scratch win: reject+=2\n",
2143 nop);
2144 reject += 2;
2148 else if (did_match)
2149 this_alternative_match_win = true;
2150 else
2152 int const_to_mem = 0;
2153 bool no_regs_p;
2155 reject += op_reject;
2156 /* Never do output reload of stack pointer. It makes
2157 impossible to do elimination when SP is changed in
2158 RTL. */
2159 if (op == stack_pointer_rtx && ! frame_pointer_needed
2160 && curr_static_id->operand[nop].type != OP_IN)
2161 goto fail;
2163 /* If this alternative asks for a specific reg class, see if there
2164 is at least one allocatable register in that class. */
2165 no_regs_p
2166 = (this_alternative == NO_REGS
2167 || (hard_reg_set_subset_p
2168 (reg_class_contents[this_alternative],
2169 lra_no_alloc_regs)));
2171 /* For asms, verify that the class for this alternative is possible
2172 for the mode that is specified. */
2173 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2175 int i;
2176 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2177 if (HARD_REGNO_MODE_OK (i, mode)
2178 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2179 mode, i))
2180 break;
2181 if (i == FIRST_PSEUDO_REGISTER)
2182 winreg = false;
2185 /* If this operand accepts a register, and if the
2186 register class has at least one allocatable register,
2187 then this operand can be reloaded. */
2188 if (winreg && !no_regs_p)
2189 badop = false;
2191 if (badop)
2193 if (lra_dump_file != NULL)
2194 fprintf (lra_dump_file,
2195 " alt=%d: Bad operand -- refuse\n",
2196 nalt);
2197 goto fail;
2200 /* If not assigned pseudo has a class which a subset of
2201 required reg class, it is a less costly alternative
2202 as the pseudo still can get a hard reg of necessary
2203 class. */
2204 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2205 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2206 && ira_class_subset_p[this_alternative][cl])
2208 if (lra_dump_file != NULL)
2209 fprintf
2210 (lra_dump_file,
2211 " %d Super set class reg: reject-=3\n", nop);
2212 reject -= 3;
2215 this_alternative_offmemok = offmemok;
2216 if (this_costly_alternative != NO_REGS)
2218 if (lra_dump_file != NULL)
2219 fprintf (lra_dump_file,
2220 " %d Costly loser: reject++\n", nop);
2221 reject++;
2223 /* If the operand is dying, has a matching constraint,
2224 and satisfies constraints of the matched operand
2225 which failed to satisfy the own constraints, most probably
2226 the reload for this operand will be gone. */
2227 if (this_alternative_matches >= 0
2228 && !curr_alt_win[this_alternative_matches]
2229 && REG_P (op)
2230 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2231 && (hard_regno[nop] >= 0
2232 ? in_hard_reg_set_p (this_alternative_set,
2233 mode, hard_regno[nop])
2234 : in_class_p (op, this_alternative, NULL)))
2236 if (lra_dump_file != NULL)
2237 fprintf
2238 (lra_dump_file,
2239 " %d Dying matched operand reload: reject++\n",
2240 nop);
2241 reject++;
2243 else
2245 /* Strict_low_part requires to reload the register
2246 not the sub-register. In this case we should
2247 check that a final reload hard reg can hold the
2248 value mode. */
2249 if (curr_static_id->operand[nop].strict_low
2250 && REG_P (op)
2251 && hard_regno[nop] < 0
2252 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2253 && ira_class_hard_regs_num[this_alternative] > 0
2254 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2255 [this_alternative][0],
2256 GET_MODE
2257 (*curr_id->operand_loc[nop])))
2259 if (lra_dump_file != NULL)
2260 fprintf
2261 (lra_dump_file,
2262 " alt=%d: Strict low subreg reload -- refuse\n",
2263 nalt);
2264 goto fail;
2266 losers++;
2268 if (operand_reg[nop] != NULL_RTX
2269 /* Output operands and matched input operands are
2270 not inherited. The following conditions do not
2271 exactly describe the previous statement but they
2272 are pretty close. */
2273 && curr_static_id->operand[nop].type != OP_OUT
2274 && (this_alternative_matches < 0
2275 || curr_static_id->operand[nop].type != OP_IN))
2277 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2278 (operand_reg[nop])]
2279 .last_reload);
2281 /* The value of reload_sum has sense only if we
2282 process insns in their order. It happens only on
2283 the first constraints sub-pass when we do most of
2284 reload work. */
2285 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2286 reload_sum += last_reload - bb_reload_num;
2288 /* If this is a constant that is reloaded into the
2289 desired class by copying it to memory first, count
2290 that as another reload. This is consistent with
2291 other code and is required to avoid choosing another
2292 alternative when the constant is moved into memory.
2293 Note that the test here is precisely the same as in
2294 the code below that calls force_const_mem. */
2295 if (CONST_POOL_OK_P (mode, op)
2296 && ((targetm.preferred_reload_class
2297 (op, this_alternative) == NO_REGS)
2298 || no_input_reloads_p))
2300 const_to_mem = 1;
2301 if (! no_regs_p)
2302 losers++;
2305 /* Alternative loses if it requires a type of reload not
2306 permitted for this insn. We can always reload
2307 objects with a REG_UNUSED note. */
2308 if ((curr_static_id->operand[nop].type != OP_IN
2309 && no_output_reloads_p
2310 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2311 || (curr_static_id->operand[nop].type != OP_OUT
2312 && no_input_reloads_p && ! const_to_mem)
2313 || (this_alternative_matches >= 0
2314 && (no_input_reloads_p
2315 || (no_output_reloads_p
2316 && (curr_static_id->operand
2317 [this_alternative_matches].type != OP_IN)
2318 && ! find_reg_note (curr_insn, REG_UNUSED,
2319 no_subreg_reg_operand
2320 [this_alternative_matches])))))
2322 if (lra_dump_file != NULL)
2323 fprintf
2324 (lra_dump_file,
2325 " alt=%d: No input/otput reload -- refuse\n",
2326 nalt);
2327 goto fail;
2330 /* Alternative loses if it required class pseudo can not
2331 hold value of required mode. Such insns can be
2332 described by insn definitions with mode iterators. */
2333 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2334 && ! hard_reg_set_empty_p (this_alternative_set)
2335 /* It is common practice for constraints to use a
2336 class which does not have actually enough regs to
2337 hold the value (e.g. x86 AREG for mode requiring
2338 more one general reg). Therefore we have 2
2339 conditions to check that the reload pseudo can
2340 not hold the mode value. */
2341 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2342 [this_alternative][0],
2343 GET_MODE (*curr_id->operand_loc[nop]))
2344 /* The above condition is not enough as the first
2345 reg in ira_class_hard_regs can be not aligned for
2346 multi-words mode values. */
2347 && (prohibited_class_reg_set_mode_p
2348 (this_alternative, this_alternative_set,
2349 GET_MODE (*curr_id->operand_loc[nop]))))
2351 if (lra_dump_file != NULL)
2352 fprintf (lra_dump_file,
2353 " alt=%d: reload pseudo for op %d "
2354 " can not hold the mode value -- refuse\n",
2355 nalt, nop);
2356 goto fail;
2359 /* Check strong discouragement of reload of non-constant
2360 into class THIS_ALTERNATIVE. */
2361 if (! CONSTANT_P (op) && ! no_regs_p
2362 && (targetm.preferred_reload_class
2363 (op, this_alternative) == NO_REGS
2364 || (curr_static_id->operand[nop].type == OP_OUT
2365 && (targetm.preferred_output_reload_class
2366 (op, this_alternative) == NO_REGS))))
2368 if (lra_dump_file != NULL)
2369 fprintf (lra_dump_file,
2370 " %d Non-prefered reload: reject+=%d\n",
2371 nop, LRA_MAX_REJECT);
2372 reject += LRA_MAX_REJECT;
2375 if (! (MEM_P (op) && offmemok)
2376 && ! (const_to_mem && constmemok))
2378 /* We prefer to reload pseudos over reloading other
2379 things, since such reloads may be able to be
2380 eliminated later. So bump REJECT in other cases.
2381 Don't do this in the case where we are forcing a
2382 constant into memory and it will then win since
2383 we don't want to have a different alternative
2384 match then. */
2385 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2387 if (lra_dump_file != NULL)
2388 fprintf
2389 (lra_dump_file,
2390 " %d Non-pseudo reload: reject+=2\n",
2391 nop);
2392 reject += 2;
2395 if (! no_regs_p)
2396 reload_nregs
2397 += ira_reg_class_max_nregs[this_alternative][mode];
2399 if (SMALL_REGISTER_CLASS_P (this_alternative))
2401 if (lra_dump_file != NULL)
2402 fprintf
2403 (lra_dump_file,
2404 " %d Small class reload: reject+=%d\n",
2405 nop, LRA_LOSER_COST_FACTOR / 2);
2406 reject += LRA_LOSER_COST_FACTOR / 2;
2410 /* We are trying to spill pseudo into memory. It is
2411 usually more costly than moving to a hard register
2412 although it might takes the same number of
2413 reloads. */
2414 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2416 if (lra_dump_file != NULL)
2417 fprintf
2418 (lra_dump_file,
2419 " %d Spill pseudo into memory: reject+=3\n",
2420 nop);
2421 reject += 3;
2422 if (VECTOR_MODE_P (mode))
2424 /* Spilling vectors into memory is usually more
2425 costly as they contain big values. */
2426 if (lra_dump_file != NULL)
2427 fprintf
2428 (lra_dump_file,
2429 " %d Spill vector pseudo: reject+=2\n",
2430 nop);
2431 reject += 2;
2435 #ifdef SECONDARY_MEMORY_NEEDED
2436 /* If reload requires moving value through secondary
2437 memory, it will need one more insn at least. */
2438 if (this_alternative != NO_REGS
2439 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2440 && ((curr_static_id->operand[nop].type != OP_OUT
2441 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2442 GET_MODE (op)))
2443 || (curr_static_id->operand[nop].type != OP_IN
2444 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2445 GET_MODE (op)))))
2446 losers++;
2447 #endif
2448 /* Input reloads can be inherited more often than output
2449 reloads can be removed, so penalize output
2450 reloads. */
2451 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2453 if (lra_dump_file != NULL)
2454 fprintf
2455 (lra_dump_file,
2456 " %d Non input pseudo reload: reject++\n",
2457 nop);
2458 reject++;
2462 if (early_clobber_p && ! scratch_p)
2464 if (lra_dump_file != NULL)
2465 fprintf (lra_dump_file,
2466 " %d Early clobber: reject++\n", nop);
2467 reject++;
2469 /* ??? We check early clobbers after processing all operands
2470 (see loop below) and there we update the costs more.
2471 Should we update the cost (may be approximately) here
2472 because of early clobber register reloads or it is a rare
2473 or non-important thing to be worth to do it. */
2474 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2475 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2477 if (lra_dump_file != NULL)
2478 fprintf (lra_dump_file,
2479 " alt=%d,overall=%d,losers=%d -- refuse\n",
2480 nalt, overall, losers);
2481 goto fail;
2484 curr_alt[nop] = this_alternative;
2485 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2486 curr_alt_win[nop] = this_alternative_win;
2487 curr_alt_match_win[nop] = this_alternative_match_win;
2488 curr_alt_offmemok[nop] = this_alternative_offmemok;
2489 curr_alt_matches[nop] = this_alternative_matches;
2491 if (this_alternative_matches >= 0
2492 && !did_match && !this_alternative_win)
2493 curr_alt_win[this_alternative_matches] = false;
2495 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2496 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2498 if (curr_insn_set != NULL_RTX && n_operands == 2
2499 /* Prevent processing non-move insns. */
2500 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2501 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2502 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2503 && REG_P (no_subreg_reg_operand[0])
2504 && REG_P (no_subreg_reg_operand[1])
2505 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2506 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2507 || (! curr_alt_win[0] && curr_alt_win[1]
2508 && REG_P (no_subreg_reg_operand[1])
2509 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2510 || (curr_alt_win[0] && ! curr_alt_win[1]
2511 && REG_P (no_subreg_reg_operand[0])
2512 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2513 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2514 no_subreg_reg_operand[1])
2515 || (targetm.preferred_reload_class
2516 (no_subreg_reg_operand[1],
2517 (enum reg_class) curr_alt[1]) != NO_REGS))
2518 /* If it is a result of recent elimination in move
2519 insn we can transform it into an add still by
2520 using this alternative. */
2521 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2523 /* We have a move insn and a new reload insn will be similar
2524 to the current insn. We should avoid such situation as it
2525 results in LRA cycling. */
2526 overall += LRA_MAX_REJECT;
2528 ok_p = true;
2529 curr_alt_dont_inherit_ops_num = 0;
2530 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2532 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2533 HARD_REG_SET temp_set;
2535 i = early_clobbered_nops[nop];
2536 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2537 || hard_regno[i] < 0)
2538 continue;
2539 lra_assert (operand_reg[i] != NULL_RTX);
2540 clobbered_hard_regno = hard_regno[i];
2541 CLEAR_HARD_REG_SET (temp_set);
2542 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2543 first_conflict_j = last_conflict_j = -1;
2544 for (j = 0; j < n_operands; j++)
2545 if (j == i
2546 /* We don't want process insides of match_operator and
2547 match_parallel because otherwise we would process
2548 their operands once again generating a wrong
2549 code. */
2550 || curr_static_id->operand[j].is_operator)
2551 continue;
2552 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2553 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2554 continue;
2555 /* If we don't reload j-th operand, check conflicts. */
2556 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2557 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2559 if (first_conflict_j < 0)
2560 first_conflict_j = j;
2561 last_conflict_j = j;
2563 if (last_conflict_j < 0)
2564 continue;
2565 /* If earlyclobber operand conflicts with another
2566 non-matching operand which is actually the same register
2567 as the earlyclobber operand, it is better to reload the
2568 another operand as an operand matching the earlyclobber
2569 operand can be also the same. */
2570 if (first_conflict_j == last_conflict_j
2571 && operand_reg[last_conflict_j]
2572 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2573 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2575 curr_alt_win[last_conflict_j] = false;
2576 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2577 = last_conflict_j;
2578 losers++;
2579 /* Early clobber was already reflected in REJECT. */
2580 lra_assert (reject > 0);
2581 if (lra_dump_file != NULL)
2582 fprintf
2583 (lra_dump_file,
2584 " %d Conflict early clobber reload: reject--\n",
2586 reject--;
2587 overall += LRA_LOSER_COST_FACTOR - 1;
2589 else
2591 /* We need to reload early clobbered register and the
2592 matched registers. */
2593 for (j = 0; j < n_operands; j++)
2594 if (curr_alt_matches[j] == i)
2596 curr_alt_match_win[j] = false;
2597 losers++;
2598 overall += LRA_LOSER_COST_FACTOR;
2600 if (! curr_alt_match_win[i])
2601 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2602 else
2604 /* Remember pseudos used for match reloads are never
2605 inherited. */
2606 lra_assert (curr_alt_matches[i] >= 0);
2607 curr_alt_win[curr_alt_matches[i]] = false;
2609 curr_alt_win[i] = curr_alt_match_win[i] = false;
2610 losers++;
2611 /* Early clobber was already reflected in REJECT. */
2612 lra_assert (reject > 0);
2613 if (lra_dump_file != NULL)
2614 fprintf
2615 (lra_dump_file,
2616 " %d Matched conflict early clobber reloads:"
2617 "reject--\n",
2619 reject--;
2620 overall += LRA_LOSER_COST_FACTOR - 1;
2623 if (lra_dump_file != NULL)
2624 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2625 nalt, overall, losers, reload_nregs);
2627 /* If this alternative can be made to work by reloading, and it
2628 needs less reloading than the others checked so far, record
2629 it as the chosen goal for reloading. */
2630 if ((best_losers != 0 && losers == 0)
2631 || (((best_losers == 0 && losers == 0)
2632 || (best_losers != 0 && losers != 0))
2633 && (best_overall > overall
2634 || (best_overall == overall
2635 /* If the cost of the reloads is the same,
2636 prefer alternative which requires minimal
2637 number of reload regs. */
2638 && (reload_nregs < best_reload_nregs
2639 || (reload_nregs == best_reload_nregs
2640 && (best_reload_sum < reload_sum
2641 || (best_reload_sum == reload_sum
2642 && nalt < goal_alt_number))))))))
2644 for (nop = 0; nop < n_operands; nop++)
2646 goal_alt_win[nop] = curr_alt_win[nop];
2647 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2648 goal_alt_matches[nop] = curr_alt_matches[nop];
2649 goal_alt[nop] = curr_alt[nop];
2650 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2652 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2653 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2654 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2655 goal_alt_swapped = curr_swapped;
2656 best_overall = overall;
2657 best_losers = losers;
2658 best_reload_nregs = reload_nregs;
2659 best_reload_sum = reload_sum;
2660 goal_alt_number = nalt;
2662 if (losers == 0)
2663 /* Everything is satisfied. Do not process alternatives
2664 anymore. */
2665 break;
2666 fail:
2669 return ok_p;
2672 /* Make reload base reg from address AD. */
2673 static rtx
2674 base_to_reg (struct address_info *ad)
2676 enum reg_class cl;
2677 int code = -1;
2678 rtx new_inner = NULL_RTX;
2679 rtx new_reg = NULL_RTX;
2680 rtx_insn *insn;
2681 rtx_insn *last_insn = get_last_insn();
2683 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2684 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2685 get_index_code (ad));
2686 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2687 cl, "base");
2688 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2689 ad->disp_term == NULL
2690 ? gen_int_mode (0, ad->mode)
2691 : *ad->disp_term);
2692 if (!valid_address_p (ad->mode, new_inner, ad->as))
2693 return NULL_RTX;
2694 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base_term));
2695 code = recog_memoized (insn);
2696 if (code < 0)
2698 delete_insns_since (last_insn);
2699 return NULL_RTX;
2702 return new_inner;
2705 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2706 static rtx
2707 base_plus_disp_to_reg (struct address_info *ad)
2709 enum reg_class cl;
2710 rtx new_reg;
2712 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2713 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2714 get_index_code (ad));
2715 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2716 cl, "base + disp");
2717 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2718 return new_reg;
2721 /* Make reload of index part of address AD. Return the new
2722 pseudo. */
2723 static rtx
2724 index_part_to_reg (struct address_info *ad)
2726 rtx new_reg;
2728 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2729 INDEX_REG_CLASS, "index term");
2730 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2731 GEN_INT (get_index_scale (ad)), new_reg, 1);
2732 return new_reg;
2735 /* Return true if we can add a displacement to address AD, even if that
2736 makes the address invalid. The fix-up code requires any new address
2737 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2738 static bool
2739 can_add_disp_p (struct address_info *ad)
2741 return (!ad->autoinc_p
2742 && ad->segment == NULL
2743 && ad->base == ad->base_term
2744 && ad->disp == ad->disp_term);
2747 /* Make equiv substitution in address AD. Return true if a substitution
2748 was made. */
2749 static bool
2750 equiv_address_substitution (struct address_info *ad)
2752 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2753 HOST_WIDE_INT disp, scale;
2754 bool change_p;
2756 base_term = strip_subreg (ad->base_term);
2757 if (base_term == NULL)
2758 base_reg = new_base_reg = NULL_RTX;
2759 else
2761 base_reg = *base_term;
2762 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2764 index_term = strip_subreg (ad->index_term);
2765 if (index_term == NULL)
2766 index_reg = new_index_reg = NULL_RTX;
2767 else
2769 index_reg = *index_term;
2770 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2772 if (base_reg == new_base_reg && index_reg == new_index_reg)
2773 return false;
2774 disp = 0;
2775 change_p = false;
2776 if (lra_dump_file != NULL)
2778 fprintf (lra_dump_file, "Changing address in insn %d ",
2779 INSN_UID (curr_insn));
2780 dump_value_slim (lra_dump_file, *ad->outer, 1);
2782 if (base_reg != new_base_reg)
2784 if (REG_P (new_base_reg))
2786 *base_term = new_base_reg;
2787 change_p = true;
2789 else if (GET_CODE (new_base_reg) == PLUS
2790 && REG_P (XEXP (new_base_reg, 0))
2791 && CONST_INT_P (XEXP (new_base_reg, 1))
2792 && can_add_disp_p (ad))
2794 disp += INTVAL (XEXP (new_base_reg, 1));
2795 *base_term = XEXP (new_base_reg, 0);
2796 change_p = true;
2798 if (ad->base_term2 != NULL)
2799 *ad->base_term2 = *ad->base_term;
2801 if (index_reg != new_index_reg)
2803 if (REG_P (new_index_reg))
2805 *index_term = new_index_reg;
2806 change_p = true;
2808 else if (GET_CODE (new_index_reg) == PLUS
2809 && REG_P (XEXP (new_index_reg, 0))
2810 && CONST_INT_P (XEXP (new_index_reg, 1))
2811 && can_add_disp_p (ad)
2812 && (scale = get_index_scale (ad)))
2814 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2815 *index_term = XEXP (new_index_reg, 0);
2816 change_p = true;
2819 if (disp != 0)
2821 if (ad->disp != NULL)
2822 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2823 else
2825 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2826 update_address (ad);
2828 change_p = true;
2830 if (lra_dump_file != NULL)
2832 if (! change_p)
2833 fprintf (lra_dump_file, " -- no change\n");
2834 else
2836 fprintf (lra_dump_file, " on equiv ");
2837 dump_value_slim (lra_dump_file, *ad->outer, 1);
2838 fprintf (lra_dump_file, "\n");
2841 return change_p;
2844 /* Major function to make reloads for an address in operand NOP or
2845 check its correctness (If CHECK_ONLY_P is true). The supported
2846 cases are:
2848 1) an address that existed before LRA started, at which point it
2849 must have been valid. These addresses are subject to elimination
2850 and may have become invalid due to the elimination offset being out
2851 of range.
2853 2) an address created by forcing a constant to memory
2854 (force_const_to_mem). The initial form of these addresses might
2855 not be valid, and it is this function's job to make them valid.
2857 3) a frame address formed from a register and a (possibly zero)
2858 constant offset. As above, these addresses might not be valid and
2859 this function must make them so.
2861 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2862 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2863 address. Return true for any RTL change.
2865 The function is a helper function which does not produce all
2866 transformations (when CHECK_ONLY_P is false) which can be
2867 necessary. It does just basic steps. To do all necessary
2868 transformations use function process_address. */
2869 static bool
2870 process_address_1 (int nop, bool check_only_p,
2871 rtx_insn **before, rtx_insn **after)
2873 struct address_info ad;
2874 rtx new_reg;
2875 rtx op = *curr_id->operand_loc[nop];
2876 const char *constraint = curr_static_id->operand[nop].constraint;
2877 enum constraint_num cn = lookup_constraint (constraint);
2878 bool change_p = false;
2880 if (insn_extra_address_constraint (cn))
2881 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2882 else if (MEM_P (op))
2883 decompose_mem_address (&ad, op);
2884 else if (GET_CODE (op) == SUBREG
2885 && MEM_P (SUBREG_REG (op)))
2886 decompose_mem_address (&ad, SUBREG_REG (op));
2887 else
2888 return false;
2889 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
2890 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
2891 when INDEX_REG_CLASS is a single register class. */
2892 if (ad.base_term != NULL
2893 && ad.index_term != NULL
2894 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
2895 && REG_P (*ad.base_term)
2896 && REG_P (*ad.index_term)
2897 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
2898 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
2900 std::swap (ad.base, ad.index);
2901 std::swap (ad.base_term, ad.index_term);
2903 if (! check_only_p)
2904 change_p = equiv_address_substitution (&ad);
2905 if (ad.base_term != NULL
2906 && (process_addr_reg
2907 (ad.base_term, check_only_p, before,
2908 (ad.autoinc_p
2909 && !(REG_P (*ad.base_term)
2910 && find_regno_note (curr_insn, REG_DEAD,
2911 REGNO (*ad.base_term)) != NULL_RTX)
2912 ? after : NULL),
2913 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2914 get_index_code (&ad)))))
2916 change_p = true;
2917 if (ad.base_term2 != NULL)
2918 *ad.base_term2 = *ad.base_term;
2920 if (ad.index_term != NULL
2921 && process_addr_reg (ad.index_term, check_only_p,
2922 before, NULL, INDEX_REG_CLASS))
2923 change_p = true;
2925 /* Target hooks sometimes don't treat extra-constraint addresses as
2926 legitimate address_operands, so handle them specially. */
2927 if (insn_extra_address_constraint (cn)
2928 && satisfies_address_constraint_p (&ad, cn))
2929 return change_p;
2931 if (check_only_p)
2932 return change_p;
2934 /* There are three cases where the shape of *AD.INNER may now be invalid:
2936 1) the original address was valid, but either elimination or
2937 equiv_address_substitution was applied and that made
2938 the address invalid.
2940 2) the address is an invalid symbolic address created by
2941 force_const_to_mem.
2943 3) the address is a frame address with an invalid offset.
2945 4) the address is a frame address with an invalid base.
2947 All these cases involve a non-autoinc address, so there is no
2948 point revalidating other types. */
2949 if (ad.autoinc_p || valid_address_p (&ad))
2950 return change_p;
2952 /* Any index existed before LRA started, so we can assume that the
2953 presence and shape of the index is valid. */
2954 push_to_sequence (*before);
2955 lra_assert (ad.disp == ad.disp_term);
2956 if (ad.base == NULL)
2958 if (ad.index == NULL)
2960 int code = -1;
2961 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2962 SCRATCH, SCRATCH);
2963 rtx addr = *ad.inner;
2965 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2966 if (HAVE_lo_sum)
2968 rtx_insn *insn;
2969 rtx_insn *last = get_last_insn ();
2971 /* addr => lo_sum (new_base, addr), case (2) above. */
2972 insn = emit_insn (gen_rtx_SET
2973 (new_reg,
2974 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2975 code = recog_memoized (insn);
2976 if (code >= 0)
2978 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2979 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2981 /* Try to put lo_sum into register. */
2982 insn = emit_insn (gen_rtx_SET
2983 (new_reg,
2984 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2985 code = recog_memoized (insn);
2986 if (code >= 0)
2988 *ad.inner = new_reg;
2989 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2991 *ad.inner = addr;
2992 code = -1;
2998 if (code < 0)
2999 delete_insns_since (last);
3002 if (code < 0)
3004 /* addr => new_base, case (2) above. */
3005 lra_emit_move (new_reg, addr);
3006 *ad.inner = new_reg;
3009 else
3011 /* index * scale + disp => new base + index * scale,
3012 case (1) above. */
3013 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3014 GET_CODE (*ad.index));
3016 lra_assert (INDEX_REG_CLASS != NO_REGS);
3017 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3018 lra_emit_move (new_reg, *ad.disp);
3019 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3020 new_reg, *ad.index);
3023 else if (ad.index == NULL)
3025 int regno;
3026 enum reg_class cl;
3027 rtx set;
3028 rtx_insn *insns, *last_insn;
3029 /* Try to reload base into register only if the base is invalid
3030 for the address but with valid offset, case (4) above. */
3031 start_sequence ();
3032 new_reg = base_to_reg (&ad);
3034 /* base + disp => new base, cases (1) and (3) above. */
3035 /* Another option would be to reload the displacement into an
3036 index register. However, postreload has code to optimize
3037 address reloads that have the same base and different
3038 displacements, so reloading into an index register would
3039 not necessarily be a win. */
3040 if (new_reg == NULL_RTX)
3041 new_reg = base_plus_disp_to_reg (&ad);
3042 insns = get_insns ();
3043 last_insn = get_last_insn ();
3044 /* If we generated at least two insns, try last insn source as
3045 an address. If we succeed, we generate one less insn. */
3046 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3047 && GET_CODE (SET_SRC (set)) == PLUS
3048 && REG_P (XEXP (SET_SRC (set), 0))
3049 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3051 *ad.inner = SET_SRC (set);
3052 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3054 *ad.base_term = XEXP (SET_SRC (set), 0);
3055 *ad.disp_term = XEXP (SET_SRC (set), 1);
3056 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3057 get_index_code (&ad));
3058 regno = REGNO (*ad.base_term);
3059 if (regno >= FIRST_PSEUDO_REGISTER
3060 && cl != lra_get_allocno_class (regno))
3061 lra_change_class (regno, cl, " Change to", true);
3062 new_reg = SET_SRC (set);
3063 delete_insns_since (PREV_INSN (last_insn));
3066 /* Try if target can split displacement into legitimite new disp
3067 and offset. If it's the case, we replace the last insn with
3068 insns for base + offset => new_reg and set new_reg + new disp
3069 to *ad.inner. */
3070 last_insn = get_last_insn ();
3071 if ((set = single_set (last_insn)) != NULL_RTX
3072 && GET_CODE (SET_SRC (set)) == PLUS
3073 && REG_P (XEXP (SET_SRC (set), 0))
3074 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3075 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3077 rtx addend, disp = XEXP (SET_SRC (set), 1);
3078 if (targetm.legitimize_address_displacement (&disp, &addend,
3079 ad.mode))
3081 rtx_insn *new_insns;
3082 start_sequence ();
3083 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3084 new_insns = get_insns ();
3085 end_sequence ();
3086 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3087 delete_insns_since (PREV_INSN (last_insn));
3088 add_insn (new_insns);
3089 insns = get_insns ();
3092 end_sequence ();
3093 emit_insn (insns);
3094 *ad.inner = new_reg;
3096 else if (ad.disp_term != NULL)
3098 /* base + scale * index + disp => new base + scale * index,
3099 case (1) above. */
3100 new_reg = base_plus_disp_to_reg (&ad);
3101 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3102 new_reg, *ad.index);
3104 else if (get_index_scale (&ad) == 1)
3106 /* The last transformation to one reg will be made in
3107 curr_insn_transform function. */
3108 end_sequence ();
3109 return false;
3111 else
3113 /* base + scale * index => base + new_reg,
3114 case (1) above.
3115 Index part of address may become invalid. For example, we
3116 changed pseudo on the equivalent memory and a subreg of the
3117 pseudo onto the memory of different mode for which the scale is
3118 prohibitted. */
3119 new_reg = index_part_to_reg (&ad);
3120 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3121 *ad.base_term, new_reg);
3123 *before = get_insns ();
3124 end_sequence ();
3125 return true;
3128 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3129 Use process_address_1 as a helper function. Return true for any
3130 RTL changes.
3132 If CHECK_ONLY_P is true, just check address correctness. Return
3133 false if the address correct. */
3134 static bool
3135 process_address (int nop, bool check_only_p,
3136 rtx_insn **before, rtx_insn **after)
3138 bool res = false;
3140 while (process_address_1 (nop, check_only_p, before, after))
3142 if (check_only_p)
3143 return true;
3144 res = true;
3146 return res;
3149 /* Emit insns to reload VALUE into a new register. VALUE is an
3150 auto-increment or auto-decrement RTX whose operand is a register or
3151 memory location; so reloading involves incrementing that location.
3152 IN is either identical to VALUE, or some cheaper place to reload
3153 value being incremented/decremented from.
3155 INC_AMOUNT is the number to increment or decrement by (always
3156 positive and ignored for POST_MODIFY/PRE_MODIFY).
3158 Return pseudo containing the result. */
3159 static rtx
3160 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3162 /* REG or MEM to be copied and incremented. */
3163 rtx incloc = XEXP (value, 0);
3164 /* Nonzero if increment after copying. */
3165 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3166 || GET_CODE (value) == POST_MODIFY);
3167 rtx_insn *last;
3168 rtx inc;
3169 rtx_insn *add_insn;
3170 int code;
3171 rtx real_in = in == value ? incloc : in;
3172 rtx result;
3173 bool plus_p = true;
3175 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3177 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3178 || GET_CODE (XEXP (value, 1)) == MINUS);
3179 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3180 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3181 inc = XEXP (XEXP (value, 1), 1);
3183 else
3185 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3186 inc_amount = -inc_amount;
3188 inc = GEN_INT (inc_amount);
3191 if (! post && REG_P (incloc))
3192 result = incloc;
3193 else
3194 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3195 "INC/DEC result");
3197 if (real_in != result)
3199 /* First copy the location to the result register. */
3200 lra_assert (REG_P (result));
3201 emit_insn (gen_move_insn (result, real_in));
3204 /* We suppose that there are insns to add/sub with the constant
3205 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3206 old reload worked with this assumption. If the assumption
3207 becomes wrong, we should use approach in function
3208 base_plus_disp_to_reg. */
3209 if (in == value)
3211 /* See if we can directly increment INCLOC. */
3212 last = get_last_insn ();
3213 add_insn = emit_insn (plus_p
3214 ? gen_add2_insn (incloc, inc)
3215 : gen_sub2_insn (incloc, inc));
3217 code = recog_memoized (add_insn);
3218 if (code >= 0)
3220 if (! post && result != incloc)
3221 emit_insn (gen_move_insn (result, incloc));
3222 return result;
3224 delete_insns_since (last);
3227 /* If couldn't do the increment directly, must increment in RESULT.
3228 The way we do this depends on whether this is pre- or
3229 post-increment. For pre-increment, copy INCLOC to the reload
3230 register, increment it there, then save back. */
3231 if (! post)
3233 if (real_in != result)
3234 emit_insn (gen_move_insn (result, real_in));
3235 if (plus_p)
3236 emit_insn (gen_add2_insn (result, inc));
3237 else
3238 emit_insn (gen_sub2_insn (result, inc));
3239 if (result != incloc)
3240 emit_insn (gen_move_insn (incloc, result));
3242 else
3244 /* Post-increment.
3246 Because this might be a jump insn or a compare, and because
3247 RESULT may not be available after the insn in an input
3248 reload, we must do the incrementing before the insn being
3249 reloaded for.
3251 We have already copied IN to RESULT. Increment the copy in
3252 RESULT, save that back, then decrement RESULT so it has
3253 the original value. */
3254 if (plus_p)
3255 emit_insn (gen_add2_insn (result, inc));
3256 else
3257 emit_insn (gen_sub2_insn (result, inc));
3258 emit_insn (gen_move_insn (incloc, result));
3259 /* Restore non-modified value for the result. We prefer this
3260 way because it does not require an additional hard
3261 register. */
3262 if (plus_p)
3264 if (CONST_INT_P (inc))
3265 emit_insn (gen_add2_insn (result,
3266 gen_int_mode (-INTVAL (inc),
3267 GET_MODE (result))));
3268 else
3269 emit_insn (gen_sub2_insn (result, inc));
3271 else
3272 emit_insn (gen_add2_insn (result, inc));
3274 return result;
3277 /* Return true if the current move insn does not need processing as we
3278 already know that it satisfies its constraints. */
3279 static bool
3280 simple_move_p (void)
3282 rtx dest, src;
3283 enum reg_class dclass, sclass;
3285 lra_assert (curr_insn_set != NULL_RTX);
3286 dest = SET_DEST (curr_insn_set);
3287 src = SET_SRC (curr_insn_set);
3288 return ((dclass = get_op_class (dest)) != NO_REGS
3289 && (sclass = get_op_class (src)) != NO_REGS
3290 /* The backend guarantees that register moves of cost 2
3291 never need reloads. */
3292 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3295 /* Swap operands NOP and NOP + 1. */
3296 static inline void
3297 swap_operands (int nop)
3299 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3300 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3301 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3302 /* Swap the duplicates too. */
3303 lra_update_dup (curr_id, nop);
3304 lra_update_dup (curr_id, nop + 1);
3307 /* Main entry point of the constraint code: search the body of the
3308 current insn to choose the best alternative. It is mimicking insn
3309 alternative cost calculation model of former reload pass. That is
3310 because machine descriptions were written to use this model. This
3311 model can be changed in future. Make commutative operand exchange
3312 if it is chosen.
3314 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3315 constraints. Return true if any change happened during function
3316 call.
3318 If CHECK_ONLY_P is true then don't do any transformation. Just
3319 check that the insn satisfies all constraints. If the insn does
3320 not satisfy any constraint, return true. */
3321 static bool
3322 curr_insn_transform (bool check_only_p)
3324 int i, j, k;
3325 int n_operands;
3326 int n_alternatives;
3327 int commutative;
3328 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3329 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3330 rtx_insn *before, *after;
3331 bool alt_p = false;
3332 /* Flag that the insn has been changed through a transformation. */
3333 bool change_p;
3334 bool sec_mem_p;
3335 #ifdef SECONDARY_MEMORY_NEEDED
3336 bool use_sec_mem_p;
3337 #endif
3338 int max_regno_before;
3339 int reused_alternative_num;
3341 curr_insn_set = single_set (curr_insn);
3342 if (curr_insn_set != NULL_RTX && simple_move_p ())
3343 return false;
3345 no_input_reloads_p = no_output_reloads_p = false;
3346 goal_alt_number = -1;
3347 change_p = sec_mem_p = false;
3348 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3349 reloads; neither are insns that SET cc0. Insns that use CC0 are
3350 not allowed to have any input reloads. */
3351 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3352 no_output_reloads_p = true;
3354 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3355 no_input_reloads_p = true;
3356 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3357 no_output_reloads_p = true;
3359 n_operands = curr_static_id->n_operands;
3360 n_alternatives = curr_static_id->n_alternatives;
3362 /* Just return "no reloads" if insn has no operands with
3363 constraints. */
3364 if (n_operands == 0 || n_alternatives == 0)
3365 return false;
3367 max_regno_before = max_reg_num ();
3369 for (i = 0; i < n_operands; i++)
3371 goal_alt_matched[i][0] = -1;
3372 goal_alt_matches[i] = -1;
3375 commutative = curr_static_id->commutative;
3377 /* Now see what we need for pseudos that didn't get hard regs or got
3378 the wrong kind of hard reg. For this, we must consider all the
3379 operands together against the register constraints. */
3381 best_losers = best_overall = INT_MAX;
3382 best_reload_sum = 0;
3384 curr_swapped = false;
3385 goal_alt_swapped = false;
3387 if (! check_only_p)
3388 /* Make equivalence substitution and memory subreg elimination
3389 before address processing because an address legitimacy can
3390 depend on memory mode. */
3391 for (i = 0; i < n_operands; i++)
3393 rtx op = *curr_id->operand_loc[i];
3394 rtx subst, old = op;
3395 bool op_change_p = false;
3397 if (GET_CODE (old) == SUBREG)
3398 old = SUBREG_REG (old);
3399 subst = get_equiv_with_elimination (old, curr_insn);
3400 original_subreg_reg_mode[i] = VOIDmode;
3401 if (subst != old)
3403 subst = copy_rtx (subst);
3404 lra_assert (REG_P (old));
3405 if (GET_CODE (op) != SUBREG)
3406 *curr_id->operand_loc[i] = subst;
3407 else
3409 SUBREG_REG (op) = subst;
3410 if (GET_MODE (subst) == VOIDmode)
3411 original_subreg_reg_mode[i] = GET_MODE (old);
3413 if (lra_dump_file != NULL)
3415 fprintf (lra_dump_file,
3416 "Changing pseudo %d in operand %i of insn %u on equiv ",
3417 REGNO (old), i, INSN_UID (curr_insn));
3418 dump_value_slim (lra_dump_file, subst, 1);
3419 fprintf (lra_dump_file, "\n");
3421 op_change_p = change_p = true;
3423 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3425 change_p = true;
3426 lra_update_dup (curr_id, i);
3430 /* Reload address registers and displacements. We do it before
3431 finding an alternative because of memory constraints. */
3432 before = after = NULL;
3433 for (i = 0; i < n_operands; i++)
3434 if (! curr_static_id->operand[i].is_operator
3435 && process_address (i, check_only_p, &before, &after))
3437 if (check_only_p)
3438 return true;
3439 change_p = true;
3440 lra_update_dup (curr_id, i);
3443 if (change_p)
3444 /* If we've changed the instruction then any alternative that
3445 we chose previously may no longer be valid. */
3446 lra_set_used_insn_alternative (curr_insn, -1);
3448 if (! check_only_p && curr_insn_set != NULL_RTX
3449 && check_and_process_move (&change_p, &sec_mem_p))
3450 return change_p;
3452 try_swapped:
3454 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3455 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3456 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3457 reused_alternative_num, INSN_UID (curr_insn));
3459 if (process_alt_operands (reused_alternative_num))
3460 alt_p = true;
3462 if (check_only_p)
3463 return ! alt_p || best_losers != 0;
3465 /* If insn is commutative (it's safe to exchange a certain pair of
3466 operands) then we need to try each alternative twice, the second
3467 time matching those two operands as if we had exchanged them. To
3468 do this, really exchange them in operands.
3470 If we have just tried the alternatives the second time, return
3471 operands to normal and drop through. */
3473 if (reused_alternative_num < 0 && commutative >= 0)
3475 curr_swapped = !curr_swapped;
3476 if (curr_swapped)
3478 swap_operands (commutative);
3479 goto try_swapped;
3481 else
3482 swap_operands (commutative);
3485 if (! alt_p && ! sec_mem_p)
3487 /* No alternative works with reloads?? */
3488 if (INSN_CODE (curr_insn) >= 0)
3489 fatal_insn ("unable to generate reloads for:", curr_insn);
3490 error_for_asm (curr_insn,
3491 "inconsistent operand constraints in an %<asm%>");
3492 /* Avoid further trouble with this insn. */
3493 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3494 lra_invalidate_insn_data (curr_insn);
3495 return true;
3498 /* If the best alternative is with operands 1 and 2 swapped, swap
3499 them. Update the operand numbers of any reloads already
3500 pushed. */
3502 if (goal_alt_swapped)
3504 if (lra_dump_file != NULL)
3505 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3506 INSN_UID (curr_insn));
3508 /* Swap the duplicates too. */
3509 swap_operands (commutative);
3510 change_p = true;
3513 #ifdef SECONDARY_MEMORY_NEEDED
3514 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3515 too conservatively. So we use the secondary memory only if there
3516 is no any alternative without reloads. */
3517 use_sec_mem_p = false;
3518 if (! alt_p)
3519 use_sec_mem_p = true;
3520 else if (sec_mem_p)
3522 for (i = 0; i < n_operands; i++)
3523 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3524 break;
3525 use_sec_mem_p = i < n_operands;
3528 if (use_sec_mem_p)
3530 rtx new_reg, src, dest, rld;
3531 machine_mode sec_mode, rld_mode;
3533 lra_assert (sec_mem_p);
3534 lra_assert (curr_static_id->operand[0].type == OP_OUT
3535 && curr_static_id->operand[1].type == OP_IN);
3536 dest = *curr_id->operand_loc[0];
3537 src = *curr_id->operand_loc[1];
3538 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3539 ? dest : src);
3540 rld_mode = GET_MODE (rld);
3541 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3542 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3543 #else
3544 sec_mode = rld_mode;
3545 #endif
3546 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3547 NO_REGS, "secondary");
3548 /* If the mode is changed, it should be wider. */
3549 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3550 if (sec_mode != rld_mode)
3552 /* If the target says specifically to use another mode for
3553 secondary memory moves we can not reuse the original
3554 insn. */
3555 after = emit_spill_move (false, new_reg, dest);
3556 lra_process_new_insns (curr_insn, NULL, after,
3557 "Inserting the sec. move");
3558 /* We may have non null BEFORE here (e.g. after address
3559 processing. */
3560 push_to_sequence (before);
3561 before = emit_spill_move (true, new_reg, src);
3562 emit_insn (before);
3563 before = get_insns ();
3564 end_sequence ();
3565 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3566 lra_set_insn_deleted (curr_insn);
3568 else if (dest == rld)
3570 *curr_id->operand_loc[0] = new_reg;
3571 after = emit_spill_move (false, new_reg, dest);
3572 lra_process_new_insns (curr_insn, NULL, after,
3573 "Inserting the sec. move");
3575 else
3577 *curr_id->operand_loc[1] = new_reg;
3578 /* See comments above. */
3579 push_to_sequence (before);
3580 before = emit_spill_move (true, new_reg, src);
3581 emit_insn (before);
3582 before = get_insns ();
3583 end_sequence ();
3584 lra_process_new_insns (curr_insn, before, NULL,
3585 "Inserting the sec. move");
3587 lra_update_insn_regno_info (curr_insn);
3588 return true;
3590 #endif
3592 lra_assert (goal_alt_number >= 0);
3593 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3595 if (lra_dump_file != NULL)
3597 const char *p;
3599 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3600 goal_alt_number, INSN_UID (curr_insn));
3601 for (i = 0; i < n_operands; i++)
3603 p = (curr_static_id->operand_alternative
3604 [goal_alt_number * n_operands + i].constraint);
3605 if (*p == '\0')
3606 continue;
3607 fprintf (lra_dump_file, " (%d) ", i);
3608 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3609 fputc (*p, lra_dump_file);
3611 if (INSN_CODE (curr_insn) >= 0
3612 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3613 fprintf (lra_dump_file, " {%s}", p);
3614 if (curr_id->sp_offset != 0)
3615 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3616 curr_id->sp_offset);
3617 fprintf (lra_dump_file, "\n");
3620 /* Right now, for any pair of operands I and J that are required to
3621 match, with J < I, goal_alt_matches[I] is J. Add I to
3622 goal_alt_matched[J]. */
3624 for (i = 0; i < n_operands; i++)
3625 if ((j = goal_alt_matches[i]) >= 0)
3627 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3629 /* We allow matching one output operand and several input
3630 operands. */
3631 lra_assert (k == 0
3632 || (curr_static_id->operand[j].type == OP_OUT
3633 && curr_static_id->operand[i].type == OP_IN
3634 && (curr_static_id->operand
3635 [goal_alt_matched[j][0]].type == OP_IN)));
3636 goal_alt_matched[j][k] = i;
3637 goal_alt_matched[j][k + 1] = -1;
3640 for (i = 0; i < n_operands; i++)
3641 goal_alt_win[i] |= goal_alt_match_win[i];
3643 /* Any constants that aren't allowed and can't be reloaded into
3644 registers are here changed into memory references. */
3645 for (i = 0; i < n_operands; i++)
3646 if (goal_alt_win[i])
3648 int regno;
3649 enum reg_class new_class;
3650 rtx reg = *curr_id->operand_loc[i];
3652 if (GET_CODE (reg) == SUBREG)
3653 reg = SUBREG_REG (reg);
3655 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3657 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3659 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3661 lra_assert (ok_p);
3662 lra_change_class (regno, new_class, " Change to", true);
3666 else
3668 const char *constraint;
3669 char c;
3670 rtx op = *curr_id->operand_loc[i];
3671 rtx subreg = NULL_RTX;
3672 machine_mode mode = curr_operand_mode[i];
3674 if (GET_CODE (op) == SUBREG)
3676 subreg = op;
3677 op = SUBREG_REG (op);
3678 mode = GET_MODE (op);
3681 if (CONST_POOL_OK_P (mode, op)
3682 && ((targetm.preferred_reload_class
3683 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3684 || no_input_reloads_p))
3686 rtx tem = force_const_mem (mode, op);
3688 change_p = true;
3689 if (subreg != NULL_RTX)
3690 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3692 *curr_id->operand_loc[i] = tem;
3693 lra_update_dup (curr_id, i);
3694 process_address (i, false, &before, &after);
3696 /* If the alternative accepts constant pool refs directly
3697 there will be no reload needed at all. */
3698 if (subreg != NULL_RTX)
3699 continue;
3700 /* Skip alternatives before the one requested. */
3701 constraint = (curr_static_id->operand_alternative
3702 [goal_alt_number * n_operands + i].constraint);
3703 for (;
3704 (c = *constraint) && c != ',' && c != '#';
3705 constraint += CONSTRAINT_LEN (c, constraint))
3707 enum constraint_num cn = lookup_constraint (constraint);
3708 if (insn_extra_memory_constraint (cn)
3709 && satisfies_memory_constraint_p (tem, cn))
3710 break;
3712 if (c == '\0' || c == ',' || c == '#')
3713 continue;
3715 goal_alt_win[i] = true;
3719 for (i = 0; i < n_operands; i++)
3721 int regno;
3722 bool optional_p = false;
3723 rtx old, new_reg;
3724 rtx op = *curr_id->operand_loc[i];
3726 if (goal_alt_win[i])
3728 if (goal_alt[i] == NO_REGS
3729 && REG_P (op)
3730 /* When we assign NO_REGS it means that we will not
3731 assign a hard register to the scratch pseudo by
3732 assigment pass and the scratch pseudo will be
3733 spilled. Spilled scratch pseudos are transformed
3734 back to scratches at the LRA end. */
3735 && lra_former_scratch_operand_p (curr_insn, i))
3737 int regno = REGNO (op);
3738 lra_change_class (regno, NO_REGS, " Change to", true);
3739 if (lra_get_regno_hard_regno (regno) >= 0)
3740 /* We don't have to mark all insn affected by the
3741 spilled pseudo as there is only one such insn, the
3742 current one. */
3743 reg_renumber[regno] = -1;
3745 /* We can do an optional reload. If the pseudo got a hard
3746 reg, we might improve the code through inheritance. If
3747 it does not get a hard register we coalesce memory/memory
3748 moves later. Ignore move insns to avoid cycling. */
3749 if (! lra_simple_p
3750 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3751 && goal_alt[i] != NO_REGS && REG_P (op)
3752 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3753 && regno < new_regno_start
3754 && ! lra_former_scratch_p (regno)
3755 && reg_renumber[regno] < 0
3756 /* Check that the optional reload pseudo will be able to
3757 hold given mode value. */
3758 && ! (prohibited_class_reg_set_mode_p
3759 (goal_alt[i], reg_class_contents[goal_alt[i]],
3760 PSEUDO_REGNO_MODE (regno)))
3761 && (curr_insn_set == NULL_RTX
3762 || !((REG_P (SET_SRC (curr_insn_set))
3763 || MEM_P (SET_SRC (curr_insn_set))
3764 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3765 && (REG_P (SET_DEST (curr_insn_set))
3766 || MEM_P (SET_DEST (curr_insn_set))
3767 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3768 optional_p = true;
3769 else
3770 continue;
3773 /* Operands that match previous ones have already been handled. */
3774 if (goal_alt_matches[i] >= 0)
3775 continue;
3777 /* We should not have an operand with a non-offsettable address
3778 appearing where an offsettable address will do. It also may
3779 be a case when the address should be special in other words
3780 not a general one (e.g. it needs no index reg). */
3781 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3783 enum reg_class rclass;
3784 rtx *loc = &XEXP (op, 0);
3785 enum rtx_code code = GET_CODE (*loc);
3787 push_to_sequence (before);
3788 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3789 MEM, SCRATCH);
3790 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3791 new_reg = emit_inc (rclass, *loc, *loc,
3792 /* This value does not matter for MODIFY. */
3793 GET_MODE_SIZE (GET_MODE (op)));
3794 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3795 "offsetable address", &new_reg))
3796 lra_emit_move (new_reg, *loc);
3797 before = get_insns ();
3798 end_sequence ();
3799 *loc = new_reg;
3800 lra_update_dup (curr_id, i);
3802 else if (goal_alt_matched[i][0] == -1)
3804 machine_mode mode;
3805 rtx reg, *loc;
3806 int hard_regno, byte;
3807 enum op_type type = curr_static_id->operand[i].type;
3809 loc = curr_id->operand_loc[i];
3810 mode = curr_operand_mode[i];
3811 if (GET_CODE (*loc) == SUBREG)
3813 reg = SUBREG_REG (*loc);
3814 byte = SUBREG_BYTE (*loc);
3815 if (REG_P (reg)
3816 /* Strict_low_part requires reload the register not
3817 the sub-register. */
3818 && (curr_static_id->operand[i].strict_low
3819 || (GET_MODE_SIZE (mode)
3820 <= GET_MODE_SIZE (GET_MODE (reg))
3821 && (hard_regno
3822 = get_try_hard_regno (REGNO (reg))) >= 0
3823 && (simplify_subreg_regno
3824 (hard_regno,
3825 GET_MODE (reg), byte, mode) < 0)
3826 && (goal_alt[i] == NO_REGS
3827 || (simplify_subreg_regno
3828 (ira_class_hard_regs[goal_alt[i]][0],
3829 GET_MODE (reg), byte, mode) >= 0)))))
3831 if (type == OP_OUT)
3832 type = OP_INOUT;
3833 loc = &SUBREG_REG (*loc);
3834 mode = GET_MODE (*loc);
3837 old = *loc;
3838 if (get_reload_reg (type, mode, old, goal_alt[i],
3839 loc != curr_id->operand_loc[i], "", &new_reg)
3840 && type != OP_OUT)
3842 push_to_sequence (before);
3843 lra_emit_move (new_reg, old);
3844 before = get_insns ();
3845 end_sequence ();
3847 *loc = new_reg;
3848 if (type != OP_IN
3849 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3851 start_sequence ();
3852 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3853 emit_insn (after);
3854 after = get_insns ();
3855 end_sequence ();
3856 *loc = new_reg;
3858 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3859 if (goal_alt_dont_inherit_ops[j] == i)
3861 lra_set_regno_unique_value (REGNO (new_reg));
3862 break;
3864 lra_update_dup (curr_id, i);
3866 else if (curr_static_id->operand[i].type == OP_IN
3867 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3868 == OP_OUT))
3870 /* generate reloads for input and matched outputs. */
3871 match_inputs[0] = i;
3872 match_inputs[1] = -1;
3873 match_reload (goal_alt_matched[i][0], match_inputs,
3874 goal_alt[i], &before, &after,
3875 curr_static_id->operand_alternative
3876 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
3877 .earlyclobber);
3879 else if (curr_static_id->operand[i].type == OP_OUT
3880 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3881 == OP_IN))
3882 /* Generate reloads for output and matched inputs. */
3883 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after,
3884 curr_static_id->operand_alternative
3885 [goal_alt_number * n_operands + i].earlyclobber);
3886 else if (curr_static_id->operand[i].type == OP_IN
3887 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3888 == OP_IN))
3890 /* Generate reloads for matched inputs. */
3891 match_inputs[0] = i;
3892 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3893 match_inputs[j + 1] = k;
3894 match_inputs[j + 1] = -1;
3895 match_reload (-1, match_inputs, goal_alt[i], &before, &after, false);
3897 else
3898 /* We must generate code in any case when function
3899 process_alt_operands decides that it is possible. */
3900 gcc_unreachable ();
3901 if (optional_p)
3903 lra_assert (REG_P (op));
3904 regno = REGNO (op);
3905 op = *curr_id->operand_loc[i]; /* Substitution. */
3906 if (GET_CODE (op) == SUBREG)
3907 op = SUBREG_REG (op);
3908 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3909 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3910 lra_reg_info[REGNO (op)].restore_regno = regno;
3911 if (lra_dump_file != NULL)
3912 fprintf (lra_dump_file,
3913 " Making reload reg %d for reg %d optional\n",
3914 REGNO (op), regno);
3917 if (before != NULL_RTX || after != NULL_RTX
3918 || max_regno_before != max_reg_num ())
3919 change_p = true;
3920 if (change_p)
3922 lra_update_operator_dups (curr_id);
3923 /* Something changes -- process the insn. */
3924 lra_update_insn_regno_info (curr_insn);
3926 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3927 return change_p;
3930 /* Return true if INSN satisfies all constraints. In other words, no
3931 reload insns are needed. */
3932 bool
3933 lra_constrain_insn (rtx_insn *insn)
3935 int saved_new_regno_start = new_regno_start;
3936 int saved_new_insn_uid_start = new_insn_uid_start;
3937 bool change_p;
3939 curr_insn = insn;
3940 curr_id = lra_get_insn_recog_data (curr_insn);
3941 curr_static_id = curr_id->insn_static_data;
3942 new_insn_uid_start = get_max_uid ();
3943 new_regno_start = max_reg_num ();
3944 change_p = curr_insn_transform (true);
3945 new_regno_start = saved_new_regno_start;
3946 new_insn_uid_start = saved_new_insn_uid_start;
3947 return ! change_p;
3950 /* Return true if X is in LIST. */
3951 static bool
3952 in_list_p (rtx x, rtx list)
3954 for (; list != NULL_RTX; list = XEXP (list, 1))
3955 if (XEXP (list, 0) == x)
3956 return true;
3957 return false;
3960 /* Return true if X contains an allocatable hard register (if
3961 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3962 static bool
3963 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3965 int i, j;
3966 const char *fmt;
3967 enum rtx_code code;
3969 code = GET_CODE (x);
3970 if (REG_P (x))
3972 int regno = REGNO (x);
3973 HARD_REG_SET alloc_regs;
3975 if (hard_reg_p)
3977 if (regno >= FIRST_PSEUDO_REGISTER)
3978 regno = lra_get_regno_hard_regno (regno);
3979 if (regno < 0)
3980 return false;
3981 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3982 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3984 else
3986 if (regno < FIRST_PSEUDO_REGISTER)
3987 return false;
3988 if (! spilled_p)
3989 return true;
3990 return lra_get_regno_hard_regno (regno) < 0;
3993 fmt = GET_RTX_FORMAT (code);
3994 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3996 if (fmt[i] == 'e')
3998 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3999 return true;
4001 else if (fmt[i] == 'E')
4003 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4004 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4005 return true;
4008 return false;
4011 /* Return true if X contains a symbol reg. */
4012 static bool
4013 contains_symbol_ref_p (rtx x)
4015 int i, j;
4016 const char *fmt;
4017 enum rtx_code code;
4019 code = GET_CODE (x);
4020 if (code == SYMBOL_REF)
4021 return true;
4022 fmt = GET_RTX_FORMAT (code);
4023 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4025 if (fmt[i] == 'e')
4027 if (contains_symbol_ref_p (XEXP (x, i)))
4028 return true;
4030 else if (fmt[i] == 'E')
4032 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4033 if (contains_symbol_ref_p (XVECEXP (x, i, j)))
4034 return true;
4037 return false;
4040 /* Process all regs in location *LOC and change them on equivalent
4041 substitution. Return true if any change was done. */
4042 static bool
4043 loc_equivalence_change_p (rtx *loc)
4045 rtx subst, reg, x = *loc;
4046 bool result = false;
4047 enum rtx_code code = GET_CODE (x);
4048 const char *fmt;
4049 int i, j;
4051 if (code == SUBREG)
4053 reg = SUBREG_REG (x);
4054 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4055 && GET_MODE (subst) == VOIDmode)
4057 /* We cannot reload debug location. Simplify subreg here
4058 while we know the inner mode. */
4059 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4060 GET_MODE (reg), SUBREG_BYTE (x));
4061 return true;
4064 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4066 *loc = subst;
4067 return true;
4070 /* Scan all the operand sub-expressions. */
4071 fmt = GET_RTX_FORMAT (code);
4072 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4074 if (fmt[i] == 'e')
4075 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4076 else if (fmt[i] == 'E')
4077 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4078 result
4079 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4081 return result;
4084 /* Similar to loc_equivalence_change_p, but for use as
4085 simplify_replace_fn_rtx callback. DATA is insn for which the
4086 elimination is done. If it null we don't do the elimination. */
4087 static rtx
4088 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4090 if (!REG_P (loc))
4091 return NULL_RTX;
4093 rtx subst = (data == NULL
4094 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4095 if (subst != loc)
4096 return subst;
4098 return NULL_RTX;
4101 /* Maximum number of generated reload insns per an insn. It is for
4102 preventing this pass cycling in a bug case. */
4103 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4105 /* The current iteration number of this LRA pass. */
4106 int lra_constraint_iter;
4108 /* True if we substituted equiv which needs checking register
4109 allocation correctness because the equivalent value contains
4110 allocatable hard registers or when we restore multi-register
4111 pseudo. */
4112 bool lra_risky_transformations_p;
4114 /* Return true if REGNO is referenced in more than one block. */
4115 static bool
4116 multi_block_pseudo_p (int regno)
4118 basic_block bb = NULL;
4119 unsigned int uid;
4120 bitmap_iterator bi;
4122 if (regno < FIRST_PSEUDO_REGISTER)
4123 return false;
4125 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4126 if (bb == NULL)
4127 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4128 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4129 return true;
4130 return false;
4133 /* Return true if LIST contains a deleted insn. */
4134 static bool
4135 contains_deleted_insn_p (rtx_insn_list *list)
4137 for (; list != NULL_RTX; list = list->next ())
4138 if (NOTE_P (list->insn ())
4139 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4140 return true;
4141 return false;
4144 /* Return true if X contains a pseudo dying in INSN. */
4145 static bool
4146 dead_pseudo_p (rtx x, rtx_insn *insn)
4148 int i, j;
4149 const char *fmt;
4150 enum rtx_code code;
4152 if (REG_P (x))
4153 return (insn != NULL_RTX
4154 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4155 code = GET_CODE (x);
4156 fmt = GET_RTX_FORMAT (code);
4157 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4159 if (fmt[i] == 'e')
4161 if (dead_pseudo_p (XEXP (x, i), insn))
4162 return true;
4164 else if (fmt[i] == 'E')
4166 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4167 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4168 return true;
4171 return false;
4174 /* Return true if INSN contains a dying pseudo in INSN right hand
4175 side. */
4176 static bool
4177 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4179 rtx set = single_set (insn);
4181 gcc_assert (set != NULL);
4182 return dead_pseudo_p (SET_SRC (set), insn);
4185 /* Return true if any init insn of REGNO contains a dying pseudo in
4186 insn right hand side. */
4187 static bool
4188 init_insn_rhs_dead_pseudo_p (int regno)
4190 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4192 if (insns == NULL)
4193 return false;
4194 for (; insns != NULL_RTX; insns = insns->next ())
4195 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4196 return true;
4197 return false;
4200 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4201 reverse only if we have one init insn with given REGNO as a
4202 source. */
4203 static bool
4204 reverse_equiv_p (int regno)
4206 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4207 rtx set;
4209 if (insns == NULL)
4210 return false;
4211 if (! INSN_P (insns->insn ())
4212 || insns->next () != NULL)
4213 return false;
4214 if ((set = single_set (insns->insn ())) == NULL_RTX)
4215 return false;
4216 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4219 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4220 call this function only for non-reverse equivalence. */
4221 static bool
4222 contains_reloaded_insn_p (int regno)
4224 rtx set;
4225 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4227 for (; list != NULL; list = list->next ())
4228 if ((set = single_set (list->insn ())) == NULL_RTX
4229 || ! REG_P (SET_DEST (set))
4230 || (int) REGNO (SET_DEST (set)) != regno)
4231 return true;
4232 return false;
4235 /* Entry function of LRA constraint pass. Return true if the
4236 constraint pass did change the code. */
4237 bool
4238 lra_constraints (bool first_p)
4240 bool changed_p;
4241 int i, hard_regno, new_insns_num;
4242 unsigned int min_len, new_min_len, uid;
4243 rtx set, x, reg, dest_reg;
4244 basic_block last_bb;
4245 bitmap_head equiv_insn_bitmap;
4246 bitmap_iterator bi;
4248 lra_constraint_iter++;
4249 if (lra_dump_file != NULL)
4250 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4251 lra_constraint_iter);
4252 changed_p = false;
4253 if (pic_offset_table_rtx
4254 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4255 lra_risky_transformations_p = true;
4256 else
4257 lra_risky_transformations_p = false;
4258 new_insn_uid_start = get_max_uid ();
4259 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4260 /* Mark used hard regs for target stack size calulations. */
4261 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4262 if (lra_reg_info[i].nrefs != 0
4263 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4265 int j, nregs;
4267 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4268 for (j = 0; j < nregs; j++)
4269 df_set_regs_ever_live (hard_regno + j, true);
4271 /* Do elimination before the equivalence processing as we can spill
4272 some pseudos during elimination. */
4273 lra_eliminate (false, first_p);
4274 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4275 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4276 if (lra_reg_info[i].nrefs != 0)
4278 ira_reg_equiv[i].profitable_p = true;
4279 reg = regno_reg_rtx[i];
4280 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4282 bool pseudo_p = contains_reg_p (x, false, false);
4284 /* After RTL transformation, we can not guarantee that
4285 pseudo in the substitution was not reloaded which might
4286 make equivalence invalid. For example, in reverse
4287 equiv of p0
4289 p0 <- ...
4291 equiv_mem <- p0
4293 the memory address register was reloaded before the 2nd
4294 insn. */
4295 if ((! first_p && pseudo_p)
4296 /* We don't use DF for compilation speed sake. So it
4297 is problematic to update live info when we use an
4298 equivalence containing pseudos in more than one
4299 BB. */
4300 || (pseudo_p && multi_block_pseudo_p (i))
4301 /* If an init insn was deleted for some reason, cancel
4302 the equiv. We could update the equiv insns after
4303 transformations including an equiv insn deletion
4304 but it is not worthy as such cases are extremely
4305 rare. */
4306 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4307 /* If it is not a reverse equivalence, we check that a
4308 pseudo in rhs of the init insn is not dying in the
4309 insn. Otherwise, the live info at the beginning of
4310 the corresponding BB might be wrong after we
4311 removed the insn. When the equiv can be a
4312 constant, the right hand side of the init insn can
4313 be a pseudo. */
4314 || (! reverse_equiv_p (i)
4315 && (init_insn_rhs_dead_pseudo_p (i)
4316 /* If we reloaded the pseudo in an equivalence
4317 init insn, we can not remove the equiv init
4318 insns and the init insns might write into
4319 const memory in this case. */
4320 || contains_reloaded_insn_p (i)))
4321 /* Prevent access beyond equivalent memory for
4322 paradoxical subregs. */
4323 || (MEM_P (x)
4324 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4325 > GET_MODE_SIZE (GET_MODE (x))))
4326 || (pic_offset_table_rtx
4327 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4328 && (targetm.preferred_reload_class
4329 (x, lra_get_allocno_class (i)) == NO_REGS))
4330 || contains_symbol_ref_p (x))))
4331 ira_reg_equiv[i].defined_p = false;
4332 if (contains_reg_p (x, false, true))
4333 ira_reg_equiv[i].profitable_p = false;
4334 if (get_equiv (reg) != reg)
4335 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4338 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4339 update_equiv (i);
4340 /* We should add all insns containing pseudos which should be
4341 substituted by their equivalences. */
4342 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4343 lra_push_insn_by_uid (uid);
4344 min_len = lra_insn_stack_length ();
4345 new_insns_num = 0;
4346 last_bb = NULL;
4347 changed_p = false;
4348 while ((new_min_len = lra_insn_stack_length ()) != 0)
4350 curr_insn = lra_pop_insn ();
4351 --new_min_len;
4352 curr_bb = BLOCK_FOR_INSN (curr_insn);
4353 if (curr_bb != last_bb)
4355 last_bb = curr_bb;
4356 bb_reload_num = lra_curr_reload_num;
4358 if (min_len > new_min_len)
4360 min_len = new_min_len;
4361 new_insns_num = 0;
4363 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4364 internal_error
4365 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4366 MAX_RELOAD_INSNS_NUMBER);
4367 new_insns_num++;
4368 if (DEBUG_INSN_P (curr_insn))
4370 /* We need to check equivalence in debug insn and change
4371 pseudo to the equivalent value if necessary. */
4372 curr_id = lra_get_insn_recog_data (curr_insn);
4373 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4375 rtx old = *curr_id->operand_loc[0];
4376 *curr_id->operand_loc[0]
4377 = simplify_replace_fn_rtx (old, NULL_RTX,
4378 loc_equivalence_callback, curr_insn);
4379 if (old != *curr_id->operand_loc[0])
4381 lra_update_insn_regno_info (curr_insn);
4382 changed_p = true;
4386 else if (INSN_P (curr_insn))
4388 if ((set = single_set (curr_insn)) != NULL_RTX)
4390 dest_reg = SET_DEST (set);
4391 /* The equivalence pseudo could be set up as SUBREG in a
4392 case when it is a call restore insn in a mode
4393 different from the pseudo mode. */
4394 if (GET_CODE (dest_reg) == SUBREG)
4395 dest_reg = SUBREG_REG (dest_reg);
4396 if ((REG_P (dest_reg)
4397 && (x = get_equiv (dest_reg)) != dest_reg
4398 /* Remove insns which set up a pseudo whose value
4399 can not be changed. Such insns might be not in
4400 init_insns because we don't update equiv data
4401 during insn transformations.
4403 As an example, let suppose that a pseudo got
4404 hard register and on the 1st pass was not
4405 changed to equivalent constant. We generate an
4406 additional insn setting up the pseudo because of
4407 secondary memory movement. Then the pseudo is
4408 spilled and we use the equiv constant. In this
4409 case we should remove the additional insn and
4410 this insn is not init_insns list. */
4411 && (! MEM_P (x) || MEM_READONLY_P (x)
4412 /* Check that this is actually an insn setting
4413 up the equivalence. */
4414 || in_list_p (curr_insn,
4415 ira_reg_equiv
4416 [REGNO (dest_reg)].init_insns)))
4417 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4418 && in_list_p (curr_insn,
4419 ira_reg_equiv
4420 [REGNO (SET_SRC (set))].init_insns)))
4422 /* This is equiv init insn of pseudo which did not get a
4423 hard register -- remove the insn. */
4424 if (lra_dump_file != NULL)
4426 fprintf (lra_dump_file,
4427 " Removing equiv init insn %i (freq=%d)\n",
4428 INSN_UID (curr_insn),
4429 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4430 dump_insn_slim (lra_dump_file, curr_insn);
4432 if (contains_reg_p (x, true, false))
4433 lra_risky_transformations_p = true;
4434 lra_set_insn_deleted (curr_insn);
4435 continue;
4438 curr_id = lra_get_insn_recog_data (curr_insn);
4439 curr_static_id = curr_id->insn_static_data;
4440 init_curr_insn_input_reloads ();
4441 init_curr_operand_mode ();
4442 if (curr_insn_transform (false))
4443 changed_p = true;
4444 /* Check non-transformed insns too for equiv change as USE
4445 or CLOBBER don't need reloads but can contain pseudos
4446 being changed on their equivalences. */
4447 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4448 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4450 lra_update_insn_regno_info (curr_insn);
4451 changed_p = true;
4455 bitmap_clear (&equiv_insn_bitmap);
4456 /* If we used a new hard regno, changed_p should be true because the
4457 hard reg is assigned to a new pseudo. */
4458 if (flag_checking && !changed_p)
4460 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4461 if (lra_reg_info[i].nrefs != 0
4462 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4464 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4466 for (j = 0; j < nregs; j++)
4467 lra_assert (df_regs_ever_live_p (hard_regno + j));
4470 return changed_p;
4473 /* Initiate the LRA constraint pass. It is done once per
4474 function. */
4475 void
4476 lra_constraints_init (void)
4480 /* Finalize the LRA constraint pass. It is done once per
4481 function. */
4482 void
4483 lra_constraints_finish (void)
4489 /* This page contains code to do inheritance/split
4490 transformations. */
4492 /* Number of reloads passed so far in current EBB. */
4493 static int reloads_num;
4495 /* Number of calls passed so far in current EBB. */
4496 static int calls_num;
4498 /* Current reload pseudo check for validity of elements in
4499 USAGE_INSNS. */
4500 static int curr_usage_insns_check;
4502 /* Info about last usage of registers in EBB to do inheritance/split
4503 transformation. Inheritance transformation is done from a spilled
4504 pseudo and split transformations from a hard register or a pseudo
4505 assigned to a hard register. */
4506 struct usage_insns
4508 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4509 value INSNS is valid. The insns is chain of optional debug insns
4510 and a finishing non-debug insn using the corresponding reg. The
4511 value is also used to mark the registers which are set up in the
4512 current insn. The negated insn uid is used for this. */
4513 int check;
4514 /* Value of global reloads_num at the last insn in INSNS. */
4515 int reloads_num;
4516 /* Value of global reloads_nums at the last insn in INSNS. */
4517 int calls_num;
4518 /* It can be true only for splitting. And it means that the restore
4519 insn should be put after insn given by the following member. */
4520 bool after_p;
4521 /* Next insns in the current EBB which use the original reg and the
4522 original reg value is not changed between the current insn and
4523 the next insns. In order words, e.g. for inheritance, if we need
4524 to use the original reg value again in the next insns we can try
4525 to use the value in a hard register from a reload insn of the
4526 current insn. */
4527 rtx insns;
4530 /* Map: regno -> corresponding pseudo usage insns. */
4531 static struct usage_insns *usage_insns;
4533 static void
4534 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4536 usage_insns[regno].check = curr_usage_insns_check;
4537 usage_insns[regno].insns = insn;
4538 usage_insns[regno].reloads_num = reloads_num;
4539 usage_insns[regno].calls_num = calls_num;
4540 usage_insns[regno].after_p = after_p;
4543 /* The function is used to form list REGNO usages which consists of
4544 optional debug insns finished by a non-debug insn using REGNO.
4545 RELOADS_NUM is current number of reload insns processed so far. */
4546 static void
4547 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
4549 rtx next_usage_insns;
4551 if (usage_insns[regno].check == curr_usage_insns_check
4552 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4553 && DEBUG_INSN_P (insn))
4555 /* Check that we did not add the debug insn yet. */
4556 if (next_usage_insns != insn
4557 && (GET_CODE (next_usage_insns) != INSN_LIST
4558 || XEXP (next_usage_insns, 0) != insn))
4559 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4560 next_usage_insns);
4562 else if (NONDEBUG_INSN_P (insn))
4563 setup_next_usage_insn (regno, insn, reloads_num, false);
4564 else
4565 usage_insns[regno].check = 0;
4568 /* Return first non-debug insn in list USAGE_INSNS. */
4569 static rtx_insn *
4570 skip_usage_debug_insns (rtx usage_insns)
4572 rtx insn;
4574 /* Skip debug insns. */
4575 for (insn = usage_insns;
4576 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4577 insn = XEXP (insn, 1))
4579 return safe_as_a <rtx_insn *> (insn);
4582 /* Return true if we need secondary memory moves for insn in
4583 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4584 into the insn. */
4585 static bool
4586 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4587 rtx usage_insns ATTRIBUTE_UNUSED)
4589 #ifndef SECONDARY_MEMORY_NEEDED
4590 return false;
4591 #else
4592 rtx_insn *insn;
4593 rtx set, dest;
4594 enum reg_class cl;
4596 if (inher_cl == ALL_REGS
4597 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4598 return false;
4599 lra_assert (INSN_P (insn));
4600 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4601 return false;
4602 dest = SET_DEST (set);
4603 if (! REG_P (dest))
4604 return false;
4605 lra_assert (inher_cl != NO_REGS);
4606 cl = get_reg_class (REGNO (dest));
4607 return (cl != NO_REGS && cl != ALL_REGS
4608 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4609 #endif
4612 /* Registers involved in inheritance/split in the current EBB
4613 (inheritance/split pseudos and original registers). */
4614 static bitmap_head check_only_regs;
4616 /* Do inheritance transformations for insn INSN, which defines (if
4617 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4618 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4619 form as the "insns" field of usage_insns. Return true if we
4620 succeed in such transformation.
4622 The transformations look like:
4624 p <- ... i <- ...
4625 ... p <- i (new insn)
4626 ... =>
4627 <- ... p ... <- ... i ...
4629 ... i <- p (new insn)
4630 <- ... p ... <- ... i ...
4631 ... =>
4632 <- ... p ... <- ... i ...
4633 where p is a spilled original pseudo and i is a new inheritance pseudo.
4636 The inheritance pseudo has the smallest class of two classes CL and
4637 class of ORIGINAL REGNO. */
4638 static bool
4639 inherit_reload_reg (bool def_p, int original_regno,
4640 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4642 if (optimize_function_for_size_p (cfun))
4643 return false;
4645 enum reg_class rclass = lra_get_allocno_class (original_regno);
4646 rtx original_reg = regno_reg_rtx[original_regno];
4647 rtx new_reg, usage_insn;
4648 rtx_insn *new_insns;
4650 lra_assert (! usage_insns[original_regno].after_p);
4651 if (lra_dump_file != NULL)
4652 fprintf (lra_dump_file,
4653 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4654 if (! ira_reg_classes_intersect_p[cl][rclass])
4656 if (lra_dump_file != NULL)
4658 fprintf (lra_dump_file,
4659 " Rejecting inheritance for %d "
4660 "because of disjoint classes %s and %s\n",
4661 original_regno, reg_class_names[cl],
4662 reg_class_names[rclass]);
4663 fprintf (lra_dump_file,
4664 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4666 return false;
4668 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4669 /* We don't use a subset of two classes because it can be
4670 NO_REGS. This transformation is still profitable in most
4671 cases even if the classes are not intersected as register
4672 move is probably cheaper than a memory load. */
4673 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4675 if (lra_dump_file != NULL)
4676 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4677 reg_class_names[cl], reg_class_names[rclass]);
4679 rclass = cl;
4681 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4683 /* Reject inheritance resulting in secondary memory moves.
4684 Otherwise, there is a danger in LRA cycling. Also such
4685 transformation will be unprofitable. */
4686 if (lra_dump_file != NULL)
4688 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4689 rtx set = single_set (insn);
4691 lra_assert (set != NULL_RTX);
4693 rtx dest = SET_DEST (set);
4695 lra_assert (REG_P (dest));
4696 fprintf (lra_dump_file,
4697 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4698 "as secondary mem is needed\n",
4699 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4700 original_regno, reg_class_names[rclass]);
4701 fprintf (lra_dump_file,
4702 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4704 return false;
4706 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4707 rclass, "inheritance");
4708 start_sequence ();
4709 if (def_p)
4710 lra_emit_move (original_reg, new_reg);
4711 else
4712 lra_emit_move (new_reg, original_reg);
4713 new_insns = get_insns ();
4714 end_sequence ();
4715 if (NEXT_INSN (new_insns) != NULL_RTX)
4717 if (lra_dump_file != NULL)
4719 fprintf (lra_dump_file,
4720 " Rejecting inheritance %d->%d "
4721 "as it results in 2 or more insns:\n",
4722 original_regno, REGNO (new_reg));
4723 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4724 fprintf (lra_dump_file,
4725 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4727 return false;
4729 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
4730 lra_update_insn_regno_info (insn);
4731 if (! def_p)
4732 /* We now have a new usage insn for original regno. */
4733 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4734 if (lra_dump_file != NULL)
4735 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4736 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4737 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4738 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4739 bitmap_set_bit (&check_only_regs, original_regno);
4740 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4741 if (def_p)
4742 lra_process_new_insns (insn, NULL, new_insns,
4743 "Add original<-inheritance");
4744 else
4745 lra_process_new_insns (insn, new_insns, NULL,
4746 "Add inheritance<-original");
4747 while (next_usage_insns != NULL_RTX)
4749 if (GET_CODE (next_usage_insns) != INSN_LIST)
4751 usage_insn = next_usage_insns;
4752 lra_assert (NONDEBUG_INSN_P (usage_insn));
4753 next_usage_insns = NULL;
4755 else
4757 usage_insn = XEXP (next_usage_insns, 0);
4758 lra_assert (DEBUG_INSN_P (usage_insn));
4759 next_usage_insns = XEXP (next_usage_insns, 1);
4761 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
4762 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4763 if (lra_dump_file != NULL)
4765 fprintf (lra_dump_file,
4766 " Inheritance reuse change %d->%d (bb%d):\n",
4767 original_regno, REGNO (new_reg),
4768 BLOCK_FOR_INSN (usage_insn)->index);
4769 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
4772 if (lra_dump_file != NULL)
4773 fprintf (lra_dump_file,
4774 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4775 return true;
4778 /* Return true if we need a caller save/restore for pseudo REGNO which
4779 was assigned to a hard register. */
4780 static inline bool
4781 need_for_call_save_p (int regno)
4783 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4784 return (usage_insns[regno].calls_num < calls_num
4785 && (overlaps_hard_reg_set_p
4786 ((flag_ipa_ra &&
4787 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4788 ? lra_reg_info[regno].actual_call_used_reg_set
4789 : call_used_reg_set,
4790 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4791 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4792 PSEUDO_REGNO_MODE (regno))));
4795 /* Global registers occurring in the current EBB. */
4796 static bitmap_head ebb_global_regs;
4798 /* Return true if we need a split for hard register REGNO or pseudo
4799 REGNO which was assigned to a hard register.
4800 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4801 used for reloads since the EBB end. It is an approximation of the
4802 used hard registers in the split range. The exact value would
4803 require expensive calculations. If we were aggressive with
4804 splitting because of the approximation, the split pseudo will save
4805 the same hard register assignment and will be removed in the undo
4806 pass. We still need the approximation because too aggressive
4807 splitting would result in too inaccurate cost calculation in the
4808 assignment pass because of too many generated moves which will be
4809 probably removed in the undo pass. */
4810 static inline bool
4811 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4813 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4815 lra_assert (hard_regno >= 0);
4816 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4817 /* Don't split eliminable hard registers, otherwise we can
4818 split hard registers like hard frame pointer, which
4819 lives on BB start/end according to DF-infrastructure,
4820 when there is a pseudo assigned to the register and
4821 living in the same BB. */
4822 && (regno >= FIRST_PSEUDO_REGISTER
4823 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4824 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4825 /* Don't split call clobbered hard regs living through
4826 calls, otherwise we might have a check problem in the
4827 assign sub-pass as in the most cases (exception is a
4828 situation when lra_risky_transformations_p value is
4829 true) the assign pass assumes that all pseudos living
4830 through calls are assigned to call saved hard regs. */
4831 && (regno >= FIRST_PSEUDO_REGISTER
4832 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4833 || usage_insns[regno].calls_num == calls_num)
4834 /* We need at least 2 reloads to make pseudo splitting
4835 profitable. We should provide hard regno splitting in
4836 any case to solve 1st insn scheduling problem when
4837 moving hard register definition up might result in
4838 impossibility to find hard register for reload pseudo of
4839 small register class. */
4840 && (usage_insns[regno].reloads_num
4841 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4842 && (regno < FIRST_PSEUDO_REGISTER
4843 /* For short living pseudos, spilling + inheritance can
4844 be considered a substitution for splitting.
4845 Therefore we do not splitting for local pseudos. It
4846 decreases also aggressiveness of splitting. The
4847 minimal number of references is chosen taking into
4848 account that for 2 references splitting has no sense
4849 as we can just spill the pseudo. */
4850 || (regno >= FIRST_PSEUDO_REGISTER
4851 && lra_reg_info[regno].nrefs > 3
4852 && bitmap_bit_p (&ebb_global_regs, regno))))
4853 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4856 /* Return class for the split pseudo created from original pseudo with
4857 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4858 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4859 results in no secondary memory movements. */
4860 static enum reg_class
4861 choose_split_class (enum reg_class allocno_class,
4862 int hard_regno ATTRIBUTE_UNUSED,
4863 machine_mode mode ATTRIBUTE_UNUSED)
4865 #ifndef SECONDARY_MEMORY_NEEDED
4866 return allocno_class;
4867 #else
4868 int i;
4869 enum reg_class cl, best_cl = NO_REGS;
4870 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4871 = REGNO_REG_CLASS (hard_regno);
4873 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4874 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4875 return allocno_class;
4876 for (i = 0;
4877 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4878 i++)
4879 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4880 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4881 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4882 && (best_cl == NO_REGS
4883 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4884 best_cl = cl;
4885 return best_cl;
4886 #endif
4889 /* Do split transformations for insn INSN, which defines or uses
4890 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4891 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4892 "insns" field of usage_insns.
4894 The transformations look like:
4896 p <- ... p <- ...
4897 ... s <- p (new insn -- save)
4898 ... =>
4899 ... p <- s (new insn -- restore)
4900 <- ... p ... <- ... p ...
4902 <- ... p ... <- ... p ...
4903 ... s <- p (new insn -- save)
4904 ... =>
4905 ... p <- s (new insn -- restore)
4906 <- ... p ... <- ... p ...
4908 where p is an original pseudo got a hard register or a hard
4909 register and s is a new split pseudo. The save is put before INSN
4910 if BEFORE_P is true. Return true if we succeed in such
4911 transformation. */
4912 static bool
4913 split_reg (bool before_p, int original_regno, rtx_insn *insn,
4914 rtx next_usage_insns)
4916 enum reg_class rclass;
4917 rtx original_reg;
4918 int hard_regno, nregs;
4919 rtx new_reg, usage_insn;
4920 rtx_insn *restore, *save;
4921 bool after_p;
4922 bool call_save_p;
4924 if (original_regno < FIRST_PSEUDO_REGISTER)
4926 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4927 hard_regno = original_regno;
4928 call_save_p = false;
4929 nregs = 1;
4931 else
4933 hard_regno = reg_renumber[original_regno];
4934 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4935 rclass = lra_get_allocno_class (original_regno);
4936 original_reg = regno_reg_rtx[original_regno];
4937 call_save_p = need_for_call_save_p (original_regno);
4939 original_reg = regno_reg_rtx[original_regno];
4940 lra_assert (hard_regno >= 0);
4941 if (lra_dump_file != NULL)
4942 fprintf (lra_dump_file,
4943 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4944 if (call_save_p)
4946 machine_mode mode = GET_MODE (original_reg);
4948 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4949 hard_regno_nregs[hard_regno][mode],
4950 mode);
4951 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4953 else
4955 rclass = choose_split_class (rclass, hard_regno,
4956 GET_MODE (original_reg));
4957 if (rclass == NO_REGS)
4959 if (lra_dump_file != NULL)
4961 fprintf (lra_dump_file,
4962 " Rejecting split of %d(%s): "
4963 "no good reg class for %d(%s)\n",
4964 original_regno,
4965 reg_class_names[lra_get_allocno_class (original_regno)],
4966 hard_regno,
4967 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4968 fprintf
4969 (lra_dump_file,
4970 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4972 return false;
4974 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4975 rclass, "split");
4976 reg_renumber[REGNO (new_reg)] = hard_regno;
4978 save = emit_spill_move (true, new_reg, original_reg);
4979 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
4981 if (lra_dump_file != NULL)
4983 fprintf
4984 (lra_dump_file,
4985 " Rejecting split %d->%d resulting in > 2 save insns:\n",
4986 original_regno, REGNO (new_reg));
4987 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
4988 fprintf (lra_dump_file,
4989 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4991 return false;
4993 restore = emit_spill_move (false, new_reg, original_reg);
4994 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
4996 if (lra_dump_file != NULL)
4998 fprintf (lra_dump_file,
4999 " Rejecting split %d->%d "
5000 "resulting in > 2 restore insns:\n",
5001 original_regno, REGNO (new_reg));
5002 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
5003 fprintf (lra_dump_file,
5004 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5006 return false;
5008 after_p = usage_insns[original_regno].after_p;
5009 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
5010 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5011 bitmap_set_bit (&check_only_regs, original_regno);
5012 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
5013 for (;;)
5015 if (GET_CODE (next_usage_insns) != INSN_LIST)
5017 usage_insn = next_usage_insns;
5018 break;
5020 usage_insn = XEXP (next_usage_insns, 0);
5021 lra_assert (DEBUG_INSN_P (usage_insn));
5022 next_usage_insns = XEXP (next_usage_insns, 1);
5023 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5024 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5025 if (lra_dump_file != NULL)
5027 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5028 original_regno, REGNO (new_reg));
5029 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5032 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5033 lra_assert (usage_insn != insn || (after_p && before_p));
5034 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5035 after_p ? NULL : restore,
5036 after_p ? restore : NULL,
5037 call_save_p
5038 ? "Add reg<-save" : "Add reg<-split");
5039 lra_process_new_insns (insn, before_p ? save : NULL,
5040 before_p ? NULL : save,
5041 call_save_p
5042 ? "Add save<-reg" : "Add split<-reg");
5043 if (nregs > 1)
5044 /* If we are trying to split multi-register. We should check
5045 conflicts on the next assignment sub-pass. IRA can allocate on
5046 sub-register levels, LRA do this on pseudos level right now and
5047 this discrepancy may create allocation conflicts after
5048 splitting. */
5049 lra_risky_transformations_p = true;
5050 if (lra_dump_file != NULL)
5051 fprintf (lra_dump_file,
5052 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5053 return true;
5056 /* Recognize that we need a split transformation for insn INSN, which
5057 defines or uses REGNO in its insn biggest MODE (we use it only if
5058 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5059 hard registers which might be used for reloads since the EBB end.
5060 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5061 uid before starting INSN processing. Return true if we succeed in
5062 such transformation. */
5063 static bool
5064 split_if_necessary (int regno, machine_mode mode,
5065 HARD_REG_SET potential_reload_hard_regs,
5066 bool before_p, rtx_insn *insn, int max_uid)
5068 bool res = false;
5069 int i, nregs = 1;
5070 rtx next_usage_insns;
5072 if (regno < FIRST_PSEUDO_REGISTER)
5073 nregs = hard_regno_nregs[regno][mode];
5074 for (i = 0; i < nregs; i++)
5075 if (usage_insns[regno + i].check == curr_usage_insns_check
5076 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5077 /* To avoid processing the register twice or more. */
5078 && ((GET_CODE (next_usage_insns) != INSN_LIST
5079 && INSN_UID (next_usage_insns) < max_uid)
5080 || (GET_CODE (next_usage_insns) == INSN_LIST
5081 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5082 && need_for_split_p (potential_reload_hard_regs, regno + i)
5083 && split_reg (before_p, regno + i, insn, next_usage_insns))
5084 res = true;
5085 return res;
5088 /* Check only registers living at the current program point in the
5089 current EBB. */
5090 static bitmap_head live_regs;
5092 /* Update live info in EBB given by its HEAD and TAIL insns after
5093 inheritance/split transformation. The function removes dead moves
5094 too. */
5095 static void
5096 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5098 unsigned int j;
5099 int i, regno;
5100 bool live_p;
5101 rtx_insn *prev_insn;
5102 rtx set;
5103 bool remove_p;
5104 basic_block last_bb, prev_bb, curr_bb;
5105 bitmap_iterator bi;
5106 struct lra_insn_reg *reg;
5107 edge e;
5108 edge_iterator ei;
5110 last_bb = BLOCK_FOR_INSN (tail);
5111 prev_bb = NULL;
5112 for (curr_insn = tail;
5113 curr_insn != PREV_INSN (head);
5114 curr_insn = prev_insn)
5116 prev_insn = PREV_INSN (curr_insn);
5117 /* We need to process empty blocks too. They contain
5118 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5119 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5120 continue;
5121 curr_bb = BLOCK_FOR_INSN (curr_insn);
5122 if (curr_bb != prev_bb)
5124 if (prev_bb != NULL)
5126 /* Update df_get_live_in (prev_bb): */
5127 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5128 if (bitmap_bit_p (&live_regs, j))
5129 bitmap_set_bit (df_get_live_in (prev_bb), j);
5130 else
5131 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5133 if (curr_bb != last_bb)
5135 /* Update df_get_live_out (curr_bb): */
5136 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5138 live_p = bitmap_bit_p (&live_regs, j);
5139 if (! live_p)
5140 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5141 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5143 live_p = true;
5144 break;
5146 if (live_p)
5147 bitmap_set_bit (df_get_live_out (curr_bb), j);
5148 else
5149 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5152 prev_bb = curr_bb;
5153 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5155 if (! NONDEBUG_INSN_P (curr_insn))
5156 continue;
5157 curr_id = lra_get_insn_recog_data (curr_insn);
5158 curr_static_id = curr_id->insn_static_data;
5159 remove_p = false;
5160 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
5161 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5162 && bitmap_bit_p (&check_only_regs, regno)
5163 && ! bitmap_bit_p (&live_regs, regno))
5164 remove_p = true;
5165 /* See which defined values die here. */
5166 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5167 if (reg->type == OP_OUT && ! reg->subreg_p)
5168 bitmap_clear_bit (&live_regs, reg->regno);
5169 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5170 if (reg->type == OP_OUT && ! reg->subreg_p)
5171 bitmap_clear_bit (&live_regs, reg->regno);
5172 if (curr_id->arg_hard_regs != NULL)
5173 /* Make clobbered argument hard registers die. */
5174 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5175 if (regno >= FIRST_PSEUDO_REGISTER)
5176 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
5177 /* Mark each used value as live. */
5178 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5179 if (reg->type != OP_OUT
5180 && bitmap_bit_p (&check_only_regs, reg->regno))
5181 bitmap_set_bit (&live_regs, reg->regno);
5182 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5183 if (reg->type != OP_OUT
5184 && bitmap_bit_p (&check_only_regs, reg->regno))
5185 bitmap_set_bit (&live_regs, reg->regno);
5186 if (curr_id->arg_hard_regs != NULL)
5187 /* Make used argument hard registers live. */
5188 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5189 if (regno < FIRST_PSEUDO_REGISTER
5190 && bitmap_bit_p (&check_only_regs, regno))
5191 bitmap_set_bit (&live_regs, regno);
5192 /* It is quite important to remove dead move insns because it
5193 means removing dead store. We don't need to process them for
5194 constraints. */
5195 if (remove_p)
5197 if (lra_dump_file != NULL)
5199 fprintf (lra_dump_file, " Removing dead insn:\n ");
5200 dump_insn_slim (lra_dump_file, curr_insn);
5202 lra_set_insn_deleted (curr_insn);
5207 /* The structure describes info to do an inheritance for the current
5208 insn. We need to collect such info first before doing the
5209 transformations because the transformations change the insn
5210 internal representation. */
5211 struct to_inherit
5213 /* Original regno. */
5214 int regno;
5215 /* Subsequent insns which can inherit original reg value. */
5216 rtx insns;
5219 /* Array containing all info for doing inheritance from the current
5220 insn. */
5221 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5223 /* Number elements in the previous array. */
5224 static int to_inherit_num;
5226 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5227 structure to_inherit. */
5228 static void
5229 add_to_inherit (int regno, rtx insns)
5231 int i;
5233 for (i = 0; i < to_inherit_num; i++)
5234 if (to_inherit[i].regno == regno)
5235 return;
5236 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5237 to_inherit[to_inherit_num].regno = regno;
5238 to_inherit[to_inherit_num++].insns = insns;
5241 /* Return the last non-debug insn in basic block BB, or the block begin
5242 note if none. */
5243 static rtx_insn *
5244 get_last_insertion_point (basic_block bb)
5246 rtx_insn *insn;
5248 FOR_BB_INSNS_REVERSE (bb, insn)
5249 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5250 return insn;
5251 gcc_unreachable ();
5254 /* Set up RES by registers living on edges FROM except the edge (FROM,
5255 TO) or by registers set up in a jump insn in BB FROM. */
5256 static void
5257 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5259 rtx_insn *last;
5260 struct lra_insn_reg *reg;
5261 edge e;
5262 edge_iterator ei;
5264 lra_assert (to != NULL);
5265 bitmap_clear (res);
5266 FOR_EACH_EDGE (e, ei, from->succs)
5267 if (e->dest != to)
5268 bitmap_ior_into (res, df_get_live_in (e->dest));
5269 last = get_last_insertion_point (from);
5270 if (! JUMP_P (last))
5271 return;
5272 curr_id = lra_get_insn_recog_data (last);
5273 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5274 if (reg->type != OP_IN)
5275 bitmap_set_bit (res, reg->regno);
5278 /* Used as a temporary results of some bitmap calculations. */
5279 static bitmap_head temp_bitmap;
5281 /* We split for reloads of small class of hard regs. The following
5282 defines how many hard regs the class should have to be qualified as
5283 small. The code is mostly oriented to x86/x86-64 architecture
5284 where some insns need to use only specific register or pair of
5285 registers and these register can live in RTL explicitly, e.g. for
5286 parameter passing. */
5287 static const int max_small_class_regs_num = 2;
5289 /* Do inheritance/split transformations in EBB starting with HEAD and
5290 finishing on TAIL. We process EBB insns in the reverse order.
5291 Return true if we did any inheritance/split transformation in the
5292 EBB.
5294 We should avoid excessive splitting which results in worse code
5295 because of inaccurate cost calculations for spilling new split
5296 pseudos in such case. To achieve this we do splitting only if
5297 register pressure is high in given basic block and there are reload
5298 pseudos requiring hard registers. We could do more register
5299 pressure calculations at any given program point to avoid necessary
5300 splitting even more but it is to expensive and the current approach
5301 works well enough. */
5302 static bool
5303 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5305 int i, src_regno, dst_regno, nregs;
5306 bool change_p, succ_p, update_reloads_num_p;
5307 rtx_insn *prev_insn, *last_insn;
5308 rtx next_usage_insns, set;
5309 enum reg_class cl;
5310 struct lra_insn_reg *reg;
5311 basic_block last_processed_bb, curr_bb = NULL;
5312 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5313 bitmap to_process;
5314 unsigned int j;
5315 bitmap_iterator bi;
5316 bool head_p, after_p;
5318 change_p = false;
5319 curr_usage_insns_check++;
5320 reloads_num = calls_num = 0;
5321 bitmap_clear (&check_only_regs);
5322 last_processed_bb = NULL;
5323 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5324 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5325 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5326 /* We don't process new insns generated in the loop. */
5327 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5329 prev_insn = PREV_INSN (curr_insn);
5330 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5331 curr_bb = BLOCK_FOR_INSN (curr_insn);
5332 if (last_processed_bb != curr_bb)
5334 /* We are at the end of BB. Add qualified living
5335 pseudos for potential splitting. */
5336 to_process = df_get_live_out (curr_bb);
5337 if (last_processed_bb != NULL)
5339 /* We are somewhere in the middle of EBB. */
5340 get_live_on_other_edges (curr_bb, last_processed_bb,
5341 &temp_bitmap);
5342 to_process = &temp_bitmap;
5344 last_processed_bb = curr_bb;
5345 last_insn = get_last_insertion_point (curr_bb);
5346 after_p = (! JUMP_P (last_insn)
5347 && (! CALL_P (last_insn)
5348 || (find_reg_note (last_insn,
5349 REG_NORETURN, NULL_RTX) == NULL_RTX
5350 && ! SIBLING_CALL_P (last_insn))));
5351 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5352 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5354 if ((int) j >= lra_constraint_new_regno_start)
5355 break;
5356 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5358 if (j < FIRST_PSEUDO_REGISTER)
5359 SET_HARD_REG_BIT (live_hard_regs, j);
5360 else
5361 add_to_hard_reg_set (&live_hard_regs,
5362 PSEUDO_REGNO_MODE (j),
5363 reg_renumber[j]);
5364 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5368 src_regno = dst_regno = -1;
5369 if (NONDEBUG_INSN_P (curr_insn)
5370 && (set = single_set (curr_insn)) != NULL_RTX
5371 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5373 src_regno = REGNO (SET_SRC (set));
5374 dst_regno = REGNO (SET_DEST (set));
5376 update_reloads_num_p = true;
5377 if (src_regno < lra_constraint_new_regno_start
5378 && src_regno >= FIRST_PSEUDO_REGISTER
5379 && reg_renumber[src_regno] < 0
5380 && dst_regno >= lra_constraint_new_regno_start
5381 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5383 /* 'reload_pseudo <- original_pseudo'. */
5384 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5385 reloads_num++;
5386 update_reloads_num_p = false;
5387 succ_p = false;
5388 if (usage_insns[src_regno].check == curr_usage_insns_check
5389 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5390 succ_p = inherit_reload_reg (false, src_regno, cl,
5391 curr_insn, next_usage_insns);
5392 if (succ_p)
5393 change_p = true;
5394 else
5395 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5396 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5397 IOR_HARD_REG_SET (potential_reload_hard_regs,
5398 reg_class_contents[cl]);
5400 else if (src_regno >= lra_constraint_new_regno_start
5401 && dst_regno < lra_constraint_new_regno_start
5402 && dst_regno >= FIRST_PSEUDO_REGISTER
5403 && reg_renumber[dst_regno] < 0
5404 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5405 && usage_insns[dst_regno].check == curr_usage_insns_check
5406 && (next_usage_insns
5407 = usage_insns[dst_regno].insns) != NULL_RTX)
5409 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5410 reloads_num++;
5411 update_reloads_num_p = false;
5412 /* 'original_pseudo <- reload_pseudo'. */
5413 if (! JUMP_P (curr_insn)
5414 && inherit_reload_reg (true, dst_regno, cl,
5415 curr_insn, next_usage_insns))
5416 change_p = true;
5417 /* Invalidate. */
5418 usage_insns[dst_regno].check = 0;
5419 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5420 IOR_HARD_REG_SET (potential_reload_hard_regs,
5421 reg_class_contents[cl]);
5423 else if (INSN_P (curr_insn))
5425 int iter;
5426 int max_uid = get_max_uid ();
5428 curr_id = lra_get_insn_recog_data (curr_insn);
5429 curr_static_id = curr_id->insn_static_data;
5430 to_inherit_num = 0;
5431 /* Process insn definitions. */
5432 for (iter = 0; iter < 2; iter++)
5433 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5434 reg != NULL;
5435 reg = reg->next)
5436 if (reg->type != OP_IN
5437 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5439 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5440 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5441 && usage_insns[dst_regno].check == curr_usage_insns_check
5442 && (next_usage_insns
5443 = usage_insns[dst_regno].insns) != NULL_RTX)
5445 struct lra_insn_reg *r;
5447 for (r = curr_id->regs; r != NULL; r = r->next)
5448 if (r->type != OP_OUT && r->regno == dst_regno)
5449 break;
5450 /* Don't do inheritance if the pseudo is also
5451 used in the insn. */
5452 if (r == NULL)
5453 /* We can not do inheritance right now
5454 because the current insn reg info (chain
5455 regs) can change after that. */
5456 add_to_inherit (dst_regno, next_usage_insns);
5458 /* We can not process one reg twice here because of
5459 usage_insns invalidation. */
5460 if ((dst_regno < FIRST_PSEUDO_REGISTER
5461 || reg_renumber[dst_regno] >= 0)
5462 && ! reg->subreg_p && reg->type != OP_IN)
5464 HARD_REG_SET s;
5466 if (split_if_necessary (dst_regno, reg->biggest_mode,
5467 potential_reload_hard_regs,
5468 false, curr_insn, max_uid))
5469 change_p = true;
5470 CLEAR_HARD_REG_SET (s);
5471 if (dst_regno < FIRST_PSEUDO_REGISTER)
5472 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5473 else
5474 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5475 reg_renumber[dst_regno]);
5476 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5478 /* We should invalidate potential inheritance or
5479 splitting for the current insn usages to the next
5480 usage insns (see code below) as the output pseudo
5481 prevents this. */
5482 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5483 && reg_renumber[dst_regno] < 0)
5484 || (reg->type == OP_OUT && ! reg->subreg_p
5485 && (dst_regno < FIRST_PSEUDO_REGISTER
5486 || reg_renumber[dst_regno] >= 0)))
5488 /* Invalidate and mark definitions. */
5489 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5490 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5491 else
5493 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5494 for (i = 0; i < nregs; i++)
5495 usage_insns[dst_regno + i].check
5496 = -(int) INSN_UID (curr_insn);
5500 /* Process clobbered call regs. */
5501 if (curr_id->arg_hard_regs != NULL)
5502 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5503 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5504 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
5505 = -(int) INSN_UID (curr_insn);
5506 if (! JUMP_P (curr_insn))
5507 for (i = 0; i < to_inherit_num; i++)
5508 if (inherit_reload_reg (true, to_inherit[i].regno,
5509 ALL_REGS, curr_insn,
5510 to_inherit[i].insns))
5511 change_p = true;
5512 if (CALL_P (curr_insn))
5514 rtx cheap, pat, dest;
5515 rtx_insn *restore;
5516 int regno, hard_regno;
5518 calls_num++;
5519 if ((cheap = find_reg_note (curr_insn,
5520 REG_RETURNED, NULL_RTX)) != NULL_RTX
5521 && ((cheap = XEXP (cheap, 0)), true)
5522 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5523 && (hard_regno = reg_renumber[regno]) >= 0
5524 /* If there are pending saves/restores, the
5525 optimization is not worth. */
5526 && usage_insns[regno].calls_num == calls_num - 1
5527 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5529 /* Restore the pseudo from the call result as
5530 REG_RETURNED note says that the pseudo value is
5531 in the call result and the pseudo is an argument
5532 of the call. */
5533 pat = PATTERN (curr_insn);
5534 if (GET_CODE (pat) == PARALLEL)
5535 pat = XVECEXP (pat, 0, 0);
5536 dest = SET_DEST (pat);
5537 /* For multiple return values dest is PARALLEL.
5538 Currently we handle only single return value case. */
5539 if (REG_P (dest))
5541 start_sequence ();
5542 emit_move_insn (cheap, copy_rtx (dest));
5543 restore = get_insns ();
5544 end_sequence ();
5545 lra_process_new_insns (curr_insn, NULL, restore,
5546 "Inserting call parameter restore");
5547 /* We don't need to save/restore of the pseudo from
5548 this call. */
5549 usage_insns[regno].calls_num = calls_num;
5550 bitmap_set_bit (&check_only_regs, regno);
5554 to_inherit_num = 0;
5555 /* Process insn usages. */
5556 for (iter = 0; iter < 2; iter++)
5557 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5558 reg != NULL;
5559 reg = reg->next)
5560 if ((reg->type != OP_OUT
5561 || (reg->type == OP_OUT && reg->subreg_p))
5562 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5564 if (src_regno >= FIRST_PSEUDO_REGISTER
5565 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5567 if (usage_insns[src_regno].check == curr_usage_insns_check
5568 && (next_usage_insns
5569 = usage_insns[src_regno].insns) != NULL_RTX
5570 && NONDEBUG_INSN_P (curr_insn))
5571 add_to_inherit (src_regno, next_usage_insns);
5572 else if (usage_insns[src_regno].check
5573 != -(int) INSN_UID (curr_insn))
5574 /* Add usages but only if the reg is not set up
5575 in the same insn. */
5576 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5578 else if (src_regno < FIRST_PSEUDO_REGISTER
5579 || reg_renumber[src_regno] >= 0)
5581 bool before_p;
5582 rtx_insn *use_insn = curr_insn;
5584 before_p = (JUMP_P (curr_insn)
5585 || (CALL_P (curr_insn) && reg->type == OP_IN));
5586 if (NONDEBUG_INSN_P (curr_insn)
5587 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5588 && split_if_necessary (src_regno, reg->biggest_mode,
5589 potential_reload_hard_regs,
5590 before_p, curr_insn, max_uid))
5592 if (reg->subreg_p)
5593 lra_risky_transformations_p = true;
5594 change_p = true;
5595 /* Invalidate. */
5596 usage_insns[src_regno].check = 0;
5597 if (before_p)
5598 use_insn = PREV_INSN (curr_insn);
5600 if (NONDEBUG_INSN_P (curr_insn))
5602 if (src_regno < FIRST_PSEUDO_REGISTER)
5603 add_to_hard_reg_set (&live_hard_regs,
5604 reg->biggest_mode, src_regno);
5605 else
5606 add_to_hard_reg_set (&live_hard_regs,
5607 PSEUDO_REGNO_MODE (src_regno),
5608 reg_renumber[src_regno]);
5610 add_next_usage_insn (src_regno, use_insn, reloads_num);
5613 /* Process used call regs. */
5614 if (curr_id->arg_hard_regs != NULL)
5615 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5616 if (src_regno < FIRST_PSEUDO_REGISTER)
5618 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5619 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5621 for (i = 0; i < to_inherit_num; i++)
5623 src_regno = to_inherit[i].regno;
5624 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5625 curr_insn, to_inherit[i].insns))
5626 change_p = true;
5627 else
5628 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5631 if (update_reloads_num_p
5632 && NONDEBUG_INSN_P (curr_insn)
5633 && (set = single_set (curr_insn)) != NULL_RTX)
5635 int regno = -1;
5636 if ((REG_P (SET_DEST (set))
5637 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5638 && reg_renumber[regno] < 0
5639 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5640 || (REG_P (SET_SRC (set))
5641 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5642 && reg_renumber[regno] < 0
5643 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5645 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5646 reloads_num++;
5647 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5648 IOR_HARD_REG_SET (potential_reload_hard_regs,
5649 reg_class_contents[cl]);
5652 /* We reached the start of the current basic block. */
5653 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5654 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5656 /* We reached the beginning of the current block -- do
5657 rest of spliting in the current BB. */
5658 to_process = df_get_live_in (curr_bb);
5659 if (BLOCK_FOR_INSN (head) != curr_bb)
5661 /* We are somewhere in the middle of EBB. */
5662 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5663 curr_bb, &temp_bitmap);
5664 to_process = &temp_bitmap;
5666 head_p = true;
5667 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5669 if ((int) j >= lra_constraint_new_regno_start)
5670 break;
5671 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5672 && usage_insns[j].check == curr_usage_insns_check
5673 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5675 if (need_for_split_p (potential_reload_hard_regs, j))
5677 if (lra_dump_file != NULL && head_p)
5679 fprintf (lra_dump_file,
5680 " ----------------------------------\n");
5681 head_p = false;
5683 if (split_reg (false, j, bb_note (curr_bb),
5684 next_usage_insns))
5685 change_p = true;
5687 usage_insns[j].check = 0;
5692 return change_p;
5695 /* This value affects EBB forming. If probability of edge from EBB to
5696 a BB is not greater than the following value, we don't add the BB
5697 to EBB. */
5698 #define EBB_PROBABILITY_CUTOFF \
5699 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
5701 /* Current number of inheritance/split iteration. */
5702 int lra_inheritance_iter;
5704 /* Entry function for inheritance/split pass. */
5705 void
5706 lra_inheritance (void)
5708 int i;
5709 basic_block bb, start_bb;
5710 edge e;
5712 lra_inheritance_iter++;
5713 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5714 return;
5715 timevar_push (TV_LRA_INHERITANCE);
5716 if (lra_dump_file != NULL)
5717 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5718 lra_inheritance_iter);
5719 curr_usage_insns_check = 0;
5720 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5721 for (i = 0; i < lra_constraint_new_regno_start; i++)
5722 usage_insns[i].check = 0;
5723 bitmap_initialize (&check_only_regs, &reg_obstack);
5724 bitmap_initialize (&live_regs, &reg_obstack);
5725 bitmap_initialize (&temp_bitmap, &reg_obstack);
5726 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5727 FOR_EACH_BB_FN (bb, cfun)
5729 start_bb = bb;
5730 if (lra_dump_file != NULL)
5731 fprintf (lra_dump_file, "EBB");
5732 /* Form a EBB starting with BB. */
5733 bitmap_clear (&ebb_global_regs);
5734 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5735 for (;;)
5737 if (lra_dump_file != NULL)
5738 fprintf (lra_dump_file, " %d", bb->index);
5739 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5740 || LABEL_P (BB_HEAD (bb->next_bb)))
5741 break;
5742 e = find_fallthru_edge (bb->succs);
5743 if (! e)
5744 break;
5745 if (e->probability < EBB_PROBABILITY_CUTOFF)
5746 break;
5747 bb = bb->next_bb;
5749 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5750 if (lra_dump_file != NULL)
5751 fprintf (lra_dump_file, "\n");
5752 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5753 /* Remember that the EBB head and tail can change in
5754 inherit_in_ebb. */
5755 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5757 bitmap_clear (&ebb_global_regs);
5758 bitmap_clear (&temp_bitmap);
5759 bitmap_clear (&live_regs);
5760 bitmap_clear (&check_only_regs);
5761 free (usage_insns);
5763 timevar_pop (TV_LRA_INHERITANCE);
5768 /* This page contains code to undo failed inheritance/split
5769 transformations. */
5771 /* Current number of iteration undoing inheritance/split. */
5772 int lra_undo_inheritance_iter;
5774 /* Fix BB live info LIVE after removing pseudos created on pass doing
5775 inheritance/split which are REMOVED_PSEUDOS. */
5776 static void
5777 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5779 unsigned int regno;
5780 bitmap_iterator bi;
5782 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5783 if (bitmap_clear_bit (live, regno))
5784 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5787 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5788 number. */
5789 static int
5790 get_regno (rtx reg)
5792 if (GET_CODE (reg) == SUBREG)
5793 reg = SUBREG_REG (reg);
5794 if (REG_P (reg))
5795 return REGNO (reg);
5796 return -1;
5799 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5800 return true if we did any change. The undo transformations for
5801 inheritance looks like
5802 i <- i2
5803 p <- i => p <- i2
5804 or removing
5805 p <- i, i <- p, and i <- i3
5806 where p is original pseudo from which inheritance pseudo i was
5807 created, i and i3 are removed inheritance pseudos, i2 is another
5808 not removed inheritance pseudo. All split pseudos or other
5809 occurrences of removed inheritance pseudos are changed on the
5810 corresponding original pseudos.
5812 The function also schedules insns changed and created during
5813 inheritance/split pass for processing by the subsequent constraint
5814 pass. */
5815 static bool
5816 remove_inheritance_pseudos (bitmap remove_pseudos)
5818 basic_block bb;
5819 int regno, sregno, prev_sregno, dregno, restore_regno;
5820 rtx set, prev_set;
5821 rtx_insn *prev_insn;
5822 bool change_p, done_p;
5824 change_p = ! bitmap_empty_p (remove_pseudos);
5825 /* We can not finish the function right away if CHANGE_P is true
5826 because we need to marks insns affected by previous
5827 inheritance/split pass for processing by the subsequent
5828 constraint pass. */
5829 FOR_EACH_BB_FN (bb, cfun)
5831 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5832 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5833 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5835 if (! INSN_P (curr_insn))
5836 continue;
5837 done_p = false;
5838 sregno = dregno = -1;
5839 if (change_p && NONDEBUG_INSN_P (curr_insn)
5840 && (set = single_set (curr_insn)) != NULL_RTX)
5842 dregno = get_regno (SET_DEST (set));
5843 sregno = get_regno (SET_SRC (set));
5846 if (sregno >= 0 && dregno >= 0)
5848 if ((bitmap_bit_p (remove_pseudos, sregno)
5849 && (lra_reg_info[sregno].restore_regno == dregno
5850 || (bitmap_bit_p (remove_pseudos, dregno)
5851 && (lra_reg_info[sregno].restore_regno
5852 == lra_reg_info[dregno].restore_regno))))
5853 || (bitmap_bit_p (remove_pseudos, dregno)
5854 && lra_reg_info[dregno].restore_regno == sregno))
5855 /* One of the following cases:
5856 original <- removed inheritance pseudo
5857 removed inherit pseudo <- another removed inherit pseudo
5858 removed inherit pseudo <- original pseudo
5860 removed_split_pseudo <- original_reg
5861 original_reg <- removed_split_pseudo */
5863 if (lra_dump_file != NULL)
5865 fprintf (lra_dump_file, " Removing %s:\n",
5866 bitmap_bit_p (&lra_split_regs, sregno)
5867 || bitmap_bit_p (&lra_split_regs, dregno)
5868 ? "split" : "inheritance");
5869 dump_insn_slim (lra_dump_file, curr_insn);
5871 lra_set_insn_deleted (curr_insn);
5872 done_p = true;
5874 else if (bitmap_bit_p (remove_pseudos, sregno)
5875 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5877 /* Search the following pattern:
5878 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5879 original_pseudo <- inherit_or_split_pseudo1
5880 where the 2nd insn is the current insn and
5881 inherit_or_split_pseudo2 is not removed. If it is found,
5882 change the current insn onto:
5883 original_pseudo <- inherit_or_split_pseudo2. */
5884 for (prev_insn = PREV_INSN (curr_insn);
5885 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5886 prev_insn = PREV_INSN (prev_insn))
5888 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5889 && (prev_set = single_set (prev_insn)) != NULL_RTX
5890 /* There should be no subregs in insn we are
5891 searching because only the original reg might
5892 be in subreg when we changed the mode of
5893 load/store for splitting. */
5894 && REG_P (SET_DEST (prev_set))
5895 && REG_P (SET_SRC (prev_set))
5896 && (int) REGNO (SET_DEST (prev_set)) == sregno
5897 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5898 >= FIRST_PSEUDO_REGISTER)
5899 /* As we consider chain of inheritance or
5900 splitting described in above comment we should
5901 check that sregno and prev_sregno were
5902 inheritance/split pseudos created from the
5903 same original regno. */
5904 && (lra_reg_info[sregno].restore_regno
5905 == lra_reg_info[prev_sregno].restore_regno)
5906 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5908 lra_assert (GET_MODE (SET_SRC (prev_set))
5909 == GET_MODE (regno_reg_rtx[sregno]));
5910 if (GET_CODE (SET_SRC (set)) == SUBREG)
5911 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5912 else
5913 SET_SRC (set) = SET_SRC (prev_set);
5914 /* As we are finishing with processing the insn
5915 here, check the destination too as it might
5916 inheritance pseudo for another pseudo. */
5917 if (bitmap_bit_p (remove_pseudos, dregno)
5918 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
5919 && (restore_regno
5920 = lra_reg_info[dregno].restore_regno) >= 0)
5922 if (GET_CODE (SET_DEST (set)) == SUBREG)
5923 SUBREG_REG (SET_DEST (set))
5924 = regno_reg_rtx[restore_regno];
5925 else
5926 SET_DEST (set) = regno_reg_rtx[restore_regno];
5928 lra_push_insn_and_update_insn_regno_info (curr_insn);
5929 lra_set_used_insn_alternative_by_uid
5930 (INSN_UID (curr_insn), -1);
5931 done_p = true;
5932 if (lra_dump_file != NULL)
5934 fprintf (lra_dump_file, " Change reload insn:\n");
5935 dump_insn_slim (lra_dump_file, curr_insn);
5940 if (! done_p)
5942 struct lra_insn_reg *reg;
5943 bool restored_regs_p = false;
5944 bool kept_regs_p = false;
5946 curr_id = lra_get_insn_recog_data (curr_insn);
5947 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5949 regno = reg->regno;
5950 restore_regno = lra_reg_info[regno].restore_regno;
5951 if (restore_regno >= 0)
5953 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5955 lra_substitute_pseudo_within_insn
5956 (curr_insn, regno, regno_reg_rtx[restore_regno],
5957 false);
5958 restored_regs_p = true;
5960 else
5961 kept_regs_p = true;
5964 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5966 /* The instruction has changed since the previous
5967 constraints pass. */
5968 lra_push_insn_and_update_insn_regno_info (curr_insn);
5969 lra_set_used_insn_alternative_by_uid
5970 (INSN_UID (curr_insn), -1);
5972 else if (restored_regs_p)
5973 /* The instruction has been restored to the form that
5974 it had during the previous constraints pass. */
5975 lra_update_insn_regno_info (curr_insn);
5976 if (restored_regs_p && lra_dump_file != NULL)
5978 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5979 dump_insn_slim (lra_dump_file, curr_insn);
5984 return change_p;
5987 /* If optional reload pseudos failed to get a hard register or was not
5988 inherited, it is better to remove optional reloads. We do this
5989 transformation after undoing inheritance to figure out necessity to
5990 remove optional reloads easier. Return true if we do any
5991 change. */
5992 static bool
5993 undo_optional_reloads (void)
5995 bool change_p, keep_p;
5996 unsigned int regno, uid;
5997 bitmap_iterator bi, bi2;
5998 rtx_insn *insn;
5999 rtx set, src, dest;
6000 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
6002 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
6003 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
6004 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6006 keep_p = false;
6007 /* Keep optional reloads from previous subpasses. */
6008 if (lra_reg_info[regno].restore_regno < 0
6009 /* If the original pseudo changed its allocation, just
6010 removing the optional pseudo is dangerous as the original
6011 pseudo will have longer live range. */
6012 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
6013 keep_p = true;
6014 else if (reg_renumber[regno] >= 0)
6015 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
6017 insn = lra_insn_recog_data[uid]->insn;
6018 if ((set = single_set (insn)) == NULL_RTX)
6019 continue;
6020 src = SET_SRC (set);
6021 dest = SET_DEST (set);
6022 if (! REG_P (src) || ! REG_P (dest))
6023 continue;
6024 if (REGNO (dest) == regno
6025 /* Ignore insn for optional reloads itself. */
6026 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
6027 /* Check only inheritance on last inheritance pass. */
6028 && (int) REGNO (src) >= new_regno_start
6029 /* Check that the optional reload was inherited. */
6030 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6032 keep_p = true;
6033 break;
6036 if (keep_p)
6038 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6039 if (lra_dump_file != NULL)
6040 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6043 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6044 bitmap_initialize (&insn_bitmap, &reg_obstack);
6045 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6047 if (lra_dump_file != NULL)
6048 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6049 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6050 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6052 insn = lra_insn_recog_data[uid]->insn;
6053 if ((set = single_set (insn)) != NULL_RTX)
6055 src = SET_SRC (set);
6056 dest = SET_DEST (set);
6057 if (REG_P (src) && REG_P (dest)
6058 && ((REGNO (src) == regno
6059 && (lra_reg_info[regno].restore_regno
6060 == (int) REGNO (dest)))
6061 || (REGNO (dest) == regno
6062 && (lra_reg_info[regno].restore_regno
6063 == (int) REGNO (src)))))
6065 if (lra_dump_file != NULL)
6067 fprintf (lra_dump_file, " Deleting move %u\n",
6068 INSN_UID (insn));
6069 dump_insn_slim (lra_dump_file, insn);
6071 lra_set_insn_deleted (insn);
6072 continue;
6074 /* We should not worry about generation memory-memory
6075 moves here as if the corresponding inheritance did
6076 not work (inheritance pseudo did not get a hard reg),
6077 we remove the inheritance pseudo and the optional
6078 reload. */
6080 lra_substitute_pseudo_within_insn
6081 (insn, regno, regno_reg_rtx[lra_reg_info[regno].restore_regno],
6082 false);
6083 lra_update_insn_regno_info (insn);
6084 if (lra_dump_file != NULL)
6086 fprintf (lra_dump_file,
6087 " Restoring original insn:\n");
6088 dump_insn_slim (lra_dump_file, insn);
6092 /* Clear restore_regnos. */
6093 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6094 lra_reg_info[regno].restore_regno = -1;
6095 bitmap_clear (&insn_bitmap);
6096 bitmap_clear (&removed_optional_reload_pseudos);
6097 return change_p;
6100 /* Entry function for undoing inheritance/split transformation. Return true
6101 if we did any RTL change in this pass. */
6102 bool
6103 lra_undo_inheritance (void)
6105 unsigned int regno;
6106 int restore_regno, hard_regno;
6107 int n_all_inherit, n_inherit, n_all_split, n_split;
6108 bitmap_head remove_pseudos;
6109 bitmap_iterator bi;
6110 bool change_p;
6112 lra_undo_inheritance_iter++;
6113 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6114 return false;
6115 if (lra_dump_file != NULL)
6116 fprintf (lra_dump_file,
6117 "\n********** Undoing inheritance #%d: **********\n\n",
6118 lra_undo_inheritance_iter);
6119 bitmap_initialize (&remove_pseudos, &reg_obstack);
6120 n_inherit = n_all_inherit = 0;
6121 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6122 if (lra_reg_info[regno].restore_regno >= 0)
6124 n_all_inherit++;
6125 if (reg_renumber[regno] < 0
6126 /* If the original pseudo changed its allocation, just
6127 removing inheritance is dangerous as for changing
6128 allocation we used shorter live-ranges. */
6129 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6130 bitmap_set_bit (&remove_pseudos, regno);
6131 else
6132 n_inherit++;
6134 if (lra_dump_file != NULL && n_all_inherit != 0)
6135 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6136 n_inherit, n_all_inherit,
6137 (double) n_inherit / n_all_inherit * 100);
6138 n_split = n_all_split = 0;
6139 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6140 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6142 n_all_split++;
6143 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6144 ? reg_renumber[restore_regno] : restore_regno);
6145 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6146 bitmap_set_bit (&remove_pseudos, regno);
6147 else
6149 n_split++;
6150 if (lra_dump_file != NULL)
6151 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6152 regno, restore_regno);
6155 if (lra_dump_file != NULL && n_all_split != 0)
6156 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6157 n_split, n_all_split,
6158 (double) n_split / n_all_split * 100);
6159 change_p = remove_inheritance_pseudos (&remove_pseudos);
6160 bitmap_clear (&remove_pseudos);
6161 /* Clear restore_regnos. */
6162 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6163 lra_reg_info[regno].restore_regno = -1;
6164 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6165 lra_reg_info[regno].restore_regno = -1;
6166 change_p = undo_optional_reloads () || change_p;
6167 return change_p;