c-family/
[official-gcc.git] / gcc / lra-constraints.c
blob4b357261bbe24f7232448ff2aa584602f39a8b48
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010, 2011, 2012
3 Free Software Foundation, Inc.
4 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file contains code for 3 passes: constraint pass,
24 inheritance/split pass, and pass for undoing failed inheritance and
25 split.
27 The major goal of constraint pass is to transform RTL to satisfy
28 insn and address constraints by:
29 o choosing insn alternatives;
30 o generating *reload insns* (or reloads in brief) and *reload
31 pseudos* which will get necessary hard registers later;
32 o substituting pseudos with equivalent values and removing the
33 instructions that initialized those pseudos.
35 The constraint pass has biggest and most complicated code in LRA.
36 There are a lot of important details like:
37 o reuse of input reload pseudos to simplify reload pseudo
38 allocations;
39 o some heuristics to choose insn alternative to improve the
40 inheritance;
41 o early clobbers etc.
43 The pass is mimicking former reload pass in alternative choosing
44 because the reload pass is oriented to current machine description
45 model. It might be changed if the machine description model is
46 changed.
48 There is special code for preventing all LRA and this pass cycling
49 in case of bugs.
51 On the first iteration of the pass we process every instruction and
52 choose an alternative for each one. On subsequent iterations we try
53 to avoid reprocessing instructions if we can be sure that the old
54 choice is still valid.
56 The inheritance/spilt pass is to transform code to achieve
57 ineheritance and live range splitting. It is done on backward
58 traversal of EBBs.
60 The inheritance optimization goal is to reuse values in hard
61 registers. There is analogous optimization in old reload pass. The
62 inheritance is achieved by following transformation:
64 reload_p1 <- p reload_p1 <- p
65 ... new_p <- reload_p1
66 ... => ...
67 reload_p2 <- p reload_p2 <- new_p
69 where p is spilled and not changed between the insns. Reload_p1 is
70 also called *original pseudo* and new_p is called *inheritance
71 pseudo*.
73 The subsequent assignment pass will try to assign the same (or
74 another if it is not possible) hard register to new_p as to
75 reload_p1 or reload_p2.
77 If the assignment pass fails to assign a hard register to new_p,
78 this file will undo the inheritance and restore the original code.
79 This is because implementing the above sequence with a spilled
80 new_p would make the code much worse. The inheritance is done in
81 EBB scope. The above is just a simplified example to get an idea
82 of the inheritance as the inheritance is also done for non-reload
83 insns.
85 Splitting (transformation) is also done in EBB scope on the same
86 pass as the inheritance:
88 r <- ... or ... <- r r <- ... or ... <- r
89 ... s <- r (new insn -- save)
90 ... =>
91 ... r <- s (new insn -- restore)
92 ... <- r ... <- r
94 The *split pseudo* s is assigned to the hard register of the
95 original pseudo or hard register r.
97 Splitting is done:
98 o In EBBs with high register pressure for global pseudos (living
99 in at least 2 BBs) and assigned to hard registers when there
100 are more one reloads needing the hard registers;
101 o for pseudos needing save/restore code around calls.
103 If the split pseudo still has the same hard register as the
104 original pseudo after the subsequent assignment pass or the
105 original pseudo was split, the opposite transformation is done on
106 the same pass for undoing inheritance. */
108 #undef REG_OK_STRICT
110 #include "config.h"
111 #include "system.h"
112 #include "coretypes.h"
113 #include "tm.h"
114 #include "hard-reg-set.h"
115 #include "rtl.h"
116 #include "tm_p.h"
117 #include "regs.h"
118 #include "insn-config.h"
119 #include "insn-codes.h"
120 #include "recog.h"
121 #include "output.h"
122 #include "addresses.h"
123 #include "target.h"
124 #include "function.h"
125 #include "expr.h"
126 #include "basic-block.h"
127 #include "except.h"
128 #include "optabs.h"
129 #include "df.h"
130 #include "ira.h"
131 #include "rtl-error.h"
132 #include "lra-int.h"
134 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
135 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
136 reload insns. */
137 static int bb_reload_num;
139 /* The current insn being processed and corresponding its data (basic
140 block, the insn data, the insn static data, and the mode of each
141 operand). */
142 static rtx curr_insn;
143 static basic_block curr_bb;
144 static lra_insn_recog_data_t curr_id;
145 static struct lra_static_insn_data *curr_static_id;
146 static enum machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
150 /* Start numbers for new registers and insns at the current constraints
151 pass start. */
152 static int new_regno_start;
153 static int new_insn_uid_start;
155 /* Return hard regno of REGNO or if it is was not assigned to a hard
156 register, use a hard register from its allocno class. */
157 static int
158 get_try_hard_regno (int regno)
160 int hard_regno;
161 enum reg_class rclass;
163 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
164 hard_regno = lra_get_regno_hard_regno (regno);
165 if (hard_regno >= 0)
166 return hard_regno;
167 rclass = lra_get_allocno_class (regno);
168 if (rclass == NO_REGS)
169 return -1;
170 return ira_class_hard_regs[rclass][0];
173 /* Return final hard regno (plus offset) which will be after
174 elimination. We do this for matching constraints because the final
175 hard regno could have a different class. */
176 static int
177 get_final_hard_regno (int hard_regno, int offset)
179 if (hard_regno < 0)
180 return hard_regno;
181 hard_regno = lra_get_elimination_hard_regno (hard_regno);
182 return hard_regno + offset;
185 /* Return hard regno of X after removing subreg and making
186 elimination. If X is not a register or subreg of register, return
187 -1. For pseudo use its assignment. */
188 static int
189 get_hard_regno (rtx x)
191 rtx reg;
192 int offset, hard_regno;
194 reg = x;
195 if (GET_CODE (x) == SUBREG)
196 reg = SUBREG_REG (x);
197 if (! REG_P (reg))
198 return -1;
199 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
200 hard_regno = lra_get_regno_hard_regno (hard_regno);
201 if (hard_regno < 0)
202 return -1;
203 offset = 0;
204 if (GET_CODE (x) == SUBREG)
205 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
206 SUBREG_BYTE (x), GET_MODE (x));
207 return get_final_hard_regno (hard_regno, offset);
210 /* If REGNO is a hard register or has been allocated a hard register,
211 return the class of that register. If REGNO is a reload pseudo
212 created by the current constraints pass, return its allocno class.
213 Return NO_REGS otherwise. */
214 static enum reg_class
215 get_reg_class (int regno)
217 int hard_regno;
219 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
220 hard_regno = lra_get_regno_hard_regno (regno);
221 if (hard_regno >= 0)
223 hard_regno = get_final_hard_regno (hard_regno, 0);
224 return REGNO_REG_CLASS (hard_regno);
226 if (regno >= new_regno_start)
227 return lra_get_allocno_class (regno);
228 return NO_REGS;
231 /* Return true if REG satisfies (or will satisfy) reg class constraint
232 CL. Use elimination first if REG is a hard register. If REG is a
233 reload pseudo created by this constraints pass, assume that it will
234 be allocated a hard register from its allocno class, but allow that
235 class to be narrowed to CL if it is currently a superset of CL.
237 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
238 REGNO (reg), or NO_REGS if no change in its class was needed. */
239 static bool
240 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
242 enum reg_class rclass, common_class;
243 enum machine_mode reg_mode;
244 int class_size, hard_regno, nregs, i, j;
245 int regno = REGNO (reg);
247 if (new_class != NULL)
248 *new_class = NO_REGS;
249 if (regno < FIRST_PSEUDO_REGISTER)
251 rtx final_reg = reg;
252 rtx *final_loc = &final_reg;
254 lra_eliminate_reg_if_possible (final_loc);
255 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
257 reg_mode = GET_MODE (reg);
258 rclass = get_reg_class (regno);
259 if (regno < new_regno_start
260 /* Do not allow the constraints for reload instructions to
261 influence the classes of new pseudos. These reloads are
262 typically moves that have many alternatives, and restricting
263 reload pseudos for one alternative may lead to situations
264 where other reload pseudos are no longer allocatable. */
265 || INSN_UID (curr_insn) >= new_insn_uid_start)
266 /* When we don't know what class will be used finally for reload
267 pseudos, we use ALL_REGS. */
268 return ((regno >= new_regno_start && rclass == ALL_REGS)
269 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
270 && ! hard_reg_set_subset_p (reg_class_contents[cl],
271 lra_no_alloc_regs)));
272 else
274 common_class = ira_reg_class_subset[rclass][cl];
275 if (new_class != NULL)
276 *new_class = common_class;
277 if (hard_reg_set_subset_p (reg_class_contents[common_class],
278 lra_no_alloc_regs))
279 return false;
280 /* Check that there are enough allocatable regs. */
281 class_size = ira_class_hard_regs_num[common_class];
282 for (i = 0; i < class_size; i++)
284 hard_regno = ira_class_hard_regs[common_class][i];
285 nregs = hard_regno_nregs[hard_regno][reg_mode];
286 if (nregs == 1)
287 return true;
288 for (j = 0; j < nregs; j++)
289 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j))
290 break;
291 if (j >= nregs)
292 return true;
294 return false;
298 /* Return true if REGNO satisfies a memory constraint. */
299 static bool
300 in_mem_p (int regno)
302 return get_reg_class (regno) == NO_REGS;
305 /* If we have decided to substitute X with another value, return that
306 value, otherwise return X. */
307 static rtx
308 get_equiv_substitution (rtx x)
310 int regno;
311 rtx res;
313 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
314 || ! ira_reg_equiv[regno].defined_p
315 || ! ira_reg_equiv[regno].profitable_p
316 || lra_get_regno_hard_regno (regno) >= 0)
317 return x;
318 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
319 return res;
320 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
321 return res;
322 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
323 return res;
324 gcc_unreachable ();
327 /* Set up curr_operand_mode. */
328 static void
329 init_curr_operand_mode (void)
331 int nop = curr_static_id->n_operands;
332 for (int i = 0; i < nop; i++)
334 enum machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
335 if (mode == VOIDmode)
337 /* The .md mode for address operands is the mode of the
338 addressed value rather than the mode of the address itself. */
339 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
340 mode = Pmode;
341 else
342 mode = curr_static_id->operand[i].mode;
344 curr_operand_mode[i] = mode;
350 /* The page contains code to reuse input reloads. */
352 /* Structure describes input reload of the current insns. */
353 struct input_reload
355 /* Reloaded value. */
356 rtx input;
357 /* Reload pseudo used. */
358 rtx reg;
361 /* The number of elements in the following array. */
362 static int curr_insn_input_reloads_num;
363 /* Array containing info about input reloads. It is used to find the
364 same input reload and reuse the reload pseudo in this case. */
365 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
367 /* Initiate data concerning reuse of input reloads for the current
368 insn. */
369 static void
370 init_curr_insn_input_reloads (void)
372 curr_insn_input_reloads_num = 0;
375 /* Change class of pseudo REGNO to NEW_CLASS. Print info about it
376 using TITLE. Output a new line if NL_P. */
377 static void
378 change_class (int regno, enum reg_class new_class,
379 const char *title, bool nl_p)
381 lra_assert (regno >= FIRST_PSEUDO_REGISTER);
382 if (lra_dump_file != NULL)
383 fprintf (lra_dump_file, "%s to class %s for r%d",
384 title, reg_class_names[new_class], regno);
385 setup_reg_classes (regno, new_class, NO_REGS, new_class);
386 if (lra_dump_file != NULL && nl_p)
387 fprintf (lra_dump_file, "\n");
390 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
391 created input reload pseudo (only if TYPE is not OP_OUT). The
392 result pseudo is returned through RESULT_REG. Return TRUE if we
393 created a new pseudo, FALSE if we reused the already created input
394 reload pseudo. Use TITLE to describe new registers for debug
395 purposes. */
396 static bool
397 get_reload_reg (enum op_type type, enum machine_mode mode, rtx original,
398 enum reg_class rclass, const char *title, rtx *result_reg)
400 int i, regno;
401 enum reg_class new_class;
403 if (type == OP_OUT)
405 *result_reg
406 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
407 return true;
409 for (i = 0; i < curr_insn_input_reloads_num; i++)
410 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
411 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
413 lra_assert (! side_effects_p (original));
414 *result_reg = curr_insn_input_reloads[i].reg;
415 regno = REGNO (*result_reg);
416 if (lra_dump_file != NULL)
418 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
419 print_value_slim (lra_dump_file, original, 1);
421 if (rclass != new_class)
422 change_class (regno, new_class, ", change", false);
423 if (lra_dump_file != NULL)
424 fprintf (lra_dump_file, "\n");
425 return false;
427 *result_reg = lra_create_new_reg (mode, original, rclass, title);
428 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
429 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
430 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
431 return true;
436 /* The page contains code to extract memory address parts. */
438 /* Info about base and index regs of an address. In some rare cases,
439 base/index register can be actually memory. In this case we will
440 reload it. */
441 struct address
443 /* NULL if there is no a base register. */
444 rtx *base_reg_loc;
445 /* Second location of {post/pre}_modify, NULL otherwise. */
446 rtx *base_reg_loc2;
447 /* NULL if there is no an index register. */
448 rtx *index_reg_loc;
449 /* Location of index reg * scale or index_reg_loc otherwise. */
450 rtx *index_loc;
451 /* NULL if there is no a displacement. */
452 rtx *disp_loc;
453 /* Defined if base_reg_loc is not NULL. */
454 enum rtx_code base_outer_code, index_code;
455 /* True if the base register is modified in the address, for
456 example, in PRE_INC. */
457 bool base_modify_p;
460 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
461 static inline bool
462 ok_for_index_p_nonstrict (rtx reg)
464 unsigned regno = REGNO (reg);
466 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
469 /* A version of regno_ok_for_base_p for use here, when all pseudos
470 should count as OK. Arguments as for regno_ok_for_base_p. */
471 static inline bool
472 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode, addr_space_t as,
473 enum rtx_code outer_code, enum rtx_code index_code)
475 unsigned regno = REGNO (reg);
477 if (regno >= FIRST_PSEUDO_REGISTER)
478 return true;
479 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
482 /* Process address part in space AS (or all address if TOP_P) with
483 location *LOC to extract address characteristics.
485 If CONTEXT_P is false, we are looking at the base part of an
486 address, otherwise we are looking at the index part.
488 MODE is the mode of the memory reference; OUTER_CODE and INDEX_CODE
489 give the context that the rtx appears in; MODIFY_P if *LOC is
490 modified. */
491 static void
492 extract_loc_address_regs (bool top_p, enum machine_mode mode, addr_space_t as,
493 rtx *loc, bool context_p, enum rtx_code outer_code,
494 enum rtx_code index_code,
495 bool modify_p, struct address *ad)
497 rtx x = *loc;
498 enum rtx_code code = GET_CODE (x);
499 bool base_ok_p;
501 switch (code)
503 case CONST_INT:
504 case CONST:
505 case SYMBOL_REF:
506 case LABEL_REF:
507 if (! context_p)
509 lra_assert (top_p);
510 ad->disp_loc = loc;
512 return;
514 case CC0:
515 case PC:
516 return;
518 case ZERO_EXTEND:
519 /* Pass TOP_P for displacement. */
520 extract_loc_address_regs (top_p, mode, as, &XEXP (*loc, 0), context_p,
521 code, index_code, modify_p, ad);
522 return;
524 case PLUS:
525 case LO_SUM:
526 /* When we have an address that is a sum, we must determine
527 whether registers are "base" or "index" regs. If there is a
528 sum of two registers, we must choose one to be the
529 "base". */
531 rtx *arg0_loc = &XEXP (x, 0);
532 rtx *arg1_loc = &XEXP (x, 1);
533 rtx *tloc;
534 rtx arg0 = *arg0_loc;
535 rtx arg1 = *arg1_loc;
536 enum rtx_code code0 = GET_CODE (arg0);
537 enum rtx_code code1 = GET_CODE (arg1);
539 /* Look inside subregs. */
540 if (code0 == SUBREG)
542 arg0_loc = &SUBREG_REG (arg0);
543 arg0 = *arg0_loc;
544 code0 = GET_CODE (arg0);
546 if (code1 == SUBREG)
548 arg1_loc = &SUBREG_REG (arg1);
549 arg1 = *arg1_loc;
550 code1 = GET_CODE (arg1);
553 if (CONSTANT_P (arg0)
554 || code1 == PLUS || code1 == MULT || code1 == ASHIFT)
556 tloc = arg1_loc;
557 arg1_loc = arg0_loc;
558 arg0_loc = tloc;
559 arg0 = *arg0_loc;
560 code0 = GET_CODE (arg0);
561 arg1 = *arg1_loc;
562 code1 = GET_CODE (arg1);
564 /* If this machine only allows one register per address, it
565 must be in the first operand. */
566 if (MAX_REGS_PER_ADDRESS == 1 || code == LO_SUM)
568 lra_assert (ad->disp_loc == NULL);
569 ad->disp_loc = arg1_loc;
570 extract_loc_address_regs (false, mode, as, arg0_loc, false, code,
571 code1, modify_p, ad);
573 /* Base + disp addressing */
574 else if (code0 != PLUS && code0 != MULT && code0 != ASHIFT
575 && CONSTANT_P (arg1))
577 lra_assert (ad->disp_loc == NULL);
578 ad->disp_loc = arg1_loc;
579 extract_loc_address_regs (false, mode, as, arg0_loc, false, PLUS,
580 code1, modify_p, ad);
582 /* If index and base registers are the same on this machine,
583 just record registers in any non-constant operands. We
584 assume here, as well as in the tests below, that all
585 addresses are in canonical form. */
586 else if (INDEX_REG_CLASS
587 == base_reg_class (VOIDmode, as, PLUS, SCRATCH)
588 && code0 != PLUS && code0 != MULT && code0 != ASHIFT)
590 extract_loc_address_regs (false, mode, as, arg0_loc, false, PLUS,
591 code1, modify_p, ad);
592 lra_assert (! CONSTANT_P (arg1));
593 extract_loc_address_regs (false, mode, as, arg1_loc, true, PLUS,
594 code0, modify_p, ad);
596 /* It might be [base + ]index * scale + disp. */
597 else if (CONSTANT_P (arg1))
599 lra_assert (ad->disp_loc == NULL);
600 ad->disp_loc = arg1_loc;
601 extract_loc_address_regs (false, mode, as, arg0_loc, context_p,
602 PLUS, code0, modify_p, ad);
604 /* If both operands are registers but one is already a hard
605 register of index or reg-base class, give the other the
606 class that the hard register is not. */
607 else if (code0 == REG && code1 == REG
608 && REGNO (arg0) < FIRST_PSEUDO_REGISTER
609 && ((base_ok_p
610 = ok_for_base_p_nonstrict (arg0, mode, as, PLUS, REG))
611 || ok_for_index_p_nonstrict (arg0)))
613 extract_loc_address_regs (false, mode, as, arg0_loc, ! base_ok_p,
614 PLUS, REG, modify_p, ad);
615 extract_loc_address_regs (false, mode, as, arg1_loc, base_ok_p,
616 PLUS, REG, modify_p, ad);
618 else if (code0 == REG && code1 == REG
619 && REGNO (arg1) < FIRST_PSEUDO_REGISTER
620 && ((base_ok_p
621 = ok_for_base_p_nonstrict (arg1, mode, as, PLUS, REG))
622 || ok_for_index_p_nonstrict (arg1)))
624 extract_loc_address_regs (false, mode, as, arg0_loc, base_ok_p,
625 PLUS, REG, modify_p, ad);
626 extract_loc_address_regs (false, mode, as, arg1_loc, ! base_ok_p,
627 PLUS, REG, modify_p, ad);
629 /* Otherwise, count equal chances that each might be a base or
630 index register. This case should be rare. */
631 else
633 extract_loc_address_regs (false, mode, as, arg0_loc, false, PLUS,
634 code1, modify_p, ad);
635 extract_loc_address_regs (false, mode, as, arg1_loc,
636 ad->base_reg_loc != NULL, PLUS,
637 code0, modify_p, ad);
640 break;
642 case MULT:
643 case ASHIFT:
645 rtx *arg0_loc = &XEXP (x, 0);
646 enum rtx_code code0 = GET_CODE (*arg0_loc);
648 if (code0 == CONST_INT)
649 arg0_loc = &XEXP (x, 1);
650 extract_loc_address_regs (false, mode, as, arg0_loc, true,
651 outer_code, code, modify_p, ad);
652 lra_assert (ad->index_loc == NULL);
653 ad->index_loc = loc;
654 break;
657 case POST_MODIFY:
658 case PRE_MODIFY:
659 extract_loc_address_regs (false, mode, as, &XEXP (x, 0), false,
660 code, GET_CODE (XEXP (XEXP (x, 1), 1)),
661 true, ad);
662 lra_assert (rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)));
663 ad->base_reg_loc2 = &XEXP (XEXP (x, 1), 0);
664 if (REG_P (XEXP (XEXP (x, 1), 1)))
665 extract_loc_address_regs (false, mode, as, &XEXP (XEXP (x, 1), 1),
666 true, code, REG, modify_p, ad);
667 break;
669 case POST_INC:
670 case PRE_INC:
671 case POST_DEC:
672 case PRE_DEC:
673 extract_loc_address_regs (false, mode, as, &XEXP (x, 0), false, code,
674 SCRATCH, true, ad);
675 break;
677 /* We process memory as a register. That means we flatten
678 addresses. In other words, the final code will never
679 contains memory in an address even if the target supports
680 such addresses (it is too rare these days). Memory also can
681 occur in address as a result some previous transformations
682 like equivalence substitution. */
683 case MEM:
684 case REG:
685 if (context_p)
687 lra_assert (ad->index_reg_loc == NULL);
688 ad->index_reg_loc = loc;
690 else
692 lra_assert (ad->base_reg_loc == NULL);
693 ad->base_reg_loc = loc;
694 ad->base_outer_code = outer_code;
695 ad->index_code = index_code;
696 ad->base_modify_p = modify_p;
698 break;
699 default:
701 const char *fmt = GET_RTX_FORMAT (code);
702 int i;
704 if (GET_RTX_LENGTH (code) != 1
705 || fmt[0] != 'e' || GET_CODE (XEXP (x, 0)) != UNSPEC)
707 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
708 if (fmt[i] == 'e')
709 extract_loc_address_regs (false, mode, as, &XEXP (x, i),
710 context_p, code, SCRATCH,
711 modify_p, ad);
712 break;
714 /* fall through for case UNARY_OP (UNSPEC ...) */
717 case UNSPEC:
718 if (ad->disp_loc == NULL)
719 ad->disp_loc = loc;
720 else if (ad->base_reg_loc == NULL)
722 ad->base_reg_loc = loc;
723 ad->base_outer_code = outer_code;
724 ad->index_code = index_code;
725 ad->base_modify_p = modify_p;
727 else
729 lra_assert (ad->index_reg_loc == NULL);
730 ad->index_reg_loc = loc;
732 break;
738 /* Describe address *LOC in AD. There are two cases:
739 - *LOC is the address in a (mem ...). In this case OUTER_CODE is MEM
740 and AS is the mem's address space.
741 - *LOC is matched to an address constraint such as 'p'. In this case
742 OUTER_CODE is ADDRESS and AS is ADDR_SPACE_GENERIC. */
743 static void
744 extract_address_regs (enum machine_mode mem_mode, addr_space_t as,
745 rtx *loc, enum rtx_code outer_code, struct address *ad)
747 ad->base_reg_loc = ad->base_reg_loc2
748 = ad->index_reg_loc = ad->index_loc = ad->disp_loc = NULL;
749 ad->base_outer_code = SCRATCH;
750 ad->index_code = SCRATCH;
751 ad->base_modify_p = false;
752 extract_loc_address_regs (true, mem_mode, as, loc, false, outer_code,
753 SCRATCH, false, ad);
754 if (ad->index_loc == NULL)
755 /* SUBREG ??? */
756 ad->index_loc = ad->index_reg_loc;
761 /* The page contains major code to choose the current insn alternative
762 and generate reloads for it. */
764 /* Return the offset from REGNO of the least significant register
765 in (reg:MODE REGNO).
767 This function is used to tell whether two registers satisfy
768 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
770 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
771 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
773 lra_constraint_offset (int regno, enum machine_mode mode)
775 lra_assert (regno < FIRST_PSEUDO_REGISTER);
776 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
777 && SCALAR_INT_MODE_P (mode))
778 return hard_regno_nregs[regno][mode] - 1;
779 return 0;
782 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
783 if they are the same hard reg, and has special hacks for
784 auto-increment and auto-decrement. This is specifically intended for
785 process_alt_operands to use in determining whether two operands
786 match. X is the operand whose number is the lower of the two.
788 It is supposed that X is the output operand and Y is the input
789 operand. Y_HARD_REGNO is the final hard regno of register Y or
790 register in subreg Y as we know it now. Otherwise, it is a
791 negative value. */
792 static bool
793 operands_match_p (rtx x, rtx y, int y_hard_regno)
795 int i;
796 RTX_CODE code = GET_CODE (x);
797 const char *fmt;
799 if (x == y)
800 return true;
801 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
802 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
804 int j;
806 i = get_hard_regno (x);
807 if (i < 0)
808 goto slow;
810 if ((j = y_hard_regno) < 0)
811 goto slow;
813 i += lra_constraint_offset (i, GET_MODE (x));
814 j += lra_constraint_offset (j, GET_MODE (y));
816 return i == j;
819 /* If two operands must match, because they are really a single
820 operand of an assembler insn, then two post-increments are invalid
821 because the assembler insn would increment only once. On the
822 other hand, a post-increment matches ordinary indexing if the
823 post-increment is the output operand. */
824 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
825 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
827 /* Two pre-increments are invalid because the assembler insn would
828 increment only once. On the other hand, a pre-increment matches
829 ordinary indexing if the pre-increment is the input operand. */
830 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
831 || GET_CODE (y) == PRE_MODIFY)
832 return operands_match_p (x, XEXP (y, 0), -1);
834 slow:
836 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
837 && x == SUBREG_REG (y))
838 return true;
839 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
840 && SUBREG_REG (x) == y)
841 return true;
843 /* Now we have disposed of all the cases in which different rtx
844 codes can match. */
845 if (code != GET_CODE (y))
846 return false;
848 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
849 if (GET_MODE (x) != GET_MODE (y))
850 return false;
852 switch (code)
854 CASE_CONST_UNIQUE:
855 return false;
857 case LABEL_REF:
858 return XEXP (x, 0) == XEXP (y, 0);
859 case SYMBOL_REF:
860 return XSTR (x, 0) == XSTR (y, 0);
862 default:
863 break;
866 /* Compare the elements. If any pair of corresponding elements fail
867 to match, return false for the whole things. */
869 fmt = GET_RTX_FORMAT (code);
870 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
872 int val, j;
873 switch (fmt[i])
875 case 'w':
876 if (XWINT (x, i) != XWINT (y, i))
877 return false;
878 break;
880 case 'i':
881 if (XINT (x, i) != XINT (y, i))
882 return false;
883 break;
885 case 'e':
886 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
887 if (val == 0)
888 return false;
889 break;
891 case '0':
892 break;
894 case 'E':
895 if (XVECLEN (x, i) != XVECLEN (y, i))
896 return false;
897 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
899 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
900 if (val == 0)
901 return false;
903 break;
905 /* It is believed that rtx's at this level will never
906 contain anything but integers and other rtx's, except for
907 within LABEL_REFs and SYMBOL_REFs. */
908 default:
909 gcc_unreachable ();
912 return true;
915 /* True if X is a constant that can be forced into the constant pool.
916 MODE is the mode of the operand, or VOIDmode if not known. */
917 #define CONST_POOL_OK_P(MODE, X) \
918 ((MODE) != VOIDmode \
919 && CONSTANT_P (X) \
920 && GET_CODE (X) != HIGH \
921 && !targetm.cannot_force_const_mem (MODE, X))
923 /* True if C is a non-empty register class that has too few registers
924 to be safely used as a reload target class. */
925 #define SMALL_REGISTER_CLASS_P(C) \
926 (reg_class_size [(C)] == 1 \
927 || (reg_class_size [(C)] >= 1 && targetm.class_likely_spilled_p (C)))
929 /* If REG is a reload pseudo, try to make its class satisfying CL. */
930 static void
931 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
933 enum reg_class rclass;
935 /* Do not make more accurate class from reloads generated. They are
936 mostly moves with a lot of constraints. Making more accurate
937 class may results in very narrow class and impossibility of find
938 registers for several reloads of one insn. */
939 if (INSN_UID (curr_insn) >= new_insn_uid_start)
940 return;
941 if (GET_CODE (reg) == SUBREG)
942 reg = SUBREG_REG (reg);
943 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
944 return;
945 if (in_class_p (reg, cl, &rclass) && rclass != cl)
946 change_class (REGNO (reg), rclass, " Change", true);
949 /* Generate reloads for matching OUT and INS (array of input operand
950 numbers with end marker -1) with reg class GOAL_CLASS. Add input
951 and output reloads correspondingly to the lists *BEFORE and
952 *AFTER. */
953 static void
954 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
955 rtx *before, rtx *after)
957 int i, in;
958 rtx new_in_reg, new_out_reg, reg;
959 enum machine_mode inmode, outmode;
960 rtx in_rtx = *curr_id->operand_loc[ins[0]];
961 rtx out_rtx = *curr_id->operand_loc[out];
963 outmode = curr_operand_mode[out];
964 inmode = curr_operand_mode[ins[0]];
965 push_to_sequence (*before);
966 if (inmode != outmode)
968 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
970 reg = new_in_reg
971 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
972 goal_class, "");
973 if (SCALAR_INT_MODE_P (inmode))
974 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
975 else
976 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
978 else
980 reg = new_out_reg
981 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
982 goal_class, "");
983 if (SCALAR_INT_MODE_P (outmode))
984 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
985 else
986 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
987 /* NEW_IN_REG is non-paradoxical subreg. We don't want
988 NEW_OUT_REG living above. We add clobber clause for
989 this. */
990 emit_clobber (new_out_reg);
993 else
995 /* Pseudos have values -- see comments for lra_reg_info.
996 Different pseudos with the same value do not conflict even if
997 they live in the same place. When we create a pseudo we
998 assign value of original pseudo (if any) from which we
999 created the new pseudo. If we create the pseudo from the
1000 input pseudo, the new pseudo will no conflict with the input
1001 pseudo which is wrong when the input pseudo lives after the
1002 insn and as the new pseudo value is changed by the insn
1003 output. Therefore we create the new pseudo from the output.
1005 We cannot reuse the current output register because we might
1006 have a situation like "a <- a op b", where the constraints
1007 force the second input operand ("b") to match the output
1008 operand ("a"). "b" must then be copied into a new register
1009 so that it doesn't clobber the current value of "a". */
1011 new_in_reg = new_out_reg
1012 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
1013 goal_class, "");
1015 /* In and out operand can be got from transformations before
1016 processing insn constraints. One example of such transformations
1017 is subreg reloading (see function simplify_operand_subreg). The
1018 new pseudos created by the transformations might have inaccurate
1019 class (ALL_REGS) and we should make their classes more
1020 accurate. */
1021 narrow_reload_pseudo_class (in_rtx, goal_class);
1022 narrow_reload_pseudo_class (out_rtx, goal_class);
1023 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
1024 *before = get_insns ();
1025 end_sequence ();
1026 for (i = 0; (in = ins[i]) >= 0; i++)
1028 lra_assert
1029 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
1030 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
1031 *curr_id->operand_loc[in] = new_in_reg;
1033 lra_update_dups (curr_id, ins);
1034 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
1036 start_sequence ();
1037 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
1038 emit_insn (*after);
1039 *after = get_insns ();
1040 end_sequence ();
1042 *curr_id->operand_loc[out] = new_out_reg;
1043 lra_update_dup (curr_id, out);
1046 /* Return register class which is union of all reg classes in insn
1047 constraint alternative string starting with P. */
1048 static enum reg_class
1049 reg_class_from_constraints (const char *p)
1051 int c, len;
1052 enum reg_class op_class = NO_REGS;
1055 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1057 case '#':
1058 case ',':
1059 return op_class;
1061 case 'p':
1062 op_class = (reg_class_subunion
1063 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1064 ADDRESS, SCRATCH)]);
1065 break;
1067 case 'g':
1068 case 'r':
1069 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1070 break;
1072 default:
1073 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
1075 #ifdef EXTRA_CONSTRAINT_STR
1076 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1077 op_class
1078 = (reg_class_subunion
1079 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1080 ADDRESS, SCRATCH)]);
1081 #endif
1082 break;
1085 op_class
1086 = reg_class_subunion[op_class][REG_CLASS_FROM_CONSTRAINT (c, p)];
1087 break;
1089 while ((p += len), c);
1090 return op_class;
1093 /* If OP is a register, return the class of the register as per
1094 get_reg_class, otherwise return NO_REGS. */
1095 static inline enum reg_class
1096 get_op_class (rtx op)
1098 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1101 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1102 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1103 SUBREG for VAL to make them equal. */
1104 static rtx
1105 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1107 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1108 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1109 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1111 return (to_p
1112 ? gen_move_insn (mem_pseudo, val)
1113 : gen_move_insn (val, mem_pseudo));
1116 /* Process a special case insn (register move), return true if we
1117 don't need to process it anymore. Return that RTL was changed
1118 through CHANGE_P and macro SECONDARY_MEMORY_NEEDED says to use
1119 secondary memory through SEC_MEM_P. */
1120 static bool
1121 check_and_process_move (bool *change_p, bool *sec_mem_p)
1123 int sregno, dregno;
1124 rtx set, dest, src, dreg, sreg, old_sreg, new_reg, before, scratch_reg;
1125 enum reg_class dclass, sclass, secondary_class;
1126 enum machine_mode sreg_mode;
1127 secondary_reload_info sri;
1129 *sec_mem_p = *change_p = false;
1130 if ((set = single_set (curr_insn)) == NULL)
1131 return false;
1132 dreg = dest = SET_DEST (set);
1133 sreg = src = SET_SRC (set);
1134 /* Quick check on the right move insn which does not need
1135 reloads. */
1136 if ((dclass = get_op_class (dest)) != NO_REGS
1137 && (sclass = get_op_class (src)) != NO_REGS
1138 /* The backend guarantees that register moves of cost 2 never
1139 need reloads. */
1140 && targetm.register_move_cost (GET_MODE (src), dclass, sclass) == 2)
1141 return true;
1142 if (GET_CODE (dest) == SUBREG)
1143 dreg = SUBREG_REG (dest);
1144 if (GET_CODE (src) == SUBREG)
1145 sreg = SUBREG_REG (src);
1146 if (! REG_P (dreg) || ! REG_P (sreg))
1147 return false;
1148 sclass = dclass = NO_REGS;
1149 dreg = get_equiv_substitution (dreg);
1150 if (REG_P (dreg))
1151 dclass = get_reg_class (REGNO (dreg));
1152 if (dclass == ALL_REGS)
1153 /* ALL_REGS is used for new pseudos created by transformations
1154 like reload of SUBREG_REG (see function
1155 simplify_operand_subreg). We don't know their class yet. We
1156 should figure out the class from processing the insn
1157 constraints not in this fast path function. Even if ALL_REGS
1158 were a right class for the pseudo, secondary_... hooks usually
1159 are not define for ALL_REGS. */
1160 return false;
1161 sreg_mode = GET_MODE (sreg);
1162 old_sreg = sreg;
1163 sreg = get_equiv_substitution (sreg);
1164 if (REG_P (sreg))
1165 sclass = get_reg_class (REGNO (sreg));
1166 if (sclass == ALL_REGS)
1167 /* See comments above. */
1168 return false;
1169 #ifdef SECONDARY_MEMORY_NEEDED
1170 if (dclass != NO_REGS && sclass != NO_REGS
1171 && SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src)))
1173 *sec_mem_p = true;
1174 return false;
1176 #endif
1177 sri.prev_sri = NULL;
1178 sri.icode = CODE_FOR_nothing;
1179 sri.extra_cost = 0;
1180 secondary_class = NO_REGS;
1181 /* Set up hard register for a reload pseudo for hook
1182 secondary_reload because some targets just ignore unassigned
1183 pseudos in the hook. */
1184 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1186 dregno = REGNO (dreg);
1187 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1189 else
1190 dregno = -1;
1191 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1193 sregno = REGNO (sreg);
1194 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1196 else
1197 sregno = -1;
1198 if (sclass != NO_REGS)
1199 secondary_class
1200 = (enum reg_class) targetm.secondary_reload (false, dest,
1201 (reg_class_t) sclass,
1202 GET_MODE (src), &sri);
1203 if (sclass == NO_REGS
1204 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1205 && dclass != NO_REGS))
1207 #if ENABLE_ASSERT_CHECKING
1208 enum reg_class old_sclass = secondary_class;
1209 secondary_reload_info old_sri = sri;
1210 #endif
1212 sri.prev_sri = NULL;
1213 sri.icode = CODE_FOR_nothing;
1214 sri.extra_cost = 0;
1215 secondary_class
1216 = (enum reg_class) targetm.secondary_reload (true, sreg,
1217 (reg_class_t) dclass,
1218 sreg_mode, &sri);
1219 /* Check the target hook consistency. */
1220 lra_assert
1221 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1222 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1223 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1225 if (sregno >= 0)
1226 reg_renumber [sregno] = -1;
1227 if (dregno >= 0)
1228 reg_renumber [dregno] = -1;
1229 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1230 return false;
1231 *change_p = true;
1232 new_reg = NULL_RTX;
1233 if (secondary_class != NO_REGS)
1234 new_reg = lra_create_new_reg_with_unique_value (sreg_mode, NULL_RTX,
1235 secondary_class,
1236 "secondary");
1237 start_sequence ();
1238 if (old_sreg != sreg)
1239 sreg = copy_rtx (sreg);
1240 if (sri.icode == CODE_FOR_nothing)
1241 lra_emit_move (new_reg, sreg);
1242 else
1244 enum reg_class scratch_class;
1246 scratch_class = (reg_class_from_constraints
1247 (insn_data[sri.icode].operand[2].constraint));
1248 scratch_reg = (lra_create_new_reg_with_unique_value
1249 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1250 scratch_class, "scratch"));
1251 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1252 sreg, scratch_reg));
1254 before = get_insns ();
1255 end_sequence ();
1256 lra_process_new_insns (curr_insn, before, NULL_RTX, "Inserting the move");
1257 if (new_reg != NULL_RTX)
1259 if (GET_CODE (src) == SUBREG)
1260 SUBREG_REG (src) = new_reg;
1261 else
1262 SET_SRC (set) = new_reg;
1264 else
1266 if (lra_dump_file != NULL)
1268 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1269 debug_rtl_slim (lra_dump_file, curr_insn, curr_insn, -1, 0);
1271 lra_set_insn_deleted (curr_insn);
1272 return true;
1274 return false;
1277 /* The following data describe the result of process_alt_operands.
1278 The data are used in curr_insn_transform to generate reloads. */
1280 /* The chosen reg classes which should be used for the corresponding
1281 operands. */
1282 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1283 /* True if the operand should be the same as another operand and that
1284 other operand does not need a reload. */
1285 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1286 /* True if the operand does not need a reload. */
1287 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1288 /* True if the operand can be offsetable memory. */
1289 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1290 /* The number of an operand to which given operand can be matched to. */
1291 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1292 /* The number of elements in the following array. */
1293 static int goal_alt_dont_inherit_ops_num;
1294 /* Numbers of operands whose reload pseudos should not be inherited. */
1295 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1296 /* True if the insn commutative operands should be swapped. */
1297 static bool goal_alt_swapped;
1298 /* The chosen insn alternative. */
1299 static int goal_alt_number;
1301 /* The following five variables are used to choose the best insn
1302 alternative. They reflect final characteristics of the best
1303 alternative. */
1305 /* Number of necessary reloads and overall cost reflecting the
1306 previous value and other unpleasantness of the best alternative. */
1307 static int best_losers, best_overall;
1308 /* Number of small register classes used for operands of the best
1309 alternative. */
1310 static int best_small_class_operands_num;
1311 /* Overall number hard registers used for reloads. For example, on
1312 some targets we need 2 general registers to reload DFmode and only
1313 one floating point register. */
1314 static int best_reload_nregs;
1315 /* Overall number reflecting distances of previous reloading the same
1316 value. The distances are counted from the current BB start. It is
1317 used to improve inheritance chances. */
1318 static int best_reload_sum;
1320 /* True if the current insn should have no correspondingly input or
1321 output reloads. */
1322 static bool no_input_reloads_p, no_output_reloads_p;
1324 /* True if we swapped the commutative operands in the current
1325 insn. */
1326 static int curr_swapped;
1328 /* Arrange for address element *LOC to be a register of class CL.
1329 Add any input reloads to list BEFORE. AFTER is nonnull if *LOC is an
1330 automodified value; handle that case by adding the required output
1331 reloads to list AFTER. Return true if the RTL was changed. */
1332 static bool
1333 process_addr_reg (rtx *loc, rtx *before, rtx *after, enum reg_class cl)
1335 int regno;
1336 enum reg_class rclass, new_class;
1337 rtx reg = *loc;
1338 rtx new_reg;
1339 enum machine_mode mode;
1340 bool before_p = false;
1342 mode = GET_MODE (reg);
1343 if (! REG_P (reg))
1345 /* Always reload memory in an address even if the target supports
1346 such addresses. */
1347 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1348 before_p = true;
1350 else
1352 regno = REGNO (reg);
1353 rclass = get_reg_class (regno);
1354 if ((*loc = get_equiv_substitution (reg)) != reg)
1356 if (lra_dump_file != NULL)
1358 fprintf (lra_dump_file,
1359 "Changing pseudo %d in address of insn %u on equiv ",
1360 REGNO (reg), INSN_UID (curr_insn));
1361 print_value_slim (lra_dump_file, *loc, 1);
1362 fprintf (lra_dump_file, "\n");
1364 *loc = copy_rtx (*loc);
1366 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1368 reg = *loc;
1369 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1370 mode, reg, cl, "address", &new_reg))
1371 before_p = true;
1373 else if (new_class != NO_REGS && rclass != new_class)
1375 change_class (regno, new_class, " Change", true);
1376 return false;
1378 else
1379 return false;
1381 if (before_p)
1383 push_to_sequence (*before);
1384 lra_emit_move (new_reg, reg);
1385 *before = get_insns ();
1386 end_sequence ();
1388 *loc = new_reg;
1389 if (after != NULL)
1391 start_sequence ();
1392 lra_emit_move (reg, new_reg);
1393 emit_insn (*after);
1394 *after = get_insns ();
1395 end_sequence ();
1397 return true;
1400 #ifndef SLOW_UNALIGNED_ACCESS
1401 #define SLOW_UNALIGNED_ACCESS(mode, align) 0
1402 #endif
1404 /* Make reloads for subreg in operand NOP with internal subreg mode
1405 REG_MODE, add new reloads for further processing. Return true if
1406 any reload was generated. */
1407 static bool
1408 simplify_operand_subreg (int nop, enum machine_mode reg_mode)
1410 int hard_regno;
1411 rtx before, after;
1412 enum machine_mode mode;
1413 rtx reg, new_reg;
1414 rtx operand = *curr_id->operand_loc[nop];
1416 before = after = NULL_RTX;
1418 if (GET_CODE (operand) != SUBREG)
1419 return false;
1421 mode = GET_MODE (operand);
1422 reg = SUBREG_REG (operand);
1423 /* If we change address for paradoxical subreg of memory, the
1424 address might violate the necessary alignment or the access might
1425 be slow. So take this into consideration. */
1426 if ((MEM_P (reg)
1427 && ((! STRICT_ALIGNMENT
1428 && ! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg)))
1429 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1430 || (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER))
1432 alter_subreg (curr_id->operand_loc[nop], false);
1433 return true;
1435 /* Put constant into memory when we have mixed modes. It generates
1436 a better code in most cases as it does not need a secondary
1437 reload memory. It also prevents LRA looping when LRA is using
1438 secondary reload memory again and again. */
1439 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1440 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1442 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1443 alter_subreg (curr_id->operand_loc[nop], false);
1444 return true;
1446 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1447 if there may be a problem accessing OPERAND in the outer
1448 mode. */
1449 if ((REG_P (reg)
1450 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1451 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1452 /* Don't reload paradoxical subregs because we could be looping
1453 having repeatedly final regno out of hard regs range. */
1454 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1455 >= hard_regno_nregs[hard_regno][mode])
1456 && simplify_subreg_regno (hard_regno, GET_MODE (reg),
1457 SUBREG_BYTE (operand), mode) < 0)
1458 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1460 enum op_type type = curr_static_id->operand[nop].type;
1461 /* The class will be defined later in curr_insn_transform. */
1462 enum reg_class rclass
1463 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1465 new_reg = lra_create_new_reg_with_unique_value (reg_mode, reg, rclass,
1466 "subreg reg");
1467 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (new_reg));
1468 if (type != OP_OUT
1469 || GET_MODE_SIZE (GET_MODE (reg)) > GET_MODE_SIZE (mode))
1471 push_to_sequence (before);
1472 lra_emit_move (new_reg, reg);
1473 before = get_insns ();
1474 end_sequence ();
1476 if (type != OP_IN)
1478 start_sequence ();
1479 lra_emit_move (reg, new_reg);
1480 emit_insn (after);
1481 after = get_insns ();
1482 end_sequence ();
1484 SUBREG_REG (operand) = new_reg;
1485 lra_process_new_insns (curr_insn, before, after,
1486 "Inserting subreg reload");
1487 return true;
1489 return false;
1492 /* Return TRUE if X refers for a hard register from SET. */
1493 static bool
1494 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1496 int i, j, x_hard_regno;
1497 enum machine_mode mode;
1498 const char *fmt;
1499 enum rtx_code code;
1501 if (x == NULL_RTX)
1502 return false;
1503 code = GET_CODE (x);
1504 mode = GET_MODE (x);
1505 if (code == SUBREG)
1507 x = SUBREG_REG (x);
1508 code = GET_CODE (x);
1509 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1510 mode = GET_MODE (x);
1513 if (REG_P (x))
1515 x_hard_regno = get_hard_regno (x);
1516 return (x_hard_regno >= 0
1517 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1519 if (MEM_P (x))
1521 struct address ad;
1522 enum machine_mode mode = GET_MODE (x);
1523 rtx *addr_loc = &XEXP (x, 0);
1525 extract_address_regs (mode, MEM_ADDR_SPACE (x), addr_loc, MEM, &ad);
1526 if (ad.base_reg_loc != NULL)
1528 if (uses_hard_regs_p (*ad.base_reg_loc, set))
1529 return true;
1531 if (ad.index_reg_loc != NULL)
1533 if (uses_hard_regs_p (*ad.index_reg_loc, set))
1534 return true;
1537 fmt = GET_RTX_FORMAT (code);
1538 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1540 if (fmt[i] == 'e')
1542 if (uses_hard_regs_p (XEXP (x, i), set))
1543 return true;
1545 else if (fmt[i] == 'E')
1547 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1548 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1549 return true;
1552 return false;
1555 /* Return true if OP is a spilled pseudo. */
1556 static inline bool
1557 spilled_pseudo_p (rtx op)
1559 return (REG_P (op)
1560 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1563 /* Return true if X is a general constant. */
1564 static inline bool
1565 general_constant_p (rtx x)
1567 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1570 /* Cost factor for each additional reload and maximal cost bound for
1571 insn reloads. One might ask about such strange numbers. Their
1572 values occurred historically from former reload pass. */
1573 #define LOSER_COST_FACTOR 6
1574 #define MAX_OVERALL_COST_BOUND 600
1576 /* Major function to choose the current insn alternative and what
1577 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1578 negative we should consider only this alternative. Return false if
1579 we can not choose the alternative or find how to reload the
1580 operands. */
1581 static bool
1582 process_alt_operands (int only_alternative)
1584 bool ok_p = false;
1585 int nop, small_class_operands_num, overall, nalt;
1586 int n_alternatives = curr_static_id->n_alternatives;
1587 int n_operands = curr_static_id->n_operands;
1588 /* LOSERS counts the operands that don't fit this alternative and
1589 would require loading. */
1590 int losers;
1591 /* REJECT is a count of how undesirable this alternative says it is
1592 if any reloading is required. If the alternative matches exactly
1593 then REJECT is ignored, but otherwise it gets this much counted
1594 against it in addition to the reloading needed. */
1595 int reject;
1596 /* The number of elements in the following array. */
1597 int early_clobbered_regs_num;
1598 /* Numbers of operands which are early clobber registers. */
1599 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1600 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1601 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1602 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1603 bool curr_alt_win[MAX_RECOG_OPERANDS];
1604 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1605 int curr_alt_matches[MAX_RECOG_OPERANDS];
1606 /* The number of elements in the following array. */
1607 int curr_alt_dont_inherit_ops_num;
1608 /* Numbers of operands whose reload pseudos should not be inherited. */
1609 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1610 rtx op;
1611 /* The register when the operand is a subreg of register, otherwise the
1612 operand itself. */
1613 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1614 /* The register if the operand is a register or subreg of register,
1615 otherwise NULL. */
1616 rtx operand_reg[MAX_RECOG_OPERANDS];
1617 int hard_regno[MAX_RECOG_OPERANDS];
1618 enum machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1619 int reload_nregs, reload_sum;
1620 bool costly_p;
1621 enum reg_class cl;
1623 /* Calculate some data common for all alternatives to speed up the
1624 function. */
1625 for (nop = 0; nop < n_operands; nop++)
1627 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1628 /* The real hard regno of the operand after the allocation. */
1629 hard_regno[nop] = get_hard_regno (op);
1631 operand_reg[nop] = op;
1632 biggest_mode[nop] = GET_MODE (operand_reg[nop]);
1633 if (GET_CODE (operand_reg[nop]) == SUBREG)
1635 operand_reg[nop] = SUBREG_REG (operand_reg[nop]);
1636 if (GET_MODE_SIZE (biggest_mode[nop])
1637 < GET_MODE_SIZE (GET_MODE (operand_reg[nop])))
1638 biggest_mode[nop] = GET_MODE (operand_reg[nop]);
1640 if (REG_P (operand_reg[nop]))
1641 no_subreg_reg_operand[nop] = operand_reg[nop];
1642 else
1643 operand_reg[nop] = NULL_RTX;
1646 /* The constraints are made of several alternatives. Each operand's
1647 constraint looks like foo,bar,... with commas separating the
1648 alternatives. The first alternatives for all operands go
1649 together, the second alternatives go together, etc.
1651 First loop over alternatives. */
1652 for (nalt = 0; nalt < n_alternatives; nalt++)
1654 /* Loop over operands for one constraint alternative. */
1655 #ifdef HAVE_ATTR_enabled
1656 if (curr_id->alternative_enabled_p != NULL
1657 && ! curr_id->alternative_enabled_p[nalt])
1658 continue;
1659 #endif
1661 if (only_alternative >= 0 && nalt != only_alternative)
1662 continue;
1664 overall = losers = reject = reload_nregs = reload_sum = 0;
1665 for (nop = 0; nop < n_operands; nop++)
1666 reject += (curr_static_id
1667 ->operand_alternative[nalt * n_operands + nop].reject);
1668 early_clobbered_regs_num = 0;
1670 for (nop = 0; nop < n_operands; nop++)
1672 const char *p;
1673 char *end;
1674 int len, c, m, i, opalt_num, this_alternative_matches;
1675 bool win, did_match, offmemok, early_clobber_p;
1676 /* false => this operand can be reloaded somehow for this
1677 alternative. */
1678 bool badop;
1679 /* true => this operand can be reloaded if the alternative
1680 allows regs. */
1681 bool winreg;
1682 /* True if a constant forced into memory would be OK for
1683 this operand. */
1684 bool constmemok;
1685 enum reg_class this_alternative, this_costly_alternative;
1686 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1687 bool this_alternative_match_win, this_alternative_win;
1688 bool this_alternative_offmemok;
1689 enum machine_mode mode;
1691 opalt_num = nalt * n_operands + nop;
1692 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1694 /* Fast track for no constraints at all. */
1695 curr_alt[nop] = NO_REGS;
1696 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1697 curr_alt_win[nop] = true;
1698 curr_alt_match_win[nop] = false;
1699 curr_alt_offmemok[nop] = false;
1700 curr_alt_matches[nop] = -1;
1701 continue;
1704 op = no_subreg_reg_operand[nop];
1705 mode = curr_operand_mode[nop];
1707 win = did_match = winreg = offmemok = constmemok = false;
1708 badop = true;
1710 early_clobber_p = false;
1711 p = curr_static_id->operand_alternative[opalt_num].constraint;
1713 this_costly_alternative = this_alternative = NO_REGS;
1714 /* We update set of possible hard regs besides its class
1715 because reg class might be inaccurate. For example,
1716 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1717 is translated in HI_REGS because classes are merged by
1718 pairs and there is no accurate intermediate class. */
1719 CLEAR_HARD_REG_SET (this_alternative_set);
1720 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1721 this_alternative_win = false;
1722 this_alternative_match_win = false;
1723 this_alternative_offmemok = false;
1724 this_alternative_matches = -1;
1726 /* An empty constraint should be excluded by the fast
1727 track. */
1728 lra_assert (*p != 0 && *p != ',');
1730 /* Scan this alternative's specs for this operand; set WIN
1731 if the operand fits any letter in this alternative.
1732 Otherwise, clear BADOP if this operand could fit some
1733 letter after reloads, or set WINREG if this operand could
1734 fit after reloads provided the constraint allows some
1735 registers. */
1736 costly_p = false;
1739 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1741 case '\0':
1742 len = 0;
1743 break;
1744 case ',':
1745 c = '\0';
1746 break;
1748 case '=': case '+': case '?': case '*': case '!':
1749 case ' ': case '\t':
1750 break;
1752 case '%':
1753 /* We only support one commutative marker, the first
1754 one. We already set commutative above. */
1755 break;
1757 case '&':
1758 early_clobber_p = true;
1759 break;
1761 case '#':
1762 /* Ignore rest of this alternative. */
1763 c = '\0';
1764 break;
1766 case '0': case '1': case '2': case '3': case '4':
1767 case '5': case '6': case '7': case '8': case '9':
1769 int m_hregno;
1770 bool match_p;
1772 m = strtoul (p, &end, 10);
1773 p = end;
1774 len = 0;
1775 lra_assert (nop > m);
1777 this_alternative_matches = m;
1778 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1779 /* We are supposed to match a previous operand.
1780 If we do, we win if that one did. If we do
1781 not, count both of the operands as losers.
1782 (This is too conservative, since most of the
1783 time only a single reload insn will be needed
1784 to make the two operands win. As a result,
1785 this alternative may be rejected when it is
1786 actually desirable.) */
1787 match_p = false;
1788 if (operands_match_p (*curr_id->operand_loc[nop],
1789 *curr_id->operand_loc[m], m_hregno))
1791 /* We should reject matching of an early
1792 clobber operand if the matching operand is
1793 not dying in the insn. */
1794 if (! curr_static_id->operand[m].early_clobber
1795 || operand_reg[nop] == NULL_RTX
1796 || (find_regno_note (curr_insn, REG_DEAD,
1797 REGNO (operand_reg[nop]))
1798 != NULL_RTX))
1799 match_p = true;
1801 if (match_p)
1803 /* If we are matching a non-offsettable
1804 address where an offsettable address was
1805 expected, then we must reject this
1806 combination, because we can't reload
1807 it. */
1808 if (curr_alt_offmemok[m]
1809 && MEM_P (*curr_id->operand_loc[m])
1810 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1811 continue;
1814 else
1816 /* Operands don't match. Both operands must
1817 allow a reload register, otherwise we
1818 cannot make them match. */
1819 if (curr_alt[m] == NO_REGS)
1820 break;
1821 /* Retroactively mark the operand we had to
1822 match as a loser, if it wasn't already and
1823 it wasn't matched to a register constraint
1824 (e.g it might be matched by memory). */
1825 if (curr_alt_win[m]
1826 && (operand_reg[m] == NULL_RTX
1827 || hard_regno[m] < 0))
1829 losers++;
1830 reload_nregs
1831 += (ira_reg_class_max_nregs[curr_alt[m]]
1832 [GET_MODE (*curr_id->operand_loc[m])]);
1835 /* We prefer no matching alternatives because
1836 it gives more freedom in RA. */
1837 if (operand_reg[nop] == NULL_RTX
1838 || (find_regno_note (curr_insn, REG_DEAD,
1839 REGNO (operand_reg[nop]))
1840 == NULL_RTX))
1841 reject += 2;
1843 /* If we have to reload this operand and some
1844 previous operand also had to match the same
1845 thing as this operand, we don't know how to do
1846 that. */
1847 if (!match_p || !curr_alt_win[m])
1849 for (i = 0; i < nop; i++)
1850 if (curr_alt_matches[i] == m)
1851 break;
1852 if (i < nop)
1853 break;
1855 else
1856 did_match = true;
1858 /* This can be fixed with reloads if the operand
1859 we are supposed to match can be fixed with
1860 reloads. */
1861 badop = false;
1862 this_alternative = curr_alt[m];
1863 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1864 break;
1867 case 'p':
1868 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1869 ADDRESS, SCRATCH);
1870 this_alternative = reg_class_subunion[this_alternative][cl];
1871 IOR_HARD_REG_SET (this_alternative_set,
1872 reg_class_contents[cl]);
1873 if (costly_p)
1875 this_costly_alternative
1876 = reg_class_subunion[this_costly_alternative][cl];
1877 IOR_HARD_REG_SET (this_costly_alternative_set,
1878 reg_class_contents[cl]);
1880 win = true;
1881 badop = false;
1882 break;
1884 case TARGET_MEM_CONSTRAINT:
1885 if (MEM_P (op) || spilled_pseudo_p (op))
1886 win = true;
1887 if (CONST_POOL_OK_P (mode, op))
1888 badop = false;
1889 constmemok = true;
1890 break;
1892 case '<':
1893 if (MEM_P (op)
1894 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1895 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1896 win = true;
1897 break;
1899 case '>':
1900 if (MEM_P (op)
1901 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1902 || GET_CODE (XEXP (op, 0)) == POST_INC))
1903 win = true;
1904 break;
1906 /* Memory op whose address is not offsettable. */
1907 case 'V':
1908 if (MEM_P (op)
1909 && ! offsettable_nonstrict_memref_p (op))
1910 win = true;
1911 break;
1913 /* Memory operand whose address is offsettable. */
1914 case 'o':
1915 if ((MEM_P (op)
1916 && offsettable_nonstrict_memref_p (op))
1917 || spilled_pseudo_p (op))
1918 win = true;
1919 if (CONST_POOL_OK_P (mode, op) || MEM_P (op))
1920 badop = false;
1921 constmemok = true;
1922 offmemok = true;
1923 break;
1925 case 'E':
1926 case 'F':
1927 if (GET_CODE (op) == CONST_DOUBLE
1928 || (GET_CODE (op) == CONST_VECTOR
1929 && (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)))
1930 win = true;
1931 break;
1933 case 'G':
1934 case 'H':
1935 if (GET_CODE (op) == CONST_DOUBLE
1936 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1937 win = true;
1938 break;
1940 case 's':
1941 if (CONST_INT_P (op)
1942 || (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode))
1943 break;
1944 case 'i':
1945 if (general_constant_p (op))
1946 win = true;
1947 break;
1949 case 'n':
1950 if (CONST_INT_P (op)
1951 || (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode))
1952 win = true;
1953 break;
1955 case 'I':
1956 case 'J':
1957 case 'K':
1958 case 'L':
1959 case 'M':
1960 case 'N':
1961 case 'O':
1962 case 'P':
1963 if (CONST_INT_P (op)
1964 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1965 win = true;
1966 break;
1968 case 'X':
1969 /* This constraint should be excluded by the fast
1970 track. */
1971 gcc_unreachable ();
1972 break;
1974 case 'g':
1975 if (MEM_P (op)
1976 || general_constant_p (op)
1977 || spilled_pseudo_p (op))
1978 win = true;
1979 /* Drop through into 'r' case. */
1981 case 'r':
1982 this_alternative
1983 = reg_class_subunion[this_alternative][GENERAL_REGS];
1984 IOR_HARD_REG_SET (this_alternative_set,
1985 reg_class_contents[GENERAL_REGS]);
1986 if (costly_p)
1988 this_costly_alternative
1989 = (reg_class_subunion
1990 [this_costly_alternative][GENERAL_REGS]);
1991 IOR_HARD_REG_SET (this_costly_alternative_set,
1992 reg_class_contents[GENERAL_REGS]);
1994 goto reg;
1996 default:
1997 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
1999 #ifdef EXTRA_CONSTRAINT_STR
2000 if (EXTRA_MEMORY_CONSTRAINT (c, p))
2002 if (EXTRA_CONSTRAINT_STR (op, c, p))
2003 win = true;
2004 else if (spilled_pseudo_p (op))
2005 win = true;
2007 /* If we didn't already win, we can reload
2008 constants via force_const_mem, and other
2009 MEMs by reloading the address like for
2010 'o'. */
2011 if (CONST_POOL_OK_P (mode, op) || MEM_P (op))
2012 badop = false;
2013 constmemok = true;
2014 offmemok = true;
2015 break;
2017 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
2019 if (EXTRA_CONSTRAINT_STR (op, c, p))
2020 win = true;
2022 /* If we didn't already win, we can reload
2023 the address into a base register. */
2024 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2025 ADDRESS, SCRATCH);
2026 this_alternative
2027 = reg_class_subunion[this_alternative][cl];
2028 IOR_HARD_REG_SET (this_alternative_set,
2029 reg_class_contents[cl]);
2030 if (costly_p)
2032 this_costly_alternative
2033 = (reg_class_subunion
2034 [this_costly_alternative][cl]);
2035 IOR_HARD_REG_SET (this_costly_alternative_set,
2036 reg_class_contents[cl]);
2038 badop = false;
2039 break;
2042 if (EXTRA_CONSTRAINT_STR (op, c, p))
2043 win = true;
2044 #endif
2045 break;
2048 cl = REG_CLASS_FROM_CONSTRAINT (c, p);
2049 this_alternative = reg_class_subunion[this_alternative][cl];
2050 IOR_HARD_REG_SET (this_alternative_set,
2051 reg_class_contents[cl]);
2052 if (costly_p)
2054 this_costly_alternative
2055 = reg_class_subunion[this_costly_alternative][cl];
2056 IOR_HARD_REG_SET (this_costly_alternative_set,
2057 reg_class_contents[cl]);
2059 reg:
2060 if (mode == BLKmode)
2061 break;
2062 winreg = true;
2063 if (REG_P (op))
2065 if (hard_regno[nop] >= 0
2066 && in_hard_reg_set_p (this_alternative_set,
2067 mode, hard_regno[nop]))
2068 win = true;
2069 else if (hard_regno[nop] < 0
2070 && in_class_p (op, this_alternative, NULL))
2071 win = true;
2073 break;
2075 if (c != ' ' && c != '\t')
2076 costly_p = c == '*';
2078 while ((p += len), c);
2080 /* Record which operands fit this alternative. */
2081 if (win)
2083 this_alternative_win = true;
2084 if (operand_reg[nop] != NULL_RTX)
2086 if (hard_regno[nop] >= 0)
2088 if (in_hard_reg_set_p (this_costly_alternative_set,
2089 mode, hard_regno[nop]))
2090 reject++;
2092 else
2094 /* Prefer won reg to spilled pseudo under other equal
2095 conditions. */
2096 reject++;
2097 if (in_class_p (operand_reg[nop],
2098 this_costly_alternative, NULL))
2099 reject++;
2101 /* We simulate the behaviour of old reload here.
2102 Although scratches need hard registers and it
2103 might result in spilling other pseudos, no reload
2104 insns are generated for the scratches. So it
2105 might cost something but probably less than old
2106 reload pass believes. */
2107 if (lra_former_scratch_p (REGNO (operand_reg[nop])))
2108 reject += LOSER_COST_FACTOR;
2111 else if (did_match)
2112 this_alternative_match_win = true;
2113 else
2115 int const_to_mem = 0;
2116 bool no_regs_p;
2118 no_regs_p
2119 = (this_alternative == NO_REGS
2120 || (hard_reg_set_subset_p
2121 (reg_class_contents[this_alternative],
2122 lra_no_alloc_regs)));
2123 /* If this operand accepts a register, and if the
2124 register class has at least one allocatable register,
2125 then this operand can be reloaded. */
2126 if (winreg && !no_regs_p)
2127 badop = false;
2129 if (badop)
2130 goto fail;
2132 this_alternative_offmemok = offmemok;
2133 if (this_costly_alternative != NO_REGS)
2134 reject++;
2135 /* If the operand is dying, has a matching constraint,
2136 and satisfies constraints of the matched operand
2137 which failed to satisfy the own constraints, we do
2138 not need to generate a reload insn for this
2139 operand. */
2140 if (!(this_alternative_matches >= 0
2141 && !curr_alt_win[this_alternative_matches]
2142 && REG_P (op)
2143 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2144 && (hard_regno[nop] >= 0
2145 ? in_hard_reg_set_p (this_alternative_set,
2146 mode, hard_regno[nop])
2147 : in_class_p (op, this_alternative, NULL))))
2148 losers++;
2149 if (operand_reg[nop] != NULL_RTX
2150 /* Output operands and matched input operands are
2151 not inherited. The following conditions do not
2152 exactly describe the previous statement but they
2153 are pretty close. */
2154 && curr_static_id->operand[nop].type != OP_OUT
2155 && (this_alternative_matches < 0
2156 || curr_static_id->operand[nop].type != OP_IN))
2158 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2159 (operand_reg[nop])]
2160 .last_reload);
2162 if (last_reload > bb_reload_num)
2163 reload_sum += last_reload - bb_reload_num;
2165 /* If this is a constant that is reloaded into the
2166 desired class by copying it to memory first, count
2167 that as another reload. This is consistent with
2168 other code and is required to avoid choosing another
2169 alternative when the constant is moved into memory.
2170 Note that the test here is precisely the same as in
2171 the code below that calls force_const_mem. */
2172 if (CONST_POOL_OK_P (mode, op)
2173 && ((targetm.preferred_reload_class
2174 (op, this_alternative) == NO_REGS)
2175 || no_input_reloads_p))
2177 const_to_mem = 1;
2178 if (! no_regs_p)
2179 losers++;
2182 /* Alternative loses if it requires a type of reload not
2183 permitted for this insn. We can always reload
2184 objects with a REG_UNUSED note. */
2185 if ((curr_static_id->operand[nop].type != OP_IN
2186 && no_output_reloads_p
2187 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2188 || (curr_static_id->operand[nop].type != OP_OUT
2189 && no_input_reloads_p && ! const_to_mem))
2190 goto fail;
2192 /* If we can't reload this value at all, reject this
2193 alternative. Note that we could also lose due to
2194 LIMIT_RELOAD_CLASS, but we don't check that here. */
2195 if (! CONSTANT_P (op) && ! no_regs_p)
2197 if (targetm.preferred_reload_class
2198 (op, this_alternative) == NO_REGS)
2199 reject = MAX_OVERALL_COST_BOUND;
2201 if (curr_static_id->operand[nop].type == OP_OUT
2202 && (targetm.preferred_output_reload_class
2203 (op, this_alternative) == NO_REGS))
2204 reject = MAX_OVERALL_COST_BOUND;
2207 if (! ((const_to_mem && constmemok)
2208 || (MEM_P (op) && offmemok)))
2210 /* We prefer to reload pseudos over reloading other
2211 things, since such reloads may be able to be
2212 eliminated later. So bump REJECT in other cases.
2213 Don't do this in the case where we are forcing a
2214 constant into memory and it will then win since
2215 we don't want to have a different alternative
2216 match then. */
2217 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2218 reject += 2;
2220 if (! no_regs_p)
2221 reload_nregs
2222 += ira_reg_class_max_nregs[this_alternative][mode];
2225 /* Input reloads can be inherited more often than output
2226 reloads can be removed, so penalize output
2227 reloads. */
2228 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2229 reject++;
2232 if (early_clobber_p)
2233 reject++;
2234 /* ??? We check early clobbers after processing all operands
2235 (see loop below) and there we update the costs more.
2236 Should we update the cost (may be approximately) here
2237 because of early clobber register reloads or it is a rare
2238 or non-important thing to be worth to do it. */
2239 overall = losers * LOSER_COST_FACTOR + reject;
2240 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2241 goto fail;
2243 curr_alt[nop] = this_alternative;
2244 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2245 curr_alt_win[nop] = this_alternative_win;
2246 curr_alt_match_win[nop] = this_alternative_match_win;
2247 curr_alt_offmemok[nop] = this_alternative_offmemok;
2248 curr_alt_matches[nop] = this_alternative_matches;
2250 if (this_alternative_matches >= 0
2251 && !did_match && !this_alternative_win)
2252 curr_alt_win[this_alternative_matches] = false;
2254 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2255 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2257 ok_p = true;
2258 curr_alt_dont_inherit_ops_num = 0;
2259 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2261 int i, j, clobbered_hard_regno;
2262 HARD_REG_SET temp_set;
2264 i = early_clobbered_nops[nop];
2265 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2266 || hard_regno[i] < 0)
2267 continue;
2268 clobbered_hard_regno = hard_regno[i];
2269 CLEAR_HARD_REG_SET (temp_set);
2270 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2271 for (j = 0; j < n_operands; j++)
2272 if (j == i
2273 /* We don't want process insides of match_operator and
2274 match_parallel because otherwise we would process
2275 their operands once again generating a wrong
2276 code. */
2277 || curr_static_id->operand[j].is_operator)
2278 continue;
2279 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2280 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2281 continue;
2282 else if (uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2283 break;
2284 if (j >= n_operands)
2285 continue;
2286 /* We need to reload early clobbered register. */
2287 for (j = 0; j < n_operands; j++)
2288 if (curr_alt_matches[j] == i)
2290 curr_alt_match_win[j] = false;
2291 losers++;
2292 overall += LOSER_COST_FACTOR;
2294 if (! curr_alt_match_win[i])
2295 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2296 else
2298 /* Remember pseudos used for match reloads are never
2299 inherited. */
2300 lra_assert (curr_alt_matches[i] >= 0);
2301 curr_alt_win[curr_alt_matches[i]] = false;
2303 curr_alt_win[i] = curr_alt_match_win[i] = false;
2304 losers++;
2305 overall += LOSER_COST_FACTOR;
2307 small_class_operands_num = 0;
2308 for (nop = 0; nop < n_operands; nop++)
2309 small_class_operands_num
2310 += SMALL_REGISTER_CLASS_P (curr_alt[nop]) ? 1 : 0;
2312 /* If this alternative can be made to work by reloading, and it
2313 needs less reloading than the others checked so far, record
2314 it as the chosen goal for reloading. */
2315 if ((best_losers != 0 && losers == 0)
2316 || (((best_losers == 0 && losers == 0)
2317 || (best_losers != 0 && losers != 0))
2318 && (best_overall > overall
2319 || (best_overall == overall
2320 /* If the cost of the reloads is the same,
2321 prefer alternative which requires minimal
2322 number of small register classes for the
2323 operands. This improves chances of reloads
2324 for insn requiring small register
2325 classes. */
2326 && (small_class_operands_num
2327 < best_small_class_operands_num
2328 || (small_class_operands_num
2329 == best_small_class_operands_num
2330 && (reload_nregs < best_reload_nregs
2331 || (reload_nregs == best_reload_nregs
2332 && best_reload_sum < reload_sum))))))))
2334 for (nop = 0; nop < n_operands; nop++)
2336 goal_alt_win[nop] = curr_alt_win[nop];
2337 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2338 goal_alt_matches[nop] = curr_alt_matches[nop];
2339 goal_alt[nop] = curr_alt[nop];
2340 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2342 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2343 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2344 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2345 goal_alt_swapped = curr_swapped;
2346 best_overall = overall;
2347 best_losers = losers;
2348 best_small_class_operands_num = small_class_operands_num;
2349 best_reload_nregs = reload_nregs;
2350 best_reload_sum = reload_sum;
2351 goal_alt_number = nalt;
2353 if (losers == 0)
2354 /* Everything is satisfied. Do not process alternatives
2355 anymore. */
2356 break;
2357 fail:
2360 return ok_p;
2363 /* Return 1 if ADDR is a valid memory address for mode MODE in address
2364 space AS, and check that each pseudo has the proper kind of hard
2365 reg. */
2366 static int
2367 valid_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2368 rtx addr, addr_space_t as)
2370 #ifdef GO_IF_LEGITIMATE_ADDRESS
2371 lra_assert (ADDR_SPACE_GENERIC_P (as));
2372 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
2373 return 0;
2375 win:
2376 return 1;
2377 #else
2378 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
2379 #endif
2382 /* Make reload base reg + disp from address AD in space AS of memory
2383 with MODE into a new pseudo. Return the new pseudo. */
2384 static rtx
2385 base_plus_disp_to_reg (enum machine_mode mode, addr_space_t as,
2386 struct address *ad)
2388 enum reg_class cl;
2389 rtx new_reg;
2391 lra_assert (ad->base_reg_loc != NULL && ad->disp_loc != NULL);
2392 cl = base_reg_class (mode, as, ad->base_outer_code, ad->index_code);
2393 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "base + disp");
2394 lra_emit_add (new_reg, *ad->base_reg_loc, *ad->disp_loc);
2395 return new_reg;
2398 /* Make substitution in address AD in space AS with location ADDR_LOC.
2399 Update AD and ADDR_LOC if it is necessary. Return true if a
2400 substitution was made. */
2401 static bool
2402 equiv_address_substitution (struct address *ad, rtx *addr_loc,
2403 enum machine_mode mode, addr_space_t as,
2404 enum rtx_code code)
2406 rtx base_reg, new_base_reg, index_reg, new_index_reg;
2407 HOST_WIDE_INT disp, scale;
2408 bool change_p;
2410 if (ad->base_reg_loc == NULL)
2411 base_reg = new_base_reg = NULL_RTX;
2412 else
2414 base_reg = *ad->base_reg_loc;
2415 new_base_reg = get_equiv_substitution (base_reg);
2417 if (ad->index_reg_loc == NULL)
2418 index_reg = new_index_reg = NULL_RTX;
2419 else
2421 index_reg = *ad->index_reg_loc;
2422 new_index_reg = get_equiv_substitution (index_reg);
2424 if (base_reg == new_base_reg && index_reg == new_index_reg)
2425 return false;
2426 disp = 0;
2427 change_p = false;
2428 if (lra_dump_file != NULL)
2430 fprintf (lra_dump_file, "Changing address in insn %d ",
2431 INSN_UID (curr_insn));
2432 print_value_slim (lra_dump_file, *addr_loc, 1);
2434 if (base_reg != new_base_reg)
2436 if (REG_P (new_base_reg))
2438 *ad->base_reg_loc = new_base_reg;
2439 change_p = true;
2441 else if (GET_CODE (new_base_reg) == PLUS
2442 && REG_P (XEXP (new_base_reg, 0))
2443 && CONST_INT_P (XEXP (new_base_reg, 1)))
2445 disp += INTVAL (XEXP (new_base_reg, 1));
2446 *ad->base_reg_loc = XEXP (new_base_reg, 0);
2447 change_p = true;
2449 if (ad->base_reg_loc2 != NULL)
2450 *ad->base_reg_loc2 = *ad->base_reg_loc;
2452 scale = 1;
2453 if (ad->index_loc != NULL && GET_CODE (*ad->index_loc) == MULT)
2455 lra_assert (CONST_INT_P (XEXP (*ad->index_loc, 1)));
2456 scale = INTVAL (XEXP (*ad->index_loc, 1));
2458 if (index_reg != new_index_reg)
2460 if (REG_P (new_index_reg))
2462 *ad->index_reg_loc = new_index_reg;
2463 change_p = true;
2465 else if (GET_CODE (new_index_reg) == PLUS
2466 && REG_P (XEXP (new_index_reg, 0))
2467 && CONST_INT_P (XEXP (new_index_reg, 1)))
2469 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2470 *ad->index_reg_loc = XEXP (new_index_reg, 0);
2471 change_p = true;
2474 if (disp != 0)
2476 if (ad->disp_loc != NULL)
2477 *ad->disp_loc = plus_constant (Pmode, *ad->disp_loc, disp);
2478 else
2480 *addr_loc = gen_rtx_PLUS (Pmode, *addr_loc, GEN_INT (disp));
2481 extract_address_regs (mode, as, addr_loc, code, ad);
2483 change_p = true;
2485 if (lra_dump_file != NULL)
2487 if (! change_p)
2488 fprintf (lra_dump_file, " -- no change\n");
2489 else
2491 fprintf (lra_dump_file, " on equiv ");
2492 print_value_slim (lra_dump_file, *addr_loc, 1);
2493 fprintf (lra_dump_file, "\n");
2496 return change_p;
2499 /* Major function to make reloads for address in operand NOP. Add to
2500 reloads to the list *BEFORE and *AFTER. We might need to add
2501 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2502 address. Return true for any RTL change. */
2503 static bool
2504 process_address (int nop, rtx *before, rtx *after)
2506 struct address ad;
2507 enum machine_mode mode;
2508 rtx new_reg, *addr_loc, saved_index_reg, saved_base_reg;
2509 bool ok_p;
2510 addr_space_t as;
2511 rtx op = *curr_id->operand_loc[nop];
2512 const char *constraint = curr_static_id->operand[nop].constraint;
2513 bool change_p;
2514 enum rtx_code code;
2516 if (constraint[0] == 'p'
2517 || EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint))
2519 mode = VOIDmode;
2520 addr_loc = curr_id->operand_loc[nop];
2521 as = ADDR_SPACE_GENERIC;
2522 code = ADDRESS;
2524 else if (MEM_P (op))
2526 mode = GET_MODE (op);
2527 addr_loc = &XEXP (op, 0);
2528 as = MEM_ADDR_SPACE (op);
2529 code = MEM;
2531 else if (GET_CODE (op) == SUBREG
2532 && MEM_P (SUBREG_REG (op)))
2534 mode = GET_MODE (SUBREG_REG (op));
2535 addr_loc = &XEXP (SUBREG_REG (op), 0);
2536 as = MEM_ADDR_SPACE (SUBREG_REG (op));
2537 code = MEM;
2539 else
2540 return false;
2541 if (GET_CODE (*addr_loc) == AND)
2542 addr_loc = &XEXP (*addr_loc, 0);
2543 extract_address_regs (mode, as, addr_loc, code, &ad);
2544 change_p = equiv_address_substitution (&ad, addr_loc, mode, as, code);
2545 if (ad.base_reg_loc != NULL
2546 && (process_addr_reg
2547 (ad.base_reg_loc, before,
2548 (ad.base_modify_p && REG_P (*ad.base_reg_loc)
2549 && find_regno_note (curr_insn, REG_DEAD,
2550 REGNO (*ad.base_reg_loc)) == NULL_RTX
2551 ? after : NULL),
2552 base_reg_class (mode, as, ad.base_outer_code, ad.index_code))))
2554 change_p = true;
2555 if (ad.base_reg_loc2 != NULL)
2556 *ad.base_reg_loc2 = *ad.base_reg_loc;
2558 if (ad.index_reg_loc != NULL
2559 && process_addr_reg (ad.index_reg_loc, before, NULL, INDEX_REG_CLASS))
2560 change_p = true;
2562 /* The address was valid before LRA. We only change its form if the
2563 address has a displacement, so if it has no displacement it must
2564 still be valid. */
2565 if (ad.disp_loc == NULL)
2566 return change_p;
2568 /* See whether the address is still valid. Some ports do not check
2569 displacements for eliminable registers, so we replace them
2570 temporarily with the elimination target. */
2571 saved_base_reg = saved_index_reg = NULL_RTX;
2572 if (ad.base_reg_loc != NULL)
2574 saved_base_reg = *ad.base_reg_loc;
2575 lra_eliminate_reg_if_possible (ad.base_reg_loc);
2576 if (ad.base_reg_loc2 != NULL)
2577 *ad.base_reg_loc2 = *ad.base_reg_loc;
2579 if (ad.index_reg_loc != NULL)
2581 saved_index_reg = *ad.index_reg_loc;
2582 lra_eliminate_reg_if_possible (ad.index_reg_loc);
2584 /* Some ports do not check displacements for virtual registers -- so
2585 we substitute them temporarily by real registers. */
2586 ok_p = valid_address_p (mode, *addr_loc, as);
2587 if (saved_base_reg != NULL_RTX)
2589 *ad.base_reg_loc = saved_base_reg;
2590 if (ad.base_reg_loc2 != NULL)
2591 *ad.base_reg_loc2 = saved_base_reg;
2593 if (saved_index_reg != NULL_RTX)
2594 *ad.index_reg_loc = saved_index_reg;
2596 if (ok_p)
2597 return change_p;
2599 /* Addresses were legitimate before LRA. So if the address has
2600 two registers than it can have two of them. We should also
2601 not worry about scale for the same reason. */
2602 push_to_sequence (*before);
2603 if (ad.base_reg_loc == NULL)
2605 if (ad.index_reg_loc == NULL)
2607 int code = -1;
2608 enum reg_class cl = base_reg_class (mode, as, SCRATCH, SCRATCH);
2610 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2611 #ifdef HAVE_lo_sum
2613 rtx insn;
2614 rtx last = get_last_insn ();
2616 /* disp => lo_sum (new_base, disp) */
2617 insn = emit_insn (gen_rtx_SET
2618 (VOIDmode, new_reg,
2619 gen_rtx_HIGH (Pmode, copy_rtx (*ad.disp_loc))));
2620 code = recog_memoized (insn);
2621 if (code >= 0)
2623 rtx save = *ad.disp_loc;
2625 *ad.disp_loc = gen_rtx_LO_SUM (Pmode, new_reg, *ad.disp_loc);
2626 if (! valid_address_p (mode, *ad.disp_loc, as))
2628 *ad.disp_loc = save;
2629 code = -1;
2632 if (code < 0)
2633 delete_insns_since (last);
2635 #endif
2636 if (code < 0)
2638 /* disp => new_base */
2639 lra_emit_move (new_reg, *ad.disp_loc);
2640 *ad.disp_loc = new_reg;
2643 else
2645 /* index * scale + disp => new base + index * scale */
2646 enum reg_class cl = base_reg_class (mode, as, SCRATCH, SCRATCH);
2648 lra_assert (INDEX_REG_CLASS != NO_REGS);
2649 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2650 lra_assert (GET_CODE (*addr_loc) == PLUS);
2651 lra_emit_move (new_reg, *ad.disp_loc);
2652 if (CONSTANT_P (XEXP (*addr_loc, 1)))
2653 XEXP (*addr_loc, 1) = XEXP (*addr_loc, 0);
2654 XEXP (*addr_loc, 0) = new_reg;
2657 else if (ad.index_reg_loc == NULL)
2659 /* base + disp => new base */
2660 /* Another option would be to reload the displacement into an
2661 index register. However, postreload has code to optimize
2662 address reloads that have the same base and different
2663 displacements, so reloading into an index register would
2664 not necessarily be a win. */
2665 new_reg = base_plus_disp_to_reg (mode, as, &ad);
2666 *addr_loc = new_reg;
2668 else
2670 /* base + scale * index + disp => new base + scale * index */
2671 new_reg = base_plus_disp_to_reg (mode, as, &ad);
2672 *addr_loc = gen_rtx_PLUS (Pmode, new_reg, *ad.index_loc);
2674 *before = get_insns ();
2675 end_sequence ();
2676 return true;
2679 /* Emit insns to reload VALUE into a new register. VALUE is an
2680 auto-increment or auto-decrement RTX whose operand is a register or
2681 memory location; so reloading involves incrementing that location.
2682 IN is either identical to VALUE, or some cheaper place to reload
2683 value being incremented/decremented from.
2685 INC_AMOUNT is the number to increment or decrement by (always
2686 positive and ignored for POST_MODIFY/PRE_MODIFY).
2688 Return pseudo containing the result. */
2689 static rtx
2690 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
2692 /* REG or MEM to be copied and incremented. */
2693 rtx incloc = XEXP (value, 0);
2694 /* Nonzero if increment after copying. */
2695 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
2696 || GET_CODE (value) == POST_MODIFY);
2697 rtx last;
2698 rtx inc;
2699 rtx add_insn;
2700 int code;
2701 rtx real_in = in == value ? incloc : in;
2702 rtx result;
2703 bool plus_p = true;
2705 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
2707 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
2708 || GET_CODE (XEXP (value, 1)) == MINUS);
2709 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
2710 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
2711 inc = XEXP (XEXP (value, 1), 1);
2713 else
2715 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
2716 inc_amount = -inc_amount;
2718 inc = GEN_INT (inc_amount);
2721 if (! post && REG_P (incloc))
2722 result = incloc;
2723 else
2724 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
2725 "INC/DEC result");
2727 if (real_in != result)
2729 /* First copy the location to the result register. */
2730 lra_assert (REG_P (result));
2731 emit_insn (gen_move_insn (result, real_in));
2734 /* We suppose that there are insns to add/sub with the constant
2735 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
2736 old reload worked with this assumption. If the assumption
2737 becomes wrong, we should use approach in function
2738 base_plus_disp_to_reg. */
2739 if (in == value)
2741 /* See if we can directly increment INCLOC. */
2742 last = get_last_insn ();
2743 add_insn = emit_insn (plus_p
2744 ? gen_add2_insn (incloc, inc)
2745 : gen_sub2_insn (incloc, inc));
2747 code = recog_memoized (add_insn);
2748 if (code >= 0)
2750 if (! post && result != incloc)
2751 emit_insn (gen_move_insn (result, incloc));
2752 return result;
2754 delete_insns_since (last);
2757 /* If couldn't do the increment directly, must increment in RESULT.
2758 The way we do this depends on whether this is pre- or
2759 post-increment. For pre-increment, copy INCLOC to the reload
2760 register, increment it there, then save back. */
2761 if (! post)
2763 if (real_in != result)
2764 emit_insn (gen_move_insn (result, real_in));
2765 if (plus_p)
2766 emit_insn (gen_add2_insn (result, inc));
2767 else
2768 emit_insn (gen_sub2_insn (result, inc));
2769 if (result != incloc)
2770 emit_insn (gen_move_insn (incloc, result));
2772 else
2774 /* Post-increment.
2776 Because this might be a jump insn or a compare, and because
2777 RESULT may not be available after the insn in an input
2778 reload, we must do the incrementing before the insn being
2779 reloaded for.
2781 We have already copied IN to RESULT. Increment the copy in
2782 RESULT, save that back, then decrement RESULT so it has
2783 the original value. */
2784 if (plus_p)
2785 emit_insn (gen_add2_insn (result, inc));
2786 else
2787 emit_insn (gen_sub2_insn (result, inc));
2788 emit_insn (gen_move_insn (incloc, result));
2789 /* Restore non-modified value for the result. We prefer this
2790 way because it does not require an additional hard
2791 register. */
2792 if (plus_p)
2794 if (CONST_INT_P (inc))
2795 emit_insn (gen_add2_insn (result, GEN_INT (-INTVAL (inc))));
2796 else
2797 emit_insn (gen_sub2_insn (result, inc));
2799 else
2800 emit_insn (gen_add2_insn (result, inc));
2802 return result;
2805 /* Swap operands NOP and NOP + 1. */
2806 static inline void
2807 swap_operands (int nop)
2809 enum machine_mode mode = curr_operand_mode[nop];
2810 curr_operand_mode[nop] = curr_operand_mode[nop + 1];
2811 curr_operand_mode[nop + 1] = mode;
2812 rtx x = *curr_id->operand_loc[nop];
2813 *curr_id->operand_loc[nop] = *curr_id->operand_loc[nop + 1];
2814 *curr_id->operand_loc[nop + 1] = x;
2815 /* Swap the duplicates too. */
2816 lra_update_dup (curr_id, nop);
2817 lra_update_dup (curr_id, nop + 1);
2820 /* Main entry point of the constraint code: search the body of the
2821 current insn to choose the best alternative. It is mimicking insn
2822 alternative cost calculation model of former reload pass. That is
2823 because machine descriptions were written to use this model. This
2824 model can be changed in future. Make commutative operand exchange
2825 if it is chosen.
2827 Return true if some RTL changes happened during function call. */
2828 static bool
2829 curr_insn_transform (void)
2831 int i, j, k;
2832 int n_operands;
2833 int n_alternatives;
2834 int commutative;
2835 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
2836 rtx before, after;
2837 bool alt_p = false;
2838 /* Flag that the insn has been changed through a transformation. */
2839 bool change_p;
2840 bool sec_mem_p;
2841 #ifdef SECONDARY_MEMORY_NEEDED
2842 bool use_sec_mem_p;
2843 #endif
2844 int max_regno_before;
2845 int reused_alternative_num;
2847 no_input_reloads_p = no_output_reloads_p = false;
2848 goal_alt_number = -1;
2850 if (check_and_process_move (&change_p, &sec_mem_p))
2851 return change_p;
2853 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
2854 reloads; neither are insns that SET cc0. Insns that use CC0 are
2855 not allowed to have any input reloads. */
2856 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
2857 no_output_reloads_p = true;
2859 #ifdef HAVE_cc0
2860 if (reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
2861 no_input_reloads_p = true;
2862 if (reg_set_p (cc0_rtx, PATTERN (curr_insn)))
2863 no_output_reloads_p = true;
2864 #endif
2866 n_operands = curr_static_id->n_operands;
2867 n_alternatives = curr_static_id->n_alternatives;
2869 /* Just return "no reloads" if insn has no operands with
2870 constraints. */
2871 if (n_operands == 0 || n_alternatives == 0)
2872 return false;
2874 max_regno_before = max_reg_num ();
2876 for (i = 0; i < n_operands; i++)
2878 goal_alt_matched[i][0] = -1;
2879 goal_alt_matches[i] = -1;
2882 commutative = curr_static_id->commutative;
2884 /* Now see what we need for pseudos that didn't get hard regs or got
2885 the wrong kind of hard reg. For this, we must consider all the
2886 operands together against the register constraints. */
2888 best_losers = best_overall = MAX_RECOG_OPERANDS * 2 + MAX_OVERALL_COST_BOUND;
2889 best_small_class_operands_num = best_reload_sum = 0;
2891 curr_swapped = false;
2892 goal_alt_swapped = false;
2894 /* Make equivalence substitution and memory subreg elimination
2895 before address processing because an address legitimacy can
2896 depend on memory mode. */
2897 for (i = 0; i < n_operands; i++)
2899 rtx op = *curr_id->operand_loc[i];
2900 rtx subst, old = op;
2901 bool op_change_p = false;
2903 if (GET_CODE (old) == SUBREG)
2904 old = SUBREG_REG (old);
2905 subst = get_equiv_substitution (old);
2906 if (subst != old)
2908 subst = copy_rtx (subst);
2909 lra_assert (REG_P (old));
2910 if (GET_CODE (op) == SUBREG)
2911 SUBREG_REG (op) = subst;
2912 else
2913 *curr_id->operand_loc[i] = subst;
2914 if (lra_dump_file != NULL)
2916 fprintf (lra_dump_file,
2917 "Changing pseudo %d in operand %i of insn %u on equiv ",
2918 REGNO (old), i, INSN_UID (curr_insn));
2919 print_value_slim (lra_dump_file, subst, 1);
2920 fprintf (lra_dump_file, "\n");
2922 op_change_p = change_p = true;
2924 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
2926 change_p = true;
2927 lra_update_dup (curr_id, i);
2931 /* Reload address registers and displacements. We do it before
2932 finding an alternative because of memory constraints. */
2933 before = after = NULL_RTX;
2934 for (i = 0; i < n_operands; i++)
2935 if (! curr_static_id->operand[i].is_operator
2936 && process_address (i, &before, &after))
2938 change_p = true;
2939 lra_update_dup (curr_id, i);
2942 if (change_p)
2943 /* If we've changed the instruction then any alternative that
2944 we chose previously may no longer be valid. */
2945 lra_set_used_insn_alternative (curr_insn, -1);
2947 try_swapped:
2949 reused_alternative_num = curr_id->used_insn_alternative;
2950 if (lra_dump_file != NULL && reused_alternative_num >= 0)
2951 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
2952 reused_alternative_num, INSN_UID (curr_insn));
2954 if (process_alt_operands (reused_alternative_num))
2955 alt_p = true;
2957 /* If insn is commutative (it's safe to exchange a certain pair of
2958 operands) then we need to try each alternative twice, the second
2959 time matching those two operands as if we had exchanged them. To
2960 do this, really exchange them in operands.
2962 If we have just tried the alternatives the second time, return
2963 operands to normal and drop through. */
2965 if (reused_alternative_num < 0 && commutative >= 0)
2967 curr_swapped = !curr_swapped;
2968 if (curr_swapped)
2970 swap_operands (commutative);
2971 goto try_swapped;
2973 else
2974 swap_operands (commutative);
2977 /* The operands don't meet the constraints. goal_alt describes the
2978 alternative that we could reach by reloading the fewest operands.
2979 Reload so as to fit it. */
2981 if (! alt_p && ! sec_mem_p)
2983 /* No alternative works with reloads?? */
2984 if (INSN_CODE (curr_insn) >= 0)
2985 fatal_insn ("unable to generate reloads for:", curr_insn);
2986 error_for_asm (curr_insn,
2987 "inconsistent operand constraints in an %<asm%>");
2988 /* Avoid further trouble with this insn. */
2989 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
2990 lra_invalidate_insn_data (curr_insn);
2991 return true;
2994 /* If the best alternative is with operands 1 and 2 swapped, swap
2995 them. Update the operand numbers of any reloads already
2996 pushed. */
2998 if (goal_alt_swapped)
3000 if (lra_dump_file != NULL)
3001 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3002 INSN_UID (curr_insn));
3004 /* Swap the duplicates too. */
3005 swap_operands (commutative);
3006 change_p = true;
3009 #ifdef SECONDARY_MEMORY_NEEDED
3010 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3011 too conservatively. So we use the secondary memory only if there
3012 is no any alternative without reloads. */
3013 use_sec_mem_p = false;
3014 if (! alt_p)
3015 use_sec_mem_p = true;
3016 else if (sec_mem_p)
3018 for (i = 0; i < n_operands; i++)
3019 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3020 break;
3021 use_sec_mem_p = i < n_operands;
3024 if (use_sec_mem_p)
3026 rtx new_reg, set, src, dest;
3027 enum machine_mode sec_mode;
3029 lra_assert (sec_mem_p);
3030 set = single_set (curr_insn);
3031 lra_assert (set != NULL_RTX && ! side_effects_p (set));
3032 dest = SET_DEST (set);
3033 src = SET_SRC (set);
3034 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3035 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src));
3036 #else
3037 sec_mode = GET_MODE (src);
3038 #endif
3039 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3040 NO_REGS, "secondary");
3041 /* If the mode is changed, it should be wider. */
3042 lra_assert (GET_MODE_SIZE (GET_MODE (new_reg))
3043 >= GET_MODE_SIZE (GET_MODE (src)));
3044 after = emit_spill_move (false, new_reg, dest);
3045 lra_process_new_insns (curr_insn, NULL_RTX, after,
3046 "Inserting the sec. move");
3047 before = emit_spill_move (true, new_reg, src);
3048 lra_process_new_insns (curr_insn, before, NULL_RTX, "Changing on");
3049 lra_set_insn_deleted (curr_insn);
3050 return true;
3052 #endif
3054 lra_assert (goal_alt_number >= 0);
3055 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3057 if (lra_dump_file != NULL)
3059 const char *p;
3061 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3062 goal_alt_number, INSN_UID (curr_insn));
3063 for (i = 0; i < n_operands; i++)
3065 p = (curr_static_id->operand_alternative
3066 [goal_alt_number * n_operands + i].constraint);
3067 if (*p == '\0')
3068 continue;
3069 fprintf (lra_dump_file, " (%d) ", i);
3070 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3071 fputc (*p, lra_dump_file);
3073 fprintf (lra_dump_file, "\n");
3076 /* Right now, for any pair of operands I and J that are required to
3077 match, with J < I, goal_alt_matches[I] is J. Add I to
3078 goal_alt_matched[J]. */
3080 for (i = 0; i < n_operands; i++)
3081 if ((j = goal_alt_matches[i]) >= 0)
3083 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3085 /* We allow matching one output operand and several input
3086 operands. */
3087 lra_assert (k == 0
3088 || (curr_static_id->operand[j].type == OP_OUT
3089 && curr_static_id->operand[i].type == OP_IN
3090 && (curr_static_id->operand
3091 [goal_alt_matched[j][0]].type == OP_IN)));
3092 goal_alt_matched[j][k] = i;
3093 goal_alt_matched[j][k + 1] = -1;
3096 for (i = 0; i < n_operands; i++)
3097 goal_alt_win[i] |= goal_alt_match_win[i];
3099 /* Any constants that aren't allowed and can't be reloaded into
3100 registers are here changed into memory references. */
3101 for (i = 0; i < n_operands; i++)
3102 if (goal_alt_win[i])
3104 int regno;
3105 enum reg_class new_class;
3106 rtx reg = *curr_id->operand_loc[i];
3108 if (GET_CODE (reg) == SUBREG)
3109 reg = SUBREG_REG (reg);
3111 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3113 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3115 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3117 lra_assert (ok_p);
3118 change_class (regno, new_class, " Change", true);
3122 else
3124 const char *constraint;
3125 char c;
3126 rtx op = *curr_id->operand_loc[i];
3127 rtx subreg = NULL_RTX;
3128 enum machine_mode mode = curr_operand_mode[i];
3130 if (GET_CODE (op) == SUBREG)
3132 subreg = op;
3133 op = SUBREG_REG (op);
3134 mode = GET_MODE (op);
3137 if (CONST_POOL_OK_P (mode, op)
3138 && ((targetm.preferred_reload_class
3139 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3140 || no_input_reloads_p))
3142 rtx tem = force_const_mem (mode, op);
3144 change_p = true;
3145 if (subreg != NULL_RTX)
3146 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3148 *curr_id->operand_loc[i] = tem;
3149 lra_update_dup (curr_id, i);
3150 process_address (i, &before, &after);
3152 /* If the alternative accepts constant pool refs directly
3153 there will be no reload needed at all. */
3154 if (subreg != NULL_RTX)
3155 continue;
3156 /* Skip alternatives before the one requested. */
3157 constraint = (curr_static_id->operand_alternative
3158 [goal_alt_number * n_operands + i].constraint);
3159 for (;
3160 (c = *constraint) && c != ',' && c != '#';
3161 constraint += CONSTRAINT_LEN (c, constraint))
3163 if (c == TARGET_MEM_CONSTRAINT || c == 'o')
3164 break;
3165 #ifdef EXTRA_CONSTRAINT_STR
3166 if (EXTRA_MEMORY_CONSTRAINT (c, constraint)
3167 && EXTRA_CONSTRAINT_STR (tem, c, constraint))
3168 break;
3169 #endif
3171 if (c == '\0' || c == ',' || c == '#')
3172 continue;
3174 goal_alt_win[i] = true;
3178 for (i = 0; i < n_operands; i++)
3180 rtx old, new_reg;
3181 rtx op = *curr_id->operand_loc[i];
3183 if (goal_alt_win[i])
3185 if (goal_alt[i] == NO_REGS
3186 && REG_P (op)
3187 /* When we assign NO_REGS it means that we will not
3188 assign a hard register to the scratch pseudo by
3189 assigment pass and the scratch pseudo will be
3190 spilled. Spilled scratch pseudos are transformed
3191 back to scratches at the LRA end. */
3192 && lra_former_scratch_operand_p (curr_insn, i))
3193 change_class (REGNO (op), NO_REGS, " Change", true);
3194 continue;
3197 /* Operands that match previous ones have already been handled. */
3198 if (goal_alt_matches[i] >= 0)
3199 continue;
3201 /* We should not have an operand with a non-offsettable address
3202 appearing where an offsettable address will do. It also may
3203 be a case when the address should be special in other words
3204 not a general one (e.g. it needs no index reg). */
3205 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3207 enum reg_class rclass;
3208 rtx *loc = &XEXP (op, 0);
3209 enum rtx_code code = GET_CODE (*loc);
3211 push_to_sequence (before);
3212 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3213 MEM, SCRATCH);
3214 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3215 new_reg = emit_inc (rclass, *loc, *loc,
3216 /* This value does not matter for MODIFY. */
3217 GET_MODE_SIZE (GET_MODE (op)));
3218 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass,
3219 "offsetable address", &new_reg))
3220 lra_emit_move (new_reg, *loc);
3221 before = get_insns ();
3222 end_sequence ();
3223 *loc = new_reg;
3224 lra_update_dup (curr_id, i);
3226 else if (goal_alt_matched[i][0] == -1)
3228 enum machine_mode mode;
3229 rtx reg, *loc;
3230 int hard_regno, byte;
3231 enum op_type type = curr_static_id->operand[i].type;
3233 loc = curr_id->operand_loc[i];
3234 mode = curr_operand_mode[i];
3235 if (GET_CODE (*loc) == SUBREG)
3237 reg = SUBREG_REG (*loc);
3238 byte = SUBREG_BYTE (*loc);
3239 if (REG_P (reg)
3240 /* Strict_low_part requires reload the register not
3241 the sub-register. */
3242 && (curr_static_id->operand[i].strict_low
3243 || (GET_MODE_SIZE (mode)
3244 <= GET_MODE_SIZE (GET_MODE (reg))
3245 && (hard_regno
3246 = get_try_hard_regno (REGNO (reg))) >= 0
3247 && (simplify_subreg_regno
3248 (hard_regno,
3249 GET_MODE (reg), byte, mode) < 0)
3250 && (goal_alt[i] == NO_REGS
3251 || (simplify_subreg_regno
3252 (ira_class_hard_regs[goal_alt[i]][0],
3253 GET_MODE (reg), byte, mode) >= 0)))))
3255 loc = &SUBREG_REG (*loc);
3256 mode = GET_MODE (*loc);
3259 old = *loc;
3260 if (get_reload_reg (type, mode, old, goal_alt[i], "", &new_reg)
3261 && type != OP_OUT)
3263 push_to_sequence (before);
3264 lra_emit_move (new_reg, old);
3265 before = get_insns ();
3266 end_sequence ();
3268 *loc = new_reg;
3269 if (type != OP_IN
3270 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3272 start_sequence ();
3273 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3274 emit_insn (after);
3275 after = get_insns ();
3276 end_sequence ();
3277 *loc = new_reg;
3279 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3280 if (goal_alt_dont_inherit_ops[j] == i)
3282 lra_set_regno_unique_value (REGNO (new_reg));
3283 break;
3285 lra_update_dup (curr_id, i);
3287 else if (curr_static_id->operand[i].type == OP_IN
3288 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3289 == OP_OUT))
3291 signed char arr[2];
3293 arr[0] = i;
3294 arr[1] = -1;
3295 match_reload (goal_alt_matched[i][0], arr,
3296 goal_alt[i], &before, &after);
3298 else if (curr_static_id->operand[i].type == OP_OUT
3299 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3300 == OP_IN))
3301 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3302 else
3303 /* We must generate code in any case when function
3304 process_alt_operands decides that it is possible. */
3305 gcc_unreachable ();
3307 if (before != NULL_RTX || after != NULL_RTX
3308 || max_regno_before != max_reg_num ())
3309 change_p = true;
3310 if (change_p)
3312 lra_update_operator_dups (curr_id);
3313 /* Something changes -- process the insn. */
3314 lra_update_insn_regno_info (curr_insn);
3316 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3317 return change_p;
3320 /* Return true if X is in LIST. */
3321 static bool
3322 in_list_p (rtx x, rtx list)
3324 for (; list != NULL_RTX; list = XEXP (list, 1))
3325 if (XEXP (list, 0) == x)
3326 return true;
3327 return false;
3330 /* Return true if X contains an allocatable hard register (if
3331 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3332 static bool
3333 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3335 int i, j;
3336 const char *fmt;
3337 enum rtx_code code;
3339 code = GET_CODE (x);
3340 if (REG_P (x))
3342 int regno = REGNO (x);
3343 HARD_REG_SET alloc_regs;
3345 if (hard_reg_p)
3347 if (regno >= FIRST_PSEUDO_REGISTER)
3348 regno = lra_get_regno_hard_regno (regno);
3349 if (regno < 0)
3350 return false;
3351 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3352 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3354 else
3356 if (regno < FIRST_PSEUDO_REGISTER)
3357 return false;
3358 if (! spilled_p)
3359 return true;
3360 return lra_get_regno_hard_regno (regno) < 0;
3363 fmt = GET_RTX_FORMAT (code);
3364 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3366 if (fmt[i] == 'e')
3368 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3369 return true;
3371 else if (fmt[i] == 'E')
3373 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3374 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3375 return true;
3378 return false;
3381 /* Process all regs in debug location *LOC and change them on
3382 equivalent substitution. Return true if any change was done. */
3383 static bool
3384 debug_loc_equivalence_change_p (rtx *loc)
3386 rtx subst, reg, x = *loc;
3387 bool result = false;
3388 enum rtx_code code = GET_CODE (x);
3389 const char *fmt;
3390 int i, j;
3392 if (code == SUBREG)
3394 reg = SUBREG_REG (x);
3395 if ((subst = get_equiv_substitution (reg)) != reg
3396 && GET_MODE (subst) == VOIDmode)
3398 /* We cannot reload debug location. Simplify subreg here
3399 while we know the inner mode. */
3400 *loc = simplify_gen_subreg (GET_MODE (x), subst,
3401 GET_MODE (reg), SUBREG_BYTE (x));
3402 return true;
3405 if (code == REG && (subst = get_equiv_substitution (x)) != x)
3407 *loc = subst;
3408 return true;
3411 /* Scan all the operand sub-expressions. */
3412 fmt = GET_RTX_FORMAT (code);
3413 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3415 if (fmt[i] == 'e')
3416 result = debug_loc_equivalence_change_p (&XEXP (x, i)) || result;
3417 else if (fmt[i] == 'E')
3418 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3419 result
3420 = debug_loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
3422 return result;
3425 /* Maximum allowed number of constraint pass iterations after the last
3426 spill pass. It is for preventing LRA cycling in a bug case. */
3427 #define MAX_CONSTRAINT_ITERATION_NUMBER 15
3429 /* Maximum number of generated reload insns per an insn. It is for
3430 preventing this pass cycling in a bug case. */
3431 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
3433 /* The current iteration number of this LRA pass. */
3434 int lra_constraint_iter;
3436 /* The current iteration number of this LRA pass after the last spill
3437 pass. */
3438 int lra_constraint_iter_after_spill;
3440 /* True if we substituted equiv which needs checking register
3441 allocation correctness because the equivalent value contains
3442 allocatable hard registers or when we restore multi-register
3443 pseudo. */
3444 bool lra_risky_transformations_p;
3446 /* Return true if REGNO is referenced in more than one block. */
3447 static bool
3448 multi_block_pseudo_p (int regno)
3450 basic_block bb = NULL;
3451 unsigned int uid;
3452 bitmap_iterator bi;
3454 if (regno < FIRST_PSEUDO_REGISTER)
3455 return false;
3457 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
3458 if (bb == NULL)
3459 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
3460 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
3461 return true;
3462 return false;
3465 /* Return true if X contains a pseudo dying in INSN. */
3466 static bool
3467 dead_pseudo_p (rtx x, rtx insn)
3469 int i, j;
3470 const char *fmt;
3471 enum rtx_code code;
3473 if (REG_P (x))
3474 return (insn != NULL_RTX
3475 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
3476 code = GET_CODE (x);
3477 fmt = GET_RTX_FORMAT (code);
3478 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3480 if (fmt[i] == 'e')
3482 if (dead_pseudo_p (XEXP (x, i), insn))
3483 return true;
3485 else if (fmt[i] == 'E')
3487 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3488 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
3489 return true;
3492 return false;
3495 /* Return true if INSN contains a dying pseudo in INSN right hand
3496 side. */
3497 static bool
3498 insn_rhs_dead_pseudo_p (rtx insn)
3500 rtx set = single_set (insn);
3502 gcc_assert (set != NULL);
3503 return dead_pseudo_p (SET_SRC (set), insn);
3506 /* Return true if any init insn of REGNO contains a dying pseudo in
3507 insn right hand side. */
3508 static bool
3509 init_insn_rhs_dead_pseudo_p (int regno)
3511 rtx insns = ira_reg_equiv[regno].init_insns;
3513 if (insns == NULL)
3514 return false;
3515 if (INSN_P (insns))
3516 return insn_rhs_dead_pseudo_p (insns);
3517 for (; insns != NULL_RTX; insns = XEXP (insns, 1))
3518 if (insn_rhs_dead_pseudo_p (XEXP (insns, 0)))
3519 return true;
3520 return false;
3523 /* Entry function of LRA constraint pass. Return true if the
3524 constraint pass did change the code. */
3525 bool
3526 lra_constraints (bool first_p)
3528 bool changed_p;
3529 int i, hard_regno, new_insns_num;
3530 unsigned int min_len, new_min_len;
3531 rtx set, x, dest_reg;
3532 basic_block last_bb;
3534 lra_constraint_iter++;
3535 if (lra_dump_file != NULL)
3536 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
3537 lra_constraint_iter);
3538 lra_constraint_iter_after_spill++;
3539 if (lra_constraint_iter_after_spill > MAX_CONSTRAINT_ITERATION_NUMBER)
3540 internal_error
3541 ("Maximum number of LRA constraint passes is achieved (%d)\n",
3542 MAX_CONSTRAINT_ITERATION_NUMBER);
3543 changed_p = false;
3544 lra_risky_transformations_p = false;
3545 new_insn_uid_start = get_max_uid ();
3546 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
3547 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3548 if (lra_reg_info[i].nrefs != 0)
3550 ira_reg_equiv[i].profitable_p = true;
3551 if ((hard_regno = lra_get_regno_hard_regno (i)) >= 0)
3553 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
3555 for (j = 0; j < nregs; j++)
3556 df_set_regs_ever_live (hard_regno + j, true);
3558 else if ((x = get_equiv_substitution (regno_reg_rtx[i])) != NULL_RTX)
3560 bool pseudo_p = contains_reg_p (x, false, false);
3561 rtx set, insn;
3563 /* We don't use DF for compilation speed sake. So it is
3564 problematic to update live info when we use an
3565 equivalence containing pseudos in more than one BB. */
3566 if ((pseudo_p && multi_block_pseudo_p (i))
3567 /* If it is not a reverse equivalence, we check that a
3568 pseudo in rhs of the init insn is not dying in the
3569 insn. Otherwise, the live info at the beginning of
3570 the corresponding BB might be wrong after we
3571 removed the insn. When the equiv can be a
3572 constant, the right hand side of the init insn can
3573 be a pseudo. */
3574 || (! ((insn = ira_reg_equiv[i].init_insns) != NULL_RTX
3575 && INSN_P (insn)
3576 && (set = single_set (insn)) != NULL_RTX
3577 && REG_P (SET_DEST (set))
3578 && (int) REGNO (SET_DEST (set)) == i)
3579 && init_insn_rhs_dead_pseudo_p (i)))
3580 ira_reg_equiv[i].defined_p = false;
3581 else if (! first_p && pseudo_p)
3582 /* After RTL transformation, we can not guarantee that
3583 pseudo in the substitution was not reloaded which
3584 might make equivalence invalid. For example, in
3585 reverse equiv of p0
3587 p0 <- ...
3589 equiv_mem <- p0
3591 the memory address register was reloaded before the
3592 2nd insn. */
3593 ira_reg_equiv[i].defined_p = false;
3594 if (contains_reg_p (x, false, true))
3595 ira_reg_equiv[i].profitable_p = false;
3598 lra_eliminate (false);
3599 min_len = lra_insn_stack_length ();
3600 new_insns_num = 0;
3601 last_bb = NULL;
3602 changed_p = false;
3603 while ((new_min_len = lra_insn_stack_length ()) != 0)
3605 curr_insn = lra_pop_insn ();
3606 --new_min_len;
3607 curr_bb = BLOCK_FOR_INSN (curr_insn);
3608 if (curr_bb != last_bb)
3610 last_bb = curr_bb;
3611 bb_reload_num = lra_curr_reload_num;
3613 if (min_len > new_min_len)
3615 min_len = new_min_len;
3616 new_insns_num = 0;
3618 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
3619 internal_error
3620 ("Max. number of generated reload insns per insn is achieved (%d)\n",
3621 MAX_RELOAD_INSNS_NUMBER);
3622 new_insns_num++;
3623 if (DEBUG_INSN_P (curr_insn))
3625 /* We need to check equivalence in debug insn and change
3626 pseudo to the equivalent value if necessary. */
3627 curr_id = lra_get_insn_recog_data (curr_insn);
3628 if (debug_loc_equivalence_change_p (curr_id->operand_loc[0]))
3629 changed_p = true;
3631 else if (INSN_P (curr_insn))
3633 if ((set = single_set (curr_insn)) != NULL_RTX)
3635 dest_reg = SET_DEST (set);
3636 /* The equivalence pseudo could be set up as SUBREG in a
3637 case when it is a call restore insn in a mode
3638 different from the pseudo mode. */
3639 if (GET_CODE (dest_reg) == SUBREG)
3640 dest_reg = SUBREG_REG (dest_reg);
3641 if ((REG_P (dest_reg)
3642 && (x = get_equiv_substitution (dest_reg)) != dest_reg
3643 /* Remove insns which set up a pseudo whose value
3644 can not be changed. Such insns might be not in
3645 init_insns because we don't update equiv data
3646 during insn transformations.
3648 As an example, let suppose that a pseudo got
3649 hard register and on the 1st pass was not
3650 changed to equivalent constant. We generate an
3651 additional insn setting up the pseudo because of
3652 secondary memory movement. Then the pseudo is
3653 spilled and we use the equiv constant. In this
3654 case we should remove the additional insn and
3655 this insn is not init_insns list. */
3656 && (! MEM_P (x) || MEM_READONLY_P (x)
3657 || in_list_p (curr_insn,
3658 ira_reg_equiv
3659 [REGNO (dest_reg)].init_insns)))
3660 || (((x = get_equiv_substitution (SET_SRC (set)))
3661 != SET_SRC (set))
3662 && in_list_p (curr_insn,
3663 ira_reg_equiv
3664 [REGNO (SET_SRC (set))].init_insns)))
3666 /* This is equiv init insn of pseudo which did not get a
3667 hard register -- remove the insn. */
3668 if (lra_dump_file != NULL)
3670 fprintf (lra_dump_file,
3671 " Removing equiv init insn %i (freq=%d)\n",
3672 INSN_UID (curr_insn),
3673 BLOCK_FOR_INSN (curr_insn)->frequency);
3674 debug_rtl_slim (lra_dump_file,
3675 curr_insn, curr_insn, -1, 0);
3677 if (contains_reg_p (x, true, false))
3678 lra_risky_transformations_p = true;
3679 lra_set_insn_deleted (curr_insn);
3680 continue;
3683 curr_id = lra_get_insn_recog_data (curr_insn);
3684 curr_static_id = curr_id->insn_static_data;
3685 init_curr_insn_input_reloads ();
3686 init_curr_operand_mode ();
3687 if (curr_insn_transform ())
3688 changed_p = true;
3691 /* If we used a new hard regno, changed_p should be true because the
3692 hard reg is assigned to a new pseudo. */
3693 #ifdef ENABLE_CHECKING
3694 if (! changed_p)
3696 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3697 if (lra_reg_info[i].nrefs != 0
3698 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
3700 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
3702 for (j = 0; j < nregs; j++)
3703 lra_assert (df_regs_ever_live_p (hard_regno + j));
3706 #endif
3707 return changed_p;
3710 /* Initiate the LRA constraint pass. It is done once per
3711 function. */
3712 void
3713 lra_constraints_init (void)
3717 /* Finalize the LRA constraint pass. It is done once per
3718 function. */
3719 void
3720 lra_constraints_finish (void)
3726 /* This page contains code to do inheritance/split
3727 transformations. */
3729 /* Number of reloads passed so far in current EBB. */
3730 static int reloads_num;
3732 /* Number of calls passed so far in current EBB. */
3733 static int calls_num;
3735 /* Current reload pseudo check for validity of elements in
3736 USAGE_INSNS. */
3737 static int curr_usage_insns_check;
3739 /* Info about last usage of registers in EBB to do inheritance/split
3740 transformation. Inheritance transformation is done from a spilled
3741 pseudo and split transformations from a hard register or a pseudo
3742 assigned to a hard register. */
3743 struct usage_insns
3745 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
3746 value INSNS is valid. The insns is chain of optional debug insns
3747 and a finishing non-debug insn using the corresponding reg. */
3748 int check;
3749 /* Value of global reloads_num at the last insn in INSNS. */
3750 int reloads_num;
3751 /* Value of global reloads_nums at the last insn in INSNS. */
3752 int calls_num;
3753 /* It can be true only for splitting. And it means that the restore
3754 insn should be put after insn given by the following member. */
3755 bool after_p;
3756 /* Next insns in the current EBB which use the original reg and the
3757 original reg value is not changed between the current insn and
3758 the next insns. In order words, e.g. for inheritance, if we need
3759 to use the original reg value again in the next insns we can try
3760 to use the value in a hard register from a reload insn of the
3761 current insn. */
3762 rtx insns;
3765 /* Map: regno -> corresponding pseudo usage insns. */
3766 static struct usage_insns *usage_insns;
3768 static void
3769 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
3771 usage_insns[regno].check = curr_usage_insns_check;
3772 usage_insns[regno].insns = insn;
3773 usage_insns[regno].reloads_num = reloads_num;
3774 usage_insns[regno].calls_num = calls_num;
3775 usage_insns[regno].after_p = after_p;
3778 /* The function is used to form list REGNO usages which consists of
3779 optional debug insns finished by a non-debug insn using REGNO.
3780 RELOADS_NUM is current number of reload insns processed so far. */
3781 static void
3782 add_next_usage_insn (int regno, rtx insn, int reloads_num)
3784 rtx next_usage_insns;
3786 if (usage_insns[regno].check == curr_usage_insns_check
3787 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
3788 && DEBUG_INSN_P (insn))
3790 /* Check that we did not add the debug insn yet. */
3791 if (next_usage_insns != insn
3792 && (GET_CODE (next_usage_insns) != INSN_LIST
3793 || XEXP (next_usage_insns, 0) != insn))
3794 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
3795 next_usage_insns);
3797 else if (NONDEBUG_INSN_P (insn))
3798 setup_next_usage_insn (regno, insn, reloads_num, false);
3799 else
3800 usage_insns[regno].check = 0;
3803 /* Replace all references to register OLD_REGNO in *LOC with pseudo
3804 register NEW_REG. Return true if any change was made. */
3805 static bool
3806 substitute_pseudo (rtx *loc, int old_regno, rtx new_reg)
3808 rtx x = *loc;
3809 bool result = false;
3810 enum rtx_code code;
3811 const char *fmt;
3812 int i, j;
3814 if (x == NULL_RTX)
3815 return false;
3817 code = GET_CODE (x);
3818 if (code == REG && (int) REGNO (x) == old_regno)
3820 enum machine_mode mode = GET_MODE (*loc);
3821 enum machine_mode inner_mode = GET_MODE (new_reg);
3823 if (mode != inner_mode)
3825 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
3826 || ! SCALAR_INT_MODE_P (inner_mode))
3827 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
3828 else
3829 new_reg = gen_lowpart_SUBREG (mode, new_reg);
3831 *loc = new_reg;
3832 return true;
3835 /* Scan all the operand sub-expressions. */
3836 fmt = GET_RTX_FORMAT (code);
3837 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3839 if (fmt[i] == 'e')
3841 if (substitute_pseudo (&XEXP (x, i), old_regno, new_reg))
3842 result = true;
3844 else if (fmt[i] == 'E')
3846 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3847 if (substitute_pseudo (&XVECEXP (x, i, j), old_regno, new_reg))
3848 result = true;
3851 return result;
3854 /* Registers involved in inheritance/split in the current EBB
3855 (inheritance/split pseudos and original registers). */
3856 static bitmap_head check_only_regs;
3858 /* Do inheritance transformations for insn INSN, which defines (if
3859 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
3860 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
3861 form as the "insns" field of usage_insns. Return true if we
3862 succeed in such transformation.
3864 The transformations look like:
3866 p <- ... i <- ...
3867 ... p <- i (new insn)
3868 ... =>
3869 <- ... p ... <- ... i ...
3871 ... i <- p (new insn)
3872 <- ... p ... <- ... i ...
3873 ... =>
3874 <- ... p ... <- ... i ...
3875 where p is a spilled original pseudo and i is a new inheritance pseudo.
3878 The inheritance pseudo has the smallest class of two classes CL and
3879 class of ORIGINAL REGNO. */
3880 static bool
3881 inherit_reload_reg (bool def_p, int original_regno,
3882 enum reg_class cl, rtx insn, rtx next_usage_insns)
3884 enum reg_class rclass = lra_get_allocno_class (original_regno);
3885 rtx original_reg = regno_reg_rtx[original_regno];
3886 rtx new_reg, new_insns, usage_insn;
3888 lra_assert (! usage_insns[original_regno].after_p);
3889 if (lra_dump_file != NULL)
3890 fprintf (lra_dump_file,
3891 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
3892 if (! ira_reg_classes_intersect_p[cl][rclass])
3894 if (lra_dump_file != NULL)
3896 fprintf (lra_dump_file,
3897 " Rejecting inheritance for %d "
3898 "because of disjoint classes %s and %s\n",
3899 original_regno, reg_class_names[cl],
3900 reg_class_names[rclass]);
3901 fprintf (lra_dump_file,
3902 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
3904 return false;
3906 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
3907 /* We don't use a subset of two classes because it can be
3908 NO_REGS. This transformation is still profitable in most
3909 cases even if the classes are not intersected as register
3910 move is probably cheaper than a memory load. */
3911 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
3913 if (lra_dump_file != NULL)
3914 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
3915 reg_class_names[cl], reg_class_names[rclass]);
3917 rclass = cl;
3919 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
3920 rclass, "inheritance");
3921 start_sequence ();
3922 if (def_p)
3923 emit_move_insn (original_reg, new_reg);
3924 else
3925 emit_move_insn (new_reg, original_reg);
3926 new_insns = get_insns ();
3927 end_sequence ();
3928 if (NEXT_INSN (new_insns) != NULL_RTX)
3930 if (lra_dump_file != NULL)
3932 fprintf (lra_dump_file,
3933 " Rejecting inheritance %d->%d "
3934 "as it results in 2 or more insns:\n",
3935 original_regno, REGNO (new_reg));
3936 debug_rtl_slim (lra_dump_file, new_insns, NULL_RTX, -1, 0);
3937 fprintf (lra_dump_file,
3938 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
3940 return false;
3942 substitute_pseudo (&insn, original_regno, new_reg);
3943 lra_update_insn_regno_info (insn);
3944 if (! def_p)
3945 /* We now have a new usage insn for original regno. */
3946 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
3947 if (lra_dump_file != NULL)
3948 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
3949 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
3950 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
3951 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
3952 bitmap_set_bit (&check_only_regs, original_regno);
3953 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
3954 if (def_p)
3955 lra_process_new_insns (insn, NULL_RTX, new_insns,
3956 "Add original<-inheritance");
3957 else
3958 lra_process_new_insns (insn, new_insns, NULL_RTX,
3959 "Add inheritance<-original");
3960 while (next_usage_insns != NULL_RTX)
3962 if (GET_CODE (next_usage_insns) != INSN_LIST)
3964 usage_insn = next_usage_insns;
3965 lra_assert (NONDEBUG_INSN_P (usage_insn));
3966 next_usage_insns = NULL;
3968 else
3970 usage_insn = XEXP (next_usage_insns, 0);
3971 lra_assert (DEBUG_INSN_P (usage_insn));
3972 next_usage_insns = XEXP (next_usage_insns, 1);
3974 substitute_pseudo (&usage_insn, original_regno, new_reg);
3975 lra_update_insn_regno_info (usage_insn);
3976 if (lra_dump_file != NULL)
3978 fprintf (lra_dump_file,
3979 " Inheritance reuse change %d->%d (bb%d):\n",
3980 original_regno, REGNO (new_reg),
3981 BLOCK_FOR_INSN (usage_insn)->index);
3982 debug_rtl_slim (lra_dump_file, usage_insn, usage_insn,
3983 -1, 0);
3986 if (lra_dump_file != NULL)
3987 fprintf (lra_dump_file,
3988 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
3989 return true;
3992 /* Return true if we need a caller save/restore for pseudo REGNO which
3993 was assigned to a hard register. */
3994 static inline bool
3995 need_for_call_save_p (int regno)
3997 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
3998 return (usage_insns[regno].calls_num < calls_num
3999 && (overlaps_hard_reg_set_p
4000 (call_used_reg_set,
4001 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])));
4004 /* Global registers occuring in the current EBB. */
4005 static bitmap_head ebb_global_regs;
4007 /* Return true if we need a split for hard register REGNO or pseudo
4008 REGNO which was assigned to a hard register.
4009 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4010 used for reloads since the EBB end. It is an approximation of the
4011 used hard registers in the split range. The exact value would
4012 require expensive calculations. If we were aggressive with
4013 splitting because of the approximation, the split pseudo will save
4014 the same hard register assignment and will be removed in the undo
4015 pass. We still need the approximation because too aggressive
4016 splitting would result in too inaccurate cost calculation in the
4017 assignment pass because of too many generated moves which will be
4018 probably removed in the undo pass. */
4019 static inline bool
4020 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4022 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4024 lra_assert (hard_regno >= 0);
4025 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4026 /* Don't split eliminable hard registers, otherwise we can
4027 split hard registers like hard frame pointer, which
4028 lives on BB start/end according to DF-infrastructure,
4029 when there is a pseudo assigned to the register and
4030 living in the same BB. */
4031 && (regno >= FIRST_PSEUDO_REGISTER
4032 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4033 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4034 /* We need at least 2 reloads to make pseudo splitting
4035 profitable. We should provide hard regno splitting in
4036 any case to solve 1st insn scheduling problem when
4037 moving hard register definition up might result in
4038 impossibility to find hard register for reload pseudo of
4039 small register class. */
4040 && (usage_insns[regno].reloads_num
4041 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 2) < reloads_num)
4042 && (regno < FIRST_PSEUDO_REGISTER
4043 /* For short living pseudos, spilling + inheritance can
4044 be considered a substitution for splitting.
4045 Therefore we do not splitting for local pseudos. It
4046 decreases also aggressiveness of splitting. The
4047 minimal number of references is chosen taking into
4048 account that for 2 references splitting has no sense
4049 as we can just spill the pseudo. */
4050 || (regno >= FIRST_PSEUDO_REGISTER
4051 && lra_reg_info[regno].nrefs > 3
4052 && bitmap_bit_p (&ebb_global_regs, regno))))
4053 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4056 /* Return class for the split pseudo created from original pseudo with
4057 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4058 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4059 results in no secondary memory movements. */
4060 static enum reg_class
4061 choose_split_class (enum reg_class allocno_class,
4062 int hard_regno ATTRIBUTE_UNUSED,
4063 enum machine_mode mode ATTRIBUTE_UNUSED)
4065 #ifndef SECONDARY_MEMORY_NEEDED
4066 return allocno_class;
4067 #else
4068 int i;
4069 enum reg_class cl, best_cl = NO_REGS;
4070 enum reg_class hard_reg_class = REGNO_REG_CLASS (hard_regno);
4072 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4073 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4074 return allocno_class;
4075 for (i = 0;
4076 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4077 i++)
4078 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4079 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4080 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4081 && (best_cl == NO_REGS
4082 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4083 best_cl = cl;
4084 return best_cl;
4085 #endif
4088 /* Do split transformations for insn INSN, which defines or uses
4089 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4090 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4091 "insns" field of usage_insns.
4093 The transformations look like:
4095 p <- ... p <- ...
4096 ... s <- p (new insn -- save)
4097 ... =>
4098 ... p <- s (new insn -- restore)
4099 <- ... p ... <- ... p ...
4101 <- ... p ... <- ... p ...
4102 ... s <- p (new insn -- save)
4103 ... =>
4104 ... p <- s (new insn -- restore)
4105 <- ... p ... <- ... p ...
4107 where p is an original pseudo got a hard register or a hard
4108 register and s is a new split pseudo. The save is put before INSN
4109 if BEFORE_P is true. Return true if we succeed in such
4110 transformation. */
4111 static bool
4112 split_reg (bool before_p, int original_regno, rtx insn, rtx next_usage_insns)
4114 enum reg_class rclass;
4115 rtx original_reg;
4116 int hard_regno;
4117 rtx new_reg, save, restore, usage_insn;
4118 bool after_p;
4119 bool call_save_p;
4121 if (original_regno < FIRST_PSEUDO_REGISTER)
4123 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4124 hard_regno = original_regno;
4125 call_save_p = false;
4127 else
4129 hard_regno = reg_renumber[original_regno];
4130 rclass = lra_get_allocno_class (original_regno);
4131 original_reg = regno_reg_rtx[original_regno];
4132 call_save_p = need_for_call_save_p (original_regno);
4134 original_reg = regno_reg_rtx[original_regno];
4135 lra_assert (hard_regno >= 0);
4136 if (lra_dump_file != NULL)
4137 fprintf (lra_dump_file,
4138 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4139 if (call_save_p)
4141 enum machine_mode sec_mode;
4143 #ifdef SECONDARY_MEMORY_NEEDED_MODE
4144 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (original_reg));
4145 #else
4146 sec_mode = GET_MODE (original_reg);
4147 #endif
4148 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
4149 NO_REGS, "save");
4151 else
4153 rclass = choose_split_class (rclass, hard_regno,
4154 GET_MODE (original_reg));
4155 if (rclass == NO_REGS)
4157 if (lra_dump_file != NULL)
4159 fprintf (lra_dump_file,
4160 " Rejecting split of %d(%s): "
4161 "no good reg class for %d(%s)\n",
4162 original_regno,
4163 reg_class_names[lra_get_allocno_class (original_regno)],
4164 hard_regno,
4165 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4166 fprintf
4167 (lra_dump_file,
4168 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4170 return false;
4172 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4173 rclass, "split");
4174 reg_renumber[REGNO (new_reg)] = hard_regno;
4176 save = emit_spill_move (true, new_reg, original_reg);
4177 if (NEXT_INSN (save) != NULL_RTX)
4179 lra_assert (! call_save_p);
4180 if (lra_dump_file != NULL)
4182 fprintf
4183 (lra_dump_file,
4184 " Rejecting split %d->%d resulting in > 2 %s save insns:\n",
4185 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4186 debug_rtl_slim (lra_dump_file, save, NULL_RTX, -1, 0);
4187 fprintf (lra_dump_file,
4188 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4190 return false;
4192 restore = emit_spill_move (false, new_reg, original_reg);
4193 if (NEXT_INSN (restore) != NULL_RTX)
4195 lra_assert (! call_save_p);
4196 if (lra_dump_file != NULL)
4198 fprintf (lra_dump_file,
4199 " Rejecting split %d->%d "
4200 "resulting in > 2 %s restore insns:\n",
4201 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4202 debug_rtl_slim (lra_dump_file, restore, NULL_RTX, -1, 0);
4203 fprintf (lra_dump_file,
4204 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4206 return false;
4208 after_p = usage_insns[original_regno].after_p;
4209 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4210 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4211 bitmap_set_bit (&check_only_regs, original_regno);
4212 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4213 for (;;)
4215 if (GET_CODE (next_usage_insns) != INSN_LIST)
4217 usage_insn = next_usage_insns;
4218 break;
4220 usage_insn = XEXP (next_usage_insns, 0);
4221 lra_assert (DEBUG_INSN_P (usage_insn));
4222 next_usage_insns = XEXP (next_usage_insns, 1);
4223 substitute_pseudo (&usage_insn, original_regno, new_reg);
4224 lra_update_insn_regno_info (usage_insn);
4225 if (lra_dump_file != NULL)
4227 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
4228 original_regno, REGNO (new_reg));
4229 debug_rtl_slim (lra_dump_file, usage_insn, usage_insn,
4230 -1, 0);
4233 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
4234 lra_assert (usage_insn != insn || (after_p && before_p));
4235 lra_process_new_insns (usage_insn, after_p ? NULL_RTX : restore,
4236 after_p ? restore : NULL_RTX,
4237 call_save_p
4238 ? "Add reg<-save" : "Add reg<-split");
4239 lra_process_new_insns (insn, before_p ? save : NULL_RTX,
4240 before_p ? NULL_RTX : save,
4241 call_save_p
4242 ? "Add save<-reg" : "Add split<-reg");
4243 if (lra_dump_file != NULL)
4244 fprintf (lra_dump_file,
4245 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4246 return true;
4249 /* Recognize that we need a split transformation for insn INSN, which
4250 defines or uses REGNO in its insn biggest MODE (we use it only if
4251 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
4252 hard registers which might be used for reloads since the EBB end.
4253 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
4254 uid before starting INSN processing. Return true if we succeed in
4255 such transformation. */
4256 static bool
4257 split_if_necessary (int regno, enum machine_mode mode,
4258 HARD_REG_SET potential_reload_hard_regs,
4259 bool before_p, rtx insn, int max_uid)
4261 bool res = false;
4262 int i, nregs = 1;
4263 rtx next_usage_insns;
4265 if (regno < FIRST_PSEUDO_REGISTER)
4266 nregs = hard_regno_nregs[regno][mode];
4267 for (i = 0; i < nregs; i++)
4268 if (usage_insns[regno + i].check == curr_usage_insns_check
4269 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
4270 /* To avoid processing the register twice or more. */
4271 && ((GET_CODE (next_usage_insns) != INSN_LIST
4272 && INSN_UID (next_usage_insns) < max_uid)
4273 || (GET_CODE (next_usage_insns) == INSN_LIST
4274 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
4275 && need_for_split_p (potential_reload_hard_regs, regno + i)
4276 && split_reg (before_p, regno + i, insn, next_usage_insns))
4277 res = true;
4278 return res;
4281 /* Check only registers living at the current program point in the
4282 current EBB. */
4283 static bitmap_head live_regs;
4285 /* Update live info in EBB given by its HEAD and TAIL insns after
4286 inheritance/split transformation. The function removes dead moves
4287 too. */
4288 static void
4289 update_ebb_live_info (rtx head, rtx tail)
4291 unsigned int j;
4292 int regno;
4293 bool live_p;
4294 rtx prev_insn, set;
4295 bool remove_p;
4296 basic_block last_bb, prev_bb, curr_bb;
4297 bitmap_iterator bi;
4298 struct lra_insn_reg *reg;
4299 edge e;
4300 edge_iterator ei;
4302 last_bb = BLOCK_FOR_INSN (tail);
4303 prev_bb = NULL;
4304 for (curr_insn = tail;
4305 curr_insn != PREV_INSN (head);
4306 curr_insn = prev_insn)
4308 prev_insn = PREV_INSN (curr_insn);
4309 /* We need to process empty blocks too. They contain
4310 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
4311 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
4312 continue;
4313 curr_bb = BLOCK_FOR_INSN (curr_insn);
4314 if (curr_bb != prev_bb)
4316 if (prev_bb != NULL)
4318 /* Update df_get_live_in (prev_bb): */
4319 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4320 if (bitmap_bit_p (&live_regs, j))
4321 bitmap_set_bit (df_get_live_in (prev_bb), j);
4322 else
4323 bitmap_clear_bit (df_get_live_in (prev_bb), j);
4325 if (curr_bb != last_bb)
4327 /* Update df_get_live_out (curr_bb): */
4328 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4330 live_p = bitmap_bit_p (&live_regs, j);
4331 if (! live_p)
4332 FOR_EACH_EDGE (e, ei, curr_bb->succs)
4333 if (bitmap_bit_p (df_get_live_in (e->dest), j))
4335 live_p = true;
4336 break;
4338 if (live_p)
4339 bitmap_set_bit (df_get_live_out (curr_bb), j);
4340 else
4341 bitmap_clear_bit (df_get_live_out (curr_bb), j);
4344 prev_bb = curr_bb;
4345 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
4347 if (! NONDEBUG_INSN_P (curr_insn))
4348 continue;
4349 curr_id = lra_get_insn_recog_data (curr_insn);
4350 remove_p = false;
4351 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
4352 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
4353 && bitmap_bit_p (&check_only_regs, regno)
4354 && ! bitmap_bit_p (&live_regs, regno))
4355 remove_p = true;
4356 /* See which defined values die here. */
4357 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4358 if (reg->type == OP_OUT && ! reg->subreg_p)
4359 bitmap_clear_bit (&live_regs, reg->regno);
4360 /* Mark each used value as live. */
4361 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4362 if (reg->type == OP_IN
4363 && bitmap_bit_p (&check_only_regs, reg->regno))
4364 bitmap_set_bit (&live_regs, reg->regno);
4365 /* It is quite important to remove dead move insns because it
4366 means removing dead store. We don't need to process them for
4367 constraints. */
4368 if (remove_p)
4370 if (lra_dump_file != NULL)
4372 fprintf (lra_dump_file, " Removing dead insn:\n ");
4373 debug_rtl_slim (lra_dump_file, curr_insn, curr_insn, -1, 0);
4375 lra_set_insn_deleted (curr_insn);
4380 /* The structure describes info to do an inheritance for the current
4381 insn. We need to collect such info first before doing the
4382 transformations because the transformations change the insn
4383 internal representation. */
4384 struct to_inherit
4386 /* Original regno. */
4387 int regno;
4388 /* Subsequent insns which can inherit original reg value. */
4389 rtx insns;
4392 /* Array containing all info for doing inheritance from the current
4393 insn. */
4394 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
4396 /* Number elements in the previous array. */
4397 static int to_inherit_num;
4399 /* Add inheritance info REGNO and INSNS. Their meaning is described in
4400 structure to_inherit. */
4401 static void
4402 add_to_inherit (int regno, rtx insns)
4404 int i;
4406 for (i = 0; i < to_inherit_num; i++)
4407 if (to_inherit[i].regno == regno)
4408 return;
4409 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
4410 to_inherit[to_inherit_num].regno = regno;
4411 to_inherit[to_inherit_num++].insns = insns;
4414 /* Return the last non-debug insn in basic block BB, or the block begin
4415 note if none. */
4416 static rtx
4417 get_last_insertion_point (basic_block bb)
4419 rtx insn;
4421 FOR_BB_INSNS_REVERSE (bb, insn)
4422 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
4423 return insn;
4424 gcc_unreachable ();
4427 /* Set up RES by registers living on edges FROM except the edge (FROM,
4428 TO) or by registers set up in a jump insn in BB FROM. */
4429 static void
4430 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
4432 rtx last;
4433 struct lra_insn_reg *reg;
4434 edge e;
4435 edge_iterator ei;
4437 lra_assert (to != NULL);
4438 bitmap_clear (res);
4439 FOR_EACH_EDGE (e, ei, from->succs)
4440 if (e->dest != to)
4441 bitmap_ior_into (res, df_get_live_in (e->dest));
4442 last = get_last_insertion_point (from);
4443 if (! JUMP_P (last))
4444 return;
4445 curr_id = lra_get_insn_recog_data (last);
4446 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4447 if (reg->type != OP_IN)
4448 bitmap_set_bit (res, reg->regno);
4451 /* Used as a temporary results of some bitmap calculations. */
4452 static bitmap_head temp_bitmap;
4454 /* Do inheritance/split transformations in EBB starting with HEAD and
4455 finishing on TAIL. We process EBB insns in the reverse order.
4456 Return true if we did any inheritance/split transformation in the
4457 EBB.
4459 We should avoid excessive splitting which results in worse code
4460 because of inaccurate cost calculations for spilling new split
4461 pseudos in such case. To achieve this we do splitting only if
4462 register pressure is high in given basic block and there are reload
4463 pseudos requiring hard registers. We could do more register
4464 pressure calculations at any given program point to avoid necessary
4465 splitting even more but it is to expensive and the current approach
4466 works well enough. */
4467 static bool
4468 inherit_in_ebb (rtx head, rtx tail)
4470 int i, src_regno, dst_regno, nregs;
4471 bool change_p, succ_p;
4472 rtx prev_insn, next_usage_insns, set, last_insn;
4473 enum reg_class cl;
4474 struct lra_insn_reg *reg;
4475 basic_block last_processed_bb, curr_bb = NULL;
4476 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
4477 bitmap to_process;
4478 unsigned int j;
4479 bitmap_iterator bi;
4480 bool head_p, after_p;
4482 change_p = false;
4483 curr_usage_insns_check++;
4484 reloads_num = calls_num = 0;
4485 bitmap_clear (&check_only_regs);
4486 last_processed_bb = NULL;
4487 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
4488 CLEAR_HARD_REG_SET (live_hard_regs);
4489 /* We don't process new insns generated in the loop. */
4490 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
4492 prev_insn = PREV_INSN (curr_insn);
4493 if (BLOCK_FOR_INSN (curr_insn) != NULL)
4494 curr_bb = BLOCK_FOR_INSN (curr_insn);
4495 if (last_processed_bb != curr_bb)
4497 /* We are at the end of BB. Add qualified living
4498 pseudos for potential splitting. */
4499 to_process = df_get_live_out (curr_bb);
4500 if (last_processed_bb != NULL)
4502 /* We are somewhere in the middle of EBB. */
4503 get_live_on_other_edges (curr_bb, last_processed_bb,
4504 &temp_bitmap);
4505 to_process = &temp_bitmap;
4507 last_processed_bb = curr_bb;
4508 last_insn = get_last_insertion_point (curr_bb);
4509 after_p = (! JUMP_P (last_insn)
4510 && (! CALL_P (last_insn)
4511 || (find_reg_note (last_insn,
4512 REG_NORETURN, NULL_RTX) == NULL_RTX
4513 && ! SIBLING_CALL_P (last_insn))));
4514 REG_SET_TO_HARD_REG_SET (live_hard_regs, df_get_live_out (curr_bb));
4515 IOR_HARD_REG_SET (live_hard_regs, eliminable_regset);
4516 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
4517 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
4518 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
4520 if ((int) j >= lra_constraint_new_regno_start)
4521 break;
4522 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
4524 if (j < FIRST_PSEUDO_REGISTER)
4525 SET_HARD_REG_BIT (live_hard_regs, j);
4526 else
4527 add_to_hard_reg_set (&live_hard_regs,
4528 PSEUDO_REGNO_MODE (j),
4529 reg_renumber[j]);
4530 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
4534 src_regno = dst_regno = -1;
4535 if (NONDEBUG_INSN_P (curr_insn)
4536 && (set = single_set (curr_insn)) != NULL_RTX
4537 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
4539 src_regno = REGNO (SET_SRC (set));
4540 dst_regno = REGNO (SET_DEST (set));
4542 if (src_regno < lra_constraint_new_regno_start
4543 && src_regno >= FIRST_PSEUDO_REGISTER
4544 && reg_renumber[src_regno] < 0
4545 && dst_regno >= lra_constraint_new_regno_start
4546 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
4548 /* 'reload_pseudo <- original_pseudo'. */
4549 reloads_num++;
4550 succ_p = false;
4551 if (usage_insns[src_regno].check == curr_usage_insns_check
4552 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
4553 succ_p = inherit_reload_reg (false, src_regno, cl,
4554 curr_insn, next_usage_insns);
4555 if (succ_p)
4556 change_p = true;
4557 else
4558 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
4559 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
4560 IOR_HARD_REG_SET (potential_reload_hard_regs,
4561 reg_class_contents[cl]);
4563 else if (src_regno >= lra_constraint_new_regno_start
4564 && dst_regno < lra_constraint_new_regno_start
4565 && dst_regno >= FIRST_PSEUDO_REGISTER
4566 && reg_renumber[dst_regno] < 0
4567 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
4568 && usage_insns[dst_regno].check == curr_usage_insns_check
4569 && (next_usage_insns
4570 = usage_insns[dst_regno].insns) != NULL_RTX)
4572 reloads_num++;
4573 /* 'original_pseudo <- reload_pseudo'. */
4574 if (! JUMP_P (curr_insn)
4575 && inherit_reload_reg (true, dst_regno, cl,
4576 curr_insn, next_usage_insns))
4577 change_p = true;
4578 /* Invalidate. */
4579 usage_insns[dst_regno].check = 0;
4580 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
4581 IOR_HARD_REG_SET (potential_reload_hard_regs,
4582 reg_class_contents[cl]);
4584 else if (INSN_P (curr_insn))
4586 int max_uid = get_max_uid ();
4588 curr_id = lra_get_insn_recog_data (curr_insn);
4589 to_inherit_num = 0;
4590 /* Process insn definitions. */
4591 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4592 if (reg->type != OP_IN
4593 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
4595 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
4596 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
4597 && usage_insns[dst_regno].check == curr_usage_insns_check
4598 && (next_usage_insns
4599 = usage_insns[dst_regno].insns) != NULL_RTX)
4601 struct lra_insn_reg *r;
4603 for (r = curr_id->regs; r != NULL; r = r->next)
4604 if (r->type != OP_OUT && r->regno == dst_regno)
4605 break;
4606 /* Don't do inheritance if the pseudo is also
4607 used in the insn. */
4608 if (r == NULL)
4609 /* We can not do inheritance right now
4610 because the current insn reg info (chain
4611 regs) can change after that. */
4612 add_to_inherit (dst_regno, next_usage_insns);
4614 /* We can not process one reg twice here because of
4615 usage_insns invalidation. */
4616 if ((dst_regno < FIRST_PSEUDO_REGISTER
4617 || reg_renumber[dst_regno] >= 0)
4618 && ! reg->subreg_p && reg->type == OP_OUT)
4620 HARD_REG_SET s;
4622 if (split_if_necessary (dst_regno, reg->biggest_mode,
4623 potential_reload_hard_regs,
4624 false, curr_insn, max_uid))
4625 change_p = true;
4626 CLEAR_HARD_REG_SET (s);
4627 if (dst_regno < FIRST_PSEUDO_REGISTER)
4628 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
4629 else
4630 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
4631 reg_renumber[dst_regno]);
4632 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
4634 /* We should invalidate potential inheritance or
4635 splitting for the current insn usages to the next
4636 usage insns (see code below) as the output pseudo
4637 prevents this. */
4638 if ((dst_regno >= FIRST_PSEUDO_REGISTER
4639 && reg_renumber[dst_regno] < 0)
4640 || (reg->type == OP_OUT && ! reg->subreg_p
4641 && (dst_regno < FIRST_PSEUDO_REGISTER
4642 || reg_renumber[dst_regno] >= 0)))
4644 /* Invalidate. */
4645 if (dst_regno >= FIRST_PSEUDO_REGISTER)
4646 usage_insns[dst_regno].check = 0;
4647 else
4649 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
4650 for (i = 0; i < nregs; i++)
4651 usage_insns[dst_regno + i].check = 0;
4655 if (! JUMP_P (curr_insn))
4656 for (i = 0; i < to_inherit_num; i++)
4657 if (inherit_reload_reg (true, to_inherit[i].regno,
4658 ALL_REGS, curr_insn,
4659 to_inherit[i].insns))
4660 change_p = true;
4661 if (CALL_P (curr_insn))
4663 rtx cheap, pat, dest, restore;
4664 int regno, hard_regno;
4666 calls_num++;
4667 if ((cheap = find_reg_note (curr_insn,
4668 REG_RETURNED, NULL_RTX)) != NULL_RTX
4669 && ((cheap = XEXP (cheap, 0)), true)
4670 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
4671 && (hard_regno = reg_renumber[regno]) >= 0
4672 /* If there are pending saves/restores, the
4673 optimization is not worth. */
4674 && usage_insns[regno].calls_num == calls_num - 1
4675 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
4677 /* Restore the pseudo from the call result as
4678 REG_RETURNED note says that the pseudo value is
4679 in the call result and the pseudo is an argument
4680 of the call. */
4681 pat = PATTERN (curr_insn);
4682 if (GET_CODE (pat) == PARALLEL)
4683 pat = XVECEXP (pat, 0, 0);
4684 dest = SET_DEST (pat);
4685 start_sequence ();
4686 emit_move_insn (cheap, copy_rtx (dest));
4687 restore = get_insns ();
4688 end_sequence ();
4689 lra_process_new_insns (curr_insn, NULL, restore,
4690 "Inserting call parameter restore");
4691 /* We don't need to save/restore of the pseudo from
4692 this call. */
4693 usage_insns[regno].calls_num = calls_num;
4694 bitmap_set_bit (&check_only_regs, regno);
4697 to_inherit_num = 0;
4698 /* Process insn usages. */
4699 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4700 if ((reg->type != OP_OUT
4701 || (reg->type == OP_OUT && reg->subreg_p))
4702 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
4704 if (src_regno >= FIRST_PSEUDO_REGISTER
4705 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
4707 if (usage_insns[src_regno].check == curr_usage_insns_check
4708 && (next_usage_insns
4709 = usage_insns[src_regno].insns) != NULL_RTX
4710 && NONDEBUG_INSN_P (curr_insn))
4711 add_to_inherit (src_regno, next_usage_insns);
4712 else
4713 /* Add usages. */
4714 add_next_usage_insn (src_regno, curr_insn, reloads_num);
4716 else if (src_regno < FIRST_PSEUDO_REGISTER
4717 || reg_renumber[src_regno] >= 0)
4719 bool before_p;
4720 rtx use_insn = curr_insn;
4722 before_p = (JUMP_P (curr_insn)
4723 || (CALL_P (curr_insn) && reg->type == OP_IN));
4724 if (NONDEBUG_INSN_P (curr_insn)
4725 && split_if_necessary (src_regno, reg->biggest_mode,
4726 potential_reload_hard_regs,
4727 before_p, curr_insn, max_uid))
4729 if (reg->subreg_p)
4730 lra_risky_transformations_p = true;
4731 change_p = true;
4732 /* Invalidate. */
4733 usage_insns[src_regno].check = 0;
4734 if (before_p)
4735 use_insn = PREV_INSN (curr_insn);
4737 if (NONDEBUG_INSN_P (curr_insn))
4739 if (src_regno < FIRST_PSEUDO_REGISTER)
4740 add_to_hard_reg_set (&live_hard_regs,
4741 reg->biggest_mode, src_regno);
4742 else
4743 add_to_hard_reg_set (&live_hard_regs,
4744 PSEUDO_REGNO_MODE (src_regno),
4745 reg_renumber[src_regno]);
4747 add_next_usage_insn (src_regno, use_insn, reloads_num);
4750 for (i = 0; i < to_inherit_num; i++)
4752 src_regno = to_inherit[i].regno;
4753 if (inherit_reload_reg (false, src_regno, ALL_REGS,
4754 curr_insn, to_inherit[i].insns))
4755 change_p = true;
4756 else
4757 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
4760 /* We reached the start of the current basic block. */
4761 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
4762 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
4764 /* We reached the beginning of the current block -- do
4765 rest of spliting in the current BB. */
4766 to_process = df_get_live_in (curr_bb);
4767 if (BLOCK_FOR_INSN (head) != curr_bb)
4769 /* We are somewhere in the middle of EBB. */
4770 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
4771 curr_bb, &temp_bitmap);
4772 to_process = &temp_bitmap;
4774 head_p = true;
4775 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
4777 if ((int) j >= lra_constraint_new_regno_start)
4778 break;
4779 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
4780 && usage_insns[j].check == curr_usage_insns_check
4781 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
4783 if (need_for_split_p (potential_reload_hard_regs, j))
4785 if (lra_dump_file != NULL && head_p)
4787 fprintf (lra_dump_file,
4788 " ----------------------------------\n");
4789 head_p = false;
4791 if (split_reg (false, j, bb_note (curr_bb),
4792 next_usage_insns))
4793 change_p = true;
4795 usage_insns[j].check = 0;
4800 return change_p;
4803 /* This value affects EBB forming. If probability of edge from EBB to
4804 a BB is not greater than the following value, we don't add the BB
4805 to EBB. */
4806 #define EBB_PROBABILITY_CUTOFF (REG_BR_PROB_BASE / 2)
4808 /* Current number of inheritance/split iteration. */
4809 int lra_inheritance_iter;
4811 /* Entry function for inheritance/split pass. */
4812 void
4813 lra_inheritance (void)
4815 int i;
4816 basic_block bb, start_bb;
4817 edge e;
4819 timevar_push (TV_LRA_INHERITANCE);
4820 lra_inheritance_iter++;
4821 if (lra_dump_file != NULL)
4822 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
4823 lra_inheritance_iter);
4824 curr_usage_insns_check = 0;
4825 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
4826 for (i = 0; i < lra_constraint_new_regno_start; i++)
4827 usage_insns[i].check = 0;
4828 bitmap_initialize (&check_only_regs, &reg_obstack);
4829 bitmap_initialize (&live_regs, &reg_obstack);
4830 bitmap_initialize (&temp_bitmap, &reg_obstack);
4831 bitmap_initialize (&ebb_global_regs, &reg_obstack);
4832 FOR_EACH_BB (bb)
4834 start_bb = bb;
4835 if (lra_dump_file != NULL)
4836 fprintf (lra_dump_file, "EBB");
4837 /* Form a EBB starting with BB. */
4838 bitmap_clear (&ebb_global_regs);
4839 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
4840 for (;;)
4842 if (lra_dump_file != NULL)
4843 fprintf (lra_dump_file, " %d", bb->index);
4844 if (bb->next_bb == EXIT_BLOCK_PTR || LABEL_P (BB_HEAD (bb->next_bb)))
4845 break;
4846 e = find_fallthru_edge (bb->succs);
4847 if (! e)
4848 break;
4849 if (e->probability <= EBB_PROBABILITY_CUTOFF)
4850 break;
4851 bb = bb->next_bb;
4853 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
4854 if (lra_dump_file != NULL)
4855 fprintf (lra_dump_file, "\n");
4856 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
4857 /* Remember that the EBB head and tail can change in
4858 inherit_in_ebb. */
4859 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
4861 bitmap_clear (&ebb_global_regs);
4862 bitmap_clear (&temp_bitmap);
4863 bitmap_clear (&live_regs);
4864 bitmap_clear (&check_only_regs);
4865 free (usage_insns);
4867 timevar_pop (TV_LRA_INHERITANCE);
4872 /* This page contains code to undo failed inheritance/split
4873 transformations. */
4875 /* Current number of iteration undoing inheritance/split. */
4876 int lra_undo_inheritance_iter;
4878 /* Fix BB live info LIVE after removing pseudos created on pass doing
4879 inheritance/split which are REMOVED_PSEUDOS. */
4880 static void
4881 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
4883 unsigned int regno;
4884 bitmap_iterator bi;
4886 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
4887 if (bitmap_clear_bit (live, regno))
4888 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
4891 /* Return regno of the (subreg of) REG. Otherwise, return a negative
4892 number. */
4893 static int
4894 get_regno (rtx reg)
4896 if (GET_CODE (reg) == SUBREG)
4897 reg = SUBREG_REG (reg);
4898 if (REG_P (reg))
4899 return REGNO (reg);
4900 return -1;
4903 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
4904 return true if we did any change. The undo transformations for
4905 inheritance looks like
4906 i <- i2
4907 p <- i => p <- i2
4908 or removing
4909 p <- i, i <- p, and i <- i3
4910 where p is original pseudo from which inheritance pseudo i was
4911 created, i and i3 are removed inheritance pseudos, i2 is another
4912 not removed inheritance pseudo. All split pseudos or other
4913 occurrences of removed inheritance pseudos are changed on the
4914 corresponding original pseudos.
4916 The function also schedules insns changed and created during
4917 inheritance/split pass for processing by the subsequent constraint
4918 pass. */
4919 static bool
4920 remove_inheritance_pseudos (bitmap remove_pseudos)
4922 basic_block bb;
4923 int regno, sregno, prev_sregno, dregno, restore_regno;
4924 rtx set, prev_set, prev_insn;
4925 bool change_p, done_p;
4927 change_p = ! bitmap_empty_p (remove_pseudos);
4928 /* We can not finish the function right away if CHANGE_P is true
4929 because we need to marks insns affected by previous
4930 inheritance/split pass for processing by the subsequent
4931 constraint pass. */
4932 FOR_EACH_BB (bb)
4934 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
4935 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
4936 FOR_BB_INSNS_REVERSE (bb, curr_insn)
4938 if (! INSN_P (curr_insn))
4939 continue;
4940 done_p = false;
4941 sregno = dregno = -1;
4942 if (change_p && NONDEBUG_INSN_P (curr_insn)
4943 && (set = single_set (curr_insn)) != NULL_RTX)
4945 dregno = get_regno (SET_DEST (set));
4946 sregno = get_regno (SET_SRC (set));
4949 if (sregno >= 0 && dregno >= 0)
4951 if ((bitmap_bit_p (remove_pseudos, sregno)
4952 && (lra_reg_info[sregno].restore_regno == dregno
4953 || (bitmap_bit_p (remove_pseudos, dregno)
4954 && (lra_reg_info[sregno].restore_regno
4955 == lra_reg_info[dregno].restore_regno))))
4956 || (bitmap_bit_p (remove_pseudos, dregno)
4957 && lra_reg_info[dregno].restore_regno == sregno))
4958 /* One of the following cases:
4959 original <- removed inheritance pseudo
4960 removed inherit pseudo <- another removed inherit pseudo
4961 removed inherit pseudo <- original pseudo
4963 removed_split_pseudo <- original_reg
4964 original_reg <- removed_split_pseudo */
4966 if (lra_dump_file != NULL)
4968 fprintf (lra_dump_file, " Removing %s:\n",
4969 bitmap_bit_p (&lra_split_regs, sregno)
4970 || bitmap_bit_p (&lra_split_regs, dregno)
4971 ? "split" : "inheritance");
4972 debug_rtl_slim (lra_dump_file,
4973 curr_insn, curr_insn, -1, 0);
4975 lra_set_insn_deleted (curr_insn);
4976 done_p = true;
4978 else if (bitmap_bit_p (remove_pseudos, sregno)
4979 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
4981 /* Search the following pattern:
4982 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
4983 original_pseudo <- inherit_or_split_pseudo1
4984 where the 2nd insn is the current insn and
4985 inherit_or_split_pseudo2 is not removed. If it is found,
4986 change the current insn onto:
4987 original_pseudo <- inherit_or_split_pseudo2. */
4988 for (prev_insn = PREV_INSN (curr_insn);
4989 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
4990 prev_insn = PREV_INSN (prev_insn))
4992 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
4993 && (prev_set = single_set (prev_insn)) != NULL_RTX
4994 /* There should be no subregs in insn we are
4995 searching because only the original reg might
4996 be in subreg when we changed the mode of
4997 load/store for splitting. */
4998 && REG_P (SET_DEST (prev_set))
4999 && REG_P (SET_SRC (prev_set))
5000 && (int) REGNO (SET_DEST (prev_set)) == sregno
5001 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5002 >= FIRST_PSEUDO_REGISTER)
5003 /* As we consider chain of inheritance or
5004 splitting described in above comment we should
5005 check that sregno and prev_sregno were
5006 inheritance/split pseudos created from the
5007 same original regno. */
5008 && (lra_reg_info[sregno].restore_regno
5009 == lra_reg_info[prev_sregno].restore_regno)
5010 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5012 lra_assert (GET_MODE (SET_SRC (prev_set))
5013 == GET_MODE (regno_reg_rtx[sregno]));
5014 if (GET_CODE (SET_SRC (set)) == SUBREG)
5015 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5016 else
5017 SET_SRC (set) = SET_SRC (prev_set);
5018 lra_push_insn_and_update_insn_regno_info (curr_insn);
5019 lra_set_used_insn_alternative_by_uid
5020 (INSN_UID (curr_insn), -1);
5021 done_p = true;
5022 if (lra_dump_file != NULL)
5024 fprintf (lra_dump_file, " Change reload insn:\n");
5025 debug_rtl_slim (lra_dump_file,
5026 curr_insn, curr_insn, -1, 0);
5031 if (! done_p)
5033 struct lra_insn_reg *reg;
5034 bool restored_regs_p = false;
5035 bool kept_regs_p = false;
5037 curr_id = lra_get_insn_recog_data (curr_insn);
5038 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5040 regno = reg->regno;
5041 restore_regno = lra_reg_info[regno].restore_regno;
5042 if (restore_regno >= 0)
5044 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5046 substitute_pseudo (&curr_insn, regno,
5047 regno_reg_rtx[restore_regno]);
5048 restored_regs_p = true;
5050 else
5051 kept_regs_p = true;
5054 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5056 /* The instruction has changed since the previous
5057 constraints pass. */
5058 lra_push_insn_and_update_insn_regno_info (curr_insn);
5059 lra_set_used_insn_alternative_by_uid
5060 (INSN_UID (curr_insn), -1);
5062 else if (restored_regs_p)
5063 /* The instruction has been restored to the form that
5064 it had during the previous constraints pass. */
5065 lra_update_insn_regno_info (curr_insn);
5066 if (restored_regs_p && lra_dump_file != NULL)
5068 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5069 debug_rtl_slim (lra_dump_file, curr_insn, curr_insn, -1, 0);
5074 return change_p;
5077 /* Entry function for undoing inheritance/split transformation. Return true
5078 if we did any RTL change in this pass. */
5079 bool
5080 lra_undo_inheritance (void)
5082 unsigned int regno;
5083 int restore_regno, hard_regno;
5084 int n_all_inherit, n_inherit, n_all_split, n_split;
5085 bitmap_head remove_pseudos;
5086 bitmap_iterator bi;
5087 bool change_p;
5089 lra_undo_inheritance_iter++;
5090 if (lra_dump_file != NULL)
5091 fprintf (lra_dump_file,
5092 "\n********** Undoing inheritance #%d: **********\n\n",
5093 lra_undo_inheritance_iter);
5094 bitmap_initialize (&remove_pseudos, &reg_obstack);
5095 n_inherit = n_all_inherit = 0;
5096 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5097 if (lra_reg_info[regno].restore_regno >= 0)
5099 n_all_inherit++;
5100 if (reg_renumber[regno] < 0)
5101 bitmap_set_bit (&remove_pseudos, regno);
5102 else
5103 n_inherit++;
5105 if (lra_dump_file != NULL && n_all_inherit != 0)
5106 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
5107 n_inherit, n_all_inherit,
5108 (double) n_inherit / n_all_inherit * 100);
5109 n_split = n_all_split = 0;
5110 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5111 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
5113 n_all_split++;
5114 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
5115 ? reg_renumber[restore_regno] : restore_regno);
5116 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
5117 bitmap_set_bit (&remove_pseudos, regno);
5118 else
5120 n_split++;
5121 if (lra_dump_file != NULL)
5122 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
5123 regno, restore_regno);
5126 if (lra_dump_file != NULL && n_all_split != 0)
5127 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
5128 n_split, n_all_split,
5129 (double) n_split / n_all_split * 100);
5130 change_p = remove_inheritance_pseudos (&remove_pseudos);
5131 bitmap_clear (&remove_pseudos);
5132 /* Clear restore_regnos. */
5133 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5134 lra_reg_info[regno].restore_regno = -1;
5135 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5136 lra_reg_info[regno].restore_regno = -1;
5137 return change_p;