Suppress -fstack-protector warning on hppa.
[official-gcc.git] / gcc / lra-constraints.cc
blob02b5ab4a3169e52477e9da78aa4835e858939465
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2022 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains code for 3 passes: constraint pass,
23 inheritance/split pass, and pass for undoing failed inheritance and
24 split.
26 The major goal of constraint pass is to transform RTL to satisfy
27 insn and address constraints by:
28 o choosing insn alternatives;
29 o generating *reload insns* (or reloads in brief) and *reload
30 pseudos* which will get necessary hard registers later;
31 o substituting pseudos with equivalent values and removing the
32 instructions that initialized those pseudos.
34 The constraint pass has biggest and most complicated code in LRA.
35 There are a lot of important details like:
36 o reuse of input reload pseudos to simplify reload pseudo
37 allocations;
38 o some heuristics to choose insn alternative to improve the
39 inheritance;
40 o early clobbers etc.
42 The pass is mimicking former reload pass in alternative choosing
43 because the reload pass is oriented to current machine description
44 model. It might be changed if the machine description model is
45 changed.
47 There is special code for preventing all LRA and this pass cycling
48 in case of bugs.
50 On the first iteration of the pass we process every instruction and
51 choose an alternative for each one. On subsequent iterations we try
52 to avoid reprocessing instructions if we can be sure that the old
53 choice is still valid.
55 The inheritance/spilt pass is to transform code to achieve
56 ineheritance and live range splitting. It is done on backward
57 traversal of EBBs.
59 The inheritance optimization goal is to reuse values in hard
60 registers. There is analogous optimization in old reload pass. The
61 inheritance is achieved by following transformation:
63 reload_p1 <- p reload_p1 <- p
64 ... new_p <- reload_p1
65 ... => ...
66 reload_p2 <- p reload_p2 <- new_p
68 where p is spilled and not changed between the insns. Reload_p1 is
69 also called *original pseudo* and new_p is called *inheritance
70 pseudo*.
72 The subsequent assignment pass will try to assign the same (or
73 another if it is not possible) hard register to new_p as to
74 reload_p1 or reload_p2.
76 If the assignment pass fails to assign a hard register to new_p,
77 this file will undo the inheritance and restore the original code.
78 This is because implementing the above sequence with a spilled
79 new_p would make the code much worse. The inheritance is done in
80 EBB scope. The above is just a simplified example to get an idea
81 of the inheritance as the inheritance is also done for non-reload
82 insns.
84 Splitting (transformation) is also done in EBB scope on the same
85 pass as the inheritance:
87 r <- ... or ... <- r r <- ... or ... <- r
88 ... s <- r (new insn -- save)
89 ... =>
90 ... r <- s (new insn -- restore)
91 ... <- r ... <- r
93 The *split pseudo* s is assigned to the hard register of the
94 original pseudo or hard register r.
96 Splitting is done:
97 o In EBBs with high register pressure for global pseudos (living
98 in at least 2 BBs) and assigned to hard registers when there
99 are more one reloads needing the hard registers;
100 o for pseudos needing save/restore code around calls.
102 If the split pseudo still has the same hard register as the
103 original pseudo after the subsequent assignment pass or the
104 original pseudo was split, the opposite transformation is done on
105 the same pass for undoing inheritance. */
107 #undef REG_OK_STRICT
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "backend.h"
113 #include "target.h"
114 #include "rtl.h"
115 #include "tree.h"
116 #include "predict.h"
117 #include "df.h"
118 #include "memmodel.h"
119 #include "tm_p.h"
120 #include "expmed.h"
121 #include "optabs.h"
122 #include "regs.h"
123 #include "ira.h"
124 #include "recog.h"
125 #include "output.h"
126 #include "addresses.h"
127 #include "expr.h"
128 #include "cfgrtl.h"
129 #include "rtl-error.h"
130 #include "lra.h"
131 #include "lra-int.h"
132 #include "print-rtl.h"
133 #include "function-abi.h"
134 #include "rtl-iter.h"
136 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
137 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
138 reload insns. */
139 static int bb_reload_num;
141 /* The current insn being processed and corresponding its single set
142 (NULL otherwise), its data (basic block, the insn data, the insn
143 static data, and the mode of each operand). */
144 static rtx_insn *curr_insn;
145 static rtx curr_insn_set;
146 static basic_block curr_bb;
147 static lra_insn_recog_data_t curr_id;
148 static struct lra_static_insn_data *curr_static_id;
149 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
150 /* Mode of the register substituted by its equivalence with VOIDmode
151 (e.g. constant) and whose subreg is given operand of the current
152 insn. VOIDmode in all other cases. */
153 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
157 /* Start numbers for new registers and insns at the current constraints
158 pass start. */
159 static int new_regno_start;
160 static int new_insn_uid_start;
162 /* If LOC is nonnull, strip any outer subreg from it. */
163 static inline rtx *
164 strip_subreg (rtx *loc)
166 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
169 /* Return hard regno of REGNO or if it is was not assigned to a hard
170 register, use a hard register from its allocno class. */
171 static int
172 get_try_hard_regno (int regno)
174 int hard_regno;
175 enum reg_class rclass;
177 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
178 hard_regno = lra_get_regno_hard_regno (regno);
179 if (hard_regno >= 0)
180 return hard_regno;
181 rclass = lra_get_allocno_class (regno);
182 if (rclass == NO_REGS)
183 return -1;
184 return ira_class_hard_regs[rclass][0];
187 /* Return the hard regno of X after removing its subreg. If X is not
188 a register or a subreg of a register, return -1. If X is a pseudo,
189 use its assignment. If FINAL_P return the final hard regno which will
190 be after elimination. */
191 static int
192 get_hard_regno (rtx x, bool final_p)
194 rtx reg;
195 int hard_regno;
197 reg = x;
198 if (SUBREG_P (x))
199 reg = SUBREG_REG (x);
200 if (! REG_P (reg))
201 return -1;
202 if (! HARD_REGISTER_NUM_P (hard_regno = REGNO (reg)))
203 hard_regno = lra_get_regno_hard_regno (hard_regno);
204 if (hard_regno < 0)
205 return -1;
206 if (final_p)
207 hard_regno = lra_get_elimination_hard_regno (hard_regno);
208 if (SUBREG_P (x))
209 hard_regno += subreg_regno_offset (hard_regno, GET_MODE (reg),
210 SUBREG_BYTE (x), GET_MODE (x));
211 return hard_regno;
214 /* If REGNO is a hard register or has been allocated a hard register,
215 return the class of that register. If REGNO is a reload pseudo
216 created by the current constraints pass, return its allocno class.
217 Return NO_REGS otherwise. */
218 static enum reg_class
219 get_reg_class (int regno)
221 int hard_regno;
223 if (! HARD_REGISTER_NUM_P (hard_regno = regno))
224 hard_regno = lra_get_regno_hard_regno (regno);
225 if (hard_regno >= 0)
227 hard_regno = lra_get_elimination_hard_regno (hard_regno);
228 return REGNO_REG_CLASS (hard_regno);
230 if (regno >= new_regno_start)
231 return lra_get_allocno_class (regno);
232 return NO_REGS;
235 /* Return true if REG satisfies (or will satisfy) reg class constraint
236 CL. Use elimination first if REG is a hard register. If REG is a
237 reload pseudo created by this constraints pass, assume that it will
238 be allocated a hard register from its allocno class, but allow that
239 class to be narrowed to CL if it is currently a superset of CL and
240 if either:
242 - ALLOW_ALL_RELOAD_CLASS_CHANGES_P is true or
243 - the instruction we're processing is not a reload move.
245 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
246 REGNO (reg), or NO_REGS if no change in its class was needed. */
247 static bool
248 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class,
249 bool allow_all_reload_class_changes_p = false)
251 enum reg_class rclass, common_class;
252 machine_mode reg_mode;
253 rtx src;
254 int class_size, hard_regno, nregs, i, j;
255 int regno = REGNO (reg);
257 if (new_class != NULL)
258 *new_class = NO_REGS;
259 if (regno < FIRST_PSEUDO_REGISTER)
261 rtx final_reg = reg;
262 rtx *final_loc = &final_reg;
264 lra_eliminate_reg_if_possible (final_loc);
265 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
267 reg_mode = GET_MODE (reg);
268 rclass = get_reg_class (regno);
269 src = curr_insn_set != NULL ? SET_SRC (curr_insn_set) : NULL;
270 if (regno < new_regno_start
271 /* Do not allow the constraints for reload instructions to
272 influence the classes of new pseudos. These reloads are
273 typically moves that have many alternatives, and restricting
274 reload pseudos for one alternative may lead to situations
275 where other reload pseudos are no longer allocatable. */
276 || (!allow_all_reload_class_changes_p
277 && INSN_UID (curr_insn) >= new_insn_uid_start
278 && src != NULL
279 && ((REG_P (src) || MEM_P (src))
280 || (GET_CODE (src) == SUBREG
281 && (REG_P (SUBREG_REG (src)) || MEM_P (SUBREG_REG (src)))))))
282 /* When we don't know what class will be used finally for reload
283 pseudos, we use ALL_REGS. */
284 return ((regno >= new_regno_start && rclass == ALL_REGS)
285 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
286 && ! hard_reg_set_subset_p (reg_class_contents[cl],
287 lra_no_alloc_regs)));
288 else
290 common_class = ira_reg_class_subset[rclass][cl];
291 if (new_class != NULL)
292 *new_class = common_class;
293 if (hard_reg_set_subset_p (reg_class_contents[common_class],
294 lra_no_alloc_regs))
295 return false;
296 /* Check that there are enough allocatable regs. */
297 class_size = ira_class_hard_regs_num[common_class];
298 for (i = 0; i < class_size; i++)
300 hard_regno = ira_class_hard_regs[common_class][i];
301 nregs = hard_regno_nregs (hard_regno, reg_mode);
302 if (nregs == 1)
303 return true;
304 for (j = 0; j < nregs; j++)
305 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
306 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
307 hard_regno + j))
308 break;
309 if (j >= nregs)
310 return true;
312 return false;
316 /* Return true if REGNO satisfies a memory constraint. */
317 static bool
318 in_mem_p (int regno)
320 return get_reg_class (regno) == NO_REGS;
323 /* Return 1 if ADDR is a valid memory address for mode MODE in address
324 space AS, and check that each pseudo has the proper kind of hard
325 reg. */
326 static int
327 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
328 rtx addr, addr_space_t as)
330 #ifdef GO_IF_LEGITIMATE_ADDRESS
331 lra_assert (ADDR_SPACE_GENERIC_P (as));
332 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
333 return 0;
335 win:
336 return 1;
337 #else
338 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
339 #endif
342 namespace {
343 /* Temporarily eliminates registers in an address (for the lifetime of
344 the object). */
345 class address_eliminator {
346 public:
347 address_eliminator (struct address_info *ad);
348 ~address_eliminator ();
350 private:
351 struct address_info *m_ad;
352 rtx *m_base_loc;
353 rtx m_base_reg;
354 rtx *m_index_loc;
355 rtx m_index_reg;
359 address_eliminator::address_eliminator (struct address_info *ad)
360 : m_ad (ad),
361 m_base_loc (strip_subreg (ad->base_term)),
362 m_base_reg (NULL_RTX),
363 m_index_loc (strip_subreg (ad->index_term)),
364 m_index_reg (NULL_RTX)
366 if (m_base_loc != NULL)
368 m_base_reg = *m_base_loc;
369 /* If we have non-legitimate address which is decomposed not in
370 the way we expected, don't do elimination here. In such case
371 the address will be reloaded and elimination will be done in
372 reload insn finally. */
373 if (REG_P (m_base_reg))
374 lra_eliminate_reg_if_possible (m_base_loc);
375 if (m_ad->base_term2 != NULL)
376 *m_ad->base_term2 = *m_ad->base_term;
378 if (m_index_loc != NULL)
380 m_index_reg = *m_index_loc;
381 if (REG_P (m_index_reg))
382 lra_eliminate_reg_if_possible (m_index_loc);
386 address_eliminator::~address_eliminator ()
388 if (m_base_loc && *m_base_loc != m_base_reg)
390 *m_base_loc = m_base_reg;
391 if (m_ad->base_term2 != NULL)
392 *m_ad->base_term2 = *m_ad->base_term;
394 if (m_index_loc && *m_index_loc != m_index_reg)
395 *m_index_loc = m_index_reg;
398 /* Return true if the eliminated form of AD is a legitimate target address.
399 If OP is a MEM, AD is the address within OP, otherwise OP should be
400 ignored. CONSTRAINT is one constraint that the operand may need
401 to meet. */
402 static bool
403 valid_address_p (rtx op, struct address_info *ad,
404 enum constraint_num constraint)
406 address_eliminator eliminator (ad);
408 /* Allow a memory OP if it matches CONSTRAINT, even if CONSTRAINT is more
409 forgiving than "m".
410 Need to extract memory from op for special memory constraint,
411 i.e. bcst_mem_operand in i386 backend. */
412 if (MEM_P (extract_mem_from_operand (op))
413 && insn_extra_relaxed_memory_constraint (constraint)
414 && constraint_satisfied_p (op, constraint))
415 return true;
417 return valid_address_p (ad->mode, *ad->outer, ad->as);
420 /* For special_memory_operand, it could be false for MEM_P (op),
421 i.e. bcst_mem_operand in i386 backend.
422 Extract and return real memory operand or op. */
424 extract_mem_from_operand (rtx op)
426 for (rtx x = op;; x = XEXP (x, 0))
428 if (MEM_P (x))
429 return x;
430 if (GET_RTX_LENGTH (GET_CODE (x)) != 1
431 || GET_RTX_FORMAT (GET_CODE (x))[0] != 'e')
432 break;
434 return op;
437 /* Return true if the eliminated form of memory reference OP satisfies
438 extra (special) memory constraint CONSTRAINT. */
439 static bool
440 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
442 struct address_info ad;
443 rtx mem = extract_mem_from_operand (op);
444 if (!MEM_P (mem))
445 return false;
447 decompose_mem_address (&ad, mem);
448 address_eliminator eliminator (&ad);
449 return constraint_satisfied_p (op, constraint);
452 /* Return true if the eliminated form of address AD satisfies extra
453 address constraint CONSTRAINT. */
454 static bool
455 satisfies_address_constraint_p (struct address_info *ad,
456 enum constraint_num constraint)
458 address_eliminator eliminator (ad);
459 return constraint_satisfied_p (*ad->outer, constraint);
462 /* Return true if the eliminated form of address OP satisfies extra
463 address constraint CONSTRAINT. */
464 static bool
465 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
467 struct address_info ad;
469 decompose_lea_address (&ad, &op);
470 return satisfies_address_constraint_p (&ad, constraint);
473 /* Initiate equivalences for LRA. As we keep original equivalences
474 before any elimination, we need to make copies otherwise any change
475 in insns might change the equivalences. */
476 void
477 lra_init_equiv (void)
479 ira_expand_reg_equiv ();
480 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
482 rtx res;
484 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
485 ira_reg_equiv[i].memory = copy_rtx (res);
486 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
487 ira_reg_equiv[i].invariant = copy_rtx (res);
491 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
493 /* Update equivalence for REGNO. We need to this as the equivalence
494 might contain other pseudos which are changed by their
495 equivalences. */
496 static void
497 update_equiv (int regno)
499 rtx x;
501 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
502 ira_reg_equiv[regno].memory
503 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
504 NULL_RTX);
505 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
506 ira_reg_equiv[regno].invariant
507 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
508 NULL_RTX);
511 /* If we have decided to substitute X with another value, return that
512 value, otherwise return X. */
513 static rtx
514 get_equiv (rtx x)
516 int regno;
517 rtx res;
519 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
520 || ! ira_reg_equiv[regno].defined_p
521 || ! ira_reg_equiv[regno].profitable_p
522 || lra_get_regno_hard_regno (regno) >= 0)
523 return x;
524 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
526 if (targetm.cannot_substitute_mem_equiv_p (res))
527 return x;
528 return res;
530 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
531 return res;
532 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
533 return res;
534 gcc_unreachable ();
537 /* If we have decided to substitute X with the equivalent value,
538 return that value after elimination for INSN, otherwise return
539 X. */
540 static rtx
541 get_equiv_with_elimination (rtx x, rtx_insn *insn)
543 rtx res = get_equiv (x);
545 if (x == res || CONSTANT_P (res))
546 return res;
547 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
548 false, false, 0, true);
551 /* Set up curr_operand_mode. */
552 static void
553 init_curr_operand_mode (void)
555 int nop = curr_static_id->n_operands;
556 for (int i = 0; i < nop; i++)
558 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
559 if (mode == VOIDmode)
561 /* The .md mode for address operands is the mode of the
562 addressed value rather than the mode of the address itself. */
563 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
564 mode = Pmode;
565 else
566 mode = curr_static_id->operand[i].mode;
568 curr_operand_mode[i] = mode;
574 /* The page contains code to reuse input reloads. */
576 /* Structure describes input reload of the current insns. */
577 struct input_reload
579 /* True for input reload of matched operands. */
580 bool match_p;
581 /* Reloaded value. */
582 rtx input;
583 /* Reload pseudo used. */
584 rtx reg;
587 /* The number of elements in the following array. */
588 static int curr_insn_input_reloads_num;
589 /* Array containing info about input reloads. It is used to find the
590 same input reload and reuse the reload pseudo in this case. */
591 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
593 /* Initiate data concerning reuse of input reloads for the current
594 insn. */
595 static void
596 init_curr_insn_input_reloads (void)
598 curr_insn_input_reloads_num = 0;
601 /* The canonical form of an rtx inside a MEM is not necessarily the same as the
602 canonical form of the rtx outside the MEM. Fix this up in the case that
603 we're reloading an address (and therefore pulling it outside a MEM). */
604 static rtx
605 canonicalize_reload_addr (rtx addr)
607 subrtx_var_iterator::array_type array;
608 FOR_EACH_SUBRTX_VAR (iter, array, addr, NONCONST)
610 rtx x = *iter;
611 if (GET_CODE (x) == MULT && CONST_INT_P (XEXP (x, 1)))
613 const HOST_WIDE_INT ci = INTVAL (XEXP (x, 1));
614 const int pwr2 = exact_log2 (ci);
615 if (pwr2 > 0)
617 /* Rewrite this to use a shift instead, which is canonical when
618 outside of a MEM. */
619 PUT_CODE (x, ASHIFT);
620 XEXP (x, 1) = GEN_INT (pwr2);
625 return addr;
628 /* Create a new pseudo using MODE, RCLASS, EXCLUDE_START_HARD_REGS, ORIGINAL or
629 reuse an existing reload pseudo. Don't reuse an existing reload pseudo if
630 IN_SUBREG_P is true and the reused pseudo should be wrapped up in a SUBREG.
631 The result pseudo is returned through RESULT_REG. Return TRUE if we created
632 a new pseudo, FALSE if we reused an existing reload pseudo. Use TITLE to
633 describe new registers for debug purposes. */
634 static bool
635 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
636 enum reg_class rclass, HARD_REG_SET *exclude_start_hard_regs,
637 bool in_subreg_p, const char *title, rtx *result_reg)
639 int i, regno;
640 enum reg_class new_class;
641 bool unique_p = false;
643 if (type == OP_OUT)
645 /* Output reload registers tend to start out with a conservative
646 choice of register class. Usually this is ALL_REGS, although
647 a target might narrow it (for performance reasons) through
648 targetm.preferred_reload_class. It's therefore quite common
649 for a reload instruction to require a more restrictive class
650 than the class that was originally assigned to the reload register.
652 In these situations, it's more efficient to refine the choice
653 of register class rather than create a second reload register.
654 This also helps to avoid cycling for registers that are only
655 used by reload instructions. */
656 if (REG_P (original)
657 && (int) REGNO (original) >= new_regno_start
658 && INSN_UID (curr_insn) >= new_insn_uid_start
659 && in_class_p (original, rclass, &new_class, true))
661 unsigned int regno = REGNO (original);
662 if (lra_dump_file != NULL)
664 fprintf (lra_dump_file, " Reuse r%d for output ", regno);
665 dump_value_slim (lra_dump_file, original, 1);
667 if (new_class != lra_get_allocno_class (regno))
668 lra_change_class (regno, new_class, ", change to", false);
669 if (lra_dump_file != NULL)
670 fprintf (lra_dump_file, "\n");
671 *result_reg = original;
672 return false;
674 *result_reg
675 = lra_create_new_reg_with_unique_value (mode, original, rclass,
676 exclude_start_hard_regs, title);
677 return true;
679 /* Prevent reuse value of expression with side effects,
680 e.g. volatile memory. */
681 if (! side_effects_p (original))
682 for (i = 0; i < curr_insn_input_reloads_num; i++)
684 if (! curr_insn_input_reloads[i].match_p
685 && rtx_equal_p (curr_insn_input_reloads[i].input, original)
686 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
688 rtx reg = curr_insn_input_reloads[i].reg;
689 regno = REGNO (reg);
690 /* If input is equal to original and both are VOIDmode,
691 GET_MODE (reg) might be still different from mode.
692 Ensure we don't return *result_reg with wrong mode. */
693 if (GET_MODE (reg) != mode)
695 if (in_subreg_p)
696 continue;
697 if (maybe_lt (GET_MODE_SIZE (GET_MODE (reg)),
698 GET_MODE_SIZE (mode)))
699 continue;
700 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
701 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
702 continue;
704 *result_reg = reg;
705 if (lra_dump_file != NULL)
707 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
708 dump_value_slim (lra_dump_file, original, 1);
710 if (new_class != lra_get_allocno_class (regno))
711 lra_change_class (regno, new_class, ", change to", false);
712 if (lra_dump_file != NULL)
713 fprintf (lra_dump_file, "\n");
714 return false;
716 /* If we have an input reload with a different mode, make sure it
717 will get a different hard reg. */
718 else if (REG_P (original)
719 && REG_P (curr_insn_input_reloads[i].input)
720 && REGNO (original) == REGNO (curr_insn_input_reloads[i].input)
721 && (GET_MODE (original)
722 != GET_MODE (curr_insn_input_reloads[i].input)))
723 unique_p = true;
725 *result_reg = (unique_p
726 ? lra_create_new_reg_with_unique_value
727 : lra_create_new_reg) (mode, original, rclass,
728 exclude_start_hard_regs, title);
729 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
730 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
731 curr_insn_input_reloads[curr_insn_input_reloads_num].match_p = false;
732 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
733 return true;
737 /* The page contains major code to choose the current insn alternative
738 and generate reloads for it. */
740 /* Return the offset from REGNO of the least significant register
741 in (reg:MODE REGNO).
743 This function is used to tell whether two registers satisfy
744 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
746 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
747 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
749 lra_constraint_offset (int regno, machine_mode mode)
751 lra_assert (regno < FIRST_PSEUDO_REGISTER);
753 scalar_int_mode int_mode;
754 if (WORDS_BIG_ENDIAN
755 && is_a <scalar_int_mode> (mode, &int_mode)
756 && GET_MODE_SIZE (int_mode) > UNITS_PER_WORD)
757 return hard_regno_nregs (regno, mode) - 1;
758 return 0;
761 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
762 if they are the same hard reg, and has special hacks for
763 auto-increment and auto-decrement. This is specifically intended for
764 process_alt_operands to use in determining whether two operands
765 match. X is the operand whose number is the lower of the two.
767 It is supposed that X is the output operand and Y is the input
768 operand. Y_HARD_REGNO is the final hard regno of register Y or
769 register in subreg Y as we know it now. Otherwise, it is a
770 negative value. */
771 static bool
772 operands_match_p (rtx x, rtx y, int y_hard_regno)
774 int i;
775 RTX_CODE code = GET_CODE (x);
776 const char *fmt;
778 if (x == y)
779 return true;
780 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
781 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
783 int j;
785 i = get_hard_regno (x, false);
786 if (i < 0)
787 goto slow;
789 if ((j = y_hard_regno) < 0)
790 goto slow;
792 i += lra_constraint_offset (i, GET_MODE (x));
793 j += lra_constraint_offset (j, GET_MODE (y));
795 return i == j;
798 /* If two operands must match, because they are really a single
799 operand of an assembler insn, then two post-increments are invalid
800 because the assembler insn would increment only once. On the
801 other hand, a post-increment matches ordinary indexing if the
802 post-increment is the output operand. */
803 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
804 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
806 /* Two pre-increments are invalid because the assembler insn would
807 increment only once. On the other hand, a pre-increment matches
808 ordinary indexing if the pre-increment is the input operand. */
809 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
810 || GET_CODE (y) == PRE_MODIFY)
811 return operands_match_p (x, XEXP (y, 0), -1);
813 slow:
815 if (code == REG && REG_P (y))
816 return REGNO (x) == REGNO (y);
818 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
819 && x == SUBREG_REG (y))
820 return true;
821 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
822 && SUBREG_REG (x) == y)
823 return true;
825 /* Now we have disposed of all the cases in which different rtx
826 codes can match. */
827 if (code != GET_CODE (y))
828 return false;
830 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
831 if (GET_MODE (x) != GET_MODE (y))
832 return false;
834 switch (code)
836 CASE_CONST_UNIQUE:
837 return false;
839 case CONST_VECTOR:
840 if (!same_vector_encodings_p (x, y))
841 return false;
842 break;
844 case LABEL_REF:
845 return label_ref_label (x) == label_ref_label (y);
846 case SYMBOL_REF:
847 return XSTR (x, 0) == XSTR (y, 0);
849 default:
850 break;
853 /* Compare the elements. If any pair of corresponding elements fail
854 to match, return false for the whole things. */
856 fmt = GET_RTX_FORMAT (code);
857 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
859 int val, j;
860 switch (fmt[i])
862 case 'w':
863 if (XWINT (x, i) != XWINT (y, i))
864 return false;
865 break;
867 case 'i':
868 if (XINT (x, i) != XINT (y, i))
869 return false;
870 break;
872 case 'p':
873 if (maybe_ne (SUBREG_BYTE (x), SUBREG_BYTE (y)))
874 return false;
875 break;
877 case 'e':
878 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
879 if (val == 0)
880 return false;
881 break;
883 case '0':
884 break;
886 case 'E':
887 if (XVECLEN (x, i) != XVECLEN (y, i))
888 return false;
889 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
891 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
892 if (val == 0)
893 return false;
895 break;
897 /* It is believed that rtx's at this level will never
898 contain anything but integers and other rtx's, except for
899 within LABEL_REFs and SYMBOL_REFs. */
900 default:
901 gcc_unreachable ();
904 return true;
907 /* True if X is a constant that can be forced into the constant pool.
908 MODE is the mode of the operand, or VOIDmode if not known. */
909 #define CONST_POOL_OK_P(MODE, X) \
910 ((MODE) != VOIDmode \
911 && CONSTANT_P (X) \
912 && GET_CODE (X) != HIGH \
913 && GET_MODE_SIZE (MODE).is_constant () \
914 && !targetm.cannot_force_const_mem (MODE, X))
916 /* True if C is a non-empty register class that has too few registers
917 to be safely used as a reload target class. */
918 #define SMALL_REGISTER_CLASS_P(C) \
919 (ira_class_hard_regs_num [(C)] == 1 \
920 || (ira_class_hard_regs_num [(C)] >= 1 \
921 && targetm.class_likely_spilled_p (C)))
923 /* If REG is a reload pseudo, try to make its class satisfying CL. */
924 static void
925 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
927 enum reg_class rclass;
929 /* Do not make more accurate class from reloads generated. They are
930 mostly moves with a lot of constraints. Making more accurate
931 class may results in very narrow class and impossibility of find
932 registers for several reloads of one insn. */
933 if (INSN_UID (curr_insn) >= new_insn_uid_start)
934 return;
935 if (GET_CODE (reg) == SUBREG)
936 reg = SUBREG_REG (reg);
937 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
938 return;
939 if (in_class_p (reg, cl, &rclass) && rclass != cl)
940 lra_change_class (REGNO (reg), rclass, " Change to", true);
943 /* Searches X for any reference to a reg with the same value as REGNO,
944 returning the rtx of the reference found if any. Otherwise,
945 returns NULL_RTX. */
946 static rtx
947 regno_val_use_in (unsigned int regno, rtx x)
949 const char *fmt;
950 int i, j;
951 rtx tem;
953 if (REG_P (x) && lra_reg_info[REGNO (x)].val == lra_reg_info[regno].val)
954 return x;
956 fmt = GET_RTX_FORMAT (GET_CODE (x));
957 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
959 if (fmt[i] == 'e')
961 if ((tem = regno_val_use_in (regno, XEXP (x, i))))
962 return tem;
964 else if (fmt[i] == 'E')
965 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
966 if ((tem = regno_val_use_in (regno , XVECEXP (x, i, j))))
967 return tem;
970 return NULL_RTX;
973 /* Return true if all current insn non-output operands except INS (it
974 has a negaitve end marker) do not use pseudos with the same value
975 as REGNO. */
976 static bool
977 check_conflict_input_operands (int regno, signed char *ins)
979 int in;
980 int n_operands = curr_static_id->n_operands;
982 for (int nop = 0; nop < n_operands; nop++)
983 if (! curr_static_id->operand[nop].is_operator
984 && curr_static_id->operand[nop].type != OP_OUT)
986 for (int i = 0; (in = ins[i]) >= 0; i++)
987 if (in == nop)
988 break;
989 if (in < 0
990 && regno_val_use_in (regno, *curr_id->operand_loc[nop]) != NULL_RTX)
991 return false;
993 return true;
996 /* Generate reloads for matching OUT and INS (array of input operand numbers
997 with end marker -1) with reg class GOAL_CLASS and EXCLUDE_START_HARD_REGS,
998 considering output operands OUTS (similar array to INS) needing to be in
999 different registers. Add input and output reloads correspondingly to the
1000 lists *BEFORE and *AFTER. OUT might be negative. In this case we generate
1001 input reloads for matched input operands INS. EARLY_CLOBBER_P is a flag
1002 that the output operand is early clobbered for chosen alternative. */
1003 static void
1004 match_reload (signed char out, signed char *ins, signed char *outs,
1005 enum reg_class goal_class, HARD_REG_SET *exclude_start_hard_regs,
1006 rtx_insn **before, rtx_insn **after, bool early_clobber_p)
1008 bool out_conflict;
1009 int i, in;
1010 rtx new_in_reg, new_out_reg, reg;
1011 machine_mode inmode, outmode;
1012 rtx in_rtx = *curr_id->operand_loc[ins[0]];
1013 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
1015 inmode = curr_operand_mode[ins[0]];
1016 outmode = out < 0 ? inmode : curr_operand_mode[out];
1017 push_to_sequence (*before);
1018 if (inmode != outmode)
1020 /* process_alt_operands has already checked that the mode sizes
1021 are ordered. */
1022 if (partial_subreg_p (outmode, inmode))
1024 reg = new_in_reg
1025 = lra_create_new_reg_with_unique_value (inmode, in_rtx, goal_class,
1026 exclude_start_hard_regs,
1027 "");
1028 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
1029 LRA_SUBREG_P (new_out_reg) = 1;
1030 /* If the input reg is dying here, we can use the same hard
1031 register for REG and IN_RTX. We do it only for original
1032 pseudos as reload pseudos can die although original
1033 pseudos still live where reload pseudos dies. */
1034 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
1035 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
1036 && (!early_clobber_p
1037 || check_conflict_input_operands(REGNO (in_rtx), ins)))
1038 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
1040 else
1042 reg = new_out_reg
1043 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
1044 goal_class,
1045 exclude_start_hard_regs,
1046 "");
1047 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
1048 /* NEW_IN_REG is non-paradoxical subreg. We don't want
1049 NEW_OUT_REG living above. We add clobber clause for
1050 this. This is just a temporary clobber. We can remove
1051 it at the end of LRA work. */
1052 rtx_insn *clobber = emit_clobber (new_out_reg);
1053 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
1054 LRA_SUBREG_P (new_in_reg) = 1;
1055 if (GET_CODE (in_rtx) == SUBREG)
1057 rtx subreg_reg = SUBREG_REG (in_rtx);
1059 /* If SUBREG_REG is dying here and sub-registers IN_RTX
1060 and NEW_IN_REG are similar, we can use the same hard
1061 register for REG and SUBREG_REG. */
1062 if (REG_P (subreg_reg)
1063 && (int) REGNO (subreg_reg) < lra_new_regno_start
1064 && GET_MODE (subreg_reg) == outmode
1065 && known_eq (SUBREG_BYTE (in_rtx), SUBREG_BYTE (new_in_reg))
1066 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg))
1067 && (! early_clobber_p
1068 || check_conflict_input_operands (REGNO (subreg_reg),
1069 ins)))
1070 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
1074 else
1076 /* Pseudos have values -- see comments for lra_reg_info.
1077 Different pseudos with the same value do not conflict even if
1078 they live in the same place. When we create a pseudo we
1079 assign value of original pseudo (if any) from which we
1080 created the new pseudo. If we create the pseudo from the
1081 input pseudo, the new pseudo will have no conflict with the
1082 input pseudo which is wrong when the input pseudo lives after
1083 the insn and as the new pseudo value is changed by the insn
1084 output. Therefore we create the new pseudo from the output
1085 except the case when we have single matched dying input
1086 pseudo.
1088 We cannot reuse the current output register because we might
1089 have a situation like "a <- a op b", where the constraints
1090 force the second input operand ("b") to match the output
1091 operand ("a"). "b" must then be copied into a new register
1092 so that it doesn't clobber the current value of "a".
1094 We cannot use the same value if the output pseudo is
1095 early clobbered or the input pseudo is mentioned in the
1096 output, e.g. as an address part in memory, because
1097 output reload will actually extend the pseudo liveness.
1098 We don't care about eliminable hard regs here as we are
1099 interesting only in pseudos. */
1101 /* Matching input's register value is the same as one of the other
1102 output operand. Output operands in a parallel insn must be in
1103 different registers. */
1104 out_conflict = false;
1105 if (REG_P (in_rtx))
1107 for (i = 0; outs[i] >= 0; i++)
1109 rtx other_out_rtx = *curr_id->operand_loc[outs[i]];
1110 if (outs[i] != out && REG_P (other_out_rtx)
1111 && (regno_val_use_in (REGNO (in_rtx), other_out_rtx)
1112 != NULL_RTX))
1114 out_conflict = true;
1115 break;
1120 new_in_reg = new_out_reg
1121 = (! early_clobber_p && ins[1] < 0 && REG_P (in_rtx)
1122 && (int) REGNO (in_rtx) < lra_new_regno_start
1123 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
1124 && (! early_clobber_p
1125 || check_conflict_input_operands (REGNO (in_rtx), ins))
1126 && (out < 0
1127 || regno_val_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
1128 && !out_conflict
1129 ? lra_create_new_reg (inmode, in_rtx, goal_class,
1130 exclude_start_hard_regs, "")
1131 : lra_create_new_reg_with_unique_value (outmode, out_rtx, goal_class,
1132 exclude_start_hard_regs,
1133 ""));
1135 /* In operand can be got from transformations before processing insn
1136 constraints. One example of such transformations is subreg
1137 reloading (see function simplify_operand_subreg). The new
1138 pseudos created by the transformations might have inaccurate
1139 class (ALL_REGS) and we should make their classes more
1140 accurate. */
1141 narrow_reload_pseudo_class (in_rtx, goal_class);
1142 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
1143 *before = get_insns ();
1144 end_sequence ();
1145 /* Add the new pseudo to consider values of subsequent input reload
1146 pseudos. */
1147 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
1148 curr_insn_input_reloads[curr_insn_input_reloads_num].input = in_rtx;
1149 curr_insn_input_reloads[curr_insn_input_reloads_num].match_p = true;
1150 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = new_in_reg;
1151 for (i = 0; (in = ins[i]) >= 0; i++)
1152 if (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
1153 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]))
1154 *curr_id->operand_loc[in] = new_in_reg;
1155 else
1157 lra_assert
1158 (GET_MODE (new_out_reg) == GET_MODE (*curr_id->operand_loc[in]));
1159 *curr_id->operand_loc[in] = new_out_reg;
1161 lra_update_dups (curr_id, ins);
1162 if (out < 0)
1163 return;
1164 /* See a comment for the input operand above. */
1165 narrow_reload_pseudo_class (out_rtx, goal_class);
1166 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
1168 reg = SUBREG_P (out_rtx) ? SUBREG_REG (out_rtx) : out_rtx;
1169 start_sequence ();
1170 /* If we had strict_low_part, use it also in reload to keep other
1171 parts unchanged but do it only for regs as strict_low_part
1172 has no sense for memory and probably there is no insn pattern
1173 to match the reload insn in memory case. */
1174 if (out >= 0 && curr_static_id->operand[out].strict_low && REG_P (reg))
1175 out_rtx = gen_rtx_STRICT_LOW_PART (VOIDmode, out_rtx);
1176 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
1177 emit_insn (*after);
1178 *after = get_insns ();
1179 end_sequence ();
1181 *curr_id->operand_loc[out] = new_out_reg;
1182 lra_update_dup (curr_id, out);
1185 /* Return register class which is union of all reg classes in insn
1186 constraint alternative string starting with P. */
1187 static enum reg_class
1188 reg_class_from_constraints (const char *p)
1190 int c, len;
1191 enum reg_class op_class = NO_REGS;
1194 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1196 case '#':
1197 case ',':
1198 return op_class;
1200 case 'g':
1201 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1202 break;
1204 default:
1205 enum constraint_num cn = lookup_constraint (p);
1206 enum reg_class cl = reg_class_for_constraint (cn);
1207 if (cl == NO_REGS)
1209 if (insn_extra_address_constraint (cn))
1210 op_class
1211 = (reg_class_subunion
1212 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1213 ADDRESS, SCRATCH)]);
1214 break;
1217 op_class = reg_class_subunion[op_class][cl];
1218 break;
1220 while ((p += len), c);
1221 return op_class;
1224 /* If OP is a register, return the class of the register as per
1225 get_reg_class, otherwise return NO_REGS. */
1226 static inline enum reg_class
1227 get_op_class (rtx op)
1229 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1232 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1233 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1234 SUBREG for VAL to make them equal. */
1235 static rtx_insn *
1236 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1238 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1240 /* Usually size of mem_pseudo is greater than val size but in
1241 rare cases it can be less as it can be defined by target
1242 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1243 if (! MEM_P (val))
1245 val = gen_lowpart_SUBREG (GET_MODE (mem_pseudo),
1246 GET_CODE (val) == SUBREG
1247 ? SUBREG_REG (val) : val);
1248 LRA_SUBREG_P (val) = 1;
1250 else
1252 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1253 LRA_SUBREG_P (mem_pseudo) = 1;
1256 return to_p ? gen_move_insn (mem_pseudo, val)
1257 : gen_move_insn (val, mem_pseudo);
1260 /* Process a special case insn (register move), return true if we
1261 don't need to process it anymore. INSN should be a single set
1262 insn. Set up that RTL was changed through CHANGE_P and that hook
1263 TARGET_SECONDARY_MEMORY_NEEDED says to use secondary memory through
1264 SEC_MEM_P. */
1265 static bool
1266 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1268 int sregno, dregno;
1269 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1270 rtx_insn *before;
1271 enum reg_class dclass, sclass, secondary_class;
1272 secondary_reload_info sri;
1274 lra_assert (curr_insn_set != NULL_RTX);
1275 dreg = dest = SET_DEST (curr_insn_set);
1276 sreg = src = SET_SRC (curr_insn_set);
1277 if (GET_CODE (dest) == SUBREG)
1278 dreg = SUBREG_REG (dest);
1279 if (GET_CODE (src) == SUBREG)
1280 sreg = SUBREG_REG (src);
1281 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1282 return false;
1283 sclass = dclass = NO_REGS;
1284 if (REG_P (dreg))
1285 dclass = get_reg_class (REGNO (dreg));
1286 gcc_assert (dclass < LIM_REG_CLASSES && dclass >= NO_REGS);
1287 if (dclass == ALL_REGS)
1288 /* ALL_REGS is used for new pseudos created by transformations
1289 like reload of SUBREG_REG (see function
1290 simplify_operand_subreg). We don't know their class yet. We
1291 should figure out the class from processing the insn
1292 constraints not in this fast path function. Even if ALL_REGS
1293 were a right class for the pseudo, secondary_... hooks usually
1294 are not define for ALL_REGS. */
1295 return false;
1296 if (REG_P (sreg))
1297 sclass = get_reg_class (REGNO (sreg));
1298 gcc_assert (sclass < LIM_REG_CLASSES && sclass >= NO_REGS);
1299 if (sclass == ALL_REGS)
1300 /* See comments above. */
1301 return false;
1302 if (sclass == NO_REGS && dclass == NO_REGS)
1303 return false;
1304 if (targetm.secondary_memory_needed (GET_MODE (src), sclass, dclass)
1305 && ((sclass != NO_REGS && dclass != NO_REGS)
1306 || (GET_MODE (src)
1307 != targetm.secondary_memory_needed_mode (GET_MODE (src)))))
1309 *sec_mem_p = true;
1310 return false;
1312 if (! REG_P (dreg) || ! REG_P (sreg))
1313 return false;
1314 sri.prev_sri = NULL;
1315 sri.icode = CODE_FOR_nothing;
1316 sri.extra_cost = 0;
1317 secondary_class = NO_REGS;
1318 /* Set up hard register for a reload pseudo for hook
1319 secondary_reload because some targets just ignore unassigned
1320 pseudos in the hook. */
1321 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1323 dregno = REGNO (dreg);
1324 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1326 else
1327 dregno = -1;
1328 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1330 sregno = REGNO (sreg);
1331 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1333 else
1334 sregno = -1;
1335 if (sclass != NO_REGS)
1336 secondary_class
1337 = (enum reg_class) targetm.secondary_reload (false, dest,
1338 (reg_class_t) sclass,
1339 GET_MODE (src), &sri);
1340 if (sclass == NO_REGS
1341 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1342 && dclass != NO_REGS))
1344 enum reg_class old_sclass = secondary_class;
1345 secondary_reload_info old_sri = sri;
1347 sri.prev_sri = NULL;
1348 sri.icode = CODE_FOR_nothing;
1349 sri.extra_cost = 0;
1350 secondary_class
1351 = (enum reg_class) targetm.secondary_reload (true, src,
1352 (reg_class_t) dclass,
1353 GET_MODE (src), &sri);
1354 /* Check the target hook consistency. */
1355 lra_assert
1356 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1357 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1358 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1360 if (sregno >= 0)
1361 reg_renumber [sregno] = -1;
1362 if (dregno >= 0)
1363 reg_renumber [dregno] = -1;
1364 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1365 return false;
1366 *change_p = true;
1367 new_reg = NULL_RTX;
1368 if (secondary_class != NO_REGS)
1369 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1370 secondary_class, NULL,
1371 "secondary");
1372 start_sequence ();
1373 if (sri.icode == CODE_FOR_nothing)
1374 lra_emit_move (new_reg, src);
1375 else
1377 enum reg_class scratch_class;
1379 scratch_class = (reg_class_from_constraints
1380 (insn_data[sri.icode].operand[2].constraint));
1381 scratch_reg = (lra_create_new_reg_with_unique_value
1382 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1383 scratch_class, NULL, "scratch"));
1384 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1385 src, scratch_reg));
1387 before = get_insns ();
1388 end_sequence ();
1389 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1390 if (new_reg != NULL_RTX)
1391 SET_SRC (curr_insn_set) = new_reg;
1392 else
1394 if (lra_dump_file != NULL)
1396 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1397 dump_insn_slim (lra_dump_file, curr_insn);
1399 lra_set_insn_deleted (curr_insn);
1400 return true;
1402 return false;
1405 /* The following data describe the result of process_alt_operands.
1406 The data are used in curr_insn_transform to generate reloads. */
1408 /* The chosen reg classes which should be used for the corresponding
1409 operands. */
1410 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1411 /* Hard registers which cannot be a start hard register for the corresponding
1412 operands. */
1413 static HARD_REG_SET goal_alt_exclude_start_hard_regs[MAX_RECOG_OPERANDS];
1414 /* True if the operand should be the same as another operand and that
1415 other operand does not need a reload. */
1416 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1417 /* True if the operand does not need a reload. */
1418 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1419 /* True if the operand can be offsetable memory. */
1420 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1421 /* The number of an operand to which given operand can be matched to. */
1422 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1423 /* The number of elements in the following array. */
1424 static int goal_alt_dont_inherit_ops_num;
1425 /* Numbers of operands whose reload pseudos should not be inherited. */
1426 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1427 /* True if the insn commutative operands should be swapped. */
1428 static bool goal_alt_swapped;
1429 /* The chosen insn alternative. */
1430 static int goal_alt_number;
1432 /* True if the corresponding operand is the result of an equivalence
1433 substitution. */
1434 static bool equiv_substition_p[MAX_RECOG_OPERANDS];
1436 /* The following five variables are used to choose the best insn
1437 alternative. They reflect final characteristics of the best
1438 alternative. */
1440 /* Number of necessary reloads and overall cost reflecting the
1441 previous value and other unpleasantness of the best alternative. */
1442 static int best_losers, best_overall;
1443 /* Overall number hard registers used for reloads. For example, on
1444 some targets we need 2 general registers to reload DFmode and only
1445 one floating point register. */
1446 static int best_reload_nregs;
1447 /* Overall number reflecting distances of previous reloading the same
1448 value. The distances are counted from the current BB start. It is
1449 used to improve inheritance chances. */
1450 static int best_reload_sum;
1452 /* True if the current insn should have no correspondingly input or
1453 output reloads. */
1454 static bool no_input_reloads_p, no_output_reloads_p;
1456 /* True if we swapped the commutative operands in the current
1457 insn. */
1458 static int curr_swapped;
1460 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1461 register of class CL. Add any input reloads to list BEFORE. AFTER
1462 is nonnull if *LOC is an automodified value; handle that case by
1463 adding the required output reloads to list AFTER. Return true if
1464 the RTL was changed.
1466 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1467 register. Return false if the address register is correct. */
1468 static bool
1469 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1470 enum reg_class cl)
1472 int regno;
1473 enum reg_class rclass, new_class;
1474 rtx reg;
1475 rtx new_reg;
1476 machine_mode mode;
1477 bool subreg_p, before_p = false;
1479 subreg_p = GET_CODE (*loc) == SUBREG;
1480 if (subreg_p)
1482 reg = SUBREG_REG (*loc);
1483 mode = GET_MODE (reg);
1485 /* For mode with size bigger than ptr_mode, there unlikely to be "mov"
1486 between two registers with different classes, but there normally will
1487 be "mov" which transfers element of vector register into the general
1488 register, and this normally will be a subreg which should be reloaded
1489 as a whole. This is particularly likely to be triggered when
1490 -fno-split-wide-types specified. */
1491 if (!REG_P (reg)
1492 || in_class_p (reg, cl, &new_class)
1493 || known_le (GET_MODE_SIZE (mode), GET_MODE_SIZE (ptr_mode)))
1494 loc = &SUBREG_REG (*loc);
1497 reg = *loc;
1498 mode = GET_MODE (reg);
1499 if (! REG_P (reg))
1501 if (check_only_p)
1502 return true;
1503 /* Always reload memory in an address even if the target supports
1504 such addresses. */
1505 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, NULL,
1506 "address");
1507 before_p = true;
1509 else
1511 regno = REGNO (reg);
1512 rclass = get_reg_class (regno);
1513 if (! check_only_p
1514 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1516 if (lra_dump_file != NULL)
1518 fprintf (lra_dump_file,
1519 "Changing pseudo %d in address of insn %u on equiv ",
1520 REGNO (reg), INSN_UID (curr_insn));
1521 dump_value_slim (lra_dump_file, *loc, 1);
1522 fprintf (lra_dump_file, "\n");
1524 *loc = copy_rtx (*loc);
1526 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1528 if (check_only_p)
1529 return true;
1530 reg = *loc;
1531 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1532 mode, reg, cl, NULL,
1533 subreg_p, "address", &new_reg))
1534 before_p = true;
1536 else if (new_class != NO_REGS && rclass != new_class)
1538 if (check_only_p)
1539 return true;
1540 lra_change_class (regno, new_class, " Change to", true);
1541 return false;
1543 else
1544 return false;
1546 if (before_p)
1548 push_to_sequence (*before);
1549 lra_emit_move (new_reg, reg);
1550 *before = get_insns ();
1551 end_sequence ();
1553 *loc = new_reg;
1554 if (after != NULL)
1556 start_sequence ();
1557 lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1558 emit_insn (*after);
1559 *after = get_insns ();
1560 end_sequence ();
1562 return true;
1565 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1566 the insn to be inserted before curr insn. AFTER returns the
1567 the insn to be inserted after curr insn. ORIGREG and NEWREG
1568 are the original reg and new reg for reload. */
1569 static void
1570 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1571 rtx newreg)
1573 if (before)
1575 push_to_sequence (*before);
1576 lra_emit_move (newreg, origreg);
1577 *before = get_insns ();
1578 end_sequence ();
1580 if (after)
1582 start_sequence ();
1583 lra_emit_move (origreg, newreg);
1584 emit_insn (*after);
1585 *after = get_insns ();
1586 end_sequence ();
1590 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1591 static bool process_address (int, bool, rtx_insn **, rtx_insn **);
1593 /* Make reloads for subreg in operand NOP with internal subreg mode
1594 REG_MODE, add new reloads for further processing. Return true if
1595 any change was done. */
1596 static bool
1597 simplify_operand_subreg (int nop, machine_mode reg_mode)
1599 int hard_regno, inner_hard_regno;
1600 rtx_insn *before, *after;
1601 machine_mode mode, innermode;
1602 rtx reg, new_reg;
1603 rtx operand = *curr_id->operand_loc[nop];
1604 enum reg_class regclass;
1605 enum op_type type;
1607 before = after = NULL;
1609 if (GET_CODE (operand) != SUBREG)
1610 return false;
1612 mode = GET_MODE (operand);
1613 reg = SUBREG_REG (operand);
1614 innermode = GET_MODE (reg);
1615 type = curr_static_id->operand[nop].type;
1616 if (MEM_P (reg))
1618 const bool addr_was_valid
1619 = valid_address_p (innermode, XEXP (reg, 0), MEM_ADDR_SPACE (reg));
1620 alter_subreg (curr_id->operand_loc[nop], false);
1621 rtx subst = *curr_id->operand_loc[nop];
1622 lra_assert (MEM_P (subst));
1623 const bool addr_is_valid = valid_address_p (GET_MODE (subst),
1624 XEXP (subst, 0),
1625 MEM_ADDR_SPACE (subst));
1626 if (!addr_was_valid
1627 || addr_is_valid
1628 || ((get_constraint_type (lookup_constraint
1629 (curr_static_id->operand[nop].constraint))
1630 != CT_SPECIAL_MEMORY)
1631 /* We still can reload address and if the address is
1632 valid, we can remove subreg without reloading its
1633 inner memory. */
1634 && valid_address_p (GET_MODE (subst),
1635 regno_reg_rtx
1636 [ira_class_hard_regs
1637 [base_reg_class (GET_MODE (subst),
1638 MEM_ADDR_SPACE (subst),
1639 ADDRESS, SCRATCH)][0]],
1640 MEM_ADDR_SPACE (subst))))
1642 /* If we change the address for a paradoxical subreg of memory, the
1643 new address might violate the necessary alignment or the access
1644 might be slow; take this into consideration. We need not worry
1645 about accesses beyond allocated memory for paradoxical memory
1646 subregs as we don't substitute such equiv memory (see processing
1647 equivalences in function lra_constraints) and because for spilled
1648 pseudos we allocate stack memory enough for the biggest
1649 corresponding paradoxical subreg.
1651 However, do not blindly simplify a (subreg (mem ...)) for
1652 WORD_REGISTER_OPERATIONS targets as this may lead to loading junk
1653 data into a register when the inner is narrower than outer or
1654 missing important data from memory when the inner is wider than
1655 outer. This rule only applies to modes that are no wider than
1656 a word.
1658 If valid memory becomes invalid after subreg elimination
1659 and address might be different we still have to reload
1660 memory.
1662 if ((! addr_was_valid
1663 || addr_is_valid
1664 || known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (innermode)))
1665 && !(maybe_ne (GET_MODE_PRECISION (mode),
1666 GET_MODE_PRECISION (innermode))
1667 && known_le (GET_MODE_SIZE (mode), UNITS_PER_WORD)
1668 && known_le (GET_MODE_SIZE (innermode), UNITS_PER_WORD)
1669 && WORD_REGISTER_OPERATIONS)
1670 && (!(MEM_ALIGN (subst) < GET_MODE_ALIGNMENT (mode)
1671 && targetm.slow_unaligned_access (mode, MEM_ALIGN (subst)))
1672 || (MEM_ALIGN (reg) < GET_MODE_ALIGNMENT (innermode)
1673 && targetm.slow_unaligned_access (innermode,
1674 MEM_ALIGN (reg)))))
1675 return true;
1677 *curr_id->operand_loc[nop] = operand;
1679 /* But if the address was not valid, we cannot reload the MEM without
1680 reloading the address first. */
1681 if (!addr_was_valid)
1682 process_address (nop, false, &before, &after);
1684 /* INNERMODE is fast, MODE slow. Reload the mem in INNERMODE. */
1685 enum reg_class rclass
1686 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1687 if (get_reload_reg (curr_static_id->operand[nop].type, innermode,
1688 reg, rclass, NULL,
1689 TRUE, "slow/invalid mem", &new_reg))
1691 bool insert_before, insert_after;
1692 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1694 insert_before = (type != OP_OUT
1695 || partial_subreg_p (mode, innermode));
1696 insert_after = type != OP_IN;
1697 insert_move_for_subreg (insert_before ? &before : NULL,
1698 insert_after ? &after : NULL,
1699 reg, new_reg);
1701 SUBREG_REG (operand) = new_reg;
1703 /* Convert to MODE. */
1704 reg = operand;
1705 rclass
1706 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1707 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1708 rclass, NULL,
1709 TRUE, "slow/invalid mem", &new_reg))
1711 bool insert_before, insert_after;
1712 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1714 insert_before = type != OP_OUT;
1715 insert_after = type != OP_IN;
1716 insert_move_for_subreg (insert_before ? &before : NULL,
1717 insert_after ? &after : NULL,
1718 reg, new_reg);
1720 *curr_id->operand_loc[nop] = new_reg;
1721 lra_process_new_insns (curr_insn, before, after,
1722 "Inserting slow/invalid mem reload");
1723 return true;
1726 /* If the address was valid and became invalid, prefer to reload
1727 the memory. Typical case is when the index scale should
1728 correspond the memory. */
1729 *curr_id->operand_loc[nop] = operand;
1730 /* Do not return false here as the MEM_P (reg) will be processed
1731 later in this function. */
1733 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1735 alter_subreg (curr_id->operand_loc[nop], false);
1736 return true;
1738 else if (CONSTANT_P (reg))
1740 /* Try to simplify subreg of constant. It is usually result of
1741 equivalence substitution. */
1742 if (innermode == VOIDmode
1743 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1744 innermode = curr_static_id->operand[nop].mode;
1745 if ((new_reg = simplify_subreg (mode, reg, innermode,
1746 SUBREG_BYTE (operand))) != NULL_RTX)
1748 *curr_id->operand_loc[nop] = new_reg;
1749 return true;
1752 /* Put constant into memory when we have mixed modes. It generates
1753 a better code in most cases as it does not need a secondary
1754 reload memory. It also prevents LRA looping when LRA is using
1755 secondary reload memory again and again. */
1756 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1757 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1759 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1760 alter_subreg (curr_id->operand_loc[nop], false);
1761 return true;
1763 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1764 if there may be a problem accessing OPERAND in the outer
1765 mode. */
1766 if ((REG_P (reg)
1767 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1768 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1769 /* Don't reload paradoxical subregs because we could be looping
1770 having repeatedly final regno out of hard regs range. */
1771 && (hard_regno_nregs (hard_regno, innermode)
1772 >= hard_regno_nregs (hard_regno, mode))
1773 && simplify_subreg_regno (hard_regno, innermode,
1774 SUBREG_BYTE (operand), mode) < 0
1775 /* Don't reload subreg for matching reload. It is actually
1776 valid subreg in LRA. */
1777 && ! LRA_SUBREG_P (operand))
1778 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1780 enum reg_class rclass;
1782 if (REG_P (reg))
1783 /* There is a big probability that we will get the same class
1784 for the new pseudo and we will get the same insn which
1785 means infinite looping. So spill the new pseudo. */
1786 rclass = NO_REGS;
1787 else
1788 /* The class will be defined later in curr_insn_transform. */
1789 rclass
1790 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1792 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1793 rclass, NULL,
1794 TRUE, "subreg reg", &new_reg))
1796 bool insert_before, insert_after;
1797 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1799 insert_before = (type != OP_OUT
1800 || read_modify_subreg_p (operand));
1801 insert_after = (type != OP_IN);
1802 insert_move_for_subreg (insert_before ? &before : NULL,
1803 insert_after ? &after : NULL,
1804 reg, new_reg);
1806 SUBREG_REG (operand) = new_reg;
1807 lra_process_new_insns (curr_insn, before, after,
1808 "Inserting subreg reload");
1809 return true;
1811 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1812 IRA allocates hardreg to the inner pseudo reg according to its mode
1813 instead of the outermode, so the size of the hardreg may not be enough
1814 to contain the outermode operand, in that case we may need to insert
1815 reload for the reg. For the following two types of paradoxical subreg,
1816 we need to insert reload:
1817 1. If the op_type is OP_IN, and the hardreg could not be paired with
1818 other hardreg to contain the outermode operand
1819 (checked by in_hard_reg_set_p), we need to insert the reload.
1820 2. If the op_type is OP_OUT or OP_INOUT.
1822 Here is a paradoxical subreg example showing how the reload is generated:
1824 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1825 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1827 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1828 here, if reg107 is assigned to hardreg R15, because R15 is the last
1829 hardreg, compiler cannot find another hardreg to pair with R15 to
1830 contain TImode data. So we insert a TImode reload reg180 for it.
1831 After reload is inserted:
1833 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1834 (reg:DI 107 [ __comp ])) -1
1835 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1836 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1838 Two reload hard registers will be allocated to reg180 to save TImode data
1839 in LRA_assign.
1841 For LRA pseudos this should normally be handled by the biggest_mode
1842 mechanism. However, it's possible for new uses of an LRA pseudo
1843 to be introduced after we've allocated it, such as when undoing
1844 inheritance, and the allocated register might not then be appropriate
1845 for the new uses. */
1846 else if (REG_P (reg)
1847 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1848 && paradoxical_subreg_p (operand)
1849 && (inner_hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1850 && ((hard_regno
1851 = simplify_subreg_regno (inner_hard_regno, innermode,
1852 SUBREG_BYTE (operand), mode)) < 0
1853 || ((hard_regno_nregs (inner_hard_regno, innermode)
1854 < hard_regno_nregs (hard_regno, mode))
1855 && (regclass = lra_get_allocno_class (REGNO (reg)))
1856 && (type != OP_IN
1857 || !in_hard_reg_set_p (reg_class_contents[regclass],
1858 mode, hard_regno)
1859 || overlaps_hard_reg_set_p (lra_no_alloc_regs,
1860 mode, hard_regno)))))
1862 /* The class will be defined later in curr_insn_transform. */
1863 enum reg_class rclass
1864 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1866 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1867 rclass, NULL,
1868 TRUE, "paradoxical subreg", &new_reg))
1870 rtx subreg;
1871 bool insert_before, insert_after;
1873 PUT_MODE (new_reg, mode);
1874 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1875 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1877 insert_before = (type != OP_OUT);
1878 insert_after = (type != OP_IN);
1879 insert_move_for_subreg (insert_before ? &before : NULL,
1880 insert_after ? &after : NULL,
1881 reg, subreg);
1883 SUBREG_REG (operand) = new_reg;
1884 lra_process_new_insns (curr_insn, before, after,
1885 "Inserting paradoxical subreg reload");
1886 return true;
1888 return false;
1891 /* Return TRUE if X refers for a hard register from SET. */
1892 static bool
1893 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1895 int i, j, x_hard_regno;
1896 machine_mode mode;
1897 const char *fmt;
1898 enum rtx_code code;
1900 if (x == NULL_RTX)
1901 return false;
1902 code = GET_CODE (x);
1903 mode = GET_MODE (x);
1905 if (code == SUBREG)
1907 /* For all SUBREGs we want to check whether the full multi-register
1908 overlaps the set. For normal SUBREGs this means 'get_hard_regno' of
1909 the inner register, for paradoxical SUBREGs this means the
1910 'get_hard_regno' of the full SUBREG and for complete SUBREGs either is
1911 fine. Use the wider mode for all cases. */
1912 rtx subreg = SUBREG_REG (x);
1913 mode = wider_subreg_mode (x);
1914 if (mode == GET_MODE (subreg))
1916 x = subreg;
1917 code = GET_CODE (x);
1921 if (REG_P (x) || SUBREG_P (x))
1923 x_hard_regno = get_hard_regno (x, true);
1924 return (x_hard_regno >= 0
1925 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1927 fmt = GET_RTX_FORMAT (code);
1928 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1930 if (fmt[i] == 'e')
1932 if (uses_hard_regs_p (XEXP (x, i), set))
1933 return true;
1935 else if (fmt[i] == 'E')
1937 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1938 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1939 return true;
1942 return false;
1945 /* Return true if OP is a spilled pseudo. */
1946 static inline bool
1947 spilled_pseudo_p (rtx op)
1949 return (REG_P (op)
1950 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1953 /* Return true if X is a general constant. */
1954 static inline bool
1955 general_constant_p (rtx x)
1957 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1960 static bool
1961 reg_in_class_p (rtx reg, enum reg_class cl)
1963 if (cl == NO_REGS)
1964 return get_reg_class (REGNO (reg)) == NO_REGS;
1965 return in_class_p (reg, cl, NULL);
1968 /* Return true if SET of RCLASS contains no hard regs which can be
1969 used in MODE. */
1970 static bool
1971 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1972 HARD_REG_SET &set,
1973 machine_mode mode)
1975 HARD_REG_SET temp;
1977 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1978 temp = set & ~lra_no_alloc_regs;
1979 return (hard_reg_set_subset_p
1980 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1984 /* Used to check validity info about small class input operands. It
1985 should be incremented at start of processing an insn
1986 alternative. */
1987 static unsigned int curr_small_class_check = 0;
1989 /* Update number of used inputs of class OP_CLASS for operand NOP
1990 of alternative NALT. Return true if we have more such class operands
1991 than the number of available regs. */
1992 static bool
1993 update_and_check_small_class_inputs (int nop, int nalt,
1994 enum reg_class op_class)
1996 static unsigned int small_class_check[LIM_REG_CLASSES];
1997 static int small_class_input_nums[LIM_REG_CLASSES];
1999 if (SMALL_REGISTER_CLASS_P (op_class)
2000 /* We are interesting in classes became small because of fixing
2001 some hard regs, e.g. by an user through GCC options. */
2002 && hard_reg_set_intersect_p (reg_class_contents[op_class],
2003 ira_no_alloc_regs)
2004 && (curr_static_id->operand[nop].type != OP_OUT
2005 || TEST_BIT (curr_static_id->operand[nop].early_clobber_alts, nalt)))
2007 if (small_class_check[op_class] == curr_small_class_check)
2008 small_class_input_nums[op_class]++;
2009 else
2011 small_class_check[op_class] = curr_small_class_check;
2012 small_class_input_nums[op_class] = 1;
2014 if (small_class_input_nums[op_class] > ira_class_hard_regs_num[op_class])
2015 return true;
2017 return false;
2020 /* Major function to choose the current insn alternative and what
2021 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
2022 negative we should consider only this alternative. Return false if
2023 we cannot choose the alternative or find how to reload the
2024 operands. */
2025 static bool
2026 process_alt_operands (int only_alternative)
2028 bool ok_p = false;
2029 int nop, overall, nalt;
2030 int n_alternatives = curr_static_id->n_alternatives;
2031 int n_operands = curr_static_id->n_operands;
2032 /* LOSERS counts the operands that don't fit this alternative and
2033 would require loading. */
2034 int losers;
2035 int addr_losers;
2036 /* REJECT is a count of how undesirable this alternative says it is
2037 if any reloading is required. If the alternative matches exactly
2038 then REJECT is ignored, but otherwise it gets this much counted
2039 against it in addition to the reloading needed. */
2040 int reject;
2041 /* This is defined by '!' or '?' alternative constraint and added to
2042 reject. But in some cases it can be ignored. */
2043 int static_reject;
2044 int op_reject;
2045 /* The number of elements in the following array. */
2046 int early_clobbered_regs_num;
2047 /* Numbers of operands which are early clobber registers. */
2048 int early_clobbered_nops[MAX_RECOG_OPERANDS];
2049 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
2050 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
2051 HARD_REG_SET curr_alt_exclude_start_hard_regs[MAX_RECOG_OPERANDS];
2052 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
2053 bool curr_alt_win[MAX_RECOG_OPERANDS];
2054 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
2055 int curr_alt_matches[MAX_RECOG_OPERANDS];
2056 /* The number of elements in the following array. */
2057 int curr_alt_dont_inherit_ops_num;
2058 /* Numbers of operands whose reload pseudos should not be inherited. */
2059 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
2060 rtx op;
2061 /* The register when the operand is a subreg of register, otherwise the
2062 operand itself. */
2063 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
2064 /* The register if the operand is a register or subreg of register,
2065 otherwise NULL. */
2066 rtx operand_reg[MAX_RECOG_OPERANDS];
2067 int hard_regno[MAX_RECOG_OPERANDS];
2068 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
2069 int reload_nregs, reload_sum;
2070 bool costly_p;
2071 enum reg_class cl;
2073 /* Calculate some data common for all alternatives to speed up the
2074 function. */
2075 for (nop = 0; nop < n_operands; nop++)
2077 rtx reg;
2079 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
2080 /* The real hard regno of the operand after the allocation. */
2081 hard_regno[nop] = get_hard_regno (op, true);
2083 operand_reg[nop] = reg = op;
2084 biggest_mode[nop] = GET_MODE (op);
2085 if (GET_CODE (op) == SUBREG)
2087 biggest_mode[nop] = wider_subreg_mode (op);
2088 operand_reg[nop] = reg = SUBREG_REG (op);
2090 if (! REG_P (reg))
2091 operand_reg[nop] = NULL_RTX;
2092 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
2093 || ((int) REGNO (reg)
2094 == lra_get_elimination_hard_regno (REGNO (reg))))
2095 no_subreg_reg_operand[nop] = reg;
2096 else
2097 operand_reg[nop] = no_subreg_reg_operand[nop]
2098 /* Just use natural mode for elimination result. It should
2099 be enough for extra constraints hooks. */
2100 = regno_reg_rtx[hard_regno[nop]];
2103 /* The constraints are made of several alternatives. Each operand's
2104 constraint looks like foo,bar,... with commas separating the
2105 alternatives. The first alternatives for all operands go
2106 together, the second alternatives go together, etc.
2108 First loop over alternatives. */
2109 alternative_mask preferred = curr_id->preferred_alternatives;
2110 if (only_alternative >= 0)
2111 preferred &= ALTERNATIVE_BIT (only_alternative);
2113 for (nalt = 0; nalt < n_alternatives; nalt++)
2115 /* Loop over operands for one constraint alternative. */
2116 if (!TEST_BIT (preferred, nalt))
2117 continue;
2119 bool matching_early_clobber[MAX_RECOG_OPERANDS];
2120 curr_small_class_check++;
2121 overall = losers = addr_losers = 0;
2122 static_reject = reject = reload_nregs = reload_sum = 0;
2123 for (nop = 0; nop < n_operands; nop++)
2125 int inc = (curr_static_id
2126 ->operand_alternative[nalt * n_operands + nop].reject);
2127 if (lra_dump_file != NULL && inc != 0)
2128 fprintf (lra_dump_file,
2129 " Staticly defined alt reject+=%d\n", inc);
2130 static_reject += inc;
2131 matching_early_clobber[nop] = 0;
2133 reject += static_reject;
2134 early_clobbered_regs_num = 0;
2136 for (nop = 0; nop < n_operands; nop++)
2138 const char *p;
2139 char *end;
2140 int len, c, m, i, opalt_num, this_alternative_matches;
2141 bool win, did_match, offmemok, early_clobber_p;
2142 /* false => this operand can be reloaded somehow for this
2143 alternative. */
2144 bool badop;
2145 /* true => this operand can be reloaded if the alternative
2146 allows regs. */
2147 bool winreg;
2148 /* True if a constant forced into memory would be OK for
2149 this operand. */
2150 bool constmemok;
2151 enum reg_class this_alternative, this_costly_alternative;
2152 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
2153 HARD_REG_SET this_alternative_exclude_start_hard_regs;
2154 bool this_alternative_match_win, this_alternative_win;
2155 bool this_alternative_offmemok;
2156 bool scratch_p;
2157 machine_mode mode;
2158 enum constraint_num cn;
2160 opalt_num = nalt * n_operands + nop;
2161 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
2163 /* Fast track for no constraints at all. */
2164 curr_alt[nop] = NO_REGS;
2165 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
2166 curr_alt_win[nop] = true;
2167 curr_alt_match_win[nop] = false;
2168 curr_alt_offmemok[nop] = false;
2169 curr_alt_matches[nop] = -1;
2170 continue;
2173 op = no_subreg_reg_operand[nop];
2174 mode = curr_operand_mode[nop];
2176 win = did_match = winreg = offmemok = constmemok = false;
2177 badop = true;
2179 early_clobber_p = false;
2180 p = curr_static_id->operand_alternative[opalt_num].constraint;
2182 this_costly_alternative = this_alternative = NO_REGS;
2183 /* We update set of possible hard regs besides its class
2184 because reg class might be inaccurate. For example,
2185 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
2186 is translated in HI_REGS because classes are merged by
2187 pairs and there is no accurate intermediate class. */
2188 CLEAR_HARD_REG_SET (this_alternative_set);
2189 CLEAR_HARD_REG_SET (this_costly_alternative_set);
2190 CLEAR_HARD_REG_SET (this_alternative_exclude_start_hard_regs);
2191 this_alternative_win = false;
2192 this_alternative_match_win = false;
2193 this_alternative_offmemok = false;
2194 this_alternative_matches = -1;
2196 /* An empty constraint should be excluded by the fast
2197 track. */
2198 lra_assert (*p != 0 && *p != ',');
2200 op_reject = 0;
2201 /* Scan this alternative's specs for this operand; set WIN
2202 if the operand fits any letter in this alternative.
2203 Otherwise, clear BADOP if this operand could fit some
2204 letter after reloads, or set WINREG if this operand could
2205 fit after reloads provided the constraint allows some
2206 registers. */
2207 costly_p = false;
2210 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
2212 case '\0':
2213 len = 0;
2214 break;
2215 case ',':
2216 c = '\0';
2217 break;
2219 case '&':
2220 early_clobber_p = true;
2221 break;
2223 case '$':
2224 op_reject += LRA_MAX_REJECT;
2225 break;
2226 case '^':
2227 op_reject += LRA_LOSER_COST_FACTOR;
2228 break;
2230 case '#':
2231 /* Ignore rest of this alternative. */
2232 c = '\0';
2233 break;
2235 case '0': case '1': case '2': case '3': case '4':
2236 case '5': case '6': case '7': case '8': case '9':
2238 int m_hregno;
2239 bool match_p;
2241 m = strtoul (p, &end, 10);
2242 p = end;
2243 len = 0;
2244 lra_assert (nop > m);
2246 /* Reject matches if we don't know which operand is
2247 bigger. This situation would arguably be a bug in
2248 an .md pattern, but could also occur in a user asm. */
2249 if (!ordered_p (GET_MODE_SIZE (biggest_mode[m]),
2250 GET_MODE_SIZE (biggest_mode[nop])))
2251 break;
2253 /* Don't match wrong asm insn operands for proper
2254 diagnostic later. */
2255 if (INSN_CODE (curr_insn) < 0
2256 && (curr_operand_mode[m] == BLKmode
2257 || curr_operand_mode[nop] == BLKmode)
2258 && curr_operand_mode[m] != curr_operand_mode[nop])
2259 break;
2261 m_hregno = get_hard_regno (*curr_id->operand_loc[m], false);
2262 /* We are supposed to match a previous operand.
2263 If we do, we win if that one did. If we do
2264 not, count both of the operands as losers.
2265 (This is too conservative, since most of the
2266 time only a single reload insn will be needed
2267 to make the two operands win. As a result,
2268 this alternative may be rejected when it is
2269 actually desirable.) */
2270 match_p = false;
2271 if (operands_match_p (*curr_id->operand_loc[nop],
2272 *curr_id->operand_loc[m], m_hregno))
2274 /* We should reject matching of an early
2275 clobber operand if the matching operand is
2276 not dying in the insn. */
2277 if (!TEST_BIT (curr_static_id->operand[m]
2278 .early_clobber_alts, nalt)
2279 || operand_reg[nop] == NULL_RTX
2280 || (find_regno_note (curr_insn, REG_DEAD,
2281 REGNO (op))
2282 || REGNO (op) == REGNO (operand_reg[m])))
2283 match_p = true;
2285 if (match_p)
2287 /* If we are matching a non-offsettable
2288 address where an offsettable address was
2289 expected, then we must reject this
2290 combination, because we can't reload
2291 it. */
2292 if (curr_alt_offmemok[m]
2293 && MEM_P (*curr_id->operand_loc[m])
2294 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
2295 continue;
2297 else
2299 /* If the operands do not match and one
2300 operand is INOUT, we can not match them.
2301 Try other possibilities, e.g. other
2302 alternatives or commutative operand
2303 exchange. */
2304 if (curr_static_id->operand[nop].type == OP_INOUT
2305 || curr_static_id->operand[m].type == OP_INOUT)
2306 break;
2307 /* Operands don't match. If the operands are
2308 different user defined explicit hard
2309 registers, then we cannot make them match
2310 when one is early clobber operand. */
2311 if ((REG_P (*curr_id->operand_loc[nop])
2312 || SUBREG_P (*curr_id->operand_loc[nop]))
2313 && (REG_P (*curr_id->operand_loc[m])
2314 || SUBREG_P (*curr_id->operand_loc[m])))
2316 rtx nop_reg = *curr_id->operand_loc[nop];
2317 if (SUBREG_P (nop_reg))
2318 nop_reg = SUBREG_REG (nop_reg);
2319 rtx m_reg = *curr_id->operand_loc[m];
2320 if (SUBREG_P (m_reg))
2321 m_reg = SUBREG_REG (m_reg);
2323 if (REG_P (nop_reg)
2324 && HARD_REGISTER_P (nop_reg)
2325 && REG_USERVAR_P (nop_reg)
2326 && REG_P (m_reg)
2327 && HARD_REGISTER_P (m_reg)
2328 && REG_USERVAR_P (m_reg))
2330 int i;
2332 for (i = 0; i < early_clobbered_regs_num; i++)
2333 if (m == early_clobbered_nops[i])
2334 break;
2335 if (i < early_clobbered_regs_num
2336 || early_clobber_p)
2337 break;
2340 /* Both operands must allow a reload register,
2341 otherwise we cannot make them match. */
2342 if (curr_alt[m] == NO_REGS)
2343 break;
2344 /* Retroactively mark the operand we had to
2345 match as a loser, if it wasn't already and
2346 it wasn't matched to a register constraint
2347 (e.g it might be matched by memory). */
2348 if (curr_alt_win[m]
2349 && (operand_reg[m] == NULL_RTX
2350 || hard_regno[m] < 0))
2352 losers++;
2353 reload_nregs
2354 += (ira_reg_class_max_nregs[curr_alt[m]]
2355 [GET_MODE (*curr_id->operand_loc[m])]);
2358 /* Prefer matching earlyclobber alternative as
2359 it results in less hard regs required for
2360 the insn than a non-matching earlyclobber
2361 alternative. */
2362 if (TEST_BIT (curr_static_id->operand[m]
2363 .early_clobber_alts, nalt))
2365 if (lra_dump_file != NULL)
2366 fprintf
2367 (lra_dump_file,
2368 " %d Matching earlyclobber alt:"
2369 " reject--\n",
2370 nop);
2371 if (!matching_early_clobber[m])
2373 reject--;
2374 matching_early_clobber[m] = 1;
2377 /* Otherwise we prefer no matching
2378 alternatives because it gives more freedom
2379 in RA. */
2380 else if (operand_reg[nop] == NULL_RTX
2381 || (find_regno_note (curr_insn, REG_DEAD,
2382 REGNO (operand_reg[nop]))
2383 == NULL_RTX))
2385 if (lra_dump_file != NULL)
2386 fprintf
2387 (lra_dump_file,
2388 " %d Matching alt: reject+=2\n",
2389 nop);
2390 reject += 2;
2393 /* If we have to reload this operand and some
2394 previous operand also had to match the same
2395 thing as this operand, we don't know how to do
2396 that. */
2397 if (!match_p || !curr_alt_win[m])
2399 for (i = 0; i < nop; i++)
2400 if (curr_alt_matches[i] == m)
2401 break;
2402 if (i < nop)
2403 break;
2405 else
2406 did_match = true;
2408 this_alternative_matches = m;
2409 /* This can be fixed with reloads if the operand
2410 we are supposed to match can be fixed with
2411 reloads. */
2412 badop = false;
2413 this_alternative = curr_alt[m];
2414 this_alternative_set = curr_alt_set[m];
2415 this_alternative_exclude_start_hard_regs
2416 = curr_alt_exclude_start_hard_regs[m];
2417 winreg = this_alternative != NO_REGS;
2418 break;
2421 case 'g':
2422 if (MEM_P (op)
2423 || general_constant_p (op)
2424 || spilled_pseudo_p (op))
2425 win = true;
2426 cl = GENERAL_REGS;
2427 goto reg;
2429 default:
2430 cn = lookup_constraint (p);
2431 switch (get_constraint_type (cn))
2433 case CT_REGISTER:
2434 cl = reg_class_for_constraint (cn);
2435 if (cl != NO_REGS)
2436 goto reg;
2437 break;
2439 case CT_CONST_INT:
2440 if (CONST_INT_P (op)
2441 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2442 win = true;
2443 break;
2445 case CT_MEMORY:
2446 case CT_RELAXED_MEMORY:
2447 if (MEM_P (op)
2448 && satisfies_memory_constraint_p (op, cn))
2449 win = true;
2450 else if (spilled_pseudo_p (op))
2451 win = true;
2453 /* If we didn't already win, we can reload constants
2454 via force_const_mem or put the pseudo value into
2455 memory, or make other memory by reloading the
2456 address like for 'o'. */
2457 if (CONST_POOL_OK_P (mode, op)
2458 || MEM_P (op) || REG_P (op)
2459 /* We can restore the equiv insn by a
2460 reload. */
2461 || equiv_substition_p[nop])
2462 badop = false;
2463 constmemok = true;
2464 offmemok = true;
2465 break;
2467 case CT_ADDRESS:
2468 /* An asm operand with an address constraint
2469 that doesn't satisfy address_operand has
2470 is_address cleared, so that we don't try to
2471 make a non-address fit. */
2472 if (!curr_static_id->operand[nop].is_address)
2473 break;
2474 /* If we didn't already win, we can reload the address
2475 into a base register. */
2476 if (satisfies_address_constraint_p (op, cn))
2477 win = true;
2478 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2479 ADDRESS, SCRATCH);
2480 badop = false;
2481 goto reg;
2483 case CT_FIXED_FORM:
2484 if (constraint_satisfied_p (op, cn))
2485 win = true;
2486 break;
2488 case CT_SPECIAL_MEMORY:
2489 if (satisfies_memory_constraint_p (op, cn))
2490 win = true;
2491 else if (spilled_pseudo_p (op))
2492 win = true;
2493 break;
2495 break;
2497 reg:
2498 if (mode == BLKmode)
2499 break;
2500 this_alternative = reg_class_subunion[this_alternative][cl];
2501 if (hard_reg_set_subset_p (this_alternative_set,
2502 reg_class_contents[cl]))
2503 this_alternative_exclude_start_hard_regs
2504 = ira_exclude_class_mode_regs[cl][mode];
2505 else if (!hard_reg_set_subset_p (reg_class_contents[cl],
2506 this_alternative_set))
2507 this_alternative_exclude_start_hard_regs
2508 |= ira_exclude_class_mode_regs[cl][mode];
2509 this_alternative_set |= reg_class_contents[cl];
2510 if (costly_p)
2512 this_costly_alternative
2513 = reg_class_subunion[this_costly_alternative][cl];
2514 this_costly_alternative_set |= reg_class_contents[cl];
2516 winreg = true;
2517 if (REG_P (op))
2519 if (hard_regno[nop] >= 0
2520 && in_hard_reg_set_p (this_alternative_set,
2521 mode, hard_regno[nop])
2522 && !TEST_HARD_REG_BIT
2523 (this_alternative_exclude_start_hard_regs,
2524 hard_regno[nop]))
2525 win = true;
2526 else if (hard_regno[nop] < 0
2527 && in_class_p (op, this_alternative, NULL))
2528 win = true;
2530 break;
2532 if (c != ' ' && c != '\t')
2533 costly_p = c == '*';
2535 while ((p += len), c);
2537 scratch_p = (operand_reg[nop] != NULL_RTX
2538 && ira_former_scratch_p (REGNO (operand_reg[nop])));
2539 /* Record which operands fit this alternative. */
2540 if (win)
2542 this_alternative_win = true;
2543 if (operand_reg[nop] != NULL_RTX)
2545 if (hard_regno[nop] >= 0)
2547 if (in_hard_reg_set_p (this_costly_alternative_set,
2548 mode, hard_regno[nop]))
2550 if (lra_dump_file != NULL)
2551 fprintf (lra_dump_file,
2552 " %d Costly set: reject++\n",
2553 nop);
2554 reject++;
2557 else
2559 /* Prefer won reg to spilled pseudo under other
2560 equal conditions for possibe inheritance. */
2561 if (! scratch_p)
2563 if (lra_dump_file != NULL)
2564 fprintf
2565 (lra_dump_file,
2566 " %d Non pseudo reload: reject++\n",
2567 nop);
2568 reject++;
2570 if (in_class_p (operand_reg[nop],
2571 this_costly_alternative, NULL))
2573 if (lra_dump_file != NULL)
2574 fprintf
2575 (lra_dump_file,
2576 " %d Non pseudo costly reload:"
2577 " reject++\n",
2578 nop);
2579 reject++;
2582 /* We simulate the behavior of old reload here.
2583 Although scratches need hard registers and it
2584 might result in spilling other pseudos, no reload
2585 insns are generated for the scratches. So it
2586 might cost something but probably less than old
2587 reload pass believes. */
2588 if (scratch_p)
2590 if (lra_dump_file != NULL)
2591 fprintf (lra_dump_file,
2592 " %d Scratch win: reject+=2\n",
2593 nop);
2594 reject += 2;
2598 else if (did_match)
2599 this_alternative_match_win = true;
2600 else
2602 int const_to_mem = 0;
2603 bool no_regs_p;
2605 reject += op_reject;
2606 /* Never do output reload of stack pointer. It makes
2607 impossible to do elimination when SP is changed in
2608 RTL. */
2609 if (op == stack_pointer_rtx && ! frame_pointer_needed
2610 && curr_static_id->operand[nop].type != OP_IN)
2611 goto fail;
2613 /* If this alternative asks for a specific reg class, see if there
2614 is at least one allocatable register in that class. */
2615 no_regs_p
2616 = (this_alternative == NO_REGS
2617 || (hard_reg_set_subset_p
2618 (reg_class_contents[this_alternative],
2619 lra_no_alloc_regs)));
2621 /* For asms, verify that the class for this alternative is possible
2622 for the mode that is specified. */
2623 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2625 int i;
2626 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2627 if (targetm.hard_regno_mode_ok (i, mode)
2628 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2629 mode, i))
2630 break;
2631 if (i == FIRST_PSEUDO_REGISTER)
2632 winreg = false;
2635 /* If this operand accepts a register, and if the
2636 register class has at least one allocatable register,
2637 then this operand can be reloaded. */
2638 if (winreg && !no_regs_p)
2639 badop = false;
2641 if (badop)
2643 if (lra_dump_file != NULL)
2644 fprintf (lra_dump_file,
2645 " alt=%d: Bad operand -- refuse\n",
2646 nalt);
2647 goto fail;
2650 if (this_alternative != NO_REGS)
2652 HARD_REG_SET available_regs
2653 = (reg_class_contents[this_alternative]
2654 & ~((ira_prohibited_class_mode_regs
2655 [this_alternative][mode])
2656 | lra_no_alloc_regs));
2657 if (hard_reg_set_empty_p (available_regs))
2659 /* There are no hard regs holding a value of given
2660 mode. */
2661 if (offmemok)
2663 this_alternative = NO_REGS;
2664 if (lra_dump_file != NULL)
2665 fprintf (lra_dump_file,
2666 " %d Using memory because of"
2667 " a bad mode: reject+=2\n",
2668 nop);
2669 reject += 2;
2671 else
2673 if (lra_dump_file != NULL)
2674 fprintf (lra_dump_file,
2675 " alt=%d: Wrong mode -- refuse\n",
2676 nalt);
2677 goto fail;
2682 /* If not assigned pseudo has a class which a subset of
2683 required reg class, it is a less costly alternative
2684 as the pseudo still can get a hard reg of necessary
2685 class. */
2686 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2687 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2688 && ira_class_subset_p[this_alternative][cl])
2690 if (lra_dump_file != NULL)
2691 fprintf
2692 (lra_dump_file,
2693 " %d Super set class reg: reject-=3\n", nop);
2694 reject -= 3;
2697 this_alternative_offmemok = offmemok;
2698 if (this_costly_alternative != NO_REGS)
2700 if (lra_dump_file != NULL)
2701 fprintf (lra_dump_file,
2702 " %d Costly loser: reject++\n", nop);
2703 reject++;
2705 /* If the operand is dying, has a matching constraint,
2706 and satisfies constraints of the matched operand
2707 which failed to satisfy the own constraints, most probably
2708 the reload for this operand will be gone. */
2709 if (this_alternative_matches >= 0
2710 && !curr_alt_win[this_alternative_matches]
2711 && REG_P (op)
2712 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2713 && (hard_regno[nop] >= 0
2714 ? in_hard_reg_set_p (this_alternative_set,
2715 mode, hard_regno[nop])
2716 : in_class_p (op, this_alternative, NULL)))
2718 if (lra_dump_file != NULL)
2719 fprintf
2720 (lra_dump_file,
2721 " %d Dying matched operand reload: reject++\n",
2722 nop);
2723 reject++;
2725 else
2727 /* Strict_low_part requires to reload the register
2728 not the sub-register. In this case we should
2729 check that a final reload hard reg can hold the
2730 value mode. */
2731 if (curr_static_id->operand[nop].strict_low
2732 && REG_P (op)
2733 && hard_regno[nop] < 0
2734 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2735 && ira_class_hard_regs_num[this_alternative] > 0
2736 && (!targetm.hard_regno_mode_ok
2737 (ira_class_hard_regs[this_alternative][0],
2738 GET_MODE (*curr_id->operand_loc[nop]))))
2740 if (lra_dump_file != NULL)
2741 fprintf
2742 (lra_dump_file,
2743 " alt=%d: Strict low subreg reload -- refuse\n",
2744 nalt);
2745 goto fail;
2747 losers++;
2749 if (operand_reg[nop] != NULL_RTX
2750 /* Output operands and matched input operands are
2751 not inherited. The following conditions do not
2752 exactly describe the previous statement but they
2753 are pretty close. */
2754 && curr_static_id->operand[nop].type != OP_OUT
2755 && (this_alternative_matches < 0
2756 || curr_static_id->operand[nop].type != OP_IN))
2758 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2759 (operand_reg[nop])]
2760 .last_reload);
2762 /* The value of reload_sum has sense only if we
2763 process insns in their order. It happens only on
2764 the first constraints sub-pass when we do most of
2765 reload work. */
2766 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2767 reload_sum += last_reload - bb_reload_num;
2769 /* If this is a constant that is reloaded into the
2770 desired class by copying it to memory first, count
2771 that as another reload. This is consistent with
2772 other code and is required to avoid choosing another
2773 alternative when the constant is moved into memory.
2774 Note that the test here is precisely the same as in
2775 the code below that calls force_const_mem. */
2776 if (CONST_POOL_OK_P (mode, op)
2777 && ((targetm.preferred_reload_class
2778 (op, this_alternative) == NO_REGS)
2779 || no_input_reloads_p))
2781 const_to_mem = 1;
2782 if (! no_regs_p)
2783 losers++;
2786 /* Alternative loses if it requires a type of reload not
2787 permitted for this insn. We can always reload
2788 objects with a REG_UNUSED note. */
2789 if ((curr_static_id->operand[nop].type != OP_IN
2790 && no_output_reloads_p
2791 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2792 || (curr_static_id->operand[nop].type != OP_OUT
2793 && no_input_reloads_p && ! const_to_mem)
2794 || (this_alternative_matches >= 0
2795 && (no_input_reloads_p
2796 || (no_output_reloads_p
2797 && (curr_static_id->operand
2798 [this_alternative_matches].type != OP_IN)
2799 && ! find_reg_note (curr_insn, REG_UNUSED,
2800 no_subreg_reg_operand
2801 [this_alternative_matches])))))
2803 if (lra_dump_file != NULL)
2804 fprintf
2805 (lra_dump_file,
2806 " alt=%d: No input/output reload -- refuse\n",
2807 nalt);
2808 goto fail;
2811 /* Alternative loses if it required class pseudo cannot
2812 hold value of required mode. Such insns can be
2813 described by insn definitions with mode iterators. */
2814 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2815 && ! hard_reg_set_empty_p (this_alternative_set)
2816 /* It is common practice for constraints to use a
2817 class which does not have actually enough regs to
2818 hold the value (e.g. x86 AREG for mode requiring
2819 more one general reg). Therefore we have 2
2820 conditions to check that the reload pseudo cannot
2821 hold the mode value. */
2822 && (!targetm.hard_regno_mode_ok
2823 (ira_class_hard_regs[this_alternative][0],
2824 GET_MODE (*curr_id->operand_loc[nop])))
2825 /* The above condition is not enough as the first
2826 reg in ira_class_hard_regs can be not aligned for
2827 multi-words mode values. */
2828 && (prohibited_class_reg_set_mode_p
2829 (this_alternative, this_alternative_set,
2830 GET_MODE (*curr_id->operand_loc[nop]))))
2832 if (lra_dump_file != NULL)
2833 fprintf (lra_dump_file,
2834 " alt=%d: reload pseudo for op %d "
2835 "cannot hold the mode value -- refuse\n",
2836 nalt, nop);
2837 goto fail;
2840 /* Check strong discouragement of reload of non-constant
2841 into class THIS_ALTERNATIVE. */
2842 if (! CONSTANT_P (op) && ! no_regs_p
2843 && (targetm.preferred_reload_class
2844 (op, this_alternative) == NO_REGS
2845 || (curr_static_id->operand[nop].type == OP_OUT
2846 && (targetm.preferred_output_reload_class
2847 (op, this_alternative) == NO_REGS))))
2849 if (offmemok && REG_P (op))
2851 if (lra_dump_file != NULL)
2852 fprintf
2853 (lra_dump_file,
2854 " %d Spill pseudo into memory: reject+=3\n",
2855 nop);
2856 reject += 3;
2858 else
2860 if (lra_dump_file != NULL)
2861 fprintf
2862 (lra_dump_file,
2863 " %d Non-prefered reload: reject+=%d\n",
2864 nop, LRA_MAX_REJECT);
2865 reject += LRA_MAX_REJECT;
2869 if (! (MEM_P (op) && offmemok)
2870 && ! (const_to_mem && constmemok))
2872 /* We prefer to reload pseudos over reloading other
2873 things, since such reloads may be able to be
2874 eliminated later. So bump REJECT in other cases.
2875 Don't do this in the case where we are forcing a
2876 constant into memory and it will then win since
2877 we don't want to have a different alternative
2878 match then. */
2879 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2881 if (lra_dump_file != NULL)
2882 fprintf
2883 (lra_dump_file,
2884 " %d Non-pseudo reload: reject+=2\n",
2885 nop);
2886 reject += 2;
2889 if (! no_regs_p)
2890 reload_nregs
2891 += ira_reg_class_max_nregs[this_alternative][mode];
2893 if (SMALL_REGISTER_CLASS_P (this_alternative))
2895 if (lra_dump_file != NULL)
2896 fprintf
2897 (lra_dump_file,
2898 " %d Small class reload: reject+=%d\n",
2899 nop, LRA_LOSER_COST_FACTOR / 2);
2900 reject += LRA_LOSER_COST_FACTOR / 2;
2904 /* We are trying to spill pseudo into memory. It is
2905 usually more costly than moving to a hard register
2906 although it might takes the same number of
2907 reloads.
2909 Non-pseudo spill may happen also. Suppose a target allows both
2910 register and memory in the operand constraint alternatives,
2911 then it's typical that an eliminable register has a substition
2912 of "base + offset" which can either be reloaded by a simple
2913 "new_reg <= base + offset" which will match the register
2914 constraint, or a similar reg addition followed by further spill
2915 to and reload from memory which will match the memory
2916 constraint, but this memory spill will be much more costly
2917 usually.
2919 Code below increases the reject for both pseudo and non-pseudo
2920 spill. */
2921 if (no_regs_p
2922 && !(MEM_P (op) && offmemok)
2923 && !(REG_P (op) && hard_regno[nop] < 0))
2925 if (lra_dump_file != NULL)
2926 fprintf
2927 (lra_dump_file,
2928 " %d Spill %spseudo into memory: reject+=3\n",
2929 nop, REG_P (op) ? "" : "Non-");
2930 reject += 3;
2931 if (VECTOR_MODE_P (mode))
2933 /* Spilling vectors into memory is usually more
2934 costly as they contain big values. */
2935 if (lra_dump_file != NULL)
2936 fprintf
2937 (lra_dump_file,
2938 " %d Spill vector pseudo: reject+=2\n",
2939 nop);
2940 reject += 2;
2944 /* When we use an operand requiring memory in given
2945 alternative, the insn should write *and* read the
2946 value to/from memory it is costly in comparison with
2947 an insn alternative which does not use memory
2948 (e.g. register or immediate operand). We exclude
2949 memory operand for such case as we can satisfy the
2950 memory constraints by reloading address. */
2951 if (no_regs_p && offmemok && !MEM_P (op))
2953 if (lra_dump_file != NULL)
2954 fprintf
2955 (lra_dump_file,
2956 " Using memory insn operand %d: reject+=3\n",
2957 nop);
2958 reject += 3;
2961 /* If reload requires moving value through secondary
2962 memory, it will need one more insn at least. */
2963 if (this_alternative != NO_REGS
2964 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2965 && ((curr_static_id->operand[nop].type != OP_OUT
2966 && targetm.secondary_memory_needed (GET_MODE (op), cl,
2967 this_alternative))
2968 || (curr_static_id->operand[nop].type != OP_IN
2969 && (targetm.secondary_memory_needed
2970 (GET_MODE (op), this_alternative, cl)))))
2971 losers++;
2973 if (MEM_P (op) && offmemok)
2974 addr_losers++;
2975 else
2977 /* Input reloads can be inherited more often than
2978 output reloads can be removed, so penalize output
2979 reloads. */
2980 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2982 if (lra_dump_file != NULL)
2983 fprintf
2984 (lra_dump_file,
2985 " %d Non input pseudo reload: reject++\n",
2986 nop);
2987 reject++;
2990 if (curr_static_id->operand[nop].type == OP_INOUT)
2992 if (lra_dump_file != NULL)
2993 fprintf
2994 (lra_dump_file,
2995 " %d Input/Output reload: reject+=%d\n",
2996 nop, LRA_LOSER_COST_FACTOR);
2997 reject += LRA_LOSER_COST_FACTOR;
3002 if (early_clobber_p && ! scratch_p)
3004 if (lra_dump_file != NULL)
3005 fprintf (lra_dump_file,
3006 " %d Early clobber: reject++\n", nop);
3007 reject++;
3009 /* ??? We check early clobbers after processing all operands
3010 (see loop below) and there we update the costs more.
3011 Should we update the cost (may be approximately) here
3012 because of early clobber register reloads or it is a rare
3013 or non-important thing to be worth to do it. */
3014 overall = (losers * LRA_LOSER_COST_FACTOR + reject
3015 - (addr_losers == losers ? static_reject : 0));
3016 if ((best_losers == 0 || losers != 0) && best_overall < overall)
3018 if (lra_dump_file != NULL)
3019 fprintf (lra_dump_file,
3020 " alt=%d,overall=%d,losers=%d -- refuse\n",
3021 nalt, overall, losers);
3022 goto fail;
3025 if (update_and_check_small_class_inputs (nop, nalt,
3026 this_alternative))
3028 if (lra_dump_file != NULL)
3029 fprintf (lra_dump_file,
3030 " alt=%d, not enough small class regs -- refuse\n",
3031 nalt);
3032 goto fail;
3034 curr_alt[nop] = this_alternative;
3035 curr_alt_set[nop] = this_alternative_set;
3036 curr_alt_exclude_start_hard_regs[nop]
3037 = this_alternative_exclude_start_hard_regs;
3038 curr_alt_win[nop] = this_alternative_win;
3039 curr_alt_match_win[nop] = this_alternative_match_win;
3040 curr_alt_offmemok[nop] = this_alternative_offmemok;
3041 curr_alt_matches[nop] = this_alternative_matches;
3043 if (this_alternative_matches >= 0
3044 && !did_match && !this_alternative_win)
3045 curr_alt_win[this_alternative_matches] = false;
3047 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
3048 early_clobbered_nops[early_clobbered_regs_num++] = nop;
3051 if (curr_insn_set != NULL_RTX && n_operands == 2
3052 /* Prevent processing non-move insns. */
3053 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
3054 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
3055 && ((! curr_alt_win[0] && ! curr_alt_win[1]
3056 && REG_P (no_subreg_reg_operand[0])
3057 && REG_P (no_subreg_reg_operand[1])
3058 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
3059 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
3060 || (! curr_alt_win[0] && curr_alt_win[1]
3061 && REG_P (no_subreg_reg_operand[1])
3062 /* Check that we reload memory not the memory
3063 address. */
3064 && ! (curr_alt_offmemok[0]
3065 && MEM_P (no_subreg_reg_operand[0]))
3066 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
3067 || (curr_alt_win[0] && ! curr_alt_win[1]
3068 && REG_P (no_subreg_reg_operand[0])
3069 /* Check that we reload memory not the memory
3070 address. */
3071 && ! (curr_alt_offmemok[1]
3072 && MEM_P (no_subreg_reg_operand[1]))
3073 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
3074 && (! CONST_POOL_OK_P (curr_operand_mode[1],
3075 no_subreg_reg_operand[1])
3076 || (targetm.preferred_reload_class
3077 (no_subreg_reg_operand[1],
3078 (enum reg_class) curr_alt[1]) != NO_REGS))
3079 /* If it is a result of recent elimination in move
3080 insn we can transform it into an add still by
3081 using this alternative. */
3082 && GET_CODE (no_subreg_reg_operand[1]) != PLUS
3083 /* Likewise if the source has been replaced with an
3084 equivalent value. This only happens once -- the reload
3085 will use the equivalent value instead of the register it
3086 replaces -- so there should be no danger of cycling. */
3087 && !equiv_substition_p[1])))
3089 /* We have a move insn and a new reload insn will be similar
3090 to the current insn. We should avoid such situation as
3091 it results in LRA cycling. */
3092 if (lra_dump_file != NULL)
3093 fprintf (lra_dump_file,
3094 " Cycle danger: overall += LRA_MAX_REJECT\n");
3095 overall += LRA_MAX_REJECT;
3097 ok_p = true;
3098 curr_alt_dont_inherit_ops_num = 0;
3099 for (nop = 0; nop < early_clobbered_regs_num; nop++)
3101 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
3102 HARD_REG_SET temp_set;
3104 i = early_clobbered_nops[nop];
3105 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
3106 || hard_regno[i] < 0)
3107 continue;
3108 lra_assert (operand_reg[i] != NULL_RTX);
3109 clobbered_hard_regno = hard_regno[i];
3110 CLEAR_HARD_REG_SET (temp_set);
3111 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
3112 first_conflict_j = last_conflict_j = -1;
3113 for (j = 0; j < n_operands; j++)
3114 if (j == i
3115 /* We don't want process insides of match_operator and
3116 match_parallel because otherwise we would process
3117 their operands once again generating a wrong
3118 code. */
3119 || curr_static_id->operand[j].is_operator)
3120 continue;
3121 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
3122 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
3123 continue;
3124 /* If we don't reload j-th operand, check conflicts. */
3125 else if ((curr_alt_win[j] || curr_alt_match_win[j])
3126 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
3128 if (first_conflict_j < 0)
3129 first_conflict_j = j;
3130 last_conflict_j = j;
3131 /* Both the earlyclobber operand and conflicting operand
3132 cannot both be user defined hard registers. */
3133 if (HARD_REGISTER_P (operand_reg[i])
3134 && REG_USERVAR_P (operand_reg[i])
3135 && operand_reg[j] != NULL_RTX
3136 && HARD_REGISTER_P (operand_reg[j])
3137 && REG_USERVAR_P (operand_reg[j]))
3139 /* For asm, let curr_insn_transform diagnose it. */
3140 if (INSN_CODE (curr_insn) < 0)
3141 return false;
3142 fatal_insn ("unable to generate reloads for "
3143 "impossible constraints:", curr_insn);
3146 if (last_conflict_j < 0)
3147 continue;
3149 /* If an earlyclobber operand conflicts with another non-matching
3150 operand (ie, they have been assigned the same hard register),
3151 then it is better to reload the other operand, as there may
3152 exist yet another operand with a matching constraint associated
3153 with the earlyclobber operand. However, if one of the operands
3154 is an explicit use of a hard register, then we must reload the
3155 other non-hard register operand. */
3156 if (HARD_REGISTER_P (operand_reg[i])
3157 || (first_conflict_j == last_conflict_j
3158 && operand_reg[last_conflict_j] != NULL_RTX
3159 && !curr_alt_match_win[last_conflict_j]
3160 && !HARD_REGISTER_P (operand_reg[last_conflict_j])))
3162 curr_alt_win[last_conflict_j] = false;
3163 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
3164 = last_conflict_j;
3165 losers++;
3166 if (lra_dump_file != NULL)
3167 fprintf
3168 (lra_dump_file,
3169 " %d Conflict early clobber reload: reject--\n",
3172 else
3174 /* We need to reload early clobbered register and the
3175 matched registers. */
3176 for (j = 0; j < n_operands; j++)
3177 if (curr_alt_matches[j] == i)
3179 curr_alt_match_win[j] = false;
3180 losers++;
3181 overall += LRA_LOSER_COST_FACTOR;
3183 if (! curr_alt_match_win[i])
3184 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
3185 else
3187 /* Remember pseudos used for match reloads are never
3188 inherited. */
3189 lra_assert (curr_alt_matches[i] >= 0);
3190 curr_alt_win[curr_alt_matches[i]] = false;
3192 curr_alt_win[i] = curr_alt_match_win[i] = false;
3193 losers++;
3194 if (lra_dump_file != NULL)
3195 fprintf
3196 (lra_dump_file,
3197 " %d Matched conflict early clobber reloads: "
3198 "reject--\n",
3201 /* Early clobber was already reflected in REJECT. */
3202 if (!matching_early_clobber[i])
3204 lra_assert (reject > 0);
3205 reject--;
3206 matching_early_clobber[i] = 1;
3208 overall += LRA_LOSER_COST_FACTOR - 1;
3210 if (lra_dump_file != NULL)
3211 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
3212 nalt, overall, losers, reload_nregs);
3214 /* If this alternative can be made to work by reloading, and it
3215 needs less reloading than the others checked so far, record
3216 it as the chosen goal for reloading. */
3217 if ((best_losers != 0 && losers == 0)
3218 || (((best_losers == 0 && losers == 0)
3219 || (best_losers != 0 && losers != 0))
3220 && (best_overall > overall
3221 || (best_overall == overall
3222 /* If the cost of the reloads is the same,
3223 prefer alternative which requires minimal
3224 number of reload regs. */
3225 && (reload_nregs < best_reload_nregs
3226 || (reload_nregs == best_reload_nregs
3227 && (best_reload_sum < reload_sum
3228 || (best_reload_sum == reload_sum
3229 && nalt < goal_alt_number))))))))
3231 for (nop = 0; nop < n_operands; nop++)
3233 goal_alt_win[nop] = curr_alt_win[nop];
3234 goal_alt_match_win[nop] = curr_alt_match_win[nop];
3235 goal_alt_matches[nop] = curr_alt_matches[nop];
3236 goal_alt[nop] = curr_alt[nop];
3237 goal_alt_exclude_start_hard_regs[nop]
3238 = curr_alt_exclude_start_hard_regs[nop];
3239 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
3241 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
3242 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
3243 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
3244 goal_alt_swapped = curr_swapped;
3245 best_overall = overall;
3246 best_losers = losers;
3247 best_reload_nregs = reload_nregs;
3248 best_reload_sum = reload_sum;
3249 goal_alt_number = nalt;
3251 if (losers == 0)
3252 /* Everything is satisfied. Do not process alternatives
3253 anymore. */
3254 break;
3255 fail:
3258 return ok_p;
3261 /* Make reload base reg from address AD. */
3262 static rtx
3263 base_to_reg (struct address_info *ad)
3265 enum reg_class cl;
3266 int code = -1;
3267 rtx new_inner = NULL_RTX;
3268 rtx new_reg = NULL_RTX;
3269 rtx_insn *insn;
3270 rtx_insn *last_insn = get_last_insn();
3272 lra_assert (ad->disp == ad->disp_term);
3273 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
3274 get_index_code (ad));
3275 new_reg = lra_create_new_reg (GET_MODE (*ad->base), NULL_RTX, cl, NULL,
3276 "base");
3277 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
3278 ad->disp_term == NULL
3279 ? const0_rtx
3280 : *ad->disp_term);
3281 if (!valid_address_p (ad->mode, new_inner, ad->as))
3282 return NULL_RTX;
3283 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base));
3284 code = recog_memoized (insn);
3285 if (code < 0)
3287 delete_insns_since (last_insn);
3288 return NULL_RTX;
3291 return new_inner;
3294 /* Make reload base reg + DISP from address AD. Return the new pseudo. */
3295 static rtx
3296 base_plus_disp_to_reg (struct address_info *ad, rtx disp)
3298 enum reg_class cl;
3299 rtx new_reg;
3301 lra_assert (ad->base == ad->base_term);
3302 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
3303 get_index_code (ad));
3304 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX, cl, NULL,
3305 "base + disp");
3306 lra_emit_add (new_reg, *ad->base_term, disp);
3307 return new_reg;
3310 /* Make reload of index part of address AD. Return the new
3311 pseudo. */
3312 static rtx
3313 index_part_to_reg (struct address_info *ad)
3315 rtx new_reg;
3317 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
3318 INDEX_REG_CLASS, NULL, "index term");
3319 expand_mult (GET_MODE (*ad->index), *ad->index_term,
3320 GEN_INT (get_index_scale (ad)), new_reg, 1);
3321 return new_reg;
3324 /* Return true if we can add a displacement to address AD, even if that
3325 makes the address invalid. The fix-up code requires any new address
3326 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
3327 static bool
3328 can_add_disp_p (struct address_info *ad)
3330 return (!ad->autoinc_p
3331 && ad->segment == NULL
3332 && ad->base == ad->base_term
3333 && ad->disp == ad->disp_term);
3336 /* Make equiv substitution in address AD. Return true if a substitution
3337 was made. */
3338 static bool
3339 equiv_address_substitution (struct address_info *ad)
3341 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
3342 poly_int64 disp;
3343 HOST_WIDE_INT scale;
3344 bool change_p;
3346 base_term = strip_subreg (ad->base_term);
3347 if (base_term == NULL)
3348 base_reg = new_base_reg = NULL_RTX;
3349 else
3351 base_reg = *base_term;
3352 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
3354 index_term = strip_subreg (ad->index_term);
3355 if (index_term == NULL)
3356 index_reg = new_index_reg = NULL_RTX;
3357 else
3359 index_reg = *index_term;
3360 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
3362 if (base_reg == new_base_reg && index_reg == new_index_reg)
3363 return false;
3364 disp = 0;
3365 change_p = false;
3366 if (lra_dump_file != NULL)
3368 fprintf (lra_dump_file, "Changing address in insn %d ",
3369 INSN_UID (curr_insn));
3370 dump_value_slim (lra_dump_file, *ad->outer, 1);
3372 if (base_reg != new_base_reg)
3374 poly_int64 offset;
3375 if (REG_P (new_base_reg))
3377 *base_term = new_base_reg;
3378 change_p = true;
3380 else if (GET_CODE (new_base_reg) == PLUS
3381 && REG_P (XEXP (new_base_reg, 0))
3382 && poly_int_rtx_p (XEXP (new_base_reg, 1), &offset)
3383 && can_add_disp_p (ad))
3385 disp += offset;
3386 *base_term = XEXP (new_base_reg, 0);
3387 change_p = true;
3389 if (ad->base_term2 != NULL)
3390 *ad->base_term2 = *ad->base_term;
3392 if (index_reg != new_index_reg)
3394 poly_int64 offset;
3395 if (REG_P (new_index_reg))
3397 *index_term = new_index_reg;
3398 change_p = true;
3400 else if (GET_CODE (new_index_reg) == PLUS
3401 && REG_P (XEXP (new_index_reg, 0))
3402 && poly_int_rtx_p (XEXP (new_index_reg, 1), &offset)
3403 && can_add_disp_p (ad)
3404 && (scale = get_index_scale (ad)))
3406 disp += offset * scale;
3407 *index_term = XEXP (new_index_reg, 0);
3408 change_p = true;
3411 if (maybe_ne (disp, 0))
3413 if (ad->disp != NULL)
3414 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
3415 else
3417 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
3418 update_address (ad);
3420 change_p = true;
3422 if (lra_dump_file != NULL)
3424 if (! change_p)
3425 fprintf (lra_dump_file, " -- no change\n");
3426 else
3428 fprintf (lra_dump_file, " on equiv ");
3429 dump_value_slim (lra_dump_file, *ad->outer, 1);
3430 fprintf (lra_dump_file, "\n");
3433 return change_p;
3436 /* Skip all modifiers and whitespaces in constraint STR and return the
3437 result. */
3438 static const char *
3439 skip_constraint_modifiers (const char *str)
3441 for (;;str++)
3442 switch (*str)
3444 case '+': case '&' : case '=': case '*': case ' ': case '\t':
3445 case '$': case '^' : case '%': case '?': case '!':
3446 break;
3447 default: return str;
3451 /* Major function to make reloads for an address in operand NOP or
3452 check its correctness (If CHECK_ONLY_P is true). The supported
3453 cases are:
3455 1) an address that existed before LRA started, at which point it
3456 must have been valid. These addresses are subject to elimination
3457 and may have become invalid due to the elimination offset being out
3458 of range.
3460 2) an address created by forcing a constant to memory
3461 (force_const_to_mem). The initial form of these addresses might
3462 not be valid, and it is this function's job to make them valid.
3464 3) a frame address formed from a register and a (possibly zero)
3465 constant offset. As above, these addresses might not be valid and
3466 this function must make them so.
3468 Add reloads to the lists *BEFORE and *AFTER. We might need to add
3469 reloads to *AFTER because of inc/dec, {pre, post} modify in the
3470 address. Return true for any RTL change.
3472 The function is a helper function which does not produce all
3473 transformations (when CHECK_ONLY_P is false) which can be
3474 necessary. It does just basic steps. To do all necessary
3475 transformations use function process_address. */
3476 static bool
3477 process_address_1 (int nop, bool check_only_p,
3478 rtx_insn **before, rtx_insn **after)
3480 struct address_info ad;
3481 rtx new_reg;
3482 HOST_WIDE_INT scale;
3483 rtx op = *curr_id->operand_loc[nop];
3484 rtx mem = extract_mem_from_operand (op);
3485 const char *constraint;
3486 enum constraint_num cn;
3487 bool change_p = false;
3489 if (MEM_P (mem)
3490 && GET_MODE (mem) == BLKmode
3491 && GET_CODE (XEXP (mem, 0)) == SCRATCH)
3492 return false;
3494 constraint
3495 = skip_constraint_modifiers (curr_static_id->operand[nop].constraint);
3496 if (IN_RANGE (constraint[0], '0', '9'))
3498 char *end;
3499 unsigned long dup = strtoul (constraint, &end, 10);
3500 constraint
3501 = skip_constraint_modifiers (curr_static_id->operand[dup].constraint);
3503 cn = lookup_constraint (*constraint == '\0' ? "X" : constraint);
3504 /* If we have several alternatives or/and several constraints in an
3505 alternative and we can not say at this stage what constraint will be used,
3506 use unknown constraint. The exception is an address constraint. If
3507 operand has one address constraint, probably all others constraints are
3508 address ones. */
3509 if (constraint[0] != '\0' && get_constraint_type (cn) != CT_ADDRESS
3510 && *skip_constraint_modifiers (constraint
3511 + CONSTRAINT_LEN (constraint[0],
3512 constraint)) != '\0')
3513 cn = CONSTRAINT__UNKNOWN;
3514 if (insn_extra_address_constraint (cn)
3515 /* When we find an asm operand with an address constraint that
3516 doesn't satisfy address_operand to begin with, we clear
3517 is_address, so that we don't try to make a non-address fit.
3518 If the asm statement got this far, it's because other
3519 constraints are available, and we'll use them, disregarding
3520 the unsatisfiable address ones. */
3521 && curr_static_id->operand[nop].is_address)
3522 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
3523 /* Do not attempt to decompose arbitrary addresses generated by combine
3524 for asm operands with loose constraints, e.g 'X'.
3525 Need to extract memory from op for special memory constraint,
3526 i.e. bcst_mem_operand in i386 backend. */
3527 else if (MEM_P (mem)
3528 && !(INSN_CODE (curr_insn) < 0
3529 && get_constraint_type (cn) == CT_FIXED_FORM
3530 && constraint_satisfied_p (op, cn)))
3531 decompose_mem_address (&ad, mem);
3532 else if (GET_CODE (op) == SUBREG
3533 && MEM_P (SUBREG_REG (op)))
3534 decompose_mem_address (&ad, SUBREG_REG (op));
3535 else
3536 return false;
3537 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
3538 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
3539 when INDEX_REG_CLASS is a single register class. */
3540 if (ad.base_term != NULL
3541 && ad.index_term != NULL
3542 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
3543 && REG_P (*ad.base_term)
3544 && REG_P (*ad.index_term)
3545 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
3546 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
3548 std::swap (ad.base, ad.index);
3549 std::swap (ad.base_term, ad.index_term);
3551 if (! check_only_p)
3552 change_p = equiv_address_substitution (&ad);
3553 if (ad.base_term != NULL
3554 && (process_addr_reg
3555 (ad.base_term, check_only_p, before,
3556 (ad.autoinc_p
3557 && !(REG_P (*ad.base_term)
3558 && find_regno_note (curr_insn, REG_DEAD,
3559 REGNO (*ad.base_term)) != NULL_RTX)
3560 ? after : NULL),
3561 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3562 get_index_code (&ad)))))
3564 change_p = true;
3565 if (ad.base_term2 != NULL)
3566 *ad.base_term2 = *ad.base_term;
3568 if (ad.index_term != NULL
3569 && process_addr_reg (ad.index_term, check_only_p,
3570 before, NULL, INDEX_REG_CLASS))
3571 change_p = true;
3573 /* Target hooks sometimes don't treat extra-constraint addresses as
3574 legitimate address_operands, so handle them specially. */
3575 if (insn_extra_address_constraint (cn)
3576 && satisfies_address_constraint_p (&ad, cn))
3577 return change_p;
3579 if (check_only_p)
3580 return change_p;
3582 /* There are three cases where the shape of *AD.INNER may now be invalid:
3584 1) the original address was valid, but either elimination or
3585 equiv_address_substitution was applied and that made
3586 the address invalid.
3588 2) the address is an invalid symbolic address created by
3589 force_const_to_mem.
3591 3) the address is a frame address with an invalid offset.
3593 4) the address is a frame address with an invalid base.
3595 All these cases involve a non-autoinc address, so there is no
3596 point revalidating other types. */
3597 if (ad.autoinc_p || valid_address_p (op, &ad, cn))
3598 return change_p;
3600 /* Any index existed before LRA started, so we can assume that the
3601 presence and shape of the index is valid. */
3602 push_to_sequence (*before);
3603 lra_assert (ad.disp == ad.disp_term);
3604 if (ad.base == NULL)
3606 if (ad.index == NULL)
3608 rtx_insn *insn;
3609 rtx_insn *last = get_last_insn ();
3610 int code = -1;
3611 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3612 SCRATCH, SCRATCH);
3613 rtx addr = *ad.inner;
3615 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, NULL, "addr");
3616 if (HAVE_lo_sum)
3618 /* addr => lo_sum (new_base, addr), case (2) above. */
3619 insn = emit_insn (gen_rtx_SET
3620 (new_reg,
3621 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
3622 code = recog_memoized (insn);
3623 if (code >= 0)
3625 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
3626 if (!valid_address_p (op, &ad, cn))
3628 /* Try to put lo_sum into register. */
3629 insn = emit_insn (gen_rtx_SET
3630 (new_reg,
3631 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
3632 code = recog_memoized (insn);
3633 if (code >= 0)
3635 *ad.inner = new_reg;
3636 if (!valid_address_p (op, &ad, cn))
3638 *ad.inner = addr;
3639 code = -1;
3645 if (code < 0)
3646 delete_insns_since (last);
3649 if (code < 0)
3651 /* addr => new_base, case (2) above. */
3652 lra_emit_move (new_reg, addr);
3654 for (insn = last == NULL_RTX ? get_insns () : NEXT_INSN (last);
3655 insn != NULL_RTX;
3656 insn = NEXT_INSN (insn))
3657 if (recog_memoized (insn) < 0)
3658 break;
3659 if (insn != NULL_RTX)
3661 /* Do nothing if we cannot generate right insns.
3662 This is analogous to reload pass behavior. */
3663 delete_insns_since (last);
3664 end_sequence ();
3665 return false;
3667 *ad.inner = new_reg;
3670 else
3672 /* index * scale + disp => new base + index * scale,
3673 case (1) above. */
3674 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3675 GET_CODE (*ad.index));
3677 lra_assert (INDEX_REG_CLASS != NO_REGS);
3678 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, NULL, "disp");
3679 lra_emit_move (new_reg, *ad.disp);
3680 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3681 new_reg, *ad.index);
3684 else if (ad.index == NULL)
3686 int regno;
3687 enum reg_class cl;
3688 rtx set;
3689 rtx_insn *insns, *last_insn;
3690 /* Try to reload base into register only if the base is invalid
3691 for the address but with valid offset, case (4) above. */
3692 start_sequence ();
3693 new_reg = base_to_reg (&ad);
3695 /* base + disp => new base, cases (1) and (3) above. */
3696 /* Another option would be to reload the displacement into an
3697 index register. However, postreload has code to optimize
3698 address reloads that have the same base and different
3699 displacements, so reloading into an index register would
3700 not necessarily be a win. */
3701 if (new_reg == NULL_RTX)
3703 /* See if the target can split the displacement into a
3704 legitimate new displacement from a local anchor. */
3705 gcc_assert (ad.disp == ad.disp_term);
3706 poly_int64 orig_offset;
3707 rtx offset1, offset2;
3708 if (poly_int_rtx_p (*ad.disp, &orig_offset)
3709 && targetm.legitimize_address_displacement (&offset1, &offset2,
3710 orig_offset,
3711 ad.mode))
3713 new_reg = base_plus_disp_to_reg (&ad, offset1);
3714 new_reg = gen_rtx_PLUS (GET_MODE (new_reg), new_reg, offset2);
3716 else
3717 new_reg = base_plus_disp_to_reg (&ad, *ad.disp);
3719 insns = get_insns ();
3720 last_insn = get_last_insn ();
3721 /* If we generated at least two insns, try last insn source as
3722 an address. If we succeed, we generate one less insn. */
3723 if (REG_P (new_reg)
3724 && last_insn != insns
3725 && (set = single_set (last_insn)) != NULL_RTX
3726 && GET_CODE (SET_SRC (set)) == PLUS
3727 && REG_P (XEXP (SET_SRC (set), 0))
3728 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3730 *ad.inner = SET_SRC (set);
3731 if (valid_address_p (op, &ad, cn))
3733 *ad.base_term = XEXP (SET_SRC (set), 0);
3734 *ad.disp_term = XEXP (SET_SRC (set), 1);
3735 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3736 get_index_code (&ad));
3737 regno = REGNO (*ad.base_term);
3738 if (regno >= FIRST_PSEUDO_REGISTER
3739 && cl != lra_get_allocno_class (regno))
3740 lra_change_class (regno, cl, " Change to", true);
3741 new_reg = SET_SRC (set);
3742 delete_insns_since (PREV_INSN (last_insn));
3745 end_sequence ();
3746 emit_insn (insns);
3747 *ad.inner = new_reg;
3749 else if (ad.disp_term != NULL)
3751 /* base + scale * index + disp => new base + scale * index,
3752 case (1) above. */
3753 gcc_assert (ad.disp == ad.disp_term);
3754 new_reg = base_plus_disp_to_reg (&ad, *ad.disp);
3755 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3756 new_reg, *ad.index);
3758 else if ((scale = get_index_scale (&ad)) == 1)
3760 /* The last transformation to one reg will be made in
3761 curr_insn_transform function. */
3762 end_sequence ();
3763 return false;
3765 else if (scale != 0)
3767 /* base + scale * index => base + new_reg,
3768 case (1) above.
3769 Index part of address may become invalid. For example, we
3770 changed pseudo on the equivalent memory and a subreg of the
3771 pseudo onto the memory of different mode for which the scale is
3772 prohibitted. */
3773 new_reg = index_part_to_reg (&ad);
3774 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3775 *ad.base_term, new_reg);
3777 else
3779 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3780 SCRATCH, SCRATCH);
3781 rtx addr = *ad.inner;
3783 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, NULL, "addr");
3784 /* addr => new_base. */
3785 lra_emit_move (new_reg, addr);
3786 *ad.inner = new_reg;
3788 *before = get_insns ();
3789 end_sequence ();
3790 return true;
3793 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3794 Use process_address_1 as a helper function. Return true for any
3795 RTL changes.
3797 If CHECK_ONLY_P is true, just check address correctness. Return
3798 false if the address correct. */
3799 static bool
3800 process_address (int nop, bool check_only_p,
3801 rtx_insn **before, rtx_insn **after)
3803 bool res = false;
3805 while (process_address_1 (nop, check_only_p, before, after))
3807 if (check_only_p)
3808 return true;
3809 res = true;
3811 return res;
3814 /* Emit insns to reload VALUE into a new register. VALUE is an
3815 auto-increment or auto-decrement RTX whose operand is a register or
3816 memory location; so reloading involves incrementing that location.
3817 IN is either identical to VALUE, or some cheaper place to reload
3818 value being incremented/decremented from.
3820 INC_AMOUNT is the number to increment or decrement by (always
3821 positive and ignored for POST_MODIFY/PRE_MODIFY).
3823 Return pseudo containing the result. */
3824 static rtx
3825 emit_inc (enum reg_class new_rclass, rtx in, rtx value, poly_int64 inc_amount)
3827 /* REG or MEM to be copied and incremented. */
3828 rtx incloc = XEXP (value, 0);
3829 /* Nonzero if increment after copying. */
3830 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3831 || GET_CODE (value) == POST_MODIFY);
3832 rtx_insn *last;
3833 rtx inc;
3834 rtx_insn *add_insn;
3835 int code;
3836 rtx real_in = in == value ? incloc : in;
3837 rtx result;
3838 bool plus_p = true;
3840 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3842 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3843 || GET_CODE (XEXP (value, 1)) == MINUS);
3844 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3845 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3846 inc = XEXP (XEXP (value, 1), 1);
3848 else
3850 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3851 inc_amount = -inc_amount;
3853 inc = gen_int_mode (inc_amount, GET_MODE (value));
3856 if (! post && REG_P (incloc))
3857 result = incloc;
3858 else
3859 result = lra_create_new_reg (GET_MODE (value), value, new_rclass, NULL,
3860 "INC/DEC result");
3862 if (real_in != result)
3864 /* First copy the location to the result register. */
3865 lra_assert (REG_P (result));
3866 emit_insn (gen_move_insn (result, real_in));
3869 /* We suppose that there are insns to add/sub with the constant
3870 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3871 old reload worked with this assumption. If the assumption
3872 becomes wrong, we should use approach in function
3873 base_plus_disp_to_reg. */
3874 if (in == value)
3876 /* See if we can directly increment INCLOC. */
3877 last = get_last_insn ();
3878 add_insn = emit_insn (plus_p
3879 ? gen_add2_insn (incloc, inc)
3880 : gen_sub2_insn (incloc, inc));
3882 code = recog_memoized (add_insn);
3883 if (code >= 0)
3885 if (! post && result != incloc)
3886 emit_insn (gen_move_insn (result, incloc));
3887 return result;
3889 delete_insns_since (last);
3892 /* If couldn't do the increment directly, must increment in RESULT.
3893 The way we do this depends on whether this is pre- or
3894 post-increment. For pre-increment, copy INCLOC to the reload
3895 register, increment it there, then save back. */
3896 if (! post)
3898 if (real_in != result)
3899 emit_insn (gen_move_insn (result, real_in));
3900 if (plus_p)
3901 emit_insn (gen_add2_insn (result, inc));
3902 else
3903 emit_insn (gen_sub2_insn (result, inc));
3904 if (result != incloc)
3905 emit_insn (gen_move_insn (incloc, result));
3907 else
3909 /* Post-increment.
3911 Because this might be a jump insn or a compare, and because
3912 RESULT may not be available after the insn in an input
3913 reload, we must do the incrementing before the insn being
3914 reloaded for.
3916 We have already copied IN to RESULT. Increment the copy in
3917 RESULT, save that back, then decrement RESULT so it has
3918 the original value. */
3919 if (plus_p)
3920 emit_insn (gen_add2_insn (result, inc));
3921 else
3922 emit_insn (gen_sub2_insn (result, inc));
3923 emit_insn (gen_move_insn (incloc, result));
3924 /* Restore non-modified value for the result. We prefer this
3925 way because it does not require an additional hard
3926 register. */
3927 if (plus_p)
3929 poly_int64 offset;
3930 if (poly_int_rtx_p (inc, &offset))
3931 emit_insn (gen_add2_insn (result,
3932 gen_int_mode (-offset,
3933 GET_MODE (result))));
3934 else
3935 emit_insn (gen_sub2_insn (result, inc));
3937 else
3938 emit_insn (gen_add2_insn (result, inc));
3940 return result;
3943 /* Return true if the current move insn does not need processing as we
3944 already know that it satisfies its constraints. */
3945 static bool
3946 simple_move_p (void)
3948 rtx dest, src;
3949 enum reg_class dclass, sclass;
3951 lra_assert (curr_insn_set != NULL_RTX);
3952 dest = SET_DEST (curr_insn_set);
3953 src = SET_SRC (curr_insn_set);
3955 /* If the instruction has multiple sets we need to process it even if it
3956 is single_set. This can happen if one or more of the SETs are dead.
3957 See PR73650. */
3958 if (multiple_sets (curr_insn))
3959 return false;
3961 return ((dclass = get_op_class (dest)) != NO_REGS
3962 && (sclass = get_op_class (src)) != NO_REGS
3963 /* The backend guarantees that register moves of cost 2
3964 never need reloads. */
3965 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3968 /* Swap operands NOP and NOP + 1. */
3969 static inline void
3970 swap_operands (int nop)
3972 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3973 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3974 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3975 std::swap (equiv_substition_p[nop], equiv_substition_p[nop + 1]);
3976 /* Swap the duplicates too. */
3977 lra_update_dup (curr_id, nop);
3978 lra_update_dup (curr_id, nop + 1);
3981 /* Main entry point of the constraint code: search the body of the
3982 current insn to choose the best alternative. It is mimicking insn
3983 alternative cost calculation model of former reload pass. That is
3984 because machine descriptions were written to use this model. This
3985 model can be changed in future. Make commutative operand exchange
3986 if it is chosen.
3988 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3989 constraints. Return true if any change happened during function
3990 call.
3992 If CHECK_ONLY_P is true then don't do any transformation. Just
3993 check that the insn satisfies all constraints. If the insn does
3994 not satisfy any constraint, return true. */
3995 static bool
3996 curr_insn_transform (bool check_only_p)
3998 int i, j, k;
3999 int n_operands;
4000 int n_alternatives;
4001 int n_outputs;
4002 int commutative;
4003 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
4004 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
4005 signed char outputs[MAX_RECOG_OPERANDS + 1];
4006 rtx_insn *before, *after;
4007 bool alt_p = false;
4008 /* Flag that the insn has been changed through a transformation. */
4009 bool change_p;
4010 bool sec_mem_p;
4011 bool use_sec_mem_p;
4012 int max_regno_before;
4013 int reused_alternative_num;
4015 curr_insn_set = single_set (curr_insn);
4016 if (curr_insn_set != NULL_RTX && simple_move_p ())
4018 /* We assume that the corresponding insn alternative has no
4019 earlier clobbers. If it is not the case, don't define move
4020 cost equal to 2 for the corresponding register classes. */
4021 lra_set_used_insn_alternative (curr_insn, LRA_NON_CLOBBERED_ALT);
4022 return false;
4025 no_input_reloads_p = no_output_reloads_p = false;
4026 goal_alt_number = -1;
4027 change_p = sec_mem_p = false;
4029 /* CALL_INSNs are not allowed to have any output reloads. */
4030 if (CALL_P (curr_insn))
4031 no_output_reloads_p = true;
4033 n_operands = curr_static_id->n_operands;
4034 n_alternatives = curr_static_id->n_alternatives;
4036 /* Just return "no reloads" if insn has no operands with
4037 constraints. */
4038 if (n_operands == 0 || n_alternatives == 0)
4039 return false;
4041 max_regno_before = max_reg_num ();
4043 for (i = 0; i < n_operands; i++)
4045 goal_alt_matched[i][0] = -1;
4046 goal_alt_matches[i] = -1;
4049 commutative = curr_static_id->commutative;
4051 /* Now see what we need for pseudos that didn't get hard regs or got
4052 the wrong kind of hard reg. For this, we must consider all the
4053 operands together against the register constraints. */
4055 best_losers = best_overall = INT_MAX;
4056 best_reload_sum = 0;
4058 curr_swapped = false;
4059 goal_alt_swapped = false;
4061 if (! check_only_p)
4062 /* Make equivalence substitution and memory subreg elimination
4063 before address processing because an address legitimacy can
4064 depend on memory mode. */
4065 for (i = 0; i < n_operands; i++)
4067 rtx op, subst, old;
4068 bool op_change_p = false;
4070 if (curr_static_id->operand[i].is_operator)
4071 continue;
4073 old = op = *curr_id->operand_loc[i];
4074 if (GET_CODE (old) == SUBREG)
4075 old = SUBREG_REG (old);
4076 subst = get_equiv_with_elimination (old, curr_insn);
4077 original_subreg_reg_mode[i] = VOIDmode;
4078 equiv_substition_p[i] = false;
4079 if (subst != old)
4081 equiv_substition_p[i] = true;
4082 subst = copy_rtx (subst);
4083 lra_assert (REG_P (old));
4084 if (GET_CODE (op) != SUBREG)
4085 *curr_id->operand_loc[i] = subst;
4086 else
4088 SUBREG_REG (op) = subst;
4089 if (GET_MODE (subst) == VOIDmode)
4090 original_subreg_reg_mode[i] = GET_MODE (old);
4092 if (lra_dump_file != NULL)
4094 fprintf (lra_dump_file,
4095 "Changing pseudo %d in operand %i of insn %u on equiv ",
4096 REGNO (old), i, INSN_UID (curr_insn));
4097 dump_value_slim (lra_dump_file, subst, 1);
4098 fprintf (lra_dump_file, "\n");
4100 op_change_p = change_p = true;
4102 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
4104 change_p = true;
4105 lra_update_dup (curr_id, i);
4109 /* Reload address registers and displacements. We do it before
4110 finding an alternative because of memory constraints. */
4111 before = after = NULL;
4112 for (i = 0; i < n_operands; i++)
4113 if (! curr_static_id->operand[i].is_operator
4114 && process_address (i, check_only_p, &before, &after))
4116 if (check_only_p)
4117 return true;
4118 change_p = true;
4119 lra_update_dup (curr_id, i);
4122 if (change_p)
4123 /* If we've changed the instruction then any alternative that
4124 we chose previously may no longer be valid. */
4125 lra_set_used_insn_alternative (curr_insn, LRA_UNKNOWN_ALT);
4127 if (! check_only_p && curr_insn_set != NULL_RTX
4128 && check_and_process_move (&change_p, &sec_mem_p))
4129 return change_p;
4131 try_swapped:
4133 reused_alternative_num = check_only_p ? LRA_UNKNOWN_ALT : curr_id->used_insn_alternative;
4134 if (lra_dump_file != NULL && reused_alternative_num >= 0)
4135 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
4136 reused_alternative_num, INSN_UID (curr_insn));
4138 if (process_alt_operands (reused_alternative_num))
4139 alt_p = true;
4141 if (check_only_p)
4142 return ! alt_p || best_losers != 0;
4144 /* If insn is commutative (it's safe to exchange a certain pair of
4145 operands) then we need to try each alternative twice, the second
4146 time matching those two operands as if we had exchanged them. To
4147 do this, really exchange them in operands.
4149 If we have just tried the alternatives the second time, return
4150 operands to normal and drop through. */
4152 if (reused_alternative_num < 0 && commutative >= 0)
4154 curr_swapped = !curr_swapped;
4155 if (curr_swapped)
4157 swap_operands (commutative);
4158 goto try_swapped;
4160 else
4161 swap_operands (commutative);
4164 if (! alt_p && ! sec_mem_p)
4166 /* No alternative works with reloads?? */
4167 if (INSN_CODE (curr_insn) >= 0)
4168 fatal_insn ("unable to generate reloads for:", curr_insn);
4169 error_for_asm (curr_insn,
4170 "inconsistent operand constraints in an %<asm%>");
4171 lra_asm_error_p = true;
4172 if (! JUMP_P (curr_insn))
4174 /* Avoid further trouble with this insn. Don't generate use
4175 pattern here as we could use the insn SP offset. */
4176 lra_set_insn_deleted (curr_insn);
4178 else
4180 lra_invalidate_insn_data (curr_insn);
4181 ira_nullify_asm_goto (curr_insn);
4182 lra_update_insn_regno_info (curr_insn);
4184 return true;
4187 /* If the best alternative is with operands 1 and 2 swapped, swap
4188 them. Update the operand numbers of any reloads already
4189 pushed. */
4191 if (goal_alt_swapped)
4193 if (lra_dump_file != NULL)
4194 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
4195 INSN_UID (curr_insn));
4197 /* Swap the duplicates too. */
4198 swap_operands (commutative);
4199 change_p = true;
4202 /* Some targets' TARGET_SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
4203 too conservatively. So we use the secondary memory only if there
4204 is no any alternative without reloads. */
4205 use_sec_mem_p = false;
4206 if (! alt_p)
4207 use_sec_mem_p = true;
4208 else if (sec_mem_p)
4210 for (i = 0; i < n_operands; i++)
4211 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
4212 break;
4213 use_sec_mem_p = i < n_operands;
4216 if (use_sec_mem_p)
4218 int in = -1, out = -1;
4219 rtx new_reg, src, dest, rld;
4220 machine_mode sec_mode, rld_mode;
4222 lra_assert (curr_insn_set != NULL_RTX && sec_mem_p);
4223 dest = SET_DEST (curr_insn_set);
4224 src = SET_SRC (curr_insn_set);
4225 for (i = 0; i < n_operands; i++)
4226 if (*curr_id->operand_loc[i] == dest)
4227 out = i;
4228 else if (*curr_id->operand_loc[i] == src)
4229 in = i;
4230 for (i = 0; i < curr_static_id->n_dups; i++)
4231 if (out < 0 && *curr_id->dup_loc[i] == dest)
4232 out = curr_static_id->dup_num[i];
4233 else if (in < 0 && *curr_id->dup_loc[i] == src)
4234 in = curr_static_id->dup_num[i];
4235 lra_assert (out >= 0 && in >= 0
4236 && curr_static_id->operand[out].type == OP_OUT
4237 && curr_static_id->operand[in].type == OP_IN);
4238 rld = partial_subreg_p (GET_MODE (src), GET_MODE (dest)) ? src : dest;
4239 rld_mode = GET_MODE (rld);
4240 sec_mode = targetm.secondary_memory_needed_mode (rld_mode);
4241 new_reg = lra_create_new_reg (sec_mode, NULL_RTX, NO_REGS, NULL,
4242 "secondary");
4243 /* If the mode is changed, it should be wider. */
4244 lra_assert (!partial_subreg_p (sec_mode, rld_mode));
4245 if (sec_mode != rld_mode)
4247 /* If the target says specifically to use another mode for
4248 secondary memory moves we cannot reuse the original
4249 insn. */
4250 after = emit_spill_move (false, new_reg, dest);
4251 lra_process_new_insns (curr_insn, NULL, after,
4252 "Inserting the sec. move");
4253 /* We may have non null BEFORE here (e.g. after address
4254 processing. */
4255 push_to_sequence (before);
4256 before = emit_spill_move (true, new_reg, src);
4257 emit_insn (before);
4258 before = get_insns ();
4259 end_sequence ();
4260 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
4261 lra_set_insn_deleted (curr_insn);
4263 else if (dest == rld)
4265 *curr_id->operand_loc[out] = new_reg;
4266 lra_update_dup (curr_id, out);
4267 after = emit_spill_move (false, new_reg, dest);
4268 lra_process_new_insns (curr_insn, NULL, after,
4269 "Inserting the sec. move");
4271 else
4273 *curr_id->operand_loc[in] = new_reg;
4274 lra_update_dup (curr_id, in);
4275 /* See comments above. */
4276 push_to_sequence (before);
4277 before = emit_spill_move (true, new_reg, src);
4278 emit_insn (before);
4279 before = get_insns ();
4280 end_sequence ();
4281 lra_process_new_insns (curr_insn, before, NULL,
4282 "Inserting the sec. move");
4284 lra_update_insn_regno_info (curr_insn);
4285 return true;
4288 lra_assert (goal_alt_number >= 0);
4289 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
4291 if (lra_dump_file != NULL)
4293 const char *p;
4295 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
4296 goal_alt_number, INSN_UID (curr_insn));
4297 for (i = 0; i < n_operands; i++)
4299 p = (curr_static_id->operand_alternative
4300 [goal_alt_number * n_operands + i].constraint);
4301 if (*p == '\0')
4302 continue;
4303 fprintf (lra_dump_file, " (%d) ", i);
4304 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
4305 fputc (*p, lra_dump_file);
4307 if (INSN_CODE (curr_insn) >= 0
4308 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
4309 fprintf (lra_dump_file, " {%s}", p);
4310 if (maybe_ne (curr_id->sp_offset, 0))
4312 fprintf (lra_dump_file, " (sp_off=");
4313 print_dec (curr_id->sp_offset, lra_dump_file);
4314 fprintf (lra_dump_file, ")");
4316 fprintf (lra_dump_file, "\n");
4319 /* Right now, for any pair of operands I and J that are required to
4320 match, with J < I, goal_alt_matches[I] is J. Add I to
4321 goal_alt_matched[J]. */
4323 for (i = 0; i < n_operands; i++)
4324 if ((j = goal_alt_matches[i]) >= 0)
4326 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
4328 /* We allow matching one output operand and several input
4329 operands. */
4330 lra_assert (k == 0
4331 || (curr_static_id->operand[j].type == OP_OUT
4332 && curr_static_id->operand[i].type == OP_IN
4333 && (curr_static_id->operand
4334 [goal_alt_matched[j][0]].type == OP_IN)));
4335 goal_alt_matched[j][k] = i;
4336 goal_alt_matched[j][k + 1] = -1;
4339 for (i = 0; i < n_operands; i++)
4340 goal_alt_win[i] |= goal_alt_match_win[i];
4342 /* Any constants that aren't allowed and can't be reloaded into
4343 registers are here changed into memory references. */
4344 for (i = 0; i < n_operands; i++)
4345 if (goal_alt_win[i])
4347 int regno;
4348 enum reg_class new_class;
4349 rtx reg = *curr_id->operand_loc[i];
4351 if (GET_CODE (reg) == SUBREG)
4352 reg = SUBREG_REG (reg);
4354 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
4356 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
4358 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
4360 lra_assert (ok_p);
4361 lra_change_class (regno, new_class, " Change to", true);
4365 else
4367 const char *constraint;
4368 char c;
4369 rtx op = *curr_id->operand_loc[i];
4370 rtx subreg = NULL_RTX;
4371 machine_mode mode = curr_operand_mode[i];
4373 if (GET_CODE (op) == SUBREG)
4375 subreg = op;
4376 op = SUBREG_REG (op);
4377 mode = GET_MODE (op);
4380 if (CONST_POOL_OK_P (mode, op)
4381 && ((targetm.preferred_reload_class
4382 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
4383 || no_input_reloads_p))
4385 rtx tem = force_const_mem (mode, op);
4387 change_p = true;
4388 if (subreg != NULL_RTX)
4389 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
4391 *curr_id->operand_loc[i] = tem;
4392 lra_update_dup (curr_id, i);
4393 process_address (i, false, &before, &after);
4395 /* If the alternative accepts constant pool refs directly
4396 there will be no reload needed at all. */
4397 if (subreg != NULL_RTX)
4398 continue;
4399 /* Skip alternatives before the one requested. */
4400 constraint = (curr_static_id->operand_alternative
4401 [goal_alt_number * n_operands + i].constraint);
4402 for (;
4403 (c = *constraint) && c != ',' && c != '#';
4404 constraint += CONSTRAINT_LEN (c, constraint))
4406 enum constraint_num cn = lookup_constraint (constraint);
4407 if ((insn_extra_memory_constraint (cn)
4408 || insn_extra_special_memory_constraint (cn)
4409 || insn_extra_relaxed_memory_constraint (cn))
4410 && satisfies_memory_constraint_p (tem, cn))
4411 break;
4413 if (c == '\0' || c == ',' || c == '#')
4414 continue;
4416 goal_alt_win[i] = true;
4420 n_outputs = 0;
4421 for (i = 0; i < n_operands; i++)
4422 if (curr_static_id->operand[i].type == OP_OUT)
4423 outputs[n_outputs++] = i;
4424 outputs[n_outputs] = -1;
4425 for (i = 0; i < n_operands; i++)
4427 int regno;
4428 bool optional_p = false;
4429 rtx old, new_reg;
4430 rtx op = *curr_id->operand_loc[i];
4432 if (goal_alt_win[i])
4434 if (goal_alt[i] == NO_REGS
4435 && REG_P (op)
4436 /* When we assign NO_REGS it means that we will not
4437 assign a hard register to the scratch pseudo by
4438 assigment pass and the scratch pseudo will be
4439 spilled. Spilled scratch pseudos are transformed
4440 back to scratches at the LRA end. */
4441 && ira_former_scratch_operand_p (curr_insn, i)
4442 && ira_former_scratch_p (REGNO (op)))
4444 int regno = REGNO (op);
4445 lra_change_class (regno, NO_REGS, " Change to", true);
4446 if (lra_get_regno_hard_regno (regno) >= 0)
4447 /* We don't have to mark all insn affected by the
4448 spilled pseudo as there is only one such insn, the
4449 current one. */
4450 reg_renumber[regno] = -1;
4451 lra_assert (bitmap_single_bit_set_p
4452 (&lra_reg_info[REGNO (op)].insn_bitmap));
4454 /* We can do an optional reload. If the pseudo got a hard
4455 reg, we might improve the code through inheritance. If
4456 it does not get a hard register we coalesce memory/memory
4457 moves later. Ignore move insns to avoid cycling. */
4458 if (! lra_simple_p
4459 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
4460 && goal_alt[i] != NO_REGS && REG_P (op)
4461 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
4462 && regno < new_regno_start
4463 && ! ira_former_scratch_p (regno)
4464 && reg_renumber[regno] < 0
4465 /* Check that the optional reload pseudo will be able to
4466 hold given mode value. */
4467 && ! (prohibited_class_reg_set_mode_p
4468 (goal_alt[i], reg_class_contents[goal_alt[i]],
4469 PSEUDO_REGNO_MODE (regno)))
4470 && (curr_insn_set == NULL_RTX
4471 || !((REG_P (SET_SRC (curr_insn_set))
4472 || MEM_P (SET_SRC (curr_insn_set))
4473 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
4474 && (REG_P (SET_DEST (curr_insn_set))
4475 || MEM_P (SET_DEST (curr_insn_set))
4476 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
4477 optional_p = true;
4478 else if (goal_alt_matched[i][0] != -1
4479 && curr_static_id->operand[i].type == OP_OUT
4480 && (curr_static_id->operand_alternative
4481 [goal_alt_number * n_operands + i].earlyclobber)
4482 && REG_P (op))
4484 for (j = 0; goal_alt_matched[i][j] != -1; j++)
4486 rtx op2 = *curr_id->operand_loc[goal_alt_matched[i][j]];
4488 if (REG_P (op2) && REGNO (op) != REGNO (op2))
4489 break;
4491 if (goal_alt_matched[i][j] != -1)
4493 /* Generate reloads for different output and matched
4494 input registers. This is the easiest way to avoid
4495 creation of non-existing register conflicts in
4496 lra-lives.cc. */
4497 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i],
4498 &goal_alt_exclude_start_hard_regs[i], &before,
4499 &after, TRUE);
4501 continue;
4503 else
4504 continue;
4507 /* Operands that match previous ones have already been handled. */
4508 if (goal_alt_matches[i] >= 0)
4509 continue;
4511 /* We should not have an operand with a non-offsettable address
4512 appearing where an offsettable address will do. It also may
4513 be a case when the address should be special in other words
4514 not a general one (e.g. it needs no index reg). */
4515 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
4517 enum reg_class rclass;
4518 rtx *loc = &XEXP (op, 0);
4519 enum rtx_code code = GET_CODE (*loc);
4521 push_to_sequence (before);
4522 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
4523 MEM, SCRATCH);
4524 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
4525 new_reg = emit_inc (rclass, *loc, *loc,
4526 /* This value does not matter for MODIFY. */
4527 GET_MODE_SIZE (GET_MODE (op)));
4528 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass,
4529 NULL, FALSE,
4530 "offsetable address", &new_reg))
4532 rtx addr = *loc;
4533 enum rtx_code code = GET_CODE (addr);
4534 bool align_p = false;
4536 if (code == AND && CONST_INT_P (XEXP (addr, 1)))
4538 /* (and ... (const_int -X)) is used to align to X bytes. */
4539 align_p = true;
4540 addr = XEXP (*loc, 0);
4542 else
4543 addr = canonicalize_reload_addr (addr);
4545 lra_emit_move (new_reg, addr);
4546 if (align_p)
4547 emit_move_insn (new_reg, gen_rtx_AND (GET_MODE (new_reg), new_reg, XEXP (*loc, 1)));
4549 before = get_insns ();
4550 end_sequence ();
4551 *loc = new_reg;
4552 lra_update_dup (curr_id, i);
4554 else if (goal_alt_matched[i][0] == -1)
4556 machine_mode mode;
4557 rtx reg, *loc;
4558 int hard_regno;
4559 enum op_type type = curr_static_id->operand[i].type;
4561 loc = curr_id->operand_loc[i];
4562 mode = curr_operand_mode[i];
4563 if (GET_CODE (*loc) == SUBREG)
4565 reg = SUBREG_REG (*loc);
4566 poly_int64 byte = SUBREG_BYTE (*loc);
4567 if (REG_P (reg)
4568 /* Strict_low_part requires reloading the register and not
4569 just the subreg. Likewise for a strict subreg no wider
4570 than a word for WORD_REGISTER_OPERATIONS targets. */
4571 && (curr_static_id->operand[i].strict_low
4572 || (!paradoxical_subreg_p (mode, GET_MODE (reg))
4573 && (hard_regno
4574 = get_try_hard_regno (REGNO (reg))) >= 0
4575 && (simplify_subreg_regno
4576 (hard_regno,
4577 GET_MODE (reg), byte, mode) < 0)
4578 && (goal_alt[i] == NO_REGS
4579 || (simplify_subreg_regno
4580 (ira_class_hard_regs[goal_alt[i]][0],
4581 GET_MODE (reg), byte, mode) >= 0)))
4582 || (partial_subreg_p (mode, GET_MODE (reg))
4583 && known_le (GET_MODE_SIZE (GET_MODE (reg)),
4584 UNITS_PER_WORD)
4585 && WORD_REGISTER_OPERATIONS))
4586 /* Avoid the situation when there are no available hard regs
4587 for the pseudo mode but there are ones for the subreg
4588 mode: */
4589 && !(goal_alt[i] != NO_REGS
4590 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
4591 && (prohibited_class_reg_set_mode_p
4592 (goal_alt[i], reg_class_contents[goal_alt[i]],
4593 GET_MODE (reg)))
4594 && !(prohibited_class_reg_set_mode_p
4595 (goal_alt[i], reg_class_contents[goal_alt[i]],
4596 mode))))
4598 /* An OP_INOUT is required when reloading a subreg of a
4599 mode wider than a word to ensure that data beyond the
4600 word being reloaded is preserved. Also automatically
4601 ensure that strict_low_part reloads are made into
4602 OP_INOUT which should already be true from the backend
4603 constraints. */
4604 if (type == OP_OUT
4605 && (curr_static_id->operand[i].strict_low
4606 || read_modify_subreg_p (*loc)))
4607 type = OP_INOUT;
4608 loc = &SUBREG_REG (*loc);
4609 mode = GET_MODE (*loc);
4612 old = *loc;
4613 if (get_reload_reg (type, mode, old, goal_alt[i],
4614 &goal_alt_exclude_start_hard_regs[i],
4615 loc != curr_id->operand_loc[i], "", &new_reg)
4616 && type != OP_OUT)
4618 push_to_sequence (before);
4619 lra_emit_move (new_reg, old);
4620 before = get_insns ();
4621 end_sequence ();
4623 *loc = new_reg;
4624 if (type != OP_IN
4625 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
4627 start_sequence ();
4628 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
4629 emit_insn (after);
4630 after = get_insns ();
4631 end_sequence ();
4632 *loc = new_reg;
4634 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
4635 if (goal_alt_dont_inherit_ops[j] == i)
4637 lra_set_regno_unique_value (REGNO (new_reg));
4638 break;
4640 lra_update_dup (curr_id, i);
4642 else if (curr_static_id->operand[i].type == OP_IN
4643 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4644 == OP_OUT
4645 || (curr_static_id->operand[goal_alt_matched[i][0]].type
4646 == OP_INOUT
4647 && (operands_match_p
4648 (*curr_id->operand_loc[i],
4649 *curr_id->operand_loc[goal_alt_matched[i][0]],
4650 -1)))))
4652 /* generate reloads for input and matched outputs. */
4653 match_inputs[0] = i;
4654 match_inputs[1] = -1;
4655 match_reload (goal_alt_matched[i][0], match_inputs, outputs,
4656 goal_alt[i], &goal_alt_exclude_start_hard_regs[i],
4657 &before, &after,
4658 curr_static_id->operand_alternative
4659 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
4660 .earlyclobber);
4662 else if ((curr_static_id->operand[i].type == OP_OUT
4663 || (curr_static_id->operand[i].type == OP_INOUT
4664 && (operands_match_p
4665 (*curr_id->operand_loc[i],
4666 *curr_id->operand_loc[goal_alt_matched[i][0]],
4667 -1))))
4668 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4669 == OP_IN))
4670 /* Generate reloads for output and matched inputs. */
4671 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i],
4672 &goal_alt_exclude_start_hard_regs[i], &before, &after,
4673 curr_static_id->operand_alternative
4674 [goal_alt_number * n_operands + i].earlyclobber);
4675 else if (curr_static_id->operand[i].type == OP_IN
4676 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4677 == OP_IN))
4679 /* Generate reloads for matched inputs. */
4680 match_inputs[0] = i;
4681 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
4682 match_inputs[j + 1] = k;
4683 match_inputs[j + 1] = -1;
4684 match_reload (-1, match_inputs, outputs, goal_alt[i],
4685 &goal_alt_exclude_start_hard_regs[i],
4686 &before, &after, false);
4688 else
4689 /* We must generate code in any case when function
4690 process_alt_operands decides that it is possible. */
4691 gcc_unreachable ();
4693 if (optional_p)
4695 rtx reg = op;
4697 lra_assert (REG_P (reg));
4698 regno = REGNO (reg);
4699 op = *curr_id->operand_loc[i]; /* Substitution. */
4700 if (GET_CODE (op) == SUBREG)
4701 op = SUBREG_REG (op);
4702 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
4703 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
4704 lra_reg_info[REGNO (op)].restore_rtx = reg;
4705 if (lra_dump_file != NULL)
4706 fprintf (lra_dump_file,
4707 " Making reload reg %d for reg %d optional\n",
4708 REGNO (op), regno);
4711 if (before != NULL_RTX || after != NULL_RTX
4712 || max_regno_before != max_reg_num ())
4713 change_p = true;
4714 if (change_p)
4716 lra_update_operator_dups (curr_id);
4717 /* Something changes -- process the insn. */
4718 lra_update_insn_regno_info (curr_insn);
4720 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
4721 return change_p;
4724 /* Return true if INSN satisfies all constraints. In other words, no
4725 reload insns are needed. */
4726 bool
4727 lra_constrain_insn (rtx_insn *insn)
4729 int saved_new_regno_start = new_regno_start;
4730 int saved_new_insn_uid_start = new_insn_uid_start;
4731 bool change_p;
4733 curr_insn = insn;
4734 curr_id = lra_get_insn_recog_data (curr_insn);
4735 curr_static_id = curr_id->insn_static_data;
4736 new_insn_uid_start = get_max_uid ();
4737 new_regno_start = max_reg_num ();
4738 change_p = curr_insn_transform (true);
4739 new_regno_start = saved_new_regno_start;
4740 new_insn_uid_start = saved_new_insn_uid_start;
4741 return ! change_p;
4744 /* Return true if X is in LIST. */
4745 static bool
4746 in_list_p (rtx x, rtx list)
4748 for (; list != NULL_RTX; list = XEXP (list, 1))
4749 if (XEXP (list, 0) == x)
4750 return true;
4751 return false;
4754 /* Return true if X contains an allocatable hard register (if
4755 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
4756 static bool
4757 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
4759 int i, j;
4760 const char *fmt;
4761 enum rtx_code code;
4763 code = GET_CODE (x);
4764 if (REG_P (x))
4766 int regno = REGNO (x);
4767 HARD_REG_SET alloc_regs;
4769 if (hard_reg_p)
4771 if (regno >= FIRST_PSEUDO_REGISTER)
4772 regno = lra_get_regno_hard_regno (regno);
4773 if (regno < 0)
4774 return false;
4775 alloc_regs = ~lra_no_alloc_regs;
4776 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
4778 else
4780 if (regno < FIRST_PSEUDO_REGISTER)
4781 return false;
4782 if (! spilled_p)
4783 return true;
4784 return lra_get_regno_hard_regno (regno) < 0;
4787 fmt = GET_RTX_FORMAT (code);
4788 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4790 if (fmt[i] == 'e')
4792 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4793 return true;
4795 else if (fmt[i] == 'E')
4797 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4798 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4799 return true;
4802 return false;
4805 /* Process all regs in location *LOC and change them on equivalent
4806 substitution. Return true if any change was done. */
4807 static bool
4808 loc_equivalence_change_p (rtx *loc)
4810 rtx subst, reg, x = *loc;
4811 bool result = false;
4812 enum rtx_code code = GET_CODE (x);
4813 const char *fmt;
4814 int i, j;
4816 if (code == SUBREG)
4818 reg = SUBREG_REG (x);
4819 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4820 && GET_MODE (subst) == VOIDmode)
4822 /* We cannot reload debug location. Simplify subreg here
4823 while we know the inner mode. */
4824 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4825 GET_MODE (reg), SUBREG_BYTE (x));
4826 return true;
4829 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4831 *loc = subst;
4832 return true;
4835 /* Scan all the operand sub-expressions. */
4836 fmt = GET_RTX_FORMAT (code);
4837 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4839 if (fmt[i] == 'e')
4840 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4841 else if (fmt[i] == 'E')
4842 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4843 result
4844 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4846 return result;
4849 /* Similar to loc_equivalence_change_p, but for use as
4850 simplify_replace_fn_rtx callback. DATA is insn for which the
4851 elimination is done. If it null we don't do the elimination. */
4852 static rtx
4853 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4855 if (!REG_P (loc))
4856 return NULL_RTX;
4858 rtx subst = (data == NULL
4859 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4860 if (subst != loc)
4861 return subst;
4863 return NULL_RTX;
4866 /* Maximum number of generated reload insns per an insn. It is for
4867 preventing this pass cycling in a bug case. */
4868 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4870 /* The current iteration number of this LRA pass. */
4871 int lra_constraint_iter;
4873 /* True if we should during assignment sub-pass check assignment
4874 correctness for all pseudos and spill some of them to correct
4875 conflicts. It can be necessary when we substitute equiv which
4876 needs checking register allocation correctness because the
4877 equivalent value contains allocatable hard registers, or when we
4878 restore multi-register pseudo, or when we change the insn code and
4879 its operand became INOUT operand when it was IN one before. */
4880 bool check_and_force_assignment_correctness_p;
4882 /* Return true if REGNO is referenced in more than one block. */
4883 static bool
4884 multi_block_pseudo_p (int regno)
4886 basic_block bb = NULL;
4887 unsigned int uid;
4888 bitmap_iterator bi;
4890 if (regno < FIRST_PSEUDO_REGISTER)
4891 return false;
4893 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4894 if (bb == NULL)
4895 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4896 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4897 return true;
4898 return false;
4901 /* Return true if LIST contains a deleted insn. */
4902 static bool
4903 contains_deleted_insn_p (rtx_insn_list *list)
4905 for (; list != NULL_RTX; list = list->next ())
4906 if (NOTE_P (list->insn ())
4907 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4908 return true;
4909 return false;
4912 /* Return true if X contains a pseudo dying in INSN. */
4913 static bool
4914 dead_pseudo_p (rtx x, rtx_insn *insn)
4916 int i, j;
4917 const char *fmt;
4918 enum rtx_code code;
4920 if (REG_P (x))
4921 return (insn != NULL_RTX
4922 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4923 code = GET_CODE (x);
4924 fmt = GET_RTX_FORMAT (code);
4925 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4927 if (fmt[i] == 'e')
4929 if (dead_pseudo_p (XEXP (x, i), insn))
4930 return true;
4932 else if (fmt[i] == 'E')
4934 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4935 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4936 return true;
4939 return false;
4942 /* Return true if INSN contains a dying pseudo in INSN right hand
4943 side. */
4944 static bool
4945 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4947 rtx set = single_set (insn);
4949 gcc_assert (set != NULL);
4950 return dead_pseudo_p (SET_SRC (set), insn);
4953 /* Return true if any init insn of REGNO contains a dying pseudo in
4954 insn right hand side. */
4955 static bool
4956 init_insn_rhs_dead_pseudo_p (int regno)
4958 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4960 if (insns == NULL)
4961 return false;
4962 for (; insns != NULL_RTX; insns = insns->next ())
4963 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4964 return true;
4965 return false;
4968 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4969 reverse only if we have one init insn with given REGNO as a
4970 source. */
4971 static bool
4972 reverse_equiv_p (int regno)
4974 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4975 rtx set;
4977 if (insns == NULL)
4978 return false;
4979 if (! INSN_P (insns->insn ())
4980 || insns->next () != NULL)
4981 return false;
4982 if ((set = single_set (insns->insn ())) == NULL_RTX)
4983 return false;
4984 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4987 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4988 call this function only for non-reverse equivalence. */
4989 static bool
4990 contains_reloaded_insn_p (int regno)
4992 rtx set;
4993 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4995 for (; list != NULL; list = list->next ())
4996 if ((set = single_set (list->insn ())) == NULL_RTX
4997 || ! REG_P (SET_DEST (set))
4998 || (int) REGNO (SET_DEST (set)) != regno)
4999 return true;
5000 return false;
5003 /* Entry function of LRA constraint pass. Return true if the
5004 constraint pass did change the code. */
5005 bool
5006 lra_constraints (bool first_p)
5008 bool changed_p;
5009 int i, hard_regno, new_insns_num;
5010 unsigned int min_len, new_min_len, uid;
5011 rtx set, x, reg, dest_reg;
5012 basic_block last_bb;
5013 bitmap_iterator bi;
5015 lra_constraint_iter++;
5016 if (lra_dump_file != NULL)
5017 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
5018 lra_constraint_iter);
5019 changed_p = false;
5020 if (pic_offset_table_rtx
5021 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
5022 check_and_force_assignment_correctness_p = true;
5023 else if (first_p)
5024 /* On the first iteration we should check IRA assignment
5025 correctness. In rare cases, the assignments can be wrong as
5026 early clobbers operands are ignored in IRA or usages of
5027 paradoxical sub-registers are not taken into account by
5028 IRA. */
5029 check_and_force_assignment_correctness_p = true;
5030 new_insn_uid_start = get_max_uid ();
5031 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
5032 /* Mark used hard regs for target stack size calulations. */
5033 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5034 if (lra_reg_info[i].nrefs != 0
5035 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
5037 int j, nregs;
5039 nregs = hard_regno_nregs (hard_regno, lra_reg_info[i].biggest_mode);
5040 for (j = 0; j < nregs; j++)
5041 df_set_regs_ever_live (hard_regno + j, true);
5043 /* Do elimination before the equivalence processing as we can spill
5044 some pseudos during elimination. */
5045 lra_eliminate (false, first_p);
5046 auto_bitmap equiv_insn_bitmap (&reg_obstack);
5047 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5048 if (lra_reg_info[i].nrefs != 0)
5050 ira_reg_equiv[i].profitable_p = true;
5051 reg = regno_reg_rtx[i];
5052 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
5054 bool pseudo_p = contains_reg_p (x, false, false);
5056 /* After RTL transformation, we cannot guarantee that
5057 pseudo in the substitution was not reloaded which might
5058 make equivalence invalid. For example, in reverse
5059 equiv of p0
5061 p0 <- ...
5063 equiv_mem <- p0
5065 the memory address register was reloaded before the 2nd
5066 insn. */
5067 if ((! first_p && pseudo_p)
5068 /* We don't use DF for compilation speed sake. So it
5069 is problematic to update live info when we use an
5070 equivalence containing pseudos in more than one
5071 BB. */
5072 || (pseudo_p && multi_block_pseudo_p (i))
5073 /* If an init insn was deleted for some reason, cancel
5074 the equiv. We could update the equiv insns after
5075 transformations including an equiv insn deletion
5076 but it is not worthy as such cases are extremely
5077 rare. */
5078 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
5079 /* If it is not a reverse equivalence, we check that a
5080 pseudo in rhs of the init insn is not dying in the
5081 insn. Otherwise, the live info at the beginning of
5082 the corresponding BB might be wrong after we
5083 removed the insn. When the equiv can be a
5084 constant, the right hand side of the init insn can
5085 be a pseudo. */
5086 || (! reverse_equiv_p (i)
5087 && (init_insn_rhs_dead_pseudo_p (i)
5088 /* If we reloaded the pseudo in an equivalence
5089 init insn, we cannot remove the equiv init
5090 insns and the init insns might write into
5091 const memory in this case. */
5092 || contains_reloaded_insn_p (i)))
5093 /* Prevent access beyond equivalent memory for
5094 paradoxical subregs. */
5095 || (MEM_P (x)
5096 && maybe_gt (GET_MODE_SIZE (lra_reg_info[i].biggest_mode),
5097 GET_MODE_SIZE (GET_MODE (x))))
5098 || (pic_offset_table_rtx
5099 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
5100 && (targetm.preferred_reload_class
5101 (x, lra_get_allocno_class (i)) == NO_REGS))
5102 || contains_symbol_ref_p (x))))
5103 ira_reg_equiv[i].defined_p = false;
5104 if (contains_reg_p (x, false, true))
5105 ira_reg_equiv[i].profitable_p = false;
5106 if (get_equiv (reg) != reg)
5107 bitmap_ior_into (equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
5110 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5111 update_equiv (i);
5112 /* We should add all insns containing pseudos which should be
5113 substituted by their equivalences. */
5114 EXECUTE_IF_SET_IN_BITMAP (equiv_insn_bitmap, 0, uid, bi)
5115 lra_push_insn_by_uid (uid);
5116 min_len = lra_insn_stack_length ();
5117 new_insns_num = 0;
5118 last_bb = NULL;
5119 changed_p = false;
5120 while ((new_min_len = lra_insn_stack_length ()) != 0)
5122 curr_insn = lra_pop_insn ();
5123 --new_min_len;
5124 curr_bb = BLOCK_FOR_INSN (curr_insn);
5125 if (curr_bb != last_bb)
5127 last_bb = curr_bb;
5128 bb_reload_num = lra_curr_reload_num;
5130 if (min_len > new_min_len)
5132 min_len = new_min_len;
5133 new_insns_num = 0;
5135 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
5136 internal_error
5137 ("maximum number of generated reload insns per insn achieved (%d)",
5138 MAX_RELOAD_INSNS_NUMBER);
5139 new_insns_num++;
5140 if (DEBUG_INSN_P (curr_insn))
5142 /* We need to check equivalence in debug insn and change
5143 pseudo to the equivalent value if necessary. */
5144 curr_id = lra_get_insn_recog_data (curr_insn);
5145 if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn)))
5147 rtx old = *curr_id->operand_loc[0];
5148 *curr_id->operand_loc[0]
5149 = simplify_replace_fn_rtx (old, NULL_RTX,
5150 loc_equivalence_callback, curr_insn);
5151 if (old != *curr_id->operand_loc[0])
5153 lra_update_insn_regno_info (curr_insn);
5154 changed_p = true;
5158 else if (INSN_P (curr_insn))
5160 if ((set = single_set (curr_insn)) != NULL_RTX)
5162 dest_reg = SET_DEST (set);
5163 /* The equivalence pseudo could be set up as SUBREG in a
5164 case when it is a call restore insn in a mode
5165 different from the pseudo mode. */
5166 if (GET_CODE (dest_reg) == SUBREG)
5167 dest_reg = SUBREG_REG (dest_reg);
5168 if ((REG_P (dest_reg)
5169 && (x = get_equiv (dest_reg)) != dest_reg
5170 /* Remove insns which set up a pseudo whose value
5171 cannot be changed. Such insns might be not in
5172 init_insns because we don't update equiv data
5173 during insn transformations.
5175 As an example, let suppose that a pseudo got
5176 hard register and on the 1st pass was not
5177 changed to equivalent constant. We generate an
5178 additional insn setting up the pseudo because of
5179 secondary memory movement. Then the pseudo is
5180 spilled and we use the equiv constant. In this
5181 case we should remove the additional insn and
5182 this insn is not init_insns list. */
5183 && (! MEM_P (x) || MEM_READONLY_P (x)
5184 /* Check that this is actually an insn setting
5185 up the equivalence. */
5186 || in_list_p (curr_insn,
5187 ira_reg_equiv
5188 [REGNO (dest_reg)].init_insns)))
5189 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
5190 && in_list_p (curr_insn,
5191 ira_reg_equiv
5192 [REGNO (SET_SRC (set))].init_insns)))
5194 /* This is equiv init insn of pseudo which did not get a
5195 hard register -- remove the insn. */
5196 if (lra_dump_file != NULL)
5198 fprintf (lra_dump_file,
5199 " Removing equiv init insn %i (freq=%d)\n",
5200 INSN_UID (curr_insn),
5201 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
5202 dump_insn_slim (lra_dump_file, curr_insn);
5204 if (contains_reg_p (x, true, false))
5205 check_and_force_assignment_correctness_p = true;
5206 lra_set_insn_deleted (curr_insn);
5207 continue;
5210 curr_id = lra_get_insn_recog_data (curr_insn);
5211 curr_static_id = curr_id->insn_static_data;
5212 init_curr_insn_input_reloads ();
5213 init_curr_operand_mode ();
5214 if (curr_insn_transform (false))
5215 changed_p = true;
5216 /* Check non-transformed insns too for equiv change as USE
5217 or CLOBBER don't need reloads but can contain pseudos
5218 being changed on their equivalences. */
5219 else if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn))
5220 && loc_equivalence_change_p (&PATTERN (curr_insn)))
5222 lra_update_insn_regno_info (curr_insn);
5223 changed_p = true;
5228 /* If we used a new hard regno, changed_p should be true because the
5229 hard reg is assigned to a new pseudo. */
5230 if (flag_checking && !changed_p)
5232 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5233 if (lra_reg_info[i].nrefs != 0
5234 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
5236 int j, nregs = hard_regno_nregs (hard_regno,
5237 PSEUDO_REGNO_MODE (i));
5239 for (j = 0; j < nregs; j++)
5240 lra_assert (df_regs_ever_live_p (hard_regno + j));
5243 return changed_p;
5246 static void initiate_invariants (void);
5247 static void finish_invariants (void);
5249 /* Initiate the LRA constraint pass. It is done once per
5250 function. */
5251 void
5252 lra_constraints_init (void)
5254 initiate_invariants ();
5257 /* Finalize the LRA constraint pass. It is done once per
5258 function. */
5259 void
5260 lra_constraints_finish (void)
5262 finish_invariants ();
5267 /* Structure describes invariants for ineheritance. */
5268 struct lra_invariant
5270 /* The order number of the invariant. */
5271 int num;
5272 /* The invariant RTX. */
5273 rtx invariant_rtx;
5274 /* The origin insn of the invariant. */
5275 rtx_insn *insn;
5278 typedef lra_invariant invariant_t;
5279 typedef invariant_t *invariant_ptr_t;
5280 typedef const invariant_t *const_invariant_ptr_t;
5282 /* Pointer to the inheritance invariants. */
5283 static vec<invariant_ptr_t> invariants;
5285 /* Allocation pool for the invariants. */
5286 static object_allocator<lra_invariant> *invariants_pool;
5288 /* Hash table for the invariants. */
5289 static htab_t invariant_table;
5291 /* Hash function for INVARIANT. */
5292 static hashval_t
5293 invariant_hash (const void *invariant)
5295 rtx inv = ((const_invariant_ptr_t) invariant)->invariant_rtx;
5296 return lra_rtx_hash (inv);
5299 /* Equal function for invariants INVARIANT1 and INVARIANT2. */
5300 static int
5301 invariant_eq_p (const void *invariant1, const void *invariant2)
5303 rtx inv1 = ((const_invariant_ptr_t) invariant1)->invariant_rtx;
5304 rtx inv2 = ((const_invariant_ptr_t) invariant2)->invariant_rtx;
5306 return rtx_equal_p (inv1, inv2);
5309 /* Insert INVARIANT_RTX into the table if it is not there yet. Return
5310 invariant which is in the table. */
5311 static invariant_ptr_t
5312 insert_invariant (rtx invariant_rtx)
5314 void **entry_ptr;
5315 invariant_t invariant;
5316 invariant_ptr_t invariant_ptr;
5318 invariant.invariant_rtx = invariant_rtx;
5319 entry_ptr = htab_find_slot (invariant_table, &invariant, INSERT);
5320 if (*entry_ptr == NULL)
5322 invariant_ptr = invariants_pool->allocate ();
5323 invariant_ptr->invariant_rtx = invariant_rtx;
5324 invariant_ptr->insn = NULL;
5325 invariants.safe_push (invariant_ptr);
5326 *entry_ptr = (void *) invariant_ptr;
5328 return (invariant_ptr_t) *entry_ptr;
5331 /* Initiate the invariant table. */
5332 static void
5333 initiate_invariants (void)
5335 invariants.create (100);
5336 invariants_pool
5337 = new object_allocator<lra_invariant> ("Inheritance invariants");
5338 invariant_table = htab_create (100, invariant_hash, invariant_eq_p, NULL);
5341 /* Finish the invariant table. */
5342 static void
5343 finish_invariants (void)
5345 htab_delete (invariant_table);
5346 delete invariants_pool;
5347 invariants.release ();
5350 /* Make the invariant table empty. */
5351 static void
5352 clear_invariants (void)
5354 htab_empty (invariant_table);
5355 invariants_pool->release ();
5356 invariants.truncate (0);
5361 /* This page contains code to do inheritance/split
5362 transformations. */
5364 /* Number of reloads passed so far in current EBB. */
5365 static int reloads_num;
5367 /* Number of calls passed so far in current EBB. */
5368 static int calls_num;
5370 /* Index ID is the CALLS_NUM associated the last call we saw with
5371 ABI identifier ID. */
5372 static int last_call_for_abi[NUM_ABI_IDS];
5374 /* Which registers have been fully or partially clobbered by a call
5375 since they were last used. */
5376 static HARD_REG_SET full_and_partial_call_clobbers;
5378 /* Current reload pseudo check for validity of elements in
5379 USAGE_INSNS. */
5380 static int curr_usage_insns_check;
5382 /* Info about last usage of registers in EBB to do inheritance/split
5383 transformation. Inheritance transformation is done from a spilled
5384 pseudo and split transformations from a hard register or a pseudo
5385 assigned to a hard register. */
5386 struct usage_insns
5388 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
5389 value INSNS is valid. The insns is chain of optional debug insns
5390 and a finishing non-debug insn using the corresponding reg. The
5391 value is also used to mark the registers which are set up in the
5392 current insn. The negated insn uid is used for this. */
5393 int check;
5394 /* Value of global reloads_num at the last insn in INSNS. */
5395 int reloads_num;
5396 /* Value of global reloads_nums at the last insn in INSNS. */
5397 int calls_num;
5398 /* It can be true only for splitting. And it means that the restore
5399 insn should be put after insn given by the following member. */
5400 bool after_p;
5401 /* Next insns in the current EBB which use the original reg and the
5402 original reg value is not changed between the current insn and
5403 the next insns. In order words, e.g. for inheritance, if we need
5404 to use the original reg value again in the next insns we can try
5405 to use the value in a hard register from a reload insn of the
5406 current insn. */
5407 rtx insns;
5410 /* Map: regno -> corresponding pseudo usage insns. */
5411 static struct usage_insns *usage_insns;
5413 static void
5414 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
5416 usage_insns[regno].check = curr_usage_insns_check;
5417 usage_insns[regno].insns = insn;
5418 usage_insns[regno].reloads_num = reloads_num;
5419 usage_insns[regno].calls_num = calls_num;
5420 usage_insns[regno].after_p = after_p;
5421 if (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0)
5422 remove_from_hard_reg_set (&full_and_partial_call_clobbers,
5423 PSEUDO_REGNO_MODE (regno),
5424 reg_renumber[regno]);
5427 /* The function is used to form list REGNO usages which consists of
5428 optional debug insns finished by a non-debug insn using REGNO.
5429 RELOADS_NUM is current number of reload insns processed so far. */
5430 static void
5431 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
5433 rtx next_usage_insns;
5435 if (usage_insns[regno].check == curr_usage_insns_check
5436 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
5437 && DEBUG_INSN_P (insn))
5439 /* Check that we did not add the debug insn yet. */
5440 if (next_usage_insns != insn
5441 && (GET_CODE (next_usage_insns) != INSN_LIST
5442 || XEXP (next_usage_insns, 0) != insn))
5443 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
5444 next_usage_insns);
5446 else if (NONDEBUG_INSN_P (insn))
5447 setup_next_usage_insn (regno, insn, reloads_num, false);
5448 else
5449 usage_insns[regno].check = 0;
5452 /* Return first non-debug insn in list USAGE_INSNS. */
5453 static rtx_insn *
5454 skip_usage_debug_insns (rtx usage_insns)
5456 rtx insn;
5458 /* Skip debug insns. */
5459 for (insn = usage_insns;
5460 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
5461 insn = XEXP (insn, 1))
5463 return safe_as_a <rtx_insn *> (insn);
5466 /* Return true if we need secondary memory moves for insn in
5467 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
5468 into the insn. */
5469 static bool
5470 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
5471 rtx usage_insns ATTRIBUTE_UNUSED)
5473 rtx_insn *insn;
5474 rtx set, dest;
5475 enum reg_class cl;
5477 if (inher_cl == ALL_REGS
5478 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
5479 return false;
5480 lra_assert (INSN_P (insn));
5481 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
5482 return false;
5483 dest = SET_DEST (set);
5484 if (! REG_P (dest))
5485 return false;
5486 lra_assert (inher_cl != NO_REGS);
5487 cl = get_reg_class (REGNO (dest));
5488 return (cl != NO_REGS && cl != ALL_REGS
5489 && targetm.secondary_memory_needed (GET_MODE (dest), inher_cl, cl));
5492 /* Registers involved in inheritance/split in the current EBB
5493 (inheritance/split pseudos and original registers). */
5494 static bitmap_head check_only_regs;
5496 /* Reload pseudos cannot be involded in invariant inheritance in the
5497 current EBB. */
5498 static bitmap_head invalid_invariant_regs;
5500 /* Do inheritance transformations for insn INSN, which defines (if
5501 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
5502 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
5503 form as the "insns" field of usage_insns. Return true if we
5504 succeed in such transformation.
5506 The transformations look like:
5508 p <- ... i <- ...
5509 ... p <- i (new insn)
5510 ... =>
5511 <- ... p ... <- ... i ...
5513 ... i <- p (new insn)
5514 <- ... p ... <- ... i ...
5515 ... =>
5516 <- ... p ... <- ... i ...
5517 where p is a spilled original pseudo and i is a new inheritance pseudo.
5520 The inheritance pseudo has the smallest class of two classes CL and
5521 class of ORIGINAL REGNO. */
5522 static bool
5523 inherit_reload_reg (bool def_p, int original_regno,
5524 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
5526 if (optimize_function_for_size_p (cfun))
5527 return false;
5529 enum reg_class rclass = lra_get_allocno_class (original_regno);
5530 rtx original_reg = regno_reg_rtx[original_regno];
5531 rtx new_reg, usage_insn;
5532 rtx_insn *new_insns;
5534 lra_assert (! usage_insns[original_regno].after_p);
5535 if (lra_dump_file != NULL)
5536 fprintf (lra_dump_file,
5537 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
5538 if (! ira_reg_classes_intersect_p[cl][rclass])
5540 if (lra_dump_file != NULL)
5542 fprintf (lra_dump_file,
5543 " Rejecting inheritance for %d "
5544 "because of disjoint classes %s and %s\n",
5545 original_regno, reg_class_names[cl],
5546 reg_class_names[rclass]);
5547 fprintf (lra_dump_file,
5548 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5550 return false;
5552 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
5553 /* We don't use a subset of two classes because it can be
5554 NO_REGS. This transformation is still profitable in most
5555 cases even if the classes are not intersected as register
5556 move is probably cheaper than a memory load. */
5557 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
5559 if (lra_dump_file != NULL)
5560 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
5561 reg_class_names[cl], reg_class_names[rclass]);
5563 rclass = cl;
5565 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
5567 /* Reject inheritance resulting in secondary memory moves.
5568 Otherwise, there is a danger in LRA cycling. Also such
5569 transformation will be unprofitable. */
5570 if (lra_dump_file != NULL)
5572 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
5573 rtx set = single_set (insn);
5575 lra_assert (set != NULL_RTX);
5577 rtx dest = SET_DEST (set);
5579 lra_assert (REG_P (dest));
5580 fprintf (lra_dump_file,
5581 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
5582 "as secondary mem is needed\n",
5583 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
5584 original_regno, reg_class_names[rclass]);
5585 fprintf (lra_dump_file,
5586 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5588 return false;
5590 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
5591 rclass, NULL, "inheritance");
5592 start_sequence ();
5593 if (def_p)
5594 lra_emit_move (original_reg, new_reg);
5595 else
5596 lra_emit_move (new_reg, original_reg);
5597 new_insns = get_insns ();
5598 end_sequence ();
5599 if (NEXT_INSN (new_insns) != NULL_RTX)
5601 if (lra_dump_file != NULL)
5603 fprintf (lra_dump_file,
5604 " Rejecting inheritance %d->%d "
5605 "as it results in 2 or more insns:\n",
5606 original_regno, REGNO (new_reg));
5607 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
5608 fprintf (lra_dump_file,
5609 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5611 return false;
5613 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
5614 lra_update_insn_regno_info (insn);
5615 if (! def_p)
5616 /* We now have a new usage insn for original regno. */
5617 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
5618 if (lra_dump_file != NULL)
5619 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
5620 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
5621 lra_reg_info[REGNO (new_reg)].restore_rtx = regno_reg_rtx[original_regno];
5622 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5623 bitmap_set_bit (&check_only_regs, original_regno);
5624 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
5625 if (def_p)
5626 lra_process_new_insns (insn, NULL, new_insns,
5627 "Add original<-inheritance");
5628 else
5629 lra_process_new_insns (insn, new_insns, NULL,
5630 "Add inheritance<-original");
5631 while (next_usage_insns != NULL_RTX)
5633 if (GET_CODE (next_usage_insns) != INSN_LIST)
5635 usage_insn = next_usage_insns;
5636 lra_assert (NONDEBUG_INSN_P (usage_insn));
5637 next_usage_insns = NULL;
5639 else
5641 usage_insn = XEXP (next_usage_insns, 0);
5642 lra_assert (DEBUG_INSN_P (usage_insn));
5643 next_usage_insns = XEXP (next_usage_insns, 1);
5645 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
5646 DEBUG_INSN_P (usage_insn));
5647 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5648 if (lra_dump_file != NULL)
5650 basic_block bb = BLOCK_FOR_INSN (usage_insn);
5651 fprintf (lra_dump_file,
5652 " Inheritance reuse change %d->%d (bb%d):\n",
5653 original_regno, REGNO (new_reg),
5654 bb ? bb->index : -1);
5655 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5658 if (lra_dump_file != NULL)
5659 fprintf (lra_dump_file,
5660 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5661 return true;
5664 /* Return true if we need a caller save/restore for pseudo REGNO which
5665 was assigned to a hard register. */
5666 static inline bool
5667 need_for_call_save_p (int regno)
5669 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
5670 if (usage_insns[regno].calls_num < calls_num)
5672 unsigned int abis = 0;
5673 for (unsigned int i = 0; i < NUM_ABI_IDS; ++i)
5674 if (last_call_for_abi[i] > usage_insns[regno].calls_num)
5675 abis |= 1 << i;
5676 gcc_assert (abis);
5677 if (call_clobbered_in_region_p (abis, full_and_partial_call_clobbers,
5678 PSEUDO_REGNO_MODE (regno),
5679 reg_renumber[regno]))
5680 return true;
5682 return false;
5685 /* Global registers occurring in the current EBB. */
5686 static bitmap_head ebb_global_regs;
5688 /* Return true if we need a split for hard register REGNO or pseudo
5689 REGNO which was assigned to a hard register.
5690 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
5691 used for reloads since the EBB end. It is an approximation of the
5692 used hard registers in the split range. The exact value would
5693 require expensive calculations. If we were aggressive with
5694 splitting because of the approximation, the split pseudo will save
5695 the same hard register assignment and will be removed in the undo
5696 pass. We still need the approximation because too aggressive
5697 splitting would result in too inaccurate cost calculation in the
5698 assignment pass because of too many generated moves which will be
5699 probably removed in the undo pass. */
5700 static inline bool
5701 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
5703 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
5705 lra_assert (hard_regno >= 0);
5706 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
5707 /* Don't split eliminable hard registers, otherwise we can
5708 split hard registers like hard frame pointer, which
5709 lives on BB start/end according to DF-infrastructure,
5710 when there is a pseudo assigned to the register and
5711 living in the same BB. */
5712 && (regno >= FIRST_PSEUDO_REGISTER
5713 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
5714 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
5715 /* Don't split call clobbered hard regs living through
5716 calls, otherwise we might have a check problem in the
5717 assign sub-pass as in the most cases (exception is a
5718 situation when check_and_force_assignment_correctness_p value is
5719 true) the assign pass assumes that all pseudos living
5720 through calls are assigned to call saved hard regs. */
5721 && (regno >= FIRST_PSEUDO_REGISTER
5722 || !TEST_HARD_REG_BIT (full_and_partial_call_clobbers, regno))
5723 /* We need at least 2 reloads to make pseudo splitting
5724 profitable. We should provide hard regno splitting in
5725 any case to solve 1st insn scheduling problem when
5726 moving hard register definition up might result in
5727 impossibility to find hard register for reload pseudo of
5728 small register class. */
5729 && (usage_insns[regno].reloads_num
5730 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
5731 && (regno < FIRST_PSEUDO_REGISTER
5732 /* For short living pseudos, spilling + inheritance can
5733 be considered a substitution for splitting.
5734 Therefore we do not splitting for local pseudos. It
5735 decreases also aggressiveness of splitting. The
5736 minimal number of references is chosen taking into
5737 account that for 2 references splitting has no sense
5738 as we can just spill the pseudo. */
5739 || (regno >= FIRST_PSEUDO_REGISTER
5740 && lra_reg_info[regno].nrefs > 3
5741 && bitmap_bit_p (&ebb_global_regs, regno))))
5742 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
5745 /* Return class for the split pseudo created from original pseudo with
5746 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
5747 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
5748 results in no secondary memory movements. */
5749 static enum reg_class
5750 choose_split_class (enum reg_class allocno_class,
5751 int hard_regno ATTRIBUTE_UNUSED,
5752 machine_mode mode ATTRIBUTE_UNUSED)
5754 int i;
5755 enum reg_class cl, best_cl = NO_REGS;
5756 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
5757 = REGNO_REG_CLASS (hard_regno);
5759 if (! targetm.secondary_memory_needed (mode, allocno_class, allocno_class)
5760 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
5761 return allocno_class;
5762 for (i = 0;
5763 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
5764 i++)
5765 if (! targetm.secondary_memory_needed (mode, cl, hard_reg_class)
5766 && ! targetm.secondary_memory_needed (mode, hard_reg_class, cl)
5767 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
5768 && (best_cl == NO_REGS
5769 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
5770 best_cl = cl;
5771 return best_cl;
5774 /* Copy any equivalence information from ORIGINAL_REGNO to NEW_REGNO.
5775 It only makes sense to call this function if NEW_REGNO is always
5776 equal to ORIGINAL_REGNO. */
5778 static void
5779 lra_copy_reg_equiv (unsigned int new_regno, unsigned int original_regno)
5781 if (!ira_reg_equiv[original_regno].defined_p)
5782 return;
5784 ira_expand_reg_equiv ();
5785 ira_reg_equiv[new_regno].defined_p = true;
5786 if (ira_reg_equiv[original_regno].memory)
5787 ira_reg_equiv[new_regno].memory
5788 = copy_rtx (ira_reg_equiv[original_regno].memory);
5789 if (ira_reg_equiv[original_regno].constant)
5790 ira_reg_equiv[new_regno].constant
5791 = copy_rtx (ira_reg_equiv[original_regno].constant);
5792 if (ira_reg_equiv[original_regno].invariant)
5793 ira_reg_equiv[new_regno].invariant
5794 = copy_rtx (ira_reg_equiv[original_regno].invariant);
5797 /* Do split transformations for insn INSN, which defines or uses
5798 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
5799 the EBB next uses ORIGINAL_REGNO; it has the same form as the
5800 "insns" field of usage_insns. If TO is not NULL, we don't use
5801 usage_insns, we put restore insns after TO insn. It is a case when
5802 we call it from lra_split_hard_reg_for, outside the inheritance
5803 pass.
5805 The transformations look like:
5807 p <- ... p <- ...
5808 ... s <- p (new insn -- save)
5809 ... =>
5810 ... p <- s (new insn -- restore)
5811 <- ... p ... <- ... p ...
5813 <- ... p ... <- ... p ...
5814 ... s <- p (new insn -- save)
5815 ... =>
5816 ... p <- s (new insn -- restore)
5817 <- ... p ... <- ... p ...
5819 where p is an original pseudo got a hard register or a hard
5820 register and s is a new split pseudo. The save is put before INSN
5821 if BEFORE_P is true. Return true if we succeed in such
5822 transformation. */
5823 static bool
5824 split_reg (bool before_p, int original_regno, rtx_insn *insn,
5825 rtx next_usage_insns, rtx_insn *to)
5827 enum reg_class rclass;
5828 rtx original_reg;
5829 int hard_regno, nregs;
5830 rtx new_reg, usage_insn;
5831 rtx_insn *restore, *save;
5832 bool after_p;
5833 bool call_save_p;
5834 machine_mode mode;
5836 if (original_regno < FIRST_PSEUDO_REGISTER)
5838 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
5839 hard_regno = original_regno;
5840 call_save_p = false;
5841 nregs = 1;
5842 mode = lra_reg_info[hard_regno].biggest_mode;
5843 machine_mode reg_rtx_mode = GET_MODE (regno_reg_rtx[hard_regno]);
5844 /* A reg can have a biggest_mode of VOIDmode if it was only ever seen as
5845 part of a multi-word register. In that case, just use the reg_rtx
5846 mode. Do the same also if the biggest mode was larger than a register
5847 or we can not compare the modes. Otherwise, limit the size to that of
5848 the biggest access in the function or to the natural mode at least. */
5849 if (mode == VOIDmode
5850 || !ordered_p (GET_MODE_PRECISION (mode),
5851 GET_MODE_PRECISION (reg_rtx_mode))
5852 || paradoxical_subreg_p (mode, reg_rtx_mode)
5853 || maybe_gt (GET_MODE_PRECISION (reg_rtx_mode), GET_MODE_PRECISION (mode)))
5855 original_reg = regno_reg_rtx[hard_regno];
5856 mode = reg_rtx_mode;
5858 else
5859 original_reg = gen_rtx_REG (mode, hard_regno);
5861 else
5863 mode = PSEUDO_REGNO_MODE (original_regno);
5864 hard_regno = reg_renumber[original_regno];
5865 nregs = hard_regno_nregs (hard_regno, mode);
5866 rclass = lra_get_allocno_class (original_regno);
5867 original_reg = regno_reg_rtx[original_regno];
5868 call_save_p = need_for_call_save_p (original_regno);
5870 lra_assert (hard_regno >= 0);
5871 if (lra_dump_file != NULL)
5872 fprintf (lra_dump_file,
5873 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
5875 if (call_save_p)
5877 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
5878 hard_regno_nregs (hard_regno, mode),
5879 mode);
5880 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, NULL, "save");
5882 else
5884 rclass = choose_split_class (rclass, hard_regno, mode);
5885 if (rclass == NO_REGS)
5887 if (lra_dump_file != NULL)
5889 fprintf (lra_dump_file,
5890 " Rejecting split of %d(%s): "
5891 "no good reg class for %d(%s)\n",
5892 original_regno,
5893 reg_class_names[lra_get_allocno_class (original_regno)],
5894 hard_regno,
5895 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
5896 fprintf
5897 (lra_dump_file,
5898 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5900 return false;
5902 /* Split_if_necessary can split hard registers used as part of a
5903 multi-register mode but splits each register individually. The
5904 mode used for each independent register may not be supported
5905 so reject the split. Splitting the wider mode should theoretically
5906 be possible but is not implemented. */
5907 if (!targetm.hard_regno_mode_ok (hard_regno, mode))
5909 if (lra_dump_file != NULL)
5911 fprintf (lra_dump_file,
5912 " Rejecting split of %d(%s): unsuitable mode %s\n",
5913 original_regno,
5914 reg_class_names[lra_get_allocno_class (original_regno)],
5915 GET_MODE_NAME (mode));
5916 fprintf
5917 (lra_dump_file,
5918 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5920 return false;
5922 new_reg = lra_create_new_reg (mode, original_reg, rclass, NULL, "split");
5923 reg_renumber[REGNO (new_reg)] = hard_regno;
5925 int new_regno = REGNO (new_reg);
5926 save = emit_spill_move (true, new_reg, original_reg);
5927 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
5929 if (lra_dump_file != NULL)
5931 fprintf
5932 (lra_dump_file,
5933 " Rejecting split %d->%d resulting in > 2 save insns:\n",
5934 original_regno, new_regno);
5935 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
5936 fprintf (lra_dump_file,
5937 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5939 return false;
5941 restore = emit_spill_move (false, new_reg, original_reg);
5942 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
5944 if (lra_dump_file != NULL)
5946 fprintf (lra_dump_file,
5947 " Rejecting split %d->%d "
5948 "resulting in > 2 restore insns:\n",
5949 original_regno, new_regno);
5950 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
5951 fprintf (lra_dump_file,
5952 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5954 return false;
5956 /* Transfer equivalence information to the spill register, so that
5957 if we fail to allocate the spill register, we have the option of
5958 rematerializing the original value instead of spilling to the stack. */
5959 if (!HARD_REGISTER_NUM_P (original_regno)
5960 && mode == PSEUDO_REGNO_MODE (original_regno))
5961 lra_copy_reg_equiv (new_regno, original_regno);
5962 lra_reg_info[new_regno].restore_rtx = regno_reg_rtx[original_regno];
5963 bitmap_set_bit (&lra_split_regs, new_regno);
5964 if (to != NULL)
5966 lra_assert (next_usage_insns == NULL);
5967 usage_insn = to;
5968 after_p = TRUE;
5970 else
5972 /* We need check_only_regs only inside the inheritance pass. */
5973 bitmap_set_bit (&check_only_regs, new_regno);
5974 bitmap_set_bit (&check_only_regs, original_regno);
5975 after_p = usage_insns[original_regno].after_p;
5976 for (;;)
5978 if (GET_CODE (next_usage_insns) != INSN_LIST)
5980 usage_insn = next_usage_insns;
5981 break;
5983 usage_insn = XEXP (next_usage_insns, 0);
5984 lra_assert (DEBUG_INSN_P (usage_insn));
5985 next_usage_insns = XEXP (next_usage_insns, 1);
5986 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
5987 true);
5988 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5989 if (lra_dump_file != NULL)
5991 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5992 original_regno, new_regno);
5993 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5997 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5998 lra_assert (usage_insn != insn || (after_p && before_p));
5999 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
6000 after_p ? NULL : restore,
6001 after_p ? restore : NULL,
6002 call_save_p
6003 ? "Add reg<-save" : "Add reg<-split");
6004 lra_process_new_insns (insn, before_p ? save : NULL,
6005 before_p ? NULL : save,
6006 call_save_p
6007 ? "Add save<-reg" : "Add split<-reg");
6008 if (nregs > 1 || original_regno < FIRST_PSEUDO_REGISTER)
6009 /* If we are trying to split multi-register. We should check
6010 conflicts on the next assignment sub-pass. IRA can allocate on
6011 sub-register levels, LRA do this on pseudos level right now and
6012 this discrepancy may create allocation conflicts after
6013 splitting.
6015 If we are trying to split hard register we should also check conflicts
6016 as such splitting can create artificial conflict of the hard register
6017 with another pseudo because of simplified conflict calculation in
6018 LRA. */
6019 check_and_force_assignment_correctness_p = true;
6020 if (lra_dump_file != NULL)
6021 fprintf (lra_dump_file,
6022 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6023 return true;
6026 /* Split a hard reg for reload pseudo REGNO having RCLASS and living
6027 in the range [FROM, TO]. Return true if did a split. Otherwise,
6028 return false. */
6029 bool
6030 spill_hard_reg_in_range (int regno, enum reg_class rclass, rtx_insn *from, rtx_insn *to)
6032 int i, hard_regno;
6033 int rclass_size;
6034 rtx_insn *insn;
6035 unsigned int uid;
6036 bitmap_iterator bi;
6037 HARD_REG_SET ignore;
6039 lra_assert (from != NULL && to != NULL);
6040 ignore = lra_no_alloc_regs;
6041 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
6043 lra_insn_recog_data_t id = lra_insn_recog_data[uid];
6044 struct lra_static_insn_data *static_id = id->insn_static_data;
6045 struct lra_insn_reg *reg;
6047 for (reg = id->regs; reg != NULL; reg = reg->next)
6048 if (reg->regno < FIRST_PSEUDO_REGISTER)
6049 SET_HARD_REG_BIT (ignore, reg->regno);
6050 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
6051 SET_HARD_REG_BIT (ignore, reg->regno);
6053 rclass_size = ira_class_hard_regs_num[rclass];
6054 for (i = 0; i < rclass_size; i++)
6056 hard_regno = ira_class_hard_regs[rclass][i];
6057 if (! TEST_HARD_REG_BIT (lra_reg_info[regno].conflict_hard_regs, hard_regno)
6058 || TEST_HARD_REG_BIT (ignore, hard_regno))
6059 continue;
6060 for (insn = from; insn != NEXT_INSN (to); insn = NEXT_INSN (insn))
6062 struct lra_static_insn_data *static_id;
6063 struct lra_insn_reg *reg;
6065 if (!INSN_P (insn))
6066 continue;
6067 if (bitmap_bit_p (&lra_reg_info[hard_regno].insn_bitmap,
6068 INSN_UID (insn)))
6069 break;
6070 static_id = lra_get_insn_recog_data (insn)->insn_static_data;
6071 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
6072 if (reg->regno == hard_regno)
6073 break;
6074 if (reg != NULL)
6075 break;
6077 if (insn != NEXT_INSN (to))
6078 continue;
6079 if (split_reg (TRUE, hard_regno, from, NULL, to))
6080 return true;
6082 return false;
6085 /* Recognize that we need a split transformation for insn INSN, which
6086 defines or uses REGNO in its insn biggest MODE (we use it only if
6087 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
6088 hard registers which might be used for reloads since the EBB end.
6089 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
6090 uid before starting INSN processing. Return true if we succeed in
6091 such transformation. */
6092 static bool
6093 split_if_necessary (int regno, machine_mode mode,
6094 HARD_REG_SET potential_reload_hard_regs,
6095 bool before_p, rtx_insn *insn, int max_uid)
6097 bool res = false;
6098 int i, nregs = 1;
6099 rtx next_usage_insns;
6101 if (regno < FIRST_PSEUDO_REGISTER)
6102 nregs = hard_regno_nregs (regno, mode);
6103 for (i = 0; i < nregs; i++)
6104 if (usage_insns[regno + i].check == curr_usage_insns_check
6105 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
6106 /* To avoid processing the register twice or more. */
6107 && ((GET_CODE (next_usage_insns) != INSN_LIST
6108 && INSN_UID (next_usage_insns) < max_uid)
6109 || (GET_CODE (next_usage_insns) == INSN_LIST
6110 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
6111 && need_for_split_p (potential_reload_hard_regs, regno + i)
6112 && split_reg (before_p, regno + i, insn, next_usage_insns, NULL))
6113 res = true;
6114 return res;
6117 /* Return TRUE if rtx X is considered as an invariant for
6118 inheritance. */
6119 static bool
6120 invariant_p (const_rtx x)
6122 machine_mode mode;
6123 const char *fmt;
6124 enum rtx_code code;
6125 int i, j;
6127 if (side_effects_p (x))
6128 return false;
6130 code = GET_CODE (x);
6131 mode = GET_MODE (x);
6132 if (code == SUBREG)
6134 x = SUBREG_REG (x);
6135 code = GET_CODE (x);
6136 mode = wider_subreg_mode (mode, GET_MODE (x));
6139 if (MEM_P (x))
6140 return false;
6142 if (REG_P (x))
6144 int i, nregs, regno = REGNO (x);
6146 if (regno >= FIRST_PSEUDO_REGISTER || regno == STACK_POINTER_REGNUM
6147 || TEST_HARD_REG_BIT (eliminable_regset, regno)
6148 || GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
6149 return false;
6150 nregs = hard_regno_nregs (regno, mode);
6151 for (i = 0; i < nregs; i++)
6152 if (! fixed_regs[regno + i]
6153 /* A hard register may be clobbered in the current insn
6154 but we can ignore this case because if the hard
6155 register is used it should be set somewhere after the
6156 clobber. */
6157 || bitmap_bit_p (&invalid_invariant_regs, regno + i))
6158 return false;
6160 fmt = GET_RTX_FORMAT (code);
6161 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6163 if (fmt[i] == 'e')
6165 if (! invariant_p (XEXP (x, i)))
6166 return false;
6168 else if (fmt[i] == 'E')
6170 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6171 if (! invariant_p (XVECEXP (x, i, j)))
6172 return false;
6175 return true;
6178 /* We have 'dest_reg <- invariant'. Let us try to make an invariant
6179 inheritance transformation (using dest_reg instead invariant in a
6180 subsequent insn). */
6181 static bool
6182 process_invariant_for_inheritance (rtx dst_reg, rtx invariant_rtx)
6184 invariant_ptr_t invariant_ptr;
6185 rtx_insn *insn, *new_insns;
6186 rtx insn_set, insn_reg, new_reg;
6187 int insn_regno;
6188 bool succ_p = false;
6189 int dst_regno = REGNO (dst_reg);
6190 machine_mode dst_mode = GET_MODE (dst_reg);
6191 enum reg_class cl = lra_get_allocno_class (dst_regno), insn_reg_cl;
6193 invariant_ptr = insert_invariant (invariant_rtx);
6194 if ((insn = invariant_ptr->insn) != NULL_RTX)
6196 /* We have a subsequent insn using the invariant. */
6197 insn_set = single_set (insn);
6198 lra_assert (insn_set != NULL);
6199 insn_reg = SET_DEST (insn_set);
6200 lra_assert (REG_P (insn_reg));
6201 insn_regno = REGNO (insn_reg);
6202 insn_reg_cl = lra_get_allocno_class (insn_regno);
6204 if (dst_mode == GET_MODE (insn_reg)
6205 /* We should consider only result move reg insns which are
6206 cheap. */
6207 && targetm.register_move_cost (dst_mode, cl, insn_reg_cl) == 2
6208 && targetm.register_move_cost (dst_mode, cl, cl) == 2)
6210 if (lra_dump_file != NULL)
6211 fprintf (lra_dump_file,
6212 " [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[\n");
6213 new_reg = lra_create_new_reg (dst_mode, dst_reg, cl, NULL,
6214 "invariant inheritance");
6215 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
6216 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
6217 lra_reg_info[REGNO (new_reg)].restore_rtx = PATTERN (insn);
6218 start_sequence ();
6219 lra_emit_move (new_reg, dst_reg);
6220 new_insns = get_insns ();
6221 end_sequence ();
6222 lra_process_new_insns (curr_insn, NULL, new_insns,
6223 "Add invariant inheritance<-original");
6224 start_sequence ();
6225 lra_emit_move (SET_DEST (insn_set), new_reg);
6226 new_insns = get_insns ();
6227 end_sequence ();
6228 lra_process_new_insns (insn, NULL, new_insns,
6229 "Changing reload<-inheritance");
6230 lra_set_insn_deleted (insn);
6231 succ_p = true;
6232 if (lra_dump_file != NULL)
6234 fprintf (lra_dump_file,
6235 " Invariant inheritance reuse change %d (bb%d):\n",
6236 REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
6237 dump_insn_slim (lra_dump_file, insn);
6238 fprintf (lra_dump_file,
6239 " ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]\n");
6243 invariant_ptr->insn = curr_insn;
6244 return succ_p;
6247 /* Check only registers living at the current program point in the
6248 current EBB. */
6249 static bitmap_head live_regs;
6251 /* Update live info in EBB given by its HEAD and TAIL insns after
6252 inheritance/split transformation. The function removes dead moves
6253 too. */
6254 static void
6255 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
6257 unsigned int j;
6258 int i, regno;
6259 bool live_p;
6260 rtx_insn *prev_insn;
6261 rtx set;
6262 bool remove_p;
6263 basic_block last_bb, prev_bb, curr_bb;
6264 bitmap_iterator bi;
6265 struct lra_insn_reg *reg;
6266 edge e;
6267 edge_iterator ei;
6269 last_bb = BLOCK_FOR_INSN (tail);
6270 prev_bb = NULL;
6271 for (curr_insn = tail;
6272 curr_insn != PREV_INSN (head);
6273 curr_insn = prev_insn)
6275 prev_insn = PREV_INSN (curr_insn);
6276 /* We need to process empty blocks too. They contain
6277 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
6278 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
6279 continue;
6280 curr_bb = BLOCK_FOR_INSN (curr_insn);
6281 if (curr_bb != prev_bb)
6283 if (prev_bb != NULL)
6285 /* Update df_get_live_in (prev_bb): */
6286 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6287 if (bitmap_bit_p (&live_regs, j))
6288 bitmap_set_bit (df_get_live_in (prev_bb), j);
6289 else
6290 bitmap_clear_bit (df_get_live_in (prev_bb), j);
6292 if (curr_bb != last_bb)
6294 /* Update df_get_live_out (curr_bb): */
6295 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6297 live_p = bitmap_bit_p (&live_regs, j);
6298 if (! live_p)
6299 FOR_EACH_EDGE (e, ei, curr_bb->succs)
6300 if (bitmap_bit_p (df_get_live_in (e->dest), j))
6302 live_p = true;
6303 break;
6305 if (live_p)
6306 bitmap_set_bit (df_get_live_out (curr_bb), j);
6307 else
6308 bitmap_clear_bit (df_get_live_out (curr_bb), j);
6311 prev_bb = curr_bb;
6312 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
6314 if (! NONDEBUG_INSN_P (curr_insn))
6315 continue;
6316 curr_id = lra_get_insn_recog_data (curr_insn);
6317 curr_static_id = curr_id->insn_static_data;
6318 remove_p = false;
6319 if ((set = single_set (curr_insn)) != NULL_RTX
6320 && REG_P (SET_DEST (set))
6321 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
6322 && SET_DEST (set) != pic_offset_table_rtx
6323 && bitmap_bit_p (&check_only_regs, regno)
6324 && ! bitmap_bit_p (&live_regs, regno))
6325 remove_p = true;
6326 /* See which defined values die here. */
6327 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6328 if (reg->type == OP_OUT && ! reg->subreg_p)
6329 bitmap_clear_bit (&live_regs, reg->regno);
6330 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6331 if (reg->type == OP_OUT && ! reg->subreg_p)
6332 bitmap_clear_bit (&live_regs, reg->regno);
6333 if (curr_id->arg_hard_regs != NULL)
6334 /* Make clobbered argument hard registers die. */
6335 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6336 if (regno >= FIRST_PSEUDO_REGISTER)
6337 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
6338 /* Mark each used value as live. */
6339 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6340 if (reg->type != OP_OUT
6341 && bitmap_bit_p (&check_only_regs, reg->regno))
6342 bitmap_set_bit (&live_regs, reg->regno);
6343 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6344 if (reg->type != OP_OUT
6345 && bitmap_bit_p (&check_only_regs, reg->regno))
6346 bitmap_set_bit (&live_regs, reg->regno);
6347 if (curr_id->arg_hard_regs != NULL)
6348 /* Make used argument hard registers live. */
6349 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6350 if (regno < FIRST_PSEUDO_REGISTER
6351 && bitmap_bit_p (&check_only_regs, regno))
6352 bitmap_set_bit (&live_regs, regno);
6353 /* It is quite important to remove dead move insns because it
6354 means removing dead store. We don't need to process them for
6355 constraints. */
6356 if (remove_p)
6358 if (lra_dump_file != NULL)
6360 fprintf (lra_dump_file, " Removing dead insn:\n ");
6361 dump_insn_slim (lra_dump_file, curr_insn);
6363 lra_set_insn_deleted (curr_insn);
6368 /* The structure describes info to do an inheritance for the current
6369 insn. We need to collect such info first before doing the
6370 transformations because the transformations change the insn
6371 internal representation. */
6372 struct to_inherit
6374 /* Original regno. */
6375 int regno;
6376 /* Subsequent insns which can inherit original reg value. */
6377 rtx insns;
6380 /* Array containing all info for doing inheritance from the current
6381 insn. */
6382 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
6384 /* Number elements in the previous array. */
6385 static int to_inherit_num;
6387 /* Add inheritance info REGNO and INSNS. Their meaning is described in
6388 structure to_inherit. */
6389 static void
6390 add_to_inherit (int regno, rtx insns)
6392 int i;
6394 for (i = 0; i < to_inherit_num; i++)
6395 if (to_inherit[i].regno == regno)
6396 return;
6397 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
6398 to_inherit[to_inherit_num].regno = regno;
6399 to_inherit[to_inherit_num++].insns = insns;
6402 /* Return the last non-debug insn in basic block BB, or the block begin
6403 note if none. */
6404 static rtx_insn *
6405 get_last_insertion_point (basic_block bb)
6407 rtx_insn *insn;
6409 FOR_BB_INSNS_REVERSE (bb, insn)
6410 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
6411 return insn;
6412 gcc_unreachable ();
6415 /* Set up RES by registers living on edges FROM except the edge (FROM,
6416 TO) or by registers set up in a jump insn in BB FROM. */
6417 static void
6418 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
6420 rtx_insn *last;
6421 struct lra_insn_reg *reg;
6422 edge e;
6423 edge_iterator ei;
6425 lra_assert (to != NULL);
6426 bitmap_clear (res);
6427 FOR_EACH_EDGE (e, ei, from->succs)
6428 if (e->dest != to)
6429 bitmap_ior_into (res, df_get_live_in (e->dest));
6430 last = get_last_insertion_point (from);
6431 if (! JUMP_P (last))
6432 return;
6433 curr_id = lra_get_insn_recog_data (last);
6434 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6435 if (reg->type != OP_IN)
6436 bitmap_set_bit (res, reg->regno);
6439 /* Used as a temporary results of some bitmap calculations. */
6440 static bitmap_head temp_bitmap;
6442 /* We split for reloads of small class of hard regs. The following
6443 defines how many hard regs the class should have to be qualified as
6444 small. The code is mostly oriented to x86/x86-64 architecture
6445 where some insns need to use only specific register or pair of
6446 registers and these register can live in RTL explicitly, e.g. for
6447 parameter passing. */
6448 static const int max_small_class_regs_num = 2;
6450 /* Do inheritance/split transformations in EBB starting with HEAD and
6451 finishing on TAIL. We process EBB insns in the reverse order.
6452 Return true if we did any inheritance/split transformation in the
6453 EBB.
6455 We should avoid excessive splitting which results in worse code
6456 because of inaccurate cost calculations for spilling new split
6457 pseudos in such case. To achieve this we do splitting only if
6458 register pressure is high in given basic block and there are reload
6459 pseudos requiring hard registers. We could do more register
6460 pressure calculations at any given program point to avoid necessary
6461 splitting even more but it is to expensive and the current approach
6462 works well enough. */
6463 static bool
6464 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
6466 int i, src_regno, dst_regno, nregs;
6467 bool change_p, succ_p, update_reloads_num_p;
6468 rtx_insn *prev_insn, *last_insn;
6469 rtx next_usage_insns, curr_set;
6470 enum reg_class cl;
6471 struct lra_insn_reg *reg;
6472 basic_block last_processed_bb, curr_bb = NULL;
6473 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
6474 bitmap to_process;
6475 unsigned int j;
6476 bitmap_iterator bi;
6477 bool head_p, after_p;
6479 change_p = false;
6480 curr_usage_insns_check++;
6481 clear_invariants ();
6482 reloads_num = calls_num = 0;
6483 for (unsigned int i = 0; i < NUM_ABI_IDS; ++i)
6484 last_call_for_abi[i] = 0;
6485 CLEAR_HARD_REG_SET (full_and_partial_call_clobbers);
6486 bitmap_clear (&check_only_regs);
6487 bitmap_clear (&invalid_invariant_regs);
6488 last_processed_bb = NULL;
6489 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6490 live_hard_regs = eliminable_regset | lra_no_alloc_regs;
6491 /* We don't process new insns generated in the loop. */
6492 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
6494 prev_insn = PREV_INSN (curr_insn);
6495 if (BLOCK_FOR_INSN (curr_insn) != NULL)
6496 curr_bb = BLOCK_FOR_INSN (curr_insn);
6497 if (last_processed_bb != curr_bb)
6499 /* We are at the end of BB. Add qualified living
6500 pseudos for potential splitting. */
6501 to_process = df_get_live_out (curr_bb);
6502 if (last_processed_bb != NULL)
6504 /* We are somewhere in the middle of EBB. */
6505 get_live_on_other_edges (curr_bb, last_processed_bb,
6506 &temp_bitmap);
6507 to_process = &temp_bitmap;
6509 last_processed_bb = curr_bb;
6510 last_insn = get_last_insertion_point (curr_bb);
6511 after_p = (! JUMP_P (last_insn)
6512 && (! CALL_P (last_insn)
6513 || (find_reg_note (last_insn,
6514 REG_NORETURN, NULL_RTX) == NULL_RTX
6515 && ! SIBLING_CALL_P (last_insn))));
6516 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6517 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
6519 if ((int) j >= lra_constraint_new_regno_start)
6520 break;
6521 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
6523 if (j < FIRST_PSEUDO_REGISTER)
6524 SET_HARD_REG_BIT (live_hard_regs, j);
6525 else
6526 add_to_hard_reg_set (&live_hard_regs,
6527 PSEUDO_REGNO_MODE (j),
6528 reg_renumber[j]);
6529 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
6533 src_regno = dst_regno = -1;
6534 curr_set = single_set (curr_insn);
6535 if (curr_set != NULL_RTX && REG_P (SET_DEST (curr_set)))
6536 dst_regno = REGNO (SET_DEST (curr_set));
6537 if (curr_set != NULL_RTX && REG_P (SET_SRC (curr_set)))
6538 src_regno = REGNO (SET_SRC (curr_set));
6539 update_reloads_num_p = true;
6540 if (src_regno < lra_constraint_new_regno_start
6541 && src_regno >= FIRST_PSEUDO_REGISTER
6542 && reg_renumber[src_regno] < 0
6543 && dst_regno >= lra_constraint_new_regno_start
6544 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
6546 /* 'reload_pseudo <- original_pseudo'. */
6547 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6548 reloads_num++;
6549 update_reloads_num_p = false;
6550 succ_p = false;
6551 if (usage_insns[src_regno].check == curr_usage_insns_check
6552 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
6553 succ_p = inherit_reload_reg (false, src_regno, cl,
6554 curr_insn, next_usage_insns);
6555 if (succ_p)
6556 change_p = true;
6557 else
6558 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6559 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6560 potential_reload_hard_regs |= reg_class_contents[cl];
6562 else if (src_regno < 0
6563 && dst_regno >= lra_constraint_new_regno_start
6564 && invariant_p (SET_SRC (curr_set))
6565 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS
6566 && ! bitmap_bit_p (&invalid_invariant_regs, dst_regno)
6567 && ! bitmap_bit_p (&invalid_invariant_regs,
6568 ORIGINAL_REGNO(regno_reg_rtx[dst_regno])))
6570 /* 'reload_pseudo <- invariant'. */
6571 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6572 reloads_num++;
6573 update_reloads_num_p = false;
6574 if (process_invariant_for_inheritance (SET_DEST (curr_set), SET_SRC (curr_set)))
6575 change_p = true;
6576 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6577 potential_reload_hard_regs |= reg_class_contents[cl];
6579 else if (src_regno >= lra_constraint_new_regno_start
6580 && dst_regno < lra_constraint_new_regno_start
6581 && dst_regno >= FIRST_PSEUDO_REGISTER
6582 && reg_renumber[dst_regno] < 0
6583 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
6584 && usage_insns[dst_regno].check == curr_usage_insns_check
6585 && (next_usage_insns
6586 = usage_insns[dst_regno].insns) != NULL_RTX)
6588 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6589 reloads_num++;
6590 update_reloads_num_p = false;
6591 /* 'original_pseudo <- reload_pseudo'. */
6592 if (! JUMP_P (curr_insn)
6593 && inherit_reload_reg (true, dst_regno, cl,
6594 curr_insn, next_usage_insns))
6595 change_p = true;
6596 /* Invalidate. */
6597 usage_insns[dst_regno].check = 0;
6598 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6599 potential_reload_hard_regs |= reg_class_contents[cl];
6601 else if (INSN_P (curr_insn))
6603 int iter;
6604 int max_uid = get_max_uid ();
6606 curr_id = lra_get_insn_recog_data (curr_insn);
6607 curr_static_id = curr_id->insn_static_data;
6608 to_inherit_num = 0;
6609 /* Process insn definitions. */
6610 for (iter = 0; iter < 2; iter++)
6611 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6612 reg != NULL;
6613 reg = reg->next)
6614 if (reg->type != OP_IN
6615 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
6617 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
6618 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
6619 && usage_insns[dst_regno].check == curr_usage_insns_check
6620 && (next_usage_insns
6621 = usage_insns[dst_regno].insns) != NULL_RTX)
6623 struct lra_insn_reg *r;
6625 for (r = curr_id->regs; r != NULL; r = r->next)
6626 if (r->type != OP_OUT && r->regno == dst_regno)
6627 break;
6628 /* Don't do inheritance if the pseudo is also
6629 used in the insn. */
6630 if (r == NULL)
6631 /* We cannot do inheritance right now
6632 because the current insn reg info (chain
6633 regs) can change after that. */
6634 add_to_inherit (dst_regno, next_usage_insns);
6636 /* We cannot process one reg twice here because of
6637 usage_insns invalidation. */
6638 if ((dst_regno < FIRST_PSEUDO_REGISTER
6639 || reg_renumber[dst_regno] >= 0)
6640 && ! reg->subreg_p && reg->type != OP_IN)
6642 HARD_REG_SET s;
6644 if (split_if_necessary (dst_regno, reg->biggest_mode,
6645 potential_reload_hard_regs,
6646 false, curr_insn, max_uid))
6647 change_p = true;
6648 CLEAR_HARD_REG_SET (s);
6649 if (dst_regno < FIRST_PSEUDO_REGISTER)
6650 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
6651 else
6652 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
6653 reg_renumber[dst_regno]);
6654 live_hard_regs &= ~s;
6655 potential_reload_hard_regs &= ~s;
6657 /* We should invalidate potential inheritance or
6658 splitting for the current insn usages to the next
6659 usage insns (see code below) as the output pseudo
6660 prevents this. */
6661 if ((dst_regno >= FIRST_PSEUDO_REGISTER
6662 && reg_renumber[dst_regno] < 0)
6663 || (reg->type == OP_OUT && ! reg->subreg_p
6664 && (dst_regno < FIRST_PSEUDO_REGISTER
6665 || reg_renumber[dst_regno] >= 0)))
6667 /* Invalidate and mark definitions. */
6668 if (dst_regno >= FIRST_PSEUDO_REGISTER)
6669 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
6670 else
6672 nregs = hard_regno_nregs (dst_regno,
6673 reg->biggest_mode);
6674 for (i = 0; i < nregs; i++)
6675 usage_insns[dst_regno + i].check
6676 = -(int) INSN_UID (curr_insn);
6680 /* Process clobbered call regs. */
6681 if (curr_id->arg_hard_regs != NULL)
6682 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6683 if (dst_regno >= FIRST_PSEUDO_REGISTER)
6684 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
6685 = -(int) INSN_UID (curr_insn);
6686 if (! JUMP_P (curr_insn))
6687 for (i = 0; i < to_inherit_num; i++)
6688 if (inherit_reload_reg (true, to_inherit[i].regno,
6689 ALL_REGS, curr_insn,
6690 to_inherit[i].insns))
6691 change_p = true;
6692 if (CALL_P (curr_insn))
6694 rtx cheap, pat, dest;
6695 rtx_insn *restore;
6696 int regno, hard_regno;
6698 calls_num++;
6699 function_abi callee_abi = insn_callee_abi (curr_insn);
6700 last_call_for_abi[callee_abi.id ()] = calls_num;
6701 full_and_partial_call_clobbers
6702 |= callee_abi.full_and_partial_reg_clobbers ();
6703 if ((cheap = find_reg_note (curr_insn,
6704 REG_RETURNED, NULL_RTX)) != NULL_RTX
6705 && ((cheap = XEXP (cheap, 0)), true)
6706 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
6707 && (hard_regno = reg_renumber[regno]) >= 0
6708 && usage_insns[regno].check == curr_usage_insns_check
6709 /* If there are pending saves/restores, the
6710 optimization is not worth. */
6711 && usage_insns[regno].calls_num == calls_num - 1
6712 && callee_abi.clobbers_reg_p (GET_MODE (cheap), hard_regno))
6714 /* Restore the pseudo from the call result as
6715 REG_RETURNED note says that the pseudo value is
6716 in the call result and the pseudo is an argument
6717 of the call. */
6718 pat = PATTERN (curr_insn);
6719 if (GET_CODE (pat) == PARALLEL)
6720 pat = XVECEXP (pat, 0, 0);
6721 dest = SET_DEST (pat);
6722 /* For multiple return values dest is PARALLEL.
6723 Currently we handle only single return value case. */
6724 if (REG_P (dest))
6726 start_sequence ();
6727 emit_move_insn (cheap, copy_rtx (dest));
6728 restore = get_insns ();
6729 end_sequence ();
6730 lra_process_new_insns (curr_insn, NULL, restore,
6731 "Inserting call parameter restore");
6732 /* We don't need to save/restore of the pseudo from
6733 this call. */
6734 usage_insns[regno].calls_num = calls_num;
6735 remove_from_hard_reg_set
6736 (&full_and_partial_call_clobbers,
6737 GET_MODE (cheap), hard_regno);
6738 bitmap_set_bit (&check_only_regs, regno);
6742 to_inherit_num = 0;
6743 /* Process insn usages. */
6744 for (iter = 0; iter < 2; iter++)
6745 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6746 reg != NULL;
6747 reg = reg->next)
6748 if ((reg->type != OP_OUT
6749 || (reg->type == OP_OUT && reg->subreg_p))
6750 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
6752 if (src_regno >= FIRST_PSEUDO_REGISTER
6753 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
6755 if (usage_insns[src_regno].check == curr_usage_insns_check
6756 && (next_usage_insns
6757 = usage_insns[src_regno].insns) != NULL_RTX
6758 && NONDEBUG_INSN_P (curr_insn))
6759 add_to_inherit (src_regno, next_usage_insns);
6760 else if (usage_insns[src_regno].check
6761 != -(int) INSN_UID (curr_insn))
6762 /* Add usages but only if the reg is not set up
6763 in the same insn. */
6764 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6766 else if (src_regno < FIRST_PSEUDO_REGISTER
6767 || reg_renumber[src_regno] >= 0)
6769 bool before_p;
6770 rtx_insn *use_insn = curr_insn;
6772 before_p = (JUMP_P (curr_insn)
6773 || (CALL_P (curr_insn) && reg->type == OP_IN));
6774 if (NONDEBUG_INSN_P (curr_insn)
6775 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
6776 && split_if_necessary (src_regno, reg->biggest_mode,
6777 potential_reload_hard_regs,
6778 before_p, curr_insn, max_uid))
6780 if (reg->subreg_p)
6781 check_and_force_assignment_correctness_p = true;
6782 change_p = true;
6783 /* Invalidate. */
6784 usage_insns[src_regno].check = 0;
6785 if (before_p)
6786 use_insn = PREV_INSN (curr_insn);
6788 if (NONDEBUG_INSN_P (curr_insn))
6790 if (src_regno < FIRST_PSEUDO_REGISTER)
6791 add_to_hard_reg_set (&live_hard_regs,
6792 reg->biggest_mode, src_regno);
6793 else
6794 add_to_hard_reg_set (&live_hard_regs,
6795 PSEUDO_REGNO_MODE (src_regno),
6796 reg_renumber[src_regno]);
6798 if (src_regno >= FIRST_PSEUDO_REGISTER)
6799 add_next_usage_insn (src_regno, use_insn, reloads_num);
6800 else
6802 for (i = 0; i < hard_regno_nregs (src_regno, reg->biggest_mode); i++)
6803 add_next_usage_insn (src_regno + i, use_insn, reloads_num);
6807 /* Process used call regs. */
6808 if (curr_id->arg_hard_regs != NULL)
6809 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6810 if (src_regno < FIRST_PSEUDO_REGISTER)
6812 SET_HARD_REG_BIT (live_hard_regs, src_regno);
6813 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6815 for (i = 0; i < to_inherit_num; i++)
6817 src_regno = to_inherit[i].regno;
6818 if (inherit_reload_reg (false, src_regno, ALL_REGS,
6819 curr_insn, to_inherit[i].insns))
6820 change_p = true;
6821 else
6822 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6825 if (update_reloads_num_p
6826 && NONDEBUG_INSN_P (curr_insn) && curr_set != NULL_RTX)
6828 int regno = -1;
6829 if ((REG_P (SET_DEST (curr_set))
6830 && (regno = REGNO (SET_DEST (curr_set))) >= lra_constraint_new_regno_start
6831 && reg_renumber[regno] < 0
6832 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
6833 || (REG_P (SET_SRC (curr_set))
6834 && (regno = REGNO (SET_SRC (curr_set))) >= lra_constraint_new_regno_start
6835 && reg_renumber[regno] < 0
6836 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
6838 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6839 reloads_num++;
6840 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6841 potential_reload_hard_regs |= reg_class_contents[cl];
6844 if (NONDEBUG_INSN_P (curr_insn))
6846 int regno;
6848 /* Invalidate invariants with changed regs. */
6849 curr_id = lra_get_insn_recog_data (curr_insn);
6850 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6851 if (reg->type != OP_IN)
6853 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6854 bitmap_set_bit (&invalid_invariant_regs,
6855 ORIGINAL_REGNO (regno_reg_rtx[reg->regno]));
6857 curr_static_id = curr_id->insn_static_data;
6858 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6859 if (reg->type != OP_IN)
6860 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6861 if (curr_id->arg_hard_regs != NULL)
6862 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6863 if (regno >= FIRST_PSEUDO_REGISTER)
6864 bitmap_set_bit (&invalid_invariant_regs,
6865 regno - FIRST_PSEUDO_REGISTER);
6867 /* We reached the start of the current basic block. */
6868 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
6869 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
6871 /* We reached the beginning of the current block -- do
6872 rest of spliting in the current BB. */
6873 to_process = df_get_live_in (curr_bb);
6874 if (BLOCK_FOR_INSN (head) != curr_bb)
6876 /* We are somewhere in the middle of EBB. */
6877 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
6878 curr_bb, &temp_bitmap);
6879 to_process = &temp_bitmap;
6881 head_p = true;
6882 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
6884 if ((int) j >= lra_constraint_new_regno_start)
6885 break;
6886 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
6887 && usage_insns[j].check == curr_usage_insns_check
6888 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
6890 if (need_for_split_p (potential_reload_hard_regs, j))
6892 if (lra_dump_file != NULL && head_p)
6894 fprintf (lra_dump_file,
6895 " ----------------------------------\n");
6896 head_p = false;
6898 if (split_reg (false, j, bb_note (curr_bb),
6899 next_usage_insns, NULL))
6900 change_p = true;
6902 usage_insns[j].check = 0;
6907 return change_p;
6910 /* This value affects EBB forming. If probability of edge from EBB to
6911 a BB is not greater than the following value, we don't add the BB
6912 to EBB. */
6913 #define EBB_PROBABILITY_CUTOFF \
6914 ((REG_BR_PROB_BASE * param_lra_inheritance_ebb_probability_cutoff) / 100)
6916 /* Current number of inheritance/split iteration. */
6917 int lra_inheritance_iter;
6919 /* Entry function for inheritance/split pass. */
6920 void
6921 lra_inheritance (void)
6923 int i;
6924 basic_block bb, start_bb;
6925 edge e;
6927 lra_inheritance_iter++;
6928 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6929 return;
6930 timevar_push (TV_LRA_INHERITANCE);
6931 if (lra_dump_file != NULL)
6932 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
6933 lra_inheritance_iter);
6934 curr_usage_insns_check = 0;
6935 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
6936 for (i = 0; i < lra_constraint_new_regno_start; i++)
6937 usage_insns[i].check = 0;
6938 bitmap_initialize (&check_only_regs, &reg_obstack);
6939 bitmap_initialize (&invalid_invariant_regs, &reg_obstack);
6940 bitmap_initialize (&live_regs, &reg_obstack);
6941 bitmap_initialize (&temp_bitmap, &reg_obstack);
6942 bitmap_initialize (&ebb_global_regs, &reg_obstack);
6943 FOR_EACH_BB_FN (bb, cfun)
6945 start_bb = bb;
6946 if (lra_dump_file != NULL)
6947 fprintf (lra_dump_file, "EBB");
6948 /* Form a EBB starting with BB. */
6949 bitmap_clear (&ebb_global_regs);
6950 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
6951 for (;;)
6953 if (lra_dump_file != NULL)
6954 fprintf (lra_dump_file, " %d", bb->index);
6955 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
6956 || LABEL_P (BB_HEAD (bb->next_bb)))
6957 break;
6958 e = find_fallthru_edge (bb->succs);
6959 if (! e)
6960 break;
6961 if (e->probability.initialized_p ()
6962 && e->probability.to_reg_br_prob_base () < EBB_PROBABILITY_CUTOFF)
6963 break;
6964 bb = bb->next_bb;
6966 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
6967 if (lra_dump_file != NULL)
6968 fprintf (lra_dump_file, "\n");
6969 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
6970 /* Remember that the EBB head and tail can change in
6971 inherit_in_ebb. */
6972 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
6974 bitmap_release (&ebb_global_regs);
6975 bitmap_release (&temp_bitmap);
6976 bitmap_release (&live_regs);
6977 bitmap_release (&invalid_invariant_regs);
6978 bitmap_release (&check_only_regs);
6979 free (usage_insns);
6981 timevar_pop (TV_LRA_INHERITANCE);
6986 /* This page contains code to undo failed inheritance/split
6987 transformations. */
6989 /* Current number of iteration undoing inheritance/split. */
6990 int lra_undo_inheritance_iter;
6992 /* Fix BB live info LIVE after removing pseudos created on pass doing
6993 inheritance/split which are REMOVED_PSEUDOS. */
6994 static void
6995 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
6997 unsigned int regno;
6998 bitmap_iterator bi;
7000 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
7001 if (bitmap_clear_bit (live, regno)
7002 && REG_P (lra_reg_info[regno].restore_rtx))
7003 bitmap_set_bit (live, REGNO (lra_reg_info[regno].restore_rtx));
7006 /* Return regno of the (subreg of) REG. Otherwise, return a negative
7007 number. */
7008 static int
7009 get_regno (rtx reg)
7011 if (GET_CODE (reg) == SUBREG)
7012 reg = SUBREG_REG (reg);
7013 if (REG_P (reg))
7014 return REGNO (reg);
7015 return -1;
7018 /* Delete a move INSN with destination reg DREGNO and a previous
7019 clobber insn with the same regno. The inheritance/split code can
7020 generate moves with preceding clobber and when we delete such moves
7021 we should delete the clobber insn too to keep the correct life
7022 info. */
7023 static void
7024 delete_move_and_clobber (rtx_insn *insn, int dregno)
7026 rtx_insn *prev_insn = PREV_INSN (insn);
7028 lra_set_insn_deleted (insn);
7029 lra_assert (dregno >= 0);
7030 if (prev_insn != NULL && NONDEBUG_INSN_P (prev_insn)
7031 && GET_CODE (PATTERN (prev_insn)) == CLOBBER
7032 && dregno == get_regno (XEXP (PATTERN (prev_insn), 0)))
7033 lra_set_insn_deleted (prev_insn);
7036 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
7037 return true if we did any change. The undo transformations for
7038 inheritance looks like
7039 i <- i2
7040 p <- i => p <- i2
7041 or removing
7042 p <- i, i <- p, and i <- i3
7043 where p is original pseudo from which inheritance pseudo i was
7044 created, i and i3 are removed inheritance pseudos, i2 is another
7045 not removed inheritance pseudo. All split pseudos or other
7046 occurrences of removed inheritance pseudos are changed on the
7047 corresponding original pseudos.
7049 The function also schedules insns changed and created during
7050 inheritance/split pass for processing by the subsequent constraint
7051 pass. */
7052 static bool
7053 remove_inheritance_pseudos (bitmap remove_pseudos)
7055 basic_block bb;
7056 int regno, sregno, prev_sregno, dregno;
7057 rtx restore_rtx;
7058 rtx set, prev_set;
7059 rtx_insn *prev_insn;
7060 bool change_p, done_p;
7062 change_p = ! bitmap_empty_p (remove_pseudos);
7063 /* We cannot finish the function right away if CHANGE_P is true
7064 because we need to marks insns affected by previous
7065 inheritance/split pass for processing by the subsequent
7066 constraint pass. */
7067 FOR_EACH_BB_FN (bb, cfun)
7069 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
7070 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
7071 FOR_BB_INSNS_REVERSE (bb, curr_insn)
7073 if (! INSN_P (curr_insn))
7074 continue;
7075 done_p = false;
7076 sregno = dregno = -1;
7077 if (change_p && NONDEBUG_INSN_P (curr_insn)
7078 && (set = single_set (curr_insn)) != NULL_RTX)
7080 dregno = get_regno (SET_DEST (set));
7081 sregno = get_regno (SET_SRC (set));
7084 if (sregno >= 0 && dregno >= 0)
7086 if (bitmap_bit_p (remove_pseudos, dregno)
7087 && ! REG_P (lra_reg_info[dregno].restore_rtx))
7089 /* invariant inheritance pseudo <- original pseudo */
7090 if (lra_dump_file != NULL)
7092 fprintf (lra_dump_file, " Removing invariant inheritance:\n");
7093 dump_insn_slim (lra_dump_file, curr_insn);
7094 fprintf (lra_dump_file, "\n");
7096 delete_move_and_clobber (curr_insn, dregno);
7097 done_p = true;
7099 else if (bitmap_bit_p (remove_pseudos, sregno)
7100 && ! REG_P (lra_reg_info[sregno].restore_rtx))
7102 /* reload pseudo <- invariant inheritance pseudo */
7103 start_sequence ();
7104 /* We cannot just change the source. It might be
7105 an insn different from the move. */
7106 emit_insn (lra_reg_info[sregno].restore_rtx);
7107 rtx_insn *new_insns = get_insns ();
7108 end_sequence ();
7109 lra_assert (single_set (new_insns) != NULL
7110 && SET_DEST (set) == SET_DEST (single_set (new_insns)));
7111 lra_process_new_insns (curr_insn, NULL, new_insns,
7112 "Changing reload<-invariant inheritance");
7113 delete_move_and_clobber (curr_insn, dregno);
7114 done_p = true;
7116 else if ((bitmap_bit_p (remove_pseudos, sregno)
7117 && (get_regno (lra_reg_info[sregno].restore_rtx) == dregno
7118 || (bitmap_bit_p (remove_pseudos, dregno)
7119 && get_regno (lra_reg_info[sregno].restore_rtx) >= 0
7120 && (get_regno (lra_reg_info[sregno].restore_rtx)
7121 == get_regno (lra_reg_info[dregno].restore_rtx)))))
7122 || (bitmap_bit_p (remove_pseudos, dregno)
7123 && get_regno (lra_reg_info[dregno].restore_rtx) == sregno))
7124 /* One of the following cases:
7125 original <- removed inheritance pseudo
7126 removed inherit pseudo <- another removed inherit pseudo
7127 removed inherit pseudo <- original pseudo
7129 removed_split_pseudo <- original_reg
7130 original_reg <- removed_split_pseudo */
7132 if (lra_dump_file != NULL)
7134 fprintf (lra_dump_file, " Removing %s:\n",
7135 bitmap_bit_p (&lra_split_regs, sregno)
7136 || bitmap_bit_p (&lra_split_regs, dregno)
7137 ? "split" : "inheritance");
7138 dump_insn_slim (lra_dump_file, curr_insn);
7140 delete_move_and_clobber (curr_insn, dregno);
7141 done_p = true;
7143 else if (bitmap_bit_p (remove_pseudos, sregno)
7144 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
7146 /* Search the following pattern:
7147 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
7148 original_pseudo <- inherit_or_split_pseudo1
7149 where the 2nd insn is the current insn and
7150 inherit_or_split_pseudo2 is not removed. If it is found,
7151 change the current insn onto:
7152 original_pseudo <- inherit_or_split_pseudo2. */
7153 for (prev_insn = PREV_INSN (curr_insn);
7154 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
7155 prev_insn = PREV_INSN (prev_insn))
7157 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
7158 && (prev_set = single_set (prev_insn)) != NULL_RTX
7159 /* There should be no subregs in insn we are
7160 searching because only the original reg might
7161 be in subreg when we changed the mode of
7162 load/store for splitting. */
7163 && REG_P (SET_DEST (prev_set))
7164 && REG_P (SET_SRC (prev_set))
7165 && (int) REGNO (SET_DEST (prev_set)) == sregno
7166 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
7167 >= FIRST_PSEUDO_REGISTER)
7168 && (lra_reg_info[prev_sregno].restore_rtx == NULL_RTX
7170 /* As we consider chain of inheritance or
7171 splitting described in above comment we should
7172 check that sregno and prev_sregno were
7173 inheritance/split pseudos created from the
7174 same original regno. */
7175 (get_regno (lra_reg_info[sregno].restore_rtx) >= 0
7176 && (get_regno (lra_reg_info[sregno].restore_rtx)
7177 == get_regno (lra_reg_info[prev_sregno].restore_rtx))))
7178 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
7180 lra_assert (GET_MODE (SET_SRC (prev_set))
7181 == GET_MODE (regno_reg_rtx[sregno]));
7182 /* Although we have a single set, the insn can
7183 contain more one sregno register occurrence
7184 as a source. Change all occurrences. */
7185 lra_substitute_pseudo_within_insn (curr_insn, sregno,
7186 SET_SRC (prev_set),
7187 false);
7188 /* As we are finishing with processing the insn
7189 here, check the destination too as it might
7190 inheritance pseudo for another pseudo. */
7191 if (bitmap_bit_p (remove_pseudos, dregno)
7192 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
7193 && (restore_rtx
7194 = lra_reg_info[dregno].restore_rtx) != NULL_RTX)
7196 if (GET_CODE (SET_DEST (set)) == SUBREG)
7197 SUBREG_REG (SET_DEST (set)) = restore_rtx;
7198 else
7199 SET_DEST (set) = restore_rtx;
7201 lra_push_insn_and_update_insn_regno_info (curr_insn);
7202 lra_set_used_insn_alternative_by_uid
7203 (INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
7204 done_p = true;
7205 if (lra_dump_file != NULL)
7207 fprintf (lra_dump_file, " Change reload insn:\n");
7208 dump_insn_slim (lra_dump_file, curr_insn);
7213 if (! done_p)
7215 struct lra_insn_reg *reg;
7216 bool restored_regs_p = false;
7217 bool kept_regs_p = false;
7219 curr_id = lra_get_insn_recog_data (curr_insn);
7220 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
7222 regno = reg->regno;
7223 restore_rtx = lra_reg_info[regno].restore_rtx;
7224 if (restore_rtx != NULL_RTX)
7226 if (change_p && bitmap_bit_p (remove_pseudos, regno))
7228 lra_substitute_pseudo_within_insn
7229 (curr_insn, regno, restore_rtx, false);
7230 restored_regs_p = true;
7232 else
7233 kept_regs_p = true;
7236 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
7238 /* The instruction has changed since the previous
7239 constraints pass. */
7240 lra_push_insn_and_update_insn_regno_info (curr_insn);
7241 lra_set_used_insn_alternative_by_uid
7242 (INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
7244 else if (restored_regs_p)
7245 /* The instruction has been restored to the form that
7246 it had during the previous constraints pass. */
7247 lra_update_insn_regno_info (curr_insn);
7248 if (restored_regs_p && lra_dump_file != NULL)
7250 fprintf (lra_dump_file, " Insn after restoring regs:\n");
7251 dump_insn_slim (lra_dump_file, curr_insn);
7256 return change_p;
7259 /* If optional reload pseudos failed to get a hard register or was not
7260 inherited, it is better to remove optional reloads. We do this
7261 transformation after undoing inheritance to figure out necessity to
7262 remove optional reloads easier. Return true if we do any
7263 change. */
7264 static bool
7265 undo_optional_reloads (void)
7267 bool change_p, keep_p;
7268 unsigned int regno, uid;
7269 bitmap_iterator bi, bi2;
7270 rtx_insn *insn;
7271 rtx set, src, dest;
7272 auto_bitmap removed_optional_reload_pseudos (&reg_obstack);
7274 bitmap_copy (removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
7275 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7277 keep_p = false;
7278 /* Keep optional reloads from previous subpasses. */
7279 if (lra_reg_info[regno].restore_rtx == NULL_RTX
7280 /* If the original pseudo changed its allocation, just
7281 removing the optional pseudo is dangerous as the original
7282 pseudo will have longer live range. */
7283 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] >= 0)
7284 keep_p = true;
7285 else if (reg_renumber[regno] >= 0)
7286 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
7288 insn = lra_insn_recog_data[uid]->insn;
7289 if ((set = single_set (insn)) == NULL_RTX)
7290 continue;
7291 src = SET_SRC (set);
7292 dest = SET_DEST (set);
7293 if ((! REG_P (src) && ! SUBREG_P (src))
7294 || (! REG_P (dest) && ! SUBREG_P (dest)))
7295 continue;
7296 if (get_regno (dest) == (int) regno
7297 /* Ignore insn for optional reloads itself. */
7298 && (get_regno (lra_reg_info[regno].restore_rtx)
7299 != get_regno (src))
7300 /* Check only inheritance on last inheritance pass. */
7301 && get_regno (src) >= new_regno_start
7302 /* Check that the optional reload was inherited. */
7303 && bitmap_bit_p (&lra_inheritance_pseudos, get_regno (src)))
7305 keep_p = true;
7306 break;
7309 if (keep_p)
7311 bitmap_clear_bit (removed_optional_reload_pseudos, regno);
7312 if (lra_dump_file != NULL)
7313 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
7316 change_p = ! bitmap_empty_p (removed_optional_reload_pseudos);
7317 auto_bitmap insn_bitmap (&reg_obstack);
7318 EXECUTE_IF_SET_IN_BITMAP (removed_optional_reload_pseudos, 0, regno, bi)
7320 if (lra_dump_file != NULL)
7321 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
7322 bitmap_copy (insn_bitmap, &lra_reg_info[regno].insn_bitmap);
7323 EXECUTE_IF_SET_IN_BITMAP (insn_bitmap, 0, uid, bi2)
7325 /* We may have already removed a clobber. */
7326 if (!lra_insn_recog_data[uid])
7327 continue;
7328 insn = lra_insn_recog_data[uid]->insn;
7329 if ((set = single_set (insn)) != NULL_RTX)
7331 src = SET_SRC (set);
7332 dest = SET_DEST (set);
7333 if ((REG_P (src) || SUBREG_P (src))
7334 && (REG_P (dest) || SUBREG_P (dest))
7335 && ((get_regno (src) == (int) regno
7336 && (get_regno (lra_reg_info[regno].restore_rtx)
7337 == get_regno (dest)))
7338 || (get_regno (dest) == (int) regno
7339 && (get_regno (lra_reg_info[regno].restore_rtx)
7340 == get_regno (src)))))
7342 if (lra_dump_file != NULL)
7344 fprintf (lra_dump_file, " Deleting move %u\n",
7345 INSN_UID (insn));
7346 dump_insn_slim (lra_dump_file, insn);
7348 delete_move_and_clobber (insn, get_regno (dest));
7349 continue;
7351 /* We should not worry about generation memory-memory
7352 moves here as if the corresponding inheritance did
7353 not work (inheritance pseudo did not get a hard reg),
7354 we remove the inheritance pseudo and the optional
7355 reload. */
7357 if (GET_CODE (PATTERN (insn)) == CLOBBER
7358 && REG_P (SET_DEST (insn))
7359 && get_regno (SET_DEST (insn)) == (int) regno)
7360 /* Refuse to remap clobbers to preexisting pseudos. */
7361 gcc_unreachable ();
7362 lra_substitute_pseudo_within_insn
7363 (insn, regno, lra_reg_info[regno].restore_rtx, false);
7364 lra_update_insn_regno_info (insn);
7365 if (lra_dump_file != NULL)
7367 fprintf (lra_dump_file,
7368 " Restoring original insn:\n");
7369 dump_insn_slim (lra_dump_file, insn);
7373 /* Clear restore_regnos. */
7374 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7375 lra_reg_info[regno].restore_rtx = NULL_RTX;
7376 return change_p;
7379 /* Entry function for undoing inheritance/split transformation. Return true
7380 if we did any RTL change in this pass. */
7381 bool
7382 lra_undo_inheritance (void)
7384 unsigned int regno;
7385 int hard_regno;
7386 int n_all_inherit, n_inherit, n_all_split, n_split;
7387 rtx restore_rtx;
7388 bitmap_iterator bi;
7389 bool change_p;
7391 lra_undo_inheritance_iter++;
7392 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
7393 return false;
7394 if (lra_dump_file != NULL)
7395 fprintf (lra_dump_file,
7396 "\n********** Undoing inheritance #%d: **********\n\n",
7397 lra_undo_inheritance_iter);
7398 auto_bitmap remove_pseudos (&reg_obstack);
7399 n_inherit = n_all_inherit = 0;
7400 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7401 if (lra_reg_info[regno].restore_rtx != NULL_RTX)
7403 n_all_inherit++;
7404 if (reg_renumber[regno] < 0
7405 /* If the original pseudo changed its allocation, just
7406 removing inheritance is dangerous as for changing
7407 allocation we used shorter live-ranges. */
7408 && (! REG_P (lra_reg_info[regno].restore_rtx)
7409 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] < 0))
7410 bitmap_set_bit (remove_pseudos, regno);
7411 else
7412 n_inherit++;
7414 if (lra_dump_file != NULL && n_all_inherit != 0)
7415 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
7416 n_inherit, n_all_inherit,
7417 (double) n_inherit / n_all_inherit * 100);
7418 n_split = n_all_split = 0;
7419 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7420 if ((restore_rtx = lra_reg_info[regno].restore_rtx) != NULL_RTX)
7422 int restore_regno = REGNO (restore_rtx);
7424 n_all_split++;
7425 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
7426 ? reg_renumber[restore_regno] : restore_regno);
7427 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
7428 bitmap_set_bit (remove_pseudos, regno);
7429 else
7431 n_split++;
7432 if (lra_dump_file != NULL)
7433 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
7434 regno, restore_regno);
7437 if (lra_dump_file != NULL && n_all_split != 0)
7438 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
7439 n_split, n_all_split,
7440 (double) n_split / n_all_split * 100);
7441 change_p = remove_inheritance_pseudos (remove_pseudos);
7442 /* Clear restore_regnos. */
7443 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7444 lra_reg_info[regno].restore_rtx = NULL_RTX;
7445 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7446 lra_reg_info[regno].restore_rtx = NULL_RTX;
7447 change_p = undo_optional_reloads () || change_p;
7448 return change_p;