* include/bits/stl_list.h (_M_resize_pos(size_type&)): Declare.
[official-gcc.git] / gcc / lra-constraints.c
blob122d4bb72af589164e9ac1fcb3fe6773ff4a777d
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 as_a <rtx_insn *> (to_p
1067 ? gen_move_insn (mem_pseudo, val)
1068 : gen_move_insn (val, mem_pseudo));
1071 /* Process a special case insn (register move), return true if we
1072 don't need to process it anymore. INSN should be a single set
1073 insn. Set up that RTL was changed through CHANGE_P and macro
1074 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1075 SEC_MEM_P. */
1076 static bool
1077 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1079 int sregno, dregno;
1080 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1081 rtx_insn *before;
1082 enum reg_class dclass, sclass, secondary_class;
1083 secondary_reload_info sri;
1085 lra_assert (curr_insn_set != NULL_RTX);
1086 dreg = dest = SET_DEST (curr_insn_set);
1087 sreg = src = SET_SRC (curr_insn_set);
1088 if (GET_CODE (dest) == SUBREG)
1089 dreg = SUBREG_REG (dest);
1090 if (GET_CODE (src) == SUBREG)
1091 sreg = SUBREG_REG (src);
1092 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1093 return false;
1094 sclass = dclass = NO_REGS;
1095 if (REG_P (dreg))
1096 dclass = get_reg_class (REGNO (dreg));
1097 if (dclass == ALL_REGS)
1098 /* ALL_REGS is used for new pseudos created by transformations
1099 like reload of SUBREG_REG (see function
1100 simplify_operand_subreg). We don't know their class yet. We
1101 should figure out the class from processing the insn
1102 constraints not in this fast path function. Even if ALL_REGS
1103 were a right class for the pseudo, secondary_... hooks usually
1104 are not define for ALL_REGS. */
1105 return false;
1106 if (REG_P (sreg))
1107 sclass = get_reg_class (REGNO (sreg));
1108 if (sclass == ALL_REGS)
1109 /* See comments above. */
1110 return false;
1111 if (sclass == NO_REGS && dclass == NO_REGS)
1112 return false;
1113 #ifdef SECONDARY_MEMORY_NEEDED
1114 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1115 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1116 && ((sclass != NO_REGS && dclass != NO_REGS)
1117 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1118 #endif
1121 *sec_mem_p = true;
1122 return false;
1124 #endif
1125 if (! REG_P (dreg) || ! REG_P (sreg))
1126 return false;
1127 sri.prev_sri = NULL;
1128 sri.icode = CODE_FOR_nothing;
1129 sri.extra_cost = 0;
1130 secondary_class = NO_REGS;
1131 /* Set up hard register for a reload pseudo for hook
1132 secondary_reload because some targets just ignore unassigned
1133 pseudos in the hook. */
1134 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1136 dregno = REGNO (dreg);
1137 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1139 else
1140 dregno = -1;
1141 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1143 sregno = REGNO (sreg);
1144 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1146 else
1147 sregno = -1;
1148 if (sclass != NO_REGS)
1149 secondary_class
1150 = (enum reg_class) targetm.secondary_reload (false, dest,
1151 (reg_class_t) sclass,
1152 GET_MODE (src), &sri);
1153 if (sclass == NO_REGS
1154 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1155 && dclass != NO_REGS))
1157 enum reg_class old_sclass = secondary_class;
1158 secondary_reload_info old_sri = sri;
1160 sri.prev_sri = NULL;
1161 sri.icode = CODE_FOR_nothing;
1162 sri.extra_cost = 0;
1163 secondary_class
1164 = (enum reg_class) targetm.secondary_reload (true, src,
1165 (reg_class_t) dclass,
1166 GET_MODE (src), &sri);
1167 /* Check the target hook consistency. */
1168 lra_assert
1169 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1170 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1171 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1173 if (sregno >= 0)
1174 reg_renumber [sregno] = -1;
1175 if (dregno >= 0)
1176 reg_renumber [dregno] = -1;
1177 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1178 return false;
1179 *change_p = true;
1180 new_reg = NULL_RTX;
1181 if (secondary_class != NO_REGS)
1182 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1183 secondary_class,
1184 "secondary");
1185 start_sequence ();
1186 if (sri.icode == CODE_FOR_nothing)
1187 lra_emit_move (new_reg, src);
1188 else
1190 enum reg_class scratch_class;
1192 scratch_class = (reg_class_from_constraints
1193 (insn_data[sri.icode].operand[2].constraint));
1194 scratch_reg = (lra_create_new_reg_with_unique_value
1195 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1196 scratch_class, "scratch"));
1197 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1198 src, scratch_reg));
1200 before = get_insns ();
1201 end_sequence ();
1202 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1203 if (new_reg != NULL_RTX)
1204 SET_SRC (curr_insn_set) = new_reg;
1205 else
1207 if (lra_dump_file != NULL)
1209 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1210 dump_insn_slim (lra_dump_file, curr_insn);
1212 lra_set_insn_deleted (curr_insn);
1213 return true;
1215 return false;
1218 /* The following data describe the result of process_alt_operands.
1219 The data are used in curr_insn_transform to generate reloads. */
1221 /* The chosen reg classes which should be used for the corresponding
1222 operands. */
1223 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1224 /* True if the operand should be the same as another operand and that
1225 other operand does not need a reload. */
1226 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1227 /* True if the operand does not need a reload. */
1228 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1229 /* True if the operand can be offsetable memory. */
1230 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1231 /* The number of an operand to which given operand can be matched to. */
1232 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1233 /* The number of elements in the following array. */
1234 static int goal_alt_dont_inherit_ops_num;
1235 /* Numbers of operands whose reload pseudos should not be inherited. */
1236 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1237 /* True if the insn commutative operands should be swapped. */
1238 static bool goal_alt_swapped;
1239 /* The chosen insn alternative. */
1240 static int goal_alt_number;
1242 /* The following five variables are used to choose the best insn
1243 alternative. They reflect final characteristics of the best
1244 alternative. */
1246 /* Number of necessary reloads and overall cost reflecting the
1247 previous value and other unpleasantness of the best alternative. */
1248 static int best_losers, best_overall;
1249 /* Overall number hard registers used for reloads. For example, on
1250 some targets we need 2 general registers to reload DFmode and only
1251 one floating point register. */
1252 static int best_reload_nregs;
1253 /* Overall number reflecting distances of previous reloading the same
1254 value. The distances are counted from the current BB start. It is
1255 used to improve inheritance chances. */
1256 static int best_reload_sum;
1258 /* True if the current insn should have no correspondingly input or
1259 output reloads. */
1260 static bool no_input_reloads_p, no_output_reloads_p;
1262 /* True if we swapped the commutative operands in the current
1263 insn. */
1264 static int curr_swapped;
1266 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1267 register of class CL. Add any input reloads to list BEFORE. AFTER
1268 is nonnull if *LOC is an automodified value; handle that case by
1269 adding the required output reloads to list AFTER. Return true if
1270 the RTL was changed.
1272 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1273 register. Return false if the address register is correct. */
1274 static bool
1275 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1276 enum reg_class cl)
1278 int regno;
1279 enum reg_class rclass, new_class;
1280 rtx reg;
1281 rtx new_reg;
1282 machine_mode mode;
1283 bool subreg_p, before_p = false;
1285 subreg_p = GET_CODE (*loc) == SUBREG;
1286 if (subreg_p)
1287 loc = &SUBREG_REG (*loc);
1288 reg = *loc;
1289 mode = GET_MODE (reg);
1290 if (! REG_P (reg))
1292 if (check_only_p)
1293 return true;
1294 /* Always reload memory in an address even if the target supports
1295 such addresses. */
1296 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1297 before_p = true;
1299 else
1301 regno = REGNO (reg);
1302 rclass = get_reg_class (regno);
1303 if (! check_only_p
1304 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1306 if (lra_dump_file != NULL)
1308 fprintf (lra_dump_file,
1309 "Changing pseudo %d in address of insn %u on equiv ",
1310 REGNO (reg), INSN_UID (curr_insn));
1311 dump_value_slim (lra_dump_file, *loc, 1);
1312 fprintf (lra_dump_file, "\n");
1314 *loc = copy_rtx (*loc);
1316 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1318 if (check_only_p)
1319 return true;
1320 reg = *loc;
1321 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1322 mode, reg, cl, subreg_p, "address", &new_reg))
1323 before_p = true;
1325 else if (new_class != NO_REGS && rclass != new_class)
1327 if (check_only_p)
1328 return true;
1329 lra_change_class (regno, new_class, " Change to", true);
1330 return false;
1332 else
1333 return false;
1335 if (before_p)
1337 push_to_sequence (*before);
1338 lra_emit_move (new_reg, reg);
1339 *before = get_insns ();
1340 end_sequence ();
1342 *loc = new_reg;
1343 if (after != NULL)
1345 start_sequence ();
1346 lra_emit_move (reg, new_reg);
1347 emit_insn (*after);
1348 *after = get_insns ();
1349 end_sequence ();
1351 return true;
1354 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1355 the insn to be inserted before curr insn. AFTER returns the
1356 the insn to be inserted after curr insn. ORIGREG and NEWREG
1357 are the original reg and new reg for reload. */
1358 static void
1359 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1360 rtx newreg)
1362 if (before)
1364 push_to_sequence (*before);
1365 lra_emit_move (newreg, origreg);
1366 *before = get_insns ();
1367 end_sequence ();
1369 if (after)
1371 start_sequence ();
1372 lra_emit_move (origreg, newreg);
1373 emit_insn (*after);
1374 *after = get_insns ();
1375 end_sequence ();
1379 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1381 /* Make reloads for subreg in operand NOP with internal subreg mode
1382 REG_MODE, add new reloads for further processing. Return true if
1383 any change was done. */
1384 static bool
1385 simplify_operand_subreg (int nop, machine_mode reg_mode)
1387 int hard_regno;
1388 rtx_insn *before, *after;
1389 machine_mode mode, innermode;
1390 rtx reg, new_reg;
1391 rtx operand = *curr_id->operand_loc[nop];
1392 enum reg_class regclass;
1393 enum op_type type;
1395 before = after = NULL;
1397 if (GET_CODE (operand) != SUBREG)
1398 return false;
1400 mode = GET_MODE (operand);
1401 reg = SUBREG_REG (operand);
1402 innermode = GET_MODE (reg);
1403 type = curr_static_id->operand[nop].type;
1404 /* If we change address for paradoxical subreg of memory, the
1405 address might violate the necessary alignment or the access might
1406 be slow. So take this into consideration. We should not worry
1407 about access beyond allocated memory for paradoxical memory
1408 subregs as we don't substitute such equiv memory (see processing
1409 equivalences in function lra_constraints) and because for spilled
1410 pseudos we allocate stack memory enough for the biggest
1411 corresponding paradoxical subreg. */
1412 if (MEM_P (reg)
1413 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1414 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1416 rtx subst, old = *curr_id->operand_loc[nop];
1418 alter_subreg (curr_id->operand_loc[nop], false);
1419 subst = *curr_id->operand_loc[nop];
1420 lra_assert (MEM_P (subst));
1421 if (! valid_address_p (innermode, XEXP (reg, 0),
1422 MEM_ADDR_SPACE (reg))
1423 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1424 MEM_ADDR_SPACE (subst)))
1425 return true;
1426 /* If the address was valid and became invalid, prefer to reload
1427 the memory. Typical case is when the index scale should
1428 correspond the memory. */
1429 *curr_id->operand_loc[nop] = old;
1431 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1433 alter_subreg (curr_id->operand_loc[nop], false);
1434 return true;
1436 else if (CONSTANT_P (reg))
1438 /* Try to simplify subreg of constant. It is usually result of
1439 equivalence substitution. */
1440 if (innermode == VOIDmode
1441 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1442 innermode = curr_static_id->operand[nop].mode;
1443 if ((new_reg = simplify_subreg (mode, reg, innermode,
1444 SUBREG_BYTE (operand))) != NULL_RTX)
1446 *curr_id->operand_loc[nop] = new_reg;
1447 return true;
1450 /* Put constant into memory when we have mixed modes. It generates
1451 a better code in most cases as it does not need a secondary
1452 reload memory. It also prevents LRA looping when LRA is using
1453 secondary reload memory again and again. */
1454 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1455 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1457 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1458 alter_subreg (curr_id->operand_loc[nop], false);
1459 return true;
1461 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1462 if there may be a problem accessing OPERAND in the outer
1463 mode. */
1464 if ((REG_P (reg)
1465 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1466 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1467 /* Don't reload paradoxical subregs because we could be looping
1468 having repeatedly final regno out of hard regs range. */
1469 && (hard_regno_nregs[hard_regno][innermode]
1470 >= hard_regno_nregs[hard_regno][mode])
1471 && simplify_subreg_regno (hard_regno, innermode,
1472 SUBREG_BYTE (operand), mode) < 0
1473 /* Don't reload subreg for matching reload. It is actually
1474 valid subreg in LRA. */
1475 && ! LRA_SUBREG_P (operand))
1476 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1478 enum reg_class rclass;
1480 if (REG_P (reg))
1481 /* There is a big probability that we will get the same class
1482 for the new pseudo and we will get the same insn which
1483 means infinite looping. So spill the new pseudo. */
1484 rclass = NO_REGS;
1485 else
1486 /* The class will be defined later in curr_insn_transform. */
1487 rclass
1488 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1490 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1491 rclass, TRUE, "subreg reg", &new_reg))
1493 bool insert_before, insert_after;
1494 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1496 insert_before = (type != OP_OUT
1497 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1498 insert_after = (type != OP_IN);
1499 insert_move_for_subreg (insert_before ? &before : NULL,
1500 insert_after ? &after : NULL,
1501 reg, new_reg);
1503 SUBREG_REG (operand) = new_reg;
1504 lra_process_new_insns (curr_insn, before, after,
1505 "Inserting subreg reload");
1506 return true;
1508 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1509 IRA allocates hardreg to the inner pseudo reg according to its mode
1510 instead of the outermode, so the size of the hardreg may not be enough
1511 to contain the outermode operand, in that case we may need to insert
1512 reload for the reg. For the following two types of paradoxical subreg,
1513 we need to insert reload:
1514 1. If the op_type is OP_IN, and the hardreg could not be paired with
1515 other hardreg to contain the outermode operand
1516 (checked by in_hard_reg_set_p), we need to insert the reload.
1517 2. If the op_type is OP_OUT or OP_INOUT.
1519 Here is a paradoxical subreg example showing how the reload is generated:
1521 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1522 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1524 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1525 here, if reg107 is assigned to hardreg R15, because R15 is the last
1526 hardreg, compiler cannot find another hardreg to pair with R15 to
1527 contain TImode data. So we insert a TImode reload reg180 for it.
1528 After reload is inserted:
1530 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1531 (reg:DI 107 [ __comp ])) -1
1532 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1533 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1535 Two reload hard registers will be allocated to reg180 to save TImode data
1536 in LRA_assign. */
1537 else if (REG_P (reg)
1538 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1539 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1540 && (hard_regno_nregs[hard_regno][innermode]
1541 < hard_regno_nregs[hard_regno][mode])
1542 && (regclass = lra_get_allocno_class (REGNO (reg)))
1543 && (type != OP_IN
1544 || !in_hard_reg_set_p (reg_class_contents[regclass],
1545 mode, hard_regno)))
1547 /* The class will be defined later in curr_insn_transform. */
1548 enum reg_class rclass
1549 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1551 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1552 rclass, TRUE, "paradoxical subreg", &new_reg))
1554 rtx subreg;
1555 bool insert_before, insert_after;
1557 PUT_MODE (new_reg, mode);
1558 subreg = simplify_gen_subreg (innermode, new_reg, mode, 0);
1559 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1561 insert_before = (type != OP_OUT);
1562 insert_after = (type != OP_IN);
1563 insert_move_for_subreg (insert_before ? &before : NULL,
1564 insert_after ? &after : NULL,
1565 reg, subreg);
1567 SUBREG_REG (operand) = new_reg;
1568 lra_process_new_insns (curr_insn, before, after,
1569 "Inserting paradoxical subreg reload");
1570 return true;
1572 return false;
1575 /* Return TRUE if X refers for a hard register from SET. */
1576 static bool
1577 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1579 int i, j, x_hard_regno;
1580 machine_mode mode;
1581 const char *fmt;
1582 enum rtx_code code;
1584 if (x == NULL_RTX)
1585 return false;
1586 code = GET_CODE (x);
1587 mode = GET_MODE (x);
1588 if (code == SUBREG)
1590 x = SUBREG_REG (x);
1591 code = GET_CODE (x);
1592 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1593 mode = GET_MODE (x);
1596 if (REG_P (x))
1598 x_hard_regno = get_hard_regno (x);
1599 return (x_hard_regno >= 0
1600 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1602 if (MEM_P (x))
1604 struct address_info ad;
1606 decompose_mem_address (&ad, x);
1607 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1608 return true;
1609 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1610 return true;
1612 fmt = GET_RTX_FORMAT (code);
1613 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1615 if (fmt[i] == 'e')
1617 if (uses_hard_regs_p (XEXP (x, i), set))
1618 return true;
1620 else if (fmt[i] == 'E')
1622 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1623 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1624 return true;
1627 return false;
1630 /* Return true if OP is a spilled pseudo. */
1631 static inline bool
1632 spilled_pseudo_p (rtx op)
1634 return (REG_P (op)
1635 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1638 /* Return true if X is a general constant. */
1639 static inline bool
1640 general_constant_p (rtx x)
1642 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1645 static bool
1646 reg_in_class_p (rtx reg, enum reg_class cl)
1648 if (cl == NO_REGS)
1649 return get_reg_class (REGNO (reg)) == NO_REGS;
1650 return in_class_p (reg, cl, NULL);
1653 /* Return true if SET of RCLASS contains no hard regs which can be
1654 used in MODE. */
1655 static bool
1656 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1657 HARD_REG_SET &set,
1658 enum machine_mode mode)
1660 HARD_REG_SET temp;
1662 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1663 COPY_HARD_REG_SET (temp, set);
1664 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1665 return (hard_reg_set_subset_p
1666 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1669 /* Major function to choose the current insn alternative and what
1670 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1671 negative we should consider only this alternative. Return false if
1672 we can not choose the alternative or find how to reload the
1673 operands. */
1674 static bool
1675 process_alt_operands (int only_alternative)
1677 bool ok_p = false;
1678 int nop, overall, nalt;
1679 int n_alternatives = curr_static_id->n_alternatives;
1680 int n_operands = curr_static_id->n_operands;
1681 /* LOSERS counts the operands that don't fit this alternative and
1682 would require loading. */
1683 int losers;
1684 /* REJECT is a count of how undesirable this alternative says it is
1685 if any reloading is required. If the alternative matches exactly
1686 then REJECT is ignored, but otherwise it gets this much counted
1687 against it in addition to the reloading needed. */
1688 int reject;
1689 int op_reject;
1690 /* The number of elements in the following array. */
1691 int early_clobbered_regs_num;
1692 /* Numbers of operands which are early clobber registers. */
1693 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1694 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1695 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1696 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1697 bool curr_alt_win[MAX_RECOG_OPERANDS];
1698 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1699 int curr_alt_matches[MAX_RECOG_OPERANDS];
1700 /* The number of elements in the following array. */
1701 int curr_alt_dont_inherit_ops_num;
1702 /* Numbers of operands whose reload pseudos should not be inherited. */
1703 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1704 rtx op;
1705 /* The register when the operand is a subreg of register, otherwise the
1706 operand itself. */
1707 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1708 /* The register if the operand is a register or subreg of register,
1709 otherwise NULL. */
1710 rtx operand_reg[MAX_RECOG_OPERANDS];
1711 int hard_regno[MAX_RECOG_OPERANDS];
1712 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1713 int reload_nregs, reload_sum;
1714 bool costly_p;
1715 enum reg_class cl;
1717 /* Calculate some data common for all alternatives to speed up the
1718 function. */
1719 for (nop = 0; nop < n_operands; nop++)
1721 rtx reg;
1723 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1724 /* The real hard regno of the operand after the allocation. */
1725 hard_regno[nop] = get_hard_regno (op);
1727 operand_reg[nop] = reg = op;
1728 biggest_mode[nop] = GET_MODE (op);
1729 if (GET_CODE (op) == SUBREG)
1731 operand_reg[nop] = reg = SUBREG_REG (op);
1732 if (GET_MODE_SIZE (biggest_mode[nop])
1733 < GET_MODE_SIZE (GET_MODE (reg)))
1734 biggest_mode[nop] = GET_MODE (reg);
1736 if (! REG_P (reg))
1737 operand_reg[nop] = NULL_RTX;
1738 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1739 || ((int) REGNO (reg)
1740 == lra_get_elimination_hard_regno (REGNO (reg))))
1741 no_subreg_reg_operand[nop] = reg;
1742 else
1743 operand_reg[nop] = no_subreg_reg_operand[nop]
1744 /* Just use natural mode for elimination result. It should
1745 be enough for extra constraints hooks. */
1746 = regno_reg_rtx[hard_regno[nop]];
1749 /* The constraints are made of several alternatives. Each operand's
1750 constraint looks like foo,bar,... with commas separating the
1751 alternatives. The first alternatives for all operands go
1752 together, the second alternatives go together, etc.
1754 First loop over alternatives. */
1755 alternative_mask preferred = curr_id->preferred_alternatives;
1756 if (only_alternative >= 0)
1757 preferred &= ALTERNATIVE_BIT (only_alternative);
1759 for (nalt = 0; nalt < n_alternatives; nalt++)
1761 /* Loop over operands for one constraint alternative. */
1762 if (!TEST_BIT (preferred, nalt))
1763 continue;
1765 overall = losers = reject = reload_nregs = reload_sum = 0;
1766 for (nop = 0; nop < n_operands; nop++)
1768 int inc = (curr_static_id
1769 ->operand_alternative[nalt * n_operands + nop].reject);
1770 if (lra_dump_file != NULL && inc != 0)
1771 fprintf (lra_dump_file,
1772 " Staticly defined alt reject+=%d\n", inc);
1773 reject += inc;
1775 early_clobbered_regs_num = 0;
1777 for (nop = 0; nop < n_operands; nop++)
1779 const char *p;
1780 char *end;
1781 int len, c, m, i, opalt_num, this_alternative_matches;
1782 bool win, did_match, offmemok, early_clobber_p;
1783 /* false => this operand can be reloaded somehow for this
1784 alternative. */
1785 bool badop;
1786 /* true => this operand can be reloaded if the alternative
1787 allows regs. */
1788 bool winreg;
1789 /* True if a constant forced into memory would be OK for
1790 this operand. */
1791 bool constmemok;
1792 enum reg_class this_alternative, this_costly_alternative;
1793 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1794 bool this_alternative_match_win, this_alternative_win;
1795 bool this_alternative_offmemok;
1796 bool scratch_p;
1797 machine_mode mode;
1798 enum constraint_num cn;
1800 opalt_num = nalt * n_operands + nop;
1801 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1803 /* Fast track for no constraints at all. */
1804 curr_alt[nop] = NO_REGS;
1805 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1806 curr_alt_win[nop] = true;
1807 curr_alt_match_win[nop] = false;
1808 curr_alt_offmemok[nop] = false;
1809 curr_alt_matches[nop] = -1;
1810 continue;
1813 op = no_subreg_reg_operand[nop];
1814 mode = curr_operand_mode[nop];
1816 win = did_match = winreg = offmemok = constmemok = false;
1817 badop = true;
1819 early_clobber_p = false;
1820 p = curr_static_id->operand_alternative[opalt_num].constraint;
1822 this_costly_alternative = this_alternative = NO_REGS;
1823 /* We update set of possible hard regs besides its class
1824 because reg class might be inaccurate. For example,
1825 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1826 is translated in HI_REGS because classes are merged by
1827 pairs and there is no accurate intermediate class. */
1828 CLEAR_HARD_REG_SET (this_alternative_set);
1829 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1830 this_alternative_win = false;
1831 this_alternative_match_win = false;
1832 this_alternative_offmemok = false;
1833 this_alternative_matches = -1;
1835 /* An empty constraint should be excluded by the fast
1836 track. */
1837 lra_assert (*p != 0 && *p != ',');
1839 op_reject = 0;
1840 /* Scan this alternative's specs for this operand; set WIN
1841 if the operand fits any letter in this alternative.
1842 Otherwise, clear BADOP if this operand could fit some
1843 letter after reloads, or set WINREG if this operand could
1844 fit after reloads provided the constraint allows some
1845 registers. */
1846 costly_p = false;
1849 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1851 case '\0':
1852 len = 0;
1853 break;
1854 case ',':
1855 c = '\0';
1856 break;
1858 case '&':
1859 early_clobber_p = true;
1860 break;
1862 case '$':
1863 op_reject += LRA_MAX_REJECT;
1864 break;
1865 case '^':
1866 op_reject += LRA_LOSER_COST_FACTOR;
1867 break;
1869 case '#':
1870 /* Ignore rest of this alternative. */
1871 c = '\0';
1872 break;
1874 case '0': case '1': case '2': case '3': case '4':
1875 case '5': case '6': case '7': case '8': case '9':
1877 int m_hregno;
1878 bool match_p;
1880 m = strtoul (p, &end, 10);
1881 p = end;
1882 len = 0;
1883 lra_assert (nop > m);
1885 this_alternative_matches = m;
1886 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1887 /* We are supposed to match a previous operand.
1888 If we do, we win if that one did. If we do
1889 not, count both of the operands as losers.
1890 (This is too conservative, since most of the
1891 time only a single reload insn will be needed
1892 to make the two operands win. As a result,
1893 this alternative may be rejected when it is
1894 actually desirable.) */
1895 match_p = false;
1896 if (operands_match_p (*curr_id->operand_loc[nop],
1897 *curr_id->operand_loc[m], m_hregno))
1899 /* We should reject matching of an early
1900 clobber operand if the matching operand is
1901 not dying in the insn. */
1902 if (! curr_static_id->operand[m].early_clobber
1903 || operand_reg[nop] == NULL_RTX
1904 || (find_regno_note (curr_insn, REG_DEAD,
1905 REGNO (op))
1906 || REGNO (op) == REGNO (operand_reg[m])))
1907 match_p = true;
1909 if (match_p)
1911 /* If we are matching a non-offsettable
1912 address where an offsettable address was
1913 expected, then we must reject this
1914 combination, because we can't reload
1915 it. */
1916 if (curr_alt_offmemok[m]
1917 && MEM_P (*curr_id->operand_loc[m])
1918 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1919 continue;
1921 else
1923 /* Operands don't match. Both operands must
1924 allow a reload register, otherwise we
1925 cannot make them match. */
1926 if (curr_alt[m] == NO_REGS)
1927 break;
1928 /* Retroactively mark the operand we had to
1929 match as a loser, if it wasn't already and
1930 it wasn't matched to a register constraint
1931 (e.g it might be matched by memory). */
1932 if (curr_alt_win[m]
1933 && (operand_reg[m] == NULL_RTX
1934 || hard_regno[m] < 0))
1936 losers++;
1937 reload_nregs
1938 += (ira_reg_class_max_nregs[curr_alt[m]]
1939 [GET_MODE (*curr_id->operand_loc[m])]);
1942 /* Prefer matching earlyclobber alternative as
1943 it results in less hard regs required for
1944 the insn than a non-matching earlyclobber
1945 alternative. */
1946 if (curr_static_id->operand[m].early_clobber)
1948 if (lra_dump_file != NULL)
1949 fprintf
1950 (lra_dump_file,
1951 " %d Matching earlyclobber alt:"
1952 " reject--\n",
1953 nop);
1954 reject--;
1956 /* Otherwise we prefer no matching
1957 alternatives because it gives more freedom
1958 in RA. */
1959 else if (operand_reg[nop] == NULL_RTX
1960 || (find_regno_note (curr_insn, REG_DEAD,
1961 REGNO (operand_reg[nop]))
1962 == NULL_RTX))
1964 if (lra_dump_file != NULL)
1965 fprintf
1966 (lra_dump_file,
1967 " %d Matching alt: reject+=2\n",
1968 nop);
1969 reject += 2;
1972 /* If we have to reload this operand and some
1973 previous operand also had to match the same
1974 thing as this operand, we don't know how to do
1975 that. */
1976 if (!match_p || !curr_alt_win[m])
1978 for (i = 0; i < nop; i++)
1979 if (curr_alt_matches[i] == m)
1980 break;
1981 if (i < nop)
1982 break;
1984 else
1985 did_match = true;
1987 /* This can be fixed with reloads if the operand
1988 we are supposed to match can be fixed with
1989 reloads. */
1990 badop = false;
1991 this_alternative = curr_alt[m];
1992 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1993 winreg = this_alternative != NO_REGS;
1994 break;
1997 case 'g':
1998 if (MEM_P (op)
1999 || general_constant_p (op)
2000 || spilled_pseudo_p (op))
2001 win = true;
2002 cl = GENERAL_REGS;
2003 goto reg;
2005 default:
2006 cn = lookup_constraint (p);
2007 switch (get_constraint_type (cn))
2009 case CT_REGISTER:
2010 cl = reg_class_for_constraint (cn);
2011 if (cl != NO_REGS)
2012 goto reg;
2013 break;
2015 case CT_CONST_INT:
2016 if (CONST_INT_P (op)
2017 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2018 win = true;
2019 break;
2021 case CT_MEMORY:
2022 if (MEM_P (op)
2023 && satisfies_memory_constraint_p (op, cn))
2024 win = true;
2025 else if (spilled_pseudo_p (op))
2026 win = true;
2028 /* If we didn't already win, we can reload constants
2029 via force_const_mem or put the pseudo value into
2030 memory, or make other memory by reloading the
2031 address like for 'o'. */
2032 if (CONST_POOL_OK_P (mode, op)
2033 || MEM_P (op) || REG_P (op))
2034 badop = false;
2035 constmemok = true;
2036 offmemok = true;
2037 break;
2039 case CT_ADDRESS:
2040 /* If we didn't already win, we can reload the address
2041 into a base register. */
2042 if (satisfies_address_constraint_p (op, cn))
2043 win = true;
2044 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2045 ADDRESS, SCRATCH);
2046 badop = false;
2047 goto reg;
2049 case CT_FIXED_FORM:
2050 if (constraint_satisfied_p (op, cn))
2051 win = true;
2052 break;
2054 break;
2056 reg:
2057 this_alternative = reg_class_subunion[this_alternative][cl];
2058 IOR_HARD_REG_SET (this_alternative_set,
2059 reg_class_contents[cl]);
2060 if (costly_p)
2062 this_costly_alternative
2063 = reg_class_subunion[this_costly_alternative][cl];
2064 IOR_HARD_REG_SET (this_costly_alternative_set,
2065 reg_class_contents[cl]);
2067 if (mode == BLKmode)
2068 break;
2069 winreg = true;
2070 if (REG_P (op))
2072 if (hard_regno[nop] >= 0
2073 && in_hard_reg_set_p (this_alternative_set,
2074 mode, hard_regno[nop]))
2075 win = true;
2076 else if (hard_regno[nop] < 0
2077 && in_class_p (op, this_alternative, NULL))
2078 win = true;
2080 break;
2082 if (c != ' ' && c != '\t')
2083 costly_p = c == '*';
2085 while ((p += len), c);
2087 scratch_p = (operand_reg[nop] != NULL_RTX
2088 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2089 /* Record which operands fit this alternative. */
2090 if (win)
2092 this_alternative_win = true;
2093 if (operand_reg[nop] != NULL_RTX)
2095 if (hard_regno[nop] >= 0)
2097 if (in_hard_reg_set_p (this_costly_alternative_set,
2098 mode, hard_regno[nop]))
2100 if (lra_dump_file != NULL)
2101 fprintf (lra_dump_file,
2102 " %d Costly set: reject++\n",
2103 nop);
2104 reject++;
2107 else
2109 /* Prefer won reg to spilled pseudo under other
2110 equal conditions for possibe inheritance. */
2111 if (! scratch_p)
2113 if (lra_dump_file != NULL)
2114 fprintf
2115 (lra_dump_file,
2116 " %d Non pseudo reload: reject++\n",
2117 nop);
2118 reject++;
2120 if (in_class_p (operand_reg[nop],
2121 this_costly_alternative, NULL))
2123 if (lra_dump_file != NULL)
2124 fprintf
2125 (lra_dump_file,
2126 " %d Non pseudo costly reload:"
2127 " reject++\n",
2128 nop);
2129 reject++;
2132 /* We simulate the behaviour of old reload here.
2133 Although scratches need hard registers and it
2134 might result in spilling other pseudos, no reload
2135 insns are generated for the scratches. So it
2136 might cost something but probably less than old
2137 reload pass believes. */
2138 if (scratch_p)
2140 if (lra_dump_file != NULL)
2141 fprintf (lra_dump_file,
2142 " %d Scratch win: reject+=2\n",
2143 nop);
2144 reject += 2;
2148 else if (did_match)
2149 this_alternative_match_win = true;
2150 else
2152 int const_to_mem = 0;
2153 bool no_regs_p;
2155 reject += op_reject;
2156 /* Never do output reload of stack pointer. It makes
2157 impossible to do elimination when SP is changed in
2158 RTL. */
2159 if (op == stack_pointer_rtx && ! frame_pointer_needed
2160 && curr_static_id->operand[nop].type != OP_IN)
2161 goto fail;
2163 /* If this alternative asks for a specific reg class, see if there
2164 is at least one allocatable register in that class. */
2165 no_regs_p
2166 = (this_alternative == NO_REGS
2167 || (hard_reg_set_subset_p
2168 (reg_class_contents[this_alternative],
2169 lra_no_alloc_regs)));
2171 /* For asms, verify that the class for this alternative is possible
2172 for the mode that is specified. */
2173 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2175 int i;
2176 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2177 if (HARD_REGNO_MODE_OK (i, mode)
2178 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2179 mode, i))
2180 break;
2181 if (i == FIRST_PSEUDO_REGISTER)
2182 winreg = false;
2185 /* If this operand accepts a register, and if the
2186 register class has at least one allocatable register,
2187 then this operand can be reloaded. */
2188 if (winreg && !no_regs_p)
2189 badop = false;
2191 if (badop)
2193 if (lra_dump_file != NULL)
2194 fprintf (lra_dump_file,
2195 " alt=%d: Bad operand -- refuse\n",
2196 nalt);
2197 goto fail;
2200 /* If not assigned pseudo has a class which a subset of
2201 required reg class, it is a less costly alternative
2202 as the pseudo still can get a hard reg of necessary
2203 class. */
2204 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2205 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2206 && ira_class_subset_p[this_alternative][cl])
2208 if (lra_dump_file != NULL)
2209 fprintf
2210 (lra_dump_file,
2211 " %d Super set class reg: reject-=3\n", nop);
2212 reject -= 3;
2215 this_alternative_offmemok = offmemok;
2216 if (this_costly_alternative != NO_REGS)
2218 if (lra_dump_file != NULL)
2219 fprintf (lra_dump_file,
2220 " %d Costly loser: reject++\n", nop);
2221 reject++;
2223 /* If the operand is dying, has a matching constraint,
2224 and satisfies constraints of the matched operand
2225 which failed to satisfy the own constraints, most probably
2226 the reload for this operand will be gone. */
2227 if (this_alternative_matches >= 0
2228 && !curr_alt_win[this_alternative_matches]
2229 && REG_P (op)
2230 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2231 && (hard_regno[nop] >= 0
2232 ? in_hard_reg_set_p (this_alternative_set,
2233 mode, hard_regno[nop])
2234 : in_class_p (op, this_alternative, NULL)))
2236 if (lra_dump_file != NULL)
2237 fprintf
2238 (lra_dump_file,
2239 " %d Dying matched operand reload: reject++\n",
2240 nop);
2241 reject++;
2243 else
2245 /* Strict_low_part requires to reload the register
2246 not the sub-register. In this case we should
2247 check that a final reload hard reg can hold the
2248 value mode. */
2249 if (curr_static_id->operand[nop].strict_low
2250 && REG_P (op)
2251 && hard_regno[nop] < 0
2252 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2253 && ira_class_hard_regs_num[this_alternative] > 0
2254 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2255 [this_alternative][0],
2256 GET_MODE
2257 (*curr_id->operand_loc[nop])))
2259 if (lra_dump_file != NULL)
2260 fprintf
2261 (lra_dump_file,
2262 " alt=%d: Strict low subreg reload -- refuse\n",
2263 nalt);
2264 goto fail;
2266 losers++;
2268 if (operand_reg[nop] != NULL_RTX
2269 /* Output operands and matched input operands are
2270 not inherited. The following conditions do not
2271 exactly describe the previous statement but they
2272 are pretty close. */
2273 && curr_static_id->operand[nop].type != OP_OUT
2274 && (this_alternative_matches < 0
2275 || curr_static_id->operand[nop].type != OP_IN))
2277 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2278 (operand_reg[nop])]
2279 .last_reload);
2281 /* The value of reload_sum has sense only if we
2282 process insns in their order. It happens only on
2283 the first constraints sub-pass when we do most of
2284 reload work. */
2285 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2286 reload_sum += last_reload - bb_reload_num;
2288 /* If this is a constant that is reloaded into the
2289 desired class by copying it to memory first, count
2290 that as another reload. This is consistent with
2291 other code and is required to avoid choosing another
2292 alternative when the constant is moved into memory.
2293 Note that the test here is precisely the same as in
2294 the code below that calls force_const_mem. */
2295 if (CONST_POOL_OK_P (mode, op)
2296 && ((targetm.preferred_reload_class
2297 (op, this_alternative) == NO_REGS)
2298 || no_input_reloads_p))
2300 const_to_mem = 1;
2301 if (! no_regs_p)
2302 losers++;
2305 /* Alternative loses if it requires a type of reload not
2306 permitted for this insn. We can always reload
2307 objects with a REG_UNUSED note. */
2308 if ((curr_static_id->operand[nop].type != OP_IN
2309 && no_output_reloads_p
2310 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2311 || (curr_static_id->operand[nop].type != OP_OUT
2312 && no_input_reloads_p && ! const_to_mem)
2313 || (this_alternative_matches >= 0
2314 && (no_input_reloads_p
2315 || (no_output_reloads_p
2316 && (curr_static_id->operand
2317 [this_alternative_matches].type != OP_IN)
2318 && ! find_reg_note (curr_insn, REG_UNUSED,
2319 no_subreg_reg_operand
2320 [this_alternative_matches])))))
2322 if (lra_dump_file != NULL)
2323 fprintf
2324 (lra_dump_file,
2325 " alt=%d: No input/otput reload -- refuse\n",
2326 nalt);
2327 goto fail;
2330 /* Alternative loses if it required class pseudo can not
2331 hold value of required mode. Such insns can be
2332 described by insn definitions with mode iterators. */
2333 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2334 && ! hard_reg_set_empty_p (this_alternative_set)
2335 /* It is common practice for constraints to use a
2336 class which does not have actually enough regs to
2337 hold the value (e.g. x86 AREG for mode requiring
2338 more one general reg). Therefore we have 2
2339 conditions to check that the reload pseudo can
2340 not hold the mode value. */
2341 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2342 [this_alternative][0],
2343 GET_MODE (*curr_id->operand_loc[nop]))
2344 /* The above condition is not enough as the first
2345 reg in ira_class_hard_regs can be not aligned for
2346 multi-words mode values. */
2347 && (prohibited_class_reg_set_mode_p
2348 (this_alternative, this_alternative_set,
2349 GET_MODE (*curr_id->operand_loc[nop]))))
2351 if (lra_dump_file != NULL)
2352 fprintf (lra_dump_file,
2353 " alt=%d: reload pseudo for op %d "
2354 " can not hold the mode value -- refuse\n",
2355 nalt, nop);
2356 goto fail;
2359 /* Check strong discouragement of reload of non-constant
2360 into class THIS_ALTERNATIVE. */
2361 if (! CONSTANT_P (op) && ! no_regs_p
2362 && (targetm.preferred_reload_class
2363 (op, this_alternative) == NO_REGS
2364 || (curr_static_id->operand[nop].type == OP_OUT
2365 && (targetm.preferred_output_reload_class
2366 (op, this_alternative) == NO_REGS))))
2368 if (lra_dump_file != NULL)
2369 fprintf (lra_dump_file,
2370 " %d Non-prefered reload: reject+=%d\n",
2371 nop, LRA_MAX_REJECT);
2372 reject += LRA_MAX_REJECT;
2375 if (! (MEM_P (op) && offmemok)
2376 && ! (const_to_mem && constmemok))
2378 /* We prefer to reload pseudos over reloading other
2379 things, since such reloads may be able to be
2380 eliminated later. So bump REJECT in other cases.
2381 Don't do this in the case where we are forcing a
2382 constant into memory and it will then win since
2383 we don't want to have a different alternative
2384 match then. */
2385 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2387 if (lra_dump_file != NULL)
2388 fprintf
2389 (lra_dump_file,
2390 " %d Non-pseudo reload: reject+=2\n",
2391 nop);
2392 reject += 2;
2395 if (! no_regs_p)
2396 reload_nregs
2397 += ira_reg_class_max_nregs[this_alternative][mode];
2399 if (SMALL_REGISTER_CLASS_P (this_alternative))
2401 if (lra_dump_file != NULL)
2402 fprintf
2403 (lra_dump_file,
2404 " %d Small class reload: reject+=%d\n",
2405 nop, LRA_LOSER_COST_FACTOR / 2);
2406 reject += LRA_LOSER_COST_FACTOR / 2;
2410 /* We are trying to spill pseudo into memory. It is
2411 usually more costly than moving to a hard register
2412 although it might takes the same number of
2413 reloads. */
2414 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2416 if (lra_dump_file != NULL)
2417 fprintf
2418 (lra_dump_file,
2419 " %d Spill pseudo into memory: reject+=3\n",
2420 nop);
2421 reject += 3;
2422 if (VECTOR_MODE_P (mode))
2424 /* Spilling vectors into memory is usually more
2425 costly as they contain big values. */
2426 if (lra_dump_file != NULL)
2427 fprintf
2428 (lra_dump_file,
2429 " %d Spill vector pseudo: reject+=2\n",
2430 nop);
2431 reject += 2;
2435 #ifdef SECONDARY_MEMORY_NEEDED
2436 /* If reload requires moving value through secondary
2437 memory, it will need one more insn at least. */
2438 if (this_alternative != NO_REGS
2439 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2440 && ((curr_static_id->operand[nop].type != OP_OUT
2441 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2442 GET_MODE (op)))
2443 || (curr_static_id->operand[nop].type != OP_IN
2444 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2445 GET_MODE (op)))))
2446 losers++;
2447 #endif
2448 /* Input reloads can be inherited more often than output
2449 reloads can be removed, so penalize output
2450 reloads. */
2451 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2453 if (lra_dump_file != NULL)
2454 fprintf
2455 (lra_dump_file,
2456 " %d Non input pseudo reload: reject++\n",
2457 nop);
2458 reject++;
2462 if (early_clobber_p && ! scratch_p)
2464 if (lra_dump_file != NULL)
2465 fprintf (lra_dump_file,
2466 " %d Early clobber: reject++\n", nop);
2467 reject++;
2469 /* ??? We check early clobbers after processing all operands
2470 (see loop below) and there we update the costs more.
2471 Should we update the cost (may be approximately) here
2472 because of early clobber register reloads or it is a rare
2473 or non-important thing to be worth to do it. */
2474 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2475 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2477 if (lra_dump_file != NULL)
2478 fprintf (lra_dump_file,
2479 " alt=%d,overall=%d,losers=%d -- refuse\n",
2480 nalt, overall, losers);
2481 goto fail;
2484 curr_alt[nop] = this_alternative;
2485 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2486 curr_alt_win[nop] = this_alternative_win;
2487 curr_alt_match_win[nop] = this_alternative_match_win;
2488 curr_alt_offmemok[nop] = this_alternative_offmemok;
2489 curr_alt_matches[nop] = this_alternative_matches;
2491 if (this_alternative_matches >= 0
2492 && !did_match && !this_alternative_win)
2493 curr_alt_win[this_alternative_matches] = false;
2495 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2496 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2498 if (curr_insn_set != NULL_RTX && n_operands == 2
2499 /* Prevent processing non-move insns. */
2500 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2501 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2502 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2503 && REG_P (no_subreg_reg_operand[0])
2504 && REG_P (no_subreg_reg_operand[1])
2505 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2506 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2507 || (! curr_alt_win[0] && curr_alt_win[1]
2508 && REG_P (no_subreg_reg_operand[1])
2509 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2510 || (curr_alt_win[0] && ! curr_alt_win[1]
2511 && REG_P (no_subreg_reg_operand[0])
2512 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2513 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2514 no_subreg_reg_operand[1])
2515 || (targetm.preferred_reload_class
2516 (no_subreg_reg_operand[1],
2517 (enum reg_class) curr_alt[1]) != NO_REGS))
2518 /* If it is a result of recent elimination in move
2519 insn we can transform it into an add still by
2520 using this alternative. */
2521 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2523 /* We have a move insn and a new reload insn will be similar
2524 to the current insn. We should avoid such situation as it
2525 results in LRA cycling. */
2526 overall += LRA_MAX_REJECT;
2528 ok_p = true;
2529 curr_alt_dont_inherit_ops_num = 0;
2530 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2532 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2533 HARD_REG_SET temp_set;
2535 i = early_clobbered_nops[nop];
2536 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2537 || hard_regno[i] < 0)
2538 continue;
2539 lra_assert (operand_reg[i] != NULL_RTX);
2540 clobbered_hard_regno = hard_regno[i];
2541 CLEAR_HARD_REG_SET (temp_set);
2542 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2543 first_conflict_j = last_conflict_j = -1;
2544 for (j = 0; j < n_operands; j++)
2545 if (j == i
2546 /* We don't want process insides of match_operator and
2547 match_parallel because otherwise we would process
2548 their operands once again generating a wrong
2549 code. */
2550 || curr_static_id->operand[j].is_operator)
2551 continue;
2552 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2553 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2554 continue;
2555 /* If we don't reload j-th operand, check conflicts. */
2556 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2557 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2559 if (first_conflict_j < 0)
2560 first_conflict_j = j;
2561 last_conflict_j = j;
2563 if (last_conflict_j < 0)
2564 continue;
2565 /* If earlyclobber operand conflicts with another
2566 non-matching operand which is actually the same register
2567 as the earlyclobber operand, it is better to reload the
2568 another operand as an operand matching the earlyclobber
2569 operand can be also the same. */
2570 if (first_conflict_j == last_conflict_j
2571 && operand_reg[last_conflict_j]
2572 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2573 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2575 curr_alt_win[last_conflict_j] = false;
2576 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2577 = last_conflict_j;
2578 losers++;
2579 /* Early clobber was already reflected in REJECT. */
2580 lra_assert (reject > 0);
2581 if (lra_dump_file != NULL)
2582 fprintf
2583 (lra_dump_file,
2584 " %d Conflict early clobber reload: reject--\n",
2586 reject--;
2587 overall += LRA_LOSER_COST_FACTOR - 1;
2589 else
2591 /* We need to reload early clobbered register and the
2592 matched registers. */
2593 for (j = 0; j < n_operands; j++)
2594 if (curr_alt_matches[j] == i)
2596 curr_alt_match_win[j] = false;
2597 losers++;
2598 overall += LRA_LOSER_COST_FACTOR;
2600 if (! curr_alt_match_win[i])
2601 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2602 else
2604 /* Remember pseudos used for match reloads are never
2605 inherited. */
2606 lra_assert (curr_alt_matches[i] >= 0);
2607 curr_alt_win[curr_alt_matches[i]] = false;
2609 curr_alt_win[i] = curr_alt_match_win[i] = false;
2610 losers++;
2611 /* Early clobber was already reflected in REJECT. */
2612 lra_assert (reject > 0);
2613 if (lra_dump_file != NULL)
2614 fprintf
2615 (lra_dump_file,
2616 " %d Matched conflict early clobber reloads:"
2617 "reject--\n",
2619 reject--;
2620 overall += LRA_LOSER_COST_FACTOR - 1;
2623 if (lra_dump_file != NULL)
2624 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2625 nalt, overall, losers, reload_nregs);
2627 /* If this alternative can be made to work by reloading, and it
2628 needs less reloading than the others checked so far, record
2629 it as the chosen goal for reloading. */
2630 if ((best_losers != 0 && losers == 0)
2631 || (((best_losers == 0 && losers == 0)
2632 || (best_losers != 0 && losers != 0))
2633 && (best_overall > overall
2634 || (best_overall == overall
2635 /* If the cost of the reloads is the same,
2636 prefer alternative which requires minimal
2637 number of reload regs. */
2638 && (reload_nregs < best_reload_nregs
2639 || (reload_nregs == best_reload_nregs
2640 && (best_reload_sum < reload_sum
2641 || (best_reload_sum == reload_sum
2642 && nalt < goal_alt_number))))))))
2644 for (nop = 0; nop < n_operands; nop++)
2646 goal_alt_win[nop] = curr_alt_win[nop];
2647 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2648 goal_alt_matches[nop] = curr_alt_matches[nop];
2649 goal_alt[nop] = curr_alt[nop];
2650 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2652 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2653 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2654 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2655 goal_alt_swapped = curr_swapped;
2656 best_overall = overall;
2657 best_losers = losers;
2658 best_reload_nregs = reload_nregs;
2659 best_reload_sum = reload_sum;
2660 goal_alt_number = nalt;
2662 if (losers == 0)
2663 /* Everything is satisfied. Do not process alternatives
2664 anymore. */
2665 break;
2666 fail:
2669 return ok_p;
2672 /* Make reload base reg from address AD. */
2673 static rtx
2674 base_to_reg (struct address_info *ad)
2676 enum reg_class cl;
2677 int code = -1;
2678 rtx new_inner = NULL_RTX;
2679 rtx new_reg = NULL_RTX;
2680 rtx_insn *insn;
2681 rtx_insn *last_insn = get_last_insn();
2683 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2684 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2685 get_index_code (ad));
2686 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2687 cl, "base");
2688 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2689 ad->disp_term == NULL
2690 ? gen_int_mode (0, ad->mode)
2691 : *ad->disp_term);
2692 if (!valid_address_p (ad->mode, new_inner, ad->as))
2693 return NULL_RTX;
2694 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base_term));
2695 code = recog_memoized (insn);
2696 if (code < 0)
2698 delete_insns_since (last_insn);
2699 return NULL_RTX;
2702 return new_inner;
2705 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2706 static rtx
2707 base_plus_disp_to_reg (struct address_info *ad)
2709 enum reg_class cl;
2710 rtx new_reg;
2712 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2713 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2714 get_index_code (ad));
2715 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2716 cl, "base + disp");
2717 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2718 return new_reg;
2721 /* Make reload of index part of address AD. Return the new
2722 pseudo. */
2723 static rtx
2724 index_part_to_reg (struct address_info *ad)
2726 rtx new_reg;
2728 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2729 INDEX_REG_CLASS, "index term");
2730 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2731 GEN_INT (get_index_scale (ad)), new_reg, 1);
2732 return new_reg;
2735 /* Return true if we can add a displacement to address AD, even if that
2736 makes the address invalid. The fix-up code requires any new address
2737 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2738 static bool
2739 can_add_disp_p (struct address_info *ad)
2741 return (!ad->autoinc_p
2742 && ad->segment == NULL
2743 && ad->base == ad->base_term
2744 && ad->disp == ad->disp_term);
2747 /* Make equiv substitution in address AD. Return true if a substitution
2748 was made. */
2749 static bool
2750 equiv_address_substitution (struct address_info *ad)
2752 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2753 HOST_WIDE_INT disp, scale;
2754 bool change_p;
2756 base_term = strip_subreg (ad->base_term);
2757 if (base_term == NULL)
2758 base_reg = new_base_reg = NULL_RTX;
2759 else
2761 base_reg = *base_term;
2762 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2764 index_term = strip_subreg (ad->index_term);
2765 if (index_term == NULL)
2766 index_reg = new_index_reg = NULL_RTX;
2767 else
2769 index_reg = *index_term;
2770 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2772 if (base_reg == new_base_reg && index_reg == new_index_reg)
2773 return false;
2774 disp = 0;
2775 change_p = false;
2776 if (lra_dump_file != NULL)
2778 fprintf (lra_dump_file, "Changing address in insn %d ",
2779 INSN_UID (curr_insn));
2780 dump_value_slim (lra_dump_file, *ad->outer, 1);
2782 if (base_reg != new_base_reg)
2784 if (REG_P (new_base_reg))
2786 *base_term = new_base_reg;
2787 change_p = true;
2789 else if (GET_CODE (new_base_reg) == PLUS
2790 && REG_P (XEXP (new_base_reg, 0))
2791 && CONST_INT_P (XEXP (new_base_reg, 1))
2792 && can_add_disp_p (ad))
2794 disp += INTVAL (XEXP (new_base_reg, 1));
2795 *base_term = XEXP (new_base_reg, 0);
2796 change_p = true;
2798 if (ad->base_term2 != NULL)
2799 *ad->base_term2 = *ad->base_term;
2801 if (index_reg != new_index_reg)
2803 if (REG_P (new_index_reg))
2805 *index_term = new_index_reg;
2806 change_p = true;
2808 else if (GET_CODE (new_index_reg) == PLUS
2809 && REG_P (XEXP (new_index_reg, 0))
2810 && CONST_INT_P (XEXP (new_index_reg, 1))
2811 && can_add_disp_p (ad)
2812 && (scale = get_index_scale (ad)))
2814 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2815 *index_term = XEXP (new_index_reg, 0);
2816 change_p = true;
2819 if (disp != 0)
2821 if (ad->disp != NULL)
2822 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2823 else
2825 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2826 update_address (ad);
2828 change_p = true;
2830 if (lra_dump_file != NULL)
2832 if (! change_p)
2833 fprintf (lra_dump_file, " -- no change\n");
2834 else
2836 fprintf (lra_dump_file, " on equiv ");
2837 dump_value_slim (lra_dump_file, *ad->outer, 1);
2838 fprintf (lra_dump_file, "\n");
2841 return change_p;
2844 /* Major function to make reloads for an address in operand NOP or
2845 check its correctness (If CHECK_ONLY_P is true). The supported
2846 cases are:
2848 1) an address that existed before LRA started, at which point it
2849 must have been valid. These addresses are subject to elimination
2850 and may have become invalid due to the elimination offset being out
2851 of range.
2853 2) an address created by forcing a constant to memory
2854 (force_const_to_mem). The initial form of these addresses might
2855 not be valid, and it is this function's job to make them valid.
2857 3) a frame address formed from a register and a (possibly zero)
2858 constant offset. As above, these addresses might not be valid and
2859 this function must make them so.
2861 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2862 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2863 address. Return true for any RTL change.
2865 The function is a helper function which does not produce all
2866 transformations (when CHECK_ONLY_P is false) which can be
2867 necessary. It does just basic steps. To do all necessary
2868 transformations use function process_address. */
2869 static bool
2870 process_address_1 (int nop, bool check_only_p,
2871 rtx_insn **before, rtx_insn **after)
2873 struct address_info ad;
2874 rtx new_reg;
2875 rtx op = *curr_id->operand_loc[nop];
2876 const char *constraint = curr_static_id->operand[nop].constraint;
2877 enum constraint_num cn = lookup_constraint (constraint);
2878 bool change_p = false;
2880 if (insn_extra_address_constraint (cn))
2881 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2882 else if (MEM_P (op))
2883 decompose_mem_address (&ad, op);
2884 else if (GET_CODE (op) == SUBREG
2885 && MEM_P (SUBREG_REG (op)))
2886 decompose_mem_address (&ad, SUBREG_REG (op));
2887 else
2888 return false;
2889 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
2890 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
2891 when INDEX_REG_CLASS is a single register class. */
2892 if (ad.base_term != NULL
2893 && ad.index_term != NULL
2894 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
2895 && REG_P (*ad.base_term)
2896 && REG_P (*ad.index_term)
2897 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
2898 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
2900 std::swap (ad.base, ad.index);
2901 std::swap (ad.base_term, ad.index_term);
2903 if (! check_only_p)
2904 change_p = equiv_address_substitution (&ad);
2905 if (ad.base_term != NULL
2906 && (process_addr_reg
2907 (ad.base_term, check_only_p, before,
2908 (ad.autoinc_p
2909 && !(REG_P (*ad.base_term)
2910 && find_regno_note (curr_insn, REG_DEAD,
2911 REGNO (*ad.base_term)) != NULL_RTX)
2912 ? after : NULL),
2913 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2914 get_index_code (&ad)))))
2916 change_p = true;
2917 if (ad.base_term2 != NULL)
2918 *ad.base_term2 = *ad.base_term;
2920 if (ad.index_term != NULL
2921 && process_addr_reg (ad.index_term, check_only_p,
2922 before, NULL, INDEX_REG_CLASS))
2923 change_p = true;
2925 /* Target hooks sometimes don't treat extra-constraint addresses as
2926 legitimate address_operands, so handle them specially. */
2927 if (insn_extra_address_constraint (cn)
2928 && satisfies_address_constraint_p (&ad, cn))
2929 return change_p;
2931 if (check_only_p)
2932 return change_p;
2934 /* There are three cases where the shape of *AD.INNER may now be invalid:
2936 1) the original address was valid, but either elimination or
2937 equiv_address_substitution was applied and that made
2938 the address invalid.
2940 2) the address is an invalid symbolic address created by
2941 force_const_to_mem.
2943 3) the address is a frame address with an invalid offset.
2945 4) the address is a frame address with an invalid base.
2947 All these cases involve a non-autoinc address, so there is no
2948 point revalidating other types. */
2949 if (ad.autoinc_p || valid_address_p (&ad))
2950 return change_p;
2952 /* Any index existed before LRA started, so we can assume that the
2953 presence and shape of the index is valid. */
2954 push_to_sequence (*before);
2955 lra_assert (ad.disp == ad.disp_term);
2956 if (ad.base == NULL)
2958 if (ad.index == NULL)
2960 int code = -1;
2961 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2962 SCRATCH, SCRATCH);
2963 rtx addr = *ad.inner;
2965 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2966 #ifdef HAVE_lo_sum
2968 rtx_insn *insn;
2969 rtx_insn *last = get_last_insn ();
2971 /* addr => lo_sum (new_base, addr), case (2) above. */
2972 insn = emit_insn (gen_rtx_SET
2973 (new_reg,
2974 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2975 code = recog_memoized (insn);
2976 if (code >= 0)
2978 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2979 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2981 /* Try to put lo_sum into register. */
2982 insn = emit_insn (gen_rtx_SET
2983 (new_reg,
2984 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2985 code = recog_memoized (insn);
2986 if (code >= 0)
2988 *ad.inner = new_reg;
2989 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2991 *ad.inner = addr;
2992 code = -1;
2998 if (code < 0)
2999 delete_insns_since (last);
3001 #endif
3002 if (code < 0)
3004 /* addr => new_base, case (2) above. */
3005 lra_emit_move (new_reg, addr);
3006 *ad.inner = new_reg;
3009 else
3011 /* index * scale + disp => new base + index * scale,
3012 case (1) above. */
3013 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3014 GET_CODE (*ad.index));
3016 lra_assert (INDEX_REG_CLASS != NO_REGS);
3017 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3018 lra_emit_move (new_reg, *ad.disp);
3019 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3020 new_reg, *ad.index);
3023 else if (ad.index == NULL)
3025 int regno;
3026 enum reg_class cl;
3027 rtx set;
3028 rtx_insn *insns, *last_insn;
3029 /* Try to reload base into register only if the base is invalid
3030 for the address but with valid offset, case (4) above. */
3031 start_sequence ();
3032 new_reg = base_to_reg (&ad);
3034 /* base + disp => new base, cases (1) and (3) above. */
3035 /* Another option would be to reload the displacement into an
3036 index register. However, postreload has code to optimize
3037 address reloads that have the same base and different
3038 displacements, so reloading into an index register would
3039 not necessarily be a win. */
3040 if (new_reg == NULL_RTX)
3041 new_reg = base_plus_disp_to_reg (&ad);
3042 insns = get_insns ();
3043 last_insn = get_last_insn ();
3044 /* If we generated at least two insns, try last insn source as
3045 an address. If we succeed, we generate one less insn. */
3046 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3047 && GET_CODE (SET_SRC (set)) == PLUS
3048 && REG_P (XEXP (SET_SRC (set), 0))
3049 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3051 *ad.inner = SET_SRC (set);
3052 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3054 *ad.base_term = XEXP (SET_SRC (set), 0);
3055 *ad.disp_term = XEXP (SET_SRC (set), 1);
3056 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3057 get_index_code (&ad));
3058 regno = REGNO (*ad.base_term);
3059 if (regno >= FIRST_PSEUDO_REGISTER
3060 && cl != lra_get_allocno_class (regno))
3061 lra_change_class (regno, cl, " Change to", true);
3062 new_reg = SET_SRC (set);
3063 delete_insns_since (PREV_INSN (last_insn));
3066 /* Try if target can split displacement into legitimite new disp
3067 and offset. If it's the case, we replace the last insn with
3068 insns for base + offset => new_reg and set new_reg + new disp
3069 to *ad.inner. */
3070 last_insn = get_last_insn ();
3071 if ((set = single_set (last_insn)) != NULL_RTX
3072 && GET_CODE (SET_SRC (set)) == PLUS
3073 && REG_P (XEXP (SET_SRC (set), 0))
3074 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3075 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3077 rtx addend, disp = XEXP (SET_SRC (set), 1);
3078 if (targetm.legitimize_address_displacement (&disp, &addend,
3079 ad.mode))
3081 rtx_insn *new_insns;
3082 start_sequence ();
3083 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3084 new_insns = get_insns ();
3085 end_sequence ();
3086 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3087 delete_insns_since (PREV_INSN (last_insn));
3088 add_insn (new_insns);
3089 insns = get_insns ();
3092 end_sequence ();
3093 emit_insn (insns);
3094 *ad.inner = new_reg;
3096 else if (ad.disp_term != NULL)
3098 /* base + scale * index + disp => new base + scale * index,
3099 case (1) above. */
3100 new_reg = base_plus_disp_to_reg (&ad);
3101 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3102 new_reg, *ad.index);
3104 else if (get_index_scale (&ad) == 1)
3106 /* The last transformation to one reg will be made in
3107 curr_insn_transform function. */
3108 end_sequence ();
3109 return false;
3111 else
3113 /* base + scale * index => base + new_reg,
3114 case (1) above.
3115 Index part of address may become invalid. For example, we
3116 changed pseudo on the equivalent memory and a subreg of the
3117 pseudo onto the memory of different mode for which the scale is
3118 prohibitted. */
3119 new_reg = index_part_to_reg (&ad);
3120 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3121 *ad.base_term, new_reg);
3123 *before = get_insns ();
3124 end_sequence ();
3125 return true;
3128 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3129 Use process_address_1 as a helper function. Return true for any
3130 RTL changes.
3132 If CHECK_ONLY_P is true, just check address correctness. Return
3133 false if the address correct. */
3134 static bool
3135 process_address (int nop, bool check_only_p,
3136 rtx_insn **before, rtx_insn **after)
3138 bool res = false;
3140 while (process_address_1 (nop, check_only_p, before, after))
3142 if (check_only_p)
3143 return true;
3144 res = true;
3146 return res;
3149 /* Emit insns to reload VALUE into a new register. VALUE is an
3150 auto-increment or auto-decrement RTX whose operand is a register or
3151 memory location; so reloading involves incrementing that location.
3152 IN is either identical to VALUE, or some cheaper place to reload
3153 value being incremented/decremented from.
3155 INC_AMOUNT is the number to increment or decrement by (always
3156 positive and ignored for POST_MODIFY/PRE_MODIFY).
3158 Return pseudo containing the result. */
3159 static rtx
3160 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3162 /* REG or MEM to be copied and incremented. */
3163 rtx incloc = XEXP (value, 0);
3164 /* Nonzero if increment after copying. */
3165 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3166 || GET_CODE (value) == POST_MODIFY);
3167 rtx_insn *last;
3168 rtx inc;
3169 rtx_insn *add_insn;
3170 int code;
3171 rtx real_in = in == value ? incloc : in;
3172 rtx result;
3173 bool plus_p = true;
3175 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3177 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3178 || GET_CODE (XEXP (value, 1)) == MINUS);
3179 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3180 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3181 inc = XEXP (XEXP (value, 1), 1);
3183 else
3185 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3186 inc_amount = -inc_amount;
3188 inc = GEN_INT (inc_amount);
3191 if (! post && REG_P (incloc))
3192 result = incloc;
3193 else
3194 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3195 "INC/DEC result");
3197 if (real_in != result)
3199 /* First copy the location to the result register. */
3200 lra_assert (REG_P (result));
3201 emit_insn (gen_move_insn (result, real_in));
3204 /* We suppose that there are insns to add/sub with the constant
3205 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3206 old reload worked with this assumption. If the assumption
3207 becomes wrong, we should use approach in function
3208 base_plus_disp_to_reg. */
3209 if (in == value)
3211 /* See if we can directly increment INCLOC. */
3212 last = get_last_insn ();
3213 add_insn = emit_insn (plus_p
3214 ? gen_add2_insn (incloc, inc)
3215 : gen_sub2_insn (incloc, inc));
3217 code = recog_memoized (add_insn);
3218 if (code >= 0)
3220 if (! post && result != incloc)
3221 emit_insn (gen_move_insn (result, incloc));
3222 return result;
3224 delete_insns_since (last);
3227 /* If couldn't do the increment directly, must increment in RESULT.
3228 The way we do this depends on whether this is pre- or
3229 post-increment. For pre-increment, copy INCLOC to the reload
3230 register, increment it there, then save back. */
3231 if (! post)
3233 if (real_in != result)
3234 emit_insn (gen_move_insn (result, real_in));
3235 if (plus_p)
3236 emit_insn (gen_add2_insn (result, inc));
3237 else
3238 emit_insn (gen_sub2_insn (result, inc));
3239 if (result != incloc)
3240 emit_insn (gen_move_insn (incloc, result));
3242 else
3244 /* Post-increment.
3246 Because this might be a jump insn or a compare, and because
3247 RESULT may not be available after the insn in an input
3248 reload, we must do the incrementing before the insn being
3249 reloaded for.
3251 We have already copied IN to RESULT. Increment the copy in
3252 RESULT, save that back, then decrement RESULT so it has
3253 the original value. */
3254 if (plus_p)
3255 emit_insn (gen_add2_insn (result, inc));
3256 else
3257 emit_insn (gen_sub2_insn (result, inc));
3258 emit_insn (gen_move_insn (incloc, result));
3259 /* Restore non-modified value for the result. We prefer this
3260 way because it does not require an additional hard
3261 register. */
3262 if (plus_p)
3264 if (CONST_INT_P (inc))
3265 emit_insn (gen_add2_insn (result,
3266 gen_int_mode (-INTVAL (inc),
3267 GET_MODE (result))));
3268 else
3269 emit_insn (gen_sub2_insn (result, inc));
3271 else
3272 emit_insn (gen_add2_insn (result, inc));
3274 return result;
3277 /* Return true if the current move insn does not need processing as we
3278 already know that it satisfies its constraints. */
3279 static bool
3280 simple_move_p (void)
3282 rtx dest, src;
3283 enum reg_class dclass, sclass;
3285 lra_assert (curr_insn_set != NULL_RTX);
3286 dest = SET_DEST (curr_insn_set);
3287 src = SET_SRC (curr_insn_set);
3288 return ((dclass = get_op_class (dest)) != NO_REGS
3289 && (sclass = get_op_class (src)) != NO_REGS
3290 /* The backend guarantees that register moves of cost 2
3291 never need reloads. */
3292 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3295 /* Swap operands NOP and NOP + 1. */
3296 static inline void
3297 swap_operands (int nop)
3299 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3300 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3301 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3302 /* Swap the duplicates too. */
3303 lra_update_dup (curr_id, nop);
3304 lra_update_dup (curr_id, nop + 1);
3307 /* Main entry point of the constraint code: search the body of the
3308 current insn to choose the best alternative. It is mimicking insn
3309 alternative cost calculation model of former reload pass. That is
3310 because machine descriptions were written to use this model. This
3311 model can be changed in future. Make commutative operand exchange
3312 if it is chosen.
3314 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3315 constraints. Return true if any change happened during function
3316 call.
3318 If CHECK_ONLY_P is true then don't do any transformation. Just
3319 check that the insn satisfies all constraints. If the insn does
3320 not satisfy any constraint, return true. */
3321 static bool
3322 curr_insn_transform (bool check_only_p)
3324 int i, j, k;
3325 int n_operands;
3326 int n_alternatives;
3327 int commutative;
3328 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3329 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3330 rtx_insn *before, *after;
3331 bool alt_p = false;
3332 /* Flag that the insn has been changed through a transformation. */
3333 bool change_p;
3334 bool sec_mem_p;
3335 #ifdef SECONDARY_MEMORY_NEEDED
3336 bool use_sec_mem_p;
3337 #endif
3338 int max_regno_before;
3339 int reused_alternative_num;
3341 curr_insn_set = single_set (curr_insn);
3342 if (curr_insn_set != NULL_RTX && simple_move_p ())
3343 return false;
3345 no_input_reloads_p = no_output_reloads_p = false;
3346 goal_alt_number = -1;
3347 change_p = sec_mem_p = false;
3348 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3349 reloads; neither are insns that SET cc0. Insns that use CC0 are
3350 not allowed to have any input reloads. */
3351 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3352 no_output_reloads_p = true;
3354 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3355 no_input_reloads_p = true;
3356 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3357 no_output_reloads_p = true;
3359 n_operands = curr_static_id->n_operands;
3360 n_alternatives = curr_static_id->n_alternatives;
3362 /* Just return "no reloads" if insn has no operands with
3363 constraints. */
3364 if (n_operands == 0 || n_alternatives == 0)
3365 return false;
3367 max_regno_before = max_reg_num ();
3369 for (i = 0; i < n_operands; i++)
3371 goal_alt_matched[i][0] = -1;
3372 goal_alt_matches[i] = -1;
3375 commutative = curr_static_id->commutative;
3377 /* Now see what we need for pseudos that didn't get hard regs or got
3378 the wrong kind of hard reg. For this, we must consider all the
3379 operands together against the register constraints. */
3381 best_losers = best_overall = INT_MAX;
3382 best_reload_sum = 0;
3384 curr_swapped = false;
3385 goal_alt_swapped = false;
3387 if (! check_only_p)
3388 /* Make equivalence substitution and memory subreg elimination
3389 before address processing because an address legitimacy can
3390 depend on memory mode. */
3391 for (i = 0; i < n_operands; i++)
3393 rtx op = *curr_id->operand_loc[i];
3394 rtx subst, old = op;
3395 bool op_change_p = false;
3397 if (GET_CODE (old) == SUBREG)
3398 old = SUBREG_REG (old);
3399 subst = get_equiv_with_elimination (old, curr_insn);
3400 original_subreg_reg_mode[i] = VOIDmode;
3401 if (subst != old)
3403 subst = copy_rtx (subst);
3404 lra_assert (REG_P (old));
3405 if (GET_CODE (op) != SUBREG)
3406 *curr_id->operand_loc[i] = subst;
3407 else
3409 SUBREG_REG (op) = subst;
3410 if (GET_MODE (subst) == VOIDmode)
3411 original_subreg_reg_mode[i] = GET_MODE (old);
3413 if (lra_dump_file != NULL)
3415 fprintf (lra_dump_file,
3416 "Changing pseudo %d in operand %i of insn %u on equiv ",
3417 REGNO (old), i, INSN_UID (curr_insn));
3418 dump_value_slim (lra_dump_file, subst, 1);
3419 fprintf (lra_dump_file, "\n");
3421 op_change_p = change_p = true;
3423 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3425 change_p = true;
3426 lra_update_dup (curr_id, i);
3430 /* Reload address registers and displacements. We do it before
3431 finding an alternative because of memory constraints. */
3432 before = after = NULL;
3433 for (i = 0; i < n_operands; i++)
3434 if (! curr_static_id->operand[i].is_operator
3435 && process_address (i, check_only_p, &before, &after))
3437 if (check_only_p)
3438 return true;
3439 change_p = true;
3440 lra_update_dup (curr_id, i);
3443 if (change_p)
3444 /* If we've changed the instruction then any alternative that
3445 we chose previously may no longer be valid. */
3446 lra_set_used_insn_alternative (curr_insn, -1);
3448 if (! check_only_p && curr_insn_set != NULL_RTX
3449 && check_and_process_move (&change_p, &sec_mem_p))
3450 return change_p;
3452 try_swapped:
3454 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3455 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3456 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3457 reused_alternative_num, INSN_UID (curr_insn));
3459 if (process_alt_operands (reused_alternative_num))
3460 alt_p = true;
3462 if (check_only_p)
3463 return ! alt_p || best_losers != 0;
3465 /* If insn is commutative (it's safe to exchange a certain pair of
3466 operands) then we need to try each alternative twice, the second
3467 time matching those two operands as if we had exchanged them. To
3468 do this, really exchange them in operands.
3470 If we have just tried the alternatives the second time, return
3471 operands to normal and drop through. */
3473 if (reused_alternative_num < 0 && commutative >= 0)
3475 curr_swapped = !curr_swapped;
3476 if (curr_swapped)
3478 swap_operands (commutative);
3479 goto try_swapped;
3481 else
3482 swap_operands (commutative);
3485 if (! alt_p && ! sec_mem_p)
3487 /* No alternative works with reloads?? */
3488 if (INSN_CODE (curr_insn) >= 0)
3489 fatal_insn ("unable to generate reloads for:", curr_insn);
3490 error_for_asm (curr_insn,
3491 "inconsistent operand constraints in an %<asm%>");
3492 /* Avoid further trouble with this insn. */
3493 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3494 lra_invalidate_insn_data (curr_insn);
3495 return true;
3498 /* If the best alternative is with operands 1 and 2 swapped, swap
3499 them. Update the operand numbers of any reloads already
3500 pushed. */
3502 if (goal_alt_swapped)
3504 if (lra_dump_file != NULL)
3505 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3506 INSN_UID (curr_insn));
3508 /* Swap the duplicates too. */
3509 swap_operands (commutative);
3510 change_p = true;
3513 #ifdef SECONDARY_MEMORY_NEEDED
3514 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3515 too conservatively. So we use the secondary memory only if there
3516 is no any alternative without reloads. */
3517 use_sec_mem_p = false;
3518 if (! alt_p)
3519 use_sec_mem_p = true;
3520 else if (sec_mem_p)
3522 for (i = 0; i < n_operands; i++)
3523 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3524 break;
3525 use_sec_mem_p = i < n_operands;
3528 if (use_sec_mem_p)
3530 rtx new_reg, src, dest, rld;
3531 machine_mode sec_mode, rld_mode;
3533 lra_assert (sec_mem_p);
3534 lra_assert (curr_static_id->operand[0].type == OP_OUT
3535 && curr_static_id->operand[1].type == OP_IN);
3536 dest = *curr_id->operand_loc[0];
3537 src = *curr_id->operand_loc[1];
3538 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3539 ? dest : src);
3540 rld_mode = GET_MODE (rld);
3541 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3542 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3543 #else
3544 sec_mode = rld_mode;
3545 #endif
3546 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3547 NO_REGS, "secondary");
3548 /* If the mode is changed, it should be wider. */
3549 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3550 if (sec_mode != rld_mode)
3552 /* If the target says specifically to use another mode for
3553 secondary memory moves we can not reuse the original
3554 insn. */
3555 after = emit_spill_move (false, new_reg, dest);
3556 lra_process_new_insns (curr_insn, NULL, after,
3557 "Inserting the sec. move");
3558 /* We may have non null BEFORE here (e.g. after address
3559 processing. */
3560 push_to_sequence (before);
3561 before = emit_spill_move (true, new_reg, src);
3562 emit_insn (before);
3563 before = get_insns ();
3564 end_sequence ();
3565 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3566 lra_set_insn_deleted (curr_insn);
3568 else if (dest == rld)
3570 *curr_id->operand_loc[0] = new_reg;
3571 after = emit_spill_move (false, new_reg, dest);
3572 lra_process_new_insns (curr_insn, NULL, after,
3573 "Inserting the sec. move");
3575 else
3577 *curr_id->operand_loc[1] = new_reg;
3578 /* See comments above. */
3579 push_to_sequence (before);
3580 before = emit_spill_move (true, new_reg, src);
3581 emit_insn (before);
3582 before = get_insns ();
3583 end_sequence ();
3584 lra_process_new_insns (curr_insn, before, NULL,
3585 "Inserting the sec. move");
3587 lra_update_insn_regno_info (curr_insn);
3588 return true;
3590 #endif
3592 lra_assert (goal_alt_number >= 0);
3593 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3595 if (lra_dump_file != NULL)
3597 const char *p;
3599 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3600 goal_alt_number, INSN_UID (curr_insn));
3601 for (i = 0; i < n_operands; i++)
3603 p = (curr_static_id->operand_alternative
3604 [goal_alt_number * n_operands + i].constraint);
3605 if (*p == '\0')
3606 continue;
3607 fprintf (lra_dump_file, " (%d) ", i);
3608 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3609 fputc (*p, lra_dump_file);
3611 if (INSN_CODE (curr_insn) >= 0
3612 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3613 fprintf (lra_dump_file, " {%s}", p);
3614 if (curr_id->sp_offset != 0)
3615 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3616 curr_id->sp_offset);
3617 fprintf (lra_dump_file, "\n");
3620 /* Right now, for any pair of operands I and J that are required to
3621 match, with J < I, goal_alt_matches[I] is J. Add I to
3622 goal_alt_matched[J]. */
3624 for (i = 0; i < n_operands; i++)
3625 if ((j = goal_alt_matches[i]) >= 0)
3627 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3629 /* We allow matching one output operand and several input
3630 operands. */
3631 lra_assert (k == 0
3632 || (curr_static_id->operand[j].type == OP_OUT
3633 && curr_static_id->operand[i].type == OP_IN
3634 && (curr_static_id->operand
3635 [goal_alt_matched[j][0]].type == OP_IN)));
3636 goal_alt_matched[j][k] = i;
3637 goal_alt_matched[j][k + 1] = -1;
3640 for (i = 0; i < n_operands; i++)
3641 goal_alt_win[i] |= goal_alt_match_win[i];
3643 /* Any constants that aren't allowed and can't be reloaded into
3644 registers are here changed into memory references. */
3645 for (i = 0; i < n_operands; i++)
3646 if (goal_alt_win[i])
3648 int regno;
3649 enum reg_class new_class;
3650 rtx reg = *curr_id->operand_loc[i];
3652 if (GET_CODE (reg) == SUBREG)
3653 reg = SUBREG_REG (reg);
3655 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3657 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3659 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3661 lra_assert (ok_p);
3662 lra_change_class (regno, new_class, " Change to", true);
3666 else
3668 const char *constraint;
3669 char c;
3670 rtx op = *curr_id->operand_loc[i];
3671 rtx subreg = NULL_RTX;
3672 machine_mode mode = curr_operand_mode[i];
3674 if (GET_CODE (op) == SUBREG)
3676 subreg = op;
3677 op = SUBREG_REG (op);
3678 mode = GET_MODE (op);
3681 if (CONST_POOL_OK_P (mode, op)
3682 && ((targetm.preferred_reload_class
3683 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3684 || no_input_reloads_p))
3686 rtx tem = force_const_mem (mode, op);
3688 change_p = true;
3689 if (subreg != NULL_RTX)
3690 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3692 *curr_id->operand_loc[i] = tem;
3693 lra_update_dup (curr_id, i);
3694 process_address (i, false, &before, &after);
3696 /* If the alternative accepts constant pool refs directly
3697 there will be no reload needed at all. */
3698 if (subreg != NULL_RTX)
3699 continue;
3700 /* Skip alternatives before the one requested. */
3701 constraint = (curr_static_id->operand_alternative
3702 [goal_alt_number * n_operands + i].constraint);
3703 for (;
3704 (c = *constraint) && c != ',' && c != '#';
3705 constraint += CONSTRAINT_LEN (c, constraint))
3707 enum constraint_num cn = lookup_constraint (constraint);
3708 if (insn_extra_memory_constraint (cn)
3709 && satisfies_memory_constraint_p (tem, cn))
3710 break;
3712 if (c == '\0' || c == ',' || c == '#')
3713 continue;
3715 goal_alt_win[i] = true;
3719 for (i = 0; i < n_operands; i++)
3721 int regno;
3722 bool optional_p = false;
3723 rtx old, new_reg;
3724 rtx op = *curr_id->operand_loc[i];
3726 if (goal_alt_win[i])
3728 if (goal_alt[i] == NO_REGS
3729 && REG_P (op)
3730 /* When we assign NO_REGS it means that we will not
3731 assign a hard register to the scratch pseudo by
3732 assigment pass and the scratch pseudo will be
3733 spilled. Spilled scratch pseudos are transformed
3734 back to scratches at the LRA end. */
3735 && lra_former_scratch_operand_p (curr_insn, i))
3737 int regno = REGNO (op);
3738 lra_change_class (regno, NO_REGS, " Change to", true);
3739 if (lra_get_regno_hard_regno (regno) >= 0)
3740 /* We don't have to mark all insn affected by the
3741 spilled pseudo as there is only one such insn, the
3742 current one. */
3743 reg_renumber[regno] = -1;
3745 /* We can do an optional reload. If the pseudo got a hard
3746 reg, we might improve the code through inheritance. If
3747 it does not get a hard register we coalesce memory/memory
3748 moves later. Ignore move insns to avoid cycling. */
3749 if (! lra_simple_p
3750 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3751 && goal_alt[i] != NO_REGS && REG_P (op)
3752 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3753 && regno < new_regno_start
3754 && ! lra_former_scratch_p (regno)
3755 && reg_renumber[regno] < 0
3756 /* Check that the optional reload pseudo will be able to
3757 hold given mode value. */
3758 && ! (prohibited_class_reg_set_mode_p
3759 (goal_alt[i], reg_class_contents[goal_alt[i]],
3760 PSEUDO_REGNO_MODE (regno)))
3761 && (curr_insn_set == NULL_RTX
3762 || !((REG_P (SET_SRC (curr_insn_set))
3763 || MEM_P (SET_SRC (curr_insn_set))
3764 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3765 && (REG_P (SET_DEST (curr_insn_set))
3766 || MEM_P (SET_DEST (curr_insn_set))
3767 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3768 optional_p = true;
3769 else
3770 continue;
3773 /* Operands that match previous ones have already been handled. */
3774 if (goal_alt_matches[i] >= 0)
3775 continue;
3777 /* We should not have an operand with a non-offsettable address
3778 appearing where an offsettable address will do. It also may
3779 be a case when the address should be special in other words
3780 not a general one (e.g. it needs no index reg). */
3781 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3783 enum reg_class rclass;
3784 rtx *loc = &XEXP (op, 0);
3785 enum rtx_code code = GET_CODE (*loc);
3787 push_to_sequence (before);
3788 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3789 MEM, SCRATCH);
3790 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3791 new_reg = emit_inc (rclass, *loc, *loc,
3792 /* This value does not matter for MODIFY. */
3793 GET_MODE_SIZE (GET_MODE (op)));
3794 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3795 "offsetable address", &new_reg))
3796 lra_emit_move (new_reg, *loc);
3797 before = get_insns ();
3798 end_sequence ();
3799 *loc = new_reg;
3800 lra_update_dup (curr_id, i);
3802 else if (goal_alt_matched[i][0] == -1)
3804 machine_mode mode;
3805 rtx reg, *loc;
3806 int hard_regno, byte;
3807 enum op_type type = curr_static_id->operand[i].type;
3809 loc = curr_id->operand_loc[i];
3810 mode = curr_operand_mode[i];
3811 if (GET_CODE (*loc) == SUBREG)
3813 reg = SUBREG_REG (*loc);
3814 byte = SUBREG_BYTE (*loc);
3815 if (REG_P (reg)
3816 /* Strict_low_part requires reload the register not
3817 the sub-register. */
3818 && (curr_static_id->operand[i].strict_low
3819 || (GET_MODE_SIZE (mode)
3820 <= GET_MODE_SIZE (GET_MODE (reg))
3821 && (hard_regno
3822 = get_try_hard_regno (REGNO (reg))) >= 0
3823 && (simplify_subreg_regno
3824 (hard_regno,
3825 GET_MODE (reg), byte, mode) < 0)
3826 && (goal_alt[i] == NO_REGS
3827 || (simplify_subreg_regno
3828 (ira_class_hard_regs[goal_alt[i]][0],
3829 GET_MODE (reg), byte, mode) >= 0)))))
3831 if (type == OP_OUT)
3832 type = OP_INOUT;
3833 loc = &SUBREG_REG (*loc);
3834 mode = GET_MODE (*loc);
3837 old = *loc;
3838 if (get_reload_reg (type, mode, old, goal_alt[i],
3839 loc != curr_id->operand_loc[i], "", &new_reg)
3840 && type != OP_OUT)
3842 push_to_sequence (before);
3843 lra_emit_move (new_reg, old);
3844 before = get_insns ();
3845 end_sequence ();
3847 *loc = new_reg;
3848 if (type != OP_IN
3849 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3851 start_sequence ();
3852 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3853 emit_insn (after);
3854 after = get_insns ();
3855 end_sequence ();
3856 *loc = new_reg;
3858 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3859 if (goal_alt_dont_inherit_ops[j] == i)
3861 lra_set_regno_unique_value (REGNO (new_reg));
3862 break;
3864 lra_update_dup (curr_id, i);
3866 else if (curr_static_id->operand[i].type == OP_IN
3867 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3868 == OP_OUT))
3870 /* generate reloads for input and matched outputs. */
3871 match_inputs[0] = i;
3872 match_inputs[1] = -1;
3873 match_reload (goal_alt_matched[i][0], match_inputs,
3874 goal_alt[i], &before, &after);
3876 else if (curr_static_id->operand[i].type == OP_OUT
3877 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3878 == OP_IN))
3879 /* Generate reloads for output and matched inputs. */
3880 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3881 else if (curr_static_id->operand[i].type == OP_IN
3882 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3883 == OP_IN))
3885 /* Generate reloads for matched inputs. */
3886 match_inputs[0] = i;
3887 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3888 match_inputs[j + 1] = k;
3889 match_inputs[j + 1] = -1;
3890 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3892 else
3893 /* We must generate code in any case when function
3894 process_alt_operands decides that it is possible. */
3895 gcc_unreachable ();
3896 if (optional_p)
3898 lra_assert (REG_P (op));
3899 regno = REGNO (op);
3900 op = *curr_id->operand_loc[i]; /* Substitution. */
3901 if (GET_CODE (op) == SUBREG)
3902 op = SUBREG_REG (op);
3903 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3904 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3905 lra_reg_info[REGNO (op)].restore_regno = regno;
3906 if (lra_dump_file != NULL)
3907 fprintf (lra_dump_file,
3908 " Making reload reg %d for reg %d optional\n",
3909 REGNO (op), regno);
3912 if (before != NULL_RTX || after != NULL_RTX
3913 || max_regno_before != max_reg_num ())
3914 change_p = true;
3915 if (change_p)
3917 lra_update_operator_dups (curr_id);
3918 /* Something changes -- process the insn. */
3919 lra_update_insn_regno_info (curr_insn);
3921 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3922 return change_p;
3925 /* Return true if INSN satisfies all constraints. In other words, no
3926 reload insns are needed. */
3927 bool
3928 lra_constrain_insn (rtx_insn *insn)
3930 int saved_new_regno_start = new_regno_start;
3931 int saved_new_insn_uid_start = new_insn_uid_start;
3932 bool change_p;
3934 curr_insn = insn;
3935 curr_id = lra_get_insn_recog_data (curr_insn);
3936 curr_static_id = curr_id->insn_static_data;
3937 new_insn_uid_start = get_max_uid ();
3938 new_regno_start = max_reg_num ();
3939 change_p = curr_insn_transform (true);
3940 new_regno_start = saved_new_regno_start;
3941 new_insn_uid_start = saved_new_insn_uid_start;
3942 return ! change_p;
3945 /* Return true if X is in LIST. */
3946 static bool
3947 in_list_p (rtx x, rtx list)
3949 for (; list != NULL_RTX; list = XEXP (list, 1))
3950 if (XEXP (list, 0) == x)
3951 return true;
3952 return false;
3955 /* Return true if X contains an allocatable hard register (if
3956 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3957 static bool
3958 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3960 int i, j;
3961 const char *fmt;
3962 enum rtx_code code;
3964 code = GET_CODE (x);
3965 if (REG_P (x))
3967 int regno = REGNO (x);
3968 HARD_REG_SET alloc_regs;
3970 if (hard_reg_p)
3972 if (regno >= FIRST_PSEUDO_REGISTER)
3973 regno = lra_get_regno_hard_regno (regno);
3974 if (regno < 0)
3975 return false;
3976 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3977 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3979 else
3981 if (regno < FIRST_PSEUDO_REGISTER)
3982 return false;
3983 if (! spilled_p)
3984 return true;
3985 return lra_get_regno_hard_regno (regno) < 0;
3988 fmt = GET_RTX_FORMAT (code);
3989 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3991 if (fmt[i] == 'e')
3993 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3994 return true;
3996 else if (fmt[i] == 'E')
3998 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3999 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4000 return true;
4003 return false;
4006 /* Return true if X contains a symbol reg. */
4007 static bool
4008 contains_symbol_ref_p (rtx x)
4010 int i, j;
4011 const char *fmt;
4012 enum rtx_code code;
4014 code = GET_CODE (x);
4015 if (code == SYMBOL_REF)
4016 return true;
4017 fmt = GET_RTX_FORMAT (code);
4018 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4020 if (fmt[i] == 'e')
4022 if (contains_symbol_ref_p (XEXP (x, i)))
4023 return true;
4025 else if (fmt[i] == 'E')
4027 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4028 if (contains_symbol_ref_p (XVECEXP (x, i, j)))
4029 return true;
4032 return false;
4035 /* Process all regs in location *LOC and change them on equivalent
4036 substitution. Return true if any change was done. */
4037 static bool
4038 loc_equivalence_change_p (rtx *loc)
4040 rtx subst, reg, x = *loc;
4041 bool result = false;
4042 enum rtx_code code = GET_CODE (x);
4043 const char *fmt;
4044 int i, j;
4046 if (code == SUBREG)
4048 reg = SUBREG_REG (x);
4049 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4050 && GET_MODE (subst) == VOIDmode)
4052 /* We cannot reload debug location. Simplify subreg here
4053 while we know the inner mode. */
4054 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4055 GET_MODE (reg), SUBREG_BYTE (x));
4056 return true;
4059 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4061 *loc = subst;
4062 return true;
4065 /* Scan all the operand sub-expressions. */
4066 fmt = GET_RTX_FORMAT (code);
4067 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4069 if (fmt[i] == 'e')
4070 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4071 else if (fmt[i] == 'E')
4072 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4073 result
4074 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4076 return result;
4079 /* Similar to loc_equivalence_change_p, but for use as
4080 simplify_replace_fn_rtx callback. DATA is insn for which the
4081 elimination is done. If it null we don't do the elimination. */
4082 static rtx
4083 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4085 if (!REG_P (loc))
4086 return NULL_RTX;
4088 rtx subst = (data == NULL
4089 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4090 if (subst != loc)
4091 return subst;
4093 return NULL_RTX;
4096 /* Maximum number of generated reload insns per an insn. It is for
4097 preventing this pass cycling in a bug case. */
4098 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4100 /* The current iteration number of this LRA pass. */
4101 int lra_constraint_iter;
4103 /* True if we substituted equiv which needs checking register
4104 allocation correctness because the equivalent value contains
4105 allocatable hard registers or when we restore multi-register
4106 pseudo. */
4107 bool lra_risky_transformations_p;
4109 /* Return true if REGNO is referenced in more than one block. */
4110 static bool
4111 multi_block_pseudo_p (int regno)
4113 basic_block bb = NULL;
4114 unsigned int uid;
4115 bitmap_iterator bi;
4117 if (regno < FIRST_PSEUDO_REGISTER)
4118 return false;
4120 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4121 if (bb == NULL)
4122 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4123 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4124 return true;
4125 return false;
4128 /* Return true if LIST contains a deleted insn. */
4129 static bool
4130 contains_deleted_insn_p (rtx_insn_list *list)
4132 for (; list != NULL_RTX; list = list->next ())
4133 if (NOTE_P (list->insn ())
4134 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4135 return true;
4136 return false;
4139 /* Return true if X contains a pseudo dying in INSN. */
4140 static bool
4141 dead_pseudo_p (rtx x, rtx_insn *insn)
4143 int i, j;
4144 const char *fmt;
4145 enum rtx_code code;
4147 if (REG_P (x))
4148 return (insn != NULL_RTX
4149 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4150 code = GET_CODE (x);
4151 fmt = GET_RTX_FORMAT (code);
4152 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4154 if (fmt[i] == 'e')
4156 if (dead_pseudo_p (XEXP (x, i), insn))
4157 return true;
4159 else if (fmt[i] == 'E')
4161 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4162 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4163 return true;
4166 return false;
4169 /* Return true if INSN contains a dying pseudo in INSN right hand
4170 side. */
4171 static bool
4172 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4174 rtx set = single_set (insn);
4176 gcc_assert (set != NULL);
4177 return dead_pseudo_p (SET_SRC (set), insn);
4180 /* Return true if any init insn of REGNO contains a dying pseudo in
4181 insn right hand side. */
4182 static bool
4183 init_insn_rhs_dead_pseudo_p (int regno)
4185 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4187 if (insns == NULL)
4188 return false;
4189 for (; insns != NULL_RTX; insns = insns->next ())
4190 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4191 return true;
4192 return false;
4195 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4196 reverse only if we have one init insn with given REGNO as a
4197 source. */
4198 static bool
4199 reverse_equiv_p (int regno)
4201 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4202 rtx set;
4204 if (insns == NULL)
4205 return false;
4206 if (! INSN_P (insns->insn ())
4207 || insns->next () != NULL)
4208 return false;
4209 if ((set = single_set (insns->insn ())) == NULL_RTX)
4210 return false;
4211 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4214 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4215 call this function only for non-reverse equivalence. */
4216 static bool
4217 contains_reloaded_insn_p (int regno)
4219 rtx set;
4220 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4222 for (; list != NULL; list = list->next ())
4223 if ((set = single_set (list->insn ())) == NULL_RTX
4224 || ! REG_P (SET_DEST (set))
4225 || (int) REGNO (SET_DEST (set)) != regno)
4226 return true;
4227 return false;
4230 /* Entry function of LRA constraint pass. Return true if the
4231 constraint pass did change the code. */
4232 bool
4233 lra_constraints (bool first_p)
4235 bool changed_p;
4236 int i, hard_regno, new_insns_num;
4237 unsigned int min_len, new_min_len, uid;
4238 rtx set, x, reg, dest_reg;
4239 basic_block last_bb;
4240 bitmap_head equiv_insn_bitmap;
4241 bitmap_iterator bi;
4243 lra_constraint_iter++;
4244 if (lra_dump_file != NULL)
4245 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4246 lra_constraint_iter);
4247 changed_p = false;
4248 if (pic_offset_table_rtx
4249 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4250 lra_risky_transformations_p = true;
4251 else
4252 lra_risky_transformations_p = false;
4253 new_insn_uid_start = get_max_uid ();
4254 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4255 /* Mark used hard regs for target stack size calulations. */
4256 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4257 if (lra_reg_info[i].nrefs != 0
4258 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4260 int j, nregs;
4262 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4263 for (j = 0; j < nregs; j++)
4264 df_set_regs_ever_live (hard_regno + j, true);
4266 /* Do elimination before the equivalence processing as we can spill
4267 some pseudos during elimination. */
4268 lra_eliminate (false, first_p);
4269 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4270 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4271 if (lra_reg_info[i].nrefs != 0)
4273 ira_reg_equiv[i].profitable_p = true;
4274 reg = regno_reg_rtx[i];
4275 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4277 bool pseudo_p = contains_reg_p (x, false, false);
4279 /* After RTL transformation, we can not guarantee that
4280 pseudo in the substitution was not reloaded which might
4281 make equivalence invalid. For example, in reverse
4282 equiv of p0
4284 p0 <- ...
4286 equiv_mem <- p0
4288 the memory address register was reloaded before the 2nd
4289 insn. */
4290 if ((! first_p && pseudo_p)
4291 /* We don't use DF for compilation speed sake. So it
4292 is problematic to update live info when we use an
4293 equivalence containing pseudos in more than one
4294 BB. */
4295 || (pseudo_p && multi_block_pseudo_p (i))
4296 /* If an init insn was deleted for some reason, cancel
4297 the equiv. We could update the equiv insns after
4298 transformations including an equiv insn deletion
4299 but it is not worthy as such cases are extremely
4300 rare. */
4301 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4302 /* If it is not a reverse equivalence, we check that a
4303 pseudo in rhs of the init insn is not dying in the
4304 insn. Otherwise, the live info at the beginning of
4305 the corresponding BB might be wrong after we
4306 removed the insn. When the equiv can be a
4307 constant, the right hand side of the init insn can
4308 be a pseudo. */
4309 || (! reverse_equiv_p (i)
4310 && (init_insn_rhs_dead_pseudo_p (i)
4311 /* If we reloaded the pseudo in an equivalence
4312 init insn, we can not remove the equiv init
4313 insns and the init insns might write into
4314 const memory in this case. */
4315 || contains_reloaded_insn_p (i)))
4316 /* Prevent access beyond equivalent memory for
4317 paradoxical subregs. */
4318 || (MEM_P (x)
4319 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4320 > GET_MODE_SIZE (GET_MODE (x))))
4321 || (pic_offset_table_rtx
4322 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4323 && (targetm.preferred_reload_class
4324 (x, lra_get_allocno_class (i)) == NO_REGS))
4325 || contains_symbol_ref_p (x))))
4326 ira_reg_equiv[i].defined_p = false;
4327 if (contains_reg_p (x, false, true))
4328 ira_reg_equiv[i].profitable_p = false;
4329 if (get_equiv (reg) != reg)
4330 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4333 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4334 update_equiv (i);
4335 /* We should add all insns containing pseudos which should be
4336 substituted by their equivalences. */
4337 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4338 lra_push_insn_by_uid (uid);
4339 min_len = lra_insn_stack_length ();
4340 new_insns_num = 0;
4341 last_bb = NULL;
4342 changed_p = false;
4343 while ((new_min_len = lra_insn_stack_length ()) != 0)
4345 curr_insn = lra_pop_insn ();
4346 --new_min_len;
4347 curr_bb = BLOCK_FOR_INSN (curr_insn);
4348 if (curr_bb != last_bb)
4350 last_bb = curr_bb;
4351 bb_reload_num = lra_curr_reload_num;
4353 if (min_len > new_min_len)
4355 min_len = new_min_len;
4356 new_insns_num = 0;
4358 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4359 internal_error
4360 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4361 MAX_RELOAD_INSNS_NUMBER);
4362 new_insns_num++;
4363 if (DEBUG_INSN_P (curr_insn))
4365 /* We need to check equivalence in debug insn and change
4366 pseudo to the equivalent value if necessary. */
4367 curr_id = lra_get_insn_recog_data (curr_insn);
4368 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4370 rtx old = *curr_id->operand_loc[0];
4371 *curr_id->operand_loc[0]
4372 = simplify_replace_fn_rtx (old, NULL_RTX,
4373 loc_equivalence_callback, curr_insn);
4374 if (old != *curr_id->operand_loc[0])
4376 lra_update_insn_regno_info (curr_insn);
4377 changed_p = true;
4381 else if (INSN_P (curr_insn))
4383 if ((set = single_set (curr_insn)) != NULL_RTX)
4385 dest_reg = SET_DEST (set);
4386 /* The equivalence pseudo could be set up as SUBREG in a
4387 case when it is a call restore insn in a mode
4388 different from the pseudo mode. */
4389 if (GET_CODE (dest_reg) == SUBREG)
4390 dest_reg = SUBREG_REG (dest_reg);
4391 if ((REG_P (dest_reg)
4392 && (x = get_equiv (dest_reg)) != dest_reg
4393 /* Remove insns which set up a pseudo whose value
4394 can not be changed. Such insns might be not in
4395 init_insns because we don't update equiv data
4396 during insn transformations.
4398 As an example, let suppose that a pseudo got
4399 hard register and on the 1st pass was not
4400 changed to equivalent constant. We generate an
4401 additional insn setting up the pseudo because of
4402 secondary memory movement. Then the pseudo is
4403 spilled and we use the equiv constant. In this
4404 case we should remove the additional insn and
4405 this insn is not init_insns list. */
4406 && (! MEM_P (x) || MEM_READONLY_P (x)
4407 /* Check that this is actually an insn setting
4408 up the equivalence. */
4409 || in_list_p (curr_insn,
4410 ira_reg_equiv
4411 [REGNO (dest_reg)].init_insns)))
4412 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4413 && in_list_p (curr_insn,
4414 ira_reg_equiv
4415 [REGNO (SET_SRC (set))].init_insns)))
4417 /* This is equiv init insn of pseudo which did not get a
4418 hard register -- remove the insn. */
4419 if (lra_dump_file != NULL)
4421 fprintf (lra_dump_file,
4422 " Removing equiv init insn %i (freq=%d)\n",
4423 INSN_UID (curr_insn),
4424 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4425 dump_insn_slim (lra_dump_file, curr_insn);
4427 if (contains_reg_p (x, true, false))
4428 lra_risky_transformations_p = true;
4429 lra_set_insn_deleted (curr_insn);
4430 continue;
4433 curr_id = lra_get_insn_recog_data (curr_insn);
4434 curr_static_id = curr_id->insn_static_data;
4435 init_curr_insn_input_reloads ();
4436 init_curr_operand_mode ();
4437 if (curr_insn_transform (false))
4438 changed_p = true;
4439 /* Check non-transformed insns too for equiv change as USE
4440 or CLOBBER don't need reloads but can contain pseudos
4441 being changed on their equivalences. */
4442 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4443 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4445 lra_update_insn_regno_info (curr_insn);
4446 changed_p = true;
4450 bitmap_clear (&equiv_insn_bitmap);
4451 /* If we used a new hard regno, changed_p should be true because the
4452 hard reg is assigned to a new pseudo. */
4453 #ifdef ENABLE_CHECKING
4454 if (! changed_p)
4456 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4457 if (lra_reg_info[i].nrefs != 0
4458 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4460 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4462 for (j = 0; j < nregs; j++)
4463 lra_assert (df_regs_ever_live_p (hard_regno + j));
4466 #endif
4467 return changed_p;
4470 /* Initiate the LRA constraint pass. It is done once per
4471 function. */
4472 void
4473 lra_constraints_init (void)
4477 /* Finalize the LRA constraint pass. It is done once per
4478 function. */
4479 void
4480 lra_constraints_finish (void)
4486 /* This page contains code to do inheritance/split
4487 transformations. */
4489 /* Number of reloads passed so far in current EBB. */
4490 static int reloads_num;
4492 /* Number of calls passed so far in current EBB. */
4493 static int calls_num;
4495 /* Current reload pseudo check for validity of elements in
4496 USAGE_INSNS. */
4497 static int curr_usage_insns_check;
4499 /* Info about last usage of registers in EBB to do inheritance/split
4500 transformation. Inheritance transformation is done from a spilled
4501 pseudo and split transformations from a hard register or a pseudo
4502 assigned to a hard register. */
4503 struct usage_insns
4505 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4506 value INSNS is valid. The insns is chain of optional debug insns
4507 and a finishing non-debug insn using the corresponding reg. The
4508 value is also used to mark the registers which are set up in the
4509 current insn. The negated insn uid is used for this. */
4510 int check;
4511 /* Value of global reloads_num at the last insn in INSNS. */
4512 int reloads_num;
4513 /* Value of global reloads_nums at the last insn in INSNS. */
4514 int calls_num;
4515 /* It can be true only for splitting. And it means that the restore
4516 insn should be put after insn given by the following member. */
4517 bool after_p;
4518 /* Next insns in the current EBB which use the original reg and the
4519 original reg value is not changed between the current insn and
4520 the next insns. In order words, e.g. for inheritance, if we need
4521 to use the original reg value again in the next insns we can try
4522 to use the value in a hard register from a reload insn of the
4523 current insn. */
4524 rtx insns;
4527 /* Map: regno -> corresponding pseudo usage insns. */
4528 static struct usage_insns *usage_insns;
4530 static void
4531 setup_next_usage_insn (int regno, rtx_insn *insn, int reloads_num, bool after_p)
4533 usage_insns[regno].check = curr_usage_insns_check;
4534 usage_insns[regno].insns = insn;
4535 usage_insns[regno].reloads_num = reloads_num;
4536 usage_insns[regno].calls_num = calls_num;
4537 usage_insns[regno].after_p = after_p;
4540 /* The function is used to form list REGNO usages which consists of
4541 optional debug insns finished by a non-debug insn using REGNO.
4542 RELOADS_NUM is current number of reload insns processed so far. */
4543 static void
4544 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
4546 rtx next_usage_insns;
4548 if (usage_insns[regno].check == curr_usage_insns_check
4549 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4550 && DEBUG_INSN_P (insn))
4552 /* Check that we did not add the debug insn yet. */
4553 if (next_usage_insns != insn
4554 && (GET_CODE (next_usage_insns) != INSN_LIST
4555 || XEXP (next_usage_insns, 0) != insn))
4556 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4557 next_usage_insns);
4559 else if (NONDEBUG_INSN_P (insn))
4560 setup_next_usage_insn (regno, insn, reloads_num, false);
4561 else
4562 usage_insns[regno].check = 0;
4565 /* Return first non-debug insn in list USAGE_INSNS. */
4566 static rtx_insn *
4567 skip_usage_debug_insns (rtx usage_insns)
4569 rtx insn;
4571 /* Skip debug insns. */
4572 for (insn = usage_insns;
4573 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4574 insn = XEXP (insn, 1))
4576 return safe_as_a <rtx_insn *> (insn);
4579 /* Return true if we need secondary memory moves for insn in
4580 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4581 into the insn. */
4582 static bool
4583 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4584 rtx usage_insns ATTRIBUTE_UNUSED)
4586 #ifndef SECONDARY_MEMORY_NEEDED
4587 return false;
4588 #else
4589 rtx_insn *insn;
4590 rtx set, dest;
4591 enum reg_class cl;
4593 if (inher_cl == ALL_REGS
4594 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4595 return false;
4596 lra_assert (INSN_P (insn));
4597 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4598 return false;
4599 dest = SET_DEST (set);
4600 if (! REG_P (dest))
4601 return false;
4602 lra_assert (inher_cl != NO_REGS);
4603 cl = get_reg_class (REGNO (dest));
4604 return (cl != NO_REGS && cl != ALL_REGS
4605 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4606 #endif
4609 /* Registers involved in inheritance/split in the current EBB
4610 (inheritance/split pseudos and original registers). */
4611 static bitmap_head check_only_regs;
4613 /* Do inheritance transformations for insn INSN, which defines (if
4614 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4615 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4616 form as the "insns" field of usage_insns. Return true if we
4617 succeed in such transformation.
4619 The transformations look like:
4621 p <- ... i <- ...
4622 ... p <- i (new insn)
4623 ... =>
4624 <- ... p ... <- ... i ...
4626 ... i <- p (new insn)
4627 <- ... p ... <- ... i ...
4628 ... =>
4629 <- ... p ... <- ... i ...
4630 where p is a spilled original pseudo and i is a new inheritance pseudo.
4633 The inheritance pseudo has the smallest class of two classes CL and
4634 class of ORIGINAL REGNO. */
4635 static bool
4636 inherit_reload_reg (bool def_p, int original_regno,
4637 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4639 if (optimize_function_for_size_p (cfun))
4640 return false;
4642 enum reg_class rclass = lra_get_allocno_class (original_regno);
4643 rtx original_reg = regno_reg_rtx[original_regno];
4644 rtx new_reg, usage_insn;
4645 rtx_insn *new_insns;
4647 lra_assert (! usage_insns[original_regno].after_p);
4648 if (lra_dump_file != NULL)
4649 fprintf (lra_dump_file,
4650 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4651 if (! ira_reg_classes_intersect_p[cl][rclass])
4653 if (lra_dump_file != NULL)
4655 fprintf (lra_dump_file,
4656 " Rejecting inheritance for %d "
4657 "because of disjoint classes %s and %s\n",
4658 original_regno, reg_class_names[cl],
4659 reg_class_names[rclass]);
4660 fprintf (lra_dump_file,
4661 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4663 return false;
4665 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4666 /* We don't use a subset of two classes because it can be
4667 NO_REGS. This transformation is still profitable in most
4668 cases even if the classes are not intersected as register
4669 move is probably cheaper than a memory load. */
4670 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4672 if (lra_dump_file != NULL)
4673 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4674 reg_class_names[cl], reg_class_names[rclass]);
4676 rclass = cl;
4678 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4680 /* Reject inheritance resulting in secondary memory moves.
4681 Otherwise, there is a danger in LRA cycling. Also such
4682 transformation will be unprofitable. */
4683 if (lra_dump_file != NULL)
4685 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4686 rtx set = single_set (insn);
4688 lra_assert (set != NULL_RTX);
4690 rtx dest = SET_DEST (set);
4692 lra_assert (REG_P (dest));
4693 fprintf (lra_dump_file,
4694 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4695 "as secondary mem is needed\n",
4696 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4697 original_regno, reg_class_names[rclass]);
4698 fprintf (lra_dump_file,
4699 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4701 return false;
4703 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4704 rclass, "inheritance");
4705 start_sequence ();
4706 if (def_p)
4707 lra_emit_move (original_reg, new_reg);
4708 else
4709 lra_emit_move (new_reg, original_reg);
4710 new_insns = get_insns ();
4711 end_sequence ();
4712 if (NEXT_INSN (new_insns) != NULL_RTX)
4714 if (lra_dump_file != NULL)
4716 fprintf (lra_dump_file,
4717 " Rejecting inheritance %d->%d "
4718 "as it results in 2 or more insns:\n",
4719 original_regno, REGNO (new_reg));
4720 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4721 fprintf (lra_dump_file,
4722 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4724 return false;
4726 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg);
4727 lra_update_insn_regno_info (insn);
4728 if (! def_p)
4729 /* We now have a new usage insn for original regno. */
4730 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4731 if (lra_dump_file != NULL)
4732 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4733 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4734 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4735 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4736 bitmap_set_bit (&check_only_regs, original_regno);
4737 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4738 if (def_p)
4739 lra_process_new_insns (insn, NULL, new_insns,
4740 "Add original<-inheritance");
4741 else
4742 lra_process_new_insns (insn, new_insns, NULL,
4743 "Add inheritance<-original");
4744 while (next_usage_insns != NULL_RTX)
4746 if (GET_CODE (next_usage_insns) != INSN_LIST)
4748 usage_insn = next_usage_insns;
4749 lra_assert (NONDEBUG_INSN_P (usage_insn));
4750 next_usage_insns = NULL;
4752 else
4754 usage_insn = XEXP (next_usage_insns, 0);
4755 lra_assert (DEBUG_INSN_P (usage_insn));
4756 next_usage_insns = XEXP (next_usage_insns, 1);
4758 lra_substitute_pseudo (&usage_insn, original_regno, new_reg);
4759 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4760 if (lra_dump_file != NULL)
4762 fprintf (lra_dump_file,
4763 " Inheritance reuse change %d->%d (bb%d):\n",
4764 original_regno, REGNO (new_reg),
4765 BLOCK_FOR_INSN (usage_insn)->index);
4766 dump_insn_slim (lra_dump_file, usage_insn);
4769 if (lra_dump_file != NULL)
4770 fprintf (lra_dump_file,
4771 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4772 return true;
4775 /* Return true if we need a caller save/restore for pseudo REGNO which
4776 was assigned to a hard register. */
4777 static inline bool
4778 need_for_call_save_p (int regno)
4780 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4781 return (usage_insns[regno].calls_num < calls_num
4782 && (overlaps_hard_reg_set_p
4783 ((flag_ipa_ra &&
4784 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4785 ? lra_reg_info[regno].actual_call_used_reg_set
4786 : call_used_reg_set,
4787 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4788 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4789 PSEUDO_REGNO_MODE (regno))));
4792 /* Global registers occurring in the current EBB. */
4793 static bitmap_head ebb_global_regs;
4795 /* Return true if we need a split for hard register REGNO or pseudo
4796 REGNO which was assigned to a hard register.
4797 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4798 used for reloads since the EBB end. It is an approximation of the
4799 used hard registers in the split range. The exact value would
4800 require expensive calculations. If we were aggressive with
4801 splitting because of the approximation, the split pseudo will save
4802 the same hard register assignment and will be removed in the undo
4803 pass. We still need the approximation because too aggressive
4804 splitting would result in too inaccurate cost calculation in the
4805 assignment pass because of too many generated moves which will be
4806 probably removed in the undo pass. */
4807 static inline bool
4808 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4810 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4812 lra_assert (hard_regno >= 0);
4813 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4814 /* Don't split eliminable hard registers, otherwise we can
4815 split hard registers like hard frame pointer, which
4816 lives on BB start/end according to DF-infrastructure,
4817 when there is a pseudo assigned to the register and
4818 living in the same BB. */
4819 && (regno >= FIRST_PSEUDO_REGISTER
4820 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4821 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4822 /* Don't split call clobbered hard regs living through
4823 calls, otherwise we might have a check problem in the
4824 assign sub-pass as in the most cases (exception is a
4825 situation when lra_risky_transformations_p value is
4826 true) the assign pass assumes that all pseudos living
4827 through calls are assigned to call saved hard regs. */
4828 && (regno >= FIRST_PSEUDO_REGISTER
4829 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4830 || usage_insns[regno].calls_num == calls_num)
4831 /* We need at least 2 reloads to make pseudo splitting
4832 profitable. We should provide hard regno splitting in
4833 any case to solve 1st insn scheduling problem when
4834 moving hard register definition up might result in
4835 impossibility to find hard register for reload pseudo of
4836 small register class. */
4837 && (usage_insns[regno].reloads_num
4838 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4839 && (regno < FIRST_PSEUDO_REGISTER
4840 /* For short living pseudos, spilling + inheritance can
4841 be considered a substitution for splitting.
4842 Therefore we do not splitting for local pseudos. It
4843 decreases also aggressiveness of splitting. The
4844 minimal number of references is chosen taking into
4845 account that for 2 references splitting has no sense
4846 as we can just spill the pseudo. */
4847 || (regno >= FIRST_PSEUDO_REGISTER
4848 && lra_reg_info[regno].nrefs > 3
4849 && bitmap_bit_p (&ebb_global_regs, regno))))
4850 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4853 /* Return class for the split pseudo created from original pseudo with
4854 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4855 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4856 results in no secondary memory movements. */
4857 static enum reg_class
4858 choose_split_class (enum reg_class allocno_class,
4859 int hard_regno ATTRIBUTE_UNUSED,
4860 machine_mode mode ATTRIBUTE_UNUSED)
4862 #ifndef SECONDARY_MEMORY_NEEDED
4863 return allocno_class;
4864 #else
4865 int i;
4866 enum reg_class cl, best_cl = NO_REGS;
4867 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4868 = REGNO_REG_CLASS (hard_regno);
4870 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4871 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4872 return allocno_class;
4873 for (i = 0;
4874 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4875 i++)
4876 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4877 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4878 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4879 && (best_cl == NO_REGS
4880 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4881 best_cl = cl;
4882 return best_cl;
4883 #endif
4886 /* Do split transformations for insn INSN, which defines or uses
4887 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4888 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4889 "insns" field of usage_insns.
4891 The transformations look like:
4893 p <- ... p <- ...
4894 ... s <- p (new insn -- save)
4895 ... =>
4896 ... p <- s (new insn -- restore)
4897 <- ... p ... <- ... p ...
4899 <- ... p ... <- ... p ...
4900 ... s <- p (new insn -- save)
4901 ... =>
4902 ... p <- s (new insn -- restore)
4903 <- ... p ... <- ... p ...
4905 where p is an original pseudo got a hard register or a hard
4906 register and s is a new split pseudo. The save is put before INSN
4907 if BEFORE_P is true. Return true if we succeed in such
4908 transformation. */
4909 static bool
4910 split_reg (bool before_p, int original_regno, rtx_insn *insn,
4911 rtx next_usage_insns)
4913 enum reg_class rclass;
4914 rtx original_reg;
4915 int hard_regno, nregs;
4916 rtx new_reg, usage_insn;
4917 rtx_insn *restore, *save;
4918 bool after_p;
4919 bool call_save_p;
4921 if (original_regno < FIRST_PSEUDO_REGISTER)
4923 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4924 hard_regno = original_regno;
4925 call_save_p = false;
4926 nregs = 1;
4928 else
4930 hard_regno = reg_renumber[original_regno];
4931 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4932 rclass = lra_get_allocno_class (original_regno);
4933 original_reg = regno_reg_rtx[original_regno];
4934 call_save_p = need_for_call_save_p (original_regno);
4936 original_reg = regno_reg_rtx[original_regno];
4937 lra_assert (hard_regno >= 0);
4938 if (lra_dump_file != NULL)
4939 fprintf (lra_dump_file,
4940 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4941 if (call_save_p)
4943 machine_mode mode = GET_MODE (original_reg);
4945 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4946 hard_regno_nregs[hard_regno][mode],
4947 mode);
4948 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4950 else
4952 rclass = choose_split_class (rclass, hard_regno,
4953 GET_MODE (original_reg));
4954 if (rclass == NO_REGS)
4956 if (lra_dump_file != NULL)
4958 fprintf (lra_dump_file,
4959 " Rejecting split of %d(%s): "
4960 "no good reg class for %d(%s)\n",
4961 original_regno,
4962 reg_class_names[lra_get_allocno_class (original_regno)],
4963 hard_regno,
4964 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4965 fprintf
4966 (lra_dump_file,
4967 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4969 return false;
4971 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4972 rclass, "split");
4973 reg_renumber[REGNO (new_reg)] = hard_regno;
4975 save = emit_spill_move (true, new_reg, original_reg);
4976 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
4978 if (lra_dump_file != NULL)
4980 fprintf
4981 (lra_dump_file,
4982 " Rejecting split %d->%d resulting in > 2 save insns:\n",
4983 original_regno, REGNO (new_reg));
4984 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
4985 fprintf (lra_dump_file,
4986 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4988 return false;
4990 restore = emit_spill_move (false, new_reg, original_reg);
4991 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
4993 if (lra_dump_file != NULL)
4995 fprintf (lra_dump_file,
4996 " Rejecting split %d->%d "
4997 "resulting in > 2 restore insns:\n",
4998 original_regno, REGNO (new_reg));
4999 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
5000 fprintf (lra_dump_file,
5001 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5003 return false;
5005 after_p = usage_insns[original_regno].after_p;
5006 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
5007 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5008 bitmap_set_bit (&check_only_regs, original_regno);
5009 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
5010 for (;;)
5012 if (GET_CODE (next_usage_insns) != INSN_LIST)
5014 usage_insn = next_usage_insns;
5015 break;
5017 usage_insn = XEXP (next_usage_insns, 0);
5018 lra_assert (DEBUG_INSN_P (usage_insn));
5019 next_usage_insns = XEXP (next_usage_insns, 1);
5020 lra_substitute_pseudo (&usage_insn, original_regno, new_reg);
5021 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5022 if (lra_dump_file != NULL)
5024 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5025 original_regno, REGNO (new_reg));
5026 dump_insn_slim (lra_dump_file, usage_insn);
5029 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5030 lra_assert (usage_insn != insn || (after_p && before_p));
5031 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5032 after_p ? NULL : restore,
5033 after_p ? restore : NULL,
5034 call_save_p
5035 ? "Add reg<-save" : "Add reg<-split");
5036 lra_process_new_insns (insn, before_p ? save : NULL,
5037 before_p ? NULL : save,
5038 call_save_p
5039 ? "Add save<-reg" : "Add split<-reg");
5040 if (nregs > 1)
5041 /* If we are trying to split multi-register. We should check
5042 conflicts on the next assignment sub-pass. IRA can allocate on
5043 sub-register levels, LRA do this on pseudos level right now and
5044 this discrepancy may create allocation conflicts after
5045 splitting. */
5046 lra_risky_transformations_p = true;
5047 if (lra_dump_file != NULL)
5048 fprintf (lra_dump_file,
5049 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5050 return true;
5053 /* Recognize that we need a split transformation for insn INSN, which
5054 defines or uses REGNO in its insn biggest MODE (we use it only if
5055 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5056 hard registers which might be used for reloads since the EBB end.
5057 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5058 uid before starting INSN processing. Return true if we succeed in
5059 such transformation. */
5060 static bool
5061 split_if_necessary (int regno, machine_mode mode,
5062 HARD_REG_SET potential_reload_hard_regs,
5063 bool before_p, rtx_insn *insn, int max_uid)
5065 bool res = false;
5066 int i, nregs = 1;
5067 rtx next_usage_insns;
5069 if (regno < FIRST_PSEUDO_REGISTER)
5070 nregs = hard_regno_nregs[regno][mode];
5071 for (i = 0; i < nregs; i++)
5072 if (usage_insns[regno + i].check == curr_usage_insns_check
5073 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5074 /* To avoid processing the register twice or more. */
5075 && ((GET_CODE (next_usage_insns) != INSN_LIST
5076 && INSN_UID (next_usage_insns) < max_uid)
5077 || (GET_CODE (next_usage_insns) == INSN_LIST
5078 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5079 && need_for_split_p (potential_reload_hard_regs, regno + i)
5080 && split_reg (before_p, regno + i, insn, next_usage_insns))
5081 res = true;
5082 return res;
5085 /* Check only registers living at the current program point in the
5086 current EBB. */
5087 static bitmap_head live_regs;
5089 /* Update live info in EBB given by its HEAD and TAIL insns after
5090 inheritance/split transformation. The function removes dead moves
5091 too. */
5092 static void
5093 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5095 unsigned int j;
5096 int i, regno;
5097 bool live_p;
5098 rtx_insn *prev_insn;
5099 rtx set;
5100 bool remove_p;
5101 basic_block last_bb, prev_bb, curr_bb;
5102 bitmap_iterator bi;
5103 struct lra_insn_reg *reg;
5104 edge e;
5105 edge_iterator ei;
5107 last_bb = BLOCK_FOR_INSN (tail);
5108 prev_bb = NULL;
5109 for (curr_insn = tail;
5110 curr_insn != PREV_INSN (head);
5111 curr_insn = prev_insn)
5113 prev_insn = PREV_INSN (curr_insn);
5114 /* We need to process empty blocks too. They contain
5115 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5116 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5117 continue;
5118 curr_bb = BLOCK_FOR_INSN (curr_insn);
5119 if (curr_bb != prev_bb)
5121 if (prev_bb != NULL)
5123 /* Update df_get_live_in (prev_bb): */
5124 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5125 if (bitmap_bit_p (&live_regs, j))
5126 bitmap_set_bit (df_get_live_in (prev_bb), j);
5127 else
5128 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5130 if (curr_bb != last_bb)
5132 /* Update df_get_live_out (curr_bb): */
5133 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5135 live_p = bitmap_bit_p (&live_regs, j);
5136 if (! live_p)
5137 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5138 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5140 live_p = true;
5141 break;
5143 if (live_p)
5144 bitmap_set_bit (df_get_live_out (curr_bb), j);
5145 else
5146 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5149 prev_bb = curr_bb;
5150 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5152 if (! NONDEBUG_INSN_P (curr_insn))
5153 continue;
5154 curr_id = lra_get_insn_recog_data (curr_insn);
5155 curr_static_id = curr_id->insn_static_data;
5156 remove_p = false;
5157 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
5158 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5159 && bitmap_bit_p (&check_only_regs, regno)
5160 && ! bitmap_bit_p (&live_regs, regno))
5161 remove_p = true;
5162 /* See which defined values die here. */
5163 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5164 if (reg->type == OP_OUT && ! reg->subreg_p)
5165 bitmap_clear_bit (&live_regs, reg->regno);
5166 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5167 if (reg->type == OP_OUT && ! reg->subreg_p)
5168 bitmap_clear_bit (&live_regs, reg->regno);
5169 /* Mark each used value as live. */
5170 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5171 if (reg->type != OP_OUT
5172 && bitmap_bit_p (&check_only_regs, reg->regno))
5173 bitmap_set_bit (&live_regs, reg->regno);
5174 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5175 if (reg->type != OP_OUT
5176 && bitmap_bit_p (&check_only_regs, reg->regno))
5177 bitmap_set_bit (&live_regs, reg->regno);
5178 if (curr_id->arg_hard_regs != NULL)
5179 /* Make argument hard registers live. */
5180 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5181 if (bitmap_bit_p (&check_only_regs, regno))
5182 bitmap_set_bit (&live_regs, regno);
5183 /* It is quite important to remove dead move insns because it
5184 means removing dead store. We don't need to process them for
5185 constraints. */
5186 if (remove_p)
5188 if (lra_dump_file != NULL)
5190 fprintf (lra_dump_file, " Removing dead insn:\n ");
5191 dump_insn_slim (lra_dump_file, curr_insn);
5193 lra_set_insn_deleted (curr_insn);
5198 /* The structure describes info to do an inheritance for the current
5199 insn. We need to collect such info first before doing the
5200 transformations because the transformations change the insn
5201 internal representation. */
5202 struct to_inherit
5204 /* Original regno. */
5205 int regno;
5206 /* Subsequent insns which can inherit original reg value. */
5207 rtx insns;
5210 /* Array containing all info for doing inheritance from the current
5211 insn. */
5212 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5214 /* Number elements in the previous array. */
5215 static int to_inherit_num;
5217 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5218 structure to_inherit. */
5219 static void
5220 add_to_inherit (int regno, rtx insns)
5222 int i;
5224 for (i = 0; i < to_inherit_num; i++)
5225 if (to_inherit[i].regno == regno)
5226 return;
5227 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5228 to_inherit[to_inherit_num].regno = regno;
5229 to_inherit[to_inherit_num++].insns = insns;
5232 /* Return the last non-debug insn in basic block BB, or the block begin
5233 note if none. */
5234 static rtx_insn *
5235 get_last_insertion_point (basic_block bb)
5237 rtx_insn *insn;
5239 FOR_BB_INSNS_REVERSE (bb, insn)
5240 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5241 return insn;
5242 gcc_unreachable ();
5245 /* Set up RES by registers living on edges FROM except the edge (FROM,
5246 TO) or by registers set up in a jump insn in BB FROM. */
5247 static void
5248 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5250 rtx_insn *last;
5251 struct lra_insn_reg *reg;
5252 edge e;
5253 edge_iterator ei;
5255 lra_assert (to != NULL);
5256 bitmap_clear (res);
5257 FOR_EACH_EDGE (e, ei, from->succs)
5258 if (e->dest != to)
5259 bitmap_ior_into (res, df_get_live_in (e->dest));
5260 last = get_last_insertion_point (from);
5261 if (! JUMP_P (last))
5262 return;
5263 curr_id = lra_get_insn_recog_data (last);
5264 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5265 if (reg->type != OP_IN)
5266 bitmap_set_bit (res, reg->regno);
5269 /* Used as a temporary results of some bitmap calculations. */
5270 static bitmap_head temp_bitmap;
5272 /* We split for reloads of small class of hard regs. The following
5273 defines how many hard regs the class should have to be qualified as
5274 small. The code is mostly oriented to x86/x86-64 architecture
5275 where some insns need to use only specific register or pair of
5276 registers and these register can live in RTL explicitly, e.g. for
5277 parameter passing. */
5278 static const int max_small_class_regs_num = 2;
5280 /* Do inheritance/split transformations in EBB starting with HEAD and
5281 finishing on TAIL. We process EBB insns in the reverse order.
5282 Return true if we did any inheritance/split transformation in the
5283 EBB.
5285 We should avoid excessive splitting which results in worse code
5286 because of inaccurate cost calculations for spilling new split
5287 pseudos in such case. To achieve this we do splitting only if
5288 register pressure is high in given basic block and there are reload
5289 pseudos requiring hard registers. We could do more register
5290 pressure calculations at any given program point to avoid necessary
5291 splitting even more but it is to expensive and the current approach
5292 works well enough. */
5293 static bool
5294 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5296 int i, src_regno, dst_regno, nregs;
5297 bool change_p, succ_p, update_reloads_num_p;
5298 rtx_insn *prev_insn, *last_insn;
5299 rtx next_usage_insns, set;
5300 enum reg_class cl;
5301 struct lra_insn_reg *reg;
5302 basic_block last_processed_bb, curr_bb = NULL;
5303 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5304 bitmap to_process;
5305 unsigned int j;
5306 bitmap_iterator bi;
5307 bool head_p, after_p;
5309 change_p = false;
5310 curr_usage_insns_check++;
5311 reloads_num = calls_num = 0;
5312 bitmap_clear (&check_only_regs);
5313 last_processed_bb = NULL;
5314 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5315 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5316 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5317 /* We don't process new insns generated in the loop. */
5318 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5320 prev_insn = PREV_INSN (curr_insn);
5321 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5322 curr_bb = BLOCK_FOR_INSN (curr_insn);
5323 if (last_processed_bb != curr_bb)
5325 /* We are at the end of BB. Add qualified living
5326 pseudos for potential splitting. */
5327 to_process = df_get_live_out (curr_bb);
5328 if (last_processed_bb != NULL)
5330 /* We are somewhere in the middle of EBB. */
5331 get_live_on_other_edges (curr_bb, last_processed_bb,
5332 &temp_bitmap);
5333 to_process = &temp_bitmap;
5335 last_processed_bb = curr_bb;
5336 last_insn = get_last_insertion_point (curr_bb);
5337 after_p = (! JUMP_P (last_insn)
5338 && (! CALL_P (last_insn)
5339 || (find_reg_note (last_insn,
5340 REG_NORETURN, NULL_RTX) == NULL_RTX
5341 && ! SIBLING_CALL_P (last_insn))));
5342 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5343 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5345 if ((int) j >= lra_constraint_new_regno_start)
5346 break;
5347 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5349 if (j < FIRST_PSEUDO_REGISTER)
5350 SET_HARD_REG_BIT (live_hard_regs, j);
5351 else
5352 add_to_hard_reg_set (&live_hard_regs,
5353 PSEUDO_REGNO_MODE (j),
5354 reg_renumber[j]);
5355 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5359 src_regno = dst_regno = -1;
5360 if (NONDEBUG_INSN_P (curr_insn)
5361 && (set = single_set (curr_insn)) != NULL_RTX
5362 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5364 src_regno = REGNO (SET_SRC (set));
5365 dst_regno = REGNO (SET_DEST (set));
5367 update_reloads_num_p = true;
5368 if (src_regno < lra_constraint_new_regno_start
5369 && src_regno >= FIRST_PSEUDO_REGISTER
5370 && reg_renumber[src_regno] < 0
5371 && dst_regno >= lra_constraint_new_regno_start
5372 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5374 /* 'reload_pseudo <- original_pseudo'. */
5375 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5376 reloads_num++;
5377 update_reloads_num_p = false;
5378 succ_p = false;
5379 if (usage_insns[src_regno].check == curr_usage_insns_check
5380 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5381 succ_p = inherit_reload_reg (false, src_regno, cl,
5382 curr_insn, next_usage_insns);
5383 if (succ_p)
5384 change_p = true;
5385 else
5386 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5387 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5388 IOR_HARD_REG_SET (potential_reload_hard_regs,
5389 reg_class_contents[cl]);
5391 else if (src_regno >= lra_constraint_new_regno_start
5392 && dst_regno < lra_constraint_new_regno_start
5393 && dst_regno >= FIRST_PSEUDO_REGISTER
5394 && reg_renumber[dst_regno] < 0
5395 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5396 && usage_insns[dst_regno].check == curr_usage_insns_check
5397 && (next_usage_insns
5398 = usage_insns[dst_regno].insns) != NULL_RTX)
5400 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5401 reloads_num++;
5402 update_reloads_num_p = false;
5403 /* 'original_pseudo <- reload_pseudo'. */
5404 if (! JUMP_P (curr_insn)
5405 && inherit_reload_reg (true, dst_regno, cl,
5406 curr_insn, next_usage_insns))
5407 change_p = true;
5408 /* Invalidate. */
5409 usage_insns[dst_regno].check = 0;
5410 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5411 IOR_HARD_REG_SET (potential_reload_hard_regs,
5412 reg_class_contents[cl]);
5414 else if (INSN_P (curr_insn))
5416 int iter;
5417 int max_uid = get_max_uid ();
5419 curr_id = lra_get_insn_recog_data (curr_insn);
5420 curr_static_id = curr_id->insn_static_data;
5421 to_inherit_num = 0;
5422 /* Process insn definitions. */
5423 for (iter = 0; iter < 2; iter++)
5424 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5425 reg != NULL;
5426 reg = reg->next)
5427 if (reg->type != OP_IN
5428 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5430 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5431 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5432 && usage_insns[dst_regno].check == curr_usage_insns_check
5433 && (next_usage_insns
5434 = usage_insns[dst_regno].insns) != NULL_RTX)
5436 struct lra_insn_reg *r;
5438 for (r = curr_id->regs; r != NULL; r = r->next)
5439 if (r->type != OP_OUT && r->regno == dst_regno)
5440 break;
5441 /* Don't do inheritance if the pseudo is also
5442 used in the insn. */
5443 if (r == NULL)
5444 /* We can not do inheritance right now
5445 because the current insn reg info (chain
5446 regs) can change after that. */
5447 add_to_inherit (dst_regno, next_usage_insns);
5449 /* We can not process one reg twice here because of
5450 usage_insns invalidation. */
5451 if ((dst_regno < FIRST_PSEUDO_REGISTER
5452 || reg_renumber[dst_regno] >= 0)
5453 && ! reg->subreg_p && reg->type != OP_IN)
5455 HARD_REG_SET s;
5457 if (split_if_necessary (dst_regno, reg->biggest_mode,
5458 potential_reload_hard_regs,
5459 false, curr_insn, max_uid))
5460 change_p = true;
5461 CLEAR_HARD_REG_SET (s);
5462 if (dst_regno < FIRST_PSEUDO_REGISTER)
5463 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5464 else
5465 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5466 reg_renumber[dst_regno]);
5467 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5469 /* We should invalidate potential inheritance or
5470 splitting for the current insn usages to the next
5471 usage insns (see code below) as the output pseudo
5472 prevents this. */
5473 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5474 && reg_renumber[dst_regno] < 0)
5475 || (reg->type == OP_OUT && ! reg->subreg_p
5476 && (dst_regno < FIRST_PSEUDO_REGISTER
5477 || reg_renumber[dst_regno] >= 0)))
5479 /* Invalidate and mark definitions. */
5480 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5481 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5482 else
5484 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5485 for (i = 0; i < nregs; i++)
5486 usage_insns[dst_regno + i].check
5487 = -(int) INSN_UID (curr_insn);
5491 if (! JUMP_P (curr_insn))
5492 for (i = 0; i < to_inherit_num; i++)
5493 if (inherit_reload_reg (true, to_inherit[i].regno,
5494 ALL_REGS, curr_insn,
5495 to_inherit[i].insns))
5496 change_p = true;
5497 if (CALL_P (curr_insn))
5499 rtx cheap, pat, dest;
5500 rtx_insn *restore;
5501 int regno, hard_regno;
5503 calls_num++;
5504 if ((cheap = find_reg_note (curr_insn,
5505 REG_RETURNED, NULL_RTX)) != NULL_RTX
5506 && ((cheap = XEXP (cheap, 0)), true)
5507 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5508 && (hard_regno = reg_renumber[regno]) >= 0
5509 /* If there are pending saves/restores, the
5510 optimization is not worth. */
5511 && usage_insns[regno].calls_num == calls_num - 1
5512 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5514 /* Restore the pseudo from the call result as
5515 REG_RETURNED note says that the pseudo value is
5516 in the call result and the pseudo is an argument
5517 of the call. */
5518 pat = PATTERN (curr_insn);
5519 if (GET_CODE (pat) == PARALLEL)
5520 pat = XVECEXP (pat, 0, 0);
5521 dest = SET_DEST (pat);
5522 /* For multiple return values dest is PARALLEL.
5523 Currently we handle only single return value case. */
5524 if (REG_P (dest))
5526 start_sequence ();
5527 emit_move_insn (cheap, copy_rtx (dest));
5528 restore = get_insns ();
5529 end_sequence ();
5530 lra_process_new_insns (curr_insn, NULL, restore,
5531 "Inserting call parameter restore");
5532 /* We don't need to save/restore of the pseudo from
5533 this call. */
5534 usage_insns[regno].calls_num = calls_num;
5535 bitmap_set_bit (&check_only_regs, regno);
5539 to_inherit_num = 0;
5540 /* Process insn usages. */
5541 for (iter = 0; iter < 2; iter++)
5542 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5543 reg != NULL;
5544 reg = reg->next)
5545 if ((reg->type != OP_OUT
5546 || (reg->type == OP_OUT && reg->subreg_p))
5547 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5549 if (src_regno >= FIRST_PSEUDO_REGISTER
5550 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5552 if (usage_insns[src_regno].check == curr_usage_insns_check
5553 && (next_usage_insns
5554 = usage_insns[src_regno].insns) != NULL_RTX
5555 && NONDEBUG_INSN_P (curr_insn))
5556 add_to_inherit (src_regno, next_usage_insns);
5557 else if (usage_insns[src_regno].check
5558 != -(int) INSN_UID (curr_insn))
5559 /* Add usages but only if the reg is not set up
5560 in the same insn. */
5561 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5563 else if (src_regno < FIRST_PSEUDO_REGISTER
5564 || reg_renumber[src_regno] >= 0)
5566 bool before_p;
5567 rtx_insn *use_insn = curr_insn;
5569 before_p = (JUMP_P (curr_insn)
5570 || (CALL_P (curr_insn) && reg->type == OP_IN));
5571 if (NONDEBUG_INSN_P (curr_insn)
5572 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5573 && split_if_necessary (src_regno, reg->biggest_mode,
5574 potential_reload_hard_regs,
5575 before_p, curr_insn, max_uid))
5577 if (reg->subreg_p)
5578 lra_risky_transformations_p = true;
5579 change_p = true;
5580 /* Invalidate. */
5581 usage_insns[src_regno].check = 0;
5582 if (before_p)
5583 use_insn = PREV_INSN (curr_insn);
5585 if (NONDEBUG_INSN_P (curr_insn))
5587 if (src_regno < FIRST_PSEUDO_REGISTER)
5588 add_to_hard_reg_set (&live_hard_regs,
5589 reg->biggest_mode, src_regno);
5590 else
5591 add_to_hard_reg_set (&live_hard_regs,
5592 PSEUDO_REGNO_MODE (src_regno),
5593 reg_renumber[src_regno]);
5595 add_next_usage_insn (src_regno, use_insn, reloads_num);
5598 /* Process call args. */
5599 if (curr_id->arg_hard_regs != NULL)
5600 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5601 if (src_regno < FIRST_PSEUDO_REGISTER)
5603 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5604 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5606 for (i = 0; i < to_inherit_num; i++)
5608 src_regno = to_inherit[i].regno;
5609 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5610 curr_insn, to_inherit[i].insns))
5611 change_p = true;
5612 else
5613 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5616 if (update_reloads_num_p
5617 && NONDEBUG_INSN_P (curr_insn)
5618 && (set = single_set (curr_insn)) != NULL_RTX)
5620 int regno = -1;
5621 if ((REG_P (SET_DEST (set))
5622 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5623 && reg_renumber[regno] < 0
5624 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5625 || (REG_P (SET_SRC (set))
5626 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5627 && reg_renumber[regno] < 0
5628 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5630 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5631 reloads_num++;
5632 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5633 IOR_HARD_REG_SET (potential_reload_hard_regs,
5634 reg_class_contents[cl]);
5637 /* We reached the start of the current basic block. */
5638 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5639 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5641 /* We reached the beginning of the current block -- do
5642 rest of spliting in the current BB. */
5643 to_process = df_get_live_in (curr_bb);
5644 if (BLOCK_FOR_INSN (head) != curr_bb)
5646 /* We are somewhere in the middle of EBB. */
5647 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5648 curr_bb, &temp_bitmap);
5649 to_process = &temp_bitmap;
5651 head_p = true;
5652 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5654 if ((int) j >= lra_constraint_new_regno_start)
5655 break;
5656 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5657 && usage_insns[j].check == curr_usage_insns_check
5658 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5660 if (need_for_split_p (potential_reload_hard_regs, j))
5662 if (lra_dump_file != NULL && head_p)
5664 fprintf (lra_dump_file,
5665 " ----------------------------------\n");
5666 head_p = false;
5668 if (split_reg (false, j, bb_note (curr_bb),
5669 next_usage_insns))
5670 change_p = true;
5672 usage_insns[j].check = 0;
5677 return change_p;
5680 /* This value affects EBB forming. If probability of edge from EBB to
5681 a BB is not greater than the following value, we don't add the BB
5682 to EBB. */
5683 #define EBB_PROBABILITY_CUTOFF \
5684 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
5686 /* Current number of inheritance/split iteration. */
5687 int lra_inheritance_iter;
5689 /* Entry function for inheritance/split pass. */
5690 void
5691 lra_inheritance (void)
5693 int i;
5694 basic_block bb, start_bb;
5695 edge e;
5697 lra_inheritance_iter++;
5698 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5699 return;
5700 timevar_push (TV_LRA_INHERITANCE);
5701 if (lra_dump_file != NULL)
5702 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5703 lra_inheritance_iter);
5704 curr_usage_insns_check = 0;
5705 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5706 for (i = 0; i < lra_constraint_new_regno_start; i++)
5707 usage_insns[i].check = 0;
5708 bitmap_initialize (&check_only_regs, &reg_obstack);
5709 bitmap_initialize (&live_regs, &reg_obstack);
5710 bitmap_initialize (&temp_bitmap, &reg_obstack);
5711 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5712 FOR_EACH_BB_FN (bb, cfun)
5714 start_bb = bb;
5715 if (lra_dump_file != NULL)
5716 fprintf (lra_dump_file, "EBB");
5717 /* Form a EBB starting with BB. */
5718 bitmap_clear (&ebb_global_regs);
5719 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5720 for (;;)
5722 if (lra_dump_file != NULL)
5723 fprintf (lra_dump_file, " %d", bb->index);
5724 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5725 || LABEL_P (BB_HEAD (bb->next_bb)))
5726 break;
5727 e = find_fallthru_edge (bb->succs);
5728 if (! e)
5729 break;
5730 if (e->probability < EBB_PROBABILITY_CUTOFF)
5731 break;
5732 bb = bb->next_bb;
5734 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5735 if (lra_dump_file != NULL)
5736 fprintf (lra_dump_file, "\n");
5737 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5738 /* Remember that the EBB head and tail can change in
5739 inherit_in_ebb. */
5740 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5742 bitmap_clear (&ebb_global_regs);
5743 bitmap_clear (&temp_bitmap);
5744 bitmap_clear (&live_regs);
5745 bitmap_clear (&check_only_regs);
5746 free (usage_insns);
5748 timevar_pop (TV_LRA_INHERITANCE);
5753 /* This page contains code to undo failed inheritance/split
5754 transformations. */
5756 /* Current number of iteration undoing inheritance/split. */
5757 int lra_undo_inheritance_iter;
5759 /* Fix BB live info LIVE after removing pseudos created on pass doing
5760 inheritance/split which are REMOVED_PSEUDOS. */
5761 static void
5762 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5764 unsigned int regno;
5765 bitmap_iterator bi;
5767 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5768 if (bitmap_clear_bit (live, regno))
5769 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5772 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5773 number. */
5774 static int
5775 get_regno (rtx reg)
5777 if (GET_CODE (reg) == SUBREG)
5778 reg = SUBREG_REG (reg);
5779 if (REG_P (reg))
5780 return REGNO (reg);
5781 return -1;
5784 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5785 return true if we did any change. The undo transformations for
5786 inheritance looks like
5787 i <- i2
5788 p <- i => p <- i2
5789 or removing
5790 p <- i, i <- p, and i <- i3
5791 where p is original pseudo from which inheritance pseudo i was
5792 created, i and i3 are removed inheritance pseudos, i2 is another
5793 not removed inheritance pseudo. All split pseudos or other
5794 occurrences of removed inheritance pseudos are changed on the
5795 corresponding original pseudos.
5797 The function also schedules insns changed and created during
5798 inheritance/split pass for processing by the subsequent constraint
5799 pass. */
5800 static bool
5801 remove_inheritance_pseudos (bitmap remove_pseudos)
5803 basic_block bb;
5804 int regno, sregno, prev_sregno, dregno, restore_regno;
5805 rtx set, prev_set;
5806 rtx_insn *prev_insn;
5807 bool change_p, done_p;
5809 change_p = ! bitmap_empty_p (remove_pseudos);
5810 /* We can not finish the function right away if CHANGE_P is true
5811 because we need to marks insns affected by previous
5812 inheritance/split pass for processing by the subsequent
5813 constraint pass. */
5814 FOR_EACH_BB_FN (bb, cfun)
5816 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5817 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5818 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5820 if (! INSN_P (curr_insn))
5821 continue;
5822 done_p = false;
5823 sregno = dregno = -1;
5824 if (change_p && NONDEBUG_INSN_P (curr_insn)
5825 && (set = single_set (curr_insn)) != NULL_RTX)
5827 dregno = get_regno (SET_DEST (set));
5828 sregno = get_regno (SET_SRC (set));
5831 if (sregno >= 0 && dregno >= 0)
5833 if ((bitmap_bit_p (remove_pseudos, sregno)
5834 && (lra_reg_info[sregno].restore_regno == dregno
5835 || (bitmap_bit_p (remove_pseudos, dregno)
5836 && (lra_reg_info[sregno].restore_regno
5837 == lra_reg_info[dregno].restore_regno))))
5838 || (bitmap_bit_p (remove_pseudos, dregno)
5839 && lra_reg_info[dregno].restore_regno == sregno))
5840 /* One of the following cases:
5841 original <- removed inheritance pseudo
5842 removed inherit pseudo <- another removed inherit pseudo
5843 removed inherit pseudo <- original pseudo
5845 removed_split_pseudo <- original_reg
5846 original_reg <- removed_split_pseudo */
5848 if (lra_dump_file != NULL)
5850 fprintf (lra_dump_file, " Removing %s:\n",
5851 bitmap_bit_p (&lra_split_regs, sregno)
5852 || bitmap_bit_p (&lra_split_regs, dregno)
5853 ? "split" : "inheritance");
5854 dump_insn_slim (lra_dump_file, curr_insn);
5856 lra_set_insn_deleted (curr_insn);
5857 done_p = true;
5859 else if (bitmap_bit_p (remove_pseudos, sregno)
5860 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5862 /* Search the following pattern:
5863 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5864 original_pseudo <- inherit_or_split_pseudo1
5865 where the 2nd insn is the current insn and
5866 inherit_or_split_pseudo2 is not removed. If it is found,
5867 change the current insn onto:
5868 original_pseudo <- inherit_or_split_pseudo2. */
5869 for (prev_insn = PREV_INSN (curr_insn);
5870 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5871 prev_insn = PREV_INSN (prev_insn))
5873 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5874 && (prev_set = single_set (prev_insn)) != NULL_RTX
5875 /* There should be no subregs in insn we are
5876 searching because only the original reg might
5877 be in subreg when we changed the mode of
5878 load/store for splitting. */
5879 && REG_P (SET_DEST (prev_set))
5880 && REG_P (SET_SRC (prev_set))
5881 && (int) REGNO (SET_DEST (prev_set)) == sregno
5882 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5883 >= FIRST_PSEUDO_REGISTER)
5884 /* As we consider chain of inheritance or
5885 splitting described in above comment we should
5886 check that sregno and prev_sregno were
5887 inheritance/split pseudos created from the
5888 same original regno. */
5889 && (lra_reg_info[sregno].restore_regno
5890 == lra_reg_info[prev_sregno].restore_regno)
5891 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5893 lra_assert (GET_MODE (SET_SRC (prev_set))
5894 == GET_MODE (regno_reg_rtx[sregno]));
5895 if (GET_CODE (SET_SRC (set)) == SUBREG)
5896 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5897 else
5898 SET_SRC (set) = SET_SRC (prev_set);
5899 /* As we are finishing with processing the insn
5900 here, check the destination too as it might
5901 inheritance pseudo for another pseudo. */
5902 if (bitmap_bit_p (remove_pseudos, dregno)
5903 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
5904 && (restore_regno
5905 = lra_reg_info[dregno].restore_regno) >= 0)
5907 if (GET_CODE (SET_DEST (set)) == SUBREG)
5908 SUBREG_REG (SET_DEST (set))
5909 = regno_reg_rtx[restore_regno];
5910 else
5911 SET_DEST (set) = regno_reg_rtx[restore_regno];
5913 lra_push_insn_and_update_insn_regno_info (curr_insn);
5914 lra_set_used_insn_alternative_by_uid
5915 (INSN_UID (curr_insn), -1);
5916 done_p = true;
5917 if (lra_dump_file != NULL)
5919 fprintf (lra_dump_file, " Change reload insn:\n");
5920 dump_insn_slim (lra_dump_file, curr_insn);
5925 if (! done_p)
5927 struct lra_insn_reg *reg;
5928 bool restored_regs_p = false;
5929 bool kept_regs_p = false;
5931 curr_id = lra_get_insn_recog_data (curr_insn);
5932 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5934 regno = reg->regno;
5935 restore_regno = lra_reg_info[regno].restore_regno;
5936 if (restore_regno >= 0)
5938 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5940 lra_substitute_pseudo_within_insn (
5941 curr_insn, regno, regno_reg_rtx[restore_regno]);
5942 restored_regs_p = true;
5944 else
5945 kept_regs_p = true;
5948 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5950 /* The instruction has changed since the previous
5951 constraints pass. */
5952 lra_push_insn_and_update_insn_regno_info (curr_insn);
5953 lra_set_used_insn_alternative_by_uid
5954 (INSN_UID (curr_insn), -1);
5956 else if (restored_regs_p)
5957 /* The instruction has been restored to the form that
5958 it had during the previous constraints pass. */
5959 lra_update_insn_regno_info (curr_insn);
5960 if (restored_regs_p && lra_dump_file != NULL)
5962 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5963 dump_insn_slim (lra_dump_file, curr_insn);
5968 return change_p;
5971 /* If optional reload pseudos failed to get a hard register or was not
5972 inherited, it is better to remove optional reloads. We do this
5973 transformation after undoing inheritance to figure out necessity to
5974 remove optional reloads easier. Return true if we do any
5975 change. */
5976 static bool
5977 undo_optional_reloads (void)
5979 bool change_p, keep_p;
5980 unsigned int regno, uid;
5981 bitmap_iterator bi, bi2;
5982 rtx_insn *insn;
5983 rtx set, src, dest;
5984 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5986 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5987 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5988 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5990 keep_p = false;
5991 /* Keep optional reloads from previous subpasses. */
5992 if (lra_reg_info[regno].restore_regno < 0
5993 /* If the original pseudo changed its allocation, just
5994 removing the optional pseudo is dangerous as the original
5995 pseudo will have longer live range. */
5996 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
5997 keep_p = true;
5998 else if (reg_renumber[regno] >= 0)
5999 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
6001 insn = lra_insn_recog_data[uid]->insn;
6002 if ((set = single_set (insn)) == NULL_RTX)
6003 continue;
6004 src = SET_SRC (set);
6005 dest = SET_DEST (set);
6006 if (! REG_P (src) || ! REG_P (dest))
6007 continue;
6008 if (REGNO (dest) == regno
6009 /* Ignore insn for optional reloads itself. */
6010 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
6011 /* Check only inheritance on last inheritance pass. */
6012 && (int) REGNO (src) >= new_regno_start
6013 /* Check that the optional reload was inherited. */
6014 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6016 keep_p = true;
6017 break;
6020 if (keep_p)
6022 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6023 if (lra_dump_file != NULL)
6024 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6027 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6028 bitmap_initialize (&insn_bitmap, &reg_obstack);
6029 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6031 if (lra_dump_file != NULL)
6032 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6033 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6034 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6036 insn = lra_insn_recog_data[uid]->insn;
6037 if ((set = single_set (insn)) != NULL_RTX)
6039 src = SET_SRC (set);
6040 dest = SET_DEST (set);
6041 if (REG_P (src) && REG_P (dest)
6042 && ((REGNO (src) == regno
6043 && (lra_reg_info[regno].restore_regno
6044 == (int) REGNO (dest)))
6045 || (REGNO (dest) == regno
6046 && (lra_reg_info[regno].restore_regno
6047 == (int) REGNO (src)))))
6049 if (lra_dump_file != NULL)
6051 fprintf (lra_dump_file, " Deleting move %u\n",
6052 INSN_UID (insn));
6053 dump_insn_slim (lra_dump_file, insn);
6055 lra_set_insn_deleted (insn);
6056 continue;
6058 /* We should not worry about generation memory-memory
6059 moves here as if the corresponding inheritance did
6060 not work (inheritance pseudo did not get a hard reg),
6061 we remove the inheritance pseudo and the optional
6062 reload. */
6064 lra_substitute_pseudo_within_insn (
6065 insn, regno,
6066 regno_reg_rtx[lra_reg_info[regno].restore_regno]);
6067 lra_update_insn_regno_info (insn);
6068 if (lra_dump_file != NULL)
6070 fprintf (lra_dump_file,
6071 " Restoring original insn:\n");
6072 dump_insn_slim (lra_dump_file, insn);
6076 /* Clear restore_regnos. */
6077 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6078 lra_reg_info[regno].restore_regno = -1;
6079 bitmap_clear (&insn_bitmap);
6080 bitmap_clear (&removed_optional_reload_pseudos);
6081 return change_p;
6084 /* Entry function for undoing inheritance/split transformation. Return true
6085 if we did any RTL change in this pass. */
6086 bool
6087 lra_undo_inheritance (void)
6089 unsigned int regno;
6090 int restore_regno, hard_regno;
6091 int n_all_inherit, n_inherit, n_all_split, n_split;
6092 bitmap_head remove_pseudos;
6093 bitmap_iterator bi;
6094 bool change_p;
6096 lra_undo_inheritance_iter++;
6097 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6098 return false;
6099 if (lra_dump_file != NULL)
6100 fprintf (lra_dump_file,
6101 "\n********** Undoing inheritance #%d: **********\n\n",
6102 lra_undo_inheritance_iter);
6103 bitmap_initialize (&remove_pseudos, &reg_obstack);
6104 n_inherit = n_all_inherit = 0;
6105 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6106 if (lra_reg_info[regno].restore_regno >= 0)
6108 n_all_inherit++;
6109 if (reg_renumber[regno] < 0
6110 /* If the original pseudo changed its allocation, just
6111 removing inheritance is dangerous as for changing
6112 allocation we used shorter live-ranges. */
6113 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6114 bitmap_set_bit (&remove_pseudos, regno);
6115 else
6116 n_inherit++;
6118 if (lra_dump_file != NULL && n_all_inherit != 0)
6119 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6120 n_inherit, n_all_inherit,
6121 (double) n_inherit / n_all_inherit * 100);
6122 n_split = n_all_split = 0;
6123 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6124 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6126 n_all_split++;
6127 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6128 ? reg_renumber[restore_regno] : restore_regno);
6129 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6130 bitmap_set_bit (&remove_pseudos, regno);
6131 else
6133 n_split++;
6134 if (lra_dump_file != NULL)
6135 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6136 regno, restore_regno);
6139 if (lra_dump_file != NULL && n_all_split != 0)
6140 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6141 n_split, n_all_split,
6142 (double) n_split / n_all_split * 100);
6143 change_p = remove_inheritance_pseudos (&remove_pseudos);
6144 bitmap_clear (&remove_pseudos);
6145 /* Clear restore_regnos. */
6146 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6147 lra_reg_info[regno].restore_regno = -1;
6148 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6149 lra_reg_info[regno].restore_regno = -1;
6150 change_p = undo_optional_reloads () || change_p;
6151 return change_p;