Update gimple.texi class hierarchy diagram
[official-gcc.git] / gcc / lra-constraints.c
blobf90d13be0a38539acd763e0047779f93b36ab7ed
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 /* The current iteration number of this LRA pass after the last spill
3899 pass. */
3900 int lra_constraint_iter_after_spill;
3902 /* True if we substituted equiv which needs checking register
3903 allocation correctness because the equivalent value contains
3904 allocatable hard registers or when we restore multi-register
3905 pseudo. */
3906 bool lra_risky_transformations_p;
3908 /* Return true if REGNO is referenced in more than one block. */
3909 static bool
3910 multi_block_pseudo_p (int regno)
3912 basic_block bb = NULL;
3913 unsigned int uid;
3914 bitmap_iterator bi;
3916 if (regno < FIRST_PSEUDO_REGISTER)
3917 return false;
3919 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
3920 if (bb == NULL)
3921 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
3922 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
3923 return true;
3924 return false;
3927 /* Return true if LIST contains a deleted insn. */
3928 static bool
3929 contains_deleted_insn_p (rtx_insn_list *list)
3931 for (; list != NULL_RTX; list = list->next ())
3932 if (NOTE_P (list->insn ())
3933 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
3934 return true;
3935 return false;
3938 /* Return true if X contains a pseudo dying in INSN. */
3939 static bool
3940 dead_pseudo_p (rtx x, rtx insn)
3942 int i, j;
3943 const char *fmt;
3944 enum rtx_code code;
3946 if (REG_P (x))
3947 return (insn != NULL_RTX
3948 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
3949 code = GET_CODE (x);
3950 fmt = GET_RTX_FORMAT (code);
3951 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3953 if (fmt[i] == 'e')
3955 if (dead_pseudo_p (XEXP (x, i), insn))
3956 return true;
3958 else if (fmt[i] == 'E')
3960 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3961 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
3962 return true;
3965 return false;
3968 /* Return true if INSN contains a dying pseudo in INSN right hand
3969 side. */
3970 static bool
3971 insn_rhs_dead_pseudo_p (rtx_insn *insn)
3973 rtx set = single_set (insn);
3975 gcc_assert (set != NULL);
3976 return dead_pseudo_p (SET_SRC (set), insn);
3979 /* Return true if any init insn of REGNO contains a dying pseudo in
3980 insn right hand side. */
3981 static bool
3982 init_insn_rhs_dead_pseudo_p (int regno)
3984 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
3986 if (insns == NULL)
3987 return false;
3988 for (; insns != NULL_RTX; insns = insns->next ())
3989 if (insn_rhs_dead_pseudo_p (insns->insn ()))
3990 return true;
3991 return false;
3994 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
3995 reverse only if we have one init insn with given REGNO as a
3996 source. */
3997 static bool
3998 reverse_equiv_p (int regno)
4000 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4001 rtx set;
4003 if (insns == NULL)
4004 return false;
4005 if (! INSN_P (insns->insn ())
4006 || insns->next () != NULL)
4007 return false;
4008 if ((set = single_set (insns->insn ())) == NULL_RTX)
4009 return false;
4010 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4013 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4014 call this function only for non-reverse equivalence. */
4015 static bool
4016 contains_reloaded_insn_p (int regno)
4018 rtx set;
4019 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4021 for (; list != NULL; list = list->next ())
4022 if ((set = single_set (list->insn ())) == NULL_RTX
4023 || ! REG_P (SET_DEST (set))
4024 || (int) REGNO (SET_DEST (set)) != regno)
4025 return true;
4026 return false;
4029 /* Entry function of LRA constraint pass. Return true if the
4030 constraint pass did change the code. */
4031 bool
4032 lra_constraints (bool first_p)
4034 bool changed_p;
4035 int i, hard_regno, new_insns_num;
4036 unsigned int min_len, new_min_len, uid;
4037 rtx set, x, reg, dest_reg;
4038 basic_block last_bb;
4039 bitmap_head equiv_insn_bitmap;
4040 bitmap_iterator bi;
4042 lra_constraint_iter++;
4043 if (lra_dump_file != NULL)
4044 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4045 lra_constraint_iter);
4046 lra_constraint_iter_after_spill++;
4047 if (lra_constraint_iter_after_spill > LRA_MAX_CONSTRAINT_ITERATION_NUMBER)
4048 internal_error
4049 ("Maximum number of LRA constraint passes is achieved (%d)\n",
4050 LRA_MAX_CONSTRAINT_ITERATION_NUMBER);
4051 changed_p = false;
4052 if (pic_offset_table_rtx
4053 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4054 lra_risky_transformations_p = true;
4055 else
4056 lra_risky_transformations_p = false;
4057 new_insn_uid_start = get_max_uid ();
4058 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4059 /* Mark used hard regs for target stack size calulations. */
4060 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4061 if (lra_reg_info[i].nrefs != 0
4062 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4064 int j, nregs;
4066 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4067 for (j = 0; j < nregs; j++)
4068 df_set_regs_ever_live (hard_regno + j, true);
4070 /* Do elimination before the equivalence processing as we can spill
4071 some pseudos during elimination. */
4072 lra_eliminate (false, first_p);
4073 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4074 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4075 if (lra_reg_info[i].nrefs != 0)
4077 ira_reg_equiv[i].profitable_p = true;
4078 reg = regno_reg_rtx[i];
4079 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4081 bool pseudo_p = contains_reg_p (x, false, false);
4083 /* After RTL transformation, we can not guarantee that
4084 pseudo in the substitution was not reloaded which might
4085 make equivalence invalid. For example, in reverse
4086 equiv of p0
4088 p0 <- ...
4090 equiv_mem <- p0
4092 the memory address register was reloaded before the 2nd
4093 insn. */
4094 if ((! first_p && pseudo_p)
4095 /* We don't use DF for compilation speed sake. So it
4096 is problematic to update live info when we use an
4097 equivalence containing pseudos in more than one
4098 BB. */
4099 || (pseudo_p && multi_block_pseudo_p (i))
4100 /* If an init insn was deleted for some reason, cancel
4101 the equiv. We could update the equiv insns after
4102 transformations including an equiv insn deletion
4103 but it is not worthy as such cases are extremely
4104 rare. */
4105 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4106 /* If it is not a reverse equivalence, we check that a
4107 pseudo in rhs of the init insn is not dying in the
4108 insn. Otherwise, the live info at the beginning of
4109 the corresponding BB might be wrong after we
4110 removed the insn. When the equiv can be a
4111 constant, the right hand side of the init insn can
4112 be a pseudo. */
4113 || (! reverse_equiv_p (i)
4114 && (init_insn_rhs_dead_pseudo_p (i)
4115 /* If we reloaded the pseudo in an equivalence
4116 init insn, we can not remove the equiv init
4117 insns and the init insns might write into
4118 const memory in this case. */
4119 || contains_reloaded_insn_p (i)))
4120 /* Prevent access beyond equivalent memory for
4121 paradoxical subregs. */
4122 || (MEM_P (x)
4123 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4124 > GET_MODE_SIZE (GET_MODE (x))))
4125 || (pic_offset_table_rtx
4126 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4127 && (targetm.preferred_reload_class
4128 (x, lra_get_allocno_class (i)) == NO_REGS))
4129 || contains_symbol_ref_p (x))))
4130 ira_reg_equiv[i].defined_p = false;
4131 if (contains_reg_p (x, false, true))
4132 ira_reg_equiv[i].profitable_p = false;
4133 if (get_equiv (reg) != reg)
4134 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4137 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4138 update_equiv (i);
4139 /* We should add all insns containing pseudos which should be
4140 substituted by their equivalences. */
4141 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4142 lra_push_insn_by_uid (uid);
4143 min_len = lra_insn_stack_length ();
4144 new_insns_num = 0;
4145 last_bb = NULL;
4146 changed_p = false;
4147 while ((new_min_len = lra_insn_stack_length ()) != 0)
4149 curr_insn = lra_pop_insn ();
4150 --new_min_len;
4151 curr_bb = BLOCK_FOR_INSN (curr_insn);
4152 if (curr_bb != last_bb)
4154 last_bb = curr_bb;
4155 bb_reload_num = lra_curr_reload_num;
4157 if (min_len > new_min_len)
4159 min_len = new_min_len;
4160 new_insns_num = 0;
4162 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4163 internal_error
4164 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4165 MAX_RELOAD_INSNS_NUMBER);
4166 new_insns_num++;
4167 if (DEBUG_INSN_P (curr_insn))
4169 /* We need to check equivalence in debug insn and change
4170 pseudo to the equivalent value if necessary. */
4171 curr_id = lra_get_insn_recog_data (curr_insn);
4172 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4174 rtx old = *curr_id->operand_loc[0];
4175 *curr_id->operand_loc[0]
4176 = simplify_replace_fn_rtx (old, NULL_RTX,
4177 loc_equivalence_callback, curr_insn);
4178 if (old != *curr_id->operand_loc[0])
4180 lra_update_insn_regno_info (curr_insn);
4181 changed_p = true;
4185 else if (INSN_P (curr_insn))
4187 if ((set = single_set (curr_insn)) != NULL_RTX)
4189 dest_reg = SET_DEST (set);
4190 /* The equivalence pseudo could be set up as SUBREG in a
4191 case when it is a call restore insn in a mode
4192 different from the pseudo mode. */
4193 if (GET_CODE (dest_reg) == SUBREG)
4194 dest_reg = SUBREG_REG (dest_reg);
4195 if ((REG_P (dest_reg)
4196 && (x = get_equiv (dest_reg)) != dest_reg
4197 /* Remove insns which set up a pseudo whose value
4198 can not be changed. Such insns might be not in
4199 init_insns because we don't update equiv data
4200 during insn transformations.
4202 As an example, let suppose that a pseudo got
4203 hard register and on the 1st pass was not
4204 changed to equivalent constant. We generate an
4205 additional insn setting up the pseudo because of
4206 secondary memory movement. Then the pseudo is
4207 spilled and we use the equiv constant. In this
4208 case we should remove the additional insn and
4209 this insn is not init_insns list. */
4210 && (! MEM_P (x) || MEM_READONLY_P (x)
4211 /* Check that this is actually an insn setting
4212 up the equivalence. */
4213 || in_list_p (curr_insn,
4214 ira_reg_equiv
4215 [REGNO (dest_reg)].init_insns)))
4216 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4217 && in_list_p (curr_insn,
4218 ira_reg_equiv
4219 [REGNO (SET_SRC (set))].init_insns)))
4221 /* This is equiv init insn of pseudo which did not get a
4222 hard register -- remove the insn. */
4223 if (lra_dump_file != NULL)
4225 fprintf (lra_dump_file,
4226 " Removing equiv init insn %i (freq=%d)\n",
4227 INSN_UID (curr_insn),
4228 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4229 dump_insn_slim (lra_dump_file, curr_insn);
4231 if (contains_reg_p (x, true, false))
4232 lra_risky_transformations_p = true;
4233 lra_set_insn_deleted (curr_insn);
4234 continue;
4237 curr_id = lra_get_insn_recog_data (curr_insn);
4238 curr_static_id = curr_id->insn_static_data;
4239 init_curr_insn_input_reloads ();
4240 init_curr_operand_mode ();
4241 if (curr_insn_transform ())
4242 changed_p = true;
4243 /* Check non-transformed insns too for equiv change as USE
4244 or CLOBBER don't need reloads but can contain pseudos
4245 being changed on their equivalences. */
4246 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4247 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4249 lra_update_insn_regno_info (curr_insn);
4250 changed_p = true;
4254 bitmap_clear (&equiv_insn_bitmap);
4255 /* If we used a new hard regno, changed_p should be true because the
4256 hard reg is assigned to a new pseudo. */
4257 #ifdef ENABLE_CHECKING
4258 if (! changed_p)
4260 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4261 if (lra_reg_info[i].nrefs != 0
4262 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4264 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4266 for (j = 0; j < nregs; j++)
4267 lra_assert (df_regs_ever_live_p (hard_regno + j));
4270 #endif
4271 return changed_p;
4274 /* Initiate the LRA constraint pass. It is done once per
4275 function. */
4276 void
4277 lra_constraints_init (void)
4281 /* Finalize the LRA constraint pass. It is done once per
4282 function. */
4283 void
4284 lra_constraints_finish (void)
4290 /* This page contains code to do inheritance/split
4291 transformations. */
4293 /* Number of reloads passed so far in current EBB. */
4294 static int reloads_num;
4296 /* Number of calls passed so far in current EBB. */
4297 static int calls_num;
4299 /* Current reload pseudo check for validity of elements in
4300 USAGE_INSNS. */
4301 static int curr_usage_insns_check;
4303 /* Info about last usage of registers in EBB to do inheritance/split
4304 transformation. Inheritance transformation is done from a spilled
4305 pseudo and split transformations from a hard register or a pseudo
4306 assigned to a hard register. */
4307 struct usage_insns
4309 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4310 value INSNS is valid. The insns is chain of optional debug insns
4311 and a finishing non-debug insn using the corresponding reg. The
4312 value is also used to mark the registers which are set up in the
4313 current insn. The negated insn uid is used for this. */
4314 int check;
4315 /* Value of global reloads_num at the last insn in INSNS. */
4316 int reloads_num;
4317 /* Value of global reloads_nums at the last insn in INSNS. */
4318 int calls_num;
4319 /* It can be true only for splitting. And it means that the restore
4320 insn should be put after insn given by the following member. */
4321 bool after_p;
4322 /* Next insns in the current EBB which use the original reg and the
4323 original reg value is not changed between the current insn and
4324 the next insns. In order words, e.g. for inheritance, if we need
4325 to use the original reg value again in the next insns we can try
4326 to use the value in a hard register from a reload insn of the
4327 current insn. */
4328 rtx insns;
4331 /* Map: regno -> corresponding pseudo usage insns. */
4332 static struct usage_insns *usage_insns;
4334 static void
4335 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4337 usage_insns[regno].check = curr_usage_insns_check;
4338 usage_insns[regno].insns = insn;
4339 usage_insns[regno].reloads_num = reloads_num;
4340 usage_insns[regno].calls_num = calls_num;
4341 usage_insns[regno].after_p = after_p;
4344 /* The function is used to form list REGNO usages which consists of
4345 optional debug insns finished by a non-debug insn using REGNO.
4346 RELOADS_NUM is current number of reload insns processed so far. */
4347 static void
4348 add_next_usage_insn (int regno, rtx insn, int reloads_num)
4350 rtx next_usage_insns;
4352 if (usage_insns[regno].check == curr_usage_insns_check
4353 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4354 && DEBUG_INSN_P (insn))
4356 /* Check that we did not add the debug insn yet. */
4357 if (next_usage_insns != insn
4358 && (GET_CODE (next_usage_insns) != INSN_LIST
4359 || XEXP (next_usage_insns, 0) != insn))
4360 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4361 next_usage_insns);
4363 else if (NONDEBUG_INSN_P (insn))
4364 setup_next_usage_insn (regno, insn, reloads_num, false);
4365 else
4366 usage_insns[regno].check = 0;
4369 /* Replace all references to register OLD_REGNO in *LOC with pseudo
4370 register NEW_REG. Return true if any change was made. */
4371 static bool
4372 substitute_pseudo (rtx *loc, int old_regno, rtx new_reg)
4374 rtx x = *loc;
4375 bool result = false;
4376 enum rtx_code code;
4377 const char *fmt;
4378 int i, j;
4380 if (x == NULL_RTX)
4381 return false;
4383 code = GET_CODE (x);
4384 if (code == REG && (int) REGNO (x) == old_regno)
4386 enum machine_mode mode = GET_MODE (*loc);
4387 enum machine_mode inner_mode = GET_MODE (new_reg);
4389 if (mode != inner_mode)
4391 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
4392 || ! SCALAR_INT_MODE_P (inner_mode))
4393 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
4394 else
4395 new_reg = gen_lowpart_SUBREG (mode, new_reg);
4397 *loc = new_reg;
4398 return true;
4401 /* Scan all the operand sub-expressions. */
4402 fmt = GET_RTX_FORMAT (code);
4403 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4405 if (fmt[i] == 'e')
4407 if (substitute_pseudo (&XEXP (x, i), old_regno, new_reg))
4408 result = true;
4410 else if (fmt[i] == 'E')
4412 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4413 if (substitute_pseudo (&XVECEXP (x, i, j), old_regno, new_reg))
4414 result = true;
4417 return result;
4420 /* Call substitute_pseudo within an insn. This won't update the insn ptr,
4421 just the contents of the insn. */
4423 static bool
4424 substitute_pseudo_within_insn (rtx_insn *insn, int old_regno, rtx new_reg)
4426 rtx loc = insn;
4427 return substitute_pseudo (&loc, old_regno, new_reg);
4430 /* Return first non-debug insn in list USAGE_INSNS. */
4431 static rtx_insn *
4432 skip_usage_debug_insns (rtx usage_insns)
4434 rtx insn;
4436 /* Skip debug insns. */
4437 for (insn = usage_insns;
4438 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4439 insn = XEXP (insn, 1))
4441 return safe_as_a <rtx_insn *> (insn);
4444 /* Return true if we need secondary memory moves for insn in
4445 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4446 into the insn. */
4447 static bool
4448 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4449 rtx usage_insns ATTRIBUTE_UNUSED)
4451 #ifndef SECONDARY_MEMORY_NEEDED
4452 return false;
4453 #else
4454 rtx_insn *insn;
4455 rtx set, dest;
4456 enum reg_class cl;
4458 if (inher_cl == ALL_REGS
4459 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4460 return false;
4461 lra_assert (INSN_P (insn));
4462 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4463 return false;
4464 dest = SET_DEST (set);
4465 if (! REG_P (dest))
4466 return false;
4467 lra_assert (inher_cl != NO_REGS);
4468 cl = get_reg_class (REGNO (dest));
4469 return (cl != NO_REGS && cl != ALL_REGS
4470 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4471 #endif
4474 /* Registers involved in inheritance/split in the current EBB
4475 (inheritance/split pseudos and original registers). */
4476 static bitmap_head check_only_regs;
4478 /* Do inheritance transformations for insn INSN, which defines (if
4479 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4480 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4481 form as the "insns" field of usage_insns. Return true if we
4482 succeed in such transformation.
4484 The transformations look like:
4486 p <- ... i <- ...
4487 ... p <- i (new insn)
4488 ... =>
4489 <- ... p ... <- ... i ...
4491 ... i <- p (new insn)
4492 <- ... p ... <- ... i ...
4493 ... =>
4494 <- ... p ... <- ... i ...
4495 where p is a spilled original pseudo and i is a new inheritance pseudo.
4498 The inheritance pseudo has the smallest class of two classes CL and
4499 class of ORIGINAL REGNO. */
4500 static bool
4501 inherit_reload_reg (bool def_p, int original_regno,
4502 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4504 if (optimize_function_for_size_p (cfun))
4505 return false;
4507 enum reg_class rclass = lra_get_allocno_class (original_regno);
4508 rtx original_reg = regno_reg_rtx[original_regno];
4509 rtx new_reg, usage_insn;
4510 rtx_insn *new_insns;
4512 lra_assert (! usage_insns[original_regno].after_p);
4513 if (lra_dump_file != NULL)
4514 fprintf (lra_dump_file,
4515 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4516 if (! ira_reg_classes_intersect_p[cl][rclass])
4518 if (lra_dump_file != NULL)
4520 fprintf (lra_dump_file,
4521 " Rejecting inheritance for %d "
4522 "because of disjoint classes %s and %s\n",
4523 original_regno, reg_class_names[cl],
4524 reg_class_names[rclass]);
4525 fprintf (lra_dump_file,
4526 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4528 return false;
4530 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4531 /* We don't use a subset of two classes because it can be
4532 NO_REGS. This transformation is still profitable in most
4533 cases even if the classes are not intersected as register
4534 move is probably cheaper than a memory load. */
4535 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4537 if (lra_dump_file != NULL)
4538 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4539 reg_class_names[cl], reg_class_names[rclass]);
4541 rclass = cl;
4543 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4545 /* Reject inheritance resulting in secondary memory moves.
4546 Otherwise, there is a danger in LRA cycling. Also such
4547 transformation will be unprofitable. */
4548 if (lra_dump_file != NULL)
4550 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4551 rtx set = single_set (insn);
4553 lra_assert (set != NULL_RTX);
4555 rtx dest = SET_DEST (set);
4557 lra_assert (REG_P (dest));
4558 fprintf (lra_dump_file,
4559 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4560 "as secondary mem is needed\n",
4561 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4562 original_regno, reg_class_names[rclass]);
4563 fprintf (lra_dump_file,
4564 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4566 return false;
4568 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4569 rclass, "inheritance");
4570 start_sequence ();
4571 if (def_p)
4572 lra_emit_move (original_reg, new_reg);
4573 else
4574 lra_emit_move (new_reg, original_reg);
4575 new_insns = get_insns ();
4576 end_sequence ();
4577 if (NEXT_INSN (new_insns) != NULL_RTX)
4579 if (lra_dump_file != NULL)
4581 fprintf (lra_dump_file,
4582 " Rejecting inheritance %d->%d "
4583 "as it results in 2 or more insns:\n",
4584 original_regno, REGNO (new_reg));
4585 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4586 fprintf (lra_dump_file,
4587 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4589 return false;
4591 substitute_pseudo_within_insn (insn, original_regno, new_reg);
4592 lra_update_insn_regno_info (insn);
4593 if (! def_p)
4594 /* We now have a new usage insn for original regno. */
4595 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4596 if (lra_dump_file != NULL)
4597 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4598 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4599 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4600 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4601 bitmap_set_bit (&check_only_regs, original_regno);
4602 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4603 if (def_p)
4604 lra_process_new_insns (insn, NULL, new_insns,
4605 "Add original<-inheritance");
4606 else
4607 lra_process_new_insns (insn, new_insns, NULL,
4608 "Add inheritance<-original");
4609 while (next_usage_insns != NULL_RTX)
4611 if (GET_CODE (next_usage_insns) != INSN_LIST)
4613 usage_insn = next_usage_insns;
4614 lra_assert (NONDEBUG_INSN_P (usage_insn));
4615 next_usage_insns = NULL;
4617 else
4619 usage_insn = XEXP (next_usage_insns, 0);
4620 lra_assert (DEBUG_INSN_P (usage_insn));
4621 next_usage_insns = XEXP (next_usage_insns, 1);
4623 substitute_pseudo (&usage_insn, original_regno, new_reg);
4624 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4625 if (lra_dump_file != NULL)
4627 fprintf (lra_dump_file,
4628 " Inheritance reuse change %d->%d (bb%d):\n",
4629 original_regno, REGNO (new_reg),
4630 BLOCK_FOR_INSN (usage_insn)->index);
4631 dump_insn_slim (lra_dump_file, usage_insn);
4634 if (lra_dump_file != NULL)
4635 fprintf (lra_dump_file,
4636 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4637 return true;
4640 /* Return true if we need a caller save/restore for pseudo REGNO which
4641 was assigned to a hard register. */
4642 static inline bool
4643 need_for_call_save_p (int regno)
4645 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4646 return (usage_insns[regno].calls_num < calls_num
4647 && (overlaps_hard_reg_set_p
4648 ((flag_use_caller_save &&
4649 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4650 ? lra_reg_info[regno].actual_call_used_reg_set
4651 : call_used_reg_set,
4652 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4653 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4654 PSEUDO_REGNO_MODE (regno))));
4657 /* Global registers occurring in the current EBB. */
4658 static bitmap_head ebb_global_regs;
4660 /* Return true if we need a split for hard register REGNO or pseudo
4661 REGNO which was assigned to a hard register.
4662 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4663 used for reloads since the EBB end. It is an approximation of the
4664 used hard registers in the split range. The exact value would
4665 require expensive calculations. If we were aggressive with
4666 splitting because of the approximation, the split pseudo will save
4667 the same hard register assignment and will be removed in the undo
4668 pass. We still need the approximation because too aggressive
4669 splitting would result in too inaccurate cost calculation in the
4670 assignment pass because of too many generated moves which will be
4671 probably removed in the undo pass. */
4672 static inline bool
4673 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4675 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4677 lra_assert (hard_regno >= 0);
4678 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4679 /* Don't split eliminable hard registers, otherwise we can
4680 split hard registers like hard frame pointer, which
4681 lives on BB start/end according to DF-infrastructure,
4682 when there is a pseudo assigned to the register and
4683 living in the same BB. */
4684 && (regno >= FIRST_PSEUDO_REGISTER
4685 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4686 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4687 /* Don't split call clobbered hard regs living through
4688 calls, otherwise we might have a check problem in the
4689 assign sub-pass as in the most cases (exception is a
4690 situation when lra_risky_transformations_p value is
4691 true) the assign pass assumes that all pseudos living
4692 through calls are assigned to call saved hard regs. */
4693 && (regno >= FIRST_PSEUDO_REGISTER
4694 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4695 || usage_insns[regno].calls_num == calls_num)
4696 /* We need at least 2 reloads to make pseudo splitting
4697 profitable. We should provide hard regno splitting in
4698 any case to solve 1st insn scheduling problem when
4699 moving hard register definition up might result in
4700 impossibility to find hard register for reload pseudo of
4701 small register class. */
4702 && (usage_insns[regno].reloads_num
4703 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4704 && (regno < FIRST_PSEUDO_REGISTER
4705 /* For short living pseudos, spilling + inheritance can
4706 be considered a substitution for splitting.
4707 Therefore we do not splitting for local pseudos. It
4708 decreases also aggressiveness of splitting. The
4709 minimal number of references is chosen taking into
4710 account that for 2 references splitting has no sense
4711 as we can just spill the pseudo. */
4712 || (regno >= FIRST_PSEUDO_REGISTER
4713 && lra_reg_info[regno].nrefs > 3
4714 && bitmap_bit_p (&ebb_global_regs, regno))))
4715 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4718 /* Return class for the split pseudo created from original pseudo with
4719 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4720 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4721 results in no secondary memory movements. */
4722 static enum reg_class
4723 choose_split_class (enum reg_class allocno_class,
4724 int hard_regno ATTRIBUTE_UNUSED,
4725 enum machine_mode mode ATTRIBUTE_UNUSED)
4727 #ifndef SECONDARY_MEMORY_NEEDED
4728 return allocno_class;
4729 #else
4730 int i;
4731 enum reg_class cl, best_cl = NO_REGS;
4732 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4733 = REGNO_REG_CLASS (hard_regno);
4735 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4736 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4737 return allocno_class;
4738 for (i = 0;
4739 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4740 i++)
4741 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4742 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4743 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4744 && (best_cl == NO_REGS
4745 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4746 best_cl = cl;
4747 return best_cl;
4748 #endif
4751 /* Do split transformations for insn INSN, which defines or uses
4752 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4753 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4754 "insns" field of usage_insns.
4756 The transformations look like:
4758 p <- ... p <- ...
4759 ... s <- p (new insn -- save)
4760 ... =>
4761 ... p <- s (new insn -- restore)
4762 <- ... p ... <- ... p ...
4764 <- ... p ... <- ... p ...
4765 ... s <- p (new insn -- save)
4766 ... =>
4767 ... p <- s (new insn -- restore)
4768 <- ... p ... <- ... p ...
4770 where p is an original pseudo got a hard register or a hard
4771 register and s is a new split pseudo. The save is put before INSN
4772 if BEFORE_P is true. Return true if we succeed in such
4773 transformation. */
4774 static bool
4775 split_reg (bool before_p, int original_regno, rtx_insn *insn,
4776 rtx next_usage_insns)
4778 enum reg_class rclass;
4779 rtx original_reg;
4780 int hard_regno, nregs;
4781 rtx new_reg, usage_insn;
4782 rtx_insn *restore, *save;
4783 bool after_p;
4784 bool call_save_p;
4786 if (original_regno < FIRST_PSEUDO_REGISTER)
4788 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4789 hard_regno = original_regno;
4790 call_save_p = false;
4791 nregs = 1;
4793 else
4795 hard_regno = reg_renumber[original_regno];
4796 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4797 rclass = lra_get_allocno_class (original_regno);
4798 original_reg = regno_reg_rtx[original_regno];
4799 call_save_p = need_for_call_save_p (original_regno);
4801 original_reg = regno_reg_rtx[original_regno];
4802 lra_assert (hard_regno >= 0);
4803 if (lra_dump_file != NULL)
4804 fprintf (lra_dump_file,
4805 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4806 if (call_save_p)
4808 enum machine_mode mode = GET_MODE (original_reg);
4810 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4811 hard_regno_nregs[hard_regno][mode],
4812 mode);
4813 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4815 else
4817 rclass = choose_split_class (rclass, hard_regno,
4818 GET_MODE (original_reg));
4819 if (rclass == NO_REGS)
4821 if (lra_dump_file != NULL)
4823 fprintf (lra_dump_file,
4824 " Rejecting split of %d(%s): "
4825 "no good reg class for %d(%s)\n",
4826 original_regno,
4827 reg_class_names[lra_get_allocno_class (original_regno)],
4828 hard_regno,
4829 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4830 fprintf
4831 (lra_dump_file,
4832 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4834 return false;
4836 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4837 rclass, "split");
4838 reg_renumber[REGNO (new_reg)] = hard_regno;
4840 save = emit_spill_move (true, new_reg, original_reg);
4841 if (NEXT_INSN (save) != NULL_RTX)
4843 lra_assert (! call_save_p);
4844 if (lra_dump_file != NULL)
4846 fprintf
4847 (lra_dump_file,
4848 " Rejecting split %d->%d resulting in > 2 %s save insns:\n",
4849 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4850 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
4851 fprintf (lra_dump_file,
4852 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4854 return false;
4856 restore = emit_spill_move (false, new_reg, original_reg);
4857 if (NEXT_INSN (restore) != NULL_RTX)
4859 lra_assert (! call_save_p);
4860 if (lra_dump_file != NULL)
4862 fprintf (lra_dump_file,
4863 " Rejecting split %d->%d "
4864 "resulting in > 2 %s restore insns:\n",
4865 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4866 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
4867 fprintf (lra_dump_file,
4868 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4870 return false;
4872 after_p = usage_insns[original_regno].after_p;
4873 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4874 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4875 bitmap_set_bit (&check_only_regs, original_regno);
4876 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4877 for (;;)
4879 if (GET_CODE (next_usage_insns) != INSN_LIST)
4881 usage_insn = next_usage_insns;
4882 break;
4884 usage_insn = XEXP (next_usage_insns, 0);
4885 lra_assert (DEBUG_INSN_P (usage_insn));
4886 next_usage_insns = XEXP (next_usage_insns, 1);
4887 substitute_pseudo (&usage_insn, original_regno, new_reg);
4888 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4889 if (lra_dump_file != NULL)
4891 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
4892 original_regno, REGNO (new_reg));
4893 dump_insn_slim (lra_dump_file, usage_insn);
4896 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
4897 lra_assert (usage_insn != insn || (after_p && before_p));
4898 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
4899 after_p ? NULL : restore,
4900 after_p ? restore : NULL,
4901 call_save_p
4902 ? "Add reg<-save" : "Add reg<-split");
4903 lra_process_new_insns (insn, before_p ? save : NULL,
4904 before_p ? NULL : save,
4905 call_save_p
4906 ? "Add save<-reg" : "Add split<-reg");
4907 if (nregs > 1)
4908 /* If we are trying to split multi-register. We should check
4909 conflicts on the next assignment sub-pass. IRA can allocate on
4910 sub-register levels, LRA do this on pseudos level right now and
4911 this discrepancy may create allocation conflicts after
4912 splitting. */
4913 lra_risky_transformations_p = true;
4914 if (lra_dump_file != NULL)
4915 fprintf (lra_dump_file,
4916 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4917 return true;
4920 /* Recognize that we need a split transformation for insn INSN, which
4921 defines or uses REGNO in its insn biggest MODE (we use it only if
4922 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
4923 hard registers which might be used for reloads since the EBB end.
4924 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
4925 uid before starting INSN processing. Return true if we succeed in
4926 such transformation. */
4927 static bool
4928 split_if_necessary (int regno, enum machine_mode mode,
4929 HARD_REG_SET potential_reload_hard_regs,
4930 bool before_p, rtx_insn *insn, int max_uid)
4932 bool res = false;
4933 int i, nregs = 1;
4934 rtx next_usage_insns;
4936 if (regno < FIRST_PSEUDO_REGISTER)
4937 nregs = hard_regno_nregs[regno][mode];
4938 for (i = 0; i < nregs; i++)
4939 if (usage_insns[regno + i].check == curr_usage_insns_check
4940 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
4941 /* To avoid processing the register twice or more. */
4942 && ((GET_CODE (next_usage_insns) != INSN_LIST
4943 && INSN_UID (next_usage_insns) < max_uid)
4944 || (GET_CODE (next_usage_insns) == INSN_LIST
4945 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
4946 && need_for_split_p (potential_reload_hard_regs, regno + i)
4947 && split_reg (before_p, regno + i, insn, next_usage_insns))
4948 res = true;
4949 return res;
4952 /* Check only registers living at the current program point in the
4953 current EBB. */
4954 static bitmap_head live_regs;
4956 /* Update live info in EBB given by its HEAD and TAIL insns after
4957 inheritance/split transformation. The function removes dead moves
4958 too. */
4959 static void
4960 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
4962 unsigned int j;
4963 int i, regno;
4964 bool live_p;
4965 rtx_insn *prev_insn;
4966 rtx set;
4967 bool remove_p;
4968 basic_block last_bb, prev_bb, curr_bb;
4969 bitmap_iterator bi;
4970 struct lra_insn_reg *reg;
4971 edge e;
4972 edge_iterator ei;
4974 last_bb = BLOCK_FOR_INSN (tail);
4975 prev_bb = NULL;
4976 for (curr_insn = tail;
4977 curr_insn != PREV_INSN (head);
4978 curr_insn = prev_insn)
4980 prev_insn = PREV_INSN (curr_insn);
4981 /* We need to process empty blocks too. They contain
4982 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
4983 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
4984 continue;
4985 curr_bb = BLOCK_FOR_INSN (curr_insn);
4986 if (curr_bb != prev_bb)
4988 if (prev_bb != NULL)
4990 /* Update df_get_live_in (prev_bb): */
4991 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4992 if (bitmap_bit_p (&live_regs, j))
4993 bitmap_set_bit (df_get_live_in (prev_bb), j);
4994 else
4995 bitmap_clear_bit (df_get_live_in (prev_bb), j);
4997 if (curr_bb != last_bb)
4999 /* Update df_get_live_out (curr_bb): */
5000 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5002 live_p = bitmap_bit_p (&live_regs, j);
5003 if (! live_p)
5004 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5005 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5007 live_p = true;
5008 break;
5010 if (live_p)
5011 bitmap_set_bit (df_get_live_out (curr_bb), j);
5012 else
5013 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5016 prev_bb = curr_bb;
5017 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5019 if (! NONDEBUG_INSN_P (curr_insn))
5020 continue;
5021 curr_id = lra_get_insn_recog_data (curr_insn);
5022 curr_static_id = curr_id->insn_static_data;
5023 remove_p = false;
5024 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
5025 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5026 && bitmap_bit_p (&check_only_regs, regno)
5027 && ! bitmap_bit_p (&live_regs, regno))
5028 remove_p = true;
5029 /* See which defined values die here. */
5030 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5031 if (reg->type == OP_OUT && ! reg->subreg_p)
5032 bitmap_clear_bit (&live_regs, reg->regno);
5033 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5034 if (reg->type == OP_OUT && ! reg->subreg_p)
5035 bitmap_clear_bit (&live_regs, reg->regno);
5036 /* Mark each used value as live. */
5037 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5038 if (reg->type != OP_OUT
5039 && bitmap_bit_p (&check_only_regs, reg->regno))
5040 bitmap_set_bit (&live_regs, reg->regno);
5041 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5042 if (reg->type != OP_OUT
5043 && bitmap_bit_p (&check_only_regs, reg->regno))
5044 bitmap_set_bit (&live_regs, reg->regno);
5045 if (curr_id->arg_hard_regs != NULL)
5046 /* Make argument hard registers live. */
5047 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5048 if (bitmap_bit_p (&check_only_regs, regno))
5049 bitmap_set_bit (&live_regs, regno);
5050 /* It is quite important to remove dead move insns because it
5051 means removing dead store. We don't need to process them for
5052 constraints. */
5053 if (remove_p)
5055 if (lra_dump_file != NULL)
5057 fprintf (lra_dump_file, " Removing dead insn:\n ");
5058 dump_insn_slim (lra_dump_file, curr_insn);
5060 lra_set_insn_deleted (curr_insn);
5065 /* The structure describes info to do an inheritance for the current
5066 insn. We need to collect such info first before doing the
5067 transformations because the transformations change the insn
5068 internal representation. */
5069 struct to_inherit
5071 /* Original regno. */
5072 int regno;
5073 /* Subsequent insns which can inherit original reg value. */
5074 rtx insns;
5077 /* Array containing all info for doing inheritance from the current
5078 insn. */
5079 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5081 /* Number elements in the previous array. */
5082 static int to_inherit_num;
5084 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5085 structure to_inherit. */
5086 static void
5087 add_to_inherit (int regno, rtx insns)
5089 int i;
5091 for (i = 0; i < to_inherit_num; i++)
5092 if (to_inherit[i].regno == regno)
5093 return;
5094 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5095 to_inherit[to_inherit_num].regno = regno;
5096 to_inherit[to_inherit_num++].insns = insns;
5099 /* Return the last non-debug insn in basic block BB, or the block begin
5100 note if none. */
5101 static rtx_insn *
5102 get_last_insertion_point (basic_block bb)
5104 rtx_insn *insn;
5106 FOR_BB_INSNS_REVERSE (bb, insn)
5107 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5108 return insn;
5109 gcc_unreachable ();
5112 /* Set up RES by registers living on edges FROM except the edge (FROM,
5113 TO) or by registers set up in a jump insn in BB FROM. */
5114 static void
5115 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5117 rtx_insn *last;
5118 struct lra_insn_reg *reg;
5119 edge e;
5120 edge_iterator ei;
5122 lra_assert (to != NULL);
5123 bitmap_clear (res);
5124 FOR_EACH_EDGE (e, ei, from->succs)
5125 if (e->dest != to)
5126 bitmap_ior_into (res, df_get_live_in (e->dest));
5127 last = get_last_insertion_point (from);
5128 if (! JUMP_P (last))
5129 return;
5130 curr_id = lra_get_insn_recog_data (last);
5131 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5132 if (reg->type != OP_IN)
5133 bitmap_set_bit (res, reg->regno);
5136 /* Used as a temporary results of some bitmap calculations. */
5137 static bitmap_head temp_bitmap;
5139 /* We split for reloads of small class of hard regs. The following
5140 defines how many hard regs the class should have to be qualified as
5141 small. The code is mostly oriented to x86/x86-64 architecture
5142 where some insns need to use only specific register or pair of
5143 registers and these register can live in RTL explicitly, e.g. for
5144 parameter passing. */
5145 static const int max_small_class_regs_num = 2;
5147 /* Do inheritance/split transformations in EBB starting with HEAD and
5148 finishing on TAIL. We process EBB insns in the reverse order.
5149 Return true if we did any inheritance/split transformation in the
5150 EBB.
5152 We should avoid excessive splitting which results in worse code
5153 because of inaccurate cost calculations for spilling new split
5154 pseudos in such case. To achieve this we do splitting only if
5155 register pressure is high in given basic block and there are reload
5156 pseudos requiring hard registers. We could do more register
5157 pressure calculations at any given program point to avoid necessary
5158 splitting even more but it is to expensive and the current approach
5159 works well enough. */
5160 static bool
5161 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5163 int i, src_regno, dst_regno, nregs;
5164 bool change_p, succ_p, update_reloads_num_p;
5165 rtx_insn *prev_insn, *last_insn;
5166 rtx next_usage_insns, set;
5167 enum reg_class cl;
5168 struct lra_insn_reg *reg;
5169 basic_block last_processed_bb, curr_bb = NULL;
5170 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5171 bitmap to_process;
5172 unsigned int j;
5173 bitmap_iterator bi;
5174 bool head_p, after_p;
5176 change_p = false;
5177 curr_usage_insns_check++;
5178 reloads_num = calls_num = 0;
5179 bitmap_clear (&check_only_regs);
5180 last_processed_bb = NULL;
5181 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5182 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5183 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5184 /* We don't process new insns generated in the loop. */
5185 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5187 prev_insn = PREV_INSN (curr_insn);
5188 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5189 curr_bb = BLOCK_FOR_INSN (curr_insn);
5190 if (last_processed_bb != curr_bb)
5192 /* We are at the end of BB. Add qualified living
5193 pseudos for potential splitting. */
5194 to_process = df_get_live_out (curr_bb);
5195 if (last_processed_bb != NULL)
5197 /* We are somewhere in the middle of EBB. */
5198 get_live_on_other_edges (curr_bb, last_processed_bb,
5199 &temp_bitmap);
5200 to_process = &temp_bitmap;
5202 last_processed_bb = curr_bb;
5203 last_insn = get_last_insertion_point (curr_bb);
5204 after_p = (! JUMP_P (last_insn)
5205 && (! CALL_P (last_insn)
5206 || (find_reg_note (last_insn,
5207 REG_NORETURN, NULL_RTX) == NULL_RTX
5208 && ! SIBLING_CALL_P (last_insn))));
5209 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5210 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5212 if ((int) j >= lra_constraint_new_regno_start)
5213 break;
5214 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5216 if (j < FIRST_PSEUDO_REGISTER)
5217 SET_HARD_REG_BIT (live_hard_regs, j);
5218 else
5219 add_to_hard_reg_set (&live_hard_regs,
5220 PSEUDO_REGNO_MODE (j),
5221 reg_renumber[j]);
5222 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5226 src_regno = dst_regno = -1;
5227 if (NONDEBUG_INSN_P (curr_insn)
5228 && (set = single_set (curr_insn)) != NULL_RTX
5229 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5231 src_regno = REGNO (SET_SRC (set));
5232 dst_regno = REGNO (SET_DEST (set));
5234 update_reloads_num_p = true;
5235 if (src_regno < lra_constraint_new_regno_start
5236 && src_regno >= FIRST_PSEUDO_REGISTER
5237 && reg_renumber[src_regno] < 0
5238 && dst_regno >= lra_constraint_new_regno_start
5239 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5241 /* 'reload_pseudo <- original_pseudo'. */
5242 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5243 reloads_num++;
5244 update_reloads_num_p = false;
5245 succ_p = false;
5246 if (usage_insns[src_regno].check == curr_usage_insns_check
5247 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5248 succ_p = inherit_reload_reg (false, src_regno, cl,
5249 curr_insn, next_usage_insns);
5250 if (succ_p)
5251 change_p = true;
5252 else
5253 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5254 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5255 IOR_HARD_REG_SET (potential_reload_hard_regs,
5256 reg_class_contents[cl]);
5258 else if (src_regno >= lra_constraint_new_regno_start
5259 && dst_regno < lra_constraint_new_regno_start
5260 && dst_regno >= FIRST_PSEUDO_REGISTER
5261 && reg_renumber[dst_regno] < 0
5262 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5263 && usage_insns[dst_regno].check == curr_usage_insns_check
5264 && (next_usage_insns
5265 = usage_insns[dst_regno].insns) != NULL_RTX)
5267 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5268 reloads_num++;
5269 update_reloads_num_p = false;
5270 /* 'original_pseudo <- reload_pseudo'. */
5271 if (! JUMP_P (curr_insn)
5272 && inherit_reload_reg (true, dst_regno, cl,
5273 curr_insn, next_usage_insns))
5274 change_p = true;
5275 /* Invalidate. */
5276 usage_insns[dst_regno].check = 0;
5277 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5278 IOR_HARD_REG_SET (potential_reload_hard_regs,
5279 reg_class_contents[cl]);
5281 else if (INSN_P (curr_insn))
5283 int iter;
5284 int max_uid = get_max_uid ();
5286 curr_id = lra_get_insn_recog_data (curr_insn);
5287 curr_static_id = curr_id->insn_static_data;
5288 to_inherit_num = 0;
5289 /* Process insn definitions. */
5290 for (iter = 0; iter < 2; iter++)
5291 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5292 reg != NULL;
5293 reg = reg->next)
5294 if (reg->type != OP_IN
5295 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5297 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5298 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5299 && usage_insns[dst_regno].check == curr_usage_insns_check
5300 && (next_usage_insns
5301 = usage_insns[dst_regno].insns) != NULL_RTX)
5303 struct lra_insn_reg *r;
5305 for (r = curr_id->regs; r != NULL; r = r->next)
5306 if (r->type != OP_OUT && r->regno == dst_regno)
5307 break;
5308 /* Don't do inheritance if the pseudo is also
5309 used in the insn. */
5310 if (r == NULL)
5311 /* We can not do inheritance right now
5312 because the current insn reg info (chain
5313 regs) can change after that. */
5314 add_to_inherit (dst_regno, next_usage_insns);
5316 /* We can not process one reg twice here because of
5317 usage_insns invalidation. */
5318 if ((dst_regno < FIRST_PSEUDO_REGISTER
5319 || reg_renumber[dst_regno] >= 0)
5320 && ! reg->subreg_p && reg->type != OP_IN)
5322 HARD_REG_SET s;
5324 if (split_if_necessary (dst_regno, reg->biggest_mode,
5325 potential_reload_hard_regs,
5326 false, curr_insn, max_uid))
5327 change_p = true;
5328 CLEAR_HARD_REG_SET (s);
5329 if (dst_regno < FIRST_PSEUDO_REGISTER)
5330 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5331 else
5332 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5333 reg_renumber[dst_regno]);
5334 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5336 /* We should invalidate potential inheritance or
5337 splitting for the current insn usages to the next
5338 usage insns (see code below) as the output pseudo
5339 prevents this. */
5340 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5341 && reg_renumber[dst_regno] < 0)
5342 || (reg->type == OP_OUT && ! reg->subreg_p
5343 && (dst_regno < FIRST_PSEUDO_REGISTER
5344 || reg_renumber[dst_regno] >= 0)))
5346 /* Invalidate and mark definitions. */
5347 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5348 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5349 else
5351 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5352 for (i = 0; i < nregs; i++)
5353 usage_insns[dst_regno + i].check
5354 = -(int) INSN_UID (curr_insn);
5358 if (! JUMP_P (curr_insn))
5359 for (i = 0; i < to_inherit_num; i++)
5360 if (inherit_reload_reg (true, to_inherit[i].regno,
5361 ALL_REGS, curr_insn,
5362 to_inherit[i].insns))
5363 change_p = true;
5364 if (CALL_P (curr_insn))
5366 rtx cheap, pat, dest;
5367 rtx_insn *restore;
5368 int regno, hard_regno;
5370 calls_num++;
5371 if ((cheap = find_reg_note (curr_insn,
5372 REG_RETURNED, NULL_RTX)) != NULL_RTX
5373 && ((cheap = XEXP (cheap, 0)), true)
5374 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5375 && (hard_regno = reg_renumber[regno]) >= 0
5376 /* If there are pending saves/restores, the
5377 optimization is not worth. */
5378 && usage_insns[regno].calls_num == calls_num - 1
5379 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5381 /* Restore the pseudo from the call result as
5382 REG_RETURNED note says that the pseudo value is
5383 in the call result and the pseudo is an argument
5384 of the call. */
5385 pat = PATTERN (curr_insn);
5386 if (GET_CODE (pat) == PARALLEL)
5387 pat = XVECEXP (pat, 0, 0);
5388 dest = SET_DEST (pat);
5389 /* For multiple return values dest is PARALLEL.
5390 Currently we handle only single return value case. */
5391 if (REG_P (dest))
5393 start_sequence ();
5394 emit_move_insn (cheap, copy_rtx (dest));
5395 restore = get_insns ();
5396 end_sequence ();
5397 lra_process_new_insns (curr_insn, NULL, restore,
5398 "Inserting call parameter restore");
5399 /* We don't need to save/restore of the pseudo from
5400 this call. */
5401 usage_insns[regno].calls_num = calls_num;
5402 bitmap_set_bit (&check_only_regs, regno);
5406 to_inherit_num = 0;
5407 /* Process insn usages. */
5408 for (iter = 0; iter < 2; iter++)
5409 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5410 reg != NULL;
5411 reg = reg->next)
5412 if ((reg->type != OP_OUT
5413 || (reg->type == OP_OUT && reg->subreg_p))
5414 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5416 if (src_regno >= FIRST_PSEUDO_REGISTER
5417 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5419 if (usage_insns[src_regno].check == curr_usage_insns_check
5420 && (next_usage_insns
5421 = usage_insns[src_regno].insns) != NULL_RTX
5422 && NONDEBUG_INSN_P (curr_insn))
5423 add_to_inherit (src_regno, next_usage_insns);
5424 else if (usage_insns[src_regno].check
5425 != -(int) INSN_UID (curr_insn))
5426 /* Add usages but only if the reg is not set up
5427 in the same insn. */
5428 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5430 else if (src_regno < FIRST_PSEUDO_REGISTER
5431 || reg_renumber[src_regno] >= 0)
5433 bool before_p;
5434 rtx use_insn = curr_insn;
5436 before_p = (JUMP_P (curr_insn)
5437 || (CALL_P (curr_insn) && reg->type == OP_IN));
5438 if (NONDEBUG_INSN_P (curr_insn)
5439 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5440 && split_if_necessary (src_regno, reg->biggest_mode,
5441 potential_reload_hard_regs,
5442 before_p, curr_insn, max_uid))
5444 if (reg->subreg_p)
5445 lra_risky_transformations_p = true;
5446 change_p = true;
5447 /* Invalidate. */
5448 usage_insns[src_regno].check = 0;
5449 if (before_p)
5450 use_insn = PREV_INSN (curr_insn);
5452 if (NONDEBUG_INSN_P (curr_insn))
5454 if (src_regno < FIRST_PSEUDO_REGISTER)
5455 add_to_hard_reg_set (&live_hard_regs,
5456 reg->biggest_mode, src_regno);
5457 else
5458 add_to_hard_reg_set (&live_hard_regs,
5459 PSEUDO_REGNO_MODE (src_regno),
5460 reg_renumber[src_regno]);
5462 add_next_usage_insn (src_regno, use_insn, reloads_num);
5465 /* Process call args. */
5466 if (curr_id->arg_hard_regs != NULL)
5467 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5468 if (src_regno < FIRST_PSEUDO_REGISTER)
5470 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5471 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5473 for (i = 0; i < to_inherit_num; i++)
5475 src_regno = to_inherit[i].regno;
5476 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5477 curr_insn, to_inherit[i].insns))
5478 change_p = true;
5479 else
5480 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5483 if (update_reloads_num_p
5484 && NONDEBUG_INSN_P (curr_insn)
5485 && (set = single_set (curr_insn)) != NULL_RTX)
5487 int regno = -1;
5488 if ((REG_P (SET_DEST (set))
5489 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5490 && reg_renumber[regno] < 0
5491 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5492 || (REG_P (SET_SRC (set))
5493 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5494 && reg_renumber[regno] < 0
5495 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5497 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5498 reloads_num++;
5499 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5500 IOR_HARD_REG_SET (potential_reload_hard_regs,
5501 reg_class_contents[cl]);
5504 /* We reached the start of the current basic block. */
5505 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5506 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5508 /* We reached the beginning of the current block -- do
5509 rest of spliting in the current BB. */
5510 to_process = df_get_live_in (curr_bb);
5511 if (BLOCK_FOR_INSN (head) != curr_bb)
5513 /* We are somewhere in the middle of EBB. */
5514 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5515 curr_bb, &temp_bitmap);
5516 to_process = &temp_bitmap;
5518 head_p = true;
5519 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5521 if ((int) j >= lra_constraint_new_regno_start)
5522 break;
5523 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5524 && usage_insns[j].check == curr_usage_insns_check
5525 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5527 if (need_for_split_p (potential_reload_hard_regs, j))
5529 if (lra_dump_file != NULL && head_p)
5531 fprintf (lra_dump_file,
5532 " ----------------------------------\n");
5533 head_p = false;
5535 if (split_reg (false, j, bb_note (curr_bb),
5536 next_usage_insns))
5537 change_p = true;
5539 usage_insns[j].check = 0;
5544 return change_p;
5547 /* This value affects EBB forming. If probability of edge from EBB to
5548 a BB is not greater than the following value, we don't add the BB
5549 to EBB. */
5550 #define EBB_PROBABILITY_CUTOFF ((REG_BR_PROB_BASE * 50) / 100)
5552 /* Current number of inheritance/split iteration. */
5553 int lra_inheritance_iter;
5555 /* Entry function for inheritance/split pass. */
5556 void
5557 lra_inheritance (void)
5559 int i;
5560 basic_block bb, start_bb;
5561 edge e;
5563 lra_inheritance_iter++;
5564 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5565 return;
5566 timevar_push (TV_LRA_INHERITANCE);
5567 if (lra_dump_file != NULL)
5568 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5569 lra_inheritance_iter);
5570 curr_usage_insns_check = 0;
5571 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5572 for (i = 0; i < lra_constraint_new_regno_start; i++)
5573 usage_insns[i].check = 0;
5574 bitmap_initialize (&check_only_regs, &reg_obstack);
5575 bitmap_initialize (&live_regs, &reg_obstack);
5576 bitmap_initialize (&temp_bitmap, &reg_obstack);
5577 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5578 FOR_EACH_BB_FN (bb, cfun)
5580 start_bb = bb;
5581 if (lra_dump_file != NULL)
5582 fprintf (lra_dump_file, "EBB");
5583 /* Form a EBB starting with BB. */
5584 bitmap_clear (&ebb_global_regs);
5585 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5586 for (;;)
5588 if (lra_dump_file != NULL)
5589 fprintf (lra_dump_file, " %d", bb->index);
5590 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5591 || LABEL_P (BB_HEAD (bb->next_bb)))
5592 break;
5593 e = find_fallthru_edge (bb->succs);
5594 if (! e)
5595 break;
5596 if (e->probability <= EBB_PROBABILITY_CUTOFF)
5597 break;
5598 bb = bb->next_bb;
5600 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5601 if (lra_dump_file != NULL)
5602 fprintf (lra_dump_file, "\n");
5603 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5604 /* Remember that the EBB head and tail can change in
5605 inherit_in_ebb. */
5606 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5608 bitmap_clear (&ebb_global_regs);
5609 bitmap_clear (&temp_bitmap);
5610 bitmap_clear (&live_regs);
5611 bitmap_clear (&check_only_regs);
5612 free (usage_insns);
5614 timevar_pop (TV_LRA_INHERITANCE);
5619 /* This page contains code to undo failed inheritance/split
5620 transformations. */
5622 /* Current number of iteration undoing inheritance/split. */
5623 int lra_undo_inheritance_iter;
5625 /* Fix BB live info LIVE after removing pseudos created on pass doing
5626 inheritance/split which are REMOVED_PSEUDOS. */
5627 static void
5628 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5630 unsigned int regno;
5631 bitmap_iterator bi;
5633 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5634 if (bitmap_clear_bit (live, regno))
5635 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5638 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5639 number. */
5640 static int
5641 get_regno (rtx reg)
5643 if (GET_CODE (reg) == SUBREG)
5644 reg = SUBREG_REG (reg);
5645 if (REG_P (reg))
5646 return REGNO (reg);
5647 return -1;
5650 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5651 return true if we did any change. The undo transformations for
5652 inheritance looks like
5653 i <- i2
5654 p <- i => p <- i2
5655 or removing
5656 p <- i, i <- p, and i <- i3
5657 where p is original pseudo from which inheritance pseudo i was
5658 created, i and i3 are removed inheritance pseudos, i2 is another
5659 not removed inheritance pseudo. All split pseudos or other
5660 occurrences of removed inheritance pseudos are changed on the
5661 corresponding original pseudos.
5663 The function also schedules insns changed and created during
5664 inheritance/split pass for processing by the subsequent constraint
5665 pass. */
5666 static bool
5667 remove_inheritance_pseudos (bitmap remove_pseudos)
5669 basic_block bb;
5670 int regno, sregno, prev_sregno, dregno, restore_regno;
5671 rtx set, prev_set;
5672 rtx_insn *prev_insn;
5673 bool change_p, done_p;
5675 change_p = ! bitmap_empty_p (remove_pseudos);
5676 /* We can not finish the function right away if CHANGE_P is true
5677 because we need to marks insns affected by previous
5678 inheritance/split pass for processing by the subsequent
5679 constraint pass. */
5680 FOR_EACH_BB_FN (bb, cfun)
5682 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5683 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5684 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5686 if (! INSN_P (curr_insn))
5687 continue;
5688 done_p = false;
5689 sregno = dregno = -1;
5690 if (change_p && NONDEBUG_INSN_P (curr_insn)
5691 && (set = single_set (curr_insn)) != NULL_RTX)
5693 dregno = get_regno (SET_DEST (set));
5694 sregno = get_regno (SET_SRC (set));
5697 if (sregno >= 0 && dregno >= 0)
5699 if ((bitmap_bit_p (remove_pseudos, sregno)
5700 && (lra_reg_info[sregno].restore_regno == dregno
5701 || (bitmap_bit_p (remove_pseudos, dregno)
5702 && (lra_reg_info[sregno].restore_regno
5703 == lra_reg_info[dregno].restore_regno))))
5704 || (bitmap_bit_p (remove_pseudos, dregno)
5705 && lra_reg_info[dregno].restore_regno == sregno))
5706 /* One of the following cases:
5707 original <- removed inheritance pseudo
5708 removed inherit pseudo <- another removed inherit pseudo
5709 removed inherit pseudo <- original pseudo
5711 removed_split_pseudo <- original_reg
5712 original_reg <- removed_split_pseudo */
5714 if (lra_dump_file != NULL)
5716 fprintf (lra_dump_file, " Removing %s:\n",
5717 bitmap_bit_p (&lra_split_regs, sregno)
5718 || bitmap_bit_p (&lra_split_regs, dregno)
5719 ? "split" : "inheritance");
5720 dump_insn_slim (lra_dump_file, curr_insn);
5722 lra_set_insn_deleted (curr_insn);
5723 done_p = true;
5725 else if (bitmap_bit_p (remove_pseudos, sregno)
5726 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5728 /* Search the following pattern:
5729 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5730 original_pseudo <- inherit_or_split_pseudo1
5731 where the 2nd insn is the current insn and
5732 inherit_or_split_pseudo2 is not removed. If it is found,
5733 change the current insn onto:
5734 original_pseudo <- inherit_or_split_pseudo2. */
5735 for (prev_insn = PREV_INSN (curr_insn);
5736 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5737 prev_insn = PREV_INSN (prev_insn))
5739 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5740 && (prev_set = single_set (prev_insn)) != NULL_RTX
5741 /* There should be no subregs in insn we are
5742 searching because only the original reg might
5743 be in subreg when we changed the mode of
5744 load/store for splitting. */
5745 && REG_P (SET_DEST (prev_set))
5746 && REG_P (SET_SRC (prev_set))
5747 && (int) REGNO (SET_DEST (prev_set)) == sregno
5748 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5749 >= FIRST_PSEUDO_REGISTER)
5750 /* As we consider chain of inheritance or
5751 splitting described in above comment we should
5752 check that sregno and prev_sregno were
5753 inheritance/split pseudos created from the
5754 same original regno. */
5755 && (lra_reg_info[sregno].restore_regno
5756 == lra_reg_info[prev_sregno].restore_regno)
5757 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5759 lra_assert (GET_MODE (SET_SRC (prev_set))
5760 == GET_MODE (regno_reg_rtx[sregno]));
5761 if (GET_CODE (SET_SRC (set)) == SUBREG)
5762 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5763 else
5764 SET_SRC (set) = SET_SRC (prev_set);
5765 /* As we are finishing with processing the insn
5766 here, check the destination too as it might
5767 inheritance pseudo for another pseudo. */
5768 if (bitmap_bit_p (remove_pseudos, dregno)
5769 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
5770 && (restore_regno
5771 = lra_reg_info[dregno].restore_regno) >= 0)
5773 if (GET_CODE (SET_DEST (set)) == SUBREG)
5774 SUBREG_REG (SET_DEST (set))
5775 = regno_reg_rtx[restore_regno];
5776 else
5777 SET_DEST (set) = regno_reg_rtx[restore_regno];
5779 lra_push_insn_and_update_insn_regno_info (curr_insn);
5780 lra_set_used_insn_alternative_by_uid
5781 (INSN_UID (curr_insn), -1);
5782 done_p = true;
5783 if (lra_dump_file != NULL)
5785 fprintf (lra_dump_file, " Change reload insn:\n");
5786 dump_insn_slim (lra_dump_file, curr_insn);
5791 if (! done_p)
5793 struct lra_insn_reg *reg;
5794 bool restored_regs_p = false;
5795 bool kept_regs_p = false;
5797 curr_id = lra_get_insn_recog_data (curr_insn);
5798 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5800 regno = reg->regno;
5801 restore_regno = lra_reg_info[regno].restore_regno;
5802 if (restore_regno >= 0)
5804 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5806 substitute_pseudo_within_insn (
5807 curr_insn, regno, regno_reg_rtx[restore_regno]);
5808 restored_regs_p = true;
5810 else
5811 kept_regs_p = true;
5814 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5816 /* The instruction has changed since the previous
5817 constraints pass. */
5818 lra_push_insn_and_update_insn_regno_info (curr_insn);
5819 lra_set_used_insn_alternative_by_uid
5820 (INSN_UID (curr_insn), -1);
5822 else if (restored_regs_p)
5823 /* The instruction has been restored to the form that
5824 it had during the previous constraints pass. */
5825 lra_update_insn_regno_info (curr_insn);
5826 if (restored_regs_p && lra_dump_file != NULL)
5828 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5829 dump_insn_slim (lra_dump_file, curr_insn);
5834 return change_p;
5837 /* If optional reload pseudos failed to get a hard register or was not
5838 inherited, it is better to remove optional reloads. We do this
5839 transformation after undoing inheritance to figure out necessity to
5840 remove optional reloads easier. Return true if we do any
5841 change. */
5842 static bool
5843 undo_optional_reloads (void)
5845 bool change_p, keep_p;
5846 unsigned int regno, uid;
5847 bitmap_iterator bi, bi2;
5848 rtx_insn *insn;
5849 rtx set, src, dest;
5850 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5852 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5853 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5854 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5856 keep_p = false;
5857 /* Keep optional reloads from previous subpasses. */
5858 if (lra_reg_info[regno].restore_regno < 0
5859 /* If the original pseudo changed its allocation, just
5860 removing the optional pseudo is dangerous as the original
5861 pseudo will have longer live range. */
5862 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
5863 keep_p = true;
5864 else if (reg_renumber[regno] >= 0)
5865 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
5867 insn = lra_insn_recog_data[uid]->insn;
5868 if ((set = single_set (insn)) == NULL_RTX)
5869 continue;
5870 src = SET_SRC (set);
5871 dest = SET_DEST (set);
5872 if (! REG_P (src) || ! REG_P (dest))
5873 continue;
5874 if (REGNO (dest) == regno
5875 /* Ignore insn for optional reloads itself. */
5876 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
5877 /* Check only inheritance on last inheritance pass. */
5878 && (int) REGNO (src) >= new_regno_start
5879 /* Check that the optional reload was inherited. */
5880 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
5882 keep_p = true;
5883 break;
5886 if (keep_p)
5888 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
5889 if (lra_dump_file != NULL)
5890 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
5893 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
5894 bitmap_initialize (&insn_bitmap, &reg_obstack);
5895 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
5897 if (lra_dump_file != NULL)
5898 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
5899 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
5900 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
5902 insn = lra_insn_recog_data[uid]->insn;
5903 if ((set = single_set (insn)) != NULL_RTX)
5905 src = SET_SRC (set);
5906 dest = SET_DEST (set);
5907 if (REG_P (src) && REG_P (dest)
5908 && ((REGNO (src) == regno
5909 && (lra_reg_info[regno].restore_regno
5910 == (int) REGNO (dest)))
5911 || (REGNO (dest) == regno
5912 && (lra_reg_info[regno].restore_regno
5913 == (int) REGNO (src)))))
5915 if (lra_dump_file != NULL)
5917 fprintf (lra_dump_file, " Deleting move %u\n",
5918 INSN_UID (insn));
5919 dump_insn_slim (lra_dump_file, insn);
5921 lra_set_insn_deleted (insn);
5922 continue;
5924 /* We should not worry about generation memory-memory
5925 moves here as if the corresponding inheritance did
5926 not work (inheritance pseudo did not get a hard reg),
5927 we remove the inheritance pseudo and the optional
5928 reload. */
5930 substitute_pseudo_within_insn (
5931 insn, regno,
5932 regno_reg_rtx[lra_reg_info[regno].restore_regno]);
5933 lra_update_insn_regno_info (insn);
5934 if (lra_dump_file != NULL)
5936 fprintf (lra_dump_file,
5937 " Restoring original insn:\n");
5938 dump_insn_slim (lra_dump_file, insn);
5942 /* Clear restore_regnos. */
5943 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5944 lra_reg_info[regno].restore_regno = -1;
5945 bitmap_clear (&insn_bitmap);
5946 bitmap_clear (&removed_optional_reload_pseudos);
5947 return change_p;
5950 /* Entry function for undoing inheritance/split transformation. Return true
5951 if we did any RTL change in this pass. */
5952 bool
5953 lra_undo_inheritance (void)
5955 unsigned int regno;
5956 int restore_regno, hard_regno;
5957 int n_all_inherit, n_inherit, n_all_split, n_split;
5958 bitmap_head remove_pseudos;
5959 bitmap_iterator bi;
5960 bool change_p;
5962 lra_undo_inheritance_iter++;
5963 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5964 return false;
5965 if (lra_dump_file != NULL)
5966 fprintf (lra_dump_file,
5967 "\n********** Undoing inheritance #%d: **********\n\n",
5968 lra_undo_inheritance_iter);
5969 bitmap_initialize (&remove_pseudos, &reg_obstack);
5970 n_inherit = n_all_inherit = 0;
5971 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5972 if (lra_reg_info[regno].restore_regno >= 0)
5974 n_all_inherit++;
5975 if (reg_renumber[regno] < 0
5976 /* If the original pseudo changed its allocation, just
5977 removing inheritance is dangerous as for changing
5978 allocation we used shorter live-ranges. */
5979 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
5980 bitmap_set_bit (&remove_pseudos, regno);
5981 else
5982 n_inherit++;
5984 if (lra_dump_file != NULL && n_all_inherit != 0)
5985 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
5986 n_inherit, n_all_inherit,
5987 (double) n_inherit / n_all_inherit * 100);
5988 n_split = n_all_split = 0;
5989 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5990 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
5992 n_all_split++;
5993 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
5994 ? reg_renumber[restore_regno] : restore_regno);
5995 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
5996 bitmap_set_bit (&remove_pseudos, regno);
5997 else
5999 n_split++;
6000 if (lra_dump_file != NULL)
6001 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6002 regno, restore_regno);
6005 if (lra_dump_file != NULL && n_all_split != 0)
6006 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6007 n_split, n_all_split,
6008 (double) n_split / n_all_split * 100);
6009 change_p = remove_inheritance_pseudos (&remove_pseudos);
6010 bitmap_clear (&remove_pseudos);
6011 /* Clear restore_regnos. */
6012 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6013 lra_reg_info[regno].restore_regno = -1;
6014 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6015 lra_reg_info[regno].restore_regno = -1;
6016 change_p = undo_optional_reloads () || change_p;
6017 return change_p;