2013-04-29 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / lra-constraints.c
blobd9e14dc44ce767597b4ed3832c219d4684aa90be
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 /* Number of small register classes used for operands of the best
1052 alternative. */
1053 static int best_small_class_operands_num;
1054 /* Overall number hard registers used for reloads. For example, on
1055 some targets we need 2 general registers to reload DFmode and only
1056 one floating point register. */
1057 static int best_reload_nregs;
1058 /* Overall number reflecting distances of previous reloading the same
1059 value. The distances are counted from the current BB start. It is
1060 used to improve inheritance chances. */
1061 static int best_reload_sum;
1063 /* True if the current insn should have no correspondingly input or
1064 output reloads. */
1065 static bool no_input_reloads_p, no_output_reloads_p;
1067 /* True if we swapped the commutative operands in the current
1068 insn. */
1069 static int curr_swapped;
1071 /* Arrange for address element *LOC to be a register of class CL.
1072 Add any input reloads to list BEFORE. AFTER is nonnull if *LOC is an
1073 automodified value; handle that case by adding the required output
1074 reloads to list AFTER. Return true if the RTL was changed. */
1075 static bool
1076 process_addr_reg (rtx *loc, rtx *before, rtx *after, enum reg_class cl)
1078 int regno;
1079 enum reg_class rclass, new_class;
1080 rtx reg;
1081 rtx new_reg;
1082 enum machine_mode mode;
1083 bool before_p = false;
1085 loc = strip_subreg (loc);
1086 reg = *loc;
1087 mode = GET_MODE (reg);
1088 if (! REG_P (reg))
1090 /* Always reload memory in an address even if the target supports
1091 such addresses. */
1092 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1093 before_p = true;
1095 else
1097 regno = REGNO (reg);
1098 rclass = get_reg_class (regno);
1099 if ((*loc = get_equiv_substitution (reg)) != reg)
1101 if (lra_dump_file != NULL)
1103 fprintf (lra_dump_file,
1104 "Changing pseudo %d in address of insn %u on equiv ",
1105 REGNO (reg), INSN_UID (curr_insn));
1106 dump_value_slim (lra_dump_file, *loc, 1);
1107 fprintf (lra_dump_file, "\n");
1109 *loc = copy_rtx (*loc);
1111 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1113 reg = *loc;
1114 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1115 mode, reg, cl, "address", &new_reg))
1116 before_p = true;
1118 else if (new_class != NO_REGS && rclass != new_class)
1120 change_class (regno, new_class, " Change", true);
1121 return false;
1123 else
1124 return false;
1126 if (before_p)
1128 push_to_sequence (*before);
1129 lra_emit_move (new_reg, reg);
1130 *before = get_insns ();
1131 end_sequence ();
1133 *loc = new_reg;
1134 if (after != NULL)
1136 start_sequence ();
1137 lra_emit_move (reg, new_reg);
1138 emit_insn (*after);
1139 *after = get_insns ();
1140 end_sequence ();
1142 return true;
1145 /* Make reloads for subreg in operand NOP with internal subreg mode
1146 REG_MODE, add new reloads for further processing. Return true if
1147 any reload was generated. */
1148 static bool
1149 simplify_operand_subreg (int nop, enum machine_mode reg_mode)
1151 int hard_regno;
1152 rtx before, after;
1153 enum machine_mode mode;
1154 rtx reg, new_reg;
1155 rtx operand = *curr_id->operand_loc[nop];
1157 before = after = NULL_RTX;
1159 if (GET_CODE (operand) != SUBREG)
1160 return false;
1162 mode = GET_MODE (operand);
1163 reg = SUBREG_REG (operand);
1164 /* If we change address for paradoxical subreg of memory, the
1165 address might violate the necessary alignment or the access might
1166 be slow. So take this into consideration. We should not worry
1167 about access beyond allocated memory for paradoxical memory
1168 subregs as we don't substitute such equiv memory (see processing
1169 equivalences in function lra_constraints) and because for spilled
1170 pseudos we allocate stack memory enough for the biggest
1171 corresponding paradoxical subreg. */
1172 if ((MEM_P (reg)
1173 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1174 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1175 || (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER))
1177 alter_subreg (curr_id->operand_loc[nop], false);
1178 return true;
1180 /* Put constant into memory when we have mixed modes. It generates
1181 a better code in most cases as it does not need a secondary
1182 reload memory. It also prevents LRA looping when LRA is using
1183 secondary reload memory again and again. */
1184 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1185 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1187 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1188 alter_subreg (curr_id->operand_loc[nop], false);
1189 return true;
1191 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1192 if there may be a problem accessing OPERAND in the outer
1193 mode. */
1194 if ((REG_P (reg)
1195 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1196 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1197 /* Don't reload paradoxical subregs because we could be looping
1198 having repeatedly final regno out of hard regs range. */
1199 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1200 >= hard_regno_nregs[hard_regno][mode])
1201 && simplify_subreg_regno (hard_regno, GET_MODE (reg),
1202 SUBREG_BYTE (operand), mode) < 0
1203 /* Don't reload subreg for matching reload. It is actually
1204 valid subreg in LRA. */
1205 && ! LRA_SUBREG_P (operand))
1206 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1208 enum op_type type = curr_static_id->operand[nop].type;
1209 /* The class will be defined later in curr_insn_transform. */
1210 enum reg_class rclass
1211 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1213 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1214 rclass, "subreg reg", &new_reg))
1216 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (new_reg));
1217 if (type != OP_OUT
1218 || GET_MODE_SIZE (GET_MODE (reg)) > GET_MODE_SIZE (mode))
1220 push_to_sequence (before);
1221 lra_emit_move (new_reg, reg);
1222 before = get_insns ();
1223 end_sequence ();
1225 if (type != OP_IN)
1227 start_sequence ();
1228 lra_emit_move (reg, new_reg);
1229 emit_insn (after);
1230 after = get_insns ();
1231 end_sequence ();
1234 SUBREG_REG (operand) = new_reg;
1235 lra_process_new_insns (curr_insn, before, after,
1236 "Inserting subreg reload");
1237 return true;
1239 return false;
1242 /* Return TRUE if X refers for a hard register from SET. */
1243 static bool
1244 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1246 int i, j, x_hard_regno;
1247 enum machine_mode mode;
1248 const char *fmt;
1249 enum rtx_code code;
1251 if (x == NULL_RTX)
1252 return false;
1253 code = GET_CODE (x);
1254 mode = GET_MODE (x);
1255 if (code == SUBREG)
1257 x = SUBREG_REG (x);
1258 code = GET_CODE (x);
1259 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1260 mode = GET_MODE (x);
1263 if (REG_P (x))
1265 x_hard_regno = get_hard_regno (x);
1266 return (x_hard_regno >= 0
1267 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1269 if (MEM_P (x))
1271 struct address_info ad;
1273 decompose_mem_address (&ad, x);
1274 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1275 return true;
1276 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1277 return true;
1279 fmt = GET_RTX_FORMAT (code);
1280 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1282 if (fmt[i] == 'e')
1284 if (uses_hard_regs_p (XEXP (x, i), set))
1285 return true;
1287 else if (fmt[i] == 'E')
1289 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1290 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1291 return true;
1294 return false;
1297 /* Return true if OP is a spilled pseudo. */
1298 static inline bool
1299 spilled_pseudo_p (rtx op)
1301 return (REG_P (op)
1302 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1305 /* Return true if X is a general constant. */
1306 static inline bool
1307 general_constant_p (rtx x)
1309 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1312 static bool
1313 reg_in_class_p (rtx reg, enum reg_class cl)
1315 if (cl == NO_REGS)
1316 return get_reg_class (REGNO (reg)) == NO_REGS;
1317 return in_class_p (reg, cl, NULL);
1320 /* Major function to choose the current insn alternative and what
1321 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1322 negative we should consider only this alternative. Return false if
1323 we can not choose the alternative or find how to reload the
1324 operands. */
1325 static bool
1326 process_alt_operands (int only_alternative)
1328 bool ok_p = false;
1329 int nop, small_class_operands_num, overall, nalt;
1330 int n_alternatives = curr_static_id->n_alternatives;
1331 int n_operands = curr_static_id->n_operands;
1332 /* LOSERS counts the operands that don't fit this alternative and
1333 would require loading. */
1334 int losers;
1335 /* REJECT is a count of how undesirable this alternative says it is
1336 if any reloading is required. If the alternative matches exactly
1337 then REJECT is ignored, but otherwise it gets this much counted
1338 against it in addition to the reloading needed. */
1339 int reject;
1340 /* The number of elements in the following array. */
1341 int early_clobbered_regs_num;
1342 /* Numbers of operands which are early clobber registers. */
1343 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1344 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1345 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1346 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1347 bool curr_alt_win[MAX_RECOG_OPERANDS];
1348 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1349 int curr_alt_matches[MAX_RECOG_OPERANDS];
1350 /* The number of elements in the following array. */
1351 int curr_alt_dont_inherit_ops_num;
1352 /* Numbers of operands whose reload pseudos should not be inherited. */
1353 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1354 rtx op;
1355 /* The register when the operand is a subreg of register, otherwise the
1356 operand itself. */
1357 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1358 /* The register if the operand is a register or subreg of register,
1359 otherwise NULL. */
1360 rtx operand_reg[MAX_RECOG_OPERANDS];
1361 int hard_regno[MAX_RECOG_OPERANDS];
1362 enum machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1363 int reload_nregs, reload_sum;
1364 bool costly_p;
1365 enum reg_class cl;
1367 /* Calculate some data common for all alternatives to speed up the
1368 function. */
1369 for (nop = 0; nop < n_operands; nop++)
1371 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1372 /* The real hard regno of the operand after the allocation. */
1373 hard_regno[nop] = get_hard_regno (op);
1375 operand_reg[nop] = op;
1376 biggest_mode[nop] = GET_MODE (operand_reg[nop]);
1377 if (GET_CODE (operand_reg[nop]) == SUBREG)
1379 operand_reg[nop] = SUBREG_REG (operand_reg[nop]);
1380 if (GET_MODE_SIZE (biggest_mode[nop])
1381 < GET_MODE_SIZE (GET_MODE (operand_reg[nop])))
1382 biggest_mode[nop] = GET_MODE (operand_reg[nop]);
1384 if (REG_P (operand_reg[nop]))
1385 no_subreg_reg_operand[nop] = operand_reg[nop];
1386 else
1387 operand_reg[nop] = NULL_RTX;
1390 /* The constraints are made of several alternatives. Each operand's
1391 constraint looks like foo,bar,... with commas separating the
1392 alternatives. The first alternatives for all operands go
1393 together, the second alternatives go together, etc.
1395 First loop over alternatives. */
1396 for (nalt = 0; nalt < n_alternatives; nalt++)
1398 /* Loop over operands for one constraint alternative. */
1399 #if HAVE_ATTR_enabled
1400 if (curr_id->alternative_enabled_p != NULL
1401 && ! curr_id->alternative_enabled_p[nalt])
1402 continue;
1403 #endif
1405 if (only_alternative >= 0 && nalt != only_alternative)
1406 continue;
1408 overall = losers = reject = reload_nregs = reload_sum = 0;
1409 for (nop = 0; nop < n_operands; nop++)
1410 reject += (curr_static_id
1411 ->operand_alternative[nalt * n_operands + nop].reject);
1412 early_clobbered_regs_num = 0;
1414 for (nop = 0; nop < n_operands; nop++)
1416 const char *p;
1417 char *end;
1418 int len, c, m, i, opalt_num, this_alternative_matches;
1419 bool win, did_match, offmemok, early_clobber_p;
1420 /* false => this operand can be reloaded somehow for this
1421 alternative. */
1422 bool badop;
1423 /* true => this operand can be reloaded if the alternative
1424 allows regs. */
1425 bool winreg;
1426 /* True if a constant forced into memory would be OK for
1427 this operand. */
1428 bool constmemok;
1429 enum reg_class this_alternative, this_costly_alternative;
1430 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1431 bool this_alternative_match_win, this_alternative_win;
1432 bool this_alternative_offmemok;
1433 enum machine_mode mode;
1435 opalt_num = nalt * n_operands + nop;
1436 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1438 /* Fast track for no constraints at all. */
1439 curr_alt[nop] = NO_REGS;
1440 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1441 curr_alt_win[nop] = true;
1442 curr_alt_match_win[nop] = false;
1443 curr_alt_offmemok[nop] = false;
1444 curr_alt_matches[nop] = -1;
1445 continue;
1448 op = no_subreg_reg_operand[nop];
1449 mode = curr_operand_mode[nop];
1451 win = did_match = winreg = offmemok = constmemok = false;
1452 badop = true;
1454 early_clobber_p = false;
1455 p = curr_static_id->operand_alternative[opalt_num].constraint;
1457 this_costly_alternative = this_alternative = NO_REGS;
1458 /* We update set of possible hard regs besides its class
1459 because reg class might be inaccurate. For example,
1460 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1461 is translated in HI_REGS because classes are merged by
1462 pairs and there is no accurate intermediate class. */
1463 CLEAR_HARD_REG_SET (this_alternative_set);
1464 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1465 this_alternative_win = false;
1466 this_alternative_match_win = false;
1467 this_alternative_offmemok = false;
1468 this_alternative_matches = -1;
1470 /* An empty constraint should be excluded by the fast
1471 track. */
1472 lra_assert (*p != 0 && *p != ',');
1474 /* Scan this alternative's specs for this operand; set WIN
1475 if the operand fits any letter in this alternative.
1476 Otherwise, clear BADOP if this operand could fit some
1477 letter after reloads, or set WINREG if this operand could
1478 fit after reloads provided the constraint allows some
1479 registers. */
1480 costly_p = false;
1483 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1485 case '\0':
1486 len = 0;
1487 break;
1488 case ',':
1489 c = '\0';
1490 break;
1492 case '=': case '+': case '?': case '*': case '!':
1493 case ' ': case '\t':
1494 break;
1496 case '%':
1497 /* We only support one commutative marker, the first
1498 one. We already set commutative above. */
1499 break;
1501 case '&':
1502 early_clobber_p = true;
1503 break;
1505 case '#':
1506 /* Ignore rest of this alternative. */
1507 c = '\0';
1508 break;
1510 case '0': case '1': case '2': case '3': case '4':
1511 case '5': case '6': case '7': case '8': case '9':
1513 int m_hregno;
1514 bool match_p;
1516 m = strtoul (p, &end, 10);
1517 p = end;
1518 len = 0;
1519 lra_assert (nop > m);
1521 this_alternative_matches = m;
1522 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1523 /* We are supposed to match a previous operand.
1524 If we do, we win if that one did. If we do
1525 not, count both of the operands as losers.
1526 (This is too conservative, since most of the
1527 time only a single reload insn will be needed
1528 to make the two operands win. As a result,
1529 this alternative may be rejected when it is
1530 actually desirable.) */
1531 match_p = false;
1532 if (operands_match_p (*curr_id->operand_loc[nop],
1533 *curr_id->operand_loc[m], m_hregno))
1535 /* We should reject matching of an early
1536 clobber operand if the matching operand is
1537 not dying in the insn. */
1538 if (! curr_static_id->operand[m].early_clobber
1539 || operand_reg[nop] == NULL_RTX
1540 || (find_regno_note (curr_insn, REG_DEAD,
1541 REGNO (op))
1542 || REGNO (op) == REGNO (operand_reg[m])))
1543 match_p = true;
1545 if (match_p)
1547 /* If we are matching a non-offsettable
1548 address where an offsettable address was
1549 expected, then we must reject this
1550 combination, because we can't reload
1551 it. */
1552 if (curr_alt_offmemok[m]
1553 && MEM_P (*curr_id->operand_loc[m])
1554 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1555 continue;
1558 else
1560 /* Operands don't match. Both operands must
1561 allow a reload register, otherwise we
1562 cannot make them match. */
1563 if (curr_alt[m] == NO_REGS)
1564 break;
1565 /* Retroactively mark the operand we had to
1566 match as a loser, if it wasn't already and
1567 it wasn't matched to a register constraint
1568 (e.g it might be matched by memory). */
1569 if (curr_alt_win[m]
1570 && (operand_reg[m] == NULL_RTX
1571 || hard_regno[m] < 0))
1573 losers++;
1574 reload_nregs
1575 += (ira_reg_class_max_nregs[curr_alt[m]]
1576 [GET_MODE (*curr_id->operand_loc[m])]);
1579 /* We prefer no matching alternatives because
1580 it gives more freedom in RA. */
1581 if (operand_reg[nop] == NULL_RTX
1582 || (find_regno_note (curr_insn, REG_DEAD,
1583 REGNO (operand_reg[nop]))
1584 == NULL_RTX))
1585 reject += 2;
1587 /* If we have to reload this operand and some
1588 previous operand also had to match the same
1589 thing as this operand, we don't know how to do
1590 that. */
1591 if (!match_p || !curr_alt_win[m])
1593 for (i = 0; i < nop; i++)
1594 if (curr_alt_matches[i] == m)
1595 break;
1596 if (i < nop)
1597 break;
1599 else
1600 did_match = true;
1602 /* This can be fixed with reloads if the operand
1603 we are supposed to match can be fixed with
1604 reloads. */
1605 badop = false;
1606 this_alternative = curr_alt[m];
1607 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1608 winreg = this_alternative != NO_REGS;
1609 break;
1612 case 'p':
1613 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1614 ADDRESS, SCRATCH);
1615 this_alternative = reg_class_subunion[this_alternative][cl];
1616 IOR_HARD_REG_SET (this_alternative_set,
1617 reg_class_contents[cl]);
1618 if (costly_p)
1620 this_costly_alternative
1621 = reg_class_subunion[this_costly_alternative][cl];
1622 IOR_HARD_REG_SET (this_costly_alternative_set,
1623 reg_class_contents[cl]);
1625 win = true;
1626 badop = false;
1627 break;
1629 case TARGET_MEM_CONSTRAINT:
1630 if (MEM_P (op) || spilled_pseudo_p (op))
1631 win = true;
1632 /* We can put constant or pseudo value into memory
1633 to satisfy the constraint. */
1634 if (CONST_POOL_OK_P (mode, op) || REG_P (op))
1635 badop = false;
1636 constmemok = true;
1637 break;
1639 case '<':
1640 if (MEM_P (op)
1641 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1642 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1643 win = true;
1644 break;
1646 case '>':
1647 if (MEM_P (op)
1648 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1649 || GET_CODE (XEXP (op, 0)) == POST_INC))
1650 win = true;
1651 break;
1653 /* Memory op whose address is not offsettable. */
1654 case 'V':
1655 if (MEM_P (op)
1656 && ! offsettable_nonstrict_memref_p (op))
1657 win = true;
1658 break;
1660 /* Memory operand whose address is offsettable. */
1661 case 'o':
1662 if ((MEM_P (op)
1663 && offsettable_nonstrict_memref_p (op))
1664 || spilled_pseudo_p (op))
1665 win = true;
1666 /* We can put constant or pseudo value into memory
1667 or make memory address offsetable to satisfy the
1668 constraint. */
1669 if (CONST_POOL_OK_P (mode, op) || MEM_P (op) || REG_P (op))
1670 badop = false;
1671 constmemok = true;
1672 offmemok = true;
1673 break;
1675 case 'E':
1676 case 'F':
1677 if (GET_CODE (op) == CONST_DOUBLE
1678 || (GET_CODE (op) == CONST_VECTOR
1679 && (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)))
1680 win = true;
1681 break;
1683 case 'G':
1684 case 'H':
1685 if (GET_CODE (op) == CONST_DOUBLE
1686 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1687 win = true;
1688 break;
1690 case 's':
1691 if (CONST_INT_P (op)
1692 || (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode))
1693 break;
1695 case 'i':
1696 if (general_constant_p (op))
1697 win = true;
1698 break;
1700 case 'n':
1701 if (CONST_INT_P (op)
1702 || (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode))
1703 win = true;
1704 break;
1706 case 'I':
1707 case 'J':
1708 case 'K':
1709 case 'L':
1710 case 'M':
1711 case 'N':
1712 case 'O':
1713 case 'P':
1714 if (CONST_INT_P (op)
1715 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1716 win = true;
1717 break;
1719 case 'X':
1720 /* This constraint should be excluded by the fast
1721 track. */
1722 gcc_unreachable ();
1723 break;
1725 case 'g':
1726 if (MEM_P (op)
1727 || general_constant_p (op)
1728 || spilled_pseudo_p (op))
1729 win = true;
1730 /* Drop through into 'r' case. */
1732 case 'r':
1733 this_alternative
1734 = reg_class_subunion[this_alternative][GENERAL_REGS];
1735 IOR_HARD_REG_SET (this_alternative_set,
1736 reg_class_contents[GENERAL_REGS]);
1737 if (costly_p)
1739 this_costly_alternative
1740 = (reg_class_subunion
1741 [this_costly_alternative][GENERAL_REGS]);
1742 IOR_HARD_REG_SET (this_costly_alternative_set,
1743 reg_class_contents[GENERAL_REGS]);
1745 goto reg;
1747 default:
1748 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
1750 #ifdef EXTRA_CONSTRAINT_STR
1751 if (EXTRA_MEMORY_CONSTRAINT (c, p))
1753 if (EXTRA_CONSTRAINT_STR (op, c, p))
1754 win = true;
1755 else if (spilled_pseudo_p (op))
1756 win = true;
1758 /* If we didn't already win, we can reload
1759 constants via force_const_mem or put the
1760 pseudo value into memory, or make other
1761 memory by reloading the address like for
1762 'o'. */
1763 if (CONST_POOL_OK_P (mode, op)
1764 || MEM_P (op) || REG_P (op))
1765 badop = false;
1766 constmemok = true;
1767 offmemok = true;
1768 break;
1770 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1772 if (EXTRA_CONSTRAINT_STR (op, c, p))
1773 win = true;
1775 /* If we didn't already win, we can reload
1776 the address into a base register. */
1777 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1778 ADDRESS, SCRATCH);
1779 this_alternative
1780 = reg_class_subunion[this_alternative][cl];
1781 IOR_HARD_REG_SET (this_alternative_set,
1782 reg_class_contents[cl]);
1783 if (costly_p)
1785 this_costly_alternative
1786 = (reg_class_subunion
1787 [this_costly_alternative][cl]);
1788 IOR_HARD_REG_SET (this_costly_alternative_set,
1789 reg_class_contents[cl]);
1791 badop = false;
1792 break;
1795 if (EXTRA_CONSTRAINT_STR (op, c, p))
1796 win = true;
1797 #endif
1798 break;
1801 cl = REG_CLASS_FROM_CONSTRAINT (c, p);
1802 this_alternative = reg_class_subunion[this_alternative][cl];
1803 IOR_HARD_REG_SET (this_alternative_set,
1804 reg_class_contents[cl]);
1805 if (costly_p)
1807 this_costly_alternative
1808 = reg_class_subunion[this_costly_alternative][cl];
1809 IOR_HARD_REG_SET (this_costly_alternative_set,
1810 reg_class_contents[cl]);
1812 reg:
1813 if (mode == BLKmode)
1814 break;
1815 winreg = true;
1816 if (REG_P (op))
1818 if (hard_regno[nop] >= 0
1819 && in_hard_reg_set_p (this_alternative_set,
1820 mode, hard_regno[nop]))
1821 win = true;
1822 else if (hard_regno[nop] < 0
1823 && in_class_p (op, this_alternative, NULL))
1824 win = true;
1826 break;
1828 if (c != ' ' && c != '\t')
1829 costly_p = c == '*';
1831 while ((p += len), c);
1833 /* Record which operands fit this alternative. */
1834 if (win)
1836 this_alternative_win = true;
1837 if (operand_reg[nop] != NULL_RTX)
1839 if (hard_regno[nop] >= 0)
1841 if (in_hard_reg_set_p (this_costly_alternative_set,
1842 mode, hard_regno[nop]))
1843 reject++;
1845 else
1847 /* Prefer won reg to spilled pseudo under other equal
1848 conditions. */
1849 reject++;
1850 if (in_class_p (operand_reg[nop],
1851 this_costly_alternative, NULL))
1852 reject++;
1854 /* We simulate the behaviour of old reload here.
1855 Although scratches need hard registers and it
1856 might result in spilling other pseudos, no reload
1857 insns are generated for the scratches. So it
1858 might cost something but probably less than old
1859 reload pass believes. */
1860 if (lra_former_scratch_p (REGNO (operand_reg[nop])))
1861 reject += LRA_LOSER_COST_FACTOR;
1864 else if (did_match)
1865 this_alternative_match_win = true;
1866 else
1868 int const_to_mem = 0;
1869 bool no_regs_p;
1871 /* If this alternative asks for a specific reg class, see if there
1872 is at least one allocatable register in that class. */
1873 no_regs_p
1874 = (this_alternative == NO_REGS
1875 || (hard_reg_set_subset_p
1876 (reg_class_contents[this_alternative],
1877 lra_no_alloc_regs)));
1879 /* For asms, verify that the class for this alternative is possible
1880 for the mode that is specified. */
1881 if (!no_regs_p && REG_P (op) && INSN_CODE (curr_insn) < 0)
1883 int i;
1884 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1885 if (HARD_REGNO_MODE_OK (i, mode)
1886 && in_hard_reg_set_p (reg_class_contents[this_alternative], mode, i))
1887 break;
1888 if (i == FIRST_PSEUDO_REGISTER)
1889 winreg = false;
1892 /* If this operand accepts a register, and if the
1893 register class has at least one allocatable register,
1894 then this operand can be reloaded. */
1895 if (winreg && !no_regs_p)
1896 badop = false;
1898 if (badop)
1899 goto fail;
1901 this_alternative_offmemok = offmemok;
1902 if (this_costly_alternative != NO_REGS)
1903 reject++;
1904 /* If the operand is dying, has a matching constraint,
1905 and satisfies constraints of the matched operand
1906 which failed to satisfy the own constraints, we do
1907 not need to generate a reload insn for this
1908 operand. */
1909 if (!(this_alternative_matches >= 0
1910 && !curr_alt_win[this_alternative_matches]
1911 && REG_P (op)
1912 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
1913 && (hard_regno[nop] >= 0
1914 ? in_hard_reg_set_p (this_alternative_set,
1915 mode, hard_regno[nop])
1916 : in_class_p (op, this_alternative, NULL))))
1918 /* Strict_low_part requires to reload the register
1919 not the sub-register. In this case we should
1920 check that a final reload hard reg can hold the
1921 value mode. */
1922 if (curr_static_id->operand[nop].strict_low
1923 && REG_P (op)
1924 && hard_regno[nop] < 0
1925 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
1926 && ira_class_hard_regs_num[this_alternative] > 0
1927 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
1928 [this_alternative][0],
1929 GET_MODE (op)))
1930 goto fail;
1931 losers++;
1933 if (operand_reg[nop] != NULL_RTX
1934 /* Output operands and matched input operands are
1935 not inherited. The following conditions do not
1936 exactly describe the previous statement but they
1937 are pretty close. */
1938 && curr_static_id->operand[nop].type != OP_OUT
1939 && (this_alternative_matches < 0
1940 || curr_static_id->operand[nop].type != OP_IN))
1942 int last_reload = (lra_reg_info[ORIGINAL_REGNO
1943 (operand_reg[nop])]
1944 .last_reload);
1946 if (last_reload > bb_reload_num)
1947 reload_sum += last_reload - bb_reload_num;
1949 /* If this is a constant that is reloaded into the
1950 desired class by copying it to memory first, count
1951 that as another reload. This is consistent with
1952 other code and is required to avoid choosing another
1953 alternative when the constant is moved into memory.
1954 Note that the test here is precisely the same as in
1955 the code below that calls force_const_mem. */
1956 if (CONST_POOL_OK_P (mode, op)
1957 && ((targetm.preferred_reload_class
1958 (op, this_alternative) == NO_REGS)
1959 || no_input_reloads_p))
1961 const_to_mem = 1;
1962 if (! no_regs_p)
1963 losers++;
1966 /* Alternative loses if it requires a type of reload not
1967 permitted for this insn. We can always reload
1968 objects with a REG_UNUSED note. */
1969 if ((curr_static_id->operand[nop].type != OP_IN
1970 && no_output_reloads_p
1971 && ! find_reg_note (curr_insn, REG_UNUSED, op))
1972 || (curr_static_id->operand[nop].type != OP_OUT
1973 && no_input_reloads_p && ! const_to_mem))
1974 goto fail;
1976 /* Check strong discouragement of reload of non-constant
1977 into class THIS_ALTERNATIVE. */
1978 if (! CONSTANT_P (op) && ! no_regs_p
1979 && (targetm.preferred_reload_class
1980 (op, this_alternative) == NO_REGS
1981 || (curr_static_id->operand[nop].type == OP_OUT
1982 && (targetm.preferred_output_reload_class
1983 (op, this_alternative) == NO_REGS))))
1984 reject += LRA_MAX_REJECT;
1986 if (MEM_P (op) && offmemok)
1988 /* If we know offset and this non-offsetable memory,
1989 something wrong with this memory and it is better
1990 to try other memory possibilities. */
1991 if (MEM_OFFSET_KNOWN_P (op))
1992 reject += LRA_MAX_REJECT;
1994 else if (! (const_to_mem && constmemok))
1996 /* We prefer to reload pseudos over reloading other
1997 things, since such reloads may be able to be
1998 eliminated later. So bump REJECT in other cases.
1999 Don't do this in the case where we are forcing a
2000 constant into memory and it will then win since
2001 we don't want to have a different alternative
2002 match then. */
2003 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2004 reject += 2;
2006 if (! no_regs_p)
2007 reload_nregs
2008 += ira_reg_class_max_nregs[this_alternative][mode];
2011 /* We are trying to spill pseudo into memory. It is
2012 usually more costly than moving to a hard register
2013 although it might takes the same number of
2014 reloads. */
2015 if (no_regs_p && REG_P (op))
2016 reject += 2;
2018 #ifdef SECONDARY_MEMORY_NEEDED
2019 /* If reload requires moving value through secondary
2020 memory, it will need one more insn at least. */
2021 if (this_alternative != NO_REGS
2022 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2023 && ((curr_static_id->operand[nop].type != OP_OUT
2024 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2025 GET_MODE (op)))
2026 || (curr_static_id->operand[nop].type != OP_IN
2027 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2028 GET_MODE (op)))))
2029 losers++;
2030 #endif
2031 /* Input reloads can be inherited more often than output
2032 reloads can be removed, so penalize output
2033 reloads. */
2034 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2035 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 small_class_operands_num = 0;
2182 for (nop = 0; nop < n_operands; nop++)
2183 small_class_operands_num
2184 += SMALL_REGISTER_CLASS_P (curr_alt[nop]) ? 1 : 0;
2186 if (lra_dump_file != NULL)
2187 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,"
2188 "small_class_ops=%d,rld_nregs=%d\n",
2189 nalt, overall, losers, small_class_operands_num, reload_nregs);
2191 /* If this alternative can be made to work by reloading, and it
2192 needs less reloading than the others checked so far, record
2193 it as the chosen goal for reloading. */
2194 if ((best_losers != 0 && losers == 0)
2195 || (((best_losers == 0 && losers == 0)
2196 || (best_losers != 0 && losers != 0))
2197 && (best_overall > overall
2198 || (best_overall == overall
2199 /* If the cost of the reloads is the same,
2200 prefer alternative which requires minimal
2201 number of small register classes for the
2202 operands. This improves chances of reloads
2203 for insn requiring small register
2204 classes. */
2205 && (small_class_operands_num
2206 < best_small_class_operands_num
2207 || (small_class_operands_num
2208 == best_small_class_operands_num
2209 && (reload_nregs < best_reload_nregs
2210 || (reload_nregs == best_reload_nregs
2211 && best_reload_sum < reload_sum))))))))
2213 for (nop = 0; nop < n_operands; nop++)
2215 goal_alt_win[nop] = curr_alt_win[nop];
2216 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2217 goal_alt_matches[nop] = curr_alt_matches[nop];
2218 goal_alt[nop] = curr_alt[nop];
2219 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2221 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2222 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2223 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2224 goal_alt_swapped = curr_swapped;
2225 best_overall = overall;
2226 best_losers = losers;
2227 best_small_class_operands_num = small_class_operands_num;
2228 best_reload_nregs = reload_nregs;
2229 best_reload_sum = reload_sum;
2230 goal_alt_number = nalt;
2232 if (losers == 0)
2233 /* Everything is satisfied. Do not process alternatives
2234 anymore. */
2235 break;
2236 fail:
2239 return ok_p;
2242 /* Return 1 if ADDR is a valid memory address for mode MODE in address
2243 space AS, and check that each pseudo has the proper kind of hard
2244 reg. */
2245 static int
2246 valid_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2247 rtx addr, addr_space_t as)
2249 #ifdef GO_IF_LEGITIMATE_ADDRESS
2250 lra_assert (ADDR_SPACE_GENERIC_P (as));
2251 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
2252 return 0;
2254 win:
2255 return 1;
2256 #else
2257 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
2258 #endif
2261 /* Return whether address AD is valid. */
2263 static bool
2264 valid_address_p (struct address_info *ad)
2266 /* Some ports do not check displacements for eliminable registers,
2267 so we replace them temporarily with the elimination target. */
2268 rtx saved_base_reg = NULL_RTX;
2269 rtx saved_index_reg = NULL_RTX;
2270 rtx *base_term = strip_subreg (ad->base_term);
2271 rtx *index_term = strip_subreg (ad->index_term);
2272 if (base_term != NULL)
2274 saved_base_reg = *base_term;
2275 lra_eliminate_reg_if_possible (base_term);
2276 if (ad->base_term2 != NULL)
2277 *ad->base_term2 = *ad->base_term;
2279 if (index_term != NULL)
2281 saved_index_reg = *index_term;
2282 lra_eliminate_reg_if_possible (index_term);
2284 bool ok_p = valid_address_p (ad->mode, *ad->outer, ad->as);
2285 if (saved_base_reg != NULL_RTX)
2287 *base_term = saved_base_reg;
2288 if (ad->base_term2 != NULL)
2289 *ad->base_term2 = *ad->base_term;
2291 if (saved_index_reg != NULL_RTX)
2292 *index_term = saved_index_reg;
2293 return ok_p;
2296 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2297 static rtx
2298 base_plus_disp_to_reg (struct address_info *ad)
2300 enum reg_class cl;
2301 rtx new_reg;
2303 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2304 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2305 get_index_code (ad));
2306 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2307 cl, "base + disp");
2308 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2309 return new_reg;
2312 /* Return true if we can add a displacement to address AD, even if that
2313 makes the address invalid. The fix-up code requires any new address
2314 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2315 static bool
2316 can_add_disp_p (struct address_info *ad)
2318 return (!ad->autoinc_p
2319 && ad->segment == NULL
2320 && ad->base == ad->base_term
2321 && ad->disp == ad->disp_term);
2324 /* Make equiv substitution in address AD. Return true if a substitution
2325 was made. */
2326 static bool
2327 equiv_address_substitution (struct address_info *ad)
2329 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2330 HOST_WIDE_INT disp, scale;
2331 bool change_p;
2333 base_term = strip_subreg (ad->base_term);
2334 if (base_term == NULL)
2335 base_reg = new_base_reg = NULL_RTX;
2336 else
2338 base_reg = *base_term;
2339 new_base_reg = get_equiv_substitution (base_reg);
2341 index_term = strip_subreg (ad->index_term);
2342 if (index_term == NULL)
2343 index_reg = new_index_reg = NULL_RTX;
2344 else
2346 index_reg = *index_term;
2347 new_index_reg = get_equiv_substitution (index_reg);
2349 if (base_reg == new_base_reg && index_reg == new_index_reg)
2350 return false;
2351 disp = 0;
2352 change_p = false;
2353 if (lra_dump_file != NULL)
2355 fprintf (lra_dump_file, "Changing address in insn %d ",
2356 INSN_UID (curr_insn));
2357 dump_value_slim (lra_dump_file, *ad->outer, 1);
2359 if (base_reg != new_base_reg)
2361 if (REG_P (new_base_reg))
2363 *base_term = new_base_reg;
2364 change_p = true;
2366 else if (GET_CODE (new_base_reg) == PLUS
2367 && REG_P (XEXP (new_base_reg, 0))
2368 && CONST_INT_P (XEXP (new_base_reg, 1))
2369 && can_add_disp_p (ad))
2371 disp += INTVAL (XEXP (new_base_reg, 1));
2372 *base_term = XEXP (new_base_reg, 0);
2373 change_p = true;
2375 if (ad->base_term2 != NULL)
2376 *ad->base_term2 = *ad->base_term;
2378 if (index_reg != new_index_reg)
2380 if (REG_P (new_index_reg))
2382 *index_term = new_index_reg;
2383 change_p = true;
2385 else if (GET_CODE (new_index_reg) == PLUS
2386 && REG_P (XEXP (new_index_reg, 0))
2387 && CONST_INT_P (XEXP (new_index_reg, 1))
2388 && can_add_disp_p (ad)
2389 && (scale = get_index_scale (ad)))
2391 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2392 *index_term = XEXP (new_index_reg, 0);
2393 change_p = true;
2396 if (disp != 0)
2398 if (ad->disp != NULL)
2399 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2400 else
2402 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2403 update_address (ad);
2405 change_p = true;
2407 if (lra_dump_file != NULL)
2409 if (! change_p)
2410 fprintf (lra_dump_file, " -- no change\n");
2411 else
2413 fprintf (lra_dump_file, " on equiv ");
2414 dump_value_slim (lra_dump_file, *ad->outer, 1);
2415 fprintf (lra_dump_file, "\n");
2418 return change_p;
2421 /* Major function to make reloads for an address in operand NOP.
2422 The supported cases are:
2424 1) an address that existed before LRA started, at which point it must
2425 have been valid. These addresses are subject to elimination and
2426 may have become invalid due to the elimination offset being out
2427 of range.
2429 2) an address created by forcing a constant to memory (force_const_to_mem).
2430 The initial form of these addresses might not be valid, and it is this
2431 function's job to make them valid.
2433 3) a frame address formed from a register and a (possibly zero)
2434 constant offset. As above, these addresses might not be valid
2435 and this function must make them so.
2437 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2438 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2439 address. Return true for any RTL change. */
2440 static bool
2441 process_address (int nop, rtx *before, rtx *after)
2443 struct address_info ad;
2444 rtx new_reg;
2445 rtx op = *curr_id->operand_loc[nop];
2446 const char *constraint = curr_static_id->operand[nop].constraint;
2447 bool change_p;
2449 if (constraint[0] == 'p'
2450 || EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint))
2451 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2452 else if (MEM_P (op))
2453 decompose_mem_address (&ad, op);
2454 else if (GET_CODE (op) == SUBREG
2455 && MEM_P (SUBREG_REG (op)))
2456 decompose_mem_address (&ad, SUBREG_REG (op));
2457 else
2458 return false;
2459 change_p = equiv_address_substitution (&ad);
2460 if (ad.base_term != NULL
2461 && (process_addr_reg
2462 (ad.base_term, before,
2463 (ad.autoinc_p
2464 && !(REG_P (*ad.base_term)
2465 && find_regno_note (curr_insn, REG_DEAD,
2466 REGNO (*ad.base_term)) != NULL_RTX)
2467 ? after : NULL),
2468 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2469 get_index_code (&ad)))))
2471 change_p = true;
2472 if (ad.base_term2 != NULL)
2473 *ad.base_term2 = *ad.base_term;
2475 if (ad.index_term != NULL
2476 && process_addr_reg (ad.index_term, before, NULL, INDEX_REG_CLASS))
2477 change_p = true;
2479 #ifdef EXTRA_CONSTRAINT_STR
2480 /* Target hooks sometimes reject extra constraint addresses -- use
2481 EXTRA_CONSTRAINT_STR for the validation. */
2482 if (constraint[0] != 'p'
2483 && EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint)
2484 && EXTRA_CONSTRAINT_STR (op, constraint[0], constraint))
2485 return change_p;
2486 #endif
2488 /* There are three cases where the shape of *AD.INNER may now be invalid:
2490 1) the original address was valid, but either elimination or
2491 equiv_address_substitution was applied and that made
2492 the address invalid.
2494 2) the address is an invalid symbolic address created by
2495 force_const_to_mem.
2497 3) the address is a frame address with an invalid offset.
2499 All these cases involve a non-autoinc address, so there is no
2500 point revalidating other types. */
2501 if (ad.autoinc_p || valid_address_p (&ad))
2502 return change_p;
2504 /* Any index existed before LRA started, so we can assume that the
2505 presence and shape of the index is valid. */
2506 push_to_sequence (*before);
2507 lra_assert (ad.disp == ad.disp_term);
2508 if (ad.base == NULL)
2510 if (ad.index == NULL)
2512 int code = -1;
2513 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2514 SCRATCH, SCRATCH);
2515 rtx addr = *ad.inner;
2517 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2518 #ifdef HAVE_lo_sum
2520 rtx insn;
2521 rtx last = get_last_insn ();
2523 /* addr => lo_sum (new_base, addr), case (2) above. */
2524 insn = emit_insn (gen_rtx_SET
2525 (VOIDmode, new_reg,
2526 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2527 code = recog_memoized (insn);
2528 if (code >= 0)
2530 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2531 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2533 /* Try to put lo_sum into register. */
2534 insn = emit_insn (gen_rtx_SET
2535 (VOIDmode, new_reg,
2536 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2537 code = recog_memoized (insn);
2538 if (code >= 0)
2540 *ad.inner = new_reg;
2541 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2543 *ad.inner = addr;
2544 code = -1;
2550 if (code < 0)
2551 delete_insns_since (last);
2553 #endif
2554 if (code < 0)
2556 /* addr => new_base, case (2) above. */
2557 lra_emit_move (new_reg, addr);
2558 *ad.inner = new_reg;
2561 else
2563 /* index * scale + disp => new base + index * scale,
2564 case (1) above. */
2565 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
2566 GET_CODE (*ad.index));
2568 lra_assert (INDEX_REG_CLASS != NO_REGS);
2569 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2570 lra_emit_move (new_reg, *ad.disp);
2571 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2572 new_reg, *ad.index);
2575 else if (ad.index == NULL)
2577 /* base + disp => new base, cases (1) and (3) above. */
2578 /* Another option would be to reload the displacement into an
2579 index register. However, postreload has code to optimize
2580 address reloads that have the same base and different
2581 displacements, so reloading into an index register would
2582 not necessarily be a win. */
2583 new_reg = base_plus_disp_to_reg (&ad);
2584 *ad.inner = new_reg;
2586 else
2588 /* base + scale * index + disp => new base + scale * index,
2589 case (1) above. */
2590 new_reg = base_plus_disp_to_reg (&ad);
2591 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2592 new_reg, *ad.index);
2594 *before = get_insns ();
2595 end_sequence ();
2596 return true;
2599 /* Emit insns to reload VALUE into a new register. VALUE is an
2600 auto-increment or auto-decrement RTX whose operand is a register or
2601 memory location; so reloading involves incrementing that location.
2602 IN is either identical to VALUE, or some cheaper place to reload
2603 value being incremented/decremented from.
2605 INC_AMOUNT is the number to increment or decrement by (always
2606 positive and ignored for POST_MODIFY/PRE_MODIFY).
2608 Return pseudo containing the result. */
2609 static rtx
2610 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
2612 /* REG or MEM to be copied and incremented. */
2613 rtx incloc = XEXP (value, 0);
2614 /* Nonzero if increment after copying. */
2615 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
2616 || GET_CODE (value) == POST_MODIFY);
2617 rtx last;
2618 rtx inc;
2619 rtx add_insn;
2620 int code;
2621 rtx real_in = in == value ? incloc : in;
2622 rtx result;
2623 bool plus_p = true;
2625 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
2627 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
2628 || GET_CODE (XEXP (value, 1)) == MINUS);
2629 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
2630 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
2631 inc = XEXP (XEXP (value, 1), 1);
2633 else
2635 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
2636 inc_amount = -inc_amount;
2638 inc = GEN_INT (inc_amount);
2641 if (! post && REG_P (incloc))
2642 result = incloc;
2643 else
2644 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
2645 "INC/DEC result");
2647 if (real_in != result)
2649 /* First copy the location to the result register. */
2650 lra_assert (REG_P (result));
2651 emit_insn (gen_move_insn (result, real_in));
2654 /* We suppose that there are insns to add/sub with the constant
2655 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
2656 old reload worked with this assumption. If the assumption
2657 becomes wrong, we should use approach in function
2658 base_plus_disp_to_reg. */
2659 if (in == value)
2661 /* See if we can directly increment INCLOC. */
2662 last = get_last_insn ();
2663 add_insn = emit_insn (plus_p
2664 ? gen_add2_insn (incloc, inc)
2665 : gen_sub2_insn (incloc, inc));
2667 code = recog_memoized (add_insn);
2668 if (code >= 0)
2670 if (! post && result != incloc)
2671 emit_insn (gen_move_insn (result, incloc));
2672 return result;
2674 delete_insns_since (last);
2677 /* If couldn't do the increment directly, must increment in RESULT.
2678 The way we do this depends on whether this is pre- or
2679 post-increment. For pre-increment, copy INCLOC to the reload
2680 register, increment it there, then save back. */
2681 if (! post)
2683 if (real_in != result)
2684 emit_insn (gen_move_insn (result, real_in));
2685 if (plus_p)
2686 emit_insn (gen_add2_insn (result, inc));
2687 else
2688 emit_insn (gen_sub2_insn (result, inc));
2689 if (result != incloc)
2690 emit_insn (gen_move_insn (incloc, result));
2692 else
2694 /* Post-increment.
2696 Because this might be a jump insn or a compare, and because
2697 RESULT may not be available after the insn in an input
2698 reload, we must do the incrementing before the insn being
2699 reloaded for.
2701 We have already copied IN to RESULT. Increment the copy in
2702 RESULT, save that back, then decrement RESULT so it has
2703 the original value. */
2704 if (plus_p)
2705 emit_insn (gen_add2_insn (result, inc));
2706 else
2707 emit_insn (gen_sub2_insn (result, inc));
2708 emit_insn (gen_move_insn (incloc, result));
2709 /* Restore non-modified value for the result. We prefer this
2710 way because it does not require an additional hard
2711 register. */
2712 if (plus_p)
2714 if (CONST_INT_P (inc))
2715 emit_insn (gen_add2_insn (result, GEN_INT (-INTVAL (inc))));
2716 else
2717 emit_insn (gen_sub2_insn (result, inc));
2719 else
2720 emit_insn (gen_add2_insn (result, inc));
2722 return result;
2725 /* Return true if the current move insn does not need processing as we
2726 already know that it satisfies its constraints. */
2727 static bool
2728 simple_move_p (void)
2730 rtx dest, src;
2731 enum reg_class dclass, sclass;
2733 lra_assert (curr_insn_set != NULL_RTX);
2734 dest = SET_DEST (curr_insn_set);
2735 src = SET_SRC (curr_insn_set);
2736 return ((dclass = get_op_class (dest)) != NO_REGS
2737 && (sclass = get_op_class (src)) != NO_REGS
2738 /* The backend guarantees that register moves of cost 2
2739 never need reloads. */
2740 && targetm.register_move_cost (GET_MODE (src), dclass, sclass) == 2);
2743 /* Swap operands NOP and NOP + 1. */
2744 static inline void
2745 swap_operands (int nop)
2747 enum machine_mode mode = curr_operand_mode[nop];
2748 curr_operand_mode[nop] = curr_operand_mode[nop + 1];
2749 curr_operand_mode[nop + 1] = mode;
2750 rtx x = *curr_id->operand_loc[nop];
2751 *curr_id->operand_loc[nop] = *curr_id->operand_loc[nop + 1];
2752 *curr_id->operand_loc[nop + 1] = x;
2753 /* Swap the duplicates too. */
2754 lra_update_dup (curr_id, nop);
2755 lra_update_dup (curr_id, nop + 1);
2758 /* Main entry point of the constraint code: search the body of the
2759 current insn to choose the best alternative. It is mimicking insn
2760 alternative cost calculation model of former reload pass. That is
2761 because machine descriptions were written to use this model. This
2762 model can be changed in future. Make commutative operand exchange
2763 if it is chosen.
2765 Return true if some RTL changes happened during function call. */
2766 static bool
2767 curr_insn_transform (void)
2769 int i, j, k;
2770 int n_operands;
2771 int n_alternatives;
2772 int commutative;
2773 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
2774 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
2775 rtx before, after;
2776 bool alt_p = false;
2777 /* Flag that the insn has been changed through a transformation. */
2778 bool change_p;
2779 bool sec_mem_p;
2780 #ifdef SECONDARY_MEMORY_NEEDED
2781 bool use_sec_mem_p;
2782 #endif
2783 int max_regno_before;
2784 int reused_alternative_num;
2786 curr_insn_set = single_set (curr_insn);
2787 if (curr_insn_set != NULL_RTX && simple_move_p ())
2788 return false;
2790 no_input_reloads_p = no_output_reloads_p = false;
2791 goal_alt_number = -1;
2792 change_p = sec_mem_p = false;
2793 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
2794 reloads; neither are insns that SET cc0. Insns that use CC0 are
2795 not allowed to have any input reloads. */
2796 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
2797 no_output_reloads_p = true;
2799 #ifdef HAVE_cc0
2800 if (reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
2801 no_input_reloads_p = true;
2802 if (reg_set_p (cc0_rtx, PATTERN (curr_insn)))
2803 no_output_reloads_p = true;
2804 #endif
2806 n_operands = curr_static_id->n_operands;
2807 n_alternatives = curr_static_id->n_alternatives;
2809 /* Just return "no reloads" if insn has no operands with
2810 constraints. */
2811 if (n_operands == 0 || n_alternatives == 0)
2812 return false;
2814 max_regno_before = max_reg_num ();
2816 for (i = 0; i < n_operands; i++)
2818 goal_alt_matched[i][0] = -1;
2819 goal_alt_matches[i] = -1;
2822 commutative = curr_static_id->commutative;
2824 /* Now see what we need for pseudos that didn't get hard regs or got
2825 the wrong kind of hard reg. For this, we must consider all the
2826 operands together against the register constraints. */
2828 best_losers = best_overall = INT_MAX;
2829 best_small_class_operands_num = best_reload_sum = 0;
2831 curr_swapped = false;
2832 goal_alt_swapped = false;
2834 /* Make equivalence substitution and memory subreg elimination
2835 before address processing because an address legitimacy can
2836 depend on memory mode. */
2837 for (i = 0; i < n_operands; i++)
2839 rtx op = *curr_id->operand_loc[i];
2840 rtx subst, old = op;
2841 bool op_change_p = false;
2843 if (GET_CODE (old) == SUBREG)
2844 old = SUBREG_REG (old);
2845 subst = get_equiv_substitution (old);
2846 if (subst != old)
2848 subst = copy_rtx (subst);
2849 lra_assert (REG_P (old));
2850 if (GET_CODE (op) == SUBREG)
2851 SUBREG_REG (op) = subst;
2852 else
2853 *curr_id->operand_loc[i] = subst;
2854 if (lra_dump_file != NULL)
2856 fprintf (lra_dump_file,
2857 "Changing pseudo %d in operand %i of insn %u on equiv ",
2858 REGNO (old), i, INSN_UID (curr_insn));
2859 dump_value_slim (lra_dump_file, subst, 1);
2860 fprintf (lra_dump_file, "\n");
2862 op_change_p = change_p = true;
2864 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
2866 change_p = true;
2867 lra_update_dup (curr_id, i);
2871 /* Reload address registers and displacements. We do it before
2872 finding an alternative because of memory constraints. */
2873 before = after = NULL_RTX;
2874 for (i = 0; i < n_operands; i++)
2875 if (! curr_static_id->operand[i].is_operator
2876 && process_address (i, &before, &after))
2878 change_p = true;
2879 lra_update_dup (curr_id, i);
2882 if (change_p)
2883 /* If we've changed the instruction then any alternative that
2884 we chose previously may no longer be valid. */
2885 lra_set_used_insn_alternative (curr_insn, -1);
2887 if (curr_insn_set != NULL_RTX
2888 && check_and_process_move (&change_p, &sec_mem_p))
2889 return change_p;
2891 try_swapped:
2893 reused_alternative_num = curr_id->used_insn_alternative;
2894 if (lra_dump_file != NULL && reused_alternative_num >= 0)
2895 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
2896 reused_alternative_num, INSN_UID (curr_insn));
2898 if (process_alt_operands (reused_alternative_num))
2899 alt_p = true;
2901 /* If insn is commutative (it's safe to exchange a certain pair of
2902 operands) then we need to try each alternative twice, the second
2903 time matching those two operands as if we had exchanged them. To
2904 do this, really exchange them in operands.
2906 If we have just tried the alternatives the second time, return
2907 operands to normal and drop through. */
2909 if (reused_alternative_num < 0 && commutative >= 0)
2911 curr_swapped = !curr_swapped;
2912 if (curr_swapped)
2914 swap_operands (commutative);
2915 goto try_swapped;
2917 else
2918 swap_operands (commutative);
2921 if (! alt_p && ! sec_mem_p)
2923 /* No alternative works with reloads?? */
2924 if (INSN_CODE (curr_insn) >= 0)
2925 fatal_insn ("unable to generate reloads for:", curr_insn);
2926 error_for_asm (curr_insn,
2927 "inconsistent operand constraints in an %<asm%>");
2928 /* Avoid further trouble with this insn. */
2929 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
2930 lra_invalidate_insn_data (curr_insn);
2931 return true;
2934 /* If the best alternative is with operands 1 and 2 swapped, swap
2935 them. Update the operand numbers of any reloads already
2936 pushed. */
2938 if (goal_alt_swapped)
2940 if (lra_dump_file != NULL)
2941 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
2942 INSN_UID (curr_insn));
2944 /* Swap the duplicates too. */
2945 swap_operands (commutative);
2946 change_p = true;
2949 #ifdef SECONDARY_MEMORY_NEEDED
2950 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
2951 too conservatively. So we use the secondary memory only if there
2952 is no any alternative without reloads. */
2953 use_sec_mem_p = false;
2954 if (! alt_p)
2955 use_sec_mem_p = true;
2956 else if (sec_mem_p)
2958 for (i = 0; i < n_operands; i++)
2959 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
2960 break;
2961 use_sec_mem_p = i < n_operands;
2964 if (use_sec_mem_p)
2966 rtx new_reg, src, dest, rld;
2967 enum machine_mode sec_mode, rld_mode;
2969 lra_assert (sec_mem_p);
2970 lra_assert (curr_static_id->operand[0].type == OP_OUT
2971 && curr_static_id->operand[1].type == OP_IN);
2972 dest = *curr_id->operand_loc[0];
2973 src = *curr_id->operand_loc[1];
2974 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
2975 ? dest : src);
2976 rld_mode = GET_MODE (rld);
2977 #ifdef SECONDARY_MEMORY_NEEDED_MODE
2978 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
2979 #else
2980 sec_mode = rld_mode;
2981 #endif
2982 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
2983 NO_REGS, "secondary");
2984 /* If the mode is changed, it should be wider. */
2985 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
2986 if (sec_mode != rld_mode)
2988 /* If the target says specifically to use another mode for
2989 secondary memory moves we can not reuse the original
2990 insn. */
2991 after = emit_spill_move (false, new_reg, dest);
2992 lra_process_new_insns (curr_insn, NULL_RTX, after,
2993 "Inserting the sec. move");
2994 before = emit_spill_move (true, new_reg, src);
2995 lra_process_new_insns (curr_insn, before, NULL_RTX, "Changing on");
2996 lra_set_insn_deleted (curr_insn);
2998 else if (dest == rld)
3000 *curr_id->operand_loc[0] = new_reg;
3001 after = emit_spill_move (false, new_reg, dest);
3002 lra_process_new_insns (curr_insn, NULL_RTX, after,
3003 "Inserting the sec. move");
3005 else
3007 *curr_id->operand_loc[1] = new_reg;
3008 before = emit_spill_move (true, new_reg, src);
3009 lra_process_new_insns (curr_insn, before, NULL_RTX,
3010 "Inserting the sec. move");
3012 lra_update_insn_regno_info (curr_insn);
3013 return true;
3015 #endif
3017 lra_assert (goal_alt_number >= 0);
3018 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3020 if (lra_dump_file != NULL)
3022 const char *p;
3024 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3025 goal_alt_number, INSN_UID (curr_insn));
3026 for (i = 0; i < n_operands; i++)
3028 p = (curr_static_id->operand_alternative
3029 [goal_alt_number * n_operands + i].constraint);
3030 if (*p == '\0')
3031 continue;
3032 fprintf (lra_dump_file, " (%d) ", i);
3033 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3034 fputc (*p, lra_dump_file);
3036 fprintf (lra_dump_file, "\n");
3039 /* Right now, for any pair of operands I and J that are required to
3040 match, with J < I, goal_alt_matches[I] is J. Add I to
3041 goal_alt_matched[J]. */
3043 for (i = 0; i < n_operands; i++)
3044 if ((j = goal_alt_matches[i]) >= 0)
3046 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3048 /* We allow matching one output operand and several input
3049 operands. */
3050 lra_assert (k == 0
3051 || (curr_static_id->operand[j].type == OP_OUT
3052 && curr_static_id->operand[i].type == OP_IN
3053 && (curr_static_id->operand
3054 [goal_alt_matched[j][0]].type == OP_IN)));
3055 goal_alt_matched[j][k] = i;
3056 goal_alt_matched[j][k + 1] = -1;
3059 for (i = 0; i < n_operands; i++)
3060 goal_alt_win[i] |= goal_alt_match_win[i];
3062 /* Any constants that aren't allowed and can't be reloaded into
3063 registers are here changed into memory references. */
3064 for (i = 0; i < n_operands; i++)
3065 if (goal_alt_win[i])
3067 int regno;
3068 enum reg_class new_class;
3069 rtx reg = *curr_id->operand_loc[i];
3071 if (GET_CODE (reg) == SUBREG)
3072 reg = SUBREG_REG (reg);
3074 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3076 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3078 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3080 lra_assert (ok_p);
3081 change_class (regno, new_class, " Change", true);
3085 else
3087 const char *constraint;
3088 char c;
3089 rtx op = *curr_id->operand_loc[i];
3090 rtx subreg = NULL_RTX;
3091 enum machine_mode mode = curr_operand_mode[i];
3093 if (GET_CODE (op) == SUBREG)
3095 subreg = op;
3096 op = SUBREG_REG (op);
3097 mode = GET_MODE (op);
3100 if (CONST_POOL_OK_P (mode, op)
3101 && ((targetm.preferred_reload_class
3102 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3103 || no_input_reloads_p))
3105 rtx tem = force_const_mem (mode, op);
3107 change_p = true;
3108 if (subreg != NULL_RTX)
3109 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3111 *curr_id->operand_loc[i] = tem;
3112 lra_update_dup (curr_id, i);
3113 process_address (i, &before, &after);
3115 /* If the alternative accepts constant pool refs directly
3116 there will be no reload needed at all. */
3117 if (subreg != NULL_RTX)
3118 continue;
3119 /* Skip alternatives before the one requested. */
3120 constraint = (curr_static_id->operand_alternative
3121 [goal_alt_number * n_operands + i].constraint);
3122 for (;
3123 (c = *constraint) && c != ',' && c != '#';
3124 constraint += CONSTRAINT_LEN (c, constraint))
3126 if (c == TARGET_MEM_CONSTRAINT || c == 'o')
3127 break;
3128 #ifdef EXTRA_CONSTRAINT_STR
3129 if (EXTRA_MEMORY_CONSTRAINT (c, constraint)
3130 && EXTRA_CONSTRAINT_STR (tem, c, constraint))
3131 break;
3132 #endif
3134 if (c == '\0' || c == ',' || c == '#')
3135 continue;
3137 goal_alt_win[i] = true;
3141 for (i = 0; i < n_operands; i++)
3143 rtx old, new_reg;
3144 rtx op = *curr_id->operand_loc[i];
3146 if (goal_alt_win[i])
3148 if (goal_alt[i] == NO_REGS
3149 && REG_P (op)
3150 /* When we assign NO_REGS it means that we will not
3151 assign a hard register to the scratch pseudo by
3152 assigment pass and the scratch pseudo will be
3153 spilled. Spilled scratch pseudos are transformed
3154 back to scratches at the LRA end. */
3155 && lra_former_scratch_operand_p (curr_insn, i))
3157 int regno = REGNO (op);
3158 change_class (regno, NO_REGS, " Change", true);
3159 if (lra_get_regno_hard_regno (regno) >= 0)
3160 /* We don't have to mark all insn affected by the
3161 spilled pseudo as there is only one such insn, the
3162 current one. */
3163 reg_renumber[regno] = -1;
3165 continue;
3168 /* Operands that match previous ones have already been handled. */
3169 if (goal_alt_matches[i] >= 0)
3170 continue;
3172 /* We should not have an operand with a non-offsettable address
3173 appearing where an offsettable address will do. It also may
3174 be a case when the address should be special in other words
3175 not a general one (e.g. it needs no index reg). */
3176 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3178 enum reg_class rclass;
3179 rtx *loc = &XEXP (op, 0);
3180 enum rtx_code code = GET_CODE (*loc);
3182 push_to_sequence (before);
3183 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3184 MEM, SCRATCH);
3185 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3186 new_reg = emit_inc (rclass, *loc, *loc,
3187 /* This value does not matter for MODIFY. */
3188 GET_MODE_SIZE (GET_MODE (op)));
3189 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass,
3190 "offsetable address", &new_reg))
3191 lra_emit_move (new_reg, *loc);
3192 before = get_insns ();
3193 end_sequence ();
3194 *loc = new_reg;
3195 lra_update_dup (curr_id, i);
3197 else if (goal_alt_matched[i][0] == -1)
3199 enum machine_mode mode;
3200 rtx reg, *loc;
3201 int hard_regno, byte;
3202 enum op_type type = curr_static_id->operand[i].type;
3204 loc = curr_id->operand_loc[i];
3205 mode = curr_operand_mode[i];
3206 if (GET_CODE (*loc) == SUBREG)
3208 reg = SUBREG_REG (*loc);
3209 byte = SUBREG_BYTE (*loc);
3210 if (REG_P (reg)
3211 /* Strict_low_part requires reload the register not
3212 the sub-register. */
3213 && (curr_static_id->operand[i].strict_low
3214 || (GET_MODE_SIZE (mode)
3215 <= GET_MODE_SIZE (GET_MODE (reg))
3216 && (hard_regno
3217 = get_try_hard_regno (REGNO (reg))) >= 0
3218 && (simplify_subreg_regno
3219 (hard_regno,
3220 GET_MODE (reg), byte, mode) < 0)
3221 && (goal_alt[i] == NO_REGS
3222 || (simplify_subreg_regno
3223 (ira_class_hard_regs[goal_alt[i]][0],
3224 GET_MODE (reg), byte, mode) >= 0)))))
3226 loc = &SUBREG_REG (*loc);
3227 mode = GET_MODE (*loc);
3230 old = *loc;
3231 if (get_reload_reg (type, mode, old, goal_alt[i], "", &new_reg)
3232 && type != OP_OUT)
3234 push_to_sequence (before);
3235 lra_emit_move (new_reg, old);
3236 before = get_insns ();
3237 end_sequence ();
3239 *loc = new_reg;
3240 if (type != OP_IN
3241 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3243 start_sequence ();
3244 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3245 emit_insn (after);
3246 after = get_insns ();
3247 end_sequence ();
3248 *loc = new_reg;
3250 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3251 if (goal_alt_dont_inherit_ops[j] == i)
3253 lra_set_regno_unique_value (REGNO (new_reg));
3254 break;
3256 lra_update_dup (curr_id, i);
3258 else if (curr_static_id->operand[i].type == OP_IN
3259 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3260 == OP_OUT))
3262 /* generate reloads for input and matched outputs. */
3263 match_inputs[0] = i;
3264 match_inputs[1] = -1;
3265 match_reload (goal_alt_matched[i][0], match_inputs,
3266 goal_alt[i], &before, &after);
3268 else if (curr_static_id->operand[i].type == OP_OUT
3269 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3270 == OP_IN))
3271 /* Generate reloads for output and matched inputs. */
3272 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3273 else if (curr_static_id->operand[i].type == OP_IN
3274 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3275 == OP_IN))
3277 /* Generate reloads for matched inputs. */
3278 match_inputs[0] = i;
3279 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3280 match_inputs[j + 1] = k;
3281 match_inputs[j + 1] = -1;
3282 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3284 else
3285 /* We must generate code in any case when function
3286 process_alt_operands decides that it is possible. */
3287 gcc_unreachable ();
3289 if (before != NULL_RTX || after != NULL_RTX
3290 || max_regno_before != max_reg_num ())
3291 change_p = true;
3292 if (change_p)
3294 lra_update_operator_dups (curr_id);
3295 /* Something changes -- process the insn. */
3296 lra_update_insn_regno_info (curr_insn);
3298 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3299 return change_p;
3302 /* Return true if X is in LIST. */
3303 static bool
3304 in_list_p (rtx x, rtx list)
3306 for (; list != NULL_RTX; list = XEXP (list, 1))
3307 if (XEXP (list, 0) == x)
3308 return true;
3309 return false;
3312 /* Return true if X contains an allocatable hard register (if
3313 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3314 static bool
3315 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3317 int i, j;
3318 const char *fmt;
3319 enum rtx_code code;
3321 code = GET_CODE (x);
3322 if (REG_P (x))
3324 int regno = REGNO (x);
3325 HARD_REG_SET alloc_regs;
3327 if (hard_reg_p)
3329 if (regno >= FIRST_PSEUDO_REGISTER)
3330 regno = lra_get_regno_hard_regno (regno);
3331 if (regno < 0)
3332 return false;
3333 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3334 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3336 else
3338 if (regno < FIRST_PSEUDO_REGISTER)
3339 return false;
3340 if (! spilled_p)
3341 return true;
3342 return lra_get_regno_hard_regno (regno) < 0;
3345 fmt = GET_RTX_FORMAT (code);
3346 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3348 if (fmt[i] == 'e')
3350 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3351 return true;
3353 else if (fmt[i] == 'E')
3355 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3356 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3357 return true;
3360 return false;
3363 /* Process all regs in location *LOC and change them on equivalent
3364 substitution. Return true if any change was done. */
3365 static bool
3366 loc_equivalence_change_p (rtx *loc)
3368 rtx subst, reg, x = *loc;
3369 bool result = false;
3370 enum rtx_code code = GET_CODE (x);
3371 const char *fmt;
3372 int i, j;
3374 if (code == SUBREG)
3376 reg = SUBREG_REG (x);
3377 if ((subst = get_equiv_substitution (reg)) != reg
3378 && GET_MODE (subst) == VOIDmode)
3380 /* We cannot reload debug location. Simplify subreg here
3381 while we know the inner mode. */
3382 *loc = simplify_gen_subreg (GET_MODE (x), subst,
3383 GET_MODE (reg), SUBREG_BYTE (x));
3384 return true;
3387 if (code == REG && (subst = get_equiv_substitution (x)) != x)
3389 *loc = subst;
3390 return true;
3393 /* Scan all the operand sub-expressions. */
3394 fmt = GET_RTX_FORMAT (code);
3395 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3397 if (fmt[i] == 'e')
3398 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
3399 else if (fmt[i] == 'E')
3400 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3401 result
3402 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
3404 return result;
3407 /* Similar to loc_equivalence_change_p, but for use as
3408 simplify_replace_fn_rtx callback. */
3409 static rtx
3410 loc_equivalence_callback (rtx loc, const_rtx, void *)
3412 if (!REG_P (loc))
3413 return NULL_RTX;
3415 rtx subst = get_equiv_substitution (loc);
3416 if (subst != loc)
3417 return subst;
3419 return NULL_RTX;
3422 /* Maximum number of generated reload insns per an insn. It is for
3423 preventing this pass cycling in a bug case. */
3424 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
3426 /* The current iteration number of this LRA pass. */
3427 int lra_constraint_iter;
3429 /* The current iteration number of this LRA pass after the last spill
3430 pass. */
3431 int lra_constraint_iter_after_spill;
3433 /* True if we substituted equiv which needs checking register
3434 allocation correctness because the equivalent value contains
3435 allocatable hard registers or when we restore multi-register
3436 pseudo. */
3437 bool lra_risky_transformations_p;
3439 /* Return true if REGNO is referenced in more than one block. */
3440 static bool
3441 multi_block_pseudo_p (int regno)
3443 basic_block bb = NULL;
3444 unsigned int uid;
3445 bitmap_iterator bi;
3447 if (regno < FIRST_PSEUDO_REGISTER)
3448 return false;
3450 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
3451 if (bb == NULL)
3452 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
3453 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
3454 return true;
3455 return false;
3458 /* Return true if LIST contains a deleted insn. */
3459 static bool
3460 contains_deleted_insn_p (rtx list)
3462 for (; list != NULL_RTX; list = XEXP (list, 1))
3463 if (NOTE_P (XEXP (list, 0))
3464 && NOTE_KIND (XEXP (list, 0)) == NOTE_INSN_DELETED)
3465 return true;
3466 return false;
3469 /* Return true if X contains a pseudo dying in INSN. */
3470 static bool
3471 dead_pseudo_p (rtx x, rtx insn)
3473 int i, j;
3474 const char *fmt;
3475 enum rtx_code code;
3477 if (REG_P (x))
3478 return (insn != NULL_RTX
3479 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
3480 code = GET_CODE (x);
3481 fmt = GET_RTX_FORMAT (code);
3482 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3484 if (fmt[i] == 'e')
3486 if (dead_pseudo_p (XEXP (x, i), insn))
3487 return true;
3489 else if (fmt[i] == 'E')
3491 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3492 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
3493 return true;
3496 return false;
3499 /* Return true if INSN contains a dying pseudo in INSN right hand
3500 side. */
3501 static bool
3502 insn_rhs_dead_pseudo_p (rtx insn)
3504 rtx set = single_set (insn);
3506 gcc_assert (set != NULL);
3507 return dead_pseudo_p (SET_SRC (set), insn);
3510 /* Return true if any init insn of REGNO contains a dying pseudo in
3511 insn right hand side. */
3512 static bool
3513 init_insn_rhs_dead_pseudo_p (int regno)
3515 rtx insns = ira_reg_equiv[regno].init_insns;
3517 if (insns == NULL)
3518 return false;
3519 if (INSN_P (insns))
3520 return insn_rhs_dead_pseudo_p (insns);
3521 for (; insns != NULL_RTX; insns = XEXP (insns, 1))
3522 if (insn_rhs_dead_pseudo_p (XEXP (insns, 0)))
3523 return true;
3524 return false;
3527 /* Entry function of LRA constraint pass. Return true if the
3528 constraint pass did change the code. */
3529 bool
3530 lra_constraints (bool first_p)
3532 bool changed_p;
3533 int i, hard_regno, new_insns_num;
3534 unsigned int min_len, new_min_len, uid;
3535 rtx set, x, reg, dest_reg;
3536 basic_block last_bb;
3537 bitmap_head equiv_insn_bitmap;
3538 bitmap_iterator bi;
3540 lra_constraint_iter++;
3541 if (lra_dump_file != NULL)
3542 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
3543 lra_constraint_iter);
3544 lra_constraint_iter_after_spill++;
3545 if (lra_constraint_iter_after_spill > LRA_MAX_CONSTRAINT_ITERATION_NUMBER)
3546 internal_error
3547 ("Maximum number of LRA constraint passes is achieved (%d)\n",
3548 LRA_MAX_CONSTRAINT_ITERATION_NUMBER);
3549 changed_p = false;
3550 lra_risky_transformations_p = false;
3551 new_insn_uid_start = get_max_uid ();
3552 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
3553 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
3554 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3555 if (lra_reg_info[i].nrefs != 0)
3557 ira_reg_equiv[i].profitable_p = true;
3558 reg = regno_reg_rtx[i];
3559 if ((hard_regno = lra_get_regno_hard_regno (i)) >= 0)
3561 int j, nregs;
3563 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
3564 for (j = 0; j < nregs; j++)
3565 df_set_regs_ever_live (hard_regno + j, true);
3567 else if ((x = get_equiv_substitution (reg)) != reg)
3569 bool pseudo_p = contains_reg_p (x, false, false);
3570 rtx set, insn;
3572 /* After RTL transformation, we can not guarantee that
3573 pseudo in the substitution was not reloaded which might
3574 make equivalence invalid. For example, in reverse
3575 equiv of p0
3577 p0 <- ...
3579 equiv_mem <- p0
3581 the memory address register was reloaded before the 2nd
3582 insn. */
3583 if ((! first_p && pseudo_p)
3584 /* We don't use DF for compilation speed sake. So it
3585 is problematic to update live info when we use an
3586 equivalence containing pseudos in more than one
3587 BB. */
3588 || (pseudo_p && multi_block_pseudo_p (i))
3589 /* If an init insn was deleted for some reason, cancel
3590 the equiv. We could update the equiv insns after
3591 transformations including an equiv insn deletion
3592 but it is not worthy as such cases are extremely
3593 rare. */
3594 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
3595 /* If it is not a reverse equivalence, we check that a
3596 pseudo in rhs of the init insn is not dying in the
3597 insn. Otherwise, the live info at the beginning of
3598 the corresponding BB might be wrong after we
3599 removed the insn. When the equiv can be a
3600 constant, the right hand side of the init insn can
3601 be a pseudo. */
3602 || (! ((insn = ira_reg_equiv[i].init_insns) != NULL_RTX
3603 && INSN_P (insn)
3604 && (set = single_set (insn)) != NULL_RTX
3605 && REG_P (SET_DEST (set))
3606 && (int) REGNO (SET_DEST (set)) == i)
3607 && init_insn_rhs_dead_pseudo_p (i))
3608 /* Prevent access beyond equivalent memory for
3609 paradoxical subregs. */
3610 || (MEM_P (x)
3611 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
3612 > GET_MODE_SIZE (GET_MODE (x)))))
3613 ira_reg_equiv[i].defined_p = false;
3614 if (contains_reg_p (x, false, true))
3615 ira_reg_equiv[i].profitable_p = false;
3616 if (get_equiv_substitution (reg) != reg)
3617 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
3620 /* We should add all insns containing pseudos which should be
3621 substituted by their equivalences. */
3622 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
3623 lra_push_insn_by_uid (uid);
3624 lra_eliminate (false);
3625 min_len = lra_insn_stack_length ();
3626 new_insns_num = 0;
3627 last_bb = NULL;
3628 changed_p = false;
3629 while ((new_min_len = lra_insn_stack_length ()) != 0)
3631 curr_insn = lra_pop_insn ();
3632 --new_min_len;
3633 curr_bb = BLOCK_FOR_INSN (curr_insn);
3634 if (curr_bb != last_bb)
3636 last_bb = curr_bb;
3637 bb_reload_num = lra_curr_reload_num;
3639 if (min_len > new_min_len)
3641 min_len = new_min_len;
3642 new_insns_num = 0;
3644 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
3645 internal_error
3646 ("Max. number of generated reload insns per insn is achieved (%d)\n",
3647 MAX_RELOAD_INSNS_NUMBER);
3648 new_insns_num++;
3649 if (DEBUG_INSN_P (curr_insn))
3651 /* We need to check equivalence in debug insn and change
3652 pseudo to the equivalent value if necessary. */
3653 curr_id = lra_get_insn_recog_data (curr_insn);
3654 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
3656 rtx old = *curr_id->operand_loc[0];
3657 *curr_id->operand_loc[0]
3658 = simplify_replace_fn_rtx (old, NULL_RTX,
3659 loc_equivalence_callback, NULL);
3660 if (old != *curr_id->operand_loc[0])
3662 lra_update_insn_regno_info (curr_insn);
3663 changed_p = true;
3667 else if (INSN_P (curr_insn))
3669 if ((set = single_set (curr_insn)) != NULL_RTX)
3671 dest_reg = SET_DEST (set);
3672 /* The equivalence pseudo could be set up as SUBREG in a
3673 case when it is a call restore insn in a mode
3674 different from the pseudo mode. */
3675 if (GET_CODE (dest_reg) == SUBREG)
3676 dest_reg = SUBREG_REG (dest_reg);
3677 if ((REG_P (dest_reg)
3678 && (x = get_equiv_substitution (dest_reg)) != dest_reg
3679 /* Remove insns which set up a pseudo whose value
3680 can not be changed. Such insns might be not in
3681 init_insns because we don't update equiv data
3682 during insn transformations.
3684 As an example, let suppose that a pseudo got
3685 hard register and on the 1st pass was not
3686 changed to equivalent constant. We generate an
3687 additional insn setting up the pseudo because of
3688 secondary memory movement. Then the pseudo is
3689 spilled and we use the equiv constant. In this
3690 case we should remove the additional insn and
3691 this insn is not init_insns list. */
3692 && (! MEM_P (x) || MEM_READONLY_P (x)
3693 || in_list_p (curr_insn,
3694 ira_reg_equiv
3695 [REGNO (dest_reg)].init_insns)))
3696 || (((x = get_equiv_substitution (SET_SRC (set)))
3697 != SET_SRC (set))
3698 && in_list_p (curr_insn,
3699 ira_reg_equiv
3700 [REGNO (SET_SRC (set))].init_insns)))
3702 /* This is equiv init insn of pseudo which did not get a
3703 hard register -- remove the insn. */
3704 if (lra_dump_file != NULL)
3706 fprintf (lra_dump_file,
3707 " Removing equiv init insn %i (freq=%d)\n",
3708 INSN_UID (curr_insn),
3709 BLOCK_FOR_INSN (curr_insn)->frequency);
3710 dump_insn_slim (lra_dump_file, curr_insn);
3712 if (contains_reg_p (x, true, false))
3713 lra_risky_transformations_p = true;
3714 lra_set_insn_deleted (curr_insn);
3715 continue;
3718 curr_id = lra_get_insn_recog_data (curr_insn);
3719 curr_static_id = curr_id->insn_static_data;
3720 init_curr_insn_input_reloads ();
3721 init_curr_operand_mode ();
3722 if (curr_insn_transform ())
3723 changed_p = true;
3724 /* Check non-transformed insns too for equiv change as USE
3725 or CLOBBER don't need reloads but can contain pseudos
3726 being changed on their equivalences. */
3727 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
3728 && loc_equivalence_change_p (&PATTERN (curr_insn)))
3730 lra_update_insn_regno_info (curr_insn);
3731 changed_p = true;
3735 bitmap_clear (&equiv_insn_bitmap);
3736 /* If we used a new hard regno, changed_p should be true because the
3737 hard reg is assigned to a new pseudo. */
3738 #ifdef ENABLE_CHECKING
3739 if (! changed_p)
3741 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3742 if (lra_reg_info[i].nrefs != 0
3743 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
3745 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
3747 for (j = 0; j < nregs; j++)
3748 lra_assert (df_regs_ever_live_p (hard_regno + j));
3751 #endif
3752 return changed_p;
3755 /* Initiate the LRA constraint pass. It is done once per
3756 function. */
3757 void
3758 lra_constraints_init (void)
3762 /* Finalize the LRA constraint pass. It is done once per
3763 function. */
3764 void
3765 lra_constraints_finish (void)
3771 /* This page contains code to do inheritance/split
3772 transformations. */
3774 /* Number of reloads passed so far in current EBB. */
3775 static int reloads_num;
3777 /* Number of calls passed so far in current EBB. */
3778 static int calls_num;
3780 /* Current reload pseudo check for validity of elements in
3781 USAGE_INSNS. */
3782 static int curr_usage_insns_check;
3784 /* Info about last usage of registers in EBB to do inheritance/split
3785 transformation. Inheritance transformation is done from a spilled
3786 pseudo and split transformations from a hard register or a pseudo
3787 assigned to a hard register. */
3788 struct usage_insns
3790 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
3791 value INSNS is valid. The insns is chain of optional debug insns
3792 and a finishing non-debug insn using the corresponding reg. */
3793 int check;
3794 /* Value of global reloads_num at the last insn in INSNS. */
3795 int reloads_num;
3796 /* Value of global reloads_nums at the last insn in INSNS. */
3797 int calls_num;
3798 /* It can be true only for splitting. And it means that the restore
3799 insn should be put after insn given by the following member. */
3800 bool after_p;
3801 /* Next insns in the current EBB which use the original reg and the
3802 original reg value is not changed between the current insn and
3803 the next insns. In order words, e.g. for inheritance, if we need
3804 to use the original reg value again in the next insns we can try
3805 to use the value in a hard register from a reload insn of the
3806 current insn. */
3807 rtx insns;
3810 /* Map: regno -> corresponding pseudo usage insns. */
3811 static struct usage_insns *usage_insns;
3813 static void
3814 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
3816 usage_insns[regno].check = curr_usage_insns_check;
3817 usage_insns[regno].insns = insn;
3818 usage_insns[regno].reloads_num = reloads_num;
3819 usage_insns[regno].calls_num = calls_num;
3820 usage_insns[regno].after_p = after_p;
3823 /* The function is used to form list REGNO usages which consists of
3824 optional debug insns finished by a non-debug insn using REGNO.
3825 RELOADS_NUM is current number of reload insns processed so far. */
3826 static void
3827 add_next_usage_insn (int regno, rtx insn, int reloads_num)
3829 rtx next_usage_insns;
3831 if (usage_insns[regno].check == curr_usage_insns_check
3832 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
3833 && DEBUG_INSN_P (insn))
3835 /* Check that we did not add the debug insn yet. */
3836 if (next_usage_insns != insn
3837 && (GET_CODE (next_usage_insns) != INSN_LIST
3838 || XEXP (next_usage_insns, 0) != insn))
3839 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
3840 next_usage_insns);
3842 else if (NONDEBUG_INSN_P (insn))
3843 setup_next_usage_insn (regno, insn, reloads_num, false);
3844 else
3845 usage_insns[regno].check = 0;
3848 /* Replace all references to register OLD_REGNO in *LOC with pseudo
3849 register NEW_REG. Return true if any change was made. */
3850 static bool
3851 substitute_pseudo (rtx *loc, int old_regno, rtx new_reg)
3853 rtx x = *loc;
3854 bool result = false;
3855 enum rtx_code code;
3856 const char *fmt;
3857 int i, j;
3859 if (x == NULL_RTX)
3860 return false;
3862 code = GET_CODE (x);
3863 if (code == REG && (int) REGNO (x) == old_regno)
3865 enum machine_mode mode = GET_MODE (*loc);
3866 enum machine_mode inner_mode = GET_MODE (new_reg);
3868 if (mode != inner_mode)
3870 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
3871 || ! SCALAR_INT_MODE_P (inner_mode))
3872 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
3873 else
3874 new_reg = gen_lowpart_SUBREG (mode, new_reg);
3876 *loc = new_reg;
3877 return true;
3880 /* Scan all the operand sub-expressions. */
3881 fmt = GET_RTX_FORMAT (code);
3882 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3884 if (fmt[i] == 'e')
3886 if (substitute_pseudo (&XEXP (x, i), old_regno, new_reg))
3887 result = true;
3889 else if (fmt[i] == 'E')
3891 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3892 if (substitute_pseudo (&XVECEXP (x, i, j), old_regno, new_reg))
3893 result = true;
3896 return result;
3899 /* Return first non-debug insn in list USAGE_INSNS. */
3900 static rtx
3901 skip_usage_debug_insns (rtx usage_insns)
3903 rtx insn;
3905 /* Skip debug insns. */
3906 for (insn = usage_insns;
3907 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
3908 insn = XEXP (insn, 1))
3910 return insn;
3913 /* Return true if we need secondary memory moves for insn in
3914 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
3915 into the insn. */
3916 static bool
3917 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
3918 rtx usage_insns ATTRIBUTE_UNUSED)
3920 #ifndef SECONDARY_MEMORY_NEEDED
3921 return false;
3922 #else
3923 rtx insn, set, dest;
3924 enum reg_class cl;
3926 if (inher_cl == ALL_REGS
3927 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
3928 return false;
3929 lra_assert (INSN_P (insn));
3930 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
3931 return false;
3932 dest = SET_DEST (set);
3933 if (! REG_P (dest))
3934 return false;
3935 lra_assert (inher_cl != NO_REGS);
3936 cl = get_reg_class (REGNO (dest));
3937 return (cl != NO_REGS && cl != ALL_REGS
3938 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
3939 #endif
3942 /* Registers involved in inheritance/split in the current EBB
3943 (inheritance/split pseudos and original registers). */
3944 static bitmap_head check_only_regs;
3946 /* Do inheritance transformations for insn INSN, which defines (if
3947 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
3948 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
3949 form as the "insns" field of usage_insns. Return true if we
3950 succeed in such transformation.
3952 The transformations look like:
3954 p <- ... i <- ...
3955 ... p <- i (new insn)
3956 ... =>
3957 <- ... p ... <- ... i ...
3959 ... i <- p (new insn)
3960 <- ... p ... <- ... i ...
3961 ... =>
3962 <- ... p ... <- ... i ...
3963 where p is a spilled original pseudo and i is a new inheritance pseudo.
3966 The inheritance pseudo has the smallest class of two classes CL and
3967 class of ORIGINAL REGNO. */
3968 static bool
3969 inherit_reload_reg (bool def_p, int original_regno,
3970 enum reg_class cl, rtx insn, rtx next_usage_insns)
3972 enum reg_class rclass = lra_get_allocno_class (original_regno);
3973 rtx original_reg = regno_reg_rtx[original_regno];
3974 rtx new_reg, new_insns, usage_insn;
3976 lra_assert (! usage_insns[original_regno].after_p);
3977 if (lra_dump_file != NULL)
3978 fprintf (lra_dump_file,
3979 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
3980 if (! ira_reg_classes_intersect_p[cl][rclass])
3982 if (lra_dump_file != NULL)
3984 fprintf (lra_dump_file,
3985 " Rejecting inheritance for %d "
3986 "because of disjoint classes %s and %s\n",
3987 original_regno, reg_class_names[cl],
3988 reg_class_names[rclass]);
3989 fprintf (lra_dump_file,
3990 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
3992 return false;
3994 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
3995 /* We don't use a subset of two classes because it can be
3996 NO_REGS. This transformation is still profitable in most
3997 cases even if the classes are not intersected as register
3998 move is probably cheaper than a memory load. */
3999 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4001 if (lra_dump_file != NULL)
4002 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4003 reg_class_names[cl], reg_class_names[rclass]);
4005 rclass = cl;
4007 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4009 /* Reject inheritance resulting in secondary memory moves.
4010 Otherwise, there is a danger in LRA cycling. Also such
4011 transformation will be unprofitable. */
4012 if (lra_dump_file != NULL)
4014 rtx insn = skip_usage_debug_insns (next_usage_insns);
4015 rtx set = single_set (insn);
4017 lra_assert (set != NULL_RTX);
4019 rtx dest = SET_DEST (set);
4021 lra_assert (REG_P (dest));
4022 fprintf (lra_dump_file,
4023 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4024 "as secondary mem is needed\n",
4025 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4026 original_regno, reg_class_names[rclass]);
4027 fprintf (lra_dump_file,
4028 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4030 return false;
4032 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4033 rclass, "inheritance");
4034 start_sequence ();
4035 if (def_p)
4036 emit_move_insn (original_reg, new_reg);
4037 else
4038 emit_move_insn (new_reg, original_reg);
4039 new_insns = get_insns ();
4040 end_sequence ();
4041 if (NEXT_INSN (new_insns) != NULL_RTX)
4043 if (lra_dump_file != NULL)
4045 fprintf (lra_dump_file,
4046 " Rejecting inheritance %d->%d "
4047 "as it results in 2 or more insns:\n",
4048 original_regno, REGNO (new_reg));
4049 dump_rtl_slim (lra_dump_file, new_insns, NULL_RTX, -1, 0);
4050 fprintf (lra_dump_file,
4051 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4053 return false;
4055 substitute_pseudo (&insn, original_regno, new_reg);
4056 lra_update_insn_regno_info (insn);
4057 if (! def_p)
4058 /* We now have a new usage insn for original regno. */
4059 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4060 if (lra_dump_file != NULL)
4061 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4062 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4063 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4064 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4065 bitmap_set_bit (&check_only_regs, original_regno);
4066 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4067 if (def_p)
4068 lra_process_new_insns (insn, NULL_RTX, new_insns,
4069 "Add original<-inheritance");
4070 else
4071 lra_process_new_insns (insn, new_insns, NULL_RTX,
4072 "Add inheritance<-original");
4073 while (next_usage_insns != NULL_RTX)
4075 if (GET_CODE (next_usage_insns) != INSN_LIST)
4077 usage_insn = next_usage_insns;
4078 lra_assert (NONDEBUG_INSN_P (usage_insn));
4079 next_usage_insns = NULL;
4081 else
4083 usage_insn = XEXP (next_usage_insns, 0);
4084 lra_assert (DEBUG_INSN_P (usage_insn));
4085 next_usage_insns = XEXP (next_usage_insns, 1);
4087 substitute_pseudo (&usage_insn, original_regno, new_reg);
4088 lra_update_insn_regno_info (usage_insn);
4089 if (lra_dump_file != NULL)
4091 fprintf (lra_dump_file,
4092 " Inheritance reuse change %d->%d (bb%d):\n",
4093 original_regno, REGNO (new_reg),
4094 BLOCK_FOR_INSN (usage_insn)->index);
4095 dump_insn_slim (lra_dump_file, usage_insn);
4098 if (lra_dump_file != NULL)
4099 fprintf (lra_dump_file,
4100 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4101 return true;
4104 /* Return true if we need a caller save/restore for pseudo REGNO which
4105 was assigned to a hard register. */
4106 static inline bool
4107 need_for_call_save_p (int regno)
4109 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4110 return (usage_insns[regno].calls_num < calls_num
4111 && (overlaps_hard_reg_set_p
4112 (call_used_reg_set,
4113 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])));
4116 /* Global registers occuring in the current EBB. */
4117 static bitmap_head ebb_global_regs;
4119 /* Return true if we need a split for hard register REGNO or pseudo
4120 REGNO which was assigned to a hard register.
4121 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4122 used for reloads since the EBB end. It is an approximation of the
4123 used hard registers in the split range. The exact value would
4124 require expensive calculations. If we were aggressive with
4125 splitting because of the approximation, the split pseudo will save
4126 the same hard register assignment and will be removed in the undo
4127 pass. We still need the approximation because too aggressive
4128 splitting would result in too inaccurate cost calculation in the
4129 assignment pass because of too many generated moves which will be
4130 probably removed in the undo pass. */
4131 static inline bool
4132 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4134 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4136 lra_assert (hard_regno >= 0);
4137 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4138 /* Don't split eliminable hard registers, otherwise we can
4139 split hard registers like hard frame pointer, which
4140 lives on BB start/end according to DF-infrastructure,
4141 when there is a pseudo assigned to the register and
4142 living in the same BB. */
4143 && (regno >= FIRST_PSEUDO_REGISTER
4144 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4145 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4146 /* We need at least 2 reloads to make pseudo splitting
4147 profitable. We should provide hard regno splitting in
4148 any case to solve 1st insn scheduling problem when
4149 moving hard register definition up might result in
4150 impossibility to find hard register for reload pseudo of
4151 small register class. */
4152 && (usage_insns[regno].reloads_num
4153 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 2) < reloads_num)
4154 && (regno < FIRST_PSEUDO_REGISTER
4155 /* For short living pseudos, spilling + inheritance can
4156 be considered a substitution for splitting.
4157 Therefore we do not splitting for local pseudos. It
4158 decreases also aggressiveness of splitting. The
4159 minimal number of references is chosen taking into
4160 account that for 2 references splitting has no sense
4161 as we can just spill the pseudo. */
4162 || (regno >= FIRST_PSEUDO_REGISTER
4163 && lra_reg_info[regno].nrefs > 3
4164 && bitmap_bit_p (&ebb_global_regs, regno))))
4165 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4168 /* Return class for the split pseudo created from original pseudo with
4169 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4170 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4171 results in no secondary memory movements. */
4172 static enum reg_class
4173 choose_split_class (enum reg_class allocno_class,
4174 int hard_regno ATTRIBUTE_UNUSED,
4175 enum machine_mode mode ATTRIBUTE_UNUSED)
4177 #ifndef SECONDARY_MEMORY_NEEDED
4178 return allocno_class;
4179 #else
4180 int i;
4181 enum reg_class cl, best_cl = NO_REGS;
4182 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4183 = REGNO_REG_CLASS (hard_regno);
4185 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4186 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4187 return allocno_class;
4188 for (i = 0;
4189 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4190 i++)
4191 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4192 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4193 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4194 && (best_cl == NO_REGS
4195 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4196 best_cl = cl;
4197 return best_cl;
4198 #endif
4201 /* Do split transformations for insn INSN, which defines or uses
4202 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4203 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4204 "insns" field of usage_insns.
4206 The transformations look like:
4208 p <- ... p <- ...
4209 ... s <- p (new insn -- save)
4210 ... =>
4211 ... p <- s (new insn -- restore)
4212 <- ... p ... <- ... p ...
4214 <- ... p ... <- ... p ...
4215 ... s <- p (new insn -- save)
4216 ... =>
4217 ... p <- s (new insn -- restore)
4218 <- ... p ... <- ... p ...
4220 where p is an original pseudo got a hard register or a hard
4221 register and s is a new split pseudo. The save is put before INSN
4222 if BEFORE_P is true. Return true if we succeed in such
4223 transformation. */
4224 static bool
4225 split_reg (bool before_p, int original_regno, rtx insn, rtx next_usage_insns)
4227 enum reg_class rclass;
4228 rtx original_reg;
4229 int hard_regno, nregs;
4230 rtx new_reg, save, restore, usage_insn;
4231 bool after_p;
4232 bool call_save_p;
4234 if (original_regno < FIRST_PSEUDO_REGISTER)
4236 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4237 hard_regno = original_regno;
4238 call_save_p = false;
4239 nregs = 1;
4241 else
4243 hard_regno = reg_renumber[original_regno];
4244 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4245 rclass = lra_get_allocno_class (original_regno);
4246 original_reg = regno_reg_rtx[original_regno];
4247 call_save_p = need_for_call_save_p (original_regno);
4249 original_reg = regno_reg_rtx[original_regno];
4250 lra_assert (hard_regno >= 0);
4251 if (lra_dump_file != NULL)
4252 fprintf (lra_dump_file,
4253 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4254 if (call_save_p)
4256 enum machine_mode sec_mode;
4258 #ifdef SECONDARY_MEMORY_NEEDED_MODE
4259 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (original_reg));
4260 #else
4261 sec_mode = GET_MODE (original_reg);
4262 #endif
4263 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
4264 NO_REGS, "save");
4266 else
4268 rclass = choose_split_class (rclass, hard_regno,
4269 GET_MODE (original_reg));
4270 if (rclass == NO_REGS)
4272 if (lra_dump_file != NULL)
4274 fprintf (lra_dump_file,
4275 " Rejecting split of %d(%s): "
4276 "no good reg class for %d(%s)\n",
4277 original_regno,
4278 reg_class_names[lra_get_allocno_class (original_regno)],
4279 hard_regno,
4280 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4281 fprintf
4282 (lra_dump_file,
4283 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4285 return false;
4287 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4288 rclass, "split");
4289 reg_renumber[REGNO (new_reg)] = hard_regno;
4291 save = emit_spill_move (true, new_reg, original_reg);
4292 if (NEXT_INSN (save) != NULL_RTX)
4294 lra_assert (! call_save_p);
4295 if (lra_dump_file != NULL)
4297 fprintf
4298 (lra_dump_file,
4299 " Rejecting split %d->%d resulting in > 2 %s save insns:\n",
4300 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4301 dump_rtl_slim (lra_dump_file, save, NULL_RTX, -1, 0);
4302 fprintf (lra_dump_file,
4303 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4305 return false;
4307 restore = emit_spill_move (false, new_reg, original_reg);
4308 if (NEXT_INSN (restore) != NULL_RTX)
4310 lra_assert (! call_save_p);
4311 if (lra_dump_file != NULL)
4313 fprintf (lra_dump_file,
4314 " Rejecting split %d->%d "
4315 "resulting in > 2 %s restore insns:\n",
4316 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4317 dump_rtl_slim (lra_dump_file, restore, NULL_RTX, -1, 0);
4318 fprintf (lra_dump_file,
4319 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4321 return false;
4323 after_p = usage_insns[original_regno].after_p;
4324 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4325 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4326 bitmap_set_bit (&check_only_regs, original_regno);
4327 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4328 for (;;)
4330 if (GET_CODE (next_usage_insns) != INSN_LIST)
4332 usage_insn = next_usage_insns;
4333 break;
4335 usage_insn = XEXP (next_usage_insns, 0);
4336 lra_assert (DEBUG_INSN_P (usage_insn));
4337 next_usage_insns = XEXP (next_usage_insns, 1);
4338 substitute_pseudo (&usage_insn, original_regno, new_reg);
4339 lra_update_insn_regno_info (usage_insn);
4340 if (lra_dump_file != NULL)
4342 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
4343 original_regno, REGNO (new_reg));
4344 dump_insn_slim (lra_dump_file, usage_insn);
4347 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
4348 lra_assert (usage_insn != insn || (after_p && before_p));
4349 lra_process_new_insns (usage_insn, after_p ? NULL_RTX : restore,
4350 after_p ? restore : NULL_RTX,
4351 call_save_p
4352 ? "Add reg<-save" : "Add reg<-split");
4353 lra_process_new_insns (insn, before_p ? save : NULL_RTX,
4354 before_p ? NULL_RTX : save,
4355 call_save_p
4356 ? "Add save<-reg" : "Add split<-reg");
4357 if (nregs > 1)
4358 /* If we are trying to split multi-register. We should check
4359 conflicts on the next assignment sub-pass. IRA can allocate on
4360 sub-register levels, LRA do this on pseudos level right now and
4361 this discrepancy may create allocation conflicts after
4362 splitting. */
4363 lra_risky_transformations_p = true;
4364 if (lra_dump_file != NULL)
4365 fprintf (lra_dump_file,
4366 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4367 return true;
4370 /* Recognize that we need a split transformation for insn INSN, which
4371 defines or uses REGNO in its insn biggest MODE (we use it only if
4372 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
4373 hard registers which might be used for reloads since the EBB end.
4374 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
4375 uid before starting INSN processing. Return true if we succeed in
4376 such transformation. */
4377 static bool
4378 split_if_necessary (int regno, enum machine_mode mode,
4379 HARD_REG_SET potential_reload_hard_regs,
4380 bool before_p, rtx insn, int max_uid)
4382 bool res = false;
4383 int i, nregs = 1;
4384 rtx next_usage_insns;
4386 if (regno < FIRST_PSEUDO_REGISTER)
4387 nregs = hard_regno_nregs[regno][mode];
4388 for (i = 0; i < nregs; i++)
4389 if (usage_insns[regno + i].check == curr_usage_insns_check
4390 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
4391 /* To avoid processing the register twice or more. */
4392 && ((GET_CODE (next_usage_insns) != INSN_LIST
4393 && INSN_UID (next_usage_insns) < max_uid)
4394 || (GET_CODE (next_usage_insns) == INSN_LIST
4395 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
4396 && need_for_split_p (potential_reload_hard_regs, regno + i)
4397 && split_reg (before_p, regno + i, insn, next_usage_insns))
4398 res = true;
4399 return res;
4402 /* Check only registers living at the current program point in the
4403 current EBB. */
4404 static bitmap_head live_regs;
4406 /* Update live info in EBB given by its HEAD and TAIL insns after
4407 inheritance/split transformation. The function removes dead moves
4408 too. */
4409 static void
4410 update_ebb_live_info (rtx head, rtx tail)
4412 unsigned int j;
4413 int regno;
4414 bool live_p;
4415 rtx prev_insn, set;
4416 bool remove_p;
4417 basic_block last_bb, prev_bb, curr_bb;
4418 bitmap_iterator bi;
4419 struct lra_insn_reg *reg;
4420 edge e;
4421 edge_iterator ei;
4423 last_bb = BLOCK_FOR_INSN (tail);
4424 prev_bb = NULL;
4425 for (curr_insn = tail;
4426 curr_insn != PREV_INSN (head);
4427 curr_insn = prev_insn)
4429 prev_insn = PREV_INSN (curr_insn);
4430 /* We need to process empty blocks too. They contain
4431 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
4432 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
4433 continue;
4434 curr_bb = BLOCK_FOR_INSN (curr_insn);
4435 if (curr_bb != prev_bb)
4437 if (prev_bb != NULL)
4439 /* Update df_get_live_in (prev_bb): */
4440 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4441 if (bitmap_bit_p (&live_regs, j))
4442 bitmap_set_bit (df_get_live_in (prev_bb), j);
4443 else
4444 bitmap_clear_bit (df_get_live_in (prev_bb), j);
4446 if (curr_bb != last_bb)
4448 /* Update df_get_live_out (curr_bb): */
4449 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4451 live_p = bitmap_bit_p (&live_regs, j);
4452 if (! live_p)
4453 FOR_EACH_EDGE (e, ei, curr_bb->succs)
4454 if (bitmap_bit_p (df_get_live_in (e->dest), j))
4456 live_p = true;
4457 break;
4459 if (live_p)
4460 bitmap_set_bit (df_get_live_out (curr_bb), j);
4461 else
4462 bitmap_clear_bit (df_get_live_out (curr_bb), j);
4465 prev_bb = curr_bb;
4466 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
4468 if (! NONDEBUG_INSN_P (curr_insn))
4469 continue;
4470 curr_id = lra_get_insn_recog_data (curr_insn);
4471 remove_p = false;
4472 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
4473 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
4474 && bitmap_bit_p (&check_only_regs, regno)
4475 && ! bitmap_bit_p (&live_regs, regno))
4476 remove_p = true;
4477 /* See which defined values die here. */
4478 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4479 if (reg->type == OP_OUT && ! reg->subreg_p)
4480 bitmap_clear_bit (&live_regs, reg->regno);
4481 /* Mark each used value as live. */
4482 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4483 if (reg->type == OP_IN
4484 && bitmap_bit_p (&check_only_regs, reg->regno))
4485 bitmap_set_bit (&live_regs, reg->regno);
4486 /* It is quite important to remove dead move insns because it
4487 means removing dead store. We don't need to process them for
4488 constraints. */
4489 if (remove_p)
4491 if (lra_dump_file != NULL)
4493 fprintf (lra_dump_file, " Removing dead insn:\n ");
4494 dump_insn_slim (lra_dump_file, curr_insn);
4496 lra_set_insn_deleted (curr_insn);
4501 /* The structure describes info to do an inheritance for the current
4502 insn. We need to collect such info first before doing the
4503 transformations because the transformations change the insn
4504 internal representation. */
4505 struct to_inherit
4507 /* Original regno. */
4508 int regno;
4509 /* Subsequent insns which can inherit original reg value. */
4510 rtx insns;
4513 /* Array containing all info for doing inheritance from the current
4514 insn. */
4515 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
4517 /* Number elements in the previous array. */
4518 static int to_inherit_num;
4520 /* Add inheritance info REGNO and INSNS. Their meaning is described in
4521 structure to_inherit. */
4522 static void
4523 add_to_inherit (int regno, rtx insns)
4525 int i;
4527 for (i = 0; i < to_inherit_num; i++)
4528 if (to_inherit[i].regno == regno)
4529 return;
4530 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
4531 to_inherit[to_inherit_num].regno = regno;
4532 to_inherit[to_inherit_num++].insns = insns;
4535 /* Return the last non-debug insn in basic block BB, or the block begin
4536 note if none. */
4537 static rtx
4538 get_last_insertion_point (basic_block bb)
4540 rtx insn;
4542 FOR_BB_INSNS_REVERSE (bb, insn)
4543 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
4544 return insn;
4545 gcc_unreachable ();
4548 /* Set up RES by registers living on edges FROM except the edge (FROM,
4549 TO) or by registers set up in a jump insn in BB FROM. */
4550 static void
4551 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
4553 rtx last;
4554 struct lra_insn_reg *reg;
4555 edge e;
4556 edge_iterator ei;
4558 lra_assert (to != NULL);
4559 bitmap_clear (res);
4560 FOR_EACH_EDGE (e, ei, from->succs)
4561 if (e->dest != to)
4562 bitmap_ior_into (res, df_get_live_in (e->dest));
4563 last = get_last_insertion_point (from);
4564 if (! JUMP_P (last))
4565 return;
4566 curr_id = lra_get_insn_recog_data (last);
4567 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4568 if (reg->type != OP_IN)
4569 bitmap_set_bit (res, reg->regno);
4572 /* Used as a temporary results of some bitmap calculations. */
4573 static bitmap_head temp_bitmap;
4575 /* Do inheritance/split transformations in EBB starting with HEAD and
4576 finishing on TAIL. We process EBB insns in the reverse order.
4577 Return true if we did any inheritance/split transformation in the
4578 EBB.
4580 We should avoid excessive splitting which results in worse code
4581 because of inaccurate cost calculations for spilling new split
4582 pseudos in such case. To achieve this we do splitting only if
4583 register pressure is high in given basic block and there are reload
4584 pseudos requiring hard registers. We could do more register
4585 pressure calculations at any given program point to avoid necessary
4586 splitting even more but it is to expensive and the current approach
4587 works well enough. */
4588 static bool
4589 inherit_in_ebb (rtx head, rtx tail)
4591 int i, src_regno, dst_regno, nregs;
4592 bool change_p, succ_p;
4593 rtx prev_insn, next_usage_insns, set, last_insn;
4594 enum reg_class cl;
4595 struct lra_insn_reg *reg;
4596 basic_block last_processed_bb, curr_bb = NULL;
4597 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
4598 bitmap to_process;
4599 unsigned int j;
4600 bitmap_iterator bi;
4601 bool head_p, after_p;
4603 change_p = false;
4604 curr_usage_insns_check++;
4605 reloads_num = calls_num = 0;
4606 bitmap_clear (&check_only_regs);
4607 last_processed_bb = NULL;
4608 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
4609 CLEAR_HARD_REG_SET (live_hard_regs);
4610 /* We don't process new insns generated in the loop. */
4611 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
4613 prev_insn = PREV_INSN (curr_insn);
4614 if (BLOCK_FOR_INSN (curr_insn) != NULL)
4615 curr_bb = BLOCK_FOR_INSN (curr_insn);
4616 if (last_processed_bb != curr_bb)
4618 /* We are at the end of BB. Add qualified living
4619 pseudos for potential splitting. */
4620 to_process = df_get_live_out (curr_bb);
4621 if (last_processed_bb != NULL)
4623 /* We are somewhere in the middle of EBB. */
4624 get_live_on_other_edges (curr_bb, last_processed_bb,
4625 &temp_bitmap);
4626 to_process = &temp_bitmap;
4628 last_processed_bb = curr_bb;
4629 last_insn = get_last_insertion_point (curr_bb);
4630 after_p = (! JUMP_P (last_insn)
4631 && (! CALL_P (last_insn)
4632 || (find_reg_note (last_insn,
4633 REG_NORETURN, NULL_RTX) == NULL_RTX
4634 && ! SIBLING_CALL_P (last_insn))));
4635 REG_SET_TO_HARD_REG_SET (live_hard_regs, df_get_live_out (curr_bb));
4636 IOR_HARD_REG_SET (live_hard_regs, eliminable_regset);
4637 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
4638 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
4639 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
4641 if ((int) j >= lra_constraint_new_regno_start)
4642 break;
4643 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
4645 if (j < FIRST_PSEUDO_REGISTER)
4646 SET_HARD_REG_BIT (live_hard_regs, j);
4647 else
4648 add_to_hard_reg_set (&live_hard_regs,
4649 PSEUDO_REGNO_MODE (j),
4650 reg_renumber[j]);
4651 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
4655 src_regno = dst_regno = -1;
4656 if (NONDEBUG_INSN_P (curr_insn)
4657 && (set = single_set (curr_insn)) != NULL_RTX
4658 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
4660 src_regno = REGNO (SET_SRC (set));
4661 dst_regno = REGNO (SET_DEST (set));
4663 if (src_regno < lra_constraint_new_regno_start
4664 && src_regno >= FIRST_PSEUDO_REGISTER
4665 && reg_renumber[src_regno] < 0
4666 && dst_regno >= lra_constraint_new_regno_start
4667 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
4669 /* 'reload_pseudo <- original_pseudo'. */
4670 reloads_num++;
4671 succ_p = false;
4672 if (usage_insns[src_regno].check == curr_usage_insns_check
4673 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
4674 succ_p = inherit_reload_reg (false, src_regno, cl,
4675 curr_insn, next_usage_insns);
4676 if (succ_p)
4677 change_p = true;
4678 else
4679 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
4680 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
4681 IOR_HARD_REG_SET (potential_reload_hard_regs,
4682 reg_class_contents[cl]);
4684 else if (src_regno >= lra_constraint_new_regno_start
4685 && dst_regno < lra_constraint_new_regno_start
4686 && dst_regno >= FIRST_PSEUDO_REGISTER
4687 && reg_renumber[dst_regno] < 0
4688 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
4689 && usage_insns[dst_regno].check == curr_usage_insns_check
4690 && (next_usage_insns
4691 = usage_insns[dst_regno].insns) != NULL_RTX)
4693 reloads_num++;
4694 /* 'original_pseudo <- reload_pseudo'. */
4695 if (! JUMP_P (curr_insn)
4696 && inherit_reload_reg (true, dst_regno, cl,
4697 curr_insn, next_usage_insns))
4698 change_p = true;
4699 /* Invalidate. */
4700 usage_insns[dst_regno].check = 0;
4701 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
4702 IOR_HARD_REG_SET (potential_reload_hard_regs,
4703 reg_class_contents[cl]);
4705 else if (INSN_P (curr_insn))
4707 int max_uid = get_max_uid ();
4709 curr_id = lra_get_insn_recog_data (curr_insn);
4710 to_inherit_num = 0;
4711 /* Process insn definitions. */
4712 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4713 if (reg->type != OP_IN
4714 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
4716 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
4717 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
4718 && usage_insns[dst_regno].check == curr_usage_insns_check
4719 && (next_usage_insns
4720 = usage_insns[dst_regno].insns) != NULL_RTX)
4722 struct lra_insn_reg *r;
4724 for (r = curr_id->regs; r != NULL; r = r->next)
4725 if (r->type != OP_OUT && r->regno == dst_regno)
4726 break;
4727 /* Don't do inheritance if the pseudo is also
4728 used in the insn. */
4729 if (r == NULL)
4730 /* We can not do inheritance right now
4731 because the current insn reg info (chain
4732 regs) can change after that. */
4733 add_to_inherit (dst_regno, next_usage_insns);
4735 /* We can not process one reg twice here because of
4736 usage_insns invalidation. */
4737 if ((dst_regno < FIRST_PSEUDO_REGISTER
4738 || reg_renumber[dst_regno] >= 0)
4739 && ! reg->subreg_p && reg->type == OP_OUT)
4741 HARD_REG_SET s;
4743 if (split_if_necessary (dst_regno, reg->biggest_mode,
4744 potential_reload_hard_regs,
4745 false, curr_insn, max_uid))
4746 change_p = true;
4747 CLEAR_HARD_REG_SET (s);
4748 if (dst_regno < FIRST_PSEUDO_REGISTER)
4749 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
4750 else
4751 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
4752 reg_renumber[dst_regno]);
4753 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
4755 /* We should invalidate potential inheritance or
4756 splitting for the current insn usages to the next
4757 usage insns (see code below) as the output pseudo
4758 prevents this. */
4759 if ((dst_regno >= FIRST_PSEUDO_REGISTER
4760 && reg_renumber[dst_regno] < 0)
4761 || (reg->type == OP_OUT && ! reg->subreg_p
4762 && (dst_regno < FIRST_PSEUDO_REGISTER
4763 || reg_renumber[dst_regno] >= 0)))
4765 /* Invalidate. */
4766 if (dst_regno >= FIRST_PSEUDO_REGISTER)
4767 usage_insns[dst_regno].check = 0;
4768 else
4770 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
4771 for (i = 0; i < nregs; i++)
4772 usage_insns[dst_regno + i].check = 0;
4776 if (! JUMP_P (curr_insn))
4777 for (i = 0; i < to_inherit_num; i++)
4778 if (inherit_reload_reg (true, to_inherit[i].regno,
4779 ALL_REGS, curr_insn,
4780 to_inherit[i].insns))
4781 change_p = true;
4782 if (CALL_P (curr_insn))
4784 rtx cheap, pat, dest, restore;
4785 int regno, hard_regno;
4787 calls_num++;
4788 if ((cheap = find_reg_note (curr_insn,
4789 REG_RETURNED, NULL_RTX)) != NULL_RTX
4790 && ((cheap = XEXP (cheap, 0)), true)
4791 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
4792 && (hard_regno = reg_renumber[regno]) >= 0
4793 /* If there are pending saves/restores, the
4794 optimization is not worth. */
4795 && usage_insns[regno].calls_num == calls_num - 1
4796 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
4798 /* Restore the pseudo from the call result as
4799 REG_RETURNED note says that the pseudo value is
4800 in the call result and the pseudo is an argument
4801 of the call. */
4802 pat = PATTERN (curr_insn);
4803 if (GET_CODE (pat) == PARALLEL)
4804 pat = XVECEXP (pat, 0, 0);
4805 dest = SET_DEST (pat);
4806 start_sequence ();
4807 emit_move_insn (cheap, copy_rtx (dest));
4808 restore = get_insns ();
4809 end_sequence ();
4810 lra_process_new_insns (curr_insn, NULL, restore,
4811 "Inserting call parameter restore");
4812 /* We don't need to save/restore of the pseudo from
4813 this call. */
4814 usage_insns[regno].calls_num = calls_num;
4815 bitmap_set_bit (&check_only_regs, regno);
4818 to_inherit_num = 0;
4819 /* Process insn usages. */
4820 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4821 if ((reg->type != OP_OUT
4822 || (reg->type == OP_OUT && reg->subreg_p))
4823 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
4825 if (src_regno >= FIRST_PSEUDO_REGISTER
4826 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
4828 if (usage_insns[src_regno].check == curr_usage_insns_check
4829 && (next_usage_insns
4830 = usage_insns[src_regno].insns) != NULL_RTX
4831 && NONDEBUG_INSN_P (curr_insn))
4832 add_to_inherit (src_regno, next_usage_insns);
4833 else
4834 /* Add usages. */
4835 add_next_usage_insn (src_regno, curr_insn, reloads_num);
4837 else if (src_regno < FIRST_PSEUDO_REGISTER
4838 || reg_renumber[src_regno] >= 0)
4840 bool before_p;
4841 rtx use_insn = curr_insn;
4843 before_p = (JUMP_P (curr_insn)
4844 || (CALL_P (curr_insn) && reg->type == OP_IN));
4845 if (NONDEBUG_INSN_P (curr_insn)
4846 && split_if_necessary (src_regno, reg->biggest_mode,
4847 potential_reload_hard_regs,
4848 before_p, curr_insn, max_uid))
4850 if (reg->subreg_p)
4851 lra_risky_transformations_p = true;
4852 change_p = true;
4853 /* Invalidate. */
4854 usage_insns[src_regno].check = 0;
4855 if (before_p)
4856 use_insn = PREV_INSN (curr_insn);
4858 if (NONDEBUG_INSN_P (curr_insn))
4860 if (src_regno < FIRST_PSEUDO_REGISTER)
4861 add_to_hard_reg_set (&live_hard_regs,
4862 reg->biggest_mode, src_regno);
4863 else
4864 add_to_hard_reg_set (&live_hard_regs,
4865 PSEUDO_REGNO_MODE (src_regno),
4866 reg_renumber[src_regno]);
4868 add_next_usage_insn (src_regno, use_insn, reloads_num);
4871 for (i = 0; i < to_inherit_num; i++)
4873 src_regno = to_inherit[i].regno;
4874 if (inherit_reload_reg (false, src_regno, ALL_REGS,
4875 curr_insn, to_inherit[i].insns))
4876 change_p = true;
4877 else
4878 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
4881 /* We reached the start of the current basic block. */
4882 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
4883 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
4885 /* We reached the beginning of the current block -- do
4886 rest of spliting in the current BB. */
4887 to_process = df_get_live_in (curr_bb);
4888 if (BLOCK_FOR_INSN (head) != curr_bb)
4890 /* We are somewhere in the middle of EBB. */
4891 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
4892 curr_bb, &temp_bitmap);
4893 to_process = &temp_bitmap;
4895 head_p = true;
4896 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
4898 if ((int) j >= lra_constraint_new_regno_start)
4899 break;
4900 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
4901 && usage_insns[j].check == curr_usage_insns_check
4902 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
4904 if (need_for_split_p (potential_reload_hard_regs, j))
4906 if (lra_dump_file != NULL && head_p)
4908 fprintf (lra_dump_file,
4909 " ----------------------------------\n");
4910 head_p = false;
4912 if (split_reg (false, j, bb_note (curr_bb),
4913 next_usage_insns))
4914 change_p = true;
4916 usage_insns[j].check = 0;
4921 return change_p;
4924 /* This value affects EBB forming. If probability of edge from EBB to
4925 a BB is not greater than the following value, we don't add the BB
4926 to EBB. */
4927 #define EBB_PROBABILITY_CUTOFF ((REG_BR_PROB_BASE * 50) / 100)
4929 /* Current number of inheritance/split iteration. */
4930 int lra_inheritance_iter;
4932 /* Entry function for inheritance/split pass. */
4933 void
4934 lra_inheritance (void)
4936 int i;
4937 basic_block bb, start_bb;
4938 edge e;
4940 lra_inheritance_iter++;
4941 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
4942 return;
4943 timevar_push (TV_LRA_INHERITANCE);
4944 if (lra_dump_file != NULL)
4945 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
4946 lra_inheritance_iter);
4947 curr_usage_insns_check = 0;
4948 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
4949 for (i = 0; i < lra_constraint_new_regno_start; i++)
4950 usage_insns[i].check = 0;
4951 bitmap_initialize (&check_only_regs, &reg_obstack);
4952 bitmap_initialize (&live_regs, &reg_obstack);
4953 bitmap_initialize (&temp_bitmap, &reg_obstack);
4954 bitmap_initialize (&ebb_global_regs, &reg_obstack);
4955 FOR_EACH_BB (bb)
4957 start_bb = bb;
4958 if (lra_dump_file != NULL)
4959 fprintf (lra_dump_file, "EBB");
4960 /* Form a EBB starting with BB. */
4961 bitmap_clear (&ebb_global_regs);
4962 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
4963 for (;;)
4965 if (lra_dump_file != NULL)
4966 fprintf (lra_dump_file, " %d", bb->index);
4967 if (bb->next_bb == EXIT_BLOCK_PTR || LABEL_P (BB_HEAD (bb->next_bb)))
4968 break;
4969 e = find_fallthru_edge (bb->succs);
4970 if (! e)
4971 break;
4972 if (e->probability <= EBB_PROBABILITY_CUTOFF)
4973 break;
4974 bb = bb->next_bb;
4976 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
4977 if (lra_dump_file != NULL)
4978 fprintf (lra_dump_file, "\n");
4979 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
4980 /* Remember that the EBB head and tail can change in
4981 inherit_in_ebb. */
4982 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
4984 bitmap_clear (&ebb_global_regs);
4985 bitmap_clear (&temp_bitmap);
4986 bitmap_clear (&live_regs);
4987 bitmap_clear (&check_only_regs);
4988 free (usage_insns);
4990 timevar_pop (TV_LRA_INHERITANCE);
4995 /* This page contains code to undo failed inheritance/split
4996 transformations. */
4998 /* Current number of iteration undoing inheritance/split. */
4999 int lra_undo_inheritance_iter;
5001 /* Fix BB live info LIVE after removing pseudos created on pass doing
5002 inheritance/split which are REMOVED_PSEUDOS. */
5003 static void
5004 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5006 unsigned int regno;
5007 bitmap_iterator bi;
5009 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5010 if (bitmap_clear_bit (live, regno))
5011 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5014 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5015 number. */
5016 static int
5017 get_regno (rtx reg)
5019 if (GET_CODE (reg) == SUBREG)
5020 reg = SUBREG_REG (reg);
5021 if (REG_P (reg))
5022 return REGNO (reg);
5023 return -1;
5026 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5027 return true if we did any change. The undo transformations for
5028 inheritance looks like
5029 i <- i2
5030 p <- i => p <- i2
5031 or removing
5032 p <- i, i <- p, and i <- i3
5033 where p is original pseudo from which inheritance pseudo i was
5034 created, i and i3 are removed inheritance pseudos, i2 is another
5035 not removed inheritance pseudo. All split pseudos or other
5036 occurrences of removed inheritance pseudos are changed on the
5037 corresponding original pseudos.
5039 The function also schedules insns changed and created during
5040 inheritance/split pass for processing by the subsequent constraint
5041 pass. */
5042 static bool
5043 remove_inheritance_pseudos (bitmap remove_pseudos)
5045 basic_block bb;
5046 int regno, sregno, prev_sregno, dregno, restore_regno;
5047 rtx set, prev_set, prev_insn;
5048 bool change_p, done_p;
5050 change_p = ! bitmap_empty_p (remove_pseudos);
5051 /* We can not finish the function right away if CHANGE_P is true
5052 because we need to marks insns affected by previous
5053 inheritance/split pass for processing by the subsequent
5054 constraint pass. */
5055 FOR_EACH_BB (bb)
5057 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5058 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5059 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5061 if (! INSN_P (curr_insn))
5062 continue;
5063 done_p = false;
5064 sregno = dregno = -1;
5065 if (change_p && NONDEBUG_INSN_P (curr_insn)
5066 && (set = single_set (curr_insn)) != NULL_RTX)
5068 dregno = get_regno (SET_DEST (set));
5069 sregno = get_regno (SET_SRC (set));
5072 if (sregno >= 0 && dregno >= 0)
5074 if ((bitmap_bit_p (remove_pseudos, sregno)
5075 && (lra_reg_info[sregno].restore_regno == dregno
5076 || (bitmap_bit_p (remove_pseudos, dregno)
5077 && (lra_reg_info[sregno].restore_regno
5078 == lra_reg_info[dregno].restore_regno))))
5079 || (bitmap_bit_p (remove_pseudos, dregno)
5080 && lra_reg_info[dregno].restore_regno == sregno))
5081 /* One of the following cases:
5082 original <- removed inheritance pseudo
5083 removed inherit pseudo <- another removed inherit pseudo
5084 removed inherit pseudo <- original pseudo
5086 removed_split_pseudo <- original_reg
5087 original_reg <- removed_split_pseudo */
5089 if (lra_dump_file != NULL)
5091 fprintf (lra_dump_file, " Removing %s:\n",
5092 bitmap_bit_p (&lra_split_regs, sregno)
5093 || bitmap_bit_p (&lra_split_regs, dregno)
5094 ? "split" : "inheritance");
5095 dump_insn_slim (lra_dump_file, curr_insn);
5097 lra_set_insn_deleted (curr_insn);
5098 done_p = true;
5100 else if (bitmap_bit_p (remove_pseudos, sregno)
5101 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5103 /* Search the following pattern:
5104 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5105 original_pseudo <- inherit_or_split_pseudo1
5106 where the 2nd insn is the current insn and
5107 inherit_or_split_pseudo2 is not removed. If it is found,
5108 change the current insn onto:
5109 original_pseudo <- inherit_or_split_pseudo2. */
5110 for (prev_insn = PREV_INSN (curr_insn);
5111 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5112 prev_insn = PREV_INSN (prev_insn))
5114 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5115 && (prev_set = single_set (prev_insn)) != NULL_RTX
5116 /* There should be no subregs in insn we are
5117 searching because only the original reg might
5118 be in subreg when we changed the mode of
5119 load/store for splitting. */
5120 && REG_P (SET_DEST (prev_set))
5121 && REG_P (SET_SRC (prev_set))
5122 && (int) REGNO (SET_DEST (prev_set)) == sregno
5123 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5124 >= FIRST_PSEUDO_REGISTER)
5125 /* As we consider chain of inheritance or
5126 splitting described in above comment we should
5127 check that sregno and prev_sregno were
5128 inheritance/split pseudos created from the
5129 same original regno. */
5130 && (lra_reg_info[sregno].restore_regno
5131 == lra_reg_info[prev_sregno].restore_regno)
5132 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5134 lra_assert (GET_MODE (SET_SRC (prev_set))
5135 == GET_MODE (regno_reg_rtx[sregno]));
5136 if (GET_CODE (SET_SRC (set)) == SUBREG)
5137 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5138 else
5139 SET_SRC (set) = SET_SRC (prev_set);
5140 lra_push_insn_and_update_insn_regno_info (curr_insn);
5141 lra_set_used_insn_alternative_by_uid
5142 (INSN_UID (curr_insn), -1);
5143 done_p = true;
5144 if (lra_dump_file != NULL)
5146 fprintf (lra_dump_file, " Change reload insn:\n");
5147 dump_insn_slim (lra_dump_file, curr_insn);
5152 if (! done_p)
5154 struct lra_insn_reg *reg;
5155 bool restored_regs_p = false;
5156 bool kept_regs_p = false;
5158 curr_id = lra_get_insn_recog_data (curr_insn);
5159 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5161 regno = reg->regno;
5162 restore_regno = lra_reg_info[regno].restore_regno;
5163 if (restore_regno >= 0)
5165 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5167 substitute_pseudo (&curr_insn, regno,
5168 regno_reg_rtx[restore_regno]);
5169 restored_regs_p = true;
5171 else
5172 kept_regs_p = true;
5175 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5177 /* The instruction has changed since the previous
5178 constraints pass. */
5179 lra_push_insn_and_update_insn_regno_info (curr_insn);
5180 lra_set_used_insn_alternative_by_uid
5181 (INSN_UID (curr_insn), -1);
5183 else if (restored_regs_p)
5184 /* The instruction has been restored to the form that
5185 it had during the previous constraints pass. */
5186 lra_update_insn_regno_info (curr_insn);
5187 if (restored_regs_p && lra_dump_file != NULL)
5189 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5190 dump_insn_slim (lra_dump_file, curr_insn);
5195 return change_p;
5198 /* Entry function for undoing inheritance/split transformation. Return true
5199 if we did any RTL change in this pass. */
5200 bool
5201 lra_undo_inheritance (void)
5203 unsigned int regno;
5204 int restore_regno, hard_regno;
5205 int n_all_inherit, n_inherit, n_all_split, n_split;
5206 bitmap_head remove_pseudos;
5207 bitmap_iterator bi;
5208 bool change_p;
5210 lra_undo_inheritance_iter++;
5211 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5212 return false;
5213 if (lra_dump_file != NULL)
5214 fprintf (lra_dump_file,
5215 "\n********** Undoing inheritance #%d: **********\n\n",
5216 lra_undo_inheritance_iter);
5217 bitmap_initialize (&remove_pseudos, &reg_obstack);
5218 n_inherit = n_all_inherit = 0;
5219 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5220 if (lra_reg_info[regno].restore_regno >= 0)
5222 n_all_inherit++;
5223 if (reg_renumber[regno] < 0)
5224 bitmap_set_bit (&remove_pseudos, regno);
5225 else
5226 n_inherit++;
5228 if (lra_dump_file != NULL && n_all_inherit != 0)
5229 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
5230 n_inherit, n_all_inherit,
5231 (double) n_inherit / n_all_inherit * 100);
5232 n_split = n_all_split = 0;
5233 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5234 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
5236 n_all_split++;
5237 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
5238 ? reg_renumber[restore_regno] : restore_regno);
5239 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
5240 bitmap_set_bit (&remove_pseudos, regno);
5241 else
5243 n_split++;
5244 if (lra_dump_file != NULL)
5245 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
5246 regno, restore_regno);
5249 if (lra_dump_file != NULL && n_all_split != 0)
5250 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
5251 n_split, n_all_split,
5252 (double) n_split / n_all_split * 100);
5253 change_p = remove_inheritance_pseudos (&remove_pseudos);
5254 bitmap_clear (&remove_pseudos);
5255 /* Clear restore_regnos. */
5256 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5257 lra_reg_info[regno].restore_regno = -1;
5258 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5259 lra_reg_info[regno].restore_regno = -1;
5260 return change_p;