Merge branches/gcc-4_8-branch rev 208968.
[official-gcc.git] / gcc-4_8-branch / gcc / lra-constraints.c
blob32e8c45f2874e4eac6b863a748d5d9b757432939
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 data (basic
139 block, the insn data, the insn static data, and the mode of each
140 operand). */
141 static rtx curr_insn;
142 static basic_block curr_bb;
143 static lra_insn_recog_data_t curr_id;
144 static struct lra_static_insn_data *curr_static_id;
145 static enum machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
149 /* Start numbers for new registers and insns at the current constraints
150 pass start. */
151 static int new_regno_start;
152 static int new_insn_uid_start;
154 /* If LOC is nonnull, strip any outer subreg from it. */
155 static inline rtx *
156 strip_subreg (rtx *loc)
158 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
161 /* Return hard regno of REGNO or if it is was not assigned to a hard
162 register, use a hard register from its allocno class. */
163 static int
164 get_try_hard_regno (int regno)
166 int hard_regno;
167 enum reg_class rclass;
169 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
170 hard_regno = lra_get_regno_hard_regno (regno);
171 if (hard_regno >= 0)
172 return hard_regno;
173 rclass = lra_get_allocno_class (regno);
174 if (rclass == NO_REGS)
175 return -1;
176 return ira_class_hard_regs[rclass][0];
179 /* Return final hard regno (plus offset) which will be after
180 elimination. We do this for matching constraints because the final
181 hard regno could have a different class. */
182 static int
183 get_final_hard_regno (int hard_regno, int offset)
185 if (hard_regno < 0)
186 return hard_regno;
187 hard_regno = lra_get_elimination_hard_regno (hard_regno);
188 return hard_regno + offset;
191 /* Return hard regno of X after removing subreg and making
192 elimination. If X is not a register or subreg of register, return
193 -1. For pseudo use its assignment. */
194 static int
195 get_hard_regno (rtx x)
197 rtx reg;
198 int offset, hard_regno;
200 reg = x;
201 if (GET_CODE (x) == SUBREG)
202 reg = SUBREG_REG (x);
203 if (! REG_P (reg))
204 return -1;
205 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
206 hard_regno = lra_get_regno_hard_regno (hard_regno);
207 if (hard_regno < 0)
208 return -1;
209 offset = 0;
210 if (GET_CODE (x) == SUBREG)
211 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
212 SUBREG_BYTE (x), GET_MODE (x));
213 return get_final_hard_regno (hard_regno, offset);
216 /* If REGNO is a hard register or has been allocated a hard register,
217 return the class of that register. If REGNO is a reload pseudo
218 created by the current constraints pass, return its allocno class.
219 Return NO_REGS otherwise. */
220 static enum reg_class
221 get_reg_class (int regno)
223 int hard_regno;
225 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
226 hard_regno = lra_get_regno_hard_regno (regno);
227 if (hard_regno >= 0)
229 hard_regno = get_final_hard_regno (hard_regno, 0);
230 return REGNO_REG_CLASS (hard_regno);
232 if (regno >= new_regno_start)
233 return lra_get_allocno_class (regno);
234 return NO_REGS;
237 /* Return true if REG satisfies (or will satisfy) reg class constraint
238 CL. Use elimination first if REG is a hard register. If REG is a
239 reload pseudo created by this constraints pass, assume that it will
240 be allocated a hard register from its allocno class, but allow that
241 class to be narrowed to CL if it is currently a superset of CL.
243 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
244 REGNO (reg), or NO_REGS if no change in its class was needed. */
245 static bool
246 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
248 enum reg_class rclass, common_class;
249 enum machine_mode reg_mode;
250 int class_size, hard_regno, nregs, i, j;
251 int regno = REGNO (reg);
253 if (new_class != NULL)
254 *new_class = NO_REGS;
255 if (regno < FIRST_PSEUDO_REGISTER)
257 rtx final_reg = reg;
258 rtx *final_loc = &final_reg;
260 lra_eliminate_reg_if_possible (final_loc);
261 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
263 reg_mode = GET_MODE (reg);
264 rclass = get_reg_class (regno);
265 if (regno < new_regno_start
266 /* Do not allow the constraints for reload instructions to
267 influence the classes of new pseudos. These reloads are
268 typically moves that have many alternatives, and restricting
269 reload pseudos for one alternative may lead to situations
270 where other reload pseudos are no longer allocatable. */
271 || INSN_UID (curr_insn) >= new_insn_uid_start)
272 /* When we don't know what class will be used finally for reload
273 pseudos, we use ALL_REGS. */
274 return ((regno >= new_regno_start && rclass == ALL_REGS)
275 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
276 && ! hard_reg_set_subset_p (reg_class_contents[cl],
277 lra_no_alloc_regs)));
278 else
280 common_class = ira_reg_class_subset[rclass][cl];
281 if (new_class != NULL)
282 *new_class = common_class;
283 if (hard_reg_set_subset_p (reg_class_contents[common_class],
284 lra_no_alloc_regs))
285 return false;
286 /* Check that there are enough allocatable regs. */
287 class_size = ira_class_hard_regs_num[common_class];
288 for (i = 0; i < class_size; i++)
290 hard_regno = ira_class_hard_regs[common_class][i];
291 nregs = hard_regno_nregs[hard_regno][reg_mode];
292 if (nregs == 1)
293 return true;
294 for (j = 0; j < nregs; j++)
295 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
296 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
297 hard_regno + j))
298 break;
299 if (j >= nregs)
300 return true;
302 return false;
306 /* Return true if REGNO satisfies a memory constraint. */
307 static bool
308 in_mem_p (int regno)
310 return get_reg_class (regno) == NO_REGS;
313 /* If we have decided to substitute X with another value, return that
314 value, otherwise return X. */
315 static rtx
316 get_equiv_substitution (rtx x)
318 int regno;
319 rtx res;
321 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
322 || ! ira_reg_equiv[regno].defined_p
323 || ! ira_reg_equiv[regno].profitable_p
324 || lra_get_regno_hard_regno (regno) >= 0)
325 return x;
326 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
327 return res;
328 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
329 return res;
330 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
331 return res;
332 gcc_unreachable ();
335 /* Set up curr_operand_mode. */
336 static void
337 init_curr_operand_mode (void)
339 int nop = curr_static_id->n_operands;
340 for (int i = 0; i < nop; i++)
342 enum machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
343 if (mode == VOIDmode)
345 /* The .md mode for address operands is the mode of the
346 addressed value rather than the mode of the address itself. */
347 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
348 mode = Pmode;
349 else
350 mode = curr_static_id->operand[i].mode;
352 curr_operand_mode[i] = mode;
358 /* The page contains code to reuse input reloads. */
360 /* Structure describes input reload of the current insns. */
361 struct input_reload
363 /* Reloaded value. */
364 rtx input;
365 /* Reload pseudo used. */
366 rtx reg;
369 /* The number of elements in the following array. */
370 static int curr_insn_input_reloads_num;
371 /* Array containing info about input reloads. It is used to find the
372 same input reload and reuse the reload pseudo in this case. */
373 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
375 /* Initiate data concerning reuse of input reloads for the current
376 insn. */
377 static void
378 init_curr_insn_input_reloads (void)
380 curr_insn_input_reloads_num = 0;
383 /* Change class of pseudo REGNO to NEW_CLASS. Print info about it
384 using TITLE. Output a new line if NL_P. */
385 static void
386 change_class (int regno, enum reg_class new_class,
387 const char *title, bool nl_p)
389 lra_assert (regno >= FIRST_PSEUDO_REGISTER);
390 if (lra_dump_file != NULL)
391 fprintf (lra_dump_file, "%s to class %s for r%d",
392 title, reg_class_names[new_class], regno);
393 setup_reg_classes (regno, new_class, NO_REGS, new_class);
394 if (lra_dump_file != NULL && nl_p)
395 fprintf (lra_dump_file, "\n");
398 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
399 created input reload pseudo (only if TYPE is not OP_OUT). The
400 result pseudo is returned through RESULT_REG. Return TRUE if we
401 created a new pseudo, FALSE if we reused the already created input
402 reload pseudo. Use TITLE to describe new registers for debug
403 purposes. */
404 static bool
405 get_reload_reg (enum op_type type, enum machine_mode mode, rtx original,
406 enum reg_class rclass, const char *title, rtx *result_reg)
408 int i, regno;
409 enum reg_class new_class;
411 if (type == OP_OUT)
413 *result_reg
414 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
415 return true;
417 /* Prevent reuse value of expression with side effects,
418 e.g. volatile memory. */
419 if (! side_effects_p (original))
420 for (i = 0; i < curr_insn_input_reloads_num; i++)
421 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
422 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
424 rtx reg = curr_insn_input_reloads[i].reg;
425 regno = REGNO (reg);
426 /* If input is equal to original and both are VOIDmode,
427 GET_MODE (reg) might be still different from mode.
428 Ensure we don't return *result_reg with wrong mode. */
429 if (GET_MODE (reg) != mode)
431 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
432 continue;
433 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
434 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
435 continue;
437 *result_reg = reg;
438 if (lra_dump_file != NULL)
440 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
441 dump_value_slim (lra_dump_file, original, 1);
443 if (new_class != lra_get_allocno_class (regno))
444 change_class (regno, new_class, ", change", false);
445 if (lra_dump_file != NULL)
446 fprintf (lra_dump_file, "\n");
447 return false;
449 *result_reg = lra_create_new_reg (mode, original, rclass, title);
450 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
451 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
452 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
453 return true;
458 /* The page contains code to extract memory address parts. */
460 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
461 static inline bool
462 ok_for_index_p_nonstrict (rtx reg)
464 unsigned regno = REGNO (reg);
466 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
469 /* A version of regno_ok_for_base_p for use here, when all pseudos
470 should count as OK. Arguments as for regno_ok_for_base_p. */
471 static inline bool
472 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode, addr_space_t as,
473 enum rtx_code outer_code, enum rtx_code index_code)
475 unsigned regno = REGNO (reg);
477 if (regno >= FIRST_PSEUDO_REGISTER)
478 return true;
479 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
484 /* The page contains major code to choose the current insn alternative
485 and generate reloads for it. */
487 /* Return the offset from REGNO of the least significant register
488 in (reg:MODE REGNO).
490 This function is used to tell whether two registers satisfy
491 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
493 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
494 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
496 lra_constraint_offset (int regno, enum machine_mode mode)
498 lra_assert (regno < FIRST_PSEUDO_REGISTER);
499 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
500 && SCALAR_INT_MODE_P (mode))
501 return hard_regno_nregs[regno][mode] - 1;
502 return 0;
505 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
506 if they are the same hard reg, and has special hacks for
507 auto-increment and auto-decrement. This is specifically intended for
508 process_alt_operands to use in determining whether two operands
509 match. X is the operand whose number is the lower of the two.
511 It is supposed that X is the output operand and Y is the input
512 operand. Y_HARD_REGNO is the final hard regno of register Y or
513 register in subreg Y as we know it now. Otherwise, it is a
514 negative value. */
515 static bool
516 operands_match_p (rtx x, rtx y, int y_hard_regno)
518 int i;
519 RTX_CODE code = GET_CODE (x);
520 const char *fmt;
522 if (x == y)
523 return true;
524 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
525 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
527 int j;
529 i = get_hard_regno (x);
530 if (i < 0)
531 goto slow;
533 if ((j = y_hard_regno) < 0)
534 goto slow;
536 i += lra_constraint_offset (i, GET_MODE (x));
537 j += lra_constraint_offset (j, GET_MODE (y));
539 return i == j;
542 /* If two operands must match, because they are really a single
543 operand of an assembler insn, then two post-increments are invalid
544 because the assembler insn would increment only once. On the
545 other hand, a post-increment matches ordinary indexing if the
546 post-increment is the output operand. */
547 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
548 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
550 /* Two pre-increments are invalid because the assembler insn would
551 increment only once. On the other hand, a pre-increment matches
552 ordinary indexing if the pre-increment is the input operand. */
553 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
554 || GET_CODE (y) == PRE_MODIFY)
555 return operands_match_p (x, XEXP (y, 0), -1);
557 slow:
559 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
560 && x == SUBREG_REG (y))
561 return true;
562 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
563 && SUBREG_REG (x) == y)
564 return true;
566 /* Now we have disposed of all the cases in which different rtx
567 codes can match. */
568 if (code != GET_CODE (y))
569 return false;
571 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
572 if (GET_MODE (x) != GET_MODE (y))
573 return false;
575 switch (code)
577 CASE_CONST_UNIQUE:
578 return false;
580 case LABEL_REF:
581 return XEXP (x, 0) == XEXP (y, 0);
582 case SYMBOL_REF:
583 return XSTR (x, 0) == XSTR (y, 0);
585 default:
586 break;
589 /* Compare the elements. If any pair of corresponding elements fail
590 to match, return false for the whole things. */
592 fmt = GET_RTX_FORMAT (code);
593 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
595 int val, j;
596 switch (fmt[i])
598 case 'w':
599 if (XWINT (x, i) != XWINT (y, i))
600 return false;
601 break;
603 case 'i':
604 if (XINT (x, i) != XINT (y, i))
605 return false;
606 break;
608 case 'e':
609 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
610 if (val == 0)
611 return false;
612 break;
614 case '0':
615 break;
617 case 'E':
618 if (XVECLEN (x, i) != XVECLEN (y, i))
619 return false;
620 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
622 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
623 if (val == 0)
624 return false;
626 break;
628 /* It is believed that rtx's at this level will never
629 contain anything but integers and other rtx's, except for
630 within LABEL_REFs and SYMBOL_REFs. */
631 default:
632 gcc_unreachable ();
635 return true;
638 /* True if X is a constant that can be forced into the constant pool.
639 MODE is the mode of the operand, or VOIDmode if not known. */
640 #define CONST_POOL_OK_P(MODE, X) \
641 ((MODE) != VOIDmode \
642 && CONSTANT_P (X) \
643 && GET_CODE (X) != HIGH \
644 && !targetm.cannot_force_const_mem (MODE, X))
646 /* True if C is a non-empty register class that has too few registers
647 to be safely used as a reload target class. */
648 #define SMALL_REGISTER_CLASS_P(C) \
649 (reg_class_size [(C)] == 1 \
650 || (reg_class_size [(C)] >= 1 && targetm.class_likely_spilled_p (C)))
652 /* If REG is a reload pseudo, try to make its class satisfying CL. */
653 static void
654 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
656 enum reg_class rclass;
658 /* Do not make more accurate class from reloads generated. They are
659 mostly moves with a lot of constraints. Making more accurate
660 class may results in very narrow class and impossibility of find
661 registers for several reloads of one insn. */
662 if (INSN_UID (curr_insn) >= new_insn_uid_start)
663 return;
664 if (GET_CODE (reg) == SUBREG)
665 reg = SUBREG_REG (reg);
666 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
667 return;
668 if (in_class_p (reg, cl, &rclass) && rclass != cl)
669 change_class (REGNO (reg), rclass, " Change", true);
672 /* Generate reloads for matching OUT and INS (array of input operand
673 numbers with end marker -1) with reg class GOAL_CLASS. Add input
674 and output reloads correspondingly to the lists *BEFORE and *AFTER.
675 OUT might be negative. In this case we generate input reloads for
676 matched input operands INS. */
677 static void
678 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
679 rtx *before, rtx *after)
681 int i, in;
682 rtx new_in_reg, new_out_reg, reg, clobber;
683 enum machine_mode inmode, outmode;
684 rtx in_rtx = *curr_id->operand_loc[ins[0]];
685 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
687 inmode = curr_operand_mode[ins[0]];
688 outmode = out < 0 ? inmode : curr_operand_mode[out];
689 push_to_sequence (*before);
690 if (inmode != outmode)
692 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
694 reg = new_in_reg
695 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
696 goal_class, "");
697 if (SCALAR_INT_MODE_P (inmode))
698 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
699 else
700 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
701 /* If the input reg is dying here, we can use the same hard
702 register for REG and IN_RTX. We do it only for original
703 pseudos as reload pseudos can die although original
704 pseudos still live where reload pseudos dies. */
705 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
706 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
707 lra_reg_info[REGNO (reg)].val = lra_reg_info[REGNO (in_rtx)].val;
709 else
711 reg = new_out_reg
712 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
713 goal_class, "");
714 if (SCALAR_INT_MODE_P (outmode))
715 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
716 else
717 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
718 /* NEW_IN_REG is non-paradoxical subreg. We don't want
719 NEW_OUT_REG living above. We add clobber clause for
720 this. This is just a temporary clobber. We can remove
721 it at the end of LRA work. */
722 clobber = emit_clobber (new_out_reg);
723 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
724 if (GET_CODE (in_rtx) == SUBREG)
726 rtx subreg_reg = SUBREG_REG (in_rtx);
728 /* If SUBREG_REG is dying here and sub-registers IN_RTX
729 and NEW_IN_REG are similar, we can use the same hard
730 register for REG and SUBREG_REG. */
731 if (REG_P (subreg_reg)
732 && (int) REGNO (subreg_reg) < lra_new_regno_start
733 && GET_MODE (subreg_reg) == outmode
734 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
735 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
736 lra_reg_info[REGNO (reg)].val
737 = lra_reg_info[REGNO (subreg_reg)].val;
741 else
743 /* Pseudos have values -- see comments for lra_reg_info.
744 Different pseudos with the same value do not conflict even if
745 they live in the same place. When we create a pseudo we
746 assign value of original pseudo (if any) from which we
747 created the new pseudo. If we create the pseudo from the
748 input pseudo, the new pseudo will no conflict with the input
749 pseudo which is wrong when the input pseudo lives after the
750 insn and as the new pseudo value is changed by the insn
751 output. Therefore we create the new pseudo from the output.
753 We cannot reuse the current output register because we might
754 have a situation like "a <- a op b", where the constraints
755 force the second input operand ("b") to match the output
756 operand ("a"). "b" must then be copied into a new register
757 so that it doesn't clobber the current value of "a". */
759 new_in_reg = new_out_reg
760 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
761 goal_class, "");
763 /* In operand can be got from transformations before processing insn
764 constraints. One example of such transformations is subreg
765 reloading (see function simplify_operand_subreg). The new
766 pseudos created by the transformations might have inaccurate
767 class (ALL_REGS) and we should make their classes more
768 accurate. */
769 narrow_reload_pseudo_class (in_rtx, goal_class);
770 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
771 *before = get_insns ();
772 end_sequence ();
773 for (i = 0; (in = ins[i]) >= 0; i++)
775 lra_assert
776 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
777 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
778 *curr_id->operand_loc[in] = new_in_reg;
780 lra_update_dups (curr_id, ins);
781 if (out < 0)
782 return;
783 /* See a comment for the input operand above. */
784 narrow_reload_pseudo_class (out_rtx, goal_class);
785 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
787 start_sequence ();
788 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
789 emit_insn (*after);
790 *after = get_insns ();
791 end_sequence ();
793 *curr_id->operand_loc[out] = new_out_reg;
794 lra_update_dup (curr_id, out);
797 /* Return register class which is union of all reg classes in insn
798 constraint alternative string starting with P. */
799 static enum reg_class
800 reg_class_from_constraints (const char *p)
802 int c, len;
803 enum reg_class op_class = NO_REGS;
806 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
808 case '#':
809 case ',':
810 return op_class;
812 case 'p':
813 op_class = (reg_class_subunion
814 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
815 ADDRESS, SCRATCH)]);
816 break;
818 case 'g':
819 case 'r':
820 op_class = reg_class_subunion[op_class][GENERAL_REGS];
821 break;
823 default:
824 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
826 #ifdef EXTRA_CONSTRAINT_STR
827 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
828 op_class
829 = (reg_class_subunion
830 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
831 ADDRESS, SCRATCH)]);
832 #endif
833 break;
836 op_class
837 = reg_class_subunion[op_class][REG_CLASS_FROM_CONSTRAINT (c, p)];
838 break;
840 while ((p += len), c);
841 return op_class;
844 /* If OP is a register, return the class of the register as per
845 get_reg_class, otherwise return NO_REGS. */
846 static inline enum reg_class
847 get_op_class (rtx op)
849 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
852 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
853 otherwise. If modes of MEM_PSEUDO and VAL are different, use
854 SUBREG for VAL to make them equal. */
855 static rtx
856 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
858 if (GET_MODE (mem_pseudo) != GET_MODE (val))
859 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
860 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
862 return (to_p
863 ? gen_move_insn (mem_pseudo, val)
864 : gen_move_insn (val, mem_pseudo));
867 /* Process a special case insn (register move), return true if we
868 don't need to process it anymore. Return that RTL was changed
869 through CHANGE_P and macro SECONDARY_MEMORY_NEEDED says to use
870 secondary memory through SEC_MEM_P. */
871 static bool
872 check_and_process_move (bool *change_p, bool *sec_mem_p)
874 int sregno, dregno;
875 rtx set, dest, src, dreg, sreg, old_sreg, new_reg, before, scratch_reg;
876 enum reg_class dclass, sclass, secondary_class;
877 enum machine_mode sreg_mode;
878 secondary_reload_info sri;
880 *sec_mem_p = *change_p = false;
881 if ((set = single_set (curr_insn)) == NULL)
882 return false;
883 dreg = dest = SET_DEST (set);
884 sreg = src = SET_SRC (set);
885 /* Quick check on the right move insn which does not need
886 reloads. */
887 if ((dclass = get_op_class (dest)) != NO_REGS
888 && (sclass = get_op_class (src)) != NO_REGS
889 /* The backend guarantees that register moves of cost 2 never
890 need reloads. */
891 && targetm.register_move_cost (GET_MODE (src), dclass, sclass) == 2)
892 return true;
893 if (GET_CODE (dest) == SUBREG)
894 dreg = SUBREG_REG (dest);
895 if (GET_CODE (src) == SUBREG)
896 sreg = SUBREG_REG (src);
897 if (! REG_P (dreg) || ! REG_P (sreg))
898 return false;
899 sclass = dclass = NO_REGS;
900 dreg = get_equiv_substitution (dreg);
901 if (REG_P (dreg))
902 dclass = get_reg_class (REGNO (dreg));
903 if (dclass == ALL_REGS)
904 /* ALL_REGS is used for new pseudos created by transformations
905 like reload of SUBREG_REG (see function
906 simplify_operand_subreg). We don't know their class yet. We
907 should figure out the class from processing the insn
908 constraints not in this fast path function. Even if ALL_REGS
909 were a right class for the pseudo, secondary_... hooks usually
910 are not define for ALL_REGS. */
911 return false;
912 sreg_mode = GET_MODE (sreg);
913 old_sreg = sreg;
914 sreg = get_equiv_substitution (sreg);
915 if (REG_P (sreg))
916 sclass = get_reg_class (REGNO (sreg));
917 if (sclass == ALL_REGS)
918 /* See comments above. */
919 return false;
920 #ifdef SECONDARY_MEMORY_NEEDED
921 if (dclass != NO_REGS && sclass != NO_REGS
922 && SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src)))
924 *sec_mem_p = true;
925 return false;
927 #endif
928 sri.prev_sri = NULL;
929 sri.icode = CODE_FOR_nothing;
930 sri.extra_cost = 0;
931 secondary_class = NO_REGS;
932 /* Set up hard register for a reload pseudo for hook
933 secondary_reload because some targets just ignore unassigned
934 pseudos in the hook. */
935 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
937 dregno = REGNO (dreg);
938 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
940 else
941 dregno = -1;
942 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
944 sregno = REGNO (sreg);
945 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
947 else
948 sregno = -1;
949 if (sclass != NO_REGS)
950 secondary_class
951 = (enum reg_class) targetm.secondary_reload (false, dest,
952 (reg_class_t) sclass,
953 GET_MODE (src), &sri);
954 if (sclass == NO_REGS
955 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
956 && dclass != NO_REGS))
958 enum reg_class old_sclass = secondary_class;
959 secondary_reload_info old_sri = sri;
961 sri.prev_sri = NULL;
962 sri.icode = CODE_FOR_nothing;
963 sri.extra_cost = 0;
964 secondary_class
965 = (enum reg_class) targetm.secondary_reload (true, sreg,
966 (reg_class_t) dclass,
967 sreg_mode, &sri);
968 /* Check the target hook consistency. */
969 lra_assert
970 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
971 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
972 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
974 if (sregno >= 0)
975 reg_renumber [sregno] = -1;
976 if (dregno >= 0)
977 reg_renumber [dregno] = -1;
978 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
979 return false;
980 *change_p = true;
981 new_reg = NULL_RTX;
982 if (secondary_class != NO_REGS)
983 new_reg = lra_create_new_reg_with_unique_value (sreg_mode, NULL_RTX,
984 secondary_class,
985 "secondary");
986 start_sequence ();
987 if (old_sreg != sreg)
988 sreg = copy_rtx (sreg);
989 if (sri.icode == CODE_FOR_nothing)
990 lra_emit_move (new_reg, sreg);
991 else
993 enum reg_class scratch_class;
995 scratch_class = (reg_class_from_constraints
996 (insn_data[sri.icode].operand[2].constraint));
997 scratch_reg = (lra_create_new_reg_with_unique_value
998 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
999 scratch_class, "scratch"));
1000 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1001 sreg, scratch_reg));
1003 before = get_insns ();
1004 end_sequence ();
1005 lra_process_new_insns (curr_insn, before, NULL_RTX, "Inserting the move");
1006 if (new_reg != NULL_RTX)
1008 if (GET_CODE (src) == SUBREG)
1009 SUBREG_REG (src) = new_reg;
1010 else
1011 SET_SRC (set) = new_reg;
1013 else
1015 if (lra_dump_file != NULL)
1017 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1018 dump_insn_slim (lra_dump_file, curr_insn);
1020 lra_set_insn_deleted (curr_insn);
1021 return true;
1023 return false;
1026 /* The following data describe the result of process_alt_operands.
1027 The data are used in curr_insn_transform to generate reloads. */
1029 /* The chosen reg classes which should be used for the corresponding
1030 operands. */
1031 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1032 /* True if the operand should be the same as another operand and that
1033 other operand does not need a reload. */
1034 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1035 /* True if the operand does not need a reload. */
1036 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1037 /* True if the operand can be offsetable memory. */
1038 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1039 /* The number of an operand to which given operand can be matched to. */
1040 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1041 /* The number of elements in the following array. */
1042 static int goal_alt_dont_inherit_ops_num;
1043 /* Numbers of operands whose reload pseudos should not be inherited. */
1044 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1045 /* True if the insn commutative operands should be swapped. */
1046 static bool goal_alt_swapped;
1047 /* The chosen insn alternative. */
1048 static int goal_alt_number;
1050 /* The following five variables are used to choose the best insn
1051 alternative. They reflect final characteristics of the best
1052 alternative. */
1054 /* Number of necessary reloads and overall cost reflecting the
1055 previous value and other unpleasantness of the best alternative. */
1056 static int best_losers, best_overall;
1057 /* Overall number hard registers used for reloads. For example, on
1058 some targets we need 2 general registers to reload DFmode and only
1059 one floating point register. */
1060 static int best_reload_nregs;
1061 /* Overall number reflecting distances of previous reloading the same
1062 value. The distances are counted from the current BB start. It is
1063 used to improve inheritance chances. */
1064 static int best_reload_sum;
1066 /* True if the current insn should have no correspondingly input or
1067 output reloads. */
1068 static bool no_input_reloads_p, no_output_reloads_p;
1070 /* True if we swapped the commutative operands in the current
1071 insn. */
1072 static int curr_swapped;
1074 /* Arrange for address element *LOC to be a register of class CL.
1075 Add any input reloads to list BEFORE. AFTER is nonnull if *LOC is an
1076 automodified value; handle that case by adding the required output
1077 reloads to list AFTER. Return true if the RTL was changed. */
1078 static bool
1079 process_addr_reg (rtx *loc, rtx *before, rtx *after, enum reg_class cl)
1081 int regno;
1082 enum reg_class rclass, new_class;
1083 rtx reg;
1084 rtx new_reg;
1085 enum machine_mode mode;
1086 bool before_p = false;
1088 loc = strip_subreg (loc);
1089 reg = *loc;
1090 mode = GET_MODE (reg);
1091 if (! REG_P (reg))
1093 /* Always reload memory in an address even if the target supports
1094 such addresses. */
1095 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1096 before_p = true;
1098 else
1100 regno = REGNO (reg);
1101 rclass = get_reg_class (regno);
1102 if ((*loc = get_equiv_substitution (reg)) != reg)
1104 if (lra_dump_file != NULL)
1106 fprintf (lra_dump_file,
1107 "Changing pseudo %d in address of insn %u on equiv ",
1108 REGNO (reg), INSN_UID (curr_insn));
1109 dump_value_slim (lra_dump_file, *loc, 1);
1110 fprintf (lra_dump_file, "\n");
1112 *loc = copy_rtx (*loc);
1114 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1116 reg = *loc;
1117 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1118 mode, reg, cl, "address", &new_reg))
1119 before_p = true;
1121 else if (new_class != NO_REGS && rclass != new_class)
1123 change_class (regno, new_class, " Change", true);
1124 return false;
1126 else
1127 return false;
1129 if (before_p)
1131 push_to_sequence (*before);
1132 lra_emit_move (new_reg, reg);
1133 *before = get_insns ();
1134 end_sequence ();
1136 *loc = new_reg;
1137 if (after != NULL)
1139 start_sequence ();
1140 lra_emit_move (reg, new_reg);
1141 emit_insn (*after);
1142 *after = get_insns ();
1143 end_sequence ();
1145 return true;
1148 /* Make reloads for subreg in operand NOP with internal subreg mode
1149 REG_MODE, add new reloads for further processing. Return true if
1150 any reload was generated. */
1151 static bool
1152 simplify_operand_subreg (int nop, enum machine_mode reg_mode)
1154 int hard_regno;
1155 rtx before, after;
1156 enum machine_mode mode;
1157 rtx reg, new_reg;
1158 rtx operand = *curr_id->operand_loc[nop];
1160 before = after = NULL_RTX;
1162 if (GET_CODE (operand) != SUBREG)
1163 return false;
1165 mode = GET_MODE (operand);
1166 reg = SUBREG_REG (operand);
1167 /* If we change address for paradoxical subreg of memory, the
1168 address might violate the necessary alignment or the access might
1169 be slow. So take this into consideration. We should not worry
1170 about access beyond allocated memory for paradoxical memory
1171 subregs as we don't substitute such equiv memory (see processing
1172 equivalences in function lra_constraints) and because for spilled
1173 pseudos we allocate stack memory enough for the biggest
1174 corresponding paradoxical subreg. */
1175 if ((MEM_P (reg)
1176 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1177 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1178 || (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER))
1180 alter_subreg (curr_id->operand_loc[nop], false);
1181 return true;
1183 /* Put constant into memory when we have mixed modes. It generates
1184 a better code in most cases as it does not need a secondary
1185 reload memory. It also prevents LRA looping when LRA is using
1186 secondary reload memory again and again. */
1187 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1188 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1190 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1191 alter_subreg (curr_id->operand_loc[nop], false);
1192 return true;
1194 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1195 if there may be a problem accessing OPERAND in the outer
1196 mode. */
1197 if ((REG_P (reg)
1198 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1199 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1200 /* Don't reload paradoxical subregs because we could be looping
1201 having repeatedly final regno out of hard regs range. */
1202 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1203 >= hard_regno_nregs[hard_regno][mode])
1204 && simplify_subreg_regno (hard_regno, GET_MODE (reg),
1205 SUBREG_BYTE (operand), mode) < 0)
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 /* Major function to choose the current insn alternative and what
1313 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1314 negative we should consider only this alternative. Return false if
1315 we can not choose the alternative or find how to reload the
1316 operands. */
1317 static bool
1318 process_alt_operands (int only_alternative)
1320 bool ok_p = false;
1321 int nop, overall, nalt;
1322 int n_alternatives = curr_static_id->n_alternatives;
1323 int n_operands = curr_static_id->n_operands;
1324 /* LOSERS counts the operands that don't fit this alternative and
1325 would require loading. */
1326 int losers;
1327 /* REJECT is a count of how undesirable this alternative says it is
1328 if any reloading is required. If the alternative matches exactly
1329 then REJECT is ignored, but otherwise it gets this much counted
1330 against it in addition to the reloading needed. */
1331 int reject;
1332 /* The number of elements in the following array. */
1333 int early_clobbered_regs_num;
1334 /* Numbers of operands which are early clobber registers. */
1335 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1336 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1337 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1338 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1339 bool curr_alt_win[MAX_RECOG_OPERANDS];
1340 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1341 int curr_alt_matches[MAX_RECOG_OPERANDS];
1342 /* The number of elements in the following array. */
1343 int curr_alt_dont_inherit_ops_num;
1344 /* Numbers of operands whose reload pseudos should not be inherited. */
1345 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1346 rtx op;
1347 /* The register when the operand is a subreg of register, otherwise the
1348 operand itself. */
1349 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1350 /* The register if the operand is a register or subreg of register,
1351 otherwise NULL. */
1352 rtx operand_reg[MAX_RECOG_OPERANDS];
1353 int hard_regno[MAX_RECOG_OPERANDS];
1354 enum machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1355 int reload_nregs, reload_sum;
1356 bool costly_p;
1357 enum reg_class cl;
1359 /* Calculate some data common for all alternatives to speed up the
1360 function. */
1361 for (nop = 0; nop < n_operands; nop++)
1363 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1364 /* The real hard regno of the operand after the allocation. */
1365 hard_regno[nop] = get_hard_regno (op);
1367 operand_reg[nop] = op;
1368 biggest_mode[nop] = GET_MODE (operand_reg[nop]);
1369 if (GET_CODE (operand_reg[nop]) == SUBREG)
1371 operand_reg[nop] = SUBREG_REG (operand_reg[nop]);
1372 if (GET_MODE_SIZE (biggest_mode[nop])
1373 < GET_MODE_SIZE (GET_MODE (operand_reg[nop])))
1374 biggest_mode[nop] = GET_MODE (operand_reg[nop]);
1376 if (REG_P (operand_reg[nop]))
1377 no_subreg_reg_operand[nop] = operand_reg[nop];
1378 else
1379 operand_reg[nop] = NULL_RTX;
1382 /* The constraints are made of several alternatives. Each operand's
1383 constraint looks like foo,bar,... with commas separating the
1384 alternatives. The first alternatives for all operands go
1385 together, the second alternatives go together, etc.
1387 First loop over alternatives. */
1388 for (nalt = 0; nalt < n_alternatives; nalt++)
1390 /* Loop over operands for one constraint alternative. */
1391 #if HAVE_ATTR_enabled
1392 if (curr_id->alternative_enabled_p != NULL
1393 && ! curr_id->alternative_enabled_p[nalt])
1394 continue;
1395 #endif
1397 if (only_alternative >= 0 && nalt != only_alternative)
1398 continue;
1401 overall = losers = reject = reload_nregs = reload_sum = 0;
1402 for (nop = 0; nop < n_operands; nop++)
1403 reject += (curr_static_id
1404 ->operand_alternative[nalt * n_operands + nop].reject);
1405 early_clobbered_regs_num = 0;
1407 for (nop = 0; nop < n_operands; nop++)
1409 const char *p;
1410 char *end;
1411 int len, c, m, i, opalt_num, this_alternative_matches;
1412 bool win, did_match, offmemok, early_clobber_p;
1413 /* false => this operand can be reloaded somehow for this
1414 alternative. */
1415 bool badop;
1416 /* true => this operand can be reloaded if the alternative
1417 allows regs. */
1418 bool winreg;
1419 /* True if a constant forced into memory would be OK for
1420 this operand. */
1421 bool constmemok;
1422 enum reg_class this_alternative, this_costly_alternative;
1423 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1424 bool this_alternative_match_win, this_alternative_win;
1425 bool this_alternative_offmemok;
1426 enum machine_mode mode;
1428 opalt_num = nalt * n_operands + nop;
1429 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1431 /* Fast track for no constraints at all. */
1432 curr_alt[nop] = NO_REGS;
1433 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1434 curr_alt_win[nop] = true;
1435 curr_alt_match_win[nop] = false;
1436 curr_alt_offmemok[nop] = false;
1437 curr_alt_matches[nop] = -1;
1438 continue;
1441 op = no_subreg_reg_operand[nop];
1442 mode = curr_operand_mode[nop];
1444 win = did_match = winreg = offmemok = constmemok = false;
1445 badop = true;
1447 early_clobber_p = false;
1448 p = curr_static_id->operand_alternative[opalt_num].constraint;
1450 this_costly_alternative = this_alternative = NO_REGS;
1451 /* We update set of possible hard regs besides its class
1452 because reg class might be inaccurate. For example,
1453 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1454 is translated in HI_REGS because classes are merged by
1455 pairs and there is no accurate intermediate class. */
1456 CLEAR_HARD_REG_SET (this_alternative_set);
1457 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1458 this_alternative_win = false;
1459 this_alternative_match_win = false;
1460 this_alternative_offmemok = false;
1461 this_alternative_matches = -1;
1463 /* An empty constraint should be excluded by the fast
1464 track. */
1465 lra_assert (*p != 0 && *p != ',');
1467 /* Scan this alternative's specs for this operand; set WIN
1468 if the operand fits any letter in this alternative.
1469 Otherwise, clear BADOP if this operand could fit some
1470 letter after reloads, or set WINREG if this operand could
1471 fit after reloads provided the constraint allows some
1472 registers. */
1473 costly_p = false;
1476 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1478 case '\0':
1479 len = 0;
1480 break;
1481 case ',':
1482 c = '\0';
1483 break;
1485 case '=': case '+': case '?': case '*': case '!':
1486 case ' ': case '\t':
1487 break;
1489 case '%':
1490 /* We only support one commutative marker, the first
1491 one. We already set commutative above. */
1492 break;
1494 case '&':
1495 early_clobber_p = true;
1496 break;
1498 case '#':
1499 /* Ignore rest of this alternative. */
1500 c = '\0';
1501 break;
1503 case '0': case '1': case '2': case '3': case '4':
1504 case '5': case '6': case '7': case '8': case '9':
1506 int m_hregno;
1507 bool match_p;
1509 m = strtoul (p, &end, 10);
1510 p = end;
1511 len = 0;
1512 lra_assert (nop > m);
1514 this_alternative_matches = m;
1515 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1516 /* We are supposed to match a previous operand.
1517 If we do, we win if that one did. If we do
1518 not, count both of the operands as losers.
1519 (This is too conservative, since most of the
1520 time only a single reload insn will be needed
1521 to make the two operands win. As a result,
1522 this alternative may be rejected when it is
1523 actually desirable.) */
1524 match_p = false;
1525 if (operands_match_p (*curr_id->operand_loc[nop],
1526 *curr_id->operand_loc[m], m_hregno))
1528 /* We should reject matching of an early
1529 clobber operand if the matching operand is
1530 not dying in the insn. */
1531 if (! curr_static_id->operand[m].early_clobber
1532 || operand_reg[nop] == NULL_RTX
1533 || (find_regno_note (curr_insn, REG_DEAD,
1534 REGNO (op))
1535 || REGNO (op) == REGNO (operand_reg[m])))
1536 match_p = true;
1538 if (match_p)
1540 /* If we are matching a non-offsettable
1541 address where an offsettable address was
1542 expected, then we must reject this
1543 combination, because we can't reload
1544 it. */
1545 if (curr_alt_offmemok[m]
1546 && MEM_P (*curr_id->operand_loc[m])
1547 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1548 continue;
1551 else
1553 /* Operands don't match. Both operands must
1554 allow a reload register, otherwise we
1555 cannot make them match. */
1556 if (curr_alt[m] == NO_REGS)
1557 break;
1558 /* Retroactively mark the operand we had to
1559 match as a loser, if it wasn't already and
1560 it wasn't matched to a register constraint
1561 (e.g it might be matched by memory). */
1562 if (curr_alt_win[m]
1563 && (operand_reg[m] == NULL_RTX
1564 || hard_regno[m] < 0))
1566 losers++;
1567 reload_nregs
1568 += (ira_reg_class_max_nregs[curr_alt[m]]
1569 [GET_MODE (*curr_id->operand_loc[m])]);
1572 /* We prefer no matching alternatives because
1573 it gives more freedom in RA. */
1574 if (operand_reg[nop] == NULL_RTX
1575 || (find_regno_note (curr_insn, REG_DEAD,
1576 REGNO (operand_reg[nop]))
1577 == NULL_RTX))
1578 reject += 2;
1580 /* If we have to reload this operand and some
1581 previous operand also had to match the same
1582 thing as this operand, we don't know how to do
1583 that. */
1584 if (!match_p || !curr_alt_win[m])
1586 for (i = 0; i < nop; i++)
1587 if (curr_alt_matches[i] == m)
1588 break;
1589 if (i < nop)
1590 break;
1592 else
1593 did_match = true;
1595 /* This can be fixed with reloads if the operand
1596 we are supposed to match can be fixed with
1597 reloads. */
1598 badop = false;
1599 this_alternative = curr_alt[m];
1600 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1601 winreg = this_alternative != NO_REGS;
1602 break;
1605 case 'p':
1606 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1607 ADDRESS, SCRATCH);
1608 this_alternative = reg_class_subunion[this_alternative][cl];
1609 IOR_HARD_REG_SET (this_alternative_set,
1610 reg_class_contents[cl]);
1611 if (costly_p)
1613 this_costly_alternative
1614 = reg_class_subunion[this_costly_alternative][cl];
1615 IOR_HARD_REG_SET (this_costly_alternative_set,
1616 reg_class_contents[cl]);
1618 win = true;
1619 badop = false;
1620 break;
1622 case TARGET_MEM_CONSTRAINT:
1623 if (MEM_P (op) || spilled_pseudo_p (op))
1624 win = true;
1625 /* We can put constant or pseudo value into memory
1626 to satisfy the constraint. */
1627 if (CONST_POOL_OK_P (mode, op) || REG_P (op))
1628 badop = false;
1629 constmemok = true;
1630 break;
1632 case '<':
1633 if (MEM_P (op)
1634 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1635 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1636 win = true;
1637 break;
1639 case '>':
1640 if (MEM_P (op)
1641 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1642 || GET_CODE (XEXP (op, 0)) == POST_INC))
1643 win = true;
1644 break;
1646 /* Memory op whose address is not offsettable. */
1647 case 'V':
1648 if (MEM_P (op)
1649 && ! offsettable_nonstrict_memref_p (op))
1650 win = true;
1651 break;
1653 /* Memory operand whose address is offsettable. */
1654 case 'o':
1655 if ((MEM_P (op)
1656 && offsettable_nonstrict_memref_p (op))
1657 || spilled_pseudo_p (op))
1658 win = true;
1659 /* We can put constant or pseudo value into memory
1660 or make memory address offsetable to satisfy the
1661 constraint. */
1662 if (CONST_POOL_OK_P (mode, op) || MEM_P (op) || REG_P (op))
1663 badop = false;
1664 constmemok = true;
1665 offmemok = true;
1666 break;
1668 case 'E':
1669 case 'F':
1670 if (GET_CODE (op) == CONST_DOUBLE
1671 || (GET_CODE (op) == CONST_VECTOR
1672 && (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)))
1673 win = true;
1674 break;
1676 case 'G':
1677 case 'H':
1678 if (GET_CODE (op) == CONST_DOUBLE
1679 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1680 win = true;
1681 break;
1683 case 's':
1684 if (CONST_INT_P (op)
1685 || (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode))
1686 break;
1688 case 'i':
1689 if (general_constant_p (op))
1690 win = true;
1691 break;
1693 case 'n':
1694 if (CONST_INT_P (op)
1695 || (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode))
1696 win = true;
1697 break;
1699 case 'I':
1700 case 'J':
1701 case 'K':
1702 case 'L':
1703 case 'M':
1704 case 'N':
1705 case 'O':
1706 case 'P':
1707 if (CONST_INT_P (op)
1708 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1709 win = true;
1710 break;
1712 case 'X':
1713 /* This constraint should be excluded by the fast
1714 track. */
1715 gcc_unreachable ();
1716 break;
1718 case 'g':
1719 if (MEM_P (op)
1720 || general_constant_p (op)
1721 || spilled_pseudo_p (op))
1722 win = true;
1723 /* Drop through into 'r' case. */
1725 case 'r':
1726 this_alternative
1727 = reg_class_subunion[this_alternative][GENERAL_REGS];
1728 IOR_HARD_REG_SET (this_alternative_set,
1729 reg_class_contents[GENERAL_REGS]);
1730 if (costly_p)
1732 this_costly_alternative
1733 = (reg_class_subunion
1734 [this_costly_alternative][GENERAL_REGS]);
1735 IOR_HARD_REG_SET (this_costly_alternative_set,
1736 reg_class_contents[GENERAL_REGS]);
1738 goto reg;
1740 default:
1741 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
1743 #ifdef EXTRA_CONSTRAINT_STR
1744 if (EXTRA_MEMORY_CONSTRAINT (c, p))
1746 if (EXTRA_CONSTRAINT_STR (op, c, p))
1747 win = true;
1748 else if (spilled_pseudo_p (op))
1749 win = true;
1751 /* If we didn't already win, we can reload
1752 constants via force_const_mem or put the
1753 pseudo value into memory, or make other
1754 memory by reloading the address like for
1755 'o'. */
1756 if (CONST_POOL_OK_P (mode, op)
1757 || MEM_P (op) || REG_P (op))
1758 badop = false;
1759 constmemok = true;
1760 offmemok = true;
1761 break;
1763 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1765 if (EXTRA_CONSTRAINT_STR (op, c, p))
1766 win = true;
1768 /* If we didn't already win, we can reload
1769 the address into a base register. */
1770 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1771 ADDRESS, SCRATCH);
1772 this_alternative
1773 = reg_class_subunion[this_alternative][cl];
1774 IOR_HARD_REG_SET (this_alternative_set,
1775 reg_class_contents[cl]);
1776 if (costly_p)
1778 this_costly_alternative
1779 = (reg_class_subunion
1780 [this_costly_alternative][cl]);
1781 IOR_HARD_REG_SET (this_costly_alternative_set,
1782 reg_class_contents[cl]);
1784 badop = false;
1785 break;
1788 if (EXTRA_CONSTRAINT_STR (op, c, p))
1789 win = true;
1790 #endif
1791 break;
1794 cl = REG_CLASS_FROM_CONSTRAINT (c, p);
1795 this_alternative = reg_class_subunion[this_alternative][cl];
1796 IOR_HARD_REG_SET (this_alternative_set,
1797 reg_class_contents[cl]);
1798 if (costly_p)
1800 this_costly_alternative
1801 = reg_class_subunion[this_costly_alternative][cl];
1802 IOR_HARD_REG_SET (this_costly_alternative_set,
1803 reg_class_contents[cl]);
1805 reg:
1806 if (mode == BLKmode)
1807 break;
1808 winreg = true;
1809 if (REG_P (op))
1811 if (hard_regno[nop] >= 0
1812 && in_hard_reg_set_p (this_alternative_set,
1813 mode, hard_regno[nop]))
1814 win = true;
1815 else if (hard_regno[nop] < 0
1816 && in_class_p (op, this_alternative, NULL))
1817 win = true;
1819 break;
1821 if (c != ' ' && c != '\t')
1822 costly_p = c == '*';
1824 while ((p += len), c);
1826 /* Record which operands fit this alternative. */
1827 if (win)
1829 this_alternative_win = true;
1830 if (operand_reg[nop] != NULL_RTX)
1832 if (hard_regno[nop] >= 0)
1834 if (in_hard_reg_set_p (this_costly_alternative_set,
1835 mode, hard_regno[nop]))
1836 reject++;
1838 else
1840 /* Prefer won reg to spilled pseudo under other equal
1841 conditions. */
1842 reject++;
1843 if (in_class_p (operand_reg[nop],
1844 this_costly_alternative, NULL))
1845 reject++;
1847 /* We simulate the behaviour of old reload here.
1848 Although scratches need hard registers and it
1849 might result in spilling other pseudos, no reload
1850 insns are generated for the scratches. So it
1851 might cost something but probably less than old
1852 reload pass believes. */
1853 if (lra_former_scratch_p (REGNO (operand_reg[nop])))
1854 reject += LRA_LOSER_COST_FACTOR;
1857 else if (did_match)
1858 this_alternative_match_win = true;
1859 else
1861 int const_to_mem = 0;
1862 bool no_regs_p;
1864 /* If this alternative asks for a specific reg class, see if there
1865 is at least one allocatable register in that class. */
1866 no_regs_p
1867 = (this_alternative == NO_REGS
1868 || (hard_reg_set_subset_p
1869 (reg_class_contents[this_alternative],
1870 lra_no_alloc_regs)));
1872 /* For asms, verify that the class for this alternative is possible
1873 for the mode that is specified. */
1874 if (!no_regs_p && REG_P (op) && INSN_CODE (curr_insn) < 0)
1876 int i;
1877 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1878 if (HARD_REGNO_MODE_OK (i, mode)
1879 && in_hard_reg_set_p (reg_class_contents[this_alternative], mode, i))
1880 break;
1881 if (i == FIRST_PSEUDO_REGISTER)
1882 winreg = false;
1885 /* If this operand accepts a register, and if the
1886 register class has at least one allocatable register,
1887 then this operand can be reloaded. */
1888 if (winreg && !no_regs_p)
1889 badop = false;
1891 if (badop)
1892 goto fail;
1894 this_alternative_offmemok = offmemok;
1895 if (this_costly_alternative != NO_REGS)
1896 reject++;
1897 /* If the operand is dying, has a matching constraint,
1898 and satisfies constraints of the matched operand
1899 which failed to satisfy the own constraints, we do
1900 not need to generate a reload insn for this
1901 operand. */
1902 if (!(this_alternative_matches >= 0
1903 && !curr_alt_win[this_alternative_matches]
1904 && REG_P (op)
1905 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
1906 && (hard_regno[nop] >= 0
1907 ? in_hard_reg_set_p (this_alternative_set,
1908 mode, hard_regno[nop])
1909 : in_class_p (op, this_alternative, NULL))))
1911 /* Strict_low_part requires to reload the register
1912 not the sub-register. In this case we should
1913 check that a final reload hard reg can hold the
1914 value mode. */
1915 if (curr_static_id->operand[nop].strict_low
1916 && REG_P (op)
1917 && hard_regno[nop] < 0
1918 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
1919 && ira_class_hard_regs_num[this_alternative] > 0
1920 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
1921 [this_alternative][0],
1922 GET_MODE (op)))
1923 goto fail;
1924 losers++;
1926 if (operand_reg[nop] != NULL_RTX
1927 /* Output operands and matched input operands are
1928 not inherited. The following conditions do not
1929 exactly describe the previous statement but they
1930 are pretty close. */
1931 && curr_static_id->operand[nop].type != OP_OUT
1932 && (this_alternative_matches < 0
1933 || curr_static_id->operand[nop].type != OP_IN))
1935 int last_reload = (lra_reg_info[ORIGINAL_REGNO
1936 (operand_reg[nop])]
1937 .last_reload);
1939 if (last_reload > bb_reload_num)
1940 reload_sum += last_reload - bb_reload_num;
1942 /* If this is a constant that is reloaded into the
1943 desired class by copying it to memory first, count
1944 that as another reload. This is consistent with
1945 other code and is required to avoid choosing another
1946 alternative when the constant is moved into memory.
1947 Note that the test here is precisely the same as in
1948 the code below that calls force_const_mem. */
1949 if (CONST_POOL_OK_P (mode, op)
1950 && ((targetm.preferred_reload_class
1951 (op, this_alternative) == NO_REGS)
1952 || no_input_reloads_p))
1954 const_to_mem = 1;
1955 if (! no_regs_p)
1956 losers++;
1959 /* Alternative loses if it requires a type of reload not
1960 permitted for this insn. We can always reload
1961 objects with a REG_UNUSED note. */
1962 if ((curr_static_id->operand[nop].type != OP_IN
1963 && no_output_reloads_p
1964 && ! find_reg_note (curr_insn, REG_UNUSED, op))
1965 || (curr_static_id->operand[nop].type != OP_OUT
1966 && no_input_reloads_p && ! const_to_mem))
1967 goto fail;
1969 /* Check strong discouragement of reload of non-constant
1970 into class THIS_ALTERNATIVE. */
1971 if (! CONSTANT_P (op) && ! no_regs_p
1972 && (targetm.preferred_reload_class
1973 (op, this_alternative) == NO_REGS
1974 || (curr_static_id->operand[nop].type == OP_OUT
1975 && (targetm.preferred_output_reload_class
1976 (op, this_alternative) == NO_REGS))))
1977 reject += LRA_MAX_REJECT;
1979 if (MEM_P (op) && offmemok)
1981 /* If we know offset and this non-offsetable memory,
1982 something wrong with this memory and it is better
1983 to try other memory possibilities. */
1984 if (MEM_OFFSET_KNOWN_P (op))
1985 reject += LRA_MAX_REJECT;
1987 else if (! (const_to_mem && constmemok))
1989 /* We prefer to reload pseudos over reloading other
1990 things, since such reloads may be able to be
1991 eliminated later. So bump REJECT in other cases.
1992 Don't do this in the case where we are forcing a
1993 constant into memory and it will then win since
1994 we don't want to have a different alternative
1995 match then. */
1996 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
1997 reject += 2;
1999 if (! no_regs_p)
2000 reload_nregs
2001 += ira_reg_class_max_nregs[this_alternative][mode];
2003 if (SMALL_REGISTER_CLASS_P (this_alternative))
2004 reject += LRA_LOSER_COST_FACTOR / 2;
2007 /* We are trying to spill pseudo into memory. It is
2008 usually more costly than moving to a hard register
2009 although it might takes the same number of
2010 reloads. */
2011 if (no_regs_p && REG_P (op))
2012 reject += 2;
2014 #ifdef SECONDARY_MEMORY_NEEDED
2015 /* If reload requires moving value through secondary
2016 memory, it will need one more insn at least. */
2017 if (this_alternative != NO_REGS
2018 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2019 && ((curr_static_id->operand[nop].type != OP_OUT
2020 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2021 GET_MODE (op)))
2022 || (curr_static_id->operand[nop].type != OP_IN
2023 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2024 GET_MODE (op)))))
2025 losers++;
2026 #endif
2027 /* Input reloads can be inherited more often than output
2028 reloads can be removed, so penalize output
2029 reloads. */
2030 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2031 reject++;
2035 if (early_clobber_p)
2036 reject++;
2037 /* ??? We check early clobbers after processing all operands
2038 (see loop below) and there we update the costs more.
2039 Should we update the cost (may be approximately) here
2040 because of early clobber register reloads or it is a rare
2041 or non-important thing to be worth to do it. */
2042 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2043 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2045 if (lra_dump_file != NULL)
2046 fprintf (lra_dump_file,
2047 " alt=%d,overall=%d,losers=%d -- reject\n",
2048 nalt, overall, losers);
2049 goto fail;
2052 curr_alt[nop] = this_alternative;
2053 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2054 curr_alt_win[nop] = this_alternative_win;
2055 curr_alt_match_win[nop] = this_alternative_match_win;
2056 curr_alt_offmemok[nop] = this_alternative_offmemok;
2057 curr_alt_matches[nop] = this_alternative_matches;
2059 if (this_alternative_matches >= 0
2060 && !did_match && !this_alternative_win)
2061 curr_alt_win[this_alternative_matches] = false;
2063 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2064 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2066 ok_p = true;
2067 curr_alt_dont_inherit_ops_num = 0;
2068 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2070 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2071 HARD_REG_SET temp_set;
2073 i = early_clobbered_nops[nop];
2074 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2075 || hard_regno[i] < 0)
2076 continue;
2077 lra_assert (operand_reg[i] != NULL_RTX);
2078 clobbered_hard_regno = hard_regno[i];
2079 CLEAR_HARD_REG_SET (temp_set);
2080 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2081 first_conflict_j = last_conflict_j = -1;
2082 for (j = 0; j < n_operands; j++)
2083 if (j == i
2084 /* We don't want process insides of match_operator and
2085 match_parallel because otherwise we would process
2086 their operands once again generating a wrong
2087 code. */
2088 || curr_static_id->operand[j].is_operator)
2089 continue;
2090 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2091 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2092 continue;
2093 /* If we don't reload j-th operand, check conflicts. */
2094 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2095 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2097 if (first_conflict_j < 0)
2098 first_conflict_j = j;
2099 last_conflict_j = j;
2101 if (last_conflict_j < 0)
2102 continue;
2103 /* If earlyclobber operand conflicts with another
2104 non-matching operand which is actually the same register
2105 as the earlyclobber operand, it is better to reload the
2106 another operand as an operand matching the earlyclobber
2107 operand can be also the same. */
2108 if (first_conflict_j == last_conflict_j
2109 && operand_reg[last_conflict_j]
2110 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2111 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2113 curr_alt_win[last_conflict_j] = false;
2114 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2115 = last_conflict_j;
2116 losers++;
2117 /* Early clobber was already reflected in REJECT. */
2118 lra_assert (reject > 0);
2119 reject--;
2120 overall += LRA_LOSER_COST_FACTOR - 1;
2122 else
2124 /* We need to reload early clobbered register and the
2125 matched registers. */
2126 for (j = 0; j < n_operands; j++)
2127 if (curr_alt_matches[j] == i)
2129 curr_alt_match_win[j] = false;
2130 losers++;
2131 overall += LRA_LOSER_COST_FACTOR;
2133 if (! curr_alt_match_win[i])
2134 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2135 else
2137 /* Remember pseudos used for match reloads are never
2138 inherited. */
2139 lra_assert (curr_alt_matches[i] >= 0);
2140 curr_alt_win[curr_alt_matches[i]] = false;
2142 curr_alt_win[i] = curr_alt_match_win[i] = false;
2143 losers++;
2144 /* Early clobber was already reflected in REJECT. */
2145 lra_assert (reject > 0);
2146 reject--;
2147 overall += LRA_LOSER_COST_FACTOR - 1;
2150 if (lra_dump_file != NULL)
2151 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2152 nalt, overall, losers, reload_nregs);
2154 /* If this alternative can be made to work by reloading, and it
2155 needs less reloading than the others checked so far, record
2156 it as the chosen goal for reloading. */
2157 if ((best_losers != 0 && losers == 0)
2158 || (((best_losers == 0 && losers == 0)
2159 || (best_losers != 0 && losers != 0))
2160 && (best_overall > overall
2161 || (best_overall == overall
2162 /* If the cost of the reloads is the same,
2163 prefer alternative which requires minimal
2164 number of reload regs. */
2165 && (reload_nregs < best_reload_nregs
2166 || (reload_nregs == best_reload_nregs
2167 && (best_reload_sum < reload_sum
2168 || (best_reload_sum == reload_sum
2169 && nalt < goal_alt_number))))))))
2171 for (nop = 0; nop < n_operands; nop++)
2173 goal_alt_win[nop] = curr_alt_win[nop];
2174 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2175 goal_alt_matches[nop] = curr_alt_matches[nop];
2176 goal_alt[nop] = curr_alt[nop];
2177 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2179 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2180 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2181 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2182 goal_alt_swapped = curr_swapped;
2183 best_overall = overall;
2184 best_losers = losers;
2185 best_reload_nregs = reload_nregs;
2186 best_reload_sum = reload_sum;
2187 goal_alt_number = nalt;
2189 if (losers == 0)
2190 /* Everything is satisfied. Do not process alternatives
2191 anymore. */
2192 break;
2193 fail:
2196 return ok_p;
2199 /* Return 1 if ADDR is a valid memory address for mode MODE in address
2200 space AS, and check that each pseudo has the proper kind of hard
2201 reg. */
2202 static int
2203 valid_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2204 rtx addr, addr_space_t as)
2206 #ifdef GO_IF_LEGITIMATE_ADDRESS
2207 lra_assert (ADDR_SPACE_GENERIC_P (as));
2208 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
2209 return 0;
2211 win:
2212 return 1;
2213 #else
2214 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
2215 #endif
2218 /* Return whether address AD is valid. */
2220 static bool
2221 valid_address_p (struct address_info *ad)
2223 /* Some ports do not check displacements for eliminable registers,
2224 so we replace them temporarily with the elimination target. */
2225 rtx saved_base_reg = NULL_RTX;
2226 rtx saved_index_reg = NULL_RTX;
2227 rtx *base_term = strip_subreg (ad->base_term);
2228 rtx *index_term = strip_subreg (ad->index_term);
2229 if (base_term != NULL)
2231 saved_base_reg = *base_term;
2232 lra_eliminate_reg_if_possible (base_term);
2233 if (ad->base_term2 != NULL)
2234 *ad->base_term2 = *ad->base_term;
2236 if (index_term != NULL)
2238 saved_index_reg = *index_term;
2239 lra_eliminate_reg_if_possible (index_term);
2241 bool ok_p = valid_address_p (ad->mode, *ad->outer, ad->as);
2242 if (saved_base_reg != NULL_RTX)
2244 *base_term = saved_base_reg;
2245 if (ad->base_term2 != NULL)
2246 *ad->base_term2 = *ad->base_term;
2248 if (saved_index_reg != NULL_RTX)
2249 *index_term = saved_index_reg;
2250 return ok_p;
2253 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2254 static rtx
2255 base_plus_disp_to_reg (struct address_info *ad)
2257 enum reg_class cl;
2258 rtx new_reg;
2260 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2261 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2262 get_index_code (ad));
2263 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2264 cl, "base + disp");
2265 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2266 return new_reg;
2269 /* Return true if we can add a displacement to address AD, even if that
2270 makes the address invalid. The fix-up code requires any new address
2271 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2272 static bool
2273 can_add_disp_p (struct address_info *ad)
2275 return (!ad->autoinc_p
2276 && ad->segment == NULL
2277 && ad->base == ad->base_term
2278 && ad->disp == ad->disp_term);
2281 /* Make equiv substitution in address AD. Return true if a substitution
2282 was made. */
2283 static bool
2284 equiv_address_substitution (struct address_info *ad)
2286 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2287 HOST_WIDE_INT disp, scale;
2288 bool change_p;
2290 base_term = strip_subreg (ad->base_term);
2291 if (base_term == NULL)
2292 base_reg = new_base_reg = NULL_RTX;
2293 else
2295 base_reg = *base_term;
2296 new_base_reg = get_equiv_substitution (base_reg);
2298 index_term = strip_subreg (ad->index_term);
2299 if (index_term == NULL)
2300 index_reg = new_index_reg = NULL_RTX;
2301 else
2303 index_reg = *index_term;
2304 new_index_reg = get_equiv_substitution (index_reg);
2306 if (base_reg == new_base_reg && index_reg == new_index_reg)
2307 return false;
2308 disp = 0;
2309 change_p = false;
2310 if (lra_dump_file != NULL)
2312 fprintf (lra_dump_file, "Changing address in insn %d ",
2313 INSN_UID (curr_insn));
2314 dump_value_slim (lra_dump_file, *ad->outer, 1);
2316 if (base_reg != new_base_reg)
2318 if (REG_P (new_base_reg))
2320 *base_term = new_base_reg;
2321 change_p = true;
2323 else if (GET_CODE (new_base_reg) == PLUS
2324 && REG_P (XEXP (new_base_reg, 0))
2325 && CONST_INT_P (XEXP (new_base_reg, 1))
2326 && can_add_disp_p (ad))
2328 disp += INTVAL (XEXP (new_base_reg, 1));
2329 *base_term = XEXP (new_base_reg, 0);
2330 change_p = true;
2332 if (ad->base_term2 != NULL)
2333 *ad->base_term2 = *ad->base_term;
2335 if (index_reg != new_index_reg)
2337 if (REG_P (new_index_reg))
2339 *index_term = new_index_reg;
2340 change_p = true;
2342 else if (GET_CODE (new_index_reg) == PLUS
2343 && REG_P (XEXP (new_index_reg, 0))
2344 && CONST_INT_P (XEXP (new_index_reg, 1))
2345 && can_add_disp_p (ad)
2346 && (scale = get_index_scale (ad)))
2348 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2349 *index_term = XEXP (new_index_reg, 0);
2350 change_p = true;
2353 if (disp != 0)
2355 if (ad->disp != NULL)
2356 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2357 else
2359 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2360 update_address (ad);
2362 change_p = true;
2364 if (lra_dump_file != NULL)
2366 if (! change_p)
2367 fprintf (lra_dump_file, " -- no change\n");
2368 else
2370 fprintf (lra_dump_file, " on equiv ");
2371 dump_value_slim (lra_dump_file, *ad->outer, 1);
2372 fprintf (lra_dump_file, "\n");
2375 return change_p;
2378 /* Major function to make reloads for an address in operand NOP.
2379 The supported cases are:
2381 1) an address that existed before LRA started, at which point it must
2382 have been valid. These addresses are subject to elimination and
2383 may have become invalid due to the elimination offset being out
2384 of range.
2386 2) an address created by forcing a constant to memory (force_const_to_mem).
2387 The initial form of these addresses might not be valid, and it is this
2388 function's job to make them valid.
2390 3) a frame address formed from a register and a (possibly zero)
2391 constant offset. As above, these addresses might not be valid
2392 and this function must make them so.
2394 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2395 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2396 address. Return true for any RTL change. */
2397 static bool
2398 process_address (int nop, rtx *before, rtx *after)
2400 struct address_info ad;
2401 rtx new_reg;
2402 rtx op = *curr_id->operand_loc[nop];
2403 const char *constraint = curr_static_id->operand[nop].constraint;
2404 bool change_p;
2406 if (constraint[0] == 'p'
2407 || EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint))
2408 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2409 else if (MEM_P (op))
2410 decompose_mem_address (&ad, op);
2411 else if (GET_CODE (op) == SUBREG
2412 && MEM_P (SUBREG_REG (op)))
2413 decompose_mem_address (&ad, SUBREG_REG (op));
2414 else
2415 return false;
2416 change_p = equiv_address_substitution (&ad);
2417 if (ad.base_term != NULL
2418 && (process_addr_reg
2419 (ad.base_term, before,
2420 (ad.autoinc_p
2421 && !(REG_P (*ad.base_term)
2422 && find_regno_note (curr_insn, REG_DEAD,
2423 REGNO (*ad.base_term)) != NULL_RTX)
2424 ? after : NULL),
2425 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2426 get_index_code (&ad)))))
2428 change_p = true;
2429 if (ad.base_term2 != NULL)
2430 *ad.base_term2 = *ad.base_term;
2432 if (ad.index_term != NULL
2433 && process_addr_reg (ad.index_term, before, NULL, INDEX_REG_CLASS))
2434 change_p = true;
2436 /* There are three cases where the shape of *AD.INNER may now be invalid:
2438 1) the original address was valid, but either elimination or
2439 equiv_address_substitution applied a displacement that made
2440 it invalid.
2442 2) the address is an invalid symbolic address created by
2443 force_const_to_mem.
2445 3) the address is a frame address with an invalid offset.
2447 All these cases involve a displacement and a non-autoinc address,
2448 so there is no point revalidating other types. */
2449 if (ad.disp == NULL || ad.autoinc_p || valid_address_p (&ad))
2450 return change_p;
2452 /* Any index existed before LRA started, so we can assume that the
2453 presence and shape of the index is valid. */
2454 push_to_sequence (*before);
2455 gcc_assert (ad.segment == NULL);
2456 gcc_assert (ad.disp == ad.disp_term);
2457 if (ad.base == NULL)
2459 if (ad.index == NULL)
2461 int code = -1;
2462 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2463 SCRATCH, SCRATCH);
2464 rtx disp = *ad.disp;
2466 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2467 #ifdef HAVE_lo_sum
2469 rtx insn;
2470 rtx last = get_last_insn ();
2472 /* disp => lo_sum (new_base, disp), case (2) above. */
2473 insn = emit_insn (gen_rtx_SET
2474 (VOIDmode, new_reg,
2475 gen_rtx_HIGH (Pmode, copy_rtx (disp))));
2476 code = recog_memoized (insn);
2477 if (code >= 0)
2479 *ad.disp = gen_rtx_LO_SUM (Pmode, new_reg, disp);
2480 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2482 *ad.disp = disp;
2483 code = -1;
2486 if (code < 0)
2487 delete_insns_since (last);
2489 #endif
2490 if (code < 0)
2492 /* disp => new_base, case (2) above. */
2493 lra_emit_move (new_reg, disp);
2494 *ad.disp = new_reg;
2497 else
2499 /* index * scale + disp => new base + index * scale,
2500 case (1) above. */
2501 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
2502 GET_CODE (*ad.index));
2504 lra_assert (INDEX_REG_CLASS != NO_REGS);
2505 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2506 lra_emit_move (new_reg, *ad.disp);
2507 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2508 new_reg, *ad.index);
2511 else if (ad.index == NULL)
2513 /* base + disp => new base, cases (1) and (3) above. */
2514 /* Another option would be to reload the displacement into an
2515 index register. However, postreload has code to optimize
2516 address reloads that have the same base and different
2517 displacements, so reloading into an index register would
2518 not necessarily be a win. */
2519 new_reg = base_plus_disp_to_reg (&ad);
2520 *ad.inner = new_reg;
2522 else
2524 /* base + scale * index + disp => new base + scale * index,
2525 case (1) above. */
2526 new_reg = base_plus_disp_to_reg (&ad);
2527 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2528 new_reg, *ad.index);
2530 *before = get_insns ();
2531 end_sequence ();
2532 return true;
2535 /* Emit insns to reload VALUE into a new register. VALUE is an
2536 auto-increment or auto-decrement RTX whose operand is a register or
2537 memory location; so reloading involves incrementing that location.
2538 IN is either identical to VALUE, or some cheaper place to reload
2539 value being incremented/decremented from.
2541 INC_AMOUNT is the number to increment or decrement by (always
2542 positive and ignored for POST_MODIFY/PRE_MODIFY).
2544 Return pseudo containing the result. */
2545 static rtx
2546 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
2548 /* REG or MEM to be copied and incremented. */
2549 rtx incloc = XEXP (value, 0);
2550 /* Nonzero if increment after copying. */
2551 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
2552 || GET_CODE (value) == POST_MODIFY);
2553 rtx last;
2554 rtx inc;
2555 rtx add_insn;
2556 int code;
2557 rtx real_in = in == value ? incloc : in;
2558 rtx result;
2559 bool plus_p = true;
2561 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
2563 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
2564 || GET_CODE (XEXP (value, 1)) == MINUS);
2565 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
2566 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
2567 inc = XEXP (XEXP (value, 1), 1);
2569 else
2571 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
2572 inc_amount = -inc_amount;
2574 inc = GEN_INT (inc_amount);
2577 if (! post && REG_P (incloc))
2578 result = incloc;
2579 else
2580 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
2581 "INC/DEC result");
2583 if (real_in != result)
2585 /* First copy the location to the result register. */
2586 lra_assert (REG_P (result));
2587 emit_insn (gen_move_insn (result, real_in));
2590 /* We suppose that there are insns to add/sub with the constant
2591 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
2592 old reload worked with this assumption. If the assumption
2593 becomes wrong, we should use approach in function
2594 base_plus_disp_to_reg. */
2595 if (in == value)
2597 /* See if we can directly increment INCLOC. */
2598 last = get_last_insn ();
2599 add_insn = emit_insn (plus_p
2600 ? gen_add2_insn (incloc, inc)
2601 : gen_sub2_insn (incloc, inc));
2603 code = recog_memoized (add_insn);
2604 if (code >= 0)
2606 if (! post && result != incloc)
2607 emit_insn (gen_move_insn (result, incloc));
2608 return result;
2610 delete_insns_since (last);
2613 /* If couldn't do the increment directly, must increment in RESULT.
2614 The way we do this depends on whether this is pre- or
2615 post-increment. For pre-increment, copy INCLOC to the reload
2616 register, increment it there, then save back. */
2617 if (! post)
2619 if (real_in != result)
2620 emit_insn (gen_move_insn (result, real_in));
2621 if (plus_p)
2622 emit_insn (gen_add2_insn (result, inc));
2623 else
2624 emit_insn (gen_sub2_insn (result, inc));
2625 if (result != incloc)
2626 emit_insn (gen_move_insn (incloc, result));
2628 else
2630 /* Post-increment.
2632 Because this might be a jump insn or a compare, and because
2633 RESULT may not be available after the insn in an input
2634 reload, we must do the incrementing before the insn being
2635 reloaded for.
2637 We have already copied IN to RESULT. Increment the copy in
2638 RESULT, save that back, then decrement RESULT so it has
2639 the original value. */
2640 if (plus_p)
2641 emit_insn (gen_add2_insn (result, inc));
2642 else
2643 emit_insn (gen_sub2_insn (result, inc));
2644 emit_insn (gen_move_insn (incloc, result));
2645 /* Restore non-modified value for the result. We prefer this
2646 way because it does not require an additional hard
2647 register. */
2648 if (plus_p)
2650 if (CONST_INT_P (inc))
2651 emit_insn (gen_add2_insn (result, GEN_INT (-INTVAL (inc))));
2652 else
2653 emit_insn (gen_sub2_insn (result, inc));
2655 else
2656 emit_insn (gen_add2_insn (result, inc));
2658 return result;
2661 /* Swap operands NOP and NOP + 1. */
2662 static inline void
2663 swap_operands (int nop)
2665 enum machine_mode mode = curr_operand_mode[nop];
2666 curr_operand_mode[nop] = curr_operand_mode[nop + 1];
2667 curr_operand_mode[nop + 1] = mode;
2668 rtx x = *curr_id->operand_loc[nop];
2669 *curr_id->operand_loc[nop] = *curr_id->operand_loc[nop + 1];
2670 *curr_id->operand_loc[nop + 1] = x;
2671 /* Swap the duplicates too. */
2672 lra_update_dup (curr_id, nop);
2673 lra_update_dup (curr_id, nop + 1);
2676 /* Main entry point of the constraint code: search the body of the
2677 current insn to choose the best alternative. It is mimicking insn
2678 alternative cost calculation model of former reload pass. That is
2679 because machine descriptions were written to use this model. This
2680 model can be changed in future. Make commutative operand exchange
2681 if it is chosen.
2683 Return true if some RTL changes happened during function call. */
2684 static bool
2685 curr_insn_transform (void)
2687 int i, j, k;
2688 int n_operands;
2689 int n_alternatives;
2690 int commutative;
2691 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
2692 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
2693 rtx before, after;
2694 bool alt_p = false;
2695 /* Flag that the insn has been changed through a transformation. */
2696 bool change_p;
2697 bool sec_mem_p;
2698 #ifdef SECONDARY_MEMORY_NEEDED
2699 bool use_sec_mem_p;
2700 #endif
2701 int max_regno_before;
2702 int reused_alternative_num;
2704 no_input_reloads_p = no_output_reloads_p = false;
2705 goal_alt_number = -1;
2707 if (check_and_process_move (&change_p, &sec_mem_p))
2708 return change_p;
2710 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
2711 reloads; neither are insns that SET cc0. Insns that use CC0 are
2712 not allowed to have any input reloads. */
2713 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
2714 no_output_reloads_p = true;
2716 #ifdef HAVE_cc0
2717 if (reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
2718 no_input_reloads_p = true;
2719 if (reg_set_p (cc0_rtx, PATTERN (curr_insn)))
2720 no_output_reloads_p = true;
2721 #endif
2723 n_operands = curr_static_id->n_operands;
2724 n_alternatives = curr_static_id->n_alternatives;
2726 /* Just return "no reloads" if insn has no operands with
2727 constraints. */
2728 if (n_operands == 0 || n_alternatives == 0)
2729 return false;
2731 max_regno_before = max_reg_num ();
2733 for (i = 0; i < n_operands; i++)
2735 goal_alt_matched[i][0] = -1;
2736 goal_alt_matches[i] = -1;
2739 commutative = curr_static_id->commutative;
2741 /* Now see what we need for pseudos that didn't get hard regs or got
2742 the wrong kind of hard reg. For this, we must consider all the
2743 operands together against the register constraints. */
2745 best_losers = best_overall = INT_MAX;
2746 best_reload_sum = 0;
2748 curr_swapped = false;
2749 goal_alt_swapped = false;
2751 /* Make equivalence substitution and memory subreg elimination
2752 before address processing because an address legitimacy can
2753 depend on memory mode. */
2754 for (i = 0; i < n_operands; i++)
2756 rtx op = *curr_id->operand_loc[i];
2757 rtx subst, old = op;
2758 bool op_change_p = false;
2760 if (GET_CODE (old) == SUBREG)
2761 old = SUBREG_REG (old);
2762 subst = get_equiv_substitution (old);
2763 if (subst != old)
2765 subst = copy_rtx (subst);
2766 lra_assert (REG_P (old));
2767 if (GET_CODE (op) == SUBREG)
2768 SUBREG_REG (op) = subst;
2769 else
2770 *curr_id->operand_loc[i] = subst;
2771 if (lra_dump_file != NULL)
2773 fprintf (lra_dump_file,
2774 "Changing pseudo %d in operand %i of insn %u on equiv ",
2775 REGNO (old), i, INSN_UID (curr_insn));
2776 dump_value_slim (lra_dump_file, subst, 1);
2777 fprintf (lra_dump_file, "\n");
2779 op_change_p = change_p = true;
2781 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
2783 change_p = true;
2784 lra_update_dup (curr_id, i);
2788 /* Reload address registers and displacements. We do it before
2789 finding an alternative because of memory constraints. */
2790 before = after = NULL_RTX;
2791 for (i = 0; i < n_operands; i++)
2792 if (! curr_static_id->operand[i].is_operator
2793 && process_address (i, &before, &after))
2795 change_p = true;
2796 lra_update_dup (curr_id, i);
2799 if (change_p)
2800 /* If we've changed the instruction then any alternative that
2801 we chose previously may no longer be valid. */
2802 lra_set_used_insn_alternative (curr_insn, -1);
2804 try_swapped:
2806 reused_alternative_num = curr_id->used_insn_alternative;
2807 if (lra_dump_file != NULL && reused_alternative_num >= 0)
2808 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
2809 reused_alternative_num, INSN_UID (curr_insn));
2811 if (process_alt_operands (reused_alternative_num))
2812 alt_p = true;
2814 /* If insn is commutative (it's safe to exchange a certain pair of
2815 operands) then we need to try each alternative twice, the second
2816 time matching those two operands as if we had exchanged them. To
2817 do this, really exchange them in operands.
2819 If we have just tried the alternatives the second time, return
2820 operands to normal and drop through. */
2822 if (reused_alternative_num < 0 && commutative >= 0)
2824 curr_swapped = !curr_swapped;
2825 if (curr_swapped)
2827 swap_operands (commutative);
2828 goto try_swapped;
2830 else
2831 swap_operands (commutative);
2834 if (! alt_p && ! sec_mem_p)
2836 /* No alternative works with reloads?? */
2837 if (INSN_CODE (curr_insn) >= 0)
2838 fatal_insn ("unable to generate reloads for:", curr_insn);
2839 error_for_asm (curr_insn,
2840 "inconsistent operand constraints in an %<asm%>");
2841 /* Avoid further trouble with this insn. */
2842 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
2843 lra_invalidate_insn_data (curr_insn);
2844 return true;
2847 /* If the best alternative is with operands 1 and 2 swapped, swap
2848 them. Update the operand numbers of any reloads already
2849 pushed. */
2851 if (goal_alt_swapped)
2853 if (lra_dump_file != NULL)
2854 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
2855 INSN_UID (curr_insn));
2857 /* Swap the duplicates too. */
2858 swap_operands (commutative);
2859 change_p = true;
2862 #ifdef SECONDARY_MEMORY_NEEDED
2863 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
2864 too conservatively. So we use the secondary memory only if there
2865 is no any alternative without reloads. */
2866 use_sec_mem_p = false;
2867 if (! alt_p)
2868 use_sec_mem_p = true;
2869 else if (sec_mem_p)
2871 for (i = 0; i < n_operands; i++)
2872 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
2873 break;
2874 use_sec_mem_p = i < n_operands;
2877 if (use_sec_mem_p)
2879 rtx new_reg, src, dest, rld;
2880 enum machine_mode sec_mode, rld_mode;
2882 lra_assert (sec_mem_p);
2883 lra_assert (curr_static_id->operand[0].type == OP_OUT
2884 && curr_static_id->operand[1].type == OP_IN);
2885 dest = *curr_id->operand_loc[0];
2886 src = *curr_id->operand_loc[1];
2887 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
2888 ? dest : src);
2889 rld_mode = GET_MODE (rld);
2890 #ifdef SECONDARY_MEMORY_NEEDED_MODE
2891 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
2892 #else
2893 sec_mode = rld_mode;
2894 #endif
2895 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
2896 NO_REGS, "secondary");
2897 /* If the mode is changed, it should be wider. */
2898 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
2899 if (sec_mode != rld_mode)
2901 /* If the target says specifically to use another mode for
2902 secondary memory moves we can not reuse the original
2903 insn. */
2904 after = emit_spill_move (false, new_reg, dest);
2905 lra_process_new_insns (curr_insn, NULL_RTX, after,
2906 "Inserting the sec. move");
2907 before = emit_spill_move (true, new_reg, src);
2908 lra_process_new_insns (curr_insn, before, NULL_RTX, "Changing on");
2909 lra_set_insn_deleted (curr_insn);
2911 else if (dest == rld)
2913 *curr_id->operand_loc[0] = new_reg;
2914 after = emit_spill_move (false, new_reg, dest);
2915 lra_process_new_insns (curr_insn, NULL_RTX, after,
2916 "Inserting the sec. move");
2918 else
2920 *curr_id->operand_loc[1] = new_reg;
2921 before = emit_spill_move (true, new_reg, src);
2922 lra_process_new_insns (curr_insn, before, NULL_RTX,
2923 "Inserting the sec. move");
2925 lra_update_insn_regno_info (curr_insn);
2926 return true;
2928 #endif
2930 lra_assert (goal_alt_number >= 0);
2931 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
2933 if (lra_dump_file != NULL)
2935 const char *p;
2937 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
2938 goal_alt_number, INSN_UID (curr_insn));
2939 for (i = 0; i < n_operands; i++)
2941 p = (curr_static_id->operand_alternative
2942 [goal_alt_number * n_operands + i].constraint);
2943 if (*p == '\0')
2944 continue;
2945 fprintf (lra_dump_file, " (%d) ", i);
2946 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
2947 fputc (*p, lra_dump_file);
2949 if (INSN_CODE (curr_insn) >= 0
2950 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
2951 fprintf (lra_dump_file, " {%s}", p);
2952 fprintf (lra_dump_file, "\n");
2955 /* Right now, for any pair of operands I and J that are required to
2956 match, with J < I, goal_alt_matches[I] is J. Add I to
2957 goal_alt_matched[J]. */
2959 for (i = 0; i < n_operands; i++)
2960 if ((j = goal_alt_matches[i]) >= 0)
2962 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
2964 /* We allow matching one output operand and several input
2965 operands. */
2966 lra_assert (k == 0
2967 || (curr_static_id->operand[j].type == OP_OUT
2968 && curr_static_id->operand[i].type == OP_IN
2969 && (curr_static_id->operand
2970 [goal_alt_matched[j][0]].type == OP_IN)));
2971 goal_alt_matched[j][k] = i;
2972 goal_alt_matched[j][k + 1] = -1;
2975 for (i = 0; i < n_operands; i++)
2976 goal_alt_win[i] |= goal_alt_match_win[i];
2978 /* Any constants that aren't allowed and can't be reloaded into
2979 registers are here changed into memory references. */
2980 for (i = 0; i < n_operands; i++)
2981 if (goal_alt_win[i])
2983 int regno;
2984 enum reg_class new_class;
2985 rtx reg = *curr_id->operand_loc[i];
2987 if (GET_CODE (reg) == SUBREG)
2988 reg = SUBREG_REG (reg);
2990 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
2992 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
2994 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
2996 lra_assert (ok_p);
2997 change_class (regno, new_class, " Change", true);
3001 else
3003 const char *constraint;
3004 char c;
3005 rtx op = *curr_id->operand_loc[i];
3006 rtx subreg = NULL_RTX;
3007 enum machine_mode mode = curr_operand_mode[i];
3009 if (GET_CODE (op) == SUBREG)
3011 subreg = op;
3012 op = SUBREG_REG (op);
3013 mode = GET_MODE (op);
3016 if (CONST_POOL_OK_P (mode, op)
3017 && ((targetm.preferred_reload_class
3018 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3019 || no_input_reloads_p))
3021 rtx tem = force_const_mem (mode, op);
3023 change_p = true;
3024 if (subreg != NULL_RTX)
3025 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3027 *curr_id->operand_loc[i] = tem;
3028 lra_update_dup (curr_id, i);
3029 process_address (i, &before, &after);
3031 /* If the alternative accepts constant pool refs directly
3032 there will be no reload needed at all. */
3033 if (subreg != NULL_RTX)
3034 continue;
3035 /* Skip alternatives before the one requested. */
3036 constraint = (curr_static_id->operand_alternative
3037 [goal_alt_number * n_operands + i].constraint);
3038 for (;
3039 (c = *constraint) && c != ',' && c != '#';
3040 constraint += CONSTRAINT_LEN (c, constraint))
3042 if (c == TARGET_MEM_CONSTRAINT || c == 'o')
3043 break;
3044 #ifdef EXTRA_CONSTRAINT_STR
3045 if (EXTRA_MEMORY_CONSTRAINT (c, constraint)
3046 && EXTRA_CONSTRAINT_STR (tem, c, constraint))
3047 break;
3048 #endif
3050 if (c == '\0' || c == ',' || c == '#')
3051 continue;
3053 goal_alt_win[i] = true;
3057 for (i = 0; i < n_operands; i++)
3059 rtx old, new_reg;
3060 rtx op = *curr_id->operand_loc[i];
3062 if (goal_alt_win[i])
3064 if (goal_alt[i] == NO_REGS
3065 && REG_P (op)
3066 /* When we assign NO_REGS it means that we will not
3067 assign a hard register to the scratch pseudo by
3068 assigment pass and the scratch pseudo will be
3069 spilled. Spilled scratch pseudos are transformed
3070 back to scratches at the LRA end. */
3071 && lra_former_scratch_operand_p (curr_insn, i))
3073 int regno = REGNO (op);
3074 change_class (regno, NO_REGS, " Change", true);
3075 if (lra_get_regno_hard_regno (regno) >= 0)
3076 /* We don't have to mark all insn affected by the
3077 spilled pseudo as there is only one such insn, the
3078 current one. */
3079 reg_renumber[regno] = -1;
3081 continue;
3084 /* Operands that match previous ones have already been handled. */
3085 if (goal_alt_matches[i] >= 0)
3086 continue;
3088 /* We should not have an operand with a non-offsettable address
3089 appearing where an offsettable address will do. It also may
3090 be a case when the address should be special in other words
3091 not a general one (e.g. it needs no index reg). */
3092 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3094 enum reg_class rclass;
3095 rtx *loc = &XEXP (op, 0);
3096 enum rtx_code code = GET_CODE (*loc);
3098 push_to_sequence (before);
3099 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3100 MEM, SCRATCH);
3101 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3102 new_reg = emit_inc (rclass, *loc, *loc,
3103 /* This value does not matter for MODIFY. */
3104 GET_MODE_SIZE (GET_MODE (op)));
3105 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass,
3106 "offsetable address", &new_reg))
3107 lra_emit_move (new_reg, *loc);
3108 before = get_insns ();
3109 end_sequence ();
3110 *loc = new_reg;
3111 lra_update_dup (curr_id, i);
3113 else if (goal_alt_matched[i][0] == -1)
3115 enum machine_mode mode;
3116 rtx reg, *loc;
3117 int hard_regno, byte;
3118 enum op_type type = curr_static_id->operand[i].type;
3120 loc = curr_id->operand_loc[i];
3121 mode = curr_operand_mode[i];
3122 if (GET_CODE (*loc) == SUBREG)
3124 reg = SUBREG_REG (*loc);
3125 byte = SUBREG_BYTE (*loc);
3126 if (REG_P (reg)
3127 /* Strict_low_part requires reload the register not
3128 the sub-register. */
3129 && (curr_static_id->operand[i].strict_low
3130 || (GET_MODE_SIZE (mode)
3131 <= GET_MODE_SIZE (GET_MODE (reg))
3132 && (hard_regno
3133 = get_try_hard_regno (REGNO (reg))) >= 0
3134 && (simplify_subreg_regno
3135 (hard_regno,
3136 GET_MODE (reg), byte, mode) < 0)
3137 && (goal_alt[i] == NO_REGS
3138 || (simplify_subreg_regno
3139 (ira_class_hard_regs[goal_alt[i]][0],
3140 GET_MODE (reg), byte, mode) >= 0)))))
3142 loc = &SUBREG_REG (*loc);
3143 mode = GET_MODE (*loc);
3146 old = *loc;
3147 if (get_reload_reg (type, mode, old, goal_alt[i], "", &new_reg)
3148 && type != OP_OUT)
3150 push_to_sequence (before);
3151 lra_emit_move (new_reg, old);
3152 before = get_insns ();
3153 end_sequence ();
3155 *loc = new_reg;
3156 if (type != OP_IN
3157 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3159 start_sequence ();
3160 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3161 emit_insn (after);
3162 after = get_insns ();
3163 end_sequence ();
3164 *loc = new_reg;
3166 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3167 if (goal_alt_dont_inherit_ops[j] == i)
3169 lra_set_regno_unique_value (REGNO (new_reg));
3170 break;
3172 lra_update_dup (curr_id, i);
3174 else if (curr_static_id->operand[i].type == OP_IN
3175 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3176 == OP_OUT))
3178 /* generate reloads for input and matched outputs. */
3179 match_inputs[0] = i;
3180 match_inputs[1] = -1;
3181 match_reload (goal_alt_matched[i][0], match_inputs,
3182 goal_alt[i], &before, &after);
3184 else if (curr_static_id->operand[i].type == OP_OUT
3185 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3186 == OP_IN))
3187 /* Generate reloads for output and matched inputs. */
3188 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3189 else if (curr_static_id->operand[i].type == OP_IN
3190 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3191 == OP_IN))
3193 /* Generate reloads for matched inputs. */
3194 match_inputs[0] = i;
3195 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3196 match_inputs[j + 1] = k;
3197 match_inputs[j + 1] = -1;
3198 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3200 else
3201 /* We must generate code in any case when function
3202 process_alt_operands decides that it is possible. */
3203 gcc_unreachable ();
3205 if (before != NULL_RTX || after != NULL_RTX
3206 || max_regno_before != max_reg_num ())
3207 change_p = true;
3208 if (change_p)
3210 lra_update_operator_dups (curr_id);
3211 /* Something changes -- process the insn. */
3212 lra_update_insn_regno_info (curr_insn);
3214 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3215 return change_p;
3218 /* Return true if X is in LIST. */
3219 static bool
3220 in_list_p (rtx x, rtx list)
3222 for (; list != NULL_RTX; list = XEXP (list, 1))
3223 if (XEXP (list, 0) == x)
3224 return true;
3225 return false;
3228 /* Return true if X contains an allocatable hard register (if
3229 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3230 static bool
3231 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3233 int i, j;
3234 const char *fmt;
3235 enum rtx_code code;
3237 code = GET_CODE (x);
3238 if (REG_P (x))
3240 int regno = REGNO (x);
3241 HARD_REG_SET alloc_regs;
3243 if (hard_reg_p)
3245 if (regno >= FIRST_PSEUDO_REGISTER)
3246 regno = lra_get_regno_hard_regno (regno);
3247 if (regno < 0)
3248 return false;
3249 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3250 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3252 else
3254 if (regno < FIRST_PSEUDO_REGISTER)
3255 return false;
3256 if (! spilled_p)
3257 return true;
3258 return lra_get_regno_hard_regno (regno) < 0;
3261 fmt = GET_RTX_FORMAT (code);
3262 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3264 if (fmt[i] == 'e')
3266 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3267 return true;
3269 else if (fmt[i] == 'E')
3271 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3272 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3273 return true;
3276 return false;
3279 /* Process all regs in location *LOC and change them on equivalent
3280 substitution. Return true if any change was done. */
3281 static bool
3282 loc_equivalence_change_p (rtx *loc)
3284 rtx subst, reg, x = *loc;
3285 bool result = false;
3286 enum rtx_code code = GET_CODE (x);
3287 const char *fmt;
3288 int i, j;
3290 if (code == SUBREG)
3292 reg = SUBREG_REG (x);
3293 if ((subst = get_equiv_substitution (reg)) != reg
3294 && GET_MODE (subst) == VOIDmode)
3296 /* We cannot reload debug location. Simplify subreg here
3297 while we know the inner mode. */
3298 *loc = simplify_gen_subreg (GET_MODE (x), subst,
3299 GET_MODE (reg), SUBREG_BYTE (x));
3300 return true;
3303 if (code == REG && (subst = get_equiv_substitution (x)) != x)
3305 *loc = subst;
3306 return true;
3309 /* Scan all the operand sub-expressions. */
3310 fmt = GET_RTX_FORMAT (code);
3311 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3313 if (fmt[i] == 'e')
3314 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
3315 else if (fmt[i] == 'E')
3316 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3317 result
3318 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
3320 return result;
3323 /* Similar to loc_equivalence_change_p, but for use as
3324 simplify_replace_fn_rtx callback. */
3325 static rtx
3326 loc_equivalence_callback (rtx loc, const_rtx, void *)
3328 if (!REG_P (loc))
3329 return NULL_RTX;
3331 rtx subst = get_equiv_substitution (loc);
3332 if (subst != loc)
3333 return subst;
3335 return NULL_RTX;
3338 /* Maximum number of generated reload insns per an insn. It is for
3339 preventing this pass cycling in a bug case. */
3340 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
3342 /* The current iteration number of this LRA pass. */
3343 int lra_constraint_iter;
3345 /* The current iteration number of this LRA pass after the last spill
3346 pass. */
3347 int lra_constraint_iter_after_spill;
3349 /* True if we substituted equiv which needs checking register
3350 allocation correctness because the equivalent value contains
3351 allocatable hard registers or when we restore multi-register
3352 pseudo. */
3353 bool lra_risky_transformations_p;
3355 /* Return true if REGNO is referenced in more than one block. */
3356 static bool
3357 multi_block_pseudo_p (int regno)
3359 basic_block bb = NULL;
3360 unsigned int uid;
3361 bitmap_iterator bi;
3363 if (regno < FIRST_PSEUDO_REGISTER)
3364 return false;
3366 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
3367 if (bb == NULL)
3368 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
3369 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
3370 return true;
3371 return false;
3374 /* Return true if LIST contains a deleted insn. */
3375 static bool
3376 contains_deleted_insn_p (rtx list)
3378 for (; list != NULL_RTX; list = XEXP (list, 1))
3379 if (NOTE_P (XEXP (list, 0))
3380 && NOTE_KIND (XEXP (list, 0)) == NOTE_INSN_DELETED)
3381 return true;
3382 return false;
3385 /* Return true if X contains a pseudo dying in INSN. */
3386 static bool
3387 dead_pseudo_p (rtx x, rtx insn)
3389 int i, j;
3390 const char *fmt;
3391 enum rtx_code code;
3393 if (REG_P (x))
3394 return (insn != NULL_RTX
3395 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
3396 code = GET_CODE (x);
3397 fmt = GET_RTX_FORMAT (code);
3398 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3400 if (fmt[i] == 'e')
3402 if (dead_pseudo_p (XEXP (x, i), insn))
3403 return true;
3405 else if (fmt[i] == 'E')
3407 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3408 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
3409 return true;
3412 return false;
3415 /* Return true if INSN contains a dying pseudo in INSN right hand
3416 side. */
3417 static bool
3418 insn_rhs_dead_pseudo_p (rtx insn)
3420 rtx set = single_set (insn);
3422 gcc_assert (set != NULL);
3423 return dead_pseudo_p (SET_SRC (set), insn);
3426 /* Return true if any init insn of REGNO contains a dying pseudo in
3427 insn right hand side. */
3428 static bool
3429 init_insn_rhs_dead_pseudo_p (int regno)
3431 rtx insns = ira_reg_equiv[regno].init_insns;
3433 if (insns == NULL)
3434 return false;
3435 if (INSN_P (insns))
3436 return insn_rhs_dead_pseudo_p (insns);
3437 for (; insns != NULL_RTX; insns = XEXP (insns, 1))
3438 if (insn_rhs_dead_pseudo_p (XEXP (insns, 0)))
3439 return true;
3440 return false;
3443 /* Entry function of LRA constraint pass. Return true if the
3444 constraint pass did change the code. */
3445 bool
3446 lra_constraints (bool first_p)
3448 bool changed_p;
3449 int i, hard_regno, new_insns_num;
3450 unsigned int min_len, new_min_len, uid;
3451 rtx set, x, reg, dest_reg;
3452 basic_block last_bb;
3453 bitmap_head equiv_insn_bitmap;
3454 bitmap_iterator bi;
3456 lra_constraint_iter++;
3457 if (lra_dump_file != NULL)
3458 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
3459 lra_constraint_iter);
3460 lra_constraint_iter_after_spill++;
3461 if (lra_constraint_iter_after_spill > LRA_MAX_CONSTRAINT_ITERATION_NUMBER)
3462 internal_error
3463 ("Maximum number of LRA constraint passes is achieved (%d)\n",
3464 LRA_MAX_CONSTRAINT_ITERATION_NUMBER);
3465 changed_p = false;
3466 lra_risky_transformations_p = false;
3467 new_insn_uid_start = get_max_uid ();
3468 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
3469 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
3470 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3471 if (lra_reg_info[i].nrefs != 0)
3473 ira_reg_equiv[i].profitable_p = true;
3474 reg = regno_reg_rtx[i];
3475 if ((hard_regno = lra_get_regno_hard_regno (i)) >= 0)
3477 int j, nregs;
3479 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
3480 for (j = 0; j < nregs; j++)
3481 df_set_regs_ever_live (hard_regno + j, true);
3483 else if ((x = get_equiv_substitution (reg)) != reg)
3485 bool pseudo_p = contains_reg_p (x, false, false);
3486 rtx set, insn;
3488 /* After RTL transformation, we can not guarantee that
3489 pseudo in the substitution was not reloaded which might
3490 make equivalence invalid. For example, in reverse
3491 equiv of p0
3493 p0 <- ...
3495 equiv_mem <- p0
3497 the memory address register was reloaded before the 2nd
3498 insn. */
3499 if ((! first_p && pseudo_p)
3500 /* We don't use DF for compilation speed sake. So it
3501 is problematic to update live info when we use an
3502 equivalence containing pseudos in more than one
3503 BB. */
3504 || (pseudo_p && multi_block_pseudo_p (i))
3505 /* If an init insn was deleted for some reason, cancel
3506 the equiv. We could update the equiv insns after
3507 transformations including an equiv insn deletion
3508 but it is not worthy as such cases are extremely
3509 rare. */
3510 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
3511 /* If it is not a reverse equivalence, we check that a
3512 pseudo in rhs of the init insn is not dying in the
3513 insn. Otherwise, the live info at the beginning of
3514 the corresponding BB might be wrong after we
3515 removed the insn. When the equiv can be a
3516 constant, the right hand side of the init insn can
3517 be a pseudo. */
3518 || (! ((insn = ira_reg_equiv[i].init_insns) != NULL_RTX
3519 && INSN_P (insn)
3520 && (set = single_set (insn)) != NULL_RTX
3521 && REG_P (SET_DEST (set))
3522 && (int) REGNO (SET_DEST (set)) == i)
3523 && init_insn_rhs_dead_pseudo_p (i))
3524 /* Prevent access beyond equivalent memory for
3525 paradoxical subregs. */
3526 || (MEM_P (x)
3527 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
3528 > GET_MODE_SIZE (GET_MODE (x)))))
3529 ira_reg_equiv[i].defined_p = false;
3530 if (contains_reg_p (x, false, true))
3531 ira_reg_equiv[i].profitable_p = false;
3532 if (get_equiv_substitution (reg) != reg)
3533 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
3536 /* We should add all insns containing pseudos which should be
3537 substituted by their equivalences. */
3538 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
3539 lra_push_insn_by_uid (uid);
3540 lra_eliminate (false);
3541 min_len = lra_insn_stack_length ();
3542 new_insns_num = 0;
3543 last_bb = NULL;
3544 changed_p = false;
3545 while ((new_min_len = lra_insn_stack_length ()) != 0)
3547 curr_insn = lra_pop_insn ();
3548 --new_min_len;
3549 curr_bb = BLOCK_FOR_INSN (curr_insn);
3550 if (curr_bb != last_bb)
3552 last_bb = curr_bb;
3553 bb_reload_num = lra_curr_reload_num;
3555 if (min_len > new_min_len)
3557 min_len = new_min_len;
3558 new_insns_num = 0;
3560 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
3561 internal_error
3562 ("Max. number of generated reload insns per insn is achieved (%d)\n",
3563 MAX_RELOAD_INSNS_NUMBER);
3564 new_insns_num++;
3565 if (DEBUG_INSN_P (curr_insn))
3567 /* We need to check equivalence in debug insn and change
3568 pseudo to the equivalent value if necessary. */
3569 curr_id = lra_get_insn_recog_data (curr_insn);
3570 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
3572 rtx old = *curr_id->operand_loc[0];
3573 *curr_id->operand_loc[0]
3574 = simplify_replace_fn_rtx (old, NULL_RTX,
3575 loc_equivalence_callback, NULL);
3576 if (old != *curr_id->operand_loc[0])
3578 lra_update_insn_regno_info (curr_insn);
3579 changed_p = true;
3583 else if (INSN_P (curr_insn))
3585 if ((set = single_set (curr_insn)) != NULL_RTX)
3587 dest_reg = SET_DEST (set);
3588 /* The equivalence pseudo could be set up as SUBREG in a
3589 case when it is a call restore insn in a mode
3590 different from the pseudo mode. */
3591 if (GET_CODE (dest_reg) == SUBREG)
3592 dest_reg = SUBREG_REG (dest_reg);
3593 if ((REG_P (dest_reg)
3594 && (x = get_equiv_substitution (dest_reg)) != dest_reg
3595 /* Remove insns which set up a pseudo whose value
3596 can not be changed. Such insns might be not in
3597 init_insns because we don't update equiv data
3598 during insn transformations.
3600 As an example, let suppose that a pseudo got
3601 hard register and on the 1st pass was not
3602 changed to equivalent constant. We generate an
3603 additional insn setting up the pseudo because of
3604 secondary memory movement. Then the pseudo is
3605 spilled and we use the equiv constant. In this
3606 case we should remove the additional insn and
3607 this insn is not init_insns list. */
3608 && (! MEM_P (x) || MEM_READONLY_P (x)
3609 || in_list_p (curr_insn,
3610 ira_reg_equiv
3611 [REGNO (dest_reg)].init_insns)))
3612 || (((x = get_equiv_substitution (SET_SRC (set)))
3613 != SET_SRC (set))
3614 && in_list_p (curr_insn,
3615 ira_reg_equiv
3616 [REGNO (SET_SRC (set))].init_insns)))
3618 /* This is equiv init insn of pseudo which did not get a
3619 hard register -- remove the insn. */
3620 if (lra_dump_file != NULL)
3622 fprintf (lra_dump_file,
3623 " Removing equiv init insn %i (freq=%d)\n",
3624 INSN_UID (curr_insn),
3625 BLOCK_FOR_INSN (curr_insn)->frequency);
3626 dump_insn_slim (lra_dump_file, curr_insn);
3628 if (contains_reg_p (x, true, false))
3629 lra_risky_transformations_p = true;
3630 lra_set_insn_deleted (curr_insn);
3631 continue;
3634 curr_id = lra_get_insn_recog_data (curr_insn);
3635 curr_static_id = curr_id->insn_static_data;
3636 init_curr_insn_input_reloads ();
3637 init_curr_operand_mode ();
3638 if (curr_insn_transform ())
3639 changed_p = true;
3640 /* Check non-transformed insns too for equiv change as USE
3641 or CLOBBER don't need reloads but can contain pseudos
3642 being changed on their equivalences. */
3643 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
3644 && loc_equivalence_change_p (&PATTERN (curr_insn)))
3646 lra_update_insn_regno_info (curr_insn);
3647 changed_p = true;
3651 bitmap_clear (&equiv_insn_bitmap);
3652 /* If we used a new hard regno, changed_p should be true because the
3653 hard reg is assigned to a new pseudo. */
3654 #ifdef ENABLE_CHECKING
3655 if (! changed_p)
3657 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3658 if (lra_reg_info[i].nrefs != 0
3659 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
3661 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
3663 for (j = 0; j < nregs; j++)
3664 lra_assert (df_regs_ever_live_p (hard_regno + j));
3667 #endif
3668 return changed_p;
3671 /* Initiate the LRA constraint pass. It is done once per
3672 function. */
3673 void
3674 lra_constraints_init (void)
3678 /* Finalize the LRA constraint pass. It is done once per
3679 function. */
3680 void
3681 lra_constraints_finish (void)
3687 /* This page contains code to do inheritance/split
3688 transformations. */
3690 /* Number of reloads passed so far in current EBB. */
3691 static int reloads_num;
3693 /* Number of calls passed so far in current EBB. */
3694 static int calls_num;
3696 /* Current reload pseudo check for validity of elements in
3697 USAGE_INSNS. */
3698 static int curr_usage_insns_check;
3700 /* Info about last usage of registers in EBB to do inheritance/split
3701 transformation. Inheritance transformation is done from a spilled
3702 pseudo and split transformations from a hard register or a pseudo
3703 assigned to a hard register. */
3704 struct usage_insns
3706 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
3707 value INSNS is valid. The insns is chain of optional debug insns
3708 and a finishing non-debug insn using the corresponding reg. */
3709 int check;
3710 /* Value of global reloads_num at the last insn in INSNS. */
3711 int reloads_num;
3712 /* Value of global reloads_nums at the last insn in INSNS. */
3713 int calls_num;
3714 /* It can be true only for splitting. And it means that the restore
3715 insn should be put after insn given by the following member. */
3716 bool after_p;
3717 /* Next insns in the current EBB which use the original reg and the
3718 original reg value is not changed between the current insn and
3719 the next insns. In order words, e.g. for inheritance, if we need
3720 to use the original reg value again in the next insns we can try
3721 to use the value in a hard register from a reload insn of the
3722 current insn. */
3723 rtx insns;
3726 /* Map: regno -> corresponding pseudo usage insns. */
3727 static struct usage_insns *usage_insns;
3729 static void
3730 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
3732 usage_insns[regno].check = curr_usage_insns_check;
3733 usage_insns[regno].insns = insn;
3734 usage_insns[regno].reloads_num = reloads_num;
3735 usage_insns[regno].calls_num = calls_num;
3736 usage_insns[regno].after_p = after_p;
3739 /* The function is used to form list REGNO usages which consists of
3740 optional debug insns finished by a non-debug insn using REGNO.
3741 RELOADS_NUM is current number of reload insns processed so far. */
3742 static void
3743 add_next_usage_insn (int regno, rtx insn, int reloads_num)
3745 rtx next_usage_insns;
3747 if (usage_insns[regno].check == curr_usage_insns_check
3748 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
3749 && DEBUG_INSN_P (insn))
3751 /* Check that we did not add the debug insn yet. */
3752 if (next_usage_insns != insn
3753 && (GET_CODE (next_usage_insns) != INSN_LIST
3754 || XEXP (next_usage_insns, 0) != insn))
3755 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
3756 next_usage_insns);
3758 else if (NONDEBUG_INSN_P (insn))
3759 setup_next_usage_insn (regno, insn, reloads_num, false);
3760 else
3761 usage_insns[regno].check = 0;
3764 /* Replace all references to register OLD_REGNO in *LOC with pseudo
3765 register NEW_REG. Return true if any change was made. */
3766 static bool
3767 substitute_pseudo (rtx *loc, int old_regno, rtx new_reg)
3769 rtx x = *loc;
3770 bool result = false;
3771 enum rtx_code code;
3772 const char *fmt;
3773 int i, j;
3775 if (x == NULL_RTX)
3776 return false;
3778 code = GET_CODE (x);
3779 if (code == REG && (int) REGNO (x) == old_regno)
3781 enum machine_mode mode = GET_MODE (*loc);
3782 enum machine_mode inner_mode = GET_MODE (new_reg);
3784 if (mode != inner_mode)
3786 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
3787 || ! SCALAR_INT_MODE_P (inner_mode))
3788 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
3789 else
3790 new_reg = gen_lowpart_SUBREG (mode, new_reg);
3792 *loc = new_reg;
3793 return true;
3796 /* Scan all the operand sub-expressions. */
3797 fmt = GET_RTX_FORMAT (code);
3798 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3800 if (fmt[i] == 'e')
3802 if (substitute_pseudo (&XEXP (x, i), old_regno, new_reg))
3803 result = true;
3805 else if (fmt[i] == 'E')
3807 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3808 if (substitute_pseudo (&XVECEXP (x, i, j), old_regno, new_reg))
3809 result = true;
3812 return result;
3815 /* Return first non-debug insn in list USAGE_INSNS. */
3816 static rtx
3817 skip_usage_debug_insns (rtx usage_insns)
3819 rtx insn;
3821 /* Skip debug insns. */
3822 for (insn = usage_insns;
3823 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
3824 insn = XEXP (insn, 1))
3826 return insn;
3829 /* Return true if we need secondary memory moves for insn in
3830 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
3831 into the insn. */
3832 static bool
3833 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
3834 rtx usage_insns ATTRIBUTE_UNUSED)
3836 #ifndef SECONDARY_MEMORY_NEEDED
3837 return false;
3838 #else
3839 rtx insn, set, dest;
3840 enum reg_class cl;
3842 if (inher_cl == ALL_REGS
3843 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
3844 return false;
3845 lra_assert (INSN_P (insn));
3846 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
3847 return false;
3848 dest = SET_DEST (set);
3849 if (! REG_P (dest))
3850 return false;
3851 lra_assert (inher_cl != NO_REGS);
3852 cl = get_reg_class (REGNO (dest));
3853 return (cl != NO_REGS && cl != ALL_REGS
3854 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
3855 #endif
3858 /* Registers involved in inheritance/split in the current EBB
3859 (inheritance/split pseudos and original registers). */
3860 static bitmap_head check_only_regs;
3862 /* Do inheritance transformations for insn INSN, which defines (if
3863 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
3864 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
3865 form as the "insns" field of usage_insns. Return true if we
3866 succeed in such transformation.
3868 The transformations look like:
3870 p <- ... i <- ...
3871 ... p <- i (new insn)
3872 ... =>
3873 <- ... p ... <- ... i ...
3875 ... i <- p (new insn)
3876 <- ... p ... <- ... i ...
3877 ... =>
3878 <- ... p ... <- ... i ...
3879 where p is a spilled original pseudo and i is a new inheritance pseudo.
3882 The inheritance pseudo has the smallest class of two classes CL and
3883 class of ORIGINAL REGNO. */
3884 static bool
3885 inherit_reload_reg (bool def_p, int original_regno,
3886 enum reg_class cl, rtx insn, rtx next_usage_insns)
3888 enum reg_class rclass = lra_get_allocno_class (original_regno);
3889 rtx original_reg = regno_reg_rtx[original_regno];
3890 rtx new_reg, new_insns, usage_insn;
3892 lra_assert (! usage_insns[original_regno].after_p);
3893 if (lra_dump_file != NULL)
3894 fprintf (lra_dump_file,
3895 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
3896 if (! ira_reg_classes_intersect_p[cl][rclass])
3898 if (lra_dump_file != NULL)
3900 fprintf (lra_dump_file,
3901 " Rejecting inheritance for %d "
3902 "because of disjoint classes %s and %s\n",
3903 original_regno, reg_class_names[cl],
3904 reg_class_names[rclass]);
3905 fprintf (lra_dump_file,
3906 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
3908 return false;
3910 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
3911 /* We don't use a subset of two classes because it can be
3912 NO_REGS. This transformation is still profitable in most
3913 cases even if the classes are not intersected as register
3914 move is probably cheaper than a memory load. */
3915 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
3917 if (lra_dump_file != NULL)
3918 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
3919 reg_class_names[cl], reg_class_names[rclass]);
3921 rclass = cl;
3923 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
3925 /* Reject inheritance resulting in secondary memory moves.
3926 Otherwise, there is a danger in LRA cycling. Also such
3927 transformation will be unprofitable. */
3928 if (lra_dump_file != NULL)
3930 rtx insn = skip_usage_debug_insns (next_usage_insns);
3931 rtx set = single_set (insn);
3933 lra_assert (set != NULL_RTX);
3935 rtx dest = SET_DEST (set);
3937 lra_assert (REG_P (dest));
3938 fprintf (lra_dump_file,
3939 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
3940 "as secondary mem is needed\n",
3941 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
3942 original_regno, reg_class_names[rclass]);
3943 fprintf (lra_dump_file,
3944 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
3946 return false;
3948 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
3949 rclass, "inheritance");
3950 start_sequence ();
3951 if (def_p)
3952 emit_move_insn (original_reg, new_reg);
3953 else
3954 emit_move_insn (new_reg, original_reg);
3955 new_insns = get_insns ();
3956 end_sequence ();
3957 if (NEXT_INSN (new_insns) != NULL_RTX)
3959 if (lra_dump_file != NULL)
3961 fprintf (lra_dump_file,
3962 " Rejecting inheritance %d->%d "
3963 "as it results in 2 or more insns:\n",
3964 original_regno, REGNO (new_reg));
3965 dump_rtl_slim (lra_dump_file, new_insns, NULL_RTX, -1, 0);
3966 fprintf (lra_dump_file,
3967 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
3969 return false;
3971 substitute_pseudo (&insn, original_regno, new_reg);
3972 lra_update_insn_regno_info (insn);
3973 if (! def_p)
3974 /* We now have a new usage insn for original regno. */
3975 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
3976 if (lra_dump_file != NULL)
3977 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
3978 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
3979 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
3980 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
3981 bitmap_set_bit (&check_only_regs, original_regno);
3982 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
3983 if (def_p)
3984 lra_process_new_insns (insn, NULL_RTX, new_insns,
3985 "Add original<-inheritance");
3986 else
3987 lra_process_new_insns (insn, new_insns, NULL_RTX,
3988 "Add inheritance<-original");
3989 while (next_usage_insns != NULL_RTX)
3991 if (GET_CODE (next_usage_insns) != INSN_LIST)
3993 usage_insn = next_usage_insns;
3994 lra_assert (NONDEBUG_INSN_P (usage_insn));
3995 next_usage_insns = NULL;
3997 else
3999 usage_insn = XEXP (next_usage_insns, 0);
4000 lra_assert (DEBUG_INSN_P (usage_insn));
4001 next_usage_insns = XEXP (next_usage_insns, 1);
4003 substitute_pseudo (&usage_insn, original_regno, new_reg);
4004 lra_update_insn_regno_info (usage_insn);
4005 if (lra_dump_file != NULL)
4007 fprintf (lra_dump_file,
4008 " Inheritance reuse change %d->%d (bb%d):\n",
4009 original_regno, REGNO (new_reg),
4010 BLOCK_FOR_INSN (usage_insn)->index);
4011 dump_insn_slim (lra_dump_file, usage_insn);
4014 if (lra_dump_file != NULL)
4015 fprintf (lra_dump_file,
4016 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4017 return true;
4020 /* Return true if we need a caller save/restore for pseudo REGNO which
4021 was assigned to a hard register. */
4022 static inline bool
4023 need_for_call_save_p (int regno)
4025 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4026 return (usage_insns[regno].calls_num < calls_num
4027 && (overlaps_hard_reg_set_p
4028 (call_used_reg_set,
4029 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])));
4032 /* Global registers occuring in the current EBB. */
4033 static bitmap_head ebb_global_regs;
4035 /* Return true if we need a split for hard register REGNO or pseudo
4036 REGNO which was assigned to a hard register.
4037 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4038 used for reloads since the EBB end. It is an approximation of the
4039 used hard registers in the split range. The exact value would
4040 require expensive calculations. If we were aggressive with
4041 splitting because of the approximation, the split pseudo will save
4042 the same hard register assignment and will be removed in the undo
4043 pass. We still need the approximation because too aggressive
4044 splitting would result in too inaccurate cost calculation in the
4045 assignment pass because of too many generated moves which will be
4046 probably removed in the undo pass. */
4047 static inline bool
4048 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4050 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4052 lra_assert (hard_regno >= 0);
4053 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4054 /* Don't split eliminable hard registers, otherwise we can
4055 split hard registers like hard frame pointer, which
4056 lives on BB start/end according to DF-infrastructure,
4057 when there is a pseudo assigned to the register and
4058 living in the same BB. */
4059 && (regno >= FIRST_PSEUDO_REGISTER
4060 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4061 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4062 /* We need at least 2 reloads to make pseudo splitting
4063 profitable. We should provide hard regno splitting in
4064 any case to solve 1st insn scheduling problem when
4065 moving hard register definition up might result in
4066 impossibility to find hard register for reload pseudo of
4067 small register class. */
4068 && (usage_insns[regno].reloads_num
4069 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 2) < reloads_num)
4070 && (regno < FIRST_PSEUDO_REGISTER
4071 /* For short living pseudos, spilling + inheritance can
4072 be considered a substitution for splitting.
4073 Therefore we do not splitting for local pseudos. It
4074 decreases also aggressiveness of splitting. The
4075 minimal number of references is chosen taking into
4076 account that for 2 references splitting has no sense
4077 as we can just spill the pseudo. */
4078 || (regno >= FIRST_PSEUDO_REGISTER
4079 && lra_reg_info[regno].nrefs > 3
4080 && bitmap_bit_p (&ebb_global_regs, regno))))
4081 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4084 /* Return class for the split pseudo created from original pseudo with
4085 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4086 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4087 results in no secondary memory movements. */
4088 static enum reg_class
4089 choose_split_class (enum reg_class allocno_class,
4090 int hard_regno ATTRIBUTE_UNUSED,
4091 enum machine_mode mode ATTRIBUTE_UNUSED)
4093 #ifndef SECONDARY_MEMORY_NEEDED
4094 return allocno_class;
4095 #else
4096 int i;
4097 enum reg_class cl, best_cl = NO_REGS;
4098 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4099 = REGNO_REG_CLASS (hard_regno);
4101 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4102 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4103 return allocno_class;
4104 for (i = 0;
4105 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4106 i++)
4107 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4108 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4109 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4110 && (best_cl == NO_REGS
4111 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4112 best_cl = cl;
4113 return best_cl;
4114 #endif
4117 /* Do split transformations for insn INSN, which defines or uses
4118 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4119 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4120 "insns" field of usage_insns.
4122 The transformations look like:
4124 p <- ... p <- ...
4125 ... s <- p (new insn -- save)
4126 ... =>
4127 ... p <- s (new insn -- restore)
4128 <- ... p ... <- ... p ...
4130 <- ... p ... <- ... p ...
4131 ... s <- p (new insn -- save)
4132 ... =>
4133 ... p <- s (new insn -- restore)
4134 <- ... p ... <- ... p ...
4136 where p is an original pseudo got a hard register or a hard
4137 register and s is a new split pseudo. The save is put before INSN
4138 if BEFORE_P is true. Return true if we succeed in such
4139 transformation. */
4140 static bool
4141 split_reg (bool before_p, int original_regno, rtx insn, rtx next_usage_insns)
4143 enum reg_class rclass;
4144 rtx original_reg;
4145 int hard_regno, nregs;
4146 rtx new_reg, save, restore, usage_insn;
4147 bool after_p;
4148 bool call_save_p;
4150 if (original_regno < FIRST_PSEUDO_REGISTER)
4152 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4153 hard_regno = original_regno;
4154 call_save_p = false;
4155 nregs = 1;
4157 else
4159 hard_regno = reg_renumber[original_regno];
4160 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4161 rclass = lra_get_allocno_class (original_regno);
4162 original_reg = regno_reg_rtx[original_regno];
4163 call_save_p = need_for_call_save_p (original_regno);
4165 original_reg = regno_reg_rtx[original_regno];
4166 lra_assert (hard_regno >= 0);
4167 if (lra_dump_file != NULL)
4168 fprintf (lra_dump_file,
4169 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4170 if (call_save_p)
4172 enum machine_mode sec_mode;
4174 #ifdef SECONDARY_MEMORY_NEEDED_MODE
4175 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (original_reg));
4176 #else
4177 sec_mode = GET_MODE (original_reg);
4178 #endif
4179 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
4180 NO_REGS, "save");
4182 else
4184 rclass = choose_split_class (rclass, hard_regno,
4185 GET_MODE (original_reg));
4186 if (rclass == NO_REGS)
4188 if (lra_dump_file != NULL)
4190 fprintf (lra_dump_file,
4191 " Rejecting split of %d(%s): "
4192 "no good reg class for %d(%s)\n",
4193 original_regno,
4194 reg_class_names[lra_get_allocno_class (original_regno)],
4195 hard_regno,
4196 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4197 fprintf
4198 (lra_dump_file,
4199 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4201 return false;
4203 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4204 rclass, "split");
4205 reg_renumber[REGNO (new_reg)] = hard_regno;
4207 save = emit_spill_move (true, new_reg, original_reg);
4208 if (NEXT_INSN (save) != NULL_RTX)
4210 lra_assert (! call_save_p);
4211 if (lra_dump_file != NULL)
4213 fprintf
4214 (lra_dump_file,
4215 " Rejecting split %d->%d resulting in > 2 %s save insns:\n",
4216 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4217 dump_rtl_slim (lra_dump_file, save, NULL_RTX, -1, 0);
4218 fprintf (lra_dump_file,
4219 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4221 return false;
4223 restore = emit_spill_move (false, new_reg, original_reg);
4224 if (NEXT_INSN (restore) != NULL_RTX)
4226 lra_assert (! call_save_p);
4227 if (lra_dump_file != NULL)
4229 fprintf (lra_dump_file,
4230 " Rejecting split %d->%d "
4231 "resulting in > 2 %s restore insns:\n",
4232 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4233 dump_rtl_slim (lra_dump_file, restore, NULL_RTX, -1, 0);
4234 fprintf (lra_dump_file,
4235 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4237 return false;
4239 after_p = usage_insns[original_regno].after_p;
4240 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4241 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4242 bitmap_set_bit (&check_only_regs, original_regno);
4243 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4244 for (;;)
4246 if (GET_CODE (next_usage_insns) != INSN_LIST)
4248 usage_insn = next_usage_insns;
4249 break;
4251 usage_insn = XEXP (next_usage_insns, 0);
4252 lra_assert (DEBUG_INSN_P (usage_insn));
4253 next_usage_insns = XEXP (next_usage_insns, 1);
4254 substitute_pseudo (&usage_insn, original_regno, new_reg);
4255 lra_update_insn_regno_info (usage_insn);
4256 if (lra_dump_file != NULL)
4258 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
4259 original_regno, REGNO (new_reg));
4260 dump_insn_slim (lra_dump_file, usage_insn);
4263 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
4264 lra_assert (usage_insn != insn || (after_p && before_p));
4265 lra_process_new_insns (usage_insn, after_p ? NULL_RTX : restore,
4266 after_p ? restore : NULL_RTX,
4267 call_save_p
4268 ? "Add reg<-save" : "Add reg<-split");
4269 lra_process_new_insns (insn, before_p ? save : NULL_RTX,
4270 before_p ? NULL_RTX : save,
4271 call_save_p
4272 ? "Add save<-reg" : "Add split<-reg");
4273 if (nregs > 1)
4274 /* If we are trying to split multi-register. We should check
4275 conflicts on the next assignment sub-pass. IRA can allocate on
4276 sub-register levels, LRA do this on pseudos level right now and
4277 this discrepancy may create allocation conflicts after
4278 splitting. */
4279 lra_risky_transformations_p = true;
4280 if (lra_dump_file != NULL)
4281 fprintf (lra_dump_file,
4282 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4283 return true;
4286 /* Recognize that we need a split transformation for insn INSN, which
4287 defines or uses REGNO in its insn biggest MODE (we use it only if
4288 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
4289 hard registers which might be used for reloads since the EBB end.
4290 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
4291 uid before starting INSN processing. Return true if we succeed in
4292 such transformation. */
4293 static bool
4294 split_if_necessary (int regno, enum machine_mode mode,
4295 HARD_REG_SET potential_reload_hard_regs,
4296 bool before_p, rtx insn, int max_uid)
4298 bool res = false;
4299 int i, nregs = 1;
4300 rtx next_usage_insns;
4302 if (regno < FIRST_PSEUDO_REGISTER)
4303 nregs = hard_regno_nregs[regno][mode];
4304 for (i = 0; i < nregs; i++)
4305 if (usage_insns[regno + i].check == curr_usage_insns_check
4306 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
4307 /* To avoid processing the register twice or more. */
4308 && ((GET_CODE (next_usage_insns) != INSN_LIST
4309 && INSN_UID (next_usage_insns) < max_uid)
4310 || (GET_CODE (next_usage_insns) == INSN_LIST
4311 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
4312 && need_for_split_p (potential_reload_hard_regs, regno + i)
4313 && split_reg (before_p, regno + i, insn, next_usage_insns))
4314 res = true;
4315 return res;
4318 /* Check only registers living at the current program point in the
4319 current EBB. */
4320 static bitmap_head live_regs;
4322 /* Update live info in EBB given by its HEAD and TAIL insns after
4323 inheritance/split transformation. The function removes dead moves
4324 too. */
4325 static void
4326 update_ebb_live_info (rtx head, rtx tail)
4328 unsigned int j;
4329 int regno;
4330 bool live_p;
4331 rtx prev_insn, set;
4332 bool remove_p;
4333 basic_block last_bb, prev_bb, curr_bb;
4334 bitmap_iterator bi;
4335 struct lra_insn_reg *reg;
4336 edge e;
4337 edge_iterator ei;
4339 last_bb = BLOCK_FOR_INSN (tail);
4340 prev_bb = NULL;
4341 for (curr_insn = tail;
4342 curr_insn != PREV_INSN (head);
4343 curr_insn = prev_insn)
4345 prev_insn = PREV_INSN (curr_insn);
4346 /* We need to process empty blocks too. They contain
4347 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
4348 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
4349 continue;
4350 curr_bb = BLOCK_FOR_INSN (curr_insn);
4351 if (curr_bb != prev_bb)
4353 if (prev_bb != NULL)
4355 /* Update df_get_live_in (prev_bb): */
4356 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4357 if (bitmap_bit_p (&live_regs, j))
4358 bitmap_set_bit (df_get_live_in (prev_bb), j);
4359 else
4360 bitmap_clear_bit (df_get_live_in (prev_bb), j);
4362 if (curr_bb != last_bb)
4364 /* Update df_get_live_out (curr_bb): */
4365 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4367 live_p = bitmap_bit_p (&live_regs, j);
4368 if (! live_p)
4369 FOR_EACH_EDGE (e, ei, curr_bb->succs)
4370 if (bitmap_bit_p (df_get_live_in (e->dest), j))
4372 live_p = true;
4373 break;
4375 if (live_p)
4376 bitmap_set_bit (df_get_live_out (curr_bb), j);
4377 else
4378 bitmap_clear_bit (df_get_live_out (curr_bb), j);
4381 prev_bb = curr_bb;
4382 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
4384 if (! NONDEBUG_INSN_P (curr_insn))
4385 continue;
4386 curr_id = lra_get_insn_recog_data (curr_insn);
4387 remove_p = false;
4388 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
4389 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
4390 && bitmap_bit_p (&check_only_regs, regno)
4391 && ! bitmap_bit_p (&live_regs, regno))
4392 remove_p = true;
4393 /* See which defined values die here. */
4394 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4395 if (reg->type == OP_OUT && ! reg->subreg_p)
4396 bitmap_clear_bit (&live_regs, reg->regno);
4397 /* Mark each used value as live. */
4398 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4399 if (reg->type != OP_OUT
4400 && bitmap_bit_p (&check_only_regs, reg->regno))
4401 bitmap_set_bit (&live_regs, reg->regno);
4402 /* It is quite important to remove dead move insns because it
4403 means removing dead store. We don't need to process them for
4404 constraints. */
4405 if (remove_p)
4407 if (lra_dump_file != NULL)
4409 fprintf (lra_dump_file, " Removing dead insn:\n ");
4410 dump_insn_slim (lra_dump_file, curr_insn);
4412 lra_set_insn_deleted (curr_insn);
4417 /* The structure describes info to do an inheritance for the current
4418 insn. We need to collect such info first before doing the
4419 transformations because the transformations change the insn
4420 internal representation. */
4421 struct to_inherit
4423 /* Original regno. */
4424 int regno;
4425 /* Subsequent insns which can inherit original reg value. */
4426 rtx insns;
4429 /* Array containing all info for doing inheritance from the current
4430 insn. */
4431 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
4433 /* Number elements in the previous array. */
4434 static int to_inherit_num;
4436 /* Add inheritance info REGNO and INSNS. Their meaning is described in
4437 structure to_inherit. */
4438 static void
4439 add_to_inherit (int regno, rtx insns)
4441 int i;
4443 for (i = 0; i < to_inherit_num; i++)
4444 if (to_inherit[i].regno == regno)
4445 return;
4446 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
4447 to_inherit[to_inherit_num].regno = regno;
4448 to_inherit[to_inherit_num++].insns = insns;
4451 /* Return the last non-debug insn in basic block BB, or the block begin
4452 note if none. */
4453 static rtx
4454 get_last_insertion_point (basic_block bb)
4456 rtx insn;
4458 FOR_BB_INSNS_REVERSE (bb, insn)
4459 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
4460 return insn;
4461 gcc_unreachable ();
4464 /* Set up RES by registers living on edges FROM except the edge (FROM,
4465 TO) or by registers set up in a jump insn in BB FROM. */
4466 static void
4467 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
4469 rtx last;
4470 struct lra_insn_reg *reg;
4471 edge e;
4472 edge_iterator ei;
4474 lra_assert (to != NULL);
4475 bitmap_clear (res);
4476 FOR_EACH_EDGE (e, ei, from->succs)
4477 if (e->dest != to)
4478 bitmap_ior_into (res, df_get_live_in (e->dest));
4479 last = get_last_insertion_point (from);
4480 if (! JUMP_P (last))
4481 return;
4482 curr_id = lra_get_insn_recog_data (last);
4483 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4484 if (reg->type != OP_IN)
4485 bitmap_set_bit (res, reg->regno);
4488 /* Used as a temporary results of some bitmap calculations. */
4489 static bitmap_head temp_bitmap;
4491 /* Do inheritance/split transformations in EBB starting with HEAD and
4492 finishing on TAIL. We process EBB insns in the reverse order.
4493 Return true if we did any inheritance/split transformation in the
4494 EBB.
4496 We should avoid excessive splitting which results in worse code
4497 because of inaccurate cost calculations for spilling new split
4498 pseudos in such case. To achieve this we do splitting only if
4499 register pressure is high in given basic block and there are reload
4500 pseudos requiring hard registers. We could do more register
4501 pressure calculations at any given program point to avoid necessary
4502 splitting even more but it is to expensive and the current approach
4503 works well enough. */
4504 static bool
4505 inherit_in_ebb (rtx head, rtx tail)
4507 int i, src_regno, dst_regno, nregs;
4508 bool change_p, succ_p;
4509 rtx prev_insn, next_usage_insns, set, last_insn;
4510 enum reg_class cl;
4511 struct lra_insn_reg *reg;
4512 basic_block last_processed_bb, curr_bb = NULL;
4513 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
4514 bitmap to_process;
4515 unsigned int j;
4516 bitmap_iterator bi;
4517 bool head_p, after_p;
4519 change_p = false;
4520 curr_usage_insns_check++;
4521 reloads_num = calls_num = 0;
4522 bitmap_clear (&check_only_regs);
4523 last_processed_bb = NULL;
4524 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
4525 CLEAR_HARD_REG_SET (live_hard_regs);
4526 /* We don't process new insns generated in the loop. */
4527 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
4529 prev_insn = PREV_INSN (curr_insn);
4530 if (BLOCK_FOR_INSN (curr_insn) != NULL)
4531 curr_bb = BLOCK_FOR_INSN (curr_insn);
4532 if (last_processed_bb != curr_bb)
4534 /* We are at the end of BB. Add qualified living
4535 pseudos for potential splitting. */
4536 to_process = df_get_live_out (curr_bb);
4537 if (last_processed_bb != NULL)
4539 /* We are somewhere in the middle of EBB. */
4540 get_live_on_other_edges (curr_bb, last_processed_bb,
4541 &temp_bitmap);
4542 to_process = &temp_bitmap;
4544 last_processed_bb = curr_bb;
4545 last_insn = get_last_insertion_point (curr_bb);
4546 after_p = (! JUMP_P (last_insn)
4547 && (! CALL_P (last_insn)
4548 || (find_reg_note (last_insn,
4549 REG_NORETURN, NULL_RTX) == NULL_RTX
4550 && ! SIBLING_CALL_P (last_insn))));
4551 REG_SET_TO_HARD_REG_SET (live_hard_regs, df_get_live_out (curr_bb));
4552 IOR_HARD_REG_SET (live_hard_regs, eliminable_regset);
4553 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
4554 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
4555 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
4557 if ((int) j >= lra_constraint_new_regno_start)
4558 break;
4559 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
4561 if (j < FIRST_PSEUDO_REGISTER)
4562 SET_HARD_REG_BIT (live_hard_regs, j);
4563 else
4564 add_to_hard_reg_set (&live_hard_regs,
4565 PSEUDO_REGNO_MODE (j),
4566 reg_renumber[j]);
4567 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
4571 src_regno = dst_regno = -1;
4572 if (NONDEBUG_INSN_P (curr_insn)
4573 && (set = single_set (curr_insn)) != NULL_RTX
4574 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
4576 src_regno = REGNO (SET_SRC (set));
4577 dst_regno = REGNO (SET_DEST (set));
4579 if (src_regno < lra_constraint_new_regno_start
4580 && src_regno >= FIRST_PSEUDO_REGISTER
4581 && reg_renumber[src_regno] < 0
4582 && dst_regno >= lra_constraint_new_regno_start
4583 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
4585 /* 'reload_pseudo <- original_pseudo'. */
4586 reloads_num++;
4587 succ_p = false;
4588 if (usage_insns[src_regno].check == curr_usage_insns_check
4589 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
4590 succ_p = inherit_reload_reg (false, src_regno, cl,
4591 curr_insn, next_usage_insns);
4592 if (succ_p)
4593 change_p = true;
4594 else
4595 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
4596 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
4597 IOR_HARD_REG_SET (potential_reload_hard_regs,
4598 reg_class_contents[cl]);
4600 else if (src_regno >= lra_constraint_new_regno_start
4601 && dst_regno < lra_constraint_new_regno_start
4602 && dst_regno >= FIRST_PSEUDO_REGISTER
4603 && reg_renumber[dst_regno] < 0
4604 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
4605 && usage_insns[dst_regno].check == curr_usage_insns_check
4606 && (next_usage_insns
4607 = usage_insns[dst_regno].insns) != NULL_RTX)
4609 reloads_num++;
4610 /* 'original_pseudo <- reload_pseudo'. */
4611 if (! JUMP_P (curr_insn)
4612 && inherit_reload_reg (true, dst_regno, cl,
4613 curr_insn, next_usage_insns))
4614 change_p = true;
4615 /* Invalidate. */
4616 usage_insns[dst_regno].check = 0;
4617 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
4618 IOR_HARD_REG_SET (potential_reload_hard_regs,
4619 reg_class_contents[cl]);
4621 else if (INSN_P (curr_insn))
4623 int max_uid = get_max_uid ();
4625 curr_id = lra_get_insn_recog_data (curr_insn);
4626 to_inherit_num = 0;
4627 /* Process insn definitions. */
4628 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4629 if (reg->type != OP_IN
4630 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
4632 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
4633 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
4634 && usage_insns[dst_regno].check == curr_usage_insns_check
4635 && (next_usage_insns
4636 = usage_insns[dst_regno].insns) != NULL_RTX)
4638 struct lra_insn_reg *r;
4640 for (r = curr_id->regs; r != NULL; r = r->next)
4641 if (r->type != OP_OUT && r->regno == dst_regno)
4642 break;
4643 /* Don't do inheritance if the pseudo is also
4644 used in the insn. */
4645 if (r == NULL)
4646 /* We can not do inheritance right now
4647 because the current insn reg info (chain
4648 regs) can change after that. */
4649 add_to_inherit (dst_regno, next_usage_insns);
4651 /* We can not process one reg twice here because of
4652 usage_insns invalidation. */
4653 if ((dst_regno < FIRST_PSEUDO_REGISTER
4654 || reg_renumber[dst_regno] >= 0)
4655 && ! reg->subreg_p && reg->type == OP_OUT)
4657 HARD_REG_SET s;
4659 if (split_if_necessary (dst_regno, reg->biggest_mode,
4660 potential_reload_hard_regs,
4661 false, curr_insn, max_uid))
4662 change_p = true;
4663 CLEAR_HARD_REG_SET (s);
4664 if (dst_regno < FIRST_PSEUDO_REGISTER)
4665 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
4666 else
4667 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
4668 reg_renumber[dst_regno]);
4669 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
4671 /* We should invalidate potential inheritance or
4672 splitting for the current insn usages to the next
4673 usage insns (see code below) as the output pseudo
4674 prevents this. */
4675 if ((dst_regno >= FIRST_PSEUDO_REGISTER
4676 && reg_renumber[dst_regno] < 0)
4677 || (reg->type == OP_OUT && ! reg->subreg_p
4678 && (dst_regno < FIRST_PSEUDO_REGISTER
4679 || reg_renumber[dst_regno] >= 0)))
4681 /* Invalidate. */
4682 if (dst_regno >= FIRST_PSEUDO_REGISTER)
4683 usage_insns[dst_regno].check = 0;
4684 else
4686 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
4687 for (i = 0; i < nregs; i++)
4688 usage_insns[dst_regno + i].check = 0;
4692 if (! JUMP_P (curr_insn))
4693 for (i = 0; i < to_inherit_num; i++)
4694 if (inherit_reload_reg (true, to_inherit[i].regno,
4695 ALL_REGS, curr_insn,
4696 to_inherit[i].insns))
4697 change_p = true;
4698 if (CALL_P (curr_insn))
4700 rtx cheap, pat, dest, restore;
4701 int regno, hard_regno;
4703 calls_num++;
4704 if ((cheap = find_reg_note (curr_insn,
4705 REG_RETURNED, NULL_RTX)) != NULL_RTX
4706 && ((cheap = XEXP (cheap, 0)), true)
4707 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
4708 && (hard_regno = reg_renumber[regno]) >= 0
4709 /* If there are pending saves/restores, the
4710 optimization is not worth. */
4711 && usage_insns[regno].calls_num == calls_num - 1
4712 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
4714 /* Restore the pseudo from the call result as
4715 REG_RETURNED note says that the pseudo value is
4716 in the call result and the pseudo is an argument
4717 of the call. */
4718 pat = PATTERN (curr_insn);
4719 if (GET_CODE (pat) == PARALLEL)
4720 pat = XVECEXP (pat, 0, 0);
4721 dest = SET_DEST (pat);
4722 start_sequence ();
4723 emit_move_insn (cheap, copy_rtx (dest));
4724 restore = get_insns ();
4725 end_sequence ();
4726 lra_process_new_insns (curr_insn, NULL, restore,
4727 "Inserting call parameter restore");
4728 /* We don't need to save/restore of the pseudo from
4729 this call. */
4730 usage_insns[regno].calls_num = calls_num;
4731 bitmap_set_bit (&check_only_regs, regno);
4734 to_inherit_num = 0;
4735 /* Process insn usages. */
4736 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4737 if ((reg->type != OP_OUT
4738 || (reg->type == OP_OUT && reg->subreg_p))
4739 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
4741 if (src_regno >= FIRST_PSEUDO_REGISTER
4742 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
4744 if (usage_insns[src_regno].check == curr_usage_insns_check
4745 && (next_usage_insns
4746 = usage_insns[src_regno].insns) != NULL_RTX
4747 && NONDEBUG_INSN_P (curr_insn))
4748 add_to_inherit (src_regno, next_usage_insns);
4749 else
4750 /* Add usages. */
4751 add_next_usage_insn (src_regno, curr_insn, reloads_num);
4753 else if (src_regno < FIRST_PSEUDO_REGISTER
4754 || reg_renumber[src_regno] >= 0)
4756 bool before_p;
4757 rtx use_insn = curr_insn;
4759 before_p = (JUMP_P (curr_insn)
4760 || (CALL_P (curr_insn) && reg->type == OP_IN));
4761 if (NONDEBUG_INSN_P (curr_insn)
4762 && split_if_necessary (src_regno, reg->biggest_mode,
4763 potential_reload_hard_regs,
4764 before_p, curr_insn, max_uid))
4766 if (reg->subreg_p)
4767 lra_risky_transformations_p = true;
4768 change_p = true;
4769 /* Invalidate. */
4770 usage_insns[src_regno].check = 0;
4771 if (before_p)
4772 use_insn = PREV_INSN (curr_insn);
4774 if (NONDEBUG_INSN_P (curr_insn))
4776 if (src_regno < FIRST_PSEUDO_REGISTER)
4777 add_to_hard_reg_set (&live_hard_regs,
4778 reg->biggest_mode, src_regno);
4779 else
4780 add_to_hard_reg_set (&live_hard_regs,
4781 PSEUDO_REGNO_MODE (src_regno),
4782 reg_renumber[src_regno]);
4784 add_next_usage_insn (src_regno, use_insn, reloads_num);
4787 for (i = 0; i < to_inherit_num; i++)
4789 src_regno = to_inherit[i].regno;
4790 if (inherit_reload_reg (false, src_regno, ALL_REGS,
4791 curr_insn, to_inherit[i].insns))
4792 change_p = true;
4793 else
4794 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
4797 /* We reached the start of the current basic block. */
4798 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
4799 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
4801 /* We reached the beginning of the current block -- do
4802 rest of spliting in the current BB. */
4803 to_process = df_get_live_in (curr_bb);
4804 if (BLOCK_FOR_INSN (head) != curr_bb)
4806 /* We are somewhere in the middle of EBB. */
4807 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
4808 curr_bb, &temp_bitmap);
4809 to_process = &temp_bitmap;
4811 head_p = true;
4812 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
4814 if ((int) j >= lra_constraint_new_regno_start)
4815 break;
4816 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
4817 && usage_insns[j].check == curr_usage_insns_check
4818 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
4820 if (need_for_split_p (potential_reload_hard_regs, j))
4822 if (lra_dump_file != NULL && head_p)
4824 fprintf (lra_dump_file,
4825 " ----------------------------------\n");
4826 head_p = false;
4828 if (split_reg (false, j, bb_note (curr_bb),
4829 next_usage_insns))
4830 change_p = true;
4832 usage_insns[j].check = 0;
4837 return change_p;
4840 /* This value affects EBB forming. If probability of edge from EBB to
4841 a BB is not greater than the following value, we don't add the BB
4842 to EBB. */
4843 #define EBB_PROBABILITY_CUTOFF (REG_BR_PROB_BASE / 2)
4845 /* Current number of inheritance/split iteration. */
4846 int lra_inheritance_iter;
4848 /* Entry function for inheritance/split pass. */
4849 void
4850 lra_inheritance (void)
4852 int i;
4853 basic_block bb, start_bb;
4854 edge e;
4856 lra_inheritance_iter++;
4857 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
4858 return;
4859 timevar_push (TV_LRA_INHERITANCE);
4860 if (lra_dump_file != NULL)
4861 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
4862 lra_inheritance_iter);
4863 curr_usage_insns_check = 0;
4864 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
4865 for (i = 0; i < lra_constraint_new_regno_start; i++)
4866 usage_insns[i].check = 0;
4867 bitmap_initialize (&check_only_regs, &reg_obstack);
4868 bitmap_initialize (&live_regs, &reg_obstack);
4869 bitmap_initialize (&temp_bitmap, &reg_obstack);
4870 bitmap_initialize (&ebb_global_regs, &reg_obstack);
4871 FOR_EACH_BB (bb)
4873 start_bb = bb;
4874 if (lra_dump_file != NULL)
4875 fprintf (lra_dump_file, "EBB");
4876 /* Form a EBB starting with BB. */
4877 bitmap_clear (&ebb_global_regs);
4878 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
4879 for (;;)
4881 if (lra_dump_file != NULL)
4882 fprintf (lra_dump_file, " %d", bb->index);
4883 if (bb->next_bb == EXIT_BLOCK_PTR || LABEL_P (BB_HEAD (bb->next_bb)))
4884 break;
4885 e = find_fallthru_edge (bb->succs);
4886 if (! e)
4887 break;
4888 if (e->probability <= EBB_PROBABILITY_CUTOFF)
4889 break;
4890 bb = bb->next_bb;
4892 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
4893 if (lra_dump_file != NULL)
4894 fprintf (lra_dump_file, "\n");
4895 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
4896 /* Remember that the EBB head and tail can change in
4897 inherit_in_ebb. */
4898 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
4900 bitmap_clear (&ebb_global_regs);
4901 bitmap_clear (&temp_bitmap);
4902 bitmap_clear (&live_regs);
4903 bitmap_clear (&check_only_regs);
4904 free (usage_insns);
4906 timevar_pop (TV_LRA_INHERITANCE);
4911 /* This page contains code to undo failed inheritance/split
4912 transformations. */
4914 /* Current number of iteration undoing inheritance/split. */
4915 int lra_undo_inheritance_iter;
4917 /* Fix BB live info LIVE after removing pseudos created on pass doing
4918 inheritance/split which are REMOVED_PSEUDOS. */
4919 static void
4920 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
4922 unsigned int regno;
4923 bitmap_iterator bi;
4925 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
4926 if (bitmap_clear_bit (live, regno))
4927 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
4930 /* Return regno of the (subreg of) REG. Otherwise, return a negative
4931 number. */
4932 static int
4933 get_regno (rtx reg)
4935 if (GET_CODE (reg) == SUBREG)
4936 reg = SUBREG_REG (reg);
4937 if (REG_P (reg))
4938 return REGNO (reg);
4939 return -1;
4942 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
4943 return true if we did any change. The undo transformations for
4944 inheritance looks like
4945 i <- i2
4946 p <- i => p <- i2
4947 or removing
4948 p <- i, i <- p, and i <- i3
4949 where p is original pseudo from which inheritance pseudo i was
4950 created, i and i3 are removed inheritance pseudos, i2 is another
4951 not removed inheritance pseudo. All split pseudos or other
4952 occurrences of removed inheritance pseudos are changed on the
4953 corresponding original pseudos.
4955 The function also schedules insns changed and created during
4956 inheritance/split pass for processing by the subsequent constraint
4957 pass. */
4958 static bool
4959 remove_inheritance_pseudos (bitmap remove_pseudos)
4961 basic_block bb;
4962 int regno, sregno, prev_sregno, dregno, restore_regno;
4963 rtx set, prev_set, prev_insn;
4964 bool change_p, done_p;
4966 change_p = ! bitmap_empty_p (remove_pseudos);
4967 /* We can not finish the function right away if CHANGE_P is true
4968 because we need to marks insns affected by previous
4969 inheritance/split pass for processing by the subsequent
4970 constraint pass. */
4971 FOR_EACH_BB (bb)
4973 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
4974 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
4975 FOR_BB_INSNS_REVERSE (bb, curr_insn)
4977 if (! INSN_P (curr_insn))
4978 continue;
4979 done_p = false;
4980 sregno = dregno = -1;
4981 if (change_p && NONDEBUG_INSN_P (curr_insn)
4982 && (set = single_set (curr_insn)) != NULL_RTX)
4984 dregno = get_regno (SET_DEST (set));
4985 sregno = get_regno (SET_SRC (set));
4988 if (sregno >= 0 && dregno >= 0)
4990 if ((bitmap_bit_p (remove_pseudos, sregno)
4991 && (lra_reg_info[sregno].restore_regno == dregno
4992 || (bitmap_bit_p (remove_pseudos, dregno)
4993 && (lra_reg_info[sregno].restore_regno
4994 == lra_reg_info[dregno].restore_regno))))
4995 || (bitmap_bit_p (remove_pseudos, dregno)
4996 && lra_reg_info[dregno].restore_regno == sregno))
4997 /* One of the following cases:
4998 original <- removed inheritance pseudo
4999 removed inherit pseudo <- another removed inherit pseudo
5000 removed inherit pseudo <- original pseudo
5002 removed_split_pseudo <- original_reg
5003 original_reg <- removed_split_pseudo */
5005 if (lra_dump_file != NULL)
5007 fprintf (lra_dump_file, " Removing %s:\n",
5008 bitmap_bit_p (&lra_split_regs, sregno)
5009 || bitmap_bit_p (&lra_split_regs, dregno)
5010 ? "split" : "inheritance");
5011 dump_insn_slim (lra_dump_file, curr_insn);
5013 lra_set_insn_deleted (curr_insn);
5014 done_p = true;
5016 else if (bitmap_bit_p (remove_pseudos, sregno)
5017 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5019 /* Search the following pattern:
5020 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5021 original_pseudo <- inherit_or_split_pseudo1
5022 where the 2nd insn is the current insn and
5023 inherit_or_split_pseudo2 is not removed. If it is found,
5024 change the current insn onto:
5025 original_pseudo <- inherit_or_split_pseudo2. */
5026 for (prev_insn = PREV_INSN (curr_insn);
5027 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5028 prev_insn = PREV_INSN (prev_insn))
5030 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5031 && (prev_set = single_set (prev_insn)) != NULL_RTX
5032 /* There should be no subregs in insn we are
5033 searching because only the original reg might
5034 be in subreg when we changed the mode of
5035 load/store for splitting. */
5036 && REG_P (SET_DEST (prev_set))
5037 && REG_P (SET_SRC (prev_set))
5038 && (int) REGNO (SET_DEST (prev_set)) == sregno
5039 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5040 >= FIRST_PSEUDO_REGISTER)
5041 /* As we consider chain of inheritance or
5042 splitting described in above comment we should
5043 check that sregno and prev_sregno were
5044 inheritance/split pseudos created from the
5045 same original regno. */
5046 && (lra_reg_info[sregno].restore_regno
5047 == lra_reg_info[prev_sregno].restore_regno)
5048 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5050 lra_assert (GET_MODE (SET_SRC (prev_set))
5051 == GET_MODE (regno_reg_rtx[sregno]));
5052 if (GET_CODE (SET_SRC (set)) == SUBREG)
5053 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5054 else
5055 SET_SRC (set) = SET_SRC (prev_set);
5056 lra_push_insn_and_update_insn_regno_info (curr_insn);
5057 lra_set_used_insn_alternative_by_uid
5058 (INSN_UID (curr_insn), -1);
5059 done_p = true;
5060 if (lra_dump_file != NULL)
5062 fprintf (lra_dump_file, " Change reload insn:\n");
5063 dump_insn_slim (lra_dump_file, curr_insn);
5068 if (! done_p)
5070 struct lra_insn_reg *reg;
5071 bool restored_regs_p = false;
5072 bool kept_regs_p = false;
5074 curr_id = lra_get_insn_recog_data (curr_insn);
5075 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5077 regno = reg->regno;
5078 restore_regno = lra_reg_info[regno].restore_regno;
5079 if (restore_regno >= 0)
5081 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5083 substitute_pseudo (&curr_insn, regno,
5084 regno_reg_rtx[restore_regno]);
5085 restored_regs_p = true;
5087 else
5088 kept_regs_p = true;
5091 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5093 /* The instruction has changed since the previous
5094 constraints pass. */
5095 lra_push_insn_and_update_insn_regno_info (curr_insn);
5096 lra_set_used_insn_alternative_by_uid
5097 (INSN_UID (curr_insn), -1);
5099 else if (restored_regs_p)
5100 /* The instruction has been restored to the form that
5101 it had during the previous constraints pass. */
5102 lra_update_insn_regno_info (curr_insn);
5103 if (restored_regs_p && lra_dump_file != NULL)
5105 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5106 dump_insn_slim (lra_dump_file, curr_insn);
5111 return change_p;
5114 /* Entry function for undoing inheritance/split transformation. Return true
5115 if we did any RTL change in this pass. */
5116 bool
5117 lra_undo_inheritance (void)
5119 unsigned int regno;
5120 int restore_regno, hard_regno;
5121 int n_all_inherit, n_inherit, n_all_split, n_split;
5122 bitmap_head remove_pseudos;
5123 bitmap_iterator bi;
5124 bool change_p;
5126 lra_undo_inheritance_iter++;
5127 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5128 return false;
5129 if (lra_dump_file != NULL)
5130 fprintf (lra_dump_file,
5131 "\n********** Undoing inheritance #%d: **********\n\n",
5132 lra_undo_inheritance_iter);
5133 bitmap_initialize (&remove_pseudos, &reg_obstack);
5134 n_inherit = n_all_inherit = 0;
5135 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5136 if (lra_reg_info[regno].restore_regno >= 0)
5138 n_all_inherit++;
5139 if (reg_renumber[regno] < 0)
5140 bitmap_set_bit (&remove_pseudos, regno);
5141 else
5142 n_inherit++;
5144 if (lra_dump_file != NULL && n_all_inherit != 0)
5145 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
5146 n_inherit, n_all_inherit,
5147 (double) n_inherit / n_all_inherit * 100);
5148 n_split = n_all_split = 0;
5149 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5150 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
5152 n_all_split++;
5153 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
5154 ? reg_renumber[restore_regno] : restore_regno);
5155 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
5156 bitmap_set_bit (&remove_pseudos, regno);
5157 else
5159 n_split++;
5160 if (lra_dump_file != NULL)
5161 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
5162 regno, restore_regno);
5165 if (lra_dump_file != NULL && n_all_split != 0)
5166 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
5167 n_split, n_all_split,
5168 (double) n_split / n_all_split * 100);
5169 change_p = remove_inheritance_pseudos (&remove_pseudos);
5170 bitmap_clear (&remove_pseudos);
5171 /* Clear restore_regnos. */
5172 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5173 lra_reg_info[regno].restore_regno = -1;
5174 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5175 lra_reg_info[regno].restore_regno = -1;
5176 return change_p;