IPA ICF, part 4/5
[official-gcc.git] / gcc / lra-constraints.c
blob2b17aa39fbcd38bba4807c65a8d7ed9798c58f40
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2014 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 "function.h"
124 #include "expr.h"
125 #include "basic-block.h"
126 #include "except.h"
127 #include "optabs.h"
128 #include "df.h"
129 #include "ira.h"
130 #include "rtl-error.h"
131 #include "lra-int.h"
133 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
134 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
135 reload insns. */
136 static int bb_reload_num;
138 /* The current insn being processed and corresponding its single set
139 (NULL otherwise), its data (basic block, the insn data, the insn
140 static data, and the mode of each operand). */
141 static rtx_insn *curr_insn;
142 static rtx curr_insn_set;
143 static basic_block curr_bb;
144 static lra_insn_recog_data_t curr_id;
145 static struct lra_static_insn_data *curr_static_id;
146 static enum machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
150 /* Start numbers for new registers and insns at the current constraints
151 pass start. */
152 static int new_regno_start;
153 static int new_insn_uid_start;
155 /* If LOC is nonnull, strip any outer subreg from it. */
156 static inline rtx *
157 strip_subreg (rtx *loc)
159 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
162 /* Return hard regno of REGNO or if it is was not assigned to a hard
163 register, use a hard register from its allocno class. */
164 static int
165 get_try_hard_regno (int regno)
167 int hard_regno;
168 enum reg_class rclass;
170 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
171 hard_regno = lra_get_regno_hard_regno (regno);
172 if (hard_regno >= 0)
173 return hard_regno;
174 rclass = lra_get_allocno_class (regno);
175 if (rclass == NO_REGS)
176 return -1;
177 return ira_class_hard_regs[rclass][0];
180 /* Return final hard regno (plus offset) which will be after
181 elimination. We do this for matching constraints because the final
182 hard regno could have a different class. */
183 static int
184 get_final_hard_regno (int hard_regno, int offset)
186 if (hard_regno < 0)
187 return hard_regno;
188 hard_regno = lra_get_elimination_hard_regno (hard_regno);
189 return hard_regno + offset;
192 /* Return hard regno of X after removing subreg and making
193 elimination. If X is not a register or subreg of register, return
194 -1. For pseudo use its assignment. */
195 static int
196 get_hard_regno (rtx x)
198 rtx reg;
199 int offset, hard_regno;
201 reg = x;
202 if (GET_CODE (x) == SUBREG)
203 reg = SUBREG_REG (x);
204 if (! REG_P (reg))
205 return -1;
206 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
207 hard_regno = lra_get_regno_hard_regno (hard_regno);
208 if (hard_regno < 0)
209 return -1;
210 offset = 0;
211 if (GET_CODE (x) == SUBREG)
212 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
213 SUBREG_BYTE (x), GET_MODE (x));
214 return get_final_hard_regno (hard_regno, offset);
217 /* If REGNO is a hard register or has been allocated a hard register,
218 return the class of that register. If REGNO is a reload pseudo
219 created by the current constraints pass, return its allocno class.
220 Return NO_REGS otherwise. */
221 static enum reg_class
222 get_reg_class (int regno)
224 int hard_regno;
226 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
227 hard_regno = lra_get_regno_hard_regno (regno);
228 if (hard_regno >= 0)
230 hard_regno = get_final_hard_regno (hard_regno, 0);
231 return REGNO_REG_CLASS (hard_regno);
233 if (regno >= new_regno_start)
234 return lra_get_allocno_class (regno);
235 return NO_REGS;
238 /* Return true if REG satisfies (or will satisfy) reg class constraint
239 CL. Use elimination first if REG is a hard register. If REG is a
240 reload pseudo created by this constraints pass, assume that it will
241 be allocated a hard register from its allocno class, but allow that
242 class to be narrowed to CL if it is currently a superset of CL.
244 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
245 REGNO (reg), or NO_REGS if no change in its class was needed. */
246 static bool
247 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
249 enum reg_class rclass, common_class;
250 enum machine_mode reg_mode;
251 int class_size, hard_regno, nregs, i, j;
252 int regno = REGNO (reg);
254 if (new_class != NULL)
255 *new_class = NO_REGS;
256 if (regno < FIRST_PSEUDO_REGISTER)
258 rtx final_reg = reg;
259 rtx *final_loc = &final_reg;
261 lra_eliminate_reg_if_possible (final_loc);
262 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
264 reg_mode = GET_MODE (reg);
265 rclass = get_reg_class (regno);
266 if (regno < new_regno_start
267 /* Do not allow the constraints for reload instructions to
268 influence the classes of new pseudos. These reloads are
269 typically moves that have many alternatives, and restricting
270 reload pseudos for one alternative may lead to situations
271 where other reload pseudos are no longer allocatable. */
272 || (INSN_UID (curr_insn) >= new_insn_uid_start
273 && curr_insn_set != NULL
274 && ((OBJECT_P (SET_SRC (curr_insn_set))
275 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
276 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
277 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
278 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
279 /* When we don't know what class will be used finally for reload
280 pseudos, we use ALL_REGS. */
281 return ((regno >= new_regno_start && rclass == ALL_REGS)
282 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
283 && ! hard_reg_set_subset_p (reg_class_contents[cl],
284 lra_no_alloc_regs)));
285 else
287 common_class = ira_reg_class_subset[rclass][cl];
288 if (new_class != NULL)
289 *new_class = common_class;
290 if (hard_reg_set_subset_p (reg_class_contents[common_class],
291 lra_no_alloc_regs))
292 return false;
293 /* Check that there are enough allocatable regs. */
294 class_size = ira_class_hard_regs_num[common_class];
295 for (i = 0; i < class_size; i++)
297 hard_regno = ira_class_hard_regs[common_class][i];
298 nregs = hard_regno_nregs[hard_regno][reg_mode];
299 if (nregs == 1)
300 return true;
301 for (j = 0; j < nregs; j++)
302 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
303 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
304 hard_regno + j))
305 break;
306 if (j >= nregs)
307 return true;
309 return false;
313 /* Return true if REGNO satisfies a memory constraint. */
314 static bool
315 in_mem_p (int regno)
317 return get_reg_class (regno) == NO_REGS;
320 /* Return 1 if ADDR is a valid memory address for mode MODE in address
321 space AS, and check that each pseudo has the proper kind of hard
322 reg. */
323 static int
324 valid_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
325 rtx addr, addr_space_t as)
327 #ifdef GO_IF_LEGITIMATE_ADDRESS
328 lra_assert (ADDR_SPACE_GENERIC_P (as));
329 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
330 return 0;
332 win:
333 return 1;
334 #else
335 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
336 #endif
339 namespace {
340 /* Temporarily eliminates registers in an address (for the lifetime of
341 the object). */
342 class address_eliminator {
343 public:
344 address_eliminator (struct address_info *ad);
345 ~address_eliminator ();
347 private:
348 struct address_info *m_ad;
349 rtx *m_base_loc;
350 rtx m_base_reg;
351 rtx *m_index_loc;
352 rtx m_index_reg;
356 address_eliminator::address_eliminator (struct address_info *ad)
357 : m_ad (ad),
358 m_base_loc (strip_subreg (ad->base_term)),
359 m_base_reg (NULL_RTX),
360 m_index_loc (strip_subreg (ad->index_term)),
361 m_index_reg (NULL_RTX)
363 if (m_base_loc != NULL)
365 m_base_reg = *m_base_loc;
366 lra_eliminate_reg_if_possible (m_base_loc);
367 if (m_ad->base_term2 != NULL)
368 *m_ad->base_term2 = *m_ad->base_term;
370 if (m_index_loc != NULL)
372 m_index_reg = *m_index_loc;
373 lra_eliminate_reg_if_possible (m_index_loc);
377 address_eliminator::~address_eliminator ()
379 if (m_base_loc && *m_base_loc != m_base_reg)
381 *m_base_loc = m_base_reg;
382 if (m_ad->base_term2 != NULL)
383 *m_ad->base_term2 = *m_ad->base_term;
385 if (m_index_loc && *m_index_loc != m_index_reg)
386 *m_index_loc = m_index_reg;
389 /* Return true if the eliminated form of AD is a legitimate target address. */
390 static bool
391 valid_address_p (struct address_info *ad)
393 address_eliminator eliminator (ad);
394 return valid_address_p (ad->mode, *ad->outer, ad->as);
397 /* Return true if the eliminated form of memory reference OP satisfies
398 extra memory constraint CONSTRAINT. */
399 static bool
400 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
402 struct address_info ad;
404 decompose_mem_address (&ad, op);
405 address_eliminator eliminator (&ad);
406 return constraint_satisfied_p (op, constraint);
409 /* Return true if the eliminated form of address AD satisfies extra
410 address constraint CONSTRAINT. */
411 static bool
412 satisfies_address_constraint_p (struct address_info *ad,
413 enum constraint_num constraint)
415 address_eliminator eliminator (ad);
416 return constraint_satisfied_p (*ad->outer, constraint);
419 /* Return true if the eliminated form of address OP satisfies extra
420 address constraint CONSTRAINT. */
421 static bool
422 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
424 struct address_info ad;
426 decompose_lea_address (&ad, &op);
427 return satisfies_address_constraint_p (&ad, constraint);
430 /* Initiate equivalences for LRA. As we keep original equivalences
431 before any elimination, we need to make copies otherwise any change
432 in insns might change the equivalences. */
433 void
434 lra_init_equiv (void)
436 ira_expand_reg_equiv ();
437 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
439 rtx res;
441 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
442 ira_reg_equiv[i].memory = copy_rtx (res);
443 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
444 ira_reg_equiv[i].invariant = copy_rtx (res);
448 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
450 /* Update equivalence for REGNO. We need to this as the equivalence
451 might contain other pseudos which are changed by their
452 equivalences. */
453 static void
454 update_equiv (int regno)
456 rtx x;
458 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
459 ira_reg_equiv[regno].memory
460 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
461 NULL_RTX);
462 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
463 ira_reg_equiv[regno].invariant
464 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
465 NULL_RTX);
468 /* If we have decided to substitute X with another value, return that
469 value, otherwise return X. */
470 static rtx
471 get_equiv (rtx x)
473 int regno;
474 rtx res;
476 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
477 || ! ira_reg_equiv[regno].defined_p
478 || ! ira_reg_equiv[regno].profitable_p
479 || lra_get_regno_hard_regno (regno) >= 0)
480 return x;
481 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
482 return res;
483 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
484 return res;
485 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
486 return res;
487 gcc_unreachable ();
490 /* If we have decided to substitute X with the equivalent value,
491 return that value after elimination for INSN, otherwise return
492 X. */
493 static rtx
494 get_equiv_with_elimination (rtx x, rtx_insn *insn)
496 rtx res = get_equiv (x);
498 if (x == res || CONSTANT_P (res))
499 return res;
500 return lra_eliminate_regs_1 (insn, res, GET_MODE (res), false, false, true);
503 /* Set up curr_operand_mode. */
504 static void
505 init_curr_operand_mode (void)
507 int nop = curr_static_id->n_operands;
508 for (int i = 0; i < nop; i++)
510 enum machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
511 if (mode == VOIDmode)
513 /* The .md mode for address operands is the mode of the
514 addressed value rather than the mode of the address itself. */
515 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
516 mode = Pmode;
517 else
518 mode = curr_static_id->operand[i].mode;
520 curr_operand_mode[i] = mode;
526 /* The page contains code to reuse input reloads. */
528 /* Structure describes input reload of the current insns. */
529 struct input_reload
531 /* Reloaded value. */
532 rtx input;
533 /* Reload pseudo used. */
534 rtx reg;
537 /* The number of elements in the following array. */
538 static int curr_insn_input_reloads_num;
539 /* Array containing info about input reloads. It is used to find the
540 same input reload and reuse the reload pseudo in this case. */
541 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
543 /* Initiate data concerning reuse of input reloads for the current
544 insn. */
545 static void
546 init_curr_insn_input_reloads (void)
548 curr_insn_input_reloads_num = 0;
551 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
552 created input reload pseudo (only if TYPE is not OP_OUT). Don't
553 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
554 wrapped up in SUBREG. The result pseudo is returned through
555 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
556 reused the already created input reload pseudo. Use TITLE to
557 describe new registers for debug purposes. */
558 static bool
559 get_reload_reg (enum op_type type, enum machine_mode mode, rtx original,
560 enum reg_class rclass, bool in_subreg_p,
561 const char *title, rtx *result_reg)
563 int i, regno;
564 enum reg_class new_class;
566 if (type == OP_OUT)
568 *result_reg
569 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
570 return true;
572 /* Prevent reuse value of expression with side effects,
573 e.g. volatile memory. */
574 if (! side_effects_p (original))
575 for (i = 0; i < curr_insn_input_reloads_num; i++)
576 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
577 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
579 rtx reg = curr_insn_input_reloads[i].reg;
580 regno = REGNO (reg);
581 /* If input is equal to original and both are VOIDmode,
582 GET_MODE (reg) might be still different from mode.
583 Ensure we don't return *result_reg with wrong mode. */
584 if (GET_MODE (reg) != mode)
586 if (in_subreg_p)
587 continue;
588 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
589 continue;
590 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
591 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
592 continue;
594 *result_reg = reg;
595 if (lra_dump_file != NULL)
597 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
598 dump_value_slim (lra_dump_file, original, 1);
600 if (new_class != lra_get_allocno_class (regno))
601 lra_change_class (regno, new_class, ", change to", false);
602 if (lra_dump_file != NULL)
603 fprintf (lra_dump_file, "\n");
604 return false;
606 *result_reg = lra_create_new_reg (mode, original, rclass, title);
607 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
608 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
609 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
610 return true;
615 /* The page contains code to extract memory address parts. */
617 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
618 static inline bool
619 ok_for_index_p_nonstrict (rtx reg)
621 unsigned regno = REGNO (reg);
623 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
626 /* A version of regno_ok_for_base_p for use here, when all pseudos
627 should count as OK. Arguments as for regno_ok_for_base_p. */
628 static inline bool
629 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode, addr_space_t as,
630 enum rtx_code outer_code, enum rtx_code index_code)
632 unsigned regno = REGNO (reg);
634 if (regno >= FIRST_PSEUDO_REGISTER)
635 return true;
636 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
641 /* The page contains major code to choose the current insn alternative
642 and generate reloads for it. */
644 /* Return the offset from REGNO of the least significant register
645 in (reg:MODE REGNO).
647 This function is used to tell whether two registers satisfy
648 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
650 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
651 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
653 lra_constraint_offset (int regno, enum machine_mode mode)
655 lra_assert (regno < FIRST_PSEUDO_REGISTER);
656 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
657 && SCALAR_INT_MODE_P (mode))
658 return hard_regno_nregs[regno][mode] - 1;
659 return 0;
662 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
663 if they are the same hard reg, and has special hacks for
664 auto-increment and auto-decrement. This is specifically intended for
665 process_alt_operands to use in determining whether two operands
666 match. X is the operand whose number is the lower of the two.
668 It is supposed that X is the output operand and Y is the input
669 operand. Y_HARD_REGNO is the final hard regno of register Y or
670 register in subreg Y as we know it now. Otherwise, it is a
671 negative value. */
672 static bool
673 operands_match_p (rtx x, rtx y, int y_hard_regno)
675 int i;
676 RTX_CODE code = GET_CODE (x);
677 const char *fmt;
679 if (x == y)
680 return true;
681 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
682 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
684 int j;
686 i = get_hard_regno (x);
687 if (i < 0)
688 goto slow;
690 if ((j = y_hard_regno) < 0)
691 goto slow;
693 i += lra_constraint_offset (i, GET_MODE (x));
694 j += lra_constraint_offset (j, GET_MODE (y));
696 return i == j;
699 /* If two operands must match, because they are really a single
700 operand of an assembler insn, then two post-increments are invalid
701 because the assembler insn would increment only once. On the
702 other hand, a post-increment matches ordinary indexing if the
703 post-increment is the output operand. */
704 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
705 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
707 /* Two pre-increments are invalid because the assembler insn would
708 increment only once. On the other hand, a pre-increment matches
709 ordinary indexing if the pre-increment is the input operand. */
710 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
711 || GET_CODE (y) == PRE_MODIFY)
712 return operands_match_p (x, XEXP (y, 0), -1);
714 slow:
716 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
717 && x == SUBREG_REG (y))
718 return true;
719 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
720 && SUBREG_REG (x) == y)
721 return true;
723 /* Now we have disposed of all the cases in which different rtx
724 codes can match. */
725 if (code != GET_CODE (y))
726 return false;
728 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
729 if (GET_MODE (x) != GET_MODE (y))
730 return false;
732 switch (code)
734 CASE_CONST_UNIQUE:
735 return false;
737 case LABEL_REF:
738 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
739 case SYMBOL_REF:
740 return XSTR (x, 0) == XSTR (y, 0);
742 default:
743 break;
746 /* Compare the elements. If any pair of corresponding elements fail
747 to match, return false for the whole things. */
749 fmt = GET_RTX_FORMAT (code);
750 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
752 int val, j;
753 switch (fmt[i])
755 case 'w':
756 if (XWINT (x, i) != XWINT (y, i))
757 return false;
758 break;
760 case 'i':
761 if (XINT (x, i) != XINT (y, i))
762 return false;
763 break;
765 case 'e':
766 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
767 if (val == 0)
768 return false;
769 break;
771 case '0':
772 break;
774 case 'E':
775 if (XVECLEN (x, i) != XVECLEN (y, i))
776 return false;
777 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
779 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
780 if (val == 0)
781 return false;
783 break;
785 /* It is believed that rtx's at this level will never
786 contain anything but integers and other rtx's, except for
787 within LABEL_REFs and SYMBOL_REFs. */
788 default:
789 gcc_unreachable ();
792 return true;
795 /* True if X is a constant that can be forced into the constant pool.
796 MODE is the mode of the operand, or VOIDmode if not known. */
797 #define CONST_POOL_OK_P(MODE, X) \
798 ((MODE) != VOIDmode \
799 && CONSTANT_P (X) \
800 && GET_CODE (X) != HIGH \
801 && !targetm.cannot_force_const_mem (MODE, X))
803 /* True if C is a non-empty register class that has too few registers
804 to be safely used as a reload target class. */
805 #define SMALL_REGISTER_CLASS_P(C) \
806 (ira_class_hard_regs_num [(C)] == 1 \
807 || (ira_class_hard_regs_num [(C)] >= 1 \
808 && targetm.class_likely_spilled_p (C)))
810 /* If REG is a reload pseudo, try to make its class satisfying CL. */
811 static void
812 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
814 enum reg_class rclass;
816 /* Do not make more accurate class from reloads generated. They are
817 mostly moves with a lot of constraints. Making more accurate
818 class may results in very narrow class and impossibility of find
819 registers for several reloads of one insn. */
820 if (INSN_UID (curr_insn) >= new_insn_uid_start)
821 return;
822 if (GET_CODE (reg) == SUBREG)
823 reg = SUBREG_REG (reg);
824 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
825 return;
826 if (in_class_p (reg, cl, &rclass) && rclass != cl)
827 lra_change_class (REGNO (reg), rclass, " Change to", true);
830 /* Generate reloads for matching OUT and INS (array of input operand
831 numbers with end marker -1) with reg class GOAL_CLASS. Add input
832 and output reloads correspondingly to the lists *BEFORE and *AFTER.
833 OUT might be negative. In this case we generate input reloads for
834 matched input operands INS. */
835 static void
836 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
837 rtx_insn **before, rtx_insn **after)
839 int i, in;
840 rtx new_in_reg, new_out_reg, reg, clobber;
841 enum machine_mode inmode, outmode;
842 rtx in_rtx = *curr_id->operand_loc[ins[0]];
843 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
845 inmode = curr_operand_mode[ins[0]];
846 outmode = out < 0 ? inmode : curr_operand_mode[out];
847 push_to_sequence (*before);
848 if (inmode != outmode)
850 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
852 reg = new_in_reg
853 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
854 goal_class, "");
855 if (SCALAR_INT_MODE_P (inmode))
856 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
857 else
858 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
859 LRA_SUBREG_P (new_out_reg) = 1;
860 /* If the input reg is dying here, we can use the same hard
861 register for REG and IN_RTX. We do it only for original
862 pseudos as reload pseudos can die although original
863 pseudos still live where reload pseudos dies. */
864 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
865 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
866 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
868 else
870 reg = new_out_reg
871 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
872 goal_class, "");
873 if (SCALAR_INT_MODE_P (outmode))
874 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
875 else
876 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
877 /* NEW_IN_REG is non-paradoxical subreg. We don't want
878 NEW_OUT_REG living above. We add clobber clause for
879 this. This is just a temporary clobber. We can remove
880 it at the end of LRA work. */
881 clobber = emit_clobber (new_out_reg);
882 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
883 LRA_SUBREG_P (new_in_reg) = 1;
884 if (GET_CODE (in_rtx) == SUBREG)
886 rtx subreg_reg = SUBREG_REG (in_rtx);
888 /* If SUBREG_REG is dying here and sub-registers IN_RTX
889 and NEW_IN_REG are similar, we can use the same hard
890 register for REG and SUBREG_REG. */
891 if (REG_P (subreg_reg)
892 && (int) REGNO (subreg_reg) < lra_new_regno_start
893 && GET_MODE (subreg_reg) == outmode
894 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
895 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
896 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
900 else
902 /* Pseudos have values -- see comments for lra_reg_info.
903 Different pseudos with the same value do not conflict even if
904 they live in the same place. When we create a pseudo we
905 assign value of original pseudo (if any) from which we
906 created the new pseudo. If we create the pseudo from the
907 input pseudo, the new pseudo will no conflict with the input
908 pseudo which is wrong when the input pseudo lives after the
909 insn and as the new pseudo value is changed by the insn
910 output. Therefore we create the new pseudo from the output.
912 We cannot reuse the current output register because we might
913 have a situation like "a <- a op b", where the constraints
914 force the second input operand ("b") to match the output
915 operand ("a"). "b" must then be copied into a new register
916 so that it doesn't clobber the current value of "a". */
918 new_in_reg = new_out_reg
919 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
920 goal_class, "");
922 /* In operand can be got from transformations before processing insn
923 constraints. One example of such transformations is subreg
924 reloading (see function simplify_operand_subreg). The new
925 pseudos created by the transformations might have inaccurate
926 class (ALL_REGS) and we should make their classes more
927 accurate. */
928 narrow_reload_pseudo_class (in_rtx, goal_class);
929 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
930 *before = get_insns ();
931 end_sequence ();
932 for (i = 0; (in = ins[i]) >= 0; i++)
934 lra_assert
935 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
936 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
937 *curr_id->operand_loc[in] = new_in_reg;
939 lra_update_dups (curr_id, ins);
940 if (out < 0)
941 return;
942 /* See a comment for the input operand above. */
943 narrow_reload_pseudo_class (out_rtx, goal_class);
944 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
946 start_sequence ();
947 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
948 emit_insn (*after);
949 *after = get_insns ();
950 end_sequence ();
952 *curr_id->operand_loc[out] = new_out_reg;
953 lra_update_dup (curr_id, out);
956 /* Return register class which is union of all reg classes in insn
957 constraint alternative string starting with P. */
958 static enum reg_class
959 reg_class_from_constraints (const char *p)
961 int c, len;
962 enum reg_class op_class = NO_REGS;
965 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
967 case '#':
968 case ',':
969 return op_class;
971 case 'g':
972 op_class = reg_class_subunion[op_class][GENERAL_REGS];
973 break;
975 default:
976 enum constraint_num cn = lookup_constraint (p);
977 enum reg_class cl = reg_class_for_constraint (cn);
978 if (cl == NO_REGS)
980 if (insn_extra_address_constraint (cn))
981 op_class
982 = (reg_class_subunion
983 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
984 ADDRESS, SCRATCH)]);
985 break;
988 op_class = reg_class_subunion[op_class][cl];
989 break;
991 while ((p += len), c);
992 return op_class;
995 /* If OP is a register, return the class of the register as per
996 get_reg_class, otherwise return NO_REGS. */
997 static inline enum reg_class
998 get_op_class (rtx op)
1000 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1003 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1004 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1005 SUBREG for VAL to make them equal. */
1006 static rtx_insn *
1007 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1009 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1011 /* Usually size of mem_pseudo is greater than val size but in
1012 rare cases it can be less as it can be defined by target
1013 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1014 if (! MEM_P (val))
1016 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1017 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1019 LRA_SUBREG_P (val) = 1;
1021 else
1023 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1024 LRA_SUBREG_P (mem_pseudo) = 1;
1027 return as_a <rtx_insn *> (to_p
1028 ? gen_move_insn (mem_pseudo, val)
1029 : gen_move_insn (val, mem_pseudo));
1032 /* Process a special case insn (register move), return true if we
1033 don't need to process it anymore. INSN should be a single set
1034 insn. Set up that RTL was changed through CHANGE_P and macro
1035 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1036 SEC_MEM_P. */
1037 static bool
1038 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1040 int sregno, dregno;
1041 rtx dest, src, dreg, sreg, old_sreg, new_reg, scratch_reg;
1042 rtx_insn *before;
1043 enum reg_class dclass, sclass, secondary_class;
1044 enum machine_mode sreg_mode;
1045 secondary_reload_info sri;
1047 lra_assert (curr_insn_set != NULL_RTX);
1048 dreg = dest = SET_DEST (curr_insn_set);
1049 sreg = src = SET_SRC (curr_insn_set);
1050 if (GET_CODE (dest) == SUBREG)
1051 dreg = SUBREG_REG (dest);
1052 if (GET_CODE (src) == SUBREG)
1053 sreg = SUBREG_REG (src);
1054 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1055 return false;
1056 sclass = dclass = NO_REGS;
1057 if (REG_P (dreg))
1058 dclass = get_reg_class (REGNO (dreg));
1059 if (dclass == ALL_REGS)
1060 /* ALL_REGS is used for new pseudos created by transformations
1061 like reload of SUBREG_REG (see function
1062 simplify_operand_subreg). We don't know their class yet. We
1063 should figure out the class from processing the insn
1064 constraints not in this fast path function. Even if ALL_REGS
1065 were a right class for the pseudo, secondary_... hooks usually
1066 are not define for ALL_REGS. */
1067 return false;
1068 sreg_mode = GET_MODE (sreg);
1069 old_sreg = sreg;
1070 if (REG_P (sreg))
1071 sclass = get_reg_class (REGNO (sreg));
1072 if (sclass == ALL_REGS)
1073 /* See comments above. */
1074 return false;
1075 if (sclass == NO_REGS && dclass == NO_REGS)
1076 return false;
1077 #ifdef SECONDARY_MEMORY_NEEDED
1078 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1079 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1080 && ((sclass != NO_REGS && dclass != NO_REGS)
1081 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1082 #endif
1085 *sec_mem_p = true;
1086 return false;
1088 #endif
1089 if (! REG_P (dreg) || ! REG_P (sreg))
1090 return false;
1091 sri.prev_sri = NULL;
1092 sri.icode = CODE_FOR_nothing;
1093 sri.extra_cost = 0;
1094 secondary_class = NO_REGS;
1095 /* Set up hard register for a reload pseudo for hook
1096 secondary_reload because some targets just ignore unassigned
1097 pseudos in the hook. */
1098 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1100 dregno = REGNO (dreg);
1101 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1103 else
1104 dregno = -1;
1105 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1107 sregno = REGNO (sreg);
1108 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1110 else
1111 sregno = -1;
1112 if (sclass != NO_REGS)
1113 secondary_class
1114 = (enum reg_class) targetm.secondary_reload (false, dest,
1115 (reg_class_t) sclass,
1116 GET_MODE (src), &sri);
1117 if (sclass == NO_REGS
1118 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1119 && dclass != NO_REGS))
1121 enum reg_class old_sclass = secondary_class;
1122 secondary_reload_info old_sri = sri;
1124 sri.prev_sri = NULL;
1125 sri.icode = CODE_FOR_nothing;
1126 sri.extra_cost = 0;
1127 secondary_class
1128 = (enum reg_class) targetm.secondary_reload (true, sreg,
1129 (reg_class_t) dclass,
1130 sreg_mode, &sri);
1131 /* Check the target hook consistency. */
1132 lra_assert
1133 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1134 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1135 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1137 if (sregno >= 0)
1138 reg_renumber [sregno] = -1;
1139 if (dregno >= 0)
1140 reg_renumber [dregno] = -1;
1141 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1142 return false;
1143 *change_p = true;
1144 new_reg = NULL_RTX;
1145 if (secondary_class != NO_REGS)
1146 new_reg = lra_create_new_reg_with_unique_value (sreg_mode, NULL_RTX,
1147 secondary_class,
1148 "secondary");
1149 start_sequence ();
1150 if (old_sreg != sreg)
1151 sreg = copy_rtx (sreg);
1152 if (sri.icode == CODE_FOR_nothing)
1153 lra_emit_move (new_reg, sreg);
1154 else
1156 enum reg_class scratch_class;
1158 scratch_class = (reg_class_from_constraints
1159 (insn_data[sri.icode].operand[2].constraint));
1160 scratch_reg = (lra_create_new_reg_with_unique_value
1161 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1162 scratch_class, "scratch"));
1163 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1164 sreg, scratch_reg));
1166 before = get_insns ();
1167 end_sequence ();
1168 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1169 if (new_reg != NULL_RTX)
1171 if (GET_CODE (src) == SUBREG)
1172 SUBREG_REG (src) = new_reg;
1173 else
1174 SET_SRC (curr_insn_set) = new_reg;
1176 else
1178 if (lra_dump_file != NULL)
1180 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1181 dump_insn_slim (lra_dump_file, curr_insn);
1183 lra_set_insn_deleted (curr_insn);
1184 return true;
1186 return false;
1189 /* The following data describe the result of process_alt_operands.
1190 The data are used in curr_insn_transform to generate reloads. */
1192 /* The chosen reg classes which should be used for the corresponding
1193 operands. */
1194 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1195 /* True if the operand should be the same as another operand and that
1196 other operand does not need a reload. */
1197 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1198 /* True if the operand does not need a reload. */
1199 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1200 /* True if the operand can be offsetable memory. */
1201 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1202 /* The number of an operand to which given operand can be matched to. */
1203 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1204 /* The number of elements in the following array. */
1205 static int goal_alt_dont_inherit_ops_num;
1206 /* Numbers of operands whose reload pseudos should not be inherited. */
1207 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1208 /* True if the insn commutative operands should be swapped. */
1209 static bool goal_alt_swapped;
1210 /* The chosen insn alternative. */
1211 static int goal_alt_number;
1213 /* The following five variables are used to choose the best insn
1214 alternative. They reflect final characteristics of the best
1215 alternative. */
1217 /* Number of necessary reloads and overall cost reflecting the
1218 previous value and other unpleasantness of the best alternative. */
1219 static int best_losers, best_overall;
1220 /* Overall number hard registers used for reloads. For example, on
1221 some targets we need 2 general registers to reload DFmode and only
1222 one floating point register. */
1223 static int best_reload_nregs;
1224 /* Overall number reflecting distances of previous reloading the same
1225 value. The distances are counted from the current BB start. It is
1226 used to improve inheritance chances. */
1227 static int best_reload_sum;
1229 /* True if the current insn should have no correspondingly input or
1230 output reloads. */
1231 static bool no_input_reloads_p, no_output_reloads_p;
1233 /* True if we swapped the commutative operands in the current
1234 insn. */
1235 static int curr_swapped;
1237 /* Arrange for address element *LOC to be a register of class CL.
1238 Add any input reloads to list BEFORE. AFTER is nonnull if *LOC is an
1239 automodified value; handle that case by adding the required output
1240 reloads to list AFTER. Return true if the RTL was changed. */
1241 static bool
1242 process_addr_reg (rtx *loc, rtx_insn **before, rtx_insn **after,
1243 enum reg_class cl)
1245 int regno;
1246 enum reg_class rclass, new_class;
1247 rtx reg;
1248 rtx new_reg;
1249 enum machine_mode mode;
1250 bool subreg_p, before_p = false;
1252 subreg_p = GET_CODE (*loc) == SUBREG;
1253 if (subreg_p)
1254 loc = &SUBREG_REG (*loc);
1255 reg = *loc;
1256 mode = GET_MODE (reg);
1257 if (! REG_P (reg))
1259 /* Always reload memory in an address even if the target supports
1260 such addresses. */
1261 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1262 before_p = true;
1264 else
1266 regno = REGNO (reg);
1267 rclass = get_reg_class (regno);
1268 if ((*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1270 if (lra_dump_file != NULL)
1272 fprintf (lra_dump_file,
1273 "Changing pseudo %d in address of insn %u on equiv ",
1274 REGNO (reg), INSN_UID (curr_insn));
1275 dump_value_slim (lra_dump_file, *loc, 1);
1276 fprintf (lra_dump_file, "\n");
1278 *loc = copy_rtx (*loc);
1280 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1282 reg = *loc;
1283 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1284 mode, reg, cl, subreg_p, "address", &new_reg))
1285 before_p = true;
1287 else if (new_class != NO_REGS && rclass != new_class)
1289 lra_change_class (regno, new_class, " Change to", true);
1290 return false;
1292 else
1293 return false;
1295 if (before_p)
1297 push_to_sequence (*before);
1298 lra_emit_move (new_reg, reg);
1299 *before = get_insns ();
1300 end_sequence ();
1302 *loc = new_reg;
1303 if (after != NULL)
1305 start_sequence ();
1306 lra_emit_move (reg, new_reg);
1307 emit_insn (*after);
1308 *after = get_insns ();
1309 end_sequence ();
1311 return true;
1314 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1315 the insn to be inserted before curr insn. AFTER returns the
1316 the insn to be inserted after curr insn. ORIGREG and NEWREG
1317 are the original reg and new reg for reload. */
1318 static void
1319 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1320 rtx newreg)
1322 if (before)
1324 push_to_sequence (*before);
1325 lra_emit_move (newreg, origreg);
1326 *before = get_insns ();
1327 end_sequence ();
1329 if (after)
1331 start_sequence ();
1332 lra_emit_move (origreg, newreg);
1333 emit_insn (*after);
1334 *after = get_insns ();
1335 end_sequence ();
1339 static int valid_address_p (enum machine_mode mode, rtx addr, addr_space_t as);
1341 /* Make reloads for subreg in operand NOP with internal subreg mode
1342 REG_MODE, add new reloads for further processing. Return true if
1343 any reload was generated. */
1344 static bool
1345 simplify_operand_subreg (int nop, enum machine_mode reg_mode)
1347 int hard_regno;
1348 rtx_insn *before, *after;
1349 enum machine_mode mode;
1350 rtx reg, new_reg;
1351 rtx operand = *curr_id->operand_loc[nop];
1352 enum reg_class regclass;
1353 enum op_type type;
1355 before = after = NULL;
1357 if (GET_CODE (operand) != SUBREG)
1358 return false;
1360 mode = GET_MODE (operand);
1361 reg = SUBREG_REG (operand);
1362 type = curr_static_id->operand[nop].type;
1363 /* If we change address for paradoxical subreg of memory, the
1364 address might violate the necessary alignment or the access might
1365 be slow. So take this into consideration. We should not worry
1366 about access beyond allocated memory for paradoxical memory
1367 subregs as we don't substitute such equiv memory (see processing
1368 equivalences in function lra_constraints) and because for spilled
1369 pseudos we allocate stack memory enough for the biggest
1370 corresponding paradoxical subreg. */
1371 if (MEM_P (reg)
1372 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1373 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1375 rtx subst, old = *curr_id->operand_loc[nop];
1377 alter_subreg (curr_id->operand_loc[nop], false);
1378 subst = *curr_id->operand_loc[nop];
1379 lra_assert (MEM_P (subst));
1380 if (! valid_address_p (GET_MODE (reg), XEXP (reg, 0),
1381 MEM_ADDR_SPACE (reg))
1382 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1383 MEM_ADDR_SPACE (subst)))
1384 return true;
1385 /* If the address was valid and became invalid, prefer to reload
1386 the memory. Typical case is when the index scale should
1387 correspond the memory. */
1388 *curr_id->operand_loc[nop] = old;
1390 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1392 alter_subreg (curr_id->operand_loc[nop], false);
1393 return true;
1395 /* Put constant into memory when we have mixed modes. It generates
1396 a better code in most cases as it does not need a secondary
1397 reload memory. It also prevents LRA looping when LRA is using
1398 secondary reload memory again and again. */
1399 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1400 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1402 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1403 alter_subreg (curr_id->operand_loc[nop], false);
1404 return true;
1406 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1407 if there may be a problem accessing OPERAND in the outer
1408 mode. */
1409 if ((REG_P (reg)
1410 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1411 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1412 /* Don't reload paradoxical subregs because we could be looping
1413 having repeatedly final regno out of hard regs range. */
1414 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1415 >= hard_regno_nregs[hard_regno][mode])
1416 && simplify_subreg_regno (hard_regno, GET_MODE (reg),
1417 SUBREG_BYTE (operand), mode) < 0
1418 /* Don't reload subreg for matching reload. It is actually
1419 valid subreg in LRA. */
1420 && ! LRA_SUBREG_P (operand))
1421 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1423 enum reg_class rclass;
1425 if (REG_P (reg))
1426 /* There is a big probability that we will get the same class
1427 for the new pseudo and we will get the same insn which
1428 means infinite looping. So spill the new pseudo. */
1429 rclass = NO_REGS;
1430 else
1431 /* The class will be defined later in curr_insn_transform. */
1432 rclass
1433 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1435 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1436 rclass, TRUE, "subreg reg", &new_reg))
1438 bool insert_before, insert_after;
1439 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1441 insert_before = (type != OP_OUT
1442 || GET_MODE_SIZE (GET_MODE (reg)) > GET_MODE_SIZE (mode));
1443 insert_after = (type != OP_IN);
1444 insert_move_for_subreg (insert_before ? &before : NULL,
1445 insert_after ? &after : NULL,
1446 reg, new_reg);
1448 SUBREG_REG (operand) = new_reg;
1449 lra_process_new_insns (curr_insn, before, after,
1450 "Inserting subreg reload");
1451 return true;
1453 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1454 IRA allocates hardreg to the inner pseudo reg according to its mode
1455 instead of the outermode, so the size of the hardreg may not be enough
1456 to contain the outermode operand, in that case we may need to insert
1457 reload for the reg. For the following two types of paradoxical subreg,
1458 we need to insert reload:
1459 1. If the op_type is OP_IN, and the hardreg could not be paired with
1460 other hardreg to contain the outermode operand
1461 (checked by in_hard_reg_set_p), we need to insert the reload.
1462 2. If the op_type is OP_OUT or OP_INOUT.
1464 Here is a paradoxical subreg example showing how the reload is generated:
1466 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1467 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1469 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1470 here, if reg107 is assigned to hardreg R15, because R15 is the last
1471 hardreg, compiler cannot find another hardreg to pair with R15 to
1472 contain TImode data. So we insert a TImode reload reg180 for it.
1473 After reload is inserted:
1475 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1476 (reg:DI 107 [ __comp ])) -1
1477 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1478 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1480 Two reload hard registers will be allocated to reg180 to save TImode data
1481 in LRA_assign. */
1482 else if (REG_P (reg)
1483 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1484 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1485 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1486 < hard_regno_nregs[hard_regno][mode])
1487 && (regclass = lra_get_allocno_class (REGNO (reg)))
1488 && (type != OP_IN
1489 || !in_hard_reg_set_p (reg_class_contents[regclass],
1490 mode, hard_regno)))
1492 /* The class will be defined later in curr_insn_transform. */
1493 enum reg_class rclass
1494 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1496 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1497 rclass, TRUE, "paradoxical subreg", &new_reg))
1499 rtx subreg;
1500 bool insert_before, insert_after;
1502 PUT_MODE (new_reg, mode);
1503 subreg = simplify_gen_subreg (GET_MODE (reg), new_reg, mode, 0);
1504 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1506 insert_before = (type != OP_OUT);
1507 insert_after = (type != OP_IN);
1508 insert_move_for_subreg (insert_before ? &before : NULL,
1509 insert_after ? &after : NULL,
1510 reg, subreg);
1512 SUBREG_REG (operand) = new_reg;
1513 lra_process_new_insns (curr_insn, before, after,
1514 "Inserting paradoxical subreg reload");
1515 return true;
1517 return false;
1520 /* Return TRUE if X refers for a hard register from SET. */
1521 static bool
1522 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1524 int i, j, x_hard_regno;
1525 enum machine_mode mode;
1526 const char *fmt;
1527 enum rtx_code code;
1529 if (x == NULL_RTX)
1530 return false;
1531 code = GET_CODE (x);
1532 mode = GET_MODE (x);
1533 if (code == SUBREG)
1535 x = SUBREG_REG (x);
1536 code = GET_CODE (x);
1537 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1538 mode = GET_MODE (x);
1541 if (REG_P (x))
1543 x_hard_regno = get_hard_regno (x);
1544 return (x_hard_regno >= 0
1545 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1547 if (MEM_P (x))
1549 struct address_info ad;
1551 decompose_mem_address (&ad, x);
1552 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1553 return true;
1554 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1555 return true;
1557 fmt = GET_RTX_FORMAT (code);
1558 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1560 if (fmt[i] == 'e')
1562 if (uses_hard_regs_p (XEXP (x, i), set))
1563 return true;
1565 else if (fmt[i] == 'E')
1567 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1568 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1569 return true;
1572 return false;
1575 /* Return true if OP is a spilled pseudo. */
1576 static inline bool
1577 spilled_pseudo_p (rtx op)
1579 return (REG_P (op)
1580 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1583 /* Return true if X is a general constant. */
1584 static inline bool
1585 general_constant_p (rtx x)
1587 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1590 static bool
1591 reg_in_class_p (rtx reg, enum reg_class cl)
1593 if (cl == NO_REGS)
1594 return get_reg_class (REGNO (reg)) == NO_REGS;
1595 return in_class_p (reg, cl, NULL);
1598 /* Major function to choose the current insn alternative and what
1599 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1600 negative we should consider only this alternative. Return false if
1601 we can not choose the alternative or find how to reload the
1602 operands. */
1603 static bool
1604 process_alt_operands (int only_alternative)
1606 bool ok_p = false;
1607 int nop, overall, nalt;
1608 int n_alternatives = curr_static_id->n_alternatives;
1609 int n_operands = curr_static_id->n_operands;
1610 /* LOSERS counts the operands that don't fit this alternative and
1611 would require loading. */
1612 int losers;
1613 /* REJECT is a count of how undesirable this alternative says it is
1614 if any reloading is required. If the alternative matches exactly
1615 then REJECT is ignored, but otherwise it gets this much counted
1616 against it in addition to the reloading needed. */
1617 int reject;
1618 /* The number of elements in the following array. */
1619 int early_clobbered_regs_num;
1620 /* Numbers of operands which are early clobber registers. */
1621 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1622 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1623 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1624 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1625 bool curr_alt_win[MAX_RECOG_OPERANDS];
1626 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1627 int curr_alt_matches[MAX_RECOG_OPERANDS];
1628 /* The number of elements in the following array. */
1629 int curr_alt_dont_inherit_ops_num;
1630 /* Numbers of operands whose reload pseudos should not be inherited. */
1631 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1632 rtx op;
1633 /* The register when the operand is a subreg of register, otherwise the
1634 operand itself. */
1635 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1636 /* The register if the operand is a register or subreg of register,
1637 otherwise NULL. */
1638 rtx operand_reg[MAX_RECOG_OPERANDS];
1639 int hard_regno[MAX_RECOG_OPERANDS];
1640 enum machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1641 int reload_nregs, reload_sum;
1642 bool costly_p;
1643 enum reg_class cl;
1645 /* Calculate some data common for all alternatives to speed up the
1646 function. */
1647 for (nop = 0; nop < n_operands; nop++)
1649 rtx reg;
1651 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1652 /* The real hard regno of the operand after the allocation. */
1653 hard_regno[nop] = get_hard_regno (op);
1655 operand_reg[nop] = reg = op;
1656 biggest_mode[nop] = GET_MODE (op);
1657 if (GET_CODE (op) == SUBREG)
1659 operand_reg[nop] = reg = SUBREG_REG (op);
1660 if (GET_MODE_SIZE (biggest_mode[nop])
1661 < GET_MODE_SIZE (GET_MODE (reg)))
1662 biggest_mode[nop] = GET_MODE (reg);
1664 if (! REG_P (reg))
1665 operand_reg[nop] = NULL_RTX;
1666 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1667 || ((int) REGNO (reg)
1668 == lra_get_elimination_hard_regno (REGNO (reg))))
1669 no_subreg_reg_operand[nop] = reg;
1670 else
1671 operand_reg[nop] = no_subreg_reg_operand[nop]
1672 /* Just use natural mode for elimination result. It should
1673 be enough for extra constraints hooks. */
1674 = regno_reg_rtx[hard_regno[nop]];
1677 /* The constraints are made of several alternatives. Each operand's
1678 constraint looks like foo,bar,... with commas separating the
1679 alternatives. The first alternatives for all operands go
1680 together, the second alternatives go together, etc.
1682 First loop over alternatives. */
1683 alternative_mask enabled = curr_id->enabled_alternatives;
1684 if (only_alternative >= 0)
1685 enabled &= ALTERNATIVE_BIT (only_alternative);
1687 for (nalt = 0; nalt < n_alternatives; nalt++)
1689 /* Loop over operands for one constraint alternative. */
1690 if (!TEST_BIT (enabled, nalt))
1691 continue;
1693 overall = losers = reject = reload_nregs = reload_sum = 0;
1694 for (nop = 0; nop < n_operands; nop++)
1696 int inc = (curr_static_id
1697 ->operand_alternative[nalt * n_operands + nop].reject);
1698 if (lra_dump_file != NULL && inc != 0)
1699 fprintf (lra_dump_file,
1700 " Staticly defined alt reject+=%d\n", inc);
1701 reject += inc;
1703 early_clobbered_regs_num = 0;
1705 for (nop = 0; nop < n_operands; nop++)
1707 const char *p;
1708 char *end;
1709 int len, c, m, i, opalt_num, this_alternative_matches;
1710 bool win, did_match, offmemok, early_clobber_p;
1711 /* false => this operand can be reloaded somehow for this
1712 alternative. */
1713 bool badop;
1714 /* true => this operand can be reloaded if the alternative
1715 allows regs. */
1716 bool winreg;
1717 /* True if a constant forced into memory would be OK for
1718 this operand. */
1719 bool constmemok;
1720 enum reg_class this_alternative, this_costly_alternative;
1721 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1722 bool this_alternative_match_win, this_alternative_win;
1723 bool this_alternative_offmemok;
1724 bool scratch_p;
1725 enum machine_mode mode;
1726 enum constraint_num cn;
1728 opalt_num = nalt * n_operands + nop;
1729 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1731 /* Fast track for no constraints at all. */
1732 curr_alt[nop] = NO_REGS;
1733 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1734 curr_alt_win[nop] = true;
1735 curr_alt_match_win[nop] = false;
1736 curr_alt_offmemok[nop] = false;
1737 curr_alt_matches[nop] = -1;
1738 continue;
1741 op = no_subreg_reg_operand[nop];
1742 mode = curr_operand_mode[nop];
1744 win = did_match = winreg = offmemok = constmemok = false;
1745 badop = true;
1747 early_clobber_p = false;
1748 p = curr_static_id->operand_alternative[opalt_num].constraint;
1750 this_costly_alternative = this_alternative = NO_REGS;
1751 /* We update set of possible hard regs besides its class
1752 because reg class might be inaccurate. For example,
1753 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1754 is translated in HI_REGS because classes are merged by
1755 pairs and there is no accurate intermediate class. */
1756 CLEAR_HARD_REG_SET (this_alternative_set);
1757 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1758 this_alternative_win = false;
1759 this_alternative_match_win = false;
1760 this_alternative_offmemok = false;
1761 this_alternative_matches = -1;
1763 /* An empty constraint should be excluded by the fast
1764 track. */
1765 lra_assert (*p != 0 && *p != ',');
1767 /* Scan this alternative's specs for this operand; set WIN
1768 if the operand fits any letter in this alternative.
1769 Otherwise, clear BADOP if this operand could fit some
1770 letter after reloads, or set WINREG if this operand could
1771 fit after reloads provided the constraint allows some
1772 registers. */
1773 costly_p = false;
1776 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1778 case '\0':
1779 len = 0;
1780 break;
1781 case ',':
1782 c = '\0';
1783 break;
1785 case '&':
1786 early_clobber_p = true;
1787 break;
1789 case '#':
1790 /* Ignore rest of this alternative. */
1791 c = '\0';
1792 break;
1794 case '0': case '1': case '2': case '3': case '4':
1795 case '5': case '6': case '7': case '8': case '9':
1797 int m_hregno;
1798 bool match_p;
1800 m = strtoul (p, &end, 10);
1801 p = end;
1802 len = 0;
1803 lra_assert (nop > m);
1805 this_alternative_matches = m;
1806 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1807 /* We are supposed to match a previous operand.
1808 If we do, we win if that one did. If we do
1809 not, count both of the operands as losers.
1810 (This is too conservative, since most of the
1811 time only a single reload insn will be needed
1812 to make the two operands win. As a result,
1813 this alternative may be rejected when it is
1814 actually desirable.) */
1815 match_p = false;
1816 if (operands_match_p (*curr_id->operand_loc[nop],
1817 *curr_id->operand_loc[m], m_hregno))
1819 /* We should reject matching of an early
1820 clobber operand if the matching operand is
1821 not dying in the insn. */
1822 if (! curr_static_id->operand[m].early_clobber
1823 || operand_reg[nop] == NULL_RTX
1824 || (find_regno_note (curr_insn, REG_DEAD,
1825 REGNO (op))
1826 || REGNO (op) == REGNO (operand_reg[m])))
1827 match_p = true;
1829 if (match_p)
1831 /* If we are matching a non-offsettable
1832 address where an offsettable address was
1833 expected, then we must reject this
1834 combination, because we can't reload
1835 it. */
1836 if (curr_alt_offmemok[m]
1837 && MEM_P (*curr_id->operand_loc[m])
1838 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1839 continue;
1841 else
1843 /* Operands don't match. Both operands must
1844 allow a reload register, otherwise we
1845 cannot make them match. */
1846 if (curr_alt[m] == NO_REGS)
1847 break;
1848 /* Retroactively mark the operand we had to
1849 match as a loser, if it wasn't already and
1850 it wasn't matched to a register constraint
1851 (e.g it might be matched by memory). */
1852 if (curr_alt_win[m]
1853 && (operand_reg[m] == NULL_RTX
1854 || hard_regno[m] < 0))
1856 losers++;
1857 reload_nregs
1858 += (ira_reg_class_max_nregs[curr_alt[m]]
1859 [GET_MODE (*curr_id->operand_loc[m])]);
1862 /* Prefer matching earlyclobber alternative as
1863 it results in less hard regs required for
1864 the insn than a non-matching earlyclobber
1865 alternative. */
1866 if (curr_static_id->operand[m].early_clobber)
1868 if (lra_dump_file != NULL)
1869 fprintf
1870 (lra_dump_file,
1871 " %d Matching earlyclobber alt:"
1872 " reject--\n",
1873 nop);
1874 reject--;
1876 /* Otherwise we prefer no matching
1877 alternatives because it gives more freedom
1878 in RA. */
1879 else if (operand_reg[nop] == NULL_RTX
1880 || (find_regno_note (curr_insn, REG_DEAD,
1881 REGNO (operand_reg[nop]))
1882 == NULL_RTX))
1884 if (lra_dump_file != NULL)
1885 fprintf
1886 (lra_dump_file,
1887 " %d Matching alt: reject+=2\n",
1888 nop);
1889 reject += 2;
1892 /* If we have to reload this operand and some
1893 previous operand also had to match the same
1894 thing as this operand, we don't know how to do
1895 that. */
1896 if (!match_p || !curr_alt_win[m])
1898 for (i = 0; i < nop; i++)
1899 if (curr_alt_matches[i] == m)
1900 break;
1901 if (i < nop)
1902 break;
1904 else
1905 did_match = true;
1907 /* This can be fixed with reloads if the operand
1908 we are supposed to match can be fixed with
1909 reloads. */
1910 badop = false;
1911 this_alternative = curr_alt[m];
1912 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1913 winreg = this_alternative != NO_REGS;
1914 break;
1917 case 'g':
1918 if (MEM_P (op)
1919 || general_constant_p (op)
1920 || spilled_pseudo_p (op))
1921 win = true;
1922 cl = GENERAL_REGS;
1923 goto reg;
1925 default:
1926 cn = lookup_constraint (p);
1927 switch (get_constraint_type (cn))
1929 case CT_REGISTER:
1930 cl = reg_class_for_constraint (cn);
1931 if (cl != NO_REGS)
1932 goto reg;
1933 break;
1935 case CT_CONST_INT:
1936 if (CONST_INT_P (op)
1937 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
1938 win = true;
1939 break;
1941 case CT_MEMORY:
1942 if (MEM_P (op)
1943 && satisfies_memory_constraint_p (op, cn))
1944 win = true;
1945 else if (spilled_pseudo_p (op))
1946 win = true;
1948 /* If we didn't already win, we can reload constants
1949 via force_const_mem or put the pseudo value into
1950 memory, or make other memory by reloading the
1951 address like for 'o'. */
1952 if (CONST_POOL_OK_P (mode, op)
1953 || MEM_P (op) || REG_P (op))
1954 badop = false;
1955 constmemok = true;
1956 offmemok = true;
1957 break;
1959 case CT_ADDRESS:
1960 /* If we didn't already win, we can reload the address
1961 into a base register. */
1962 if (satisfies_address_constraint_p (op, cn))
1963 win = true;
1964 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1965 ADDRESS, SCRATCH);
1966 badop = false;
1967 goto reg;
1969 case CT_FIXED_FORM:
1970 if (constraint_satisfied_p (op, cn))
1971 win = true;
1972 break;
1974 break;
1976 reg:
1977 this_alternative = reg_class_subunion[this_alternative][cl];
1978 IOR_HARD_REG_SET (this_alternative_set,
1979 reg_class_contents[cl]);
1980 if (costly_p)
1982 this_costly_alternative
1983 = reg_class_subunion[this_costly_alternative][cl];
1984 IOR_HARD_REG_SET (this_costly_alternative_set,
1985 reg_class_contents[cl]);
1987 if (mode == BLKmode)
1988 break;
1989 winreg = true;
1990 if (REG_P (op))
1992 if (hard_regno[nop] >= 0
1993 && in_hard_reg_set_p (this_alternative_set,
1994 mode, hard_regno[nop]))
1995 win = true;
1996 else if (hard_regno[nop] < 0
1997 && in_class_p (op, this_alternative, NULL))
1998 win = true;
2000 break;
2002 if (c != ' ' && c != '\t')
2003 costly_p = c == '*';
2005 while ((p += len), c);
2007 scratch_p = (operand_reg[nop] != NULL_RTX
2008 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2009 /* Record which operands fit this alternative. */
2010 if (win)
2012 this_alternative_win = true;
2013 if (operand_reg[nop] != NULL_RTX)
2015 if (hard_regno[nop] >= 0)
2017 if (in_hard_reg_set_p (this_costly_alternative_set,
2018 mode, hard_regno[nop]))
2020 if (lra_dump_file != NULL)
2021 fprintf (lra_dump_file,
2022 " %d Costly set: reject++\n",
2023 nop);
2024 reject++;
2027 else
2029 /* Prefer won reg to spilled pseudo under other
2030 equal conditions for possibe inheritance. */
2031 if (! scratch_p)
2033 if (lra_dump_file != NULL)
2034 fprintf
2035 (lra_dump_file,
2036 " %d Non pseudo reload: reject++\n",
2037 nop);
2038 reject++;
2040 if (in_class_p (operand_reg[nop],
2041 this_costly_alternative, NULL))
2043 if (lra_dump_file != NULL)
2044 fprintf
2045 (lra_dump_file,
2046 " %d Non pseudo costly reload:"
2047 " reject++\n",
2048 nop);
2049 reject++;
2052 /* We simulate the behaviour of old reload here.
2053 Although scratches need hard registers and it
2054 might result in spilling other pseudos, no reload
2055 insns are generated for the scratches. So it
2056 might cost something but probably less than old
2057 reload pass believes. */
2058 if (scratch_p)
2060 if (lra_dump_file != NULL)
2061 fprintf (lra_dump_file,
2062 " %d Scratch win: reject+=2\n",
2063 nop);
2064 reject += 2;
2068 else if (did_match)
2069 this_alternative_match_win = true;
2070 else
2072 int const_to_mem = 0;
2073 bool no_regs_p;
2075 /* Never do output reload of stack pointer. It makes
2076 impossible to do elimination when SP is changed in
2077 RTL. */
2078 if (op == stack_pointer_rtx && ! frame_pointer_needed
2079 && curr_static_id->operand[nop].type != OP_IN)
2080 goto fail;
2082 /* If this alternative asks for a specific reg class, see if there
2083 is at least one allocatable register in that class. */
2084 no_regs_p
2085 = (this_alternative == NO_REGS
2086 || (hard_reg_set_subset_p
2087 (reg_class_contents[this_alternative],
2088 lra_no_alloc_regs)));
2090 /* For asms, verify that the class for this alternative is possible
2091 for the mode that is specified. */
2092 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2094 int i;
2095 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2096 if (HARD_REGNO_MODE_OK (i, mode)
2097 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2098 mode, i))
2099 break;
2100 if (i == FIRST_PSEUDO_REGISTER)
2101 winreg = false;
2104 /* If this operand accepts a register, and if the
2105 register class has at least one allocatable register,
2106 then this operand can be reloaded. */
2107 if (winreg && !no_regs_p)
2108 badop = false;
2110 if (badop)
2112 if (lra_dump_file != NULL)
2113 fprintf (lra_dump_file,
2114 " alt=%d: Bad operand -- refuse\n",
2115 nalt);
2116 goto fail;
2119 /* If not assigned pseudo has a class which a subset of
2120 required reg class, it is a less costly alternative
2121 as the pseudo still can get a hard reg of necessary
2122 class. */
2123 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2124 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2125 && ira_class_subset_p[this_alternative][cl])
2127 if (lra_dump_file != NULL)
2128 fprintf
2129 (lra_dump_file,
2130 " %d Super set class reg: reject-=3\n", nop);
2131 reject -= 3;
2134 this_alternative_offmemok = offmemok;
2135 if (this_costly_alternative != NO_REGS)
2137 if (lra_dump_file != NULL)
2138 fprintf (lra_dump_file,
2139 " %d Costly loser: reject++\n", nop);
2140 reject++;
2142 /* If the operand is dying, has a matching constraint,
2143 and satisfies constraints of the matched operand
2144 which failed to satisfy the own constraints, most probably
2145 the reload for this operand will be gone. */
2146 if (this_alternative_matches >= 0
2147 && !curr_alt_win[this_alternative_matches]
2148 && REG_P (op)
2149 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2150 && (hard_regno[nop] >= 0
2151 ? in_hard_reg_set_p (this_alternative_set,
2152 mode, hard_regno[nop])
2153 : in_class_p (op, this_alternative, NULL)))
2155 if (lra_dump_file != NULL)
2156 fprintf
2157 (lra_dump_file,
2158 " %d Dying matched operand reload: reject++\n",
2159 nop);
2160 reject++;
2162 else
2164 /* Strict_low_part requires to reload the register
2165 not the sub-register. In this case we should
2166 check that a final reload hard reg can hold the
2167 value mode. */
2168 if (curr_static_id->operand[nop].strict_low
2169 && REG_P (op)
2170 && hard_regno[nop] < 0
2171 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2172 && ira_class_hard_regs_num[this_alternative] > 0
2173 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2174 [this_alternative][0],
2175 GET_MODE
2176 (*curr_id->operand_loc[nop])))
2178 if (lra_dump_file != NULL)
2179 fprintf
2180 (lra_dump_file,
2181 " alt=%d: Strict low subreg reload -- refuse\n",
2182 nalt);
2183 goto fail;
2185 losers++;
2187 if (operand_reg[nop] != NULL_RTX
2188 /* Output operands and matched input operands are
2189 not inherited. The following conditions do not
2190 exactly describe the previous statement but they
2191 are pretty close. */
2192 && curr_static_id->operand[nop].type != OP_OUT
2193 && (this_alternative_matches < 0
2194 || curr_static_id->operand[nop].type != OP_IN))
2196 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2197 (operand_reg[nop])]
2198 .last_reload);
2200 /* The value of reload_sum has sense only if we
2201 process insns in their order. It happens only on
2202 the first constraints sub-pass when we do most of
2203 reload work. */
2204 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2205 reload_sum += last_reload - bb_reload_num;
2207 /* If this is a constant that is reloaded into the
2208 desired class by copying it to memory first, count
2209 that as another reload. This is consistent with
2210 other code and is required to avoid choosing another
2211 alternative when the constant is moved into memory.
2212 Note that the test here is precisely the same as in
2213 the code below that calls force_const_mem. */
2214 if (CONST_POOL_OK_P (mode, op)
2215 && ((targetm.preferred_reload_class
2216 (op, this_alternative) == NO_REGS)
2217 || no_input_reloads_p))
2219 const_to_mem = 1;
2220 if (! no_regs_p)
2221 losers++;
2224 /* Alternative loses if it requires a type of reload not
2225 permitted for this insn. We can always reload
2226 objects with a REG_UNUSED note. */
2227 if ((curr_static_id->operand[nop].type != OP_IN
2228 && no_output_reloads_p
2229 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2230 || (curr_static_id->operand[nop].type != OP_OUT
2231 && no_input_reloads_p && ! const_to_mem)
2232 || (this_alternative_matches >= 0
2233 && (no_input_reloads_p
2234 || (no_output_reloads_p
2235 && (curr_static_id->operand
2236 [this_alternative_matches].type != OP_IN)
2237 && ! find_reg_note (curr_insn, REG_UNUSED,
2238 no_subreg_reg_operand
2239 [this_alternative_matches])))))
2241 if (lra_dump_file != NULL)
2242 fprintf
2243 (lra_dump_file,
2244 " alt=%d: No input/otput reload -- refuse\n",
2245 nalt);
2246 goto fail;
2249 /* Check strong discouragement of reload of non-constant
2250 into class THIS_ALTERNATIVE. */
2251 if (! CONSTANT_P (op) && ! no_regs_p
2252 && (targetm.preferred_reload_class
2253 (op, this_alternative) == NO_REGS
2254 || (curr_static_id->operand[nop].type == OP_OUT
2255 && (targetm.preferred_output_reload_class
2256 (op, this_alternative) == NO_REGS))))
2258 if (lra_dump_file != NULL)
2259 fprintf (lra_dump_file,
2260 " %d Non-prefered reload: reject+=%d\n",
2261 nop, LRA_MAX_REJECT);
2262 reject += LRA_MAX_REJECT;
2265 if (! (MEM_P (op) && offmemok)
2266 && ! (const_to_mem && constmemok))
2268 /* We prefer to reload pseudos over reloading other
2269 things, since such reloads may be able to be
2270 eliminated later. So bump REJECT in other cases.
2271 Don't do this in the case where we are forcing a
2272 constant into memory and it will then win since
2273 we don't want to have a different alternative
2274 match then. */
2275 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2277 if (lra_dump_file != NULL)
2278 fprintf
2279 (lra_dump_file,
2280 " %d Non-pseudo reload: reject+=2\n",
2281 nop);
2282 reject += 2;
2285 if (! no_regs_p)
2286 reload_nregs
2287 += ira_reg_class_max_nregs[this_alternative][mode];
2289 if (SMALL_REGISTER_CLASS_P (this_alternative))
2291 if (lra_dump_file != NULL)
2292 fprintf
2293 (lra_dump_file,
2294 " %d Small class reload: reject+=%d\n",
2295 nop, LRA_LOSER_COST_FACTOR / 2);
2296 reject += LRA_LOSER_COST_FACTOR / 2;
2300 /* We are trying to spill pseudo into memory. It is
2301 usually more costly than moving to a hard register
2302 although it might takes the same number of
2303 reloads. */
2304 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2306 if (lra_dump_file != NULL)
2307 fprintf
2308 (lra_dump_file,
2309 " %d Spill pseudo into memory: reject+=3\n",
2310 nop);
2311 reject += 3;
2312 if (VECTOR_MODE_P (mode))
2314 /* Spilling vectors into memory is usually more
2315 costly as they contain big values. */
2316 if (lra_dump_file != NULL)
2317 fprintf
2318 (lra_dump_file,
2319 " %d Spill vector pseudo: reject+=2\n",
2320 nop);
2321 reject += 2;
2325 #ifdef SECONDARY_MEMORY_NEEDED
2326 /* If reload requires moving value through secondary
2327 memory, it will need one more insn at least. */
2328 if (this_alternative != NO_REGS
2329 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2330 && ((curr_static_id->operand[nop].type != OP_OUT
2331 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2332 GET_MODE (op)))
2333 || (curr_static_id->operand[nop].type != OP_IN
2334 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2335 GET_MODE (op)))))
2336 losers++;
2337 #endif
2338 /* Input reloads can be inherited more often than output
2339 reloads can be removed, so penalize output
2340 reloads. */
2341 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2343 if (lra_dump_file != NULL)
2344 fprintf
2345 (lra_dump_file,
2346 " %d Non input pseudo reload: reject++\n",
2347 nop);
2348 reject++;
2352 if (early_clobber_p && ! scratch_p)
2354 if (lra_dump_file != NULL)
2355 fprintf (lra_dump_file,
2356 " %d Early clobber: reject++\n", nop);
2357 reject++;
2359 /* ??? We check early clobbers after processing all operands
2360 (see loop below) and there we update the costs more.
2361 Should we update the cost (may be approximately) here
2362 because of early clobber register reloads or it is a rare
2363 or non-important thing to be worth to do it. */
2364 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2365 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2367 if (lra_dump_file != NULL)
2368 fprintf (lra_dump_file,
2369 " alt=%d,overall=%d,losers=%d -- refuse\n",
2370 nalt, overall, losers);
2371 goto fail;
2374 curr_alt[nop] = this_alternative;
2375 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2376 curr_alt_win[nop] = this_alternative_win;
2377 curr_alt_match_win[nop] = this_alternative_match_win;
2378 curr_alt_offmemok[nop] = this_alternative_offmemok;
2379 curr_alt_matches[nop] = this_alternative_matches;
2381 if (this_alternative_matches >= 0
2382 && !did_match && !this_alternative_win)
2383 curr_alt_win[this_alternative_matches] = false;
2385 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2386 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2388 if (curr_insn_set != NULL_RTX && n_operands == 2
2389 /* Prevent processing non-move insns. */
2390 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2391 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2392 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2393 && REG_P (no_subreg_reg_operand[0])
2394 && REG_P (no_subreg_reg_operand[1])
2395 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2396 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2397 || (! curr_alt_win[0] && curr_alt_win[1]
2398 && REG_P (no_subreg_reg_operand[1])
2399 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2400 || (curr_alt_win[0] && ! curr_alt_win[1]
2401 && REG_P (no_subreg_reg_operand[0])
2402 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2403 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2404 no_subreg_reg_operand[1])
2405 || (targetm.preferred_reload_class
2406 (no_subreg_reg_operand[1],
2407 (enum reg_class) curr_alt[1]) != NO_REGS))
2408 /* If it is a result of recent elimination in move
2409 insn we can transform it into an add still by
2410 using this alternative. */
2411 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2413 /* We have a move insn and a new reload insn will be similar
2414 to the current insn. We should avoid such situation as it
2415 results in LRA cycling. */
2416 overall += LRA_MAX_REJECT;
2418 ok_p = true;
2419 curr_alt_dont_inherit_ops_num = 0;
2420 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2422 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2423 HARD_REG_SET temp_set;
2425 i = early_clobbered_nops[nop];
2426 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2427 || hard_regno[i] < 0)
2428 continue;
2429 lra_assert (operand_reg[i] != NULL_RTX);
2430 clobbered_hard_regno = hard_regno[i];
2431 CLEAR_HARD_REG_SET (temp_set);
2432 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2433 first_conflict_j = last_conflict_j = -1;
2434 for (j = 0; j < n_operands; j++)
2435 if (j == i
2436 /* We don't want process insides of match_operator and
2437 match_parallel because otherwise we would process
2438 their operands once again generating a wrong
2439 code. */
2440 || curr_static_id->operand[j].is_operator)
2441 continue;
2442 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2443 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2444 continue;
2445 /* If we don't reload j-th operand, check conflicts. */
2446 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2447 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2449 if (first_conflict_j < 0)
2450 first_conflict_j = j;
2451 last_conflict_j = j;
2453 if (last_conflict_j < 0)
2454 continue;
2455 /* If earlyclobber operand conflicts with another
2456 non-matching operand which is actually the same register
2457 as the earlyclobber operand, it is better to reload the
2458 another operand as an operand matching the earlyclobber
2459 operand can be also the same. */
2460 if (first_conflict_j == last_conflict_j
2461 && operand_reg[last_conflict_j]
2462 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2463 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2465 curr_alt_win[last_conflict_j] = false;
2466 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2467 = last_conflict_j;
2468 losers++;
2469 /* Early clobber was already reflected in REJECT. */
2470 lra_assert (reject > 0);
2471 if (lra_dump_file != NULL)
2472 fprintf
2473 (lra_dump_file,
2474 " %d Conflict early clobber reload: reject--\n",
2476 reject--;
2477 overall += LRA_LOSER_COST_FACTOR - 1;
2479 else
2481 /* We need to reload early clobbered register and the
2482 matched registers. */
2483 for (j = 0; j < n_operands; j++)
2484 if (curr_alt_matches[j] == i)
2486 curr_alt_match_win[j] = false;
2487 losers++;
2488 overall += LRA_LOSER_COST_FACTOR;
2490 if (! curr_alt_match_win[i])
2491 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2492 else
2494 /* Remember pseudos used for match reloads are never
2495 inherited. */
2496 lra_assert (curr_alt_matches[i] >= 0);
2497 curr_alt_win[curr_alt_matches[i]] = false;
2499 curr_alt_win[i] = curr_alt_match_win[i] = false;
2500 losers++;
2501 /* Early clobber was already reflected in REJECT. */
2502 lra_assert (reject > 0);
2503 if (lra_dump_file != NULL)
2504 fprintf
2505 (lra_dump_file,
2506 " %d Matched conflict early clobber reloads:"
2507 "reject--\n",
2509 reject--;
2510 overall += LRA_LOSER_COST_FACTOR - 1;
2513 if (lra_dump_file != NULL)
2514 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2515 nalt, overall, losers, reload_nregs);
2517 /* If this alternative can be made to work by reloading, and it
2518 needs less reloading than the others checked so far, record
2519 it as the chosen goal for reloading. */
2520 if ((best_losers != 0 && losers == 0)
2521 || (((best_losers == 0 && losers == 0)
2522 || (best_losers != 0 && losers != 0))
2523 && (best_overall > overall
2524 || (best_overall == overall
2525 /* If the cost of the reloads is the same,
2526 prefer alternative which requires minimal
2527 number of reload regs. */
2528 && (reload_nregs < best_reload_nregs
2529 || (reload_nregs == best_reload_nregs
2530 && (best_reload_sum < reload_sum
2531 || (best_reload_sum == reload_sum
2532 && nalt < goal_alt_number))))))))
2534 for (nop = 0; nop < n_operands; nop++)
2536 goal_alt_win[nop] = curr_alt_win[nop];
2537 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2538 goal_alt_matches[nop] = curr_alt_matches[nop];
2539 goal_alt[nop] = curr_alt[nop];
2540 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2542 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2543 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2544 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2545 goal_alt_swapped = curr_swapped;
2546 best_overall = overall;
2547 best_losers = losers;
2548 best_reload_nregs = reload_nregs;
2549 best_reload_sum = reload_sum;
2550 goal_alt_number = nalt;
2552 if (losers == 0)
2553 /* Everything is satisfied. Do not process alternatives
2554 anymore. */
2555 break;
2556 fail:
2559 return ok_p;
2562 /* Make reload base reg from address AD. */
2563 static rtx
2564 base_to_reg (struct address_info *ad)
2566 enum reg_class cl;
2567 int code = -1;
2568 rtx new_inner = NULL_RTX;
2569 rtx new_reg = NULL_RTX;
2570 rtx_insn *insn;
2571 rtx_insn *last_insn = get_last_insn();
2573 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2574 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2575 get_index_code (ad));
2576 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2577 cl, "base");
2578 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2579 ad->disp_term == NULL
2580 ? gen_int_mode (0, ad->mode)
2581 : *ad->disp_term);
2582 if (!valid_address_p (ad->mode, new_inner, ad->as))
2583 return NULL_RTX;
2584 insn = emit_insn (gen_rtx_SET (ad->mode, new_reg, *ad->base_term));
2585 code = recog_memoized (insn);
2586 if (code < 0)
2588 delete_insns_since (last_insn);
2589 return NULL_RTX;
2592 return new_inner;
2595 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2596 static rtx
2597 base_plus_disp_to_reg (struct address_info *ad)
2599 enum reg_class cl;
2600 rtx new_reg;
2602 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2603 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2604 get_index_code (ad));
2605 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2606 cl, "base + disp");
2607 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2608 return new_reg;
2611 /* Make reload of index part of address AD. Return the new
2612 pseudo. */
2613 static rtx
2614 index_part_to_reg (struct address_info *ad)
2616 rtx new_reg;
2618 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2619 INDEX_REG_CLASS, "index term");
2620 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2621 GEN_INT (get_index_scale (ad)), new_reg, 1);
2622 return new_reg;
2625 /* Return true if we can add a displacement to address AD, even if that
2626 makes the address invalid. The fix-up code requires any new address
2627 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2628 static bool
2629 can_add_disp_p (struct address_info *ad)
2631 return (!ad->autoinc_p
2632 && ad->segment == NULL
2633 && ad->base == ad->base_term
2634 && ad->disp == ad->disp_term);
2637 /* Make equiv substitution in address AD. Return true if a substitution
2638 was made. */
2639 static bool
2640 equiv_address_substitution (struct address_info *ad)
2642 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2643 HOST_WIDE_INT disp, scale;
2644 bool change_p;
2646 base_term = strip_subreg (ad->base_term);
2647 if (base_term == NULL)
2648 base_reg = new_base_reg = NULL_RTX;
2649 else
2651 base_reg = *base_term;
2652 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2654 index_term = strip_subreg (ad->index_term);
2655 if (index_term == NULL)
2656 index_reg = new_index_reg = NULL_RTX;
2657 else
2659 index_reg = *index_term;
2660 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2662 if (base_reg == new_base_reg && index_reg == new_index_reg)
2663 return false;
2664 disp = 0;
2665 change_p = false;
2666 if (lra_dump_file != NULL)
2668 fprintf (lra_dump_file, "Changing address in insn %d ",
2669 INSN_UID (curr_insn));
2670 dump_value_slim (lra_dump_file, *ad->outer, 1);
2672 if (base_reg != new_base_reg)
2674 if (REG_P (new_base_reg))
2676 *base_term = new_base_reg;
2677 change_p = true;
2679 else if (GET_CODE (new_base_reg) == PLUS
2680 && REG_P (XEXP (new_base_reg, 0))
2681 && CONST_INT_P (XEXP (new_base_reg, 1))
2682 && can_add_disp_p (ad))
2684 disp += INTVAL (XEXP (new_base_reg, 1));
2685 *base_term = XEXP (new_base_reg, 0);
2686 change_p = true;
2688 if (ad->base_term2 != NULL)
2689 *ad->base_term2 = *ad->base_term;
2691 if (index_reg != new_index_reg)
2693 if (REG_P (new_index_reg))
2695 *index_term = new_index_reg;
2696 change_p = true;
2698 else if (GET_CODE (new_index_reg) == PLUS
2699 && REG_P (XEXP (new_index_reg, 0))
2700 && CONST_INT_P (XEXP (new_index_reg, 1))
2701 && can_add_disp_p (ad)
2702 && (scale = get_index_scale (ad)))
2704 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2705 *index_term = XEXP (new_index_reg, 0);
2706 change_p = true;
2709 if (disp != 0)
2711 if (ad->disp != NULL)
2712 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2713 else
2715 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2716 update_address (ad);
2718 change_p = true;
2720 if (lra_dump_file != NULL)
2722 if (! change_p)
2723 fprintf (lra_dump_file, " -- no change\n");
2724 else
2726 fprintf (lra_dump_file, " on equiv ");
2727 dump_value_slim (lra_dump_file, *ad->outer, 1);
2728 fprintf (lra_dump_file, "\n");
2731 return change_p;
2734 /* Major function to make reloads for an address in operand NOP.
2735 The supported cases are:
2737 1) an address that existed before LRA started, at which point it
2738 must have been valid. These addresses are subject to elimination
2739 and may have become invalid due to the elimination offset being out
2740 of range.
2742 2) an address created by forcing a constant to memory
2743 (force_const_to_mem). The initial form of these addresses might
2744 not be valid, and it is this function's job to make them valid.
2746 3) a frame address formed from a register and a (possibly zero)
2747 constant offset. As above, these addresses might not be valid and
2748 this function must make them so.
2750 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2751 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2752 address. Return true for any RTL change.
2754 The function is a helper function which does not produce all
2755 transformations which can be necessary. It does just basic steps.
2756 To do all necessary transformations use function
2757 process_address. */
2758 static bool
2759 process_address_1 (int nop, rtx_insn **before, rtx_insn **after)
2761 struct address_info ad;
2762 rtx new_reg;
2763 rtx op = *curr_id->operand_loc[nop];
2764 const char *constraint = curr_static_id->operand[nop].constraint;
2765 enum constraint_num cn = lookup_constraint (constraint);
2766 bool change_p;
2768 if (insn_extra_address_constraint (cn))
2769 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2770 else if (MEM_P (op))
2771 decompose_mem_address (&ad, op);
2772 else if (GET_CODE (op) == SUBREG
2773 && MEM_P (SUBREG_REG (op)))
2774 decompose_mem_address (&ad, SUBREG_REG (op));
2775 else
2776 return false;
2777 change_p = equiv_address_substitution (&ad);
2778 if (ad.base_term != NULL
2779 && (process_addr_reg
2780 (ad.base_term, before,
2781 (ad.autoinc_p
2782 && !(REG_P (*ad.base_term)
2783 && find_regno_note (curr_insn, REG_DEAD,
2784 REGNO (*ad.base_term)) != NULL_RTX)
2785 ? after : NULL),
2786 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2787 get_index_code (&ad)))))
2789 change_p = true;
2790 if (ad.base_term2 != NULL)
2791 *ad.base_term2 = *ad.base_term;
2793 if (ad.index_term != NULL
2794 && process_addr_reg (ad.index_term, before, NULL, INDEX_REG_CLASS))
2795 change_p = true;
2797 /* Target hooks sometimes don't treat extra-constraint addresses as
2798 legitimate address_operands, so handle them specially. */
2799 if (insn_extra_address_constraint (cn)
2800 && satisfies_address_constraint_p (&ad, cn))
2801 return change_p;
2803 /* There are three cases where the shape of *AD.INNER may now be invalid:
2805 1) the original address was valid, but either elimination or
2806 equiv_address_substitution was applied and that made
2807 the address invalid.
2809 2) the address is an invalid symbolic address created by
2810 force_const_to_mem.
2812 3) the address is a frame address with an invalid offset.
2814 4) the address is a frame address with an invalid base.
2816 All these cases involve a non-autoinc address, so there is no
2817 point revalidating other types. */
2818 if (ad.autoinc_p || valid_address_p (&ad))
2819 return change_p;
2821 /* Any index existed before LRA started, so we can assume that the
2822 presence and shape of the index is valid. */
2823 push_to_sequence (*before);
2824 lra_assert (ad.disp == ad.disp_term);
2825 if (ad.base == NULL)
2827 if (ad.index == NULL)
2829 int code = -1;
2830 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2831 SCRATCH, SCRATCH);
2832 rtx addr = *ad.inner;
2834 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2835 #ifdef HAVE_lo_sum
2837 rtx_insn *insn;
2838 rtx_insn *last = get_last_insn ();
2840 /* addr => lo_sum (new_base, addr), case (2) above. */
2841 insn = emit_insn (gen_rtx_SET
2842 (VOIDmode, new_reg,
2843 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2844 code = recog_memoized (insn);
2845 if (code >= 0)
2847 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2848 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2850 /* Try to put lo_sum into register. */
2851 insn = emit_insn (gen_rtx_SET
2852 (VOIDmode, new_reg,
2853 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2854 code = recog_memoized (insn);
2855 if (code >= 0)
2857 *ad.inner = new_reg;
2858 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2860 *ad.inner = addr;
2861 code = -1;
2867 if (code < 0)
2868 delete_insns_since (last);
2870 #endif
2871 if (code < 0)
2873 /* addr => new_base, case (2) above. */
2874 lra_emit_move (new_reg, addr);
2875 *ad.inner = new_reg;
2878 else
2880 /* index * scale + disp => new base + index * scale,
2881 case (1) above. */
2882 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
2883 GET_CODE (*ad.index));
2885 lra_assert (INDEX_REG_CLASS != NO_REGS);
2886 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2887 lra_emit_move (new_reg, *ad.disp);
2888 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2889 new_reg, *ad.index);
2892 else if (ad.index == NULL)
2894 int regno;
2895 enum reg_class cl;
2896 rtx set;
2897 rtx_insn *insns, *last_insn;
2898 /* Try to reload base into register only if the base is invalid
2899 for the address but with valid offset, case (4) above. */
2900 start_sequence ();
2901 new_reg = base_to_reg (&ad);
2903 /* base + disp => new base, cases (1) and (3) above. */
2904 /* Another option would be to reload the displacement into an
2905 index register. However, postreload has code to optimize
2906 address reloads that have the same base and different
2907 displacements, so reloading into an index register would
2908 not necessarily be a win. */
2909 if (new_reg == NULL_RTX)
2910 new_reg = base_plus_disp_to_reg (&ad);
2911 insns = get_insns ();
2912 last_insn = get_last_insn ();
2913 /* If we generated at least two insns, try last insn source as
2914 an address. If we succeed, we generate one less insn. */
2915 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
2916 && GET_CODE (SET_SRC (set)) == PLUS
2917 && REG_P (XEXP (SET_SRC (set), 0))
2918 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
2920 *ad.inner = SET_SRC (set);
2921 if (valid_address_p (ad.mode, *ad.outer, ad.as))
2923 *ad.base_term = XEXP (SET_SRC (set), 0);
2924 *ad.disp_term = XEXP (SET_SRC (set), 1);
2925 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2926 get_index_code (&ad));
2927 regno = REGNO (*ad.base_term);
2928 if (regno >= FIRST_PSEUDO_REGISTER
2929 && cl != lra_get_allocno_class (regno))
2930 lra_change_class (regno, cl, " Change to", true);
2931 new_reg = SET_SRC (set);
2932 delete_insns_since (PREV_INSN (last_insn));
2935 end_sequence ();
2936 emit_insn (insns);
2937 *ad.inner = new_reg;
2939 else if (ad.disp_term != NULL)
2941 /* base + scale * index + disp => new base + scale * index,
2942 case (1) above. */
2943 new_reg = base_plus_disp_to_reg (&ad);
2944 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2945 new_reg, *ad.index);
2947 else if (get_index_scale (&ad) == 1)
2949 /* The last transformation to one reg will be made in
2950 curr_insn_transform function. */
2951 end_sequence ();
2952 return false;
2954 else
2956 /* base + scale * index => base + new_reg,
2957 case (1) above.
2958 Index part of address may become invalid. For example, we
2959 changed pseudo on the equivalent memory and a subreg of the
2960 pseudo onto the memory of different mode for which the scale is
2961 prohibitted. */
2962 new_reg = index_part_to_reg (&ad);
2963 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2964 *ad.base_term, new_reg);
2966 *before = get_insns ();
2967 end_sequence ();
2968 return true;
2971 /* Do address reloads until it is necessary. Use process_address_1 as
2972 a helper function. Return true for any RTL changes. */
2973 static bool
2974 process_address (int nop, rtx_insn **before, rtx_insn **after)
2976 bool res = false;
2978 while (process_address_1 (nop, before, after))
2979 res = true;
2980 return res;
2983 /* Emit insns to reload VALUE into a new register. VALUE is an
2984 auto-increment or auto-decrement RTX whose operand is a register or
2985 memory location; so reloading involves incrementing that location.
2986 IN is either identical to VALUE, or some cheaper place to reload
2987 value being incremented/decremented from.
2989 INC_AMOUNT is the number to increment or decrement by (always
2990 positive and ignored for POST_MODIFY/PRE_MODIFY).
2992 Return pseudo containing the result. */
2993 static rtx
2994 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
2996 /* REG or MEM to be copied and incremented. */
2997 rtx incloc = XEXP (value, 0);
2998 /* Nonzero if increment after copying. */
2999 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3000 || GET_CODE (value) == POST_MODIFY);
3001 rtx_insn *last;
3002 rtx inc;
3003 rtx_insn *add_insn;
3004 int code;
3005 rtx real_in = in == value ? incloc : in;
3006 rtx result;
3007 bool plus_p = true;
3009 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3011 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3012 || GET_CODE (XEXP (value, 1)) == MINUS);
3013 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3014 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3015 inc = XEXP (XEXP (value, 1), 1);
3017 else
3019 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3020 inc_amount = -inc_amount;
3022 inc = GEN_INT (inc_amount);
3025 if (! post && REG_P (incloc))
3026 result = incloc;
3027 else
3028 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3029 "INC/DEC result");
3031 if (real_in != result)
3033 /* First copy the location to the result register. */
3034 lra_assert (REG_P (result));
3035 emit_insn (gen_move_insn (result, real_in));
3038 /* We suppose that there are insns to add/sub with the constant
3039 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3040 old reload worked with this assumption. If the assumption
3041 becomes wrong, we should use approach in function
3042 base_plus_disp_to_reg. */
3043 if (in == value)
3045 /* See if we can directly increment INCLOC. */
3046 last = get_last_insn ();
3047 add_insn = emit_insn (plus_p
3048 ? gen_add2_insn (incloc, inc)
3049 : gen_sub2_insn (incloc, inc));
3051 code = recog_memoized (add_insn);
3052 if (code >= 0)
3054 if (! post && result != incloc)
3055 emit_insn (gen_move_insn (result, incloc));
3056 return result;
3058 delete_insns_since (last);
3061 /* If couldn't do the increment directly, must increment in RESULT.
3062 The way we do this depends on whether this is pre- or
3063 post-increment. For pre-increment, copy INCLOC to the reload
3064 register, increment it there, then save back. */
3065 if (! post)
3067 if (real_in != result)
3068 emit_insn (gen_move_insn (result, real_in));
3069 if (plus_p)
3070 emit_insn (gen_add2_insn (result, inc));
3071 else
3072 emit_insn (gen_sub2_insn (result, inc));
3073 if (result != incloc)
3074 emit_insn (gen_move_insn (incloc, result));
3076 else
3078 /* Post-increment.
3080 Because this might be a jump insn or a compare, and because
3081 RESULT may not be available after the insn in an input
3082 reload, we must do the incrementing before the insn being
3083 reloaded for.
3085 We have already copied IN to RESULT. Increment the copy in
3086 RESULT, save that back, then decrement RESULT so it has
3087 the original value. */
3088 if (plus_p)
3089 emit_insn (gen_add2_insn (result, inc));
3090 else
3091 emit_insn (gen_sub2_insn (result, inc));
3092 emit_insn (gen_move_insn (incloc, result));
3093 /* Restore non-modified value for the result. We prefer this
3094 way because it does not require an additional hard
3095 register. */
3096 if (plus_p)
3098 if (CONST_INT_P (inc))
3099 emit_insn (gen_add2_insn (result,
3100 gen_int_mode (-INTVAL (inc),
3101 GET_MODE (result))));
3102 else
3103 emit_insn (gen_sub2_insn (result, inc));
3105 else
3106 emit_insn (gen_add2_insn (result, inc));
3108 return result;
3111 /* Return true if the current move insn does not need processing as we
3112 already know that it satisfies its constraints. */
3113 static bool
3114 simple_move_p (void)
3116 rtx dest, src;
3117 enum reg_class dclass, sclass;
3119 lra_assert (curr_insn_set != NULL_RTX);
3120 dest = SET_DEST (curr_insn_set);
3121 src = SET_SRC (curr_insn_set);
3122 return ((dclass = get_op_class (dest)) != NO_REGS
3123 && (sclass = get_op_class (src)) != NO_REGS
3124 /* The backend guarantees that register moves of cost 2
3125 never need reloads. */
3126 && targetm.register_move_cost (GET_MODE (src), dclass, sclass) == 2);
3129 /* Swap operands NOP and NOP + 1. */
3130 static inline void
3131 swap_operands (int nop)
3133 enum machine_mode mode = curr_operand_mode[nop];
3134 curr_operand_mode[nop] = curr_operand_mode[nop + 1];
3135 curr_operand_mode[nop + 1] = mode;
3136 rtx x = *curr_id->operand_loc[nop];
3137 *curr_id->operand_loc[nop] = *curr_id->operand_loc[nop + 1];
3138 *curr_id->operand_loc[nop + 1] = x;
3139 /* Swap the duplicates too. */
3140 lra_update_dup (curr_id, nop);
3141 lra_update_dup (curr_id, nop + 1);
3144 /* Main entry point of the constraint code: search the body of the
3145 current insn to choose the best alternative. It is mimicking insn
3146 alternative cost calculation model of former reload pass. That is
3147 because machine descriptions were written to use this model. This
3148 model can be changed in future. Make commutative operand exchange
3149 if it is chosen.
3151 Return true if some RTL changes happened during function call. */
3152 static bool
3153 curr_insn_transform (void)
3155 int i, j, k;
3156 int n_operands;
3157 int n_alternatives;
3158 int commutative;
3159 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3160 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3161 rtx_insn *before, *after;
3162 bool alt_p = false;
3163 /* Flag that the insn has been changed through a transformation. */
3164 bool change_p;
3165 bool sec_mem_p;
3166 #ifdef SECONDARY_MEMORY_NEEDED
3167 bool use_sec_mem_p;
3168 #endif
3169 int max_regno_before;
3170 int reused_alternative_num;
3172 curr_insn_set = single_set (curr_insn);
3173 if (curr_insn_set != NULL_RTX && simple_move_p ())
3174 return false;
3176 no_input_reloads_p = no_output_reloads_p = false;
3177 goal_alt_number = -1;
3178 change_p = sec_mem_p = false;
3179 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3180 reloads; neither are insns that SET cc0. Insns that use CC0 are
3181 not allowed to have any input reloads. */
3182 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3183 no_output_reloads_p = true;
3185 #ifdef HAVE_cc0
3186 if (reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3187 no_input_reloads_p = true;
3188 if (reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3189 no_output_reloads_p = true;
3190 #endif
3192 n_operands = curr_static_id->n_operands;
3193 n_alternatives = curr_static_id->n_alternatives;
3195 /* Just return "no reloads" if insn has no operands with
3196 constraints. */
3197 if (n_operands == 0 || n_alternatives == 0)
3198 return false;
3200 max_regno_before = max_reg_num ();
3202 for (i = 0; i < n_operands; i++)
3204 goal_alt_matched[i][0] = -1;
3205 goal_alt_matches[i] = -1;
3208 commutative = curr_static_id->commutative;
3210 /* Now see what we need for pseudos that didn't get hard regs or got
3211 the wrong kind of hard reg. For this, we must consider all the
3212 operands together against the register constraints. */
3214 best_losers = best_overall = INT_MAX;
3215 best_reload_sum = 0;
3217 curr_swapped = false;
3218 goal_alt_swapped = false;
3220 /* Make equivalence substitution and memory subreg elimination
3221 before address processing because an address legitimacy can
3222 depend on memory mode. */
3223 for (i = 0; i < n_operands; i++)
3225 rtx op = *curr_id->operand_loc[i];
3226 rtx subst, old = op;
3227 bool op_change_p = false;
3229 if (GET_CODE (old) == SUBREG)
3230 old = SUBREG_REG (old);
3231 subst = get_equiv_with_elimination (old, curr_insn);
3232 if (subst != old)
3234 subst = copy_rtx (subst);
3235 lra_assert (REG_P (old));
3236 if (GET_CODE (op) == SUBREG)
3237 SUBREG_REG (op) = subst;
3238 else
3239 *curr_id->operand_loc[i] = subst;
3240 if (lra_dump_file != NULL)
3242 fprintf (lra_dump_file,
3243 "Changing pseudo %d in operand %i of insn %u on equiv ",
3244 REGNO (old), i, INSN_UID (curr_insn));
3245 dump_value_slim (lra_dump_file, subst, 1);
3246 fprintf (lra_dump_file, "\n");
3248 op_change_p = change_p = true;
3250 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3252 change_p = true;
3253 lra_update_dup (curr_id, i);
3257 /* Reload address registers and displacements. We do it before
3258 finding an alternative because of memory constraints. */
3259 before = after = NULL;
3260 for (i = 0; i < n_operands; i++)
3261 if (! curr_static_id->operand[i].is_operator
3262 && process_address (i, &before, &after))
3264 change_p = true;
3265 lra_update_dup (curr_id, i);
3268 if (change_p)
3269 /* If we've changed the instruction then any alternative that
3270 we chose previously may no longer be valid. */
3271 lra_set_used_insn_alternative (curr_insn, -1);
3273 if (curr_insn_set != NULL_RTX
3274 && check_and_process_move (&change_p, &sec_mem_p))
3275 return change_p;
3277 try_swapped:
3279 reused_alternative_num = curr_id->used_insn_alternative;
3280 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3281 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3282 reused_alternative_num, INSN_UID (curr_insn));
3284 if (process_alt_operands (reused_alternative_num))
3285 alt_p = true;
3287 /* If insn is commutative (it's safe to exchange a certain pair of
3288 operands) then we need to try each alternative twice, the second
3289 time matching those two operands as if we had exchanged them. To
3290 do this, really exchange them in operands.
3292 If we have just tried the alternatives the second time, return
3293 operands to normal and drop through. */
3295 if (reused_alternative_num < 0 && commutative >= 0)
3297 curr_swapped = !curr_swapped;
3298 if (curr_swapped)
3300 swap_operands (commutative);
3301 goto try_swapped;
3303 else
3304 swap_operands (commutative);
3307 if (! alt_p && ! sec_mem_p)
3309 /* No alternative works with reloads?? */
3310 if (INSN_CODE (curr_insn) >= 0)
3311 fatal_insn ("unable to generate reloads for:", curr_insn);
3312 error_for_asm (curr_insn,
3313 "inconsistent operand constraints in an %<asm%>");
3314 /* Avoid further trouble with this insn. */
3315 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3316 lra_invalidate_insn_data (curr_insn);
3317 return true;
3320 /* If the best alternative is with operands 1 and 2 swapped, swap
3321 them. Update the operand numbers of any reloads already
3322 pushed. */
3324 if (goal_alt_swapped)
3326 if (lra_dump_file != NULL)
3327 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3328 INSN_UID (curr_insn));
3330 /* Swap the duplicates too. */
3331 swap_operands (commutative);
3332 change_p = true;
3335 #ifdef SECONDARY_MEMORY_NEEDED
3336 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3337 too conservatively. So we use the secondary memory only if there
3338 is no any alternative without reloads. */
3339 use_sec_mem_p = false;
3340 if (! alt_p)
3341 use_sec_mem_p = true;
3342 else if (sec_mem_p)
3344 for (i = 0; i < n_operands; i++)
3345 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3346 break;
3347 use_sec_mem_p = i < n_operands;
3350 if (use_sec_mem_p)
3352 rtx new_reg, src, dest, rld;
3353 enum machine_mode sec_mode, rld_mode;
3355 lra_assert (sec_mem_p);
3356 lra_assert (curr_static_id->operand[0].type == OP_OUT
3357 && curr_static_id->operand[1].type == OP_IN);
3358 dest = *curr_id->operand_loc[0];
3359 src = *curr_id->operand_loc[1];
3360 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3361 ? dest : src);
3362 rld_mode = GET_MODE (rld);
3363 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3364 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3365 #else
3366 sec_mode = rld_mode;
3367 #endif
3368 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3369 NO_REGS, "secondary");
3370 /* If the mode is changed, it should be wider. */
3371 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3372 if (sec_mode != rld_mode)
3374 /* If the target says specifically to use another mode for
3375 secondary memory moves we can not reuse the original
3376 insn. */
3377 after = emit_spill_move (false, new_reg, dest);
3378 lra_process_new_insns (curr_insn, NULL, after,
3379 "Inserting the sec. move");
3380 /* We may have non null BEFORE here (e.g. after address
3381 processing. */
3382 push_to_sequence (before);
3383 before = emit_spill_move (true, new_reg, src);
3384 emit_insn (before);
3385 before = get_insns ();
3386 end_sequence ();
3387 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3388 lra_set_insn_deleted (curr_insn);
3390 else if (dest == rld)
3392 *curr_id->operand_loc[0] = new_reg;
3393 after = emit_spill_move (false, new_reg, dest);
3394 lra_process_new_insns (curr_insn, NULL, after,
3395 "Inserting the sec. move");
3397 else
3399 *curr_id->operand_loc[1] = new_reg;
3400 /* See comments above. */
3401 push_to_sequence (before);
3402 before = emit_spill_move (true, new_reg, src);
3403 emit_insn (before);
3404 before = get_insns ();
3405 end_sequence ();
3406 lra_process_new_insns (curr_insn, before, NULL,
3407 "Inserting the sec. move");
3409 lra_update_insn_regno_info (curr_insn);
3410 return true;
3412 #endif
3414 lra_assert (goal_alt_number >= 0);
3415 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3417 if (lra_dump_file != NULL)
3419 const char *p;
3421 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3422 goal_alt_number, INSN_UID (curr_insn));
3423 for (i = 0; i < n_operands; i++)
3425 p = (curr_static_id->operand_alternative
3426 [goal_alt_number * n_operands + i].constraint);
3427 if (*p == '\0')
3428 continue;
3429 fprintf (lra_dump_file, " (%d) ", i);
3430 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3431 fputc (*p, lra_dump_file);
3433 if (INSN_CODE (curr_insn) >= 0
3434 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3435 fprintf (lra_dump_file, " {%s}", p);
3436 if (curr_id->sp_offset != 0)
3437 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3438 curr_id->sp_offset);
3439 fprintf (lra_dump_file, "\n");
3442 /* Right now, for any pair of operands I and J that are required to
3443 match, with J < I, goal_alt_matches[I] is J. Add I to
3444 goal_alt_matched[J]. */
3446 for (i = 0; i < n_operands; i++)
3447 if ((j = goal_alt_matches[i]) >= 0)
3449 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3451 /* We allow matching one output operand and several input
3452 operands. */
3453 lra_assert (k == 0
3454 || (curr_static_id->operand[j].type == OP_OUT
3455 && curr_static_id->operand[i].type == OP_IN
3456 && (curr_static_id->operand
3457 [goal_alt_matched[j][0]].type == OP_IN)));
3458 goal_alt_matched[j][k] = i;
3459 goal_alt_matched[j][k + 1] = -1;
3462 for (i = 0; i < n_operands; i++)
3463 goal_alt_win[i] |= goal_alt_match_win[i];
3465 /* Any constants that aren't allowed and can't be reloaded into
3466 registers are here changed into memory references. */
3467 for (i = 0; i < n_operands; i++)
3468 if (goal_alt_win[i])
3470 int regno;
3471 enum reg_class new_class;
3472 rtx reg = *curr_id->operand_loc[i];
3474 if (GET_CODE (reg) == SUBREG)
3475 reg = SUBREG_REG (reg);
3477 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3479 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3481 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3483 lra_assert (ok_p);
3484 lra_change_class (regno, new_class, " Change to", true);
3488 else
3490 const char *constraint;
3491 char c;
3492 rtx op = *curr_id->operand_loc[i];
3493 rtx subreg = NULL_RTX;
3494 enum machine_mode mode = curr_operand_mode[i];
3496 if (GET_CODE (op) == SUBREG)
3498 subreg = op;
3499 op = SUBREG_REG (op);
3500 mode = GET_MODE (op);
3503 if (CONST_POOL_OK_P (mode, op)
3504 && ((targetm.preferred_reload_class
3505 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3506 || no_input_reloads_p))
3508 rtx tem = force_const_mem (mode, op);
3510 change_p = true;
3511 if (subreg != NULL_RTX)
3512 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3514 *curr_id->operand_loc[i] = tem;
3515 lra_update_dup (curr_id, i);
3516 process_address (i, &before, &after);
3518 /* If the alternative accepts constant pool refs directly
3519 there will be no reload needed at all. */
3520 if (subreg != NULL_RTX)
3521 continue;
3522 /* Skip alternatives before the one requested. */
3523 constraint = (curr_static_id->operand_alternative
3524 [goal_alt_number * n_operands + i].constraint);
3525 for (;
3526 (c = *constraint) && c != ',' && c != '#';
3527 constraint += CONSTRAINT_LEN (c, constraint))
3529 enum constraint_num cn = lookup_constraint (constraint);
3530 if (insn_extra_memory_constraint (cn)
3531 && satisfies_memory_constraint_p (tem, cn))
3532 break;
3534 if (c == '\0' || c == ',' || c == '#')
3535 continue;
3537 goal_alt_win[i] = true;
3541 for (i = 0; i < n_operands; i++)
3543 int regno;
3544 bool optional_p = false;
3545 rtx old, new_reg;
3546 rtx op = *curr_id->operand_loc[i];
3548 if (goal_alt_win[i])
3550 if (goal_alt[i] == NO_REGS
3551 && REG_P (op)
3552 /* When we assign NO_REGS it means that we will not
3553 assign a hard register to the scratch pseudo by
3554 assigment pass and the scratch pseudo will be
3555 spilled. Spilled scratch pseudos are transformed
3556 back to scratches at the LRA end. */
3557 && lra_former_scratch_operand_p (curr_insn, i))
3559 int regno = REGNO (op);
3560 lra_change_class (regno, NO_REGS, " Change to", true);
3561 if (lra_get_regno_hard_regno (regno) >= 0)
3562 /* We don't have to mark all insn affected by the
3563 spilled pseudo as there is only one such insn, the
3564 current one. */
3565 reg_renumber[regno] = -1;
3567 /* We can do an optional reload. If the pseudo got a hard
3568 reg, we might improve the code through inheritance. If
3569 it does not get a hard register we coalesce memory/memory
3570 moves later. Ignore move insns to avoid cycling. */
3571 if (! lra_simple_p
3572 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3573 && goal_alt[i] != NO_REGS && REG_P (op)
3574 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3575 && regno < new_regno_start
3576 && ! lra_former_scratch_p (regno)
3577 && reg_renumber[regno] < 0
3578 && (curr_insn_set == NULL_RTX
3579 || !((REG_P (SET_SRC (curr_insn_set))
3580 || MEM_P (SET_SRC (curr_insn_set))
3581 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3582 && (REG_P (SET_DEST (curr_insn_set))
3583 || MEM_P (SET_DEST (curr_insn_set))
3584 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3585 optional_p = true;
3586 else
3587 continue;
3590 /* Operands that match previous ones have already been handled. */
3591 if (goal_alt_matches[i] >= 0)
3592 continue;
3594 /* We should not have an operand with a non-offsettable address
3595 appearing where an offsettable address will do. It also may
3596 be a case when the address should be special in other words
3597 not a general one (e.g. it needs no index reg). */
3598 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3600 enum reg_class rclass;
3601 rtx *loc = &XEXP (op, 0);
3602 enum rtx_code code = GET_CODE (*loc);
3604 push_to_sequence (before);
3605 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3606 MEM, SCRATCH);
3607 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3608 new_reg = emit_inc (rclass, *loc, *loc,
3609 /* This value does not matter for MODIFY. */
3610 GET_MODE_SIZE (GET_MODE (op)));
3611 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3612 "offsetable address", &new_reg))
3613 lra_emit_move (new_reg, *loc);
3614 before = get_insns ();
3615 end_sequence ();
3616 *loc = new_reg;
3617 lra_update_dup (curr_id, i);
3619 else if (goal_alt_matched[i][0] == -1)
3621 enum machine_mode mode;
3622 rtx reg, *loc;
3623 int hard_regno, byte;
3624 enum op_type type = curr_static_id->operand[i].type;
3626 loc = curr_id->operand_loc[i];
3627 mode = curr_operand_mode[i];
3628 if (GET_CODE (*loc) == SUBREG)
3630 reg = SUBREG_REG (*loc);
3631 byte = SUBREG_BYTE (*loc);
3632 if (REG_P (reg)
3633 /* Strict_low_part requires reload the register not
3634 the sub-register. */
3635 && (curr_static_id->operand[i].strict_low
3636 || (GET_MODE_SIZE (mode)
3637 <= GET_MODE_SIZE (GET_MODE (reg))
3638 && (hard_regno
3639 = get_try_hard_regno (REGNO (reg))) >= 0
3640 && (simplify_subreg_regno
3641 (hard_regno,
3642 GET_MODE (reg), byte, mode) < 0)
3643 && (goal_alt[i] == NO_REGS
3644 || (simplify_subreg_regno
3645 (ira_class_hard_regs[goal_alt[i]][0],
3646 GET_MODE (reg), byte, mode) >= 0)))))
3648 loc = &SUBREG_REG (*loc);
3649 mode = GET_MODE (*loc);
3652 old = *loc;
3653 if (get_reload_reg (type, mode, old, goal_alt[i],
3654 loc != curr_id->operand_loc[i], "", &new_reg)
3655 && type != OP_OUT)
3657 push_to_sequence (before);
3658 lra_emit_move (new_reg, old);
3659 before = get_insns ();
3660 end_sequence ();
3662 *loc = new_reg;
3663 if (type != OP_IN
3664 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3666 start_sequence ();
3667 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3668 emit_insn (after);
3669 after = get_insns ();
3670 end_sequence ();
3671 *loc = new_reg;
3673 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3674 if (goal_alt_dont_inherit_ops[j] == i)
3676 lra_set_regno_unique_value (REGNO (new_reg));
3677 break;
3679 lra_update_dup (curr_id, i);
3681 else if (curr_static_id->operand[i].type == OP_IN
3682 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3683 == OP_OUT))
3685 /* generate reloads for input and matched outputs. */
3686 match_inputs[0] = i;
3687 match_inputs[1] = -1;
3688 match_reload (goal_alt_matched[i][0], match_inputs,
3689 goal_alt[i], &before, &after);
3691 else if (curr_static_id->operand[i].type == OP_OUT
3692 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3693 == OP_IN))
3694 /* Generate reloads for output and matched inputs. */
3695 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3696 else if (curr_static_id->operand[i].type == OP_IN
3697 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3698 == OP_IN))
3700 /* Generate reloads for matched inputs. */
3701 match_inputs[0] = i;
3702 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3703 match_inputs[j + 1] = k;
3704 match_inputs[j + 1] = -1;
3705 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3707 else
3708 /* We must generate code in any case when function
3709 process_alt_operands decides that it is possible. */
3710 gcc_unreachable ();
3711 if (optional_p)
3713 lra_assert (REG_P (op));
3714 regno = REGNO (op);
3715 op = *curr_id->operand_loc[i]; /* Substitution. */
3716 if (GET_CODE (op) == SUBREG)
3717 op = SUBREG_REG (op);
3718 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3719 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3720 lra_reg_info[REGNO (op)].restore_regno = regno;
3721 if (lra_dump_file != NULL)
3722 fprintf (lra_dump_file,
3723 " Making reload reg %d for reg %d optional\n",
3724 REGNO (op), regno);
3727 if (before != NULL_RTX || after != NULL_RTX
3728 || max_regno_before != max_reg_num ())
3729 change_p = true;
3730 if (change_p)
3732 lra_update_operator_dups (curr_id);
3733 /* Something changes -- process the insn. */
3734 lra_update_insn_regno_info (curr_insn);
3736 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3737 return change_p;
3740 /* Return true if X is in LIST. */
3741 static bool
3742 in_list_p (rtx x, rtx list)
3744 for (; list != NULL_RTX; list = XEXP (list, 1))
3745 if (XEXP (list, 0) == x)
3746 return true;
3747 return false;
3750 /* Return true if X contains an allocatable hard register (if
3751 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3752 static bool
3753 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3755 int i, j;
3756 const char *fmt;
3757 enum rtx_code code;
3759 code = GET_CODE (x);
3760 if (REG_P (x))
3762 int regno = REGNO (x);
3763 HARD_REG_SET alloc_regs;
3765 if (hard_reg_p)
3767 if (regno >= FIRST_PSEUDO_REGISTER)
3768 regno = lra_get_regno_hard_regno (regno);
3769 if (regno < 0)
3770 return false;
3771 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3772 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3774 else
3776 if (regno < FIRST_PSEUDO_REGISTER)
3777 return false;
3778 if (! spilled_p)
3779 return true;
3780 return lra_get_regno_hard_regno (regno) < 0;
3783 fmt = GET_RTX_FORMAT (code);
3784 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3786 if (fmt[i] == 'e')
3788 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3789 return true;
3791 else if (fmt[i] == 'E')
3793 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3794 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3795 return true;
3798 return false;
3801 /* Return true if X contains a symbol reg. */
3802 static bool
3803 contains_symbol_ref_p (rtx x)
3805 int i, j;
3806 const char *fmt;
3807 enum rtx_code code;
3809 code = GET_CODE (x);
3810 if (code == SYMBOL_REF)
3811 return true;
3812 fmt = GET_RTX_FORMAT (code);
3813 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3815 if (fmt[i] == 'e')
3817 if (contains_symbol_ref_p (XEXP (x, i)))
3818 return true;
3820 else if (fmt[i] == 'E')
3822 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3823 if (contains_symbol_ref_p (XVECEXP (x, i, j)))
3824 return true;
3827 return false;
3830 /* Process all regs in location *LOC and change them on equivalent
3831 substitution. Return true if any change was done. */
3832 static bool
3833 loc_equivalence_change_p (rtx *loc)
3835 rtx subst, reg, x = *loc;
3836 bool result = false;
3837 enum rtx_code code = GET_CODE (x);
3838 const char *fmt;
3839 int i, j;
3841 if (code == SUBREG)
3843 reg = SUBREG_REG (x);
3844 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
3845 && GET_MODE (subst) == VOIDmode)
3847 /* We cannot reload debug location. Simplify subreg here
3848 while we know the inner mode. */
3849 *loc = simplify_gen_subreg (GET_MODE (x), subst,
3850 GET_MODE (reg), SUBREG_BYTE (x));
3851 return true;
3854 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
3856 *loc = subst;
3857 return true;
3860 /* Scan all the operand sub-expressions. */
3861 fmt = GET_RTX_FORMAT (code);
3862 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3864 if (fmt[i] == 'e')
3865 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
3866 else if (fmt[i] == 'E')
3867 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3868 result
3869 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
3871 return result;
3874 /* Similar to loc_equivalence_change_p, but for use as
3875 simplify_replace_fn_rtx callback. DATA is insn for which the
3876 elimination is done. If it null we don't do the elimination. */
3877 static rtx
3878 loc_equivalence_callback (rtx loc, const_rtx, void *data)
3880 if (!REG_P (loc))
3881 return NULL_RTX;
3883 rtx subst = (data == NULL
3884 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
3885 if (subst != loc)
3886 return subst;
3888 return NULL_RTX;
3891 /* Maximum number of generated reload insns per an insn. It is for
3892 preventing this pass cycling in a bug case. */
3893 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
3895 /* The current iteration number of this LRA pass. */
3896 int lra_constraint_iter;
3898 /* True if we substituted equiv which needs checking register
3899 allocation correctness because the equivalent value contains
3900 allocatable hard registers or when we restore multi-register
3901 pseudo. */
3902 bool lra_risky_transformations_p;
3904 /* Return true if REGNO is referenced in more than one block. */
3905 static bool
3906 multi_block_pseudo_p (int regno)
3908 basic_block bb = NULL;
3909 unsigned int uid;
3910 bitmap_iterator bi;
3912 if (regno < FIRST_PSEUDO_REGISTER)
3913 return false;
3915 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
3916 if (bb == NULL)
3917 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
3918 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
3919 return true;
3920 return false;
3923 /* Return true if LIST contains a deleted insn. */
3924 static bool
3925 contains_deleted_insn_p (rtx_insn_list *list)
3927 for (; list != NULL_RTX; list = list->next ())
3928 if (NOTE_P (list->insn ())
3929 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
3930 return true;
3931 return false;
3934 /* Return true if X contains a pseudo dying in INSN. */
3935 static bool
3936 dead_pseudo_p (rtx x, rtx insn)
3938 int i, j;
3939 const char *fmt;
3940 enum rtx_code code;
3942 if (REG_P (x))
3943 return (insn != NULL_RTX
3944 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
3945 code = GET_CODE (x);
3946 fmt = GET_RTX_FORMAT (code);
3947 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3949 if (fmt[i] == 'e')
3951 if (dead_pseudo_p (XEXP (x, i), insn))
3952 return true;
3954 else if (fmt[i] == 'E')
3956 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3957 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
3958 return true;
3961 return false;
3964 /* Return true if INSN contains a dying pseudo in INSN right hand
3965 side. */
3966 static bool
3967 insn_rhs_dead_pseudo_p (rtx_insn *insn)
3969 rtx set = single_set (insn);
3971 gcc_assert (set != NULL);
3972 return dead_pseudo_p (SET_SRC (set), insn);
3975 /* Return true if any init insn of REGNO contains a dying pseudo in
3976 insn right hand side. */
3977 static bool
3978 init_insn_rhs_dead_pseudo_p (int regno)
3980 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
3982 if (insns == NULL)
3983 return false;
3984 for (; insns != NULL_RTX; insns = insns->next ())
3985 if (insn_rhs_dead_pseudo_p (insns->insn ()))
3986 return true;
3987 return false;
3990 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
3991 reverse only if we have one init insn with given REGNO as a
3992 source. */
3993 static bool
3994 reverse_equiv_p (int regno)
3996 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
3997 rtx set;
3999 if (insns == NULL)
4000 return false;
4001 if (! INSN_P (insns->insn ())
4002 || insns->next () != NULL)
4003 return false;
4004 if ((set = single_set (insns->insn ())) == NULL_RTX)
4005 return false;
4006 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4009 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4010 call this function only for non-reverse equivalence. */
4011 static bool
4012 contains_reloaded_insn_p (int regno)
4014 rtx set;
4015 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4017 for (; list != NULL; list = list->next ())
4018 if ((set = single_set (list->insn ())) == NULL_RTX
4019 || ! REG_P (SET_DEST (set))
4020 || (int) REGNO (SET_DEST (set)) != regno)
4021 return true;
4022 return false;
4025 /* Entry function of LRA constraint pass. Return true if the
4026 constraint pass did change the code. */
4027 bool
4028 lra_constraints (bool first_p)
4030 bool changed_p;
4031 int i, hard_regno, new_insns_num;
4032 unsigned int min_len, new_min_len, uid;
4033 rtx set, x, reg, dest_reg;
4034 basic_block last_bb;
4035 bitmap_head equiv_insn_bitmap;
4036 bitmap_iterator bi;
4038 lra_constraint_iter++;
4039 if (lra_dump_file != NULL)
4040 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4041 lra_constraint_iter);
4042 changed_p = false;
4043 if (pic_offset_table_rtx
4044 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4045 lra_risky_transformations_p = true;
4046 else
4047 lra_risky_transformations_p = false;
4048 new_insn_uid_start = get_max_uid ();
4049 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4050 /* Mark used hard regs for target stack size calulations. */
4051 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4052 if (lra_reg_info[i].nrefs != 0
4053 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4055 int j, nregs;
4057 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4058 for (j = 0; j < nregs; j++)
4059 df_set_regs_ever_live (hard_regno + j, true);
4061 /* Do elimination before the equivalence processing as we can spill
4062 some pseudos during elimination. */
4063 lra_eliminate (false, first_p);
4064 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4065 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4066 if (lra_reg_info[i].nrefs != 0)
4068 ira_reg_equiv[i].profitable_p = true;
4069 reg = regno_reg_rtx[i];
4070 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4072 bool pseudo_p = contains_reg_p (x, false, false);
4074 /* After RTL transformation, we can not guarantee that
4075 pseudo in the substitution was not reloaded which might
4076 make equivalence invalid. For example, in reverse
4077 equiv of p0
4079 p0 <- ...
4081 equiv_mem <- p0
4083 the memory address register was reloaded before the 2nd
4084 insn. */
4085 if ((! first_p && pseudo_p)
4086 /* We don't use DF for compilation speed sake. So it
4087 is problematic to update live info when we use an
4088 equivalence containing pseudos in more than one
4089 BB. */
4090 || (pseudo_p && multi_block_pseudo_p (i))
4091 /* If an init insn was deleted for some reason, cancel
4092 the equiv. We could update the equiv insns after
4093 transformations including an equiv insn deletion
4094 but it is not worthy as such cases are extremely
4095 rare. */
4096 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4097 /* If it is not a reverse equivalence, we check that a
4098 pseudo in rhs of the init insn is not dying in the
4099 insn. Otherwise, the live info at the beginning of
4100 the corresponding BB might be wrong after we
4101 removed the insn. When the equiv can be a
4102 constant, the right hand side of the init insn can
4103 be a pseudo. */
4104 || (! reverse_equiv_p (i)
4105 && (init_insn_rhs_dead_pseudo_p (i)
4106 /* If we reloaded the pseudo in an equivalence
4107 init insn, we can not remove the equiv init
4108 insns and the init insns might write into
4109 const memory in this case. */
4110 || contains_reloaded_insn_p (i)))
4111 /* Prevent access beyond equivalent memory for
4112 paradoxical subregs. */
4113 || (MEM_P (x)
4114 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4115 > GET_MODE_SIZE (GET_MODE (x))))
4116 || (pic_offset_table_rtx
4117 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4118 && (targetm.preferred_reload_class
4119 (x, lra_get_allocno_class (i)) == NO_REGS))
4120 || contains_symbol_ref_p (x))))
4121 ira_reg_equiv[i].defined_p = false;
4122 if (contains_reg_p (x, false, true))
4123 ira_reg_equiv[i].profitable_p = false;
4124 if (get_equiv (reg) != reg)
4125 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4128 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4129 update_equiv (i);
4130 /* We should add all insns containing pseudos which should be
4131 substituted by their equivalences. */
4132 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4133 lra_push_insn_by_uid (uid);
4134 min_len = lra_insn_stack_length ();
4135 new_insns_num = 0;
4136 last_bb = NULL;
4137 changed_p = false;
4138 while ((new_min_len = lra_insn_stack_length ()) != 0)
4140 curr_insn = lra_pop_insn ();
4141 --new_min_len;
4142 curr_bb = BLOCK_FOR_INSN (curr_insn);
4143 if (curr_bb != last_bb)
4145 last_bb = curr_bb;
4146 bb_reload_num = lra_curr_reload_num;
4148 if (min_len > new_min_len)
4150 min_len = new_min_len;
4151 new_insns_num = 0;
4153 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4154 internal_error
4155 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4156 MAX_RELOAD_INSNS_NUMBER);
4157 new_insns_num++;
4158 if (DEBUG_INSN_P (curr_insn))
4160 /* We need to check equivalence in debug insn and change
4161 pseudo to the equivalent value if necessary. */
4162 curr_id = lra_get_insn_recog_data (curr_insn);
4163 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4165 rtx old = *curr_id->operand_loc[0];
4166 *curr_id->operand_loc[0]
4167 = simplify_replace_fn_rtx (old, NULL_RTX,
4168 loc_equivalence_callback, curr_insn);
4169 if (old != *curr_id->operand_loc[0])
4171 lra_update_insn_regno_info (curr_insn);
4172 changed_p = true;
4176 else if (INSN_P (curr_insn))
4178 if ((set = single_set (curr_insn)) != NULL_RTX)
4180 dest_reg = SET_DEST (set);
4181 /* The equivalence pseudo could be set up as SUBREG in a
4182 case when it is a call restore insn in a mode
4183 different from the pseudo mode. */
4184 if (GET_CODE (dest_reg) == SUBREG)
4185 dest_reg = SUBREG_REG (dest_reg);
4186 if ((REG_P (dest_reg)
4187 && (x = get_equiv (dest_reg)) != dest_reg
4188 /* Remove insns which set up a pseudo whose value
4189 can not be changed. Such insns might be not in
4190 init_insns because we don't update equiv data
4191 during insn transformations.
4193 As an example, let suppose that a pseudo got
4194 hard register and on the 1st pass was not
4195 changed to equivalent constant. We generate an
4196 additional insn setting up the pseudo because of
4197 secondary memory movement. Then the pseudo is
4198 spilled and we use the equiv constant. In this
4199 case we should remove the additional insn and
4200 this insn is not init_insns list. */
4201 && (! MEM_P (x) || MEM_READONLY_P (x)
4202 /* Check that this is actually an insn setting
4203 up the equivalence. */
4204 || in_list_p (curr_insn,
4205 ira_reg_equiv
4206 [REGNO (dest_reg)].init_insns)))
4207 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4208 && in_list_p (curr_insn,
4209 ira_reg_equiv
4210 [REGNO (SET_SRC (set))].init_insns)))
4212 /* This is equiv init insn of pseudo which did not get a
4213 hard register -- remove the insn. */
4214 if (lra_dump_file != NULL)
4216 fprintf (lra_dump_file,
4217 " Removing equiv init insn %i (freq=%d)\n",
4218 INSN_UID (curr_insn),
4219 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4220 dump_insn_slim (lra_dump_file, curr_insn);
4222 if (contains_reg_p (x, true, false))
4223 lra_risky_transformations_p = true;
4224 lra_set_insn_deleted (curr_insn);
4225 continue;
4228 curr_id = lra_get_insn_recog_data (curr_insn);
4229 curr_static_id = curr_id->insn_static_data;
4230 init_curr_insn_input_reloads ();
4231 init_curr_operand_mode ();
4232 if (curr_insn_transform ())
4233 changed_p = true;
4234 /* Check non-transformed insns too for equiv change as USE
4235 or CLOBBER don't need reloads but can contain pseudos
4236 being changed on their equivalences. */
4237 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4238 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4240 lra_update_insn_regno_info (curr_insn);
4241 changed_p = true;
4245 bitmap_clear (&equiv_insn_bitmap);
4246 /* If we used a new hard regno, changed_p should be true because the
4247 hard reg is assigned to a new pseudo. */
4248 #ifdef ENABLE_CHECKING
4249 if (! changed_p)
4251 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4252 if (lra_reg_info[i].nrefs != 0
4253 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4255 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4257 for (j = 0; j < nregs; j++)
4258 lra_assert (df_regs_ever_live_p (hard_regno + j));
4261 #endif
4262 return changed_p;
4265 /* Initiate the LRA constraint pass. It is done once per
4266 function. */
4267 void
4268 lra_constraints_init (void)
4272 /* Finalize the LRA constraint pass. It is done once per
4273 function. */
4274 void
4275 lra_constraints_finish (void)
4281 /* This page contains code to do inheritance/split
4282 transformations. */
4284 /* Number of reloads passed so far in current EBB. */
4285 static int reloads_num;
4287 /* Number of calls passed so far in current EBB. */
4288 static int calls_num;
4290 /* Current reload pseudo check for validity of elements in
4291 USAGE_INSNS. */
4292 static int curr_usage_insns_check;
4294 /* Info about last usage of registers in EBB to do inheritance/split
4295 transformation. Inheritance transformation is done from a spilled
4296 pseudo and split transformations from a hard register or a pseudo
4297 assigned to a hard register. */
4298 struct usage_insns
4300 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4301 value INSNS is valid. The insns is chain of optional debug insns
4302 and a finishing non-debug insn using the corresponding reg. The
4303 value is also used to mark the registers which are set up in the
4304 current insn. The negated insn uid is used for this. */
4305 int check;
4306 /* Value of global reloads_num at the last insn in INSNS. */
4307 int reloads_num;
4308 /* Value of global reloads_nums at the last insn in INSNS. */
4309 int calls_num;
4310 /* It can be true only for splitting. And it means that the restore
4311 insn should be put after insn given by the following member. */
4312 bool after_p;
4313 /* Next insns in the current EBB which use the original reg and the
4314 original reg value is not changed between the current insn and
4315 the next insns. In order words, e.g. for inheritance, if we need
4316 to use the original reg value again in the next insns we can try
4317 to use the value in a hard register from a reload insn of the
4318 current insn. */
4319 rtx insns;
4322 /* Map: regno -> corresponding pseudo usage insns. */
4323 static struct usage_insns *usage_insns;
4325 static void
4326 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4328 usage_insns[regno].check = curr_usage_insns_check;
4329 usage_insns[regno].insns = insn;
4330 usage_insns[regno].reloads_num = reloads_num;
4331 usage_insns[regno].calls_num = calls_num;
4332 usage_insns[regno].after_p = after_p;
4335 /* The function is used to form list REGNO usages which consists of
4336 optional debug insns finished by a non-debug insn using REGNO.
4337 RELOADS_NUM is current number of reload insns processed so far. */
4338 static void
4339 add_next_usage_insn (int regno, rtx insn, int reloads_num)
4341 rtx next_usage_insns;
4343 if (usage_insns[regno].check == curr_usage_insns_check
4344 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4345 && DEBUG_INSN_P (insn))
4347 /* Check that we did not add the debug insn yet. */
4348 if (next_usage_insns != insn
4349 && (GET_CODE (next_usage_insns) != INSN_LIST
4350 || XEXP (next_usage_insns, 0) != insn))
4351 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4352 next_usage_insns);
4354 else if (NONDEBUG_INSN_P (insn))
4355 setup_next_usage_insn (regno, insn, reloads_num, false);
4356 else
4357 usage_insns[regno].check = 0;
4360 /* Replace all references to register OLD_REGNO in *LOC with pseudo
4361 register NEW_REG. Return true if any change was made. */
4362 static bool
4363 substitute_pseudo (rtx *loc, int old_regno, rtx new_reg)
4365 rtx x = *loc;
4366 bool result = false;
4367 enum rtx_code code;
4368 const char *fmt;
4369 int i, j;
4371 if (x == NULL_RTX)
4372 return false;
4374 code = GET_CODE (x);
4375 if (code == REG && (int) REGNO (x) == old_regno)
4377 enum machine_mode mode = GET_MODE (*loc);
4378 enum machine_mode inner_mode = GET_MODE (new_reg);
4380 if (mode != inner_mode)
4382 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
4383 || ! SCALAR_INT_MODE_P (inner_mode))
4384 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
4385 else
4386 new_reg = gen_lowpart_SUBREG (mode, new_reg);
4388 *loc = new_reg;
4389 return true;
4392 /* Scan all the operand sub-expressions. */
4393 fmt = GET_RTX_FORMAT (code);
4394 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4396 if (fmt[i] == 'e')
4398 if (substitute_pseudo (&XEXP (x, i), old_regno, new_reg))
4399 result = true;
4401 else if (fmt[i] == 'E')
4403 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4404 if (substitute_pseudo (&XVECEXP (x, i, j), old_regno, new_reg))
4405 result = true;
4408 return result;
4411 /* Call substitute_pseudo within an insn. This won't update the insn ptr,
4412 just the contents of the insn. */
4414 static bool
4415 substitute_pseudo_within_insn (rtx_insn *insn, int old_regno, rtx new_reg)
4417 rtx loc = insn;
4418 return substitute_pseudo (&loc, old_regno, new_reg);
4421 /* Return first non-debug insn in list USAGE_INSNS. */
4422 static rtx_insn *
4423 skip_usage_debug_insns (rtx usage_insns)
4425 rtx insn;
4427 /* Skip debug insns. */
4428 for (insn = usage_insns;
4429 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4430 insn = XEXP (insn, 1))
4432 return safe_as_a <rtx_insn *> (insn);
4435 /* Return true if we need secondary memory moves for insn in
4436 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4437 into the insn. */
4438 static bool
4439 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4440 rtx usage_insns ATTRIBUTE_UNUSED)
4442 #ifndef SECONDARY_MEMORY_NEEDED
4443 return false;
4444 #else
4445 rtx_insn *insn;
4446 rtx set, dest;
4447 enum reg_class cl;
4449 if (inher_cl == ALL_REGS
4450 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4451 return false;
4452 lra_assert (INSN_P (insn));
4453 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4454 return false;
4455 dest = SET_DEST (set);
4456 if (! REG_P (dest))
4457 return false;
4458 lra_assert (inher_cl != NO_REGS);
4459 cl = get_reg_class (REGNO (dest));
4460 return (cl != NO_REGS && cl != ALL_REGS
4461 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4462 #endif
4465 /* Registers involved in inheritance/split in the current EBB
4466 (inheritance/split pseudos and original registers). */
4467 static bitmap_head check_only_regs;
4469 /* Do inheritance transformations for insn INSN, which defines (if
4470 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4471 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4472 form as the "insns" field of usage_insns. Return true if we
4473 succeed in such transformation.
4475 The transformations look like:
4477 p <- ... i <- ...
4478 ... p <- i (new insn)
4479 ... =>
4480 <- ... p ... <- ... i ...
4482 ... i <- p (new insn)
4483 <- ... p ... <- ... i ...
4484 ... =>
4485 <- ... p ... <- ... i ...
4486 where p is a spilled original pseudo and i is a new inheritance pseudo.
4489 The inheritance pseudo has the smallest class of two classes CL and
4490 class of ORIGINAL REGNO. */
4491 static bool
4492 inherit_reload_reg (bool def_p, int original_regno,
4493 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4495 if (optimize_function_for_size_p (cfun))
4496 return false;
4498 enum reg_class rclass = lra_get_allocno_class (original_regno);
4499 rtx original_reg = regno_reg_rtx[original_regno];
4500 rtx new_reg, usage_insn;
4501 rtx_insn *new_insns;
4503 lra_assert (! usage_insns[original_regno].after_p);
4504 if (lra_dump_file != NULL)
4505 fprintf (lra_dump_file,
4506 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4507 if (! ira_reg_classes_intersect_p[cl][rclass])
4509 if (lra_dump_file != NULL)
4511 fprintf (lra_dump_file,
4512 " Rejecting inheritance for %d "
4513 "because of disjoint classes %s and %s\n",
4514 original_regno, reg_class_names[cl],
4515 reg_class_names[rclass]);
4516 fprintf (lra_dump_file,
4517 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4519 return false;
4521 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4522 /* We don't use a subset of two classes because it can be
4523 NO_REGS. This transformation is still profitable in most
4524 cases even if the classes are not intersected as register
4525 move is probably cheaper than a memory load. */
4526 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4528 if (lra_dump_file != NULL)
4529 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4530 reg_class_names[cl], reg_class_names[rclass]);
4532 rclass = cl;
4534 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4536 /* Reject inheritance resulting in secondary memory moves.
4537 Otherwise, there is a danger in LRA cycling. Also such
4538 transformation will be unprofitable. */
4539 if (lra_dump_file != NULL)
4541 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4542 rtx set = single_set (insn);
4544 lra_assert (set != NULL_RTX);
4546 rtx dest = SET_DEST (set);
4548 lra_assert (REG_P (dest));
4549 fprintf (lra_dump_file,
4550 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4551 "as secondary mem is needed\n",
4552 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4553 original_regno, reg_class_names[rclass]);
4554 fprintf (lra_dump_file,
4555 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4557 return false;
4559 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4560 rclass, "inheritance");
4561 start_sequence ();
4562 if (def_p)
4563 lra_emit_move (original_reg, new_reg);
4564 else
4565 lra_emit_move (new_reg, original_reg);
4566 new_insns = get_insns ();
4567 end_sequence ();
4568 if (NEXT_INSN (new_insns) != NULL_RTX)
4570 if (lra_dump_file != NULL)
4572 fprintf (lra_dump_file,
4573 " Rejecting inheritance %d->%d "
4574 "as it results in 2 or more insns:\n",
4575 original_regno, REGNO (new_reg));
4576 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4577 fprintf (lra_dump_file,
4578 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4580 return false;
4582 substitute_pseudo_within_insn (insn, original_regno, new_reg);
4583 lra_update_insn_regno_info (insn);
4584 if (! def_p)
4585 /* We now have a new usage insn for original regno. */
4586 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4587 if (lra_dump_file != NULL)
4588 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4589 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4590 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4591 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4592 bitmap_set_bit (&check_only_regs, original_regno);
4593 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4594 if (def_p)
4595 lra_process_new_insns (insn, NULL, new_insns,
4596 "Add original<-inheritance");
4597 else
4598 lra_process_new_insns (insn, new_insns, NULL,
4599 "Add inheritance<-original");
4600 while (next_usage_insns != NULL_RTX)
4602 if (GET_CODE (next_usage_insns) != INSN_LIST)
4604 usage_insn = next_usage_insns;
4605 lra_assert (NONDEBUG_INSN_P (usage_insn));
4606 next_usage_insns = NULL;
4608 else
4610 usage_insn = XEXP (next_usage_insns, 0);
4611 lra_assert (DEBUG_INSN_P (usage_insn));
4612 next_usage_insns = XEXP (next_usage_insns, 1);
4614 substitute_pseudo (&usage_insn, original_regno, new_reg);
4615 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4616 if (lra_dump_file != NULL)
4618 fprintf (lra_dump_file,
4619 " Inheritance reuse change %d->%d (bb%d):\n",
4620 original_regno, REGNO (new_reg),
4621 BLOCK_FOR_INSN (usage_insn)->index);
4622 dump_insn_slim (lra_dump_file, usage_insn);
4625 if (lra_dump_file != NULL)
4626 fprintf (lra_dump_file,
4627 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4628 return true;
4631 /* Return true if we need a caller save/restore for pseudo REGNO which
4632 was assigned to a hard register. */
4633 static inline bool
4634 need_for_call_save_p (int regno)
4636 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4637 return (usage_insns[regno].calls_num < calls_num
4638 && (overlaps_hard_reg_set_p
4639 ((flag_use_caller_save &&
4640 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4641 ? lra_reg_info[regno].actual_call_used_reg_set
4642 : call_used_reg_set,
4643 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4644 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4645 PSEUDO_REGNO_MODE (regno))));
4648 /* Global registers occurring in the current EBB. */
4649 static bitmap_head ebb_global_regs;
4651 /* Return true if we need a split for hard register REGNO or pseudo
4652 REGNO which was assigned to a hard register.
4653 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4654 used for reloads since the EBB end. It is an approximation of the
4655 used hard registers in the split range. The exact value would
4656 require expensive calculations. If we were aggressive with
4657 splitting because of the approximation, the split pseudo will save
4658 the same hard register assignment and will be removed in the undo
4659 pass. We still need the approximation because too aggressive
4660 splitting would result in too inaccurate cost calculation in the
4661 assignment pass because of too many generated moves which will be
4662 probably removed in the undo pass. */
4663 static inline bool
4664 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4666 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4668 lra_assert (hard_regno >= 0);
4669 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4670 /* Don't split eliminable hard registers, otherwise we can
4671 split hard registers like hard frame pointer, which
4672 lives on BB start/end according to DF-infrastructure,
4673 when there is a pseudo assigned to the register and
4674 living in the same BB. */
4675 && (regno >= FIRST_PSEUDO_REGISTER
4676 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4677 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4678 /* Don't split call clobbered hard regs living through
4679 calls, otherwise we might have a check problem in the
4680 assign sub-pass as in the most cases (exception is a
4681 situation when lra_risky_transformations_p value is
4682 true) the assign pass assumes that all pseudos living
4683 through calls are assigned to call saved hard regs. */
4684 && (regno >= FIRST_PSEUDO_REGISTER
4685 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4686 || usage_insns[regno].calls_num == calls_num)
4687 /* We need at least 2 reloads to make pseudo splitting
4688 profitable. We should provide hard regno splitting in
4689 any case to solve 1st insn scheduling problem when
4690 moving hard register definition up might result in
4691 impossibility to find hard register for reload pseudo of
4692 small register class. */
4693 && (usage_insns[regno].reloads_num
4694 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4695 && (regno < FIRST_PSEUDO_REGISTER
4696 /* For short living pseudos, spilling + inheritance can
4697 be considered a substitution for splitting.
4698 Therefore we do not splitting for local pseudos. It
4699 decreases also aggressiveness of splitting. The
4700 minimal number of references is chosen taking into
4701 account that for 2 references splitting has no sense
4702 as we can just spill the pseudo. */
4703 || (regno >= FIRST_PSEUDO_REGISTER
4704 && lra_reg_info[regno].nrefs > 3
4705 && bitmap_bit_p (&ebb_global_regs, regno))))
4706 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4709 /* Return class for the split pseudo created from original pseudo with
4710 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4711 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4712 results in no secondary memory movements. */
4713 static enum reg_class
4714 choose_split_class (enum reg_class allocno_class,
4715 int hard_regno ATTRIBUTE_UNUSED,
4716 enum machine_mode mode ATTRIBUTE_UNUSED)
4718 #ifndef SECONDARY_MEMORY_NEEDED
4719 return allocno_class;
4720 #else
4721 int i;
4722 enum reg_class cl, best_cl = NO_REGS;
4723 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4724 = REGNO_REG_CLASS (hard_regno);
4726 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4727 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4728 return allocno_class;
4729 for (i = 0;
4730 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4731 i++)
4732 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4733 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4734 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4735 && (best_cl == NO_REGS
4736 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4737 best_cl = cl;
4738 return best_cl;
4739 #endif
4742 /* Do split transformations for insn INSN, which defines or uses
4743 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4744 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4745 "insns" field of usage_insns.
4747 The transformations look like:
4749 p <- ... p <- ...
4750 ... s <- p (new insn -- save)
4751 ... =>
4752 ... p <- s (new insn -- restore)
4753 <- ... p ... <- ... p ...
4755 <- ... p ... <- ... p ...
4756 ... s <- p (new insn -- save)
4757 ... =>
4758 ... p <- s (new insn -- restore)
4759 <- ... p ... <- ... p ...
4761 where p is an original pseudo got a hard register or a hard
4762 register and s is a new split pseudo. The save is put before INSN
4763 if BEFORE_P is true. Return true if we succeed in such
4764 transformation. */
4765 static bool
4766 split_reg (bool before_p, int original_regno, rtx_insn *insn,
4767 rtx next_usage_insns)
4769 enum reg_class rclass;
4770 rtx original_reg;
4771 int hard_regno, nregs;
4772 rtx new_reg, usage_insn;
4773 rtx_insn *restore, *save;
4774 bool after_p;
4775 bool call_save_p;
4777 if (original_regno < FIRST_PSEUDO_REGISTER)
4779 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4780 hard_regno = original_regno;
4781 call_save_p = false;
4782 nregs = 1;
4784 else
4786 hard_regno = reg_renumber[original_regno];
4787 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4788 rclass = lra_get_allocno_class (original_regno);
4789 original_reg = regno_reg_rtx[original_regno];
4790 call_save_p = need_for_call_save_p (original_regno);
4792 original_reg = regno_reg_rtx[original_regno];
4793 lra_assert (hard_regno >= 0);
4794 if (lra_dump_file != NULL)
4795 fprintf (lra_dump_file,
4796 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4797 if (call_save_p)
4799 enum machine_mode mode = GET_MODE (original_reg);
4801 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4802 hard_regno_nregs[hard_regno][mode],
4803 mode);
4804 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4806 else
4808 rclass = choose_split_class (rclass, hard_regno,
4809 GET_MODE (original_reg));
4810 if (rclass == NO_REGS)
4812 if (lra_dump_file != NULL)
4814 fprintf (lra_dump_file,
4815 " Rejecting split of %d(%s): "
4816 "no good reg class for %d(%s)\n",
4817 original_regno,
4818 reg_class_names[lra_get_allocno_class (original_regno)],
4819 hard_regno,
4820 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4821 fprintf
4822 (lra_dump_file,
4823 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4825 return false;
4827 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4828 rclass, "split");
4829 reg_renumber[REGNO (new_reg)] = hard_regno;
4831 save = emit_spill_move (true, new_reg, original_reg);
4832 if (NEXT_INSN (save) != NULL_RTX)
4834 lra_assert (! call_save_p);
4835 if (lra_dump_file != NULL)
4837 fprintf
4838 (lra_dump_file,
4839 " Rejecting split %d->%d resulting in > 2 %s save insns:\n",
4840 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4841 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
4842 fprintf (lra_dump_file,
4843 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4845 return false;
4847 restore = emit_spill_move (false, new_reg, original_reg);
4848 if (NEXT_INSN (restore) != NULL_RTX)
4850 lra_assert (! call_save_p);
4851 if (lra_dump_file != NULL)
4853 fprintf (lra_dump_file,
4854 " Rejecting split %d->%d "
4855 "resulting in > 2 %s restore insns:\n",
4856 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4857 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
4858 fprintf (lra_dump_file,
4859 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4861 return false;
4863 after_p = usage_insns[original_regno].after_p;
4864 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4865 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4866 bitmap_set_bit (&check_only_regs, original_regno);
4867 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4868 for (;;)
4870 if (GET_CODE (next_usage_insns) != INSN_LIST)
4872 usage_insn = next_usage_insns;
4873 break;
4875 usage_insn = XEXP (next_usage_insns, 0);
4876 lra_assert (DEBUG_INSN_P (usage_insn));
4877 next_usage_insns = XEXP (next_usage_insns, 1);
4878 substitute_pseudo (&usage_insn, original_regno, new_reg);
4879 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4880 if (lra_dump_file != NULL)
4882 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
4883 original_regno, REGNO (new_reg));
4884 dump_insn_slim (lra_dump_file, usage_insn);
4887 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
4888 lra_assert (usage_insn != insn || (after_p && before_p));
4889 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
4890 after_p ? NULL : restore,
4891 after_p ? restore : NULL,
4892 call_save_p
4893 ? "Add reg<-save" : "Add reg<-split");
4894 lra_process_new_insns (insn, before_p ? save : NULL,
4895 before_p ? NULL : save,
4896 call_save_p
4897 ? "Add save<-reg" : "Add split<-reg");
4898 if (nregs > 1)
4899 /* If we are trying to split multi-register. We should check
4900 conflicts on the next assignment sub-pass. IRA can allocate on
4901 sub-register levels, LRA do this on pseudos level right now and
4902 this discrepancy may create allocation conflicts after
4903 splitting. */
4904 lra_risky_transformations_p = true;
4905 if (lra_dump_file != NULL)
4906 fprintf (lra_dump_file,
4907 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4908 return true;
4911 /* Recognize that we need a split transformation for insn INSN, which
4912 defines or uses REGNO in its insn biggest MODE (we use it only if
4913 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
4914 hard registers which might be used for reloads since the EBB end.
4915 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
4916 uid before starting INSN processing. Return true if we succeed in
4917 such transformation. */
4918 static bool
4919 split_if_necessary (int regno, enum machine_mode mode,
4920 HARD_REG_SET potential_reload_hard_regs,
4921 bool before_p, rtx_insn *insn, int max_uid)
4923 bool res = false;
4924 int i, nregs = 1;
4925 rtx next_usage_insns;
4927 if (regno < FIRST_PSEUDO_REGISTER)
4928 nregs = hard_regno_nregs[regno][mode];
4929 for (i = 0; i < nregs; i++)
4930 if (usage_insns[regno + i].check == curr_usage_insns_check
4931 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
4932 /* To avoid processing the register twice or more. */
4933 && ((GET_CODE (next_usage_insns) != INSN_LIST
4934 && INSN_UID (next_usage_insns) < max_uid)
4935 || (GET_CODE (next_usage_insns) == INSN_LIST
4936 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
4937 && need_for_split_p (potential_reload_hard_regs, regno + i)
4938 && split_reg (before_p, regno + i, insn, next_usage_insns))
4939 res = true;
4940 return res;
4943 /* Check only registers living at the current program point in the
4944 current EBB. */
4945 static bitmap_head live_regs;
4947 /* Update live info in EBB given by its HEAD and TAIL insns after
4948 inheritance/split transformation. The function removes dead moves
4949 too. */
4950 static void
4951 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
4953 unsigned int j;
4954 int i, regno;
4955 bool live_p;
4956 rtx_insn *prev_insn;
4957 rtx set;
4958 bool remove_p;
4959 basic_block last_bb, prev_bb, curr_bb;
4960 bitmap_iterator bi;
4961 struct lra_insn_reg *reg;
4962 edge e;
4963 edge_iterator ei;
4965 last_bb = BLOCK_FOR_INSN (tail);
4966 prev_bb = NULL;
4967 for (curr_insn = tail;
4968 curr_insn != PREV_INSN (head);
4969 curr_insn = prev_insn)
4971 prev_insn = PREV_INSN (curr_insn);
4972 /* We need to process empty blocks too. They contain
4973 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
4974 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
4975 continue;
4976 curr_bb = BLOCK_FOR_INSN (curr_insn);
4977 if (curr_bb != prev_bb)
4979 if (prev_bb != NULL)
4981 /* Update df_get_live_in (prev_bb): */
4982 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4983 if (bitmap_bit_p (&live_regs, j))
4984 bitmap_set_bit (df_get_live_in (prev_bb), j);
4985 else
4986 bitmap_clear_bit (df_get_live_in (prev_bb), j);
4988 if (curr_bb != last_bb)
4990 /* Update df_get_live_out (curr_bb): */
4991 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4993 live_p = bitmap_bit_p (&live_regs, j);
4994 if (! live_p)
4995 FOR_EACH_EDGE (e, ei, curr_bb->succs)
4996 if (bitmap_bit_p (df_get_live_in (e->dest), j))
4998 live_p = true;
4999 break;
5001 if (live_p)
5002 bitmap_set_bit (df_get_live_out (curr_bb), j);
5003 else
5004 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5007 prev_bb = curr_bb;
5008 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5010 if (! NONDEBUG_INSN_P (curr_insn))
5011 continue;
5012 curr_id = lra_get_insn_recog_data (curr_insn);
5013 curr_static_id = curr_id->insn_static_data;
5014 remove_p = false;
5015 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
5016 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5017 && bitmap_bit_p (&check_only_regs, regno)
5018 && ! bitmap_bit_p (&live_regs, regno))
5019 remove_p = true;
5020 /* See which defined values die here. */
5021 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5022 if (reg->type == OP_OUT && ! reg->subreg_p)
5023 bitmap_clear_bit (&live_regs, reg->regno);
5024 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5025 if (reg->type == OP_OUT && ! reg->subreg_p)
5026 bitmap_clear_bit (&live_regs, reg->regno);
5027 /* Mark each used value as live. */
5028 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5029 if (reg->type != OP_OUT
5030 && bitmap_bit_p (&check_only_regs, reg->regno))
5031 bitmap_set_bit (&live_regs, reg->regno);
5032 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5033 if (reg->type != OP_OUT
5034 && bitmap_bit_p (&check_only_regs, reg->regno))
5035 bitmap_set_bit (&live_regs, reg->regno);
5036 if (curr_id->arg_hard_regs != NULL)
5037 /* Make argument hard registers live. */
5038 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5039 if (bitmap_bit_p (&check_only_regs, regno))
5040 bitmap_set_bit (&live_regs, regno);
5041 /* It is quite important to remove dead move insns because it
5042 means removing dead store. We don't need to process them for
5043 constraints. */
5044 if (remove_p)
5046 if (lra_dump_file != NULL)
5048 fprintf (lra_dump_file, " Removing dead insn:\n ");
5049 dump_insn_slim (lra_dump_file, curr_insn);
5051 lra_set_insn_deleted (curr_insn);
5056 /* The structure describes info to do an inheritance for the current
5057 insn. We need to collect such info first before doing the
5058 transformations because the transformations change the insn
5059 internal representation. */
5060 struct to_inherit
5062 /* Original regno. */
5063 int regno;
5064 /* Subsequent insns which can inherit original reg value. */
5065 rtx insns;
5068 /* Array containing all info for doing inheritance from the current
5069 insn. */
5070 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5072 /* Number elements in the previous array. */
5073 static int to_inherit_num;
5075 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5076 structure to_inherit. */
5077 static void
5078 add_to_inherit (int regno, rtx insns)
5080 int i;
5082 for (i = 0; i < to_inherit_num; i++)
5083 if (to_inherit[i].regno == regno)
5084 return;
5085 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5086 to_inherit[to_inherit_num].regno = regno;
5087 to_inherit[to_inherit_num++].insns = insns;
5090 /* Return the last non-debug insn in basic block BB, or the block begin
5091 note if none. */
5092 static rtx_insn *
5093 get_last_insertion_point (basic_block bb)
5095 rtx_insn *insn;
5097 FOR_BB_INSNS_REVERSE (bb, insn)
5098 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5099 return insn;
5100 gcc_unreachable ();
5103 /* Set up RES by registers living on edges FROM except the edge (FROM,
5104 TO) or by registers set up in a jump insn in BB FROM. */
5105 static void
5106 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5108 rtx_insn *last;
5109 struct lra_insn_reg *reg;
5110 edge e;
5111 edge_iterator ei;
5113 lra_assert (to != NULL);
5114 bitmap_clear (res);
5115 FOR_EACH_EDGE (e, ei, from->succs)
5116 if (e->dest != to)
5117 bitmap_ior_into (res, df_get_live_in (e->dest));
5118 last = get_last_insertion_point (from);
5119 if (! JUMP_P (last))
5120 return;
5121 curr_id = lra_get_insn_recog_data (last);
5122 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5123 if (reg->type != OP_IN)
5124 bitmap_set_bit (res, reg->regno);
5127 /* Used as a temporary results of some bitmap calculations. */
5128 static bitmap_head temp_bitmap;
5130 /* We split for reloads of small class of hard regs. The following
5131 defines how many hard regs the class should have to be qualified as
5132 small. The code is mostly oriented to x86/x86-64 architecture
5133 where some insns need to use only specific register or pair of
5134 registers and these register can live in RTL explicitly, e.g. for
5135 parameter passing. */
5136 static const int max_small_class_regs_num = 2;
5138 /* Do inheritance/split transformations in EBB starting with HEAD and
5139 finishing on TAIL. We process EBB insns in the reverse order.
5140 Return true if we did any inheritance/split transformation in the
5141 EBB.
5143 We should avoid excessive splitting which results in worse code
5144 because of inaccurate cost calculations for spilling new split
5145 pseudos in such case. To achieve this we do splitting only if
5146 register pressure is high in given basic block and there are reload
5147 pseudos requiring hard registers. We could do more register
5148 pressure calculations at any given program point to avoid necessary
5149 splitting even more but it is to expensive and the current approach
5150 works well enough. */
5151 static bool
5152 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5154 int i, src_regno, dst_regno, nregs;
5155 bool change_p, succ_p, update_reloads_num_p;
5156 rtx_insn *prev_insn, *last_insn;
5157 rtx next_usage_insns, set;
5158 enum reg_class cl;
5159 struct lra_insn_reg *reg;
5160 basic_block last_processed_bb, curr_bb = NULL;
5161 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5162 bitmap to_process;
5163 unsigned int j;
5164 bitmap_iterator bi;
5165 bool head_p, after_p;
5167 change_p = false;
5168 curr_usage_insns_check++;
5169 reloads_num = calls_num = 0;
5170 bitmap_clear (&check_only_regs);
5171 last_processed_bb = NULL;
5172 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5173 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5174 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5175 /* We don't process new insns generated in the loop. */
5176 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5178 prev_insn = PREV_INSN (curr_insn);
5179 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5180 curr_bb = BLOCK_FOR_INSN (curr_insn);
5181 if (last_processed_bb != curr_bb)
5183 /* We are at the end of BB. Add qualified living
5184 pseudos for potential splitting. */
5185 to_process = df_get_live_out (curr_bb);
5186 if (last_processed_bb != NULL)
5188 /* We are somewhere in the middle of EBB. */
5189 get_live_on_other_edges (curr_bb, last_processed_bb,
5190 &temp_bitmap);
5191 to_process = &temp_bitmap;
5193 last_processed_bb = curr_bb;
5194 last_insn = get_last_insertion_point (curr_bb);
5195 after_p = (! JUMP_P (last_insn)
5196 && (! CALL_P (last_insn)
5197 || (find_reg_note (last_insn,
5198 REG_NORETURN, NULL_RTX) == NULL_RTX
5199 && ! SIBLING_CALL_P (last_insn))));
5200 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5201 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5203 if ((int) j >= lra_constraint_new_regno_start)
5204 break;
5205 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5207 if (j < FIRST_PSEUDO_REGISTER)
5208 SET_HARD_REG_BIT (live_hard_regs, j);
5209 else
5210 add_to_hard_reg_set (&live_hard_regs,
5211 PSEUDO_REGNO_MODE (j),
5212 reg_renumber[j]);
5213 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5217 src_regno = dst_regno = -1;
5218 if (NONDEBUG_INSN_P (curr_insn)
5219 && (set = single_set (curr_insn)) != NULL_RTX
5220 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5222 src_regno = REGNO (SET_SRC (set));
5223 dst_regno = REGNO (SET_DEST (set));
5225 update_reloads_num_p = true;
5226 if (src_regno < lra_constraint_new_regno_start
5227 && src_regno >= FIRST_PSEUDO_REGISTER
5228 && reg_renumber[src_regno] < 0
5229 && dst_regno >= lra_constraint_new_regno_start
5230 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5232 /* 'reload_pseudo <- original_pseudo'. */
5233 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5234 reloads_num++;
5235 update_reloads_num_p = false;
5236 succ_p = false;
5237 if (usage_insns[src_regno].check == curr_usage_insns_check
5238 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5239 succ_p = inherit_reload_reg (false, src_regno, cl,
5240 curr_insn, next_usage_insns);
5241 if (succ_p)
5242 change_p = true;
5243 else
5244 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5245 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5246 IOR_HARD_REG_SET (potential_reload_hard_regs,
5247 reg_class_contents[cl]);
5249 else if (src_regno >= lra_constraint_new_regno_start
5250 && dst_regno < lra_constraint_new_regno_start
5251 && dst_regno >= FIRST_PSEUDO_REGISTER
5252 && reg_renumber[dst_regno] < 0
5253 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5254 && usage_insns[dst_regno].check == curr_usage_insns_check
5255 && (next_usage_insns
5256 = usage_insns[dst_regno].insns) != NULL_RTX)
5258 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5259 reloads_num++;
5260 update_reloads_num_p = false;
5261 /* 'original_pseudo <- reload_pseudo'. */
5262 if (! JUMP_P (curr_insn)
5263 && inherit_reload_reg (true, dst_regno, cl,
5264 curr_insn, next_usage_insns))
5265 change_p = true;
5266 /* Invalidate. */
5267 usage_insns[dst_regno].check = 0;
5268 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5269 IOR_HARD_REG_SET (potential_reload_hard_regs,
5270 reg_class_contents[cl]);
5272 else if (INSN_P (curr_insn))
5274 int iter;
5275 int max_uid = get_max_uid ();
5277 curr_id = lra_get_insn_recog_data (curr_insn);
5278 curr_static_id = curr_id->insn_static_data;
5279 to_inherit_num = 0;
5280 /* Process insn definitions. */
5281 for (iter = 0; iter < 2; iter++)
5282 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5283 reg != NULL;
5284 reg = reg->next)
5285 if (reg->type != OP_IN
5286 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5288 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5289 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5290 && usage_insns[dst_regno].check == curr_usage_insns_check
5291 && (next_usage_insns
5292 = usage_insns[dst_regno].insns) != NULL_RTX)
5294 struct lra_insn_reg *r;
5296 for (r = curr_id->regs; r != NULL; r = r->next)
5297 if (r->type != OP_OUT && r->regno == dst_regno)
5298 break;
5299 /* Don't do inheritance if the pseudo is also
5300 used in the insn. */
5301 if (r == NULL)
5302 /* We can not do inheritance right now
5303 because the current insn reg info (chain
5304 regs) can change after that. */
5305 add_to_inherit (dst_regno, next_usage_insns);
5307 /* We can not process one reg twice here because of
5308 usage_insns invalidation. */
5309 if ((dst_regno < FIRST_PSEUDO_REGISTER
5310 || reg_renumber[dst_regno] >= 0)
5311 && ! reg->subreg_p && reg->type != OP_IN)
5313 HARD_REG_SET s;
5315 if (split_if_necessary (dst_regno, reg->biggest_mode,
5316 potential_reload_hard_regs,
5317 false, curr_insn, max_uid))
5318 change_p = true;
5319 CLEAR_HARD_REG_SET (s);
5320 if (dst_regno < FIRST_PSEUDO_REGISTER)
5321 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5322 else
5323 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5324 reg_renumber[dst_regno]);
5325 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5327 /* We should invalidate potential inheritance or
5328 splitting for the current insn usages to the next
5329 usage insns (see code below) as the output pseudo
5330 prevents this. */
5331 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5332 && reg_renumber[dst_regno] < 0)
5333 || (reg->type == OP_OUT && ! reg->subreg_p
5334 && (dst_regno < FIRST_PSEUDO_REGISTER
5335 || reg_renumber[dst_regno] >= 0)))
5337 /* Invalidate and mark definitions. */
5338 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5339 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5340 else
5342 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5343 for (i = 0; i < nregs; i++)
5344 usage_insns[dst_regno + i].check
5345 = -(int) INSN_UID (curr_insn);
5349 if (! JUMP_P (curr_insn))
5350 for (i = 0; i < to_inherit_num; i++)
5351 if (inherit_reload_reg (true, to_inherit[i].regno,
5352 ALL_REGS, curr_insn,
5353 to_inherit[i].insns))
5354 change_p = true;
5355 if (CALL_P (curr_insn))
5357 rtx cheap, pat, dest;
5358 rtx_insn *restore;
5359 int regno, hard_regno;
5361 calls_num++;
5362 if ((cheap = find_reg_note (curr_insn,
5363 REG_RETURNED, NULL_RTX)) != NULL_RTX
5364 && ((cheap = XEXP (cheap, 0)), true)
5365 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5366 && (hard_regno = reg_renumber[regno]) >= 0
5367 /* If there are pending saves/restores, the
5368 optimization is not worth. */
5369 && usage_insns[regno].calls_num == calls_num - 1
5370 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5372 /* Restore the pseudo from the call result as
5373 REG_RETURNED note says that the pseudo value is
5374 in the call result and the pseudo is an argument
5375 of the call. */
5376 pat = PATTERN (curr_insn);
5377 if (GET_CODE (pat) == PARALLEL)
5378 pat = XVECEXP (pat, 0, 0);
5379 dest = SET_DEST (pat);
5380 /* For multiple return values dest is PARALLEL.
5381 Currently we handle only single return value case. */
5382 if (REG_P (dest))
5384 start_sequence ();
5385 emit_move_insn (cheap, copy_rtx (dest));
5386 restore = get_insns ();
5387 end_sequence ();
5388 lra_process_new_insns (curr_insn, NULL, restore,
5389 "Inserting call parameter restore");
5390 /* We don't need to save/restore of the pseudo from
5391 this call. */
5392 usage_insns[regno].calls_num = calls_num;
5393 bitmap_set_bit (&check_only_regs, regno);
5397 to_inherit_num = 0;
5398 /* Process insn usages. */
5399 for (iter = 0; iter < 2; iter++)
5400 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5401 reg != NULL;
5402 reg = reg->next)
5403 if ((reg->type != OP_OUT
5404 || (reg->type == OP_OUT && reg->subreg_p))
5405 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5407 if (src_regno >= FIRST_PSEUDO_REGISTER
5408 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5410 if (usage_insns[src_regno].check == curr_usage_insns_check
5411 && (next_usage_insns
5412 = usage_insns[src_regno].insns) != NULL_RTX
5413 && NONDEBUG_INSN_P (curr_insn))
5414 add_to_inherit (src_regno, next_usage_insns);
5415 else if (usage_insns[src_regno].check
5416 != -(int) INSN_UID (curr_insn))
5417 /* Add usages but only if the reg is not set up
5418 in the same insn. */
5419 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5421 else if (src_regno < FIRST_PSEUDO_REGISTER
5422 || reg_renumber[src_regno] >= 0)
5424 bool before_p;
5425 rtx use_insn = curr_insn;
5427 before_p = (JUMP_P (curr_insn)
5428 || (CALL_P (curr_insn) && reg->type == OP_IN));
5429 if (NONDEBUG_INSN_P (curr_insn)
5430 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5431 && split_if_necessary (src_regno, reg->biggest_mode,
5432 potential_reload_hard_regs,
5433 before_p, curr_insn, max_uid))
5435 if (reg->subreg_p)
5436 lra_risky_transformations_p = true;
5437 change_p = true;
5438 /* Invalidate. */
5439 usage_insns[src_regno].check = 0;
5440 if (before_p)
5441 use_insn = PREV_INSN (curr_insn);
5443 if (NONDEBUG_INSN_P (curr_insn))
5445 if (src_regno < FIRST_PSEUDO_REGISTER)
5446 add_to_hard_reg_set (&live_hard_regs,
5447 reg->biggest_mode, src_regno);
5448 else
5449 add_to_hard_reg_set (&live_hard_regs,
5450 PSEUDO_REGNO_MODE (src_regno),
5451 reg_renumber[src_regno]);
5453 add_next_usage_insn (src_regno, use_insn, reloads_num);
5456 /* Process call args. */
5457 if (curr_id->arg_hard_regs != NULL)
5458 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5459 if (src_regno < FIRST_PSEUDO_REGISTER)
5461 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5462 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5464 for (i = 0; i < to_inherit_num; i++)
5466 src_regno = to_inherit[i].regno;
5467 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5468 curr_insn, to_inherit[i].insns))
5469 change_p = true;
5470 else
5471 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5474 if (update_reloads_num_p
5475 && NONDEBUG_INSN_P (curr_insn)
5476 && (set = single_set (curr_insn)) != NULL_RTX)
5478 int regno = -1;
5479 if ((REG_P (SET_DEST (set))
5480 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5481 && reg_renumber[regno] < 0
5482 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5483 || (REG_P (SET_SRC (set))
5484 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5485 && reg_renumber[regno] < 0
5486 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5488 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5489 reloads_num++;
5490 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5491 IOR_HARD_REG_SET (potential_reload_hard_regs,
5492 reg_class_contents[cl]);
5495 /* We reached the start of the current basic block. */
5496 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5497 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5499 /* We reached the beginning of the current block -- do
5500 rest of spliting in the current BB. */
5501 to_process = df_get_live_in (curr_bb);
5502 if (BLOCK_FOR_INSN (head) != curr_bb)
5504 /* We are somewhere in the middle of EBB. */
5505 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5506 curr_bb, &temp_bitmap);
5507 to_process = &temp_bitmap;
5509 head_p = true;
5510 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5512 if ((int) j >= lra_constraint_new_regno_start)
5513 break;
5514 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5515 && usage_insns[j].check == curr_usage_insns_check
5516 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5518 if (need_for_split_p (potential_reload_hard_regs, j))
5520 if (lra_dump_file != NULL && head_p)
5522 fprintf (lra_dump_file,
5523 " ----------------------------------\n");
5524 head_p = false;
5526 if (split_reg (false, j, bb_note (curr_bb),
5527 next_usage_insns))
5528 change_p = true;
5530 usage_insns[j].check = 0;
5535 return change_p;
5538 /* This value affects EBB forming. If probability of edge from EBB to
5539 a BB is not greater than the following value, we don't add the BB
5540 to EBB. */
5541 #define EBB_PROBABILITY_CUTOFF ((REG_BR_PROB_BASE * 50) / 100)
5543 /* Current number of inheritance/split iteration. */
5544 int lra_inheritance_iter;
5546 /* Entry function for inheritance/split pass. */
5547 void
5548 lra_inheritance (void)
5550 int i;
5551 basic_block bb, start_bb;
5552 edge e;
5554 lra_inheritance_iter++;
5555 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5556 return;
5557 timevar_push (TV_LRA_INHERITANCE);
5558 if (lra_dump_file != NULL)
5559 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5560 lra_inheritance_iter);
5561 curr_usage_insns_check = 0;
5562 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5563 for (i = 0; i < lra_constraint_new_regno_start; i++)
5564 usage_insns[i].check = 0;
5565 bitmap_initialize (&check_only_regs, &reg_obstack);
5566 bitmap_initialize (&live_regs, &reg_obstack);
5567 bitmap_initialize (&temp_bitmap, &reg_obstack);
5568 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5569 FOR_EACH_BB_FN (bb, cfun)
5571 start_bb = bb;
5572 if (lra_dump_file != NULL)
5573 fprintf (lra_dump_file, "EBB");
5574 /* Form a EBB starting with BB. */
5575 bitmap_clear (&ebb_global_regs);
5576 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5577 for (;;)
5579 if (lra_dump_file != NULL)
5580 fprintf (lra_dump_file, " %d", bb->index);
5581 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5582 || LABEL_P (BB_HEAD (bb->next_bb)))
5583 break;
5584 e = find_fallthru_edge (bb->succs);
5585 if (! e)
5586 break;
5587 if (e->probability <= EBB_PROBABILITY_CUTOFF)
5588 break;
5589 bb = bb->next_bb;
5591 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5592 if (lra_dump_file != NULL)
5593 fprintf (lra_dump_file, "\n");
5594 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5595 /* Remember that the EBB head and tail can change in
5596 inherit_in_ebb. */
5597 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5599 bitmap_clear (&ebb_global_regs);
5600 bitmap_clear (&temp_bitmap);
5601 bitmap_clear (&live_regs);
5602 bitmap_clear (&check_only_regs);
5603 free (usage_insns);
5605 timevar_pop (TV_LRA_INHERITANCE);
5610 /* This page contains code to undo failed inheritance/split
5611 transformations. */
5613 /* Current number of iteration undoing inheritance/split. */
5614 int lra_undo_inheritance_iter;
5616 /* Fix BB live info LIVE after removing pseudos created on pass doing
5617 inheritance/split which are REMOVED_PSEUDOS. */
5618 static void
5619 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5621 unsigned int regno;
5622 bitmap_iterator bi;
5624 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5625 if (bitmap_clear_bit (live, regno))
5626 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5629 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5630 number. */
5631 static int
5632 get_regno (rtx reg)
5634 if (GET_CODE (reg) == SUBREG)
5635 reg = SUBREG_REG (reg);
5636 if (REG_P (reg))
5637 return REGNO (reg);
5638 return -1;
5641 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5642 return true if we did any change. The undo transformations for
5643 inheritance looks like
5644 i <- i2
5645 p <- i => p <- i2
5646 or removing
5647 p <- i, i <- p, and i <- i3
5648 where p is original pseudo from which inheritance pseudo i was
5649 created, i and i3 are removed inheritance pseudos, i2 is another
5650 not removed inheritance pseudo. All split pseudos or other
5651 occurrences of removed inheritance pseudos are changed on the
5652 corresponding original pseudos.
5654 The function also schedules insns changed and created during
5655 inheritance/split pass for processing by the subsequent constraint
5656 pass. */
5657 static bool
5658 remove_inheritance_pseudos (bitmap remove_pseudos)
5660 basic_block bb;
5661 int regno, sregno, prev_sregno, dregno, restore_regno;
5662 rtx set, prev_set;
5663 rtx_insn *prev_insn;
5664 bool change_p, done_p;
5666 change_p = ! bitmap_empty_p (remove_pseudos);
5667 /* We can not finish the function right away if CHANGE_P is true
5668 because we need to marks insns affected by previous
5669 inheritance/split pass for processing by the subsequent
5670 constraint pass. */
5671 FOR_EACH_BB_FN (bb, cfun)
5673 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5674 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5675 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5677 if (! INSN_P (curr_insn))
5678 continue;
5679 done_p = false;
5680 sregno = dregno = -1;
5681 if (change_p && NONDEBUG_INSN_P (curr_insn)
5682 && (set = single_set (curr_insn)) != NULL_RTX)
5684 dregno = get_regno (SET_DEST (set));
5685 sregno = get_regno (SET_SRC (set));
5688 if (sregno >= 0 && dregno >= 0)
5690 if ((bitmap_bit_p (remove_pseudos, sregno)
5691 && (lra_reg_info[sregno].restore_regno == dregno
5692 || (bitmap_bit_p (remove_pseudos, dregno)
5693 && (lra_reg_info[sregno].restore_regno
5694 == lra_reg_info[dregno].restore_regno))))
5695 || (bitmap_bit_p (remove_pseudos, dregno)
5696 && lra_reg_info[dregno].restore_regno == sregno))
5697 /* One of the following cases:
5698 original <- removed inheritance pseudo
5699 removed inherit pseudo <- another removed inherit pseudo
5700 removed inherit pseudo <- original pseudo
5702 removed_split_pseudo <- original_reg
5703 original_reg <- removed_split_pseudo */
5705 if (lra_dump_file != NULL)
5707 fprintf (lra_dump_file, " Removing %s:\n",
5708 bitmap_bit_p (&lra_split_regs, sregno)
5709 || bitmap_bit_p (&lra_split_regs, dregno)
5710 ? "split" : "inheritance");
5711 dump_insn_slim (lra_dump_file, curr_insn);
5713 lra_set_insn_deleted (curr_insn);
5714 done_p = true;
5716 else if (bitmap_bit_p (remove_pseudos, sregno)
5717 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5719 /* Search the following pattern:
5720 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5721 original_pseudo <- inherit_or_split_pseudo1
5722 where the 2nd insn is the current insn and
5723 inherit_or_split_pseudo2 is not removed. If it is found,
5724 change the current insn onto:
5725 original_pseudo <- inherit_or_split_pseudo2. */
5726 for (prev_insn = PREV_INSN (curr_insn);
5727 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5728 prev_insn = PREV_INSN (prev_insn))
5730 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5731 && (prev_set = single_set (prev_insn)) != NULL_RTX
5732 /* There should be no subregs in insn we are
5733 searching because only the original reg might
5734 be in subreg when we changed the mode of
5735 load/store for splitting. */
5736 && REG_P (SET_DEST (prev_set))
5737 && REG_P (SET_SRC (prev_set))
5738 && (int) REGNO (SET_DEST (prev_set)) == sregno
5739 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5740 >= FIRST_PSEUDO_REGISTER)
5741 /* As we consider chain of inheritance or
5742 splitting described in above comment we should
5743 check that sregno and prev_sregno were
5744 inheritance/split pseudos created from the
5745 same original regno. */
5746 && (lra_reg_info[sregno].restore_regno
5747 == lra_reg_info[prev_sregno].restore_regno)
5748 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5750 lra_assert (GET_MODE (SET_SRC (prev_set))
5751 == GET_MODE (regno_reg_rtx[sregno]));
5752 if (GET_CODE (SET_SRC (set)) == SUBREG)
5753 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5754 else
5755 SET_SRC (set) = SET_SRC (prev_set);
5756 /* As we are finishing with processing the insn
5757 here, check the destination too as it might
5758 inheritance pseudo for another pseudo. */
5759 if (bitmap_bit_p (remove_pseudos, dregno)
5760 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
5761 && (restore_regno
5762 = lra_reg_info[dregno].restore_regno) >= 0)
5764 if (GET_CODE (SET_DEST (set)) == SUBREG)
5765 SUBREG_REG (SET_DEST (set))
5766 = regno_reg_rtx[restore_regno];
5767 else
5768 SET_DEST (set) = regno_reg_rtx[restore_regno];
5770 lra_push_insn_and_update_insn_regno_info (curr_insn);
5771 lra_set_used_insn_alternative_by_uid
5772 (INSN_UID (curr_insn), -1);
5773 done_p = true;
5774 if (lra_dump_file != NULL)
5776 fprintf (lra_dump_file, " Change reload insn:\n");
5777 dump_insn_slim (lra_dump_file, curr_insn);
5782 if (! done_p)
5784 struct lra_insn_reg *reg;
5785 bool restored_regs_p = false;
5786 bool kept_regs_p = false;
5788 curr_id = lra_get_insn_recog_data (curr_insn);
5789 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5791 regno = reg->regno;
5792 restore_regno = lra_reg_info[regno].restore_regno;
5793 if (restore_regno >= 0)
5795 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5797 substitute_pseudo_within_insn (
5798 curr_insn, regno, regno_reg_rtx[restore_regno]);
5799 restored_regs_p = true;
5801 else
5802 kept_regs_p = true;
5805 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5807 /* The instruction has changed since the previous
5808 constraints pass. */
5809 lra_push_insn_and_update_insn_regno_info (curr_insn);
5810 lra_set_used_insn_alternative_by_uid
5811 (INSN_UID (curr_insn), -1);
5813 else if (restored_regs_p)
5814 /* The instruction has been restored to the form that
5815 it had during the previous constraints pass. */
5816 lra_update_insn_regno_info (curr_insn);
5817 if (restored_regs_p && lra_dump_file != NULL)
5819 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5820 dump_insn_slim (lra_dump_file, curr_insn);
5825 return change_p;
5828 /* If optional reload pseudos failed to get a hard register or was not
5829 inherited, it is better to remove optional reloads. We do this
5830 transformation after undoing inheritance to figure out necessity to
5831 remove optional reloads easier. Return true if we do any
5832 change. */
5833 static bool
5834 undo_optional_reloads (void)
5836 bool change_p, keep_p;
5837 unsigned int regno, uid;
5838 bitmap_iterator bi, bi2;
5839 rtx_insn *insn;
5840 rtx set, src, dest;
5841 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5843 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5844 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5845 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5847 keep_p = false;
5848 /* Keep optional reloads from previous subpasses. */
5849 if (lra_reg_info[regno].restore_regno < 0
5850 /* If the original pseudo changed its allocation, just
5851 removing the optional pseudo is dangerous as the original
5852 pseudo will have longer live range. */
5853 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
5854 keep_p = true;
5855 else if (reg_renumber[regno] >= 0)
5856 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
5858 insn = lra_insn_recog_data[uid]->insn;
5859 if ((set = single_set (insn)) == NULL_RTX)
5860 continue;
5861 src = SET_SRC (set);
5862 dest = SET_DEST (set);
5863 if (! REG_P (src) || ! REG_P (dest))
5864 continue;
5865 if (REGNO (dest) == regno
5866 /* Ignore insn for optional reloads itself. */
5867 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
5868 /* Check only inheritance on last inheritance pass. */
5869 && (int) REGNO (src) >= new_regno_start
5870 /* Check that the optional reload was inherited. */
5871 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
5873 keep_p = true;
5874 break;
5877 if (keep_p)
5879 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
5880 if (lra_dump_file != NULL)
5881 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
5884 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
5885 bitmap_initialize (&insn_bitmap, &reg_obstack);
5886 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
5888 if (lra_dump_file != NULL)
5889 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
5890 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
5891 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
5893 insn = lra_insn_recog_data[uid]->insn;
5894 if ((set = single_set (insn)) != NULL_RTX)
5896 src = SET_SRC (set);
5897 dest = SET_DEST (set);
5898 if (REG_P (src) && REG_P (dest)
5899 && ((REGNO (src) == regno
5900 && (lra_reg_info[regno].restore_regno
5901 == (int) REGNO (dest)))
5902 || (REGNO (dest) == regno
5903 && (lra_reg_info[regno].restore_regno
5904 == (int) REGNO (src)))))
5906 if (lra_dump_file != NULL)
5908 fprintf (lra_dump_file, " Deleting move %u\n",
5909 INSN_UID (insn));
5910 dump_insn_slim (lra_dump_file, insn);
5912 lra_set_insn_deleted (insn);
5913 continue;
5915 /* We should not worry about generation memory-memory
5916 moves here as if the corresponding inheritance did
5917 not work (inheritance pseudo did not get a hard reg),
5918 we remove the inheritance pseudo and the optional
5919 reload. */
5921 substitute_pseudo_within_insn (
5922 insn, regno,
5923 regno_reg_rtx[lra_reg_info[regno].restore_regno]);
5924 lra_update_insn_regno_info (insn);
5925 if (lra_dump_file != NULL)
5927 fprintf (lra_dump_file,
5928 " Restoring original insn:\n");
5929 dump_insn_slim (lra_dump_file, insn);
5933 /* Clear restore_regnos. */
5934 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5935 lra_reg_info[regno].restore_regno = -1;
5936 bitmap_clear (&insn_bitmap);
5937 bitmap_clear (&removed_optional_reload_pseudos);
5938 return change_p;
5941 /* Entry function for undoing inheritance/split transformation. Return true
5942 if we did any RTL change in this pass. */
5943 bool
5944 lra_undo_inheritance (void)
5946 unsigned int regno;
5947 int restore_regno, hard_regno;
5948 int n_all_inherit, n_inherit, n_all_split, n_split;
5949 bitmap_head remove_pseudos;
5950 bitmap_iterator bi;
5951 bool change_p;
5953 lra_undo_inheritance_iter++;
5954 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5955 return false;
5956 if (lra_dump_file != NULL)
5957 fprintf (lra_dump_file,
5958 "\n********** Undoing inheritance #%d: **********\n\n",
5959 lra_undo_inheritance_iter);
5960 bitmap_initialize (&remove_pseudos, &reg_obstack);
5961 n_inherit = n_all_inherit = 0;
5962 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5963 if (lra_reg_info[regno].restore_regno >= 0)
5965 n_all_inherit++;
5966 if (reg_renumber[regno] < 0
5967 /* If the original pseudo changed its allocation, just
5968 removing inheritance is dangerous as for changing
5969 allocation we used shorter live-ranges. */
5970 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
5971 bitmap_set_bit (&remove_pseudos, regno);
5972 else
5973 n_inherit++;
5975 if (lra_dump_file != NULL && n_all_inherit != 0)
5976 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
5977 n_inherit, n_all_inherit,
5978 (double) n_inherit / n_all_inherit * 100);
5979 n_split = n_all_split = 0;
5980 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5981 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
5983 n_all_split++;
5984 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
5985 ? reg_renumber[restore_regno] : restore_regno);
5986 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
5987 bitmap_set_bit (&remove_pseudos, regno);
5988 else
5990 n_split++;
5991 if (lra_dump_file != NULL)
5992 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
5993 regno, restore_regno);
5996 if (lra_dump_file != NULL && n_all_split != 0)
5997 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
5998 n_split, n_all_split,
5999 (double) n_split / n_all_split * 100);
6000 change_p = remove_inheritance_pseudos (&remove_pseudos);
6001 bitmap_clear (&remove_pseudos);
6002 /* Clear restore_regnos. */
6003 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6004 lra_reg_info[regno].restore_regno = -1;
6005 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6006 lra_reg_info[regno].restore_regno = -1;
6007 change_p = undo_optional_reloads () || change_p;
6008 return change_p;