compiler: Do not declare type switch variable outside case statements.
[official-gcc.git] / gcc / lra-constraints.c
blob0ddd842deea7fbf3205de29b762f5ae0cf0ff1d6
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2015 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains code for 3 passes: constraint pass,
23 inheritance/split pass, and pass for undoing failed inheritance and
24 split.
26 The major goal of constraint pass is to transform RTL to satisfy
27 insn and address constraints by:
28 o choosing insn alternatives;
29 o generating *reload insns* (or reloads in brief) and *reload
30 pseudos* which will get necessary hard registers later;
31 o substituting pseudos with equivalent values and removing the
32 instructions that initialized those pseudos.
34 The constraint pass has biggest and most complicated code in LRA.
35 There are a lot of important details like:
36 o reuse of input reload pseudos to simplify reload pseudo
37 allocations;
38 o some heuristics to choose insn alternative to improve the
39 inheritance;
40 o early clobbers etc.
42 The pass is mimicking former reload pass in alternative choosing
43 because the reload pass is oriented to current machine description
44 model. It might be changed if the machine description model is
45 changed.
47 There is special code for preventing all LRA and this pass cycling
48 in case of bugs.
50 On the first iteration of the pass we process every instruction and
51 choose an alternative for each one. On subsequent iterations we try
52 to avoid reprocessing instructions if we can be sure that the old
53 choice is still valid.
55 The inheritance/spilt pass is to transform code to achieve
56 ineheritance and live range splitting. It is done on backward
57 traversal of EBBs.
59 The inheritance optimization goal is to reuse values in hard
60 registers. There is analogous optimization in old reload pass. The
61 inheritance is achieved by following transformation:
63 reload_p1 <- p reload_p1 <- p
64 ... new_p <- reload_p1
65 ... => ...
66 reload_p2 <- p reload_p2 <- new_p
68 where p is spilled and not changed between the insns. Reload_p1 is
69 also called *original pseudo* and new_p is called *inheritance
70 pseudo*.
72 The subsequent assignment pass will try to assign the same (or
73 another if it is not possible) hard register to new_p as to
74 reload_p1 or reload_p2.
76 If the assignment pass fails to assign a hard register to new_p,
77 this file will undo the inheritance and restore the original code.
78 This is because implementing the above sequence with a spilled
79 new_p would make the code much worse. The inheritance is done in
80 EBB scope. The above is just a simplified example to get an idea
81 of the inheritance as the inheritance is also done for non-reload
82 insns.
84 Splitting (transformation) is also done in EBB scope on the same
85 pass as the inheritance:
87 r <- ... or ... <- r r <- ... or ... <- r
88 ... s <- r (new insn -- save)
89 ... =>
90 ... r <- s (new insn -- restore)
91 ... <- r ... <- r
93 The *split pseudo* s is assigned to the hard register of the
94 original pseudo or hard register r.
96 Splitting is done:
97 o In EBBs with high register pressure for global pseudos (living
98 in at least 2 BBs) and assigned to hard registers when there
99 are more one reloads needing the hard registers;
100 o for pseudos needing save/restore code around calls.
102 If the split pseudo still has the same hard register as the
103 original pseudo after the subsequent assignment pass or the
104 original pseudo was split, the opposite transformation is done on
105 the same pass for undoing inheritance. */
107 #undef REG_OK_STRICT
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "tm.h"
113 #include "hard-reg-set.h"
114 #include "rtl.h"
115 #include "tm_p.h"
116 #include "regs.h"
117 #include "insn-config.h"
118 #include "insn-codes.h"
119 #include "recog.h"
120 #include "output.h"
121 #include "addresses.h"
122 #include "target.h"
123 #include "hashtab.h"
124 #include "hash-set.h"
125 #include "vec.h"
126 #include "machmode.h"
127 #include "input.h"
128 #include "function.h"
129 #include "symtab.h"
130 #include "flags.h"
131 #include "statistics.h"
132 #include "double-int.h"
133 #include "real.h"
134 #include "fixed-value.h"
135 #include "alias.h"
136 #include "wide-int.h"
137 #include "inchash.h"
138 #include "tree.h"
139 #include "expmed.h"
140 #include "dojump.h"
141 #include "explow.h"
142 #include "calls.h"
143 #include "emit-rtl.h"
144 #include "varasm.h"
145 #include "stmt.h"
146 #include "expr.h"
147 #include "predict.h"
148 #include "dominance.h"
149 #include "cfg.h"
150 #include "cfgrtl.h"
151 #include "basic-block.h"
152 #include "except.h"
153 #include "optabs.h"
154 #include "df.h"
155 #include "ira.h"
156 #include "rtl-error.h"
157 #include "params.h"
158 #include "lra-int.h"
160 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
161 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
162 reload insns. */
163 static int bb_reload_num;
165 /* The current insn being processed and corresponding its single set
166 (NULL otherwise), its data (basic block, the insn data, the insn
167 static data, and the mode of each operand). */
168 static rtx_insn *curr_insn;
169 static rtx curr_insn_set;
170 static basic_block curr_bb;
171 static lra_insn_recog_data_t curr_id;
172 static struct lra_static_insn_data *curr_static_id;
173 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
174 /* Mode of the register substituted by its equivalence with VOIDmode
175 (e.g. constant) and whose subreg is given operand of the current
176 insn. VOIDmode in all other cases. */
177 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
181 /* Start numbers for new registers and insns at the current constraints
182 pass start. */
183 static int new_regno_start;
184 static int new_insn_uid_start;
186 /* If LOC is nonnull, strip any outer subreg from it. */
187 static inline rtx *
188 strip_subreg (rtx *loc)
190 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
193 /* Return hard regno of REGNO or if it is was not assigned to a hard
194 register, use a hard register from its allocno class. */
195 static int
196 get_try_hard_regno (int regno)
198 int hard_regno;
199 enum reg_class rclass;
201 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
202 hard_regno = lra_get_regno_hard_regno (regno);
203 if (hard_regno >= 0)
204 return hard_regno;
205 rclass = lra_get_allocno_class (regno);
206 if (rclass == NO_REGS)
207 return -1;
208 return ira_class_hard_regs[rclass][0];
211 /* Return final hard regno (plus offset) which will be after
212 elimination. We do this for matching constraints because the final
213 hard regno could have a different class. */
214 static int
215 get_final_hard_regno (int hard_regno, int offset)
217 if (hard_regno < 0)
218 return hard_regno;
219 hard_regno = lra_get_elimination_hard_regno (hard_regno);
220 return hard_regno + offset;
223 /* Return hard regno of X after removing subreg and making
224 elimination. If X is not a register or subreg of register, return
225 -1. For pseudo use its assignment. */
226 static int
227 get_hard_regno (rtx x)
229 rtx reg;
230 int offset, hard_regno;
232 reg = x;
233 if (GET_CODE (x) == SUBREG)
234 reg = SUBREG_REG (x);
235 if (! REG_P (reg))
236 return -1;
237 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
238 hard_regno = lra_get_regno_hard_regno (hard_regno);
239 if (hard_regno < 0)
240 return -1;
241 offset = 0;
242 if (GET_CODE (x) == SUBREG)
243 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
244 SUBREG_BYTE (x), GET_MODE (x));
245 return get_final_hard_regno (hard_regno, offset);
248 /* If REGNO is a hard register or has been allocated a hard register,
249 return the class of that register. If REGNO is a reload pseudo
250 created by the current constraints pass, return its allocno class.
251 Return NO_REGS otherwise. */
252 static enum reg_class
253 get_reg_class (int regno)
255 int hard_regno;
257 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
258 hard_regno = lra_get_regno_hard_regno (regno);
259 if (hard_regno >= 0)
261 hard_regno = get_final_hard_regno (hard_regno, 0);
262 return REGNO_REG_CLASS (hard_regno);
264 if (regno >= new_regno_start)
265 return lra_get_allocno_class (regno);
266 return NO_REGS;
269 /* Return true if REG satisfies (or will satisfy) reg class constraint
270 CL. Use elimination first if REG is a hard register. If REG is a
271 reload pseudo created by this constraints pass, assume that it will
272 be allocated a hard register from its allocno class, but allow that
273 class to be narrowed to CL if it is currently a superset of CL.
275 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
276 REGNO (reg), or NO_REGS if no change in its class was needed. */
277 static bool
278 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
280 enum reg_class rclass, common_class;
281 machine_mode reg_mode;
282 int class_size, hard_regno, nregs, i, j;
283 int regno = REGNO (reg);
285 if (new_class != NULL)
286 *new_class = NO_REGS;
287 if (regno < FIRST_PSEUDO_REGISTER)
289 rtx final_reg = reg;
290 rtx *final_loc = &final_reg;
292 lra_eliminate_reg_if_possible (final_loc);
293 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
295 reg_mode = GET_MODE (reg);
296 rclass = get_reg_class (regno);
297 if (regno < new_regno_start
298 /* Do not allow the constraints for reload instructions to
299 influence the classes of new pseudos. These reloads are
300 typically moves that have many alternatives, and restricting
301 reload pseudos for one alternative may lead to situations
302 where other reload pseudos are no longer allocatable. */
303 || (INSN_UID (curr_insn) >= new_insn_uid_start
304 && curr_insn_set != NULL
305 && ((OBJECT_P (SET_SRC (curr_insn_set))
306 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
307 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
308 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
309 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
310 /* When we don't know what class will be used finally for reload
311 pseudos, we use ALL_REGS. */
312 return ((regno >= new_regno_start && rclass == ALL_REGS)
313 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
314 && ! hard_reg_set_subset_p (reg_class_contents[cl],
315 lra_no_alloc_regs)));
316 else
318 common_class = ira_reg_class_subset[rclass][cl];
319 if (new_class != NULL)
320 *new_class = common_class;
321 if (hard_reg_set_subset_p (reg_class_contents[common_class],
322 lra_no_alloc_regs))
323 return false;
324 /* Check that there are enough allocatable regs. */
325 class_size = ira_class_hard_regs_num[common_class];
326 for (i = 0; i < class_size; i++)
328 hard_regno = ira_class_hard_regs[common_class][i];
329 nregs = hard_regno_nregs[hard_regno][reg_mode];
330 if (nregs == 1)
331 return true;
332 for (j = 0; j < nregs; j++)
333 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
334 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
335 hard_regno + j))
336 break;
337 if (j >= nregs)
338 return true;
340 return false;
344 /* Return true if REGNO satisfies a memory constraint. */
345 static bool
346 in_mem_p (int regno)
348 return get_reg_class (regno) == NO_REGS;
351 /* Return 1 if ADDR is a valid memory address for mode MODE in address
352 space AS, and check that each pseudo has the proper kind of hard
353 reg. */
354 static int
355 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
356 rtx addr, addr_space_t as)
358 #ifdef GO_IF_LEGITIMATE_ADDRESS
359 lra_assert (ADDR_SPACE_GENERIC_P (as));
360 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
361 return 0;
363 win:
364 return 1;
365 #else
366 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
367 #endif
370 namespace {
371 /* Temporarily eliminates registers in an address (for the lifetime of
372 the object). */
373 class address_eliminator {
374 public:
375 address_eliminator (struct address_info *ad);
376 ~address_eliminator ();
378 private:
379 struct address_info *m_ad;
380 rtx *m_base_loc;
381 rtx m_base_reg;
382 rtx *m_index_loc;
383 rtx m_index_reg;
387 address_eliminator::address_eliminator (struct address_info *ad)
388 : m_ad (ad),
389 m_base_loc (strip_subreg (ad->base_term)),
390 m_base_reg (NULL_RTX),
391 m_index_loc (strip_subreg (ad->index_term)),
392 m_index_reg (NULL_RTX)
394 if (m_base_loc != NULL)
396 m_base_reg = *m_base_loc;
397 lra_eliminate_reg_if_possible (m_base_loc);
398 if (m_ad->base_term2 != NULL)
399 *m_ad->base_term2 = *m_ad->base_term;
401 if (m_index_loc != NULL)
403 m_index_reg = *m_index_loc;
404 lra_eliminate_reg_if_possible (m_index_loc);
408 address_eliminator::~address_eliminator ()
410 if (m_base_loc && *m_base_loc != m_base_reg)
412 *m_base_loc = m_base_reg;
413 if (m_ad->base_term2 != NULL)
414 *m_ad->base_term2 = *m_ad->base_term;
416 if (m_index_loc && *m_index_loc != m_index_reg)
417 *m_index_loc = m_index_reg;
420 /* Return true if the eliminated form of AD is a legitimate target address. */
421 static bool
422 valid_address_p (struct address_info *ad)
424 address_eliminator eliminator (ad);
425 return valid_address_p (ad->mode, *ad->outer, ad->as);
428 /* Return true if the eliminated form of memory reference OP satisfies
429 extra memory constraint CONSTRAINT. */
430 static bool
431 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
433 struct address_info ad;
435 decompose_mem_address (&ad, op);
436 address_eliminator eliminator (&ad);
437 return constraint_satisfied_p (op, constraint);
440 /* Return true if the eliminated form of address AD satisfies extra
441 address constraint CONSTRAINT. */
442 static bool
443 satisfies_address_constraint_p (struct address_info *ad,
444 enum constraint_num constraint)
446 address_eliminator eliminator (ad);
447 return constraint_satisfied_p (*ad->outer, constraint);
450 /* Return true if the eliminated form of address OP satisfies extra
451 address constraint CONSTRAINT. */
452 static bool
453 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
455 struct address_info ad;
457 decompose_lea_address (&ad, &op);
458 return satisfies_address_constraint_p (&ad, constraint);
461 /* Initiate equivalences for LRA. As we keep original equivalences
462 before any elimination, we need to make copies otherwise any change
463 in insns might change the equivalences. */
464 void
465 lra_init_equiv (void)
467 ira_expand_reg_equiv ();
468 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
470 rtx res;
472 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
473 ira_reg_equiv[i].memory = copy_rtx (res);
474 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
475 ira_reg_equiv[i].invariant = copy_rtx (res);
479 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
481 /* Update equivalence for REGNO. We need to this as the equivalence
482 might contain other pseudos which are changed by their
483 equivalences. */
484 static void
485 update_equiv (int regno)
487 rtx x;
489 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
490 ira_reg_equiv[regno].memory
491 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
492 NULL_RTX);
493 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
494 ira_reg_equiv[regno].invariant
495 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
496 NULL_RTX);
499 /* If we have decided to substitute X with another value, return that
500 value, otherwise return X. */
501 static rtx
502 get_equiv (rtx x)
504 int regno;
505 rtx res;
507 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
508 || ! ira_reg_equiv[regno].defined_p
509 || ! ira_reg_equiv[regno].profitable_p
510 || lra_get_regno_hard_regno (regno) >= 0)
511 return x;
512 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
514 if (targetm.cannot_substitute_mem_equiv_p (res))
515 return x;
516 return res;
518 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
519 return res;
520 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
521 return res;
522 gcc_unreachable ();
525 /* If we have decided to substitute X with the equivalent value,
526 return that value after elimination for INSN, otherwise return
527 X. */
528 static rtx
529 get_equiv_with_elimination (rtx x, rtx_insn *insn)
531 rtx res = get_equiv (x);
533 if (x == res || CONSTANT_P (res))
534 return res;
535 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
536 0, false, false, true);
539 /* Set up curr_operand_mode. */
540 static void
541 init_curr_operand_mode (void)
543 int nop = curr_static_id->n_operands;
544 for (int i = 0; i < nop; i++)
546 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
547 if (mode == VOIDmode)
549 /* The .md mode for address operands is the mode of the
550 addressed value rather than the mode of the address itself. */
551 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
552 mode = Pmode;
553 else
554 mode = curr_static_id->operand[i].mode;
556 curr_operand_mode[i] = mode;
562 /* The page contains code to reuse input reloads. */
564 /* Structure describes input reload of the current insns. */
565 struct input_reload
567 /* Reloaded value. */
568 rtx input;
569 /* Reload pseudo used. */
570 rtx reg;
573 /* The number of elements in the following array. */
574 static int curr_insn_input_reloads_num;
575 /* Array containing info about input reloads. It is used to find the
576 same input reload and reuse the reload pseudo in this case. */
577 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
579 /* Initiate data concerning reuse of input reloads for the current
580 insn. */
581 static void
582 init_curr_insn_input_reloads (void)
584 curr_insn_input_reloads_num = 0;
587 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
588 created input reload pseudo (only if TYPE is not OP_OUT). Don't
589 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
590 wrapped up in SUBREG. The result pseudo is returned through
591 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
592 reused the already created input reload pseudo. Use TITLE to
593 describe new registers for debug purposes. */
594 static bool
595 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
596 enum reg_class rclass, bool in_subreg_p,
597 const char *title, rtx *result_reg)
599 int i, regno;
600 enum reg_class new_class;
602 if (type == OP_OUT)
604 *result_reg
605 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
606 return true;
608 /* Prevent reuse value of expression with side effects,
609 e.g. volatile memory. */
610 if (! side_effects_p (original))
611 for (i = 0; i < curr_insn_input_reloads_num; i++)
612 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
613 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
615 rtx reg = curr_insn_input_reloads[i].reg;
616 regno = REGNO (reg);
617 /* If input is equal to original and both are VOIDmode,
618 GET_MODE (reg) might be still different from mode.
619 Ensure we don't return *result_reg with wrong mode. */
620 if (GET_MODE (reg) != mode)
622 if (in_subreg_p)
623 continue;
624 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
625 continue;
626 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
627 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
628 continue;
630 *result_reg = reg;
631 if (lra_dump_file != NULL)
633 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
634 dump_value_slim (lra_dump_file, original, 1);
636 if (new_class != lra_get_allocno_class (regno))
637 lra_change_class (regno, new_class, ", change to", false);
638 if (lra_dump_file != NULL)
639 fprintf (lra_dump_file, "\n");
640 return false;
642 *result_reg = lra_create_new_reg (mode, original, rclass, title);
643 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
644 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
645 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
646 return true;
651 /* The page contains code to extract memory address parts. */
653 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
654 static inline bool
655 ok_for_index_p_nonstrict (rtx reg)
657 unsigned regno = REGNO (reg);
659 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
662 /* A version of regno_ok_for_base_p for use here, when all pseudos
663 should count as OK. Arguments as for regno_ok_for_base_p. */
664 static inline bool
665 ok_for_base_p_nonstrict (rtx reg, machine_mode mode, addr_space_t as,
666 enum rtx_code outer_code, enum rtx_code index_code)
668 unsigned regno = REGNO (reg);
670 if (regno >= FIRST_PSEUDO_REGISTER)
671 return true;
672 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
677 /* The page contains major code to choose the current insn alternative
678 and generate reloads for it. */
680 /* Return the offset from REGNO of the least significant register
681 in (reg:MODE REGNO).
683 This function is used to tell whether two registers satisfy
684 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
686 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
687 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
689 lra_constraint_offset (int regno, machine_mode mode)
691 lra_assert (regno < FIRST_PSEUDO_REGISTER);
692 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
693 && SCALAR_INT_MODE_P (mode))
694 return hard_regno_nregs[regno][mode] - 1;
695 return 0;
698 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
699 if they are the same hard reg, and has special hacks for
700 auto-increment and auto-decrement. This is specifically intended for
701 process_alt_operands to use in determining whether two operands
702 match. X is the operand whose number is the lower of the two.
704 It is supposed that X is the output operand and Y is the input
705 operand. Y_HARD_REGNO is the final hard regno of register Y or
706 register in subreg Y as we know it now. Otherwise, it is a
707 negative value. */
708 static bool
709 operands_match_p (rtx x, rtx y, int y_hard_regno)
711 int i;
712 RTX_CODE code = GET_CODE (x);
713 const char *fmt;
715 if (x == y)
716 return true;
717 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
718 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
720 int j;
722 i = get_hard_regno (x);
723 if (i < 0)
724 goto slow;
726 if ((j = y_hard_regno) < 0)
727 goto slow;
729 i += lra_constraint_offset (i, GET_MODE (x));
730 j += lra_constraint_offset (j, GET_MODE (y));
732 return i == j;
735 /* If two operands must match, because they are really a single
736 operand of an assembler insn, then two post-increments are invalid
737 because the assembler insn would increment only once. On the
738 other hand, a post-increment matches ordinary indexing if the
739 post-increment is the output operand. */
740 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
741 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
743 /* Two pre-increments are invalid because the assembler insn would
744 increment only once. On the other hand, a pre-increment matches
745 ordinary indexing if the pre-increment is the input operand. */
746 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
747 || GET_CODE (y) == PRE_MODIFY)
748 return operands_match_p (x, XEXP (y, 0), -1);
750 slow:
752 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
753 && x == SUBREG_REG (y))
754 return true;
755 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
756 && SUBREG_REG (x) == y)
757 return true;
759 /* Now we have disposed of all the cases in which different rtx
760 codes can match. */
761 if (code != GET_CODE (y))
762 return false;
764 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
765 if (GET_MODE (x) != GET_MODE (y))
766 return false;
768 switch (code)
770 CASE_CONST_UNIQUE:
771 return false;
773 case LABEL_REF:
774 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
775 case SYMBOL_REF:
776 return XSTR (x, 0) == XSTR (y, 0);
778 default:
779 break;
782 /* Compare the elements. If any pair of corresponding elements fail
783 to match, return false for the whole things. */
785 fmt = GET_RTX_FORMAT (code);
786 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
788 int val, j;
789 switch (fmt[i])
791 case 'w':
792 if (XWINT (x, i) != XWINT (y, i))
793 return false;
794 break;
796 case 'i':
797 if (XINT (x, i) != XINT (y, i))
798 return false;
799 break;
801 case 'e':
802 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
803 if (val == 0)
804 return false;
805 break;
807 case '0':
808 break;
810 case 'E':
811 if (XVECLEN (x, i) != XVECLEN (y, i))
812 return false;
813 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
815 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
816 if (val == 0)
817 return false;
819 break;
821 /* It is believed that rtx's at this level will never
822 contain anything but integers and other rtx's, except for
823 within LABEL_REFs and SYMBOL_REFs. */
824 default:
825 gcc_unreachable ();
828 return true;
831 /* True if X is a constant that can be forced into the constant pool.
832 MODE is the mode of the operand, or VOIDmode if not known. */
833 #define CONST_POOL_OK_P(MODE, X) \
834 ((MODE) != VOIDmode \
835 && CONSTANT_P (X) \
836 && GET_CODE (X) != HIGH \
837 && !targetm.cannot_force_const_mem (MODE, X))
839 /* True if C is a non-empty register class that has too few registers
840 to be safely used as a reload target class. */
841 #define SMALL_REGISTER_CLASS_P(C) \
842 (ira_class_hard_regs_num [(C)] == 1 \
843 || (ira_class_hard_regs_num [(C)] >= 1 \
844 && targetm.class_likely_spilled_p (C)))
846 /* If REG is a reload pseudo, try to make its class satisfying CL. */
847 static void
848 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
850 enum reg_class rclass;
852 /* Do not make more accurate class from reloads generated. They are
853 mostly moves with a lot of constraints. Making more accurate
854 class may results in very narrow class and impossibility of find
855 registers for several reloads of one insn. */
856 if (INSN_UID (curr_insn) >= new_insn_uid_start)
857 return;
858 if (GET_CODE (reg) == SUBREG)
859 reg = SUBREG_REG (reg);
860 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
861 return;
862 if (in_class_p (reg, cl, &rclass) && rclass != cl)
863 lra_change_class (REGNO (reg), rclass, " Change to", true);
866 /* Generate reloads for matching OUT and INS (array of input operand
867 numbers with end marker -1) with reg class GOAL_CLASS. Add input
868 and output reloads correspondingly to the lists *BEFORE and *AFTER.
869 OUT might be negative. In this case we generate input reloads for
870 matched input operands INS. */
871 static void
872 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
873 rtx_insn **before, rtx_insn **after)
875 int i, in;
876 rtx new_in_reg, new_out_reg, reg, clobber;
877 machine_mode inmode, outmode;
878 rtx in_rtx = *curr_id->operand_loc[ins[0]];
879 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
881 inmode = curr_operand_mode[ins[0]];
882 outmode = out < 0 ? inmode : curr_operand_mode[out];
883 push_to_sequence (*before);
884 if (inmode != outmode)
886 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
888 reg = new_in_reg
889 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
890 goal_class, "");
891 if (SCALAR_INT_MODE_P (inmode))
892 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
893 else
894 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
895 LRA_SUBREG_P (new_out_reg) = 1;
896 /* If the input reg is dying here, we can use the same hard
897 register for REG and IN_RTX. We do it only for original
898 pseudos as reload pseudos can die although original
899 pseudos still live where reload pseudos dies. */
900 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
901 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
902 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
904 else
906 reg = new_out_reg
907 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
908 goal_class, "");
909 if (SCALAR_INT_MODE_P (outmode))
910 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
911 else
912 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
913 /* NEW_IN_REG is non-paradoxical subreg. We don't want
914 NEW_OUT_REG living above. We add clobber clause for
915 this. This is just a temporary clobber. We can remove
916 it at the end of LRA work. */
917 clobber = emit_clobber (new_out_reg);
918 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
919 LRA_SUBREG_P (new_in_reg) = 1;
920 if (GET_CODE (in_rtx) == SUBREG)
922 rtx subreg_reg = SUBREG_REG (in_rtx);
924 /* If SUBREG_REG is dying here and sub-registers IN_RTX
925 and NEW_IN_REG are similar, we can use the same hard
926 register for REG and SUBREG_REG. */
927 if (REG_P (subreg_reg)
928 && (int) REGNO (subreg_reg) < lra_new_regno_start
929 && GET_MODE (subreg_reg) == outmode
930 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
931 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
932 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
936 else
938 /* Pseudos have values -- see comments for lra_reg_info.
939 Different pseudos with the same value do not conflict even if
940 they live in the same place. When we create a pseudo we
941 assign value of original pseudo (if any) from which we
942 created the new pseudo. If we create the pseudo from the
943 input pseudo, the new pseudo will no conflict with the input
944 pseudo which is wrong when the input pseudo lives after the
945 insn and as the new pseudo value is changed by the insn
946 output. Therefore we create the new pseudo from the output.
948 We cannot reuse the current output register because we might
949 have a situation like "a <- a op b", where the constraints
950 force the second input operand ("b") to match the output
951 operand ("a"). "b" must then be copied into a new register
952 so that it doesn't clobber the current value of "a". */
954 new_in_reg = new_out_reg
955 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
956 goal_class, "");
958 /* In operand can be got from transformations before processing insn
959 constraints. One example of such transformations is subreg
960 reloading (see function simplify_operand_subreg). The new
961 pseudos created by the transformations might have inaccurate
962 class (ALL_REGS) and we should make their classes more
963 accurate. */
964 narrow_reload_pseudo_class (in_rtx, goal_class);
965 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
966 *before = get_insns ();
967 end_sequence ();
968 for (i = 0; (in = ins[i]) >= 0; i++)
970 lra_assert
971 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
972 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
973 *curr_id->operand_loc[in] = new_in_reg;
975 lra_update_dups (curr_id, ins);
976 if (out < 0)
977 return;
978 /* See a comment for the input operand above. */
979 narrow_reload_pseudo_class (out_rtx, goal_class);
980 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
982 start_sequence ();
983 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
984 emit_insn (*after);
985 *after = get_insns ();
986 end_sequence ();
988 *curr_id->operand_loc[out] = new_out_reg;
989 lra_update_dup (curr_id, out);
992 /* Return register class which is union of all reg classes in insn
993 constraint alternative string starting with P. */
994 static enum reg_class
995 reg_class_from_constraints (const char *p)
997 int c, len;
998 enum reg_class op_class = NO_REGS;
1001 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1003 case '#':
1004 case ',':
1005 return op_class;
1007 case 'g':
1008 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1009 break;
1011 default:
1012 enum constraint_num cn = lookup_constraint (p);
1013 enum reg_class cl = reg_class_for_constraint (cn);
1014 if (cl == NO_REGS)
1016 if (insn_extra_address_constraint (cn))
1017 op_class
1018 = (reg_class_subunion
1019 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1020 ADDRESS, SCRATCH)]);
1021 break;
1024 op_class = reg_class_subunion[op_class][cl];
1025 break;
1027 while ((p += len), c);
1028 return op_class;
1031 /* If OP is a register, return the class of the register as per
1032 get_reg_class, otherwise return NO_REGS. */
1033 static inline enum reg_class
1034 get_op_class (rtx op)
1036 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1039 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1040 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1041 SUBREG for VAL to make them equal. */
1042 static rtx_insn *
1043 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1045 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1047 /* Usually size of mem_pseudo is greater than val size but in
1048 rare cases it can be less as it can be defined by target
1049 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1050 if (! MEM_P (val))
1052 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1053 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1055 LRA_SUBREG_P (val) = 1;
1057 else
1059 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1060 LRA_SUBREG_P (mem_pseudo) = 1;
1063 return as_a <rtx_insn *> (to_p
1064 ? gen_move_insn (mem_pseudo, val)
1065 : gen_move_insn (val, mem_pseudo));
1068 /* Process a special case insn (register move), return true if we
1069 don't need to process it anymore. INSN should be a single set
1070 insn. Set up that RTL was changed through CHANGE_P and macro
1071 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1072 SEC_MEM_P. */
1073 static bool
1074 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1076 int sregno, dregno;
1077 rtx dest, src, dreg, sreg, old_sreg, new_reg, scratch_reg;
1078 rtx_insn *before;
1079 enum reg_class dclass, sclass, secondary_class;
1080 machine_mode sreg_mode;
1081 secondary_reload_info sri;
1083 lra_assert (curr_insn_set != NULL_RTX);
1084 dreg = dest = SET_DEST (curr_insn_set);
1085 sreg = src = SET_SRC (curr_insn_set);
1086 if (GET_CODE (dest) == SUBREG)
1087 dreg = SUBREG_REG (dest);
1088 if (GET_CODE (src) == SUBREG)
1089 sreg = SUBREG_REG (src);
1090 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1091 return false;
1092 sclass = dclass = NO_REGS;
1093 if (REG_P (dreg))
1094 dclass = get_reg_class (REGNO (dreg));
1095 if (dclass == ALL_REGS)
1096 /* ALL_REGS is used for new pseudos created by transformations
1097 like reload of SUBREG_REG (see function
1098 simplify_operand_subreg). We don't know their class yet. We
1099 should figure out the class from processing the insn
1100 constraints not in this fast path function. Even if ALL_REGS
1101 were a right class for the pseudo, secondary_... hooks usually
1102 are not define for ALL_REGS. */
1103 return false;
1104 sreg_mode = GET_MODE (sreg);
1105 old_sreg = sreg;
1106 if (REG_P (sreg))
1107 sclass = get_reg_class (REGNO (sreg));
1108 if (sclass == ALL_REGS)
1109 /* See comments above. */
1110 return false;
1111 if (sclass == NO_REGS && dclass == NO_REGS)
1112 return false;
1113 #ifdef SECONDARY_MEMORY_NEEDED
1114 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1115 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1116 && ((sclass != NO_REGS && dclass != NO_REGS)
1117 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1118 #endif
1121 *sec_mem_p = true;
1122 return false;
1124 #endif
1125 if (! REG_P (dreg) || ! REG_P (sreg))
1126 return false;
1127 sri.prev_sri = NULL;
1128 sri.icode = CODE_FOR_nothing;
1129 sri.extra_cost = 0;
1130 secondary_class = NO_REGS;
1131 /* Set up hard register for a reload pseudo for hook
1132 secondary_reload because some targets just ignore unassigned
1133 pseudos in the hook. */
1134 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1136 dregno = REGNO (dreg);
1137 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1139 else
1140 dregno = -1;
1141 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1143 sregno = REGNO (sreg);
1144 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1146 else
1147 sregno = -1;
1148 if (sclass != NO_REGS)
1149 secondary_class
1150 = (enum reg_class) targetm.secondary_reload (false, dest,
1151 (reg_class_t) sclass,
1152 GET_MODE (src), &sri);
1153 if (sclass == NO_REGS
1154 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1155 && dclass != NO_REGS))
1157 enum reg_class old_sclass = secondary_class;
1158 secondary_reload_info old_sri = sri;
1160 sri.prev_sri = NULL;
1161 sri.icode = CODE_FOR_nothing;
1162 sri.extra_cost = 0;
1163 secondary_class
1164 = (enum reg_class) targetm.secondary_reload (true, sreg,
1165 (reg_class_t) dclass,
1166 sreg_mode, &sri);
1167 /* Check the target hook consistency. */
1168 lra_assert
1169 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1170 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1171 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1173 if (sregno >= 0)
1174 reg_renumber [sregno] = -1;
1175 if (dregno >= 0)
1176 reg_renumber [dregno] = -1;
1177 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1178 return false;
1179 *change_p = true;
1180 new_reg = NULL_RTX;
1181 if (secondary_class != NO_REGS)
1182 new_reg = lra_create_new_reg_with_unique_value (sreg_mode, NULL_RTX,
1183 secondary_class,
1184 "secondary");
1185 start_sequence ();
1186 if (old_sreg != sreg)
1187 sreg = copy_rtx (sreg);
1188 if (sri.icode == CODE_FOR_nothing)
1189 lra_emit_move (new_reg, sreg);
1190 else
1192 enum reg_class scratch_class;
1194 scratch_class = (reg_class_from_constraints
1195 (insn_data[sri.icode].operand[2].constraint));
1196 scratch_reg = (lra_create_new_reg_with_unique_value
1197 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1198 scratch_class, "scratch"));
1199 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1200 sreg, scratch_reg));
1202 before = get_insns ();
1203 end_sequence ();
1204 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1205 if (new_reg != NULL_RTX)
1207 if (GET_CODE (src) == SUBREG)
1208 SUBREG_REG (src) = new_reg;
1209 else
1210 SET_SRC (curr_insn_set) = new_reg;
1212 else
1214 if (lra_dump_file != NULL)
1216 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1217 dump_insn_slim (lra_dump_file, curr_insn);
1219 lra_set_insn_deleted (curr_insn);
1220 return true;
1222 return false;
1225 /* The following data describe the result of process_alt_operands.
1226 The data are used in curr_insn_transform to generate reloads. */
1228 /* The chosen reg classes which should be used for the corresponding
1229 operands. */
1230 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1231 /* True if the operand should be the same as another operand and that
1232 other operand does not need a reload. */
1233 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1234 /* True if the operand does not need a reload. */
1235 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1236 /* True if the operand can be offsetable memory. */
1237 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1238 /* The number of an operand to which given operand can be matched to. */
1239 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1240 /* The number of elements in the following array. */
1241 static int goal_alt_dont_inherit_ops_num;
1242 /* Numbers of operands whose reload pseudos should not be inherited. */
1243 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1244 /* True if the insn commutative operands should be swapped. */
1245 static bool goal_alt_swapped;
1246 /* The chosen insn alternative. */
1247 static int goal_alt_number;
1249 /* The following five variables are used to choose the best insn
1250 alternative. They reflect final characteristics of the best
1251 alternative. */
1253 /* Number of necessary reloads and overall cost reflecting the
1254 previous value and other unpleasantness of the best alternative. */
1255 static int best_losers, best_overall;
1256 /* Overall number hard registers used for reloads. For example, on
1257 some targets we need 2 general registers to reload DFmode and only
1258 one floating point register. */
1259 static int best_reload_nregs;
1260 /* Overall number reflecting distances of previous reloading the same
1261 value. The distances are counted from the current BB start. It is
1262 used to improve inheritance chances. */
1263 static int best_reload_sum;
1265 /* True if the current insn should have no correspondingly input or
1266 output reloads. */
1267 static bool no_input_reloads_p, no_output_reloads_p;
1269 /* True if we swapped the commutative operands in the current
1270 insn. */
1271 static int curr_swapped;
1273 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1274 register of class CL. Add any input reloads to list BEFORE. AFTER
1275 is nonnull if *LOC is an automodified value; handle that case by
1276 adding the required output reloads to list AFTER. Return true if
1277 the RTL was changed.
1279 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1280 register. Return false if the address register is correct. */
1281 static bool
1282 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1283 enum reg_class cl)
1285 int regno;
1286 enum reg_class rclass, new_class;
1287 rtx reg;
1288 rtx new_reg;
1289 machine_mode mode;
1290 bool subreg_p, before_p = false;
1292 subreg_p = GET_CODE (*loc) == SUBREG;
1293 if (subreg_p)
1294 loc = &SUBREG_REG (*loc);
1295 reg = *loc;
1296 mode = GET_MODE (reg);
1297 if (! REG_P (reg))
1299 if (check_only_p)
1300 return true;
1301 /* Always reload memory in an address even if the target supports
1302 such addresses. */
1303 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1304 before_p = true;
1306 else
1308 regno = REGNO (reg);
1309 rclass = get_reg_class (regno);
1310 if (! check_only_p
1311 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1313 if (lra_dump_file != NULL)
1315 fprintf (lra_dump_file,
1316 "Changing pseudo %d in address of insn %u on equiv ",
1317 REGNO (reg), INSN_UID (curr_insn));
1318 dump_value_slim (lra_dump_file, *loc, 1);
1319 fprintf (lra_dump_file, "\n");
1321 *loc = copy_rtx (*loc);
1323 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1325 if (check_only_p)
1326 return true;
1327 reg = *loc;
1328 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1329 mode, reg, cl, subreg_p, "address", &new_reg))
1330 before_p = true;
1332 else if (new_class != NO_REGS && rclass != new_class)
1334 if (check_only_p)
1335 return true;
1336 lra_change_class (regno, new_class, " Change to", true);
1337 return false;
1339 else
1340 return false;
1342 if (before_p)
1344 push_to_sequence (*before);
1345 lra_emit_move (new_reg, reg);
1346 *before = get_insns ();
1347 end_sequence ();
1349 *loc = new_reg;
1350 if (after != NULL)
1352 start_sequence ();
1353 lra_emit_move (reg, new_reg);
1354 emit_insn (*after);
1355 *after = get_insns ();
1356 end_sequence ();
1358 return true;
1361 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1362 the insn to be inserted before curr insn. AFTER returns the
1363 the insn to be inserted after curr insn. ORIGREG and NEWREG
1364 are the original reg and new reg for reload. */
1365 static void
1366 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1367 rtx newreg)
1369 if (before)
1371 push_to_sequence (*before);
1372 lra_emit_move (newreg, origreg);
1373 *before = get_insns ();
1374 end_sequence ();
1376 if (after)
1378 start_sequence ();
1379 lra_emit_move (origreg, newreg);
1380 emit_insn (*after);
1381 *after = get_insns ();
1382 end_sequence ();
1386 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1388 /* Make reloads for subreg in operand NOP with internal subreg mode
1389 REG_MODE, add new reloads for further processing. Return true if
1390 any change was done. */
1391 static bool
1392 simplify_operand_subreg (int nop, machine_mode reg_mode)
1394 int hard_regno;
1395 rtx_insn *before, *after;
1396 machine_mode mode, innermode;
1397 rtx reg, new_reg;
1398 rtx operand = *curr_id->operand_loc[nop];
1399 enum reg_class regclass;
1400 enum op_type type;
1402 before = after = NULL;
1404 if (GET_CODE (operand) != SUBREG)
1405 return false;
1407 mode = GET_MODE (operand);
1408 reg = SUBREG_REG (operand);
1409 innermode = GET_MODE (reg);
1410 type = curr_static_id->operand[nop].type;
1411 /* If we change address for paradoxical subreg of memory, the
1412 address might violate the necessary alignment or the access might
1413 be slow. So take this into consideration. We should not worry
1414 about access beyond allocated memory for paradoxical memory
1415 subregs as we don't substitute such equiv memory (see processing
1416 equivalences in function lra_constraints) and because for spilled
1417 pseudos we allocate stack memory enough for the biggest
1418 corresponding paradoxical subreg. */
1419 if (MEM_P (reg)
1420 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1421 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1423 rtx subst, old = *curr_id->operand_loc[nop];
1425 alter_subreg (curr_id->operand_loc[nop], false);
1426 subst = *curr_id->operand_loc[nop];
1427 lra_assert (MEM_P (subst));
1428 if (! valid_address_p (innermode, XEXP (reg, 0),
1429 MEM_ADDR_SPACE (reg))
1430 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1431 MEM_ADDR_SPACE (subst)))
1432 return true;
1433 /* If the address was valid and became invalid, prefer to reload
1434 the memory. Typical case is when the index scale should
1435 correspond the memory. */
1436 *curr_id->operand_loc[nop] = old;
1438 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1440 alter_subreg (curr_id->operand_loc[nop], false);
1441 return true;
1443 else if (CONSTANT_P (reg))
1445 /* Try to simplify subreg of constant. It is usually result of
1446 equivalence substitution. */
1447 if (innermode == VOIDmode
1448 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1449 innermode = curr_static_id->operand[nop].mode;
1450 if ((new_reg = simplify_subreg (mode, reg, innermode,
1451 SUBREG_BYTE (operand))) != NULL_RTX)
1453 *curr_id->operand_loc[nop] = new_reg;
1454 return true;
1457 /* Put constant into memory when we have mixed modes. It generates
1458 a better code in most cases as it does not need a secondary
1459 reload memory. It also prevents LRA looping when LRA is using
1460 secondary reload memory again and again. */
1461 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1462 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1464 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1465 alter_subreg (curr_id->operand_loc[nop], false);
1466 return true;
1468 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1469 if there may be a problem accessing OPERAND in the outer
1470 mode. */
1471 if ((REG_P (reg)
1472 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1473 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1474 /* Don't reload paradoxical subregs because we could be looping
1475 having repeatedly final regno out of hard regs range. */
1476 && (hard_regno_nregs[hard_regno][innermode]
1477 >= hard_regno_nregs[hard_regno][mode])
1478 && simplify_subreg_regno (hard_regno, innermode,
1479 SUBREG_BYTE (operand), mode) < 0
1480 /* Don't reload subreg for matching reload. It is actually
1481 valid subreg in LRA. */
1482 && ! LRA_SUBREG_P (operand))
1483 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1485 enum reg_class rclass;
1487 if (REG_P (reg))
1488 /* There is a big probability that we will get the same class
1489 for the new pseudo and we will get the same insn which
1490 means infinite looping. So spill the new pseudo. */
1491 rclass = NO_REGS;
1492 else
1493 /* The class will be defined later in curr_insn_transform. */
1494 rclass
1495 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1497 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1498 rclass, TRUE, "subreg reg", &new_reg))
1500 bool insert_before, insert_after;
1501 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1503 insert_before = (type != OP_OUT
1504 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1505 insert_after = (type != OP_IN);
1506 insert_move_for_subreg (insert_before ? &before : NULL,
1507 insert_after ? &after : NULL,
1508 reg, new_reg);
1510 SUBREG_REG (operand) = new_reg;
1511 lra_process_new_insns (curr_insn, before, after,
1512 "Inserting subreg reload");
1513 return true;
1515 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1516 IRA allocates hardreg to the inner pseudo reg according to its mode
1517 instead of the outermode, so the size of the hardreg may not be enough
1518 to contain the outermode operand, in that case we may need to insert
1519 reload for the reg. For the following two types of paradoxical subreg,
1520 we need to insert reload:
1521 1. If the op_type is OP_IN, and the hardreg could not be paired with
1522 other hardreg to contain the outermode operand
1523 (checked by in_hard_reg_set_p), we need to insert the reload.
1524 2. If the op_type is OP_OUT or OP_INOUT.
1526 Here is a paradoxical subreg example showing how the reload is generated:
1528 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1529 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1531 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1532 here, if reg107 is assigned to hardreg R15, because R15 is the last
1533 hardreg, compiler cannot find another hardreg to pair with R15 to
1534 contain TImode data. So we insert a TImode reload reg180 for it.
1535 After reload is inserted:
1537 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1538 (reg:DI 107 [ __comp ])) -1
1539 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1540 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1542 Two reload hard registers will be allocated to reg180 to save TImode data
1543 in LRA_assign. */
1544 else if (REG_P (reg)
1545 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1546 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1547 && (hard_regno_nregs[hard_regno][innermode]
1548 < hard_regno_nregs[hard_regno][mode])
1549 && (regclass = lra_get_allocno_class (REGNO (reg)))
1550 && (type != OP_IN
1551 || !in_hard_reg_set_p (reg_class_contents[regclass],
1552 mode, hard_regno)))
1554 /* The class will be defined later in curr_insn_transform. */
1555 enum reg_class rclass
1556 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1558 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1559 rclass, TRUE, "paradoxical subreg", &new_reg))
1561 rtx subreg;
1562 bool insert_before, insert_after;
1564 PUT_MODE (new_reg, mode);
1565 subreg = simplify_gen_subreg (innermode, new_reg, mode, 0);
1566 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1568 insert_before = (type != OP_OUT);
1569 insert_after = (type != OP_IN);
1570 insert_move_for_subreg (insert_before ? &before : NULL,
1571 insert_after ? &after : NULL,
1572 reg, subreg);
1574 SUBREG_REG (operand) = new_reg;
1575 lra_process_new_insns (curr_insn, before, after,
1576 "Inserting paradoxical subreg reload");
1577 return true;
1579 return false;
1582 /* Return TRUE if X refers for a hard register from SET. */
1583 static bool
1584 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1586 int i, j, x_hard_regno;
1587 machine_mode mode;
1588 const char *fmt;
1589 enum rtx_code code;
1591 if (x == NULL_RTX)
1592 return false;
1593 code = GET_CODE (x);
1594 mode = GET_MODE (x);
1595 if (code == SUBREG)
1597 x = SUBREG_REG (x);
1598 code = GET_CODE (x);
1599 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1600 mode = GET_MODE (x);
1603 if (REG_P (x))
1605 x_hard_regno = get_hard_regno (x);
1606 return (x_hard_regno >= 0
1607 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1609 if (MEM_P (x))
1611 struct address_info ad;
1613 decompose_mem_address (&ad, x);
1614 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1615 return true;
1616 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1617 return true;
1619 fmt = GET_RTX_FORMAT (code);
1620 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1622 if (fmt[i] == 'e')
1624 if (uses_hard_regs_p (XEXP (x, i), set))
1625 return true;
1627 else if (fmt[i] == 'E')
1629 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1630 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1631 return true;
1634 return false;
1637 /* Return true if OP is a spilled pseudo. */
1638 static inline bool
1639 spilled_pseudo_p (rtx op)
1641 return (REG_P (op)
1642 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1645 /* Return true if X is a general constant. */
1646 static inline bool
1647 general_constant_p (rtx x)
1649 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1652 static bool
1653 reg_in_class_p (rtx reg, enum reg_class cl)
1655 if (cl == NO_REGS)
1656 return get_reg_class (REGNO (reg)) == NO_REGS;
1657 return in_class_p (reg, cl, NULL);
1660 /* Return true if SET of RCLASS contains no hard regs which can be
1661 used in MODE. */
1662 static bool
1663 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1664 HARD_REG_SET &set,
1665 enum machine_mode mode)
1667 HARD_REG_SET temp;
1669 lra_assert (hard_reg_set_subset_p (set, reg_class_contents[rclass]));
1670 COPY_HARD_REG_SET (temp, set);
1671 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1672 return (hard_reg_set_subset_p
1673 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1676 /* Major function to choose the current insn alternative and what
1677 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1678 negative we should consider only this alternative. Return false if
1679 we can not choose the alternative or find how to reload the
1680 operands. */
1681 static bool
1682 process_alt_operands (int only_alternative)
1684 bool ok_p = false;
1685 int nop, overall, nalt;
1686 int n_alternatives = curr_static_id->n_alternatives;
1687 int n_operands = curr_static_id->n_operands;
1688 /* LOSERS counts the operands that don't fit this alternative and
1689 would require loading. */
1690 int losers;
1691 /* REJECT is a count of how undesirable this alternative says it is
1692 if any reloading is required. If the alternative matches exactly
1693 then REJECT is ignored, but otherwise it gets this much counted
1694 against it in addition to the reloading needed. */
1695 int reject;
1696 int op_reject;
1697 /* The number of elements in the following array. */
1698 int early_clobbered_regs_num;
1699 /* Numbers of operands which are early clobber registers. */
1700 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1701 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1702 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1703 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1704 bool curr_alt_win[MAX_RECOG_OPERANDS];
1705 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1706 int curr_alt_matches[MAX_RECOG_OPERANDS];
1707 /* The number of elements in the following array. */
1708 int curr_alt_dont_inherit_ops_num;
1709 /* Numbers of operands whose reload pseudos should not be inherited. */
1710 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1711 rtx op;
1712 /* The register when the operand is a subreg of register, otherwise the
1713 operand itself. */
1714 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1715 /* The register if the operand is a register or subreg of register,
1716 otherwise NULL. */
1717 rtx operand_reg[MAX_RECOG_OPERANDS];
1718 int hard_regno[MAX_RECOG_OPERANDS];
1719 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1720 int reload_nregs, reload_sum;
1721 bool costly_p;
1722 enum reg_class cl;
1724 /* Calculate some data common for all alternatives to speed up the
1725 function. */
1726 for (nop = 0; nop < n_operands; nop++)
1728 rtx reg;
1730 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1731 /* The real hard regno of the operand after the allocation. */
1732 hard_regno[nop] = get_hard_regno (op);
1734 operand_reg[nop] = reg = op;
1735 biggest_mode[nop] = GET_MODE (op);
1736 if (GET_CODE (op) == SUBREG)
1738 operand_reg[nop] = reg = SUBREG_REG (op);
1739 if (GET_MODE_SIZE (biggest_mode[nop])
1740 < GET_MODE_SIZE (GET_MODE (reg)))
1741 biggest_mode[nop] = GET_MODE (reg);
1743 if (! REG_P (reg))
1744 operand_reg[nop] = NULL_RTX;
1745 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1746 || ((int) REGNO (reg)
1747 == lra_get_elimination_hard_regno (REGNO (reg))))
1748 no_subreg_reg_operand[nop] = reg;
1749 else
1750 operand_reg[nop] = no_subreg_reg_operand[nop]
1751 /* Just use natural mode for elimination result. It should
1752 be enough for extra constraints hooks. */
1753 = regno_reg_rtx[hard_regno[nop]];
1756 /* The constraints are made of several alternatives. Each operand's
1757 constraint looks like foo,bar,... with commas separating the
1758 alternatives. The first alternatives for all operands go
1759 together, the second alternatives go together, etc.
1761 First loop over alternatives. */
1762 alternative_mask preferred = curr_id->preferred_alternatives;
1763 if (only_alternative >= 0)
1764 preferred &= ALTERNATIVE_BIT (only_alternative);
1766 for (nalt = 0; nalt < n_alternatives; nalt++)
1768 /* Loop over operands for one constraint alternative. */
1769 if (!TEST_BIT (preferred, nalt))
1770 continue;
1772 overall = losers = reject = reload_nregs = reload_sum = 0;
1773 for (nop = 0; nop < n_operands; nop++)
1775 int inc = (curr_static_id
1776 ->operand_alternative[nalt * n_operands + nop].reject);
1777 if (lra_dump_file != NULL && inc != 0)
1778 fprintf (lra_dump_file,
1779 " Staticly defined alt reject+=%d\n", inc);
1780 reject += inc;
1782 early_clobbered_regs_num = 0;
1784 for (nop = 0; nop < n_operands; nop++)
1786 const char *p;
1787 char *end;
1788 int len, c, m, i, opalt_num, this_alternative_matches;
1789 bool win, did_match, offmemok, early_clobber_p;
1790 /* false => this operand can be reloaded somehow for this
1791 alternative. */
1792 bool badop;
1793 /* true => this operand can be reloaded if the alternative
1794 allows regs. */
1795 bool winreg;
1796 /* True if a constant forced into memory would be OK for
1797 this operand. */
1798 bool constmemok;
1799 enum reg_class this_alternative, this_costly_alternative;
1800 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1801 bool this_alternative_match_win, this_alternative_win;
1802 bool this_alternative_offmemok;
1803 bool scratch_p;
1804 machine_mode mode;
1805 enum constraint_num cn;
1807 opalt_num = nalt * n_operands + nop;
1808 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1810 /* Fast track for no constraints at all. */
1811 curr_alt[nop] = NO_REGS;
1812 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1813 curr_alt_win[nop] = true;
1814 curr_alt_match_win[nop] = false;
1815 curr_alt_offmemok[nop] = false;
1816 curr_alt_matches[nop] = -1;
1817 continue;
1820 op = no_subreg_reg_operand[nop];
1821 mode = curr_operand_mode[nop];
1823 win = did_match = winreg = offmemok = constmemok = false;
1824 badop = true;
1826 early_clobber_p = false;
1827 p = curr_static_id->operand_alternative[opalt_num].constraint;
1829 this_costly_alternative = this_alternative = NO_REGS;
1830 /* We update set of possible hard regs besides its class
1831 because reg class might be inaccurate. For example,
1832 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1833 is translated in HI_REGS because classes are merged by
1834 pairs and there is no accurate intermediate class. */
1835 CLEAR_HARD_REG_SET (this_alternative_set);
1836 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1837 this_alternative_win = false;
1838 this_alternative_match_win = false;
1839 this_alternative_offmemok = false;
1840 this_alternative_matches = -1;
1842 /* An empty constraint should be excluded by the fast
1843 track. */
1844 lra_assert (*p != 0 && *p != ',');
1846 op_reject = 0;
1847 /* Scan this alternative's specs for this operand; set WIN
1848 if the operand fits any letter in this alternative.
1849 Otherwise, clear BADOP if this operand could fit some
1850 letter after reloads, or set WINREG if this operand could
1851 fit after reloads provided the constraint allows some
1852 registers. */
1853 costly_p = false;
1856 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1858 case '\0':
1859 len = 0;
1860 break;
1861 case ',':
1862 c = '\0';
1863 break;
1865 case '&':
1866 early_clobber_p = true;
1867 break;
1869 case '$':
1870 op_reject += LRA_MAX_REJECT;
1871 break;
1872 case '^':
1873 op_reject += LRA_LOSER_COST_FACTOR;
1874 break;
1876 case '#':
1877 /* Ignore rest of this alternative. */
1878 c = '\0';
1879 break;
1881 case '0': case '1': case '2': case '3': case '4':
1882 case '5': case '6': case '7': case '8': case '9':
1884 int m_hregno;
1885 bool match_p;
1887 m = strtoul (p, &end, 10);
1888 p = end;
1889 len = 0;
1890 lra_assert (nop > m);
1892 this_alternative_matches = m;
1893 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1894 /* We are supposed to match a previous operand.
1895 If we do, we win if that one did. If we do
1896 not, count both of the operands as losers.
1897 (This is too conservative, since most of the
1898 time only a single reload insn will be needed
1899 to make the two operands win. As a result,
1900 this alternative may be rejected when it is
1901 actually desirable.) */
1902 match_p = false;
1903 if (operands_match_p (*curr_id->operand_loc[nop],
1904 *curr_id->operand_loc[m], m_hregno))
1906 /* We should reject matching of an early
1907 clobber operand if the matching operand is
1908 not dying in the insn. */
1909 if (! curr_static_id->operand[m].early_clobber
1910 || operand_reg[nop] == NULL_RTX
1911 || (find_regno_note (curr_insn, REG_DEAD,
1912 REGNO (op))
1913 || REGNO (op) == REGNO (operand_reg[m])))
1914 match_p = true;
1916 if (match_p)
1918 /* If we are matching a non-offsettable
1919 address where an offsettable address was
1920 expected, then we must reject this
1921 combination, because we can't reload
1922 it. */
1923 if (curr_alt_offmemok[m]
1924 && MEM_P (*curr_id->operand_loc[m])
1925 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1926 continue;
1928 else
1930 /* Operands don't match. Both operands must
1931 allow a reload register, otherwise we
1932 cannot make them match. */
1933 if (curr_alt[m] == NO_REGS)
1934 break;
1935 /* Retroactively mark the operand we had to
1936 match as a loser, if it wasn't already and
1937 it wasn't matched to a register constraint
1938 (e.g it might be matched by memory). */
1939 if (curr_alt_win[m]
1940 && (operand_reg[m] == NULL_RTX
1941 || hard_regno[m] < 0))
1943 losers++;
1944 reload_nregs
1945 += (ira_reg_class_max_nregs[curr_alt[m]]
1946 [GET_MODE (*curr_id->operand_loc[m])]);
1949 /* Prefer matching earlyclobber alternative as
1950 it results in less hard regs required for
1951 the insn than a non-matching earlyclobber
1952 alternative. */
1953 if (curr_static_id->operand[m].early_clobber)
1955 if (lra_dump_file != NULL)
1956 fprintf
1957 (lra_dump_file,
1958 " %d Matching earlyclobber alt:"
1959 " reject--\n",
1960 nop);
1961 reject--;
1963 /* Otherwise we prefer no matching
1964 alternatives because it gives more freedom
1965 in RA. */
1966 else if (operand_reg[nop] == NULL_RTX
1967 || (find_regno_note (curr_insn, REG_DEAD,
1968 REGNO (operand_reg[nop]))
1969 == NULL_RTX))
1971 if (lra_dump_file != NULL)
1972 fprintf
1973 (lra_dump_file,
1974 " %d Matching alt: reject+=2\n",
1975 nop);
1976 reject += 2;
1979 /* If we have to reload this operand and some
1980 previous operand also had to match the same
1981 thing as this operand, we don't know how to do
1982 that. */
1983 if (!match_p || !curr_alt_win[m])
1985 for (i = 0; i < nop; i++)
1986 if (curr_alt_matches[i] == m)
1987 break;
1988 if (i < nop)
1989 break;
1991 else
1992 did_match = true;
1994 /* This can be fixed with reloads if the operand
1995 we are supposed to match can be fixed with
1996 reloads. */
1997 badop = false;
1998 this_alternative = curr_alt[m];
1999 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
2000 winreg = this_alternative != NO_REGS;
2001 break;
2004 case 'g':
2005 if (MEM_P (op)
2006 || general_constant_p (op)
2007 || spilled_pseudo_p (op))
2008 win = true;
2009 cl = GENERAL_REGS;
2010 goto reg;
2012 default:
2013 cn = lookup_constraint (p);
2014 switch (get_constraint_type (cn))
2016 case CT_REGISTER:
2017 cl = reg_class_for_constraint (cn);
2018 if (cl != NO_REGS)
2019 goto reg;
2020 break;
2022 case CT_CONST_INT:
2023 if (CONST_INT_P (op)
2024 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2025 win = true;
2026 break;
2028 case CT_MEMORY:
2029 if (MEM_P (op)
2030 && satisfies_memory_constraint_p (op, cn))
2031 win = true;
2032 else if (spilled_pseudo_p (op))
2033 win = true;
2035 /* If we didn't already win, we can reload constants
2036 via force_const_mem or put the pseudo value into
2037 memory, or make other memory by reloading the
2038 address like for 'o'. */
2039 if (CONST_POOL_OK_P (mode, op)
2040 || MEM_P (op) || REG_P (op))
2041 badop = false;
2042 constmemok = true;
2043 offmemok = true;
2044 break;
2046 case CT_ADDRESS:
2047 /* If we didn't already win, we can reload the address
2048 into a base register. */
2049 if (satisfies_address_constraint_p (op, cn))
2050 win = true;
2051 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2052 ADDRESS, SCRATCH);
2053 badop = false;
2054 goto reg;
2056 case CT_FIXED_FORM:
2057 if (constraint_satisfied_p (op, cn))
2058 win = true;
2059 break;
2061 break;
2063 reg:
2064 this_alternative = reg_class_subunion[this_alternative][cl];
2065 IOR_HARD_REG_SET (this_alternative_set,
2066 reg_class_contents[cl]);
2067 if (costly_p)
2069 this_costly_alternative
2070 = reg_class_subunion[this_costly_alternative][cl];
2071 IOR_HARD_REG_SET (this_costly_alternative_set,
2072 reg_class_contents[cl]);
2074 if (mode == BLKmode)
2075 break;
2076 winreg = true;
2077 if (REG_P (op))
2079 if (hard_regno[nop] >= 0
2080 && in_hard_reg_set_p (this_alternative_set,
2081 mode, hard_regno[nop]))
2082 win = true;
2083 else if (hard_regno[nop] < 0
2084 && in_class_p (op, this_alternative, NULL))
2085 win = true;
2087 break;
2089 if (c != ' ' && c != '\t')
2090 costly_p = c == '*';
2092 while ((p += len), c);
2094 scratch_p = (operand_reg[nop] != NULL_RTX
2095 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2096 /* Record which operands fit this alternative. */
2097 if (win)
2099 this_alternative_win = true;
2100 if (operand_reg[nop] != NULL_RTX)
2102 if (hard_regno[nop] >= 0)
2104 if (in_hard_reg_set_p (this_costly_alternative_set,
2105 mode, hard_regno[nop]))
2107 if (lra_dump_file != NULL)
2108 fprintf (lra_dump_file,
2109 " %d Costly set: reject++\n",
2110 nop);
2111 reject++;
2114 else
2116 /* Prefer won reg to spilled pseudo under other
2117 equal conditions for possibe inheritance. */
2118 if (! scratch_p)
2120 if (lra_dump_file != NULL)
2121 fprintf
2122 (lra_dump_file,
2123 " %d Non pseudo reload: reject++\n",
2124 nop);
2125 reject++;
2127 if (in_class_p (operand_reg[nop],
2128 this_costly_alternative, NULL))
2130 if (lra_dump_file != NULL)
2131 fprintf
2132 (lra_dump_file,
2133 " %d Non pseudo costly reload:"
2134 " reject++\n",
2135 nop);
2136 reject++;
2139 /* We simulate the behaviour of old reload here.
2140 Although scratches need hard registers and it
2141 might result in spilling other pseudos, no reload
2142 insns are generated for the scratches. So it
2143 might cost something but probably less than old
2144 reload pass believes. */
2145 if (scratch_p)
2147 if (lra_dump_file != NULL)
2148 fprintf (lra_dump_file,
2149 " %d Scratch win: reject+=2\n",
2150 nop);
2151 reject += 2;
2155 else if (did_match)
2156 this_alternative_match_win = true;
2157 else
2159 int const_to_mem = 0;
2160 bool no_regs_p;
2162 reject += op_reject;
2163 /* Never do output reload of stack pointer. It makes
2164 impossible to do elimination when SP is changed in
2165 RTL. */
2166 if (op == stack_pointer_rtx && ! frame_pointer_needed
2167 && curr_static_id->operand[nop].type != OP_IN)
2168 goto fail;
2170 /* If this alternative asks for a specific reg class, see if there
2171 is at least one allocatable register in that class. */
2172 no_regs_p
2173 = (this_alternative == NO_REGS
2174 || (hard_reg_set_subset_p
2175 (reg_class_contents[this_alternative],
2176 lra_no_alloc_regs)));
2178 /* For asms, verify that the class for this alternative is possible
2179 for the mode that is specified. */
2180 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2182 int i;
2183 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2184 if (HARD_REGNO_MODE_OK (i, mode)
2185 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2186 mode, i))
2187 break;
2188 if (i == FIRST_PSEUDO_REGISTER)
2189 winreg = false;
2192 /* If this operand accepts a register, and if the
2193 register class has at least one allocatable register,
2194 then this operand can be reloaded. */
2195 if (winreg && !no_regs_p)
2196 badop = false;
2198 if (badop)
2200 if (lra_dump_file != NULL)
2201 fprintf (lra_dump_file,
2202 " alt=%d: Bad operand -- refuse\n",
2203 nalt);
2204 goto fail;
2207 /* If not assigned pseudo has a class which a subset of
2208 required reg class, it is a less costly alternative
2209 as the pseudo still can get a hard reg of necessary
2210 class. */
2211 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2212 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2213 && ira_class_subset_p[this_alternative][cl])
2215 if (lra_dump_file != NULL)
2216 fprintf
2217 (lra_dump_file,
2218 " %d Super set class reg: reject-=3\n", nop);
2219 reject -= 3;
2222 this_alternative_offmemok = offmemok;
2223 if (this_costly_alternative != NO_REGS)
2225 if (lra_dump_file != NULL)
2226 fprintf (lra_dump_file,
2227 " %d Costly loser: reject++\n", nop);
2228 reject++;
2230 /* If the operand is dying, has a matching constraint,
2231 and satisfies constraints of the matched operand
2232 which failed to satisfy the own constraints, most probably
2233 the reload for this operand will be gone. */
2234 if (this_alternative_matches >= 0
2235 && !curr_alt_win[this_alternative_matches]
2236 && REG_P (op)
2237 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2238 && (hard_regno[nop] >= 0
2239 ? in_hard_reg_set_p (this_alternative_set,
2240 mode, hard_regno[nop])
2241 : in_class_p (op, this_alternative, NULL)))
2243 if (lra_dump_file != NULL)
2244 fprintf
2245 (lra_dump_file,
2246 " %d Dying matched operand reload: reject++\n",
2247 nop);
2248 reject++;
2250 else
2252 /* Strict_low_part requires to reload the register
2253 not the sub-register. In this case we should
2254 check that a final reload hard reg can hold the
2255 value mode. */
2256 if (curr_static_id->operand[nop].strict_low
2257 && REG_P (op)
2258 && hard_regno[nop] < 0
2259 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2260 && ira_class_hard_regs_num[this_alternative] > 0
2261 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2262 [this_alternative][0],
2263 GET_MODE
2264 (*curr_id->operand_loc[nop])))
2266 if (lra_dump_file != NULL)
2267 fprintf
2268 (lra_dump_file,
2269 " alt=%d: Strict low subreg reload -- refuse\n",
2270 nalt);
2271 goto fail;
2273 losers++;
2275 if (operand_reg[nop] != NULL_RTX
2276 /* Output operands and matched input operands are
2277 not inherited. The following conditions do not
2278 exactly describe the previous statement but they
2279 are pretty close. */
2280 && curr_static_id->operand[nop].type != OP_OUT
2281 && (this_alternative_matches < 0
2282 || curr_static_id->operand[nop].type != OP_IN))
2284 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2285 (operand_reg[nop])]
2286 .last_reload);
2288 /* The value of reload_sum has sense only if we
2289 process insns in their order. It happens only on
2290 the first constraints sub-pass when we do most of
2291 reload work. */
2292 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2293 reload_sum += last_reload - bb_reload_num;
2295 /* If this is a constant that is reloaded into the
2296 desired class by copying it to memory first, count
2297 that as another reload. This is consistent with
2298 other code and is required to avoid choosing another
2299 alternative when the constant is moved into memory.
2300 Note that the test here is precisely the same as in
2301 the code below that calls force_const_mem. */
2302 if (CONST_POOL_OK_P (mode, op)
2303 && ((targetm.preferred_reload_class
2304 (op, this_alternative) == NO_REGS)
2305 || no_input_reloads_p))
2307 const_to_mem = 1;
2308 if (! no_regs_p)
2309 losers++;
2312 /* Alternative loses if it requires a type of reload not
2313 permitted for this insn. We can always reload
2314 objects with a REG_UNUSED note. */
2315 if ((curr_static_id->operand[nop].type != OP_IN
2316 && no_output_reloads_p
2317 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2318 || (curr_static_id->operand[nop].type != OP_OUT
2319 && no_input_reloads_p && ! const_to_mem)
2320 || (this_alternative_matches >= 0
2321 && (no_input_reloads_p
2322 || (no_output_reloads_p
2323 && (curr_static_id->operand
2324 [this_alternative_matches].type != OP_IN)
2325 && ! find_reg_note (curr_insn, REG_UNUSED,
2326 no_subreg_reg_operand
2327 [this_alternative_matches])))))
2329 if (lra_dump_file != NULL)
2330 fprintf
2331 (lra_dump_file,
2332 " alt=%d: No input/otput reload -- refuse\n",
2333 nalt);
2334 goto fail;
2337 /* Alternative loses if it required class pseudo can not
2338 hold value of required mode. Such insns can be
2339 described by insn definitions with mode iterators. */
2340 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2341 && ! hard_reg_set_empty_p (this_alternative_set)
2342 /* It is common practice for constraints to use a
2343 class which does not have actually enough regs to
2344 hold the value (e.g. x86 AREG for mode requiring
2345 more one general reg). Therefore we have 2
2346 conditions to check that the reload pseudo can
2347 not hold the mode value. */
2348 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2349 [this_alternative][0],
2350 GET_MODE (*curr_id->operand_loc[nop]))
2351 /* The above condition is not enough as the first
2352 reg in ira_class_hard_regs can be not aligned for
2353 multi-words mode values. */
2354 && (prohibited_class_reg_set_mode_p
2355 (this_alternative, this_alternative_set,
2356 GET_MODE (*curr_id->operand_loc[nop]))))
2358 if (lra_dump_file != NULL)
2359 fprintf (lra_dump_file,
2360 " alt=%d: reload pseudo for op %d "
2361 " can not hold the mode value -- refuse\n",
2362 nalt, nop);
2363 goto fail;
2366 /* Check strong discouragement of reload of non-constant
2367 into class THIS_ALTERNATIVE. */
2368 if (! CONSTANT_P (op) && ! no_regs_p
2369 && (targetm.preferred_reload_class
2370 (op, this_alternative) == NO_REGS
2371 || (curr_static_id->operand[nop].type == OP_OUT
2372 && (targetm.preferred_output_reload_class
2373 (op, this_alternative) == NO_REGS))))
2375 if (lra_dump_file != NULL)
2376 fprintf (lra_dump_file,
2377 " %d Non-prefered reload: reject+=%d\n",
2378 nop, LRA_MAX_REJECT);
2379 reject += LRA_MAX_REJECT;
2382 if (! (MEM_P (op) && offmemok)
2383 && ! (const_to_mem && constmemok))
2385 /* We prefer to reload pseudos over reloading other
2386 things, since such reloads may be able to be
2387 eliminated later. So bump REJECT in other cases.
2388 Don't do this in the case where we are forcing a
2389 constant into memory and it will then win since
2390 we don't want to have a different alternative
2391 match then. */
2392 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2394 if (lra_dump_file != NULL)
2395 fprintf
2396 (lra_dump_file,
2397 " %d Non-pseudo reload: reject+=2\n",
2398 nop);
2399 reject += 2;
2402 if (! no_regs_p)
2403 reload_nregs
2404 += ira_reg_class_max_nregs[this_alternative][mode];
2406 if (SMALL_REGISTER_CLASS_P (this_alternative))
2408 if (lra_dump_file != NULL)
2409 fprintf
2410 (lra_dump_file,
2411 " %d Small class reload: reject+=%d\n",
2412 nop, LRA_LOSER_COST_FACTOR / 2);
2413 reject += LRA_LOSER_COST_FACTOR / 2;
2417 /* We are trying to spill pseudo into memory. It is
2418 usually more costly than moving to a hard register
2419 although it might takes the same number of
2420 reloads. */
2421 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2423 if (lra_dump_file != NULL)
2424 fprintf
2425 (lra_dump_file,
2426 " %d Spill pseudo into memory: reject+=3\n",
2427 nop);
2428 reject += 3;
2429 if (VECTOR_MODE_P (mode))
2431 /* Spilling vectors into memory is usually more
2432 costly as they contain big values. */
2433 if (lra_dump_file != NULL)
2434 fprintf
2435 (lra_dump_file,
2436 " %d Spill vector pseudo: reject+=2\n",
2437 nop);
2438 reject += 2;
2442 #ifdef SECONDARY_MEMORY_NEEDED
2443 /* If reload requires moving value through secondary
2444 memory, it will need one more insn at least. */
2445 if (this_alternative != NO_REGS
2446 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2447 && ((curr_static_id->operand[nop].type != OP_OUT
2448 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2449 GET_MODE (op)))
2450 || (curr_static_id->operand[nop].type != OP_IN
2451 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2452 GET_MODE (op)))))
2453 losers++;
2454 #endif
2455 /* Input reloads can be inherited more often than output
2456 reloads can be removed, so penalize output
2457 reloads. */
2458 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2460 if (lra_dump_file != NULL)
2461 fprintf
2462 (lra_dump_file,
2463 " %d Non input pseudo reload: reject++\n",
2464 nop);
2465 reject++;
2469 if (early_clobber_p && ! scratch_p)
2471 if (lra_dump_file != NULL)
2472 fprintf (lra_dump_file,
2473 " %d Early clobber: reject++\n", nop);
2474 reject++;
2476 /* ??? We check early clobbers after processing all operands
2477 (see loop below) and there we update the costs more.
2478 Should we update the cost (may be approximately) here
2479 because of early clobber register reloads or it is a rare
2480 or non-important thing to be worth to do it. */
2481 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2482 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2484 if (lra_dump_file != NULL)
2485 fprintf (lra_dump_file,
2486 " alt=%d,overall=%d,losers=%d -- refuse\n",
2487 nalt, overall, losers);
2488 goto fail;
2491 curr_alt[nop] = this_alternative;
2492 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2493 curr_alt_win[nop] = this_alternative_win;
2494 curr_alt_match_win[nop] = this_alternative_match_win;
2495 curr_alt_offmemok[nop] = this_alternative_offmemok;
2496 curr_alt_matches[nop] = this_alternative_matches;
2498 if (this_alternative_matches >= 0
2499 && !did_match && !this_alternative_win)
2500 curr_alt_win[this_alternative_matches] = false;
2502 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2503 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2505 if (curr_insn_set != NULL_RTX && n_operands == 2
2506 /* Prevent processing non-move insns. */
2507 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2508 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2509 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2510 && REG_P (no_subreg_reg_operand[0])
2511 && REG_P (no_subreg_reg_operand[1])
2512 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2513 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2514 || (! curr_alt_win[0] && curr_alt_win[1]
2515 && REG_P (no_subreg_reg_operand[1])
2516 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2517 || (curr_alt_win[0] && ! curr_alt_win[1]
2518 && REG_P (no_subreg_reg_operand[0])
2519 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2520 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2521 no_subreg_reg_operand[1])
2522 || (targetm.preferred_reload_class
2523 (no_subreg_reg_operand[1],
2524 (enum reg_class) curr_alt[1]) != NO_REGS))
2525 /* If it is a result of recent elimination in move
2526 insn we can transform it into an add still by
2527 using this alternative. */
2528 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2530 /* We have a move insn and a new reload insn will be similar
2531 to the current insn. We should avoid such situation as it
2532 results in LRA cycling. */
2533 overall += LRA_MAX_REJECT;
2535 ok_p = true;
2536 curr_alt_dont_inherit_ops_num = 0;
2537 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2539 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2540 HARD_REG_SET temp_set;
2542 i = early_clobbered_nops[nop];
2543 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2544 || hard_regno[i] < 0)
2545 continue;
2546 lra_assert (operand_reg[i] != NULL_RTX);
2547 clobbered_hard_regno = hard_regno[i];
2548 CLEAR_HARD_REG_SET (temp_set);
2549 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2550 first_conflict_j = last_conflict_j = -1;
2551 for (j = 0; j < n_operands; j++)
2552 if (j == i
2553 /* We don't want process insides of match_operator and
2554 match_parallel because otherwise we would process
2555 their operands once again generating a wrong
2556 code. */
2557 || curr_static_id->operand[j].is_operator)
2558 continue;
2559 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2560 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2561 continue;
2562 /* If we don't reload j-th operand, check conflicts. */
2563 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2564 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2566 if (first_conflict_j < 0)
2567 first_conflict_j = j;
2568 last_conflict_j = j;
2570 if (last_conflict_j < 0)
2571 continue;
2572 /* If earlyclobber operand conflicts with another
2573 non-matching operand which is actually the same register
2574 as the earlyclobber operand, it is better to reload the
2575 another operand as an operand matching the earlyclobber
2576 operand can be also the same. */
2577 if (first_conflict_j == last_conflict_j
2578 && operand_reg[last_conflict_j]
2579 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2580 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2582 curr_alt_win[last_conflict_j] = false;
2583 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2584 = last_conflict_j;
2585 losers++;
2586 /* Early clobber was already reflected in REJECT. */
2587 lra_assert (reject > 0);
2588 if (lra_dump_file != NULL)
2589 fprintf
2590 (lra_dump_file,
2591 " %d Conflict early clobber reload: reject--\n",
2593 reject--;
2594 overall += LRA_LOSER_COST_FACTOR - 1;
2596 else
2598 /* We need to reload early clobbered register and the
2599 matched registers. */
2600 for (j = 0; j < n_operands; j++)
2601 if (curr_alt_matches[j] == i)
2603 curr_alt_match_win[j] = false;
2604 losers++;
2605 overall += LRA_LOSER_COST_FACTOR;
2607 if (! curr_alt_match_win[i])
2608 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2609 else
2611 /* Remember pseudos used for match reloads are never
2612 inherited. */
2613 lra_assert (curr_alt_matches[i] >= 0);
2614 curr_alt_win[curr_alt_matches[i]] = false;
2616 curr_alt_win[i] = curr_alt_match_win[i] = false;
2617 losers++;
2618 /* Early clobber was already reflected in REJECT. */
2619 lra_assert (reject > 0);
2620 if (lra_dump_file != NULL)
2621 fprintf
2622 (lra_dump_file,
2623 " %d Matched conflict early clobber reloads:"
2624 "reject--\n",
2626 reject--;
2627 overall += LRA_LOSER_COST_FACTOR - 1;
2630 if (lra_dump_file != NULL)
2631 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2632 nalt, overall, losers, reload_nregs);
2634 /* If this alternative can be made to work by reloading, and it
2635 needs less reloading than the others checked so far, record
2636 it as the chosen goal for reloading. */
2637 if ((best_losers != 0 && losers == 0)
2638 || (((best_losers == 0 && losers == 0)
2639 || (best_losers != 0 && losers != 0))
2640 && (best_overall > overall
2641 || (best_overall == overall
2642 /* If the cost of the reloads is the same,
2643 prefer alternative which requires minimal
2644 number of reload regs. */
2645 && (reload_nregs < best_reload_nregs
2646 || (reload_nregs == best_reload_nregs
2647 && (best_reload_sum < reload_sum
2648 || (best_reload_sum == reload_sum
2649 && nalt < goal_alt_number))))))))
2651 for (nop = 0; nop < n_operands; nop++)
2653 goal_alt_win[nop] = curr_alt_win[nop];
2654 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2655 goal_alt_matches[nop] = curr_alt_matches[nop];
2656 goal_alt[nop] = curr_alt[nop];
2657 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2659 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2660 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2661 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2662 goal_alt_swapped = curr_swapped;
2663 best_overall = overall;
2664 best_losers = losers;
2665 best_reload_nregs = reload_nregs;
2666 best_reload_sum = reload_sum;
2667 goal_alt_number = nalt;
2669 if (losers == 0)
2670 /* Everything is satisfied. Do not process alternatives
2671 anymore. */
2672 break;
2673 fail:
2676 return ok_p;
2679 /* Make reload base reg from address AD. */
2680 static rtx
2681 base_to_reg (struct address_info *ad)
2683 enum reg_class cl;
2684 int code = -1;
2685 rtx new_inner = NULL_RTX;
2686 rtx new_reg = NULL_RTX;
2687 rtx_insn *insn;
2688 rtx_insn *last_insn = get_last_insn();
2690 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2691 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2692 get_index_code (ad));
2693 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2694 cl, "base");
2695 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2696 ad->disp_term == NULL
2697 ? gen_int_mode (0, ad->mode)
2698 : *ad->disp_term);
2699 if (!valid_address_p (ad->mode, new_inner, ad->as))
2700 return NULL_RTX;
2701 insn = emit_insn (gen_rtx_SET (ad->mode, new_reg, *ad->base_term));
2702 code = recog_memoized (insn);
2703 if (code < 0)
2705 delete_insns_since (last_insn);
2706 return NULL_RTX;
2709 return new_inner;
2712 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2713 static rtx
2714 base_plus_disp_to_reg (struct address_info *ad)
2716 enum reg_class cl;
2717 rtx new_reg;
2719 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2720 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2721 get_index_code (ad));
2722 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2723 cl, "base + disp");
2724 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2725 return new_reg;
2728 /* Make reload of index part of address AD. Return the new
2729 pseudo. */
2730 static rtx
2731 index_part_to_reg (struct address_info *ad)
2733 rtx new_reg;
2735 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2736 INDEX_REG_CLASS, "index term");
2737 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2738 GEN_INT (get_index_scale (ad)), new_reg, 1);
2739 return new_reg;
2742 /* Return true if we can add a displacement to address AD, even if that
2743 makes the address invalid. The fix-up code requires any new address
2744 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2745 static bool
2746 can_add_disp_p (struct address_info *ad)
2748 return (!ad->autoinc_p
2749 && ad->segment == NULL
2750 && ad->base == ad->base_term
2751 && ad->disp == ad->disp_term);
2754 /* Make equiv substitution in address AD. Return true if a substitution
2755 was made. */
2756 static bool
2757 equiv_address_substitution (struct address_info *ad)
2759 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2760 HOST_WIDE_INT disp, scale;
2761 bool change_p;
2763 base_term = strip_subreg (ad->base_term);
2764 if (base_term == NULL)
2765 base_reg = new_base_reg = NULL_RTX;
2766 else
2768 base_reg = *base_term;
2769 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2771 index_term = strip_subreg (ad->index_term);
2772 if (index_term == NULL)
2773 index_reg = new_index_reg = NULL_RTX;
2774 else
2776 index_reg = *index_term;
2777 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2779 if (base_reg == new_base_reg && index_reg == new_index_reg)
2780 return false;
2781 disp = 0;
2782 change_p = false;
2783 if (lra_dump_file != NULL)
2785 fprintf (lra_dump_file, "Changing address in insn %d ",
2786 INSN_UID (curr_insn));
2787 dump_value_slim (lra_dump_file, *ad->outer, 1);
2789 if (base_reg != new_base_reg)
2791 if (REG_P (new_base_reg))
2793 *base_term = new_base_reg;
2794 change_p = true;
2796 else if (GET_CODE (new_base_reg) == PLUS
2797 && REG_P (XEXP (new_base_reg, 0))
2798 && CONST_INT_P (XEXP (new_base_reg, 1))
2799 && can_add_disp_p (ad))
2801 disp += INTVAL (XEXP (new_base_reg, 1));
2802 *base_term = XEXP (new_base_reg, 0);
2803 change_p = true;
2805 if (ad->base_term2 != NULL)
2806 *ad->base_term2 = *ad->base_term;
2808 if (index_reg != new_index_reg)
2810 if (REG_P (new_index_reg))
2812 *index_term = new_index_reg;
2813 change_p = true;
2815 else if (GET_CODE (new_index_reg) == PLUS
2816 && REG_P (XEXP (new_index_reg, 0))
2817 && CONST_INT_P (XEXP (new_index_reg, 1))
2818 && can_add_disp_p (ad)
2819 && (scale = get_index_scale (ad)))
2821 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2822 *index_term = XEXP (new_index_reg, 0);
2823 change_p = true;
2826 if (disp != 0)
2828 if (ad->disp != NULL)
2829 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2830 else
2832 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2833 update_address (ad);
2835 change_p = true;
2837 if (lra_dump_file != NULL)
2839 if (! change_p)
2840 fprintf (lra_dump_file, " -- no change\n");
2841 else
2843 fprintf (lra_dump_file, " on equiv ");
2844 dump_value_slim (lra_dump_file, *ad->outer, 1);
2845 fprintf (lra_dump_file, "\n");
2848 return change_p;
2851 /* Major function to make reloads for an address in operand NOP or
2852 check its correctness (If CHECK_ONLY_P is true). The supported
2853 cases are:
2855 1) an address that existed before LRA started, at which point it
2856 must have been valid. These addresses are subject to elimination
2857 and may have become invalid due to the elimination offset being out
2858 of range.
2860 2) an address created by forcing a constant to memory
2861 (force_const_to_mem). The initial form of these addresses might
2862 not be valid, and it is this function's job to make them valid.
2864 3) a frame address formed from a register and a (possibly zero)
2865 constant offset. As above, these addresses might not be valid and
2866 this function must make them so.
2868 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2869 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2870 address. Return true for any RTL change.
2872 The function is a helper function which does not produce all
2873 transformations (when CHECK_ONLY_P is false) which can be
2874 necessary. It does just basic steps. To do all necessary
2875 transformations use function process_address. */
2876 static bool
2877 process_address_1 (int nop, bool check_only_p,
2878 rtx_insn **before, rtx_insn **after)
2880 struct address_info ad;
2881 rtx new_reg;
2882 rtx op = *curr_id->operand_loc[nop];
2883 const char *constraint = curr_static_id->operand[nop].constraint;
2884 enum constraint_num cn = lookup_constraint (constraint);
2885 bool change_p = false;
2887 if (insn_extra_address_constraint (cn))
2888 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2889 else if (MEM_P (op))
2890 decompose_mem_address (&ad, op);
2891 else if (GET_CODE (op) == SUBREG
2892 && MEM_P (SUBREG_REG (op)))
2893 decompose_mem_address (&ad, SUBREG_REG (op));
2894 else
2895 return false;
2896 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
2897 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
2898 when INDEX_REG_CLASS is a single register class. */
2899 if (ad.base_term != NULL
2900 && ad.index_term != NULL
2901 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
2902 && REG_P (*ad.base_term)
2903 && REG_P (*ad.index_term)
2904 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
2905 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
2907 std::swap (ad.base, ad.index);
2908 std::swap (ad.base_term, ad.index_term);
2910 if (! check_only_p)
2911 change_p = equiv_address_substitution (&ad);
2912 if (ad.base_term != NULL
2913 && (process_addr_reg
2914 (ad.base_term, check_only_p, before,
2915 (ad.autoinc_p
2916 && !(REG_P (*ad.base_term)
2917 && find_regno_note (curr_insn, REG_DEAD,
2918 REGNO (*ad.base_term)) != NULL_RTX)
2919 ? after : NULL),
2920 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2921 get_index_code (&ad)))))
2923 change_p = true;
2924 if (ad.base_term2 != NULL)
2925 *ad.base_term2 = *ad.base_term;
2927 if (ad.index_term != NULL
2928 && process_addr_reg (ad.index_term, check_only_p,
2929 before, NULL, INDEX_REG_CLASS))
2930 change_p = true;
2932 /* Target hooks sometimes don't treat extra-constraint addresses as
2933 legitimate address_operands, so handle them specially. */
2934 if (insn_extra_address_constraint (cn)
2935 && satisfies_address_constraint_p (&ad, cn))
2936 return change_p;
2938 if (check_only_p)
2939 return change_p;
2941 /* There are three cases where the shape of *AD.INNER may now be invalid:
2943 1) the original address was valid, but either elimination or
2944 equiv_address_substitution was applied and that made
2945 the address invalid.
2947 2) the address is an invalid symbolic address created by
2948 force_const_to_mem.
2950 3) the address is a frame address with an invalid offset.
2952 4) the address is a frame address with an invalid base.
2954 All these cases involve a non-autoinc address, so there is no
2955 point revalidating other types. */
2956 if (ad.autoinc_p || valid_address_p (&ad))
2957 return change_p;
2959 /* Any index existed before LRA started, so we can assume that the
2960 presence and shape of the index is valid. */
2961 push_to_sequence (*before);
2962 lra_assert (ad.disp == ad.disp_term);
2963 if (ad.base == NULL)
2965 if (ad.index == NULL)
2967 int code = -1;
2968 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2969 SCRATCH, SCRATCH);
2970 rtx addr = *ad.inner;
2972 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2973 #ifdef HAVE_lo_sum
2975 rtx_insn *insn;
2976 rtx_insn *last = get_last_insn ();
2978 /* addr => lo_sum (new_base, addr), case (2) above. */
2979 insn = emit_insn (gen_rtx_SET
2980 (VOIDmode, new_reg,
2981 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2982 code = recog_memoized (insn);
2983 if (code >= 0)
2985 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2986 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2988 /* Try to put lo_sum into register. */
2989 insn = emit_insn (gen_rtx_SET
2990 (VOIDmode, new_reg,
2991 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2992 code = recog_memoized (insn);
2993 if (code >= 0)
2995 *ad.inner = new_reg;
2996 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2998 *ad.inner = addr;
2999 code = -1;
3005 if (code < 0)
3006 delete_insns_since (last);
3008 #endif
3009 if (code < 0)
3011 /* addr => new_base, case (2) above. */
3012 lra_emit_move (new_reg, addr);
3013 *ad.inner = new_reg;
3016 else
3018 /* index * scale + disp => new base + index * scale,
3019 case (1) above. */
3020 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3021 GET_CODE (*ad.index));
3023 lra_assert (INDEX_REG_CLASS != NO_REGS);
3024 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3025 lra_emit_move (new_reg, *ad.disp);
3026 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3027 new_reg, *ad.index);
3030 else if (ad.index == NULL)
3032 int regno;
3033 enum reg_class cl;
3034 rtx set;
3035 rtx_insn *insns, *last_insn;
3036 /* Try to reload base into register only if the base is invalid
3037 for the address but with valid offset, case (4) above. */
3038 start_sequence ();
3039 new_reg = base_to_reg (&ad);
3041 /* base + disp => new base, cases (1) and (3) above. */
3042 /* Another option would be to reload the displacement into an
3043 index register. However, postreload has code to optimize
3044 address reloads that have the same base and different
3045 displacements, so reloading into an index register would
3046 not necessarily be a win. */
3047 if (new_reg == NULL_RTX)
3048 new_reg = base_plus_disp_to_reg (&ad);
3049 insns = get_insns ();
3050 last_insn = get_last_insn ();
3051 /* If we generated at least two insns, try last insn source as
3052 an address. If we succeed, we generate one less insn. */
3053 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3054 && GET_CODE (SET_SRC (set)) == PLUS
3055 && REG_P (XEXP (SET_SRC (set), 0))
3056 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3058 *ad.inner = SET_SRC (set);
3059 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3061 *ad.base_term = XEXP (SET_SRC (set), 0);
3062 *ad.disp_term = XEXP (SET_SRC (set), 1);
3063 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3064 get_index_code (&ad));
3065 regno = REGNO (*ad.base_term);
3066 if (regno >= FIRST_PSEUDO_REGISTER
3067 && cl != lra_get_allocno_class (regno))
3068 lra_change_class (regno, cl, " Change to", true);
3069 new_reg = SET_SRC (set);
3070 delete_insns_since (PREV_INSN (last_insn));
3073 /* Try if target can split displacement into legitimite new disp
3074 and offset. If it's the case, we replace the last insn with
3075 insns for base + offset => new_reg and set new_reg + new disp
3076 to *ad.inner. */
3077 last_insn = get_last_insn ();
3078 if ((set = single_set (last_insn)) != NULL_RTX
3079 && GET_CODE (SET_SRC (set)) == PLUS
3080 && REG_P (XEXP (SET_SRC (set), 0))
3081 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3082 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3084 rtx addend, disp = XEXP (SET_SRC (set), 1);
3085 if (targetm.legitimize_address_displacement (&disp, &addend,
3086 ad.mode))
3088 rtx_insn *new_insns;
3089 start_sequence ();
3090 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3091 new_insns = get_insns ();
3092 end_sequence ();
3093 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3094 delete_insns_since (PREV_INSN (last_insn));
3095 add_insn (new_insns);
3096 insns = get_insns ();
3099 end_sequence ();
3100 emit_insn (insns);
3101 *ad.inner = new_reg;
3103 else if (ad.disp_term != NULL)
3105 /* base + scale * index + disp => new base + scale * index,
3106 case (1) above. */
3107 new_reg = base_plus_disp_to_reg (&ad);
3108 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3109 new_reg, *ad.index);
3111 else if (get_index_scale (&ad) == 1)
3113 /* The last transformation to one reg will be made in
3114 curr_insn_transform function. */
3115 end_sequence ();
3116 return false;
3118 else
3120 /* base + scale * index => base + new_reg,
3121 case (1) above.
3122 Index part of address may become invalid. For example, we
3123 changed pseudo on the equivalent memory and a subreg of the
3124 pseudo onto the memory of different mode for which the scale is
3125 prohibitted. */
3126 new_reg = index_part_to_reg (&ad);
3127 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3128 *ad.base_term, new_reg);
3130 *before = get_insns ();
3131 end_sequence ();
3132 return true;
3135 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3136 Use process_address_1 as a helper function. Return true for any
3137 RTL changes.
3139 If CHECK_ONLY_P is true, just check address correctness. Return
3140 false if the address correct. */
3141 static bool
3142 process_address (int nop, bool check_only_p,
3143 rtx_insn **before, rtx_insn **after)
3145 bool res = false;
3147 while (process_address_1 (nop, check_only_p, before, after))
3149 if (check_only_p)
3150 return true;
3151 res = true;
3153 return res;
3156 /* Emit insns to reload VALUE into a new register. VALUE is an
3157 auto-increment or auto-decrement RTX whose operand is a register or
3158 memory location; so reloading involves incrementing that location.
3159 IN is either identical to VALUE, or some cheaper place to reload
3160 value being incremented/decremented from.
3162 INC_AMOUNT is the number to increment or decrement by (always
3163 positive and ignored for POST_MODIFY/PRE_MODIFY).
3165 Return pseudo containing the result. */
3166 static rtx
3167 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3169 /* REG or MEM to be copied and incremented. */
3170 rtx incloc = XEXP (value, 0);
3171 /* Nonzero if increment after copying. */
3172 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3173 || GET_CODE (value) == POST_MODIFY);
3174 rtx_insn *last;
3175 rtx inc;
3176 rtx_insn *add_insn;
3177 int code;
3178 rtx real_in = in == value ? incloc : in;
3179 rtx result;
3180 bool plus_p = true;
3182 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3184 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3185 || GET_CODE (XEXP (value, 1)) == MINUS);
3186 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3187 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3188 inc = XEXP (XEXP (value, 1), 1);
3190 else
3192 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3193 inc_amount = -inc_amount;
3195 inc = GEN_INT (inc_amount);
3198 if (! post && REG_P (incloc))
3199 result = incloc;
3200 else
3201 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3202 "INC/DEC result");
3204 if (real_in != result)
3206 /* First copy the location to the result register. */
3207 lra_assert (REG_P (result));
3208 emit_insn (gen_move_insn (result, real_in));
3211 /* We suppose that there are insns to add/sub with the constant
3212 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3213 old reload worked with this assumption. If the assumption
3214 becomes wrong, we should use approach in function
3215 base_plus_disp_to_reg. */
3216 if (in == value)
3218 /* See if we can directly increment INCLOC. */
3219 last = get_last_insn ();
3220 add_insn = emit_insn (plus_p
3221 ? gen_add2_insn (incloc, inc)
3222 : gen_sub2_insn (incloc, inc));
3224 code = recog_memoized (add_insn);
3225 if (code >= 0)
3227 if (! post && result != incloc)
3228 emit_insn (gen_move_insn (result, incloc));
3229 return result;
3231 delete_insns_since (last);
3234 /* If couldn't do the increment directly, must increment in RESULT.
3235 The way we do this depends on whether this is pre- or
3236 post-increment. For pre-increment, copy INCLOC to the reload
3237 register, increment it there, then save back. */
3238 if (! post)
3240 if (real_in != result)
3241 emit_insn (gen_move_insn (result, real_in));
3242 if (plus_p)
3243 emit_insn (gen_add2_insn (result, inc));
3244 else
3245 emit_insn (gen_sub2_insn (result, inc));
3246 if (result != incloc)
3247 emit_insn (gen_move_insn (incloc, result));
3249 else
3251 /* Post-increment.
3253 Because this might be a jump insn or a compare, and because
3254 RESULT may not be available after the insn in an input
3255 reload, we must do the incrementing before the insn being
3256 reloaded for.
3258 We have already copied IN to RESULT. Increment the copy in
3259 RESULT, save that back, then decrement RESULT so it has
3260 the original value. */
3261 if (plus_p)
3262 emit_insn (gen_add2_insn (result, inc));
3263 else
3264 emit_insn (gen_sub2_insn (result, inc));
3265 emit_insn (gen_move_insn (incloc, result));
3266 /* Restore non-modified value for the result. We prefer this
3267 way because it does not require an additional hard
3268 register. */
3269 if (plus_p)
3271 if (CONST_INT_P (inc))
3272 emit_insn (gen_add2_insn (result,
3273 gen_int_mode (-INTVAL (inc),
3274 GET_MODE (result))));
3275 else
3276 emit_insn (gen_sub2_insn (result, inc));
3278 else
3279 emit_insn (gen_add2_insn (result, inc));
3281 return result;
3284 /* Return true if the current move insn does not need processing as we
3285 already know that it satisfies its constraints. */
3286 static bool
3287 simple_move_p (void)
3289 rtx dest, src;
3290 enum reg_class dclass, sclass;
3292 lra_assert (curr_insn_set != NULL_RTX);
3293 dest = SET_DEST (curr_insn_set);
3294 src = SET_SRC (curr_insn_set);
3295 return ((dclass = get_op_class (dest)) != NO_REGS
3296 && (sclass = get_op_class (src)) != NO_REGS
3297 /* The backend guarantees that register moves of cost 2
3298 never need reloads. */
3299 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3302 /* Swap operands NOP and NOP + 1. */
3303 static inline void
3304 swap_operands (int nop)
3306 machine_mode mode = curr_operand_mode[nop];
3307 curr_operand_mode[nop] = curr_operand_mode[nop + 1];
3308 curr_operand_mode[nop + 1] = mode;
3309 mode = original_subreg_reg_mode[nop];
3310 original_subreg_reg_mode[nop] = original_subreg_reg_mode[nop + 1];
3311 original_subreg_reg_mode[nop + 1] = mode;
3312 rtx x = *curr_id->operand_loc[nop];
3313 *curr_id->operand_loc[nop] = *curr_id->operand_loc[nop + 1];
3314 *curr_id->operand_loc[nop + 1] = x;
3315 /* Swap the duplicates too. */
3316 lra_update_dup (curr_id, nop);
3317 lra_update_dup (curr_id, nop + 1);
3320 /* Main entry point of the constraint code: search the body of the
3321 current insn to choose the best alternative. It is mimicking insn
3322 alternative cost calculation model of former reload pass. That is
3323 because machine descriptions were written to use this model. This
3324 model can be changed in future. Make commutative operand exchange
3325 if it is chosen.
3327 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3328 constraints. Return true if any change happened during function
3329 call.
3331 If CHECK_ONLY_P is true then don't do any transformation. Just
3332 check that the insn satisfies all constraints. If the insn does
3333 not satisfy any constraint, return true. */
3334 static bool
3335 curr_insn_transform (bool check_only_p)
3337 int i, j, k;
3338 int n_operands;
3339 int n_alternatives;
3340 int commutative;
3341 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3342 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3343 rtx_insn *before, *after;
3344 bool alt_p = false;
3345 /* Flag that the insn has been changed through a transformation. */
3346 bool change_p;
3347 bool sec_mem_p;
3348 #ifdef SECONDARY_MEMORY_NEEDED
3349 bool use_sec_mem_p;
3350 #endif
3351 int max_regno_before;
3352 int reused_alternative_num;
3354 curr_insn_set = single_set (curr_insn);
3355 if (curr_insn_set != NULL_RTX && simple_move_p ())
3356 return false;
3358 no_input_reloads_p = no_output_reloads_p = false;
3359 goal_alt_number = -1;
3360 change_p = sec_mem_p = false;
3361 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3362 reloads; neither are insns that SET cc0. Insns that use CC0 are
3363 not allowed to have any input reloads. */
3364 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3365 no_output_reloads_p = true;
3367 #ifdef HAVE_cc0
3368 if (reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3369 no_input_reloads_p = true;
3370 if (reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3371 no_output_reloads_p = true;
3372 #endif
3374 n_operands = curr_static_id->n_operands;
3375 n_alternatives = curr_static_id->n_alternatives;
3377 /* Just return "no reloads" if insn has no operands with
3378 constraints. */
3379 if (n_operands == 0 || n_alternatives == 0)
3380 return false;
3382 max_regno_before = max_reg_num ();
3384 for (i = 0; i < n_operands; i++)
3386 goal_alt_matched[i][0] = -1;
3387 goal_alt_matches[i] = -1;
3390 commutative = curr_static_id->commutative;
3392 /* Now see what we need for pseudos that didn't get hard regs or got
3393 the wrong kind of hard reg. For this, we must consider all the
3394 operands together against the register constraints. */
3396 best_losers = best_overall = INT_MAX;
3397 best_reload_sum = 0;
3399 curr_swapped = false;
3400 goal_alt_swapped = false;
3402 if (! check_only_p)
3403 /* Make equivalence substitution and memory subreg elimination
3404 before address processing because an address legitimacy can
3405 depend on memory mode. */
3406 for (i = 0; i < n_operands; i++)
3408 rtx op = *curr_id->operand_loc[i];
3409 rtx subst, old = op;
3410 bool op_change_p = false;
3412 if (GET_CODE (old) == SUBREG)
3413 old = SUBREG_REG (old);
3414 subst = get_equiv_with_elimination (old, curr_insn);
3415 original_subreg_reg_mode[i] = VOIDmode;
3416 if (subst != old)
3418 subst = copy_rtx (subst);
3419 lra_assert (REG_P (old));
3420 if (GET_CODE (op) != SUBREG)
3421 *curr_id->operand_loc[i] = subst;
3422 else
3424 SUBREG_REG (op) = subst;
3425 if (GET_MODE (subst) == VOIDmode)
3426 original_subreg_reg_mode[i] = GET_MODE (old);
3428 if (lra_dump_file != NULL)
3430 fprintf (lra_dump_file,
3431 "Changing pseudo %d in operand %i of insn %u on equiv ",
3432 REGNO (old), i, INSN_UID (curr_insn));
3433 dump_value_slim (lra_dump_file, subst, 1);
3434 fprintf (lra_dump_file, "\n");
3436 op_change_p = change_p = true;
3438 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3440 change_p = true;
3441 lra_update_dup (curr_id, i);
3445 /* Reload address registers and displacements. We do it before
3446 finding an alternative because of memory constraints. */
3447 before = after = NULL;
3448 for (i = 0; i < n_operands; i++)
3449 if (! curr_static_id->operand[i].is_operator
3450 && process_address (i, check_only_p, &before, &after))
3452 if (check_only_p)
3453 return true;
3454 change_p = true;
3455 lra_update_dup (curr_id, i);
3458 if (change_p)
3459 /* If we've changed the instruction then any alternative that
3460 we chose previously may no longer be valid. */
3461 lra_set_used_insn_alternative (curr_insn, -1);
3463 if (! check_only_p && curr_insn_set != NULL_RTX
3464 && check_and_process_move (&change_p, &sec_mem_p))
3465 return change_p;
3467 try_swapped:
3469 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3470 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3471 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3472 reused_alternative_num, INSN_UID (curr_insn));
3474 if (process_alt_operands (reused_alternative_num))
3475 alt_p = true;
3477 if (check_only_p)
3478 return ! alt_p || best_losers != 0;
3480 /* If insn is commutative (it's safe to exchange a certain pair of
3481 operands) then we need to try each alternative twice, the second
3482 time matching those two operands as if we had exchanged them. To
3483 do this, really exchange them in operands.
3485 If we have just tried the alternatives the second time, return
3486 operands to normal and drop through. */
3488 if (reused_alternative_num < 0 && commutative >= 0)
3490 curr_swapped = !curr_swapped;
3491 if (curr_swapped)
3493 swap_operands (commutative);
3494 goto try_swapped;
3496 else
3497 swap_operands (commutative);
3500 if (! alt_p && ! sec_mem_p)
3502 /* No alternative works with reloads?? */
3503 if (INSN_CODE (curr_insn) >= 0)
3504 fatal_insn ("unable to generate reloads for:", curr_insn);
3505 error_for_asm (curr_insn,
3506 "inconsistent operand constraints in an %<asm%>");
3507 /* Avoid further trouble with this insn. */
3508 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3509 lra_invalidate_insn_data (curr_insn);
3510 return true;
3513 /* If the best alternative is with operands 1 and 2 swapped, swap
3514 them. Update the operand numbers of any reloads already
3515 pushed. */
3517 if (goal_alt_swapped)
3519 if (lra_dump_file != NULL)
3520 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3521 INSN_UID (curr_insn));
3523 /* Swap the duplicates too. */
3524 swap_operands (commutative);
3525 change_p = true;
3528 #ifdef SECONDARY_MEMORY_NEEDED
3529 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3530 too conservatively. So we use the secondary memory only if there
3531 is no any alternative without reloads. */
3532 use_sec_mem_p = false;
3533 if (! alt_p)
3534 use_sec_mem_p = true;
3535 else if (sec_mem_p)
3537 for (i = 0; i < n_operands; i++)
3538 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3539 break;
3540 use_sec_mem_p = i < n_operands;
3543 if (use_sec_mem_p)
3545 rtx new_reg, src, dest, rld;
3546 machine_mode sec_mode, rld_mode;
3548 lra_assert (sec_mem_p);
3549 lra_assert (curr_static_id->operand[0].type == OP_OUT
3550 && curr_static_id->operand[1].type == OP_IN);
3551 dest = *curr_id->operand_loc[0];
3552 src = *curr_id->operand_loc[1];
3553 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3554 ? dest : src);
3555 rld_mode = GET_MODE (rld);
3556 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3557 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3558 #else
3559 sec_mode = rld_mode;
3560 #endif
3561 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3562 NO_REGS, "secondary");
3563 /* If the mode is changed, it should be wider. */
3564 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3565 if (sec_mode != rld_mode)
3567 /* If the target says specifically to use another mode for
3568 secondary memory moves we can not reuse the original
3569 insn. */
3570 after = emit_spill_move (false, new_reg, dest);
3571 lra_process_new_insns (curr_insn, NULL, after,
3572 "Inserting the sec. move");
3573 /* We may have non null BEFORE here (e.g. after address
3574 processing. */
3575 push_to_sequence (before);
3576 before = emit_spill_move (true, new_reg, src);
3577 emit_insn (before);
3578 before = get_insns ();
3579 end_sequence ();
3580 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3581 lra_set_insn_deleted (curr_insn);
3583 else if (dest == rld)
3585 *curr_id->operand_loc[0] = new_reg;
3586 after = emit_spill_move (false, new_reg, dest);
3587 lra_process_new_insns (curr_insn, NULL, after,
3588 "Inserting the sec. move");
3590 else
3592 *curr_id->operand_loc[1] = new_reg;
3593 /* See comments above. */
3594 push_to_sequence (before);
3595 before = emit_spill_move (true, new_reg, src);
3596 emit_insn (before);
3597 before = get_insns ();
3598 end_sequence ();
3599 lra_process_new_insns (curr_insn, before, NULL,
3600 "Inserting the sec. move");
3602 lra_update_insn_regno_info (curr_insn);
3603 return true;
3605 #endif
3607 lra_assert (goal_alt_number >= 0);
3608 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3610 if (lra_dump_file != NULL)
3612 const char *p;
3614 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3615 goal_alt_number, INSN_UID (curr_insn));
3616 for (i = 0; i < n_operands; i++)
3618 p = (curr_static_id->operand_alternative
3619 [goal_alt_number * n_operands + i].constraint);
3620 if (*p == '\0')
3621 continue;
3622 fprintf (lra_dump_file, " (%d) ", i);
3623 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3624 fputc (*p, lra_dump_file);
3626 if (INSN_CODE (curr_insn) >= 0
3627 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3628 fprintf (lra_dump_file, " {%s}", p);
3629 if (curr_id->sp_offset != 0)
3630 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3631 curr_id->sp_offset);
3632 fprintf (lra_dump_file, "\n");
3635 /* Right now, for any pair of operands I and J that are required to
3636 match, with J < I, goal_alt_matches[I] is J. Add I to
3637 goal_alt_matched[J]. */
3639 for (i = 0; i < n_operands; i++)
3640 if ((j = goal_alt_matches[i]) >= 0)
3642 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3644 /* We allow matching one output operand and several input
3645 operands. */
3646 lra_assert (k == 0
3647 || (curr_static_id->operand[j].type == OP_OUT
3648 && curr_static_id->operand[i].type == OP_IN
3649 && (curr_static_id->operand
3650 [goal_alt_matched[j][0]].type == OP_IN)));
3651 goal_alt_matched[j][k] = i;
3652 goal_alt_matched[j][k + 1] = -1;
3655 for (i = 0; i < n_operands; i++)
3656 goal_alt_win[i] |= goal_alt_match_win[i];
3658 /* Any constants that aren't allowed and can't be reloaded into
3659 registers are here changed into memory references. */
3660 for (i = 0; i < n_operands; i++)
3661 if (goal_alt_win[i])
3663 int regno;
3664 enum reg_class new_class;
3665 rtx reg = *curr_id->operand_loc[i];
3667 if (GET_CODE (reg) == SUBREG)
3668 reg = SUBREG_REG (reg);
3670 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3672 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3674 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3676 lra_assert (ok_p);
3677 lra_change_class (regno, new_class, " Change to", true);
3681 else
3683 const char *constraint;
3684 char c;
3685 rtx op = *curr_id->operand_loc[i];
3686 rtx subreg = NULL_RTX;
3687 machine_mode mode = curr_operand_mode[i];
3689 if (GET_CODE (op) == SUBREG)
3691 subreg = op;
3692 op = SUBREG_REG (op);
3693 mode = GET_MODE (op);
3696 if (CONST_POOL_OK_P (mode, op)
3697 && ((targetm.preferred_reload_class
3698 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3699 || no_input_reloads_p))
3701 rtx tem = force_const_mem (mode, op);
3703 change_p = true;
3704 if (subreg != NULL_RTX)
3705 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3707 *curr_id->operand_loc[i] = tem;
3708 lra_update_dup (curr_id, i);
3709 process_address (i, false, &before, &after);
3711 /* If the alternative accepts constant pool refs directly
3712 there will be no reload needed at all. */
3713 if (subreg != NULL_RTX)
3714 continue;
3715 /* Skip alternatives before the one requested. */
3716 constraint = (curr_static_id->operand_alternative
3717 [goal_alt_number * n_operands + i].constraint);
3718 for (;
3719 (c = *constraint) && c != ',' && c != '#';
3720 constraint += CONSTRAINT_LEN (c, constraint))
3722 enum constraint_num cn = lookup_constraint (constraint);
3723 if (insn_extra_memory_constraint (cn)
3724 && satisfies_memory_constraint_p (tem, cn))
3725 break;
3727 if (c == '\0' || c == ',' || c == '#')
3728 continue;
3730 goal_alt_win[i] = true;
3734 for (i = 0; i < n_operands; i++)
3736 int regno;
3737 bool optional_p = false;
3738 rtx old, new_reg;
3739 rtx op = *curr_id->operand_loc[i];
3741 if (goal_alt_win[i])
3743 if (goal_alt[i] == NO_REGS
3744 && REG_P (op)
3745 /* When we assign NO_REGS it means that we will not
3746 assign a hard register to the scratch pseudo by
3747 assigment pass and the scratch pseudo will be
3748 spilled. Spilled scratch pseudos are transformed
3749 back to scratches at the LRA end. */
3750 && lra_former_scratch_operand_p (curr_insn, i))
3752 int regno = REGNO (op);
3753 lra_change_class (regno, NO_REGS, " Change to", true);
3754 if (lra_get_regno_hard_regno (regno) >= 0)
3755 /* We don't have to mark all insn affected by the
3756 spilled pseudo as there is only one such insn, the
3757 current one. */
3758 reg_renumber[regno] = -1;
3760 /* We can do an optional reload. If the pseudo got a hard
3761 reg, we might improve the code through inheritance. If
3762 it does not get a hard register we coalesce memory/memory
3763 moves later. Ignore move insns to avoid cycling. */
3764 if (! lra_simple_p
3765 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3766 && goal_alt[i] != NO_REGS && REG_P (op)
3767 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3768 && regno < new_regno_start
3769 && ! lra_former_scratch_p (regno)
3770 && reg_renumber[regno] < 0
3771 /* Check that the optional reload pseudo will be able to
3772 hold given mode value. */
3773 && ! (prohibited_class_reg_set_mode_p
3774 (goal_alt[i], reg_class_contents[goal_alt[i]],
3775 PSEUDO_REGNO_MODE (regno)))
3776 && (curr_insn_set == NULL_RTX
3777 || !((REG_P (SET_SRC (curr_insn_set))
3778 || MEM_P (SET_SRC (curr_insn_set))
3779 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3780 && (REG_P (SET_DEST (curr_insn_set))
3781 || MEM_P (SET_DEST (curr_insn_set))
3782 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3783 optional_p = true;
3784 else
3785 continue;
3788 /* Operands that match previous ones have already been handled. */
3789 if (goal_alt_matches[i] >= 0)
3790 continue;
3792 /* We should not have an operand with a non-offsettable address
3793 appearing where an offsettable address will do. It also may
3794 be a case when the address should be special in other words
3795 not a general one (e.g. it needs no index reg). */
3796 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3798 enum reg_class rclass;
3799 rtx *loc = &XEXP (op, 0);
3800 enum rtx_code code = GET_CODE (*loc);
3802 push_to_sequence (before);
3803 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3804 MEM, SCRATCH);
3805 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3806 new_reg = emit_inc (rclass, *loc, *loc,
3807 /* This value does not matter for MODIFY. */
3808 GET_MODE_SIZE (GET_MODE (op)));
3809 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3810 "offsetable address", &new_reg))
3811 lra_emit_move (new_reg, *loc);
3812 before = get_insns ();
3813 end_sequence ();
3814 *loc = new_reg;
3815 lra_update_dup (curr_id, i);
3817 else if (goal_alt_matched[i][0] == -1)
3819 machine_mode mode;
3820 rtx reg, *loc;
3821 int hard_regno, byte;
3822 enum op_type type = curr_static_id->operand[i].type;
3824 loc = curr_id->operand_loc[i];
3825 mode = curr_operand_mode[i];
3826 if (GET_CODE (*loc) == SUBREG)
3828 reg = SUBREG_REG (*loc);
3829 byte = SUBREG_BYTE (*loc);
3830 if (REG_P (reg)
3831 /* Strict_low_part requires reload the register not
3832 the sub-register. */
3833 && (curr_static_id->operand[i].strict_low
3834 || (GET_MODE_SIZE (mode)
3835 <= GET_MODE_SIZE (GET_MODE (reg))
3836 && (hard_regno
3837 = get_try_hard_regno (REGNO (reg))) >= 0
3838 && (simplify_subreg_regno
3839 (hard_regno,
3840 GET_MODE (reg), byte, mode) < 0)
3841 && (goal_alt[i] == NO_REGS
3842 || (simplify_subreg_regno
3843 (ira_class_hard_regs[goal_alt[i]][0],
3844 GET_MODE (reg), byte, mode) >= 0)))))
3846 if (type == OP_OUT)
3847 type = OP_INOUT;
3848 loc = &SUBREG_REG (*loc);
3849 mode = GET_MODE (*loc);
3852 old = *loc;
3853 if (get_reload_reg (type, mode, old, goal_alt[i],
3854 loc != curr_id->operand_loc[i], "", &new_reg)
3855 && type != OP_OUT)
3857 push_to_sequence (before);
3858 lra_emit_move (new_reg, old);
3859 before = get_insns ();
3860 end_sequence ();
3862 *loc = new_reg;
3863 if (type != OP_IN
3864 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3866 start_sequence ();
3867 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3868 emit_insn (after);
3869 after = get_insns ();
3870 end_sequence ();
3871 *loc = new_reg;
3873 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3874 if (goal_alt_dont_inherit_ops[j] == i)
3876 lra_set_regno_unique_value (REGNO (new_reg));
3877 break;
3879 lra_update_dup (curr_id, i);
3881 else if (curr_static_id->operand[i].type == OP_IN
3882 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3883 == OP_OUT))
3885 /* generate reloads for input and matched outputs. */
3886 match_inputs[0] = i;
3887 match_inputs[1] = -1;
3888 match_reload (goal_alt_matched[i][0], match_inputs,
3889 goal_alt[i], &before, &after);
3891 else if (curr_static_id->operand[i].type == OP_OUT
3892 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3893 == OP_IN))
3894 /* Generate reloads for output and matched inputs. */
3895 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3896 else if (curr_static_id->operand[i].type == OP_IN
3897 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3898 == OP_IN))
3900 /* Generate reloads for matched inputs. */
3901 match_inputs[0] = i;
3902 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3903 match_inputs[j + 1] = k;
3904 match_inputs[j + 1] = -1;
3905 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3907 else
3908 /* We must generate code in any case when function
3909 process_alt_operands decides that it is possible. */
3910 gcc_unreachable ();
3911 if (optional_p)
3913 lra_assert (REG_P (op));
3914 regno = REGNO (op);
3915 op = *curr_id->operand_loc[i]; /* Substitution. */
3916 if (GET_CODE (op) == SUBREG)
3917 op = SUBREG_REG (op);
3918 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3919 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3920 lra_reg_info[REGNO (op)].restore_regno = regno;
3921 if (lra_dump_file != NULL)
3922 fprintf (lra_dump_file,
3923 " Making reload reg %d for reg %d optional\n",
3924 REGNO (op), regno);
3927 if (before != NULL_RTX || after != NULL_RTX
3928 || max_regno_before != max_reg_num ())
3929 change_p = true;
3930 if (change_p)
3932 lra_update_operator_dups (curr_id);
3933 /* Something changes -- process the insn. */
3934 lra_update_insn_regno_info (curr_insn);
3936 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3937 return change_p;
3940 /* Return true if INSN satisfies all constraints. In other words, no
3941 reload insns are needed. */
3942 bool
3943 lra_constrain_insn (rtx_insn *insn)
3945 int saved_new_regno_start = new_regno_start;
3946 int saved_new_insn_uid_start = new_insn_uid_start;
3947 bool change_p;
3949 curr_insn = insn;
3950 curr_id = lra_get_insn_recog_data (curr_insn);
3951 curr_static_id = curr_id->insn_static_data;
3952 new_insn_uid_start = get_max_uid ();
3953 new_regno_start = max_reg_num ();
3954 change_p = curr_insn_transform (true);
3955 new_regno_start = saved_new_regno_start;
3956 new_insn_uid_start = saved_new_insn_uid_start;
3957 return ! change_p;
3960 /* Return true if X is in LIST. */
3961 static bool
3962 in_list_p (rtx x, rtx list)
3964 for (; list != NULL_RTX; list = XEXP (list, 1))
3965 if (XEXP (list, 0) == x)
3966 return true;
3967 return false;
3970 /* Return true if X contains an allocatable hard register (if
3971 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3972 static bool
3973 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3975 int i, j;
3976 const char *fmt;
3977 enum rtx_code code;
3979 code = GET_CODE (x);
3980 if (REG_P (x))
3982 int regno = REGNO (x);
3983 HARD_REG_SET alloc_regs;
3985 if (hard_reg_p)
3987 if (regno >= FIRST_PSEUDO_REGISTER)
3988 regno = lra_get_regno_hard_regno (regno);
3989 if (regno < 0)
3990 return false;
3991 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3992 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3994 else
3996 if (regno < FIRST_PSEUDO_REGISTER)
3997 return false;
3998 if (! spilled_p)
3999 return true;
4000 return lra_get_regno_hard_regno (regno) < 0;
4003 fmt = GET_RTX_FORMAT (code);
4004 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4006 if (fmt[i] == 'e')
4008 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4009 return true;
4011 else if (fmt[i] == 'E')
4013 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4014 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4015 return true;
4018 return false;
4021 /* Return true if X contains a symbol reg. */
4022 static bool
4023 contains_symbol_ref_p (rtx x)
4025 int i, j;
4026 const char *fmt;
4027 enum rtx_code code;
4029 code = GET_CODE (x);
4030 if (code == SYMBOL_REF)
4031 return true;
4032 fmt = GET_RTX_FORMAT (code);
4033 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4035 if (fmt[i] == 'e')
4037 if (contains_symbol_ref_p (XEXP (x, i)))
4038 return true;
4040 else if (fmt[i] == 'E')
4042 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4043 if (contains_symbol_ref_p (XVECEXP (x, i, j)))
4044 return true;
4047 return false;
4050 /* Process all regs in location *LOC and change them on equivalent
4051 substitution. Return true if any change was done. */
4052 static bool
4053 loc_equivalence_change_p (rtx *loc)
4055 rtx subst, reg, x = *loc;
4056 bool result = false;
4057 enum rtx_code code = GET_CODE (x);
4058 const char *fmt;
4059 int i, j;
4061 if (code == SUBREG)
4063 reg = SUBREG_REG (x);
4064 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4065 && GET_MODE (subst) == VOIDmode)
4067 /* We cannot reload debug location. Simplify subreg here
4068 while we know the inner mode. */
4069 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4070 GET_MODE (reg), SUBREG_BYTE (x));
4071 return true;
4074 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4076 *loc = subst;
4077 return true;
4080 /* Scan all the operand sub-expressions. */
4081 fmt = GET_RTX_FORMAT (code);
4082 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4084 if (fmt[i] == 'e')
4085 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4086 else if (fmt[i] == 'E')
4087 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4088 result
4089 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4091 return result;
4094 /* Similar to loc_equivalence_change_p, but for use as
4095 simplify_replace_fn_rtx callback. DATA is insn for which the
4096 elimination is done. If it null we don't do the elimination. */
4097 static rtx
4098 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4100 if (!REG_P (loc))
4101 return NULL_RTX;
4103 rtx subst = (data == NULL
4104 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4105 if (subst != loc)
4106 return subst;
4108 return NULL_RTX;
4111 /* Maximum number of generated reload insns per an insn. It is for
4112 preventing this pass cycling in a bug case. */
4113 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4115 /* The current iteration number of this LRA pass. */
4116 int lra_constraint_iter;
4118 /* True if we substituted equiv which needs checking register
4119 allocation correctness because the equivalent value contains
4120 allocatable hard registers or when we restore multi-register
4121 pseudo. */
4122 bool lra_risky_transformations_p;
4124 /* Return true if REGNO is referenced in more than one block. */
4125 static bool
4126 multi_block_pseudo_p (int regno)
4128 basic_block bb = NULL;
4129 unsigned int uid;
4130 bitmap_iterator bi;
4132 if (regno < FIRST_PSEUDO_REGISTER)
4133 return false;
4135 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4136 if (bb == NULL)
4137 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4138 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4139 return true;
4140 return false;
4143 /* Return true if LIST contains a deleted insn. */
4144 static bool
4145 contains_deleted_insn_p (rtx_insn_list *list)
4147 for (; list != NULL_RTX; list = list->next ())
4148 if (NOTE_P (list->insn ())
4149 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4150 return true;
4151 return false;
4154 /* Return true if X contains a pseudo dying in INSN. */
4155 static bool
4156 dead_pseudo_p (rtx x, rtx insn)
4158 int i, j;
4159 const char *fmt;
4160 enum rtx_code code;
4162 if (REG_P (x))
4163 return (insn != NULL_RTX
4164 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4165 code = GET_CODE (x);
4166 fmt = GET_RTX_FORMAT (code);
4167 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4169 if (fmt[i] == 'e')
4171 if (dead_pseudo_p (XEXP (x, i), insn))
4172 return true;
4174 else if (fmt[i] == 'E')
4176 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4177 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4178 return true;
4181 return false;
4184 /* Return true if INSN contains a dying pseudo in INSN right hand
4185 side. */
4186 static bool
4187 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4189 rtx set = single_set (insn);
4191 gcc_assert (set != NULL);
4192 return dead_pseudo_p (SET_SRC (set), insn);
4195 /* Return true if any init insn of REGNO contains a dying pseudo in
4196 insn right hand side. */
4197 static bool
4198 init_insn_rhs_dead_pseudo_p (int regno)
4200 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4202 if (insns == NULL)
4203 return false;
4204 for (; insns != NULL_RTX; insns = insns->next ())
4205 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4206 return true;
4207 return false;
4210 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4211 reverse only if we have one init insn with given REGNO as a
4212 source. */
4213 static bool
4214 reverse_equiv_p (int regno)
4216 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4217 rtx set;
4219 if (insns == NULL)
4220 return false;
4221 if (! INSN_P (insns->insn ())
4222 || insns->next () != NULL)
4223 return false;
4224 if ((set = single_set (insns->insn ())) == NULL_RTX)
4225 return false;
4226 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4229 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4230 call this function only for non-reverse equivalence. */
4231 static bool
4232 contains_reloaded_insn_p (int regno)
4234 rtx set;
4235 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4237 for (; list != NULL; list = list->next ())
4238 if ((set = single_set (list->insn ())) == NULL_RTX
4239 || ! REG_P (SET_DEST (set))
4240 || (int) REGNO (SET_DEST (set)) != regno)
4241 return true;
4242 return false;
4245 /* Entry function of LRA constraint pass. Return true if the
4246 constraint pass did change the code. */
4247 bool
4248 lra_constraints (bool first_p)
4250 bool changed_p;
4251 int i, hard_regno, new_insns_num;
4252 unsigned int min_len, new_min_len, uid;
4253 rtx set, x, reg, dest_reg;
4254 basic_block last_bb;
4255 bitmap_head equiv_insn_bitmap;
4256 bitmap_iterator bi;
4258 lra_constraint_iter++;
4259 if (lra_dump_file != NULL)
4260 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4261 lra_constraint_iter);
4262 changed_p = false;
4263 if (pic_offset_table_rtx
4264 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4265 lra_risky_transformations_p = true;
4266 else
4267 lra_risky_transformations_p = false;
4268 new_insn_uid_start = get_max_uid ();
4269 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4270 /* Mark used hard regs for target stack size calulations. */
4271 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4272 if (lra_reg_info[i].nrefs != 0
4273 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4275 int j, nregs;
4277 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4278 for (j = 0; j < nregs; j++)
4279 df_set_regs_ever_live (hard_regno + j, true);
4281 /* Do elimination before the equivalence processing as we can spill
4282 some pseudos during elimination. */
4283 lra_eliminate (false, first_p);
4284 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4285 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4286 if (lra_reg_info[i].nrefs != 0)
4288 ira_reg_equiv[i].profitable_p = true;
4289 reg = regno_reg_rtx[i];
4290 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4292 bool pseudo_p = contains_reg_p (x, false, false);
4294 /* After RTL transformation, we can not guarantee that
4295 pseudo in the substitution was not reloaded which might
4296 make equivalence invalid. For example, in reverse
4297 equiv of p0
4299 p0 <- ...
4301 equiv_mem <- p0
4303 the memory address register was reloaded before the 2nd
4304 insn. */
4305 if ((! first_p && pseudo_p)
4306 /* We don't use DF for compilation speed sake. So it
4307 is problematic to update live info when we use an
4308 equivalence containing pseudos in more than one
4309 BB. */
4310 || (pseudo_p && multi_block_pseudo_p (i))
4311 /* If an init insn was deleted for some reason, cancel
4312 the equiv. We could update the equiv insns after
4313 transformations including an equiv insn deletion
4314 but it is not worthy as such cases are extremely
4315 rare. */
4316 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4317 /* If it is not a reverse equivalence, we check that a
4318 pseudo in rhs of the init insn is not dying in the
4319 insn. Otherwise, the live info at the beginning of
4320 the corresponding BB might be wrong after we
4321 removed the insn. When the equiv can be a
4322 constant, the right hand side of the init insn can
4323 be a pseudo. */
4324 || (! reverse_equiv_p (i)
4325 && (init_insn_rhs_dead_pseudo_p (i)
4326 /* If we reloaded the pseudo in an equivalence
4327 init insn, we can not remove the equiv init
4328 insns and the init insns might write into
4329 const memory in this case. */
4330 || contains_reloaded_insn_p (i)))
4331 /* Prevent access beyond equivalent memory for
4332 paradoxical subregs. */
4333 || (MEM_P (x)
4334 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4335 > GET_MODE_SIZE (GET_MODE (x))))
4336 || (pic_offset_table_rtx
4337 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4338 && (targetm.preferred_reload_class
4339 (x, lra_get_allocno_class (i)) == NO_REGS))
4340 || contains_symbol_ref_p (x))))
4341 ira_reg_equiv[i].defined_p = false;
4342 if (contains_reg_p (x, false, true))
4343 ira_reg_equiv[i].profitable_p = false;
4344 if (get_equiv (reg) != reg)
4345 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4348 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4349 update_equiv (i);
4350 /* We should add all insns containing pseudos which should be
4351 substituted by their equivalences. */
4352 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4353 lra_push_insn_by_uid (uid);
4354 min_len = lra_insn_stack_length ();
4355 new_insns_num = 0;
4356 last_bb = NULL;
4357 changed_p = false;
4358 while ((new_min_len = lra_insn_stack_length ()) != 0)
4360 curr_insn = lra_pop_insn ();
4361 --new_min_len;
4362 curr_bb = BLOCK_FOR_INSN (curr_insn);
4363 if (curr_bb != last_bb)
4365 last_bb = curr_bb;
4366 bb_reload_num = lra_curr_reload_num;
4368 if (min_len > new_min_len)
4370 min_len = new_min_len;
4371 new_insns_num = 0;
4373 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4374 internal_error
4375 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4376 MAX_RELOAD_INSNS_NUMBER);
4377 new_insns_num++;
4378 if (DEBUG_INSN_P (curr_insn))
4380 /* We need to check equivalence in debug insn and change
4381 pseudo to the equivalent value if necessary. */
4382 curr_id = lra_get_insn_recog_data (curr_insn);
4383 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4385 rtx old = *curr_id->operand_loc[0];
4386 *curr_id->operand_loc[0]
4387 = simplify_replace_fn_rtx (old, NULL_RTX,
4388 loc_equivalence_callback, curr_insn);
4389 if (old != *curr_id->operand_loc[0])
4391 lra_update_insn_regno_info (curr_insn);
4392 changed_p = true;
4396 else if (INSN_P (curr_insn))
4398 if ((set = single_set (curr_insn)) != NULL_RTX)
4400 dest_reg = SET_DEST (set);
4401 /* The equivalence pseudo could be set up as SUBREG in a
4402 case when it is a call restore insn in a mode
4403 different from the pseudo mode. */
4404 if (GET_CODE (dest_reg) == SUBREG)
4405 dest_reg = SUBREG_REG (dest_reg);
4406 if ((REG_P (dest_reg)
4407 && (x = get_equiv (dest_reg)) != dest_reg
4408 /* Remove insns which set up a pseudo whose value
4409 can not be changed. Such insns might be not in
4410 init_insns because we don't update equiv data
4411 during insn transformations.
4413 As an example, let suppose that a pseudo got
4414 hard register and on the 1st pass was not
4415 changed to equivalent constant. We generate an
4416 additional insn setting up the pseudo because of
4417 secondary memory movement. Then the pseudo is
4418 spilled and we use the equiv constant. In this
4419 case we should remove the additional insn and
4420 this insn is not init_insns list. */
4421 && (! MEM_P (x) || MEM_READONLY_P (x)
4422 /* Check that this is actually an insn setting
4423 up the equivalence. */
4424 || in_list_p (curr_insn,
4425 ira_reg_equiv
4426 [REGNO (dest_reg)].init_insns)))
4427 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4428 && in_list_p (curr_insn,
4429 ira_reg_equiv
4430 [REGNO (SET_SRC (set))].init_insns)))
4432 /* This is equiv init insn of pseudo which did not get a
4433 hard register -- remove the insn. */
4434 if (lra_dump_file != NULL)
4436 fprintf (lra_dump_file,
4437 " Removing equiv init insn %i (freq=%d)\n",
4438 INSN_UID (curr_insn),
4439 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4440 dump_insn_slim (lra_dump_file, curr_insn);
4442 if (contains_reg_p (x, true, false))
4443 lra_risky_transformations_p = true;
4444 lra_set_insn_deleted (curr_insn);
4445 continue;
4448 curr_id = lra_get_insn_recog_data (curr_insn);
4449 curr_static_id = curr_id->insn_static_data;
4450 init_curr_insn_input_reloads ();
4451 init_curr_operand_mode ();
4452 if (curr_insn_transform (false))
4453 changed_p = true;
4454 /* Check non-transformed insns too for equiv change as USE
4455 or CLOBBER don't need reloads but can contain pseudos
4456 being changed on their equivalences. */
4457 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4458 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4460 lra_update_insn_regno_info (curr_insn);
4461 changed_p = true;
4465 bitmap_clear (&equiv_insn_bitmap);
4466 /* If we used a new hard regno, changed_p should be true because the
4467 hard reg is assigned to a new pseudo. */
4468 #ifdef ENABLE_CHECKING
4469 if (! changed_p)
4471 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4472 if (lra_reg_info[i].nrefs != 0
4473 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4475 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4477 for (j = 0; j < nregs; j++)
4478 lra_assert (df_regs_ever_live_p (hard_regno + j));
4481 #endif
4482 return changed_p;
4485 /* Initiate the LRA constraint pass. It is done once per
4486 function. */
4487 void
4488 lra_constraints_init (void)
4492 /* Finalize the LRA constraint pass. It is done once per
4493 function. */
4494 void
4495 lra_constraints_finish (void)
4501 /* This page contains code to do inheritance/split
4502 transformations. */
4504 /* Number of reloads passed so far in current EBB. */
4505 static int reloads_num;
4507 /* Number of calls passed so far in current EBB. */
4508 static int calls_num;
4510 /* Current reload pseudo check for validity of elements in
4511 USAGE_INSNS. */
4512 static int curr_usage_insns_check;
4514 /* Info about last usage of registers in EBB to do inheritance/split
4515 transformation. Inheritance transformation is done from a spilled
4516 pseudo and split transformations from a hard register or a pseudo
4517 assigned to a hard register. */
4518 struct usage_insns
4520 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4521 value INSNS is valid. The insns is chain of optional debug insns
4522 and a finishing non-debug insn using the corresponding reg. The
4523 value is also used to mark the registers which are set up in the
4524 current insn. The negated insn uid is used for this. */
4525 int check;
4526 /* Value of global reloads_num at the last insn in INSNS. */
4527 int reloads_num;
4528 /* Value of global reloads_nums at the last insn in INSNS. */
4529 int calls_num;
4530 /* It can be true only for splitting. And it means that the restore
4531 insn should be put after insn given by the following member. */
4532 bool after_p;
4533 /* Next insns in the current EBB which use the original reg and the
4534 original reg value is not changed between the current insn and
4535 the next insns. In order words, e.g. for inheritance, if we need
4536 to use the original reg value again in the next insns we can try
4537 to use the value in a hard register from a reload insn of the
4538 current insn. */
4539 rtx insns;
4542 /* Map: regno -> corresponding pseudo usage insns. */
4543 static struct usage_insns *usage_insns;
4545 static void
4546 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4548 usage_insns[regno].check = curr_usage_insns_check;
4549 usage_insns[regno].insns = insn;
4550 usage_insns[regno].reloads_num = reloads_num;
4551 usage_insns[regno].calls_num = calls_num;
4552 usage_insns[regno].after_p = after_p;
4555 /* The function is used to form list REGNO usages which consists of
4556 optional debug insns finished by a non-debug insn using REGNO.
4557 RELOADS_NUM is current number of reload insns processed so far. */
4558 static void
4559 add_next_usage_insn (int regno, rtx insn, int reloads_num)
4561 rtx next_usage_insns;
4563 if (usage_insns[regno].check == curr_usage_insns_check
4564 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4565 && DEBUG_INSN_P (insn))
4567 /* Check that we did not add the debug insn yet. */
4568 if (next_usage_insns != insn
4569 && (GET_CODE (next_usage_insns) != INSN_LIST
4570 || XEXP (next_usage_insns, 0) != insn))
4571 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4572 next_usage_insns);
4574 else if (NONDEBUG_INSN_P (insn))
4575 setup_next_usage_insn (regno, insn, reloads_num, false);
4576 else
4577 usage_insns[regno].check = 0;
4580 /* Return first non-debug insn in list USAGE_INSNS. */
4581 static rtx_insn *
4582 skip_usage_debug_insns (rtx usage_insns)
4584 rtx insn;
4586 /* Skip debug insns. */
4587 for (insn = usage_insns;
4588 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4589 insn = XEXP (insn, 1))
4591 return safe_as_a <rtx_insn *> (insn);
4594 /* Return true if we need secondary memory moves for insn in
4595 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4596 into the insn. */
4597 static bool
4598 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4599 rtx usage_insns ATTRIBUTE_UNUSED)
4601 #ifndef SECONDARY_MEMORY_NEEDED
4602 return false;
4603 #else
4604 rtx_insn *insn;
4605 rtx set, dest;
4606 enum reg_class cl;
4608 if (inher_cl == ALL_REGS
4609 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4610 return false;
4611 lra_assert (INSN_P (insn));
4612 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4613 return false;
4614 dest = SET_DEST (set);
4615 if (! REG_P (dest))
4616 return false;
4617 lra_assert (inher_cl != NO_REGS);
4618 cl = get_reg_class (REGNO (dest));
4619 return (cl != NO_REGS && cl != ALL_REGS
4620 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4621 #endif
4624 /* Registers involved in inheritance/split in the current EBB
4625 (inheritance/split pseudos and original registers). */
4626 static bitmap_head check_only_regs;
4628 /* Do inheritance transformations for insn INSN, which defines (if
4629 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4630 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4631 form as the "insns" field of usage_insns. Return true if we
4632 succeed in such transformation.
4634 The transformations look like:
4636 p <- ... i <- ...
4637 ... p <- i (new insn)
4638 ... =>
4639 <- ... p ... <- ... i ...
4641 ... i <- p (new insn)
4642 <- ... p ... <- ... i ...
4643 ... =>
4644 <- ... p ... <- ... i ...
4645 where p is a spilled original pseudo and i is a new inheritance pseudo.
4648 The inheritance pseudo has the smallest class of two classes CL and
4649 class of ORIGINAL REGNO. */
4650 static bool
4651 inherit_reload_reg (bool def_p, int original_regno,
4652 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4654 if (optimize_function_for_size_p (cfun))
4655 return false;
4657 enum reg_class rclass = lra_get_allocno_class (original_regno);
4658 rtx original_reg = regno_reg_rtx[original_regno];
4659 rtx new_reg, usage_insn;
4660 rtx_insn *new_insns;
4662 lra_assert (! usage_insns[original_regno].after_p);
4663 if (lra_dump_file != NULL)
4664 fprintf (lra_dump_file,
4665 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4666 if (! ira_reg_classes_intersect_p[cl][rclass])
4668 if (lra_dump_file != NULL)
4670 fprintf (lra_dump_file,
4671 " Rejecting inheritance for %d "
4672 "because of disjoint classes %s and %s\n",
4673 original_regno, reg_class_names[cl],
4674 reg_class_names[rclass]);
4675 fprintf (lra_dump_file,
4676 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4678 return false;
4680 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4681 /* We don't use a subset of two classes because it can be
4682 NO_REGS. This transformation is still profitable in most
4683 cases even if the classes are not intersected as register
4684 move is probably cheaper than a memory load. */
4685 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4687 if (lra_dump_file != NULL)
4688 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4689 reg_class_names[cl], reg_class_names[rclass]);
4691 rclass = cl;
4693 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4695 /* Reject inheritance resulting in secondary memory moves.
4696 Otherwise, there is a danger in LRA cycling. Also such
4697 transformation will be unprofitable. */
4698 if (lra_dump_file != NULL)
4700 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4701 rtx set = single_set (insn);
4703 lra_assert (set != NULL_RTX);
4705 rtx dest = SET_DEST (set);
4707 lra_assert (REG_P (dest));
4708 fprintf (lra_dump_file,
4709 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4710 "as secondary mem is needed\n",
4711 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4712 original_regno, reg_class_names[rclass]);
4713 fprintf (lra_dump_file,
4714 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4716 return false;
4718 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4719 rclass, "inheritance");
4720 start_sequence ();
4721 if (def_p)
4722 lra_emit_move (original_reg, new_reg);
4723 else
4724 lra_emit_move (new_reg, original_reg);
4725 new_insns = get_insns ();
4726 end_sequence ();
4727 if (NEXT_INSN (new_insns) != NULL_RTX)
4729 if (lra_dump_file != NULL)
4731 fprintf (lra_dump_file,
4732 " Rejecting inheritance %d->%d "
4733 "as it results in 2 or more insns:\n",
4734 original_regno, REGNO (new_reg));
4735 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4736 fprintf (lra_dump_file,
4737 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4739 return false;
4741 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg);
4742 lra_update_insn_regno_info (insn);
4743 if (! def_p)
4744 /* We now have a new usage insn for original regno. */
4745 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4746 if (lra_dump_file != NULL)
4747 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4748 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4749 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4750 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4751 bitmap_set_bit (&check_only_regs, original_regno);
4752 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4753 if (def_p)
4754 lra_process_new_insns (insn, NULL, new_insns,
4755 "Add original<-inheritance");
4756 else
4757 lra_process_new_insns (insn, new_insns, NULL,
4758 "Add inheritance<-original");
4759 while (next_usage_insns != NULL_RTX)
4761 if (GET_CODE (next_usage_insns) != INSN_LIST)
4763 usage_insn = next_usage_insns;
4764 lra_assert (NONDEBUG_INSN_P (usage_insn));
4765 next_usage_insns = NULL;
4767 else
4769 usage_insn = XEXP (next_usage_insns, 0);
4770 lra_assert (DEBUG_INSN_P (usage_insn));
4771 next_usage_insns = XEXP (next_usage_insns, 1);
4773 lra_substitute_pseudo (&usage_insn, original_regno, new_reg);
4774 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4775 if (lra_dump_file != NULL)
4777 fprintf (lra_dump_file,
4778 " Inheritance reuse change %d->%d (bb%d):\n",
4779 original_regno, REGNO (new_reg),
4780 BLOCK_FOR_INSN (usage_insn)->index);
4781 dump_insn_slim (lra_dump_file, usage_insn);
4784 if (lra_dump_file != NULL)
4785 fprintf (lra_dump_file,
4786 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4787 return true;
4790 /* Return true if we need a caller save/restore for pseudo REGNO which
4791 was assigned to a hard register. */
4792 static inline bool
4793 need_for_call_save_p (int regno)
4795 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4796 return (usage_insns[regno].calls_num < calls_num
4797 && (overlaps_hard_reg_set_p
4798 ((flag_ipa_ra &&
4799 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4800 ? lra_reg_info[regno].actual_call_used_reg_set
4801 : call_used_reg_set,
4802 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4803 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4804 PSEUDO_REGNO_MODE (regno))));
4807 /* Global registers occurring in the current EBB. */
4808 static bitmap_head ebb_global_regs;
4810 /* Return true if we need a split for hard register REGNO or pseudo
4811 REGNO which was assigned to a hard register.
4812 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4813 used for reloads since the EBB end. It is an approximation of the
4814 used hard registers in the split range. The exact value would
4815 require expensive calculations. If we were aggressive with
4816 splitting because of the approximation, the split pseudo will save
4817 the same hard register assignment and will be removed in the undo
4818 pass. We still need the approximation because too aggressive
4819 splitting would result in too inaccurate cost calculation in the
4820 assignment pass because of too many generated moves which will be
4821 probably removed in the undo pass. */
4822 static inline bool
4823 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4825 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4827 lra_assert (hard_regno >= 0);
4828 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4829 /* Don't split eliminable hard registers, otherwise we can
4830 split hard registers like hard frame pointer, which
4831 lives on BB start/end according to DF-infrastructure,
4832 when there is a pseudo assigned to the register and
4833 living in the same BB. */
4834 && (regno >= FIRST_PSEUDO_REGISTER
4835 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4836 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4837 /* Don't split call clobbered hard regs living through
4838 calls, otherwise we might have a check problem in the
4839 assign sub-pass as in the most cases (exception is a
4840 situation when lra_risky_transformations_p value is
4841 true) the assign pass assumes that all pseudos living
4842 through calls are assigned to call saved hard regs. */
4843 && (regno >= FIRST_PSEUDO_REGISTER
4844 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4845 || usage_insns[regno].calls_num == calls_num)
4846 /* We need at least 2 reloads to make pseudo splitting
4847 profitable. We should provide hard regno splitting in
4848 any case to solve 1st insn scheduling problem when
4849 moving hard register definition up might result in
4850 impossibility to find hard register for reload pseudo of
4851 small register class. */
4852 && (usage_insns[regno].reloads_num
4853 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4854 && (regno < FIRST_PSEUDO_REGISTER
4855 /* For short living pseudos, spilling + inheritance can
4856 be considered a substitution for splitting.
4857 Therefore we do not splitting for local pseudos. It
4858 decreases also aggressiveness of splitting. The
4859 minimal number of references is chosen taking into
4860 account that for 2 references splitting has no sense
4861 as we can just spill the pseudo. */
4862 || (regno >= FIRST_PSEUDO_REGISTER
4863 && lra_reg_info[regno].nrefs > 3
4864 && bitmap_bit_p (&ebb_global_regs, regno))))
4865 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4868 /* Return class for the split pseudo created from original pseudo with
4869 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4870 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4871 results in no secondary memory movements. */
4872 static enum reg_class
4873 choose_split_class (enum reg_class allocno_class,
4874 int hard_regno ATTRIBUTE_UNUSED,
4875 machine_mode mode ATTRIBUTE_UNUSED)
4877 #ifndef SECONDARY_MEMORY_NEEDED
4878 return allocno_class;
4879 #else
4880 int i;
4881 enum reg_class cl, best_cl = NO_REGS;
4882 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4883 = REGNO_REG_CLASS (hard_regno);
4885 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4886 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4887 return allocno_class;
4888 for (i = 0;
4889 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4890 i++)
4891 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4892 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4893 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4894 && (best_cl == NO_REGS
4895 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4896 best_cl = cl;
4897 return best_cl;
4898 #endif
4901 /* Do split transformations for insn INSN, which defines or uses
4902 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4903 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4904 "insns" field of usage_insns.
4906 The transformations look like:
4908 p <- ... p <- ...
4909 ... s <- p (new insn -- save)
4910 ... =>
4911 ... p <- s (new insn -- restore)
4912 <- ... p ... <- ... p ...
4914 <- ... p ... <- ... p ...
4915 ... s <- p (new insn -- save)
4916 ... =>
4917 ... p <- s (new insn -- restore)
4918 <- ... p ... <- ... p ...
4920 where p is an original pseudo got a hard register or a hard
4921 register and s is a new split pseudo. The save is put before INSN
4922 if BEFORE_P is true. Return true if we succeed in such
4923 transformation. */
4924 static bool
4925 split_reg (bool before_p, int original_regno, rtx_insn *insn,
4926 rtx next_usage_insns)
4928 enum reg_class rclass;
4929 rtx original_reg;
4930 int hard_regno, nregs;
4931 rtx new_reg, usage_insn;
4932 rtx_insn *restore, *save;
4933 bool after_p;
4934 bool call_save_p;
4936 if (original_regno < FIRST_PSEUDO_REGISTER)
4938 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4939 hard_regno = original_regno;
4940 call_save_p = false;
4941 nregs = 1;
4943 else
4945 hard_regno = reg_renumber[original_regno];
4946 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4947 rclass = lra_get_allocno_class (original_regno);
4948 original_reg = regno_reg_rtx[original_regno];
4949 call_save_p = need_for_call_save_p (original_regno);
4951 original_reg = regno_reg_rtx[original_regno];
4952 lra_assert (hard_regno >= 0);
4953 if (lra_dump_file != NULL)
4954 fprintf (lra_dump_file,
4955 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4956 if (call_save_p)
4958 machine_mode mode = GET_MODE (original_reg);
4960 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4961 hard_regno_nregs[hard_regno][mode],
4962 mode);
4963 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4965 else
4967 rclass = choose_split_class (rclass, hard_regno,
4968 GET_MODE (original_reg));
4969 if (rclass == NO_REGS)
4971 if (lra_dump_file != NULL)
4973 fprintf (lra_dump_file,
4974 " Rejecting split of %d(%s): "
4975 "no good reg class for %d(%s)\n",
4976 original_regno,
4977 reg_class_names[lra_get_allocno_class (original_regno)],
4978 hard_regno,
4979 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4980 fprintf
4981 (lra_dump_file,
4982 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4984 return false;
4986 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4987 rclass, "split");
4988 reg_renumber[REGNO (new_reg)] = hard_regno;
4990 save = emit_spill_move (true, new_reg, original_reg);
4991 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
4993 if (lra_dump_file != NULL)
4995 fprintf
4996 (lra_dump_file,
4997 " Rejecting split %d->%d resulting in > 2 save insns:\n",
4998 original_regno, REGNO (new_reg));
4999 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
5000 fprintf (lra_dump_file,
5001 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5003 return false;
5005 restore = emit_spill_move (false, new_reg, original_reg);
5006 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
5008 if (lra_dump_file != NULL)
5010 fprintf (lra_dump_file,
5011 " Rejecting split %d->%d "
5012 "resulting in > 2 restore insns:\n",
5013 original_regno, REGNO (new_reg));
5014 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
5015 fprintf (lra_dump_file,
5016 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5018 return false;
5020 after_p = usage_insns[original_regno].after_p;
5021 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
5022 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5023 bitmap_set_bit (&check_only_regs, original_regno);
5024 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
5025 for (;;)
5027 if (GET_CODE (next_usage_insns) != INSN_LIST)
5029 usage_insn = next_usage_insns;
5030 break;
5032 usage_insn = XEXP (next_usage_insns, 0);
5033 lra_assert (DEBUG_INSN_P (usage_insn));
5034 next_usage_insns = XEXP (next_usage_insns, 1);
5035 lra_substitute_pseudo (&usage_insn, original_regno, new_reg);
5036 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5037 if (lra_dump_file != NULL)
5039 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5040 original_regno, REGNO (new_reg));
5041 dump_insn_slim (lra_dump_file, usage_insn);
5044 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5045 lra_assert (usage_insn != insn || (after_p && before_p));
5046 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5047 after_p ? NULL : restore,
5048 after_p ? restore : NULL,
5049 call_save_p
5050 ? "Add reg<-save" : "Add reg<-split");
5051 lra_process_new_insns (insn, before_p ? save : NULL,
5052 before_p ? NULL : save,
5053 call_save_p
5054 ? "Add save<-reg" : "Add split<-reg");
5055 if (nregs > 1)
5056 /* If we are trying to split multi-register. We should check
5057 conflicts on the next assignment sub-pass. IRA can allocate on
5058 sub-register levels, LRA do this on pseudos level right now and
5059 this discrepancy may create allocation conflicts after
5060 splitting. */
5061 lra_risky_transformations_p = true;
5062 if (lra_dump_file != NULL)
5063 fprintf (lra_dump_file,
5064 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5065 return true;
5068 /* Recognize that we need a split transformation for insn INSN, which
5069 defines or uses REGNO in its insn biggest MODE (we use it only if
5070 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5071 hard registers which might be used for reloads since the EBB end.
5072 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5073 uid before starting INSN processing. Return true if we succeed in
5074 such transformation. */
5075 static bool
5076 split_if_necessary (int regno, machine_mode mode,
5077 HARD_REG_SET potential_reload_hard_regs,
5078 bool before_p, rtx_insn *insn, int max_uid)
5080 bool res = false;
5081 int i, nregs = 1;
5082 rtx next_usage_insns;
5084 if (regno < FIRST_PSEUDO_REGISTER)
5085 nregs = hard_regno_nregs[regno][mode];
5086 for (i = 0; i < nregs; i++)
5087 if (usage_insns[regno + i].check == curr_usage_insns_check
5088 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5089 /* To avoid processing the register twice or more. */
5090 && ((GET_CODE (next_usage_insns) != INSN_LIST
5091 && INSN_UID (next_usage_insns) < max_uid)
5092 || (GET_CODE (next_usage_insns) == INSN_LIST
5093 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5094 && need_for_split_p (potential_reload_hard_regs, regno + i)
5095 && split_reg (before_p, regno + i, insn, next_usage_insns))
5096 res = true;
5097 return res;
5100 /* Check only registers living at the current program point in the
5101 current EBB. */
5102 static bitmap_head live_regs;
5104 /* Update live info in EBB given by its HEAD and TAIL insns after
5105 inheritance/split transformation. The function removes dead moves
5106 too. */
5107 static void
5108 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5110 unsigned int j;
5111 int i, regno;
5112 bool live_p;
5113 rtx_insn *prev_insn;
5114 rtx set;
5115 bool remove_p;
5116 basic_block last_bb, prev_bb, curr_bb;
5117 bitmap_iterator bi;
5118 struct lra_insn_reg *reg;
5119 edge e;
5120 edge_iterator ei;
5122 last_bb = BLOCK_FOR_INSN (tail);
5123 prev_bb = NULL;
5124 for (curr_insn = tail;
5125 curr_insn != PREV_INSN (head);
5126 curr_insn = prev_insn)
5128 prev_insn = PREV_INSN (curr_insn);
5129 /* We need to process empty blocks too. They contain
5130 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5131 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5132 continue;
5133 curr_bb = BLOCK_FOR_INSN (curr_insn);
5134 if (curr_bb != prev_bb)
5136 if (prev_bb != NULL)
5138 /* Update df_get_live_in (prev_bb): */
5139 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5140 if (bitmap_bit_p (&live_regs, j))
5141 bitmap_set_bit (df_get_live_in (prev_bb), j);
5142 else
5143 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5145 if (curr_bb != last_bb)
5147 /* Update df_get_live_out (curr_bb): */
5148 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5150 live_p = bitmap_bit_p (&live_regs, j);
5151 if (! live_p)
5152 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5153 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5155 live_p = true;
5156 break;
5158 if (live_p)
5159 bitmap_set_bit (df_get_live_out (curr_bb), j);
5160 else
5161 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5164 prev_bb = curr_bb;
5165 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5167 if (! NONDEBUG_INSN_P (curr_insn))
5168 continue;
5169 curr_id = lra_get_insn_recog_data (curr_insn);
5170 curr_static_id = curr_id->insn_static_data;
5171 remove_p = false;
5172 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
5173 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5174 && bitmap_bit_p (&check_only_regs, regno)
5175 && ! bitmap_bit_p (&live_regs, regno))
5176 remove_p = true;
5177 /* See which defined values die here. */
5178 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5179 if (reg->type == OP_OUT && ! reg->subreg_p)
5180 bitmap_clear_bit (&live_regs, reg->regno);
5181 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5182 if (reg->type == OP_OUT && ! reg->subreg_p)
5183 bitmap_clear_bit (&live_regs, reg->regno);
5184 /* Mark each used value as live. */
5185 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5186 if (reg->type != OP_OUT
5187 && bitmap_bit_p (&check_only_regs, reg->regno))
5188 bitmap_set_bit (&live_regs, reg->regno);
5189 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5190 if (reg->type != OP_OUT
5191 && bitmap_bit_p (&check_only_regs, reg->regno))
5192 bitmap_set_bit (&live_regs, reg->regno);
5193 if (curr_id->arg_hard_regs != NULL)
5194 /* Make argument hard registers live. */
5195 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5196 if (bitmap_bit_p (&check_only_regs, regno))
5197 bitmap_set_bit (&live_regs, regno);
5198 /* It is quite important to remove dead move insns because it
5199 means removing dead store. We don't need to process them for
5200 constraints. */
5201 if (remove_p)
5203 if (lra_dump_file != NULL)
5205 fprintf (lra_dump_file, " Removing dead insn:\n ");
5206 dump_insn_slim (lra_dump_file, curr_insn);
5208 lra_set_insn_deleted (curr_insn);
5213 /* The structure describes info to do an inheritance for the current
5214 insn. We need to collect such info first before doing the
5215 transformations because the transformations change the insn
5216 internal representation. */
5217 struct to_inherit
5219 /* Original regno. */
5220 int regno;
5221 /* Subsequent insns which can inherit original reg value. */
5222 rtx insns;
5225 /* Array containing all info for doing inheritance from the current
5226 insn. */
5227 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5229 /* Number elements in the previous array. */
5230 static int to_inherit_num;
5232 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5233 structure to_inherit. */
5234 static void
5235 add_to_inherit (int regno, rtx insns)
5237 int i;
5239 for (i = 0; i < to_inherit_num; i++)
5240 if (to_inherit[i].regno == regno)
5241 return;
5242 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5243 to_inherit[to_inherit_num].regno = regno;
5244 to_inherit[to_inherit_num++].insns = insns;
5247 /* Return the last non-debug insn in basic block BB, or the block begin
5248 note if none. */
5249 static rtx_insn *
5250 get_last_insertion_point (basic_block bb)
5252 rtx_insn *insn;
5254 FOR_BB_INSNS_REVERSE (bb, insn)
5255 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5256 return insn;
5257 gcc_unreachable ();
5260 /* Set up RES by registers living on edges FROM except the edge (FROM,
5261 TO) or by registers set up in a jump insn in BB FROM. */
5262 static void
5263 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5265 rtx_insn *last;
5266 struct lra_insn_reg *reg;
5267 edge e;
5268 edge_iterator ei;
5270 lra_assert (to != NULL);
5271 bitmap_clear (res);
5272 FOR_EACH_EDGE (e, ei, from->succs)
5273 if (e->dest != to)
5274 bitmap_ior_into (res, df_get_live_in (e->dest));
5275 last = get_last_insertion_point (from);
5276 if (! JUMP_P (last))
5277 return;
5278 curr_id = lra_get_insn_recog_data (last);
5279 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5280 if (reg->type != OP_IN)
5281 bitmap_set_bit (res, reg->regno);
5284 /* Used as a temporary results of some bitmap calculations. */
5285 static bitmap_head temp_bitmap;
5287 /* We split for reloads of small class of hard regs. The following
5288 defines how many hard regs the class should have to be qualified as
5289 small. The code is mostly oriented to x86/x86-64 architecture
5290 where some insns need to use only specific register or pair of
5291 registers and these register can live in RTL explicitly, e.g. for
5292 parameter passing. */
5293 static const int max_small_class_regs_num = 2;
5295 /* Do inheritance/split transformations in EBB starting with HEAD and
5296 finishing on TAIL. We process EBB insns in the reverse order.
5297 Return true if we did any inheritance/split transformation in the
5298 EBB.
5300 We should avoid excessive splitting which results in worse code
5301 because of inaccurate cost calculations for spilling new split
5302 pseudos in such case. To achieve this we do splitting only if
5303 register pressure is high in given basic block and there are reload
5304 pseudos requiring hard registers. We could do more register
5305 pressure calculations at any given program point to avoid necessary
5306 splitting even more but it is to expensive and the current approach
5307 works well enough. */
5308 static bool
5309 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5311 int i, src_regno, dst_regno, nregs;
5312 bool change_p, succ_p, update_reloads_num_p;
5313 rtx_insn *prev_insn, *last_insn;
5314 rtx next_usage_insns, set;
5315 enum reg_class cl;
5316 struct lra_insn_reg *reg;
5317 basic_block last_processed_bb, curr_bb = NULL;
5318 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5319 bitmap to_process;
5320 unsigned int j;
5321 bitmap_iterator bi;
5322 bool head_p, after_p;
5324 change_p = false;
5325 curr_usage_insns_check++;
5326 reloads_num = calls_num = 0;
5327 bitmap_clear (&check_only_regs);
5328 last_processed_bb = NULL;
5329 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5330 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5331 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5332 /* We don't process new insns generated in the loop. */
5333 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5335 prev_insn = PREV_INSN (curr_insn);
5336 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5337 curr_bb = BLOCK_FOR_INSN (curr_insn);
5338 if (last_processed_bb != curr_bb)
5340 /* We are at the end of BB. Add qualified living
5341 pseudos for potential splitting. */
5342 to_process = df_get_live_out (curr_bb);
5343 if (last_processed_bb != NULL)
5345 /* We are somewhere in the middle of EBB. */
5346 get_live_on_other_edges (curr_bb, last_processed_bb,
5347 &temp_bitmap);
5348 to_process = &temp_bitmap;
5350 last_processed_bb = curr_bb;
5351 last_insn = get_last_insertion_point (curr_bb);
5352 after_p = (! JUMP_P (last_insn)
5353 && (! CALL_P (last_insn)
5354 || (find_reg_note (last_insn,
5355 REG_NORETURN, NULL_RTX) == NULL_RTX
5356 && ! SIBLING_CALL_P (last_insn))));
5357 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5358 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5360 if ((int) j >= lra_constraint_new_regno_start)
5361 break;
5362 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5364 if (j < FIRST_PSEUDO_REGISTER)
5365 SET_HARD_REG_BIT (live_hard_regs, j);
5366 else
5367 add_to_hard_reg_set (&live_hard_regs,
5368 PSEUDO_REGNO_MODE (j),
5369 reg_renumber[j]);
5370 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5374 src_regno = dst_regno = -1;
5375 if (NONDEBUG_INSN_P (curr_insn)
5376 && (set = single_set (curr_insn)) != NULL_RTX
5377 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5379 src_regno = REGNO (SET_SRC (set));
5380 dst_regno = REGNO (SET_DEST (set));
5382 update_reloads_num_p = true;
5383 if (src_regno < lra_constraint_new_regno_start
5384 && src_regno >= FIRST_PSEUDO_REGISTER
5385 && reg_renumber[src_regno] < 0
5386 && dst_regno >= lra_constraint_new_regno_start
5387 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5389 /* 'reload_pseudo <- original_pseudo'. */
5390 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5391 reloads_num++;
5392 update_reloads_num_p = false;
5393 succ_p = false;
5394 if (usage_insns[src_regno].check == curr_usage_insns_check
5395 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5396 succ_p = inherit_reload_reg (false, src_regno, cl,
5397 curr_insn, next_usage_insns);
5398 if (succ_p)
5399 change_p = true;
5400 else
5401 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5402 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5403 IOR_HARD_REG_SET (potential_reload_hard_regs,
5404 reg_class_contents[cl]);
5406 else if (src_regno >= lra_constraint_new_regno_start
5407 && dst_regno < lra_constraint_new_regno_start
5408 && dst_regno >= FIRST_PSEUDO_REGISTER
5409 && reg_renumber[dst_regno] < 0
5410 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5411 && usage_insns[dst_regno].check == curr_usage_insns_check
5412 && (next_usage_insns
5413 = usage_insns[dst_regno].insns) != NULL_RTX)
5415 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5416 reloads_num++;
5417 update_reloads_num_p = false;
5418 /* 'original_pseudo <- reload_pseudo'. */
5419 if (! JUMP_P (curr_insn)
5420 && inherit_reload_reg (true, dst_regno, cl,
5421 curr_insn, next_usage_insns))
5422 change_p = true;
5423 /* Invalidate. */
5424 usage_insns[dst_regno].check = 0;
5425 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5426 IOR_HARD_REG_SET (potential_reload_hard_regs,
5427 reg_class_contents[cl]);
5429 else if (INSN_P (curr_insn))
5431 int iter;
5432 int max_uid = get_max_uid ();
5434 curr_id = lra_get_insn_recog_data (curr_insn);
5435 curr_static_id = curr_id->insn_static_data;
5436 to_inherit_num = 0;
5437 /* Process insn definitions. */
5438 for (iter = 0; iter < 2; iter++)
5439 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5440 reg != NULL;
5441 reg = reg->next)
5442 if (reg->type != OP_IN
5443 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5445 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5446 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5447 && usage_insns[dst_regno].check == curr_usage_insns_check
5448 && (next_usage_insns
5449 = usage_insns[dst_regno].insns) != NULL_RTX)
5451 struct lra_insn_reg *r;
5453 for (r = curr_id->regs; r != NULL; r = r->next)
5454 if (r->type != OP_OUT && r->regno == dst_regno)
5455 break;
5456 /* Don't do inheritance if the pseudo is also
5457 used in the insn. */
5458 if (r == NULL)
5459 /* We can not do inheritance right now
5460 because the current insn reg info (chain
5461 regs) can change after that. */
5462 add_to_inherit (dst_regno, next_usage_insns);
5464 /* We can not process one reg twice here because of
5465 usage_insns invalidation. */
5466 if ((dst_regno < FIRST_PSEUDO_REGISTER
5467 || reg_renumber[dst_regno] >= 0)
5468 && ! reg->subreg_p && reg->type != OP_IN)
5470 HARD_REG_SET s;
5472 if (split_if_necessary (dst_regno, reg->biggest_mode,
5473 potential_reload_hard_regs,
5474 false, curr_insn, max_uid))
5475 change_p = true;
5476 CLEAR_HARD_REG_SET (s);
5477 if (dst_regno < FIRST_PSEUDO_REGISTER)
5478 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5479 else
5480 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5481 reg_renumber[dst_regno]);
5482 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5484 /* We should invalidate potential inheritance or
5485 splitting for the current insn usages to the next
5486 usage insns (see code below) as the output pseudo
5487 prevents this. */
5488 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5489 && reg_renumber[dst_regno] < 0)
5490 || (reg->type == OP_OUT && ! reg->subreg_p
5491 && (dst_regno < FIRST_PSEUDO_REGISTER
5492 || reg_renumber[dst_regno] >= 0)))
5494 /* Invalidate and mark definitions. */
5495 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5496 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5497 else
5499 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5500 for (i = 0; i < nregs; i++)
5501 usage_insns[dst_regno + i].check
5502 = -(int) INSN_UID (curr_insn);
5506 if (! JUMP_P (curr_insn))
5507 for (i = 0; i < to_inherit_num; i++)
5508 if (inherit_reload_reg (true, to_inherit[i].regno,
5509 ALL_REGS, curr_insn,
5510 to_inherit[i].insns))
5511 change_p = true;
5512 if (CALL_P (curr_insn))
5514 rtx cheap, pat, dest;
5515 rtx_insn *restore;
5516 int regno, hard_regno;
5518 calls_num++;
5519 if ((cheap = find_reg_note (curr_insn,
5520 REG_RETURNED, NULL_RTX)) != NULL_RTX
5521 && ((cheap = XEXP (cheap, 0)), true)
5522 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5523 && (hard_regno = reg_renumber[regno]) >= 0
5524 /* If there are pending saves/restores, the
5525 optimization is not worth. */
5526 && usage_insns[regno].calls_num == calls_num - 1
5527 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5529 /* Restore the pseudo from the call result as
5530 REG_RETURNED note says that the pseudo value is
5531 in the call result and the pseudo is an argument
5532 of the call. */
5533 pat = PATTERN (curr_insn);
5534 if (GET_CODE (pat) == PARALLEL)
5535 pat = XVECEXP (pat, 0, 0);
5536 dest = SET_DEST (pat);
5537 /* For multiple return values dest is PARALLEL.
5538 Currently we handle only single return value case. */
5539 if (REG_P (dest))
5541 start_sequence ();
5542 emit_move_insn (cheap, copy_rtx (dest));
5543 restore = get_insns ();
5544 end_sequence ();
5545 lra_process_new_insns (curr_insn, NULL, restore,
5546 "Inserting call parameter restore");
5547 /* We don't need to save/restore of the pseudo from
5548 this call. */
5549 usage_insns[regno].calls_num = calls_num;
5550 bitmap_set_bit (&check_only_regs, regno);
5554 to_inherit_num = 0;
5555 /* Process insn usages. */
5556 for (iter = 0; iter < 2; iter++)
5557 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5558 reg != NULL;
5559 reg = reg->next)
5560 if ((reg->type != OP_OUT
5561 || (reg->type == OP_OUT && reg->subreg_p))
5562 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5564 if (src_regno >= FIRST_PSEUDO_REGISTER
5565 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5567 if (usage_insns[src_regno].check == curr_usage_insns_check
5568 && (next_usage_insns
5569 = usage_insns[src_regno].insns) != NULL_RTX
5570 && NONDEBUG_INSN_P (curr_insn))
5571 add_to_inherit (src_regno, next_usage_insns);
5572 else if (usage_insns[src_regno].check
5573 != -(int) INSN_UID (curr_insn))
5574 /* Add usages but only if the reg is not set up
5575 in the same insn. */
5576 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5578 else if (src_regno < FIRST_PSEUDO_REGISTER
5579 || reg_renumber[src_regno] >= 0)
5581 bool before_p;
5582 rtx use_insn = curr_insn;
5584 before_p = (JUMP_P (curr_insn)
5585 || (CALL_P (curr_insn) && reg->type == OP_IN));
5586 if (NONDEBUG_INSN_P (curr_insn)
5587 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5588 && split_if_necessary (src_regno, reg->biggest_mode,
5589 potential_reload_hard_regs,
5590 before_p, curr_insn, max_uid))
5592 if (reg->subreg_p)
5593 lra_risky_transformations_p = true;
5594 change_p = true;
5595 /* Invalidate. */
5596 usage_insns[src_regno].check = 0;
5597 if (before_p)
5598 use_insn = PREV_INSN (curr_insn);
5600 if (NONDEBUG_INSN_P (curr_insn))
5602 if (src_regno < FIRST_PSEUDO_REGISTER)
5603 add_to_hard_reg_set (&live_hard_regs,
5604 reg->biggest_mode, src_regno);
5605 else
5606 add_to_hard_reg_set (&live_hard_regs,
5607 PSEUDO_REGNO_MODE (src_regno),
5608 reg_renumber[src_regno]);
5610 add_next_usage_insn (src_regno, use_insn, reloads_num);
5613 /* Process call args. */
5614 if (curr_id->arg_hard_regs != NULL)
5615 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5616 if (src_regno < FIRST_PSEUDO_REGISTER)
5618 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5619 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5621 for (i = 0; i < to_inherit_num; i++)
5623 src_regno = to_inherit[i].regno;
5624 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5625 curr_insn, to_inherit[i].insns))
5626 change_p = true;
5627 else
5628 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5631 if (update_reloads_num_p
5632 && NONDEBUG_INSN_P (curr_insn)
5633 && (set = single_set (curr_insn)) != NULL_RTX)
5635 int regno = -1;
5636 if ((REG_P (SET_DEST (set))
5637 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5638 && reg_renumber[regno] < 0
5639 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5640 || (REG_P (SET_SRC (set))
5641 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5642 && reg_renumber[regno] < 0
5643 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5645 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5646 reloads_num++;
5647 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5648 IOR_HARD_REG_SET (potential_reload_hard_regs,
5649 reg_class_contents[cl]);
5652 /* We reached the start of the current basic block. */
5653 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5654 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5656 /* We reached the beginning of the current block -- do
5657 rest of spliting in the current BB. */
5658 to_process = df_get_live_in (curr_bb);
5659 if (BLOCK_FOR_INSN (head) != curr_bb)
5661 /* We are somewhere in the middle of EBB. */
5662 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5663 curr_bb, &temp_bitmap);
5664 to_process = &temp_bitmap;
5666 head_p = true;
5667 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5669 if ((int) j >= lra_constraint_new_regno_start)
5670 break;
5671 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5672 && usage_insns[j].check == curr_usage_insns_check
5673 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5675 if (need_for_split_p (potential_reload_hard_regs, j))
5677 if (lra_dump_file != NULL && head_p)
5679 fprintf (lra_dump_file,
5680 " ----------------------------------\n");
5681 head_p = false;
5683 if (split_reg (false, j, bb_note (curr_bb),
5684 next_usage_insns))
5685 change_p = true;
5687 usage_insns[j].check = 0;
5692 return change_p;
5695 /* This value affects EBB forming. If probability of edge from EBB to
5696 a BB is not greater than the following value, we don't add the BB
5697 to EBB. */
5698 #define EBB_PROBABILITY_CUTOFF \
5699 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
5701 /* Current number of inheritance/split iteration. */
5702 int lra_inheritance_iter;
5704 /* Entry function for inheritance/split pass. */
5705 void
5706 lra_inheritance (void)
5708 int i;
5709 basic_block bb, start_bb;
5710 edge e;
5712 lra_inheritance_iter++;
5713 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5714 return;
5715 timevar_push (TV_LRA_INHERITANCE);
5716 if (lra_dump_file != NULL)
5717 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5718 lra_inheritance_iter);
5719 curr_usage_insns_check = 0;
5720 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5721 for (i = 0; i < lra_constraint_new_regno_start; i++)
5722 usage_insns[i].check = 0;
5723 bitmap_initialize (&check_only_regs, &reg_obstack);
5724 bitmap_initialize (&live_regs, &reg_obstack);
5725 bitmap_initialize (&temp_bitmap, &reg_obstack);
5726 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5727 FOR_EACH_BB_FN (bb, cfun)
5729 start_bb = bb;
5730 if (lra_dump_file != NULL)
5731 fprintf (lra_dump_file, "EBB");
5732 /* Form a EBB starting with BB. */
5733 bitmap_clear (&ebb_global_regs);
5734 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5735 for (;;)
5737 if (lra_dump_file != NULL)
5738 fprintf (lra_dump_file, " %d", bb->index);
5739 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5740 || LABEL_P (BB_HEAD (bb->next_bb)))
5741 break;
5742 e = find_fallthru_edge (bb->succs);
5743 if (! e)
5744 break;
5745 if (e->probability < EBB_PROBABILITY_CUTOFF)
5746 break;
5747 bb = bb->next_bb;
5749 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5750 if (lra_dump_file != NULL)
5751 fprintf (lra_dump_file, "\n");
5752 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5753 /* Remember that the EBB head and tail can change in
5754 inherit_in_ebb. */
5755 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5757 bitmap_clear (&ebb_global_regs);
5758 bitmap_clear (&temp_bitmap);
5759 bitmap_clear (&live_regs);
5760 bitmap_clear (&check_only_regs);
5761 free (usage_insns);
5763 timevar_pop (TV_LRA_INHERITANCE);
5768 /* This page contains code to undo failed inheritance/split
5769 transformations. */
5771 /* Current number of iteration undoing inheritance/split. */
5772 int lra_undo_inheritance_iter;
5774 /* Fix BB live info LIVE after removing pseudos created on pass doing
5775 inheritance/split which are REMOVED_PSEUDOS. */
5776 static void
5777 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5779 unsigned int regno;
5780 bitmap_iterator bi;
5782 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5783 if (bitmap_clear_bit (live, regno))
5784 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5787 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5788 number. */
5789 static int
5790 get_regno (rtx reg)
5792 if (GET_CODE (reg) == SUBREG)
5793 reg = SUBREG_REG (reg);
5794 if (REG_P (reg))
5795 return REGNO (reg);
5796 return -1;
5799 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5800 return true if we did any change. The undo transformations for
5801 inheritance looks like
5802 i <- i2
5803 p <- i => p <- i2
5804 or removing
5805 p <- i, i <- p, and i <- i3
5806 where p is original pseudo from which inheritance pseudo i was
5807 created, i and i3 are removed inheritance pseudos, i2 is another
5808 not removed inheritance pseudo. All split pseudos or other
5809 occurrences of removed inheritance pseudos are changed on the
5810 corresponding original pseudos.
5812 The function also schedules insns changed and created during
5813 inheritance/split pass for processing by the subsequent constraint
5814 pass. */
5815 static bool
5816 remove_inheritance_pseudos (bitmap remove_pseudos)
5818 basic_block bb;
5819 int regno, sregno, prev_sregno, dregno, restore_regno;
5820 rtx set, prev_set;
5821 rtx_insn *prev_insn;
5822 bool change_p, done_p;
5824 change_p = ! bitmap_empty_p (remove_pseudos);
5825 /* We can not finish the function right away if CHANGE_P is true
5826 because we need to marks insns affected by previous
5827 inheritance/split pass for processing by the subsequent
5828 constraint pass. */
5829 FOR_EACH_BB_FN (bb, cfun)
5831 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5832 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5833 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5835 if (! INSN_P (curr_insn))
5836 continue;
5837 done_p = false;
5838 sregno = dregno = -1;
5839 if (change_p && NONDEBUG_INSN_P (curr_insn)
5840 && (set = single_set (curr_insn)) != NULL_RTX)
5842 dregno = get_regno (SET_DEST (set));
5843 sregno = get_regno (SET_SRC (set));
5846 if (sregno >= 0 && dregno >= 0)
5848 if ((bitmap_bit_p (remove_pseudos, sregno)
5849 && (lra_reg_info[sregno].restore_regno == dregno
5850 || (bitmap_bit_p (remove_pseudos, dregno)
5851 && (lra_reg_info[sregno].restore_regno
5852 == lra_reg_info[dregno].restore_regno))))
5853 || (bitmap_bit_p (remove_pseudos, dregno)
5854 && lra_reg_info[dregno].restore_regno == sregno))
5855 /* One of the following cases:
5856 original <- removed inheritance pseudo
5857 removed inherit pseudo <- another removed inherit pseudo
5858 removed inherit pseudo <- original pseudo
5860 removed_split_pseudo <- original_reg
5861 original_reg <- removed_split_pseudo */
5863 if (lra_dump_file != NULL)
5865 fprintf (lra_dump_file, " Removing %s:\n",
5866 bitmap_bit_p (&lra_split_regs, sregno)
5867 || bitmap_bit_p (&lra_split_regs, dregno)
5868 ? "split" : "inheritance");
5869 dump_insn_slim (lra_dump_file, curr_insn);
5871 lra_set_insn_deleted (curr_insn);
5872 done_p = true;
5874 else if (bitmap_bit_p (remove_pseudos, sregno)
5875 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5877 /* Search the following pattern:
5878 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5879 original_pseudo <- inherit_or_split_pseudo1
5880 where the 2nd insn is the current insn and
5881 inherit_or_split_pseudo2 is not removed. If it is found,
5882 change the current insn onto:
5883 original_pseudo <- inherit_or_split_pseudo2. */
5884 for (prev_insn = PREV_INSN (curr_insn);
5885 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5886 prev_insn = PREV_INSN (prev_insn))
5888 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5889 && (prev_set = single_set (prev_insn)) != NULL_RTX
5890 /* There should be no subregs in insn we are
5891 searching because only the original reg might
5892 be in subreg when we changed the mode of
5893 load/store for splitting. */
5894 && REG_P (SET_DEST (prev_set))
5895 && REG_P (SET_SRC (prev_set))
5896 && (int) REGNO (SET_DEST (prev_set)) == sregno
5897 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5898 >= FIRST_PSEUDO_REGISTER)
5899 /* As we consider chain of inheritance or
5900 splitting described in above comment we should
5901 check that sregno and prev_sregno were
5902 inheritance/split pseudos created from the
5903 same original regno. */
5904 && (lra_reg_info[sregno].restore_regno
5905 == lra_reg_info[prev_sregno].restore_regno)
5906 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5908 lra_assert (GET_MODE (SET_SRC (prev_set))
5909 == GET_MODE (regno_reg_rtx[sregno]));
5910 if (GET_CODE (SET_SRC (set)) == SUBREG)
5911 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5912 else
5913 SET_SRC (set) = SET_SRC (prev_set);
5914 /* As we are finishing with processing the insn
5915 here, check the destination too as it might
5916 inheritance pseudo for another pseudo. */
5917 if (bitmap_bit_p (remove_pseudos, dregno)
5918 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
5919 && (restore_regno
5920 = lra_reg_info[dregno].restore_regno) >= 0)
5922 if (GET_CODE (SET_DEST (set)) == SUBREG)
5923 SUBREG_REG (SET_DEST (set))
5924 = regno_reg_rtx[restore_regno];
5925 else
5926 SET_DEST (set) = regno_reg_rtx[restore_regno];
5928 lra_push_insn_and_update_insn_regno_info (curr_insn);
5929 lra_set_used_insn_alternative_by_uid
5930 (INSN_UID (curr_insn), -1);
5931 done_p = true;
5932 if (lra_dump_file != NULL)
5934 fprintf (lra_dump_file, " Change reload insn:\n");
5935 dump_insn_slim (lra_dump_file, curr_insn);
5940 if (! done_p)
5942 struct lra_insn_reg *reg;
5943 bool restored_regs_p = false;
5944 bool kept_regs_p = false;
5946 curr_id = lra_get_insn_recog_data (curr_insn);
5947 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5949 regno = reg->regno;
5950 restore_regno = lra_reg_info[regno].restore_regno;
5951 if (restore_regno >= 0)
5953 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5955 lra_substitute_pseudo_within_insn (
5956 curr_insn, regno, regno_reg_rtx[restore_regno]);
5957 restored_regs_p = true;
5959 else
5960 kept_regs_p = true;
5963 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5965 /* The instruction has changed since the previous
5966 constraints pass. */
5967 lra_push_insn_and_update_insn_regno_info (curr_insn);
5968 lra_set_used_insn_alternative_by_uid
5969 (INSN_UID (curr_insn), -1);
5971 else if (restored_regs_p)
5972 /* The instruction has been restored to the form that
5973 it had during the previous constraints pass. */
5974 lra_update_insn_regno_info (curr_insn);
5975 if (restored_regs_p && lra_dump_file != NULL)
5977 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5978 dump_insn_slim (lra_dump_file, curr_insn);
5983 return change_p;
5986 /* If optional reload pseudos failed to get a hard register or was not
5987 inherited, it is better to remove optional reloads. We do this
5988 transformation after undoing inheritance to figure out necessity to
5989 remove optional reloads easier. Return true if we do any
5990 change. */
5991 static bool
5992 undo_optional_reloads (void)
5994 bool change_p, keep_p;
5995 unsigned int regno, uid;
5996 bitmap_iterator bi, bi2;
5997 rtx_insn *insn;
5998 rtx set, src, dest;
5999 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
6001 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
6002 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
6003 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6005 keep_p = false;
6006 /* Keep optional reloads from previous subpasses. */
6007 if (lra_reg_info[regno].restore_regno < 0
6008 /* If the original pseudo changed its allocation, just
6009 removing the optional pseudo is dangerous as the original
6010 pseudo will have longer live range. */
6011 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
6012 keep_p = true;
6013 else if (reg_renumber[regno] >= 0)
6014 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
6016 insn = lra_insn_recog_data[uid]->insn;
6017 if ((set = single_set (insn)) == NULL_RTX)
6018 continue;
6019 src = SET_SRC (set);
6020 dest = SET_DEST (set);
6021 if (! REG_P (src) || ! REG_P (dest))
6022 continue;
6023 if (REGNO (dest) == regno
6024 /* Ignore insn for optional reloads itself. */
6025 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
6026 /* Check only inheritance on last inheritance pass. */
6027 && (int) REGNO (src) >= new_regno_start
6028 /* Check that the optional reload was inherited. */
6029 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6031 keep_p = true;
6032 break;
6035 if (keep_p)
6037 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6038 if (lra_dump_file != NULL)
6039 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6042 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6043 bitmap_initialize (&insn_bitmap, &reg_obstack);
6044 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6046 if (lra_dump_file != NULL)
6047 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6048 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6049 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6051 insn = lra_insn_recog_data[uid]->insn;
6052 if ((set = single_set (insn)) != NULL_RTX)
6054 src = SET_SRC (set);
6055 dest = SET_DEST (set);
6056 if (REG_P (src) && REG_P (dest)
6057 && ((REGNO (src) == regno
6058 && (lra_reg_info[regno].restore_regno
6059 == (int) REGNO (dest)))
6060 || (REGNO (dest) == regno
6061 && (lra_reg_info[regno].restore_regno
6062 == (int) REGNO (src)))))
6064 if (lra_dump_file != NULL)
6066 fprintf (lra_dump_file, " Deleting move %u\n",
6067 INSN_UID (insn));
6068 dump_insn_slim (lra_dump_file, insn);
6070 lra_set_insn_deleted (insn);
6071 continue;
6073 /* We should not worry about generation memory-memory
6074 moves here as if the corresponding inheritance did
6075 not work (inheritance pseudo did not get a hard reg),
6076 we remove the inheritance pseudo and the optional
6077 reload. */
6079 lra_substitute_pseudo_within_insn (
6080 insn, regno,
6081 regno_reg_rtx[lra_reg_info[regno].restore_regno]);
6082 lra_update_insn_regno_info (insn);
6083 if (lra_dump_file != NULL)
6085 fprintf (lra_dump_file,
6086 " Restoring original insn:\n");
6087 dump_insn_slim (lra_dump_file, insn);
6091 /* Clear restore_regnos. */
6092 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6093 lra_reg_info[regno].restore_regno = -1;
6094 bitmap_clear (&insn_bitmap);
6095 bitmap_clear (&removed_optional_reload_pseudos);
6096 return change_p;
6099 /* Entry function for undoing inheritance/split transformation. Return true
6100 if we did any RTL change in this pass. */
6101 bool
6102 lra_undo_inheritance (void)
6104 unsigned int regno;
6105 int restore_regno, hard_regno;
6106 int n_all_inherit, n_inherit, n_all_split, n_split;
6107 bitmap_head remove_pseudos;
6108 bitmap_iterator bi;
6109 bool change_p;
6111 lra_undo_inheritance_iter++;
6112 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6113 return false;
6114 if (lra_dump_file != NULL)
6115 fprintf (lra_dump_file,
6116 "\n********** Undoing inheritance #%d: **********\n\n",
6117 lra_undo_inheritance_iter);
6118 bitmap_initialize (&remove_pseudos, &reg_obstack);
6119 n_inherit = n_all_inherit = 0;
6120 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6121 if (lra_reg_info[regno].restore_regno >= 0)
6123 n_all_inherit++;
6124 if (reg_renumber[regno] < 0
6125 /* If the original pseudo changed its allocation, just
6126 removing inheritance is dangerous as for changing
6127 allocation we used shorter live-ranges. */
6128 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6129 bitmap_set_bit (&remove_pseudos, regno);
6130 else
6131 n_inherit++;
6133 if (lra_dump_file != NULL && n_all_inherit != 0)
6134 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6135 n_inherit, n_all_inherit,
6136 (double) n_inherit / n_all_inherit * 100);
6137 n_split = n_all_split = 0;
6138 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6139 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6141 n_all_split++;
6142 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6143 ? reg_renumber[restore_regno] : restore_regno);
6144 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6145 bitmap_set_bit (&remove_pseudos, regno);
6146 else
6148 n_split++;
6149 if (lra_dump_file != NULL)
6150 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6151 regno, restore_regno);
6154 if (lra_dump_file != NULL && n_all_split != 0)
6155 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6156 n_split, n_all_split,
6157 (double) n_split / n_all_split * 100);
6158 change_p = remove_inheritance_pseudos (&remove_pseudos);
6159 bitmap_clear (&remove_pseudos);
6160 /* Clear restore_regnos. */
6161 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6162 lra_reg_info[regno].restore_regno = -1;
6163 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6164 lra_reg_info[regno].restore_regno = -1;
6165 change_p = undo_optional_reloads () || change_p;
6166 return change_p;