2015-05-22 Pascal Obry <obry@adacore.com>
[official-gcc.git] / gcc / lra-constraints.c
blobc0f29956edddc8b3f0b3fe6ac4b9f89c4db9d117
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 false, false, 0, 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 && REG_P (y))
753 return REGNO (x) == REGNO (y);
755 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
756 && x == SUBREG_REG (y))
757 return true;
758 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
759 && SUBREG_REG (x) == y)
760 return true;
762 /* Now we have disposed of all the cases in which different rtx
763 codes can match. */
764 if (code != GET_CODE (y))
765 return false;
767 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
768 if (GET_MODE (x) != GET_MODE (y))
769 return false;
771 switch (code)
773 CASE_CONST_UNIQUE:
774 return false;
776 case LABEL_REF:
777 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
778 case SYMBOL_REF:
779 return XSTR (x, 0) == XSTR (y, 0);
781 default:
782 break;
785 /* Compare the elements. If any pair of corresponding elements fail
786 to match, return false for the whole things. */
788 fmt = GET_RTX_FORMAT (code);
789 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
791 int val, j;
792 switch (fmt[i])
794 case 'w':
795 if (XWINT (x, i) != XWINT (y, i))
796 return false;
797 break;
799 case 'i':
800 if (XINT (x, i) != XINT (y, i))
801 return false;
802 break;
804 case 'e':
805 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
806 if (val == 0)
807 return false;
808 break;
810 case '0':
811 break;
813 case 'E':
814 if (XVECLEN (x, i) != XVECLEN (y, i))
815 return false;
816 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
818 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
819 if (val == 0)
820 return false;
822 break;
824 /* It is believed that rtx's at this level will never
825 contain anything but integers and other rtx's, except for
826 within LABEL_REFs and SYMBOL_REFs. */
827 default:
828 gcc_unreachable ();
831 return true;
834 /* True if X is a constant that can be forced into the constant pool.
835 MODE is the mode of the operand, or VOIDmode if not known. */
836 #define CONST_POOL_OK_P(MODE, X) \
837 ((MODE) != VOIDmode \
838 && CONSTANT_P (X) \
839 && GET_CODE (X) != HIGH \
840 && !targetm.cannot_force_const_mem (MODE, X))
842 /* True if C is a non-empty register class that has too few registers
843 to be safely used as a reload target class. */
844 #define SMALL_REGISTER_CLASS_P(C) \
845 (ira_class_hard_regs_num [(C)] == 1 \
846 || (ira_class_hard_regs_num [(C)] >= 1 \
847 && targetm.class_likely_spilled_p (C)))
849 /* If REG is a reload pseudo, try to make its class satisfying CL. */
850 static void
851 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
853 enum reg_class rclass;
855 /* Do not make more accurate class from reloads generated. They are
856 mostly moves with a lot of constraints. Making more accurate
857 class may results in very narrow class and impossibility of find
858 registers for several reloads of one insn. */
859 if (INSN_UID (curr_insn) >= new_insn_uid_start)
860 return;
861 if (GET_CODE (reg) == SUBREG)
862 reg = SUBREG_REG (reg);
863 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
864 return;
865 if (in_class_p (reg, cl, &rclass) && rclass != cl)
866 lra_change_class (REGNO (reg), rclass, " Change to", true);
869 /* Generate reloads for matching OUT and INS (array of input operand
870 numbers with end marker -1) with reg class GOAL_CLASS. Add input
871 and output reloads correspondingly to the lists *BEFORE and *AFTER.
872 OUT might be negative. In this case we generate input reloads for
873 matched input operands INS. */
874 static void
875 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
876 rtx_insn **before, rtx_insn **after)
878 int i, in;
879 rtx new_in_reg, new_out_reg, reg, clobber;
880 machine_mode inmode, outmode;
881 rtx in_rtx = *curr_id->operand_loc[ins[0]];
882 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
884 inmode = curr_operand_mode[ins[0]];
885 outmode = out < 0 ? inmode : curr_operand_mode[out];
886 push_to_sequence (*before);
887 if (inmode != outmode)
889 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
891 reg = new_in_reg
892 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
893 goal_class, "");
894 if (SCALAR_INT_MODE_P (inmode))
895 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
896 else
897 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
898 LRA_SUBREG_P (new_out_reg) = 1;
899 /* If the input reg is dying here, we can use the same hard
900 register for REG and IN_RTX. We do it only for original
901 pseudos as reload pseudos can die although original
902 pseudos still live where reload pseudos dies. */
903 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
904 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
905 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
907 else
909 reg = new_out_reg
910 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
911 goal_class, "");
912 if (SCALAR_INT_MODE_P (outmode))
913 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
914 else
915 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
916 /* NEW_IN_REG is non-paradoxical subreg. We don't want
917 NEW_OUT_REG living above. We add clobber clause for
918 this. This is just a temporary clobber. We can remove
919 it at the end of LRA work. */
920 clobber = emit_clobber (new_out_reg);
921 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
922 LRA_SUBREG_P (new_in_reg) = 1;
923 if (GET_CODE (in_rtx) == SUBREG)
925 rtx subreg_reg = SUBREG_REG (in_rtx);
927 /* If SUBREG_REG is dying here and sub-registers IN_RTX
928 and NEW_IN_REG are similar, we can use the same hard
929 register for REG and SUBREG_REG. */
930 if (REG_P (subreg_reg)
931 && (int) REGNO (subreg_reg) < lra_new_regno_start
932 && GET_MODE (subreg_reg) == outmode
933 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
934 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
935 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
939 else
941 /* Pseudos have values -- see comments for lra_reg_info.
942 Different pseudos with the same value do not conflict even if
943 they live in the same place. When we create a pseudo we
944 assign value of original pseudo (if any) from which we
945 created the new pseudo. If we create the pseudo from the
946 input pseudo, the new pseudo will no conflict with the input
947 pseudo which is wrong when the input pseudo lives after the
948 insn and as the new pseudo value is changed by the insn
949 output. Therefore we create the new pseudo from the output.
951 We cannot reuse the current output register because we might
952 have a situation like "a <- a op b", where the constraints
953 force the second input operand ("b") to match the output
954 operand ("a"). "b" must then be copied into a new register
955 so that it doesn't clobber the current value of "a". */
957 new_in_reg = new_out_reg
958 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
959 goal_class, "");
961 /* In operand can be got from transformations before processing insn
962 constraints. One example of such transformations is subreg
963 reloading (see function simplify_operand_subreg). The new
964 pseudos created by the transformations might have inaccurate
965 class (ALL_REGS) and we should make their classes more
966 accurate. */
967 narrow_reload_pseudo_class (in_rtx, goal_class);
968 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
969 *before = get_insns ();
970 end_sequence ();
971 for (i = 0; (in = ins[i]) >= 0; i++)
973 lra_assert
974 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
975 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
976 *curr_id->operand_loc[in] = new_in_reg;
978 lra_update_dups (curr_id, ins);
979 if (out < 0)
980 return;
981 /* See a comment for the input operand above. */
982 narrow_reload_pseudo_class (out_rtx, goal_class);
983 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
985 start_sequence ();
986 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
987 emit_insn (*after);
988 *after = get_insns ();
989 end_sequence ();
991 *curr_id->operand_loc[out] = new_out_reg;
992 lra_update_dup (curr_id, out);
995 /* Return register class which is union of all reg classes in insn
996 constraint alternative string starting with P. */
997 static enum reg_class
998 reg_class_from_constraints (const char *p)
1000 int c, len;
1001 enum reg_class op_class = NO_REGS;
1004 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1006 case '#':
1007 case ',':
1008 return op_class;
1010 case 'g':
1011 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1012 break;
1014 default:
1015 enum constraint_num cn = lookup_constraint (p);
1016 enum reg_class cl = reg_class_for_constraint (cn);
1017 if (cl == NO_REGS)
1019 if (insn_extra_address_constraint (cn))
1020 op_class
1021 = (reg_class_subunion
1022 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1023 ADDRESS, SCRATCH)]);
1024 break;
1027 op_class = reg_class_subunion[op_class][cl];
1028 break;
1030 while ((p += len), c);
1031 return op_class;
1034 /* If OP is a register, return the class of the register as per
1035 get_reg_class, otherwise return NO_REGS. */
1036 static inline enum reg_class
1037 get_op_class (rtx op)
1039 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1042 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1043 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1044 SUBREG for VAL to make them equal. */
1045 static rtx_insn *
1046 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1048 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1050 /* Usually size of mem_pseudo is greater than val size but in
1051 rare cases it can be less as it can be defined by target
1052 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1053 if (! MEM_P (val))
1055 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1056 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1058 LRA_SUBREG_P (val) = 1;
1060 else
1062 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1063 LRA_SUBREG_P (mem_pseudo) = 1;
1066 return to_p ? gen_move_insn (mem_pseudo, val)
1067 : gen_move_insn (val, mem_pseudo);
1070 /* Process a special case insn (register move), return true if we
1071 don't need to process it anymore. INSN should be a single set
1072 insn. Set up that RTL was changed through CHANGE_P and macro
1073 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1074 SEC_MEM_P. */
1075 static bool
1076 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1078 int sregno, dregno;
1079 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1080 rtx_insn *before;
1081 enum reg_class dclass, sclass, secondary_class;
1082 secondary_reload_info sri;
1084 lra_assert (curr_insn_set != NULL_RTX);
1085 dreg = dest = SET_DEST (curr_insn_set);
1086 sreg = src = SET_SRC (curr_insn_set);
1087 if (GET_CODE (dest) == SUBREG)
1088 dreg = SUBREG_REG (dest);
1089 if (GET_CODE (src) == SUBREG)
1090 sreg = SUBREG_REG (src);
1091 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1092 return false;
1093 sclass = dclass = NO_REGS;
1094 if (REG_P (dreg))
1095 dclass = get_reg_class (REGNO (dreg));
1096 if (dclass == ALL_REGS)
1097 /* ALL_REGS is used for new pseudos created by transformations
1098 like reload of SUBREG_REG (see function
1099 simplify_operand_subreg). We don't know their class yet. We
1100 should figure out the class from processing the insn
1101 constraints not in this fast path function. Even if ALL_REGS
1102 were a right class for the pseudo, secondary_... hooks usually
1103 are not define for ALL_REGS. */
1104 return false;
1105 if (REG_P (sreg))
1106 sclass = get_reg_class (REGNO (sreg));
1107 if (sclass == ALL_REGS)
1108 /* See comments above. */
1109 return false;
1110 if (sclass == NO_REGS && dclass == NO_REGS)
1111 return false;
1112 #ifdef SECONDARY_MEMORY_NEEDED
1113 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1114 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1115 && ((sclass != NO_REGS && dclass != NO_REGS)
1116 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1117 #endif
1120 *sec_mem_p = true;
1121 return false;
1123 #endif
1124 if (! REG_P (dreg) || ! REG_P (sreg))
1125 return false;
1126 sri.prev_sri = NULL;
1127 sri.icode = CODE_FOR_nothing;
1128 sri.extra_cost = 0;
1129 secondary_class = NO_REGS;
1130 /* Set up hard register for a reload pseudo for hook
1131 secondary_reload because some targets just ignore unassigned
1132 pseudos in the hook. */
1133 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1135 dregno = REGNO (dreg);
1136 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1138 else
1139 dregno = -1;
1140 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1142 sregno = REGNO (sreg);
1143 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1145 else
1146 sregno = -1;
1147 if (sclass != NO_REGS)
1148 secondary_class
1149 = (enum reg_class) targetm.secondary_reload (false, dest,
1150 (reg_class_t) sclass,
1151 GET_MODE (src), &sri);
1152 if (sclass == NO_REGS
1153 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1154 && dclass != NO_REGS))
1156 enum reg_class old_sclass = secondary_class;
1157 secondary_reload_info old_sri = sri;
1159 sri.prev_sri = NULL;
1160 sri.icode = CODE_FOR_nothing;
1161 sri.extra_cost = 0;
1162 secondary_class
1163 = (enum reg_class) targetm.secondary_reload (true, src,
1164 (reg_class_t) dclass,
1165 GET_MODE (src), &sri);
1166 /* Check the target hook consistency. */
1167 lra_assert
1168 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1169 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1170 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1172 if (sregno >= 0)
1173 reg_renumber [sregno] = -1;
1174 if (dregno >= 0)
1175 reg_renumber [dregno] = -1;
1176 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1177 return false;
1178 *change_p = true;
1179 new_reg = NULL_RTX;
1180 if (secondary_class != NO_REGS)
1181 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1182 secondary_class,
1183 "secondary");
1184 start_sequence ();
1185 if (sri.icode == CODE_FOR_nothing)
1186 lra_emit_move (new_reg, src);
1187 else
1189 enum reg_class scratch_class;
1191 scratch_class = (reg_class_from_constraints
1192 (insn_data[sri.icode].operand[2].constraint));
1193 scratch_reg = (lra_create_new_reg_with_unique_value
1194 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1195 scratch_class, "scratch"));
1196 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1197 src, scratch_reg));
1199 before = get_insns ();
1200 end_sequence ();
1201 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1202 if (new_reg != NULL_RTX)
1203 SET_SRC (curr_insn_set) = new_reg;
1204 else
1206 if (lra_dump_file != NULL)
1208 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1209 dump_insn_slim (lra_dump_file, curr_insn);
1211 lra_set_insn_deleted (curr_insn);
1212 return true;
1214 return false;
1217 /* The following data describe the result of process_alt_operands.
1218 The data are used in curr_insn_transform to generate reloads. */
1220 /* The chosen reg classes which should be used for the corresponding
1221 operands. */
1222 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1223 /* True if the operand should be the same as another operand and that
1224 other operand does not need a reload. */
1225 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1226 /* True if the operand does not need a reload. */
1227 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1228 /* True if the operand can be offsetable memory. */
1229 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1230 /* The number of an operand to which given operand can be matched to. */
1231 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1232 /* The number of elements in the following array. */
1233 static int goal_alt_dont_inherit_ops_num;
1234 /* Numbers of operands whose reload pseudos should not be inherited. */
1235 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1236 /* True if the insn commutative operands should be swapped. */
1237 static bool goal_alt_swapped;
1238 /* The chosen insn alternative. */
1239 static int goal_alt_number;
1241 /* The following five variables are used to choose the best insn
1242 alternative. They reflect final characteristics of the best
1243 alternative. */
1245 /* Number of necessary reloads and overall cost reflecting the
1246 previous value and other unpleasantness of the best alternative. */
1247 static int best_losers, best_overall;
1248 /* Overall number hard registers used for reloads. For example, on
1249 some targets we need 2 general registers to reload DFmode and only
1250 one floating point register. */
1251 static int best_reload_nregs;
1252 /* Overall number reflecting distances of previous reloading the same
1253 value. The distances are counted from the current BB start. It is
1254 used to improve inheritance chances. */
1255 static int best_reload_sum;
1257 /* True if the current insn should have no correspondingly input or
1258 output reloads. */
1259 static bool no_input_reloads_p, no_output_reloads_p;
1261 /* True if we swapped the commutative operands in the current
1262 insn. */
1263 static int curr_swapped;
1265 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1266 register of class CL. Add any input reloads to list BEFORE. AFTER
1267 is nonnull if *LOC is an automodified value; handle that case by
1268 adding the required output reloads to list AFTER. Return true if
1269 the RTL was changed.
1271 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1272 register. Return false if the address register is correct. */
1273 static bool
1274 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1275 enum reg_class cl)
1277 int regno;
1278 enum reg_class rclass, new_class;
1279 rtx reg;
1280 rtx new_reg;
1281 machine_mode mode;
1282 bool subreg_p, before_p = false;
1284 subreg_p = GET_CODE (*loc) == SUBREG;
1285 if (subreg_p)
1286 loc = &SUBREG_REG (*loc);
1287 reg = *loc;
1288 mode = GET_MODE (reg);
1289 if (! REG_P (reg))
1291 if (check_only_p)
1292 return true;
1293 /* Always reload memory in an address even if the target supports
1294 such addresses. */
1295 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1296 before_p = true;
1298 else
1300 regno = REGNO (reg);
1301 rclass = get_reg_class (regno);
1302 if (! check_only_p
1303 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1305 if (lra_dump_file != NULL)
1307 fprintf (lra_dump_file,
1308 "Changing pseudo %d in address of insn %u on equiv ",
1309 REGNO (reg), INSN_UID (curr_insn));
1310 dump_value_slim (lra_dump_file, *loc, 1);
1311 fprintf (lra_dump_file, "\n");
1313 *loc = copy_rtx (*loc);
1315 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1317 if (check_only_p)
1318 return true;
1319 reg = *loc;
1320 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1321 mode, reg, cl, subreg_p, "address", &new_reg))
1322 before_p = true;
1324 else if (new_class != NO_REGS && rclass != new_class)
1326 if (check_only_p)
1327 return true;
1328 lra_change_class (regno, new_class, " Change to", true);
1329 return false;
1331 else
1332 return false;
1334 if (before_p)
1336 push_to_sequence (*before);
1337 lra_emit_move (new_reg, reg);
1338 *before = get_insns ();
1339 end_sequence ();
1341 *loc = new_reg;
1342 if (after != NULL)
1344 start_sequence ();
1345 lra_emit_move (reg, new_reg);
1346 emit_insn (*after);
1347 *after = get_insns ();
1348 end_sequence ();
1350 return true;
1353 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1354 the insn to be inserted before curr insn. AFTER returns the
1355 the insn to be inserted after curr insn. ORIGREG and NEWREG
1356 are the original reg and new reg for reload. */
1357 static void
1358 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1359 rtx newreg)
1361 if (before)
1363 push_to_sequence (*before);
1364 lra_emit_move (newreg, origreg);
1365 *before = get_insns ();
1366 end_sequence ();
1368 if (after)
1370 start_sequence ();
1371 lra_emit_move (origreg, newreg);
1372 emit_insn (*after);
1373 *after = get_insns ();
1374 end_sequence ();
1378 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1380 /* Make reloads for subreg in operand NOP with internal subreg mode
1381 REG_MODE, add new reloads for further processing. Return true if
1382 any change was done. */
1383 static bool
1384 simplify_operand_subreg (int nop, machine_mode reg_mode)
1386 int hard_regno;
1387 rtx_insn *before, *after;
1388 machine_mode mode, innermode;
1389 rtx reg, new_reg;
1390 rtx operand = *curr_id->operand_loc[nop];
1391 enum reg_class regclass;
1392 enum op_type type;
1394 before = after = NULL;
1396 if (GET_CODE (operand) != SUBREG)
1397 return false;
1399 mode = GET_MODE (operand);
1400 reg = SUBREG_REG (operand);
1401 innermode = GET_MODE (reg);
1402 type = curr_static_id->operand[nop].type;
1403 /* If we change address for paradoxical subreg of memory, the
1404 address might violate the necessary alignment or the access might
1405 be slow. So take this into consideration. We should not worry
1406 about access beyond allocated memory for paradoxical memory
1407 subregs as we don't substitute such equiv memory (see processing
1408 equivalences in function lra_constraints) and because for spilled
1409 pseudos we allocate stack memory enough for the biggest
1410 corresponding paradoxical subreg. */
1411 if (MEM_P (reg)
1412 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1413 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1415 rtx subst, old = *curr_id->operand_loc[nop];
1417 alter_subreg (curr_id->operand_loc[nop], false);
1418 subst = *curr_id->operand_loc[nop];
1419 lra_assert (MEM_P (subst));
1420 if (! valid_address_p (innermode, XEXP (reg, 0),
1421 MEM_ADDR_SPACE (reg))
1422 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1423 MEM_ADDR_SPACE (subst)))
1424 return true;
1425 /* If the address was valid and became invalid, prefer to reload
1426 the memory. Typical case is when the index scale should
1427 correspond the memory. */
1428 *curr_id->operand_loc[nop] = old;
1430 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1432 alter_subreg (curr_id->operand_loc[nop], false);
1433 return true;
1435 else if (CONSTANT_P (reg))
1437 /* Try to simplify subreg of constant. It is usually result of
1438 equivalence substitution. */
1439 if (innermode == VOIDmode
1440 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1441 innermode = curr_static_id->operand[nop].mode;
1442 if ((new_reg = simplify_subreg (mode, reg, innermode,
1443 SUBREG_BYTE (operand))) != NULL_RTX)
1445 *curr_id->operand_loc[nop] = new_reg;
1446 return true;
1449 /* Put constant into memory when we have mixed modes. It generates
1450 a better code in most cases as it does not need a secondary
1451 reload memory. It also prevents LRA looping when LRA is using
1452 secondary reload memory again and again. */
1453 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1454 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1456 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1457 alter_subreg (curr_id->operand_loc[nop], false);
1458 return true;
1460 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1461 if there may be a problem accessing OPERAND in the outer
1462 mode. */
1463 if ((REG_P (reg)
1464 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1465 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1466 /* Don't reload paradoxical subregs because we could be looping
1467 having repeatedly final regno out of hard regs range. */
1468 && (hard_regno_nregs[hard_regno][innermode]
1469 >= hard_regno_nregs[hard_regno][mode])
1470 && simplify_subreg_regno (hard_regno, innermode,
1471 SUBREG_BYTE (operand), mode) < 0
1472 /* Don't reload subreg for matching reload. It is actually
1473 valid subreg in LRA. */
1474 && ! LRA_SUBREG_P (operand))
1475 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1477 enum reg_class rclass;
1479 if (REG_P (reg))
1480 /* There is a big probability that we will get the same class
1481 for the new pseudo and we will get the same insn which
1482 means infinite looping. So spill the new pseudo. */
1483 rclass = NO_REGS;
1484 else
1485 /* The class will be defined later in curr_insn_transform. */
1486 rclass
1487 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1489 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1490 rclass, TRUE, "subreg reg", &new_reg))
1492 bool insert_before, insert_after;
1493 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1495 insert_before = (type != OP_OUT
1496 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1497 insert_after = (type != OP_IN);
1498 insert_move_for_subreg (insert_before ? &before : NULL,
1499 insert_after ? &after : NULL,
1500 reg, new_reg);
1502 SUBREG_REG (operand) = new_reg;
1503 lra_process_new_insns (curr_insn, before, after,
1504 "Inserting subreg reload");
1505 return true;
1507 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1508 IRA allocates hardreg to the inner pseudo reg according to its mode
1509 instead of the outermode, so the size of the hardreg may not be enough
1510 to contain the outermode operand, in that case we may need to insert
1511 reload for the reg. For the following two types of paradoxical subreg,
1512 we need to insert reload:
1513 1. If the op_type is OP_IN, and the hardreg could not be paired with
1514 other hardreg to contain the outermode operand
1515 (checked by in_hard_reg_set_p), we need to insert the reload.
1516 2. If the op_type is OP_OUT or OP_INOUT.
1518 Here is a paradoxical subreg example showing how the reload is generated:
1520 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1521 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1523 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1524 here, if reg107 is assigned to hardreg R15, because R15 is the last
1525 hardreg, compiler cannot find another hardreg to pair with R15 to
1526 contain TImode data. So we insert a TImode reload reg180 for it.
1527 After reload is inserted:
1529 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1530 (reg:DI 107 [ __comp ])) -1
1531 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1532 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1534 Two reload hard registers will be allocated to reg180 to save TImode data
1535 in LRA_assign. */
1536 else if (REG_P (reg)
1537 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1538 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1539 && (hard_regno_nregs[hard_regno][innermode]
1540 < hard_regno_nregs[hard_regno][mode])
1541 && (regclass = lra_get_allocno_class (REGNO (reg)))
1542 && (type != OP_IN
1543 || !in_hard_reg_set_p (reg_class_contents[regclass],
1544 mode, hard_regno)))
1546 /* The class will be defined later in curr_insn_transform. */
1547 enum reg_class rclass
1548 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1550 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1551 rclass, TRUE, "paradoxical subreg", &new_reg))
1553 rtx subreg;
1554 bool insert_before, insert_after;
1556 PUT_MODE (new_reg, mode);
1557 subreg = simplify_gen_subreg (innermode, new_reg, mode, 0);
1558 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1560 insert_before = (type != OP_OUT);
1561 insert_after = (type != OP_IN);
1562 insert_move_for_subreg (insert_before ? &before : NULL,
1563 insert_after ? &after : NULL,
1564 reg, subreg);
1566 SUBREG_REG (operand) = new_reg;
1567 lra_process_new_insns (curr_insn, before, after,
1568 "Inserting paradoxical subreg reload");
1569 return true;
1571 return false;
1574 /* Return TRUE if X refers for a hard register from SET. */
1575 static bool
1576 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1578 int i, j, x_hard_regno;
1579 machine_mode mode;
1580 const char *fmt;
1581 enum rtx_code code;
1583 if (x == NULL_RTX)
1584 return false;
1585 code = GET_CODE (x);
1586 mode = GET_MODE (x);
1587 if (code == SUBREG)
1589 x = SUBREG_REG (x);
1590 code = GET_CODE (x);
1591 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1592 mode = GET_MODE (x);
1595 if (REG_P (x))
1597 x_hard_regno = get_hard_regno (x);
1598 return (x_hard_regno >= 0
1599 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1601 if (MEM_P (x))
1603 struct address_info ad;
1605 decompose_mem_address (&ad, x);
1606 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1607 return true;
1608 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1609 return true;
1611 fmt = GET_RTX_FORMAT (code);
1612 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1614 if (fmt[i] == 'e')
1616 if (uses_hard_regs_p (XEXP (x, i), set))
1617 return true;
1619 else if (fmt[i] == 'E')
1621 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1622 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1623 return true;
1626 return false;
1629 /* Return true if OP is a spilled pseudo. */
1630 static inline bool
1631 spilled_pseudo_p (rtx op)
1633 return (REG_P (op)
1634 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1637 /* Return true if X is a general constant. */
1638 static inline bool
1639 general_constant_p (rtx x)
1641 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1644 static bool
1645 reg_in_class_p (rtx reg, enum reg_class cl)
1647 if (cl == NO_REGS)
1648 return get_reg_class (REGNO (reg)) == NO_REGS;
1649 return in_class_p (reg, cl, NULL);
1652 /* Return true if SET of RCLASS contains no hard regs which can be
1653 used in MODE. */
1654 static bool
1655 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1656 HARD_REG_SET &set,
1657 enum machine_mode mode)
1659 HARD_REG_SET temp;
1661 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1662 COPY_HARD_REG_SET (temp, set);
1663 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1664 return (hard_reg_set_subset_p
1665 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1668 /* Major function to choose the current insn alternative and what
1669 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1670 negative we should consider only this alternative. Return false if
1671 we can not choose the alternative or find how to reload the
1672 operands. */
1673 static bool
1674 process_alt_operands (int only_alternative)
1676 bool ok_p = false;
1677 int nop, overall, nalt;
1678 int n_alternatives = curr_static_id->n_alternatives;
1679 int n_operands = curr_static_id->n_operands;
1680 /* LOSERS counts the operands that don't fit this alternative and
1681 would require loading. */
1682 int losers;
1683 /* REJECT is a count of how undesirable this alternative says it is
1684 if any reloading is required. If the alternative matches exactly
1685 then REJECT is ignored, but otherwise it gets this much counted
1686 against it in addition to the reloading needed. */
1687 int reject;
1688 int op_reject;
1689 /* The number of elements in the following array. */
1690 int early_clobbered_regs_num;
1691 /* Numbers of operands which are early clobber registers. */
1692 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1693 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1694 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1695 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1696 bool curr_alt_win[MAX_RECOG_OPERANDS];
1697 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1698 int curr_alt_matches[MAX_RECOG_OPERANDS];
1699 /* The number of elements in the following array. */
1700 int curr_alt_dont_inherit_ops_num;
1701 /* Numbers of operands whose reload pseudos should not be inherited. */
1702 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1703 rtx op;
1704 /* The register when the operand is a subreg of register, otherwise the
1705 operand itself. */
1706 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1707 /* The register if the operand is a register or subreg of register,
1708 otherwise NULL. */
1709 rtx operand_reg[MAX_RECOG_OPERANDS];
1710 int hard_regno[MAX_RECOG_OPERANDS];
1711 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1712 int reload_nregs, reload_sum;
1713 bool costly_p;
1714 enum reg_class cl;
1716 /* Calculate some data common for all alternatives to speed up the
1717 function. */
1718 for (nop = 0; nop < n_operands; nop++)
1720 rtx reg;
1722 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1723 /* The real hard regno of the operand after the allocation. */
1724 hard_regno[nop] = get_hard_regno (op);
1726 operand_reg[nop] = reg = op;
1727 biggest_mode[nop] = GET_MODE (op);
1728 if (GET_CODE (op) == SUBREG)
1730 operand_reg[nop] = reg = SUBREG_REG (op);
1731 if (GET_MODE_SIZE (biggest_mode[nop])
1732 < GET_MODE_SIZE (GET_MODE (reg)))
1733 biggest_mode[nop] = GET_MODE (reg);
1735 if (! REG_P (reg))
1736 operand_reg[nop] = NULL_RTX;
1737 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1738 || ((int) REGNO (reg)
1739 == lra_get_elimination_hard_regno (REGNO (reg))))
1740 no_subreg_reg_operand[nop] = reg;
1741 else
1742 operand_reg[nop] = no_subreg_reg_operand[nop]
1743 /* Just use natural mode for elimination result. It should
1744 be enough for extra constraints hooks. */
1745 = regno_reg_rtx[hard_regno[nop]];
1748 /* The constraints are made of several alternatives. Each operand's
1749 constraint looks like foo,bar,... with commas separating the
1750 alternatives. The first alternatives for all operands go
1751 together, the second alternatives go together, etc.
1753 First loop over alternatives. */
1754 alternative_mask preferred = curr_id->preferred_alternatives;
1755 if (only_alternative >= 0)
1756 preferred &= ALTERNATIVE_BIT (only_alternative);
1758 for (nalt = 0; nalt < n_alternatives; nalt++)
1760 /* Loop over operands for one constraint alternative. */
1761 if (!TEST_BIT (preferred, nalt))
1762 continue;
1764 overall = losers = reject = reload_nregs = reload_sum = 0;
1765 for (nop = 0; nop < n_operands; nop++)
1767 int inc = (curr_static_id
1768 ->operand_alternative[nalt * n_operands + nop].reject);
1769 if (lra_dump_file != NULL && inc != 0)
1770 fprintf (lra_dump_file,
1771 " Staticly defined alt reject+=%d\n", inc);
1772 reject += inc;
1774 early_clobbered_regs_num = 0;
1776 for (nop = 0; nop < n_operands; nop++)
1778 const char *p;
1779 char *end;
1780 int len, c, m, i, opalt_num, this_alternative_matches;
1781 bool win, did_match, offmemok, early_clobber_p;
1782 /* false => this operand can be reloaded somehow for this
1783 alternative. */
1784 bool badop;
1785 /* true => this operand can be reloaded if the alternative
1786 allows regs. */
1787 bool winreg;
1788 /* True if a constant forced into memory would be OK for
1789 this operand. */
1790 bool constmemok;
1791 enum reg_class this_alternative, this_costly_alternative;
1792 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1793 bool this_alternative_match_win, this_alternative_win;
1794 bool this_alternative_offmemok;
1795 bool scratch_p;
1796 machine_mode mode;
1797 enum constraint_num cn;
1799 opalt_num = nalt * n_operands + nop;
1800 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1802 /* Fast track for no constraints at all. */
1803 curr_alt[nop] = NO_REGS;
1804 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1805 curr_alt_win[nop] = true;
1806 curr_alt_match_win[nop] = false;
1807 curr_alt_offmemok[nop] = false;
1808 curr_alt_matches[nop] = -1;
1809 continue;
1812 op = no_subreg_reg_operand[nop];
1813 mode = curr_operand_mode[nop];
1815 win = did_match = winreg = offmemok = constmemok = false;
1816 badop = true;
1818 early_clobber_p = false;
1819 p = curr_static_id->operand_alternative[opalt_num].constraint;
1821 this_costly_alternative = this_alternative = NO_REGS;
1822 /* We update set of possible hard regs besides its class
1823 because reg class might be inaccurate. For example,
1824 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1825 is translated in HI_REGS because classes are merged by
1826 pairs and there is no accurate intermediate class. */
1827 CLEAR_HARD_REG_SET (this_alternative_set);
1828 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1829 this_alternative_win = false;
1830 this_alternative_match_win = false;
1831 this_alternative_offmemok = false;
1832 this_alternative_matches = -1;
1834 /* An empty constraint should be excluded by the fast
1835 track. */
1836 lra_assert (*p != 0 && *p != ',');
1838 op_reject = 0;
1839 /* Scan this alternative's specs for this operand; set WIN
1840 if the operand fits any letter in this alternative.
1841 Otherwise, clear BADOP if this operand could fit some
1842 letter after reloads, or set WINREG if this operand could
1843 fit after reloads provided the constraint allows some
1844 registers. */
1845 costly_p = false;
1848 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1850 case '\0':
1851 len = 0;
1852 break;
1853 case ',':
1854 c = '\0';
1855 break;
1857 case '&':
1858 early_clobber_p = true;
1859 break;
1861 case '$':
1862 op_reject += LRA_MAX_REJECT;
1863 break;
1864 case '^':
1865 op_reject += LRA_LOSER_COST_FACTOR;
1866 break;
1868 case '#':
1869 /* Ignore rest of this alternative. */
1870 c = '\0';
1871 break;
1873 case '0': case '1': case '2': case '3': case '4':
1874 case '5': case '6': case '7': case '8': case '9':
1876 int m_hregno;
1877 bool match_p;
1879 m = strtoul (p, &end, 10);
1880 p = end;
1881 len = 0;
1882 lra_assert (nop > m);
1884 this_alternative_matches = m;
1885 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1886 /* We are supposed to match a previous operand.
1887 If we do, we win if that one did. If we do
1888 not, count both of the operands as losers.
1889 (This is too conservative, since most of the
1890 time only a single reload insn will be needed
1891 to make the two operands win. As a result,
1892 this alternative may be rejected when it is
1893 actually desirable.) */
1894 match_p = false;
1895 if (operands_match_p (*curr_id->operand_loc[nop],
1896 *curr_id->operand_loc[m], m_hregno))
1898 /* We should reject matching of an early
1899 clobber operand if the matching operand is
1900 not dying in the insn. */
1901 if (! curr_static_id->operand[m].early_clobber
1902 || operand_reg[nop] == NULL_RTX
1903 || (find_regno_note (curr_insn, REG_DEAD,
1904 REGNO (op))
1905 || REGNO (op) == REGNO (operand_reg[m])))
1906 match_p = true;
1908 if (match_p)
1910 /* If we are matching a non-offsettable
1911 address where an offsettable address was
1912 expected, then we must reject this
1913 combination, because we can't reload
1914 it. */
1915 if (curr_alt_offmemok[m]
1916 && MEM_P (*curr_id->operand_loc[m])
1917 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1918 continue;
1920 else
1922 /* Operands don't match. Both operands must
1923 allow a reload register, otherwise we
1924 cannot make them match. */
1925 if (curr_alt[m] == NO_REGS)
1926 break;
1927 /* Retroactively mark the operand we had to
1928 match as a loser, if it wasn't already and
1929 it wasn't matched to a register constraint
1930 (e.g it might be matched by memory). */
1931 if (curr_alt_win[m]
1932 && (operand_reg[m] == NULL_RTX
1933 || hard_regno[m] < 0))
1935 losers++;
1936 reload_nregs
1937 += (ira_reg_class_max_nregs[curr_alt[m]]
1938 [GET_MODE (*curr_id->operand_loc[m])]);
1941 /* Prefer matching earlyclobber alternative as
1942 it results in less hard regs required for
1943 the insn than a non-matching earlyclobber
1944 alternative. */
1945 if (curr_static_id->operand[m].early_clobber)
1947 if (lra_dump_file != NULL)
1948 fprintf
1949 (lra_dump_file,
1950 " %d Matching earlyclobber alt:"
1951 " reject--\n",
1952 nop);
1953 reject--;
1955 /* Otherwise we prefer no matching
1956 alternatives because it gives more freedom
1957 in RA. */
1958 else if (operand_reg[nop] == NULL_RTX
1959 || (find_regno_note (curr_insn, REG_DEAD,
1960 REGNO (operand_reg[nop]))
1961 == NULL_RTX))
1963 if (lra_dump_file != NULL)
1964 fprintf
1965 (lra_dump_file,
1966 " %d Matching alt: reject+=2\n",
1967 nop);
1968 reject += 2;
1971 /* If we have to reload this operand and some
1972 previous operand also had to match the same
1973 thing as this operand, we don't know how to do
1974 that. */
1975 if (!match_p || !curr_alt_win[m])
1977 for (i = 0; i < nop; i++)
1978 if (curr_alt_matches[i] == m)
1979 break;
1980 if (i < nop)
1981 break;
1983 else
1984 did_match = true;
1986 /* This can be fixed with reloads if the operand
1987 we are supposed to match can be fixed with
1988 reloads. */
1989 badop = false;
1990 this_alternative = curr_alt[m];
1991 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1992 winreg = this_alternative != NO_REGS;
1993 break;
1996 case 'g':
1997 if (MEM_P (op)
1998 || general_constant_p (op)
1999 || spilled_pseudo_p (op))
2000 win = true;
2001 cl = GENERAL_REGS;
2002 goto reg;
2004 default:
2005 cn = lookup_constraint (p);
2006 switch (get_constraint_type (cn))
2008 case CT_REGISTER:
2009 cl = reg_class_for_constraint (cn);
2010 if (cl != NO_REGS)
2011 goto reg;
2012 break;
2014 case CT_CONST_INT:
2015 if (CONST_INT_P (op)
2016 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2017 win = true;
2018 break;
2020 case CT_MEMORY:
2021 if (MEM_P (op)
2022 && satisfies_memory_constraint_p (op, cn))
2023 win = true;
2024 else if (spilled_pseudo_p (op))
2025 win = true;
2027 /* If we didn't already win, we can reload constants
2028 via force_const_mem or put the pseudo value into
2029 memory, or make other memory by reloading the
2030 address like for 'o'. */
2031 if (CONST_POOL_OK_P (mode, op)
2032 || MEM_P (op) || REG_P (op))
2033 badop = false;
2034 constmemok = true;
2035 offmemok = true;
2036 break;
2038 case CT_ADDRESS:
2039 /* If we didn't already win, we can reload the address
2040 into a base register. */
2041 if (satisfies_address_constraint_p (op, cn))
2042 win = true;
2043 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2044 ADDRESS, SCRATCH);
2045 badop = false;
2046 goto reg;
2048 case CT_FIXED_FORM:
2049 if (constraint_satisfied_p (op, cn))
2050 win = true;
2051 break;
2053 break;
2055 reg:
2056 this_alternative = reg_class_subunion[this_alternative][cl];
2057 IOR_HARD_REG_SET (this_alternative_set,
2058 reg_class_contents[cl]);
2059 if (costly_p)
2061 this_costly_alternative
2062 = reg_class_subunion[this_costly_alternative][cl];
2063 IOR_HARD_REG_SET (this_costly_alternative_set,
2064 reg_class_contents[cl]);
2066 if (mode == BLKmode)
2067 break;
2068 winreg = true;
2069 if (REG_P (op))
2071 if (hard_regno[nop] >= 0
2072 && in_hard_reg_set_p (this_alternative_set,
2073 mode, hard_regno[nop]))
2074 win = true;
2075 else if (hard_regno[nop] < 0
2076 && in_class_p (op, this_alternative, NULL))
2077 win = true;
2079 break;
2081 if (c != ' ' && c != '\t')
2082 costly_p = c == '*';
2084 while ((p += len), c);
2086 scratch_p = (operand_reg[nop] != NULL_RTX
2087 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2088 /* Record which operands fit this alternative. */
2089 if (win)
2091 this_alternative_win = true;
2092 if (operand_reg[nop] != NULL_RTX)
2094 if (hard_regno[nop] >= 0)
2096 if (in_hard_reg_set_p (this_costly_alternative_set,
2097 mode, hard_regno[nop]))
2099 if (lra_dump_file != NULL)
2100 fprintf (lra_dump_file,
2101 " %d Costly set: reject++\n",
2102 nop);
2103 reject++;
2106 else
2108 /* Prefer won reg to spilled pseudo under other
2109 equal conditions for possibe inheritance. */
2110 if (! scratch_p)
2112 if (lra_dump_file != NULL)
2113 fprintf
2114 (lra_dump_file,
2115 " %d Non pseudo reload: reject++\n",
2116 nop);
2117 reject++;
2119 if (in_class_p (operand_reg[nop],
2120 this_costly_alternative, NULL))
2122 if (lra_dump_file != NULL)
2123 fprintf
2124 (lra_dump_file,
2125 " %d Non pseudo costly reload:"
2126 " reject++\n",
2127 nop);
2128 reject++;
2131 /* We simulate the behaviour of old reload here.
2132 Although scratches need hard registers and it
2133 might result in spilling other pseudos, no reload
2134 insns are generated for the scratches. So it
2135 might cost something but probably less than old
2136 reload pass believes. */
2137 if (scratch_p)
2139 if (lra_dump_file != NULL)
2140 fprintf (lra_dump_file,
2141 " %d Scratch win: reject+=2\n",
2142 nop);
2143 reject += 2;
2147 else if (did_match)
2148 this_alternative_match_win = true;
2149 else
2151 int const_to_mem = 0;
2152 bool no_regs_p;
2154 reject += op_reject;
2155 /* Never do output reload of stack pointer. It makes
2156 impossible to do elimination when SP is changed in
2157 RTL. */
2158 if (op == stack_pointer_rtx && ! frame_pointer_needed
2159 && curr_static_id->operand[nop].type != OP_IN)
2160 goto fail;
2162 /* If this alternative asks for a specific reg class, see if there
2163 is at least one allocatable register in that class. */
2164 no_regs_p
2165 = (this_alternative == NO_REGS
2166 || (hard_reg_set_subset_p
2167 (reg_class_contents[this_alternative],
2168 lra_no_alloc_regs)));
2170 /* For asms, verify that the class for this alternative is possible
2171 for the mode that is specified. */
2172 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2174 int i;
2175 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2176 if (HARD_REGNO_MODE_OK (i, mode)
2177 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2178 mode, i))
2179 break;
2180 if (i == FIRST_PSEUDO_REGISTER)
2181 winreg = false;
2184 /* If this operand accepts a register, and if the
2185 register class has at least one allocatable register,
2186 then this operand can be reloaded. */
2187 if (winreg && !no_regs_p)
2188 badop = false;
2190 if (badop)
2192 if (lra_dump_file != NULL)
2193 fprintf (lra_dump_file,
2194 " alt=%d: Bad operand -- refuse\n",
2195 nalt);
2196 goto fail;
2199 /* If not assigned pseudo has a class which a subset of
2200 required reg class, it is a less costly alternative
2201 as the pseudo still can get a hard reg of necessary
2202 class. */
2203 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2204 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2205 && ira_class_subset_p[this_alternative][cl])
2207 if (lra_dump_file != NULL)
2208 fprintf
2209 (lra_dump_file,
2210 " %d Super set class reg: reject-=3\n", nop);
2211 reject -= 3;
2214 this_alternative_offmemok = offmemok;
2215 if (this_costly_alternative != NO_REGS)
2217 if (lra_dump_file != NULL)
2218 fprintf (lra_dump_file,
2219 " %d Costly loser: reject++\n", nop);
2220 reject++;
2222 /* If the operand is dying, has a matching constraint,
2223 and satisfies constraints of the matched operand
2224 which failed to satisfy the own constraints, most probably
2225 the reload for this operand will be gone. */
2226 if (this_alternative_matches >= 0
2227 && !curr_alt_win[this_alternative_matches]
2228 && REG_P (op)
2229 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2230 && (hard_regno[nop] >= 0
2231 ? in_hard_reg_set_p (this_alternative_set,
2232 mode, hard_regno[nop])
2233 : in_class_p (op, this_alternative, NULL)))
2235 if (lra_dump_file != NULL)
2236 fprintf
2237 (lra_dump_file,
2238 " %d Dying matched operand reload: reject++\n",
2239 nop);
2240 reject++;
2242 else
2244 /* Strict_low_part requires to reload the register
2245 not the sub-register. In this case we should
2246 check that a final reload hard reg can hold the
2247 value mode. */
2248 if (curr_static_id->operand[nop].strict_low
2249 && REG_P (op)
2250 && hard_regno[nop] < 0
2251 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2252 && ira_class_hard_regs_num[this_alternative] > 0
2253 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2254 [this_alternative][0],
2255 GET_MODE
2256 (*curr_id->operand_loc[nop])))
2258 if (lra_dump_file != NULL)
2259 fprintf
2260 (lra_dump_file,
2261 " alt=%d: Strict low subreg reload -- refuse\n",
2262 nalt);
2263 goto fail;
2265 losers++;
2267 if (operand_reg[nop] != NULL_RTX
2268 /* Output operands and matched input operands are
2269 not inherited. The following conditions do not
2270 exactly describe the previous statement but they
2271 are pretty close. */
2272 && curr_static_id->operand[nop].type != OP_OUT
2273 && (this_alternative_matches < 0
2274 || curr_static_id->operand[nop].type != OP_IN))
2276 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2277 (operand_reg[nop])]
2278 .last_reload);
2280 /* The value of reload_sum has sense only if we
2281 process insns in their order. It happens only on
2282 the first constraints sub-pass when we do most of
2283 reload work. */
2284 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2285 reload_sum += last_reload - bb_reload_num;
2287 /* If this is a constant that is reloaded into the
2288 desired class by copying it to memory first, count
2289 that as another reload. This is consistent with
2290 other code and is required to avoid choosing another
2291 alternative when the constant is moved into memory.
2292 Note that the test here is precisely the same as in
2293 the code below that calls force_const_mem. */
2294 if (CONST_POOL_OK_P (mode, op)
2295 && ((targetm.preferred_reload_class
2296 (op, this_alternative) == NO_REGS)
2297 || no_input_reloads_p))
2299 const_to_mem = 1;
2300 if (! no_regs_p)
2301 losers++;
2304 /* Alternative loses if it requires a type of reload not
2305 permitted for this insn. We can always reload
2306 objects with a REG_UNUSED note. */
2307 if ((curr_static_id->operand[nop].type != OP_IN
2308 && no_output_reloads_p
2309 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2310 || (curr_static_id->operand[nop].type != OP_OUT
2311 && no_input_reloads_p && ! const_to_mem)
2312 || (this_alternative_matches >= 0
2313 && (no_input_reloads_p
2314 || (no_output_reloads_p
2315 && (curr_static_id->operand
2316 [this_alternative_matches].type != OP_IN)
2317 && ! find_reg_note (curr_insn, REG_UNUSED,
2318 no_subreg_reg_operand
2319 [this_alternative_matches])))))
2321 if (lra_dump_file != NULL)
2322 fprintf
2323 (lra_dump_file,
2324 " alt=%d: No input/otput reload -- refuse\n",
2325 nalt);
2326 goto fail;
2329 /* Alternative loses if it required class pseudo can not
2330 hold value of required mode. Such insns can be
2331 described by insn definitions with mode iterators. */
2332 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2333 && ! hard_reg_set_empty_p (this_alternative_set)
2334 /* It is common practice for constraints to use a
2335 class which does not have actually enough regs to
2336 hold the value (e.g. x86 AREG for mode requiring
2337 more one general reg). Therefore we have 2
2338 conditions to check that the reload pseudo can
2339 not hold the mode value. */
2340 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2341 [this_alternative][0],
2342 GET_MODE (*curr_id->operand_loc[nop]))
2343 /* The above condition is not enough as the first
2344 reg in ira_class_hard_regs can be not aligned for
2345 multi-words mode values. */
2346 && (prohibited_class_reg_set_mode_p
2347 (this_alternative, this_alternative_set,
2348 GET_MODE (*curr_id->operand_loc[nop]))))
2350 if (lra_dump_file != NULL)
2351 fprintf (lra_dump_file,
2352 " alt=%d: reload pseudo for op %d "
2353 " can not hold the mode value -- refuse\n",
2354 nalt, nop);
2355 goto fail;
2358 /* Check strong discouragement of reload of non-constant
2359 into class THIS_ALTERNATIVE. */
2360 if (! CONSTANT_P (op) && ! no_regs_p
2361 && (targetm.preferred_reload_class
2362 (op, this_alternative) == NO_REGS
2363 || (curr_static_id->operand[nop].type == OP_OUT
2364 && (targetm.preferred_output_reload_class
2365 (op, this_alternative) == NO_REGS))))
2367 if (lra_dump_file != NULL)
2368 fprintf (lra_dump_file,
2369 " %d Non-prefered reload: reject+=%d\n",
2370 nop, LRA_MAX_REJECT);
2371 reject += LRA_MAX_REJECT;
2374 if (! (MEM_P (op) && offmemok)
2375 && ! (const_to_mem && constmemok))
2377 /* We prefer to reload pseudos over reloading other
2378 things, since such reloads may be able to be
2379 eliminated later. So bump REJECT in other cases.
2380 Don't do this in the case where we are forcing a
2381 constant into memory and it will then win since
2382 we don't want to have a different alternative
2383 match then. */
2384 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2386 if (lra_dump_file != NULL)
2387 fprintf
2388 (lra_dump_file,
2389 " %d Non-pseudo reload: reject+=2\n",
2390 nop);
2391 reject += 2;
2394 if (! no_regs_p)
2395 reload_nregs
2396 += ira_reg_class_max_nregs[this_alternative][mode];
2398 if (SMALL_REGISTER_CLASS_P (this_alternative))
2400 if (lra_dump_file != NULL)
2401 fprintf
2402 (lra_dump_file,
2403 " %d Small class reload: reject+=%d\n",
2404 nop, LRA_LOSER_COST_FACTOR / 2);
2405 reject += LRA_LOSER_COST_FACTOR / 2;
2409 /* We are trying to spill pseudo into memory. It is
2410 usually more costly than moving to a hard register
2411 although it might takes the same number of
2412 reloads. */
2413 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2415 if (lra_dump_file != NULL)
2416 fprintf
2417 (lra_dump_file,
2418 " %d Spill pseudo into memory: reject+=3\n",
2419 nop);
2420 reject += 3;
2421 if (VECTOR_MODE_P (mode))
2423 /* Spilling vectors into memory is usually more
2424 costly as they contain big values. */
2425 if (lra_dump_file != NULL)
2426 fprintf
2427 (lra_dump_file,
2428 " %d Spill vector pseudo: reject+=2\n",
2429 nop);
2430 reject += 2;
2434 #ifdef SECONDARY_MEMORY_NEEDED
2435 /* If reload requires moving value through secondary
2436 memory, it will need one more insn at least. */
2437 if (this_alternative != NO_REGS
2438 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2439 && ((curr_static_id->operand[nop].type != OP_OUT
2440 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2441 GET_MODE (op)))
2442 || (curr_static_id->operand[nop].type != OP_IN
2443 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2444 GET_MODE (op)))))
2445 losers++;
2446 #endif
2447 /* Input reloads can be inherited more often than output
2448 reloads can be removed, so penalize output
2449 reloads. */
2450 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2452 if (lra_dump_file != NULL)
2453 fprintf
2454 (lra_dump_file,
2455 " %d Non input pseudo reload: reject++\n",
2456 nop);
2457 reject++;
2461 if (early_clobber_p && ! scratch_p)
2463 if (lra_dump_file != NULL)
2464 fprintf (lra_dump_file,
2465 " %d Early clobber: reject++\n", nop);
2466 reject++;
2468 /* ??? We check early clobbers after processing all operands
2469 (see loop below) and there we update the costs more.
2470 Should we update the cost (may be approximately) here
2471 because of early clobber register reloads or it is a rare
2472 or non-important thing to be worth to do it. */
2473 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2474 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2476 if (lra_dump_file != NULL)
2477 fprintf (lra_dump_file,
2478 " alt=%d,overall=%d,losers=%d -- refuse\n",
2479 nalt, overall, losers);
2480 goto fail;
2483 curr_alt[nop] = this_alternative;
2484 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2485 curr_alt_win[nop] = this_alternative_win;
2486 curr_alt_match_win[nop] = this_alternative_match_win;
2487 curr_alt_offmemok[nop] = this_alternative_offmemok;
2488 curr_alt_matches[nop] = this_alternative_matches;
2490 if (this_alternative_matches >= 0
2491 && !did_match && !this_alternative_win)
2492 curr_alt_win[this_alternative_matches] = false;
2494 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2495 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2497 if (curr_insn_set != NULL_RTX && n_operands == 2
2498 /* Prevent processing non-move insns. */
2499 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2500 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2501 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2502 && REG_P (no_subreg_reg_operand[0])
2503 && REG_P (no_subreg_reg_operand[1])
2504 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2505 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2506 || (! curr_alt_win[0] && curr_alt_win[1]
2507 && REG_P (no_subreg_reg_operand[1])
2508 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2509 || (curr_alt_win[0] && ! curr_alt_win[1]
2510 && REG_P (no_subreg_reg_operand[0])
2511 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2512 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2513 no_subreg_reg_operand[1])
2514 || (targetm.preferred_reload_class
2515 (no_subreg_reg_operand[1],
2516 (enum reg_class) curr_alt[1]) != NO_REGS))
2517 /* If it is a result of recent elimination in move
2518 insn we can transform it into an add still by
2519 using this alternative. */
2520 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2522 /* We have a move insn and a new reload insn will be similar
2523 to the current insn. We should avoid such situation as it
2524 results in LRA cycling. */
2525 overall += LRA_MAX_REJECT;
2527 ok_p = true;
2528 curr_alt_dont_inherit_ops_num = 0;
2529 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2531 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2532 HARD_REG_SET temp_set;
2534 i = early_clobbered_nops[nop];
2535 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2536 || hard_regno[i] < 0)
2537 continue;
2538 lra_assert (operand_reg[i] != NULL_RTX);
2539 clobbered_hard_regno = hard_regno[i];
2540 CLEAR_HARD_REG_SET (temp_set);
2541 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2542 first_conflict_j = last_conflict_j = -1;
2543 for (j = 0; j < n_operands; j++)
2544 if (j == i
2545 /* We don't want process insides of match_operator and
2546 match_parallel because otherwise we would process
2547 their operands once again generating a wrong
2548 code. */
2549 || curr_static_id->operand[j].is_operator)
2550 continue;
2551 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2552 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2553 continue;
2554 /* If we don't reload j-th operand, check conflicts. */
2555 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2556 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2558 if (first_conflict_j < 0)
2559 first_conflict_j = j;
2560 last_conflict_j = j;
2562 if (last_conflict_j < 0)
2563 continue;
2564 /* If earlyclobber operand conflicts with another
2565 non-matching operand which is actually the same register
2566 as the earlyclobber operand, it is better to reload the
2567 another operand as an operand matching the earlyclobber
2568 operand can be also the same. */
2569 if (first_conflict_j == last_conflict_j
2570 && operand_reg[last_conflict_j]
2571 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2572 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2574 curr_alt_win[last_conflict_j] = false;
2575 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2576 = last_conflict_j;
2577 losers++;
2578 /* Early clobber was already reflected in REJECT. */
2579 lra_assert (reject > 0);
2580 if (lra_dump_file != NULL)
2581 fprintf
2582 (lra_dump_file,
2583 " %d Conflict early clobber reload: reject--\n",
2585 reject--;
2586 overall += LRA_LOSER_COST_FACTOR - 1;
2588 else
2590 /* We need to reload early clobbered register and the
2591 matched registers. */
2592 for (j = 0; j < n_operands; j++)
2593 if (curr_alt_matches[j] == i)
2595 curr_alt_match_win[j] = false;
2596 losers++;
2597 overall += LRA_LOSER_COST_FACTOR;
2599 if (! curr_alt_match_win[i])
2600 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2601 else
2603 /* Remember pseudos used for match reloads are never
2604 inherited. */
2605 lra_assert (curr_alt_matches[i] >= 0);
2606 curr_alt_win[curr_alt_matches[i]] = false;
2608 curr_alt_win[i] = curr_alt_match_win[i] = false;
2609 losers++;
2610 /* Early clobber was already reflected in REJECT. */
2611 lra_assert (reject > 0);
2612 if (lra_dump_file != NULL)
2613 fprintf
2614 (lra_dump_file,
2615 " %d Matched conflict early clobber reloads:"
2616 "reject--\n",
2618 reject--;
2619 overall += LRA_LOSER_COST_FACTOR - 1;
2622 if (lra_dump_file != NULL)
2623 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2624 nalt, overall, losers, reload_nregs);
2626 /* If this alternative can be made to work by reloading, and it
2627 needs less reloading than the others checked so far, record
2628 it as the chosen goal for reloading. */
2629 if ((best_losers != 0 && losers == 0)
2630 || (((best_losers == 0 && losers == 0)
2631 || (best_losers != 0 && losers != 0))
2632 && (best_overall > overall
2633 || (best_overall == overall
2634 /* If the cost of the reloads is the same,
2635 prefer alternative which requires minimal
2636 number of reload regs. */
2637 && (reload_nregs < best_reload_nregs
2638 || (reload_nregs == best_reload_nregs
2639 && (best_reload_sum < reload_sum
2640 || (best_reload_sum == reload_sum
2641 && nalt < goal_alt_number))))))))
2643 for (nop = 0; nop < n_operands; nop++)
2645 goal_alt_win[nop] = curr_alt_win[nop];
2646 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2647 goal_alt_matches[nop] = curr_alt_matches[nop];
2648 goal_alt[nop] = curr_alt[nop];
2649 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2651 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2652 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2653 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2654 goal_alt_swapped = curr_swapped;
2655 best_overall = overall;
2656 best_losers = losers;
2657 best_reload_nregs = reload_nregs;
2658 best_reload_sum = reload_sum;
2659 goal_alt_number = nalt;
2661 if (losers == 0)
2662 /* Everything is satisfied. Do not process alternatives
2663 anymore. */
2664 break;
2665 fail:
2668 return ok_p;
2671 /* Make reload base reg from address AD. */
2672 static rtx
2673 base_to_reg (struct address_info *ad)
2675 enum reg_class cl;
2676 int code = -1;
2677 rtx new_inner = NULL_RTX;
2678 rtx new_reg = NULL_RTX;
2679 rtx_insn *insn;
2680 rtx_insn *last_insn = get_last_insn();
2682 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2683 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2684 get_index_code (ad));
2685 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2686 cl, "base");
2687 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2688 ad->disp_term == NULL
2689 ? gen_int_mode (0, ad->mode)
2690 : *ad->disp_term);
2691 if (!valid_address_p (ad->mode, new_inner, ad->as))
2692 return NULL_RTX;
2693 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base_term));
2694 code = recog_memoized (insn);
2695 if (code < 0)
2697 delete_insns_since (last_insn);
2698 return NULL_RTX;
2701 return new_inner;
2704 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2705 static rtx
2706 base_plus_disp_to_reg (struct address_info *ad)
2708 enum reg_class cl;
2709 rtx new_reg;
2711 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2712 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2713 get_index_code (ad));
2714 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2715 cl, "base + disp");
2716 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2717 return new_reg;
2720 /* Make reload of index part of address AD. Return the new
2721 pseudo. */
2722 static rtx
2723 index_part_to_reg (struct address_info *ad)
2725 rtx new_reg;
2727 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2728 INDEX_REG_CLASS, "index term");
2729 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2730 GEN_INT (get_index_scale (ad)), new_reg, 1);
2731 return new_reg;
2734 /* Return true if we can add a displacement to address AD, even if that
2735 makes the address invalid. The fix-up code requires any new address
2736 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2737 static bool
2738 can_add_disp_p (struct address_info *ad)
2740 return (!ad->autoinc_p
2741 && ad->segment == NULL
2742 && ad->base == ad->base_term
2743 && ad->disp == ad->disp_term);
2746 /* Make equiv substitution in address AD. Return true if a substitution
2747 was made. */
2748 static bool
2749 equiv_address_substitution (struct address_info *ad)
2751 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2752 HOST_WIDE_INT disp, scale;
2753 bool change_p;
2755 base_term = strip_subreg (ad->base_term);
2756 if (base_term == NULL)
2757 base_reg = new_base_reg = NULL_RTX;
2758 else
2760 base_reg = *base_term;
2761 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2763 index_term = strip_subreg (ad->index_term);
2764 if (index_term == NULL)
2765 index_reg = new_index_reg = NULL_RTX;
2766 else
2768 index_reg = *index_term;
2769 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2771 if (base_reg == new_base_reg && index_reg == new_index_reg)
2772 return false;
2773 disp = 0;
2774 change_p = false;
2775 if (lra_dump_file != NULL)
2777 fprintf (lra_dump_file, "Changing address in insn %d ",
2778 INSN_UID (curr_insn));
2779 dump_value_slim (lra_dump_file, *ad->outer, 1);
2781 if (base_reg != new_base_reg)
2783 if (REG_P (new_base_reg))
2785 *base_term = new_base_reg;
2786 change_p = true;
2788 else if (GET_CODE (new_base_reg) == PLUS
2789 && REG_P (XEXP (new_base_reg, 0))
2790 && CONST_INT_P (XEXP (new_base_reg, 1))
2791 && can_add_disp_p (ad))
2793 disp += INTVAL (XEXP (new_base_reg, 1));
2794 *base_term = XEXP (new_base_reg, 0);
2795 change_p = true;
2797 if (ad->base_term2 != NULL)
2798 *ad->base_term2 = *ad->base_term;
2800 if (index_reg != new_index_reg)
2802 if (REG_P (new_index_reg))
2804 *index_term = new_index_reg;
2805 change_p = true;
2807 else if (GET_CODE (new_index_reg) == PLUS
2808 && REG_P (XEXP (new_index_reg, 0))
2809 && CONST_INT_P (XEXP (new_index_reg, 1))
2810 && can_add_disp_p (ad)
2811 && (scale = get_index_scale (ad)))
2813 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2814 *index_term = XEXP (new_index_reg, 0);
2815 change_p = true;
2818 if (disp != 0)
2820 if (ad->disp != NULL)
2821 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2822 else
2824 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2825 update_address (ad);
2827 change_p = true;
2829 if (lra_dump_file != NULL)
2831 if (! change_p)
2832 fprintf (lra_dump_file, " -- no change\n");
2833 else
2835 fprintf (lra_dump_file, " on equiv ");
2836 dump_value_slim (lra_dump_file, *ad->outer, 1);
2837 fprintf (lra_dump_file, "\n");
2840 return change_p;
2843 /* Major function to make reloads for an address in operand NOP or
2844 check its correctness (If CHECK_ONLY_P is true). The supported
2845 cases are:
2847 1) an address that existed before LRA started, at which point it
2848 must have been valid. These addresses are subject to elimination
2849 and may have become invalid due to the elimination offset being out
2850 of range.
2852 2) an address created by forcing a constant to memory
2853 (force_const_to_mem). The initial form of these addresses might
2854 not be valid, and it is this function's job to make them valid.
2856 3) a frame address formed from a register and a (possibly zero)
2857 constant offset. As above, these addresses might not be valid and
2858 this function must make them so.
2860 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2861 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2862 address. Return true for any RTL change.
2864 The function is a helper function which does not produce all
2865 transformations (when CHECK_ONLY_P is false) which can be
2866 necessary. It does just basic steps. To do all necessary
2867 transformations use function process_address. */
2868 static bool
2869 process_address_1 (int nop, bool check_only_p,
2870 rtx_insn **before, rtx_insn **after)
2872 struct address_info ad;
2873 rtx new_reg;
2874 rtx op = *curr_id->operand_loc[nop];
2875 const char *constraint = curr_static_id->operand[nop].constraint;
2876 enum constraint_num cn = lookup_constraint (constraint);
2877 bool change_p = false;
2879 if (insn_extra_address_constraint (cn))
2880 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2881 else if (MEM_P (op))
2882 decompose_mem_address (&ad, op);
2883 else if (GET_CODE (op) == SUBREG
2884 && MEM_P (SUBREG_REG (op)))
2885 decompose_mem_address (&ad, SUBREG_REG (op));
2886 else
2887 return false;
2888 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
2889 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
2890 when INDEX_REG_CLASS is a single register class. */
2891 if (ad.base_term != NULL
2892 && ad.index_term != NULL
2893 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
2894 && REG_P (*ad.base_term)
2895 && REG_P (*ad.index_term)
2896 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
2897 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
2899 std::swap (ad.base, ad.index);
2900 std::swap (ad.base_term, ad.index_term);
2902 if (! check_only_p)
2903 change_p = equiv_address_substitution (&ad);
2904 if (ad.base_term != NULL
2905 && (process_addr_reg
2906 (ad.base_term, check_only_p, before,
2907 (ad.autoinc_p
2908 && !(REG_P (*ad.base_term)
2909 && find_regno_note (curr_insn, REG_DEAD,
2910 REGNO (*ad.base_term)) != NULL_RTX)
2911 ? after : NULL),
2912 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2913 get_index_code (&ad)))))
2915 change_p = true;
2916 if (ad.base_term2 != NULL)
2917 *ad.base_term2 = *ad.base_term;
2919 if (ad.index_term != NULL
2920 && process_addr_reg (ad.index_term, check_only_p,
2921 before, NULL, INDEX_REG_CLASS))
2922 change_p = true;
2924 /* Target hooks sometimes don't treat extra-constraint addresses as
2925 legitimate address_operands, so handle them specially. */
2926 if (insn_extra_address_constraint (cn)
2927 && satisfies_address_constraint_p (&ad, cn))
2928 return change_p;
2930 if (check_only_p)
2931 return change_p;
2933 /* There are three cases where the shape of *AD.INNER may now be invalid:
2935 1) the original address was valid, but either elimination or
2936 equiv_address_substitution was applied and that made
2937 the address invalid.
2939 2) the address is an invalid symbolic address created by
2940 force_const_to_mem.
2942 3) the address is a frame address with an invalid offset.
2944 4) the address is a frame address with an invalid base.
2946 All these cases involve a non-autoinc address, so there is no
2947 point revalidating other types. */
2948 if (ad.autoinc_p || valid_address_p (&ad))
2949 return change_p;
2951 /* Any index existed before LRA started, so we can assume that the
2952 presence and shape of the index is valid. */
2953 push_to_sequence (*before);
2954 lra_assert (ad.disp == ad.disp_term);
2955 if (ad.base == NULL)
2957 if (ad.index == NULL)
2959 int code = -1;
2960 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2961 SCRATCH, SCRATCH);
2962 rtx addr = *ad.inner;
2964 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2965 #ifdef HAVE_lo_sum
2967 rtx_insn *insn;
2968 rtx_insn *last = get_last_insn ();
2970 /* addr => lo_sum (new_base, addr), case (2) above. */
2971 insn = emit_insn (gen_rtx_SET
2972 (new_reg,
2973 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2974 code = recog_memoized (insn);
2975 if (code >= 0)
2977 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2978 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2980 /* Try to put lo_sum into register. */
2981 insn = emit_insn (gen_rtx_SET
2982 (new_reg,
2983 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2984 code = recog_memoized (insn);
2985 if (code >= 0)
2987 *ad.inner = new_reg;
2988 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2990 *ad.inner = addr;
2991 code = -1;
2997 if (code < 0)
2998 delete_insns_since (last);
3000 #endif
3001 if (code < 0)
3003 /* addr => new_base, case (2) above. */
3004 lra_emit_move (new_reg, addr);
3005 *ad.inner = new_reg;
3008 else
3010 /* index * scale + disp => new base + index * scale,
3011 case (1) above. */
3012 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3013 GET_CODE (*ad.index));
3015 lra_assert (INDEX_REG_CLASS != NO_REGS);
3016 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3017 lra_emit_move (new_reg, *ad.disp);
3018 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3019 new_reg, *ad.index);
3022 else if (ad.index == NULL)
3024 int regno;
3025 enum reg_class cl;
3026 rtx set;
3027 rtx_insn *insns, *last_insn;
3028 /* Try to reload base into register only if the base is invalid
3029 for the address but with valid offset, case (4) above. */
3030 start_sequence ();
3031 new_reg = base_to_reg (&ad);
3033 /* base + disp => new base, cases (1) and (3) above. */
3034 /* Another option would be to reload the displacement into an
3035 index register. However, postreload has code to optimize
3036 address reloads that have the same base and different
3037 displacements, so reloading into an index register would
3038 not necessarily be a win. */
3039 if (new_reg == NULL_RTX)
3040 new_reg = base_plus_disp_to_reg (&ad);
3041 insns = get_insns ();
3042 last_insn = get_last_insn ();
3043 /* If we generated at least two insns, try last insn source as
3044 an address. If we succeed, we generate one less insn. */
3045 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3046 && GET_CODE (SET_SRC (set)) == PLUS
3047 && REG_P (XEXP (SET_SRC (set), 0))
3048 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3050 *ad.inner = SET_SRC (set);
3051 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3053 *ad.base_term = XEXP (SET_SRC (set), 0);
3054 *ad.disp_term = XEXP (SET_SRC (set), 1);
3055 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3056 get_index_code (&ad));
3057 regno = REGNO (*ad.base_term);
3058 if (regno >= FIRST_PSEUDO_REGISTER
3059 && cl != lra_get_allocno_class (regno))
3060 lra_change_class (regno, cl, " Change to", true);
3061 new_reg = SET_SRC (set);
3062 delete_insns_since (PREV_INSN (last_insn));
3065 /* Try if target can split displacement into legitimite new disp
3066 and offset. If it's the case, we replace the last insn with
3067 insns for base + offset => new_reg and set new_reg + new disp
3068 to *ad.inner. */
3069 last_insn = get_last_insn ();
3070 if ((set = single_set (last_insn)) != NULL_RTX
3071 && GET_CODE (SET_SRC (set)) == PLUS
3072 && REG_P (XEXP (SET_SRC (set), 0))
3073 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3074 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3076 rtx addend, disp = XEXP (SET_SRC (set), 1);
3077 if (targetm.legitimize_address_displacement (&disp, &addend,
3078 ad.mode))
3080 rtx_insn *new_insns;
3081 start_sequence ();
3082 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3083 new_insns = get_insns ();
3084 end_sequence ();
3085 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3086 delete_insns_since (PREV_INSN (last_insn));
3087 add_insn (new_insns);
3088 insns = get_insns ();
3091 end_sequence ();
3092 emit_insn (insns);
3093 *ad.inner = new_reg;
3095 else if (ad.disp_term != NULL)
3097 /* base + scale * index + disp => new base + scale * index,
3098 case (1) above. */
3099 new_reg = base_plus_disp_to_reg (&ad);
3100 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3101 new_reg, *ad.index);
3103 else if (get_index_scale (&ad) == 1)
3105 /* The last transformation to one reg will be made in
3106 curr_insn_transform function. */
3107 end_sequence ();
3108 return false;
3110 else
3112 /* base + scale * index => base + new_reg,
3113 case (1) above.
3114 Index part of address may become invalid. For example, we
3115 changed pseudo on the equivalent memory and a subreg of the
3116 pseudo onto the memory of different mode for which the scale is
3117 prohibitted. */
3118 new_reg = index_part_to_reg (&ad);
3119 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3120 *ad.base_term, new_reg);
3122 *before = get_insns ();
3123 end_sequence ();
3124 return true;
3127 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3128 Use process_address_1 as a helper function. Return true for any
3129 RTL changes.
3131 If CHECK_ONLY_P is true, just check address correctness. Return
3132 false if the address correct. */
3133 static bool
3134 process_address (int nop, bool check_only_p,
3135 rtx_insn **before, rtx_insn **after)
3137 bool res = false;
3139 while (process_address_1 (nop, check_only_p, before, after))
3141 if (check_only_p)
3142 return true;
3143 res = true;
3145 return res;
3148 /* Emit insns to reload VALUE into a new register. VALUE is an
3149 auto-increment or auto-decrement RTX whose operand is a register or
3150 memory location; so reloading involves incrementing that location.
3151 IN is either identical to VALUE, or some cheaper place to reload
3152 value being incremented/decremented from.
3154 INC_AMOUNT is the number to increment or decrement by (always
3155 positive and ignored for POST_MODIFY/PRE_MODIFY).
3157 Return pseudo containing the result. */
3158 static rtx
3159 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3161 /* REG or MEM to be copied and incremented. */
3162 rtx incloc = XEXP (value, 0);
3163 /* Nonzero if increment after copying. */
3164 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3165 || GET_CODE (value) == POST_MODIFY);
3166 rtx_insn *last;
3167 rtx inc;
3168 rtx_insn *add_insn;
3169 int code;
3170 rtx real_in = in == value ? incloc : in;
3171 rtx result;
3172 bool plus_p = true;
3174 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3176 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3177 || GET_CODE (XEXP (value, 1)) == MINUS);
3178 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3179 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3180 inc = XEXP (XEXP (value, 1), 1);
3182 else
3184 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3185 inc_amount = -inc_amount;
3187 inc = GEN_INT (inc_amount);
3190 if (! post && REG_P (incloc))
3191 result = incloc;
3192 else
3193 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3194 "INC/DEC result");
3196 if (real_in != result)
3198 /* First copy the location to the result register. */
3199 lra_assert (REG_P (result));
3200 emit_insn (gen_move_insn (result, real_in));
3203 /* We suppose that there are insns to add/sub with the constant
3204 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3205 old reload worked with this assumption. If the assumption
3206 becomes wrong, we should use approach in function
3207 base_plus_disp_to_reg. */
3208 if (in == value)
3210 /* See if we can directly increment INCLOC. */
3211 last = get_last_insn ();
3212 add_insn = emit_insn (plus_p
3213 ? gen_add2_insn (incloc, inc)
3214 : gen_sub2_insn (incloc, inc));
3216 code = recog_memoized (add_insn);
3217 if (code >= 0)
3219 if (! post && result != incloc)
3220 emit_insn (gen_move_insn (result, incloc));
3221 return result;
3223 delete_insns_since (last);
3226 /* If couldn't do the increment directly, must increment in RESULT.
3227 The way we do this depends on whether this is pre- or
3228 post-increment. For pre-increment, copy INCLOC to the reload
3229 register, increment it there, then save back. */
3230 if (! post)
3232 if (real_in != result)
3233 emit_insn (gen_move_insn (result, real_in));
3234 if (plus_p)
3235 emit_insn (gen_add2_insn (result, inc));
3236 else
3237 emit_insn (gen_sub2_insn (result, inc));
3238 if (result != incloc)
3239 emit_insn (gen_move_insn (incloc, result));
3241 else
3243 /* Post-increment.
3245 Because this might be a jump insn or a compare, and because
3246 RESULT may not be available after the insn in an input
3247 reload, we must do the incrementing before the insn being
3248 reloaded for.
3250 We have already copied IN to RESULT. Increment the copy in
3251 RESULT, save that back, then decrement RESULT so it has
3252 the original value. */
3253 if (plus_p)
3254 emit_insn (gen_add2_insn (result, inc));
3255 else
3256 emit_insn (gen_sub2_insn (result, inc));
3257 emit_insn (gen_move_insn (incloc, result));
3258 /* Restore non-modified value for the result. We prefer this
3259 way because it does not require an additional hard
3260 register. */
3261 if (plus_p)
3263 if (CONST_INT_P (inc))
3264 emit_insn (gen_add2_insn (result,
3265 gen_int_mode (-INTVAL (inc),
3266 GET_MODE (result))));
3267 else
3268 emit_insn (gen_sub2_insn (result, inc));
3270 else
3271 emit_insn (gen_add2_insn (result, inc));
3273 return result;
3276 /* Return true if the current move insn does not need processing as we
3277 already know that it satisfies its constraints. */
3278 static bool
3279 simple_move_p (void)
3281 rtx dest, src;
3282 enum reg_class dclass, sclass;
3284 lra_assert (curr_insn_set != NULL_RTX);
3285 dest = SET_DEST (curr_insn_set);
3286 src = SET_SRC (curr_insn_set);
3287 return ((dclass = get_op_class (dest)) != NO_REGS
3288 && (sclass = get_op_class (src)) != NO_REGS
3289 /* The backend guarantees that register moves of cost 2
3290 never need reloads. */
3291 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3294 /* Swap operands NOP and NOP + 1. */
3295 static inline void
3296 swap_operands (int nop)
3298 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3299 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3300 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3301 /* Swap the duplicates too. */
3302 lra_update_dup (curr_id, nop);
3303 lra_update_dup (curr_id, nop + 1);
3306 /* Main entry point of the constraint code: search the body of the
3307 current insn to choose the best alternative. It is mimicking insn
3308 alternative cost calculation model of former reload pass. That is
3309 because machine descriptions were written to use this model. This
3310 model can be changed in future. Make commutative operand exchange
3311 if it is chosen.
3313 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3314 constraints. Return true if any change happened during function
3315 call.
3317 If CHECK_ONLY_P is true then don't do any transformation. Just
3318 check that the insn satisfies all constraints. If the insn does
3319 not satisfy any constraint, return true. */
3320 static bool
3321 curr_insn_transform (bool check_only_p)
3323 int i, j, k;
3324 int n_operands;
3325 int n_alternatives;
3326 int commutative;
3327 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3328 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3329 rtx_insn *before, *after;
3330 bool alt_p = false;
3331 /* Flag that the insn has been changed through a transformation. */
3332 bool change_p;
3333 bool sec_mem_p;
3334 #ifdef SECONDARY_MEMORY_NEEDED
3335 bool use_sec_mem_p;
3336 #endif
3337 int max_regno_before;
3338 int reused_alternative_num;
3340 curr_insn_set = single_set (curr_insn);
3341 if (curr_insn_set != NULL_RTX && simple_move_p ())
3342 return false;
3344 no_input_reloads_p = no_output_reloads_p = false;
3345 goal_alt_number = -1;
3346 change_p = sec_mem_p = false;
3347 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3348 reloads; neither are insns that SET cc0. Insns that use CC0 are
3349 not allowed to have any input reloads. */
3350 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3351 no_output_reloads_p = true;
3353 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3354 no_input_reloads_p = true;
3355 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3356 no_output_reloads_p = true;
3358 n_operands = curr_static_id->n_operands;
3359 n_alternatives = curr_static_id->n_alternatives;
3361 /* Just return "no reloads" if insn has no operands with
3362 constraints. */
3363 if (n_operands == 0 || n_alternatives == 0)
3364 return false;
3366 max_regno_before = max_reg_num ();
3368 for (i = 0; i < n_operands; i++)
3370 goal_alt_matched[i][0] = -1;
3371 goal_alt_matches[i] = -1;
3374 commutative = curr_static_id->commutative;
3376 /* Now see what we need for pseudos that didn't get hard regs or got
3377 the wrong kind of hard reg. For this, we must consider all the
3378 operands together against the register constraints. */
3380 best_losers = best_overall = INT_MAX;
3381 best_reload_sum = 0;
3383 curr_swapped = false;
3384 goal_alt_swapped = false;
3386 if (! check_only_p)
3387 /* Make equivalence substitution and memory subreg elimination
3388 before address processing because an address legitimacy can
3389 depend on memory mode. */
3390 for (i = 0; i < n_operands; i++)
3392 rtx op = *curr_id->operand_loc[i];
3393 rtx subst, old = op;
3394 bool op_change_p = false;
3396 if (GET_CODE (old) == SUBREG)
3397 old = SUBREG_REG (old);
3398 subst = get_equiv_with_elimination (old, curr_insn);
3399 original_subreg_reg_mode[i] = VOIDmode;
3400 if (subst != old)
3402 subst = copy_rtx (subst);
3403 lra_assert (REG_P (old));
3404 if (GET_CODE (op) != SUBREG)
3405 *curr_id->operand_loc[i] = subst;
3406 else
3408 SUBREG_REG (op) = subst;
3409 if (GET_MODE (subst) == VOIDmode)
3410 original_subreg_reg_mode[i] = GET_MODE (old);
3412 if (lra_dump_file != NULL)
3414 fprintf (lra_dump_file,
3415 "Changing pseudo %d in operand %i of insn %u on equiv ",
3416 REGNO (old), i, INSN_UID (curr_insn));
3417 dump_value_slim (lra_dump_file, subst, 1);
3418 fprintf (lra_dump_file, "\n");
3420 op_change_p = change_p = true;
3422 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3424 change_p = true;
3425 lra_update_dup (curr_id, i);
3429 /* Reload address registers and displacements. We do it before
3430 finding an alternative because of memory constraints. */
3431 before = after = NULL;
3432 for (i = 0; i < n_operands; i++)
3433 if (! curr_static_id->operand[i].is_operator
3434 && process_address (i, check_only_p, &before, &after))
3436 if (check_only_p)
3437 return true;
3438 change_p = true;
3439 lra_update_dup (curr_id, i);
3442 if (change_p)
3443 /* If we've changed the instruction then any alternative that
3444 we chose previously may no longer be valid. */
3445 lra_set_used_insn_alternative (curr_insn, -1);
3447 if (! check_only_p && curr_insn_set != NULL_RTX
3448 && check_and_process_move (&change_p, &sec_mem_p))
3449 return change_p;
3451 try_swapped:
3453 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3454 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3455 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3456 reused_alternative_num, INSN_UID (curr_insn));
3458 if (process_alt_operands (reused_alternative_num))
3459 alt_p = true;
3461 if (check_only_p)
3462 return ! alt_p || best_losers != 0;
3464 /* If insn is commutative (it's safe to exchange a certain pair of
3465 operands) then we need to try each alternative twice, the second
3466 time matching those two operands as if we had exchanged them. To
3467 do this, really exchange them in operands.
3469 If we have just tried the alternatives the second time, return
3470 operands to normal and drop through. */
3472 if (reused_alternative_num < 0 && commutative >= 0)
3474 curr_swapped = !curr_swapped;
3475 if (curr_swapped)
3477 swap_operands (commutative);
3478 goto try_swapped;
3480 else
3481 swap_operands (commutative);
3484 if (! alt_p && ! sec_mem_p)
3486 /* No alternative works with reloads?? */
3487 if (INSN_CODE (curr_insn) >= 0)
3488 fatal_insn ("unable to generate reloads for:", curr_insn);
3489 error_for_asm (curr_insn,
3490 "inconsistent operand constraints in an %<asm%>");
3491 /* Avoid further trouble with this insn. */
3492 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3493 lra_invalidate_insn_data (curr_insn);
3494 return true;
3497 /* If the best alternative is with operands 1 and 2 swapped, swap
3498 them. Update the operand numbers of any reloads already
3499 pushed. */
3501 if (goal_alt_swapped)
3503 if (lra_dump_file != NULL)
3504 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3505 INSN_UID (curr_insn));
3507 /* Swap the duplicates too. */
3508 swap_operands (commutative);
3509 change_p = true;
3512 #ifdef SECONDARY_MEMORY_NEEDED
3513 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3514 too conservatively. So we use the secondary memory only if there
3515 is no any alternative without reloads. */
3516 use_sec_mem_p = false;
3517 if (! alt_p)
3518 use_sec_mem_p = true;
3519 else if (sec_mem_p)
3521 for (i = 0; i < n_operands; i++)
3522 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3523 break;
3524 use_sec_mem_p = i < n_operands;
3527 if (use_sec_mem_p)
3529 rtx new_reg, src, dest, rld;
3530 machine_mode sec_mode, rld_mode;
3532 lra_assert (sec_mem_p);
3533 lra_assert (curr_static_id->operand[0].type == OP_OUT
3534 && curr_static_id->operand[1].type == OP_IN);
3535 dest = *curr_id->operand_loc[0];
3536 src = *curr_id->operand_loc[1];
3537 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3538 ? dest : src);
3539 rld_mode = GET_MODE (rld);
3540 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3541 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3542 #else
3543 sec_mode = rld_mode;
3544 #endif
3545 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3546 NO_REGS, "secondary");
3547 /* If the mode is changed, it should be wider. */
3548 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3549 if (sec_mode != rld_mode)
3551 /* If the target says specifically to use another mode for
3552 secondary memory moves we can not reuse the original
3553 insn. */
3554 after = emit_spill_move (false, new_reg, dest);
3555 lra_process_new_insns (curr_insn, NULL, after,
3556 "Inserting the sec. move");
3557 /* We may have non null BEFORE here (e.g. after address
3558 processing. */
3559 push_to_sequence (before);
3560 before = emit_spill_move (true, new_reg, src);
3561 emit_insn (before);
3562 before = get_insns ();
3563 end_sequence ();
3564 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3565 lra_set_insn_deleted (curr_insn);
3567 else if (dest == rld)
3569 *curr_id->operand_loc[0] = new_reg;
3570 after = emit_spill_move (false, new_reg, dest);
3571 lra_process_new_insns (curr_insn, NULL, after,
3572 "Inserting the sec. move");
3574 else
3576 *curr_id->operand_loc[1] = new_reg;
3577 /* See comments above. */
3578 push_to_sequence (before);
3579 before = emit_spill_move (true, new_reg, src);
3580 emit_insn (before);
3581 before = get_insns ();
3582 end_sequence ();
3583 lra_process_new_insns (curr_insn, before, NULL,
3584 "Inserting the sec. move");
3586 lra_update_insn_regno_info (curr_insn);
3587 return true;
3589 #endif
3591 lra_assert (goal_alt_number >= 0);
3592 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3594 if (lra_dump_file != NULL)
3596 const char *p;
3598 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3599 goal_alt_number, INSN_UID (curr_insn));
3600 for (i = 0; i < n_operands; i++)
3602 p = (curr_static_id->operand_alternative
3603 [goal_alt_number * n_operands + i].constraint);
3604 if (*p == '\0')
3605 continue;
3606 fprintf (lra_dump_file, " (%d) ", i);
3607 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3608 fputc (*p, lra_dump_file);
3610 if (INSN_CODE (curr_insn) >= 0
3611 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3612 fprintf (lra_dump_file, " {%s}", p);
3613 if (curr_id->sp_offset != 0)
3614 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3615 curr_id->sp_offset);
3616 fprintf (lra_dump_file, "\n");
3619 /* Right now, for any pair of operands I and J that are required to
3620 match, with J < I, goal_alt_matches[I] is J. Add I to
3621 goal_alt_matched[J]. */
3623 for (i = 0; i < n_operands; i++)
3624 if ((j = goal_alt_matches[i]) >= 0)
3626 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3628 /* We allow matching one output operand and several input
3629 operands. */
3630 lra_assert (k == 0
3631 || (curr_static_id->operand[j].type == OP_OUT
3632 && curr_static_id->operand[i].type == OP_IN
3633 && (curr_static_id->operand
3634 [goal_alt_matched[j][0]].type == OP_IN)));
3635 goal_alt_matched[j][k] = i;
3636 goal_alt_matched[j][k + 1] = -1;
3639 for (i = 0; i < n_operands; i++)
3640 goal_alt_win[i] |= goal_alt_match_win[i];
3642 /* Any constants that aren't allowed and can't be reloaded into
3643 registers are here changed into memory references. */
3644 for (i = 0; i < n_operands; i++)
3645 if (goal_alt_win[i])
3647 int regno;
3648 enum reg_class new_class;
3649 rtx reg = *curr_id->operand_loc[i];
3651 if (GET_CODE (reg) == SUBREG)
3652 reg = SUBREG_REG (reg);
3654 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3656 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3658 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3660 lra_assert (ok_p);
3661 lra_change_class (regno, new_class, " Change to", true);
3665 else
3667 const char *constraint;
3668 char c;
3669 rtx op = *curr_id->operand_loc[i];
3670 rtx subreg = NULL_RTX;
3671 machine_mode mode = curr_operand_mode[i];
3673 if (GET_CODE (op) == SUBREG)
3675 subreg = op;
3676 op = SUBREG_REG (op);
3677 mode = GET_MODE (op);
3680 if (CONST_POOL_OK_P (mode, op)
3681 && ((targetm.preferred_reload_class
3682 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3683 || no_input_reloads_p))
3685 rtx tem = force_const_mem (mode, op);
3687 change_p = true;
3688 if (subreg != NULL_RTX)
3689 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3691 *curr_id->operand_loc[i] = tem;
3692 lra_update_dup (curr_id, i);
3693 process_address (i, false, &before, &after);
3695 /* If the alternative accepts constant pool refs directly
3696 there will be no reload needed at all. */
3697 if (subreg != NULL_RTX)
3698 continue;
3699 /* Skip alternatives before the one requested. */
3700 constraint = (curr_static_id->operand_alternative
3701 [goal_alt_number * n_operands + i].constraint);
3702 for (;
3703 (c = *constraint) && c != ',' && c != '#';
3704 constraint += CONSTRAINT_LEN (c, constraint))
3706 enum constraint_num cn = lookup_constraint (constraint);
3707 if (insn_extra_memory_constraint (cn)
3708 && satisfies_memory_constraint_p (tem, cn))
3709 break;
3711 if (c == '\0' || c == ',' || c == '#')
3712 continue;
3714 goal_alt_win[i] = true;
3718 for (i = 0; i < n_operands; i++)
3720 int regno;
3721 bool optional_p = false;
3722 rtx old, new_reg;
3723 rtx op = *curr_id->operand_loc[i];
3725 if (goal_alt_win[i])
3727 if (goal_alt[i] == NO_REGS
3728 && REG_P (op)
3729 /* When we assign NO_REGS it means that we will not
3730 assign a hard register to the scratch pseudo by
3731 assigment pass and the scratch pseudo will be
3732 spilled. Spilled scratch pseudos are transformed
3733 back to scratches at the LRA end. */
3734 && lra_former_scratch_operand_p (curr_insn, i))
3736 int regno = REGNO (op);
3737 lra_change_class (regno, NO_REGS, " Change to", true);
3738 if (lra_get_regno_hard_regno (regno) >= 0)
3739 /* We don't have to mark all insn affected by the
3740 spilled pseudo as there is only one such insn, the
3741 current one. */
3742 reg_renumber[regno] = -1;
3744 /* We can do an optional reload. If the pseudo got a hard
3745 reg, we might improve the code through inheritance. If
3746 it does not get a hard register we coalesce memory/memory
3747 moves later. Ignore move insns to avoid cycling. */
3748 if (! lra_simple_p
3749 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3750 && goal_alt[i] != NO_REGS && REG_P (op)
3751 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3752 && regno < new_regno_start
3753 && ! lra_former_scratch_p (regno)
3754 && reg_renumber[regno] < 0
3755 /* Check that the optional reload pseudo will be able to
3756 hold given mode value. */
3757 && ! (prohibited_class_reg_set_mode_p
3758 (goal_alt[i], reg_class_contents[goal_alt[i]],
3759 PSEUDO_REGNO_MODE (regno)))
3760 && (curr_insn_set == NULL_RTX
3761 || !((REG_P (SET_SRC (curr_insn_set))
3762 || MEM_P (SET_SRC (curr_insn_set))
3763 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3764 && (REG_P (SET_DEST (curr_insn_set))
3765 || MEM_P (SET_DEST (curr_insn_set))
3766 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3767 optional_p = true;
3768 else
3769 continue;
3772 /* Operands that match previous ones have already been handled. */
3773 if (goal_alt_matches[i] >= 0)
3774 continue;
3776 /* We should not have an operand with a non-offsettable address
3777 appearing where an offsettable address will do. It also may
3778 be a case when the address should be special in other words
3779 not a general one (e.g. it needs no index reg). */
3780 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3782 enum reg_class rclass;
3783 rtx *loc = &XEXP (op, 0);
3784 enum rtx_code code = GET_CODE (*loc);
3786 push_to_sequence (before);
3787 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3788 MEM, SCRATCH);
3789 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3790 new_reg = emit_inc (rclass, *loc, *loc,
3791 /* This value does not matter for MODIFY. */
3792 GET_MODE_SIZE (GET_MODE (op)));
3793 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3794 "offsetable address", &new_reg))
3795 lra_emit_move (new_reg, *loc);
3796 before = get_insns ();
3797 end_sequence ();
3798 *loc = new_reg;
3799 lra_update_dup (curr_id, i);
3801 else if (goal_alt_matched[i][0] == -1)
3803 machine_mode mode;
3804 rtx reg, *loc;
3805 int hard_regno, byte;
3806 enum op_type type = curr_static_id->operand[i].type;
3808 loc = curr_id->operand_loc[i];
3809 mode = curr_operand_mode[i];
3810 if (GET_CODE (*loc) == SUBREG)
3812 reg = SUBREG_REG (*loc);
3813 byte = SUBREG_BYTE (*loc);
3814 if (REG_P (reg)
3815 /* Strict_low_part requires reload the register not
3816 the sub-register. */
3817 && (curr_static_id->operand[i].strict_low
3818 || (GET_MODE_SIZE (mode)
3819 <= GET_MODE_SIZE (GET_MODE (reg))
3820 && (hard_regno
3821 = get_try_hard_regno (REGNO (reg))) >= 0
3822 && (simplify_subreg_regno
3823 (hard_regno,
3824 GET_MODE (reg), byte, mode) < 0)
3825 && (goal_alt[i] == NO_REGS
3826 || (simplify_subreg_regno
3827 (ira_class_hard_regs[goal_alt[i]][0],
3828 GET_MODE (reg), byte, mode) >= 0)))))
3830 if (type == OP_OUT)
3831 type = OP_INOUT;
3832 loc = &SUBREG_REG (*loc);
3833 mode = GET_MODE (*loc);
3836 old = *loc;
3837 if (get_reload_reg (type, mode, old, goal_alt[i],
3838 loc != curr_id->operand_loc[i], "", &new_reg)
3839 && type != OP_OUT)
3841 push_to_sequence (before);
3842 lra_emit_move (new_reg, old);
3843 before = get_insns ();
3844 end_sequence ();
3846 *loc = new_reg;
3847 if (type != OP_IN
3848 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3850 start_sequence ();
3851 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3852 emit_insn (after);
3853 after = get_insns ();
3854 end_sequence ();
3855 *loc = new_reg;
3857 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3858 if (goal_alt_dont_inherit_ops[j] == i)
3860 lra_set_regno_unique_value (REGNO (new_reg));
3861 break;
3863 lra_update_dup (curr_id, i);
3865 else if (curr_static_id->operand[i].type == OP_IN
3866 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3867 == OP_OUT))
3869 /* generate reloads for input and matched outputs. */
3870 match_inputs[0] = i;
3871 match_inputs[1] = -1;
3872 match_reload (goal_alt_matched[i][0], match_inputs,
3873 goal_alt[i], &before, &after);
3875 else if (curr_static_id->operand[i].type == OP_OUT
3876 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3877 == OP_IN))
3878 /* Generate reloads for output and matched inputs. */
3879 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3880 else if (curr_static_id->operand[i].type == OP_IN
3881 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3882 == OP_IN))
3884 /* Generate reloads for matched inputs. */
3885 match_inputs[0] = i;
3886 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3887 match_inputs[j + 1] = k;
3888 match_inputs[j + 1] = -1;
3889 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3891 else
3892 /* We must generate code in any case when function
3893 process_alt_operands decides that it is possible. */
3894 gcc_unreachable ();
3895 if (optional_p)
3897 lra_assert (REG_P (op));
3898 regno = REGNO (op);
3899 op = *curr_id->operand_loc[i]; /* Substitution. */
3900 if (GET_CODE (op) == SUBREG)
3901 op = SUBREG_REG (op);
3902 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3903 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3904 lra_reg_info[REGNO (op)].restore_regno = regno;
3905 if (lra_dump_file != NULL)
3906 fprintf (lra_dump_file,
3907 " Making reload reg %d for reg %d optional\n",
3908 REGNO (op), regno);
3911 if (before != NULL_RTX || after != NULL_RTX
3912 || max_regno_before != max_reg_num ())
3913 change_p = true;
3914 if (change_p)
3916 lra_update_operator_dups (curr_id);
3917 /* Something changes -- process the insn. */
3918 lra_update_insn_regno_info (curr_insn);
3920 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3921 return change_p;
3924 /* Return true if INSN satisfies all constraints. In other words, no
3925 reload insns are needed. */
3926 bool
3927 lra_constrain_insn (rtx_insn *insn)
3929 int saved_new_regno_start = new_regno_start;
3930 int saved_new_insn_uid_start = new_insn_uid_start;
3931 bool change_p;
3933 curr_insn = insn;
3934 curr_id = lra_get_insn_recog_data (curr_insn);
3935 curr_static_id = curr_id->insn_static_data;
3936 new_insn_uid_start = get_max_uid ();
3937 new_regno_start = max_reg_num ();
3938 change_p = curr_insn_transform (true);
3939 new_regno_start = saved_new_regno_start;
3940 new_insn_uid_start = saved_new_insn_uid_start;
3941 return ! change_p;
3944 /* Return true if X is in LIST. */
3945 static bool
3946 in_list_p (rtx x, rtx list)
3948 for (; list != NULL_RTX; list = XEXP (list, 1))
3949 if (XEXP (list, 0) == x)
3950 return true;
3951 return false;
3954 /* Return true if X contains an allocatable hard register (if
3955 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3956 static bool
3957 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3959 int i, j;
3960 const char *fmt;
3961 enum rtx_code code;
3963 code = GET_CODE (x);
3964 if (REG_P (x))
3966 int regno = REGNO (x);
3967 HARD_REG_SET alloc_regs;
3969 if (hard_reg_p)
3971 if (regno >= FIRST_PSEUDO_REGISTER)
3972 regno = lra_get_regno_hard_regno (regno);
3973 if (regno < 0)
3974 return false;
3975 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3976 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3978 else
3980 if (regno < FIRST_PSEUDO_REGISTER)
3981 return false;
3982 if (! spilled_p)
3983 return true;
3984 return lra_get_regno_hard_regno (regno) < 0;
3987 fmt = GET_RTX_FORMAT (code);
3988 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3990 if (fmt[i] == 'e')
3992 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3993 return true;
3995 else if (fmt[i] == 'E')
3997 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3998 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3999 return true;
4002 return false;
4005 /* Return true if X contains a symbol reg. */
4006 static bool
4007 contains_symbol_ref_p (rtx x)
4009 int i, j;
4010 const char *fmt;
4011 enum rtx_code code;
4013 code = GET_CODE (x);
4014 if (code == SYMBOL_REF)
4015 return true;
4016 fmt = GET_RTX_FORMAT (code);
4017 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4019 if (fmt[i] == 'e')
4021 if (contains_symbol_ref_p (XEXP (x, i)))
4022 return true;
4024 else if (fmt[i] == 'E')
4026 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4027 if (contains_symbol_ref_p (XVECEXP (x, i, j)))
4028 return true;
4031 return false;
4034 /* Process all regs in location *LOC and change them on equivalent
4035 substitution. Return true if any change was done. */
4036 static bool
4037 loc_equivalence_change_p (rtx *loc)
4039 rtx subst, reg, x = *loc;
4040 bool result = false;
4041 enum rtx_code code = GET_CODE (x);
4042 const char *fmt;
4043 int i, j;
4045 if (code == SUBREG)
4047 reg = SUBREG_REG (x);
4048 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4049 && GET_MODE (subst) == VOIDmode)
4051 /* We cannot reload debug location. Simplify subreg here
4052 while we know the inner mode. */
4053 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4054 GET_MODE (reg), SUBREG_BYTE (x));
4055 return true;
4058 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4060 *loc = subst;
4061 return true;
4064 /* Scan all the operand sub-expressions. */
4065 fmt = GET_RTX_FORMAT (code);
4066 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4068 if (fmt[i] == 'e')
4069 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4070 else if (fmt[i] == 'E')
4071 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4072 result
4073 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4075 return result;
4078 /* Similar to loc_equivalence_change_p, but for use as
4079 simplify_replace_fn_rtx callback. DATA is insn for which the
4080 elimination is done. If it null we don't do the elimination. */
4081 static rtx
4082 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4084 if (!REG_P (loc))
4085 return NULL_RTX;
4087 rtx subst = (data == NULL
4088 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4089 if (subst != loc)
4090 return subst;
4092 return NULL_RTX;
4095 /* Maximum number of generated reload insns per an insn. It is for
4096 preventing this pass cycling in a bug case. */
4097 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4099 /* The current iteration number of this LRA pass. */
4100 int lra_constraint_iter;
4102 /* True if we substituted equiv which needs checking register
4103 allocation correctness because the equivalent value contains
4104 allocatable hard registers or when we restore multi-register
4105 pseudo. */
4106 bool lra_risky_transformations_p;
4108 /* Return true if REGNO is referenced in more than one block. */
4109 static bool
4110 multi_block_pseudo_p (int regno)
4112 basic_block bb = NULL;
4113 unsigned int uid;
4114 bitmap_iterator bi;
4116 if (regno < FIRST_PSEUDO_REGISTER)
4117 return false;
4119 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4120 if (bb == NULL)
4121 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4122 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4123 return true;
4124 return false;
4127 /* Return true if LIST contains a deleted insn. */
4128 static bool
4129 contains_deleted_insn_p (rtx_insn_list *list)
4131 for (; list != NULL_RTX; list = list->next ())
4132 if (NOTE_P (list->insn ())
4133 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4134 return true;
4135 return false;
4138 /* Return true if X contains a pseudo dying in INSN. */
4139 static bool
4140 dead_pseudo_p (rtx x, rtx_insn *insn)
4142 int i, j;
4143 const char *fmt;
4144 enum rtx_code code;
4146 if (REG_P (x))
4147 return (insn != NULL_RTX
4148 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4149 code = GET_CODE (x);
4150 fmt = GET_RTX_FORMAT (code);
4151 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4153 if (fmt[i] == 'e')
4155 if (dead_pseudo_p (XEXP (x, i), insn))
4156 return true;
4158 else if (fmt[i] == 'E')
4160 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4161 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4162 return true;
4165 return false;
4168 /* Return true if INSN contains a dying pseudo in INSN right hand
4169 side. */
4170 static bool
4171 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4173 rtx set = single_set (insn);
4175 gcc_assert (set != NULL);
4176 return dead_pseudo_p (SET_SRC (set), insn);
4179 /* Return true if any init insn of REGNO contains a dying pseudo in
4180 insn right hand side. */
4181 static bool
4182 init_insn_rhs_dead_pseudo_p (int regno)
4184 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4186 if (insns == NULL)
4187 return false;
4188 for (; insns != NULL_RTX; insns = insns->next ())
4189 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4190 return true;
4191 return false;
4194 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4195 reverse only if we have one init insn with given REGNO as a
4196 source. */
4197 static bool
4198 reverse_equiv_p (int regno)
4200 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4201 rtx set;
4203 if (insns == NULL)
4204 return false;
4205 if (! INSN_P (insns->insn ())
4206 || insns->next () != NULL)
4207 return false;
4208 if ((set = single_set (insns->insn ())) == NULL_RTX)
4209 return false;
4210 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4213 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4214 call this function only for non-reverse equivalence. */
4215 static bool
4216 contains_reloaded_insn_p (int regno)
4218 rtx set;
4219 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4221 for (; list != NULL; list = list->next ())
4222 if ((set = single_set (list->insn ())) == NULL_RTX
4223 || ! REG_P (SET_DEST (set))
4224 || (int) REGNO (SET_DEST (set)) != regno)
4225 return true;
4226 return false;
4229 /* Entry function of LRA constraint pass. Return true if the
4230 constraint pass did change the code. */
4231 bool
4232 lra_constraints (bool first_p)
4234 bool changed_p;
4235 int i, hard_regno, new_insns_num;
4236 unsigned int min_len, new_min_len, uid;
4237 rtx set, x, reg, dest_reg;
4238 basic_block last_bb;
4239 bitmap_head equiv_insn_bitmap;
4240 bitmap_iterator bi;
4242 lra_constraint_iter++;
4243 if (lra_dump_file != NULL)
4244 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4245 lra_constraint_iter);
4246 changed_p = false;
4247 if (pic_offset_table_rtx
4248 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4249 lra_risky_transformations_p = true;
4250 else
4251 lra_risky_transformations_p = false;
4252 new_insn_uid_start = get_max_uid ();
4253 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4254 /* Mark used hard regs for target stack size calulations. */
4255 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4256 if (lra_reg_info[i].nrefs != 0
4257 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4259 int j, nregs;
4261 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4262 for (j = 0; j < nregs; j++)
4263 df_set_regs_ever_live (hard_regno + j, true);
4265 /* Do elimination before the equivalence processing as we can spill
4266 some pseudos during elimination. */
4267 lra_eliminate (false, first_p);
4268 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4269 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4270 if (lra_reg_info[i].nrefs != 0)
4272 ira_reg_equiv[i].profitable_p = true;
4273 reg = regno_reg_rtx[i];
4274 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4276 bool pseudo_p = contains_reg_p (x, false, false);
4278 /* After RTL transformation, we can not guarantee that
4279 pseudo in the substitution was not reloaded which might
4280 make equivalence invalid. For example, in reverse
4281 equiv of p0
4283 p0 <- ...
4285 equiv_mem <- p0
4287 the memory address register was reloaded before the 2nd
4288 insn. */
4289 if ((! first_p && pseudo_p)
4290 /* We don't use DF for compilation speed sake. So it
4291 is problematic to update live info when we use an
4292 equivalence containing pseudos in more than one
4293 BB. */
4294 || (pseudo_p && multi_block_pseudo_p (i))
4295 /* If an init insn was deleted for some reason, cancel
4296 the equiv. We could update the equiv insns after
4297 transformations including an equiv insn deletion
4298 but it is not worthy as such cases are extremely
4299 rare. */
4300 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4301 /* If it is not a reverse equivalence, we check that a
4302 pseudo in rhs of the init insn is not dying in the
4303 insn. Otherwise, the live info at the beginning of
4304 the corresponding BB might be wrong after we
4305 removed the insn. When the equiv can be a
4306 constant, the right hand side of the init insn can
4307 be a pseudo. */
4308 || (! reverse_equiv_p (i)
4309 && (init_insn_rhs_dead_pseudo_p (i)
4310 /* If we reloaded the pseudo in an equivalence
4311 init insn, we can not remove the equiv init
4312 insns and the init insns might write into
4313 const memory in this case. */
4314 || contains_reloaded_insn_p (i)))
4315 /* Prevent access beyond equivalent memory for
4316 paradoxical subregs. */
4317 || (MEM_P (x)
4318 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4319 > GET_MODE_SIZE (GET_MODE (x))))
4320 || (pic_offset_table_rtx
4321 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4322 && (targetm.preferred_reload_class
4323 (x, lra_get_allocno_class (i)) == NO_REGS))
4324 || contains_symbol_ref_p (x))))
4325 ira_reg_equiv[i].defined_p = false;
4326 if (contains_reg_p (x, false, true))
4327 ira_reg_equiv[i].profitable_p = false;
4328 if (get_equiv (reg) != reg)
4329 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4332 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4333 update_equiv (i);
4334 /* We should add all insns containing pseudos which should be
4335 substituted by their equivalences. */
4336 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4337 lra_push_insn_by_uid (uid);
4338 min_len = lra_insn_stack_length ();
4339 new_insns_num = 0;
4340 last_bb = NULL;
4341 changed_p = false;
4342 while ((new_min_len = lra_insn_stack_length ()) != 0)
4344 curr_insn = lra_pop_insn ();
4345 --new_min_len;
4346 curr_bb = BLOCK_FOR_INSN (curr_insn);
4347 if (curr_bb != last_bb)
4349 last_bb = curr_bb;
4350 bb_reload_num = lra_curr_reload_num;
4352 if (min_len > new_min_len)
4354 min_len = new_min_len;
4355 new_insns_num = 0;
4357 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4358 internal_error
4359 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4360 MAX_RELOAD_INSNS_NUMBER);
4361 new_insns_num++;
4362 if (DEBUG_INSN_P (curr_insn))
4364 /* We need to check equivalence in debug insn and change
4365 pseudo to the equivalent value if necessary. */
4366 curr_id = lra_get_insn_recog_data (curr_insn);
4367 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4369 rtx old = *curr_id->operand_loc[0];
4370 *curr_id->operand_loc[0]
4371 = simplify_replace_fn_rtx (old, NULL_RTX,
4372 loc_equivalence_callback, curr_insn);
4373 if (old != *curr_id->operand_loc[0])
4375 lra_update_insn_regno_info (curr_insn);
4376 changed_p = true;
4380 else if (INSN_P (curr_insn))
4382 if ((set = single_set (curr_insn)) != NULL_RTX)
4384 dest_reg = SET_DEST (set);
4385 /* The equivalence pseudo could be set up as SUBREG in a
4386 case when it is a call restore insn in a mode
4387 different from the pseudo mode. */
4388 if (GET_CODE (dest_reg) == SUBREG)
4389 dest_reg = SUBREG_REG (dest_reg);
4390 if ((REG_P (dest_reg)
4391 && (x = get_equiv (dest_reg)) != dest_reg
4392 /* Remove insns which set up a pseudo whose value
4393 can not be changed. Such insns might be not in
4394 init_insns because we don't update equiv data
4395 during insn transformations.
4397 As an example, let suppose that a pseudo got
4398 hard register and on the 1st pass was not
4399 changed to equivalent constant. We generate an
4400 additional insn setting up the pseudo because of
4401 secondary memory movement. Then the pseudo is
4402 spilled and we use the equiv constant. In this
4403 case we should remove the additional insn and
4404 this insn is not init_insns list. */
4405 && (! MEM_P (x) || MEM_READONLY_P (x)
4406 /* Check that this is actually an insn setting
4407 up the equivalence. */
4408 || in_list_p (curr_insn,
4409 ira_reg_equiv
4410 [REGNO (dest_reg)].init_insns)))
4411 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4412 && in_list_p (curr_insn,
4413 ira_reg_equiv
4414 [REGNO (SET_SRC (set))].init_insns)))
4416 /* This is equiv init insn of pseudo which did not get a
4417 hard register -- remove the insn. */
4418 if (lra_dump_file != NULL)
4420 fprintf (lra_dump_file,
4421 " Removing equiv init insn %i (freq=%d)\n",
4422 INSN_UID (curr_insn),
4423 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4424 dump_insn_slim (lra_dump_file, curr_insn);
4426 if (contains_reg_p (x, true, false))
4427 lra_risky_transformations_p = true;
4428 lra_set_insn_deleted (curr_insn);
4429 continue;
4432 curr_id = lra_get_insn_recog_data (curr_insn);
4433 curr_static_id = curr_id->insn_static_data;
4434 init_curr_insn_input_reloads ();
4435 init_curr_operand_mode ();
4436 if (curr_insn_transform (false))
4437 changed_p = true;
4438 /* Check non-transformed insns too for equiv change as USE
4439 or CLOBBER don't need reloads but can contain pseudos
4440 being changed on their equivalences. */
4441 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4442 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4444 lra_update_insn_regno_info (curr_insn);
4445 changed_p = true;
4449 bitmap_clear (&equiv_insn_bitmap);
4450 /* If we used a new hard regno, changed_p should be true because the
4451 hard reg is assigned to a new pseudo. */
4452 #ifdef ENABLE_CHECKING
4453 if (! changed_p)
4455 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4456 if (lra_reg_info[i].nrefs != 0
4457 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4459 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4461 for (j = 0; j < nregs; j++)
4462 lra_assert (df_regs_ever_live_p (hard_regno + j));
4465 #endif
4466 return changed_p;
4469 /* Initiate the LRA constraint pass. It is done once per
4470 function. */
4471 void
4472 lra_constraints_init (void)
4476 /* Finalize the LRA constraint pass. It is done once per
4477 function. */
4478 void
4479 lra_constraints_finish (void)
4485 /* This page contains code to do inheritance/split
4486 transformations. */
4488 /* Number of reloads passed so far in current EBB. */
4489 static int reloads_num;
4491 /* Number of calls passed so far in current EBB. */
4492 static int calls_num;
4494 /* Current reload pseudo check for validity of elements in
4495 USAGE_INSNS. */
4496 static int curr_usage_insns_check;
4498 /* Info about last usage of registers in EBB to do inheritance/split
4499 transformation. Inheritance transformation is done from a spilled
4500 pseudo and split transformations from a hard register or a pseudo
4501 assigned to a hard register. */
4502 struct usage_insns
4504 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4505 value INSNS is valid. The insns is chain of optional debug insns
4506 and a finishing non-debug insn using the corresponding reg. The
4507 value is also used to mark the registers which are set up in the
4508 current insn. The negated insn uid is used for this. */
4509 int check;
4510 /* Value of global reloads_num at the last insn in INSNS. */
4511 int reloads_num;
4512 /* Value of global reloads_nums at the last insn in INSNS. */
4513 int calls_num;
4514 /* It can be true only for splitting. And it means that the restore
4515 insn should be put after insn given by the following member. */
4516 bool after_p;
4517 /* Next insns in the current EBB which use the original reg and the
4518 original reg value is not changed between the current insn and
4519 the next insns. In order words, e.g. for inheritance, if we need
4520 to use the original reg value again in the next insns we can try
4521 to use the value in a hard register from a reload insn of the
4522 current insn. */
4523 rtx insns;
4526 /* Map: regno -> corresponding pseudo usage insns. */
4527 static struct usage_insns *usage_insns;
4529 static void
4530 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4532 usage_insns[regno].check = curr_usage_insns_check;
4533 usage_insns[regno].insns = insn;
4534 usage_insns[regno].reloads_num = reloads_num;
4535 usage_insns[regno].calls_num = calls_num;
4536 usage_insns[regno].after_p = after_p;
4539 /* The function is used to form list REGNO usages which consists of
4540 optional debug insns finished by a non-debug insn using REGNO.
4541 RELOADS_NUM is current number of reload insns processed so far. */
4542 static void
4543 add_next_usage_insn (int regno, rtx insn, int reloads_num)
4545 rtx next_usage_insns;
4547 if (usage_insns[regno].check == curr_usage_insns_check
4548 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4549 && DEBUG_INSN_P (insn))
4551 /* Check that we did not add the debug insn yet. */
4552 if (next_usage_insns != insn
4553 && (GET_CODE (next_usage_insns) != INSN_LIST
4554 || XEXP (next_usage_insns, 0) != insn))
4555 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4556 next_usage_insns);
4558 else if (NONDEBUG_INSN_P (insn))
4559 setup_next_usage_insn (regno, insn, reloads_num, false);
4560 else
4561 usage_insns[regno].check = 0;
4564 /* Return first non-debug insn in list USAGE_INSNS. */
4565 static rtx_insn *
4566 skip_usage_debug_insns (rtx usage_insns)
4568 rtx insn;
4570 /* Skip debug insns. */
4571 for (insn = usage_insns;
4572 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4573 insn = XEXP (insn, 1))
4575 return safe_as_a <rtx_insn *> (insn);
4578 /* Return true if we need secondary memory moves for insn in
4579 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4580 into the insn. */
4581 static bool
4582 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4583 rtx usage_insns ATTRIBUTE_UNUSED)
4585 #ifndef SECONDARY_MEMORY_NEEDED
4586 return false;
4587 #else
4588 rtx_insn *insn;
4589 rtx set, dest;
4590 enum reg_class cl;
4592 if (inher_cl == ALL_REGS
4593 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4594 return false;
4595 lra_assert (INSN_P (insn));
4596 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4597 return false;
4598 dest = SET_DEST (set);
4599 if (! REG_P (dest))
4600 return false;
4601 lra_assert (inher_cl != NO_REGS);
4602 cl = get_reg_class (REGNO (dest));
4603 return (cl != NO_REGS && cl != ALL_REGS
4604 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4605 #endif
4608 /* Registers involved in inheritance/split in the current EBB
4609 (inheritance/split pseudos and original registers). */
4610 static bitmap_head check_only_regs;
4612 /* Do inheritance transformations for insn INSN, which defines (if
4613 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4614 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4615 form as the "insns" field of usage_insns. Return true if we
4616 succeed in such transformation.
4618 The transformations look like:
4620 p <- ... i <- ...
4621 ... p <- i (new insn)
4622 ... =>
4623 <- ... p ... <- ... i ...
4625 ... i <- p (new insn)
4626 <- ... p ... <- ... i ...
4627 ... =>
4628 <- ... p ... <- ... i ...
4629 where p is a spilled original pseudo and i is a new inheritance pseudo.
4632 The inheritance pseudo has the smallest class of two classes CL and
4633 class of ORIGINAL REGNO. */
4634 static bool
4635 inherit_reload_reg (bool def_p, int original_regno,
4636 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4638 if (optimize_function_for_size_p (cfun))
4639 return false;
4641 enum reg_class rclass = lra_get_allocno_class (original_regno);
4642 rtx original_reg = regno_reg_rtx[original_regno];
4643 rtx new_reg, usage_insn;
4644 rtx_insn *new_insns;
4646 lra_assert (! usage_insns[original_regno].after_p);
4647 if (lra_dump_file != NULL)
4648 fprintf (lra_dump_file,
4649 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4650 if (! ira_reg_classes_intersect_p[cl][rclass])
4652 if (lra_dump_file != NULL)
4654 fprintf (lra_dump_file,
4655 " Rejecting inheritance for %d "
4656 "because of disjoint classes %s and %s\n",
4657 original_regno, reg_class_names[cl],
4658 reg_class_names[rclass]);
4659 fprintf (lra_dump_file,
4660 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4662 return false;
4664 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4665 /* We don't use a subset of two classes because it can be
4666 NO_REGS. This transformation is still profitable in most
4667 cases even if the classes are not intersected as register
4668 move is probably cheaper than a memory load. */
4669 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4671 if (lra_dump_file != NULL)
4672 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4673 reg_class_names[cl], reg_class_names[rclass]);
4675 rclass = cl;
4677 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4679 /* Reject inheritance resulting in secondary memory moves.
4680 Otherwise, there is a danger in LRA cycling. Also such
4681 transformation will be unprofitable. */
4682 if (lra_dump_file != NULL)
4684 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4685 rtx set = single_set (insn);
4687 lra_assert (set != NULL_RTX);
4689 rtx dest = SET_DEST (set);
4691 lra_assert (REG_P (dest));
4692 fprintf (lra_dump_file,
4693 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4694 "as secondary mem is needed\n",
4695 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4696 original_regno, reg_class_names[rclass]);
4697 fprintf (lra_dump_file,
4698 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4700 return false;
4702 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4703 rclass, "inheritance");
4704 start_sequence ();
4705 if (def_p)
4706 lra_emit_move (original_reg, new_reg);
4707 else
4708 lra_emit_move (new_reg, original_reg);
4709 new_insns = get_insns ();
4710 end_sequence ();
4711 if (NEXT_INSN (new_insns) != NULL_RTX)
4713 if (lra_dump_file != NULL)
4715 fprintf (lra_dump_file,
4716 " Rejecting inheritance %d->%d "
4717 "as it results in 2 or more insns:\n",
4718 original_regno, REGNO (new_reg));
4719 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4720 fprintf (lra_dump_file,
4721 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4723 return false;
4725 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg);
4726 lra_update_insn_regno_info (insn);
4727 if (! def_p)
4728 /* We now have a new usage insn for original regno. */
4729 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4730 if (lra_dump_file != NULL)
4731 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4732 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4733 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4734 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4735 bitmap_set_bit (&check_only_regs, original_regno);
4736 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4737 if (def_p)
4738 lra_process_new_insns (insn, NULL, new_insns,
4739 "Add original<-inheritance");
4740 else
4741 lra_process_new_insns (insn, new_insns, NULL,
4742 "Add inheritance<-original");
4743 while (next_usage_insns != NULL_RTX)
4745 if (GET_CODE (next_usage_insns) != INSN_LIST)
4747 usage_insn = next_usage_insns;
4748 lra_assert (NONDEBUG_INSN_P (usage_insn));
4749 next_usage_insns = NULL;
4751 else
4753 usage_insn = XEXP (next_usage_insns, 0);
4754 lra_assert (DEBUG_INSN_P (usage_insn));
4755 next_usage_insns = XEXP (next_usage_insns, 1);
4757 lra_substitute_pseudo (&usage_insn, original_regno, new_reg);
4758 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4759 if (lra_dump_file != NULL)
4761 fprintf (lra_dump_file,
4762 " Inheritance reuse change %d->%d (bb%d):\n",
4763 original_regno, REGNO (new_reg),
4764 BLOCK_FOR_INSN (usage_insn)->index);
4765 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
4768 if (lra_dump_file != NULL)
4769 fprintf (lra_dump_file,
4770 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4771 return true;
4774 /* Return true if we need a caller save/restore for pseudo REGNO which
4775 was assigned to a hard register. */
4776 static inline bool
4777 need_for_call_save_p (int regno)
4779 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4780 return (usage_insns[regno].calls_num < calls_num
4781 && (overlaps_hard_reg_set_p
4782 ((flag_ipa_ra &&
4783 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4784 ? lra_reg_info[regno].actual_call_used_reg_set
4785 : call_used_reg_set,
4786 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4787 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4788 PSEUDO_REGNO_MODE (regno))));
4791 /* Global registers occurring in the current EBB. */
4792 static bitmap_head ebb_global_regs;
4794 /* Return true if we need a split for hard register REGNO or pseudo
4795 REGNO which was assigned to a hard register.
4796 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4797 used for reloads since the EBB end. It is an approximation of the
4798 used hard registers in the split range. The exact value would
4799 require expensive calculations. If we were aggressive with
4800 splitting because of the approximation, the split pseudo will save
4801 the same hard register assignment and will be removed in the undo
4802 pass. We still need the approximation because too aggressive
4803 splitting would result in too inaccurate cost calculation in the
4804 assignment pass because of too many generated moves which will be
4805 probably removed in the undo pass. */
4806 static inline bool
4807 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4809 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4811 lra_assert (hard_regno >= 0);
4812 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4813 /* Don't split eliminable hard registers, otherwise we can
4814 split hard registers like hard frame pointer, which
4815 lives on BB start/end according to DF-infrastructure,
4816 when there is a pseudo assigned to the register and
4817 living in the same BB. */
4818 && (regno >= FIRST_PSEUDO_REGISTER
4819 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4820 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4821 /* Don't split call clobbered hard regs living through
4822 calls, otherwise we might have a check problem in the
4823 assign sub-pass as in the most cases (exception is a
4824 situation when lra_risky_transformations_p value is
4825 true) the assign pass assumes that all pseudos living
4826 through calls are assigned to call saved hard regs. */
4827 && (regno >= FIRST_PSEUDO_REGISTER
4828 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4829 || usage_insns[regno].calls_num == calls_num)
4830 /* We need at least 2 reloads to make pseudo splitting
4831 profitable. We should provide hard regno splitting in
4832 any case to solve 1st insn scheduling problem when
4833 moving hard register definition up might result in
4834 impossibility to find hard register for reload pseudo of
4835 small register class. */
4836 && (usage_insns[regno].reloads_num
4837 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4838 && (regno < FIRST_PSEUDO_REGISTER
4839 /* For short living pseudos, spilling + inheritance can
4840 be considered a substitution for splitting.
4841 Therefore we do not splitting for local pseudos. It
4842 decreases also aggressiveness of splitting. The
4843 minimal number of references is chosen taking into
4844 account that for 2 references splitting has no sense
4845 as we can just spill the pseudo. */
4846 || (regno >= FIRST_PSEUDO_REGISTER
4847 && lra_reg_info[regno].nrefs > 3
4848 && bitmap_bit_p (&ebb_global_regs, regno))))
4849 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4852 /* Return class for the split pseudo created from original pseudo with
4853 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4854 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4855 results in no secondary memory movements. */
4856 static enum reg_class
4857 choose_split_class (enum reg_class allocno_class,
4858 int hard_regno ATTRIBUTE_UNUSED,
4859 machine_mode mode ATTRIBUTE_UNUSED)
4861 #ifndef SECONDARY_MEMORY_NEEDED
4862 return allocno_class;
4863 #else
4864 int i;
4865 enum reg_class cl, best_cl = NO_REGS;
4866 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4867 = REGNO_REG_CLASS (hard_regno);
4869 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4870 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4871 return allocno_class;
4872 for (i = 0;
4873 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4874 i++)
4875 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4876 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4877 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4878 && (best_cl == NO_REGS
4879 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4880 best_cl = cl;
4881 return best_cl;
4882 #endif
4885 /* Do split transformations for insn INSN, which defines or uses
4886 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4887 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4888 "insns" field of usage_insns.
4890 The transformations look like:
4892 p <- ... p <- ...
4893 ... s <- p (new insn -- save)
4894 ... =>
4895 ... p <- s (new insn -- restore)
4896 <- ... p ... <- ... p ...
4898 <- ... p ... <- ... p ...
4899 ... s <- p (new insn -- save)
4900 ... =>
4901 ... p <- s (new insn -- restore)
4902 <- ... p ... <- ... p ...
4904 where p is an original pseudo got a hard register or a hard
4905 register and s is a new split pseudo. The save is put before INSN
4906 if BEFORE_P is true. Return true if we succeed in such
4907 transformation. */
4908 static bool
4909 split_reg (bool before_p, int original_regno, rtx_insn *insn,
4910 rtx next_usage_insns)
4912 enum reg_class rclass;
4913 rtx original_reg;
4914 int hard_regno, nregs;
4915 rtx new_reg, usage_insn;
4916 rtx_insn *restore, *save;
4917 bool after_p;
4918 bool call_save_p;
4920 if (original_regno < FIRST_PSEUDO_REGISTER)
4922 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4923 hard_regno = original_regno;
4924 call_save_p = false;
4925 nregs = 1;
4927 else
4929 hard_regno = reg_renumber[original_regno];
4930 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4931 rclass = lra_get_allocno_class (original_regno);
4932 original_reg = regno_reg_rtx[original_regno];
4933 call_save_p = need_for_call_save_p (original_regno);
4935 original_reg = regno_reg_rtx[original_regno];
4936 lra_assert (hard_regno >= 0);
4937 if (lra_dump_file != NULL)
4938 fprintf (lra_dump_file,
4939 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4940 if (call_save_p)
4942 machine_mode mode = GET_MODE (original_reg);
4944 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4945 hard_regno_nregs[hard_regno][mode],
4946 mode);
4947 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4949 else
4951 rclass = choose_split_class (rclass, hard_regno,
4952 GET_MODE (original_reg));
4953 if (rclass == NO_REGS)
4955 if (lra_dump_file != NULL)
4957 fprintf (lra_dump_file,
4958 " Rejecting split of %d(%s): "
4959 "no good reg class for %d(%s)\n",
4960 original_regno,
4961 reg_class_names[lra_get_allocno_class (original_regno)],
4962 hard_regno,
4963 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4964 fprintf
4965 (lra_dump_file,
4966 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4968 return false;
4970 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4971 rclass, "split");
4972 reg_renumber[REGNO (new_reg)] = hard_regno;
4974 save = emit_spill_move (true, new_reg, original_reg);
4975 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
4977 if (lra_dump_file != NULL)
4979 fprintf
4980 (lra_dump_file,
4981 " Rejecting split %d->%d resulting in > 2 save insns:\n",
4982 original_regno, REGNO (new_reg));
4983 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
4984 fprintf (lra_dump_file,
4985 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4987 return false;
4989 restore = emit_spill_move (false, new_reg, original_reg);
4990 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
4992 if (lra_dump_file != NULL)
4994 fprintf (lra_dump_file,
4995 " Rejecting split %d->%d "
4996 "resulting in > 2 restore insns:\n",
4997 original_regno, REGNO (new_reg));
4998 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
4999 fprintf (lra_dump_file,
5000 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5002 return false;
5004 after_p = usage_insns[original_regno].after_p;
5005 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
5006 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5007 bitmap_set_bit (&check_only_regs, original_regno);
5008 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
5009 for (;;)
5011 if (GET_CODE (next_usage_insns) != INSN_LIST)
5013 usage_insn = next_usage_insns;
5014 break;
5016 usage_insn = XEXP (next_usage_insns, 0);
5017 lra_assert (DEBUG_INSN_P (usage_insn));
5018 next_usage_insns = XEXP (next_usage_insns, 1);
5019 lra_substitute_pseudo (&usage_insn, original_regno, new_reg);
5020 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5021 if (lra_dump_file != NULL)
5023 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5024 original_regno, REGNO (new_reg));
5025 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5028 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5029 lra_assert (usage_insn != insn || (after_p && before_p));
5030 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5031 after_p ? NULL : restore,
5032 after_p ? restore : NULL,
5033 call_save_p
5034 ? "Add reg<-save" : "Add reg<-split");
5035 lra_process_new_insns (insn, before_p ? save : NULL,
5036 before_p ? NULL : save,
5037 call_save_p
5038 ? "Add save<-reg" : "Add split<-reg");
5039 if (nregs > 1)
5040 /* If we are trying to split multi-register. We should check
5041 conflicts on the next assignment sub-pass. IRA can allocate on
5042 sub-register levels, LRA do this on pseudos level right now and
5043 this discrepancy may create allocation conflicts after
5044 splitting. */
5045 lra_risky_transformations_p = true;
5046 if (lra_dump_file != NULL)
5047 fprintf (lra_dump_file,
5048 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5049 return true;
5052 /* Recognize that we need a split transformation for insn INSN, which
5053 defines or uses REGNO in its insn biggest MODE (we use it only if
5054 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5055 hard registers which might be used for reloads since the EBB end.
5056 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5057 uid before starting INSN processing. Return true if we succeed in
5058 such transformation. */
5059 static bool
5060 split_if_necessary (int regno, machine_mode mode,
5061 HARD_REG_SET potential_reload_hard_regs,
5062 bool before_p, rtx_insn *insn, int max_uid)
5064 bool res = false;
5065 int i, nregs = 1;
5066 rtx next_usage_insns;
5068 if (regno < FIRST_PSEUDO_REGISTER)
5069 nregs = hard_regno_nregs[regno][mode];
5070 for (i = 0; i < nregs; i++)
5071 if (usage_insns[regno + i].check == curr_usage_insns_check
5072 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5073 /* To avoid processing the register twice or more. */
5074 && ((GET_CODE (next_usage_insns) != INSN_LIST
5075 && INSN_UID (next_usage_insns) < max_uid)
5076 || (GET_CODE (next_usage_insns) == INSN_LIST
5077 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5078 && need_for_split_p (potential_reload_hard_regs, regno + i)
5079 && split_reg (before_p, regno + i, insn, next_usage_insns))
5080 res = true;
5081 return res;
5084 /* Check only registers living at the current program point in the
5085 current EBB. */
5086 static bitmap_head live_regs;
5088 /* Update live info in EBB given by its HEAD and TAIL insns after
5089 inheritance/split transformation. The function removes dead moves
5090 too. */
5091 static void
5092 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5094 unsigned int j;
5095 int i, regno;
5096 bool live_p;
5097 rtx_insn *prev_insn;
5098 rtx set;
5099 bool remove_p;
5100 basic_block last_bb, prev_bb, curr_bb;
5101 bitmap_iterator bi;
5102 struct lra_insn_reg *reg;
5103 edge e;
5104 edge_iterator ei;
5106 last_bb = BLOCK_FOR_INSN (tail);
5107 prev_bb = NULL;
5108 for (curr_insn = tail;
5109 curr_insn != PREV_INSN (head);
5110 curr_insn = prev_insn)
5112 prev_insn = PREV_INSN (curr_insn);
5113 /* We need to process empty blocks too. They contain
5114 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5115 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5116 continue;
5117 curr_bb = BLOCK_FOR_INSN (curr_insn);
5118 if (curr_bb != prev_bb)
5120 if (prev_bb != NULL)
5122 /* Update df_get_live_in (prev_bb): */
5123 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5124 if (bitmap_bit_p (&live_regs, j))
5125 bitmap_set_bit (df_get_live_in (prev_bb), j);
5126 else
5127 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5129 if (curr_bb != last_bb)
5131 /* Update df_get_live_out (curr_bb): */
5132 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5134 live_p = bitmap_bit_p (&live_regs, j);
5135 if (! live_p)
5136 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5137 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5139 live_p = true;
5140 break;
5142 if (live_p)
5143 bitmap_set_bit (df_get_live_out (curr_bb), j);
5144 else
5145 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5148 prev_bb = curr_bb;
5149 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5151 if (! NONDEBUG_INSN_P (curr_insn))
5152 continue;
5153 curr_id = lra_get_insn_recog_data (curr_insn);
5154 curr_static_id = curr_id->insn_static_data;
5155 remove_p = false;
5156 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
5157 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5158 && bitmap_bit_p (&check_only_regs, regno)
5159 && ! bitmap_bit_p (&live_regs, regno))
5160 remove_p = true;
5161 /* See which defined values die here. */
5162 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5163 if (reg->type == OP_OUT && ! reg->subreg_p)
5164 bitmap_clear_bit (&live_regs, reg->regno);
5165 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5166 if (reg->type == OP_OUT && ! reg->subreg_p)
5167 bitmap_clear_bit (&live_regs, reg->regno);
5168 /* Mark each used value as live. */
5169 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5170 if (reg->type != OP_OUT
5171 && bitmap_bit_p (&check_only_regs, reg->regno))
5172 bitmap_set_bit (&live_regs, reg->regno);
5173 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5174 if (reg->type != OP_OUT
5175 && bitmap_bit_p (&check_only_regs, reg->regno))
5176 bitmap_set_bit (&live_regs, reg->regno);
5177 if (curr_id->arg_hard_regs != NULL)
5178 /* Make argument hard registers live. */
5179 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5180 if (bitmap_bit_p (&check_only_regs, regno))
5181 bitmap_set_bit (&live_regs, regno);
5182 /* It is quite important to remove dead move insns because it
5183 means removing dead store. We don't need to process them for
5184 constraints. */
5185 if (remove_p)
5187 if (lra_dump_file != NULL)
5189 fprintf (lra_dump_file, " Removing dead insn:\n ");
5190 dump_insn_slim (lra_dump_file, curr_insn);
5192 lra_set_insn_deleted (curr_insn);
5197 /* The structure describes info to do an inheritance for the current
5198 insn. We need to collect such info first before doing the
5199 transformations because the transformations change the insn
5200 internal representation. */
5201 struct to_inherit
5203 /* Original regno. */
5204 int regno;
5205 /* Subsequent insns which can inherit original reg value. */
5206 rtx insns;
5209 /* Array containing all info for doing inheritance from the current
5210 insn. */
5211 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5213 /* Number elements in the previous array. */
5214 static int to_inherit_num;
5216 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5217 structure to_inherit. */
5218 static void
5219 add_to_inherit (int regno, rtx insns)
5221 int i;
5223 for (i = 0; i < to_inherit_num; i++)
5224 if (to_inherit[i].regno == regno)
5225 return;
5226 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5227 to_inherit[to_inherit_num].regno = regno;
5228 to_inherit[to_inherit_num++].insns = insns;
5231 /* Return the last non-debug insn in basic block BB, or the block begin
5232 note if none. */
5233 static rtx_insn *
5234 get_last_insertion_point (basic_block bb)
5236 rtx_insn *insn;
5238 FOR_BB_INSNS_REVERSE (bb, insn)
5239 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5240 return insn;
5241 gcc_unreachable ();
5244 /* Set up RES by registers living on edges FROM except the edge (FROM,
5245 TO) or by registers set up in a jump insn in BB FROM. */
5246 static void
5247 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5249 rtx_insn *last;
5250 struct lra_insn_reg *reg;
5251 edge e;
5252 edge_iterator ei;
5254 lra_assert (to != NULL);
5255 bitmap_clear (res);
5256 FOR_EACH_EDGE (e, ei, from->succs)
5257 if (e->dest != to)
5258 bitmap_ior_into (res, df_get_live_in (e->dest));
5259 last = get_last_insertion_point (from);
5260 if (! JUMP_P (last))
5261 return;
5262 curr_id = lra_get_insn_recog_data (last);
5263 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5264 if (reg->type != OP_IN)
5265 bitmap_set_bit (res, reg->regno);
5268 /* Used as a temporary results of some bitmap calculations. */
5269 static bitmap_head temp_bitmap;
5271 /* We split for reloads of small class of hard regs. The following
5272 defines how many hard regs the class should have to be qualified as
5273 small. The code is mostly oriented to x86/x86-64 architecture
5274 where some insns need to use only specific register or pair of
5275 registers and these register can live in RTL explicitly, e.g. for
5276 parameter passing. */
5277 static const int max_small_class_regs_num = 2;
5279 /* Do inheritance/split transformations in EBB starting with HEAD and
5280 finishing on TAIL. We process EBB insns in the reverse order.
5281 Return true if we did any inheritance/split transformation in the
5282 EBB.
5284 We should avoid excessive splitting which results in worse code
5285 because of inaccurate cost calculations for spilling new split
5286 pseudos in such case. To achieve this we do splitting only if
5287 register pressure is high in given basic block and there are reload
5288 pseudos requiring hard registers. We could do more register
5289 pressure calculations at any given program point to avoid necessary
5290 splitting even more but it is to expensive and the current approach
5291 works well enough. */
5292 static bool
5293 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5295 int i, src_regno, dst_regno, nregs;
5296 bool change_p, succ_p, update_reloads_num_p;
5297 rtx_insn *prev_insn, *last_insn;
5298 rtx next_usage_insns, set;
5299 enum reg_class cl;
5300 struct lra_insn_reg *reg;
5301 basic_block last_processed_bb, curr_bb = NULL;
5302 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5303 bitmap to_process;
5304 unsigned int j;
5305 bitmap_iterator bi;
5306 bool head_p, after_p;
5308 change_p = false;
5309 curr_usage_insns_check++;
5310 reloads_num = calls_num = 0;
5311 bitmap_clear (&check_only_regs);
5312 last_processed_bb = NULL;
5313 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5314 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5315 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5316 /* We don't process new insns generated in the loop. */
5317 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5319 prev_insn = PREV_INSN (curr_insn);
5320 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5321 curr_bb = BLOCK_FOR_INSN (curr_insn);
5322 if (last_processed_bb != curr_bb)
5324 /* We are at the end of BB. Add qualified living
5325 pseudos for potential splitting. */
5326 to_process = df_get_live_out (curr_bb);
5327 if (last_processed_bb != NULL)
5329 /* We are somewhere in the middle of EBB. */
5330 get_live_on_other_edges (curr_bb, last_processed_bb,
5331 &temp_bitmap);
5332 to_process = &temp_bitmap;
5334 last_processed_bb = curr_bb;
5335 last_insn = get_last_insertion_point (curr_bb);
5336 after_p = (! JUMP_P (last_insn)
5337 && (! CALL_P (last_insn)
5338 || (find_reg_note (last_insn,
5339 REG_NORETURN, NULL_RTX) == NULL_RTX
5340 && ! SIBLING_CALL_P (last_insn))));
5341 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5342 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5344 if ((int) j >= lra_constraint_new_regno_start)
5345 break;
5346 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5348 if (j < FIRST_PSEUDO_REGISTER)
5349 SET_HARD_REG_BIT (live_hard_regs, j);
5350 else
5351 add_to_hard_reg_set (&live_hard_regs,
5352 PSEUDO_REGNO_MODE (j),
5353 reg_renumber[j]);
5354 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5358 src_regno = dst_regno = -1;
5359 if (NONDEBUG_INSN_P (curr_insn)
5360 && (set = single_set (curr_insn)) != NULL_RTX
5361 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5363 src_regno = REGNO (SET_SRC (set));
5364 dst_regno = REGNO (SET_DEST (set));
5366 update_reloads_num_p = true;
5367 if (src_regno < lra_constraint_new_regno_start
5368 && src_regno >= FIRST_PSEUDO_REGISTER
5369 && reg_renumber[src_regno] < 0
5370 && dst_regno >= lra_constraint_new_regno_start
5371 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5373 /* 'reload_pseudo <- original_pseudo'. */
5374 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5375 reloads_num++;
5376 update_reloads_num_p = false;
5377 succ_p = false;
5378 if (usage_insns[src_regno].check == curr_usage_insns_check
5379 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5380 succ_p = inherit_reload_reg (false, src_regno, cl,
5381 curr_insn, next_usage_insns);
5382 if (succ_p)
5383 change_p = true;
5384 else
5385 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5386 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5387 IOR_HARD_REG_SET (potential_reload_hard_regs,
5388 reg_class_contents[cl]);
5390 else if (src_regno >= lra_constraint_new_regno_start
5391 && dst_regno < lra_constraint_new_regno_start
5392 && dst_regno >= FIRST_PSEUDO_REGISTER
5393 && reg_renumber[dst_regno] < 0
5394 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5395 && usage_insns[dst_regno].check == curr_usage_insns_check
5396 && (next_usage_insns
5397 = usage_insns[dst_regno].insns) != NULL_RTX)
5399 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5400 reloads_num++;
5401 update_reloads_num_p = false;
5402 /* 'original_pseudo <- reload_pseudo'. */
5403 if (! JUMP_P (curr_insn)
5404 && inherit_reload_reg (true, dst_regno, cl,
5405 curr_insn, next_usage_insns))
5406 change_p = true;
5407 /* Invalidate. */
5408 usage_insns[dst_regno].check = 0;
5409 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5410 IOR_HARD_REG_SET (potential_reload_hard_regs,
5411 reg_class_contents[cl]);
5413 else if (INSN_P (curr_insn))
5415 int iter;
5416 int max_uid = get_max_uid ();
5418 curr_id = lra_get_insn_recog_data (curr_insn);
5419 curr_static_id = curr_id->insn_static_data;
5420 to_inherit_num = 0;
5421 /* Process insn definitions. */
5422 for (iter = 0; iter < 2; iter++)
5423 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5424 reg != NULL;
5425 reg = reg->next)
5426 if (reg->type != OP_IN
5427 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5429 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5430 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5431 && usage_insns[dst_regno].check == curr_usage_insns_check
5432 && (next_usage_insns
5433 = usage_insns[dst_regno].insns) != NULL_RTX)
5435 struct lra_insn_reg *r;
5437 for (r = curr_id->regs; r != NULL; r = r->next)
5438 if (r->type != OP_OUT && r->regno == dst_regno)
5439 break;
5440 /* Don't do inheritance if the pseudo is also
5441 used in the insn. */
5442 if (r == NULL)
5443 /* We can not do inheritance right now
5444 because the current insn reg info (chain
5445 regs) can change after that. */
5446 add_to_inherit (dst_regno, next_usage_insns);
5448 /* We can not process one reg twice here because of
5449 usage_insns invalidation. */
5450 if ((dst_regno < FIRST_PSEUDO_REGISTER
5451 || reg_renumber[dst_regno] >= 0)
5452 && ! reg->subreg_p && reg->type != OP_IN)
5454 HARD_REG_SET s;
5456 if (split_if_necessary (dst_regno, reg->biggest_mode,
5457 potential_reload_hard_regs,
5458 false, curr_insn, max_uid))
5459 change_p = true;
5460 CLEAR_HARD_REG_SET (s);
5461 if (dst_regno < FIRST_PSEUDO_REGISTER)
5462 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5463 else
5464 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5465 reg_renumber[dst_regno]);
5466 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5468 /* We should invalidate potential inheritance or
5469 splitting for the current insn usages to the next
5470 usage insns (see code below) as the output pseudo
5471 prevents this. */
5472 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5473 && reg_renumber[dst_regno] < 0)
5474 || (reg->type == OP_OUT && ! reg->subreg_p
5475 && (dst_regno < FIRST_PSEUDO_REGISTER
5476 || reg_renumber[dst_regno] >= 0)))
5478 /* Invalidate and mark definitions. */
5479 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5480 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5481 else
5483 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5484 for (i = 0; i < nregs; i++)
5485 usage_insns[dst_regno + i].check
5486 = -(int) INSN_UID (curr_insn);
5490 if (! JUMP_P (curr_insn))
5491 for (i = 0; i < to_inherit_num; i++)
5492 if (inherit_reload_reg (true, to_inherit[i].regno,
5493 ALL_REGS, curr_insn,
5494 to_inherit[i].insns))
5495 change_p = true;
5496 if (CALL_P (curr_insn))
5498 rtx cheap, pat, dest;
5499 rtx_insn *restore;
5500 int regno, hard_regno;
5502 calls_num++;
5503 if ((cheap = find_reg_note (curr_insn,
5504 REG_RETURNED, NULL_RTX)) != NULL_RTX
5505 && ((cheap = XEXP (cheap, 0)), true)
5506 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5507 && (hard_regno = reg_renumber[regno]) >= 0
5508 /* If there are pending saves/restores, the
5509 optimization is not worth. */
5510 && usage_insns[regno].calls_num == calls_num - 1
5511 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5513 /* Restore the pseudo from the call result as
5514 REG_RETURNED note says that the pseudo value is
5515 in the call result and the pseudo is an argument
5516 of the call. */
5517 pat = PATTERN (curr_insn);
5518 if (GET_CODE (pat) == PARALLEL)
5519 pat = XVECEXP (pat, 0, 0);
5520 dest = SET_DEST (pat);
5521 /* For multiple return values dest is PARALLEL.
5522 Currently we handle only single return value case. */
5523 if (REG_P (dest))
5525 start_sequence ();
5526 emit_move_insn (cheap, copy_rtx (dest));
5527 restore = get_insns ();
5528 end_sequence ();
5529 lra_process_new_insns (curr_insn, NULL, restore,
5530 "Inserting call parameter restore");
5531 /* We don't need to save/restore of the pseudo from
5532 this call. */
5533 usage_insns[regno].calls_num = calls_num;
5534 bitmap_set_bit (&check_only_regs, regno);
5538 to_inherit_num = 0;
5539 /* Process insn usages. */
5540 for (iter = 0; iter < 2; iter++)
5541 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5542 reg != NULL;
5543 reg = reg->next)
5544 if ((reg->type != OP_OUT
5545 || (reg->type == OP_OUT && reg->subreg_p))
5546 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5548 if (src_regno >= FIRST_PSEUDO_REGISTER
5549 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5551 if (usage_insns[src_regno].check == curr_usage_insns_check
5552 && (next_usage_insns
5553 = usage_insns[src_regno].insns) != NULL_RTX
5554 && NONDEBUG_INSN_P (curr_insn))
5555 add_to_inherit (src_regno, next_usage_insns);
5556 else if (usage_insns[src_regno].check
5557 != -(int) INSN_UID (curr_insn))
5558 /* Add usages but only if the reg is not set up
5559 in the same insn. */
5560 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5562 else if (src_regno < FIRST_PSEUDO_REGISTER
5563 || reg_renumber[src_regno] >= 0)
5565 bool before_p;
5566 rtx use_insn = curr_insn;
5568 before_p = (JUMP_P (curr_insn)
5569 || (CALL_P (curr_insn) && reg->type == OP_IN));
5570 if (NONDEBUG_INSN_P (curr_insn)
5571 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5572 && split_if_necessary (src_regno, reg->biggest_mode,
5573 potential_reload_hard_regs,
5574 before_p, curr_insn, max_uid))
5576 if (reg->subreg_p)
5577 lra_risky_transformations_p = true;
5578 change_p = true;
5579 /* Invalidate. */
5580 usage_insns[src_regno].check = 0;
5581 if (before_p)
5582 use_insn = PREV_INSN (curr_insn);
5584 if (NONDEBUG_INSN_P (curr_insn))
5586 if (src_regno < FIRST_PSEUDO_REGISTER)
5587 add_to_hard_reg_set (&live_hard_regs,
5588 reg->biggest_mode, src_regno);
5589 else
5590 add_to_hard_reg_set (&live_hard_regs,
5591 PSEUDO_REGNO_MODE (src_regno),
5592 reg_renumber[src_regno]);
5594 add_next_usage_insn (src_regno, use_insn, reloads_num);
5597 /* Process call args. */
5598 if (curr_id->arg_hard_regs != NULL)
5599 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5600 if (src_regno < FIRST_PSEUDO_REGISTER)
5602 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5603 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5605 for (i = 0; i < to_inherit_num; i++)
5607 src_regno = to_inherit[i].regno;
5608 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5609 curr_insn, to_inherit[i].insns))
5610 change_p = true;
5611 else
5612 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5615 if (update_reloads_num_p
5616 && NONDEBUG_INSN_P (curr_insn)
5617 && (set = single_set (curr_insn)) != NULL_RTX)
5619 int regno = -1;
5620 if ((REG_P (SET_DEST (set))
5621 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5622 && reg_renumber[regno] < 0
5623 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5624 || (REG_P (SET_SRC (set))
5625 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5626 && reg_renumber[regno] < 0
5627 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5629 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5630 reloads_num++;
5631 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5632 IOR_HARD_REG_SET (potential_reload_hard_regs,
5633 reg_class_contents[cl]);
5636 /* We reached the start of the current basic block. */
5637 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5638 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5640 /* We reached the beginning of the current block -- do
5641 rest of spliting in the current BB. */
5642 to_process = df_get_live_in (curr_bb);
5643 if (BLOCK_FOR_INSN (head) != curr_bb)
5645 /* We are somewhere in the middle of EBB. */
5646 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5647 curr_bb, &temp_bitmap);
5648 to_process = &temp_bitmap;
5650 head_p = true;
5651 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5653 if ((int) j >= lra_constraint_new_regno_start)
5654 break;
5655 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5656 && usage_insns[j].check == curr_usage_insns_check
5657 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5659 if (need_for_split_p (potential_reload_hard_regs, j))
5661 if (lra_dump_file != NULL && head_p)
5663 fprintf (lra_dump_file,
5664 " ----------------------------------\n");
5665 head_p = false;
5667 if (split_reg (false, j, bb_note (curr_bb),
5668 next_usage_insns))
5669 change_p = true;
5671 usage_insns[j].check = 0;
5676 return change_p;
5679 /* This value affects EBB forming. If probability of edge from EBB to
5680 a BB is not greater than the following value, we don't add the BB
5681 to EBB. */
5682 #define EBB_PROBABILITY_CUTOFF \
5683 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
5685 /* Current number of inheritance/split iteration. */
5686 int lra_inheritance_iter;
5688 /* Entry function for inheritance/split pass. */
5689 void
5690 lra_inheritance (void)
5692 int i;
5693 basic_block bb, start_bb;
5694 edge e;
5696 lra_inheritance_iter++;
5697 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5698 return;
5699 timevar_push (TV_LRA_INHERITANCE);
5700 if (lra_dump_file != NULL)
5701 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5702 lra_inheritance_iter);
5703 curr_usage_insns_check = 0;
5704 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5705 for (i = 0; i < lra_constraint_new_regno_start; i++)
5706 usage_insns[i].check = 0;
5707 bitmap_initialize (&check_only_regs, &reg_obstack);
5708 bitmap_initialize (&live_regs, &reg_obstack);
5709 bitmap_initialize (&temp_bitmap, &reg_obstack);
5710 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5711 FOR_EACH_BB_FN (bb, cfun)
5713 start_bb = bb;
5714 if (lra_dump_file != NULL)
5715 fprintf (lra_dump_file, "EBB");
5716 /* Form a EBB starting with BB. */
5717 bitmap_clear (&ebb_global_regs);
5718 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5719 for (;;)
5721 if (lra_dump_file != NULL)
5722 fprintf (lra_dump_file, " %d", bb->index);
5723 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5724 || LABEL_P (BB_HEAD (bb->next_bb)))
5725 break;
5726 e = find_fallthru_edge (bb->succs);
5727 if (! e)
5728 break;
5729 if (e->probability < EBB_PROBABILITY_CUTOFF)
5730 break;
5731 bb = bb->next_bb;
5733 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5734 if (lra_dump_file != NULL)
5735 fprintf (lra_dump_file, "\n");
5736 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5737 /* Remember that the EBB head and tail can change in
5738 inherit_in_ebb. */
5739 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5741 bitmap_clear (&ebb_global_regs);
5742 bitmap_clear (&temp_bitmap);
5743 bitmap_clear (&live_regs);
5744 bitmap_clear (&check_only_regs);
5745 free (usage_insns);
5747 timevar_pop (TV_LRA_INHERITANCE);
5752 /* This page contains code to undo failed inheritance/split
5753 transformations. */
5755 /* Current number of iteration undoing inheritance/split. */
5756 int lra_undo_inheritance_iter;
5758 /* Fix BB live info LIVE after removing pseudos created on pass doing
5759 inheritance/split which are REMOVED_PSEUDOS. */
5760 static void
5761 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5763 unsigned int regno;
5764 bitmap_iterator bi;
5766 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5767 if (bitmap_clear_bit (live, regno))
5768 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5771 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5772 number. */
5773 static int
5774 get_regno (rtx reg)
5776 if (GET_CODE (reg) == SUBREG)
5777 reg = SUBREG_REG (reg);
5778 if (REG_P (reg))
5779 return REGNO (reg);
5780 return -1;
5783 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5784 return true if we did any change. The undo transformations for
5785 inheritance looks like
5786 i <- i2
5787 p <- i => p <- i2
5788 or removing
5789 p <- i, i <- p, and i <- i3
5790 where p is original pseudo from which inheritance pseudo i was
5791 created, i and i3 are removed inheritance pseudos, i2 is another
5792 not removed inheritance pseudo. All split pseudos or other
5793 occurrences of removed inheritance pseudos are changed on the
5794 corresponding original pseudos.
5796 The function also schedules insns changed and created during
5797 inheritance/split pass for processing by the subsequent constraint
5798 pass. */
5799 static bool
5800 remove_inheritance_pseudos (bitmap remove_pseudos)
5802 basic_block bb;
5803 int regno, sregno, prev_sregno, dregno, restore_regno;
5804 rtx set, prev_set;
5805 rtx_insn *prev_insn;
5806 bool change_p, done_p;
5808 change_p = ! bitmap_empty_p (remove_pseudos);
5809 /* We can not finish the function right away if CHANGE_P is true
5810 because we need to marks insns affected by previous
5811 inheritance/split pass for processing by the subsequent
5812 constraint pass. */
5813 FOR_EACH_BB_FN (bb, cfun)
5815 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5816 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5817 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5819 if (! INSN_P (curr_insn))
5820 continue;
5821 done_p = false;
5822 sregno = dregno = -1;
5823 if (change_p && NONDEBUG_INSN_P (curr_insn)
5824 && (set = single_set (curr_insn)) != NULL_RTX)
5826 dregno = get_regno (SET_DEST (set));
5827 sregno = get_regno (SET_SRC (set));
5830 if (sregno >= 0 && dregno >= 0)
5832 if ((bitmap_bit_p (remove_pseudos, sregno)
5833 && (lra_reg_info[sregno].restore_regno == dregno
5834 || (bitmap_bit_p (remove_pseudos, dregno)
5835 && (lra_reg_info[sregno].restore_regno
5836 == lra_reg_info[dregno].restore_regno))))
5837 || (bitmap_bit_p (remove_pseudos, dregno)
5838 && lra_reg_info[dregno].restore_regno == sregno))
5839 /* One of the following cases:
5840 original <- removed inheritance pseudo
5841 removed inherit pseudo <- another removed inherit pseudo
5842 removed inherit pseudo <- original pseudo
5844 removed_split_pseudo <- original_reg
5845 original_reg <- removed_split_pseudo */
5847 if (lra_dump_file != NULL)
5849 fprintf (lra_dump_file, " Removing %s:\n",
5850 bitmap_bit_p (&lra_split_regs, sregno)
5851 || bitmap_bit_p (&lra_split_regs, dregno)
5852 ? "split" : "inheritance");
5853 dump_insn_slim (lra_dump_file, curr_insn);
5855 lra_set_insn_deleted (curr_insn);
5856 done_p = true;
5858 else if (bitmap_bit_p (remove_pseudos, sregno)
5859 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5861 /* Search the following pattern:
5862 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5863 original_pseudo <- inherit_or_split_pseudo1
5864 where the 2nd insn is the current insn and
5865 inherit_or_split_pseudo2 is not removed. If it is found,
5866 change the current insn onto:
5867 original_pseudo <- inherit_or_split_pseudo2. */
5868 for (prev_insn = PREV_INSN (curr_insn);
5869 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5870 prev_insn = PREV_INSN (prev_insn))
5872 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5873 && (prev_set = single_set (prev_insn)) != NULL_RTX
5874 /* There should be no subregs in insn we are
5875 searching because only the original reg might
5876 be in subreg when we changed the mode of
5877 load/store for splitting. */
5878 && REG_P (SET_DEST (prev_set))
5879 && REG_P (SET_SRC (prev_set))
5880 && (int) REGNO (SET_DEST (prev_set)) == sregno
5881 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5882 >= FIRST_PSEUDO_REGISTER)
5883 /* As we consider chain of inheritance or
5884 splitting described in above comment we should
5885 check that sregno and prev_sregno were
5886 inheritance/split pseudos created from the
5887 same original regno. */
5888 && (lra_reg_info[sregno].restore_regno
5889 == lra_reg_info[prev_sregno].restore_regno)
5890 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5892 lra_assert (GET_MODE (SET_SRC (prev_set))
5893 == GET_MODE (regno_reg_rtx[sregno]));
5894 if (GET_CODE (SET_SRC (set)) == SUBREG)
5895 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5896 else
5897 SET_SRC (set) = SET_SRC (prev_set);
5898 /* As we are finishing with processing the insn
5899 here, check the destination too as it might
5900 inheritance pseudo for another pseudo. */
5901 if (bitmap_bit_p (remove_pseudos, dregno)
5902 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
5903 && (restore_regno
5904 = lra_reg_info[dregno].restore_regno) >= 0)
5906 if (GET_CODE (SET_DEST (set)) == SUBREG)
5907 SUBREG_REG (SET_DEST (set))
5908 = regno_reg_rtx[restore_regno];
5909 else
5910 SET_DEST (set) = regno_reg_rtx[restore_regno];
5912 lra_push_insn_and_update_insn_regno_info (curr_insn);
5913 lra_set_used_insn_alternative_by_uid
5914 (INSN_UID (curr_insn), -1);
5915 done_p = true;
5916 if (lra_dump_file != NULL)
5918 fprintf (lra_dump_file, " Change reload insn:\n");
5919 dump_insn_slim (lra_dump_file, curr_insn);
5924 if (! done_p)
5926 struct lra_insn_reg *reg;
5927 bool restored_regs_p = false;
5928 bool kept_regs_p = false;
5930 curr_id = lra_get_insn_recog_data (curr_insn);
5931 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5933 regno = reg->regno;
5934 restore_regno = lra_reg_info[regno].restore_regno;
5935 if (restore_regno >= 0)
5937 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5939 lra_substitute_pseudo_within_insn (
5940 curr_insn, regno, regno_reg_rtx[restore_regno]);
5941 restored_regs_p = true;
5943 else
5944 kept_regs_p = true;
5947 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5949 /* The instruction has changed since the previous
5950 constraints pass. */
5951 lra_push_insn_and_update_insn_regno_info (curr_insn);
5952 lra_set_used_insn_alternative_by_uid
5953 (INSN_UID (curr_insn), -1);
5955 else if (restored_regs_p)
5956 /* The instruction has been restored to the form that
5957 it had during the previous constraints pass. */
5958 lra_update_insn_regno_info (curr_insn);
5959 if (restored_regs_p && lra_dump_file != NULL)
5961 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5962 dump_insn_slim (lra_dump_file, curr_insn);
5967 return change_p;
5970 /* If optional reload pseudos failed to get a hard register or was not
5971 inherited, it is better to remove optional reloads. We do this
5972 transformation after undoing inheritance to figure out necessity to
5973 remove optional reloads easier. Return true if we do any
5974 change. */
5975 static bool
5976 undo_optional_reloads (void)
5978 bool change_p, keep_p;
5979 unsigned int regno, uid;
5980 bitmap_iterator bi, bi2;
5981 rtx_insn *insn;
5982 rtx set, src, dest;
5983 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5985 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5986 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5987 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5989 keep_p = false;
5990 /* Keep optional reloads from previous subpasses. */
5991 if (lra_reg_info[regno].restore_regno < 0
5992 /* If the original pseudo changed its allocation, just
5993 removing the optional pseudo is dangerous as the original
5994 pseudo will have longer live range. */
5995 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
5996 keep_p = true;
5997 else if (reg_renumber[regno] >= 0)
5998 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
6000 insn = lra_insn_recog_data[uid]->insn;
6001 if ((set = single_set (insn)) == NULL_RTX)
6002 continue;
6003 src = SET_SRC (set);
6004 dest = SET_DEST (set);
6005 if (! REG_P (src) || ! REG_P (dest))
6006 continue;
6007 if (REGNO (dest) == regno
6008 /* Ignore insn for optional reloads itself. */
6009 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
6010 /* Check only inheritance on last inheritance pass. */
6011 && (int) REGNO (src) >= new_regno_start
6012 /* Check that the optional reload was inherited. */
6013 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6015 keep_p = true;
6016 break;
6019 if (keep_p)
6021 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6022 if (lra_dump_file != NULL)
6023 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6026 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6027 bitmap_initialize (&insn_bitmap, &reg_obstack);
6028 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6030 if (lra_dump_file != NULL)
6031 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6032 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6033 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6035 insn = lra_insn_recog_data[uid]->insn;
6036 if ((set = single_set (insn)) != NULL_RTX)
6038 src = SET_SRC (set);
6039 dest = SET_DEST (set);
6040 if (REG_P (src) && REG_P (dest)
6041 && ((REGNO (src) == regno
6042 && (lra_reg_info[regno].restore_regno
6043 == (int) REGNO (dest)))
6044 || (REGNO (dest) == regno
6045 && (lra_reg_info[regno].restore_regno
6046 == (int) REGNO (src)))))
6048 if (lra_dump_file != NULL)
6050 fprintf (lra_dump_file, " Deleting move %u\n",
6051 INSN_UID (insn));
6052 dump_insn_slim (lra_dump_file, insn);
6054 lra_set_insn_deleted (insn);
6055 continue;
6057 /* We should not worry about generation memory-memory
6058 moves here as if the corresponding inheritance did
6059 not work (inheritance pseudo did not get a hard reg),
6060 we remove the inheritance pseudo and the optional
6061 reload. */
6063 lra_substitute_pseudo_within_insn (
6064 insn, regno,
6065 regno_reg_rtx[lra_reg_info[regno].restore_regno]);
6066 lra_update_insn_regno_info (insn);
6067 if (lra_dump_file != NULL)
6069 fprintf (lra_dump_file,
6070 " Restoring original insn:\n");
6071 dump_insn_slim (lra_dump_file, insn);
6075 /* Clear restore_regnos. */
6076 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6077 lra_reg_info[regno].restore_regno = -1;
6078 bitmap_clear (&insn_bitmap);
6079 bitmap_clear (&removed_optional_reload_pseudos);
6080 return change_p;
6083 /* Entry function for undoing inheritance/split transformation. Return true
6084 if we did any RTL change in this pass. */
6085 bool
6086 lra_undo_inheritance (void)
6088 unsigned int regno;
6089 int restore_regno, hard_regno;
6090 int n_all_inherit, n_inherit, n_all_split, n_split;
6091 bitmap_head remove_pseudos;
6092 bitmap_iterator bi;
6093 bool change_p;
6095 lra_undo_inheritance_iter++;
6096 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6097 return false;
6098 if (lra_dump_file != NULL)
6099 fprintf (lra_dump_file,
6100 "\n********** Undoing inheritance #%d: **********\n\n",
6101 lra_undo_inheritance_iter);
6102 bitmap_initialize (&remove_pseudos, &reg_obstack);
6103 n_inherit = n_all_inherit = 0;
6104 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6105 if (lra_reg_info[regno].restore_regno >= 0)
6107 n_all_inherit++;
6108 if (reg_renumber[regno] < 0
6109 /* If the original pseudo changed its allocation, just
6110 removing inheritance is dangerous as for changing
6111 allocation we used shorter live-ranges. */
6112 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6113 bitmap_set_bit (&remove_pseudos, regno);
6114 else
6115 n_inherit++;
6117 if (lra_dump_file != NULL && n_all_inherit != 0)
6118 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6119 n_inherit, n_all_inherit,
6120 (double) n_inherit / n_all_inherit * 100);
6121 n_split = n_all_split = 0;
6122 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6123 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6125 n_all_split++;
6126 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6127 ? reg_renumber[restore_regno] : restore_regno);
6128 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6129 bitmap_set_bit (&remove_pseudos, regno);
6130 else
6132 n_split++;
6133 if (lra_dump_file != NULL)
6134 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6135 regno, restore_regno);
6138 if (lra_dump_file != NULL && n_all_split != 0)
6139 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6140 n_split, n_all_split,
6141 (double) n_split / n_all_split * 100);
6142 change_p = remove_inheritance_pseudos (&remove_pseudos);
6143 bitmap_clear (&remove_pseudos);
6144 /* Clear restore_regnos. */
6145 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6146 lra_reg_info[regno].restore_regno = -1;
6147 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6148 lra_reg_info[regno].restore_regno = -1;
6149 change_p = undo_optional_reloads () || change_p;
6150 return change_p;