* gfortran.fortran-torture/execute/intrinsic_nearest.x: Skip AIX.
[official-gcc.git] / gcc / lra-constraints.c
blobe9cb1adeff47a7ff5f6fe84e7d3570d209dd667d
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2013 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains code for 3 passes: constraint pass,
23 inheritance/split pass, and pass for undoing failed inheritance and
24 split.
26 The major goal of constraint pass is to transform RTL to satisfy
27 insn and address constraints by:
28 o choosing insn alternatives;
29 o generating *reload insns* (or reloads in brief) and *reload
30 pseudos* which will get necessary hard registers later;
31 o substituting pseudos with equivalent values and removing the
32 instructions that initialized those pseudos.
34 The constraint pass has biggest and most complicated code in LRA.
35 There are a lot of important details like:
36 o reuse of input reload pseudos to simplify reload pseudo
37 allocations;
38 o some heuristics to choose insn alternative to improve the
39 inheritance;
40 o early clobbers etc.
42 The pass is mimicking former reload pass in alternative choosing
43 because the reload pass is oriented to current machine description
44 model. It might be changed if the machine description model is
45 changed.
47 There is special code for preventing all LRA and this pass cycling
48 in case of bugs.
50 On the first iteration of the pass we process every instruction and
51 choose an alternative for each one. On subsequent iterations we try
52 to avoid reprocessing instructions if we can be sure that the old
53 choice is still valid.
55 The inheritance/spilt pass is to transform code to achieve
56 ineheritance and live range splitting. It is done on backward
57 traversal of EBBs.
59 The inheritance optimization goal is to reuse values in hard
60 registers. There is analogous optimization in old reload pass. The
61 inheritance is achieved by following transformation:
63 reload_p1 <- p reload_p1 <- p
64 ... new_p <- reload_p1
65 ... => ...
66 reload_p2 <- p reload_p2 <- new_p
68 where p is spilled and not changed between the insns. Reload_p1 is
69 also called *original pseudo* and new_p is called *inheritance
70 pseudo*.
72 The subsequent assignment pass will try to assign the same (or
73 another if it is not possible) hard register to new_p as to
74 reload_p1 or reload_p2.
76 If the assignment pass fails to assign a hard register to new_p,
77 this file will undo the inheritance and restore the original code.
78 This is because implementing the above sequence with a spilled
79 new_p would make the code much worse. The inheritance is done in
80 EBB scope. The above is just a simplified example to get an idea
81 of the inheritance as the inheritance is also done for non-reload
82 insns.
84 Splitting (transformation) is also done in EBB scope on the same
85 pass as the inheritance:
87 r <- ... or ... <- r r <- ... or ... <- r
88 ... s <- r (new insn -- save)
89 ... =>
90 ... r <- s (new insn -- restore)
91 ... <- r ... <- r
93 The *split pseudo* s is assigned to the hard register of the
94 original pseudo or hard register r.
96 Splitting is done:
97 o In EBBs with high register pressure for global pseudos (living
98 in at least 2 BBs) and assigned to hard registers when there
99 are more one reloads needing the hard registers;
100 o for pseudos needing save/restore code around calls.
102 If the split pseudo still has the same hard register as the
103 original pseudo after the subsequent assignment pass or the
104 original pseudo was split, the opposite transformation is done on
105 the same pass for undoing inheritance. */
107 #undef REG_OK_STRICT
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "tm.h"
113 #include "hard-reg-set.h"
114 #include "rtl.h"
115 #include "tm_p.h"
116 #include "regs.h"
117 #include "insn-config.h"
118 #include "insn-codes.h"
119 #include "recog.h"
120 #include "output.h"
121 #include "addresses.h"
122 #include "target.h"
123 #include "function.h"
124 #include "expr.h"
125 #include "basic-block.h"
126 #include "except.h"
127 #include "optabs.h"
128 #include "df.h"
129 #include "ira.h"
130 #include "rtl-error.h"
131 #include "lra-int.h"
133 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
134 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
135 reload insns. */
136 static int bb_reload_num;
138 /* The current insn being processed and corresponding its single set
139 (NULL otherwise), its data (basic block, the insn data, the insn
140 static data, and the mode of each operand). */
141 static rtx curr_insn;
142 static rtx curr_insn_set;
143 static basic_block curr_bb;
144 static lra_insn_recog_data_t curr_id;
145 static struct lra_static_insn_data *curr_static_id;
146 static enum machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
150 /* Start numbers for new registers and insns at the current constraints
151 pass start. */
152 static int new_regno_start;
153 static int new_insn_uid_start;
155 /* If LOC is nonnull, strip any outer subreg from it. */
156 static inline rtx *
157 strip_subreg (rtx *loc)
159 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
162 /* Return hard regno of REGNO or if it is was not assigned to a hard
163 register, use a hard register from its allocno class. */
164 static int
165 get_try_hard_regno (int regno)
167 int hard_regno;
168 enum reg_class rclass;
170 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
171 hard_regno = lra_get_regno_hard_regno (regno);
172 if (hard_regno >= 0)
173 return hard_regno;
174 rclass = lra_get_allocno_class (regno);
175 if (rclass == NO_REGS)
176 return -1;
177 return ira_class_hard_regs[rclass][0];
180 /* Return final hard regno (plus offset) which will be after
181 elimination. We do this for matching constraints because the final
182 hard regno could have a different class. */
183 static int
184 get_final_hard_regno (int hard_regno, int offset)
186 if (hard_regno < 0)
187 return hard_regno;
188 hard_regno = lra_get_elimination_hard_regno (hard_regno);
189 return hard_regno + offset;
192 /* Return hard regno of X after removing subreg and making
193 elimination. If X is not a register or subreg of register, return
194 -1. For pseudo use its assignment. */
195 static int
196 get_hard_regno (rtx x)
198 rtx reg;
199 int offset, hard_regno;
201 reg = x;
202 if (GET_CODE (x) == SUBREG)
203 reg = SUBREG_REG (x);
204 if (! REG_P (reg))
205 return -1;
206 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
207 hard_regno = lra_get_regno_hard_regno (hard_regno);
208 if (hard_regno < 0)
209 return -1;
210 offset = 0;
211 if (GET_CODE (x) == SUBREG)
212 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
213 SUBREG_BYTE (x), GET_MODE (x));
214 return get_final_hard_regno (hard_regno, offset);
217 /* If REGNO is a hard register or has been allocated a hard register,
218 return the class of that register. If REGNO is a reload pseudo
219 created by the current constraints pass, return its allocno class.
220 Return NO_REGS otherwise. */
221 static enum reg_class
222 get_reg_class (int regno)
224 int hard_regno;
226 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
227 hard_regno = lra_get_regno_hard_regno (regno);
228 if (hard_regno >= 0)
230 hard_regno = get_final_hard_regno (hard_regno, 0);
231 return REGNO_REG_CLASS (hard_regno);
233 if (regno >= new_regno_start)
234 return lra_get_allocno_class (regno);
235 return NO_REGS;
238 /* Return true if REG satisfies (or will satisfy) reg class constraint
239 CL. Use elimination first if REG is a hard register. If REG is a
240 reload pseudo created by this constraints pass, assume that it will
241 be allocated a hard register from its allocno class, but allow that
242 class to be narrowed to CL if it is currently a superset of CL.
244 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
245 REGNO (reg), or NO_REGS if no change in its class was needed. */
246 static bool
247 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
249 enum reg_class rclass, common_class;
250 enum machine_mode reg_mode;
251 int class_size, hard_regno, nregs, i, j;
252 int regno = REGNO (reg);
254 if (new_class != NULL)
255 *new_class = NO_REGS;
256 if (regno < FIRST_PSEUDO_REGISTER)
258 rtx final_reg = reg;
259 rtx *final_loc = &final_reg;
261 lra_eliminate_reg_if_possible (final_loc);
262 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
264 reg_mode = GET_MODE (reg);
265 rclass = get_reg_class (regno);
266 if (regno < new_regno_start
267 /* Do not allow the constraints for reload instructions to
268 influence the classes of new pseudos. These reloads are
269 typically moves that have many alternatives, and restricting
270 reload pseudos for one alternative may lead to situations
271 where other reload pseudos are no longer allocatable. */
272 || INSN_UID (curr_insn) >= new_insn_uid_start)
273 /* When we don't know what class will be used finally for reload
274 pseudos, we use ALL_REGS. */
275 return ((regno >= new_regno_start && rclass == ALL_REGS)
276 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
277 && ! hard_reg_set_subset_p (reg_class_contents[cl],
278 lra_no_alloc_regs)));
279 else
281 common_class = ira_reg_class_subset[rclass][cl];
282 if (new_class != NULL)
283 *new_class = common_class;
284 if (hard_reg_set_subset_p (reg_class_contents[common_class],
285 lra_no_alloc_regs))
286 return false;
287 /* Check that there are enough allocatable regs. */
288 class_size = ira_class_hard_regs_num[common_class];
289 for (i = 0; i < class_size; i++)
291 hard_regno = ira_class_hard_regs[common_class][i];
292 nregs = hard_regno_nregs[hard_regno][reg_mode];
293 if (nregs == 1)
294 return true;
295 for (j = 0; j < nregs; j++)
296 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
297 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
298 hard_regno + j))
299 break;
300 if (j >= nregs)
301 return true;
303 return false;
307 /* Return true if REGNO satisfies a memory constraint. */
308 static bool
309 in_mem_p (int regno)
311 return get_reg_class (regno) == NO_REGS;
314 /* If we have decided to substitute X with another value, return that
315 value, otherwise return X. */
316 static rtx
317 get_equiv_substitution (rtx x)
319 int regno;
320 rtx res;
322 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
323 || ! ira_reg_equiv[regno].defined_p
324 || ! ira_reg_equiv[regno].profitable_p
325 || lra_get_regno_hard_regno (regno) >= 0)
326 return x;
327 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
328 return res;
329 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
330 return res;
331 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
332 return res;
333 gcc_unreachable ();
336 /* Set up curr_operand_mode. */
337 static void
338 init_curr_operand_mode (void)
340 int nop = curr_static_id->n_operands;
341 for (int i = 0; i < nop; i++)
343 enum machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
344 if (mode == VOIDmode)
346 /* The .md mode for address operands is the mode of the
347 addressed value rather than the mode of the address itself. */
348 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
349 mode = Pmode;
350 else
351 mode = curr_static_id->operand[i].mode;
353 curr_operand_mode[i] = mode;
359 /* The page contains code to reuse input reloads. */
361 /* Structure describes input reload of the current insns. */
362 struct input_reload
364 /* Reloaded value. */
365 rtx input;
366 /* Reload pseudo used. */
367 rtx reg;
370 /* The number of elements in the following array. */
371 static int curr_insn_input_reloads_num;
372 /* Array containing info about input reloads. It is used to find the
373 same input reload and reuse the reload pseudo in this case. */
374 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
376 /* Initiate data concerning reuse of input reloads for the current
377 insn. */
378 static void
379 init_curr_insn_input_reloads (void)
381 curr_insn_input_reloads_num = 0;
384 /* Change class of pseudo REGNO to NEW_CLASS. Print info about it
385 using TITLE. Output a new line if NL_P. */
386 static void
387 change_class (int regno, enum reg_class new_class,
388 const char *title, bool nl_p)
390 lra_assert (regno >= FIRST_PSEUDO_REGISTER);
391 if (lra_dump_file != NULL)
392 fprintf (lra_dump_file, "%s to class %s for r%d",
393 title, reg_class_names[new_class], regno);
394 setup_reg_classes (regno, new_class, NO_REGS, new_class);
395 if (lra_dump_file != NULL && nl_p)
396 fprintf (lra_dump_file, "\n");
399 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
400 created input reload pseudo (only if TYPE is not OP_OUT). The
401 result pseudo is returned through RESULT_REG. Return TRUE if we
402 created a new pseudo, FALSE if we reused the already created input
403 reload pseudo. Use TITLE to describe new registers for debug
404 purposes. */
405 static bool
406 get_reload_reg (enum op_type type, enum machine_mode mode, rtx original,
407 enum reg_class rclass, const char *title, rtx *result_reg)
409 int i, regno;
410 enum reg_class new_class;
412 if (type == OP_OUT)
414 *result_reg
415 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
416 return true;
418 /* Prevent reuse value of expression with side effects,
419 e.g. volatile memory. */
420 if (! side_effects_p (original))
421 for (i = 0; i < curr_insn_input_reloads_num; i++)
422 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
423 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
425 rtx reg = curr_insn_input_reloads[i].reg;
426 regno = REGNO (reg);
427 /* If input is equal to original and both are VOIDmode,
428 GET_MODE (reg) might be still different from mode.
429 Ensure we don't return *result_reg with wrong mode. */
430 if (GET_MODE (reg) != mode)
432 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
433 continue;
434 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
435 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
436 continue;
438 *result_reg = reg;
439 if (lra_dump_file != NULL)
441 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
442 dump_value_slim (lra_dump_file, original, 1);
444 if (new_class != lra_get_allocno_class (regno))
445 change_class (regno, new_class, ", change", false);
446 if (lra_dump_file != NULL)
447 fprintf (lra_dump_file, "\n");
448 return false;
450 *result_reg = lra_create_new_reg (mode, original, rclass, title);
451 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
452 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
453 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
454 return true;
459 /* The page contains code to extract memory address parts. */
461 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
462 static inline bool
463 ok_for_index_p_nonstrict (rtx reg)
465 unsigned regno = REGNO (reg);
467 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
470 /* A version of regno_ok_for_base_p for use here, when all pseudos
471 should count as OK. Arguments as for regno_ok_for_base_p. */
472 static inline bool
473 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode, addr_space_t as,
474 enum rtx_code outer_code, enum rtx_code index_code)
476 unsigned regno = REGNO (reg);
478 if (regno >= FIRST_PSEUDO_REGISTER)
479 return true;
480 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
485 /* The page contains major code to choose the current insn alternative
486 and generate reloads for it. */
488 /* Return the offset from REGNO of the least significant register
489 in (reg:MODE REGNO).
491 This function is used to tell whether two registers satisfy
492 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
494 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
495 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
497 lra_constraint_offset (int regno, enum machine_mode mode)
499 lra_assert (regno < FIRST_PSEUDO_REGISTER);
500 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
501 && SCALAR_INT_MODE_P (mode))
502 return hard_regno_nregs[regno][mode] - 1;
503 return 0;
506 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
507 if they are the same hard reg, and has special hacks for
508 auto-increment and auto-decrement. This is specifically intended for
509 process_alt_operands to use in determining whether two operands
510 match. X is the operand whose number is the lower of the two.
512 It is supposed that X is the output operand and Y is the input
513 operand. Y_HARD_REGNO is the final hard regno of register Y or
514 register in subreg Y as we know it now. Otherwise, it is a
515 negative value. */
516 static bool
517 operands_match_p (rtx x, rtx y, int y_hard_regno)
519 int i;
520 RTX_CODE code = GET_CODE (x);
521 const char *fmt;
523 if (x == y)
524 return true;
525 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
526 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
528 int j;
530 i = get_hard_regno (x);
531 if (i < 0)
532 goto slow;
534 if ((j = y_hard_regno) < 0)
535 goto slow;
537 i += lra_constraint_offset (i, GET_MODE (x));
538 j += lra_constraint_offset (j, GET_MODE (y));
540 return i == j;
543 /* If two operands must match, because they are really a single
544 operand of an assembler insn, then two post-increments are invalid
545 because the assembler insn would increment only once. On the
546 other hand, a post-increment matches ordinary indexing if the
547 post-increment is the output operand. */
548 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
549 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
551 /* Two pre-increments are invalid because the assembler insn would
552 increment only once. On the other hand, a pre-increment matches
553 ordinary indexing if the pre-increment is the input operand. */
554 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
555 || GET_CODE (y) == PRE_MODIFY)
556 return operands_match_p (x, XEXP (y, 0), -1);
558 slow:
560 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
561 && x == SUBREG_REG (y))
562 return true;
563 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
564 && SUBREG_REG (x) == y)
565 return true;
567 /* Now we have disposed of all the cases in which different rtx
568 codes can match. */
569 if (code != GET_CODE (y))
570 return false;
572 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
573 if (GET_MODE (x) != GET_MODE (y))
574 return false;
576 switch (code)
578 CASE_CONST_UNIQUE:
579 return false;
581 case LABEL_REF:
582 return XEXP (x, 0) == XEXP (y, 0);
583 case SYMBOL_REF:
584 return XSTR (x, 0) == XSTR (y, 0);
586 default:
587 break;
590 /* Compare the elements. If any pair of corresponding elements fail
591 to match, return false for the whole things. */
593 fmt = GET_RTX_FORMAT (code);
594 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
596 int val, j;
597 switch (fmt[i])
599 case 'w':
600 if (XWINT (x, i) != XWINT (y, i))
601 return false;
602 break;
604 case 'i':
605 if (XINT (x, i) != XINT (y, i))
606 return false;
607 break;
609 case 'e':
610 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
611 if (val == 0)
612 return false;
613 break;
615 case '0':
616 break;
618 case 'E':
619 if (XVECLEN (x, i) != XVECLEN (y, i))
620 return false;
621 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
623 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
624 if (val == 0)
625 return false;
627 break;
629 /* It is believed that rtx's at this level will never
630 contain anything but integers and other rtx's, except for
631 within LABEL_REFs and SYMBOL_REFs. */
632 default:
633 gcc_unreachable ();
636 return true;
639 /* True if X is a constant that can be forced into the constant pool.
640 MODE is the mode of the operand, or VOIDmode if not known. */
641 #define CONST_POOL_OK_P(MODE, X) \
642 ((MODE) != VOIDmode \
643 && CONSTANT_P (X) \
644 && GET_CODE (X) != HIGH \
645 && !targetm.cannot_force_const_mem (MODE, X))
647 /* True if C is a non-empty register class that has too few registers
648 to be safely used as a reload target class. */
649 #define SMALL_REGISTER_CLASS_P(C) \
650 (reg_class_size [(C)] == 1 \
651 || (reg_class_size [(C)] >= 1 && targetm.class_likely_spilled_p (C)))
653 /* If REG is a reload pseudo, try to make its class satisfying CL. */
654 static void
655 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
657 enum reg_class rclass;
659 /* Do not make more accurate class from reloads generated. They are
660 mostly moves with a lot of constraints. Making more accurate
661 class may results in very narrow class and impossibility of find
662 registers for several reloads of one insn. */
663 if (INSN_UID (curr_insn) >= new_insn_uid_start)
664 return;
665 if (GET_CODE (reg) == SUBREG)
666 reg = SUBREG_REG (reg);
667 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
668 return;
669 if (in_class_p (reg, cl, &rclass) && rclass != cl)
670 change_class (REGNO (reg), rclass, " Change", true);
673 /* Generate reloads for matching OUT and INS (array of input operand
674 numbers with end marker -1) with reg class GOAL_CLASS. Add input
675 and output reloads correspondingly to the lists *BEFORE and *AFTER.
676 OUT might be negative. In this case we generate input reloads for
677 matched input operands INS. */
678 static void
679 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
680 rtx *before, rtx *after)
682 int i, in;
683 rtx new_in_reg, new_out_reg, reg, clobber;
684 enum machine_mode inmode, outmode;
685 rtx in_rtx = *curr_id->operand_loc[ins[0]];
686 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
688 inmode = curr_operand_mode[ins[0]];
689 outmode = out < 0 ? inmode : curr_operand_mode[out];
690 push_to_sequence (*before);
691 if (inmode != outmode)
693 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
695 reg = new_in_reg
696 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
697 goal_class, "");
698 if (SCALAR_INT_MODE_P (inmode))
699 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
700 else
701 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
702 LRA_SUBREG_P (new_out_reg) = 1;
703 /* If the input reg is dying here, we can use the same hard
704 register for REG and IN_RTX. We do it only for original
705 pseudos as reload pseudos can die although original
706 pseudos still live where reload pseudos dies. */
707 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
708 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
709 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
711 else
713 reg = new_out_reg
714 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
715 goal_class, "");
716 if (SCALAR_INT_MODE_P (outmode))
717 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
718 else
719 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
720 /* NEW_IN_REG is non-paradoxical subreg. We don't want
721 NEW_OUT_REG living above. We add clobber clause for
722 this. This is just a temporary clobber. We can remove
723 it at the end of LRA work. */
724 clobber = emit_clobber (new_out_reg);
725 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
726 LRA_SUBREG_P (new_in_reg) = 1;
727 if (GET_CODE (in_rtx) == SUBREG)
729 rtx subreg_reg = SUBREG_REG (in_rtx);
731 /* If SUBREG_REG is dying here and sub-registers IN_RTX
732 and NEW_IN_REG are similar, we can use the same hard
733 register for REG and SUBREG_REG. */
734 if (REG_P (subreg_reg)
735 && (int) REGNO (subreg_reg) < lra_new_regno_start
736 && GET_MODE (subreg_reg) == outmode
737 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
738 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
739 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
743 else
745 /* Pseudos have values -- see comments for lra_reg_info.
746 Different pseudos with the same value do not conflict even if
747 they live in the same place. When we create a pseudo we
748 assign value of original pseudo (if any) from which we
749 created the new pseudo. If we create the pseudo from the
750 input pseudo, the new pseudo will no conflict with the input
751 pseudo which is wrong when the input pseudo lives after the
752 insn and as the new pseudo value is changed by the insn
753 output. Therefore we create the new pseudo from the output.
755 We cannot reuse the current output register because we might
756 have a situation like "a <- a op b", where the constraints
757 force the second input operand ("b") to match the output
758 operand ("a"). "b" must then be copied into a new register
759 so that it doesn't clobber the current value of "a". */
761 new_in_reg = new_out_reg
762 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
763 goal_class, "");
765 /* In operand can be got from transformations before processing insn
766 constraints. One example of such transformations is subreg
767 reloading (see function simplify_operand_subreg). The new
768 pseudos created by the transformations might have inaccurate
769 class (ALL_REGS) and we should make their classes more
770 accurate. */
771 narrow_reload_pseudo_class (in_rtx, goal_class);
772 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
773 *before = get_insns ();
774 end_sequence ();
775 for (i = 0; (in = ins[i]) >= 0; i++)
777 lra_assert
778 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
779 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
780 *curr_id->operand_loc[in] = new_in_reg;
782 lra_update_dups (curr_id, ins);
783 if (out < 0)
784 return;
785 /* See a comment for the input operand above. */
786 narrow_reload_pseudo_class (out_rtx, goal_class);
787 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
789 start_sequence ();
790 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
791 emit_insn (*after);
792 *after = get_insns ();
793 end_sequence ();
795 *curr_id->operand_loc[out] = new_out_reg;
796 lra_update_dup (curr_id, out);
799 /* Return register class which is union of all reg classes in insn
800 constraint alternative string starting with P. */
801 static enum reg_class
802 reg_class_from_constraints (const char *p)
804 int c, len;
805 enum reg_class op_class = NO_REGS;
808 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
810 case '#':
811 case ',':
812 return op_class;
814 case 'p':
815 op_class = (reg_class_subunion
816 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
817 ADDRESS, SCRATCH)]);
818 break;
820 case 'g':
821 case 'r':
822 op_class = reg_class_subunion[op_class][GENERAL_REGS];
823 break;
825 default:
826 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
828 #ifdef EXTRA_CONSTRAINT_STR
829 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
830 op_class
831 = (reg_class_subunion
832 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
833 ADDRESS, SCRATCH)]);
834 #endif
835 break;
838 op_class
839 = reg_class_subunion[op_class][REG_CLASS_FROM_CONSTRAINT (c, p)];
840 break;
842 while ((p += len), c);
843 return op_class;
846 /* If OP is a register, return the class of the register as per
847 get_reg_class, otherwise return NO_REGS. */
848 static inline enum reg_class
849 get_op_class (rtx op)
851 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
854 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
855 otherwise. If modes of MEM_PSEUDO and VAL are different, use
856 SUBREG for VAL to make them equal. */
857 static rtx
858 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
860 if (GET_MODE (mem_pseudo) != GET_MODE (val))
862 lra_assert (GET_MODE_SIZE (GET_MODE (mem_pseudo))
863 >= GET_MODE_SIZE (GET_MODE (val)));
864 if (! MEM_P (val))
866 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
867 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
869 LRA_SUBREG_P (val) = 1;
871 else
873 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
874 LRA_SUBREG_P (mem_pseudo) = 1;
877 return (to_p
878 ? gen_move_insn (mem_pseudo, val)
879 : gen_move_insn (val, mem_pseudo));
882 /* Process a special case insn (register move), return true if we
883 don't need to process it anymore. INSN should be a single set
884 insn. Set up that RTL was changed through CHANGE_P and macro
885 SECONDARY_MEMORY_NEEDED says to use secondary memory through
886 SEC_MEM_P. */
887 static bool
888 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
890 int sregno, dregno;
891 rtx dest, src, dreg, sreg, old_sreg, new_reg, before, scratch_reg;
892 enum reg_class dclass, sclass, secondary_class;
893 enum machine_mode sreg_mode;
894 secondary_reload_info sri;
896 lra_assert (curr_insn_set != NULL_RTX);
897 dreg = dest = SET_DEST (curr_insn_set);
898 sreg = src = SET_SRC (curr_insn_set);
899 if (GET_CODE (dest) == SUBREG)
900 dreg = SUBREG_REG (dest);
901 if (GET_CODE (src) == SUBREG)
902 sreg = SUBREG_REG (src);
903 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
904 return false;
905 sclass = dclass = NO_REGS;
906 if (REG_P (dreg))
907 dclass = get_reg_class (REGNO (dreg));
908 if (dclass == ALL_REGS)
909 /* ALL_REGS is used for new pseudos created by transformations
910 like reload of SUBREG_REG (see function
911 simplify_operand_subreg). We don't know their class yet. We
912 should figure out the class from processing the insn
913 constraints not in this fast path function. Even if ALL_REGS
914 were a right class for the pseudo, secondary_... hooks usually
915 are not define for ALL_REGS. */
916 return false;
917 sreg_mode = GET_MODE (sreg);
918 old_sreg = sreg;
919 if (REG_P (sreg))
920 sclass = get_reg_class (REGNO (sreg));
921 if (sclass == ALL_REGS)
922 /* See comments above. */
923 return false;
924 if (sclass == NO_REGS && dclass == NO_REGS)
925 return false;
926 #ifdef SECONDARY_MEMORY_NEEDED
927 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
928 #ifdef SECONDARY_MEMORY_NEEDED_MODE
929 && ((sclass != NO_REGS && dclass != NO_REGS)
930 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
931 #endif
934 *sec_mem_p = true;
935 return false;
937 #endif
938 if (! REG_P (dreg) || ! REG_P (sreg))
939 return false;
940 sri.prev_sri = NULL;
941 sri.icode = CODE_FOR_nothing;
942 sri.extra_cost = 0;
943 secondary_class = NO_REGS;
944 /* Set up hard register for a reload pseudo for hook
945 secondary_reload because some targets just ignore unassigned
946 pseudos in the hook. */
947 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
949 dregno = REGNO (dreg);
950 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
952 else
953 dregno = -1;
954 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
956 sregno = REGNO (sreg);
957 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
959 else
960 sregno = -1;
961 if (sclass != NO_REGS)
962 secondary_class
963 = (enum reg_class) targetm.secondary_reload (false, dest,
964 (reg_class_t) sclass,
965 GET_MODE (src), &sri);
966 if (sclass == NO_REGS
967 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
968 && dclass != NO_REGS))
970 enum reg_class old_sclass = secondary_class;
971 secondary_reload_info old_sri = sri;
973 sri.prev_sri = NULL;
974 sri.icode = CODE_FOR_nothing;
975 sri.extra_cost = 0;
976 secondary_class
977 = (enum reg_class) targetm.secondary_reload (true, sreg,
978 (reg_class_t) dclass,
979 sreg_mode, &sri);
980 /* Check the target hook consistency. */
981 lra_assert
982 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
983 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
984 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
986 if (sregno >= 0)
987 reg_renumber [sregno] = -1;
988 if (dregno >= 0)
989 reg_renumber [dregno] = -1;
990 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
991 return false;
992 *change_p = true;
993 new_reg = NULL_RTX;
994 if (secondary_class != NO_REGS)
995 new_reg = lra_create_new_reg_with_unique_value (sreg_mode, NULL_RTX,
996 secondary_class,
997 "secondary");
998 start_sequence ();
999 if (old_sreg != sreg)
1000 sreg = copy_rtx (sreg);
1001 if (sri.icode == CODE_FOR_nothing)
1002 lra_emit_move (new_reg, sreg);
1003 else
1005 enum reg_class scratch_class;
1007 scratch_class = (reg_class_from_constraints
1008 (insn_data[sri.icode].operand[2].constraint));
1009 scratch_reg = (lra_create_new_reg_with_unique_value
1010 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1011 scratch_class, "scratch"));
1012 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1013 sreg, scratch_reg));
1015 before = get_insns ();
1016 end_sequence ();
1017 lra_process_new_insns (curr_insn, before, NULL_RTX, "Inserting the move");
1018 if (new_reg != NULL_RTX)
1020 if (GET_CODE (src) == SUBREG)
1021 SUBREG_REG (src) = new_reg;
1022 else
1023 SET_SRC (curr_insn_set) = new_reg;
1025 else
1027 if (lra_dump_file != NULL)
1029 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1030 dump_insn_slim (lra_dump_file, curr_insn);
1032 lra_set_insn_deleted (curr_insn);
1033 return true;
1035 return false;
1038 /* The following data describe the result of process_alt_operands.
1039 The data are used in curr_insn_transform to generate reloads. */
1041 /* The chosen reg classes which should be used for the corresponding
1042 operands. */
1043 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1044 /* True if the operand should be the same as another operand and that
1045 other operand does not need a reload. */
1046 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1047 /* True if the operand does not need a reload. */
1048 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1049 /* True if the operand can be offsetable memory. */
1050 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1051 /* The number of an operand to which given operand can be matched to. */
1052 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1053 /* The number of elements in the following array. */
1054 static int goal_alt_dont_inherit_ops_num;
1055 /* Numbers of operands whose reload pseudos should not be inherited. */
1056 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1057 /* True if the insn commutative operands should be swapped. */
1058 static bool goal_alt_swapped;
1059 /* The chosen insn alternative. */
1060 static int goal_alt_number;
1062 /* The following five variables are used to choose the best insn
1063 alternative. They reflect final characteristics of the best
1064 alternative. */
1066 /* Number of necessary reloads and overall cost reflecting the
1067 previous value and other unpleasantness of the best alternative. */
1068 static int best_losers, best_overall;
1069 /* Overall number hard registers used for reloads. For example, on
1070 some targets we need 2 general registers to reload DFmode and only
1071 one floating point register. */
1072 static int best_reload_nregs;
1073 /* Overall number reflecting distances of previous reloading the same
1074 value. The distances are counted from the current BB start. It is
1075 used to improve inheritance chances. */
1076 static int best_reload_sum;
1078 /* True if the current insn should have no correspondingly input or
1079 output reloads. */
1080 static bool no_input_reloads_p, no_output_reloads_p;
1082 /* True if we swapped the commutative operands in the current
1083 insn. */
1084 static int curr_swapped;
1086 /* Arrange for address element *LOC to be a register of class CL.
1087 Add any input reloads to list BEFORE. AFTER is nonnull if *LOC is an
1088 automodified value; handle that case by adding the required output
1089 reloads to list AFTER. Return true if the RTL was changed. */
1090 static bool
1091 process_addr_reg (rtx *loc, rtx *before, rtx *after, enum reg_class cl)
1093 int regno;
1094 enum reg_class rclass, new_class;
1095 rtx reg;
1096 rtx new_reg;
1097 enum machine_mode mode;
1098 bool before_p = false;
1100 loc = strip_subreg (loc);
1101 reg = *loc;
1102 mode = GET_MODE (reg);
1103 if (! REG_P (reg))
1105 /* Always reload memory in an address even if the target supports
1106 such addresses. */
1107 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1108 before_p = true;
1110 else
1112 regno = REGNO (reg);
1113 rclass = get_reg_class (regno);
1114 if ((*loc = get_equiv_substitution (reg)) != reg)
1116 if (lra_dump_file != NULL)
1118 fprintf (lra_dump_file,
1119 "Changing pseudo %d in address of insn %u on equiv ",
1120 REGNO (reg), INSN_UID (curr_insn));
1121 dump_value_slim (lra_dump_file, *loc, 1);
1122 fprintf (lra_dump_file, "\n");
1124 *loc = copy_rtx (*loc);
1126 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1128 reg = *loc;
1129 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1130 mode, reg, cl, "address", &new_reg))
1131 before_p = true;
1133 else if (new_class != NO_REGS && rclass != new_class)
1135 change_class (regno, new_class, " Change", true);
1136 return false;
1138 else
1139 return false;
1141 if (before_p)
1143 push_to_sequence (*before);
1144 lra_emit_move (new_reg, reg);
1145 *before = get_insns ();
1146 end_sequence ();
1148 *loc = new_reg;
1149 if (after != NULL)
1151 start_sequence ();
1152 lra_emit_move (reg, new_reg);
1153 emit_insn (*after);
1154 *after = get_insns ();
1155 end_sequence ();
1157 return true;
1160 /* Make reloads for subreg in operand NOP with internal subreg mode
1161 REG_MODE, add new reloads for further processing. Return true if
1162 any reload was generated. */
1163 static bool
1164 simplify_operand_subreg (int nop, enum machine_mode reg_mode)
1166 int hard_regno;
1167 rtx before, after;
1168 enum machine_mode mode;
1169 rtx reg, new_reg;
1170 rtx operand = *curr_id->operand_loc[nop];
1172 before = after = NULL_RTX;
1174 if (GET_CODE (operand) != SUBREG)
1175 return false;
1177 mode = GET_MODE (operand);
1178 reg = SUBREG_REG (operand);
1179 /* If we change address for paradoxical subreg of memory, the
1180 address might violate the necessary alignment or the access might
1181 be slow. So take this into consideration. We should not worry
1182 about access beyond allocated memory for paradoxical memory
1183 subregs as we don't substitute such equiv memory (see processing
1184 equivalences in function lra_constraints) and because for spilled
1185 pseudos we allocate stack memory enough for the biggest
1186 corresponding paradoxical subreg. */
1187 if ((MEM_P (reg)
1188 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1189 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1190 || (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER))
1192 alter_subreg (curr_id->operand_loc[nop], false);
1193 return true;
1195 /* Put constant into memory when we have mixed modes. It generates
1196 a better code in most cases as it does not need a secondary
1197 reload memory. It also prevents LRA looping when LRA is using
1198 secondary reload memory again and again. */
1199 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1200 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1202 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1203 alter_subreg (curr_id->operand_loc[nop], false);
1204 return true;
1206 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1207 if there may be a problem accessing OPERAND in the outer
1208 mode. */
1209 if ((REG_P (reg)
1210 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1211 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1212 /* Don't reload paradoxical subregs because we could be looping
1213 having repeatedly final regno out of hard regs range. */
1214 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1215 >= hard_regno_nregs[hard_regno][mode])
1216 && simplify_subreg_regno (hard_regno, GET_MODE (reg),
1217 SUBREG_BYTE (operand), mode) < 0
1218 /* Don't reload subreg for matching reload. It is actually
1219 valid subreg in LRA. */
1220 && ! LRA_SUBREG_P (operand))
1221 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1223 enum op_type type = curr_static_id->operand[nop].type;
1224 /* The class will be defined later in curr_insn_transform. */
1225 enum reg_class rclass
1226 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1228 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1229 rclass, "subreg reg", &new_reg))
1231 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1232 if (type != OP_OUT
1233 || GET_MODE_SIZE (GET_MODE (reg)) > GET_MODE_SIZE (mode))
1235 push_to_sequence (before);
1236 lra_emit_move (new_reg, reg);
1237 before = get_insns ();
1238 end_sequence ();
1240 if (type != OP_IN)
1242 start_sequence ();
1243 lra_emit_move (reg, new_reg);
1244 emit_insn (after);
1245 after = get_insns ();
1246 end_sequence ();
1249 SUBREG_REG (operand) = new_reg;
1250 lra_process_new_insns (curr_insn, before, after,
1251 "Inserting subreg reload");
1252 return true;
1254 return false;
1257 /* Return TRUE if X refers for a hard register from SET. */
1258 static bool
1259 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1261 int i, j, x_hard_regno;
1262 enum machine_mode mode;
1263 const char *fmt;
1264 enum rtx_code code;
1266 if (x == NULL_RTX)
1267 return false;
1268 code = GET_CODE (x);
1269 mode = GET_MODE (x);
1270 if (code == SUBREG)
1272 x = SUBREG_REG (x);
1273 code = GET_CODE (x);
1274 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1275 mode = GET_MODE (x);
1278 if (REG_P (x))
1280 x_hard_regno = get_hard_regno (x);
1281 return (x_hard_regno >= 0
1282 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1284 if (MEM_P (x))
1286 struct address_info ad;
1288 decompose_mem_address (&ad, x);
1289 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1290 return true;
1291 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1292 return true;
1294 fmt = GET_RTX_FORMAT (code);
1295 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1297 if (fmt[i] == 'e')
1299 if (uses_hard_regs_p (XEXP (x, i), set))
1300 return true;
1302 else if (fmt[i] == 'E')
1304 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1305 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1306 return true;
1309 return false;
1312 /* Return true if OP is a spilled pseudo. */
1313 static inline bool
1314 spilled_pseudo_p (rtx op)
1316 return (REG_P (op)
1317 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1320 /* Return true if X is a general constant. */
1321 static inline bool
1322 general_constant_p (rtx x)
1324 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1327 static bool
1328 reg_in_class_p (rtx reg, enum reg_class cl)
1330 if (cl == NO_REGS)
1331 return get_reg_class (REGNO (reg)) == NO_REGS;
1332 return in_class_p (reg, cl, NULL);
1335 /* Major function to choose the current insn alternative and what
1336 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1337 negative we should consider only this alternative. Return false if
1338 we can not choose the alternative or find how to reload the
1339 operands. */
1340 static bool
1341 process_alt_operands (int only_alternative)
1343 bool ok_p = false;
1344 int nop, overall, nalt;
1345 int n_alternatives = curr_static_id->n_alternatives;
1346 int n_operands = curr_static_id->n_operands;
1347 /* LOSERS counts the operands that don't fit this alternative and
1348 would require loading. */
1349 int losers;
1350 /* REJECT is a count of how undesirable this alternative says it is
1351 if any reloading is required. If the alternative matches exactly
1352 then REJECT is ignored, but otherwise it gets this much counted
1353 against it in addition to the reloading needed. */
1354 int reject;
1355 /* The number of elements in the following array. */
1356 int early_clobbered_regs_num;
1357 /* Numbers of operands which are early clobber registers. */
1358 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1359 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1360 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1361 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1362 bool curr_alt_win[MAX_RECOG_OPERANDS];
1363 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1364 int curr_alt_matches[MAX_RECOG_OPERANDS];
1365 /* The number of elements in the following array. */
1366 int curr_alt_dont_inherit_ops_num;
1367 /* Numbers of operands whose reload pseudos should not be inherited. */
1368 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1369 rtx op;
1370 /* The register when the operand is a subreg of register, otherwise the
1371 operand itself. */
1372 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1373 /* The register if the operand is a register or subreg of register,
1374 otherwise NULL. */
1375 rtx operand_reg[MAX_RECOG_OPERANDS];
1376 int hard_regno[MAX_RECOG_OPERANDS];
1377 enum machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1378 int reload_nregs, reload_sum;
1379 bool costly_p;
1380 enum reg_class cl;
1382 /* Calculate some data common for all alternatives to speed up the
1383 function. */
1384 for (nop = 0; nop < n_operands; nop++)
1386 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1387 /* The real hard regno of the operand after the allocation. */
1388 hard_regno[nop] = get_hard_regno (op);
1390 operand_reg[nop] = op;
1391 biggest_mode[nop] = GET_MODE (operand_reg[nop]);
1392 if (GET_CODE (operand_reg[nop]) == SUBREG)
1394 operand_reg[nop] = SUBREG_REG (operand_reg[nop]);
1395 if (GET_MODE_SIZE (biggest_mode[nop])
1396 < GET_MODE_SIZE (GET_MODE (operand_reg[nop])))
1397 biggest_mode[nop] = GET_MODE (operand_reg[nop]);
1399 if (REG_P (operand_reg[nop]))
1400 no_subreg_reg_operand[nop] = operand_reg[nop];
1401 else
1402 operand_reg[nop] = NULL_RTX;
1405 /* The constraints are made of several alternatives. Each operand's
1406 constraint looks like foo,bar,... with commas separating the
1407 alternatives. The first alternatives for all operands go
1408 together, the second alternatives go together, etc.
1410 First loop over alternatives. */
1411 for (nalt = 0; nalt < n_alternatives; nalt++)
1413 /* Loop over operands for one constraint alternative. */
1414 #if HAVE_ATTR_enabled
1415 if (curr_id->alternative_enabled_p != NULL
1416 && ! curr_id->alternative_enabled_p[nalt])
1417 continue;
1418 #endif
1420 if (only_alternative >= 0 && nalt != only_alternative)
1421 continue;
1424 overall = losers = reject = reload_nregs = reload_sum = 0;
1425 for (nop = 0; nop < n_operands; nop++)
1426 reject += (curr_static_id
1427 ->operand_alternative[nalt * n_operands + nop].reject);
1428 early_clobbered_regs_num = 0;
1430 for (nop = 0; nop < n_operands; nop++)
1432 const char *p;
1433 char *end;
1434 int len, c, m, i, opalt_num, this_alternative_matches;
1435 bool win, did_match, offmemok, early_clobber_p;
1436 /* false => this operand can be reloaded somehow for this
1437 alternative. */
1438 bool badop;
1439 /* true => this operand can be reloaded if the alternative
1440 allows regs. */
1441 bool winreg;
1442 /* True if a constant forced into memory would be OK for
1443 this operand. */
1444 bool constmemok;
1445 enum reg_class this_alternative, this_costly_alternative;
1446 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1447 bool this_alternative_match_win, this_alternative_win;
1448 bool this_alternative_offmemok;
1449 enum machine_mode mode;
1451 opalt_num = nalt * n_operands + nop;
1452 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1454 /* Fast track for no constraints at all. */
1455 curr_alt[nop] = NO_REGS;
1456 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1457 curr_alt_win[nop] = true;
1458 curr_alt_match_win[nop] = false;
1459 curr_alt_offmemok[nop] = false;
1460 curr_alt_matches[nop] = -1;
1461 continue;
1464 op = no_subreg_reg_operand[nop];
1465 mode = curr_operand_mode[nop];
1467 win = did_match = winreg = offmemok = constmemok = false;
1468 badop = true;
1470 early_clobber_p = false;
1471 p = curr_static_id->operand_alternative[opalt_num].constraint;
1473 this_costly_alternative = this_alternative = NO_REGS;
1474 /* We update set of possible hard regs besides its class
1475 because reg class might be inaccurate. For example,
1476 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1477 is translated in HI_REGS because classes are merged by
1478 pairs and there is no accurate intermediate class. */
1479 CLEAR_HARD_REG_SET (this_alternative_set);
1480 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1481 this_alternative_win = false;
1482 this_alternative_match_win = false;
1483 this_alternative_offmemok = false;
1484 this_alternative_matches = -1;
1486 /* An empty constraint should be excluded by the fast
1487 track. */
1488 lra_assert (*p != 0 && *p != ',');
1490 /* Scan this alternative's specs for this operand; set WIN
1491 if the operand fits any letter in this alternative.
1492 Otherwise, clear BADOP if this operand could fit some
1493 letter after reloads, or set WINREG if this operand could
1494 fit after reloads provided the constraint allows some
1495 registers. */
1496 costly_p = false;
1499 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1501 case '\0':
1502 len = 0;
1503 break;
1504 case ',':
1505 c = '\0';
1506 break;
1508 case '=': case '+': case '?': case '*': case '!':
1509 case ' ': case '\t':
1510 break;
1512 case '%':
1513 /* We only support one commutative marker, the first
1514 one. We already set commutative above. */
1515 break;
1517 case '&':
1518 early_clobber_p = true;
1519 break;
1521 case '#':
1522 /* Ignore rest of this alternative. */
1523 c = '\0';
1524 break;
1526 case '0': case '1': case '2': case '3': case '4':
1527 case '5': case '6': case '7': case '8': case '9':
1529 int m_hregno;
1530 bool match_p;
1532 m = strtoul (p, &end, 10);
1533 p = end;
1534 len = 0;
1535 lra_assert (nop > m);
1537 this_alternative_matches = m;
1538 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1539 /* We are supposed to match a previous operand.
1540 If we do, we win if that one did. If we do
1541 not, count both of the operands as losers.
1542 (This is too conservative, since most of the
1543 time only a single reload insn will be needed
1544 to make the two operands win. As a result,
1545 this alternative may be rejected when it is
1546 actually desirable.) */
1547 match_p = false;
1548 if (operands_match_p (*curr_id->operand_loc[nop],
1549 *curr_id->operand_loc[m], m_hregno))
1551 /* We should reject matching of an early
1552 clobber operand if the matching operand is
1553 not dying in the insn. */
1554 if (! curr_static_id->operand[m].early_clobber
1555 || operand_reg[nop] == NULL_RTX
1556 || (find_regno_note (curr_insn, REG_DEAD,
1557 REGNO (op))
1558 || REGNO (op) == REGNO (operand_reg[m])))
1559 match_p = true;
1561 if (match_p)
1563 /* If we are matching a non-offsettable
1564 address where an offsettable address was
1565 expected, then we must reject this
1566 combination, because we can't reload
1567 it. */
1568 if (curr_alt_offmemok[m]
1569 && MEM_P (*curr_id->operand_loc[m])
1570 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1571 continue;
1574 else
1576 /* Operands don't match. Both operands must
1577 allow a reload register, otherwise we
1578 cannot make them match. */
1579 if (curr_alt[m] == NO_REGS)
1580 break;
1581 /* Retroactively mark the operand we had to
1582 match as a loser, if it wasn't already and
1583 it wasn't matched to a register constraint
1584 (e.g it might be matched by memory). */
1585 if (curr_alt_win[m]
1586 && (operand_reg[m] == NULL_RTX
1587 || hard_regno[m] < 0))
1589 losers++;
1590 reload_nregs
1591 += (ira_reg_class_max_nregs[curr_alt[m]]
1592 [GET_MODE (*curr_id->operand_loc[m])]);
1595 /* We prefer no matching alternatives because
1596 it gives more freedom in RA. */
1597 if (operand_reg[nop] == NULL_RTX
1598 || (find_regno_note (curr_insn, REG_DEAD,
1599 REGNO (operand_reg[nop]))
1600 == NULL_RTX))
1601 reject += 2;
1603 /* If we have to reload this operand and some
1604 previous operand also had to match the same
1605 thing as this operand, we don't know how to do
1606 that. */
1607 if (!match_p || !curr_alt_win[m])
1609 for (i = 0; i < nop; i++)
1610 if (curr_alt_matches[i] == m)
1611 break;
1612 if (i < nop)
1613 break;
1615 else
1616 did_match = true;
1618 /* This can be fixed with reloads if the operand
1619 we are supposed to match can be fixed with
1620 reloads. */
1621 badop = false;
1622 this_alternative = curr_alt[m];
1623 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1624 winreg = this_alternative != NO_REGS;
1625 break;
1628 case 'p':
1629 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1630 ADDRESS, SCRATCH);
1631 this_alternative = reg_class_subunion[this_alternative][cl];
1632 IOR_HARD_REG_SET (this_alternative_set,
1633 reg_class_contents[cl]);
1634 if (costly_p)
1636 this_costly_alternative
1637 = reg_class_subunion[this_costly_alternative][cl];
1638 IOR_HARD_REG_SET (this_costly_alternative_set,
1639 reg_class_contents[cl]);
1641 win = true;
1642 badop = false;
1643 break;
1645 case TARGET_MEM_CONSTRAINT:
1646 if (MEM_P (op) || spilled_pseudo_p (op))
1647 win = true;
1648 /* We can put constant or pseudo value into memory
1649 to satisfy the constraint. */
1650 if (CONST_POOL_OK_P (mode, op) || REG_P (op))
1651 badop = false;
1652 constmemok = true;
1653 break;
1655 case '<':
1656 if (MEM_P (op)
1657 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1658 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1659 win = true;
1660 break;
1662 case '>':
1663 if (MEM_P (op)
1664 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1665 || GET_CODE (XEXP (op, 0)) == POST_INC))
1666 win = true;
1667 break;
1669 /* Memory op whose address is not offsettable. */
1670 case 'V':
1671 if (MEM_P (op)
1672 && ! offsettable_nonstrict_memref_p (op))
1673 win = true;
1674 break;
1676 /* Memory operand whose address is offsettable. */
1677 case 'o':
1678 if ((MEM_P (op)
1679 && offsettable_nonstrict_memref_p (op))
1680 || spilled_pseudo_p (op))
1681 win = true;
1682 /* We can put constant or pseudo value into memory
1683 or make memory address offsetable to satisfy the
1684 constraint. */
1685 if (CONST_POOL_OK_P (mode, op) || MEM_P (op) || REG_P (op))
1686 badop = false;
1687 constmemok = true;
1688 offmemok = true;
1689 break;
1691 case 'E':
1692 case 'F':
1693 if (GET_CODE (op) == CONST_DOUBLE
1694 || (GET_CODE (op) == CONST_VECTOR
1695 && (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)))
1696 win = true;
1697 break;
1699 case 'G':
1700 case 'H':
1701 if (CONST_DOUBLE_AS_FLOAT_P (op)
1702 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1703 win = true;
1704 break;
1706 case 's':
1707 if (CONST_SCALAR_INT_P (op))
1708 break;
1710 case 'i':
1711 if (general_constant_p (op))
1712 win = true;
1713 break;
1715 case 'n':
1716 if (CONST_SCALAR_INT_P (op))
1717 win = true;
1718 break;
1720 case 'I':
1721 case 'J':
1722 case 'K':
1723 case 'L':
1724 case 'M':
1725 case 'N':
1726 case 'O':
1727 case 'P':
1728 if (CONST_INT_P (op)
1729 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1730 win = true;
1731 break;
1733 case 'X':
1734 /* This constraint should be excluded by the fast
1735 track. */
1736 gcc_unreachable ();
1737 break;
1739 case 'g':
1740 if (MEM_P (op)
1741 || general_constant_p (op)
1742 || spilled_pseudo_p (op))
1743 win = true;
1744 /* Drop through into 'r' case. */
1746 case 'r':
1747 this_alternative
1748 = reg_class_subunion[this_alternative][GENERAL_REGS];
1749 IOR_HARD_REG_SET (this_alternative_set,
1750 reg_class_contents[GENERAL_REGS]);
1751 if (costly_p)
1753 this_costly_alternative
1754 = (reg_class_subunion
1755 [this_costly_alternative][GENERAL_REGS]);
1756 IOR_HARD_REG_SET (this_costly_alternative_set,
1757 reg_class_contents[GENERAL_REGS]);
1759 goto reg;
1761 default:
1762 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
1764 #ifdef EXTRA_CONSTRAINT_STR
1765 if (EXTRA_MEMORY_CONSTRAINT (c, p))
1767 if (EXTRA_CONSTRAINT_STR (op, c, p))
1768 win = true;
1769 else if (spilled_pseudo_p (op))
1770 win = true;
1772 /* If we didn't already win, we can reload
1773 constants via force_const_mem or put the
1774 pseudo value into memory, or make other
1775 memory by reloading the address like for
1776 'o'. */
1777 if (CONST_POOL_OK_P (mode, op)
1778 || MEM_P (op) || REG_P (op))
1779 badop = false;
1780 constmemok = true;
1781 offmemok = true;
1782 break;
1784 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1786 if (EXTRA_CONSTRAINT_STR (op, c, p))
1787 win = true;
1789 /* If we didn't already win, we can reload
1790 the address into a base register. */
1791 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1792 ADDRESS, SCRATCH);
1793 this_alternative
1794 = reg_class_subunion[this_alternative][cl];
1795 IOR_HARD_REG_SET (this_alternative_set,
1796 reg_class_contents[cl]);
1797 if (costly_p)
1799 this_costly_alternative
1800 = (reg_class_subunion
1801 [this_costly_alternative][cl]);
1802 IOR_HARD_REG_SET (this_costly_alternative_set,
1803 reg_class_contents[cl]);
1805 badop = false;
1806 break;
1809 if (EXTRA_CONSTRAINT_STR (op, c, p))
1810 win = true;
1811 #endif
1812 break;
1815 cl = REG_CLASS_FROM_CONSTRAINT (c, p);
1816 this_alternative = reg_class_subunion[this_alternative][cl];
1817 IOR_HARD_REG_SET (this_alternative_set,
1818 reg_class_contents[cl]);
1819 if (costly_p)
1821 this_costly_alternative
1822 = reg_class_subunion[this_costly_alternative][cl];
1823 IOR_HARD_REG_SET (this_costly_alternative_set,
1824 reg_class_contents[cl]);
1826 reg:
1827 if (mode == BLKmode)
1828 break;
1829 winreg = true;
1830 if (REG_P (op))
1832 if (hard_regno[nop] >= 0
1833 && in_hard_reg_set_p (this_alternative_set,
1834 mode, hard_regno[nop]))
1835 win = true;
1836 else if (hard_regno[nop] < 0
1837 && in_class_p (op, this_alternative, NULL))
1838 win = true;
1840 break;
1842 if (c != ' ' && c != '\t')
1843 costly_p = c == '*';
1845 while ((p += len), c);
1847 /* Record which operands fit this alternative. */
1848 if (win)
1850 this_alternative_win = true;
1851 if (operand_reg[nop] != NULL_RTX)
1853 if (hard_regno[nop] >= 0)
1855 if (in_hard_reg_set_p (this_costly_alternative_set,
1856 mode, hard_regno[nop]))
1857 reject++;
1859 else
1861 /* Prefer won reg to spilled pseudo under other equal
1862 conditions. */
1863 reject++;
1864 if (in_class_p (operand_reg[nop],
1865 this_costly_alternative, NULL))
1866 reject++;
1868 /* We simulate the behaviour of old reload here.
1869 Although scratches need hard registers and it
1870 might result in spilling other pseudos, no reload
1871 insns are generated for the scratches. So it
1872 might cost something but probably less than old
1873 reload pass believes. */
1874 if (lra_former_scratch_p (REGNO (operand_reg[nop])))
1875 reject += LRA_LOSER_COST_FACTOR;
1878 else if (did_match)
1879 this_alternative_match_win = true;
1880 else
1882 int const_to_mem = 0;
1883 bool no_regs_p;
1885 /* If this alternative asks for a specific reg class, see if there
1886 is at least one allocatable register in that class. */
1887 no_regs_p
1888 = (this_alternative == NO_REGS
1889 || (hard_reg_set_subset_p
1890 (reg_class_contents[this_alternative],
1891 lra_no_alloc_regs)));
1893 /* For asms, verify that the class for this alternative is possible
1894 for the mode that is specified. */
1895 if (!no_regs_p && REG_P (op) && INSN_CODE (curr_insn) < 0)
1897 int i;
1898 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1899 if (HARD_REGNO_MODE_OK (i, mode)
1900 && in_hard_reg_set_p (reg_class_contents[this_alternative], mode, i))
1901 break;
1902 if (i == FIRST_PSEUDO_REGISTER)
1903 winreg = false;
1906 /* If this operand accepts a register, and if the
1907 register class has at least one allocatable register,
1908 then this operand can be reloaded. */
1909 if (winreg && !no_regs_p)
1910 badop = false;
1912 if (badop)
1913 goto fail;
1915 this_alternative_offmemok = offmemok;
1916 if (this_costly_alternative != NO_REGS)
1917 reject++;
1918 /* If the operand is dying, has a matching constraint,
1919 and satisfies constraints of the matched operand
1920 which failed to satisfy the own constraints, we do
1921 not need to generate a reload insn for this
1922 operand. */
1923 if (!(this_alternative_matches >= 0
1924 && !curr_alt_win[this_alternative_matches]
1925 && REG_P (op)
1926 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
1927 && (hard_regno[nop] >= 0
1928 ? in_hard_reg_set_p (this_alternative_set,
1929 mode, hard_regno[nop])
1930 : in_class_p (op, this_alternative, NULL))))
1932 /* Strict_low_part requires to reload the register
1933 not the sub-register. In this case we should
1934 check that a final reload hard reg can hold the
1935 value mode. */
1936 if (curr_static_id->operand[nop].strict_low
1937 && REG_P (op)
1938 && hard_regno[nop] < 0
1939 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
1940 && ira_class_hard_regs_num[this_alternative] > 0
1941 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
1942 [this_alternative][0],
1943 GET_MODE (op)))
1944 goto fail;
1945 losers++;
1947 if (operand_reg[nop] != NULL_RTX
1948 /* Output operands and matched input operands are
1949 not inherited. The following conditions do not
1950 exactly describe the previous statement but they
1951 are pretty close. */
1952 && curr_static_id->operand[nop].type != OP_OUT
1953 && (this_alternative_matches < 0
1954 || curr_static_id->operand[nop].type != OP_IN))
1956 int last_reload = (lra_reg_info[ORIGINAL_REGNO
1957 (operand_reg[nop])]
1958 .last_reload);
1960 if (last_reload > bb_reload_num)
1961 reload_sum += last_reload - bb_reload_num;
1963 /* If this is a constant that is reloaded into the
1964 desired class by copying it to memory first, count
1965 that as another reload. This is consistent with
1966 other code and is required to avoid choosing another
1967 alternative when the constant is moved into memory.
1968 Note that the test here is precisely the same as in
1969 the code below that calls force_const_mem. */
1970 if (CONST_POOL_OK_P (mode, op)
1971 && ((targetm.preferred_reload_class
1972 (op, this_alternative) == NO_REGS)
1973 || no_input_reloads_p))
1975 const_to_mem = 1;
1976 if (! no_regs_p)
1977 losers++;
1980 /* Alternative loses if it requires a type of reload not
1981 permitted for this insn. We can always reload
1982 objects with a REG_UNUSED note. */
1983 if ((curr_static_id->operand[nop].type != OP_IN
1984 && no_output_reloads_p
1985 && ! find_reg_note (curr_insn, REG_UNUSED, op))
1986 || (curr_static_id->operand[nop].type != OP_OUT
1987 && no_input_reloads_p && ! const_to_mem))
1988 goto fail;
1990 /* Check strong discouragement of reload of non-constant
1991 into class THIS_ALTERNATIVE. */
1992 if (! CONSTANT_P (op) && ! no_regs_p
1993 && (targetm.preferred_reload_class
1994 (op, this_alternative) == NO_REGS
1995 || (curr_static_id->operand[nop].type == OP_OUT
1996 && (targetm.preferred_output_reload_class
1997 (op, this_alternative) == NO_REGS))))
1998 reject += LRA_MAX_REJECT;
2000 if (! (MEM_P (op) && offmemok)
2001 && ! (const_to_mem && constmemok))
2003 /* We prefer to reload pseudos over reloading other
2004 things, since such reloads may be able to be
2005 eliminated later. So bump REJECT in other cases.
2006 Don't do this in the case where we are forcing a
2007 constant into memory and it will then win since
2008 we don't want to have a different alternative
2009 match then. */
2010 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2011 reject += 2;
2013 if (! no_regs_p)
2014 reload_nregs
2015 += ira_reg_class_max_nregs[this_alternative][mode];
2017 if (SMALL_REGISTER_CLASS_P (this_alternative))
2018 reject += LRA_LOSER_COST_FACTOR / 2;
2021 /* We are trying to spill pseudo into memory. It is
2022 usually more costly than moving to a hard register
2023 although it might takes the same number of
2024 reloads. */
2025 if (no_regs_p && REG_P (op))
2026 reject += 2;
2028 #ifdef SECONDARY_MEMORY_NEEDED
2029 /* If reload requires moving value through secondary
2030 memory, it will need one more insn at least. */
2031 if (this_alternative != NO_REGS
2032 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2033 && ((curr_static_id->operand[nop].type != OP_OUT
2034 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2035 GET_MODE (op)))
2036 || (curr_static_id->operand[nop].type != OP_IN
2037 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2038 GET_MODE (op)))))
2039 losers++;
2040 #endif
2041 /* Input reloads can be inherited more often than output
2042 reloads can be removed, so penalize output
2043 reloads. */
2044 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2045 reject++;
2049 if (early_clobber_p)
2050 reject++;
2051 /* ??? We check early clobbers after processing all operands
2052 (see loop below) and there we update the costs more.
2053 Should we update the cost (may be approximately) here
2054 because of early clobber register reloads or it is a rare
2055 or non-important thing to be worth to do it. */
2056 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2057 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2059 if (lra_dump_file != NULL)
2060 fprintf (lra_dump_file,
2061 " alt=%d,overall=%d,losers=%d -- reject\n",
2062 nalt, overall, losers);
2063 goto fail;
2066 curr_alt[nop] = this_alternative;
2067 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2068 curr_alt_win[nop] = this_alternative_win;
2069 curr_alt_match_win[nop] = this_alternative_match_win;
2070 curr_alt_offmemok[nop] = this_alternative_offmemok;
2071 curr_alt_matches[nop] = this_alternative_matches;
2073 if (this_alternative_matches >= 0
2074 && !did_match && !this_alternative_win)
2075 curr_alt_win[this_alternative_matches] = false;
2077 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2078 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2080 if (curr_insn_set != NULL_RTX && n_operands == 2
2081 /* Prevent processing non-move insns. */
2082 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2083 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2084 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2085 && REG_P (no_subreg_reg_operand[0])
2086 && REG_P (no_subreg_reg_operand[1])
2087 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2088 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2089 || (! curr_alt_win[0] && curr_alt_win[1]
2090 && REG_P (no_subreg_reg_operand[1])
2091 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2092 || (curr_alt_win[0] && ! curr_alt_win[1]
2093 && REG_P (no_subreg_reg_operand[0])
2094 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2095 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2096 no_subreg_reg_operand[1])
2097 || (targetm.preferred_reload_class
2098 (no_subreg_reg_operand[1],
2099 (enum reg_class) curr_alt[1]) != NO_REGS))
2100 /* If it is a result of recent elimination in move
2101 insn we can transform it into an add still by
2102 using this alternative. */
2103 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2104 /* We have a move insn and a new reload insn will be similar
2105 to the current insn. We should avoid such situation as it
2106 results in LRA cycling. */
2107 overall += LRA_MAX_REJECT;
2108 ok_p = true;
2109 curr_alt_dont_inherit_ops_num = 0;
2110 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2112 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2113 HARD_REG_SET temp_set;
2115 i = early_clobbered_nops[nop];
2116 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2117 || hard_regno[i] < 0)
2118 continue;
2119 lra_assert (operand_reg[i] != NULL_RTX);
2120 clobbered_hard_regno = hard_regno[i];
2121 CLEAR_HARD_REG_SET (temp_set);
2122 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2123 first_conflict_j = last_conflict_j = -1;
2124 for (j = 0; j < n_operands; j++)
2125 if (j == i
2126 /* We don't want process insides of match_operator and
2127 match_parallel because otherwise we would process
2128 their operands once again generating a wrong
2129 code. */
2130 || curr_static_id->operand[j].is_operator)
2131 continue;
2132 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2133 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2134 continue;
2135 /* If we don't reload j-th operand, check conflicts. */
2136 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2137 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2139 if (first_conflict_j < 0)
2140 first_conflict_j = j;
2141 last_conflict_j = j;
2143 if (last_conflict_j < 0)
2144 continue;
2145 /* If earlyclobber operand conflicts with another
2146 non-matching operand which is actually the same register
2147 as the earlyclobber operand, it is better to reload the
2148 another operand as an operand matching the earlyclobber
2149 operand can be also the same. */
2150 if (first_conflict_j == last_conflict_j
2151 && operand_reg[last_conflict_j]
2152 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2153 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2155 curr_alt_win[last_conflict_j] = false;
2156 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2157 = last_conflict_j;
2158 losers++;
2159 /* Early clobber was already reflected in REJECT. */
2160 lra_assert (reject > 0);
2161 reject--;
2162 overall += LRA_LOSER_COST_FACTOR - 1;
2164 else
2166 /* We need to reload early clobbered register and the
2167 matched registers. */
2168 for (j = 0; j < n_operands; j++)
2169 if (curr_alt_matches[j] == i)
2171 curr_alt_match_win[j] = false;
2172 losers++;
2173 overall += LRA_LOSER_COST_FACTOR;
2175 if (! curr_alt_match_win[i])
2176 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2177 else
2179 /* Remember pseudos used for match reloads are never
2180 inherited. */
2181 lra_assert (curr_alt_matches[i] >= 0);
2182 curr_alt_win[curr_alt_matches[i]] = false;
2184 curr_alt_win[i] = curr_alt_match_win[i] = false;
2185 losers++;
2186 /* Early clobber was already reflected in REJECT. */
2187 lra_assert (reject > 0);
2188 reject--;
2189 overall += LRA_LOSER_COST_FACTOR - 1;
2192 if (lra_dump_file != NULL)
2193 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2194 nalt, overall, losers, reload_nregs);
2196 /* If this alternative can be made to work by reloading, and it
2197 needs less reloading than the others checked so far, record
2198 it as the chosen goal for reloading. */
2199 if ((best_losers != 0 && losers == 0)
2200 || (((best_losers == 0 && losers == 0)
2201 || (best_losers != 0 && losers != 0))
2202 && (best_overall > overall
2203 || (best_overall == overall
2204 /* If the cost of the reloads is the same,
2205 prefer alternative which requires minimal
2206 number of reload regs. */
2207 && (reload_nregs < best_reload_nregs
2208 || (reload_nregs == best_reload_nregs
2209 && (best_reload_sum < reload_sum
2210 || (best_reload_sum == reload_sum
2211 && nalt < goal_alt_number))))))))
2213 for (nop = 0; nop < n_operands; nop++)
2215 goal_alt_win[nop] = curr_alt_win[nop];
2216 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2217 goal_alt_matches[nop] = curr_alt_matches[nop];
2218 goal_alt[nop] = curr_alt[nop];
2219 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2221 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2222 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2223 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2224 goal_alt_swapped = curr_swapped;
2225 best_overall = overall;
2226 best_losers = losers;
2227 best_reload_nregs = reload_nregs;
2228 best_reload_sum = reload_sum;
2229 goal_alt_number = nalt;
2231 if (losers == 0)
2232 /* Everything is satisfied. Do not process alternatives
2233 anymore. */
2234 break;
2235 fail:
2238 return ok_p;
2241 /* Return 1 if ADDR is a valid memory address for mode MODE in address
2242 space AS, and check that each pseudo has the proper kind of hard
2243 reg. */
2244 static int
2245 valid_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2246 rtx addr, addr_space_t as)
2248 #ifdef GO_IF_LEGITIMATE_ADDRESS
2249 lra_assert (ADDR_SPACE_GENERIC_P (as));
2250 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
2251 return 0;
2253 win:
2254 return 1;
2255 #else
2256 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
2257 #endif
2260 /* Return whether address AD is valid. */
2262 static bool
2263 valid_address_p (struct address_info *ad)
2265 /* Some ports do not check displacements for eliminable registers,
2266 so we replace them temporarily with the elimination target. */
2267 rtx saved_base_reg = NULL_RTX;
2268 rtx saved_index_reg = NULL_RTX;
2269 rtx *base_term = strip_subreg (ad->base_term);
2270 rtx *index_term = strip_subreg (ad->index_term);
2271 if (base_term != NULL)
2273 saved_base_reg = *base_term;
2274 lra_eliminate_reg_if_possible (base_term);
2275 if (ad->base_term2 != NULL)
2276 *ad->base_term2 = *ad->base_term;
2278 if (index_term != NULL)
2280 saved_index_reg = *index_term;
2281 lra_eliminate_reg_if_possible (index_term);
2283 bool ok_p = valid_address_p (ad->mode, *ad->outer, ad->as);
2284 if (saved_base_reg != NULL_RTX)
2286 *base_term = saved_base_reg;
2287 if (ad->base_term2 != NULL)
2288 *ad->base_term2 = *ad->base_term;
2290 if (saved_index_reg != NULL_RTX)
2291 *index_term = saved_index_reg;
2292 return ok_p;
2295 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2296 static rtx
2297 base_plus_disp_to_reg (struct address_info *ad)
2299 enum reg_class cl;
2300 rtx new_reg;
2302 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2303 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2304 get_index_code (ad));
2305 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2306 cl, "base + disp");
2307 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2308 return new_reg;
2311 /* Return true if we can add a displacement to address AD, even if that
2312 makes the address invalid. The fix-up code requires any new address
2313 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2314 static bool
2315 can_add_disp_p (struct address_info *ad)
2317 return (!ad->autoinc_p
2318 && ad->segment == NULL
2319 && ad->base == ad->base_term
2320 && ad->disp == ad->disp_term);
2323 /* Make equiv substitution in address AD. Return true if a substitution
2324 was made. */
2325 static bool
2326 equiv_address_substitution (struct address_info *ad)
2328 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2329 HOST_WIDE_INT disp, scale;
2330 bool change_p;
2332 base_term = strip_subreg (ad->base_term);
2333 if (base_term == NULL)
2334 base_reg = new_base_reg = NULL_RTX;
2335 else
2337 base_reg = *base_term;
2338 new_base_reg = get_equiv_substitution (base_reg);
2340 index_term = strip_subreg (ad->index_term);
2341 if (index_term == NULL)
2342 index_reg = new_index_reg = NULL_RTX;
2343 else
2345 index_reg = *index_term;
2346 new_index_reg = get_equiv_substitution (index_reg);
2348 if (base_reg == new_base_reg && index_reg == new_index_reg)
2349 return false;
2350 disp = 0;
2351 change_p = false;
2352 if (lra_dump_file != NULL)
2354 fprintf (lra_dump_file, "Changing address in insn %d ",
2355 INSN_UID (curr_insn));
2356 dump_value_slim (lra_dump_file, *ad->outer, 1);
2358 if (base_reg != new_base_reg)
2360 if (REG_P (new_base_reg))
2362 *base_term = new_base_reg;
2363 change_p = true;
2365 else if (GET_CODE (new_base_reg) == PLUS
2366 && REG_P (XEXP (new_base_reg, 0))
2367 && CONST_INT_P (XEXP (new_base_reg, 1))
2368 && can_add_disp_p (ad))
2370 disp += INTVAL (XEXP (new_base_reg, 1));
2371 *base_term = XEXP (new_base_reg, 0);
2372 change_p = true;
2374 if (ad->base_term2 != NULL)
2375 *ad->base_term2 = *ad->base_term;
2377 if (index_reg != new_index_reg)
2379 if (REG_P (new_index_reg))
2381 *index_term = new_index_reg;
2382 change_p = true;
2384 else if (GET_CODE (new_index_reg) == PLUS
2385 && REG_P (XEXP (new_index_reg, 0))
2386 && CONST_INT_P (XEXP (new_index_reg, 1))
2387 && can_add_disp_p (ad)
2388 && (scale = get_index_scale (ad)))
2390 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2391 *index_term = XEXP (new_index_reg, 0);
2392 change_p = true;
2395 if (disp != 0)
2397 if (ad->disp != NULL)
2398 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2399 else
2401 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2402 update_address (ad);
2404 change_p = true;
2406 if (lra_dump_file != NULL)
2408 if (! change_p)
2409 fprintf (lra_dump_file, " -- no change\n");
2410 else
2412 fprintf (lra_dump_file, " on equiv ");
2413 dump_value_slim (lra_dump_file, *ad->outer, 1);
2414 fprintf (lra_dump_file, "\n");
2417 return change_p;
2420 /* Major function to make reloads for an address in operand NOP.
2421 The supported cases are:
2423 1) an address that existed before LRA started, at which point it
2424 must have been valid. These addresses are subject to elimination
2425 and may have become invalid due to the elimination offset being out
2426 of range.
2428 2) an address created by forcing a constant to memory
2429 (force_const_to_mem). The initial form of these addresses might
2430 not be valid, and it is this function's job to make them valid.
2432 3) a frame address formed from a register and a (possibly zero)
2433 constant offset. As above, these addresses might not be valid and
2434 this function must make them so.
2436 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2437 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2438 address. Return true for any RTL change. */
2439 static bool
2440 process_address (int nop, rtx *before, rtx *after)
2442 struct address_info ad;
2443 rtx new_reg;
2444 rtx op = *curr_id->operand_loc[nop];
2445 const char *constraint = curr_static_id->operand[nop].constraint;
2446 bool change_p;
2448 if (constraint[0] == 'p'
2449 || EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint))
2450 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2451 else if (MEM_P (op))
2452 decompose_mem_address (&ad, op);
2453 else if (GET_CODE (op) == SUBREG
2454 && MEM_P (SUBREG_REG (op)))
2455 decompose_mem_address (&ad, SUBREG_REG (op));
2456 else
2457 return false;
2458 change_p = equiv_address_substitution (&ad);
2459 if (ad.base_term != NULL
2460 && (process_addr_reg
2461 (ad.base_term, before,
2462 (ad.autoinc_p
2463 && !(REG_P (*ad.base_term)
2464 && find_regno_note (curr_insn, REG_DEAD,
2465 REGNO (*ad.base_term)) != NULL_RTX)
2466 ? after : NULL),
2467 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2468 get_index_code (&ad)))))
2470 change_p = true;
2471 if (ad.base_term2 != NULL)
2472 *ad.base_term2 = *ad.base_term;
2474 if (ad.index_term != NULL
2475 && process_addr_reg (ad.index_term, before, NULL, INDEX_REG_CLASS))
2476 change_p = true;
2478 #ifdef EXTRA_CONSTRAINT_STR
2479 /* Target hooks sometimes reject extra constraint addresses -- use
2480 EXTRA_CONSTRAINT_STR for the validation. */
2481 if (constraint[0] != 'p'
2482 && EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint)
2483 && EXTRA_CONSTRAINT_STR (op, constraint[0], constraint))
2484 return change_p;
2485 #endif
2487 /* There are three cases where the shape of *AD.INNER may now be invalid:
2489 1) the original address was valid, but either elimination or
2490 equiv_address_substitution was applied and that made
2491 the address invalid.
2493 2) the address is an invalid symbolic address created by
2494 force_const_to_mem.
2496 3) the address is a frame address with an invalid offset.
2498 All these cases involve a non-autoinc address, so there is no
2499 point revalidating other types. */
2500 if (ad.autoinc_p || valid_address_p (&ad))
2501 return change_p;
2503 /* Any index existed before LRA started, so we can assume that the
2504 presence and shape of the index is valid. */
2505 push_to_sequence (*before);
2506 lra_assert (ad.disp == ad.disp_term);
2507 if (ad.base == NULL)
2509 if (ad.index == NULL)
2511 int code = -1;
2512 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2513 SCRATCH, SCRATCH);
2514 rtx addr = *ad.inner;
2516 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2517 #ifdef HAVE_lo_sum
2519 rtx insn;
2520 rtx last = get_last_insn ();
2522 /* addr => lo_sum (new_base, addr), case (2) above. */
2523 insn = emit_insn (gen_rtx_SET
2524 (VOIDmode, new_reg,
2525 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2526 code = recog_memoized (insn);
2527 if (code >= 0)
2529 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2530 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2532 /* Try to put lo_sum into register. */
2533 insn = emit_insn (gen_rtx_SET
2534 (VOIDmode, new_reg,
2535 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2536 code = recog_memoized (insn);
2537 if (code >= 0)
2539 *ad.inner = new_reg;
2540 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2542 *ad.inner = addr;
2543 code = -1;
2549 if (code < 0)
2550 delete_insns_since (last);
2552 #endif
2553 if (code < 0)
2555 /* addr => new_base, case (2) above. */
2556 lra_emit_move (new_reg, addr);
2557 *ad.inner = new_reg;
2560 else
2562 /* index * scale + disp => new base + index * scale,
2563 case (1) above. */
2564 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
2565 GET_CODE (*ad.index));
2567 lra_assert (INDEX_REG_CLASS != NO_REGS);
2568 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2569 lra_emit_move (new_reg, *ad.disp);
2570 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2571 new_reg, *ad.index);
2574 else if (ad.index == NULL)
2576 int regno;
2577 enum reg_class cl;
2578 rtx set, insns, last_insn;
2579 /* base + disp => new base, cases (1) and (3) above. */
2580 /* Another option would be to reload the displacement into an
2581 index register. However, postreload has code to optimize
2582 address reloads that have the same base and different
2583 displacements, so reloading into an index register would
2584 not necessarily be a win. */
2585 start_sequence ();
2586 new_reg = base_plus_disp_to_reg (&ad);
2587 insns = get_insns ();
2588 last_insn = get_last_insn ();
2589 /* If we generated at least two insns, try last insn source as
2590 an address. If we succeed, we generate one less insn. */
2591 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
2592 && GET_CODE (SET_SRC (set)) == PLUS
2593 && REG_P (XEXP (SET_SRC (set), 0))
2594 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
2596 *ad.inner = SET_SRC (set);
2597 if (valid_address_p (ad.mode, *ad.outer, ad.as))
2599 *ad.base_term = XEXP (SET_SRC (set), 0);
2600 *ad.disp_term = XEXP (SET_SRC (set), 1);
2601 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2602 get_index_code (&ad));
2603 regno = REGNO (*ad.base_term);
2604 if (regno >= FIRST_PSEUDO_REGISTER
2605 && cl != lra_get_allocno_class (regno))
2606 change_class (regno, cl, " Change", true);
2607 new_reg = SET_SRC (set);
2608 delete_insns_since (PREV_INSN (last_insn));
2611 end_sequence ();
2612 emit_insn (insns);
2613 *ad.inner = new_reg;
2615 else
2617 /* base + scale * index + disp => new base + scale * index,
2618 case (1) above. */
2619 new_reg = base_plus_disp_to_reg (&ad);
2620 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2621 new_reg, *ad.index);
2623 *before = get_insns ();
2624 end_sequence ();
2625 return true;
2628 /* Emit insns to reload VALUE into a new register. VALUE is an
2629 auto-increment or auto-decrement RTX whose operand is a register or
2630 memory location; so reloading involves incrementing that location.
2631 IN is either identical to VALUE, or some cheaper place to reload
2632 value being incremented/decremented from.
2634 INC_AMOUNT is the number to increment or decrement by (always
2635 positive and ignored for POST_MODIFY/PRE_MODIFY).
2637 Return pseudo containing the result. */
2638 static rtx
2639 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
2641 /* REG or MEM to be copied and incremented. */
2642 rtx incloc = XEXP (value, 0);
2643 /* Nonzero if increment after copying. */
2644 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
2645 || GET_CODE (value) == POST_MODIFY);
2646 rtx last;
2647 rtx inc;
2648 rtx add_insn;
2649 int code;
2650 rtx real_in = in == value ? incloc : in;
2651 rtx result;
2652 bool plus_p = true;
2654 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
2656 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
2657 || GET_CODE (XEXP (value, 1)) == MINUS);
2658 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
2659 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
2660 inc = XEXP (XEXP (value, 1), 1);
2662 else
2664 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
2665 inc_amount = -inc_amount;
2667 inc = GEN_INT (inc_amount);
2670 if (! post && REG_P (incloc))
2671 result = incloc;
2672 else
2673 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
2674 "INC/DEC result");
2676 if (real_in != result)
2678 /* First copy the location to the result register. */
2679 lra_assert (REG_P (result));
2680 emit_insn (gen_move_insn (result, real_in));
2683 /* We suppose that there are insns to add/sub with the constant
2684 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
2685 old reload worked with this assumption. If the assumption
2686 becomes wrong, we should use approach in function
2687 base_plus_disp_to_reg. */
2688 if (in == value)
2690 /* See if we can directly increment INCLOC. */
2691 last = get_last_insn ();
2692 add_insn = emit_insn (plus_p
2693 ? gen_add2_insn (incloc, inc)
2694 : gen_sub2_insn (incloc, inc));
2696 code = recog_memoized (add_insn);
2697 if (code >= 0)
2699 if (! post && result != incloc)
2700 emit_insn (gen_move_insn (result, incloc));
2701 return result;
2703 delete_insns_since (last);
2706 /* If couldn't do the increment directly, must increment in RESULT.
2707 The way we do this depends on whether this is pre- or
2708 post-increment. For pre-increment, copy INCLOC to the reload
2709 register, increment it there, then save back. */
2710 if (! post)
2712 if (real_in != result)
2713 emit_insn (gen_move_insn (result, real_in));
2714 if (plus_p)
2715 emit_insn (gen_add2_insn (result, inc));
2716 else
2717 emit_insn (gen_sub2_insn (result, inc));
2718 if (result != incloc)
2719 emit_insn (gen_move_insn (incloc, result));
2721 else
2723 /* Post-increment.
2725 Because this might be a jump insn or a compare, and because
2726 RESULT may not be available after the insn in an input
2727 reload, we must do the incrementing before the insn being
2728 reloaded for.
2730 We have already copied IN to RESULT. Increment the copy in
2731 RESULT, save that back, then decrement RESULT so it has
2732 the original value. */
2733 if (plus_p)
2734 emit_insn (gen_add2_insn (result, inc));
2735 else
2736 emit_insn (gen_sub2_insn (result, inc));
2737 emit_insn (gen_move_insn (incloc, result));
2738 /* Restore non-modified value for the result. We prefer this
2739 way because it does not require an additional hard
2740 register. */
2741 if (plus_p)
2743 if (CONST_INT_P (inc))
2744 emit_insn (gen_add2_insn (result, GEN_INT (-INTVAL (inc))));
2745 else
2746 emit_insn (gen_sub2_insn (result, inc));
2748 else
2749 emit_insn (gen_add2_insn (result, inc));
2751 return result;
2754 /* Return true if the current move insn does not need processing as we
2755 already know that it satisfies its constraints. */
2756 static bool
2757 simple_move_p (void)
2759 rtx dest, src;
2760 enum reg_class dclass, sclass;
2762 lra_assert (curr_insn_set != NULL_RTX);
2763 dest = SET_DEST (curr_insn_set);
2764 src = SET_SRC (curr_insn_set);
2765 return ((dclass = get_op_class (dest)) != NO_REGS
2766 && (sclass = get_op_class (src)) != NO_REGS
2767 /* The backend guarantees that register moves of cost 2
2768 never need reloads. */
2769 && targetm.register_move_cost (GET_MODE (src), dclass, sclass) == 2);
2772 /* Swap operands NOP and NOP + 1. */
2773 static inline void
2774 swap_operands (int nop)
2776 enum machine_mode mode = curr_operand_mode[nop];
2777 curr_operand_mode[nop] = curr_operand_mode[nop + 1];
2778 curr_operand_mode[nop + 1] = mode;
2779 rtx x = *curr_id->operand_loc[nop];
2780 *curr_id->operand_loc[nop] = *curr_id->operand_loc[nop + 1];
2781 *curr_id->operand_loc[nop + 1] = x;
2782 /* Swap the duplicates too. */
2783 lra_update_dup (curr_id, nop);
2784 lra_update_dup (curr_id, nop + 1);
2787 /* Main entry point of the constraint code: search the body of the
2788 current insn to choose the best alternative. It is mimicking insn
2789 alternative cost calculation model of former reload pass. That is
2790 because machine descriptions were written to use this model. This
2791 model can be changed in future. Make commutative operand exchange
2792 if it is chosen.
2794 Return true if some RTL changes happened during function call. */
2795 static bool
2796 curr_insn_transform (void)
2798 int i, j, k;
2799 int n_operands;
2800 int n_alternatives;
2801 int commutative;
2802 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
2803 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
2804 rtx before, after;
2805 bool alt_p = false;
2806 /* Flag that the insn has been changed through a transformation. */
2807 bool change_p;
2808 bool sec_mem_p;
2809 #ifdef SECONDARY_MEMORY_NEEDED
2810 bool use_sec_mem_p;
2811 #endif
2812 int max_regno_before;
2813 int reused_alternative_num;
2815 curr_insn_set = single_set (curr_insn);
2816 if (curr_insn_set != NULL_RTX && simple_move_p ())
2817 return false;
2819 no_input_reloads_p = no_output_reloads_p = false;
2820 goal_alt_number = -1;
2821 change_p = sec_mem_p = false;
2822 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
2823 reloads; neither are insns that SET cc0. Insns that use CC0 are
2824 not allowed to have any input reloads. */
2825 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
2826 no_output_reloads_p = true;
2828 #ifdef HAVE_cc0
2829 if (reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
2830 no_input_reloads_p = true;
2831 if (reg_set_p (cc0_rtx, PATTERN (curr_insn)))
2832 no_output_reloads_p = true;
2833 #endif
2835 n_operands = curr_static_id->n_operands;
2836 n_alternatives = curr_static_id->n_alternatives;
2838 /* Just return "no reloads" if insn has no operands with
2839 constraints. */
2840 if (n_operands == 0 || n_alternatives == 0)
2841 return false;
2843 max_regno_before = max_reg_num ();
2845 for (i = 0; i < n_operands; i++)
2847 goal_alt_matched[i][0] = -1;
2848 goal_alt_matches[i] = -1;
2851 commutative = curr_static_id->commutative;
2853 /* Now see what we need for pseudos that didn't get hard regs or got
2854 the wrong kind of hard reg. For this, we must consider all the
2855 operands together against the register constraints. */
2857 best_losers = best_overall = INT_MAX;
2858 best_reload_sum = 0;
2860 curr_swapped = false;
2861 goal_alt_swapped = false;
2863 /* Make equivalence substitution and memory subreg elimination
2864 before address processing because an address legitimacy can
2865 depend on memory mode. */
2866 for (i = 0; i < n_operands; i++)
2868 rtx op = *curr_id->operand_loc[i];
2869 rtx subst, old = op;
2870 bool op_change_p = false;
2872 if (GET_CODE (old) == SUBREG)
2873 old = SUBREG_REG (old);
2874 subst = get_equiv_substitution (old);
2875 if (subst != old)
2877 subst = copy_rtx (subst);
2878 lra_assert (REG_P (old));
2879 if (GET_CODE (op) == SUBREG)
2880 SUBREG_REG (op) = subst;
2881 else
2882 *curr_id->operand_loc[i] = subst;
2883 if (lra_dump_file != NULL)
2885 fprintf (lra_dump_file,
2886 "Changing pseudo %d in operand %i of insn %u on equiv ",
2887 REGNO (old), i, INSN_UID (curr_insn));
2888 dump_value_slim (lra_dump_file, subst, 1);
2889 fprintf (lra_dump_file, "\n");
2891 op_change_p = change_p = true;
2893 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
2895 change_p = true;
2896 lra_update_dup (curr_id, i);
2900 /* Reload address registers and displacements. We do it before
2901 finding an alternative because of memory constraints. */
2902 before = after = NULL_RTX;
2903 for (i = 0; i < n_operands; i++)
2904 if (! curr_static_id->operand[i].is_operator
2905 && process_address (i, &before, &after))
2907 change_p = true;
2908 lra_update_dup (curr_id, i);
2911 if (change_p)
2912 /* If we've changed the instruction then any alternative that
2913 we chose previously may no longer be valid. */
2914 lra_set_used_insn_alternative (curr_insn, -1);
2916 if (curr_insn_set != NULL_RTX
2917 && check_and_process_move (&change_p, &sec_mem_p))
2918 return change_p;
2920 try_swapped:
2922 reused_alternative_num = curr_id->used_insn_alternative;
2923 if (lra_dump_file != NULL && reused_alternative_num >= 0)
2924 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
2925 reused_alternative_num, INSN_UID (curr_insn));
2927 if (process_alt_operands (reused_alternative_num))
2928 alt_p = true;
2930 /* If insn is commutative (it's safe to exchange a certain pair of
2931 operands) then we need to try each alternative twice, the second
2932 time matching those two operands as if we had exchanged them. To
2933 do this, really exchange them in operands.
2935 If we have just tried the alternatives the second time, return
2936 operands to normal and drop through. */
2938 if (reused_alternative_num < 0 && commutative >= 0)
2940 curr_swapped = !curr_swapped;
2941 if (curr_swapped)
2943 swap_operands (commutative);
2944 goto try_swapped;
2946 else
2947 swap_operands (commutative);
2950 if (! alt_p && ! sec_mem_p)
2952 /* No alternative works with reloads?? */
2953 if (INSN_CODE (curr_insn) >= 0)
2954 fatal_insn ("unable to generate reloads for:", curr_insn);
2955 error_for_asm (curr_insn,
2956 "inconsistent operand constraints in an %<asm%>");
2957 /* Avoid further trouble with this insn. */
2958 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
2959 lra_invalidate_insn_data (curr_insn);
2960 return true;
2963 /* If the best alternative is with operands 1 and 2 swapped, swap
2964 them. Update the operand numbers of any reloads already
2965 pushed. */
2967 if (goal_alt_swapped)
2969 if (lra_dump_file != NULL)
2970 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
2971 INSN_UID (curr_insn));
2973 /* Swap the duplicates too. */
2974 swap_operands (commutative);
2975 change_p = true;
2978 #ifdef SECONDARY_MEMORY_NEEDED
2979 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
2980 too conservatively. So we use the secondary memory only if there
2981 is no any alternative without reloads. */
2982 use_sec_mem_p = false;
2983 if (! alt_p)
2984 use_sec_mem_p = true;
2985 else if (sec_mem_p)
2987 for (i = 0; i < n_operands; i++)
2988 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
2989 break;
2990 use_sec_mem_p = i < n_operands;
2993 if (use_sec_mem_p)
2995 rtx new_reg, src, dest, rld;
2996 enum machine_mode sec_mode, rld_mode;
2998 lra_assert (sec_mem_p);
2999 lra_assert (curr_static_id->operand[0].type == OP_OUT
3000 && curr_static_id->operand[1].type == OP_IN);
3001 dest = *curr_id->operand_loc[0];
3002 src = *curr_id->operand_loc[1];
3003 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3004 ? dest : src);
3005 rld_mode = GET_MODE (rld);
3006 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3007 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3008 #else
3009 sec_mode = rld_mode;
3010 #endif
3011 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3012 NO_REGS, "secondary");
3013 /* If the mode is changed, it should be wider. */
3014 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3015 if (sec_mode != rld_mode)
3017 /* If the target says specifically to use another mode for
3018 secondary memory moves we can not reuse the original
3019 insn. */
3020 after = emit_spill_move (false, new_reg, dest);
3021 lra_process_new_insns (curr_insn, NULL_RTX, after,
3022 "Inserting the sec. move");
3023 /* We may have non null BEFORE here (e.g. after address
3024 processing. */
3025 push_to_sequence (before);
3026 before = emit_spill_move (true, new_reg, src);
3027 emit_insn (before);
3028 before = get_insns ();
3029 end_sequence ();
3030 lra_process_new_insns (curr_insn, before, NULL_RTX, "Changing on");
3031 lra_set_insn_deleted (curr_insn);
3033 else if (dest == rld)
3035 *curr_id->operand_loc[0] = new_reg;
3036 after = emit_spill_move (false, new_reg, dest);
3037 lra_process_new_insns (curr_insn, NULL_RTX, after,
3038 "Inserting the sec. move");
3040 else
3042 *curr_id->operand_loc[1] = new_reg;
3043 /* See comments above. */
3044 push_to_sequence (before);
3045 before = emit_spill_move (true, new_reg, src);
3046 emit_insn (before);
3047 before = get_insns ();
3048 end_sequence ();
3049 lra_process_new_insns (curr_insn, before, NULL_RTX,
3050 "Inserting the sec. move");
3052 lra_update_insn_regno_info (curr_insn);
3053 return true;
3055 #endif
3057 lra_assert (goal_alt_number >= 0);
3058 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3060 if (lra_dump_file != NULL)
3062 const char *p;
3064 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3065 goal_alt_number, INSN_UID (curr_insn));
3066 for (i = 0; i < n_operands; i++)
3068 p = (curr_static_id->operand_alternative
3069 [goal_alt_number * n_operands + i].constraint);
3070 if (*p == '\0')
3071 continue;
3072 fprintf (lra_dump_file, " (%d) ", i);
3073 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3074 fputc (*p, lra_dump_file);
3076 if (INSN_CODE (curr_insn) >= 0
3077 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3078 fprintf (lra_dump_file, " {%s}", p);
3079 fprintf (lra_dump_file, "\n");
3082 /* Right now, for any pair of operands I and J that are required to
3083 match, with J < I, goal_alt_matches[I] is J. Add I to
3084 goal_alt_matched[J]. */
3086 for (i = 0; i < n_operands; i++)
3087 if ((j = goal_alt_matches[i]) >= 0)
3089 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3091 /* We allow matching one output operand and several input
3092 operands. */
3093 lra_assert (k == 0
3094 || (curr_static_id->operand[j].type == OP_OUT
3095 && curr_static_id->operand[i].type == OP_IN
3096 && (curr_static_id->operand
3097 [goal_alt_matched[j][0]].type == OP_IN)));
3098 goal_alt_matched[j][k] = i;
3099 goal_alt_matched[j][k + 1] = -1;
3102 for (i = 0; i < n_operands; i++)
3103 goal_alt_win[i] |= goal_alt_match_win[i];
3105 /* Any constants that aren't allowed and can't be reloaded into
3106 registers are here changed into memory references. */
3107 for (i = 0; i < n_operands; i++)
3108 if (goal_alt_win[i])
3110 int regno;
3111 enum reg_class new_class;
3112 rtx reg = *curr_id->operand_loc[i];
3114 if (GET_CODE (reg) == SUBREG)
3115 reg = SUBREG_REG (reg);
3117 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3119 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3121 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3123 lra_assert (ok_p);
3124 change_class (regno, new_class, " Change", true);
3128 else
3130 const char *constraint;
3131 char c;
3132 rtx op = *curr_id->operand_loc[i];
3133 rtx subreg = NULL_RTX;
3134 enum machine_mode mode = curr_operand_mode[i];
3136 if (GET_CODE (op) == SUBREG)
3138 subreg = op;
3139 op = SUBREG_REG (op);
3140 mode = GET_MODE (op);
3143 if (CONST_POOL_OK_P (mode, op)
3144 && ((targetm.preferred_reload_class
3145 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3146 || no_input_reloads_p))
3148 rtx tem = force_const_mem (mode, op);
3150 change_p = true;
3151 if (subreg != NULL_RTX)
3152 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3154 *curr_id->operand_loc[i] = tem;
3155 lra_update_dup (curr_id, i);
3156 process_address (i, &before, &after);
3158 /* If the alternative accepts constant pool refs directly
3159 there will be no reload needed at all. */
3160 if (subreg != NULL_RTX)
3161 continue;
3162 /* Skip alternatives before the one requested. */
3163 constraint = (curr_static_id->operand_alternative
3164 [goal_alt_number * n_operands + i].constraint);
3165 for (;
3166 (c = *constraint) && c != ',' && c != '#';
3167 constraint += CONSTRAINT_LEN (c, constraint))
3169 if (c == TARGET_MEM_CONSTRAINT || c == 'o')
3170 break;
3171 #ifdef EXTRA_CONSTRAINT_STR
3172 if (EXTRA_MEMORY_CONSTRAINT (c, constraint)
3173 && EXTRA_CONSTRAINT_STR (tem, c, constraint))
3174 break;
3175 #endif
3177 if (c == '\0' || c == ',' || c == '#')
3178 continue;
3180 goal_alt_win[i] = true;
3184 for (i = 0; i < n_operands; i++)
3186 int regno;
3187 bool optional_p = false;
3188 rtx old, new_reg;
3189 rtx op = *curr_id->operand_loc[i];
3191 if (goal_alt_win[i])
3193 if (goal_alt[i] == NO_REGS
3194 && REG_P (op)
3195 /* When we assign NO_REGS it means that we will not
3196 assign a hard register to the scratch pseudo by
3197 assigment pass and the scratch pseudo will be
3198 spilled. Spilled scratch pseudos are transformed
3199 back to scratches at the LRA end. */
3200 && lra_former_scratch_operand_p (curr_insn, i))
3202 int regno = REGNO (op);
3203 change_class (regno, NO_REGS, " Change", true);
3204 if (lra_get_regno_hard_regno (regno) >= 0)
3205 /* We don't have to mark all insn affected by the
3206 spilled pseudo as there is only one such insn, the
3207 current one. */
3208 reg_renumber[regno] = -1;
3210 /* We can do an optional reload. If the pseudo got a hard
3211 reg, we might improve the code through inheritance. If
3212 it does not get a hard register we coalesce memory/memory
3213 moves later. Ignore move insns to avoid cycling. */
3214 if (0 && ! lra_simple_p
3215 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3216 && goal_alt[i] != NO_REGS && REG_P (op)
3217 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3218 && reg_renumber[regno] < 0
3219 && (curr_insn_set == NULL_RTX
3220 || !(REG_P (SET_SRC (curr_insn_set))
3221 || MEM_P (SET_SRC (curr_insn_set))
3222 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)))
3223 optional_p = true;
3224 else
3225 continue;
3228 /* Operands that match previous ones have already been handled. */
3229 if (goal_alt_matches[i] >= 0)
3230 continue;
3232 /* We should not have an operand with a non-offsettable address
3233 appearing where an offsettable address will do. It also may
3234 be a case when the address should be special in other words
3235 not a general one (e.g. it needs no index reg). */
3236 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3238 enum reg_class rclass;
3239 rtx *loc = &XEXP (op, 0);
3240 enum rtx_code code = GET_CODE (*loc);
3242 push_to_sequence (before);
3243 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3244 MEM, SCRATCH);
3245 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3246 new_reg = emit_inc (rclass, *loc, *loc,
3247 /* This value does not matter for MODIFY. */
3248 GET_MODE_SIZE (GET_MODE (op)));
3249 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass,
3250 "offsetable address", &new_reg))
3251 lra_emit_move (new_reg, *loc);
3252 before = get_insns ();
3253 end_sequence ();
3254 *loc = new_reg;
3255 lra_update_dup (curr_id, i);
3257 else if (goal_alt_matched[i][0] == -1)
3259 enum machine_mode mode;
3260 rtx reg, *loc;
3261 int hard_regno, byte;
3262 enum op_type type = curr_static_id->operand[i].type;
3264 loc = curr_id->operand_loc[i];
3265 mode = curr_operand_mode[i];
3266 if (GET_CODE (*loc) == SUBREG)
3268 reg = SUBREG_REG (*loc);
3269 byte = SUBREG_BYTE (*loc);
3270 if (REG_P (reg)
3271 /* Strict_low_part requires reload the register not
3272 the sub-register. */
3273 && (curr_static_id->operand[i].strict_low
3274 || (GET_MODE_SIZE (mode)
3275 <= GET_MODE_SIZE (GET_MODE (reg))
3276 && (hard_regno
3277 = get_try_hard_regno (REGNO (reg))) >= 0
3278 && (simplify_subreg_regno
3279 (hard_regno,
3280 GET_MODE (reg), byte, mode) < 0)
3281 && (goal_alt[i] == NO_REGS
3282 || (simplify_subreg_regno
3283 (ira_class_hard_regs[goal_alt[i]][0],
3284 GET_MODE (reg), byte, mode) >= 0)))))
3286 loc = &SUBREG_REG (*loc);
3287 mode = GET_MODE (*loc);
3290 old = *loc;
3291 if (get_reload_reg (type, mode, old, goal_alt[i], "", &new_reg)
3292 && type != OP_OUT)
3294 push_to_sequence (before);
3295 lra_emit_move (new_reg, old);
3296 before = get_insns ();
3297 end_sequence ();
3299 *loc = new_reg;
3300 if (type != OP_IN
3301 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3303 start_sequence ();
3304 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3305 emit_insn (after);
3306 after = get_insns ();
3307 end_sequence ();
3308 *loc = new_reg;
3310 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3311 if (goal_alt_dont_inherit_ops[j] == i)
3313 lra_set_regno_unique_value (REGNO (new_reg));
3314 break;
3316 lra_update_dup (curr_id, i);
3318 else if (curr_static_id->operand[i].type == OP_IN
3319 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3320 == OP_OUT))
3322 /* generate reloads for input and matched outputs. */
3323 match_inputs[0] = i;
3324 match_inputs[1] = -1;
3325 match_reload (goal_alt_matched[i][0], match_inputs,
3326 goal_alt[i], &before, &after);
3328 else if (curr_static_id->operand[i].type == OP_OUT
3329 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3330 == OP_IN))
3331 /* Generate reloads for output and matched inputs. */
3332 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3333 else if (curr_static_id->operand[i].type == OP_IN
3334 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3335 == OP_IN))
3337 /* Generate reloads for matched inputs. */
3338 match_inputs[0] = i;
3339 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3340 match_inputs[j + 1] = k;
3341 match_inputs[j + 1] = -1;
3342 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3344 else
3345 /* We must generate code in any case when function
3346 process_alt_operands decides that it is possible. */
3347 gcc_unreachable ();
3348 if (optional_p)
3350 lra_assert (REG_P (op));
3351 regno = REGNO (op);
3352 op = *curr_id->operand_loc[i]; /* Substitution. */
3353 if (GET_CODE (op) == SUBREG)
3354 op = SUBREG_REG (op);
3355 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3356 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3357 lra_reg_info[REGNO (op)].restore_regno = regno;
3358 if (lra_dump_file != NULL)
3359 fprintf (lra_dump_file,
3360 " Making reload reg %d for reg %d optional\n",
3361 REGNO (op), regno);
3364 if (before != NULL_RTX || after != NULL_RTX
3365 || max_regno_before != max_reg_num ())
3366 change_p = true;
3367 if (change_p)
3369 lra_update_operator_dups (curr_id);
3370 /* Something changes -- process the insn. */
3371 lra_update_insn_regno_info (curr_insn);
3373 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3374 return change_p;
3377 /* Return true if X is in LIST. */
3378 static bool
3379 in_list_p (rtx x, rtx list)
3381 for (; list != NULL_RTX; list = XEXP (list, 1))
3382 if (XEXP (list, 0) == x)
3383 return true;
3384 return false;
3387 /* Return true if X contains an allocatable hard register (if
3388 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3389 static bool
3390 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3392 int i, j;
3393 const char *fmt;
3394 enum rtx_code code;
3396 code = GET_CODE (x);
3397 if (REG_P (x))
3399 int regno = REGNO (x);
3400 HARD_REG_SET alloc_regs;
3402 if (hard_reg_p)
3404 if (regno >= FIRST_PSEUDO_REGISTER)
3405 regno = lra_get_regno_hard_regno (regno);
3406 if (regno < 0)
3407 return false;
3408 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3409 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3411 else
3413 if (regno < FIRST_PSEUDO_REGISTER)
3414 return false;
3415 if (! spilled_p)
3416 return true;
3417 return lra_get_regno_hard_regno (regno) < 0;
3420 fmt = GET_RTX_FORMAT (code);
3421 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3423 if (fmt[i] == 'e')
3425 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3426 return true;
3428 else if (fmt[i] == 'E')
3430 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3431 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3432 return true;
3435 return false;
3438 /* Process all regs in location *LOC and change them on equivalent
3439 substitution. Return true if any change was done. */
3440 static bool
3441 loc_equivalence_change_p (rtx *loc)
3443 rtx subst, reg, x = *loc;
3444 bool result = false;
3445 enum rtx_code code = GET_CODE (x);
3446 const char *fmt;
3447 int i, j;
3449 if (code == SUBREG)
3451 reg = SUBREG_REG (x);
3452 if ((subst = get_equiv_substitution (reg)) != reg
3453 && GET_MODE (subst) == VOIDmode)
3455 /* We cannot reload debug location. Simplify subreg here
3456 while we know the inner mode. */
3457 *loc = simplify_gen_subreg (GET_MODE (x), subst,
3458 GET_MODE (reg), SUBREG_BYTE (x));
3459 return true;
3462 if (code == REG && (subst = get_equiv_substitution (x)) != x)
3464 *loc = subst;
3465 return true;
3468 /* Scan all the operand sub-expressions. */
3469 fmt = GET_RTX_FORMAT (code);
3470 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3472 if (fmt[i] == 'e')
3473 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
3474 else if (fmt[i] == 'E')
3475 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3476 result
3477 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
3479 return result;
3482 /* Similar to loc_equivalence_change_p, but for use as
3483 simplify_replace_fn_rtx callback. */
3484 static rtx
3485 loc_equivalence_callback (rtx loc, const_rtx, void *)
3487 if (!REG_P (loc))
3488 return NULL_RTX;
3490 rtx subst = get_equiv_substitution (loc);
3491 if (subst != loc)
3492 return subst;
3494 return NULL_RTX;
3497 /* Maximum number of generated reload insns per an insn. It is for
3498 preventing this pass cycling in a bug case. */
3499 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
3501 /* The current iteration number of this LRA pass. */
3502 int lra_constraint_iter;
3504 /* The current iteration number of this LRA pass after the last spill
3505 pass. */
3506 int lra_constraint_iter_after_spill;
3508 /* True if we substituted equiv which needs checking register
3509 allocation correctness because the equivalent value contains
3510 allocatable hard registers or when we restore multi-register
3511 pseudo. */
3512 bool lra_risky_transformations_p;
3514 /* Return true if REGNO is referenced in more than one block. */
3515 static bool
3516 multi_block_pseudo_p (int regno)
3518 basic_block bb = NULL;
3519 unsigned int uid;
3520 bitmap_iterator bi;
3522 if (regno < FIRST_PSEUDO_REGISTER)
3523 return false;
3525 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
3526 if (bb == NULL)
3527 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
3528 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
3529 return true;
3530 return false;
3533 /* Return true if LIST contains a deleted insn. */
3534 static bool
3535 contains_deleted_insn_p (rtx list)
3537 for (; list != NULL_RTX; list = XEXP (list, 1))
3538 if (NOTE_P (XEXP (list, 0))
3539 && NOTE_KIND (XEXP (list, 0)) == NOTE_INSN_DELETED)
3540 return true;
3541 return false;
3544 /* Return true if X contains a pseudo dying in INSN. */
3545 static bool
3546 dead_pseudo_p (rtx x, rtx insn)
3548 int i, j;
3549 const char *fmt;
3550 enum rtx_code code;
3552 if (REG_P (x))
3553 return (insn != NULL_RTX
3554 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
3555 code = GET_CODE (x);
3556 fmt = GET_RTX_FORMAT (code);
3557 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3559 if (fmt[i] == 'e')
3561 if (dead_pseudo_p (XEXP (x, i), insn))
3562 return true;
3564 else if (fmt[i] == 'E')
3566 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3567 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
3568 return true;
3571 return false;
3574 /* Return true if INSN contains a dying pseudo in INSN right hand
3575 side. */
3576 static bool
3577 insn_rhs_dead_pseudo_p (rtx insn)
3579 rtx set = single_set (insn);
3581 gcc_assert (set != NULL);
3582 return dead_pseudo_p (SET_SRC (set), insn);
3585 /* Return true if any init insn of REGNO contains a dying pseudo in
3586 insn right hand side. */
3587 static bool
3588 init_insn_rhs_dead_pseudo_p (int regno)
3590 rtx insns = ira_reg_equiv[regno].init_insns;
3592 if (insns == NULL)
3593 return false;
3594 if (INSN_P (insns))
3595 return insn_rhs_dead_pseudo_p (insns);
3596 for (; insns != NULL_RTX; insns = XEXP (insns, 1))
3597 if (insn_rhs_dead_pseudo_p (XEXP (insns, 0)))
3598 return true;
3599 return false;
3602 /* Entry function of LRA constraint pass. Return true if the
3603 constraint pass did change the code. */
3604 bool
3605 lra_constraints (bool first_p)
3607 bool changed_p;
3608 int i, hard_regno, new_insns_num;
3609 unsigned int min_len, new_min_len, uid;
3610 rtx set, x, reg, dest_reg;
3611 basic_block last_bb;
3612 bitmap_head equiv_insn_bitmap;
3613 bitmap_iterator bi;
3615 lra_constraint_iter++;
3616 if (lra_dump_file != NULL)
3617 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
3618 lra_constraint_iter);
3619 lra_constraint_iter_after_spill++;
3620 if (lra_constraint_iter_after_spill > LRA_MAX_CONSTRAINT_ITERATION_NUMBER)
3621 internal_error
3622 ("Maximum number of LRA constraint passes is achieved (%d)\n",
3623 LRA_MAX_CONSTRAINT_ITERATION_NUMBER);
3624 changed_p = false;
3625 lra_risky_transformations_p = false;
3626 new_insn_uid_start = get_max_uid ();
3627 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
3628 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
3629 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3630 if (lra_reg_info[i].nrefs != 0)
3632 ira_reg_equiv[i].profitable_p = true;
3633 reg = regno_reg_rtx[i];
3634 if ((hard_regno = lra_get_regno_hard_regno (i)) >= 0)
3636 int j, nregs;
3638 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
3639 for (j = 0; j < nregs; j++)
3640 df_set_regs_ever_live (hard_regno + j, true);
3642 else if ((x = get_equiv_substitution (reg)) != reg)
3644 bool pseudo_p = contains_reg_p (x, false, false);
3645 rtx set, insns;
3647 /* After RTL transformation, we can not guarantee that
3648 pseudo in the substitution was not reloaded which might
3649 make equivalence invalid. For example, in reverse
3650 equiv of p0
3652 p0 <- ...
3654 equiv_mem <- p0
3656 the memory address register was reloaded before the 2nd
3657 insn. */
3658 if ((! first_p && pseudo_p)
3659 /* We don't use DF for compilation speed sake. So it
3660 is problematic to update live info when we use an
3661 equivalence containing pseudos in more than one
3662 BB. */
3663 || (pseudo_p && multi_block_pseudo_p (i))
3664 /* If an init insn was deleted for some reason, cancel
3665 the equiv. We could update the equiv insns after
3666 transformations including an equiv insn deletion
3667 but it is not worthy as such cases are extremely
3668 rare. */
3669 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
3670 /* If it is not a reverse equivalence, we check that a
3671 pseudo in rhs of the init insn is not dying in the
3672 insn. Otherwise, the live info at the beginning of
3673 the corresponding BB might be wrong after we
3674 removed the insn. When the equiv can be a
3675 constant, the right hand side of the init insn can
3676 be a pseudo. */
3677 || (! ((insns = ira_reg_equiv[i].init_insns) != NULL_RTX
3678 && INSN_P (XEXP (insns, 0))
3679 && XEXP (insns, 1) == NULL_RTX
3680 && (set = single_set (XEXP (insns, 0))) != NULL_RTX
3681 && REG_P (SET_SRC (set))
3682 && (int) REGNO (SET_SRC (set)) == i)
3683 && init_insn_rhs_dead_pseudo_p (i))
3684 /* Prevent access beyond equivalent memory for
3685 paradoxical subregs. */
3686 || (MEM_P (x)
3687 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
3688 > GET_MODE_SIZE (GET_MODE (x)))))
3689 ira_reg_equiv[i].defined_p = false;
3690 if (contains_reg_p (x, false, true))
3691 ira_reg_equiv[i].profitable_p = false;
3692 if (get_equiv_substitution (reg) != reg)
3693 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
3696 /* We should add all insns containing pseudos which should be
3697 substituted by their equivalences. */
3698 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
3699 lra_push_insn_by_uid (uid);
3700 lra_eliminate (false);
3701 min_len = lra_insn_stack_length ();
3702 new_insns_num = 0;
3703 last_bb = NULL;
3704 changed_p = false;
3705 while ((new_min_len = lra_insn_stack_length ()) != 0)
3707 curr_insn = lra_pop_insn ();
3708 --new_min_len;
3709 curr_bb = BLOCK_FOR_INSN (curr_insn);
3710 if (curr_bb != last_bb)
3712 last_bb = curr_bb;
3713 bb_reload_num = lra_curr_reload_num;
3715 if (min_len > new_min_len)
3717 min_len = new_min_len;
3718 new_insns_num = 0;
3720 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
3721 internal_error
3722 ("Max. number of generated reload insns per insn is achieved (%d)\n",
3723 MAX_RELOAD_INSNS_NUMBER);
3724 new_insns_num++;
3725 if (DEBUG_INSN_P (curr_insn))
3727 /* We need to check equivalence in debug insn and change
3728 pseudo to the equivalent value if necessary. */
3729 curr_id = lra_get_insn_recog_data (curr_insn);
3730 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
3732 rtx old = *curr_id->operand_loc[0];
3733 *curr_id->operand_loc[0]
3734 = simplify_replace_fn_rtx (old, NULL_RTX,
3735 loc_equivalence_callback, NULL);
3736 if (old != *curr_id->operand_loc[0])
3738 lra_update_insn_regno_info (curr_insn);
3739 changed_p = true;
3743 else if (INSN_P (curr_insn))
3745 if ((set = single_set (curr_insn)) != NULL_RTX)
3747 dest_reg = SET_DEST (set);
3748 /* The equivalence pseudo could be set up as SUBREG in a
3749 case when it is a call restore insn in a mode
3750 different from the pseudo mode. */
3751 if (GET_CODE (dest_reg) == SUBREG)
3752 dest_reg = SUBREG_REG (dest_reg);
3753 if ((REG_P (dest_reg)
3754 && (x = get_equiv_substitution (dest_reg)) != dest_reg
3755 /* Check that this is actually an insn setting up
3756 the equivalence. */
3757 && (in_list_p (curr_insn,
3758 ira_reg_equiv
3759 [REGNO (dest_reg)].init_insns)
3760 /* Init insns may contain not all insns setting
3761 up equivalence as we have live range
3762 splitting. So here we use another condition
3763 to check insn setting up the equivalence
3764 which should be removed, e.g. in case when
3765 the equivalence is a constant. */
3766 || ! MEM_P (x))
3767 /* Remove insns which set up a pseudo whose value
3768 can not be changed. Such insns might be not in
3769 init_insns because we don't update equiv data
3770 during insn transformations.
3772 As an example, let suppose that a pseudo got
3773 hard register and on the 1st pass was not
3774 changed to equivalent constant. We generate an
3775 additional insn setting up the pseudo because of
3776 secondary memory movement. Then the pseudo is
3777 spilled and we use the equiv constant. In this
3778 case we should remove the additional insn and
3779 this insn is not init_insns list. */
3780 && (! MEM_P (x) || MEM_READONLY_P (x)
3781 || in_list_p (curr_insn,
3782 ira_reg_equiv
3783 [REGNO (dest_reg)].init_insns)))
3784 || (((x = get_equiv_substitution (SET_SRC (set)))
3785 != SET_SRC (set))
3786 && in_list_p (curr_insn,
3787 ira_reg_equiv
3788 [REGNO (SET_SRC (set))].init_insns)))
3790 /* This is equiv init insn of pseudo which did not get a
3791 hard register -- remove the insn. */
3792 if (lra_dump_file != NULL)
3794 fprintf (lra_dump_file,
3795 " Removing equiv init insn %i (freq=%d)\n",
3796 INSN_UID (curr_insn),
3797 BLOCK_FOR_INSN (curr_insn)->frequency);
3798 dump_insn_slim (lra_dump_file, curr_insn);
3800 if (contains_reg_p (x, true, false))
3801 lra_risky_transformations_p = true;
3802 lra_set_insn_deleted (curr_insn);
3803 continue;
3806 curr_id = lra_get_insn_recog_data (curr_insn);
3807 curr_static_id = curr_id->insn_static_data;
3808 init_curr_insn_input_reloads ();
3809 init_curr_operand_mode ();
3810 if (curr_insn_transform ())
3811 changed_p = true;
3812 /* Check non-transformed insns too for equiv change as USE
3813 or CLOBBER don't need reloads but can contain pseudos
3814 being changed on their equivalences. */
3815 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
3816 && loc_equivalence_change_p (&PATTERN (curr_insn)))
3818 lra_update_insn_regno_info (curr_insn);
3819 changed_p = true;
3823 bitmap_clear (&equiv_insn_bitmap);
3824 /* If we used a new hard regno, changed_p should be true because the
3825 hard reg is assigned to a new pseudo. */
3826 #ifdef ENABLE_CHECKING
3827 if (! changed_p)
3829 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3830 if (lra_reg_info[i].nrefs != 0
3831 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
3833 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
3835 for (j = 0; j < nregs; j++)
3836 lra_assert (df_regs_ever_live_p (hard_regno + j));
3839 #endif
3840 return changed_p;
3843 /* Initiate the LRA constraint pass. It is done once per
3844 function. */
3845 void
3846 lra_constraints_init (void)
3850 /* Finalize the LRA constraint pass. It is done once per
3851 function. */
3852 void
3853 lra_constraints_finish (void)
3859 /* This page contains code to do inheritance/split
3860 transformations. */
3862 /* Number of reloads passed so far in current EBB. */
3863 static int reloads_num;
3865 /* Number of calls passed so far in current EBB. */
3866 static int calls_num;
3868 /* Current reload pseudo check for validity of elements in
3869 USAGE_INSNS. */
3870 static int curr_usage_insns_check;
3872 /* Info about last usage of registers in EBB to do inheritance/split
3873 transformation. Inheritance transformation is done from a spilled
3874 pseudo and split transformations from a hard register or a pseudo
3875 assigned to a hard register. */
3876 struct usage_insns
3878 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
3879 value INSNS is valid. The insns is chain of optional debug insns
3880 and a finishing non-debug insn using the corresponding reg. The
3881 value is also used to mark the registers which are set up in the
3882 current insn. The negated insn uid is used for this. */
3883 int check;
3884 /* Value of global reloads_num at the last insn in INSNS. */
3885 int reloads_num;
3886 /* Value of global reloads_nums at the last insn in INSNS. */
3887 int calls_num;
3888 /* It can be true only for splitting. And it means that the restore
3889 insn should be put after insn given by the following member. */
3890 bool after_p;
3891 /* Next insns in the current EBB which use the original reg and the
3892 original reg value is not changed between the current insn and
3893 the next insns. In order words, e.g. for inheritance, if we need
3894 to use the original reg value again in the next insns we can try
3895 to use the value in a hard register from a reload insn of the
3896 current insn. */
3897 rtx insns;
3900 /* Map: regno -> corresponding pseudo usage insns. */
3901 static struct usage_insns *usage_insns;
3903 static void
3904 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
3906 usage_insns[regno].check = curr_usage_insns_check;
3907 usage_insns[regno].insns = insn;
3908 usage_insns[regno].reloads_num = reloads_num;
3909 usage_insns[regno].calls_num = calls_num;
3910 usage_insns[regno].after_p = after_p;
3913 /* The function is used to form list REGNO usages which consists of
3914 optional debug insns finished by a non-debug insn using REGNO.
3915 RELOADS_NUM is current number of reload insns processed so far. */
3916 static void
3917 add_next_usage_insn (int regno, rtx insn, int reloads_num)
3919 rtx next_usage_insns;
3921 if (usage_insns[regno].check == curr_usage_insns_check
3922 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
3923 && DEBUG_INSN_P (insn))
3925 /* Check that we did not add the debug insn yet. */
3926 if (next_usage_insns != insn
3927 && (GET_CODE (next_usage_insns) != INSN_LIST
3928 || XEXP (next_usage_insns, 0) != insn))
3929 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
3930 next_usage_insns);
3932 else if (NONDEBUG_INSN_P (insn))
3933 setup_next_usage_insn (regno, insn, reloads_num, false);
3934 else
3935 usage_insns[regno].check = 0;
3938 /* Replace all references to register OLD_REGNO in *LOC with pseudo
3939 register NEW_REG. Return true if any change was made. */
3940 static bool
3941 substitute_pseudo (rtx *loc, int old_regno, rtx new_reg)
3943 rtx x = *loc;
3944 bool result = false;
3945 enum rtx_code code;
3946 const char *fmt;
3947 int i, j;
3949 if (x == NULL_RTX)
3950 return false;
3952 code = GET_CODE (x);
3953 if (code == REG && (int) REGNO (x) == old_regno)
3955 enum machine_mode mode = GET_MODE (*loc);
3956 enum machine_mode inner_mode = GET_MODE (new_reg);
3958 if (mode != inner_mode)
3960 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
3961 || ! SCALAR_INT_MODE_P (inner_mode))
3962 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
3963 else
3964 new_reg = gen_lowpart_SUBREG (mode, new_reg);
3966 *loc = new_reg;
3967 return true;
3970 /* Scan all the operand sub-expressions. */
3971 fmt = GET_RTX_FORMAT (code);
3972 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3974 if (fmt[i] == 'e')
3976 if (substitute_pseudo (&XEXP (x, i), old_regno, new_reg))
3977 result = true;
3979 else if (fmt[i] == 'E')
3981 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3982 if (substitute_pseudo (&XVECEXP (x, i, j), old_regno, new_reg))
3983 result = true;
3986 return result;
3989 /* Return first non-debug insn in list USAGE_INSNS. */
3990 static rtx
3991 skip_usage_debug_insns (rtx usage_insns)
3993 rtx insn;
3995 /* Skip debug insns. */
3996 for (insn = usage_insns;
3997 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
3998 insn = XEXP (insn, 1))
4000 return insn;
4003 /* Return true if we need secondary memory moves for insn in
4004 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4005 into the insn. */
4006 static bool
4007 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4008 rtx usage_insns ATTRIBUTE_UNUSED)
4010 #ifndef SECONDARY_MEMORY_NEEDED
4011 return false;
4012 #else
4013 rtx insn, set, dest;
4014 enum reg_class cl;
4016 if (inher_cl == ALL_REGS
4017 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4018 return false;
4019 lra_assert (INSN_P (insn));
4020 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4021 return false;
4022 dest = SET_DEST (set);
4023 if (! REG_P (dest))
4024 return false;
4025 lra_assert (inher_cl != NO_REGS);
4026 cl = get_reg_class (REGNO (dest));
4027 return (cl != NO_REGS && cl != ALL_REGS
4028 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4029 #endif
4032 /* Registers involved in inheritance/split in the current EBB
4033 (inheritance/split pseudos and original registers). */
4034 static bitmap_head check_only_regs;
4036 /* Do inheritance transformations for insn INSN, which defines (if
4037 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4038 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4039 form as the "insns" field of usage_insns. Return true if we
4040 succeed in such transformation.
4042 The transformations look like:
4044 p <- ... i <- ...
4045 ... p <- i (new insn)
4046 ... =>
4047 <- ... p ... <- ... i ...
4049 ... i <- p (new insn)
4050 <- ... p ... <- ... i ...
4051 ... =>
4052 <- ... p ... <- ... i ...
4053 where p is a spilled original pseudo and i is a new inheritance pseudo.
4056 The inheritance pseudo has the smallest class of two classes CL and
4057 class of ORIGINAL REGNO. */
4058 static bool
4059 inherit_reload_reg (bool def_p, int original_regno,
4060 enum reg_class cl, rtx insn, rtx next_usage_insns)
4062 enum reg_class rclass = lra_get_allocno_class (original_regno);
4063 rtx original_reg = regno_reg_rtx[original_regno];
4064 rtx new_reg, new_insns, usage_insn;
4066 lra_assert (! usage_insns[original_regno].after_p);
4067 if (lra_dump_file != NULL)
4068 fprintf (lra_dump_file,
4069 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4070 if (! ira_reg_classes_intersect_p[cl][rclass])
4072 if (lra_dump_file != NULL)
4074 fprintf (lra_dump_file,
4075 " Rejecting inheritance for %d "
4076 "because of disjoint classes %s and %s\n",
4077 original_regno, reg_class_names[cl],
4078 reg_class_names[rclass]);
4079 fprintf (lra_dump_file,
4080 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4082 return false;
4084 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4085 /* We don't use a subset of two classes because it can be
4086 NO_REGS. This transformation is still profitable in most
4087 cases even if the classes are not intersected as register
4088 move is probably cheaper than a memory load. */
4089 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4091 if (lra_dump_file != NULL)
4092 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4093 reg_class_names[cl], reg_class_names[rclass]);
4095 rclass = cl;
4097 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4099 /* Reject inheritance resulting in secondary memory moves.
4100 Otherwise, there is a danger in LRA cycling. Also such
4101 transformation will be unprofitable. */
4102 if (lra_dump_file != NULL)
4104 rtx insn = skip_usage_debug_insns (next_usage_insns);
4105 rtx set = single_set (insn);
4107 lra_assert (set != NULL_RTX);
4109 rtx dest = SET_DEST (set);
4111 lra_assert (REG_P (dest));
4112 fprintf (lra_dump_file,
4113 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4114 "as secondary mem is needed\n",
4115 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4116 original_regno, reg_class_names[rclass]);
4117 fprintf (lra_dump_file,
4118 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4120 return false;
4122 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4123 rclass, "inheritance");
4124 start_sequence ();
4125 if (def_p)
4126 emit_move_insn (original_reg, new_reg);
4127 else
4128 emit_move_insn (new_reg, original_reg);
4129 new_insns = get_insns ();
4130 end_sequence ();
4131 if (NEXT_INSN (new_insns) != NULL_RTX)
4133 if (lra_dump_file != NULL)
4135 fprintf (lra_dump_file,
4136 " Rejecting inheritance %d->%d "
4137 "as it results in 2 or more insns:\n",
4138 original_regno, REGNO (new_reg));
4139 dump_rtl_slim (lra_dump_file, new_insns, NULL_RTX, -1, 0);
4140 fprintf (lra_dump_file,
4141 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4143 return false;
4145 substitute_pseudo (&insn, original_regno, new_reg);
4146 lra_update_insn_regno_info (insn);
4147 if (! def_p)
4148 /* We now have a new usage insn for original regno. */
4149 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4150 if (lra_dump_file != NULL)
4151 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4152 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4153 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4154 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4155 bitmap_set_bit (&check_only_regs, original_regno);
4156 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4157 if (def_p)
4158 lra_process_new_insns (insn, NULL_RTX, new_insns,
4159 "Add original<-inheritance");
4160 else
4161 lra_process_new_insns (insn, new_insns, NULL_RTX,
4162 "Add inheritance<-original");
4163 while (next_usage_insns != NULL_RTX)
4165 if (GET_CODE (next_usage_insns) != INSN_LIST)
4167 usage_insn = next_usage_insns;
4168 lra_assert (NONDEBUG_INSN_P (usage_insn));
4169 next_usage_insns = NULL;
4171 else
4173 usage_insn = XEXP (next_usage_insns, 0);
4174 lra_assert (DEBUG_INSN_P (usage_insn));
4175 next_usage_insns = XEXP (next_usage_insns, 1);
4177 substitute_pseudo (&usage_insn, original_regno, new_reg);
4178 lra_update_insn_regno_info (usage_insn);
4179 if (lra_dump_file != NULL)
4181 fprintf (lra_dump_file,
4182 " Inheritance reuse change %d->%d (bb%d):\n",
4183 original_regno, REGNO (new_reg),
4184 BLOCK_FOR_INSN (usage_insn)->index);
4185 dump_insn_slim (lra_dump_file, usage_insn);
4188 if (lra_dump_file != NULL)
4189 fprintf (lra_dump_file,
4190 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4191 return true;
4194 /* Return true if we need a caller save/restore for pseudo REGNO which
4195 was assigned to a hard register. */
4196 static inline bool
4197 need_for_call_save_p (int regno)
4199 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4200 return (usage_insns[regno].calls_num < calls_num
4201 && (overlaps_hard_reg_set_p
4202 (call_used_reg_set,
4203 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])));
4206 /* Global registers occuring in the current EBB. */
4207 static bitmap_head ebb_global_regs;
4209 /* Return true if we need a split for hard register REGNO or pseudo
4210 REGNO which was assigned to a hard register.
4211 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4212 used for reloads since the EBB end. It is an approximation of the
4213 used hard registers in the split range. The exact value would
4214 require expensive calculations. If we were aggressive with
4215 splitting because of the approximation, the split pseudo will save
4216 the same hard register assignment and will be removed in the undo
4217 pass. We still need the approximation because too aggressive
4218 splitting would result in too inaccurate cost calculation in the
4219 assignment pass because of too many generated moves which will be
4220 probably removed in the undo pass. */
4221 static inline bool
4222 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4224 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4226 lra_assert (hard_regno >= 0);
4227 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4228 /* Don't split eliminable hard registers, otherwise we can
4229 split hard registers like hard frame pointer, which
4230 lives on BB start/end according to DF-infrastructure,
4231 when there is a pseudo assigned to the register and
4232 living in the same BB. */
4233 && (regno >= FIRST_PSEUDO_REGISTER
4234 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4235 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4236 /* Don't split call clobbered hard regs living through
4237 calls, otherwise we might have a check problem in the
4238 assign sub-pass as in the most cases (exception is a
4239 situation when lra_risky_transformations_p value is
4240 true) the assign pass assumes that all pseudos living
4241 through calls are assigned to call saved hard regs. */
4242 && (regno >= FIRST_PSEUDO_REGISTER
4243 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4244 || usage_insns[regno].calls_num == calls_num)
4245 /* We need at least 2 reloads to make pseudo splitting
4246 profitable. We should provide hard regno splitting in
4247 any case to solve 1st insn scheduling problem when
4248 moving hard register definition up might result in
4249 impossibility to find hard register for reload pseudo of
4250 small register class. */
4251 && (usage_insns[regno].reloads_num
4252 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 2) < reloads_num)
4253 && (regno < FIRST_PSEUDO_REGISTER
4254 /* For short living pseudos, spilling + inheritance can
4255 be considered a substitution for splitting.
4256 Therefore we do not splitting for local pseudos. It
4257 decreases also aggressiveness of splitting. The
4258 minimal number of references is chosen taking into
4259 account that for 2 references splitting has no sense
4260 as we can just spill the pseudo. */
4261 || (regno >= FIRST_PSEUDO_REGISTER
4262 && lra_reg_info[regno].nrefs > 3
4263 && bitmap_bit_p (&ebb_global_regs, regno))))
4264 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4267 /* Return class for the split pseudo created from original pseudo with
4268 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4269 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4270 results in no secondary memory movements. */
4271 static enum reg_class
4272 choose_split_class (enum reg_class allocno_class,
4273 int hard_regno ATTRIBUTE_UNUSED,
4274 enum machine_mode mode ATTRIBUTE_UNUSED)
4276 #ifndef SECONDARY_MEMORY_NEEDED
4277 return allocno_class;
4278 #else
4279 int i;
4280 enum reg_class cl, best_cl = NO_REGS;
4281 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4282 = REGNO_REG_CLASS (hard_regno);
4284 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4285 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4286 return allocno_class;
4287 for (i = 0;
4288 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4289 i++)
4290 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4291 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4292 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4293 && (best_cl == NO_REGS
4294 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4295 best_cl = cl;
4296 return best_cl;
4297 #endif
4300 /* Do split transformations for insn INSN, which defines or uses
4301 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4302 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4303 "insns" field of usage_insns.
4305 The transformations look like:
4307 p <- ... p <- ...
4308 ... s <- p (new insn -- save)
4309 ... =>
4310 ... p <- s (new insn -- restore)
4311 <- ... p ... <- ... p ...
4313 <- ... p ... <- ... p ...
4314 ... s <- p (new insn -- save)
4315 ... =>
4316 ... p <- s (new insn -- restore)
4317 <- ... p ... <- ... p ...
4319 where p is an original pseudo got a hard register or a hard
4320 register and s is a new split pseudo. The save is put before INSN
4321 if BEFORE_P is true. Return true if we succeed in such
4322 transformation. */
4323 static bool
4324 split_reg (bool before_p, int original_regno, rtx insn, rtx next_usage_insns)
4326 enum reg_class rclass;
4327 rtx original_reg;
4328 int hard_regno, nregs;
4329 rtx new_reg, save, restore, usage_insn;
4330 bool after_p;
4331 bool call_save_p;
4333 if (original_regno < FIRST_PSEUDO_REGISTER)
4335 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4336 hard_regno = original_regno;
4337 call_save_p = false;
4338 nregs = 1;
4340 else
4342 hard_regno = reg_renumber[original_regno];
4343 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4344 rclass = lra_get_allocno_class (original_regno);
4345 original_reg = regno_reg_rtx[original_regno];
4346 call_save_p = need_for_call_save_p (original_regno);
4348 original_reg = regno_reg_rtx[original_regno];
4349 lra_assert (hard_regno >= 0);
4350 if (lra_dump_file != NULL)
4351 fprintf (lra_dump_file,
4352 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4353 if (call_save_p)
4355 enum machine_mode sec_mode;
4357 #ifdef SECONDARY_MEMORY_NEEDED_MODE
4358 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (original_reg));
4359 #else
4360 sec_mode = GET_MODE (original_reg);
4361 #endif
4362 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
4363 NO_REGS, "save");
4365 else
4367 rclass = choose_split_class (rclass, hard_regno,
4368 GET_MODE (original_reg));
4369 if (rclass == NO_REGS)
4371 if (lra_dump_file != NULL)
4373 fprintf (lra_dump_file,
4374 " Rejecting split of %d(%s): "
4375 "no good reg class for %d(%s)\n",
4376 original_regno,
4377 reg_class_names[lra_get_allocno_class (original_regno)],
4378 hard_regno,
4379 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4380 fprintf
4381 (lra_dump_file,
4382 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4384 return false;
4386 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4387 rclass, "split");
4388 reg_renumber[REGNO (new_reg)] = hard_regno;
4390 save = emit_spill_move (true, new_reg, original_reg);
4391 if (NEXT_INSN (save) != NULL_RTX)
4393 lra_assert (! call_save_p);
4394 if (lra_dump_file != NULL)
4396 fprintf
4397 (lra_dump_file,
4398 " Rejecting split %d->%d resulting in > 2 %s save insns:\n",
4399 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4400 dump_rtl_slim (lra_dump_file, save, NULL_RTX, -1, 0);
4401 fprintf (lra_dump_file,
4402 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4404 return false;
4406 restore = emit_spill_move (false, new_reg, original_reg);
4407 if (NEXT_INSN (restore) != NULL_RTX)
4409 lra_assert (! call_save_p);
4410 if (lra_dump_file != NULL)
4412 fprintf (lra_dump_file,
4413 " Rejecting split %d->%d "
4414 "resulting in > 2 %s restore insns:\n",
4415 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4416 dump_rtl_slim (lra_dump_file, restore, NULL_RTX, -1, 0);
4417 fprintf (lra_dump_file,
4418 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4420 return false;
4422 after_p = usage_insns[original_regno].after_p;
4423 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4424 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4425 bitmap_set_bit (&check_only_regs, original_regno);
4426 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4427 for (;;)
4429 if (GET_CODE (next_usage_insns) != INSN_LIST)
4431 usage_insn = next_usage_insns;
4432 break;
4434 usage_insn = XEXP (next_usage_insns, 0);
4435 lra_assert (DEBUG_INSN_P (usage_insn));
4436 next_usage_insns = XEXP (next_usage_insns, 1);
4437 substitute_pseudo (&usage_insn, original_regno, new_reg);
4438 lra_update_insn_regno_info (usage_insn);
4439 if (lra_dump_file != NULL)
4441 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
4442 original_regno, REGNO (new_reg));
4443 dump_insn_slim (lra_dump_file, usage_insn);
4446 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
4447 lra_assert (usage_insn != insn || (after_p && before_p));
4448 lra_process_new_insns (usage_insn, after_p ? NULL_RTX : restore,
4449 after_p ? restore : NULL_RTX,
4450 call_save_p
4451 ? "Add reg<-save" : "Add reg<-split");
4452 lra_process_new_insns (insn, before_p ? save : NULL_RTX,
4453 before_p ? NULL_RTX : save,
4454 call_save_p
4455 ? "Add save<-reg" : "Add split<-reg");
4456 if (nregs > 1)
4457 /* If we are trying to split multi-register. We should check
4458 conflicts on the next assignment sub-pass. IRA can allocate on
4459 sub-register levels, LRA do this on pseudos level right now and
4460 this discrepancy may create allocation conflicts after
4461 splitting. */
4462 lra_risky_transformations_p = true;
4463 if (lra_dump_file != NULL)
4464 fprintf (lra_dump_file,
4465 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4466 return true;
4469 /* Recognize that we need a split transformation for insn INSN, which
4470 defines or uses REGNO in its insn biggest MODE (we use it only if
4471 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
4472 hard registers which might be used for reloads since the EBB end.
4473 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
4474 uid before starting INSN processing. Return true if we succeed in
4475 such transformation. */
4476 static bool
4477 split_if_necessary (int regno, enum machine_mode mode,
4478 HARD_REG_SET potential_reload_hard_regs,
4479 bool before_p, rtx insn, int max_uid)
4481 bool res = false;
4482 int i, nregs = 1;
4483 rtx next_usage_insns;
4485 if (regno < FIRST_PSEUDO_REGISTER)
4486 nregs = hard_regno_nregs[regno][mode];
4487 for (i = 0; i < nregs; i++)
4488 if (usage_insns[regno + i].check == curr_usage_insns_check
4489 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
4490 /* To avoid processing the register twice or more. */
4491 && ((GET_CODE (next_usage_insns) != INSN_LIST
4492 && INSN_UID (next_usage_insns) < max_uid)
4493 || (GET_CODE (next_usage_insns) == INSN_LIST
4494 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
4495 && need_for_split_p (potential_reload_hard_regs, regno + i)
4496 && split_reg (before_p, regno + i, insn, next_usage_insns))
4497 res = true;
4498 return res;
4501 /* Check only registers living at the current program point in the
4502 current EBB. */
4503 static bitmap_head live_regs;
4505 /* Update live info in EBB given by its HEAD and TAIL insns after
4506 inheritance/split transformation. The function removes dead moves
4507 too. */
4508 static void
4509 update_ebb_live_info (rtx head, rtx tail)
4511 unsigned int j;
4512 int regno;
4513 bool live_p;
4514 rtx prev_insn, set;
4515 bool remove_p;
4516 basic_block last_bb, prev_bb, curr_bb;
4517 bitmap_iterator bi;
4518 struct lra_insn_reg *reg;
4519 edge e;
4520 edge_iterator ei;
4522 last_bb = BLOCK_FOR_INSN (tail);
4523 prev_bb = NULL;
4524 for (curr_insn = tail;
4525 curr_insn != PREV_INSN (head);
4526 curr_insn = prev_insn)
4528 prev_insn = PREV_INSN (curr_insn);
4529 /* We need to process empty blocks too. They contain
4530 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
4531 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
4532 continue;
4533 curr_bb = BLOCK_FOR_INSN (curr_insn);
4534 if (curr_bb != prev_bb)
4536 if (prev_bb != NULL)
4538 /* Update df_get_live_in (prev_bb): */
4539 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4540 if (bitmap_bit_p (&live_regs, j))
4541 bitmap_set_bit (df_get_live_in (prev_bb), j);
4542 else
4543 bitmap_clear_bit (df_get_live_in (prev_bb), j);
4545 if (curr_bb != last_bb)
4547 /* Update df_get_live_out (curr_bb): */
4548 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4550 live_p = bitmap_bit_p (&live_regs, j);
4551 if (! live_p)
4552 FOR_EACH_EDGE (e, ei, curr_bb->succs)
4553 if (bitmap_bit_p (df_get_live_in (e->dest), j))
4555 live_p = true;
4556 break;
4558 if (live_p)
4559 bitmap_set_bit (df_get_live_out (curr_bb), j);
4560 else
4561 bitmap_clear_bit (df_get_live_out (curr_bb), j);
4564 prev_bb = curr_bb;
4565 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
4567 if (! NONDEBUG_INSN_P (curr_insn))
4568 continue;
4569 curr_id = lra_get_insn_recog_data (curr_insn);
4570 remove_p = false;
4571 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
4572 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
4573 && bitmap_bit_p (&check_only_regs, regno)
4574 && ! bitmap_bit_p (&live_regs, regno))
4575 remove_p = true;
4576 /* See which defined values die here. */
4577 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4578 if (reg->type == OP_OUT && ! reg->subreg_p)
4579 bitmap_clear_bit (&live_regs, reg->regno);
4580 /* Mark each used value as live. */
4581 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4582 if (reg->type != OP_OUT
4583 && bitmap_bit_p (&check_only_regs, reg->regno))
4584 bitmap_set_bit (&live_regs, reg->regno);
4585 /* It is quite important to remove dead move insns because it
4586 means removing dead store. We don't need to process them for
4587 constraints. */
4588 if (remove_p)
4590 if (lra_dump_file != NULL)
4592 fprintf (lra_dump_file, " Removing dead insn:\n ");
4593 dump_insn_slim (lra_dump_file, curr_insn);
4595 lra_set_insn_deleted (curr_insn);
4600 /* The structure describes info to do an inheritance for the current
4601 insn. We need to collect such info first before doing the
4602 transformations because the transformations change the insn
4603 internal representation. */
4604 struct to_inherit
4606 /* Original regno. */
4607 int regno;
4608 /* Subsequent insns which can inherit original reg value. */
4609 rtx insns;
4612 /* Array containing all info for doing inheritance from the current
4613 insn. */
4614 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
4616 /* Number elements in the previous array. */
4617 static int to_inherit_num;
4619 /* Add inheritance info REGNO and INSNS. Their meaning is described in
4620 structure to_inherit. */
4621 static void
4622 add_to_inherit (int regno, rtx insns)
4624 int i;
4626 for (i = 0; i < to_inherit_num; i++)
4627 if (to_inherit[i].regno == regno)
4628 return;
4629 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
4630 to_inherit[to_inherit_num].regno = regno;
4631 to_inherit[to_inherit_num++].insns = insns;
4634 /* Return the last non-debug insn in basic block BB, or the block begin
4635 note if none. */
4636 static rtx
4637 get_last_insertion_point (basic_block bb)
4639 rtx insn;
4641 FOR_BB_INSNS_REVERSE (bb, insn)
4642 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
4643 return insn;
4644 gcc_unreachable ();
4647 /* Set up RES by registers living on edges FROM except the edge (FROM,
4648 TO) or by registers set up in a jump insn in BB FROM. */
4649 static void
4650 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
4652 rtx last;
4653 struct lra_insn_reg *reg;
4654 edge e;
4655 edge_iterator ei;
4657 lra_assert (to != NULL);
4658 bitmap_clear (res);
4659 FOR_EACH_EDGE (e, ei, from->succs)
4660 if (e->dest != to)
4661 bitmap_ior_into (res, df_get_live_in (e->dest));
4662 last = get_last_insertion_point (from);
4663 if (! JUMP_P (last))
4664 return;
4665 curr_id = lra_get_insn_recog_data (last);
4666 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4667 if (reg->type != OP_IN)
4668 bitmap_set_bit (res, reg->regno);
4671 /* Used as a temporary results of some bitmap calculations. */
4672 static bitmap_head temp_bitmap;
4674 /* Do inheritance/split transformations in EBB starting with HEAD and
4675 finishing on TAIL. We process EBB insns in the reverse order.
4676 Return true if we did any inheritance/split transformation in the
4677 EBB.
4679 We should avoid excessive splitting which results in worse code
4680 because of inaccurate cost calculations for spilling new split
4681 pseudos in such case. To achieve this we do splitting only if
4682 register pressure is high in given basic block and there are reload
4683 pseudos requiring hard registers. We could do more register
4684 pressure calculations at any given program point to avoid necessary
4685 splitting even more but it is to expensive and the current approach
4686 works well enough. */
4687 static bool
4688 inherit_in_ebb (rtx head, rtx tail)
4690 int i, src_regno, dst_regno, nregs;
4691 bool change_p, succ_p;
4692 rtx prev_insn, next_usage_insns, set, last_insn;
4693 enum reg_class cl;
4694 struct lra_insn_reg *reg;
4695 basic_block last_processed_bb, curr_bb = NULL;
4696 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
4697 bitmap to_process;
4698 unsigned int j;
4699 bitmap_iterator bi;
4700 bool head_p, after_p;
4702 change_p = false;
4703 curr_usage_insns_check++;
4704 reloads_num = calls_num = 0;
4705 bitmap_clear (&check_only_regs);
4706 last_processed_bb = NULL;
4707 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
4708 CLEAR_HARD_REG_SET (live_hard_regs);
4709 /* We don't process new insns generated in the loop. */
4710 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
4712 prev_insn = PREV_INSN (curr_insn);
4713 if (BLOCK_FOR_INSN (curr_insn) != NULL)
4714 curr_bb = BLOCK_FOR_INSN (curr_insn);
4715 if (last_processed_bb != curr_bb)
4717 /* We are at the end of BB. Add qualified living
4718 pseudos for potential splitting. */
4719 to_process = df_get_live_out (curr_bb);
4720 if (last_processed_bb != NULL)
4722 /* We are somewhere in the middle of EBB. */
4723 get_live_on_other_edges (curr_bb, last_processed_bb,
4724 &temp_bitmap);
4725 to_process = &temp_bitmap;
4727 last_processed_bb = curr_bb;
4728 last_insn = get_last_insertion_point (curr_bb);
4729 after_p = (! JUMP_P (last_insn)
4730 && (! CALL_P (last_insn)
4731 || (find_reg_note (last_insn,
4732 REG_NORETURN, NULL_RTX) == NULL_RTX
4733 && ! SIBLING_CALL_P (last_insn))));
4734 REG_SET_TO_HARD_REG_SET (live_hard_regs, df_get_live_out (curr_bb));
4735 IOR_HARD_REG_SET (live_hard_regs, eliminable_regset);
4736 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
4737 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
4738 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
4740 if ((int) j >= lra_constraint_new_regno_start)
4741 break;
4742 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
4744 if (j < FIRST_PSEUDO_REGISTER)
4745 SET_HARD_REG_BIT (live_hard_regs, j);
4746 else
4747 add_to_hard_reg_set (&live_hard_regs,
4748 PSEUDO_REGNO_MODE (j),
4749 reg_renumber[j]);
4750 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
4754 src_regno = dst_regno = -1;
4755 if (NONDEBUG_INSN_P (curr_insn)
4756 && (set = single_set (curr_insn)) != NULL_RTX
4757 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
4759 src_regno = REGNO (SET_SRC (set));
4760 dst_regno = REGNO (SET_DEST (set));
4762 if (src_regno < lra_constraint_new_regno_start
4763 && src_regno >= FIRST_PSEUDO_REGISTER
4764 && reg_renumber[src_regno] < 0
4765 && dst_regno >= lra_constraint_new_regno_start
4766 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
4768 /* 'reload_pseudo <- original_pseudo'. */
4769 reloads_num++;
4770 succ_p = false;
4771 if (usage_insns[src_regno].check == curr_usage_insns_check
4772 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
4773 succ_p = inherit_reload_reg (false, src_regno, cl,
4774 curr_insn, next_usage_insns);
4775 if (succ_p)
4776 change_p = true;
4777 else
4778 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
4779 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
4780 IOR_HARD_REG_SET (potential_reload_hard_regs,
4781 reg_class_contents[cl]);
4783 else if (src_regno >= lra_constraint_new_regno_start
4784 && dst_regno < lra_constraint_new_regno_start
4785 && dst_regno >= FIRST_PSEUDO_REGISTER
4786 && reg_renumber[dst_regno] < 0
4787 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
4788 && usage_insns[dst_regno].check == curr_usage_insns_check
4789 && (next_usage_insns
4790 = usage_insns[dst_regno].insns) != NULL_RTX)
4792 reloads_num++;
4793 /* 'original_pseudo <- reload_pseudo'. */
4794 if (! JUMP_P (curr_insn)
4795 && inherit_reload_reg (true, dst_regno, cl,
4796 curr_insn, next_usage_insns))
4797 change_p = true;
4798 /* Invalidate. */
4799 usage_insns[dst_regno].check = 0;
4800 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
4801 IOR_HARD_REG_SET (potential_reload_hard_regs,
4802 reg_class_contents[cl]);
4804 else if (INSN_P (curr_insn))
4806 int iter;
4807 int max_uid = get_max_uid ();
4809 curr_id = lra_get_insn_recog_data (curr_insn);
4810 curr_static_id = curr_id->insn_static_data;
4811 to_inherit_num = 0;
4812 /* Process insn definitions. */
4813 for (iter = 0; iter < 2; iter++)
4814 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
4815 reg != NULL;
4816 reg = reg->next)
4817 if (reg->type != OP_IN
4818 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
4820 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
4821 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
4822 && usage_insns[dst_regno].check == curr_usage_insns_check
4823 && (next_usage_insns
4824 = usage_insns[dst_regno].insns) != NULL_RTX)
4826 struct lra_insn_reg *r;
4828 for (r = curr_id->regs; r != NULL; r = r->next)
4829 if (r->type != OP_OUT && r->regno == dst_regno)
4830 break;
4831 /* Don't do inheritance if the pseudo is also
4832 used in the insn. */
4833 if (r == NULL)
4834 /* We can not do inheritance right now
4835 because the current insn reg info (chain
4836 regs) can change after that. */
4837 add_to_inherit (dst_regno, next_usage_insns);
4839 /* We can not process one reg twice here because of
4840 usage_insns invalidation. */
4841 if ((dst_regno < FIRST_PSEUDO_REGISTER
4842 || reg_renumber[dst_regno] >= 0)
4843 && ! reg->subreg_p && reg->type != OP_IN)
4845 HARD_REG_SET s;
4847 if (split_if_necessary (dst_regno, reg->biggest_mode,
4848 potential_reload_hard_regs,
4849 false, curr_insn, max_uid))
4850 change_p = true;
4851 CLEAR_HARD_REG_SET (s);
4852 if (dst_regno < FIRST_PSEUDO_REGISTER)
4853 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
4854 else
4855 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
4856 reg_renumber[dst_regno]);
4857 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
4859 /* We should invalidate potential inheritance or
4860 splitting for the current insn usages to the next
4861 usage insns (see code below) as the output pseudo
4862 prevents this. */
4863 if ((dst_regno >= FIRST_PSEUDO_REGISTER
4864 && reg_renumber[dst_regno] < 0)
4865 || (reg->type == OP_OUT && ! reg->subreg_p
4866 && (dst_regno < FIRST_PSEUDO_REGISTER
4867 || reg_renumber[dst_regno] >= 0)))
4869 /* Invalidate and mark definitions. */
4870 if (dst_regno >= FIRST_PSEUDO_REGISTER)
4871 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
4872 else
4874 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
4875 for (i = 0; i < nregs; i++)
4876 usage_insns[dst_regno + i].check
4877 = -(int) INSN_UID (curr_insn);
4881 if (! JUMP_P (curr_insn))
4882 for (i = 0; i < to_inherit_num; i++)
4883 if (inherit_reload_reg (true, to_inherit[i].regno,
4884 ALL_REGS, curr_insn,
4885 to_inherit[i].insns))
4886 change_p = true;
4887 if (CALL_P (curr_insn))
4889 rtx cheap, pat, dest, restore;
4890 int regno, hard_regno;
4892 calls_num++;
4893 if ((cheap = find_reg_note (curr_insn,
4894 REG_RETURNED, NULL_RTX)) != NULL_RTX
4895 && ((cheap = XEXP (cheap, 0)), true)
4896 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
4897 && (hard_regno = reg_renumber[regno]) >= 0
4898 /* If there are pending saves/restores, the
4899 optimization is not worth. */
4900 && usage_insns[regno].calls_num == calls_num - 1
4901 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
4903 /* Restore the pseudo from the call result as
4904 REG_RETURNED note says that the pseudo value is
4905 in the call result and the pseudo is an argument
4906 of the call. */
4907 pat = PATTERN (curr_insn);
4908 if (GET_CODE (pat) == PARALLEL)
4909 pat = XVECEXP (pat, 0, 0);
4910 dest = SET_DEST (pat);
4911 start_sequence ();
4912 emit_move_insn (cheap, copy_rtx (dest));
4913 restore = get_insns ();
4914 end_sequence ();
4915 lra_process_new_insns (curr_insn, NULL, restore,
4916 "Inserting call parameter restore");
4917 /* We don't need to save/restore of the pseudo from
4918 this call. */
4919 usage_insns[regno].calls_num = calls_num;
4920 bitmap_set_bit (&check_only_regs, regno);
4923 to_inherit_num = 0;
4924 /* Process insn usages. */
4925 for (iter = 0; iter < 2; iter++)
4926 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
4927 reg != NULL;
4928 reg = reg->next)
4929 if ((reg->type != OP_OUT
4930 || (reg->type == OP_OUT && reg->subreg_p))
4931 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
4933 if (src_regno >= FIRST_PSEUDO_REGISTER
4934 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
4936 if (usage_insns[src_regno].check == curr_usage_insns_check
4937 && (next_usage_insns
4938 = usage_insns[src_regno].insns) != NULL_RTX
4939 && NONDEBUG_INSN_P (curr_insn))
4940 add_to_inherit (src_regno, next_usage_insns);
4941 else if (usage_insns[src_regno].check
4942 != -(int) INSN_UID (curr_insn))
4943 /* Add usages but only if the reg is not set up
4944 in the same insn. */
4945 add_next_usage_insn (src_regno, curr_insn, reloads_num);
4947 else if (src_regno < FIRST_PSEUDO_REGISTER
4948 || reg_renumber[src_regno] >= 0)
4950 bool before_p;
4951 rtx use_insn = curr_insn;
4953 before_p = (JUMP_P (curr_insn)
4954 || (CALL_P (curr_insn) && reg->type == OP_IN));
4955 if (NONDEBUG_INSN_P (curr_insn)
4956 && split_if_necessary (src_regno, reg->biggest_mode,
4957 potential_reload_hard_regs,
4958 before_p, curr_insn, max_uid))
4960 if (reg->subreg_p)
4961 lra_risky_transformations_p = true;
4962 change_p = true;
4963 /* Invalidate. */
4964 usage_insns[src_regno].check = 0;
4965 if (before_p)
4966 use_insn = PREV_INSN (curr_insn);
4968 if (NONDEBUG_INSN_P (curr_insn))
4970 if (src_regno < FIRST_PSEUDO_REGISTER)
4971 add_to_hard_reg_set (&live_hard_regs,
4972 reg->biggest_mode, src_regno);
4973 else
4974 add_to_hard_reg_set (&live_hard_regs,
4975 PSEUDO_REGNO_MODE (src_regno),
4976 reg_renumber[src_regno]);
4978 add_next_usage_insn (src_regno, use_insn, reloads_num);
4981 for (i = 0; i < to_inherit_num; i++)
4983 src_regno = to_inherit[i].regno;
4984 if (inherit_reload_reg (false, src_regno, ALL_REGS,
4985 curr_insn, to_inherit[i].insns))
4986 change_p = true;
4987 else
4988 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
4991 /* We reached the start of the current basic block. */
4992 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
4993 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
4995 /* We reached the beginning of the current block -- do
4996 rest of spliting in the current BB. */
4997 to_process = df_get_live_in (curr_bb);
4998 if (BLOCK_FOR_INSN (head) != curr_bb)
5000 /* We are somewhere in the middle of EBB. */
5001 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5002 curr_bb, &temp_bitmap);
5003 to_process = &temp_bitmap;
5005 head_p = true;
5006 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5008 if ((int) j >= lra_constraint_new_regno_start)
5009 break;
5010 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5011 && usage_insns[j].check == curr_usage_insns_check
5012 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5014 if (need_for_split_p (potential_reload_hard_regs, j))
5016 if (lra_dump_file != NULL && head_p)
5018 fprintf (lra_dump_file,
5019 " ----------------------------------\n");
5020 head_p = false;
5022 if (split_reg (false, j, bb_note (curr_bb),
5023 next_usage_insns))
5024 change_p = true;
5026 usage_insns[j].check = 0;
5031 return change_p;
5034 /* This value affects EBB forming. If probability of edge from EBB to
5035 a BB is not greater than the following value, we don't add the BB
5036 to EBB. */
5037 #define EBB_PROBABILITY_CUTOFF ((REG_BR_PROB_BASE * 50) / 100)
5039 /* Current number of inheritance/split iteration. */
5040 int lra_inheritance_iter;
5042 /* Entry function for inheritance/split pass. */
5043 void
5044 lra_inheritance (void)
5046 int i;
5047 basic_block bb, start_bb;
5048 edge e;
5050 lra_inheritance_iter++;
5051 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5052 return;
5053 timevar_push (TV_LRA_INHERITANCE);
5054 if (lra_dump_file != NULL)
5055 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5056 lra_inheritance_iter);
5057 curr_usage_insns_check = 0;
5058 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5059 for (i = 0; i < lra_constraint_new_regno_start; i++)
5060 usage_insns[i].check = 0;
5061 bitmap_initialize (&check_only_regs, &reg_obstack);
5062 bitmap_initialize (&live_regs, &reg_obstack);
5063 bitmap_initialize (&temp_bitmap, &reg_obstack);
5064 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5065 FOR_EACH_BB (bb)
5067 start_bb = bb;
5068 if (lra_dump_file != NULL)
5069 fprintf (lra_dump_file, "EBB");
5070 /* Form a EBB starting with BB. */
5071 bitmap_clear (&ebb_global_regs);
5072 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5073 for (;;)
5075 if (lra_dump_file != NULL)
5076 fprintf (lra_dump_file, " %d", bb->index);
5077 if (bb->next_bb == EXIT_BLOCK_PTR || LABEL_P (BB_HEAD (bb->next_bb)))
5078 break;
5079 e = find_fallthru_edge (bb->succs);
5080 if (! e)
5081 break;
5082 if (e->probability <= EBB_PROBABILITY_CUTOFF)
5083 break;
5084 bb = bb->next_bb;
5086 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5087 if (lra_dump_file != NULL)
5088 fprintf (lra_dump_file, "\n");
5089 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5090 /* Remember that the EBB head and tail can change in
5091 inherit_in_ebb. */
5092 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5094 bitmap_clear (&ebb_global_regs);
5095 bitmap_clear (&temp_bitmap);
5096 bitmap_clear (&live_regs);
5097 bitmap_clear (&check_only_regs);
5098 free (usage_insns);
5100 timevar_pop (TV_LRA_INHERITANCE);
5105 /* This page contains code to undo failed inheritance/split
5106 transformations. */
5108 /* Current number of iteration undoing inheritance/split. */
5109 int lra_undo_inheritance_iter;
5111 /* Fix BB live info LIVE after removing pseudos created on pass doing
5112 inheritance/split which are REMOVED_PSEUDOS. */
5113 static void
5114 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5116 unsigned int regno;
5117 bitmap_iterator bi;
5119 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5120 if (bitmap_clear_bit (live, regno))
5121 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5124 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5125 number. */
5126 static int
5127 get_regno (rtx reg)
5129 if (GET_CODE (reg) == SUBREG)
5130 reg = SUBREG_REG (reg);
5131 if (REG_P (reg))
5132 return REGNO (reg);
5133 return -1;
5136 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5137 return true if we did any change. The undo transformations for
5138 inheritance looks like
5139 i <- i2
5140 p <- i => p <- i2
5141 or removing
5142 p <- i, i <- p, and i <- i3
5143 where p is original pseudo from which inheritance pseudo i was
5144 created, i and i3 are removed inheritance pseudos, i2 is another
5145 not removed inheritance pseudo. All split pseudos or other
5146 occurrences of removed inheritance pseudos are changed on the
5147 corresponding original pseudos.
5149 The function also schedules insns changed and created during
5150 inheritance/split pass for processing by the subsequent constraint
5151 pass. */
5152 static bool
5153 remove_inheritance_pseudos (bitmap remove_pseudos)
5155 basic_block bb;
5156 int regno, sregno, prev_sregno, dregno, restore_regno;
5157 rtx set, prev_set, prev_insn;
5158 bool change_p, done_p;
5160 change_p = ! bitmap_empty_p (remove_pseudos);
5161 /* We can not finish the function right away if CHANGE_P is true
5162 because we need to marks insns affected by previous
5163 inheritance/split pass for processing by the subsequent
5164 constraint pass. */
5165 FOR_EACH_BB (bb)
5167 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5168 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5169 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5171 if (! INSN_P (curr_insn))
5172 continue;
5173 done_p = false;
5174 sregno = dregno = -1;
5175 if (change_p && NONDEBUG_INSN_P (curr_insn)
5176 && (set = single_set (curr_insn)) != NULL_RTX)
5178 dregno = get_regno (SET_DEST (set));
5179 sregno = get_regno (SET_SRC (set));
5182 if (sregno >= 0 && dregno >= 0)
5184 if ((bitmap_bit_p (remove_pseudos, sregno)
5185 && (lra_reg_info[sregno].restore_regno == dregno
5186 || (bitmap_bit_p (remove_pseudos, dregno)
5187 && (lra_reg_info[sregno].restore_regno
5188 == lra_reg_info[dregno].restore_regno))))
5189 || (bitmap_bit_p (remove_pseudos, dregno)
5190 && lra_reg_info[dregno].restore_regno == sregno))
5191 /* One of the following cases:
5192 original <- removed inheritance pseudo
5193 removed inherit pseudo <- another removed inherit pseudo
5194 removed inherit pseudo <- original pseudo
5196 removed_split_pseudo <- original_reg
5197 original_reg <- removed_split_pseudo */
5199 if (lra_dump_file != NULL)
5201 fprintf (lra_dump_file, " Removing %s:\n",
5202 bitmap_bit_p (&lra_split_regs, sregno)
5203 || bitmap_bit_p (&lra_split_regs, dregno)
5204 ? "split" : "inheritance");
5205 dump_insn_slim (lra_dump_file, curr_insn);
5207 lra_set_insn_deleted (curr_insn);
5208 done_p = true;
5210 else if (bitmap_bit_p (remove_pseudos, sregno)
5211 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5213 /* Search the following pattern:
5214 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5215 original_pseudo <- inherit_or_split_pseudo1
5216 where the 2nd insn is the current insn and
5217 inherit_or_split_pseudo2 is not removed. If it is found,
5218 change the current insn onto:
5219 original_pseudo <- inherit_or_split_pseudo2. */
5220 for (prev_insn = PREV_INSN (curr_insn);
5221 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5222 prev_insn = PREV_INSN (prev_insn))
5224 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5225 && (prev_set = single_set (prev_insn)) != NULL_RTX
5226 /* There should be no subregs in insn we are
5227 searching because only the original reg might
5228 be in subreg when we changed the mode of
5229 load/store for splitting. */
5230 && REG_P (SET_DEST (prev_set))
5231 && REG_P (SET_SRC (prev_set))
5232 && (int) REGNO (SET_DEST (prev_set)) == sregno
5233 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5234 >= FIRST_PSEUDO_REGISTER)
5235 /* As we consider chain of inheritance or
5236 splitting described in above comment we should
5237 check that sregno and prev_sregno were
5238 inheritance/split pseudos created from the
5239 same original regno. */
5240 && (lra_reg_info[sregno].restore_regno
5241 == lra_reg_info[prev_sregno].restore_regno)
5242 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5244 lra_assert (GET_MODE (SET_SRC (prev_set))
5245 == GET_MODE (regno_reg_rtx[sregno]));
5246 if (GET_CODE (SET_SRC (set)) == SUBREG)
5247 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5248 else
5249 SET_SRC (set) = SET_SRC (prev_set);
5250 lra_push_insn_and_update_insn_regno_info (curr_insn);
5251 lra_set_used_insn_alternative_by_uid
5252 (INSN_UID (curr_insn), -1);
5253 done_p = true;
5254 if (lra_dump_file != NULL)
5256 fprintf (lra_dump_file, " Change reload insn:\n");
5257 dump_insn_slim (lra_dump_file, curr_insn);
5262 if (! done_p)
5264 struct lra_insn_reg *reg;
5265 bool restored_regs_p = false;
5266 bool kept_regs_p = false;
5268 curr_id = lra_get_insn_recog_data (curr_insn);
5269 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5271 regno = reg->regno;
5272 restore_regno = lra_reg_info[regno].restore_regno;
5273 if (restore_regno >= 0)
5275 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5277 substitute_pseudo (&curr_insn, regno,
5278 regno_reg_rtx[restore_regno]);
5279 restored_regs_p = true;
5281 else
5282 kept_regs_p = true;
5285 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5287 /* The instruction has changed since the previous
5288 constraints pass. */
5289 lra_push_insn_and_update_insn_regno_info (curr_insn);
5290 lra_set_used_insn_alternative_by_uid
5291 (INSN_UID (curr_insn), -1);
5293 else if (restored_regs_p)
5294 /* The instruction has been restored to the form that
5295 it had during the previous constraints pass. */
5296 lra_update_insn_regno_info (curr_insn);
5297 if (restored_regs_p && lra_dump_file != NULL)
5299 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5300 dump_insn_slim (lra_dump_file, curr_insn);
5305 return change_p;
5308 /* If optional reload pseudos failed to get a hard register or was not
5309 inherited, it is better to remove optional reloads. We do this
5310 transformation after undoing inheritance to figure out necessity to
5311 remove optional reloads easier. Return true if we do any
5312 change. */
5313 static bool
5314 undo_optional_reloads (void)
5316 bool change_p;
5317 unsigned int regno, uid;
5318 bitmap_iterator bi, bi2;
5319 rtx insn, set, src, dest;
5320 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5322 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5323 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5324 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5325 if (reg_renumber[regno] >= 0)
5326 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
5328 insn = lra_insn_recog_data[uid]->insn;
5329 if ((set = single_set (insn)) == NULL_RTX)
5330 continue;
5331 src = SET_SRC (set);
5332 dest = SET_DEST (set);
5333 if (! REG_P (src) || ! REG_P (dest))
5334 continue;
5335 if ((REGNO (src) == regno
5336 && lra_reg_info[regno].restore_regno != (int) REGNO (dest))
5337 || (REGNO (dest) == regno
5338 && lra_reg_info[regno].restore_regno != (int) REGNO (src)))
5340 /* Optional reload was inherited. Keep it. */
5341 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
5342 if (lra_dump_file != NULL)
5343 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
5346 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
5347 bitmap_initialize (&insn_bitmap, &reg_obstack);
5348 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
5350 if (lra_dump_file != NULL)
5351 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
5352 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
5353 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
5355 insn = lra_insn_recog_data[uid]->insn;
5356 if ((set = single_set (insn)) != NULL_RTX)
5358 src = SET_SRC (set);
5359 dest = SET_DEST (set);
5360 if (REG_P (src) && REG_P (dest)
5361 && ((REGNO (src) == regno
5362 && (lra_reg_info[regno].restore_regno
5363 == (int) REGNO (dest)))
5364 || (REGNO (dest) == regno
5365 && (lra_reg_info[regno].restore_regno
5366 == (int) REGNO (src)))))
5368 if (lra_dump_file != NULL)
5370 fprintf (lra_dump_file, " Deleting move %u\n",
5371 INSN_UID (insn));
5372 dump_insn_slim (lra_dump_file, insn);
5374 lra_set_insn_deleted (insn);
5375 continue;
5377 /* We should not worry about generation memory-memory
5378 moves here as if the corresponding inheritance did
5379 not work (inheritance pseudo did not get a hard reg),
5380 we remove the inheritance pseudo and the optional
5381 reload. */
5383 substitute_pseudo (&insn, regno,
5384 regno_reg_rtx[lra_reg_info[regno].restore_regno]);
5385 lra_update_insn_regno_info (insn);
5386 if (lra_dump_file != NULL)
5388 fprintf (lra_dump_file,
5389 " Restoring original insn:\n");
5390 dump_insn_slim (lra_dump_file, insn);
5394 /* Clear restore_regnos. */
5395 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5396 lra_reg_info[regno].restore_regno = -1;
5397 bitmap_clear (&insn_bitmap);
5398 bitmap_clear (&removed_optional_reload_pseudos);
5399 return change_p;
5402 /* Entry function for undoing inheritance/split transformation. Return true
5403 if we did any RTL change in this pass. */
5404 bool
5405 lra_undo_inheritance (void)
5407 unsigned int regno;
5408 int restore_regno, hard_regno;
5409 int n_all_inherit, n_inherit, n_all_split, n_split;
5410 bitmap_head remove_pseudos;
5411 bitmap_iterator bi;
5412 bool change_p;
5414 lra_undo_inheritance_iter++;
5415 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5416 return false;
5417 if (lra_dump_file != NULL)
5418 fprintf (lra_dump_file,
5419 "\n********** Undoing inheritance #%d: **********\n\n",
5420 lra_undo_inheritance_iter);
5421 bitmap_initialize (&remove_pseudos, &reg_obstack);
5422 n_inherit = n_all_inherit = 0;
5423 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5424 if (lra_reg_info[regno].restore_regno >= 0)
5426 n_all_inherit++;
5427 if (reg_renumber[regno] < 0)
5428 bitmap_set_bit (&remove_pseudos, regno);
5429 else
5430 n_inherit++;
5432 if (lra_dump_file != NULL && n_all_inherit != 0)
5433 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
5434 n_inherit, n_all_inherit,
5435 (double) n_inherit / n_all_inherit * 100);
5436 n_split = n_all_split = 0;
5437 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5438 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
5440 n_all_split++;
5441 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
5442 ? reg_renumber[restore_regno] : restore_regno);
5443 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
5444 bitmap_set_bit (&remove_pseudos, regno);
5445 else
5447 n_split++;
5448 if (lra_dump_file != NULL)
5449 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
5450 regno, restore_regno);
5453 if (lra_dump_file != NULL && n_all_split != 0)
5454 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
5455 n_split, n_all_split,
5456 (double) n_split / n_all_split * 100);
5457 change_p = remove_inheritance_pseudos (&remove_pseudos);
5458 bitmap_clear (&remove_pseudos);
5459 /* Clear restore_regnos. */
5460 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5461 lra_reg_info[regno].restore_regno = -1;
5462 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5463 lra_reg_info[regno].restore_regno = -1;
5464 change_p = undo_optional_reloads () || change_p;
5465 return change_p;