2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / lra-constraints.c
blob898dbecb7dbbfc665775687ac197cd916af23d88
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2015 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains code for 3 passes: constraint pass,
23 inheritance/split pass, and pass for undoing failed inheritance and
24 split.
26 The major goal of constraint pass is to transform RTL to satisfy
27 insn and address constraints by:
28 o choosing insn alternatives;
29 o generating *reload insns* (or reloads in brief) and *reload
30 pseudos* which will get necessary hard registers later;
31 o substituting pseudos with equivalent values and removing the
32 instructions that initialized those pseudos.
34 The constraint pass has biggest and most complicated code in LRA.
35 There are a lot of important details like:
36 o reuse of input reload pseudos to simplify reload pseudo
37 allocations;
38 o some heuristics to choose insn alternative to improve the
39 inheritance;
40 o early clobbers etc.
42 The pass is mimicking former reload pass in alternative choosing
43 because the reload pass is oriented to current machine description
44 model. It might be changed if the machine description model is
45 changed.
47 There is special code for preventing all LRA and this pass cycling
48 in case of bugs.
50 On the first iteration of the pass we process every instruction and
51 choose an alternative for each one. On subsequent iterations we try
52 to avoid reprocessing instructions if we can be sure that the old
53 choice is still valid.
55 The inheritance/spilt pass is to transform code to achieve
56 ineheritance and live range splitting. It is done on backward
57 traversal of EBBs.
59 The inheritance optimization goal is to reuse values in hard
60 registers. There is analogous optimization in old reload pass. The
61 inheritance is achieved by following transformation:
63 reload_p1 <- p reload_p1 <- p
64 ... new_p <- reload_p1
65 ... => ...
66 reload_p2 <- p reload_p2 <- new_p
68 where p is spilled and not changed between the insns. Reload_p1 is
69 also called *original pseudo* and new_p is called *inheritance
70 pseudo*.
72 The subsequent assignment pass will try to assign the same (or
73 another if it is not possible) hard register to new_p as to
74 reload_p1 or reload_p2.
76 If the assignment pass fails to assign a hard register to new_p,
77 this file will undo the inheritance and restore the original code.
78 This is because implementing the above sequence with a spilled
79 new_p would make the code much worse. The inheritance is done in
80 EBB scope. The above is just a simplified example to get an idea
81 of the inheritance as the inheritance is also done for non-reload
82 insns.
84 Splitting (transformation) is also done in EBB scope on the same
85 pass as the inheritance:
87 r <- ... or ... <- r r <- ... or ... <- r
88 ... s <- r (new insn -- save)
89 ... =>
90 ... r <- s (new insn -- restore)
91 ... <- r ... <- r
93 The *split pseudo* s is assigned to the hard register of the
94 original pseudo or hard register r.
96 Splitting is done:
97 o In EBBs with high register pressure for global pseudos (living
98 in at least 2 BBs) and assigned to hard registers when there
99 are more one reloads needing the hard registers;
100 o for pseudos needing save/restore code around calls.
102 If the split pseudo still has the same hard register as the
103 original pseudo after the subsequent assignment pass or the
104 original pseudo was split, the opposite transformation is done on
105 the same pass for undoing inheritance. */
107 #undef REG_OK_STRICT
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "tm.h"
113 #include "hard-reg-set.h"
114 #include "rtl.h"
115 #include "tm_p.h"
116 #include "regs.h"
117 #include "insn-config.h"
118 #include "insn-codes.h"
119 #include "recog.h"
120 #include "output.h"
121 #include "addresses.h"
122 #include "target.h"
123 #include "input.h"
124 #include "function.h"
125 #include "symtab.h"
126 #include "flags.h"
127 #include "alias.h"
128 #include "tree.h"
129 #include "expmed.h"
130 #include "dojump.h"
131 #include "explow.h"
132 #include "calls.h"
133 #include "emit-rtl.h"
134 #include "varasm.h"
135 #include "stmt.h"
136 #include "expr.h"
137 #include "predict.h"
138 #include "dominance.h"
139 #include "cfg.h"
140 #include "cfgrtl.h"
141 #include "basic-block.h"
142 #include "except.h"
143 #include "optabs.h"
144 #include "df.h"
145 #include "ira.h"
146 #include "rtl-error.h"
147 #include "params.h"
148 #include "lra-int.h"
150 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
151 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
152 reload insns. */
153 static int bb_reload_num;
155 /* The current insn being processed and corresponding its single set
156 (NULL otherwise), its data (basic block, the insn data, the insn
157 static data, and the mode of each operand). */
158 static rtx_insn *curr_insn;
159 static rtx curr_insn_set;
160 static basic_block curr_bb;
161 static lra_insn_recog_data_t curr_id;
162 static struct lra_static_insn_data *curr_static_id;
163 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
164 /* Mode of the register substituted by its equivalence with VOIDmode
165 (e.g. constant) and whose subreg is given operand of the current
166 insn. VOIDmode in all other cases. */
167 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
171 /* Start numbers for new registers and insns at the current constraints
172 pass start. */
173 static int new_regno_start;
174 static int new_insn_uid_start;
176 /* If LOC is nonnull, strip any outer subreg from it. */
177 static inline rtx *
178 strip_subreg (rtx *loc)
180 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
183 /* Return hard regno of REGNO or if it is was not assigned to a hard
184 register, use a hard register from its allocno class. */
185 static int
186 get_try_hard_regno (int regno)
188 int hard_regno;
189 enum reg_class rclass;
191 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
192 hard_regno = lra_get_regno_hard_regno (regno);
193 if (hard_regno >= 0)
194 return hard_regno;
195 rclass = lra_get_allocno_class (regno);
196 if (rclass == NO_REGS)
197 return -1;
198 return ira_class_hard_regs[rclass][0];
201 /* Return final hard regno (plus offset) which will be after
202 elimination. We do this for matching constraints because the final
203 hard regno could have a different class. */
204 static int
205 get_final_hard_regno (int hard_regno, int offset)
207 if (hard_regno < 0)
208 return hard_regno;
209 hard_regno = lra_get_elimination_hard_regno (hard_regno);
210 return hard_regno + offset;
213 /* Return hard regno of X after removing subreg and making
214 elimination. If X is not a register or subreg of register, return
215 -1. For pseudo use its assignment. */
216 static int
217 get_hard_regno (rtx x)
219 rtx reg;
220 int offset, hard_regno;
222 reg = x;
223 if (GET_CODE (x) == SUBREG)
224 reg = SUBREG_REG (x);
225 if (! REG_P (reg))
226 return -1;
227 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
228 hard_regno = lra_get_regno_hard_regno (hard_regno);
229 if (hard_regno < 0)
230 return -1;
231 offset = 0;
232 if (GET_CODE (x) == SUBREG)
233 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
234 SUBREG_BYTE (x), GET_MODE (x));
235 return get_final_hard_regno (hard_regno, offset);
238 /* If REGNO is a hard register or has been allocated a hard register,
239 return the class of that register. If REGNO is a reload pseudo
240 created by the current constraints pass, return its allocno class.
241 Return NO_REGS otherwise. */
242 static enum reg_class
243 get_reg_class (int regno)
245 int hard_regno;
247 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
248 hard_regno = lra_get_regno_hard_regno (regno);
249 if (hard_regno >= 0)
251 hard_regno = get_final_hard_regno (hard_regno, 0);
252 return REGNO_REG_CLASS (hard_regno);
254 if (regno >= new_regno_start)
255 return lra_get_allocno_class (regno);
256 return NO_REGS;
259 /* Return true if REG satisfies (or will satisfy) reg class constraint
260 CL. Use elimination first if REG is a hard register. If REG is a
261 reload pseudo created by this constraints pass, assume that it will
262 be allocated a hard register from its allocno class, but allow that
263 class to be narrowed to CL if it is currently a superset of CL.
265 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
266 REGNO (reg), or NO_REGS if no change in its class was needed. */
267 static bool
268 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
270 enum reg_class rclass, common_class;
271 machine_mode reg_mode;
272 int class_size, hard_regno, nregs, i, j;
273 int regno = REGNO (reg);
275 if (new_class != NULL)
276 *new_class = NO_REGS;
277 if (regno < FIRST_PSEUDO_REGISTER)
279 rtx final_reg = reg;
280 rtx *final_loc = &final_reg;
282 lra_eliminate_reg_if_possible (final_loc);
283 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
285 reg_mode = GET_MODE (reg);
286 rclass = get_reg_class (regno);
287 if (regno < new_regno_start
288 /* Do not allow the constraints for reload instructions to
289 influence the classes of new pseudos. These reloads are
290 typically moves that have many alternatives, and restricting
291 reload pseudos for one alternative may lead to situations
292 where other reload pseudos are no longer allocatable. */
293 || (INSN_UID (curr_insn) >= new_insn_uid_start
294 && curr_insn_set != NULL
295 && ((OBJECT_P (SET_SRC (curr_insn_set))
296 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
297 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
298 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
299 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
300 /* When we don't know what class will be used finally for reload
301 pseudos, we use ALL_REGS. */
302 return ((regno >= new_regno_start && rclass == ALL_REGS)
303 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
304 && ! hard_reg_set_subset_p (reg_class_contents[cl],
305 lra_no_alloc_regs)));
306 else
308 common_class = ira_reg_class_subset[rclass][cl];
309 if (new_class != NULL)
310 *new_class = common_class;
311 if (hard_reg_set_subset_p (reg_class_contents[common_class],
312 lra_no_alloc_regs))
313 return false;
314 /* Check that there are enough allocatable regs. */
315 class_size = ira_class_hard_regs_num[common_class];
316 for (i = 0; i < class_size; i++)
318 hard_regno = ira_class_hard_regs[common_class][i];
319 nregs = hard_regno_nregs[hard_regno][reg_mode];
320 if (nregs == 1)
321 return true;
322 for (j = 0; j < nregs; j++)
323 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
324 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
325 hard_regno + j))
326 break;
327 if (j >= nregs)
328 return true;
330 return false;
334 /* Return true if REGNO satisfies a memory constraint. */
335 static bool
336 in_mem_p (int regno)
338 return get_reg_class (regno) == NO_REGS;
341 /* Return 1 if ADDR is a valid memory address for mode MODE in address
342 space AS, and check that each pseudo has the proper kind of hard
343 reg. */
344 static int
345 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
346 rtx addr, addr_space_t as)
348 #ifdef GO_IF_LEGITIMATE_ADDRESS
349 lra_assert (ADDR_SPACE_GENERIC_P (as));
350 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
351 return 0;
353 win:
354 return 1;
355 #else
356 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
357 #endif
360 namespace {
361 /* Temporarily eliminates registers in an address (for the lifetime of
362 the object). */
363 class address_eliminator {
364 public:
365 address_eliminator (struct address_info *ad);
366 ~address_eliminator ();
368 private:
369 struct address_info *m_ad;
370 rtx *m_base_loc;
371 rtx m_base_reg;
372 rtx *m_index_loc;
373 rtx m_index_reg;
377 address_eliminator::address_eliminator (struct address_info *ad)
378 : m_ad (ad),
379 m_base_loc (strip_subreg (ad->base_term)),
380 m_base_reg (NULL_RTX),
381 m_index_loc (strip_subreg (ad->index_term)),
382 m_index_reg (NULL_RTX)
384 if (m_base_loc != NULL)
386 m_base_reg = *m_base_loc;
387 lra_eliminate_reg_if_possible (m_base_loc);
388 if (m_ad->base_term2 != NULL)
389 *m_ad->base_term2 = *m_ad->base_term;
391 if (m_index_loc != NULL)
393 m_index_reg = *m_index_loc;
394 lra_eliminate_reg_if_possible (m_index_loc);
398 address_eliminator::~address_eliminator ()
400 if (m_base_loc && *m_base_loc != m_base_reg)
402 *m_base_loc = m_base_reg;
403 if (m_ad->base_term2 != NULL)
404 *m_ad->base_term2 = *m_ad->base_term;
406 if (m_index_loc && *m_index_loc != m_index_reg)
407 *m_index_loc = m_index_reg;
410 /* Return true if the eliminated form of AD is a legitimate target address. */
411 static bool
412 valid_address_p (struct address_info *ad)
414 address_eliminator eliminator (ad);
415 return valid_address_p (ad->mode, *ad->outer, ad->as);
418 /* Return true if the eliminated form of memory reference OP satisfies
419 extra memory constraint CONSTRAINT. */
420 static bool
421 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
423 struct address_info ad;
425 decompose_mem_address (&ad, op);
426 address_eliminator eliminator (&ad);
427 return constraint_satisfied_p (op, constraint);
430 /* Return true if the eliminated form of address AD satisfies extra
431 address constraint CONSTRAINT. */
432 static bool
433 satisfies_address_constraint_p (struct address_info *ad,
434 enum constraint_num constraint)
436 address_eliminator eliminator (ad);
437 return constraint_satisfied_p (*ad->outer, constraint);
440 /* Return true if the eliminated form of address OP satisfies extra
441 address constraint CONSTRAINT. */
442 static bool
443 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
445 struct address_info ad;
447 decompose_lea_address (&ad, &op);
448 return satisfies_address_constraint_p (&ad, constraint);
451 /* Initiate equivalences for LRA. As we keep original equivalences
452 before any elimination, we need to make copies otherwise any change
453 in insns might change the equivalences. */
454 void
455 lra_init_equiv (void)
457 ira_expand_reg_equiv ();
458 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
460 rtx res;
462 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
463 ira_reg_equiv[i].memory = copy_rtx (res);
464 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
465 ira_reg_equiv[i].invariant = copy_rtx (res);
469 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
471 /* Update equivalence for REGNO. We need to this as the equivalence
472 might contain other pseudos which are changed by their
473 equivalences. */
474 static void
475 update_equiv (int regno)
477 rtx x;
479 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
480 ira_reg_equiv[regno].memory
481 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
482 NULL_RTX);
483 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
484 ira_reg_equiv[regno].invariant
485 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
486 NULL_RTX);
489 /* If we have decided to substitute X with another value, return that
490 value, otherwise return X. */
491 static rtx
492 get_equiv (rtx x)
494 int regno;
495 rtx res;
497 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
498 || ! ira_reg_equiv[regno].defined_p
499 || ! ira_reg_equiv[regno].profitable_p
500 || lra_get_regno_hard_regno (regno) >= 0)
501 return x;
502 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
504 if (targetm.cannot_substitute_mem_equiv_p (res))
505 return x;
506 return res;
508 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
509 return res;
510 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
511 return res;
512 gcc_unreachable ();
515 /* If we have decided to substitute X with the equivalent value,
516 return that value after elimination for INSN, otherwise return
517 X. */
518 static rtx
519 get_equiv_with_elimination (rtx x, rtx_insn *insn)
521 rtx res = get_equiv (x);
523 if (x == res || CONSTANT_P (res))
524 return res;
525 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
526 false, false, 0, true);
529 /* Set up curr_operand_mode. */
530 static void
531 init_curr_operand_mode (void)
533 int nop = curr_static_id->n_operands;
534 for (int i = 0; i < nop; i++)
536 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
537 if (mode == VOIDmode)
539 /* The .md mode for address operands is the mode of the
540 addressed value rather than the mode of the address itself. */
541 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
542 mode = Pmode;
543 else
544 mode = curr_static_id->operand[i].mode;
546 curr_operand_mode[i] = mode;
552 /* The page contains code to reuse input reloads. */
554 /* Structure describes input reload of the current insns. */
555 struct input_reload
557 /* Reloaded value. */
558 rtx input;
559 /* Reload pseudo used. */
560 rtx reg;
563 /* The number of elements in the following array. */
564 static int curr_insn_input_reloads_num;
565 /* Array containing info about input reloads. It is used to find the
566 same input reload and reuse the reload pseudo in this case. */
567 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
569 /* Initiate data concerning reuse of input reloads for the current
570 insn. */
571 static void
572 init_curr_insn_input_reloads (void)
574 curr_insn_input_reloads_num = 0;
577 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
578 created input reload pseudo (only if TYPE is not OP_OUT). Don't
579 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
580 wrapped up in SUBREG. The result pseudo is returned through
581 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
582 reused the already created input reload pseudo. Use TITLE to
583 describe new registers for debug purposes. */
584 static bool
585 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
586 enum reg_class rclass, bool in_subreg_p,
587 const char *title, rtx *result_reg)
589 int i, regno;
590 enum reg_class new_class;
592 if (type == OP_OUT)
594 *result_reg
595 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
596 return true;
598 /* Prevent reuse value of expression with side effects,
599 e.g. volatile memory. */
600 if (! side_effects_p (original))
601 for (i = 0; i < curr_insn_input_reloads_num; i++)
602 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
603 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
605 rtx reg = curr_insn_input_reloads[i].reg;
606 regno = REGNO (reg);
607 /* If input is equal to original and both are VOIDmode,
608 GET_MODE (reg) might be still different from mode.
609 Ensure we don't return *result_reg with wrong mode. */
610 if (GET_MODE (reg) != mode)
612 if (in_subreg_p)
613 continue;
614 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
615 continue;
616 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
617 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
618 continue;
620 *result_reg = reg;
621 if (lra_dump_file != NULL)
623 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
624 dump_value_slim (lra_dump_file, original, 1);
626 if (new_class != lra_get_allocno_class (regno))
627 lra_change_class (regno, new_class, ", change to", false);
628 if (lra_dump_file != NULL)
629 fprintf (lra_dump_file, "\n");
630 return false;
632 *result_reg = lra_create_new_reg (mode, original, rclass, title);
633 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
634 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
635 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
636 return true;
641 /* The page contains code to extract memory address parts. */
643 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
644 static inline bool
645 ok_for_index_p_nonstrict (rtx reg)
647 unsigned regno = REGNO (reg);
649 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
652 /* A version of regno_ok_for_base_p for use here, when all pseudos
653 should count as OK. Arguments as for regno_ok_for_base_p. */
654 static inline bool
655 ok_for_base_p_nonstrict (rtx reg, machine_mode mode, addr_space_t as,
656 enum rtx_code outer_code, enum rtx_code index_code)
658 unsigned regno = REGNO (reg);
660 if (regno >= FIRST_PSEUDO_REGISTER)
661 return true;
662 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
667 /* The page contains major code to choose the current insn alternative
668 and generate reloads for it. */
670 /* Return the offset from REGNO of the least significant register
671 in (reg:MODE REGNO).
673 This function is used to tell whether two registers satisfy
674 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
676 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
677 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
679 lra_constraint_offset (int regno, machine_mode mode)
681 lra_assert (regno < FIRST_PSEUDO_REGISTER);
682 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
683 && SCALAR_INT_MODE_P (mode))
684 return hard_regno_nregs[regno][mode] - 1;
685 return 0;
688 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
689 if they are the same hard reg, and has special hacks for
690 auto-increment and auto-decrement. This is specifically intended for
691 process_alt_operands to use in determining whether two operands
692 match. X is the operand whose number is the lower of the two.
694 It is supposed that X is the output operand and Y is the input
695 operand. Y_HARD_REGNO is the final hard regno of register Y or
696 register in subreg Y as we know it now. Otherwise, it is a
697 negative value. */
698 static bool
699 operands_match_p (rtx x, rtx y, int y_hard_regno)
701 int i;
702 RTX_CODE code = GET_CODE (x);
703 const char *fmt;
705 if (x == y)
706 return true;
707 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
708 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
710 int j;
712 i = get_hard_regno (x);
713 if (i < 0)
714 goto slow;
716 if ((j = y_hard_regno) < 0)
717 goto slow;
719 i += lra_constraint_offset (i, GET_MODE (x));
720 j += lra_constraint_offset (j, GET_MODE (y));
722 return i == j;
725 /* If two operands must match, because they are really a single
726 operand of an assembler insn, then two post-increments are invalid
727 because the assembler insn would increment only once. On the
728 other hand, a post-increment matches ordinary indexing if the
729 post-increment is the output operand. */
730 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
731 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
733 /* Two pre-increments are invalid because the assembler insn would
734 increment only once. On the other hand, a pre-increment matches
735 ordinary indexing if the pre-increment is the input operand. */
736 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
737 || GET_CODE (y) == PRE_MODIFY)
738 return operands_match_p (x, XEXP (y, 0), -1);
740 slow:
742 if (code == REG && REG_P (y))
743 return REGNO (x) == REGNO (y);
745 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
746 && x == SUBREG_REG (y))
747 return true;
748 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
749 && SUBREG_REG (x) == y)
750 return true;
752 /* Now we have disposed of all the cases in which different rtx
753 codes can match. */
754 if (code != GET_CODE (y))
755 return false;
757 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
758 if (GET_MODE (x) != GET_MODE (y))
759 return false;
761 switch (code)
763 CASE_CONST_UNIQUE:
764 return false;
766 case LABEL_REF:
767 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
768 case SYMBOL_REF:
769 return XSTR (x, 0) == XSTR (y, 0);
771 default:
772 break;
775 /* Compare the elements. If any pair of corresponding elements fail
776 to match, return false for the whole things. */
778 fmt = GET_RTX_FORMAT (code);
779 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
781 int val, j;
782 switch (fmt[i])
784 case 'w':
785 if (XWINT (x, i) != XWINT (y, i))
786 return false;
787 break;
789 case 'i':
790 if (XINT (x, i) != XINT (y, i))
791 return false;
792 break;
794 case 'e':
795 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
796 if (val == 0)
797 return false;
798 break;
800 case '0':
801 break;
803 case 'E':
804 if (XVECLEN (x, i) != XVECLEN (y, i))
805 return false;
806 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
808 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
809 if (val == 0)
810 return false;
812 break;
814 /* It is believed that rtx's at this level will never
815 contain anything but integers and other rtx's, except for
816 within LABEL_REFs and SYMBOL_REFs. */
817 default:
818 gcc_unreachable ();
821 return true;
824 /* True if X is a constant that can be forced into the constant pool.
825 MODE is the mode of the operand, or VOIDmode if not known. */
826 #define CONST_POOL_OK_P(MODE, X) \
827 ((MODE) != VOIDmode \
828 && CONSTANT_P (X) \
829 && GET_CODE (X) != HIGH \
830 && !targetm.cannot_force_const_mem (MODE, X))
832 /* True if C is a non-empty register class that has too few registers
833 to be safely used as a reload target class. */
834 #define SMALL_REGISTER_CLASS_P(C) \
835 (ira_class_hard_regs_num [(C)] == 1 \
836 || (ira_class_hard_regs_num [(C)] >= 1 \
837 && targetm.class_likely_spilled_p (C)))
839 /* If REG is a reload pseudo, try to make its class satisfying CL. */
840 static void
841 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
843 enum reg_class rclass;
845 /* Do not make more accurate class from reloads generated. They are
846 mostly moves with a lot of constraints. Making more accurate
847 class may results in very narrow class and impossibility of find
848 registers for several reloads of one insn. */
849 if (INSN_UID (curr_insn) >= new_insn_uid_start)
850 return;
851 if (GET_CODE (reg) == SUBREG)
852 reg = SUBREG_REG (reg);
853 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
854 return;
855 if (in_class_p (reg, cl, &rclass) && rclass != cl)
856 lra_change_class (REGNO (reg), rclass, " Change to", true);
859 /* Generate reloads for matching OUT and INS (array of input operand
860 numbers with end marker -1) with reg class GOAL_CLASS. Add input
861 and output reloads correspondingly to the lists *BEFORE and *AFTER.
862 OUT might be negative. In this case we generate input reloads for
863 matched input operands INS. */
864 static void
865 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
866 rtx_insn **before, rtx_insn **after)
868 int i, in;
869 rtx new_in_reg, new_out_reg, reg;
870 machine_mode inmode, outmode;
871 rtx in_rtx = *curr_id->operand_loc[ins[0]];
872 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
874 inmode = curr_operand_mode[ins[0]];
875 outmode = out < 0 ? inmode : curr_operand_mode[out];
876 push_to_sequence (*before);
877 if (inmode != outmode)
879 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
881 reg = new_in_reg
882 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
883 goal_class, "");
884 if (SCALAR_INT_MODE_P (inmode))
885 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
886 else
887 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
888 LRA_SUBREG_P (new_out_reg) = 1;
889 /* If the input reg is dying here, we can use the same hard
890 register for REG and IN_RTX. We do it only for original
891 pseudos as reload pseudos can die although original
892 pseudos still live where reload pseudos dies. */
893 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
894 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
895 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
897 else
899 reg = new_out_reg
900 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
901 goal_class, "");
902 if (SCALAR_INT_MODE_P (outmode))
903 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
904 else
905 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
906 /* NEW_IN_REG is non-paradoxical subreg. We don't want
907 NEW_OUT_REG living above. We add clobber clause for
908 this. This is just a temporary clobber. We can remove
909 it at the end of LRA work. */
910 rtx_insn *clobber = emit_clobber (new_out_reg);
911 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
912 LRA_SUBREG_P (new_in_reg) = 1;
913 if (GET_CODE (in_rtx) == SUBREG)
915 rtx subreg_reg = SUBREG_REG (in_rtx);
917 /* If SUBREG_REG is dying here and sub-registers IN_RTX
918 and NEW_IN_REG are similar, we can use the same hard
919 register for REG and SUBREG_REG. */
920 if (REG_P (subreg_reg)
921 && (int) REGNO (subreg_reg) < lra_new_regno_start
922 && GET_MODE (subreg_reg) == outmode
923 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
924 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
925 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
929 else
931 /* Pseudos have values -- see comments for lra_reg_info.
932 Different pseudos with the same value do not conflict even if
933 they live in the same place. When we create a pseudo we
934 assign value of original pseudo (if any) from which we
935 created the new pseudo. If we create the pseudo from the
936 input pseudo, the new pseudo will no conflict with the input
937 pseudo which is wrong when the input pseudo lives after the
938 insn and as the new pseudo value is changed by the insn
939 output. Therefore we create the new pseudo from the output.
941 We cannot reuse the current output register because we might
942 have a situation like "a <- a op b", where the constraints
943 force the second input operand ("b") to match the output
944 operand ("a"). "b" must then be copied into a new register
945 so that it doesn't clobber the current value of "a". */
947 new_in_reg = new_out_reg
948 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
949 goal_class, "");
951 /* In operand can be got from transformations before processing insn
952 constraints. One example of such transformations is subreg
953 reloading (see function simplify_operand_subreg). The new
954 pseudos created by the transformations might have inaccurate
955 class (ALL_REGS) and we should make their classes more
956 accurate. */
957 narrow_reload_pseudo_class (in_rtx, goal_class);
958 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
959 *before = get_insns ();
960 end_sequence ();
961 for (i = 0; (in = ins[i]) >= 0; i++)
963 lra_assert
964 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
965 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
966 *curr_id->operand_loc[in] = new_in_reg;
968 lra_update_dups (curr_id, ins);
969 if (out < 0)
970 return;
971 /* See a comment for the input operand above. */
972 narrow_reload_pseudo_class (out_rtx, goal_class);
973 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
975 start_sequence ();
976 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
977 emit_insn (*after);
978 *after = get_insns ();
979 end_sequence ();
981 *curr_id->operand_loc[out] = new_out_reg;
982 lra_update_dup (curr_id, out);
985 /* Return register class which is union of all reg classes in insn
986 constraint alternative string starting with P. */
987 static enum reg_class
988 reg_class_from_constraints (const char *p)
990 int c, len;
991 enum reg_class op_class = NO_REGS;
994 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
996 case '#':
997 case ',':
998 return op_class;
1000 case 'g':
1001 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1002 break;
1004 default:
1005 enum constraint_num cn = lookup_constraint (p);
1006 enum reg_class cl = reg_class_for_constraint (cn);
1007 if (cl == NO_REGS)
1009 if (insn_extra_address_constraint (cn))
1010 op_class
1011 = (reg_class_subunion
1012 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1013 ADDRESS, SCRATCH)]);
1014 break;
1017 op_class = reg_class_subunion[op_class][cl];
1018 break;
1020 while ((p += len), c);
1021 return op_class;
1024 /* If OP is a register, return the class of the register as per
1025 get_reg_class, otherwise return NO_REGS. */
1026 static inline enum reg_class
1027 get_op_class (rtx op)
1029 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1032 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1033 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1034 SUBREG for VAL to make them equal. */
1035 static rtx_insn *
1036 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1038 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1040 /* Usually size of mem_pseudo is greater than val size but in
1041 rare cases it can be less as it can be defined by target
1042 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1043 if (! MEM_P (val))
1045 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1046 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1048 LRA_SUBREG_P (val) = 1;
1050 else
1052 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1053 LRA_SUBREG_P (mem_pseudo) = 1;
1056 return to_p ? gen_move_insn (mem_pseudo, val)
1057 : gen_move_insn (val, mem_pseudo);
1060 /* Process a special case insn (register move), return true if we
1061 don't need to process it anymore. INSN should be a single set
1062 insn. Set up that RTL was changed through CHANGE_P and macro
1063 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1064 SEC_MEM_P. */
1065 static bool
1066 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1068 int sregno, dregno;
1069 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1070 rtx_insn *before;
1071 enum reg_class dclass, sclass, secondary_class;
1072 secondary_reload_info sri;
1074 lra_assert (curr_insn_set != NULL_RTX);
1075 dreg = dest = SET_DEST (curr_insn_set);
1076 sreg = src = SET_SRC (curr_insn_set);
1077 if (GET_CODE (dest) == SUBREG)
1078 dreg = SUBREG_REG (dest);
1079 if (GET_CODE (src) == SUBREG)
1080 sreg = SUBREG_REG (src);
1081 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1082 return false;
1083 sclass = dclass = NO_REGS;
1084 if (REG_P (dreg))
1085 dclass = get_reg_class (REGNO (dreg));
1086 if (dclass == ALL_REGS)
1087 /* ALL_REGS is used for new pseudos created by transformations
1088 like reload of SUBREG_REG (see function
1089 simplify_operand_subreg). We don't know their class yet. We
1090 should figure out the class from processing the insn
1091 constraints not in this fast path function. Even if ALL_REGS
1092 were a right class for the pseudo, secondary_... hooks usually
1093 are not define for ALL_REGS. */
1094 return false;
1095 if (REG_P (sreg))
1096 sclass = get_reg_class (REGNO (sreg));
1097 if (sclass == ALL_REGS)
1098 /* See comments above. */
1099 return false;
1100 if (sclass == NO_REGS && dclass == NO_REGS)
1101 return false;
1102 #ifdef SECONDARY_MEMORY_NEEDED
1103 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1104 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1105 && ((sclass != NO_REGS && dclass != NO_REGS)
1106 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1107 #endif
1110 *sec_mem_p = true;
1111 return false;
1113 #endif
1114 if (! REG_P (dreg) || ! REG_P (sreg))
1115 return false;
1116 sri.prev_sri = NULL;
1117 sri.icode = CODE_FOR_nothing;
1118 sri.extra_cost = 0;
1119 secondary_class = NO_REGS;
1120 /* Set up hard register for a reload pseudo for hook
1121 secondary_reload because some targets just ignore unassigned
1122 pseudos in the hook. */
1123 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1125 dregno = REGNO (dreg);
1126 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1128 else
1129 dregno = -1;
1130 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1132 sregno = REGNO (sreg);
1133 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1135 else
1136 sregno = -1;
1137 if (sclass != NO_REGS)
1138 secondary_class
1139 = (enum reg_class) targetm.secondary_reload (false, dest,
1140 (reg_class_t) sclass,
1141 GET_MODE (src), &sri);
1142 if (sclass == NO_REGS
1143 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1144 && dclass != NO_REGS))
1146 enum reg_class old_sclass = secondary_class;
1147 secondary_reload_info old_sri = sri;
1149 sri.prev_sri = NULL;
1150 sri.icode = CODE_FOR_nothing;
1151 sri.extra_cost = 0;
1152 secondary_class
1153 = (enum reg_class) targetm.secondary_reload (true, src,
1154 (reg_class_t) dclass,
1155 GET_MODE (src), &sri);
1156 /* Check the target hook consistency. */
1157 lra_assert
1158 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1159 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1160 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1162 if (sregno >= 0)
1163 reg_renumber [sregno] = -1;
1164 if (dregno >= 0)
1165 reg_renumber [dregno] = -1;
1166 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1167 return false;
1168 *change_p = true;
1169 new_reg = NULL_RTX;
1170 if (secondary_class != NO_REGS)
1171 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1172 secondary_class,
1173 "secondary");
1174 start_sequence ();
1175 if (sri.icode == CODE_FOR_nothing)
1176 lra_emit_move (new_reg, src);
1177 else
1179 enum reg_class scratch_class;
1181 scratch_class = (reg_class_from_constraints
1182 (insn_data[sri.icode].operand[2].constraint));
1183 scratch_reg = (lra_create_new_reg_with_unique_value
1184 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1185 scratch_class, "scratch"));
1186 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1187 src, scratch_reg));
1189 before = get_insns ();
1190 end_sequence ();
1191 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1192 if (new_reg != NULL_RTX)
1193 SET_SRC (curr_insn_set) = new_reg;
1194 else
1196 if (lra_dump_file != NULL)
1198 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1199 dump_insn_slim (lra_dump_file, curr_insn);
1201 lra_set_insn_deleted (curr_insn);
1202 return true;
1204 return false;
1207 /* The following data describe the result of process_alt_operands.
1208 The data are used in curr_insn_transform to generate reloads. */
1210 /* The chosen reg classes which should be used for the corresponding
1211 operands. */
1212 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1213 /* True if the operand should be the same as another operand and that
1214 other operand does not need a reload. */
1215 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1216 /* True if the operand does not need a reload. */
1217 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1218 /* True if the operand can be offsetable memory. */
1219 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1220 /* The number of an operand to which given operand can be matched to. */
1221 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1222 /* The number of elements in the following array. */
1223 static int goal_alt_dont_inherit_ops_num;
1224 /* Numbers of operands whose reload pseudos should not be inherited. */
1225 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1226 /* True if the insn commutative operands should be swapped. */
1227 static bool goal_alt_swapped;
1228 /* The chosen insn alternative. */
1229 static int goal_alt_number;
1231 /* The following five variables are used to choose the best insn
1232 alternative. They reflect final characteristics of the best
1233 alternative. */
1235 /* Number of necessary reloads and overall cost reflecting the
1236 previous value and other unpleasantness of the best alternative. */
1237 static int best_losers, best_overall;
1238 /* Overall number hard registers used for reloads. For example, on
1239 some targets we need 2 general registers to reload DFmode and only
1240 one floating point register. */
1241 static int best_reload_nregs;
1242 /* Overall number reflecting distances of previous reloading the same
1243 value. The distances are counted from the current BB start. It is
1244 used to improve inheritance chances. */
1245 static int best_reload_sum;
1247 /* True if the current insn should have no correspondingly input or
1248 output reloads. */
1249 static bool no_input_reloads_p, no_output_reloads_p;
1251 /* True if we swapped the commutative operands in the current
1252 insn. */
1253 static int curr_swapped;
1255 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1256 register of class CL. Add any input reloads to list BEFORE. AFTER
1257 is nonnull if *LOC is an automodified value; handle that case by
1258 adding the required output reloads to list AFTER. Return true if
1259 the RTL was changed.
1261 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1262 register. Return false if the address register is correct. */
1263 static bool
1264 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1265 enum reg_class cl)
1267 int regno;
1268 enum reg_class rclass, new_class;
1269 rtx reg;
1270 rtx new_reg;
1271 machine_mode mode;
1272 bool subreg_p, before_p = false;
1274 subreg_p = GET_CODE (*loc) == SUBREG;
1275 if (subreg_p)
1276 loc = &SUBREG_REG (*loc);
1277 reg = *loc;
1278 mode = GET_MODE (reg);
1279 if (! REG_P (reg))
1281 if (check_only_p)
1282 return true;
1283 /* Always reload memory in an address even if the target supports
1284 such addresses. */
1285 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1286 before_p = true;
1288 else
1290 regno = REGNO (reg);
1291 rclass = get_reg_class (regno);
1292 if (! check_only_p
1293 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1295 if (lra_dump_file != NULL)
1297 fprintf (lra_dump_file,
1298 "Changing pseudo %d in address of insn %u on equiv ",
1299 REGNO (reg), INSN_UID (curr_insn));
1300 dump_value_slim (lra_dump_file, *loc, 1);
1301 fprintf (lra_dump_file, "\n");
1303 *loc = copy_rtx (*loc);
1305 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1307 if (check_only_p)
1308 return true;
1309 reg = *loc;
1310 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1311 mode, reg, cl, subreg_p, "address", &new_reg))
1312 before_p = true;
1314 else if (new_class != NO_REGS && rclass != new_class)
1316 if (check_only_p)
1317 return true;
1318 lra_change_class (regno, new_class, " Change to", true);
1319 return false;
1321 else
1322 return false;
1324 if (before_p)
1326 push_to_sequence (*before);
1327 lra_emit_move (new_reg, reg);
1328 *before = get_insns ();
1329 end_sequence ();
1331 *loc = new_reg;
1332 if (after != NULL)
1334 start_sequence ();
1335 lra_emit_move (reg, new_reg);
1336 emit_insn (*after);
1337 *after = get_insns ();
1338 end_sequence ();
1340 return true;
1343 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1344 the insn to be inserted before curr insn. AFTER returns the
1345 the insn to be inserted after curr insn. ORIGREG and NEWREG
1346 are the original reg and new reg for reload. */
1347 static void
1348 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1349 rtx newreg)
1351 if (before)
1353 push_to_sequence (*before);
1354 lra_emit_move (newreg, origreg);
1355 *before = get_insns ();
1356 end_sequence ();
1358 if (after)
1360 start_sequence ();
1361 lra_emit_move (origreg, newreg);
1362 emit_insn (*after);
1363 *after = get_insns ();
1364 end_sequence ();
1368 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1370 /* Make reloads for subreg in operand NOP with internal subreg mode
1371 REG_MODE, add new reloads for further processing. Return true if
1372 any change was done. */
1373 static bool
1374 simplify_operand_subreg (int nop, machine_mode reg_mode)
1376 int hard_regno;
1377 rtx_insn *before, *after;
1378 machine_mode mode, innermode;
1379 rtx reg, new_reg;
1380 rtx operand = *curr_id->operand_loc[nop];
1381 enum reg_class regclass;
1382 enum op_type type;
1384 before = after = NULL;
1386 if (GET_CODE (operand) != SUBREG)
1387 return false;
1389 mode = GET_MODE (operand);
1390 reg = SUBREG_REG (operand);
1391 innermode = GET_MODE (reg);
1392 type = curr_static_id->operand[nop].type;
1393 /* If we change address for paradoxical subreg of memory, the
1394 address might violate the necessary alignment or the access might
1395 be slow. So take this into consideration. We should not worry
1396 about access beyond allocated memory for paradoxical memory
1397 subregs as we don't substitute such equiv memory (see processing
1398 equivalences in function lra_constraints) and because for spilled
1399 pseudos we allocate stack memory enough for the biggest
1400 corresponding paradoxical subreg. */
1401 if (MEM_P (reg)
1402 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1403 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1405 rtx subst, old = *curr_id->operand_loc[nop];
1407 alter_subreg (curr_id->operand_loc[nop], false);
1408 subst = *curr_id->operand_loc[nop];
1409 lra_assert (MEM_P (subst));
1410 if (! valid_address_p (innermode, XEXP (reg, 0),
1411 MEM_ADDR_SPACE (reg))
1412 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1413 MEM_ADDR_SPACE (subst)))
1414 return true;
1415 /* If the address was valid and became invalid, prefer to reload
1416 the memory. Typical case is when the index scale should
1417 correspond the memory. */
1418 *curr_id->operand_loc[nop] = old;
1420 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1422 alter_subreg (curr_id->operand_loc[nop], false);
1423 return true;
1425 else if (CONSTANT_P (reg))
1427 /* Try to simplify subreg of constant. It is usually result of
1428 equivalence substitution. */
1429 if (innermode == VOIDmode
1430 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1431 innermode = curr_static_id->operand[nop].mode;
1432 if ((new_reg = simplify_subreg (mode, reg, innermode,
1433 SUBREG_BYTE (operand))) != NULL_RTX)
1435 *curr_id->operand_loc[nop] = new_reg;
1436 return true;
1439 /* Put constant into memory when we have mixed modes. It generates
1440 a better code in most cases as it does not need a secondary
1441 reload memory. It also prevents LRA looping when LRA is using
1442 secondary reload memory again and again. */
1443 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1444 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1446 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1447 alter_subreg (curr_id->operand_loc[nop], false);
1448 return true;
1450 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1451 if there may be a problem accessing OPERAND in the outer
1452 mode. */
1453 if ((REG_P (reg)
1454 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1455 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1456 /* Don't reload paradoxical subregs because we could be looping
1457 having repeatedly final regno out of hard regs range. */
1458 && (hard_regno_nregs[hard_regno][innermode]
1459 >= hard_regno_nregs[hard_regno][mode])
1460 && simplify_subreg_regno (hard_regno, innermode,
1461 SUBREG_BYTE (operand), mode) < 0
1462 /* Don't reload subreg for matching reload. It is actually
1463 valid subreg in LRA. */
1464 && ! LRA_SUBREG_P (operand))
1465 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1467 enum reg_class rclass;
1469 if (REG_P (reg))
1470 /* There is a big probability that we will get the same class
1471 for the new pseudo and we will get the same insn which
1472 means infinite looping. So spill the new pseudo. */
1473 rclass = NO_REGS;
1474 else
1475 /* The class will be defined later in curr_insn_transform. */
1476 rclass
1477 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1479 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1480 rclass, TRUE, "subreg reg", &new_reg))
1482 bool insert_before, insert_after;
1483 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1485 insert_before = (type != OP_OUT
1486 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1487 insert_after = (type != OP_IN);
1488 insert_move_for_subreg (insert_before ? &before : NULL,
1489 insert_after ? &after : NULL,
1490 reg, new_reg);
1492 SUBREG_REG (operand) = new_reg;
1493 lra_process_new_insns (curr_insn, before, after,
1494 "Inserting subreg reload");
1495 return true;
1497 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1498 IRA allocates hardreg to the inner pseudo reg according to its mode
1499 instead of the outermode, so the size of the hardreg may not be enough
1500 to contain the outermode operand, in that case we may need to insert
1501 reload for the reg. For the following two types of paradoxical subreg,
1502 we need to insert reload:
1503 1. If the op_type is OP_IN, and the hardreg could not be paired with
1504 other hardreg to contain the outermode operand
1505 (checked by in_hard_reg_set_p), we need to insert the reload.
1506 2. If the op_type is OP_OUT or OP_INOUT.
1508 Here is a paradoxical subreg example showing how the reload is generated:
1510 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1511 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1513 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1514 here, if reg107 is assigned to hardreg R15, because R15 is the last
1515 hardreg, compiler cannot find another hardreg to pair with R15 to
1516 contain TImode data. So we insert a TImode reload reg180 for it.
1517 After reload is inserted:
1519 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1520 (reg:DI 107 [ __comp ])) -1
1521 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1522 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1524 Two reload hard registers will be allocated to reg180 to save TImode data
1525 in LRA_assign. */
1526 else if (REG_P (reg)
1527 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1528 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1529 && (hard_regno_nregs[hard_regno][innermode]
1530 < hard_regno_nregs[hard_regno][mode])
1531 && (regclass = lra_get_allocno_class (REGNO (reg)))
1532 && (type != OP_IN
1533 || !in_hard_reg_set_p (reg_class_contents[regclass],
1534 mode, hard_regno)))
1536 /* The class will be defined later in curr_insn_transform. */
1537 enum reg_class rclass
1538 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1540 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1541 rclass, TRUE, "paradoxical subreg", &new_reg))
1543 rtx subreg;
1544 bool insert_before, insert_after;
1546 PUT_MODE (new_reg, mode);
1547 subreg = simplify_gen_subreg (innermode, new_reg, mode, 0);
1548 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1550 insert_before = (type != OP_OUT);
1551 insert_after = (type != OP_IN);
1552 insert_move_for_subreg (insert_before ? &before : NULL,
1553 insert_after ? &after : NULL,
1554 reg, subreg);
1556 SUBREG_REG (operand) = new_reg;
1557 lra_process_new_insns (curr_insn, before, after,
1558 "Inserting paradoxical subreg reload");
1559 return true;
1561 return false;
1564 /* Return TRUE if X refers for a hard register from SET. */
1565 static bool
1566 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1568 int i, j, x_hard_regno;
1569 machine_mode mode;
1570 const char *fmt;
1571 enum rtx_code code;
1573 if (x == NULL_RTX)
1574 return false;
1575 code = GET_CODE (x);
1576 mode = GET_MODE (x);
1577 if (code == SUBREG)
1579 x = SUBREG_REG (x);
1580 code = GET_CODE (x);
1581 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1582 mode = GET_MODE (x);
1585 if (REG_P (x))
1587 x_hard_regno = get_hard_regno (x);
1588 return (x_hard_regno >= 0
1589 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1591 if (MEM_P (x))
1593 struct address_info ad;
1595 decompose_mem_address (&ad, x);
1596 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1597 return true;
1598 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1599 return true;
1601 fmt = GET_RTX_FORMAT (code);
1602 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1604 if (fmt[i] == 'e')
1606 if (uses_hard_regs_p (XEXP (x, i), set))
1607 return true;
1609 else if (fmt[i] == 'E')
1611 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1612 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1613 return true;
1616 return false;
1619 /* Return true if OP is a spilled pseudo. */
1620 static inline bool
1621 spilled_pseudo_p (rtx op)
1623 return (REG_P (op)
1624 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1627 /* Return true if X is a general constant. */
1628 static inline bool
1629 general_constant_p (rtx x)
1631 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1634 static bool
1635 reg_in_class_p (rtx reg, enum reg_class cl)
1637 if (cl == NO_REGS)
1638 return get_reg_class (REGNO (reg)) == NO_REGS;
1639 return in_class_p (reg, cl, NULL);
1642 /* Return true if SET of RCLASS contains no hard regs which can be
1643 used in MODE. */
1644 static bool
1645 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1646 HARD_REG_SET &set,
1647 enum machine_mode mode)
1649 HARD_REG_SET temp;
1651 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1652 COPY_HARD_REG_SET (temp, set);
1653 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1654 return (hard_reg_set_subset_p
1655 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1658 /* Major function to choose the current insn alternative and what
1659 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1660 negative we should consider only this alternative. Return false if
1661 we can not choose the alternative or find how to reload the
1662 operands. */
1663 static bool
1664 process_alt_operands (int only_alternative)
1666 bool ok_p = false;
1667 int nop, overall, nalt;
1668 int n_alternatives = curr_static_id->n_alternatives;
1669 int n_operands = curr_static_id->n_operands;
1670 /* LOSERS counts the operands that don't fit this alternative and
1671 would require loading. */
1672 int losers;
1673 /* REJECT is a count of how undesirable this alternative says it is
1674 if any reloading is required. If the alternative matches exactly
1675 then REJECT is ignored, but otherwise it gets this much counted
1676 against it in addition to the reloading needed. */
1677 int reject;
1678 int op_reject;
1679 /* The number of elements in the following array. */
1680 int early_clobbered_regs_num;
1681 /* Numbers of operands which are early clobber registers. */
1682 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1683 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1684 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1685 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1686 bool curr_alt_win[MAX_RECOG_OPERANDS];
1687 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1688 int curr_alt_matches[MAX_RECOG_OPERANDS];
1689 /* The number of elements in the following array. */
1690 int curr_alt_dont_inherit_ops_num;
1691 /* Numbers of operands whose reload pseudos should not be inherited. */
1692 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1693 rtx op;
1694 /* The register when the operand is a subreg of register, otherwise the
1695 operand itself. */
1696 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1697 /* The register if the operand is a register or subreg of register,
1698 otherwise NULL. */
1699 rtx operand_reg[MAX_RECOG_OPERANDS];
1700 int hard_regno[MAX_RECOG_OPERANDS];
1701 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1702 int reload_nregs, reload_sum;
1703 bool costly_p;
1704 enum reg_class cl;
1706 /* Calculate some data common for all alternatives to speed up the
1707 function. */
1708 for (nop = 0; nop < n_operands; nop++)
1710 rtx reg;
1712 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1713 /* The real hard regno of the operand after the allocation. */
1714 hard_regno[nop] = get_hard_regno (op);
1716 operand_reg[nop] = reg = op;
1717 biggest_mode[nop] = GET_MODE (op);
1718 if (GET_CODE (op) == SUBREG)
1720 operand_reg[nop] = reg = SUBREG_REG (op);
1721 if (GET_MODE_SIZE (biggest_mode[nop])
1722 < GET_MODE_SIZE (GET_MODE (reg)))
1723 biggest_mode[nop] = GET_MODE (reg);
1725 if (! REG_P (reg))
1726 operand_reg[nop] = NULL_RTX;
1727 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1728 || ((int) REGNO (reg)
1729 == lra_get_elimination_hard_regno (REGNO (reg))))
1730 no_subreg_reg_operand[nop] = reg;
1731 else
1732 operand_reg[nop] = no_subreg_reg_operand[nop]
1733 /* Just use natural mode for elimination result. It should
1734 be enough for extra constraints hooks. */
1735 = regno_reg_rtx[hard_regno[nop]];
1738 /* The constraints are made of several alternatives. Each operand's
1739 constraint looks like foo,bar,... with commas separating the
1740 alternatives. The first alternatives for all operands go
1741 together, the second alternatives go together, etc.
1743 First loop over alternatives. */
1744 alternative_mask preferred = curr_id->preferred_alternatives;
1745 if (only_alternative >= 0)
1746 preferred &= ALTERNATIVE_BIT (only_alternative);
1748 for (nalt = 0; nalt < n_alternatives; nalt++)
1750 /* Loop over operands for one constraint alternative. */
1751 if (!TEST_BIT (preferred, nalt))
1752 continue;
1754 overall = losers = reject = reload_nregs = reload_sum = 0;
1755 for (nop = 0; nop < n_operands; nop++)
1757 int inc = (curr_static_id
1758 ->operand_alternative[nalt * n_operands + nop].reject);
1759 if (lra_dump_file != NULL && inc != 0)
1760 fprintf (lra_dump_file,
1761 " Staticly defined alt reject+=%d\n", inc);
1762 reject += inc;
1764 early_clobbered_regs_num = 0;
1766 for (nop = 0; nop < n_operands; nop++)
1768 const char *p;
1769 char *end;
1770 int len, c, m, i, opalt_num, this_alternative_matches;
1771 bool win, did_match, offmemok, early_clobber_p;
1772 /* false => this operand can be reloaded somehow for this
1773 alternative. */
1774 bool badop;
1775 /* true => this operand can be reloaded if the alternative
1776 allows regs. */
1777 bool winreg;
1778 /* True if a constant forced into memory would be OK for
1779 this operand. */
1780 bool constmemok;
1781 enum reg_class this_alternative, this_costly_alternative;
1782 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1783 bool this_alternative_match_win, this_alternative_win;
1784 bool this_alternative_offmemok;
1785 bool scratch_p;
1786 machine_mode mode;
1787 enum constraint_num cn;
1789 opalt_num = nalt * n_operands + nop;
1790 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1792 /* Fast track for no constraints at all. */
1793 curr_alt[nop] = NO_REGS;
1794 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1795 curr_alt_win[nop] = true;
1796 curr_alt_match_win[nop] = false;
1797 curr_alt_offmemok[nop] = false;
1798 curr_alt_matches[nop] = -1;
1799 continue;
1802 op = no_subreg_reg_operand[nop];
1803 mode = curr_operand_mode[nop];
1805 win = did_match = winreg = offmemok = constmemok = false;
1806 badop = true;
1808 early_clobber_p = false;
1809 p = curr_static_id->operand_alternative[opalt_num].constraint;
1811 this_costly_alternative = this_alternative = NO_REGS;
1812 /* We update set of possible hard regs besides its class
1813 because reg class might be inaccurate. For example,
1814 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1815 is translated in HI_REGS because classes are merged by
1816 pairs and there is no accurate intermediate class. */
1817 CLEAR_HARD_REG_SET (this_alternative_set);
1818 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1819 this_alternative_win = false;
1820 this_alternative_match_win = false;
1821 this_alternative_offmemok = false;
1822 this_alternative_matches = -1;
1824 /* An empty constraint should be excluded by the fast
1825 track. */
1826 lra_assert (*p != 0 && *p != ',');
1828 op_reject = 0;
1829 /* Scan this alternative's specs for this operand; set WIN
1830 if the operand fits any letter in this alternative.
1831 Otherwise, clear BADOP if this operand could fit some
1832 letter after reloads, or set WINREG if this operand could
1833 fit after reloads provided the constraint allows some
1834 registers. */
1835 costly_p = false;
1838 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1840 case '\0':
1841 len = 0;
1842 break;
1843 case ',':
1844 c = '\0';
1845 break;
1847 case '&':
1848 early_clobber_p = true;
1849 break;
1851 case '$':
1852 op_reject += LRA_MAX_REJECT;
1853 break;
1854 case '^':
1855 op_reject += LRA_LOSER_COST_FACTOR;
1856 break;
1858 case '#':
1859 /* Ignore rest of this alternative. */
1860 c = '\0';
1861 break;
1863 case '0': case '1': case '2': case '3': case '4':
1864 case '5': case '6': case '7': case '8': case '9':
1866 int m_hregno;
1867 bool match_p;
1869 m = strtoul (p, &end, 10);
1870 p = end;
1871 len = 0;
1872 lra_assert (nop > m);
1874 this_alternative_matches = m;
1875 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1876 /* We are supposed to match a previous operand.
1877 If we do, we win if that one did. If we do
1878 not, count both of the operands as losers.
1879 (This is too conservative, since most of the
1880 time only a single reload insn will be needed
1881 to make the two operands win. As a result,
1882 this alternative may be rejected when it is
1883 actually desirable.) */
1884 match_p = false;
1885 if (operands_match_p (*curr_id->operand_loc[nop],
1886 *curr_id->operand_loc[m], m_hregno))
1888 /* We should reject matching of an early
1889 clobber operand if the matching operand is
1890 not dying in the insn. */
1891 if (! curr_static_id->operand[m].early_clobber
1892 || operand_reg[nop] == NULL_RTX
1893 || (find_regno_note (curr_insn, REG_DEAD,
1894 REGNO (op))
1895 || REGNO (op) == REGNO (operand_reg[m])))
1896 match_p = true;
1898 if (match_p)
1900 /* If we are matching a non-offsettable
1901 address where an offsettable address was
1902 expected, then we must reject this
1903 combination, because we can't reload
1904 it. */
1905 if (curr_alt_offmemok[m]
1906 && MEM_P (*curr_id->operand_loc[m])
1907 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1908 continue;
1910 else
1912 /* Operands don't match. Both operands must
1913 allow a reload register, otherwise we
1914 cannot make them match. */
1915 if (curr_alt[m] == NO_REGS)
1916 break;
1917 /* Retroactively mark the operand we had to
1918 match as a loser, if it wasn't already and
1919 it wasn't matched to a register constraint
1920 (e.g it might be matched by memory). */
1921 if (curr_alt_win[m]
1922 && (operand_reg[m] == NULL_RTX
1923 || hard_regno[m] < 0))
1925 losers++;
1926 reload_nregs
1927 += (ira_reg_class_max_nregs[curr_alt[m]]
1928 [GET_MODE (*curr_id->operand_loc[m])]);
1931 /* Prefer matching earlyclobber alternative as
1932 it results in less hard regs required for
1933 the insn than a non-matching earlyclobber
1934 alternative. */
1935 if (curr_static_id->operand[m].early_clobber)
1937 if (lra_dump_file != NULL)
1938 fprintf
1939 (lra_dump_file,
1940 " %d Matching earlyclobber alt:"
1941 " reject--\n",
1942 nop);
1943 reject--;
1945 /* Otherwise we prefer no matching
1946 alternatives because it gives more freedom
1947 in RA. */
1948 else if (operand_reg[nop] == NULL_RTX
1949 || (find_regno_note (curr_insn, REG_DEAD,
1950 REGNO (operand_reg[nop]))
1951 == NULL_RTX))
1953 if (lra_dump_file != NULL)
1954 fprintf
1955 (lra_dump_file,
1956 " %d Matching alt: reject+=2\n",
1957 nop);
1958 reject += 2;
1961 /* If we have to reload this operand and some
1962 previous operand also had to match the same
1963 thing as this operand, we don't know how to do
1964 that. */
1965 if (!match_p || !curr_alt_win[m])
1967 for (i = 0; i < nop; i++)
1968 if (curr_alt_matches[i] == m)
1969 break;
1970 if (i < nop)
1971 break;
1973 else
1974 did_match = true;
1976 /* This can be fixed with reloads if the operand
1977 we are supposed to match can be fixed with
1978 reloads. */
1979 badop = false;
1980 this_alternative = curr_alt[m];
1981 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1982 winreg = this_alternative != NO_REGS;
1983 break;
1986 case 'g':
1987 if (MEM_P (op)
1988 || general_constant_p (op)
1989 || spilled_pseudo_p (op))
1990 win = true;
1991 cl = GENERAL_REGS;
1992 goto reg;
1994 default:
1995 cn = lookup_constraint (p);
1996 switch (get_constraint_type (cn))
1998 case CT_REGISTER:
1999 cl = reg_class_for_constraint (cn);
2000 if (cl != NO_REGS)
2001 goto reg;
2002 break;
2004 case CT_CONST_INT:
2005 if (CONST_INT_P (op)
2006 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2007 win = true;
2008 break;
2010 case CT_MEMORY:
2011 if (MEM_P (op)
2012 && satisfies_memory_constraint_p (op, cn))
2013 win = true;
2014 else if (spilled_pseudo_p (op))
2015 win = true;
2017 /* If we didn't already win, we can reload constants
2018 via force_const_mem or put the pseudo value into
2019 memory, or make other memory by reloading the
2020 address like for 'o'. */
2021 if (CONST_POOL_OK_P (mode, op)
2022 || MEM_P (op) || REG_P (op))
2023 badop = false;
2024 constmemok = true;
2025 offmemok = true;
2026 break;
2028 case CT_ADDRESS:
2029 /* If we didn't already win, we can reload the address
2030 into a base register. */
2031 if (satisfies_address_constraint_p (op, cn))
2032 win = true;
2033 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2034 ADDRESS, SCRATCH);
2035 badop = false;
2036 goto reg;
2038 case CT_FIXED_FORM:
2039 if (constraint_satisfied_p (op, cn))
2040 win = true;
2041 break;
2043 break;
2045 reg:
2046 this_alternative = reg_class_subunion[this_alternative][cl];
2047 IOR_HARD_REG_SET (this_alternative_set,
2048 reg_class_contents[cl]);
2049 if (costly_p)
2051 this_costly_alternative
2052 = reg_class_subunion[this_costly_alternative][cl];
2053 IOR_HARD_REG_SET (this_costly_alternative_set,
2054 reg_class_contents[cl]);
2056 if (mode == BLKmode)
2057 break;
2058 winreg = true;
2059 if (REG_P (op))
2061 if (hard_regno[nop] >= 0
2062 && in_hard_reg_set_p (this_alternative_set,
2063 mode, hard_regno[nop]))
2064 win = true;
2065 else if (hard_regno[nop] < 0
2066 && in_class_p (op, this_alternative, NULL))
2067 win = true;
2069 break;
2071 if (c != ' ' && c != '\t')
2072 costly_p = c == '*';
2074 while ((p += len), c);
2076 scratch_p = (operand_reg[nop] != NULL_RTX
2077 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2078 /* Record which operands fit this alternative. */
2079 if (win)
2081 this_alternative_win = true;
2082 if (operand_reg[nop] != NULL_RTX)
2084 if (hard_regno[nop] >= 0)
2086 if (in_hard_reg_set_p (this_costly_alternative_set,
2087 mode, hard_regno[nop]))
2089 if (lra_dump_file != NULL)
2090 fprintf (lra_dump_file,
2091 " %d Costly set: reject++\n",
2092 nop);
2093 reject++;
2096 else
2098 /* Prefer won reg to spilled pseudo under other
2099 equal conditions for possibe inheritance. */
2100 if (! scratch_p)
2102 if (lra_dump_file != NULL)
2103 fprintf
2104 (lra_dump_file,
2105 " %d Non pseudo reload: reject++\n",
2106 nop);
2107 reject++;
2109 if (in_class_p (operand_reg[nop],
2110 this_costly_alternative, NULL))
2112 if (lra_dump_file != NULL)
2113 fprintf
2114 (lra_dump_file,
2115 " %d Non pseudo costly reload:"
2116 " reject++\n",
2117 nop);
2118 reject++;
2121 /* We simulate the behaviour of old reload here.
2122 Although scratches need hard registers and it
2123 might result in spilling other pseudos, no reload
2124 insns are generated for the scratches. So it
2125 might cost something but probably less than old
2126 reload pass believes. */
2127 if (scratch_p)
2129 if (lra_dump_file != NULL)
2130 fprintf (lra_dump_file,
2131 " %d Scratch win: reject+=2\n",
2132 nop);
2133 reject += 2;
2137 else if (did_match)
2138 this_alternative_match_win = true;
2139 else
2141 int const_to_mem = 0;
2142 bool no_regs_p;
2144 reject += op_reject;
2145 /* Never do output reload of stack pointer. It makes
2146 impossible to do elimination when SP is changed in
2147 RTL. */
2148 if (op == stack_pointer_rtx && ! frame_pointer_needed
2149 && curr_static_id->operand[nop].type != OP_IN)
2150 goto fail;
2152 /* If this alternative asks for a specific reg class, see if there
2153 is at least one allocatable register in that class. */
2154 no_regs_p
2155 = (this_alternative == NO_REGS
2156 || (hard_reg_set_subset_p
2157 (reg_class_contents[this_alternative],
2158 lra_no_alloc_regs)));
2160 /* For asms, verify that the class for this alternative is possible
2161 for the mode that is specified. */
2162 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2164 int i;
2165 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2166 if (HARD_REGNO_MODE_OK (i, mode)
2167 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2168 mode, i))
2169 break;
2170 if (i == FIRST_PSEUDO_REGISTER)
2171 winreg = false;
2174 /* If this operand accepts a register, and if the
2175 register class has at least one allocatable register,
2176 then this operand can be reloaded. */
2177 if (winreg && !no_regs_p)
2178 badop = false;
2180 if (badop)
2182 if (lra_dump_file != NULL)
2183 fprintf (lra_dump_file,
2184 " alt=%d: Bad operand -- refuse\n",
2185 nalt);
2186 goto fail;
2189 /* If not assigned pseudo has a class which a subset of
2190 required reg class, it is a less costly alternative
2191 as the pseudo still can get a hard reg of necessary
2192 class. */
2193 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2194 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2195 && ira_class_subset_p[this_alternative][cl])
2197 if (lra_dump_file != NULL)
2198 fprintf
2199 (lra_dump_file,
2200 " %d Super set class reg: reject-=3\n", nop);
2201 reject -= 3;
2204 this_alternative_offmemok = offmemok;
2205 if (this_costly_alternative != NO_REGS)
2207 if (lra_dump_file != NULL)
2208 fprintf (lra_dump_file,
2209 " %d Costly loser: reject++\n", nop);
2210 reject++;
2212 /* If the operand is dying, has a matching constraint,
2213 and satisfies constraints of the matched operand
2214 which failed to satisfy the own constraints, most probably
2215 the reload for this operand will be gone. */
2216 if (this_alternative_matches >= 0
2217 && !curr_alt_win[this_alternative_matches]
2218 && REG_P (op)
2219 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2220 && (hard_regno[nop] >= 0
2221 ? in_hard_reg_set_p (this_alternative_set,
2222 mode, hard_regno[nop])
2223 : in_class_p (op, this_alternative, NULL)))
2225 if (lra_dump_file != NULL)
2226 fprintf
2227 (lra_dump_file,
2228 " %d Dying matched operand reload: reject++\n",
2229 nop);
2230 reject++;
2232 else
2234 /* Strict_low_part requires to reload the register
2235 not the sub-register. In this case we should
2236 check that a final reload hard reg can hold the
2237 value mode. */
2238 if (curr_static_id->operand[nop].strict_low
2239 && REG_P (op)
2240 && hard_regno[nop] < 0
2241 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2242 && ira_class_hard_regs_num[this_alternative] > 0
2243 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2244 [this_alternative][0],
2245 GET_MODE
2246 (*curr_id->operand_loc[nop])))
2248 if (lra_dump_file != NULL)
2249 fprintf
2250 (lra_dump_file,
2251 " alt=%d: Strict low subreg reload -- refuse\n",
2252 nalt);
2253 goto fail;
2255 losers++;
2257 if (operand_reg[nop] != NULL_RTX
2258 /* Output operands and matched input operands are
2259 not inherited. The following conditions do not
2260 exactly describe the previous statement but they
2261 are pretty close. */
2262 && curr_static_id->operand[nop].type != OP_OUT
2263 && (this_alternative_matches < 0
2264 || curr_static_id->operand[nop].type != OP_IN))
2266 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2267 (operand_reg[nop])]
2268 .last_reload);
2270 /* The value of reload_sum has sense only if we
2271 process insns in their order. It happens only on
2272 the first constraints sub-pass when we do most of
2273 reload work. */
2274 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2275 reload_sum += last_reload - bb_reload_num;
2277 /* If this is a constant that is reloaded into the
2278 desired class by copying it to memory first, count
2279 that as another reload. This is consistent with
2280 other code and is required to avoid choosing another
2281 alternative when the constant is moved into memory.
2282 Note that the test here is precisely the same as in
2283 the code below that calls force_const_mem. */
2284 if (CONST_POOL_OK_P (mode, op)
2285 && ((targetm.preferred_reload_class
2286 (op, this_alternative) == NO_REGS)
2287 || no_input_reloads_p))
2289 const_to_mem = 1;
2290 if (! no_regs_p)
2291 losers++;
2294 /* Alternative loses if it requires a type of reload not
2295 permitted for this insn. We can always reload
2296 objects with a REG_UNUSED note. */
2297 if ((curr_static_id->operand[nop].type != OP_IN
2298 && no_output_reloads_p
2299 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2300 || (curr_static_id->operand[nop].type != OP_OUT
2301 && no_input_reloads_p && ! const_to_mem)
2302 || (this_alternative_matches >= 0
2303 && (no_input_reloads_p
2304 || (no_output_reloads_p
2305 && (curr_static_id->operand
2306 [this_alternative_matches].type != OP_IN)
2307 && ! find_reg_note (curr_insn, REG_UNUSED,
2308 no_subreg_reg_operand
2309 [this_alternative_matches])))))
2311 if (lra_dump_file != NULL)
2312 fprintf
2313 (lra_dump_file,
2314 " alt=%d: No input/otput reload -- refuse\n",
2315 nalt);
2316 goto fail;
2319 /* Alternative loses if it required class pseudo can not
2320 hold value of required mode. Such insns can be
2321 described by insn definitions with mode iterators. */
2322 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2323 && ! hard_reg_set_empty_p (this_alternative_set)
2324 /* It is common practice for constraints to use a
2325 class which does not have actually enough regs to
2326 hold the value (e.g. x86 AREG for mode requiring
2327 more one general reg). Therefore we have 2
2328 conditions to check that the reload pseudo can
2329 not hold the mode value. */
2330 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2331 [this_alternative][0],
2332 GET_MODE (*curr_id->operand_loc[nop]))
2333 /* The above condition is not enough as the first
2334 reg in ira_class_hard_regs can be not aligned for
2335 multi-words mode values. */
2336 && (prohibited_class_reg_set_mode_p
2337 (this_alternative, this_alternative_set,
2338 GET_MODE (*curr_id->operand_loc[nop]))))
2340 if (lra_dump_file != NULL)
2341 fprintf (lra_dump_file,
2342 " alt=%d: reload pseudo for op %d "
2343 " can not hold the mode value -- refuse\n",
2344 nalt, nop);
2345 goto fail;
2348 /* Check strong discouragement of reload of non-constant
2349 into class THIS_ALTERNATIVE. */
2350 if (! CONSTANT_P (op) && ! no_regs_p
2351 && (targetm.preferred_reload_class
2352 (op, this_alternative) == NO_REGS
2353 || (curr_static_id->operand[nop].type == OP_OUT
2354 && (targetm.preferred_output_reload_class
2355 (op, this_alternative) == NO_REGS))))
2357 if (lra_dump_file != NULL)
2358 fprintf (lra_dump_file,
2359 " %d Non-prefered reload: reject+=%d\n",
2360 nop, LRA_MAX_REJECT);
2361 reject += LRA_MAX_REJECT;
2364 if (! (MEM_P (op) && offmemok)
2365 && ! (const_to_mem && constmemok))
2367 /* We prefer to reload pseudos over reloading other
2368 things, since such reloads may be able to be
2369 eliminated later. So bump REJECT in other cases.
2370 Don't do this in the case where we are forcing a
2371 constant into memory and it will then win since
2372 we don't want to have a different alternative
2373 match then. */
2374 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2376 if (lra_dump_file != NULL)
2377 fprintf
2378 (lra_dump_file,
2379 " %d Non-pseudo reload: reject+=2\n",
2380 nop);
2381 reject += 2;
2384 if (! no_regs_p)
2385 reload_nregs
2386 += ira_reg_class_max_nregs[this_alternative][mode];
2388 if (SMALL_REGISTER_CLASS_P (this_alternative))
2390 if (lra_dump_file != NULL)
2391 fprintf
2392 (lra_dump_file,
2393 " %d Small class reload: reject+=%d\n",
2394 nop, LRA_LOSER_COST_FACTOR / 2);
2395 reject += LRA_LOSER_COST_FACTOR / 2;
2399 /* We are trying to spill pseudo into memory. It is
2400 usually more costly than moving to a hard register
2401 although it might takes the same number of
2402 reloads. */
2403 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2405 if (lra_dump_file != NULL)
2406 fprintf
2407 (lra_dump_file,
2408 " %d Spill pseudo into memory: reject+=3\n",
2409 nop);
2410 reject += 3;
2411 if (VECTOR_MODE_P (mode))
2413 /* Spilling vectors into memory is usually more
2414 costly as they contain big values. */
2415 if (lra_dump_file != NULL)
2416 fprintf
2417 (lra_dump_file,
2418 " %d Spill vector pseudo: reject+=2\n",
2419 nop);
2420 reject += 2;
2424 #ifdef SECONDARY_MEMORY_NEEDED
2425 /* If reload requires moving value through secondary
2426 memory, it will need one more insn at least. */
2427 if (this_alternative != NO_REGS
2428 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2429 && ((curr_static_id->operand[nop].type != OP_OUT
2430 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2431 GET_MODE (op)))
2432 || (curr_static_id->operand[nop].type != OP_IN
2433 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2434 GET_MODE (op)))))
2435 losers++;
2436 #endif
2437 /* Input reloads can be inherited more often than output
2438 reloads can be removed, so penalize output
2439 reloads. */
2440 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2442 if (lra_dump_file != NULL)
2443 fprintf
2444 (lra_dump_file,
2445 " %d Non input pseudo reload: reject++\n",
2446 nop);
2447 reject++;
2451 if (early_clobber_p && ! scratch_p)
2453 if (lra_dump_file != NULL)
2454 fprintf (lra_dump_file,
2455 " %d Early clobber: reject++\n", nop);
2456 reject++;
2458 /* ??? We check early clobbers after processing all operands
2459 (see loop below) and there we update the costs more.
2460 Should we update the cost (may be approximately) here
2461 because of early clobber register reloads or it is a rare
2462 or non-important thing to be worth to do it. */
2463 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2464 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2466 if (lra_dump_file != NULL)
2467 fprintf (lra_dump_file,
2468 " alt=%d,overall=%d,losers=%d -- refuse\n",
2469 nalt, overall, losers);
2470 goto fail;
2473 curr_alt[nop] = this_alternative;
2474 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2475 curr_alt_win[nop] = this_alternative_win;
2476 curr_alt_match_win[nop] = this_alternative_match_win;
2477 curr_alt_offmemok[nop] = this_alternative_offmemok;
2478 curr_alt_matches[nop] = this_alternative_matches;
2480 if (this_alternative_matches >= 0
2481 && !did_match && !this_alternative_win)
2482 curr_alt_win[this_alternative_matches] = false;
2484 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2485 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2487 if (curr_insn_set != NULL_RTX && n_operands == 2
2488 /* Prevent processing non-move insns. */
2489 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2490 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2491 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2492 && REG_P (no_subreg_reg_operand[0])
2493 && REG_P (no_subreg_reg_operand[1])
2494 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2495 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2496 || (! curr_alt_win[0] && curr_alt_win[1]
2497 && REG_P (no_subreg_reg_operand[1])
2498 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2499 || (curr_alt_win[0] && ! curr_alt_win[1]
2500 && REG_P (no_subreg_reg_operand[0])
2501 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2502 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2503 no_subreg_reg_operand[1])
2504 || (targetm.preferred_reload_class
2505 (no_subreg_reg_operand[1],
2506 (enum reg_class) curr_alt[1]) != NO_REGS))
2507 /* If it is a result of recent elimination in move
2508 insn we can transform it into an add still by
2509 using this alternative. */
2510 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2512 /* We have a move insn and a new reload insn will be similar
2513 to the current insn. We should avoid such situation as it
2514 results in LRA cycling. */
2515 overall += LRA_MAX_REJECT;
2517 ok_p = true;
2518 curr_alt_dont_inherit_ops_num = 0;
2519 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2521 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2522 HARD_REG_SET temp_set;
2524 i = early_clobbered_nops[nop];
2525 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2526 || hard_regno[i] < 0)
2527 continue;
2528 lra_assert (operand_reg[i] != NULL_RTX);
2529 clobbered_hard_regno = hard_regno[i];
2530 CLEAR_HARD_REG_SET (temp_set);
2531 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2532 first_conflict_j = last_conflict_j = -1;
2533 for (j = 0; j < n_operands; j++)
2534 if (j == i
2535 /* We don't want process insides of match_operator and
2536 match_parallel because otherwise we would process
2537 their operands once again generating a wrong
2538 code. */
2539 || curr_static_id->operand[j].is_operator)
2540 continue;
2541 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2542 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2543 continue;
2544 /* If we don't reload j-th operand, check conflicts. */
2545 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2546 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2548 if (first_conflict_j < 0)
2549 first_conflict_j = j;
2550 last_conflict_j = j;
2552 if (last_conflict_j < 0)
2553 continue;
2554 /* If earlyclobber operand conflicts with another
2555 non-matching operand which is actually the same register
2556 as the earlyclobber operand, it is better to reload the
2557 another operand as an operand matching the earlyclobber
2558 operand can be also the same. */
2559 if (first_conflict_j == last_conflict_j
2560 && operand_reg[last_conflict_j]
2561 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2562 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2564 curr_alt_win[last_conflict_j] = false;
2565 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2566 = last_conflict_j;
2567 losers++;
2568 /* Early clobber was already reflected in REJECT. */
2569 lra_assert (reject > 0);
2570 if (lra_dump_file != NULL)
2571 fprintf
2572 (lra_dump_file,
2573 " %d Conflict early clobber reload: reject--\n",
2575 reject--;
2576 overall += LRA_LOSER_COST_FACTOR - 1;
2578 else
2580 /* We need to reload early clobbered register and the
2581 matched registers. */
2582 for (j = 0; j < n_operands; j++)
2583 if (curr_alt_matches[j] == i)
2585 curr_alt_match_win[j] = false;
2586 losers++;
2587 overall += LRA_LOSER_COST_FACTOR;
2589 if (! curr_alt_match_win[i])
2590 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2591 else
2593 /* Remember pseudos used for match reloads are never
2594 inherited. */
2595 lra_assert (curr_alt_matches[i] >= 0);
2596 curr_alt_win[curr_alt_matches[i]] = false;
2598 curr_alt_win[i] = curr_alt_match_win[i] = false;
2599 losers++;
2600 /* Early clobber was already reflected in REJECT. */
2601 lra_assert (reject > 0);
2602 if (lra_dump_file != NULL)
2603 fprintf
2604 (lra_dump_file,
2605 " %d Matched conflict early clobber reloads:"
2606 "reject--\n",
2608 reject--;
2609 overall += LRA_LOSER_COST_FACTOR - 1;
2612 if (lra_dump_file != NULL)
2613 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2614 nalt, overall, losers, reload_nregs);
2616 /* If this alternative can be made to work by reloading, and it
2617 needs less reloading than the others checked so far, record
2618 it as the chosen goal for reloading. */
2619 if ((best_losers != 0 && losers == 0)
2620 || (((best_losers == 0 && losers == 0)
2621 || (best_losers != 0 && losers != 0))
2622 && (best_overall > overall
2623 || (best_overall == overall
2624 /* If the cost of the reloads is the same,
2625 prefer alternative which requires minimal
2626 number of reload regs. */
2627 && (reload_nregs < best_reload_nregs
2628 || (reload_nregs == best_reload_nregs
2629 && (best_reload_sum < reload_sum
2630 || (best_reload_sum == reload_sum
2631 && nalt < goal_alt_number))))))))
2633 for (nop = 0; nop < n_operands; nop++)
2635 goal_alt_win[nop] = curr_alt_win[nop];
2636 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2637 goal_alt_matches[nop] = curr_alt_matches[nop];
2638 goal_alt[nop] = curr_alt[nop];
2639 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2641 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2642 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2643 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2644 goal_alt_swapped = curr_swapped;
2645 best_overall = overall;
2646 best_losers = losers;
2647 best_reload_nregs = reload_nregs;
2648 best_reload_sum = reload_sum;
2649 goal_alt_number = nalt;
2651 if (losers == 0)
2652 /* Everything is satisfied. Do not process alternatives
2653 anymore. */
2654 break;
2655 fail:
2658 return ok_p;
2661 /* Make reload base reg from address AD. */
2662 static rtx
2663 base_to_reg (struct address_info *ad)
2665 enum reg_class cl;
2666 int code = -1;
2667 rtx new_inner = NULL_RTX;
2668 rtx new_reg = NULL_RTX;
2669 rtx_insn *insn;
2670 rtx_insn *last_insn = get_last_insn();
2672 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2673 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2674 get_index_code (ad));
2675 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2676 cl, "base");
2677 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2678 ad->disp_term == NULL
2679 ? gen_int_mode (0, ad->mode)
2680 : *ad->disp_term);
2681 if (!valid_address_p (ad->mode, new_inner, ad->as))
2682 return NULL_RTX;
2683 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base_term));
2684 code = recog_memoized (insn);
2685 if (code < 0)
2687 delete_insns_since (last_insn);
2688 return NULL_RTX;
2691 return new_inner;
2694 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2695 static rtx
2696 base_plus_disp_to_reg (struct address_info *ad)
2698 enum reg_class cl;
2699 rtx new_reg;
2701 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2702 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2703 get_index_code (ad));
2704 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2705 cl, "base + disp");
2706 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2707 return new_reg;
2710 /* Make reload of index part of address AD. Return the new
2711 pseudo. */
2712 static rtx
2713 index_part_to_reg (struct address_info *ad)
2715 rtx new_reg;
2717 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2718 INDEX_REG_CLASS, "index term");
2719 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2720 GEN_INT (get_index_scale (ad)), new_reg, 1);
2721 return new_reg;
2724 /* Return true if we can add a displacement to address AD, even if that
2725 makes the address invalid. The fix-up code requires any new address
2726 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2727 static bool
2728 can_add_disp_p (struct address_info *ad)
2730 return (!ad->autoinc_p
2731 && ad->segment == NULL
2732 && ad->base == ad->base_term
2733 && ad->disp == ad->disp_term);
2736 /* Make equiv substitution in address AD. Return true if a substitution
2737 was made. */
2738 static bool
2739 equiv_address_substitution (struct address_info *ad)
2741 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2742 HOST_WIDE_INT disp, scale;
2743 bool change_p;
2745 base_term = strip_subreg (ad->base_term);
2746 if (base_term == NULL)
2747 base_reg = new_base_reg = NULL_RTX;
2748 else
2750 base_reg = *base_term;
2751 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2753 index_term = strip_subreg (ad->index_term);
2754 if (index_term == NULL)
2755 index_reg = new_index_reg = NULL_RTX;
2756 else
2758 index_reg = *index_term;
2759 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2761 if (base_reg == new_base_reg && index_reg == new_index_reg)
2762 return false;
2763 disp = 0;
2764 change_p = false;
2765 if (lra_dump_file != NULL)
2767 fprintf (lra_dump_file, "Changing address in insn %d ",
2768 INSN_UID (curr_insn));
2769 dump_value_slim (lra_dump_file, *ad->outer, 1);
2771 if (base_reg != new_base_reg)
2773 if (REG_P (new_base_reg))
2775 *base_term = new_base_reg;
2776 change_p = true;
2778 else if (GET_CODE (new_base_reg) == PLUS
2779 && REG_P (XEXP (new_base_reg, 0))
2780 && CONST_INT_P (XEXP (new_base_reg, 1))
2781 && can_add_disp_p (ad))
2783 disp += INTVAL (XEXP (new_base_reg, 1));
2784 *base_term = XEXP (new_base_reg, 0);
2785 change_p = true;
2787 if (ad->base_term2 != NULL)
2788 *ad->base_term2 = *ad->base_term;
2790 if (index_reg != new_index_reg)
2792 if (REG_P (new_index_reg))
2794 *index_term = new_index_reg;
2795 change_p = true;
2797 else if (GET_CODE (new_index_reg) == PLUS
2798 && REG_P (XEXP (new_index_reg, 0))
2799 && CONST_INT_P (XEXP (new_index_reg, 1))
2800 && can_add_disp_p (ad)
2801 && (scale = get_index_scale (ad)))
2803 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2804 *index_term = XEXP (new_index_reg, 0);
2805 change_p = true;
2808 if (disp != 0)
2810 if (ad->disp != NULL)
2811 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2812 else
2814 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2815 update_address (ad);
2817 change_p = true;
2819 if (lra_dump_file != NULL)
2821 if (! change_p)
2822 fprintf (lra_dump_file, " -- no change\n");
2823 else
2825 fprintf (lra_dump_file, " on equiv ");
2826 dump_value_slim (lra_dump_file, *ad->outer, 1);
2827 fprintf (lra_dump_file, "\n");
2830 return change_p;
2833 /* Major function to make reloads for an address in operand NOP or
2834 check its correctness (If CHECK_ONLY_P is true). The supported
2835 cases are:
2837 1) an address that existed before LRA started, at which point it
2838 must have been valid. These addresses are subject to elimination
2839 and may have become invalid due to the elimination offset being out
2840 of range.
2842 2) an address created by forcing a constant to memory
2843 (force_const_to_mem). The initial form of these addresses might
2844 not be valid, and it is this function's job to make them valid.
2846 3) a frame address formed from a register and a (possibly zero)
2847 constant offset. As above, these addresses might not be valid and
2848 this function must make them so.
2850 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2851 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2852 address. Return true for any RTL change.
2854 The function is a helper function which does not produce all
2855 transformations (when CHECK_ONLY_P is false) which can be
2856 necessary. It does just basic steps. To do all necessary
2857 transformations use function process_address. */
2858 static bool
2859 process_address_1 (int nop, bool check_only_p,
2860 rtx_insn **before, rtx_insn **after)
2862 struct address_info ad;
2863 rtx new_reg;
2864 rtx op = *curr_id->operand_loc[nop];
2865 const char *constraint = curr_static_id->operand[nop].constraint;
2866 enum constraint_num cn = lookup_constraint (constraint);
2867 bool change_p = false;
2869 if (insn_extra_address_constraint (cn))
2870 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2871 else if (MEM_P (op))
2872 decompose_mem_address (&ad, op);
2873 else if (GET_CODE (op) == SUBREG
2874 && MEM_P (SUBREG_REG (op)))
2875 decompose_mem_address (&ad, SUBREG_REG (op));
2876 else
2877 return false;
2878 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
2879 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
2880 when INDEX_REG_CLASS is a single register class. */
2881 if (ad.base_term != NULL
2882 && ad.index_term != NULL
2883 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
2884 && REG_P (*ad.base_term)
2885 && REG_P (*ad.index_term)
2886 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
2887 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
2889 std::swap (ad.base, ad.index);
2890 std::swap (ad.base_term, ad.index_term);
2892 if (! check_only_p)
2893 change_p = equiv_address_substitution (&ad);
2894 if (ad.base_term != NULL
2895 && (process_addr_reg
2896 (ad.base_term, check_only_p, before,
2897 (ad.autoinc_p
2898 && !(REG_P (*ad.base_term)
2899 && find_regno_note (curr_insn, REG_DEAD,
2900 REGNO (*ad.base_term)) != NULL_RTX)
2901 ? after : NULL),
2902 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2903 get_index_code (&ad)))))
2905 change_p = true;
2906 if (ad.base_term2 != NULL)
2907 *ad.base_term2 = *ad.base_term;
2909 if (ad.index_term != NULL
2910 && process_addr_reg (ad.index_term, check_only_p,
2911 before, NULL, INDEX_REG_CLASS))
2912 change_p = true;
2914 /* Target hooks sometimes don't treat extra-constraint addresses as
2915 legitimate address_operands, so handle them specially. */
2916 if (insn_extra_address_constraint (cn)
2917 && satisfies_address_constraint_p (&ad, cn))
2918 return change_p;
2920 if (check_only_p)
2921 return change_p;
2923 /* There are three cases where the shape of *AD.INNER may now be invalid:
2925 1) the original address was valid, but either elimination or
2926 equiv_address_substitution was applied and that made
2927 the address invalid.
2929 2) the address is an invalid symbolic address created by
2930 force_const_to_mem.
2932 3) the address is a frame address with an invalid offset.
2934 4) the address is a frame address with an invalid base.
2936 All these cases involve a non-autoinc address, so there is no
2937 point revalidating other types. */
2938 if (ad.autoinc_p || valid_address_p (&ad))
2939 return change_p;
2941 /* Any index existed before LRA started, so we can assume that the
2942 presence and shape of the index is valid. */
2943 push_to_sequence (*before);
2944 lra_assert (ad.disp == ad.disp_term);
2945 if (ad.base == NULL)
2947 if (ad.index == NULL)
2949 int code = -1;
2950 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2951 SCRATCH, SCRATCH);
2952 rtx addr = *ad.inner;
2954 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2955 if (HAVE_lo_sum)
2957 rtx_insn *insn;
2958 rtx_insn *last = get_last_insn ();
2960 /* addr => lo_sum (new_base, addr), case (2) above. */
2961 insn = emit_insn (gen_rtx_SET
2962 (new_reg,
2963 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2964 code = recog_memoized (insn);
2965 if (code >= 0)
2967 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2968 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2970 /* Try to put lo_sum into register. */
2971 insn = emit_insn (gen_rtx_SET
2972 (new_reg,
2973 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2974 code = recog_memoized (insn);
2975 if (code >= 0)
2977 *ad.inner = new_reg;
2978 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2980 *ad.inner = addr;
2981 code = -1;
2987 if (code < 0)
2988 delete_insns_since (last);
2991 if (code < 0)
2993 /* addr => new_base, case (2) above. */
2994 lra_emit_move (new_reg, addr);
2995 *ad.inner = new_reg;
2998 else
3000 /* index * scale + disp => new base + index * scale,
3001 case (1) above. */
3002 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3003 GET_CODE (*ad.index));
3005 lra_assert (INDEX_REG_CLASS != NO_REGS);
3006 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3007 lra_emit_move (new_reg, *ad.disp);
3008 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3009 new_reg, *ad.index);
3012 else if (ad.index == NULL)
3014 int regno;
3015 enum reg_class cl;
3016 rtx set;
3017 rtx_insn *insns, *last_insn;
3018 /* Try to reload base into register only if the base is invalid
3019 for the address but with valid offset, case (4) above. */
3020 start_sequence ();
3021 new_reg = base_to_reg (&ad);
3023 /* base + disp => new base, cases (1) and (3) above. */
3024 /* Another option would be to reload the displacement into an
3025 index register. However, postreload has code to optimize
3026 address reloads that have the same base and different
3027 displacements, so reloading into an index register would
3028 not necessarily be a win. */
3029 if (new_reg == NULL_RTX)
3030 new_reg = base_plus_disp_to_reg (&ad);
3031 insns = get_insns ();
3032 last_insn = get_last_insn ();
3033 /* If we generated at least two insns, try last insn source as
3034 an address. If we succeed, we generate one less insn. */
3035 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3036 && GET_CODE (SET_SRC (set)) == PLUS
3037 && REG_P (XEXP (SET_SRC (set), 0))
3038 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3040 *ad.inner = SET_SRC (set);
3041 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3043 *ad.base_term = XEXP (SET_SRC (set), 0);
3044 *ad.disp_term = XEXP (SET_SRC (set), 1);
3045 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3046 get_index_code (&ad));
3047 regno = REGNO (*ad.base_term);
3048 if (regno >= FIRST_PSEUDO_REGISTER
3049 && cl != lra_get_allocno_class (regno))
3050 lra_change_class (regno, cl, " Change to", true);
3051 new_reg = SET_SRC (set);
3052 delete_insns_since (PREV_INSN (last_insn));
3055 /* Try if target can split displacement into legitimite new disp
3056 and offset. If it's the case, we replace the last insn with
3057 insns for base + offset => new_reg and set new_reg + new disp
3058 to *ad.inner. */
3059 last_insn = get_last_insn ();
3060 if ((set = single_set (last_insn)) != NULL_RTX
3061 && GET_CODE (SET_SRC (set)) == PLUS
3062 && REG_P (XEXP (SET_SRC (set), 0))
3063 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3064 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3066 rtx addend, disp = XEXP (SET_SRC (set), 1);
3067 if (targetm.legitimize_address_displacement (&disp, &addend,
3068 ad.mode))
3070 rtx_insn *new_insns;
3071 start_sequence ();
3072 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3073 new_insns = get_insns ();
3074 end_sequence ();
3075 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3076 delete_insns_since (PREV_INSN (last_insn));
3077 add_insn (new_insns);
3078 insns = get_insns ();
3081 end_sequence ();
3082 emit_insn (insns);
3083 *ad.inner = new_reg;
3085 else if (ad.disp_term != NULL)
3087 /* base + scale * index + disp => new base + scale * index,
3088 case (1) above. */
3089 new_reg = base_plus_disp_to_reg (&ad);
3090 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3091 new_reg, *ad.index);
3093 else if (get_index_scale (&ad) == 1)
3095 /* The last transformation to one reg will be made in
3096 curr_insn_transform function. */
3097 end_sequence ();
3098 return false;
3100 else
3102 /* base + scale * index => base + new_reg,
3103 case (1) above.
3104 Index part of address may become invalid. For example, we
3105 changed pseudo on the equivalent memory and a subreg of the
3106 pseudo onto the memory of different mode for which the scale is
3107 prohibitted. */
3108 new_reg = index_part_to_reg (&ad);
3109 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3110 *ad.base_term, new_reg);
3112 *before = get_insns ();
3113 end_sequence ();
3114 return true;
3117 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3118 Use process_address_1 as a helper function. Return true for any
3119 RTL changes.
3121 If CHECK_ONLY_P is true, just check address correctness. Return
3122 false if the address correct. */
3123 static bool
3124 process_address (int nop, bool check_only_p,
3125 rtx_insn **before, rtx_insn **after)
3127 bool res = false;
3129 while (process_address_1 (nop, check_only_p, before, after))
3131 if (check_only_p)
3132 return true;
3133 res = true;
3135 return res;
3138 /* Emit insns to reload VALUE into a new register. VALUE is an
3139 auto-increment or auto-decrement RTX whose operand is a register or
3140 memory location; so reloading involves incrementing that location.
3141 IN is either identical to VALUE, or some cheaper place to reload
3142 value being incremented/decremented from.
3144 INC_AMOUNT is the number to increment or decrement by (always
3145 positive and ignored for POST_MODIFY/PRE_MODIFY).
3147 Return pseudo containing the result. */
3148 static rtx
3149 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3151 /* REG or MEM to be copied and incremented. */
3152 rtx incloc = XEXP (value, 0);
3153 /* Nonzero if increment after copying. */
3154 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3155 || GET_CODE (value) == POST_MODIFY);
3156 rtx_insn *last;
3157 rtx inc;
3158 rtx_insn *add_insn;
3159 int code;
3160 rtx real_in = in == value ? incloc : in;
3161 rtx result;
3162 bool plus_p = true;
3164 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3166 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3167 || GET_CODE (XEXP (value, 1)) == MINUS);
3168 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3169 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3170 inc = XEXP (XEXP (value, 1), 1);
3172 else
3174 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3175 inc_amount = -inc_amount;
3177 inc = GEN_INT (inc_amount);
3180 if (! post && REG_P (incloc))
3181 result = incloc;
3182 else
3183 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3184 "INC/DEC result");
3186 if (real_in != result)
3188 /* First copy the location to the result register. */
3189 lra_assert (REG_P (result));
3190 emit_insn (gen_move_insn (result, real_in));
3193 /* We suppose that there are insns to add/sub with the constant
3194 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3195 old reload worked with this assumption. If the assumption
3196 becomes wrong, we should use approach in function
3197 base_plus_disp_to_reg. */
3198 if (in == value)
3200 /* See if we can directly increment INCLOC. */
3201 last = get_last_insn ();
3202 add_insn = emit_insn (plus_p
3203 ? gen_add2_insn (incloc, inc)
3204 : gen_sub2_insn (incloc, inc));
3206 code = recog_memoized (add_insn);
3207 if (code >= 0)
3209 if (! post && result != incloc)
3210 emit_insn (gen_move_insn (result, incloc));
3211 return result;
3213 delete_insns_since (last);
3216 /* If couldn't do the increment directly, must increment in RESULT.
3217 The way we do this depends on whether this is pre- or
3218 post-increment. For pre-increment, copy INCLOC to the reload
3219 register, increment it there, then save back. */
3220 if (! post)
3222 if (real_in != result)
3223 emit_insn (gen_move_insn (result, real_in));
3224 if (plus_p)
3225 emit_insn (gen_add2_insn (result, inc));
3226 else
3227 emit_insn (gen_sub2_insn (result, inc));
3228 if (result != incloc)
3229 emit_insn (gen_move_insn (incloc, result));
3231 else
3233 /* Post-increment.
3235 Because this might be a jump insn or a compare, and because
3236 RESULT may not be available after the insn in an input
3237 reload, we must do the incrementing before the insn being
3238 reloaded for.
3240 We have already copied IN to RESULT. Increment the copy in
3241 RESULT, save that back, then decrement RESULT so it has
3242 the original value. */
3243 if (plus_p)
3244 emit_insn (gen_add2_insn (result, inc));
3245 else
3246 emit_insn (gen_sub2_insn (result, inc));
3247 emit_insn (gen_move_insn (incloc, result));
3248 /* Restore non-modified value for the result. We prefer this
3249 way because it does not require an additional hard
3250 register. */
3251 if (plus_p)
3253 if (CONST_INT_P (inc))
3254 emit_insn (gen_add2_insn (result,
3255 gen_int_mode (-INTVAL (inc),
3256 GET_MODE (result))));
3257 else
3258 emit_insn (gen_sub2_insn (result, inc));
3260 else
3261 emit_insn (gen_add2_insn (result, inc));
3263 return result;
3266 /* Return true if the current move insn does not need processing as we
3267 already know that it satisfies its constraints. */
3268 static bool
3269 simple_move_p (void)
3271 rtx dest, src;
3272 enum reg_class dclass, sclass;
3274 lra_assert (curr_insn_set != NULL_RTX);
3275 dest = SET_DEST (curr_insn_set);
3276 src = SET_SRC (curr_insn_set);
3277 return ((dclass = get_op_class (dest)) != NO_REGS
3278 && (sclass = get_op_class (src)) != NO_REGS
3279 /* The backend guarantees that register moves of cost 2
3280 never need reloads. */
3281 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3284 /* Swap operands NOP and NOP + 1. */
3285 static inline void
3286 swap_operands (int nop)
3288 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3289 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3290 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3291 /* Swap the duplicates too. */
3292 lra_update_dup (curr_id, nop);
3293 lra_update_dup (curr_id, nop + 1);
3296 /* Main entry point of the constraint code: search the body of the
3297 current insn to choose the best alternative. It is mimicking insn
3298 alternative cost calculation model of former reload pass. That is
3299 because machine descriptions were written to use this model. This
3300 model can be changed in future. Make commutative operand exchange
3301 if it is chosen.
3303 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3304 constraints. Return true if any change happened during function
3305 call.
3307 If CHECK_ONLY_P is true then don't do any transformation. Just
3308 check that the insn satisfies all constraints. If the insn does
3309 not satisfy any constraint, return true. */
3310 static bool
3311 curr_insn_transform (bool check_only_p)
3313 int i, j, k;
3314 int n_operands;
3315 int n_alternatives;
3316 int commutative;
3317 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3318 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3319 rtx_insn *before, *after;
3320 bool alt_p = false;
3321 /* Flag that the insn has been changed through a transformation. */
3322 bool change_p;
3323 bool sec_mem_p;
3324 #ifdef SECONDARY_MEMORY_NEEDED
3325 bool use_sec_mem_p;
3326 #endif
3327 int max_regno_before;
3328 int reused_alternative_num;
3330 curr_insn_set = single_set (curr_insn);
3331 if (curr_insn_set != NULL_RTX && simple_move_p ())
3332 return false;
3334 no_input_reloads_p = no_output_reloads_p = false;
3335 goal_alt_number = -1;
3336 change_p = sec_mem_p = false;
3337 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3338 reloads; neither are insns that SET cc0. Insns that use CC0 are
3339 not allowed to have any input reloads. */
3340 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3341 no_output_reloads_p = true;
3343 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3344 no_input_reloads_p = true;
3345 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3346 no_output_reloads_p = true;
3348 n_operands = curr_static_id->n_operands;
3349 n_alternatives = curr_static_id->n_alternatives;
3351 /* Just return "no reloads" if insn has no operands with
3352 constraints. */
3353 if (n_operands == 0 || n_alternatives == 0)
3354 return false;
3356 max_regno_before = max_reg_num ();
3358 for (i = 0; i < n_operands; i++)
3360 goal_alt_matched[i][0] = -1;
3361 goal_alt_matches[i] = -1;
3364 commutative = curr_static_id->commutative;
3366 /* Now see what we need for pseudos that didn't get hard regs or got
3367 the wrong kind of hard reg. For this, we must consider all the
3368 operands together against the register constraints. */
3370 best_losers = best_overall = INT_MAX;
3371 best_reload_sum = 0;
3373 curr_swapped = false;
3374 goal_alt_swapped = false;
3376 if (! check_only_p)
3377 /* Make equivalence substitution and memory subreg elimination
3378 before address processing because an address legitimacy can
3379 depend on memory mode. */
3380 for (i = 0; i < n_operands; i++)
3382 rtx op = *curr_id->operand_loc[i];
3383 rtx subst, old = op;
3384 bool op_change_p = false;
3386 if (GET_CODE (old) == SUBREG)
3387 old = SUBREG_REG (old);
3388 subst = get_equiv_with_elimination (old, curr_insn);
3389 original_subreg_reg_mode[i] = VOIDmode;
3390 if (subst != old)
3392 subst = copy_rtx (subst);
3393 lra_assert (REG_P (old));
3394 if (GET_CODE (op) != SUBREG)
3395 *curr_id->operand_loc[i] = subst;
3396 else
3398 SUBREG_REG (op) = subst;
3399 if (GET_MODE (subst) == VOIDmode)
3400 original_subreg_reg_mode[i] = GET_MODE (old);
3402 if (lra_dump_file != NULL)
3404 fprintf (lra_dump_file,
3405 "Changing pseudo %d in operand %i of insn %u on equiv ",
3406 REGNO (old), i, INSN_UID (curr_insn));
3407 dump_value_slim (lra_dump_file, subst, 1);
3408 fprintf (lra_dump_file, "\n");
3410 op_change_p = change_p = true;
3412 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3414 change_p = true;
3415 lra_update_dup (curr_id, i);
3419 /* Reload address registers and displacements. We do it before
3420 finding an alternative because of memory constraints. */
3421 before = after = NULL;
3422 for (i = 0; i < n_operands; i++)
3423 if (! curr_static_id->operand[i].is_operator
3424 && process_address (i, check_only_p, &before, &after))
3426 if (check_only_p)
3427 return true;
3428 change_p = true;
3429 lra_update_dup (curr_id, i);
3432 if (change_p)
3433 /* If we've changed the instruction then any alternative that
3434 we chose previously may no longer be valid. */
3435 lra_set_used_insn_alternative (curr_insn, -1);
3437 if (! check_only_p && curr_insn_set != NULL_RTX
3438 && check_and_process_move (&change_p, &sec_mem_p))
3439 return change_p;
3441 try_swapped:
3443 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3444 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3445 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3446 reused_alternative_num, INSN_UID (curr_insn));
3448 if (process_alt_operands (reused_alternative_num))
3449 alt_p = true;
3451 if (check_only_p)
3452 return ! alt_p || best_losers != 0;
3454 /* If insn is commutative (it's safe to exchange a certain pair of
3455 operands) then we need to try each alternative twice, the second
3456 time matching those two operands as if we had exchanged them. To
3457 do this, really exchange them in operands.
3459 If we have just tried the alternatives the second time, return
3460 operands to normal and drop through. */
3462 if (reused_alternative_num < 0 && commutative >= 0)
3464 curr_swapped = !curr_swapped;
3465 if (curr_swapped)
3467 swap_operands (commutative);
3468 goto try_swapped;
3470 else
3471 swap_operands (commutative);
3474 if (! alt_p && ! sec_mem_p)
3476 /* No alternative works with reloads?? */
3477 if (INSN_CODE (curr_insn) >= 0)
3478 fatal_insn ("unable to generate reloads for:", curr_insn);
3479 error_for_asm (curr_insn,
3480 "inconsistent operand constraints in an %<asm%>");
3481 /* Avoid further trouble with this insn. */
3482 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3483 lra_invalidate_insn_data (curr_insn);
3484 return true;
3487 /* If the best alternative is with operands 1 and 2 swapped, swap
3488 them. Update the operand numbers of any reloads already
3489 pushed. */
3491 if (goal_alt_swapped)
3493 if (lra_dump_file != NULL)
3494 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3495 INSN_UID (curr_insn));
3497 /* Swap the duplicates too. */
3498 swap_operands (commutative);
3499 change_p = true;
3502 #ifdef SECONDARY_MEMORY_NEEDED
3503 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3504 too conservatively. So we use the secondary memory only if there
3505 is no any alternative without reloads. */
3506 use_sec_mem_p = false;
3507 if (! alt_p)
3508 use_sec_mem_p = true;
3509 else if (sec_mem_p)
3511 for (i = 0; i < n_operands; i++)
3512 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3513 break;
3514 use_sec_mem_p = i < n_operands;
3517 if (use_sec_mem_p)
3519 rtx new_reg, src, dest, rld;
3520 machine_mode sec_mode, rld_mode;
3522 lra_assert (sec_mem_p);
3523 lra_assert (curr_static_id->operand[0].type == OP_OUT
3524 && curr_static_id->operand[1].type == OP_IN);
3525 dest = *curr_id->operand_loc[0];
3526 src = *curr_id->operand_loc[1];
3527 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3528 ? dest : src);
3529 rld_mode = GET_MODE (rld);
3530 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3531 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3532 #else
3533 sec_mode = rld_mode;
3534 #endif
3535 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3536 NO_REGS, "secondary");
3537 /* If the mode is changed, it should be wider. */
3538 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3539 if (sec_mode != rld_mode)
3541 /* If the target says specifically to use another mode for
3542 secondary memory moves we can not reuse the original
3543 insn. */
3544 after = emit_spill_move (false, new_reg, dest);
3545 lra_process_new_insns (curr_insn, NULL, after,
3546 "Inserting the sec. move");
3547 /* We may have non null BEFORE here (e.g. after address
3548 processing. */
3549 push_to_sequence (before);
3550 before = emit_spill_move (true, new_reg, src);
3551 emit_insn (before);
3552 before = get_insns ();
3553 end_sequence ();
3554 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3555 lra_set_insn_deleted (curr_insn);
3557 else if (dest == rld)
3559 *curr_id->operand_loc[0] = new_reg;
3560 after = emit_spill_move (false, new_reg, dest);
3561 lra_process_new_insns (curr_insn, NULL, after,
3562 "Inserting the sec. move");
3564 else
3566 *curr_id->operand_loc[1] = new_reg;
3567 /* See comments above. */
3568 push_to_sequence (before);
3569 before = emit_spill_move (true, new_reg, src);
3570 emit_insn (before);
3571 before = get_insns ();
3572 end_sequence ();
3573 lra_process_new_insns (curr_insn, before, NULL,
3574 "Inserting the sec. move");
3576 lra_update_insn_regno_info (curr_insn);
3577 return true;
3579 #endif
3581 lra_assert (goal_alt_number >= 0);
3582 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3584 if (lra_dump_file != NULL)
3586 const char *p;
3588 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3589 goal_alt_number, INSN_UID (curr_insn));
3590 for (i = 0; i < n_operands; i++)
3592 p = (curr_static_id->operand_alternative
3593 [goal_alt_number * n_operands + i].constraint);
3594 if (*p == '\0')
3595 continue;
3596 fprintf (lra_dump_file, " (%d) ", i);
3597 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3598 fputc (*p, lra_dump_file);
3600 if (INSN_CODE (curr_insn) >= 0
3601 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3602 fprintf (lra_dump_file, " {%s}", p);
3603 if (curr_id->sp_offset != 0)
3604 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3605 curr_id->sp_offset);
3606 fprintf (lra_dump_file, "\n");
3609 /* Right now, for any pair of operands I and J that are required to
3610 match, with J < I, goal_alt_matches[I] is J. Add I to
3611 goal_alt_matched[J]. */
3613 for (i = 0; i < n_operands; i++)
3614 if ((j = goal_alt_matches[i]) >= 0)
3616 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3618 /* We allow matching one output operand and several input
3619 operands. */
3620 lra_assert (k == 0
3621 || (curr_static_id->operand[j].type == OP_OUT
3622 && curr_static_id->operand[i].type == OP_IN
3623 && (curr_static_id->operand
3624 [goal_alt_matched[j][0]].type == OP_IN)));
3625 goal_alt_matched[j][k] = i;
3626 goal_alt_matched[j][k + 1] = -1;
3629 for (i = 0; i < n_operands; i++)
3630 goal_alt_win[i] |= goal_alt_match_win[i];
3632 /* Any constants that aren't allowed and can't be reloaded into
3633 registers are here changed into memory references. */
3634 for (i = 0; i < n_operands; i++)
3635 if (goal_alt_win[i])
3637 int regno;
3638 enum reg_class new_class;
3639 rtx reg = *curr_id->operand_loc[i];
3641 if (GET_CODE (reg) == SUBREG)
3642 reg = SUBREG_REG (reg);
3644 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3646 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3648 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3650 lra_assert (ok_p);
3651 lra_change_class (regno, new_class, " Change to", true);
3655 else
3657 const char *constraint;
3658 char c;
3659 rtx op = *curr_id->operand_loc[i];
3660 rtx subreg = NULL_RTX;
3661 machine_mode mode = curr_operand_mode[i];
3663 if (GET_CODE (op) == SUBREG)
3665 subreg = op;
3666 op = SUBREG_REG (op);
3667 mode = GET_MODE (op);
3670 if (CONST_POOL_OK_P (mode, op)
3671 && ((targetm.preferred_reload_class
3672 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3673 || no_input_reloads_p))
3675 rtx tem = force_const_mem (mode, op);
3677 change_p = true;
3678 if (subreg != NULL_RTX)
3679 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3681 *curr_id->operand_loc[i] = tem;
3682 lra_update_dup (curr_id, i);
3683 process_address (i, false, &before, &after);
3685 /* If the alternative accepts constant pool refs directly
3686 there will be no reload needed at all. */
3687 if (subreg != NULL_RTX)
3688 continue;
3689 /* Skip alternatives before the one requested. */
3690 constraint = (curr_static_id->operand_alternative
3691 [goal_alt_number * n_operands + i].constraint);
3692 for (;
3693 (c = *constraint) && c != ',' && c != '#';
3694 constraint += CONSTRAINT_LEN (c, constraint))
3696 enum constraint_num cn = lookup_constraint (constraint);
3697 if (insn_extra_memory_constraint (cn)
3698 && satisfies_memory_constraint_p (tem, cn))
3699 break;
3701 if (c == '\0' || c == ',' || c == '#')
3702 continue;
3704 goal_alt_win[i] = true;
3708 for (i = 0; i < n_operands; i++)
3710 int regno;
3711 bool optional_p = false;
3712 rtx old, new_reg;
3713 rtx op = *curr_id->operand_loc[i];
3715 if (goal_alt_win[i])
3717 if (goal_alt[i] == NO_REGS
3718 && REG_P (op)
3719 /* When we assign NO_REGS it means that we will not
3720 assign a hard register to the scratch pseudo by
3721 assigment pass and the scratch pseudo will be
3722 spilled. Spilled scratch pseudos are transformed
3723 back to scratches at the LRA end. */
3724 && lra_former_scratch_operand_p (curr_insn, i))
3726 int regno = REGNO (op);
3727 lra_change_class (regno, NO_REGS, " Change to", true);
3728 if (lra_get_regno_hard_regno (regno) >= 0)
3729 /* We don't have to mark all insn affected by the
3730 spilled pseudo as there is only one such insn, the
3731 current one. */
3732 reg_renumber[regno] = -1;
3734 /* We can do an optional reload. If the pseudo got a hard
3735 reg, we might improve the code through inheritance. If
3736 it does not get a hard register we coalesce memory/memory
3737 moves later. Ignore move insns to avoid cycling. */
3738 if (! lra_simple_p
3739 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3740 && goal_alt[i] != NO_REGS && REG_P (op)
3741 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3742 && regno < new_regno_start
3743 && ! lra_former_scratch_p (regno)
3744 && reg_renumber[regno] < 0
3745 /* Check that the optional reload pseudo will be able to
3746 hold given mode value. */
3747 && ! (prohibited_class_reg_set_mode_p
3748 (goal_alt[i], reg_class_contents[goal_alt[i]],
3749 PSEUDO_REGNO_MODE (regno)))
3750 && (curr_insn_set == NULL_RTX
3751 || !((REG_P (SET_SRC (curr_insn_set))
3752 || MEM_P (SET_SRC (curr_insn_set))
3753 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3754 && (REG_P (SET_DEST (curr_insn_set))
3755 || MEM_P (SET_DEST (curr_insn_set))
3756 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3757 optional_p = true;
3758 else
3759 continue;
3762 /* Operands that match previous ones have already been handled. */
3763 if (goal_alt_matches[i] >= 0)
3764 continue;
3766 /* We should not have an operand with a non-offsettable address
3767 appearing where an offsettable address will do. It also may
3768 be a case when the address should be special in other words
3769 not a general one (e.g. it needs no index reg). */
3770 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3772 enum reg_class rclass;
3773 rtx *loc = &XEXP (op, 0);
3774 enum rtx_code code = GET_CODE (*loc);
3776 push_to_sequence (before);
3777 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3778 MEM, SCRATCH);
3779 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3780 new_reg = emit_inc (rclass, *loc, *loc,
3781 /* This value does not matter for MODIFY. */
3782 GET_MODE_SIZE (GET_MODE (op)));
3783 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3784 "offsetable address", &new_reg))
3785 lra_emit_move (new_reg, *loc);
3786 before = get_insns ();
3787 end_sequence ();
3788 *loc = new_reg;
3789 lra_update_dup (curr_id, i);
3791 else if (goal_alt_matched[i][0] == -1)
3793 machine_mode mode;
3794 rtx reg, *loc;
3795 int hard_regno, byte;
3796 enum op_type type = curr_static_id->operand[i].type;
3798 loc = curr_id->operand_loc[i];
3799 mode = curr_operand_mode[i];
3800 if (GET_CODE (*loc) == SUBREG)
3802 reg = SUBREG_REG (*loc);
3803 byte = SUBREG_BYTE (*loc);
3804 if (REG_P (reg)
3805 /* Strict_low_part requires reload the register not
3806 the sub-register. */
3807 && (curr_static_id->operand[i].strict_low
3808 || (GET_MODE_SIZE (mode)
3809 <= GET_MODE_SIZE (GET_MODE (reg))
3810 && (hard_regno
3811 = get_try_hard_regno (REGNO (reg))) >= 0
3812 && (simplify_subreg_regno
3813 (hard_regno,
3814 GET_MODE (reg), byte, mode) < 0)
3815 && (goal_alt[i] == NO_REGS
3816 || (simplify_subreg_regno
3817 (ira_class_hard_regs[goal_alt[i]][0],
3818 GET_MODE (reg), byte, mode) >= 0)))))
3820 if (type == OP_OUT)
3821 type = OP_INOUT;
3822 loc = &SUBREG_REG (*loc);
3823 mode = GET_MODE (*loc);
3826 old = *loc;
3827 if (get_reload_reg (type, mode, old, goal_alt[i],
3828 loc != curr_id->operand_loc[i], "", &new_reg)
3829 && type != OP_OUT)
3831 push_to_sequence (before);
3832 lra_emit_move (new_reg, old);
3833 before = get_insns ();
3834 end_sequence ();
3836 *loc = new_reg;
3837 if (type != OP_IN
3838 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3840 start_sequence ();
3841 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3842 emit_insn (after);
3843 after = get_insns ();
3844 end_sequence ();
3845 *loc = new_reg;
3847 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3848 if (goal_alt_dont_inherit_ops[j] == i)
3850 lra_set_regno_unique_value (REGNO (new_reg));
3851 break;
3853 lra_update_dup (curr_id, i);
3855 else if (curr_static_id->operand[i].type == OP_IN
3856 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3857 == OP_OUT))
3859 /* generate reloads for input and matched outputs. */
3860 match_inputs[0] = i;
3861 match_inputs[1] = -1;
3862 match_reload (goal_alt_matched[i][0], match_inputs,
3863 goal_alt[i], &before, &after);
3865 else if (curr_static_id->operand[i].type == OP_OUT
3866 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3867 == OP_IN))
3868 /* Generate reloads for output and matched inputs. */
3869 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3870 else if (curr_static_id->operand[i].type == OP_IN
3871 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3872 == OP_IN))
3874 /* Generate reloads for matched inputs. */
3875 match_inputs[0] = i;
3876 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3877 match_inputs[j + 1] = k;
3878 match_inputs[j + 1] = -1;
3879 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3881 else
3882 /* We must generate code in any case when function
3883 process_alt_operands decides that it is possible. */
3884 gcc_unreachable ();
3885 if (optional_p)
3887 lra_assert (REG_P (op));
3888 regno = REGNO (op);
3889 op = *curr_id->operand_loc[i]; /* Substitution. */
3890 if (GET_CODE (op) == SUBREG)
3891 op = SUBREG_REG (op);
3892 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3893 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3894 lra_reg_info[REGNO (op)].restore_regno = regno;
3895 if (lra_dump_file != NULL)
3896 fprintf (lra_dump_file,
3897 " Making reload reg %d for reg %d optional\n",
3898 REGNO (op), regno);
3901 if (before != NULL_RTX || after != NULL_RTX
3902 || max_regno_before != max_reg_num ())
3903 change_p = true;
3904 if (change_p)
3906 lra_update_operator_dups (curr_id);
3907 /* Something changes -- process the insn. */
3908 lra_update_insn_regno_info (curr_insn);
3910 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3911 return change_p;
3914 /* Return true if INSN satisfies all constraints. In other words, no
3915 reload insns are needed. */
3916 bool
3917 lra_constrain_insn (rtx_insn *insn)
3919 int saved_new_regno_start = new_regno_start;
3920 int saved_new_insn_uid_start = new_insn_uid_start;
3921 bool change_p;
3923 curr_insn = insn;
3924 curr_id = lra_get_insn_recog_data (curr_insn);
3925 curr_static_id = curr_id->insn_static_data;
3926 new_insn_uid_start = get_max_uid ();
3927 new_regno_start = max_reg_num ();
3928 change_p = curr_insn_transform (true);
3929 new_regno_start = saved_new_regno_start;
3930 new_insn_uid_start = saved_new_insn_uid_start;
3931 return ! change_p;
3934 /* Return true if X is in LIST. */
3935 static bool
3936 in_list_p (rtx x, rtx list)
3938 for (; list != NULL_RTX; list = XEXP (list, 1))
3939 if (XEXP (list, 0) == x)
3940 return true;
3941 return false;
3944 /* Return true if X contains an allocatable hard register (if
3945 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3946 static bool
3947 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3949 int i, j;
3950 const char *fmt;
3951 enum rtx_code code;
3953 code = GET_CODE (x);
3954 if (REG_P (x))
3956 int regno = REGNO (x);
3957 HARD_REG_SET alloc_regs;
3959 if (hard_reg_p)
3961 if (regno >= FIRST_PSEUDO_REGISTER)
3962 regno = lra_get_regno_hard_regno (regno);
3963 if (regno < 0)
3964 return false;
3965 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3966 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3968 else
3970 if (regno < FIRST_PSEUDO_REGISTER)
3971 return false;
3972 if (! spilled_p)
3973 return true;
3974 return lra_get_regno_hard_regno (regno) < 0;
3977 fmt = GET_RTX_FORMAT (code);
3978 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3980 if (fmt[i] == 'e')
3982 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3983 return true;
3985 else if (fmt[i] == 'E')
3987 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3988 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3989 return true;
3992 return false;
3995 /* Return true if X contains a symbol reg. */
3996 static bool
3997 contains_symbol_ref_p (rtx x)
3999 int i, j;
4000 const char *fmt;
4001 enum rtx_code code;
4003 code = GET_CODE (x);
4004 if (code == SYMBOL_REF)
4005 return true;
4006 fmt = GET_RTX_FORMAT (code);
4007 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4009 if (fmt[i] == 'e')
4011 if (contains_symbol_ref_p (XEXP (x, i)))
4012 return true;
4014 else if (fmt[i] == 'E')
4016 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4017 if (contains_symbol_ref_p (XVECEXP (x, i, j)))
4018 return true;
4021 return false;
4024 /* Process all regs in location *LOC and change them on equivalent
4025 substitution. Return true if any change was done. */
4026 static bool
4027 loc_equivalence_change_p (rtx *loc)
4029 rtx subst, reg, x = *loc;
4030 bool result = false;
4031 enum rtx_code code = GET_CODE (x);
4032 const char *fmt;
4033 int i, j;
4035 if (code == SUBREG)
4037 reg = SUBREG_REG (x);
4038 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4039 && GET_MODE (subst) == VOIDmode)
4041 /* We cannot reload debug location. Simplify subreg here
4042 while we know the inner mode. */
4043 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4044 GET_MODE (reg), SUBREG_BYTE (x));
4045 return true;
4048 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4050 *loc = subst;
4051 return true;
4054 /* Scan all the operand sub-expressions. */
4055 fmt = GET_RTX_FORMAT (code);
4056 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4058 if (fmt[i] == 'e')
4059 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4060 else if (fmt[i] == 'E')
4061 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4062 result
4063 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4065 return result;
4068 /* Similar to loc_equivalence_change_p, but for use as
4069 simplify_replace_fn_rtx callback. DATA is insn for which the
4070 elimination is done. If it null we don't do the elimination. */
4071 static rtx
4072 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4074 if (!REG_P (loc))
4075 return NULL_RTX;
4077 rtx subst = (data == NULL
4078 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4079 if (subst != loc)
4080 return subst;
4082 return NULL_RTX;
4085 /* Maximum number of generated reload insns per an insn. It is for
4086 preventing this pass cycling in a bug case. */
4087 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4089 /* The current iteration number of this LRA pass. */
4090 int lra_constraint_iter;
4092 /* True if we substituted equiv which needs checking register
4093 allocation correctness because the equivalent value contains
4094 allocatable hard registers or when we restore multi-register
4095 pseudo. */
4096 bool lra_risky_transformations_p;
4098 /* Return true if REGNO is referenced in more than one block. */
4099 static bool
4100 multi_block_pseudo_p (int regno)
4102 basic_block bb = NULL;
4103 unsigned int uid;
4104 bitmap_iterator bi;
4106 if (regno < FIRST_PSEUDO_REGISTER)
4107 return false;
4109 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4110 if (bb == NULL)
4111 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4112 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4113 return true;
4114 return false;
4117 /* Return true if LIST contains a deleted insn. */
4118 static bool
4119 contains_deleted_insn_p (rtx_insn_list *list)
4121 for (; list != NULL_RTX; list = list->next ())
4122 if (NOTE_P (list->insn ())
4123 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4124 return true;
4125 return false;
4128 /* Return true if X contains a pseudo dying in INSN. */
4129 static bool
4130 dead_pseudo_p (rtx x, rtx_insn *insn)
4132 int i, j;
4133 const char *fmt;
4134 enum rtx_code code;
4136 if (REG_P (x))
4137 return (insn != NULL_RTX
4138 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4139 code = GET_CODE (x);
4140 fmt = GET_RTX_FORMAT (code);
4141 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4143 if (fmt[i] == 'e')
4145 if (dead_pseudo_p (XEXP (x, i), insn))
4146 return true;
4148 else if (fmt[i] == 'E')
4150 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4151 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4152 return true;
4155 return false;
4158 /* Return true if INSN contains a dying pseudo in INSN right hand
4159 side. */
4160 static bool
4161 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4163 rtx set = single_set (insn);
4165 gcc_assert (set != NULL);
4166 return dead_pseudo_p (SET_SRC (set), insn);
4169 /* Return true if any init insn of REGNO contains a dying pseudo in
4170 insn right hand side. */
4171 static bool
4172 init_insn_rhs_dead_pseudo_p (int regno)
4174 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4176 if (insns == NULL)
4177 return false;
4178 for (; insns != NULL_RTX; insns = insns->next ())
4179 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4180 return true;
4181 return false;
4184 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4185 reverse only if we have one init insn with given REGNO as a
4186 source. */
4187 static bool
4188 reverse_equiv_p (int regno)
4190 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4191 rtx set;
4193 if (insns == NULL)
4194 return false;
4195 if (! INSN_P (insns->insn ())
4196 || insns->next () != NULL)
4197 return false;
4198 if ((set = single_set (insns->insn ())) == NULL_RTX)
4199 return false;
4200 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4203 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4204 call this function only for non-reverse equivalence. */
4205 static bool
4206 contains_reloaded_insn_p (int regno)
4208 rtx set;
4209 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4211 for (; list != NULL; list = list->next ())
4212 if ((set = single_set (list->insn ())) == NULL_RTX
4213 || ! REG_P (SET_DEST (set))
4214 || (int) REGNO (SET_DEST (set)) != regno)
4215 return true;
4216 return false;
4219 /* Entry function of LRA constraint pass. Return true if the
4220 constraint pass did change the code. */
4221 bool
4222 lra_constraints (bool first_p)
4224 bool changed_p;
4225 int i, hard_regno, new_insns_num;
4226 unsigned int min_len, new_min_len, uid;
4227 rtx set, x, reg, dest_reg;
4228 basic_block last_bb;
4229 bitmap_head equiv_insn_bitmap;
4230 bitmap_iterator bi;
4232 lra_constraint_iter++;
4233 if (lra_dump_file != NULL)
4234 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4235 lra_constraint_iter);
4236 changed_p = false;
4237 if (pic_offset_table_rtx
4238 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4239 lra_risky_transformations_p = true;
4240 else
4241 lra_risky_transformations_p = false;
4242 new_insn_uid_start = get_max_uid ();
4243 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4244 /* Mark used hard regs for target stack size calulations. */
4245 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4246 if (lra_reg_info[i].nrefs != 0
4247 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4249 int j, nregs;
4251 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4252 for (j = 0; j < nregs; j++)
4253 df_set_regs_ever_live (hard_regno + j, true);
4255 /* Do elimination before the equivalence processing as we can spill
4256 some pseudos during elimination. */
4257 lra_eliminate (false, first_p);
4258 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4259 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4260 if (lra_reg_info[i].nrefs != 0)
4262 ira_reg_equiv[i].profitable_p = true;
4263 reg = regno_reg_rtx[i];
4264 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4266 bool pseudo_p = contains_reg_p (x, false, false);
4268 /* After RTL transformation, we can not guarantee that
4269 pseudo in the substitution was not reloaded which might
4270 make equivalence invalid. For example, in reverse
4271 equiv of p0
4273 p0 <- ...
4275 equiv_mem <- p0
4277 the memory address register was reloaded before the 2nd
4278 insn. */
4279 if ((! first_p && pseudo_p)
4280 /* We don't use DF for compilation speed sake. So it
4281 is problematic to update live info when we use an
4282 equivalence containing pseudos in more than one
4283 BB. */
4284 || (pseudo_p && multi_block_pseudo_p (i))
4285 /* If an init insn was deleted for some reason, cancel
4286 the equiv. We could update the equiv insns after
4287 transformations including an equiv insn deletion
4288 but it is not worthy as such cases are extremely
4289 rare. */
4290 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4291 /* If it is not a reverse equivalence, we check that a
4292 pseudo in rhs of the init insn is not dying in the
4293 insn. Otherwise, the live info at the beginning of
4294 the corresponding BB might be wrong after we
4295 removed the insn. When the equiv can be a
4296 constant, the right hand side of the init insn can
4297 be a pseudo. */
4298 || (! reverse_equiv_p (i)
4299 && (init_insn_rhs_dead_pseudo_p (i)
4300 /* If we reloaded the pseudo in an equivalence
4301 init insn, we can not remove the equiv init
4302 insns and the init insns might write into
4303 const memory in this case. */
4304 || contains_reloaded_insn_p (i)))
4305 /* Prevent access beyond equivalent memory for
4306 paradoxical subregs. */
4307 || (MEM_P (x)
4308 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4309 > GET_MODE_SIZE (GET_MODE (x))))
4310 || (pic_offset_table_rtx
4311 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4312 && (targetm.preferred_reload_class
4313 (x, lra_get_allocno_class (i)) == NO_REGS))
4314 || contains_symbol_ref_p (x))))
4315 ira_reg_equiv[i].defined_p = false;
4316 if (contains_reg_p (x, false, true))
4317 ira_reg_equiv[i].profitable_p = false;
4318 if (get_equiv (reg) != reg)
4319 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4322 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4323 update_equiv (i);
4324 /* We should add all insns containing pseudos which should be
4325 substituted by their equivalences. */
4326 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4327 lra_push_insn_by_uid (uid);
4328 min_len = lra_insn_stack_length ();
4329 new_insns_num = 0;
4330 last_bb = NULL;
4331 changed_p = false;
4332 while ((new_min_len = lra_insn_stack_length ()) != 0)
4334 curr_insn = lra_pop_insn ();
4335 --new_min_len;
4336 curr_bb = BLOCK_FOR_INSN (curr_insn);
4337 if (curr_bb != last_bb)
4339 last_bb = curr_bb;
4340 bb_reload_num = lra_curr_reload_num;
4342 if (min_len > new_min_len)
4344 min_len = new_min_len;
4345 new_insns_num = 0;
4347 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4348 internal_error
4349 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4350 MAX_RELOAD_INSNS_NUMBER);
4351 new_insns_num++;
4352 if (DEBUG_INSN_P (curr_insn))
4354 /* We need to check equivalence in debug insn and change
4355 pseudo to the equivalent value if necessary. */
4356 curr_id = lra_get_insn_recog_data (curr_insn);
4357 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4359 rtx old = *curr_id->operand_loc[0];
4360 *curr_id->operand_loc[0]
4361 = simplify_replace_fn_rtx (old, NULL_RTX,
4362 loc_equivalence_callback, curr_insn);
4363 if (old != *curr_id->operand_loc[0])
4365 lra_update_insn_regno_info (curr_insn);
4366 changed_p = true;
4370 else if (INSN_P (curr_insn))
4372 if ((set = single_set (curr_insn)) != NULL_RTX)
4374 dest_reg = SET_DEST (set);
4375 /* The equivalence pseudo could be set up as SUBREG in a
4376 case when it is a call restore insn in a mode
4377 different from the pseudo mode. */
4378 if (GET_CODE (dest_reg) == SUBREG)
4379 dest_reg = SUBREG_REG (dest_reg);
4380 if ((REG_P (dest_reg)
4381 && (x = get_equiv (dest_reg)) != dest_reg
4382 /* Remove insns which set up a pseudo whose value
4383 can not be changed. Such insns might be not in
4384 init_insns because we don't update equiv data
4385 during insn transformations.
4387 As an example, let suppose that a pseudo got
4388 hard register and on the 1st pass was not
4389 changed to equivalent constant. We generate an
4390 additional insn setting up the pseudo because of
4391 secondary memory movement. Then the pseudo is
4392 spilled and we use the equiv constant. In this
4393 case we should remove the additional insn and
4394 this insn is not init_insns list. */
4395 && (! MEM_P (x) || MEM_READONLY_P (x)
4396 /* Check that this is actually an insn setting
4397 up the equivalence. */
4398 || in_list_p (curr_insn,
4399 ira_reg_equiv
4400 [REGNO (dest_reg)].init_insns)))
4401 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4402 && in_list_p (curr_insn,
4403 ira_reg_equiv
4404 [REGNO (SET_SRC (set))].init_insns)))
4406 /* This is equiv init insn of pseudo which did not get a
4407 hard register -- remove the insn. */
4408 if (lra_dump_file != NULL)
4410 fprintf (lra_dump_file,
4411 " Removing equiv init insn %i (freq=%d)\n",
4412 INSN_UID (curr_insn),
4413 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4414 dump_insn_slim (lra_dump_file, curr_insn);
4416 if (contains_reg_p (x, true, false))
4417 lra_risky_transformations_p = true;
4418 lra_set_insn_deleted (curr_insn);
4419 continue;
4422 curr_id = lra_get_insn_recog_data (curr_insn);
4423 curr_static_id = curr_id->insn_static_data;
4424 init_curr_insn_input_reloads ();
4425 init_curr_operand_mode ();
4426 if (curr_insn_transform (false))
4427 changed_p = true;
4428 /* Check non-transformed insns too for equiv change as USE
4429 or CLOBBER don't need reloads but can contain pseudos
4430 being changed on their equivalences. */
4431 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4432 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4434 lra_update_insn_regno_info (curr_insn);
4435 changed_p = true;
4439 bitmap_clear (&equiv_insn_bitmap);
4440 /* If we used a new hard regno, changed_p should be true because the
4441 hard reg is assigned to a new pseudo. */
4442 #ifdef ENABLE_CHECKING
4443 if (! changed_p)
4445 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4446 if (lra_reg_info[i].nrefs != 0
4447 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4449 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4451 for (j = 0; j < nregs; j++)
4452 lra_assert (df_regs_ever_live_p (hard_regno + j));
4455 #endif
4456 return changed_p;
4459 /* Initiate the LRA constraint pass. It is done once per
4460 function. */
4461 void
4462 lra_constraints_init (void)
4466 /* Finalize the LRA constraint pass. It is done once per
4467 function. */
4468 void
4469 lra_constraints_finish (void)
4475 /* This page contains code to do inheritance/split
4476 transformations. */
4478 /* Number of reloads passed so far in current EBB. */
4479 static int reloads_num;
4481 /* Number of calls passed so far in current EBB. */
4482 static int calls_num;
4484 /* Current reload pseudo check for validity of elements in
4485 USAGE_INSNS. */
4486 static int curr_usage_insns_check;
4488 /* Info about last usage of registers in EBB to do inheritance/split
4489 transformation. Inheritance transformation is done from a spilled
4490 pseudo and split transformations from a hard register or a pseudo
4491 assigned to a hard register. */
4492 struct usage_insns
4494 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4495 value INSNS is valid. The insns is chain of optional debug insns
4496 and a finishing non-debug insn using the corresponding reg. The
4497 value is also used to mark the registers which are set up in the
4498 current insn. The negated insn uid is used for this. */
4499 int check;
4500 /* Value of global reloads_num at the last insn in INSNS. */
4501 int reloads_num;
4502 /* Value of global reloads_nums at the last insn in INSNS. */
4503 int calls_num;
4504 /* It can be true only for splitting. And it means that the restore
4505 insn should be put after insn given by the following member. */
4506 bool after_p;
4507 /* Next insns in the current EBB which use the original reg and the
4508 original reg value is not changed between the current insn and
4509 the next insns. In order words, e.g. for inheritance, if we need
4510 to use the original reg value again in the next insns we can try
4511 to use the value in a hard register from a reload insn of the
4512 current insn. */
4513 rtx insns;
4516 /* Map: regno -> corresponding pseudo usage insns. */
4517 static struct usage_insns *usage_insns;
4519 static void
4520 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4522 usage_insns[regno].check = curr_usage_insns_check;
4523 usage_insns[regno].insns = insn;
4524 usage_insns[regno].reloads_num = reloads_num;
4525 usage_insns[regno].calls_num = calls_num;
4526 usage_insns[regno].after_p = after_p;
4529 /* The function is used to form list REGNO usages which consists of
4530 optional debug insns finished by a non-debug insn using REGNO.
4531 RELOADS_NUM is current number of reload insns processed so far. */
4532 static void
4533 add_next_usage_insn (int regno, rtx insn, int reloads_num)
4535 rtx next_usage_insns;
4537 if (usage_insns[regno].check == curr_usage_insns_check
4538 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4539 && DEBUG_INSN_P (insn))
4541 /* Check that we did not add the debug insn yet. */
4542 if (next_usage_insns != insn
4543 && (GET_CODE (next_usage_insns) != INSN_LIST
4544 || XEXP (next_usage_insns, 0) != insn))
4545 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4546 next_usage_insns);
4548 else if (NONDEBUG_INSN_P (insn))
4549 setup_next_usage_insn (regno, insn, reloads_num, false);
4550 else
4551 usage_insns[regno].check = 0;
4554 /* Return first non-debug insn in list USAGE_INSNS. */
4555 static rtx_insn *
4556 skip_usage_debug_insns (rtx usage_insns)
4558 rtx insn;
4560 /* Skip debug insns. */
4561 for (insn = usage_insns;
4562 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4563 insn = XEXP (insn, 1))
4565 return safe_as_a <rtx_insn *> (insn);
4568 /* Return true if we need secondary memory moves for insn in
4569 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4570 into the insn. */
4571 static bool
4572 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4573 rtx usage_insns ATTRIBUTE_UNUSED)
4575 #ifndef SECONDARY_MEMORY_NEEDED
4576 return false;
4577 #else
4578 rtx_insn *insn;
4579 rtx set, dest;
4580 enum reg_class cl;
4582 if (inher_cl == ALL_REGS
4583 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4584 return false;
4585 lra_assert (INSN_P (insn));
4586 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4587 return false;
4588 dest = SET_DEST (set);
4589 if (! REG_P (dest))
4590 return false;
4591 lra_assert (inher_cl != NO_REGS);
4592 cl = get_reg_class (REGNO (dest));
4593 return (cl != NO_REGS && cl != ALL_REGS
4594 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4595 #endif
4598 /* Registers involved in inheritance/split in the current EBB
4599 (inheritance/split pseudos and original registers). */
4600 static bitmap_head check_only_regs;
4602 /* Do inheritance transformations for insn INSN, which defines (if
4603 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4604 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4605 form as the "insns" field of usage_insns. Return true if we
4606 succeed in such transformation.
4608 The transformations look like:
4610 p <- ... i <- ...
4611 ... p <- i (new insn)
4612 ... =>
4613 <- ... p ... <- ... i ...
4615 ... i <- p (new insn)
4616 <- ... p ... <- ... i ...
4617 ... =>
4618 <- ... p ... <- ... i ...
4619 where p is a spilled original pseudo and i is a new inheritance pseudo.
4622 The inheritance pseudo has the smallest class of two classes CL and
4623 class of ORIGINAL REGNO. */
4624 static bool
4625 inherit_reload_reg (bool def_p, int original_regno,
4626 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4628 if (optimize_function_for_size_p (cfun))
4629 return false;
4631 enum reg_class rclass = lra_get_allocno_class (original_regno);
4632 rtx original_reg = regno_reg_rtx[original_regno];
4633 rtx new_reg, usage_insn;
4634 rtx_insn *new_insns;
4636 lra_assert (! usage_insns[original_regno].after_p);
4637 if (lra_dump_file != NULL)
4638 fprintf (lra_dump_file,
4639 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4640 if (! ira_reg_classes_intersect_p[cl][rclass])
4642 if (lra_dump_file != NULL)
4644 fprintf (lra_dump_file,
4645 " Rejecting inheritance for %d "
4646 "because of disjoint classes %s and %s\n",
4647 original_regno, reg_class_names[cl],
4648 reg_class_names[rclass]);
4649 fprintf (lra_dump_file,
4650 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4652 return false;
4654 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4655 /* We don't use a subset of two classes because it can be
4656 NO_REGS. This transformation is still profitable in most
4657 cases even if the classes are not intersected as register
4658 move is probably cheaper than a memory load. */
4659 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4661 if (lra_dump_file != NULL)
4662 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4663 reg_class_names[cl], reg_class_names[rclass]);
4665 rclass = cl;
4667 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4669 /* Reject inheritance resulting in secondary memory moves.
4670 Otherwise, there is a danger in LRA cycling. Also such
4671 transformation will be unprofitable. */
4672 if (lra_dump_file != NULL)
4674 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4675 rtx set = single_set (insn);
4677 lra_assert (set != NULL_RTX);
4679 rtx dest = SET_DEST (set);
4681 lra_assert (REG_P (dest));
4682 fprintf (lra_dump_file,
4683 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4684 "as secondary mem is needed\n",
4685 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4686 original_regno, reg_class_names[rclass]);
4687 fprintf (lra_dump_file,
4688 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4690 return false;
4692 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4693 rclass, "inheritance");
4694 start_sequence ();
4695 if (def_p)
4696 lra_emit_move (original_reg, new_reg);
4697 else
4698 lra_emit_move (new_reg, original_reg);
4699 new_insns = get_insns ();
4700 end_sequence ();
4701 if (NEXT_INSN (new_insns) != NULL_RTX)
4703 if (lra_dump_file != NULL)
4705 fprintf (lra_dump_file,
4706 " Rejecting inheritance %d->%d "
4707 "as it results in 2 or more insns:\n",
4708 original_regno, REGNO (new_reg));
4709 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4710 fprintf (lra_dump_file,
4711 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4713 return false;
4715 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg);
4716 lra_update_insn_regno_info (insn);
4717 if (! def_p)
4718 /* We now have a new usage insn for original regno. */
4719 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4720 if (lra_dump_file != NULL)
4721 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4722 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4723 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4724 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4725 bitmap_set_bit (&check_only_regs, original_regno);
4726 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4727 if (def_p)
4728 lra_process_new_insns (insn, NULL, new_insns,
4729 "Add original<-inheritance");
4730 else
4731 lra_process_new_insns (insn, new_insns, NULL,
4732 "Add inheritance<-original");
4733 while (next_usage_insns != NULL_RTX)
4735 if (GET_CODE (next_usage_insns) != INSN_LIST)
4737 usage_insn = next_usage_insns;
4738 lra_assert (NONDEBUG_INSN_P (usage_insn));
4739 next_usage_insns = NULL;
4741 else
4743 usage_insn = XEXP (next_usage_insns, 0);
4744 lra_assert (DEBUG_INSN_P (usage_insn));
4745 next_usage_insns = XEXP (next_usage_insns, 1);
4747 lra_substitute_pseudo (&usage_insn, original_regno, new_reg);
4748 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4749 if (lra_dump_file != NULL)
4751 fprintf (lra_dump_file,
4752 " Inheritance reuse change %d->%d (bb%d):\n",
4753 original_regno, REGNO (new_reg),
4754 BLOCK_FOR_INSN (usage_insn)->index);
4755 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
4758 if (lra_dump_file != NULL)
4759 fprintf (lra_dump_file,
4760 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4761 return true;
4764 /* Return true if we need a caller save/restore for pseudo REGNO which
4765 was assigned to a hard register. */
4766 static inline bool
4767 need_for_call_save_p (int regno)
4769 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4770 return (usage_insns[regno].calls_num < calls_num
4771 && (overlaps_hard_reg_set_p
4772 ((flag_ipa_ra &&
4773 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4774 ? lra_reg_info[regno].actual_call_used_reg_set
4775 : call_used_reg_set,
4776 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4777 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4778 PSEUDO_REGNO_MODE (regno))));
4781 /* Global registers occurring in the current EBB. */
4782 static bitmap_head ebb_global_regs;
4784 /* Return true if we need a split for hard register REGNO or pseudo
4785 REGNO which was assigned to a hard register.
4786 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4787 used for reloads since the EBB end. It is an approximation of the
4788 used hard registers in the split range. The exact value would
4789 require expensive calculations. If we were aggressive with
4790 splitting because of the approximation, the split pseudo will save
4791 the same hard register assignment and will be removed in the undo
4792 pass. We still need the approximation because too aggressive
4793 splitting would result in too inaccurate cost calculation in the
4794 assignment pass because of too many generated moves which will be
4795 probably removed in the undo pass. */
4796 static inline bool
4797 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4799 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4801 lra_assert (hard_regno >= 0);
4802 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4803 /* Don't split eliminable hard registers, otherwise we can
4804 split hard registers like hard frame pointer, which
4805 lives on BB start/end according to DF-infrastructure,
4806 when there is a pseudo assigned to the register and
4807 living in the same BB. */
4808 && (regno >= FIRST_PSEUDO_REGISTER
4809 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4810 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4811 /* Don't split call clobbered hard regs living through
4812 calls, otherwise we might have a check problem in the
4813 assign sub-pass as in the most cases (exception is a
4814 situation when lra_risky_transformations_p value is
4815 true) the assign pass assumes that all pseudos living
4816 through calls are assigned to call saved hard regs. */
4817 && (regno >= FIRST_PSEUDO_REGISTER
4818 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4819 || usage_insns[regno].calls_num == calls_num)
4820 /* We need at least 2 reloads to make pseudo splitting
4821 profitable. We should provide hard regno splitting in
4822 any case to solve 1st insn scheduling problem when
4823 moving hard register definition up might result in
4824 impossibility to find hard register for reload pseudo of
4825 small register class. */
4826 && (usage_insns[regno].reloads_num
4827 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4828 && (regno < FIRST_PSEUDO_REGISTER
4829 /* For short living pseudos, spilling + inheritance can
4830 be considered a substitution for splitting.
4831 Therefore we do not splitting for local pseudos. It
4832 decreases also aggressiveness of splitting. The
4833 minimal number of references is chosen taking into
4834 account that for 2 references splitting has no sense
4835 as we can just spill the pseudo. */
4836 || (regno >= FIRST_PSEUDO_REGISTER
4837 && lra_reg_info[regno].nrefs > 3
4838 && bitmap_bit_p (&ebb_global_regs, regno))))
4839 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4842 /* Return class for the split pseudo created from original pseudo with
4843 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4844 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4845 results in no secondary memory movements. */
4846 static enum reg_class
4847 choose_split_class (enum reg_class allocno_class,
4848 int hard_regno ATTRIBUTE_UNUSED,
4849 machine_mode mode ATTRIBUTE_UNUSED)
4851 #ifndef SECONDARY_MEMORY_NEEDED
4852 return allocno_class;
4853 #else
4854 int i;
4855 enum reg_class cl, best_cl = NO_REGS;
4856 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4857 = REGNO_REG_CLASS (hard_regno);
4859 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4860 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4861 return allocno_class;
4862 for (i = 0;
4863 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4864 i++)
4865 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4866 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4867 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4868 && (best_cl == NO_REGS
4869 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4870 best_cl = cl;
4871 return best_cl;
4872 #endif
4875 /* Do split transformations for insn INSN, which defines or uses
4876 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4877 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4878 "insns" field of usage_insns.
4880 The transformations look like:
4882 p <- ... p <- ...
4883 ... s <- p (new insn -- save)
4884 ... =>
4885 ... p <- s (new insn -- restore)
4886 <- ... p ... <- ... p ...
4888 <- ... p ... <- ... p ...
4889 ... s <- p (new insn -- save)
4890 ... =>
4891 ... p <- s (new insn -- restore)
4892 <- ... p ... <- ... p ...
4894 where p is an original pseudo got a hard register or a hard
4895 register and s is a new split pseudo. The save is put before INSN
4896 if BEFORE_P is true. Return true if we succeed in such
4897 transformation. */
4898 static bool
4899 split_reg (bool before_p, int original_regno, rtx_insn *insn,
4900 rtx next_usage_insns)
4902 enum reg_class rclass;
4903 rtx original_reg;
4904 int hard_regno, nregs;
4905 rtx new_reg, usage_insn;
4906 rtx_insn *restore, *save;
4907 bool after_p;
4908 bool call_save_p;
4910 if (original_regno < FIRST_PSEUDO_REGISTER)
4912 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4913 hard_regno = original_regno;
4914 call_save_p = false;
4915 nregs = 1;
4917 else
4919 hard_regno = reg_renumber[original_regno];
4920 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4921 rclass = lra_get_allocno_class (original_regno);
4922 original_reg = regno_reg_rtx[original_regno];
4923 call_save_p = need_for_call_save_p (original_regno);
4925 original_reg = regno_reg_rtx[original_regno];
4926 lra_assert (hard_regno >= 0);
4927 if (lra_dump_file != NULL)
4928 fprintf (lra_dump_file,
4929 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4930 if (call_save_p)
4932 machine_mode mode = GET_MODE (original_reg);
4934 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4935 hard_regno_nregs[hard_regno][mode],
4936 mode);
4937 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4939 else
4941 rclass = choose_split_class (rclass, hard_regno,
4942 GET_MODE (original_reg));
4943 if (rclass == NO_REGS)
4945 if (lra_dump_file != NULL)
4947 fprintf (lra_dump_file,
4948 " Rejecting split of %d(%s): "
4949 "no good reg class for %d(%s)\n",
4950 original_regno,
4951 reg_class_names[lra_get_allocno_class (original_regno)],
4952 hard_regno,
4953 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4954 fprintf
4955 (lra_dump_file,
4956 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4958 return false;
4960 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4961 rclass, "split");
4962 reg_renumber[REGNO (new_reg)] = hard_regno;
4964 save = emit_spill_move (true, new_reg, original_reg);
4965 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
4967 if (lra_dump_file != NULL)
4969 fprintf
4970 (lra_dump_file,
4971 " Rejecting split %d->%d resulting in > 2 save insns:\n",
4972 original_regno, REGNO (new_reg));
4973 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
4974 fprintf (lra_dump_file,
4975 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4977 return false;
4979 restore = emit_spill_move (false, new_reg, original_reg);
4980 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
4982 if (lra_dump_file != NULL)
4984 fprintf (lra_dump_file,
4985 " Rejecting split %d->%d "
4986 "resulting in > 2 restore insns:\n",
4987 original_regno, REGNO (new_reg));
4988 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
4989 fprintf (lra_dump_file,
4990 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4992 return false;
4994 after_p = usage_insns[original_regno].after_p;
4995 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4996 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4997 bitmap_set_bit (&check_only_regs, original_regno);
4998 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4999 for (;;)
5001 if (GET_CODE (next_usage_insns) != INSN_LIST)
5003 usage_insn = next_usage_insns;
5004 break;
5006 usage_insn = XEXP (next_usage_insns, 0);
5007 lra_assert (DEBUG_INSN_P (usage_insn));
5008 next_usage_insns = XEXP (next_usage_insns, 1);
5009 lra_substitute_pseudo (&usage_insn, original_regno, new_reg);
5010 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5011 if (lra_dump_file != NULL)
5013 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5014 original_regno, REGNO (new_reg));
5015 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5018 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5019 lra_assert (usage_insn != insn || (after_p && before_p));
5020 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5021 after_p ? NULL : restore,
5022 after_p ? restore : NULL,
5023 call_save_p
5024 ? "Add reg<-save" : "Add reg<-split");
5025 lra_process_new_insns (insn, before_p ? save : NULL,
5026 before_p ? NULL : save,
5027 call_save_p
5028 ? "Add save<-reg" : "Add split<-reg");
5029 if (nregs > 1)
5030 /* If we are trying to split multi-register. We should check
5031 conflicts on the next assignment sub-pass. IRA can allocate on
5032 sub-register levels, LRA do this on pseudos level right now and
5033 this discrepancy may create allocation conflicts after
5034 splitting. */
5035 lra_risky_transformations_p = true;
5036 if (lra_dump_file != NULL)
5037 fprintf (lra_dump_file,
5038 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5039 return true;
5042 /* Recognize that we need a split transformation for insn INSN, which
5043 defines or uses REGNO in its insn biggest MODE (we use it only if
5044 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5045 hard registers which might be used for reloads since the EBB end.
5046 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5047 uid before starting INSN processing. Return true if we succeed in
5048 such transformation. */
5049 static bool
5050 split_if_necessary (int regno, machine_mode mode,
5051 HARD_REG_SET potential_reload_hard_regs,
5052 bool before_p, rtx_insn *insn, int max_uid)
5054 bool res = false;
5055 int i, nregs = 1;
5056 rtx next_usage_insns;
5058 if (regno < FIRST_PSEUDO_REGISTER)
5059 nregs = hard_regno_nregs[regno][mode];
5060 for (i = 0; i < nregs; i++)
5061 if (usage_insns[regno + i].check == curr_usage_insns_check
5062 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5063 /* To avoid processing the register twice or more. */
5064 && ((GET_CODE (next_usage_insns) != INSN_LIST
5065 && INSN_UID (next_usage_insns) < max_uid)
5066 || (GET_CODE (next_usage_insns) == INSN_LIST
5067 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5068 && need_for_split_p (potential_reload_hard_regs, regno + i)
5069 && split_reg (before_p, regno + i, insn, next_usage_insns))
5070 res = true;
5071 return res;
5074 /* Check only registers living at the current program point in the
5075 current EBB. */
5076 static bitmap_head live_regs;
5078 /* Update live info in EBB given by its HEAD and TAIL insns after
5079 inheritance/split transformation. The function removes dead moves
5080 too. */
5081 static void
5082 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5084 unsigned int j;
5085 int i, regno;
5086 bool live_p;
5087 rtx_insn *prev_insn;
5088 rtx set;
5089 bool remove_p;
5090 basic_block last_bb, prev_bb, curr_bb;
5091 bitmap_iterator bi;
5092 struct lra_insn_reg *reg;
5093 edge e;
5094 edge_iterator ei;
5096 last_bb = BLOCK_FOR_INSN (tail);
5097 prev_bb = NULL;
5098 for (curr_insn = tail;
5099 curr_insn != PREV_INSN (head);
5100 curr_insn = prev_insn)
5102 prev_insn = PREV_INSN (curr_insn);
5103 /* We need to process empty blocks too. They contain
5104 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5105 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5106 continue;
5107 curr_bb = BLOCK_FOR_INSN (curr_insn);
5108 if (curr_bb != prev_bb)
5110 if (prev_bb != NULL)
5112 /* Update df_get_live_in (prev_bb): */
5113 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5114 if (bitmap_bit_p (&live_regs, j))
5115 bitmap_set_bit (df_get_live_in (prev_bb), j);
5116 else
5117 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5119 if (curr_bb != last_bb)
5121 /* Update df_get_live_out (curr_bb): */
5122 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5124 live_p = bitmap_bit_p (&live_regs, j);
5125 if (! live_p)
5126 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5127 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5129 live_p = true;
5130 break;
5132 if (live_p)
5133 bitmap_set_bit (df_get_live_out (curr_bb), j);
5134 else
5135 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5138 prev_bb = curr_bb;
5139 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5141 if (! NONDEBUG_INSN_P (curr_insn))
5142 continue;
5143 curr_id = lra_get_insn_recog_data (curr_insn);
5144 curr_static_id = curr_id->insn_static_data;
5145 remove_p = false;
5146 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
5147 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5148 && bitmap_bit_p (&check_only_regs, regno)
5149 && ! bitmap_bit_p (&live_regs, regno))
5150 remove_p = true;
5151 /* See which defined values die here. */
5152 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5153 if (reg->type == OP_OUT && ! reg->subreg_p)
5154 bitmap_clear_bit (&live_regs, reg->regno);
5155 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5156 if (reg->type == OP_OUT && ! reg->subreg_p)
5157 bitmap_clear_bit (&live_regs, reg->regno);
5158 /* Mark each used value as live. */
5159 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5160 if (reg->type != OP_OUT
5161 && bitmap_bit_p (&check_only_regs, reg->regno))
5162 bitmap_set_bit (&live_regs, reg->regno);
5163 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5164 if (reg->type != OP_OUT
5165 && bitmap_bit_p (&check_only_regs, reg->regno))
5166 bitmap_set_bit (&live_regs, reg->regno);
5167 if (curr_id->arg_hard_regs != NULL)
5168 /* Make argument hard registers live. */
5169 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5170 if (bitmap_bit_p (&check_only_regs, regno))
5171 bitmap_set_bit (&live_regs, regno);
5172 /* It is quite important to remove dead move insns because it
5173 means removing dead store. We don't need to process them for
5174 constraints. */
5175 if (remove_p)
5177 if (lra_dump_file != NULL)
5179 fprintf (lra_dump_file, " Removing dead insn:\n ");
5180 dump_insn_slim (lra_dump_file, curr_insn);
5182 lra_set_insn_deleted (curr_insn);
5187 /* The structure describes info to do an inheritance for the current
5188 insn. We need to collect such info first before doing the
5189 transformations because the transformations change the insn
5190 internal representation. */
5191 struct to_inherit
5193 /* Original regno. */
5194 int regno;
5195 /* Subsequent insns which can inherit original reg value. */
5196 rtx insns;
5199 /* Array containing all info for doing inheritance from the current
5200 insn. */
5201 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5203 /* Number elements in the previous array. */
5204 static int to_inherit_num;
5206 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5207 structure to_inherit. */
5208 static void
5209 add_to_inherit (int regno, rtx insns)
5211 int i;
5213 for (i = 0; i < to_inherit_num; i++)
5214 if (to_inherit[i].regno == regno)
5215 return;
5216 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5217 to_inherit[to_inherit_num].regno = regno;
5218 to_inherit[to_inherit_num++].insns = insns;
5221 /* Return the last non-debug insn in basic block BB, or the block begin
5222 note if none. */
5223 static rtx_insn *
5224 get_last_insertion_point (basic_block bb)
5226 rtx_insn *insn;
5228 FOR_BB_INSNS_REVERSE (bb, insn)
5229 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5230 return insn;
5231 gcc_unreachable ();
5234 /* Set up RES by registers living on edges FROM except the edge (FROM,
5235 TO) or by registers set up in a jump insn in BB FROM. */
5236 static void
5237 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5239 rtx_insn *last;
5240 struct lra_insn_reg *reg;
5241 edge e;
5242 edge_iterator ei;
5244 lra_assert (to != NULL);
5245 bitmap_clear (res);
5246 FOR_EACH_EDGE (e, ei, from->succs)
5247 if (e->dest != to)
5248 bitmap_ior_into (res, df_get_live_in (e->dest));
5249 last = get_last_insertion_point (from);
5250 if (! JUMP_P (last))
5251 return;
5252 curr_id = lra_get_insn_recog_data (last);
5253 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5254 if (reg->type != OP_IN)
5255 bitmap_set_bit (res, reg->regno);
5258 /* Used as a temporary results of some bitmap calculations. */
5259 static bitmap_head temp_bitmap;
5261 /* We split for reloads of small class of hard regs. The following
5262 defines how many hard regs the class should have to be qualified as
5263 small. The code is mostly oriented to x86/x86-64 architecture
5264 where some insns need to use only specific register or pair of
5265 registers and these register can live in RTL explicitly, e.g. for
5266 parameter passing. */
5267 static const int max_small_class_regs_num = 2;
5269 /* Do inheritance/split transformations in EBB starting with HEAD and
5270 finishing on TAIL. We process EBB insns in the reverse order.
5271 Return true if we did any inheritance/split transformation in the
5272 EBB.
5274 We should avoid excessive splitting which results in worse code
5275 because of inaccurate cost calculations for spilling new split
5276 pseudos in such case. To achieve this we do splitting only if
5277 register pressure is high in given basic block and there are reload
5278 pseudos requiring hard registers. We could do more register
5279 pressure calculations at any given program point to avoid necessary
5280 splitting even more but it is to expensive and the current approach
5281 works well enough. */
5282 static bool
5283 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5285 int i, src_regno, dst_regno, nregs;
5286 bool change_p, succ_p, update_reloads_num_p;
5287 rtx_insn *prev_insn, *last_insn;
5288 rtx next_usage_insns, set;
5289 enum reg_class cl;
5290 struct lra_insn_reg *reg;
5291 basic_block last_processed_bb, curr_bb = NULL;
5292 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5293 bitmap to_process;
5294 unsigned int j;
5295 bitmap_iterator bi;
5296 bool head_p, after_p;
5298 change_p = false;
5299 curr_usage_insns_check++;
5300 reloads_num = calls_num = 0;
5301 bitmap_clear (&check_only_regs);
5302 last_processed_bb = NULL;
5303 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5304 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5305 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5306 /* We don't process new insns generated in the loop. */
5307 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5309 prev_insn = PREV_INSN (curr_insn);
5310 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5311 curr_bb = BLOCK_FOR_INSN (curr_insn);
5312 if (last_processed_bb != curr_bb)
5314 /* We are at the end of BB. Add qualified living
5315 pseudos for potential splitting. */
5316 to_process = df_get_live_out (curr_bb);
5317 if (last_processed_bb != NULL)
5319 /* We are somewhere in the middle of EBB. */
5320 get_live_on_other_edges (curr_bb, last_processed_bb,
5321 &temp_bitmap);
5322 to_process = &temp_bitmap;
5324 last_processed_bb = curr_bb;
5325 last_insn = get_last_insertion_point (curr_bb);
5326 after_p = (! JUMP_P (last_insn)
5327 && (! CALL_P (last_insn)
5328 || (find_reg_note (last_insn,
5329 REG_NORETURN, NULL_RTX) == NULL_RTX
5330 && ! SIBLING_CALL_P (last_insn))));
5331 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5332 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5334 if ((int) j >= lra_constraint_new_regno_start)
5335 break;
5336 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5338 if (j < FIRST_PSEUDO_REGISTER)
5339 SET_HARD_REG_BIT (live_hard_regs, j);
5340 else
5341 add_to_hard_reg_set (&live_hard_regs,
5342 PSEUDO_REGNO_MODE (j),
5343 reg_renumber[j]);
5344 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5348 src_regno = dst_regno = -1;
5349 if (NONDEBUG_INSN_P (curr_insn)
5350 && (set = single_set (curr_insn)) != NULL_RTX
5351 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5353 src_regno = REGNO (SET_SRC (set));
5354 dst_regno = REGNO (SET_DEST (set));
5356 update_reloads_num_p = true;
5357 if (src_regno < lra_constraint_new_regno_start
5358 && src_regno >= FIRST_PSEUDO_REGISTER
5359 && reg_renumber[src_regno] < 0
5360 && dst_regno >= lra_constraint_new_regno_start
5361 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5363 /* 'reload_pseudo <- original_pseudo'. */
5364 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5365 reloads_num++;
5366 update_reloads_num_p = false;
5367 succ_p = false;
5368 if (usage_insns[src_regno].check == curr_usage_insns_check
5369 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5370 succ_p = inherit_reload_reg (false, src_regno, cl,
5371 curr_insn, next_usage_insns);
5372 if (succ_p)
5373 change_p = true;
5374 else
5375 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5376 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5377 IOR_HARD_REG_SET (potential_reload_hard_regs,
5378 reg_class_contents[cl]);
5380 else if (src_regno >= lra_constraint_new_regno_start
5381 && dst_regno < lra_constraint_new_regno_start
5382 && dst_regno >= FIRST_PSEUDO_REGISTER
5383 && reg_renumber[dst_regno] < 0
5384 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5385 && usage_insns[dst_regno].check == curr_usage_insns_check
5386 && (next_usage_insns
5387 = usage_insns[dst_regno].insns) != NULL_RTX)
5389 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5390 reloads_num++;
5391 update_reloads_num_p = false;
5392 /* 'original_pseudo <- reload_pseudo'. */
5393 if (! JUMP_P (curr_insn)
5394 && inherit_reload_reg (true, dst_regno, cl,
5395 curr_insn, next_usage_insns))
5396 change_p = true;
5397 /* Invalidate. */
5398 usage_insns[dst_regno].check = 0;
5399 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5400 IOR_HARD_REG_SET (potential_reload_hard_regs,
5401 reg_class_contents[cl]);
5403 else if (INSN_P (curr_insn))
5405 int iter;
5406 int max_uid = get_max_uid ();
5408 curr_id = lra_get_insn_recog_data (curr_insn);
5409 curr_static_id = curr_id->insn_static_data;
5410 to_inherit_num = 0;
5411 /* Process insn definitions. */
5412 for (iter = 0; iter < 2; iter++)
5413 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5414 reg != NULL;
5415 reg = reg->next)
5416 if (reg->type != OP_IN
5417 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5419 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5420 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5421 && usage_insns[dst_regno].check == curr_usage_insns_check
5422 && (next_usage_insns
5423 = usage_insns[dst_regno].insns) != NULL_RTX)
5425 struct lra_insn_reg *r;
5427 for (r = curr_id->regs; r != NULL; r = r->next)
5428 if (r->type != OP_OUT && r->regno == dst_regno)
5429 break;
5430 /* Don't do inheritance if the pseudo is also
5431 used in the insn. */
5432 if (r == NULL)
5433 /* We can not do inheritance right now
5434 because the current insn reg info (chain
5435 regs) can change after that. */
5436 add_to_inherit (dst_regno, next_usage_insns);
5438 /* We can not process one reg twice here because of
5439 usage_insns invalidation. */
5440 if ((dst_regno < FIRST_PSEUDO_REGISTER
5441 || reg_renumber[dst_regno] >= 0)
5442 && ! reg->subreg_p && reg->type != OP_IN)
5444 HARD_REG_SET s;
5446 if (split_if_necessary (dst_regno, reg->biggest_mode,
5447 potential_reload_hard_regs,
5448 false, curr_insn, max_uid))
5449 change_p = true;
5450 CLEAR_HARD_REG_SET (s);
5451 if (dst_regno < FIRST_PSEUDO_REGISTER)
5452 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5453 else
5454 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5455 reg_renumber[dst_regno]);
5456 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5458 /* We should invalidate potential inheritance or
5459 splitting for the current insn usages to the next
5460 usage insns (see code below) as the output pseudo
5461 prevents this. */
5462 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5463 && reg_renumber[dst_regno] < 0)
5464 || (reg->type == OP_OUT && ! reg->subreg_p
5465 && (dst_regno < FIRST_PSEUDO_REGISTER
5466 || reg_renumber[dst_regno] >= 0)))
5468 /* Invalidate and mark definitions. */
5469 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5470 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5471 else
5473 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5474 for (i = 0; i < nregs; i++)
5475 usage_insns[dst_regno + i].check
5476 = -(int) INSN_UID (curr_insn);
5480 if (! JUMP_P (curr_insn))
5481 for (i = 0; i < to_inherit_num; i++)
5482 if (inherit_reload_reg (true, to_inherit[i].regno,
5483 ALL_REGS, curr_insn,
5484 to_inherit[i].insns))
5485 change_p = true;
5486 if (CALL_P (curr_insn))
5488 rtx cheap, pat, dest;
5489 rtx_insn *restore;
5490 int regno, hard_regno;
5492 calls_num++;
5493 if ((cheap = find_reg_note (curr_insn,
5494 REG_RETURNED, NULL_RTX)) != NULL_RTX
5495 && ((cheap = XEXP (cheap, 0)), true)
5496 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5497 && (hard_regno = reg_renumber[regno]) >= 0
5498 /* If there are pending saves/restores, the
5499 optimization is not worth. */
5500 && usage_insns[regno].calls_num == calls_num - 1
5501 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5503 /* Restore the pseudo from the call result as
5504 REG_RETURNED note says that the pseudo value is
5505 in the call result and the pseudo is an argument
5506 of the call. */
5507 pat = PATTERN (curr_insn);
5508 if (GET_CODE (pat) == PARALLEL)
5509 pat = XVECEXP (pat, 0, 0);
5510 dest = SET_DEST (pat);
5511 /* For multiple return values dest is PARALLEL.
5512 Currently we handle only single return value case. */
5513 if (REG_P (dest))
5515 start_sequence ();
5516 emit_move_insn (cheap, copy_rtx (dest));
5517 restore = get_insns ();
5518 end_sequence ();
5519 lra_process_new_insns (curr_insn, NULL, restore,
5520 "Inserting call parameter restore");
5521 /* We don't need to save/restore of the pseudo from
5522 this call. */
5523 usage_insns[regno].calls_num = calls_num;
5524 bitmap_set_bit (&check_only_regs, regno);
5528 to_inherit_num = 0;
5529 /* Process insn usages. */
5530 for (iter = 0; iter < 2; iter++)
5531 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5532 reg != NULL;
5533 reg = reg->next)
5534 if ((reg->type != OP_OUT
5535 || (reg->type == OP_OUT && reg->subreg_p))
5536 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5538 if (src_regno >= FIRST_PSEUDO_REGISTER
5539 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5541 if (usage_insns[src_regno].check == curr_usage_insns_check
5542 && (next_usage_insns
5543 = usage_insns[src_regno].insns) != NULL_RTX
5544 && NONDEBUG_INSN_P (curr_insn))
5545 add_to_inherit (src_regno, next_usage_insns);
5546 else if (usage_insns[src_regno].check
5547 != -(int) INSN_UID (curr_insn))
5548 /* Add usages but only if the reg is not set up
5549 in the same insn. */
5550 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5552 else if (src_regno < FIRST_PSEUDO_REGISTER
5553 || reg_renumber[src_regno] >= 0)
5555 bool before_p;
5556 rtx_insn *use_insn = curr_insn;
5558 before_p = (JUMP_P (curr_insn)
5559 || (CALL_P (curr_insn) && reg->type == OP_IN));
5560 if (NONDEBUG_INSN_P (curr_insn)
5561 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5562 && split_if_necessary (src_regno, reg->biggest_mode,
5563 potential_reload_hard_regs,
5564 before_p, curr_insn, max_uid))
5566 if (reg->subreg_p)
5567 lra_risky_transformations_p = true;
5568 change_p = true;
5569 /* Invalidate. */
5570 usage_insns[src_regno].check = 0;
5571 if (before_p)
5572 use_insn = PREV_INSN (curr_insn);
5574 if (NONDEBUG_INSN_P (curr_insn))
5576 if (src_regno < FIRST_PSEUDO_REGISTER)
5577 add_to_hard_reg_set (&live_hard_regs,
5578 reg->biggest_mode, src_regno);
5579 else
5580 add_to_hard_reg_set (&live_hard_regs,
5581 PSEUDO_REGNO_MODE (src_regno),
5582 reg_renumber[src_regno]);
5584 add_next_usage_insn (src_regno, use_insn, reloads_num);
5587 /* Process call args. */
5588 if (curr_id->arg_hard_regs != NULL)
5589 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5590 if (src_regno < FIRST_PSEUDO_REGISTER)
5592 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5593 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5595 for (i = 0; i < to_inherit_num; i++)
5597 src_regno = to_inherit[i].regno;
5598 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5599 curr_insn, to_inherit[i].insns))
5600 change_p = true;
5601 else
5602 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5605 if (update_reloads_num_p
5606 && NONDEBUG_INSN_P (curr_insn)
5607 && (set = single_set (curr_insn)) != NULL_RTX)
5609 int regno = -1;
5610 if ((REG_P (SET_DEST (set))
5611 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5612 && reg_renumber[regno] < 0
5613 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5614 || (REG_P (SET_SRC (set))
5615 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5616 && reg_renumber[regno] < 0
5617 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5619 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5620 reloads_num++;
5621 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5622 IOR_HARD_REG_SET (potential_reload_hard_regs,
5623 reg_class_contents[cl]);
5626 /* We reached the start of the current basic block. */
5627 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5628 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5630 /* We reached the beginning of the current block -- do
5631 rest of spliting in the current BB. */
5632 to_process = df_get_live_in (curr_bb);
5633 if (BLOCK_FOR_INSN (head) != curr_bb)
5635 /* We are somewhere in the middle of EBB. */
5636 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5637 curr_bb, &temp_bitmap);
5638 to_process = &temp_bitmap;
5640 head_p = true;
5641 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5643 if ((int) j >= lra_constraint_new_regno_start)
5644 break;
5645 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5646 && usage_insns[j].check == curr_usage_insns_check
5647 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5649 if (need_for_split_p (potential_reload_hard_regs, j))
5651 if (lra_dump_file != NULL && head_p)
5653 fprintf (lra_dump_file,
5654 " ----------------------------------\n");
5655 head_p = false;
5657 if (split_reg (false, j, bb_note (curr_bb),
5658 next_usage_insns))
5659 change_p = true;
5661 usage_insns[j].check = 0;
5666 return change_p;
5669 /* This value affects EBB forming. If probability of edge from EBB to
5670 a BB is not greater than the following value, we don't add the BB
5671 to EBB. */
5672 #define EBB_PROBABILITY_CUTOFF \
5673 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
5675 /* Current number of inheritance/split iteration. */
5676 int lra_inheritance_iter;
5678 /* Entry function for inheritance/split pass. */
5679 void
5680 lra_inheritance (void)
5682 int i;
5683 basic_block bb, start_bb;
5684 edge e;
5686 lra_inheritance_iter++;
5687 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5688 return;
5689 timevar_push (TV_LRA_INHERITANCE);
5690 if (lra_dump_file != NULL)
5691 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5692 lra_inheritance_iter);
5693 curr_usage_insns_check = 0;
5694 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5695 for (i = 0; i < lra_constraint_new_regno_start; i++)
5696 usage_insns[i].check = 0;
5697 bitmap_initialize (&check_only_regs, &reg_obstack);
5698 bitmap_initialize (&live_regs, &reg_obstack);
5699 bitmap_initialize (&temp_bitmap, &reg_obstack);
5700 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5701 FOR_EACH_BB_FN (bb, cfun)
5703 start_bb = bb;
5704 if (lra_dump_file != NULL)
5705 fprintf (lra_dump_file, "EBB");
5706 /* Form a EBB starting with BB. */
5707 bitmap_clear (&ebb_global_regs);
5708 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5709 for (;;)
5711 if (lra_dump_file != NULL)
5712 fprintf (lra_dump_file, " %d", bb->index);
5713 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5714 || LABEL_P (BB_HEAD (bb->next_bb)))
5715 break;
5716 e = find_fallthru_edge (bb->succs);
5717 if (! e)
5718 break;
5719 if (e->probability < EBB_PROBABILITY_CUTOFF)
5720 break;
5721 bb = bb->next_bb;
5723 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5724 if (lra_dump_file != NULL)
5725 fprintf (lra_dump_file, "\n");
5726 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5727 /* Remember that the EBB head and tail can change in
5728 inherit_in_ebb. */
5729 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5731 bitmap_clear (&ebb_global_regs);
5732 bitmap_clear (&temp_bitmap);
5733 bitmap_clear (&live_regs);
5734 bitmap_clear (&check_only_regs);
5735 free (usage_insns);
5737 timevar_pop (TV_LRA_INHERITANCE);
5742 /* This page contains code to undo failed inheritance/split
5743 transformations. */
5745 /* Current number of iteration undoing inheritance/split. */
5746 int lra_undo_inheritance_iter;
5748 /* Fix BB live info LIVE after removing pseudos created on pass doing
5749 inheritance/split which are REMOVED_PSEUDOS. */
5750 static void
5751 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5753 unsigned int regno;
5754 bitmap_iterator bi;
5756 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5757 if (bitmap_clear_bit (live, regno))
5758 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5761 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5762 number. */
5763 static int
5764 get_regno (rtx reg)
5766 if (GET_CODE (reg) == SUBREG)
5767 reg = SUBREG_REG (reg);
5768 if (REG_P (reg))
5769 return REGNO (reg);
5770 return -1;
5773 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5774 return true if we did any change. The undo transformations for
5775 inheritance looks like
5776 i <- i2
5777 p <- i => p <- i2
5778 or removing
5779 p <- i, i <- p, and i <- i3
5780 where p is original pseudo from which inheritance pseudo i was
5781 created, i and i3 are removed inheritance pseudos, i2 is another
5782 not removed inheritance pseudo. All split pseudos or other
5783 occurrences of removed inheritance pseudos are changed on the
5784 corresponding original pseudos.
5786 The function also schedules insns changed and created during
5787 inheritance/split pass for processing by the subsequent constraint
5788 pass. */
5789 static bool
5790 remove_inheritance_pseudos (bitmap remove_pseudos)
5792 basic_block bb;
5793 int regno, sregno, prev_sregno, dregno, restore_regno;
5794 rtx set, prev_set;
5795 rtx_insn *prev_insn;
5796 bool change_p, done_p;
5798 change_p = ! bitmap_empty_p (remove_pseudos);
5799 /* We can not finish the function right away if CHANGE_P is true
5800 because we need to marks insns affected by previous
5801 inheritance/split pass for processing by the subsequent
5802 constraint pass. */
5803 FOR_EACH_BB_FN (bb, cfun)
5805 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5806 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5807 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5809 if (! INSN_P (curr_insn))
5810 continue;
5811 done_p = false;
5812 sregno = dregno = -1;
5813 if (change_p && NONDEBUG_INSN_P (curr_insn)
5814 && (set = single_set (curr_insn)) != NULL_RTX)
5816 dregno = get_regno (SET_DEST (set));
5817 sregno = get_regno (SET_SRC (set));
5820 if (sregno >= 0 && dregno >= 0)
5822 if ((bitmap_bit_p (remove_pseudos, sregno)
5823 && (lra_reg_info[sregno].restore_regno == dregno
5824 || (bitmap_bit_p (remove_pseudos, dregno)
5825 && (lra_reg_info[sregno].restore_regno
5826 == lra_reg_info[dregno].restore_regno))))
5827 || (bitmap_bit_p (remove_pseudos, dregno)
5828 && lra_reg_info[dregno].restore_regno == sregno))
5829 /* One of the following cases:
5830 original <- removed inheritance pseudo
5831 removed inherit pseudo <- another removed inherit pseudo
5832 removed inherit pseudo <- original pseudo
5834 removed_split_pseudo <- original_reg
5835 original_reg <- removed_split_pseudo */
5837 if (lra_dump_file != NULL)
5839 fprintf (lra_dump_file, " Removing %s:\n",
5840 bitmap_bit_p (&lra_split_regs, sregno)
5841 || bitmap_bit_p (&lra_split_regs, dregno)
5842 ? "split" : "inheritance");
5843 dump_insn_slim (lra_dump_file, curr_insn);
5845 lra_set_insn_deleted (curr_insn);
5846 done_p = true;
5848 else if (bitmap_bit_p (remove_pseudos, sregno)
5849 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5851 /* Search the following pattern:
5852 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5853 original_pseudo <- inherit_or_split_pseudo1
5854 where the 2nd insn is the current insn and
5855 inherit_or_split_pseudo2 is not removed. If it is found,
5856 change the current insn onto:
5857 original_pseudo <- inherit_or_split_pseudo2. */
5858 for (prev_insn = PREV_INSN (curr_insn);
5859 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5860 prev_insn = PREV_INSN (prev_insn))
5862 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5863 && (prev_set = single_set (prev_insn)) != NULL_RTX
5864 /* There should be no subregs in insn we are
5865 searching because only the original reg might
5866 be in subreg when we changed the mode of
5867 load/store for splitting. */
5868 && REG_P (SET_DEST (prev_set))
5869 && REG_P (SET_SRC (prev_set))
5870 && (int) REGNO (SET_DEST (prev_set)) == sregno
5871 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5872 >= FIRST_PSEUDO_REGISTER)
5873 /* As we consider chain of inheritance or
5874 splitting described in above comment we should
5875 check that sregno and prev_sregno were
5876 inheritance/split pseudos created from the
5877 same original regno. */
5878 && (lra_reg_info[sregno].restore_regno
5879 == lra_reg_info[prev_sregno].restore_regno)
5880 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5882 lra_assert (GET_MODE (SET_SRC (prev_set))
5883 == GET_MODE (regno_reg_rtx[sregno]));
5884 if (GET_CODE (SET_SRC (set)) == SUBREG)
5885 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5886 else
5887 SET_SRC (set) = SET_SRC (prev_set);
5888 /* As we are finishing with processing the insn
5889 here, check the destination too as it might
5890 inheritance pseudo for another pseudo. */
5891 if (bitmap_bit_p (remove_pseudos, dregno)
5892 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
5893 && (restore_regno
5894 = lra_reg_info[dregno].restore_regno) >= 0)
5896 if (GET_CODE (SET_DEST (set)) == SUBREG)
5897 SUBREG_REG (SET_DEST (set))
5898 = regno_reg_rtx[restore_regno];
5899 else
5900 SET_DEST (set) = regno_reg_rtx[restore_regno];
5902 lra_push_insn_and_update_insn_regno_info (curr_insn);
5903 lra_set_used_insn_alternative_by_uid
5904 (INSN_UID (curr_insn), -1);
5905 done_p = true;
5906 if (lra_dump_file != NULL)
5908 fprintf (lra_dump_file, " Change reload insn:\n");
5909 dump_insn_slim (lra_dump_file, curr_insn);
5914 if (! done_p)
5916 struct lra_insn_reg *reg;
5917 bool restored_regs_p = false;
5918 bool kept_regs_p = false;
5920 curr_id = lra_get_insn_recog_data (curr_insn);
5921 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5923 regno = reg->regno;
5924 restore_regno = lra_reg_info[regno].restore_regno;
5925 if (restore_regno >= 0)
5927 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5929 lra_substitute_pseudo_within_insn (
5930 curr_insn, regno, regno_reg_rtx[restore_regno]);
5931 restored_regs_p = true;
5933 else
5934 kept_regs_p = true;
5937 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5939 /* The instruction has changed since the previous
5940 constraints pass. */
5941 lra_push_insn_and_update_insn_regno_info (curr_insn);
5942 lra_set_used_insn_alternative_by_uid
5943 (INSN_UID (curr_insn), -1);
5945 else if (restored_regs_p)
5946 /* The instruction has been restored to the form that
5947 it had during the previous constraints pass. */
5948 lra_update_insn_regno_info (curr_insn);
5949 if (restored_regs_p && lra_dump_file != NULL)
5951 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5952 dump_insn_slim (lra_dump_file, curr_insn);
5957 return change_p;
5960 /* If optional reload pseudos failed to get a hard register or was not
5961 inherited, it is better to remove optional reloads. We do this
5962 transformation after undoing inheritance to figure out necessity to
5963 remove optional reloads easier. Return true if we do any
5964 change. */
5965 static bool
5966 undo_optional_reloads (void)
5968 bool change_p, keep_p;
5969 unsigned int regno, uid;
5970 bitmap_iterator bi, bi2;
5971 rtx_insn *insn;
5972 rtx set, src, dest;
5973 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5975 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5976 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5977 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5979 keep_p = false;
5980 /* Keep optional reloads from previous subpasses. */
5981 if (lra_reg_info[regno].restore_regno < 0
5982 /* If the original pseudo changed its allocation, just
5983 removing the optional pseudo is dangerous as the original
5984 pseudo will have longer live range. */
5985 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
5986 keep_p = true;
5987 else if (reg_renumber[regno] >= 0)
5988 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
5990 insn = lra_insn_recog_data[uid]->insn;
5991 if ((set = single_set (insn)) == NULL_RTX)
5992 continue;
5993 src = SET_SRC (set);
5994 dest = SET_DEST (set);
5995 if (! REG_P (src) || ! REG_P (dest))
5996 continue;
5997 if (REGNO (dest) == regno
5998 /* Ignore insn for optional reloads itself. */
5999 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
6000 /* Check only inheritance on last inheritance pass. */
6001 && (int) REGNO (src) >= new_regno_start
6002 /* Check that the optional reload was inherited. */
6003 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6005 keep_p = true;
6006 break;
6009 if (keep_p)
6011 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6012 if (lra_dump_file != NULL)
6013 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6016 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6017 bitmap_initialize (&insn_bitmap, &reg_obstack);
6018 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6020 if (lra_dump_file != NULL)
6021 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6022 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6023 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6025 insn = lra_insn_recog_data[uid]->insn;
6026 if ((set = single_set (insn)) != NULL_RTX)
6028 src = SET_SRC (set);
6029 dest = SET_DEST (set);
6030 if (REG_P (src) && REG_P (dest)
6031 && ((REGNO (src) == regno
6032 && (lra_reg_info[regno].restore_regno
6033 == (int) REGNO (dest)))
6034 || (REGNO (dest) == regno
6035 && (lra_reg_info[regno].restore_regno
6036 == (int) REGNO (src)))))
6038 if (lra_dump_file != NULL)
6040 fprintf (lra_dump_file, " Deleting move %u\n",
6041 INSN_UID (insn));
6042 dump_insn_slim (lra_dump_file, insn);
6044 lra_set_insn_deleted (insn);
6045 continue;
6047 /* We should not worry about generation memory-memory
6048 moves here as if the corresponding inheritance did
6049 not work (inheritance pseudo did not get a hard reg),
6050 we remove the inheritance pseudo and the optional
6051 reload. */
6053 lra_substitute_pseudo_within_insn (
6054 insn, regno,
6055 regno_reg_rtx[lra_reg_info[regno].restore_regno]);
6056 lra_update_insn_regno_info (insn);
6057 if (lra_dump_file != NULL)
6059 fprintf (lra_dump_file,
6060 " Restoring original insn:\n");
6061 dump_insn_slim (lra_dump_file, insn);
6065 /* Clear restore_regnos. */
6066 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6067 lra_reg_info[regno].restore_regno = -1;
6068 bitmap_clear (&insn_bitmap);
6069 bitmap_clear (&removed_optional_reload_pseudos);
6070 return change_p;
6073 /* Entry function for undoing inheritance/split transformation. Return true
6074 if we did any RTL change in this pass. */
6075 bool
6076 lra_undo_inheritance (void)
6078 unsigned int regno;
6079 int restore_regno, hard_regno;
6080 int n_all_inherit, n_inherit, n_all_split, n_split;
6081 bitmap_head remove_pseudos;
6082 bitmap_iterator bi;
6083 bool change_p;
6085 lra_undo_inheritance_iter++;
6086 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6087 return false;
6088 if (lra_dump_file != NULL)
6089 fprintf (lra_dump_file,
6090 "\n********** Undoing inheritance #%d: **********\n\n",
6091 lra_undo_inheritance_iter);
6092 bitmap_initialize (&remove_pseudos, &reg_obstack);
6093 n_inherit = n_all_inherit = 0;
6094 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6095 if (lra_reg_info[regno].restore_regno >= 0)
6097 n_all_inherit++;
6098 if (reg_renumber[regno] < 0
6099 /* If the original pseudo changed its allocation, just
6100 removing inheritance is dangerous as for changing
6101 allocation we used shorter live-ranges. */
6102 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6103 bitmap_set_bit (&remove_pseudos, regno);
6104 else
6105 n_inherit++;
6107 if (lra_dump_file != NULL && n_all_inherit != 0)
6108 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6109 n_inherit, n_all_inherit,
6110 (double) n_inherit / n_all_inherit * 100);
6111 n_split = n_all_split = 0;
6112 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6113 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6115 n_all_split++;
6116 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6117 ? reg_renumber[restore_regno] : restore_regno);
6118 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6119 bitmap_set_bit (&remove_pseudos, regno);
6120 else
6122 n_split++;
6123 if (lra_dump_file != NULL)
6124 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6125 regno, restore_regno);
6128 if (lra_dump_file != NULL && n_all_split != 0)
6129 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6130 n_split, n_all_split,
6131 (double) n_split / n_all_split * 100);
6132 change_p = remove_inheritance_pseudos (&remove_pseudos);
6133 bitmap_clear (&remove_pseudos);
6134 /* Clear restore_regnos. */
6135 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6136 lra_reg_info[regno].restore_regno = -1;
6137 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6138 lra_reg_info[regno].restore_regno = -1;
6139 change_p = undo_optional_reloads () || change_p;
6140 return change_p;