2013-11-29 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / lra-constraints.c
blobbb5242a962a1397b4602de22a106220d1ad1cbc3
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 (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 /* If we have decided to substitute X with the equivalent value,
341 return that value after elimination for INSN, otherwise return
342 X. */
343 static rtx
344 get_equiv_with_elimination (rtx x, rtx insn)
346 rtx res = get_equiv (x);
348 if (x == res || CONSTANT_P (res))
349 return res;
350 return lra_eliminate_regs_1 (insn, res, GET_MODE (res), false, false, true);
353 /* Set up curr_operand_mode. */
354 static void
355 init_curr_operand_mode (void)
357 int nop = curr_static_id->n_operands;
358 for (int i = 0; i < nop; i++)
360 enum machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
361 if (mode == VOIDmode)
363 /* The .md mode for address operands is the mode of the
364 addressed value rather than the mode of the address itself. */
365 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
366 mode = Pmode;
367 else
368 mode = curr_static_id->operand[i].mode;
370 curr_operand_mode[i] = mode;
376 /* The page contains code to reuse input reloads. */
378 /* Structure describes input reload of the current insns. */
379 struct input_reload
381 /* Reloaded value. */
382 rtx input;
383 /* Reload pseudo used. */
384 rtx reg;
387 /* The number of elements in the following array. */
388 static int curr_insn_input_reloads_num;
389 /* Array containing info about input reloads. It is used to find the
390 same input reload and reuse the reload pseudo in this case. */
391 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
393 /* Initiate data concerning reuse of input reloads for the current
394 insn. */
395 static void
396 init_curr_insn_input_reloads (void)
398 curr_insn_input_reloads_num = 0;
401 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
402 created input reload pseudo (only if TYPE is not OP_OUT). The
403 result pseudo is returned through RESULT_REG. Return TRUE if we
404 created a new pseudo, FALSE if we reused the already created input
405 reload pseudo. Use TITLE to describe new registers for debug
406 purposes. */
407 static bool
408 get_reload_reg (enum op_type type, enum machine_mode mode, rtx original,
409 enum reg_class rclass, const char *title, rtx *result_reg)
411 int i, regno;
412 enum reg_class new_class;
414 if (type == OP_OUT)
416 *result_reg
417 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
418 return true;
420 /* Prevent reuse value of expression with side effects,
421 e.g. volatile memory. */
422 if (! side_effects_p (original))
423 for (i = 0; i < curr_insn_input_reloads_num; i++)
424 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
425 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
427 rtx reg = curr_insn_input_reloads[i].reg;
428 regno = REGNO (reg);
429 /* If input is equal to original and both are VOIDmode,
430 GET_MODE (reg) might be still different from mode.
431 Ensure we don't return *result_reg with wrong mode. */
432 if (GET_MODE (reg) != mode)
434 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
435 continue;
436 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
437 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
438 continue;
440 *result_reg = reg;
441 if (lra_dump_file != NULL)
443 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
444 dump_value_slim (lra_dump_file, original, 1);
446 if (new_class != lra_get_allocno_class (regno))
447 lra_change_class (regno, new_class, ", change to", false);
448 if (lra_dump_file != NULL)
449 fprintf (lra_dump_file, "\n");
450 return false;
452 *result_reg = lra_create_new_reg (mode, original, rclass, title);
453 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
454 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
455 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
456 return true;
461 /* The page contains code to extract memory address parts. */
463 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
464 static inline bool
465 ok_for_index_p_nonstrict (rtx reg)
467 unsigned regno = REGNO (reg);
469 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
472 /* A version of regno_ok_for_base_p for use here, when all pseudos
473 should count as OK. Arguments as for regno_ok_for_base_p. */
474 static inline bool
475 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode, addr_space_t as,
476 enum rtx_code outer_code, enum rtx_code index_code)
478 unsigned regno = REGNO (reg);
480 if (regno >= FIRST_PSEUDO_REGISTER)
481 return true;
482 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
487 /* The page contains major code to choose the current insn alternative
488 and generate reloads for it. */
490 /* Return the offset from REGNO of the least significant register
491 in (reg:MODE REGNO).
493 This function is used to tell whether two registers satisfy
494 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
496 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
497 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
499 lra_constraint_offset (int regno, enum machine_mode mode)
501 lra_assert (regno < FIRST_PSEUDO_REGISTER);
502 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
503 && SCALAR_INT_MODE_P (mode))
504 return hard_regno_nregs[regno][mode] - 1;
505 return 0;
508 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
509 if they are the same hard reg, and has special hacks for
510 auto-increment and auto-decrement. This is specifically intended for
511 process_alt_operands to use in determining whether two operands
512 match. X is the operand whose number is the lower of the two.
514 It is supposed that X is the output operand and Y is the input
515 operand. Y_HARD_REGNO is the final hard regno of register Y or
516 register in subreg Y as we know it now. Otherwise, it is a
517 negative value. */
518 static bool
519 operands_match_p (rtx x, rtx y, int y_hard_regno)
521 int i;
522 RTX_CODE code = GET_CODE (x);
523 const char *fmt;
525 if (x == y)
526 return true;
527 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
528 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
530 int j;
532 i = get_hard_regno (x);
533 if (i < 0)
534 goto slow;
536 if ((j = y_hard_regno) < 0)
537 goto slow;
539 i += lra_constraint_offset (i, GET_MODE (x));
540 j += lra_constraint_offset (j, GET_MODE (y));
542 return i == j;
545 /* If two operands must match, because they are really a single
546 operand of an assembler insn, then two post-increments are invalid
547 because the assembler insn would increment only once. On the
548 other hand, a post-increment matches ordinary indexing if the
549 post-increment is the output operand. */
550 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
551 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
553 /* Two pre-increments are invalid because the assembler insn would
554 increment only once. On the other hand, a pre-increment matches
555 ordinary indexing if the pre-increment is the input operand. */
556 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
557 || GET_CODE (y) == PRE_MODIFY)
558 return operands_match_p (x, XEXP (y, 0), -1);
560 slow:
562 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
563 && x == SUBREG_REG (y))
564 return true;
565 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
566 && SUBREG_REG (x) == y)
567 return true;
569 /* Now we have disposed of all the cases in which different rtx
570 codes can match. */
571 if (code != GET_CODE (y))
572 return false;
574 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
575 if (GET_MODE (x) != GET_MODE (y))
576 return false;
578 switch (code)
580 CASE_CONST_UNIQUE:
581 return false;
583 case LABEL_REF:
584 return XEXP (x, 0) == XEXP (y, 0);
585 case SYMBOL_REF:
586 return XSTR (x, 0) == XSTR (y, 0);
588 default:
589 break;
592 /* Compare the elements. If any pair of corresponding elements fail
593 to match, return false for the whole things. */
595 fmt = GET_RTX_FORMAT (code);
596 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
598 int val, j;
599 switch (fmt[i])
601 case 'w':
602 if (XWINT (x, i) != XWINT (y, i))
603 return false;
604 break;
606 case 'i':
607 if (XINT (x, i) != XINT (y, i))
608 return false;
609 break;
611 case 'e':
612 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
613 if (val == 0)
614 return false;
615 break;
617 case '0':
618 break;
620 case 'E':
621 if (XVECLEN (x, i) != XVECLEN (y, i))
622 return false;
623 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
625 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
626 if (val == 0)
627 return false;
629 break;
631 /* It is believed that rtx's at this level will never
632 contain anything but integers and other rtx's, except for
633 within LABEL_REFs and SYMBOL_REFs. */
634 default:
635 gcc_unreachable ();
638 return true;
641 /* True if X is a constant that can be forced into the constant pool.
642 MODE is the mode of the operand, or VOIDmode if not known. */
643 #define CONST_POOL_OK_P(MODE, X) \
644 ((MODE) != VOIDmode \
645 && CONSTANT_P (X) \
646 && GET_CODE (X) != HIGH \
647 && !targetm.cannot_force_const_mem (MODE, X))
649 /* True if C is a non-empty register class that has too few registers
650 to be safely used as a reload target class. */
651 #define SMALL_REGISTER_CLASS_P(C) \
652 (reg_class_size [(C)] == 1 \
653 || (reg_class_size [(C)] >= 1 && targetm.class_likely_spilled_p (C)))
655 /* If REG is a reload pseudo, try to make its class satisfying CL. */
656 static void
657 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
659 enum reg_class rclass;
661 /* Do not make more accurate class from reloads generated. They are
662 mostly moves with a lot of constraints. Making more accurate
663 class may results in very narrow class and impossibility of find
664 registers for several reloads of one insn. */
665 if (INSN_UID (curr_insn) >= new_insn_uid_start)
666 return;
667 if (GET_CODE (reg) == SUBREG)
668 reg = SUBREG_REG (reg);
669 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
670 return;
671 if (in_class_p (reg, cl, &rclass) && rclass != cl)
672 lra_change_class (REGNO (reg), rclass, " Change to", true);
675 /* Generate reloads for matching OUT and INS (array of input operand
676 numbers with end marker -1) with reg class GOAL_CLASS. Add input
677 and output reloads correspondingly to the lists *BEFORE and *AFTER.
678 OUT might be negative. In this case we generate input reloads for
679 matched input operands INS. */
680 static void
681 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
682 rtx *before, rtx *after)
684 int i, in;
685 rtx new_in_reg, new_out_reg, reg, clobber;
686 enum machine_mode inmode, outmode;
687 rtx in_rtx = *curr_id->operand_loc[ins[0]];
688 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
690 inmode = curr_operand_mode[ins[0]];
691 outmode = out < 0 ? inmode : curr_operand_mode[out];
692 push_to_sequence (*before);
693 if (inmode != outmode)
695 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
697 reg = new_in_reg
698 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
699 goal_class, "");
700 if (SCALAR_INT_MODE_P (inmode))
701 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
702 else
703 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
704 LRA_SUBREG_P (new_out_reg) = 1;
705 /* If the input reg is dying here, we can use the same hard
706 register for REG and IN_RTX. We do it only for original
707 pseudos as reload pseudos can die although original
708 pseudos still live where reload pseudos dies. */
709 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
710 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
711 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
713 else
715 reg = new_out_reg
716 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
717 goal_class, "");
718 if (SCALAR_INT_MODE_P (outmode))
719 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
720 else
721 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
722 /* NEW_IN_REG is non-paradoxical subreg. We don't want
723 NEW_OUT_REG living above. We add clobber clause for
724 this. This is just a temporary clobber. We can remove
725 it at the end of LRA work. */
726 clobber = emit_clobber (new_out_reg);
727 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
728 LRA_SUBREG_P (new_in_reg) = 1;
729 if (GET_CODE (in_rtx) == SUBREG)
731 rtx subreg_reg = SUBREG_REG (in_rtx);
733 /* If SUBREG_REG is dying here and sub-registers IN_RTX
734 and NEW_IN_REG are similar, we can use the same hard
735 register for REG and SUBREG_REG. */
736 if (REG_P (subreg_reg)
737 && (int) REGNO (subreg_reg) < lra_new_regno_start
738 && GET_MODE (subreg_reg) == outmode
739 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
740 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
741 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
745 else
747 /* Pseudos have values -- see comments for lra_reg_info.
748 Different pseudos with the same value do not conflict even if
749 they live in the same place. When we create a pseudo we
750 assign value of original pseudo (if any) from which we
751 created the new pseudo. If we create the pseudo from the
752 input pseudo, the new pseudo will no conflict with the input
753 pseudo which is wrong when the input pseudo lives after the
754 insn and as the new pseudo value is changed by the insn
755 output. Therefore we create the new pseudo from the output.
757 We cannot reuse the current output register because we might
758 have a situation like "a <- a op b", where the constraints
759 force the second input operand ("b") to match the output
760 operand ("a"). "b" must then be copied into a new register
761 so that it doesn't clobber the current value of "a". */
763 new_in_reg = new_out_reg
764 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
765 goal_class, "");
767 /* In operand can be got from transformations before processing insn
768 constraints. One example of such transformations is subreg
769 reloading (see function simplify_operand_subreg). The new
770 pseudos created by the transformations might have inaccurate
771 class (ALL_REGS) and we should make their classes more
772 accurate. */
773 narrow_reload_pseudo_class (in_rtx, goal_class);
774 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
775 *before = get_insns ();
776 end_sequence ();
777 for (i = 0; (in = ins[i]) >= 0; i++)
779 lra_assert
780 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
781 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
782 *curr_id->operand_loc[in] = new_in_reg;
784 lra_update_dups (curr_id, ins);
785 if (out < 0)
786 return;
787 /* See a comment for the input operand above. */
788 narrow_reload_pseudo_class (out_rtx, goal_class);
789 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
791 start_sequence ();
792 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
793 emit_insn (*after);
794 *after = get_insns ();
795 end_sequence ();
797 *curr_id->operand_loc[out] = new_out_reg;
798 lra_update_dup (curr_id, out);
801 /* Return register class which is union of all reg classes in insn
802 constraint alternative string starting with P. */
803 static enum reg_class
804 reg_class_from_constraints (const char *p)
806 int c, len;
807 enum reg_class op_class = NO_REGS;
810 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
812 case '#':
813 case ',':
814 return op_class;
816 case 'p':
817 op_class = (reg_class_subunion
818 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
819 ADDRESS, SCRATCH)]);
820 break;
822 case 'g':
823 case 'r':
824 op_class = reg_class_subunion[op_class][GENERAL_REGS];
825 break;
827 default:
828 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
830 #ifdef EXTRA_CONSTRAINT_STR
831 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
832 op_class
833 = (reg_class_subunion
834 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
835 ADDRESS, SCRATCH)]);
836 #endif
837 break;
840 op_class
841 = reg_class_subunion[op_class][REG_CLASS_FROM_CONSTRAINT (c, p)];
842 break;
844 while ((p += len), c);
845 return op_class;
848 /* If OP is a register, return the class of the register as per
849 get_reg_class, otherwise return NO_REGS. */
850 static inline enum reg_class
851 get_op_class (rtx op)
853 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
856 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
857 otherwise. If modes of MEM_PSEUDO and VAL are different, use
858 SUBREG for VAL to make them equal. */
859 static rtx
860 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
862 if (GET_MODE (mem_pseudo) != GET_MODE (val))
864 /* Usually size of mem_pseudo is greater than val size but in
865 rare cases it can be less as it can be defined by target
866 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
867 if (! MEM_P (val))
869 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
870 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
872 LRA_SUBREG_P (val) = 1;
874 else
876 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
877 LRA_SUBREG_P (mem_pseudo) = 1;
880 return (to_p
881 ? gen_move_insn (mem_pseudo, val)
882 : gen_move_insn (val, mem_pseudo));
885 /* Process a special case insn (register move), return true if we
886 don't need to process it anymore. INSN should be a single set
887 insn. Set up that RTL was changed through CHANGE_P and macro
888 SECONDARY_MEMORY_NEEDED says to use secondary memory through
889 SEC_MEM_P. */
890 static bool
891 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
893 int sregno, dregno;
894 rtx dest, src, dreg, sreg, old_sreg, new_reg, before, scratch_reg;
895 enum reg_class dclass, sclass, secondary_class;
896 enum machine_mode sreg_mode;
897 secondary_reload_info sri;
899 lra_assert (curr_insn_set != NULL_RTX);
900 dreg = dest = SET_DEST (curr_insn_set);
901 sreg = src = SET_SRC (curr_insn_set);
902 if (GET_CODE (dest) == SUBREG)
903 dreg = SUBREG_REG (dest);
904 if (GET_CODE (src) == SUBREG)
905 sreg = SUBREG_REG (src);
906 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
907 return false;
908 sclass = dclass = NO_REGS;
909 if (REG_P (dreg))
910 dclass = get_reg_class (REGNO (dreg));
911 if (dclass == ALL_REGS)
912 /* ALL_REGS is used for new pseudos created by transformations
913 like reload of SUBREG_REG (see function
914 simplify_operand_subreg). We don't know their class yet. We
915 should figure out the class from processing the insn
916 constraints not in this fast path function. Even if ALL_REGS
917 were a right class for the pseudo, secondary_... hooks usually
918 are not define for ALL_REGS. */
919 return false;
920 sreg_mode = GET_MODE (sreg);
921 old_sreg = sreg;
922 if (REG_P (sreg))
923 sclass = get_reg_class (REGNO (sreg));
924 if (sclass == ALL_REGS)
925 /* See comments above. */
926 return false;
927 if (sclass == NO_REGS && dclass == NO_REGS)
928 return false;
929 #ifdef SECONDARY_MEMORY_NEEDED
930 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
931 #ifdef SECONDARY_MEMORY_NEEDED_MODE
932 && ((sclass != NO_REGS && dclass != NO_REGS)
933 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
934 #endif
937 *sec_mem_p = true;
938 return false;
940 #endif
941 if (! REG_P (dreg) || ! REG_P (sreg))
942 return false;
943 sri.prev_sri = NULL;
944 sri.icode = CODE_FOR_nothing;
945 sri.extra_cost = 0;
946 secondary_class = NO_REGS;
947 /* Set up hard register for a reload pseudo for hook
948 secondary_reload because some targets just ignore unassigned
949 pseudos in the hook. */
950 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
952 dregno = REGNO (dreg);
953 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
955 else
956 dregno = -1;
957 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
959 sregno = REGNO (sreg);
960 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
962 else
963 sregno = -1;
964 if (sclass != NO_REGS)
965 secondary_class
966 = (enum reg_class) targetm.secondary_reload (false, dest,
967 (reg_class_t) sclass,
968 GET_MODE (src), &sri);
969 if (sclass == NO_REGS
970 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
971 && dclass != NO_REGS))
973 enum reg_class old_sclass = secondary_class;
974 secondary_reload_info old_sri = sri;
976 sri.prev_sri = NULL;
977 sri.icode = CODE_FOR_nothing;
978 sri.extra_cost = 0;
979 secondary_class
980 = (enum reg_class) targetm.secondary_reload (true, sreg,
981 (reg_class_t) dclass,
982 sreg_mode, &sri);
983 /* Check the target hook consistency. */
984 lra_assert
985 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
986 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
987 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
989 if (sregno >= 0)
990 reg_renumber [sregno] = -1;
991 if (dregno >= 0)
992 reg_renumber [dregno] = -1;
993 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
994 return false;
995 *change_p = true;
996 new_reg = NULL_RTX;
997 if (secondary_class != NO_REGS)
998 new_reg = lra_create_new_reg_with_unique_value (sreg_mode, NULL_RTX,
999 secondary_class,
1000 "secondary");
1001 start_sequence ();
1002 if (old_sreg != sreg)
1003 sreg = copy_rtx (sreg);
1004 if (sri.icode == CODE_FOR_nothing)
1005 lra_emit_move (new_reg, sreg);
1006 else
1008 enum reg_class scratch_class;
1010 scratch_class = (reg_class_from_constraints
1011 (insn_data[sri.icode].operand[2].constraint));
1012 scratch_reg = (lra_create_new_reg_with_unique_value
1013 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1014 scratch_class, "scratch"));
1015 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1016 sreg, scratch_reg));
1018 before = get_insns ();
1019 end_sequence ();
1020 lra_process_new_insns (curr_insn, before, NULL_RTX, "Inserting the move");
1021 if (new_reg != NULL_RTX)
1023 if (GET_CODE (src) == SUBREG)
1024 SUBREG_REG (src) = new_reg;
1025 else
1026 SET_SRC (curr_insn_set) = new_reg;
1028 else
1030 if (lra_dump_file != NULL)
1032 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1033 dump_insn_slim (lra_dump_file, curr_insn);
1035 lra_set_insn_deleted (curr_insn);
1036 return true;
1038 return false;
1041 /* The following data describe the result of process_alt_operands.
1042 The data are used in curr_insn_transform to generate reloads. */
1044 /* The chosen reg classes which should be used for the corresponding
1045 operands. */
1046 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1047 /* True if the operand should be the same as another operand and that
1048 other operand does not need a reload. */
1049 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1050 /* True if the operand does not need a reload. */
1051 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1052 /* True if the operand can be offsetable memory. */
1053 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1054 /* The number of an operand to which given operand can be matched to. */
1055 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1056 /* The number of elements in the following array. */
1057 static int goal_alt_dont_inherit_ops_num;
1058 /* Numbers of operands whose reload pseudos should not be inherited. */
1059 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1060 /* True if the insn commutative operands should be swapped. */
1061 static bool goal_alt_swapped;
1062 /* The chosen insn alternative. */
1063 static int goal_alt_number;
1065 /* The following five variables are used to choose the best insn
1066 alternative. They reflect final characteristics of the best
1067 alternative. */
1069 /* Number of necessary reloads and overall cost reflecting the
1070 previous value and other unpleasantness of the best alternative. */
1071 static int best_losers, best_overall;
1072 /* Overall number hard registers used for reloads. For example, on
1073 some targets we need 2 general registers to reload DFmode and only
1074 one floating point register. */
1075 static int best_reload_nregs;
1076 /* Overall number reflecting distances of previous reloading the same
1077 value. The distances are counted from the current BB start. It is
1078 used to improve inheritance chances. */
1079 static int best_reload_sum;
1081 /* True if the current insn should have no correspondingly input or
1082 output reloads. */
1083 static bool no_input_reloads_p, no_output_reloads_p;
1085 /* True if we swapped the commutative operands in the current
1086 insn. */
1087 static int curr_swapped;
1089 /* Arrange for address element *LOC to be a register of class CL.
1090 Add any input reloads to list BEFORE. AFTER is nonnull if *LOC is an
1091 automodified value; handle that case by adding the required output
1092 reloads to list AFTER. Return true if the RTL was changed. */
1093 static bool
1094 process_addr_reg (rtx *loc, rtx *before, rtx *after, enum reg_class cl)
1096 int regno;
1097 enum reg_class rclass, new_class;
1098 rtx reg;
1099 rtx new_reg;
1100 enum machine_mode mode;
1101 bool before_p = false;
1103 loc = strip_subreg (loc);
1104 reg = *loc;
1105 mode = GET_MODE (reg);
1106 if (! REG_P (reg))
1108 /* Always reload memory in an address even if the target supports
1109 such addresses. */
1110 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1111 before_p = true;
1113 else
1115 regno = REGNO (reg);
1116 rclass = get_reg_class (regno);
1117 if ((*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1119 if (lra_dump_file != NULL)
1121 fprintf (lra_dump_file,
1122 "Changing pseudo %d in address of insn %u on equiv ",
1123 REGNO (reg), INSN_UID (curr_insn));
1124 dump_value_slim (lra_dump_file, *loc, 1);
1125 fprintf (lra_dump_file, "\n");
1127 *loc = copy_rtx (*loc);
1129 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1131 reg = *loc;
1132 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1133 mode, reg, cl, "address", &new_reg))
1134 before_p = true;
1136 else if (new_class != NO_REGS && rclass != new_class)
1138 lra_change_class (regno, new_class, " Change to", true);
1139 return false;
1141 else
1142 return false;
1144 if (before_p)
1146 push_to_sequence (*before);
1147 lra_emit_move (new_reg, reg);
1148 *before = get_insns ();
1149 end_sequence ();
1151 *loc = new_reg;
1152 if (after != NULL)
1154 start_sequence ();
1155 lra_emit_move (reg, new_reg);
1156 emit_insn (*after);
1157 *after = get_insns ();
1158 end_sequence ();
1160 return true;
1163 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1164 the insn to be inserted before curr insn. AFTER returns the
1165 the insn to be inserted after curr insn. ORIGREG and NEWREG
1166 are the original reg and new reg for reload. */
1167 static void
1168 insert_move_for_subreg (rtx *before, rtx *after, rtx origreg, rtx newreg)
1170 if (before)
1172 push_to_sequence (*before);
1173 lra_emit_move (newreg, origreg);
1174 *before = get_insns ();
1175 end_sequence ();
1177 if (after)
1179 start_sequence ();
1180 lra_emit_move (origreg, newreg);
1181 emit_insn (*after);
1182 *after = get_insns ();
1183 end_sequence ();
1187 /* Make reloads for subreg in operand NOP with internal subreg mode
1188 REG_MODE, add new reloads for further processing. Return true if
1189 any reload was generated. */
1190 static bool
1191 simplify_operand_subreg (int nop, enum machine_mode reg_mode)
1193 int hard_regno;
1194 rtx before, after;
1195 enum machine_mode mode;
1196 rtx reg, new_reg;
1197 rtx operand = *curr_id->operand_loc[nop];
1198 enum reg_class regclass;
1199 enum op_type type;
1201 before = after = NULL_RTX;
1203 if (GET_CODE (operand) != SUBREG)
1204 return false;
1206 mode = GET_MODE (operand);
1207 reg = SUBREG_REG (operand);
1208 type = curr_static_id->operand[nop].type;
1209 /* If we change address for paradoxical subreg of memory, the
1210 address might violate the necessary alignment or the access might
1211 be slow. So take this into consideration. We should not worry
1212 about access beyond allocated memory for paradoxical memory
1213 subregs as we don't substitute such equiv memory (see processing
1214 equivalences in function lra_constraints) and because for spilled
1215 pseudos we allocate stack memory enough for the biggest
1216 corresponding paradoxical subreg. */
1217 if ((MEM_P (reg)
1218 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1219 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1220 || (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER))
1222 alter_subreg (curr_id->operand_loc[nop], false);
1223 return true;
1225 /* Put constant into memory when we have mixed modes. It generates
1226 a better code in most cases as it does not need a secondary
1227 reload memory. It also prevents LRA looping when LRA is using
1228 secondary reload memory again and again. */
1229 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1230 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1232 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1233 alter_subreg (curr_id->operand_loc[nop], false);
1234 return true;
1236 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1237 if there may be a problem accessing OPERAND in the outer
1238 mode. */
1239 if ((REG_P (reg)
1240 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1241 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1242 /* Don't reload paradoxical subregs because we could be looping
1243 having repeatedly final regno out of hard regs range. */
1244 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1245 >= hard_regno_nregs[hard_regno][mode])
1246 && simplify_subreg_regno (hard_regno, GET_MODE (reg),
1247 SUBREG_BYTE (operand), mode) < 0
1248 /* Don't reload subreg for matching reload. It is actually
1249 valid subreg in LRA. */
1250 && ! LRA_SUBREG_P (operand))
1251 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1253 /* The class will be defined later in curr_insn_transform. */
1254 enum reg_class rclass
1255 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1257 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1258 rclass, "subreg reg", &new_reg))
1260 bool insert_before, insert_after;
1261 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1263 insert_before = (type != OP_OUT
1264 || GET_MODE_SIZE (GET_MODE (reg)) > GET_MODE_SIZE (mode));
1265 insert_after = (type != OP_IN);
1266 insert_move_for_subreg (insert_before ? &before : NULL,
1267 insert_after ? &after : NULL,
1268 reg, new_reg);
1270 SUBREG_REG (operand) = new_reg;
1271 lra_process_new_insns (curr_insn, before, after,
1272 "Inserting subreg reload");
1273 return true;
1275 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1276 IRA allocates hardreg to the inner pseudo reg according to its mode
1277 instead of the outermode, so the size of the hardreg may not be enough
1278 to contain the outermode operand, in that case we may need to insert
1279 reload for the reg. For the following two types of paradoxical subreg,
1280 we need to insert reload:
1281 1. If the op_type is OP_IN, and the hardreg could not be paired with
1282 other hardreg to contain the outermode operand
1283 (checked by in_hard_reg_set_p), we need to insert the reload.
1284 2. If the op_type is OP_OUT or OP_INOUT.
1286 Here is a paradoxical subreg example showing how the reload is generated:
1288 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1289 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1291 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1292 here, if reg107 is assigned to hardreg R15, because R15 is the last
1293 hardreg, compiler cannot find another hardreg to pair with R15 to
1294 contain TImode data. So we insert a TImode reload reg180 for it.
1295 After reload is inserted:
1297 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1298 (reg:DI 107 [ __comp ])) -1
1299 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1300 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1302 Two reload hard registers will be allocated to reg180 to save TImode data
1303 in LRA_assign. */
1304 else if (REG_P (reg)
1305 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1306 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1307 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1308 < hard_regno_nregs[hard_regno][mode])
1309 && (regclass = lra_get_allocno_class (REGNO (reg)))
1310 && (type != OP_IN
1311 || !in_hard_reg_set_p (reg_class_contents[regclass],
1312 mode, hard_regno)))
1314 /* The class will be defined later in curr_insn_transform. */
1315 enum reg_class rclass
1316 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1318 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1319 rclass, "paradoxical subreg", &new_reg))
1321 rtx subreg;
1322 bool insert_before, insert_after;
1324 PUT_MODE (new_reg, mode);
1325 subreg = simplify_gen_subreg (GET_MODE (reg), new_reg, mode, 0);
1326 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1328 insert_before = (type != OP_OUT);
1329 insert_after = (type != OP_IN);
1330 insert_move_for_subreg (insert_before ? &before : NULL,
1331 insert_after ? &after : NULL,
1332 reg, subreg);
1334 SUBREG_REG (operand) = new_reg;
1335 lra_process_new_insns (curr_insn, before, after,
1336 "Inserting paradoxical subreg reload");
1337 return true;
1339 return false;
1342 /* Return TRUE if X refers for a hard register from SET. */
1343 static bool
1344 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1346 int i, j, x_hard_regno;
1347 enum machine_mode mode;
1348 const char *fmt;
1349 enum rtx_code code;
1351 if (x == NULL_RTX)
1352 return false;
1353 code = GET_CODE (x);
1354 mode = GET_MODE (x);
1355 if (code == SUBREG)
1357 x = SUBREG_REG (x);
1358 code = GET_CODE (x);
1359 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1360 mode = GET_MODE (x);
1363 if (REG_P (x))
1365 x_hard_regno = get_hard_regno (x);
1366 return (x_hard_regno >= 0
1367 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1369 if (MEM_P (x))
1371 struct address_info ad;
1373 decompose_mem_address (&ad, x);
1374 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1375 return true;
1376 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1377 return true;
1379 fmt = GET_RTX_FORMAT (code);
1380 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1382 if (fmt[i] == 'e')
1384 if (uses_hard_regs_p (XEXP (x, i), set))
1385 return true;
1387 else if (fmt[i] == 'E')
1389 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1390 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1391 return true;
1394 return false;
1397 /* Return true if OP is a spilled pseudo. */
1398 static inline bool
1399 spilled_pseudo_p (rtx op)
1401 return (REG_P (op)
1402 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1405 /* Return true if X is a general constant. */
1406 static inline bool
1407 general_constant_p (rtx x)
1409 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1412 static bool
1413 reg_in_class_p (rtx reg, enum reg_class cl)
1415 if (cl == NO_REGS)
1416 return get_reg_class (REGNO (reg)) == NO_REGS;
1417 return in_class_p (reg, cl, NULL);
1420 /* Major function to choose the current insn alternative and what
1421 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1422 negative we should consider only this alternative. Return false if
1423 we can not choose the alternative or find how to reload the
1424 operands. */
1425 static bool
1426 process_alt_operands (int only_alternative)
1428 bool ok_p = false;
1429 int nop, overall, nalt;
1430 int n_alternatives = curr_static_id->n_alternatives;
1431 int n_operands = curr_static_id->n_operands;
1432 /* LOSERS counts the operands that don't fit this alternative and
1433 would require loading. */
1434 int losers;
1435 /* REJECT is a count of how undesirable this alternative says it is
1436 if any reloading is required. If the alternative matches exactly
1437 then REJECT is ignored, but otherwise it gets this much counted
1438 against it in addition to the reloading needed. */
1439 int reject;
1440 /* The number of elements in the following array. */
1441 int early_clobbered_regs_num;
1442 /* Numbers of operands which are early clobber registers. */
1443 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1444 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1445 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1446 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1447 bool curr_alt_win[MAX_RECOG_OPERANDS];
1448 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1449 int curr_alt_matches[MAX_RECOG_OPERANDS];
1450 /* The number of elements in the following array. */
1451 int curr_alt_dont_inherit_ops_num;
1452 /* Numbers of operands whose reload pseudos should not be inherited. */
1453 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1454 rtx op;
1455 /* The register when the operand is a subreg of register, otherwise the
1456 operand itself. */
1457 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1458 /* The register if the operand is a register or subreg of register,
1459 otherwise NULL. */
1460 rtx operand_reg[MAX_RECOG_OPERANDS];
1461 int hard_regno[MAX_RECOG_OPERANDS];
1462 enum machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1463 int reload_nregs, reload_sum;
1464 bool costly_p;
1465 enum reg_class cl;
1467 /* Calculate some data common for all alternatives to speed up the
1468 function. */
1469 for (nop = 0; nop < n_operands; nop++)
1471 rtx reg;
1473 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1474 /* The real hard regno of the operand after the allocation. */
1475 hard_regno[nop] = get_hard_regno (op);
1477 operand_reg[nop] = reg = op;
1478 biggest_mode[nop] = GET_MODE (op);
1479 if (GET_CODE (op) == SUBREG)
1481 operand_reg[nop] = reg = SUBREG_REG (op);
1482 if (GET_MODE_SIZE (biggest_mode[nop])
1483 < GET_MODE_SIZE (GET_MODE (reg)))
1484 biggest_mode[nop] = GET_MODE (reg);
1486 if (! REG_P (reg))
1487 operand_reg[nop] = NULL_RTX;
1488 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1489 || ((int) REGNO (reg)
1490 == lra_get_elimination_hard_regno (REGNO (reg))))
1491 no_subreg_reg_operand[nop] = reg;
1492 else
1493 operand_reg[nop] = no_subreg_reg_operand[nop]
1494 /* Just use natural mode for elimination result. It should
1495 be enough for extra constraints hooks. */
1496 = regno_reg_rtx[hard_regno[nop]];
1499 /* The constraints are made of several alternatives. Each operand's
1500 constraint looks like foo,bar,... with commas separating the
1501 alternatives. The first alternatives for all operands go
1502 together, the second alternatives go together, etc.
1504 First loop over alternatives. */
1505 for (nalt = 0; nalt < n_alternatives; nalt++)
1507 /* Loop over operands for one constraint alternative. */
1508 #if HAVE_ATTR_enabled
1509 if (curr_id->alternative_enabled_p != NULL
1510 && ! curr_id->alternative_enabled_p[nalt])
1511 continue;
1512 #endif
1514 if (only_alternative >= 0 && nalt != only_alternative)
1515 continue;
1518 overall = losers = reject = reload_nregs = reload_sum = 0;
1519 for (nop = 0; nop < n_operands; nop++)
1521 int inc = (curr_static_id
1522 ->operand_alternative[nalt * n_operands + nop].reject);
1523 if (lra_dump_file != NULL && inc != 0)
1524 fprintf (lra_dump_file,
1525 " Staticly defined alt reject+=%d\n", inc);
1526 reject += inc;
1528 early_clobbered_regs_num = 0;
1530 for (nop = 0; nop < n_operands; nop++)
1532 const char *p;
1533 char *end;
1534 int len, c, m, i, opalt_num, this_alternative_matches;
1535 bool win, did_match, offmemok, early_clobber_p;
1536 /* false => this operand can be reloaded somehow for this
1537 alternative. */
1538 bool badop;
1539 /* true => this operand can be reloaded if the alternative
1540 allows regs. */
1541 bool winreg;
1542 /* True if a constant forced into memory would be OK for
1543 this operand. */
1544 bool constmemok;
1545 enum reg_class this_alternative, this_costly_alternative;
1546 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1547 bool this_alternative_match_win, this_alternative_win;
1548 bool this_alternative_offmemok;
1549 bool scratch_p;
1550 enum machine_mode mode;
1552 opalt_num = nalt * n_operands + nop;
1553 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1555 /* Fast track for no constraints at all. */
1556 curr_alt[nop] = NO_REGS;
1557 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1558 curr_alt_win[nop] = true;
1559 curr_alt_match_win[nop] = false;
1560 curr_alt_offmemok[nop] = false;
1561 curr_alt_matches[nop] = -1;
1562 continue;
1565 op = no_subreg_reg_operand[nop];
1566 mode = curr_operand_mode[nop];
1568 win = did_match = winreg = offmemok = constmemok = false;
1569 badop = true;
1571 early_clobber_p = false;
1572 p = curr_static_id->operand_alternative[opalt_num].constraint;
1574 this_costly_alternative = this_alternative = NO_REGS;
1575 /* We update set of possible hard regs besides its class
1576 because reg class might be inaccurate. For example,
1577 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1578 is translated in HI_REGS because classes are merged by
1579 pairs and there is no accurate intermediate class. */
1580 CLEAR_HARD_REG_SET (this_alternative_set);
1581 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1582 this_alternative_win = false;
1583 this_alternative_match_win = false;
1584 this_alternative_offmemok = false;
1585 this_alternative_matches = -1;
1587 /* An empty constraint should be excluded by the fast
1588 track. */
1589 lra_assert (*p != 0 && *p != ',');
1591 /* Scan this alternative's specs for this operand; set WIN
1592 if the operand fits any letter in this alternative.
1593 Otherwise, clear BADOP if this operand could fit some
1594 letter after reloads, or set WINREG if this operand could
1595 fit after reloads provided the constraint allows some
1596 registers. */
1597 costly_p = false;
1600 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1602 case '\0':
1603 len = 0;
1604 break;
1605 case ',':
1606 c = '\0';
1607 break;
1609 case '=': case '+': case '?': case '*': case '!':
1610 case ' ': case '\t':
1611 break;
1613 case '%':
1614 /* We only support one commutative marker, the first
1615 one. We already set commutative above. */
1616 break;
1618 case '&':
1619 early_clobber_p = true;
1620 break;
1622 case '#':
1623 /* Ignore rest of this alternative. */
1624 c = '\0';
1625 break;
1627 case '0': case '1': case '2': case '3': case '4':
1628 case '5': case '6': case '7': case '8': case '9':
1630 int m_hregno;
1631 bool match_p;
1633 m = strtoul (p, &end, 10);
1634 p = end;
1635 len = 0;
1636 lra_assert (nop > m);
1638 this_alternative_matches = m;
1639 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1640 /* We are supposed to match a previous operand.
1641 If we do, we win if that one did. If we do
1642 not, count both of the operands as losers.
1643 (This is too conservative, since most of the
1644 time only a single reload insn will be needed
1645 to make the two operands win. As a result,
1646 this alternative may be rejected when it is
1647 actually desirable.) */
1648 match_p = false;
1649 if (operands_match_p (*curr_id->operand_loc[nop],
1650 *curr_id->operand_loc[m], m_hregno))
1652 /* We should reject matching of an early
1653 clobber operand if the matching operand is
1654 not dying in the insn. */
1655 if (! curr_static_id->operand[m].early_clobber
1656 || operand_reg[nop] == NULL_RTX
1657 || (find_regno_note (curr_insn, REG_DEAD,
1658 REGNO (op))
1659 || REGNO (op) == REGNO (operand_reg[m])))
1660 match_p = true;
1662 if (match_p)
1664 /* If we are matching a non-offsettable
1665 address where an offsettable address was
1666 expected, then we must reject this
1667 combination, because we can't reload
1668 it. */
1669 if (curr_alt_offmemok[m]
1670 && MEM_P (*curr_id->operand_loc[m])
1671 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1672 continue;
1675 else
1677 /* Operands don't match. Both operands must
1678 allow a reload register, otherwise we
1679 cannot make them match. */
1680 if (curr_alt[m] == NO_REGS)
1681 break;
1682 /* Retroactively mark the operand we had to
1683 match as a loser, if it wasn't already and
1684 it wasn't matched to a register constraint
1685 (e.g it might be matched by memory). */
1686 if (curr_alt_win[m]
1687 && (operand_reg[m] == NULL_RTX
1688 || hard_regno[m] < 0))
1690 losers++;
1691 reload_nregs
1692 += (ira_reg_class_max_nregs[curr_alt[m]]
1693 [GET_MODE (*curr_id->operand_loc[m])]);
1696 /* We prefer no matching alternatives because
1697 it gives more freedom in RA. */
1698 if (operand_reg[nop] == NULL_RTX
1699 || (find_regno_note (curr_insn, REG_DEAD,
1700 REGNO (operand_reg[nop]))
1701 == NULL_RTX))
1703 if (lra_dump_file != NULL)
1704 fprintf
1705 (lra_dump_file,
1706 " %d Matching alt: reject+=2\n",
1707 nop);
1708 reject += 2;
1711 /* If we have to reload this operand and some
1712 previous operand also had to match the same
1713 thing as this operand, we don't know how to do
1714 that. */
1715 if (!match_p || !curr_alt_win[m])
1717 for (i = 0; i < nop; i++)
1718 if (curr_alt_matches[i] == m)
1719 break;
1720 if (i < nop)
1721 break;
1723 else
1724 did_match = true;
1726 /* This can be fixed with reloads if the operand
1727 we are supposed to match can be fixed with
1728 reloads. */
1729 badop = false;
1730 this_alternative = curr_alt[m];
1731 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1732 winreg = this_alternative != NO_REGS;
1733 break;
1736 case 'p':
1737 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1738 ADDRESS, SCRATCH);
1739 this_alternative = reg_class_subunion[this_alternative][cl];
1740 IOR_HARD_REG_SET (this_alternative_set,
1741 reg_class_contents[cl]);
1742 if (costly_p)
1744 this_costly_alternative
1745 = reg_class_subunion[this_costly_alternative][cl];
1746 IOR_HARD_REG_SET (this_costly_alternative_set,
1747 reg_class_contents[cl]);
1749 win = true;
1750 badop = false;
1751 break;
1753 case TARGET_MEM_CONSTRAINT:
1754 if (MEM_P (op) || spilled_pseudo_p (op))
1755 win = true;
1756 /* We can put constant or pseudo value into memory
1757 to satisfy the constraint. */
1758 if (CONST_POOL_OK_P (mode, op) || REG_P (op))
1759 badop = false;
1760 constmemok = true;
1761 break;
1763 case '<':
1764 if (MEM_P (op)
1765 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1766 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1767 win = true;
1768 break;
1770 case '>':
1771 if (MEM_P (op)
1772 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1773 || GET_CODE (XEXP (op, 0)) == POST_INC))
1774 win = true;
1775 break;
1777 /* Memory op whose address is not offsettable. */
1778 case 'V':
1779 if (MEM_P (op)
1780 && ! offsettable_nonstrict_memref_p (op))
1781 win = true;
1782 break;
1784 /* Memory operand whose address is offsettable. */
1785 case 'o':
1786 if ((MEM_P (op)
1787 && offsettable_nonstrict_memref_p (op))
1788 || spilled_pseudo_p (op))
1789 win = true;
1790 /* We can put constant or pseudo value into memory
1791 or make memory address offsetable to satisfy the
1792 constraint. */
1793 if (CONST_POOL_OK_P (mode, op) || MEM_P (op) || REG_P (op))
1794 badop = false;
1795 constmemok = true;
1796 offmemok = true;
1797 break;
1799 case 'E':
1800 case 'F':
1801 if (GET_CODE (op) == CONST_DOUBLE
1802 || (GET_CODE (op) == CONST_VECTOR
1803 && (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)))
1804 win = true;
1805 break;
1807 case 'G':
1808 case 'H':
1809 if (CONST_DOUBLE_AS_FLOAT_P (op)
1810 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1811 win = true;
1812 break;
1814 case 's':
1815 if (CONST_SCALAR_INT_P (op))
1816 break;
1818 case 'i':
1819 if (general_constant_p (op))
1820 win = true;
1821 break;
1823 case 'n':
1824 if (CONST_SCALAR_INT_P (op))
1825 win = true;
1826 break;
1828 case 'I':
1829 case 'J':
1830 case 'K':
1831 case 'L':
1832 case 'M':
1833 case 'N':
1834 case 'O':
1835 case 'P':
1836 if (CONST_INT_P (op)
1837 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1838 win = true;
1839 break;
1841 case 'X':
1842 /* This constraint should be excluded by the fast
1843 track. */
1844 gcc_unreachable ();
1845 break;
1847 case 'g':
1848 if (MEM_P (op)
1849 || general_constant_p (op)
1850 || spilled_pseudo_p (op))
1851 win = true;
1852 /* Drop through into 'r' case. */
1854 case 'r':
1855 this_alternative
1856 = reg_class_subunion[this_alternative][GENERAL_REGS];
1857 IOR_HARD_REG_SET (this_alternative_set,
1858 reg_class_contents[GENERAL_REGS]);
1859 if (costly_p)
1861 this_costly_alternative
1862 = (reg_class_subunion
1863 [this_costly_alternative][GENERAL_REGS]);
1864 IOR_HARD_REG_SET (this_costly_alternative_set,
1865 reg_class_contents[GENERAL_REGS]);
1867 goto reg;
1869 default:
1870 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
1872 #ifdef EXTRA_CONSTRAINT_STR
1873 if (EXTRA_MEMORY_CONSTRAINT (c, p))
1875 if (EXTRA_CONSTRAINT_STR (op, c, p))
1876 win = true;
1877 else if (spilled_pseudo_p (op))
1878 win = true;
1880 /* If we didn't already win, we can reload
1881 constants via force_const_mem or put the
1882 pseudo value into memory, or make other
1883 memory by reloading the address like for
1884 'o'. */
1885 if (CONST_POOL_OK_P (mode, op)
1886 || MEM_P (op) || REG_P (op))
1887 badop = false;
1888 constmemok = true;
1889 offmemok = true;
1890 break;
1892 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1894 if (EXTRA_CONSTRAINT_STR (op, c, p))
1895 win = true;
1897 /* If we didn't already win, we can reload
1898 the address into a base register. */
1899 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1900 ADDRESS, SCRATCH);
1901 this_alternative
1902 = reg_class_subunion[this_alternative][cl];
1903 IOR_HARD_REG_SET (this_alternative_set,
1904 reg_class_contents[cl]);
1905 if (costly_p)
1907 this_costly_alternative
1908 = (reg_class_subunion
1909 [this_costly_alternative][cl]);
1910 IOR_HARD_REG_SET (this_costly_alternative_set,
1911 reg_class_contents[cl]);
1913 badop = false;
1914 break;
1917 if (EXTRA_CONSTRAINT_STR (op, c, p))
1918 win = true;
1919 #endif
1920 break;
1923 cl = REG_CLASS_FROM_CONSTRAINT (c, p);
1924 this_alternative = reg_class_subunion[this_alternative][cl];
1925 IOR_HARD_REG_SET (this_alternative_set,
1926 reg_class_contents[cl]);
1927 if (costly_p)
1929 this_costly_alternative
1930 = reg_class_subunion[this_costly_alternative][cl];
1931 IOR_HARD_REG_SET (this_costly_alternative_set,
1932 reg_class_contents[cl]);
1934 reg:
1935 if (mode == BLKmode)
1936 break;
1937 winreg = true;
1938 if (REG_P (op))
1940 if (hard_regno[nop] >= 0
1941 && in_hard_reg_set_p (this_alternative_set,
1942 mode, hard_regno[nop]))
1943 win = true;
1944 else if (hard_regno[nop] < 0
1945 && in_class_p (op, this_alternative, NULL))
1946 win = true;
1948 break;
1950 if (c != ' ' && c != '\t')
1951 costly_p = c == '*';
1953 while ((p += len), c);
1955 scratch_p = (operand_reg[nop] != NULL_RTX
1956 && lra_former_scratch_p (REGNO (operand_reg[nop])));
1957 /* Record which operands fit this alternative. */
1958 if (win)
1960 this_alternative_win = true;
1961 if (operand_reg[nop] != NULL_RTX)
1963 if (hard_regno[nop] >= 0)
1965 if (in_hard_reg_set_p (this_costly_alternative_set,
1966 mode, hard_regno[nop]))
1968 if (lra_dump_file != NULL)
1969 fprintf (lra_dump_file,
1970 " %d Costly set: reject++\n",
1971 nop);
1972 reject++;
1975 else
1977 /* Prefer won reg to spilled pseudo under other
1978 equal conditions for possibe inheritance. */
1979 if (! scratch_p)
1981 if (lra_dump_file != NULL)
1982 fprintf
1983 (lra_dump_file,
1984 " %d Non pseudo reload: reject++\n",
1985 nop);
1986 reject++;
1988 if (in_class_p (operand_reg[nop],
1989 this_costly_alternative, NULL))
1991 if (lra_dump_file != NULL)
1992 fprintf
1993 (lra_dump_file,
1994 " %d Non pseudo costly reload:"
1995 " reject++\n",
1996 nop);
1997 reject++;
2000 /* We simulate the behaviour of old reload here.
2001 Although scratches need hard registers and it
2002 might result in spilling other pseudos, no reload
2003 insns are generated for the scratches. So it
2004 might cost something but probably less than old
2005 reload pass believes. */
2006 if (scratch_p)
2008 if (lra_dump_file != NULL)
2009 fprintf (lra_dump_file,
2010 " %d Scratch win: reject+=2\n",
2011 nop);
2012 reject += 2;
2016 else if (did_match)
2017 this_alternative_match_win = true;
2018 else
2020 int const_to_mem = 0;
2021 bool no_regs_p;
2023 /* Never do output reload of stack pointer. It makes
2024 impossible to do elimination when SP is changed in
2025 RTL. */
2026 if (op == stack_pointer_rtx && ! frame_pointer_needed
2027 && curr_static_id->operand[nop].type != OP_IN)
2028 goto fail;
2030 /* If this alternative asks for a specific reg class, see if there
2031 is at least one allocatable register in that class. */
2032 no_regs_p
2033 = (this_alternative == NO_REGS
2034 || (hard_reg_set_subset_p
2035 (reg_class_contents[this_alternative],
2036 lra_no_alloc_regs)));
2038 /* For asms, verify that the class for this alternative is possible
2039 for the mode that is specified. */
2040 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2042 int i;
2043 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2044 if (HARD_REGNO_MODE_OK (i, mode)
2045 && in_hard_reg_set_p (reg_class_contents[this_alternative], mode, i))
2046 break;
2047 if (i == FIRST_PSEUDO_REGISTER)
2048 winreg = false;
2051 /* If this operand accepts a register, and if the
2052 register class has at least one allocatable register,
2053 then this operand can be reloaded. */
2054 if (winreg && !no_regs_p)
2055 badop = false;
2057 if (badop)
2058 goto fail;
2060 this_alternative_offmemok = offmemok;
2061 if (this_costly_alternative != NO_REGS)
2063 if (lra_dump_file != NULL)
2064 fprintf (lra_dump_file,
2065 " %d Costly loser: reject++\n", nop);
2066 reject++;
2068 /* If the operand is dying, has a matching constraint,
2069 and satisfies constraints of the matched operand
2070 which failed to satisfy the own constraints, we do
2071 not need to generate a reload insn for this
2072 operand. */
2073 if (!(this_alternative_matches >= 0
2074 && !curr_alt_win[this_alternative_matches]
2075 && REG_P (op)
2076 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2077 && (hard_regno[nop] >= 0
2078 ? in_hard_reg_set_p (this_alternative_set,
2079 mode, hard_regno[nop])
2080 : in_class_p (op, this_alternative, NULL))))
2082 /* Strict_low_part requires to reload the register
2083 not the sub-register. In this case we should
2084 check that a final reload hard reg can hold the
2085 value mode. */
2086 if (curr_static_id->operand[nop].strict_low
2087 && REG_P (op)
2088 && hard_regno[nop] < 0
2089 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2090 && ira_class_hard_regs_num[this_alternative] > 0
2091 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2092 [this_alternative][0],
2093 GET_MODE
2094 (*curr_id->operand_loc[nop])))
2095 goto fail;
2096 losers++;
2098 if (operand_reg[nop] != NULL_RTX
2099 /* Output operands and matched input operands are
2100 not inherited. The following conditions do not
2101 exactly describe the previous statement but they
2102 are pretty close. */
2103 && curr_static_id->operand[nop].type != OP_OUT
2104 && (this_alternative_matches < 0
2105 || curr_static_id->operand[nop].type != OP_IN))
2107 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2108 (operand_reg[nop])]
2109 .last_reload);
2111 if (last_reload > bb_reload_num)
2112 reload_sum += last_reload - bb_reload_num;
2114 /* If this is a constant that is reloaded into the
2115 desired class by copying it to memory first, count
2116 that as another reload. This is consistent with
2117 other code and is required to avoid choosing another
2118 alternative when the constant is moved into memory.
2119 Note that the test here is precisely the same as in
2120 the code below that calls force_const_mem. */
2121 if (CONST_POOL_OK_P (mode, op)
2122 && ((targetm.preferred_reload_class
2123 (op, this_alternative) == NO_REGS)
2124 || no_input_reloads_p))
2126 const_to_mem = 1;
2127 if (! no_regs_p)
2128 losers++;
2131 /* Alternative loses if it requires a type of reload not
2132 permitted for this insn. We can always reload
2133 objects with a REG_UNUSED note. */
2134 if ((curr_static_id->operand[nop].type != OP_IN
2135 && no_output_reloads_p
2136 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2137 || (curr_static_id->operand[nop].type != OP_OUT
2138 && no_input_reloads_p && ! const_to_mem))
2139 goto fail;
2141 /* Check strong discouragement of reload of non-constant
2142 into class THIS_ALTERNATIVE. */
2143 if (! CONSTANT_P (op) && ! no_regs_p
2144 && (targetm.preferred_reload_class
2145 (op, this_alternative) == NO_REGS
2146 || (curr_static_id->operand[nop].type == OP_OUT
2147 && (targetm.preferred_output_reload_class
2148 (op, this_alternative) == NO_REGS))))
2150 if (lra_dump_file != NULL)
2151 fprintf (lra_dump_file,
2152 " %d Non-prefered reload: reject+=%d\n",
2153 nop, LRA_MAX_REJECT);
2154 reject += LRA_MAX_REJECT;
2157 if (! (MEM_P (op) && offmemok)
2158 && ! (const_to_mem && constmemok))
2160 /* We prefer to reload pseudos over reloading other
2161 things, since such reloads may be able to be
2162 eliminated later. So bump REJECT in other cases.
2163 Don't do this in the case where we are forcing a
2164 constant into memory and it will then win since
2165 we don't want to have a different alternative
2166 match then. */
2167 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2169 if (lra_dump_file != NULL)
2170 fprintf
2171 (lra_dump_file,
2172 " %d Non-pseudo reload: reject+=2\n",
2173 nop);
2174 reject += 2;
2177 if (! no_regs_p)
2178 reload_nregs
2179 += ira_reg_class_max_nregs[this_alternative][mode];
2181 if (SMALL_REGISTER_CLASS_P (this_alternative))
2183 if (lra_dump_file != NULL)
2184 fprintf
2185 (lra_dump_file,
2186 " %d Small class reload: reject+=%d\n",
2187 nop, LRA_LOSER_COST_FACTOR / 2);
2188 reject += LRA_LOSER_COST_FACTOR / 2;
2192 /* We are trying to spill pseudo into memory. It is
2193 usually more costly than moving to a hard register
2194 although it might takes the same number of
2195 reloads. */
2196 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2198 if (lra_dump_file != NULL)
2199 fprintf
2200 (lra_dump_file,
2201 " %d Spill pseudo in memory: reject+=3\n",
2202 nop);
2203 reject += 3;
2206 #ifdef SECONDARY_MEMORY_NEEDED
2207 /* If reload requires moving value through secondary
2208 memory, it will need one more insn at least. */
2209 if (this_alternative != NO_REGS
2210 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2211 && ((curr_static_id->operand[nop].type != OP_OUT
2212 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2213 GET_MODE (op)))
2214 || (curr_static_id->operand[nop].type != OP_IN
2215 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2216 GET_MODE (op)))))
2217 losers++;
2218 #endif
2219 /* Input reloads can be inherited more often than output
2220 reloads can be removed, so penalize output
2221 reloads. */
2222 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2224 if (lra_dump_file != NULL)
2225 fprintf
2226 (lra_dump_file,
2227 " %d Non input pseudo reload: reject++\n",
2228 nop);
2229 reject++;
2233 if (early_clobber_p && ! scratch_p)
2235 if (lra_dump_file != NULL)
2236 fprintf (lra_dump_file,
2237 " %d Early clobber: reject++\n", nop);
2238 reject++;
2240 /* ??? We check early clobbers after processing all operands
2241 (see loop below) and there we update the costs more.
2242 Should we update the cost (may be approximately) here
2243 because of early clobber register reloads or it is a rare
2244 or non-important thing to be worth to do it. */
2245 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2246 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2248 if (lra_dump_file != NULL)
2249 fprintf (lra_dump_file,
2250 " alt=%d,overall=%d,losers=%d -- refuse\n",
2251 nalt, overall, losers);
2252 goto fail;
2255 curr_alt[nop] = this_alternative;
2256 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2257 curr_alt_win[nop] = this_alternative_win;
2258 curr_alt_match_win[nop] = this_alternative_match_win;
2259 curr_alt_offmemok[nop] = this_alternative_offmemok;
2260 curr_alt_matches[nop] = this_alternative_matches;
2262 if (this_alternative_matches >= 0
2263 && !did_match && !this_alternative_win)
2264 curr_alt_win[this_alternative_matches] = false;
2266 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2267 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2269 if (curr_insn_set != NULL_RTX && n_operands == 2
2270 /* Prevent processing non-move insns. */
2271 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2272 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2273 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2274 && REG_P (no_subreg_reg_operand[0])
2275 && REG_P (no_subreg_reg_operand[1])
2276 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2277 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2278 || (! curr_alt_win[0] && curr_alt_win[1]
2279 && REG_P (no_subreg_reg_operand[1])
2280 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2281 || (curr_alt_win[0] && ! curr_alt_win[1]
2282 && REG_P (no_subreg_reg_operand[0])
2283 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2284 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2285 no_subreg_reg_operand[1])
2286 || (targetm.preferred_reload_class
2287 (no_subreg_reg_operand[1],
2288 (enum reg_class) curr_alt[1]) != NO_REGS))
2289 /* If it is a result of recent elimination in move
2290 insn we can transform it into an add still by
2291 using this alternative. */
2292 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2294 /* We have a move insn and a new reload insn will be similar
2295 to the current insn. We should avoid such situation as it
2296 results in LRA cycling. */
2297 overall += LRA_MAX_REJECT;
2299 ok_p = true;
2300 curr_alt_dont_inherit_ops_num = 0;
2301 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2303 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2304 HARD_REG_SET temp_set;
2306 i = early_clobbered_nops[nop];
2307 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2308 || hard_regno[i] < 0)
2309 continue;
2310 lra_assert (operand_reg[i] != NULL_RTX);
2311 clobbered_hard_regno = hard_regno[i];
2312 CLEAR_HARD_REG_SET (temp_set);
2313 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2314 first_conflict_j = last_conflict_j = -1;
2315 for (j = 0; j < n_operands; j++)
2316 if (j == i
2317 /* We don't want process insides of match_operator and
2318 match_parallel because otherwise we would process
2319 their operands once again generating a wrong
2320 code. */
2321 || curr_static_id->operand[j].is_operator)
2322 continue;
2323 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2324 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2325 continue;
2326 /* If we don't reload j-th operand, check conflicts. */
2327 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2328 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2330 if (first_conflict_j < 0)
2331 first_conflict_j = j;
2332 last_conflict_j = j;
2334 if (last_conflict_j < 0)
2335 continue;
2336 /* If earlyclobber operand conflicts with another
2337 non-matching operand which is actually the same register
2338 as the earlyclobber operand, it is better to reload the
2339 another operand as an operand matching the earlyclobber
2340 operand can be also the same. */
2341 if (first_conflict_j == last_conflict_j
2342 && operand_reg[last_conflict_j]
2343 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2344 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2346 curr_alt_win[last_conflict_j] = false;
2347 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2348 = last_conflict_j;
2349 losers++;
2350 /* Early clobber was already reflected in REJECT. */
2351 lra_assert (reject > 0);
2352 if (lra_dump_file != NULL)
2353 fprintf
2354 (lra_dump_file,
2355 " %d Conflict early clobber reload: reject--\n",
2357 reject--;
2358 overall += LRA_LOSER_COST_FACTOR - 1;
2360 else
2362 /* We need to reload early clobbered register and the
2363 matched registers. */
2364 for (j = 0; j < n_operands; j++)
2365 if (curr_alt_matches[j] == i)
2367 curr_alt_match_win[j] = false;
2368 losers++;
2369 overall += LRA_LOSER_COST_FACTOR;
2371 if (! curr_alt_match_win[i])
2372 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2373 else
2375 /* Remember pseudos used for match reloads are never
2376 inherited. */
2377 lra_assert (curr_alt_matches[i] >= 0);
2378 curr_alt_win[curr_alt_matches[i]] = false;
2380 curr_alt_win[i] = curr_alt_match_win[i] = false;
2381 losers++;
2382 /* Early clobber was already reflected in REJECT. */
2383 lra_assert (reject > 0);
2384 if (lra_dump_file != NULL)
2385 fprintf
2386 (lra_dump_file,
2387 " %d Matched conflict early clobber reloads:"
2388 "reject--\n",
2390 reject--;
2391 overall += LRA_LOSER_COST_FACTOR - 1;
2394 if (lra_dump_file != NULL)
2395 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2396 nalt, overall, losers, reload_nregs);
2398 /* If this alternative can be made to work by reloading, and it
2399 needs less reloading than the others checked so far, record
2400 it as the chosen goal for reloading. */
2401 if ((best_losers != 0 && losers == 0)
2402 || (((best_losers == 0 && losers == 0)
2403 || (best_losers != 0 && losers != 0))
2404 && (best_overall > overall
2405 || (best_overall == overall
2406 /* If the cost of the reloads is the same,
2407 prefer alternative which requires minimal
2408 number of reload regs. */
2409 && (reload_nregs < best_reload_nregs
2410 || (reload_nregs == best_reload_nregs
2411 && (best_reload_sum < reload_sum
2412 || (best_reload_sum == reload_sum
2413 && nalt < goal_alt_number))))))))
2415 for (nop = 0; nop < n_operands; nop++)
2417 goal_alt_win[nop] = curr_alt_win[nop];
2418 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2419 goal_alt_matches[nop] = curr_alt_matches[nop];
2420 goal_alt[nop] = curr_alt[nop];
2421 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2423 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2424 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2425 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2426 goal_alt_swapped = curr_swapped;
2427 best_overall = overall;
2428 best_losers = losers;
2429 best_reload_nregs = reload_nregs;
2430 best_reload_sum = reload_sum;
2431 goal_alt_number = nalt;
2433 if (losers == 0)
2434 /* Everything is satisfied. Do not process alternatives
2435 anymore. */
2436 break;
2437 fail:
2440 return ok_p;
2443 /* Return 1 if ADDR is a valid memory address for mode MODE in address
2444 space AS, and check that each pseudo has the proper kind of hard
2445 reg. */
2446 static int
2447 valid_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2448 rtx addr, addr_space_t as)
2450 #ifdef GO_IF_LEGITIMATE_ADDRESS
2451 lra_assert (ADDR_SPACE_GENERIC_P (as));
2452 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
2453 return 0;
2455 win:
2456 return 1;
2457 #else
2458 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
2459 #endif
2462 /* Return whether address AD is valid. */
2464 static bool
2465 valid_address_p (struct address_info *ad)
2467 /* Some ports do not check displacements for eliminable registers,
2468 so we replace them temporarily with the elimination target. */
2469 rtx saved_base_reg = NULL_RTX;
2470 rtx saved_index_reg = NULL_RTX;
2471 rtx *base_term = strip_subreg (ad->base_term);
2472 rtx *index_term = strip_subreg (ad->index_term);
2473 if (base_term != NULL)
2475 saved_base_reg = *base_term;
2476 lra_eliminate_reg_if_possible (base_term);
2477 if (ad->base_term2 != NULL)
2478 *ad->base_term2 = *ad->base_term;
2480 if (index_term != NULL)
2482 saved_index_reg = *index_term;
2483 lra_eliminate_reg_if_possible (index_term);
2485 bool ok_p = valid_address_p (ad->mode, *ad->outer, ad->as);
2486 if (saved_base_reg != NULL_RTX)
2488 *base_term = saved_base_reg;
2489 if (ad->base_term2 != NULL)
2490 *ad->base_term2 = *ad->base_term;
2492 if (saved_index_reg != NULL_RTX)
2493 *index_term = saved_index_reg;
2494 return ok_p;
2497 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2498 static rtx
2499 base_plus_disp_to_reg (struct address_info *ad)
2501 enum reg_class cl;
2502 rtx new_reg;
2504 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2505 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2506 get_index_code (ad));
2507 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2508 cl, "base + disp");
2509 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2510 return new_reg;
2513 /* Return true if we can add a displacement to address AD, even if that
2514 makes the address invalid. The fix-up code requires any new address
2515 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2516 static bool
2517 can_add_disp_p (struct address_info *ad)
2519 return (!ad->autoinc_p
2520 && ad->segment == NULL
2521 && ad->base == ad->base_term
2522 && ad->disp == ad->disp_term);
2525 /* Make equiv substitution in address AD. Return true if a substitution
2526 was made. */
2527 static bool
2528 equiv_address_substitution (struct address_info *ad)
2530 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2531 HOST_WIDE_INT disp, scale;
2532 bool change_p;
2534 base_term = strip_subreg (ad->base_term);
2535 if (base_term == NULL)
2536 base_reg = new_base_reg = NULL_RTX;
2537 else
2539 base_reg = *base_term;
2540 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2542 index_term = strip_subreg (ad->index_term);
2543 if (index_term == NULL)
2544 index_reg = new_index_reg = NULL_RTX;
2545 else
2547 index_reg = *index_term;
2548 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2550 if (base_reg == new_base_reg && index_reg == new_index_reg)
2551 return false;
2552 disp = 0;
2553 change_p = false;
2554 if (lra_dump_file != NULL)
2556 fprintf (lra_dump_file, "Changing address in insn %d ",
2557 INSN_UID (curr_insn));
2558 dump_value_slim (lra_dump_file, *ad->outer, 1);
2560 if (base_reg != new_base_reg)
2562 if (REG_P (new_base_reg))
2564 *base_term = new_base_reg;
2565 change_p = true;
2567 else if (GET_CODE (new_base_reg) == PLUS
2568 && REG_P (XEXP (new_base_reg, 0))
2569 && CONST_INT_P (XEXP (new_base_reg, 1))
2570 && can_add_disp_p (ad))
2572 disp += INTVAL (XEXP (new_base_reg, 1));
2573 *base_term = XEXP (new_base_reg, 0);
2574 change_p = true;
2576 if (ad->base_term2 != NULL)
2577 *ad->base_term2 = *ad->base_term;
2579 if (index_reg != new_index_reg)
2581 if (REG_P (new_index_reg))
2583 *index_term = new_index_reg;
2584 change_p = true;
2586 else if (GET_CODE (new_index_reg) == PLUS
2587 && REG_P (XEXP (new_index_reg, 0))
2588 && CONST_INT_P (XEXP (new_index_reg, 1))
2589 && can_add_disp_p (ad)
2590 && (scale = get_index_scale (ad)))
2592 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2593 *index_term = XEXP (new_index_reg, 0);
2594 change_p = true;
2597 if (disp != 0)
2599 if (ad->disp != NULL)
2600 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2601 else
2603 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2604 update_address (ad);
2606 change_p = true;
2608 if (lra_dump_file != NULL)
2610 if (! change_p)
2611 fprintf (lra_dump_file, " -- no change\n");
2612 else
2614 fprintf (lra_dump_file, " on equiv ");
2615 dump_value_slim (lra_dump_file, *ad->outer, 1);
2616 fprintf (lra_dump_file, "\n");
2619 return change_p;
2622 /* Major function to make reloads for an address in operand NOP.
2623 The supported cases are:
2625 1) an address that existed before LRA started, at which point it
2626 must have been valid. These addresses are subject to elimination
2627 and may have become invalid due to the elimination offset being out
2628 of range.
2630 2) an address created by forcing a constant to memory
2631 (force_const_to_mem). The initial form of these addresses might
2632 not be valid, and it is this function's job to make them valid.
2634 3) a frame address formed from a register and a (possibly zero)
2635 constant offset. As above, these addresses might not be valid and
2636 this function must make them so.
2638 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2639 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2640 address. Return true for any RTL change. */
2641 static bool
2642 process_address (int nop, rtx *before, rtx *after)
2644 struct address_info ad;
2645 rtx new_reg;
2646 rtx op = *curr_id->operand_loc[nop];
2647 const char *constraint = curr_static_id->operand[nop].constraint;
2648 bool change_p;
2650 if (constraint[0] == 'p'
2651 || EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint))
2652 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2653 else if (MEM_P (op))
2654 decompose_mem_address (&ad, op);
2655 else if (GET_CODE (op) == SUBREG
2656 && MEM_P (SUBREG_REG (op)))
2657 decompose_mem_address (&ad, SUBREG_REG (op));
2658 else
2659 return false;
2660 change_p = equiv_address_substitution (&ad);
2661 if (ad.base_term != NULL
2662 && (process_addr_reg
2663 (ad.base_term, before,
2664 (ad.autoinc_p
2665 && !(REG_P (*ad.base_term)
2666 && find_regno_note (curr_insn, REG_DEAD,
2667 REGNO (*ad.base_term)) != NULL_RTX)
2668 ? after : NULL),
2669 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2670 get_index_code (&ad)))))
2672 change_p = true;
2673 if (ad.base_term2 != NULL)
2674 *ad.base_term2 = *ad.base_term;
2676 if (ad.index_term != NULL
2677 && process_addr_reg (ad.index_term, before, NULL, INDEX_REG_CLASS))
2678 change_p = true;
2680 #ifdef EXTRA_CONSTRAINT_STR
2681 /* Target hooks sometimes reject extra constraint addresses -- use
2682 EXTRA_CONSTRAINT_STR for the validation. */
2683 if (constraint[0] != 'p'
2684 && EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint)
2685 && EXTRA_CONSTRAINT_STR (op, constraint[0], constraint))
2686 return change_p;
2687 #endif
2689 /* There are three cases where the shape of *AD.INNER may now be invalid:
2691 1) the original address was valid, but either elimination or
2692 equiv_address_substitution was applied and that made
2693 the address invalid.
2695 2) the address is an invalid symbolic address created by
2696 force_const_to_mem.
2698 3) the address is a frame address with an invalid offset.
2700 All these cases involve a non-autoinc address, so there is no
2701 point revalidating other types. */
2702 if (ad.autoinc_p || valid_address_p (&ad))
2703 return change_p;
2705 /* Any index existed before LRA started, so we can assume that the
2706 presence and shape of the index is valid. */
2707 push_to_sequence (*before);
2708 lra_assert (ad.disp == ad.disp_term);
2709 if (ad.base == NULL)
2711 if (ad.index == NULL)
2713 int code = -1;
2714 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2715 SCRATCH, SCRATCH);
2716 rtx addr = *ad.inner;
2718 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2719 #ifdef HAVE_lo_sum
2721 rtx insn;
2722 rtx last = get_last_insn ();
2724 /* addr => lo_sum (new_base, addr), case (2) above. */
2725 insn = emit_insn (gen_rtx_SET
2726 (VOIDmode, new_reg,
2727 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2728 code = recog_memoized (insn);
2729 if (code >= 0)
2731 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2732 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2734 /* Try to put lo_sum into register. */
2735 insn = emit_insn (gen_rtx_SET
2736 (VOIDmode, new_reg,
2737 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2738 code = recog_memoized (insn);
2739 if (code >= 0)
2741 *ad.inner = new_reg;
2742 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2744 *ad.inner = addr;
2745 code = -1;
2751 if (code < 0)
2752 delete_insns_since (last);
2754 #endif
2755 if (code < 0)
2757 /* addr => new_base, case (2) above. */
2758 lra_emit_move (new_reg, addr);
2759 *ad.inner = new_reg;
2762 else
2764 /* index * scale + disp => new base + index * scale,
2765 case (1) above. */
2766 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
2767 GET_CODE (*ad.index));
2769 lra_assert (INDEX_REG_CLASS != NO_REGS);
2770 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2771 lra_emit_move (new_reg, *ad.disp);
2772 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2773 new_reg, *ad.index);
2776 else if (ad.index == NULL)
2778 int regno;
2779 enum reg_class cl;
2780 rtx set, insns, last_insn;
2781 /* base + disp => new base, cases (1) and (3) above. */
2782 /* Another option would be to reload the displacement into an
2783 index register. However, postreload has code to optimize
2784 address reloads that have the same base and different
2785 displacements, so reloading into an index register would
2786 not necessarily be a win. */
2787 start_sequence ();
2788 new_reg = base_plus_disp_to_reg (&ad);
2789 insns = get_insns ();
2790 last_insn = get_last_insn ();
2791 /* If we generated at least two insns, try last insn source as
2792 an address. If we succeed, we generate one less insn. */
2793 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
2794 && GET_CODE (SET_SRC (set)) == PLUS
2795 && REG_P (XEXP (SET_SRC (set), 0))
2796 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
2798 *ad.inner = SET_SRC (set);
2799 if (valid_address_p (ad.mode, *ad.outer, ad.as))
2801 *ad.base_term = XEXP (SET_SRC (set), 0);
2802 *ad.disp_term = XEXP (SET_SRC (set), 1);
2803 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2804 get_index_code (&ad));
2805 regno = REGNO (*ad.base_term);
2806 if (regno >= FIRST_PSEUDO_REGISTER
2807 && cl != lra_get_allocno_class (regno))
2808 lra_change_class (regno, cl, " Change to", true);
2809 new_reg = SET_SRC (set);
2810 delete_insns_since (PREV_INSN (last_insn));
2813 end_sequence ();
2814 emit_insn (insns);
2815 *ad.inner = new_reg;
2817 else
2819 /* base + scale * index + disp => new base + scale * index,
2820 case (1) above. */
2821 new_reg = base_plus_disp_to_reg (&ad);
2822 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2823 new_reg, *ad.index);
2825 *before = get_insns ();
2826 end_sequence ();
2827 return true;
2830 /* Emit insns to reload VALUE into a new register. VALUE is an
2831 auto-increment or auto-decrement RTX whose operand is a register or
2832 memory location; so reloading involves incrementing that location.
2833 IN is either identical to VALUE, or some cheaper place to reload
2834 value being incremented/decremented from.
2836 INC_AMOUNT is the number to increment or decrement by (always
2837 positive and ignored for POST_MODIFY/PRE_MODIFY).
2839 Return pseudo containing the result. */
2840 static rtx
2841 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
2843 /* REG or MEM to be copied and incremented. */
2844 rtx incloc = XEXP (value, 0);
2845 /* Nonzero if increment after copying. */
2846 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
2847 || GET_CODE (value) == POST_MODIFY);
2848 rtx last;
2849 rtx inc;
2850 rtx add_insn;
2851 int code;
2852 rtx real_in = in == value ? incloc : in;
2853 rtx result;
2854 bool plus_p = true;
2856 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
2858 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
2859 || GET_CODE (XEXP (value, 1)) == MINUS);
2860 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
2861 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
2862 inc = XEXP (XEXP (value, 1), 1);
2864 else
2866 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
2867 inc_amount = -inc_amount;
2869 inc = GEN_INT (inc_amount);
2872 if (! post && REG_P (incloc))
2873 result = incloc;
2874 else
2875 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
2876 "INC/DEC result");
2878 if (real_in != result)
2880 /* First copy the location to the result register. */
2881 lra_assert (REG_P (result));
2882 emit_insn (gen_move_insn (result, real_in));
2885 /* We suppose that there are insns to add/sub with the constant
2886 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
2887 old reload worked with this assumption. If the assumption
2888 becomes wrong, we should use approach in function
2889 base_plus_disp_to_reg. */
2890 if (in == value)
2892 /* See if we can directly increment INCLOC. */
2893 last = get_last_insn ();
2894 add_insn = emit_insn (plus_p
2895 ? gen_add2_insn (incloc, inc)
2896 : gen_sub2_insn (incloc, inc));
2898 code = recog_memoized (add_insn);
2899 if (code >= 0)
2901 if (! post && result != incloc)
2902 emit_insn (gen_move_insn (result, incloc));
2903 return result;
2905 delete_insns_since (last);
2908 /* If couldn't do the increment directly, must increment in RESULT.
2909 The way we do this depends on whether this is pre- or
2910 post-increment. For pre-increment, copy INCLOC to the reload
2911 register, increment it there, then save back. */
2912 if (! post)
2914 if (real_in != result)
2915 emit_insn (gen_move_insn (result, real_in));
2916 if (plus_p)
2917 emit_insn (gen_add2_insn (result, inc));
2918 else
2919 emit_insn (gen_sub2_insn (result, inc));
2920 if (result != incloc)
2921 emit_insn (gen_move_insn (incloc, result));
2923 else
2925 /* Post-increment.
2927 Because this might be a jump insn or a compare, and because
2928 RESULT may not be available after the insn in an input
2929 reload, we must do the incrementing before the insn being
2930 reloaded for.
2932 We have already copied IN to RESULT. Increment the copy in
2933 RESULT, save that back, then decrement RESULT so it has
2934 the original value. */
2935 if (plus_p)
2936 emit_insn (gen_add2_insn (result, inc));
2937 else
2938 emit_insn (gen_sub2_insn (result, inc));
2939 emit_insn (gen_move_insn (incloc, result));
2940 /* Restore non-modified value for the result. We prefer this
2941 way because it does not require an additional hard
2942 register. */
2943 if (plus_p)
2945 if (CONST_INT_P (inc))
2946 emit_insn (gen_add2_insn (result,
2947 gen_int_mode (-INTVAL (inc),
2948 GET_MODE (result))));
2949 else
2950 emit_insn (gen_sub2_insn (result, inc));
2952 else
2953 emit_insn (gen_add2_insn (result, inc));
2955 return result;
2958 /* Return true if the current move insn does not need processing as we
2959 already know that it satisfies its constraints. */
2960 static bool
2961 simple_move_p (void)
2963 rtx dest, src;
2964 enum reg_class dclass, sclass;
2966 lra_assert (curr_insn_set != NULL_RTX);
2967 dest = SET_DEST (curr_insn_set);
2968 src = SET_SRC (curr_insn_set);
2969 return ((dclass = get_op_class (dest)) != NO_REGS
2970 && (sclass = get_op_class (src)) != NO_REGS
2971 /* The backend guarantees that register moves of cost 2
2972 never need reloads. */
2973 && targetm.register_move_cost (GET_MODE (src), dclass, sclass) == 2);
2976 /* Swap operands NOP and NOP + 1. */
2977 static inline void
2978 swap_operands (int nop)
2980 enum machine_mode mode = curr_operand_mode[nop];
2981 curr_operand_mode[nop] = curr_operand_mode[nop + 1];
2982 curr_operand_mode[nop + 1] = mode;
2983 rtx x = *curr_id->operand_loc[nop];
2984 *curr_id->operand_loc[nop] = *curr_id->operand_loc[nop + 1];
2985 *curr_id->operand_loc[nop + 1] = x;
2986 /* Swap the duplicates too. */
2987 lra_update_dup (curr_id, nop);
2988 lra_update_dup (curr_id, nop + 1);
2991 /* Main entry point of the constraint code: search the body of the
2992 current insn to choose the best alternative. It is mimicking insn
2993 alternative cost calculation model of former reload pass. That is
2994 because machine descriptions were written to use this model. This
2995 model can be changed in future. Make commutative operand exchange
2996 if it is chosen.
2998 Return true if some RTL changes happened during function call. */
2999 static bool
3000 curr_insn_transform (void)
3002 int i, j, k;
3003 int n_operands;
3004 int n_alternatives;
3005 int commutative;
3006 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3007 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3008 rtx before, after;
3009 bool alt_p = false;
3010 /* Flag that the insn has been changed through a transformation. */
3011 bool change_p;
3012 bool sec_mem_p;
3013 #ifdef SECONDARY_MEMORY_NEEDED
3014 bool use_sec_mem_p;
3015 #endif
3016 int max_regno_before;
3017 int reused_alternative_num;
3019 curr_insn_set = single_set (curr_insn);
3020 if (curr_insn_set != NULL_RTX && simple_move_p ())
3021 return false;
3023 no_input_reloads_p = no_output_reloads_p = false;
3024 goal_alt_number = -1;
3025 change_p = sec_mem_p = false;
3026 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3027 reloads; neither are insns that SET cc0. Insns that use CC0 are
3028 not allowed to have any input reloads. */
3029 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3030 no_output_reloads_p = true;
3032 #ifdef HAVE_cc0
3033 if (reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3034 no_input_reloads_p = true;
3035 if (reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3036 no_output_reloads_p = true;
3037 #endif
3039 n_operands = curr_static_id->n_operands;
3040 n_alternatives = curr_static_id->n_alternatives;
3042 /* Just return "no reloads" if insn has no operands with
3043 constraints. */
3044 if (n_operands == 0 || n_alternatives == 0)
3045 return false;
3047 max_regno_before = max_reg_num ();
3049 for (i = 0; i < n_operands; i++)
3051 goal_alt_matched[i][0] = -1;
3052 goal_alt_matches[i] = -1;
3055 commutative = curr_static_id->commutative;
3057 /* Now see what we need for pseudos that didn't get hard regs or got
3058 the wrong kind of hard reg. For this, we must consider all the
3059 operands together against the register constraints. */
3061 best_losers = best_overall = INT_MAX;
3062 best_reload_sum = 0;
3064 curr_swapped = false;
3065 goal_alt_swapped = false;
3067 /* Make equivalence substitution and memory subreg elimination
3068 before address processing because an address legitimacy can
3069 depend on memory mode. */
3070 for (i = 0; i < n_operands; i++)
3072 rtx op = *curr_id->operand_loc[i];
3073 rtx subst, old = op;
3074 bool op_change_p = false;
3076 if (GET_CODE (old) == SUBREG)
3077 old = SUBREG_REG (old);
3078 subst = get_equiv_with_elimination (old, curr_insn);
3079 if (subst != old)
3081 subst = copy_rtx (subst);
3082 lra_assert (REG_P (old));
3083 if (GET_CODE (op) == SUBREG)
3084 SUBREG_REG (op) = subst;
3085 else
3086 *curr_id->operand_loc[i] = subst;
3087 if (lra_dump_file != NULL)
3089 fprintf (lra_dump_file,
3090 "Changing pseudo %d in operand %i of insn %u on equiv ",
3091 REGNO (old), i, INSN_UID (curr_insn));
3092 dump_value_slim (lra_dump_file, subst, 1);
3093 fprintf (lra_dump_file, "\n");
3095 op_change_p = change_p = true;
3097 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3099 change_p = true;
3100 lra_update_dup (curr_id, i);
3104 /* Reload address registers and displacements. We do it before
3105 finding an alternative because of memory constraints. */
3106 before = after = NULL_RTX;
3107 for (i = 0; i < n_operands; i++)
3108 if (! curr_static_id->operand[i].is_operator
3109 && process_address (i, &before, &after))
3111 change_p = true;
3112 lra_update_dup (curr_id, i);
3115 if (change_p)
3116 /* If we've changed the instruction then any alternative that
3117 we chose previously may no longer be valid. */
3118 lra_set_used_insn_alternative (curr_insn, -1);
3120 if (curr_insn_set != NULL_RTX
3121 && check_and_process_move (&change_p, &sec_mem_p))
3122 return change_p;
3124 try_swapped:
3126 reused_alternative_num = curr_id->used_insn_alternative;
3127 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3128 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3129 reused_alternative_num, INSN_UID (curr_insn));
3131 if (process_alt_operands (reused_alternative_num))
3132 alt_p = true;
3134 /* If insn is commutative (it's safe to exchange a certain pair of
3135 operands) then we need to try each alternative twice, the second
3136 time matching those two operands as if we had exchanged them. To
3137 do this, really exchange them in operands.
3139 If we have just tried the alternatives the second time, return
3140 operands to normal and drop through. */
3142 if (reused_alternative_num < 0 && commutative >= 0)
3144 curr_swapped = !curr_swapped;
3145 if (curr_swapped)
3147 swap_operands (commutative);
3148 goto try_swapped;
3150 else
3151 swap_operands (commutative);
3154 if (! alt_p && ! sec_mem_p)
3156 /* No alternative works with reloads?? */
3157 if (INSN_CODE (curr_insn) >= 0)
3158 fatal_insn ("unable to generate reloads for:", curr_insn);
3159 error_for_asm (curr_insn,
3160 "inconsistent operand constraints in an %<asm%>");
3161 /* Avoid further trouble with this insn. */
3162 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3163 lra_invalidate_insn_data (curr_insn);
3164 return true;
3167 /* If the best alternative is with operands 1 and 2 swapped, swap
3168 them. Update the operand numbers of any reloads already
3169 pushed. */
3171 if (goal_alt_swapped)
3173 if (lra_dump_file != NULL)
3174 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3175 INSN_UID (curr_insn));
3177 /* Swap the duplicates too. */
3178 swap_operands (commutative);
3179 change_p = true;
3182 #ifdef SECONDARY_MEMORY_NEEDED
3183 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3184 too conservatively. So we use the secondary memory only if there
3185 is no any alternative without reloads. */
3186 use_sec_mem_p = false;
3187 if (! alt_p)
3188 use_sec_mem_p = true;
3189 else if (sec_mem_p)
3191 for (i = 0; i < n_operands; i++)
3192 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3193 break;
3194 use_sec_mem_p = i < n_operands;
3197 if (use_sec_mem_p)
3199 rtx new_reg, src, dest, rld;
3200 enum machine_mode sec_mode, rld_mode;
3202 lra_assert (sec_mem_p);
3203 lra_assert (curr_static_id->operand[0].type == OP_OUT
3204 && curr_static_id->operand[1].type == OP_IN);
3205 dest = *curr_id->operand_loc[0];
3206 src = *curr_id->operand_loc[1];
3207 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3208 ? dest : src);
3209 rld_mode = GET_MODE (rld);
3210 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3211 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3212 #else
3213 sec_mode = rld_mode;
3214 #endif
3215 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3216 NO_REGS, "secondary");
3217 /* If the mode is changed, it should be wider. */
3218 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3219 if (sec_mode != rld_mode)
3221 /* If the target says specifically to use another mode for
3222 secondary memory moves we can not reuse the original
3223 insn. */
3224 after = emit_spill_move (false, new_reg, dest);
3225 lra_process_new_insns (curr_insn, NULL_RTX, after,
3226 "Inserting the sec. move");
3227 /* We may have non null BEFORE here (e.g. after address
3228 processing. */
3229 push_to_sequence (before);
3230 before = emit_spill_move (true, new_reg, src);
3231 emit_insn (before);
3232 before = get_insns ();
3233 end_sequence ();
3234 lra_process_new_insns (curr_insn, before, NULL_RTX, "Changing on");
3235 lra_set_insn_deleted (curr_insn);
3237 else if (dest == rld)
3239 *curr_id->operand_loc[0] = new_reg;
3240 after = emit_spill_move (false, new_reg, dest);
3241 lra_process_new_insns (curr_insn, NULL_RTX, after,
3242 "Inserting the sec. move");
3244 else
3246 *curr_id->operand_loc[1] = new_reg;
3247 /* See comments above. */
3248 push_to_sequence (before);
3249 before = emit_spill_move (true, new_reg, src);
3250 emit_insn (before);
3251 before = get_insns ();
3252 end_sequence ();
3253 lra_process_new_insns (curr_insn, before, NULL_RTX,
3254 "Inserting the sec. move");
3256 lra_update_insn_regno_info (curr_insn);
3257 return true;
3259 #endif
3261 lra_assert (goal_alt_number >= 0);
3262 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3264 if (lra_dump_file != NULL)
3266 const char *p;
3268 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3269 goal_alt_number, INSN_UID (curr_insn));
3270 for (i = 0; i < n_operands; i++)
3272 p = (curr_static_id->operand_alternative
3273 [goal_alt_number * n_operands + i].constraint);
3274 if (*p == '\0')
3275 continue;
3276 fprintf (lra_dump_file, " (%d) ", i);
3277 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3278 fputc (*p, lra_dump_file);
3280 if (INSN_CODE (curr_insn) >= 0
3281 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3282 fprintf (lra_dump_file, " {%s}", p);
3283 if (curr_id->sp_offset != 0)
3284 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3285 curr_id->sp_offset);
3286 fprintf (lra_dump_file, "\n");
3289 /* Right now, for any pair of operands I and J that are required to
3290 match, with J < I, goal_alt_matches[I] is J. Add I to
3291 goal_alt_matched[J]. */
3293 for (i = 0; i < n_operands; i++)
3294 if ((j = goal_alt_matches[i]) >= 0)
3296 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3298 /* We allow matching one output operand and several input
3299 operands. */
3300 lra_assert (k == 0
3301 || (curr_static_id->operand[j].type == OP_OUT
3302 && curr_static_id->operand[i].type == OP_IN
3303 && (curr_static_id->operand
3304 [goal_alt_matched[j][0]].type == OP_IN)));
3305 goal_alt_matched[j][k] = i;
3306 goal_alt_matched[j][k + 1] = -1;
3309 for (i = 0; i < n_operands; i++)
3310 goal_alt_win[i] |= goal_alt_match_win[i];
3312 /* Any constants that aren't allowed and can't be reloaded into
3313 registers are here changed into memory references. */
3314 for (i = 0; i < n_operands; i++)
3315 if (goal_alt_win[i])
3317 int regno;
3318 enum reg_class new_class;
3319 rtx reg = *curr_id->operand_loc[i];
3321 if (GET_CODE (reg) == SUBREG)
3322 reg = SUBREG_REG (reg);
3324 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3326 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3328 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3330 lra_assert (ok_p);
3331 lra_change_class (regno, new_class, " Change to", true);
3335 else
3337 const char *constraint;
3338 char c;
3339 rtx op = *curr_id->operand_loc[i];
3340 rtx subreg = NULL_RTX;
3341 enum machine_mode mode = curr_operand_mode[i];
3343 if (GET_CODE (op) == SUBREG)
3345 subreg = op;
3346 op = SUBREG_REG (op);
3347 mode = GET_MODE (op);
3350 if (CONST_POOL_OK_P (mode, op)
3351 && ((targetm.preferred_reload_class
3352 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3353 || no_input_reloads_p))
3355 rtx tem = force_const_mem (mode, op);
3357 change_p = true;
3358 if (subreg != NULL_RTX)
3359 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3361 *curr_id->operand_loc[i] = tem;
3362 lra_update_dup (curr_id, i);
3363 process_address (i, &before, &after);
3365 /* If the alternative accepts constant pool refs directly
3366 there will be no reload needed at all. */
3367 if (subreg != NULL_RTX)
3368 continue;
3369 /* Skip alternatives before the one requested. */
3370 constraint = (curr_static_id->operand_alternative
3371 [goal_alt_number * n_operands + i].constraint);
3372 for (;
3373 (c = *constraint) && c != ',' && c != '#';
3374 constraint += CONSTRAINT_LEN (c, constraint))
3376 if (c == TARGET_MEM_CONSTRAINT || c == 'o')
3377 break;
3378 #ifdef EXTRA_CONSTRAINT_STR
3379 if (EXTRA_MEMORY_CONSTRAINT (c, constraint)
3380 && EXTRA_CONSTRAINT_STR (tem, c, constraint))
3381 break;
3382 #endif
3384 if (c == '\0' || c == ',' || c == '#')
3385 continue;
3387 goal_alt_win[i] = true;
3391 for (i = 0; i < n_operands; i++)
3393 int regno;
3394 bool optional_p = false;
3395 rtx old, new_reg;
3396 rtx op = *curr_id->operand_loc[i];
3398 if (goal_alt_win[i])
3400 if (goal_alt[i] == NO_REGS
3401 && REG_P (op)
3402 /* When we assign NO_REGS it means that we will not
3403 assign a hard register to the scratch pseudo by
3404 assigment pass and the scratch pseudo will be
3405 spilled. Spilled scratch pseudos are transformed
3406 back to scratches at the LRA end. */
3407 && lra_former_scratch_operand_p (curr_insn, i))
3409 int regno = REGNO (op);
3410 lra_change_class (regno, NO_REGS, " Change to", true);
3411 if (lra_get_regno_hard_regno (regno) >= 0)
3412 /* We don't have to mark all insn affected by the
3413 spilled pseudo as there is only one such insn, the
3414 current one. */
3415 reg_renumber[regno] = -1;
3417 /* We can do an optional reload. If the pseudo got a hard
3418 reg, we might improve the code through inheritance. If
3419 it does not get a hard register we coalesce memory/memory
3420 moves later. Ignore move insns to avoid cycling. */
3421 if (! lra_simple_p
3422 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3423 && goal_alt[i] != NO_REGS && REG_P (op)
3424 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3425 && regno < new_regno_start
3426 && ! lra_former_scratch_p (regno)
3427 && reg_renumber[regno] < 0
3428 && (curr_insn_set == NULL_RTX
3429 || !((REG_P (SET_SRC (curr_insn_set))
3430 || MEM_P (SET_SRC (curr_insn_set))
3431 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3432 && (REG_P (SET_DEST (curr_insn_set))
3433 || MEM_P (SET_DEST (curr_insn_set))
3434 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3435 optional_p = true;
3436 else
3437 continue;
3440 /* Operands that match previous ones have already been handled. */
3441 if (goal_alt_matches[i] >= 0)
3442 continue;
3444 /* We should not have an operand with a non-offsettable address
3445 appearing where an offsettable address will do. It also may
3446 be a case when the address should be special in other words
3447 not a general one (e.g. it needs no index reg). */
3448 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3450 enum reg_class rclass;
3451 rtx *loc = &XEXP (op, 0);
3452 enum rtx_code code = GET_CODE (*loc);
3454 push_to_sequence (before);
3455 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3456 MEM, SCRATCH);
3457 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3458 new_reg = emit_inc (rclass, *loc, *loc,
3459 /* This value does not matter for MODIFY. */
3460 GET_MODE_SIZE (GET_MODE (op)));
3461 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass,
3462 "offsetable address", &new_reg))
3463 lra_emit_move (new_reg, *loc);
3464 before = get_insns ();
3465 end_sequence ();
3466 *loc = new_reg;
3467 lra_update_dup (curr_id, i);
3469 else if (goal_alt_matched[i][0] == -1)
3471 enum machine_mode mode;
3472 rtx reg, *loc;
3473 int hard_regno, byte;
3474 enum op_type type = curr_static_id->operand[i].type;
3476 loc = curr_id->operand_loc[i];
3477 mode = curr_operand_mode[i];
3478 if (GET_CODE (*loc) == SUBREG)
3480 reg = SUBREG_REG (*loc);
3481 byte = SUBREG_BYTE (*loc);
3482 if (REG_P (reg)
3483 /* Strict_low_part requires reload the register not
3484 the sub-register. */
3485 && (curr_static_id->operand[i].strict_low
3486 || (GET_MODE_SIZE (mode)
3487 <= GET_MODE_SIZE (GET_MODE (reg))
3488 && (hard_regno
3489 = get_try_hard_regno (REGNO (reg))) >= 0
3490 && (simplify_subreg_regno
3491 (hard_regno,
3492 GET_MODE (reg), byte, mode) < 0)
3493 && (goal_alt[i] == NO_REGS
3494 || (simplify_subreg_regno
3495 (ira_class_hard_regs[goal_alt[i]][0],
3496 GET_MODE (reg), byte, mode) >= 0)))))
3498 loc = &SUBREG_REG (*loc);
3499 mode = GET_MODE (*loc);
3502 old = *loc;
3503 if (get_reload_reg (type, mode, old, goal_alt[i], "", &new_reg)
3504 && type != OP_OUT)
3506 push_to_sequence (before);
3507 lra_emit_move (new_reg, old);
3508 before = get_insns ();
3509 end_sequence ();
3511 *loc = new_reg;
3512 if (type != OP_IN
3513 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3515 start_sequence ();
3516 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3517 emit_insn (after);
3518 after = get_insns ();
3519 end_sequence ();
3520 *loc = new_reg;
3522 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3523 if (goal_alt_dont_inherit_ops[j] == i)
3525 lra_set_regno_unique_value (REGNO (new_reg));
3526 break;
3528 lra_update_dup (curr_id, i);
3530 else if (curr_static_id->operand[i].type == OP_IN
3531 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3532 == OP_OUT))
3534 /* generate reloads for input and matched outputs. */
3535 match_inputs[0] = i;
3536 match_inputs[1] = -1;
3537 match_reload (goal_alt_matched[i][0], match_inputs,
3538 goal_alt[i], &before, &after);
3540 else if (curr_static_id->operand[i].type == OP_OUT
3541 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3542 == OP_IN))
3543 /* Generate reloads for output and matched inputs. */
3544 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3545 else if (curr_static_id->operand[i].type == OP_IN
3546 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3547 == OP_IN))
3549 /* Generate reloads for matched inputs. */
3550 match_inputs[0] = i;
3551 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3552 match_inputs[j + 1] = k;
3553 match_inputs[j + 1] = -1;
3554 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3556 else
3557 /* We must generate code in any case when function
3558 process_alt_operands decides that it is possible. */
3559 gcc_unreachable ();
3560 if (optional_p)
3562 lra_assert (REG_P (op));
3563 regno = REGNO (op);
3564 op = *curr_id->operand_loc[i]; /* Substitution. */
3565 if (GET_CODE (op) == SUBREG)
3566 op = SUBREG_REG (op);
3567 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3568 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3569 lra_reg_info[REGNO (op)].restore_regno = regno;
3570 if (lra_dump_file != NULL)
3571 fprintf (lra_dump_file,
3572 " Making reload reg %d for reg %d optional\n",
3573 REGNO (op), regno);
3576 if (before != NULL_RTX || after != NULL_RTX
3577 || max_regno_before != max_reg_num ())
3578 change_p = true;
3579 if (change_p)
3581 lra_update_operator_dups (curr_id);
3582 /* Something changes -- process the insn. */
3583 lra_update_insn_regno_info (curr_insn);
3585 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3586 return change_p;
3589 /* Return true if X is in LIST. */
3590 static bool
3591 in_list_p (rtx x, rtx list)
3593 for (; list != NULL_RTX; list = XEXP (list, 1))
3594 if (XEXP (list, 0) == x)
3595 return true;
3596 return false;
3599 /* Return true if X contains an allocatable hard register (if
3600 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3601 static bool
3602 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3604 int i, j;
3605 const char *fmt;
3606 enum rtx_code code;
3608 code = GET_CODE (x);
3609 if (REG_P (x))
3611 int regno = REGNO (x);
3612 HARD_REG_SET alloc_regs;
3614 if (hard_reg_p)
3616 if (regno >= FIRST_PSEUDO_REGISTER)
3617 regno = lra_get_regno_hard_regno (regno);
3618 if (regno < 0)
3619 return false;
3620 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3621 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3623 else
3625 if (regno < FIRST_PSEUDO_REGISTER)
3626 return false;
3627 if (! spilled_p)
3628 return true;
3629 return lra_get_regno_hard_regno (regno) < 0;
3632 fmt = GET_RTX_FORMAT (code);
3633 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3635 if (fmt[i] == 'e')
3637 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3638 return true;
3640 else if (fmt[i] == 'E')
3642 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3643 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3644 return true;
3647 return false;
3650 /* Process all regs in location *LOC and change them on equivalent
3651 substitution. Return true if any change was done. */
3652 static bool
3653 loc_equivalence_change_p (rtx *loc)
3655 rtx subst, reg, x = *loc;
3656 bool result = false;
3657 enum rtx_code code = GET_CODE (x);
3658 const char *fmt;
3659 int i, j;
3661 if (code == SUBREG)
3663 reg = SUBREG_REG (x);
3664 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
3665 && GET_MODE (subst) == VOIDmode)
3667 /* We cannot reload debug location. Simplify subreg here
3668 while we know the inner mode. */
3669 *loc = simplify_gen_subreg (GET_MODE (x), subst,
3670 GET_MODE (reg), SUBREG_BYTE (x));
3671 return true;
3674 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
3676 *loc = subst;
3677 return true;
3680 /* Scan all the operand sub-expressions. */
3681 fmt = GET_RTX_FORMAT (code);
3682 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3684 if (fmt[i] == 'e')
3685 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
3686 else if (fmt[i] == 'E')
3687 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3688 result
3689 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
3691 return result;
3694 /* Similar to loc_equivalence_change_p, but for use as
3695 simplify_replace_fn_rtx callback. */
3696 static rtx
3697 loc_equivalence_callback (rtx loc, const_rtx, void *)
3699 if (!REG_P (loc))
3700 return NULL_RTX;
3702 rtx subst = get_equiv_with_elimination (loc, curr_insn);
3703 if (subst != loc)
3704 return subst;
3706 return NULL_RTX;
3709 /* Maximum number of generated reload insns per an insn. It is for
3710 preventing this pass cycling in a bug case. */
3711 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
3713 /* The current iteration number of this LRA pass. */
3714 int lra_constraint_iter;
3716 /* The current iteration number of this LRA pass after the last spill
3717 pass. */
3718 int lra_constraint_iter_after_spill;
3720 /* True if we substituted equiv which needs checking register
3721 allocation correctness because the equivalent value contains
3722 allocatable hard registers or when we restore multi-register
3723 pseudo. */
3724 bool lra_risky_transformations_p;
3726 /* Return true if REGNO is referenced in more than one block. */
3727 static bool
3728 multi_block_pseudo_p (int regno)
3730 basic_block bb = NULL;
3731 unsigned int uid;
3732 bitmap_iterator bi;
3734 if (regno < FIRST_PSEUDO_REGISTER)
3735 return false;
3737 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
3738 if (bb == NULL)
3739 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
3740 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
3741 return true;
3742 return false;
3745 /* Return true if LIST contains a deleted insn. */
3746 static bool
3747 contains_deleted_insn_p (rtx list)
3749 for (; list != NULL_RTX; list = XEXP (list, 1))
3750 if (NOTE_P (XEXP (list, 0))
3751 && NOTE_KIND (XEXP (list, 0)) == NOTE_INSN_DELETED)
3752 return true;
3753 return false;
3756 /* Return true if X contains a pseudo dying in INSN. */
3757 static bool
3758 dead_pseudo_p (rtx x, rtx insn)
3760 int i, j;
3761 const char *fmt;
3762 enum rtx_code code;
3764 if (REG_P (x))
3765 return (insn != NULL_RTX
3766 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
3767 code = GET_CODE (x);
3768 fmt = GET_RTX_FORMAT (code);
3769 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3771 if (fmt[i] == 'e')
3773 if (dead_pseudo_p (XEXP (x, i), insn))
3774 return true;
3776 else if (fmt[i] == 'E')
3778 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3779 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
3780 return true;
3783 return false;
3786 /* Return true if INSN contains a dying pseudo in INSN right hand
3787 side. */
3788 static bool
3789 insn_rhs_dead_pseudo_p (rtx insn)
3791 rtx set = single_set (insn);
3793 gcc_assert (set != NULL);
3794 return dead_pseudo_p (SET_SRC (set), insn);
3797 /* Return true if any init insn of REGNO contains a dying pseudo in
3798 insn right hand side. */
3799 static bool
3800 init_insn_rhs_dead_pseudo_p (int regno)
3802 rtx insns = ira_reg_equiv[regno].init_insns;
3804 if (insns == NULL)
3805 return false;
3806 if (INSN_P (insns))
3807 return insn_rhs_dead_pseudo_p (insns);
3808 for (; insns != NULL_RTX; insns = XEXP (insns, 1))
3809 if (insn_rhs_dead_pseudo_p (XEXP (insns, 0)))
3810 return true;
3811 return false;
3814 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
3815 reverse only if we have one init insn with given REGNO as a
3816 source. */
3817 static bool
3818 reverse_equiv_p (int regno)
3820 rtx insns, set;
3822 if ((insns = ira_reg_equiv[regno].init_insns) == NULL_RTX)
3823 return false;
3824 if (! INSN_P (XEXP (insns, 0))
3825 || XEXP (insns, 1) != NULL_RTX)
3826 return false;
3827 if ((set = single_set (XEXP (insns, 0))) == NULL_RTX)
3828 return false;
3829 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
3832 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
3833 call this function only for non-reverse equivalence. */
3834 static bool
3835 contains_reloaded_insn_p (int regno)
3837 rtx set;
3838 rtx list = ira_reg_equiv[regno].init_insns;
3840 for (; list != NULL_RTX; list = XEXP (list, 1))
3841 if ((set = single_set (XEXP (list, 0))) == NULL_RTX
3842 || ! REG_P (SET_DEST (set))
3843 || (int) REGNO (SET_DEST (set)) != regno)
3844 return true;
3845 return false;
3848 /* Entry function of LRA constraint pass. Return true if the
3849 constraint pass did change the code. */
3850 bool
3851 lra_constraints (bool first_p)
3853 bool changed_p;
3854 int i, hard_regno, new_insns_num;
3855 unsigned int min_len, new_min_len, uid;
3856 rtx set, x, reg, dest_reg;
3857 basic_block last_bb;
3858 bitmap_head equiv_insn_bitmap;
3859 bitmap_iterator bi;
3861 lra_constraint_iter++;
3862 if (lra_dump_file != NULL)
3863 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
3864 lra_constraint_iter);
3865 lra_constraint_iter_after_spill++;
3866 if (lra_constraint_iter_after_spill > LRA_MAX_CONSTRAINT_ITERATION_NUMBER)
3867 internal_error
3868 ("Maximum number of LRA constraint passes is achieved (%d)\n",
3869 LRA_MAX_CONSTRAINT_ITERATION_NUMBER);
3870 changed_p = false;
3871 lra_risky_transformations_p = false;
3872 new_insn_uid_start = get_max_uid ();
3873 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
3874 /* Mark used hard regs for target stack size calulations. */
3875 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3876 if (lra_reg_info[i].nrefs != 0
3877 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
3879 int j, nregs;
3881 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
3882 for (j = 0; j < nregs; j++)
3883 df_set_regs_ever_live (hard_regno + j, true);
3885 /* Do elimination before the equivalence processing as we can spill
3886 some pseudos during elimination. */
3887 lra_eliminate (false, first_p);
3888 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
3889 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3890 if (lra_reg_info[i].nrefs != 0)
3892 ira_reg_equiv[i].profitable_p = true;
3893 reg = regno_reg_rtx[i];
3894 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
3896 bool pseudo_p = contains_reg_p (x, false, false);
3898 /* After RTL transformation, we can not guarantee that
3899 pseudo in the substitution was not reloaded which might
3900 make equivalence invalid. For example, in reverse
3901 equiv of p0
3903 p0 <- ...
3905 equiv_mem <- p0
3907 the memory address register was reloaded before the 2nd
3908 insn. */
3909 if ((! first_p && pseudo_p)
3910 /* We don't use DF for compilation speed sake. So it
3911 is problematic to update live info when we use an
3912 equivalence containing pseudos in more than one
3913 BB. */
3914 || (pseudo_p && multi_block_pseudo_p (i))
3915 /* If an init insn was deleted for some reason, cancel
3916 the equiv. We could update the equiv insns after
3917 transformations including an equiv insn deletion
3918 but it is not worthy as such cases are extremely
3919 rare. */
3920 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
3921 /* If it is not a reverse equivalence, we check that a
3922 pseudo in rhs of the init insn is not dying in the
3923 insn. Otherwise, the live info at the beginning of
3924 the corresponding BB might be wrong after we
3925 removed the insn. When the equiv can be a
3926 constant, the right hand side of the init insn can
3927 be a pseudo. */
3928 || (! reverse_equiv_p (i)
3929 && (init_insn_rhs_dead_pseudo_p (i)
3930 /* If we reloaded the pseudo in an equivalence
3931 init insn, we can not remove the equiv init
3932 insns and the init insns might write into
3933 const memory in this case. */
3934 || contains_reloaded_insn_p (i)))
3935 /* Prevent access beyond equivalent memory for
3936 paradoxical subregs. */
3937 || (MEM_P (x)
3938 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
3939 > GET_MODE_SIZE (GET_MODE (x)))))
3940 ira_reg_equiv[i].defined_p = false;
3941 if (contains_reg_p (x, false, true))
3942 ira_reg_equiv[i].profitable_p = false;
3943 if (get_equiv (reg) != reg)
3944 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
3947 /* We should add all insns containing pseudos which should be
3948 substituted by their equivalences. */
3949 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
3950 lra_push_insn_by_uid (uid);
3951 min_len = lra_insn_stack_length ();
3952 new_insns_num = 0;
3953 last_bb = NULL;
3954 changed_p = false;
3955 while ((new_min_len = lra_insn_stack_length ()) != 0)
3957 curr_insn = lra_pop_insn ();
3958 --new_min_len;
3959 curr_bb = BLOCK_FOR_INSN (curr_insn);
3960 if (curr_bb != last_bb)
3962 last_bb = curr_bb;
3963 bb_reload_num = lra_curr_reload_num;
3965 if (min_len > new_min_len)
3967 min_len = new_min_len;
3968 new_insns_num = 0;
3970 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
3971 internal_error
3972 ("Max. number of generated reload insns per insn is achieved (%d)\n",
3973 MAX_RELOAD_INSNS_NUMBER);
3974 new_insns_num++;
3975 if (DEBUG_INSN_P (curr_insn))
3977 /* We need to check equivalence in debug insn and change
3978 pseudo to the equivalent value if necessary. */
3979 curr_id = lra_get_insn_recog_data (curr_insn);
3980 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
3982 rtx old = *curr_id->operand_loc[0];
3983 *curr_id->operand_loc[0]
3984 = simplify_replace_fn_rtx (old, NULL_RTX,
3985 loc_equivalence_callback, NULL);
3986 if (old != *curr_id->operand_loc[0])
3988 lra_update_insn_regno_info (curr_insn);
3989 changed_p = true;
3993 else if (INSN_P (curr_insn))
3995 if ((set = single_set (curr_insn)) != NULL_RTX)
3997 dest_reg = SET_DEST (set);
3998 /* The equivalence pseudo could be set up as SUBREG in a
3999 case when it is a call restore insn in a mode
4000 different from the pseudo mode. */
4001 if (GET_CODE (dest_reg) == SUBREG)
4002 dest_reg = SUBREG_REG (dest_reg);
4003 if ((REG_P (dest_reg)
4004 && (x = get_equiv (dest_reg)) != dest_reg
4005 /* Remove insns which set up a pseudo whose value
4006 can not be changed. Such insns might be not in
4007 init_insns because we don't update equiv data
4008 during insn transformations.
4010 As an example, let suppose that a pseudo got
4011 hard register and on the 1st pass was not
4012 changed to equivalent constant. We generate an
4013 additional insn setting up the pseudo because of
4014 secondary memory movement. Then the pseudo is
4015 spilled and we use the equiv constant. In this
4016 case we should remove the additional insn and
4017 this insn is not init_insns list. */
4018 && (! MEM_P (x) || MEM_READONLY_P (x)
4019 /* Check that this is actually an insn setting
4020 up the equivalence. */
4021 || in_list_p (curr_insn,
4022 ira_reg_equiv
4023 [REGNO (dest_reg)].init_insns)))
4024 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4025 && in_list_p (curr_insn,
4026 ira_reg_equiv
4027 [REGNO (SET_SRC (set))].init_insns)))
4029 /* This is equiv init insn of pseudo which did not get a
4030 hard register -- remove the insn. */
4031 if (lra_dump_file != NULL)
4033 fprintf (lra_dump_file,
4034 " Removing equiv init insn %i (freq=%d)\n",
4035 INSN_UID (curr_insn),
4036 BLOCK_FOR_INSN (curr_insn)->frequency);
4037 dump_insn_slim (lra_dump_file, curr_insn);
4039 if (contains_reg_p (x, true, false))
4040 lra_risky_transformations_p = true;
4041 lra_set_insn_deleted (curr_insn);
4042 continue;
4045 curr_id = lra_get_insn_recog_data (curr_insn);
4046 curr_static_id = curr_id->insn_static_data;
4047 init_curr_insn_input_reloads ();
4048 init_curr_operand_mode ();
4049 if (curr_insn_transform ())
4050 changed_p = true;
4051 /* Check non-transformed insns too for equiv change as USE
4052 or CLOBBER don't need reloads but can contain pseudos
4053 being changed on their equivalences. */
4054 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4055 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4057 lra_update_insn_regno_info (curr_insn);
4058 changed_p = true;
4062 bitmap_clear (&equiv_insn_bitmap);
4063 /* If we used a new hard regno, changed_p should be true because the
4064 hard reg is assigned to a new pseudo. */
4065 #ifdef ENABLE_CHECKING
4066 if (! changed_p)
4068 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4069 if (lra_reg_info[i].nrefs != 0
4070 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4072 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4074 for (j = 0; j < nregs; j++)
4075 lra_assert (df_regs_ever_live_p (hard_regno + j));
4078 #endif
4079 return changed_p;
4082 /* Initiate the LRA constraint pass. It is done once per
4083 function. */
4084 void
4085 lra_constraints_init (void)
4089 /* Finalize the LRA constraint pass. It is done once per
4090 function. */
4091 void
4092 lra_constraints_finish (void)
4098 /* This page contains code to do inheritance/split
4099 transformations. */
4101 /* Number of reloads passed so far in current EBB. */
4102 static int reloads_num;
4104 /* Number of calls passed so far in current EBB. */
4105 static int calls_num;
4107 /* Current reload pseudo check for validity of elements in
4108 USAGE_INSNS. */
4109 static int curr_usage_insns_check;
4111 /* Info about last usage of registers in EBB to do inheritance/split
4112 transformation. Inheritance transformation is done from a spilled
4113 pseudo and split transformations from a hard register or a pseudo
4114 assigned to a hard register. */
4115 struct usage_insns
4117 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4118 value INSNS is valid. The insns is chain of optional debug insns
4119 and a finishing non-debug insn using the corresponding reg. The
4120 value is also used to mark the registers which are set up in the
4121 current insn. The negated insn uid is used for this. */
4122 int check;
4123 /* Value of global reloads_num at the last insn in INSNS. */
4124 int reloads_num;
4125 /* Value of global reloads_nums at the last insn in INSNS. */
4126 int calls_num;
4127 /* It can be true only for splitting. And it means that the restore
4128 insn should be put after insn given by the following member. */
4129 bool after_p;
4130 /* Next insns in the current EBB which use the original reg and the
4131 original reg value is not changed between the current insn and
4132 the next insns. In order words, e.g. for inheritance, if we need
4133 to use the original reg value again in the next insns we can try
4134 to use the value in a hard register from a reload insn of the
4135 current insn. */
4136 rtx insns;
4139 /* Map: regno -> corresponding pseudo usage insns. */
4140 static struct usage_insns *usage_insns;
4142 static void
4143 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4145 usage_insns[regno].check = curr_usage_insns_check;
4146 usage_insns[regno].insns = insn;
4147 usage_insns[regno].reloads_num = reloads_num;
4148 usage_insns[regno].calls_num = calls_num;
4149 usage_insns[regno].after_p = after_p;
4152 /* The function is used to form list REGNO usages which consists of
4153 optional debug insns finished by a non-debug insn using REGNO.
4154 RELOADS_NUM is current number of reload insns processed so far. */
4155 static void
4156 add_next_usage_insn (int regno, rtx insn, int reloads_num)
4158 rtx next_usage_insns;
4160 if (usage_insns[regno].check == curr_usage_insns_check
4161 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4162 && DEBUG_INSN_P (insn))
4164 /* Check that we did not add the debug insn yet. */
4165 if (next_usage_insns != insn
4166 && (GET_CODE (next_usage_insns) != INSN_LIST
4167 || XEXP (next_usage_insns, 0) != insn))
4168 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4169 next_usage_insns);
4171 else if (NONDEBUG_INSN_P (insn))
4172 setup_next_usage_insn (regno, insn, reloads_num, false);
4173 else
4174 usage_insns[regno].check = 0;
4177 /* Replace all references to register OLD_REGNO in *LOC with pseudo
4178 register NEW_REG. Return true if any change was made. */
4179 static bool
4180 substitute_pseudo (rtx *loc, int old_regno, rtx new_reg)
4182 rtx x = *loc;
4183 bool result = false;
4184 enum rtx_code code;
4185 const char *fmt;
4186 int i, j;
4188 if (x == NULL_RTX)
4189 return false;
4191 code = GET_CODE (x);
4192 if (code == REG && (int) REGNO (x) == old_regno)
4194 enum machine_mode mode = GET_MODE (*loc);
4195 enum machine_mode inner_mode = GET_MODE (new_reg);
4197 if (mode != inner_mode)
4199 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
4200 || ! SCALAR_INT_MODE_P (inner_mode))
4201 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
4202 else
4203 new_reg = gen_lowpart_SUBREG (mode, new_reg);
4205 *loc = new_reg;
4206 return true;
4209 /* Scan all the operand sub-expressions. */
4210 fmt = GET_RTX_FORMAT (code);
4211 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4213 if (fmt[i] == 'e')
4215 if (substitute_pseudo (&XEXP (x, i), old_regno, new_reg))
4216 result = true;
4218 else if (fmt[i] == 'E')
4220 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4221 if (substitute_pseudo (&XVECEXP (x, i, j), old_regno, new_reg))
4222 result = true;
4225 return result;
4228 /* Return first non-debug insn in list USAGE_INSNS. */
4229 static rtx
4230 skip_usage_debug_insns (rtx usage_insns)
4232 rtx insn;
4234 /* Skip debug insns. */
4235 for (insn = usage_insns;
4236 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4237 insn = XEXP (insn, 1))
4239 return insn;
4242 /* Return true if we need secondary memory moves for insn in
4243 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4244 into the insn. */
4245 static bool
4246 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4247 rtx usage_insns ATTRIBUTE_UNUSED)
4249 #ifndef SECONDARY_MEMORY_NEEDED
4250 return false;
4251 #else
4252 rtx insn, set, dest;
4253 enum reg_class cl;
4255 if (inher_cl == ALL_REGS
4256 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4257 return false;
4258 lra_assert (INSN_P (insn));
4259 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4260 return false;
4261 dest = SET_DEST (set);
4262 if (! REG_P (dest))
4263 return false;
4264 lra_assert (inher_cl != NO_REGS);
4265 cl = get_reg_class (REGNO (dest));
4266 return (cl != NO_REGS && cl != ALL_REGS
4267 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4268 #endif
4271 /* Registers involved in inheritance/split in the current EBB
4272 (inheritance/split pseudos and original registers). */
4273 static bitmap_head check_only_regs;
4275 /* Do inheritance transformations for insn INSN, which defines (if
4276 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4277 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4278 form as the "insns" field of usage_insns. Return true if we
4279 succeed in such transformation.
4281 The transformations look like:
4283 p <- ... i <- ...
4284 ... p <- i (new insn)
4285 ... =>
4286 <- ... p ... <- ... i ...
4288 ... i <- p (new insn)
4289 <- ... p ... <- ... i ...
4290 ... =>
4291 <- ... p ... <- ... i ...
4292 where p is a spilled original pseudo and i is a new inheritance pseudo.
4295 The inheritance pseudo has the smallest class of two classes CL and
4296 class of ORIGINAL REGNO. */
4297 static bool
4298 inherit_reload_reg (bool def_p, int original_regno,
4299 enum reg_class cl, rtx insn, rtx next_usage_insns)
4301 enum reg_class rclass = lra_get_allocno_class (original_regno);
4302 rtx original_reg = regno_reg_rtx[original_regno];
4303 rtx new_reg, new_insns, usage_insn;
4305 lra_assert (! usage_insns[original_regno].after_p);
4306 if (lra_dump_file != NULL)
4307 fprintf (lra_dump_file,
4308 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4309 if (! ira_reg_classes_intersect_p[cl][rclass])
4311 if (lra_dump_file != NULL)
4313 fprintf (lra_dump_file,
4314 " Rejecting inheritance for %d "
4315 "because of disjoint classes %s and %s\n",
4316 original_regno, reg_class_names[cl],
4317 reg_class_names[rclass]);
4318 fprintf (lra_dump_file,
4319 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4321 return false;
4323 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4324 /* We don't use a subset of two classes because it can be
4325 NO_REGS. This transformation is still profitable in most
4326 cases even if the classes are not intersected as register
4327 move is probably cheaper than a memory load. */
4328 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4330 if (lra_dump_file != NULL)
4331 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4332 reg_class_names[cl], reg_class_names[rclass]);
4334 rclass = cl;
4336 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4338 /* Reject inheritance resulting in secondary memory moves.
4339 Otherwise, there is a danger in LRA cycling. Also such
4340 transformation will be unprofitable. */
4341 if (lra_dump_file != NULL)
4343 rtx insn = skip_usage_debug_insns (next_usage_insns);
4344 rtx set = single_set (insn);
4346 lra_assert (set != NULL_RTX);
4348 rtx dest = SET_DEST (set);
4350 lra_assert (REG_P (dest));
4351 fprintf (lra_dump_file,
4352 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4353 "as secondary mem is needed\n",
4354 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4355 original_regno, reg_class_names[rclass]);
4356 fprintf (lra_dump_file,
4357 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4359 return false;
4361 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4362 rclass, "inheritance");
4363 start_sequence ();
4364 if (def_p)
4365 emit_move_insn (original_reg, new_reg);
4366 else
4367 emit_move_insn (new_reg, original_reg);
4368 new_insns = get_insns ();
4369 end_sequence ();
4370 if (NEXT_INSN (new_insns) != NULL_RTX)
4372 if (lra_dump_file != NULL)
4374 fprintf (lra_dump_file,
4375 " Rejecting inheritance %d->%d "
4376 "as it results in 2 or more insns:\n",
4377 original_regno, REGNO (new_reg));
4378 dump_rtl_slim (lra_dump_file, new_insns, NULL_RTX, -1, 0);
4379 fprintf (lra_dump_file,
4380 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4382 return false;
4384 substitute_pseudo (&insn, original_regno, new_reg);
4385 lra_update_insn_regno_info (insn);
4386 if (! def_p)
4387 /* We now have a new usage insn for original regno. */
4388 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4389 if (lra_dump_file != NULL)
4390 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4391 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4392 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4393 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4394 bitmap_set_bit (&check_only_regs, original_regno);
4395 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4396 if (def_p)
4397 lra_process_new_insns (insn, NULL_RTX, new_insns,
4398 "Add original<-inheritance");
4399 else
4400 lra_process_new_insns (insn, new_insns, NULL_RTX,
4401 "Add inheritance<-original");
4402 while (next_usage_insns != NULL_RTX)
4404 if (GET_CODE (next_usage_insns) != INSN_LIST)
4406 usage_insn = next_usage_insns;
4407 lra_assert (NONDEBUG_INSN_P (usage_insn));
4408 next_usage_insns = NULL;
4410 else
4412 usage_insn = XEXP (next_usage_insns, 0);
4413 lra_assert (DEBUG_INSN_P (usage_insn));
4414 next_usage_insns = XEXP (next_usage_insns, 1);
4416 substitute_pseudo (&usage_insn, original_regno, new_reg);
4417 lra_update_insn_regno_info (usage_insn);
4418 if (lra_dump_file != NULL)
4420 fprintf (lra_dump_file,
4421 " Inheritance reuse change %d->%d (bb%d):\n",
4422 original_regno, REGNO (new_reg),
4423 BLOCK_FOR_INSN (usage_insn)->index);
4424 dump_insn_slim (lra_dump_file, usage_insn);
4427 if (lra_dump_file != NULL)
4428 fprintf (lra_dump_file,
4429 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4430 return true;
4433 /* Return true if we need a caller save/restore for pseudo REGNO which
4434 was assigned to a hard register. */
4435 static inline bool
4436 need_for_call_save_p (int regno)
4438 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4439 return (usage_insns[regno].calls_num < calls_num
4440 && (overlaps_hard_reg_set_p
4441 (call_used_reg_set,
4442 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4443 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4444 PSEUDO_REGNO_MODE (regno))));
4447 /* Global registers occurring in the current EBB. */
4448 static bitmap_head ebb_global_regs;
4450 /* Return true if we need a split for hard register REGNO or pseudo
4451 REGNO which was assigned to a hard register.
4452 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4453 used for reloads since the EBB end. It is an approximation of the
4454 used hard registers in the split range. The exact value would
4455 require expensive calculations. If we were aggressive with
4456 splitting because of the approximation, the split pseudo will save
4457 the same hard register assignment and will be removed in the undo
4458 pass. We still need the approximation because too aggressive
4459 splitting would result in too inaccurate cost calculation in the
4460 assignment pass because of too many generated moves which will be
4461 probably removed in the undo pass. */
4462 static inline bool
4463 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4465 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4467 lra_assert (hard_regno >= 0);
4468 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4469 /* Don't split eliminable hard registers, otherwise we can
4470 split hard registers like hard frame pointer, which
4471 lives on BB start/end according to DF-infrastructure,
4472 when there is a pseudo assigned to the register and
4473 living in the same BB. */
4474 && (regno >= FIRST_PSEUDO_REGISTER
4475 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4476 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4477 /* Don't split call clobbered hard regs living through
4478 calls, otherwise we might have a check problem in the
4479 assign sub-pass as in the most cases (exception is a
4480 situation when lra_risky_transformations_p value is
4481 true) the assign pass assumes that all pseudos living
4482 through calls are assigned to call saved hard regs. */
4483 && (regno >= FIRST_PSEUDO_REGISTER
4484 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4485 || usage_insns[regno].calls_num == calls_num)
4486 /* We need at least 2 reloads to make pseudo splitting
4487 profitable. We should provide hard regno splitting in
4488 any case to solve 1st insn scheduling problem when
4489 moving hard register definition up might result in
4490 impossibility to find hard register for reload pseudo of
4491 small register class. */
4492 && (usage_insns[regno].reloads_num
4493 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 2) < reloads_num)
4494 && (regno < FIRST_PSEUDO_REGISTER
4495 /* For short living pseudos, spilling + inheritance can
4496 be considered a substitution for splitting.
4497 Therefore we do not splitting for local pseudos. It
4498 decreases also aggressiveness of splitting. The
4499 minimal number of references is chosen taking into
4500 account that for 2 references splitting has no sense
4501 as we can just spill the pseudo. */
4502 || (regno >= FIRST_PSEUDO_REGISTER
4503 && lra_reg_info[regno].nrefs > 3
4504 && bitmap_bit_p (&ebb_global_regs, regno))))
4505 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4508 /* Return class for the split pseudo created from original pseudo with
4509 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4510 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4511 results in no secondary memory movements. */
4512 static enum reg_class
4513 choose_split_class (enum reg_class allocno_class,
4514 int hard_regno ATTRIBUTE_UNUSED,
4515 enum machine_mode mode ATTRIBUTE_UNUSED)
4517 #ifndef SECONDARY_MEMORY_NEEDED
4518 return allocno_class;
4519 #else
4520 int i;
4521 enum reg_class cl, best_cl = NO_REGS;
4522 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4523 = REGNO_REG_CLASS (hard_regno);
4525 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4526 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4527 return allocno_class;
4528 for (i = 0;
4529 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4530 i++)
4531 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4532 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4533 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4534 && (best_cl == NO_REGS
4535 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4536 best_cl = cl;
4537 return best_cl;
4538 #endif
4541 /* Do split transformations for insn INSN, which defines or uses
4542 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4543 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4544 "insns" field of usage_insns.
4546 The transformations look like:
4548 p <- ... p <- ...
4549 ... s <- p (new insn -- save)
4550 ... =>
4551 ... p <- s (new insn -- restore)
4552 <- ... p ... <- ... p ...
4554 <- ... p ... <- ... p ...
4555 ... s <- p (new insn -- save)
4556 ... =>
4557 ... p <- s (new insn -- restore)
4558 <- ... p ... <- ... p ...
4560 where p is an original pseudo got a hard register or a hard
4561 register and s is a new split pseudo. The save is put before INSN
4562 if BEFORE_P is true. Return true if we succeed in such
4563 transformation. */
4564 static bool
4565 split_reg (bool before_p, int original_regno, rtx insn, rtx next_usage_insns)
4567 enum reg_class rclass;
4568 rtx original_reg;
4569 int hard_regno, nregs;
4570 rtx new_reg, save, restore, usage_insn;
4571 bool after_p;
4572 bool call_save_p;
4574 if (original_regno < FIRST_PSEUDO_REGISTER)
4576 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4577 hard_regno = original_regno;
4578 call_save_p = false;
4579 nregs = 1;
4581 else
4583 hard_regno = reg_renumber[original_regno];
4584 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4585 rclass = lra_get_allocno_class (original_regno);
4586 original_reg = regno_reg_rtx[original_regno];
4587 call_save_p = need_for_call_save_p (original_regno);
4589 original_reg = regno_reg_rtx[original_regno];
4590 lra_assert (hard_regno >= 0);
4591 if (lra_dump_file != NULL)
4592 fprintf (lra_dump_file,
4593 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4594 if (call_save_p)
4596 enum machine_mode mode = GET_MODE (original_reg);
4598 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4599 hard_regno_nregs[hard_regno][mode],
4600 mode);
4601 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4603 else
4605 rclass = choose_split_class (rclass, hard_regno,
4606 GET_MODE (original_reg));
4607 if (rclass == NO_REGS)
4609 if (lra_dump_file != NULL)
4611 fprintf (lra_dump_file,
4612 " Rejecting split of %d(%s): "
4613 "no good reg class for %d(%s)\n",
4614 original_regno,
4615 reg_class_names[lra_get_allocno_class (original_regno)],
4616 hard_regno,
4617 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4618 fprintf
4619 (lra_dump_file,
4620 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4622 return false;
4624 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4625 rclass, "split");
4626 reg_renumber[REGNO (new_reg)] = hard_regno;
4628 save = emit_spill_move (true, new_reg, original_reg);
4629 if (NEXT_INSN (save) != NULL_RTX)
4631 lra_assert (! call_save_p);
4632 if (lra_dump_file != NULL)
4634 fprintf
4635 (lra_dump_file,
4636 " Rejecting split %d->%d resulting in > 2 %s save insns:\n",
4637 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4638 dump_rtl_slim (lra_dump_file, save, NULL_RTX, -1, 0);
4639 fprintf (lra_dump_file,
4640 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4642 return false;
4644 restore = emit_spill_move (false, new_reg, original_reg);
4645 if (NEXT_INSN (restore) != NULL_RTX)
4647 lra_assert (! call_save_p);
4648 if (lra_dump_file != NULL)
4650 fprintf (lra_dump_file,
4651 " Rejecting split %d->%d "
4652 "resulting in > 2 %s restore insns:\n",
4653 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4654 dump_rtl_slim (lra_dump_file, restore, NULL_RTX, -1, 0);
4655 fprintf (lra_dump_file,
4656 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4658 return false;
4660 after_p = usage_insns[original_regno].after_p;
4661 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4662 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4663 bitmap_set_bit (&check_only_regs, original_regno);
4664 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4665 for (;;)
4667 if (GET_CODE (next_usage_insns) != INSN_LIST)
4669 usage_insn = next_usage_insns;
4670 break;
4672 usage_insn = XEXP (next_usage_insns, 0);
4673 lra_assert (DEBUG_INSN_P (usage_insn));
4674 next_usage_insns = XEXP (next_usage_insns, 1);
4675 substitute_pseudo (&usage_insn, original_regno, new_reg);
4676 lra_update_insn_regno_info (usage_insn);
4677 if (lra_dump_file != NULL)
4679 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
4680 original_regno, REGNO (new_reg));
4681 dump_insn_slim (lra_dump_file, usage_insn);
4684 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
4685 lra_assert (usage_insn != insn || (after_p && before_p));
4686 lra_process_new_insns (usage_insn, after_p ? NULL_RTX : restore,
4687 after_p ? restore : NULL_RTX,
4688 call_save_p
4689 ? "Add reg<-save" : "Add reg<-split");
4690 lra_process_new_insns (insn, before_p ? save : NULL_RTX,
4691 before_p ? NULL_RTX : save,
4692 call_save_p
4693 ? "Add save<-reg" : "Add split<-reg");
4694 if (nregs > 1)
4695 /* If we are trying to split multi-register. We should check
4696 conflicts on the next assignment sub-pass. IRA can allocate on
4697 sub-register levels, LRA do this on pseudos level right now and
4698 this discrepancy may create allocation conflicts after
4699 splitting. */
4700 lra_risky_transformations_p = true;
4701 if (lra_dump_file != NULL)
4702 fprintf (lra_dump_file,
4703 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4704 return true;
4707 /* Recognize that we need a split transformation for insn INSN, which
4708 defines or uses REGNO in its insn biggest MODE (we use it only if
4709 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
4710 hard registers which might be used for reloads since the EBB end.
4711 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
4712 uid before starting INSN processing. Return true if we succeed in
4713 such transformation. */
4714 static bool
4715 split_if_necessary (int regno, enum machine_mode mode,
4716 HARD_REG_SET potential_reload_hard_regs,
4717 bool before_p, rtx insn, int max_uid)
4719 bool res = false;
4720 int i, nregs = 1;
4721 rtx next_usage_insns;
4723 if (regno < FIRST_PSEUDO_REGISTER)
4724 nregs = hard_regno_nregs[regno][mode];
4725 for (i = 0; i < nregs; i++)
4726 if (usage_insns[regno + i].check == curr_usage_insns_check
4727 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
4728 /* To avoid processing the register twice or more. */
4729 && ((GET_CODE (next_usage_insns) != INSN_LIST
4730 && INSN_UID (next_usage_insns) < max_uid)
4731 || (GET_CODE (next_usage_insns) == INSN_LIST
4732 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
4733 && need_for_split_p (potential_reload_hard_regs, regno + i)
4734 && split_reg (before_p, regno + i, insn, next_usage_insns))
4735 res = true;
4736 return res;
4739 /* Check only registers living at the current program point in the
4740 current EBB. */
4741 static bitmap_head live_regs;
4743 /* Update live info in EBB given by its HEAD and TAIL insns after
4744 inheritance/split transformation. The function removes dead moves
4745 too. */
4746 static void
4747 update_ebb_live_info (rtx head, rtx tail)
4749 unsigned int j;
4750 int regno;
4751 bool live_p;
4752 rtx prev_insn, set;
4753 bool remove_p;
4754 basic_block last_bb, prev_bb, curr_bb;
4755 bitmap_iterator bi;
4756 struct lra_insn_reg *reg;
4757 edge e;
4758 edge_iterator ei;
4760 last_bb = BLOCK_FOR_INSN (tail);
4761 prev_bb = NULL;
4762 for (curr_insn = tail;
4763 curr_insn != PREV_INSN (head);
4764 curr_insn = prev_insn)
4766 prev_insn = PREV_INSN (curr_insn);
4767 /* We need to process empty blocks too. They contain
4768 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
4769 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
4770 continue;
4771 curr_bb = BLOCK_FOR_INSN (curr_insn);
4772 if (curr_bb != prev_bb)
4774 if (prev_bb != NULL)
4776 /* Update df_get_live_in (prev_bb): */
4777 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4778 if (bitmap_bit_p (&live_regs, j))
4779 bitmap_set_bit (df_get_live_in (prev_bb), j);
4780 else
4781 bitmap_clear_bit (df_get_live_in (prev_bb), j);
4783 if (curr_bb != last_bb)
4785 /* Update df_get_live_out (curr_bb): */
4786 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4788 live_p = bitmap_bit_p (&live_regs, j);
4789 if (! live_p)
4790 FOR_EACH_EDGE (e, ei, curr_bb->succs)
4791 if (bitmap_bit_p (df_get_live_in (e->dest), j))
4793 live_p = true;
4794 break;
4796 if (live_p)
4797 bitmap_set_bit (df_get_live_out (curr_bb), j);
4798 else
4799 bitmap_clear_bit (df_get_live_out (curr_bb), j);
4802 prev_bb = curr_bb;
4803 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
4805 if (! NONDEBUG_INSN_P (curr_insn))
4806 continue;
4807 curr_id = lra_get_insn_recog_data (curr_insn);
4808 remove_p = false;
4809 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
4810 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
4811 && bitmap_bit_p (&check_only_regs, regno)
4812 && ! bitmap_bit_p (&live_regs, regno))
4813 remove_p = true;
4814 /* See which defined values die here. */
4815 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4816 if (reg->type == OP_OUT && ! reg->subreg_p)
4817 bitmap_clear_bit (&live_regs, reg->regno);
4818 /* Mark each used value as live. */
4819 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4820 if (reg->type != OP_OUT
4821 && bitmap_bit_p (&check_only_regs, reg->regno))
4822 bitmap_set_bit (&live_regs, reg->regno);
4823 /* It is quite important to remove dead move insns because it
4824 means removing dead store. We don't need to process them for
4825 constraints. */
4826 if (remove_p)
4828 if (lra_dump_file != NULL)
4830 fprintf (lra_dump_file, " Removing dead insn:\n ");
4831 dump_insn_slim (lra_dump_file, curr_insn);
4833 lra_set_insn_deleted (curr_insn);
4838 /* The structure describes info to do an inheritance for the current
4839 insn. We need to collect such info first before doing the
4840 transformations because the transformations change the insn
4841 internal representation. */
4842 struct to_inherit
4844 /* Original regno. */
4845 int regno;
4846 /* Subsequent insns which can inherit original reg value. */
4847 rtx insns;
4850 /* Array containing all info for doing inheritance from the current
4851 insn. */
4852 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
4854 /* Number elements in the previous array. */
4855 static int to_inherit_num;
4857 /* Add inheritance info REGNO and INSNS. Their meaning is described in
4858 structure to_inherit. */
4859 static void
4860 add_to_inherit (int regno, rtx insns)
4862 int i;
4864 for (i = 0; i < to_inherit_num; i++)
4865 if (to_inherit[i].regno == regno)
4866 return;
4867 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
4868 to_inherit[to_inherit_num].regno = regno;
4869 to_inherit[to_inherit_num++].insns = insns;
4872 /* Return the last non-debug insn in basic block BB, or the block begin
4873 note if none. */
4874 static rtx
4875 get_last_insertion_point (basic_block bb)
4877 rtx insn;
4879 FOR_BB_INSNS_REVERSE (bb, insn)
4880 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
4881 return insn;
4882 gcc_unreachable ();
4885 /* Set up RES by registers living on edges FROM except the edge (FROM,
4886 TO) or by registers set up in a jump insn in BB FROM. */
4887 static void
4888 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
4890 rtx last;
4891 struct lra_insn_reg *reg;
4892 edge e;
4893 edge_iterator ei;
4895 lra_assert (to != NULL);
4896 bitmap_clear (res);
4897 FOR_EACH_EDGE (e, ei, from->succs)
4898 if (e->dest != to)
4899 bitmap_ior_into (res, df_get_live_in (e->dest));
4900 last = get_last_insertion_point (from);
4901 if (! JUMP_P (last))
4902 return;
4903 curr_id = lra_get_insn_recog_data (last);
4904 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4905 if (reg->type != OP_IN)
4906 bitmap_set_bit (res, reg->regno);
4909 /* Used as a temporary results of some bitmap calculations. */
4910 static bitmap_head temp_bitmap;
4912 /* Do inheritance/split transformations in EBB starting with HEAD and
4913 finishing on TAIL. We process EBB insns in the reverse order.
4914 Return true if we did any inheritance/split transformation in the
4915 EBB.
4917 We should avoid excessive splitting which results in worse code
4918 because of inaccurate cost calculations for spilling new split
4919 pseudos in such case. To achieve this we do splitting only if
4920 register pressure is high in given basic block and there are reload
4921 pseudos requiring hard registers. We could do more register
4922 pressure calculations at any given program point to avoid necessary
4923 splitting even more but it is to expensive and the current approach
4924 works well enough. */
4925 static bool
4926 inherit_in_ebb (rtx head, rtx tail)
4928 int i, src_regno, dst_regno, nregs;
4929 bool change_p, succ_p;
4930 rtx prev_insn, next_usage_insns, set, last_insn;
4931 enum reg_class cl;
4932 struct lra_insn_reg *reg;
4933 basic_block last_processed_bb, curr_bb = NULL;
4934 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
4935 bitmap to_process;
4936 unsigned int j;
4937 bitmap_iterator bi;
4938 bool head_p, after_p;
4940 change_p = false;
4941 curr_usage_insns_check++;
4942 reloads_num = calls_num = 0;
4943 bitmap_clear (&check_only_regs);
4944 last_processed_bb = NULL;
4945 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
4946 CLEAR_HARD_REG_SET (live_hard_regs);
4947 /* We don't process new insns generated in the loop. */
4948 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
4950 prev_insn = PREV_INSN (curr_insn);
4951 if (BLOCK_FOR_INSN (curr_insn) != NULL)
4952 curr_bb = BLOCK_FOR_INSN (curr_insn);
4953 if (last_processed_bb != curr_bb)
4955 /* We are at the end of BB. Add qualified living
4956 pseudos for potential splitting. */
4957 to_process = df_get_live_out (curr_bb);
4958 if (last_processed_bb != NULL)
4960 /* We are somewhere in the middle of EBB. */
4961 get_live_on_other_edges (curr_bb, last_processed_bb,
4962 &temp_bitmap);
4963 to_process = &temp_bitmap;
4965 last_processed_bb = curr_bb;
4966 last_insn = get_last_insertion_point (curr_bb);
4967 after_p = (! JUMP_P (last_insn)
4968 && (! CALL_P (last_insn)
4969 || (find_reg_note (last_insn,
4970 REG_NORETURN, NULL_RTX) == NULL_RTX
4971 && ! SIBLING_CALL_P (last_insn))));
4972 REG_SET_TO_HARD_REG_SET (live_hard_regs, df_get_live_out (curr_bb));
4973 IOR_HARD_REG_SET (live_hard_regs, eliminable_regset);
4974 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
4975 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
4976 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
4978 if ((int) j >= lra_constraint_new_regno_start)
4979 break;
4980 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
4982 if (j < FIRST_PSEUDO_REGISTER)
4983 SET_HARD_REG_BIT (live_hard_regs, j);
4984 else
4985 add_to_hard_reg_set (&live_hard_regs,
4986 PSEUDO_REGNO_MODE (j),
4987 reg_renumber[j]);
4988 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
4992 src_regno = dst_regno = -1;
4993 if (NONDEBUG_INSN_P (curr_insn)
4994 && (set = single_set (curr_insn)) != NULL_RTX
4995 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
4997 src_regno = REGNO (SET_SRC (set));
4998 dst_regno = REGNO (SET_DEST (set));
5000 if (src_regno < lra_constraint_new_regno_start
5001 && src_regno >= FIRST_PSEUDO_REGISTER
5002 && reg_renumber[src_regno] < 0
5003 && dst_regno >= lra_constraint_new_regno_start
5004 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5006 /* 'reload_pseudo <- original_pseudo'. */
5007 reloads_num++;
5008 succ_p = false;
5009 if (usage_insns[src_regno].check == curr_usage_insns_check
5010 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5011 succ_p = inherit_reload_reg (false, src_regno, cl,
5012 curr_insn, next_usage_insns);
5013 if (succ_p)
5014 change_p = true;
5015 else
5016 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5017 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5018 IOR_HARD_REG_SET (potential_reload_hard_regs,
5019 reg_class_contents[cl]);
5021 else if (src_regno >= lra_constraint_new_regno_start
5022 && dst_regno < lra_constraint_new_regno_start
5023 && dst_regno >= FIRST_PSEUDO_REGISTER
5024 && reg_renumber[dst_regno] < 0
5025 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5026 && usage_insns[dst_regno].check == curr_usage_insns_check
5027 && (next_usage_insns
5028 = usage_insns[dst_regno].insns) != NULL_RTX)
5030 reloads_num++;
5031 /* 'original_pseudo <- reload_pseudo'. */
5032 if (! JUMP_P (curr_insn)
5033 && inherit_reload_reg (true, dst_regno, cl,
5034 curr_insn, next_usage_insns))
5035 change_p = true;
5036 /* Invalidate. */
5037 usage_insns[dst_regno].check = 0;
5038 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5039 IOR_HARD_REG_SET (potential_reload_hard_regs,
5040 reg_class_contents[cl]);
5042 else if (INSN_P (curr_insn))
5044 int iter;
5045 int max_uid = get_max_uid ();
5047 curr_id = lra_get_insn_recog_data (curr_insn);
5048 curr_static_id = curr_id->insn_static_data;
5049 to_inherit_num = 0;
5050 /* Process insn definitions. */
5051 for (iter = 0; iter < 2; iter++)
5052 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5053 reg != NULL;
5054 reg = reg->next)
5055 if (reg->type != OP_IN
5056 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5058 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5059 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5060 && usage_insns[dst_regno].check == curr_usage_insns_check
5061 && (next_usage_insns
5062 = usage_insns[dst_regno].insns) != NULL_RTX)
5064 struct lra_insn_reg *r;
5066 for (r = curr_id->regs; r != NULL; r = r->next)
5067 if (r->type != OP_OUT && r->regno == dst_regno)
5068 break;
5069 /* Don't do inheritance if the pseudo is also
5070 used in the insn. */
5071 if (r == NULL)
5072 /* We can not do inheritance right now
5073 because the current insn reg info (chain
5074 regs) can change after that. */
5075 add_to_inherit (dst_regno, next_usage_insns);
5077 /* We can not process one reg twice here because of
5078 usage_insns invalidation. */
5079 if ((dst_regno < FIRST_PSEUDO_REGISTER
5080 || reg_renumber[dst_regno] >= 0)
5081 && ! reg->subreg_p && reg->type != OP_IN)
5083 HARD_REG_SET s;
5085 if (split_if_necessary (dst_regno, reg->biggest_mode,
5086 potential_reload_hard_regs,
5087 false, curr_insn, max_uid))
5088 change_p = true;
5089 CLEAR_HARD_REG_SET (s);
5090 if (dst_regno < FIRST_PSEUDO_REGISTER)
5091 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5092 else
5093 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5094 reg_renumber[dst_regno]);
5095 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5097 /* We should invalidate potential inheritance or
5098 splitting for the current insn usages to the next
5099 usage insns (see code below) as the output pseudo
5100 prevents this. */
5101 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5102 && reg_renumber[dst_regno] < 0)
5103 || (reg->type == OP_OUT && ! reg->subreg_p
5104 && (dst_regno < FIRST_PSEUDO_REGISTER
5105 || reg_renumber[dst_regno] >= 0)))
5107 /* Invalidate and mark definitions. */
5108 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5109 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5110 else
5112 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5113 for (i = 0; i < nregs; i++)
5114 usage_insns[dst_regno + i].check
5115 = -(int) INSN_UID (curr_insn);
5119 if (! JUMP_P (curr_insn))
5120 for (i = 0; i < to_inherit_num; i++)
5121 if (inherit_reload_reg (true, to_inherit[i].regno,
5122 ALL_REGS, curr_insn,
5123 to_inherit[i].insns))
5124 change_p = true;
5125 if (CALL_P (curr_insn))
5127 rtx cheap, pat, dest, restore;
5128 int regno, hard_regno;
5130 calls_num++;
5131 if ((cheap = find_reg_note (curr_insn,
5132 REG_RETURNED, NULL_RTX)) != NULL_RTX
5133 && ((cheap = XEXP (cheap, 0)), true)
5134 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5135 && (hard_regno = reg_renumber[regno]) >= 0
5136 /* If there are pending saves/restores, the
5137 optimization is not worth. */
5138 && usage_insns[regno].calls_num == calls_num - 1
5139 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5141 /* Restore the pseudo from the call result as
5142 REG_RETURNED note says that the pseudo value is
5143 in the call result and the pseudo is an argument
5144 of the call. */
5145 pat = PATTERN (curr_insn);
5146 if (GET_CODE (pat) == PARALLEL)
5147 pat = XVECEXP (pat, 0, 0);
5148 dest = SET_DEST (pat);
5149 start_sequence ();
5150 emit_move_insn (cheap, copy_rtx (dest));
5151 restore = get_insns ();
5152 end_sequence ();
5153 lra_process_new_insns (curr_insn, NULL, restore,
5154 "Inserting call parameter restore");
5155 /* We don't need to save/restore of the pseudo from
5156 this call. */
5157 usage_insns[regno].calls_num = calls_num;
5158 bitmap_set_bit (&check_only_regs, regno);
5161 to_inherit_num = 0;
5162 /* Process insn usages. */
5163 for (iter = 0; iter < 2; iter++)
5164 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5165 reg != NULL;
5166 reg = reg->next)
5167 if ((reg->type != OP_OUT
5168 || (reg->type == OP_OUT && reg->subreg_p))
5169 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5171 if (src_regno >= FIRST_PSEUDO_REGISTER
5172 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5174 if (usage_insns[src_regno].check == curr_usage_insns_check
5175 && (next_usage_insns
5176 = usage_insns[src_regno].insns) != NULL_RTX
5177 && NONDEBUG_INSN_P (curr_insn))
5178 add_to_inherit (src_regno, next_usage_insns);
5179 else if (usage_insns[src_regno].check
5180 != -(int) INSN_UID (curr_insn))
5181 /* Add usages but only if the reg is not set up
5182 in the same insn. */
5183 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5185 else if (src_regno < FIRST_PSEUDO_REGISTER
5186 || reg_renumber[src_regno] >= 0)
5188 bool before_p;
5189 rtx use_insn = curr_insn;
5191 before_p = (JUMP_P (curr_insn)
5192 || (CALL_P (curr_insn) && reg->type == OP_IN));
5193 if (NONDEBUG_INSN_P (curr_insn)
5194 && split_if_necessary (src_regno, reg->biggest_mode,
5195 potential_reload_hard_regs,
5196 before_p, curr_insn, max_uid))
5198 if (reg->subreg_p)
5199 lra_risky_transformations_p = true;
5200 change_p = true;
5201 /* Invalidate. */
5202 usage_insns[src_regno].check = 0;
5203 if (before_p)
5204 use_insn = PREV_INSN (curr_insn);
5206 if (NONDEBUG_INSN_P (curr_insn))
5208 if (src_regno < FIRST_PSEUDO_REGISTER)
5209 add_to_hard_reg_set (&live_hard_regs,
5210 reg->biggest_mode, src_regno);
5211 else
5212 add_to_hard_reg_set (&live_hard_regs,
5213 PSEUDO_REGNO_MODE (src_regno),
5214 reg_renumber[src_regno]);
5216 add_next_usage_insn (src_regno, use_insn, reloads_num);
5219 for (i = 0; i < to_inherit_num; i++)
5221 src_regno = to_inherit[i].regno;
5222 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5223 curr_insn, to_inherit[i].insns))
5224 change_p = true;
5225 else
5226 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5229 /* We reached the start of the current basic block. */
5230 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5231 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5233 /* We reached the beginning of the current block -- do
5234 rest of spliting in the current BB. */
5235 to_process = df_get_live_in (curr_bb);
5236 if (BLOCK_FOR_INSN (head) != curr_bb)
5238 /* We are somewhere in the middle of EBB. */
5239 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5240 curr_bb, &temp_bitmap);
5241 to_process = &temp_bitmap;
5243 head_p = true;
5244 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5246 if ((int) j >= lra_constraint_new_regno_start)
5247 break;
5248 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5249 && usage_insns[j].check == curr_usage_insns_check
5250 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5252 if (need_for_split_p (potential_reload_hard_regs, j))
5254 if (lra_dump_file != NULL && head_p)
5256 fprintf (lra_dump_file,
5257 " ----------------------------------\n");
5258 head_p = false;
5260 if (split_reg (false, j, bb_note (curr_bb),
5261 next_usage_insns))
5262 change_p = true;
5264 usage_insns[j].check = 0;
5269 return change_p;
5272 /* This value affects EBB forming. If probability of edge from EBB to
5273 a BB is not greater than the following value, we don't add the BB
5274 to EBB. */
5275 #define EBB_PROBABILITY_CUTOFF ((REG_BR_PROB_BASE * 50) / 100)
5277 /* Current number of inheritance/split iteration. */
5278 int lra_inheritance_iter;
5280 /* Entry function for inheritance/split pass. */
5281 void
5282 lra_inheritance (void)
5284 int i;
5285 basic_block bb, start_bb;
5286 edge e;
5288 lra_inheritance_iter++;
5289 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5290 return;
5291 timevar_push (TV_LRA_INHERITANCE);
5292 if (lra_dump_file != NULL)
5293 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5294 lra_inheritance_iter);
5295 curr_usage_insns_check = 0;
5296 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5297 for (i = 0; i < lra_constraint_new_regno_start; i++)
5298 usage_insns[i].check = 0;
5299 bitmap_initialize (&check_only_regs, &reg_obstack);
5300 bitmap_initialize (&live_regs, &reg_obstack);
5301 bitmap_initialize (&temp_bitmap, &reg_obstack);
5302 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5303 FOR_EACH_BB (bb)
5305 start_bb = bb;
5306 if (lra_dump_file != NULL)
5307 fprintf (lra_dump_file, "EBB");
5308 /* Form a EBB starting with BB. */
5309 bitmap_clear (&ebb_global_regs);
5310 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5311 for (;;)
5313 if (lra_dump_file != NULL)
5314 fprintf (lra_dump_file, " %d", bb->index);
5315 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5316 || LABEL_P (BB_HEAD (bb->next_bb)))
5317 break;
5318 e = find_fallthru_edge (bb->succs);
5319 if (! e)
5320 break;
5321 if (e->probability <= EBB_PROBABILITY_CUTOFF)
5322 break;
5323 bb = bb->next_bb;
5325 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5326 if (lra_dump_file != NULL)
5327 fprintf (lra_dump_file, "\n");
5328 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5329 /* Remember that the EBB head and tail can change in
5330 inherit_in_ebb. */
5331 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5333 bitmap_clear (&ebb_global_regs);
5334 bitmap_clear (&temp_bitmap);
5335 bitmap_clear (&live_regs);
5336 bitmap_clear (&check_only_regs);
5337 free (usage_insns);
5339 timevar_pop (TV_LRA_INHERITANCE);
5344 /* This page contains code to undo failed inheritance/split
5345 transformations. */
5347 /* Current number of iteration undoing inheritance/split. */
5348 int lra_undo_inheritance_iter;
5350 /* Fix BB live info LIVE after removing pseudos created on pass doing
5351 inheritance/split which are REMOVED_PSEUDOS. */
5352 static void
5353 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5355 unsigned int regno;
5356 bitmap_iterator bi;
5358 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5359 if (bitmap_clear_bit (live, regno))
5360 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5363 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5364 number. */
5365 static int
5366 get_regno (rtx reg)
5368 if (GET_CODE (reg) == SUBREG)
5369 reg = SUBREG_REG (reg);
5370 if (REG_P (reg))
5371 return REGNO (reg);
5372 return -1;
5375 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5376 return true if we did any change. The undo transformations for
5377 inheritance looks like
5378 i <- i2
5379 p <- i => p <- i2
5380 or removing
5381 p <- i, i <- p, and i <- i3
5382 where p is original pseudo from which inheritance pseudo i was
5383 created, i and i3 are removed inheritance pseudos, i2 is another
5384 not removed inheritance pseudo. All split pseudos or other
5385 occurrences of removed inheritance pseudos are changed on the
5386 corresponding original pseudos.
5388 The function also schedules insns changed and created during
5389 inheritance/split pass for processing by the subsequent constraint
5390 pass. */
5391 static bool
5392 remove_inheritance_pseudos (bitmap remove_pseudos)
5394 basic_block bb;
5395 int regno, sregno, prev_sregno, dregno, restore_regno;
5396 rtx set, prev_set, prev_insn;
5397 bool change_p, done_p;
5399 change_p = ! bitmap_empty_p (remove_pseudos);
5400 /* We can not finish the function right away if CHANGE_P is true
5401 because we need to marks insns affected by previous
5402 inheritance/split pass for processing by the subsequent
5403 constraint pass. */
5404 FOR_EACH_BB (bb)
5406 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5407 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5408 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5410 if (! INSN_P (curr_insn))
5411 continue;
5412 done_p = false;
5413 sregno = dregno = -1;
5414 if (change_p && NONDEBUG_INSN_P (curr_insn)
5415 && (set = single_set (curr_insn)) != NULL_RTX)
5417 dregno = get_regno (SET_DEST (set));
5418 sregno = get_regno (SET_SRC (set));
5421 if (sregno >= 0 && dregno >= 0)
5423 if ((bitmap_bit_p (remove_pseudos, sregno)
5424 && (lra_reg_info[sregno].restore_regno == dregno
5425 || (bitmap_bit_p (remove_pseudos, dregno)
5426 && (lra_reg_info[sregno].restore_regno
5427 == lra_reg_info[dregno].restore_regno))))
5428 || (bitmap_bit_p (remove_pseudos, dregno)
5429 && lra_reg_info[dregno].restore_regno == sregno))
5430 /* One of the following cases:
5431 original <- removed inheritance pseudo
5432 removed inherit pseudo <- another removed inherit pseudo
5433 removed inherit pseudo <- original pseudo
5435 removed_split_pseudo <- original_reg
5436 original_reg <- removed_split_pseudo */
5438 if (lra_dump_file != NULL)
5440 fprintf (lra_dump_file, " Removing %s:\n",
5441 bitmap_bit_p (&lra_split_regs, sregno)
5442 || bitmap_bit_p (&lra_split_regs, dregno)
5443 ? "split" : "inheritance");
5444 dump_insn_slim (lra_dump_file, curr_insn);
5446 lra_set_insn_deleted (curr_insn);
5447 done_p = true;
5449 else if (bitmap_bit_p (remove_pseudos, sregno)
5450 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5452 /* Search the following pattern:
5453 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5454 original_pseudo <- inherit_or_split_pseudo1
5455 where the 2nd insn is the current insn and
5456 inherit_or_split_pseudo2 is not removed. If it is found,
5457 change the current insn onto:
5458 original_pseudo <- inherit_or_split_pseudo2. */
5459 for (prev_insn = PREV_INSN (curr_insn);
5460 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5461 prev_insn = PREV_INSN (prev_insn))
5463 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5464 && (prev_set = single_set (prev_insn)) != NULL_RTX
5465 /* There should be no subregs in insn we are
5466 searching because only the original reg might
5467 be in subreg when we changed the mode of
5468 load/store for splitting. */
5469 && REG_P (SET_DEST (prev_set))
5470 && REG_P (SET_SRC (prev_set))
5471 && (int) REGNO (SET_DEST (prev_set)) == sregno
5472 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5473 >= FIRST_PSEUDO_REGISTER)
5474 /* As we consider chain of inheritance or
5475 splitting described in above comment we should
5476 check that sregno and prev_sregno were
5477 inheritance/split pseudos created from the
5478 same original regno. */
5479 && (lra_reg_info[sregno].restore_regno
5480 == lra_reg_info[prev_sregno].restore_regno)
5481 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5483 lra_assert (GET_MODE (SET_SRC (prev_set))
5484 == GET_MODE (regno_reg_rtx[sregno]));
5485 if (GET_CODE (SET_SRC (set)) == SUBREG)
5486 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5487 else
5488 SET_SRC (set) = SET_SRC (prev_set);
5489 lra_push_insn_and_update_insn_regno_info (curr_insn);
5490 lra_set_used_insn_alternative_by_uid
5491 (INSN_UID (curr_insn), -1);
5492 done_p = true;
5493 if (lra_dump_file != NULL)
5495 fprintf (lra_dump_file, " Change reload insn:\n");
5496 dump_insn_slim (lra_dump_file, curr_insn);
5501 if (! done_p)
5503 struct lra_insn_reg *reg;
5504 bool restored_regs_p = false;
5505 bool kept_regs_p = false;
5507 curr_id = lra_get_insn_recog_data (curr_insn);
5508 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5510 regno = reg->regno;
5511 restore_regno = lra_reg_info[regno].restore_regno;
5512 if (restore_regno >= 0)
5514 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5516 substitute_pseudo (&curr_insn, regno,
5517 regno_reg_rtx[restore_regno]);
5518 restored_regs_p = true;
5520 else
5521 kept_regs_p = true;
5524 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5526 /* The instruction has changed since the previous
5527 constraints pass. */
5528 lra_push_insn_and_update_insn_regno_info (curr_insn);
5529 lra_set_used_insn_alternative_by_uid
5530 (INSN_UID (curr_insn), -1);
5532 else if (restored_regs_p)
5533 /* The instruction has been restored to the form that
5534 it had during the previous constraints pass. */
5535 lra_update_insn_regno_info (curr_insn);
5536 if (restored_regs_p && lra_dump_file != NULL)
5538 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5539 dump_insn_slim (lra_dump_file, curr_insn);
5544 return change_p;
5547 /* If optional reload pseudos failed to get a hard register or was not
5548 inherited, it is better to remove optional reloads. We do this
5549 transformation after undoing inheritance to figure out necessity to
5550 remove optional reloads easier. Return true if we do any
5551 change. */
5552 static bool
5553 undo_optional_reloads (void)
5555 bool change_p, keep_p;
5556 unsigned int regno, uid;
5557 bitmap_iterator bi, bi2;
5558 rtx insn, set, src, dest;
5559 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5561 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5562 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5563 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5565 keep_p = false;
5566 /* Keep optional reloads from previous subpasses. */
5567 if (lra_reg_info[regno].restore_regno < 0
5568 /* If the original pseudo changed its allocation, just
5569 removing the optional pseudo is dangerous as the original
5570 pseudo will have longer live range. */
5571 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
5572 keep_p = true;
5573 else if (reg_renumber[regno] >= 0)
5574 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
5576 insn = lra_insn_recog_data[uid]->insn;
5577 if ((set = single_set (insn)) == NULL_RTX)
5578 continue;
5579 src = SET_SRC (set);
5580 dest = SET_DEST (set);
5581 if (! REG_P (src) || ! REG_P (dest))
5582 continue;
5583 if (REGNO (dest) == regno
5584 /* Ignore insn for optional reloads itself. */
5585 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
5586 /* Check only inheritance on last inheritance pass. */
5587 && (int) REGNO (src) >= new_regno_start
5588 /* Check that the optional reload was inherited. */
5589 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
5591 keep_p = true;
5592 break;
5595 if (keep_p)
5597 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
5598 if (lra_dump_file != NULL)
5599 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
5602 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
5603 bitmap_initialize (&insn_bitmap, &reg_obstack);
5604 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
5606 if (lra_dump_file != NULL)
5607 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
5608 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
5609 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
5611 insn = lra_insn_recog_data[uid]->insn;
5612 if ((set = single_set (insn)) != NULL_RTX)
5614 src = SET_SRC (set);
5615 dest = SET_DEST (set);
5616 if (REG_P (src) && REG_P (dest)
5617 && ((REGNO (src) == regno
5618 && (lra_reg_info[regno].restore_regno
5619 == (int) REGNO (dest)))
5620 || (REGNO (dest) == regno
5621 && (lra_reg_info[regno].restore_regno
5622 == (int) REGNO (src)))))
5624 if (lra_dump_file != NULL)
5626 fprintf (lra_dump_file, " Deleting move %u\n",
5627 INSN_UID (insn));
5628 dump_insn_slim (lra_dump_file, insn);
5630 lra_set_insn_deleted (insn);
5631 continue;
5633 /* We should not worry about generation memory-memory
5634 moves here as if the corresponding inheritance did
5635 not work (inheritance pseudo did not get a hard reg),
5636 we remove the inheritance pseudo and the optional
5637 reload. */
5639 substitute_pseudo (&insn, regno,
5640 regno_reg_rtx[lra_reg_info[regno].restore_regno]);
5641 lra_update_insn_regno_info (insn);
5642 if (lra_dump_file != NULL)
5644 fprintf (lra_dump_file,
5645 " Restoring original insn:\n");
5646 dump_insn_slim (lra_dump_file, insn);
5650 /* Clear restore_regnos. */
5651 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5652 lra_reg_info[regno].restore_regno = -1;
5653 bitmap_clear (&insn_bitmap);
5654 bitmap_clear (&removed_optional_reload_pseudos);
5655 return change_p;
5658 /* Entry function for undoing inheritance/split transformation. Return true
5659 if we did any RTL change in this pass. */
5660 bool
5661 lra_undo_inheritance (void)
5663 unsigned int regno;
5664 int restore_regno, hard_regno;
5665 int n_all_inherit, n_inherit, n_all_split, n_split;
5666 bitmap_head remove_pseudos;
5667 bitmap_iterator bi;
5668 bool change_p;
5670 lra_undo_inheritance_iter++;
5671 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5672 return false;
5673 if (lra_dump_file != NULL)
5674 fprintf (lra_dump_file,
5675 "\n********** Undoing inheritance #%d: **********\n\n",
5676 lra_undo_inheritance_iter);
5677 bitmap_initialize (&remove_pseudos, &reg_obstack);
5678 n_inherit = n_all_inherit = 0;
5679 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5680 if (lra_reg_info[regno].restore_regno >= 0)
5682 n_all_inherit++;
5683 if (reg_renumber[regno] < 0
5684 /* If the original pseudo changed its allocation, just
5685 removing inheritance is dangerous as for changing
5686 allocation we used shorter live-ranges. */
5687 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
5688 bitmap_set_bit (&remove_pseudos, regno);
5689 else
5690 n_inherit++;
5692 if (lra_dump_file != NULL && n_all_inherit != 0)
5693 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
5694 n_inherit, n_all_inherit,
5695 (double) n_inherit / n_all_inherit * 100);
5696 n_split = n_all_split = 0;
5697 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5698 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
5700 n_all_split++;
5701 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
5702 ? reg_renumber[restore_regno] : restore_regno);
5703 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
5704 bitmap_set_bit (&remove_pseudos, regno);
5705 else
5707 n_split++;
5708 if (lra_dump_file != NULL)
5709 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
5710 regno, restore_regno);
5713 if (lra_dump_file != NULL && n_all_split != 0)
5714 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
5715 n_split, n_all_split,
5716 (double) n_split / n_all_split * 100);
5717 change_p = remove_inheritance_pseudos (&remove_pseudos);
5718 bitmap_clear (&remove_pseudos);
5719 /* Clear restore_regnos. */
5720 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5721 lra_reg_info[regno].restore_regno = -1;
5722 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5723 lra_reg_info[regno].restore_regno = -1;
5724 change_p = undo_optional_reloads () || change_p;
5725 return change_p;