gcc/
[official-gcc.git] / gcc / lra-constraints.c
blobf59bf555544331af605cc095b5e8fc022ec0cc2b
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 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 /* Return whether address AD is valid. If CONSTRAINT is null,
340 check for general addresses, otherwise check the extra constraint
341 CONSTRAINT. */
342 static bool
343 valid_address_p (struct address_info *ad, const char *constraint = 0)
345 /* Some ports do not check displacements for eliminable registers,
346 so we replace them temporarily with the elimination target. */
347 rtx saved_base_reg = NULL_RTX;
348 rtx saved_index_reg = NULL_RTX;
349 rtx *base_term = strip_subreg (ad->base_term);
350 rtx *index_term = strip_subreg (ad->index_term);
351 if (base_term != NULL)
353 saved_base_reg = *base_term;
354 lra_eliminate_reg_if_possible (base_term);
355 if (ad->base_term2 != NULL)
356 *ad->base_term2 = *ad->base_term;
358 if (index_term != NULL)
360 saved_index_reg = *index_term;
361 lra_eliminate_reg_if_possible (index_term);
363 bool ok_p = (constraint
364 #ifdef EXTRA_CONSTRAINT_STR
365 ? EXTRA_CONSTRAINT_STR (*ad->outer, constraint[0], constraint)
366 #else
367 ? false
368 #endif
369 : valid_address_p (ad->mode, *ad->outer, ad->as));
370 if (saved_base_reg != NULL_RTX)
372 *base_term = saved_base_reg;
373 if (ad->base_term2 != NULL)
374 *ad->base_term2 = *ad->base_term;
376 if (saved_index_reg != NULL_RTX)
377 *index_term = saved_index_reg;
378 return ok_p;
381 #ifdef EXTRA_CONSTRAINT_STR
382 /* Return true if, after elimination, OP satisfies extra memory constraint
383 CONSTRAINT. */
384 static bool
385 satisfies_memory_constraint_p (rtx op, const char *constraint)
387 struct address_info ad;
389 if (!MEM_P (op))
390 return false;
392 decompose_mem_address (&ad, op);
393 return valid_address_p (&ad, constraint);
396 /* Return true if, after elimination, OP satisfies extra address constraint
397 CONSTRAINT. */
398 static bool
399 satisfies_address_constraint_p (rtx op, const char *constraint)
401 struct address_info ad;
403 decompose_lea_address (&ad, &op);
404 return valid_address_p (&ad, constraint);
406 #endif
408 /* Initiate equivalences for LRA. As we keep original equivalences
409 before any elimination, we need to make copies otherwise any change
410 in insns might change the equivalences. */
411 void
412 lra_init_equiv (void)
414 ira_expand_reg_equiv ();
415 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
417 rtx res;
419 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
420 ira_reg_equiv[i].memory = copy_rtx (res);
421 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
422 ira_reg_equiv[i].invariant = copy_rtx (res);
426 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
428 /* Update equivalence for REGNO. We need to this as the equivalence
429 might contain other pseudos which are changed by their
430 equivalences. */
431 static void
432 update_equiv (int regno)
434 rtx x;
436 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
437 ira_reg_equiv[regno].memory
438 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
439 NULL_RTX);
440 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
441 ira_reg_equiv[regno].invariant
442 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
443 NULL_RTX);
446 /* If we have decided to substitute X with another value, return that
447 value, otherwise return X. */
448 static rtx
449 get_equiv (rtx x)
451 int regno;
452 rtx res;
454 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
455 || ! ira_reg_equiv[regno].defined_p
456 || ! ira_reg_equiv[regno].profitable_p
457 || lra_get_regno_hard_regno (regno) >= 0)
458 return x;
459 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
460 return res;
461 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
462 return res;
463 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
464 return res;
465 gcc_unreachable ();
468 /* If we have decided to substitute X with the equivalent value,
469 return that value after elimination for INSN, otherwise return
470 X. */
471 static rtx
472 get_equiv_with_elimination (rtx x, rtx insn)
474 rtx res = get_equiv (x);
476 if (x == res || CONSTANT_P (res))
477 return res;
478 return lra_eliminate_regs_1 (insn, res, GET_MODE (res), false, false, true);
481 /* Set up curr_operand_mode. */
482 static void
483 init_curr_operand_mode (void)
485 int nop = curr_static_id->n_operands;
486 for (int i = 0; i < nop; i++)
488 enum machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
489 if (mode == VOIDmode)
491 /* The .md mode for address operands is the mode of the
492 addressed value rather than the mode of the address itself. */
493 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
494 mode = Pmode;
495 else
496 mode = curr_static_id->operand[i].mode;
498 curr_operand_mode[i] = mode;
504 /* The page contains code to reuse input reloads. */
506 /* Structure describes input reload of the current insns. */
507 struct input_reload
509 /* Reloaded value. */
510 rtx input;
511 /* Reload pseudo used. */
512 rtx reg;
515 /* The number of elements in the following array. */
516 static int curr_insn_input_reloads_num;
517 /* Array containing info about input reloads. It is used to find the
518 same input reload and reuse the reload pseudo in this case. */
519 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
521 /* Initiate data concerning reuse of input reloads for the current
522 insn. */
523 static void
524 init_curr_insn_input_reloads (void)
526 curr_insn_input_reloads_num = 0;
529 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
530 created input reload pseudo (only if TYPE is not OP_OUT). Don't
531 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
532 wrapped up in SUBREG. The result pseudo is returned through
533 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
534 reused the already created input reload pseudo. Use TITLE to
535 describe new registers for debug purposes. */
536 static bool
537 get_reload_reg (enum op_type type, enum machine_mode mode, rtx original,
538 enum reg_class rclass, bool in_subreg_p,
539 const char *title, rtx *result_reg)
541 int i, regno;
542 enum reg_class new_class;
544 if (type == OP_OUT)
546 *result_reg
547 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
548 return true;
550 /* Prevent reuse value of expression with side effects,
551 e.g. volatile memory. */
552 if (! side_effects_p (original))
553 for (i = 0; i < curr_insn_input_reloads_num; i++)
554 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
555 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
557 rtx reg = curr_insn_input_reloads[i].reg;
558 regno = REGNO (reg);
559 /* If input is equal to original and both are VOIDmode,
560 GET_MODE (reg) might be still different from mode.
561 Ensure we don't return *result_reg with wrong mode. */
562 if (GET_MODE (reg) != mode)
564 if (in_subreg_p)
565 continue;
566 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
567 continue;
568 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
569 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
570 continue;
572 *result_reg = reg;
573 if (lra_dump_file != NULL)
575 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
576 dump_value_slim (lra_dump_file, original, 1);
578 if (new_class != lra_get_allocno_class (regno))
579 lra_change_class (regno, new_class, ", change to", false);
580 if (lra_dump_file != NULL)
581 fprintf (lra_dump_file, "\n");
582 return false;
584 *result_reg = lra_create_new_reg (mode, original, rclass, title);
585 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
586 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
587 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
588 return true;
593 /* The page contains code to extract memory address parts. */
595 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
596 static inline bool
597 ok_for_index_p_nonstrict (rtx reg)
599 unsigned regno = REGNO (reg);
601 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
604 /* A version of regno_ok_for_base_p for use here, when all pseudos
605 should count as OK. Arguments as for regno_ok_for_base_p. */
606 static inline bool
607 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode, addr_space_t as,
608 enum rtx_code outer_code, enum rtx_code index_code)
610 unsigned regno = REGNO (reg);
612 if (regno >= FIRST_PSEUDO_REGISTER)
613 return true;
614 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
619 /* The page contains major code to choose the current insn alternative
620 and generate reloads for it. */
622 /* Return the offset from REGNO of the least significant register
623 in (reg:MODE REGNO).
625 This function is used to tell whether two registers satisfy
626 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
628 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
629 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
631 lra_constraint_offset (int regno, enum machine_mode mode)
633 lra_assert (regno < FIRST_PSEUDO_REGISTER);
634 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
635 && SCALAR_INT_MODE_P (mode))
636 return hard_regno_nregs[regno][mode] - 1;
637 return 0;
640 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
641 if they are the same hard reg, and has special hacks for
642 auto-increment and auto-decrement. This is specifically intended for
643 process_alt_operands to use in determining whether two operands
644 match. X is the operand whose number is the lower of the two.
646 It is supposed that X is the output operand and Y is the input
647 operand. Y_HARD_REGNO is the final hard regno of register Y or
648 register in subreg Y as we know it now. Otherwise, it is a
649 negative value. */
650 static bool
651 operands_match_p (rtx x, rtx y, int y_hard_regno)
653 int i;
654 RTX_CODE code = GET_CODE (x);
655 const char *fmt;
657 if (x == y)
658 return true;
659 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
660 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
662 int j;
664 i = get_hard_regno (x);
665 if (i < 0)
666 goto slow;
668 if ((j = y_hard_regno) < 0)
669 goto slow;
671 i += lra_constraint_offset (i, GET_MODE (x));
672 j += lra_constraint_offset (j, GET_MODE (y));
674 return i == j;
677 /* If two operands must match, because they are really a single
678 operand of an assembler insn, then two post-increments are invalid
679 because the assembler insn would increment only once. On the
680 other hand, a post-increment matches ordinary indexing if the
681 post-increment is the output operand. */
682 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
683 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
685 /* Two pre-increments are invalid because the assembler insn would
686 increment only once. On the other hand, a pre-increment matches
687 ordinary indexing if the pre-increment is the input operand. */
688 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
689 || GET_CODE (y) == PRE_MODIFY)
690 return operands_match_p (x, XEXP (y, 0), -1);
692 slow:
694 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
695 && x == SUBREG_REG (y))
696 return true;
697 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
698 && SUBREG_REG (x) == y)
699 return true;
701 /* Now we have disposed of all the cases in which different rtx
702 codes can match. */
703 if (code != GET_CODE (y))
704 return false;
706 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
707 if (GET_MODE (x) != GET_MODE (y))
708 return false;
710 switch (code)
712 CASE_CONST_UNIQUE:
713 return false;
715 case LABEL_REF:
716 return XEXP (x, 0) == XEXP (y, 0);
717 case SYMBOL_REF:
718 return XSTR (x, 0) == XSTR (y, 0);
720 default:
721 break;
724 /* Compare the elements. If any pair of corresponding elements fail
725 to match, return false for the whole things. */
727 fmt = GET_RTX_FORMAT (code);
728 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
730 int val, j;
731 switch (fmt[i])
733 case 'w':
734 if (XWINT (x, i) != XWINT (y, i))
735 return false;
736 break;
738 case 'i':
739 if (XINT (x, i) != XINT (y, i))
740 return false;
741 break;
743 case 'e':
744 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
745 if (val == 0)
746 return false;
747 break;
749 case '0':
750 break;
752 case 'E':
753 if (XVECLEN (x, i) != XVECLEN (y, i))
754 return false;
755 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
757 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
758 if (val == 0)
759 return false;
761 break;
763 /* It is believed that rtx's at this level will never
764 contain anything but integers and other rtx's, except for
765 within LABEL_REFs and SYMBOL_REFs. */
766 default:
767 gcc_unreachable ();
770 return true;
773 /* True if X is a constant that can be forced into the constant pool.
774 MODE is the mode of the operand, or VOIDmode if not known. */
775 #define CONST_POOL_OK_P(MODE, X) \
776 ((MODE) != VOIDmode \
777 && CONSTANT_P (X) \
778 && GET_CODE (X) != HIGH \
779 && !targetm.cannot_force_const_mem (MODE, X))
781 /* True if C is a non-empty register class that has too few registers
782 to be safely used as a reload target class. */
783 #define SMALL_REGISTER_CLASS_P(C) \
784 (ira_class_hard_regs_num [(C)] == 1 \
785 || (ira_class_hard_regs_num [(C)] >= 1 \
786 && targetm.class_likely_spilled_p (C)))
788 /* If REG is a reload pseudo, try to make its class satisfying CL. */
789 static void
790 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
792 enum reg_class rclass;
794 /* Do not make more accurate class from reloads generated. They are
795 mostly moves with a lot of constraints. Making more accurate
796 class may results in very narrow class and impossibility of find
797 registers for several reloads of one insn. */
798 if (INSN_UID (curr_insn) >= new_insn_uid_start)
799 return;
800 if (GET_CODE (reg) == SUBREG)
801 reg = SUBREG_REG (reg);
802 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
803 return;
804 if (in_class_p (reg, cl, &rclass) && rclass != cl)
805 lra_change_class (REGNO (reg), rclass, " Change to", true);
808 /* Generate reloads for matching OUT and INS (array of input operand
809 numbers with end marker -1) with reg class GOAL_CLASS. Add input
810 and output reloads correspondingly to the lists *BEFORE and *AFTER.
811 OUT might be negative. In this case we generate input reloads for
812 matched input operands INS. */
813 static void
814 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
815 rtx *before, rtx *after)
817 int i, in;
818 rtx new_in_reg, new_out_reg, reg, clobber;
819 enum machine_mode inmode, outmode;
820 rtx in_rtx = *curr_id->operand_loc[ins[0]];
821 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
823 inmode = curr_operand_mode[ins[0]];
824 outmode = out < 0 ? inmode : curr_operand_mode[out];
825 push_to_sequence (*before);
826 if (inmode != outmode)
828 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
830 reg = new_in_reg
831 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
832 goal_class, "");
833 if (SCALAR_INT_MODE_P (inmode))
834 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
835 else
836 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
837 LRA_SUBREG_P (new_out_reg) = 1;
838 /* If the input reg is dying here, we can use the same hard
839 register for REG and IN_RTX. We do it only for original
840 pseudos as reload pseudos can die although original
841 pseudos still live where reload pseudos dies. */
842 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
843 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
844 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
846 else
848 reg = new_out_reg
849 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
850 goal_class, "");
851 if (SCALAR_INT_MODE_P (outmode))
852 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
853 else
854 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
855 /* NEW_IN_REG is non-paradoxical subreg. We don't want
856 NEW_OUT_REG living above. We add clobber clause for
857 this. This is just a temporary clobber. We can remove
858 it at the end of LRA work. */
859 clobber = emit_clobber (new_out_reg);
860 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
861 LRA_SUBREG_P (new_in_reg) = 1;
862 if (GET_CODE (in_rtx) == SUBREG)
864 rtx subreg_reg = SUBREG_REG (in_rtx);
866 /* If SUBREG_REG is dying here and sub-registers IN_RTX
867 and NEW_IN_REG are similar, we can use the same hard
868 register for REG and SUBREG_REG. */
869 if (REG_P (subreg_reg)
870 && (int) REGNO (subreg_reg) < lra_new_regno_start
871 && GET_MODE (subreg_reg) == outmode
872 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
873 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
874 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
878 else
880 /* Pseudos have values -- see comments for lra_reg_info.
881 Different pseudos with the same value do not conflict even if
882 they live in the same place. When we create a pseudo we
883 assign value of original pseudo (if any) from which we
884 created the new pseudo. If we create the pseudo from the
885 input pseudo, the new pseudo will no conflict with the input
886 pseudo which is wrong when the input pseudo lives after the
887 insn and as the new pseudo value is changed by the insn
888 output. Therefore we create the new pseudo from the output.
890 We cannot reuse the current output register because we might
891 have a situation like "a <- a op b", where the constraints
892 force the second input operand ("b") to match the output
893 operand ("a"). "b" must then be copied into a new register
894 so that it doesn't clobber the current value of "a". */
896 new_in_reg = new_out_reg
897 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
898 goal_class, "");
900 /* In operand can be got from transformations before processing insn
901 constraints. One example of such transformations is subreg
902 reloading (see function simplify_operand_subreg). The new
903 pseudos created by the transformations might have inaccurate
904 class (ALL_REGS) and we should make their classes more
905 accurate. */
906 narrow_reload_pseudo_class (in_rtx, goal_class);
907 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
908 *before = get_insns ();
909 end_sequence ();
910 for (i = 0; (in = ins[i]) >= 0; i++)
912 lra_assert
913 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
914 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
915 *curr_id->operand_loc[in] = new_in_reg;
917 lra_update_dups (curr_id, ins);
918 if (out < 0)
919 return;
920 /* See a comment for the input operand above. */
921 narrow_reload_pseudo_class (out_rtx, goal_class);
922 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
924 start_sequence ();
925 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
926 emit_insn (*after);
927 *after = get_insns ();
928 end_sequence ();
930 *curr_id->operand_loc[out] = new_out_reg;
931 lra_update_dup (curr_id, out);
934 /* Return register class which is union of all reg classes in insn
935 constraint alternative string starting with P. */
936 static enum reg_class
937 reg_class_from_constraints (const char *p)
939 int c, len;
940 enum reg_class op_class = NO_REGS;
943 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
945 case '#':
946 case ',':
947 return op_class;
949 case 'p':
950 op_class = (reg_class_subunion
951 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
952 ADDRESS, SCRATCH)]);
953 break;
955 case 'g':
956 case 'r':
957 op_class = reg_class_subunion[op_class][GENERAL_REGS];
958 break;
960 default:
961 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
963 #ifdef EXTRA_CONSTRAINT_STR
964 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
965 op_class
966 = (reg_class_subunion
967 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
968 ADDRESS, SCRATCH)]);
969 #endif
970 break;
973 op_class
974 = reg_class_subunion[op_class][REG_CLASS_FROM_CONSTRAINT (c, p)];
975 break;
977 while ((p += len), c);
978 return op_class;
981 /* If OP is a register, return the class of the register as per
982 get_reg_class, otherwise return NO_REGS. */
983 static inline enum reg_class
984 get_op_class (rtx op)
986 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
989 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
990 otherwise. If modes of MEM_PSEUDO and VAL are different, use
991 SUBREG for VAL to make them equal. */
992 static rtx
993 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
995 if (GET_MODE (mem_pseudo) != GET_MODE (val))
997 /* Usually size of mem_pseudo is greater than val size but in
998 rare cases it can be less as it can be defined by target
999 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1000 if (! MEM_P (val))
1002 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1003 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1005 LRA_SUBREG_P (val) = 1;
1007 else
1009 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1010 LRA_SUBREG_P (mem_pseudo) = 1;
1013 return (to_p
1014 ? gen_move_insn (mem_pseudo, val)
1015 : gen_move_insn (val, mem_pseudo));
1018 /* Process a special case insn (register move), return true if we
1019 don't need to process it anymore. INSN should be a single set
1020 insn. Set up that RTL was changed through CHANGE_P and macro
1021 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1022 SEC_MEM_P. */
1023 static bool
1024 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1026 int sregno, dregno;
1027 rtx dest, src, dreg, sreg, old_sreg, new_reg, before, scratch_reg;
1028 enum reg_class dclass, sclass, secondary_class;
1029 enum machine_mode sreg_mode;
1030 secondary_reload_info sri;
1032 lra_assert (curr_insn_set != NULL_RTX);
1033 dreg = dest = SET_DEST (curr_insn_set);
1034 sreg = src = SET_SRC (curr_insn_set);
1035 if (GET_CODE (dest) == SUBREG)
1036 dreg = SUBREG_REG (dest);
1037 if (GET_CODE (src) == SUBREG)
1038 sreg = SUBREG_REG (src);
1039 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1040 return false;
1041 sclass = dclass = NO_REGS;
1042 if (REG_P (dreg))
1043 dclass = get_reg_class (REGNO (dreg));
1044 if (dclass == ALL_REGS)
1045 /* ALL_REGS is used for new pseudos created by transformations
1046 like reload of SUBREG_REG (see function
1047 simplify_operand_subreg). We don't know their class yet. We
1048 should figure out the class from processing the insn
1049 constraints not in this fast path function. Even if ALL_REGS
1050 were a right class for the pseudo, secondary_... hooks usually
1051 are not define for ALL_REGS. */
1052 return false;
1053 sreg_mode = GET_MODE (sreg);
1054 old_sreg = sreg;
1055 if (REG_P (sreg))
1056 sclass = get_reg_class (REGNO (sreg));
1057 if (sclass == ALL_REGS)
1058 /* See comments above. */
1059 return false;
1060 if (sclass == NO_REGS && dclass == NO_REGS)
1061 return false;
1062 #ifdef SECONDARY_MEMORY_NEEDED
1063 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1064 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1065 && ((sclass != NO_REGS && dclass != NO_REGS)
1066 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1067 #endif
1070 *sec_mem_p = true;
1071 return false;
1073 #endif
1074 if (! REG_P (dreg) || ! REG_P (sreg))
1075 return false;
1076 sri.prev_sri = NULL;
1077 sri.icode = CODE_FOR_nothing;
1078 sri.extra_cost = 0;
1079 secondary_class = NO_REGS;
1080 /* Set up hard register for a reload pseudo for hook
1081 secondary_reload because some targets just ignore unassigned
1082 pseudos in the hook. */
1083 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1085 dregno = REGNO (dreg);
1086 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1088 else
1089 dregno = -1;
1090 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1092 sregno = REGNO (sreg);
1093 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1095 else
1096 sregno = -1;
1097 if (sclass != NO_REGS)
1098 secondary_class
1099 = (enum reg_class) targetm.secondary_reload (false, dest,
1100 (reg_class_t) sclass,
1101 GET_MODE (src), &sri);
1102 if (sclass == NO_REGS
1103 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1104 && dclass != NO_REGS))
1106 enum reg_class old_sclass = secondary_class;
1107 secondary_reload_info old_sri = sri;
1109 sri.prev_sri = NULL;
1110 sri.icode = CODE_FOR_nothing;
1111 sri.extra_cost = 0;
1112 secondary_class
1113 = (enum reg_class) targetm.secondary_reload (true, sreg,
1114 (reg_class_t) dclass,
1115 sreg_mode, &sri);
1116 /* Check the target hook consistency. */
1117 lra_assert
1118 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1119 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1120 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1122 if (sregno >= 0)
1123 reg_renumber [sregno] = -1;
1124 if (dregno >= 0)
1125 reg_renumber [dregno] = -1;
1126 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1127 return false;
1128 *change_p = true;
1129 new_reg = NULL_RTX;
1130 if (secondary_class != NO_REGS)
1131 new_reg = lra_create_new_reg_with_unique_value (sreg_mode, NULL_RTX,
1132 secondary_class,
1133 "secondary");
1134 start_sequence ();
1135 if (old_sreg != sreg)
1136 sreg = copy_rtx (sreg);
1137 if (sri.icode == CODE_FOR_nothing)
1138 lra_emit_move (new_reg, sreg);
1139 else
1141 enum reg_class scratch_class;
1143 scratch_class = (reg_class_from_constraints
1144 (insn_data[sri.icode].operand[2].constraint));
1145 scratch_reg = (lra_create_new_reg_with_unique_value
1146 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1147 scratch_class, "scratch"));
1148 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1149 sreg, scratch_reg));
1151 before = get_insns ();
1152 end_sequence ();
1153 lra_process_new_insns (curr_insn, before, NULL_RTX, "Inserting the move");
1154 if (new_reg != NULL_RTX)
1156 if (GET_CODE (src) == SUBREG)
1157 SUBREG_REG (src) = new_reg;
1158 else
1159 SET_SRC (curr_insn_set) = new_reg;
1161 else
1163 if (lra_dump_file != NULL)
1165 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1166 dump_insn_slim (lra_dump_file, curr_insn);
1168 lra_set_insn_deleted (curr_insn);
1169 return true;
1171 return false;
1174 /* The following data describe the result of process_alt_operands.
1175 The data are used in curr_insn_transform to generate reloads. */
1177 /* The chosen reg classes which should be used for the corresponding
1178 operands. */
1179 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1180 /* True if the operand should be the same as another operand and that
1181 other operand does not need a reload. */
1182 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1183 /* True if the operand does not need a reload. */
1184 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1185 /* True if the operand can be offsetable memory. */
1186 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1187 /* The number of an operand to which given operand can be matched to. */
1188 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1189 /* The number of elements in the following array. */
1190 static int goal_alt_dont_inherit_ops_num;
1191 /* Numbers of operands whose reload pseudos should not be inherited. */
1192 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1193 /* True if the insn commutative operands should be swapped. */
1194 static bool goal_alt_swapped;
1195 /* The chosen insn alternative. */
1196 static int goal_alt_number;
1198 /* The following five variables are used to choose the best insn
1199 alternative. They reflect final characteristics of the best
1200 alternative. */
1202 /* Number of necessary reloads and overall cost reflecting the
1203 previous value and other unpleasantness of the best alternative. */
1204 static int best_losers, best_overall;
1205 /* Overall number hard registers used for reloads. For example, on
1206 some targets we need 2 general registers to reload DFmode and only
1207 one floating point register. */
1208 static int best_reload_nregs;
1209 /* Overall number reflecting distances of previous reloading the same
1210 value. The distances are counted from the current BB start. It is
1211 used to improve inheritance chances. */
1212 static int best_reload_sum;
1214 /* True if the current insn should have no correspondingly input or
1215 output reloads. */
1216 static bool no_input_reloads_p, no_output_reloads_p;
1218 /* True if we swapped the commutative operands in the current
1219 insn. */
1220 static int curr_swapped;
1222 /* Arrange for address element *LOC to be a register of class CL.
1223 Add any input reloads to list BEFORE. AFTER is nonnull if *LOC is an
1224 automodified value; handle that case by adding the required output
1225 reloads to list AFTER. Return true if the RTL was changed. */
1226 static bool
1227 process_addr_reg (rtx *loc, rtx *before, rtx *after, enum reg_class cl)
1229 int regno;
1230 enum reg_class rclass, new_class;
1231 rtx reg;
1232 rtx new_reg;
1233 enum machine_mode mode;
1234 bool subreg_p, before_p = false;
1236 subreg_p = GET_CODE (*loc) == SUBREG;
1237 if (subreg_p)
1238 loc = &SUBREG_REG (*loc);
1239 reg = *loc;
1240 mode = GET_MODE (reg);
1241 if (! REG_P (reg))
1243 /* Always reload memory in an address even if the target supports
1244 such addresses. */
1245 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1246 before_p = true;
1248 else
1250 regno = REGNO (reg);
1251 rclass = get_reg_class (regno);
1252 if ((*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1254 if (lra_dump_file != NULL)
1256 fprintf (lra_dump_file,
1257 "Changing pseudo %d in address of insn %u on equiv ",
1258 REGNO (reg), INSN_UID (curr_insn));
1259 dump_value_slim (lra_dump_file, *loc, 1);
1260 fprintf (lra_dump_file, "\n");
1262 *loc = copy_rtx (*loc);
1264 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1266 reg = *loc;
1267 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1268 mode, reg, cl, subreg_p, "address", &new_reg))
1269 before_p = true;
1271 else if (new_class != NO_REGS && rclass != new_class)
1273 lra_change_class (regno, new_class, " Change to", true);
1274 return false;
1276 else
1277 return false;
1279 if (before_p)
1281 push_to_sequence (*before);
1282 lra_emit_move (new_reg, reg);
1283 *before = get_insns ();
1284 end_sequence ();
1286 *loc = new_reg;
1287 if (after != NULL)
1289 start_sequence ();
1290 lra_emit_move (reg, new_reg);
1291 emit_insn (*after);
1292 *after = get_insns ();
1293 end_sequence ();
1295 return true;
1298 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1299 the insn to be inserted before curr insn. AFTER returns the
1300 the insn to be inserted after curr insn. ORIGREG and NEWREG
1301 are the original reg and new reg for reload. */
1302 static void
1303 insert_move_for_subreg (rtx *before, rtx *after, rtx origreg, rtx newreg)
1305 if (before)
1307 push_to_sequence (*before);
1308 lra_emit_move (newreg, origreg);
1309 *before = get_insns ();
1310 end_sequence ();
1312 if (after)
1314 start_sequence ();
1315 lra_emit_move (origreg, newreg);
1316 emit_insn (*after);
1317 *after = get_insns ();
1318 end_sequence ();
1322 /* Make reloads for subreg in operand NOP with internal subreg mode
1323 REG_MODE, add new reloads for further processing. Return true if
1324 any reload was generated. */
1325 static bool
1326 simplify_operand_subreg (int nop, enum machine_mode reg_mode)
1328 int hard_regno;
1329 rtx before, after;
1330 enum machine_mode mode;
1331 rtx reg, new_reg;
1332 rtx operand = *curr_id->operand_loc[nop];
1333 enum reg_class regclass;
1334 enum op_type type;
1336 before = after = NULL_RTX;
1338 if (GET_CODE (operand) != SUBREG)
1339 return false;
1341 mode = GET_MODE (operand);
1342 reg = SUBREG_REG (operand);
1343 type = curr_static_id->operand[nop].type;
1344 /* If we change address for paradoxical subreg of memory, the
1345 address might violate the necessary alignment or the access might
1346 be slow. So take this into consideration. We should not worry
1347 about access beyond allocated memory for paradoxical memory
1348 subregs as we don't substitute such equiv memory (see processing
1349 equivalences in function lra_constraints) and because for spilled
1350 pseudos we allocate stack memory enough for the biggest
1351 corresponding paradoxical subreg. */
1352 if ((MEM_P (reg)
1353 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1354 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1355 || (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER))
1357 alter_subreg (curr_id->operand_loc[nop], false);
1358 return true;
1360 /* Put constant into memory when we have mixed modes. It generates
1361 a better code in most cases as it does not need a secondary
1362 reload memory. It also prevents LRA looping when LRA is using
1363 secondary reload memory again and again. */
1364 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1365 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1367 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1368 alter_subreg (curr_id->operand_loc[nop], false);
1369 return true;
1371 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1372 if there may be a problem accessing OPERAND in the outer
1373 mode. */
1374 if ((REG_P (reg)
1375 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1376 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1377 /* Don't reload paradoxical subregs because we could be looping
1378 having repeatedly final regno out of hard regs range. */
1379 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1380 >= hard_regno_nregs[hard_regno][mode])
1381 && simplify_subreg_regno (hard_regno, GET_MODE (reg),
1382 SUBREG_BYTE (operand), mode) < 0
1383 /* Don't reload subreg for matching reload. It is actually
1384 valid subreg in LRA. */
1385 && ! LRA_SUBREG_P (operand))
1386 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1388 enum reg_class rclass;
1390 if (REG_P (reg))
1391 /* There is a big probability that we will get the same class
1392 for the new pseudo and we will get the same insn which
1393 means infinite looping. So spill the new pseudo. */
1394 rclass = NO_REGS;
1395 else
1396 /* The class will be defined later in curr_insn_transform. */
1397 rclass
1398 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1400 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1401 rclass, TRUE, "subreg reg", &new_reg))
1403 bool insert_before, insert_after;
1404 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1406 insert_before = (type != OP_OUT
1407 || GET_MODE_SIZE (GET_MODE (reg)) > GET_MODE_SIZE (mode));
1408 insert_after = (type != OP_IN);
1409 insert_move_for_subreg (insert_before ? &before : NULL,
1410 insert_after ? &after : NULL,
1411 reg, new_reg);
1413 SUBREG_REG (operand) = new_reg;
1414 lra_process_new_insns (curr_insn, before, after,
1415 "Inserting subreg reload");
1416 return true;
1418 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1419 IRA allocates hardreg to the inner pseudo reg according to its mode
1420 instead of the outermode, so the size of the hardreg may not be enough
1421 to contain the outermode operand, in that case we may need to insert
1422 reload for the reg. For the following two types of paradoxical subreg,
1423 we need to insert reload:
1424 1. If the op_type is OP_IN, and the hardreg could not be paired with
1425 other hardreg to contain the outermode operand
1426 (checked by in_hard_reg_set_p), we need to insert the reload.
1427 2. If the op_type is OP_OUT or OP_INOUT.
1429 Here is a paradoxical subreg example showing how the reload is generated:
1431 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1432 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1434 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1435 here, if reg107 is assigned to hardreg R15, because R15 is the last
1436 hardreg, compiler cannot find another hardreg to pair with R15 to
1437 contain TImode data. So we insert a TImode reload reg180 for it.
1438 After reload is inserted:
1440 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1441 (reg:DI 107 [ __comp ])) -1
1442 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1443 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1445 Two reload hard registers will be allocated to reg180 to save TImode data
1446 in LRA_assign. */
1447 else if (REG_P (reg)
1448 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1449 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1450 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1451 < hard_regno_nregs[hard_regno][mode])
1452 && (regclass = lra_get_allocno_class (REGNO (reg)))
1453 && (type != OP_IN
1454 || !in_hard_reg_set_p (reg_class_contents[regclass],
1455 mode, hard_regno)))
1457 /* The class will be defined later in curr_insn_transform. */
1458 enum reg_class rclass
1459 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1461 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1462 rclass, TRUE, "paradoxical subreg", &new_reg))
1464 rtx subreg;
1465 bool insert_before, insert_after;
1467 PUT_MODE (new_reg, mode);
1468 subreg = simplify_gen_subreg (GET_MODE (reg), new_reg, mode, 0);
1469 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1471 insert_before = (type != OP_OUT);
1472 insert_after = (type != OP_IN);
1473 insert_move_for_subreg (insert_before ? &before : NULL,
1474 insert_after ? &after : NULL,
1475 reg, subreg);
1477 SUBREG_REG (operand) = new_reg;
1478 lra_process_new_insns (curr_insn, before, after,
1479 "Inserting paradoxical subreg reload");
1480 return true;
1482 return false;
1485 /* Return TRUE if X refers for a hard register from SET. */
1486 static bool
1487 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1489 int i, j, x_hard_regno;
1490 enum machine_mode mode;
1491 const char *fmt;
1492 enum rtx_code code;
1494 if (x == NULL_RTX)
1495 return false;
1496 code = GET_CODE (x);
1497 mode = GET_MODE (x);
1498 if (code == SUBREG)
1500 x = SUBREG_REG (x);
1501 code = GET_CODE (x);
1502 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1503 mode = GET_MODE (x);
1506 if (REG_P (x))
1508 x_hard_regno = get_hard_regno (x);
1509 return (x_hard_regno >= 0
1510 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1512 if (MEM_P (x))
1514 struct address_info ad;
1516 decompose_mem_address (&ad, x);
1517 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1518 return true;
1519 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1520 return true;
1522 fmt = GET_RTX_FORMAT (code);
1523 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1525 if (fmt[i] == 'e')
1527 if (uses_hard_regs_p (XEXP (x, i), set))
1528 return true;
1530 else if (fmt[i] == 'E')
1532 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1533 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1534 return true;
1537 return false;
1540 /* Return true if OP is a spilled pseudo. */
1541 static inline bool
1542 spilled_pseudo_p (rtx op)
1544 return (REG_P (op)
1545 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1548 /* Return true if X is a general constant. */
1549 static inline bool
1550 general_constant_p (rtx x)
1552 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1555 static bool
1556 reg_in_class_p (rtx reg, enum reg_class cl)
1558 if (cl == NO_REGS)
1559 return get_reg_class (REGNO (reg)) == NO_REGS;
1560 return in_class_p (reg, cl, NULL);
1563 /* Major function to choose the current insn alternative and what
1564 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1565 negative we should consider only this alternative. Return false if
1566 we can not choose the alternative or find how to reload the
1567 operands. */
1568 static bool
1569 process_alt_operands (int only_alternative)
1571 bool ok_p = false;
1572 int nop, overall, nalt;
1573 int n_alternatives = curr_static_id->n_alternatives;
1574 int n_operands = curr_static_id->n_operands;
1575 /* LOSERS counts the operands that don't fit this alternative and
1576 would require loading. */
1577 int losers;
1578 /* REJECT is a count of how undesirable this alternative says it is
1579 if any reloading is required. If the alternative matches exactly
1580 then REJECT is ignored, but otherwise it gets this much counted
1581 against it in addition to the reloading needed. */
1582 int reject;
1583 /* The number of elements in the following array. */
1584 int early_clobbered_regs_num;
1585 /* Numbers of operands which are early clobber registers. */
1586 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1587 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1588 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1589 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1590 bool curr_alt_win[MAX_RECOG_OPERANDS];
1591 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1592 int curr_alt_matches[MAX_RECOG_OPERANDS];
1593 /* The number of elements in the following array. */
1594 int curr_alt_dont_inherit_ops_num;
1595 /* Numbers of operands whose reload pseudos should not be inherited. */
1596 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1597 rtx op;
1598 /* The register when the operand is a subreg of register, otherwise the
1599 operand itself. */
1600 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1601 /* The register if the operand is a register or subreg of register,
1602 otherwise NULL. */
1603 rtx operand_reg[MAX_RECOG_OPERANDS];
1604 int hard_regno[MAX_RECOG_OPERANDS];
1605 enum machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1606 int reload_nregs, reload_sum;
1607 bool costly_p;
1608 enum reg_class cl;
1610 /* Calculate some data common for all alternatives to speed up the
1611 function. */
1612 for (nop = 0; nop < n_operands; nop++)
1614 rtx reg;
1616 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1617 /* The real hard regno of the operand after the allocation. */
1618 hard_regno[nop] = get_hard_regno (op);
1620 operand_reg[nop] = reg = op;
1621 biggest_mode[nop] = GET_MODE (op);
1622 if (GET_CODE (op) == SUBREG)
1624 operand_reg[nop] = reg = SUBREG_REG (op);
1625 if (GET_MODE_SIZE (biggest_mode[nop])
1626 < GET_MODE_SIZE (GET_MODE (reg)))
1627 biggest_mode[nop] = GET_MODE (reg);
1629 if (! REG_P (reg))
1630 operand_reg[nop] = NULL_RTX;
1631 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1632 || ((int) REGNO (reg)
1633 == lra_get_elimination_hard_regno (REGNO (reg))))
1634 no_subreg_reg_operand[nop] = reg;
1635 else
1636 operand_reg[nop] = no_subreg_reg_operand[nop]
1637 /* Just use natural mode for elimination result. It should
1638 be enough for extra constraints hooks. */
1639 = regno_reg_rtx[hard_regno[nop]];
1642 /* The constraints are made of several alternatives. Each operand's
1643 constraint looks like foo,bar,... with commas separating the
1644 alternatives. The first alternatives for all operands go
1645 together, the second alternatives go together, etc.
1647 First loop over alternatives. */
1648 for (nalt = 0; nalt < n_alternatives; nalt++)
1650 /* Loop over operands for one constraint alternative. */
1651 #if HAVE_ATTR_enabled
1652 if (curr_id->alternative_enabled_p != NULL
1653 && ! curr_id->alternative_enabled_p[nalt])
1654 continue;
1655 #endif
1657 if (only_alternative >= 0 && nalt != only_alternative)
1658 continue;
1661 overall = losers = reject = reload_nregs = reload_sum = 0;
1662 for (nop = 0; nop < n_operands; nop++)
1664 int inc = (curr_static_id
1665 ->operand_alternative[nalt * n_operands + nop].reject);
1666 if (lra_dump_file != NULL && inc != 0)
1667 fprintf (lra_dump_file,
1668 " Staticly defined alt reject+=%d\n", inc);
1669 reject += inc;
1671 early_clobbered_regs_num = 0;
1673 for (nop = 0; nop < n_operands; nop++)
1675 const char *p;
1676 char *end;
1677 int len, c, m, i, opalt_num, this_alternative_matches;
1678 bool win, did_match, offmemok, early_clobber_p;
1679 /* false => this operand can be reloaded somehow for this
1680 alternative. */
1681 bool badop;
1682 /* true => this operand can be reloaded if the alternative
1683 allows regs. */
1684 bool winreg;
1685 /* True if a constant forced into memory would be OK for
1686 this operand. */
1687 bool constmemok;
1688 enum reg_class this_alternative, this_costly_alternative;
1689 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1690 bool this_alternative_match_win, this_alternative_win;
1691 bool this_alternative_offmemok;
1692 bool scratch_p;
1693 enum machine_mode mode;
1695 opalt_num = nalt * n_operands + nop;
1696 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1698 /* Fast track for no constraints at all. */
1699 curr_alt[nop] = NO_REGS;
1700 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1701 curr_alt_win[nop] = true;
1702 curr_alt_match_win[nop] = false;
1703 curr_alt_offmemok[nop] = false;
1704 curr_alt_matches[nop] = -1;
1705 continue;
1708 op = no_subreg_reg_operand[nop];
1709 mode = curr_operand_mode[nop];
1711 win = did_match = winreg = offmemok = constmemok = false;
1712 badop = true;
1714 early_clobber_p = false;
1715 p = curr_static_id->operand_alternative[opalt_num].constraint;
1717 this_costly_alternative = this_alternative = NO_REGS;
1718 /* We update set of possible hard regs besides its class
1719 because reg class might be inaccurate. For example,
1720 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1721 is translated in HI_REGS because classes are merged by
1722 pairs and there is no accurate intermediate class. */
1723 CLEAR_HARD_REG_SET (this_alternative_set);
1724 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1725 this_alternative_win = false;
1726 this_alternative_match_win = false;
1727 this_alternative_offmemok = false;
1728 this_alternative_matches = -1;
1730 /* An empty constraint should be excluded by the fast
1731 track. */
1732 lra_assert (*p != 0 && *p != ',');
1734 /* Scan this alternative's specs for this operand; set WIN
1735 if the operand fits any letter in this alternative.
1736 Otherwise, clear BADOP if this operand could fit some
1737 letter after reloads, or set WINREG if this operand could
1738 fit after reloads provided the constraint allows some
1739 registers. */
1740 costly_p = false;
1743 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1745 case '\0':
1746 len = 0;
1747 break;
1748 case ',':
1749 c = '\0';
1750 break;
1752 case '=': case '+': case '?': case '*': case '!':
1753 case ' ': case '\t':
1754 break;
1756 case '%':
1757 /* We only support one commutative marker, the first
1758 one. We already set commutative above. */
1759 break;
1761 case '&':
1762 early_clobber_p = true;
1763 break;
1765 case '#':
1766 /* Ignore rest of this alternative. */
1767 c = '\0';
1768 break;
1770 case '0': case '1': case '2': case '3': case '4':
1771 case '5': case '6': case '7': case '8': case '9':
1773 int m_hregno;
1774 bool match_p;
1776 m = strtoul (p, &end, 10);
1777 p = end;
1778 len = 0;
1779 lra_assert (nop > m);
1781 this_alternative_matches = m;
1782 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1783 /* We are supposed to match a previous operand.
1784 If we do, we win if that one did. If we do
1785 not, count both of the operands as losers.
1786 (This is too conservative, since most of the
1787 time only a single reload insn will be needed
1788 to make the two operands win. As a result,
1789 this alternative may be rejected when it is
1790 actually desirable.) */
1791 match_p = false;
1792 if (operands_match_p (*curr_id->operand_loc[nop],
1793 *curr_id->operand_loc[m], m_hregno))
1795 /* We should reject matching of an early
1796 clobber operand if the matching operand is
1797 not dying in the insn. */
1798 if (! curr_static_id->operand[m].early_clobber
1799 || operand_reg[nop] == NULL_RTX
1800 || (find_regno_note (curr_insn, REG_DEAD,
1801 REGNO (op))
1802 || REGNO (op) == REGNO (operand_reg[m])))
1803 match_p = true;
1805 if (match_p)
1807 /* If we are matching a non-offsettable
1808 address where an offsettable address was
1809 expected, then we must reject this
1810 combination, because we can't reload
1811 it. */
1812 if (curr_alt_offmemok[m]
1813 && MEM_P (*curr_id->operand_loc[m])
1814 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1815 continue;
1817 else
1819 /* Operands don't match. Both operands must
1820 allow a reload register, otherwise we
1821 cannot make them match. */
1822 if (curr_alt[m] == NO_REGS)
1823 break;
1824 /* Retroactively mark the operand we had to
1825 match as a loser, if it wasn't already and
1826 it wasn't matched to a register constraint
1827 (e.g it might be matched by memory). */
1828 if (curr_alt_win[m]
1829 && (operand_reg[m] == NULL_RTX
1830 || hard_regno[m] < 0))
1832 losers++;
1833 reload_nregs
1834 += (ira_reg_class_max_nregs[curr_alt[m]]
1835 [GET_MODE (*curr_id->operand_loc[m])]);
1838 /* Prefer matching earlyclobber alternative as
1839 it results in less hard regs required for
1840 the insn than a non-matching earlyclobber
1841 alternative. */
1842 if (curr_static_id->operand[m].early_clobber)
1844 if (lra_dump_file != NULL)
1845 fprintf
1846 (lra_dump_file,
1847 " %d Matching earlyclobber alt:"
1848 " reject--\n",
1849 nop);
1850 reject--;
1852 /* Otherwise we prefer no matching
1853 alternatives because it gives more freedom
1854 in RA. */
1855 else if (operand_reg[nop] == NULL_RTX
1856 || (find_regno_note (curr_insn, REG_DEAD,
1857 REGNO (operand_reg[nop]))
1858 == NULL_RTX))
1860 if (lra_dump_file != NULL)
1861 fprintf
1862 (lra_dump_file,
1863 " %d Matching alt: reject+=2\n",
1864 nop);
1865 reject += 2;
1868 /* If we have to reload this operand and some
1869 previous operand also had to match the same
1870 thing as this operand, we don't know how to do
1871 that. */
1872 if (!match_p || !curr_alt_win[m])
1874 for (i = 0; i < nop; i++)
1875 if (curr_alt_matches[i] == m)
1876 break;
1877 if (i < nop)
1878 break;
1880 else
1881 did_match = true;
1883 /* This can be fixed with reloads if the operand
1884 we are supposed to match can be fixed with
1885 reloads. */
1886 badop = false;
1887 this_alternative = curr_alt[m];
1888 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1889 winreg = this_alternative != NO_REGS;
1890 break;
1893 case 'p':
1894 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1895 ADDRESS, SCRATCH);
1896 this_alternative = reg_class_subunion[this_alternative][cl];
1897 IOR_HARD_REG_SET (this_alternative_set,
1898 reg_class_contents[cl]);
1899 if (costly_p)
1901 this_costly_alternative
1902 = reg_class_subunion[this_costly_alternative][cl];
1903 IOR_HARD_REG_SET (this_costly_alternative_set,
1904 reg_class_contents[cl]);
1906 win = true;
1907 badop = false;
1908 break;
1910 case TARGET_MEM_CONSTRAINT:
1911 if (MEM_P (op) || spilled_pseudo_p (op))
1912 win = true;
1913 /* We can put constant or pseudo value into memory
1914 to satisfy the constraint. */
1915 if (CONST_POOL_OK_P (mode, op) || REG_P (op))
1916 badop = false;
1917 constmemok = true;
1918 break;
1920 case '<':
1921 if (MEM_P (op)
1922 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1923 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1924 win = true;
1925 break;
1927 case '>':
1928 if (MEM_P (op)
1929 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1930 || GET_CODE (XEXP (op, 0)) == POST_INC))
1931 win = true;
1932 break;
1934 /* Memory op whose address is not offsettable. */
1935 case 'V':
1936 if (MEM_P (op)
1937 && ! offsettable_nonstrict_memref_p (op))
1938 win = true;
1939 break;
1941 /* Memory operand whose address is offsettable. */
1942 case 'o':
1943 if ((MEM_P (op)
1944 && offsettable_nonstrict_memref_p (op))
1945 || spilled_pseudo_p (op))
1946 win = true;
1947 /* We can put constant or pseudo value into memory
1948 or make memory address offsetable to satisfy the
1949 constraint. */
1950 if (CONST_POOL_OK_P (mode, op) || MEM_P (op) || REG_P (op))
1951 badop = false;
1952 constmemok = true;
1953 offmemok = true;
1954 break;
1956 case 'E':
1957 case 'F':
1958 if (GET_CODE (op) == CONST_DOUBLE
1959 || (GET_CODE (op) == CONST_VECTOR
1960 && (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)))
1961 win = true;
1962 break;
1964 case 'G':
1965 case 'H':
1966 if (CONST_DOUBLE_AS_FLOAT_P (op)
1967 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1968 win = true;
1969 break;
1971 case 's':
1972 if (CONST_SCALAR_INT_P (op))
1973 break;
1975 case 'i':
1976 if (general_constant_p (op))
1977 win = true;
1978 break;
1980 case 'n':
1981 if (CONST_SCALAR_INT_P (op))
1982 win = true;
1983 break;
1985 case 'I':
1986 case 'J':
1987 case 'K':
1988 case 'L':
1989 case 'M':
1990 case 'N':
1991 case 'O':
1992 case 'P':
1993 if (CONST_INT_P (op)
1994 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1995 win = true;
1996 break;
1998 case 'X':
1999 /* This constraint should be excluded by the fast
2000 track. */
2001 gcc_unreachable ();
2002 break;
2004 case 'g':
2005 if (MEM_P (op)
2006 || general_constant_p (op)
2007 || spilled_pseudo_p (op))
2008 win = true;
2009 /* Drop through into 'r' case. */
2011 case 'r':
2012 this_alternative
2013 = reg_class_subunion[this_alternative][GENERAL_REGS];
2014 IOR_HARD_REG_SET (this_alternative_set,
2015 reg_class_contents[GENERAL_REGS]);
2016 if (costly_p)
2018 this_costly_alternative
2019 = (reg_class_subunion
2020 [this_costly_alternative][GENERAL_REGS]);
2021 IOR_HARD_REG_SET (this_costly_alternative_set,
2022 reg_class_contents[GENERAL_REGS]);
2024 goto reg;
2026 default:
2027 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
2029 #ifdef EXTRA_CONSTRAINT_STR
2030 if (EXTRA_MEMORY_CONSTRAINT (c, p))
2032 if (satisfies_memory_constraint_p (op, p))
2033 win = true;
2034 else if (spilled_pseudo_p (op))
2035 win = true;
2037 /* If we didn't already win, we can reload
2038 constants via force_const_mem or put the
2039 pseudo value into memory, or make other
2040 memory by reloading the address like for
2041 'o'. */
2042 if (CONST_POOL_OK_P (mode, op)
2043 || MEM_P (op) || REG_P (op))
2044 badop = false;
2045 constmemok = true;
2046 offmemok = true;
2047 break;
2049 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
2051 if (satisfies_address_constraint_p (op, p))
2052 win = true;
2054 /* If we didn't already win, we can reload
2055 the address into a base register. */
2056 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2057 ADDRESS, SCRATCH);
2058 this_alternative
2059 = reg_class_subunion[this_alternative][cl];
2060 IOR_HARD_REG_SET (this_alternative_set,
2061 reg_class_contents[cl]);
2062 if (costly_p)
2064 this_costly_alternative
2065 = (reg_class_subunion
2066 [this_costly_alternative][cl]);
2067 IOR_HARD_REG_SET (this_costly_alternative_set,
2068 reg_class_contents[cl]);
2070 badop = false;
2071 break;
2074 if (EXTRA_CONSTRAINT_STR (op, c, p))
2075 win = true;
2076 #endif
2077 break;
2080 cl = REG_CLASS_FROM_CONSTRAINT (c, p);
2081 this_alternative = reg_class_subunion[this_alternative][cl];
2082 IOR_HARD_REG_SET (this_alternative_set,
2083 reg_class_contents[cl]);
2084 if (costly_p)
2086 this_costly_alternative
2087 = reg_class_subunion[this_costly_alternative][cl];
2088 IOR_HARD_REG_SET (this_costly_alternative_set,
2089 reg_class_contents[cl]);
2091 reg:
2092 if (mode == BLKmode)
2093 break;
2094 winreg = true;
2095 if (REG_P (op))
2097 if (hard_regno[nop] >= 0
2098 && in_hard_reg_set_p (this_alternative_set,
2099 mode, hard_regno[nop]))
2100 win = true;
2101 else if (hard_regno[nop] < 0
2102 && in_class_p (op, this_alternative, NULL))
2103 win = true;
2105 break;
2107 if (c != ' ' && c != '\t')
2108 costly_p = c == '*';
2110 while ((p += len), c);
2112 scratch_p = (operand_reg[nop] != NULL_RTX
2113 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2114 /* Record which operands fit this alternative. */
2115 if (win)
2117 this_alternative_win = true;
2118 if (operand_reg[nop] != NULL_RTX)
2120 if (hard_regno[nop] >= 0)
2122 if (in_hard_reg_set_p (this_costly_alternative_set,
2123 mode, hard_regno[nop]))
2125 if (lra_dump_file != NULL)
2126 fprintf (lra_dump_file,
2127 " %d Costly set: reject++\n",
2128 nop);
2129 reject++;
2132 else
2134 /* Prefer won reg to spilled pseudo under other
2135 equal conditions for possibe inheritance. */
2136 if (! scratch_p)
2138 if (lra_dump_file != NULL)
2139 fprintf
2140 (lra_dump_file,
2141 " %d Non pseudo reload: reject++\n",
2142 nop);
2143 reject++;
2145 if (in_class_p (operand_reg[nop],
2146 this_costly_alternative, NULL))
2148 if (lra_dump_file != NULL)
2149 fprintf
2150 (lra_dump_file,
2151 " %d Non pseudo costly reload:"
2152 " reject++\n",
2153 nop);
2154 reject++;
2157 /* We simulate the behaviour of old reload here.
2158 Although scratches need hard registers and it
2159 might result in spilling other pseudos, no reload
2160 insns are generated for the scratches. So it
2161 might cost something but probably less than old
2162 reload pass believes. */
2163 if (scratch_p)
2165 if (lra_dump_file != NULL)
2166 fprintf (lra_dump_file,
2167 " %d Scratch win: reject+=2\n",
2168 nop);
2169 reject += 2;
2173 else if (did_match)
2174 this_alternative_match_win = true;
2175 else
2177 int const_to_mem = 0;
2178 bool no_regs_p;
2180 /* Never do output reload of stack pointer. It makes
2181 impossible to do elimination when SP is changed in
2182 RTL. */
2183 if (op == stack_pointer_rtx && ! frame_pointer_needed
2184 && curr_static_id->operand[nop].type != OP_IN)
2185 goto fail;
2187 /* If this alternative asks for a specific reg class, see if there
2188 is at least one allocatable register in that class. */
2189 no_regs_p
2190 = (this_alternative == NO_REGS
2191 || (hard_reg_set_subset_p
2192 (reg_class_contents[this_alternative],
2193 lra_no_alloc_regs)));
2195 /* For asms, verify that the class for this alternative is possible
2196 for the mode that is specified. */
2197 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2199 int i;
2200 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2201 if (HARD_REGNO_MODE_OK (i, mode)
2202 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2203 mode, i))
2204 break;
2205 if (i == FIRST_PSEUDO_REGISTER)
2206 winreg = false;
2209 /* If this operand accepts a register, and if the
2210 register class has at least one allocatable register,
2211 then this operand can be reloaded. */
2212 if (winreg && !no_regs_p)
2213 badop = false;
2215 if (badop)
2217 if (lra_dump_file != NULL)
2218 fprintf (lra_dump_file,
2219 " alt=%d: Bad operand -- refuse\n",
2220 nalt);
2221 goto fail;
2224 /* If not assigned pseudo has a class which a subset of
2225 required reg class, it is a less costly alternative
2226 as the pseudo still can get a hard reg of necessary
2227 class. */
2228 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2229 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2230 && ira_class_subset_p[this_alternative][cl])
2232 if (lra_dump_file != NULL)
2233 fprintf
2234 (lra_dump_file,
2235 " %d Super set class reg: reject-=3\n", nop);
2236 reject -= 3;
2239 this_alternative_offmemok = offmemok;
2240 if (this_costly_alternative != NO_REGS)
2242 if (lra_dump_file != NULL)
2243 fprintf (lra_dump_file,
2244 " %d Costly loser: reject++\n", nop);
2245 reject++;
2247 /* If the operand is dying, has a matching constraint,
2248 and satisfies constraints of the matched operand
2249 which failed to satisfy the own constraints, most probably
2250 the reload for this operand will be gone. */
2251 if (this_alternative_matches >= 0
2252 && !curr_alt_win[this_alternative_matches]
2253 && REG_P (op)
2254 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2255 && (hard_regno[nop] >= 0
2256 ? in_hard_reg_set_p (this_alternative_set,
2257 mode, hard_regno[nop])
2258 : in_class_p (op, this_alternative, NULL)))
2260 if (lra_dump_file != NULL)
2261 fprintf
2262 (lra_dump_file,
2263 " %d Dying matched operand reload: reject++\n",
2264 nop);
2265 reject++;
2267 else
2269 /* Strict_low_part requires to reload the register
2270 not the sub-register. In this case we should
2271 check that a final reload hard reg can hold the
2272 value mode. */
2273 if (curr_static_id->operand[nop].strict_low
2274 && REG_P (op)
2275 && hard_regno[nop] < 0
2276 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2277 && ira_class_hard_regs_num[this_alternative] > 0
2278 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2279 [this_alternative][0],
2280 GET_MODE
2281 (*curr_id->operand_loc[nop])))
2283 if (lra_dump_file != NULL)
2284 fprintf
2285 (lra_dump_file,
2286 " alt=%d: Strict low subreg reload -- refuse\n",
2287 nalt);
2288 goto fail;
2290 losers++;
2292 if (operand_reg[nop] != NULL_RTX
2293 /* Output operands and matched input operands are
2294 not inherited. The following conditions do not
2295 exactly describe the previous statement but they
2296 are pretty close. */
2297 && curr_static_id->operand[nop].type != OP_OUT
2298 && (this_alternative_matches < 0
2299 || curr_static_id->operand[nop].type != OP_IN))
2301 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2302 (operand_reg[nop])]
2303 .last_reload);
2305 /* The value of reload_sum has sense only if we
2306 process insns in their order. It happens only on
2307 the first constraints sub-pass when we do most of
2308 reload work. */
2309 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2310 reload_sum += last_reload - bb_reload_num;
2312 /* If this is a constant that is reloaded into the
2313 desired class by copying it to memory first, count
2314 that as another reload. This is consistent with
2315 other code and is required to avoid choosing another
2316 alternative when the constant is moved into memory.
2317 Note that the test here is precisely the same as in
2318 the code below that calls force_const_mem. */
2319 if (CONST_POOL_OK_P (mode, op)
2320 && ((targetm.preferred_reload_class
2321 (op, this_alternative) == NO_REGS)
2322 || no_input_reloads_p))
2324 const_to_mem = 1;
2325 if (! no_regs_p)
2326 losers++;
2329 /* Alternative loses if it requires a type of reload not
2330 permitted for this insn. We can always reload
2331 objects with a REG_UNUSED note. */
2332 if ((curr_static_id->operand[nop].type != OP_IN
2333 && no_output_reloads_p
2334 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2335 || (curr_static_id->operand[nop].type != OP_OUT
2336 && no_input_reloads_p && ! const_to_mem)
2337 || (this_alternative_matches >= 0
2338 && (no_input_reloads_p
2339 || (no_output_reloads_p
2340 && (curr_static_id->operand
2341 [this_alternative_matches].type != OP_IN)
2342 && ! find_reg_note (curr_insn, REG_UNUSED,
2343 no_subreg_reg_operand
2344 [this_alternative_matches])))))
2346 if (lra_dump_file != NULL)
2347 fprintf
2348 (lra_dump_file,
2349 " alt=%d: No input/otput reload -- refuse\n",
2350 nalt);
2351 goto fail;
2354 /* Check strong discouragement of reload of non-constant
2355 into class THIS_ALTERNATIVE. */
2356 if (! CONSTANT_P (op) && ! no_regs_p
2357 && (targetm.preferred_reload_class
2358 (op, this_alternative) == NO_REGS
2359 || (curr_static_id->operand[nop].type == OP_OUT
2360 && (targetm.preferred_output_reload_class
2361 (op, this_alternative) == NO_REGS))))
2363 if (lra_dump_file != NULL)
2364 fprintf (lra_dump_file,
2365 " %d Non-prefered reload: reject+=%d\n",
2366 nop, LRA_MAX_REJECT);
2367 reject += LRA_MAX_REJECT;
2370 if (! (MEM_P (op) && offmemok)
2371 && ! (const_to_mem && constmemok))
2373 /* We prefer to reload pseudos over reloading other
2374 things, since such reloads may be able to be
2375 eliminated later. So bump REJECT in other cases.
2376 Don't do this in the case where we are forcing a
2377 constant into memory and it will then win since
2378 we don't want to have a different alternative
2379 match then. */
2380 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2382 if (lra_dump_file != NULL)
2383 fprintf
2384 (lra_dump_file,
2385 " %d Non-pseudo reload: reject+=2\n",
2386 nop);
2387 reject += 2;
2390 if (! no_regs_p)
2391 reload_nregs
2392 += ira_reg_class_max_nregs[this_alternative][mode];
2394 if (SMALL_REGISTER_CLASS_P (this_alternative))
2396 if (lra_dump_file != NULL)
2397 fprintf
2398 (lra_dump_file,
2399 " %d Small class reload: reject+=%d\n",
2400 nop, LRA_LOSER_COST_FACTOR / 2);
2401 reject += LRA_LOSER_COST_FACTOR / 2;
2405 /* We are trying to spill pseudo into memory. It is
2406 usually more costly than moving to a hard register
2407 although it might takes the same number of
2408 reloads. */
2409 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2411 if (lra_dump_file != NULL)
2412 fprintf
2413 (lra_dump_file,
2414 " %d Spill pseudo into memory: reject+=3\n",
2415 nop);
2416 reject += 3;
2417 if (VECTOR_MODE_P (mode))
2419 /* Spilling vectors into memory is usually more
2420 costly as they contain big values. */
2421 if (lra_dump_file != NULL)
2422 fprintf
2423 (lra_dump_file,
2424 " %d Spill vector pseudo: reject+=2\n",
2425 nop);
2426 reject += 2;
2430 #ifdef SECONDARY_MEMORY_NEEDED
2431 /* If reload requires moving value through secondary
2432 memory, it will need one more insn at least. */
2433 if (this_alternative != NO_REGS
2434 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2435 && ((curr_static_id->operand[nop].type != OP_OUT
2436 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2437 GET_MODE (op)))
2438 || (curr_static_id->operand[nop].type != OP_IN
2439 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2440 GET_MODE (op)))))
2441 losers++;
2442 #endif
2443 /* Input reloads can be inherited more often than output
2444 reloads can be removed, so penalize output
2445 reloads. */
2446 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2448 if (lra_dump_file != NULL)
2449 fprintf
2450 (lra_dump_file,
2451 " %d Non input pseudo reload: reject++\n",
2452 nop);
2453 reject++;
2457 if (early_clobber_p && ! scratch_p)
2459 if (lra_dump_file != NULL)
2460 fprintf (lra_dump_file,
2461 " %d Early clobber: reject++\n", nop);
2462 reject++;
2464 /* ??? We check early clobbers after processing all operands
2465 (see loop below) and there we update the costs more.
2466 Should we update the cost (may be approximately) here
2467 because of early clobber register reloads or it is a rare
2468 or non-important thing to be worth to do it. */
2469 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2470 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2472 if (lra_dump_file != NULL)
2473 fprintf (lra_dump_file,
2474 " alt=%d,overall=%d,losers=%d -- refuse\n",
2475 nalt, overall, losers);
2476 goto fail;
2479 curr_alt[nop] = this_alternative;
2480 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2481 curr_alt_win[nop] = this_alternative_win;
2482 curr_alt_match_win[nop] = this_alternative_match_win;
2483 curr_alt_offmemok[nop] = this_alternative_offmemok;
2484 curr_alt_matches[nop] = this_alternative_matches;
2486 if (this_alternative_matches >= 0
2487 && !did_match && !this_alternative_win)
2488 curr_alt_win[this_alternative_matches] = false;
2490 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2491 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2493 if (curr_insn_set != NULL_RTX && n_operands == 2
2494 /* Prevent processing non-move insns. */
2495 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2496 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2497 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2498 && REG_P (no_subreg_reg_operand[0])
2499 && REG_P (no_subreg_reg_operand[1])
2500 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2501 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2502 || (! curr_alt_win[0] && curr_alt_win[1]
2503 && REG_P (no_subreg_reg_operand[1])
2504 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2505 || (curr_alt_win[0] && ! curr_alt_win[1]
2506 && REG_P (no_subreg_reg_operand[0])
2507 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2508 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2509 no_subreg_reg_operand[1])
2510 || (targetm.preferred_reload_class
2511 (no_subreg_reg_operand[1],
2512 (enum reg_class) curr_alt[1]) != NO_REGS))
2513 /* If it is a result of recent elimination in move
2514 insn we can transform it into an add still by
2515 using this alternative. */
2516 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2518 /* We have a move insn and a new reload insn will be similar
2519 to the current insn. We should avoid such situation as it
2520 results in LRA cycling. */
2521 overall += LRA_MAX_REJECT;
2523 ok_p = true;
2524 curr_alt_dont_inherit_ops_num = 0;
2525 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2527 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2528 HARD_REG_SET temp_set;
2530 i = early_clobbered_nops[nop];
2531 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2532 || hard_regno[i] < 0)
2533 continue;
2534 lra_assert (operand_reg[i] != NULL_RTX);
2535 clobbered_hard_regno = hard_regno[i];
2536 CLEAR_HARD_REG_SET (temp_set);
2537 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2538 first_conflict_j = last_conflict_j = -1;
2539 for (j = 0; j < n_operands; j++)
2540 if (j == i
2541 /* We don't want process insides of match_operator and
2542 match_parallel because otherwise we would process
2543 their operands once again generating a wrong
2544 code. */
2545 || curr_static_id->operand[j].is_operator)
2546 continue;
2547 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2548 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2549 continue;
2550 /* If we don't reload j-th operand, check conflicts. */
2551 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2552 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2554 if (first_conflict_j < 0)
2555 first_conflict_j = j;
2556 last_conflict_j = j;
2558 if (last_conflict_j < 0)
2559 continue;
2560 /* If earlyclobber operand conflicts with another
2561 non-matching operand which is actually the same register
2562 as the earlyclobber operand, it is better to reload the
2563 another operand as an operand matching the earlyclobber
2564 operand can be also the same. */
2565 if (first_conflict_j == last_conflict_j
2566 && operand_reg[last_conflict_j]
2567 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2568 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2570 curr_alt_win[last_conflict_j] = false;
2571 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2572 = last_conflict_j;
2573 losers++;
2574 /* Early clobber was already reflected in REJECT. */
2575 lra_assert (reject > 0);
2576 if (lra_dump_file != NULL)
2577 fprintf
2578 (lra_dump_file,
2579 " %d Conflict early clobber reload: reject--\n",
2581 reject--;
2582 overall += LRA_LOSER_COST_FACTOR - 1;
2584 else
2586 /* We need to reload early clobbered register and the
2587 matched registers. */
2588 for (j = 0; j < n_operands; j++)
2589 if (curr_alt_matches[j] == i)
2591 curr_alt_match_win[j] = false;
2592 losers++;
2593 overall += LRA_LOSER_COST_FACTOR;
2595 if (! curr_alt_match_win[i])
2596 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2597 else
2599 /* Remember pseudos used for match reloads are never
2600 inherited. */
2601 lra_assert (curr_alt_matches[i] >= 0);
2602 curr_alt_win[curr_alt_matches[i]] = false;
2604 curr_alt_win[i] = curr_alt_match_win[i] = false;
2605 losers++;
2606 /* Early clobber was already reflected in REJECT. */
2607 lra_assert (reject > 0);
2608 if (lra_dump_file != NULL)
2609 fprintf
2610 (lra_dump_file,
2611 " %d Matched conflict early clobber reloads:"
2612 "reject--\n",
2614 reject--;
2615 overall += LRA_LOSER_COST_FACTOR - 1;
2618 if (lra_dump_file != NULL)
2619 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2620 nalt, overall, losers, reload_nregs);
2622 /* If this alternative can be made to work by reloading, and it
2623 needs less reloading than the others checked so far, record
2624 it as the chosen goal for reloading. */
2625 if ((best_losers != 0 && losers == 0)
2626 || (((best_losers == 0 && losers == 0)
2627 || (best_losers != 0 && losers != 0))
2628 && (best_overall > overall
2629 || (best_overall == overall
2630 /* If the cost of the reloads is the same,
2631 prefer alternative which requires minimal
2632 number of reload regs. */
2633 && (reload_nregs < best_reload_nregs
2634 || (reload_nregs == best_reload_nregs
2635 && (best_reload_sum < reload_sum
2636 || (best_reload_sum == reload_sum
2637 && nalt < goal_alt_number))))))))
2639 for (nop = 0; nop < n_operands; nop++)
2641 goal_alt_win[nop] = curr_alt_win[nop];
2642 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2643 goal_alt_matches[nop] = curr_alt_matches[nop];
2644 goal_alt[nop] = curr_alt[nop];
2645 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2647 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2648 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2649 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2650 goal_alt_swapped = curr_swapped;
2651 best_overall = overall;
2652 best_losers = losers;
2653 best_reload_nregs = reload_nregs;
2654 best_reload_sum = reload_sum;
2655 goal_alt_number = nalt;
2657 if (losers == 0)
2658 /* Everything is satisfied. Do not process alternatives
2659 anymore. */
2660 break;
2661 fail:
2664 return ok_p;
2667 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2668 static rtx
2669 base_plus_disp_to_reg (struct address_info *ad)
2671 enum reg_class cl;
2672 rtx new_reg;
2674 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2675 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2676 get_index_code (ad));
2677 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2678 cl, "base + disp");
2679 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2680 return new_reg;
2683 /* Make reload of index part of address AD. Return the new
2684 pseudo. */
2685 static rtx
2686 index_part_to_reg (struct address_info *ad)
2688 rtx new_reg;
2690 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2691 INDEX_REG_CLASS, "index term");
2692 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2693 GEN_INT (get_index_scale (ad)), new_reg, 1);
2694 return new_reg;
2697 /* Return true if we can add a displacement to address AD, even if that
2698 makes the address invalid. The fix-up code requires any new address
2699 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2700 static bool
2701 can_add_disp_p (struct address_info *ad)
2703 return (!ad->autoinc_p
2704 && ad->segment == NULL
2705 && ad->base == ad->base_term
2706 && ad->disp == ad->disp_term);
2709 /* Make equiv substitution in address AD. Return true if a substitution
2710 was made. */
2711 static bool
2712 equiv_address_substitution (struct address_info *ad)
2714 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2715 HOST_WIDE_INT disp, scale;
2716 bool change_p;
2718 base_term = strip_subreg (ad->base_term);
2719 if (base_term == NULL)
2720 base_reg = new_base_reg = NULL_RTX;
2721 else
2723 base_reg = *base_term;
2724 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2726 index_term = strip_subreg (ad->index_term);
2727 if (index_term == NULL)
2728 index_reg = new_index_reg = NULL_RTX;
2729 else
2731 index_reg = *index_term;
2732 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2734 if (base_reg == new_base_reg && index_reg == new_index_reg)
2735 return false;
2736 disp = 0;
2737 change_p = false;
2738 if (lra_dump_file != NULL)
2740 fprintf (lra_dump_file, "Changing address in insn %d ",
2741 INSN_UID (curr_insn));
2742 dump_value_slim (lra_dump_file, *ad->outer, 1);
2744 if (base_reg != new_base_reg)
2746 if (REG_P (new_base_reg))
2748 *base_term = new_base_reg;
2749 change_p = true;
2751 else if (GET_CODE (new_base_reg) == PLUS
2752 && REG_P (XEXP (new_base_reg, 0))
2753 && CONST_INT_P (XEXP (new_base_reg, 1))
2754 && can_add_disp_p (ad))
2756 disp += INTVAL (XEXP (new_base_reg, 1));
2757 *base_term = XEXP (new_base_reg, 0);
2758 change_p = true;
2760 if (ad->base_term2 != NULL)
2761 *ad->base_term2 = *ad->base_term;
2763 if (index_reg != new_index_reg)
2765 if (REG_P (new_index_reg))
2767 *index_term = new_index_reg;
2768 change_p = true;
2770 else if (GET_CODE (new_index_reg) == PLUS
2771 && REG_P (XEXP (new_index_reg, 0))
2772 && CONST_INT_P (XEXP (new_index_reg, 1))
2773 && can_add_disp_p (ad)
2774 && (scale = get_index_scale (ad)))
2776 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2777 *index_term = XEXP (new_index_reg, 0);
2778 change_p = true;
2781 if (disp != 0)
2783 if (ad->disp != NULL)
2784 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2785 else
2787 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2788 update_address (ad);
2790 change_p = true;
2792 if (lra_dump_file != NULL)
2794 if (! change_p)
2795 fprintf (lra_dump_file, " -- no change\n");
2796 else
2798 fprintf (lra_dump_file, " on equiv ");
2799 dump_value_slim (lra_dump_file, *ad->outer, 1);
2800 fprintf (lra_dump_file, "\n");
2803 return change_p;
2806 /* Major function to make reloads for an address in operand NOP.
2807 The supported cases are:
2809 1) an address that existed before LRA started, at which point it
2810 must have been valid. These addresses are subject to elimination
2811 and may have become invalid due to the elimination offset being out
2812 of range.
2814 2) an address created by forcing a constant to memory
2815 (force_const_to_mem). The initial form of these addresses might
2816 not be valid, and it is this function's job to make them valid.
2818 3) a frame address formed from a register and a (possibly zero)
2819 constant offset. As above, these addresses might not be valid and
2820 this function must make them so.
2822 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2823 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2824 address. Return true for any RTL change. */
2825 static bool
2826 process_address (int nop, rtx *before, rtx *after)
2828 struct address_info ad;
2829 rtx new_reg;
2830 rtx op = *curr_id->operand_loc[nop];
2831 const char *constraint = curr_static_id->operand[nop].constraint;
2832 bool change_p;
2834 if (constraint[0] == 'p'
2835 || EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint))
2836 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2837 else if (MEM_P (op))
2838 decompose_mem_address (&ad, op);
2839 else if (GET_CODE (op) == SUBREG
2840 && MEM_P (SUBREG_REG (op)))
2841 decompose_mem_address (&ad, SUBREG_REG (op));
2842 else
2843 return false;
2844 change_p = equiv_address_substitution (&ad);
2845 if (ad.base_term != NULL
2846 && (process_addr_reg
2847 (ad.base_term, before,
2848 (ad.autoinc_p
2849 && !(REG_P (*ad.base_term)
2850 && find_regno_note (curr_insn, REG_DEAD,
2851 REGNO (*ad.base_term)) != NULL_RTX)
2852 ? after : NULL),
2853 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2854 get_index_code (&ad)))))
2856 change_p = true;
2857 if (ad.base_term2 != NULL)
2858 *ad.base_term2 = *ad.base_term;
2860 if (ad.index_term != NULL
2861 && process_addr_reg (ad.index_term, before, NULL, INDEX_REG_CLASS))
2862 change_p = true;
2864 #ifdef EXTRA_CONSTRAINT_STR
2865 /* Target hooks sometimes reject extra constraint addresses -- use
2866 EXTRA_CONSTRAINT_STR for the validation. */
2867 if (constraint[0] != 'p'
2868 && EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint)
2869 && valid_address_p (&ad, constraint))
2870 return change_p;
2871 #endif
2873 /* There are three cases where the shape of *AD.INNER may now be invalid:
2875 1) the original address was valid, but either elimination or
2876 equiv_address_substitution was applied and that made
2877 the address invalid.
2879 2) the address is an invalid symbolic address created by
2880 force_const_to_mem.
2882 3) the address is a frame address with an invalid offset.
2884 All these cases involve a non-autoinc address, so there is no
2885 point revalidating other types. */
2886 if (ad.autoinc_p || valid_address_p (&ad))
2887 return change_p;
2889 /* Any index existed before LRA started, so we can assume that the
2890 presence and shape of the index is valid. */
2891 push_to_sequence (*before);
2892 lra_assert (ad.disp == ad.disp_term);
2893 if (ad.base == NULL)
2895 if (ad.index == NULL)
2897 int code = -1;
2898 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2899 SCRATCH, SCRATCH);
2900 rtx addr = *ad.inner;
2902 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2903 #ifdef HAVE_lo_sum
2905 rtx insn;
2906 rtx last = get_last_insn ();
2908 /* addr => lo_sum (new_base, addr), case (2) above. */
2909 insn = emit_insn (gen_rtx_SET
2910 (VOIDmode, new_reg,
2911 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2912 code = recog_memoized (insn);
2913 if (code >= 0)
2915 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2916 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2918 /* Try to put lo_sum into register. */
2919 insn = emit_insn (gen_rtx_SET
2920 (VOIDmode, new_reg,
2921 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2922 code = recog_memoized (insn);
2923 if (code >= 0)
2925 *ad.inner = new_reg;
2926 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2928 *ad.inner = addr;
2929 code = -1;
2935 if (code < 0)
2936 delete_insns_since (last);
2938 #endif
2939 if (code < 0)
2941 /* addr => new_base, case (2) above. */
2942 lra_emit_move (new_reg, addr);
2943 *ad.inner = new_reg;
2946 else
2948 /* index * scale + disp => new base + index * scale,
2949 case (1) above. */
2950 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
2951 GET_CODE (*ad.index));
2953 lra_assert (INDEX_REG_CLASS != NO_REGS);
2954 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2955 lra_emit_move (new_reg, *ad.disp);
2956 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2957 new_reg, *ad.index);
2960 else if (ad.index == NULL)
2962 int regno;
2963 enum reg_class cl;
2964 rtx set, insns, last_insn;
2965 /* base + disp => new base, cases (1) and (3) above. */
2966 /* Another option would be to reload the displacement into an
2967 index register. However, postreload has code to optimize
2968 address reloads that have the same base and different
2969 displacements, so reloading into an index register would
2970 not necessarily be a win. */
2971 start_sequence ();
2972 new_reg = base_plus_disp_to_reg (&ad);
2973 insns = get_insns ();
2974 last_insn = get_last_insn ();
2975 /* If we generated at least two insns, try last insn source as
2976 an address. If we succeed, we generate one less insn. */
2977 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
2978 && GET_CODE (SET_SRC (set)) == PLUS
2979 && REG_P (XEXP (SET_SRC (set), 0))
2980 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
2982 *ad.inner = SET_SRC (set);
2983 if (valid_address_p (ad.mode, *ad.outer, ad.as))
2985 *ad.base_term = XEXP (SET_SRC (set), 0);
2986 *ad.disp_term = XEXP (SET_SRC (set), 1);
2987 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2988 get_index_code (&ad));
2989 regno = REGNO (*ad.base_term);
2990 if (regno >= FIRST_PSEUDO_REGISTER
2991 && cl != lra_get_allocno_class (regno))
2992 lra_change_class (regno, cl, " Change to", true);
2993 new_reg = SET_SRC (set);
2994 delete_insns_since (PREV_INSN (last_insn));
2997 end_sequence ();
2998 emit_insn (insns);
2999 *ad.inner = new_reg;
3001 else if (ad.disp_term != NULL)
3003 /* base + scale * index + disp => new base + scale * index,
3004 case (1) above. */
3005 new_reg = base_plus_disp_to_reg (&ad);
3006 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3007 new_reg, *ad.index);
3009 else
3011 /* base + scale * index => base + new_reg,
3012 case (1) above.
3013 Index part of address may become invalid. For example, we
3014 changed pseudo on the equivalent memory and a subreg of the
3015 pseudo onto the memory of different mode for which the scale is
3016 prohibitted. */
3017 new_reg = index_part_to_reg (&ad);
3018 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3019 *ad.base_term, new_reg);
3021 *before = get_insns ();
3022 end_sequence ();
3023 return true;
3026 /* Emit insns to reload VALUE into a new register. VALUE is an
3027 auto-increment or auto-decrement RTX whose operand is a register or
3028 memory location; so reloading involves incrementing that location.
3029 IN is either identical to VALUE, or some cheaper place to reload
3030 value being incremented/decremented from.
3032 INC_AMOUNT is the number to increment or decrement by (always
3033 positive and ignored for POST_MODIFY/PRE_MODIFY).
3035 Return pseudo containing the result. */
3036 static rtx
3037 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3039 /* REG or MEM to be copied and incremented. */
3040 rtx incloc = XEXP (value, 0);
3041 /* Nonzero if increment after copying. */
3042 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3043 || GET_CODE (value) == POST_MODIFY);
3044 rtx last;
3045 rtx inc;
3046 rtx add_insn;
3047 int code;
3048 rtx real_in = in == value ? incloc : in;
3049 rtx result;
3050 bool plus_p = true;
3052 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3054 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3055 || GET_CODE (XEXP (value, 1)) == MINUS);
3056 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3057 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3058 inc = XEXP (XEXP (value, 1), 1);
3060 else
3062 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3063 inc_amount = -inc_amount;
3065 inc = GEN_INT (inc_amount);
3068 if (! post && REG_P (incloc))
3069 result = incloc;
3070 else
3071 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3072 "INC/DEC result");
3074 if (real_in != result)
3076 /* First copy the location to the result register. */
3077 lra_assert (REG_P (result));
3078 emit_insn (gen_move_insn (result, real_in));
3081 /* We suppose that there are insns to add/sub with the constant
3082 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3083 old reload worked with this assumption. If the assumption
3084 becomes wrong, we should use approach in function
3085 base_plus_disp_to_reg. */
3086 if (in == value)
3088 /* See if we can directly increment INCLOC. */
3089 last = get_last_insn ();
3090 add_insn = emit_insn (plus_p
3091 ? gen_add2_insn (incloc, inc)
3092 : gen_sub2_insn (incloc, inc));
3094 code = recog_memoized (add_insn);
3095 if (code >= 0)
3097 if (! post && result != incloc)
3098 emit_insn (gen_move_insn (result, incloc));
3099 return result;
3101 delete_insns_since (last);
3104 /* If couldn't do the increment directly, must increment in RESULT.
3105 The way we do this depends on whether this is pre- or
3106 post-increment. For pre-increment, copy INCLOC to the reload
3107 register, increment it there, then save back. */
3108 if (! post)
3110 if (real_in != result)
3111 emit_insn (gen_move_insn (result, real_in));
3112 if (plus_p)
3113 emit_insn (gen_add2_insn (result, inc));
3114 else
3115 emit_insn (gen_sub2_insn (result, inc));
3116 if (result != incloc)
3117 emit_insn (gen_move_insn (incloc, result));
3119 else
3121 /* Post-increment.
3123 Because this might be a jump insn or a compare, and because
3124 RESULT may not be available after the insn in an input
3125 reload, we must do the incrementing before the insn being
3126 reloaded for.
3128 We have already copied IN to RESULT. Increment the copy in
3129 RESULT, save that back, then decrement RESULT so it has
3130 the original value. */
3131 if (plus_p)
3132 emit_insn (gen_add2_insn (result, inc));
3133 else
3134 emit_insn (gen_sub2_insn (result, inc));
3135 emit_insn (gen_move_insn (incloc, result));
3136 /* Restore non-modified value for the result. We prefer this
3137 way because it does not require an additional hard
3138 register. */
3139 if (plus_p)
3141 if (CONST_INT_P (inc))
3142 emit_insn (gen_add2_insn (result,
3143 gen_int_mode (-INTVAL (inc),
3144 GET_MODE (result))));
3145 else
3146 emit_insn (gen_sub2_insn (result, inc));
3148 else
3149 emit_insn (gen_add2_insn (result, inc));
3151 return result;
3154 /* Return true if the current move insn does not need processing as we
3155 already know that it satisfies its constraints. */
3156 static bool
3157 simple_move_p (void)
3159 rtx dest, src;
3160 enum reg_class dclass, sclass;
3162 lra_assert (curr_insn_set != NULL_RTX);
3163 dest = SET_DEST (curr_insn_set);
3164 src = SET_SRC (curr_insn_set);
3165 return ((dclass = get_op_class (dest)) != NO_REGS
3166 && (sclass = get_op_class (src)) != NO_REGS
3167 /* The backend guarantees that register moves of cost 2
3168 never need reloads. */
3169 && targetm.register_move_cost (GET_MODE (src), dclass, sclass) == 2);
3172 /* Swap operands NOP and NOP + 1. */
3173 static inline void
3174 swap_operands (int nop)
3176 enum machine_mode mode = curr_operand_mode[nop];
3177 curr_operand_mode[nop] = curr_operand_mode[nop + 1];
3178 curr_operand_mode[nop + 1] = mode;
3179 rtx x = *curr_id->operand_loc[nop];
3180 *curr_id->operand_loc[nop] = *curr_id->operand_loc[nop + 1];
3181 *curr_id->operand_loc[nop + 1] = x;
3182 /* Swap the duplicates too. */
3183 lra_update_dup (curr_id, nop);
3184 lra_update_dup (curr_id, nop + 1);
3187 /* Main entry point of the constraint code: search the body of the
3188 current insn to choose the best alternative. It is mimicking insn
3189 alternative cost calculation model of former reload pass. That is
3190 because machine descriptions were written to use this model. This
3191 model can be changed in future. Make commutative operand exchange
3192 if it is chosen.
3194 Return true if some RTL changes happened during function call. */
3195 static bool
3196 curr_insn_transform (void)
3198 int i, j, k;
3199 int n_operands;
3200 int n_alternatives;
3201 int commutative;
3202 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3203 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3204 rtx before, after;
3205 bool alt_p = false;
3206 /* Flag that the insn has been changed through a transformation. */
3207 bool change_p;
3208 bool sec_mem_p;
3209 #ifdef SECONDARY_MEMORY_NEEDED
3210 bool use_sec_mem_p;
3211 #endif
3212 int max_regno_before;
3213 int reused_alternative_num;
3215 curr_insn_set = single_set (curr_insn);
3216 if (curr_insn_set != NULL_RTX && simple_move_p ())
3217 return false;
3219 no_input_reloads_p = no_output_reloads_p = false;
3220 goal_alt_number = -1;
3221 change_p = sec_mem_p = false;
3222 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3223 reloads; neither are insns that SET cc0. Insns that use CC0 are
3224 not allowed to have any input reloads. */
3225 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3226 no_output_reloads_p = true;
3228 #ifdef HAVE_cc0
3229 if (reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3230 no_input_reloads_p = true;
3231 if (reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3232 no_output_reloads_p = true;
3233 #endif
3235 n_operands = curr_static_id->n_operands;
3236 n_alternatives = curr_static_id->n_alternatives;
3238 /* Just return "no reloads" if insn has no operands with
3239 constraints. */
3240 if (n_operands == 0 || n_alternatives == 0)
3241 return false;
3243 max_regno_before = max_reg_num ();
3245 for (i = 0; i < n_operands; i++)
3247 goal_alt_matched[i][0] = -1;
3248 goal_alt_matches[i] = -1;
3251 commutative = curr_static_id->commutative;
3253 /* Now see what we need for pseudos that didn't get hard regs or got
3254 the wrong kind of hard reg. For this, we must consider all the
3255 operands together against the register constraints. */
3257 best_losers = best_overall = INT_MAX;
3258 best_reload_sum = 0;
3260 curr_swapped = false;
3261 goal_alt_swapped = false;
3263 /* Make equivalence substitution and memory subreg elimination
3264 before address processing because an address legitimacy can
3265 depend on memory mode. */
3266 for (i = 0; i < n_operands; i++)
3268 rtx op = *curr_id->operand_loc[i];
3269 rtx subst, old = op;
3270 bool op_change_p = false;
3272 if (GET_CODE (old) == SUBREG)
3273 old = SUBREG_REG (old);
3274 subst = get_equiv_with_elimination (old, curr_insn);
3275 if (subst != old)
3277 subst = copy_rtx (subst);
3278 lra_assert (REG_P (old));
3279 if (GET_CODE (op) == SUBREG)
3280 SUBREG_REG (op) = subst;
3281 else
3282 *curr_id->operand_loc[i] = subst;
3283 if (lra_dump_file != NULL)
3285 fprintf (lra_dump_file,
3286 "Changing pseudo %d in operand %i of insn %u on equiv ",
3287 REGNO (old), i, INSN_UID (curr_insn));
3288 dump_value_slim (lra_dump_file, subst, 1);
3289 fprintf (lra_dump_file, "\n");
3291 op_change_p = change_p = true;
3293 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3295 change_p = true;
3296 lra_update_dup (curr_id, i);
3300 /* Reload address registers and displacements. We do it before
3301 finding an alternative because of memory constraints. */
3302 before = after = NULL_RTX;
3303 for (i = 0; i < n_operands; i++)
3304 if (! curr_static_id->operand[i].is_operator
3305 && process_address (i, &before, &after))
3307 change_p = true;
3308 lra_update_dup (curr_id, i);
3311 if (change_p)
3312 /* If we've changed the instruction then any alternative that
3313 we chose previously may no longer be valid. */
3314 lra_set_used_insn_alternative (curr_insn, -1);
3316 if (curr_insn_set != NULL_RTX
3317 && check_and_process_move (&change_p, &sec_mem_p))
3318 return change_p;
3320 try_swapped:
3322 reused_alternative_num = curr_id->used_insn_alternative;
3323 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3324 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3325 reused_alternative_num, INSN_UID (curr_insn));
3327 if (process_alt_operands (reused_alternative_num))
3328 alt_p = true;
3330 /* If insn is commutative (it's safe to exchange a certain pair of
3331 operands) then we need to try each alternative twice, the second
3332 time matching those two operands as if we had exchanged them. To
3333 do this, really exchange them in operands.
3335 If we have just tried the alternatives the second time, return
3336 operands to normal and drop through. */
3338 if (reused_alternative_num < 0 && commutative >= 0)
3340 curr_swapped = !curr_swapped;
3341 if (curr_swapped)
3343 swap_operands (commutative);
3344 goto try_swapped;
3346 else
3347 swap_operands (commutative);
3350 if (! alt_p && ! sec_mem_p)
3352 /* No alternative works with reloads?? */
3353 if (INSN_CODE (curr_insn) >= 0)
3354 fatal_insn ("unable to generate reloads for:", curr_insn);
3355 error_for_asm (curr_insn,
3356 "inconsistent operand constraints in an %<asm%>");
3357 /* Avoid further trouble with this insn. */
3358 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3359 lra_invalidate_insn_data (curr_insn);
3360 return true;
3363 /* If the best alternative is with operands 1 and 2 swapped, swap
3364 them. Update the operand numbers of any reloads already
3365 pushed. */
3367 if (goal_alt_swapped)
3369 if (lra_dump_file != NULL)
3370 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3371 INSN_UID (curr_insn));
3373 /* Swap the duplicates too. */
3374 swap_operands (commutative);
3375 change_p = true;
3378 #ifdef SECONDARY_MEMORY_NEEDED
3379 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3380 too conservatively. So we use the secondary memory only if there
3381 is no any alternative without reloads. */
3382 use_sec_mem_p = false;
3383 if (! alt_p)
3384 use_sec_mem_p = true;
3385 else if (sec_mem_p)
3387 for (i = 0; i < n_operands; i++)
3388 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3389 break;
3390 use_sec_mem_p = i < n_operands;
3393 if (use_sec_mem_p)
3395 rtx new_reg, src, dest, rld;
3396 enum machine_mode sec_mode, rld_mode;
3398 lra_assert (sec_mem_p);
3399 lra_assert (curr_static_id->operand[0].type == OP_OUT
3400 && curr_static_id->operand[1].type == OP_IN);
3401 dest = *curr_id->operand_loc[0];
3402 src = *curr_id->operand_loc[1];
3403 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3404 ? dest : src);
3405 rld_mode = GET_MODE (rld);
3406 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3407 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3408 #else
3409 sec_mode = rld_mode;
3410 #endif
3411 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3412 NO_REGS, "secondary");
3413 /* If the mode is changed, it should be wider. */
3414 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3415 if (sec_mode != rld_mode)
3417 /* If the target says specifically to use another mode for
3418 secondary memory moves we can not reuse the original
3419 insn. */
3420 after = emit_spill_move (false, new_reg, dest);
3421 lra_process_new_insns (curr_insn, NULL_RTX, after,
3422 "Inserting the sec. move");
3423 /* We may have non null BEFORE here (e.g. after address
3424 processing. */
3425 push_to_sequence (before);
3426 before = emit_spill_move (true, new_reg, src);
3427 emit_insn (before);
3428 before = get_insns ();
3429 end_sequence ();
3430 lra_process_new_insns (curr_insn, before, NULL_RTX, "Changing on");
3431 lra_set_insn_deleted (curr_insn);
3433 else if (dest == rld)
3435 *curr_id->operand_loc[0] = new_reg;
3436 after = emit_spill_move (false, new_reg, dest);
3437 lra_process_new_insns (curr_insn, NULL_RTX, after,
3438 "Inserting the sec. move");
3440 else
3442 *curr_id->operand_loc[1] = new_reg;
3443 /* See comments above. */
3444 push_to_sequence (before);
3445 before = emit_spill_move (true, new_reg, src);
3446 emit_insn (before);
3447 before = get_insns ();
3448 end_sequence ();
3449 lra_process_new_insns (curr_insn, before, NULL_RTX,
3450 "Inserting the sec. move");
3452 lra_update_insn_regno_info (curr_insn);
3453 return true;
3455 #endif
3457 lra_assert (goal_alt_number >= 0);
3458 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3460 if (lra_dump_file != NULL)
3462 const char *p;
3464 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3465 goal_alt_number, INSN_UID (curr_insn));
3466 for (i = 0; i < n_operands; i++)
3468 p = (curr_static_id->operand_alternative
3469 [goal_alt_number * n_operands + i].constraint);
3470 if (*p == '\0')
3471 continue;
3472 fprintf (lra_dump_file, " (%d) ", i);
3473 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3474 fputc (*p, lra_dump_file);
3476 if (INSN_CODE (curr_insn) >= 0
3477 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3478 fprintf (lra_dump_file, " {%s}", p);
3479 if (curr_id->sp_offset != 0)
3480 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3481 curr_id->sp_offset);
3482 fprintf (lra_dump_file, "\n");
3485 /* Right now, for any pair of operands I and J that are required to
3486 match, with J < I, goal_alt_matches[I] is J. Add I to
3487 goal_alt_matched[J]. */
3489 for (i = 0; i < n_operands; i++)
3490 if ((j = goal_alt_matches[i]) >= 0)
3492 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3494 /* We allow matching one output operand and several input
3495 operands. */
3496 lra_assert (k == 0
3497 || (curr_static_id->operand[j].type == OP_OUT
3498 && curr_static_id->operand[i].type == OP_IN
3499 && (curr_static_id->operand
3500 [goal_alt_matched[j][0]].type == OP_IN)));
3501 goal_alt_matched[j][k] = i;
3502 goal_alt_matched[j][k + 1] = -1;
3505 for (i = 0; i < n_operands; i++)
3506 goal_alt_win[i] |= goal_alt_match_win[i];
3508 /* Any constants that aren't allowed and can't be reloaded into
3509 registers are here changed into memory references. */
3510 for (i = 0; i < n_operands; i++)
3511 if (goal_alt_win[i])
3513 int regno;
3514 enum reg_class new_class;
3515 rtx reg = *curr_id->operand_loc[i];
3517 if (GET_CODE (reg) == SUBREG)
3518 reg = SUBREG_REG (reg);
3520 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3522 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3524 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3526 lra_assert (ok_p);
3527 lra_change_class (regno, new_class, " Change to", true);
3531 else
3533 const char *constraint;
3534 char c;
3535 rtx op = *curr_id->operand_loc[i];
3536 rtx subreg = NULL_RTX;
3537 enum machine_mode mode = curr_operand_mode[i];
3539 if (GET_CODE (op) == SUBREG)
3541 subreg = op;
3542 op = SUBREG_REG (op);
3543 mode = GET_MODE (op);
3546 if (CONST_POOL_OK_P (mode, op)
3547 && ((targetm.preferred_reload_class
3548 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3549 || no_input_reloads_p))
3551 rtx tem = force_const_mem (mode, op);
3553 change_p = true;
3554 if (subreg != NULL_RTX)
3555 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3557 *curr_id->operand_loc[i] = tem;
3558 lra_update_dup (curr_id, i);
3559 process_address (i, &before, &after);
3561 /* If the alternative accepts constant pool refs directly
3562 there will be no reload needed at all. */
3563 if (subreg != NULL_RTX)
3564 continue;
3565 /* Skip alternatives before the one requested. */
3566 constraint = (curr_static_id->operand_alternative
3567 [goal_alt_number * n_operands + i].constraint);
3568 for (;
3569 (c = *constraint) && c != ',' && c != '#';
3570 constraint += CONSTRAINT_LEN (c, constraint))
3572 if (c == TARGET_MEM_CONSTRAINT || c == 'o')
3573 break;
3574 #ifdef EXTRA_CONSTRAINT_STR
3575 if (EXTRA_MEMORY_CONSTRAINT (c, constraint)
3576 && satisfies_memory_constraint_p (tem, constraint))
3577 break;
3578 #endif
3580 if (c == '\0' || c == ',' || c == '#')
3581 continue;
3583 goal_alt_win[i] = true;
3587 for (i = 0; i < n_operands; i++)
3589 int regno;
3590 bool optional_p = false;
3591 rtx old, new_reg;
3592 rtx op = *curr_id->operand_loc[i];
3594 if (goal_alt_win[i])
3596 if (goal_alt[i] == NO_REGS
3597 && REG_P (op)
3598 /* When we assign NO_REGS it means that we will not
3599 assign a hard register to the scratch pseudo by
3600 assigment pass and the scratch pseudo will be
3601 spilled. Spilled scratch pseudos are transformed
3602 back to scratches at the LRA end. */
3603 && lra_former_scratch_operand_p (curr_insn, i))
3605 int regno = REGNO (op);
3606 lra_change_class (regno, NO_REGS, " Change to", true);
3607 if (lra_get_regno_hard_regno (regno) >= 0)
3608 /* We don't have to mark all insn affected by the
3609 spilled pseudo as there is only one such insn, the
3610 current one. */
3611 reg_renumber[regno] = -1;
3613 /* We can do an optional reload. If the pseudo got a hard
3614 reg, we might improve the code through inheritance. If
3615 it does not get a hard register we coalesce memory/memory
3616 moves later. Ignore move insns to avoid cycling. */
3617 if (! lra_simple_p
3618 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3619 && goal_alt[i] != NO_REGS && REG_P (op)
3620 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3621 && regno < new_regno_start
3622 && ! lra_former_scratch_p (regno)
3623 && reg_renumber[regno] < 0
3624 && (curr_insn_set == NULL_RTX
3625 || !((REG_P (SET_SRC (curr_insn_set))
3626 || MEM_P (SET_SRC (curr_insn_set))
3627 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3628 && (REG_P (SET_DEST (curr_insn_set))
3629 || MEM_P (SET_DEST (curr_insn_set))
3630 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3631 optional_p = true;
3632 else
3633 continue;
3636 /* Operands that match previous ones have already been handled. */
3637 if (goal_alt_matches[i] >= 0)
3638 continue;
3640 /* We should not have an operand with a non-offsettable address
3641 appearing where an offsettable address will do. It also may
3642 be a case when the address should be special in other words
3643 not a general one (e.g. it needs no index reg). */
3644 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3646 enum reg_class rclass;
3647 rtx *loc = &XEXP (op, 0);
3648 enum rtx_code code = GET_CODE (*loc);
3650 push_to_sequence (before);
3651 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3652 MEM, SCRATCH);
3653 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3654 new_reg = emit_inc (rclass, *loc, *loc,
3655 /* This value does not matter for MODIFY. */
3656 GET_MODE_SIZE (GET_MODE (op)));
3657 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3658 "offsetable address", &new_reg))
3659 lra_emit_move (new_reg, *loc);
3660 before = get_insns ();
3661 end_sequence ();
3662 *loc = new_reg;
3663 lra_update_dup (curr_id, i);
3665 else if (goal_alt_matched[i][0] == -1)
3667 enum machine_mode mode;
3668 rtx reg, *loc;
3669 int hard_regno, byte;
3670 enum op_type type = curr_static_id->operand[i].type;
3672 loc = curr_id->operand_loc[i];
3673 mode = curr_operand_mode[i];
3674 if (GET_CODE (*loc) == SUBREG)
3676 reg = SUBREG_REG (*loc);
3677 byte = SUBREG_BYTE (*loc);
3678 if (REG_P (reg)
3679 /* Strict_low_part requires reload the register not
3680 the sub-register. */
3681 && (curr_static_id->operand[i].strict_low
3682 || (GET_MODE_SIZE (mode)
3683 <= GET_MODE_SIZE (GET_MODE (reg))
3684 && (hard_regno
3685 = get_try_hard_regno (REGNO (reg))) >= 0
3686 && (simplify_subreg_regno
3687 (hard_regno,
3688 GET_MODE (reg), byte, mode) < 0)
3689 && (goal_alt[i] == NO_REGS
3690 || (simplify_subreg_regno
3691 (ira_class_hard_regs[goal_alt[i]][0],
3692 GET_MODE (reg), byte, mode) >= 0)))))
3694 loc = &SUBREG_REG (*loc);
3695 mode = GET_MODE (*loc);
3698 old = *loc;
3699 if (get_reload_reg (type, mode, old, goal_alt[i],
3700 loc != curr_id->operand_loc[i], "", &new_reg)
3701 && type != OP_OUT)
3703 push_to_sequence (before);
3704 lra_emit_move (new_reg, old);
3705 before = get_insns ();
3706 end_sequence ();
3708 *loc = new_reg;
3709 if (type != OP_IN
3710 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3712 start_sequence ();
3713 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3714 emit_insn (after);
3715 after = get_insns ();
3716 end_sequence ();
3717 *loc = new_reg;
3719 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3720 if (goal_alt_dont_inherit_ops[j] == i)
3722 lra_set_regno_unique_value (REGNO (new_reg));
3723 break;
3725 lra_update_dup (curr_id, i);
3727 else if (curr_static_id->operand[i].type == OP_IN
3728 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3729 == OP_OUT))
3731 /* generate reloads for input and matched outputs. */
3732 match_inputs[0] = i;
3733 match_inputs[1] = -1;
3734 match_reload (goal_alt_matched[i][0], match_inputs,
3735 goal_alt[i], &before, &after);
3737 else if (curr_static_id->operand[i].type == OP_OUT
3738 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3739 == OP_IN))
3740 /* Generate reloads for output and matched inputs. */
3741 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3742 else if (curr_static_id->operand[i].type == OP_IN
3743 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3744 == OP_IN))
3746 /* Generate reloads for matched inputs. */
3747 match_inputs[0] = i;
3748 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3749 match_inputs[j + 1] = k;
3750 match_inputs[j + 1] = -1;
3751 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3753 else
3754 /* We must generate code in any case when function
3755 process_alt_operands decides that it is possible. */
3756 gcc_unreachable ();
3757 if (optional_p)
3759 lra_assert (REG_P (op));
3760 regno = REGNO (op);
3761 op = *curr_id->operand_loc[i]; /* Substitution. */
3762 if (GET_CODE (op) == SUBREG)
3763 op = SUBREG_REG (op);
3764 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3765 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3766 lra_reg_info[REGNO (op)].restore_regno = regno;
3767 if (lra_dump_file != NULL)
3768 fprintf (lra_dump_file,
3769 " Making reload reg %d for reg %d optional\n",
3770 REGNO (op), regno);
3773 if (before != NULL_RTX || after != NULL_RTX
3774 || max_regno_before != max_reg_num ())
3775 change_p = true;
3776 if (change_p)
3778 lra_update_operator_dups (curr_id);
3779 /* Something changes -- process the insn. */
3780 lra_update_insn_regno_info (curr_insn);
3782 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3783 return change_p;
3786 /* Return true if X is in LIST. */
3787 static bool
3788 in_list_p (rtx x, rtx list)
3790 for (; list != NULL_RTX; list = XEXP (list, 1))
3791 if (XEXP (list, 0) == x)
3792 return true;
3793 return false;
3796 /* Return true if X contains an allocatable hard register (if
3797 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3798 static bool
3799 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3801 int i, j;
3802 const char *fmt;
3803 enum rtx_code code;
3805 code = GET_CODE (x);
3806 if (REG_P (x))
3808 int regno = REGNO (x);
3809 HARD_REG_SET alloc_regs;
3811 if (hard_reg_p)
3813 if (regno >= FIRST_PSEUDO_REGISTER)
3814 regno = lra_get_regno_hard_regno (regno);
3815 if (regno < 0)
3816 return false;
3817 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3818 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3820 else
3822 if (regno < FIRST_PSEUDO_REGISTER)
3823 return false;
3824 if (! spilled_p)
3825 return true;
3826 return lra_get_regno_hard_regno (regno) < 0;
3829 fmt = GET_RTX_FORMAT (code);
3830 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3832 if (fmt[i] == 'e')
3834 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3835 return true;
3837 else if (fmt[i] == 'E')
3839 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3840 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3841 return true;
3844 return false;
3847 /* Process all regs in location *LOC and change them on equivalent
3848 substitution. Return true if any change was done. */
3849 static bool
3850 loc_equivalence_change_p (rtx *loc)
3852 rtx subst, reg, x = *loc;
3853 bool result = false;
3854 enum rtx_code code = GET_CODE (x);
3855 const char *fmt;
3856 int i, j;
3858 if (code == SUBREG)
3860 reg = SUBREG_REG (x);
3861 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
3862 && GET_MODE (subst) == VOIDmode)
3864 /* We cannot reload debug location. Simplify subreg here
3865 while we know the inner mode. */
3866 *loc = simplify_gen_subreg (GET_MODE (x), subst,
3867 GET_MODE (reg), SUBREG_BYTE (x));
3868 return true;
3871 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
3873 *loc = subst;
3874 return true;
3877 /* Scan all the operand sub-expressions. */
3878 fmt = GET_RTX_FORMAT (code);
3879 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3881 if (fmt[i] == 'e')
3882 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
3883 else if (fmt[i] == 'E')
3884 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3885 result
3886 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
3888 return result;
3891 /* Similar to loc_equivalence_change_p, but for use as
3892 simplify_replace_fn_rtx callback. DATA is insn for which the
3893 elimination is done. If it null we don't do the elimination. */
3894 static rtx
3895 loc_equivalence_callback (rtx loc, const_rtx, void *data)
3897 if (!REG_P (loc))
3898 return NULL_RTX;
3900 rtx subst = (data == NULL
3901 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx) data));
3902 if (subst != loc)
3903 return subst;
3905 return NULL_RTX;
3908 /* Maximum number of generated reload insns per an insn. It is for
3909 preventing this pass cycling in a bug case. */
3910 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
3912 /* The current iteration number of this LRA pass. */
3913 int lra_constraint_iter;
3915 /* The current iteration number of this LRA pass after the last spill
3916 pass. */
3917 int lra_constraint_iter_after_spill;
3919 /* True if we substituted equiv which needs checking register
3920 allocation correctness because the equivalent value contains
3921 allocatable hard registers or when we restore multi-register
3922 pseudo. */
3923 bool lra_risky_transformations_p;
3925 /* Return true if REGNO is referenced in more than one block. */
3926 static bool
3927 multi_block_pseudo_p (int regno)
3929 basic_block bb = NULL;
3930 unsigned int uid;
3931 bitmap_iterator bi;
3933 if (regno < FIRST_PSEUDO_REGISTER)
3934 return false;
3936 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
3937 if (bb == NULL)
3938 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
3939 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
3940 return true;
3941 return false;
3944 /* Return true if LIST contains a deleted insn. */
3945 static bool
3946 contains_deleted_insn_p (rtx list)
3948 for (; list != NULL_RTX; list = XEXP (list, 1))
3949 if (NOTE_P (XEXP (list, 0))
3950 && NOTE_KIND (XEXP (list, 0)) == NOTE_INSN_DELETED)
3951 return true;
3952 return false;
3955 /* Return true if X contains a pseudo dying in INSN. */
3956 static bool
3957 dead_pseudo_p (rtx x, rtx insn)
3959 int i, j;
3960 const char *fmt;
3961 enum rtx_code code;
3963 if (REG_P (x))
3964 return (insn != NULL_RTX
3965 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
3966 code = GET_CODE (x);
3967 fmt = GET_RTX_FORMAT (code);
3968 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3970 if (fmt[i] == 'e')
3972 if (dead_pseudo_p (XEXP (x, i), insn))
3973 return true;
3975 else if (fmt[i] == 'E')
3977 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3978 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
3979 return true;
3982 return false;
3985 /* Return true if INSN contains a dying pseudo in INSN right hand
3986 side. */
3987 static bool
3988 insn_rhs_dead_pseudo_p (rtx insn)
3990 rtx set = single_set (insn);
3992 gcc_assert (set != NULL);
3993 return dead_pseudo_p (SET_SRC (set), insn);
3996 /* Return true if any init insn of REGNO contains a dying pseudo in
3997 insn right hand side. */
3998 static bool
3999 init_insn_rhs_dead_pseudo_p (int regno)
4001 rtx insns = ira_reg_equiv[regno].init_insns;
4003 if (insns == NULL)
4004 return false;
4005 if (INSN_P (insns))
4006 return insn_rhs_dead_pseudo_p (insns);
4007 for (; insns != NULL_RTX; insns = XEXP (insns, 1))
4008 if (insn_rhs_dead_pseudo_p (XEXP (insns, 0)))
4009 return true;
4010 return false;
4013 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4014 reverse only if we have one init insn with given REGNO as a
4015 source. */
4016 static bool
4017 reverse_equiv_p (int regno)
4019 rtx insns, set;
4021 if ((insns = ira_reg_equiv[regno].init_insns) == NULL_RTX)
4022 return false;
4023 if (! INSN_P (XEXP (insns, 0))
4024 || XEXP (insns, 1) != NULL_RTX)
4025 return false;
4026 if ((set = single_set (XEXP (insns, 0))) == NULL_RTX)
4027 return false;
4028 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4031 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4032 call this function only for non-reverse equivalence. */
4033 static bool
4034 contains_reloaded_insn_p (int regno)
4036 rtx set;
4037 rtx list = ira_reg_equiv[regno].init_insns;
4039 for (; list != NULL_RTX; list = XEXP (list, 1))
4040 if ((set = single_set (XEXP (list, 0))) == NULL_RTX
4041 || ! REG_P (SET_DEST (set))
4042 || (int) REGNO (SET_DEST (set)) != regno)
4043 return true;
4044 return false;
4047 /* Entry function of LRA constraint pass. Return true if the
4048 constraint pass did change the code. */
4049 bool
4050 lra_constraints (bool first_p)
4052 bool changed_p;
4053 int i, hard_regno, new_insns_num;
4054 unsigned int min_len, new_min_len, uid;
4055 rtx set, x, reg, dest_reg;
4056 basic_block last_bb;
4057 bitmap_head equiv_insn_bitmap;
4058 bitmap_iterator bi;
4060 lra_constraint_iter++;
4061 if (lra_dump_file != NULL)
4062 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4063 lra_constraint_iter);
4064 lra_constraint_iter_after_spill++;
4065 if (lra_constraint_iter_after_spill > LRA_MAX_CONSTRAINT_ITERATION_NUMBER)
4066 internal_error
4067 ("Maximum number of LRA constraint passes is achieved (%d)\n",
4068 LRA_MAX_CONSTRAINT_ITERATION_NUMBER);
4069 changed_p = false;
4070 lra_risky_transformations_p = false;
4071 new_insn_uid_start = get_max_uid ();
4072 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4073 /* Mark used hard regs for target stack size calulations. */
4074 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4075 if (lra_reg_info[i].nrefs != 0
4076 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4078 int j, nregs;
4080 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4081 for (j = 0; j < nregs; j++)
4082 df_set_regs_ever_live (hard_regno + j, true);
4084 /* Do elimination before the equivalence processing as we can spill
4085 some pseudos during elimination. */
4086 lra_eliminate (false, first_p);
4087 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4088 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4089 if (lra_reg_info[i].nrefs != 0)
4091 ira_reg_equiv[i].profitable_p = true;
4092 reg = regno_reg_rtx[i];
4093 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4095 bool pseudo_p = contains_reg_p (x, false, false);
4097 /* After RTL transformation, we can not guarantee that
4098 pseudo in the substitution was not reloaded which might
4099 make equivalence invalid. For example, in reverse
4100 equiv of p0
4102 p0 <- ...
4104 equiv_mem <- p0
4106 the memory address register was reloaded before the 2nd
4107 insn. */
4108 if ((! first_p && pseudo_p)
4109 /* We don't use DF for compilation speed sake. So it
4110 is problematic to update live info when we use an
4111 equivalence containing pseudos in more than one
4112 BB. */
4113 || (pseudo_p && multi_block_pseudo_p (i))
4114 /* If an init insn was deleted for some reason, cancel
4115 the equiv. We could update the equiv insns after
4116 transformations including an equiv insn deletion
4117 but it is not worthy as such cases are extremely
4118 rare. */
4119 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4120 /* If it is not a reverse equivalence, we check that a
4121 pseudo in rhs of the init insn is not dying in the
4122 insn. Otherwise, the live info at the beginning of
4123 the corresponding BB might be wrong after we
4124 removed the insn. When the equiv can be a
4125 constant, the right hand side of the init insn can
4126 be a pseudo. */
4127 || (! reverse_equiv_p (i)
4128 && (init_insn_rhs_dead_pseudo_p (i)
4129 /* If we reloaded the pseudo in an equivalence
4130 init insn, we can not remove the equiv init
4131 insns and the init insns might write into
4132 const memory in this case. */
4133 || contains_reloaded_insn_p (i)))
4134 /* Prevent access beyond equivalent memory for
4135 paradoxical subregs. */
4136 || (MEM_P (x)
4137 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4138 > GET_MODE_SIZE (GET_MODE (x)))))
4139 ira_reg_equiv[i].defined_p = false;
4140 if (contains_reg_p (x, false, true))
4141 ira_reg_equiv[i].profitable_p = false;
4142 if (get_equiv (reg) != reg)
4143 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4146 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4147 update_equiv (i);
4148 /* We should add all insns containing pseudos which should be
4149 substituted by their equivalences. */
4150 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4151 lra_push_insn_by_uid (uid);
4152 min_len = lra_insn_stack_length ();
4153 new_insns_num = 0;
4154 last_bb = NULL;
4155 changed_p = false;
4156 while ((new_min_len = lra_insn_stack_length ()) != 0)
4158 curr_insn = lra_pop_insn ();
4159 --new_min_len;
4160 curr_bb = BLOCK_FOR_INSN (curr_insn);
4161 if (curr_bb != last_bb)
4163 last_bb = curr_bb;
4164 bb_reload_num = lra_curr_reload_num;
4166 if (min_len > new_min_len)
4168 min_len = new_min_len;
4169 new_insns_num = 0;
4171 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4172 internal_error
4173 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4174 MAX_RELOAD_INSNS_NUMBER);
4175 new_insns_num++;
4176 if (DEBUG_INSN_P (curr_insn))
4178 /* We need to check equivalence in debug insn and change
4179 pseudo to the equivalent value if necessary. */
4180 curr_id = lra_get_insn_recog_data (curr_insn);
4181 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4183 rtx old = *curr_id->operand_loc[0];
4184 *curr_id->operand_loc[0]
4185 = simplify_replace_fn_rtx (old, NULL_RTX,
4186 loc_equivalence_callback, curr_insn);
4187 if (old != *curr_id->operand_loc[0])
4189 lra_update_insn_regno_info (curr_insn);
4190 changed_p = true;
4194 else if (INSN_P (curr_insn))
4196 if ((set = single_set (curr_insn)) != NULL_RTX)
4198 dest_reg = SET_DEST (set);
4199 /* The equivalence pseudo could be set up as SUBREG in a
4200 case when it is a call restore insn in a mode
4201 different from the pseudo mode. */
4202 if (GET_CODE (dest_reg) == SUBREG)
4203 dest_reg = SUBREG_REG (dest_reg);
4204 if ((REG_P (dest_reg)
4205 && (x = get_equiv (dest_reg)) != dest_reg
4206 /* Remove insns which set up a pseudo whose value
4207 can not be changed. Such insns might be not in
4208 init_insns because we don't update equiv data
4209 during insn transformations.
4211 As an example, let suppose that a pseudo got
4212 hard register and on the 1st pass was not
4213 changed to equivalent constant. We generate an
4214 additional insn setting up the pseudo because of
4215 secondary memory movement. Then the pseudo is
4216 spilled and we use the equiv constant. In this
4217 case we should remove the additional insn and
4218 this insn is not init_insns list. */
4219 && (! MEM_P (x) || MEM_READONLY_P (x)
4220 /* Check that this is actually an insn setting
4221 up the equivalence. */
4222 || in_list_p (curr_insn,
4223 ira_reg_equiv
4224 [REGNO (dest_reg)].init_insns)))
4225 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4226 && in_list_p (curr_insn,
4227 ira_reg_equiv
4228 [REGNO (SET_SRC (set))].init_insns)))
4230 /* This is equiv init insn of pseudo which did not get a
4231 hard register -- remove the insn. */
4232 if (lra_dump_file != NULL)
4234 fprintf (lra_dump_file,
4235 " Removing equiv init insn %i (freq=%d)\n",
4236 INSN_UID (curr_insn),
4237 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4238 dump_insn_slim (lra_dump_file, curr_insn);
4240 if (contains_reg_p (x, true, false))
4241 lra_risky_transformations_p = true;
4242 lra_set_insn_deleted (curr_insn);
4243 continue;
4246 curr_id = lra_get_insn_recog_data (curr_insn);
4247 curr_static_id = curr_id->insn_static_data;
4248 init_curr_insn_input_reloads ();
4249 init_curr_operand_mode ();
4250 if (curr_insn_transform ())
4251 changed_p = true;
4252 /* Check non-transformed insns too for equiv change as USE
4253 or CLOBBER don't need reloads but can contain pseudos
4254 being changed on their equivalences. */
4255 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4256 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4258 lra_update_insn_regno_info (curr_insn);
4259 changed_p = true;
4263 bitmap_clear (&equiv_insn_bitmap);
4264 /* If we used a new hard regno, changed_p should be true because the
4265 hard reg is assigned to a new pseudo. */
4266 #ifdef ENABLE_CHECKING
4267 if (! changed_p)
4269 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4270 if (lra_reg_info[i].nrefs != 0
4271 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4273 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4275 for (j = 0; j < nregs; j++)
4276 lra_assert (df_regs_ever_live_p (hard_regno + j));
4279 #endif
4280 return changed_p;
4283 /* Initiate the LRA constraint pass. It is done once per
4284 function. */
4285 void
4286 lra_constraints_init (void)
4290 /* Finalize the LRA constraint pass. It is done once per
4291 function. */
4292 void
4293 lra_constraints_finish (void)
4299 /* This page contains code to do inheritance/split
4300 transformations. */
4302 /* Number of reloads passed so far in current EBB. */
4303 static int reloads_num;
4305 /* Number of calls passed so far in current EBB. */
4306 static int calls_num;
4308 /* Current reload pseudo check for validity of elements in
4309 USAGE_INSNS. */
4310 static int curr_usage_insns_check;
4312 /* Info about last usage of registers in EBB to do inheritance/split
4313 transformation. Inheritance transformation is done from a spilled
4314 pseudo and split transformations from a hard register or a pseudo
4315 assigned to a hard register. */
4316 struct usage_insns
4318 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4319 value INSNS is valid. The insns is chain of optional debug insns
4320 and a finishing non-debug insn using the corresponding reg. The
4321 value is also used to mark the registers which are set up in the
4322 current insn. The negated insn uid is used for this. */
4323 int check;
4324 /* Value of global reloads_num at the last insn in INSNS. */
4325 int reloads_num;
4326 /* Value of global reloads_nums at the last insn in INSNS. */
4327 int calls_num;
4328 /* It can be true only for splitting. And it means that the restore
4329 insn should be put after insn given by the following member. */
4330 bool after_p;
4331 /* Next insns in the current EBB which use the original reg and the
4332 original reg value is not changed between the current insn and
4333 the next insns. In order words, e.g. for inheritance, if we need
4334 to use the original reg value again in the next insns we can try
4335 to use the value in a hard register from a reload insn of the
4336 current insn. */
4337 rtx insns;
4340 /* Map: regno -> corresponding pseudo usage insns. */
4341 static struct usage_insns *usage_insns;
4343 static void
4344 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4346 usage_insns[regno].check = curr_usage_insns_check;
4347 usage_insns[regno].insns = insn;
4348 usage_insns[regno].reloads_num = reloads_num;
4349 usage_insns[regno].calls_num = calls_num;
4350 usage_insns[regno].after_p = after_p;
4353 /* The function is used to form list REGNO usages which consists of
4354 optional debug insns finished by a non-debug insn using REGNO.
4355 RELOADS_NUM is current number of reload insns processed so far. */
4356 static void
4357 add_next_usage_insn (int regno, rtx insn, int reloads_num)
4359 rtx next_usage_insns;
4361 if (usage_insns[regno].check == curr_usage_insns_check
4362 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4363 && DEBUG_INSN_P (insn))
4365 /* Check that we did not add the debug insn yet. */
4366 if (next_usage_insns != insn
4367 && (GET_CODE (next_usage_insns) != INSN_LIST
4368 || XEXP (next_usage_insns, 0) != insn))
4369 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4370 next_usage_insns);
4372 else if (NONDEBUG_INSN_P (insn))
4373 setup_next_usage_insn (regno, insn, reloads_num, false);
4374 else
4375 usage_insns[regno].check = 0;
4378 /* Replace all references to register OLD_REGNO in *LOC with pseudo
4379 register NEW_REG. Return true if any change was made. */
4380 static bool
4381 substitute_pseudo (rtx *loc, int old_regno, rtx new_reg)
4383 rtx x = *loc;
4384 bool result = false;
4385 enum rtx_code code;
4386 const char *fmt;
4387 int i, j;
4389 if (x == NULL_RTX)
4390 return false;
4392 code = GET_CODE (x);
4393 if (code == REG && (int) REGNO (x) == old_regno)
4395 enum machine_mode mode = GET_MODE (*loc);
4396 enum machine_mode inner_mode = GET_MODE (new_reg);
4398 if (mode != inner_mode)
4400 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
4401 || ! SCALAR_INT_MODE_P (inner_mode))
4402 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
4403 else
4404 new_reg = gen_lowpart_SUBREG (mode, new_reg);
4406 *loc = new_reg;
4407 return true;
4410 /* Scan all the operand sub-expressions. */
4411 fmt = GET_RTX_FORMAT (code);
4412 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4414 if (fmt[i] == 'e')
4416 if (substitute_pseudo (&XEXP (x, i), old_regno, new_reg))
4417 result = true;
4419 else if (fmt[i] == 'E')
4421 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4422 if (substitute_pseudo (&XVECEXP (x, i, j), old_regno, new_reg))
4423 result = true;
4426 return result;
4429 /* Return first non-debug insn in list USAGE_INSNS. */
4430 static rtx
4431 skip_usage_debug_insns (rtx usage_insns)
4433 rtx insn;
4435 /* Skip debug insns. */
4436 for (insn = usage_insns;
4437 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4438 insn = XEXP (insn, 1))
4440 return insn;
4443 /* Return true if we need secondary memory moves for insn in
4444 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4445 into the insn. */
4446 static bool
4447 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4448 rtx usage_insns ATTRIBUTE_UNUSED)
4450 #ifndef SECONDARY_MEMORY_NEEDED
4451 return false;
4452 #else
4453 rtx insn, set, dest;
4454 enum reg_class cl;
4456 if (inher_cl == ALL_REGS
4457 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4458 return false;
4459 lra_assert (INSN_P (insn));
4460 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4461 return false;
4462 dest = SET_DEST (set);
4463 if (! REG_P (dest))
4464 return false;
4465 lra_assert (inher_cl != NO_REGS);
4466 cl = get_reg_class (REGNO (dest));
4467 return (cl != NO_REGS && cl != ALL_REGS
4468 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4469 #endif
4472 /* Registers involved in inheritance/split in the current EBB
4473 (inheritance/split pseudos and original registers). */
4474 static bitmap_head check_only_regs;
4476 /* Do inheritance transformations for insn INSN, which defines (if
4477 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4478 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4479 form as the "insns" field of usage_insns. Return true if we
4480 succeed in such transformation.
4482 The transformations look like:
4484 p <- ... i <- ...
4485 ... p <- i (new insn)
4486 ... =>
4487 <- ... p ... <- ... i ...
4489 ... i <- p (new insn)
4490 <- ... p ... <- ... i ...
4491 ... =>
4492 <- ... p ... <- ... i ...
4493 where p is a spilled original pseudo and i is a new inheritance pseudo.
4496 The inheritance pseudo has the smallest class of two classes CL and
4497 class of ORIGINAL REGNO. */
4498 static bool
4499 inherit_reload_reg (bool def_p, int original_regno,
4500 enum reg_class cl, rtx insn, rtx next_usage_insns)
4502 if (optimize_function_for_size_p (cfun))
4503 return false;
4505 enum reg_class rclass = lra_get_allocno_class (original_regno);
4506 rtx original_reg = regno_reg_rtx[original_regno];
4507 rtx new_reg, new_insns, usage_insn;
4509 lra_assert (! usage_insns[original_regno].after_p);
4510 if (lra_dump_file != NULL)
4511 fprintf (lra_dump_file,
4512 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4513 if (! ira_reg_classes_intersect_p[cl][rclass])
4515 if (lra_dump_file != NULL)
4517 fprintf (lra_dump_file,
4518 " Rejecting inheritance for %d "
4519 "because of disjoint classes %s and %s\n",
4520 original_regno, reg_class_names[cl],
4521 reg_class_names[rclass]);
4522 fprintf (lra_dump_file,
4523 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4525 return false;
4527 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4528 /* We don't use a subset of two classes because it can be
4529 NO_REGS. This transformation is still profitable in most
4530 cases even if the classes are not intersected as register
4531 move is probably cheaper than a memory load. */
4532 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4534 if (lra_dump_file != NULL)
4535 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4536 reg_class_names[cl], reg_class_names[rclass]);
4538 rclass = cl;
4540 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4542 /* Reject inheritance resulting in secondary memory moves.
4543 Otherwise, there is a danger in LRA cycling. Also such
4544 transformation will be unprofitable. */
4545 if (lra_dump_file != NULL)
4547 rtx insn = skip_usage_debug_insns (next_usage_insns);
4548 rtx set = single_set (insn);
4550 lra_assert (set != NULL_RTX);
4552 rtx dest = SET_DEST (set);
4554 lra_assert (REG_P (dest));
4555 fprintf (lra_dump_file,
4556 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4557 "as secondary mem is needed\n",
4558 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4559 original_regno, reg_class_names[rclass]);
4560 fprintf (lra_dump_file,
4561 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4563 return false;
4565 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4566 rclass, "inheritance");
4567 start_sequence ();
4568 if (def_p)
4569 lra_emit_move (original_reg, new_reg);
4570 else
4571 lra_emit_move (new_reg, original_reg);
4572 new_insns = get_insns ();
4573 end_sequence ();
4574 if (NEXT_INSN (new_insns) != NULL_RTX)
4576 if (lra_dump_file != NULL)
4578 fprintf (lra_dump_file,
4579 " Rejecting inheritance %d->%d "
4580 "as it results in 2 or more insns:\n",
4581 original_regno, REGNO (new_reg));
4582 dump_rtl_slim (lra_dump_file, new_insns, NULL_RTX, -1, 0);
4583 fprintf (lra_dump_file,
4584 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4586 return false;
4588 substitute_pseudo (&insn, original_regno, new_reg);
4589 lra_update_insn_regno_info (insn);
4590 if (! def_p)
4591 /* We now have a new usage insn for original regno. */
4592 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4593 if (lra_dump_file != NULL)
4594 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4595 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4596 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4597 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4598 bitmap_set_bit (&check_only_regs, original_regno);
4599 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4600 if (def_p)
4601 lra_process_new_insns (insn, NULL_RTX, new_insns,
4602 "Add original<-inheritance");
4603 else
4604 lra_process_new_insns (insn, new_insns, NULL_RTX,
4605 "Add inheritance<-original");
4606 while (next_usage_insns != NULL_RTX)
4608 if (GET_CODE (next_usage_insns) != INSN_LIST)
4610 usage_insn = next_usage_insns;
4611 lra_assert (NONDEBUG_INSN_P (usage_insn));
4612 next_usage_insns = NULL;
4614 else
4616 usage_insn = XEXP (next_usage_insns, 0);
4617 lra_assert (DEBUG_INSN_P (usage_insn));
4618 next_usage_insns = XEXP (next_usage_insns, 1);
4620 substitute_pseudo (&usage_insn, original_regno, new_reg);
4621 lra_update_insn_regno_info (usage_insn);
4622 if (lra_dump_file != NULL)
4624 fprintf (lra_dump_file,
4625 " Inheritance reuse change %d->%d (bb%d):\n",
4626 original_regno, REGNO (new_reg),
4627 BLOCK_FOR_INSN (usage_insn)->index);
4628 dump_insn_slim (lra_dump_file, usage_insn);
4631 if (lra_dump_file != NULL)
4632 fprintf (lra_dump_file,
4633 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4634 return true;
4637 /* Return true if we need a caller save/restore for pseudo REGNO which
4638 was assigned to a hard register. */
4639 static inline bool
4640 need_for_call_save_p (int regno)
4642 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4643 return (usage_insns[regno].calls_num < calls_num
4644 && (overlaps_hard_reg_set_p
4645 (call_used_reg_set,
4646 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4647 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4648 PSEUDO_REGNO_MODE (regno))));
4651 /* Global registers occurring in the current EBB. */
4652 static bitmap_head ebb_global_regs;
4654 /* Return true if we need a split for hard register REGNO or pseudo
4655 REGNO which was assigned to a hard register.
4656 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4657 used for reloads since the EBB end. It is an approximation of the
4658 used hard registers in the split range. The exact value would
4659 require expensive calculations. If we were aggressive with
4660 splitting because of the approximation, the split pseudo will save
4661 the same hard register assignment and will be removed in the undo
4662 pass. We still need the approximation because too aggressive
4663 splitting would result in too inaccurate cost calculation in the
4664 assignment pass because of too many generated moves which will be
4665 probably removed in the undo pass. */
4666 static inline bool
4667 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4669 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4671 lra_assert (hard_regno >= 0);
4672 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4673 /* Don't split eliminable hard registers, otherwise we can
4674 split hard registers like hard frame pointer, which
4675 lives on BB start/end according to DF-infrastructure,
4676 when there is a pseudo assigned to the register and
4677 living in the same BB. */
4678 && (regno >= FIRST_PSEUDO_REGISTER
4679 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4680 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4681 /* Don't split call clobbered hard regs living through
4682 calls, otherwise we might have a check problem in the
4683 assign sub-pass as in the most cases (exception is a
4684 situation when lra_risky_transformations_p value is
4685 true) the assign pass assumes that all pseudos living
4686 through calls are assigned to call saved hard regs. */
4687 && (regno >= FIRST_PSEUDO_REGISTER
4688 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4689 || usage_insns[regno].calls_num == calls_num)
4690 /* We need at least 2 reloads to make pseudo splitting
4691 profitable. We should provide hard regno splitting in
4692 any case to solve 1st insn scheduling problem when
4693 moving hard register definition up might result in
4694 impossibility to find hard register for reload pseudo of
4695 small register class. */
4696 && (usage_insns[regno].reloads_num
4697 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4698 && (regno < FIRST_PSEUDO_REGISTER
4699 /* For short living pseudos, spilling + inheritance can
4700 be considered a substitution for splitting.
4701 Therefore we do not splitting for local pseudos. It
4702 decreases also aggressiveness of splitting. The
4703 minimal number of references is chosen taking into
4704 account that for 2 references splitting has no sense
4705 as we can just spill the pseudo. */
4706 || (regno >= FIRST_PSEUDO_REGISTER
4707 && lra_reg_info[regno].nrefs > 3
4708 && bitmap_bit_p (&ebb_global_regs, regno))))
4709 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4712 /* Return class for the split pseudo created from original pseudo with
4713 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4714 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4715 results in no secondary memory movements. */
4716 static enum reg_class
4717 choose_split_class (enum reg_class allocno_class,
4718 int hard_regno ATTRIBUTE_UNUSED,
4719 enum machine_mode mode ATTRIBUTE_UNUSED)
4721 #ifndef SECONDARY_MEMORY_NEEDED
4722 return allocno_class;
4723 #else
4724 int i;
4725 enum reg_class cl, best_cl = NO_REGS;
4726 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4727 = REGNO_REG_CLASS (hard_regno);
4729 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4730 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4731 return allocno_class;
4732 for (i = 0;
4733 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4734 i++)
4735 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4736 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4737 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4738 && (best_cl == NO_REGS
4739 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4740 best_cl = cl;
4741 return best_cl;
4742 #endif
4745 /* Do split transformations for insn INSN, which defines or uses
4746 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4747 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4748 "insns" field of usage_insns.
4750 The transformations look like:
4752 p <- ... p <- ...
4753 ... s <- p (new insn -- save)
4754 ... =>
4755 ... p <- s (new insn -- restore)
4756 <- ... p ... <- ... p ...
4758 <- ... p ... <- ... p ...
4759 ... s <- p (new insn -- save)
4760 ... =>
4761 ... p <- s (new insn -- restore)
4762 <- ... p ... <- ... p ...
4764 where p is an original pseudo got a hard register or a hard
4765 register and s is a new split pseudo. The save is put before INSN
4766 if BEFORE_P is true. Return true if we succeed in such
4767 transformation. */
4768 static bool
4769 split_reg (bool before_p, int original_regno, rtx insn, rtx next_usage_insns)
4771 enum reg_class rclass;
4772 rtx original_reg;
4773 int hard_regno, nregs;
4774 rtx new_reg, save, restore, usage_insn;
4775 bool after_p;
4776 bool call_save_p;
4778 if (original_regno < FIRST_PSEUDO_REGISTER)
4780 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4781 hard_regno = original_regno;
4782 call_save_p = false;
4783 nregs = 1;
4785 else
4787 hard_regno = reg_renumber[original_regno];
4788 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4789 rclass = lra_get_allocno_class (original_regno);
4790 original_reg = regno_reg_rtx[original_regno];
4791 call_save_p = need_for_call_save_p (original_regno);
4793 original_reg = regno_reg_rtx[original_regno];
4794 lra_assert (hard_regno >= 0);
4795 if (lra_dump_file != NULL)
4796 fprintf (lra_dump_file,
4797 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4798 if (call_save_p)
4800 enum machine_mode mode = GET_MODE (original_reg);
4802 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4803 hard_regno_nregs[hard_regno][mode],
4804 mode);
4805 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4807 else
4809 rclass = choose_split_class (rclass, hard_regno,
4810 GET_MODE (original_reg));
4811 if (rclass == NO_REGS)
4813 if (lra_dump_file != NULL)
4815 fprintf (lra_dump_file,
4816 " Rejecting split of %d(%s): "
4817 "no good reg class for %d(%s)\n",
4818 original_regno,
4819 reg_class_names[lra_get_allocno_class (original_regno)],
4820 hard_regno,
4821 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4822 fprintf
4823 (lra_dump_file,
4824 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4826 return false;
4828 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4829 rclass, "split");
4830 reg_renumber[REGNO (new_reg)] = hard_regno;
4832 save = emit_spill_move (true, new_reg, original_reg);
4833 if (NEXT_INSN (save) != NULL_RTX)
4835 lra_assert (! call_save_p);
4836 if (lra_dump_file != NULL)
4838 fprintf
4839 (lra_dump_file,
4840 " Rejecting split %d->%d resulting in > 2 %s save insns:\n",
4841 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4842 dump_rtl_slim (lra_dump_file, save, NULL_RTX, -1, 0);
4843 fprintf (lra_dump_file,
4844 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4846 return false;
4848 restore = emit_spill_move (false, new_reg, original_reg);
4849 if (NEXT_INSN (restore) != NULL_RTX)
4851 lra_assert (! call_save_p);
4852 if (lra_dump_file != NULL)
4854 fprintf (lra_dump_file,
4855 " Rejecting split %d->%d "
4856 "resulting in > 2 %s restore insns:\n",
4857 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4858 dump_rtl_slim (lra_dump_file, restore, NULL_RTX, -1, 0);
4859 fprintf (lra_dump_file,
4860 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4862 return false;
4864 after_p = usage_insns[original_regno].after_p;
4865 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4866 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4867 bitmap_set_bit (&check_only_regs, original_regno);
4868 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4869 for (;;)
4871 if (GET_CODE (next_usage_insns) != INSN_LIST)
4873 usage_insn = next_usage_insns;
4874 break;
4876 usage_insn = XEXP (next_usage_insns, 0);
4877 lra_assert (DEBUG_INSN_P (usage_insn));
4878 next_usage_insns = XEXP (next_usage_insns, 1);
4879 substitute_pseudo (&usage_insn, original_regno, new_reg);
4880 lra_update_insn_regno_info (usage_insn);
4881 if (lra_dump_file != NULL)
4883 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
4884 original_regno, REGNO (new_reg));
4885 dump_insn_slim (lra_dump_file, usage_insn);
4888 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
4889 lra_assert (usage_insn != insn || (after_p && before_p));
4890 lra_process_new_insns (usage_insn, after_p ? NULL_RTX : restore,
4891 after_p ? restore : NULL_RTX,
4892 call_save_p
4893 ? "Add reg<-save" : "Add reg<-split");
4894 lra_process_new_insns (insn, before_p ? save : NULL_RTX,
4895 before_p ? NULL_RTX : save,
4896 call_save_p
4897 ? "Add save<-reg" : "Add split<-reg");
4898 if (nregs > 1)
4899 /* If we are trying to split multi-register. We should check
4900 conflicts on the next assignment sub-pass. IRA can allocate on
4901 sub-register levels, LRA do this on pseudos level right now and
4902 this discrepancy may create allocation conflicts after
4903 splitting. */
4904 lra_risky_transformations_p = true;
4905 if (lra_dump_file != NULL)
4906 fprintf (lra_dump_file,
4907 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4908 return true;
4911 /* Recognize that we need a split transformation for insn INSN, which
4912 defines or uses REGNO in its insn biggest MODE (we use it only if
4913 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
4914 hard registers which might be used for reloads since the EBB end.
4915 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
4916 uid before starting INSN processing. Return true if we succeed in
4917 such transformation. */
4918 static bool
4919 split_if_necessary (int regno, enum machine_mode mode,
4920 HARD_REG_SET potential_reload_hard_regs,
4921 bool before_p, rtx insn, int max_uid)
4923 bool res = false;
4924 int i, nregs = 1;
4925 rtx next_usage_insns;
4927 if (regno < FIRST_PSEUDO_REGISTER)
4928 nregs = hard_regno_nregs[regno][mode];
4929 for (i = 0; i < nregs; i++)
4930 if (usage_insns[regno + i].check == curr_usage_insns_check
4931 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
4932 /* To avoid processing the register twice or more. */
4933 && ((GET_CODE (next_usage_insns) != INSN_LIST
4934 && INSN_UID (next_usage_insns) < max_uid)
4935 || (GET_CODE (next_usage_insns) == INSN_LIST
4936 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
4937 && need_for_split_p (potential_reload_hard_regs, regno + i)
4938 && split_reg (before_p, regno + i, insn, next_usage_insns))
4939 res = true;
4940 return res;
4943 /* Check only registers living at the current program point in the
4944 current EBB. */
4945 static bitmap_head live_regs;
4947 /* Update live info in EBB given by its HEAD and TAIL insns after
4948 inheritance/split transformation. The function removes dead moves
4949 too. */
4950 static void
4951 update_ebb_live_info (rtx head, rtx tail)
4953 unsigned int j;
4954 int i, regno;
4955 bool live_p;
4956 rtx prev_insn, set;
4957 bool remove_p;
4958 basic_block last_bb, prev_bb, curr_bb;
4959 bitmap_iterator bi;
4960 struct lra_insn_reg *reg;
4961 edge e;
4962 edge_iterator ei;
4964 last_bb = BLOCK_FOR_INSN (tail);
4965 prev_bb = NULL;
4966 for (curr_insn = tail;
4967 curr_insn != PREV_INSN (head);
4968 curr_insn = prev_insn)
4970 prev_insn = PREV_INSN (curr_insn);
4971 /* We need to process empty blocks too. They contain
4972 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
4973 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
4974 continue;
4975 curr_bb = BLOCK_FOR_INSN (curr_insn);
4976 if (curr_bb != prev_bb)
4978 if (prev_bb != NULL)
4980 /* Update df_get_live_in (prev_bb): */
4981 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4982 if (bitmap_bit_p (&live_regs, j))
4983 bitmap_set_bit (df_get_live_in (prev_bb), j);
4984 else
4985 bitmap_clear_bit (df_get_live_in (prev_bb), j);
4987 if (curr_bb != last_bb)
4989 /* Update df_get_live_out (curr_bb): */
4990 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4992 live_p = bitmap_bit_p (&live_regs, j);
4993 if (! live_p)
4994 FOR_EACH_EDGE (e, ei, curr_bb->succs)
4995 if (bitmap_bit_p (df_get_live_in (e->dest), j))
4997 live_p = true;
4998 break;
5000 if (live_p)
5001 bitmap_set_bit (df_get_live_out (curr_bb), j);
5002 else
5003 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5006 prev_bb = curr_bb;
5007 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5009 if (! NONDEBUG_INSN_P (curr_insn))
5010 continue;
5011 curr_id = lra_get_insn_recog_data (curr_insn);
5012 curr_static_id = curr_id->insn_static_data;
5013 remove_p = false;
5014 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
5015 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5016 && bitmap_bit_p (&check_only_regs, regno)
5017 && ! bitmap_bit_p (&live_regs, regno))
5018 remove_p = true;
5019 /* See which defined values die here. */
5020 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5021 if (reg->type == OP_OUT && ! reg->subreg_p)
5022 bitmap_clear_bit (&live_regs, reg->regno);
5023 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5024 if (reg->type == OP_OUT && ! reg->subreg_p)
5025 bitmap_clear_bit (&live_regs, reg->regno);
5026 /* Mark each used value as live. */
5027 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5028 if (reg->type != OP_OUT
5029 && bitmap_bit_p (&check_only_regs, reg->regno))
5030 bitmap_set_bit (&live_regs, reg->regno);
5031 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5032 if (reg->type != OP_OUT
5033 && bitmap_bit_p (&check_only_regs, reg->regno))
5034 bitmap_set_bit (&live_regs, reg->regno);
5035 if (curr_id->arg_hard_regs != NULL)
5036 /* Make argument hard registers live. */
5037 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5038 if (bitmap_bit_p (&check_only_regs, regno))
5039 bitmap_set_bit (&live_regs, regno);
5040 /* It is quite important to remove dead move insns because it
5041 means removing dead store. We don't need to process them for
5042 constraints. */
5043 if (remove_p)
5045 if (lra_dump_file != NULL)
5047 fprintf (lra_dump_file, " Removing dead insn:\n ");
5048 dump_insn_slim (lra_dump_file, curr_insn);
5050 lra_set_insn_deleted (curr_insn);
5055 /* The structure describes info to do an inheritance for the current
5056 insn. We need to collect such info first before doing the
5057 transformations because the transformations change the insn
5058 internal representation. */
5059 struct to_inherit
5061 /* Original regno. */
5062 int regno;
5063 /* Subsequent insns which can inherit original reg value. */
5064 rtx insns;
5067 /* Array containing all info for doing inheritance from the current
5068 insn. */
5069 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5071 /* Number elements in the previous array. */
5072 static int to_inherit_num;
5074 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5075 structure to_inherit. */
5076 static void
5077 add_to_inherit (int regno, rtx insns)
5079 int i;
5081 for (i = 0; i < to_inherit_num; i++)
5082 if (to_inherit[i].regno == regno)
5083 return;
5084 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5085 to_inherit[to_inherit_num].regno = regno;
5086 to_inherit[to_inherit_num++].insns = insns;
5089 /* Return the last non-debug insn in basic block BB, or the block begin
5090 note if none. */
5091 static rtx
5092 get_last_insertion_point (basic_block bb)
5094 rtx insn;
5096 FOR_BB_INSNS_REVERSE (bb, insn)
5097 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5098 return insn;
5099 gcc_unreachable ();
5102 /* Set up RES by registers living on edges FROM except the edge (FROM,
5103 TO) or by registers set up in a jump insn in BB FROM. */
5104 static void
5105 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5107 rtx last;
5108 struct lra_insn_reg *reg;
5109 edge e;
5110 edge_iterator ei;
5112 lra_assert (to != NULL);
5113 bitmap_clear (res);
5114 FOR_EACH_EDGE (e, ei, from->succs)
5115 if (e->dest != to)
5116 bitmap_ior_into (res, df_get_live_in (e->dest));
5117 last = get_last_insertion_point (from);
5118 if (! JUMP_P (last))
5119 return;
5120 curr_id = lra_get_insn_recog_data (last);
5121 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5122 if (reg->type != OP_IN)
5123 bitmap_set_bit (res, reg->regno);
5126 /* Used as a temporary results of some bitmap calculations. */
5127 static bitmap_head temp_bitmap;
5129 /* We split for reloads of small class of hard regs. The following
5130 defines how many hard regs the class should have to be qualified as
5131 small. The code is mostly oriented to x86/x86-64 architecture
5132 where some insns need to use only specific register or pair of
5133 registers and these register can live in RTL explicitly, e.g. for
5134 parameter passing. */
5135 static const int max_small_class_regs_num = 2;
5137 /* Do inheritance/split transformations in EBB starting with HEAD and
5138 finishing on TAIL. We process EBB insns in the reverse order.
5139 Return true if we did any inheritance/split transformation in the
5140 EBB.
5142 We should avoid excessive splitting which results in worse code
5143 because of inaccurate cost calculations for spilling new split
5144 pseudos in such case. To achieve this we do splitting only if
5145 register pressure is high in given basic block and there are reload
5146 pseudos requiring hard registers. We could do more register
5147 pressure calculations at any given program point to avoid necessary
5148 splitting even more but it is to expensive and the current approach
5149 works well enough. */
5150 static bool
5151 inherit_in_ebb (rtx head, rtx tail)
5153 int i, src_regno, dst_regno, nregs;
5154 bool change_p, succ_p, update_reloads_num_p;
5155 rtx prev_insn, next_usage_insns, set, last_insn;
5156 enum reg_class cl;
5157 struct lra_insn_reg *reg;
5158 basic_block last_processed_bb, curr_bb = NULL;
5159 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5160 bitmap to_process;
5161 unsigned int j;
5162 bitmap_iterator bi;
5163 bool head_p, after_p;
5165 change_p = false;
5166 curr_usage_insns_check++;
5167 reloads_num = calls_num = 0;
5168 bitmap_clear (&check_only_regs);
5169 last_processed_bb = NULL;
5170 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5171 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5172 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5173 /* We don't process new insns generated in the loop. */
5174 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5176 prev_insn = PREV_INSN (curr_insn);
5177 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5178 curr_bb = BLOCK_FOR_INSN (curr_insn);
5179 if (last_processed_bb != curr_bb)
5181 /* We are at the end of BB. Add qualified living
5182 pseudos for potential splitting. */
5183 to_process = df_get_live_out (curr_bb);
5184 if (last_processed_bb != NULL)
5186 /* We are somewhere in the middle of EBB. */
5187 get_live_on_other_edges (curr_bb, last_processed_bb,
5188 &temp_bitmap);
5189 to_process = &temp_bitmap;
5191 last_processed_bb = curr_bb;
5192 last_insn = get_last_insertion_point (curr_bb);
5193 after_p = (! JUMP_P (last_insn)
5194 && (! CALL_P (last_insn)
5195 || (find_reg_note (last_insn,
5196 REG_NORETURN, NULL_RTX) == NULL_RTX
5197 && ! SIBLING_CALL_P (last_insn))));
5198 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5199 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5201 if ((int) j >= lra_constraint_new_regno_start)
5202 break;
5203 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5205 if (j < FIRST_PSEUDO_REGISTER)
5206 SET_HARD_REG_BIT (live_hard_regs, j);
5207 else
5208 add_to_hard_reg_set (&live_hard_regs,
5209 PSEUDO_REGNO_MODE (j),
5210 reg_renumber[j]);
5211 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5215 src_regno = dst_regno = -1;
5216 if (NONDEBUG_INSN_P (curr_insn)
5217 && (set = single_set (curr_insn)) != NULL_RTX
5218 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5220 src_regno = REGNO (SET_SRC (set));
5221 dst_regno = REGNO (SET_DEST (set));
5223 update_reloads_num_p = true;
5224 if (src_regno < lra_constraint_new_regno_start
5225 && src_regno >= FIRST_PSEUDO_REGISTER
5226 && reg_renumber[src_regno] < 0
5227 && dst_regno >= lra_constraint_new_regno_start
5228 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5230 /* 'reload_pseudo <- original_pseudo'. */
5231 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5232 reloads_num++;
5233 update_reloads_num_p = false;
5234 succ_p = false;
5235 if (usage_insns[src_regno].check == curr_usage_insns_check
5236 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5237 succ_p = inherit_reload_reg (false, src_regno, cl,
5238 curr_insn, next_usage_insns);
5239 if (succ_p)
5240 change_p = true;
5241 else
5242 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5243 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5244 IOR_HARD_REG_SET (potential_reload_hard_regs,
5245 reg_class_contents[cl]);
5247 else if (src_regno >= lra_constraint_new_regno_start
5248 && dst_regno < lra_constraint_new_regno_start
5249 && dst_regno >= FIRST_PSEUDO_REGISTER
5250 && reg_renumber[dst_regno] < 0
5251 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5252 && usage_insns[dst_regno].check == curr_usage_insns_check
5253 && (next_usage_insns
5254 = usage_insns[dst_regno].insns) != NULL_RTX)
5256 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5257 reloads_num++;
5258 update_reloads_num_p = false;
5259 /* 'original_pseudo <- reload_pseudo'. */
5260 if (! JUMP_P (curr_insn)
5261 && inherit_reload_reg (true, dst_regno, cl,
5262 curr_insn, next_usage_insns))
5263 change_p = true;
5264 /* Invalidate. */
5265 usage_insns[dst_regno].check = 0;
5266 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5267 IOR_HARD_REG_SET (potential_reload_hard_regs,
5268 reg_class_contents[cl]);
5270 else if (INSN_P (curr_insn))
5272 int iter;
5273 int max_uid = get_max_uid ();
5275 curr_id = lra_get_insn_recog_data (curr_insn);
5276 curr_static_id = curr_id->insn_static_data;
5277 to_inherit_num = 0;
5278 /* Process insn definitions. */
5279 for (iter = 0; iter < 2; iter++)
5280 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5281 reg != NULL;
5282 reg = reg->next)
5283 if (reg->type != OP_IN
5284 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5286 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5287 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5288 && usage_insns[dst_regno].check == curr_usage_insns_check
5289 && (next_usage_insns
5290 = usage_insns[dst_regno].insns) != NULL_RTX)
5292 struct lra_insn_reg *r;
5294 for (r = curr_id->regs; r != NULL; r = r->next)
5295 if (r->type != OP_OUT && r->regno == dst_regno)
5296 break;
5297 /* Don't do inheritance if the pseudo is also
5298 used in the insn. */
5299 if (r == NULL)
5300 /* We can not do inheritance right now
5301 because the current insn reg info (chain
5302 regs) can change after that. */
5303 add_to_inherit (dst_regno, next_usage_insns);
5305 /* We can not process one reg twice here because of
5306 usage_insns invalidation. */
5307 if ((dst_regno < FIRST_PSEUDO_REGISTER
5308 || reg_renumber[dst_regno] >= 0)
5309 && ! reg->subreg_p && reg->type != OP_IN)
5311 HARD_REG_SET s;
5313 if (split_if_necessary (dst_regno, reg->biggest_mode,
5314 potential_reload_hard_regs,
5315 false, curr_insn, max_uid))
5316 change_p = true;
5317 CLEAR_HARD_REG_SET (s);
5318 if (dst_regno < FIRST_PSEUDO_REGISTER)
5319 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5320 else
5321 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5322 reg_renumber[dst_regno]);
5323 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5325 /* We should invalidate potential inheritance or
5326 splitting for the current insn usages to the next
5327 usage insns (see code below) as the output pseudo
5328 prevents this. */
5329 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5330 && reg_renumber[dst_regno] < 0)
5331 || (reg->type == OP_OUT && ! reg->subreg_p
5332 && (dst_regno < FIRST_PSEUDO_REGISTER
5333 || reg_renumber[dst_regno] >= 0)))
5335 /* Invalidate and mark definitions. */
5336 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5337 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5338 else
5340 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5341 for (i = 0; i < nregs; i++)
5342 usage_insns[dst_regno + i].check
5343 = -(int) INSN_UID (curr_insn);
5347 if (! JUMP_P (curr_insn))
5348 for (i = 0; i < to_inherit_num; i++)
5349 if (inherit_reload_reg (true, to_inherit[i].regno,
5350 ALL_REGS, curr_insn,
5351 to_inherit[i].insns))
5352 change_p = true;
5353 if (CALL_P (curr_insn))
5355 rtx cheap, pat, dest, restore;
5356 int regno, hard_regno;
5358 calls_num++;
5359 if ((cheap = find_reg_note (curr_insn,
5360 REG_RETURNED, NULL_RTX)) != NULL_RTX
5361 && ((cheap = XEXP (cheap, 0)), true)
5362 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5363 && (hard_regno = reg_renumber[regno]) >= 0
5364 /* If there are pending saves/restores, the
5365 optimization is not worth. */
5366 && usage_insns[regno].calls_num == calls_num - 1
5367 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5369 /* Restore the pseudo from the call result as
5370 REG_RETURNED note says that the pseudo value is
5371 in the call result and the pseudo is an argument
5372 of the call. */
5373 pat = PATTERN (curr_insn);
5374 if (GET_CODE (pat) == PARALLEL)
5375 pat = XVECEXP (pat, 0, 0);
5376 dest = SET_DEST (pat);
5377 start_sequence ();
5378 emit_move_insn (cheap, copy_rtx (dest));
5379 restore = get_insns ();
5380 end_sequence ();
5381 lra_process_new_insns (curr_insn, NULL, restore,
5382 "Inserting call parameter restore");
5383 /* We don't need to save/restore of the pseudo from
5384 this call. */
5385 usage_insns[regno].calls_num = calls_num;
5386 bitmap_set_bit (&check_only_regs, regno);
5389 to_inherit_num = 0;
5390 /* Process insn usages. */
5391 for (iter = 0; iter < 2; iter++)
5392 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5393 reg != NULL;
5394 reg = reg->next)
5395 if ((reg->type != OP_OUT
5396 || (reg->type == OP_OUT && reg->subreg_p))
5397 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5399 if (src_regno >= FIRST_PSEUDO_REGISTER
5400 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5402 if (usage_insns[src_regno].check == curr_usage_insns_check
5403 && (next_usage_insns
5404 = usage_insns[src_regno].insns) != NULL_RTX
5405 && NONDEBUG_INSN_P (curr_insn))
5406 add_to_inherit (src_regno, next_usage_insns);
5407 else if (usage_insns[src_regno].check
5408 != -(int) INSN_UID (curr_insn))
5409 /* Add usages but only if the reg is not set up
5410 in the same insn. */
5411 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5413 else if (src_regno < FIRST_PSEUDO_REGISTER
5414 || reg_renumber[src_regno] >= 0)
5416 bool before_p;
5417 rtx use_insn = curr_insn;
5419 before_p = (JUMP_P (curr_insn)
5420 || (CALL_P (curr_insn) && reg->type == OP_IN));
5421 if (NONDEBUG_INSN_P (curr_insn)
5422 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5423 && split_if_necessary (src_regno, reg->biggest_mode,
5424 potential_reload_hard_regs,
5425 before_p, curr_insn, max_uid))
5427 if (reg->subreg_p)
5428 lra_risky_transformations_p = true;
5429 change_p = true;
5430 /* Invalidate. */
5431 usage_insns[src_regno].check = 0;
5432 if (before_p)
5433 use_insn = PREV_INSN (curr_insn);
5435 if (NONDEBUG_INSN_P (curr_insn))
5437 if (src_regno < FIRST_PSEUDO_REGISTER)
5438 add_to_hard_reg_set (&live_hard_regs,
5439 reg->biggest_mode, src_regno);
5440 else
5441 add_to_hard_reg_set (&live_hard_regs,
5442 PSEUDO_REGNO_MODE (src_regno),
5443 reg_renumber[src_regno]);
5445 add_next_usage_insn (src_regno, use_insn, reloads_num);
5448 /* Process call args. */
5449 if (curr_id->arg_hard_regs != NULL)
5450 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5451 if (src_regno < FIRST_PSEUDO_REGISTER)
5453 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5454 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5456 for (i = 0; i < to_inherit_num; i++)
5458 src_regno = to_inherit[i].regno;
5459 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5460 curr_insn, to_inherit[i].insns))
5461 change_p = true;
5462 else
5463 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5466 if (update_reloads_num_p
5467 && NONDEBUG_INSN_P (curr_insn)
5468 && (set = single_set (curr_insn)) != NULL_RTX)
5470 int regno = -1;
5471 if ((REG_P (SET_DEST (set))
5472 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5473 && reg_renumber[regno] < 0
5474 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5475 || (REG_P (SET_SRC (set))
5476 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5477 && reg_renumber[regno] < 0
5478 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5480 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5481 reloads_num++;
5482 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5483 IOR_HARD_REG_SET (potential_reload_hard_regs,
5484 reg_class_contents[cl]);
5487 /* We reached the start of the current basic block. */
5488 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5489 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5491 /* We reached the beginning of the current block -- do
5492 rest of spliting in the current BB. */
5493 to_process = df_get_live_in (curr_bb);
5494 if (BLOCK_FOR_INSN (head) != curr_bb)
5496 /* We are somewhere in the middle of EBB. */
5497 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5498 curr_bb, &temp_bitmap);
5499 to_process = &temp_bitmap;
5501 head_p = true;
5502 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5504 if ((int) j >= lra_constraint_new_regno_start)
5505 break;
5506 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5507 && usage_insns[j].check == curr_usage_insns_check
5508 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5510 if (need_for_split_p (potential_reload_hard_regs, j))
5512 if (lra_dump_file != NULL && head_p)
5514 fprintf (lra_dump_file,
5515 " ----------------------------------\n");
5516 head_p = false;
5518 if (split_reg (false, j, bb_note (curr_bb),
5519 next_usage_insns))
5520 change_p = true;
5522 usage_insns[j].check = 0;
5527 return change_p;
5530 /* This value affects EBB forming. If probability of edge from EBB to
5531 a BB is not greater than the following value, we don't add the BB
5532 to EBB. */
5533 #define EBB_PROBABILITY_CUTOFF ((REG_BR_PROB_BASE * 50) / 100)
5535 /* Current number of inheritance/split iteration. */
5536 int lra_inheritance_iter;
5538 /* Entry function for inheritance/split pass. */
5539 void
5540 lra_inheritance (void)
5542 int i;
5543 basic_block bb, start_bb;
5544 edge e;
5546 lra_inheritance_iter++;
5547 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5548 return;
5549 timevar_push (TV_LRA_INHERITANCE);
5550 if (lra_dump_file != NULL)
5551 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5552 lra_inheritance_iter);
5553 curr_usage_insns_check = 0;
5554 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5555 for (i = 0; i < lra_constraint_new_regno_start; i++)
5556 usage_insns[i].check = 0;
5557 bitmap_initialize (&check_only_regs, &reg_obstack);
5558 bitmap_initialize (&live_regs, &reg_obstack);
5559 bitmap_initialize (&temp_bitmap, &reg_obstack);
5560 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5561 FOR_EACH_BB_FN (bb, cfun)
5563 start_bb = bb;
5564 if (lra_dump_file != NULL)
5565 fprintf (lra_dump_file, "EBB");
5566 /* Form a EBB starting with BB. */
5567 bitmap_clear (&ebb_global_regs);
5568 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5569 for (;;)
5571 if (lra_dump_file != NULL)
5572 fprintf (lra_dump_file, " %d", bb->index);
5573 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5574 || LABEL_P (BB_HEAD (bb->next_bb)))
5575 break;
5576 e = find_fallthru_edge (bb->succs);
5577 if (! e)
5578 break;
5579 if (e->probability <= EBB_PROBABILITY_CUTOFF)
5580 break;
5581 bb = bb->next_bb;
5583 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5584 if (lra_dump_file != NULL)
5585 fprintf (lra_dump_file, "\n");
5586 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5587 /* Remember that the EBB head and tail can change in
5588 inherit_in_ebb. */
5589 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5591 bitmap_clear (&ebb_global_regs);
5592 bitmap_clear (&temp_bitmap);
5593 bitmap_clear (&live_regs);
5594 bitmap_clear (&check_only_regs);
5595 free (usage_insns);
5597 timevar_pop (TV_LRA_INHERITANCE);
5602 /* This page contains code to undo failed inheritance/split
5603 transformations. */
5605 /* Current number of iteration undoing inheritance/split. */
5606 int lra_undo_inheritance_iter;
5608 /* Fix BB live info LIVE after removing pseudos created on pass doing
5609 inheritance/split which are REMOVED_PSEUDOS. */
5610 static void
5611 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5613 unsigned int regno;
5614 bitmap_iterator bi;
5616 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5617 if (bitmap_clear_bit (live, regno))
5618 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5621 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5622 number. */
5623 static int
5624 get_regno (rtx reg)
5626 if (GET_CODE (reg) == SUBREG)
5627 reg = SUBREG_REG (reg);
5628 if (REG_P (reg))
5629 return REGNO (reg);
5630 return -1;
5633 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5634 return true if we did any change. The undo transformations for
5635 inheritance looks like
5636 i <- i2
5637 p <- i => p <- i2
5638 or removing
5639 p <- i, i <- p, and i <- i3
5640 where p is original pseudo from which inheritance pseudo i was
5641 created, i and i3 are removed inheritance pseudos, i2 is another
5642 not removed inheritance pseudo. All split pseudos or other
5643 occurrences of removed inheritance pseudos are changed on the
5644 corresponding original pseudos.
5646 The function also schedules insns changed and created during
5647 inheritance/split pass for processing by the subsequent constraint
5648 pass. */
5649 static bool
5650 remove_inheritance_pseudos (bitmap remove_pseudos)
5652 basic_block bb;
5653 int regno, sregno, prev_sregno, dregno, restore_regno;
5654 rtx set, prev_set, prev_insn;
5655 bool change_p, done_p;
5657 change_p = ! bitmap_empty_p (remove_pseudos);
5658 /* We can not finish the function right away if CHANGE_P is true
5659 because we need to marks insns affected by previous
5660 inheritance/split pass for processing by the subsequent
5661 constraint pass. */
5662 FOR_EACH_BB_FN (bb, cfun)
5664 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5665 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5666 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5668 if (! INSN_P (curr_insn))
5669 continue;
5670 done_p = false;
5671 sregno = dregno = -1;
5672 if (change_p && NONDEBUG_INSN_P (curr_insn)
5673 && (set = single_set (curr_insn)) != NULL_RTX)
5675 dregno = get_regno (SET_DEST (set));
5676 sregno = get_regno (SET_SRC (set));
5679 if (sregno >= 0 && dregno >= 0)
5681 if ((bitmap_bit_p (remove_pseudos, sregno)
5682 && (lra_reg_info[sregno].restore_regno == dregno
5683 || (bitmap_bit_p (remove_pseudos, dregno)
5684 && (lra_reg_info[sregno].restore_regno
5685 == lra_reg_info[dregno].restore_regno))))
5686 || (bitmap_bit_p (remove_pseudos, dregno)
5687 && lra_reg_info[dregno].restore_regno == sregno))
5688 /* One of the following cases:
5689 original <- removed inheritance pseudo
5690 removed inherit pseudo <- another removed inherit pseudo
5691 removed inherit pseudo <- original pseudo
5693 removed_split_pseudo <- original_reg
5694 original_reg <- removed_split_pseudo */
5696 if (lra_dump_file != NULL)
5698 fprintf (lra_dump_file, " Removing %s:\n",
5699 bitmap_bit_p (&lra_split_regs, sregno)
5700 || bitmap_bit_p (&lra_split_regs, dregno)
5701 ? "split" : "inheritance");
5702 dump_insn_slim (lra_dump_file, curr_insn);
5704 lra_set_insn_deleted (curr_insn);
5705 done_p = true;
5707 else if (bitmap_bit_p (remove_pseudos, sregno)
5708 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5710 /* Search the following pattern:
5711 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5712 original_pseudo <- inherit_or_split_pseudo1
5713 where the 2nd insn is the current insn and
5714 inherit_or_split_pseudo2 is not removed. If it is found,
5715 change the current insn onto:
5716 original_pseudo <- inherit_or_split_pseudo2. */
5717 for (prev_insn = PREV_INSN (curr_insn);
5718 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5719 prev_insn = PREV_INSN (prev_insn))
5721 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5722 && (prev_set = single_set (prev_insn)) != NULL_RTX
5723 /* There should be no subregs in insn we are
5724 searching because only the original reg might
5725 be in subreg when we changed the mode of
5726 load/store for splitting. */
5727 && REG_P (SET_DEST (prev_set))
5728 && REG_P (SET_SRC (prev_set))
5729 && (int) REGNO (SET_DEST (prev_set)) == sregno
5730 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5731 >= FIRST_PSEUDO_REGISTER)
5732 /* As we consider chain of inheritance or
5733 splitting described in above comment we should
5734 check that sregno and prev_sregno were
5735 inheritance/split pseudos created from the
5736 same original regno. */
5737 && (lra_reg_info[sregno].restore_regno
5738 == lra_reg_info[prev_sregno].restore_regno)
5739 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5741 lra_assert (GET_MODE (SET_SRC (prev_set))
5742 == GET_MODE (regno_reg_rtx[sregno]));
5743 if (GET_CODE (SET_SRC (set)) == SUBREG)
5744 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5745 else
5746 SET_SRC (set) = SET_SRC (prev_set);
5747 lra_push_insn_and_update_insn_regno_info (curr_insn);
5748 lra_set_used_insn_alternative_by_uid
5749 (INSN_UID (curr_insn), -1);
5750 done_p = true;
5751 if (lra_dump_file != NULL)
5753 fprintf (lra_dump_file, " Change reload insn:\n");
5754 dump_insn_slim (lra_dump_file, curr_insn);
5759 if (! done_p)
5761 struct lra_insn_reg *reg;
5762 bool restored_regs_p = false;
5763 bool kept_regs_p = false;
5765 curr_id = lra_get_insn_recog_data (curr_insn);
5766 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5768 regno = reg->regno;
5769 restore_regno = lra_reg_info[regno].restore_regno;
5770 if (restore_regno >= 0)
5772 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5774 substitute_pseudo (&curr_insn, regno,
5775 regno_reg_rtx[restore_regno]);
5776 restored_regs_p = true;
5778 else
5779 kept_regs_p = true;
5782 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5784 /* The instruction has changed since the previous
5785 constraints pass. */
5786 lra_push_insn_and_update_insn_regno_info (curr_insn);
5787 lra_set_used_insn_alternative_by_uid
5788 (INSN_UID (curr_insn), -1);
5790 else if (restored_regs_p)
5791 /* The instruction has been restored to the form that
5792 it had during the previous constraints pass. */
5793 lra_update_insn_regno_info (curr_insn);
5794 if (restored_regs_p && lra_dump_file != NULL)
5796 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5797 dump_insn_slim (lra_dump_file, curr_insn);
5802 return change_p;
5805 /* If optional reload pseudos failed to get a hard register or was not
5806 inherited, it is better to remove optional reloads. We do this
5807 transformation after undoing inheritance to figure out necessity to
5808 remove optional reloads easier. Return true if we do any
5809 change. */
5810 static bool
5811 undo_optional_reloads (void)
5813 bool change_p, keep_p;
5814 unsigned int regno, uid;
5815 bitmap_iterator bi, bi2;
5816 rtx insn, set, src, dest;
5817 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5819 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5820 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5821 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5823 keep_p = false;
5824 /* Keep optional reloads from previous subpasses. */
5825 if (lra_reg_info[regno].restore_regno < 0
5826 /* If the original pseudo changed its allocation, just
5827 removing the optional pseudo is dangerous as the original
5828 pseudo will have longer live range. */
5829 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
5830 keep_p = true;
5831 else if (reg_renumber[regno] >= 0)
5832 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
5834 insn = lra_insn_recog_data[uid]->insn;
5835 if ((set = single_set (insn)) == NULL_RTX)
5836 continue;
5837 src = SET_SRC (set);
5838 dest = SET_DEST (set);
5839 if (! REG_P (src) || ! REG_P (dest))
5840 continue;
5841 if (REGNO (dest) == regno
5842 /* Ignore insn for optional reloads itself. */
5843 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
5844 /* Check only inheritance on last inheritance pass. */
5845 && (int) REGNO (src) >= new_regno_start
5846 /* Check that the optional reload was inherited. */
5847 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
5849 keep_p = true;
5850 break;
5853 if (keep_p)
5855 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
5856 if (lra_dump_file != NULL)
5857 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
5860 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
5861 bitmap_initialize (&insn_bitmap, &reg_obstack);
5862 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
5864 if (lra_dump_file != NULL)
5865 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
5866 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
5867 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
5869 insn = lra_insn_recog_data[uid]->insn;
5870 if ((set = single_set (insn)) != NULL_RTX)
5872 src = SET_SRC (set);
5873 dest = SET_DEST (set);
5874 if (REG_P (src) && REG_P (dest)
5875 && ((REGNO (src) == regno
5876 && (lra_reg_info[regno].restore_regno
5877 == (int) REGNO (dest)))
5878 || (REGNO (dest) == regno
5879 && (lra_reg_info[regno].restore_regno
5880 == (int) REGNO (src)))))
5882 if (lra_dump_file != NULL)
5884 fprintf (lra_dump_file, " Deleting move %u\n",
5885 INSN_UID (insn));
5886 dump_insn_slim (lra_dump_file, insn);
5888 lra_set_insn_deleted (insn);
5889 continue;
5891 /* We should not worry about generation memory-memory
5892 moves here as if the corresponding inheritance did
5893 not work (inheritance pseudo did not get a hard reg),
5894 we remove the inheritance pseudo and the optional
5895 reload. */
5897 substitute_pseudo (&insn, regno,
5898 regno_reg_rtx[lra_reg_info[regno].restore_regno]);
5899 lra_update_insn_regno_info (insn);
5900 if (lra_dump_file != NULL)
5902 fprintf (lra_dump_file,
5903 " Restoring original insn:\n");
5904 dump_insn_slim (lra_dump_file, insn);
5908 /* Clear restore_regnos. */
5909 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5910 lra_reg_info[regno].restore_regno = -1;
5911 bitmap_clear (&insn_bitmap);
5912 bitmap_clear (&removed_optional_reload_pseudos);
5913 return change_p;
5916 /* Entry function for undoing inheritance/split transformation. Return true
5917 if we did any RTL change in this pass. */
5918 bool
5919 lra_undo_inheritance (void)
5921 unsigned int regno;
5922 int restore_regno, hard_regno;
5923 int n_all_inherit, n_inherit, n_all_split, n_split;
5924 bitmap_head remove_pseudos;
5925 bitmap_iterator bi;
5926 bool change_p;
5928 lra_undo_inheritance_iter++;
5929 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5930 return false;
5931 if (lra_dump_file != NULL)
5932 fprintf (lra_dump_file,
5933 "\n********** Undoing inheritance #%d: **********\n\n",
5934 lra_undo_inheritance_iter);
5935 bitmap_initialize (&remove_pseudos, &reg_obstack);
5936 n_inherit = n_all_inherit = 0;
5937 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5938 if (lra_reg_info[regno].restore_regno >= 0)
5940 n_all_inherit++;
5941 if (reg_renumber[regno] < 0
5942 /* If the original pseudo changed its allocation, just
5943 removing inheritance is dangerous as for changing
5944 allocation we used shorter live-ranges. */
5945 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
5946 bitmap_set_bit (&remove_pseudos, regno);
5947 else
5948 n_inherit++;
5950 if (lra_dump_file != NULL && n_all_inherit != 0)
5951 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
5952 n_inherit, n_all_inherit,
5953 (double) n_inherit / n_all_inherit * 100);
5954 n_split = n_all_split = 0;
5955 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5956 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
5958 n_all_split++;
5959 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
5960 ? reg_renumber[restore_regno] : restore_regno);
5961 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
5962 bitmap_set_bit (&remove_pseudos, regno);
5963 else
5965 n_split++;
5966 if (lra_dump_file != NULL)
5967 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
5968 regno, restore_regno);
5971 if (lra_dump_file != NULL && n_all_split != 0)
5972 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
5973 n_split, n_all_split,
5974 (double) n_split / n_all_split * 100);
5975 change_p = remove_inheritance_pseudos (&remove_pseudos);
5976 bitmap_clear (&remove_pseudos);
5977 /* Clear restore_regnos. */
5978 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5979 lra_reg_info[regno].restore_regno = -1;
5980 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5981 lra_reg_info[regno].restore_regno = -1;
5982 change_p = undo_optional_reloads () || change_p;
5983 return change_p;