2013-11-27 Kugan Vivekanandarajah <kuganv@linaro.org>
[official-gcc.git] / gcc / lra-constraints.c
blob4c88cca669d28e2ef48b767eae88489acaa7539e
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 && curr_insn_set != NULL
274 && (OBJECT_P (SET_SRC (curr_insn_set))
275 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
276 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
277 /* When we don't know what class will be used finally for reload
278 pseudos, we use ALL_REGS. */
279 return ((regno >= new_regno_start && rclass == ALL_REGS)
280 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
281 && ! hard_reg_set_subset_p (reg_class_contents[cl],
282 lra_no_alloc_regs)));
283 else
285 common_class = ira_reg_class_subset[rclass][cl];
286 if (new_class != NULL)
287 *new_class = common_class;
288 if (hard_reg_set_subset_p (reg_class_contents[common_class],
289 lra_no_alloc_regs))
290 return false;
291 /* Check that there are enough allocatable regs. */
292 class_size = ira_class_hard_regs_num[common_class];
293 for (i = 0; i < class_size; i++)
295 hard_regno = ira_class_hard_regs[common_class][i];
296 nregs = hard_regno_nregs[hard_regno][reg_mode];
297 if (nregs == 1)
298 return true;
299 for (j = 0; j < nregs; j++)
300 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
301 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
302 hard_regno + j))
303 break;
304 if (j >= nregs)
305 return true;
307 return false;
311 /* Return true if REGNO satisfies a memory constraint. */
312 static bool
313 in_mem_p (int regno)
315 return get_reg_class (regno) == NO_REGS;
318 /* If we have decided to substitute X with another value, return that
319 value, otherwise return X. */
320 static rtx
321 get_equiv_substitution (rtx x)
323 int regno;
324 rtx res;
326 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
327 || ! ira_reg_equiv[regno].defined_p
328 || ! ira_reg_equiv[regno].profitable_p
329 || lra_get_regno_hard_regno (regno) >= 0)
330 return x;
331 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
332 return res;
333 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
334 return res;
335 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
336 return res;
337 gcc_unreachable ();
340 /* Set up curr_operand_mode. */
341 static void
342 init_curr_operand_mode (void)
344 int nop = curr_static_id->n_operands;
345 for (int i = 0; i < nop; i++)
347 enum machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
348 if (mode == VOIDmode)
350 /* The .md mode for address operands is the mode of the
351 addressed value rather than the mode of the address itself. */
352 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
353 mode = Pmode;
354 else
355 mode = curr_static_id->operand[i].mode;
357 curr_operand_mode[i] = mode;
363 /* The page contains code to reuse input reloads. */
365 /* Structure describes input reload of the current insns. */
366 struct input_reload
368 /* Reloaded value. */
369 rtx input;
370 /* Reload pseudo used. */
371 rtx reg;
374 /* The number of elements in the following array. */
375 static int curr_insn_input_reloads_num;
376 /* Array containing info about input reloads. It is used to find the
377 same input reload and reuse the reload pseudo in this case. */
378 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
380 /* Initiate data concerning reuse of input reloads for the current
381 insn. */
382 static void
383 init_curr_insn_input_reloads (void)
385 curr_insn_input_reloads_num = 0;
388 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
389 created input reload pseudo (only if TYPE is not OP_OUT). The
390 result pseudo is returned through RESULT_REG. Return TRUE if we
391 created a new pseudo, FALSE if we reused the already created input
392 reload pseudo. Use TITLE to describe new registers for debug
393 purposes. */
394 static bool
395 get_reload_reg (enum op_type type, enum machine_mode mode, rtx original,
396 enum reg_class rclass, const char *title, rtx *result_reg)
398 int i, regno;
399 enum reg_class new_class;
401 if (type == OP_OUT)
403 *result_reg
404 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
405 return true;
407 /* Prevent reuse value of expression with side effects,
408 e.g. volatile memory. */
409 if (! side_effects_p (original))
410 for (i = 0; i < curr_insn_input_reloads_num; i++)
411 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
412 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
414 rtx reg = curr_insn_input_reloads[i].reg;
415 regno = REGNO (reg);
416 /* If input is equal to original and both are VOIDmode,
417 GET_MODE (reg) might be still different from mode.
418 Ensure we don't return *result_reg with wrong mode. */
419 if (GET_MODE (reg) != mode)
421 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
422 continue;
423 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
424 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
425 continue;
427 *result_reg = reg;
428 if (lra_dump_file != NULL)
430 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
431 dump_value_slim (lra_dump_file, original, 1);
433 if (new_class != lra_get_allocno_class (regno))
434 lra_change_class (regno, new_class, ", change to", false);
435 if (lra_dump_file != NULL)
436 fprintf (lra_dump_file, "\n");
437 return false;
439 *result_reg = lra_create_new_reg (mode, original, rclass, title);
440 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
441 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
442 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
443 return true;
448 /* The page contains code to extract memory address parts. */
450 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
451 static inline bool
452 ok_for_index_p_nonstrict (rtx reg)
454 unsigned regno = REGNO (reg);
456 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
459 /* A version of regno_ok_for_base_p for use here, when all pseudos
460 should count as OK. Arguments as for regno_ok_for_base_p. */
461 static inline bool
462 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode, addr_space_t as,
463 enum rtx_code outer_code, enum rtx_code index_code)
465 unsigned regno = REGNO (reg);
467 if (regno >= FIRST_PSEUDO_REGISTER)
468 return true;
469 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
474 /* The page contains major code to choose the current insn alternative
475 and generate reloads for it. */
477 /* Return the offset from REGNO of the least significant register
478 in (reg:MODE REGNO).
480 This function is used to tell whether two registers satisfy
481 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
483 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
484 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
486 lra_constraint_offset (int regno, enum machine_mode mode)
488 lra_assert (regno < FIRST_PSEUDO_REGISTER);
489 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
490 && SCALAR_INT_MODE_P (mode))
491 return hard_regno_nregs[regno][mode] - 1;
492 return 0;
495 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
496 if they are the same hard reg, and has special hacks for
497 auto-increment and auto-decrement. This is specifically intended for
498 process_alt_operands to use in determining whether two operands
499 match. X is the operand whose number is the lower of the two.
501 It is supposed that X is the output operand and Y is the input
502 operand. Y_HARD_REGNO is the final hard regno of register Y or
503 register in subreg Y as we know it now. Otherwise, it is a
504 negative value. */
505 static bool
506 operands_match_p (rtx x, rtx y, int y_hard_regno)
508 int i;
509 RTX_CODE code = GET_CODE (x);
510 const char *fmt;
512 if (x == y)
513 return true;
514 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
515 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
517 int j;
519 i = get_hard_regno (x);
520 if (i < 0)
521 goto slow;
523 if ((j = y_hard_regno) < 0)
524 goto slow;
526 i += lra_constraint_offset (i, GET_MODE (x));
527 j += lra_constraint_offset (j, GET_MODE (y));
529 return i == j;
532 /* If two operands must match, because they are really a single
533 operand of an assembler insn, then two post-increments are invalid
534 because the assembler insn would increment only once. On the
535 other hand, a post-increment matches ordinary indexing if the
536 post-increment is the output operand. */
537 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
538 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
540 /* Two pre-increments are invalid because the assembler insn would
541 increment only once. On the other hand, a pre-increment matches
542 ordinary indexing if the pre-increment is the input operand. */
543 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
544 || GET_CODE (y) == PRE_MODIFY)
545 return operands_match_p (x, XEXP (y, 0), -1);
547 slow:
549 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
550 && x == SUBREG_REG (y))
551 return true;
552 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
553 && SUBREG_REG (x) == y)
554 return true;
556 /* Now we have disposed of all the cases in which different rtx
557 codes can match. */
558 if (code != GET_CODE (y))
559 return false;
561 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
562 if (GET_MODE (x) != GET_MODE (y))
563 return false;
565 switch (code)
567 CASE_CONST_UNIQUE:
568 return false;
570 case LABEL_REF:
571 return XEXP (x, 0) == XEXP (y, 0);
572 case SYMBOL_REF:
573 return XSTR (x, 0) == XSTR (y, 0);
575 default:
576 break;
579 /* Compare the elements. If any pair of corresponding elements fail
580 to match, return false for the whole things. */
582 fmt = GET_RTX_FORMAT (code);
583 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
585 int val, j;
586 switch (fmt[i])
588 case 'w':
589 if (XWINT (x, i) != XWINT (y, i))
590 return false;
591 break;
593 case 'i':
594 if (XINT (x, i) != XINT (y, i))
595 return false;
596 break;
598 case 'e':
599 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
600 if (val == 0)
601 return false;
602 break;
604 case '0':
605 break;
607 case 'E':
608 if (XVECLEN (x, i) != XVECLEN (y, i))
609 return false;
610 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
612 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
613 if (val == 0)
614 return false;
616 break;
618 /* It is believed that rtx's at this level will never
619 contain anything but integers and other rtx's, except for
620 within LABEL_REFs and SYMBOL_REFs. */
621 default:
622 gcc_unreachable ();
625 return true;
628 /* True if X is a constant that can be forced into the constant pool.
629 MODE is the mode of the operand, or VOIDmode if not known. */
630 #define CONST_POOL_OK_P(MODE, X) \
631 ((MODE) != VOIDmode \
632 && CONSTANT_P (X) \
633 && GET_CODE (X) != HIGH \
634 && !targetm.cannot_force_const_mem (MODE, X))
636 /* True if C is a non-empty register class that has too few registers
637 to be safely used as a reload target class. */
638 #define SMALL_REGISTER_CLASS_P(C) \
639 (reg_class_size [(C)] == 1 \
640 || (reg_class_size [(C)] >= 1 && targetm.class_likely_spilled_p (C)))
642 /* If REG is a reload pseudo, try to make its class satisfying CL. */
643 static void
644 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
646 enum reg_class rclass;
648 /* Do not make more accurate class from reloads generated. They are
649 mostly moves with a lot of constraints. Making more accurate
650 class may results in very narrow class and impossibility of find
651 registers for several reloads of one insn. */
652 if (INSN_UID (curr_insn) >= new_insn_uid_start)
653 return;
654 if (GET_CODE (reg) == SUBREG)
655 reg = SUBREG_REG (reg);
656 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
657 return;
658 if (in_class_p (reg, cl, &rclass) && rclass != cl)
659 lra_change_class (REGNO (reg), rclass, " Change to", true);
662 /* Generate reloads for matching OUT and INS (array of input operand
663 numbers with end marker -1) with reg class GOAL_CLASS. Add input
664 and output reloads correspondingly to the lists *BEFORE and *AFTER.
665 OUT might be negative. In this case we generate input reloads for
666 matched input operands INS. */
667 static void
668 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
669 rtx *before, rtx *after)
671 int i, in;
672 rtx new_in_reg, new_out_reg, reg, clobber;
673 enum machine_mode inmode, outmode;
674 rtx in_rtx = *curr_id->operand_loc[ins[0]];
675 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
677 inmode = curr_operand_mode[ins[0]];
678 outmode = out < 0 ? inmode : curr_operand_mode[out];
679 push_to_sequence (*before);
680 if (inmode != outmode)
682 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
684 reg = new_in_reg
685 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
686 goal_class, "");
687 if (SCALAR_INT_MODE_P (inmode))
688 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
689 else
690 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
691 LRA_SUBREG_P (new_out_reg) = 1;
692 /* If the input reg is dying here, we can use the same hard
693 register for REG and IN_RTX. We do it only for original
694 pseudos as reload pseudos can die although original
695 pseudos still live where reload pseudos dies. */
696 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
697 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
698 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
700 else
702 reg = new_out_reg
703 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
704 goal_class, "");
705 if (SCALAR_INT_MODE_P (outmode))
706 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
707 else
708 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
709 /* NEW_IN_REG is non-paradoxical subreg. We don't want
710 NEW_OUT_REG living above. We add clobber clause for
711 this. This is just a temporary clobber. We can remove
712 it at the end of LRA work. */
713 clobber = emit_clobber (new_out_reg);
714 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
715 LRA_SUBREG_P (new_in_reg) = 1;
716 if (GET_CODE (in_rtx) == SUBREG)
718 rtx subreg_reg = SUBREG_REG (in_rtx);
720 /* If SUBREG_REG is dying here and sub-registers IN_RTX
721 and NEW_IN_REG are similar, we can use the same hard
722 register for REG and SUBREG_REG. */
723 if (REG_P (subreg_reg)
724 && (int) REGNO (subreg_reg) < lra_new_regno_start
725 && GET_MODE (subreg_reg) == outmode
726 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
727 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
728 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
732 else
734 /* Pseudos have values -- see comments for lra_reg_info.
735 Different pseudos with the same value do not conflict even if
736 they live in the same place. When we create a pseudo we
737 assign value of original pseudo (if any) from which we
738 created the new pseudo. If we create the pseudo from the
739 input pseudo, the new pseudo will no conflict with the input
740 pseudo which is wrong when the input pseudo lives after the
741 insn and as the new pseudo value is changed by the insn
742 output. Therefore we create the new pseudo from the output.
744 We cannot reuse the current output register because we might
745 have a situation like "a <- a op b", where the constraints
746 force the second input operand ("b") to match the output
747 operand ("a"). "b" must then be copied into a new register
748 so that it doesn't clobber the current value of "a". */
750 new_in_reg = new_out_reg
751 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
752 goal_class, "");
754 /* In operand can be got from transformations before processing insn
755 constraints. One example of such transformations is subreg
756 reloading (see function simplify_operand_subreg). The new
757 pseudos created by the transformations might have inaccurate
758 class (ALL_REGS) and we should make their classes more
759 accurate. */
760 narrow_reload_pseudo_class (in_rtx, goal_class);
761 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
762 *before = get_insns ();
763 end_sequence ();
764 for (i = 0; (in = ins[i]) >= 0; i++)
766 lra_assert
767 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
768 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
769 *curr_id->operand_loc[in] = new_in_reg;
771 lra_update_dups (curr_id, ins);
772 if (out < 0)
773 return;
774 /* See a comment for the input operand above. */
775 narrow_reload_pseudo_class (out_rtx, goal_class);
776 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
778 start_sequence ();
779 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
780 emit_insn (*after);
781 *after = get_insns ();
782 end_sequence ();
784 *curr_id->operand_loc[out] = new_out_reg;
785 lra_update_dup (curr_id, out);
788 /* Return register class which is union of all reg classes in insn
789 constraint alternative string starting with P. */
790 static enum reg_class
791 reg_class_from_constraints (const char *p)
793 int c, len;
794 enum reg_class op_class = NO_REGS;
797 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
799 case '#':
800 case ',':
801 return op_class;
803 case 'p':
804 op_class = (reg_class_subunion
805 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
806 ADDRESS, SCRATCH)]);
807 break;
809 case 'g':
810 case 'r':
811 op_class = reg_class_subunion[op_class][GENERAL_REGS];
812 break;
814 default:
815 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
817 #ifdef EXTRA_CONSTRAINT_STR
818 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
819 op_class
820 = (reg_class_subunion
821 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
822 ADDRESS, SCRATCH)]);
823 #endif
824 break;
827 op_class
828 = reg_class_subunion[op_class][REG_CLASS_FROM_CONSTRAINT (c, p)];
829 break;
831 while ((p += len), c);
832 return op_class;
835 /* If OP is a register, return the class of the register as per
836 get_reg_class, otherwise return NO_REGS. */
837 static inline enum reg_class
838 get_op_class (rtx op)
840 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
843 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
844 otherwise. If modes of MEM_PSEUDO and VAL are different, use
845 SUBREG for VAL to make them equal. */
846 static rtx
847 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
849 if (GET_MODE (mem_pseudo) != GET_MODE (val))
851 /* Usually size of mem_pseudo is greater than val size but in
852 rare cases it can be less as it can be defined by target
853 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
854 if (! MEM_P (val))
856 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
857 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
859 LRA_SUBREG_P (val) = 1;
861 else
863 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
864 LRA_SUBREG_P (mem_pseudo) = 1;
867 return (to_p
868 ? gen_move_insn (mem_pseudo, val)
869 : gen_move_insn (val, mem_pseudo));
872 /* Process a special case insn (register move), return true if we
873 don't need to process it anymore. INSN should be a single set
874 insn. Set up that RTL was changed through CHANGE_P and macro
875 SECONDARY_MEMORY_NEEDED says to use secondary memory through
876 SEC_MEM_P. */
877 static bool
878 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
880 int sregno, dregno;
881 rtx dest, src, dreg, sreg, old_sreg, new_reg, before, scratch_reg;
882 enum reg_class dclass, sclass, secondary_class;
883 enum machine_mode sreg_mode;
884 secondary_reload_info sri;
886 lra_assert (curr_insn_set != NULL_RTX);
887 dreg = dest = SET_DEST (curr_insn_set);
888 sreg = src = SET_SRC (curr_insn_set);
889 if (GET_CODE (dest) == SUBREG)
890 dreg = SUBREG_REG (dest);
891 if (GET_CODE (src) == SUBREG)
892 sreg = SUBREG_REG (src);
893 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
894 return false;
895 sclass = dclass = NO_REGS;
896 if (REG_P (dreg))
897 dclass = get_reg_class (REGNO (dreg));
898 if (dclass == ALL_REGS)
899 /* ALL_REGS is used for new pseudos created by transformations
900 like reload of SUBREG_REG (see function
901 simplify_operand_subreg). We don't know their class yet. We
902 should figure out the class from processing the insn
903 constraints not in this fast path function. Even if ALL_REGS
904 were a right class for the pseudo, secondary_... hooks usually
905 are not define for ALL_REGS. */
906 return false;
907 sreg_mode = GET_MODE (sreg);
908 old_sreg = sreg;
909 if (REG_P (sreg))
910 sclass = get_reg_class (REGNO (sreg));
911 if (sclass == ALL_REGS)
912 /* See comments above. */
913 return false;
914 if (sclass == NO_REGS && dclass == NO_REGS)
915 return false;
916 #ifdef SECONDARY_MEMORY_NEEDED
917 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
918 #ifdef SECONDARY_MEMORY_NEEDED_MODE
919 && ((sclass != NO_REGS && dclass != NO_REGS)
920 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
921 #endif
924 *sec_mem_p = true;
925 return false;
927 #endif
928 if (! REG_P (dreg) || ! REG_P (sreg))
929 return false;
930 sri.prev_sri = NULL;
931 sri.icode = CODE_FOR_nothing;
932 sri.extra_cost = 0;
933 secondary_class = NO_REGS;
934 /* Set up hard register for a reload pseudo for hook
935 secondary_reload because some targets just ignore unassigned
936 pseudos in the hook. */
937 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
939 dregno = REGNO (dreg);
940 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
942 else
943 dregno = -1;
944 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
946 sregno = REGNO (sreg);
947 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
949 else
950 sregno = -1;
951 if (sclass != NO_REGS)
952 secondary_class
953 = (enum reg_class) targetm.secondary_reload (false, dest,
954 (reg_class_t) sclass,
955 GET_MODE (src), &sri);
956 if (sclass == NO_REGS
957 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
958 && dclass != NO_REGS))
960 enum reg_class old_sclass = secondary_class;
961 secondary_reload_info old_sri = sri;
963 sri.prev_sri = NULL;
964 sri.icode = CODE_FOR_nothing;
965 sri.extra_cost = 0;
966 secondary_class
967 = (enum reg_class) targetm.secondary_reload (true, sreg,
968 (reg_class_t) dclass,
969 sreg_mode, &sri);
970 /* Check the target hook consistency. */
971 lra_assert
972 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
973 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
974 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
976 if (sregno >= 0)
977 reg_renumber [sregno] = -1;
978 if (dregno >= 0)
979 reg_renumber [dregno] = -1;
980 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
981 return false;
982 *change_p = true;
983 new_reg = NULL_RTX;
984 if (secondary_class != NO_REGS)
985 new_reg = lra_create_new_reg_with_unique_value (sreg_mode, NULL_RTX,
986 secondary_class,
987 "secondary");
988 start_sequence ();
989 if (old_sreg != sreg)
990 sreg = copy_rtx (sreg);
991 if (sri.icode == CODE_FOR_nothing)
992 lra_emit_move (new_reg, sreg);
993 else
995 enum reg_class scratch_class;
997 scratch_class = (reg_class_from_constraints
998 (insn_data[sri.icode].operand[2].constraint));
999 scratch_reg = (lra_create_new_reg_with_unique_value
1000 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1001 scratch_class, "scratch"));
1002 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1003 sreg, scratch_reg));
1005 before = get_insns ();
1006 end_sequence ();
1007 lra_process_new_insns (curr_insn, before, NULL_RTX, "Inserting the move");
1008 if (new_reg != NULL_RTX)
1010 if (GET_CODE (src) == SUBREG)
1011 SUBREG_REG (src) = new_reg;
1012 else
1013 SET_SRC (curr_insn_set) = new_reg;
1015 else
1017 if (lra_dump_file != NULL)
1019 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1020 dump_insn_slim (lra_dump_file, curr_insn);
1022 lra_set_insn_deleted (curr_insn);
1023 return true;
1025 return false;
1028 /* The following data describe the result of process_alt_operands.
1029 The data are used in curr_insn_transform to generate reloads. */
1031 /* The chosen reg classes which should be used for the corresponding
1032 operands. */
1033 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1034 /* True if the operand should be the same as another operand and that
1035 other operand does not need a reload. */
1036 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1037 /* True if the operand does not need a reload. */
1038 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1039 /* True if the operand can be offsetable memory. */
1040 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1041 /* The number of an operand to which given operand can be matched to. */
1042 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1043 /* The number of elements in the following array. */
1044 static int goal_alt_dont_inherit_ops_num;
1045 /* Numbers of operands whose reload pseudos should not be inherited. */
1046 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1047 /* True if the insn commutative operands should be swapped. */
1048 static bool goal_alt_swapped;
1049 /* The chosen insn alternative. */
1050 static int goal_alt_number;
1052 /* The following five variables are used to choose the best insn
1053 alternative. They reflect final characteristics of the best
1054 alternative. */
1056 /* Number of necessary reloads and overall cost reflecting the
1057 previous value and other unpleasantness of the best alternative. */
1058 static int best_losers, best_overall;
1059 /* Overall number hard registers used for reloads. For example, on
1060 some targets we need 2 general registers to reload DFmode and only
1061 one floating point register. */
1062 static int best_reload_nregs;
1063 /* Overall number reflecting distances of previous reloading the same
1064 value. The distances are counted from the current BB start. It is
1065 used to improve inheritance chances. */
1066 static int best_reload_sum;
1068 /* True if the current insn should have no correspondingly input or
1069 output reloads. */
1070 static bool no_input_reloads_p, no_output_reloads_p;
1072 /* True if we swapped the commutative operands in the current
1073 insn. */
1074 static int curr_swapped;
1076 /* Arrange for address element *LOC to be a register of class CL.
1077 Add any input reloads to list BEFORE. AFTER is nonnull if *LOC is an
1078 automodified value; handle that case by adding the required output
1079 reloads to list AFTER. Return true if the RTL was changed. */
1080 static bool
1081 process_addr_reg (rtx *loc, rtx *before, rtx *after, enum reg_class cl)
1083 int regno;
1084 enum reg_class rclass, new_class;
1085 rtx reg;
1086 rtx new_reg;
1087 enum machine_mode mode;
1088 bool before_p = false;
1090 loc = strip_subreg (loc);
1091 reg = *loc;
1092 mode = GET_MODE (reg);
1093 if (! REG_P (reg))
1095 /* Always reload memory in an address even if the target supports
1096 such addresses. */
1097 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1098 before_p = true;
1100 else
1102 regno = REGNO (reg);
1103 rclass = get_reg_class (regno);
1104 if ((*loc = get_equiv_substitution (reg)) != reg)
1106 if (lra_dump_file != NULL)
1108 fprintf (lra_dump_file,
1109 "Changing pseudo %d in address of insn %u on equiv ",
1110 REGNO (reg), INSN_UID (curr_insn));
1111 dump_value_slim (lra_dump_file, *loc, 1);
1112 fprintf (lra_dump_file, "\n");
1114 *loc = copy_rtx (*loc);
1116 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1118 reg = *loc;
1119 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1120 mode, reg, cl, "address", &new_reg))
1121 before_p = true;
1123 else if (new_class != NO_REGS && rclass != new_class)
1125 lra_change_class (regno, new_class, " Change to", true);
1126 return false;
1128 else
1129 return false;
1131 if (before_p)
1133 push_to_sequence (*before);
1134 lra_emit_move (new_reg, reg);
1135 *before = get_insns ();
1136 end_sequence ();
1138 *loc = new_reg;
1139 if (after != NULL)
1141 start_sequence ();
1142 lra_emit_move (reg, new_reg);
1143 emit_insn (*after);
1144 *after = get_insns ();
1145 end_sequence ();
1147 return true;
1150 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1151 the insn to be inserted before curr insn. AFTER returns the
1152 the insn to be inserted after curr insn. ORIGREG and NEWREG
1153 are the original reg and new reg for reload. */
1154 static void
1155 insert_move_for_subreg (rtx *before, rtx *after, rtx origreg, rtx newreg)
1157 if (before)
1159 push_to_sequence (*before);
1160 lra_emit_move (newreg, origreg);
1161 *before = get_insns ();
1162 end_sequence ();
1164 if (after)
1166 start_sequence ();
1167 lra_emit_move (origreg, newreg);
1168 emit_insn (*after);
1169 *after = get_insns ();
1170 end_sequence ();
1174 /* Make reloads for subreg in operand NOP with internal subreg mode
1175 REG_MODE, add new reloads for further processing. Return true if
1176 any reload was generated. */
1177 static bool
1178 simplify_operand_subreg (int nop, enum machine_mode reg_mode)
1180 int hard_regno;
1181 rtx before, after;
1182 enum machine_mode mode;
1183 rtx reg, new_reg;
1184 rtx operand = *curr_id->operand_loc[nop];
1185 enum reg_class regclass;
1186 enum op_type type;
1188 before = after = NULL_RTX;
1190 if (GET_CODE (operand) != SUBREG)
1191 return false;
1193 mode = GET_MODE (operand);
1194 reg = SUBREG_REG (operand);
1195 type = curr_static_id->operand[nop].type;
1196 /* If we change address for paradoxical subreg of memory, the
1197 address might violate the necessary alignment or the access might
1198 be slow. So take this into consideration. We should not worry
1199 about access beyond allocated memory for paradoxical memory
1200 subregs as we don't substitute such equiv memory (see processing
1201 equivalences in function lra_constraints) and because for spilled
1202 pseudos we allocate stack memory enough for the biggest
1203 corresponding paradoxical subreg. */
1204 if ((MEM_P (reg)
1205 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1206 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1207 || (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER))
1209 alter_subreg (curr_id->operand_loc[nop], false);
1210 return true;
1212 /* Put constant into memory when we have mixed modes. It generates
1213 a better code in most cases as it does not need a secondary
1214 reload memory. It also prevents LRA looping when LRA is using
1215 secondary reload memory again and again. */
1216 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1217 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1219 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1220 alter_subreg (curr_id->operand_loc[nop], false);
1221 return true;
1223 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1224 if there may be a problem accessing OPERAND in the outer
1225 mode. */
1226 if ((REG_P (reg)
1227 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1228 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1229 /* Don't reload paradoxical subregs because we could be looping
1230 having repeatedly final regno out of hard regs range. */
1231 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1232 >= hard_regno_nregs[hard_regno][mode])
1233 && simplify_subreg_regno (hard_regno, GET_MODE (reg),
1234 SUBREG_BYTE (operand), mode) < 0
1235 /* Don't reload subreg for matching reload. It is actually
1236 valid subreg in LRA. */
1237 && ! LRA_SUBREG_P (operand))
1238 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1240 /* The class will be defined later in curr_insn_transform. */
1241 enum reg_class rclass
1242 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1244 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1245 rclass, "subreg reg", &new_reg))
1247 bool insert_before, insert_after;
1248 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1250 insert_before = (type != OP_OUT
1251 || GET_MODE_SIZE (GET_MODE (reg)) > GET_MODE_SIZE (mode));
1252 insert_after = (type != OP_IN);
1253 insert_move_for_subreg (insert_before ? &before : NULL,
1254 insert_after ? &after : NULL,
1255 reg, new_reg);
1257 SUBREG_REG (operand) = new_reg;
1258 lra_process_new_insns (curr_insn, before, after,
1259 "Inserting subreg reload");
1260 return true;
1262 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1263 IRA allocates hardreg to the inner pseudo reg according to its mode
1264 instead of the outermode, so the size of the hardreg may not be enough
1265 to contain the outermode operand, in that case we may need to insert
1266 reload for the reg. For the following two types of paradoxical subreg,
1267 we need to insert reload:
1268 1. If the op_type is OP_IN, and the hardreg could not be paired with
1269 other hardreg to contain the outermode operand
1270 (checked by in_hard_reg_set_p), we need to insert the reload.
1271 2. If the op_type is OP_OUT or OP_INOUT.
1273 Here is a paradoxical subreg example showing how the reload is generated:
1275 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1276 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1278 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1279 here, if reg107 is assigned to hardreg R15, because R15 is the last
1280 hardreg, compiler cannot find another hardreg to pair with R15 to
1281 contain TImode data. So we insert a TImode reload reg180 for it.
1282 After reload is inserted:
1284 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1285 (reg:DI 107 [ __comp ])) -1
1286 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1287 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1289 Two reload hard registers will be allocated to reg180 to save TImode data
1290 in LRA_assign. */
1291 else if (REG_P (reg)
1292 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1293 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1294 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1295 < hard_regno_nregs[hard_regno][mode])
1296 && (regclass = lra_get_allocno_class (REGNO (reg)))
1297 && (type != OP_IN
1298 || !in_hard_reg_set_p (reg_class_contents[regclass],
1299 mode, hard_regno)))
1301 /* The class will be defined later in curr_insn_transform. */
1302 enum reg_class rclass
1303 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1305 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1306 rclass, "paradoxical subreg", &new_reg))
1308 rtx subreg;
1309 bool insert_before, insert_after;
1311 PUT_MODE (new_reg, mode);
1312 subreg = simplify_gen_subreg (GET_MODE (reg), new_reg, mode, 0);
1313 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1315 insert_before = (type != OP_OUT);
1316 insert_after = (type != OP_IN);
1317 insert_move_for_subreg (insert_before ? &before : NULL,
1318 insert_after ? &after : NULL,
1319 reg, subreg);
1321 SUBREG_REG (operand) = new_reg;
1322 lra_process_new_insns (curr_insn, before, after,
1323 "Inserting paradoxical subreg reload");
1324 return true;
1326 return false;
1329 /* Return TRUE if X refers for a hard register from SET. */
1330 static bool
1331 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1333 int i, j, x_hard_regno;
1334 enum machine_mode mode;
1335 const char *fmt;
1336 enum rtx_code code;
1338 if (x == NULL_RTX)
1339 return false;
1340 code = GET_CODE (x);
1341 mode = GET_MODE (x);
1342 if (code == SUBREG)
1344 x = SUBREG_REG (x);
1345 code = GET_CODE (x);
1346 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1347 mode = GET_MODE (x);
1350 if (REG_P (x))
1352 x_hard_regno = get_hard_regno (x);
1353 return (x_hard_regno >= 0
1354 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1356 if (MEM_P (x))
1358 struct address_info ad;
1360 decompose_mem_address (&ad, x);
1361 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1362 return true;
1363 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1364 return true;
1366 fmt = GET_RTX_FORMAT (code);
1367 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1369 if (fmt[i] == 'e')
1371 if (uses_hard_regs_p (XEXP (x, i), set))
1372 return true;
1374 else if (fmt[i] == 'E')
1376 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1377 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1378 return true;
1381 return false;
1384 /* Return true if OP is a spilled pseudo. */
1385 static inline bool
1386 spilled_pseudo_p (rtx op)
1388 return (REG_P (op)
1389 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1392 /* Return true if X is a general constant. */
1393 static inline bool
1394 general_constant_p (rtx x)
1396 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1399 static bool
1400 reg_in_class_p (rtx reg, enum reg_class cl)
1402 if (cl == NO_REGS)
1403 return get_reg_class (REGNO (reg)) == NO_REGS;
1404 return in_class_p (reg, cl, NULL);
1407 /* Major function to choose the current insn alternative and what
1408 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1409 negative we should consider only this alternative. Return false if
1410 we can not choose the alternative or find how to reload the
1411 operands. */
1412 static bool
1413 process_alt_operands (int only_alternative)
1415 bool ok_p = false;
1416 int nop, overall, nalt;
1417 int n_alternatives = curr_static_id->n_alternatives;
1418 int n_operands = curr_static_id->n_operands;
1419 /* LOSERS counts the operands that don't fit this alternative and
1420 would require loading. */
1421 int losers;
1422 /* REJECT is a count of how undesirable this alternative says it is
1423 if any reloading is required. If the alternative matches exactly
1424 then REJECT is ignored, but otherwise it gets this much counted
1425 against it in addition to the reloading needed. */
1426 int reject;
1427 /* The number of elements in the following array. */
1428 int early_clobbered_regs_num;
1429 /* Numbers of operands which are early clobber registers. */
1430 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1431 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1432 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1433 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1434 bool curr_alt_win[MAX_RECOG_OPERANDS];
1435 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1436 int curr_alt_matches[MAX_RECOG_OPERANDS];
1437 /* The number of elements in the following array. */
1438 int curr_alt_dont_inherit_ops_num;
1439 /* Numbers of operands whose reload pseudos should not be inherited. */
1440 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1441 rtx op;
1442 /* The register when the operand is a subreg of register, otherwise the
1443 operand itself. */
1444 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1445 /* The register if the operand is a register or subreg of register,
1446 otherwise NULL. */
1447 rtx operand_reg[MAX_RECOG_OPERANDS];
1448 int hard_regno[MAX_RECOG_OPERANDS];
1449 enum machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1450 int reload_nregs, reload_sum;
1451 bool costly_p;
1452 enum reg_class cl;
1454 /* Calculate some data common for all alternatives to speed up the
1455 function. */
1456 for (nop = 0; nop < n_operands; nop++)
1458 rtx reg;
1460 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1461 /* The real hard regno of the operand after the allocation. */
1462 hard_regno[nop] = get_hard_regno (op);
1464 operand_reg[nop] = reg = op;
1465 biggest_mode[nop] = GET_MODE (op);
1466 if (GET_CODE (op) == SUBREG)
1468 operand_reg[nop] = reg = SUBREG_REG (op);
1469 if (GET_MODE_SIZE (biggest_mode[nop])
1470 < GET_MODE_SIZE (GET_MODE (reg)))
1471 biggest_mode[nop] = GET_MODE (reg);
1473 if (! REG_P (reg))
1474 operand_reg[nop] = NULL_RTX;
1475 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1476 || ((int) REGNO (reg)
1477 == lra_get_elimination_hard_regno (REGNO (reg))))
1478 no_subreg_reg_operand[nop] = reg;
1479 else
1480 operand_reg[nop] = no_subreg_reg_operand[nop]
1481 /* Just use natural mode for elimination result. It should
1482 be enough for extra constraints hooks. */
1483 = regno_reg_rtx[hard_regno[nop]];
1486 /* The constraints are made of several alternatives. Each operand's
1487 constraint looks like foo,bar,... with commas separating the
1488 alternatives. The first alternatives for all operands go
1489 together, the second alternatives go together, etc.
1491 First loop over alternatives. */
1492 for (nalt = 0; nalt < n_alternatives; nalt++)
1494 /* Loop over operands for one constraint alternative. */
1495 #if HAVE_ATTR_enabled
1496 if (curr_id->alternative_enabled_p != NULL
1497 && ! curr_id->alternative_enabled_p[nalt])
1498 continue;
1499 #endif
1501 if (only_alternative >= 0 && nalt != only_alternative)
1502 continue;
1505 overall = losers = reject = reload_nregs = reload_sum = 0;
1506 for (nop = 0; nop < n_operands; nop++)
1508 int inc = (curr_static_id
1509 ->operand_alternative[nalt * n_operands + nop].reject);
1510 if (lra_dump_file != NULL && inc != 0)
1511 fprintf (lra_dump_file,
1512 " Staticly defined alt reject+=%d\n", inc);
1513 reject += inc;
1515 early_clobbered_regs_num = 0;
1517 for (nop = 0; nop < n_operands; nop++)
1519 const char *p;
1520 char *end;
1521 int len, c, m, i, opalt_num, this_alternative_matches;
1522 bool win, did_match, offmemok, early_clobber_p;
1523 /* false => this operand can be reloaded somehow for this
1524 alternative. */
1525 bool badop;
1526 /* true => this operand can be reloaded if the alternative
1527 allows regs. */
1528 bool winreg;
1529 /* True if a constant forced into memory would be OK for
1530 this operand. */
1531 bool constmemok;
1532 enum reg_class this_alternative, this_costly_alternative;
1533 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1534 bool this_alternative_match_win, this_alternative_win;
1535 bool this_alternative_offmemok;
1536 bool scratch_p;
1537 enum machine_mode mode;
1539 opalt_num = nalt * n_operands + nop;
1540 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1542 /* Fast track for no constraints at all. */
1543 curr_alt[nop] = NO_REGS;
1544 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1545 curr_alt_win[nop] = true;
1546 curr_alt_match_win[nop] = false;
1547 curr_alt_offmemok[nop] = false;
1548 curr_alt_matches[nop] = -1;
1549 continue;
1552 op = no_subreg_reg_operand[nop];
1553 mode = curr_operand_mode[nop];
1555 win = did_match = winreg = offmemok = constmemok = false;
1556 badop = true;
1558 early_clobber_p = false;
1559 p = curr_static_id->operand_alternative[opalt_num].constraint;
1561 this_costly_alternative = this_alternative = NO_REGS;
1562 /* We update set of possible hard regs besides its class
1563 because reg class might be inaccurate. For example,
1564 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1565 is translated in HI_REGS because classes are merged by
1566 pairs and there is no accurate intermediate class. */
1567 CLEAR_HARD_REG_SET (this_alternative_set);
1568 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1569 this_alternative_win = false;
1570 this_alternative_match_win = false;
1571 this_alternative_offmemok = false;
1572 this_alternative_matches = -1;
1574 /* An empty constraint should be excluded by the fast
1575 track. */
1576 lra_assert (*p != 0 && *p != ',');
1578 /* Scan this alternative's specs for this operand; set WIN
1579 if the operand fits any letter in this alternative.
1580 Otherwise, clear BADOP if this operand could fit some
1581 letter after reloads, or set WINREG if this operand could
1582 fit after reloads provided the constraint allows some
1583 registers. */
1584 costly_p = false;
1587 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1589 case '\0':
1590 len = 0;
1591 break;
1592 case ',':
1593 c = '\0';
1594 break;
1596 case '=': case '+': case '?': case '*': case '!':
1597 case ' ': case '\t':
1598 break;
1600 case '%':
1601 /* We only support one commutative marker, the first
1602 one. We already set commutative above. */
1603 break;
1605 case '&':
1606 early_clobber_p = true;
1607 break;
1609 case '#':
1610 /* Ignore rest of this alternative. */
1611 c = '\0';
1612 break;
1614 case '0': case '1': case '2': case '3': case '4':
1615 case '5': case '6': case '7': case '8': case '9':
1617 int m_hregno;
1618 bool match_p;
1620 m = strtoul (p, &end, 10);
1621 p = end;
1622 len = 0;
1623 lra_assert (nop > m);
1625 this_alternative_matches = m;
1626 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1627 /* We are supposed to match a previous operand.
1628 If we do, we win if that one did. If we do
1629 not, count both of the operands as losers.
1630 (This is too conservative, since most of the
1631 time only a single reload insn will be needed
1632 to make the two operands win. As a result,
1633 this alternative may be rejected when it is
1634 actually desirable.) */
1635 match_p = false;
1636 if (operands_match_p (*curr_id->operand_loc[nop],
1637 *curr_id->operand_loc[m], m_hregno))
1639 /* We should reject matching of an early
1640 clobber operand if the matching operand is
1641 not dying in the insn. */
1642 if (! curr_static_id->operand[m].early_clobber
1643 || operand_reg[nop] == NULL_RTX
1644 || (find_regno_note (curr_insn, REG_DEAD,
1645 REGNO (op))
1646 || REGNO (op) == REGNO (operand_reg[m])))
1647 match_p = true;
1649 if (match_p)
1651 /* If we are matching a non-offsettable
1652 address where an offsettable address was
1653 expected, then we must reject this
1654 combination, because we can't reload
1655 it. */
1656 if (curr_alt_offmemok[m]
1657 && MEM_P (*curr_id->operand_loc[m])
1658 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1659 continue;
1662 else
1664 /* Operands don't match. Both operands must
1665 allow a reload register, otherwise we
1666 cannot make them match. */
1667 if (curr_alt[m] == NO_REGS)
1668 break;
1669 /* Retroactively mark the operand we had to
1670 match as a loser, if it wasn't already and
1671 it wasn't matched to a register constraint
1672 (e.g it might be matched by memory). */
1673 if (curr_alt_win[m]
1674 && (operand_reg[m] == NULL_RTX
1675 || hard_regno[m] < 0))
1677 losers++;
1678 reload_nregs
1679 += (ira_reg_class_max_nregs[curr_alt[m]]
1680 [GET_MODE (*curr_id->operand_loc[m])]);
1683 /* We prefer no matching alternatives because
1684 it gives more freedom in RA. */
1685 if (operand_reg[nop] == NULL_RTX
1686 || (find_regno_note (curr_insn, REG_DEAD,
1687 REGNO (operand_reg[nop]))
1688 == NULL_RTX))
1690 if (lra_dump_file != NULL)
1691 fprintf
1692 (lra_dump_file,
1693 " %d Matching alt: reject+=2\n",
1694 nop);
1695 reject += 2;
1698 /* If we have to reload this operand and some
1699 previous operand also had to match the same
1700 thing as this operand, we don't know how to do
1701 that. */
1702 if (!match_p || !curr_alt_win[m])
1704 for (i = 0; i < nop; i++)
1705 if (curr_alt_matches[i] == m)
1706 break;
1707 if (i < nop)
1708 break;
1710 else
1711 did_match = true;
1713 /* This can be fixed with reloads if the operand
1714 we are supposed to match can be fixed with
1715 reloads. */
1716 badop = false;
1717 this_alternative = curr_alt[m];
1718 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1719 winreg = this_alternative != NO_REGS;
1720 break;
1723 case 'p':
1724 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1725 ADDRESS, SCRATCH);
1726 this_alternative = reg_class_subunion[this_alternative][cl];
1727 IOR_HARD_REG_SET (this_alternative_set,
1728 reg_class_contents[cl]);
1729 if (costly_p)
1731 this_costly_alternative
1732 = reg_class_subunion[this_costly_alternative][cl];
1733 IOR_HARD_REG_SET (this_costly_alternative_set,
1734 reg_class_contents[cl]);
1736 win = true;
1737 badop = false;
1738 break;
1740 case TARGET_MEM_CONSTRAINT:
1741 if (MEM_P (op) || spilled_pseudo_p (op))
1742 win = true;
1743 /* We can put constant or pseudo value into memory
1744 to satisfy the constraint. */
1745 if (CONST_POOL_OK_P (mode, op) || REG_P (op))
1746 badop = false;
1747 constmemok = true;
1748 break;
1750 case '<':
1751 if (MEM_P (op)
1752 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1753 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1754 win = true;
1755 break;
1757 case '>':
1758 if (MEM_P (op)
1759 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1760 || GET_CODE (XEXP (op, 0)) == POST_INC))
1761 win = true;
1762 break;
1764 /* Memory op whose address is not offsettable. */
1765 case 'V':
1766 if (MEM_P (op)
1767 && ! offsettable_nonstrict_memref_p (op))
1768 win = true;
1769 break;
1771 /* Memory operand whose address is offsettable. */
1772 case 'o':
1773 if ((MEM_P (op)
1774 && offsettable_nonstrict_memref_p (op))
1775 || spilled_pseudo_p (op))
1776 win = true;
1777 /* We can put constant or pseudo value into memory
1778 or make memory address offsetable to satisfy the
1779 constraint. */
1780 if (CONST_POOL_OK_P (mode, op) || MEM_P (op) || REG_P (op))
1781 badop = false;
1782 constmemok = true;
1783 offmemok = true;
1784 break;
1786 case 'E':
1787 case 'F':
1788 if (GET_CODE (op) == CONST_DOUBLE
1789 || (GET_CODE (op) == CONST_VECTOR
1790 && (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)))
1791 win = true;
1792 break;
1794 case 'G':
1795 case 'H':
1796 if (CONST_DOUBLE_AS_FLOAT_P (op)
1797 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1798 win = true;
1799 break;
1801 case 's':
1802 if (CONST_SCALAR_INT_P (op))
1803 break;
1805 case 'i':
1806 if (general_constant_p (op))
1807 win = true;
1808 break;
1810 case 'n':
1811 if (CONST_SCALAR_INT_P (op))
1812 win = true;
1813 break;
1815 case 'I':
1816 case 'J':
1817 case 'K':
1818 case 'L':
1819 case 'M':
1820 case 'N':
1821 case 'O':
1822 case 'P':
1823 if (CONST_INT_P (op)
1824 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1825 win = true;
1826 break;
1828 case 'X':
1829 /* This constraint should be excluded by the fast
1830 track. */
1831 gcc_unreachable ();
1832 break;
1834 case 'g':
1835 if (MEM_P (op)
1836 || general_constant_p (op)
1837 || spilled_pseudo_p (op))
1838 win = true;
1839 /* Drop through into 'r' case. */
1841 case 'r':
1842 this_alternative
1843 = reg_class_subunion[this_alternative][GENERAL_REGS];
1844 IOR_HARD_REG_SET (this_alternative_set,
1845 reg_class_contents[GENERAL_REGS]);
1846 if (costly_p)
1848 this_costly_alternative
1849 = (reg_class_subunion
1850 [this_costly_alternative][GENERAL_REGS]);
1851 IOR_HARD_REG_SET (this_costly_alternative_set,
1852 reg_class_contents[GENERAL_REGS]);
1854 goto reg;
1856 default:
1857 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
1859 #ifdef EXTRA_CONSTRAINT_STR
1860 if (EXTRA_MEMORY_CONSTRAINT (c, p))
1862 if (EXTRA_CONSTRAINT_STR (op, c, p))
1863 win = true;
1864 else if (spilled_pseudo_p (op))
1865 win = true;
1867 /* If we didn't already win, we can reload
1868 constants via force_const_mem or put the
1869 pseudo value into memory, or make other
1870 memory by reloading the address like for
1871 'o'. */
1872 if (CONST_POOL_OK_P (mode, op)
1873 || MEM_P (op) || REG_P (op))
1874 badop = false;
1875 constmemok = true;
1876 offmemok = true;
1877 break;
1879 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1881 if (EXTRA_CONSTRAINT_STR (op, c, p))
1882 win = true;
1884 /* If we didn't already win, we can reload
1885 the address into a base register. */
1886 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1887 ADDRESS, SCRATCH);
1888 this_alternative
1889 = reg_class_subunion[this_alternative][cl];
1890 IOR_HARD_REG_SET (this_alternative_set,
1891 reg_class_contents[cl]);
1892 if (costly_p)
1894 this_costly_alternative
1895 = (reg_class_subunion
1896 [this_costly_alternative][cl]);
1897 IOR_HARD_REG_SET (this_costly_alternative_set,
1898 reg_class_contents[cl]);
1900 badop = false;
1901 break;
1904 if (EXTRA_CONSTRAINT_STR (op, c, p))
1905 win = true;
1906 #endif
1907 break;
1910 cl = REG_CLASS_FROM_CONSTRAINT (c, p);
1911 this_alternative = reg_class_subunion[this_alternative][cl];
1912 IOR_HARD_REG_SET (this_alternative_set,
1913 reg_class_contents[cl]);
1914 if (costly_p)
1916 this_costly_alternative
1917 = reg_class_subunion[this_costly_alternative][cl];
1918 IOR_HARD_REG_SET (this_costly_alternative_set,
1919 reg_class_contents[cl]);
1921 reg:
1922 if (mode == BLKmode)
1923 break;
1924 winreg = true;
1925 if (REG_P (op))
1927 if (hard_regno[nop] >= 0
1928 && in_hard_reg_set_p (this_alternative_set,
1929 mode, hard_regno[nop]))
1930 win = true;
1931 else if (hard_regno[nop] < 0
1932 && in_class_p (op, this_alternative, NULL))
1933 win = true;
1935 break;
1937 if (c != ' ' && c != '\t')
1938 costly_p = c == '*';
1940 while ((p += len), c);
1942 scratch_p = (operand_reg[nop] != NULL_RTX
1943 && lra_former_scratch_p (REGNO (operand_reg[nop])));
1944 /* Record which operands fit this alternative. */
1945 if (win)
1947 this_alternative_win = true;
1948 if (operand_reg[nop] != NULL_RTX)
1950 if (hard_regno[nop] >= 0)
1952 if (in_hard_reg_set_p (this_costly_alternative_set,
1953 mode, hard_regno[nop]))
1955 if (lra_dump_file != NULL)
1956 fprintf (lra_dump_file,
1957 " %d Costly set: reject++\n",
1958 nop);
1959 reject++;
1962 else
1964 /* Prefer won reg to spilled pseudo under other
1965 equal conditions for possibe inheritance. */
1966 if (! scratch_p)
1968 if (lra_dump_file != NULL)
1969 fprintf
1970 (lra_dump_file,
1971 " %d Non pseudo reload: reject++\n",
1972 nop);
1973 reject++;
1975 if (in_class_p (operand_reg[nop],
1976 this_costly_alternative, NULL))
1978 if (lra_dump_file != NULL)
1979 fprintf
1980 (lra_dump_file,
1981 " %d Non pseudo costly reload:"
1982 " reject++\n",
1983 nop);
1984 reject++;
1987 /* We simulate the behaviour of old reload here.
1988 Although scratches need hard registers and it
1989 might result in spilling other pseudos, no reload
1990 insns are generated for the scratches. So it
1991 might cost something but probably less than old
1992 reload pass believes. */
1993 if (scratch_p)
1995 if (lra_dump_file != NULL)
1996 fprintf (lra_dump_file,
1997 " %d Scratch win: reject+=2\n",
1998 nop);
1999 reject += 2;
2003 else if (did_match)
2004 this_alternative_match_win = true;
2005 else
2007 int const_to_mem = 0;
2008 bool no_regs_p;
2010 /* If this alternative asks for a specific reg class, see if there
2011 is at least one allocatable register in that class. */
2012 no_regs_p
2013 = (this_alternative == NO_REGS
2014 || (hard_reg_set_subset_p
2015 (reg_class_contents[this_alternative],
2016 lra_no_alloc_regs)));
2018 /* For asms, verify that the class for this alternative is possible
2019 for the mode that is specified. */
2020 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2022 int i;
2023 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2024 if (HARD_REGNO_MODE_OK (i, mode)
2025 && in_hard_reg_set_p (reg_class_contents[this_alternative], mode, i))
2026 break;
2027 if (i == FIRST_PSEUDO_REGISTER)
2028 winreg = false;
2031 /* If this operand accepts a register, and if the
2032 register class has at least one allocatable register,
2033 then this operand can be reloaded. */
2034 if (winreg && !no_regs_p)
2035 badop = false;
2037 if (badop)
2038 goto fail;
2040 this_alternative_offmemok = offmemok;
2041 if (this_costly_alternative != NO_REGS)
2043 if (lra_dump_file != NULL)
2044 fprintf (lra_dump_file,
2045 " %d Costly loser: reject++\n", nop);
2046 reject++;
2048 /* If the operand is dying, has a matching constraint,
2049 and satisfies constraints of the matched operand
2050 which failed to satisfy the own constraints, we do
2051 not need to generate a reload insn for this
2052 operand. */
2053 if (!(this_alternative_matches >= 0
2054 && !curr_alt_win[this_alternative_matches]
2055 && REG_P (op)
2056 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2057 && (hard_regno[nop] >= 0
2058 ? in_hard_reg_set_p (this_alternative_set,
2059 mode, hard_regno[nop])
2060 : in_class_p (op, this_alternative, NULL))))
2062 /* Strict_low_part requires to reload the register
2063 not the sub-register. In this case we should
2064 check that a final reload hard reg can hold the
2065 value mode. */
2066 if (curr_static_id->operand[nop].strict_low
2067 && REG_P (op)
2068 && hard_regno[nop] < 0
2069 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2070 && ira_class_hard_regs_num[this_alternative] > 0
2071 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2072 [this_alternative][0],
2073 GET_MODE
2074 (*curr_id->operand_loc[nop])))
2075 goto fail;
2076 losers++;
2078 if (operand_reg[nop] != NULL_RTX
2079 /* Output operands and matched input operands are
2080 not inherited. The following conditions do not
2081 exactly describe the previous statement but they
2082 are pretty close. */
2083 && curr_static_id->operand[nop].type != OP_OUT
2084 && (this_alternative_matches < 0
2085 || curr_static_id->operand[nop].type != OP_IN))
2087 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2088 (operand_reg[nop])]
2089 .last_reload);
2091 if (last_reload > bb_reload_num)
2092 reload_sum += last_reload - bb_reload_num;
2094 /* If this is a constant that is reloaded into the
2095 desired class by copying it to memory first, count
2096 that as another reload. This is consistent with
2097 other code and is required to avoid choosing another
2098 alternative when the constant is moved into memory.
2099 Note that the test here is precisely the same as in
2100 the code below that calls force_const_mem. */
2101 if (CONST_POOL_OK_P (mode, op)
2102 && ((targetm.preferred_reload_class
2103 (op, this_alternative) == NO_REGS)
2104 || no_input_reloads_p))
2106 const_to_mem = 1;
2107 if (! no_regs_p)
2108 losers++;
2111 /* Alternative loses if it requires a type of reload not
2112 permitted for this insn. We can always reload
2113 objects with a REG_UNUSED note. */
2114 if ((curr_static_id->operand[nop].type != OP_IN
2115 && no_output_reloads_p
2116 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2117 || (curr_static_id->operand[nop].type != OP_OUT
2118 && no_input_reloads_p && ! const_to_mem))
2119 goto fail;
2121 /* Check strong discouragement of reload of non-constant
2122 into class THIS_ALTERNATIVE. */
2123 if (! CONSTANT_P (op) && ! no_regs_p
2124 && (targetm.preferred_reload_class
2125 (op, this_alternative) == NO_REGS
2126 || (curr_static_id->operand[nop].type == OP_OUT
2127 && (targetm.preferred_output_reload_class
2128 (op, this_alternative) == NO_REGS))))
2130 if (lra_dump_file != NULL)
2131 fprintf (lra_dump_file,
2132 " %d Non-prefered reload: reject+=%d\n",
2133 nop, LRA_MAX_REJECT);
2134 reject += LRA_MAX_REJECT;
2137 if (! (MEM_P (op) && offmemok)
2138 && ! (const_to_mem && constmemok))
2140 /* We prefer to reload pseudos over reloading other
2141 things, since such reloads may be able to be
2142 eliminated later. So bump REJECT in other cases.
2143 Don't do this in the case where we are forcing a
2144 constant into memory and it will then win since
2145 we don't want to have a different alternative
2146 match then. */
2147 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2149 if (lra_dump_file != NULL)
2150 fprintf
2151 (lra_dump_file,
2152 " %d Non-pseudo reload: reject+=2\n",
2153 nop);
2154 reject += 2;
2157 if (! no_regs_p)
2158 reload_nregs
2159 += ira_reg_class_max_nregs[this_alternative][mode];
2161 if (SMALL_REGISTER_CLASS_P (this_alternative))
2163 if (lra_dump_file != NULL)
2164 fprintf
2165 (lra_dump_file,
2166 " %d Small class reload: reject+=%d\n",
2167 nop, LRA_LOSER_COST_FACTOR / 2);
2168 reject += LRA_LOSER_COST_FACTOR / 2;
2172 /* We are trying to spill pseudo into memory. It is
2173 usually more costly than moving to a hard register
2174 although it might takes the same number of
2175 reloads. */
2176 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2178 if (lra_dump_file != NULL)
2179 fprintf
2180 (lra_dump_file,
2181 " %d Spill pseudo in memory: reject+=3\n",
2182 nop);
2183 reject += 3;
2186 #ifdef SECONDARY_MEMORY_NEEDED
2187 /* If reload requires moving value through secondary
2188 memory, it will need one more insn at least. */
2189 if (this_alternative != NO_REGS
2190 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2191 && ((curr_static_id->operand[nop].type != OP_OUT
2192 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2193 GET_MODE (op)))
2194 || (curr_static_id->operand[nop].type != OP_IN
2195 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2196 GET_MODE (op)))))
2197 losers++;
2198 #endif
2199 /* Input reloads can be inherited more often than output
2200 reloads can be removed, so penalize output
2201 reloads. */
2202 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2204 if (lra_dump_file != NULL)
2205 fprintf
2206 (lra_dump_file,
2207 " %d Non input pseudo reload: reject++\n",
2208 nop);
2209 reject++;
2213 if (early_clobber_p && ! scratch_p)
2215 if (lra_dump_file != NULL)
2216 fprintf (lra_dump_file,
2217 " %d Early clobber: reject++\n", nop);
2218 reject++;
2220 /* ??? We check early clobbers after processing all operands
2221 (see loop below) and there we update the costs more.
2222 Should we update the cost (may be approximately) here
2223 because of early clobber register reloads or it is a rare
2224 or non-important thing to be worth to do it. */
2225 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2226 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2228 if (lra_dump_file != NULL)
2229 fprintf (lra_dump_file,
2230 " alt=%d,overall=%d,losers=%d -- refuse\n",
2231 nalt, overall, losers);
2232 goto fail;
2235 curr_alt[nop] = this_alternative;
2236 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2237 curr_alt_win[nop] = this_alternative_win;
2238 curr_alt_match_win[nop] = this_alternative_match_win;
2239 curr_alt_offmemok[nop] = this_alternative_offmemok;
2240 curr_alt_matches[nop] = this_alternative_matches;
2242 if (this_alternative_matches >= 0
2243 && !did_match && !this_alternative_win)
2244 curr_alt_win[this_alternative_matches] = false;
2246 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2247 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2249 if (curr_insn_set != NULL_RTX && n_operands == 2
2250 /* Prevent processing non-move insns. */
2251 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2252 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2253 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2254 && REG_P (no_subreg_reg_operand[0])
2255 && REG_P (no_subreg_reg_operand[1])
2256 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2257 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2258 || (! curr_alt_win[0] && curr_alt_win[1]
2259 && REG_P (no_subreg_reg_operand[1])
2260 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2261 || (curr_alt_win[0] && ! curr_alt_win[1]
2262 && REG_P (no_subreg_reg_operand[0])
2263 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2264 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2265 no_subreg_reg_operand[1])
2266 || (targetm.preferred_reload_class
2267 (no_subreg_reg_operand[1],
2268 (enum reg_class) curr_alt[1]) != NO_REGS))
2269 /* If it is a result of recent elimination in move
2270 insn we can transform it into an add still by
2271 using this alternative. */
2272 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2274 /* We have a move insn and a new reload insn will be similar
2275 to the current insn. We should avoid such situation as it
2276 results in LRA cycling. */
2277 overall += LRA_MAX_REJECT;
2279 ok_p = true;
2280 curr_alt_dont_inherit_ops_num = 0;
2281 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2283 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2284 HARD_REG_SET temp_set;
2286 i = early_clobbered_nops[nop];
2287 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2288 || hard_regno[i] < 0)
2289 continue;
2290 lra_assert (operand_reg[i] != NULL_RTX);
2291 clobbered_hard_regno = hard_regno[i];
2292 CLEAR_HARD_REG_SET (temp_set);
2293 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2294 first_conflict_j = last_conflict_j = -1;
2295 for (j = 0; j < n_operands; j++)
2296 if (j == i
2297 /* We don't want process insides of match_operator and
2298 match_parallel because otherwise we would process
2299 their operands once again generating a wrong
2300 code. */
2301 || curr_static_id->operand[j].is_operator)
2302 continue;
2303 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2304 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2305 continue;
2306 /* If we don't reload j-th operand, check conflicts. */
2307 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2308 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2310 if (first_conflict_j < 0)
2311 first_conflict_j = j;
2312 last_conflict_j = j;
2314 if (last_conflict_j < 0)
2315 continue;
2316 /* If earlyclobber operand conflicts with another
2317 non-matching operand which is actually the same register
2318 as the earlyclobber operand, it is better to reload the
2319 another operand as an operand matching the earlyclobber
2320 operand can be also the same. */
2321 if (first_conflict_j == last_conflict_j
2322 && operand_reg[last_conflict_j]
2323 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2324 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2326 curr_alt_win[last_conflict_j] = false;
2327 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2328 = last_conflict_j;
2329 losers++;
2330 /* Early clobber was already reflected in REJECT. */
2331 lra_assert (reject > 0);
2332 if (lra_dump_file != NULL)
2333 fprintf
2334 (lra_dump_file,
2335 " %d Conflict early clobber reload: reject--\n",
2337 reject--;
2338 overall += LRA_LOSER_COST_FACTOR - 1;
2340 else
2342 /* We need to reload early clobbered register and the
2343 matched registers. */
2344 for (j = 0; j < n_operands; j++)
2345 if (curr_alt_matches[j] == i)
2347 curr_alt_match_win[j] = false;
2348 losers++;
2349 overall += LRA_LOSER_COST_FACTOR;
2351 if (! curr_alt_match_win[i])
2352 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2353 else
2355 /* Remember pseudos used for match reloads are never
2356 inherited. */
2357 lra_assert (curr_alt_matches[i] >= 0);
2358 curr_alt_win[curr_alt_matches[i]] = false;
2360 curr_alt_win[i] = curr_alt_match_win[i] = false;
2361 losers++;
2362 /* Early clobber was already reflected in REJECT. */
2363 lra_assert (reject > 0);
2364 if (lra_dump_file != NULL)
2365 fprintf
2366 (lra_dump_file,
2367 " %d Matched conflict early clobber reloads:"
2368 "reject--\n",
2370 reject--;
2371 overall += LRA_LOSER_COST_FACTOR - 1;
2374 if (lra_dump_file != NULL)
2375 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2376 nalt, overall, losers, reload_nregs);
2378 /* If this alternative can be made to work by reloading, and it
2379 needs less reloading than the others checked so far, record
2380 it as the chosen goal for reloading. */
2381 if ((best_losers != 0 && losers == 0)
2382 || (((best_losers == 0 && losers == 0)
2383 || (best_losers != 0 && losers != 0))
2384 && (best_overall > overall
2385 || (best_overall == overall
2386 /* If the cost of the reloads is the same,
2387 prefer alternative which requires minimal
2388 number of reload regs. */
2389 && (reload_nregs < best_reload_nregs
2390 || (reload_nregs == best_reload_nregs
2391 && (best_reload_sum < reload_sum
2392 || (best_reload_sum == reload_sum
2393 && nalt < goal_alt_number))))))))
2395 for (nop = 0; nop < n_operands; nop++)
2397 goal_alt_win[nop] = curr_alt_win[nop];
2398 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2399 goal_alt_matches[nop] = curr_alt_matches[nop];
2400 goal_alt[nop] = curr_alt[nop];
2401 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2403 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2404 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2405 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2406 goal_alt_swapped = curr_swapped;
2407 best_overall = overall;
2408 best_losers = losers;
2409 best_reload_nregs = reload_nregs;
2410 best_reload_sum = reload_sum;
2411 goal_alt_number = nalt;
2413 if (losers == 0)
2414 /* Everything is satisfied. Do not process alternatives
2415 anymore. */
2416 break;
2417 fail:
2420 return ok_p;
2423 /* Return 1 if ADDR is a valid memory address for mode MODE in address
2424 space AS, and check that each pseudo has the proper kind of hard
2425 reg. */
2426 static int
2427 valid_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2428 rtx addr, addr_space_t as)
2430 #ifdef GO_IF_LEGITIMATE_ADDRESS
2431 lra_assert (ADDR_SPACE_GENERIC_P (as));
2432 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
2433 return 0;
2435 win:
2436 return 1;
2437 #else
2438 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
2439 #endif
2442 /* Return whether address AD is valid. */
2444 static bool
2445 valid_address_p (struct address_info *ad)
2447 /* Some ports do not check displacements for eliminable registers,
2448 so we replace them temporarily with the elimination target. */
2449 rtx saved_base_reg = NULL_RTX;
2450 rtx saved_index_reg = NULL_RTX;
2451 rtx *base_term = strip_subreg (ad->base_term);
2452 rtx *index_term = strip_subreg (ad->index_term);
2453 if (base_term != NULL)
2455 saved_base_reg = *base_term;
2456 lra_eliminate_reg_if_possible (base_term);
2457 if (ad->base_term2 != NULL)
2458 *ad->base_term2 = *ad->base_term;
2460 if (index_term != NULL)
2462 saved_index_reg = *index_term;
2463 lra_eliminate_reg_if_possible (index_term);
2465 bool ok_p = valid_address_p (ad->mode, *ad->outer, ad->as);
2466 if (saved_base_reg != NULL_RTX)
2468 *base_term = saved_base_reg;
2469 if (ad->base_term2 != NULL)
2470 *ad->base_term2 = *ad->base_term;
2472 if (saved_index_reg != NULL_RTX)
2473 *index_term = saved_index_reg;
2474 return ok_p;
2477 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2478 static rtx
2479 base_plus_disp_to_reg (struct address_info *ad)
2481 enum reg_class cl;
2482 rtx new_reg;
2484 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2485 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2486 get_index_code (ad));
2487 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2488 cl, "base + disp");
2489 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2490 return new_reg;
2493 /* Return true if we can add a displacement to address AD, even if that
2494 makes the address invalid. The fix-up code requires any new address
2495 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2496 static bool
2497 can_add_disp_p (struct address_info *ad)
2499 return (!ad->autoinc_p
2500 && ad->segment == NULL
2501 && ad->base == ad->base_term
2502 && ad->disp == ad->disp_term);
2505 /* Make equiv substitution in address AD. Return true if a substitution
2506 was made. */
2507 static bool
2508 equiv_address_substitution (struct address_info *ad)
2510 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2511 HOST_WIDE_INT disp, scale;
2512 bool change_p;
2514 base_term = strip_subreg (ad->base_term);
2515 if (base_term == NULL)
2516 base_reg = new_base_reg = NULL_RTX;
2517 else
2519 base_reg = *base_term;
2520 new_base_reg = get_equiv_substitution (base_reg);
2522 index_term = strip_subreg (ad->index_term);
2523 if (index_term == NULL)
2524 index_reg = new_index_reg = NULL_RTX;
2525 else
2527 index_reg = *index_term;
2528 new_index_reg = get_equiv_substitution (index_reg);
2530 if (base_reg == new_base_reg && index_reg == new_index_reg)
2531 return false;
2532 disp = 0;
2533 change_p = false;
2534 if (lra_dump_file != NULL)
2536 fprintf (lra_dump_file, "Changing address in insn %d ",
2537 INSN_UID (curr_insn));
2538 dump_value_slim (lra_dump_file, *ad->outer, 1);
2540 if (base_reg != new_base_reg)
2542 if (REG_P (new_base_reg))
2544 *base_term = new_base_reg;
2545 change_p = true;
2547 else if (GET_CODE (new_base_reg) == PLUS
2548 && REG_P (XEXP (new_base_reg, 0))
2549 && CONST_INT_P (XEXP (new_base_reg, 1))
2550 && can_add_disp_p (ad))
2552 disp += INTVAL (XEXP (new_base_reg, 1));
2553 *base_term = XEXP (new_base_reg, 0);
2554 change_p = true;
2556 if (ad->base_term2 != NULL)
2557 *ad->base_term2 = *ad->base_term;
2559 if (index_reg != new_index_reg)
2561 if (REG_P (new_index_reg))
2563 *index_term = new_index_reg;
2564 change_p = true;
2566 else if (GET_CODE (new_index_reg) == PLUS
2567 && REG_P (XEXP (new_index_reg, 0))
2568 && CONST_INT_P (XEXP (new_index_reg, 1))
2569 && can_add_disp_p (ad)
2570 && (scale = get_index_scale (ad)))
2572 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2573 *index_term = XEXP (new_index_reg, 0);
2574 change_p = true;
2577 if (disp != 0)
2579 if (ad->disp != NULL)
2580 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2581 else
2583 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2584 update_address (ad);
2586 change_p = true;
2588 if (lra_dump_file != NULL)
2590 if (! change_p)
2591 fprintf (lra_dump_file, " -- no change\n");
2592 else
2594 fprintf (lra_dump_file, " on equiv ");
2595 dump_value_slim (lra_dump_file, *ad->outer, 1);
2596 fprintf (lra_dump_file, "\n");
2599 return change_p;
2602 /* Major function to make reloads for an address in operand NOP.
2603 The supported cases are:
2605 1) an address that existed before LRA started, at which point it
2606 must have been valid. These addresses are subject to elimination
2607 and may have become invalid due to the elimination offset being out
2608 of range.
2610 2) an address created by forcing a constant to memory
2611 (force_const_to_mem). The initial form of these addresses might
2612 not be valid, and it is this function's job to make them valid.
2614 3) a frame address formed from a register and a (possibly zero)
2615 constant offset. As above, these addresses might not be valid and
2616 this function must make them so.
2618 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2619 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2620 address. Return true for any RTL change. */
2621 static bool
2622 process_address (int nop, rtx *before, rtx *after)
2624 struct address_info ad;
2625 rtx new_reg;
2626 rtx op = *curr_id->operand_loc[nop];
2627 const char *constraint = curr_static_id->operand[nop].constraint;
2628 bool change_p;
2630 if (constraint[0] == 'p'
2631 || EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint))
2632 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2633 else if (MEM_P (op))
2634 decompose_mem_address (&ad, op);
2635 else if (GET_CODE (op) == SUBREG
2636 && MEM_P (SUBREG_REG (op)))
2637 decompose_mem_address (&ad, SUBREG_REG (op));
2638 else
2639 return false;
2640 change_p = equiv_address_substitution (&ad);
2641 if (ad.base_term != NULL
2642 && (process_addr_reg
2643 (ad.base_term, before,
2644 (ad.autoinc_p
2645 && !(REG_P (*ad.base_term)
2646 && find_regno_note (curr_insn, REG_DEAD,
2647 REGNO (*ad.base_term)) != NULL_RTX)
2648 ? after : NULL),
2649 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2650 get_index_code (&ad)))))
2652 change_p = true;
2653 if (ad.base_term2 != NULL)
2654 *ad.base_term2 = *ad.base_term;
2656 if (ad.index_term != NULL
2657 && process_addr_reg (ad.index_term, before, NULL, INDEX_REG_CLASS))
2658 change_p = true;
2660 #ifdef EXTRA_CONSTRAINT_STR
2661 /* Target hooks sometimes reject extra constraint addresses -- use
2662 EXTRA_CONSTRAINT_STR for the validation. */
2663 if (constraint[0] != 'p'
2664 && EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint)
2665 && EXTRA_CONSTRAINT_STR (op, constraint[0], constraint))
2666 return change_p;
2667 #endif
2669 /* There are three cases where the shape of *AD.INNER may now be invalid:
2671 1) the original address was valid, but either elimination or
2672 equiv_address_substitution was applied and that made
2673 the address invalid.
2675 2) the address is an invalid symbolic address created by
2676 force_const_to_mem.
2678 3) the address is a frame address with an invalid offset.
2680 All these cases involve a non-autoinc address, so there is no
2681 point revalidating other types. */
2682 if (ad.autoinc_p || valid_address_p (&ad))
2683 return change_p;
2685 /* Any index existed before LRA started, so we can assume that the
2686 presence and shape of the index is valid. */
2687 push_to_sequence (*before);
2688 lra_assert (ad.disp == ad.disp_term);
2689 if (ad.base == NULL)
2691 if (ad.index == NULL)
2693 int code = -1;
2694 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2695 SCRATCH, SCRATCH);
2696 rtx addr = *ad.inner;
2698 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2699 #ifdef HAVE_lo_sum
2701 rtx insn;
2702 rtx last = get_last_insn ();
2704 /* addr => lo_sum (new_base, addr), case (2) above. */
2705 insn = emit_insn (gen_rtx_SET
2706 (VOIDmode, new_reg,
2707 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2708 code = recog_memoized (insn);
2709 if (code >= 0)
2711 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2712 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2714 /* Try to put lo_sum into register. */
2715 insn = emit_insn (gen_rtx_SET
2716 (VOIDmode, new_reg,
2717 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2718 code = recog_memoized (insn);
2719 if (code >= 0)
2721 *ad.inner = new_reg;
2722 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2724 *ad.inner = addr;
2725 code = -1;
2731 if (code < 0)
2732 delete_insns_since (last);
2734 #endif
2735 if (code < 0)
2737 /* addr => new_base, case (2) above. */
2738 lra_emit_move (new_reg, addr);
2739 *ad.inner = new_reg;
2742 else
2744 /* index * scale + disp => new base + index * scale,
2745 case (1) above. */
2746 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
2747 GET_CODE (*ad.index));
2749 lra_assert (INDEX_REG_CLASS != NO_REGS);
2750 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2751 lra_emit_move (new_reg, *ad.disp);
2752 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2753 new_reg, *ad.index);
2756 else if (ad.index == NULL)
2758 int regno;
2759 enum reg_class cl;
2760 rtx set, insns, last_insn;
2761 /* base + disp => new base, cases (1) and (3) above. */
2762 /* Another option would be to reload the displacement into an
2763 index register. However, postreload has code to optimize
2764 address reloads that have the same base and different
2765 displacements, so reloading into an index register would
2766 not necessarily be a win. */
2767 start_sequence ();
2768 new_reg = base_plus_disp_to_reg (&ad);
2769 insns = get_insns ();
2770 last_insn = get_last_insn ();
2771 /* If we generated at least two insns, try last insn source as
2772 an address. If we succeed, we generate one less insn. */
2773 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
2774 && GET_CODE (SET_SRC (set)) == PLUS
2775 && REG_P (XEXP (SET_SRC (set), 0))
2776 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
2778 *ad.inner = SET_SRC (set);
2779 if (valid_address_p (ad.mode, *ad.outer, ad.as))
2781 *ad.base_term = XEXP (SET_SRC (set), 0);
2782 *ad.disp_term = XEXP (SET_SRC (set), 1);
2783 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2784 get_index_code (&ad));
2785 regno = REGNO (*ad.base_term);
2786 if (regno >= FIRST_PSEUDO_REGISTER
2787 && cl != lra_get_allocno_class (regno))
2788 lra_change_class (regno, cl, " Change to", true);
2789 new_reg = SET_SRC (set);
2790 delete_insns_since (PREV_INSN (last_insn));
2793 end_sequence ();
2794 emit_insn (insns);
2795 *ad.inner = new_reg;
2797 else
2799 /* base + scale * index + disp => new base + scale * index,
2800 case (1) above. */
2801 new_reg = base_plus_disp_to_reg (&ad);
2802 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2803 new_reg, *ad.index);
2805 *before = get_insns ();
2806 end_sequence ();
2807 return true;
2810 /* Emit insns to reload VALUE into a new register. VALUE is an
2811 auto-increment or auto-decrement RTX whose operand is a register or
2812 memory location; so reloading involves incrementing that location.
2813 IN is either identical to VALUE, or some cheaper place to reload
2814 value being incremented/decremented from.
2816 INC_AMOUNT is the number to increment or decrement by (always
2817 positive and ignored for POST_MODIFY/PRE_MODIFY).
2819 Return pseudo containing the result. */
2820 static rtx
2821 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
2823 /* REG or MEM to be copied and incremented. */
2824 rtx incloc = XEXP (value, 0);
2825 /* Nonzero if increment after copying. */
2826 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
2827 || GET_CODE (value) == POST_MODIFY);
2828 rtx last;
2829 rtx inc;
2830 rtx add_insn;
2831 int code;
2832 rtx real_in = in == value ? incloc : in;
2833 rtx result;
2834 bool plus_p = true;
2836 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
2838 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
2839 || GET_CODE (XEXP (value, 1)) == MINUS);
2840 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
2841 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
2842 inc = XEXP (XEXP (value, 1), 1);
2844 else
2846 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
2847 inc_amount = -inc_amount;
2849 inc = GEN_INT (inc_amount);
2852 if (! post && REG_P (incloc))
2853 result = incloc;
2854 else
2855 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
2856 "INC/DEC result");
2858 if (real_in != result)
2860 /* First copy the location to the result register. */
2861 lra_assert (REG_P (result));
2862 emit_insn (gen_move_insn (result, real_in));
2865 /* We suppose that there are insns to add/sub with the constant
2866 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
2867 old reload worked with this assumption. If the assumption
2868 becomes wrong, we should use approach in function
2869 base_plus_disp_to_reg. */
2870 if (in == value)
2872 /* See if we can directly increment INCLOC. */
2873 last = get_last_insn ();
2874 add_insn = emit_insn (plus_p
2875 ? gen_add2_insn (incloc, inc)
2876 : gen_sub2_insn (incloc, inc));
2878 code = recog_memoized (add_insn);
2879 if (code >= 0)
2881 if (! post && result != incloc)
2882 emit_insn (gen_move_insn (result, incloc));
2883 return result;
2885 delete_insns_since (last);
2888 /* If couldn't do the increment directly, must increment in RESULT.
2889 The way we do this depends on whether this is pre- or
2890 post-increment. For pre-increment, copy INCLOC to the reload
2891 register, increment it there, then save back. */
2892 if (! post)
2894 if (real_in != result)
2895 emit_insn (gen_move_insn (result, real_in));
2896 if (plus_p)
2897 emit_insn (gen_add2_insn (result, inc));
2898 else
2899 emit_insn (gen_sub2_insn (result, inc));
2900 if (result != incloc)
2901 emit_insn (gen_move_insn (incloc, result));
2903 else
2905 /* Post-increment.
2907 Because this might be a jump insn or a compare, and because
2908 RESULT may not be available after the insn in an input
2909 reload, we must do the incrementing before the insn being
2910 reloaded for.
2912 We have already copied IN to RESULT. Increment the copy in
2913 RESULT, save that back, then decrement RESULT so it has
2914 the original value. */
2915 if (plus_p)
2916 emit_insn (gen_add2_insn (result, inc));
2917 else
2918 emit_insn (gen_sub2_insn (result, inc));
2919 emit_insn (gen_move_insn (incloc, result));
2920 /* Restore non-modified value for the result. We prefer this
2921 way because it does not require an additional hard
2922 register. */
2923 if (plus_p)
2925 if (CONST_INT_P (inc))
2926 emit_insn (gen_add2_insn (result,
2927 gen_int_mode (-INTVAL (inc),
2928 GET_MODE (result))));
2929 else
2930 emit_insn (gen_sub2_insn (result, inc));
2932 else
2933 emit_insn (gen_add2_insn (result, inc));
2935 return result;
2938 /* Return true if the current move insn does not need processing as we
2939 already know that it satisfies its constraints. */
2940 static bool
2941 simple_move_p (void)
2943 rtx dest, src;
2944 enum reg_class dclass, sclass;
2946 lra_assert (curr_insn_set != NULL_RTX);
2947 dest = SET_DEST (curr_insn_set);
2948 src = SET_SRC (curr_insn_set);
2949 return ((dclass = get_op_class (dest)) != NO_REGS
2950 && (sclass = get_op_class (src)) != NO_REGS
2951 /* The backend guarantees that register moves of cost 2
2952 never need reloads. */
2953 && targetm.register_move_cost (GET_MODE (src), dclass, sclass) == 2);
2956 /* Swap operands NOP and NOP + 1. */
2957 static inline void
2958 swap_operands (int nop)
2960 enum machine_mode mode = curr_operand_mode[nop];
2961 curr_operand_mode[nop] = curr_operand_mode[nop + 1];
2962 curr_operand_mode[nop + 1] = mode;
2963 rtx x = *curr_id->operand_loc[nop];
2964 *curr_id->operand_loc[nop] = *curr_id->operand_loc[nop + 1];
2965 *curr_id->operand_loc[nop + 1] = x;
2966 /* Swap the duplicates too. */
2967 lra_update_dup (curr_id, nop);
2968 lra_update_dup (curr_id, nop + 1);
2971 /* Main entry point of the constraint code: search the body of the
2972 current insn to choose the best alternative. It is mimicking insn
2973 alternative cost calculation model of former reload pass. That is
2974 because machine descriptions were written to use this model. This
2975 model can be changed in future. Make commutative operand exchange
2976 if it is chosen.
2978 Return true if some RTL changes happened during function call. */
2979 static bool
2980 curr_insn_transform (void)
2982 int i, j, k;
2983 int n_operands;
2984 int n_alternatives;
2985 int commutative;
2986 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
2987 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
2988 rtx before, after;
2989 bool alt_p = false;
2990 /* Flag that the insn has been changed through a transformation. */
2991 bool change_p;
2992 bool sec_mem_p;
2993 #ifdef SECONDARY_MEMORY_NEEDED
2994 bool use_sec_mem_p;
2995 #endif
2996 int max_regno_before;
2997 int reused_alternative_num;
2999 curr_insn_set = single_set (curr_insn);
3000 if (curr_insn_set != NULL_RTX && simple_move_p ())
3001 return false;
3003 no_input_reloads_p = no_output_reloads_p = false;
3004 goal_alt_number = -1;
3005 change_p = sec_mem_p = false;
3006 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3007 reloads; neither are insns that SET cc0. Insns that use CC0 are
3008 not allowed to have any input reloads. */
3009 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3010 no_output_reloads_p = true;
3012 #ifdef HAVE_cc0
3013 if (reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3014 no_input_reloads_p = true;
3015 if (reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3016 no_output_reloads_p = true;
3017 #endif
3019 n_operands = curr_static_id->n_operands;
3020 n_alternatives = curr_static_id->n_alternatives;
3022 /* Just return "no reloads" if insn has no operands with
3023 constraints. */
3024 if (n_operands == 0 || n_alternatives == 0)
3025 return false;
3027 max_regno_before = max_reg_num ();
3029 for (i = 0; i < n_operands; i++)
3031 goal_alt_matched[i][0] = -1;
3032 goal_alt_matches[i] = -1;
3035 commutative = curr_static_id->commutative;
3037 /* Now see what we need for pseudos that didn't get hard regs or got
3038 the wrong kind of hard reg. For this, we must consider all the
3039 operands together against the register constraints. */
3041 best_losers = best_overall = INT_MAX;
3042 best_reload_sum = 0;
3044 curr_swapped = false;
3045 goal_alt_swapped = false;
3047 /* Make equivalence substitution and memory subreg elimination
3048 before address processing because an address legitimacy can
3049 depend on memory mode. */
3050 for (i = 0; i < n_operands; i++)
3052 rtx op = *curr_id->operand_loc[i];
3053 rtx subst, old = op;
3054 bool op_change_p = false;
3056 if (GET_CODE (old) == SUBREG)
3057 old = SUBREG_REG (old);
3058 subst = get_equiv_substitution (old);
3059 if (subst != old)
3061 subst = copy_rtx (subst);
3062 lra_assert (REG_P (old));
3063 if (GET_CODE (op) == SUBREG)
3064 SUBREG_REG (op) = subst;
3065 else
3066 *curr_id->operand_loc[i] = subst;
3067 if (lra_dump_file != NULL)
3069 fprintf (lra_dump_file,
3070 "Changing pseudo %d in operand %i of insn %u on equiv ",
3071 REGNO (old), i, INSN_UID (curr_insn));
3072 dump_value_slim (lra_dump_file, subst, 1);
3073 fprintf (lra_dump_file, "\n");
3075 op_change_p = change_p = true;
3077 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3079 change_p = true;
3080 lra_update_dup (curr_id, i);
3084 /* Reload address registers and displacements. We do it before
3085 finding an alternative because of memory constraints. */
3086 before = after = NULL_RTX;
3087 for (i = 0; i < n_operands; i++)
3088 if (! curr_static_id->operand[i].is_operator
3089 && process_address (i, &before, &after))
3091 change_p = true;
3092 lra_update_dup (curr_id, i);
3095 if (change_p)
3096 /* If we've changed the instruction then any alternative that
3097 we chose previously may no longer be valid. */
3098 lra_set_used_insn_alternative (curr_insn, -1);
3100 if (curr_insn_set != NULL_RTX
3101 && check_and_process_move (&change_p, &sec_mem_p))
3102 return change_p;
3104 try_swapped:
3106 reused_alternative_num = curr_id->used_insn_alternative;
3107 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3108 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3109 reused_alternative_num, INSN_UID (curr_insn));
3111 if (process_alt_operands (reused_alternative_num))
3112 alt_p = true;
3114 /* If insn is commutative (it's safe to exchange a certain pair of
3115 operands) then we need to try each alternative twice, the second
3116 time matching those two operands as if we had exchanged them. To
3117 do this, really exchange them in operands.
3119 If we have just tried the alternatives the second time, return
3120 operands to normal and drop through. */
3122 if (reused_alternative_num < 0 && commutative >= 0)
3124 curr_swapped = !curr_swapped;
3125 if (curr_swapped)
3127 swap_operands (commutative);
3128 goto try_swapped;
3130 else
3131 swap_operands (commutative);
3134 if (! alt_p && ! sec_mem_p)
3136 /* No alternative works with reloads?? */
3137 if (INSN_CODE (curr_insn) >= 0)
3138 fatal_insn ("unable to generate reloads for:", curr_insn);
3139 error_for_asm (curr_insn,
3140 "inconsistent operand constraints in an %<asm%>");
3141 /* Avoid further trouble with this insn. */
3142 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3143 lra_invalidate_insn_data (curr_insn);
3144 return true;
3147 /* If the best alternative is with operands 1 and 2 swapped, swap
3148 them. Update the operand numbers of any reloads already
3149 pushed. */
3151 if (goal_alt_swapped)
3153 if (lra_dump_file != NULL)
3154 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3155 INSN_UID (curr_insn));
3157 /* Swap the duplicates too. */
3158 swap_operands (commutative);
3159 change_p = true;
3162 #ifdef SECONDARY_MEMORY_NEEDED
3163 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3164 too conservatively. So we use the secondary memory only if there
3165 is no any alternative without reloads. */
3166 use_sec_mem_p = false;
3167 if (! alt_p)
3168 use_sec_mem_p = true;
3169 else if (sec_mem_p)
3171 for (i = 0; i < n_operands; i++)
3172 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3173 break;
3174 use_sec_mem_p = i < n_operands;
3177 if (use_sec_mem_p)
3179 rtx new_reg, src, dest, rld;
3180 enum machine_mode sec_mode, rld_mode;
3182 lra_assert (sec_mem_p);
3183 lra_assert (curr_static_id->operand[0].type == OP_OUT
3184 && curr_static_id->operand[1].type == OP_IN);
3185 dest = *curr_id->operand_loc[0];
3186 src = *curr_id->operand_loc[1];
3187 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3188 ? dest : src);
3189 rld_mode = GET_MODE (rld);
3190 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3191 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3192 #else
3193 sec_mode = rld_mode;
3194 #endif
3195 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3196 NO_REGS, "secondary");
3197 /* If the mode is changed, it should be wider. */
3198 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3199 if (sec_mode != rld_mode)
3201 /* If the target says specifically to use another mode for
3202 secondary memory moves we can not reuse the original
3203 insn. */
3204 after = emit_spill_move (false, new_reg, dest);
3205 lra_process_new_insns (curr_insn, NULL_RTX, after,
3206 "Inserting the sec. move");
3207 /* We may have non null BEFORE here (e.g. after address
3208 processing. */
3209 push_to_sequence (before);
3210 before = emit_spill_move (true, new_reg, src);
3211 emit_insn (before);
3212 before = get_insns ();
3213 end_sequence ();
3214 lra_process_new_insns (curr_insn, before, NULL_RTX, "Changing on");
3215 lra_set_insn_deleted (curr_insn);
3217 else if (dest == rld)
3219 *curr_id->operand_loc[0] = new_reg;
3220 after = emit_spill_move (false, new_reg, dest);
3221 lra_process_new_insns (curr_insn, NULL_RTX, after,
3222 "Inserting the sec. move");
3224 else
3226 *curr_id->operand_loc[1] = new_reg;
3227 /* See comments above. */
3228 push_to_sequence (before);
3229 before = emit_spill_move (true, new_reg, src);
3230 emit_insn (before);
3231 before = get_insns ();
3232 end_sequence ();
3233 lra_process_new_insns (curr_insn, before, NULL_RTX,
3234 "Inserting the sec. move");
3236 lra_update_insn_regno_info (curr_insn);
3237 return true;
3239 #endif
3241 lra_assert (goal_alt_number >= 0);
3242 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3244 if (lra_dump_file != NULL)
3246 const char *p;
3248 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3249 goal_alt_number, INSN_UID (curr_insn));
3250 for (i = 0; i < n_operands; i++)
3252 p = (curr_static_id->operand_alternative
3253 [goal_alt_number * n_operands + i].constraint);
3254 if (*p == '\0')
3255 continue;
3256 fprintf (lra_dump_file, " (%d) ", i);
3257 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3258 fputc (*p, lra_dump_file);
3260 if (INSN_CODE (curr_insn) >= 0
3261 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3262 fprintf (lra_dump_file, " {%s}", p);
3263 fprintf (lra_dump_file, "\n");
3266 /* Right now, for any pair of operands I and J that are required to
3267 match, with J < I, goal_alt_matches[I] is J. Add I to
3268 goal_alt_matched[J]. */
3270 for (i = 0; i < n_operands; i++)
3271 if ((j = goal_alt_matches[i]) >= 0)
3273 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3275 /* We allow matching one output operand and several input
3276 operands. */
3277 lra_assert (k == 0
3278 || (curr_static_id->operand[j].type == OP_OUT
3279 && curr_static_id->operand[i].type == OP_IN
3280 && (curr_static_id->operand
3281 [goal_alt_matched[j][0]].type == OP_IN)));
3282 goal_alt_matched[j][k] = i;
3283 goal_alt_matched[j][k + 1] = -1;
3286 for (i = 0; i < n_operands; i++)
3287 goal_alt_win[i] |= goal_alt_match_win[i];
3289 /* Any constants that aren't allowed and can't be reloaded into
3290 registers are here changed into memory references. */
3291 for (i = 0; i < n_operands; i++)
3292 if (goal_alt_win[i])
3294 int regno;
3295 enum reg_class new_class;
3296 rtx reg = *curr_id->operand_loc[i];
3298 if (GET_CODE (reg) == SUBREG)
3299 reg = SUBREG_REG (reg);
3301 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3303 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3305 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3307 lra_assert (ok_p);
3308 lra_change_class (regno, new_class, " Change to", true);
3312 else
3314 const char *constraint;
3315 char c;
3316 rtx op = *curr_id->operand_loc[i];
3317 rtx subreg = NULL_RTX;
3318 enum machine_mode mode = curr_operand_mode[i];
3320 if (GET_CODE (op) == SUBREG)
3322 subreg = op;
3323 op = SUBREG_REG (op);
3324 mode = GET_MODE (op);
3327 if (CONST_POOL_OK_P (mode, op)
3328 && ((targetm.preferred_reload_class
3329 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3330 || no_input_reloads_p))
3332 rtx tem = force_const_mem (mode, op);
3334 change_p = true;
3335 if (subreg != NULL_RTX)
3336 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3338 *curr_id->operand_loc[i] = tem;
3339 lra_update_dup (curr_id, i);
3340 process_address (i, &before, &after);
3342 /* If the alternative accepts constant pool refs directly
3343 there will be no reload needed at all. */
3344 if (subreg != NULL_RTX)
3345 continue;
3346 /* Skip alternatives before the one requested. */
3347 constraint = (curr_static_id->operand_alternative
3348 [goal_alt_number * n_operands + i].constraint);
3349 for (;
3350 (c = *constraint) && c != ',' && c != '#';
3351 constraint += CONSTRAINT_LEN (c, constraint))
3353 if (c == TARGET_MEM_CONSTRAINT || c == 'o')
3354 break;
3355 #ifdef EXTRA_CONSTRAINT_STR
3356 if (EXTRA_MEMORY_CONSTRAINT (c, constraint)
3357 && EXTRA_CONSTRAINT_STR (tem, c, constraint))
3358 break;
3359 #endif
3361 if (c == '\0' || c == ',' || c == '#')
3362 continue;
3364 goal_alt_win[i] = true;
3368 for (i = 0; i < n_operands; i++)
3370 int regno;
3371 bool optional_p = false;
3372 rtx old, new_reg;
3373 rtx op = *curr_id->operand_loc[i];
3375 if (goal_alt_win[i])
3377 if (goal_alt[i] == NO_REGS
3378 && REG_P (op)
3379 /* When we assign NO_REGS it means that we will not
3380 assign a hard register to the scratch pseudo by
3381 assigment pass and the scratch pseudo will be
3382 spilled. Spilled scratch pseudos are transformed
3383 back to scratches at the LRA end. */
3384 && lra_former_scratch_operand_p (curr_insn, i))
3386 int regno = REGNO (op);
3387 lra_change_class (regno, NO_REGS, " Change to", true);
3388 if (lra_get_regno_hard_regno (regno) >= 0)
3389 /* We don't have to mark all insn affected by the
3390 spilled pseudo as there is only one such insn, the
3391 current one. */
3392 reg_renumber[regno] = -1;
3394 /* We can do an optional reload. If the pseudo got a hard
3395 reg, we might improve the code through inheritance. If
3396 it does not get a hard register we coalesce memory/memory
3397 moves later. Ignore move insns to avoid cycling. */
3398 if (! lra_simple_p
3399 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3400 && goal_alt[i] != NO_REGS && REG_P (op)
3401 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3402 && regno < new_regno_start
3403 && ! lra_former_scratch_p (regno)
3404 && reg_renumber[regno] < 0
3405 && (curr_insn_set == NULL_RTX
3406 || !((REG_P (SET_SRC (curr_insn_set))
3407 || MEM_P (SET_SRC (curr_insn_set))
3408 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3409 && (REG_P (SET_DEST (curr_insn_set))
3410 || MEM_P (SET_DEST (curr_insn_set))
3411 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3412 optional_p = true;
3413 else
3414 continue;
3417 /* Operands that match previous ones have already been handled. */
3418 if (goal_alt_matches[i] >= 0)
3419 continue;
3421 /* We should not have an operand with a non-offsettable address
3422 appearing where an offsettable address will do. It also may
3423 be a case when the address should be special in other words
3424 not a general one (e.g. it needs no index reg). */
3425 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3427 enum reg_class rclass;
3428 rtx *loc = &XEXP (op, 0);
3429 enum rtx_code code = GET_CODE (*loc);
3431 push_to_sequence (before);
3432 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3433 MEM, SCRATCH);
3434 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3435 new_reg = emit_inc (rclass, *loc, *loc,
3436 /* This value does not matter for MODIFY. */
3437 GET_MODE_SIZE (GET_MODE (op)));
3438 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass,
3439 "offsetable address", &new_reg))
3440 lra_emit_move (new_reg, *loc);
3441 before = get_insns ();
3442 end_sequence ();
3443 *loc = new_reg;
3444 lra_update_dup (curr_id, i);
3446 else if (goal_alt_matched[i][0] == -1)
3448 enum machine_mode mode;
3449 rtx reg, *loc;
3450 int hard_regno, byte;
3451 enum op_type type = curr_static_id->operand[i].type;
3453 loc = curr_id->operand_loc[i];
3454 mode = curr_operand_mode[i];
3455 if (GET_CODE (*loc) == SUBREG)
3457 reg = SUBREG_REG (*loc);
3458 byte = SUBREG_BYTE (*loc);
3459 if (REG_P (reg)
3460 /* Strict_low_part requires reload the register not
3461 the sub-register. */
3462 && (curr_static_id->operand[i].strict_low
3463 || (GET_MODE_SIZE (mode)
3464 <= GET_MODE_SIZE (GET_MODE (reg))
3465 && (hard_regno
3466 = get_try_hard_regno (REGNO (reg))) >= 0
3467 && (simplify_subreg_regno
3468 (hard_regno,
3469 GET_MODE (reg), byte, mode) < 0)
3470 && (goal_alt[i] == NO_REGS
3471 || (simplify_subreg_regno
3472 (ira_class_hard_regs[goal_alt[i]][0],
3473 GET_MODE (reg), byte, mode) >= 0)))))
3475 loc = &SUBREG_REG (*loc);
3476 mode = GET_MODE (*loc);
3479 old = *loc;
3480 if (get_reload_reg (type, mode, old, goal_alt[i], "", &new_reg)
3481 && type != OP_OUT)
3483 push_to_sequence (before);
3484 lra_emit_move (new_reg, old);
3485 before = get_insns ();
3486 end_sequence ();
3488 *loc = new_reg;
3489 if (type != OP_IN
3490 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3492 start_sequence ();
3493 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3494 emit_insn (after);
3495 after = get_insns ();
3496 end_sequence ();
3497 *loc = new_reg;
3499 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3500 if (goal_alt_dont_inherit_ops[j] == i)
3502 lra_set_regno_unique_value (REGNO (new_reg));
3503 break;
3505 lra_update_dup (curr_id, i);
3507 else if (curr_static_id->operand[i].type == OP_IN
3508 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3509 == OP_OUT))
3511 /* generate reloads for input and matched outputs. */
3512 match_inputs[0] = i;
3513 match_inputs[1] = -1;
3514 match_reload (goal_alt_matched[i][0], match_inputs,
3515 goal_alt[i], &before, &after);
3517 else if (curr_static_id->operand[i].type == OP_OUT
3518 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3519 == OP_IN))
3520 /* Generate reloads for output and matched inputs. */
3521 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3522 else if (curr_static_id->operand[i].type == OP_IN
3523 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3524 == OP_IN))
3526 /* Generate reloads for matched inputs. */
3527 match_inputs[0] = i;
3528 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3529 match_inputs[j + 1] = k;
3530 match_inputs[j + 1] = -1;
3531 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3533 else
3534 /* We must generate code in any case when function
3535 process_alt_operands decides that it is possible. */
3536 gcc_unreachable ();
3537 if (optional_p)
3539 lra_assert (REG_P (op));
3540 regno = REGNO (op);
3541 op = *curr_id->operand_loc[i]; /* Substitution. */
3542 if (GET_CODE (op) == SUBREG)
3543 op = SUBREG_REG (op);
3544 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3545 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3546 lra_reg_info[REGNO (op)].restore_regno = regno;
3547 if (lra_dump_file != NULL)
3548 fprintf (lra_dump_file,
3549 " Making reload reg %d for reg %d optional\n",
3550 REGNO (op), regno);
3553 if (before != NULL_RTX || after != NULL_RTX
3554 || max_regno_before != max_reg_num ())
3555 change_p = true;
3556 if (change_p)
3558 lra_update_operator_dups (curr_id);
3559 /* Something changes -- process the insn. */
3560 lra_update_insn_regno_info (curr_insn);
3562 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3563 return change_p;
3566 /* Return true if X is in LIST. */
3567 static bool
3568 in_list_p (rtx x, rtx list)
3570 for (; list != NULL_RTX; list = XEXP (list, 1))
3571 if (XEXP (list, 0) == x)
3572 return true;
3573 return false;
3576 /* Return true if X contains an allocatable hard register (if
3577 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3578 static bool
3579 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3581 int i, j;
3582 const char *fmt;
3583 enum rtx_code code;
3585 code = GET_CODE (x);
3586 if (REG_P (x))
3588 int regno = REGNO (x);
3589 HARD_REG_SET alloc_regs;
3591 if (hard_reg_p)
3593 if (regno >= FIRST_PSEUDO_REGISTER)
3594 regno = lra_get_regno_hard_regno (regno);
3595 if (regno < 0)
3596 return false;
3597 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3598 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3600 else
3602 if (regno < FIRST_PSEUDO_REGISTER)
3603 return false;
3604 if (! spilled_p)
3605 return true;
3606 return lra_get_regno_hard_regno (regno) < 0;
3609 fmt = GET_RTX_FORMAT (code);
3610 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3612 if (fmt[i] == 'e')
3614 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3615 return true;
3617 else if (fmt[i] == 'E')
3619 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3620 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3621 return true;
3624 return false;
3627 /* Process all regs in location *LOC and change them on equivalent
3628 substitution. Return true if any change was done. */
3629 static bool
3630 loc_equivalence_change_p (rtx *loc)
3632 rtx subst, reg, x = *loc;
3633 bool result = false;
3634 enum rtx_code code = GET_CODE (x);
3635 const char *fmt;
3636 int i, j;
3638 if (code == SUBREG)
3640 reg = SUBREG_REG (x);
3641 if ((subst = get_equiv_substitution (reg)) != reg
3642 && GET_MODE (subst) == VOIDmode)
3644 /* We cannot reload debug location. Simplify subreg here
3645 while we know the inner mode. */
3646 *loc = simplify_gen_subreg (GET_MODE (x), subst,
3647 GET_MODE (reg), SUBREG_BYTE (x));
3648 return true;
3651 if (code == REG && (subst = get_equiv_substitution (x)) != x)
3653 *loc = subst;
3654 return true;
3657 /* Scan all the operand sub-expressions. */
3658 fmt = GET_RTX_FORMAT (code);
3659 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3661 if (fmt[i] == 'e')
3662 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
3663 else if (fmt[i] == 'E')
3664 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3665 result
3666 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
3668 return result;
3671 /* Similar to loc_equivalence_change_p, but for use as
3672 simplify_replace_fn_rtx callback. */
3673 static rtx
3674 loc_equivalence_callback (rtx loc, const_rtx, void *)
3676 if (!REG_P (loc))
3677 return NULL_RTX;
3679 rtx subst = get_equiv_substitution (loc);
3680 if (subst != loc)
3681 return subst;
3683 return NULL_RTX;
3686 /* Maximum number of generated reload insns per an insn. It is for
3687 preventing this pass cycling in a bug case. */
3688 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
3690 /* The current iteration number of this LRA pass. */
3691 int lra_constraint_iter;
3693 /* The current iteration number of this LRA pass after the last spill
3694 pass. */
3695 int lra_constraint_iter_after_spill;
3697 /* True if we substituted equiv which needs checking register
3698 allocation correctness because the equivalent value contains
3699 allocatable hard registers or when we restore multi-register
3700 pseudo. */
3701 bool lra_risky_transformations_p;
3703 /* Return true if REGNO is referenced in more than one block. */
3704 static bool
3705 multi_block_pseudo_p (int regno)
3707 basic_block bb = NULL;
3708 unsigned int uid;
3709 bitmap_iterator bi;
3711 if (regno < FIRST_PSEUDO_REGISTER)
3712 return false;
3714 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
3715 if (bb == NULL)
3716 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
3717 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
3718 return true;
3719 return false;
3722 /* Return true if LIST contains a deleted insn. */
3723 static bool
3724 contains_deleted_insn_p (rtx list)
3726 for (; list != NULL_RTX; list = XEXP (list, 1))
3727 if (NOTE_P (XEXP (list, 0))
3728 && NOTE_KIND (XEXP (list, 0)) == NOTE_INSN_DELETED)
3729 return true;
3730 return false;
3733 /* Return true if X contains a pseudo dying in INSN. */
3734 static bool
3735 dead_pseudo_p (rtx x, rtx insn)
3737 int i, j;
3738 const char *fmt;
3739 enum rtx_code code;
3741 if (REG_P (x))
3742 return (insn != NULL_RTX
3743 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
3744 code = GET_CODE (x);
3745 fmt = GET_RTX_FORMAT (code);
3746 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3748 if (fmt[i] == 'e')
3750 if (dead_pseudo_p (XEXP (x, i), insn))
3751 return true;
3753 else if (fmt[i] == 'E')
3755 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3756 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
3757 return true;
3760 return false;
3763 /* Return true if INSN contains a dying pseudo in INSN right hand
3764 side. */
3765 static bool
3766 insn_rhs_dead_pseudo_p (rtx insn)
3768 rtx set = single_set (insn);
3770 gcc_assert (set != NULL);
3771 return dead_pseudo_p (SET_SRC (set), insn);
3774 /* Return true if any init insn of REGNO contains a dying pseudo in
3775 insn right hand side. */
3776 static bool
3777 init_insn_rhs_dead_pseudo_p (int regno)
3779 rtx insns = ira_reg_equiv[regno].init_insns;
3781 if (insns == NULL)
3782 return false;
3783 if (INSN_P (insns))
3784 return insn_rhs_dead_pseudo_p (insns);
3785 for (; insns != NULL_RTX; insns = XEXP (insns, 1))
3786 if (insn_rhs_dead_pseudo_p (XEXP (insns, 0)))
3787 return true;
3788 return false;
3791 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
3792 reverse only if we have one init insn with given REGNO as a
3793 source. */
3794 static bool
3795 reverse_equiv_p (int regno)
3797 rtx insns, set;
3799 if ((insns = ira_reg_equiv[regno].init_insns) == NULL_RTX)
3800 return false;
3801 if (! INSN_P (XEXP (insns, 0))
3802 || XEXP (insns, 1) != NULL_RTX)
3803 return false;
3804 if ((set = single_set (XEXP (insns, 0))) == NULL_RTX)
3805 return false;
3806 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
3809 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
3810 call this function only for non-reverse equivalence. */
3811 static bool
3812 contains_reloaded_insn_p (int regno)
3814 rtx set;
3815 rtx list = ira_reg_equiv[regno].init_insns;
3817 for (; list != NULL_RTX; list = XEXP (list, 1))
3818 if ((set = single_set (XEXP (list, 0))) == NULL_RTX
3819 || ! REG_P (SET_DEST (set))
3820 || (int) REGNO (SET_DEST (set)) != regno)
3821 return true;
3822 return false;
3825 /* Entry function of LRA constraint pass. Return true if the
3826 constraint pass did change the code. */
3827 bool
3828 lra_constraints (bool first_p)
3830 bool changed_p;
3831 int i, hard_regno, new_insns_num;
3832 unsigned int min_len, new_min_len, uid;
3833 rtx set, x, reg, dest_reg;
3834 basic_block last_bb;
3835 bitmap_head equiv_insn_bitmap;
3836 bitmap_iterator bi;
3838 lra_constraint_iter++;
3839 if (lra_dump_file != NULL)
3840 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
3841 lra_constraint_iter);
3842 lra_constraint_iter_after_spill++;
3843 if (lra_constraint_iter_after_spill > LRA_MAX_CONSTRAINT_ITERATION_NUMBER)
3844 internal_error
3845 ("Maximum number of LRA constraint passes is achieved (%d)\n",
3846 LRA_MAX_CONSTRAINT_ITERATION_NUMBER);
3847 changed_p = false;
3848 lra_risky_transformations_p = false;
3849 new_insn_uid_start = get_max_uid ();
3850 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
3851 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
3852 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3853 if (lra_reg_info[i].nrefs != 0)
3855 ira_reg_equiv[i].profitable_p = true;
3856 reg = regno_reg_rtx[i];
3857 if ((hard_regno = lra_get_regno_hard_regno (i)) >= 0)
3859 int j, nregs;
3861 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
3862 for (j = 0; j < nregs; j++)
3863 df_set_regs_ever_live (hard_regno + j, true);
3865 else if ((x = get_equiv_substitution (reg)) != reg)
3867 bool pseudo_p = contains_reg_p (x, false, false);
3869 /* After RTL transformation, we can not guarantee that
3870 pseudo in the substitution was not reloaded which might
3871 make equivalence invalid. For example, in reverse
3872 equiv of p0
3874 p0 <- ...
3876 equiv_mem <- p0
3878 the memory address register was reloaded before the 2nd
3879 insn. */
3880 if ((! first_p && pseudo_p)
3881 /* We don't use DF for compilation speed sake. So it
3882 is problematic to update live info when we use an
3883 equivalence containing pseudos in more than one
3884 BB. */
3885 || (pseudo_p && multi_block_pseudo_p (i))
3886 /* If an init insn was deleted for some reason, cancel
3887 the equiv. We could update the equiv insns after
3888 transformations including an equiv insn deletion
3889 but it is not worthy as such cases are extremely
3890 rare. */
3891 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
3892 /* If it is not a reverse equivalence, we check that a
3893 pseudo in rhs of the init insn is not dying in the
3894 insn. Otherwise, the live info at the beginning of
3895 the corresponding BB might be wrong after we
3896 removed the insn. When the equiv can be a
3897 constant, the right hand side of the init insn can
3898 be a pseudo. */
3899 || (! reverse_equiv_p (i)
3900 && (init_insn_rhs_dead_pseudo_p (i)
3901 /* If we reloaded the pseudo in an equivalence
3902 init insn, we can not remove the equiv init
3903 insns and the init insns might write into
3904 const memory in this case. */
3905 || contains_reloaded_insn_p (i)))
3906 /* Prevent access beyond equivalent memory for
3907 paradoxical subregs. */
3908 || (MEM_P (x)
3909 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
3910 > GET_MODE_SIZE (GET_MODE (x)))))
3911 ira_reg_equiv[i].defined_p = false;
3912 if (contains_reg_p (x, false, true))
3913 ira_reg_equiv[i].profitable_p = false;
3914 if (get_equiv_substitution (reg) != reg)
3915 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
3918 /* We should add all insns containing pseudos which should be
3919 substituted by their equivalences. */
3920 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
3921 lra_push_insn_by_uid (uid);
3922 lra_eliminate (false);
3923 min_len = lra_insn_stack_length ();
3924 new_insns_num = 0;
3925 last_bb = NULL;
3926 changed_p = false;
3927 while ((new_min_len = lra_insn_stack_length ()) != 0)
3929 curr_insn = lra_pop_insn ();
3930 --new_min_len;
3931 curr_bb = BLOCK_FOR_INSN (curr_insn);
3932 if (curr_bb != last_bb)
3934 last_bb = curr_bb;
3935 bb_reload_num = lra_curr_reload_num;
3937 if (min_len > new_min_len)
3939 min_len = new_min_len;
3940 new_insns_num = 0;
3942 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
3943 internal_error
3944 ("Max. number of generated reload insns per insn is achieved (%d)\n",
3945 MAX_RELOAD_INSNS_NUMBER);
3946 new_insns_num++;
3947 if (DEBUG_INSN_P (curr_insn))
3949 /* We need to check equivalence in debug insn and change
3950 pseudo to the equivalent value if necessary. */
3951 curr_id = lra_get_insn_recog_data (curr_insn);
3952 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
3954 rtx old = *curr_id->operand_loc[0];
3955 *curr_id->operand_loc[0]
3956 = simplify_replace_fn_rtx (old, NULL_RTX,
3957 loc_equivalence_callback, NULL);
3958 if (old != *curr_id->operand_loc[0])
3960 lra_update_insn_regno_info (curr_insn);
3961 changed_p = true;
3965 else if (INSN_P (curr_insn))
3967 if ((set = single_set (curr_insn)) != NULL_RTX)
3969 dest_reg = SET_DEST (set);
3970 /* The equivalence pseudo could be set up as SUBREG in a
3971 case when it is a call restore insn in a mode
3972 different from the pseudo mode. */
3973 if (GET_CODE (dest_reg) == SUBREG)
3974 dest_reg = SUBREG_REG (dest_reg);
3975 if ((REG_P (dest_reg)
3976 && (x = get_equiv_substitution (dest_reg)) != dest_reg
3977 /* Remove insns which set up a pseudo whose value
3978 can not be changed. Such insns might be not in
3979 init_insns because we don't update equiv data
3980 during insn transformations.
3982 As an example, let suppose that a pseudo got
3983 hard register and on the 1st pass was not
3984 changed to equivalent constant. We generate an
3985 additional insn setting up the pseudo because of
3986 secondary memory movement. Then the pseudo is
3987 spilled and we use the equiv constant. In this
3988 case we should remove the additional insn and
3989 this insn is not init_insns list. */
3990 && (! MEM_P (x) || MEM_READONLY_P (x)
3991 /* Check that this is actually an insn setting
3992 up the equivalence. */
3993 || in_list_p (curr_insn,
3994 ira_reg_equiv
3995 [REGNO (dest_reg)].init_insns)))
3996 || (((x = get_equiv_substitution (SET_SRC (set)))
3997 != SET_SRC (set))
3998 && in_list_p (curr_insn,
3999 ira_reg_equiv
4000 [REGNO (SET_SRC (set))].init_insns)))
4002 /* This is equiv init insn of pseudo which did not get a
4003 hard register -- remove the insn. */
4004 if (lra_dump_file != NULL)
4006 fprintf (lra_dump_file,
4007 " Removing equiv init insn %i (freq=%d)\n",
4008 INSN_UID (curr_insn),
4009 BLOCK_FOR_INSN (curr_insn)->frequency);
4010 dump_insn_slim (lra_dump_file, curr_insn);
4012 if (contains_reg_p (x, true, false))
4013 lra_risky_transformations_p = true;
4014 lra_set_insn_deleted (curr_insn);
4015 continue;
4018 curr_id = lra_get_insn_recog_data (curr_insn);
4019 curr_static_id = curr_id->insn_static_data;
4020 init_curr_insn_input_reloads ();
4021 init_curr_operand_mode ();
4022 if (curr_insn_transform ())
4023 changed_p = true;
4024 /* Check non-transformed insns too for equiv change as USE
4025 or CLOBBER don't need reloads but can contain pseudos
4026 being changed on their equivalences. */
4027 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4028 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4030 lra_update_insn_regno_info (curr_insn);
4031 changed_p = true;
4035 bitmap_clear (&equiv_insn_bitmap);
4036 /* If we used a new hard regno, changed_p should be true because the
4037 hard reg is assigned to a new pseudo. */
4038 #ifdef ENABLE_CHECKING
4039 if (! changed_p)
4041 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4042 if (lra_reg_info[i].nrefs != 0
4043 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4045 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4047 for (j = 0; j < nregs; j++)
4048 lra_assert (df_regs_ever_live_p (hard_regno + j));
4051 #endif
4052 return changed_p;
4055 /* Initiate the LRA constraint pass. It is done once per
4056 function. */
4057 void
4058 lra_constraints_init (void)
4062 /* Finalize the LRA constraint pass. It is done once per
4063 function. */
4064 void
4065 lra_constraints_finish (void)
4071 /* This page contains code to do inheritance/split
4072 transformations. */
4074 /* Number of reloads passed so far in current EBB. */
4075 static int reloads_num;
4077 /* Number of calls passed so far in current EBB. */
4078 static int calls_num;
4080 /* Current reload pseudo check for validity of elements in
4081 USAGE_INSNS. */
4082 static int curr_usage_insns_check;
4084 /* Info about last usage of registers in EBB to do inheritance/split
4085 transformation. Inheritance transformation is done from a spilled
4086 pseudo and split transformations from a hard register or a pseudo
4087 assigned to a hard register. */
4088 struct usage_insns
4090 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4091 value INSNS is valid. The insns is chain of optional debug insns
4092 and a finishing non-debug insn using the corresponding reg. The
4093 value is also used to mark the registers which are set up in the
4094 current insn. The negated insn uid is used for this. */
4095 int check;
4096 /* Value of global reloads_num at the last insn in INSNS. */
4097 int reloads_num;
4098 /* Value of global reloads_nums at the last insn in INSNS. */
4099 int calls_num;
4100 /* It can be true only for splitting. And it means that the restore
4101 insn should be put after insn given by the following member. */
4102 bool after_p;
4103 /* Next insns in the current EBB which use the original reg and the
4104 original reg value is not changed between the current insn and
4105 the next insns. In order words, e.g. for inheritance, if we need
4106 to use the original reg value again in the next insns we can try
4107 to use the value in a hard register from a reload insn of the
4108 current insn. */
4109 rtx insns;
4112 /* Map: regno -> corresponding pseudo usage insns. */
4113 static struct usage_insns *usage_insns;
4115 static void
4116 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4118 usage_insns[regno].check = curr_usage_insns_check;
4119 usage_insns[regno].insns = insn;
4120 usage_insns[regno].reloads_num = reloads_num;
4121 usage_insns[regno].calls_num = calls_num;
4122 usage_insns[regno].after_p = after_p;
4125 /* The function is used to form list REGNO usages which consists of
4126 optional debug insns finished by a non-debug insn using REGNO.
4127 RELOADS_NUM is current number of reload insns processed so far. */
4128 static void
4129 add_next_usage_insn (int regno, rtx insn, int reloads_num)
4131 rtx next_usage_insns;
4133 if (usage_insns[regno].check == curr_usage_insns_check
4134 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4135 && DEBUG_INSN_P (insn))
4137 /* Check that we did not add the debug insn yet. */
4138 if (next_usage_insns != insn
4139 && (GET_CODE (next_usage_insns) != INSN_LIST
4140 || XEXP (next_usage_insns, 0) != insn))
4141 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4142 next_usage_insns);
4144 else if (NONDEBUG_INSN_P (insn))
4145 setup_next_usage_insn (regno, insn, reloads_num, false);
4146 else
4147 usage_insns[regno].check = 0;
4150 /* Replace all references to register OLD_REGNO in *LOC with pseudo
4151 register NEW_REG. Return true if any change was made. */
4152 static bool
4153 substitute_pseudo (rtx *loc, int old_regno, rtx new_reg)
4155 rtx x = *loc;
4156 bool result = false;
4157 enum rtx_code code;
4158 const char *fmt;
4159 int i, j;
4161 if (x == NULL_RTX)
4162 return false;
4164 code = GET_CODE (x);
4165 if (code == REG && (int) REGNO (x) == old_regno)
4167 enum machine_mode mode = GET_MODE (*loc);
4168 enum machine_mode inner_mode = GET_MODE (new_reg);
4170 if (mode != inner_mode)
4172 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
4173 || ! SCALAR_INT_MODE_P (inner_mode))
4174 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
4175 else
4176 new_reg = gen_lowpart_SUBREG (mode, new_reg);
4178 *loc = new_reg;
4179 return true;
4182 /* Scan all the operand sub-expressions. */
4183 fmt = GET_RTX_FORMAT (code);
4184 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4186 if (fmt[i] == 'e')
4188 if (substitute_pseudo (&XEXP (x, i), old_regno, new_reg))
4189 result = true;
4191 else if (fmt[i] == 'E')
4193 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4194 if (substitute_pseudo (&XVECEXP (x, i, j), old_regno, new_reg))
4195 result = true;
4198 return result;
4201 /* Return first non-debug insn in list USAGE_INSNS. */
4202 static rtx
4203 skip_usage_debug_insns (rtx usage_insns)
4205 rtx insn;
4207 /* Skip debug insns. */
4208 for (insn = usage_insns;
4209 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4210 insn = XEXP (insn, 1))
4212 return insn;
4215 /* Return true if we need secondary memory moves for insn in
4216 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4217 into the insn. */
4218 static bool
4219 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4220 rtx usage_insns ATTRIBUTE_UNUSED)
4222 #ifndef SECONDARY_MEMORY_NEEDED
4223 return false;
4224 #else
4225 rtx insn, set, dest;
4226 enum reg_class cl;
4228 if (inher_cl == ALL_REGS
4229 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4230 return false;
4231 lra_assert (INSN_P (insn));
4232 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4233 return false;
4234 dest = SET_DEST (set);
4235 if (! REG_P (dest))
4236 return false;
4237 lra_assert (inher_cl != NO_REGS);
4238 cl = get_reg_class (REGNO (dest));
4239 return (cl != NO_REGS && cl != ALL_REGS
4240 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4241 #endif
4244 /* Registers involved in inheritance/split in the current EBB
4245 (inheritance/split pseudos and original registers). */
4246 static bitmap_head check_only_regs;
4248 /* Do inheritance transformations for insn INSN, which defines (if
4249 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4250 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4251 form as the "insns" field of usage_insns. Return true if we
4252 succeed in such transformation.
4254 The transformations look like:
4256 p <- ... i <- ...
4257 ... p <- i (new insn)
4258 ... =>
4259 <- ... p ... <- ... i ...
4261 ... i <- p (new insn)
4262 <- ... p ... <- ... i ...
4263 ... =>
4264 <- ... p ... <- ... i ...
4265 where p is a spilled original pseudo and i is a new inheritance pseudo.
4268 The inheritance pseudo has the smallest class of two classes CL and
4269 class of ORIGINAL REGNO. */
4270 static bool
4271 inherit_reload_reg (bool def_p, int original_regno,
4272 enum reg_class cl, rtx insn, rtx next_usage_insns)
4274 enum reg_class rclass = lra_get_allocno_class (original_regno);
4275 rtx original_reg = regno_reg_rtx[original_regno];
4276 rtx new_reg, new_insns, usage_insn;
4278 lra_assert (! usage_insns[original_regno].after_p);
4279 if (lra_dump_file != NULL)
4280 fprintf (lra_dump_file,
4281 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4282 if (! ira_reg_classes_intersect_p[cl][rclass])
4284 if (lra_dump_file != NULL)
4286 fprintf (lra_dump_file,
4287 " Rejecting inheritance for %d "
4288 "because of disjoint classes %s and %s\n",
4289 original_regno, reg_class_names[cl],
4290 reg_class_names[rclass]);
4291 fprintf (lra_dump_file,
4292 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4294 return false;
4296 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4297 /* We don't use a subset of two classes because it can be
4298 NO_REGS. This transformation is still profitable in most
4299 cases even if the classes are not intersected as register
4300 move is probably cheaper than a memory load. */
4301 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4303 if (lra_dump_file != NULL)
4304 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4305 reg_class_names[cl], reg_class_names[rclass]);
4307 rclass = cl;
4309 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4311 /* Reject inheritance resulting in secondary memory moves.
4312 Otherwise, there is a danger in LRA cycling. Also such
4313 transformation will be unprofitable. */
4314 if (lra_dump_file != NULL)
4316 rtx insn = skip_usage_debug_insns (next_usage_insns);
4317 rtx set = single_set (insn);
4319 lra_assert (set != NULL_RTX);
4321 rtx dest = SET_DEST (set);
4323 lra_assert (REG_P (dest));
4324 fprintf (lra_dump_file,
4325 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4326 "as secondary mem is needed\n",
4327 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4328 original_regno, reg_class_names[rclass]);
4329 fprintf (lra_dump_file,
4330 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4332 return false;
4334 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4335 rclass, "inheritance");
4336 start_sequence ();
4337 if (def_p)
4338 emit_move_insn (original_reg, new_reg);
4339 else
4340 emit_move_insn (new_reg, original_reg);
4341 new_insns = get_insns ();
4342 end_sequence ();
4343 if (NEXT_INSN (new_insns) != NULL_RTX)
4345 if (lra_dump_file != NULL)
4347 fprintf (lra_dump_file,
4348 " Rejecting inheritance %d->%d "
4349 "as it results in 2 or more insns:\n",
4350 original_regno, REGNO (new_reg));
4351 dump_rtl_slim (lra_dump_file, new_insns, NULL_RTX, -1, 0);
4352 fprintf (lra_dump_file,
4353 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4355 return false;
4357 substitute_pseudo (&insn, original_regno, new_reg);
4358 lra_update_insn_regno_info (insn);
4359 if (! def_p)
4360 /* We now have a new usage insn for original regno. */
4361 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4362 if (lra_dump_file != NULL)
4363 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4364 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4365 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4366 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4367 bitmap_set_bit (&check_only_regs, original_regno);
4368 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4369 if (def_p)
4370 lra_process_new_insns (insn, NULL_RTX, new_insns,
4371 "Add original<-inheritance");
4372 else
4373 lra_process_new_insns (insn, new_insns, NULL_RTX,
4374 "Add inheritance<-original");
4375 while (next_usage_insns != NULL_RTX)
4377 if (GET_CODE (next_usage_insns) != INSN_LIST)
4379 usage_insn = next_usage_insns;
4380 lra_assert (NONDEBUG_INSN_P (usage_insn));
4381 next_usage_insns = NULL;
4383 else
4385 usage_insn = XEXP (next_usage_insns, 0);
4386 lra_assert (DEBUG_INSN_P (usage_insn));
4387 next_usage_insns = XEXP (next_usage_insns, 1);
4389 substitute_pseudo (&usage_insn, original_regno, new_reg);
4390 lra_update_insn_regno_info (usage_insn);
4391 if (lra_dump_file != NULL)
4393 fprintf (lra_dump_file,
4394 " Inheritance reuse change %d->%d (bb%d):\n",
4395 original_regno, REGNO (new_reg),
4396 BLOCK_FOR_INSN (usage_insn)->index);
4397 dump_insn_slim (lra_dump_file, usage_insn);
4400 if (lra_dump_file != NULL)
4401 fprintf (lra_dump_file,
4402 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4403 return true;
4406 /* Return true if we need a caller save/restore for pseudo REGNO which
4407 was assigned to a hard register. */
4408 static inline bool
4409 need_for_call_save_p (int regno)
4411 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4412 return (usage_insns[regno].calls_num < calls_num
4413 && (overlaps_hard_reg_set_p
4414 (call_used_reg_set,
4415 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4416 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4417 PSEUDO_REGNO_MODE (regno))));
4420 /* Global registers occurring in the current EBB. */
4421 static bitmap_head ebb_global_regs;
4423 /* Return true if we need a split for hard register REGNO or pseudo
4424 REGNO which was assigned to a hard register.
4425 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4426 used for reloads since the EBB end. It is an approximation of the
4427 used hard registers in the split range. The exact value would
4428 require expensive calculations. If we were aggressive with
4429 splitting because of the approximation, the split pseudo will save
4430 the same hard register assignment and will be removed in the undo
4431 pass. We still need the approximation because too aggressive
4432 splitting would result in too inaccurate cost calculation in the
4433 assignment pass because of too many generated moves which will be
4434 probably removed in the undo pass. */
4435 static inline bool
4436 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4438 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4440 lra_assert (hard_regno >= 0);
4441 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4442 /* Don't split eliminable hard registers, otherwise we can
4443 split hard registers like hard frame pointer, which
4444 lives on BB start/end according to DF-infrastructure,
4445 when there is a pseudo assigned to the register and
4446 living in the same BB. */
4447 && (regno >= FIRST_PSEUDO_REGISTER
4448 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4449 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4450 /* Don't split call clobbered hard regs living through
4451 calls, otherwise we might have a check problem in the
4452 assign sub-pass as in the most cases (exception is a
4453 situation when lra_risky_transformations_p value is
4454 true) the assign pass assumes that all pseudos living
4455 through calls are assigned to call saved hard regs. */
4456 && (regno >= FIRST_PSEUDO_REGISTER
4457 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4458 || usage_insns[regno].calls_num == calls_num)
4459 /* We need at least 2 reloads to make pseudo splitting
4460 profitable. We should provide hard regno splitting in
4461 any case to solve 1st insn scheduling problem when
4462 moving hard register definition up might result in
4463 impossibility to find hard register for reload pseudo of
4464 small register class. */
4465 && (usage_insns[regno].reloads_num
4466 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 2) < reloads_num)
4467 && (regno < FIRST_PSEUDO_REGISTER
4468 /* For short living pseudos, spilling + inheritance can
4469 be considered a substitution for splitting.
4470 Therefore we do not splitting for local pseudos. It
4471 decreases also aggressiveness of splitting. The
4472 minimal number of references is chosen taking into
4473 account that for 2 references splitting has no sense
4474 as we can just spill the pseudo. */
4475 || (regno >= FIRST_PSEUDO_REGISTER
4476 && lra_reg_info[regno].nrefs > 3
4477 && bitmap_bit_p (&ebb_global_regs, regno))))
4478 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4481 /* Return class for the split pseudo created from original pseudo with
4482 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4483 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4484 results in no secondary memory movements. */
4485 static enum reg_class
4486 choose_split_class (enum reg_class allocno_class,
4487 int hard_regno ATTRIBUTE_UNUSED,
4488 enum machine_mode mode ATTRIBUTE_UNUSED)
4490 #ifndef SECONDARY_MEMORY_NEEDED
4491 return allocno_class;
4492 #else
4493 int i;
4494 enum reg_class cl, best_cl = NO_REGS;
4495 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4496 = REGNO_REG_CLASS (hard_regno);
4498 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4499 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4500 return allocno_class;
4501 for (i = 0;
4502 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4503 i++)
4504 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4505 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4506 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4507 && (best_cl == NO_REGS
4508 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4509 best_cl = cl;
4510 return best_cl;
4511 #endif
4514 /* Do split transformations for insn INSN, which defines or uses
4515 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4516 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4517 "insns" field of usage_insns.
4519 The transformations look like:
4521 p <- ... p <- ...
4522 ... s <- p (new insn -- save)
4523 ... =>
4524 ... p <- s (new insn -- restore)
4525 <- ... p ... <- ... p ...
4527 <- ... p ... <- ... p ...
4528 ... s <- p (new insn -- save)
4529 ... =>
4530 ... p <- s (new insn -- restore)
4531 <- ... p ... <- ... p ...
4533 where p is an original pseudo got a hard register or a hard
4534 register and s is a new split pseudo. The save is put before INSN
4535 if BEFORE_P is true. Return true if we succeed in such
4536 transformation. */
4537 static bool
4538 split_reg (bool before_p, int original_regno, rtx insn, rtx next_usage_insns)
4540 enum reg_class rclass;
4541 rtx original_reg;
4542 int hard_regno, nregs;
4543 rtx new_reg, save, restore, usage_insn;
4544 bool after_p;
4545 bool call_save_p;
4547 if (original_regno < FIRST_PSEUDO_REGISTER)
4549 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4550 hard_regno = original_regno;
4551 call_save_p = false;
4552 nregs = 1;
4554 else
4556 hard_regno = reg_renumber[original_regno];
4557 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4558 rclass = lra_get_allocno_class (original_regno);
4559 original_reg = regno_reg_rtx[original_regno];
4560 call_save_p = need_for_call_save_p (original_regno);
4562 original_reg = regno_reg_rtx[original_regno];
4563 lra_assert (hard_regno >= 0);
4564 if (lra_dump_file != NULL)
4565 fprintf (lra_dump_file,
4566 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4567 if (call_save_p)
4569 enum machine_mode mode = GET_MODE (original_reg);
4571 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4572 hard_regno_nregs[hard_regno][mode],
4573 mode);
4574 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4576 else
4578 rclass = choose_split_class (rclass, hard_regno,
4579 GET_MODE (original_reg));
4580 if (rclass == NO_REGS)
4582 if (lra_dump_file != NULL)
4584 fprintf (lra_dump_file,
4585 " Rejecting split of %d(%s): "
4586 "no good reg class for %d(%s)\n",
4587 original_regno,
4588 reg_class_names[lra_get_allocno_class (original_regno)],
4589 hard_regno,
4590 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4591 fprintf
4592 (lra_dump_file,
4593 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4595 return false;
4597 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4598 rclass, "split");
4599 reg_renumber[REGNO (new_reg)] = hard_regno;
4601 save = emit_spill_move (true, new_reg, original_reg);
4602 if (NEXT_INSN (save) != NULL_RTX)
4604 lra_assert (! call_save_p);
4605 if (lra_dump_file != NULL)
4607 fprintf
4608 (lra_dump_file,
4609 " Rejecting split %d->%d resulting in > 2 %s save insns:\n",
4610 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4611 dump_rtl_slim (lra_dump_file, save, NULL_RTX, -1, 0);
4612 fprintf (lra_dump_file,
4613 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4615 return false;
4617 restore = emit_spill_move (false, new_reg, original_reg);
4618 if (NEXT_INSN (restore) != NULL_RTX)
4620 lra_assert (! call_save_p);
4621 if (lra_dump_file != NULL)
4623 fprintf (lra_dump_file,
4624 " Rejecting split %d->%d "
4625 "resulting in > 2 %s restore insns:\n",
4626 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4627 dump_rtl_slim (lra_dump_file, restore, NULL_RTX, -1, 0);
4628 fprintf (lra_dump_file,
4629 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4631 return false;
4633 after_p = usage_insns[original_regno].after_p;
4634 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4635 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4636 bitmap_set_bit (&check_only_regs, original_regno);
4637 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4638 for (;;)
4640 if (GET_CODE (next_usage_insns) != INSN_LIST)
4642 usage_insn = next_usage_insns;
4643 break;
4645 usage_insn = XEXP (next_usage_insns, 0);
4646 lra_assert (DEBUG_INSN_P (usage_insn));
4647 next_usage_insns = XEXP (next_usage_insns, 1);
4648 substitute_pseudo (&usage_insn, original_regno, new_reg);
4649 lra_update_insn_regno_info (usage_insn);
4650 if (lra_dump_file != NULL)
4652 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
4653 original_regno, REGNO (new_reg));
4654 dump_insn_slim (lra_dump_file, usage_insn);
4657 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
4658 lra_assert (usage_insn != insn || (after_p && before_p));
4659 lra_process_new_insns (usage_insn, after_p ? NULL_RTX : restore,
4660 after_p ? restore : NULL_RTX,
4661 call_save_p
4662 ? "Add reg<-save" : "Add reg<-split");
4663 lra_process_new_insns (insn, before_p ? save : NULL_RTX,
4664 before_p ? NULL_RTX : save,
4665 call_save_p
4666 ? "Add save<-reg" : "Add split<-reg");
4667 if (nregs > 1)
4668 /* If we are trying to split multi-register. We should check
4669 conflicts on the next assignment sub-pass. IRA can allocate on
4670 sub-register levels, LRA do this on pseudos level right now and
4671 this discrepancy may create allocation conflicts after
4672 splitting. */
4673 lra_risky_transformations_p = true;
4674 if (lra_dump_file != NULL)
4675 fprintf (lra_dump_file,
4676 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4677 return true;
4680 /* Recognize that we need a split transformation for insn INSN, which
4681 defines or uses REGNO in its insn biggest MODE (we use it only if
4682 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
4683 hard registers which might be used for reloads since the EBB end.
4684 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
4685 uid before starting INSN processing. Return true if we succeed in
4686 such transformation. */
4687 static bool
4688 split_if_necessary (int regno, enum machine_mode mode,
4689 HARD_REG_SET potential_reload_hard_regs,
4690 bool before_p, rtx insn, int max_uid)
4692 bool res = false;
4693 int i, nregs = 1;
4694 rtx next_usage_insns;
4696 if (regno < FIRST_PSEUDO_REGISTER)
4697 nregs = hard_regno_nregs[regno][mode];
4698 for (i = 0; i < nregs; i++)
4699 if (usage_insns[regno + i].check == curr_usage_insns_check
4700 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
4701 /* To avoid processing the register twice or more. */
4702 && ((GET_CODE (next_usage_insns) != INSN_LIST
4703 && INSN_UID (next_usage_insns) < max_uid)
4704 || (GET_CODE (next_usage_insns) == INSN_LIST
4705 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
4706 && need_for_split_p (potential_reload_hard_regs, regno + i)
4707 && split_reg (before_p, regno + i, insn, next_usage_insns))
4708 res = true;
4709 return res;
4712 /* Check only registers living at the current program point in the
4713 current EBB. */
4714 static bitmap_head live_regs;
4716 /* Update live info in EBB given by its HEAD and TAIL insns after
4717 inheritance/split transformation. The function removes dead moves
4718 too. */
4719 static void
4720 update_ebb_live_info (rtx head, rtx tail)
4722 unsigned int j;
4723 int regno;
4724 bool live_p;
4725 rtx prev_insn, set;
4726 bool remove_p;
4727 basic_block last_bb, prev_bb, curr_bb;
4728 bitmap_iterator bi;
4729 struct lra_insn_reg *reg;
4730 edge e;
4731 edge_iterator ei;
4733 last_bb = BLOCK_FOR_INSN (tail);
4734 prev_bb = NULL;
4735 for (curr_insn = tail;
4736 curr_insn != PREV_INSN (head);
4737 curr_insn = prev_insn)
4739 prev_insn = PREV_INSN (curr_insn);
4740 /* We need to process empty blocks too. They contain
4741 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
4742 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
4743 continue;
4744 curr_bb = BLOCK_FOR_INSN (curr_insn);
4745 if (curr_bb != prev_bb)
4747 if (prev_bb != NULL)
4749 /* Update df_get_live_in (prev_bb): */
4750 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4751 if (bitmap_bit_p (&live_regs, j))
4752 bitmap_set_bit (df_get_live_in (prev_bb), j);
4753 else
4754 bitmap_clear_bit (df_get_live_in (prev_bb), j);
4756 if (curr_bb != last_bb)
4758 /* Update df_get_live_out (curr_bb): */
4759 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4761 live_p = bitmap_bit_p (&live_regs, j);
4762 if (! live_p)
4763 FOR_EACH_EDGE (e, ei, curr_bb->succs)
4764 if (bitmap_bit_p (df_get_live_in (e->dest), j))
4766 live_p = true;
4767 break;
4769 if (live_p)
4770 bitmap_set_bit (df_get_live_out (curr_bb), j);
4771 else
4772 bitmap_clear_bit (df_get_live_out (curr_bb), j);
4775 prev_bb = curr_bb;
4776 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
4778 if (! NONDEBUG_INSN_P (curr_insn))
4779 continue;
4780 curr_id = lra_get_insn_recog_data (curr_insn);
4781 remove_p = false;
4782 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
4783 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
4784 && bitmap_bit_p (&check_only_regs, regno)
4785 && ! bitmap_bit_p (&live_regs, regno))
4786 remove_p = true;
4787 /* See which defined values die here. */
4788 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4789 if (reg->type == OP_OUT && ! reg->subreg_p)
4790 bitmap_clear_bit (&live_regs, reg->regno);
4791 /* Mark each used value as live. */
4792 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4793 if (reg->type != OP_OUT
4794 && bitmap_bit_p (&check_only_regs, reg->regno))
4795 bitmap_set_bit (&live_regs, reg->regno);
4796 /* It is quite important to remove dead move insns because it
4797 means removing dead store. We don't need to process them for
4798 constraints. */
4799 if (remove_p)
4801 if (lra_dump_file != NULL)
4803 fprintf (lra_dump_file, " Removing dead insn:\n ");
4804 dump_insn_slim (lra_dump_file, curr_insn);
4806 lra_set_insn_deleted (curr_insn);
4811 /* The structure describes info to do an inheritance for the current
4812 insn. We need to collect such info first before doing the
4813 transformations because the transformations change the insn
4814 internal representation. */
4815 struct to_inherit
4817 /* Original regno. */
4818 int regno;
4819 /* Subsequent insns which can inherit original reg value. */
4820 rtx insns;
4823 /* Array containing all info for doing inheritance from the current
4824 insn. */
4825 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
4827 /* Number elements in the previous array. */
4828 static int to_inherit_num;
4830 /* Add inheritance info REGNO and INSNS. Their meaning is described in
4831 structure to_inherit. */
4832 static void
4833 add_to_inherit (int regno, rtx insns)
4835 int i;
4837 for (i = 0; i < to_inherit_num; i++)
4838 if (to_inherit[i].regno == regno)
4839 return;
4840 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
4841 to_inherit[to_inherit_num].regno = regno;
4842 to_inherit[to_inherit_num++].insns = insns;
4845 /* Return the last non-debug insn in basic block BB, or the block begin
4846 note if none. */
4847 static rtx
4848 get_last_insertion_point (basic_block bb)
4850 rtx insn;
4852 FOR_BB_INSNS_REVERSE (bb, insn)
4853 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
4854 return insn;
4855 gcc_unreachable ();
4858 /* Set up RES by registers living on edges FROM except the edge (FROM,
4859 TO) or by registers set up in a jump insn in BB FROM. */
4860 static void
4861 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
4863 rtx last;
4864 struct lra_insn_reg *reg;
4865 edge e;
4866 edge_iterator ei;
4868 lra_assert (to != NULL);
4869 bitmap_clear (res);
4870 FOR_EACH_EDGE (e, ei, from->succs)
4871 if (e->dest != to)
4872 bitmap_ior_into (res, df_get_live_in (e->dest));
4873 last = get_last_insertion_point (from);
4874 if (! JUMP_P (last))
4875 return;
4876 curr_id = lra_get_insn_recog_data (last);
4877 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4878 if (reg->type != OP_IN)
4879 bitmap_set_bit (res, reg->regno);
4882 /* Used as a temporary results of some bitmap calculations. */
4883 static bitmap_head temp_bitmap;
4885 /* Do inheritance/split transformations in EBB starting with HEAD and
4886 finishing on TAIL. We process EBB insns in the reverse order.
4887 Return true if we did any inheritance/split transformation in the
4888 EBB.
4890 We should avoid excessive splitting which results in worse code
4891 because of inaccurate cost calculations for spilling new split
4892 pseudos in such case. To achieve this we do splitting only if
4893 register pressure is high in given basic block and there are reload
4894 pseudos requiring hard registers. We could do more register
4895 pressure calculations at any given program point to avoid necessary
4896 splitting even more but it is to expensive and the current approach
4897 works well enough. */
4898 static bool
4899 inherit_in_ebb (rtx head, rtx tail)
4901 int i, src_regno, dst_regno, nregs;
4902 bool change_p, succ_p;
4903 rtx prev_insn, next_usage_insns, set, last_insn;
4904 enum reg_class cl;
4905 struct lra_insn_reg *reg;
4906 basic_block last_processed_bb, curr_bb = NULL;
4907 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
4908 bitmap to_process;
4909 unsigned int j;
4910 bitmap_iterator bi;
4911 bool head_p, after_p;
4913 change_p = false;
4914 curr_usage_insns_check++;
4915 reloads_num = calls_num = 0;
4916 bitmap_clear (&check_only_regs);
4917 last_processed_bb = NULL;
4918 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
4919 CLEAR_HARD_REG_SET (live_hard_regs);
4920 /* We don't process new insns generated in the loop. */
4921 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
4923 prev_insn = PREV_INSN (curr_insn);
4924 if (BLOCK_FOR_INSN (curr_insn) != NULL)
4925 curr_bb = BLOCK_FOR_INSN (curr_insn);
4926 if (last_processed_bb != curr_bb)
4928 /* We are at the end of BB. Add qualified living
4929 pseudos for potential splitting. */
4930 to_process = df_get_live_out (curr_bb);
4931 if (last_processed_bb != NULL)
4933 /* We are somewhere in the middle of EBB. */
4934 get_live_on_other_edges (curr_bb, last_processed_bb,
4935 &temp_bitmap);
4936 to_process = &temp_bitmap;
4938 last_processed_bb = curr_bb;
4939 last_insn = get_last_insertion_point (curr_bb);
4940 after_p = (! JUMP_P (last_insn)
4941 && (! CALL_P (last_insn)
4942 || (find_reg_note (last_insn,
4943 REG_NORETURN, NULL_RTX) == NULL_RTX
4944 && ! SIBLING_CALL_P (last_insn))));
4945 REG_SET_TO_HARD_REG_SET (live_hard_regs, df_get_live_out (curr_bb));
4946 IOR_HARD_REG_SET (live_hard_regs, eliminable_regset);
4947 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
4948 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
4949 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
4951 if ((int) j >= lra_constraint_new_regno_start)
4952 break;
4953 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
4955 if (j < FIRST_PSEUDO_REGISTER)
4956 SET_HARD_REG_BIT (live_hard_regs, j);
4957 else
4958 add_to_hard_reg_set (&live_hard_regs,
4959 PSEUDO_REGNO_MODE (j),
4960 reg_renumber[j]);
4961 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
4965 src_regno = dst_regno = -1;
4966 if (NONDEBUG_INSN_P (curr_insn)
4967 && (set = single_set (curr_insn)) != NULL_RTX
4968 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
4970 src_regno = REGNO (SET_SRC (set));
4971 dst_regno = REGNO (SET_DEST (set));
4973 if (src_regno < lra_constraint_new_regno_start
4974 && src_regno >= FIRST_PSEUDO_REGISTER
4975 && reg_renumber[src_regno] < 0
4976 && dst_regno >= lra_constraint_new_regno_start
4977 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
4979 /* 'reload_pseudo <- original_pseudo'. */
4980 reloads_num++;
4981 succ_p = false;
4982 if (usage_insns[src_regno].check == curr_usage_insns_check
4983 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
4984 succ_p = inherit_reload_reg (false, src_regno, cl,
4985 curr_insn, next_usage_insns);
4986 if (succ_p)
4987 change_p = true;
4988 else
4989 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
4990 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
4991 IOR_HARD_REG_SET (potential_reload_hard_regs,
4992 reg_class_contents[cl]);
4994 else if (src_regno >= lra_constraint_new_regno_start
4995 && dst_regno < lra_constraint_new_regno_start
4996 && dst_regno >= FIRST_PSEUDO_REGISTER
4997 && reg_renumber[dst_regno] < 0
4998 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
4999 && usage_insns[dst_regno].check == curr_usage_insns_check
5000 && (next_usage_insns
5001 = usage_insns[dst_regno].insns) != NULL_RTX)
5003 reloads_num++;
5004 /* 'original_pseudo <- reload_pseudo'. */
5005 if (! JUMP_P (curr_insn)
5006 && inherit_reload_reg (true, dst_regno, cl,
5007 curr_insn, next_usage_insns))
5008 change_p = true;
5009 /* Invalidate. */
5010 usage_insns[dst_regno].check = 0;
5011 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5012 IOR_HARD_REG_SET (potential_reload_hard_regs,
5013 reg_class_contents[cl]);
5015 else if (INSN_P (curr_insn))
5017 int iter;
5018 int max_uid = get_max_uid ();
5020 curr_id = lra_get_insn_recog_data (curr_insn);
5021 curr_static_id = curr_id->insn_static_data;
5022 to_inherit_num = 0;
5023 /* Process insn definitions. */
5024 for (iter = 0; iter < 2; iter++)
5025 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5026 reg != NULL;
5027 reg = reg->next)
5028 if (reg->type != OP_IN
5029 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5031 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5032 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5033 && usage_insns[dst_regno].check == curr_usage_insns_check
5034 && (next_usage_insns
5035 = usage_insns[dst_regno].insns) != NULL_RTX)
5037 struct lra_insn_reg *r;
5039 for (r = curr_id->regs; r != NULL; r = r->next)
5040 if (r->type != OP_OUT && r->regno == dst_regno)
5041 break;
5042 /* Don't do inheritance if the pseudo is also
5043 used in the insn. */
5044 if (r == NULL)
5045 /* We can not do inheritance right now
5046 because the current insn reg info (chain
5047 regs) can change after that. */
5048 add_to_inherit (dst_regno, next_usage_insns);
5050 /* We can not process one reg twice here because of
5051 usage_insns invalidation. */
5052 if ((dst_regno < FIRST_PSEUDO_REGISTER
5053 || reg_renumber[dst_regno] >= 0)
5054 && ! reg->subreg_p && reg->type != OP_IN)
5056 HARD_REG_SET s;
5058 if (split_if_necessary (dst_regno, reg->biggest_mode,
5059 potential_reload_hard_regs,
5060 false, curr_insn, max_uid))
5061 change_p = true;
5062 CLEAR_HARD_REG_SET (s);
5063 if (dst_regno < FIRST_PSEUDO_REGISTER)
5064 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5065 else
5066 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5067 reg_renumber[dst_regno]);
5068 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5070 /* We should invalidate potential inheritance or
5071 splitting for the current insn usages to the next
5072 usage insns (see code below) as the output pseudo
5073 prevents this. */
5074 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5075 && reg_renumber[dst_regno] < 0)
5076 || (reg->type == OP_OUT && ! reg->subreg_p
5077 && (dst_regno < FIRST_PSEUDO_REGISTER
5078 || reg_renumber[dst_regno] >= 0)))
5080 /* Invalidate and mark definitions. */
5081 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5082 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5083 else
5085 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5086 for (i = 0; i < nregs; i++)
5087 usage_insns[dst_regno + i].check
5088 = -(int) INSN_UID (curr_insn);
5092 if (! JUMP_P (curr_insn))
5093 for (i = 0; i < to_inherit_num; i++)
5094 if (inherit_reload_reg (true, to_inherit[i].regno,
5095 ALL_REGS, curr_insn,
5096 to_inherit[i].insns))
5097 change_p = true;
5098 if (CALL_P (curr_insn))
5100 rtx cheap, pat, dest, restore;
5101 int regno, hard_regno;
5103 calls_num++;
5104 if ((cheap = find_reg_note (curr_insn,
5105 REG_RETURNED, NULL_RTX)) != NULL_RTX
5106 && ((cheap = XEXP (cheap, 0)), true)
5107 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5108 && (hard_regno = reg_renumber[regno]) >= 0
5109 /* If there are pending saves/restores, the
5110 optimization is not worth. */
5111 && usage_insns[regno].calls_num == calls_num - 1
5112 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5114 /* Restore the pseudo from the call result as
5115 REG_RETURNED note says that the pseudo value is
5116 in the call result and the pseudo is an argument
5117 of the call. */
5118 pat = PATTERN (curr_insn);
5119 if (GET_CODE (pat) == PARALLEL)
5120 pat = XVECEXP (pat, 0, 0);
5121 dest = SET_DEST (pat);
5122 start_sequence ();
5123 emit_move_insn (cheap, copy_rtx (dest));
5124 restore = get_insns ();
5125 end_sequence ();
5126 lra_process_new_insns (curr_insn, NULL, restore,
5127 "Inserting call parameter restore");
5128 /* We don't need to save/restore of the pseudo from
5129 this call. */
5130 usage_insns[regno].calls_num = calls_num;
5131 bitmap_set_bit (&check_only_regs, regno);
5134 to_inherit_num = 0;
5135 /* Process insn usages. */
5136 for (iter = 0; iter < 2; iter++)
5137 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5138 reg != NULL;
5139 reg = reg->next)
5140 if ((reg->type != OP_OUT
5141 || (reg->type == OP_OUT && reg->subreg_p))
5142 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5144 if (src_regno >= FIRST_PSEUDO_REGISTER
5145 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5147 if (usage_insns[src_regno].check == curr_usage_insns_check
5148 && (next_usage_insns
5149 = usage_insns[src_regno].insns) != NULL_RTX
5150 && NONDEBUG_INSN_P (curr_insn))
5151 add_to_inherit (src_regno, next_usage_insns);
5152 else if (usage_insns[src_regno].check
5153 != -(int) INSN_UID (curr_insn))
5154 /* Add usages but only if the reg is not set up
5155 in the same insn. */
5156 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5158 else if (src_regno < FIRST_PSEUDO_REGISTER
5159 || reg_renumber[src_regno] >= 0)
5161 bool before_p;
5162 rtx use_insn = curr_insn;
5164 before_p = (JUMP_P (curr_insn)
5165 || (CALL_P (curr_insn) && reg->type == OP_IN));
5166 if (NONDEBUG_INSN_P (curr_insn)
5167 && split_if_necessary (src_regno, reg->biggest_mode,
5168 potential_reload_hard_regs,
5169 before_p, curr_insn, max_uid))
5171 if (reg->subreg_p)
5172 lra_risky_transformations_p = true;
5173 change_p = true;
5174 /* Invalidate. */
5175 usage_insns[src_regno].check = 0;
5176 if (before_p)
5177 use_insn = PREV_INSN (curr_insn);
5179 if (NONDEBUG_INSN_P (curr_insn))
5181 if (src_regno < FIRST_PSEUDO_REGISTER)
5182 add_to_hard_reg_set (&live_hard_regs,
5183 reg->biggest_mode, src_regno);
5184 else
5185 add_to_hard_reg_set (&live_hard_regs,
5186 PSEUDO_REGNO_MODE (src_regno),
5187 reg_renumber[src_regno]);
5189 add_next_usage_insn (src_regno, use_insn, reloads_num);
5192 for (i = 0; i < to_inherit_num; i++)
5194 src_regno = to_inherit[i].regno;
5195 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5196 curr_insn, to_inherit[i].insns))
5197 change_p = true;
5198 else
5199 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5202 /* We reached the start of the current basic block. */
5203 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5204 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5206 /* We reached the beginning of the current block -- do
5207 rest of spliting in the current BB. */
5208 to_process = df_get_live_in (curr_bb);
5209 if (BLOCK_FOR_INSN (head) != curr_bb)
5211 /* We are somewhere in the middle of EBB. */
5212 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5213 curr_bb, &temp_bitmap);
5214 to_process = &temp_bitmap;
5216 head_p = true;
5217 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5219 if ((int) j >= lra_constraint_new_regno_start)
5220 break;
5221 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5222 && usage_insns[j].check == curr_usage_insns_check
5223 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5225 if (need_for_split_p (potential_reload_hard_regs, j))
5227 if (lra_dump_file != NULL && head_p)
5229 fprintf (lra_dump_file,
5230 " ----------------------------------\n");
5231 head_p = false;
5233 if (split_reg (false, j, bb_note (curr_bb),
5234 next_usage_insns))
5235 change_p = true;
5237 usage_insns[j].check = 0;
5242 return change_p;
5245 /* This value affects EBB forming. If probability of edge from EBB to
5246 a BB is not greater than the following value, we don't add the BB
5247 to EBB. */
5248 #define EBB_PROBABILITY_CUTOFF ((REG_BR_PROB_BASE * 50) / 100)
5250 /* Current number of inheritance/split iteration. */
5251 int lra_inheritance_iter;
5253 /* Entry function for inheritance/split pass. */
5254 void
5255 lra_inheritance (void)
5257 int i;
5258 basic_block bb, start_bb;
5259 edge e;
5261 lra_inheritance_iter++;
5262 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5263 return;
5264 timevar_push (TV_LRA_INHERITANCE);
5265 if (lra_dump_file != NULL)
5266 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5267 lra_inheritance_iter);
5268 curr_usage_insns_check = 0;
5269 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5270 for (i = 0; i < lra_constraint_new_regno_start; i++)
5271 usage_insns[i].check = 0;
5272 bitmap_initialize (&check_only_regs, &reg_obstack);
5273 bitmap_initialize (&live_regs, &reg_obstack);
5274 bitmap_initialize (&temp_bitmap, &reg_obstack);
5275 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5276 FOR_EACH_BB (bb)
5278 start_bb = bb;
5279 if (lra_dump_file != NULL)
5280 fprintf (lra_dump_file, "EBB");
5281 /* Form a EBB starting with BB. */
5282 bitmap_clear (&ebb_global_regs);
5283 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5284 for (;;)
5286 if (lra_dump_file != NULL)
5287 fprintf (lra_dump_file, " %d", bb->index);
5288 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5289 || LABEL_P (BB_HEAD (bb->next_bb)))
5290 break;
5291 e = find_fallthru_edge (bb->succs);
5292 if (! e)
5293 break;
5294 if (e->probability <= EBB_PROBABILITY_CUTOFF)
5295 break;
5296 bb = bb->next_bb;
5298 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5299 if (lra_dump_file != NULL)
5300 fprintf (lra_dump_file, "\n");
5301 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5302 /* Remember that the EBB head and tail can change in
5303 inherit_in_ebb. */
5304 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5306 bitmap_clear (&ebb_global_regs);
5307 bitmap_clear (&temp_bitmap);
5308 bitmap_clear (&live_regs);
5309 bitmap_clear (&check_only_regs);
5310 free (usage_insns);
5312 timevar_pop (TV_LRA_INHERITANCE);
5317 /* This page contains code to undo failed inheritance/split
5318 transformations. */
5320 /* Current number of iteration undoing inheritance/split. */
5321 int lra_undo_inheritance_iter;
5323 /* Fix BB live info LIVE after removing pseudos created on pass doing
5324 inheritance/split which are REMOVED_PSEUDOS. */
5325 static void
5326 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5328 unsigned int regno;
5329 bitmap_iterator bi;
5331 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5332 if (bitmap_clear_bit (live, regno))
5333 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5336 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5337 number. */
5338 static int
5339 get_regno (rtx reg)
5341 if (GET_CODE (reg) == SUBREG)
5342 reg = SUBREG_REG (reg);
5343 if (REG_P (reg))
5344 return REGNO (reg);
5345 return -1;
5348 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5349 return true if we did any change. The undo transformations for
5350 inheritance looks like
5351 i <- i2
5352 p <- i => p <- i2
5353 or removing
5354 p <- i, i <- p, and i <- i3
5355 where p is original pseudo from which inheritance pseudo i was
5356 created, i and i3 are removed inheritance pseudos, i2 is another
5357 not removed inheritance pseudo. All split pseudos or other
5358 occurrences of removed inheritance pseudos are changed on the
5359 corresponding original pseudos.
5361 The function also schedules insns changed and created during
5362 inheritance/split pass for processing by the subsequent constraint
5363 pass. */
5364 static bool
5365 remove_inheritance_pseudos (bitmap remove_pseudos)
5367 basic_block bb;
5368 int regno, sregno, prev_sregno, dregno, restore_regno;
5369 rtx set, prev_set, prev_insn;
5370 bool change_p, done_p;
5372 change_p = ! bitmap_empty_p (remove_pseudos);
5373 /* We can not finish the function right away if CHANGE_P is true
5374 because we need to marks insns affected by previous
5375 inheritance/split pass for processing by the subsequent
5376 constraint pass. */
5377 FOR_EACH_BB (bb)
5379 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5380 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5381 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5383 if (! INSN_P (curr_insn))
5384 continue;
5385 done_p = false;
5386 sregno = dregno = -1;
5387 if (change_p && NONDEBUG_INSN_P (curr_insn)
5388 && (set = single_set (curr_insn)) != NULL_RTX)
5390 dregno = get_regno (SET_DEST (set));
5391 sregno = get_regno (SET_SRC (set));
5394 if (sregno >= 0 && dregno >= 0)
5396 if ((bitmap_bit_p (remove_pseudos, sregno)
5397 && (lra_reg_info[sregno].restore_regno == dregno
5398 || (bitmap_bit_p (remove_pseudos, dregno)
5399 && (lra_reg_info[sregno].restore_regno
5400 == lra_reg_info[dregno].restore_regno))))
5401 || (bitmap_bit_p (remove_pseudos, dregno)
5402 && lra_reg_info[dregno].restore_regno == sregno))
5403 /* One of the following cases:
5404 original <- removed inheritance pseudo
5405 removed inherit pseudo <- another removed inherit pseudo
5406 removed inherit pseudo <- original pseudo
5408 removed_split_pseudo <- original_reg
5409 original_reg <- removed_split_pseudo */
5411 if (lra_dump_file != NULL)
5413 fprintf (lra_dump_file, " Removing %s:\n",
5414 bitmap_bit_p (&lra_split_regs, sregno)
5415 || bitmap_bit_p (&lra_split_regs, dregno)
5416 ? "split" : "inheritance");
5417 dump_insn_slim (lra_dump_file, curr_insn);
5419 lra_set_insn_deleted (curr_insn);
5420 done_p = true;
5422 else if (bitmap_bit_p (remove_pseudos, sregno)
5423 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5425 /* Search the following pattern:
5426 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5427 original_pseudo <- inherit_or_split_pseudo1
5428 where the 2nd insn is the current insn and
5429 inherit_or_split_pseudo2 is not removed. If it is found,
5430 change the current insn onto:
5431 original_pseudo <- inherit_or_split_pseudo2. */
5432 for (prev_insn = PREV_INSN (curr_insn);
5433 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5434 prev_insn = PREV_INSN (prev_insn))
5436 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5437 && (prev_set = single_set (prev_insn)) != NULL_RTX
5438 /* There should be no subregs in insn we are
5439 searching because only the original reg might
5440 be in subreg when we changed the mode of
5441 load/store for splitting. */
5442 && REG_P (SET_DEST (prev_set))
5443 && REG_P (SET_SRC (prev_set))
5444 && (int) REGNO (SET_DEST (prev_set)) == sregno
5445 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5446 >= FIRST_PSEUDO_REGISTER)
5447 /* As we consider chain of inheritance or
5448 splitting described in above comment we should
5449 check that sregno and prev_sregno were
5450 inheritance/split pseudos created from the
5451 same original regno. */
5452 && (lra_reg_info[sregno].restore_regno
5453 == lra_reg_info[prev_sregno].restore_regno)
5454 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5456 lra_assert (GET_MODE (SET_SRC (prev_set))
5457 == GET_MODE (regno_reg_rtx[sregno]));
5458 if (GET_CODE (SET_SRC (set)) == SUBREG)
5459 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5460 else
5461 SET_SRC (set) = SET_SRC (prev_set);
5462 lra_push_insn_and_update_insn_regno_info (curr_insn);
5463 lra_set_used_insn_alternative_by_uid
5464 (INSN_UID (curr_insn), -1);
5465 done_p = true;
5466 if (lra_dump_file != NULL)
5468 fprintf (lra_dump_file, " Change reload insn:\n");
5469 dump_insn_slim (lra_dump_file, curr_insn);
5474 if (! done_p)
5476 struct lra_insn_reg *reg;
5477 bool restored_regs_p = false;
5478 bool kept_regs_p = false;
5480 curr_id = lra_get_insn_recog_data (curr_insn);
5481 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5483 regno = reg->regno;
5484 restore_regno = lra_reg_info[regno].restore_regno;
5485 if (restore_regno >= 0)
5487 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5489 substitute_pseudo (&curr_insn, regno,
5490 regno_reg_rtx[restore_regno]);
5491 restored_regs_p = true;
5493 else
5494 kept_regs_p = true;
5497 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5499 /* The instruction has changed since the previous
5500 constraints pass. */
5501 lra_push_insn_and_update_insn_regno_info (curr_insn);
5502 lra_set_used_insn_alternative_by_uid
5503 (INSN_UID (curr_insn), -1);
5505 else if (restored_regs_p)
5506 /* The instruction has been restored to the form that
5507 it had during the previous constraints pass. */
5508 lra_update_insn_regno_info (curr_insn);
5509 if (restored_regs_p && lra_dump_file != NULL)
5511 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5512 dump_insn_slim (lra_dump_file, curr_insn);
5517 return change_p;
5520 /* If optional reload pseudos failed to get a hard register or was not
5521 inherited, it is better to remove optional reloads. We do this
5522 transformation after undoing inheritance to figure out necessity to
5523 remove optional reloads easier. Return true if we do any
5524 change. */
5525 static bool
5526 undo_optional_reloads (void)
5528 bool change_p, keep_p;
5529 unsigned int regno, uid;
5530 bitmap_iterator bi, bi2;
5531 rtx insn, set, src, dest;
5532 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5534 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5535 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5536 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5538 keep_p = false;
5539 /* Keep optional reloads from previous subpasses. */
5540 if (lra_reg_info[regno].restore_regno < 0
5541 /* If the original pseudo changed its allocation, just
5542 removing the optional pseudo is dangerous as the original
5543 pseudo will have longer live range. */
5544 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
5545 keep_p = true;
5546 else if (reg_renumber[regno] >= 0)
5547 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
5549 insn = lra_insn_recog_data[uid]->insn;
5550 if ((set = single_set (insn)) == NULL_RTX)
5551 continue;
5552 src = SET_SRC (set);
5553 dest = SET_DEST (set);
5554 if (! REG_P (src) || ! REG_P (dest))
5555 continue;
5556 if (REGNO (dest) == regno
5557 /* Ignore insn for optional reloads itself. */
5558 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
5559 /* Check only inheritance on last inheritance pass. */
5560 && (int) REGNO (src) >= new_regno_start
5561 /* Check that the optional reload was inherited. */
5562 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
5564 keep_p = true;
5565 break;
5568 if (keep_p)
5570 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
5571 if (lra_dump_file != NULL)
5572 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
5575 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
5576 bitmap_initialize (&insn_bitmap, &reg_obstack);
5577 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
5579 if (lra_dump_file != NULL)
5580 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
5581 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
5582 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
5584 insn = lra_insn_recog_data[uid]->insn;
5585 if ((set = single_set (insn)) != NULL_RTX)
5587 src = SET_SRC (set);
5588 dest = SET_DEST (set);
5589 if (REG_P (src) && REG_P (dest)
5590 && ((REGNO (src) == regno
5591 && (lra_reg_info[regno].restore_regno
5592 == (int) REGNO (dest)))
5593 || (REGNO (dest) == regno
5594 && (lra_reg_info[regno].restore_regno
5595 == (int) REGNO (src)))))
5597 if (lra_dump_file != NULL)
5599 fprintf (lra_dump_file, " Deleting move %u\n",
5600 INSN_UID (insn));
5601 dump_insn_slim (lra_dump_file, insn);
5603 lra_set_insn_deleted (insn);
5604 continue;
5606 /* We should not worry about generation memory-memory
5607 moves here as if the corresponding inheritance did
5608 not work (inheritance pseudo did not get a hard reg),
5609 we remove the inheritance pseudo and the optional
5610 reload. */
5612 substitute_pseudo (&insn, regno,
5613 regno_reg_rtx[lra_reg_info[regno].restore_regno]);
5614 lra_update_insn_regno_info (insn);
5615 if (lra_dump_file != NULL)
5617 fprintf (lra_dump_file,
5618 " Restoring original insn:\n");
5619 dump_insn_slim (lra_dump_file, insn);
5623 /* Clear restore_regnos. */
5624 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5625 lra_reg_info[regno].restore_regno = -1;
5626 bitmap_clear (&insn_bitmap);
5627 bitmap_clear (&removed_optional_reload_pseudos);
5628 return change_p;
5631 /* Entry function for undoing inheritance/split transformation. Return true
5632 if we did any RTL change in this pass. */
5633 bool
5634 lra_undo_inheritance (void)
5636 unsigned int regno;
5637 int restore_regno, hard_regno;
5638 int n_all_inherit, n_inherit, n_all_split, n_split;
5639 bitmap_head remove_pseudos;
5640 bitmap_iterator bi;
5641 bool change_p;
5643 lra_undo_inheritance_iter++;
5644 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5645 return false;
5646 if (lra_dump_file != NULL)
5647 fprintf (lra_dump_file,
5648 "\n********** Undoing inheritance #%d: **********\n\n",
5649 lra_undo_inheritance_iter);
5650 bitmap_initialize (&remove_pseudos, &reg_obstack);
5651 n_inherit = n_all_inherit = 0;
5652 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5653 if (lra_reg_info[regno].restore_regno >= 0)
5655 n_all_inherit++;
5656 if (reg_renumber[regno] < 0
5657 /* If the original pseudo changed its allocation, just
5658 removing inheritance is dangerous as for changing
5659 allocation we used shorter live-ranges. */
5660 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
5661 bitmap_set_bit (&remove_pseudos, regno);
5662 else
5663 n_inherit++;
5665 if (lra_dump_file != NULL && n_all_inherit != 0)
5666 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
5667 n_inherit, n_all_inherit,
5668 (double) n_inherit / n_all_inherit * 100);
5669 n_split = n_all_split = 0;
5670 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5671 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
5673 n_all_split++;
5674 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
5675 ? reg_renumber[restore_regno] : restore_regno);
5676 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
5677 bitmap_set_bit (&remove_pseudos, regno);
5678 else
5680 n_split++;
5681 if (lra_dump_file != NULL)
5682 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
5683 regno, restore_regno);
5686 if (lra_dump_file != NULL && n_all_split != 0)
5687 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
5688 n_split, n_all_split,
5689 (double) n_split / n_all_split * 100);
5690 change_p = remove_inheritance_pseudos (&remove_pseudos);
5691 bitmap_clear (&remove_pseudos);
5692 /* Clear restore_regnos. */
5693 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5694 lra_reg_info[regno].restore_regno = -1;
5695 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5696 lra_reg_info[regno].restore_regno = -1;
5697 change_p = undo_optional_reloads () || change_p;
5698 return change_p;