gcc/ChangeLog
[official-gcc.git] / gcc / lra-constraints.c
blob038acd72148c70959f05c44447e0c4ade610bd54
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2013 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 /* When we don't know what class will be used finally for reload
274 pseudos, we use ALL_REGS. */
275 return ((regno >= new_regno_start && rclass == ALL_REGS)
276 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
277 && ! hard_reg_set_subset_p (reg_class_contents[cl],
278 lra_no_alloc_regs)));
279 else
281 common_class = ira_reg_class_subset[rclass][cl];
282 if (new_class != NULL)
283 *new_class = common_class;
284 if (hard_reg_set_subset_p (reg_class_contents[common_class],
285 lra_no_alloc_regs))
286 return false;
287 /* Check that there are enough allocatable regs. */
288 class_size = ira_class_hard_regs_num[common_class];
289 for (i = 0; i < class_size; i++)
291 hard_regno = ira_class_hard_regs[common_class][i];
292 nregs = hard_regno_nregs[hard_regno][reg_mode];
293 if (nregs == 1)
294 return true;
295 for (j = 0; j < nregs; j++)
296 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
297 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
298 hard_regno + j))
299 break;
300 if (j >= nregs)
301 return true;
303 return false;
307 /* Return true if REGNO satisfies a memory constraint. */
308 static bool
309 in_mem_p (int regno)
311 return get_reg_class (regno) == NO_REGS;
314 /* If we have decided to substitute X with another value, return that
315 value, otherwise return X. */
316 static rtx
317 get_equiv_substitution (rtx x)
319 int regno;
320 rtx res;
322 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
323 || ! ira_reg_equiv[regno].defined_p
324 || ! ira_reg_equiv[regno].profitable_p
325 || lra_get_regno_hard_regno (regno) >= 0)
326 return x;
327 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
328 return res;
329 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
330 return res;
331 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
332 return res;
333 gcc_unreachable ();
336 /* Set up curr_operand_mode. */
337 static void
338 init_curr_operand_mode (void)
340 int nop = curr_static_id->n_operands;
341 for (int i = 0; i < nop; i++)
343 enum machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
344 if (mode == VOIDmode)
346 /* The .md mode for address operands is the mode of the
347 addressed value rather than the mode of the address itself. */
348 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
349 mode = Pmode;
350 else
351 mode = curr_static_id->operand[i].mode;
353 curr_operand_mode[i] = mode;
359 /* The page contains code to reuse input reloads. */
361 /* Structure describes input reload of the current insns. */
362 struct input_reload
364 /* Reloaded value. */
365 rtx input;
366 /* Reload pseudo used. */
367 rtx reg;
370 /* The number of elements in the following array. */
371 static int curr_insn_input_reloads_num;
372 /* Array containing info about input reloads. It is used to find the
373 same input reload and reuse the reload pseudo in this case. */
374 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
376 /* Initiate data concerning reuse of input reloads for the current
377 insn. */
378 static void
379 init_curr_insn_input_reloads (void)
381 curr_insn_input_reloads_num = 0;
384 /* Change class of pseudo REGNO to NEW_CLASS. Print info about it
385 using TITLE. Output a new line if NL_P. */
386 static void
387 change_class (int regno, enum reg_class new_class,
388 const char *title, bool nl_p)
390 lra_assert (regno >= FIRST_PSEUDO_REGISTER);
391 if (lra_dump_file != NULL)
392 fprintf (lra_dump_file, "%s to class %s for r%d",
393 title, reg_class_names[new_class], regno);
394 setup_reg_classes (regno, new_class, NO_REGS, new_class);
395 if (lra_dump_file != NULL && nl_p)
396 fprintf (lra_dump_file, "\n");
399 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
400 created input reload pseudo (only if TYPE is not OP_OUT). The
401 result pseudo is returned through RESULT_REG. Return TRUE if we
402 created a new pseudo, FALSE if we reused the already created input
403 reload pseudo. Use TITLE to describe new registers for debug
404 purposes. */
405 static bool
406 get_reload_reg (enum op_type type, enum machine_mode mode, rtx original,
407 enum reg_class rclass, const char *title, rtx *result_reg)
409 int i, regno;
410 enum reg_class new_class;
412 if (type == OP_OUT)
414 *result_reg
415 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
416 return true;
418 /* Prevent reuse value of expression with side effects,
419 e.g. volatile memory. */
420 if (! side_effects_p (original))
421 for (i = 0; i < curr_insn_input_reloads_num; i++)
422 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
423 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
425 rtx reg = curr_insn_input_reloads[i].reg;
426 regno = REGNO (reg);
427 /* If input is equal to original and both are VOIDmode,
428 GET_MODE (reg) might be still different from mode.
429 Ensure we don't return *result_reg with wrong mode. */
430 if (GET_MODE (reg) != mode)
432 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
433 continue;
434 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
435 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
436 continue;
438 *result_reg = reg;
439 if (lra_dump_file != NULL)
441 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
442 dump_value_slim (lra_dump_file, original, 1);
444 if (new_class != lra_get_allocno_class (regno))
445 change_class (regno, new_class, ", change", false);
446 if (lra_dump_file != NULL)
447 fprintf (lra_dump_file, "\n");
448 return false;
450 *result_reg = lra_create_new_reg (mode, original, rclass, title);
451 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
452 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
453 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
454 return true;
459 /* The page contains code to extract memory address parts. */
461 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
462 static inline bool
463 ok_for_index_p_nonstrict (rtx reg)
465 unsigned regno = REGNO (reg);
467 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
470 /* A version of regno_ok_for_base_p for use here, when all pseudos
471 should count as OK. Arguments as for regno_ok_for_base_p. */
472 static inline bool
473 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode, addr_space_t as,
474 enum rtx_code outer_code, enum rtx_code index_code)
476 unsigned regno = REGNO (reg);
478 if (regno >= FIRST_PSEUDO_REGISTER)
479 return true;
480 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
485 /* The page contains major code to choose the current insn alternative
486 and generate reloads for it. */
488 /* Return the offset from REGNO of the least significant register
489 in (reg:MODE REGNO).
491 This function is used to tell whether two registers satisfy
492 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
494 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
495 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
497 lra_constraint_offset (int regno, enum machine_mode mode)
499 lra_assert (regno < FIRST_PSEUDO_REGISTER);
500 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
501 && SCALAR_INT_MODE_P (mode))
502 return hard_regno_nregs[regno][mode] - 1;
503 return 0;
506 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
507 if they are the same hard reg, and has special hacks for
508 auto-increment and auto-decrement. This is specifically intended for
509 process_alt_operands to use in determining whether two operands
510 match. X is the operand whose number is the lower of the two.
512 It is supposed that X is the output operand and Y is the input
513 operand. Y_HARD_REGNO is the final hard regno of register Y or
514 register in subreg Y as we know it now. Otherwise, it is a
515 negative value. */
516 static bool
517 operands_match_p (rtx x, rtx y, int y_hard_regno)
519 int i;
520 RTX_CODE code = GET_CODE (x);
521 const char *fmt;
523 if (x == y)
524 return true;
525 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
526 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
528 int j;
530 i = get_hard_regno (x);
531 if (i < 0)
532 goto slow;
534 if ((j = y_hard_regno) < 0)
535 goto slow;
537 i += lra_constraint_offset (i, GET_MODE (x));
538 j += lra_constraint_offset (j, GET_MODE (y));
540 return i == j;
543 /* If two operands must match, because they are really a single
544 operand of an assembler insn, then two post-increments are invalid
545 because the assembler insn would increment only once. On the
546 other hand, a post-increment matches ordinary indexing if the
547 post-increment is the output operand. */
548 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
549 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
551 /* Two pre-increments are invalid because the assembler insn would
552 increment only once. On the other hand, a pre-increment matches
553 ordinary indexing if the pre-increment is the input operand. */
554 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
555 || GET_CODE (y) == PRE_MODIFY)
556 return operands_match_p (x, XEXP (y, 0), -1);
558 slow:
560 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
561 && x == SUBREG_REG (y))
562 return true;
563 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
564 && SUBREG_REG (x) == y)
565 return true;
567 /* Now we have disposed of all the cases in which different rtx
568 codes can match. */
569 if (code != GET_CODE (y))
570 return false;
572 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
573 if (GET_MODE (x) != GET_MODE (y))
574 return false;
576 switch (code)
578 CASE_CONST_UNIQUE:
579 return false;
581 case LABEL_REF:
582 return XEXP (x, 0) == XEXP (y, 0);
583 case SYMBOL_REF:
584 return XSTR (x, 0) == XSTR (y, 0);
586 default:
587 break;
590 /* Compare the elements. If any pair of corresponding elements fail
591 to match, return false for the whole things. */
593 fmt = GET_RTX_FORMAT (code);
594 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
596 int val, j;
597 switch (fmt[i])
599 case 'w':
600 if (XWINT (x, i) != XWINT (y, i))
601 return false;
602 break;
604 case 'i':
605 if (XINT (x, i) != XINT (y, i))
606 return false;
607 break;
609 case 'e':
610 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
611 if (val == 0)
612 return false;
613 break;
615 case '0':
616 break;
618 case 'E':
619 if (XVECLEN (x, i) != XVECLEN (y, i))
620 return false;
621 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
623 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
624 if (val == 0)
625 return false;
627 break;
629 /* It is believed that rtx's at this level will never
630 contain anything but integers and other rtx's, except for
631 within LABEL_REFs and SYMBOL_REFs. */
632 default:
633 gcc_unreachable ();
636 return true;
639 /* True if X is a constant that can be forced into the constant pool.
640 MODE is the mode of the operand, or VOIDmode if not known. */
641 #define CONST_POOL_OK_P(MODE, X) \
642 ((MODE) != VOIDmode \
643 && CONSTANT_P (X) \
644 && GET_CODE (X) != HIGH \
645 && !targetm.cannot_force_const_mem (MODE, X))
647 /* True if C is a non-empty register class that has too few registers
648 to be safely used as a reload target class. */
649 #define SMALL_REGISTER_CLASS_P(C) \
650 (reg_class_size [(C)] == 1 \
651 || (reg_class_size [(C)] >= 1 && targetm.class_likely_spilled_p (C)))
653 /* If REG is a reload pseudo, try to make its class satisfying CL. */
654 static void
655 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
657 enum reg_class rclass;
659 /* Do not make more accurate class from reloads generated. They are
660 mostly moves with a lot of constraints. Making more accurate
661 class may results in very narrow class and impossibility of find
662 registers for several reloads of one insn. */
663 if (INSN_UID (curr_insn) >= new_insn_uid_start)
664 return;
665 if (GET_CODE (reg) == SUBREG)
666 reg = SUBREG_REG (reg);
667 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
668 return;
669 if (in_class_p (reg, cl, &rclass) && rclass != cl)
670 change_class (REGNO (reg), rclass, " Change", true);
673 /* Generate reloads for matching OUT and INS (array of input operand
674 numbers with end marker -1) with reg class GOAL_CLASS. Add input
675 and output reloads correspondingly to the lists *BEFORE and *AFTER.
676 OUT might be negative. In this case we generate input reloads for
677 matched input operands INS. */
678 static void
679 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
680 rtx *before, rtx *after)
682 int i, in;
683 rtx new_in_reg, new_out_reg, reg, clobber;
684 enum machine_mode inmode, outmode;
685 rtx in_rtx = *curr_id->operand_loc[ins[0]];
686 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
688 inmode = curr_operand_mode[ins[0]];
689 outmode = out < 0 ? inmode : curr_operand_mode[out];
690 push_to_sequence (*before);
691 if (inmode != outmode)
693 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
695 reg = new_in_reg
696 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
697 goal_class, "");
698 if (SCALAR_INT_MODE_P (inmode))
699 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
700 else
701 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
702 LRA_SUBREG_P (new_out_reg) = 1;
703 /* If the input reg is dying here, we can use the same hard
704 register for REG and IN_RTX. We do it only for original
705 pseudos as reload pseudos can die although original
706 pseudos still live where reload pseudos dies. */
707 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
708 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
709 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
711 else
713 reg = new_out_reg
714 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
715 goal_class, "");
716 if (SCALAR_INT_MODE_P (outmode))
717 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
718 else
719 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
720 /* NEW_IN_REG is non-paradoxical subreg. We don't want
721 NEW_OUT_REG living above. We add clobber clause for
722 this. This is just a temporary clobber. We can remove
723 it at the end of LRA work. */
724 clobber = emit_clobber (new_out_reg);
725 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
726 LRA_SUBREG_P (new_in_reg) = 1;
727 if (GET_CODE (in_rtx) == SUBREG)
729 rtx subreg_reg = SUBREG_REG (in_rtx);
731 /* If SUBREG_REG is dying here and sub-registers IN_RTX
732 and NEW_IN_REG are similar, we can use the same hard
733 register for REG and SUBREG_REG. */
734 if (REG_P (subreg_reg)
735 && (int) REGNO (subreg_reg) < lra_new_regno_start
736 && GET_MODE (subreg_reg) == outmode
737 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
738 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
739 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
743 else
745 /* Pseudos have values -- see comments for lra_reg_info.
746 Different pseudos with the same value do not conflict even if
747 they live in the same place. When we create a pseudo we
748 assign value of original pseudo (if any) from which we
749 created the new pseudo. If we create the pseudo from the
750 input pseudo, the new pseudo will no conflict with the input
751 pseudo which is wrong when the input pseudo lives after the
752 insn and as the new pseudo value is changed by the insn
753 output. Therefore we create the new pseudo from the output.
755 We cannot reuse the current output register because we might
756 have a situation like "a <- a op b", where the constraints
757 force the second input operand ("b") to match the output
758 operand ("a"). "b" must then be copied into a new register
759 so that it doesn't clobber the current value of "a". */
761 new_in_reg = new_out_reg
762 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
763 goal_class, "");
765 /* In operand can be got from transformations before processing insn
766 constraints. One example of such transformations is subreg
767 reloading (see function simplify_operand_subreg). The new
768 pseudos created by the transformations might have inaccurate
769 class (ALL_REGS) and we should make their classes more
770 accurate. */
771 narrow_reload_pseudo_class (in_rtx, goal_class);
772 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
773 *before = get_insns ();
774 end_sequence ();
775 for (i = 0; (in = ins[i]) >= 0; i++)
777 lra_assert
778 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
779 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
780 *curr_id->operand_loc[in] = new_in_reg;
782 lra_update_dups (curr_id, ins);
783 if (out < 0)
784 return;
785 /* See a comment for the input operand above. */
786 narrow_reload_pseudo_class (out_rtx, goal_class);
787 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
789 start_sequence ();
790 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
791 emit_insn (*after);
792 *after = get_insns ();
793 end_sequence ();
795 *curr_id->operand_loc[out] = new_out_reg;
796 lra_update_dup (curr_id, out);
799 /* Return register class which is union of all reg classes in insn
800 constraint alternative string starting with P. */
801 static enum reg_class
802 reg_class_from_constraints (const char *p)
804 int c, len;
805 enum reg_class op_class = NO_REGS;
808 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
810 case '#':
811 case ',':
812 return op_class;
814 case 'p':
815 op_class = (reg_class_subunion
816 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
817 ADDRESS, SCRATCH)]);
818 break;
820 case 'g':
821 case 'r':
822 op_class = reg_class_subunion[op_class][GENERAL_REGS];
823 break;
825 default:
826 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
828 #ifdef EXTRA_CONSTRAINT_STR
829 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
830 op_class
831 = (reg_class_subunion
832 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
833 ADDRESS, SCRATCH)]);
834 #endif
835 break;
838 op_class
839 = reg_class_subunion[op_class][REG_CLASS_FROM_CONSTRAINT (c, p)];
840 break;
842 while ((p += len), c);
843 return op_class;
846 /* If OP is a register, return the class of the register as per
847 get_reg_class, otherwise return NO_REGS. */
848 static inline enum reg_class
849 get_op_class (rtx op)
851 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
854 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
855 otherwise. If modes of MEM_PSEUDO and VAL are different, use
856 SUBREG for VAL to make them equal. */
857 static rtx
858 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
860 if (GET_MODE (mem_pseudo) != GET_MODE (val))
862 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
863 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
865 LRA_SUBREG_P (val) = 1;
867 return (to_p
868 ? gen_move_insn (mem_pseudo, val)
869 : gen_move_insn (val, mem_pseudo));
872 /* Process a special case insn (register move), return true if we
873 don't need to process it anymore. INSN should be a single set
874 insn. Set up that RTL was changed through CHANGE_P and macro
875 SECONDARY_MEMORY_NEEDED says to use secondary memory through
876 SEC_MEM_P. */
877 static bool
878 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
880 int sregno, dregno;
881 rtx dest, src, dreg, sreg, old_sreg, new_reg, before, scratch_reg;
882 enum reg_class dclass, sclass, secondary_class;
883 enum machine_mode sreg_mode;
884 secondary_reload_info sri;
886 lra_assert (curr_insn_set != NULL_RTX);
887 dreg = dest = SET_DEST (curr_insn_set);
888 sreg = src = SET_SRC (curr_insn_set);
889 if (GET_CODE (dest) == SUBREG)
890 dreg = SUBREG_REG (dest);
891 if (GET_CODE (src) == SUBREG)
892 sreg = SUBREG_REG (src);
893 if (! REG_P (dreg) || ! REG_P (sreg))
894 return false;
895 sclass = dclass = NO_REGS;
896 if (REG_P (dreg))
897 dclass = get_reg_class (REGNO (dreg));
898 if (dclass == ALL_REGS)
899 /* ALL_REGS is used for new pseudos created by transformations
900 like reload of SUBREG_REG (see function
901 simplify_operand_subreg). We don't know their class yet. We
902 should figure out the class from processing the insn
903 constraints not in this fast path function. Even if ALL_REGS
904 were a right class for the pseudo, secondary_... hooks usually
905 are not define for ALL_REGS. */
906 return false;
907 sreg_mode = GET_MODE (sreg);
908 old_sreg = sreg;
909 if (REG_P (sreg))
910 sclass = get_reg_class (REGNO (sreg));
911 if (sclass == ALL_REGS)
912 /* See comments above. */
913 return false;
914 #ifdef SECONDARY_MEMORY_NEEDED
915 if (dclass != NO_REGS && sclass != NO_REGS
916 && SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src)))
918 *sec_mem_p = true;
919 return false;
921 #endif
922 sri.prev_sri = NULL;
923 sri.icode = CODE_FOR_nothing;
924 sri.extra_cost = 0;
925 secondary_class = NO_REGS;
926 /* Set up hard register for a reload pseudo for hook
927 secondary_reload because some targets just ignore unassigned
928 pseudos in the hook. */
929 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
931 dregno = REGNO (dreg);
932 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
934 else
935 dregno = -1;
936 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
938 sregno = REGNO (sreg);
939 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
941 else
942 sregno = -1;
943 if (sclass != NO_REGS)
944 secondary_class
945 = (enum reg_class) targetm.secondary_reload (false, dest,
946 (reg_class_t) sclass,
947 GET_MODE (src), &sri);
948 if (sclass == NO_REGS
949 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
950 && dclass != NO_REGS))
952 enum reg_class old_sclass = secondary_class;
953 secondary_reload_info old_sri = sri;
955 sri.prev_sri = NULL;
956 sri.icode = CODE_FOR_nothing;
957 sri.extra_cost = 0;
958 secondary_class
959 = (enum reg_class) targetm.secondary_reload (true, sreg,
960 (reg_class_t) dclass,
961 sreg_mode, &sri);
962 /* Check the target hook consistency. */
963 lra_assert
964 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
965 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
966 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
968 if (sregno >= 0)
969 reg_renumber [sregno] = -1;
970 if (dregno >= 0)
971 reg_renumber [dregno] = -1;
972 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
973 return false;
974 *change_p = true;
975 new_reg = NULL_RTX;
976 if (secondary_class != NO_REGS)
977 new_reg = lra_create_new_reg_with_unique_value (sreg_mode, NULL_RTX,
978 secondary_class,
979 "secondary");
980 start_sequence ();
981 if (old_sreg != sreg)
982 sreg = copy_rtx (sreg);
983 if (sri.icode == CODE_FOR_nothing)
984 lra_emit_move (new_reg, sreg);
985 else
987 enum reg_class scratch_class;
989 scratch_class = (reg_class_from_constraints
990 (insn_data[sri.icode].operand[2].constraint));
991 scratch_reg = (lra_create_new_reg_with_unique_value
992 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
993 scratch_class, "scratch"));
994 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
995 sreg, scratch_reg));
997 before = get_insns ();
998 end_sequence ();
999 lra_process_new_insns (curr_insn, before, NULL_RTX, "Inserting the move");
1000 if (new_reg != NULL_RTX)
1002 if (GET_CODE (src) == SUBREG)
1003 SUBREG_REG (src) = new_reg;
1004 else
1005 SET_SRC (curr_insn_set) = new_reg;
1007 else
1009 if (lra_dump_file != NULL)
1011 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1012 dump_insn_slim (lra_dump_file, curr_insn);
1014 lra_set_insn_deleted (curr_insn);
1015 return true;
1017 return false;
1020 /* The following data describe the result of process_alt_operands.
1021 The data are used in curr_insn_transform to generate reloads. */
1023 /* The chosen reg classes which should be used for the corresponding
1024 operands. */
1025 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1026 /* True if the operand should be the same as another operand and that
1027 other operand does not need a reload. */
1028 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1029 /* True if the operand does not need a reload. */
1030 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1031 /* True if the operand can be offsetable memory. */
1032 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1033 /* The number of an operand to which given operand can be matched to. */
1034 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1035 /* The number of elements in the following array. */
1036 static int goal_alt_dont_inherit_ops_num;
1037 /* Numbers of operands whose reload pseudos should not be inherited. */
1038 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1039 /* True if the insn commutative operands should be swapped. */
1040 static bool goal_alt_swapped;
1041 /* The chosen insn alternative. */
1042 static int goal_alt_number;
1044 /* The following five variables are used to choose the best insn
1045 alternative. They reflect final characteristics of the best
1046 alternative. */
1048 /* Number of necessary reloads and overall cost reflecting the
1049 previous value and other unpleasantness of the best alternative. */
1050 static int best_losers, best_overall;
1051 /* Overall number hard registers used for reloads. For example, on
1052 some targets we need 2 general registers to reload DFmode and only
1053 one floating point register. */
1054 static int best_reload_nregs;
1055 /* Overall number reflecting distances of previous reloading the same
1056 value. The distances are counted from the current BB start. It is
1057 used to improve inheritance chances. */
1058 static int best_reload_sum;
1060 /* True if the current insn should have no correspondingly input or
1061 output reloads. */
1062 static bool no_input_reloads_p, no_output_reloads_p;
1064 /* True if we swapped the commutative operands in the current
1065 insn. */
1066 static int curr_swapped;
1068 /* Arrange for address element *LOC to be a register of class CL.
1069 Add any input reloads to list BEFORE. AFTER is nonnull if *LOC is an
1070 automodified value; handle that case by adding the required output
1071 reloads to list AFTER. Return true if the RTL was changed. */
1072 static bool
1073 process_addr_reg (rtx *loc, rtx *before, rtx *after, enum reg_class cl)
1075 int regno;
1076 enum reg_class rclass, new_class;
1077 rtx reg;
1078 rtx new_reg;
1079 enum machine_mode mode;
1080 bool before_p = false;
1082 loc = strip_subreg (loc);
1083 reg = *loc;
1084 mode = GET_MODE (reg);
1085 if (! REG_P (reg))
1087 /* Always reload memory in an address even if the target supports
1088 such addresses. */
1089 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1090 before_p = true;
1092 else
1094 regno = REGNO (reg);
1095 rclass = get_reg_class (regno);
1096 if ((*loc = get_equiv_substitution (reg)) != reg)
1098 if (lra_dump_file != NULL)
1100 fprintf (lra_dump_file,
1101 "Changing pseudo %d in address of insn %u on equiv ",
1102 REGNO (reg), INSN_UID (curr_insn));
1103 dump_value_slim (lra_dump_file, *loc, 1);
1104 fprintf (lra_dump_file, "\n");
1106 *loc = copy_rtx (*loc);
1108 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1110 reg = *loc;
1111 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1112 mode, reg, cl, "address", &new_reg))
1113 before_p = true;
1115 else if (new_class != NO_REGS && rclass != new_class)
1117 change_class (regno, new_class, " Change", true);
1118 return false;
1120 else
1121 return false;
1123 if (before_p)
1125 push_to_sequence (*before);
1126 lra_emit_move (new_reg, reg);
1127 *before = get_insns ();
1128 end_sequence ();
1130 *loc = new_reg;
1131 if (after != NULL)
1133 start_sequence ();
1134 lra_emit_move (reg, new_reg);
1135 emit_insn (*after);
1136 *after = get_insns ();
1137 end_sequence ();
1139 return true;
1142 /* Make reloads for subreg in operand NOP with internal subreg mode
1143 REG_MODE, add new reloads for further processing. Return true if
1144 any reload was generated. */
1145 static bool
1146 simplify_operand_subreg (int nop, enum machine_mode reg_mode)
1148 int hard_regno;
1149 rtx before, after;
1150 enum machine_mode mode;
1151 rtx reg, new_reg;
1152 rtx operand = *curr_id->operand_loc[nop];
1154 before = after = NULL_RTX;
1156 if (GET_CODE (operand) != SUBREG)
1157 return false;
1159 mode = GET_MODE (operand);
1160 reg = SUBREG_REG (operand);
1161 /* If we change address for paradoxical subreg of memory, the
1162 address might violate the necessary alignment or the access might
1163 be slow. So take this into consideration. We should not worry
1164 about access beyond allocated memory for paradoxical memory
1165 subregs as we don't substitute such equiv memory (see processing
1166 equivalences in function lra_constraints) and because for spilled
1167 pseudos we allocate stack memory enough for the biggest
1168 corresponding paradoxical subreg. */
1169 if ((MEM_P (reg)
1170 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1171 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1172 || (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER))
1174 alter_subreg (curr_id->operand_loc[nop], false);
1175 return true;
1177 /* Put constant into memory when we have mixed modes. It generates
1178 a better code in most cases as it does not need a secondary
1179 reload memory. It also prevents LRA looping when LRA is using
1180 secondary reload memory again and again. */
1181 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1182 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1184 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1185 alter_subreg (curr_id->operand_loc[nop], false);
1186 return true;
1188 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1189 if there may be a problem accessing OPERAND in the outer
1190 mode. */
1191 if ((REG_P (reg)
1192 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1193 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1194 /* Don't reload paradoxical subregs because we could be looping
1195 having repeatedly final regno out of hard regs range. */
1196 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1197 >= hard_regno_nregs[hard_regno][mode])
1198 && simplify_subreg_regno (hard_regno, GET_MODE (reg),
1199 SUBREG_BYTE (operand), mode) < 0
1200 /* Don't reload subreg for matching reload. It is actually
1201 valid subreg in LRA. */
1202 && ! LRA_SUBREG_P (operand))
1203 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1205 enum op_type type = curr_static_id->operand[nop].type;
1206 /* The class will be defined later in curr_insn_transform. */
1207 enum reg_class rclass
1208 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1210 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1211 rclass, "subreg reg", &new_reg))
1213 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (new_reg));
1214 if (type != OP_OUT
1215 || GET_MODE_SIZE (GET_MODE (reg)) > GET_MODE_SIZE (mode))
1217 push_to_sequence (before);
1218 lra_emit_move (new_reg, reg);
1219 before = get_insns ();
1220 end_sequence ();
1222 if (type != OP_IN)
1224 start_sequence ();
1225 lra_emit_move (reg, new_reg);
1226 emit_insn (after);
1227 after = get_insns ();
1228 end_sequence ();
1231 SUBREG_REG (operand) = new_reg;
1232 lra_process_new_insns (curr_insn, before, after,
1233 "Inserting subreg reload");
1234 return true;
1236 return false;
1239 /* Return TRUE if X refers for a hard register from SET. */
1240 static bool
1241 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1243 int i, j, x_hard_regno;
1244 enum machine_mode mode;
1245 const char *fmt;
1246 enum rtx_code code;
1248 if (x == NULL_RTX)
1249 return false;
1250 code = GET_CODE (x);
1251 mode = GET_MODE (x);
1252 if (code == SUBREG)
1254 x = SUBREG_REG (x);
1255 code = GET_CODE (x);
1256 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1257 mode = GET_MODE (x);
1260 if (REG_P (x))
1262 x_hard_regno = get_hard_regno (x);
1263 return (x_hard_regno >= 0
1264 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1266 if (MEM_P (x))
1268 struct address_info ad;
1270 decompose_mem_address (&ad, x);
1271 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1272 return true;
1273 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1274 return true;
1276 fmt = GET_RTX_FORMAT (code);
1277 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1279 if (fmt[i] == 'e')
1281 if (uses_hard_regs_p (XEXP (x, i), set))
1282 return true;
1284 else if (fmt[i] == 'E')
1286 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1287 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1288 return true;
1291 return false;
1294 /* Return true if OP is a spilled pseudo. */
1295 static inline bool
1296 spilled_pseudo_p (rtx op)
1298 return (REG_P (op)
1299 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1302 /* Return true if X is a general constant. */
1303 static inline bool
1304 general_constant_p (rtx x)
1306 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1309 static bool
1310 reg_in_class_p (rtx reg, enum reg_class cl)
1312 if (cl == NO_REGS)
1313 return get_reg_class (REGNO (reg)) == NO_REGS;
1314 return in_class_p (reg, cl, NULL);
1317 /* Major function to choose the current insn alternative and what
1318 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1319 negative we should consider only this alternative. Return false if
1320 we can not choose the alternative or find how to reload the
1321 operands. */
1322 static bool
1323 process_alt_operands (int only_alternative)
1325 bool ok_p = false;
1326 int nop, overall, nalt;
1327 int n_alternatives = curr_static_id->n_alternatives;
1328 int n_operands = curr_static_id->n_operands;
1329 /* LOSERS counts the operands that don't fit this alternative and
1330 would require loading. */
1331 int losers;
1332 /* REJECT is a count of how undesirable this alternative says it is
1333 if any reloading is required. If the alternative matches exactly
1334 then REJECT is ignored, but otherwise it gets this much counted
1335 against it in addition to the reloading needed. */
1336 int reject;
1337 /* The number of elements in the following array. */
1338 int early_clobbered_regs_num;
1339 /* Numbers of operands which are early clobber registers. */
1340 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1341 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1342 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1343 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1344 bool curr_alt_win[MAX_RECOG_OPERANDS];
1345 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1346 int curr_alt_matches[MAX_RECOG_OPERANDS];
1347 /* The number of elements in the following array. */
1348 int curr_alt_dont_inherit_ops_num;
1349 /* Numbers of operands whose reload pseudos should not be inherited. */
1350 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1351 rtx op;
1352 /* The register when the operand is a subreg of register, otherwise the
1353 operand itself. */
1354 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1355 /* The register if the operand is a register or subreg of register,
1356 otherwise NULL. */
1357 rtx operand_reg[MAX_RECOG_OPERANDS];
1358 int hard_regno[MAX_RECOG_OPERANDS];
1359 enum machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1360 int reload_nregs, reload_sum;
1361 bool costly_p;
1362 enum reg_class cl;
1364 /* Calculate some data common for all alternatives to speed up the
1365 function. */
1366 for (nop = 0; nop < n_operands; nop++)
1368 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1369 /* The real hard regno of the operand after the allocation. */
1370 hard_regno[nop] = get_hard_regno (op);
1372 operand_reg[nop] = op;
1373 biggest_mode[nop] = GET_MODE (operand_reg[nop]);
1374 if (GET_CODE (operand_reg[nop]) == SUBREG)
1376 operand_reg[nop] = SUBREG_REG (operand_reg[nop]);
1377 if (GET_MODE_SIZE (biggest_mode[nop])
1378 < GET_MODE_SIZE (GET_MODE (operand_reg[nop])))
1379 biggest_mode[nop] = GET_MODE (operand_reg[nop]);
1381 if (REG_P (operand_reg[nop]))
1382 no_subreg_reg_operand[nop] = operand_reg[nop];
1383 else
1384 operand_reg[nop] = NULL_RTX;
1387 /* The constraints are made of several alternatives. Each operand's
1388 constraint looks like foo,bar,... with commas separating the
1389 alternatives. The first alternatives for all operands go
1390 together, the second alternatives go together, etc.
1392 First loop over alternatives. */
1393 for (nalt = 0; nalt < n_alternatives; nalt++)
1395 /* Loop over operands for one constraint alternative. */
1396 #if HAVE_ATTR_enabled
1397 if (curr_id->alternative_enabled_p != NULL
1398 && ! curr_id->alternative_enabled_p[nalt])
1399 continue;
1400 #endif
1402 if (only_alternative >= 0 && nalt != only_alternative)
1403 continue;
1406 overall = losers = reject = reload_nregs = reload_sum = 0;
1407 for (nop = 0; nop < n_operands; nop++)
1408 reject += (curr_static_id
1409 ->operand_alternative[nalt * n_operands + nop].reject);
1410 early_clobbered_regs_num = 0;
1412 for (nop = 0; nop < n_operands; nop++)
1414 const char *p;
1415 char *end;
1416 int len, c, m, i, opalt_num, this_alternative_matches;
1417 bool win, did_match, offmemok, early_clobber_p;
1418 /* false => this operand can be reloaded somehow for this
1419 alternative. */
1420 bool badop;
1421 /* true => this operand can be reloaded if the alternative
1422 allows regs. */
1423 bool winreg;
1424 /* True if a constant forced into memory would be OK for
1425 this operand. */
1426 bool constmemok;
1427 enum reg_class this_alternative, this_costly_alternative;
1428 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1429 bool this_alternative_match_win, this_alternative_win;
1430 bool this_alternative_offmemok;
1431 enum machine_mode mode;
1433 opalt_num = nalt * n_operands + nop;
1434 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1436 /* Fast track for no constraints at all. */
1437 curr_alt[nop] = NO_REGS;
1438 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1439 curr_alt_win[nop] = true;
1440 curr_alt_match_win[nop] = false;
1441 curr_alt_offmemok[nop] = false;
1442 curr_alt_matches[nop] = -1;
1443 continue;
1446 op = no_subreg_reg_operand[nop];
1447 mode = curr_operand_mode[nop];
1449 win = did_match = winreg = offmemok = constmemok = false;
1450 badop = true;
1452 early_clobber_p = false;
1453 p = curr_static_id->operand_alternative[opalt_num].constraint;
1455 this_costly_alternative = this_alternative = NO_REGS;
1456 /* We update set of possible hard regs besides its class
1457 because reg class might be inaccurate. For example,
1458 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1459 is translated in HI_REGS because classes are merged by
1460 pairs and there is no accurate intermediate class. */
1461 CLEAR_HARD_REG_SET (this_alternative_set);
1462 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1463 this_alternative_win = false;
1464 this_alternative_match_win = false;
1465 this_alternative_offmemok = false;
1466 this_alternative_matches = -1;
1468 /* An empty constraint should be excluded by the fast
1469 track. */
1470 lra_assert (*p != 0 && *p != ',');
1472 /* Scan this alternative's specs for this operand; set WIN
1473 if the operand fits any letter in this alternative.
1474 Otherwise, clear BADOP if this operand could fit some
1475 letter after reloads, or set WINREG if this operand could
1476 fit after reloads provided the constraint allows some
1477 registers. */
1478 costly_p = false;
1481 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1483 case '\0':
1484 len = 0;
1485 break;
1486 case ',':
1487 c = '\0';
1488 break;
1490 case '=': case '+': case '?': case '*': case '!':
1491 case ' ': case '\t':
1492 break;
1494 case '%':
1495 /* We only support one commutative marker, the first
1496 one. We already set commutative above. */
1497 break;
1499 case '&':
1500 early_clobber_p = true;
1501 break;
1503 case '#':
1504 /* Ignore rest of this alternative. */
1505 c = '\0';
1506 break;
1508 case '0': case '1': case '2': case '3': case '4':
1509 case '5': case '6': case '7': case '8': case '9':
1511 int m_hregno;
1512 bool match_p;
1514 m = strtoul (p, &end, 10);
1515 p = end;
1516 len = 0;
1517 lra_assert (nop > m);
1519 this_alternative_matches = m;
1520 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1521 /* We are supposed to match a previous operand.
1522 If we do, we win if that one did. If we do
1523 not, count both of the operands as losers.
1524 (This is too conservative, since most of the
1525 time only a single reload insn will be needed
1526 to make the two operands win. As a result,
1527 this alternative may be rejected when it is
1528 actually desirable.) */
1529 match_p = false;
1530 if (operands_match_p (*curr_id->operand_loc[nop],
1531 *curr_id->operand_loc[m], m_hregno))
1533 /* We should reject matching of an early
1534 clobber operand if the matching operand is
1535 not dying in the insn. */
1536 if (! curr_static_id->operand[m].early_clobber
1537 || operand_reg[nop] == NULL_RTX
1538 || (find_regno_note (curr_insn, REG_DEAD,
1539 REGNO (op))
1540 || REGNO (op) == REGNO (operand_reg[m])))
1541 match_p = true;
1543 if (match_p)
1545 /* If we are matching a non-offsettable
1546 address where an offsettable address was
1547 expected, then we must reject this
1548 combination, because we can't reload
1549 it. */
1550 if (curr_alt_offmemok[m]
1551 && MEM_P (*curr_id->operand_loc[m])
1552 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1553 continue;
1556 else
1558 /* Operands don't match. Both operands must
1559 allow a reload register, otherwise we
1560 cannot make them match. */
1561 if (curr_alt[m] == NO_REGS)
1562 break;
1563 /* Retroactively mark the operand we had to
1564 match as a loser, if it wasn't already and
1565 it wasn't matched to a register constraint
1566 (e.g it might be matched by memory). */
1567 if (curr_alt_win[m]
1568 && (operand_reg[m] == NULL_RTX
1569 || hard_regno[m] < 0))
1571 losers++;
1572 reload_nregs
1573 += (ira_reg_class_max_nregs[curr_alt[m]]
1574 [GET_MODE (*curr_id->operand_loc[m])]);
1577 /* We prefer no matching alternatives because
1578 it gives more freedom in RA. */
1579 if (operand_reg[nop] == NULL_RTX
1580 || (find_regno_note (curr_insn, REG_DEAD,
1581 REGNO (operand_reg[nop]))
1582 == NULL_RTX))
1583 reject += 2;
1585 /* If we have to reload this operand and some
1586 previous operand also had to match the same
1587 thing as this operand, we don't know how to do
1588 that. */
1589 if (!match_p || !curr_alt_win[m])
1591 for (i = 0; i < nop; i++)
1592 if (curr_alt_matches[i] == m)
1593 break;
1594 if (i < nop)
1595 break;
1597 else
1598 did_match = true;
1600 /* This can be fixed with reloads if the operand
1601 we are supposed to match can be fixed with
1602 reloads. */
1603 badop = false;
1604 this_alternative = curr_alt[m];
1605 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1606 winreg = this_alternative != NO_REGS;
1607 break;
1610 case 'p':
1611 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1612 ADDRESS, SCRATCH);
1613 this_alternative = reg_class_subunion[this_alternative][cl];
1614 IOR_HARD_REG_SET (this_alternative_set,
1615 reg_class_contents[cl]);
1616 if (costly_p)
1618 this_costly_alternative
1619 = reg_class_subunion[this_costly_alternative][cl];
1620 IOR_HARD_REG_SET (this_costly_alternative_set,
1621 reg_class_contents[cl]);
1623 win = true;
1624 badop = false;
1625 break;
1627 case TARGET_MEM_CONSTRAINT:
1628 if (MEM_P (op) || spilled_pseudo_p (op))
1629 win = true;
1630 /* We can put constant or pseudo value into memory
1631 to satisfy the constraint. */
1632 if (CONST_POOL_OK_P (mode, op) || REG_P (op))
1633 badop = false;
1634 constmemok = true;
1635 break;
1637 case '<':
1638 if (MEM_P (op)
1639 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1640 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1641 win = true;
1642 break;
1644 case '>':
1645 if (MEM_P (op)
1646 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1647 || GET_CODE (XEXP (op, 0)) == POST_INC))
1648 win = true;
1649 break;
1651 /* Memory op whose address is not offsettable. */
1652 case 'V':
1653 if (MEM_P (op)
1654 && ! offsettable_nonstrict_memref_p (op))
1655 win = true;
1656 break;
1658 /* Memory operand whose address is offsettable. */
1659 case 'o':
1660 if ((MEM_P (op)
1661 && offsettable_nonstrict_memref_p (op))
1662 || spilled_pseudo_p (op))
1663 win = true;
1664 /* We can put constant or pseudo value into memory
1665 or make memory address offsetable to satisfy the
1666 constraint. */
1667 if (CONST_POOL_OK_P (mode, op) || MEM_P (op) || REG_P (op))
1668 badop = false;
1669 constmemok = true;
1670 offmemok = true;
1671 break;
1673 case 'E':
1674 case 'F':
1675 if (GET_CODE (op) == CONST_DOUBLE
1676 || (GET_CODE (op) == CONST_VECTOR
1677 && (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)))
1678 win = true;
1679 break;
1681 case 'G':
1682 case 'H':
1683 if (CONST_DOUBLE_AS_FLOAT_P (op)
1684 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1685 win = true;
1686 break;
1688 case 's':
1689 if (CONST_SCALAR_INT_P (op))
1690 break;
1692 case 'i':
1693 if (general_constant_p (op))
1694 win = true;
1695 break;
1697 case 'n':
1698 if (CONST_SCALAR_INT_P (op))
1699 win = true;
1700 break;
1702 case 'I':
1703 case 'J':
1704 case 'K':
1705 case 'L':
1706 case 'M':
1707 case 'N':
1708 case 'O':
1709 case 'P':
1710 if (CONST_INT_P (op)
1711 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1712 win = true;
1713 break;
1715 case 'X':
1716 /* This constraint should be excluded by the fast
1717 track. */
1718 gcc_unreachable ();
1719 break;
1721 case 'g':
1722 if (MEM_P (op)
1723 || general_constant_p (op)
1724 || spilled_pseudo_p (op))
1725 win = true;
1726 /* Drop through into 'r' case. */
1728 case 'r':
1729 this_alternative
1730 = reg_class_subunion[this_alternative][GENERAL_REGS];
1731 IOR_HARD_REG_SET (this_alternative_set,
1732 reg_class_contents[GENERAL_REGS]);
1733 if (costly_p)
1735 this_costly_alternative
1736 = (reg_class_subunion
1737 [this_costly_alternative][GENERAL_REGS]);
1738 IOR_HARD_REG_SET (this_costly_alternative_set,
1739 reg_class_contents[GENERAL_REGS]);
1741 goto reg;
1743 default:
1744 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
1746 #ifdef EXTRA_CONSTRAINT_STR
1747 if (EXTRA_MEMORY_CONSTRAINT (c, p))
1749 if (EXTRA_CONSTRAINT_STR (op, c, p))
1750 win = true;
1751 else if (spilled_pseudo_p (op))
1752 win = true;
1754 /* If we didn't already win, we can reload
1755 constants via force_const_mem or put the
1756 pseudo value into memory, or make other
1757 memory by reloading the address like for
1758 'o'. */
1759 if (CONST_POOL_OK_P (mode, op)
1760 || MEM_P (op) || REG_P (op))
1761 badop = false;
1762 constmemok = true;
1763 offmemok = true;
1764 break;
1766 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1768 if (EXTRA_CONSTRAINT_STR (op, c, p))
1769 win = true;
1771 /* If we didn't already win, we can reload
1772 the address into a base register. */
1773 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1774 ADDRESS, SCRATCH);
1775 this_alternative
1776 = reg_class_subunion[this_alternative][cl];
1777 IOR_HARD_REG_SET (this_alternative_set,
1778 reg_class_contents[cl]);
1779 if (costly_p)
1781 this_costly_alternative
1782 = (reg_class_subunion
1783 [this_costly_alternative][cl]);
1784 IOR_HARD_REG_SET (this_costly_alternative_set,
1785 reg_class_contents[cl]);
1787 badop = false;
1788 break;
1791 if (EXTRA_CONSTRAINT_STR (op, c, p))
1792 win = true;
1793 #endif
1794 break;
1797 cl = REG_CLASS_FROM_CONSTRAINT (c, p);
1798 this_alternative = reg_class_subunion[this_alternative][cl];
1799 IOR_HARD_REG_SET (this_alternative_set,
1800 reg_class_contents[cl]);
1801 if (costly_p)
1803 this_costly_alternative
1804 = reg_class_subunion[this_costly_alternative][cl];
1805 IOR_HARD_REG_SET (this_costly_alternative_set,
1806 reg_class_contents[cl]);
1808 reg:
1809 if (mode == BLKmode)
1810 break;
1811 winreg = true;
1812 if (REG_P (op))
1814 if (hard_regno[nop] >= 0
1815 && in_hard_reg_set_p (this_alternative_set,
1816 mode, hard_regno[nop]))
1817 win = true;
1818 else if (hard_regno[nop] < 0
1819 && in_class_p (op, this_alternative, NULL))
1820 win = true;
1822 break;
1824 if (c != ' ' && c != '\t')
1825 costly_p = c == '*';
1827 while ((p += len), c);
1829 /* Record which operands fit this alternative. */
1830 if (win)
1832 this_alternative_win = true;
1833 if (operand_reg[nop] != NULL_RTX)
1835 if (hard_regno[nop] >= 0)
1837 if (in_hard_reg_set_p (this_costly_alternative_set,
1838 mode, hard_regno[nop]))
1839 reject++;
1841 else
1843 /* Prefer won reg to spilled pseudo under other equal
1844 conditions. */
1845 reject++;
1846 if (in_class_p (operand_reg[nop],
1847 this_costly_alternative, NULL))
1848 reject++;
1850 /* We simulate the behaviour of old reload here.
1851 Although scratches need hard registers and it
1852 might result in spilling other pseudos, no reload
1853 insns are generated for the scratches. So it
1854 might cost something but probably less than old
1855 reload pass believes. */
1856 if (lra_former_scratch_p (REGNO (operand_reg[nop])))
1857 reject += LRA_LOSER_COST_FACTOR;
1860 else if (did_match)
1861 this_alternative_match_win = true;
1862 else
1864 int const_to_mem = 0;
1865 bool no_regs_p;
1867 /* If this alternative asks for a specific reg class, see if there
1868 is at least one allocatable register in that class. */
1869 no_regs_p
1870 = (this_alternative == NO_REGS
1871 || (hard_reg_set_subset_p
1872 (reg_class_contents[this_alternative],
1873 lra_no_alloc_regs)));
1875 /* For asms, verify that the class for this alternative is possible
1876 for the mode that is specified. */
1877 if (!no_regs_p && REG_P (op) && INSN_CODE (curr_insn) < 0)
1879 int i;
1880 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1881 if (HARD_REGNO_MODE_OK (i, mode)
1882 && in_hard_reg_set_p (reg_class_contents[this_alternative], mode, i))
1883 break;
1884 if (i == FIRST_PSEUDO_REGISTER)
1885 winreg = false;
1888 /* If this operand accepts a register, and if the
1889 register class has at least one allocatable register,
1890 then this operand can be reloaded. */
1891 if (winreg && !no_regs_p)
1892 badop = false;
1894 if (badop)
1895 goto fail;
1897 this_alternative_offmemok = offmemok;
1898 if (this_costly_alternative != NO_REGS)
1899 reject++;
1900 /* If the operand is dying, has a matching constraint,
1901 and satisfies constraints of the matched operand
1902 which failed to satisfy the own constraints, we do
1903 not need to generate a reload insn for this
1904 operand. */
1905 if (!(this_alternative_matches >= 0
1906 && !curr_alt_win[this_alternative_matches]
1907 && REG_P (op)
1908 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
1909 && (hard_regno[nop] >= 0
1910 ? in_hard_reg_set_p (this_alternative_set,
1911 mode, hard_regno[nop])
1912 : in_class_p (op, this_alternative, NULL))))
1914 /* Strict_low_part requires to reload the register
1915 not the sub-register. In this case we should
1916 check that a final reload hard reg can hold the
1917 value mode. */
1918 if (curr_static_id->operand[nop].strict_low
1919 && REG_P (op)
1920 && hard_regno[nop] < 0
1921 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
1922 && ira_class_hard_regs_num[this_alternative] > 0
1923 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
1924 [this_alternative][0],
1925 GET_MODE (op)))
1926 goto fail;
1927 losers++;
1929 if (operand_reg[nop] != NULL_RTX
1930 /* Output operands and matched input operands are
1931 not inherited. The following conditions do not
1932 exactly describe the previous statement but they
1933 are pretty close. */
1934 && curr_static_id->operand[nop].type != OP_OUT
1935 && (this_alternative_matches < 0
1936 || curr_static_id->operand[nop].type != OP_IN))
1938 int last_reload = (lra_reg_info[ORIGINAL_REGNO
1939 (operand_reg[nop])]
1940 .last_reload);
1942 if (last_reload > bb_reload_num)
1943 reload_sum += last_reload - bb_reload_num;
1945 /* If this is a constant that is reloaded into the
1946 desired class by copying it to memory first, count
1947 that as another reload. This is consistent with
1948 other code and is required to avoid choosing another
1949 alternative when the constant is moved into memory.
1950 Note that the test here is precisely the same as in
1951 the code below that calls force_const_mem. */
1952 if (CONST_POOL_OK_P (mode, op)
1953 && ((targetm.preferred_reload_class
1954 (op, this_alternative) == NO_REGS)
1955 || no_input_reloads_p))
1957 const_to_mem = 1;
1958 if (! no_regs_p)
1959 losers++;
1962 /* Alternative loses if it requires a type of reload not
1963 permitted for this insn. We can always reload
1964 objects with a REG_UNUSED note. */
1965 if ((curr_static_id->operand[nop].type != OP_IN
1966 && no_output_reloads_p
1967 && ! find_reg_note (curr_insn, REG_UNUSED, op))
1968 || (curr_static_id->operand[nop].type != OP_OUT
1969 && no_input_reloads_p && ! const_to_mem))
1970 goto fail;
1972 /* Check strong discouragement of reload of non-constant
1973 into class THIS_ALTERNATIVE. */
1974 if (! CONSTANT_P (op) && ! no_regs_p
1975 && (targetm.preferred_reload_class
1976 (op, this_alternative) == NO_REGS
1977 || (curr_static_id->operand[nop].type == OP_OUT
1978 && (targetm.preferred_output_reload_class
1979 (op, this_alternative) == NO_REGS))))
1980 reject += LRA_MAX_REJECT;
1982 if (MEM_P (op) && offmemok)
1984 /* If we know offset and this non-offsetable memory,
1985 something wrong with this memory and it is better
1986 to try other memory possibilities. */
1987 if (MEM_OFFSET_KNOWN_P (op))
1988 reject += LRA_MAX_REJECT;
1990 else if (! (const_to_mem && constmemok))
1992 /* We prefer to reload pseudos over reloading other
1993 things, since such reloads may be able to be
1994 eliminated later. So bump REJECT in other cases.
1995 Don't do this in the case where we are forcing a
1996 constant into memory and it will then win since
1997 we don't want to have a different alternative
1998 match then. */
1999 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2000 reject += 2;
2002 if (! no_regs_p)
2003 reload_nregs
2004 += ira_reg_class_max_nregs[this_alternative][mode];
2006 if (SMALL_REGISTER_CLASS_P (this_alternative))
2007 reject += LRA_LOSER_COST_FACTOR / 2;
2010 /* We are trying to spill pseudo into memory. It is
2011 usually more costly than moving to a hard register
2012 although it might takes the same number of
2013 reloads. */
2014 if (no_regs_p && REG_P (op))
2015 reject += 2;
2017 #ifdef SECONDARY_MEMORY_NEEDED
2018 /* If reload requires moving value through secondary
2019 memory, it will need one more insn at least. */
2020 if (this_alternative != NO_REGS
2021 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2022 && ((curr_static_id->operand[nop].type != OP_OUT
2023 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2024 GET_MODE (op)))
2025 || (curr_static_id->operand[nop].type != OP_IN
2026 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2027 GET_MODE (op)))))
2028 losers++;
2029 #endif
2030 /* Input reloads can be inherited more often than output
2031 reloads can be removed, so penalize output
2032 reloads. */
2033 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2034 reject++;
2038 if (early_clobber_p)
2039 reject++;
2040 /* ??? We check early clobbers after processing all operands
2041 (see loop below) and there we update the costs more.
2042 Should we update the cost (may be approximately) here
2043 because of early clobber register reloads or it is a rare
2044 or non-important thing to be worth to do it. */
2045 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2046 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2048 if (lra_dump_file != NULL)
2049 fprintf (lra_dump_file,
2050 " alt=%d,overall=%d,losers=%d -- reject\n",
2051 nalt, overall, losers);
2052 goto fail;
2055 curr_alt[nop] = this_alternative;
2056 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2057 curr_alt_win[nop] = this_alternative_win;
2058 curr_alt_match_win[nop] = this_alternative_match_win;
2059 curr_alt_offmemok[nop] = this_alternative_offmemok;
2060 curr_alt_matches[nop] = this_alternative_matches;
2062 if (this_alternative_matches >= 0
2063 && !did_match && !this_alternative_win)
2064 curr_alt_win[this_alternative_matches] = false;
2066 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2067 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2069 if (curr_insn_set != NULL_RTX && n_operands == 2
2070 /* Prevent processing non-move insns. */
2071 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2072 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2073 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2074 && REG_P (no_subreg_reg_operand[0])
2075 && REG_P (no_subreg_reg_operand[1])
2076 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2077 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2078 || (! curr_alt_win[0] && curr_alt_win[1]
2079 && REG_P (no_subreg_reg_operand[1])
2080 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2081 || (curr_alt_win[0] && ! curr_alt_win[1]
2082 && REG_P (no_subreg_reg_operand[0])
2083 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2084 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2085 no_subreg_reg_operand[1])
2086 || (targetm.preferred_reload_class
2087 (no_subreg_reg_operand[1],
2088 (enum reg_class) curr_alt[1]) != NO_REGS))
2089 /* If it is a result of recent elimination in move
2090 insn we can transform it into an add still by
2091 using this alternative. */
2092 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2093 /* We have a move insn and a new reload insn will be similar
2094 to the current insn. We should avoid such situation as it
2095 results in LRA cycling. */
2096 overall += LRA_MAX_REJECT;
2097 ok_p = true;
2098 curr_alt_dont_inherit_ops_num = 0;
2099 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2101 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2102 HARD_REG_SET temp_set;
2104 i = early_clobbered_nops[nop];
2105 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2106 || hard_regno[i] < 0)
2107 continue;
2108 lra_assert (operand_reg[i] != NULL_RTX);
2109 clobbered_hard_regno = hard_regno[i];
2110 CLEAR_HARD_REG_SET (temp_set);
2111 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2112 first_conflict_j = last_conflict_j = -1;
2113 for (j = 0; j < n_operands; j++)
2114 if (j == i
2115 /* We don't want process insides of match_operator and
2116 match_parallel because otherwise we would process
2117 their operands once again generating a wrong
2118 code. */
2119 || curr_static_id->operand[j].is_operator)
2120 continue;
2121 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2122 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2123 continue;
2124 /* If we don't reload j-th operand, check conflicts. */
2125 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2126 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2128 if (first_conflict_j < 0)
2129 first_conflict_j = j;
2130 last_conflict_j = j;
2132 if (last_conflict_j < 0)
2133 continue;
2134 /* If earlyclobber operand conflicts with another
2135 non-matching operand which is actually the same register
2136 as the earlyclobber operand, it is better to reload the
2137 another operand as an operand matching the earlyclobber
2138 operand can be also the same. */
2139 if (first_conflict_j == last_conflict_j
2140 && operand_reg[last_conflict_j]
2141 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2142 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2144 curr_alt_win[last_conflict_j] = false;
2145 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2146 = last_conflict_j;
2147 losers++;
2148 /* Early clobber was already reflected in REJECT. */
2149 lra_assert (reject > 0);
2150 reject--;
2151 overall += LRA_LOSER_COST_FACTOR - 1;
2153 else
2155 /* We need to reload early clobbered register and the
2156 matched registers. */
2157 for (j = 0; j < n_operands; j++)
2158 if (curr_alt_matches[j] == i)
2160 curr_alt_match_win[j] = false;
2161 losers++;
2162 overall += LRA_LOSER_COST_FACTOR;
2164 if (! curr_alt_match_win[i])
2165 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2166 else
2168 /* Remember pseudos used for match reloads are never
2169 inherited. */
2170 lra_assert (curr_alt_matches[i] >= 0);
2171 curr_alt_win[curr_alt_matches[i]] = false;
2173 curr_alt_win[i] = curr_alt_match_win[i] = false;
2174 losers++;
2175 /* Early clobber was already reflected in REJECT. */
2176 lra_assert (reject > 0);
2177 reject--;
2178 overall += LRA_LOSER_COST_FACTOR - 1;
2181 if (lra_dump_file != NULL)
2182 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2183 nalt, overall, losers, reload_nregs);
2185 /* If this alternative can be made to work by reloading, and it
2186 needs less reloading than the others checked so far, record
2187 it as the chosen goal for reloading. */
2188 if ((best_losers != 0 && losers == 0)
2189 || (((best_losers == 0 && losers == 0)
2190 || (best_losers != 0 && losers != 0))
2191 && (best_overall > overall
2192 || (best_overall == overall
2193 /* If the cost of the reloads is the same,
2194 prefer alternative which requires minimal
2195 number of reload regs. */
2196 && (reload_nregs < best_reload_nregs
2197 || (reload_nregs == best_reload_nregs
2198 && (best_reload_sum < reload_sum
2199 || (best_reload_sum == reload_sum
2200 && nalt < goal_alt_number))))))))
2202 for (nop = 0; nop < n_operands; nop++)
2204 goal_alt_win[nop] = curr_alt_win[nop];
2205 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2206 goal_alt_matches[nop] = curr_alt_matches[nop];
2207 goal_alt[nop] = curr_alt[nop];
2208 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2210 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2211 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2212 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2213 goal_alt_swapped = curr_swapped;
2214 best_overall = overall;
2215 best_losers = losers;
2216 best_reload_nregs = reload_nregs;
2217 best_reload_sum = reload_sum;
2218 goal_alt_number = nalt;
2220 if (losers == 0)
2221 /* Everything is satisfied. Do not process alternatives
2222 anymore. */
2223 break;
2224 fail:
2227 return ok_p;
2230 /* Return 1 if ADDR is a valid memory address for mode MODE in address
2231 space AS, and check that each pseudo has the proper kind of hard
2232 reg. */
2233 static int
2234 valid_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2235 rtx addr, addr_space_t as)
2237 #ifdef GO_IF_LEGITIMATE_ADDRESS
2238 lra_assert (ADDR_SPACE_GENERIC_P (as));
2239 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
2240 return 0;
2242 win:
2243 return 1;
2244 #else
2245 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
2246 #endif
2249 /* Return whether address AD is valid. */
2251 static bool
2252 valid_address_p (struct address_info *ad)
2254 /* Some ports do not check displacements for eliminable registers,
2255 so we replace them temporarily with the elimination target. */
2256 rtx saved_base_reg = NULL_RTX;
2257 rtx saved_index_reg = NULL_RTX;
2258 rtx *base_term = strip_subreg (ad->base_term);
2259 rtx *index_term = strip_subreg (ad->index_term);
2260 if (base_term != NULL)
2262 saved_base_reg = *base_term;
2263 lra_eliminate_reg_if_possible (base_term);
2264 if (ad->base_term2 != NULL)
2265 *ad->base_term2 = *ad->base_term;
2267 if (index_term != NULL)
2269 saved_index_reg = *index_term;
2270 lra_eliminate_reg_if_possible (index_term);
2272 bool ok_p = valid_address_p (ad->mode, *ad->outer, ad->as);
2273 if (saved_base_reg != NULL_RTX)
2275 *base_term = saved_base_reg;
2276 if (ad->base_term2 != NULL)
2277 *ad->base_term2 = *ad->base_term;
2279 if (saved_index_reg != NULL_RTX)
2280 *index_term = saved_index_reg;
2281 return ok_p;
2284 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2285 static rtx
2286 base_plus_disp_to_reg (struct address_info *ad)
2288 enum reg_class cl;
2289 rtx new_reg;
2291 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2292 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2293 get_index_code (ad));
2294 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2295 cl, "base + disp");
2296 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2297 return new_reg;
2300 /* Return true if we can add a displacement to address AD, even if that
2301 makes the address invalid. The fix-up code requires any new address
2302 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2303 static bool
2304 can_add_disp_p (struct address_info *ad)
2306 return (!ad->autoinc_p
2307 && ad->segment == NULL
2308 && ad->base == ad->base_term
2309 && ad->disp == ad->disp_term);
2312 /* Make equiv substitution in address AD. Return true if a substitution
2313 was made. */
2314 static bool
2315 equiv_address_substitution (struct address_info *ad)
2317 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2318 HOST_WIDE_INT disp, scale;
2319 bool change_p;
2321 base_term = strip_subreg (ad->base_term);
2322 if (base_term == NULL)
2323 base_reg = new_base_reg = NULL_RTX;
2324 else
2326 base_reg = *base_term;
2327 new_base_reg = get_equiv_substitution (base_reg);
2329 index_term = strip_subreg (ad->index_term);
2330 if (index_term == NULL)
2331 index_reg = new_index_reg = NULL_RTX;
2332 else
2334 index_reg = *index_term;
2335 new_index_reg = get_equiv_substitution (index_reg);
2337 if (base_reg == new_base_reg && index_reg == new_index_reg)
2338 return false;
2339 disp = 0;
2340 change_p = false;
2341 if (lra_dump_file != NULL)
2343 fprintf (lra_dump_file, "Changing address in insn %d ",
2344 INSN_UID (curr_insn));
2345 dump_value_slim (lra_dump_file, *ad->outer, 1);
2347 if (base_reg != new_base_reg)
2349 if (REG_P (new_base_reg))
2351 *base_term = new_base_reg;
2352 change_p = true;
2354 else if (GET_CODE (new_base_reg) == PLUS
2355 && REG_P (XEXP (new_base_reg, 0))
2356 && CONST_INT_P (XEXP (new_base_reg, 1))
2357 && can_add_disp_p (ad))
2359 disp += INTVAL (XEXP (new_base_reg, 1));
2360 *base_term = XEXP (new_base_reg, 0);
2361 change_p = true;
2363 if (ad->base_term2 != NULL)
2364 *ad->base_term2 = *ad->base_term;
2366 if (index_reg != new_index_reg)
2368 if (REG_P (new_index_reg))
2370 *index_term = new_index_reg;
2371 change_p = true;
2373 else if (GET_CODE (new_index_reg) == PLUS
2374 && REG_P (XEXP (new_index_reg, 0))
2375 && CONST_INT_P (XEXP (new_index_reg, 1))
2376 && can_add_disp_p (ad)
2377 && (scale = get_index_scale (ad)))
2379 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2380 *index_term = XEXP (new_index_reg, 0);
2381 change_p = true;
2384 if (disp != 0)
2386 if (ad->disp != NULL)
2387 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2388 else
2390 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2391 update_address (ad);
2393 change_p = true;
2395 if (lra_dump_file != NULL)
2397 if (! change_p)
2398 fprintf (lra_dump_file, " -- no change\n");
2399 else
2401 fprintf (lra_dump_file, " on equiv ");
2402 dump_value_slim (lra_dump_file, *ad->outer, 1);
2403 fprintf (lra_dump_file, "\n");
2406 return change_p;
2409 /* Major function to make reloads for an address in operand NOP.
2410 The supported cases are:
2412 1) an address that existed before LRA started, at which point it
2413 must have been valid. These addresses are subject to elimination
2414 and may have become invalid due to the elimination offset being out
2415 of range.
2417 2) an address created by forcing a constant to memory
2418 (force_const_to_mem). The initial form of these addresses might
2419 not be valid, and it is this function's job to make them valid.
2421 3) a frame address formed from a register and a (possibly zero)
2422 constant offset. As above, these addresses might not be valid and
2423 this function must make them so.
2425 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2426 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2427 address. Return true for any RTL change. */
2428 static bool
2429 process_address (int nop, rtx *before, rtx *after)
2431 struct address_info ad;
2432 rtx new_reg;
2433 rtx op = *curr_id->operand_loc[nop];
2434 const char *constraint = curr_static_id->operand[nop].constraint;
2435 bool change_p;
2437 if (constraint[0] == 'p'
2438 || EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint))
2439 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2440 else if (MEM_P (op))
2441 decompose_mem_address (&ad, op);
2442 else if (GET_CODE (op) == SUBREG
2443 && MEM_P (SUBREG_REG (op)))
2444 decompose_mem_address (&ad, SUBREG_REG (op));
2445 else
2446 return false;
2447 change_p = equiv_address_substitution (&ad);
2448 if (ad.base_term != NULL
2449 && (process_addr_reg
2450 (ad.base_term, before,
2451 (ad.autoinc_p
2452 && !(REG_P (*ad.base_term)
2453 && find_regno_note (curr_insn, REG_DEAD,
2454 REGNO (*ad.base_term)) != NULL_RTX)
2455 ? after : NULL),
2456 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2457 get_index_code (&ad)))))
2459 change_p = true;
2460 if (ad.base_term2 != NULL)
2461 *ad.base_term2 = *ad.base_term;
2463 if (ad.index_term != NULL
2464 && process_addr_reg (ad.index_term, before, NULL, INDEX_REG_CLASS))
2465 change_p = true;
2467 #ifdef EXTRA_CONSTRAINT_STR
2468 /* Target hooks sometimes reject extra constraint addresses -- use
2469 EXTRA_CONSTRAINT_STR for the validation. */
2470 if (constraint[0] != 'p'
2471 && EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint)
2472 && EXTRA_CONSTRAINT_STR (op, constraint[0], constraint))
2473 return change_p;
2474 #endif
2476 /* There are three cases where the shape of *AD.INNER may now be invalid:
2478 1) the original address was valid, but either elimination or
2479 equiv_address_substitution was applied and that made
2480 the address invalid.
2482 2) the address is an invalid symbolic address created by
2483 force_const_to_mem.
2485 3) the address is a frame address with an invalid offset.
2487 All these cases involve a non-autoinc address, so there is no
2488 point revalidating other types. */
2489 if (ad.autoinc_p || valid_address_p (&ad))
2490 return change_p;
2492 /* Any index existed before LRA started, so we can assume that the
2493 presence and shape of the index is valid. */
2494 push_to_sequence (*before);
2495 lra_assert (ad.disp == ad.disp_term);
2496 if (ad.base == NULL)
2498 if (ad.index == NULL)
2500 int code = -1;
2501 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2502 SCRATCH, SCRATCH);
2503 rtx addr = *ad.inner;
2505 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2506 #ifdef HAVE_lo_sum
2508 rtx insn;
2509 rtx last = get_last_insn ();
2511 /* addr => lo_sum (new_base, addr), case (2) above. */
2512 insn = emit_insn (gen_rtx_SET
2513 (VOIDmode, new_reg,
2514 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2515 code = recog_memoized (insn);
2516 if (code >= 0)
2518 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2519 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2521 /* Try to put lo_sum into register. */
2522 insn = emit_insn (gen_rtx_SET
2523 (VOIDmode, new_reg,
2524 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2525 code = recog_memoized (insn);
2526 if (code >= 0)
2528 *ad.inner = new_reg;
2529 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2531 *ad.inner = addr;
2532 code = -1;
2538 if (code < 0)
2539 delete_insns_since (last);
2541 #endif
2542 if (code < 0)
2544 /* addr => new_base, case (2) above. */
2545 lra_emit_move (new_reg, addr);
2546 *ad.inner = new_reg;
2549 else
2551 /* index * scale + disp => new base + index * scale,
2552 case (1) above. */
2553 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
2554 GET_CODE (*ad.index));
2556 lra_assert (INDEX_REG_CLASS != NO_REGS);
2557 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2558 lra_emit_move (new_reg, *ad.disp);
2559 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2560 new_reg, *ad.index);
2563 else if (ad.index == NULL)
2565 int regno;
2566 enum reg_class cl;
2567 rtx set, insns, last_insn;
2568 /* base + disp => new base, cases (1) and (3) above. */
2569 /* Another option would be to reload the displacement into an
2570 index register. However, postreload has code to optimize
2571 address reloads that have the same base and different
2572 displacements, so reloading into an index register would
2573 not necessarily be a win. */
2574 start_sequence ();
2575 new_reg = base_plus_disp_to_reg (&ad);
2576 insns = get_insns ();
2577 last_insn = get_last_insn ();
2578 /* If we generated at least two insns, try last insn source as
2579 an address. If we succeed, we generate one less insn. */
2580 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
2581 && GET_CODE (SET_SRC (set)) == PLUS
2582 && REG_P (XEXP (SET_SRC (set), 0))
2583 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
2585 *ad.inner = SET_SRC (set);
2586 if (valid_address_p (ad.mode, *ad.outer, ad.as))
2588 *ad.base_term = XEXP (SET_SRC (set), 0);
2589 *ad.disp_term = XEXP (SET_SRC (set), 1);
2590 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2591 get_index_code (&ad));
2592 regno = REGNO (*ad.base_term);
2593 if (regno >= FIRST_PSEUDO_REGISTER
2594 && cl != lra_get_allocno_class (regno))
2595 change_class (regno, cl, " Change", true);
2596 new_reg = SET_SRC (set);
2597 delete_insns_since (PREV_INSN (last_insn));
2600 end_sequence ();
2601 emit_insn (insns);
2602 *ad.inner = new_reg;
2604 else
2606 /* base + scale * index + disp => new base + scale * index,
2607 case (1) above. */
2608 new_reg = base_plus_disp_to_reg (&ad);
2609 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2610 new_reg, *ad.index);
2612 *before = get_insns ();
2613 end_sequence ();
2614 return true;
2617 /* Emit insns to reload VALUE into a new register. VALUE is an
2618 auto-increment or auto-decrement RTX whose operand is a register or
2619 memory location; so reloading involves incrementing that location.
2620 IN is either identical to VALUE, or some cheaper place to reload
2621 value being incremented/decremented from.
2623 INC_AMOUNT is the number to increment or decrement by (always
2624 positive and ignored for POST_MODIFY/PRE_MODIFY).
2626 Return pseudo containing the result. */
2627 static rtx
2628 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
2630 /* REG or MEM to be copied and incremented. */
2631 rtx incloc = XEXP (value, 0);
2632 /* Nonzero if increment after copying. */
2633 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
2634 || GET_CODE (value) == POST_MODIFY);
2635 rtx last;
2636 rtx inc;
2637 rtx add_insn;
2638 int code;
2639 rtx real_in = in == value ? incloc : in;
2640 rtx result;
2641 bool plus_p = true;
2643 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
2645 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
2646 || GET_CODE (XEXP (value, 1)) == MINUS);
2647 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
2648 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
2649 inc = XEXP (XEXP (value, 1), 1);
2651 else
2653 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
2654 inc_amount = -inc_amount;
2656 inc = GEN_INT (inc_amount);
2659 if (! post && REG_P (incloc))
2660 result = incloc;
2661 else
2662 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
2663 "INC/DEC result");
2665 if (real_in != result)
2667 /* First copy the location to the result register. */
2668 lra_assert (REG_P (result));
2669 emit_insn (gen_move_insn (result, real_in));
2672 /* We suppose that there are insns to add/sub with the constant
2673 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
2674 old reload worked with this assumption. If the assumption
2675 becomes wrong, we should use approach in function
2676 base_plus_disp_to_reg. */
2677 if (in == value)
2679 /* See if we can directly increment INCLOC. */
2680 last = get_last_insn ();
2681 add_insn = emit_insn (plus_p
2682 ? gen_add2_insn (incloc, inc)
2683 : gen_sub2_insn (incloc, inc));
2685 code = recog_memoized (add_insn);
2686 if (code >= 0)
2688 if (! post && result != incloc)
2689 emit_insn (gen_move_insn (result, incloc));
2690 return result;
2692 delete_insns_since (last);
2695 /* If couldn't do the increment directly, must increment in RESULT.
2696 The way we do this depends on whether this is pre- or
2697 post-increment. For pre-increment, copy INCLOC to the reload
2698 register, increment it there, then save back. */
2699 if (! post)
2701 if (real_in != result)
2702 emit_insn (gen_move_insn (result, real_in));
2703 if (plus_p)
2704 emit_insn (gen_add2_insn (result, inc));
2705 else
2706 emit_insn (gen_sub2_insn (result, inc));
2707 if (result != incloc)
2708 emit_insn (gen_move_insn (incloc, result));
2710 else
2712 /* Post-increment.
2714 Because this might be a jump insn or a compare, and because
2715 RESULT may not be available after the insn in an input
2716 reload, we must do the incrementing before the insn being
2717 reloaded for.
2719 We have already copied IN to RESULT. Increment the copy in
2720 RESULT, save that back, then decrement RESULT so it has
2721 the original value. */
2722 if (plus_p)
2723 emit_insn (gen_add2_insn (result, inc));
2724 else
2725 emit_insn (gen_sub2_insn (result, inc));
2726 emit_insn (gen_move_insn (incloc, result));
2727 /* Restore non-modified value for the result. We prefer this
2728 way because it does not require an additional hard
2729 register. */
2730 if (plus_p)
2732 if (CONST_INT_P (inc))
2733 emit_insn (gen_add2_insn (result, GEN_INT (-INTVAL (inc))));
2734 else
2735 emit_insn (gen_sub2_insn (result, inc));
2737 else
2738 emit_insn (gen_add2_insn (result, inc));
2740 return result;
2743 /* Return true if the current move insn does not need processing as we
2744 already know that it satisfies its constraints. */
2745 static bool
2746 simple_move_p (void)
2748 rtx dest, src;
2749 enum reg_class dclass, sclass;
2751 lra_assert (curr_insn_set != NULL_RTX);
2752 dest = SET_DEST (curr_insn_set);
2753 src = SET_SRC (curr_insn_set);
2754 return ((dclass = get_op_class (dest)) != NO_REGS
2755 && (sclass = get_op_class (src)) != NO_REGS
2756 /* The backend guarantees that register moves of cost 2
2757 never need reloads. */
2758 && targetm.register_move_cost (GET_MODE (src), dclass, sclass) == 2);
2761 /* Swap operands NOP and NOP + 1. */
2762 static inline void
2763 swap_operands (int nop)
2765 enum machine_mode mode = curr_operand_mode[nop];
2766 curr_operand_mode[nop] = curr_operand_mode[nop + 1];
2767 curr_operand_mode[nop + 1] = mode;
2768 rtx x = *curr_id->operand_loc[nop];
2769 *curr_id->operand_loc[nop] = *curr_id->operand_loc[nop + 1];
2770 *curr_id->operand_loc[nop + 1] = x;
2771 /* Swap the duplicates too. */
2772 lra_update_dup (curr_id, nop);
2773 lra_update_dup (curr_id, nop + 1);
2776 /* Main entry point of the constraint code: search the body of the
2777 current insn to choose the best alternative. It is mimicking insn
2778 alternative cost calculation model of former reload pass. That is
2779 because machine descriptions were written to use this model. This
2780 model can be changed in future. Make commutative operand exchange
2781 if it is chosen.
2783 Return true if some RTL changes happened during function call. */
2784 static bool
2785 curr_insn_transform (void)
2787 int i, j, k;
2788 int n_operands;
2789 int n_alternatives;
2790 int commutative;
2791 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
2792 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
2793 rtx before, after;
2794 bool alt_p = false;
2795 /* Flag that the insn has been changed through a transformation. */
2796 bool change_p;
2797 bool sec_mem_p;
2798 #ifdef SECONDARY_MEMORY_NEEDED
2799 bool use_sec_mem_p;
2800 #endif
2801 int max_regno_before;
2802 int reused_alternative_num;
2804 curr_insn_set = single_set (curr_insn);
2805 if (curr_insn_set != NULL_RTX && simple_move_p ())
2806 return false;
2808 no_input_reloads_p = no_output_reloads_p = false;
2809 goal_alt_number = -1;
2810 change_p = sec_mem_p = false;
2811 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
2812 reloads; neither are insns that SET cc0. Insns that use CC0 are
2813 not allowed to have any input reloads. */
2814 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
2815 no_output_reloads_p = true;
2817 #ifdef HAVE_cc0
2818 if (reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
2819 no_input_reloads_p = true;
2820 if (reg_set_p (cc0_rtx, PATTERN (curr_insn)))
2821 no_output_reloads_p = true;
2822 #endif
2824 n_operands = curr_static_id->n_operands;
2825 n_alternatives = curr_static_id->n_alternatives;
2827 /* Just return "no reloads" if insn has no operands with
2828 constraints. */
2829 if (n_operands == 0 || n_alternatives == 0)
2830 return false;
2832 max_regno_before = max_reg_num ();
2834 for (i = 0; i < n_operands; i++)
2836 goal_alt_matched[i][0] = -1;
2837 goal_alt_matches[i] = -1;
2840 commutative = curr_static_id->commutative;
2842 /* Now see what we need for pseudos that didn't get hard regs or got
2843 the wrong kind of hard reg. For this, we must consider all the
2844 operands together against the register constraints. */
2846 best_losers = best_overall = INT_MAX;
2847 best_reload_sum = 0;
2849 curr_swapped = false;
2850 goal_alt_swapped = false;
2852 /* Make equivalence substitution and memory subreg elimination
2853 before address processing because an address legitimacy can
2854 depend on memory mode. */
2855 for (i = 0; i < n_operands; i++)
2857 rtx op = *curr_id->operand_loc[i];
2858 rtx subst, old = op;
2859 bool op_change_p = false;
2861 if (GET_CODE (old) == SUBREG)
2862 old = SUBREG_REG (old);
2863 subst = get_equiv_substitution (old);
2864 if (subst != old)
2866 subst = copy_rtx (subst);
2867 lra_assert (REG_P (old));
2868 if (GET_CODE (op) == SUBREG)
2869 SUBREG_REG (op) = subst;
2870 else
2871 *curr_id->operand_loc[i] = subst;
2872 if (lra_dump_file != NULL)
2874 fprintf (lra_dump_file,
2875 "Changing pseudo %d in operand %i of insn %u on equiv ",
2876 REGNO (old), i, INSN_UID (curr_insn));
2877 dump_value_slim (lra_dump_file, subst, 1);
2878 fprintf (lra_dump_file, "\n");
2880 op_change_p = change_p = true;
2882 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
2884 change_p = true;
2885 lra_update_dup (curr_id, i);
2889 /* Reload address registers and displacements. We do it before
2890 finding an alternative because of memory constraints. */
2891 before = after = NULL_RTX;
2892 for (i = 0; i < n_operands; i++)
2893 if (! curr_static_id->operand[i].is_operator
2894 && process_address (i, &before, &after))
2896 change_p = true;
2897 lra_update_dup (curr_id, i);
2900 if (change_p)
2901 /* If we've changed the instruction then any alternative that
2902 we chose previously may no longer be valid. */
2903 lra_set_used_insn_alternative (curr_insn, -1);
2905 if (curr_insn_set != NULL_RTX
2906 && check_and_process_move (&change_p, &sec_mem_p))
2907 return change_p;
2909 try_swapped:
2911 reused_alternative_num = curr_id->used_insn_alternative;
2912 if (lra_dump_file != NULL && reused_alternative_num >= 0)
2913 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
2914 reused_alternative_num, INSN_UID (curr_insn));
2916 if (process_alt_operands (reused_alternative_num))
2917 alt_p = true;
2919 /* If insn is commutative (it's safe to exchange a certain pair of
2920 operands) then we need to try each alternative twice, the second
2921 time matching those two operands as if we had exchanged them. To
2922 do this, really exchange them in operands.
2924 If we have just tried the alternatives the second time, return
2925 operands to normal and drop through. */
2927 if (reused_alternative_num < 0 && commutative >= 0)
2929 curr_swapped = !curr_swapped;
2930 if (curr_swapped)
2932 swap_operands (commutative);
2933 goto try_swapped;
2935 else
2936 swap_operands (commutative);
2939 if (! alt_p && ! sec_mem_p)
2941 /* No alternative works with reloads?? */
2942 if (INSN_CODE (curr_insn) >= 0)
2943 fatal_insn ("unable to generate reloads for:", curr_insn);
2944 error_for_asm (curr_insn,
2945 "inconsistent operand constraints in an %<asm%>");
2946 /* Avoid further trouble with this insn. */
2947 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
2948 lra_invalidate_insn_data (curr_insn);
2949 return true;
2952 /* If the best alternative is with operands 1 and 2 swapped, swap
2953 them. Update the operand numbers of any reloads already
2954 pushed. */
2956 if (goal_alt_swapped)
2958 if (lra_dump_file != NULL)
2959 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
2960 INSN_UID (curr_insn));
2962 /* Swap the duplicates too. */
2963 swap_operands (commutative);
2964 change_p = true;
2967 #ifdef SECONDARY_MEMORY_NEEDED
2968 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
2969 too conservatively. So we use the secondary memory only if there
2970 is no any alternative without reloads. */
2971 use_sec_mem_p = false;
2972 if (! alt_p)
2973 use_sec_mem_p = true;
2974 else if (sec_mem_p)
2976 for (i = 0; i < n_operands; i++)
2977 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
2978 break;
2979 use_sec_mem_p = i < n_operands;
2982 if (use_sec_mem_p)
2984 rtx new_reg, src, dest, rld;
2985 enum machine_mode sec_mode, rld_mode;
2987 lra_assert (sec_mem_p);
2988 lra_assert (curr_static_id->operand[0].type == OP_OUT
2989 && curr_static_id->operand[1].type == OP_IN);
2990 dest = *curr_id->operand_loc[0];
2991 src = *curr_id->operand_loc[1];
2992 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
2993 ? dest : src);
2994 rld_mode = GET_MODE (rld);
2995 #ifdef SECONDARY_MEMORY_NEEDED_MODE
2996 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
2997 #else
2998 sec_mode = rld_mode;
2999 #endif
3000 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3001 NO_REGS, "secondary");
3002 /* If the mode is changed, it should be wider. */
3003 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3004 if (sec_mode != rld_mode)
3006 /* If the target says specifically to use another mode for
3007 secondary memory moves we can not reuse the original
3008 insn. */
3009 after = emit_spill_move (false, new_reg, dest);
3010 lra_process_new_insns (curr_insn, NULL_RTX, after,
3011 "Inserting the sec. move");
3012 before = emit_spill_move (true, new_reg, src);
3013 lra_process_new_insns (curr_insn, before, NULL_RTX, "Changing on");
3014 lra_set_insn_deleted (curr_insn);
3016 else if (dest == rld)
3018 *curr_id->operand_loc[0] = new_reg;
3019 after = emit_spill_move (false, new_reg, dest);
3020 lra_process_new_insns (curr_insn, NULL_RTX, after,
3021 "Inserting the sec. move");
3023 else
3025 *curr_id->operand_loc[1] = new_reg;
3026 before = emit_spill_move (true, new_reg, src);
3027 lra_process_new_insns (curr_insn, before, NULL_RTX,
3028 "Inserting the sec. move");
3030 lra_update_insn_regno_info (curr_insn);
3031 return true;
3033 #endif
3035 lra_assert (goal_alt_number >= 0);
3036 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3038 if (lra_dump_file != NULL)
3040 const char *p;
3042 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3043 goal_alt_number, INSN_UID (curr_insn));
3044 for (i = 0; i < n_operands; i++)
3046 p = (curr_static_id->operand_alternative
3047 [goal_alt_number * n_operands + i].constraint);
3048 if (*p == '\0')
3049 continue;
3050 fprintf (lra_dump_file, " (%d) ", i);
3051 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3052 fputc (*p, lra_dump_file);
3054 if (INSN_CODE (curr_insn) >= 0
3055 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3056 fprintf (lra_dump_file, " {%s}", p);
3057 fprintf (lra_dump_file, "\n");
3060 /* Right now, for any pair of operands I and J that are required to
3061 match, with J < I, goal_alt_matches[I] is J. Add I to
3062 goal_alt_matched[J]. */
3064 for (i = 0; i < n_operands; i++)
3065 if ((j = goal_alt_matches[i]) >= 0)
3067 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3069 /* We allow matching one output operand and several input
3070 operands. */
3071 lra_assert (k == 0
3072 || (curr_static_id->operand[j].type == OP_OUT
3073 && curr_static_id->operand[i].type == OP_IN
3074 && (curr_static_id->operand
3075 [goal_alt_matched[j][0]].type == OP_IN)));
3076 goal_alt_matched[j][k] = i;
3077 goal_alt_matched[j][k + 1] = -1;
3080 for (i = 0; i < n_operands; i++)
3081 goal_alt_win[i] |= goal_alt_match_win[i];
3083 /* Any constants that aren't allowed and can't be reloaded into
3084 registers are here changed into memory references. */
3085 for (i = 0; i < n_operands; i++)
3086 if (goal_alt_win[i])
3088 int regno;
3089 enum reg_class new_class;
3090 rtx reg = *curr_id->operand_loc[i];
3092 if (GET_CODE (reg) == SUBREG)
3093 reg = SUBREG_REG (reg);
3095 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3097 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3099 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3101 lra_assert (ok_p);
3102 change_class (regno, new_class, " Change", true);
3106 else
3108 const char *constraint;
3109 char c;
3110 rtx op = *curr_id->operand_loc[i];
3111 rtx subreg = NULL_RTX;
3112 enum machine_mode mode = curr_operand_mode[i];
3114 if (GET_CODE (op) == SUBREG)
3116 subreg = op;
3117 op = SUBREG_REG (op);
3118 mode = GET_MODE (op);
3121 if (CONST_POOL_OK_P (mode, op)
3122 && ((targetm.preferred_reload_class
3123 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3124 || no_input_reloads_p))
3126 rtx tem = force_const_mem (mode, op);
3128 change_p = true;
3129 if (subreg != NULL_RTX)
3130 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3132 *curr_id->operand_loc[i] = tem;
3133 lra_update_dup (curr_id, i);
3134 process_address (i, &before, &after);
3136 /* If the alternative accepts constant pool refs directly
3137 there will be no reload needed at all. */
3138 if (subreg != NULL_RTX)
3139 continue;
3140 /* Skip alternatives before the one requested. */
3141 constraint = (curr_static_id->operand_alternative
3142 [goal_alt_number * n_operands + i].constraint);
3143 for (;
3144 (c = *constraint) && c != ',' && c != '#';
3145 constraint += CONSTRAINT_LEN (c, constraint))
3147 if (c == TARGET_MEM_CONSTRAINT || c == 'o')
3148 break;
3149 #ifdef EXTRA_CONSTRAINT_STR
3150 if (EXTRA_MEMORY_CONSTRAINT (c, constraint)
3151 && EXTRA_CONSTRAINT_STR (tem, c, constraint))
3152 break;
3153 #endif
3155 if (c == '\0' || c == ',' || c == '#')
3156 continue;
3158 goal_alt_win[i] = true;
3162 for (i = 0; i < n_operands; i++)
3164 rtx old, new_reg;
3165 rtx op = *curr_id->operand_loc[i];
3167 if (goal_alt_win[i])
3169 if (goal_alt[i] == NO_REGS
3170 && REG_P (op)
3171 /* When we assign NO_REGS it means that we will not
3172 assign a hard register to the scratch pseudo by
3173 assigment pass and the scratch pseudo will be
3174 spilled. Spilled scratch pseudos are transformed
3175 back to scratches at the LRA end. */
3176 && lra_former_scratch_operand_p (curr_insn, i))
3178 int regno = REGNO (op);
3179 change_class (regno, NO_REGS, " Change", true);
3180 if (lra_get_regno_hard_regno (regno) >= 0)
3181 /* We don't have to mark all insn affected by the
3182 spilled pseudo as there is only one such insn, the
3183 current one. */
3184 reg_renumber[regno] = -1;
3186 continue;
3189 /* Operands that match previous ones have already been handled. */
3190 if (goal_alt_matches[i] >= 0)
3191 continue;
3193 /* We should not have an operand with a non-offsettable address
3194 appearing where an offsettable address will do. It also may
3195 be a case when the address should be special in other words
3196 not a general one (e.g. it needs no index reg). */
3197 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3199 enum reg_class rclass;
3200 rtx *loc = &XEXP (op, 0);
3201 enum rtx_code code = GET_CODE (*loc);
3203 push_to_sequence (before);
3204 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3205 MEM, SCRATCH);
3206 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3207 new_reg = emit_inc (rclass, *loc, *loc,
3208 /* This value does not matter for MODIFY. */
3209 GET_MODE_SIZE (GET_MODE (op)));
3210 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass,
3211 "offsetable address", &new_reg))
3212 lra_emit_move (new_reg, *loc);
3213 before = get_insns ();
3214 end_sequence ();
3215 *loc = new_reg;
3216 lra_update_dup (curr_id, i);
3218 else if (goal_alt_matched[i][0] == -1)
3220 enum machine_mode mode;
3221 rtx reg, *loc;
3222 int hard_regno, byte;
3223 enum op_type type = curr_static_id->operand[i].type;
3225 loc = curr_id->operand_loc[i];
3226 mode = curr_operand_mode[i];
3227 if (GET_CODE (*loc) == SUBREG)
3229 reg = SUBREG_REG (*loc);
3230 byte = SUBREG_BYTE (*loc);
3231 if (REG_P (reg)
3232 /* Strict_low_part requires reload the register not
3233 the sub-register. */
3234 && (curr_static_id->operand[i].strict_low
3235 || (GET_MODE_SIZE (mode)
3236 <= GET_MODE_SIZE (GET_MODE (reg))
3237 && (hard_regno
3238 = get_try_hard_regno (REGNO (reg))) >= 0
3239 && (simplify_subreg_regno
3240 (hard_regno,
3241 GET_MODE (reg), byte, mode) < 0)
3242 && (goal_alt[i] == NO_REGS
3243 || (simplify_subreg_regno
3244 (ira_class_hard_regs[goal_alt[i]][0],
3245 GET_MODE (reg), byte, mode) >= 0)))))
3247 loc = &SUBREG_REG (*loc);
3248 mode = GET_MODE (*loc);
3251 old = *loc;
3252 if (get_reload_reg (type, mode, old, goal_alt[i], "", &new_reg)
3253 && type != OP_OUT)
3255 push_to_sequence (before);
3256 lra_emit_move (new_reg, old);
3257 before = get_insns ();
3258 end_sequence ();
3260 *loc = new_reg;
3261 if (type != OP_IN
3262 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3264 start_sequence ();
3265 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3266 emit_insn (after);
3267 after = get_insns ();
3268 end_sequence ();
3269 *loc = new_reg;
3271 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3272 if (goal_alt_dont_inherit_ops[j] == i)
3274 lra_set_regno_unique_value (REGNO (new_reg));
3275 break;
3277 lra_update_dup (curr_id, i);
3279 else if (curr_static_id->operand[i].type == OP_IN
3280 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3281 == OP_OUT))
3283 /* generate reloads for input and matched outputs. */
3284 match_inputs[0] = i;
3285 match_inputs[1] = -1;
3286 match_reload (goal_alt_matched[i][0], match_inputs,
3287 goal_alt[i], &before, &after);
3289 else if (curr_static_id->operand[i].type == OP_OUT
3290 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3291 == OP_IN))
3292 /* Generate reloads for output and matched inputs. */
3293 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3294 else if (curr_static_id->operand[i].type == OP_IN
3295 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3296 == OP_IN))
3298 /* Generate reloads for matched inputs. */
3299 match_inputs[0] = i;
3300 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3301 match_inputs[j + 1] = k;
3302 match_inputs[j + 1] = -1;
3303 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3305 else
3306 /* We must generate code in any case when function
3307 process_alt_operands decides that it is possible. */
3308 gcc_unreachable ();
3310 if (before != NULL_RTX || after != NULL_RTX
3311 || max_regno_before != max_reg_num ())
3312 change_p = true;
3313 if (change_p)
3315 lra_update_operator_dups (curr_id);
3316 /* Something changes -- process the insn. */
3317 lra_update_insn_regno_info (curr_insn);
3319 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3320 return change_p;
3323 /* Return true if X is in LIST. */
3324 static bool
3325 in_list_p (rtx x, rtx list)
3327 for (; list != NULL_RTX; list = XEXP (list, 1))
3328 if (XEXP (list, 0) == x)
3329 return true;
3330 return false;
3333 /* Return true if X contains an allocatable hard register (if
3334 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3335 static bool
3336 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3338 int i, j;
3339 const char *fmt;
3340 enum rtx_code code;
3342 code = GET_CODE (x);
3343 if (REG_P (x))
3345 int regno = REGNO (x);
3346 HARD_REG_SET alloc_regs;
3348 if (hard_reg_p)
3350 if (regno >= FIRST_PSEUDO_REGISTER)
3351 regno = lra_get_regno_hard_regno (regno);
3352 if (regno < 0)
3353 return false;
3354 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3355 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3357 else
3359 if (regno < FIRST_PSEUDO_REGISTER)
3360 return false;
3361 if (! spilled_p)
3362 return true;
3363 return lra_get_regno_hard_regno (regno) < 0;
3366 fmt = GET_RTX_FORMAT (code);
3367 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3369 if (fmt[i] == 'e')
3371 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3372 return true;
3374 else if (fmt[i] == 'E')
3376 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3377 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3378 return true;
3381 return false;
3384 /* Process all regs in location *LOC and change them on equivalent
3385 substitution. Return true if any change was done. */
3386 static bool
3387 loc_equivalence_change_p (rtx *loc)
3389 rtx subst, reg, x = *loc;
3390 bool result = false;
3391 enum rtx_code code = GET_CODE (x);
3392 const char *fmt;
3393 int i, j;
3395 if (code == SUBREG)
3397 reg = SUBREG_REG (x);
3398 if ((subst = get_equiv_substitution (reg)) != reg
3399 && GET_MODE (subst) == VOIDmode)
3401 /* We cannot reload debug location. Simplify subreg here
3402 while we know the inner mode. */
3403 *loc = simplify_gen_subreg (GET_MODE (x), subst,
3404 GET_MODE (reg), SUBREG_BYTE (x));
3405 return true;
3408 if (code == REG && (subst = get_equiv_substitution (x)) != x)
3410 *loc = subst;
3411 return true;
3414 /* Scan all the operand sub-expressions. */
3415 fmt = GET_RTX_FORMAT (code);
3416 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3418 if (fmt[i] == 'e')
3419 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
3420 else if (fmt[i] == 'E')
3421 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3422 result
3423 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
3425 return result;
3428 /* Similar to loc_equivalence_change_p, but for use as
3429 simplify_replace_fn_rtx callback. */
3430 static rtx
3431 loc_equivalence_callback (rtx loc, const_rtx, void *)
3433 if (!REG_P (loc))
3434 return NULL_RTX;
3436 rtx subst = get_equiv_substitution (loc);
3437 if (subst != loc)
3438 return subst;
3440 return NULL_RTX;
3443 /* Maximum number of generated reload insns per an insn. It is for
3444 preventing this pass cycling in a bug case. */
3445 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
3447 /* The current iteration number of this LRA pass. */
3448 int lra_constraint_iter;
3450 /* The current iteration number of this LRA pass after the last spill
3451 pass. */
3452 int lra_constraint_iter_after_spill;
3454 /* True if we substituted equiv which needs checking register
3455 allocation correctness because the equivalent value contains
3456 allocatable hard registers or when we restore multi-register
3457 pseudo. */
3458 bool lra_risky_transformations_p;
3460 /* Return true if REGNO is referenced in more than one block. */
3461 static bool
3462 multi_block_pseudo_p (int regno)
3464 basic_block bb = NULL;
3465 unsigned int uid;
3466 bitmap_iterator bi;
3468 if (regno < FIRST_PSEUDO_REGISTER)
3469 return false;
3471 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
3472 if (bb == NULL)
3473 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
3474 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
3475 return true;
3476 return false;
3479 /* Return true if LIST contains a deleted insn. */
3480 static bool
3481 contains_deleted_insn_p (rtx list)
3483 for (; list != NULL_RTX; list = XEXP (list, 1))
3484 if (NOTE_P (XEXP (list, 0))
3485 && NOTE_KIND (XEXP (list, 0)) == NOTE_INSN_DELETED)
3486 return true;
3487 return false;
3490 /* Return true if X contains a pseudo dying in INSN. */
3491 static bool
3492 dead_pseudo_p (rtx x, rtx insn)
3494 int i, j;
3495 const char *fmt;
3496 enum rtx_code code;
3498 if (REG_P (x))
3499 return (insn != NULL_RTX
3500 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
3501 code = GET_CODE (x);
3502 fmt = GET_RTX_FORMAT (code);
3503 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3505 if (fmt[i] == 'e')
3507 if (dead_pseudo_p (XEXP (x, i), insn))
3508 return true;
3510 else if (fmt[i] == 'E')
3512 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3513 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
3514 return true;
3517 return false;
3520 /* Return true if INSN contains a dying pseudo in INSN right hand
3521 side. */
3522 static bool
3523 insn_rhs_dead_pseudo_p (rtx insn)
3525 rtx set = single_set (insn);
3527 gcc_assert (set != NULL);
3528 return dead_pseudo_p (SET_SRC (set), insn);
3531 /* Return true if any init insn of REGNO contains a dying pseudo in
3532 insn right hand side. */
3533 static bool
3534 init_insn_rhs_dead_pseudo_p (int regno)
3536 rtx insns = ira_reg_equiv[regno].init_insns;
3538 if (insns == NULL)
3539 return false;
3540 if (INSN_P (insns))
3541 return insn_rhs_dead_pseudo_p (insns);
3542 for (; insns != NULL_RTX; insns = XEXP (insns, 1))
3543 if (insn_rhs_dead_pseudo_p (XEXP (insns, 0)))
3544 return true;
3545 return false;
3548 /* Entry function of LRA constraint pass. Return true if the
3549 constraint pass did change the code. */
3550 bool
3551 lra_constraints (bool first_p)
3553 bool changed_p;
3554 int i, hard_regno, new_insns_num;
3555 unsigned int min_len, new_min_len, uid;
3556 rtx set, x, reg, dest_reg;
3557 basic_block last_bb;
3558 bitmap_head equiv_insn_bitmap;
3559 bitmap_iterator bi;
3561 lra_constraint_iter++;
3562 if (lra_dump_file != NULL)
3563 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
3564 lra_constraint_iter);
3565 lra_constraint_iter_after_spill++;
3566 if (lra_constraint_iter_after_spill > LRA_MAX_CONSTRAINT_ITERATION_NUMBER)
3567 internal_error
3568 ("Maximum number of LRA constraint passes is achieved (%d)\n",
3569 LRA_MAX_CONSTRAINT_ITERATION_NUMBER);
3570 changed_p = false;
3571 lra_risky_transformations_p = false;
3572 new_insn_uid_start = get_max_uid ();
3573 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
3574 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
3575 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3576 if (lra_reg_info[i].nrefs != 0)
3578 ira_reg_equiv[i].profitable_p = true;
3579 reg = regno_reg_rtx[i];
3580 if ((hard_regno = lra_get_regno_hard_regno (i)) >= 0)
3582 int j, nregs;
3584 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
3585 for (j = 0; j < nregs; j++)
3586 df_set_regs_ever_live (hard_regno + j, true);
3588 else if ((x = get_equiv_substitution (reg)) != reg)
3590 bool pseudo_p = contains_reg_p (x, false, false);
3591 rtx set, insns;
3593 /* After RTL transformation, we can not guarantee that
3594 pseudo in the substitution was not reloaded which might
3595 make equivalence invalid. For example, in reverse
3596 equiv of p0
3598 p0 <- ...
3600 equiv_mem <- p0
3602 the memory address register was reloaded before the 2nd
3603 insn. */
3604 if ((! first_p && pseudo_p)
3605 /* We don't use DF for compilation speed sake. So it
3606 is problematic to update live info when we use an
3607 equivalence containing pseudos in more than one
3608 BB. */
3609 || (pseudo_p && multi_block_pseudo_p (i))
3610 /* If an init insn was deleted for some reason, cancel
3611 the equiv. We could update the equiv insns after
3612 transformations including an equiv insn deletion
3613 but it is not worthy as such cases are extremely
3614 rare. */
3615 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
3616 /* If it is not a reverse equivalence, we check that a
3617 pseudo in rhs of the init insn is not dying in the
3618 insn. Otherwise, the live info at the beginning of
3619 the corresponding BB might be wrong after we
3620 removed the insn. When the equiv can be a
3621 constant, the right hand side of the init insn can
3622 be a pseudo. */
3623 || (! ((insns = ira_reg_equiv[i].init_insns) != NULL_RTX
3624 && INSN_P (XEXP (insns, 0))
3625 && XEXP (insns, 1) == NULL_RTX
3626 && (set = single_set (XEXP (insns, 0))) != NULL_RTX
3627 && REG_P (SET_SRC (set))
3628 && (int) REGNO (SET_SRC (set)) == i)
3629 && init_insn_rhs_dead_pseudo_p (i))
3630 /* Prevent access beyond equivalent memory for
3631 paradoxical subregs. */
3632 || (MEM_P (x)
3633 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
3634 > GET_MODE_SIZE (GET_MODE (x)))))
3635 ira_reg_equiv[i].defined_p = false;
3636 if (contains_reg_p (x, false, true))
3637 ira_reg_equiv[i].profitable_p = false;
3638 if (get_equiv_substitution (reg) != reg)
3639 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
3642 /* We should add all insns containing pseudos which should be
3643 substituted by their equivalences. */
3644 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
3645 lra_push_insn_by_uid (uid);
3646 lra_eliminate (false);
3647 min_len = lra_insn_stack_length ();
3648 new_insns_num = 0;
3649 last_bb = NULL;
3650 changed_p = false;
3651 while ((new_min_len = lra_insn_stack_length ()) != 0)
3653 curr_insn = lra_pop_insn ();
3654 --new_min_len;
3655 curr_bb = BLOCK_FOR_INSN (curr_insn);
3656 if (curr_bb != last_bb)
3658 last_bb = curr_bb;
3659 bb_reload_num = lra_curr_reload_num;
3661 if (min_len > new_min_len)
3663 min_len = new_min_len;
3664 new_insns_num = 0;
3666 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
3667 internal_error
3668 ("Max. number of generated reload insns per insn is achieved (%d)\n",
3669 MAX_RELOAD_INSNS_NUMBER);
3670 new_insns_num++;
3671 if (DEBUG_INSN_P (curr_insn))
3673 /* We need to check equivalence in debug insn and change
3674 pseudo to the equivalent value if necessary. */
3675 curr_id = lra_get_insn_recog_data (curr_insn);
3676 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
3678 rtx old = *curr_id->operand_loc[0];
3679 *curr_id->operand_loc[0]
3680 = simplify_replace_fn_rtx (old, NULL_RTX,
3681 loc_equivalence_callback, NULL);
3682 if (old != *curr_id->operand_loc[0])
3684 lra_update_insn_regno_info (curr_insn);
3685 changed_p = true;
3689 else if (INSN_P (curr_insn))
3691 if ((set = single_set (curr_insn)) != NULL_RTX)
3693 dest_reg = SET_DEST (set);
3694 /* The equivalence pseudo could be set up as SUBREG in a
3695 case when it is a call restore insn in a mode
3696 different from the pseudo mode. */
3697 if (GET_CODE (dest_reg) == SUBREG)
3698 dest_reg = SUBREG_REG (dest_reg);
3699 if ((REG_P (dest_reg)
3700 && (x = get_equiv_substitution (dest_reg)) != dest_reg
3701 /* Check that this is actually an insn setting up
3702 the equivalence. */
3703 && (in_list_p (curr_insn,
3704 ira_reg_equiv
3705 [REGNO (dest_reg)].init_insns)
3706 /* Init insns may contain not all insns setting
3707 up equivalence as we have live range
3708 splitting. So here we use another condition
3709 to check insn setting up the equivalence
3710 which should be removed, e.g. in case when
3711 the equivalence is a constant. */
3712 || ! MEM_P (x))
3713 /* Remove insns which set up a pseudo whose value
3714 can not be changed. Such insns might be not in
3715 init_insns because we don't update equiv data
3716 during insn transformations.
3718 As an example, let suppose that a pseudo got
3719 hard register and on the 1st pass was not
3720 changed to equivalent constant. We generate an
3721 additional insn setting up the pseudo because of
3722 secondary memory movement. Then the pseudo is
3723 spilled and we use the equiv constant. In this
3724 case we should remove the additional insn and
3725 this insn is not init_insns list. */
3726 && (! MEM_P (x) || MEM_READONLY_P (x)
3727 || in_list_p (curr_insn,
3728 ira_reg_equiv
3729 [REGNO (dest_reg)].init_insns)))
3730 || (((x = get_equiv_substitution (SET_SRC (set)))
3731 != SET_SRC (set))
3732 && in_list_p (curr_insn,
3733 ira_reg_equiv
3734 [REGNO (SET_SRC (set))].init_insns)))
3736 /* This is equiv init insn of pseudo which did not get a
3737 hard register -- remove the insn. */
3738 if (lra_dump_file != NULL)
3740 fprintf (lra_dump_file,
3741 " Removing equiv init insn %i (freq=%d)\n",
3742 INSN_UID (curr_insn),
3743 BLOCK_FOR_INSN (curr_insn)->frequency);
3744 dump_insn_slim (lra_dump_file, curr_insn);
3746 if (contains_reg_p (x, true, false))
3747 lra_risky_transformations_p = true;
3748 lra_set_insn_deleted (curr_insn);
3749 continue;
3752 curr_id = lra_get_insn_recog_data (curr_insn);
3753 curr_static_id = curr_id->insn_static_data;
3754 init_curr_insn_input_reloads ();
3755 init_curr_operand_mode ();
3756 if (curr_insn_transform ())
3757 changed_p = true;
3758 /* Check non-transformed insns too for equiv change as USE
3759 or CLOBBER don't need reloads but can contain pseudos
3760 being changed on their equivalences. */
3761 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
3762 && loc_equivalence_change_p (&PATTERN (curr_insn)))
3764 lra_update_insn_regno_info (curr_insn);
3765 changed_p = true;
3769 bitmap_clear (&equiv_insn_bitmap);
3770 /* If we used a new hard regno, changed_p should be true because the
3771 hard reg is assigned to a new pseudo. */
3772 #ifdef ENABLE_CHECKING
3773 if (! changed_p)
3775 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3776 if (lra_reg_info[i].nrefs != 0
3777 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
3779 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
3781 for (j = 0; j < nregs; j++)
3782 lra_assert (df_regs_ever_live_p (hard_regno + j));
3785 #endif
3786 return changed_p;
3789 /* Initiate the LRA constraint pass. It is done once per
3790 function. */
3791 void
3792 lra_constraints_init (void)
3796 /* Finalize the LRA constraint pass. It is done once per
3797 function. */
3798 void
3799 lra_constraints_finish (void)
3805 /* This page contains code to do inheritance/split
3806 transformations. */
3808 /* Number of reloads passed so far in current EBB. */
3809 static int reloads_num;
3811 /* Number of calls passed so far in current EBB. */
3812 static int calls_num;
3814 /* Current reload pseudo check for validity of elements in
3815 USAGE_INSNS. */
3816 static int curr_usage_insns_check;
3818 /* Info about last usage of registers in EBB to do inheritance/split
3819 transformation. Inheritance transformation is done from a spilled
3820 pseudo and split transformations from a hard register or a pseudo
3821 assigned to a hard register. */
3822 struct usage_insns
3824 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
3825 value INSNS is valid. The insns is chain of optional debug insns
3826 and a finishing non-debug insn using the corresponding reg. */
3827 int check;
3828 /* Value of global reloads_num at the last insn in INSNS. */
3829 int reloads_num;
3830 /* Value of global reloads_nums at the last insn in INSNS. */
3831 int calls_num;
3832 /* It can be true only for splitting. And it means that the restore
3833 insn should be put after insn given by the following member. */
3834 bool after_p;
3835 /* Next insns in the current EBB which use the original reg and the
3836 original reg value is not changed between the current insn and
3837 the next insns. In order words, e.g. for inheritance, if we need
3838 to use the original reg value again in the next insns we can try
3839 to use the value in a hard register from a reload insn of the
3840 current insn. */
3841 rtx insns;
3844 /* Map: regno -> corresponding pseudo usage insns. */
3845 static struct usage_insns *usage_insns;
3847 static void
3848 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
3850 usage_insns[regno].check = curr_usage_insns_check;
3851 usage_insns[regno].insns = insn;
3852 usage_insns[regno].reloads_num = reloads_num;
3853 usage_insns[regno].calls_num = calls_num;
3854 usage_insns[regno].after_p = after_p;
3857 /* The function is used to form list REGNO usages which consists of
3858 optional debug insns finished by a non-debug insn using REGNO.
3859 RELOADS_NUM is current number of reload insns processed so far. */
3860 static void
3861 add_next_usage_insn (int regno, rtx insn, int reloads_num)
3863 rtx next_usage_insns;
3865 if (usage_insns[regno].check == curr_usage_insns_check
3866 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
3867 && DEBUG_INSN_P (insn))
3869 /* Check that we did not add the debug insn yet. */
3870 if (next_usage_insns != insn
3871 && (GET_CODE (next_usage_insns) != INSN_LIST
3872 || XEXP (next_usage_insns, 0) != insn))
3873 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
3874 next_usage_insns);
3876 else if (NONDEBUG_INSN_P (insn))
3877 setup_next_usage_insn (regno, insn, reloads_num, false);
3878 else
3879 usage_insns[regno].check = 0;
3882 /* Replace all references to register OLD_REGNO in *LOC with pseudo
3883 register NEW_REG. Return true if any change was made. */
3884 static bool
3885 substitute_pseudo (rtx *loc, int old_regno, rtx new_reg)
3887 rtx x = *loc;
3888 bool result = false;
3889 enum rtx_code code;
3890 const char *fmt;
3891 int i, j;
3893 if (x == NULL_RTX)
3894 return false;
3896 code = GET_CODE (x);
3897 if (code == REG && (int) REGNO (x) == old_regno)
3899 enum machine_mode mode = GET_MODE (*loc);
3900 enum machine_mode inner_mode = GET_MODE (new_reg);
3902 if (mode != inner_mode)
3904 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
3905 || ! SCALAR_INT_MODE_P (inner_mode))
3906 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
3907 else
3908 new_reg = gen_lowpart_SUBREG (mode, new_reg);
3910 *loc = new_reg;
3911 return true;
3914 /* Scan all the operand sub-expressions. */
3915 fmt = GET_RTX_FORMAT (code);
3916 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3918 if (fmt[i] == 'e')
3920 if (substitute_pseudo (&XEXP (x, i), old_regno, new_reg))
3921 result = true;
3923 else if (fmt[i] == 'E')
3925 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3926 if (substitute_pseudo (&XVECEXP (x, i, j), old_regno, new_reg))
3927 result = true;
3930 return result;
3933 /* Return first non-debug insn in list USAGE_INSNS. */
3934 static rtx
3935 skip_usage_debug_insns (rtx usage_insns)
3937 rtx insn;
3939 /* Skip debug insns. */
3940 for (insn = usage_insns;
3941 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
3942 insn = XEXP (insn, 1))
3944 return insn;
3947 /* Return true if we need secondary memory moves for insn in
3948 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
3949 into the insn. */
3950 static bool
3951 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
3952 rtx usage_insns ATTRIBUTE_UNUSED)
3954 #ifndef SECONDARY_MEMORY_NEEDED
3955 return false;
3956 #else
3957 rtx insn, set, dest;
3958 enum reg_class cl;
3960 if (inher_cl == ALL_REGS
3961 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
3962 return false;
3963 lra_assert (INSN_P (insn));
3964 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
3965 return false;
3966 dest = SET_DEST (set);
3967 if (! REG_P (dest))
3968 return false;
3969 lra_assert (inher_cl != NO_REGS);
3970 cl = get_reg_class (REGNO (dest));
3971 return (cl != NO_REGS && cl != ALL_REGS
3972 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
3973 #endif
3976 /* Registers involved in inheritance/split in the current EBB
3977 (inheritance/split pseudos and original registers). */
3978 static bitmap_head check_only_regs;
3980 /* Do inheritance transformations for insn INSN, which defines (if
3981 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
3982 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
3983 form as the "insns" field of usage_insns. Return true if we
3984 succeed in such transformation.
3986 The transformations look like:
3988 p <- ... i <- ...
3989 ... p <- i (new insn)
3990 ... =>
3991 <- ... p ... <- ... i ...
3993 ... i <- p (new insn)
3994 <- ... p ... <- ... i ...
3995 ... =>
3996 <- ... p ... <- ... i ...
3997 where p is a spilled original pseudo and i is a new inheritance pseudo.
4000 The inheritance pseudo has the smallest class of two classes CL and
4001 class of ORIGINAL REGNO. */
4002 static bool
4003 inherit_reload_reg (bool def_p, int original_regno,
4004 enum reg_class cl, rtx insn, rtx next_usage_insns)
4006 enum reg_class rclass = lra_get_allocno_class (original_regno);
4007 rtx original_reg = regno_reg_rtx[original_regno];
4008 rtx new_reg, new_insns, usage_insn;
4010 lra_assert (! usage_insns[original_regno].after_p);
4011 if (lra_dump_file != NULL)
4012 fprintf (lra_dump_file,
4013 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4014 if (! ira_reg_classes_intersect_p[cl][rclass])
4016 if (lra_dump_file != NULL)
4018 fprintf (lra_dump_file,
4019 " Rejecting inheritance for %d "
4020 "because of disjoint classes %s and %s\n",
4021 original_regno, reg_class_names[cl],
4022 reg_class_names[rclass]);
4023 fprintf (lra_dump_file,
4024 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4026 return false;
4028 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4029 /* We don't use a subset of two classes because it can be
4030 NO_REGS. This transformation is still profitable in most
4031 cases even if the classes are not intersected as register
4032 move is probably cheaper than a memory load. */
4033 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4035 if (lra_dump_file != NULL)
4036 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4037 reg_class_names[cl], reg_class_names[rclass]);
4039 rclass = cl;
4041 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4043 /* Reject inheritance resulting in secondary memory moves.
4044 Otherwise, there is a danger in LRA cycling. Also such
4045 transformation will be unprofitable. */
4046 if (lra_dump_file != NULL)
4048 rtx insn = skip_usage_debug_insns (next_usage_insns);
4049 rtx set = single_set (insn);
4051 lra_assert (set != NULL_RTX);
4053 rtx dest = SET_DEST (set);
4055 lra_assert (REG_P (dest));
4056 fprintf (lra_dump_file,
4057 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4058 "as secondary mem is needed\n",
4059 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4060 original_regno, reg_class_names[rclass]);
4061 fprintf (lra_dump_file,
4062 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4064 return false;
4066 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4067 rclass, "inheritance");
4068 start_sequence ();
4069 if (def_p)
4070 emit_move_insn (original_reg, new_reg);
4071 else
4072 emit_move_insn (new_reg, original_reg);
4073 new_insns = get_insns ();
4074 end_sequence ();
4075 if (NEXT_INSN (new_insns) != NULL_RTX)
4077 if (lra_dump_file != NULL)
4079 fprintf (lra_dump_file,
4080 " Rejecting inheritance %d->%d "
4081 "as it results in 2 or more insns:\n",
4082 original_regno, REGNO (new_reg));
4083 dump_rtl_slim (lra_dump_file, new_insns, NULL_RTX, -1, 0);
4084 fprintf (lra_dump_file,
4085 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4087 return false;
4089 substitute_pseudo (&insn, original_regno, new_reg);
4090 lra_update_insn_regno_info (insn);
4091 if (! def_p)
4092 /* We now have a new usage insn for original regno. */
4093 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4094 if (lra_dump_file != NULL)
4095 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4096 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4097 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4098 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4099 bitmap_set_bit (&check_only_regs, original_regno);
4100 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4101 if (def_p)
4102 lra_process_new_insns (insn, NULL_RTX, new_insns,
4103 "Add original<-inheritance");
4104 else
4105 lra_process_new_insns (insn, new_insns, NULL_RTX,
4106 "Add inheritance<-original");
4107 while (next_usage_insns != NULL_RTX)
4109 if (GET_CODE (next_usage_insns) != INSN_LIST)
4111 usage_insn = next_usage_insns;
4112 lra_assert (NONDEBUG_INSN_P (usage_insn));
4113 next_usage_insns = NULL;
4115 else
4117 usage_insn = XEXP (next_usage_insns, 0);
4118 lra_assert (DEBUG_INSN_P (usage_insn));
4119 next_usage_insns = XEXP (next_usage_insns, 1);
4121 substitute_pseudo (&usage_insn, original_regno, new_reg);
4122 lra_update_insn_regno_info (usage_insn);
4123 if (lra_dump_file != NULL)
4125 fprintf (lra_dump_file,
4126 " Inheritance reuse change %d->%d (bb%d):\n",
4127 original_regno, REGNO (new_reg),
4128 BLOCK_FOR_INSN (usage_insn)->index);
4129 dump_insn_slim (lra_dump_file, usage_insn);
4132 if (lra_dump_file != NULL)
4133 fprintf (lra_dump_file,
4134 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4135 return true;
4138 /* Return true if we need a caller save/restore for pseudo REGNO which
4139 was assigned to a hard register. */
4140 static inline bool
4141 need_for_call_save_p (int regno)
4143 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4144 return (usage_insns[regno].calls_num < calls_num
4145 && (overlaps_hard_reg_set_p
4146 (call_used_reg_set,
4147 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])));
4150 /* Global registers occuring in the current EBB. */
4151 static bitmap_head ebb_global_regs;
4153 /* Return true if we need a split for hard register REGNO or pseudo
4154 REGNO which was assigned to a hard register.
4155 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4156 used for reloads since the EBB end. It is an approximation of the
4157 used hard registers in the split range. The exact value would
4158 require expensive calculations. If we were aggressive with
4159 splitting because of the approximation, the split pseudo will save
4160 the same hard register assignment and will be removed in the undo
4161 pass. We still need the approximation because too aggressive
4162 splitting would result in too inaccurate cost calculation in the
4163 assignment pass because of too many generated moves which will be
4164 probably removed in the undo pass. */
4165 static inline bool
4166 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4168 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4170 lra_assert (hard_regno >= 0);
4171 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4172 /* Don't split eliminable hard registers, otherwise we can
4173 split hard registers like hard frame pointer, which
4174 lives on BB start/end according to DF-infrastructure,
4175 when there is a pseudo assigned to the register and
4176 living in the same BB. */
4177 && (regno >= FIRST_PSEUDO_REGISTER
4178 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4179 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4180 /* We need at least 2 reloads to make pseudo splitting
4181 profitable. We should provide hard regno splitting in
4182 any case to solve 1st insn scheduling problem when
4183 moving hard register definition up might result in
4184 impossibility to find hard register for reload pseudo of
4185 small register class. */
4186 && (usage_insns[regno].reloads_num
4187 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 2) < reloads_num)
4188 && (regno < FIRST_PSEUDO_REGISTER
4189 /* For short living pseudos, spilling + inheritance can
4190 be considered a substitution for splitting.
4191 Therefore we do not splitting for local pseudos. It
4192 decreases also aggressiveness of splitting. The
4193 minimal number of references is chosen taking into
4194 account that for 2 references splitting has no sense
4195 as we can just spill the pseudo. */
4196 || (regno >= FIRST_PSEUDO_REGISTER
4197 && lra_reg_info[regno].nrefs > 3
4198 && bitmap_bit_p (&ebb_global_regs, regno))))
4199 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4202 /* Return class for the split pseudo created from original pseudo with
4203 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4204 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4205 results in no secondary memory movements. */
4206 static enum reg_class
4207 choose_split_class (enum reg_class allocno_class,
4208 int hard_regno ATTRIBUTE_UNUSED,
4209 enum machine_mode mode ATTRIBUTE_UNUSED)
4211 #ifndef SECONDARY_MEMORY_NEEDED
4212 return allocno_class;
4213 #else
4214 int i;
4215 enum reg_class cl, best_cl = NO_REGS;
4216 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4217 = REGNO_REG_CLASS (hard_regno);
4219 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4220 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4221 return allocno_class;
4222 for (i = 0;
4223 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4224 i++)
4225 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4226 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4227 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4228 && (best_cl == NO_REGS
4229 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4230 best_cl = cl;
4231 return best_cl;
4232 #endif
4235 /* Do split transformations for insn INSN, which defines or uses
4236 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4237 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4238 "insns" field of usage_insns.
4240 The transformations look like:
4242 p <- ... p <- ...
4243 ... s <- p (new insn -- save)
4244 ... =>
4245 ... p <- s (new insn -- restore)
4246 <- ... p ... <- ... p ...
4248 <- ... p ... <- ... p ...
4249 ... s <- p (new insn -- save)
4250 ... =>
4251 ... p <- s (new insn -- restore)
4252 <- ... p ... <- ... p ...
4254 where p is an original pseudo got a hard register or a hard
4255 register and s is a new split pseudo. The save is put before INSN
4256 if BEFORE_P is true. Return true if we succeed in such
4257 transformation. */
4258 static bool
4259 split_reg (bool before_p, int original_regno, rtx insn, rtx next_usage_insns)
4261 enum reg_class rclass;
4262 rtx original_reg;
4263 int hard_regno, nregs;
4264 rtx new_reg, save, restore, usage_insn;
4265 bool after_p;
4266 bool call_save_p;
4268 if (original_regno < FIRST_PSEUDO_REGISTER)
4270 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4271 hard_regno = original_regno;
4272 call_save_p = false;
4273 nregs = 1;
4275 else
4277 hard_regno = reg_renumber[original_regno];
4278 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4279 rclass = lra_get_allocno_class (original_regno);
4280 original_reg = regno_reg_rtx[original_regno];
4281 call_save_p = need_for_call_save_p (original_regno);
4283 original_reg = regno_reg_rtx[original_regno];
4284 lra_assert (hard_regno >= 0);
4285 if (lra_dump_file != NULL)
4286 fprintf (lra_dump_file,
4287 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4288 if (call_save_p)
4290 enum machine_mode sec_mode;
4292 #ifdef SECONDARY_MEMORY_NEEDED_MODE
4293 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (original_reg));
4294 #else
4295 sec_mode = GET_MODE (original_reg);
4296 #endif
4297 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
4298 NO_REGS, "save");
4300 else
4302 rclass = choose_split_class (rclass, hard_regno,
4303 GET_MODE (original_reg));
4304 if (rclass == NO_REGS)
4306 if (lra_dump_file != NULL)
4308 fprintf (lra_dump_file,
4309 " Rejecting split of %d(%s): "
4310 "no good reg class for %d(%s)\n",
4311 original_regno,
4312 reg_class_names[lra_get_allocno_class (original_regno)],
4313 hard_regno,
4314 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4315 fprintf
4316 (lra_dump_file,
4317 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4319 return false;
4321 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4322 rclass, "split");
4323 reg_renumber[REGNO (new_reg)] = hard_regno;
4325 save = emit_spill_move (true, new_reg, original_reg);
4326 if (NEXT_INSN (save) != NULL_RTX)
4328 lra_assert (! call_save_p);
4329 if (lra_dump_file != NULL)
4331 fprintf
4332 (lra_dump_file,
4333 " Rejecting split %d->%d resulting in > 2 %s save insns:\n",
4334 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4335 dump_rtl_slim (lra_dump_file, save, NULL_RTX, -1, 0);
4336 fprintf (lra_dump_file,
4337 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4339 return false;
4341 restore = emit_spill_move (false, new_reg, original_reg);
4342 if (NEXT_INSN (restore) != NULL_RTX)
4344 lra_assert (! call_save_p);
4345 if (lra_dump_file != NULL)
4347 fprintf (lra_dump_file,
4348 " Rejecting split %d->%d "
4349 "resulting in > 2 %s restore insns:\n",
4350 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4351 dump_rtl_slim (lra_dump_file, restore, NULL_RTX, -1, 0);
4352 fprintf (lra_dump_file,
4353 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4355 return false;
4357 after_p = usage_insns[original_regno].after_p;
4358 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4359 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4360 bitmap_set_bit (&check_only_regs, original_regno);
4361 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4362 for (;;)
4364 if (GET_CODE (next_usage_insns) != INSN_LIST)
4366 usage_insn = next_usage_insns;
4367 break;
4369 usage_insn = XEXP (next_usage_insns, 0);
4370 lra_assert (DEBUG_INSN_P (usage_insn));
4371 next_usage_insns = XEXP (next_usage_insns, 1);
4372 substitute_pseudo (&usage_insn, original_regno, new_reg);
4373 lra_update_insn_regno_info (usage_insn);
4374 if (lra_dump_file != NULL)
4376 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
4377 original_regno, REGNO (new_reg));
4378 dump_insn_slim (lra_dump_file, usage_insn);
4381 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
4382 lra_assert (usage_insn != insn || (after_p && before_p));
4383 lra_process_new_insns (usage_insn, after_p ? NULL_RTX : restore,
4384 after_p ? restore : NULL_RTX,
4385 call_save_p
4386 ? "Add reg<-save" : "Add reg<-split");
4387 lra_process_new_insns (insn, before_p ? save : NULL_RTX,
4388 before_p ? NULL_RTX : save,
4389 call_save_p
4390 ? "Add save<-reg" : "Add split<-reg");
4391 if (nregs > 1)
4392 /* If we are trying to split multi-register. We should check
4393 conflicts on the next assignment sub-pass. IRA can allocate on
4394 sub-register levels, LRA do this on pseudos level right now and
4395 this discrepancy may create allocation conflicts after
4396 splitting. */
4397 lra_risky_transformations_p = true;
4398 if (lra_dump_file != NULL)
4399 fprintf (lra_dump_file,
4400 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4401 return true;
4404 /* Recognize that we need a split transformation for insn INSN, which
4405 defines or uses REGNO in its insn biggest MODE (we use it only if
4406 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
4407 hard registers which might be used for reloads since the EBB end.
4408 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
4409 uid before starting INSN processing. Return true if we succeed in
4410 such transformation. */
4411 static bool
4412 split_if_necessary (int regno, enum machine_mode mode,
4413 HARD_REG_SET potential_reload_hard_regs,
4414 bool before_p, rtx insn, int max_uid)
4416 bool res = false;
4417 int i, nregs = 1;
4418 rtx next_usage_insns;
4420 if (regno < FIRST_PSEUDO_REGISTER)
4421 nregs = hard_regno_nregs[regno][mode];
4422 for (i = 0; i < nregs; i++)
4423 if (usage_insns[regno + i].check == curr_usage_insns_check
4424 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
4425 /* To avoid processing the register twice or more. */
4426 && ((GET_CODE (next_usage_insns) != INSN_LIST
4427 && INSN_UID (next_usage_insns) < max_uid)
4428 || (GET_CODE (next_usage_insns) == INSN_LIST
4429 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
4430 && need_for_split_p (potential_reload_hard_regs, regno + i)
4431 && split_reg (before_p, regno + i, insn, next_usage_insns))
4432 res = true;
4433 return res;
4436 /* Check only registers living at the current program point in the
4437 current EBB. */
4438 static bitmap_head live_regs;
4440 /* Update live info in EBB given by its HEAD and TAIL insns after
4441 inheritance/split transformation. The function removes dead moves
4442 too. */
4443 static void
4444 update_ebb_live_info (rtx head, rtx tail)
4446 unsigned int j;
4447 int regno;
4448 bool live_p;
4449 rtx prev_insn, set;
4450 bool remove_p;
4451 basic_block last_bb, prev_bb, curr_bb;
4452 bitmap_iterator bi;
4453 struct lra_insn_reg *reg;
4454 edge e;
4455 edge_iterator ei;
4457 last_bb = BLOCK_FOR_INSN (tail);
4458 prev_bb = NULL;
4459 for (curr_insn = tail;
4460 curr_insn != PREV_INSN (head);
4461 curr_insn = prev_insn)
4463 prev_insn = PREV_INSN (curr_insn);
4464 /* We need to process empty blocks too. They contain
4465 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
4466 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
4467 continue;
4468 curr_bb = BLOCK_FOR_INSN (curr_insn);
4469 if (curr_bb != prev_bb)
4471 if (prev_bb != NULL)
4473 /* Update df_get_live_in (prev_bb): */
4474 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4475 if (bitmap_bit_p (&live_regs, j))
4476 bitmap_set_bit (df_get_live_in (prev_bb), j);
4477 else
4478 bitmap_clear_bit (df_get_live_in (prev_bb), j);
4480 if (curr_bb != last_bb)
4482 /* Update df_get_live_out (curr_bb): */
4483 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4485 live_p = bitmap_bit_p (&live_regs, j);
4486 if (! live_p)
4487 FOR_EACH_EDGE (e, ei, curr_bb->succs)
4488 if (bitmap_bit_p (df_get_live_in (e->dest), j))
4490 live_p = true;
4491 break;
4493 if (live_p)
4494 bitmap_set_bit (df_get_live_out (curr_bb), j);
4495 else
4496 bitmap_clear_bit (df_get_live_out (curr_bb), j);
4499 prev_bb = curr_bb;
4500 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
4502 if (! NONDEBUG_INSN_P (curr_insn))
4503 continue;
4504 curr_id = lra_get_insn_recog_data (curr_insn);
4505 remove_p = false;
4506 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
4507 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
4508 && bitmap_bit_p (&check_only_regs, regno)
4509 && ! bitmap_bit_p (&live_regs, regno))
4510 remove_p = true;
4511 /* See which defined values die here. */
4512 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4513 if (reg->type == OP_OUT && ! reg->subreg_p)
4514 bitmap_clear_bit (&live_regs, reg->regno);
4515 /* Mark each used value as live. */
4516 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4517 if (reg->type == OP_IN
4518 && bitmap_bit_p (&check_only_regs, reg->regno))
4519 bitmap_set_bit (&live_regs, reg->regno);
4520 /* It is quite important to remove dead move insns because it
4521 means removing dead store. We don't need to process them for
4522 constraints. */
4523 if (remove_p)
4525 if (lra_dump_file != NULL)
4527 fprintf (lra_dump_file, " Removing dead insn:\n ");
4528 dump_insn_slim (lra_dump_file, curr_insn);
4530 lra_set_insn_deleted (curr_insn);
4535 /* The structure describes info to do an inheritance for the current
4536 insn. We need to collect such info first before doing the
4537 transformations because the transformations change the insn
4538 internal representation. */
4539 struct to_inherit
4541 /* Original regno. */
4542 int regno;
4543 /* Subsequent insns which can inherit original reg value. */
4544 rtx insns;
4547 /* Array containing all info for doing inheritance from the current
4548 insn. */
4549 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
4551 /* Number elements in the previous array. */
4552 static int to_inherit_num;
4554 /* Add inheritance info REGNO and INSNS. Their meaning is described in
4555 structure to_inherit. */
4556 static void
4557 add_to_inherit (int regno, rtx insns)
4559 int i;
4561 for (i = 0; i < to_inherit_num; i++)
4562 if (to_inherit[i].regno == regno)
4563 return;
4564 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
4565 to_inherit[to_inherit_num].regno = regno;
4566 to_inherit[to_inherit_num++].insns = insns;
4569 /* Return the last non-debug insn in basic block BB, or the block begin
4570 note if none. */
4571 static rtx
4572 get_last_insertion_point (basic_block bb)
4574 rtx insn;
4576 FOR_BB_INSNS_REVERSE (bb, insn)
4577 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
4578 return insn;
4579 gcc_unreachable ();
4582 /* Set up RES by registers living on edges FROM except the edge (FROM,
4583 TO) or by registers set up in a jump insn in BB FROM. */
4584 static void
4585 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
4587 rtx last;
4588 struct lra_insn_reg *reg;
4589 edge e;
4590 edge_iterator ei;
4592 lra_assert (to != NULL);
4593 bitmap_clear (res);
4594 FOR_EACH_EDGE (e, ei, from->succs)
4595 if (e->dest != to)
4596 bitmap_ior_into (res, df_get_live_in (e->dest));
4597 last = get_last_insertion_point (from);
4598 if (! JUMP_P (last))
4599 return;
4600 curr_id = lra_get_insn_recog_data (last);
4601 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4602 if (reg->type != OP_IN)
4603 bitmap_set_bit (res, reg->regno);
4606 /* Used as a temporary results of some bitmap calculations. */
4607 static bitmap_head temp_bitmap;
4609 /* Do inheritance/split transformations in EBB starting with HEAD and
4610 finishing on TAIL. We process EBB insns in the reverse order.
4611 Return true if we did any inheritance/split transformation in the
4612 EBB.
4614 We should avoid excessive splitting which results in worse code
4615 because of inaccurate cost calculations for spilling new split
4616 pseudos in such case. To achieve this we do splitting only if
4617 register pressure is high in given basic block and there are reload
4618 pseudos requiring hard registers. We could do more register
4619 pressure calculations at any given program point to avoid necessary
4620 splitting even more but it is to expensive and the current approach
4621 works well enough. */
4622 static bool
4623 inherit_in_ebb (rtx head, rtx tail)
4625 int i, src_regno, dst_regno, nregs;
4626 bool change_p, succ_p;
4627 rtx prev_insn, next_usage_insns, set, last_insn;
4628 enum reg_class cl;
4629 struct lra_insn_reg *reg;
4630 basic_block last_processed_bb, curr_bb = NULL;
4631 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
4632 bitmap to_process;
4633 unsigned int j;
4634 bitmap_iterator bi;
4635 bool head_p, after_p;
4637 change_p = false;
4638 curr_usage_insns_check++;
4639 reloads_num = calls_num = 0;
4640 bitmap_clear (&check_only_regs);
4641 last_processed_bb = NULL;
4642 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
4643 CLEAR_HARD_REG_SET (live_hard_regs);
4644 /* We don't process new insns generated in the loop. */
4645 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
4647 prev_insn = PREV_INSN (curr_insn);
4648 if (BLOCK_FOR_INSN (curr_insn) != NULL)
4649 curr_bb = BLOCK_FOR_INSN (curr_insn);
4650 if (last_processed_bb != curr_bb)
4652 /* We are at the end of BB. Add qualified living
4653 pseudos for potential splitting. */
4654 to_process = df_get_live_out (curr_bb);
4655 if (last_processed_bb != NULL)
4657 /* We are somewhere in the middle of EBB. */
4658 get_live_on_other_edges (curr_bb, last_processed_bb,
4659 &temp_bitmap);
4660 to_process = &temp_bitmap;
4662 last_processed_bb = curr_bb;
4663 last_insn = get_last_insertion_point (curr_bb);
4664 after_p = (! JUMP_P (last_insn)
4665 && (! CALL_P (last_insn)
4666 || (find_reg_note (last_insn,
4667 REG_NORETURN, NULL_RTX) == NULL_RTX
4668 && ! SIBLING_CALL_P (last_insn))));
4669 REG_SET_TO_HARD_REG_SET (live_hard_regs, df_get_live_out (curr_bb));
4670 IOR_HARD_REG_SET (live_hard_regs, eliminable_regset);
4671 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
4672 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
4673 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
4675 if ((int) j >= lra_constraint_new_regno_start)
4676 break;
4677 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
4679 if (j < FIRST_PSEUDO_REGISTER)
4680 SET_HARD_REG_BIT (live_hard_regs, j);
4681 else
4682 add_to_hard_reg_set (&live_hard_regs,
4683 PSEUDO_REGNO_MODE (j),
4684 reg_renumber[j]);
4685 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
4689 src_regno = dst_regno = -1;
4690 if (NONDEBUG_INSN_P (curr_insn)
4691 && (set = single_set (curr_insn)) != NULL_RTX
4692 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
4694 src_regno = REGNO (SET_SRC (set));
4695 dst_regno = REGNO (SET_DEST (set));
4697 if (src_regno < lra_constraint_new_regno_start
4698 && src_regno >= FIRST_PSEUDO_REGISTER
4699 && reg_renumber[src_regno] < 0
4700 && dst_regno >= lra_constraint_new_regno_start
4701 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
4703 /* 'reload_pseudo <- original_pseudo'. */
4704 reloads_num++;
4705 succ_p = false;
4706 if (usage_insns[src_regno].check == curr_usage_insns_check
4707 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
4708 succ_p = inherit_reload_reg (false, src_regno, cl,
4709 curr_insn, next_usage_insns);
4710 if (succ_p)
4711 change_p = true;
4712 else
4713 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
4714 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
4715 IOR_HARD_REG_SET (potential_reload_hard_regs,
4716 reg_class_contents[cl]);
4718 else if (src_regno >= lra_constraint_new_regno_start
4719 && dst_regno < lra_constraint_new_regno_start
4720 && dst_regno >= FIRST_PSEUDO_REGISTER
4721 && reg_renumber[dst_regno] < 0
4722 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
4723 && usage_insns[dst_regno].check == curr_usage_insns_check
4724 && (next_usage_insns
4725 = usage_insns[dst_regno].insns) != NULL_RTX)
4727 reloads_num++;
4728 /* 'original_pseudo <- reload_pseudo'. */
4729 if (! JUMP_P (curr_insn)
4730 && inherit_reload_reg (true, dst_regno, cl,
4731 curr_insn, next_usage_insns))
4732 change_p = true;
4733 /* Invalidate. */
4734 usage_insns[dst_regno].check = 0;
4735 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
4736 IOR_HARD_REG_SET (potential_reload_hard_regs,
4737 reg_class_contents[cl]);
4739 else if (INSN_P (curr_insn))
4741 int max_uid = get_max_uid ();
4743 curr_id = lra_get_insn_recog_data (curr_insn);
4744 to_inherit_num = 0;
4745 /* Process insn definitions. */
4746 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4747 if (reg->type != OP_IN
4748 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
4750 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
4751 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
4752 && usage_insns[dst_regno].check == curr_usage_insns_check
4753 && (next_usage_insns
4754 = usage_insns[dst_regno].insns) != NULL_RTX)
4756 struct lra_insn_reg *r;
4758 for (r = curr_id->regs; r != NULL; r = r->next)
4759 if (r->type != OP_OUT && r->regno == dst_regno)
4760 break;
4761 /* Don't do inheritance if the pseudo is also
4762 used in the insn. */
4763 if (r == NULL)
4764 /* We can not do inheritance right now
4765 because the current insn reg info (chain
4766 regs) can change after that. */
4767 add_to_inherit (dst_regno, next_usage_insns);
4769 /* We can not process one reg twice here because of
4770 usage_insns invalidation. */
4771 if ((dst_regno < FIRST_PSEUDO_REGISTER
4772 || reg_renumber[dst_regno] >= 0)
4773 && ! reg->subreg_p && reg->type == OP_OUT)
4775 HARD_REG_SET s;
4777 if (split_if_necessary (dst_regno, reg->biggest_mode,
4778 potential_reload_hard_regs,
4779 false, curr_insn, max_uid))
4780 change_p = true;
4781 CLEAR_HARD_REG_SET (s);
4782 if (dst_regno < FIRST_PSEUDO_REGISTER)
4783 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
4784 else
4785 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
4786 reg_renumber[dst_regno]);
4787 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
4789 /* We should invalidate potential inheritance or
4790 splitting for the current insn usages to the next
4791 usage insns (see code below) as the output pseudo
4792 prevents this. */
4793 if ((dst_regno >= FIRST_PSEUDO_REGISTER
4794 && reg_renumber[dst_regno] < 0)
4795 || (reg->type == OP_OUT && ! reg->subreg_p
4796 && (dst_regno < FIRST_PSEUDO_REGISTER
4797 || reg_renumber[dst_regno] >= 0)))
4799 /* Invalidate. */
4800 if (dst_regno >= FIRST_PSEUDO_REGISTER)
4801 usage_insns[dst_regno].check = 0;
4802 else
4804 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
4805 for (i = 0; i < nregs; i++)
4806 usage_insns[dst_regno + i].check = 0;
4810 if (! JUMP_P (curr_insn))
4811 for (i = 0; i < to_inherit_num; i++)
4812 if (inherit_reload_reg (true, to_inherit[i].regno,
4813 ALL_REGS, curr_insn,
4814 to_inherit[i].insns))
4815 change_p = true;
4816 if (CALL_P (curr_insn))
4818 rtx cheap, pat, dest, restore;
4819 int regno, hard_regno;
4821 calls_num++;
4822 if ((cheap = find_reg_note (curr_insn,
4823 REG_RETURNED, NULL_RTX)) != NULL_RTX
4824 && ((cheap = XEXP (cheap, 0)), true)
4825 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
4826 && (hard_regno = reg_renumber[regno]) >= 0
4827 /* If there are pending saves/restores, the
4828 optimization is not worth. */
4829 && usage_insns[regno].calls_num == calls_num - 1
4830 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
4832 /* Restore the pseudo from the call result as
4833 REG_RETURNED note says that the pseudo value is
4834 in the call result and the pseudo is an argument
4835 of the call. */
4836 pat = PATTERN (curr_insn);
4837 if (GET_CODE (pat) == PARALLEL)
4838 pat = XVECEXP (pat, 0, 0);
4839 dest = SET_DEST (pat);
4840 start_sequence ();
4841 emit_move_insn (cheap, copy_rtx (dest));
4842 restore = get_insns ();
4843 end_sequence ();
4844 lra_process_new_insns (curr_insn, NULL, restore,
4845 "Inserting call parameter restore");
4846 /* We don't need to save/restore of the pseudo from
4847 this call. */
4848 usage_insns[regno].calls_num = calls_num;
4849 bitmap_set_bit (&check_only_regs, regno);
4852 to_inherit_num = 0;
4853 /* Process insn usages. */
4854 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4855 if ((reg->type != OP_OUT
4856 || (reg->type == OP_OUT && reg->subreg_p))
4857 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
4859 if (src_regno >= FIRST_PSEUDO_REGISTER
4860 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
4862 if (usage_insns[src_regno].check == curr_usage_insns_check
4863 && (next_usage_insns
4864 = usage_insns[src_regno].insns) != NULL_RTX
4865 && NONDEBUG_INSN_P (curr_insn))
4866 add_to_inherit (src_regno, next_usage_insns);
4867 else
4868 /* Add usages. */
4869 add_next_usage_insn (src_regno, curr_insn, reloads_num);
4871 else if (src_regno < FIRST_PSEUDO_REGISTER
4872 || reg_renumber[src_regno] >= 0)
4874 bool before_p;
4875 rtx use_insn = curr_insn;
4877 before_p = (JUMP_P (curr_insn)
4878 || (CALL_P (curr_insn) && reg->type == OP_IN));
4879 if (NONDEBUG_INSN_P (curr_insn)
4880 && split_if_necessary (src_regno, reg->biggest_mode,
4881 potential_reload_hard_regs,
4882 before_p, curr_insn, max_uid))
4884 if (reg->subreg_p)
4885 lra_risky_transformations_p = true;
4886 change_p = true;
4887 /* Invalidate. */
4888 usage_insns[src_regno].check = 0;
4889 if (before_p)
4890 use_insn = PREV_INSN (curr_insn);
4892 if (NONDEBUG_INSN_P (curr_insn))
4894 if (src_regno < FIRST_PSEUDO_REGISTER)
4895 add_to_hard_reg_set (&live_hard_regs,
4896 reg->biggest_mode, src_regno);
4897 else
4898 add_to_hard_reg_set (&live_hard_regs,
4899 PSEUDO_REGNO_MODE (src_regno),
4900 reg_renumber[src_regno]);
4902 add_next_usage_insn (src_regno, use_insn, reloads_num);
4905 for (i = 0; i < to_inherit_num; i++)
4907 src_regno = to_inherit[i].regno;
4908 if (inherit_reload_reg (false, src_regno, ALL_REGS,
4909 curr_insn, to_inherit[i].insns))
4910 change_p = true;
4911 else
4912 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
4915 /* We reached the start of the current basic block. */
4916 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
4917 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
4919 /* We reached the beginning of the current block -- do
4920 rest of spliting in the current BB. */
4921 to_process = df_get_live_in (curr_bb);
4922 if (BLOCK_FOR_INSN (head) != curr_bb)
4924 /* We are somewhere in the middle of EBB. */
4925 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
4926 curr_bb, &temp_bitmap);
4927 to_process = &temp_bitmap;
4929 head_p = true;
4930 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
4932 if ((int) j >= lra_constraint_new_regno_start)
4933 break;
4934 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
4935 && usage_insns[j].check == curr_usage_insns_check
4936 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
4938 if (need_for_split_p (potential_reload_hard_regs, j))
4940 if (lra_dump_file != NULL && head_p)
4942 fprintf (lra_dump_file,
4943 " ----------------------------------\n");
4944 head_p = false;
4946 if (split_reg (false, j, bb_note (curr_bb),
4947 next_usage_insns))
4948 change_p = true;
4950 usage_insns[j].check = 0;
4955 return change_p;
4958 /* This value affects EBB forming. If probability of edge from EBB to
4959 a BB is not greater than the following value, we don't add the BB
4960 to EBB. */
4961 #define EBB_PROBABILITY_CUTOFF ((REG_BR_PROB_BASE * 50) / 100)
4963 /* Current number of inheritance/split iteration. */
4964 int lra_inheritance_iter;
4966 /* Entry function for inheritance/split pass. */
4967 void
4968 lra_inheritance (void)
4970 int i;
4971 basic_block bb, start_bb;
4972 edge e;
4974 lra_inheritance_iter++;
4975 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
4976 return;
4977 timevar_push (TV_LRA_INHERITANCE);
4978 if (lra_dump_file != NULL)
4979 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
4980 lra_inheritance_iter);
4981 curr_usage_insns_check = 0;
4982 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
4983 for (i = 0; i < lra_constraint_new_regno_start; i++)
4984 usage_insns[i].check = 0;
4985 bitmap_initialize (&check_only_regs, &reg_obstack);
4986 bitmap_initialize (&live_regs, &reg_obstack);
4987 bitmap_initialize (&temp_bitmap, &reg_obstack);
4988 bitmap_initialize (&ebb_global_regs, &reg_obstack);
4989 FOR_EACH_BB (bb)
4991 start_bb = bb;
4992 if (lra_dump_file != NULL)
4993 fprintf (lra_dump_file, "EBB");
4994 /* Form a EBB starting with BB. */
4995 bitmap_clear (&ebb_global_regs);
4996 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
4997 for (;;)
4999 if (lra_dump_file != NULL)
5000 fprintf (lra_dump_file, " %d", bb->index);
5001 if (bb->next_bb == EXIT_BLOCK_PTR || LABEL_P (BB_HEAD (bb->next_bb)))
5002 break;
5003 e = find_fallthru_edge (bb->succs);
5004 if (! e)
5005 break;
5006 if (e->probability <= EBB_PROBABILITY_CUTOFF)
5007 break;
5008 bb = bb->next_bb;
5010 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5011 if (lra_dump_file != NULL)
5012 fprintf (lra_dump_file, "\n");
5013 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5014 /* Remember that the EBB head and tail can change in
5015 inherit_in_ebb. */
5016 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5018 bitmap_clear (&ebb_global_regs);
5019 bitmap_clear (&temp_bitmap);
5020 bitmap_clear (&live_regs);
5021 bitmap_clear (&check_only_regs);
5022 free (usage_insns);
5024 timevar_pop (TV_LRA_INHERITANCE);
5029 /* This page contains code to undo failed inheritance/split
5030 transformations. */
5032 /* Current number of iteration undoing inheritance/split. */
5033 int lra_undo_inheritance_iter;
5035 /* Fix BB live info LIVE after removing pseudos created on pass doing
5036 inheritance/split which are REMOVED_PSEUDOS. */
5037 static void
5038 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5040 unsigned int regno;
5041 bitmap_iterator bi;
5043 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5044 if (bitmap_clear_bit (live, regno))
5045 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5048 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5049 number. */
5050 static int
5051 get_regno (rtx reg)
5053 if (GET_CODE (reg) == SUBREG)
5054 reg = SUBREG_REG (reg);
5055 if (REG_P (reg))
5056 return REGNO (reg);
5057 return -1;
5060 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5061 return true if we did any change. The undo transformations for
5062 inheritance looks like
5063 i <- i2
5064 p <- i => p <- i2
5065 or removing
5066 p <- i, i <- p, and i <- i3
5067 where p is original pseudo from which inheritance pseudo i was
5068 created, i and i3 are removed inheritance pseudos, i2 is another
5069 not removed inheritance pseudo. All split pseudos or other
5070 occurrences of removed inheritance pseudos are changed on the
5071 corresponding original pseudos.
5073 The function also schedules insns changed and created during
5074 inheritance/split pass for processing by the subsequent constraint
5075 pass. */
5076 static bool
5077 remove_inheritance_pseudos (bitmap remove_pseudos)
5079 basic_block bb;
5080 int regno, sregno, prev_sregno, dregno, restore_regno;
5081 rtx set, prev_set, prev_insn;
5082 bool change_p, done_p;
5084 change_p = ! bitmap_empty_p (remove_pseudos);
5085 /* We can not finish the function right away if CHANGE_P is true
5086 because we need to marks insns affected by previous
5087 inheritance/split pass for processing by the subsequent
5088 constraint pass. */
5089 FOR_EACH_BB (bb)
5091 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5092 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5093 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5095 if (! INSN_P (curr_insn))
5096 continue;
5097 done_p = false;
5098 sregno = dregno = -1;
5099 if (change_p && NONDEBUG_INSN_P (curr_insn)
5100 && (set = single_set (curr_insn)) != NULL_RTX)
5102 dregno = get_regno (SET_DEST (set));
5103 sregno = get_regno (SET_SRC (set));
5106 if (sregno >= 0 && dregno >= 0)
5108 if ((bitmap_bit_p (remove_pseudos, sregno)
5109 && (lra_reg_info[sregno].restore_regno == dregno
5110 || (bitmap_bit_p (remove_pseudos, dregno)
5111 && (lra_reg_info[sregno].restore_regno
5112 == lra_reg_info[dregno].restore_regno))))
5113 || (bitmap_bit_p (remove_pseudos, dregno)
5114 && lra_reg_info[dregno].restore_regno == sregno))
5115 /* One of the following cases:
5116 original <- removed inheritance pseudo
5117 removed inherit pseudo <- another removed inherit pseudo
5118 removed inherit pseudo <- original pseudo
5120 removed_split_pseudo <- original_reg
5121 original_reg <- removed_split_pseudo */
5123 if (lra_dump_file != NULL)
5125 fprintf (lra_dump_file, " Removing %s:\n",
5126 bitmap_bit_p (&lra_split_regs, sregno)
5127 || bitmap_bit_p (&lra_split_regs, dregno)
5128 ? "split" : "inheritance");
5129 dump_insn_slim (lra_dump_file, curr_insn);
5131 lra_set_insn_deleted (curr_insn);
5132 done_p = true;
5134 else if (bitmap_bit_p (remove_pseudos, sregno)
5135 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5137 /* Search the following pattern:
5138 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5139 original_pseudo <- inherit_or_split_pseudo1
5140 where the 2nd insn is the current insn and
5141 inherit_or_split_pseudo2 is not removed. If it is found,
5142 change the current insn onto:
5143 original_pseudo <- inherit_or_split_pseudo2. */
5144 for (prev_insn = PREV_INSN (curr_insn);
5145 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5146 prev_insn = PREV_INSN (prev_insn))
5148 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5149 && (prev_set = single_set (prev_insn)) != NULL_RTX
5150 /* There should be no subregs in insn we are
5151 searching because only the original reg might
5152 be in subreg when we changed the mode of
5153 load/store for splitting. */
5154 && REG_P (SET_DEST (prev_set))
5155 && REG_P (SET_SRC (prev_set))
5156 && (int) REGNO (SET_DEST (prev_set)) == sregno
5157 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5158 >= FIRST_PSEUDO_REGISTER)
5159 /* As we consider chain of inheritance or
5160 splitting described in above comment we should
5161 check that sregno and prev_sregno were
5162 inheritance/split pseudos created from the
5163 same original regno. */
5164 && (lra_reg_info[sregno].restore_regno
5165 == lra_reg_info[prev_sregno].restore_regno)
5166 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5168 lra_assert (GET_MODE (SET_SRC (prev_set))
5169 == GET_MODE (regno_reg_rtx[sregno]));
5170 if (GET_CODE (SET_SRC (set)) == SUBREG)
5171 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5172 else
5173 SET_SRC (set) = SET_SRC (prev_set);
5174 lra_push_insn_and_update_insn_regno_info (curr_insn);
5175 lra_set_used_insn_alternative_by_uid
5176 (INSN_UID (curr_insn), -1);
5177 done_p = true;
5178 if (lra_dump_file != NULL)
5180 fprintf (lra_dump_file, " Change reload insn:\n");
5181 dump_insn_slim (lra_dump_file, curr_insn);
5186 if (! done_p)
5188 struct lra_insn_reg *reg;
5189 bool restored_regs_p = false;
5190 bool kept_regs_p = false;
5192 curr_id = lra_get_insn_recog_data (curr_insn);
5193 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5195 regno = reg->regno;
5196 restore_regno = lra_reg_info[regno].restore_regno;
5197 if (restore_regno >= 0)
5199 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5201 substitute_pseudo (&curr_insn, regno,
5202 regno_reg_rtx[restore_regno]);
5203 restored_regs_p = true;
5205 else
5206 kept_regs_p = true;
5209 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5211 /* The instruction has changed since the previous
5212 constraints pass. */
5213 lra_push_insn_and_update_insn_regno_info (curr_insn);
5214 lra_set_used_insn_alternative_by_uid
5215 (INSN_UID (curr_insn), -1);
5217 else if (restored_regs_p)
5218 /* The instruction has been restored to the form that
5219 it had during the previous constraints pass. */
5220 lra_update_insn_regno_info (curr_insn);
5221 if (restored_regs_p && lra_dump_file != NULL)
5223 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5224 dump_insn_slim (lra_dump_file, curr_insn);
5229 return change_p;
5232 /* Entry function for undoing inheritance/split transformation. Return true
5233 if we did any RTL change in this pass. */
5234 bool
5235 lra_undo_inheritance (void)
5237 unsigned int regno;
5238 int restore_regno, hard_regno;
5239 int n_all_inherit, n_inherit, n_all_split, n_split;
5240 bitmap_head remove_pseudos;
5241 bitmap_iterator bi;
5242 bool change_p;
5244 lra_undo_inheritance_iter++;
5245 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5246 return false;
5247 if (lra_dump_file != NULL)
5248 fprintf (lra_dump_file,
5249 "\n********** Undoing inheritance #%d: **********\n\n",
5250 lra_undo_inheritance_iter);
5251 bitmap_initialize (&remove_pseudos, &reg_obstack);
5252 n_inherit = n_all_inherit = 0;
5253 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5254 if (lra_reg_info[regno].restore_regno >= 0)
5256 n_all_inherit++;
5257 if (reg_renumber[regno] < 0)
5258 bitmap_set_bit (&remove_pseudos, regno);
5259 else
5260 n_inherit++;
5262 if (lra_dump_file != NULL && n_all_inherit != 0)
5263 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
5264 n_inherit, n_all_inherit,
5265 (double) n_inherit / n_all_inherit * 100);
5266 n_split = n_all_split = 0;
5267 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5268 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
5270 n_all_split++;
5271 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
5272 ? reg_renumber[restore_regno] : restore_regno);
5273 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
5274 bitmap_set_bit (&remove_pseudos, regno);
5275 else
5277 n_split++;
5278 if (lra_dump_file != NULL)
5279 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
5280 regno, restore_regno);
5283 if (lra_dump_file != NULL && n_all_split != 0)
5284 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
5285 n_split, n_all_split,
5286 (double) n_split / n_all_split * 100);
5287 change_p = remove_inheritance_pseudos (&remove_pseudos);
5288 bitmap_clear (&remove_pseudos);
5289 /* Clear restore_regnos. */
5290 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5291 lra_reg_info[regno].restore_regno = -1;
5292 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5293 lra_reg_info[regno].restore_regno = -1;
5294 return change_p;