Debian Codegraph @

gcc/lra-constraints.cc

raw
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2025 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <[email protected]>.
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21
22 /* This file contains code for 3 passes: constraint pass,
23 inheritance/split pass, and pass for undoing failed inheritance and
24 split.
25
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.
33
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.
41
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.
46
47 There is special code for preventing all LRA and this pass cycling
48 in case of bugs.
49
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.
54
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.
58
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:
62
63 reload_p1 <- p reload_p1 <- p
64 ... new_p <- reload_p1
65 ... => ...
66 reload_p2 <- p reload_p2 <- new_p
67
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*.
71
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.
75
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.
83
84 Splitting (transformation) is also done in EBB scope on the same
85 pass as the inheritance:
86
87 r <- ... or ... <- r r <- ... or ... <- r
88 ... s <- r (new insn -- save)
89 ... =>
90 ... r <- s (new insn -- restore)
91 ... <- r ... <- r
92
93 The *split pseudo* s is assigned to the hard register of the
94 original pseudo or hard register r.
95
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.
101
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. */
106
107 #undef REG_OK_STRICT
108
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "backend.h"
113 #include "hooks.h"
114 #include "target.h"
115 #include "rtl.h"
116 #include "tree.h"
117 #include "predict.h"
118 #include "df.h"
119 #include "memmodel.h"
120 #include "tm_p.h"
121 #include "expmed.h"
122 #include "optabs.h"
123 #include "regs.h"
124 #include "ira.h"
125 #include "recog.h"
126 #include "output.h"
127 #include "addresses.h"
128 #include "expr.h"
129 #include "cfgrtl.h"
130 #include "rtl-error.h"
131 #include "lra.h"
132 #include "lra-int.h"
133 #include "print-rtl.h"
134 #include "function-abi.h"
135 #include "rtl-iter.h"
136
137 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
138 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
139 reload insns. */
140 static int bb_reload_num;
141
142 /* The current insn being processed and corresponding its single set
143 (NULL otherwise), its data (basic block, the insn data, the insn
144 static data, and the mode of each operand). */
145 static rtx_insn *curr_insn;
146 static rtx curr_insn_set;
147 static basic_block curr_bb;
148 static lra_insn_recog_data_t curr_id;
149 static struct lra_static_insn_data *curr_static_id;
150 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
151 /* Mode of the register substituted by its equivalence with VOIDmode
152 (e.g. constant) and whose subreg is given operand of the current
153 insn. VOIDmode in all other cases. */
154 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
155 /* The first call insn after curr_insn within the EBB during inherit_in_ebb
156 or NULL outside of that function. */
157 static rtx_insn *first_call_insn;
158
159
160
161 /* Start numbers for new registers and insns at the current constraints
162 pass start. */
163 static int new_regno_start;
164 static int new_insn_uid_start;
165
166 /* If LOC is nonnull, strip any outer subreg from it. */
167 static inline rtx *
168 strip_subreg (rtx *loc)
169 {
170 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
171 }
172
173 /* Return hard regno of REGNO or if it is was not assigned to a hard
174 register, use a hard register from its allocno class. */
175 static int
176 get_try_hard_regno (int regno)
177 {
178 int hard_regno;
179 enum reg_class rclass;
180
181 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
182 hard_regno = lra_get_regno_hard_regno (regno);
183 if (hard_regno >= 0)
184 return hard_regno;
185 rclass = lra_get_allocno_class (regno);
186 if (rclass == NO_REGS)
187 return -1;
188 return ira_class_hard_regs[rclass][0];
189 }
190
191 /* Return the hard regno of X after removing its subreg. If X is not a
192 register or a subreg of a register, return -1. If X is a pseudo, use its
193 assignment. If X is a hard regno, return the final hard regno which will be
194 after elimination. */
195 static int
196 get_hard_regno (rtx x)
197 {
198 rtx reg;
199 int hard_regno;
200
201 reg = x;
202 if (SUBREG_P (x))
203 reg = SUBREG_REG (x);
204 if (! REG_P (reg))
205 return -1;
206 int regno = REGNO (reg);
207 if (HARD_REGISTER_NUM_P (regno))
208 hard_regno = lra_get_elimination_hard_regno (regno);
209 else
210 hard_regno = lra_get_regno_hard_regno (regno);
211 if (hard_regno < 0)
212 return -1;
213 if (SUBREG_P (x))
214 hard_regno += subreg_regno_offset (hard_regno, GET_MODE (reg),
215 SUBREG_BYTE (x), GET_MODE (x));
216 return hard_regno;
217 }
218
219 /* If REGNO is a hard register or has been allocated a hard register,
220 return the class of that register. If REGNO is a reload pseudo
221 created by the current constraints pass, return its allocno class.
222 Return NO_REGS otherwise. */
223 static enum reg_class
224 get_reg_class (int regno)
225 {
226 int hard_regno;
227
228 if (HARD_REGISTER_NUM_P (regno))
229 hard_regno = lra_get_elimination_hard_regno (regno);
230 else
231 hard_regno = lra_get_regno_hard_regno (regno);
232 if (hard_regno >= 0)
233 return REGNO_REG_CLASS (hard_regno);
234 if (regno >= new_regno_start)
235 return lra_get_allocno_class (regno);
236 return NO_REGS;
237 }
238
239 /* Return true if REG_CLASS has enough allocatable hard regs to keep value of
240 REG_MODE. */
241 static bool
242 enough_allocatable_hard_regs_p (enum reg_class reg_class,
243 enum machine_mode reg_mode)
244 {
245 int i, j, hard_regno, class_size, nregs;
246
247 if (hard_reg_set_subset_p (reg_class_contents[reg_class], lra_no_alloc_regs))
248 return false;
249 class_size = ira_class_hard_regs_num[reg_class];
250 for (i = 0; i < class_size; i++)
251 {
252 hard_regno = ira_class_hard_regs[reg_class][i];
253 nregs = hard_regno_nregs (hard_regno, reg_mode);
254 if (nregs == 1)
255 return true;
256 for (j = 0; j < nregs; j++)
257 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
258 || ! TEST_HARD_REG_BIT (reg_class_contents[reg_class],
259 hard_regno + j))
260 break;
261 if (j >= nregs)
262 return true;
263 }
264 return false;
265 }
266
267 /* True if C is a non-empty register class that has too few registers
268 to be safely used as a reload target class. */
269 #define SMALL_REGISTER_CLASS_P(C) \
270 (ira_class_hard_regs_num [(C)] == 1 \
271 || (ira_class_hard_regs_num [(C)] >= 1 \
272 && targetm.class_likely_spilled_p (C)))
273
274 /* Return true if REG satisfies (or will satisfy) reg class constraint
275 CL. Use elimination first if REG is a hard register. If REG is a
276 reload pseudo created by this constraints pass, assume that it will
277 be allocated a hard register from its allocno class, but allow that
278 class to be narrowed to CL if it is currently a superset of CL and
279 if either:
280
281 - ALLOW_ALL_RELOAD_CLASS_CHANGES_P is true or
282 - the instruction we're processing is not a reload move.
283
284 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
285 REGNO (reg), or NO_REGS if no change in its class was needed. */
286 static bool
287 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class,
288 bool allow_all_reload_class_changes_p = false)
289 {
290 enum reg_class rclass, common_class;
291 machine_mode reg_mode;
292 rtx src;
293 int regno = REGNO (reg);
294
295 if (new_class != NULL)
296 *new_class = NO_REGS;
297 if (regno < FIRST_PSEUDO_REGISTER)
298 {
299 rtx final_reg = reg;
300 rtx *final_loc = &final_reg;
301
302 lra_eliminate_reg_if_possible (final_loc);
303 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
304 }
305 reg_mode = GET_MODE (reg);
306 rclass = get_reg_class (regno);
307 src = curr_insn_set != NULL ? SET_SRC (curr_insn_set) : NULL;
308 if (regno < new_regno_start
309 /* Do not allow the constraints for reload instructions to
310 influence the classes of new pseudos. These reloads are
311 typically moves that have many alternatives, and restricting
312 reload pseudos for one alternative may lead to situations
313 where other reload pseudos are no longer allocatable. */
314 || (!allow_all_reload_class_changes_p
315 && INSN_UID (curr_insn) >= new_insn_uid_start
316 && src != NULL
317 && ((REG_P (src) || MEM_P (src))
318 || (GET_CODE (src) == SUBREG
319 && (REG_P (SUBREG_REG (src)) || MEM_P (SUBREG_REG (src)))))))
320 /* When we don't know what class will be used finally for reload
321 pseudos, we use ALL_REGS. */
322 return ((regno >= new_regno_start && rclass == ALL_REGS)
323 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
324 && ! hard_reg_set_subset_p (reg_class_contents[cl],
325 lra_no_alloc_regs)));
326 else
327 {
328 common_class = ira_reg_class_subset[rclass][cl];
329 if (new_class != NULL)
330 *new_class = common_class;
331 return (enough_allocatable_hard_regs_p (common_class, reg_mode)
332 /* Do not permit reload insn operand matching (new_class == NULL
333 case) if the new class is too small. */
334 && (new_class != NULL || common_class == rclass
335 || !SMALL_REGISTER_CLASS_P (common_class)));
336 }
337 }
338
339 /* Return true if REGNO satisfies a memory constraint. */
340 static bool
341 in_mem_p (int regno)
342 {
343 return get_reg_class (regno) == NO_REGS;
344 }
345
346 /* Return true if ADDR is a valid memory address for mode MODE in address
347 space AS, and check that each pseudo has the proper kind of hard
348 reg. */
349 static bool
350 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
351 rtx addr, addr_space_t as)
352 {
353 #ifdef GO_IF_LEGITIMATE_ADDRESS
354 lra_assert (ADDR_SPACE_GENERIC_P (as));
355 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
356 return false;
357
358 win:
359 return true;
360 #else
361 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as,
362 ERROR_MARK);
363 #endif
364 }
365
366 namespace {
367 /* Temporarily eliminates registers in an address (for the lifetime of
368 the object). */
369 class address_eliminator {
370 public:
371 address_eliminator (struct address_info *ad);
372 ~address_eliminator ();
373
374 private:
375 struct address_info *m_ad;
376 rtx *m_base_loc;
377 rtx m_base_reg;
378 rtx *m_index_loc;
379 rtx m_index_reg;
380 };
381 }
382
383 address_eliminator::address_eliminator (struct address_info *ad)
384 : m_ad (ad),
385 m_base_loc (strip_subreg (ad->base_term)),
386 m_base_reg (NULL_RTX),
387 m_index_loc (strip_subreg (ad->index_term)),
388 m_index_reg (NULL_RTX)
389 {
390 if (m_base_loc != NULL)
391 {
392 m_base_reg = *m_base_loc;
393 /* If we have non-legitimate address which is decomposed not in
394 the way we expected, don't do elimination here. In such case
395 the address will be reloaded and elimination will be done in
396 reload insn finally. */
397 if (REG_P (m_base_reg))
398 lra_eliminate_reg_if_possible (m_base_loc);
399 if (m_ad->base_term2 != NULL)
400 *m_ad->base_term2 = *m_ad->base_term;
401 }
402 if (m_index_loc != NULL)
403 {
404 m_index_reg = *m_index_loc;
405 if (REG_P (m_index_reg))
406 lra_eliminate_reg_if_possible (m_index_loc);
407 }
408 }
409
410 address_eliminator::~address_eliminator ()
411 {
412 if (m_base_loc && *m_base_loc != m_base_reg)
413 {
414 *m_base_loc = m_base_reg;
415 if (m_ad->base_term2 != NULL)
416 *m_ad->base_term2 = *m_ad->base_term;
417 }
418 if (m_index_loc && *m_index_loc != m_index_reg)
419 *m_index_loc = m_index_reg;
420 }
421
422 /* Return true if the eliminated form of AD is a legitimate target address.
423 If OP is a MEM, AD is the address within OP, otherwise OP should be
424 ignored. CONSTRAINT is one constraint that the operand may need
425 to meet. */
426 static bool
427 valid_address_p (rtx op, struct address_info *ad,
428 enum constraint_num constraint)
429 {
430 address_eliminator eliminator (ad);
431
432 /* Allow a memory OP if it matches CONSTRAINT, even if CONSTRAINT is more
433 forgiving than "m".
434 Need to extract memory from op for special memory constraint,
435 i.e. bcst_mem_operand in i386 backend. */
436 if (MEM_P (extract_mem_from_operand (op))
437 && insn_extra_relaxed_memory_constraint (constraint)
438 && constraint_satisfied_p (op, constraint))
439 return true;
440
441 return valid_address_p (ad->mode, *ad->outer, ad->as);
442 }
443
444 /* For special_memory_operand, it could be false for MEM_P (op),
445 i.e. bcst_mem_operand in i386 backend.
446 Extract and return real memory operand or op. */
447 rtx
448 extract_mem_from_operand (rtx op)
449 {
450 for (rtx x = op;; x = XEXP (x, 0))
451 {
452 if (MEM_P (x))
453 return x;
454 if (GET_RTX_LENGTH (GET_CODE (x)) != 1
455 || GET_RTX_FORMAT (GET_CODE (x))[0] != 'e')
456 break;
457 }
458 return op;
459 }
460
461 /* Return true if the eliminated form of memory reference OP satisfies
462 extra (special) memory constraint CONSTRAINT. */
463 static bool
464 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
465 {
466 struct address_info ad;
467 rtx mem = extract_mem_from_operand (op);
468 if (!MEM_P (mem))
469 return false;
470
471 decompose_mem_address (&ad, mem);
472 address_eliminator eliminator (&ad);
473 return constraint_satisfied_p (op, constraint);
474 }
475
476 /* Return true if the eliminated form of address AD satisfies extra
477 address constraint CONSTRAINT. */
478 static bool
479 satisfies_address_constraint_p (struct address_info *ad,
480 enum constraint_num constraint)
481 {
482 address_eliminator eliminator (ad);
483 return constraint_satisfied_p (*ad->outer, constraint);
484 }
485
486 /* Return true if the eliminated form of address OP satisfies extra
487 address constraint CONSTRAINT. */
488 static bool
489 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
490 {
491 struct address_info ad;
492
493 decompose_lea_address (&ad, &op);
494 return satisfies_address_constraint_p (&ad, constraint);
495 }
496
497 /* Initiate equivalences for LRA. As we keep original equivalences
498 before any elimination, we need to make copies otherwise any change
499 in insns might change the equivalences. */
500 void
501 lra_init_equiv (void)
502 {
503 ira_expand_reg_equiv ();
504 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
505 {
506 rtx res;
507
508 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
509 ira_reg_equiv[i].memory = copy_rtx (res);
510 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
511 ira_reg_equiv[i].invariant = copy_rtx (res);
512 }
513 }
514
515 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
516
517 /* Update equivalence for REGNO. We need to this as the equivalence
518 might contain other pseudos which are changed by their
519 equivalences. */
520 static void
521 update_equiv (int regno)
522 {
523 rtx x;
524
525 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
526 ira_reg_equiv[regno].memory
527 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
528 NULL_RTX);
529 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
530 ira_reg_equiv[regno].invariant
531 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
532 NULL_RTX);
533 }
534
535 /* If we have decided to substitute X with another value, return that
536 value, otherwise return X. */
537 static rtx
538 get_equiv (rtx x)
539 {
540 int regno;
541 rtx res;
542
543 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
544 || regno >= ira_reg_equiv_len
545 || ! ira_reg_equiv[regno].defined_p
546 || ! ira_reg_equiv[regno].profitable_p
547 || lra_get_regno_hard_regno (regno) >= 0)
548 return x;
549 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
550 {
551 if (targetm.cannot_substitute_mem_equiv_p (res))
552 return x;
553 return res;
554 }
555 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
556 return res;
557 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
558 return res;
559 gcc_unreachable ();
560 }
561
562 /* If we have decided to substitute X with the equivalent value,
563 return that value after elimination for INSN, otherwise return
564 X. */
565 static rtx
566 get_equiv_with_elimination (rtx x, rtx_insn *insn)
567 {
568 rtx res = get_equiv (x);
569
570 if (x == res || CONSTANT_P (res))
571 return res;
572 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
573 false, false, 0, true);
574 }
575
576 /* Set up curr_operand_mode. */
577 static void
578 init_curr_operand_mode (void)
579 {
580 int nop = curr_static_id->n_operands;
581 for (int i = 0; i < nop; i++)
582 {
583 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
584 if (mode == VOIDmode)
585 {
586 /* The .md mode for address operands is the mode of the
587 addressed value rather than the mode of the address itself. */
588 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
589 mode = Pmode;
590 else
591 mode = curr_static_id->operand[i].mode;
592 }
593 curr_operand_mode[i] = mode;
594 }
595 }
596
597
598
599 /* The page contains code to reuse input reloads. */
600
601 /* Structure describes input reload of the current insns. */
602 struct input_reload
603 {
604 /* True for input reload of matched operands. */
605 bool match_p;
606 /* True for input reload of inout earlyclobber operand. */
607 bool early_clobber_p;
608 /* Reloaded value. */
609 rtx input;
610 /* Reload pseudo used. */
611 rtx reg;
612 };
613
614 /* The number of elements in the following array. */
615 static int curr_insn_input_reloads_num;
616 /* Array containing info about input reloads. It is used to find the
617 same input reload and reuse the reload pseudo in this case. */
618 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
619
620 /* Initiate data concerning reuse of input reloads for the current
621 insn. */
622 static void
623 init_curr_insn_input_reloads (void)
624 {
625 curr_insn_input_reloads_num = 0;
626 }
627
628 /* The canonical form of an rtx inside a MEM is not necessarily the same as the
629 canonical form of the rtx outside the MEM. Fix this up in the case that
630 we're reloading an address (and therefore pulling it outside a MEM). */
631 static rtx
632 canonicalize_reload_addr (rtx addr)
633 {
634 subrtx_var_iterator::array_type array;
635 FOR_EACH_SUBRTX_VAR (iter, array, addr, NONCONST)
636 {
637 rtx x = *iter;
638 if (GET_CODE (x) == MULT && CONST_INT_P (XEXP (x, 1)))
639 {
640 const HOST_WIDE_INT ci = INTVAL (XEXP (x, 1));
641 const int pwr2 = exact_log2 (ci);
642 if (pwr2 > 0)
643 {
644 /* Rewrite this to use a shift instead, which is canonical when
645 outside of a MEM. */
646 PUT_CODE (x, ASHIFT);
647 XEXP (x, 1) = GEN_INT (pwr2);
648 }
649 }
650 }
651
652 return addr;
653 }
654
655 /* Create a new pseudo using MODE, RCLASS, EXCLUDE_START_HARD_REGS, ORIGINAL or
656 reuse an existing reload pseudo. Don't reuse an existing reload pseudo if
657 IN_SUBREG_P is true and the reused pseudo should be wrapped up in a SUBREG.
658 EARLY_CLOBBER_P is true for input reload of inout early clobber operand.
659 The result pseudo is returned through RESULT_REG. Return TRUE if we created
660 a new pseudo, FALSE if we reused an existing reload pseudo. Use TITLE to
661 describe new registers for debug purposes. */
662 static bool
663 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
664 enum reg_class rclass, HARD_REG_SET *exclude_start_hard_regs,
665 bool in_subreg_p, bool early_clobber_p,
666 const char *title, rtx *result_reg)
667 {
668 int i, regno;
669 enum reg_class new_class;
670
671 if (type == OP_OUT)
672 {
673 /* Output reload registers tend to start out with a conservative
674 choice of register class. Usually this is ALL_REGS, although
675 a target might narrow it (for performance reasons) through
676 targetm.preferred_reload_class. It's therefore quite common
677 for a reload instruction to require a more restrictive class
678 than the class that was originally assigned to the reload register.
679
680 In these situations, it's more efficient to refine the choice
681 of register class rather than create a second reload register.
682 This also helps to avoid cycling for registers that are only
683 used by reload instructions. */
684 if (REG_P (original)
685 && (int) REGNO (original) >= new_regno_start
686 && (INSN_UID (curr_insn) >= new_insn_uid_start
687 || ira_former_scratch_p (REGNO (original)))
688 && in_class_p (original, rclass, &new_class, true))
689 {
690 unsigned int regno = REGNO (original);
691 if (lra_dump_file != NULL)
692 {
693 fprintf (lra_dump_file, " Reuse r%d for output ", regno);
694 dump_value_slim (lra_dump_file, original, 1);
695 }
696 if (new_class != lra_get_allocno_class (regno))
697 lra_change_class (regno, new_class, ", change to", false);
698 if (lra_dump_file != NULL)
699 fprintf (lra_dump_file, "\n");
700 *result_reg = original;
701 return false;
702 }
703 *result_reg
704 = lra_create_new_reg_with_unique_value (mode, original, rclass,
705 exclude_start_hard_regs, title);
706 return true;
707 }
708
709 bool unique_p = early_clobber_p;
710 /* Prevent reuse value of expression with side effects,
711 e.g. volatile memory. */
712 if (! side_effects_p (original))
713 for (i = 0; i < curr_insn_input_reloads_num; i++)
714 {
715 if (! curr_insn_input_reloads[i].match_p
716 && ! curr_insn_input_reloads[i].early_clobber_p
717 && rtx_equal_p (curr_insn_input_reloads[i].input, original)
718 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
719 {
720 rtx reg = curr_insn_input_reloads[i].reg;
721 regno = REGNO (reg);
722 /* If input is equal to original and both are VOIDmode,
723 GET_MODE (reg) might be still different from mode.
724 Ensure we don't return *result_reg with wrong mode. */
725 if (GET_MODE (reg) != mode)
726 {
727 if (in_subreg_p)
728 continue;
729 if (maybe_lt (GET_MODE_SIZE (GET_MODE (reg)),
730 GET_MODE_SIZE (mode)))
731 continue;
732 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
733 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
734 continue;
735 }
736 *result_reg = reg;
737 if (lra_dump_file != NULL)
738 {
739 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
740 dump_value_slim (lra_dump_file, original, 1);
741 }
742 if (new_class != lra_get_allocno_class (regno))
743 lra_change_class (regno, new_class, ", change to", false);
744 if (lra_dump_file != NULL)
745 fprintf (lra_dump_file, "\n");
746 return false;
747 }
748 /* If we have an input reload with a different mode, make sure it
749 will get a different hard reg. */
750 else if (REG_P (original)
751 && REG_P (curr_insn_input_reloads[i].input)
752 && REGNO (original) == REGNO (curr_insn_input_reloads[i].input)
753 && (GET_MODE (original)
754 != GET_MODE (curr_insn_input_reloads[i].input)))
755 unique_p = true;
756 }
757 *result_reg = (unique_p
758 ? lra_create_new_reg_with_unique_value
759 : lra_create_new_reg) (mode, original, rclass,
760 exclude_start_hard_regs, title);
761 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
762 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
763 curr_insn_input_reloads[curr_insn_input_reloads_num].match_p = false;
764 curr_insn_input_reloads[curr_insn_input_reloads_num].early_clobber_p
765 = early_clobber_p;
766 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
767 return true;
768 }
769
770
771 /* The page contains major code to choose the current insn alternative
772 and generate reloads for it. */
773
774 /* Return the offset from REGNO of the least significant register
775 in (reg:MODE REGNO).
776
777 This function is used to tell whether two registers satisfy
778 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
779
780 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
781 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
782 int
783 lra_constraint_offset (int regno, machine_mode mode)
784 {
785 lra_assert (regno < FIRST_PSEUDO_REGISTER);
786
787 scalar_int_mode int_mode;
788 if (WORDS_BIG_ENDIAN
789 && is_a <scalar_int_mode> (mode, &int_mode)
790 && GET_MODE_SIZE (int_mode) > UNITS_PER_WORD)
791 return hard_regno_nregs (regno, mode) - 1;
792 return 0;
793 }
794
795 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
796 if they are the same hard reg, and has special hacks for
797 auto-increment and auto-decrement. This is specifically intended for
798 process_alt_operands to use in determining whether two operands
799 match. X is the operand whose number is the lower of the two.
800
801 It is supposed that X is the output operand and Y is the input
802 operand. Y_HARD_REGNO is the final hard regno of register Y or
803 register in subreg Y as we know it now. Otherwise, it is a
804 negative value. */
805 static bool
806 operands_match_p (rtx x, rtx y, int y_hard_regno)
807 {
808 int i;
809 RTX_CODE code = GET_CODE (x);
810 const char *fmt;
811
812 if (x == y)
813 return true;
814 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
815 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
816 {
817 int j;
818
819 i = get_hard_regno (x);
820 if (i < 0)
821 goto slow;
822
823 if ((j = y_hard_regno) < 0)
824 goto slow;
825
826 i += lra_constraint_offset (i, GET_MODE (x));
827 j += lra_constraint_offset (j, GET_MODE (y));
828
829 return i == j;
830 }
831
832 /* If two operands must match, because they are really a single
833 operand of an assembler insn, then two post-increments are invalid
834 because the assembler insn would increment only once. On the
835 other hand, a post-increment matches ordinary indexing if the
836 post-increment is the output operand. */
837 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
838 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
839
840 /* Two pre-increments are invalid because the assembler insn would
841 increment only once. On the other hand, a pre-increment matches
842 ordinary indexing if the pre-increment is the input operand. */
843 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
844 || GET_CODE (y) == PRE_MODIFY)
845 return operands_match_p (x, XEXP (y, 0), -1);
846
847 slow:
848
849 if (code == REG && REG_P (y))
850 return REGNO (x) == REGNO (y);
851
852 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
853 && x == SUBREG_REG (y))
854 return true;
855 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
856 && SUBREG_REG (x) == y)
857 return true;
858
859 /* Now we have disposed of all the cases in which different rtx
860 codes can match. */
861 if (code != GET_CODE (y))
862 return false;
863
864 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
865 if (GET_MODE (x) != GET_MODE (y))
866 return false;
867
868 switch (code)
869 {
870 CASE_CONST_UNIQUE:
871 return false;
872
873 case CONST_VECTOR:
874 if (!same_vector_encodings_p (x, y))
875 return false;
876 break;
877
878 case LABEL_REF:
879 return label_ref_label (x) == label_ref_label (y);
880 case SYMBOL_REF:
881 return XSTR (x, 0) == XSTR (y, 0);
882
883 default:
884 break;
885 }
886
887 /* Compare the elements. If any pair of corresponding elements fail
888 to match, return false for the whole things. */
889
890 fmt = GET_RTX_FORMAT (code);
891 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
892 {
893 int val, j;
894 switch (fmt[i])
895 {
896 case 'w':
897 if (XWINT (x, i) != XWINT (y, i))
898 return false;
899 break;
900
901 case 'i':
902 if (XINT (x, i) != XINT (y, i))
903 return false;
904 break;
905
906 case 'L':
907 if (XLOC (x, i) != XLOC (y, i))
908 return false;
909 break;
910
911 case 'p':
912 if (maybe_ne (SUBREG_BYTE (x), SUBREG_BYTE (y)))
913 return false;
914 break;
915
916 case 'e':
917 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
918 if (val == 0)
919 return false;
920 break;
921
922 case '0':
923 break;
924
925 case 'E':
926 if (XVECLEN (x, i) != XVECLEN (y, i))
927 return false;
928 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
929 {
930 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
931 if (val == 0)
932 return false;
933 }
934 break;
935
936 /* It is believed that rtx's at this level will never
937 contain anything but integers and other rtx's, except for
938 within LABEL_REFs and SYMBOL_REFs. */
939 default:
940 gcc_unreachable ();
941 }
942 }
943 return true;
944 }
945
946 /* True if X is a constant that can be forced into the constant pool.
947 MODE is the mode of the operand, or VOIDmode if not known. */
948 #define CONST_POOL_OK_P(MODE, X) \
949 ((MODE) != VOIDmode \
950 && CONSTANT_P (X) \
951 && GET_CODE (X) != HIGH \
952 && GET_MODE_SIZE (MODE).is_constant () \
953 && !targetm.cannot_force_const_mem (MODE, X))
954
955 /* If REG is a reload pseudo, try to make its class satisfying CL. */
956 static void
957 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
958 {
959 enum reg_class rclass;
960
961 /* Do not make more accurate class from reloads generated. They are
962 mostly moves with a lot of constraints. Making more accurate
963 class may results in very narrow class and impossibility of find
964 registers for several reloads of one insn. */
965 if (INSN_UID (curr_insn) >= new_insn_uid_start)
966 return;
967 if (GET_CODE (reg) == SUBREG)
968 reg = SUBREG_REG (reg);
969 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
970 return;
971 if (in_class_p (reg, cl, &rclass) && rclass != cl)
972 lra_change_class (REGNO (reg), rclass, " Change to", true);
973 }
974
975 /* Searches X for any reference to a reg with the same value as REGNO,
976 returning the rtx of the reference found if any. Otherwise,
977 returns NULL_RTX. */
978 static rtx
979 regno_val_use_in (unsigned int regno, rtx x)
980 {
981 const char *fmt;
982 int i, j;
983 rtx tem;
984
985 if (REG_P (x) && lra_reg_info[REGNO (x)].val == lra_reg_info[regno].val)
986 return x;
987
988 fmt = GET_RTX_FORMAT (GET_CODE (x));
989 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
990 {
991 if (fmt[i] == 'e')
992 {
993 if ((tem = regno_val_use_in (regno, XEXP (x, i))))
994 return tem;
995 }
996 else if (fmt[i] == 'E')
997 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
998 if ((tem = regno_val_use_in (regno , XVECEXP (x, i, j))))
999 return tem;
1000 }
1001
1002 return NULL_RTX;
1003 }
1004
1005 /* Return true if all current insn non-output operands except INS (it
1006 has a negaitve end marker) do not use pseudos with the same value
1007 as REGNO. */
1008 static bool
1009 check_conflict_input_operands (int regno, signed char *ins)
1010 {
1011 int in;
1012 int n_operands = curr_static_id->n_operands;
1013
1014 for (int nop = 0; nop < n_operands; nop++)
1015 if (! curr_static_id->operand[nop].is_operator
1016 && curr_static_id->operand[nop].type != OP_OUT)
1017 {
1018 for (int i = 0; (in = ins[i]) >= 0; i++)
1019 if (in == nop)
1020 break;
1021 if (in < 0
1022 && regno_val_use_in (regno, *curr_id->operand_loc[nop]) != NULL_RTX)
1023 return false;
1024 }
1025 return true;
1026 }
1027
1028 /* Generate reloads for matching OUT and INS (array of input operand numbers
1029 with end marker -1) with reg class GOAL_CLASS and EXCLUDE_START_HARD_REGS,
1030 considering output operands OUTS (similar array to INS) needing to be in
1031 different registers. Add input and output reloads correspondingly to the
1032 lists *BEFORE and *AFTER. OUT might be negative. In this case we generate
1033 input reloads for matched input operands INS. EARLY_CLOBBER_P is a flag
1034 that the output operand is early clobbered for chosen alternative. */
1035 static void
1036 match_reload (signed char out, signed char *ins, signed char *outs,
1037 enum reg_class goal_class, HARD_REG_SET *exclude_start_hard_regs,
1038 rtx_insn **before, rtx_insn **after, bool early_clobber_p)
1039 {
1040 bool out_conflict;
1041 int i, in;
1042 rtx new_in_reg, new_out_reg, reg;
1043 machine_mode inmode, outmode;
1044 rtx in_rtx = *curr_id->operand_loc[ins[0]];
1045 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
1046
1047 inmode = curr_operand_mode[ins[0]];
1048 outmode = out < 0 ? inmode : curr_operand_mode[out];
1049 push_to_sequence (*before);
1050 if (inmode != outmode)
1051 {
1052 /* process_alt_operands has already checked that the mode sizes
1053 are ordered. */
1054 if (partial_subreg_p (outmode, inmode))
1055 {
1056 bool asm_p = asm_noperands (PATTERN (curr_insn)) >= 0;
1057 int hr;
1058 HARD_REG_SET temp_hard_reg_set;
1059
1060 if (asm_p && (hr = get_hard_regno (out_rtx)) >= 0
1061 && hard_regno_nregs (hr, inmode) > 1)
1062 {
1063 /* See gcc.c-torture/execute/20030222-1.c.
1064 Consider the code for 32-bit (e.g. BE) target:
1065 int i, v; long x; x = v; asm ("" : "=r" (i) : "0" (x));
1066 We generate the following RTL with reload insns:
1067 1. subreg:si(x:di, 0) = 0;
1068 2. subreg:si(x:di, 4) = v:si;
1069 3. t:di = x:di, dead x;
1070 4. asm ("" : "=r" (subreg:si(t:di,4)) : "0" (t:di))
1071 5. i:si = subreg:si(t:di,4);
1072 If we assign hard reg of x to t, dead code elimination
1073 will remove insn #2 and we will use unitialized hard reg.
1074 So exclude the hard reg of x for t. We could ignore this
1075 problem for non-empty asm using all x value but it is hard to
1076 check that the asm are expanded into insn realy using x
1077 and setting r. */
1078 CLEAR_HARD_REG_SET (temp_hard_reg_set);
1079 if (exclude_start_hard_regs != NULL)
1080 temp_hard_reg_set = *exclude_start_hard_regs;
1081 SET_HARD_REG_BIT (temp_hard_reg_set, hr);
1082 exclude_start_hard_regs = &temp_hard_reg_set;
1083 }
1084 reg = new_in_reg
1085 = lra_create_new_reg_with_unique_value (inmode, in_rtx, goal_class,
1086 exclude_start_hard_regs,
1087 "");
1088 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
1089 LRA_SUBREG_P (new_out_reg) = 1;
1090 /* If the input reg is dying here, we can use the same hard
1091 register for REG and IN_RTX. We do it only for original
1092 pseudos as reload pseudos can die although original
1093 pseudos still live where reload pseudos dies. */
1094 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
1095 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
1096 && (!early_clobber_p
1097 || check_conflict_input_operands(REGNO (in_rtx), ins)))
1098 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
1099 }
1100 else
1101 {
1102 reg = new_out_reg
1103 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
1104 goal_class,
1105 exclude_start_hard_regs,
1106 "");
1107 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
1108 /* NEW_IN_REG is non-paradoxical subreg. We don't want
1109 NEW_OUT_REG living above. We add clobber clause for
1110 this. This is just a temporary clobber. We can remove
1111 it at the end of LRA work. */
1112 rtx_insn *clobber = emit_clobber (new_out_reg);
1113 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
1114 LRA_SUBREG_P (new_in_reg) = 1;
1115 if (GET_CODE (in_rtx) == SUBREG)
1116 {
1117 rtx subreg_reg = SUBREG_REG (in_rtx);
1118
1119 /* If SUBREG_REG is dying here and sub-registers IN_RTX
1120 and NEW_IN_REG are similar, we can use the same hard
1121 register for REG and SUBREG_REG. */
1122 if (REG_P (subreg_reg)
1123 && (int) REGNO (subreg_reg) < lra_new_regno_start
1124 && GET_MODE (subreg_reg) == outmode
1125 && known_eq (SUBREG_BYTE (in_rtx), SUBREG_BYTE (new_in_reg))
1126 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg))
1127 && (! early_clobber_p
1128 || check_conflict_input_operands (REGNO (subreg_reg),
1129 ins)))
1130 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
1131 }
1132 }
1133 }
1134 else
1135 {
1136 /* Pseudos have values -- see comments for lra_reg_info.
1137 Different pseudos with the same value do not conflict even if
1138 they live in the same place. When we create a pseudo we
1139 assign value of original pseudo (if any) from which we
1140 created the new pseudo. If we create the pseudo from the
1141 input pseudo, the new pseudo will have no conflict with the
1142 input pseudo which is wrong when the input pseudo lives after
1143 the insn and as the new pseudo value is changed by the insn
1144 output. Therefore we create the new pseudo from the output
1145 except the case when we have single matched dying input
1146 pseudo.
1147
1148 We cannot reuse the current output register because we might
1149 have a situation like "a <- a op b", where the constraints
1150 force the second input operand ("b") to match the output
1151 operand ("a"). "b" must then be copied into a new register
1152 so that it doesn't clobber the current value of "a".
1153
1154 We cannot use the same value if the output pseudo is
1155 early clobbered or the input pseudo is mentioned in the
1156 output, e.g. as an address part in memory, because
1157 output reload will actually extend the pseudo liveness.
1158 We don't care about eliminable hard regs here as we are
1159 interesting only in pseudos. */
1160
1161 /* Matching input's register value is the same as one of the other
1162 output operand. Output operands in a parallel insn must be in
1163 different registers. */
1164 out_conflict = false;
1165 if (REG_P (in_rtx))
1166 {
1167 for (i = 0; outs[i] >= 0; i++)
1168 {
1169 rtx other_out_rtx = *curr_id->operand_loc[outs[i]];
1170 if (outs[i] != out && REG_P (other_out_rtx)
1171 && (regno_val_use_in (REGNO (in_rtx), other_out_rtx)
1172 != NULL_RTX))
1173 {
1174 out_conflict = true;
1175 break;
1176 }
1177 }
1178 }
1179
1180 new_in_reg = new_out_reg
1181 = (! early_clobber_p && ins[1] < 0 && REG_P (in_rtx)
1182 && (int) REGNO (in_rtx) < lra_new_regno_start
1183 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
1184 && (! early_clobber_p
1185 || check_conflict_input_operands (REGNO (in_rtx), ins))
1186 && (out < 0
1187 || regno_val_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
1188 && !out_conflict
1189 ? lra_create_new_reg (inmode, in_rtx, goal_class,
1190 exclude_start_hard_regs, "")
1191 : lra_create_new_reg_with_unique_value (outmode, out_rtx, goal_class,
1192 exclude_start_hard_regs,
1193 ""));
1194 }
1195 /* In operand can be got from transformations before processing insn
1196 constraints. One example of such transformations is subreg
1197 reloading (see function simplify_operand_subreg). The new
1198 pseudos created by the transformations might have inaccurate
1199 class (ALL_REGS) and we should make their classes more
1200 accurate. */
1201 narrow_reload_pseudo_class (in_rtx, goal_class);
1202 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
1203 *before = get_insns ();
1204 end_sequence ();
1205 /* Add the new pseudo to consider values of subsequent input reload
1206 pseudos. */
1207 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
1208 curr_insn_input_reloads[curr_insn_input_reloads_num].input = in_rtx;
1209 curr_insn_input_reloads[curr_insn_input_reloads_num].match_p = true;
1210 curr_insn_input_reloads[curr_insn_input_reloads_num].early_clobber_p = false;
1211 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = new_in_reg;
1212 for (i = 0; (in = ins[i]) >= 0; i++)
1213 if (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
1214 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]))
1215 *curr_id->operand_loc[in] = new_in_reg;
1216 else
1217 {
1218 lra_assert
1219 (GET_MODE (new_out_reg) == GET_MODE (*curr_id->operand_loc[in]));
1220 *curr_id->operand_loc[in] = new_out_reg;
1221 }
1222 lra_update_dups (curr_id, ins);
1223 if (out < 0)
1224 return;
1225 /* See a comment for the input operand above. */
1226 narrow_reload_pseudo_class (out_rtx, goal_class);
1227 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
1228 {
1229 reg = SUBREG_P (out_rtx) ? SUBREG_REG (out_rtx) : out_rtx;
1230 start_sequence ();
1231 /* If we had strict_low_part, use it also in reload to keep other
1232 parts unchanged but do it only for regs as strict_low_part
1233 has no sense for memory and probably there is no insn pattern
1234 to match the reload insn in memory case. */
1235 if (out >= 0 && curr_static_id->operand[out].strict_low && REG_P (reg))
1236 out_rtx = gen_rtx_STRICT_LOW_PART (VOIDmode, out_rtx);
1237 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
1238 emit_insn (*after);
1239 *after = get_insns ();
1240 end_sequence ();
1241 }
1242 *curr_id->operand_loc[out] = new_out_reg;
1243 lra_update_dup (curr_id, out);
1244 }
1245
1246 /* Return register class which is union of all reg classes in insn
1247 constraint alternative string starting with P. */
1248 static enum reg_class
1249 reg_class_from_constraints (const char *p)
1250 {
1251 int c, len;
1252 enum reg_class op_class = NO_REGS;
1253
1254 do
1255 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1256 {
1257 case '#':
1258 case ',':
1259 return op_class;
1260
1261 case 'g':
1262 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1263 break;
1264
1265 default:
1266 enum constraint_num cn = lookup_constraint (p);
1267 enum reg_class cl = reg_class_for_constraint (cn);
1268 if (cl == NO_REGS)
1269 {
1270 if (insn_extra_address_constraint (cn))
1271 op_class
1272 = (reg_class_subunion
1273 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1274 ADDRESS, SCRATCH)]);
1275 break;
1276 }
1277
1278 op_class = reg_class_subunion[op_class][cl];
1279 break;
1280 }
1281 while ((p += len), c);
1282 return op_class;
1283 }
1284
1285 /* If OP is a register, return the class of the register as per
1286 get_reg_class, otherwise return NO_REGS. */
1287 static inline enum reg_class
1288 get_op_class (rtx op)
1289 {
1290 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1291 }
1292
1293 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1294 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1295 SUBREG for VAL to make them equal. */
1296 static rtx_insn *
1297 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1298 {
1299 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1300 {
1301 /* Usually size of mem_pseudo is greater than val size but in
1302 rare cases it can be less as it can be defined by target
1303 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1304 if (! MEM_P (val))
1305 {
1306 val = gen_lowpart_SUBREG (GET_MODE (mem_pseudo),
1307 GET_CODE (val) == SUBREG
1308 ? SUBREG_REG (val) : val);
1309 LRA_SUBREG_P (val) = 1;
1310 }
1311 else
1312 {
1313 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1314 LRA_SUBREG_P (mem_pseudo) = 1;
1315 }
1316 }
1317 return to_p ? gen_move_insn (mem_pseudo, val)
1318 : gen_move_insn (val, mem_pseudo);
1319 }
1320
1321 /* Process a special case insn (register move), return true if we
1322 don't need to process it anymore. INSN should be a single set
1323 insn. Set up that RTL was changed through CHANGE_P and that hook
1324 TARGET_SECONDARY_MEMORY_NEEDED says to use secondary memory through
1325 SEC_MEM_P. */
1326 static bool
1327 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1328 {
1329 int sregno, dregno;
1330 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1331 rtx_insn *before;
1332 enum reg_class dclass, sclass, secondary_class;
1333 secondary_reload_info sri;
1334
1335 lra_assert (curr_insn_set != NULL_RTX);
1336 dreg = dest = SET_DEST (curr_insn_set);
1337 sreg = src = SET_SRC (curr_insn_set);
1338 if (GET_CODE (dest) == SUBREG)
1339 dreg = SUBREG_REG (dest);
1340 if (GET_CODE (src) == SUBREG)
1341 sreg = SUBREG_REG (src);
1342 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1343 return false;
1344 sclass = dclass = NO_REGS;
1345 if (REG_P (dreg))
1346 dclass = get_reg_class (REGNO (dreg));
1347 gcc_assert (dclass < LIM_REG_CLASSES && dclass >= NO_REGS);
1348 if (dclass == ALL_REGS)
1349 /* ALL_REGS is used for new pseudos created by transformations
1350 like reload of SUBREG_REG (see function
1351 simplify_operand_subreg). We don't know their class yet. We
1352 should figure out the class from processing the insn
1353 constraints not in this fast path function. Even if ALL_REGS
1354 were a right class for the pseudo, secondary_... hooks usually
1355 are not define for ALL_REGS. */
1356 return false;
1357 if (REG_P (sreg))
1358 sclass = get_reg_class (REGNO (sreg));
1359 gcc_assert (sclass < LIM_REG_CLASSES && sclass >= NO_REGS);
1360 if (sclass == ALL_REGS)
1361 /* See comments above. */
1362 return false;
1363 if (sclass == NO_REGS && dclass == NO_REGS)
1364 return false;
1365 if (targetm.secondary_memory_needed (GET_MODE (src), sclass, dclass)
1366 && ((sclass != NO_REGS && dclass != NO_REGS)
1367 || (GET_MODE (src)
1368 != targetm.secondary_memory_needed_mode (GET_MODE (src)))))
1369 {
1370 *sec_mem_p = true;
1371 return false;
1372 }
1373 if (! REG_P (dreg) || ! REG_P (sreg))
1374 return false;
1375 sri.prev_sri = NULL;
1376 sri.icode = CODE_FOR_nothing;
1377 sri.extra_cost = 0;
1378 secondary_class = NO_REGS;
1379 /* Set up hard register for a reload pseudo for hook
1380 secondary_reload because some targets just ignore unassigned
1381 pseudos in the hook. */
1382 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1383 {
1384 dregno = REGNO (dreg);
1385 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1386 }
1387 else
1388 dregno = -1;
1389 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1390 {
1391 sregno = REGNO (sreg);
1392 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1393 }
1394 else
1395 sregno = -1;
1396 if (sclass != NO_REGS)
1397 secondary_class
1398 = (enum reg_class) targetm.secondary_reload (false, dest,
1399 (reg_class_t) sclass,
1400 GET_MODE (src), &sri);
1401 if (sclass == NO_REGS
1402 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1403 && dclass != NO_REGS))
1404 {
1405 enum reg_class old_sclass = secondary_class;
1406 secondary_reload_info old_sri = sri;
1407
1408 sri.prev_sri = NULL;
1409 sri.icode = CODE_FOR_nothing;
1410 sri.extra_cost = 0;
1411 secondary_class
1412 = (enum reg_class) targetm.secondary_reload (true, src,
1413 (reg_class_t) dclass,
1414 GET_MODE (src), &sri);
1415 /* Check the target hook consistency. */
1416 lra_assert
1417 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1418 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1419 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1420 }
1421 if (sregno >= 0)
1422 reg_renumber [sregno] = -1;
1423 if (dregno >= 0)
1424 reg_renumber [dregno] = -1;
1425 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1426 return false;
1427 *change_p = true;
1428 new_reg = NULL_RTX;
1429 if (secondary_class != NO_REGS)
1430 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1431 secondary_class, NULL,
1432 "secondary");
1433 start_sequence ();
1434 if (sri.icode == CODE_FOR_nothing)
1435 lra_emit_move (new_reg, src);
1436 else
1437 {
1438 enum reg_class scratch_class;
1439
1440 scratch_class = (reg_class_from_constraints
1441 (insn_data[sri.icode].operand[2].constraint));
1442 scratch_reg = (lra_create_new_reg_with_unique_value
1443 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1444 scratch_class, NULL, "scratch"));
1445 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1446 src, scratch_reg));
1447 }
1448 before = get_insns ();
1449 end_sequence ();
1450 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1451 if (new_reg != NULL_RTX)
1452 SET_SRC (curr_insn_set) = new_reg;
1453 else
1454 {
1455 if (lra_dump_file != NULL)
1456 {
1457 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1458 dump_insn_slim (lra_dump_file, curr_insn);
1459 }
1460 lra_set_insn_deleted (curr_insn);
1461 return true;
1462 }
1463 return false;
1464 }
1465
1466 /* The following data describe the result of process_alt_operands.
1467 The data are used in curr_insn_transform to generate reloads. */
1468
1469 /* The chosen reg classes which should be used for the corresponding
1470 operands. */
1471 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1472 /* Hard registers which cannot be a start hard register for the corresponding
1473 operands. */
1474 static HARD_REG_SET goal_alt_exclude_start_hard_regs[MAX_RECOG_OPERANDS];
1475 /* True if the operand should be the same as another operand and that
1476 other operand does not need a reload. */
1477 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1478 /* True if the operand does not need a reload. */
1479 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1480 /* True if the operand can be offsetable memory. */
1481 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1482 /* The number of an operand to which given operand can be matched to. */
1483 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1484 /* The number of elements in the following array. */
1485 static int goal_alt_dont_inherit_ops_num;
1486 /* Numbers of operands whose reload pseudos should not be inherited. */
1487 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1488 /* True if we should try only this alternative for the next constraint sub-pass
1489 to speed up the sub-pass. */
1490 static bool goal_reuse_alt_p;
1491 /* True if the insn commutative operands should be swapped. */
1492 static bool goal_alt_swapped;
1493 /* The chosen insn alternative. */
1494 static int goal_alt_number;
1495 /* True if output reload of the stack pointer should be generated. */
1496 static bool goal_alt_out_sp_reload_p;
1497
1498 /* True if the corresponding operand is the result of an equivalence
1499 substitution. */
1500 static bool equiv_substition_p[MAX_RECOG_OPERANDS];
1501
1502 /* The following five variables are used to choose the best insn
1503 alternative. They reflect final characteristics of the best
1504 alternative. */
1505
1506 /* Number of necessary reloads and overall cost reflecting the
1507 previous value and other unpleasantness of the best alternative. */
1508 static int best_losers, best_overall;
1509 /* Overall number hard registers used for reloads. For example, on
1510 some targets we need 2 general registers to reload DFmode and only
1511 one floating point register. */
1512 static int best_reload_nregs;
1513 /* Overall number reflecting distances of previous reloading the same
1514 value. The distances are counted from the current BB start. It is
1515 used to improve inheritance chances. */
1516 static int best_reload_sum;
1517
1518 /* True if the current insn should have no correspondingly input or
1519 output reloads. */
1520 static bool no_input_reloads_p, no_output_reloads_p;
1521
1522 /* True if we swapped the commutative operands in the current
1523 insn. */
1524 static int curr_swapped;
1525
1526 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1527 register of class CL. Add any input reloads to list BEFORE. AFTER
1528 is nonnull if *LOC is an automodified value; handle that case by
1529 adding the required output reloads to list AFTER. Return true if
1530 the RTL was changed.
1531
1532 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1533 register. Return false if the address register is correct. */
1534 static bool
1535 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1536 enum reg_class cl)
1537 {
1538 int regno;
1539 enum reg_class rclass, new_class;
1540 rtx reg;
1541 rtx new_reg;
1542 machine_mode mode;
1543 bool subreg_p, before_p = false;
1544
1545 subreg_p = GET_CODE (*loc) == SUBREG;
1546 if (subreg_p)
1547 {
1548 reg = SUBREG_REG (*loc);
1549 mode = GET_MODE (reg);
1550
1551 /* For mode with size bigger than ptr_mode, there unlikely to be "mov"
1552 between two registers with different classes, but there normally will
1553 be "mov" which transfers element of vector register into the general
1554 register, and this normally will be a subreg which should be reloaded
1555 as a whole. This is particularly likely to be triggered when
1556 -fno-split-wide-types specified. */
1557 if (!REG_P (reg)
1558 || in_class_p (reg, cl, &new_class)
1559 || known_le (GET_MODE_SIZE (mode), GET_MODE_SIZE (ptr_mode)))
1560 loc = &SUBREG_REG (*loc);
1561 }
1562
1563 reg = *loc;
1564 mode = GET_MODE (reg);
1565 if (! REG_P (reg))
1566 {
1567 if (check_only_p)
1568 return true;
1569 /* Always reload memory in an address even if the target supports
1570 such addresses. */
1571 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, NULL,
1572 "address");
1573 before_p = true;
1574 }
1575 else
1576 {
1577 regno = REGNO (reg);
1578 rclass = get_reg_class (regno);
1579 if (! check_only_p
1580 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1581 {
1582 if (lra_dump_file != NULL)
1583 {
1584 fprintf (lra_dump_file,
1585 "Changing pseudo %d in address of insn %u on equiv ",
1586 REGNO (reg), INSN_UID (curr_insn));
1587 dump_value_slim (lra_dump_file, *loc, 1);
1588 fprintf (lra_dump_file, "\n");
1589 }
1590 *loc = copy_rtx (*loc);
1591 }
1592 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1593 {
1594 if (check_only_p)
1595 return true;
1596 reg = *loc;
1597 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1598 mode, reg, cl, NULL,
1599 subreg_p, false, "address", &new_reg))
1600 before_p = true;
1601 }
1602 else if (new_class != NO_REGS && rclass != new_class)
1603 {
1604 if (check_only_p)
1605 return true;
1606 lra_change_class (regno, new_class, " Change to", true);
1607 return false;
1608 }
1609 else
1610 return false;
1611 }
1612 if (before_p)
1613 {
1614 push_to_sequence (*before);
1615 lra_emit_move (new_reg, reg);
1616 *before = get_insns ();
1617 end_sequence ();
1618 }
1619 *loc = new_reg;
1620 if (after != NULL)
1621 {
1622 start_sequence ();
1623 lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1624 emit_insn (*after);
1625 *after = get_insns ();
1626 end_sequence ();
1627 }
1628 return true;
1629 }
1630
1631 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1632 the insn to be inserted before curr insn. AFTER returns the
1633 the insn to be inserted after curr insn. ORIGREG and NEWREG
1634 are the original reg and new reg for reload. */
1635 static void
1636 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1637 rtx newreg)
1638 {
1639 if (before)
1640 {
1641 push_to_sequence (*before);
1642 lra_emit_move (newreg, origreg);
1643 *before = get_insns ();
1644 end_sequence ();
1645 }
1646 if (after)
1647 {
1648 start_sequence ();
1649 lra_emit_move (origreg, newreg);
1650 emit_insn (*after);
1651 *after = get_insns ();
1652 end_sequence ();
1653 }
1654 }
1655
1656 static bool valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1657 static bool process_address (int, bool, rtx_insn **, rtx_insn **);
1658
1659 /* Make reloads for subreg in operand NOP with internal subreg mode
1660 REG_MODE, add new reloads for further processing. Return true if
1661 any change was done. */
1662 static bool
1663 simplify_operand_subreg (int nop, machine_mode reg_mode)
1664 {
1665 int hard_regno, inner_hard_regno;
1666 rtx_insn *before, *after;
1667 machine_mode mode, innermode;
1668 rtx reg, new_reg;
1669 rtx operand = *curr_id->operand_loc[nop];
1670 enum reg_class regclass;
1671 enum op_type type;
1672
1673 before = after = NULL;
1674
1675 if (GET_CODE (operand) != SUBREG)
1676 return false;
1677
1678 mode = GET_MODE (operand);
1679 reg = SUBREG_REG (operand);
1680 innermode = GET_MODE (reg);
1681 type = curr_static_id->operand[nop].type;
1682 if (MEM_P (reg))
1683 {
1684 const bool addr_was_valid
1685 = valid_address_p (innermode, XEXP (reg, 0), MEM_ADDR_SPACE (reg));
1686 alter_subreg (curr_id->operand_loc[nop], false);
1687 rtx subst = *curr_id->operand_loc[nop];
1688 lra_assert (MEM_P (subst));
1689 const bool addr_is_valid = valid_address_p (GET_MODE (subst),
1690 XEXP (subst, 0),
1691 MEM_ADDR_SPACE (subst));
1692 if (!addr_was_valid
1693 || addr_is_valid
1694 || ((get_constraint_type (lookup_constraint
1695 (curr_static_id->operand[nop].constraint))
1696 != CT_SPECIAL_MEMORY)
1697 /* We still can reload address and if the address is
1698 valid, we can remove subreg without reloading its
1699 inner memory. */
1700 && valid_address_p (GET_MODE (subst),
1701 regno_reg_rtx
1702 [ira_class_hard_regs
1703 [base_reg_class (GET_MODE (subst),
1704 MEM_ADDR_SPACE (subst),
1705 ADDRESS, SCRATCH)][0]],
1706 MEM_ADDR_SPACE (subst))))
1707 {
1708 /* If we change the address for a paradoxical subreg of memory, the
1709 new address might violate the necessary alignment or the access
1710 might be slow; take this into consideration. We need not worry
1711 about accesses beyond allocated memory for paradoxical memory
1712 subregs as we don't substitute such equiv memory (see processing
1713 equivalences in function lra_constraints) and because for spilled
1714 pseudos we allocate stack memory enough for the biggest
1715 corresponding paradoxical subreg.
1716
1717 However, do not blindly simplify a (subreg (mem ...)) for
1718 WORD_REGISTER_OPERATIONS targets as this may lead to loading junk
1719 data into a register when the inner is narrower than outer or
1720 missing important data from memory when the inner is wider than
1721 outer. This rule only applies to modes that are no wider than
1722 a word.
1723
1724 If valid memory becomes invalid after subreg elimination
1725 and address might be different we still have to reload
1726 memory.
1727 */
1728 if ((! addr_was_valid
1729 || addr_is_valid
1730 || known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (innermode)))
1731 && !(maybe_ne (GET_MODE_PRECISION (mode),
1732 GET_MODE_PRECISION (innermode))
1733 && known_le (GET_MODE_SIZE (mode), UNITS_PER_WORD)
1734 && known_le (GET_MODE_SIZE (innermode), UNITS_PER_WORD)
1735 && WORD_REGISTER_OPERATIONS)
1736 && (!(MEM_ALIGN (subst) < GET_MODE_ALIGNMENT (mode)
1737 && targetm.slow_unaligned_access (mode, MEM_ALIGN (subst)))
1738 || (MEM_ALIGN (reg) < GET_MODE_ALIGNMENT (innermode)
1739 && targetm.slow_unaligned_access (innermode,
1740 MEM_ALIGN (reg)))))
1741 return true;
1742
1743 *curr_id->operand_loc[nop] = operand;
1744
1745 /* But if the address was not valid, we cannot reload the MEM without
1746 reloading the address first. */
1747 if (!addr_was_valid)
1748 process_address (nop, false, &before, &after);
1749
1750 /* INNERMODE is fast, MODE slow. Reload the mem in INNERMODE. */
1751 enum reg_class rclass
1752 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1753 if (get_reload_reg (curr_static_id->operand[nop].type, innermode,
1754 reg, rclass, NULL,
1755 true, false, "slow/invalid mem", &new_reg))
1756 {
1757 bool insert_before, insert_after;
1758 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1759
1760 insert_before = (type != OP_OUT
1761 || partial_subreg_p (mode, innermode));
1762 insert_after = type != OP_IN;
1763 insert_move_for_subreg (insert_before ? &before : NULL,
1764 insert_after ? &after : NULL,
1765 reg, new_reg);
1766 }
1767 SUBREG_REG (operand) = new_reg;
1768
1769 /* Convert to MODE. */
1770 reg = operand;
1771 rclass
1772 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1773 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1774 rclass, NULL,
1775 true, false, "slow/invalid mem", &new_reg))
1776 {
1777 bool insert_before, insert_after;
1778 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1779
1780 insert_before = type != OP_OUT;
1781 insert_after = type != OP_IN;
1782 insert_move_for_subreg (insert_before ? &before : NULL,
1783 insert_after ? &after : NULL,
1784 reg, new_reg);
1785 }
1786 *curr_id->operand_loc[nop] = new_reg;
1787 lra_process_new_insns (curr_insn, before, after,
1788 "Inserting slow/invalid mem reload");
1789 return true;
1790 }
1791
1792 /* If the address was valid and became invalid, prefer to reload
1793 the memory. Typical case is when the index scale should
1794 correspond the memory. */
1795 *curr_id->operand_loc[nop] = operand;
1796 /* Do not return false here as the MEM_P (reg) will be processed
1797 later in this function. */
1798 }
1799 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1800 {
1801 alter_subreg (curr_id->operand_loc[nop], false);
1802 return true;
1803 }
1804 else if (CONSTANT_P (reg))
1805 {
1806 /* Try to simplify subreg of constant. It is usually result of
1807 equivalence substitution. */
1808 if (innermode == VOIDmode
1809 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1810 innermode = curr_static_id->operand[nop].mode;
1811 if ((new_reg = simplify_subreg (mode, reg, innermode,
1812 SUBREG_BYTE (operand))) != NULL_RTX)
1813 {
1814 *curr_id->operand_loc[nop] = new_reg;
1815 return true;
1816 }
1817 }
1818 /* Put constant into memory when we have mixed modes. It generates
1819 a better code in most cases as it does not need a secondary
1820 reload memory. It also prevents LRA looping when LRA is using
1821 secondary reload memory again and again. */
1822 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1823 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1824 {
1825 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1826 alter_subreg (curr_id->operand_loc[nop], false);
1827 return true;
1828 }
1829 auto fp_subreg_can_be_simplified_after_reload_p = [] (machine_mode innermode,
1830 poly_uint64 offset,
1831 machine_mode mode) {
1832 reload_completed = 1;
1833 bool res = simplify_subreg_regno (FRAME_POINTER_REGNUM,
1834 innermode,
1835 offset, mode) >= 0;
1836 reload_completed = 0;
1837 return res;
1838 };
1839 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1840 if there may be a problem accessing OPERAND in the outer
1841 mode. */
1842 if ((REG_P (reg)
1843 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1844 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1845 /* Don't reload paradoxical subregs because we could be looping
1846 having repeatedly final regno out of hard regs range. */
1847 && (hard_regno_nregs (hard_regno, innermode)
1848 >= hard_regno_nregs (hard_regno, mode))
1849 && simplify_subreg_regno (hard_regno, innermode,
1850 SUBREG_BYTE (operand), mode) < 0
1851 /* Exclude reloading of frame pointer in subreg if frame pointer can not
1852 be simplified here only because the reload is not finished yet. */
1853 && (hard_regno != FRAME_POINTER_REGNUM
1854 || !fp_subreg_can_be_simplified_after_reload_p (innermode,
1855 SUBREG_BYTE (operand),
1856 mode))
1857 /* Don't reload subreg for matching reload. It is actually
1858 valid subreg in LRA. */
1859 && ! LRA_SUBREG_P (operand))
1860 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1861 {
1862 enum reg_class rclass;
1863
1864 if (REG_P (reg))
1865 /* There is a big probability that we will get the same class
1866 for the new pseudo and we will get the same insn which
1867 means infinite looping. So spill the new pseudo. */
1868 rclass = NO_REGS;
1869 else
1870 /* The class will be defined later in curr_insn_transform. */
1871 rclass
1872 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1873
1874 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1875 rclass, NULL,
1876 true, false, "subreg reg", &new_reg))
1877 {
1878 bool insert_before, insert_after;
1879 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1880
1881 insert_before = (type != OP_OUT
1882 || read_modify_subreg_p (operand));
1883 insert_after = (type != OP_IN);
1884 insert_move_for_subreg (insert_before ? &before : NULL,
1885 insert_after ? &after : NULL,
1886 reg, new_reg);
1887 }
1888 SUBREG_REG (operand) = new_reg;
1889 lra_process_new_insns (curr_insn, before, after,
1890 "Inserting subreg reload");
1891 return true;
1892 }
1893 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1894 IRA allocates hardreg to the inner pseudo reg according to its mode
1895 instead of the outermode, so the size of the hardreg may not be enough
1896 to contain the outermode operand, in that case we may need to insert
1897 reload for the reg. For the following two types of paradoxical subreg,
1898 we need to insert reload:
1899 1. If the op_type is OP_IN, and the hardreg could not be paired with
1900 other hardreg to contain the outermode operand
1901 (checked by in_hard_reg_set_p), we need to insert the reload.
1902 2. If the op_type is OP_OUT or OP_INOUT.
1903
1904 Here is a paradoxical subreg example showing how the reload is generated:
1905
1906 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1907 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1908
1909 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1910 here, if reg107 is assigned to hardreg R15, because R15 is the last
1911 hardreg, compiler cannot find another hardreg to pair with R15 to
1912 contain TImode data. So we insert a TImode reload reg180 for it.
1913 After reload is inserted:
1914
1915 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1916 (reg:DI 107 [ __comp ])) -1
1917 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1918 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1919
1920 Two reload hard registers will be allocated to reg180 to save TImode data
1921 in LRA_assign.
1922
1923 For LRA pseudos this should normally be handled by the biggest_mode
1924 mechanism. However, it's possible for new uses of an LRA pseudo
1925 to be introduced after we've allocated it, such as when undoing
1926 inheritance, and the allocated register might not then be appropriate
1927 for the new uses. */
1928 else if (REG_P (reg)
1929 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1930 && paradoxical_subreg_p (operand)
1931 && (inner_hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1932 && ((hard_regno
1933 = simplify_subreg_regno (inner_hard_regno, innermode,
1934 SUBREG_BYTE (operand), mode)) < 0
1935 || ((hard_regno_nregs (inner_hard_regno, innermode)
1936 < hard_regno_nregs (hard_regno, mode))
1937 && (regclass = lra_get_allocno_class (REGNO (reg)))
1938 && (type != OP_IN
1939 || !in_hard_reg_set_p (reg_class_contents[regclass],
1940 mode, hard_regno)
1941 || overlaps_hard_reg_set_p (lra_no_alloc_regs,
1942 mode, hard_regno)))))
1943 {
1944 /* The class will be defined later in curr_insn_transform. */
1945 enum reg_class rclass
1946 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1947
1948 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1949 rclass, NULL,
1950 true, false, "paradoxical subreg", &new_reg))
1951 {
1952 rtx subreg;
1953 bool insert_before, insert_after;
1954
1955 PUT_MODE (new_reg, mode);
1956 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1957 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1958
1959 insert_before = (type != OP_OUT);
1960 insert_after = (type != OP_IN);
1961 insert_move_for_subreg (insert_before ? &before : NULL,
1962 insert_after ? &after : NULL,
1963 reg, subreg);
1964 }
1965 SUBREG_REG (operand) = new_reg;
1966 lra_process_new_insns (curr_insn, before, after,
1967 "Inserting paradoxical subreg reload");
1968 return true;
1969 }
1970 return false;
1971 }
1972
1973 /* Return TRUE if X refers for a hard register from SET. */
1974 static bool
1975 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1976 {
1977 int i, j, x_hard_regno;
1978 machine_mode mode;
1979 const char *fmt;
1980 enum rtx_code code;
1981
1982 if (x == NULL_RTX)
1983 return false;
1984 code = GET_CODE (x);
1985 mode = GET_MODE (x);
1986
1987 if (code == SUBREG)
1988 {
1989 /* For all SUBREGs we want to check whether the full multi-register
1990 overlaps the set. For normal SUBREGs this means 'get_hard_regno' of
1991 the inner register, for paradoxical SUBREGs this means the
1992 'get_hard_regno' of the full SUBREG and for complete SUBREGs either is
1993 fine. Use the wider mode for all cases. */
1994 rtx subreg = SUBREG_REG (x);
1995 mode = wider_subreg_mode (x);
1996 if (mode == GET_MODE (subreg))
1997 {
1998 x = subreg;
1999 code = GET_CODE (x);
2000 }
2001 }
2002
2003 if (REG_P (x) || SUBREG_P (x))
2004 {
2005 x_hard_regno = get_hard_regno (x);
2006 return (x_hard_regno >= 0
2007 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
2008 }
2009 fmt = GET_RTX_FORMAT (code);
2010 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2011 {
2012 if (fmt[i] == 'e')
2013 {
2014 if (uses_hard_regs_p (XEXP (x, i), set))
2015 return true;
2016 }
2017 else if (fmt[i] == 'E')
2018 {
2019 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2020 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
2021 return true;
2022 }
2023 }
2024 return false;
2025 }
2026
2027 /* Return true if OP is a spilled pseudo. */
2028 static inline bool
2029 spilled_pseudo_p (rtx op)
2030 {
2031 return (REG_P (op)
2032 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
2033 }
2034
2035 /* Return true if X is a general constant. */
2036 static inline bool
2037 general_constant_p (rtx x)
2038 {
2039 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
2040 }
2041
2042 static bool
2043 reg_in_class_p (rtx reg, enum reg_class cl)
2044 {
2045 if (cl == NO_REGS)
2046 return get_reg_class (REGNO (reg)) == NO_REGS;
2047 return in_class_p (reg, cl, NULL);
2048 }
2049
2050 /* Return true if SET of RCLASS contains no hard regs which can be
2051 used in MODE. */
2052 static bool
2053 prohibited_class_reg_set_mode_p (enum reg_class rclass,
2054 HARD_REG_SET &set,
2055 machine_mode mode)
2056 {
2057 HARD_REG_SET temp;
2058
2059 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
2060 temp = set & ~lra_no_alloc_regs;
2061 return (hard_reg_set_subset_p
2062 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
2063 }
2064
2065
2066 /* Used to check validity info about small class input operands. It
2067 should be incremented at start of processing an insn
2068 alternative. */
2069 static unsigned int curr_small_class_check = 0;
2070
2071 /* Update number of used inputs of class OP_CLASS for operand NOP
2072 of alternative NALT. Return true if we have more such class operands
2073 than the number of available regs. */
2074 static bool
2075 update_and_check_small_class_inputs (int nop, int nalt,
2076 enum reg_class op_class)
2077 {
2078 static unsigned int small_class_check[LIM_REG_CLASSES];
2079 static int small_class_input_nums[LIM_REG_CLASSES];
2080
2081 if (SMALL_REGISTER_CLASS_P (op_class)
2082 /* We are interesting in classes became small because of fixing
2083 some hard regs, e.g. by an user through GCC options. */
2084 && hard_reg_set_intersect_p (reg_class_contents[op_class],
2085 ira_no_alloc_regs)
2086 && (curr_static_id->operand[nop].type != OP_OUT
2087 || TEST_BIT (curr_static_id->operand[nop].early_clobber_alts, nalt)))
2088 {
2089 if (small_class_check[op_class] == curr_small_class_check)
2090 small_class_input_nums[op_class]++;
2091 else
2092 {
2093 small_class_check[op_class] = curr_small_class_check;
2094 small_class_input_nums[op_class] = 1;
2095 }
2096 if (small_class_input_nums[op_class] > ira_class_hard_regs_num[op_class])
2097 return true;
2098 }
2099 return false;
2100 }
2101
2102 /* Print operand constraints for alternative ALT_NUMBER of the current
2103 insn. */
2104 static void
2105 print_curr_insn_alt (int alt_number)
2106 {
2107 for (int i = 0; i < curr_static_id->n_operands; i++)
2108 {
2109 const char *p = (curr_static_id->operand_alternative
2110 [alt_number * curr_static_id->n_operands + i].constraint);
2111 if (*p == '\0')
2112 continue;
2113 fprintf (lra_dump_file, " (%d) ", i);
2114 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
2115 fputc (*p, lra_dump_file);
2116 }
2117 }
2118
2119 /* Major function to choose the current insn alternative and what
2120 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
2121 negative we should consider only this alternative. Return false if
2122 we cannot choose the alternative or find how to reload the
2123 operands. */
2124 static bool
2125 process_alt_operands (int only_alternative)
2126 {
2127 bool ok_p = false;
2128 int nop, overall, nalt;
2129 int n_alternatives = curr_static_id->n_alternatives;
2130 int n_operands = curr_static_id->n_operands;
2131 /* LOSERS counts the operands that don't fit this alternative and
2132 would require loading. */
2133 int losers;
2134 int addr_losers;
2135 /* REJECT is a count of how undesirable this alternative says it is
2136 if any reloading is required. If the alternative matches exactly
2137 then REJECT is ignored, but otherwise it gets this much counted
2138 against it in addition to the reloading needed. */
2139 int reject;
2140 /* This is defined by '!' or '?' alternative constraint and added to
2141 reject. But in some cases it can be ignored. */
2142 int static_reject;
2143 int op_reject;
2144 /* The number of elements in the following array. */
2145 int early_clobbered_regs_num;
2146 /* Numbers of operands which are early clobber registers. */
2147 int early_clobbered_nops[MAX_RECOG_OPERANDS];
2148 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
2149 enum reg_class all_this_alternative;
2150 int all_used_nregs, all_reload_nregs;
2151 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
2152 HARD_REG_SET curr_alt_exclude_start_hard_regs[MAX_RECOG_OPERANDS];
2153 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
2154 bool curr_alt_win[MAX_RECOG_OPERANDS];
2155 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
2156 int curr_alt_matches[MAX_RECOG_OPERANDS];
2157 /* The number of elements in the following array. */
2158 int curr_alt_dont_inherit_ops_num;
2159 /* Numbers of operands whose reload pseudos should not be inherited. */
2160 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
2161 bool curr_reuse_alt_p;
2162 /* True if output stack pointer reload should be generated for the current
2163 alternative. */
2164 bool curr_alt_out_sp_reload_p;
2165 bool curr_alt_class_change_p;
2166 rtx op;
2167 /* The register when the operand is a subreg of register, otherwise the
2168 operand itself. */
2169 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
2170 /* The register if the operand is a register or subreg of register,
2171 otherwise NULL. */
2172 rtx operand_reg[MAX_RECOG_OPERANDS];
2173 int hard_regno[MAX_RECOG_OPERANDS];
2174 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
2175 int reload_nregs, reload_sum;
2176 bool costly_p;
2177 enum reg_class cl;
2178 const HARD_REG_SET *cl_filter;
2179
2180 /* Calculate some data common for all alternatives to speed up the
2181 function. */
2182 for (nop = 0; nop < n_operands; nop++)
2183 {
2184 rtx reg;
2185
2186 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
2187 /* The real hard regno of the operand after the allocation. */
2188 hard_regno[nop] = get_hard_regno (op);
2189
2190 operand_reg[nop] = reg = op;
2191 biggest_mode[nop] = GET_MODE (op);
2192 if (GET_CODE (op) == SUBREG)
2193 {
2194 biggest_mode[nop] = wider_subreg_mode (op);
2195 operand_reg[nop] = reg = SUBREG_REG (op);
2196 }
2197 if (! REG_P (reg))
2198 operand_reg[nop] = NULL_RTX;
2199 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
2200 || ((int) REGNO (reg)
2201 == lra_get_elimination_hard_regno (REGNO (reg))))
2202 no_subreg_reg_operand[nop] = reg;
2203 else
2204 operand_reg[nop] = no_subreg_reg_operand[nop]
2205 /* Just use natural mode for elimination result. It should
2206 be enough for extra constraints hooks. */
2207 = regno_reg_rtx[hard_regno[nop]];
2208 }
2209
2210 /* The constraints are made of several alternatives. Each operand's
2211 constraint looks like foo,bar,... with commas separating the
2212 alternatives. The first alternatives for all operands go
2213 together, the second alternatives go together, etc.
2214
2215 First loop over alternatives. */
2216 alternative_mask preferred = curr_id->preferred_alternatives;
2217 if (only_alternative >= 0)
2218 preferred &= ALTERNATIVE_BIT (only_alternative);
2219
2220 bool prefer_memory_p = false;
2221 repeat:
2222 for (nalt = 0; nalt < n_alternatives; nalt++)
2223 {
2224 /* Loop over operands for one constraint alternative. */
2225 if (!TEST_BIT (preferred, nalt))
2226 continue;
2227
2228 if (lra_dump_file != NULL)
2229 {
2230 fprintf (lra_dump_file, " Considering alt=%d of insn %d: ",
2231 nalt, INSN_UID (curr_insn));
2232 print_curr_insn_alt (nalt);
2233 fprintf (lra_dump_file, "\n");
2234 }
2235
2236 bool matching_early_clobber[MAX_RECOG_OPERANDS];
2237 curr_small_class_check++;
2238 overall = losers = addr_losers = 0;
2239 static_reject = reject = reload_nregs = reload_sum = 0;
2240 for (nop = 0; nop < n_operands; nop++)
2241 {
2242 int inc = (curr_static_id
2243 ->operand_alternative[nalt * n_operands + nop].reject);
2244 if (lra_dump_file != NULL && inc != 0)
2245 fprintf (lra_dump_file,
2246 " Staticly defined alt reject+=%d\n", inc);
2247 static_reject += inc;
2248 matching_early_clobber[nop] = 0;
2249 }
2250 reject += static_reject;
2251 early_clobbered_regs_num = 0;
2252 curr_alt_out_sp_reload_p = false;
2253 curr_reuse_alt_p = true;
2254 curr_alt_class_change_p = false;
2255 all_this_alternative = NO_REGS;
2256 all_used_nregs = all_reload_nregs = 0;
2257 for (nop = 0; nop < n_operands; nop++)
2258 {
2259 const char *p;
2260 char *end;
2261 int len, c, m, i, opalt_num, this_alternative_matches;
2262 bool win, did_match, offmemok, early_clobber_p;
2263 /* false => this operand can be reloaded somehow for this
2264 alternative. */
2265 bool badop;
2266 /* true => this operand can be reloaded if the alternative
2267 allows regs. */
2268 bool winreg;
2269 /* True if a constant forced into memory would be OK for
2270 this operand. */
2271 bool constmemok;
2272 enum reg_class this_alternative, this_costly_alternative;
2273 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
2274 HARD_REG_SET this_alternative_exclude_start_hard_regs;
2275 bool this_alternative_match_win, this_alternative_win;
2276 bool this_alternative_offmemok;
2277 bool scratch_p;
2278 machine_mode mode;
2279 enum constraint_num cn;
2280 bool class_change_p = false;
2281
2282 opalt_num = nalt * n_operands + nop;
2283 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
2284 {
2285 /* Fast track for no constraints at all. */
2286 curr_alt[nop] = NO_REGS;
2287 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
2288 curr_alt_win[nop] = true;
2289 curr_alt_match_win[nop] = false;
2290 curr_alt_offmemok[nop] = false;
2291 curr_alt_matches[nop] = -1;
2292 continue;
2293 }
2294
2295 op = no_subreg_reg_operand[nop];
2296 mode = curr_operand_mode[nop];
2297
2298 win = did_match = winreg = offmemok = constmemok = false;
2299 badop = true;
2300
2301 early_clobber_p = false;
2302 p = curr_static_id->operand_alternative[opalt_num].constraint;
2303
2304 this_costly_alternative = this_alternative = NO_REGS;
2305 /* We update set of possible hard regs besides its class
2306 because reg class might be inaccurate. For example,
2307 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
2308 is translated in HI_REGS because classes are merged by
2309 pairs and there is no accurate intermediate class. */
2310 CLEAR_HARD_REG_SET (this_alternative_set);
2311 CLEAR_HARD_REG_SET (this_costly_alternative_set);
2312 CLEAR_HARD_REG_SET (this_alternative_exclude_start_hard_regs);
2313 this_alternative_win = false;
2314 this_alternative_match_win = false;
2315 this_alternative_offmemok = false;
2316 this_alternative_matches = -1;
2317
2318 /* An empty constraint should be excluded by the fast
2319 track. */
2320 lra_assert (*p != 0 && *p != ',');
2321
2322 op_reject = 0;
2323 /* Scan this alternative's specs for this operand; set WIN
2324 if the operand fits any letter in this alternative.
2325 Otherwise, clear BADOP if this operand could fit some
2326 letter after reloads, or set WINREG if this operand could
2327 fit after reloads provided the constraint allows some
2328 registers. */
2329 costly_p = false;
2330 do
2331 {
2332 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
2333 {
2334 case '\0':
2335 len = 0;
2336 break;
2337 case ',':
2338 c = '\0';
2339 break;
2340
2341 case '&':
2342 early_clobber_p = true;
2343 break;
2344
2345 case '$':
2346 op_reject += LRA_MAX_REJECT;
2347 break;
2348 case '^':
2349 op_reject += LRA_LOSER_COST_FACTOR;
2350 break;
2351
2352 case '#':
2353 /* Ignore rest of this alternative. */
2354 c = '\0';
2355 break;
2356
2357 case '0': case '1': case '2': case '3': case '4':
2358 case '5': case '6': case '7': case '8': case '9':
2359 {
2360 int m_hregno;
2361 bool match_p;
2362
2363 m = strtoul (p, &end, 10);
2364 p = end;
2365 len = 0;
2366 lra_assert (nop > m);
2367
2368 /* Reject matches if we don't know which operand is
2369 bigger. This situation would arguably be a bug in
2370 an .md pattern, but could also occur in a user asm. */
2371 if (!ordered_p (GET_MODE_SIZE (biggest_mode[m]),
2372 GET_MODE_SIZE (biggest_mode[nop])))
2373 break;
2374
2375 /* Don't match wrong asm insn operands for proper
2376 diagnostic later. */
2377 if (INSN_CODE (curr_insn) < 0
2378 && (curr_operand_mode[m] == BLKmode
2379 || curr_operand_mode[nop] == BLKmode)
2380 && curr_operand_mode[m] != curr_operand_mode[nop])
2381 break;
2382
2383 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
2384 /* We are supposed to match a previous operand.
2385 If we do, we win if that one did. If we do
2386 not, count both of the operands as losers.
2387 (This is too conservative, since most of the
2388 time only a single reload insn will be needed
2389 to make the two operands win. As a result,
2390 this alternative may be rejected when it is
2391 actually desirable.) */
2392 match_p = false;
2393 if (operands_match_p (*curr_id->operand_loc[nop],
2394 *curr_id->operand_loc[m], m_hregno))
2395 {
2396 /* We should reject matching of an early
2397 clobber operand if the matching operand is
2398 not dying in the insn. */
2399 if (!TEST_BIT (curr_static_id->operand[m]
2400 .early_clobber_alts, nalt)
2401 || operand_reg[nop] == NULL_RTX
2402 || (find_regno_note (curr_insn, REG_DEAD,
2403 REGNO (op))
2404 || REGNO (op) == REGNO (operand_reg[m])))
2405 match_p = true;
2406 }
2407 if (match_p)
2408 {
2409 /* If we are matching a non-offsettable
2410 address where an offsettable address was
2411 expected, then we must reject this
2412 combination, because we can't reload
2413 it. */
2414 if (curr_alt_offmemok[m]
2415 && MEM_P (*curr_id->operand_loc[m])
2416 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
2417 continue;
2418 }
2419 else
2420 {
2421 /* If the operands do not match and one
2422 operand is INOUT, we can not match them.
2423 Try other possibilities, e.g. other
2424 alternatives or commutative operand
2425 exchange. */
2426 if (curr_static_id->operand[nop].type == OP_INOUT
2427 || curr_static_id->operand[m].type == OP_INOUT)
2428 break;
2429 /* Operands don't match. For asm if the operands
2430 are different user defined explicit hard
2431 registers, then we cannot make them match
2432 when one is early clobber operand. */
2433 if ((REG_P (*curr_id->operand_loc[nop])
2434 || SUBREG_P (*curr_id->operand_loc[nop]))
2435 && (REG_P (*curr_id->operand_loc[m])
2436 || SUBREG_P (*curr_id->operand_loc[m]))
2437 && INSN_CODE (curr_insn) < 0)
2438 {
2439 rtx nop_reg = *curr_id->operand_loc[nop];
2440 if (SUBREG_P (nop_reg))
2441 nop_reg = SUBREG_REG (nop_reg);
2442 rtx m_reg = *curr_id->operand_loc[m];
2443 if (SUBREG_P (m_reg))
2444 m_reg = SUBREG_REG (m_reg);
2445
2446 if (REG_P (nop_reg)
2447 && HARD_REGISTER_P (nop_reg)
2448 && REG_USERVAR_P (nop_reg)
2449 && REG_P (m_reg)
2450 && HARD_REGISTER_P (m_reg)
2451 && REG_USERVAR_P (m_reg))
2452 {
2453 int i;
2454
2455 for (i = 0; i < early_clobbered_regs_num; i++)
2456 if (m == early_clobbered_nops[i])
2457 break;
2458 if (i < early_clobbered_regs_num
2459 || early_clobber_p)
2460 break;
2461 }
2462 }
2463 /* Both operands must allow a reload register,
2464 otherwise we cannot make them match. */
2465 if (curr_alt[m] == NO_REGS)
2466 break;
2467 /* Retroactively mark the operand we had to
2468 match as a loser, if it wasn't already and
2469 it wasn't matched to a register constraint
2470 (e.g it might be matched by memory). */
2471 if (curr_alt_win[m]
2472 && (operand_reg[m] == NULL_RTX
2473 || hard_regno[m] < 0))
2474 {
2475 if (lra_dump_file != NULL)
2476 fprintf
2477 (lra_dump_file,
2478 " %d Matched operand reload: "
2479 "losers++\n", m);
2480 losers++;
2481 reload_nregs
2482 += (ira_reg_class_max_nregs[curr_alt[m]]
2483 [GET_MODE (*curr_id->operand_loc[m])]);
2484 }
2485
2486 /* Prefer matching earlyclobber alternative as
2487 it results in less hard regs required for
2488 the insn than a non-matching earlyclobber
2489 alternative. */
2490 if (TEST_BIT (curr_static_id->operand[m]
2491 .early_clobber_alts, nalt))
2492 {
2493 if (lra_dump_file != NULL)
2494 fprintf
2495 (lra_dump_file,
2496 " %d Matching earlyclobber alt:"
2497 " reject--\n",
2498 nop);
2499 if (!matching_early_clobber[m])
2500 {
2501 reject--;
2502 matching_early_clobber[m] = 1;
2503 }
2504 }
2505 /* Otherwise we prefer no matching
2506 alternatives because it gives more freedom
2507 in RA. */
2508 else if (operand_reg[nop] == NULL_RTX
2509 || (find_regno_note (curr_insn, REG_DEAD,
2510 REGNO (operand_reg[nop]))
2511 == NULL_RTX))
2512 {
2513 if (lra_dump_file != NULL)
2514 fprintf
2515 (lra_dump_file,
2516 " %d Matching alt: reject+=2\n",
2517 nop);
2518 reject += 2;
2519 }
2520 }
2521 /* If we have to reload this operand and some
2522 previous operand also had to match the same
2523 thing as this operand, we don't know how to do
2524 that. */
2525 if (!match_p || !curr_alt_win[m])
2526 {
2527 for (i = 0; i < nop; i++)
2528 if (curr_alt_matches[i] == m)
2529 break;
2530 if (i < nop)
2531 break;
2532 }
2533 else
2534 did_match = true;
2535
2536 this_alternative_matches = m;
2537 /* This can be fixed with reloads if the operand
2538 we are supposed to match can be fixed with
2539 reloads. */
2540 badop = false;
2541 this_alternative = curr_alt[m];
2542 this_alternative_set = curr_alt_set[m];
2543 this_alternative_exclude_start_hard_regs
2544 = curr_alt_exclude_start_hard_regs[m];
2545 winreg = this_alternative != NO_REGS;
2546 break;
2547 }
2548
2549 case 'g':
2550 if (MEM_P (op)
2551 || general_constant_p (op)
2552 || spilled_pseudo_p (op))
2553 win = true;
2554 if (REG_P (op) && prefer_memory_p)
2555 {
2556 badop = false;
2557 offmemok = true;
2558 }
2559 cl = GENERAL_REGS;
2560 cl_filter = nullptr;
2561 goto reg;
2562
2563 default:
2564 cn = lookup_constraint (p);
2565 switch (get_constraint_type (cn))
2566 {
2567 case CT_REGISTER:
2568 cl = reg_class_for_constraint (cn);
2569 if (cl != NO_REGS)
2570 {
2571 cl_filter = get_register_filter (cn);
2572 goto reg;
2573 }
2574 break;
2575
2576 case CT_CONST_INT:
2577 if (CONST_INT_P (op)
2578 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2579 win = true;
2580 break;
2581
2582 case CT_MEMORY:
2583 case CT_RELAXED_MEMORY:
2584 if (MEM_P (op)
2585 && satisfies_memory_constraint_p (op, cn))
2586 win = true;
2587 else if (spilled_pseudo_p (op))
2588 win = true;
2589
2590 /* If we didn't already win, we can reload constants
2591 via force_const_mem or put the pseudo value into
2592 memory, or make other memory by reloading the
2593 address like for 'o'. */
2594 if (CONST_POOL_OK_P (mode, op)
2595 || MEM_P (op) || REG_P (op)
2596 /* We can restore the equiv insn by a
2597 reload. */
2598 || equiv_substition_p[nop])
2599 badop = false;
2600 constmemok = true;
2601 offmemok = true;
2602 break;
2603
2604 case CT_ADDRESS:
2605 /* An asm operand with an address constraint
2606 that doesn't satisfy address_operand has
2607 is_address cleared, so that we don't try to
2608 make a non-address fit. */
2609 if (!curr_static_id->operand[nop].is_address)
2610 break;
2611 /* If we didn't already win, we can reload the address
2612 into a base register. */
2613 if (satisfies_address_constraint_p (op, cn))
2614 win = true;
2615 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2616 ADDRESS, SCRATCH);
2617 cl_filter = nullptr;
2618 badop = false;
2619 goto reg;
2620
2621 case CT_FIXED_FORM:
2622 if (constraint_satisfied_p (op, cn))
2623 win = true;
2624 break;
2625
2626 case CT_SPECIAL_MEMORY:
2627 if (satisfies_memory_constraint_p (op, cn))
2628 win = true;
2629 else if (spilled_pseudo_p (op))
2630 {
2631 curr_reuse_alt_p = false;
2632 win = true;
2633 }
2634 break;
2635 }
2636 break;
2637
2638 reg:
2639 if (mode == BLKmode)
2640 break;
2641 this_alternative = reg_class_subunion[this_alternative][cl];
2642 if (hard_reg_set_subset_p (this_alternative_set,
2643 reg_class_contents[cl]))
2644 this_alternative_exclude_start_hard_regs
2645 = ira_exclude_class_mode_regs[cl][mode];
2646 else if (!hard_reg_set_subset_p (reg_class_contents[cl],
2647 this_alternative_set))
2648 this_alternative_exclude_start_hard_regs
2649 |= ira_exclude_class_mode_regs[cl][mode];
2650 this_alternative_set |= reg_class_contents[cl];
2651 if (cl_filter)
2652 this_alternative_exclude_start_hard_regs |= ~*cl_filter;
2653 if (costly_p)
2654 {
2655 this_costly_alternative
2656 = reg_class_subunion[this_costly_alternative][cl];
2657 this_costly_alternative_set |= reg_class_contents[cl];
2658 }
2659 winreg = true;
2660 if (REG_P (op))
2661 {
2662 tree decl;
2663 if (hard_regno[nop] >= 0
2664 && in_hard_reg_set_p (this_alternative_set,
2665 mode, hard_regno[nop])
2666 && (!cl_filter
2667 || TEST_HARD_REG_BIT (*cl_filter,
2668 hard_regno[nop]))
2669 && ((REG_ATTRS (op) && (decl = REG_EXPR (op)) != NULL
2670 && VAR_P (decl) && DECL_HARD_REGISTER (decl))
2671 || !(TEST_HARD_REG_BIT
2672 (this_alternative_exclude_start_hard_regs,
2673 hard_regno[nop]))))
2674 win = true;
2675 else if (hard_regno[nop] < 0 && !prefer_memory_p)
2676 {
2677 if (in_class_p (op, this_alternative, NULL))
2678 win = true;
2679 else if (in_class_p (op, this_alternative, NULL, true))
2680 {
2681 class_change_p = true;
2682 win = true;
2683 }
2684 }
2685 }
2686 break;
2687 }
2688 if (c != ' ' && c != '\t')
2689 costly_p = c == '*';
2690 }
2691 while ((p += len), c);
2692
2693 scratch_p = (operand_reg[nop] != NULL_RTX
2694 && ira_former_scratch_p (REGNO (operand_reg[nop])));
2695 /* Record which operands fit this alternative. */
2696 if (win)
2697 {
2698 if (early_clobber_p
2699 || curr_static_id->operand[nop].type != OP_OUT)
2700 {
2701 if (winreg)
2702 all_used_nregs
2703 += ira_reg_class_min_nregs[this_alternative][mode];
2704 all_this_alternative
2705 = (reg_class_subunion
2706 [all_this_alternative][this_alternative]);
2707 }
2708 this_alternative_win = true;
2709 if (class_change_p)
2710 {
2711 curr_alt_class_change_p = true;
2712 if (lra_dump_file != NULL)
2713 fprintf (lra_dump_file,
2714 " %d Narrowing class: reject+=3\n",
2715 nop);
2716 reject += 3;
2717 }
2718 if (operand_reg[nop] != NULL_RTX)
2719 {
2720 if (hard_regno[nop] >= 0)
2721 {
2722 if (in_hard_reg_set_p (this_costly_alternative_set,
2723 mode, hard_regno[nop]))
2724 {
2725 if (lra_dump_file != NULL)
2726 fprintf (lra_dump_file,
2727 " %d Costly set: reject++\n",
2728 nop);
2729 reject++;
2730 }
2731 }
2732 else
2733 {
2734 /* Prefer won reg to spilled pseudo under other
2735 equal conditions for possibe inheritance. */
2736 if (! scratch_p)
2737 {
2738 if (lra_dump_file != NULL)
2739 fprintf
2740 (lra_dump_file,
2741 " %d Non pseudo reload: reject++\n",
2742 nop);
2743 reject++;
2744 }
2745 if (in_class_p (operand_reg[nop],
2746 this_costly_alternative, NULL, true))
2747 {
2748 if (lra_dump_file != NULL)
2749 fprintf
2750 (lra_dump_file,
2751 " %d Non pseudo costly reload:"
2752 " reject++\n",
2753 nop);
2754 reject++;
2755 }
2756 }
2757 /* We simulate the behavior of old reload here.
2758 Although scratches need hard registers and it
2759 might result in spilling other pseudos, no reload
2760 insns are generated for the scratches. So it
2761 might cost something but probably less than old
2762 reload pass believes. */
2763 if (scratch_p)
2764 {
2765 if (lra_dump_file != NULL)
2766 fprintf (lra_dump_file,
2767 " %d Scratch win: reject+=2\n",
2768 nop);
2769 reject += 2;
2770 }
2771 }
2772 }
2773 else if (did_match)
2774 this_alternative_match_win = true;
2775 else
2776 {
2777 if (prefer_memory_p && offmemok)
2778 {
2779 winreg = false;
2780 this_alternative = NO_REGS;
2781 }
2782
2783 int const_to_mem = 0;
2784 bool no_regs_p;
2785
2786 reject += op_reject;
2787 /* Mark output reload of the stack pointer. */
2788 if (op == stack_pointer_rtx
2789 && curr_static_id->operand[nop].type != OP_IN)
2790 curr_alt_out_sp_reload_p = true;
2791
2792 /* If this alternative asks for a specific reg class, see if there
2793 is at least one allocatable register in that class. */
2794 no_regs_p
2795 = (this_alternative == NO_REGS
2796 || (hard_reg_set_subset_p
2797 (reg_class_contents[this_alternative],
2798 lra_no_alloc_regs)));
2799
2800 /* For asms, verify that the class for this alternative is possible
2801 for the mode that is specified. */
2802 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2803 {
2804 int i;
2805 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2806 if (targetm.hard_regno_mode_ok (i, mode)
2807 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2808 mode, i))
2809 break;
2810 if (i == FIRST_PSEUDO_REGISTER)
2811 winreg = false;
2812 }
2813
2814 /* If this operand accepts a register, and if the
2815 register class has at least one allocatable register,
2816 then this operand can be reloaded. */
2817 if (winreg && !no_regs_p)
2818 badop = false;
2819
2820 if (badop)
2821 {
2822 if (lra_dump_file != NULL)
2823 fprintf (lra_dump_file,
2824 " Bad operand -- refuse\n");
2825 goto fail;
2826 }
2827
2828 if (this_alternative != NO_REGS)
2829 {
2830 HARD_REG_SET available_regs
2831 = (reg_class_contents[this_alternative]
2832 & ~((ira_prohibited_class_mode_regs
2833 [this_alternative][mode])
2834 | lra_no_alloc_regs));
2835 if (!hard_reg_set_empty_p (available_regs))
2836 {
2837 if (early_clobber_p
2838 || curr_static_id->operand[nop].type != OP_OUT)
2839 {
2840 all_reload_nregs
2841 += ira_reg_class_min_nregs[this_alternative][mode];
2842 all_this_alternative
2843 = (reg_class_subunion
2844 [all_this_alternative][this_alternative]);
2845 }
2846 }
2847 else
2848 {
2849 /* There are no hard regs holding a value of given
2850 mode. */
2851 if (offmemok)
2852 {
2853 this_alternative = NO_REGS;
2854 if (lra_dump_file != NULL)
2855 fprintf (lra_dump_file,
2856 " %d Using memory because of"
2857 " a bad mode: reject+=2\n",
2858 nop);
2859 reject += 2;
2860 }
2861 else
2862 {
2863 if (lra_dump_file != NULL)
2864 fprintf (lra_dump_file,
2865 " Wrong mode -- refuse\n");
2866 goto fail;
2867 }
2868 }
2869 }
2870
2871 /* If not assigned pseudo has a class which a subset of
2872 required reg class, it is a less costly alternative
2873 as the pseudo still can get a hard reg of necessary
2874 class. */
2875 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2876 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2877 && ira_class_subset_p[this_alternative][cl])
2878 {
2879 if (lra_dump_file != NULL)
2880 fprintf
2881 (lra_dump_file,
2882 " %d Super set class reg: reject-=3\n", nop);
2883 reject -= 3;
2884 }
2885
2886 this_alternative_offmemok = offmemok;
2887 if (this_costly_alternative != NO_REGS)
2888 {
2889 if (lra_dump_file != NULL)
2890 fprintf (lra_dump_file,
2891 " %d Costly loser: reject++\n", nop);
2892 reject++;
2893 }
2894 /* If the operand is dying, has a matching constraint,
2895 and satisfies constraints of the matched operand
2896 which failed to satisfy the own constraints, most probably
2897 the reload for this operand will be gone. */
2898 if (this_alternative_matches >= 0
2899 && !curr_alt_win[this_alternative_matches]
2900 && REG_P (op)
2901 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2902 && (hard_regno[nop] >= 0
2903 ? in_hard_reg_set_p (this_alternative_set,
2904 mode, hard_regno[nop])
2905 : in_class_p (op, this_alternative, NULL)))
2906 {
2907 if (lra_dump_file != NULL)
2908 fprintf
2909 (lra_dump_file,
2910 " %d Dying matched operand reload: reject++\n",
2911 nop);
2912 reject++;
2913 }
2914 else
2915 {
2916 /* Strict_low_part requires to reload the register
2917 not the sub-register. In this case we should
2918 check that a final reload hard reg can hold the
2919 value mode. */
2920 if (curr_static_id->operand[nop].strict_low
2921 && REG_P (op)
2922 && hard_regno[nop] < 0
2923 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2924 && ira_class_hard_regs_num[this_alternative] > 0
2925 && (!targetm.hard_regno_mode_ok
2926 (ira_class_hard_regs[this_alternative][0],
2927 GET_MODE (*curr_id->operand_loc[nop]))))
2928 {
2929 if (lra_dump_file != NULL)
2930 fprintf
2931 (lra_dump_file,
2932 " Strict low subreg reload -- refuse\n");
2933 goto fail;
2934 }
2935 if (lra_dump_file != NULL)
2936 fprintf
2937 (lra_dump_file,
2938 " %d Operand reload: losers++\n", nop);
2939 losers++;
2940 }
2941 if (operand_reg[nop] != NULL_RTX
2942 /* Output operands and matched input operands are
2943 not inherited. The following conditions do not
2944 exactly describe the previous statement but they
2945 are pretty close. */
2946 && curr_static_id->operand[nop].type != OP_OUT
2947 && (this_alternative_matches < 0
2948 || curr_static_id->operand[nop].type != OP_IN))
2949 {
2950 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2951 (operand_reg[nop])]
2952 .last_reload);
2953
2954 /* The value of reload_sum has sense only if we
2955 process insns in their order. It happens only on
2956 the first constraints sub-pass when we do most of
2957 reload work. */
2958 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2959 reload_sum += last_reload - bb_reload_num;
2960 }
2961 /* If this is a constant that is reloaded into the
2962 desired class by copying it to memory first, count
2963 that as another reload. This is consistent with
2964 other code and is required to avoid choosing another
2965 alternative when the constant is moved into memory.
2966 Note that the test here is precisely the same as in
2967 the code below that calls force_const_mem. */
2968 if (CONST_POOL_OK_P (mode, op)
2969 && ((targetm.preferred_reload_class
2970 (op, this_alternative) == NO_REGS)
2971 || no_input_reloads_p))
2972 {
2973 const_to_mem = 1;
2974 if (! no_regs_p)
2975 {
2976 if (lra_dump_file != NULL)
2977 fprintf
2978 (lra_dump_file,
2979 " %d Constant reload through memory: "
2980 "losers++\n", nop);
2981 losers++;
2982 }
2983 }
2984
2985 /* Alternative loses if it requires a type of reload not
2986 permitted for this insn. We can always reload
2987 objects with a REG_UNUSED note. */
2988 if ((curr_static_id->operand[nop].type != OP_IN
2989 && no_output_reloads_p
2990 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2991 || (curr_static_id->operand[nop].type != OP_OUT
2992 && no_input_reloads_p && ! const_to_mem)
2993 || (this_alternative_matches >= 0
2994 && (no_input_reloads_p
2995 || (no_output_reloads_p
2996 && (curr_static_id->operand
2997 [this_alternative_matches].type != OP_IN)
2998 && ! find_reg_note (curr_insn, REG_UNUSED,
2999 no_subreg_reg_operand
3000 [this_alternative_matches])))))
3001 {
3002 if (lra_dump_file != NULL)
3003 fprintf
3004 (lra_dump_file,
3005 " No input/output reload -- refuse\n");
3006 goto fail;
3007 }
3008
3009 /* Alternative loses if it required class pseudo cannot
3010 hold value of required mode. Such insns can be
3011 described by insn definitions with mode iterators. */
3012 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
3013 && ! hard_reg_set_empty_p (this_alternative_set)
3014 /* It is common practice for constraints to use a
3015 class which does not have actually enough regs to
3016 hold the value (e.g. x86 AREG for mode requiring
3017 more one general reg). Therefore we have 2
3018 conditions to check that the reload pseudo cannot
3019 hold the mode value. */
3020 && (!targetm.hard_regno_mode_ok
3021 (ira_class_hard_regs[this_alternative][0],
3022 GET_MODE (*curr_id->operand_loc[nop])))
3023 /* The above condition is not enough as the first
3024 reg in ira_class_hard_regs can be not aligned for
3025 multi-words mode values. */
3026 && (prohibited_class_reg_set_mode_p
3027 (this_alternative, this_alternative_set,
3028 GET_MODE (*curr_id->operand_loc[nop]))))
3029 {
3030 if (lra_dump_file != NULL)
3031 fprintf (lra_dump_file,
3032 " reload pseudo for op %d "
3033 "cannot hold the mode value -- refuse\n",
3034 nop);
3035 goto fail;
3036 }
3037
3038 /* Check strong discouragement of reload of non-constant
3039 into class THIS_ALTERNATIVE. */
3040 if (! CONSTANT_P (op) && ! no_regs_p
3041 && (targetm.preferred_reload_class
3042 (op, this_alternative) == NO_REGS
3043 || (curr_static_id->operand[nop].type == OP_OUT
3044 && (targetm.preferred_output_reload_class
3045 (op, this_alternative) == NO_REGS))))
3046 {
3047 if (offmemok && REG_P (op))
3048 {
3049 if (lra_dump_file != NULL)
3050 fprintf
3051 (lra_dump_file,
3052 " %d Spill pseudo into memory: reject+=3\n",
3053 nop);
3054 reject += 3;
3055 }
3056 else
3057 {
3058 if (lra_dump_file != NULL)
3059 fprintf
3060 (lra_dump_file,
3061 " %d Non-prefered reload: reject+=%d\n",
3062 nop, LRA_MAX_REJECT);
3063 reject += LRA_MAX_REJECT;
3064 }
3065 }
3066
3067 if (! (MEM_P (op) && offmemok)
3068 && ! (const_to_mem && constmemok))
3069 {
3070 /* We prefer to reload pseudos over reloading other
3071 things, since such reloads may be able to be
3072 eliminated later. So bump REJECT in other cases.
3073 Don't do this in the case where we are forcing a
3074 constant into memory and it will then win since
3075 we don't want to have a different alternative
3076 match then. */
3077 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
3078 {
3079 if (lra_dump_file != NULL)
3080 fprintf
3081 (lra_dump_file,
3082 " %d Non-pseudo reload: reject+=2\n",
3083 nop);
3084 reject += 2;
3085 }
3086
3087 if (! no_regs_p)
3088 reload_nregs
3089 += ira_reg_class_max_nregs[this_alternative][mode];
3090
3091 if (SMALL_REGISTER_CLASS_P (this_alternative))
3092 {
3093 if (lra_dump_file != NULL)
3094 fprintf
3095 (lra_dump_file,
3096 " %d Small class reload: reject+=%d\n",
3097 nop, LRA_LOSER_COST_FACTOR / 2);
3098 reject += LRA_LOSER_COST_FACTOR / 2;
3099 }
3100 }
3101
3102 /* We are trying to spill pseudo into memory. It is
3103 usually more costly than moving to a hard register
3104 although it might takes the same number of
3105 reloads.
3106
3107 Non-pseudo spill may happen also. Suppose a target allows both
3108 register and memory in the operand constraint alternatives,
3109 then it's typical that an eliminable register has a substition
3110 of "base + offset" which can either be reloaded by a simple
3111 "new_reg <= base + offset" which will match the register
3112 constraint, or a similar reg addition followed by further spill
3113 to and reload from memory which will match the memory
3114 constraint, but this memory spill will be much more costly
3115 usually.
3116
3117 Code below increases the reject for both pseudo and non-pseudo
3118 spill. */
3119 if (no_regs_p
3120 && !(MEM_P (op) && offmemok)
3121 && !(REG_P (op) && hard_regno[nop] < 0))
3122 {
3123 if (lra_dump_file != NULL)
3124 fprintf
3125 (lra_dump_file,
3126 " %d Spill %spseudo into memory: reject+=3\n",
3127 nop, REG_P (op) ? "" : "Non-");
3128 reject += 3;
3129 if (VECTOR_MODE_P (mode))
3130 {
3131 /* Spilling vectors into memory is usually more
3132 costly as they contain big values. */
3133 if (lra_dump_file != NULL)
3134 fprintf
3135 (lra_dump_file,
3136 " %d Spill vector pseudo: reject+=2\n",
3137 nop);
3138 reject += 2;
3139 }
3140 }
3141
3142 /* When we use an operand requiring memory in given
3143 alternative, the insn should write *and* read the
3144 value to/from memory it is costly in comparison with
3145 an insn alternative which does not use memory
3146 (e.g. register or immediate operand). We exclude
3147 memory operand for such case as we can satisfy the
3148 memory constraints by reloading address. */
3149 if (no_regs_p && offmemok && !MEM_P (op))
3150 {
3151 if (lra_dump_file != NULL)
3152 fprintf
3153 (lra_dump_file,
3154 " Using memory insn operand %d: reject+=3\n",
3155 nop);
3156 reject += 3;
3157 }
3158
3159 /* If reload requires moving value through secondary
3160 memory, it will need one more insn at least. */
3161 if (this_alternative != NO_REGS
3162 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
3163 && ((curr_static_id->operand[nop].type != OP_OUT
3164 && targetm.secondary_memory_needed (mode, cl,
3165 this_alternative))
3166 || (curr_static_id->operand[nop].type != OP_IN
3167 && (targetm.secondary_memory_needed
3168 (mode, this_alternative, cl)))))
3169 {
3170 if (lra_dump_file != NULL)
3171 fprintf
3172 (lra_dump_file,
3173 " %d Secondary memory reload needed: "
3174 "losers++\n", nop);
3175 losers++;
3176 }
3177
3178 if (MEM_P (op) && offmemok)
3179 addr_losers++;
3180 else
3181 {
3182 /* Input reloads can be inherited more often than
3183 output reloads can be removed, so penalize output
3184 reloads. */
3185 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
3186 {
3187 if (lra_dump_file != NULL)
3188 fprintf
3189 (lra_dump_file,
3190 " %d Non input pseudo reload: reject++\n",
3191 nop);
3192 reject++;
3193 }
3194
3195 if (curr_static_id->operand[nop].type == OP_INOUT)
3196 {
3197 if (lra_dump_file != NULL)
3198 fprintf
3199 (lra_dump_file,
3200 " %d Input/Output reload: reject+=%d\n",
3201 nop, LRA_LOSER_COST_FACTOR);
3202 reject += LRA_LOSER_COST_FACTOR;
3203 }
3204 }
3205 }
3206
3207 if (early_clobber_p && ! scratch_p)
3208 {
3209 if (lra_dump_file != NULL)
3210 fprintf (lra_dump_file,
3211 " %d Early clobber: reject++\n", nop);
3212 reject++;
3213 }
3214 /* ??? We check early clobbers after processing all operands
3215 (see loop below) and there we update the costs more.
3216 Should we update the cost (may be approximately) here
3217 because of early clobber register reloads or it is a rare
3218 or non-important thing to be worth to do it. */
3219 overall = (losers * LRA_LOSER_COST_FACTOR + reject
3220 - (addr_losers == losers ? static_reject : 0));
3221 if ((best_losers == 0 || losers != 0) && best_overall < overall)
3222 {
3223 if (lra_dump_file != NULL)
3224 fprintf (lra_dump_file,
3225 " overall=%d,losers=%d -- refuse\n",
3226 overall, losers);
3227 goto fail;
3228 }
3229
3230 if (update_and_check_small_class_inputs (nop, nalt,
3231 this_alternative))
3232 {
3233 if (lra_dump_file != NULL)
3234 fprintf (lra_dump_file,
3235 " not enough small class regs -- refuse\n");
3236 goto fail;
3237 }
3238 curr_alt[nop] = this_alternative;
3239 curr_alt_set[nop] = this_alternative_set;
3240 curr_alt_exclude_start_hard_regs[nop]
3241 = this_alternative_exclude_start_hard_regs;
3242 curr_alt_win[nop] = this_alternative_win;
3243 curr_alt_match_win[nop] = this_alternative_match_win;
3244 curr_alt_offmemok[nop] = this_alternative_offmemok;
3245 curr_alt_matches[nop] = this_alternative_matches;
3246
3247 if (this_alternative_matches >= 0
3248 && !did_match && !this_alternative_win)
3249 curr_alt_win[this_alternative_matches] = false;
3250
3251 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
3252 early_clobbered_nops[early_clobbered_regs_num++] = nop;
3253 }
3254
3255 if (curr_insn_set != NULL_RTX && n_operands == 2
3256 /* Prevent processing non-move insns. */
3257 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
3258 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
3259 && ((! curr_alt_win[0] && ! curr_alt_win[1]
3260 && REG_P (no_subreg_reg_operand[0])
3261 && REG_P (no_subreg_reg_operand[1])
3262 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
3263 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
3264 || (! curr_alt_win[0] && curr_alt_win[1]
3265 && REG_P (no_subreg_reg_operand[1])
3266 /* Check that we reload memory not the memory
3267 address. */
3268 && ! (curr_alt_offmemok[0]
3269 && MEM_P (no_subreg_reg_operand[0]))
3270 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
3271 || (curr_alt_win[0] && ! curr_alt_win[1]
3272 && REG_P (no_subreg_reg_operand[0])
3273 /* Check that we reload memory not the memory
3274 address. */
3275 && ! (curr_alt_offmemok[1]
3276 && MEM_P (no_subreg_reg_operand[1]))
3277 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
3278 && (! CONST_POOL_OK_P (curr_operand_mode[1],
3279 no_subreg_reg_operand[1])
3280 || (targetm.preferred_reload_class
3281 (no_subreg_reg_operand[1],
3282 (enum reg_class) curr_alt[1]) != NO_REGS))
3283 /* If it is a result of recent elimination in move
3284 insn we can transform it into an add still by
3285 using this alternative. */
3286 && GET_CODE (no_subreg_reg_operand[1]) != PLUS
3287 /* Likewise if the source has been replaced with an
3288 equivalent value. This only happens once -- the reload
3289 will use the equivalent value instead of the register it
3290 replaces -- so there should be no danger of cycling. */
3291 && !equiv_substition_p[1])))
3292 {
3293 /* We have a move insn and a new reload insn will be similar
3294 to the current insn. We should avoid such situation as
3295 it results in LRA cycling. */
3296 if (lra_dump_file != NULL)
3297 fprintf (lra_dump_file,
3298 " Cycle danger: overall += LRA_MAX_REJECT\n");
3299 overall += LRA_MAX_REJECT;
3300 }
3301 if (all_this_alternative != NO_REGS
3302 && !SMALL_REGISTER_CLASS_P (all_this_alternative)
3303 && all_used_nregs != 0 && all_reload_nregs != 0
3304 && (all_used_nregs + all_reload_nregs + 1
3305 >= ira_class_hard_regs_num[all_this_alternative]))
3306 {
3307 if (lra_dump_file != NULL)
3308 fprintf
3309 (lra_dump_file,
3310 " Register starvation: overall += LRA_MAX_REJECT"
3311 "(class=%s,avail=%d,used=%d,reload=%d)\n",
3312 reg_class_names[all_this_alternative],
3313 ira_class_hard_regs_num[all_this_alternative],
3314 all_used_nregs, all_reload_nregs);
3315 overall += LRA_MAX_REJECT;
3316 if (!prefer_memory_p && INSN_CODE (curr_insn) < 0)
3317 {
3318 /* asm can permit memory and reg and can be not enough regs for
3319 asm -- try now memory: */
3320 prefer_memory_p = true;
3321 if (lra_dump_file != NULL)
3322 fprintf
3323 (lra_dump_file,
3324 " Trying now memory for operands\n");
3325 goto repeat;
3326 }
3327 }
3328 ok_p = true;
3329 curr_alt_dont_inherit_ops_num = 0;
3330 for (nop = 0; nop < early_clobbered_regs_num; nop++)
3331 {
3332 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
3333 HARD_REG_SET temp_set;
3334
3335 i = early_clobbered_nops[nop];
3336 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
3337 || hard_regno[i] < 0)
3338 continue;
3339 lra_assert (operand_reg[i] != NULL_RTX);
3340 clobbered_hard_regno = hard_regno[i];
3341 CLEAR_HARD_REG_SET (temp_set);
3342 add_to_hard_reg_set (&temp_set, GET_MODE (*curr_id->operand_loc[i]),
3343 clobbered_hard_regno);
3344 first_conflict_j = last_conflict_j = -1;
3345 for (j = 0; j < n_operands; j++)
3346 if (j == i
3347 /* We don't want process insides of match_operator and
3348 match_parallel because otherwise we would process
3349 their operands once again generating a wrong
3350 code. */
3351 || curr_static_id->operand[j].is_operator)
3352 continue;
3353 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
3354 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
3355 continue;
3356 /* If we don't reload j-th operand, check conflicts. */
3357 else if ((curr_alt_win[j] || curr_alt_match_win[j])
3358 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
3359 {
3360 if (first_conflict_j < 0)
3361 first_conflict_j = j;
3362 last_conflict_j = j;
3363 /* Both the earlyclobber operand and conflicting operand
3364 cannot both be user defined hard registers for asm.
3365 Let curr_insn_transform diagnose it. */
3366 if (HARD_REGISTER_P (operand_reg[i])
3367 && REG_USERVAR_P (operand_reg[i])
3368 && operand_reg[j] != NULL_RTX
3369 && HARD_REGISTER_P (operand_reg[j])
3370 && REG_USERVAR_P (operand_reg[j])
3371 && INSN_CODE (curr_insn) < 0)
3372 return false;
3373 }
3374 if (last_conflict_j < 0)
3375 continue;
3376
3377 /* If an earlyclobber operand conflicts with another non-matching
3378 operand (ie, they have been assigned the same hard register),
3379 then it is better to reload the other operand, as there may
3380 exist yet another operand with a matching constraint associated
3381 with the earlyclobber operand. However, if one of the operands
3382 is an explicit use of a hard register, then we must reload the
3383 other non-hard register operand. */
3384 if (HARD_REGISTER_P (operand_reg[i])
3385 || (first_conflict_j == last_conflict_j
3386 && operand_reg[last_conflict_j] != NULL_RTX
3387 && !curr_alt_match_win[last_conflict_j]
3388 && !HARD_REGISTER_P (operand_reg[last_conflict_j])))
3389 {
3390 curr_alt_win[last_conflict_j] = false;
3391 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
3392 = last_conflict_j;
3393 losers++;
3394 if (lra_dump_file != NULL)
3395 fprintf
3396 (lra_dump_file,
3397 " %d Conflict early clobber reload: losers++\n",
3398 i);
3399 }
3400 else
3401 {
3402 /* We need to reload early clobbered register and the
3403 matched registers. */
3404 for (j = 0; j < n_operands; j++)
3405 if (curr_alt_matches[j] == i)
3406 {
3407 curr_alt_match_win[j] = false;
3408 losers++;
3409 if (lra_dump_file != NULL)
3410 fprintf
3411 (lra_dump_file,
3412 " %d Matching conflict early clobber "
3413 "reloads: losers++\n",
3414 j);
3415 overall += LRA_LOSER_COST_FACTOR;
3416 }
3417 if (! curr_alt_match_win[i])
3418 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
3419 else
3420 {
3421 /* Remember pseudos used for match reloads are never
3422 inherited. */
3423 lra_assert (curr_alt_matches[i] >= 0);
3424 curr_alt_win[curr_alt_matches[i]] = false;
3425 }
3426 curr_alt_win[i] = curr_alt_match_win[i] = false;
3427 losers++;
3428 if (lra_dump_file != NULL)
3429 fprintf
3430 (lra_dump_file,
3431 " %d Matched conflict early clobber reloads: "
3432 "losers++\n",
3433 i);
3434 }
3435 /* Early clobber was already reflected in REJECT. */
3436 if (!matching_early_clobber[i])
3437 {
3438 lra_assert (reject > 0);
3439 reject--;
3440 matching_early_clobber[i] = 1;
3441 }
3442 overall += LRA_LOSER_COST_FACTOR - 1;
3443 }
3444 if (lra_dump_file != NULL)
3445 fprintf (lra_dump_file, " overall=%d,losers=%d,rld_nregs=%d\n",
3446 overall, losers, reload_nregs);
3447
3448 /* If this alternative can be made to work by reloading, and it
3449 needs less reloading than the others checked so far, record
3450 it as the chosen goal for reloading. */
3451 if ((best_losers != 0 && losers == 0)
3452 || (((best_losers == 0 && losers == 0)
3453 || (best_losers != 0 && losers != 0))
3454 && (best_overall > overall
3455 || (best_overall == overall
3456 /* If the cost of the reloads is the same,
3457 prefer alternative which requires minimal
3458 number of reload regs. */
3459 && (reload_nregs < best_reload_nregs
3460 || (reload_nregs == best_reload_nregs
3461 && (best_reload_sum < reload_sum
3462 || (best_reload_sum == reload_sum
3463 && nalt < goal_alt_number))))))))
3464 {
3465 for (nop = 0; nop < n_operands; nop++)
3466 {
3467 goal_alt_win[nop] = curr_alt_win[nop];
3468 goal_alt_match_win[nop] = curr_alt_match_win[nop];
3469 goal_alt_matches[nop] = curr_alt_matches[nop];
3470 goal_alt[nop] = curr_alt[nop];
3471 goal_alt_exclude_start_hard_regs[nop]
3472 = curr_alt_exclude_start_hard_regs[nop];
3473 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
3474 }
3475 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
3476 goal_reuse_alt_p = curr_reuse_alt_p;
3477 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
3478 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
3479 goal_alt_swapped = curr_swapped;
3480 goal_alt_out_sp_reload_p = curr_alt_out_sp_reload_p;
3481 best_overall = overall;
3482 best_losers = losers;
3483 best_reload_nregs = reload_nregs;
3484 best_reload_sum = reload_sum;
3485 goal_alt_number = nalt;
3486 }
3487 if (losers == 0 && !curr_alt_class_change_p)
3488 /* Everything is satisfied. Do not process alternatives
3489 anymore. */
3490 break;
3491 fail:
3492 ;
3493 }
3494 return ok_p;
3495 }
3496
3497 /* Make reload base reg from address AD. */
3498 static rtx
3499 base_to_reg (struct address_info *ad)
3500 {
3501 enum reg_class cl;
3502 int code = -1;
3503 rtx new_inner = NULL_RTX;
3504 rtx new_reg = NULL_RTX;
3505 rtx_insn *insn;
3506 rtx_insn *last_insn = get_last_insn();
3507
3508 lra_assert (ad->disp == ad->disp_term);
3509 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
3510 get_index_code (ad));
3511 new_reg = lra_create_new_reg (GET_MODE (*ad->base), NULL_RTX, cl, NULL,
3512 "base");
3513 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
3514 ad->disp_term == NULL
3515 ? const0_rtx
3516 : *ad->disp_term);
3517 if (!valid_address_p (ad->mode, new_inner, ad->as))
3518 return NULL_RTX;
3519 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base));
3520 code = recog_memoized (insn);
3521 if (code < 0)
3522 {
3523 delete_insns_since (last_insn);
3524 return NULL_RTX;
3525 }
3526
3527 return new_inner;
3528 }
3529
3530 /* Make reload base reg + DISP from address AD. Return the new pseudo. */
3531 static rtx
3532 base_plus_disp_to_reg (struct address_info *ad, rtx disp)
3533 {
3534 enum reg_class cl;
3535 rtx new_reg;
3536
3537 lra_assert (ad->base == ad->base_term);
3538 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
3539 get_index_code (ad));
3540 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX, cl, NULL,
3541 "base + disp");
3542 lra_emit_add (new_reg, *ad->base_term, disp);
3543 return new_reg;
3544 }
3545
3546 /* Make reload of index part of address AD. Return the new
3547 pseudo. */
3548 static rtx
3549 index_part_to_reg (struct address_info *ad, enum reg_class index_class)
3550 {
3551 rtx new_reg;
3552
3553 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
3554 index_class, NULL, "index term");
3555 expand_mult (GET_MODE (*ad->index), *ad->index_term,
3556 GEN_INT (get_index_scale (ad)), new_reg, 1);
3557 return new_reg;
3558 }
3559
3560 /* Return true if we can add a displacement to address AD, even if that
3561 makes the address invalid. The fix-up code requires any new address
3562 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
3563 static bool
3564 can_add_disp_p (struct address_info *ad)
3565 {
3566 return (!ad->autoinc_p
3567 && ad->segment == NULL
3568 && ad->base == ad->base_term
3569 && ad->disp == ad->disp_term);
3570 }
3571
3572 /* Make equiv substitution in address AD. Return true if a substitution
3573 was made. */
3574 static bool
3575 equiv_address_substitution (struct address_info *ad)
3576 {
3577 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
3578 poly_int64 disp;
3579 HOST_WIDE_INT scale;
3580 bool change_p;
3581
3582 base_term = strip_subreg (ad->base_term);
3583 if (base_term == NULL)
3584 base_reg = new_base_reg = NULL_RTX;
3585 else
3586 {
3587 base_reg = *base_term;
3588 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
3589 }
3590 index_term = strip_subreg (ad->index_term);
3591 if (index_term == NULL)
3592 index_reg = new_index_reg = NULL_RTX;
3593 else
3594 {
3595 index_reg = *index_term;
3596 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
3597 }
3598 if (base_reg == new_base_reg && index_reg == new_index_reg)
3599 return false;
3600 disp = 0;
3601 change_p = false;
3602 if (lra_dump_file != NULL)
3603 {
3604 fprintf (lra_dump_file, "Changing address in insn %d ",
3605 INSN_UID (curr_insn));
3606 dump_value_slim (lra_dump_file, *ad->outer, 1);
3607 }
3608 if (base_reg != new_base_reg)
3609 {
3610 poly_int64 offset;
3611 if (REG_P (new_base_reg))
3612 {
3613 *base_term = new_base_reg;
3614 change_p = true;
3615 }
3616 else if (GET_CODE (new_base_reg) == PLUS
3617 && REG_P (XEXP (new_base_reg, 0))
3618 && poly_int_rtx_p (XEXP (new_base_reg, 1), &offset)
3619 && can_add_disp_p (ad))
3620 {
3621 disp += offset;
3622 *base_term = XEXP (new_base_reg, 0);
3623 change_p = true;
3624 }
3625 if (ad->base_term2 != NULL)
3626 *ad->base_term2 = *ad->base_term;
3627 }
3628 if (index_reg != new_index_reg)
3629 {
3630 poly_int64 offset;
3631 if (REG_P (new_index_reg))
3632 {
3633 *index_term = new_index_reg;
3634 change_p = true;
3635 }
3636 else if (GET_CODE (new_index_reg) == PLUS
3637 && REG_P (XEXP (new_index_reg, 0))
3638 && poly_int_rtx_p (XEXP (new_index_reg, 1), &offset)
3639 && can_add_disp_p (ad)
3640 && (scale = get_index_scale (ad)))
3641 {
3642 disp += offset * scale;
3643 *index_term = XEXP (new_index_reg, 0);
3644 change_p = true;
3645 }
3646 }
3647 if (maybe_ne (disp, 0))
3648 {
3649 if (ad->disp != NULL)
3650 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
3651 else
3652 {
3653 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
3654 update_address (ad);
3655 }
3656 change_p = true;
3657 }
3658 if (lra_dump_file != NULL)
3659 {
3660 if (! change_p)
3661 fprintf (lra_dump_file, " -- no change\n");
3662 else
3663 {
3664 fprintf (lra_dump_file, " on equiv ");
3665 dump_value_slim (lra_dump_file, *ad->outer, 1);
3666 fprintf (lra_dump_file, "\n");
3667 }
3668 }
3669 return change_p;
3670 }
3671
3672 /* Skip all modifiers and whitespaces in constraint STR and return the
3673 result. */
3674 static const char *
3675 skip_constraint_modifiers (const char *str)
3676 {
3677 for (;;str++)
3678 switch (*str)
3679 {
3680 case '+': case '&' : case '=': case '*': case ' ': case '\t':
3681 case '$': case '^' : case '%': case '?': case '!':
3682 break;
3683 default: return str;
3684 }
3685 }
3686
3687 /* Takes a string of 0 or more comma-separated constraints. When more
3688 than one constraint is present, evaluate whether they all correspond
3689 to a single, repeated constraint (e.g. "r,r") or whether we have
3690 more than one distinct constraints (e.g. "r,m"). */
3691 static bool
3692 constraint_unique (const char *cstr)
3693 {
3694 enum constraint_num ca, cb;
3695 ca = CONSTRAINT__UNKNOWN;
3696 for (;;)
3697 {
3698 cstr = skip_constraint_modifiers (cstr);
3699 if (*cstr == '\0' || *cstr == ',')
3700 cb = CONSTRAINT_X;
3701 else
3702 {
3703 cb = lookup_constraint (cstr);
3704 if (cb == CONSTRAINT__UNKNOWN)
3705 return false;
3706 cstr += CONSTRAINT_LEN (cstr[0], cstr);
3707 }
3708 /* Handle the first iteration of the loop. */
3709 if (ca == CONSTRAINT__UNKNOWN)
3710 ca = cb;
3711 /* Handle the general case of comparing ca with subsequent
3712 constraints. */
3713 else if (ca != cb)
3714 return false;
3715 if (*cstr == '\0')
3716 return true;
3717 if (*cstr == ',')
3718 cstr += 1;
3719 }
3720 }
3721
3722 /* Major function to make reloads for an address in operand NOP or
3723 check its correctness (If CHECK_ONLY_P is true). The supported
3724 cases are:
3725
3726 1) an address that existed before LRA started, at which point it
3727 must have been valid. These addresses are subject to elimination
3728 and may have become invalid due to the elimination offset being out
3729 of range.
3730
3731 2) an address created by forcing a constant to memory
3732 (force_const_to_mem). The initial form of these addresses might
3733 not be valid, and it is this function's job to make them valid.
3734
3735 3) a frame address formed from a register and a (possibly zero)
3736 constant offset. As above, these addresses might not be valid and
3737 this function must make them so.
3738
3739 Add reloads to the lists *BEFORE and *AFTER. We might need to add
3740 reloads to *AFTER because of inc/dec, {pre, post} modify in the
3741 address. Return true for any RTL change.
3742
3743 The function is a helper function which does not produce all
3744 transformations (when CHECK_ONLY_P is false) which can be
3745 necessary. It does just basic steps. To do all necessary
3746 transformations use function process_address. */
3747 static bool
3748 process_address_1 (int nop, bool check_only_p,
3749 rtx_insn **before, rtx_insn **after)
3750 {
3751 struct address_info ad;
3752 rtx new_reg;
3753 HOST_WIDE_INT scale;
3754 rtx op = *curr_id->operand_loc[nop];
3755 rtx mem = extract_mem_from_operand (op);
3756 const char *constraint;
3757 enum constraint_num cn;
3758 bool change_p = false;
3759
3760 if (MEM_P (mem)
3761 && GET_MODE (mem) == BLKmode
3762 && GET_CODE (XEXP (mem, 0)) == SCRATCH)
3763 return false;
3764
3765 constraint
3766 = skip_constraint_modifiers (curr_static_id->operand[nop].constraint);
3767 if (IN_RANGE (constraint[0], '0', '9'))
3768 {
3769 char *end;
3770 unsigned long dup = strtoul (constraint, &end, 10);
3771 constraint
3772 = skip_constraint_modifiers (curr_static_id->operand[dup].constraint);
3773 }
3774 cn = lookup_constraint (*constraint == '\0' ? "X" : constraint);
3775 /* If we have several alternatives or/and several constraints in an
3776 alternative and we can not say at this stage what constraint will be used,
3777 use unknown constraint. The exception is an address constraint. If
3778 operand has one address constraint, probably all others constraints are
3779 address ones. */
3780 if (constraint[0] != '\0' && get_constraint_type (cn) != CT_ADDRESS
3781 && !constraint_unique (constraint))
3782 cn = CONSTRAINT__UNKNOWN;
3783 if (insn_extra_address_constraint (cn)
3784 /* When we find an asm operand with an address constraint that
3785 doesn't satisfy address_operand to begin with, we clear
3786 is_address, so that we don't try to make a non-address fit.
3787 If the asm statement got this far, it's because other
3788 constraints are available, and we'll use them, disregarding
3789 the unsatisfiable address ones. */
3790 && curr_static_id->operand[nop].is_address)
3791 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
3792 /* Do not attempt to decompose arbitrary addresses generated by combine
3793 for asm operands with loose constraints, e.g 'X'.
3794 Need to extract memory from op for special memory constraint,
3795 i.e. bcst_mem_operand in i386 backend. */
3796 else if (MEM_P (mem)
3797 && !(INSN_CODE (curr_insn) < 0
3798 && get_constraint_type (cn) == CT_FIXED_FORM
3799 && constraint_satisfied_p (op, cn)))
3800 decompose_mem_address (&ad, mem);
3801 else if (GET_CODE (op) == SUBREG
3802 && MEM_P (SUBREG_REG (op)))
3803 decompose_mem_address (&ad, SUBREG_REG (op));
3804 else
3805 return false;
3806 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
3807 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
3808 when INDEX_REG_CLASS is a single register class. */
3809 enum reg_class index_cl = index_reg_class (curr_insn);
3810 if (ad.base_term != NULL
3811 && ad.index_term != NULL
3812 && ira_class_hard_regs_num[index_cl] == 1
3813 && REG_P (*ad.base_term)
3814 && REG_P (*ad.index_term)
3815 && in_class_p (*ad.base_term, index_cl, NULL)
3816 && ! in_class_p (*ad.index_term, index_cl, NULL))
3817 {
3818 std::swap (ad.base, ad.index);
3819 std::swap (ad.base_term, ad.index_term);
3820 }
3821 if (! check_only_p)
3822 change_p = equiv_address_substitution (&ad);
3823 if (ad.base_term != NULL
3824 && (process_addr_reg
3825 (ad.base_term, check_only_p, before,
3826 (ad.autoinc_p
3827 && !(REG_P (*ad.base_term)
3828 && find_regno_note (curr_insn, REG_DEAD,
3829 REGNO (*ad.base_term)) != NULL_RTX)
3830 ? after : NULL),
3831 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3832 get_index_code (&ad), curr_insn))))
3833 {
3834 change_p = true;
3835 if (ad.base_term2 != NULL)
3836 *ad.base_term2 = *ad.base_term;
3837 }
3838 if (ad.index_term != NULL
3839 && process_addr_reg (ad.index_term, check_only_p,
3840 before, NULL, index_cl))
3841 change_p = true;
3842
3843 /* Target hooks sometimes don't treat extra-constraint addresses as
3844 legitimate address_operands, so handle them specially. */
3845 if (insn_extra_address_constraint (cn)
3846 && satisfies_address_constraint_p (&ad, cn))
3847 return change_p;
3848
3849 if (check_only_p)
3850 return change_p;
3851
3852 /* There are three cases where the shape of *AD.INNER may now be invalid:
3853
3854 1) the original address was valid, but either elimination or
3855 equiv_address_substitution was applied and that made
3856 the address invalid.
3857
3858 2) the address is an invalid symbolic address created by
3859 force_const_to_mem.
3860
3861 3) the address is a frame address with an invalid offset.
3862
3863 4) the address is a frame address with an invalid base.
3864
3865 All these cases involve a non-autoinc address, so there is no
3866 point revalidating other types. */
3867 if (ad.autoinc_p || valid_address_p (op, &ad, cn))
3868 return change_p;
3869
3870 /* Any index existed before LRA started, so we can assume that the
3871 presence and shape of the index is valid. */
3872 push_to_sequence (*before);
3873 lra_assert (ad.disp == ad.disp_term);
3874 if (ad.base == NULL)
3875 {
3876 if (ad.index == NULL)
3877 {
3878 rtx_insn *insn;
3879 rtx_insn *last = get_last_insn ();
3880 int code = -1;
3881 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3882 SCRATCH, SCRATCH,
3883 curr_insn);
3884 rtx addr = *ad.inner;
3885
3886 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, NULL, "addr");
3887 if (HAVE_lo_sum)
3888 {
3889 /* addr => lo_sum (new_base, addr), case (2) above. */
3890 insn = emit_insn (gen_rtx_SET
3891 (new_reg,
3892 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
3893 code = recog_memoized (insn);
3894 if (code >= 0)
3895 {
3896 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
3897 if (!valid_address_p (op, &ad, cn))
3898 {
3899 /* Try to put lo_sum into register. */
3900 insn = emit_insn (gen_rtx_SET
3901 (new_reg,
3902 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
3903 code = recog_memoized (insn);
3904 if (code >= 0)
3905 {
3906 *ad.inner = new_reg;
3907 if (!valid_address_p (op, &ad, cn))
3908 {
3909 *ad.inner = addr;
3910 code = -1;
3911 }
3912 }
3913
3914 }
3915 }
3916 if (code < 0)
3917 delete_insns_since (last);
3918 }
3919
3920 if (code < 0)
3921 {
3922 /* addr => new_base, case (2) above. */
3923 lra_emit_move (new_reg, addr);
3924
3925 for (insn = last == NULL_RTX ? get_insns () : NEXT_INSN (last);
3926 insn != NULL_RTX;
3927 insn = NEXT_INSN (insn))
3928 if (recog_memoized (insn) < 0)
3929 break;
3930 if (insn != NULL_RTX)
3931 {
3932 /* Do nothing if we cannot generate right insns.
3933 This is analogous to reload pass behavior. */
3934 delete_insns_since (last);
3935 end_sequence ();
3936 return false;
3937 }
3938 *ad.inner = new_reg;
3939 }
3940 }
3941 else
3942 {
3943 /* index * scale + disp => new base + index * scale,
3944 case (1) above. */
3945 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3946 GET_CODE (*ad.index),
3947 curr_insn);
3948
3949 lra_assert (index_cl != NO_REGS);
3950 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, NULL, "disp");
3951 lra_emit_move (new_reg, *ad.disp);
3952 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3953 new_reg, *ad.index);
3954 }
3955 }
3956 else if (ad.index == NULL)
3957 {
3958 int regno;
3959 enum reg_class cl;
3960 rtx set;
3961 rtx_insn *insns, *last_insn;
3962
3963 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3964 get_index_code (&ad), curr_insn);
3965
3966 if (REG_P (*ad.base_term)
3967 && ira_class_subset_p[get_reg_class (REGNO (*ad.base_term))][cl])
3968 /* It seems base reg is already in the base reg class and changing it
3969 does not make a progress. So reload the whole inner address. */
3970 goto reload_inner_addr;
3971
3972 /* Try to reload base into register only if the base is invalid
3973 for the address but with valid offset, case (4) above. */
3974 start_sequence ();
3975 new_reg = base_to_reg (&ad);
3976
3977 /* base + disp => new base, cases (1) and (3) above. */
3978 /* Another option would be to reload the displacement into an
3979 index register. However, postreload has code to optimize
3980 address reloads that have the same base and different
3981 displacements, so reloading into an index register would
3982 not necessarily be a win. */
3983 if (new_reg == NULL_RTX)
3984 {
3985 /* See if the target can split the displacement into a
3986 legitimate new displacement from a local anchor. */
3987 gcc_assert (ad.disp == ad.disp_term);
3988 poly_int64 orig_offset;
3989 rtx offset1, offset2;
3990 if (poly_int_rtx_p (*ad.disp, &orig_offset)
3991 && targetm.legitimize_address_displacement (&offset1, &offset2,
3992 orig_offset,
3993 ad.mode))
3994 {
3995 new_reg = base_plus_disp_to_reg (&ad, offset1);
3996 new_reg = gen_rtx_PLUS (GET_MODE (new_reg), new_reg, offset2);
3997 }
3998 else
3999 new_reg = base_plus_disp_to_reg (&ad, *ad.disp);
4000 }
4001 insns = get_insns ();
4002 last_insn = get_last_insn ();
4003 /* If we generated at least two insns, try last insn source as
4004 an address. If we succeed, we generate one less insn. */
4005 if (REG_P (new_reg)
4006 && last_insn != insns
4007 && (set = single_set (last_insn)) != NULL_RTX
4008 && GET_CODE (SET_SRC (set)) == PLUS
4009 && REG_P (XEXP (SET_SRC (set), 0))
4010 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
4011 {
4012 *ad.inner = SET_SRC (set);
4013 if (valid_address_p (op, &ad, cn))
4014 {
4015 *ad.base_term = XEXP (SET_SRC (set), 0);
4016 *ad.disp_term = XEXP (SET_SRC (set), 1);
4017 regno = REGNO (*ad.base_term);
4018 if (regno >= FIRST_PSEUDO_REGISTER
4019 && cl != lra_get_allocno_class (regno))
4020 lra_change_class (regno, cl, " Change to", true);
4021 new_reg = SET_SRC (set);
4022 delete_insns_since (PREV_INSN (last_insn));
4023 }
4024 }
4025 end_sequence ();
4026 emit_insn (insns);
4027 *ad.inner = new_reg;
4028 }
4029 else if (ad.disp_term != NULL)
4030 {
4031 /* base + scale * index + disp => new base + scale * index,
4032 case (1) above. */
4033 gcc_assert (ad.disp == ad.disp_term);
4034 new_reg = base_plus_disp_to_reg (&ad, *ad.disp);
4035 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
4036 new_reg, *ad.index);
4037 }
4038 else if ((scale = get_index_scale (&ad)) == 1)
4039 {
4040 /* The last transformation to one reg will be made in
4041 curr_insn_transform function. */
4042 end_sequence ();
4043 return false;
4044 }
4045 else if (scale != 0)
4046 {
4047 /* base + scale * index => base + new_reg,
4048 case (1) above.
4049 Index part of address may become invalid. For example, we
4050 changed pseudo on the equivalent memory and a subreg of the
4051 pseudo onto the memory of different mode for which the scale is
4052 prohibitted. */
4053 new_reg = index_part_to_reg (&ad, index_cl);
4054 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
4055 *ad.base_term, new_reg);
4056 }
4057 else
4058 {
4059 enum reg_class cl;
4060 rtx addr;
4061 reload_inner_addr:
4062 cl = base_reg_class (ad.mode, ad.as, SCRATCH, SCRATCH, curr_insn);
4063 addr = *ad.inner;
4064 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, NULL, "addr");
4065 /* addr => new_base. */
4066 lra_emit_move (new_reg, addr);
4067 *ad.inner = new_reg;
4068 }
4069 *before = get_insns ();
4070 end_sequence ();
4071 return true;
4072 }
4073
4074 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
4075 Use process_address_1 as a helper function. Return true for any
4076 RTL changes.
4077
4078 If CHECK_ONLY_P is true, just check address correctness. Return
4079 false if the address correct. */
4080 static bool
4081 process_address (int nop, bool check_only_p,
4082 rtx_insn **before, rtx_insn **after)
4083 {
4084 bool res = false;
4085 /* Use enough iterations to process all address parts: */
4086 for (int i = 0; i < 10; i++)
4087 {
4088 if (!process_address_1 (nop, check_only_p, before, after))
4089 {
4090 return res;
4091 }
4092 else
4093 {
4094 if (check_only_p)
4095 return true;
4096 res = true;
4097 }
4098 }
4099 fatal_insn ("unable to reload address in ", curr_insn);
4100 }
4101
4102 /* Override the generic address_reload_context in order to
4103 control the creation of reload pseudos. */
4104 class lra_autoinc_reload_context : public address_reload_context
4105 {
4106 machine_mode mode;
4107 enum reg_class rclass;
4108
4109 public:
4110 lra_autoinc_reload_context (machine_mode mode, enum reg_class new_rclass)
4111 : mode (mode), rclass (new_rclass) {}
4112
4113 rtx get_reload_reg () const override final
4114 {
4115 return lra_create_new_reg (mode, NULL_RTX, rclass, NULL, "INC/DEC result");
4116 }
4117 };
4118
4119 /* Emit insns to reload VALUE into a new register. VALUE is an
4120 auto-increment or auto-decrement RTX whose operand is a register or
4121 memory location; so reloading involves incrementing that location.
4122
4123 INC_AMOUNT is the number to increment or decrement by (always
4124 positive and ignored for POST_MODIFY/PRE_MODIFY).
4125
4126 Return a pseudo containing the result. */
4127 static rtx
4128 emit_inc (enum reg_class new_rclass, rtx value, poly_int64 inc_amount)
4129 {
4130 lra_autoinc_reload_context context (GET_MODE (value), new_rclass);
4131 return context.emit_autoinc (value, inc_amount);
4132 }
4133
4134 /* Return true if the current move insn does not need processing as we
4135 already know that it satisfies its constraints. */
4136 static bool
4137 simple_move_p (void)
4138 {
4139 rtx dest, src;
4140 enum reg_class dclass, sclass;
4141
4142 lra_assert (curr_insn_set != NULL_RTX);
4143 dest = SET_DEST (curr_insn_set);
4144 src = SET_SRC (curr_insn_set);
4145
4146 /* If the instruction has multiple sets we need to process it even if it
4147 is single_set. This can happen if one or more of the SETs are dead.
4148 See PR73650. */
4149 if (multiple_sets (curr_insn))
4150 return false;
4151
4152 return ((dclass = get_op_class (dest)) != NO_REGS
4153 && (sclass = get_op_class (src)) != NO_REGS
4154 /* The backend guarantees that register moves of cost 2
4155 never need reloads. */
4156 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
4157 }
4158
4159 /* Swap operands NOP and NOP + 1. */
4160 static inline void
4161 swap_operands (int nop)
4162 {
4163 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
4164 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
4165 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
4166 std::swap (equiv_substition_p[nop], equiv_substition_p[nop + 1]);
4167 /* Swap the duplicates too. */
4168 lra_update_dup (curr_id, nop);
4169 lra_update_dup (curr_id, nop + 1);
4170 }
4171
4172 /* Return TRUE if X is a (subreg of) reg and there are no hard regs of X class
4173 which can contain value of MODE. */
4174 static bool invalid_mode_reg_p (enum machine_mode mode, rtx x)
4175 {
4176 if (SUBREG_P (x))
4177 x = SUBREG_REG (x);
4178 if (! REG_P (x))
4179 return false;
4180 enum reg_class rclass = get_reg_class (REGNO (x));
4181 return (!hard_reg_set_empty_p (reg_class_contents[rclass])
4182 && hard_reg_set_subset_p
4183 (reg_class_contents[rclass],
4184 ira_prohibited_class_mode_regs[rclass][mode]));
4185 }
4186
4187 /* Return TRUE if regno is referenced in more than one non-debug insn. */
4188 static bool
4189 multiple_insn_refs_p (int regno)
4190 {
4191 unsigned int uid;
4192 bitmap_iterator bi;
4193 int nrefs = 0;
4194 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4195 {
4196 if (!NONDEBUG_INSN_P (lra_insn_recog_data[uid]->insn))
4197 continue;
4198 if (nrefs == 1)
4199 return true;
4200 nrefs++;
4201 }
4202 return false;
4203 }
4204
4205 /* Main entry point of the constraint code: search the body of the
4206 current insn to choose the best alternative. It is mimicking insn
4207 alternative cost calculation model of former reload pass. That is
4208 because machine descriptions were written to use this model. This
4209 model can be changed in future. Make commutative operand exchange
4210 if it is chosen.
4211
4212 if CHECK_ONLY_P is false, do RTL changes to satisfy the
4213 constraints. Return true if any change happened during function
4214 call.
4215
4216 If CHECK_ONLY_P is true then don't do any transformation. Just
4217 check that the insn satisfies all constraints. If the insn does
4218 not satisfy any constraint, return true. */
4219 static bool
4220 curr_insn_transform (bool check_only_p)
4221 {
4222 int i, j, k;
4223 int n_operands;
4224 int n_alternatives;
4225 int n_outputs;
4226 int commutative;
4227 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
4228 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
4229 signed char outputs[MAX_RECOG_OPERANDS + 1];
4230 rtx_insn *before, *after;
4231 bool alt_p = false;
4232 /* Flag that the insn has been changed through a transformation. */
4233 bool change_p;
4234 bool sec_mem_p;
4235 bool use_sec_mem_p;
4236 int max_regno_before;
4237 int reused_alternative_num;
4238
4239 curr_insn_set = single_set (curr_insn);
4240 if (curr_insn_set != NULL_RTX && simple_move_p ())
4241 {
4242 /* We assume that the corresponding insn alternative has no
4243 earlier clobbers. If it is not the case, don't define move
4244 cost equal to 2 for the corresponding register classes. */
4245 lra_set_used_insn_alternative (curr_insn, LRA_NON_CLOBBERED_ALT);
4246 return false;
4247 }
4248
4249 no_input_reloads_p = no_output_reloads_p = false;
4250 goal_alt_number = -1;
4251 change_p = sec_mem_p = false;
4252
4253 /* CALL_INSNs are not allowed to have any output reloads. */
4254 if (CALL_P (curr_insn))
4255 no_output_reloads_p = true;
4256
4257 n_operands = curr_static_id->n_operands;
4258 n_alternatives = curr_static_id->n_alternatives;
4259
4260 /* Just return "no reloads" if insn has no operands with
4261 constraints. */
4262 if (n_operands == 0 || n_alternatives == 0)
4263 return false;
4264
4265 max_regno_before = max_reg_num ();
4266
4267 for (i = 0; i < n_operands; i++)
4268 {
4269 goal_alt_matched[i][0] = -1;
4270 goal_alt_matches[i] = -1;
4271 }
4272
4273 commutative = curr_static_id->commutative;
4274
4275 /* Now see what we need for pseudos that didn't get hard regs or got
4276 the wrong kind of hard reg. For this, we must consider all the
4277 operands together against the register constraints. */
4278
4279 best_losers = best_overall = INT_MAX;
4280 best_reload_sum = 0;
4281
4282 curr_swapped = false;
4283 goal_alt_swapped = false;
4284
4285 if (! check_only_p)
4286 /* Make equivalence substitution and memory subreg elimination
4287 before address processing because an address legitimacy can
4288 depend on memory mode. */
4289 for (i = 0; i < n_operands; i++)
4290 {
4291 rtx op, subst, old;
4292 bool op_change_p = false;
4293
4294 if (curr_static_id->operand[i].is_operator)
4295 continue;
4296
4297 old = op = *curr_id->operand_loc[i];
4298 if (GET_CODE (old) == SUBREG)
4299 old = SUBREG_REG (old);
4300 subst = get_equiv_with_elimination (old, curr_insn);
4301 original_subreg_reg_mode[i] = VOIDmode;
4302 equiv_substition_p[i] = false;
4303 if (subst != old)
4304 {
4305 equiv_substition_p[i] = true;
4306 subst = copy_rtx (subst);
4307 lra_assert (REG_P (old));
4308 if (GET_CODE (op) != SUBREG)
4309 *curr_id->operand_loc[i] = subst;
4310 else
4311 {
4312 SUBREG_REG (op) = subst;
4313 if (GET_MODE (subst) == VOIDmode)
4314 original_subreg_reg_mode[i] = GET_MODE (old);
4315 }
4316 if (lra_dump_file != NULL)
4317 {
4318 fprintf (lra_dump_file,
4319 "Changing pseudo %d in operand %i of insn %u on equiv ",
4320 REGNO (old), i, INSN_UID (curr_insn));
4321 dump_value_slim (lra_dump_file, subst, 1);
4322 fprintf (lra_dump_file, "\n");
4323 }
4324 op_change_p = change_p = true;
4325 }
4326 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
4327 {
4328 change_p = true;
4329 lra_update_dup (curr_id, i);
4330 }
4331 }
4332
4333 /* Reload address registers and displacements. We do it before
4334 finding an alternative because of memory constraints. */
4335 before = after = NULL;
4336 for (i = 0; i < n_operands; i++)
4337 if (! curr_static_id->operand[i].is_operator
4338 && process_address (i, check_only_p, &before, &after))
4339 {
4340 if (check_only_p)
4341 return true;
4342 change_p = true;
4343 lra_update_dup (curr_id, i);
4344 }
4345
4346 if (change_p)
4347 /* If we've changed the instruction then any alternative that
4348 we chose previously may no longer be valid. */
4349 lra_set_used_insn_alternative (curr_insn, LRA_UNKNOWN_ALT);
4350
4351 if (! check_only_p && curr_insn_set != NULL_RTX
4352 && check_and_process_move (&change_p, &sec_mem_p))
4353 return change_p;
4354
4355 try_swapped:
4356
4357 reused_alternative_num = check_only_p ? LRA_UNKNOWN_ALT : curr_id->used_insn_alternative;
4358 if (lra_dump_file != NULL && reused_alternative_num >= 0)
4359 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
4360 reused_alternative_num, INSN_UID (curr_insn));
4361
4362 if (process_alt_operands (reused_alternative_num))
4363 alt_p = true;
4364
4365 if (check_only_p)
4366 return ! alt_p || best_losers != 0;
4367
4368 /* If insn is commutative (it's safe to exchange a certain pair of
4369 operands) then we need to try each alternative twice, the second
4370 time matching those two operands as if we had exchanged them. To
4371 do this, really exchange them in operands.
4372
4373 If we have just tried the alternatives the second time, return
4374 operands to normal and drop through. */
4375
4376 if (reused_alternative_num < 0 && commutative >= 0)
4377 {
4378 curr_swapped = !curr_swapped;
4379 if (curr_swapped)
4380 {
4381 swap_operands (commutative);
4382 goto try_swapped;
4383 }
4384 else
4385 swap_operands (commutative);
4386 }
4387
4388 if (! alt_p && ! sec_mem_p)
4389 {
4390 /* No alternative works with reloads?? */
4391 if (INSN_CODE (curr_insn) >= 0)
4392 fatal_insn ("unable to generate reloads for:", curr_insn);
4393 error_for_asm (curr_insn,
4394 "inconsistent operand constraints in an %<asm%>");
4395 lra_asm_error_p = true;
4396 if (! JUMP_P (curr_insn))
4397 {
4398 /* Avoid further trouble with this insn. Don't generate use
4399 pattern here as we could use the insn SP offset. */
4400 lra_set_insn_deleted (curr_insn);
4401 }
4402 else
4403 {
4404 lra_invalidate_insn_data (curr_insn);
4405 ira_nullify_asm_goto (curr_insn);
4406 lra_update_insn_regno_info (curr_insn);
4407 }
4408 return true;
4409 }
4410
4411 /* If the best alternative is with operands 1 and 2 swapped, swap
4412 them. Update the operand numbers of any reloads already
4413 pushed. */
4414
4415 if (goal_alt_swapped)
4416 {
4417 if (lra_dump_file != NULL)
4418 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
4419 INSN_UID (curr_insn));
4420
4421 /* Swap the duplicates too. */
4422 swap_operands (commutative);
4423 change_p = true;
4424 }
4425
4426 /* Some targets' TARGET_SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
4427 too conservatively. So we use the secondary memory only if there
4428 is no any alternative without reloads. */
4429 use_sec_mem_p = false;
4430 if (! alt_p)
4431 use_sec_mem_p = true;
4432 else if (sec_mem_p)
4433 {
4434 for (i = 0; i < n_operands; i++)
4435 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
4436 break;
4437 use_sec_mem_p = i < n_operands;
4438 }
4439
4440 if (use_sec_mem_p)
4441 {
4442 int in = -1, out = -1;
4443 rtx new_reg, src, dest, rld;
4444 machine_mode sec_mode, rld_mode;
4445
4446 lra_assert (curr_insn_set != NULL_RTX && sec_mem_p);
4447 dest = SET_DEST (curr_insn_set);
4448 src = SET_SRC (curr_insn_set);
4449 for (i = 0; i < n_operands; i++)
4450 if (*curr_id->operand_loc[i] == dest)
4451 out = i;
4452 else if (*curr_id->operand_loc[i] == src)
4453 in = i;
4454 for (i = 0; i < curr_static_id->n_dups; i++)
4455 if (out < 0 && *curr_id->dup_loc[i] == dest)
4456 out = curr_static_id->dup_num[i];
4457 else if (in < 0 && *curr_id->dup_loc[i] == src)
4458 in = curr_static_id->dup_num[i];
4459 lra_assert (out >= 0 && in >= 0
4460 && curr_static_id->operand[out].type == OP_OUT
4461 && curr_static_id->operand[in].type == OP_IN);
4462 rld = partial_subreg_p (GET_MODE (src), GET_MODE (dest)) ? src : dest;
4463 rld_mode = GET_MODE (rld);
4464 sec_mode = targetm.secondary_memory_needed_mode (rld_mode);
4465 if (rld_mode != sec_mode
4466 && (invalid_mode_reg_p (sec_mode, dest)
4467 || invalid_mode_reg_p (sec_mode, src)))
4468 sec_mode = rld_mode;
4469 new_reg = lra_create_new_reg (sec_mode, NULL_RTX, NO_REGS, NULL,
4470 "secondary");
4471 /* If the mode is changed, it should be wider. */
4472 lra_assert (!partial_subreg_p (sec_mode, rld_mode));
4473 if (sec_mode != rld_mode)
4474 {
4475 /* If the target says specifically to use another mode for
4476 secondary memory moves we cannot reuse the original
4477 insn. */
4478 after = emit_spill_move (false, new_reg, dest);
4479 lra_process_new_insns (curr_insn, NULL, after,
4480 "Inserting the sec. move");
4481 /* We may have non null BEFORE here (e.g. after address
4482 processing. */
4483 push_to_sequence (before);
4484 before = emit_spill_move (true, new_reg, src);
4485 emit_insn (before);
4486 before = get_insns ();
4487 end_sequence ();
4488 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
4489 lra_set_insn_deleted (curr_insn);
4490 }
4491 else if (dest == rld)
4492 {
4493 *curr_id->operand_loc[out] = new_reg;
4494 lra_update_dup (curr_id, out);
4495 after = emit_spill_move (false, new_reg, dest);
4496 lra_process_new_insns (curr_insn, NULL, after,
4497 "Inserting the sec. move");
4498 }
4499 else
4500 {
4501 *curr_id->operand_loc[in] = new_reg;
4502 lra_update_dup (curr_id, in);
4503 /* See comments above. */
4504 push_to_sequence (before);
4505 before = emit_spill_move (true, new_reg, src);
4506 emit_insn (before);
4507 before = get_insns ();
4508 end_sequence ();
4509 lra_process_new_insns (curr_insn, before, NULL,
4510 "Inserting the sec. move");
4511 }
4512 lra_update_insn_regno_info (curr_insn);
4513 return true;
4514 }
4515
4516 lra_assert (goal_alt_number >= 0);
4517 lra_set_used_insn_alternative (curr_insn, goal_reuse_alt_p
4518 ? goal_alt_number : LRA_UNKNOWN_ALT);
4519
4520 if (lra_dump_file != NULL)
4521 {
4522 const char *p;
4523
4524 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
4525 goal_alt_number, INSN_UID (curr_insn));
4526 print_curr_insn_alt (goal_alt_number);
4527 if (INSN_CODE (curr_insn) >= 0
4528 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
4529 fprintf (lra_dump_file, " {%s}", p);
4530 if (maybe_ne (curr_id->sp_offset, 0))
4531 {
4532 fprintf (lra_dump_file, " (sp_off=");
4533 print_dec (curr_id->sp_offset, lra_dump_file);
4534 fprintf (lra_dump_file, ")");
4535 }
4536 fprintf (lra_dump_file, "\n");
4537 }
4538
4539 /* Right now, for any pair of operands I and J that are required to
4540 match, with J < I, goal_alt_matches[I] is J. Add I to
4541 goal_alt_matched[J]. */
4542
4543 for (i = 0; i < n_operands; i++)
4544 if ((j = goal_alt_matches[i]) >= 0)
4545 {
4546 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
4547 ;
4548 /* We allow matching one output operand and several input
4549 operands. */
4550 lra_assert (k == 0
4551 || (curr_static_id->operand[j].type == OP_OUT
4552 && curr_static_id->operand[i].type == OP_IN
4553 && (curr_static_id->operand
4554 [goal_alt_matched[j][0]].type == OP_IN)));
4555 goal_alt_matched[j][k] = i;
4556 goal_alt_matched[j][k + 1] = -1;
4557 }
4558
4559 for (i = 0; i < n_operands; i++)
4560 goal_alt_win[i] |= goal_alt_match_win[i];
4561
4562 /* Any constants that aren't allowed and can't be reloaded into
4563 registers are here changed into memory references. */
4564 for (i = 0; i < n_operands; i++)
4565 if (goal_alt_win[i])
4566 {
4567 int regno;
4568 enum reg_class new_class;
4569 rtx reg = *curr_id->operand_loc[i];
4570
4571 if (GET_CODE (reg) == SUBREG)
4572 reg = SUBREG_REG (reg);
4573
4574 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
4575 {
4576 bool ok_p = in_class_p (reg, goal_alt[i], &new_class, true);
4577
4578 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
4579 {
4580 lra_assert (ok_p);
4581 lra_change_class (regno, new_class, " Change to", true);
4582 }
4583 }
4584 }
4585 else
4586 {
4587 const char *constraint;
4588 char c;
4589 rtx op = *curr_id->operand_loc[i];
4590 rtx subreg = NULL_RTX;
4591 machine_mode mode = curr_operand_mode[i];
4592
4593 if (GET_CODE (op) == SUBREG)
4594 {
4595 subreg = op;
4596 op = SUBREG_REG (op);
4597 mode = GET_MODE (op);
4598 }
4599
4600 if (CONST_POOL_OK_P (mode, op)
4601 && ((targetm.preferred_reload_class
4602 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
4603 || no_input_reloads_p))
4604 {
4605 rtx tem = force_const_mem (mode, op);
4606
4607 change_p = true;
4608 if (subreg != NULL_RTX)
4609 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
4610
4611 *curr_id->operand_loc[i] = tem;
4612 lra_update_dup (curr_id, i);
4613 process_address (i, false, &before, &after);
4614
4615 /* If the alternative accepts constant pool refs directly
4616 there will be no reload needed at all. */
4617 if (subreg != NULL_RTX)
4618 continue;
4619 /* Skip alternatives before the one requested. */
4620 constraint = (curr_static_id->operand_alternative
4621 [goal_alt_number * n_operands + i].constraint);
4622 for (;
4623 (c = *constraint) && c != ',' && c != '#';
4624 constraint += CONSTRAINT_LEN (c, constraint))
4625 {
4626 enum constraint_num cn = lookup_constraint (constraint);
4627 if ((insn_extra_memory_constraint (cn)
4628 || insn_extra_special_memory_constraint (cn)
4629 || insn_extra_relaxed_memory_constraint (cn))
4630 && satisfies_memory_constraint_p (tem, cn))
4631 break;
4632 }
4633 if (c == '\0' || c == ',' || c == '#')
4634 continue;
4635
4636 goal_alt_win[i] = true;
4637 }
4638 }
4639
4640 n_outputs = 0;
4641 for (i = 0; i < n_operands; i++)
4642 if (curr_static_id->operand[i].type == OP_OUT)
4643 outputs[n_outputs++] = i;
4644 outputs[n_outputs] = -1;
4645 for (i = 0; i < n_operands; i++)
4646 {
4647 int regno;
4648 bool optional_p = false;
4649 rtx old, new_reg;
4650 rtx op = *curr_id->operand_loc[i];
4651
4652 if (goal_alt_win[i])
4653 {
4654 if (goal_alt[i] == NO_REGS
4655 && REG_P (op)
4656 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
4657 /* We assigned a hard register to the pseudo in the past but now
4658 decided to spill it for the insn. If the pseudo is used only
4659 in this insn, it is better to spill it here as we free hard
4660 registers for other pseudos referenced in the insn. The most
4661 common case of this is a scratch register which will be
4662 transformed to scratch back at the end of LRA. */
4663 && !multiple_insn_refs_p (regno))
4664 {
4665 if (lra_get_allocno_class (regno) != NO_REGS)
4666 lra_change_class (regno, NO_REGS, " Change to", true);
4667 reg_renumber[regno] = -1;
4668 }
4669 /* We can do an optional reload. If the pseudo got a hard
4670 reg, we might improve the code through inheritance. If
4671 it does not get a hard register we coalesce memory/memory
4672 moves later. Ignore move insns to avoid cycling. */
4673 if (! lra_simple_p
4674 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
4675 && goal_alt[i] != NO_REGS && REG_P (op)
4676 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
4677 && regno < new_regno_start
4678 && ! ira_former_scratch_p (regno)
4679 && reg_renumber[regno] < 0
4680 /* Check that the optional reload pseudo will be able to
4681 hold given mode value. */
4682 && ! (prohibited_class_reg_set_mode_p
4683 (goal_alt[i], reg_class_contents[goal_alt[i]],
4684 PSEUDO_REGNO_MODE (regno)))
4685 && (curr_insn_set == NULL_RTX
4686 || !((REG_P (SET_SRC (curr_insn_set))
4687 || MEM_P (SET_SRC (curr_insn_set))
4688 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
4689 && (REG_P (SET_DEST (curr_insn_set))
4690 || MEM_P (SET_DEST (curr_insn_set))
4691 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
4692 optional_p = true;
4693 else if (goal_alt_matched[i][0] != -1
4694 && curr_static_id->operand[i].type == OP_OUT
4695 && (curr_static_id->operand_alternative
4696 [goal_alt_number * n_operands + i].earlyclobber)
4697 && REG_P (op))
4698 {
4699 for (j = 0; goal_alt_matched[i][j] != -1; j++)
4700 {
4701 rtx op2 = *curr_id->operand_loc[goal_alt_matched[i][j]];
4702
4703 if (REG_P (op2) && REGNO (op) != REGNO (op2))
4704 break;
4705 }
4706 if (goal_alt_matched[i][j] != -1)
4707 {
4708 /* Generate reloads for different output and matched
4709 input registers. This is the easiest way to avoid
4710 creation of non-existing register conflicts in
4711 lra-lives.cc. */
4712 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i],
4713 &goal_alt_exclude_start_hard_regs[i], &before,
4714 &after, true);
4715 }
4716 continue;
4717 }
4718 else
4719 {
4720 enum reg_class rclass, common_class;
4721
4722 if (REG_P (op) && goal_alt[i] != NO_REGS
4723 && (regno = REGNO (op)) >= new_regno_start
4724 && (rclass = get_reg_class (regno)) == ALL_REGS
4725 && ((common_class = ira_reg_class_subset[rclass][goal_alt[i]])
4726 != NO_REGS)
4727 && common_class != ALL_REGS
4728 && enough_allocatable_hard_regs_p (common_class,
4729 GET_MODE (op)))
4730 /* Refine reload pseudo class from chosen alternative
4731 constraint. */
4732 lra_change_class (regno, common_class, " Change to", true);
4733 continue;
4734 }
4735 }
4736
4737 /* Operands that match previous ones have already been handled. */
4738 if (goal_alt_matches[i] >= 0)
4739 continue;
4740
4741 /* We should not have an operand with a non-offsettable address
4742 appearing where an offsettable address will do. It also may
4743 be a case when the address should be special in other words
4744 not a general one (e.g. it needs no index reg). */
4745 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
4746 {
4747 enum reg_class rclass;
4748 rtx *loc = &XEXP (op, 0);
4749 enum rtx_code code = GET_CODE (*loc);
4750
4751 push_to_sequence (before);
4752 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
4753 MEM, SCRATCH, curr_insn);
4754 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
4755 new_reg = emit_inc (rclass, *loc,
4756 /* This value does not matter for MODIFY. */
4757 GET_MODE_SIZE (GET_MODE (op)));
4758 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass,
4759 NULL, false, false,
4760 "offsetable address", &new_reg))
4761 {
4762 rtx addr = *loc;
4763 enum rtx_code code = GET_CODE (addr);
4764 bool align_p = false;
4765
4766 if (code == AND && CONST_INT_P (XEXP (addr, 1)))
4767 {
4768 /* (and ... (const_int -X)) is used to align to X bytes. */
4769 align_p = true;
4770 addr = XEXP (*loc, 0);
4771 }
4772 else
4773 addr = canonicalize_reload_addr (addr);
4774
4775 lra_emit_move (new_reg, addr);
4776 if (align_p)
4777 emit_move_insn (new_reg, gen_rtx_AND (GET_MODE (new_reg), new_reg, XEXP (*loc, 1)));
4778 }
4779 before = get_insns ();
4780 end_sequence ();
4781 *loc = new_reg;
4782 lra_update_dup (curr_id, i);
4783 }
4784 else if (goal_alt_matched[i][0] == -1)
4785 {
4786 machine_mode mode;
4787 rtx reg, *loc;
4788 int hard_regno;
4789 enum op_type type = curr_static_id->operand[i].type;
4790
4791 loc = curr_id->operand_loc[i];
4792 mode = curr_operand_mode[i];
4793 if (GET_CODE (*loc) == SUBREG)
4794 {
4795 reg = SUBREG_REG (*loc);
4796 poly_int64 byte = SUBREG_BYTE (*loc);
4797 if (REG_P (reg)
4798 /* Strict_low_part requires reloading the register and not
4799 just the subreg. Likewise for a strict subreg no wider
4800 than a word for WORD_REGISTER_OPERATIONS targets. */
4801 && (curr_static_id->operand[i].strict_low
4802 || (!paradoxical_subreg_p (mode, GET_MODE (reg))
4803 && (hard_regno
4804 = get_try_hard_regno (REGNO (reg))) >= 0
4805 && (simplify_subreg_regno
4806 (hard_regno,
4807 GET_MODE (reg), byte, mode) < 0)
4808 && (goal_alt[i] == NO_REGS
4809 || (simplify_subreg_regno
4810 (ira_class_hard_regs[goal_alt[i]][0],
4811 GET_MODE (reg), byte, mode) >= 0)))
4812 || (partial_subreg_p (mode, GET_MODE (reg))
4813 && known_le (GET_MODE_SIZE (GET_MODE (reg)),
4814 UNITS_PER_WORD)
4815 && WORD_REGISTER_OPERATIONS))
4816 /* Avoid the situation when there are no available hard regs
4817 for the pseudo mode but there are ones for the subreg
4818 mode: */
4819 && !(goal_alt[i] != NO_REGS
4820 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
4821 && (prohibited_class_reg_set_mode_p
4822 (goal_alt[i], reg_class_contents[goal_alt[i]],
4823 GET_MODE (reg)))
4824 && !(prohibited_class_reg_set_mode_p
4825 (goal_alt[i], reg_class_contents[goal_alt[i]],
4826 mode))))
4827 {
4828 /* An OP_INOUT is required when reloading a subreg of a
4829 mode wider than a word to ensure that data beyond the
4830 word being reloaded is preserved. Also automatically
4831 ensure that strict_low_part reloads are made into
4832 OP_INOUT which should already be true from the backend
4833 constraints. */
4834 if (type == OP_OUT
4835 && (curr_static_id->operand[i].strict_low
4836 || read_modify_subreg_p (*loc)))
4837 type = OP_INOUT;
4838 loc = &SUBREG_REG (*loc);
4839 mode = GET_MODE (*loc);
4840 }
4841 }
4842 old = *loc;
4843 if (get_reload_reg (type, mode, old, goal_alt[i],
4844 &goal_alt_exclude_start_hard_regs[i],
4845 loc != curr_id->operand_loc[i],
4846 curr_static_id->operand_alternative
4847 [goal_alt_number * n_operands + i].earlyclobber,
4848 "", &new_reg)
4849 && type != OP_OUT)
4850 {
4851 push_to_sequence (before);
4852 lra_emit_move (new_reg, old);
4853 before = get_insns ();
4854 end_sequence ();
4855 }
4856 *loc = new_reg;
4857 if (type != OP_IN
4858 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX
4859 /* OLD can be an equivalent constant here. */
4860 && !CONSTANT_P (old))
4861 {
4862 start_sequence ();
4863 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
4864 emit_insn (after);
4865 after = get_insns ();
4866 end_sequence ();
4867 *loc = new_reg;
4868 }
4869 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
4870 if (goal_alt_dont_inherit_ops[j] == i)
4871 {
4872 lra_set_regno_unique_value (REGNO (new_reg));
4873 break;
4874 }
4875 lra_update_dup (curr_id, i);
4876 }
4877 else if (curr_static_id->operand[i].type == OP_IN
4878 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4879 == OP_OUT
4880 || (curr_static_id->operand[goal_alt_matched[i][0]].type
4881 == OP_INOUT
4882 && (operands_match_p
4883 (*curr_id->operand_loc[i],
4884 *curr_id->operand_loc[goal_alt_matched[i][0]],
4885 -1)))))
4886 {
4887 /* generate reloads for input and matched outputs. */
4888 match_inputs[0] = i;
4889 match_inputs[1] = -1;
4890 match_reload (goal_alt_matched[i][0], match_inputs, outputs,
4891 goal_alt[i], &goal_alt_exclude_start_hard_regs[i],
4892 &before, &after,
4893 curr_static_id->operand_alternative
4894 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
4895 .earlyclobber);
4896 }
4897 else if ((curr_static_id->operand[i].type == OP_OUT
4898 || (curr_static_id->operand[i].type == OP_INOUT
4899 && (operands_match_p
4900 (*curr_id->operand_loc[i],
4901 *curr_id->operand_loc[goal_alt_matched[i][0]],
4902 -1))))
4903 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4904 == OP_IN))
4905 /* Generate reloads for output and matched inputs. */
4906 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i],
4907 &goal_alt_exclude_start_hard_regs[i], &before, &after,
4908 curr_static_id->operand_alternative
4909 [goal_alt_number * n_operands + i].earlyclobber);
4910 else if (curr_static_id->operand[i].type == OP_IN
4911 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4912 == OP_IN))
4913 {
4914 /* Generate reloads for matched inputs. */
4915 match_inputs[0] = i;
4916 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
4917 match_inputs[j + 1] = k;
4918 match_inputs[j + 1] = -1;
4919 match_reload (-1, match_inputs, outputs, goal_alt[i],
4920 &goal_alt_exclude_start_hard_regs[i],
4921 &before, &after, false);
4922 }
4923 else
4924 /* We must generate code in any case when function
4925 process_alt_operands decides that it is possible. */
4926 gcc_unreachable ();
4927
4928 if (optional_p)
4929 {
4930 rtx reg = op;
4931
4932 lra_assert (REG_P (reg));
4933 regno = REGNO (reg);
4934 op = *curr_id->operand_loc[i]; /* Substitution. */
4935 if (GET_CODE (op) == SUBREG)
4936 op = SUBREG_REG (op);
4937 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
4938 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
4939 lra_reg_info[REGNO (op)].restore_rtx = reg;
4940 if (lra_dump_file != NULL)
4941 fprintf (lra_dump_file,
4942 " Making reload reg %d for reg %d optional\n",
4943 REGNO (op), regno);
4944 }
4945 }
4946 if (before != NULL_RTX || after != NULL_RTX
4947 || max_regno_before != max_reg_num ())
4948 change_p = true;
4949 if (change_p)
4950 {
4951 lra_update_operator_dups (curr_id);
4952 /* Something changes -- process the insn. */
4953 lra_update_insn_regno_info (curr_insn);
4954 if (asm_noperands (PATTERN (curr_insn)) >= 0
4955 && ++curr_id->asm_reloads_num >= FIRST_PSEUDO_REGISTER)
4956 /* Most probably there are no enough registers to satisfy asm insn: */
4957 lra_asm_insn_error (curr_insn);
4958 }
4959 if (goal_alt_out_sp_reload_p)
4960 {
4961 /* We have an output stack pointer reload -- update sp offset: */
4962 rtx set;
4963 bool done_p = false;
4964 poly_int64 sp_offset = curr_id->sp_offset;
4965 for (rtx_insn *insn = after; insn != NULL_RTX; insn = NEXT_INSN (insn))
4966 if ((set = single_set (insn)) != NULL_RTX
4967 && SET_DEST (set) == stack_pointer_rtx)
4968 {
4969 lra_assert (!done_p);
4970 done_p = true;
4971 curr_id->sp_offset = 0;
4972 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
4973 id->sp_offset = sp_offset;
4974 if (lra_dump_file != NULL)
4975 fprintf (lra_dump_file,
4976 " Moving sp offset from insn %u to %u\n",
4977 INSN_UID (curr_insn), INSN_UID (insn));
4978 }
4979 lra_assert (done_p);
4980 }
4981 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
4982 return change_p;
4983 }
4984
4985 /* Return true if INSN satisfies all constraints. In other words, no
4986 reload insns are needed. */
4987 bool
4988 lra_constrain_insn (rtx_insn *insn)
4989 {
4990 int saved_new_regno_start = new_regno_start;
4991 int saved_new_insn_uid_start = new_insn_uid_start;
4992 bool change_p;
4993
4994 curr_insn = insn;
4995 curr_id = lra_get_insn_recog_data (curr_insn);
4996 curr_static_id = curr_id->insn_static_data;
4997 new_insn_uid_start = get_max_uid ();
4998 new_regno_start = max_reg_num ();
4999 change_p = curr_insn_transform (true);
5000 new_regno_start = saved_new_regno_start;
5001 new_insn_uid_start = saved_new_insn_uid_start;
5002 return ! change_p;
5003 }
5004
5005 /* Return true if X is in LIST. */
5006 static bool
5007 in_list_p (rtx x, rtx list)
5008 {
5009 for (; list != NULL_RTX; list = XEXP (list, 1))
5010 if (XEXP (list, 0) == x)
5011 return true;
5012 return false;
5013 }
5014
5015 /* Return true if X contains an allocatable hard register (if
5016 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
5017 static bool
5018 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
5019 {
5020 int i, j;
5021 const char *fmt;
5022 enum rtx_code code;
5023
5024 code = GET_CODE (x);
5025 if (REG_P (x))
5026 {
5027 int regno = REGNO (x);
5028 HARD_REG_SET alloc_regs;
5029
5030 if (hard_reg_p)
5031 {
5032 if (regno >= FIRST_PSEUDO_REGISTER)
5033 regno = lra_get_regno_hard_regno (regno);
5034 if (regno < 0)
5035 return false;
5036 alloc_regs = ~lra_no_alloc_regs;
5037 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
5038 }
5039 else
5040 {
5041 if (regno < FIRST_PSEUDO_REGISTER)
5042 return false;
5043 if (! spilled_p)
5044 return true;
5045 return lra_get_regno_hard_regno (regno) < 0;
5046 }
5047 }
5048 fmt = GET_RTX_FORMAT (code);
5049 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5050 {
5051 if (fmt[i] == 'e')
5052 {
5053 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
5054 return true;
5055 }
5056 else if (fmt[i] == 'E')
5057 {
5058 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5059 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
5060 return true;
5061 }
5062 }
5063 return false;
5064 }
5065
5066 /* Process all regs in location *LOC and change them on equivalent
5067 substitution. Return true if any change was done. */
5068 static bool
5069 loc_equivalence_change_p (rtx *loc)
5070 {
5071 rtx subst, reg, x = *loc;
5072 bool result = false;
5073 enum rtx_code code = GET_CODE (x);
5074 const char *fmt;
5075 int i, j;
5076
5077 if (code == SUBREG)
5078 {
5079 reg = SUBREG_REG (x);
5080 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
5081 && GET_MODE (subst) == VOIDmode)
5082 {
5083 /* We cannot reload debug location. Simplify subreg here
5084 while we know the inner mode. */
5085 *loc = simplify_gen_subreg (GET_MODE (x), subst,
5086 GET_MODE (reg), SUBREG_BYTE (x));
5087 return true;
5088 }
5089 }
5090 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
5091 {
5092 *loc = subst;
5093 return true;
5094 }
5095
5096 /* Scan all the operand sub-expressions. */
5097 fmt = GET_RTX_FORMAT (code);
5098 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5099 {
5100 if (fmt[i] == 'e')
5101 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
5102 else if (fmt[i] == 'E')
5103 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5104 result
5105 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
5106 }
5107 return result;
5108 }
5109
5110 /* Similar to loc_equivalence_change_p, but for use as
5111 simplify_replace_fn_rtx callback. DATA is insn for which the
5112 elimination is done. If it null we don't do the elimination. */
5113 static rtx
5114 loc_equivalence_callback (rtx loc, const_rtx, void *data)
5115 {
5116 if (!REG_P (loc))
5117 return NULL_RTX;
5118
5119 rtx subst = (data == NULL
5120 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
5121 if (subst != loc)
5122 return subst;
5123
5124 return NULL_RTX;
5125 }
5126
5127 /* Maximum number of generated reload insns per an insn. It is for
5128 preventing this pass cycling in a bug case. */
5129 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
5130
5131 /* The current iteration number of this LRA pass. */
5132 int lra_constraint_iter;
5133
5134 /* True if we should during assignment sub-pass check assignment
5135 correctness for all pseudos and spill some of them to correct
5136 conflicts. It can be necessary when we substitute equiv which
5137 needs checking register allocation correctness because the
5138 equivalent value contains allocatable hard registers, or when we
5139 restore multi-register pseudo, or when we change the insn code and
5140 its operand became INOUT operand when it was IN one before. */
5141 bool check_and_force_assignment_correctness_p;
5142
5143 /* Return true if REGNO is referenced in more than one block. */
5144 static bool
5145 multi_block_pseudo_p (int regno)
5146 {
5147 basic_block bb = NULL;
5148 unsigned int uid;
5149 bitmap_iterator bi;
5150
5151 if (regno < FIRST_PSEUDO_REGISTER)
5152 return false;
5153
5154 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
5155 if (bb == NULL)
5156 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
5157 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
5158 return true;
5159 return false;
5160 }
5161
5162 /* Return true if LIST contains a deleted insn. */
5163 static bool
5164 contains_deleted_insn_p (rtx_insn_list *list)
5165 {
5166 for (; list != NULL_RTX; list = list->next ())
5167 if (NOTE_P (list->insn ())
5168 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
5169 return true;
5170 return false;
5171 }
5172
5173 /* Return true if X contains a pseudo dying in INSN. */
5174 static bool
5175 dead_pseudo_p (rtx x, rtx_insn *insn)
5176 {
5177 int i, j;
5178 const char *fmt;
5179 enum rtx_code code;
5180
5181 if (REG_P (x))
5182 return (insn != NULL_RTX
5183 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
5184 code = GET_CODE (x);
5185 fmt = GET_RTX_FORMAT (code);
5186 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5187 {
5188 if (fmt[i] == 'e')
5189 {
5190 if (dead_pseudo_p (XEXP (x, i), insn))
5191 return true;
5192 }
5193 else if (fmt[i] == 'E')
5194 {
5195 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5196 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
5197 return true;
5198 }
5199 }
5200 return false;
5201 }
5202
5203 /* Return true if INSN contains a dying pseudo in INSN right hand
5204 side. */
5205 static bool
5206 insn_rhs_dead_pseudo_p (rtx_insn *insn)
5207 {
5208 rtx set = single_set (insn);
5209
5210 gcc_assert (set != NULL);
5211 return dead_pseudo_p (SET_SRC (set), insn);
5212 }
5213
5214 /* Return true if any init insn of REGNO contains a dying pseudo in
5215 insn right hand side. */
5216 static bool
5217 init_insn_rhs_dead_pseudo_p (int regno)
5218 {
5219 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
5220
5221 if (insns == NULL)
5222 return false;
5223 for (; insns != NULL_RTX; insns = insns->next ())
5224 if (insn_rhs_dead_pseudo_p (insns->insn ()))
5225 return true;
5226 return false;
5227 }
5228
5229 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
5230 reverse only if we have one init insn with given REGNO as a
5231 source. */
5232 static bool
5233 reverse_equiv_p (int regno)
5234 {
5235 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
5236 rtx set;
5237
5238 if (insns == NULL)
5239 return false;
5240 if (! INSN_P (insns->insn ())
5241 || insns->next () != NULL)
5242 return false;
5243 if ((set = single_set (insns->insn ())) == NULL_RTX)
5244 return false;
5245 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
5246 }
5247
5248 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
5249 call this function only for non-reverse equivalence. */
5250 static bool
5251 contains_reloaded_insn_p (int regno)
5252 {
5253 rtx set;
5254 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
5255
5256 for (; list != NULL; list = list->next ())
5257 if ((set = single_set (list->insn ())) == NULL_RTX
5258 || ! REG_P (SET_DEST (set))
5259 || (int) REGNO (SET_DEST (set)) != regno)
5260 return true;
5261 return false;
5262 }
5263
5264 /* Try combine secondary memory reload insn FROM for insn TO into TO insn.
5265 FROM should be a load insn (usually a secondary memory reload insn). Return
5266 TRUE in case of success. */
5267 static bool
5268 combine_reload_insn (rtx_insn *from, rtx_insn *to)
5269 {
5270 bool ok_p;
5271 rtx_insn *saved_insn;
5272 rtx set, from_reg, to_reg, op;
5273 enum reg_class to_class, from_class;
5274 int n, nop;
5275 signed char changed_nops[MAX_RECOG_OPERANDS + 1];
5276
5277 /* Check conditions for second memory reload and original insn: */
5278 if ((targetm.secondary_memory_needed
5279 == hook_bool_mode_reg_class_t_reg_class_t_false)
5280 || NEXT_INSN (from) != to
5281 || !NONDEBUG_INSN_P (to)
5282 || CALL_P (to))
5283 return false;
5284
5285 lra_insn_recog_data_t id = lra_get_insn_recog_data (to);
5286 struct lra_static_insn_data *static_id = id->insn_static_data;
5287
5288 if (id->used_insn_alternative == LRA_UNKNOWN_ALT
5289 || (set = single_set (from)) == NULL_RTX)
5290 return false;
5291 from_reg = SET_DEST (set);
5292 to_reg = SET_SRC (set);
5293 /* Ignore optional reloads: */
5294 if (! REG_P (from_reg) || ! REG_P (to_reg)
5295 || bitmap_bit_p (&lra_optional_reload_pseudos, REGNO (from_reg)))
5296 return false;
5297 to_class = lra_get_allocno_class (REGNO (to_reg));
5298 from_class = lra_get_allocno_class (REGNO (from_reg));
5299 /* Check that reload insn is a load: */
5300 if (to_class != NO_REGS || from_class == NO_REGS)
5301 return false;
5302 for (n = nop = 0; nop < static_id->n_operands; nop++)
5303 {
5304 if (static_id->operand[nop].type != OP_IN)
5305 continue;
5306 op = *id->operand_loc[nop];
5307 if (!REG_P (op) || REGNO (op) != REGNO (from_reg))
5308 continue;
5309 *id->operand_loc[nop] = to_reg;
5310 changed_nops[n++] = nop;
5311 }
5312 changed_nops[n] = -1;
5313 lra_update_dups (id, changed_nops);
5314 lra_update_insn_regno_info (to);
5315 ok_p = recog_memoized (to) >= 0;
5316 if (ok_p)
5317 {
5318 /* Check that combined insn does not need any reloads: */
5319 saved_insn = curr_insn;
5320 curr_insn = to;
5321 curr_id = lra_get_insn_recog_data (curr_insn);
5322 curr_static_id = curr_id->insn_static_data;
5323 for (bool swapped_p = false;;)
5324 {
5325 ok_p = !curr_insn_transform (true);
5326 if (ok_p || curr_static_id->commutative < 0)
5327 break;
5328 swap_operands (curr_static_id->commutative);
5329 if (lra_dump_file != NULL)
5330 {
5331 fprintf (lra_dump_file,
5332 " Swapping %scombined insn operands:\n",
5333 swapped_p ? "back " : "");
5334 dump_insn_slim (lra_dump_file, to);
5335 }
5336 if (swapped_p)
5337 break;
5338 swapped_p = true;
5339 }
5340 curr_insn = saved_insn;
5341 curr_id = lra_get_insn_recog_data (curr_insn);
5342 curr_static_id = curr_id->insn_static_data;
5343 }
5344 if (ok_p)
5345 {
5346 id->used_insn_alternative = -1;
5347 lra_push_insn_and_update_insn_regno_info (to);
5348 if (lra_dump_file != NULL)
5349 {
5350 fprintf (lra_dump_file, " Use combined insn:\n");
5351 dump_insn_slim (lra_dump_file, to);
5352 }
5353 return true;
5354 }
5355 if (lra_dump_file != NULL)
5356 {
5357 fprintf (lra_dump_file, " Failed combined insn:\n");
5358 dump_insn_slim (lra_dump_file, to);
5359 }
5360 for (int i = 0; i < n; i++)
5361 {
5362 nop = changed_nops[i];
5363 *id->operand_loc[nop] = from_reg;
5364 }
5365 lra_update_dups (id, changed_nops);
5366 lra_update_insn_regno_info (to);
5367 if (lra_dump_file != NULL)
5368 {
5369 fprintf (lra_dump_file, " Restoring insn after failed combining:\n");
5370 dump_insn_slim (lra_dump_file, to);
5371 }
5372 return false;
5373 }
5374
5375 /* Entry function of LRA constraint pass. Return true if the
5376 constraint pass did change the code. */
5377 bool
5378 lra_constraints (bool first_p)
5379 {
5380 bool changed_p;
5381 int i, hard_regno, new_insns_num;
5382 unsigned int min_len, new_min_len, uid;
5383 rtx set, x, reg, nosubreg_dest;
5384 rtx_insn *original_insn;
5385 basic_block last_bb;
5386 bitmap_iterator bi;
5387
5388 lra_constraint_iter++;
5389 if (lra_dump_file != NULL)
5390 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
5391 lra_constraint_iter);
5392 changed_p = false;
5393 if (pic_offset_table_rtx
5394 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
5395 check_and_force_assignment_correctness_p = true;
5396 else if (first_p)
5397 /* On the first iteration we should check IRA assignment
5398 correctness. In rare cases, the assignments can be wrong as
5399 early clobbers operands are ignored in IRA or usages of
5400 paradoxical sub-registers are not taken into account by
5401 IRA. */
5402 check_and_force_assignment_correctness_p = true;
5403 new_insn_uid_start = get_max_uid ();
5404 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
5405 /* Mark used hard regs for target stack size calulations. */
5406 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5407 if (lra_reg_info[i].nrefs != 0
5408 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
5409 {
5410 int j, nregs;
5411
5412 nregs = hard_regno_nregs (hard_regno, lra_reg_info[i].biggest_mode);
5413 for (j = 0; j < nregs; j++)
5414 df_set_regs_ever_live (hard_regno + j, true);
5415 }
5416 /* Do elimination before the equivalence processing as we can spill
5417 some pseudos during elimination. */
5418 lra_eliminate (false, first_p);
5419 auto_bitmap equiv_insn_bitmap (&reg_obstack);
5420
5421 /* Register elimination can create new pseudos via the addptr pattern,
5422 so make sure the equivalency tables are resized appropriately. */
5423 ira_expand_reg_equiv ();
5424 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5425 if (lra_reg_info[i].nrefs != 0)
5426 {
5427 ira_reg_equiv[i].profitable_p = true;
5428 reg = regno_reg_rtx[i];
5429 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
5430 {
5431 bool pseudo_p = contains_reg_p (x, false, false);
5432
5433 /* After RTL transformation, we cannot guarantee that
5434 pseudo in the substitution was not reloaded which might
5435 make equivalence invalid. For example, in reverse
5436 equiv of p0
5437
5438 p0 <- ...
5439 ...
5440 equiv_mem <- p0
5441
5442 the memory address register was reloaded before the 2nd
5443 insn. */
5444 if ((! first_p && pseudo_p)
5445 /* We don't use DF for compilation speed sake. So it
5446 is problematic to update live info when we use an
5447 equivalence containing pseudos in more than one
5448 BB. */
5449 || (pseudo_p && multi_block_pseudo_p (i))
5450 /* If an init insn was deleted for some reason, cancel
5451 the equiv. We could update the equiv insns after
5452 transformations including an equiv insn deletion
5453 but it is not worthy as such cases are extremely
5454 rare. */
5455 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
5456 /* If it is not a reverse equivalence, we check that a
5457 pseudo in rhs of the init insn is not dying in the
5458 insn. Otherwise, the live info at the beginning of
5459 the corresponding BB might be wrong after we
5460 removed the insn. When the equiv can be a
5461 constant, the right hand side of the init insn can
5462 be a pseudo. */
5463 || (! reverse_equiv_p (i)
5464 && (init_insn_rhs_dead_pseudo_p (i)
5465 /* If we reloaded the pseudo in an equivalence
5466 init insn, we cannot remove the equiv init
5467 insns and the init insns might write into
5468 const memory in this case. */
5469 || contains_reloaded_insn_p (i)))
5470 /* Prevent access beyond equivalent memory for
5471 paradoxical subregs. */
5472 || (MEM_P (x)
5473 && maybe_gt (GET_MODE_SIZE (lra_reg_info[i].biggest_mode),
5474 GET_MODE_SIZE (GET_MODE (x))))
5475 || (pic_offset_table_rtx
5476 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
5477 && (targetm.preferred_reload_class
5478 (x, lra_get_allocno_class (i)) == NO_REGS))
5479 || contains_symbol_ref_p (x))))
5480 ira_reg_equiv[i].defined_p
5481 = ira_reg_equiv[i].caller_save_p = false;
5482 if (contains_reg_p (x, false, true))
5483 ira_reg_equiv[i].profitable_p = false;
5484 if (get_equiv (reg) != reg)
5485 bitmap_ior_into (equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
5486 }
5487 }
5488 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5489 update_equiv (i);
5490 /* We should add all insns containing pseudos which should be
5491 substituted by their equivalences. */
5492 EXECUTE_IF_SET_IN_BITMAP (equiv_insn_bitmap, 0, uid, bi)
5493 lra_push_insn_by_uid (uid);
5494 min_len = lra_insn_stack_length ();
5495 new_insns_num = 0;
5496 last_bb = NULL;
5497 changed_p = false;
5498 original_insn = NULL;
5499 while ((new_min_len = lra_insn_stack_length ()) != 0)
5500 {
5501 curr_insn = lra_pop_insn ();
5502 --new_min_len;
5503 curr_bb = BLOCK_FOR_INSN (curr_insn);
5504 if (curr_bb != last_bb)
5505 {
5506 last_bb = curr_bb;
5507 bb_reload_num = lra_curr_reload_num;
5508 }
5509 if (min_len > new_min_len)
5510 {
5511 min_len = new_min_len;
5512 new_insns_num = 0;
5513 original_insn = curr_insn;
5514 }
5515 else if (combine_reload_insn (curr_insn, original_insn))
5516 {
5517 continue;
5518 }
5519 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
5520 internal_error
5521 ("maximum number of generated reload insns per insn achieved (%d)",
5522 MAX_RELOAD_INSNS_NUMBER);
5523 new_insns_num++;
5524 if (DEBUG_INSN_P (curr_insn))
5525 {
5526 /* We need to check equivalence in debug insn and change
5527 pseudo to the equivalent value if necessary. */
5528 curr_id = lra_get_insn_recog_data (curr_insn);
5529 if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn)))
5530 {
5531 rtx old = *curr_id->operand_loc[0];
5532 *curr_id->operand_loc[0]
5533 = simplify_replace_fn_rtx (old, NULL_RTX,
5534 loc_equivalence_callback, curr_insn);
5535 if (old != *curr_id->operand_loc[0])
5536 {
5537 /* If we substitute pseudo by shared equivalence, we can fail
5538 to update LRA reg info and this can result in many
5539 unexpected consequences. So keep rtl unshared: */
5540 *curr_id->operand_loc[0]
5541 = copy_rtx (*curr_id->operand_loc[0]);
5542 lra_update_insn_regno_info (curr_insn);
5543 changed_p = true;
5544 }
5545 }
5546 }
5547 else if (INSN_P (curr_insn))
5548 {
5549 if ((set = single_set (curr_insn)) != NULL_RTX)
5550 {
5551 nosubreg_dest = SET_DEST (set);
5552 /* The equivalence pseudo could be set up as SUBREG in a
5553 case when it is a call restore insn in a mode
5554 different from the pseudo mode. */
5555 if (GET_CODE (nosubreg_dest) == SUBREG)
5556 nosubreg_dest = SUBREG_REG (nosubreg_dest);
5557 if ((REG_P (nosubreg_dest)
5558 && (x = get_equiv (nosubreg_dest)) != nosubreg_dest
5559 /* Remove insns which set up a pseudo whose value
5560 cannot be changed. Such insns might be not in
5561 init_insns because we don't update equiv data
5562 during insn transformations.
5563
5564 As an example, let suppose that a pseudo got
5565 hard register and on the 1st pass was not
5566 changed to equivalent constant. We generate an
5567 additional insn setting up the pseudo because of
5568 secondary memory movement. Then the pseudo is
5569 spilled and we use the equiv constant. In this
5570 case we should remove the additional insn and
5571 this insn is not init_insns list. */
5572 && (! MEM_P (x) || MEM_READONLY_P (x)
5573 /* Check that this is actually an insn setting
5574 up the equivalence. */
5575 || in_list_p (curr_insn,
5576 ira_reg_equiv
5577 [REGNO (nosubreg_dest)].init_insns)))
5578 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
5579 && in_list_p (curr_insn,
5580 ira_reg_equiv
5581 [REGNO (SET_SRC (set))].init_insns)
5582 /* This is a reverse equivalence to memory (see ira.cc)
5583 in store insn. We can reload all the destination and
5584 have an output reload which is a store to memory. If
5585 we just remove the insn, we will have the output
5586 reload storing an undefined value to the memory.
5587 Check that we did not reload the memory to prevent a
5588 wrong code generation. We could implement using the
5589 equivalence still in such case but doing this is not
5590 worth the efforts as such case is very rare. */
5591 && MEM_P (nosubreg_dest)))
5592 {
5593 /* This is equiv init insn of pseudo which did not get a
5594 hard register -- remove the insn. */
5595 if (lra_dump_file != NULL)
5596 {
5597 fprintf (lra_dump_file,
5598 " Removing equiv init insn %i (freq=%d)\n",
5599 INSN_UID (curr_insn),
5600 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
5601 dump_insn_slim (lra_dump_file, curr_insn);
5602 }
5603 if (contains_reg_p (x, true, false))
5604 check_and_force_assignment_correctness_p = true;
5605 lra_set_insn_deleted (curr_insn);
5606 continue;
5607 }
5608 }
5609 curr_id = lra_get_insn_recog_data (curr_insn);
5610 curr_static_id = curr_id->insn_static_data;
5611 init_curr_insn_input_reloads ();
5612 init_curr_operand_mode ();
5613 if (curr_insn_transform (false))
5614 changed_p = true;
5615 /* Check non-transformed insns too for equiv change as USE
5616 or CLOBBER don't need reloads but can contain pseudos
5617 being changed on their equivalences. */
5618 else if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn))
5619 && loc_equivalence_change_p (&PATTERN (curr_insn)))
5620 {
5621 lra_update_insn_regno_info (curr_insn);
5622 lra_push_insn_by_uid (INSN_UID (curr_insn));
5623 changed_p = true;
5624 }
5625 }
5626 }
5627
5628 /* If we used a new hard regno, changed_p should be true because the
5629 hard reg is assigned to a new pseudo. */
5630 if (flag_checking && !changed_p)
5631 {
5632 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5633 if (lra_reg_info[i].nrefs != 0
5634 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
5635 {
5636 int j, nregs = hard_regno_nregs (hard_regno,
5637 PSEUDO_REGNO_MODE (i));
5638
5639 for (j = 0; j < nregs; j++)
5640 lra_assert (df_regs_ever_live_p (hard_regno + j));
5641 }
5642 }
5643 if (changed_p)
5644 lra_dump_insns_if_possible ("changed func after local");
5645 return changed_p;
5646 }
5647
5648 static void initiate_invariants (void);
5649 static void finish_invariants (void);
5650
5651 /* Initiate the LRA constraint pass. It is done once per
5652 function. */
5653 void
5654 lra_constraints_init (void)
5655 {
5656 initiate_invariants ();
5657 }
5658
5659 /* Finalize the LRA constraint pass. It is done once per
5660 function. */
5661 void
5662 lra_constraints_finish (void)
5663 {
5664 finish_invariants ();
5665 }
5666
5667
5668
5669 /* Structure describes invariants for ineheritance. */
5670 struct lra_invariant
5671 {
5672 /* The order number of the invariant. */
5673 int num;
5674 /* The invariant RTX. */
5675 rtx invariant_rtx;
5676 /* The origin insn of the invariant. */
5677 rtx_insn *insn;
5678 };
5679
5680 typedef lra_invariant invariant_t;
5681 typedef invariant_t *invariant_ptr_t;
5682 typedef const invariant_t *const_invariant_ptr_t;
5683
5684 /* Pointer to the inheritance invariants. */
5685 static vec<invariant_ptr_t> invariants;
5686
5687 /* Allocation pool for the invariants. */
5688 static object_allocator<lra_invariant> *invariants_pool;
5689
5690 /* Hash table for the invariants. */
5691 static htab_t invariant_table;
5692
5693 /* Hash function for INVARIANT. */
5694 static hashval_t
5695 invariant_hash (const void *invariant)
5696 {
5697 rtx inv = ((const_invariant_ptr_t) invariant)->invariant_rtx;
5698 return lra_rtx_hash (inv);
5699 }
5700
5701 /* Equal function for invariants INVARIANT1 and INVARIANT2. */
5702 static int
5703 invariant_eq_p (const void *invariant1, const void *invariant2)
5704 {
5705 rtx inv1 = ((const_invariant_ptr_t) invariant1)->invariant_rtx;
5706 rtx inv2 = ((const_invariant_ptr_t) invariant2)->invariant_rtx;
5707
5708 return rtx_equal_p (inv1, inv2);
5709 }
5710
5711 /* Insert INVARIANT_RTX into the table if it is not there yet. Return
5712 invariant which is in the table. */
5713 static invariant_ptr_t
5714 insert_invariant (rtx invariant_rtx)
5715 {
5716 void **entry_ptr;
5717 invariant_t invariant;
5718 invariant_ptr_t invariant_ptr;
5719
5720 invariant.invariant_rtx = invariant_rtx;
5721 entry_ptr = htab_find_slot (invariant_table, &invariant, INSERT);
5722 if (*entry_ptr == NULL)
5723 {
5724 invariant_ptr = invariants_pool->allocate ();
5725 invariant_ptr->invariant_rtx = invariant_rtx;
5726 invariant_ptr->insn = NULL;
5727 invariants.safe_push (invariant_ptr);
5728 *entry_ptr = (void *) invariant_ptr;
5729 }
5730 return (invariant_ptr_t) *entry_ptr;
5731 }
5732
5733 /* Initiate the invariant table. */
5734 static void
5735 initiate_invariants (void)
5736 {
5737 invariants.create (100);
5738 invariants_pool
5739 = new object_allocator<lra_invariant> ("Inheritance invariants");
5740 invariant_table = htab_create (100, invariant_hash, invariant_eq_p, NULL);
5741 }
5742
5743 /* Finish the invariant table. */
5744 static void
5745 finish_invariants (void)
5746 {
5747 htab_delete (invariant_table);
5748 delete invariants_pool;
5749 invariants.release ();
5750 }
5751
5752 /* Make the invariant table empty. */
5753 static void
5754 clear_invariants (void)
5755 {
5756 htab_empty (invariant_table);
5757 invariants_pool->release ();
5758 invariants.truncate (0);
5759 }
5760
5761
5762
5763 /* This page contains code to do inheritance/split
5764 transformations. */
5765
5766 /* Number of reloads passed so far in current EBB. */
5767 static int reloads_num;
5768
5769 /* Number of calls passed so far in current EBB. */
5770 static int calls_num;
5771
5772 /* Index ID is the CALLS_NUM associated the last call we saw with
5773 ABI identifier ID. */
5774 static int last_call_for_abi[NUM_ABI_IDS];
5775
5776 /* Which registers have been fully or partially clobbered by a call
5777 since they were last used. */
5778 static HARD_REG_SET full_and_partial_call_clobbers;
5779
5780 /* Current reload pseudo check for validity of elements in
5781 USAGE_INSNS. */
5782 static int curr_usage_insns_check;
5783
5784 /* Info about last usage of registers in EBB to do inheritance/split
5785 transformation. Inheritance transformation is done from a spilled
5786 pseudo and split transformations from a hard register or a pseudo
5787 assigned to a hard register. */
5788 struct usage_insns
5789 {
5790 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
5791 value INSNS is valid. The insns is chain of optional debug insns
5792 and a finishing non-debug insn using the corresponding reg. The
5793 value is also used to mark the registers which are set up in the
5794 current insn. The negated insn uid is used for this. */
5795 int check;
5796 /* Value of global reloads_num at the last insn in INSNS. */
5797 int reloads_num;
5798 /* Value of global reloads_nums at the last insn in INSNS. */
5799 int calls_num;
5800 /* It can be true only for splitting. And it means that the restore
5801 insn should be put after insn given by the following member. */
5802 bool after_p;
5803 /* Next insns in the current EBB which use the original reg and the
5804 original reg value is not changed between the current insn and
5805 the next insns. In order words, e.g. for inheritance, if we need
5806 to use the original reg value again in the next insns we can try
5807 to use the value in a hard register from a reload insn of the
5808 current insn. */
5809 rtx insns;
5810 };
5811
5812 /* Map: regno -> corresponding pseudo usage insns. */
5813 static struct usage_insns *usage_insns;
5814
5815 static void
5816 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
5817 {
5818 usage_insns[regno].check = curr_usage_insns_check;
5819 usage_insns[regno].insns = insn;
5820 usage_insns[regno].reloads_num = reloads_num;
5821 usage_insns[regno].calls_num = calls_num;
5822 usage_insns[regno].after_p = after_p;
5823 if (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0)
5824 remove_from_hard_reg_set (&full_and_partial_call_clobbers,
5825 PSEUDO_REGNO_MODE (regno),
5826 reg_renumber[regno]);
5827 }
5828
5829 /* The function is used to form list REGNO usages which consists of
5830 optional debug insns finished by a non-debug insn using REGNO.
5831 RELOADS_NUM is current number of reload insns processed so far. */
5832 static void
5833 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
5834 {
5835 rtx next_usage_insns;
5836
5837 if (usage_insns[regno].check == curr_usage_insns_check
5838 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
5839 && DEBUG_INSN_P (insn))
5840 {
5841 /* Check that we did not add the debug insn yet. */
5842 if (next_usage_insns != insn
5843 && (GET_CODE (next_usage_insns) != INSN_LIST
5844 || XEXP (next_usage_insns, 0) != insn))
5845 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
5846 next_usage_insns);
5847 }
5848 else if (NONDEBUG_INSN_P (insn))
5849 setup_next_usage_insn (regno, insn, reloads_num, false);
5850 else
5851 usage_insns[regno].check = 0;
5852 }
5853
5854 /* Return first non-debug insn in list USAGE_INSNS. */
5855 static rtx_insn *
5856 skip_usage_debug_insns (rtx usage_insns)
5857 {
5858 rtx insn;
5859
5860 /* Skip debug insns. */
5861 for (insn = usage_insns;
5862 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
5863 insn = XEXP (insn, 1))
5864 ;
5865 return safe_as_a <rtx_insn *> (insn);
5866 }
5867
5868 /* Return true if we need secondary memory moves for insn in
5869 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
5870 into the insn. */
5871 static bool
5872 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
5873 rtx usage_insns ATTRIBUTE_UNUSED)
5874 {
5875 rtx_insn *insn;
5876 rtx set, dest;
5877 enum reg_class cl;
5878
5879 if (inher_cl == ALL_REGS
5880 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
5881 return false;
5882 lra_assert (INSN_P (insn));
5883 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
5884 return false;
5885 dest = SET_DEST (set);
5886 if (! REG_P (dest))
5887 return false;
5888 lra_assert (inher_cl != NO_REGS);
5889 cl = get_reg_class (REGNO (dest));
5890 return (cl != NO_REGS && cl != ALL_REGS
5891 && targetm.secondary_memory_needed (GET_MODE (dest), inher_cl, cl));
5892 }
5893
5894 /* Registers involved in inheritance/split in the current EBB
5895 (inheritance/split pseudos and original registers). */
5896 static bitmap_head check_only_regs;
5897
5898 /* Reload pseudos cannot be involded in invariant inheritance in the
5899 current EBB. */
5900 static bitmap_head invalid_invariant_regs;
5901
5902 /* Do inheritance transformations for insn INSN, which defines (if
5903 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
5904 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
5905 form as the "insns" field of usage_insns. Return true if we
5906 succeed in such transformation.
5907
5908 The transformations look like:
5909
5910 p <- ... i <- ...
5911 ... p <- i (new insn)
5912 ... =>
5913 <- ... p ... <- ... i ...
5914 or
5915 ... i <- p (new insn)
5916 <- ... p ... <- ... i ...
5917 ... =>
5918 <- ... p ... <- ... i ...
5919 where p is a spilled original pseudo and i is a new inheritance pseudo.
5920
5921
5922 The inheritance pseudo has the smallest class of two classes CL and
5923 class of ORIGINAL REGNO. */
5924 static bool
5925 inherit_reload_reg (bool def_p, int original_regno,
5926 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
5927 {
5928 if (optimize_function_for_size_p (cfun))
5929 return false;
5930
5931 enum reg_class rclass = lra_get_allocno_class (original_regno);
5932 rtx original_reg = regno_reg_rtx[original_regno];
5933 rtx new_reg, usage_insn;
5934 rtx_insn *new_insns;
5935
5936 lra_assert (! usage_insns[original_regno].after_p);
5937 if (lra_dump_file != NULL)
5938 fprintf (lra_dump_file,
5939 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
5940 if (! ira_reg_classes_intersect_p[cl][rclass])
5941 {
5942 if (lra_dump_file != NULL)
5943 {
5944 fprintf (lra_dump_file,
5945 " Rejecting inheritance for %d "
5946 "because of disjoint classes %s and %s\n",
5947 original_regno, reg_class_names[cl],
5948 reg_class_names[rclass]);
5949 fprintf (lra_dump_file,
5950 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5951 }
5952 return false;
5953 }
5954 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
5955 /* We don't use a subset of two classes because it can be
5956 NO_REGS. This transformation is still profitable in most
5957 cases even if the classes are not intersected as register
5958 move is probably cheaper than a memory load. */
5959 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
5960 {
5961 if (lra_dump_file != NULL)
5962 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
5963 reg_class_names[cl], reg_class_names[rclass]);
5964
5965 rclass = cl;
5966 }
5967 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
5968 {
5969 /* Reject inheritance resulting in secondary memory moves.
5970 Otherwise, there is a danger in LRA cycling. Also such
5971 transformation will be unprofitable. */
5972 if (lra_dump_file != NULL)
5973 {
5974 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
5975 rtx set = single_set (insn);
5976
5977 lra_assert (set != NULL_RTX);
5978
5979 rtx dest = SET_DEST (set);
5980
5981 lra_assert (REG_P (dest));
5982 fprintf (lra_dump_file,
5983 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
5984 "as secondary mem is needed\n",
5985 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
5986 original_regno, reg_class_names[rclass]);
5987 fprintf (lra_dump_file,
5988 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5989 }
5990 return false;
5991 }
5992 if (ira_reg_class_min_nregs[rclass][GET_MODE (original_reg)]
5993 != ira_reg_class_max_nregs[rclass][GET_MODE (original_reg)])
5994 {
5995 if (lra_dump_file != NULL)
5996 {
5997 fprintf (lra_dump_file,
5998 " Rejecting inheritance for %d "
5999 "because of requiring non-uniform class %s\n",
6000 original_regno, reg_class_names[rclass]);
6001 fprintf (lra_dump_file,
6002 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
6003 }
6004 return false;
6005 }
6006 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
6007 rclass, NULL, "inheritance");
6008 start_sequence ();
6009 if (def_p)
6010 lra_emit_move (original_reg, new_reg);
6011 else
6012 lra_emit_move (new_reg, original_reg);
6013 new_insns = get_insns ();
6014 end_sequence ();
6015 if (NEXT_INSN (new_insns) != NULL_RTX)
6016 {
6017 if (lra_dump_file != NULL)
6018 {
6019 fprintf (lra_dump_file,
6020 " Rejecting inheritance %d->%d "
6021 "as it results in 2 or more insns:\n",
6022 original_regno, REGNO (new_reg));
6023 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
6024 fprintf (lra_dump_file,
6025 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
6026 }
6027 return false;
6028 }
6029 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
6030 lra_update_insn_regno_info (insn);
6031 if (! def_p)
6032 /* We now have a new usage insn for original regno. */
6033 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
6034 if (lra_dump_file != NULL)
6035 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
6036 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
6037 lra_reg_info[REGNO (new_reg)].restore_rtx = regno_reg_rtx[original_regno];
6038 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
6039 bitmap_set_bit (&check_only_regs, original_regno);
6040 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
6041 if (def_p)
6042 lra_process_new_insns (insn, NULL, new_insns,
6043 "Add original<-inheritance");
6044 else
6045 lra_process_new_insns (insn, new_insns, NULL,
6046 "Add inheritance<-original");
6047 while (next_usage_insns != NULL_RTX)
6048 {
6049 if (GET_CODE (next_usage_insns) != INSN_LIST)
6050 {
6051 usage_insn = next_usage_insns;
6052 lra_assert (NONDEBUG_INSN_P (usage_insn));
6053 next_usage_insns = NULL;
6054 }
6055 else
6056 {
6057 usage_insn = XEXP (next_usage_insns, 0);
6058 lra_assert (DEBUG_INSN_P (usage_insn));
6059 next_usage_insns = XEXP (next_usage_insns, 1);
6060 }
6061 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
6062 DEBUG_INSN_P (usage_insn));
6063 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
6064 if (lra_dump_file != NULL)
6065 {
6066 basic_block bb = BLOCK_FOR_INSN (usage_insn);
6067 fprintf (lra_dump_file,
6068 " Inheritance reuse change %d->%d (bb%d):\n",
6069 original_regno, REGNO (new_reg),
6070 bb ? bb->index : -1);
6071 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
6072 }
6073 }
6074 if (lra_dump_file != NULL)
6075 fprintf (lra_dump_file,
6076 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
6077 return true;
6078 }
6079
6080 /* Return true if we need a caller save/restore for pseudo REGNO which
6081 was assigned to a hard register. */
6082 static inline bool
6083 need_for_call_save_p (int regno)
6084 {
6085 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
6086 if (usage_insns[regno].calls_num < calls_num)
6087 {
6088 unsigned int abis = 0;
6089 for (unsigned int i = 0; i < NUM_ABI_IDS; ++i)
6090 if (last_call_for_abi[i] > usage_insns[regno].calls_num)
6091 abis |= 1 << i;
6092 gcc_assert (abis);
6093 if (call_clobbered_in_region_p (abis, full_and_partial_call_clobbers,
6094 PSEUDO_REGNO_MODE (regno),
6095 reg_renumber[regno]))
6096 return true;
6097 }
6098 return false;
6099 }
6100
6101 /* Global registers occurring in the current EBB. */
6102 static bitmap_head ebb_global_regs;
6103
6104 /* Return true if we need a split for hard register REGNO or pseudo
6105 REGNO which was assigned to a hard register.
6106 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
6107 used for reloads since the EBB end. It is an approximation of the
6108 used hard registers in the split range. The exact value would
6109 require expensive calculations. If we were aggressive with
6110 splitting because of the approximation, the split pseudo will save
6111 the same hard register assignment and will be removed in the undo
6112 pass. We still need the approximation because too aggressive
6113 splitting would result in too inaccurate cost calculation in the
6114 assignment pass because of too many generated moves which will be
6115 probably removed in the undo pass. */
6116 static inline bool
6117 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
6118 {
6119 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
6120
6121 lra_assert (hard_regno >= 0);
6122 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
6123 /* Don't split eliminable hard registers, otherwise we can
6124 split hard registers like hard frame pointer, which
6125 lives on BB start/end according to DF-infrastructure,
6126 when there is a pseudo assigned to the register and
6127 living in the same BB. */
6128 && (regno >= FIRST_PSEUDO_REGISTER
6129 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
6130 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
6131 /* Don't split call clobbered hard regs living through
6132 calls, otherwise we might have a check problem in the
6133 assign sub-pass as in the most cases (exception is a
6134 situation when check_and_force_assignment_correctness_p value is
6135 true) the assign pass assumes that all pseudos living
6136 through calls are assigned to call saved hard regs. */
6137 && (regno >= FIRST_PSEUDO_REGISTER
6138 || !TEST_HARD_REG_BIT (full_and_partial_call_clobbers, regno))
6139 /* We need at least 2 reloads to make pseudo splitting
6140 profitable. We should provide hard regno splitting in
6141 any case to solve 1st insn scheduling problem when
6142 moving hard register definition up might result in
6143 impossibility to find hard register for reload pseudo of
6144 small register class. */
6145 && (usage_insns[regno].reloads_num
6146 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
6147 && (regno < FIRST_PSEUDO_REGISTER
6148 /* For short living pseudos, spilling + inheritance can
6149 be considered a substitution for splitting.
6150 Therefore we do not splitting for local pseudos. It
6151 decreases also aggressiveness of splitting. The
6152 minimal number of references is chosen taking into
6153 account that for 2 references splitting has no sense
6154 as we can just spill the pseudo. */
6155 || (regno >= FIRST_PSEUDO_REGISTER
6156 && lra_reg_info[regno].nrefs > 3
6157 && bitmap_bit_p (&ebb_global_regs, regno))))
6158 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
6159 }
6160
6161 /* Return class for the split pseudo created from original pseudo with
6162 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
6163 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
6164 results in no secondary memory movements. */
6165 static enum reg_class
6166 choose_split_class (enum reg_class allocno_class,
6167 int hard_regno ATTRIBUTE_UNUSED,
6168 machine_mode mode ATTRIBUTE_UNUSED)
6169 {
6170 int i;
6171 enum reg_class cl, best_cl = NO_REGS;
6172 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
6173 = REGNO_REG_CLASS (hard_regno);
6174
6175 if (! targetm.secondary_memory_needed (mode, allocno_class, allocno_class)
6176 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
6177 return allocno_class;
6178 for (i = 0;
6179 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
6180 i++)
6181 if (! targetm.secondary_memory_needed (mode, cl, hard_reg_class)
6182 && ! targetm.secondary_memory_needed (mode, hard_reg_class, cl)
6183 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
6184 && (best_cl == NO_REGS
6185 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
6186 best_cl = cl;
6187 return best_cl;
6188 }
6189
6190 /* Copy any equivalence information from ORIGINAL_REGNO to NEW_REGNO. It only
6191 makes sense to call this function if NEW_REGNO is always equal to
6192 ORIGINAL_REGNO. Set up defined_p flag when caller_save_p flag is set up and
6193 CALL_SAVE_P is true. */
6194
6195 static void
6196 lra_copy_reg_equiv (unsigned int new_regno, unsigned int original_regno,
6197 bool call_save_p)
6198 {
6199 if (!ira_reg_equiv[original_regno].defined_p
6200 && !(call_save_p && ira_reg_equiv[original_regno].caller_save_p))
6201 return;
6202
6203 ira_expand_reg_equiv ();
6204 ira_reg_equiv[new_regno].defined_p = true;
6205 if (ira_reg_equiv[original_regno].memory)
6206 ira_reg_equiv[new_regno].memory
6207 = copy_rtx (ira_reg_equiv[original_regno].memory);
6208 if (ira_reg_equiv[original_regno].constant)
6209 ira_reg_equiv[new_regno].constant
6210 = copy_rtx (ira_reg_equiv[original_regno].constant);
6211 if (ira_reg_equiv[original_regno].invariant)
6212 ira_reg_equiv[new_regno].invariant
6213 = copy_rtx (ira_reg_equiv[original_regno].invariant);
6214 }
6215
6216 /* Do split transformations for insn INSN, which defines or uses
6217 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
6218 the EBB next uses ORIGINAL_REGNO; it has the same form as the
6219 "insns" field of usage_insns. If TO is not NULL, we don't use
6220 usage_insns, we put restore insns after TO insn. It is a case when
6221 we call it from lra_split_hard_reg_for, outside the inheritance
6222 pass.
6223
6224 The transformations look like:
6225
6226 p <- ... p <- ...
6227 ... s <- p (new insn -- save)
6228 ... =>
6229 ... p <- s (new insn -- restore)
6230 <- ... p ... <- ... p ...
6231 or
6232 <- ... p ... <- ... p ...
6233 ... s <- p (new insn -- save)
6234 ... =>
6235 ... p <- s (new insn -- restore)
6236 <- ... p ... <- ... p ...
6237
6238 where p is an original pseudo got a hard register or a hard
6239 register and s is a new split pseudo. The save is put before INSN
6240 if BEFORE_P is true. Return true if we succeed in such
6241 transformation. */
6242 static bool
6243 split_reg (bool before_p, int original_regno, rtx_insn *insn,
6244 rtx next_usage_insns, rtx_insn *to)
6245 {
6246 enum reg_class rclass;
6247 rtx original_reg;
6248 int hard_regno, nregs;
6249 rtx new_reg, usage_insn;
6250 rtx_insn *restore, *save;
6251 bool after_p;
6252 bool call_save_p;
6253 machine_mode mode;
6254
6255 if (original_regno < FIRST_PSEUDO_REGISTER)
6256 {
6257 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
6258 hard_regno = original_regno;
6259 call_save_p = false;
6260 nregs = 1;
6261 mode = lra_reg_info[hard_regno].biggest_mode;
6262 machine_mode reg_rtx_mode = GET_MODE (regno_reg_rtx[hard_regno]);
6263 /* A reg can have a biggest_mode of VOIDmode if it was only ever seen as
6264 part of a multi-word register. In that case, just use the reg_rtx
6265 mode. Do the same also if the biggest mode was larger than a register
6266 or we can not compare the modes. Otherwise, limit the size to that of
6267 the biggest access in the function or to the natural mode at least. */
6268 if (mode == VOIDmode
6269 || !ordered_p (GET_MODE_PRECISION (mode),
6270 GET_MODE_PRECISION (reg_rtx_mode))
6271 || paradoxical_subreg_p (mode, reg_rtx_mode)
6272 || maybe_gt (GET_MODE_PRECISION (reg_rtx_mode), GET_MODE_PRECISION (mode)))
6273 {
6274 original_reg = regno_reg_rtx[hard_regno];
6275 mode = reg_rtx_mode;
6276 }
6277 else
6278 original_reg = gen_rtx_REG (mode, hard_regno);
6279 }
6280 else
6281 {
6282 mode = PSEUDO_REGNO_MODE (original_regno);
6283 hard_regno = reg_renumber[original_regno];
6284 nregs = hard_regno_nregs (hard_regno, mode);
6285 rclass = lra_get_allocno_class (original_regno);
6286 original_reg = regno_reg_rtx[original_regno];
6287 call_save_p = need_for_call_save_p (original_regno);
6288 }
6289 lra_assert (hard_regno >= 0);
6290 if (lra_dump_file != NULL)
6291 fprintf (lra_dump_file,
6292 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
6293
6294 if (call_save_p)
6295 {
6296 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
6297 hard_regno_nregs (hard_regno, mode),
6298 mode);
6299 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, NULL, "save");
6300 }
6301 else
6302 {
6303 rclass = choose_split_class (rclass, hard_regno, mode);
6304 if (rclass == NO_REGS)
6305 {
6306 if (lra_dump_file != NULL)
6307 {
6308 fprintf (lra_dump_file,
6309 " Rejecting split of %d(%s): "
6310 "no good reg class for %d(%s)\n",
6311 original_regno,
6312 reg_class_names[lra_get_allocno_class (original_regno)],
6313 hard_regno,
6314 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
6315 fprintf
6316 (lra_dump_file,
6317 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6318 }
6319 return false;
6320 }
6321 /* Split_if_necessary can split hard registers used as part of a
6322 multi-register mode but splits each register individually. The
6323 mode used for each independent register may not be supported
6324 so reject the split. Splitting the wider mode should theoretically
6325 be possible but is not implemented. */
6326 if (!targetm.hard_regno_mode_ok (hard_regno, mode))
6327 {
6328 if (lra_dump_file != NULL)
6329 {
6330 fprintf (lra_dump_file,
6331 " Rejecting split of %d(%s): unsuitable mode %s\n",
6332 original_regno,
6333 reg_class_names[lra_get_allocno_class (original_regno)],
6334 GET_MODE_NAME (mode));
6335 fprintf
6336 (lra_dump_file,
6337 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6338 }
6339 return false;
6340 }
6341 new_reg = lra_create_new_reg (mode, original_reg, rclass, NULL, "split");
6342 reg_renumber[REGNO (new_reg)] = hard_regno;
6343 }
6344 int new_regno = REGNO (new_reg);
6345 save = emit_spill_move (true, new_reg, original_reg);
6346 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
6347 {
6348 if (lra_dump_file != NULL)
6349 {
6350 fprintf
6351 (lra_dump_file,
6352 " Rejecting split %d->%d resulting in > 2 save insns:\n",
6353 original_regno, new_regno);
6354 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
6355 fprintf (lra_dump_file,
6356 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6357 }
6358 return false;
6359 }
6360 restore = emit_spill_move (false, new_reg, original_reg);
6361 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
6362 {
6363 if (lra_dump_file != NULL)
6364 {
6365 fprintf (lra_dump_file,
6366 " Rejecting split %d->%d "
6367 "resulting in > 2 restore insns:\n",
6368 original_regno, new_regno);
6369 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
6370 fprintf (lra_dump_file,
6371 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6372 }
6373 return false;
6374 }
6375 /* Transfer equivalence information to the spill register, so that
6376 if we fail to allocate the spill register, we have the option of
6377 rematerializing the original value instead of spilling to the stack. */
6378 if (!HARD_REGISTER_NUM_P (original_regno)
6379 && mode == PSEUDO_REGNO_MODE (original_regno))
6380 lra_copy_reg_equiv (new_regno, original_regno, call_save_p);
6381 lra_reg_info[new_regno].restore_rtx = regno_reg_rtx[original_regno];
6382 bitmap_set_bit (&lra_split_regs, new_regno);
6383 if (to != NULL)
6384 {
6385 lra_assert (next_usage_insns == NULL);
6386 usage_insn = to;
6387 after_p = true;
6388 }
6389 else
6390 {
6391 /* We need check_only_regs only inside the inheritance pass. */
6392 bitmap_set_bit (&check_only_regs, new_regno);
6393 bitmap_set_bit (&check_only_regs, original_regno);
6394 after_p = usage_insns[original_regno].after_p;
6395 for (;;)
6396 {
6397 if (GET_CODE (next_usage_insns) != INSN_LIST)
6398 {
6399 usage_insn = next_usage_insns;
6400 break;
6401 }
6402 usage_insn = XEXP (next_usage_insns, 0);
6403 lra_assert (DEBUG_INSN_P (usage_insn));
6404 next_usage_insns = XEXP (next_usage_insns, 1);
6405 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
6406 true);
6407 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
6408 if (lra_dump_file != NULL)
6409 {
6410 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
6411 original_regno, new_regno);
6412 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
6413 }
6414 }
6415 }
6416 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
6417 lra_assert (usage_insn != insn || (after_p && before_p));
6418 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
6419 after_p ? NULL : restore,
6420 after_p ? restore : NULL,
6421 call_save_p ? "Add reg<-save" : "Add reg<-split");
6422 if (call_save_p
6423 && first_call_insn != NULL
6424 && BLOCK_FOR_INSN (first_call_insn) != BLOCK_FOR_INSN (insn))
6425 /* PR116028: If original_regno is a pseudo that has been assigned a
6426 callee-saved hard register, then emit the spill insn before the call
6427 insn 'first_call_insn' instead of adjacent to 'insn'. If 'insn'
6428 and 'first_call_insn' belong to the same EBB but to two separate
6429 BBs, and if 'insn' is present in the entry BB, then generating the
6430 spill insn in the entry BB can prevent shrink wrap from happening.
6431 This is because the spill insn references the stack pointer and
6432 hence the prolog gets generated in the entry BB itself. It is
6433 also more efficient to generate the spill before
6434 'first_call_insn' as the spill now occurs only in the path
6435 containing the call. */
6436 lra_process_new_insns (first_call_insn, save, NULL, "Add save<-reg");
6437 else
6438 lra_process_new_insns (insn, before_p ? save : NULL,
6439 before_p ? NULL : save,
6440 call_save_p ? "Add save<-reg" : "Add split<-reg");
6441 if (nregs > 1 || original_regno < FIRST_PSEUDO_REGISTER)
6442 /* If we are trying to split multi-register. We should check
6443 conflicts on the next assignment sub-pass. IRA can allocate on
6444 sub-register levels, LRA do this on pseudos level right now and
6445 this discrepancy may create allocation conflicts after
6446 splitting.
6447
6448 If we are trying to split hard register we should also check conflicts
6449 as such splitting can create artificial conflict of the hard register
6450 with another pseudo because of simplified conflict calculation in
6451 LRA. */
6452 check_and_force_assignment_correctness_p = true;
6453 if (lra_dump_file != NULL)
6454 fprintf (lra_dump_file,
6455 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6456 return true;
6457 }
6458
6459 /* Split a hard reg for reload pseudo REGNO having RCLASS and living
6460 in the range [FROM, TO]. Return true if did a split. Otherwise,
6461 return false. */
6462 bool
6463 spill_hard_reg_in_range (int regno, enum reg_class rclass, rtx_insn *from, rtx_insn *to)
6464 {
6465 int i, hard_regno;
6466 int rclass_size;
6467 rtx_insn *insn;
6468 unsigned int uid;
6469 bitmap_iterator bi;
6470 HARD_REG_SET ignore;
6471
6472 lra_assert (from != NULL && to != NULL);
6473 ignore = lra_no_alloc_regs;
6474 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
6475 {
6476 lra_insn_recog_data_t id = lra_insn_recog_data[uid];
6477 struct lra_static_insn_data *static_id = id->insn_static_data;
6478 struct lra_insn_reg *reg;
6479
6480 for (reg = id->regs; reg != NULL; reg = reg->next)
6481 if (reg->regno < FIRST_PSEUDO_REGISTER)
6482 SET_HARD_REG_BIT (ignore, reg->regno);
6483 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
6484 SET_HARD_REG_BIT (ignore, reg->regno);
6485 }
6486 rclass_size = ira_class_hard_regs_num[rclass];
6487 for (i = 0; i < rclass_size; i++)
6488 {
6489 hard_regno = ira_class_hard_regs[rclass][i];
6490 if (! TEST_HARD_REG_BIT (lra_reg_info[regno].conflict_hard_regs, hard_regno)
6491 || TEST_HARD_REG_BIT (ignore, hard_regno))
6492 continue;
6493 for (insn = from; insn != NEXT_INSN (to); insn = NEXT_INSN (insn))
6494 {
6495 struct lra_static_insn_data *static_id;
6496 struct lra_insn_reg *reg;
6497
6498 if (!INSN_P (insn))
6499 continue;
6500 if (bitmap_bit_p (&lra_reg_info[hard_regno].insn_bitmap,
6501 INSN_UID (insn)))
6502 break;
6503 static_id = lra_get_insn_recog_data (insn)->insn_static_data;
6504 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
6505 if (reg->regno == hard_regno)
6506 break;
6507 if (reg != NULL)
6508 break;
6509 }
6510 if (insn != NEXT_INSN (to))
6511 continue;
6512 if (split_reg (true, hard_regno, from, NULL, to))
6513 return true;
6514 }
6515 return false;
6516 }
6517
6518 /* Recognize that we need a split transformation for insn INSN, which
6519 defines or uses REGNO in its insn biggest MODE (we use it only if
6520 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
6521 hard registers which might be used for reloads since the EBB end.
6522 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
6523 uid before starting INSN processing. Return true if we succeed in
6524 such transformation. */
6525 static bool
6526 split_if_necessary (int regno, machine_mode mode,
6527 HARD_REG_SET potential_reload_hard_regs,
6528 bool before_p, rtx_insn *insn, int max_uid)
6529 {
6530 bool res = false;
6531 int i, nregs = 1;
6532 rtx next_usage_insns;
6533
6534 if (regno < FIRST_PSEUDO_REGISTER)
6535 nregs = hard_regno_nregs (regno, mode);
6536 for (i = 0; i < nregs; i++)
6537 if (usage_insns[regno + i].check == curr_usage_insns_check
6538 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
6539 /* To avoid processing the register twice or more. */
6540 && ((GET_CODE (next_usage_insns) != INSN_LIST
6541 && INSN_UID (next_usage_insns) < max_uid)
6542 || (GET_CODE (next_usage_insns) == INSN_LIST
6543 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
6544 && need_for_split_p (potential_reload_hard_regs, regno + i)
6545 && split_reg (before_p, regno + i, insn, next_usage_insns, NULL))
6546 res = true;
6547 return res;
6548 }
6549
6550 /* Return TRUE if rtx X is considered as an invariant for
6551 inheritance. */
6552 static bool
6553 invariant_p (const_rtx x)
6554 {
6555 machine_mode mode;
6556 const char *fmt;
6557 enum rtx_code code;
6558 int i, j;
6559
6560 if (side_effects_p (x))
6561 return false;
6562
6563 code = GET_CODE (x);
6564 mode = GET_MODE (x);
6565 if (code == SUBREG)
6566 {
6567 x = SUBREG_REG (x);
6568 code = GET_CODE (x);
6569 mode = wider_subreg_mode (mode, GET_MODE (x));
6570 }
6571
6572 if (MEM_P (x))
6573 return false;
6574
6575 if (REG_P (x))
6576 {
6577 int i, nregs, regno = REGNO (x);
6578
6579 if (regno >= FIRST_PSEUDO_REGISTER || regno == STACK_POINTER_REGNUM
6580 || TEST_HARD_REG_BIT (eliminable_regset, regno)
6581 || GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
6582 return false;
6583 nregs = hard_regno_nregs (regno, mode);
6584 for (i = 0; i < nregs; i++)
6585 if (! fixed_regs[regno + i]
6586 /* A hard register may be clobbered in the current insn
6587 but we can ignore this case because if the hard
6588 register is used it should be set somewhere after the
6589 clobber. */
6590 || bitmap_bit_p (&invalid_invariant_regs, regno + i))
6591 return false;
6592 }
6593 fmt = GET_RTX_FORMAT (code);
6594 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6595 {
6596 if (fmt[i] == 'e')
6597 {
6598 if (! invariant_p (XEXP (x, i)))
6599 return false;
6600 }
6601 else if (fmt[i] == 'E')
6602 {
6603 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6604 if (! invariant_p (XVECEXP (x, i, j)))
6605 return false;
6606 }
6607 }
6608 return true;
6609 }
6610
6611 /* We have 'dest_reg <- invariant'. Let us try to make an invariant
6612 inheritance transformation (using dest_reg instead invariant in a
6613 subsequent insn). */
6614 static bool
6615 process_invariant_for_inheritance (rtx dst_reg, rtx invariant_rtx)
6616 {
6617 invariant_ptr_t invariant_ptr;
6618 rtx_insn *insn, *new_insns;
6619 rtx insn_set, insn_reg, new_reg;
6620 int insn_regno;
6621 bool succ_p = false;
6622 int dst_regno = REGNO (dst_reg);
6623 machine_mode dst_mode = GET_MODE (dst_reg);
6624 enum reg_class cl = lra_get_allocno_class (dst_regno), insn_reg_cl;
6625
6626 invariant_ptr = insert_invariant (invariant_rtx);
6627 if ((insn = invariant_ptr->insn) != NULL_RTX)
6628 {
6629 /* We have a subsequent insn using the invariant. */
6630 insn_set = single_set (insn);
6631 lra_assert (insn_set != NULL);
6632 insn_reg = SET_DEST (insn_set);
6633 lra_assert (REG_P (insn_reg));
6634 insn_regno = REGNO (insn_reg);
6635 insn_reg_cl = lra_get_allocno_class (insn_regno);
6636
6637 if (dst_mode == GET_MODE (insn_reg)
6638 /* We should consider only result move reg insns which are
6639 cheap. */
6640 && targetm.register_move_cost (dst_mode, cl, insn_reg_cl) == 2
6641 && targetm.register_move_cost (dst_mode, cl, cl) == 2)
6642 {
6643 if (lra_dump_file != NULL)
6644 fprintf (lra_dump_file,
6645 " [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[\n");
6646 new_reg = lra_create_new_reg (dst_mode, dst_reg, cl, NULL,
6647 "invariant inheritance");
6648 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
6649 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
6650 lra_reg_info[REGNO (new_reg)].restore_rtx = PATTERN (insn);
6651 start_sequence ();
6652 lra_emit_move (new_reg, dst_reg);
6653 new_insns = get_insns ();
6654 end_sequence ();
6655 lra_process_new_insns (curr_insn, NULL, new_insns,
6656 "Add invariant inheritance<-original");
6657 start_sequence ();
6658 lra_emit_move (SET_DEST (insn_set), new_reg);
6659 new_insns = get_insns ();
6660 end_sequence ();
6661 lra_process_new_insns (insn, NULL, new_insns,
6662 "Changing reload<-inheritance");
6663 lra_set_insn_deleted (insn);
6664 succ_p = true;
6665 if (lra_dump_file != NULL)
6666 {
6667 fprintf (lra_dump_file,
6668 " Invariant inheritance reuse change %d (bb%d):\n",
6669 REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
6670 dump_insn_slim (lra_dump_file, insn);
6671 fprintf (lra_dump_file,
6672 " ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]\n");
6673 }
6674 }
6675 }
6676 invariant_ptr->insn = curr_insn;
6677 return succ_p;
6678 }
6679
6680 /* Check only registers living at the current program point in the
6681 current EBB. */
6682 static bitmap_head live_regs;
6683
6684 /* Update live info in EBB given by its HEAD and TAIL insns after
6685 inheritance/split transformation. The function removes dead moves
6686 too. */
6687 static void
6688 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
6689 {
6690 unsigned int j;
6691 int i, regno;
6692 bool live_p;
6693 rtx_insn *prev_insn;
6694 rtx set;
6695 bool remove_p;
6696 basic_block last_bb, prev_bb, curr_bb;
6697 bitmap_iterator bi;
6698 struct lra_insn_reg *reg;
6699 edge e;
6700 edge_iterator ei;
6701
6702 last_bb = BLOCK_FOR_INSN (tail);
6703 prev_bb = NULL;
6704 for (curr_insn = tail;
6705 curr_insn != PREV_INSN (head);
6706 curr_insn = prev_insn)
6707 {
6708 prev_insn = PREV_INSN (curr_insn);
6709 /* We need to process empty blocks too. They contain
6710 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
6711 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
6712 continue;
6713 curr_bb = BLOCK_FOR_INSN (curr_insn);
6714 if (curr_bb != prev_bb)
6715 {
6716 if (prev_bb != NULL)
6717 {
6718 /* Update df_get_live_in (prev_bb): */
6719 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6720 if (bitmap_bit_p (&live_regs, j))
6721 bitmap_set_bit (df_get_live_in (prev_bb), j);
6722 else
6723 bitmap_clear_bit (df_get_live_in (prev_bb), j);
6724 }
6725 if (curr_bb != last_bb)
6726 {
6727 /* Update df_get_live_out (curr_bb): */
6728 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6729 {
6730 live_p = bitmap_bit_p (&live_regs, j);
6731 if (! live_p)
6732 FOR_EACH_EDGE (e, ei, curr_bb->succs)
6733 if (bitmap_bit_p (df_get_live_in (e->dest), j))
6734 {
6735 live_p = true;
6736 break;
6737 }
6738 if (live_p)
6739 bitmap_set_bit (df_get_live_out (curr_bb), j);
6740 else
6741 bitmap_clear_bit (df_get_live_out (curr_bb), j);
6742 }
6743 }
6744 prev_bb = curr_bb;
6745 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
6746 }
6747 if (! NONDEBUG_INSN_P (curr_insn))
6748 continue;
6749 curr_id = lra_get_insn_recog_data (curr_insn);
6750 curr_static_id = curr_id->insn_static_data;
6751 remove_p = false;
6752 if ((set = single_set (curr_insn)) != NULL_RTX
6753 && REG_P (SET_DEST (set))
6754 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
6755 && SET_DEST (set) != pic_offset_table_rtx
6756 && bitmap_bit_p (&check_only_regs, regno)
6757 && ! bitmap_bit_p (&live_regs, regno))
6758 remove_p = true;
6759 /* See which defined values die here. */
6760 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6761 if (reg->type == OP_OUT && ! reg->subreg_p)
6762 bitmap_clear_bit (&live_regs, reg->regno);
6763 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6764 if (reg->type == OP_OUT && ! reg->subreg_p)
6765 bitmap_clear_bit (&live_regs, reg->regno);
6766 if (curr_id->arg_hard_regs != NULL)
6767 /* Make clobbered argument hard registers die. */
6768 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6769 if (regno >= FIRST_PSEUDO_REGISTER)
6770 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
6771 /* Mark each used value as live. */
6772 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6773 if (reg->type != OP_OUT
6774 && bitmap_bit_p (&check_only_regs, reg->regno))
6775 bitmap_set_bit (&live_regs, reg->regno);
6776 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6777 if (reg->type != OP_OUT
6778 && bitmap_bit_p (&check_only_regs, reg->regno))
6779 bitmap_set_bit (&live_regs, reg->regno);
6780 if (curr_id->arg_hard_regs != NULL)
6781 /* Make used argument hard registers live. */
6782 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6783 if (regno < FIRST_PSEUDO_REGISTER
6784 && bitmap_bit_p (&check_only_regs, regno))
6785 bitmap_set_bit (&live_regs, regno);
6786 /* It is quite important to remove dead move insns because it
6787 means removing dead store. We don't need to process them for
6788 constraints. */
6789 if (remove_p)
6790 {
6791 if (lra_dump_file != NULL)
6792 {
6793 fprintf (lra_dump_file, " Removing dead insn:\n ");
6794 dump_insn_slim (lra_dump_file, curr_insn);
6795 }
6796 lra_set_insn_deleted (curr_insn);
6797 }
6798 }
6799 }
6800
6801 /* The structure describes info to do an inheritance for the current
6802 insn. We need to collect such info first before doing the
6803 transformations because the transformations change the insn
6804 internal representation. */
6805 struct to_inherit
6806 {
6807 /* Original regno. */
6808 int regno;
6809 /* Subsequent insns which can inherit original reg value. */
6810 rtx insns;
6811 };
6812
6813 /* Array containing all info for doing inheritance from the current
6814 insn. */
6815 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
6816
6817 /* Number elements in the previous array. */
6818 static int to_inherit_num;
6819
6820 /* Add inheritance info REGNO and INSNS. Their meaning is described in
6821 structure to_inherit. */
6822 static void
6823 add_to_inherit (int regno, rtx insns)
6824 {
6825 int i;
6826
6827 for (i = 0; i < to_inherit_num; i++)
6828 if (to_inherit[i].regno == regno)
6829 return;
6830 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
6831 to_inherit[to_inherit_num].regno = regno;
6832 to_inherit[to_inherit_num++].insns = insns;
6833 }
6834
6835 /* Return the last non-debug insn in basic block BB, or the block begin
6836 note if none. */
6837 static rtx_insn *
6838 get_last_insertion_point (basic_block bb)
6839 {
6840 rtx_insn *insn;
6841
6842 FOR_BB_INSNS_REVERSE (bb, insn)
6843 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
6844 return insn;
6845 gcc_unreachable ();
6846 }
6847
6848 /* Set up RES by registers living on edges FROM except the edge (FROM,
6849 TO) or by registers set up in a jump insn in BB FROM. */
6850 static void
6851 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
6852 {
6853 rtx_insn *last;
6854 struct lra_insn_reg *reg;
6855 edge e;
6856 edge_iterator ei;
6857
6858 lra_assert (to != NULL);
6859 bitmap_clear (res);
6860 FOR_EACH_EDGE (e, ei, from->succs)
6861 if (e->dest != to)
6862 bitmap_ior_into (res, df_get_live_in (e->dest));
6863 last = get_last_insertion_point (from);
6864 if (! JUMP_P (last))
6865 return;
6866 curr_id = lra_get_insn_recog_data (last);
6867 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6868 if (reg->type != OP_IN)
6869 bitmap_set_bit (res, reg->regno);
6870 }
6871
6872 /* Used as a temporary results of some bitmap calculations. */
6873 static bitmap_head temp_bitmap;
6874
6875 /* We split for reloads of small class of hard regs. The following
6876 defines how many hard regs the class should have to be qualified as
6877 small. The code is mostly oriented to x86/x86-64 architecture
6878 where some insns need to use only specific register or pair of
6879 registers and these register can live in RTL explicitly, e.g. for
6880 parameter passing. */
6881 static const int max_small_class_regs_num = 2;
6882
6883 /* Do inheritance/split transformations in EBB starting with HEAD and
6884 finishing on TAIL. We process EBB insns in the reverse order.
6885 Return true if we did any inheritance/split transformation in the
6886 EBB.
6887
6888 We should avoid excessive splitting which results in worse code
6889 because of inaccurate cost calculations for spilling new split
6890 pseudos in such case. To achieve this we do splitting only if
6891 register pressure is high in given basic block and there are reload
6892 pseudos requiring hard registers. We could do more register
6893 pressure calculations at any given program point to avoid necessary
6894 splitting even more but it is to expensive and the current approach
6895 works well enough. */
6896 static bool
6897 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
6898 {
6899 int i, src_regno, dst_regno, nregs;
6900 bool change_p, succ_p, update_reloads_num_p;
6901 rtx_insn *prev_insn, *last_insn;
6902 rtx next_usage_insns, curr_set;
6903 enum reg_class cl;
6904 struct lra_insn_reg *reg;
6905 basic_block last_processed_bb, curr_bb = NULL;
6906 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
6907 bitmap to_process;
6908 unsigned int j;
6909 bitmap_iterator bi;
6910 bool head_p, after_p;
6911
6912 change_p = false;
6913 curr_usage_insns_check++;
6914 clear_invariants ();
6915 reloads_num = calls_num = 0;
6916 for (unsigned int i = 0; i < NUM_ABI_IDS; ++i)
6917 last_call_for_abi[i] = 0;
6918 CLEAR_HARD_REG_SET (full_and_partial_call_clobbers);
6919 bitmap_clear (&check_only_regs);
6920 bitmap_clear (&invalid_invariant_regs);
6921 last_processed_bb = NULL;
6922 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6923 live_hard_regs = eliminable_regset | lra_no_alloc_regs;
6924 /* We don't process new insns generated in the loop. */
6925 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
6926 {
6927 prev_insn = PREV_INSN (curr_insn);
6928 if (BLOCK_FOR_INSN (curr_insn) != NULL)
6929 curr_bb = BLOCK_FOR_INSN (curr_insn);
6930 if (last_processed_bb != curr_bb)
6931 {
6932 /* We are at the end of BB. Add qualified living
6933 pseudos for potential splitting. */
6934 to_process = df_get_live_out (curr_bb);
6935 if (last_processed_bb != NULL)
6936 {
6937 /* We are somewhere in the middle of EBB. */
6938 get_live_on_other_edges (curr_bb, last_processed_bb,
6939 &temp_bitmap);
6940 to_process = &temp_bitmap;
6941 }
6942 last_processed_bb = curr_bb;
6943 last_insn = get_last_insertion_point (curr_bb);
6944 after_p = (! JUMP_P (last_insn)
6945 && (! CALL_P (last_insn)
6946 || (find_reg_note (last_insn,
6947 REG_NORETURN, NULL_RTX) == NULL_RTX
6948 && ! SIBLING_CALL_P (last_insn))));
6949 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6950 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
6951 {
6952 if ((int) j >= lra_constraint_new_regno_start)
6953 break;
6954 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
6955 {
6956 if (j < FIRST_PSEUDO_REGISTER)
6957 SET_HARD_REG_BIT (live_hard_regs, j);
6958 else
6959 add_to_hard_reg_set (&live_hard_regs,
6960 PSEUDO_REGNO_MODE (j),
6961 reg_renumber[j]);
6962 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
6963 }
6964 }
6965 }
6966 src_regno = dst_regno = -1;
6967 curr_set = single_set (curr_insn);
6968 if (curr_set != NULL_RTX && REG_P (SET_DEST (curr_set)))
6969 dst_regno = REGNO (SET_DEST (curr_set));
6970 if (curr_set != NULL_RTX && REG_P (SET_SRC (curr_set)))
6971 src_regno = REGNO (SET_SRC (curr_set));
6972 update_reloads_num_p = true;
6973 if (src_regno < lra_constraint_new_regno_start
6974 && src_regno >= FIRST_PSEUDO_REGISTER
6975 && reg_renumber[src_regno] < 0
6976 && dst_regno >= lra_constraint_new_regno_start
6977 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
6978 {
6979 /* 'reload_pseudo <- original_pseudo'. */
6980 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6981 reloads_num++;
6982 update_reloads_num_p = false;
6983 succ_p = false;
6984 if (usage_insns[src_regno].check == curr_usage_insns_check
6985 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
6986 succ_p = inherit_reload_reg (false, src_regno, cl,
6987 curr_insn, next_usage_insns);
6988 if (succ_p)
6989 change_p = true;
6990 else
6991 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6992 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6993 potential_reload_hard_regs |= reg_class_contents[cl];
6994 }
6995 else if (src_regno < 0
6996 && dst_regno >= lra_constraint_new_regno_start
6997 && invariant_p (SET_SRC (curr_set))
6998 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS
6999 && ! bitmap_bit_p (&invalid_invariant_regs, dst_regno)
7000 && ! bitmap_bit_p (&invalid_invariant_regs,
7001 ORIGINAL_REGNO(regno_reg_rtx[dst_regno])))
7002 {
7003 /* 'reload_pseudo <- invariant'. */
7004 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
7005 reloads_num++;
7006 update_reloads_num_p = false;
7007 if (process_invariant_for_inheritance (SET_DEST (curr_set), SET_SRC (curr_set)))
7008 change_p = true;
7009 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
7010 potential_reload_hard_regs |= reg_class_contents[cl];
7011 }
7012 else if (src_regno >= lra_constraint_new_regno_start
7013 && dst_regno < lra_constraint_new_regno_start
7014 && dst_regno >= FIRST_PSEUDO_REGISTER
7015 && reg_renumber[dst_regno] < 0
7016 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
7017 && usage_insns[dst_regno].check == curr_usage_insns_check
7018 && (next_usage_insns
7019 = usage_insns[dst_regno].insns) != NULL_RTX)
7020 {
7021 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
7022 reloads_num++;
7023 update_reloads_num_p = false;
7024 /* 'original_pseudo <- reload_pseudo'. */
7025 if (! JUMP_P (curr_insn)
7026 && inherit_reload_reg (true, dst_regno, cl,
7027 curr_insn, next_usage_insns))
7028 change_p = true;
7029 /* Invalidate. */
7030 usage_insns[dst_regno].check = 0;
7031 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
7032 potential_reload_hard_regs |= reg_class_contents[cl];
7033 }
7034 else if (INSN_P (curr_insn))
7035 {
7036 int iter;
7037 int max_uid = get_max_uid ();
7038
7039 curr_id = lra_get_insn_recog_data (curr_insn);
7040 curr_static_id = curr_id->insn_static_data;
7041 to_inherit_num = 0;
7042 /* Process insn definitions. */
7043 for (iter = 0; iter < 2; iter++)
7044 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
7045 reg != NULL;
7046 reg = reg->next)
7047 if (reg->type != OP_IN
7048 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
7049 {
7050 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
7051 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
7052 && usage_insns[dst_regno].check == curr_usage_insns_check
7053 && (next_usage_insns
7054 = usage_insns[dst_regno].insns) != NULL_RTX)
7055 {
7056 struct lra_insn_reg *r;
7057
7058 for (r = curr_id->regs; r != NULL; r = r->next)
7059 if (r->type != OP_OUT && r->regno == dst_regno)
7060 break;
7061 /* Don't do inheritance if the pseudo is also
7062 used in the insn. */
7063 if (r == NULL)
7064 /* We cannot do inheritance right now
7065 because the current insn reg info (chain
7066 regs) can change after that. */
7067 add_to_inherit (dst_regno, next_usage_insns);
7068 }
7069 /* We cannot process one reg twice here because of
7070 usage_insns invalidation. */
7071 if ((dst_regno < FIRST_PSEUDO_REGISTER
7072 || reg_renumber[dst_regno] >= 0)
7073 && ! reg->subreg_p && reg->type != OP_IN)
7074 {
7075 HARD_REG_SET s;
7076
7077 if (split_if_necessary (dst_regno, reg->biggest_mode,
7078 potential_reload_hard_regs,
7079 false, curr_insn, max_uid))
7080 change_p = true;
7081 CLEAR_HARD_REG_SET (s);
7082 if (dst_regno < FIRST_PSEUDO_REGISTER)
7083 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
7084 else
7085 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
7086 reg_renumber[dst_regno]);
7087 live_hard_regs &= ~s;
7088 potential_reload_hard_regs &= ~s;
7089 }
7090 /* We should invalidate potential inheritance or
7091 splitting for the current insn usages to the next
7092 usage insns (see code below) as the output pseudo
7093 prevents this. */
7094 if ((dst_regno >= FIRST_PSEUDO_REGISTER
7095 && reg_renumber[dst_regno] < 0)
7096 || (reg->type == OP_OUT && ! reg->subreg_p
7097 && (dst_regno < FIRST_PSEUDO_REGISTER
7098 || reg_renumber[dst_regno] >= 0)))
7099 {
7100 /* Invalidate and mark definitions. */
7101 if (dst_regno >= FIRST_PSEUDO_REGISTER)
7102 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
7103 else
7104 {
7105 nregs = hard_regno_nregs (dst_regno,
7106 reg->biggest_mode);
7107 for (i = 0; i < nregs; i++)
7108 usage_insns[dst_regno + i].check
7109 = -(int) INSN_UID (curr_insn);
7110 }
7111 }
7112 }
7113 /* Process clobbered call regs. */
7114 if (curr_id->arg_hard_regs != NULL)
7115 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
7116 if (dst_regno >= FIRST_PSEUDO_REGISTER)
7117 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
7118 = -(int) INSN_UID (curr_insn);
7119 if (! JUMP_P (curr_insn))
7120 for (i = 0; i < to_inherit_num; i++)
7121 if (inherit_reload_reg (true, to_inherit[i].regno,
7122 ALL_REGS, curr_insn,
7123 to_inherit[i].insns))
7124 change_p = true;
7125 if (CALL_P (curr_insn))
7126 {
7127 rtx cheap, pat, dest;
7128 rtx_insn *restore;
7129 int regno, hard_regno;
7130
7131 calls_num++;
7132 function_abi callee_abi = insn_callee_abi (curr_insn);
7133 last_call_for_abi[callee_abi.id ()] = calls_num;
7134 full_and_partial_call_clobbers
7135 |= callee_abi.full_and_partial_reg_clobbers ();
7136 first_call_insn = curr_insn;
7137 if ((cheap = find_reg_note (curr_insn,
7138 REG_RETURNED, NULL_RTX)) != NULL_RTX
7139 && ((cheap = XEXP (cheap, 0)), true)
7140 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
7141 && (hard_regno = reg_renumber[regno]) >= 0
7142 && usage_insns[regno].check == curr_usage_insns_check
7143 /* If there are pending saves/restores, the
7144 optimization is not worth. */
7145 && usage_insns[regno].calls_num == calls_num - 1
7146 && callee_abi.clobbers_reg_p (GET_MODE (cheap), hard_regno))
7147 {
7148 /* Restore the pseudo from the call result as
7149 REG_RETURNED note says that the pseudo value is
7150 in the call result and the pseudo is an argument
7151 of the call. */
7152 pat = PATTERN (curr_insn);
7153 if (GET_CODE (pat) == PARALLEL)
7154 pat = XVECEXP (pat, 0, 0);
7155 dest = SET_DEST (pat);
7156 /* For multiple return values dest is PARALLEL.
7157 Currently we handle only single return value case. */
7158 if (REG_P (dest))
7159 {
7160 start_sequence ();
7161 emit_move_insn (cheap, copy_rtx (dest));
7162 restore = get_insns ();
7163 end_sequence ();
7164 lra_process_new_insns (curr_insn, NULL, restore,
7165 "Inserting call parameter restore");
7166 /* We don't need to save/restore of the pseudo from
7167 this call. */
7168 usage_insns[regno].calls_num = calls_num;
7169 remove_from_hard_reg_set
7170 (&full_and_partial_call_clobbers,
7171 GET_MODE (cheap), hard_regno);
7172 bitmap_set_bit (&check_only_regs, regno);
7173 }
7174 }
7175 }
7176 to_inherit_num = 0;
7177 /* Process insn usages. */
7178 for (iter = 0; iter < 2; iter++)
7179 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
7180 reg != NULL;
7181 reg = reg->next)
7182 if ((reg->type != OP_OUT
7183 || (reg->type == OP_OUT && reg->subreg_p))
7184 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
7185 {
7186 if (src_regno >= FIRST_PSEUDO_REGISTER
7187 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
7188 {
7189 if (usage_insns[src_regno].check == curr_usage_insns_check
7190 && (next_usage_insns
7191 = usage_insns[src_regno].insns) != NULL_RTX
7192 && NONDEBUG_INSN_P (curr_insn))
7193 add_to_inherit (src_regno, next_usage_insns);
7194 else if (usage_insns[src_regno].check
7195 != -(int) INSN_UID (curr_insn))
7196 /* Add usages but only if the reg is not set up
7197 in the same insn. */
7198 add_next_usage_insn (src_regno, curr_insn, reloads_num);
7199 }
7200 else if (src_regno < FIRST_PSEUDO_REGISTER
7201 || reg_renumber[src_regno] >= 0)
7202 {
7203 bool before_p;
7204 rtx_insn *use_insn = curr_insn;
7205 rtx_insn *prev_insn = PREV_INSN (curr_insn);
7206
7207 before_p = (JUMP_P (curr_insn)
7208 || (CALL_P (curr_insn) && reg->type == OP_IN));
7209 if (NONDEBUG_INSN_P (curr_insn)
7210 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
7211 && split_if_necessary (src_regno, reg->biggest_mode,
7212 potential_reload_hard_regs,
7213 before_p, curr_insn, max_uid))
7214 {
7215 if (reg->subreg_p)
7216 check_and_force_assignment_correctness_p = true;
7217 change_p = true;
7218 /* Invalidate. */
7219 usage_insns[src_regno].check = 0;
7220 if (before_p && PREV_INSN (curr_insn) != prev_insn)
7221 use_insn = PREV_INSN (curr_insn);
7222 }
7223 if (NONDEBUG_INSN_P (curr_insn))
7224 {
7225 if (src_regno < FIRST_PSEUDO_REGISTER)
7226 add_to_hard_reg_set (&live_hard_regs,
7227 reg->biggest_mode, src_regno);
7228 else
7229 add_to_hard_reg_set (&live_hard_regs,
7230 PSEUDO_REGNO_MODE (src_regno),
7231 reg_renumber[src_regno]);
7232 }
7233 if (src_regno >= FIRST_PSEUDO_REGISTER)
7234 add_next_usage_insn (src_regno, use_insn, reloads_num);
7235 else
7236 {
7237 for (i = 0; i < hard_regno_nregs (src_regno, reg->biggest_mode); i++)
7238 add_next_usage_insn (src_regno + i, use_insn, reloads_num);
7239 }
7240 }
7241 }
7242 /* Process used call regs. */
7243 if (curr_id->arg_hard_regs != NULL)
7244 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
7245 if (src_regno < FIRST_PSEUDO_REGISTER)
7246 {
7247 SET_HARD_REG_BIT (live_hard_regs, src_regno);
7248 add_next_usage_insn (src_regno, curr_insn, reloads_num);
7249 }
7250 for (i = 0; i < to_inherit_num; i++)
7251 {
7252 src_regno = to_inherit[i].regno;
7253 if (inherit_reload_reg (false, src_regno, ALL_REGS,
7254 curr_insn, to_inherit[i].insns))
7255 change_p = true;
7256 else
7257 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
7258 }
7259 }
7260 if (update_reloads_num_p
7261 && NONDEBUG_INSN_P (curr_insn) && curr_set != NULL_RTX)
7262 {
7263 int regno = -1;
7264 if ((REG_P (SET_DEST (curr_set))
7265 && (regno = REGNO (SET_DEST (curr_set))) >= lra_constraint_new_regno_start
7266 && reg_renumber[regno] < 0
7267 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
7268 || (REG_P (SET_SRC (curr_set))
7269 && (regno = REGNO (SET_SRC (curr_set))) >= lra_constraint_new_regno_start
7270 && reg_renumber[regno] < 0
7271 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
7272 {
7273 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
7274 reloads_num++;
7275 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
7276 potential_reload_hard_regs |= reg_class_contents[cl];
7277 }
7278 }
7279 if (NONDEBUG_INSN_P (curr_insn))
7280 {
7281 int regno;
7282
7283 /* Invalidate invariants with changed regs. */
7284 curr_id = lra_get_insn_recog_data (curr_insn);
7285 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
7286 if (reg->type != OP_IN)
7287 {
7288 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
7289 bitmap_set_bit (&invalid_invariant_regs,
7290 ORIGINAL_REGNO (regno_reg_rtx[reg->regno]));
7291 }
7292 curr_static_id = curr_id->insn_static_data;
7293 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
7294 if (reg->type != OP_IN)
7295 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
7296 if (curr_id->arg_hard_regs != NULL)
7297 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
7298 if (regno >= FIRST_PSEUDO_REGISTER)
7299 bitmap_set_bit (&invalid_invariant_regs,
7300 regno - FIRST_PSEUDO_REGISTER);
7301 }
7302 /* We reached the start of the current basic block. */
7303 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
7304 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
7305 {
7306 /* We reached the beginning of the current block -- do
7307 rest of spliting in the current BB. */
7308 to_process = df_get_live_in (curr_bb);
7309 if (BLOCK_FOR_INSN (head) != curr_bb)
7310 {
7311 /* We are somewhere in the middle of EBB. */
7312 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
7313 curr_bb, &temp_bitmap);
7314 to_process = &temp_bitmap;
7315 }
7316 head_p = true;
7317 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
7318 {
7319 if ((int) j >= lra_constraint_new_regno_start)
7320 break;
7321 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
7322 && usage_insns[j].check == curr_usage_insns_check
7323 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
7324 {
7325 if (need_for_split_p (potential_reload_hard_regs, j))
7326 {
7327 if (lra_dump_file != NULL && head_p)
7328 {
7329 fprintf (lra_dump_file,
7330 " ----------------------------------\n");
7331 head_p = false;
7332 }
7333 if (split_reg (false, j, bb_note (curr_bb),
7334 next_usage_insns, NULL))
7335 change_p = true;
7336 }
7337 usage_insns[j].check = 0;
7338 }
7339 }
7340 }
7341 }
7342 first_call_insn = NULL;
7343 return change_p;
7344 }
7345
7346 /* This value affects EBB forming. If probability of edge from EBB to
7347 a BB is not greater than the following value, we don't add the BB
7348 to EBB. */
7349 #define EBB_PROBABILITY_CUTOFF \
7350 ((REG_BR_PROB_BASE * param_lra_inheritance_ebb_probability_cutoff) / 100)
7351
7352 /* Current number of inheritance/split iteration. */
7353 int lra_inheritance_iter;
7354
7355 /* Entry function for inheritance/split pass. */
7356 void
7357 lra_inheritance (void)
7358 {
7359 int i;
7360 basic_block bb, start_bb;
7361 edge e;
7362
7363 lra_inheritance_iter++;
7364 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
7365 return;
7366 timevar_push (TV_LRA_INHERITANCE);
7367 if (lra_dump_file != NULL)
7368 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
7369 lra_inheritance_iter);
7370 curr_usage_insns_check = 0;
7371 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
7372 for (i = 0; i < lra_constraint_new_regno_start; i++)
7373 usage_insns[i].check = 0;
7374 bitmap_initialize (&check_only_regs, &reg_obstack);
7375 bitmap_initialize (&invalid_invariant_regs, &reg_obstack);
7376 bitmap_initialize (&live_regs, &reg_obstack);
7377 bitmap_initialize (&temp_bitmap, &reg_obstack);
7378 bitmap_initialize (&ebb_global_regs, &reg_obstack);
7379 FOR_EACH_BB_FN (bb, cfun)
7380 {
7381 start_bb = bb;
7382 if (lra_dump_file != NULL)
7383 fprintf (lra_dump_file, "EBB");
7384 /* Form a EBB starting with BB. */
7385 bitmap_clear (&ebb_global_regs);
7386 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
7387 for (;;)
7388 {
7389 if (lra_dump_file != NULL)
7390 fprintf (lra_dump_file, " %d", bb->index);
7391 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
7392 || LABEL_P (BB_HEAD (bb->next_bb)))
7393 break;
7394 e = find_fallthru_edge (bb->succs);
7395 if (! e)
7396 break;
7397 if (e->probability.initialized_p ()
7398 && e->probability.to_reg_br_prob_base () < EBB_PROBABILITY_CUTOFF)
7399 break;
7400 bb = bb->next_bb;
7401 }
7402 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
7403 if (lra_dump_file != NULL)
7404 fprintf (lra_dump_file, "\n");
7405 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
7406 /* Remember that the EBB head and tail can change in
7407 inherit_in_ebb. */
7408 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
7409 }
7410 bitmap_release (&ebb_global_regs);
7411 bitmap_release (&temp_bitmap);
7412 bitmap_release (&live_regs);
7413 bitmap_release (&invalid_invariant_regs);
7414 bitmap_release (&check_only_regs);
7415 free (usage_insns);
7416 lra_dump_insns_if_possible ("func after inheritance");
7417 timevar_pop (TV_LRA_INHERITANCE);
7418 }
7419
7420
7421
7422 /* This page contains code to undo failed inheritance/split
7423 transformations. */
7424
7425 /* Current number of iteration undoing inheritance/split. */
7426 int lra_undo_inheritance_iter;
7427
7428 /* Fix BB live info LIVE after removing pseudos created on pass doing
7429 inheritance/split which are REMOVED_PSEUDOS. */
7430 static void
7431 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
7432 {
7433 unsigned int regno;
7434 bitmap_iterator bi;
7435
7436 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
7437 if (bitmap_clear_bit (live, regno)
7438 && REG_P (lra_reg_info[regno].restore_rtx))
7439 bitmap_set_bit (live, REGNO (lra_reg_info[regno].restore_rtx));
7440 }
7441
7442 /* Return regno of the (subreg of) REG. Otherwise, return a negative
7443 number. */
7444 static int
7445 get_regno (rtx reg)
7446 {
7447 if (GET_CODE (reg) == SUBREG)
7448 reg = SUBREG_REG (reg);
7449 if (REG_P (reg))
7450 return REGNO (reg);
7451 return -1;
7452 }
7453
7454 /* Delete a move INSN with destination reg DREGNO and a previous
7455 clobber insn with the same regno. The inheritance/split code can
7456 generate moves with preceding clobber and when we delete such moves
7457 we should delete the clobber insn too to keep the correct life
7458 info. */
7459 static void
7460 delete_move_and_clobber (rtx_insn *insn, int dregno)
7461 {
7462 rtx_insn *prev_insn = PREV_INSN (insn);
7463
7464 lra_set_insn_deleted (insn);
7465 lra_assert (dregno >= 0);
7466 if (prev_insn != NULL && NONDEBUG_INSN_P (prev_insn)
7467 && GET_CODE (PATTERN (prev_insn)) == CLOBBER
7468 && dregno == get_regno (XEXP (PATTERN (prev_insn), 0)))
7469 lra_set_insn_deleted (prev_insn);
7470 }
7471
7472 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
7473 return true if we did any change. The undo transformations for
7474 inheritance looks like
7475 i <- i2
7476 p <- i => p <- i2
7477 or removing
7478 p <- i, i <- p, and i <- i3
7479 where p is original pseudo from which inheritance pseudo i was
7480 created, i and i3 are removed inheritance pseudos, i2 is another
7481 not removed inheritance pseudo. All split pseudos or other
7482 occurrences of removed inheritance pseudos are changed on the
7483 corresponding original pseudos.
7484
7485 The function also schedules insns changed and created during
7486 inheritance/split pass for processing by the subsequent constraint
7487 pass. */
7488 static bool
7489 remove_inheritance_pseudos (bitmap remove_pseudos)
7490 {
7491 basic_block bb;
7492 int regno, sregno, prev_sregno, dregno;
7493 rtx restore_rtx;
7494 rtx set, prev_set;
7495 rtx_insn *prev_insn;
7496 bool change_p, done_p;
7497
7498 change_p = ! bitmap_empty_p (remove_pseudos);
7499 /* We cannot finish the function right away if CHANGE_P is true
7500 because we need to marks insns affected by previous
7501 inheritance/split pass for processing by the subsequent
7502 constraint pass. */
7503 FOR_EACH_BB_FN (bb, cfun)
7504 {
7505 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
7506 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
7507 FOR_BB_INSNS_REVERSE (bb, curr_insn)
7508 {
7509 if (! INSN_P (curr_insn))
7510 continue;
7511 done_p = false;
7512 sregno = dregno = -1;
7513 if (change_p && NONDEBUG_INSN_P (curr_insn)
7514 && (set = single_set (curr_insn)) != NULL_RTX)
7515 {
7516 dregno = get_regno (SET_DEST (set));
7517 sregno = get_regno (SET_SRC (set));
7518 }
7519
7520 if (sregno >= 0 && dregno >= 0)
7521 {
7522 if (bitmap_bit_p (remove_pseudos, dregno)
7523 && ! REG_P (lra_reg_info[dregno].restore_rtx))
7524 {
7525 /* invariant inheritance pseudo <- original pseudo */
7526 if (lra_dump_file != NULL)
7527 {
7528 fprintf (lra_dump_file, " Removing invariant inheritance:\n");
7529 dump_insn_slim (lra_dump_file, curr_insn);
7530 fprintf (lra_dump_file, "\n");
7531 }
7532 delete_move_and_clobber (curr_insn, dregno);
7533 done_p = true;
7534 }
7535 else if (bitmap_bit_p (remove_pseudos, sregno)
7536 && ! REG_P (lra_reg_info[sregno].restore_rtx))
7537 {
7538 /* reload pseudo <- invariant inheritance pseudo */
7539 start_sequence ();
7540 /* We cannot just change the source. It might be
7541 an insn different from the move. */
7542 emit_insn (lra_reg_info[sregno].restore_rtx);
7543 rtx_insn *new_insns = get_insns ();
7544 end_sequence ();
7545 lra_assert (single_set (new_insns) != NULL
7546 && SET_DEST (set) == SET_DEST (single_set (new_insns)));
7547 lra_process_new_insns (curr_insn, NULL, new_insns,
7548 "Changing reload<-invariant inheritance");
7549 delete_move_and_clobber (curr_insn, dregno);
7550 done_p = true;
7551 }
7552 else if ((bitmap_bit_p (remove_pseudos, sregno)
7553 && (get_regno (lra_reg_info[sregno].restore_rtx) == dregno
7554 || (bitmap_bit_p (remove_pseudos, dregno)
7555 && get_regno (lra_reg_info[sregno].restore_rtx) >= 0
7556 && (get_regno (lra_reg_info[sregno].restore_rtx)
7557 == get_regno (lra_reg_info[dregno].restore_rtx)))))
7558 || (bitmap_bit_p (remove_pseudos, dregno)
7559 && get_regno (lra_reg_info[dregno].restore_rtx) == sregno))
7560 /* One of the following cases:
7561 original <- removed inheritance pseudo
7562 removed inherit pseudo <- another removed inherit pseudo
7563 removed inherit pseudo <- original pseudo
7564 Or
7565 removed_split_pseudo <- original_reg
7566 original_reg <- removed_split_pseudo */
7567 {
7568 if (lra_dump_file != NULL)
7569 {
7570 fprintf (lra_dump_file, " Removing %s:\n",
7571 bitmap_bit_p (&lra_split_regs, sregno)
7572 || bitmap_bit_p (&lra_split_regs, dregno)
7573 ? "split" : "inheritance");
7574 dump_insn_slim (lra_dump_file, curr_insn);
7575 }
7576 delete_move_and_clobber (curr_insn, dregno);
7577 done_p = true;
7578 }
7579 else if (bitmap_bit_p (remove_pseudos, sregno)
7580 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
7581 {
7582 /* Search the following pattern:
7583 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
7584 original_pseudo <- inherit_or_split_pseudo1
7585 where the 2nd insn is the current insn and
7586 inherit_or_split_pseudo2 is not removed. If it is found,
7587 change the current insn onto:
7588 original_pseudo <- inherit_or_split_pseudo2. */
7589 for (prev_insn = PREV_INSN (curr_insn);
7590 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
7591 prev_insn = PREV_INSN (prev_insn))
7592 ;
7593 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
7594 && (prev_set = single_set (prev_insn)) != NULL_RTX
7595 /* There should be no subregs in insn we are
7596 searching because only the original reg might
7597 be in subreg when we changed the mode of
7598 load/store for splitting. */
7599 && REG_P (SET_DEST (prev_set))
7600 && REG_P (SET_SRC (prev_set))
7601 && (int) REGNO (SET_DEST (prev_set)) == sregno
7602 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
7603 >= FIRST_PSEUDO_REGISTER)
7604 && (lra_reg_info[prev_sregno].restore_rtx == NULL_RTX
7605 ||
7606 /* As we consider chain of inheritance or
7607 splitting described in above comment we should
7608 check that sregno and prev_sregno were
7609 inheritance/split pseudos created from the
7610 same original regno. */
7611 (get_regno (lra_reg_info[sregno].restore_rtx) >= 0
7612 && (get_regno (lra_reg_info[sregno].restore_rtx)
7613 == get_regno (lra_reg_info[prev_sregno].restore_rtx))))
7614 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
7615 {
7616 int restore_regno = get_regno (lra_reg_info[sregno].restore_rtx);
7617 if (restore_regno < 0)
7618 restore_regno = prev_sregno;
7619 lra_assert (GET_MODE (SET_SRC (prev_set))
7620 == GET_MODE (regno_reg_rtx[restore_regno]));
7621 /* Although we have a single set, the insn can
7622 contain more one sregno register occurrence
7623 as a source. Change all occurrences. */
7624 lra_substitute_pseudo_within_insn (curr_insn, sregno,
7625 regno_reg_rtx[restore_regno],
7626 false);
7627 /* As we are finishing with processing the insn
7628 here, check the destination too as it might
7629 inheritance pseudo for another pseudo. */
7630 if (bitmap_bit_p (remove_pseudos, dregno)
7631 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
7632 && (restore_rtx
7633 = lra_reg_info[dregno].restore_rtx) != NULL_RTX)
7634 {
7635 if (GET_CODE (SET_DEST (set)) == SUBREG)
7636 SUBREG_REG (SET_DEST (set)) = restore_rtx;
7637 else
7638 SET_DEST (set) = restore_rtx;
7639 }
7640 lra_push_insn_and_update_insn_regno_info (curr_insn);
7641 lra_set_used_insn_alternative_by_uid
7642 (INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
7643 done_p = true;
7644 if (lra_dump_file != NULL)
7645 {
7646 fprintf (lra_dump_file, " Change reload insn:\n");
7647 dump_insn_slim (lra_dump_file, curr_insn);
7648 }
7649 }
7650 }
7651 }
7652 if (! done_p)
7653 {
7654 struct lra_insn_reg *reg;
7655 bool restored_regs_p = false;
7656 bool kept_regs_p = false;
7657
7658 curr_id = lra_get_insn_recog_data (curr_insn);
7659 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
7660 {
7661 regno = reg->regno;
7662 restore_rtx = lra_reg_info[regno].restore_rtx;
7663 if (restore_rtx != NULL_RTX)
7664 {
7665 if (change_p && bitmap_bit_p (remove_pseudos, regno))
7666 {
7667 lra_substitute_pseudo_within_insn
7668 (curr_insn, regno, restore_rtx, false);
7669 restored_regs_p = true;
7670 }
7671 else
7672 kept_regs_p = true;
7673 }
7674 }
7675 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
7676 {
7677 /* The instruction has changed since the previous
7678 constraints pass. */
7679 lra_push_insn_and_update_insn_regno_info (curr_insn);
7680 lra_set_used_insn_alternative_by_uid
7681 (INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
7682 }
7683 else if (restored_regs_p)
7684 /* The instruction has been restored to the form that
7685 it had during the previous constraints pass. */
7686 lra_update_insn_regno_info (curr_insn);
7687 if (restored_regs_p && lra_dump_file != NULL)
7688 {
7689 fprintf (lra_dump_file, " Insn after restoring regs:\n");
7690 dump_insn_slim (lra_dump_file, curr_insn);
7691 }
7692 }
7693 }
7694 }
7695 return change_p;
7696 }
7697
7698 /* If optional reload pseudos failed to get a hard register or was not
7699 inherited, it is better to remove optional reloads. We do this
7700 transformation after undoing inheritance to figure out necessity to
7701 remove optional reloads easier. Return true if we do any
7702 change. */
7703 static bool
7704 undo_optional_reloads (void)
7705 {
7706 bool change_p, keep_p;
7707 unsigned int regno, uid;
7708 bitmap_iterator bi, bi2;
7709 rtx_insn *insn;
7710 rtx set, src, dest;
7711 auto_bitmap removed_optional_reload_pseudos (&reg_obstack);
7712
7713 bitmap_copy (removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
7714 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7715 {
7716 keep_p = false;
7717 /* Keep optional reloads from previous subpasses. */
7718 if (lra_reg_info[regno].restore_rtx == NULL_RTX
7719 /* If the original pseudo changed its allocation, just
7720 removing the optional pseudo is dangerous as the original
7721 pseudo will have longer live range. */
7722 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] >= 0)
7723 keep_p = true;
7724 else if (reg_renumber[regno] >= 0)
7725 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
7726 {
7727 insn = lra_insn_recog_data[uid]->insn;
7728 if ((set = single_set (insn)) == NULL_RTX)
7729 continue;
7730 src = SET_SRC (set);
7731 dest = SET_DEST (set);
7732 if ((! REG_P (src) && ! SUBREG_P (src))
7733 || (! REG_P (dest) && ! SUBREG_P (dest)))
7734 continue;
7735 if (get_regno (dest) == (int) regno
7736 /* Ignore insn for optional reloads itself. */
7737 && (get_regno (lra_reg_info[regno].restore_rtx)
7738 != get_regno (src))
7739 /* Check only inheritance on last inheritance pass. */
7740 && get_regno (src) >= new_regno_start
7741 /* Check that the optional reload was inherited. */
7742 && bitmap_bit_p (&lra_inheritance_pseudos, get_regno (src)))
7743 {
7744 keep_p = true;
7745 break;
7746 }
7747 }
7748 if (keep_p)
7749 {
7750 bitmap_clear_bit (removed_optional_reload_pseudos, regno);
7751 if (lra_dump_file != NULL)
7752 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
7753 }
7754 }
7755 change_p = ! bitmap_empty_p (removed_optional_reload_pseudos);
7756 auto_bitmap insn_bitmap (&reg_obstack);
7757 EXECUTE_IF_SET_IN_BITMAP (removed_optional_reload_pseudos, 0, regno, bi)
7758 {
7759 if (lra_dump_file != NULL)
7760 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
7761 bitmap_copy (insn_bitmap, &lra_reg_info[regno].insn_bitmap);
7762 EXECUTE_IF_SET_IN_BITMAP (insn_bitmap, 0, uid, bi2)
7763 {
7764 /* We may have already removed a clobber. */
7765 if (!lra_insn_recog_data[uid])
7766 continue;
7767 insn = lra_insn_recog_data[uid]->insn;
7768 if ((set = single_set (insn)) != NULL_RTX)
7769 {
7770 src = SET_SRC (set);
7771 dest = SET_DEST (set);
7772 if ((REG_P (src) || SUBREG_P (src))
7773 && (REG_P (dest) || SUBREG_P (dest))
7774 && ((get_regno (src) == (int) regno
7775 && (get_regno (lra_reg_info[regno].restore_rtx)
7776 == get_regno (dest)))
7777 || (get_regno (dest) == (int) regno
7778 && (get_regno (lra_reg_info[regno].restore_rtx)
7779 == get_regno (src)))))
7780 {
7781 if (lra_dump_file != NULL)
7782 {
7783 fprintf (lra_dump_file, " Deleting move %u\n",
7784 INSN_UID (insn));
7785 dump_insn_slim (lra_dump_file, insn);
7786 }
7787 delete_move_and_clobber (insn, get_regno (dest));
7788 continue;
7789 }
7790 /* We should not worry about generation memory-memory
7791 moves here as if the corresponding inheritance did
7792 not work (inheritance pseudo did not get a hard reg),
7793 we remove the inheritance pseudo and the optional
7794 reload. */
7795 }
7796 if (GET_CODE (PATTERN (insn)) == CLOBBER
7797 && REG_P (SET_DEST (insn))
7798 && get_regno (SET_DEST (insn)) == (int) regno)
7799 /* Refuse to remap clobbers to preexisting pseudos. */
7800 gcc_unreachable ();
7801 lra_substitute_pseudo_within_insn
7802 (insn, regno, lra_reg_info[regno].restore_rtx, false);
7803 lra_update_insn_regno_info (insn);
7804 if (lra_dump_file != NULL)
7805 {
7806 fprintf (lra_dump_file,
7807 " Restoring original insn:\n");
7808 dump_insn_slim (lra_dump_file, insn);
7809 }
7810 }
7811 }
7812 /* Clear restore_regnos. */
7813 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7814 lra_reg_info[regno].restore_rtx = NULL_RTX;
7815 return change_p;
7816 }
7817
7818 /* Entry function for undoing inheritance/split transformation. Return true
7819 if we did any RTL change in this pass. */
7820 bool
7821 lra_undo_inheritance (void)
7822 {
7823 unsigned int regno;
7824 int hard_regno;
7825 int n_all_inherit, n_inherit, n_all_split, n_split;
7826 rtx restore_rtx;
7827 bitmap_iterator bi;
7828 bool change_p;
7829
7830 lra_undo_inheritance_iter++;
7831 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
7832 return false;
7833 if (lra_dump_file != NULL)
7834 fprintf (lra_dump_file,
7835 "\n********** Undoing inheritance #%d: **********\n\n",
7836 lra_undo_inheritance_iter);
7837 auto_bitmap remove_pseudos (&reg_obstack);
7838 n_inherit = n_all_inherit = 0;
7839 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7840 if (lra_reg_info[regno].restore_rtx != NULL_RTX)
7841 {
7842 n_all_inherit++;
7843 if (reg_renumber[regno] < 0
7844 /* If the original pseudo changed its allocation, just
7845 removing inheritance is dangerous as for changing
7846 allocation we used shorter live-ranges. */
7847 && (! REG_P (lra_reg_info[regno].restore_rtx)
7848 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] < 0))
7849 bitmap_set_bit (remove_pseudos, regno);
7850 else
7851 n_inherit++;
7852 }
7853 if (lra_dump_file != NULL && n_all_inherit != 0)
7854 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
7855 n_inherit, n_all_inherit,
7856 (double) n_inherit / n_all_inherit * 100);
7857 n_split = n_all_split = 0;
7858 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7859 if ((restore_rtx = lra_reg_info[regno].restore_rtx) != NULL_RTX)
7860 {
7861 int restore_regno = REGNO (restore_rtx);
7862
7863 n_all_split++;
7864 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
7865 ? reg_renumber[restore_regno] : restore_regno);
7866 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
7867 bitmap_set_bit (remove_pseudos, regno);
7868 else
7869 {
7870 n_split++;
7871 if (lra_dump_file != NULL)
7872 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
7873 regno, restore_regno);
7874 }
7875 }
7876 if (lra_dump_file != NULL && n_all_split != 0)
7877 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
7878 n_split, n_all_split,
7879 (double) n_split / n_all_split * 100);
7880 change_p = remove_inheritance_pseudos (remove_pseudos);
7881 /* Clear restore_regnos. */
7882 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7883 lra_reg_info[regno].restore_rtx = NULL_RTX;
7884 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7885 lra_reg_info[regno].restore_rtx = NULL_RTX;
7886 change_p = undo_optional_reloads () || change_p;
7887 if (change_p)
7888 lra_dump_insns_if_possible ("changed func after undoing inheritance");
7889 return change_p;
7890 }