Make build_check_stmt accept an SSA_NAME for its base
[official-gcc.git] / gcc / lra-constraints.c
blobbcba59094606e2b617c15d511bc5daa2fe761c6d
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010, 2011, 2012
3 Free Software Foundation, Inc.
4 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file contains code for 3 passes: constraint pass,
24 inheritance/split pass, and pass for undoing failed inheritance and
25 split.
27 The major goal of constraint pass is to transform RTL to satisfy
28 insn and address constraints by:
29 o choosing insn alternatives;
30 o generating *reload insns* (or reloads in brief) and *reload
31 pseudos* which will get necessary hard registers later;
32 o substituting pseudos with equivalent values and removing the
33 instructions that initialized those pseudos.
35 The constraint pass has biggest and most complicated code in LRA.
36 There are a lot of important details like:
37 o reuse of input reload pseudos to simplify reload pseudo
38 allocations;
39 o some heuristics to choose insn alternative to improve the
40 inheritance;
41 o early clobbers etc.
43 The pass is mimicking former reload pass in alternative choosing
44 because the reload pass is oriented to current machine description
45 model. It might be changed if the machine description model is
46 changed.
48 There is special code for preventing all LRA and this pass cycling
49 in case of bugs.
51 On the first iteration of the pass we process every instruction and
52 choose an alternative for each one. On subsequent iterations we try
53 to avoid reprocessing instructions if we can be sure that the old
54 choice is still valid.
56 The inheritance/spilt pass is to transform code to achieve
57 ineheritance and live range splitting. It is done on backward
58 traversal of EBBs.
60 The inheritance optimization goal is to reuse values in hard
61 registers. There is analogous optimization in old reload pass. The
62 inheritance is achieved by following transformation:
64 reload_p1 <- p reload_p1 <- p
65 ... new_p <- reload_p1
66 ... => ...
67 reload_p2 <- p reload_p2 <- new_p
69 where p is spilled and not changed between the insns. Reload_p1 is
70 also called *original pseudo* and new_p is called *inheritance
71 pseudo*.
73 The subsequent assignment pass will try to assign the same (or
74 another if it is not possible) hard register to new_p as to
75 reload_p1 or reload_p2.
77 If the assignment pass fails to assign a hard register to new_p,
78 this file will undo the inheritance and restore the original code.
79 This is because implementing the above sequence with a spilled
80 new_p would make the code much worse. The inheritance is done in
81 EBB scope. The above is just a simplified example to get an idea
82 of the inheritance as the inheritance is also done for non-reload
83 insns.
85 Splitting (transformation) is also done in EBB scope on the same
86 pass as the inheritance:
88 r <- ... or ... <- r r <- ... or ... <- r
89 ... s <- r (new insn -- save)
90 ... =>
91 ... r <- s (new insn -- restore)
92 ... <- r ... <- r
94 The *split pseudo* s is assigned to the hard register of the
95 original pseudo or hard register r.
97 Splitting is done:
98 o In EBBs with high register pressure for global pseudos (living
99 in at least 2 BBs) and assigned to hard registers when there
100 are more one reloads needing the hard registers;
101 o for pseudos needing save/restore code around calls.
103 If the split pseudo still has the same hard register as the
104 original pseudo after the subsequent assignment pass or the
105 original pseudo was split, the opposite transformation is done on
106 the same pass for undoing inheritance. */
108 #undef REG_OK_STRICT
110 #include "config.h"
111 #include "system.h"
112 #include "coretypes.h"
113 #include "tm.h"
114 #include "hard-reg-set.h"
115 #include "rtl.h"
116 #include "tm_p.h"
117 #include "regs.h"
118 #include "insn-config.h"
119 #include "insn-codes.h"
120 #include "recog.h"
121 #include "output.h"
122 #include "addresses.h"
123 #include "target.h"
124 #include "function.h"
125 #include "expr.h"
126 #include "basic-block.h"
127 #include "except.h"
128 #include "optabs.h"
129 #include "df.h"
130 #include "ira.h"
131 #include "rtl-error.h"
132 #include "lra-int.h"
134 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
135 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
136 reload insns. */
137 static int bb_reload_num;
139 /* The current insn being processed and corresponding its data (basic
140 block, the insn data, the insn static data, and the mode of each
141 operand). */
142 static rtx curr_insn;
143 static basic_block curr_bb;
144 static lra_insn_recog_data_t curr_id;
145 static struct lra_static_insn_data *curr_static_id;
146 static enum machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
150 /* Start numbers for new registers and insns at the current constraints
151 pass start. */
152 static int new_regno_start;
153 static int new_insn_uid_start;
155 /* If LOC is nonnull, strip any outer subreg from it. */
156 static inline rtx *
157 strip_subreg (rtx *loc)
159 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
162 /* Return hard regno of REGNO or if it is was not assigned to a hard
163 register, use a hard register from its allocno class. */
164 static int
165 get_try_hard_regno (int regno)
167 int hard_regno;
168 enum reg_class rclass;
170 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
171 hard_regno = lra_get_regno_hard_regno (regno);
172 if (hard_regno >= 0)
173 return hard_regno;
174 rclass = lra_get_allocno_class (regno);
175 if (rclass == NO_REGS)
176 return -1;
177 return ira_class_hard_regs[rclass][0];
180 /* Return final hard regno (plus offset) which will be after
181 elimination. We do this for matching constraints because the final
182 hard regno could have a different class. */
183 static int
184 get_final_hard_regno (int hard_regno, int offset)
186 if (hard_regno < 0)
187 return hard_regno;
188 hard_regno = lra_get_elimination_hard_regno (hard_regno);
189 return hard_regno + offset;
192 /* Return hard regno of X after removing subreg and making
193 elimination. If X is not a register or subreg of register, return
194 -1. For pseudo use its assignment. */
195 static int
196 get_hard_regno (rtx x)
198 rtx reg;
199 int offset, hard_regno;
201 reg = x;
202 if (GET_CODE (x) == SUBREG)
203 reg = SUBREG_REG (x);
204 if (! REG_P (reg))
205 return -1;
206 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
207 hard_regno = lra_get_regno_hard_regno (hard_regno);
208 if (hard_regno < 0)
209 return -1;
210 offset = 0;
211 if (GET_CODE (x) == SUBREG)
212 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
213 SUBREG_BYTE (x), GET_MODE (x));
214 return get_final_hard_regno (hard_regno, offset);
217 /* If REGNO is a hard register or has been allocated a hard register,
218 return the class of that register. If REGNO is a reload pseudo
219 created by the current constraints pass, return its allocno class.
220 Return NO_REGS otherwise. */
221 static enum reg_class
222 get_reg_class (int regno)
224 int hard_regno;
226 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
227 hard_regno = lra_get_regno_hard_regno (regno);
228 if (hard_regno >= 0)
230 hard_regno = get_final_hard_regno (hard_regno, 0);
231 return REGNO_REG_CLASS (hard_regno);
233 if (regno >= new_regno_start)
234 return lra_get_allocno_class (regno);
235 return NO_REGS;
238 /* Return true if REG satisfies (or will satisfy) reg class constraint
239 CL. Use elimination first if REG is a hard register. If REG is a
240 reload pseudo created by this constraints pass, assume that it will
241 be allocated a hard register from its allocno class, but allow that
242 class to be narrowed to CL if it is currently a superset of CL.
244 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
245 REGNO (reg), or NO_REGS if no change in its class was needed. */
246 static bool
247 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
249 enum reg_class rclass, common_class;
250 enum machine_mode reg_mode;
251 int class_size, hard_regno, nregs, i, j;
252 int regno = REGNO (reg);
254 if (new_class != NULL)
255 *new_class = NO_REGS;
256 if (regno < FIRST_PSEUDO_REGISTER)
258 rtx final_reg = reg;
259 rtx *final_loc = &final_reg;
261 lra_eliminate_reg_if_possible (final_loc);
262 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
264 reg_mode = GET_MODE (reg);
265 rclass = get_reg_class (regno);
266 if (regno < new_regno_start
267 /* Do not allow the constraints for reload instructions to
268 influence the classes of new pseudos. These reloads are
269 typically moves that have many alternatives, and restricting
270 reload pseudos for one alternative may lead to situations
271 where other reload pseudos are no longer allocatable. */
272 || INSN_UID (curr_insn) >= new_insn_uid_start)
273 /* When we don't know what class will be used finally for reload
274 pseudos, we use ALL_REGS. */
275 return ((regno >= new_regno_start && rclass == ALL_REGS)
276 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
277 && ! hard_reg_set_subset_p (reg_class_contents[cl],
278 lra_no_alloc_regs)));
279 else
281 common_class = ira_reg_class_subset[rclass][cl];
282 if (new_class != NULL)
283 *new_class = common_class;
284 if (hard_reg_set_subset_p (reg_class_contents[common_class],
285 lra_no_alloc_regs))
286 return false;
287 /* Check that there are enough allocatable regs. */
288 class_size = ira_class_hard_regs_num[common_class];
289 for (i = 0; i < class_size; i++)
291 hard_regno = ira_class_hard_regs[common_class][i];
292 nregs = hard_regno_nregs[hard_regno][reg_mode];
293 if (nregs == 1)
294 return true;
295 for (j = 0; j < nregs; j++)
296 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j))
297 break;
298 if (j >= nregs)
299 return true;
301 return false;
305 /* Return true if REGNO satisfies a memory constraint. */
306 static bool
307 in_mem_p (int regno)
309 return get_reg_class (regno) == NO_REGS;
312 /* If we have decided to substitute X with another value, return that
313 value, otherwise return X. */
314 static rtx
315 get_equiv_substitution (rtx x)
317 int regno;
318 rtx res;
320 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
321 || ! ira_reg_equiv[regno].defined_p
322 || ! ira_reg_equiv[regno].profitable_p
323 || lra_get_regno_hard_regno (regno) >= 0)
324 return x;
325 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
326 return res;
327 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
328 return res;
329 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
330 return res;
331 gcc_unreachable ();
334 /* Set up curr_operand_mode. */
335 static void
336 init_curr_operand_mode (void)
338 int nop = curr_static_id->n_operands;
339 for (int i = 0; i < nop; i++)
341 enum machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
342 if (mode == VOIDmode)
344 /* The .md mode for address operands is the mode of the
345 addressed value rather than the mode of the address itself. */
346 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
347 mode = Pmode;
348 else
349 mode = curr_static_id->operand[i].mode;
351 curr_operand_mode[i] = mode;
357 /* The page contains code to reuse input reloads. */
359 /* Structure describes input reload of the current insns. */
360 struct input_reload
362 /* Reloaded value. */
363 rtx input;
364 /* Reload pseudo used. */
365 rtx reg;
368 /* The number of elements in the following array. */
369 static int curr_insn_input_reloads_num;
370 /* Array containing info about input reloads. It is used to find the
371 same input reload and reuse the reload pseudo in this case. */
372 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
374 /* Initiate data concerning reuse of input reloads for the current
375 insn. */
376 static void
377 init_curr_insn_input_reloads (void)
379 curr_insn_input_reloads_num = 0;
382 /* Change class of pseudo REGNO to NEW_CLASS. Print info about it
383 using TITLE. Output a new line if NL_P. */
384 static void
385 change_class (int regno, enum reg_class new_class,
386 const char *title, bool nl_p)
388 lra_assert (regno >= FIRST_PSEUDO_REGISTER);
389 if (lra_dump_file != NULL)
390 fprintf (lra_dump_file, "%s to class %s for r%d",
391 title, reg_class_names[new_class], regno);
392 setup_reg_classes (regno, new_class, NO_REGS, new_class);
393 if (lra_dump_file != NULL && nl_p)
394 fprintf (lra_dump_file, "\n");
397 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
398 created input reload pseudo (only if TYPE is not OP_OUT). The
399 result pseudo is returned through RESULT_REG. Return TRUE if we
400 created a new pseudo, FALSE if we reused the already created input
401 reload pseudo. Use TITLE to describe new registers for debug
402 purposes. */
403 static bool
404 get_reload_reg (enum op_type type, enum machine_mode mode, rtx original,
405 enum reg_class rclass, const char *title, rtx *result_reg)
407 int i, regno;
408 enum reg_class new_class;
410 if (type == OP_OUT)
412 *result_reg
413 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
414 return true;
416 for (i = 0; i < curr_insn_input_reloads_num; i++)
417 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
418 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
420 lra_assert (! side_effects_p (original));
421 *result_reg = curr_insn_input_reloads[i].reg;
422 regno = REGNO (*result_reg);
423 if (lra_dump_file != NULL)
425 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
426 print_value_slim (lra_dump_file, original, 1);
428 if (rclass != new_class)
429 change_class (regno, new_class, ", change", false);
430 if (lra_dump_file != NULL)
431 fprintf (lra_dump_file, "\n");
432 return false;
434 *result_reg = lra_create_new_reg (mode, original, rclass, title);
435 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
436 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
437 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
438 return true;
443 /* The page contains code to extract memory address parts. */
445 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
446 static inline bool
447 ok_for_index_p_nonstrict (rtx reg)
449 unsigned regno = REGNO (reg);
451 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
454 /* A version of regno_ok_for_base_p for use here, when all pseudos
455 should count as OK. Arguments as for regno_ok_for_base_p. */
456 static inline bool
457 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode, addr_space_t as,
458 enum rtx_code outer_code, enum rtx_code index_code)
460 unsigned regno = REGNO (reg);
462 if (regno >= FIRST_PSEUDO_REGISTER)
463 return true;
464 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
469 /* The page contains major code to choose the current insn alternative
470 and generate reloads for it. */
472 /* Return the offset from REGNO of the least significant register
473 in (reg:MODE REGNO).
475 This function is used to tell whether two registers satisfy
476 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
478 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
479 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
481 lra_constraint_offset (int regno, enum machine_mode mode)
483 lra_assert (regno < FIRST_PSEUDO_REGISTER);
484 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
485 && SCALAR_INT_MODE_P (mode))
486 return hard_regno_nregs[regno][mode] - 1;
487 return 0;
490 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
491 if they are the same hard reg, and has special hacks for
492 auto-increment and auto-decrement. This is specifically intended for
493 process_alt_operands to use in determining whether two operands
494 match. X is the operand whose number is the lower of the two.
496 It is supposed that X is the output operand and Y is the input
497 operand. Y_HARD_REGNO is the final hard regno of register Y or
498 register in subreg Y as we know it now. Otherwise, it is a
499 negative value. */
500 static bool
501 operands_match_p (rtx x, rtx y, int y_hard_regno)
503 int i;
504 RTX_CODE code = GET_CODE (x);
505 const char *fmt;
507 if (x == y)
508 return true;
509 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
510 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
512 int j;
514 i = get_hard_regno (x);
515 if (i < 0)
516 goto slow;
518 if ((j = y_hard_regno) < 0)
519 goto slow;
521 i += lra_constraint_offset (i, GET_MODE (x));
522 j += lra_constraint_offset (j, GET_MODE (y));
524 return i == j;
527 /* If two operands must match, because they are really a single
528 operand of an assembler insn, then two post-increments are invalid
529 because the assembler insn would increment only once. On the
530 other hand, a post-increment matches ordinary indexing if the
531 post-increment is the output operand. */
532 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
533 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
535 /* Two pre-increments are invalid because the assembler insn would
536 increment only once. On the other hand, a pre-increment matches
537 ordinary indexing if the pre-increment is the input operand. */
538 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
539 || GET_CODE (y) == PRE_MODIFY)
540 return operands_match_p (x, XEXP (y, 0), -1);
542 slow:
544 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
545 && x == SUBREG_REG (y))
546 return true;
547 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
548 && SUBREG_REG (x) == y)
549 return true;
551 /* Now we have disposed of all the cases in which different rtx
552 codes can match. */
553 if (code != GET_CODE (y))
554 return false;
556 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
557 if (GET_MODE (x) != GET_MODE (y))
558 return false;
560 switch (code)
562 CASE_CONST_UNIQUE:
563 return false;
565 case LABEL_REF:
566 return XEXP (x, 0) == XEXP (y, 0);
567 case SYMBOL_REF:
568 return XSTR (x, 0) == XSTR (y, 0);
570 default:
571 break;
574 /* Compare the elements. If any pair of corresponding elements fail
575 to match, return false for the whole things. */
577 fmt = GET_RTX_FORMAT (code);
578 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
580 int val, j;
581 switch (fmt[i])
583 case 'w':
584 if (XWINT (x, i) != XWINT (y, i))
585 return false;
586 break;
588 case 'i':
589 if (XINT (x, i) != XINT (y, i))
590 return false;
591 break;
593 case 'e':
594 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
595 if (val == 0)
596 return false;
597 break;
599 case '0':
600 break;
602 case 'E':
603 if (XVECLEN (x, i) != XVECLEN (y, i))
604 return false;
605 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
607 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
608 if (val == 0)
609 return false;
611 break;
613 /* It is believed that rtx's at this level will never
614 contain anything but integers and other rtx's, except for
615 within LABEL_REFs and SYMBOL_REFs. */
616 default:
617 gcc_unreachable ();
620 return true;
623 /* True if X is a constant that can be forced into the constant pool.
624 MODE is the mode of the operand, or VOIDmode if not known. */
625 #define CONST_POOL_OK_P(MODE, X) \
626 ((MODE) != VOIDmode \
627 && CONSTANT_P (X) \
628 && GET_CODE (X) != HIGH \
629 && !targetm.cannot_force_const_mem (MODE, X))
631 /* True if C is a non-empty register class that has too few registers
632 to be safely used as a reload target class. */
633 #define SMALL_REGISTER_CLASS_P(C) \
634 (reg_class_size [(C)] == 1 \
635 || (reg_class_size [(C)] >= 1 && targetm.class_likely_spilled_p (C)))
637 /* If REG is a reload pseudo, try to make its class satisfying CL. */
638 static void
639 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
641 enum reg_class rclass;
643 /* Do not make more accurate class from reloads generated. They are
644 mostly moves with a lot of constraints. Making more accurate
645 class may results in very narrow class and impossibility of find
646 registers for several reloads of one insn. */
647 if (INSN_UID (curr_insn) >= new_insn_uid_start)
648 return;
649 if (GET_CODE (reg) == SUBREG)
650 reg = SUBREG_REG (reg);
651 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
652 return;
653 if (in_class_p (reg, cl, &rclass) && rclass != cl)
654 change_class (REGNO (reg), rclass, " Change", true);
657 /* Generate reloads for matching OUT and INS (array of input operand
658 numbers with end marker -1) with reg class GOAL_CLASS. Add input
659 and output reloads correspondingly to the lists *BEFORE and
660 *AFTER. */
661 static void
662 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
663 rtx *before, rtx *after)
665 int i, in;
666 rtx new_in_reg, new_out_reg, reg, clobber;
667 enum machine_mode inmode, outmode;
668 rtx in_rtx = *curr_id->operand_loc[ins[0]];
669 rtx out_rtx = *curr_id->operand_loc[out];
671 outmode = curr_operand_mode[out];
672 inmode = curr_operand_mode[ins[0]];
673 push_to_sequence (*before);
674 if (inmode != outmode)
676 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
678 reg = new_in_reg
679 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
680 goal_class, "");
681 if (SCALAR_INT_MODE_P (inmode))
682 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
683 else
684 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
685 /* If the input reg is dying here, we can use the same hard
686 register for REG and IN_RTX. */
687 if (REG_P (in_rtx)
688 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
689 lra_reg_info[REGNO (reg)].val = lra_reg_info[REGNO (in_rtx)].val;
691 else
693 reg = new_out_reg
694 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
695 goal_class, "");
696 if (SCALAR_INT_MODE_P (outmode))
697 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
698 else
699 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
700 /* NEW_IN_REG is non-paradoxical subreg. We don't want
701 NEW_OUT_REG living above. We add clobber clause for
702 this. This is just a temporary clobber. We can remove
703 it at the end of LRA work. */
704 clobber = emit_clobber (new_out_reg);
705 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
706 if (GET_CODE (in_rtx) == SUBREG)
708 rtx subreg_reg = SUBREG_REG (in_rtx);
710 /* If SUBREG_REG is dying here and sub-registers IN_RTX
711 and NEW_IN_REG are similar, we can use the same hard
712 register for REG and SUBREG_REG. */
713 if (REG_P (subreg_reg) && GET_MODE (subreg_reg) == outmode
714 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
715 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
716 lra_reg_info[REGNO (reg)].val
717 = lra_reg_info[REGNO (subreg_reg)].val;
721 else
723 /* Pseudos have values -- see comments for lra_reg_info.
724 Different pseudos with the same value do not conflict even if
725 they live in the same place. When we create a pseudo we
726 assign value of original pseudo (if any) from which we
727 created the new pseudo. If we create the pseudo from the
728 input pseudo, the new pseudo will no conflict with the input
729 pseudo which is wrong when the input pseudo lives after the
730 insn and as the new pseudo value is changed by the insn
731 output. Therefore we create the new pseudo from the output.
733 We cannot reuse the current output register because we might
734 have a situation like "a <- a op b", where the constraints
735 force the second input operand ("b") to match the output
736 operand ("a"). "b" must then be copied into a new register
737 so that it doesn't clobber the current value of "a". */
739 new_in_reg = new_out_reg
740 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
741 goal_class, "");
743 /* In and out operand can be got from transformations before
744 processing insn constraints. One example of such transformations
745 is subreg reloading (see function simplify_operand_subreg). The
746 new pseudos created by the transformations might have inaccurate
747 class (ALL_REGS) and we should make their classes more
748 accurate. */
749 narrow_reload_pseudo_class (in_rtx, goal_class);
750 narrow_reload_pseudo_class (out_rtx, goal_class);
751 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
752 *before = get_insns ();
753 end_sequence ();
754 for (i = 0; (in = ins[i]) >= 0; i++)
756 lra_assert
757 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
758 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
759 *curr_id->operand_loc[in] = new_in_reg;
761 lra_update_dups (curr_id, ins);
762 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
764 start_sequence ();
765 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
766 emit_insn (*after);
767 *after = get_insns ();
768 end_sequence ();
770 *curr_id->operand_loc[out] = new_out_reg;
771 lra_update_dup (curr_id, out);
774 /* Return register class which is union of all reg classes in insn
775 constraint alternative string starting with P. */
776 static enum reg_class
777 reg_class_from_constraints (const char *p)
779 int c, len;
780 enum reg_class op_class = NO_REGS;
783 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
785 case '#':
786 case ',':
787 return op_class;
789 case 'p':
790 op_class = (reg_class_subunion
791 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
792 ADDRESS, SCRATCH)]);
793 break;
795 case 'g':
796 case 'r':
797 op_class = reg_class_subunion[op_class][GENERAL_REGS];
798 break;
800 default:
801 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
803 #ifdef EXTRA_CONSTRAINT_STR
804 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
805 op_class
806 = (reg_class_subunion
807 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
808 ADDRESS, SCRATCH)]);
809 #endif
810 break;
813 op_class
814 = reg_class_subunion[op_class][REG_CLASS_FROM_CONSTRAINT (c, p)];
815 break;
817 while ((p += len), c);
818 return op_class;
821 /* If OP is a register, return the class of the register as per
822 get_reg_class, otherwise return NO_REGS. */
823 static inline enum reg_class
824 get_op_class (rtx op)
826 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
829 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
830 otherwise. If modes of MEM_PSEUDO and VAL are different, use
831 SUBREG for VAL to make them equal. */
832 static rtx
833 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
835 if (GET_MODE (mem_pseudo) != GET_MODE (val))
836 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
837 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
839 return (to_p
840 ? gen_move_insn (mem_pseudo, val)
841 : gen_move_insn (val, mem_pseudo));
844 /* Process a special case insn (register move), return true if we
845 don't need to process it anymore. Return that RTL was changed
846 through CHANGE_P and macro SECONDARY_MEMORY_NEEDED says to use
847 secondary memory through SEC_MEM_P. */
848 static bool
849 check_and_process_move (bool *change_p, bool *sec_mem_p)
851 int sregno, dregno;
852 rtx set, dest, src, dreg, sreg, old_sreg, new_reg, before, scratch_reg;
853 enum reg_class dclass, sclass, secondary_class;
854 enum machine_mode sreg_mode;
855 secondary_reload_info sri;
857 *sec_mem_p = *change_p = false;
858 if ((set = single_set (curr_insn)) == NULL)
859 return false;
860 dreg = dest = SET_DEST (set);
861 sreg = src = SET_SRC (set);
862 /* Quick check on the right move insn which does not need
863 reloads. */
864 if ((dclass = get_op_class (dest)) != NO_REGS
865 && (sclass = get_op_class (src)) != NO_REGS
866 /* The backend guarantees that register moves of cost 2 never
867 need reloads. */
868 && targetm.register_move_cost (GET_MODE (src), dclass, sclass) == 2)
869 return true;
870 if (GET_CODE (dest) == SUBREG)
871 dreg = SUBREG_REG (dest);
872 if (GET_CODE (src) == SUBREG)
873 sreg = SUBREG_REG (src);
874 if (! REG_P (dreg) || ! REG_P (sreg))
875 return false;
876 sclass = dclass = NO_REGS;
877 dreg = get_equiv_substitution (dreg);
878 if (REG_P (dreg))
879 dclass = get_reg_class (REGNO (dreg));
880 if (dclass == ALL_REGS)
881 /* ALL_REGS is used for new pseudos created by transformations
882 like reload of SUBREG_REG (see function
883 simplify_operand_subreg). We don't know their class yet. We
884 should figure out the class from processing the insn
885 constraints not in this fast path function. Even if ALL_REGS
886 were a right class for the pseudo, secondary_... hooks usually
887 are not define for ALL_REGS. */
888 return false;
889 sreg_mode = GET_MODE (sreg);
890 old_sreg = sreg;
891 sreg = get_equiv_substitution (sreg);
892 if (REG_P (sreg))
893 sclass = get_reg_class (REGNO (sreg));
894 if (sclass == ALL_REGS)
895 /* See comments above. */
896 return false;
897 #ifdef SECONDARY_MEMORY_NEEDED
898 if (dclass != NO_REGS && sclass != NO_REGS
899 && SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src)))
901 *sec_mem_p = true;
902 return false;
904 #endif
905 sri.prev_sri = NULL;
906 sri.icode = CODE_FOR_nothing;
907 sri.extra_cost = 0;
908 secondary_class = NO_REGS;
909 /* Set up hard register for a reload pseudo for hook
910 secondary_reload because some targets just ignore unassigned
911 pseudos in the hook. */
912 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
914 dregno = REGNO (dreg);
915 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
917 else
918 dregno = -1;
919 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
921 sregno = REGNO (sreg);
922 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
924 else
925 sregno = -1;
926 if (sclass != NO_REGS)
927 secondary_class
928 = (enum reg_class) targetm.secondary_reload (false, dest,
929 (reg_class_t) sclass,
930 GET_MODE (src), &sri);
931 if (sclass == NO_REGS
932 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
933 && dclass != NO_REGS))
935 enum reg_class old_sclass = secondary_class;
936 secondary_reload_info old_sri = sri;
938 sri.prev_sri = NULL;
939 sri.icode = CODE_FOR_nothing;
940 sri.extra_cost = 0;
941 secondary_class
942 = (enum reg_class) targetm.secondary_reload (true, sreg,
943 (reg_class_t) dclass,
944 sreg_mode, &sri);
945 /* Check the target hook consistency. */
946 lra_assert
947 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
948 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
949 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
951 if (sregno >= 0)
952 reg_renumber [sregno] = -1;
953 if (dregno >= 0)
954 reg_renumber [dregno] = -1;
955 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
956 return false;
957 *change_p = true;
958 new_reg = NULL_RTX;
959 if (secondary_class != NO_REGS)
960 new_reg = lra_create_new_reg_with_unique_value (sreg_mode, NULL_RTX,
961 secondary_class,
962 "secondary");
963 start_sequence ();
964 if (old_sreg != sreg)
965 sreg = copy_rtx (sreg);
966 if (sri.icode == CODE_FOR_nothing)
967 lra_emit_move (new_reg, sreg);
968 else
970 enum reg_class scratch_class;
972 scratch_class = (reg_class_from_constraints
973 (insn_data[sri.icode].operand[2].constraint));
974 scratch_reg = (lra_create_new_reg_with_unique_value
975 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
976 scratch_class, "scratch"));
977 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
978 sreg, scratch_reg));
980 before = get_insns ();
981 end_sequence ();
982 lra_process_new_insns (curr_insn, before, NULL_RTX, "Inserting the move");
983 if (new_reg != NULL_RTX)
985 if (GET_CODE (src) == SUBREG)
986 SUBREG_REG (src) = new_reg;
987 else
988 SET_SRC (set) = new_reg;
990 else
992 if (lra_dump_file != NULL)
994 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
995 debug_rtl_slim (lra_dump_file, curr_insn, curr_insn, -1, 0);
997 lra_set_insn_deleted (curr_insn);
998 return true;
1000 return false;
1003 /* The following data describe the result of process_alt_operands.
1004 The data are used in curr_insn_transform to generate reloads. */
1006 /* The chosen reg classes which should be used for the corresponding
1007 operands. */
1008 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1009 /* True if the operand should be the same as another operand and that
1010 other operand does not need a reload. */
1011 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1012 /* True if the operand does not need a reload. */
1013 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1014 /* True if the operand can be offsetable memory. */
1015 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1016 /* The number of an operand to which given operand can be matched to. */
1017 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1018 /* The number of elements in the following array. */
1019 static int goal_alt_dont_inherit_ops_num;
1020 /* Numbers of operands whose reload pseudos should not be inherited. */
1021 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1022 /* True if the insn commutative operands should be swapped. */
1023 static bool goal_alt_swapped;
1024 /* The chosen insn alternative. */
1025 static int goal_alt_number;
1027 /* The following five variables are used to choose the best insn
1028 alternative. They reflect final characteristics of the best
1029 alternative. */
1031 /* Number of necessary reloads and overall cost reflecting the
1032 previous value and other unpleasantness of the best alternative. */
1033 static int best_losers, best_overall;
1034 /* Number of small register classes used for operands of the best
1035 alternative. */
1036 static int best_small_class_operands_num;
1037 /* Overall number hard registers used for reloads. For example, on
1038 some targets we need 2 general registers to reload DFmode and only
1039 one floating point register. */
1040 static int best_reload_nregs;
1041 /* Overall number reflecting distances of previous reloading the same
1042 value. The distances are counted from the current BB start. It is
1043 used to improve inheritance chances. */
1044 static int best_reload_sum;
1046 /* True if the current insn should have no correspondingly input or
1047 output reloads. */
1048 static bool no_input_reloads_p, no_output_reloads_p;
1050 /* True if we swapped the commutative operands in the current
1051 insn. */
1052 static int curr_swapped;
1054 /* Arrange for address element *LOC to be a register of class CL.
1055 Add any input reloads to list BEFORE. AFTER is nonnull if *LOC is an
1056 automodified value; handle that case by adding the required output
1057 reloads to list AFTER. Return true if the RTL was changed. */
1058 static bool
1059 process_addr_reg (rtx *loc, rtx *before, rtx *after, enum reg_class cl)
1061 int regno;
1062 enum reg_class rclass, new_class;
1063 rtx reg;
1064 rtx new_reg;
1065 enum machine_mode mode;
1066 bool before_p = false;
1068 loc = strip_subreg (loc);
1069 reg = *loc;
1070 mode = GET_MODE (reg);
1071 if (! REG_P (reg))
1073 /* Always reload memory in an address even if the target supports
1074 such addresses. */
1075 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1076 before_p = true;
1078 else
1080 regno = REGNO (reg);
1081 rclass = get_reg_class (regno);
1082 if ((*loc = get_equiv_substitution (reg)) != reg)
1084 if (lra_dump_file != NULL)
1086 fprintf (lra_dump_file,
1087 "Changing pseudo %d in address of insn %u on equiv ",
1088 REGNO (reg), INSN_UID (curr_insn));
1089 print_value_slim (lra_dump_file, *loc, 1);
1090 fprintf (lra_dump_file, "\n");
1092 *loc = copy_rtx (*loc);
1094 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1096 reg = *loc;
1097 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1098 mode, reg, cl, "address", &new_reg))
1099 before_p = true;
1101 else if (new_class != NO_REGS && rclass != new_class)
1103 change_class (regno, new_class, " Change", true);
1104 return false;
1106 else
1107 return false;
1109 if (before_p)
1111 push_to_sequence (*before);
1112 lra_emit_move (new_reg, reg);
1113 *before = get_insns ();
1114 end_sequence ();
1116 *loc = new_reg;
1117 if (after != NULL)
1119 start_sequence ();
1120 lra_emit_move (reg, new_reg);
1121 emit_insn (*after);
1122 *after = get_insns ();
1123 end_sequence ();
1125 return true;
1128 /* Make reloads for subreg in operand NOP with internal subreg mode
1129 REG_MODE, add new reloads for further processing. Return true if
1130 any reload was generated. */
1131 static bool
1132 simplify_operand_subreg (int nop, enum machine_mode reg_mode)
1134 int hard_regno;
1135 rtx before, after;
1136 enum machine_mode mode;
1137 rtx reg, new_reg;
1138 rtx operand = *curr_id->operand_loc[nop];
1140 before = after = NULL_RTX;
1142 if (GET_CODE (operand) != SUBREG)
1143 return false;
1145 mode = GET_MODE (operand);
1146 reg = SUBREG_REG (operand);
1147 /* If we change address for paradoxical subreg of memory, the
1148 address might violate the necessary alignment or the access might
1149 be slow. So take this into consideration. */
1150 if ((MEM_P (reg)
1151 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1152 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1153 || (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER))
1155 alter_subreg (curr_id->operand_loc[nop], false);
1156 return true;
1158 /* Put constant into memory when we have mixed modes. It generates
1159 a better code in most cases as it does not need a secondary
1160 reload memory. It also prevents LRA looping when LRA is using
1161 secondary reload memory again and again. */
1162 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1163 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1165 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1166 alter_subreg (curr_id->operand_loc[nop], false);
1167 return true;
1169 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1170 if there may be a problem accessing OPERAND in the outer
1171 mode. */
1172 if ((REG_P (reg)
1173 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1174 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1175 /* Don't reload paradoxical subregs because we could be looping
1176 having repeatedly final regno out of hard regs range. */
1177 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1178 >= hard_regno_nregs[hard_regno][mode])
1179 && simplify_subreg_regno (hard_regno, GET_MODE (reg),
1180 SUBREG_BYTE (operand), mode) < 0)
1181 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1183 enum op_type type = curr_static_id->operand[nop].type;
1184 /* The class will be defined later in curr_insn_transform. */
1185 enum reg_class rclass
1186 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1188 new_reg = lra_create_new_reg_with_unique_value (reg_mode, reg, rclass,
1189 "subreg reg");
1190 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (new_reg));
1191 if (type != OP_OUT
1192 || GET_MODE_SIZE (GET_MODE (reg)) > GET_MODE_SIZE (mode))
1194 push_to_sequence (before);
1195 lra_emit_move (new_reg, reg);
1196 before = get_insns ();
1197 end_sequence ();
1199 if (type != OP_IN)
1201 start_sequence ();
1202 lra_emit_move (reg, new_reg);
1203 emit_insn (after);
1204 after = get_insns ();
1205 end_sequence ();
1207 SUBREG_REG (operand) = new_reg;
1208 lra_process_new_insns (curr_insn, before, after,
1209 "Inserting subreg reload");
1210 return true;
1212 return false;
1215 /* Return TRUE if X refers for a hard register from SET. */
1216 static bool
1217 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1219 int i, j, x_hard_regno;
1220 enum machine_mode mode;
1221 const char *fmt;
1222 enum rtx_code code;
1224 if (x == NULL_RTX)
1225 return false;
1226 code = GET_CODE (x);
1227 mode = GET_MODE (x);
1228 if (code == SUBREG)
1230 x = SUBREG_REG (x);
1231 code = GET_CODE (x);
1232 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1233 mode = GET_MODE (x);
1236 if (REG_P (x))
1238 x_hard_regno = get_hard_regno (x);
1239 return (x_hard_regno >= 0
1240 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1242 if (MEM_P (x))
1244 struct address_info ad;
1246 decompose_mem_address (&ad, x);
1247 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1248 return true;
1249 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1250 return true;
1252 fmt = GET_RTX_FORMAT (code);
1253 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1255 if (fmt[i] == 'e')
1257 if (uses_hard_regs_p (XEXP (x, i), set))
1258 return true;
1260 else if (fmt[i] == 'E')
1262 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1263 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1264 return true;
1267 return false;
1270 /* Return true if OP is a spilled pseudo. */
1271 static inline bool
1272 spilled_pseudo_p (rtx op)
1274 return (REG_P (op)
1275 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1278 /* Return true if X is a general constant. */
1279 static inline bool
1280 general_constant_p (rtx x)
1282 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1285 /* Cost factor for each additional reload and maximal cost bound for
1286 insn reloads. One might ask about such strange numbers. Their
1287 values occurred historically from former reload pass. */
1288 #define LOSER_COST_FACTOR 6
1289 #define MAX_OVERALL_COST_BOUND 600
1291 /* Major function to choose the current insn alternative and what
1292 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1293 negative we should consider only this alternative. Return false if
1294 we can not choose the alternative or find how to reload the
1295 operands. */
1296 static bool
1297 process_alt_operands (int only_alternative)
1299 bool ok_p = false;
1300 int nop, small_class_operands_num, overall, nalt;
1301 int n_alternatives = curr_static_id->n_alternatives;
1302 int n_operands = curr_static_id->n_operands;
1303 /* LOSERS counts the operands that don't fit this alternative and
1304 would require loading. */
1305 int losers;
1306 /* REJECT is a count of how undesirable this alternative says it is
1307 if any reloading is required. If the alternative matches exactly
1308 then REJECT is ignored, but otherwise it gets this much counted
1309 against it in addition to the reloading needed. */
1310 int reject;
1311 /* The number of elements in the following array. */
1312 int early_clobbered_regs_num;
1313 /* Numbers of operands which are early clobber registers. */
1314 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1315 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1316 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1317 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1318 bool curr_alt_win[MAX_RECOG_OPERANDS];
1319 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1320 int curr_alt_matches[MAX_RECOG_OPERANDS];
1321 /* The number of elements in the following array. */
1322 int curr_alt_dont_inherit_ops_num;
1323 /* Numbers of operands whose reload pseudos should not be inherited. */
1324 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1325 rtx op;
1326 /* The register when the operand is a subreg of register, otherwise the
1327 operand itself. */
1328 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1329 /* The register if the operand is a register or subreg of register,
1330 otherwise NULL. */
1331 rtx operand_reg[MAX_RECOG_OPERANDS];
1332 int hard_regno[MAX_RECOG_OPERANDS];
1333 enum machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1334 int reload_nregs, reload_sum;
1335 bool costly_p;
1336 enum reg_class cl;
1338 /* Calculate some data common for all alternatives to speed up the
1339 function. */
1340 for (nop = 0; nop < n_operands; nop++)
1342 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1343 /* The real hard regno of the operand after the allocation. */
1344 hard_regno[nop] = get_hard_regno (op);
1346 operand_reg[nop] = op;
1347 biggest_mode[nop] = GET_MODE (operand_reg[nop]);
1348 if (GET_CODE (operand_reg[nop]) == SUBREG)
1350 operand_reg[nop] = SUBREG_REG (operand_reg[nop]);
1351 if (GET_MODE_SIZE (biggest_mode[nop])
1352 < GET_MODE_SIZE (GET_MODE (operand_reg[nop])))
1353 biggest_mode[nop] = GET_MODE (operand_reg[nop]);
1355 if (REG_P (operand_reg[nop]))
1356 no_subreg_reg_operand[nop] = operand_reg[nop];
1357 else
1358 operand_reg[nop] = NULL_RTX;
1361 /* The constraints are made of several alternatives. Each operand's
1362 constraint looks like foo,bar,... with commas separating the
1363 alternatives. The first alternatives for all operands go
1364 together, the second alternatives go together, etc.
1366 First loop over alternatives. */
1367 for (nalt = 0; nalt < n_alternatives; nalt++)
1369 /* Loop over operands for one constraint alternative. */
1370 #ifdef HAVE_ATTR_enabled
1371 if (curr_id->alternative_enabled_p != NULL
1372 && ! curr_id->alternative_enabled_p[nalt])
1373 continue;
1374 #endif
1376 if (only_alternative >= 0 && nalt != only_alternative)
1377 continue;
1379 overall = losers = reject = reload_nregs = reload_sum = 0;
1380 for (nop = 0; nop < n_operands; nop++)
1381 reject += (curr_static_id
1382 ->operand_alternative[nalt * n_operands + nop].reject);
1383 early_clobbered_regs_num = 0;
1385 for (nop = 0; nop < n_operands; nop++)
1387 const char *p;
1388 char *end;
1389 int len, c, m, i, opalt_num, this_alternative_matches;
1390 bool win, did_match, offmemok, early_clobber_p;
1391 /* false => this operand can be reloaded somehow for this
1392 alternative. */
1393 bool badop;
1394 /* true => this operand can be reloaded if the alternative
1395 allows regs. */
1396 bool winreg;
1397 /* True if a constant forced into memory would be OK for
1398 this operand. */
1399 bool constmemok;
1400 enum reg_class this_alternative, this_costly_alternative;
1401 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1402 bool this_alternative_match_win, this_alternative_win;
1403 bool this_alternative_offmemok;
1404 enum machine_mode mode;
1406 opalt_num = nalt * n_operands + nop;
1407 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1409 /* Fast track for no constraints at all. */
1410 curr_alt[nop] = NO_REGS;
1411 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1412 curr_alt_win[nop] = true;
1413 curr_alt_match_win[nop] = false;
1414 curr_alt_offmemok[nop] = false;
1415 curr_alt_matches[nop] = -1;
1416 continue;
1419 op = no_subreg_reg_operand[nop];
1420 mode = curr_operand_mode[nop];
1422 win = did_match = winreg = offmemok = constmemok = false;
1423 badop = true;
1425 early_clobber_p = false;
1426 p = curr_static_id->operand_alternative[opalt_num].constraint;
1428 this_costly_alternative = this_alternative = NO_REGS;
1429 /* We update set of possible hard regs besides its class
1430 because reg class might be inaccurate. For example,
1431 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1432 is translated in HI_REGS because classes are merged by
1433 pairs and there is no accurate intermediate class. */
1434 CLEAR_HARD_REG_SET (this_alternative_set);
1435 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1436 this_alternative_win = false;
1437 this_alternative_match_win = false;
1438 this_alternative_offmemok = false;
1439 this_alternative_matches = -1;
1441 /* An empty constraint should be excluded by the fast
1442 track. */
1443 lra_assert (*p != 0 && *p != ',');
1445 /* Scan this alternative's specs for this operand; set WIN
1446 if the operand fits any letter in this alternative.
1447 Otherwise, clear BADOP if this operand could fit some
1448 letter after reloads, or set WINREG if this operand could
1449 fit after reloads provided the constraint allows some
1450 registers. */
1451 costly_p = false;
1454 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1456 case '\0':
1457 len = 0;
1458 break;
1459 case ',':
1460 c = '\0';
1461 break;
1463 case '=': case '+': case '?': case '*': case '!':
1464 case ' ': case '\t':
1465 break;
1467 case '%':
1468 /* We only support one commutative marker, the first
1469 one. We already set commutative above. */
1470 break;
1472 case '&':
1473 early_clobber_p = true;
1474 break;
1476 case '#':
1477 /* Ignore rest of this alternative. */
1478 c = '\0';
1479 break;
1481 case '0': case '1': case '2': case '3': case '4':
1482 case '5': case '6': case '7': case '8': case '9':
1484 int m_hregno;
1485 bool match_p;
1487 m = strtoul (p, &end, 10);
1488 p = end;
1489 len = 0;
1490 lra_assert (nop > m);
1492 this_alternative_matches = m;
1493 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1494 /* We are supposed to match a previous operand.
1495 If we do, we win if that one did. If we do
1496 not, count both of the operands as losers.
1497 (This is too conservative, since most of the
1498 time only a single reload insn will be needed
1499 to make the two operands win. As a result,
1500 this alternative may be rejected when it is
1501 actually desirable.) */
1502 match_p = false;
1503 if (operands_match_p (*curr_id->operand_loc[nop],
1504 *curr_id->operand_loc[m], m_hregno))
1506 /* We should reject matching of an early
1507 clobber operand if the matching operand is
1508 not dying in the insn. */
1509 if (! curr_static_id->operand[m].early_clobber
1510 || operand_reg[nop] == NULL_RTX
1511 || (find_regno_note (curr_insn, REG_DEAD,
1512 REGNO (operand_reg[nop]))
1513 != NULL_RTX))
1514 match_p = true;
1516 if (match_p)
1518 /* If we are matching a non-offsettable
1519 address where an offsettable address was
1520 expected, then we must reject this
1521 combination, because we can't reload
1522 it. */
1523 if (curr_alt_offmemok[m]
1524 && MEM_P (*curr_id->operand_loc[m])
1525 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1526 continue;
1529 else
1531 /* Operands don't match. Both operands must
1532 allow a reload register, otherwise we
1533 cannot make them match. */
1534 if (curr_alt[m] == NO_REGS)
1535 break;
1536 /* Retroactively mark the operand we had to
1537 match as a loser, if it wasn't already and
1538 it wasn't matched to a register constraint
1539 (e.g it might be matched by memory). */
1540 if (curr_alt_win[m]
1541 && (operand_reg[m] == NULL_RTX
1542 || hard_regno[m] < 0))
1544 losers++;
1545 reload_nregs
1546 += (ira_reg_class_max_nregs[curr_alt[m]]
1547 [GET_MODE (*curr_id->operand_loc[m])]);
1550 /* We prefer no matching alternatives because
1551 it gives more freedom in RA. */
1552 if (operand_reg[nop] == NULL_RTX
1553 || (find_regno_note (curr_insn, REG_DEAD,
1554 REGNO (operand_reg[nop]))
1555 == NULL_RTX))
1556 reject += 2;
1558 /* If we have to reload this operand and some
1559 previous operand also had to match the same
1560 thing as this operand, we don't know how to do
1561 that. */
1562 if (!match_p || !curr_alt_win[m])
1564 for (i = 0; i < nop; i++)
1565 if (curr_alt_matches[i] == m)
1566 break;
1567 if (i < nop)
1568 break;
1570 else
1571 did_match = true;
1573 /* This can be fixed with reloads if the operand
1574 we are supposed to match can be fixed with
1575 reloads. */
1576 badop = false;
1577 this_alternative = curr_alt[m];
1578 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1579 break;
1582 case 'p':
1583 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1584 ADDRESS, SCRATCH);
1585 this_alternative = reg_class_subunion[this_alternative][cl];
1586 IOR_HARD_REG_SET (this_alternative_set,
1587 reg_class_contents[cl]);
1588 if (costly_p)
1590 this_costly_alternative
1591 = reg_class_subunion[this_costly_alternative][cl];
1592 IOR_HARD_REG_SET (this_costly_alternative_set,
1593 reg_class_contents[cl]);
1595 win = true;
1596 badop = false;
1597 break;
1599 case TARGET_MEM_CONSTRAINT:
1600 if (MEM_P (op) || spilled_pseudo_p (op))
1601 win = true;
1602 /* We can put constant or pseudo value into memory
1603 to satisfy the constraint. */
1604 if (CONST_POOL_OK_P (mode, op) || REG_P (op))
1605 badop = false;
1606 constmemok = true;
1607 break;
1609 case '<':
1610 if (MEM_P (op)
1611 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1612 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1613 win = true;
1614 break;
1616 case '>':
1617 if (MEM_P (op)
1618 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1619 || GET_CODE (XEXP (op, 0)) == POST_INC))
1620 win = true;
1621 break;
1623 /* Memory op whose address is not offsettable. */
1624 case 'V':
1625 if (MEM_P (op)
1626 && ! offsettable_nonstrict_memref_p (op))
1627 win = true;
1628 break;
1630 /* Memory operand whose address is offsettable. */
1631 case 'o':
1632 if ((MEM_P (op)
1633 && offsettable_nonstrict_memref_p (op))
1634 || spilled_pseudo_p (op))
1635 win = true;
1636 /* We can put constant or pseudo value into memory
1637 or make memory address offsetable to satisfy the
1638 constraint. */
1639 if (CONST_POOL_OK_P (mode, op) || MEM_P (op) || REG_P (op))
1640 badop = false;
1641 constmemok = true;
1642 offmemok = true;
1643 break;
1645 case 'E':
1646 case 'F':
1647 if (GET_CODE (op) == CONST_DOUBLE
1648 || (GET_CODE (op) == CONST_VECTOR
1649 && (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)))
1650 win = true;
1651 break;
1653 case 'G':
1654 case 'H':
1655 if (GET_CODE (op) == CONST_DOUBLE
1656 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1657 win = true;
1658 break;
1660 case 's':
1661 if (CONST_INT_P (op)
1662 || (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode))
1663 break;
1665 case 'i':
1666 if (general_constant_p (op))
1667 win = true;
1668 break;
1670 case 'n':
1671 if (CONST_INT_P (op)
1672 || (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode))
1673 win = true;
1674 break;
1676 case 'I':
1677 case 'J':
1678 case 'K':
1679 case 'L':
1680 case 'M':
1681 case 'N':
1682 case 'O':
1683 case 'P':
1684 if (CONST_INT_P (op)
1685 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1686 win = true;
1687 break;
1689 case 'X':
1690 /* This constraint should be excluded by the fast
1691 track. */
1692 gcc_unreachable ();
1693 break;
1695 case 'g':
1696 if (MEM_P (op)
1697 || general_constant_p (op)
1698 || spilled_pseudo_p (op))
1699 win = true;
1700 /* Drop through into 'r' case. */
1702 case 'r':
1703 this_alternative
1704 = reg_class_subunion[this_alternative][GENERAL_REGS];
1705 IOR_HARD_REG_SET (this_alternative_set,
1706 reg_class_contents[GENERAL_REGS]);
1707 if (costly_p)
1709 this_costly_alternative
1710 = (reg_class_subunion
1711 [this_costly_alternative][GENERAL_REGS]);
1712 IOR_HARD_REG_SET (this_costly_alternative_set,
1713 reg_class_contents[GENERAL_REGS]);
1715 goto reg;
1717 default:
1718 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
1720 #ifdef EXTRA_CONSTRAINT_STR
1721 if (EXTRA_MEMORY_CONSTRAINT (c, p))
1723 if (EXTRA_CONSTRAINT_STR (op, c, p))
1724 win = true;
1725 else if (spilled_pseudo_p (op))
1726 win = true;
1728 /* If we didn't already win, we can reload
1729 constants via force_const_mem or put the
1730 pseudo value into memory, or make other
1731 memory by reloading the address like for
1732 'o'. */
1733 if (CONST_POOL_OK_P (mode, op)
1734 || MEM_P (op) || REG_P (op))
1735 badop = false;
1736 constmemok = true;
1737 offmemok = true;
1738 break;
1740 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1742 if (EXTRA_CONSTRAINT_STR (op, c, p))
1743 win = true;
1745 /* If we didn't already win, we can reload
1746 the address into a base register. */
1747 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1748 ADDRESS, SCRATCH);
1749 this_alternative
1750 = reg_class_subunion[this_alternative][cl];
1751 IOR_HARD_REG_SET (this_alternative_set,
1752 reg_class_contents[cl]);
1753 if (costly_p)
1755 this_costly_alternative
1756 = (reg_class_subunion
1757 [this_costly_alternative][cl]);
1758 IOR_HARD_REG_SET (this_costly_alternative_set,
1759 reg_class_contents[cl]);
1761 badop = false;
1762 break;
1765 if (EXTRA_CONSTRAINT_STR (op, c, p))
1766 win = true;
1767 #endif
1768 break;
1771 cl = REG_CLASS_FROM_CONSTRAINT (c, p);
1772 this_alternative = reg_class_subunion[this_alternative][cl];
1773 IOR_HARD_REG_SET (this_alternative_set,
1774 reg_class_contents[cl]);
1775 if (costly_p)
1777 this_costly_alternative
1778 = reg_class_subunion[this_costly_alternative][cl];
1779 IOR_HARD_REG_SET (this_costly_alternative_set,
1780 reg_class_contents[cl]);
1782 reg:
1783 if (mode == BLKmode)
1784 break;
1785 winreg = true;
1786 if (REG_P (op))
1788 if (hard_regno[nop] >= 0
1789 && in_hard_reg_set_p (this_alternative_set,
1790 mode, hard_regno[nop]))
1791 win = true;
1792 else if (hard_regno[nop] < 0
1793 && in_class_p (op, this_alternative, NULL))
1794 win = true;
1796 break;
1798 if (c != ' ' && c != '\t')
1799 costly_p = c == '*';
1801 while ((p += len), c);
1803 /* Record which operands fit this alternative. */
1804 if (win)
1806 this_alternative_win = true;
1807 if (operand_reg[nop] != NULL_RTX)
1809 if (hard_regno[nop] >= 0)
1811 if (in_hard_reg_set_p (this_costly_alternative_set,
1812 mode, hard_regno[nop]))
1813 reject++;
1815 else
1817 /* Prefer won reg to spilled pseudo under other equal
1818 conditions. */
1819 reject++;
1820 if (in_class_p (operand_reg[nop],
1821 this_costly_alternative, NULL))
1822 reject++;
1824 /* We simulate the behaviour of old reload here.
1825 Although scratches need hard registers and it
1826 might result in spilling other pseudos, no reload
1827 insns are generated for the scratches. So it
1828 might cost something but probably less than old
1829 reload pass believes. */
1830 if (lra_former_scratch_p (REGNO (operand_reg[nop])))
1831 reject += LOSER_COST_FACTOR;
1834 else if (did_match)
1835 this_alternative_match_win = true;
1836 else
1838 int const_to_mem = 0;
1839 bool no_regs_p;
1841 no_regs_p
1842 = (this_alternative == NO_REGS
1843 || (hard_reg_set_subset_p
1844 (reg_class_contents[this_alternative],
1845 lra_no_alloc_regs)));
1846 /* If this operand accepts a register, and if the
1847 register class has at least one allocatable register,
1848 then this operand can be reloaded. */
1849 if (winreg && !no_regs_p)
1850 badop = false;
1852 if (badop)
1853 goto fail;
1855 this_alternative_offmemok = offmemok;
1856 if (this_costly_alternative != NO_REGS)
1857 reject++;
1858 /* If the operand is dying, has a matching constraint,
1859 and satisfies constraints of the matched operand
1860 which failed to satisfy the own constraints, we do
1861 not need to generate a reload insn for this
1862 operand. */
1863 if (!(this_alternative_matches >= 0
1864 && !curr_alt_win[this_alternative_matches]
1865 && REG_P (op)
1866 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
1867 && (hard_regno[nop] >= 0
1868 ? in_hard_reg_set_p (this_alternative_set,
1869 mode, hard_regno[nop])
1870 : in_class_p (op, this_alternative, NULL))))
1871 losers++;
1872 if (operand_reg[nop] != NULL_RTX
1873 /* Output operands and matched input operands are
1874 not inherited. The following conditions do not
1875 exactly describe the previous statement but they
1876 are pretty close. */
1877 && curr_static_id->operand[nop].type != OP_OUT
1878 && (this_alternative_matches < 0
1879 || curr_static_id->operand[nop].type != OP_IN))
1881 int last_reload = (lra_reg_info[ORIGINAL_REGNO
1882 (operand_reg[nop])]
1883 .last_reload);
1885 if (last_reload > bb_reload_num)
1886 reload_sum += last_reload - bb_reload_num;
1888 /* If this is a constant that is reloaded into the
1889 desired class by copying it to memory first, count
1890 that as another reload. This is consistent with
1891 other code and is required to avoid choosing another
1892 alternative when the constant is moved into memory.
1893 Note that the test here is precisely the same as in
1894 the code below that calls force_const_mem. */
1895 if (CONST_POOL_OK_P (mode, op)
1896 && ((targetm.preferred_reload_class
1897 (op, this_alternative) == NO_REGS)
1898 || no_input_reloads_p))
1900 const_to_mem = 1;
1901 if (! no_regs_p)
1902 losers++;
1905 /* Alternative loses if it requires a type of reload not
1906 permitted for this insn. We can always reload
1907 objects with a REG_UNUSED note. */
1908 if ((curr_static_id->operand[nop].type != OP_IN
1909 && no_output_reloads_p
1910 && ! find_reg_note (curr_insn, REG_UNUSED, op))
1911 || (curr_static_id->operand[nop].type != OP_OUT
1912 && no_input_reloads_p && ! const_to_mem))
1913 goto fail;
1915 /* If we can't reload this value at all, reject this
1916 alternative. Note that we could also lose due to
1917 LIMIT_RELOAD_CLASS, but we don't check that here. */
1918 if (! CONSTANT_P (op) && ! no_regs_p)
1920 if (targetm.preferred_reload_class
1921 (op, this_alternative) == NO_REGS)
1922 reject = MAX_OVERALL_COST_BOUND;
1924 if (curr_static_id->operand[nop].type == OP_OUT
1925 && (targetm.preferred_output_reload_class
1926 (op, this_alternative) == NO_REGS))
1927 reject = MAX_OVERALL_COST_BOUND;
1930 if (! ((const_to_mem && constmemok)
1931 || (MEM_P (op) && offmemok)))
1933 /* We prefer to reload pseudos over reloading other
1934 things, since such reloads may be able to be
1935 eliminated later. So bump REJECT in other cases.
1936 Don't do this in the case where we are forcing a
1937 constant into memory and it will then win since
1938 we don't want to have a different alternative
1939 match then. */
1940 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
1941 reject += 2;
1943 if (! no_regs_p)
1944 reload_nregs
1945 += ira_reg_class_max_nregs[this_alternative][mode];
1948 /* We are trying to spill pseudo into memory. It is
1949 usually more costly than moving to a hard register
1950 although it might takes the same number of
1951 reloads. */
1952 if (no_regs_p && REG_P (op))
1953 reject++;
1955 /* Input reloads can be inherited more often than output
1956 reloads can be removed, so penalize output
1957 reloads. */
1958 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
1959 reject++;
1962 if (early_clobber_p)
1963 reject++;
1964 /* ??? We check early clobbers after processing all operands
1965 (see loop below) and there we update the costs more.
1966 Should we update the cost (may be approximately) here
1967 because of early clobber register reloads or it is a rare
1968 or non-important thing to be worth to do it. */
1969 overall = losers * LOSER_COST_FACTOR + reject;
1970 if ((best_losers == 0 || losers != 0) && best_overall < overall)
1971 goto fail;
1973 curr_alt[nop] = this_alternative;
1974 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
1975 curr_alt_win[nop] = this_alternative_win;
1976 curr_alt_match_win[nop] = this_alternative_match_win;
1977 curr_alt_offmemok[nop] = this_alternative_offmemok;
1978 curr_alt_matches[nop] = this_alternative_matches;
1980 if (this_alternative_matches >= 0
1981 && !did_match && !this_alternative_win)
1982 curr_alt_win[this_alternative_matches] = false;
1984 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
1985 early_clobbered_nops[early_clobbered_regs_num++] = nop;
1987 ok_p = true;
1988 curr_alt_dont_inherit_ops_num = 0;
1989 for (nop = 0; nop < early_clobbered_regs_num; nop++)
1991 int i, j, clobbered_hard_regno;
1992 HARD_REG_SET temp_set;
1994 i = early_clobbered_nops[nop];
1995 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
1996 || hard_regno[i] < 0)
1997 continue;
1998 clobbered_hard_regno = hard_regno[i];
1999 CLEAR_HARD_REG_SET (temp_set);
2000 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2001 for (j = 0; j < n_operands; j++)
2002 if (j == i
2003 /* We don't want process insides of match_operator and
2004 match_parallel because otherwise we would process
2005 their operands once again generating a wrong
2006 code. */
2007 || curr_static_id->operand[j].is_operator)
2008 continue;
2009 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2010 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2011 continue;
2012 else if (uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2013 break;
2014 if (j >= n_operands)
2015 continue;
2016 /* We need to reload early clobbered register. */
2017 for (j = 0; j < n_operands; j++)
2018 if (curr_alt_matches[j] == i)
2020 curr_alt_match_win[j] = false;
2021 losers++;
2022 overall += LOSER_COST_FACTOR;
2024 if (! curr_alt_match_win[i])
2025 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2026 else
2028 /* Remember pseudos used for match reloads are never
2029 inherited. */
2030 lra_assert (curr_alt_matches[i] >= 0);
2031 curr_alt_win[curr_alt_matches[i]] = false;
2033 curr_alt_win[i] = curr_alt_match_win[i] = false;
2034 losers++;
2035 overall += LOSER_COST_FACTOR;
2037 small_class_operands_num = 0;
2038 for (nop = 0; nop < n_operands; nop++)
2039 small_class_operands_num
2040 += SMALL_REGISTER_CLASS_P (curr_alt[nop]) ? 1 : 0;
2042 /* If this alternative can be made to work by reloading, and it
2043 needs less reloading than the others checked so far, record
2044 it as the chosen goal for reloading. */
2045 if ((best_losers != 0 && losers == 0)
2046 || (((best_losers == 0 && losers == 0)
2047 || (best_losers != 0 && losers != 0))
2048 && (best_overall > overall
2049 || (best_overall == overall
2050 /* If the cost of the reloads is the same,
2051 prefer alternative which requires minimal
2052 number of small register classes for the
2053 operands. This improves chances of reloads
2054 for insn requiring small register
2055 classes. */
2056 && (small_class_operands_num
2057 < best_small_class_operands_num
2058 || (small_class_operands_num
2059 == best_small_class_operands_num
2060 && (reload_nregs < best_reload_nregs
2061 || (reload_nregs == best_reload_nregs
2062 && best_reload_sum < reload_sum))))))))
2064 for (nop = 0; nop < n_operands; nop++)
2066 goal_alt_win[nop] = curr_alt_win[nop];
2067 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2068 goal_alt_matches[nop] = curr_alt_matches[nop];
2069 goal_alt[nop] = curr_alt[nop];
2070 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2072 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2073 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2074 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2075 goal_alt_swapped = curr_swapped;
2076 best_overall = overall;
2077 best_losers = losers;
2078 best_small_class_operands_num = small_class_operands_num;
2079 best_reload_nregs = reload_nregs;
2080 best_reload_sum = reload_sum;
2081 goal_alt_number = nalt;
2083 if (losers == 0)
2084 /* Everything is satisfied. Do not process alternatives
2085 anymore. */
2086 break;
2087 fail:
2090 return ok_p;
2093 /* Return 1 if ADDR is a valid memory address for mode MODE in address
2094 space AS, and check that each pseudo has the proper kind of hard
2095 reg. */
2096 static int
2097 valid_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2098 rtx addr, addr_space_t as)
2100 #ifdef GO_IF_LEGITIMATE_ADDRESS
2101 lra_assert (ADDR_SPACE_GENERIC_P (as));
2102 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
2103 return 0;
2105 win:
2106 return 1;
2107 #else
2108 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
2109 #endif
2112 /* Return whether address AD is valid. */
2114 static bool
2115 valid_address_p (struct address_info *ad)
2117 /* Some ports do not check displacements for eliminable registers,
2118 so we replace them temporarily with the elimination target. */
2119 rtx saved_base_reg = NULL_RTX;
2120 rtx saved_index_reg = NULL_RTX;
2121 rtx *base_term = strip_subreg (ad->base_term);
2122 rtx *index_term = strip_subreg (ad->index_term);
2123 if (base_term != NULL)
2125 saved_base_reg = *base_term;
2126 lra_eliminate_reg_if_possible (base_term);
2127 if (ad->base_term2 != NULL)
2128 *ad->base_term2 = *ad->base_term;
2130 if (index_term != NULL)
2132 saved_index_reg = *index_term;
2133 lra_eliminate_reg_if_possible (index_term);
2135 bool ok_p = valid_address_p (ad->mode, *ad->outer, ad->as);
2136 if (saved_base_reg != NULL_RTX)
2138 *base_term = saved_base_reg;
2139 if (ad->base_term2 != NULL)
2140 *ad->base_term2 = *ad->base_term;
2142 if (saved_index_reg != NULL_RTX)
2143 *index_term = saved_index_reg;
2144 return ok_p;
2147 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2148 static rtx
2149 base_plus_disp_to_reg (struct address_info *ad)
2151 enum reg_class cl;
2152 rtx new_reg;
2154 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2155 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2156 get_index_code (ad));
2157 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2158 cl, "base + disp");
2159 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2160 return new_reg;
2163 /* Return true if we can add a displacement to address AD, even if that
2164 makes the address invalid. The fix-up code requires any new address
2165 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2166 static bool
2167 can_add_disp_p (struct address_info *ad)
2169 return (!ad->autoinc_p
2170 && ad->segment == NULL
2171 && ad->base == ad->base_term
2172 && ad->disp == ad->disp_term);
2175 /* Make equiv substitution in address AD. Return true if a substitution
2176 was made. */
2177 static bool
2178 equiv_address_substitution (struct address_info *ad)
2180 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2181 HOST_WIDE_INT disp, scale;
2182 bool change_p;
2184 base_term = strip_subreg (ad->base_term);
2185 if (base_term == NULL)
2186 base_reg = new_base_reg = NULL_RTX;
2187 else
2189 base_reg = *base_term;
2190 new_base_reg = get_equiv_substitution (base_reg);
2192 index_term = strip_subreg (ad->index_term);
2193 if (index_term == NULL)
2194 index_reg = new_index_reg = NULL_RTX;
2195 else
2197 index_reg = *index_term;
2198 new_index_reg = get_equiv_substitution (index_reg);
2200 if (base_reg == new_base_reg && index_reg == new_index_reg)
2201 return false;
2202 disp = 0;
2203 change_p = false;
2204 if (lra_dump_file != NULL)
2206 fprintf (lra_dump_file, "Changing address in insn %d ",
2207 INSN_UID (curr_insn));
2208 print_value_slim (lra_dump_file, *ad->outer, 1);
2210 if (base_reg != new_base_reg)
2212 if (REG_P (new_base_reg))
2214 *base_term = new_base_reg;
2215 change_p = true;
2217 else if (GET_CODE (new_base_reg) == PLUS
2218 && REG_P (XEXP (new_base_reg, 0))
2219 && CONST_INT_P (XEXP (new_base_reg, 1))
2220 && can_add_disp_p (ad))
2222 disp += INTVAL (XEXP (new_base_reg, 1));
2223 *base_term = XEXP (new_base_reg, 0);
2224 change_p = true;
2226 if (ad->base_term2 != NULL)
2227 *ad->base_term2 = *ad->base_term;
2229 if (index_reg != new_index_reg)
2231 if (REG_P (new_index_reg))
2233 *index_term = new_index_reg;
2234 change_p = true;
2236 else if (GET_CODE (new_index_reg) == PLUS
2237 && REG_P (XEXP (new_index_reg, 0))
2238 && CONST_INT_P (XEXP (new_index_reg, 1))
2239 && can_add_disp_p (ad)
2240 && (scale = get_index_scale (ad)))
2242 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2243 *index_term = XEXP (new_index_reg, 0);
2244 change_p = true;
2247 if (disp != 0)
2249 if (ad->disp != NULL)
2250 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2251 else
2253 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2254 update_address (ad);
2256 change_p = true;
2258 if (lra_dump_file != NULL)
2260 if (! change_p)
2261 fprintf (lra_dump_file, " -- no change\n");
2262 else
2264 fprintf (lra_dump_file, " on equiv ");
2265 print_value_slim (lra_dump_file, *ad->outer, 1);
2266 fprintf (lra_dump_file, "\n");
2269 return change_p;
2272 /* Major function to make reloads for an address in operand NOP.
2273 The supported cases are:
2275 1) an address that existed before LRA started, at which point it must
2276 have been valid. These addresses are subject to elimination and
2277 may have become invalid due to the elimination offset being out
2278 of range.
2280 2) an address created by forcing a constant to memory (force_const_to_mem).
2281 The initial form of these addresses might not be valid, and it is this
2282 function's job to make them valid.
2284 3) a frame address formed from a register and a (possibly zero)
2285 constant offset. As above, these addresses might not be valid
2286 and this function must make them so.
2288 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2289 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2290 address. Return true for any RTL change. */
2291 static bool
2292 process_address (int nop, rtx *before, rtx *after)
2294 struct address_info ad;
2295 rtx new_reg;
2296 rtx op = *curr_id->operand_loc[nop];
2297 const char *constraint = curr_static_id->operand[nop].constraint;
2298 bool change_p;
2300 if (constraint[0] == 'p'
2301 || EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint))
2302 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2303 else if (MEM_P (op))
2304 decompose_mem_address (&ad, op);
2305 else if (GET_CODE (op) == SUBREG
2306 && MEM_P (SUBREG_REG (op)))
2307 decompose_mem_address (&ad, SUBREG_REG (op));
2308 else
2309 return false;
2310 change_p = equiv_address_substitution (&ad);
2311 if (ad.base_term != NULL
2312 && (process_addr_reg
2313 (ad.base_term, before,
2314 (ad.autoinc_p
2315 && !(REG_P (*ad.base_term)
2316 && find_regno_note (curr_insn, REG_DEAD,
2317 REGNO (*ad.base_term)) != NULL_RTX)
2318 ? after : NULL),
2319 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2320 get_index_code (&ad)))))
2322 change_p = true;
2323 if (ad.base_term2 != NULL)
2324 *ad.base_term2 = *ad.base_term;
2326 if (ad.index_term != NULL
2327 && process_addr_reg (ad.index_term, before, NULL, INDEX_REG_CLASS))
2328 change_p = true;
2330 /* There are three cases where the shape of *AD.INNER may now be invalid:
2332 1) the original address was valid, but either elimination or
2333 equiv_address_substitution applied a displacement that made
2334 it invalid.
2336 2) the address is an invalid symbolic address created by
2337 force_const_to_mem.
2339 3) the address is a frame address with an invalid offset.
2341 All these cases involve a displacement and a non-autoinc address,
2342 so there is no point revalidating other types. */
2343 if (ad.disp == NULL || ad.autoinc_p || valid_address_p (&ad))
2344 return change_p;
2346 /* Any index existed before LRA started, so we can assume that the
2347 presence and shape of the index is valid. */
2348 push_to_sequence (*before);
2349 gcc_assert (ad.segment == NULL);
2350 gcc_assert (ad.disp == ad.disp_term);
2351 if (ad.base == NULL)
2353 if (ad.index == NULL)
2355 int code = -1;
2356 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2357 SCRATCH, SCRATCH);
2358 rtx disp = *ad.disp;
2360 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2361 #ifdef HAVE_lo_sum
2363 rtx insn;
2364 rtx last = get_last_insn ();
2366 /* disp => lo_sum (new_base, disp), case (2) above. */
2367 insn = emit_insn (gen_rtx_SET
2368 (VOIDmode, new_reg,
2369 gen_rtx_HIGH (Pmode, copy_rtx (disp))));
2370 code = recog_memoized (insn);
2371 if (code >= 0)
2373 *ad.disp = gen_rtx_LO_SUM (Pmode, new_reg, disp);
2374 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2376 *ad.disp = disp;
2377 code = -1;
2380 if (code < 0)
2381 delete_insns_since (last);
2383 #endif
2384 if (code < 0)
2386 /* disp => new_base, case (2) above. */
2387 lra_emit_move (new_reg, disp);
2388 *ad.disp = new_reg;
2391 else
2393 /* index * scale + disp => new base + index * scale,
2394 case (1) above. */
2395 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
2396 GET_CODE (*ad.index));
2398 lra_assert (INDEX_REG_CLASS != NO_REGS);
2399 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2400 lra_emit_move (new_reg, *ad.disp);
2401 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2402 new_reg, *ad.index);
2405 else if (ad.index == NULL)
2407 /* base + disp => new base, cases (1) and (3) above. */
2408 /* Another option would be to reload the displacement into an
2409 index register. However, postreload has code to optimize
2410 address reloads that have the same base and different
2411 displacements, so reloading into an index register would
2412 not necessarily be a win. */
2413 new_reg = base_plus_disp_to_reg (&ad);
2414 *ad.inner = new_reg;
2416 else
2418 /* base + scale * index + disp => new base + scale * index,
2419 case (1) above. */
2420 new_reg = base_plus_disp_to_reg (&ad);
2421 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2422 new_reg, *ad.index);
2424 *before = get_insns ();
2425 end_sequence ();
2426 return true;
2429 /* Emit insns to reload VALUE into a new register. VALUE is an
2430 auto-increment or auto-decrement RTX whose operand is a register or
2431 memory location; so reloading involves incrementing that location.
2432 IN is either identical to VALUE, or some cheaper place to reload
2433 value being incremented/decremented from.
2435 INC_AMOUNT is the number to increment or decrement by (always
2436 positive and ignored for POST_MODIFY/PRE_MODIFY).
2438 Return pseudo containing the result. */
2439 static rtx
2440 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
2442 /* REG or MEM to be copied and incremented. */
2443 rtx incloc = XEXP (value, 0);
2444 /* Nonzero if increment after copying. */
2445 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
2446 || GET_CODE (value) == POST_MODIFY);
2447 rtx last;
2448 rtx inc;
2449 rtx add_insn;
2450 int code;
2451 rtx real_in = in == value ? incloc : in;
2452 rtx result;
2453 bool plus_p = true;
2455 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
2457 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
2458 || GET_CODE (XEXP (value, 1)) == MINUS);
2459 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
2460 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
2461 inc = XEXP (XEXP (value, 1), 1);
2463 else
2465 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
2466 inc_amount = -inc_amount;
2468 inc = GEN_INT (inc_amount);
2471 if (! post && REG_P (incloc))
2472 result = incloc;
2473 else
2474 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
2475 "INC/DEC result");
2477 if (real_in != result)
2479 /* First copy the location to the result register. */
2480 lra_assert (REG_P (result));
2481 emit_insn (gen_move_insn (result, real_in));
2484 /* We suppose that there are insns to add/sub with the constant
2485 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
2486 old reload worked with this assumption. If the assumption
2487 becomes wrong, we should use approach in function
2488 base_plus_disp_to_reg. */
2489 if (in == value)
2491 /* See if we can directly increment INCLOC. */
2492 last = get_last_insn ();
2493 add_insn = emit_insn (plus_p
2494 ? gen_add2_insn (incloc, inc)
2495 : gen_sub2_insn (incloc, inc));
2497 code = recog_memoized (add_insn);
2498 if (code >= 0)
2500 if (! post && result != incloc)
2501 emit_insn (gen_move_insn (result, incloc));
2502 return result;
2504 delete_insns_since (last);
2507 /* If couldn't do the increment directly, must increment in RESULT.
2508 The way we do this depends on whether this is pre- or
2509 post-increment. For pre-increment, copy INCLOC to the reload
2510 register, increment it there, then save back. */
2511 if (! post)
2513 if (real_in != result)
2514 emit_insn (gen_move_insn (result, real_in));
2515 if (plus_p)
2516 emit_insn (gen_add2_insn (result, inc));
2517 else
2518 emit_insn (gen_sub2_insn (result, inc));
2519 if (result != incloc)
2520 emit_insn (gen_move_insn (incloc, result));
2522 else
2524 /* Post-increment.
2526 Because this might be a jump insn or a compare, and because
2527 RESULT may not be available after the insn in an input
2528 reload, we must do the incrementing before the insn being
2529 reloaded for.
2531 We have already copied IN to RESULT. Increment the copy in
2532 RESULT, save that back, then decrement RESULT so it has
2533 the original value. */
2534 if (plus_p)
2535 emit_insn (gen_add2_insn (result, inc));
2536 else
2537 emit_insn (gen_sub2_insn (result, inc));
2538 emit_insn (gen_move_insn (incloc, result));
2539 /* Restore non-modified value for the result. We prefer this
2540 way because it does not require an additional hard
2541 register. */
2542 if (plus_p)
2544 if (CONST_INT_P (inc))
2545 emit_insn (gen_add2_insn (result, GEN_INT (-INTVAL (inc))));
2546 else
2547 emit_insn (gen_sub2_insn (result, inc));
2549 else
2550 emit_insn (gen_add2_insn (result, inc));
2552 return result;
2555 /* Swap operands NOP and NOP + 1. */
2556 static inline void
2557 swap_operands (int nop)
2559 enum machine_mode mode = curr_operand_mode[nop];
2560 curr_operand_mode[nop] = curr_operand_mode[nop + 1];
2561 curr_operand_mode[nop + 1] = mode;
2562 rtx x = *curr_id->operand_loc[nop];
2563 *curr_id->operand_loc[nop] = *curr_id->operand_loc[nop + 1];
2564 *curr_id->operand_loc[nop + 1] = x;
2565 /* Swap the duplicates too. */
2566 lra_update_dup (curr_id, nop);
2567 lra_update_dup (curr_id, nop + 1);
2570 /* Main entry point of the constraint code: search the body of the
2571 current insn to choose the best alternative. It is mimicking insn
2572 alternative cost calculation model of former reload pass. That is
2573 because machine descriptions were written to use this model. This
2574 model can be changed in future. Make commutative operand exchange
2575 if it is chosen.
2577 Return true if some RTL changes happened during function call. */
2578 static bool
2579 curr_insn_transform (void)
2581 int i, j, k;
2582 int n_operands;
2583 int n_alternatives;
2584 int commutative;
2585 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
2586 rtx before, after;
2587 bool alt_p = false;
2588 /* Flag that the insn has been changed through a transformation. */
2589 bool change_p;
2590 bool sec_mem_p;
2591 #ifdef SECONDARY_MEMORY_NEEDED
2592 bool use_sec_mem_p;
2593 #endif
2594 int max_regno_before;
2595 int reused_alternative_num;
2597 no_input_reloads_p = no_output_reloads_p = false;
2598 goal_alt_number = -1;
2600 if (check_and_process_move (&change_p, &sec_mem_p))
2601 return change_p;
2603 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
2604 reloads; neither are insns that SET cc0. Insns that use CC0 are
2605 not allowed to have any input reloads. */
2606 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
2607 no_output_reloads_p = true;
2609 #ifdef HAVE_cc0
2610 if (reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
2611 no_input_reloads_p = true;
2612 if (reg_set_p (cc0_rtx, PATTERN (curr_insn)))
2613 no_output_reloads_p = true;
2614 #endif
2616 n_operands = curr_static_id->n_operands;
2617 n_alternatives = curr_static_id->n_alternatives;
2619 /* Just return "no reloads" if insn has no operands with
2620 constraints. */
2621 if (n_operands == 0 || n_alternatives == 0)
2622 return false;
2624 max_regno_before = max_reg_num ();
2626 for (i = 0; i < n_operands; i++)
2628 goal_alt_matched[i][0] = -1;
2629 goal_alt_matches[i] = -1;
2632 commutative = curr_static_id->commutative;
2634 /* Now see what we need for pseudos that didn't get hard regs or got
2635 the wrong kind of hard reg. For this, we must consider all the
2636 operands together against the register constraints. */
2638 best_losers = best_overall = MAX_RECOG_OPERANDS * 2 + MAX_OVERALL_COST_BOUND;
2639 best_small_class_operands_num = best_reload_sum = 0;
2641 curr_swapped = false;
2642 goal_alt_swapped = false;
2644 /* Make equivalence substitution and memory subreg elimination
2645 before address processing because an address legitimacy can
2646 depend on memory mode. */
2647 for (i = 0; i < n_operands; i++)
2649 rtx op = *curr_id->operand_loc[i];
2650 rtx subst, old = op;
2651 bool op_change_p = false;
2653 if (GET_CODE (old) == SUBREG)
2654 old = SUBREG_REG (old);
2655 subst = get_equiv_substitution (old);
2656 if (subst != old)
2658 subst = copy_rtx (subst);
2659 lra_assert (REG_P (old));
2660 if (GET_CODE (op) == SUBREG)
2661 SUBREG_REG (op) = subst;
2662 else
2663 *curr_id->operand_loc[i] = subst;
2664 if (lra_dump_file != NULL)
2666 fprintf (lra_dump_file,
2667 "Changing pseudo %d in operand %i of insn %u on equiv ",
2668 REGNO (old), i, INSN_UID (curr_insn));
2669 print_value_slim (lra_dump_file, subst, 1);
2670 fprintf (lra_dump_file, "\n");
2672 op_change_p = change_p = true;
2674 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
2676 change_p = true;
2677 lra_update_dup (curr_id, i);
2681 /* Reload address registers and displacements. We do it before
2682 finding an alternative because of memory constraints. */
2683 before = after = NULL_RTX;
2684 for (i = 0; i < n_operands; i++)
2685 if (! curr_static_id->operand[i].is_operator
2686 && process_address (i, &before, &after))
2688 change_p = true;
2689 lra_update_dup (curr_id, i);
2692 if (change_p)
2693 /* If we've changed the instruction then any alternative that
2694 we chose previously may no longer be valid. */
2695 lra_set_used_insn_alternative (curr_insn, -1);
2697 try_swapped:
2699 reused_alternative_num = curr_id->used_insn_alternative;
2700 if (lra_dump_file != NULL && reused_alternative_num >= 0)
2701 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
2702 reused_alternative_num, INSN_UID (curr_insn));
2704 if (process_alt_operands (reused_alternative_num))
2705 alt_p = true;
2707 /* If insn is commutative (it's safe to exchange a certain pair of
2708 operands) then we need to try each alternative twice, the second
2709 time matching those two operands as if we had exchanged them. To
2710 do this, really exchange them in operands.
2712 If we have just tried the alternatives the second time, return
2713 operands to normal and drop through. */
2715 if (reused_alternative_num < 0 && commutative >= 0)
2717 curr_swapped = !curr_swapped;
2718 if (curr_swapped)
2720 swap_operands (commutative);
2721 goto try_swapped;
2723 else
2724 swap_operands (commutative);
2727 /* The operands don't meet the constraints. goal_alt describes the
2728 alternative that we could reach by reloading the fewest operands.
2729 Reload so as to fit it. */
2731 if (! alt_p && ! sec_mem_p)
2733 /* No alternative works with reloads?? */
2734 if (INSN_CODE (curr_insn) >= 0)
2735 fatal_insn ("unable to generate reloads for:", curr_insn);
2736 error_for_asm (curr_insn,
2737 "inconsistent operand constraints in an %<asm%>");
2738 /* Avoid further trouble with this insn. */
2739 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
2740 lra_invalidate_insn_data (curr_insn);
2741 return true;
2744 /* If the best alternative is with operands 1 and 2 swapped, swap
2745 them. Update the operand numbers of any reloads already
2746 pushed. */
2748 if (goal_alt_swapped)
2750 if (lra_dump_file != NULL)
2751 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
2752 INSN_UID (curr_insn));
2754 /* Swap the duplicates too. */
2755 swap_operands (commutative);
2756 change_p = true;
2759 #ifdef SECONDARY_MEMORY_NEEDED
2760 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
2761 too conservatively. So we use the secondary memory only if there
2762 is no any alternative without reloads. */
2763 use_sec_mem_p = false;
2764 if (! alt_p)
2765 use_sec_mem_p = true;
2766 else if (sec_mem_p)
2768 for (i = 0; i < n_operands; i++)
2769 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
2770 break;
2771 use_sec_mem_p = i < n_operands;
2774 if (use_sec_mem_p)
2776 rtx new_reg, set, src, dest;
2777 enum machine_mode sec_mode;
2779 lra_assert (sec_mem_p);
2780 set = single_set (curr_insn);
2781 lra_assert (set != NULL_RTX && ! side_effects_p (set));
2782 dest = SET_DEST (set);
2783 src = SET_SRC (set);
2784 #ifdef SECONDARY_MEMORY_NEEDED_MODE
2785 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src));
2786 #else
2787 sec_mode = GET_MODE (src);
2788 #endif
2789 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
2790 NO_REGS, "secondary");
2791 /* If the mode is changed, it should be wider. */
2792 lra_assert (GET_MODE_SIZE (GET_MODE (new_reg))
2793 >= GET_MODE_SIZE (GET_MODE (src)));
2794 after = emit_spill_move (false, new_reg, dest);
2795 lra_process_new_insns (curr_insn, NULL_RTX, after,
2796 "Inserting the sec. move");
2797 before = emit_spill_move (true, new_reg, src);
2798 lra_process_new_insns (curr_insn, before, NULL_RTX, "Changing on");
2799 lra_set_insn_deleted (curr_insn);
2800 return true;
2802 #endif
2804 lra_assert (goal_alt_number >= 0);
2805 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
2807 if (lra_dump_file != NULL)
2809 const char *p;
2811 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
2812 goal_alt_number, INSN_UID (curr_insn));
2813 for (i = 0; i < n_operands; i++)
2815 p = (curr_static_id->operand_alternative
2816 [goal_alt_number * n_operands + i].constraint);
2817 if (*p == '\0')
2818 continue;
2819 fprintf (lra_dump_file, " (%d) ", i);
2820 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
2821 fputc (*p, lra_dump_file);
2823 fprintf (lra_dump_file, "\n");
2826 /* Right now, for any pair of operands I and J that are required to
2827 match, with J < I, goal_alt_matches[I] is J. Add I to
2828 goal_alt_matched[J]. */
2830 for (i = 0; i < n_operands; i++)
2831 if ((j = goal_alt_matches[i]) >= 0)
2833 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
2835 /* We allow matching one output operand and several input
2836 operands. */
2837 lra_assert (k == 0
2838 || (curr_static_id->operand[j].type == OP_OUT
2839 && curr_static_id->operand[i].type == OP_IN
2840 && (curr_static_id->operand
2841 [goal_alt_matched[j][0]].type == OP_IN)));
2842 goal_alt_matched[j][k] = i;
2843 goal_alt_matched[j][k + 1] = -1;
2846 for (i = 0; i < n_operands; i++)
2847 goal_alt_win[i] |= goal_alt_match_win[i];
2849 /* Any constants that aren't allowed and can't be reloaded into
2850 registers are here changed into memory references. */
2851 for (i = 0; i < n_operands; i++)
2852 if (goal_alt_win[i])
2854 int regno;
2855 enum reg_class new_class;
2856 rtx reg = *curr_id->operand_loc[i];
2858 if (GET_CODE (reg) == SUBREG)
2859 reg = SUBREG_REG (reg);
2861 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
2863 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
2865 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
2867 lra_assert (ok_p);
2868 change_class (regno, new_class, " Change", true);
2872 else
2874 const char *constraint;
2875 char c;
2876 rtx op = *curr_id->operand_loc[i];
2877 rtx subreg = NULL_RTX;
2878 enum machine_mode mode = curr_operand_mode[i];
2880 if (GET_CODE (op) == SUBREG)
2882 subreg = op;
2883 op = SUBREG_REG (op);
2884 mode = GET_MODE (op);
2887 if (CONST_POOL_OK_P (mode, op)
2888 && ((targetm.preferred_reload_class
2889 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
2890 || no_input_reloads_p))
2892 rtx tem = force_const_mem (mode, op);
2894 change_p = true;
2895 if (subreg != NULL_RTX)
2896 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
2898 *curr_id->operand_loc[i] = tem;
2899 lra_update_dup (curr_id, i);
2900 process_address (i, &before, &after);
2902 /* If the alternative accepts constant pool refs directly
2903 there will be no reload needed at all. */
2904 if (subreg != NULL_RTX)
2905 continue;
2906 /* Skip alternatives before the one requested. */
2907 constraint = (curr_static_id->operand_alternative
2908 [goal_alt_number * n_operands + i].constraint);
2909 for (;
2910 (c = *constraint) && c != ',' && c != '#';
2911 constraint += CONSTRAINT_LEN (c, constraint))
2913 if (c == TARGET_MEM_CONSTRAINT || c == 'o')
2914 break;
2915 #ifdef EXTRA_CONSTRAINT_STR
2916 if (EXTRA_MEMORY_CONSTRAINT (c, constraint)
2917 && EXTRA_CONSTRAINT_STR (tem, c, constraint))
2918 break;
2919 #endif
2921 if (c == '\0' || c == ',' || c == '#')
2922 continue;
2924 goal_alt_win[i] = true;
2928 for (i = 0; i < n_operands; i++)
2930 rtx old, new_reg;
2931 rtx op = *curr_id->operand_loc[i];
2933 if (goal_alt_win[i])
2935 if (goal_alt[i] == NO_REGS
2936 && REG_P (op)
2937 /* When we assign NO_REGS it means that we will not
2938 assign a hard register to the scratch pseudo by
2939 assigment pass and the scratch pseudo will be
2940 spilled. Spilled scratch pseudos are transformed
2941 back to scratches at the LRA end. */
2942 && lra_former_scratch_operand_p (curr_insn, i))
2943 change_class (REGNO (op), NO_REGS, " Change", true);
2944 continue;
2947 /* Operands that match previous ones have already been handled. */
2948 if (goal_alt_matches[i] >= 0)
2949 continue;
2951 /* We should not have an operand with a non-offsettable address
2952 appearing where an offsettable address will do. It also may
2953 be a case when the address should be special in other words
2954 not a general one (e.g. it needs no index reg). */
2955 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
2957 enum reg_class rclass;
2958 rtx *loc = &XEXP (op, 0);
2959 enum rtx_code code = GET_CODE (*loc);
2961 push_to_sequence (before);
2962 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
2963 MEM, SCRATCH);
2964 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
2965 new_reg = emit_inc (rclass, *loc, *loc,
2966 /* This value does not matter for MODIFY. */
2967 GET_MODE_SIZE (GET_MODE (op)));
2968 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass,
2969 "offsetable address", &new_reg))
2970 lra_emit_move (new_reg, *loc);
2971 before = get_insns ();
2972 end_sequence ();
2973 *loc = new_reg;
2974 lra_update_dup (curr_id, i);
2976 else if (goal_alt_matched[i][0] == -1)
2978 enum machine_mode mode;
2979 rtx reg, *loc;
2980 int hard_regno, byte;
2981 enum op_type type = curr_static_id->operand[i].type;
2983 loc = curr_id->operand_loc[i];
2984 mode = curr_operand_mode[i];
2985 if (GET_CODE (*loc) == SUBREG)
2987 reg = SUBREG_REG (*loc);
2988 byte = SUBREG_BYTE (*loc);
2989 if (REG_P (reg)
2990 /* Strict_low_part requires reload the register not
2991 the sub-register. */
2992 && (curr_static_id->operand[i].strict_low
2993 || (GET_MODE_SIZE (mode)
2994 <= GET_MODE_SIZE (GET_MODE (reg))
2995 && (hard_regno
2996 = get_try_hard_regno (REGNO (reg))) >= 0
2997 && (simplify_subreg_regno
2998 (hard_regno,
2999 GET_MODE (reg), byte, mode) < 0)
3000 && (goal_alt[i] == NO_REGS
3001 || (simplify_subreg_regno
3002 (ira_class_hard_regs[goal_alt[i]][0],
3003 GET_MODE (reg), byte, mode) >= 0)))))
3005 loc = &SUBREG_REG (*loc);
3006 mode = GET_MODE (*loc);
3009 old = *loc;
3010 if (get_reload_reg (type, mode, old, goal_alt[i], "", &new_reg)
3011 && type != OP_OUT)
3013 push_to_sequence (before);
3014 lra_emit_move (new_reg, old);
3015 before = get_insns ();
3016 end_sequence ();
3018 *loc = new_reg;
3019 if (type != OP_IN
3020 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3022 start_sequence ();
3023 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3024 emit_insn (after);
3025 after = get_insns ();
3026 end_sequence ();
3027 *loc = new_reg;
3029 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3030 if (goal_alt_dont_inherit_ops[j] == i)
3032 lra_set_regno_unique_value (REGNO (new_reg));
3033 break;
3035 lra_update_dup (curr_id, i);
3037 else if (curr_static_id->operand[i].type == OP_IN
3038 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3039 == OP_OUT))
3041 signed char arr[2];
3043 arr[0] = i;
3044 arr[1] = -1;
3045 match_reload (goal_alt_matched[i][0], arr,
3046 goal_alt[i], &before, &after);
3048 else if (curr_static_id->operand[i].type == OP_OUT
3049 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3050 == OP_IN))
3051 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3052 else
3053 /* We must generate code in any case when function
3054 process_alt_operands decides that it is possible. */
3055 gcc_unreachable ();
3057 if (before != NULL_RTX || after != NULL_RTX
3058 || max_regno_before != max_reg_num ())
3059 change_p = true;
3060 if (change_p)
3062 lra_update_operator_dups (curr_id);
3063 /* Something changes -- process the insn. */
3064 lra_update_insn_regno_info (curr_insn);
3066 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3067 return change_p;
3070 /* Return true if X is in LIST. */
3071 static bool
3072 in_list_p (rtx x, rtx list)
3074 for (; list != NULL_RTX; list = XEXP (list, 1))
3075 if (XEXP (list, 0) == x)
3076 return true;
3077 return false;
3080 /* Return true if X contains an allocatable hard register (if
3081 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3082 static bool
3083 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3085 int i, j;
3086 const char *fmt;
3087 enum rtx_code code;
3089 code = GET_CODE (x);
3090 if (REG_P (x))
3092 int regno = REGNO (x);
3093 HARD_REG_SET alloc_regs;
3095 if (hard_reg_p)
3097 if (regno >= FIRST_PSEUDO_REGISTER)
3098 regno = lra_get_regno_hard_regno (regno);
3099 if (regno < 0)
3100 return false;
3101 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3102 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3104 else
3106 if (regno < FIRST_PSEUDO_REGISTER)
3107 return false;
3108 if (! spilled_p)
3109 return true;
3110 return lra_get_regno_hard_regno (regno) < 0;
3113 fmt = GET_RTX_FORMAT (code);
3114 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3116 if (fmt[i] == 'e')
3118 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3119 return true;
3121 else if (fmt[i] == 'E')
3123 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3124 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3125 return true;
3128 return false;
3131 /* Process all regs in location *LOC and change them on equivalent
3132 substitution. Return true if any change was done. */
3133 static bool
3134 loc_equivalence_change_p (rtx *loc)
3136 rtx subst, reg, x = *loc;
3137 bool result = false;
3138 enum rtx_code code = GET_CODE (x);
3139 const char *fmt;
3140 int i, j;
3142 if (code == SUBREG)
3144 reg = SUBREG_REG (x);
3145 if ((subst = get_equiv_substitution (reg)) != reg
3146 && GET_MODE (subst) == VOIDmode)
3148 /* We cannot reload debug location. Simplify subreg here
3149 while we know the inner mode. */
3150 *loc = simplify_gen_subreg (GET_MODE (x), subst,
3151 GET_MODE (reg), SUBREG_BYTE (x));
3152 return true;
3155 if (code == REG && (subst = get_equiv_substitution (x)) != x)
3157 *loc = subst;
3158 return true;
3161 /* Scan all the operand sub-expressions. */
3162 fmt = GET_RTX_FORMAT (code);
3163 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3165 if (fmt[i] == 'e')
3166 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
3167 else if (fmt[i] == 'E')
3168 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3169 result
3170 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
3172 return result;
3175 /* Maximum allowed number of constraint pass iterations after the last
3176 spill pass. It is for preventing LRA cycling in a bug case. */
3177 #define MAX_CONSTRAINT_ITERATION_NUMBER 15
3179 /* Maximum number of generated reload insns per an insn. It is for
3180 preventing this pass cycling in a bug case. */
3181 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
3183 /* The current iteration number of this LRA pass. */
3184 int lra_constraint_iter;
3186 /* The current iteration number of this LRA pass after the last spill
3187 pass. */
3188 int lra_constraint_iter_after_spill;
3190 /* True if we substituted equiv which needs checking register
3191 allocation correctness because the equivalent value contains
3192 allocatable hard registers or when we restore multi-register
3193 pseudo. */
3194 bool lra_risky_transformations_p;
3196 /* Return true if REGNO is referenced in more than one block. */
3197 static bool
3198 multi_block_pseudo_p (int regno)
3200 basic_block bb = NULL;
3201 unsigned int uid;
3202 bitmap_iterator bi;
3204 if (regno < FIRST_PSEUDO_REGISTER)
3205 return false;
3207 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
3208 if (bb == NULL)
3209 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
3210 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
3211 return true;
3212 return false;
3215 /* Return true if X contains a pseudo dying in INSN. */
3216 static bool
3217 dead_pseudo_p (rtx x, rtx insn)
3219 int i, j;
3220 const char *fmt;
3221 enum rtx_code code;
3223 if (REG_P (x))
3224 return (insn != NULL_RTX
3225 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
3226 code = GET_CODE (x);
3227 fmt = GET_RTX_FORMAT (code);
3228 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3230 if (fmt[i] == 'e')
3232 if (dead_pseudo_p (XEXP (x, i), insn))
3233 return true;
3235 else if (fmt[i] == 'E')
3237 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3238 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
3239 return true;
3242 return false;
3245 /* Return true if INSN contains a dying pseudo in INSN right hand
3246 side. */
3247 static bool
3248 insn_rhs_dead_pseudo_p (rtx insn)
3250 rtx set = single_set (insn);
3252 gcc_assert (set != NULL);
3253 return dead_pseudo_p (SET_SRC (set), insn);
3256 /* Return true if any init insn of REGNO contains a dying pseudo in
3257 insn right hand side. */
3258 static bool
3259 init_insn_rhs_dead_pseudo_p (int regno)
3261 rtx insns = ira_reg_equiv[regno].init_insns;
3263 if (insns == NULL)
3264 return false;
3265 if (INSN_P (insns))
3266 return insn_rhs_dead_pseudo_p (insns);
3267 for (; insns != NULL_RTX; insns = XEXP (insns, 1))
3268 if (insn_rhs_dead_pseudo_p (XEXP (insns, 0)))
3269 return true;
3270 return false;
3273 /* Entry function of LRA constraint pass. Return true if the
3274 constraint pass did change the code. */
3275 bool
3276 lra_constraints (bool first_p)
3278 bool changed_p;
3279 int i, hard_regno, new_insns_num;
3280 unsigned int min_len, new_min_len, uid;
3281 rtx set, x, reg, dest_reg;
3282 basic_block last_bb;
3283 bitmap_head equiv_insn_bitmap;
3284 bitmap_iterator bi;
3286 lra_constraint_iter++;
3287 if (lra_dump_file != NULL)
3288 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
3289 lra_constraint_iter);
3290 lra_constraint_iter_after_spill++;
3291 if (lra_constraint_iter_after_spill > MAX_CONSTRAINT_ITERATION_NUMBER)
3292 internal_error
3293 ("Maximum number of LRA constraint passes is achieved (%d)\n",
3294 MAX_CONSTRAINT_ITERATION_NUMBER);
3295 changed_p = false;
3296 lra_risky_transformations_p = false;
3297 new_insn_uid_start = get_max_uid ();
3298 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
3299 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
3300 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3301 if (lra_reg_info[i].nrefs != 0)
3303 ira_reg_equiv[i].profitable_p = true;
3304 reg = regno_reg_rtx[i];
3305 if ((hard_regno = lra_get_regno_hard_regno (i)) >= 0)
3307 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
3309 for (j = 0; j < nregs; j++)
3310 df_set_regs_ever_live (hard_regno + j, true);
3312 else if ((x = get_equiv_substitution (reg)) != reg)
3314 bool pseudo_p = contains_reg_p (x, false, false);
3315 rtx set, insn;
3317 /* We don't use DF for compilation speed sake. So it is
3318 problematic to update live info when we use an
3319 equivalence containing pseudos in more than one BB. */
3320 if ((pseudo_p && multi_block_pseudo_p (i))
3321 /* If it is not a reverse equivalence, we check that a
3322 pseudo in rhs of the init insn is not dying in the
3323 insn. Otherwise, the live info at the beginning of
3324 the corresponding BB might be wrong after we
3325 removed the insn. When the equiv can be a
3326 constant, the right hand side of the init insn can
3327 be a pseudo. */
3328 || (! ((insn = ira_reg_equiv[i].init_insns) != NULL_RTX
3329 && INSN_P (insn)
3330 && (set = single_set (insn)) != NULL_RTX
3331 && REG_P (SET_DEST (set))
3332 && (int) REGNO (SET_DEST (set)) == i)
3333 && init_insn_rhs_dead_pseudo_p (i)))
3334 ira_reg_equiv[i].defined_p = false;
3335 else if (! first_p && pseudo_p)
3336 /* After RTL transformation, we can not guarantee that
3337 pseudo in the substitution was not reloaded which
3338 might make equivalence invalid. For example, in
3339 reverse equiv of p0
3341 p0 <- ...
3343 equiv_mem <- p0
3345 the memory address register was reloaded before the
3346 2nd insn. */
3347 ira_reg_equiv[i].defined_p = false;
3348 if (contains_reg_p (x, false, true))
3349 ira_reg_equiv[i].profitable_p = false;
3350 if (get_equiv_substitution (reg) != reg)
3351 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
3354 /* We should add all insns containing pseudos which should be
3355 substituted by their equivalences. */
3356 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
3357 lra_push_insn_by_uid (uid);
3358 lra_eliminate (false);
3359 min_len = lra_insn_stack_length ();
3360 new_insns_num = 0;
3361 last_bb = NULL;
3362 changed_p = false;
3363 while ((new_min_len = lra_insn_stack_length ()) != 0)
3365 curr_insn = lra_pop_insn ();
3366 --new_min_len;
3367 curr_bb = BLOCK_FOR_INSN (curr_insn);
3368 if (curr_bb != last_bb)
3370 last_bb = curr_bb;
3371 bb_reload_num = lra_curr_reload_num;
3373 if (min_len > new_min_len)
3375 min_len = new_min_len;
3376 new_insns_num = 0;
3378 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
3379 internal_error
3380 ("Max. number of generated reload insns per insn is achieved (%d)\n",
3381 MAX_RELOAD_INSNS_NUMBER);
3382 new_insns_num++;
3383 if (DEBUG_INSN_P (curr_insn))
3385 /* We need to check equivalence in debug insn and change
3386 pseudo to the equivalent value if necessary. */
3387 curr_id = lra_get_insn_recog_data (curr_insn);
3388 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
3389 && loc_equivalence_change_p (curr_id->operand_loc[0]))
3391 lra_update_insn_regno_info (curr_insn);
3392 changed_p = true;
3395 else if (INSN_P (curr_insn))
3397 if ((set = single_set (curr_insn)) != NULL_RTX)
3399 dest_reg = SET_DEST (set);
3400 /* The equivalence pseudo could be set up as SUBREG in a
3401 case when it is a call restore insn in a mode
3402 different from the pseudo mode. */
3403 if (GET_CODE (dest_reg) == SUBREG)
3404 dest_reg = SUBREG_REG (dest_reg);
3405 if ((REG_P (dest_reg)
3406 && (x = get_equiv_substitution (dest_reg)) != dest_reg
3407 /* Remove insns which set up a pseudo whose value
3408 can not be changed. Such insns might be not in
3409 init_insns because we don't update equiv data
3410 during insn transformations.
3412 As an example, let suppose that a pseudo got
3413 hard register and on the 1st pass was not
3414 changed to equivalent constant. We generate an
3415 additional insn setting up the pseudo because of
3416 secondary memory movement. Then the pseudo is
3417 spilled and we use the equiv constant. In this
3418 case we should remove the additional insn and
3419 this insn is not init_insns list. */
3420 && (! MEM_P (x) || MEM_READONLY_P (x)
3421 || in_list_p (curr_insn,
3422 ira_reg_equiv
3423 [REGNO (dest_reg)].init_insns)))
3424 || (((x = get_equiv_substitution (SET_SRC (set)))
3425 != SET_SRC (set))
3426 && in_list_p (curr_insn,
3427 ira_reg_equiv
3428 [REGNO (SET_SRC (set))].init_insns)))
3430 /* This is equiv init insn of pseudo which did not get a
3431 hard register -- remove the insn. */
3432 if (lra_dump_file != NULL)
3434 fprintf (lra_dump_file,
3435 " Removing equiv init insn %i (freq=%d)\n",
3436 INSN_UID (curr_insn),
3437 BLOCK_FOR_INSN (curr_insn)->frequency);
3438 debug_rtl_slim (lra_dump_file,
3439 curr_insn, curr_insn, -1, 0);
3441 if (contains_reg_p (x, true, false))
3442 lra_risky_transformations_p = true;
3443 lra_set_insn_deleted (curr_insn);
3444 continue;
3447 curr_id = lra_get_insn_recog_data (curr_insn);
3448 curr_static_id = curr_id->insn_static_data;
3449 init_curr_insn_input_reloads ();
3450 init_curr_operand_mode ();
3451 if (curr_insn_transform ())
3452 changed_p = true;
3453 /* Check non-transformed insns too for equiv change as USE
3454 or CLOBBER don't need reloads but can contain pseudos
3455 being changed on their equivalences. */
3456 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
3457 && loc_equivalence_change_p (&PATTERN (curr_insn)))
3459 lra_update_insn_regno_info (curr_insn);
3460 changed_p = true;
3464 bitmap_clear (&equiv_insn_bitmap);
3465 /* If we used a new hard regno, changed_p should be true because the
3466 hard reg is assigned to a new pseudo. */
3467 #ifdef ENABLE_CHECKING
3468 if (! changed_p)
3470 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3471 if (lra_reg_info[i].nrefs != 0
3472 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
3474 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
3476 for (j = 0; j < nregs; j++)
3477 lra_assert (df_regs_ever_live_p (hard_regno + j));
3480 #endif
3481 return changed_p;
3484 /* Initiate the LRA constraint pass. It is done once per
3485 function. */
3486 void
3487 lra_constraints_init (void)
3491 /* Finalize the LRA constraint pass. It is done once per
3492 function. */
3493 void
3494 lra_constraints_finish (void)
3500 /* This page contains code to do inheritance/split
3501 transformations. */
3503 /* Number of reloads passed so far in current EBB. */
3504 static int reloads_num;
3506 /* Number of calls passed so far in current EBB. */
3507 static int calls_num;
3509 /* Current reload pseudo check for validity of elements in
3510 USAGE_INSNS. */
3511 static int curr_usage_insns_check;
3513 /* Info about last usage of registers in EBB to do inheritance/split
3514 transformation. Inheritance transformation is done from a spilled
3515 pseudo and split transformations from a hard register or a pseudo
3516 assigned to a hard register. */
3517 struct usage_insns
3519 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
3520 value INSNS is valid. The insns is chain of optional debug insns
3521 and a finishing non-debug insn using the corresponding reg. */
3522 int check;
3523 /* Value of global reloads_num at the last insn in INSNS. */
3524 int reloads_num;
3525 /* Value of global reloads_nums at the last insn in INSNS. */
3526 int calls_num;
3527 /* It can be true only for splitting. And it means that the restore
3528 insn should be put after insn given by the following member. */
3529 bool after_p;
3530 /* Next insns in the current EBB which use the original reg and the
3531 original reg value is not changed between the current insn and
3532 the next insns. In order words, e.g. for inheritance, if we need
3533 to use the original reg value again in the next insns we can try
3534 to use the value in a hard register from a reload insn of the
3535 current insn. */
3536 rtx insns;
3539 /* Map: regno -> corresponding pseudo usage insns. */
3540 static struct usage_insns *usage_insns;
3542 static void
3543 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
3545 usage_insns[regno].check = curr_usage_insns_check;
3546 usage_insns[regno].insns = insn;
3547 usage_insns[regno].reloads_num = reloads_num;
3548 usage_insns[regno].calls_num = calls_num;
3549 usage_insns[regno].after_p = after_p;
3552 /* The function is used to form list REGNO usages which consists of
3553 optional debug insns finished by a non-debug insn using REGNO.
3554 RELOADS_NUM is current number of reload insns processed so far. */
3555 static void
3556 add_next_usage_insn (int regno, rtx insn, int reloads_num)
3558 rtx next_usage_insns;
3560 if (usage_insns[regno].check == curr_usage_insns_check
3561 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
3562 && DEBUG_INSN_P (insn))
3564 /* Check that we did not add the debug insn yet. */
3565 if (next_usage_insns != insn
3566 && (GET_CODE (next_usage_insns) != INSN_LIST
3567 || XEXP (next_usage_insns, 0) != insn))
3568 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
3569 next_usage_insns);
3571 else if (NONDEBUG_INSN_P (insn))
3572 setup_next_usage_insn (regno, insn, reloads_num, false);
3573 else
3574 usage_insns[regno].check = 0;
3577 /* Replace all references to register OLD_REGNO in *LOC with pseudo
3578 register NEW_REG. Return true if any change was made. */
3579 static bool
3580 substitute_pseudo (rtx *loc, int old_regno, rtx new_reg)
3582 rtx x = *loc;
3583 bool result = false;
3584 enum rtx_code code;
3585 const char *fmt;
3586 int i, j;
3588 if (x == NULL_RTX)
3589 return false;
3591 code = GET_CODE (x);
3592 if (code == REG && (int) REGNO (x) == old_regno)
3594 enum machine_mode mode = GET_MODE (*loc);
3595 enum machine_mode inner_mode = GET_MODE (new_reg);
3597 if (mode != inner_mode)
3599 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
3600 || ! SCALAR_INT_MODE_P (inner_mode))
3601 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
3602 else
3603 new_reg = gen_lowpart_SUBREG (mode, new_reg);
3605 *loc = new_reg;
3606 return true;
3609 /* Scan all the operand sub-expressions. */
3610 fmt = GET_RTX_FORMAT (code);
3611 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3613 if (fmt[i] == 'e')
3615 if (substitute_pseudo (&XEXP (x, i), old_regno, new_reg))
3616 result = true;
3618 else if (fmt[i] == 'E')
3620 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3621 if (substitute_pseudo (&XVECEXP (x, i, j), old_regno, new_reg))
3622 result = true;
3625 return result;
3628 /* Return first non-debug insn in list USAGE_INSNS. */
3629 static rtx
3630 skip_usage_debug_insns (rtx usage_insns)
3632 rtx insn;
3634 /* Skip debug insns. */
3635 for (insn = usage_insns;
3636 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
3637 insn = XEXP (insn, 1))
3639 return insn;
3642 /* Return true if we need secondary memory moves for insn in
3643 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
3644 into the insn. */
3645 static bool
3646 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
3647 rtx usage_insns ATTRIBUTE_UNUSED)
3649 #ifndef SECONDARY_MEMORY_NEEDED
3650 return false;
3651 #else
3652 rtx insn, set, dest;
3653 enum reg_class cl;
3655 if (inher_cl == ALL_REGS
3656 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
3657 return false;
3658 lra_assert (INSN_P (insn));
3659 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
3660 return false;
3661 dest = SET_DEST (set);
3662 if (! REG_P (dest))
3663 return false;
3664 lra_assert (inher_cl != NO_REGS);
3665 cl = get_reg_class (REGNO (dest));
3666 return (cl != NO_REGS && cl != ALL_REGS
3667 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
3668 #endif
3671 /* Registers involved in inheritance/split in the current EBB
3672 (inheritance/split pseudos and original registers). */
3673 static bitmap_head check_only_regs;
3675 /* Do inheritance transformations for insn INSN, which defines (if
3676 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
3677 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
3678 form as the "insns" field of usage_insns. Return true if we
3679 succeed in such transformation.
3681 The transformations look like:
3683 p <- ... i <- ...
3684 ... p <- i (new insn)
3685 ... =>
3686 <- ... p ... <- ... i ...
3688 ... i <- p (new insn)
3689 <- ... p ... <- ... i ...
3690 ... =>
3691 <- ... p ... <- ... i ...
3692 where p is a spilled original pseudo and i is a new inheritance pseudo.
3695 The inheritance pseudo has the smallest class of two classes CL and
3696 class of ORIGINAL REGNO. */
3697 static bool
3698 inherit_reload_reg (bool def_p, int original_regno,
3699 enum reg_class cl, rtx insn, rtx next_usage_insns)
3701 enum reg_class rclass = lra_get_allocno_class (original_regno);
3702 rtx original_reg = regno_reg_rtx[original_regno];
3703 rtx new_reg, new_insns, usage_insn;
3705 lra_assert (! usage_insns[original_regno].after_p);
3706 if (lra_dump_file != NULL)
3707 fprintf (lra_dump_file,
3708 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
3709 if (! ira_reg_classes_intersect_p[cl][rclass])
3711 if (lra_dump_file != NULL)
3713 fprintf (lra_dump_file,
3714 " Rejecting inheritance for %d "
3715 "because of disjoint classes %s and %s\n",
3716 original_regno, reg_class_names[cl],
3717 reg_class_names[rclass]);
3718 fprintf (lra_dump_file,
3719 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
3721 return false;
3723 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
3724 /* We don't use a subset of two classes because it can be
3725 NO_REGS. This transformation is still profitable in most
3726 cases even if the classes are not intersected as register
3727 move is probably cheaper than a memory load. */
3728 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
3730 if (lra_dump_file != NULL)
3731 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
3732 reg_class_names[cl], reg_class_names[rclass]);
3734 rclass = cl;
3736 if (check_secondary_memory_needed_p (cl, next_usage_insns))
3738 /* Reject inheritance resulting in secondary memory moves.
3739 Otherwise, there is a danger in LRA cycling. Also such
3740 transformation will be unprofitable. */
3741 if (lra_dump_file != NULL)
3743 rtx insn = skip_usage_debug_insns (next_usage_insns);
3744 rtx set = single_set (insn);
3746 lra_assert (set != NULL_RTX);
3748 rtx dest = SET_DEST (set);
3750 lra_assert (REG_P (dest));
3751 fprintf (lra_dump_file,
3752 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
3753 "as secondary mem is needed\n",
3754 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
3755 original_regno, reg_class_names[cl]);
3756 fprintf (lra_dump_file,
3757 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
3759 return false;
3761 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
3762 rclass, "inheritance");
3763 start_sequence ();
3764 if (def_p)
3765 emit_move_insn (original_reg, new_reg);
3766 else
3767 emit_move_insn (new_reg, original_reg);
3768 new_insns = get_insns ();
3769 end_sequence ();
3770 if (NEXT_INSN (new_insns) != NULL_RTX)
3772 if (lra_dump_file != NULL)
3774 fprintf (lra_dump_file,
3775 " Rejecting inheritance %d->%d "
3776 "as it results in 2 or more insns:\n",
3777 original_regno, REGNO (new_reg));
3778 debug_rtl_slim (lra_dump_file, new_insns, NULL_RTX, -1, 0);
3779 fprintf (lra_dump_file,
3780 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
3782 return false;
3784 substitute_pseudo (&insn, original_regno, new_reg);
3785 lra_update_insn_regno_info (insn);
3786 if (! def_p)
3787 /* We now have a new usage insn for original regno. */
3788 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
3789 if (lra_dump_file != NULL)
3790 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
3791 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
3792 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
3793 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
3794 bitmap_set_bit (&check_only_regs, original_regno);
3795 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
3796 if (def_p)
3797 lra_process_new_insns (insn, NULL_RTX, new_insns,
3798 "Add original<-inheritance");
3799 else
3800 lra_process_new_insns (insn, new_insns, NULL_RTX,
3801 "Add inheritance<-original");
3802 while (next_usage_insns != NULL_RTX)
3804 if (GET_CODE (next_usage_insns) != INSN_LIST)
3806 usage_insn = next_usage_insns;
3807 lra_assert (NONDEBUG_INSN_P (usage_insn));
3808 next_usage_insns = NULL;
3810 else
3812 usage_insn = XEXP (next_usage_insns, 0);
3813 lra_assert (DEBUG_INSN_P (usage_insn));
3814 next_usage_insns = XEXP (next_usage_insns, 1);
3816 substitute_pseudo (&usage_insn, original_regno, new_reg);
3817 lra_update_insn_regno_info (usage_insn);
3818 if (lra_dump_file != NULL)
3820 fprintf (lra_dump_file,
3821 " Inheritance reuse change %d->%d (bb%d):\n",
3822 original_regno, REGNO (new_reg),
3823 BLOCK_FOR_INSN (usage_insn)->index);
3824 debug_rtl_slim (lra_dump_file, usage_insn, usage_insn,
3825 -1, 0);
3828 if (lra_dump_file != NULL)
3829 fprintf (lra_dump_file,
3830 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
3831 return true;
3834 /* Return true if we need a caller save/restore for pseudo REGNO which
3835 was assigned to a hard register. */
3836 static inline bool
3837 need_for_call_save_p (int regno)
3839 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
3840 return (usage_insns[regno].calls_num < calls_num
3841 && (overlaps_hard_reg_set_p
3842 (call_used_reg_set,
3843 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])));
3846 /* Global registers occuring in the current EBB. */
3847 static bitmap_head ebb_global_regs;
3849 /* Return true if we need a split for hard register REGNO or pseudo
3850 REGNO which was assigned to a hard register.
3851 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
3852 used for reloads since the EBB end. It is an approximation of the
3853 used hard registers in the split range. The exact value would
3854 require expensive calculations. If we were aggressive with
3855 splitting because of the approximation, the split pseudo will save
3856 the same hard register assignment and will be removed in the undo
3857 pass. We still need the approximation because too aggressive
3858 splitting would result in too inaccurate cost calculation in the
3859 assignment pass because of too many generated moves which will be
3860 probably removed in the undo pass. */
3861 static inline bool
3862 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
3864 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
3866 lra_assert (hard_regno >= 0);
3867 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
3868 /* Don't split eliminable hard registers, otherwise we can
3869 split hard registers like hard frame pointer, which
3870 lives on BB start/end according to DF-infrastructure,
3871 when there is a pseudo assigned to the register and
3872 living in the same BB. */
3873 && (regno >= FIRST_PSEUDO_REGISTER
3874 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
3875 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
3876 /* We need at least 2 reloads to make pseudo splitting
3877 profitable. We should provide hard regno splitting in
3878 any case to solve 1st insn scheduling problem when
3879 moving hard register definition up might result in
3880 impossibility to find hard register for reload pseudo of
3881 small register class. */
3882 && (usage_insns[regno].reloads_num
3883 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 2) < reloads_num)
3884 && (regno < FIRST_PSEUDO_REGISTER
3885 /* For short living pseudos, spilling + inheritance can
3886 be considered a substitution for splitting.
3887 Therefore we do not splitting for local pseudos. It
3888 decreases also aggressiveness of splitting. The
3889 minimal number of references is chosen taking into
3890 account that for 2 references splitting has no sense
3891 as we can just spill the pseudo. */
3892 || (regno >= FIRST_PSEUDO_REGISTER
3893 && lra_reg_info[regno].nrefs > 3
3894 && bitmap_bit_p (&ebb_global_regs, regno))))
3895 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
3898 /* Return class for the split pseudo created from original pseudo with
3899 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
3900 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
3901 results in no secondary memory movements. */
3902 static enum reg_class
3903 choose_split_class (enum reg_class allocno_class,
3904 int hard_regno ATTRIBUTE_UNUSED,
3905 enum machine_mode mode ATTRIBUTE_UNUSED)
3907 #ifndef SECONDARY_MEMORY_NEEDED
3908 return allocno_class;
3909 #else
3910 int i;
3911 enum reg_class cl, best_cl = NO_REGS;
3912 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
3913 = REGNO_REG_CLASS (hard_regno);
3915 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
3916 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
3917 return allocno_class;
3918 for (i = 0;
3919 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
3920 i++)
3921 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
3922 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
3923 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
3924 && (best_cl == NO_REGS
3925 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
3926 best_cl = cl;
3927 return best_cl;
3928 #endif
3931 /* Do split transformations for insn INSN, which defines or uses
3932 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
3933 the EBB next uses ORIGINAL_REGNO; it has the same form as the
3934 "insns" field of usage_insns.
3936 The transformations look like:
3938 p <- ... p <- ...
3939 ... s <- p (new insn -- save)
3940 ... =>
3941 ... p <- s (new insn -- restore)
3942 <- ... p ... <- ... p ...
3944 <- ... p ... <- ... p ...
3945 ... s <- p (new insn -- save)
3946 ... =>
3947 ... p <- s (new insn -- restore)
3948 <- ... p ... <- ... p ...
3950 where p is an original pseudo got a hard register or a hard
3951 register and s is a new split pseudo. The save is put before INSN
3952 if BEFORE_P is true. Return true if we succeed in such
3953 transformation. */
3954 static bool
3955 split_reg (bool before_p, int original_regno, rtx insn, rtx next_usage_insns)
3957 enum reg_class rclass;
3958 rtx original_reg;
3959 int hard_regno;
3960 rtx new_reg, save, restore, usage_insn;
3961 bool after_p;
3962 bool call_save_p;
3964 if (original_regno < FIRST_PSEUDO_REGISTER)
3966 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
3967 hard_regno = original_regno;
3968 call_save_p = false;
3970 else
3972 hard_regno = reg_renumber[original_regno];
3973 rclass = lra_get_allocno_class (original_regno);
3974 original_reg = regno_reg_rtx[original_regno];
3975 call_save_p = need_for_call_save_p (original_regno);
3977 original_reg = regno_reg_rtx[original_regno];
3978 lra_assert (hard_regno >= 0);
3979 if (lra_dump_file != NULL)
3980 fprintf (lra_dump_file,
3981 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
3982 if (call_save_p)
3984 enum machine_mode sec_mode;
3986 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3987 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (original_reg));
3988 #else
3989 sec_mode = GET_MODE (original_reg);
3990 #endif
3991 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3992 NO_REGS, "save");
3994 else
3996 rclass = choose_split_class (rclass, hard_regno,
3997 GET_MODE (original_reg));
3998 if (rclass == NO_REGS)
4000 if (lra_dump_file != NULL)
4002 fprintf (lra_dump_file,
4003 " Rejecting split of %d(%s): "
4004 "no good reg class for %d(%s)\n",
4005 original_regno,
4006 reg_class_names[lra_get_allocno_class (original_regno)],
4007 hard_regno,
4008 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4009 fprintf
4010 (lra_dump_file,
4011 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4013 return false;
4015 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4016 rclass, "split");
4017 reg_renumber[REGNO (new_reg)] = hard_regno;
4019 save = emit_spill_move (true, new_reg, original_reg);
4020 if (NEXT_INSN (save) != NULL_RTX)
4022 lra_assert (! call_save_p);
4023 if (lra_dump_file != NULL)
4025 fprintf
4026 (lra_dump_file,
4027 " Rejecting split %d->%d resulting in > 2 %s save insns:\n",
4028 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4029 debug_rtl_slim (lra_dump_file, save, NULL_RTX, -1, 0);
4030 fprintf (lra_dump_file,
4031 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4033 return false;
4035 restore = emit_spill_move (false, new_reg, original_reg);
4036 if (NEXT_INSN (restore) != NULL_RTX)
4038 lra_assert (! call_save_p);
4039 if (lra_dump_file != NULL)
4041 fprintf (lra_dump_file,
4042 " Rejecting split %d->%d "
4043 "resulting in > 2 %s restore insns:\n",
4044 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4045 debug_rtl_slim (lra_dump_file, restore, NULL_RTX, -1, 0);
4046 fprintf (lra_dump_file,
4047 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4049 return false;
4051 after_p = usage_insns[original_regno].after_p;
4052 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4053 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4054 bitmap_set_bit (&check_only_regs, original_regno);
4055 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4056 for (;;)
4058 if (GET_CODE (next_usage_insns) != INSN_LIST)
4060 usage_insn = next_usage_insns;
4061 break;
4063 usage_insn = XEXP (next_usage_insns, 0);
4064 lra_assert (DEBUG_INSN_P (usage_insn));
4065 next_usage_insns = XEXP (next_usage_insns, 1);
4066 substitute_pseudo (&usage_insn, original_regno, new_reg);
4067 lra_update_insn_regno_info (usage_insn);
4068 if (lra_dump_file != NULL)
4070 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
4071 original_regno, REGNO (new_reg));
4072 debug_rtl_slim (lra_dump_file, usage_insn, usage_insn,
4073 -1, 0);
4076 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
4077 lra_assert (usage_insn != insn || (after_p && before_p));
4078 lra_process_new_insns (usage_insn, after_p ? NULL_RTX : restore,
4079 after_p ? restore : NULL_RTX,
4080 call_save_p
4081 ? "Add reg<-save" : "Add reg<-split");
4082 lra_process_new_insns (insn, before_p ? save : NULL_RTX,
4083 before_p ? NULL_RTX : save,
4084 call_save_p
4085 ? "Add save<-reg" : "Add split<-reg");
4086 if (lra_dump_file != NULL)
4087 fprintf (lra_dump_file,
4088 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4089 return true;
4092 /* Recognize that we need a split transformation for insn INSN, which
4093 defines or uses REGNO in its insn biggest MODE (we use it only if
4094 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
4095 hard registers which might be used for reloads since the EBB end.
4096 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
4097 uid before starting INSN processing. Return true if we succeed in
4098 such transformation. */
4099 static bool
4100 split_if_necessary (int regno, enum machine_mode mode,
4101 HARD_REG_SET potential_reload_hard_regs,
4102 bool before_p, rtx insn, int max_uid)
4104 bool res = false;
4105 int i, nregs = 1;
4106 rtx next_usage_insns;
4108 if (regno < FIRST_PSEUDO_REGISTER)
4109 nregs = hard_regno_nregs[regno][mode];
4110 for (i = 0; i < nregs; i++)
4111 if (usage_insns[regno + i].check == curr_usage_insns_check
4112 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
4113 /* To avoid processing the register twice or more. */
4114 && ((GET_CODE (next_usage_insns) != INSN_LIST
4115 && INSN_UID (next_usage_insns) < max_uid)
4116 || (GET_CODE (next_usage_insns) == INSN_LIST
4117 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
4118 && need_for_split_p (potential_reload_hard_regs, regno + i)
4119 && split_reg (before_p, regno + i, insn, next_usage_insns))
4120 res = true;
4121 return res;
4124 /* Check only registers living at the current program point in the
4125 current EBB. */
4126 static bitmap_head live_regs;
4128 /* Update live info in EBB given by its HEAD and TAIL insns after
4129 inheritance/split transformation. The function removes dead moves
4130 too. */
4131 static void
4132 update_ebb_live_info (rtx head, rtx tail)
4134 unsigned int j;
4135 int regno;
4136 bool live_p;
4137 rtx prev_insn, set;
4138 bool remove_p;
4139 basic_block last_bb, prev_bb, curr_bb;
4140 bitmap_iterator bi;
4141 struct lra_insn_reg *reg;
4142 edge e;
4143 edge_iterator ei;
4145 last_bb = BLOCK_FOR_INSN (tail);
4146 prev_bb = NULL;
4147 for (curr_insn = tail;
4148 curr_insn != PREV_INSN (head);
4149 curr_insn = prev_insn)
4151 prev_insn = PREV_INSN (curr_insn);
4152 /* We need to process empty blocks too. They contain
4153 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
4154 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
4155 continue;
4156 curr_bb = BLOCK_FOR_INSN (curr_insn);
4157 if (curr_bb != prev_bb)
4159 if (prev_bb != NULL)
4161 /* Update df_get_live_in (prev_bb): */
4162 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4163 if (bitmap_bit_p (&live_regs, j))
4164 bitmap_set_bit (df_get_live_in (prev_bb), j);
4165 else
4166 bitmap_clear_bit (df_get_live_in (prev_bb), j);
4168 if (curr_bb != last_bb)
4170 /* Update df_get_live_out (curr_bb): */
4171 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4173 live_p = bitmap_bit_p (&live_regs, j);
4174 if (! live_p)
4175 FOR_EACH_EDGE (e, ei, curr_bb->succs)
4176 if (bitmap_bit_p (df_get_live_in (e->dest), j))
4178 live_p = true;
4179 break;
4181 if (live_p)
4182 bitmap_set_bit (df_get_live_out (curr_bb), j);
4183 else
4184 bitmap_clear_bit (df_get_live_out (curr_bb), j);
4187 prev_bb = curr_bb;
4188 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
4190 if (! NONDEBUG_INSN_P (curr_insn))
4191 continue;
4192 curr_id = lra_get_insn_recog_data (curr_insn);
4193 remove_p = false;
4194 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
4195 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
4196 && bitmap_bit_p (&check_only_regs, regno)
4197 && ! bitmap_bit_p (&live_regs, regno))
4198 remove_p = true;
4199 /* See which defined values die here. */
4200 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4201 if (reg->type == OP_OUT && ! reg->subreg_p)
4202 bitmap_clear_bit (&live_regs, reg->regno);
4203 /* Mark each used value as live. */
4204 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4205 if (reg->type == OP_IN
4206 && bitmap_bit_p (&check_only_regs, reg->regno))
4207 bitmap_set_bit (&live_regs, reg->regno);
4208 /* It is quite important to remove dead move insns because it
4209 means removing dead store. We don't need to process them for
4210 constraints. */
4211 if (remove_p)
4213 if (lra_dump_file != NULL)
4215 fprintf (lra_dump_file, " Removing dead insn:\n ");
4216 debug_rtl_slim (lra_dump_file, curr_insn, curr_insn, -1, 0);
4218 lra_set_insn_deleted (curr_insn);
4223 /* The structure describes info to do an inheritance for the current
4224 insn. We need to collect such info first before doing the
4225 transformations because the transformations change the insn
4226 internal representation. */
4227 struct to_inherit
4229 /* Original regno. */
4230 int regno;
4231 /* Subsequent insns which can inherit original reg value. */
4232 rtx insns;
4235 /* Array containing all info for doing inheritance from the current
4236 insn. */
4237 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
4239 /* Number elements in the previous array. */
4240 static int to_inherit_num;
4242 /* Add inheritance info REGNO and INSNS. Their meaning is described in
4243 structure to_inherit. */
4244 static void
4245 add_to_inherit (int regno, rtx insns)
4247 int i;
4249 for (i = 0; i < to_inherit_num; i++)
4250 if (to_inherit[i].regno == regno)
4251 return;
4252 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
4253 to_inherit[to_inherit_num].regno = regno;
4254 to_inherit[to_inherit_num++].insns = insns;
4257 /* Return the last non-debug insn in basic block BB, or the block begin
4258 note if none. */
4259 static rtx
4260 get_last_insertion_point (basic_block bb)
4262 rtx insn;
4264 FOR_BB_INSNS_REVERSE (bb, insn)
4265 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
4266 return insn;
4267 gcc_unreachable ();
4270 /* Set up RES by registers living on edges FROM except the edge (FROM,
4271 TO) or by registers set up in a jump insn in BB FROM. */
4272 static void
4273 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
4275 rtx last;
4276 struct lra_insn_reg *reg;
4277 edge e;
4278 edge_iterator ei;
4280 lra_assert (to != NULL);
4281 bitmap_clear (res);
4282 FOR_EACH_EDGE (e, ei, from->succs)
4283 if (e->dest != to)
4284 bitmap_ior_into (res, df_get_live_in (e->dest));
4285 last = get_last_insertion_point (from);
4286 if (! JUMP_P (last))
4287 return;
4288 curr_id = lra_get_insn_recog_data (last);
4289 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4290 if (reg->type != OP_IN)
4291 bitmap_set_bit (res, reg->regno);
4294 /* Used as a temporary results of some bitmap calculations. */
4295 static bitmap_head temp_bitmap;
4297 /* Do inheritance/split transformations in EBB starting with HEAD and
4298 finishing on TAIL. We process EBB insns in the reverse order.
4299 Return true if we did any inheritance/split transformation in the
4300 EBB.
4302 We should avoid excessive splitting which results in worse code
4303 because of inaccurate cost calculations for spilling new split
4304 pseudos in such case. To achieve this we do splitting only if
4305 register pressure is high in given basic block and there are reload
4306 pseudos requiring hard registers. We could do more register
4307 pressure calculations at any given program point to avoid necessary
4308 splitting even more but it is to expensive and the current approach
4309 works well enough. */
4310 static bool
4311 inherit_in_ebb (rtx head, rtx tail)
4313 int i, src_regno, dst_regno, nregs;
4314 bool change_p, succ_p;
4315 rtx prev_insn, next_usage_insns, set, last_insn;
4316 enum reg_class cl;
4317 struct lra_insn_reg *reg;
4318 basic_block last_processed_bb, curr_bb = NULL;
4319 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
4320 bitmap to_process;
4321 unsigned int j;
4322 bitmap_iterator bi;
4323 bool head_p, after_p;
4325 change_p = false;
4326 curr_usage_insns_check++;
4327 reloads_num = calls_num = 0;
4328 bitmap_clear (&check_only_regs);
4329 last_processed_bb = NULL;
4330 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
4331 CLEAR_HARD_REG_SET (live_hard_regs);
4332 /* We don't process new insns generated in the loop. */
4333 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
4335 prev_insn = PREV_INSN (curr_insn);
4336 if (BLOCK_FOR_INSN (curr_insn) != NULL)
4337 curr_bb = BLOCK_FOR_INSN (curr_insn);
4338 if (last_processed_bb != curr_bb)
4340 /* We are at the end of BB. Add qualified living
4341 pseudos for potential splitting. */
4342 to_process = df_get_live_out (curr_bb);
4343 if (last_processed_bb != NULL)
4345 /* We are somewhere in the middle of EBB. */
4346 get_live_on_other_edges (curr_bb, last_processed_bb,
4347 &temp_bitmap);
4348 to_process = &temp_bitmap;
4350 last_processed_bb = curr_bb;
4351 last_insn = get_last_insertion_point (curr_bb);
4352 after_p = (! JUMP_P (last_insn)
4353 && (! CALL_P (last_insn)
4354 || (find_reg_note (last_insn,
4355 REG_NORETURN, NULL_RTX) == NULL_RTX
4356 && ! SIBLING_CALL_P (last_insn))));
4357 REG_SET_TO_HARD_REG_SET (live_hard_regs, df_get_live_out (curr_bb));
4358 IOR_HARD_REG_SET (live_hard_regs, eliminable_regset);
4359 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
4360 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
4361 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
4363 if ((int) j >= lra_constraint_new_regno_start)
4364 break;
4365 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
4367 if (j < FIRST_PSEUDO_REGISTER)
4368 SET_HARD_REG_BIT (live_hard_regs, j);
4369 else
4370 add_to_hard_reg_set (&live_hard_regs,
4371 PSEUDO_REGNO_MODE (j),
4372 reg_renumber[j]);
4373 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
4377 src_regno = dst_regno = -1;
4378 if (NONDEBUG_INSN_P (curr_insn)
4379 && (set = single_set (curr_insn)) != NULL_RTX
4380 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
4382 src_regno = REGNO (SET_SRC (set));
4383 dst_regno = REGNO (SET_DEST (set));
4385 if (src_regno < lra_constraint_new_regno_start
4386 && src_regno >= FIRST_PSEUDO_REGISTER
4387 && reg_renumber[src_regno] < 0
4388 && dst_regno >= lra_constraint_new_regno_start
4389 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
4391 /* 'reload_pseudo <- original_pseudo'. */
4392 reloads_num++;
4393 succ_p = false;
4394 if (usage_insns[src_regno].check == curr_usage_insns_check
4395 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
4396 succ_p = inherit_reload_reg (false, src_regno, cl,
4397 curr_insn, next_usage_insns);
4398 if (succ_p)
4399 change_p = true;
4400 else
4401 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
4402 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
4403 IOR_HARD_REG_SET (potential_reload_hard_regs,
4404 reg_class_contents[cl]);
4406 else if (src_regno >= lra_constraint_new_regno_start
4407 && dst_regno < lra_constraint_new_regno_start
4408 && dst_regno >= FIRST_PSEUDO_REGISTER
4409 && reg_renumber[dst_regno] < 0
4410 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
4411 && usage_insns[dst_regno].check == curr_usage_insns_check
4412 && (next_usage_insns
4413 = usage_insns[dst_regno].insns) != NULL_RTX)
4415 reloads_num++;
4416 /* 'original_pseudo <- reload_pseudo'. */
4417 if (! JUMP_P (curr_insn)
4418 && inherit_reload_reg (true, dst_regno, cl,
4419 curr_insn, next_usage_insns))
4420 change_p = true;
4421 /* Invalidate. */
4422 usage_insns[dst_regno].check = 0;
4423 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
4424 IOR_HARD_REG_SET (potential_reload_hard_regs,
4425 reg_class_contents[cl]);
4427 else if (INSN_P (curr_insn))
4429 int max_uid = get_max_uid ();
4431 curr_id = lra_get_insn_recog_data (curr_insn);
4432 to_inherit_num = 0;
4433 /* Process insn definitions. */
4434 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4435 if (reg->type != OP_IN
4436 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
4438 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
4439 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
4440 && usage_insns[dst_regno].check == curr_usage_insns_check
4441 && (next_usage_insns
4442 = usage_insns[dst_regno].insns) != NULL_RTX)
4444 struct lra_insn_reg *r;
4446 for (r = curr_id->regs; r != NULL; r = r->next)
4447 if (r->type != OP_OUT && r->regno == dst_regno)
4448 break;
4449 /* Don't do inheritance if the pseudo is also
4450 used in the insn. */
4451 if (r == NULL)
4452 /* We can not do inheritance right now
4453 because the current insn reg info (chain
4454 regs) can change after that. */
4455 add_to_inherit (dst_regno, next_usage_insns);
4457 /* We can not process one reg twice here because of
4458 usage_insns invalidation. */
4459 if ((dst_regno < FIRST_PSEUDO_REGISTER
4460 || reg_renumber[dst_regno] >= 0)
4461 && ! reg->subreg_p && reg->type == OP_OUT)
4463 HARD_REG_SET s;
4465 if (split_if_necessary (dst_regno, reg->biggest_mode,
4466 potential_reload_hard_regs,
4467 false, curr_insn, max_uid))
4468 change_p = true;
4469 CLEAR_HARD_REG_SET (s);
4470 if (dst_regno < FIRST_PSEUDO_REGISTER)
4471 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
4472 else
4473 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
4474 reg_renumber[dst_regno]);
4475 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
4477 /* We should invalidate potential inheritance or
4478 splitting for the current insn usages to the next
4479 usage insns (see code below) as the output pseudo
4480 prevents this. */
4481 if ((dst_regno >= FIRST_PSEUDO_REGISTER
4482 && reg_renumber[dst_regno] < 0)
4483 || (reg->type == OP_OUT && ! reg->subreg_p
4484 && (dst_regno < FIRST_PSEUDO_REGISTER
4485 || reg_renumber[dst_regno] >= 0)))
4487 /* Invalidate. */
4488 if (dst_regno >= FIRST_PSEUDO_REGISTER)
4489 usage_insns[dst_regno].check = 0;
4490 else
4492 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
4493 for (i = 0; i < nregs; i++)
4494 usage_insns[dst_regno + i].check = 0;
4498 if (! JUMP_P (curr_insn))
4499 for (i = 0; i < to_inherit_num; i++)
4500 if (inherit_reload_reg (true, to_inherit[i].regno,
4501 ALL_REGS, curr_insn,
4502 to_inherit[i].insns))
4503 change_p = true;
4504 if (CALL_P (curr_insn))
4506 rtx cheap, pat, dest, restore;
4507 int regno, hard_regno;
4509 calls_num++;
4510 if ((cheap = find_reg_note (curr_insn,
4511 REG_RETURNED, NULL_RTX)) != NULL_RTX
4512 && ((cheap = XEXP (cheap, 0)), true)
4513 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
4514 && (hard_regno = reg_renumber[regno]) >= 0
4515 /* If there are pending saves/restores, the
4516 optimization is not worth. */
4517 && usage_insns[regno].calls_num == calls_num - 1
4518 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
4520 /* Restore the pseudo from the call result as
4521 REG_RETURNED note says that the pseudo value is
4522 in the call result and the pseudo is an argument
4523 of the call. */
4524 pat = PATTERN (curr_insn);
4525 if (GET_CODE (pat) == PARALLEL)
4526 pat = XVECEXP (pat, 0, 0);
4527 dest = SET_DEST (pat);
4528 start_sequence ();
4529 emit_move_insn (cheap, copy_rtx (dest));
4530 restore = get_insns ();
4531 end_sequence ();
4532 lra_process_new_insns (curr_insn, NULL, restore,
4533 "Inserting call parameter restore");
4534 /* We don't need to save/restore of the pseudo from
4535 this call. */
4536 usage_insns[regno].calls_num = calls_num;
4537 bitmap_set_bit (&check_only_regs, regno);
4540 to_inherit_num = 0;
4541 /* Process insn usages. */
4542 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4543 if ((reg->type != OP_OUT
4544 || (reg->type == OP_OUT && reg->subreg_p))
4545 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
4547 if (src_regno >= FIRST_PSEUDO_REGISTER
4548 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
4550 if (usage_insns[src_regno].check == curr_usage_insns_check
4551 && (next_usage_insns
4552 = usage_insns[src_regno].insns) != NULL_RTX
4553 && NONDEBUG_INSN_P (curr_insn))
4554 add_to_inherit (src_regno, next_usage_insns);
4555 else
4556 /* Add usages. */
4557 add_next_usage_insn (src_regno, curr_insn, reloads_num);
4559 else if (src_regno < FIRST_PSEUDO_REGISTER
4560 || reg_renumber[src_regno] >= 0)
4562 bool before_p;
4563 rtx use_insn = curr_insn;
4565 before_p = (JUMP_P (curr_insn)
4566 || (CALL_P (curr_insn) && reg->type == OP_IN));
4567 if (NONDEBUG_INSN_P (curr_insn)
4568 && split_if_necessary (src_regno, reg->biggest_mode,
4569 potential_reload_hard_regs,
4570 before_p, curr_insn, max_uid))
4572 if (reg->subreg_p)
4573 lra_risky_transformations_p = true;
4574 change_p = true;
4575 /* Invalidate. */
4576 usage_insns[src_regno].check = 0;
4577 if (before_p)
4578 use_insn = PREV_INSN (curr_insn);
4580 if (NONDEBUG_INSN_P (curr_insn))
4582 if (src_regno < FIRST_PSEUDO_REGISTER)
4583 add_to_hard_reg_set (&live_hard_regs,
4584 reg->biggest_mode, src_regno);
4585 else
4586 add_to_hard_reg_set (&live_hard_regs,
4587 PSEUDO_REGNO_MODE (src_regno),
4588 reg_renumber[src_regno]);
4590 add_next_usage_insn (src_regno, use_insn, reloads_num);
4593 for (i = 0; i < to_inherit_num; i++)
4595 src_regno = to_inherit[i].regno;
4596 if (inherit_reload_reg (false, src_regno, ALL_REGS,
4597 curr_insn, to_inherit[i].insns))
4598 change_p = true;
4599 else
4600 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
4603 /* We reached the start of the current basic block. */
4604 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
4605 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
4607 /* We reached the beginning of the current block -- do
4608 rest of spliting in the current BB. */
4609 to_process = df_get_live_in (curr_bb);
4610 if (BLOCK_FOR_INSN (head) != curr_bb)
4612 /* We are somewhere in the middle of EBB. */
4613 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
4614 curr_bb, &temp_bitmap);
4615 to_process = &temp_bitmap;
4617 head_p = true;
4618 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
4620 if ((int) j >= lra_constraint_new_regno_start)
4621 break;
4622 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
4623 && usage_insns[j].check == curr_usage_insns_check
4624 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
4626 if (need_for_split_p (potential_reload_hard_regs, j))
4628 if (lra_dump_file != NULL && head_p)
4630 fprintf (lra_dump_file,
4631 " ----------------------------------\n");
4632 head_p = false;
4634 if (split_reg (false, j, bb_note (curr_bb),
4635 next_usage_insns))
4636 change_p = true;
4638 usage_insns[j].check = 0;
4643 return change_p;
4646 /* This value affects EBB forming. If probability of edge from EBB to
4647 a BB is not greater than the following value, we don't add the BB
4648 to EBB. */
4649 #define EBB_PROBABILITY_CUTOFF (REG_BR_PROB_BASE / 2)
4651 /* Current number of inheritance/split iteration. */
4652 int lra_inheritance_iter;
4654 /* Entry function for inheritance/split pass. */
4655 void
4656 lra_inheritance (void)
4658 int i;
4659 basic_block bb, start_bb;
4660 edge e;
4662 timevar_push (TV_LRA_INHERITANCE);
4663 lra_inheritance_iter++;
4664 if (lra_dump_file != NULL)
4665 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
4666 lra_inheritance_iter);
4667 curr_usage_insns_check = 0;
4668 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
4669 for (i = 0; i < lra_constraint_new_regno_start; i++)
4670 usage_insns[i].check = 0;
4671 bitmap_initialize (&check_only_regs, &reg_obstack);
4672 bitmap_initialize (&live_regs, &reg_obstack);
4673 bitmap_initialize (&temp_bitmap, &reg_obstack);
4674 bitmap_initialize (&ebb_global_regs, &reg_obstack);
4675 FOR_EACH_BB (bb)
4677 start_bb = bb;
4678 if (lra_dump_file != NULL)
4679 fprintf (lra_dump_file, "EBB");
4680 /* Form a EBB starting with BB. */
4681 bitmap_clear (&ebb_global_regs);
4682 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
4683 for (;;)
4685 if (lra_dump_file != NULL)
4686 fprintf (lra_dump_file, " %d", bb->index);
4687 if (bb->next_bb == EXIT_BLOCK_PTR || LABEL_P (BB_HEAD (bb->next_bb)))
4688 break;
4689 e = find_fallthru_edge (bb->succs);
4690 if (! e)
4691 break;
4692 if (e->probability <= EBB_PROBABILITY_CUTOFF)
4693 break;
4694 bb = bb->next_bb;
4696 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
4697 if (lra_dump_file != NULL)
4698 fprintf (lra_dump_file, "\n");
4699 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
4700 /* Remember that the EBB head and tail can change in
4701 inherit_in_ebb. */
4702 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
4704 bitmap_clear (&ebb_global_regs);
4705 bitmap_clear (&temp_bitmap);
4706 bitmap_clear (&live_regs);
4707 bitmap_clear (&check_only_regs);
4708 free (usage_insns);
4710 timevar_pop (TV_LRA_INHERITANCE);
4715 /* This page contains code to undo failed inheritance/split
4716 transformations. */
4718 /* Current number of iteration undoing inheritance/split. */
4719 int lra_undo_inheritance_iter;
4721 /* Fix BB live info LIVE after removing pseudos created on pass doing
4722 inheritance/split which are REMOVED_PSEUDOS. */
4723 static void
4724 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
4726 unsigned int regno;
4727 bitmap_iterator bi;
4729 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
4730 if (bitmap_clear_bit (live, regno))
4731 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
4734 /* Return regno of the (subreg of) REG. Otherwise, return a negative
4735 number. */
4736 static int
4737 get_regno (rtx reg)
4739 if (GET_CODE (reg) == SUBREG)
4740 reg = SUBREG_REG (reg);
4741 if (REG_P (reg))
4742 return REGNO (reg);
4743 return -1;
4746 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
4747 return true if we did any change. The undo transformations for
4748 inheritance looks like
4749 i <- i2
4750 p <- i => p <- i2
4751 or removing
4752 p <- i, i <- p, and i <- i3
4753 where p is original pseudo from which inheritance pseudo i was
4754 created, i and i3 are removed inheritance pseudos, i2 is another
4755 not removed inheritance pseudo. All split pseudos or other
4756 occurrences of removed inheritance pseudos are changed on the
4757 corresponding original pseudos.
4759 The function also schedules insns changed and created during
4760 inheritance/split pass for processing by the subsequent constraint
4761 pass. */
4762 static bool
4763 remove_inheritance_pseudos (bitmap remove_pseudos)
4765 basic_block bb;
4766 int regno, sregno, prev_sregno, dregno, restore_regno;
4767 rtx set, prev_set, prev_insn;
4768 bool change_p, done_p;
4770 change_p = ! bitmap_empty_p (remove_pseudos);
4771 /* We can not finish the function right away if CHANGE_P is true
4772 because we need to marks insns affected by previous
4773 inheritance/split pass for processing by the subsequent
4774 constraint pass. */
4775 FOR_EACH_BB (bb)
4777 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
4778 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
4779 FOR_BB_INSNS_REVERSE (bb, curr_insn)
4781 if (! INSN_P (curr_insn))
4782 continue;
4783 done_p = false;
4784 sregno = dregno = -1;
4785 if (change_p && NONDEBUG_INSN_P (curr_insn)
4786 && (set = single_set (curr_insn)) != NULL_RTX)
4788 dregno = get_regno (SET_DEST (set));
4789 sregno = get_regno (SET_SRC (set));
4792 if (sregno >= 0 && dregno >= 0)
4794 if ((bitmap_bit_p (remove_pseudos, sregno)
4795 && (lra_reg_info[sregno].restore_regno == dregno
4796 || (bitmap_bit_p (remove_pseudos, dregno)
4797 && (lra_reg_info[sregno].restore_regno
4798 == lra_reg_info[dregno].restore_regno))))
4799 || (bitmap_bit_p (remove_pseudos, dregno)
4800 && lra_reg_info[dregno].restore_regno == sregno))
4801 /* One of the following cases:
4802 original <- removed inheritance pseudo
4803 removed inherit pseudo <- another removed inherit pseudo
4804 removed inherit pseudo <- original pseudo
4806 removed_split_pseudo <- original_reg
4807 original_reg <- removed_split_pseudo */
4809 if (lra_dump_file != NULL)
4811 fprintf (lra_dump_file, " Removing %s:\n",
4812 bitmap_bit_p (&lra_split_regs, sregno)
4813 || bitmap_bit_p (&lra_split_regs, dregno)
4814 ? "split" : "inheritance");
4815 debug_rtl_slim (lra_dump_file,
4816 curr_insn, curr_insn, -1, 0);
4818 lra_set_insn_deleted (curr_insn);
4819 done_p = true;
4821 else if (bitmap_bit_p (remove_pseudos, sregno)
4822 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
4824 /* Search the following pattern:
4825 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
4826 original_pseudo <- inherit_or_split_pseudo1
4827 where the 2nd insn is the current insn and
4828 inherit_or_split_pseudo2 is not removed. If it is found,
4829 change the current insn onto:
4830 original_pseudo <- inherit_or_split_pseudo2. */
4831 for (prev_insn = PREV_INSN (curr_insn);
4832 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
4833 prev_insn = PREV_INSN (prev_insn))
4835 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
4836 && (prev_set = single_set (prev_insn)) != NULL_RTX
4837 /* There should be no subregs in insn we are
4838 searching because only the original reg might
4839 be in subreg when we changed the mode of
4840 load/store for splitting. */
4841 && REG_P (SET_DEST (prev_set))
4842 && REG_P (SET_SRC (prev_set))
4843 && (int) REGNO (SET_DEST (prev_set)) == sregno
4844 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
4845 >= FIRST_PSEUDO_REGISTER)
4846 /* As we consider chain of inheritance or
4847 splitting described in above comment we should
4848 check that sregno and prev_sregno were
4849 inheritance/split pseudos created from the
4850 same original regno. */
4851 && (lra_reg_info[sregno].restore_regno
4852 == lra_reg_info[prev_sregno].restore_regno)
4853 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
4855 lra_assert (GET_MODE (SET_SRC (prev_set))
4856 == GET_MODE (regno_reg_rtx[sregno]));
4857 if (GET_CODE (SET_SRC (set)) == SUBREG)
4858 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
4859 else
4860 SET_SRC (set) = SET_SRC (prev_set);
4861 lra_push_insn_and_update_insn_regno_info (curr_insn);
4862 lra_set_used_insn_alternative_by_uid
4863 (INSN_UID (curr_insn), -1);
4864 done_p = true;
4865 if (lra_dump_file != NULL)
4867 fprintf (lra_dump_file, " Change reload insn:\n");
4868 debug_rtl_slim (lra_dump_file,
4869 curr_insn, curr_insn, -1, 0);
4874 if (! done_p)
4876 struct lra_insn_reg *reg;
4877 bool restored_regs_p = false;
4878 bool kept_regs_p = false;
4880 curr_id = lra_get_insn_recog_data (curr_insn);
4881 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4883 regno = reg->regno;
4884 restore_regno = lra_reg_info[regno].restore_regno;
4885 if (restore_regno >= 0)
4887 if (change_p && bitmap_bit_p (remove_pseudos, regno))
4889 substitute_pseudo (&curr_insn, regno,
4890 regno_reg_rtx[restore_regno]);
4891 restored_regs_p = true;
4893 else
4894 kept_regs_p = true;
4897 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
4899 /* The instruction has changed since the previous
4900 constraints pass. */
4901 lra_push_insn_and_update_insn_regno_info (curr_insn);
4902 lra_set_used_insn_alternative_by_uid
4903 (INSN_UID (curr_insn), -1);
4905 else if (restored_regs_p)
4906 /* The instruction has been restored to the form that
4907 it had during the previous constraints pass. */
4908 lra_update_insn_regno_info (curr_insn);
4909 if (restored_regs_p && lra_dump_file != NULL)
4911 fprintf (lra_dump_file, " Insn after restoring regs:\n");
4912 debug_rtl_slim (lra_dump_file, curr_insn, curr_insn, -1, 0);
4917 return change_p;
4920 /* Entry function for undoing inheritance/split transformation. Return true
4921 if we did any RTL change in this pass. */
4922 bool
4923 lra_undo_inheritance (void)
4925 unsigned int regno;
4926 int restore_regno, hard_regno;
4927 int n_all_inherit, n_inherit, n_all_split, n_split;
4928 bitmap_head remove_pseudos;
4929 bitmap_iterator bi;
4930 bool change_p;
4932 lra_undo_inheritance_iter++;
4933 if (lra_dump_file != NULL)
4934 fprintf (lra_dump_file,
4935 "\n********** Undoing inheritance #%d: **********\n\n",
4936 lra_undo_inheritance_iter);
4937 bitmap_initialize (&remove_pseudos, &reg_obstack);
4938 n_inherit = n_all_inherit = 0;
4939 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
4940 if (lra_reg_info[regno].restore_regno >= 0)
4942 n_all_inherit++;
4943 if (reg_renumber[regno] < 0)
4944 bitmap_set_bit (&remove_pseudos, regno);
4945 else
4946 n_inherit++;
4948 if (lra_dump_file != NULL && n_all_inherit != 0)
4949 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
4950 n_inherit, n_all_inherit,
4951 (double) n_inherit / n_all_inherit * 100);
4952 n_split = n_all_split = 0;
4953 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
4954 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
4956 n_all_split++;
4957 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
4958 ? reg_renumber[restore_regno] : restore_regno);
4959 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
4960 bitmap_set_bit (&remove_pseudos, regno);
4961 else
4963 n_split++;
4964 if (lra_dump_file != NULL)
4965 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
4966 regno, restore_regno);
4969 if (lra_dump_file != NULL && n_all_split != 0)
4970 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
4971 n_split, n_all_split,
4972 (double) n_split / n_all_split * 100);
4973 change_p = remove_inheritance_pseudos (&remove_pseudos);
4974 bitmap_clear (&remove_pseudos);
4975 /* Clear restore_regnos. */
4976 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
4977 lra_reg_info[regno].restore_regno = -1;
4978 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
4979 lra_reg_info[regno].restore_regno = -1;
4980 return change_p;