ChangeLog/
[official-gcc.git] / gcc / lra-constraints.c
blob9e4d3b1f6def1946af7ce69d4465c433c1579f0f
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;
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);
686 else
688 reg = new_out_reg
689 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
690 goal_class, "");
691 if (SCALAR_INT_MODE_P (outmode))
692 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
693 else
694 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
695 /* NEW_IN_REG is non-paradoxical subreg. We don't want
696 NEW_OUT_REG living above. We add clobber clause for
697 this. */
698 emit_clobber (new_out_reg);
701 else
703 /* Pseudos have values -- see comments for lra_reg_info.
704 Different pseudos with the same value do not conflict even if
705 they live in the same place. When we create a pseudo we
706 assign value of original pseudo (if any) from which we
707 created the new pseudo. If we create the pseudo from the
708 input pseudo, the new pseudo will no conflict with the input
709 pseudo which is wrong when the input pseudo lives after the
710 insn and as the new pseudo value is changed by the insn
711 output. Therefore we create the new pseudo from the output.
713 We cannot reuse the current output register because we might
714 have a situation like "a <- a op b", where the constraints
715 force the second input operand ("b") to match the output
716 operand ("a"). "b" must then be copied into a new register
717 so that it doesn't clobber the current value of "a". */
719 new_in_reg = new_out_reg
720 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
721 goal_class, "");
723 /* In and out operand can be got from transformations before
724 processing insn constraints. One example of such transformations
725 is subreg reloading (see function simplify_operand_subreg). The
726 new pseudos created by the transformations might have inaccurate
727 class (ALL_REGS) and we should make their classes more
728 accurate. */
729 narrow_reload_pseudo_class (in_rtx, goal_class);
730 narrow_reload_pseudo_class (out_rtx, goal_class);
731 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
732 *before = get_insns ();
733 end_sequence ();
734 for (i = 0; (in = ins[i]) >= 0; i++)
736 lra_assert
737 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
738 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
739 *curr_id->operand_loc[in] = new_in_reg;
741 lra_update_dups (curr_id, ins);
742 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
744 start_sequence ();
745 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
746 emit_insn (*after);
747 *after = get_insns ();
748 end_sequence ();
750 *curr_id->operand_loc[out] = new_out_reg;
751 lra_update_dup (curr_id, out);
754 /* Return register class which is union of all reg classes in insn
755 constraint alternative string starting with P. */
756 static enum reg_class
757 reg_class_from_constraints (const char *p)
759 int c, len;
760 enum reg_class op_class = NO_REGS;
763 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
765 case '#':
766 case ',':
767 return op_class;
769 case 'p':
770 op_class = (reg_class_subunion
771 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
772 ADDRESS, SCRATCH)]);
773 break;
775 case 'g':
776 case 'r':
777 op_class = reg_class_subunion[op_class][GENERAL_REGS];
778 break;
780 default:
781 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
783 #ifdef EXTRA_CONSTRAINT_STR
784 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
785 op_class
786 = (reg_class_subunion
787 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
788 ADDRESS, SCRATCH)]);
789 #endif
790 break;
793 op_class
794 = reg_class_subunion[op_class][REG_CLASS_FROM_CONSTRAINT (c, p)];
795 break;
797 while ((p += len), c);
798 return op_class;
801 /* If OP is a register, return the class of the register as per
802 get_reg_class, otherwise return NO_REGS. */
803 static inline enum reg_class
804 get_op_class (rtx op)
806 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
809 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
810 otherwise. If modes of MEM_PSEUDO and VAL are different, use
811 SUBREG for VAL to make them equal. */
812 static rtx
813 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
815 if (GET_MODE (mem_pseudo) != GET_MODE (val))
816 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
817 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
819 return (to_p
820 ? gen_move_insn (mem_pseudo, val)
821 : gen_move_insn (val, mem_pseudo));
824 /* Process a special case insn (register move), return true if we
825 don't need to process it anymore. Return that RTL was changed
826 through CHANGE_P and macro SECONDARY_MEMORY_NEEDED says to use
827 secondary memory through SEC_MEM_P. */
828 static bool
829 check_and_process_move (bool *change_p, bool *sec_mem_p)
831 int sregno, dregno;
832 rtx set, dest, src, dreg, sreg, old_sreg, new_reg, before, scratch_reg;
833 enum reg_class dclass, sclass, secondary_class;
834 enum machine_mode sreg_mode;
835 secondary_reload_info sri;
837 *sec_mem_p = *change_p = false;
838 if ((set = single_set (curr_insn)) == NULL)
839 return false;
840 dreg = dest = SET_DEST (set);
841 sreg = src = SET_SRC (set);
842 /* Quick check on the right move insn which does not need
843 reloads. */
844 if ((dclass = get_op_class (dest)) != NO_REGS
845 && (sclass = get_op_class (src)) != NO_REGS
846 /* The backend guarantees that register moves of cost 2 never
847 need reloads. */
848 && targetm.register_move_cost (GET_MODE (src), dclass, sclass) == 2)
849 return true;
850 if (GET_CODE (dest) == SUBREG)
851 dreg = SUBREG_REG (dest);
852 if (GET_CODE (src) == SUBREG)
853 sreg = SUBREG_REG (src);
854 if (! REG_P (dreg) || ! REG_P (sreg))
855 return false;
856 sclass = dclass = NO_REGS;
857 dreg = get_equiv_substitution (dreg);
858 if (REG_P (dreg))
859 dclass = get_reg_class (REGNO (dreg));
860 if (dclass == ALL_REGS)
861 /* ALL_REGS is used for new pseudos created by transformations
862 like reload of SUBREG_REG (see function
863 simplify_operand_subreg). We don't know their class yet. We
864 should figure out the class from processing the insn
865 constraints not in this fast path function. Even if ALL_REGS
866 were a right class for the pseudo, secondary_... hooks usually
867 are not define for ALL_REGS. */
868 return false;
869 sreg_mode = GET_MODE (sreg);
870 old_sreg = sreg;
871 sreg = get_equiv_substitution (sreg);
872 if (REG_P (sreg))
873 sclass = get_reg_class (REGNO (sreg));
874 if (sclass == ALL_REGS)
875 /* See comments above. */
876 return false;
877 #ifdef SECONDARY_MEMORY_NEEDED
878 if (dclass != NO_REGS && sclass != NO_REGS
879 && SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src)))
881 *sec_mem_p = true;
882 return false;
884 #endif
885 sri.prev_sri = NULL;
886 sri.icode = CODE_FOR_nothing;
887 sri.extra_cost = 0;
888 secondary_class = NO_REGS;
889 /* Set up hard register for a reload pseudo for hook
890 secondary_reload because some targets just ignore unassigned
891 pseudos in the hook. */
892 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
894 dregno = REGNO (dreg);
895 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
897 else
898 dregno = -1;
899 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
901 sregno = REGNO (sreg);
902 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
904 else
905 sregno = -1;
906 if (sclass != NO_REGS)
907 secondary_class
908 = (enum reg_class) targetm.secondary_reload (false, dest,
909 (reg_class_t) sclass,
910 GET_MODE (src), &sri);
911 if (sclass == NO_REGS
912 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
913 && dclass != NO_REGS))
915 enum reg_class old_sclass = secondary_class;
916 secondary_reload_info old_sri = sri;
918 sri.prev_sri = NULL;
919 sri.icode = CODE_FOR_nothing;
920 sri.extra_cost = 0;
921 secondary_class
922 = (enum reg_class) targetm.secondary_reload (true, sreg,
923 (reg_class_t) dclass,
924 sreg_mode, &sri);
925 /* Check the target hook consistency. */
926 lra_assert
927 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
928 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
929 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
931 if (sregno >= 0)
932 reg_renumber [sregno] = -1;
933 if (dregno >= 0)
934 reg_renumber [dregno] = -1;
935 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
936 return false;
937 *change_p = true;
938 new_reg = NULL_RTX;
939 if (secondary_class != NO_REGS)
940 new_reg = lra_create_new_reg_with_unique_value (sreg_mode, NULL_RTX,
941 secondary_class,
942 "secondary");
943 start_sequence ();
944 if (old_sreg != sreg)
945 sreg = copy_rtx (sreg);
946 if (sri.icode == CODE_FOR_nothing)
947 lra_emit_move (new_reg, sreg);
948 else
950 enum reg_class scratch_class;
952 scratch_class = (reg_class_from_constraints
953 (insn_data[sri.icode].operand[2].constraint));
954 scratch_reg = (lra_create_new_reg_with_unique_value
955 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
956 scratch_class, "scratch"));
957 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
958 sreg, scratch_reg));
960 before = get_insns ();
961 end_sequence ();
962 lra_process_new_insns (curr_insn, before, NULL_RTX, "Inserting the move");
963 if (new_reg != NULL_RTX)
965 if (GET_CODE (src) == SUBREG)
966 SUBREG_REG (src) = new_reg;
967 else
968 SET_SRC (set) = new_reg;
970 else
972 if (lra_dump_file != NULL)
974 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
975 debug_rtl_slim (lra_dump_file, curr_insn, curr_insn, -1, 0);
977 lra_set_insn_deleted (curr_insn);
978 return true;
980 return false;
983 /* The following data describe the result of process_alt_operands.
984 The data are used in curr_insn_transform to generate reloads. */
986 /* The chosen reg classes which should be used for the corresponding
987 operands. */
988 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
989 /* True if the operand should be the same as another operand and that
990 other operand does not need a reload. */
991 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
992 /* True if the operand does not need a reload. */
993 static bool goal_alt_win[MAX_RECOG_OPERANDS];
994 /* True if the operand can be offsetable memory. */
995 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
996 /* The number of an operand to which given operand can be matched to. */
997 static int goal_alt_matches[MAX_RECOG_OPERANDS];
998 /* The number of elements in the following array. */
999 static int goal_alt_dont_inherit_ops_num;
1000 /* Numbers of operands whose reload pseudos should not be inherited. */
1001 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1002 /* True if the insn commutative operands should be swapped. */
1003 static bool goal_alt_swapped;
1004 /* The chosen insn alternative. */
1005 static int goal_alt_number;
1007 /* The following five variables are used to choose the best insn
1008 alternative. They reflect final characteristics of the best
1009 alternative. */
1011 /* Number of necessary reloads and overall cost reflecting the
1012 previous value and other unpleasantness of the best alternative. */
1013 static int best_losers, best_overall;
1014 /* Number of small register classes used for operands of the best
1015 alternative. */
1016 static int best_small_class_operands_num;
1017 /* Overall number hard registers used for reloads. For example, on
1018 some targets we need 2 general registers to reload DFmode and only
1019 one floating point register. */
1020 static int best_reload_nregs;
1021 /* Overall number reflecting distances of previous reloading the same
1022 value. The distances are counted from the current BB start. It is
1023 used to improve inheritance chances. */
1024 static int best_reload_sum;
1026 /* True if the current insn should have no correspondingly input or
1027 output reloads. */
1028 static bool no_input_reloads_p, no_output_reloads_p;
1030 /* True if we swapped the commutative operands in the current
1031 insn. */
1032 static int curr_swapped;
1034 /* Arrange for address element *LOC to be a register of class CL.
1035 Add any input reloads to list BEFORE. AFTER is nonnull if *LOC is an
1036 automodified value; handle that case by adding the required output
1037 reloads to list AFTER. Return true if the RTL was changed. */
1038 static bool
1039 process_addr_reg (rtx *loc, rtx *before, rtx *after, enum reg_class cl)
1041 int regno;
1042 enum reg_class rclass, new_class;
1043 rtx reg;
1044 rtx new_reg;
1045 enum machine_mode mode;
1046 bool before_p = false;
1048 loc = strip_subreg (loc);
1049 reg = *loc;
1050 mode = GET_MODE (reg);
1051 if (! REG_P (reg))
1053 /* Always reload memory in an address even if the target supports
1054 such addresses. */
1055 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1056 before_p = true;
1058 else
1060 regno = REGNO (reg);
1061 rclass = get_reg_class (regno);
1062 if ((*loc = get_equiv_substitution (reg)) != reg)
1064 if (lra_dump_file != NULL)
1066 fprintf (lra_dump_file,
1067 "Changing pseudo %d in address of insn %u on equiv ",
1068 REGNO (reg), INSN_UID (curr_insn));
1069 print_value_slim (lra_dump_file, *loc, 1);
1070 fprintf (lra_dump_file, "\n");
1072 *loc = copy_rtx (*loc);
1074 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1076 reg = *loc;
1077 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1078 mode, reg, cl, "address", &new_reg))
1079 before_p = true;
1081 else if (new_class != NO_REGS && rclass != new_class)
1083 change_class (regno, new_class, " Change", true);
1084 return false;
1086 else
1087 return false;
1089 if (before_p)
1091 push_to_sequence (*before);
1092 lra_emit_move (new_reg, reg);
1093 *before = get_insns ();
1094 end_sequence ();
1096 *loc = new_reg;
1097 if (after != NULL)
1099 start_sequence ();
1100 lra_emit_move (reg, new_reg);
1101 emit_insn (*after);
1102 *after = get_insns ();
1103 end_sequence ();
1105 return true;
1108 #ifndef SLOW_UNALIGNED_ACCESS
1109 #define SLOW_UNALIGNED_ACCESS(mode, align) 0
1110 #endif
1112 /* Make reloads for subreg in operand NOP with internal subreg mode
1113 REG_MODE, add new reloads for further processing. Return true if
1114 any reload was generated. */
1115 static bool
1116 simplify_operand_subreg (int nop, enum machine_mode reg_mode)
1118 int hard_regno;
1119 rtx before, after;
1120 enum machine_mode mode;
1121 rtx reg, new_reg;
1122 rtx operand = *curr_id->operand_loc[nop];
1124 before = after = NULL_RTX;
1126 if (GET_CODE (operand) != SUBREG)
1127 return false;
1129 mode = GET_MODE (operand);
1130 reg = SUBREG_REG (operand);
1131 /* If we change address for paradoxical subreg of memory, the
1132 address might violate the necessary alignment or the access might
1133 be slow. So take this into consideration. */
1134 if ((MEM_P (reg)
1135 && ((! STRICT_ALIGNMENT
1136 && ! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg)))
1137 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1138 || (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER))
1140 alter_subreg (curr_id->operand_loc[nop], false);
1141 return true;
1143 /* Put constant into memory when we have mixed modes. It generates
1144 a better code in most cases as it does not need a secondary
1145 reload memory. It also prevents LRA looping when LRA is using
1146 secondary reload memory again and again. */
1147 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1148 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1150 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1151 alter_subreg (curr_id->operand_loc[nop], false);
1152 return true;
1154 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1155 if there may be a problem accessing OPERAND in the outer
1156 mode. */
1157 if ((REG_P (reg)
1158 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1159 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1160 /* Don't reload paradoxical subregs because we could be looping
1161 having repeatedly final regno out of hard regs range. */
1162 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1163 >= hard_regno_nregs[hard_regno][mode])
1164 && simplify_subreg_regno (hard_regno, GET_MODE (reg),
1165 SUBREG_BYTE (operand), mode) < 0)
1166 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1168 enum op_type type = curr_static_id->operand[nop].type;
1169 /* The class will be defined later in curr_insn_transform. */
1170 enum reg_class rclass
1171 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1173 new_reg = lra_create_new_reg_with_unique_value (reg_mode, reg, rclass,
1174 "subreg reg");
1175 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (new_reg));
1176 if (type != OP_OUT
1177 || GET_MODE_SIZE (GET_MODE (reg)) > GET_MODE_SIZE (mode))
1179 push_to_sequence (before);
1180 lra_emit_move (new_reg, reg);
1181 before = get_insns ();
1182 end_sequence ();
1184 if (type != OP_IN)
1186 start_sequence ();
1187 lra_emit_move (reg, new_reg);
1188 emit_insn (after);
1189 after = get_insns ();
1190 end_sequence ();
1192 SUBREG_REG (operand) = new_reg;
1193 lra_process_new_insns (curr_insn, before, after,
1194 "Inserting subreg reload");
1195 return true;
1197 return false;
1200 /* Return TRUE if X refers for a hard register from SET. */
1201 static bool
1202 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1204 int i, j, x_hard_regno;
1205 enum machine_mode mode;
1206 const char *fmt;
1207 enum rtx_code code;
1209 if (x == NULL_RTX)
1210 return false;
1211 code = GET_CODE (x);
1212 mode = GET_MODE (x);
1213 if (code == SUBREG)
1215 x = SUBREG_REG (x);
1216 code = GET_CODE (x);
1217 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1218 mode = GET_MODE (x);
1221 if (REG_P (x))
1223 x_hard_regno = get_hard_regno (x);
1224 return (x_hard_regno >= 0
1225 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1227 if (MEM_P (x))
1229 struct address_info ad;
1231 decompose_mem_address (&ad, x);
1232 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1233 return true;
1234 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1235 return true;
1237 fmt = GET_RTX_FORMAT (code);
1238 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1240 if (fmt[i] == 'e')
1242 if (uses_hard_regs_p (XEXP (x, i), set))
1243 return true;
1245 else if (fmt[i] == 'E')
1247 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1248 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1249 return true;
1252 return false;
1255 /* Return true if OP is a spilled pseudo. */
1256 static inline bool
1257 spilled_pseudo_p (rtx op)
1259 return (REG_P (op)
1260 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1263 /* Return true if X is a general constant. */
1264 static inline bool
1265 general_constant_p (rtx x)
1267 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1270 /* Cost factor for each additional reload and maximal cost bound for
1271 insn reloads. One might ask about such strange numbers. Their
1272 values occurred historically from former reload pass. */
1273 #define LOSER_COST_FACTOR 6
1274 #define MAX_OVERALL_COST_BOUND 600
1276 /* Major function to choose the current insn alternative and what
1277 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1278 negative we should consider only this alternative. Return false if
1279 we can not choose the alternative or find how to reload the
1280 operands. */
1281 static bool
1282 process_alt_operands (int only_alternative)
1284 bool ok_p = false;
1285 int nop, small_class_operands_num, overall, nalt;
1286 int n_alternatives = curr_static_id->n_alternatives;
1287 int n_operands = curr_static_id->n_operands;
1288 /* LOSERS counts the operands that don't fit this alternative and
1289 would require loading. */
1290 int losers;
1291 /* REJECT is a count of how undesirable this alternative says it is
1292 if any reloading is required. If the alternative matches exactly
1293 then REJECT is ignored, but otherwise it gets this much counted
1294 against it in addition to the reloading needed. */
1295 int reject;
1296 /* The number of elements in the following array. */
1297 int early_clobbered_regs_num;
1298 /* Numbers of operands which are early clobber registers. */
1299 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1300 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1301 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1302 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1303 bool curr_alt_win[MAX_RECOG_OPERANDS];
1304 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1305 int curr_alt_matches[MAX_RECOG_OPERANDS];
1306 /* The number of elements in the following array. */
1307 int curr_alt_dont_inherit_ops_num;
1308 /* Numbers of operands whose reload pseudos should not be inherited. */
1309 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1310 rtx op;
1311 /* The register when the operand is a subreg of register, otherwise the
1312 operand itself. */
1313 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1314 /* The register if the operand is a register or subreg of register,
1315 otherwise NULL. */
1316 rtx operand_reg[MAX_RECOG_OPERANDS];
1317 int hard_regno[MAX_RECOG_OPERANDS];
1318 enum machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1319 int reload_nregs, reload_sum;
1320 bool costly_p;
1321 enum reg_class cl;
1323 /* Calculate some data common for all alternatives to speed up the
1324 function. */
1325 for (nop = 0; nop < n_operands; nop++)
1327 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1328 /* The real hard regno of the operand after the allocation. */
1329 hard_regno[nop] = get_hard_regno (op);
1331 operand_reg[nop] = op;
1332 biggest_mode[nop] = GET_MODE (operand_reg[nop]);
1333 if (GET_CODE (operand_reg[nop]) == SUBREG)
1335 operand_reg[nop] = SUBREG_REG (operand_reg[nop]);
1336 if (GET_MODE_SIZE (biggest_mode[nop])
1337 < GET_MODE_SIZE (GET_MODE (operand_reg[nop])))
1338 biggest_mode[nop] = GET_MODE (operand_reg[nop]);
1340 if (REG_P (operand_reg[nop]))
1341 no_subreg_reg_operand[nop] = operand_reg[nop];
1342 else
1343 operand_reg[nop] = NULL_RTX;
1346 /* The constraints are made of several alternatives. Each operand's
1347 constraint looks like foo,bar,... with commas separating the
1348 alternatives. The first alternatives for all operands go
1349 together, the second alternatives go together, etc.
1351 First loop over alternatives. */
1352 for (nalt = 0; nalt < n_alternatives; nalt++)
1354 /* Loop over operands for one constraint alternative. */
1355 #ifdef HAVE_ATTR_enabled
1356 if (curr_id->alternative_enabled_p != NULL
1357 && ! curr_id->alternative_enabled_p[nalt])
1358 continue;
1359 #endif
1361 if (only_alternative >= 0 && nalt != only_alternative)
1362 continue;
1364 overall = losers = reject = reload_nregs = reload_sum = 0;
1365 for (nop = 0; nop < n_operands; nop++)
1366 reject += (curr_static_id
1367 ->operand_alternative[nalt * n_operands + nop].reject);
1368 early_clobbered_regs_num = 0;
1370 for (nop = 0; nop < n_operands; nop++)
1372 const char *p;
1373 char *end;
1374 int len, c, m, i, opalt_num, this_alternative_matches;
1375 bool win, did_match, offmemok, early_clobber_p;
1376 /* false => this operand can be reloaded somehow for this
1377 alternative. */
1378 bool badop;
1379 /* true => this operand can be reloaded if the alternative
1380 allows regs. */
1381 bool winreg;
1382 /* True if a constant forced into memory would be OK for
1383 this operand. */
1384 bool constmemok;
1385 enum reg_class this_alternative, this_costly_alternative;
1386 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1387 bool this_alternative_match_win, this_alternative_win;
1388 bool this_alternative_offmemok;
1389 enum machine_mode mode;
1391 opalt_num = nalt * n_operands + nop;
1392 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1394 /* Fast track for no constraints at all. */
1395 curr_alt[nop] = NO_REGS;
1396 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1397 curr_alt_win[nop] = true;
1398 curr_alt_match_win[nop] = false;
1399 curr_alt_offmemok[nop] = false;
1400 curr_alt_matches[nop] = -1;
1401 continue;
1404 op = no_subreg_reg_operand[nop];
1405 mode = curr_operand_mode[nop];
1407 win = did_match = winreg = offmemok = constmemok = false;
1408 badop = true;
1410 early_clobber_p = false;
1411 p = curr_static_id->operand_alternative[opalt_num].constraint;
1413 this_costly_alternative = this_alternative = NO_REGS;
1414 /* We update set of possible hard regs besides its class
1415 because reg class might be inaccurate. For example,
1416 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1417 is translated in HI_REGS because classes are merged by
1418 pairs and there is no accurate intermediate class. */
1419 CLEAR_HARD_REG_SET (this_alternative_set);
1420 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1421 this_alternative_win = false;
1422 this_alternative_match_win = false;
1423 this_alternative_offmemok = false;
1424 this_alternative_matches = -1;
1426 /* An empty constraint should be excluded by the fast
1427 track. */
1428 lra_assert (*p != 0 && *p != ',');
1430 /* Scan this alternative's specs for this operand; set WIN
1431 if the operand fits any letter in this alternative.
1432 Otherwise, clear BADOP if this operand could fit some
1433 letter after reloads, or set WINREG if this operand could
1434 fit after reloads provided the constraint allows some
1435 registers. */
1436 costly_p = false;
1439 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1441 case '\0':
1442 len = 0;
1443 break;
1444 case ',':
1445 c = '\0';
1446 break;
1448 case '=': case '+': case '?': case '*': case '!':
1449 case ' ': case '\t':
1450 break;
1452 case '%':
1453 /* We only support one commutative marker, the first
1454 one. We already set commutative above. */
1455 break;
1457 case '&':
1458 early_clobber_p = true;
1459 break;
1461 case '#':
1462 /* Ignore rest of this alternative. */
1463 c = '\0';
1464 break;
1466 case '0': case '1': case '2': case '3': case '4':
1467 case '5': case '6': case '7': case '8': case '9':
1469 int m_hregno;
1470 bool match_p;
1472 m = strtoul (p, &end, 10);
1473 p = end;
1474 len = 0;
1475 lra_assert (nop > m);
1477 this_alternative_matches = m;
1478 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1479 /* We are supposed to match a previous operand.
1480 If we do, we win if that one did. If we do
1481 not, count both of the operands as losers.
1482 (This is too conservative, since most of the
1483 time only a single reload insn will be needed
1484 to make the two operands win. As a result,
1485 this alternative may be rejected when it is
1486 actually desirable.) */
1487 match_p = false;
1488 if (operands_match_p (*curr_id->operand_loc[nop],
1489 *curr_id->operand_loc[m], m_hregno))
1491 /* We should reject matching of an early
1492 clobber operand if the matching operand is
1493 not dying in the insn. */
1494 if (! curr_static_id->operand[m].early_clobber
1495 || operand_reg[nop] == NULL_RTX
1496 || (find_regno_note (curr_insn, REG_DEAD,
1497 REGNO (operand_reg[nop]))
1498 != NULL_RTX))
1499 match_p = true;
1501 if (match_p)
1503 /* If we are matching a non-offsettable
1504 address where an offsettable address was
1505 expected, then we must reject this
1506 combination, because we can't reload
1507 it. */
1508 if (curr_alt_offmemok[m]
1509 && MEM_P (*curr_id->operand_loc[m])
1510 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1511 continue;
1514 else
1516 /* Operands don't match. Both operands must
1517 allow a reload register, otherwise we
1518 cannot make them match. */
1519 if (curr_alt[m] == NO_REGS)
1520 break;
1521 /* Retroactively mark the operand we had to
1522 match as a loser, if it wasn't already and
1523 it wasn't matched to a register constraint
1524 (e.g it might be matched by memory). */
1525 if (curr_alt_win[m]
1526 && (operand_reg[m] == NULL_RTX
1527 || hard_regno[m] < 0))
1529 losers++;
1530 reload_nregs
1531 += (ira_reg_class_max_nregs[curr_alt[m]]
1532 [GET_MODE (*curr_id->operand_loc[m])]);
1535 /* We prefer no matching alternatives because
1536 it gives more freedom in RA. */
1537 if (operand_reg[nop] == NULL_RTX
1538 || (find_regno_note (curr_insn, REG_DEAD,
1539 REGNO (operand_reg[nop]))
1540 == NULL_RTX))
1541 reject += 2;
1543 /* If we have to reload this operand and some
1544 previous operand also had to match the same
1545 thing as this operand, we don't know how to do
1546 that. */
1547 if (!match_p || !curr_alt_win[m])
1549 for (i = 0; i < nop; i++)
1550 if (curr_alt_matches[i] == m)
1551 break;
1552 if (i < nop)
1553 break;
1555 else
1556 did_match = true;
1558 /* This can be fixed with reloads if the operand
1559 we are supposed to match can be fixed with
1560 reloads. */
1561 badop = false;
1562 this_alternative = curr_alt[m];
1563 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1564 break;
1567 case 'p':
1568 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1569 ADDRESS, SCRATCH);
1570 this_alternative = reg_class_subunion[this_alternative][cl];
1571 IOR_HARD_REG_SET (this_alternative_set,
1572 reg_class_contents[cl]);
1573 if (costly_p)
1575 this_costly_alternative
1576 = reg_class_subunion[this_costly_alternative][cl];
1577 IOR_HARD_REG_SET (this_costly_alternative_set,
1578 reg_class_contents[cl]);
1580 win = true;
1581 badop = false;
1582 break;
1584 case TARGET_MEM_CONSTRAINT:
1585 if (MEM_P (op) || spilled_pseudo_p (op))
1586 win = true;
1587 if (CONST_POOL_OK_P (mode, op))
1588 badop = false;
1589 constmemok = true;
1590 break;
1592 case '<':
1593 if (MEM_P (op)
1594 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1595 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1596 win = true;
1597 break;
1599 case '>':
1600 if (MEM_P (op)
1601 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1602 || GET_CODE (XEXP (op, 0)) == POST_INC))
1603 win = true;
1604 break;
1606 /* Memory op whose address is not offsettable. */
1607 case 'V':
1608 if (MEM_P (op)
1609 && ! offsettable_nonstrict_memref_p (op))
1610 win = true;
1611 break;
1613 /* Memory operand whose address is offsettable. */
1614 case 'o':
1615 if ((MEM_P (op)
1616 && offsettable_nonstrict_memref_p (op))
1617 || spilled_pseudo_p (op))
1618 win = true;
1619 if (CONST_POOL_OK_P (mode, op) || MEM_P (op))
1620 badop = false;
1621 constmemok = true;
1622 offmemok = true;
1623 break;
1625 case 'E':
1626 case 'F':
1627 if (GET_CODE (op) == CONST_DOUBLE
1628 || (GET_CODE (op) == CONST_VECTOR
1629 && (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)))
1630 win = true;
1631 break;
1633 case 'G':
1634 case 'H':
1635 if (GET_CODE (op) == CONST_DOUBLE
1636 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1637 win = true;
1638 break;
1640 case 's':
1641 if (CONST_INT_P (op)
1642 || (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode))
1643 break;
1644 case 'i':
1645 if (general_constant_p (op))
1646 win = true;
1647 break;
1649 case 'n':
1650 if (CONST_INT_P (op)
1651 || (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode))
1652 win = true;
1653 break;
1655 case 'I':
1656 case 'J':
1657 case 'K':
1658 case 'L':
1659 case 'M':
1660 case 'N':
1661 case 'O':
1662 case 'P':
1663 if (CONST_INT_P (op)
1664 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1665 win = true;
1666 break;
1668 case 'X':
1669 /* This constraint should be excluded by the fast
1670 track. */
1671 gcc_unreachable ();
1672 break;
1674 case 'g':
1675 if (MEM_P (op)
1676 || general_constant_p (op)
1677 || spilled_pseudo_p (op))
1678 win = true;
1679 /* Drop through into 'r' case. */
1681 case 'r':
1682 this_alternative
1683 = reg_class_subunion[this_alternative][GENERAL_REGS];
1684 IOR_HARD_REG_SET (this_alternative_set,
1685 reg_class_contents[GENERAL_REGS]);
1686 if (costly_p)
1688 this_costly_alternative
1689 = (reg_class_subunion
1690 [this_costly_alternative][GENERAL_REGS]);
1691 IOR_HARD_REG_SET (this_costly_alternative_set,
1692 reg_class_contents[GENERAL_REGS]);
1694 goto reg;
1696 default:
1697 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
1699 #ifdef EXTRA_CONSTRAINT_STR
1700 if (EXTRA_MEMORY_CONSTRAINT (c, p))
1702 if (EXTRA_CONSTRAINT_STR (op, c, p))
1703 win = true;
1704 else if (spilled_pseudo_p (op))
1705 win = true;
1707 /* If we didn't already win, we can reload
1708 constants via force_const_mem, and other
1709 MEMs by reloading the address like for
1710 'o'. */
1711 if (CONST_POOL_OK_P (mode, op) || MEM_P (op))
1712 badop = false;
1713 constmemok = true;
1714 offmemok = true;
1715 break;
1717 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1719 if (EXTRA_CONSTRAINT_STR (op, c, p))
1720 win = true;
1722 /* If we didn't already win, we can reload
1723 the address into a base register. */
1724 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1725 ADDRESS, SCRATCH);
1726 this_alternative
1727 = reg_class_subunion[this_alternative][cl];
1728 IOR_HARD_REG_SET (this_alternative_set,
1729 reg_class_contents[cl]);
1730 if (costly_p)
1732 this_costly_alternative
1733 = (reg_class_subunion
1734 [this_costly_alternative][cl]);
1735 IOR_HARD_REG_SET (this_costly_alternative_set,
1736 reg_class_contents[cl]);
1738 badop = false;
1739 break;
1742 if (EXTRA_CONSTRAINT_STR (op, c, p))
1743 win = true;
1744 #endif
1745 break;
1748 cl = REG_CLASS_FROM_CONSTRAINT (c, p);
1749 this_alternative = reg_class_subunion[this_alternative][cl];
1750 IOR_HARD_REG_SET (this_alternative_set,
1751 reg_class_contents[cl]);
1752 if (costly_p)
1754 this_costly_alternative
1755 = reg_class_subunion[this_costly_alternative][cl];
1756 IOR_HARD_REG_SET (this_costly_alternative_set,
1757 reg_class_contents[cl]);
1759 reg:
1760 if (mode == BLKmode)
1761 break;
1762 winreg = true;
1763 if (REG_P (op))
1765 if (hard_regno[nop] >= 0
1766 && in_hard_reg_set_p (this_alternative_set,
1767 mode, hard_regno[nop]))
1768 win = true;
1769 else if (hard_regno[nop] < 0
1770 && in_class_p (op, this_alternative, NULL))
1771 win = true;
1773 break;
1775 if (c != ' ' && c != '\t')
1776 costly_p = c == '*';
1778 while ((p += len), c);
1780 /* Record which operands fit this alternative. */
1781 if (win)
1783 this_alternative_win = true;
1784 if (operand_reg[nop] != NULL_RTX)
1786 if (hard_regno[nop] >= 0)
1788 if (in_hard_reg_set_p (this_costly_alternative_set,
1789 mode, hard_regno[nop]))
1790 reject++;
1792 else
1794 /* Prefer won reg to spilled pseudo under other equal
1795 conditions. */
1796 reject++;
1797 if (in_class_p (operand_reg[nop],
1798 this_costly_alternative, NULL))
1799 reject++;
1801 /* We simulate the behaviour of old reload here.
1802 Although scratches need hard registers and it
1803 might result in spilling other pseudos, no reload
1804 insns are generated for the scratches. So it
1805 might cost something but probably less than old
1806 reload pass believes. */
1807 if (lra_former_scratch_p (REGNO (operand_reg[nop])))
1808 reject += LOSER_COST_FACTOR;
1811 else if (did_match)
1812 this_alternative_match_win = true;
1813 else
1815 int const_to_mem = 0;
1816 bool no_regs_p;
1818 no_regs_p
1819 = (this_alternative == NO_REGS
1820 || (hard_reg_set_subset_p
1821 (reg_class_contents[this_alternative],
1822 lra_no_alloc_regs)));
1823 /* If this operand accepts a register, and if the
1824 register class has at least one allocatable register,
1825 then this operand can be reloaded. */
1826 if (winreg && !no_regs_p)
1827 badop = false;
1829 if (badop)
1830 goto fail;
1832 this_alternative_offmemok = offmemok;
1833 if (this_costly_alternative != NO_REGS)
1834 reject++;
1835 /* If the operand is dying, has a matching constraint,
1836 and satisfies constraints of the matched operand
1837 which failed to satisfy the own constraints, we do
1838 not need to generate a reload insn for this
1839 operand. */
1840 if (!(this_alternative_matches >= 0
1841 && !curr_alt_win[this_alternative_matches]
1842 && REG_P (op)
1843 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
1844 && (hard_regno[nop] >= 0
1845 ? in_hard_reg_set_p (this_alternative_set,
1846 mode, hard_regno[nop])
1847 : in_class_p (op, this_alternative, NULL))))
1848 losers++;
1849 if (operand_reg[nop] != NULL_RTX
1850 /* Output operands and matched input operands are
1851 not inherited. The following conditions do not
1852 exactly describe the previous statement but they
1853 are pretty close. */
1854 && curr_static_id->operand[nop].type != OP_OUT
1855 && (this_alternative_matches < 0
1856 || curr_static_id->operand[nop].type != OP_IN))
1858 int last_reload = (lra_reg_info[ORIGINAL_REGNO
1859 (operand_reg[nop])]
1860 .last_reload);
1862 if (last_reload > bb_reload_num)
1863 reload_sum += last_reload - bb_reload_num;
1865 /* If this is a constant that is reloaded into the
1866 desired class by copying it to memory first, count
1867 that as another reload. This is consistent with
1868 other code and is required to avoid choosing another
1869 alternative when the constant is moved into memory.
1870 Note that the test here is precisely the same as in
1871 the code below that calls force_const_mem. */
1872 if (CONST_POOL_OK_P (mode, op)
1873 && ((targetm.preferred_reload_class
1874 (op, this_alternative) == NO_REGS)
1875 || no_input_reloads_p))
1877 const_to_mem = 1;
1878 if (! no_regs_p)
1879 losers++;
1882 /* Alternative loses if it requires a type of reload not
1883 permitted for this insn. We can always reload
1884 objects with a REG_UNUSED note. */
1885 if ((curr_static_id->operand[nop].type != OP_IN
1886 && no_output_reloads_p
1887 && ! find_reg_note (curr_insn, REG_UNUSED, op))
1888 || (curr_static_id->operand[nop].type != OP_OUT
1889 && no_input_reloads_p && ! const_to_mem))
1890 goto fail;
1892 /* If we can't reload this value at all, reject this
1893 alternative. Note that we could also lose due to
1894 LIMIT_RELOAD_CLASS, but we don't check that here. */
1895 if (! CONSTANT_P (op) && ! no_regs_p)
1897 if (targetm.preferred_reload_class
1898 (op, this_alternative) == NO_REGS)
1899 reject = MAX_OVERALL_COST_BOUND;
1901 if (curr_static_id->operand[nop].type == OP_OUT
1902 && (targetm.preferred_output_reload_class
1903 (op, this_alternative) == NO_REGS))
1904 reject = MAX_OVERALL_COST_BOUND;
1907 if (! ((const_to_mem && constmemok)
1908 || (MEM_P (op) && offmemok)))
1910 /* We prefer to reload pseudos over reloading other
1911 things, since such reloads may be able to be
1912 eliminated later. So bump REJECT in other cases.
1913 Don't do this in the case where we are forcing a
1914 constant into memory and it will then win since
1915 we don't want to have a different alternative
1916 match then. */
1917 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
1918 reject += 2;
1920 if (! no_regs_p)
1921 reload_nregs
1922 += ira_reg_class_max_nregs[this_alternative][mode];
1925 /* Input reloads can be inherited more often than output
1926 reloads can be removed, so penalize output
1927 reloads. */
1928 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
1929 reject++;
1932 if (early_clobber_p)
1933 reject++;
1934 /* ??? We check early clobbers after processing all operands
1935 (see loop below) and there we update the costs more.
1936 Should we update the cost (may be approximately) here
1937 because of early clobber register reloads or it is a rare
1938 or non-important thing to be worth to do it. */
1939 overall = losers * LOSER_COST_FACTOR + reject;
1940 if ((best_losers == 0 || losers != 0) && best_overall < overall)
1941 goto fail;
1943 curr_alt[nop] = this_alternative;
1944 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
1945 curr_alt_win[nop] = this_alternative_win;
1946 curr_alt_match_win[nop] = this_alternative_match_win;
1947 curr_alt_offmemok[nop] = this_alternative_offmemok;
1948 curr_alt_matches[nop] = this_alternative_matches;
1950 if (this_alternative_matches >= 0
1951 && !did_match && !this_alternative_win)
1952 curr_alt_win[this_alternative_matches] = false;
1954 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
1955 early_clobbered_nops[early_clobbered_regs_num++] = nop;
1957 ok_p = true;
1958 curr_alt_dont_inherit_ops_num = 0;
1959 for (nop = 0; nop < early_clobbered_regs_num; nop++)
1961 int i, j, clobbered_hard_regno;
1962 HARD_REG_SET temp_set;
1964 i = early_clobbered_nops[nop];
1965 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
1966 || hard_regno[i] < 0)
1967 continue;
1968 clobbered_hard_regno = hard_regno[i];
1969 CLEAR_HARD_REG_SET (temp_set);
1970 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
1971 for (j = 0; j < n_operands; j++)
1972 if (j == i
1973 /* We don't want process insides of match_operator and
1974 match_parallel because otherwise we would process
1975 their operands once again generating a wrong
1976 code. */
1977 || curr_static_id->operand[j].is_operator)
1978 continue;
1979 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
1980 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
1981 continue;
1982 else if (uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
1983 break;
1984 if (j >= n_operands)
1985 continue;
1986 /* We need to reload early clobbered register. */
1987 for (j = 0; j < n_operands; j++)
1988 if (curr_alt_matches[j] == i)
1990 curr_alt_match_win[j] = false;
1991 losers++;
1992 overall += LOSER_COST_FACTOR;
1994 if (! curr_alt_match_win[i])
1995 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
1996 else
1998 /* Remember pseudos used for match reloads are never
1999 inherited. */
2000 lra_assert (curr_alt_matches[i] >= 0);
2001 curr_alt_win[curr_alt_matches[i]] = false;
2003 curr_alt_win[i] = curr_alt_match_win[i] = false;
2004 losers++;
2005 overall += LOSER_COST_FACTOR;
2007 small_class_operands_num = 0;
2008 for (nop = 0; nop < n_operands; nop++)
2009 small_class_operands_num
2010 += SMALL_REGISTER_CLASS_P (curr_alt[nop]) ? 1 : 0;
2012 /* If this alternative can be made to work by reloading, and it
2013 needs less reloading than the others checked so far, record
2014 it as the chosen goal for reloading. */
2015 if ((best_losers != 0 && losers == 0)
2016 || (((best_losers == 0 && losers == 0)
2017 || (best_losers != 0 && losers != 0))
2018 && (best_overall > overall
2019 || (best_overall == overall
2020 /* If the cost of the reloads is the same,
2021 prefer alternative which requires minimal
2022 number of small register classes for the
2023 operands. This improves chances of reloads
2024 for insn requiring small register
2025 classes. */
2026 && (small_class_operands_num
2027 < best_small_class_operands_num
2028 || (small_class_operands_num
2029 == best_small_class_operands_num
2030 && (reload_nregs < best_reload_nregs
2031 || (reload_nregs == best_reload_nregs
2032 && best_reload_sum < reload_sum))))))))
2034 for (nop = 0; nop < n_operands; nop++)
2036 goal_alt_win[nop] = curr_alt_win[nop];
2037 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2038 goal_alt_matches[nop] = curr_alt_matches[nop];
2039 goal_alt[nop] = curr_alt[nop];
2040 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2042 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2043 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2044 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2045 goal_alt_swapped = curr_swapped;
2046 best_overall = overall;
2047 best_losers = losers;
2048 best_small_class_operands_num = small_class_operands_num;
2049 best_reload_nregs = reload_nregs;
2050 best_reload_sum = reload_sum;
2051 goal_alt_number = nalt;
2053 if (losers == 0)
2054 /* Everything is satisfied. Do not process alternatives
2055 anymore. */
2056 break;
2057 fail:
2060 return ok_p;
2063 /* Return 1 if ADDR is a valid memory address for mode MODE in address
2064 space AS, and check that each pseudo has the proper kind of hard
2065 reg. */
2066 static int
2067 valid_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2068 rtx addr, addr_space_t as)
2070 #ifdef GO_IF_LEGITIMATE_ADDRESS
2071 lra_assert (ADDR_SPACE_GENERIC_P (as));
2072 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
2073 return 0;
2075 win:
2076 return 1;
2077 #else
2078 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
2079 #endif
2082 /* Return whether address AD is valid. */
2084 static bool
2085 valid_address_p (struct address_info *ad)
2087 /* Some ports do not check displacements for eliminable registers,
2088 so we replace them temporarily with the elimination target. */
2089 rtx saved_base_reg = NULL_RTX;
2090 rtx saved_index_reg = NULL_RTX;
2091 rtx *base_term = strip_subreg (ad->base_term);
2092 rtx *index_term = strip_subreg (ad->index_term);
2093 if (base_term != NULL)
2095 saved_base_reg = *base_term;
2096 lra_eliminate_reg_if_possible (base_term);
2097 if (ad->base_term2 != NULL)
2098 *ad->base_term2 = *ad->base_term;
2100 if (index_term != NULL)
2102 saved_index_reg = *index_term;
2103 lra_eliminate_reg_if_possible (index_term);
2105 bool ok_p = valid_address_p (ad->mode, *ad->outer, ad->as);
2106 if (saved_base_reg != NULL_RTX)
2108 *base_term = saved_base_reg;
2109 if (ad->base_term2 != NULL)
2110 *ad->base_term2 = *ad->base_term;
2112 if (saved_index_reg != NULL_RTX)
2113 *index_term = saved_index_reg;
2114 return ok_p;
2117 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2118 static rtx
2119 base_plus_disp_to_reg (struct address_info *ad)
2121 enum reg_class cl;
2122 rtx new_reg;
2124 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2125 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2126 get_index_code (ad));
2127 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2128 cl, "base + disp");
2129 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2130 return new_reg;
2133 /* Return true if we can add a displacement to address AD, even if that
2134 makes the address invalid. The fix-up code requires any new address
2135 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2136 static bool
2137 can_add_disp_p (struct address_info *ad)
2139 return (!ad->autoinc_p
2140 && ad->segment == NULL
2141 && ad->base == ad->base_term
2142 && ad->disp == ad->disp_term);
2145 /* Make equiv substitution in address AD. Return true if a substitution
2146 was made. */
2147 static bool
2148 equiv_address_substitution (struct address_info *ad)
2150 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2151 HOST_WIDE_INT disp, scale;
2152 bool change_p;
2154 base_term = strip_subreg (ad->base_term);
2155 if (base_term == NULL)
2156 base_reg = new_base_reg = NULL_RTX;
2157 else
2159 base_reg = *base_term;
2160 new_base_reg = get_equiv_substitution (base_reg);
2162 index_term = strip_subreg (ad->index_term);
2163 if (index_term == NULL)
2164 index_reg = new_index_reg = NULL_RTX;
2165 else
2167 index_reg = *index_term;
2168 new_index_reg = get_equiv_substitution (index_reg);
2170 if (base_reg == new_base_reg && index_reg == new_index_reg)
2171 return false;
2172 disp = 0;
2173 change_p = false;
2174 if (lra_dump_file != NULL)
2176 fprintf (lra_dump_file, "Changing address in insn %d ",
2177 INSN_UID (curr_insn));
2178 print_value_slim (lra_dump_file, *ad->outer, 1);
2180 if (base_reg != new_base_reg)
2182 if (REG_P (new_base_reg))
2184 *base_term = new_base_reg;
2185 change_p = true;
2187 else if (GET_CODE (new_base_reg) == PLUS
2188 && REG_P (XEXP (new_base_reg, 0))
2189 && CONST_INT_P (XEXP (new_base_reg, 1))
2190 && can_add_disp_p (ad))
2192 disp += INTVAL (XEXP (new_base_reg, 1));
2193 *base_term = XEXP (new_base_reg, 0);
2194 change_p = true;
2196 if (ad->base_term2 != NULL)
2197 *ad->base_term2 = *ad->base_term;
2199 if (index_reg != new_index_reg)
2201 if (REG_P (new_index_reg))
2203 *index_term = new_index_reg;
2204 change_p = true;
2206 else if (GET_CODE (new_index_reg) == PLUS
2207 && REG_P (XEXP (new_index_reg, 0))
2208 && CONST_INT_P (XEXP (new_index_reg, 1))
2209 && can_add_disp_p (ad)
2210 && (scale = get_index_scale (ad)))
2212 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2213 *index_term = XEXP (new_index_reg, 0);
2214 change_p = true;
2217 if (disp != 0)
2219 if (ad->disp != NULL)
2220 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2221 else
2223 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2224 update_address (ad);
2226 change_p = true;
2228 if (lra_dump_file != NULL)
2230 if (! change_p)
2231 fprintf (lra_dump_file, " -- no change\n");
2232 else
2234 fprintf (lra_dump_file, " on equiv ");
2235 print_value_slim (lra_dump_file, *ad->outer, 1);
2236 fprintf (lra_dump_file, "\n");
2239 return change_p;
2242 /* Major function to make reloads for an address in operand NOP.
2243 The supported cases are:
2245 1) an address that existed before LRA started, at which point it must
2246 have been valid. These addresses are subject to elimination and
2247 may have become invalid due to the elimination offset being out
2248 of range.
2250 2) an address created by forcing a constant to memory (force_const_to_mem).
2251 The initial form of these addresses might not be valid, and it is this
2252 function's job to make them valid.
2254 3) a frame address formed from a register and a (possibly zero)
2255 constant offset. As above, these addresses might not be valid
2256 and this function must make them so.
2258 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2259 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2260 address. Return true for any RTL change. */
2261 static bool
2262 process_address (int nop, rtx *before, rtx *after)
2264 struct address_info ad;
2265 rtx new_reg;
2266 rtx op = *curr_id->operand_loc[nop];
2267 const char *constraint = curr_static_id->operand[nop].constraint;
2268 bool change_p;
2270 if (constraint[0] == 'p'
2271 || EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint))
2272 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2273 else if (MEM_P (op))
2274 decompose_mem_address (&ad, op);
2275 else if (GET_CODE (op) == SUBREG
2276 && MEM_P (SUBREG_REG (op)))
2277 decompose_mem_address (&ad, SUBREG_REG (op));
2278 else
2279 return false;
2280 change_p = equiv_address_substitution (&ad);
2281 if (ad.base_term != NULL
2282 && (process_addr_reg
2283 (ad.base_term, before,
2284 (ad.autoinc_p
2285 && !(REG_P (*ad.base_term)
2286 && find_regno_note (curr_insn, REG_DEAD,
2287 REGNO (*ad.base_term)) != NULL_RTX)
2288 ? after : NULL),
2289 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2290 get_index_code (&ad)))))
2292 change_p = true;
2293 if (ad.base_term2 != NULL)
2294 *ad.base_term2 = *ad.base_term;
2296 if (ad.index_term != NULL
2297 && process_addr_reg (ad.index_term, before, NULL, INDEX_REG_CLASS))
2298 change_p = true;
2300 /* There are three cases where the shape of *AD.INNER may now be invalid:
2302 1) the original address was valid, but either elimination or
2303 equiv_address_substitution applied a displacement that made
2304 it invalid.
2306 2) the address is an invalid symbolic address created by
2307 force_const_to_mem.
2309 3) the address is a frame address with an invalid offset.
2311 All these cases involve a displacement and a non-autoinc address,
2312 so there is no point revalidating other types. */
2313 if (ad.disp == NULL || ad.autoinc_p || valid_address_p (&ad))
2314 return change_p;
2316 /* Any index existed before LRA started, so we can assume that the
2317 presence and shape of the index is valid. */
2318 push_to_sequence (*before);
2319 gcc_assert (ad.segment == NULL);
2320 gcc_assert (ad.disp == ad.disp_term);
2321 if (ad.base == NULL)
2323 if (ad.index == NULL)
2325 int code = -1;
2326 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2327 SCRATCH, SCRATCH);
2328 rtx disp = *ad.disp;
2330 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2331 #ifdef HAVE_lo_sum
2333 rtx insn;
2334 rtx last = get_last_insn ();
2336 /* disp => lo_sum (new_base, disp), case (2) above. */
2337 insn = emit_insn (gen_rtx_SET
2338 (VOIDmode, new_reg,
2339 gen_rtx_HIGH (Pmode, copy_rtx (disp))));
2340 code = recog_memoized (insn);
2341 if (code >= 0)
2343 *ad.disp = gen_rtx_LO_SUM (Pmode, new_reg, disp);
2344 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2346 *ad.disp = disp;
2347 code = -1;
2350 if (code < 0)
2351 delete_insns_since (last);
2353 #endif
2354 if (code < 0)
2356 /* disp => new_base, case (2) above. */
2357 lra_emit_move (new_reg, disp);
2358 *ad.disp = new_reg;
2361 else
2363 /* index * scale + disp => new base + index * scale,
2364 case (1) above. */
2365 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
2366 GET_CODE (*ad.index));
2368 lra_assert (INDEX_REG_CLASS != NO_REGS);
2369 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2370 lra_emit_move (new_reg, *ad.disp);
2371 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2372 new_reg, *ad.index);
2375 else if (ad.index == NULL)
2377 /* base + disp => new base, cases (1) and (3) above. */
2378 /* Another option would be to reload the displacement into an
2379 index register. However, postreload has code to optimize
2380 address reloads that have the same base and different
2381 displacements, so reloading into an index register would
2382 not necessarily be a win. */
2383 new_reg = base_plus_disp_to_reg (&ad);
2384 *ad.inner = new_reg;
2386 else
2388 /* base + scale * index + disp => new base + scale * index,
2389 case (1) above. */
2390 new_reg = base_plus_disp_to_reg (&ad);
2391 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2392 new_reg, *ad.index);
2394 *before = get_insns ();
2395 end_sequence ();
2396 return true;
2399 /* Emit insns to reload VALUE into a new register. VALUE is an
2400 auto-increment or auto-decrement RTX whose operand is a register or
2401 memory location; so reloading involves incrementing that location.
2402 IN is either identical to VALUE, or some cheaper place to reload
2403 value being incremented/decremented from.
2405 INC_AMOUNT is the number to increment or decrement by (always
2406 positive and ignored for POST_MODIFY/PRE_MODIFY).
2408 Return pseudo containing the result. */
2409 static rtx
2410 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
2412 /* REG or MEM to be copied and incremented. */
2413 rtx incloc = XEXP (value, 0);
2414 /* Nonzero if increment after copying. */
2415 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
2416 || GET_CODE (value) == POST_MODIFY);
2417 rtx last;
2418 rtx inc;
2419 rtx add_insn;
2420 int code;
2421 rtx real_in = in == value ? incloc : in;
2422 rtx result;
2423 bool plus_p = true;
2425 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
2427 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
2428 || GET_CODE (XEXP (value, 1)) == MINUS);
2429 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
2430 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
2431 inc = XEXP (XEXP (value, 1), 1);
2433 else
2435 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
2436 inc_amount = -inc_amount;
2438 inc = GEN_INT (inc_amount);
2441 if (! post && REG_P (incloc))
2442 result = incloc;
2443 else
2444 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
2445 "INC/DEC result");
2447 if (real_in != result)
2449 /* First copy the location to the result register. */
2450 lra_assert (REG_P (result));
2451 emit_insn (gen_move_insn (result, real_in));
2454 /* We suppose that there are insns to add/sub with the constant
2455 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
2456 old reload worked with this assumption. If the assumption
2457 becomes wrong, we should use approach in function
2458 base_plus_disp_to_reg. */
2459 if (in == value)
2461 /* See if we can directly increment INCLOC. */
2462 last = get_last_insn ();
2463 add_insn = emit_insn (plus_p
2464 ? gen_add2_insn (incloc, inc)
2465 : gen_sub2_insn (incloc, inc));
2467 code = recog_memoized (add_insn);
2468 if (code >= 0)
2470 if (! post && result != incloc)
2471 emit_insn (gen_move_insn (result, incloc));
2472 return result;
2474 delete_insns_since (last);
2477 /* If couldn't do the increment directly, must increment in RESULT.
2478 The way we do this depends on whether this is pre- or
2479 post-increment. For pre-increment, copy INCLOC to the reload
2480 register, increment it there, then save back. */
2481 if (! post)
2483 if (real_in != result)
2484 emit_insn (gen_move_insn (result, real_in));
2485 if (plus_p)
2486 emit_insn (gen_add2_insn (result, inc));
2487 else
2488 emit_insn (gen_sub2_insn (result, inc));
2489 if (result != incloc)
2490 emit_insn (gen_move_insn (incloc, result));
2492 else
2494 /* Post-increment.
2496 Because this might be a jump insn or a compare, and because
2497 RESULT may not be available after the insn in an input
2498 reload, we must do the incrementing before the insn being
2499 reloaded for.
2501 We have already copied IN to RESULT. Increment the copy in
2502 RESULT, save that back, then decrement RESULT so it has
2503 the original value. */
2504 if (plus_p)
2505 emit_insn (gen_add2_insn (result, inc));
2506 else
2507 emit_insn (gen_sub2_insn (result, inc));
2508 emit_insn (gen_move_insn (incloc, result));
2509 /* Restore non-modified value for the result. We prefer this
2510 way because it does not require an additional hard
2511 register. */
2512 if (plus_p)
2514 if (CONST_INT_P (inc))
2515 emit_insn (gen_add2_insn (result, GEN_INT (-INTVAL (inc))));
2516 else
2517 emit_insn (gen_sub2_insn (result, inc));
2519 else
2520 emit_insn (gen_add2_insn (result, inc));
2522 return result;
2525 /* Swap operands NOP and NOP + 1. */
2526 static inline void
2527 swap_operands (int nop)
2529 enum machine_mode mode = curr_operand_mode[nop];
2530 curr_operand_mode[nop] = curr_operand_mode[nop + 1];
2531 curr_operand_mode[nop + 1] = mode;
2532 rtx x = *curr_id->operand_loc[nop];
2533 *curr_id->operand_loc[nop] = *curr_id->operand_loc[nop + 1];
2534 *curr_id->operand_loc[nop + 1] = x;
2535 /* Swap the duplicates too. */
2536 lra_update_dup (curr_id, nop);
2537 lra_update_dup (curr_id, nop + 1);
2540 /* Main entry point of the constraint code: search the body of the
2541 current insn to choose the best alternative. It is mimicking insn
2542 alternative cost calculation model of former reload pass. That is
2543 because machine descriptions were written to use this model. This
2544 model can be changed in future. Make commutative operand exchange
2545 if it is chosen.
2547 Return true if some RTL changes happened during function call. */
2548 static bool
2549 curr_insn_transform (void)
2551 int i, j, k;
2552 int n_operands;
2553 int n_alternatives;
2554 int commutative;
2555 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
2556 rtx before, after;
2557 bool alt_p = false;
2558 /* Flag that the insn has been changed through a transformation. */
2559 bool change_p;
2560 bool sec_mem_p;
2561 #ifdef SECONDARY_MEMORY_NEEDED
2562 bool use_sec_mem_p;
2563 #endif
2564 int max_regno_before;
2565 int reused_alternative_num;
2567 no_input_reloads_p = no_output_reloads_p = false;
2568 goal_alt_number = -1;
2570 if (check_and_process_move (&change_p, &sec_mem_p))
2571 return change_p;
2573 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
2574 reloads; neither are insns that SET cc0. Insns that use CC0 are
2575 not allowed to have any input reloads. */
2576 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
2577 no_output_reloads_p = true;
2579 #ifdef HAVE_cc0
2580 if (reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
2581 no_input_reloads_p = true;
2582 if (reg_set_p (cc0_rtx, PATTERN (curr_insn)))
2583 no_output_reloads_p = true;
2584 #endif
2586 n_operands = curr_static_id->n_operands;
2587 n_alternatives = curr_static_id->n_alternatives;
2589 /* Just return "no reloads" if insn has no operands with
2590 constraints. */
2591 if (n_operands == 0 || n_alternatives == 0)
2592 return false;
2594 max_regno_before = max_reg_num ();
2596 for (i = 0; i < n_operands; i++)
2598 goal_alt_matched[i][0] = -1;
2599 goal_alt_matches[i] = -1;
2602 commutative = curr_static_id->commutative;
2604 /* Now see what we need for pseudos that didn't get hard regs or got
2605 the wrong kind of hard reg. For this, we must consider all the
2606 operands together against the register constraints. */
2608 best_losers = best_overall = MAX_RECOG_OPERANDS * 2 + MAX_OVERALL_COST_BOUND;
2609 best_small_class_operands_num = best_reload_sum = 0;
2611 curr_swapped = false;
2612 goal_alt_swapped = false;
2614 /* Make equivalence substitution and memory subreg elimination
2615 before address processing because an address legitimacy can
2616 depend on memory mode. */
2617 for (i = 0; i < n_operands; i++)
2619 rtx op = *curr_id->operand_loc[i];
2620 rtx subst, old = op;
2621 bool op_change_p = false;
2623 if (GET_CODE (old) == SUBREG)
2624 old = SUBREG_REG (old);
2625 subst = get_equiv_substitution (old);
2626 if (subst != old)
2628 subst = copy_rtx (subst);
2629 lra_assert (REG_P (old));
2630 if (GET_CODE (op) == SUBREG)
2631 SUBREG_REG (op) = subst;
2632 else
2633 *curr_id->operand_loc[i] = subst;
2634 if (lra_dump_file != NULL)
2636 fprintf (lra_dump_file,
2637 "Changing pseudo %d in operand %i of insn %u on equiv ",
2638 REGNO (old), i, INSN_UID (curr_insn));
2639 print_value_slim (lra_dump_file, subst, 1);
2640 fprintf (lra_dump_file, "\n");
2642 op_change_p = change_p = true;
2644 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
2646 change_p = true;
2647 lra_update_dup (curr_id, i);
2651 /* Reload address registers and displacements. We do it before
2652 finding an alternative because of memory constraints. */
2653 before = after = NULL_RTX;
2654 for (i = 0; i < n_operands; i++)
2655 if (! curr_static_id->operand[i].is_operator
2656 && process_address (i, &before, &after))
2658 change_p = true;
2659 lra_update_dup (curr_id, i);
2662 if (change_p)
2663 /* If we've changed the instruction then any alternative that
2664 we chose previously may no longer be valid. */
2665 lra_set_used_insn_alternative (curr_insn, -1);
2667 try_swapped:
2669 reused_alternative_num = curr_id->used_insn_alternative;
2670 if (lra_dump_file != NULL && reused_alternative_num >= 0)
2671 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
2672 reused_alternative_num, INSN_UID (curr_insn));
2674 if (process_alt_operands (reused_alternative_num))
2675 alt_p = true;
2677 /* If insn is commutative (it's safe to exchange a certain pair of
2678 operands) then we need to try each alternative twice, the second
2679 time matching those two operands as if we had exchanged them. To
2680 do this, really exchange them in operands.
2682 If we have just tried the alternatives the second time, return
2683 operands to normal and drop through. */
2685 if (reused_alternative_num < 0 && commutative >= 0)
2687 curr_swapped = !curr_swapped;
2688 if (curr_swapped)
2690 swap_operands (commutative);
2691 goto try_swapped;
2693 else
2694 swap_operands (commutative);
2697 /* The operands don't meet the constraints. goal_alt describes the
2698 alternative that we could reach by reloading the fewest operands.
2699 Reload so as to fit it. */
2701 if (! alt_p && ! sec_mem_p)
2703 /* No alternative works with reloads?? */
2704 if (INSN_CODE (curr_insn) >= 0)
2705 fatal_insn ("unable to generate reloads for:", curr_insn);
2706 error_for_asm (curr_insn,
2707 "inconsistent operand constraints in an %<asm%>");
2708 /* Avoid further trouble with this insn. */
2709 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
2710 lra_invalidate_insn_data (curr_insn);
2711 return true;
2714 /* If the best alternative is with operands 1 and 2 swapped, swap
2715 them. Update the operand numbers of any reloads already
2716 pushed. */
2718 if (goal_alt_swapped)
2720 if (lra_dump_file != NULL)
2721 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
2722 INSN_UID (curr_insn));
2724 /* Swap the duplicates too. */
2725 swap_operands (commutative);
2726 change_p = true;
2729 #ifdef SECONDARY_MEMORY_NEEDED
2730 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
2731 too conservatively. So we use the secondary memory only if there
2732 is no any alternative without reloads. */
2733 use_sec_mem_p = false;
2734 if (! alt_p)
2735 use_sec_mem_p = true;
2736 else if (sec_mem_p)
2738 for (i = 0; i < n_operands; i++)
2739 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
2740 break;
2741 use_sec_mem_p = i < n_operands;
2744 if (use_sec_mem_p)
2746 rtx new_reg, set, src, dest;
2747 enum machine_mode sec_mode;
2749 lra_assert (sec_mem_p);
2750 set = single_set (curr_insn);
2751 lra_assert (set != NULL_RTX && ! side_effects_p (set));
2752 dest = SET_DEST (set);
2753 src = SET_SRC (set);
2754 #ifdef SECONDARY_MEMORY_NEEDED_MODE
2755 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src));
2756 #else
2757 sec_mode = GET_MODE (src);
2758 #endif
2759 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
2760 NO_REGS, "secondary");
2761 /* If the mode is changed, it should be wider. */
2762 lra_assert (GET_MODE_SIZE (GET_MODE (new_reg))
2763 >= GET_MODE_SIZE (GET_MODE (src)));
2764 after = emit_spill_move (false, new_reg, dest);
2765 lra_process_new_insns (curr_insn, NULL_RTX, after,
2766 "Inserting the sec. move");
2767 before = emit_spill_move (true, new_reg, src);
2768 lra_process_new_insns (curr_insn, before, NULL_RTX, "Changing on");
2769 lra_set_insn_deleted (curr_insn);
2770 return true;
2772 #endif
2774 lra_assert (goal_alt_number >= 0);
2775 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
2777 if (lra_dump_file != NULL)
2779 const char *p;
2781 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
2782 goal_alt_number, INSN_UID (curr_insn));
2783 for (i = 0; i < n_operands; i++)
2785 p = (curr_static_id->operand_alternative
2786 [goal_alt_number * n_operands + i].constraint);
2787 if (*p == '\0')
2788 continue;
2789 fprintf (lra_dump_file, " (%d) ", i);
2790 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
2791 fputc (*p, lra_dump_file);
2793 fprintf (lra_dump_file, "\n");
2796 /* Right now, for any pair of operands I and J that are required to
2797 match, with J < I, goal_alt_matches[I] is J. Add I to
2798 goal_alt_matched[J]. */
2800 for (i = 0; i < n_operands; i++)
2801 if ((j = goal_alt_matches[i]) >= 0)
2803 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
2805 /* We allow matching one output operand and several input
2806 operands. */
2807 lra_assert (k == 0
2808 || (curr_static_id->operand[j].type == OP_OUT
2809 && curr_static_id->operand[i].type == OP_IN
2810 && (curr_static_id->operand
2811 [goal_alt_matched[j][0]].type == OP_IN)));
2812 goal_alt_matched[j][k] = i;
2813 goal_alt_matched[j][k + 1] = -1;
2816 for (i = 0; i < n_operands; i++)
2817 goal_alt_win[i] |= goal_alt_match_win[i];
2819 /* Any constants that aren't allowed and can't be reloaded into
2820 registers are here changed into memory references. */
2821 for (i = 0; i < n_operands; i++)
2822 if (goal_alt_win[i])
2824 int regno;
2825 enum reg_class new_class;
2826 rtx reg = *curr_id->operand_loc[i];
2828 if (GET_CODE (reg) == SUBREG)
2829 reg = SUBREG_REG (reg);
2831 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
2833 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
2835 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
2837 lra_assert (ok_p);
2838 change_class (regno, new_class, " Change", true);
2842 else
2844 const char *constraint;
2845 char c;
2846 rtx op = *curr_id->operand_loc[i];
2847 rtx subreg = NULL_RTX;
2848 enum machine_mode mode = curr_operand_mode[i];
2850 if (GET_CODE (op) == SUBREG)
2852 subreg = op;
2853 op = SUBREG_REG (op);
2854 mode = GET_MODE (op);
2857 if (CONST_POOL_OK_P (mode, op)
2858 && ((targetm.preferred_reload_class
2859 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
2860 || no_input_reloads_p))
2862 rtx tem = force_const_mem (mode, op);
2864 change_p = true;
2865 if (subreg != NULL_RTX)
2866 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
2868 *curr_id->operand_loc[i] = tem;
2869 lra_update_dup (curr_id, i);
2870 process_address (i, &before, &after);
2872 /* If the alternative accepts constant pool refs directly
2873 there will be no reload needed at all. */
2874 if (subreg != NULL_RTX)
2875 continue;
2876 /* Skip alternatives before the one requested. */
2877 constraint = (curr_static_id->operand_alternative
2878 [goal_alt_number * n_operands + i].constraint);
2879 for (;
2880 (c = *constraint) && c != ',' && c != '#';
2881 constraint += CONSTRAINT_LEN (c, constraint))
2883 if (c == TARGET_MEM_CONSTRAINT || c == 'o')
2884 break;
2885 #ifdef EXTRA_CONSTRAINT_STR
2886 if (EXTRA_MEMORY_CONSTRAINT (c, constraint)
2887 && EXTRA_CONSTRAINT_STR (tem, c, constraint))
2888 break;
2889 #endif
2891 if (c == '\0' || c == ',' || c == '#')
2892 continue;
2894 goal_alt_win[i] = true;
2898 for (i = 0; i < n_operands; i++)
2900 rtx old, new_reg;
2901 rtx op = *curr_id->operand_loc[i];
2903 if (goal_alt_win[i])
2905 if (goal_alt[i] == NO_REGS
2906 && REG_P (op)
2907 /* When we assign NO_REGS it means that we will not
2908 assign a hard register to the scratch pseudo by
2909 assigment pass and the scratch pseudo will be
2910 spilled. Spilled scratch pseudos are transformed
2911 back to scratches at the LRA end. */
2912 && lra_former_scratch_operand_p (curr_insn, i))
2913 change_class (REGNO (op), NO_REGS, " Change", true);
2914 continue;
2917 /* Operands that match previous ones have already been handled. */
2918 if (goal_alt_matches[i] >= 0)
2919 continue;
2921 /* We should not have an operand with a non-offsettable address
2922 appearing where an offsettable address will do. It also may
2923 be a case when the address should be special in other words
2924 not a general one (e.g. it needs no index reg). */
2925 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
2927 enum reg_class rclass;
2928 rtx *loc = &XEXP (op, 0);
2929 enum rtx_code code = GET_CODE (*loc);
2931 push_to_sequence (before);
2932 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
2933 MEM, SCRATCH);
2934 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
2935 new_reg = emit_inc (rclass, *loc, *loc,
2936 /* This value does not matter for MODIFY. */
2937 GET_MODE_SIZE (GET_MODE (op)));
2938 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass,
2939 "offsetable address", &new_reg))
2940 lra_emit_move (new_reg, *loc);
2941 before = get_insns ();
2942 end_sequence ();
2943 *loc = new_reg;
2944 lra_update_dup (curr_id, i);
2946 else if (goal_alt_matched[i][0] == -1)
2948 enum machine_mode mode;
2949 rtx reg, *loc;
2950 int hard_regno, byte;
2951 enum op_type type = curr_static_id->operand[i].type;
2953 loc = curr_id->operand_loc[i];
2954 mode = curr_operand_mode[i];
2955 if (GET_CODE (*loc) == SUBREG)
2957 reg = SUBREG_REG (*loc);
2958 byte = SUBREG_BYTE (*loc);
2959 if (REG_P (reg)
2960 /* Strict_low_part requires reload the register not
2961 the sub-register. */
2962 && (curr_static_id->operand[i].strict_low
2963 || (GET_MODE_SIZE (mode)
2964 <= GET_MODE_SIZE (GET_MODE (reg))
2965 && (hard_regno
2966 = get_try_hard_regno (REGNO (reg))) >= 0
2967 && (simplify_subreg_regno
2968 (hard_regno,
2969 GET_MODE (reg), byte, mode) < 0)
2970 && (goal_alt[i] == NO_REGS
2971 || (simplify_subreg_regno
2972 (ira_class_hard_regs[goal_alt[i]][0],
2973 GET_MODE (reg), byte, mode) >= 0)))))
2975 loc = &SUBREG_REG (*loc);
2976 mode = GET_MODE (*loc);
2979 old = *loc;
2980 if (get_reload_reg (type, mode, old, goal_alt[i], "", &new_reg)
2981 && type != OP_OUT)
2983 push_to_sequence (before);
2984 lra_emit_move (new_reg, old);
2985 before = get_insns ();
2986 end_sequence ();
2988 *loc = new_reg;
2989 if (type != OP_IN
2990 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
2992 start_sequence ();
2993 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
2994 emit_insn (after);
2995 after = get_insns ();
2996 end_sequence ();
2997 *loc = new_reg;
2999 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3000 if (goal_alt_dont_inherit_ops[j] == i)
3002 lra_set_regno_unique_value (REGNO (new_reg));
3003 break;
3005 lra_update_dup (curr_id, i);
3007 else if (curr_static_id->operand[i].type == OP_IN
3008 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3009 == OP_OUT))
3011 signed char arr[2];
3013 arr[0] = i;
3014 arr[1] = -1;
3015 match_reload (goal_alt_matched[i][0], arr,
3016 goal_alt[i], &before, &after);
3018 else if (curr_static_id->operand[i].type == OP_OUT
3019 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3020 == OP_IN))
3021 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3022 else
3023 /* We must generate code in any case when function
3024 process_alt_operands decides that it is possible. */
3025 gcc_unreachable ();
3027 if (before != NULL_RTX || after != NULL_RTX
3028 || max_regno_before != max_reg_num ())
3029 change_p = true;
3030 if (change_p)
3032 lra_update_operator_dups (curr_id);
3033 /* Something changes -- process the insn. */
3034 lra_update_insn_regno_info (curr_insn);
3036 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3037 return change_p;
3040 /* Return true if X is in LIST. */
3041 static bool
3042 in_list_p (rtx x, rtx list)
3044 for (; list != NULL_RTX; list = XEXP (list, 1))
3045 if (XEXP (list, 0) == x)
3046 return true;
3047 return false;
3050 /* Return true if X contains an allocatable hard register (if
3051 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3052 static bool
3053 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3055 int i, j;
3056 const char *fmt;
3057 enum rtx_code code;
3059 code = GET_CODE (x);
3060 if (REG_P (x))
3062 int regno = REGNO (x);
3063 HARD_REG_SET alloc_regs;
3065 if (hard_reg_p)
3067 if (regno >= FIRST_PSEUDO_REGISTER)
3068 regno = lra_get_regno_hard_regno (regno);
3069 if (regno < 0)
3070 return false;
3071 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3072 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3074 else
3076 if (regno < FIRST_PSEUDO_REGISTER)
3077 return false;
3078 if (! spilled_p)
3079 return true;
3080 return lra_get_regno_hard_regno (regno) < 0;
3083 fmt = GET_RTX_FORMAT (code);
3084 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3086 if (fmt[i] == 'e')
3088 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3089 return true;
3091 else if (fmt[i] == 'E')
3093 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3094 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3095 return true;
3098 return false;
3101 /* Process all regs in debug location *LOC and change them on
3102 equivalent substitution. Return true if any change was done. */
3103 static bool
3104 debug_loc_equivalence_change_p (rtx *loc)
3106 rtx subst, reg, x = *loc;
3107 bool result = false;
3108 enum rtx_code code = GET_CODE (x);
3109 const char *fmt;
3110 int i, j;
3112 if (code == SUBREG)
3114 reg = SUBREG_REG (x);
3115 if ((subst = get_equiv_substitution (reg)) != reg
3116 && GET_MODE (subst) == VOIDmode)
3118 /* We cannot reload debug location. Simplify subreg here
3119 while we know the inner mode. */
3120 *loc = simplify_gen_subreg (GET_MODE (x), subst,
3121 GET_MODE (reg), SUBREG_BYTE (x));
3122 return true;
3125 if (code == REG && (subst = get_equiv_substitution (x)) != x)
3127 *loc = subst;
3128 return true;
3131 /* Scan all the operand sub-expressions. */
3132 fmt = GET_RTX_FORMAT (code);
3133 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3135 if (fmt[i] == 'e')
3136 result = debug_loc_equivalence_change_p (&XEXP (x, i)) || result;
3137 else if (fmt[i] == 'E')
3138 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3139 result
3140 = debug_loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
3142 return result;
3145 /* Maximum allowed number of constraint pass iterations after the last
3146 spill pass. It is for preventing LRA cycling in a bug case. */
3147 #define MAX_CONSTRAINT_ITERATION_NUMBER 15
3149 /* Maximum number of generated reload insns per an insn. It is for
3150 preventing this pass cycling in a bug case. */
3151 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
3153 /* The current iteration number of this LRA pass. */
3154 int lra_constraint_iter;
3156 /* The current iteration number of this LRA pass after the last spill
3157 pass. */
3158 int lra_constraint_iter_after_spill;
3160 /* True if we substituted equiv which needs checking register
3161 allocation correctness because the equivalent value contains
3162 allocatable hard registers or when we restore multi-register
3163 pseudo. */
3164 bool lra_risky_transformations_p;
3166 /* Return true if REGNO is referenced in more than one block. */
3167 static bool
3168 multi_block_pseudo_p (int regno)
3170 basic_block bb = NULL;
3171 unsigned int uid;
3172 bitmap_iterator bi;
3174 if (regno < FIRST_PSEUDO_REGISTER)
3175 return false;
3177 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
3178 if (bb == NULL)
3179 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
3180 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
3181 return true;
3182 return false;
3185 /* Return true if X contains a pseudo dying in INSN. */
3186 static bool
3187 dead_pseudo_p (rtx x, rtx insn)
3189 int i, j;
3190 const char *fmt;
3191 enum rtx_code code;
3193 if (REG_P (x))
3194 return (insn != NULL_RTX
3195 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
3196 code = GET_CODE (x);
3197 fmt = GET_RTX_FORMAT (code);
3198 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3200 if (fmt[i] == 'e')
3202 if (dead_pseudo_p (XEXP (x, i), insn))
3203 return true;
3205 else if (fmt[i] == 'E')
3207 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3208 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
3209 return true;
3212 return false;
3215 /* Return true if INSN contains a dying pseudo in INSN right hand
3216 side. */
3217 static bool
3218 insn_rhs_dead_pseudo_p (rtx insn)
3220 rtx set = single_set (insn);
3222 gcc_assert (set != NULL);
3223 return dead_pseudo_p (SET_SRC (set), insn);
3226 /* Return true if any init insn of REGNO contains a dying pseudo in
3227 insn right hand side. */
3228 static bool
3229 init_insn_rhs_dead_pseudo_p (int regno)
3231 rtx insns = ira_reg_equiv[regno].init_insns;
3233 if (insns == NULL)
3234 return false;
3235 if (INSN_P (insns))
3236 return insn_rhs_dead_pseudo_p (insns);
3237 for (; insns != NULL_RTX; insns = XEXP (insns, 1))
3238 if (insn_rhs_dead_pseudo_p (XEXP (insns, 0)))
3239 return true;
3240 return false;
3243 /* Entry function of LRA constraint pass. Return true if the
3244 constraint pass did change the code. */
3245 bool
3246 lra_constraints (bool first_p)
3248 bool changed_p;
3249 int i, hard_regno, new_insns_num;
3250 unsigned int min_len, new_min_len;
3251 rtx set, x, dest_reg;
3252 basic_block last_bb;
3254 lra_constraint_iter++;
3255 if (lra_dump_file != NULL)
3256 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
3257 lra_constraint_iter);
3258 lra_constraint_iter_after_spill++;
3259 if (lra_constraint_iter_after_spill > MAX_CONSTRAINT_ITERATION_NUMBER)
3260 internal_error
3261 ("Maximum number of LRA constraint passes is achieved (%d)\n",
3262 MAX_CONSTRAINT_ITERATION_NUMBER);
3263 changed_p = false;
3264 lra_risky_transformations_p = false;
3265 new_insn_uid_start = get_max_uid ();
3266 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
3267 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3268 if (lra_reg_info[i].nrefs != 0)
3270 ira_reg_equiv[i].profitable_p = true;
3271 if ((hard_regno = lra_get_regno_hard_regno (i)) >= 0)
3273 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
3275 for (j = 0; j < nregs; j++)
3276 df_set_regs_ever_live (hard_regno + j, true);
3278 else if ((x = get_equiv_substitution (regno_reg_rtx[i])) != NULL_RTX)
3280 bool pseudo_p = contains_reg_p (x, false, false);
3281 rtx set, insn;
3283 /* We don't use DF for compilation speed sake. So it is
3284 problematic to update live info when we use an
3285 equivalence containing pseudos in more than one BB. */
3286 if ((pseudo_p && multi_block_pseudo_p (i))
3287 /* If it is not a reverse equivalence, we check that a
3288 pseudo in rhs of the init insn is not dying in the
3289 insn. Otherwise, the live info at the beginning of
3290 the corresponding BB might be wrong after we
3291 removed the insn. When the equiv can be a
3292 constant, the right hand side of the init insn can
3293 be a pseudo. */
3294 || (! ((insn = ira_reg_equiv[i].init_insns) != NULL_RTX
3295 && INSN_P (insn)
3296 && (set = single_set (insn)) != NULL_RTX
3297 && REG_P (SET_DEST (set))
3298 && (int) REGNO (SET_DEST (set)) == i)
3299 && init_insn_rhs_dead_pseudo_p (i)))
3300 ira_reg_equiv[i].defined_p = false;
3301 else if (! first_p && pseudo_p)
3302 /* After RTL transformation, we can not guarantee that
3303 pseudo in the substitution was not reloaded which
3304 might make equivalence invalid. For example, in
3305 reverse equiv of p0
3307 p0 <- ...
3309 equiv_mem <- p0
3311 the memory address register was reloaded before the
3312 2nd insn. */
3313 ira_reg_equiv[i].defined_p = false;
3314 if (contains_reg_p (x, false, true))
3315 ira_reg_equiv[i].profitable_p = false;
3318 lra_eliminate (false);
3319 min_len = lra_insn_stack_length ();
3320 new_insns_num = 0;
3321 last_bb = NULL;
3322 changed_p = false;
3323 while ((new_min_len = lra_insn_stack_length ()) != 0)
3325 curr_insn = lra_pop_insn ();
3326 --new_min_len;
3327 curr_bb = BLOCK_FOR_INSN (curr_insn);
3328 if (curr_bb != last_bb)
3330 last_bb = curr_bb;
3331 bb_reload_num = lra_curr_reload_num;
3333 if (min_len > new_min_len)
3335 min_len = new_min_len;
3336 new_insns_num = 0;
3338 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
3339 internal_error
3340 ("Max. number of generated reload insns per insn is achieved (%d)\n",
3341 MAX_RELOAD_INSNS_NUMBER);
3342 new_insns_num++;
3343 if (DEBUG_INSN_P (curr_insn))
3345 /* We need to check equivalence in debug insn and change
3346 pseudo to the equivalent value if necessary. */
3347 curr_id = lra_get_insn_recog_data (curr_insn);
3348 if (debug_loc_equivalence_change_p (curr_id->operand_loc[0]))
3349 changed_p = true;
3351 else if (INSN_P (curr_insn))
3353 if ((set = single_set (curr_insn)) != NULL_RTX)
3355 dest_reg = SET_DEST (set);
3356 /* The equivalence pseudo could be set up as SUBREG in a
3357 case when it is a call restore insn in a mode
3358 different from the pseudo mode. */
3359 if (GET_CODE (dest_reg) == SUBREG)
3360 dest_reg = SUBREG_REG (dest_reg);
3361 if ((REG_P (dest_reg)
3362 && (x = get_equiv_substitution (dest_reg)) != dest_reg
3363 /* Remove insns which set up a pseudo whose value
3364 can not be changed. Such insns might be not in
3365 init_insns because we don't update equiv data
3366 during insn transformations.
3368 As an example, let suppose that a pseudo got
3369 hard register and on the 1st pass was not
3370 changed to equivalent constant. We generate an
3371 additional insn setting up the pseudo because of
3372 secondary memory movement. Then the pseudo is
3373 spilled and we use the equiv constant. In this
3374 case we should remove the additional insn and
3375 this insn is not init_insns list. */
3376 && (! MEM_P (x) || MEM_READONLY_P (x)
3377 || in_list_p (curr_insn,
3378 ira_reg_equiv
3379 [REGNO (dest_reg)].init_insns)))
3380 || (((x = get_equiv_substitution (SET_SRC (set)))
3381 != SET_SRC (set))
3382 && in_list_p (curr_insn,
3383 ira_reg_equiv
3384 [REGNO (SET_SRC (set))].init_insns)))
3386 /* This is equiv init insn of pseudo which did not get a
3387 hard register -- remove the insn. */
3388 if (lra_dump_file != NULL)
3390 fprintf (lra_dump_file,
3391 " Removing equiv init insn %i (freq=%d)\n",
3392 INSN_UID (curr_insn),
3393 BLOCK_FOR_INSN (curr_insn)->frequency);
3394 debug_rtl_slim (lra_dump_file,
3395 curr_insn, curr_insn, -1, 0);
3397 if (contains_reg_p (x, true, false))
3398 lra_risky_transformations_p = true;
3399 lra_set_insn_deleted (curr_insn);
3400 continue;
3403 curr_id = lra_get_insn_recog_data (curr_insn);
3404 curr_static_id = curr_id->insn_static_data;
3405 init_curr_insn_input_reloads ();
3406 init_curr_operand_mode ();
3407 if (curr_insn_transform ())
3408 changed_p = true;
3411 /* If we used a new hard regno, changed_p should be true because the
3412 hard reg is assigned to a new pseudo. */
3413 #ifdef ENABLE_CHECKING
3414 if (! changed_p)
3416 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3417 if (lra_reg_info[i].nrefs != 0
3418 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
3420 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
3422 for (j = 0; j < nregs; j++)
3423 lra_assert (df_regs_ever_live_p (hard_regno + j));
3426 #endif
3427 return changed_p;
3430 /* Initiate the LRA constraint pass. It is done once per
3431 function. */
3432 void
3433 lra_constraints_init (void)
3437 /* Finalize the LRA constraint pass. It is done once per
3438 function. */
3439 void
3440 lra_constraints_finish (void)
3446 /* This page contains code to do inheritance/split
3447 transformations. */
3449 /* Number of reloads passed so far in current EBB. */
3450 static int reloads_num;
3452 /* Number of calls passed so far in current EBB. */
3453 static int calls_num;
3455 /* Current reload pseudo check for validity of elements in
3456 USAGE_INSNS. */
3457 static int curr_usage_insns_check;
3459 /* Info about last usage of registers in EBB to do inheritance/split
3460 transformation. Inheritance transformation is done from a spilled
3461 pseudo and split transformations from a hard register or a pseudo
3462 assigned to a hard register. */
3463 struct usage_insns
3465 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
3466 value INSNS is valid. The insns is chain of optional debug insns
3467 and a finishing non-debug insn using the corresponding reg. */
3468 int check;
3469 /* Value of global reloads_num at the last insn in INSNS. */
3470 int reloads_num;
3471 /* Value of global reloads_nums at the last insn in INSNS. */
3472 int calls_num;
3473 /* It can be true only for splitting. And it means that the restore
3474 insn should be put after insn given by the following member. */
3475 bool after_p;
3476 /* Next insns in the current EBB which use the original reg and the
3477 original reg value is not changed between the current insn and
3478 the next insns. In order words, e.g. for inheritance, if we need
3479 to use the original reg value again in the next insns we can try
3480 to use the value in a hard register from a reload insn of the
3481 current insn. */
3482 rtx insns;
3485 /* Map: regno -> corresponding pseudo usage insns. */
3486 static struct usage_insns *usage_insns;
3488 static void
3489 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
3491 usage_insns[regno].check = curr_usage_insns_check;
3492 usage_insns[regno].insns = insn;
3493 usage_insns[regno].reloads_num = reloads_num;
3494 usage_insns[regno].calls_num = calls_num;
3495 usage_insns[regno].after_p = after_p;
3498 /* The function is used to form list REGNO usages which consists of
3499 optional debug insns finished by a non-debug insn using REGNO.
3500 RELOADS_NUM is current number of reload insns processed so far. */
3501 static void
3502 add_next_usage_insn (int regno, rtx insn, int reloads_num)
3504 rtx next_usage_insns;
3506 if (usage_insns[regno].check == curr_usage_insns_check
3507 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
3508 && DEBUG_INSN_P (insn))
3510 /* Check that we did not add the debug insn yet. */
3511 if (next_usage_insns != insn
3512 && (GET_CODE (next_usage_insns) != INSN_LIST
3513 || XEXP (next_usage_insns, 0) != insn))
3514 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
3515 next_usage_insns);
3517 else if (NONDEBUG_INSN_P (insn))
3518 setup_next_usage_insn (regno, insn, reloads_num, false);
3519 else
3520 usage_insns[regno].check = 0;
3523 /* Replace all references to register OLD_REGNO in *LOC with pseudo
3524 register NEW_REG. Return true if any change was made. */
3525 static bool
3526 substitute_pseudo (rtx *loc, int old_regno, rtx new_reg)
3528 rtx x = *loc;
3529 bool result = false;
3530 enum rtx_code code;
3531 const char *fmt;
3532 int i, j;
3534 if (x == NULL_RTX)
3535 return false;
3537 code = GET_CODE (x);
3538 if (code == REG && (int) REGNO (x) == old_regno)
3540 enum machine_mode mode = GET_MODE (*loc);
3541 enum machine_mode inner_mode = GET_MODE (new_reg);
3543 if (mode != inner_mode)
3545 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
3546 || ! SCALAR_INT_MODE_P (inner_mode))
3547 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
3548 else
3549 new_reg = gen_lowpart_SUBREG (mode, new_reg);
3551 *loc = new_reg;
3552 return true;
3555 /* Scan all the operand sub-expressions. */
3556 fmt = GET_RTX_FORMAT (code);
3557 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3559 if (fmt[i] == 'e')
3561 if (substitute_pseudo (&XEXP (x, i), old_regno, new_reg))
3562 result = true;
3564 else if (fmt[i] == 'E')
3566 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3567 if (substitute_pseudo (&XVECEXP (x, i, j), old_regno, new_reg))
3568 result = true;
3571 return result;
3574 /* Registers involved in inheritance/split in the current EBB
3575 (inheritance/split pseudos and original registers). */
3576 static bitmap_head check_only_regs;
3578 /* Do inheritance transformations for insn INSN, which defines (if
3579 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
3580 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
3581 form as the "insns" field of usage_insns. Return true if we
3582 succeed in such transformation.
3584 The transformations look like:
3586 p <- ... i <- ...
3587 ... p <- i (new insn)
3588 ... =>
3589 <- ... p ... <- ... i ...
3591 ... i <- p (new insn)
3592 <- ... p ... <- ... i ...
3593 ... =>
3594 <- ... p ... <- ... i ...
3595 where p is a spilled original pseudo and i is a new inheritance pseudo.
3598 The inheritance pseudo has the smallest class of two classes CL and
3599 class of ORIGINAL REGNO. */
3600 static bool
3601 inherit_reload_reg (bool def_p, int original_regno,
3602 enum reg_class cl, rtx insn, rtx next_usage_insns)
3604 enum reg_class rclass = lra_get_allocno_class (original_regno);
3605 rtx original_reg = regno_reg_rtx[original_regno];
3606 rtx new_reg, new_insns, usage_insn;
3608 lra_assert (! usage_insns[original_regno].after_p);
3609 if (lra_dump_file != NULL)
3610 fprintf (lra_dump_file,
3611 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
3612 if (! ira_reg_classes_intersect_p[cl][rclass])
3614 if (lra_dump_file != NULL)
3616 fprintf (lra_dump_file,
3617 " Rejecting inheritance for %d "
3618 "because of disjoint classes %s and %s\n",
3619 original_regno, reg_class_names[cl],
3620 reg_class_names[rclass]);
3621 fprintf (lra_dump_file,
3622 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
3624 return false;
3626 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
3627 /* We don't use a subset of two classes because it can be
3628 NO_REGS. This transformation is still profitable in most
3629 cases even if the classes are not intersected as register
3630 move is probably cheaper than a memory load. */
3631 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
3633 if (lra_dump_file != NULL)
3634 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
3635 reg_class_names[cl], reg_class_names[rclass]);
3637 rclass = cl;
3639 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
3640 rclass, "inheritance");
3641 start_sequence ();
3642 if (def_p)
3643 emit_move_insn (original_reg, new_reg);
3644 else
3645 emit_move_insn (new_reg, original_reg);
3646 new_insns = get_insns ();
3647 end_sequence ();
3648 if (NEXT_INSN (new_insns) != NULL_RTX)
3650 if (lra_dump_file != NULL)
3652 fprintf (lra_dump_file,
3653 " Rejecting inheritance %d->%d "
3654 "as it results in 2 or more insns:\n",
3655 original_regno, REGNO (new_reg));
3656 debug_rtl_slim (lra_dump_file, new_insns, NULL_RTX, -1, 0);
3657 fprintf (lra_dump_file,
3658 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
3660 return false;
3662 substitute_pseudo (&insn, original_regno, new_reg);
3663 lra_update_insn_regno_info (insn);
3664 if (! def_p)
3665 /* We now have a new usage insn for original regno. */
3666 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
3667 if (lra_dump_file != NULL)
3668 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
3669 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
3670 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
3671 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
3672 bitmap_set_bit (&check_only_regs, original_regno);
3673 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
3674 if (def_p)
3675 lra_process_new_insns (insn, NULL_RTX, new_insns,
3676 "Add original<-inheritance");
3677 else
3678 lra_process_new_insns (insn, new_insns, NULL_RTX,
3679 "Add inheritance<-original");
3680 while (next_usage_insns != NULL_RTX)
3682 if (GET_CODE (next_usage_insns) != INSN_LIST)
3684 usage_insn = next_usage_insns;
3685 lra_assert (NONDEBUG_INSN_P (usage_insn));
3686 next_usage_insns = NULL;
3688 else
3690 usage_insn = XEXP (next_usage_insns, 0);
3691 lra_assert (DEBUG_INSN_P (usage_insn));
3692 next_usage_insns = XEXP (next_usage_insns, 1);
3694 substitute_pseudo (&usage_insn, original_regno, new_reg);
3695 lra_update_insn_regno_info (usage_insn);
3696 if (lra_dump_file != NULL)
3698 fprintf (lra_dump_file,
3699 " Inheritance reuse change %d->%d (bb%d):\n",
3700 original_regno, REGNO (new_reg),
3701 BLOCK_FOR_INSN (usage_insn)->index);
3702 debug_rtl_slim (lra_dump_file, usage_insn, usage_insn,
3703 -1, 0);
3706 if (lra_dump_file != NULL)
3707 fprintf (lra_dump_file,
3708 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
3709 return true;
3712 /* Return true if we need a caller save/restore for pseudo REGNO which
3713 was assigned to a hard register. */
3714 static inline bool
3715 need_for_call_save_p (int regno)
3717 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
3718 return (usage_insns[regno].calls_num < calls_num
3719 && (overlaps_hard_reg_set_p
3720 (call_used_reg_set,
3721 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])));
3724 /* Global registers occuring in the current EBB. */
3725 static bitmap_head ebb_global_regs;
3727 /* Return true if we need a split for hard register REGNO or pseudo
3728 REGNO which was assigned to a hard register.
3729 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
3730 used for reloads since the EBB end. It is an approximation of the
3731 used hard registers in the split range. The exact value would
3732 require expensive calculations. If we were aggressive with
3733 splitting because of the approximation, the split pseudo will save
3734 the same hard register assignment and will be removed in the undo
3735 pass. We still need the approximation because too aggressive
3736 splitting would result in too inaccurate cost calculation in the
3737 assignment pass because of too many generated moves which will be
3738 probably removed in the undo pass. */
3739 static inline bool
3740 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
3742 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
3744 lra_assert (hard_regno >= 0);
3745 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
3746 /* Don't split eliminable hard registers, otherwise we can
3747 split hard registers like hard frame pointer, which
3748 lives on BB start/end according to DF-infrastructure,
3749 when there is a pseudo assigned to the register and
3750 living in the same BB. */
3751 && (regno >= FIRST_PSEUDO_REGISTER
3752 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
3753 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
3754 /* We need at least 2 reloads to make pseudo splitting
3755 profitable. We should provide hard regno splitting in
3756 any case to solve 1st insn scheduling problem when
3757 moving hard register definition up might result in
3758 impossibility to find hard register for reload pseudo of
3759 small register class. */
3760 && (usage_insns[regno].reloads_num
3761 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 2) < reloads_num)
3762 && (regno < FIRST_PSEUDO_REGISTER
3763 /* For short living pseudos, spilling + inheritance can
3764 be considered a substitution for splitting.
3765 Therefore we do not splitting for local pseudos. It
3766 decreases also aggressiveness of splitting. The
3767 minimal number of references is chosen taking into
3768 account that for 2 references splitting has no sense
3769 as we can just spill the pseudo. */
3770 || (regno >= FIRST_PSEUDO_REGISTER
3771 && lra_reg_info[regno].nrefs > 3
3772 && bitmap_bit_p (&ebb_global_regs, regno))))
3773 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
3776 /* Return class for the split pseudo created from original pseudo with
3777 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
3778 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
3779 results in no secondary memory movements. */
3780 static enum reg_class
3781 choose_split_class (enum reg_class allocno_class,
3782 int hard_regno ATTRIBUTE_UNUSED,
3783 enum machine_mode mode ATTRIBUTE_UNUSED)
3785 #ifndef SECONDARY_MEMORY_NEEDED
3786 return allocno_class;
3787 #else
3788 int i;
3789 enum reg_class cl, best_cl = NO_REGS;
3790 enum reg_class hard_reg_class = REGNO_REG_CLASS (hard_regno);
3792 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
3793 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
3794 return allocno_class;
3795 for (i = 0;
3796 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
3797 i++)
3798 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
3799 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
3800 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
3801 && (best_cl == NO_REGS
3802 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
3803 best_cl = cl;
3804 return best_cl;
3805 #endif
3808 /* Do split transformations for insn INSN, which defines or uses
3809 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
3810 the EBB next uses ORIGINAL_REGNO; it has the same form as the
3811 "insns" field of usage_insns.
3813 The transformations look like:
3815 p <- ... p <- ...
3816 ... s <- p (new insn -- save)
3817 ... =>
3818 ... p <- s (new insn -- restore)
3819 <- ... p ... <- ... p ...
3821 <- ... p ... <- ... p ...
3822 ... s <- p (new insn -- save)
3823 ... =>
3824 ... p <- s (new insn -- restore)
3825 <- ... p ... <- ... p ...
3827 where p is an original pseudo got a hard register or a hard
3828 register and s is a new split pseudo. The save is put before INSN
3829 if BEFORE_P is true. Return true if we succeed in such
3830 transformation. */
3831 static bool
3832 split_reg (bool before_p, int original_regno, rtx insn, rtx next_usage_insns)
3834 enum reg_class rclass;
3835 rtx original_reg;
3836 int hard_regno;
3837 rtx new_reg, save, restore, usage_insn;
3838 bool after_p;
3839 bool call_save_p;
3841 if (original_regno < FIRST_PSEUDO_REGISTER)
3843 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
3844 hard_regno = original_regno;
3845 call_save_p = false;
3847 else
3849 hard_regno = reg_renumber[original_regno];
3850 rclass = lra_get_allocno_class (original_regno);
3851 original_reg = regno_reg_rtx[original_regno];
3852 call_save_p = need_for_call_save_p (original_regno);
3854 original_reg = regno_reg_rtx[original_regno];
3855 lra_assert (hard_regno >= 0);
3856 if (lra_dump_file != NULL)
3857 fprintf (lra_dump_file,
3858 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
3859 if (call_save_p)
3861 enum machine_mode sec_mode;
3863 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3864 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (original_reg));
3865 #else
3866 sec_mode = GET_MODE (original_reg);
3867 #endif
3868 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3869 NO_REGS, "save");
3871 else
3873 rclass = choose_split_class (rclass, hard_regno,
3874 GET_MODE (original_reg));
3875 if (rclass == NO_REGS)
3877 if (lra_dump_file != NULL)
3879 fprintf (lra_dump_file,
3880 " Rejecting split of %d(%s): "
3881 "no good reg class for %d(%s)\n",
3882 original_regno,
3883 reg_class_names[lra_get_allocno_class (original_regno)],
3884 hard_regno,
3885 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
3886 fprintf
3887 (lra_dump_file,
3888 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
3890 return false;
3892 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
3893 rclass, "split");
3894 reg_renumber[REGNO (new_reg)] = hard_regno;
3896 save = emit_spill_move (true, new_reg, original_reg);
3897 if (NEXT_INSN (save) != NULL_RTX)
3899 lra_assert (! call_save_p);
3900 if (lra_dump_file != NULL)
3902 fprintf
3903 (lra_dump_file,
3904 " Rejecting split %d->%d resulting in > 2 %s save insns:\n",
3905 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
3906 debug_rtl_slim (lra_dump_file, save, NULL_RTX, -1, 0);
3907 fprintf (lra_dump_file,
3908 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
3910 return false;
3912 restore = emit_spill_move (false, new_reg, original_reg);
3913 if (NEXT_INSN (restore) != NULL_RTX)
3915 lra_assert (! call_save_p);
3916 if (lra_dump_file != NULL)
3918 fprintf (lra_dump_file,
3919 " Rejecting split %d->%d "
3920 "resulting in > 2 %s restore insns:\n",
3921 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
3922 debug_rtl_slim (lra_dump_file, restore, NULL_RTX, -1, 0);
3923 fprintf (lra_dump_file,
3924 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
3926 return false;
3928 after_p = usage_insns[original_regno].after_p;
3929 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
3930 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
3931 bitmap_set_bit (&check_only_regs, original_regno);
3932 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
3933 for (;;)
3935 if (GET_CODE (next_usage_insns) != INSN_LIST)
3937 usage_insn = next_usage_insns;
3938 break;
3940 usage_insn = XEXP (next_usage_insns, 0);
3941 lra_assert (DEBUG_INSN_P (usage_insn));
3942 next_usage_insns = XEXP (next_usage_insns, 1);
3943 substitute_pseudo (&usage_insn, original_regno, new_reg);
3944 lra_update_insn_regno_info (usage_insn);
3945 if (lra_dump_file != NULL)
3947 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
3948 original_regno, REGNO (new_reg));
3949 debug_rtl_slim (lra_dump_file, usage_insn, usage_insn,
3950 -1, 0);
3953 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
3954 lra_assert (usage_insn != insn || (after_p && before_p));
3955 lra_process_new_insns (usage_insn, after_p ? NULL_RTX : restore,
3956 after_p ? restore : NULL_RTX,
3957 call_save_p
3958 ? "Add reg<-save" : "Add reg<-split");
3959 lra_process_new_insns (insn, before_p ? save : NULL_RTX,
3960 before_p ? NULL_RTX : save,
3961 call_save_p
3962 ? "Add save<-reg" : "Add split<-reg");
3963 if (lra_dump_file != NULL)
3964 fprintf (lra_dump_file,
3965 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
3966 return true;
3969 /* Recognize that we need a split transformation for insn INSN, which
3970 defines or uses REGNO in its insn biggest MODE (we use it only if
3971 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
3972 hard registers which might be used for reloads since the EBB end.
3973 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
3974 uid before starting INSN processing. Return true if we succeed in
3975 such transformation. */
3976 static bool
3977 split_if_necessary (int regno, enum machine_mode mode,
3978 HARD_REG_SET potential_reload_hard_regs,
3979 bool before_p, rtx insn, int max_uid)
3981 bool res = false;
3982 int i, nregs = 1;
3983 rtx next_usage_insns;
3985 if (regno < FIRST_PSEUDO_REGISTER)
3986 nregs = hard_regno_nregs[regno][mode];
3987 for (i = 0; i < nregs; i++)
3988 if (usage_insns[regno + i].check == curr_usage_insns_check
3989 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
3990 /* To avoid processing the register twice or more. */
3991 && ((GET_CODE (next_usage_insns) != INSN_LIST
3992 && INSN_UID (next_usage_insns) < max_uid)
3993 || (GET_CODE (next_usage_insns) == INSN_LIST
3994 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
3995 && need_for_split_p (potential_reload_hard_regs, regno + i)
3996 && split_reg (before_p, regno + i, insn, next_usage_insns))
3997 res = true;
3998 return res;
4001 /* Check only registers living at the current program point in the
4002 current EBB. */
4003 static bitmap_head live_regs;
4005 /* Update live info in EBB given by its HEAD and TAIL insns after
4006 inheritance/split transformation. The function removes dead moves
4007 too. */
4008 static void
4009 update_ebb_live_info (rtx head, rtx tail)
4011 unsigned int j;
4012 int regno;
4013 bool live_p;
4014 rtx prev_insn, set;
4015 bool remove_p;
4016 basic_block last_bb, prev_bb, curr_bb;
4017 bitmap_iterator bi;
4018 struct lra_insn_reg *reg;
4019 edge e;
4020 edge_iterator ei;
4022 last_bb = BLOCK_FOR_INSN (tail);
4023 prev_bb = NULL;
4024 for (curr_insn = tail;
4025 curr_insn != PREV_INSN (head);
4026 curr_insn = prev_insn)
4028 prev_insn = PREV_INSN (curr_insn);
4029 /* We need to process empty blocks too. They contain
4030 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
4031 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
4032 continue;
4033 curr_bb = BLOCK_FOR_INSN (curr_insn);
4034 if (curr_bb != prev_bb)
4036 if (prev_bb != NULL)
4038 /* Update df_get_live_in (prev_bb): */
4039 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4040 if (bitmap_bit_p (&live_regs, j))
4041 bitmap_set_bit (df_get_live_in (prev_bb), j);
4042 else
4043 bitmap_clear_bit (df_get_live_in (prev_bb), j);
4045 if (curr_bb != last_bb)
4047 /* Update df_get_live_out (curr_bb): */
4048 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4050 live_p = bitmap_bit_p (&live_regs, j);
4051 if (! live_p)
4052 FOR_EACH_EDGE (e, ei, curr_bb->succs)
4053 if (bitmap_bit_p (df_get_live_in (e->dest), j))
4055 live_p = true;
4056 break;
4058 if (live_p)
4059 bitmap_set_bit (df_get_live_out (curr_bb), j);
4060 else
4061 bitmap_clear_bit (df_get_live_out (curr_bb), j);
4064 prev_bb = curr_bb;
4065 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
4067 if (! NONDEBUG_INSN_P (curr_insn))
4068 continue;
4069 curr_id = lra_get_insn_recog_data (curr_insn);
4070 remove_p = false;
4071 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
4072 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
4073 && bitmap_bit_p (&check_only_regs, regno)
4074 && ! bitmap_bit_p (&live_regs, regno))
4075 remove_p = true;
4076 /* See which defined values die here. */
4077 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4078 if (reg->type == OP_OUT && ! reg->subreg_p)
4079 bitmap_clear_bit (&live_regs, reg->regno);
4080 /* Mark each used value as live. */
4081 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4082 if (reg->type == OP_IN
4083 && bitmap_bit_p (&check_only_regs, reg->regno))
4084 bitmap_set_bit (&live_regs, reg->regno);
4085 /* It is quite important to remove dead move insns because it
4086 means removing dead store. We don't need to process them for
4087 constraints. */
4088 if (remove_p)
4090 if (lra_dump_file != NULL)
4092 fprintf (lra_dump_file, " Removing dead insn:\n ");
4093 debug_rtl_slim (lra_dump_file, curr_insn, curr_insn, -1, 0);
4095 lra_set_insn_deleted (curr_insn);
4100 /* The structure describes info to do an inheritance for the current
4101 insn. We need to collect such info first before doing the
4102 transformations because the transformations change the insn
4103 internal representation. */
4104 struct to_inherit
4106 /* Original regno. */
4107 int regno;
4108 /* Subsequent insns which can inherit original reg value. */
4109 rtx insns;
4112 /* Array containing all info for doing inheritance from the current
4113 insn. */
4114 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
4116 /* Number elements in the previous array. */
4117 static int to_inherit_num;
4119 /* Add inheritance info REGNO and INSNS. Their meaning is described in
4120 structure to_inherit. */
4121 static void
4122 add_to_inherit (int regno, rtx insns)
4124 int i;
4126 for (i = 0; i < to_inherit_num; i++)
4127 if (to_inherit[i].regno == regno)
4128 return;
4129 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
4130 to_inherit[to_inherit_num].regno = regno;
4131 to_inherit[to_inherit_num++].insns = insns;
4134 /* Return the last non-debug insn in basic block BB, or the block begin
4135 note if none. */
4136 static rtx
4137 get_last_insertion_point (basic_block bb)
4139 rtx insn;
4141 FOR_BB_INSNS_REVERSE (bb, insn)
4142 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
4143 return insn;
4144 gcc_unreachable ();
4147 /* Set up RES by registers living on edges FROM except the edge (FROM,
4148 TO) or by registers set up in a jump insn in BB FROM. */
4149 static void
4150 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
4152 rtx last;
4153 struct lra_insn_reg *reg;
4154 edge e;
4155 edge_iterator ei;
4157 lra_assert (to != NULL);
4158 bitmap_clear (res);
4159 FOR_EACH_EDGE (e, ei, from->succs)
4160 if (e->dest != to)
4161 bitmap_ior_into (res, df_get_live_in (e->dest));
4162 last = get_last_insertion_point (from);
4163 if (! JUMP_P (last))
4164 return;
4165 curr_id = lra_get_insn_recog_data (last);
4166 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4167 if (reg->type != OP_IN)
4168 bitmap_set_bit (res, reg->regno);
4171 /* Used as a temporary results of some bitmap calculations. */
4172 static bitmap_head temp_bitmap;
4174 /* Do inheritance/split transformations in EBB starting with HEAD and
4175 finishing on TAIL. We process EBB insns in the reverse order.
4176 Return true if we did any inheritance/split transformation in the
4177 EBB.
4179 We should avoid excessive splitting which results in worse code
4180 because of inaccurate cost calculations for spilling new split
4181 pseudos in such case. To achieve this we do splitting only if
4182 register pressure is high in given basic block and there are reload
4183 pseudos requiring hard registers. We could do more register
4184 pressure calculations at any given program point to avoid necessary
4185 splitting even more but it is to expensive and the current approach
4186 works well enough. */
4187 static bool
4188 inherit_in_ebb (rtx head, rtx tail)
4190 int i, src_regno, dst_regno, nregs;
4191 bool change_p, succ_p;
4192 rtx prev_insn, next_usage_insns, set, last_insn;
4193 enum reg_class cl;
4194 struct lra_insn_reg *reg;
4195 basic_block last_processed_bb, curr_bb = NULL;
4196 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
4197 bitmap to_process;
4198 unsigned int j;
4199 bitmap_iterator bi;
4200 bool head_p, after_p;
4202 change_p = false;
4203 curr_usage_insns_check++;
4204 reloads_num = calls_num = 0;
4205 bitmap_clear (&check_only_regs);
4206 last_processed_bb = NULL;
4207 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
4208 CLEAR_HARD_REG_SET (live_hard_regs);
4209 /* We don't process new insns generated in the loop. */
4210 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
4212 prev_insn = PREV_INSN (curr_insn);
4213 if (BLOCK_FOR_INSN (curr_insn) != NULL)
4214 curr_bb = BLOCK_FOR_INSN (curr_insn);
4215 if (last_processed_bb != curr_bb)
4217 /* We are at the end of BB. Add qualified living
4218 pseudos for potential splitting. */
4219 to_process = df_get_live_out (curr_bb);
4220 if (last_processed_bb != NULL)
4222 /* We are somewhere in the middle of EBB. */
4223 get_live_on_other_edges (curr_bb, last_processed_bb,
4224 &temp_bitmap);
4225 to_process = &temp_bitmap;
4227 last_processed_bb = curr_bb;
4228 last_insn = get_last_insertion_point (curr_bb);
4229 after_p = (! JUMP_P (last_insn)
4230 && (! CALL_P (last_insn)
4231 || (find_reg_note (last_insn,
4232 REG_NORETURN, NULL_RTX) == NULL_RTX
4233 && ! SIBLING_CALL_P (last_insn))));
4234 REG_SET_TO_HARD_REG_SET (live_hard_regs, df_get_live_out (curr_bb));
4235 IOR_HARD_REG_SET (live_hard_regs, eliminable_regset);
4236 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
4237 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
4238 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
4240 if ((int) j >= lra_constraint_new_regno_start)
4241 break;
4242 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
4244 if (j < FIRST_PSEUDO_REGISTER)
4245 SET_HARD_REG_BIT (live_hard_regs, j);
4246 else
4247 add_to_hard_reg_set (&live_hard_regs,
4248 PSEUDO_REGNO_MODE (j),
4249 reg_renumber[j]);
4250 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
4254 src_regno = dst_regno = -1;
4255 if (NONDEBUG_INSN_P (curr_insn)
4256 && (set = single_set (curr_insn)) != NULL_RTX
4257 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
4259 src_regno = REGNO (SET_SRC (set));
4260 dst_regno = REGNO (SET_DEST (set));
4262 if (src_regno < lra_constraint_new_regno_start
4263 && src_regno >= FIRST_PSEUDO_REGISTER
4264 && reg_renumber[src_regno] < 0
4265 && dst_regno >= lra_constraint_new_regno_start
4266 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
4268 /* 'reload_pseudo <- original_pseudo'. */
4269 reloads_num++;
4270 succ_p = false;
4271 if (usage_insns[src_regno].check == curr_usage_insns_check
4272 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
4273 succ_p = inherit_reload_reg (false, src_regno, cl,
4274 curr_insn, next_usage_insns);
4275 if (succ_p)
4276 change_p = true;
4277 else
4278 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
4279 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
4280 IOR_HARD_REG_SET (potential_reload_hard_regs,
4281 reg_class_contents[cl]);
4283 else if (src_regno >= lra_constraint_new_regno_start
4284 && dst_regno < lra_constraint_new_regno_start
4285 && dst_regno >= FIRST_PSEUDO_REGISTER
4286 && reg_renumber[dst_regno] < 0
4287 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
4288 && usage_insns[dst_regno].check == curr_usage_insns_check
4289 && (next_usage_insns
4290 = usage_insns[dst_regno].insns) != NULL_RTX)
4292 reloads_num++;
4293 /* 'original_pseudo <- reload_pseudo'. */
4294 if (! JUMP_P (curr_insn)
4295 && inherit_reload_reg (true, dst_regno, cl,
4296 curr_insn, next_usage_insns))
4297 change_p = true;
4298 /* Invalidate. */
4299 usage_insns[dst_regno].check = 0;
4300 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
4301 IOR_HARD_REG_SET (potential_reload_hard_regs,
4302 reg_class_contents[cl]);
4304 else if (INSN_P (curr_insn))
4306 int max_uid = get_max_uid ();
4308 curr_id = lra_get_insn_recog_data (curr_insn);
4309 to_inherit_num = 0;
4310 /* Process insn definitions. */
4311 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4312 if (reg->type != OP_IN
4313 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
4315 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
4316 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
4317 && usage_insns[dst_regno].check == curr_usage_insns_check
4318 && (next_usage_insns
4319 = usage_insns[dst_regno].insns) != NULL_RTX)
4321 struct lra_insn_reg *r;
4323 for (r = curr_id->regs; r != NULL; r = r->next)
4324 if (r->type != OP_OUT && r->regno == dst_regno)
4325 break;
4326 /* Don't do inheritance if the pseudo is also
4327 used in the insn. */
4328 if (r == NULL)
4329 /* We can not do inheritance right now
4330 because the current insn reg info (chain
4331 regs) can change after that. */
4332 add_to_inherit (dst_regno, next_usage_insns);
4334 /* We can not process one reg twice here because of
4335 usage_insns invalidation. */
4336 if ((dst_regno < FIRST_PSEUDO_REGISTER
4337 || reg_renumber[dst_regno] >= 0)
4338 && ! reg->subreg_p && reg->type == OP_OUT)
4340 HARD_REG_SET s;
4342 if (split_if_necessary (dst_regno, reg->biggest_mode,
4343 potential_reload_hard_regs,
4344 false, curr_insn, max_uid))
4345 change_p = true;
4346 CLEAR_HARD_REG_SET (s);
4347 if (dst_regno < FIRST_PSEUDO_REGISTER)
4348 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
4349 else
4350 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
4351 reg_renumber[dst_regno]);
4352 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
4354 /* We should invalidate potential inheritance or
4355 splitting for the current insn usages to the next
4356 usage insns (see code below) as the output pseudo
4357 prevents this. */
4358 if ((dst_regno >= FIRST_PSEUDO_REGISTER
4359 && reg_renumber[dst_regno] < 0)
4360 || (reg->type == OP_OUT && ! reg->subreg_p
4361 && (dst_regno < FIRST_PSEUDO_REGISTER
4362 || reg_renumber[dst_regno] >= 0)))
4364 /* Invalidate. */
4365 if (dst_regno >= FIRST_PSEUDO_REGISTER)
4366 usage_insns[dst_regno].check = 0;
4367 else
4369 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
4370 for (i = 0; i < nregs; i++)
4371 usage_insns[dst_regno + i].check = 0;
4375 if (! JUMP_P (curr_insn))
4376 for (i = 0; i < to_inherit_num; i++)
4377 if (inherit_reload_reg (true, to_inherit[i].regno,
4378 ALL_REGS, curr_insn,
4379 to_inherit[i].insns))
4380 change_p = true;
4381 if (CALL_P (curr_insn))
4383 rtx cheap, pat, dest, restore;
4384 int regno, hard_regno;
4386 calls_num++;
4387 if ((cheap = find_reg_note (curr_insn,
4388 REG_RETURNED, NULL_RTX)) != NULL_RTX
4389 && ((cheap = XEXP (cheap, 0)), true)
4390 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
4391 && (hard_regno = reg_renumber[regno]) >= 0
4392 /* If there are pending saves/restores, the
4393 optimization is not worth. */
4394 && usage_insns[regno].calls_num == calls_num - 1
4395 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
4397 /* Restore the pseudo from the call result as
4398 REG_RETURNED note says that the pseudo value is
4399 in the call result and the pseudo is an argument
4400 of the call. */
4401 pat = PATTERN (curr_insn);
4402 if (GET_CODE (pat) == PARALLEL)
4403 pat = XVECEXP (pat, 0, 0);
4404 dest = SET_DEST (pat);
4405 start_sequence ();
4406 emit_move_insn (cheap, copy_rtx (dest));
4407 restore = get_insns ();
4408 end_sequence ();
4409 lra_process_new_insns (curr_insn, NULL, restore,
4410 "Inserting call parameter restore");
4411 /* We don't need to save/restore of the pseudo from
4412 this call. */
4413 usage_insns[regno].calls_num = calls_num;
4414 bitmap_set_bit (&check_only_regs, regno);
4417 to_inherit_num = 0;
4418 /* Process insn usages. */
4419 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4420 if ((reg->type != OP_OUT
4421 || (reg->type == OP_OUT && reg->subreg_p))
4422 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
4424 if (src_regno >= FIRST_PSEUDO_REGISTER
4425 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
4427 if (usage_insns[src_regno].check == curr_usage_insns_check
4428 && (next_usage_insns
4429 = usage_insns[src_regno].insns) != NULL_RTX
4430 && NONDEBUG_INSN_P (curr_insn))
4431 add_to_inherit (src_regno, next_usage_insns);
4432 else
4433 /* Add usages. */
4434 add_next_usage_insn (src_regno, curr_insn, reloads_num);
4436 else if (src_regno < FIRST_PSEUDO_REGISTER
4437 || reg_renumber[src_regno] >= 0)
4439 bool before_p;
4440 rtx use_insn = curr_insn;
4442 before_p = (JUMP_P (curr_insn)
4443 || (CALL_P (curr_insn) && reg->type == OP_IN));
4444 if (NONDEBUG_INSN_P (curr_insn)
4445 && split_if_necessary (src_regno, reg->biggest_mode,
4446 potential_reload_hard_regs,
4447 before_p, curr_insn, max_uid))
4449 if (reg->subreg_p)
4450 lra_risky_transformations_p = true;
4451 change_p = true;
4452 /* Invalidate. */
4453 usage_insns[src_regno].check = 0;
4454 if (before_p)
4455 use_insn = PREV_INSN (curr_insn);
4457 if (NONDEBUG_INSN_P (curr_insn))
4459 if (src_regno < FIRST_PSEUDO_REGISTER)
4460 add_to_hard_reg_set (&live_hard_regs,
4461 reg->biggest_mode, src_regno);
4462 else
4463 add_to_hard_reg_set (&live_hard_regs,
4464 PSEUDO_REGNO_MODE (src_regno),
4465 reg_renumber[src_regno]);
4467 add_next_usage_insn (src_regno, use_insn, reloads_num);
4470 for (i = 0; i < to_inherit_num; i++)
4472 src_regno = to_inherit[i].regno;
4473 if (inherit_reload_reg (false, src_regno, ALL_REGS,
4474 curr_insn, to_inherit[i].insns))
4475 change_p = true;
4476 else
4477 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
4480 /* We reached the start of the current basic block. */
4481 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
4482 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
4484 /* We reached the beginning of the current block -- do
4485 rest of spliting in the current BB. */
4486 to_process = df_get_live_in (curr_bb);
4487 if (BLOCK_FOR_INSN (head) != curr_bb)
4489 /* We are somewhere in the middle of EBB. */
4490 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
4491 curr_bb, &temp_bitmap);
4492 to_process = &temp_bitmap;
4494 head_p = true;
4495 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
4497 if ((int) j >= lra_constraint_new_regno_start)
4498 break;
4499 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
4500 && usage_insns[j].check == curr_usage_insns_check
4501 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
4503 if (need_for_split_p (potential_reload_hard_regs, j))
4505 if (lra_dump_file != NULL && head_p)
4507 fprintf (lra_dump_file,
4508 " ----------------------------------\n");
4509 head_p = false;
4511 if (split_reg (false, j, bb_note (curr_bb),
4512 next_usage_insns))
4513 change_p = true;
4515 usage_insns[j].check = 0;
4520 return change_p;
4523 /* This value affects EBB forming. If probability of edge from EBB to
4524 a BB is not greater than the following value, we don't add the BB
4525 to EBB. */
4526 #define EBB_PROBABILITY_CUTOFF (REG_BR_PROB_BASE / 2)
4528 /* Current number of inheritance/split iteration. */
4529 int lra_inheritance_iter;
4531 /* Entry function for inheritance/split pass. */
4532 void
4533 lra_inheritance (void)
4535 int i;
4536 basic_block bb, start_bb;
4537 edge e;
4539 timevar_push (TV_LRA_INHERITANCE);
4540 lra_inheritance_iter++;
4541 if (lra_dump_file != NULL)
4542 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
4543 lra_inheritance_iter);
4544 curr_usage_insns_check = 0;
4545 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
4546 for (i = 0; i < lra_constraint_new_regno_start; i++)
4547 usage_insns[i].check = 0;
4548 bitmap_initialize (&check_only_regs, &reg_obstack);
4549 bitmap_initialize (&live_regs, &reg_obstack);
4550 bitmap_initialize (&temp_bitmap, &reg_obstack);
4551 bitmap_initialize (&ebb_global_regs, &reg_obstack);
4552 FOR_EACH_BB (bb)
4554 start_bb = bb;
4555 if (lra_dump_file != NULL)
4556 fprintf (lra_dump_file, "EBB");
4557 /* Form a EBB starting with BB. */
4558 bitmap_clear (&ebb_global_regs);
4559 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
4560 for (;;)
4562 if (lra_dump_file != NULL)
4563 fprintf (lra_dump_file, " %d", bb->index);
4564 if (bb->next_bb == EXIT_BLOCK_PTR || LABEL_P (BB_HEAD (bb->next_bb)))
4565 break;
4566 e = find_fallthru_edge (bb->succs);
4567 if (! e)
4568 break;
4569 if (e->probability <= EBB_PROBABILITY_CUTOFF)
4570 break;
4571 bb = bb->next_bb;
4573 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
4574 if (lra_dump_file != NULL)
4575 fprintf (lra_dump_file, "\n");
4576 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
4577 /* Remember that the EBB head and tail can change in
4578 inherit_in_ebb. */
4579 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
4581 bitmap_clear (&ebb_global_regs);
4582 bitmap_clear (&temp_bitmap);
4583 bitmap_clear (&live_regs);
4584 bitmap_clear (&check_only_regs);
4585 free (usage_insns);
4587 timevar_pop (TV_LRA_INHERITANCE);
4592 /* This page contains code to undo failed inheritance/split
4593 transformations. */
4595 /* Current number of iteration undoing inheritance/split. */
4596 int lra_undo_inheritance_iter;
4598 /* Fix BB live info LIVE after removing pseudos created on pass doing
4599 inheritance/split which are REMOVED_PSEUDOS. */
4600 static void
4601 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
4603 unsigned int regno;
4604 bitmap_iterator bi;
4606 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
4607 if (bitmap_clear_bit (live, regno))
4608 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
4611 /* Return regno of the (subreg of) REG. Otherwise, return a negative
4612 number. */
4613 static int
4614 get_regno (rtx reg)
4616 if (GET_CODE (reg) == SUBREG)
4617 reg = SUBREG_REG (reg);
4618 if (REG_P (reg))
4619 return REGNO (reg);
4620 return -1;
4623 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
4624 return true if we did any change. The undo transformations for
4625 inheritance looks like
4626 i <- i2
4627 p <- i => p <- i2
4628 or removing
4629 p <- i, i <- p, and i <- i3
4630 where p is original pseudo from which inheritance pseudo i was
4631 created, i and i3 are removed inheritance pseudos, i2 is another
4632 not removed inheritance pseudo. All split pseudos or other
4633 occurrences of removed inheritance pseudos are changed on the
4634 corresponding original pseudos.
4636 The function also schedules insns changed and created during
4637 inheritance/split pass for processing by the subsequent constraint
4638 pass. */
4639 static bool
4640 remove_inheritance_pseudos (bitmap remove_pseudos)
4642 basic_block bb;
4643 int regno, sregno, prev_sregno, dregno, restore_regno;
4644 rtx set, prev_set, prev_insn;
4645 bool change_p, done_p;
4647 change_p = ! bitmap_empty_p (remove_pseudos);
4648 /* We can not finish the function right away if CHANGE_P is true
4649 because we need to marks insns affected by previous
4650 inheritance/split pass for processing by the subsequent
4651 constraint pass. */
4652 FOR_EACH_BB (bb)
4654 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
4655 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
4656 FOR_BB_INSNS_REVERSE (bb, curr_insn)
4658 if (! INSN_P (curr_insn))
4659 continue;
4660 done_p = false;
4661 sregno = dregno = -1;
4662 if (change_p && NONDEBUG_INSN_P (curr_insn)
4663 && (set = single_set (curr_insn)) != NULL_RTX)
4665 dregno = get_regno (SET_DEST (set));
4666 sregno = get_regno (SET_SRC (set));
4669 if (sregno >= 0 && dregno >= 0)
4671 if ((bitmap_bit_p (remove_pseudos, sregno)
4672 && (lra_reg_info[sregno].restore_regno == dregno
4673 || (bitmap_bit_p (remove_pseudos, dregno)
4674 && (lra_reg_info[sregno].restore_regno
4675 == lra_reg_info[dregno].restore_regno))))
4676 || (bitmap_bit_p (remove_pseudos, dregno)
4677 && lra_reg_info[dregno].restore_regno == sregno))
4678 /* One of the following cases:
4679 original <- removed inheritance pseudo
4680 removed inherit pseudo <- another removed inherit pseudo
4681 removed inherit pseudo <- original pseudo
4683 removed_split_pseudo <- original_reg
4684 original_reg <- removed_split_pseudo */
4686 if (lra_dump_file != NULL)
4688 fprintf (lra_dump_file, " Removing %s:\n",
4689 bitmap_bit_p (&lra_split_regs, sregno)
4690 || bitmap_bit_p (&lra_split_regs, dregno)
4691 ? "split" : "inheritance");
4692 debug_rtl_slim (lra_dump_file,
4693 curr_insn, curr_insn, -1, 0);
4695 lra_set_insn_deleted (curr_insn);
4696 done_p = true;
4698 else if (bitmap_bit_p (remove_pseudos, sregno)
4699 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
4701 /* Search the following pattern:
4702 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
4703 original_pseudo <- inherit_or_split_pseudo1
4704 where the 2nd insn is the current insn and
4705 inherit_or_split_pseudo2 is not removed. If it is found,
4706 change the current insn onto:
4707 original_pseudo <- inherit_or_split_pseudo2. */
4708 for (prev_insn = PREV_INSN (curr_insn);
4709 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
4710 prev_insn = PREV_INSN (prev_insn))
4712 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
4713 && (prev_set = single_set (prev_insn)) != NULL_RTX
4714 /* There should be no subregs in insn we are
4715 searching because only the original reg might
4716 be in subreg when we changed the mode of
4717 load/store for splitting. */
4718 && REG_P (SET_DEST (prev_set))
4719 && REG_P (SET_SRC (prev_set))
4720 && (int) REGNO (SET_DEST (prev_set)) == sregno
4721 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
4722 >= FIRST_PSEUDO_REGISTER)
4723 /* As we consider chain of inheritance or
4724 splitting described in above comment we should
4725 check that sregno and prev_sregno were
4726 inheritance/split pseudos created from the
4727 same original regno. */
4728 && (lra_reg_info[sregno].restore_regno
4729 == lra_reg_info[prev_sregno].restore_regno)
4730 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
4732 lra_assert (GET_MODE (SET_SRC (prev_set))
4733 == GET_MODE (regno_reg_rtx[sregno]));
4734 if (GET_CODE (SET_SRC (set)) == SUBREG)
4735 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
4736 else
4737 SET_SRC (set) = SET_SRC (prev_set);
4738 lra_push_insn_and_update_insn_regno_info (curr_insn);
4739 lra_set_used_insn_alternative_by_uid
4740 (INSN_UID (curr_insn), -1);
4741 done_p = true;
4742 if (lra_dump_file != NULL)
4744 fprintf (lra_dump_file, " Change reload insn:\n");
4745 debug_rtl_slim (lra_dump_file,
4746 curr_insn, curr_insn, -1, 0);
4751 if (! done_p)
4753 struct lra_insn_reg *reg;
4754 bool restored_regs_p = false;
4755 bool kept_regs_p = false;
4757 curr_id = lra_get_insn_recog_data (curr_insn);
4758 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4760 regno = reg->regno;
4761 restore_regno = lra_reg_info[regno].restore_regno;
4762 if (restore_regno >= 0)
4764 if (change_p && bitmap_bit_p (remove_pseudos, regno))
4766 substitute_pseudo (&curr_insn, regno,
4767 regno_reg_rtx[restore_regno]);
4768 restored_regs_p = true;
4770 else
4771 kept_regs_p = true;
4774 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
4776 /* The instruction has changed since the previous
4777 constraints pass. */
4778 lra_push_insn_and_update_insn_regno_info (curr_insn);
4779 lra_set_used_insn_alternative_by_uid
4780 (INSN_UID (curr_insn), -1);
4782 else if (restored_regs_p)
4783 /* The instruction has been restored to the form that
4784 it had during the previous constraints pass. */
4785 lra_update_insn_regno_info (curr_insn);
4786 if (restored_regs_p && lra_dump_file != NULL)
4788 fprintf (lra_dump_file, " Insn after restoring regs:\n");
4789 debug_rtl_slim (lra_dump_file, curr_insn, curr_insn, -1, 0);
4794 return change_p;
4797 /* Entry function for undoing inheritance/split transformation. Return true
4798 if we did any RTL change in this pass. */
4799 bool
4800 lra_undo_inheritance (void)
4802 unsigned int regno;
4803 int restore_regno, hard_regno;
4804 int n_all_inherit, n_inherit, n_all_split, n_split;
4805 bitmap_head remove_pseudos;
4806 bitmap_iterator bi;
4807 bool change_p;
4809 lra_undo_inheritance_iter++;
4810 if (lra_dump_file != NULL)
4811 fprintf (lra_dump_file,
4812 "\n********** Undoing inheritance #%d: **********\n\n",
4813 lra_undo_inheritance_iter);
4814 bitmap_initialize (&remove_pseudos, &reg_obstack);
4815 n_inherit = n_all_inherit = 0;
4816 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
4817 if (lra_reg_info[regno].restore_regno >= 0)
4819 n_all_inherit++;
4820 if (reg_renumber[regno] < 0)
4821 bitmap_set_bit (&remove_pseudos, regno);
4822 else
4823 n_inherit++;
4825 if (lra_dump_file != NULL && n_all_inherit != 0)
4826 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
4827 n_inherit, n_all_inherit,
4828 (double) n_inherit / n_all_inherit * 100);
4829 n_split = n_all_split = 0;
4830 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
4831 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
4833 n_all_split++;
4834 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
4835 ? reg_renumber[restore_regno] : restore_regno);
4836 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
4837 bitmap_set_bit (&remove_pseudos, regno);
4838 else
4840 n_split++;
4841 if (lra_dump_file != NULL)
4842 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
4843 regno, restore_regno);
4846 if (lra_dump_file != NULL && n_all_split != 0)
4847 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
4848 n_split, n_all_split,
4849 (double) n_split / n_all_split * 100);
4850 change_p = remove_inheritance_pseudos (&remove_pseudos);
4851 bitmap_clear (&remove_pseudos);
4852 /* Clear restore_regnos. */
4853 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
4854 lra_reg_info[regno].restore_regno = -1;
4855 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
4856 lra_reg_info[regno].restore_regno = -1;
4857 return change_p;