2013-04-19 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / lra-constraints.c
blobe3b4add9f18bf51023b19a5ad263d389e89c3c2c
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 /* Number of small register classes used for operands of the best
1058 alternative. */
1059 static int best_small_class_operands_num;
1060 /* Overall number hard registers used for reloads. For example, on
1061 some targets we need 2 general registers to reload DFmode and only
1062 one floating point register. */
1063 static int best_reload_nregs;
1064 /* Overall number reflecting distances of previous reloading the same
1065 value. The distances are counted from the current BB start. It is
1066 used to improve inheritance chances. */
1067 static int best_reload_sum;
1069 /* True if the current insn should have no correspondingly input or
1070 output reloads. */
1071 static bool no_input_reloads_p, no_output_reloads_p;
1073 /* True if we swapped the commutative operands in the current
1074 insn. */
1075 static int curr_swapped;
1077 /* Arrange for address element *LOC to be a register of class CL.
1078 Add any input reloads to list BEFORE. AFTER is nonnull if *LOC is an
1079 automodified value; handle that case by adding the required output
1080 reloads to list AFTER. Return true if the RTL was changed. */
1081 static bool
1082 process_addr_reg (rtx *loc, rtx *before, rtx *after, enum reg_class cl)
1084 int regno;
1085 enum reg_class rclass, new_class;
1086 rtx reg;
1087 rtx new_reg;
1088 enum machine_mode mode;
1089 bool before_p = false;
1091 loc = strip_subreg (loc);
1092 reg = *loc;
1093 mode = GET_MODE (reg);
1094 if (! REG_P (reg))
1096 /* Always reload memory in an address even if the target supports
1097 such addresses. */
1098 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1099 before_p = true;
1101 else
1103 regno = REGNO (reg);
1104 rclass = get_reg_class (regno);
1105 if ((*loc = get_equiv_substitution (reg)) != reg)
1107 if (lra_dump_file != NULL)
1109 fprintf (lra_dump_file,
1110 "Changing pseudo %d in address of insn %u on equiv ",
1111 REGNO (reg), INSN_UID (curr_insn));
1112 dump_value_slim (lra_dump_file, *loc, 1);
1113 fprintf (lra_dump_file, "\n");
1115 *loc = copy_rtx (*loc);
1117 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1119 reg = *loc;
1120 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1121 mode, reg, cl, "address", &new_reg))
1122 before_p = true;
1124 else if (new_class != NO_REGS && rclass != new_class)
1126 change_class (regno, new_class, " Change", true);
1127 return false;
1129 else
1130 return false;
1132 if (before_p)
1134 push_to_sequence (*before);
1135 lra_emit_move (new_reg, reg);
1136 *before = get_insns ();
1137 end_sequence ();
1139 *loc = new_reg;
1140 if (after != NULL)
1142 start_sequence ();
1143 lra_emit_move (reg, new_reg);
1144 emit_insn (*after);
1145 *after = get_insns ();
1146 end_sequence ();
1148 return true;
1151 /* Make reloads for subreg in operand NOP with internal subreg mode
1152 REG_MODE, add new reloads for further processing. Return true if
1153 any reload was generated. */
1154 static bool
1155 simplify_operand_subreg (int nop, enum machine_mode reg_mode)
1157 int hard_regno;
1158 rtx before, after;
1159 enum machine_mode mode;
1160 rtx reg, new_reg;
1161 rtx operand = *curr_id->operand_loc[nop];
1163 before = after = NULL_RTX;
1165 if (GET_CODE (operand) != SUBREG)
1166 return false;
1168 mode = GET_MODE (operand);
1169 reg = SUBREG_REG (operand);
1170 /* If we change address for paradoxical subreg of memory, the
1171 address might violate the necessary alignment or the access might
1172 be slow. So take this into consideration. We should not worry
1173 about access beyond allocated memory for paradoxical memory
1174 subregs as we don't substitute such equiv memory (see processing
1175 equivalences in function lra_constraints) and because for spilled
1176 pseudos we allocate stack memory enough for the biggest
1177 corresponding paradoxical subreg. */
1178 if ((MEM_P (reg)
1179 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1180 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1181 || (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER))
1183 alter_subreg (curr_id->operand_loc[nop], false);
1184 return true;
1186 /* Put constant into memory when we have mixed modes. It generates
1187 a better code in most cases as it does not need a secondary
1188 reload memory. It also prevents LRA looping when LRA is using
1189 secondary reload memory again and again. */
1190 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1191 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1193 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1194 alter_subreg (curr_id->operand_loc[nop], false);
1195 return true;
1197 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1198 if there may be a problem accessing OPERAND in the outer
1199 mode. */
1200 if ((REG_P (reg)
1201 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1202 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1203 /* Don't reload paradoxical subregs because we could be looping
1204 having repeatedly final regno out of hard regs range. */
1205 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1206 >= hard_regno_nregs[hard_regno][mode])
1207 && simplify_subreg_regno (hard_regno, GET_MODE (reg),
1208 SUBREG_BYTE (operand), mode) < 0)
1209 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1211 enum op_type type = curr_static_id->operand[nop].type;
1212 /* The class will be defined later in curr_insn_transform. */
1213 enum reg_class rclass
1214 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1216 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1217 rclass, "subreg reg", &new_reg))
1219 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (new_reg));
1220 if (type != OP_OUT
1221 || GET_MODE_SIZE (GET_MODE (reg)) > GET_MODE_SIZE (mode))
1223 push_to_sequence (before);
1224 lra_emit_move (new_reg, reg);
1225 before = get_insns ();
1226 end_sequence ();
1228 if (type != OP_IN)
1230 start_sequence ();
1231 lra_emit_move (reg, new_reg);
1232 emit_insn (after);
1233 after = get_insns ();
1234 end_sequence ();
1237 SUBREG_REG (operand) = new_reg;
1238 lra_process_new_insns (curr_insn, before, after,
1239 "Inserting subreg reload");
1240 return true;
1242 return false;
1245 /* Return TRUE if X refers for a hard register from SET. */
1246 static bool
1247 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1249 int i, j, x_hard_regno;
1250 enum machine_mode mode;
1251 const char *fmt;
1252 enum rtx_code code;
1254 if (x == NULL_RTX)
1255 return false;
1256 code = GET_CODE (x);
1257 mode = GET_MODE (x);
1258 if (code == SUBREG)
1260 x = SUBREG_REG (x);
1261 code = GET_CODE (x);
1262 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1263 mode = GET_MODE (x);
1266 if (REG_P (x))
1268 x_hard_regno = get_hard_regno (x);
1269 return (x_hard_regno >= 0
1270 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1272 if (MEM_P (x))
1274 struct address_info ad;
1276 decompose_mem_address (&ad, x);
1277 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1278 return true;
1279 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1280 return true;
1282 fmt = GET_RTX_FORMAT (code);
1283 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1285 if (fmt[i] == 'e')
1287 if (uses_hard_regs_p (XEXP (x, i), set))
1288 return true;
1290 else if (fmt[i] == 'E')
1292 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1293 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1294 return true;
1297 return false;
1300 /* Return true if OP is a spilled pseudo. */
1301 static inline bool
1302 spilled_pseudo_p (rtx op)
1304 return (REG_P (op)
1305 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1308 /* Return true if X is a general constant. */
1309 static inline bool
1310 general_constant_p (rtx x)
1312 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1315 /* Major function to choose the current insn alternative and what
1316 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1317 negative we should consider only this alternative. Return false if
1318 we can not choose the alternative or find how to reload the
1319 operands. */
1320 static bool
1321 process_alt_operands (int only_alternative)
1323 bool ok_p = false;
1324 int nop, small_class_operands_num, overall, nalt;
1325 int n_alternatives = curr_static_id->n_alternatives;
1326 int n_operands = curr_static_id->n_operands;
1327 /* LOSERS counts the operands that don't fit this alternative and
1328 would require loading. */
1329 int losers;
1330 /* REJECT is a count of how undesirable this alternative says it is
1331 if any reloading is required. If the alternative matches exactly
1332 then REJECT is ignored, but otherwise it gets this much counted
1333 against it in addition to the reloading needed. */
1334 int reject;
1335 /* The number of elements in the following array. */
1336 int early_clobbered_regs_num;
1337 /* Numbers of operands which are early clobber registers. */
1338 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1339 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1340 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1341 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1342 bool curr_alt_win[MAX_RECOG_OPERANDS];
1343 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1344 int curr_alt_matches[MAX_RECOG_OPERANDS];
1345 /* The number of elements in the following array. */
1346 int curr_alt_dont_inherit_ops_num;
1347 /* Numbers of operands whose reload pseudos should not be inherited. */
1348 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1349 rtx op;
1350 /* The register when the operand is a subreg of register, otherwise the
1351 operand itself. */
1352 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1353 /* The register if the operand is a register or subreg of register,
1354 otherwise NULL. */
1355 rtx operand_reg[MAX_RECOG_OPERANDS];
1356 int hard_regno[MAX_RECOG_OPERANDS];
1357 enum machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1358 int reload_nregs, reload_sum;
1359 bool costly_p;
1360 enum reg_class cl;
1362 /* Calculate some data common for all alternatives to speed up the
1363 function. */
1364 for (nop = 0; nop < n_operands; nop++)
1366 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1367 /* The real hard regno of the operand after the allocation. */
1368 hard_regno[nop] = get_hard_regno (op);
1370 operand_reg[nop] = op;
1371 biggest_mode[nop] = GET_MODE (operand_reg[nop]);
1372 if (GET_CODE (operand_reg[nop]) == SUBREG)
1374 operand_reg[nop] = SUBREG_REG (operand_reg[nop]);
1375 if (GET_MODE_SIZE (biggest_mode[nop])
1376 < GET_MODE_SIZE (GET_MODE (operand_reg[nop])))
1377 biggest_mode[nop] = GET_MODE (operand_reg[nop]);
1379 if (REG_P (operand_reg[nop]))
1380 no_subreg_reg_operand[nop] = operand_reg[nop];
1381 else
1382 operand_reg[nop] = NULL_RTX;
1385 /* The constraints are made of several alternatives. Each operand's
1386 constraint looks like foo,bar,... with commas separating the
1387 alternatives. The first alternatives for all operands go
1388 together, the second alternatives go together, etc.
1390 First loop over alternatives. */
1391 for (nalt = 0; nalt < n_alternatives; nalt++)
1393 /* Loop over operands for one constraint alternative. */
1394 #ifdef HAVE_ATTR_enabled
1395 if (curr_id->alternative_enabled_p != NULL
1396 && ! curr_id->alternative_enabled_p[nalt])
1397 continue;
1398 #endif
1400 if (only_alternative >= 0 && nalt != only_alternative)
1401 continue;
1403 overall = losers = reject = reload_nregs = reload_sum = 0;
1404 for (nop = 0; nop < n_operands; nop++)
1405 reject += (curr_static_id
1406 ->operand_alternative[nalt * n_operands + nop].reject);
1407 early_clobbered_regs_num = 0;
1409 for (nop = 0; nop < n_operands; nop++)
1411 const char *p;
1412 char *end;
1413 int len, c, m, i, opalt_num, this_alternative_matches;
1414 bool win, did_match, offmemok, early_clobber_p;
1415 /* false => this operand can be reloaded somehow for this
1416 alternative. */
1417 bool badop;
1418 /* true => this operand can be reloaded if the alternative
1419 allows regs. */
1420 bool winreg;
1421 /* True if a constant forced into memory would be OK for
1422 this operand. */
1423 bool constmemok;
1424 enum reg_class this_alternative, this_costly_alternative;
1425 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1426 bool this_alternative_match_win, this_alternative_win;
1427 bool this_alternative_offmemok;
1428 enum machine_mode mode;
1430 opalt_num = nalt * n_operands + nop;
1431 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1433 /* Fast track for no constraints at all. */
1434 curr_alt[nop] = NO_REGS;
1435 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1436 curr_alt_win[nop] = true;
1437 curr_alt_match_win[nop] = false;
1438 curr_alt_offmemok[nop] = false;
1439 curr_alt_matches[nop] = -1;
1440 continue;
1443 op = no_subreg_reg_operand[nop];
1444 mode = curr_operand_mode[nop];
1446 win = did_match = winreg = offmemok = constmemok = false;
1447 badop = true;
1449 early_clobber_p = false;
1450 p = curr_static_id->operand_alternative[opalt_num].constraint;
1452 this_costly_alternative = this_alternative = NO_REGS;
1453 /* We update set of possible hard regs besides its class
1454 because reg class might be inaccurate. For example,
1455 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1456 is translated in HI_REGS because classes are merged by
1457 pairs and there is no accurate intermediate class. */
1458 CLEAR_HARD_REG_SET (this_alternative_set);
1459 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1460 this_alternative_win = false;
1461 this_alternative_match_win = false;
1462 this_alternative_offmemok = false;
1463 this_alternative_matches = -1;
1465 /* An empty constraint should be excluded by the fast
1466 track. */
1467 lra_assert (*p != 0 && *p != ',');
1469 /* Scan this alternative's specs for this operand; set WIN
1470 if the operand fits any letter in this alternative.
1471 Otherwise, clear BADOP if this operand could fit some
1472 letter after reloads, or set WINREG if this operand could
1473 fit after reloads provided the constraint allows some
1474 registers. */
1475 costly_p = false;
1478 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1480 case '\0':
1481 len = 0;
1482 break;
1483 case ',':
1484 c = '\0';
1485 break;
1487 case '=': case '+': case '?': case '*': case '!':
1488 case ' ': case '\t':
1489 break;
1491 case '%':
1492 /* We only support one commutative marker, the first
1493 one. We already set commutative above. */
1494 break;
1496 case '&':
1497 early_clobber_p = true;
1498 break;
1500 case '#':
1501 /* Ignore rest of this alternative. */
1502 c = '\0';
1503 break;
1505 case '0': case '1': case '2': case '3': case '4':
1506 case '5': case '6': case '7': case '8': case '9':
1508 int m_hregno;
1509 bool match_p;
1511 m = strtoul (p, &end, 10);
1512 p = end;
1513 len = 0;
1514 lra_assert (nop > m);
1516 this_alternative_matches = m;
1517 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1518 /* We are supposed to match a previous operand.
1519 If we do, we win if that one did. If we do
1520 not, count both of the operands as losers.
1521 (This is too conservative, since most of the
1522 time only a single reload insn will be needed
1523 to make the two operands win. As a result,
1524 this alternative may be rejected when it is
1525 actually desirable.) */
1526 match_p = false;
1527 if (operands_match_p (*curr_id->operand_loc[nop],
1528 *curr_id->operand_loc[m], m_hregno))
1530 /* We should reject matching of an early
1531 clobber operand if the matching operand is
1532 not dying in the insn. */
1533 if (! curr_static_id->operand[m].early_clobber
1534 || operand_reg[nop] == NULL_RTX
1535 || (find_regno_note (curr_insn, REG_DEAD,
1536 REGNO (op))
1537 || REGNO (op) == REGNO (operand_reg[m])))
1538 match_p = true;
1540 if (match_p)
1542 /* If we are matching a non-offsettable
1543 address where an offsettable address was
1544 expected, then we must reject this
1545 combination, because we can't reload
1546 it. */
1547 if (curr_alt_offmemok[m]
1548 && MEM_P (*curr_id->operand_loc[m])
1549 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1550 continue;
1553 else
1555 /* Operands don't match. Both operands must
1556 allow a reload register, otherwise we
1557 cannot make them match. */
1558 if (curr_alt[m] == NO_REGS)
1559 break;
1560 /* Retroactively mark the operand we had to
1561 match as a loser, if it wasn't already and
1562 it wasn't matched to a register constraint
1563 (e.g it might be matched by memory). */
1564 if (curr_alt_win[m]
1565 && (operand_reg[m] == NULL_RTX
1566 || hard_regno[m] < 0))
1568 losers++;
1569 reload_nregs
1570 += (ira_reg_class_max_nregs[curr_alt[m]]
1571 [GET_MODE (*curr_id->operand_loc[m])]);
1574 /* We prefer no matching alternatives because
1575 it gives more freedom in RA. */
1576 if (operand_reg[nop] == NULL_RTX
1577 || (find_regno_note (curr_insn, REG_DEAD,
1578 REGNO (operand_reg[nop]))
1579 == NULL_RTX))
1580 reject += 2;
1582 /* If we have to reload this operand and some
1583 previous operand also had to match the same
1584 thing as this operand, we don't know how to do
1585 that. */
1586 if (!match_p || !curr_alt_win[m])
1588 for (i = 0; i < nop; i++)
1589 if (curr_alt_matches[i] == m)
1590 break;
1591 if (i < nop)
1592 break;
1594 else
1595 did_match = true;
1597 /* This can be fixed with reloads if the operand
1598 we are supposed to match can be fixed with
1599 reloads. */
1600 badop = false;
1601 this_alternative = curr_alt[m];
1602 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1603 winreg = this_alternative != NO_REGS;
1604 break;
1607 case 'p':
1608 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1609 ADDRESS, SCRATCH);
1610 this_alternative = reg_class_subunion[this_alternative][cl];
1611 IOR_HARD_REG_SET (this_alternative_set,
1612 reg_class_contents[cl]);
1613 if (costly_p)
1615 this_costly_alternative
1616 = reg_class_subunion[this_costly_alternative][cl];
1617 IOR_HARD_REG_SET (this_costly_alternative_set,
1618 reg_class_contents[cl]);
1620 win = true;
1621 badop = false;
1622 break;
1624 case TARGET_MEM_CONSTRAINT:
1625 if (MEM_P (op) || spilled_pseudo_p (op))
1626 win = true;
1627 /* We can put constant or pseudo value into memory
1628 to satisfy the constraint. */
1629 if (CONST_POOL_OK_P (mode, op) || REG_P (op))
1630 badop = false;
1631 constmemok = true;
1632 break;
1634 case '<':
1635 if (MEM_P (op)
1636 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1637 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1638 win = true;
1639 break;
1641 case '>':
1642 if (MEM_P (op)
1643 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1644 || GET_CODE (XEXP (op, 0)) == POST_INC))
1645 win = true;
1646 break;
1648 /* Memory op whose address is not offsettable. */
1649 case 'V':
1650 if (MEM_P (op)
1651 && ! offsettable_nonstrict_memref_p (op))
1652 win = true;
1653 break;
1655 /* Memory operand whose address is offsettable. */
1656 case 'o':
1657 if ((MEM_P (op)
1658 && offsettable_nonstrict_memref_p (op))
1659 || spilled_pseudo_p (op))
1660 win = true;
1661 /* We can put constant or pseudo value into memory
1662 or make memory address offsetable to satisfy the
1663 constraint. */
1664 if (CONST_POOL_OK_P (mode, op) || MEM_P (op) || REG_P (op))
1665 badop = false;
1666 constmemok = true;
1667 offmemok = true;
1668 break;
1670 case 'E':
1671 case 'F':
1672 if (GET_CODE (op) == CONST_DOUBLE
1673 || (GET_CODE (op) == CONST_VECTOR
1674 && (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)))
1675 win = true;
1676 break;
1678 case 'G':
1679 case 'H':
1680 if (GET_CODE (op) == CONST_DOUBLE
1681 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1682 win = true;
1683 break;
1685 case 's':
1686 if (CONST_INT_P (op)
1687 || (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode))
1688 break;
1690 case 'i':
1691 if (general_constant_p (op))
1692 win = true;
1693 break;
1695 case 'n':
1696 if (CONST_INT_P (op)
1697 || (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode))
1698 win = true;
1699 break;
1701 case 'I':
1702 case 'J':
1703 case 'K':
1704 case 'L':
1705 case 'M':
1706 case 'N':
1707 case 'O':
1708 case 'P':
1709 if (CONST_INT_P (op)
1710 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1711 win = true;
1712 break;
1714 case 'X':
1715 /* This constraint should be excluded by the fast
1716 track. */
1717 gcc_unreachable ();
1718 break;
1720 case 'g':
1721 if (MEM_P (op)
1722 || general_constant_p (op)
1723 || spilled_pseudo_p (op))
1724 win = true;
1725 /* Drop through into 'r' case. */
1727 case 'r':
1728 this_alternative
1729 = reg_class_subunion[this_alternative][GENERAL_REGS];
1730 IOR_HARD_REG_SET (this_alternative_set,
1731 reg_class_contents[GENERAL_REGS]);
1732 if (costly_p)
1734 this_costly_alternative
1735 = (reg_class_subunion
1736 [this_costly_alternative][GENERAL_REGS]);
1737 IOR_HARD_REG_SET (this_costly_alternative_set,
1738 reg_class_contents[GENERAL_REGS]);
1740 goto reg;
1742 default:
1743 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
1745 #ifdef EXTRA_CONSTRAINT_STR
1746 if (EXTRA_MEMORY_CONSTRAINT (c, p))
1748 if (EXTRA_CONSTRAINT_STR (op, c, p))
1749 win = true;
1750 else if (spilled_pseudo_p (op))
1751 win = true;
1753 /* If we didn't already win, we can reload
1754 constants via force_const_mem or put the
1755 pseudo value into memory, or make other
1756 memory by reloading the address like for
1757 'o'. */
1758 if (CONST_POOL_OK_P (mode, op)
1759 || MEM_P (op) || REG_P (op))
1760 badop = false;
1761 constmemok = true;
1762 offmemok = true;
1763 break;
1765 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1767 if (EXTRA_CONSTRAINT_STR (op, c, p))
1768 win = true;
1770 /* If we didn't already win, we can reload
1771 the address into a base register. */
1772 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1773 ADDRESS, SCRATCH);
1774 this_alternative
1775 = reg_class_subunion[this_alternative][cl];
1776 IOR_HARD_REG_SET (this_alternative_set,
1777 reg_class_contents[cl]);
1778 if (costly_p)
1780 this_costly_alternative
1781 = (reg_class_subunion
1782 [this_costly_alternative][cl]);
1783 IOR_HARD_REG_SET (this_costly_alternative_set,
1784 reg_class_contents[cl]);
1786 badop = false;
1787 break;
1790 if (EXTRA_CONSTRAINT_STR (op, c, p))
1791 win = true;
1792 #endif
1793 break;
1796 cl = REG_CLASS_FROM_CONSTRAINT (c, p);
1797 this_alternative = reg_class_subunion[this_alternative][cl];
1798 IOR_HARD_REG_SET (this_alternative_set,
1799 reg_class_contents[cl]);
1800 if (costly_p)
1802 this_costly_alternative
1803 = reg_class_subunion[this_costly_alternative][cl];
1804 IOR_HARD_REG_SET (this_costly_alternative_set,
1805 reg_class_contents[cl]);
1807 reg:
1808 if (mode == BLKmode)
1809 break;
1810 winreg = true;
1811 if (REG_P (op))
1813 if (hard_regno[nop] >= 0
1814 && in_hard_reg_set_p (this_alternative_set,
1815 mode, hard_regno[nop]))
1816 win = true;
1817 else if (hard_regno[nop] < 0
1818 && in_class_p (op, this_alternative, NULL))
1819 win = true;
1821 break;
1823 if (c != ' ' && c != '\t')
1824 costly_p = c == '*';
1826 while ((p += len), c);
1828 /* Record which operands fit this alternative. */
1829 if (win)
1831 this_alternative_win = true;
1832 if (operand_reg[nop] != NULL_RTX)
1834 if (hard_regno[nop] >= 0)
1836 if (in_hard_reg_set_p (this_costly_alternative_set,
1837 mode, hard_regno[nop]))
1838 reject++;
1840 else
1842 /* Prefer won reg to spilled pseudo under other equal
1843 conditions. */
1844 reject++;
1845 if (in_class_p (operand_reg[nop],
1846 this_costly_alternative, NULL))
1847 reject++;
1849 /* We simulate the behaviour of old reload here.
1850 Although scratches need hard registers and it
1851 might result in spilling other pseudos, no reload
1852 insns are generated for the scratches. So it
1853 might cost something but probably less than old
1854 reload pass believes. */
1855 if (lra_former_scratch_p (REGNO (operand_reg[nop])))
1856 reject += LRA_LOSER_COST_FACTOR;
1859 else if (did_match)
1860 this_alternative_match_win = true;
1861 else
1863 int const_to_mem = 0;
1864 bool no_regs_p;
1866 /* If this alternative asks for a specific reg class, see if there
1867 is at least one allocatable register in that class. */
1868 no_regs_p
1869 = (this_alternative == NO_REGS
1870 || (hard_reg_set_subset_p
1871 (reg_class_contents[this_alternative],
1872 lra_no_alloc_regs)));
1874 /* For asms, verify that the class for this alternative is possible
1875 for the mode that is specified. */
1876 if (!no_regs_p && REG_P (op) && INSN_CODE (curr_insn) < 0)
1878 int i;
1879 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1880 if (HARD_REGNO_MODE_OK (i, mode)
1881 && in_hard_reg_set_p (reg_class_contents[this_alternative], mode, i))
1882 break;
1883 if (i == FIRST_PSEUDO_REGISTER)
1884 winreg = false;
1887 /* If this operand accepts a register, and if the
1888 register class has at least one allocatable register,
1889 then this operand can be reloaded. */
1890 if (winreg && !no_regs_p)
1891 badop = false;
1893 if (badop)
1894 goto fail;
1896 this_alternative_offmemok = offmemok;
1897 if (this_costly_alternative != NO_REGS)
1898 reject++;
1899 /* If the operand is dying, has a matching constraint,
1900 and satisfies constraints of the matched operand
1901 which failed to satisfy the own constraints, we do
1902 not need to generate a reload insn for this
1903 operand. */
1904 if (!(this_alternative_matches >= 0
1905 && !curr_alt_win[this_alternative_matches]
1906 && REG_P (op)
1907 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
1908 && (hard_regno[nop] >= 0
1909 ? in_hard_reg_set_p (this_alternative_set,
1910 mode, hard_regno[nop])
1911 : in_class_p (op, this_alternative, NULL))))
1913 /* Strict_low_part requires to reload the register
1914 not the sub-register. In this case we should
1915 check that a final reload hard reg can hold the
1916 value mode. */
1917 if (curr_static_id->operand[nop].strict_low
1918 && REG_P (op)
1919 && hard_regno[nop] < 0
1920 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
1921 && ira_class_hard_regs_num[this_alternative] > 0
1922 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
1923 [this_alternative][0],
1924 GET_MODE (op)))
1925 goto fail;
1926 losers++;
1928 if (operand_reg[nop] != NULL_RTX
1929 /* Output operands and matched input operands are
1930 not inherited. The following conditions do not
1931 exactly describe the previous statement but they
1932 are pretty close. */
1933 && curr_static_id->operand[nop].type != OP_OUT
1934 && (this_alternative_matches < 0
1935 || curr_static_id->operand[nop].type != OP_IN))
1937 int last_reload = (lra_reg_info[ORIGINAL_REGNO
1938 (operand_reg[nop])]
1939 .last_reload);
1941 if (last_reload > bb_reload_num)
1942 reload_sum += last_reload - bb_reload_num;
1944 /* If this is a constant that is reloaded into the
1945 desired class by copying it to memory first, count
1946 that as another reload. This is consistent with
1947 other code and is required to avoid choosing another
1948 alternative when the constant is moved into memory.
1949 Note that the test here is precisely the same as in
1950 the code below that calls force_const_mem. */
1951 if (CONST_POOL_OK_P (mode, op)
1952 && ((targetm.preferred_reload_class
1953 (op, this_alternative) == NO_REGS)
1954 || no_input_reloads_p))
1956 const_to_mem = 1;
1957 if (! no_regs_p)
1958 losers++;
1961 /* Alternative loses if it requires a type of reload not
1962 permitted for this insn. We can always reload
1963 objects with a REG_UNUSED note. */
1964 if ((curr_static_id->operand[nop].type != OP_IN
1965 && no_output_reloads_p
1966 && ! find_reg_note (curr_insn, REG_UNUSED, op))
1967 || (curr_static_id->operand[nop].type != OP_OUT
1968 && no_input_reloads_p && ! const_to_mem))
1969 goto fail;
1971 /* Check strong discouragement of reload of non-constant
1972 into class THIS_ALTERNATIVE. */
1973 if (! CONSTANT_P (op) && ! no_regs_p
1974 && (targetm.preferred_reload_class
1975 (op, this_alternative) == NO_REGS
1976 || (curr_static_id->operand[nop].type == OP_OUT
1977 && (targetm.preferred_output_reload_class
1978 (op, this_alternative) == NO_REGS))))
1979 reject += LRA_MAX_REJECT;
1981 if (! ((const_to_mem && constmemok)
1982 || (MEM_P (op) && offmemok)))
1984 /* We prefer to reload pseudos over reloading other
1985 things, since such reloads may be able to be
1986 eliminated later. So bump REJECT in other cases.
1987 Don't do this in the case where we are forcing a
1988 constant into memory and it will then win since
1989 we don't want to have a different alternative
1990 match then. */
1991 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
1992 reject += 2;
1994 if (! no_regs_p)
1995 reload_nregs
1996 += ira_reg_class_max_nregs[this_alternative][mode];
1999 /* We are trying to spill pseudo into memory. It is
2000 usually more costly than moving to a hard register
2001 although it might takes the same number of
2002 reloads. */
2003 if (no_regs_p && REG_P (op))
2004 reject++;
2006 #ifdef SECONDARY_MEMORY_NEEDED
2007 /* If reload requires moving value through secondary
2008 memory, it will need one more insn at least. */
2009 if (this_alternative != NO_REGS
2010 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2011 && ((curr_static_id->operand[nop].type != OP_OUT
2012 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2013 GET_MODE (op)))
2014 || (curr_static_id->operand[nop].type != OP_IN
2015 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2016 GET_MODE (op)))))
2017 losers++;
2018 #endif
2019 /* Input reloads can be inherited more often than output
2020 reloads can be removed, so penalize output
2021 reloads. */
2022 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2023 reject++;
2026 if (early_clobber_p)
2027 reject++;
2028 /* ??? We check early clobbers after processing all operands
2029 (see loop below) and there we update the costs more.
2030 Should we update the cost (may be approximately) here
2031 because of early clobber register reloads or it is a rare
2032 or non-important thing to be worth to do it. */
2033 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2034 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2035 goto fail;
2037 curr_alt[nop] = this_alternative;
2038 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2039 curr_alt_win[nop] = this_alternative_win;
2040 curr_alt_match_win[nop] = this_alternative_match_win;
2041 curr_alt_offmemok[nop] = this_alternative_offmemok;
2042 curr_alt_matches[nop] = this_alternative_matches;
2044 if (this_alternative_matches >= 0
2045 && !did_match && !this_alternative_win)
2046 curr_alt_win[this_alternative_matches] = false;
2048 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2049 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2051 ok_p = true;
2052 curr_alt_dont_inherit_ops_num = 0;
2053 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2055 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2056 HARD_REG_SET temp_set;
2058 i = early_clobbered_nops[nop];
2059 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2060 || hard_regno[i] < 0)
2061 continue;
2062 lra_assert (operand_reg[i] != NULL_RTX);
2063 clobbered_hard_regno = hard_regno[i];
2064 CLEAR_HARD_REG_SET (temp_set);
2065 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2066 first_conflict_j = last_conflict_j = -1;
2067 for (j = 0; j < n_operands; j++)
2068 if (j == i
2069 /* We don't want process insides of match_operator and
2070 match_parallel because otherwise we would process
2071 their operands once again generating a wrong
2072 code. */
2073 || curr_static_id->operand[j].is_operator)
2074 continue;
2075 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2076 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2077 continue;
2078 /* If we don't reload j-th operand, check conflicts. */
2079 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2080 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2082 if (first_conflict_j < 0)
2083 first_conflict_j = j;
2084 last_conflict_j = j;
2086 if (last_conflict_j < 0)
2087 continue;
2088 /* If earlyclobber operand conflicts with another
2089 non-matching operand which is actually the same register
2090 as the earlyclobber operand, it is better to reload the
2091 another operand as an operand matching the earlyclobber
2092 operand can be also the same. */
2093 if (first_conflict_j == last_conflict_j
2094 && operand_reg[last_conflict_j]
2095 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2096 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2098 curr_alt_win[last_conflict_j] = false;
2099 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2100 = last_conflict_j;
2101 losers++;
2102 overall += LRA_LOSER_COST_FACTOR;
2104 else
2106 /* We need to reload early clobbered register and the
2107 matched registers. */
2108 for (j = 0; j < n_operands; j++)
2109 if (curr_alt_matches[j] == i)
2111 curr_alt_match_win[j] = false;
2112 losers++;
2113 overall += LRA_LOSER_COST_FACTOR;
2115 if (! curr_alt_match_win[i])
2116 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2117 else
2119 /* Remember pseudos used for match reloads are never
2120 inherited. */
2121 lra_assert (curr_alt_matches[i] >= 0);
2122 curr_alt_win[curr_alt_matches[i]] = false;
2124 curr_alt_win[i] = curr_alt_match_win[i] = false;
2125 losers++;
2126 overall += LRA_LOSER_COST_FACTOR;
2129 small_class_operands_num = 0;
2130 for (nop = 0; nop < n_operands; nop++)
2131 small_class_operands_num
2132 += SMALL_REGISTER_CLASS_P (curr_alt[nop]) ? 1 : 0;
2134 /* If this alternative can be made to work by reloading, and it
2135 needs less reloading than the others checked so far, record
2136 it as the chosen goal for reloading. */
2137 if ((best_losers != 0 && losers == 0)
2138 || (((best_losers == 0 && losers == 0)
2139 || (best_losers != 0 && losers != 0))
2140 && (best_overall > overall
2141 || (best_overall == overall
2142 /* If the cost of the reloads is the same,
2143 prefer alternative which requires minimal
2144 number of small register classes for the
2145 operands. This improves chances of reloads
2146 for insn requiring small register
2147 classes. */
2148 && (small_class_operands_num
2149 < best_small_class_operands_num
2150 || (small_class_operands_num
2151 == best_small_class_operands_num
2152 && (reload_nregs < best_reload_nregs
2153 || (reload_nregs == best_reload_nregs
2154 && best_reload_sum < reload_sum))))))))
2156 for (nop = 0; nop < n_operands; nop++)
2158 goal_alt_win[nop] = curr_alt_win[nop];
2159 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2160 goal_alt_matches[nop] = curr_alt_matches[nop];
2161 goal_alt[nop] = curr_alt[nop];
2162 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2164 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2165 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2166 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2167 goal_alt_swapped = curr_swapped;
2168 best_overall = overall;
2169 best_losers = losers;
2170 best_small_class_operands_num = small_class_operands_num;
2171 best_reload_nregs = reload_nregs;
2172 best_reload_sum = reload_sum;
2173 goal_alt_number = nalt;
2175 if (losers == 0)
2176 /* Everything is satisfied. Do not process alternatives
2177 anymore. */
2178 break;
2179 fail:
2182 return ok_p;
2185 /* Return 1 if ADDR is a valid memory address for mode MODE in address
2186 space AS, and check that each pseudo has the proper kind of hard
2187 reg. */
2188 static int
2189 valid_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2190 rtx addr, addr_space_t as)
2192 #ifdef GO_IF_LEGITIMATE_ADDRESS
2193 lra_assert (ADDR_SPACE_GENERIC_P (as));
2194 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
2195 return 0;
2197 win:
2198 return 1;
2199 #else
2200 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
2201 #endif
2204 /* Return whether address AD is valid. */
2206 static bool
2207 valid_address_p (struct address_info *ad)
2209 /* Some ports do not check displacements for eliminable registers,
2210 so we replace them temporarily with the elimination target. */
2211 rtx saved_base_reg = NULL_RTX;
2212 rtx saved_index_reg = NULL_RTX;
2213 rtx *base_term = strip_subreg (ad->base_term);
2214 rtx *index_term = strip_subreg (ad->index_term);
2215 if (base_term != NULL)
2217 saved_base_reg = *base_term;
2218 lra_eliminate_reg_if_possible (base_term);
2219 if (ad->base_term2 != NULL)
2220 *ad->base_term2 = *ad->base_term;
2222 if (index_term != NULL)
2224 saved_index_reg = *index_term;
2225 lra_eliminate_reg_if_possible (index_term);
2227 bool ok_p = valid_address_p (ad->mode, *ad->outer, ad->as);
2228 if (saved_base_reg != NULL_RTX)
2230 *base_term = saved_base_reg;
2231 if (ad->base_term2 != NULL)
2232 *ad->base_term2 = *ad->base_term;
2234 if (saved_index_reg != NULL_RTX)
2235 *index_term = saved_index_reg;
2236 return ok_p;
2239 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2240 static rtx
2241 base_plus_disp_to_reg (struct address_info *ad)
2243 enum reg_class cl;
2244 rtx new_reg;
2246 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2247 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2248 get_index_code (ad));
2249 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2250 cl, "base + disp");
2251 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2252 return new_reg;
2255 /* Return true if we can add a displacement to address AD, even if that
2256 makes the address invalid. The fix-up code requires any new address
2257 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2258 static bool
2259 can_add_disp_p (struct address_info *ad)
2261 return (!ad->autoinc_p
2262 && ad->segment == NULL
2263 && ad->base == ad->base_term
2264 && ad->disp == ad->disp_term);
2267 /* Make equiv substitution in address AD. Return true if a substitution
2268 was made. */
2269 static bool
2270 equiv_address_substitution (struct address_info *ad)
2272 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2273 HOST_WIDE_INT disp, scale;
2274 bool change_p;
2276 base_term = strip_subreg (ad->base_term);
2277 if (base_term == NULL)
2278 base_reg = new_base_reg = NULL_RTX;
2279 else
2281 base_reg = *base_term;
2282 new_base_reg = get_equiv_substitution (base_reg);
2284 index_term = strip_subreg (ad->index_term);
2285 if (index_term == NULL)
2286 index_reg = new_index_reg = NULL_RTX;
2287 else
2289 index_reg = *index_term;
2290 new_index_reg = get_equiv_substitution (index_reg);
2292 if (base_reg == new_base_reg && index_reg == new_index_reg)
2293 return false;
2294 disp = 0;
2295 change_p = false;
2296 if (lra_dump_file != NULL)
2298 fprintf (lra_dump_file, "Changing address in insn %d ",
2299 INSN_UID (curr_insn));
2300 dump_value_slim (lra_dump_file, *ad->outer, 1);
2302 if (base_reg != new_base_reg)
2304 if (REG_P (new_base_reg))
2306 *base_term = new_base_reg;
2307 change_p = true;
2309 else if (GET_CODE (new_base_reg) == PLUS
2310 && REG_P (XEXP (new_base_reg, 0))
2311 && CONST_INT_P (XEXP (new_base_reg, 1))
2312 && can_add_disp_p (ad))
2314 disp += INTVAL (XEXP (new_base_reg, 1));
2315 *base_term = XEXP (new_base_reg, 0);
2316 change_p = true;
2318 if (ad->base_term2 != NULL)
2319 *ad->base_term2 = *ad->base_term;
2321 if (index_reg != new_index_reg)
2323 if (REG_P (new_index_reg))
2325 *index_term = new_index_reg;
2326 change_p = true;
2328 else if (GET_CODE (new_index_reg) == PLUS
2329 && REG_P (XEXP (new_index_reg, 0))
2330 && CONST_INT_P (XEXP (new_index_reg, 1))
2331 && can_add_disp_p (ad)
2332 && (scale = get_index_scale (ad)))
2334 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2335 *index_term = XEXP (new_index_reg, 0);
2336 change_p = true;
2339 if (disp != 0)
2341 if (ad->disp != NULL)
2342 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2343 else
2345 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2346 update_address (ad);
2348 change_p = true;
2350 if (lra_dump_file != NULL)
2352 if (! change_p)
2353 fprintf (lra_dump_file, " -- no change\n");
2354 else
2356 fprintf (lra_dump_file, " on equiv ");
2357 dump_value_slim (lra_dump_file, *ad->outer, 1);
2358 fprintf (lra_dump_file, "\n");
2361 return change_p;
2364 /* Major function to make reloads for an address in operand NOP.
2365 The supported cases are:
2367 1) an address that existed before LRA started, at which point it must
2368 have been valid. These addresses are subject to elimination and
2369 may have become invalid due to the elimination offset being out
2370 of range.
2372 2) an address created by forcing a constant to memory (force_const_to_mem).
2373 The initial form of these addresses might not be valid, and it is this
2374 function's job to make them valid.
2376 3) a frame address formed from a register and a (possibly zero)
2377 constant offset. As above, these addresses might not be valid
2378 and this function must make them so.
2380 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2381 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2382 address. Return true for any RTL change. */
2383 static bool
2384 process_address (int nop, rtx *before, rtx *after)
2386 struct address_info ad;
2387 rtx new_reg;
2388 rtx op = *curr_id->operand_loc[nop];
2389 const char *constraint = curr_static_id->operand[nop].constraint;
2390 bool change_p;
2392 if (constraint[0] == 'p'
2393 || EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint))
2394 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2395 else if (MEM_P (op))
2396 decompose_mem_address (&ad, op);
2397 else if (GET_CODE (op) == SUBREG
2398 && MEM_P (SUBREG_REG (op)))
2399 decompose_mem_address (&ad, SUBREG_REG (op));
2400 else
2401 return false;
2402 change_p = equiv_address_substitution (&ad);
2403 if (ad.base_term != NULL
2404 && (process_addr_reg
2405 (ad.base_term, before,
2406 (ad.autoinc_p
2407 && !(REG_P (*ad.base_term)
2408 && find_regno_note (curr_insn, REG_DEAD,
2409 REGNO (*ad.base_term)) != NULL_RTX)
2410 ? after : NULL),
2411 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2412 get_index_code (&ad)))))
2414 change_p = true;
2415 if (ad.base_term2 != NULL)
2416 *ad.base_term2 = *ad.base_term;
2418 if (ad.index_term != NULL
2419 && process_addr_reg (ad.index_term, before, NULL, INDEX_REG_CLASS))
2420 change_p = true;
2422 /* There are three cases where the shape of *AD.INNER may now be invalid:
2424 1) the original address was valid, but either elimination or
2425 equiv_address_substitution applied a displacement that made
2426 it invalid.
2428 2) the address is an invalid symbolic address created by
2429 force_const_to_mem.
2431 3) the address is a frame address with an invalid offset.
2433 All these cases involve a displacement and a non-autoinc address,
2434 so there is no point revalidating other types. */
2435 if (ad.disp == NULL || ad.autoinc_p || valid_address_p (&ad))
2436 return change_p;
2438 /* Any index existed before LRA started, so we can assume that the
2439 presence and shape of the index is valid. */
2440 push_to_sequence (*before);
2441 gcc_assert (ad.segment == NULL);
2442 gcc_assert (ad.disp == ad.disp_term);
2443 if (ad.base == NULL)
2445 if (ad.index == NULL)
2447 int code = -1;
2448 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2449 SCRATCH, SCRATCH);
2450 rtx disp = *ad.disp;
2452 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2453 #ifdef HAVE_lo_sum
2455 rtx insn;
2456 rtx last = get_last_insn ();
2458 /* disp => lo_sum (new_base, disp), case (2) above. */
2459 insn = emit_insn (gen_rtx_SET
2460 (VOIDmode, new_reg,
2461 gen_rtx_HIGH (Pmode, copy_rtx (disp))));
2462 code = recog_memoized (insn);
2463 if (code >= 0)
2465 *ad.disp = gen_rtx_LO_SUM (Pmode, new_reg, disp);
2466 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2468 *ad.disp = disp;
2469 code = -1;
2472 if (code < 0)
2473 delete_insns_since (last);
2475 #endif
2476 if (code < 0)
2478 /* disp => new_base, case (2) above. */
2479 lra_emit_move (new_reg, disp);
2480 *ad.disp = new_reg;
2483 else
2485 /* index * scale + disp => new base + index * scale,
2486 case (1) above. */
2487 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
2488 GET_CODE (*ad.index));
2490 lra_assert (INDEX_REG_CLASS != NO_REGS);
2491 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2492 lra_emit_move (new_reg, *ad.disp);
2493 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2494 new_reg, *ad.index);
2497 else if (ad.index == NULL)
2499 /* base + disp => new base, cases (1) and (3) above. */
2500 /* Another option would be to reload the displacement into an
2501 index register. However, postreload has code to optimize
2502 address reloads that have the same base and different
2503 displacements, so reloading into an index register would
2504 not necessarily be a win. */
2505 new_reg = base_plus_disp_to_reg (&ad);
2506 *ad.inner = new_reg;
2508 else
2510 /* base + scale * index + disp => new base + scale * index,
2511 case (1) above. */
2512 new_reg = base_plus_disp_to_reg (&ad);
2513 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2514 new_reg, *ad.index);
2516 *before = get_insns ();
2517 end_sequence ();
2518 return true;
2521 /* Emit insns to reload VALUE into a new register. VALUE is an
2522 auto-increment or auto-decrement RTX whose operand is a register or
2523 memory location; so reloading involves incrementing that location.
2524 IN is either identical to VALUE, or some cheaper place to reload
2525 value being incremented/decremented from.
2527 INC_AMOUNT is the number to increment or decrement by (always
2528 positive and ignored for POST_MODIFY/PRE_MODIFY).
2530 Return pseudo containing the result. */
2531 static rtx
2532 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
2534 /* REG or MEM to be copied and incremented. */
2535 rtx incloc = XEXP (value, 0);
2536 /* Nonzero if increment after copying. */
2537 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
2538 || GET_CODE (value) == POST_MODIFY);
2539 rtx last;
2540 rtx inc;
2541 rtx add_insn;
2542 int code;
2543 rtx real_in = in == value ? incloc : in;
2544 rtx result;
2545 bool plus_p = true;
2547 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
2549 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
2550 || GET_CODE (XEXP (value, 1)) == MINUS);
2551 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
2552 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
2553 inc = XEXP (XEXP (value, 1), 1);
2555 else
2557 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
2558 inc_amount = -inc_amount;
2560 inc = GEN_INT (inc_amount);
2563 if (! post && REG_P (incloc))
2564 result = incloc;
2565 else
2566 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
2567 "INC/DEC result");
2569 if (real_in != result)
2571 /* First copy the location to the result register. */
2572 lra_assert (REG_P (result));
2573 emit_insn (gen_move_insn (result, real_in));
2576 /* We suppose that there are insns to add/sub with the constant
2577 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
2578 old reload worked with this assumption. If the assumption
2579 becomes wrong, we should use approach in function
2580 base_plus_disp_to_reg. */
2581 if (in == value)
2583 /* See if we can directly increment INCLOC. */
2584 last = get_last_insn ();
2585 add_insn = emit_insn (plus_p
2586 ? gen_add2_insn (incloc, inc)
2587 : gen_sub2_insn (incloc, inc));
2589 code = recog_memoized (add_insn);
2590 if (code >= 0)
2592 if (! post && result != incloc)
2593 emit_insn (gen_move_insn (result, incloc));
2594 return result;
2596 delete_insns_since (last);
2599 /* If couldn't do the increment directly, must increment in RESULT.
2600 The way we do this depends on whether this is pre- or
2601 post-increment. For pre-increment, copy INCLOC to the reload
2602 register, increment it there, then save back. */
2603 if (! post)
2605 if (real_in != result)
2606 emit_insn (gen_move_insn (result, real_in));
2607 if (plus_p)
2608 emit_insn (gen_add2_insn (result, inc));
2609 else
2610 emit_insn (gen_sub2_insn (result, inc));
2611 if (result != incloc)
2612 emit_insn (gen_move_insn (incloc, result));
2614 else
2616 /* Post-increment.
2618 Because this might be a jump insn or a compare, and because
2619 RESULT may not be available after the insn in an input
2620 reload, we must do the incrementing before the insn being
2621 reloaded for.
2623 We have already copied IN to RESULT. Increment the copy in
2624 RESULT, save that back, then decrement RESULT so it has
2625 the original value. */
2626 if (plus_p)
2627 emit_insn (gen_add2_insn (result, inc));
2628 else
2629 emit_insn (gen_sub2_insn (result, inc));
2630 emit_insn (gen_move_insn (incloc, result));
2631 /* Restore non-modified value for the result. We prefer this
2632 way because it does not require an additional hard
2633 register. */
2634 if (plus_p)
2636 if (CONST_INT_P (inc))
2637 emit_insn (gen_add2_insn (result, GEN_INT (-INTVAL (inc))));
2638 else
2639 emit_insn (gen_sub2_insn (result, inc));
2641 else
2642 emit_insn (gen_add2_insn (result, inc));
2644 return result;
2647 /* Swap operands NOP and NOP + 1. */
2648 static inline void
2649 swap_operands (int nop)
2651 enum machine_mode mode = curr_operand_mode[nop];
2652 curr_operand_mode[nop] = curr_operand_mode[nop + 1];
2653 curr_operand_mode[nop + 1] = mode;
2654 rtx x = *curr_id->operand_loc[nop];
2655 *curr_id->operand_loc[nop] = *curr_id->operand_loc[nop + 1];
2656 *curr_id->operand_loc[nop + 1] = x;
2657 /* Swap the duplicates too. */
2658 lra_update_dup (curr_id, nop);
2659 lra_update_dup (curr_id, nop + 1);
2662 /* Main entry point of the constraint code: search the body of the
2663 current insn to choose the best alternative. It is mimicking insn
2664 alternative cost calculation model of former reload pass. That is
2665 because machine descriptions were written to use this model. This
2666 model can be changed in future. Make commutative operand exchange
2667 if it is chosen.
2669 Return true if some RTL changes happened during function call. */
2670 static bool
2671 curr_insn_transform (void)
2673 int i, j, k;
2674 int n_operands;
2675 int n_alternatives;
2676 int commutative;
2677 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
2678 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
2679 rtx before, after;
2680 bool alt_p = false;
2681 /* Flag that the insn has been changed through a transformation. */
2682 bool change_p;
2683 bool sec_mem_p;
2684 #ifdef SECONDARY_MEMORY_NEEDED
2685 bool use_sec_mem_p;
2686 #endif
2687 int max_regno_before;
2688 int reused_alternative_num;
2690 no_input_reloads_p = no_output_reloads_p = false;
2691 goal_alt_number = -1;
2693 if (check_and_process_move (&change_p, &sec_mem_p))
2694 return change_p;
2696 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
2697 reloads; neither are insns that SET cc0. Insns that use CC0 are
2698 not allowed to have any input reloads. */
2699 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
2700 no_output_reloads_p = true;
2702 #ifdef HAVE_cc0
2703 if (reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
2704 no_input_reloads_p = true;
2705 if (reg_set_p (cc0_rtx, PATTERN (curr_insn)))
2706 no_output_reloads_p = true;
2707 #endif
2709 n_operands = curr_static_id->n_operands;
2710 n_alternatives = curr_static_id->n_alternatives;
2712 /* Just return "no reloads" if insn has no operands with
2713 constraints. */
2714 if (n_operands == 0 || n_alternatives == 0)
2715 return false;
2717 max_regno_before = max_reg_num ();
2719 for (i = 0; i < n_operands; i++)
2721 goal_alt_matched[i][0] = -1;
2722 goal_alt_matches[i] = -1;
2725 commutative = curr_static_id->commutative;
2727 /* Now see what we need for pseudos that didn't get hard regs or got
2728 the wrong kind of hard reg. For this, we must consider all the
2729 operands together against the register constraints. */
2731 best_losers = best_overall = INT_MAX;
2732 best_small_class_operands_num = best_reload_sum = 0;
2734 curr_swapped = false;
2735 goal_alt_swapped = false;
2737 /* Make equivalence substitution and memory subreg elimination
2738 before address processing because an address legitimacy can
2739 depend on memory mode. */
2740 for (i = 0; i < n_operands; i++)
2742 rtx op = *curr_id->operand_loc[i];
2743 rtx subst, old = op;
2744 bool op_change_p = false;
2746 if (GET_CODE (old) == SUBREG)
2747 old = SUBREG_REG (old);
2748 subst = get_equiv_substitution (old);
2749 if (subst != old)
2751 subst = copy_rtx (subst);
2752 lra_assert (REG_P (old));
2753 if (GET_CODE (op) == SUBREG)
2754 SUBREG_REG (op) = subst;
2755 else
2756 *curr_id->operand_loc[i] = subst;
2757 if (lra_dump_file != NULL)
2759 fprintf (lra_dump_file,
2760 "Changing pseudo %d in operand %i of insn %u on equiv ",
2761 REGNO (old), i, INSN_UID (curr_insn));
2762 dump_value_slim (lra_dump_file, subst, 1);
2763 fprintf (lra_dump_file, "\n");
2765 op_change_p = change_p = true;
2767 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
2769 change_p = true;
2770 lra_update_dup (curr_id, i);
2774 /* Reload address registers and displacements. We do it before
2775 finding an alternative because of memory constraints. */
2776 before = after = NULL_RTX;
2777 for (i = 0; i < n_operands; i++)
2778 if (! curr_static_id->operand[i].is_operator
2779 && process_address (i, &before, &after))
2781 change_p = true;
2782 lra_update_dup (curr_id, i);
2785 if (change_p)
2786 /* If we've changed the instruction then any alternative that
2787 we chose previously may no longer be valid. */
2788 lra_set_used_insn_alternative (curr_insn, -1);
2790 try_swapped:
2792 reused_alternative_num = curr_id->used_insn_alternative;
2793 if (lra_dump_file != NULL && reused_alternative_num >= 0)
2794 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
2795 reused_alternative_num, INSN_UID (curr_insn));
2797 if (process_alt_operands (reused_alternative_num))
2798 alt_p = true;
2800 /* If insn is commutative (it's safe to exchange a certain pair of
2801 operands) then we need to try each alternative twice, the second
2802 time matching those two operands as if we had exchanged them. To
2803 do this, really exchange them in operands.
2805 If we have just tried the alternatives the second time, return
2806 operands to normal and drop through. */
2808 if (reused_alternative_num < 0 && commutative >= 0)
2810 curr_swapped = !curr_swapped;
2811 if (curr_swapped)
2813 swap_operands (commutative);
2814 goto try_swapped;
2816 else
2817 swap_operands (commutative);
2820 if (! alt_p && ! sec_mem_p)
2822 /* No alternative works with reloads?? */
2823 if (INSN_CODE (curr_insn) >= 0)
2824 fatal_insn ("unable to generate reloads for:", curr_insn);
2825 error_for_asm (curr_insn,
2826 "inconsistent operand constraints in an %<asm%>");
2827 /* Avoid further trouble with this insn. */
2828 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
2829 lra_invalidate_insn_data (curr_insn);
2830 return true;
2833 /* If the best alternative is with operands 1 and 2 swapped, swap
2834 them. Update the operand numbers of any reloads already
2835 pushed. */
2837 if (goal_alt_swapped)
2839 if (lra_dump_file != NULL)
2840 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
2841 INSN_UID (curr_insn));
2843 /* Swap the duplicates too. */
2844 swap_operands (commutative);
2845 change_p = true;
2848 #ifdef SECONDARY_MEMORY_NEEDED
2849 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
2850 too conservatively. So we use the secondary memory only if there
2851 is no any alternative without reloads. */
2852 use_sec_mem_p = false;
2853 if (! alt_p)
2854 use_sec_mem_p = true;
2855 else if (sec_mem_p)
2857 for (i = 0; i < n_operands; i++)
2858 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
2859 break;
2860 use_sec_mem_p = i < n_operands;
2863 if (use_sec_mem_p)
2865 rtx new_reg, src, dest, rld;
2866 enum machine_mode sec_mode, rld_mode;
2868 lra_assert (sec_mem_p);
2869 lra_assert (curr_static_id->operand[0].type == OP_OUT
2870 && curr_static_id->operand[1].type == OP_IN);
2871 dest = *curr_id->operand_loc[0];
2872 src = *curr_id->operand_loc[1];
2873 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
2874 ? dest : src);
2875 rld_mode = GET_MODE (rld);
2876 #ifdef SECONDARY_MEMORY_NEEDED_MODE
2877 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
2878 #else
2879 sec_mode = rld_mode;
2880 #endif
2881 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
2882 NO_REGS, "secondary");
2883 /* If the mode is changed, it should be wider. */
2884 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
2885 if (sec_mode != rld_mode)
2887 /* If the target says specifically to use another mode for
2888 secondary memory moves we can not reuse the original
2889 insn. */
2890 after = emit_spill_move (false, new_reg, dest);
2891 lra_process_new_insns (curr_insn, NULL_RTX, after,
2892 "Inserting the sec. move");
2893 before = emit_spill_move (true, new_reg, src);
2894 lra_process_new_insns (curr_insn, before, NULL_RTX, "Changing on");
2895 lra_set_insn_deleted (curr_insn);
2897 else if (dest == rld)
2899 *curr_id->operand_loc[0] = new_reg;
2900 after = emit_spill_move (false, new_reg, dest);
2901 lra_process_new_insns (curr_insn, NULL_RTX, after,
2902 "Inserting the sec. move");
2904 else
2906 *curr_id->operand_loc[1] = new_reg;
2907 before = emit_spill_move (true, new_reg, src);
2908 lra_process_new_insns (curr_insn, before, NULL_RTX,
2909 "Inserting the sec. move");
2911 lra_update_insn_regno_info (curr_insn);
2912 return true;
2914 #endif
2916 lra_assert (goal_alt_number >= 0);
2917 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
2919 if (lra_dump_file != NULL)
2921 const char *p;
2923 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
2924 goal_alt_number, INSN_UID (curr_insn));
2925 for (i = 0; i < n_operands; i++)
2927 p = (curr_static_id->operand_alternative
2928 [goal_alt_number * n_operands + i].constraint);
2929 if (*p == '\0')
2930 continue;
2931 fprintf (lra_dump_file, " (%d) ", i);
2932 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
2933 fputc (*p, lra_dump_file);
2935 fprintf (lra_dump_file, "\n");
2938 /* Right now, for any pair of operands I and J that are required to
2939 match, with J < I, goal_alt_matches[I] is J. Add I to
2940 goal_alt_matched[J]. */
2942 for (i = 0; i < n_operands; i++)
2943 if ((j = goal_alt_matches[i]) >= 0)
2945 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
2947 /* We allow matching one output operand and several input
2948 operands. */
2949 lra_assert (k == 0
2950 || (curr_static_id->operand[j].type == OP_OUT
2951 && curr_static_id->operand[i].type == OP_IN
2952 && (curr_static_id->operand
2953 [goal_alt_matched[j][0]].type == OP_IN)));
2954 goal_alt_matched[j][k] = i;
2955 goal_alt_matched[j][k + 1] = -1;
2958 for (i = 0; i < n_operands; i++)
2959 goal_alt_win[i] |= goal_alt_match_win[i];
2961 /* Any constants that aren't allowed and can't be reloaded into
2962 registers are here changed into memory references. */
2963 for (i = 0; i < n_operands; i++)
2964 if (goal_alt_win[i])
2966 int regno;
2967 enum reg_class new_class;
2968 rtx reg = *curr_id->operand_loc[i];
2970 if (GET_CODE (reg) == SUBREG)
2971 reg = SUBREG_REG (reg);
2973 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
2975 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
2977 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
2979 lra_assert (ok_p);
2980 change_class (regno, new_class, " Change", true);
2984 else
2986 const char *constraint;
2987 char c;
2988 rtx op = *curr_id->operand_loc[i];
2989 rtx subreg = NULL_RTX;
2990 enum machine_mode mode = curr_operand_mode[i];
2992 if (GET_CODE (op) == SUBREG)
2994 subreg = op;
2995 op = SUBREG_REG (op);
2996 mode = GET_MODE (op);
2999 if (CONST_POOL_OK_P (mode, op)
3000 && ((targetm.preferred_reload_class
3001 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3002 || no_input_reloads_p))
3004 rtx tem = force_const_mem (mode, op);
3006 change_p = true;
3007 if (subreg != NULL_RTX)
3008 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3010 *curr_id->operand_loc[i] = tem;
3011 lra_update_dup (curr_id, i);
3012 process_address (i, &before, &after);
3014 /* If the alternative accepts constant pool refs directly
3015 there will be no reload needed at all. */
3016 if (subreg != NULL_RTX)
3017 continue;
3018 /* Skip alternatives before the one requested. */
3019 constraint = (curr_static_id->operand_alternative
3020 [goal_alt_number * n_operands + i].constraint);
3021 for (;
3022 (c = *constraint) && c != ',' && c != '#';
3023 constraint += CONSTRAINT_LEN (c, constraint))
3025 if (c == TARGET_MEM_CONSTRAINT || c == 'o')
3026 break;
3027 #ifdef EXTRA_CONSTRAINT_STR
3028 if (EXTRA_MEMORY_CONSTRAINT (c, constraint)
3029 && EXTRA_CONSTRAINT_STR (tem, c, constraint))
3030 break;
3031 #endif
3033 if (c == '\0' || c == ',' || c == '#')
3034 continue;
3036 goal_alt_win[i] = true;
3040 for (i = 0; i < n_operands; i++)
3042 rtx old, new_reg;
3043 rtx op = *curr_id->operand_loc[i];
3045 if (goal_alt_win[i])
3047 if (goal_alt[i] == NO_REGS
3048 && REG_P (op)
3049 /* When we assign NO_REGS it means that we will not
3050 assign a hard register to the scratch pseudo by
3051 assigment pass and the scratch pseudo will be
3052 spilled. Spilled scratch pseudos are transformed
3053 back to scratches at the LRA end. */
3054 && lra_former_scratch_operand_p (curr_insn, i))
3055 change_class (REGNO (op), NO_REGS, " Change", true);
3056 continue;
3059 /* Operands that match previous ones have already been handled. */
3060 if (goal_alt_matches[i] >= 0)
3061 continue;
3063 /* We should not have an operand with a non-offsettable address
3064 appearing where an offsettable address will do. It also may
3065 be a case when the address should be special in other words
3066 not a general one (e.g. it needs no index reg). */
3067 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3069 enum reg_class rclass;
3070 rtx *loc = &XEXP (op, 0);
3071 enum rtx_code code = GET_CODE (*loc);
3073 push_to_sequence (before);
3074 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3075 MEM, SCRATCH);
3076 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3077 new_reg = emit_inc (rclass, *loc, *loc,
3078 /* This value does not matter for MODIFY. */
3079 GET_MODE_SIZE (GET_MODE (op)));
3080 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass,
3081 "offsetable address", &new_reg))
3082 lra_emit_move (new_reg, *loc);
3083 before = get_insns ();
3084 end_sequence ();
3085 *loc = new_reg;
3086 lra_update_dup (curr_id, i);
3088 else if (goal_alt_matched[i][0] == -1)
3090 enum machine_mode mode;
3091 rtx reg, *loc;
3092 int hard_regno, byte;
3093 enum op_type type = curr_static_id->operand[i].type;
3095 loc = curr_id->operand_loc[i];
3096 mode = curr_operand_mode[i];
3097 if (GET_CODE (*loc) == SUBREG)
3099 reg = SUBREG_REG (*loc);
3100 byte = SUBREG_BYTE (*loc);
3101 if (REG_P (reg)
3102 /* Strict_low_part requires reload the register not
3103 the sub-register. */
3104 && (curr_static_id->operand[i].strict_low
3105 || (GET_MODE_SIZE (mode)
3106 <= GET_MODE_SIZE (GET_MODE (reg))
3107 && (hard_regno
3108 = get_try_hard_regno (REGNO (reg))) >= 0
3109 && (simplify_subreg_regno
3110 (hard_regno,
3111 GET_MODE (reg), byte, mode) < 0)
3112 && (goal_alt[i] == NO_REGS
3113 || (simplify_subreg_regno
3114 (ira_class_hard_regs[goal_alt[i]][0],
3115 GET_MODE (reg), byte, mode) >= 0)))))
3117 loc = &SUBREG_REG (*loc);
3118 mode = GET_MODE (*loc);
3121 old = *loc;
3122 if (get_reload_reg (type, mode, old, goal_alt[i], "", &new_reg)
3123 && type != OP_OUT)
3125 push_to_sequence (before);
3126 lra_emit_move (new_reg, old);
3127 before = get_insns ();
3128 end_sequence ();
3130 *loc = new_reg;
3131 if (type != OP_IN
3132 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3134 start_sequence ();
3135 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3136 emit_insn (after);
3137 after = get_insns ();
3138 end_sequence ();
3139 *loc = new_reg;
3141 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3142 if (goal_alt_dont_inherit_ops[j] == i)
3144 lra_set_regno_unique_value (REGNO (new_reg));
3145 break;
3147 lra_update_dup (curr_id, i);
3149 else if (curr_static_id->operand[i].type == OP_IN
3150 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3151 == OP_OUT))
3153 /* generate reloads for input and matched outputs. */
3154 match_inputs[0] = i;
3155 match_inputs[1] = -1;
3156 match_reload (goal_alt_matched[i][0], match_inputs,
3157 goal_alt[i], &before, &after);
3159 else if (curr_static_id->operand[i].type == OP_OUT
3160 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3161 == OP_IN))
3162 /* Generate reloads for output and matched inputs. */
3163 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3164 else if (curr_static_id->operand[i].type == OP_IN
3165 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3166 == OP_IN))
3168 /* Generate reloads for matched inputs. */
3169 match_inputs[0] = i;
3170 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3171 match_inputs[j + 1] = k;
3172 match_inputs[j + 1] = -1;
3173 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3175 else
3176 /* We must generate code in any case when function
3177 process_alt_operands decides that it is possible. */
3178 gcc_unreachable ();
3180 if (before != NULL_RTX || after != NULL_RTX
3181 || max_regno_before != max_reg_num ())
3182 change_p = true;
3183 if (change_p)
3185 lra_update_operator_dups (curr_id);
3186 /* Something changes -- process the insn. */
3187 lra_update_insn_regno_info (curr_insn);
3189 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3190 return change_p;
3193 /* Return true if X is in LIST. */
3194 static bool
3195 in_list_p (rtx x, rtx list)
3197 for (; list != NULL_RTX; list = XEXP (list, 1))
3198 if (XEXP (list, 0) == x)
3199 return true;
3200 return false;
3203 /* Return true if X contains an allocatable hard register (if
3204 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3205 static bool
3206 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3208 int i, j;
3209 const char *fmt;
3210 enum rtx_code code;
3212 code = GET_CODE (x);
3213 if (REG_P (x))
3215 int regno = REGNO (x);
3216 HARD_REG_SET alloc_regs;
3218 if (hard_reg_p)
3220 if (regno >= FIRST_PSEUDO_REGISTER)
3221 regno = lra_get_regno_hard_regno (regno);
3222 if (regno < 0)
3223 return false;
3224 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3225 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3227 else
3229 if (regno < FIRST_PSEUDO_REGISTER)
3230 return false;
3231 if (! spilled_p)
3232 return true;
3233 return lra_get_regno_hard_regno (regno) < 0;
3236 fmt = GET_RTX_FORMAT (code);
3237 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3239 if (fmt[i] == 'e')
3241 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3242 return true;
3244 else if (fmt[i] == 'E')
3246 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3247 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3248 return true;
3251 return false;
3254 /* Process all regs in location *LOC and change them on equivalent
3255 substitution. Return true if any change was done. */
3256 static bool
3257 loc_equivalence_change_p (rtx *loc)
3259 rtx subst, reg, x = *loc;
3260 bool result = false;
3261 enum rtx_code code = GET_CODE (x);
3262 const char *fmt;
3263 int i, j;
3265 if (code == SUBREG)
3267 reg = SUBREG_REG (x);
3268 if ((subst = get_equiv_substitution (reg)) != reg
3269 && GET_MODE (subst) == VOIDmode)
3271 /* We cannot reload debug location. Simplify subreg here
3272 while we know the inner mode. */
3273 *loc = simplify_gen_subreg (GET_MODE (x), subst,
3274 GET_MODE (reg), SUBREG_BYTE (x));
3275 return true;
3278 if (code == REG && (subst = get_equiv_substitution (x)) != x)
3280 *loc = subst;
3281 return true;
3284 /* Scan all the operand sub-expressions. */
3285 fmt = GET_RTX_FORMAT (code);
3286 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3288 if (fmt[i] == 'e')
3289 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
3290 else if (fmt[i] == 'E')
3291 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3292 result
3293 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
3295 return result;
3298 /* Similar to loc_equivalence_change_p, but for use as
3299 simplify_replace_fn_rtx callback. */
3300 static rtx
3301 loc_equivalence_callback (rtx loc, const_rtx, void *)
3303 if (!REG_P (loc))
3304 return NULL_RTX;
3306 rtx subst = get_equiv_substitution (loc);
3307 if (subst != loc)
3308 return subst;
3310 return NULL_RTX;
3313 /* Maximum number of generated reload insns per an insn. It is for
3314 preventing this pass cycling in a bug case. */
3315 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
3317 /* The current iteration number of this LRA pass. */
3318 int lra_constraint_iter;
3320 /* The current iteration number of this LRA pass after the last spill
3321 pass. */
3322 int lra_constraint_iter_after_spill;
3324 /* True if we substituted equiv which needs checking register
3325 allocation correctness because the equivalent value contains
3326 allocatable hard registers or when we restore multi-register
3327 pseudo. */
3328 bool lra_risky_transformations_p;
3330 /* Return true if REGNO is referenced in more than one block. */
3331 static bool
3332 multi_block_pseudo_p (int regno)
3334 basic_block bb = NULL;
3335 unsigned int uid;
3336 bitmap_iterator bi;
3338 if (regno < FIRST_PSEUDO_REGISTER)
3339 return false;
3341 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
3342 if (bb == NULL)
3343 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
3344 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
3345 return true;
3346 return false;
3349 /* Return true if LIST contains a deleted insn. */
3350 static bool
3351 contains_deleted_insn_p (rtx list)
3353 for (; list != NULL_RTX; list = XEXP (list, 1))
3354 if (NOTE_P (XEXP (list, 0))
3355 && NOTE_KIND (XEXP (list, 0)) == NOTE_INSN_DELETED)
3356 return true;
3357 return false;
3360 /* Return true if X contains a pseudo dying in INSN. */
3361 static bool
3362 dead_pseudo_p (rtx x, rtx insn)
3364 int i, j;
3365 const char *fmt;
3366 enum rtx_code code;
3368 if (REG_P (x))
3369 return (insn != NULL_RTX
3370 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
3371 code = GET_CODE (x);
3372 fmt = GET_RTX_FORMAT (code);
3373 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3375 if (fmt[i] == 'e')
3377 if (dead_pseudo_p (XEXP (x, i), insn))
3378 return true;
3380 else if (fmt[i] == 'E')
3382 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3383 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
3384 return true;
3387 return false;
3390 /* Return true if INSN contains a dying pseudo in INSN right hand
3391 side. */
3392 static bool
3393 insn_rhs_dead_pseudo_p (rtx insn)
3395 rtx set = single_set (insn);
3397 gcc_assert (set != NULL);
3398 return dead_pseudo_p (SET_SRC (set), insn);
3401 /* Return true if any init insn of REGNO contains a dying pseudo in
3402 insn right hand side. */
3403 static bool
3404 init_insn_rhs_dead_pseudo_p (int regno)
3406 rtx insns = ira_reg_equiv[regno].init_insns;
3408 if (insns == NULL)
3409 return false;
3410 if (INSN_P (insns))
3411 return insn_rhs_dead_pseudo_p (insns);
3412 for (; insns != NULL_RTX; insns = XEXP (insns, 1))
3413 if (insn_rhs_dead_pseudo_p (XEXP (insns, 0)))
3414 return true;
3415 return false;
3418 /* Entry function of LRA constraint pass. Return true if the
3419 constraint pass did change the code. */
3420 bool
3421 lra_constraints (bool first_p)
3423 bool changed_p;
3424 int i, hard_regno, new_insns_num;
3425 unsigned int min_len, new_min_len, uid;
3426 rtx set, x, reg, dest_reg;
3427 basic_block last_bb;
3428 bitmap_head equiv_insn_bitmap;
3429 bitmap_iterator bi;
3431 lra_constraint_iter++;
3432 if (lra_dump_file != NULL)
3433 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
3434 lra_constraint_iter);
3435 lra_constraint_iter_after_spill++;
3436 if (lra_constraint_iter_after_spill > LRA_MAX_CONSTRAINT_ITERATION_NUMBER)
3437 internal_error
3438 ("Maximum number of LRA constraint passes is achieved (%d)\n",
3439 LRA_MAX_CONSTRAINT_ITERATION_NUMBER);
3440 changed_p = false;
3441 lra_risky_transformations_p = false;
3442 new_insn_uid_start = get_max_uid ();
3443 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
3444 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
3445 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3446 if (lra_reg_info[i].nrefs != 0)
3448 ira_reg_equiv[i].profitable_p = true;
3449 reg = regno_reg_rtx[i];
3450 if ((hard_regno = lra_get_regno_hard_regno (i)) >= 0)
3452 int j, nregs;
3454 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
3455 for (j = 0; j < nregs; j++)
3456 df_set_regs_ever_live (hard_regno + j, true);
3458 else if ((x = get_equiv_substitution (reg)) != reg)
3460 bool pseudo_p = contains_reg_p (x, false, false);
3461 rtx set, insn;
3463 /* After RTL transformation, we can not guarantee that
3464 pseudo in the substitution was not reloaded which might
3465 make equivalence invalid. For example, in reverse
3466 equiv of p0
3468 p0 <- ...
3470 equiv_mem <- p0
3472 the memory address register was reloaded before the 2nd
3473 insn. */
3474 if ((! first_p && pseudo_p)
3475 /* We don't use DF for compilation speed sake. So it
3476 is problematic to update live info when we use an
3477 equivalence containing pseudos in more than one
3478 BB. */
3479 || (pseudo_p && multi_block_pseudo_p (i))
3480 /* If an init insn was deleted for some reason, cancel
3481 the equiv. We could update the equiv insns after
3482 transformations including an equiv insn deletion
3483 but it is not worthy as such cases are extremely
3484 rare. */
3485 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
3486 /* If it is not a reverse equivalence, we check that a
3487 pseudo in rhs of the init insn is not dying in the
3488 insn. Otherwise, the live info at the beginning of
3489 the corresponding BB might be wrong after we
3490 removed the insn. When the equiv can be a
3491 constant, the right hand side of the init insn can
3492 be a pseudo. */
3493 || (! ((insn = ira_reg_equiv[i].init_insns) != NULL_RTX
3494 && INSN_P (insn)
3495 && (set = single_set (insn)) != NULL_RTX
3496 && REG_P (SET_DEST (set))
3497 && (int) REGNO (SET_DEST (set)) == i)
3498 && init_insn_rhs_dead_pseudo_p (i))
3499 /* Prevent access beyond equivalent memory for
3500 paradoxical subregs. */
3501 || (MEM_P (x)
3502 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
3503 > GET_MODE_SIZE (GET_MODE (x)))))
3504 ira_reg_equiv[i].defined_p = false;
3505 if (contains_reg_p (x, false, true))
3506 ira_reg_equiv[i].profitable_p = false;
3507 if (get_equiv_substitution (reg) != reg)
3508 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
3511 /* We should add all insns containing pseudos which should be
3512 substituted by their equivalences. */
3513 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
3514 lra_push_insn_by_uid (uid);
3515 lra_eliminate (false);
3516 min_len = lra_insn_stack_length ();
3517 new_insns_num = 0;
3518 last_bb = NULL;
3519 changed_p = false;
3520 while ((new_min_len = lra_insn_stack_length ()) != 0)
3522 curr_insn = lra_pop_insn ();
3523 --new_min_len;
3524 curr_bb = BLOCK_FOR_INSN (curr_insn);
3525 if (curr_bb != last_bb)
3527 last_bb = curr_bb;
3528 bb_reload_num = lra_curr_reload_num;
3530 if (min_len > new_min_len)
3532 min_len = new_min_len;
3533 new_insns_num = 0;
3535 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
3536 internal_error
3537 ("Max. number of generated reload insns per insn is achieved (%d)\n",
3538 MAX_RELOAD_INSNS_NUMBER);
3539 new_insns_num++;
3540 if (DEBUG_INSN_P (curr_insn))
3542 /* We need to check equivalence in debug insn and change
3543 pseudo to the equivalent value if necessary. */
3544 curr_id = lra_get_insn_recog_data (curr_insn);
3545 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
3547 rtx old = *curr_id->operand_loc[0];
3548 *curr_id->operand_loc[0]
3549 = simplify_replace_fn_rtx (old, NULL_RTX,
3550 loc_equivalence_callback, NULL);
3551 if (old != *curr_id->operand_loc[0])
3553 lra_update_insn_regno_info (curr_insn);
3554 changed_p = true;
3558 else if (INSN_P (curr_insn))
3560 if ((set = single_set (curr_insn)) != NULL_RTX)
3562 dest_reg = SET_DEST (set);
3563 /* The equivalence pseudo could be set up as SUBREG in a
3564 case when it is a call restore insn in a mode
3565 different from the pseudo mode. */
3566 if (GET_CODE (dest_reg) == SUBREG)
3567 dest_reg = SUBREG_REG (dest_reg);
3568 if ((REG_P (dest_reg)
3569 && (x = get_equiv_substitution (dest_reg)) != dest_reg
3570 /* Remove insns which set up a pseudo whose value
3571 can not be changed. Such insns might be not in
3572 init_insns because we don't update equiv data
3573 during insn transformations.
3575 As an example, let suppose that a pseudo got
3576 hard register and on the 1st pass was not
3577 changed to equivalent constant. We generate an
3578 additional insn setting up the pseudo because of
3579 secondary memory movement. Then the pseudo is
3580 spilled and we use the equiv constant. In this
3581 case we should remove the additional insn and
3582 this insn is not init_insns list. */
3583 && (! MEM_P (x) || MEM_READONLY_P (x)
3584 || in_list_p (curr_insn,
3585 ira_reg_equiv
3586 [REGNO (dest_reg)].init_insns)))
3587 || (((x = get_equiv_substitution (SET_SRC (set)))
3588 != SET_SRC (set))
3589 && in_list_p (curr_insn,
3590 ira_reg_equiv
3591 [REGNO (SET_SRC (set))].init_insns)))
3593 /* This is equiv init insn of pseudo which did not get a
3594 hard register -- remove the insn. */
3595 if (lra_dump_file != NULL)
3597 fprintf (lra_dump_file,
3598 " Removing equiv init insn %i (freq=%d)\n",
3599 INSN_UID (curr_insn),
3600 BLOCK_FOR_INSN (curr_insn)->frequency);
3601 dump_insn_slim (lra_dump_file, curr_insn);
3603 if (contains_reg_p (x, true, false))
3604 lra_risky_transformations_p = true;
3605 lra_set_insn_deleted (curr_insn);
3606 continue;
3609 curr_id = lra_get_insn_recog_data (curr_insn);
3610 curr_static_id = curr_id->insn_static_data;
3611 init_curr_insn_input_reloads ();
3612 init_curr_operand_mode ();
3613 if (curr_insn_transform ())
3614 changed_p = true;
3615 /* Check non-transformed insns too for equiv change as USE
3616 or CLOBBER don't need reloads but can contain pseudos
3617 being changed on their equivalences. */
3618 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
3619 && loc_equivalence_change_p (&PATTERN (curr_insn)))
3621 lra_update_insn_regno_info (curr_insn);
3622 changed_p = true;
3626 bitmap_clear (&equiv_insn_bitmap);
3627 /* If we used a new hard regno, changed_p should be true because the
3628 hard reg is assigned to a new pseudo. */
3629 #ifdef ENABLE_CHECKING
3630 if (! changed_p)
3632 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3633 if (lra_reg_info[i].nrefs != 0
3634 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
3636 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
3638 for (j = 0; j < nregs; j++)
3639 lra_assert (df_regs_ever_live_p (hard_regno + j));
3642 #endif
3643 return changed_p;
3646 /* Initiate the LRA constraint pass. It is done once per
3647 function. */
3648 void
3649 lra_constraints_init (void)
3653 /* Finalize the LRA constraint pass. It is done once per
3654 function. */
3655 void
3656 lra_constraints_finish (void)
3662 /* This page contains code to do inheritance/split
3663 transformations. */
3665 /* Number of reloads passed so far in current EBB. */
3666 static int reloads_num;
3668 /* Number of calls passed so far in current EBB. */
3669 static int calls_num;
3671 /* Current reload pseudo check for validity of elements in
3672 USAGE_INSNS. */
3673 static int curr_usage_insns_check;
3675 /* Info about last usage of registers in EBB to do inheritance/split
3676 transformation. Inheritance transformation is done from a spilled
3677 pseudo and split transformations from a hard register or a pseudo
3678 assigned to a hard register. */
3679 struct usage_insns
3681 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
3682 value INSNS is valid. The insns is chain of optional debug insns
3683 and a finishing non-debug insn using the corresponding reg. */
3684 int check;
3685 /* Value of global reloads_num at the last insn in INSNS. */
3686 int reloads_num;
3687 /* Value of global reloads_nums at the last insn in INSNS. */
3688 int calls_num;
3689 /* It can be true only for splitting. And it means that the restore
3690 insn should be put after insn given by the following member. */
3691 bool after_p;
3692 /* Next insns in the current EBB which use the original reg and the
3693 original reg value is not changed between the current insn and
3694 the next insns. In order words, e.g. for inheritance, if we need
3695 to use the original reg value again in the next insns we can try
3696 to use the value in a hard register from a reload insn of the
3697 current insn. */
3698 rtx insns;
3701 /* Map: regno -> corresponding pseudo usage insns. */
3702 static struct usage_insns *usage_insns;
3704 static void
3705 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
3707 usage_insns[regno].check = curr_usage_insns_check;
3708 usage_insns[regno].insns = insn;
3709 usage_insns[regno].reloads_num = reloads_num;
3710 usage_insns[regno].calls_num = calls_num;
3711 usage_insns[regno].after_p = after_p;
3714 /* The function is used to form list REGNO usages which consists of
3715 optional debug insns finished by a non-debug insn using REGNO.
3716 RELOADS_NUM is current number of reload insns processed so far. */
3717 static void
3718 add_next_usage_insn (int regno, rtx insn, int reloads_num)
3720 rtx next_usage_insns;
3722 if (usage_insns[regno].check == curr_usage_insns_check
3723 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
3724 && DEBUG_INSN_P (insn))
3726 /* Check that we did not add the debug insn yet. */
3727 if (next_usage_insns != insn
3728 && (GET_CODE (next_usage_insns) != INSN_LIST
3729 || XEXP (next_usage_insns, 0) != insn))
3730 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
3731 next_usage_insns);
3733 else if (NONDEBUG_INSN_P (insn))
3734 setup_next_usage_insn (regno, insn, reloads_num, false);
3735 else
3736 usage_insns[regno].check = 0;
3739 /* Replace all references to register OLD_REGNO in *LOC with pseudo
3740 register NEW_REG. Return true if any change was made. */
3741 static bool
3742 substitute_pseudo (rtx *loc, int old_regno, rtx new_reg)
3744 rtx x = *loc;
3745 bool result = false;
3746 enum rtx_code code;
3747 const char *fmt;
3748 int i, j;
3750 if (x == NULL_RTX)
3751 return false;
3753 code = GET_CODE (x);
3754 if (code == REG && (int) REGNO (x) == old_regno)
3756 enum machine_mode mode = GET_MODE (*loc);
3757 enum machine_mode inner_mode = GET_MODE (new_reg);
3759 if (mode != inner_mode)
3761 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
3762 || ! SCALAR_INT_MODE_P (inner_mode))
3763 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
3764 else
3765 new_reg = gen_lowpart_SUBREG (mode, new_reg);
3767 *loc = new_reg;
3768 return true;
3771 /* Scan all the operand sub-expressions. */
3772 fmt = GET_RTX_FORMAT (code);
3773 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3775 if (fmt[i] == 'e')
3777 if (substitute_pseudo (&XEXP (x, i), old_regno, new_reg))
3778 result = true;
3780 else if (fmt[i] == 'E')
3782 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3783 if (substitute_pseudo (&XVECEXP (x, i, j), old_regno, new_reg))
3784 result = true;
3787 return result;
3790 /* Return first non-debug insn in list USAGE_INSNS. */
3791 static rtx
3792 skip_usage_debug_insns (rtx usage_insns)
3794 rtx insn;
3796 /* Skip debug insns. */
3797 for (insn = usage_insns;
3798 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
3799 insn = XEXP (insn, 1))
3801 return insn;
3804 /* Return true if we need secondary memory moves for insn in
3805 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
3806 into the insn. */
3807 static bool
3808 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
3809 rtx usage_insns ATTRIBUTE_UNUSED)
3811 #ifndef SECONDARY_MEMORY_NEEDED
3812 return false;
3813 #else
3814 rtx insn, set, dest;
3815 enum reg_class cl;
3817 if (inher_cl == ALL_REGS
3818 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
3819 return false;
3820 lra_assert (INSN_P (insn));
3821 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
3822 return false;
3823 dest = SET_DEST (set);
3824 if (! REG_P (dest))
3825 return false;
3826 lra_assert (inher_cl != NO_REGS);
3827 cl = get_reg_class (REGNO (dest));
3828 return (cl != NO_REGS && cl != ALL_REGS
3829 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
3830 #endif
3833 /* Registers involved in inheritance/split in the current EBB
3834 (inheritance/split pseudos and original registers). */
3835 static bitmap_head check_only_regs;
3837 /* Do inheritance transformations for insn INSN, which defines (if
3838 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
3839 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
3840 form as the "insns" field of usage_insns. Return true if we
3841 succeed in such transformation.
3843 The transformations look like:
3845 p <- ... i <- ...
3846 ... p <- i (new insn)
3847 ... =>
3848 <- ... p ... <- ... i ...
3850 ... i <- p (new insn)
3851 <- ... p ... <- ... i ...
3852 ... =>
3853 <- ... p ... <- ... i ...
3854 where p is a spilled original pseudo and i is a new inheritance pseudo.
3857 The inheritance pseudo has the smallest class of two classes CL and
3858 class of ORIGINAL REGNO. */
3859 static bool
3860 inherit_reload_reg (bool def_p, int original_regno,
3861 enum reg_class cl, rtx insn, rtx next_usage_insns)
3863 enum reg_class rclass = lra_get_allocno_class (original_regno);
3864 rtx original_reg = regno_reg_rtx[original_regno];
3865 rtx new_reg, new_insns, usage_insn;
3867 lra_assert (! usage_insns[original_regno].after_p);
3868 if (lra_dump_file != NULL)
3869 fprintf (lra_dump_file,
3870 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
3871 if (! ira_reg_classes_intersect_p[cl][rclass])
3873 if (lra_dump_file != NULL)
3875 fprintf (lra_dump_file,
3876 " Rejecting inheritance for %d "
3877 "because of disjoint classes %s and %s\n",
3878 original_regno, reg_class_names[cl],
3879 reg_class_names[rclass]);
3880 fprintf (lra_dump_file,
3881 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
3883 return false;
3885 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
3886 /* We don't use a subset of two classes because it can be
3887 NO_REGS. This transformation is still profitable in most
3888 cases even if the classes are not intersected as register
3889 move is probably cheaper than a memory load. */
3890 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
3892 if (lra_dump_file != NULL)
3893 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
3894 reg_class_names[cl], reg_class_names[rclass]);
3896 rclass = cl;
3898 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
3900 /* Reject inheritance resulting in secondary memory moves.
3901 Otherwise, there is a danger in LRA cycling. Also such
3902 transformation will be unprofitable. */
3903 if (lra_dump_file != NULL)
3905 rtx insn = skip_usage_debug_insns (next_usage_insns);
3906 rtx set = single_set (insn);
3908 lra_assert (set != NULL_RTX);
3910 rtx dest = SET_DEST (set);
3912 lra_assert (REG_P (dest));
3913 fprintf (lra_dump_file,
3914 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
3915 "as secondary mem is needed\n",
3916 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
3917 original_regno, reg_class_names[rclass]);
3918 fprintf (lra_dump_file,
3919 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
3921 return false;
3923 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
3924 rclass, "inheritance");
3925 start_sequence ();
3926 if (def_p)
3927 emit_move_insn (original_reg, new_reg);
3928 else
3929 emit_move_insn (new_reg, original_reg);
3930 new_insns = get_insns ();
3931 end_sequence ();
3932 if (NEXT_INSN (new_insns) != NULL_RTX)
3934 if (lra_dump_file != NULL)
3936 fprintf (lra_dump_file,
3937 " Rejecting inheritance %d->%d "
3938 "as it results in 2 or more insns:\n",
3939 original_regno, REGNO (new_reg));
3940 dump_rtl_slim (lra_dump_file, new_insns, NULL_RTX, -1, 0);
3941 fprintf (lra_dump_file,
3942 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
3944 return false;
3946 substitute_pseudo (&insn, original_regno, new_reg);
3947 lra_update_insn_regno_info (insn);
3948 if (! def_p)
3949 /* We now have a new usage insn for original regno. */
3950 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
3951 if (lra_dump_file != NULL)
3952 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
3953 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
3954 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
3955 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
3956 bitmap_set_bit (&check_only_regs, original_regno);
3957 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
3958 if (def_p)
3959 lra_process_new_insns (insn, NULL_RTX, new_insns,
3960 "Add original<-inheritance");
3961 else
3962 lra_process_new_insns (insn, new_insns, NULL_RTX,
3963 "Add inheritance<-original");
3964 while (next_usage_insns != NULL_RTX)
3966 if (GET_CODE (next_usage_insns) != INSN_LIST)
3968 usage_insn = next_usage_insns;
3969 lra_assert (NONDEBUG_INSN_P (usage_insn));
3970 next_usage_insns = NULL;
3972 else
3974 usage_insn = XEXP (next_usage_insns, 0);
3975 lra_assert (DEBUG_INSN_P (usage_insn));
3976 next_usage_insns = XEXP (next_usage_insns, 1);
3978 substitute_pseudo (&usage_insn, original_regno, new_reg);
3979 lra_update_insn_regno_info (usage_insn);
3980 if (lra_dump_file != NULL)
3982 fprintf (lra_dump_file,
3983 " Inheritance reuse change %d->%d (bb%d):\n",
3984 original_regno, REGNO (new_reg),
3985 BLOCK_FOR_INSN (usage_insn)->index);
3986 dump_insn_slim (lra_dump_file, usage_insn);
3989 if (lra_dump_file != NULL)
3990 fprintf (lra_dump_file,
3991 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
3992 return true;
3995 /* Return true if we need a caller save/restore for pseudo REGNO which
3996 was assigned to a hard register. */
3997 static inline bool
3998 need_for_call_save_p (int regno)
4000 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4001 return (usage_insns[regno].calls_num < calls_num
4002 && (overlaps_hard_reg_set_p
4003 (call_used_reg_set,
4004 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])));
4007 /* Global registers occuring in the current EBB. */
4008 static bitmap_head ebb_global_regs;
4010 /* Return true if we need a split for hard register REGNO or pseudo
4011 REGNO which was assigned to a hard register.
4012 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4013 used for reloads since the EBB end. It is an approximation of the
4014 used hard registers in the split range. The exact value would
4015 require expensive calculations. If we were aggressive with
4016 splitting because of the approximation, the split pseudo will save
4017 the same hard register assignment and will be removed in the undo
4018 pass. We still need the approximation because too aggressive
4019 splitting would result in too inaccurate cost calculation in the
4020 assignment pass because of too many generated moves which will be
4021 probably removed in the undo pass. */
4022 static inline bool
4023 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4025 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4027 lra_assert (hard_regno >= 0);
4028 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4029 /* Don't split eliminable hard registers, otherwise we can
4030 split hard registers like hard frame pointer, which
4031 lives on BB start/end according to DF-infrastructure,
4032 when there is a pseudo assigned to the register and
4033 living in the same BB. */
4034 && (regno >= FIRST_PSEUDO_REGISTER
4035 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4036 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4037 /* We need at least 2 reloads to make pseudo splitting
4038 profitable. We should provide hard regno splitting in
4039 any case to solve 1st insn scheduling problem when
4040 moving hard register definition up might result in
4041 impossibility to find hard register for reload pseudo of
4042 small register class. */
4043 && (usage_insns[regno].reloads_num
4044 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 2) < reloads_num)
4045 && (regno < FIRST_PSEUDO_REGISTER
4046 /* For short living pseudos, spilling + inheritance can
4047 be considered a substitution for splitting.
4048 Therefore we do not splitting for local pseudos. It
4049 decreases also aggressiveness of splitting. The
4050 minimal number of references is chosen taking into
4051 account that for 2 references splitting has no sense
4052 as we can just spill the pseudo. */
4053 || (regno >= FIRST_PSEUDO_REGISTER
4054 && lra_reg_info[regno].nrefs > 3
4055 && bitmap_bit_p (&ebb_global_regs, regno))))
4056 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4059 /* Return class for the split pseudo created from original pseudo with
4060 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4061 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4062 results in no secondary memory movements. */
4063 static enum reg_class
4064 choose_split_class (enum reg_class allocno_class,
4065 int hard_regno ATTRIBUTE_UNUSED,
4066 enum machine_mode mode ATTRIBUTE_UNUSED)
4068 #ifndef SECONDARY_MEMORY_NEEDED
4069 return allocno_class;
4070 #else
4071 int i;
4072 enum reg_class cl, best_cl = NO_REGS;
4073 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4074 = REGNO_REG_CLASS (hard_regno);
4076 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4077 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4078 return allocno_class;
4079 for (i = 0;
4080 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4081 i++)
4082 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4083 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4084 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4085 && (best_cl == NO_REGS
4086 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4087 best_cl = cl;
4088 return best_cl;
4089 #endif
4092 /* Do split transformations for insn INSN, which defines or uses
4093 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4094 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4095 "insns" field of usage_insns.
4097 The transformations look like:
4099 p <- ... p <- ...
4100 ... s <- p (new insn -- save)
4101 ... =>
4102 ... p <- s (new insn -- restore)
4103 <- ... p ... <- ... p ...
4105 <- ... p ... <- ... p ...
4106 ... s <- p (new insn -- save)
4107 ... =>
4108 ... p <- s (new insn -- restore)
4109 <- ... p ... <- ... p ...
4111 where p is an original pseudo got a hard register or a hard
4112 register and s is a new split pseudo. The save is put before INSN
4113 if BEFORE_P is true. Return true if we succeed in such
4114 transformation. */
4115 static bool
4116 split_reg (bool before_p, int original_regno, rtx insn, rtx next_usage_insns)
4118 enum reg_class rclass;
4119 rtx original_reg;
4120 int hard_regno;
4121 rtx new_reg, save, restore, usage_insn;
4122 bool after_p;
4123 bool call_save_p;
4125 if (original_regno < FIRST_PSEUDO_REGISTER)
4127 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4128 hard_regno = original_regno;
4129 call_save_p = false;
4131 else
4133 hard_regno = reg_renumber[original_regno];
4134 rclass = lra_get_allocno_class (original_regno);
4135 original_reg = regno_reg_rtx[original_regno];
4136 call_save_p = need_for_call_save_p (original_regno);
4138 original_reg = regno_reg_rtx[original_regno];
4139 lra_assert (hard_regno >= 0);
4140 if (lra_dump_file != NULL)
4141 fprintf (lra_dump_file,
4142 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4143 if (call_save_p)
4145 enum machine_mode sec_mode;
4147 #ifdef SECONDARY_MEMORY_NEEDED_MODE
4148 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (original_reg));
4149 #else
4150 sec_mode = GET_MODE (original_reg);
4151 #endif
4152 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
4153 NO_REGS, "save");
4155 else
4157 rclass = choose_split_class (rclass, hard_regno,
4158 GET_MODE (original_reg));
4159 if (rclass == NO_REGS)
4161 if (lra_dump_file != NULL)
4163 fprintf (lra_dump_file,
4164 " Rejecting split of %d(%s): "
4165 "no good reg class for %d(%s)\n",
4166 original_regno,
4167 reg_class_names[lra_get_allocno_class (original_regno)],
4168 hard_regno,
4169 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4170 fprintf
4171 (lra_dump_file,
4172 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4174 return false;
4176 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4177 rclass, "split");
4178 reg_renumber[REGNO (new_reg)] = hard_regno;
4180 save = emit_spill_move (true, new_reg, original_reg);
4181 if (NEXT_INSN (save) != NULL_RTX)
4183 lra_assert (! call_save_p);
4184 if (lra_dump_file != NULL)
4186 fprintf
4187 (lra_dump_file,
4188 " Rejecting split %d->%d resulting in > 2 %s save insns:\n",
4189 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4190 dump_rtl_slim (lra_dump_file, save, NULL_RTX, -1, 0);
4191 fprintf (lra_dump_file,
4192 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4194 return false;
4196 restore = emit_spill_move (false, new_reg, original_reg);
4197 if (NEXT_INSN (restore) != NULL_RTX)
4199 lra_assert (! call_save_p);
4200 if (lra_dump_file != NULL)
4202 fprintf (lra_dump_file,
4203 " Rejecting split %d->%d "
4204 "resulting in > 2 %s restore insns:\n",
4205 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4206 dump_rtl_slim (lra_dump_file, restore, NULL_RTX, -1, 0);
4207 fprintf (lra_dump_file,
4208 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4210 return false;
4212 after_p = usage_insns[original_regno].after_p;
4213 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4214 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4215 bitmap_set_bit (&check_only_regs, original_regno);
4216 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4217 for (;;)
4219 if (GET_CODE (next_usage_insns) != INSN_LIST)
4221 usage_insn = next_usage_insns;
4222 break;
4224 usage_insn = XEXP (next_usage_insns, 0);
4225 lra_assert (DEBUG_INSN_P (usage_insn));
4226 next_usage_insns = XEXP (next_usage_insns, 1);
4227 substitute_pseudo (&usage_insn, original_regno, new_reg);
4228 lra_update_insn_regno_info (usage_insn);
4229 if (lra_dump_file != NULL)
4231 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
4232 original_regno, REGNO (new_reg));
4233 dump_insn_slim (lra_dump_file, usage_insn);
4236 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
4237 lra_assert (usage_insn != insn || (after_p && before_p));
4238 lra_process_new_insns (usage_insn, after_p ? NULL_RTX : restore,
4239 after_p ? restore : NULL_RTX,
4240 call_save_p
4241 ? "Add reg<-save" : "Add reg<-split");
4242 lra_process_new_insns (insn, before_p ? save : NULL_RTX,
4243 before_p ? NULL_RTX : save,
4244 call_save_p
4245 ? "Add save<-reg" : "Add split<-reg");
4246 if (lra_dump_file != NULL)
4247 fprintf (lra_dump_file,
4248 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4249 return true;
4252 /* Recognize that we need a split transformation for insn INSN, which
4253 defines or uses REGNO in its insn biggest MODE (we use it only if
4254 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
4255 hard registers which might be used for reloads since the EBB end.
4256 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
4257 uid before starting INSN processing. Return true if we succeed in
4258 such transformation. */
4259 static bool
4260 split_if_necessary (int regno, enum machine_mode mode,
4261 HARD_REG_SET potential_reload_hard_regs,
4262 bool before_p, rtx insn, int max_uid)
4264 bool res = false;
4265 int i, nregs = 1;
4266 rtx next_usage_insns;
4268 if (regno < FIRST_PSEUDO_REGISTER)
4269 nregs = hard_regno_nregs[regno][mode];
4270 for (i = 0; i < nregs; i++)
4271 if (usage_insns[regno + i].check == curr_usage_insns_check
4272 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
4273 /* To avoid processing the register twice or more. */
4274 && ((GET_CODE (next_usage_insns) != INSN_LIST
4275 && INSN_UID (next_usage_insns) < max_uid)
4276 || (GET_CODE (next_usage_insns) == INSN_LIST
4277 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
4278 && need_for_split_p (potential_reload_hard_regs, regno + i)
4279 && split_reg (before_p, regno + i, insn, next_usage_insns))
4280 res = true;
4281 return res;
4284 /* Check only registers living at the current program point in the
4285 current EBB. */
4286 static bitmap_head live_regs;
4288 /* Update live info in EBB given by its HEAD and TAIL insns after
4289 inheritance/split transformation. The function removes dead moves
4290 too. */
4291 static void
4292 update_ebb_live_info (rtx head, rtx tail)
4294 unsigned int j;
4295 int regno;
4296 bool live_p;
4297 rtx prev_insn, set;
4298 bool remove_p;
4299 basic_block last_bb, prev_bb, curr_bb;
4300 bitmap_iterator bi;
4301 struct lra_insn_reg *reg;
4302 edge e;
4303 edge_iterator ei;
4305 last_bb = BLOCK_FOR_INSN (tail);
4306 prev_bb = NULL;
4307 for (curr_insn = tail;
4308 curr_insn != PREV_INSN (head);
4309 curr_insn = prev_insn)
4311 prev_insn = PREV_INSN (curr_insn);
4312 /* We need to process empty blocks too. They contain
4313 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
4314 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
4315 continue;
4316 curr_bb = BLOCK_FOR_INSN (curr_insn);
4317 if (curr_bb != prev_bb)
4319 if (prev_bb != NULL)
4321 /* Update df_get_live_in (prev_bb): */
4322 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4323 if (bitmap_bit_p (&live_regs, j))
4324 bitmap_set_bit (df_get_live_in (prev_bb), j);
4325 else
4326 bitmap_clear_bit (df_get_live_in (prev_bb), j);
4328 if (curr_bb != last_bb)
4330 /* Update df_get_live_out (curr_bb): */
4331 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4333 live_p = bitmap_bit_p (&live_regs, j);
4334 if (! live_p)
4335 FOR_EACH_EDGE (e, ei, curr_bb->succs)
4336 if (bitmap_bit_p (df_get_live_in (e->dest), j))
4338 live_p = true;
4339 break;
4341 if (live_p)
4342 bitmap_set_bit (df_get_live_out (curr_bb), j);
4343 else
4344 bitmap_clear_bit (df_get_live_out (curr_bb), j);
4347 prev_bb = curr_bb;
4348 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
4350 if (! NONDEBUG_INSN_P (curr_insn))
4351 continue;
4352 curr_id = lra_get_insn_recog_data (curr_insn);
4353 remove_p = false;
4354 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
4355 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
4356 && bitmap_bit_p (&check_only_regs, regno)
4357 && ! bitmap_bit_p (&live_regs, regno))
4358 remove_p = true;
4359 /* See which defined values die here. */
4360 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4361 if (reg->type == OP_OUT && ! reg->subreg_p)
4362 bitmap_clear_bit (&live_regs, reg->regno);
4363 /* Mark each used value as live. */
4364 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4365 if (reg->type == OP_IN
4366 && bitmap_bit_p (&check_only_regs, reg->regno))
4367 bitmap_set_bit (&live_regs, reg->regno);
4368 /* It is quite important to remove dead move insns because it
4369 means removing dead store. We don't need to process them for
4370 constraints. */
4371 if (remove_p)
4373 if (lra_dump_file != NULL)
4375 fprintf (lra_dump_file, " Removing dead insn:\n ");
4376 dump_insn_slim (lra_dump_file, curr_insn);
4378 lra_set_insn_deleted (curr_insn);
4383 /* The structure describes info to do an inheritance for the current
4384 insn. We need to collect such info first before doing the
4385 transformations because the transformations change the insn
4386 internal representation. */
4387 struct to_inherit
4389 /* Original regno. */
4390 int regno;
4391 /* Subsequent insns which can inherit original reg value. */
4392 rtx insns;
4395 /* Array containing all info for doing inheritance from the current
4396 insn. */
4397 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
4399 /* Number elements in the previous array. */
4400 static int to_inherit_num;
4402 /* Add inheritance info REGNO and INSNS. Their meaning is described in
4403 structure to_inherit. */
4404 static void
4405 add_to_inherit (int regno, rtx insns)
4407 int i;
4409 for (i = 0; i < to_inherit_num; i++)
4410 if (to_inherit[i].regno == regno)
4411 return;
4412 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
4413 to_inherit[to_inherit_num].regno = regno;
4414 to_inherit[to_inherit_num++].insns = insns;
4417 /* Return the last non-debug insn in basic block BB, or the block begin
4418 note if none. */
4419 static rtx
4420 get_last_insertion_point (basic_block bb)
4422 rtx insn;
4424 FOR_BB_INSNS_REVERSE (bb, insn)
4425 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
4426 return insn;
4427 gcc_unreachable ();
4430 /* Set up RES by registers living on edges FROM except the edge (FROM,
4431 TO) or by registers set up in a jump insn in BB FROM. */
4432 static void
4433 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
4435 rtx last;
4436 struct lra_insn_reg *reg;
4437 edge e;
4438 edge_iterator ei;
4440 lra_assert (to != NULL);
4441 bitmap_clear (res);
4442 FOR_EACH_EDGE (e, ei, from->succs)
4443 if (e->dest != to)
4444 bitmap_ior_into (res, df_get_live_in (e->dest));
4445 last = get_last_insertion_point (from);
4446 if (! JUMP_P (last))
4447 return;
4448 curr_id = lra_get_insn_recog_data (last);
4449 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4450 if (reg->type != OP_IN)
4451 bitmap_set_bit (res, reg->regno);
4454 /* Used as a temporary results of some bitmap calculations. */
4455 static bitmap_head temp_bitmap;
4457 /* Do inheritance/split transformations in EBB starting with HEAD and
4458 finishing on TAIL. We process EBB insns in the reverse order.
4459 Return true if we did any inheritance/split transformation in the
4460 EBB.
4462 We should avoid excessive splitting which results in worse code
4463 because of inaccurate cost calculations for spilling new split
4464 pseudos in such case. To achieve this we do splitting only if
4465 register pressure is high in given basic block and there are reload
4466 pseudos requiring hard registers. We could do more register
4467 pressure calculations at any given program point to avoid necessary
4468 splitting even more but it is to expensive and the current approach
4469 works well enough. */
4470 static bool
4471 inherit_in_ebb (rtx head, rtx tail)
4473 int i, src_regno, dst_regno, nregs;
4474 bool change_p, succ_p;
4475 rtx prev_insn, next_usage_insns, set, last_insn;
4476 enum reg_class cl;
4477 struct lra_insn_reg *reg;
4478 basic_block last_processed_bb, curr_bb = NULL;
4479 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
4480 bitmap to_process;
4481 unsigned int j;
4482 bitmap_iterator bi;
4483 bool head_p, after_p;
4485 change_p = false;
4486 curr_usage_insns_check++;
4487 reloads_num = calls_num = 0;
4488 bitmap_clear (&check_only_regs);
4489 last_processed_bb = NULL;
4490 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
4491 CLEAR_HARD_REG_SET (live_hard_regs);
4492 /* We don't process new insns generated in the loop. */
4493 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
4495 prev_insn = PREV_INSN (curr_insn);
4496 if (BLOCK_FOR_INSN (curr_insn) != NULL)
4497 curr_bb = BLOCK_FOR_INSN (curr_insn);
4498 if (last_processed_bb != curr_bb)
4500 /* We are at the end of BB. Add qualified living
4501 pseudos for potential splitting. */
4502 to_process = df_get_live_out (curr_bb);
4503 if (last_processed_bb != NULL)
4505 /* We are somewhere in the middle of EBB. */
4506 get_live_on_other_edges (curr_bb, last_processed_bb,
4507 &temp_bitmap);
4508 to_process = &temp_bitmap;
4510 last_processed_bb = curr_bb;
4511 last_insn = get_last_insertion_point (curr_bb);
4512 after_p = (! JUMP_P (last_insn)
4513 && (! CALL_P (last_insn)
4514 || (find_reg_note (last_insn,
4515 REG_NORETURN, NULL_RTX) == NULL_RTX
4516 && ! SIBLING_CALL_P (last_insn))));
4517 REG_SET_TO_HARD_REG_SET (live_hard_regs, df_get_live_out (curr_bb));
4518 IOR_HARD_REG_SET (live_hard_regs, eliminable_regset);
4519 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
4520 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
4521 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
4523 if ((int) j >= lra_constraint_new_regno_start)
4524 break;
4525 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
4527 if (j < FIRST_PSEUDO_REGISTER)
4528 SET_HARD_REG_BIT (live_hard_regs, j);
4529 else
4530 add_to_hard_reg_set (&live_hard_regs,
4531 PSEUDO_REGNO_MODE (j),
4532 reg_renumber[j]);
4533 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
4537 src_regno = dst_regno = -1;
4538 if (NONDEBUG_INSN_P (curr_insn)
4539 && (set = single_set (curr_insn)) != NULL_RTX
4540 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
4542 src_regno = REGNO (SET_SRC (set));
4543 dst_regno = REGNO (SET_DEST (set));
4545 if (src_regno < lra_constraint_new_regno_start
4546 && src_regno >= FIRST_PSEUDO_REGISTER
4547 && reg_renumber[src_regno] < 0
4548 && dst_regno >= lra_constraint_new_regno_start
4549 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
4551 /* 'reload_pseudo <- original_pseudo'. */
4552 reloads_num++;
4553 succ_p = false;
4554 if (usage_insns[src_regno].check == curr_usage_insns_check
4555 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
4556 succ_p = inherit_reload_reg (false, src_regno, cl,
4557 curr_insn, next_usage_insns);
4558 if (succ_p)
4559 change_p = true;
4560 else
4561 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
4562 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
4563 IOR_HARD_REG_SET (potential_reload_hard_regs,
4564 reg_class_contents[cl]);
4566 else if (src_regno >= lra_constraint_new_regno_start
4567 && dst_regno < lra_constraint_new_regno_start
4568 && dst_regno >= FIRST_PSEUDO_REGISTER
4569 && reg_renumber[dst_regno] < 0
4570 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
4571 && usage_insns[dst_regno].check == curr_usage_insns_check
4572 && (next_usage_insns
4573 = usage_insns[dst_regno].insns) != NULL_RTX)
4575 reloads_num++;
4576 /* 'original_pseudo <- reload_pseudo'. */
4577 if (! JUMP_P (curr_insn)
4578 && inherit_reload_reg (true, dst_regno, cl,
4579 curr_insn, next_usage_insns))
4580 change_p = true;
4581 /* Invalidate. */
4582 usage_insns[dst_regno].check = 0;
4583 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
4584 IOR_HARD_REG_SET (potential_reload_hard_regs,
4585 reg_class_contents[cl]);
4587 else if (INSN_P (curr_insn))
4589 int max_uid = get_max_uid ();
4591 curr_id = lra_get_insn_recog_data (curr_insn);
4592 to_inherit_num = 0;
4593 /* Process insn definitions. */
4594 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4595 if (reg->type != OP_IN
4596 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
4598 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
4599 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
4600 && usage_insns[dst_regno].check == curr_usage_insns_check
4601 && (next_usage_insns
4602 = usage_insns[dst_regno].insns) != NULL_RTX)
4604 struct lra_insn_reg *r;
4606 for (r = curr_id->regs; r != NULL; r = r->next)
4607 if (r->type != OP_OUT && r->regno == dst_regno)
4608 break;
4609 /* Don't do inheritance if the pseudo is also
4610 used in the insn. */
4611 if (r == NULL)
4612 /* We can not do inheritance right now
4613 because the current insn reg info (chain
4614 regs) can change after that. */
4615 add_to_inherit (dst_regno, next_usage_insns);
4617 /* We can not process one reg twice here because of
4618 usage_insns invalidation. */
4619 if ((dst_regno < FIRST_PSEUDO_REGISTER
4620 || reg_renumber[dst_regno] >= 0)
4621 && ! reg->subreg_p && reg->type == OP_OUT)
4623 HARD_REG_SET s;
4625 if (split_if_necessary (dst_regno, reg->biggest_mode,
4626 potential_reload_hard_regs,
4627 false, curr_insn, max_uid))
4628 change_p = true;
4629 CLEAR_HARD_REG_SET (s);
4630 if (dst_regno < FIRST_PSEUDO_REGISTER)
4631 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
4632 else
4633 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
4634 reg_renumber[dst_regno]);
4635 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
4637 /* We should invalidate potential inheritance or
4638 splitting for the current insn usages to the next
4639 usage insns (see code below) as the output pseudo
4640 prevents this. */
4641 if ((dst_regno >= FIRST_PSEUDO_REGISTER
4642 && reg_renumber[dst_regno] < 0)
4643 || (reg->type == OP_OUT && ! reg->subreg_p
4644 && (dst_regno < FIRST_PSEUDO_REGISTER
4645 || reg_renumber[dst_regno] >= 0)))
4647 /* Invalidate. */
4648 if (dst_regno >= FIRST_PSEUDO_REGISTER)
4649 usage_insns[dst_regno].check = 0;
4650 else
4652 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
4653 for (i = 0; i < nregs; i++)
4654 usage_insns[dst_regno + i].check = 0;
4658 if (! JUMP_P (curr_insn))
4659 for (i = 0; i < to_inherit_num; i++)
4660 if (inherit_reload_reg (true, to_inherit[i].regno,
4661 ALL_REGS, curr_insn,
4662 to_inherit[i].insns))
4663 change_p = true;
4664 if (CALL_P (curr_insn))
4666 rtx cheap, pat, dest, restore;
4667 int regno, hard_regno;
4669 calls_num++;
4670 if ((cheap = find_reg_note (curr_insn,
4671 REG_RETURNED, NULL_RTX)) != NULL_RTX
4672 && ((cheap = XEXP (cheap, 0)), true)
4673 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
4674 && (hard_regno = reg_renumber[regno]) >= 0
4675 /* If there are pending saves/restores, the
4676 optimization is not worth. */
4677 && usage_insns[regno].calls_num == calls_num - 1
4678 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
4680 /* Restore the pseudo from the call result as
4681 REG_RETURNED note says that the pseudo value is
4682 in the call result and the pseudo is an argument
4683 of the call. */
4684 pat = PATTERN (curr_insn);
4685 if (GET_CODE (pat) == PARALLEL)
4686 pat = XVECEXP (pat, 0, 0);
4687 dest = SET_DEST (pat);
4688 start_sequence ();
4689 emit_move_insn (cheap, copy_rtx (dest));
4690 restore = get_insns ();
4691 end_sequence ();
4692 lra_process_new_insns (curr_insn, NULL, restore,
4693 "Inserting call parameter restore");
4694 /* We don't need to save/restore of the pseudo from
4695 this call. */
4696 usage_insns[regno].calls_num = calls_num;
4697 bitmap_set_bit (&check_only_regs, regno);
4700 to_inherit_num = 0;
4701 /* Process insn usages. */
4702 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4703 if ((reg->type != OP_OUT
4704 || (reg->type == OP_OUT && reg->subreg_p))
4705 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
4707 if (src_regno >= FIRST_PSEUDO_REGISTER
4708 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
4710 if (usage_insns[src_regno].check == curr_usage_insns_check
4711 && (next_usage_insns
4712 = usage_insns[src_regno].insns) != NULL_RTX
4713 && NONDEBUG_INSN_P (curr_insn))
4714 add_to_inherit (src_regno, next_usage_insns);
4715 else
4716 /* Add usages. */
4717 add_next_usage_insn (src_regno, curr_insn, reloads_num);
4719 else if (src_regno < FIRST_PSEUDO_REGISTER
4720 || reg_renumber[src_regno] >= 0)
4722 bool before_p;
4723 rtx use_insn = curr_insn;
4725 before_p = (JUMP_P (curr_insn)
4726 || (CALL_P (curr_insn) && reg->type == OP_IN));
4727 if (NONDEBUG_INSN_P (curr_insn)
4728 && split_if_necessary (src_regno, reg->biggest_mode,
4729 potential_reload_hard_regs,
4730 before_p, curr_insn, max_uid))
4732 if (reg->subreg_p)
4733 lra_risky_transformations_p = true;
4734 change_p = true;
4735 /* Invalidate. */
4736 usage_insns[src_regno].check = 0;
4737 if (before_p)
4738 use_insn = PREV_INSN (curr_insn);
4740 if (NONDEBUG_INSN_P (curr_insn))
4742 if (src_regno < FIRST_PSEUDO_REGISTER)
4743 add_to_hard_reg_set (&live_hard_regs,
4744 reg->biggest_mode, src_regno);
4745 else
4746 add_to_hard_reg_set (&live_hard_regs,
4747 PSEUDO_REGNO_MODE (src_regno),
4748 reg_renumber[src_regno]);
4750 add_next_usage_insn (src_regno, use_insn, reloads_num);
4753 for (i = 0; i < to_inherit_num; i++)
4755 src_regno = to_inherit[i].regno;
4756 if (inherit_reload_reg (false, src_regno, ALL_REGS,
4757 curr_insn, to_inherit[i].insns))
4758 change_p = true;
4759 else
4760 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
4763 /* We reached the start of the current basic block. */
4764 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
4765 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
4767 /* We reached the beginning of the current block -- do
4768 rest of spliting in the current BB. */
4769 to_process = df_get_live_in (curr_bb);
4770 if (BLOCK_FOR_INSN (head) != curr_bb)
4772 /* We are somewhere in the middle of EBB. */
4773 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
4774 curr_bb, &temp_bitmap);
4775 to_process = &temp_bitmap;
4777 head_p = true;
4778 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
4780 if ((int) j >= lra_constraint_new_regno_start)
4781 break;
4782 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
4783 && usage_insns[j].check == curr_usage_insns_check
4784 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
4786 if (need_for_split_p (potential_reload_hard_regs, j))
4788 if (lra_dump_file != NULL && head_p)
4790 fprintf (lra_dump_file,
4791 " ----------------------------------\n");
4792 head_p = false;
4794 if (split_reg (false, j, bb_note (curr_bb),
4795 next_usage_insns))
4796 change_p = true;
4798 usage_insns[j].check = 0;
4803 return change_p;
4806 /* This value affects EBB forming. If probability of edge from EBB to
4807 a BB is not greater than the following value, we don't add the BB
4808 to EBB. */
4809 #define EBB_PROBABILITY_CUTOFF (REG_BR_PROB_BASE / 2)
4811 /* Current number of inheritance/split iteration. */
4812 int lra_inheritance_iter;
4814 /* Entry function for inheritance/split pass. */
4815 void
4816 lra_inheritance (void)
4818 int i;
4819 basic_block bb, start_bb;
4820 edge e;
4822 lra_inheritance_iter++;
4823 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
4824 return;
4825 timevar_push (TV_LRA_INHERITANCE);
4826 if (lra_dump_file != NULL)
4827 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
4828 lra_inheritance_iter);
4829 curr_usage_insns_check = 0;
4830 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
4831 for (i = 0; i < lra_constraint_new_regno_start; i++)
4832 usage_insns[i].check = 0;
4833 bitmap_initialize (&check_only_regs, &reg_obstack);
4834 bitmap_initialize (&live_regs, &reg_obstack);
4835 bitmap_initialize (&temp_bitmap, &reg_obstack);
4836 bitmap_initialize (&ebb_global_regs, &reg_obstack);
4837 FOR_EACH_BB (bb)
4839 start_bb = bb;
4840 if (lra_dump_file != NULL)
4841 fprintf (lra_dump_file, "EBB");
4842 /* Form a EBB starting with BB. */
4843 bitmap_clear (&ebb_global_regs);
4844 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
4845 for (;;)
4847 if (lra_dump_file != NULL)
4848 fprintf (lra_dump_file, " %d", bb->index);
4849 if (bb->next_bb == EXIT_BLOCK_PTR || LABEL_P (BB_HEAD (bb->next_bb)))
4850 break;
4851 e = find_fallthru_edge (bb->succs);
4852 if (! e)
4853 break;
4854 if (e->probability <= EBB_PROBABILITY_CUTOFF)
4855 break;
4856 bb = bb->next_bb;
4858 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
4859 if (lra_dump_file != NULL)
4860 fprintf (lra_dump_file, "\n");
4861 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
4862 /* Remember that the EBB head and tail can change in
4863 inherit_in_ebb. */
4864 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
4866 bitmap_clear (&ebb_global_regs);
4867 bitmap_clear (&temp_bitmap);
4868 bitmap_clear (&live_regs);
4869 bitmap_clear (&check_only_regs);
4870 free (usage_insns);
4872 timevar_pop (TV_LRA_INHERITANCE);
4877 /* This page contains code to undo failed inheritance/split
4878 transformations. */
4880 /* Current number of iteration undoing inheritance/split. */
4881 int lra_undo_inheritance_iter;
4883 /* Fix BB live info LIVE after removing pseudos created on pass doing
4884 inheritance/split which are REMOVED_PSEUDOS. */
4885 static void
4886 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
4888 unsigned int regno;
4889 bitmap_iterator bi;
4891 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
4892 if (bitmap_clear_bit (live, regno))
4893 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
4896 /* Return regno of the (subreg of) REG. Otherwise, return a negative
4897 number. */
4898 static int
4899 get_regno (rtx reg)
4901 if (GET_CODE (reg) == SUBREG)
4902 reg = SUBREG_REG (reg);
4903 if (REG_P (reg))
4904 return REGNO (reg);
4905 return -1;
4908 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
4909 return true if we did any change. The undo transformations for
4910 inheritance looks like
4911 i <- i2
4912 p <- i => p <- i2
4913 or removing
4914 p <- i, i <- p, and i <- i3
4915 where p is original pseudo from which inheritance pseudo i was
4916 created, i and i3 are removed inheritance pseudos, i2 is another
4917 not removed inheritance pseudo. All split pseudos or other
4918 occurrences of removed inheritance pseudos are changed on the
4919 corresponding original pseudos.
4921 The function also schedules insns changed and created during
4922 inheritance/split pass for processing by the subsequent constraint
4923 pass. */
4924 static bool
4925 remove_inheritance_pseudos (bitmap remove_pseudos)
4927 basic_block bb;
4928 int regno, sregno, prev_sregno, dregno, restore_regno;
4929 rtx set, prev_set, prev_insn;
4930 bool change_p, done_p;
4932 change_p = ! bitmap_empty_p (remove_pseudos);
4933 /* We can not finish the function right away if CHANGE_P is true
4934 because we need to marks insns affected by previous
4935 inheritance/split pass for processing by the subsequent
4936 constraint pass. */
4937 FOR_EACH_BB (bb)
4939 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
4940 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
4941 FOR_BB_INSNS_REVERSE (bb, curr_insn)
4943 if (! INSN_P (curr_insn))
4944 continue;
4945 done_p = false;
4946 sregno = dregno = -1;
4947 if (change_p && NONDEBUG_INSN_P (curr_insn)
4948 && (set = single_set (curr_insn)) != NULL_RTX)
4950 dregno = get_regno (SET_DEST (set));
4951 sregno = get_regno (SET_SRC (set));
4954 if (sregno >= 0 && dregno >= 0)
4956 if ((bitmap_bit_p (remove_pseudos, sregno)
4957 && (lra_reg_info[sregno].restore_regno == dregno
4958 || (bitmap_bit_p (remove_pseudos, dregno)
4959 && (lra_reg_info[sregno].restore_regno
4960 == lra_reg_info[dregno].restore_regno))))
4961 || (bitmap_bit_p (remove_pseudos, dregno)
4962 && lra_reg_info[dregno].restore_regno == sregno))
4963 /* One of the following cases:
4964 original <- removed inheritance pseudo
4965 removed inherit pseudo <- another removed inherit pseudo
4966 removed inherit pseudo <- original pseudo
4968 removed_split_pseudo <- original_reg
4969 original_reg <- removed_split_pseudo */
4971 if (lra_dump_file != NULL)
4973 fprintf (lra_dump_file, " Removing %s:\n",
4974 bitmap_bit_p (&lra_split_regs, sregno)
4975 || bitmap_bit_p (&lra_split_regs, dregno)
4976 ? "split" : "inheritance");
4977 dump_insn_slim (lra_dump_file, curr_insn);
4979 lra_set_insn_deleted (curr_insn);
4980 done_p = true;
4982 else if (bitmap_bit_p (remove_pseudos, sregno)
4983 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
4985 /* Search the following pattern:
4986 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
4987 original_pseudo <- inherit_or_split_pseudo1
4988 where the 2nd insn is the current insn and
4989 inherit_or_split_pseudo2 is not removed. If it is found,
4990 change the current insn onto:
4991 original_pseudo <- inherit_or_split_pseudo2. */
4992 for (prev_insn = PREV_INSN (curr_insn);
4993 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
4994 prev_insn = PREV_INSN (prev_insn))
4996 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
4997 && (prev_set = single_set (prev_insn)) != NULL_RTX
4998 /* There should be no subregs in insn we are
4999 searching because only the original reg might
5000 be in subreg when we changed the mode of
5001 load/store for splitting. */
5002 && REG_P (SET_DEST (prev_set))
5003 && REG_P (SET_SRC (prev_set))
5004 && (int) REGNO (SET_DEST (prev_set)) == sregno
5005 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5006 >= FIRST_PSEUDO_REGISTER)
5007 /* As we consider chain of inheritance or
5008 splitting described in above comment we should
5009 check that sregno and prev_sregno were
5010 inheritance/split pseudos created from the
5011 same original regno. */
5012 && (lra_reg_info[sregno].restore_regno
5013 == lra_reg_info[prev_sregno].restore_regno)
5014 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5016 lra_assert (GET_MODE (SET_SRC (prev_set))
5017 == GET_MODE (regno_reg_rtx[sregno]));
5018 if (GET_CODE (SET_SRC (set)) == SUBREG)
5019 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5020 else
5021 SET_SRC (set) = SET_SRC (prev_set);
5022 lra_push_insn_and_update_insn_regno_info (curr_insn);
5023 lra_set_used_insn_alternative_by_uid
5024 (INSN_UID (curr_insn), -1);
5025 done_p = true;
5026 if (lra_dump_file != NULL)
5028 fprintf (lra_dump_file, " Change reload insn:\n");
5029 dump_insn_slim (lra_dump_file, curr_insn);
5034 if (! done_p)
5036 struct lra_insn_reg *reg;
5037 bool restored_regs_p = false;
5038 bool kept_regs_p = false;
5040 curr_id = lra_get_insn_recog_data (curr_insn);
5041 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5043 regno = reg->regno;
5044 restore_regno = lra_reg_info[regno].restore_regno;
5045 if (restore_regno >= 0)
5047 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5049 substitute_pseudo (&curr_insn, regno,
5050 regno_reg_rtx[restore_regno]);
5051 restored_regs_p = true;
5053 else
5054 kept_regs_p = true;
5057 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5059 /* The instruction has changed since the previous
5060 constraints pass. */
5061 lra_push_insn_and_update_insn_regno_info (curr_insn);
5062 lra_set_used_insn_alternative_by_uid
5063 (INSN_UID (curr_insn), -1);
5065 else if (restored_regs_p)
5066 /* The instruction has been restored to the form that
5067 it had during the previous constraints pass. */
5068 lra_update_insn_regno_info (curr_insn);
5069 if (restored_regs_p && lra_dump_file != NULL)
5071 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5072 dump_insn_slim (lra_dump_file, curr_insn);
5077 return change_p;
5080 /* Entry function for undoing inheritance/split transformation. Return true
5081 if we did any RTL change in this pass. */
5082 bool
5083 lra_undo_inheritance (void)
5085 unsigned int regno;
5086 int restore_regno, hard_regno;
5087 int n_all_inherit, n_inherit, n_all_split, n_split;
5088 bitmap_head remove_pseudos;
5089 bitmap_iterator bi;
5090 bool change_p;
5092 lra_undo_inheritance_iter++;
5093 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5094 return false;
5095 if (lra_dump_file != NULL)
5096 fprintf (lra_dump_file,
5097 "\n********** Undoing inheritance #%d: **********\n\n",
5098 lra_undo_inheritance_iter);
5099 bitmap_initialize (&remove_pseudos, &reg_obstack);
5100 n_inherit = n_all_inherit = 0;
5101 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5102 if (lra_reg_info[regno].restore_regno >= 0)
5104 n_all_inherit++;
5105 if (reg_renumber[regno] < 0)
5106 bitmap_set_bit (&remove_pseudos, regno);
5107 else
5108 n_inherit++;
5110 if (lra_dump_file != NULL && n_all_inherit != 0)
5111 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
5112 n_inherit, n_all_inherit,
5113 (double) n_inherit / n_all_inherit * 100);
5114 n_split = n_all_split = 0;
5115 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5116 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
5118 n_all_split++;
5119 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
5120 ? reg_renumber[restore_regno] : restore_regno);
5121 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
5122 bitmap_set_bit (&remove_pseudos, regno);
5123 else
5125 n_split++;
5126 if (lra_dump_file != NULL)
5127 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
5128 regno, restore_regno);
5131 if (lra_dump_file != NULL && n_all_split != 0)
5132 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
5133 n_split, n_all_split,
5134 (double) n_split / n_all_split * 100);
5135 change_p = remove_inheritance_pseudos (&remove_pseudos);
5136 bitmap_clear (&remove_pseudos);
5137 /* Clear restore_regnos. */
5138 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5139 lra_reg_info[regno].restore_regno = -1;
5140 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5141 lra_reg_info[regno].restore_regno = -1;
5142 return change_p;