]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/lra-constraints.c
[PATCH] Move RTL printing code from sched-vis.c into print-rtl.c
[thirdparty/gcc.git] / gcc / lra-constraints.c
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2015 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
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 "predict.h"
114 #include "tree.h"
115 #include "rtl.h"
116 #include "df.h"
117 #include "tm_p.h"
118 #include "regs.h"
119 #include "insn-config.h"
120 #include "insn-codes.h"
121 #include "recog.h"
122 #include "output.h"
123 #include "addresses.h"
124 #include "target.h"
125 #include "flags.h"
126 #include "alias.h"
127 #include "expmed.h"
128 #include "dojump.h"
129 #include "explow.h"
130 #include "calls.h"
131 #include "emit-rtl.h"
132 #include "varasm.h"
133 #include "stmt.h"
134 #include "expr.h"
135 #include "cfgrtl.h"
136 #include "except.h"
137 #include "optabs.h"
138 #include "ira.h"
139 #include "rtl-error.h"
140 #include "params.h"
141 #include "lra.h"
142 #include "insn-attr.h"
143 #include "lra-int.h"
144 #include "print-rtl.h"
145
146 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
147 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
148 reload insns. */
149 static int bb_reload_num;
150
151 /* The current insn being processed and corresponding its single set
152 (NULL otherwise), its data (basic block, the insn data, the insn
153 static data, and the mode of each operand). */
154 static rtx_insn *curr_insn;
155 static rtx curr_insn_set;
156 static basic_block curr_bb;
157 static lra_insn_recog_data_t curr_id;
158 static struct lra_static_insn_data *curr_static_id;
159 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
160 /* Mode of the register substituted by its equivalence with VOIDmode
161 (e.g. constant) and whose subreg is given operand of the current
162 insn. VOIDmode in all other cases. */
163 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
164
165 \f
166
167 /* Start numbers for new registers and insns at the current constraints
168 pass start. */
169 static int new_regno_start;
170 static int new_insn_uid_start;
171
172 /* If LOC is nonnull, strip any outer subreg from it. */
173 static inline rtx *
174 strip_subreg (rtx *loc)
175 {
176 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
177 }
178
179 /* Return hard regno of REGNO or if it is was not assigned to a hard
180 register, use a hard register from its allocno class. */
181 static int
182 get_try_hard_regno (int regno)
183 {
184 int hard_regno;
185 enum reg_class rclass;
186
187 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
188 hard_regno = lra_get_regno_hard_regno (regno);
189 if (hard_regno >= 0)
190 return hard_regno;
191 rclass = lra_get_allocno_class (regno);
192 if (rclass == NO_REGS)
193 return -1;
194 return ira_class_hard_regs[rclass][0];
195 }
196
197 /* Return final hard regno (plus offset) which will be after
198 elimination. We do this for matching constraints because the final
199 hard regno could have a different class. */
200 static int
201 get_final_hard_regno (int hard_regno, int offset)
202 {
203 if (hard_regno < 0)
204 return hard_regno;
205 hard_regno = lra_get_elimination_hard_regno (hard_regno);
206 return hard_regno + offset;
207 }
208
209 /* Return hard regno of X after removing subreg and making
210 elimination. If X is not a register or subreg of register, return
211 -1. For pseudo use its assignment. */
212 static int
213 get_hard_regno (rtx x)
214 {
215 rtx reg;
216 int offset, hard_regno;
217
218 reg = x;
219 if (GET_CODE (x) == SUBREG)
220 reg = SUBREG_REG (x);
221 if (! REG_P (reg))
222 return -1;
223 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
224 hard_regno = lra_get_regno_hard_regno (hard_regno);
225 if (hard_regno < 0)
226 return -1;
227 offset = 0;
228 if (GET_CODE (x) == SUBREG)
229 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
230 SUBREG_BYTE (x), GET_MODE (x));
231 return get_final_hard_regno (hard_regno, offset);
232 }
233
234 /* If REGNO is a hard register or has been allocated a hard register,
235 return the class of that register. If REGNO is a reload pseudo
236 created by the current constraints pass, return its allocno class.
237 Return NO_REGS otherwise. */
238 static enum reg_class
239 get_reg_class (int regno)
240 {
241 int hard_regno;
242
243 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
244 hard_regno = lra_get_regno_hard_regno (regno);
245 if (hard_regno >= 0)
246 {
247 hard_regno = get_final_hard_regno (hard_regno, 0);
248 return REGNO_REG_CLASS (hard_regno);
249 }
250 if (regno >= new_regno_start)
251 return lra_get_allocno_class (regno);
252 return NO_REGS;
253 }
254
255 /* Return true if REG satisfies (or will satisfy) reg class constraint
256 CL. Use elimination first if REG is a hard register. If REG is a
257 reload pseudo created by this constraints pass, assume that it will
258 be allocated a hard register from its allocno class, but allow that
259 class to be narrowed to CL if it is currently a superset of CL.
260
261 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
262 REGNO (reg), or NO_REGS if no change in its class was needed. */
263 static bool
264 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
265 {
266 enum reg_class rclass, common_class;
267 machine_mode reg_mode;
268 int class_size, hard_regno, nregs, i, j;
269 int regno = REGNO (reg);
270
271 if (new_class != NULL)
272 *new_class = NO_REGS;
273 if (regno < FIRST_PSEUDO_REGISTER)
274 {
275 rtx final_reg = reg;
276 rtx *final_loc = &final_reg;
277
278 lra_eliminate_reg_if_possible (final_loc);
279 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
280 }
281 reg_mode = GET_MODE (reg);
282 rclass = get_reg_class (regno);
283 if (regno < new_regno_start
284 /* Do not allow the constraints for reload instructions to
285 influence the classes of new pseudos. These reloads are
286 typically moves that have many alternatives, and restricting
287 reload pseudos for one alternative may lead to situations
288 where other reload pseudos are no longer allocatable. */
289 || (INSN_UID (curr_insn) >= new_insn_uid_start
290 && curr_insn_set != NULL
291 && ((OBJECT_P (SET_SRC (curr_insn_set))
292 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
293 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
294 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
295 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
296 /* When we don't know what class will be used finally for reload
297 pseudos, we use ALL_REGS. */
298 return ((regno >= new_regno_start && rclass == ALL_REGS)
299 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
300 && ! hard_reg_set_subset_p (reg_class_contents[cl],
301 lra_no_alloc_regs)));
302 else
303 {
304 common_class = ira_reg_class_subset[rclass][cl];
305 if (new_class != NULL)
306 *new_class = common_class;
307 if (hard_reg_set_subset_p (reg_class_contents[common_class],
308 lra_no_alloc_regs))
309 return false;
310 /* Check that there are enough allocatable regs. */
311 class_size = ira_class_hard_regs_num[common_class];
312 for (i = 0; i < class_size; i++)
313 {
314 hard_regno = ira_class_hard_regs[common_class][i];
315 nregs = hard_regno_nregs[hard_regno][reg_mode];
316 if (nregs == 1)
317 return true;
318 for (j = 0; j < nregs; j++)
319 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
320 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
321 hard_regno + j))
322 break;
323 if (j >= nregs)
324 return true;
325 }
326 return false;
327 }
328 }
329
330 /* Return true if REGNO satisfies a memory constraint. */
331 static bool
332 in_mem_p (int regno)
333 {
334 return get_reg_class (regno) == NO_REGS;
335 }
336
337 /* Return 1 if ADDR is a valid memory address for mode MODE in address
338 space AS, and check that each pseudo has the proper kind of hard
339 reg. */
340 static int
341 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
342 rtx addr, addr_space_t as)
343 {
344 #ifdef GO_IF_LEGITIMATE_ADDRESS
345 lra_assert (ADDR_SPACE_GENERIC_P (as));
346 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
347 return 0;
348
349 win:
350 return 1;
351 #else
352 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
353 #endif
354 }
355
356 namespace {
357 /* Temporarily eliminates registers in an address (for the lifetime of
358 the object). */
359 class address_eliminator {
360 public:
361 address_eliminator (struct address_info *ad);
362 ~address_eliminator ();
363
364 private:
365 struct address_info *m_ad;
366 rtx *m_base_loc;
367 rtx m_base_reg;
368 rtx *m_index_loc;
369 rtx m_index_reg;
370 };
371 }
372
373 address_eliminator::address_eliminator (struct address_info *ad)
374 : m_ad (ad),
375 m_base_loc (strip_subreg (ad->base_term)),
376 m_base_reg (NULL_RTX),
377 m_index_loc (strip_subreg (ad->index_term)),
378 m_index_reg (NULL_RTX)
379 {
380 if (m_base_loc != NULL)
381 {
382 m_base_reg = *m_base_loc;
383 lra_eliminate_reg_if_possible (m_base_loc);
384 if (m_ad->base_term2 != NULL)
385 *m_ad->base_term2 = *m_ad->base_term;
386 }
387 if (m_index_loc != NULL)
388 {
389 m_index_reg = *m_index_loc;
390 lra_eliminate_reg_if_possible (m_index_loc);
391 }
392 }
393
394 address_eliminator::~address_eliminator ()
395 {
396 if (m_base_loc && *m_base_loc != m_base_reg)
397 {
398 *m_base_loc = m_base_reg;
399 if (m_ad->base_term2 != NULL)
400 *m_ad->base_term2 = *m_ad->base_term;
401 }
402 if (m_index_loc && *m_index_loc != m_index_reg)
403 *m_index_loc = m_index_reg;
404 }
405
406 /* Return true if the eliminated form of AD is a legitimate target address. */
407 static bool
408 valid_address_p (struct address_info *ad)
409 {
410 address_eliminator eliminator (ad);
411 return valid_address_p (ad->mode, *ad->outer, ad->as);
412 }
413
414 /* Return true if the eliminated form of memory reference OP satisfies
415 extra memory constraint CONSTRAINT. */
416 static bool
417 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
418 {
419 struct address_info ad;
420
421 decompose_mem_address (&ad, op);
422 address_eliminator eliminator (&ad);
423 return constraint_satisfied_p (op, constraint);
424 }
425
426 /* Return true if the eliminated form of address AD satisfies extra
427 address constraint CONSTRAINT. */
428 static bool
429 satisfies_address_constraint_p (struct address_info *ad,
430 enum constraint_num constraint)
431 {
432 address_eliminator eliminator (ad);
433 return constraint_satisfied_p (*ad->outer, constraint);
434 }
435
436 /* Return true if the eliminated form of address OP satisfies extra
437 address constraint CONSTRAINT. */
438 static bool
439 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
440 {
441 struct address_info ad;
442
443 decompose_lea_address (&ad, &op);
444 return satisfies_address_constraint_p (&ad, constraint);
445 }
446
447 /* Initiate equivalences for LRA. As we keep original equivalences
448 before any elimination, we need to make copies otherwise any change
449 in insns might change the equivalences. */
450 void
451 lra_init_equiv (void)
452 {
453 ira_expand_reg_equiv ();
454 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
455 {
456 rtx res;
457
458 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
459 ira_reg_equiv[i].memory = copy_rtx (res);
460 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
461 ira_reg_equiv[i].invariant = copy_rtx (res);
462 }
463 }
464
465 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
466
467 /* Update equivalence for REGNO. We need to this as the equivalence
468 might contain other pseudos which are changed by their
469 equivalences. */
470 static void
471 update_equiv (int regno)
472 {
473 rtx x;
474
475 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
476 ira_reg_equiv[regno].memory
477 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
478 NULL_RTX);
479 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
480 ira_reg_equiv[regno].invariant
481 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
482 NULL_RTX);
483 }
484
485 /* If we have decided to substitute X with another value, return that
486 value, otherwise return X. */
487 static rtx
488 get_equiv (rtx x)
489 {
490 int regno;
491 rtx res;
492
493 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
494 || ! ira_reg_equiv[regno].defined_p
495 || ! ira_reg_equiv[regno].profitable_p
496 || lra_get_regno_hard_regno (regno) >= 0)
497 return x;
498 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
499 {
500 if (targetm.cannot_substitute_mem_equiv_p (res))
501 return x;
502 return res;
503 }
504 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
505 return res;
506 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
507 return res;
508 gcc_unreachable ();
509 }
510
511 /* If we have decided to substitute X with the equivalent value,
512 return that value after elimination for INSN, otherwise return
513 X. */
514 static rtx
515 get_equiv_with_elimination (rtx x, rtx_insn *insn)
516 {
517 rtx res = get_equiv (x);
518
519 if (x == res || CONSTANT_P (res))
520 return res;
521 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
522 false, false, 0, true);
523 }
524
525 /* Set up curr_operand_mode. */
526 static void
527 init_curr_operand_mode (void)
528 {
529 int nop = curr_static_id->n_operands;
530 for (int i = 0; i < nop; i++)
531 {
532 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
533 if (mode == VOIDmode)
534 {
535 /* The .md mode for address operands is the mode of the
536 addressed value rather than the mode of the address itself. */
537 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
538 mode = Pmode;
539 else
540 mode = curr_static_id->operand[i].mode;
541 }
542 curr_operand_mode[i] = mode;
543 }
544 }
545
546 \f
547
548 /* The page contains code to reuse input reloads. */
549
550 /* Structure describes input reload of the current insns. */
551 struct input_reload
552 {
553 /* Reloaded value. */
554 rtx input;
555 /* Reload pseudo used. */
556 rtx reg;
557 };
558
559 /* The number of elements in the following array. */
560 static int curr_insn_input_reloads_num;
561 /* Array containing info about input reloads. It is used to find the
562 same input reload and reuse the reload pseudo in this case. */
563 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
564
565 /* Initiate data concerning reuse of input reloads for the current
566 insn. */
567 static void
568 init_curr_insn_input_reloads (void)
569 {
570 curr_insn_input_reloads_num = 0;
571 }
572
573 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
574 created input reload pseudo (only if TYPE is not OP_OUT). Don't
575 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
576 wrapped up in SUBREG. The result pseudo is returned through
577 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
578 reused the already created input reload pseudo. Use TITLE to
579 describe new registers for debug purposes. */
580 static bool
581 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
582 enum reg_class rclass, bool in_subreg_p,
583 const char *title, rtx *result_reg)
584 {
585 int i, regno;
586 enum reg_class new_class;
587
588 if (type == OP_OUT)
589 {
590 *result_reg
591 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
592 return true;
593 }
594 /* Prevent reuse value of expression with side effects,
595 e.g. volatile memory. */
596 if (! side_effects_p (original))
597 for (i = 0; i < curr_insn_input_reloads_num; i++)
598 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
599 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
600 {
601 rtx reg = curr_insn_input_reloads[i].reg;
602 regno = REGNO (reg);
603 /* If input is equal to original and both are VOIDmode,
604 GET_MODE (reg) might be still different from mode.
605 Ensure we don't return *result_reg with wrong mode. */
606 if (GET_MODE (reg) != mode)
607 {
608 if (in_subreg_p)
609 continue;
610 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
611 continue;
612 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
613 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
614 continue;
615 }
616 *result_reg = reg;
617 if (lra_dump_file != NULL)
618 {
619 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
620 dump_value_slim (lra_dump_file, original, 1);
621 }
622 if (new_class != lra_get_allocno_class (regno))
623 lra_change_class (regno, new_class, ", change to", false);
624 if (lra_dump_file != NULL)
625 fprintf (lra_dump_file, "\n");
626 return false;
627 }
628 *result_reg = lra_create_new_reg (mode, original, rclass, title);
629 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
630 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
631 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
632 return true;
633 }
634
635 \f
636
637 /* The page contains code to extract memory address parts. */
638
639 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
640 static inline bool
641 ok_for_index_p_nonstrict (rtx reg)
642 {
643 unsigned regno = REGNO (reg);
644
645 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
646 }
647
648 /* A version of regno_ok_for_base_p for use here, when all pseudos
649 should count as OK. Arguments as for regno_ok_for_base_p. */
650 static inline bool
651 ok_for_base_p_nonstrict (rtx reg, machine_mode mode, addr_space_t as,
652 enum rtx_code outer_code, enum rtx_code index_code)
653 {
654 unsigned regno = REGNO (reg);
655
656 if (regno >= FIRST_PSEUDO_REGISTER)
657 return true;
658 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
659 }
660
661 \f
662
663 /* The page contains major code to choose the current insn alternative
664 and generate reloads for it. */
665
666 /* Return the offset from REGNO of the least significant register
667 in (reg:MODE REGNO).
668
669 This function is used to tell whether two registers satisfy
670 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
671
672 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
673 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
674 int
675 lra_constraint_offset (int regno, machine_mode mode)
676 {
677 lra_assert (regno < FIRST_PSEUDO_REGISTER);
678 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
679 && SCALAR_INT_MODE_P (mode))
680 return hard_regno_nregs[regno][mode] - 1;
681 return 0;
682 }
683
684 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
685 if they are the same hard reg, and has special hacks for
686 auto-increment and auto-decrement. This is specifically intended for
687 process_alt_operands to use in determining whether two operands
688 match. X is the operand whose number is the lower of the two.
689
690 It is supposed that X is the output operand and Y is the input
691 operand. Y_HARD_REGNO is the final hard regno of register Y or
692 register in subreg Y as we know it now. Otherwise, it is a
693 negative value. */
694 static bool
695 operands_match_p (rtx x, rtx y, int y_hard_regno)
696 {
697 int i;
698 RTX_CODE code = GET_CODE (x);
699 const char *fmt;
700
701 if (x == y)
702 return true;
703 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
704 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
705 {
706 int j;
707
708 i = get_hard_regno (x);
709 if (i < 0)
710 goto slow;
711
712 if ((j = y_hard_regno) < 0)
713 goto slow;
714
715 i += lra_constraint_offset (i, GET_MODE (x));
716 j += lra_constraint_offset (j, GET_MODE (y));
717
718 return i == j;
719 }
720
721 /* If two operands must match, because they are really a single
722 operand of an assembler insn, then two post-increments are invalid
723 because the assembler insn would increment only once. On the
724 other hand, a post-increment matches ordinary indexing if the
725 post-increment is the output operand. */
726 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
727 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
728
729 /* Two pre-increments are invalid because the assembler insn would
730 increment only once. On the other hand, a pre-increment matches
731 ordinary indexing if the pre-increment is the input operand. */
732 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
733 || GET_CODE (y) == PRE_MODIFY)
734 return operands_match_p (x, XEXP (y, 0), -1);
735
736 slow:
737
738 if (code == REG && REG_P (y))
739 return REGNO (x) == REGNO (y);
740
741 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
742 && x == SUBREG_REG (y))
743 return true;
744 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
745 && SUBREG_REG (x) == y)
746 return true;
747
748 /* Now we have disposed of all the cases in which different rtx
749 codes can match. */
750 if (code != GET_CODE (y))
751 return false;
752
753 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
754 if (GET_MODE (x) != GET_MODE (y))
755 return false;
756
757 switch (code)
758 {
759 CASE_CONST_UNIQUE:
760 return false;
761
762 case LABEL_REF:
763 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
764 case SYMBOL_REF:
765 return XSTR (x, 0) == XSTR (y, 0);
766
767 default:
768 break;
769 }
770
771 /* Compare the elements. If any pair of corresponding elements fail
772 to match, return false for the whole things. */
773
774 fmt = GET_RTX_FORMAT (code);
775 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
776 {
777 int val, j;
778 switch (fmt[i])
779 {
780 case 'w':
781 if (XWINT (x, i) != XWINT (y, i))
782 return false;
783 break;
784
785 case 'i':
786 if (XINT (x, i) != XINT (y, i))
787 return false;
788 break;
789
790 case 'e':
791 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
792 if (val == 0)
793 return false;
794 break;
795
796 case '0':
797 break;
798
799 case 'E':
800 if (XVECLEN (x, i) != XVECLEN (y, i))
801 return false;
802 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
803 {
804 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
805 if (val == 0)
806 return false;
807 }
808 break;
809
810 /* It is believed that rtx's at this level will never
811 contain anything but integers and other rtx's, except for
812 within LABEL_REFs and SYMBOL_REFs. */
813 default:
814 gcc_unreachable ();
815 }
816 }
817 return true;
818 }
819
820 /* True if X is a constant that can be forced into the constant pool.
821 MODE is the mode of the operand, or VOIDmode if not known. */
822 #define CONST_POOL_OK_P(MODE, X) \
823 ((MODE) != VOIDmode \
824 && CONSTANT_P (X) \
825 && GET_CODE (X) != HIGH \
826 && !targetm.cannot_force_const_mem (MODE, X))
827
828 /* True if C is a non-empty register class that has too few registers
829 to be safely used as a reload target class. */
830 #define SMALL_REGISTER_CLASS_P(C) \
831 (ira_class_hard_regs_num [(C)] == 1 \
832 || (ira_class_hard_regs_num [(C)] >= 1 \
833 && targetm.class_likely_spilled_p (C)))
834
835 /* If REG is a reload pseudo, try to make its class satisfying CL. */
836 static void
837 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
838 {
839 enum reg_class rclass;
840
841 /* Do not make more accurate class from reloads generated. They are
842 mostly moves with a lot of constraints. Making more accurate
843 class may results in very narrow class and impossibility of find
844 registers for several reloads of one insn. */
845 if (INSN_UID (curr_insn) >= new_insn_uid_start)
846 return;
847 if (GET_CODE (reg) == SUBREG)
848 reg = SUBREG_REG (reg);
849 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
850 return;
851 if (in_class_p (reg, cl, &rclass) && rclass != cl)
852 lra_change_class (REGNO (reg), rclass, " Change to", true);
853 }
854
855 /* Generate reloads for matching OUT and INS (array of input operand
856 numbers with end marker -1) with reg class GOAL_CLASS. Add input
857 and output reloads correspondingly to the lists *BEFORE and *AFTER.
858 OUT might be negative. In this case we generate input reloads for
859 matched input operands INS. EARLY_CLOBBER_P is a flag that the
860 output operand is early clobbered for chosen alternative. */
861 static void
862 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
863 rtx_insn **before, rtx_insn **after, bool early_clobber_p)
864 {
865 int i, in;
866 rtx new_in_reg, new_out_reg, reg;
867 machine_mode inmode, outmode;
868 rtx in_rtx = *curr_id->operand_loc[ins[0]];
869 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
870
871 inmode = curr_operand_mode[ins[0]];
872 outmode = out < 0 ? inmode : curr_operand_mode[out];
873 push_to_sequence (*before);
874 if (inmode != outmode)
875 {
876 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
877 {
878 reg = new_in_reg
879 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
880 goal_class, "");
881 if (SCALAR_INT_MODE_P (inmode))
882 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
883 else
884 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
885 LRA_SUBREG_P (new_out_reg) = 1;
886 /* If the input reg is dying here, we can use the same hard
887 register for REG and IN_RTX. We do it only for original
888 pseudos as reload pseudos can die although original
889 pseudos still live where reload pseudos dies. */
890 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
891 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
892 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
893 }
894 else
895 {
896 reg = new_out_reg
897 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
898 goal_class, "");
899 if (SCALAR_INT_MODE_P (outmode))
900 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
901 else
902 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
903 /* NEW_IN_REG is non-paradoxical subreg. We don't want
904 NEW_OUT_REG living above. We add clobber clause for
905 this. This is just a temporary clobber. We can remove
906 it at the end of LRA work. */
907 rtx_insn *clobber = emit_clobber (new_out_reg);
908 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
909 LRA_SUBREG_P (new_in_reg) = 1;
910 if (GET_CODE (in_rtx) == SUBREG)
911 {
912 rtx subreg_reg = SUBREG_REG (in_rtx);
913
914 /* If SUBREG_REG is dying here and sub-registers IN_RTX
915 and NEW_IN_REG are similar, we can use the same hard
916 register for REG and SUBREG_REG. */
917 if (REG_P (subreg_reg)
918 && (int) REGNO (subreg_reg) < lra_new_regno_start
919 && GET_MODE (subreg_reg) == outmode
920 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
921 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
922 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
923 }
924 }
925 }
926 else
927 {
928 /* Pseudos have values -- see comments for lra_reg_info.
929 Different pseudos with the same value do not conflict even if
930 they live in the same place. When we create a pseudo we
931 assign value of original pseudo (if any) from which we
932 created the new pseudo. If we create the pseudo from the
933 input pseudo, the new pseudo will have no conflict with the
934 input pseudo which is wrong when the input pseudo lives after
935 the insn and as the new pseudo value is changed by the insn
936 output. Therefore we create the new pseudo from the output
937 except the case when we have single matched dying input
938 pseudo.
939
940 We cannot reuse the current output register because we might
941 have a situation like "a <- a op b", where the constraints
942 force the second input operand ("b") to match the output
943 operand ("a"). "b" must then be copied into a new register
944 so that it doesn't clobber the current value of "a".
945
946 We can not use the same value if the output pseudo is
947 early clobbered or the input pseudo is mentioned in the
948 output, e.g. as an address part in memory, because
949 output reload will actually extend the pseudo liveness.
950 We don't care about eliminable hard regs here as we are
951 interesting only in pseudos. */
952
953 new_in_reg = new_out_reg
954 = (! early_clobber_p && ins[1] < 0 && REG_P (in_rtx)
955 && (int) REGNO (in_rtx) < lra_new_regno_start
956 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
957 && (out < 0 || regno_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
958 ? lra_create_new_reg (inmode, in_rtx, goal_class, "")
959 : lra_create_new_reg_with_unique_value (outmode, out_rtx,
960 goal_class, ""));
961 }
962 /* In operand can be got from transformations before processing insn
963 constraints. One example of such transformations is subreg
964 reloading (see function simplify_operand_subreg). The new
965 pseudos created by the transformations might have inaccurate
966 class (ALL_REGS) and we should make their classes more
967 accurate. */
968 narrow_reload_pseudo_class (in_rtx, goal_class);
969 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
970 *before = get_insns ();
971 end_sequence ();
972 for (i = 0; (in = ins[i]) >= 0; i++)
973 {
974 lra_assert
975 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
976 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
977 *curr_id->operand_loc[in] = new_in_reg;
978 }
979 lra_update_dups (curr_id, ins);
980 if (out < 0)
981 return;
982 /* See a comment for the input operand above. */
983 narrow_reload_pseudo_class (out_rtx, goal_class);
984 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
985 {
986 start_sequence ();
987 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
988 emit_insn (*after);
989 *after = get_insns ();
990 end_sequence ();
991 }
992 *curr_id->operand_loc[out] = new_out_reg;
993 lra_update_dup (curr_id, out);
994 }
995
996 /* Return register class which is union of all reg classes in insn
997 constraint alternative string starting with P. */
998 static enum reg_class
999 reg_class_from_constraints (const char *p)
1000 {
1001 int c, len;
1002 enum reg_class op_class = NO_REGS;
1003
1004 do
1005 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1006 {
1007 case '#':
1008 case ',':
1009 return op_class;
1010
1011 case 'g':
1012 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1013 break;
1014
1015 default:
1016 enum constraint_num cn = lookup_constraint (p);
1017 enum reg_class cl = reg_class_for_constraint (cn);
1018 if (cl == NO_REGS)
1019 {
1020 if (insn_extra_address_constraint (cn))
1021 op_class
1022 = (reg_class_subunion
1023 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1024 ADDRESS, SCRATCH)]);
1025 break;
1026 }
1027
1028 op_class = reg_class_subunion[op_class][cl];
1029 break;
1030 }
1031 while ((p += len), c);
1032 return op_class;
1033 }
1034
1035 /* If OP is a register, return the class of the register as per
1036 get_reg_class, otherwise return NO_REGS. */
1037 static inline enum reg_class
1038 get_op_class (rtx op)
1039 {
1040 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1041 }
1042
1043 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1044 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1045 SUBREG for VAL to make them equal. */
1046 static rtx_insn *
1047 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1048 {
1049 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1050 {
1051 /* Usually size of mem_pseudo is greater than val size but in
1052 rare cases it can be less as it can be defined by target
1053 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1054 if (! MEM_P (val))
1055 {
1056 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1057 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1058 0);
1059 LRA_SUBREG_P (val) = 1;
1060 }
1061 else
1062 {
1063 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1064 LRA_SUBREG_P (mem_pseudo) = 1;
1065 }
1066 }
1067 return to_p ? gen_move_insn (mem_pseudo, val)
1068 : gen_move_insn (val, mem_pseudo);
1069 }
1070
1071 /* Process a special case insn (register move), return true if we
1072 don't need to process it anymore. INSN should be a single set
1073 insn. Set up that RTL was changed through CHANGE_P and macro
1074 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1075 SEC_MEM_P. */
1076 static bool
1077 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1078 {
1079 int sregno, dregno;
1080 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1081 rtx_insn *before;
1082 enum reg_class dclass, sclass, secondary_class;
1083 secondary_reload_info sri;
1084
1085 lra_assert (curr_insn_set != NULL_RTX);
1086 dreg = dest = SET_DEST (curr_insn_set);
1087 sreg = src = SET_SRC (curr_insn_set);
1088 if (GET_CODE (dest) == SUBREG)
1089 dreg = SUBREG_REG (dest);
1090 if (GET_CODE (src) == SUBREG)
1091 sreg = SUBREG_REG (src);
1092 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1093 return false;
1094 sclass = dclass = NO_REGS;
1095 if (REG_P (dreg))
1096 dclass = get_reg_class (REGNO (dreg));
1097 if (dclass == ALL_REGS)
1098 /* ALL_REGS is used for new pseudos created by transformations
1099 like reload of SUBREG_REG (see function
1100 simplify_operand_subreg). We don't know their class yet. We
1101 should figure out the class from processing the insn
1102 constraints not in this fast path function. Even if ALL_REGS
1103 were a right class for the pseudo, secondary_... hooks usually
1104 are not define for ALL_REGS. */
1105 return false;
1106 if (REG_P (sreg))
1107 sclass = get_reg_class (REGNO (sreg));
1108 if (sclass == ALL_REGS)
1109 /* See comments above. */
1110 return false;
1111 if (sclass == NO_REGS && dclass == NO_REGS)
1112 return false;
1113 #ifdef SECONDARY_MEMORY_NEEDED
1114 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1115 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1116 && ((sclass != NO_REGS && dclass != NO_REGS)
1117 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1118 #endif
1119 )
1120 {
1121 *sec_mem_p = true;
1122 return false;
1123 }
1124 #endif
1125 if (! REG_P (dreg) || ! REG_P (sreg))
1126 return false;
1127 sri.prev_sri = NULL;
1128 sri.icode = CODE_FOR_nothing;
1129 sri.extra_cost = 0;
1130 secondary_class = NO_REGS;
1131 /* Set up hard register for a reload pseudo for hook
1132 secondary_reload because some targets just ignore unassigned
1133 pseudos in the hook. */
1134 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1135 {
1136 dregno = REGNO (dreg);
1137 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1138 }
1139 else
1140 dregno = -1;
1141 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1142 {
1143 sregno = REGNO (sreg);
1144 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1145 }
1146 else
1147 sregno = -1;
1148 if (sclass != NO_REGS)
1149 secondary_class
1150 = (enum reg_class) targetm.secondary_reload (false, dest,
1151 (reg_class_t) sclass,
1152 GET_MODE (src), &sri);
1153 if (sclass == NO_REGS
1154 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1155 && dclass != NO_REGS))
1156 {
1157 enum reg_class old_sclass = secondary_class;
1158 secondary_reload_info old_sri = sri;
1159
1160 sri.prev_sri = NULL;
1161 sri.icode = CODE_FOR_nothing;
1162 sri.extra_cost = 0;
1163 secondary_class
1164 = (enum reg_class) targetm.secondary_reload (true, src,
1165 (reg_class_t) dclass,
1166 GET_MODE (src), &sri);
1167 /* Check the target hook consistency. */
1168 lra_assert
1169 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1170 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1171 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1172 }
1173 if (sregno >= 0)
1174 reg_renumber [sregno] = -1;
1175 if (dregno >= 0)
1176 reg_renumber [dregno] = -1;
1177 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1178 return false;
1179 *change_p = true;
1180 new_reg = NULL_RTX;
1181 if (secondary_class != NO_REGS)
1182 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1183 secondary_class,
1184 "secondary");
1185 start_sequence ();
1186 if (sri.icode == CODE_FOR_nothing)
1187 lra_emit_move (new_reg, src);
1188 else
1189 {
1190 enum reg_class scratch_class;
1191
1192 scratch_class = (reg_class_from_constraints
1193 (insn_data[sri.icode].operand[2].constraint));
1194 scratch_reg = (lra_create_new_reg_with_unique_value
1195 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1196 scratch_class, "scratch"));
1197 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1198 src, scratch_reg));
1199 }
1200 before = get_insns ();
1201 end_sequence ();
1202 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1203 if (new_reg != NULL_RTX)
1204 SET_SRC (curr_insn_set) = new_reg;
1205 else
1206 {
1207 if (lra_dump_file != NULL)
1208 {
1209 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1210 dump_insn_slim (lra_dump_file, curr_insn);
1211 }
1212 lra_set_insn_deleted (curr_insn);
1213 return true;
1214 }
1215 return false;
1216 }
1217
1218 /* The following data describe the result of process_alt_operands.
1219 The data are used in curr_insn_transform to generate reloads. */
1220
1221 /* The chosen reg classes which should be used for the corresponding
1222 operands. */
1223 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1224 /* True if the operand should be the same as another operand and that
1225 other operand does not need a reload. */
1226 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1227 /* True if the operand does not need a reload. */
1228 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1229 /* True if the operand can be offsetable memory. */
1230 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1231 /* The number of an operand to which given operand can be matched to. */
1232 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1233 /* The number of elements in the following array. */
1234 static int goal_alt_dont_inherit_ops_num;
1235 /* Numbers of operands whose reload pseudos should not be inherited. */
1236 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1237 /* True if the insn commutative operands should be swapped. */
1238 static bool goal_alt_swapped;
1239 /* The chosen insn alternative. */
1240 static int goal_alt_number;
1241
1242 /* The following five variables are used to choose the best insn
1243 alternative. They reflect final characteristics of the best
1244 alternative. */
1245
1246 /* Number of necessary reloads and overall cost reflecting the
1247 previous value and other unpleasantness of the best alternative. */
1248 static int best_losers, best_overall;
1249 /* Overall number hard registers used for reloads. For example, on
1250 some targets we need 2 general registers to reload DFmode and only
1251 one floating point register. */
1252 static int best_reload_nregs;
1253 /* Overall number reflecting distances of previous reloading the same
1254 value. The distances are counted from the current BB start. It is
1255 used to improve inheritance chances. */
1256 static int best_reload_sum;
1257
1258 /* True if the current insn should have no correspondingly input or
1259 output reloads. */
1260 static bool no_input_reloads_p, no_output_reloads_p;
1261
1262 /* True if we swapped the commutative operands in the current
1263 insn. */
1264 static int curr_swapped;
1265
1266 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1267 register of class CL. Add any input reloads to list BEFORE. AFTER
1268 is nonnull if *LOC is an automodified value; handle that case by
1269 adding the required output reloads to list AFTER. Return true if
1270 the RTL was changed.
1271
1272 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1273 register. Return false if the address register is correct. */
1274 static bool
1275 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1276 enum reg_class cl)
1277 {
1278 int regno;
1279 enum reg_class rclass, new_class;
1280 rtx reg;
1281 rtx new_reg;
1282 machine_mode mode;
1283 bool subreg_p, before_p = false;
1284
1285 subreg_p = GET_CODE (*loc) == SUBREG;
1286 if (subreg_p)
1287 loc = &SUBREG_REG (*loc);
1288 reg = *loc;
1289 mode = GET_MODE (reg);
1290 if (! REG_P (reg))
1291 {
1292 if (check_only_p)
1293 return true;
1294 /* Always reload memory in an address even if the target supports
1295 such addresses. */
1296 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1297 before_p = true;
1298 }
1299 else
1300 {
1301 regno = REGNO (reg);
1302 rclass = get_reg_class (regno);
1303 if (! check_only_p
1304 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1305 {
1306 if (lra_dump_file != NULL)
1307 {
1308 fprintf (lra_dump_file,
1309 "Changing pseudo %d in address of insn %u on equiv ",
1310 REGNO (reg), INSN_UID (curr_insn));
1311 dump_value_slim (lra_dump_file, *loc, 1);
1312 fprintf (lra_dump_file, "\n");
1313 }
1314 *loc = copy_rtx (*loc);
1315 }
1316 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1317 {
1318 if (check_only_p)
1319 return true;
1320 reg = *loc;
1321 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1322 mode, reg, cl, subreg_p, "address", &new_reg))
1323 before_p = true;
1324 }
1325 else if (new_class != NO_REGS && rclass != new_class)
1326 {
1327 if (check_only_p)
1328 return true;
1329 lra_change_class (regno, new_class, " Change to", true);
1330 return false;
1331 }
1332 else
1333 return false;
1334 }
1335 if (before_p)
1336 {
1337 push_to_sequence (*before);
1338 lra_emit_move (new_reg, reg);
1339 *before = get_insns ();
1340 end_sequence ();
1341 }
1342 *loc = new_reg;
1343 if (after != NULL)
1344 {
1345 start_sequence ();
1346 lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1347 emit_insn (*after);
1348 *after = get_insns ();
1349 end_sequence ();
1350 }
1351 return true;
1352 }
1353
1354 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1355 the insn to be inserted before curr insn. AFTER returns the
1356 the insn to be inserted after curr insn. ORIGREG and NEWREG
1357 are the original reg and new reg for reload. */
1358 static void
1359 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1360 rtx newreg)
1361 {
1362 if (before)
1363 {
1364 push_to_sequence (*before);
1365 lra_emit_move (newreg, origreg);
1366 *before = get_insns ();
1367 end_sequence ();
1368 }
1369 if (after)
1370 {
1371 start_sequence ();
1372 lra_emit_move (origreg, newreg);
1373 emit_insn (*after);
1374 *after = get_insns ();
1375 end_sequence ();
1376 }
1377 }
1378
1379 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1380
1381 /* Make reloads for subreg in operand NOP with internal subreg mode
1382 REG_MODE, add new reloads for further processing. Return true if
1383 any change was done. */
1384 static bool
1385 simplify_operand_subreg (int nop, machine_mode reg_mode)
1386 {
1387 int hard_regno;
1388 rtx_insn *before, *after;
1389 machine_mode mode, innermode;
1390 rtx reg, new_reg;
1391 rtx operand = *curr_id->operand_loc[nop];
1392 enum reg_class regclass;
1393 enum op_type type;
1394
1395 before = after = NULL;
1396
1397 if (GET_CODE (operand) != SUBREG)
1398 return false;
1399
1400 mode = GET_MODE (operand);
1401 reg = SUBREG_REG (operand);
1402 innermode = GET_MODE (reg);
1403 type = curr_static_id->operand[nop].type;
1404 /* If we change address for paradoxical subreg of memory, the
1405 address might violate the necessary alignment or the access might
1406 be slow. So take this into consideration. We should not worry
1407 about access beyond allocated memory for paradoxical memory
1408 subregs as we don't substitute such equiv memory (see processing
1409 equivalences in function lra_constraints) and because for spilled
1410 pseudos we allocate stack memory enough for the biggest
1411 corresponding paradoxical subreg. */
1412 if (MEM_P (reg)
1413 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1414 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1415 {
1416 rtx subst, old = *curr_id->operand_loc[nop];
1417
1418 alter_subreg (curr_id->operand_loc[nop], false);
1419 subst = *curr_id->operand_loc[nop];
1420 lra_assert (MEM_P (subst));
1421 if (! valid_address_p (innermode, XEXP (reg, 0),
1422 MEM_ADDR_SPACE (reg))
1423 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1424 MEM_ADDR_SPACE (subst)))
1425 return true;
1426 /* If the address was valid and became invalid, prefer to reload
1427 the memory. Typical case is when the index scale should
1428 correspond the memory. */
1429 *curr_id->operand_loc[nop] = old;
1430 }
1431 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1432 {
1433 alter_subreg (curr_id->operand_loc[nop], false);
1434 return true;
1435 }
1436 else if (CONSTANT_P (reg))
1437 {
1438 /* Try to simplify subreg of constant. It is usually result of
1439 equivalence substitution. */
1440 if (innermode == VOIDmode
1441 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1442 innermode = curr_static_id->operand[nop].mode;
1443 if ((new_reg = simplify_subreg (mode, reg, innermode,
1444 SUBREG_BYTE (operand))) != NULL_RTX)
1445 {
1446 *curr_id->operand_loc[nop] = new_reg;
1447 return true;
1448 }
1449 }
1450 /* Put constant into memory when we have mixed modes. It generates
1451 a better code in most cases as it does not need a secondary
1452 reload memory. It also prevents LRA looping when LRA is using
1453 secondary reload memory again and again. */
1454 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1455 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1456 {
1457 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1458 alter_subreg (curr_id->operand_loc[nop], false);
1459 return true;
1460 }
1461 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1462 if there may be a problem accessing OPERAND in the outer
1463 mode. */
1464 if ((REG_P (reg)
1465 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1466 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1467 /* Don't reload paradoxical subregs because we could be looping
1468 having repeatedly final regno out of hard regs range. */
1469 && (hard_regno_nregs[hard_regno][innermode]
1470 >= hard_regno_nregs[hard_regno][mode])
1471 && simplify_subreg_regno (hard_regno, innermode,
1472 SUBREG_BYTE (operand), mode) < 0
1473 /* Don't reload subreg for matching reload. It is actually
1474 valid subreg in LRA. */
1475 && ! LRA_SUBREG_P (operand))
1476 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1477 {
1478 enum reg_class rclass;
1479
1480 if (REG_P (reg))
1481 /* There is a big probability that we will get the same class
1482 for the new pseudo and we will get the same insn which
1483 means infinite looping. So spill the new pseudo. */
1484 rclass = NO_REGS;
1485 else
1486 /* The class will be defined later in curr_insn_transform. */
1487 rclass
1488 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1489
1490 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1491 rclass, TRUE, "subreg reg", &new_reg))
1492 {
1493 bool insert_before, insert_after;
1494 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1495
1496 insert_before = (type != OP_OUT
1497 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1498 insert_after = (type != OP_IN);
1499 insert_move_for_subreg (insert_before ? &before : NULL,
1500 insert_after ? &after : NULL,
1501 reg, new_reg);
1502 }
1503 SUBREG_REG (operand) = new_reg;
1504 lra_process_new_insns (curr_insn, before, after,
1505 "Inserting subreg reload");
1506 return true;
1507 }
1508 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1509 IRA allocates hardreg to the inner pseudo reg according to its mode
1510 instead of the outermode, so the size of the hardreg may not be enough
1511 to contain the outermode operand, in that case we may need to insert
1512 reload for the reg. For the following two types of paradoxical subreg,
1513 we need to insert reload:
1514 1. If the op_type is OP_IN, and the hardreg could not be paired with
1515 other hardreg to contain the outermode operand
1516 (checked by in_hard_reg_set_p), we need to insert the reload.
1517 2. If the op_type is OP_OUT or OP_INOUT.
1518
1519 Here is a paradoxical subreg example showing how the reload is generated:
1520
1521 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1522 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1523
1524 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1525 here, if reg107 is assigned to hardreg R15, because R15 is the last
1526 hardreg, compiler cannot find another hardreg to pair with R15 to
1527 contain TImode data. So we insert a TImode reload reg180 for it.
1528 After reload is inserted:
1529
1530 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1531 (reg:DI 107 [ __comp ])) -1
1532 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1533 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1534
1535 Two reload hard registers will be allocated to reg180 to save TImode data
1536 in LRA_assign. */
1537 else if (REG_P (reg)
1538 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1539 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1540 && (hard_regno_nregs[hard_regno][innermode]
1541 < hard_regno_nregs[hard_regno][mode])
1542 && (regclass = lra_get_allocno_class (REGNO (reg)))
1543 && (type != OP_IN
1544 || !in_hard_reg_set_p (reg_class_contents[regclass],
1545 mode, hard_regno)))
1546 {
1547 /* The class will be defined later in curr_insn_transform. */
1548 enum reg_class rclass
1549 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1550
1551 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1552 rclass, TRUE, "paradoxical subreg", &new_reg))
1553 {
1554 rtx subreg;
1555 bool insert_before, insert_after;
1556
1557 PUT_MODE (new_reg, mode);
1558 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1559 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1560
1561 insert_before = (type != OP_OUT);
1562 insert_after = (type != OP_IN);
1563 insert_move_for_subreg (insert_before ? &before : NULL,
1564 insert_after ? &after : NULL,
1565 reg, subreg);
1566 }
1567 SUBREG_REG (operand) = new_reg;
1568 lra_process_new_insns (curr_insn, before, after,
1569 "Inserting paradoxical subreg reload");
1570 return true;
1571 }
1572 return false;
1573 }
1574
1575 /* Return TRUE if X refers for a hard register from SET. */
1576 static bool
1577 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1578 {
1579 int i, j, x_hard_regno;
1580 machine_mode mode;
1581 const char *fmt;
1582 enum rtx_code code;
1583
1584 if (x == NULL_RTX)
1585 return false;
1586 code = GET_CODE (x);
1587 mode = GET_MODE (x);
1588 if (code == SUBREG)
1589 {
1590 x = SUBREG_REG (x);
1591 code = GET_CODE (x);
1592 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1593 mode = GET_MODE (x);
1594 }
1595
1596 if (REG_P (x))
1597 {
1598 x_hard_regno = get_hard_regno (x);
1599 return (x_hard_regno >= 0
1600 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1601 }
1602 if (MEM_P (x))
1603 {
1604 struct address_info ad;
1605
1606 decompose_mem_address (&ad, x);
1607 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1608 return true;
1609 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1610 return true;
1611 }
1612 fmt = GET_RTX_FORMAT (code);
1613 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1614 {
1615 if (fmt[i] == 'e')
1616 {
1617 if (uses_hard_regs_p (XEXP (x, i), set))
1618 return true;
1619 }
1620 else if (fmt[i] == 'E')
1621 {
1622 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1623 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1624 return true;
1625 }
1626 }
1627 return false;
1628 }
1629
1630 /* Return true if OP is a spilled pseudo. */
1631 static inline bool
1632 spilled_pseudo_p (rtx op)
1633 {
1634 return (REG_P (op)
1635 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1636 }
1637
1638 /* Return true if X is a general constant. */
1639 static inline bool
1640 general_constant_p (rtx x)
1641 {
1642 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1643 }
1644
1645 static bool
1646 reg_in_class_p (rtx reg, enum reg_class cl)
1647 {
1648 if (cl == NO_REGS)
1649 return get_reg_class (REGNO (reg)) == NO_REGS;
1650 return in_class_p (reg, cl, NULL);
1651 }
1652
1653 /* Return true if SET of RCLASS contains no hard regs which can be
1654 used in MODE. */
1655 static bool
1656 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1657 HARD_REG_SET &set,
1658 enum machine_mode mode)
1659 {
1660 HARD_REG_SET temp;
1661
1662 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1663 COPY_HARD_REG_SET (temp, set);
1664 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1665 return (hard_reg_set_subset_p
1666 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1667 }
1668
1669 /* Major function to choose the current insn alternative and what
1670 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1671 negative we should consider only this alternative. Return false if
1672 we can not choose the alternative or find how to reload the
1673 operands. */
1674 static bool
1675 process_alt_operands (int only_alternative)
1676 {
1677 bool ok_p = false;
1678 int nop, overall, nalt;
1679 int n_alternatives = curr_static_id->n_alternatives;
1680 int n_operands = curr_static_id->n_operands;
1681 /* LOSERS counts the operands that don't fit this alternative and
1682 would require loading. */
1683 int losers;
1684 /* REJECT is a count of how undesirable this alternative says it is
1685 if any reloading is required. If the alternative matches exactly
1686 then REJECT is ignored, but otherwise it gets this much counted
1687 against it in addition to the reloading needed. */
1688 int reject;
1689 int op_reject;
1690 /* The number of elements in the following array. */
1691 int early_clobbered_regs_num;
1692 /* Numbers of operands which are early clobber registers. */
1693 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1694 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1695 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1696 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1697 bool curr_alt_win[MAX_RECOG_OPERANDS];
1698 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1699 int curr_alt_matches[MAX_RECOG_OPERANDS];
1700 /* The number of elements in the following array. */
1701 int curr_alt_dont_inherit_ops_num;
1702 /* Numbers of operands whose reload pseudos should not be inherited. */
1703 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1704 rtx op;
1705 /* The register when the operand is a subreg of register, otherwise the
1706 operand itself. */
1707 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1708 /* The register if the operand is a register or subreg of register,
1709 otherwise NULL. */
1710 rtx operand_reg[MAX_RECOG_OPERANDS];
1711 int hard_regno[MAX_RECOG_OPERANDS];
1712 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1713 int reload_nregs, reload_sum;
1714 bool costly_p;
1715 enum reg_class cl;
1716
1717 /* Calculate some data common for all alternatives to speed up the
1718 function. */
1719 for (nop = 0; nop < n_operands; nop++)
1720 {
1721 rtx reg;
1722
1723 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1724 /* The real hard regno of the operand after the allocation. */
1725 hard_regno[nop] = get_hard_regno (op);
1726
1727 operand_reg[nop] = reg = op;
1728 biggest_mode[nop] = GET_MODE (op);
1729 if (GET_CODE (op) == SUBREG)
1730 {
1731 operand_reg[nop] = reg = SUBREG_REG (op);
1732 if (GET_MODE_SIZE (biggest_mode[nop])
1733 < GET_MODE_SIZE (GET_MODE (reg)))
1734 biggest_mode[nop] = GET_MODE (reg);
1735 }
1736 if (! REG_P (reg))
1737 operand_reg[nop] = NULL_RTX;
1738 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1739 || ((int) REGNO (reg)
1740 == lra_get_elimination_hard_regno (REGNO (reg))))
1741 no_subreg_reg_operand[nop] = reg;
1742 else
1743 operand_reg[nop] = no_subreg_reg_operand[nop]
1744 /* Just use natural mode for elimination result. It should
1745 be enough for extra constraints hooks. */
1746 = regno_reg_rtx[hard_regno[nop]];
1747 }
1748
1749 /* The constraints are made of several alternatives. Each operand's
1750 constraint looks like foo,bar,... with commas separating the
1751 alternatives. The first alternatives for all operands go
1752 together, the second alternatives go together, etc.
1753
1754 First loop over alternatives. */
1755 alternative_mask preferred = curr_id->preferred_alternatives;
1756 if (only_alternative >= 0)
1757 preferred &= ALTERNATIVE_BIT (only_alternative);
1758
1759 for (nalt = 0; nalt < n_alternatives; nalt++)
1760 {
1761 /* Loop over operands for one constraint alternative. */
1762 if (!TEST_BIT (preferred, nalt))
1763 continue;
1764
1765 overall = losers = reject = reload_nregs = reload_sum = 0;
1766 for (nop = 0; nop < n_operands; nop++)
1767 {
1768 int inc = (curr_static_id
1769 ->operand_alternative[nalt * n_operands + nop].reject);
1770 if (lra_dump_file != NULL && inc != 0)
1771 fprintf (lra_dump_file,
1772 " Staticly defined alt reject+=%d\n", inc);
1773 reject += inc;
1774 }
1775 early_clobbered_regs_num = 0;
1776
1777 for (nop = 0; nop < n_operands; nop++)
1778 {
1779 const char *p;
1780 char *end;
1781 int len, c, m, i, opalt_num, this_alternative_matches;
1782 bool win, did_match, offmemok, early_clobber_p;
1783 /* false => this operand can be reloaded somehow for this
1784 alternative. */
1785 bool badop;
1786 /* true => this operand can be reloaded if the alternative
1787 allows regs. */
1788 bool winreg;
1789 /* True if a constant forced into memory would be OK for
1790 this operand. */
1791 bool constmemok;
1792 enum reg_class this_alternative, this_costly_alternative;
1793 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1794 bool this_alternative_match_win, this_alternative_win;
1795 bool this_alternative_offmemok;
1796 bool scratch_p;
1797 machine_mode mode;
1798 enum constraint_num cn;
1799
1800 opalt_num = nalt * n_operands + nop;
1801 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1802 {
1803 /* Fast track for no constraints at all. */
1804 curr_alt[nop] = NO_REGS;
1805 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1806 curr_alt_win[nop] = true;
1807 curr_alt_match_win[nop] = false;
1808 curr_alt_offmemok[nop] = false;
1809 curr_alt_matches[nop] = -1;
1810 continue;
1811 }
1812
1813 op = no_subreg_reg_operand[nop];
1814 mode = curr_operand_mode[nop];
1815
1816 win = did_match = winreg = offmemok = constmemok = false;
1817 badop = true;
1818
1819 early_clobber_p = false;
1820 p = curr_static_id->operand_alternative[opalt_num].constraint;
1821
1822 this_costly_alternative = this_alternative = NO_REGS;
1823 /* We update set of possible hard regs besides its class
1824 because reg class might be inaccurate. For example,
1825 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1826 is translated in HI_REGS because classes are merged by
1827 pairs and there is no accurate intermediate class. */
1828 CLEAR_HARD_REG_SET (this_alternative_set);
1829 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1830 this_alternative_win = false;
1831 this_alternative_match_win = false;
1832 this_alternative_offmemok = false;
1833 this_alternative_matches = -1;
1834
1835 /* An empty constraint should be excluded by the fast
1836 track. */
1837 lra_assert (*p != 0 && *p != ',');
1838
1839 op_reject = 0;
1840 /* Scan this alternative's specs for this operand; set WIN
1841 if the operand fits any letter in this alternative.
1842 Otherwise, clear BADOP if this operand could fit some
1843 letter after reloads, or set WINREG if this operand could
1844 fit after reloads provided the constraint allows some
1845 registers. */
1846 costly_p = false;
1847 do
1848 {
1849 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1850 {
1851 case '\0':
1852 len = 0;
1853 break;
1854 case ',':
1855 c = '\0';
1856 break;
1857
1858 case '&':
1859 early_clobber_p = true;
1860 break;
1861
1862 case '$':
1863 op_reject += LRA_MAX_REJECT;
1864 break;
1865 case '^':
1866 op_reject += LRA_LOSER_COST_FACTOR;
1867 break;
1868
1869 case '#':
1870 /* Ignore rest of this alternative. */
1871 c = '\0';
1872 break;
1873
1874 case '0': case '1': case '2': case '3': case '4':
1875 case '5': case '6': case '7': case '8': case '9':
1876 {
1877 int m_hregno;
1878 bool match_p;
1879
1880 m = strtoul (p, &end, 10);
1881 p = end;
1882 len = 0;
1883 lra_assert (nop > m);
1884
1885 this_alternative_matches = m;
1886 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1887 /* We are supposed to match a previous operand.
1888 If we do, we win if that one did. If we do
1889 not, count both of the operands as losers.
1890 (This is too conservative, since most of the
1891 time only a single reload insn will be needed
1892 to make the two operands win. As a result,
1893 this alternative may be rejected when it is
1894 actually desirable.) */
1895 match_p = false;
1896 if (operands_match_p (*curr_id->operand_loc[nop],
1897 *curr_id->operand_loc[m], m_hregno))
1898 {
1899 /* We should reject matching of an early
1900 clobber operand if the matching operand is
1901 not dying in the insn. */
1902 if (! curr_static_id->operand[m].early_clobber
1903 || operand_reg[nop] == NULL_RTX
1904 || (find_regno_note (curr_insn, REG_DEAD,
1905 REGNO (op))
1906 || REGNO (op) == REGNO (operand_reg[m])))
1907 match_p = true;
1908 }
1909 if (match_p)
1910 {
1911 /* If we are matching a non-offsettable
1912 address where an offsettable address was
1913 expected, then we must reject this
1914 combination, because we can't reload
1915 it. */
1916 if (curr_alt_offmemok[m]
1917 && MEM_P (*curr_id->operand_loc[m])
1918 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1919 continue;
1920 }
1921 else
1922 {
1923 /* Operands don't match. Both operands must
1924 allow a reload register, otherwise we
1925 cannot make them match. */
1926 if (curr_alt[m] == NO_REGS)
1927 break;
1928 /* Retroactively mark the operand we had to
1929 match as a loser, if it wasn't already and
1930 it wasn't matched to a register constraint
1931 (e.g it might be matched by memory). */
1932 if (curr_alt_win[m]
1933 && (operand_reg[m] == NULL_RTX
1934 || hard_regno[m] < 0))
1935 {
1936 losers++;
1937 reload_nregs
1938 += (ira_reg_class_max_nregs[curr_alt[m]]
1939 [GET_MODE (*curr_id->operand_loc[m])]);
1940 }
1941
1942 /* Prefer matching earlyclobber alternative as
1943 it results in less hard regs required for
1944 the insn than a non-matching earlyclobber
1945 alternative. */
1946 if (curr_static_id->operand[m].early_clobber)
1947 {
1948 if (lra_dump_file != NULL)
1949 fprintf
1950 (lra_dump_file,
1951 " %d Matching earlyclobber alt:"
1952 " reject--\n",
1953 nop);
1954 reject--;
1955 }
1956 /* Otherwise we prefer no matching
1957 alternatives because it gives more freedom
1958 in RA. */
1959 else if (operand_reg[nop] == NULL_RTX
1960 || (find_regno_note (curr_insn, REG_DEAD,
1961 REGNO (operand_reg[nop]))
1962 == NULL_RTX))
1963 {
1964 if (lra_dump_file != NULL)
1965 fprintf
1966 (lra_dump_file,
1967 " %d Matching alt: reject+=2\n",
1968 nop);
1969 reject += 2;
1970 }
1971 }
1972 /* If we have to reload this operand and some
1973 previous operand also had to match the same
1974 thing as this operand, we don't know how to do
1975 that. */
1976 if (!match_p || !curr_alt_win[m])
1977 {
1978 for (i = 0; i < nop; i++)
1979 if (curr_alt_matches[i] == m)
1980 break;
1981 if (i < nop)
1982 break;
1983 }
1984 else
1985 did_match = true;
1986
1987 /* This can be fixed with reloads if the operand
1988 we are supposed to match can be fixed with
1989 reloads. */
1990 badop = false;
1991 this_alternative = curr_alt[m];
1992 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1993 winreg = this_alternative != NO_REGS;
1994 break;
1995 }
1996
1997 case 'g':
1998 if (MEM_P (op)
1999 || general_constant_p (op)
2000 || spilled_pseudo_p (op))
2001 win = true;
2002 cl = GENERAL_REGS;
2003 goto reg;
2004
2005 default:
2006 cn = lookup_constraint (p);
2007 switch (get_constraint_type (cn))
2008 {
2009 case CT_REGISTER:
2010 cl = reg_class_for_constraint (cn);
2011 if (cl != NO_REGS)
2012 goto reg;
2013 break;
2014
2015 case CT_CONST_INT:
2016 if (CONST_INT_P (op)
2017 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2018 win = true;
2019 break;
2020
2021 case CT_MEMORY:
2022 if (MEM_P (op)
2023 && satisfies_memory_constraint_p (op, cn))
2024 win = true;
2025 else if (spilled_pseudo_p (op))
2026 win = true;
2027
2028 /* If we didn't already win, we can reload constants
2029 via force_const_mem or put the pseudo value into
2030 memory, or make other memory by reloading the
2031 address like for 'o'. */
2032 if (CONST_POOL_OK_P (mode, op)
2033 || MEM_P (op) || REG_P (op))
2034 badop = false;
2035 constmemok = true;
2036 offmemok = true;
2037 break;
2038
2039 case CT_ADDRESS:
2040 /* If we didn't already win, we can reload the address
2041 into a base register. */
2042 if (satisfies_address_constraint_p (op, cn))
2043 win = true;
2044 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2045 ADDRESS, SCRATCH);
2046 badop = false;
2047 goto reg;
2048
2049 case CT_FIXED_FORM:
2050 if (constraint_satisfied_p (op, cn))
2051 win = true;
2052 break;
2053 }
2054 break;
2055
2056 reg:
2057 this_alternative = reg_class_subunion[this_alternative][cl];
2058 IOR_HARD_REG_SET (this_alternative_set,
2059 reg_class_contents[cl]);
2060 if (costly_p)
2061 {
2062 this_costly_alternative
2063 = reg_class_subunion[this_costly_alternative][cl];
2064 IOR_HARD_REG_SET (this_costly_alternative_set,
2065 reg_class_contents[cl]);
2066 }
2067 if (mode == BLKmode)
2068 break;
2069 winreg = true;
2070 if (REG_P (op))
2071 {
2072 if (hard_regno[nop] >= 0
2073 && in_hard_reg_set_p (this_alternative_set,
2074 mode, hard_regno[nop]))
2075 win = true;
2076 else if (hard_regno[nop] < 0
2077 && in_class_p (op, this_alternative, NULL))
2078 win = true;
2079 }
2080 break;
2081 }
2082 if (c != ' ' && c != '\t')
2083 costly_p = c == '*';
2084 }
2085 while ((p += len), c);
2086
2087 scratch_p = (operand_reg[nop] != NULL_RTX
2088 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2089 /* Record which operands fit this alternative. */
2090 if (win)
2091 {
2092 this_alternative_win = true;
2093 if (operand_reg[nop] != NULL_RTX)
2094 {
2095 if (hard_regno[nop] >= 0)
2096 {
2097 if (in_hard_reg_set_p (this_costly_alternative_set,
2098 mode, hard_regno[nop]))
2099 {
2100 if (lra_dump_file != NULL)
2101 fprintf (lra_dump_file,
2102 " %d Costly set: reject++\n",
2103 nop);
2104 reject++;
2105 }
2106 }
2107 else
2108 {
2109 /* Prefer won reg to spilled pseudo under other
2110 equal conditions for possibe inheritance. */
2111 if (! scratch_p)
2112 {
2113 if (lra_dump_file != NULL)
2114 fprintf
2115 (lra_dump_file,
2116 " %d Non pseudo reload: reject++\n",
2117 nop);
2118 reject++;
2119 }
2120 if (in_class_p (operand_reg[nop],
2121 this_costly_alternative, NULL))
2122 {
2123 if (lra_dump_file != NULL)
2124 fprintf
2125 (lra_dump_file,
2126 " %d Non pseudo costly reload:"
2127 " reject++\n",
2128 nop);
2129 reject++;
2130 }
2131 }
2132 /* We simulate the behaviour of old reload here.
2133 Although scratches need hard registers and it
2134 might result in spilling other pseudos, no reload
2135 insns are generated for the scratches. So it
2136 might cost something but probably less than old
2137 reload pass believes. */
2138 if (scratch_p)
2139 {
2140 if (lra_dump_file != NULL)
2141 fprintf (lra_dump_file,
2142 " %d Scratch win: reject+=2\n",
2143 nop);
2144 reject += 2;
2145 }
2146 }
2147 }
2148 else if (did_match)
2149 this_alternative_match_win = true;
2150 else
2151 {
2152 int const_to_mem = 0;
2153 bool no_regs_p;
2154
2155 reject += op_reject;
2156 /* Never do output reload of stack pointer. It makes
2157 impossible to do elimination when SP is changed in
2158 RTL. */
2159 if (op == stack_pointer_rtx && ! frame_pointer_needed
2160 && curr_static_id->operand[nop].type != OP_IN)
2161 goto fail;
2162
2163 /* If this alternative asks for a specific reg class, see if there
2164 is at least one allocatable register in that class. */
2165 no_regs_p
2166 = (this_alternative == NO_REGS
2167 || (hard_reg_set_subset_p
2168 (reg_class_contents[this_alternative],
2169 lra_no_alloc_regs)));
2170
2171 /* For asms, verify that the class for this alternative is possible
2172 for the mode that is specified. */
2173 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2174 {
2175 int i;
2176 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2177 if (HARD_REGNO_MODE_OK (i, mode)
2178 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2179 mode, i))
2180 break;
2181 if (i == FIRST_PSEUDO_REGISTER)
2182 winreg = false;
2183 }
2184
2185 /* If this operand accepts a register, and if the
2186 register class has at least one allocatable register,
2187 then this operand can be reloaded. */
2188 if (winreg && !no_regs_p)
2189 badop = false;
2190
2191 if (badop)
2192 {
2193 if (lra_dump_file != NULL)
2194 fprintf (lra_dump_file,
2195 " alt=%d: Bad operand -- refuse\n",
2196 nalt);
2197 goto fail;
2198 }
2199
2200 /* If not assigned pseudo has a class which a subset of
2201 required reg class, it is a less costly alternative
2202 as the pseudo still can get a hard reg of necessary
2203 class. */
2204 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2205 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2206 && ira_class_subset_p[this_alternative][cl])
2207 {
2208 if (lra_dump_file != NULL)
2209 fprintf
2210 (lra_dump_file,
2211 " %d Super set class reg: reject-=3\n", nop);
2212 reject -= 3;
2213 }
2214
2215 this_alternative_offmemok = offmemok;
2216 if (this_costly_alternative != NO_REGS)
2217 {
2218 if (lra_dump_file != NULL)
2219 fprintf (lra_dump_file,
2220 " %d Costly loser: reject++\n", nop);
2221 reject++;
2222 }
2223 /* If the operand is dying, has a matching constraint,
2224 and satisfies constraints of the matched operand
2225 which failed to satisfy the own constraints, most probably
2226 the reload for this operand will be gone. */
2227 if (this_alternative_matches >= 0
2228 && !curr_alt_win[this_alternative_matches]
2229 && REG_P (op)
2230 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2231 && (hard_regno[nop] >= 0
2232 ? in_hard_reg_set_p (this_alternative_set,
2233 mode, hard_regno[nop])
2234 : in_class_p (op, this_alternative, NULL)))
2235 {
2236 if (lra_dump_file != NULL)
2237 fprintf
2238 (lra_dump_file,
2239 " %d Dying matched operand reload: reject++\n",
2240 nop);
2241 reject++;
2242 }
2243 else
2244 {
2245 /* Strict_low_part requires to reload the register
2246 not the sub-register. In this case we should
2247 check that a final reload hard reg can hold the
2248 value mode. */
2249 if (curr_static_id->operand[nop].strict_low
2250 && REG_P (op)
2251 && hard_regno[nop] < 0
2252 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2253 && ira_class_hard_regs_num[this_alternative] > 0
2254 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2255 [this_alternative][0],
2256 GET_MODE
2257 (*curr_id->operand_loc[nop])))
2258 {
2259 if (lra_dump_file != NULL)
2260 fprintf
2261 (lra_dump_file,
2262 " alt=%d: Strict low subreg reload -- refuse\n",
2263 nalt);
2264 goto fail;
2265 }
2266 losers++;
2267 }
2268 if (operand_reg[nop] != NULL_RTX
2269 /* Output operands and matched input operands are
2270 not inherited. The following conditions do not
2271 exactly describe the previous statement but they
2272 are pretty close. */
2273 && curr_static_id->operand[nop].type != OP_OUT
2274 && (this_alternative_matches < 0
2275 || curr_static_id->operand[nop].type != OP_IN))
2276 {
2277 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2278 (operand_reg[nop])]
2279 .last_reload);
2280
2281 /* The value of reload_sum has sense only if we
2282 process insns in their order. It happens only on
2283 the first constraints sub-pass when we do most of
2284 reload work. */
2285 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2286 reload_sum += last_reload - bb_reload_num;
2287 }
2288 /* If this is a constant that is reloaded into the
2289 desired class by copying it to memory first, count
2290 that as another reload. This is consistent with
2291 other code and is required to avoid choosing another
2292 alternative when the constant is moved into memory.
2293 Note that the test here is precisely the same as in
2294 the code below that calls force_const_mem. */
2295 if (CONST_POOL_OK_P (mode, op)
2296 && ((targetm.preferred_reload_class
2297 (op, this_alternative) == NO_REGS)
2298 || no_input_reloads_p))
2299 {
2300 const_to_mem = 1;
2301 if (! no_regs_p)
2302 losers++;
2303 }
2304
2305 /* Alternative loses if it requires a type of reload not
2306 permitted for this insn. We can always reload
2307 objects with a REG_UNUSED note. */
2308 if ((curr_static_id->operand[nop].type != OP_IN
2309 && no_output_reloads_p
2310 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2311 || (curr_static_id->operand[nop].type != OP_OUT
2312 && no_input_reloads_p && ! const_to_mem)
2313 || (this_alternative_matches >= 0
2314 && (no_input_reloads_p
2315 || (no_output_reloads_p
2316 && (curr_static_id->operand
2317 [this_alternative_matches].type != OP_IN)
2318 && ! find_reg_note (curr_insn, REG_UNUSED,
2319 no_subreg_reg_operand
2320 [this_alternative_matches])))))
2321 {
2322 if (lra_dump_file != NULL)
2323 fprintf
2324 (lra_dump_file,
2325 " alt=%d: No input/otput reload -- refuse\n",
2326 nalt);
2327 goto fail;
2328 }
2329
2330 /* Alternative loses if it required class pseudo can not
2331 hold value of required mode. Such insns can be
2332 described by insn definitions with mode iterators. */
2333 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2334 && ! hard_reg_set_empty_p (this_alternative_set)
2335 /* It is common practice for constraints to use a
2336 class which does not have actually enough regs to
2337 hold the value (e.g. x86 AREG for mode requiring
2338 more one general reg). Therefore we have 2
2339 conditions to check that the reload pseudo can
2340 not hold the mode value. */
2341 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2342 [this_alternative][0],
2343 GET_MODE (*curr_id->operand_loc[nop]))
2344 /* The above condition is not enough as the first
2345 reg in ira_class_hard_regs can be not aligned for
2346 multi-words mode values. */
2347 && (prohibited_class_reg_set_mode_p
2348 (this_alternative, this_alternative_set,
2349 GET_MODE (*curr_id->operand_loc[nop]))))
2350 {
2351 if (lra_dump_file != NULL)
2352 fprintf (lra_dump_file,
2353 " alt=%d: reload pseudo for op %d "
2354 " can not hold the mode value -- refuse\n",
2355 nalt, nop);
2356 goto fail;
2357 }
2358
2359 /* Check strong discouragement of reload of non-constant
2360 into class THIS_ALTERNATIVE. */
2361 if (! CONSTANT_P (op) && ! no_regs_p
2362 && (targetm.preferred_reload_class
2363 (op, this_alternative) == NO_REGS
2364 || (curr_static_id->operand[nop].type == OP_OUT
2365 && (targetm.preferred_output_reload_class
2366 (op, this_alternative) == NO_REGS))))
2367 {
2368 if (lra_dump_file != NULL)
2369 fprintf (lra_dump_file,
2370 " %d Non-prefered reload: reject+=%d\n",
2371 nop, LRA_MAX_REJECT);
2372 reject += LRA_MAX_REJECT;
2373 }
2374
2375 if (! (MEM_P (op) && offmemok)
2376 && ! (const_to_mem && constmemok))
2377 {
2378 /* We prefer to reload pseudos over reloading other
2379 things, since such reloads may be able to be
2380 eliminated later. So bump REJECT in other cases.
2381 Don't do this in the case where we are forcing a
2382 constant into memory and it will then win since
2383 we don't want to have a different alternative
2384 match then. */
2385 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2386 {
2387 if (lra_dump_file != NULL)
2388 fprintf
2389 (lra_dump_file,
2390 " %d Non-pseudo reload: reject+=2\n",
2391 nop);
2392 reject += 2;
2393 }
2394
2395 if (! no_regs_p)
2396 reload_nregs
2397 += ira_reg_class_max_nregs[this_alternative][mode];
2398
2399 if (SMALL_REGISTER_CLASS_P (this_alternative))
2400 {
2401 if (lra_dump_file != NULL)
2402 fprintf
2403 (lra_dump_file,
2404 " %d Small class reload: reject+=%d\n",
2405 nop, LRA_LOSER_COST_FACTOR / 2);
2406 reject += LRA_LOSER_COST_FACTOR / 2;
2407 }
2408 }
2409
2410 /* We are trying to spill pseudo into memory. It is
2411 usually more costly than moving to a hard register
2412 although it might takes the same number of
2413 reloads. */
2414 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2415 {
2416 if (lra_dump_file != NULL)
2417 fprintf
2418 (lra_dump_file,
2419 " %d Spill pseudo into memory: reject+=3\n",
2420 nop);
2421 reject += 3;
2422 if (VECTOR_MODE_P (mode))
2423 {
2424 /* Spilling vectors into memory is usually more
2425 costly as they contain big values. */
2426 if (lra_dump_file != NULL)
2427 fprintf
2428 (lra_dump_file,
2429 " %d Spill vector pseudo: reject+=2\n",
2430 nop);
2431 reject += 2;
2432 }
2433 }
2434
2435 #ifdef SECONDARY_MEMORY_NEEDED
2436 /* If reload requires moving value through secondary
2437 memory, it will need one more insn at least. */
2438 if (this_alternative != NO_REGS
2439 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2440 && ((curr_static_id->operand[nop].type != OP_OUT
2441 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2442 GET_MODE (op)))
2443 || (curr_static_id->operand[nop].type != OP_IN
2444 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2445 GET_MODE (op)))))
2446 losers++;
2447 #endif
2448 /* Input reloads can be inherited more often than output
2449 reloads can be removed, so penalize output
2450 reloads. */
2451 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2452 {
2453 if (lra_dump_file != NULL)
2454 fprintf
2455 (lra_dump_file,
2456 " %d Non input pseudo reload: reject++\n",
2457 nop);
2458 reject++;
2459 }
2460 }
2461
2462 if (early_clobber_p && ! scratch_p)
2463 {
2464 if (lra_dump_file != NULL)
2465 fprintf (lra_dump_file,
2466 " %d Early clobber: reject++\n", nop);
2467 reject++;
2468 }
2469 /* ??? We check early clobbers after processing all operands
2470 (see loop below) and there we update the costs more.
2471 Should we update the cost (may be approximately) here
2472 because of early clobber register reloads or it is a rare
2473 or non-important thing to be worth to do it. */
2474 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2475 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2476 {
2477 if (lra_dump_file != NULL)
2478 fprintf (lra_dump_file,
2479 " alt=%d,overall=%d,losers=%d -- refuse\n",
2480 nalt, overall, losers);
2481 goto fail;
2482 }
2483
2484 curr_alt[nop] = this_alternative;
2485 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2486 curr_alt_win[nop] = this_alternative_win;
2487 curr_alt_match_win[nop] = this_alternative_match_win;
2488 curr_alt_offmemok[nop] = this_alternative_offmemok;
2489 curr_alt_matches[nop] = this_alternative_matches;
2490
2491 if (this_alternative_matches >= 0
2492 && !did_match && !this_alternative_win)
2493 curr_alt_win[this_alternative_matches] = false;
2494
2495 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2496 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2497 }
2498 if (curr_insn_set != NULL_RTX && n_operands == 2
2499 /* Prevent processing non-move insns. */
2500 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2501 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2502 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2503 && REG_P (no_subreg_reg_operand[0])
2504 && REG_P (no_subreg_reg_operand[1])
2505 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2506 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2507 || (! curr_alt_win[0] && curr_alt_win[1]
2508 && REG_P (no_subreg_reg_operand[1])
2509 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2510 || (curr_alt_win[0] && ! curr_alt_win[1]
2511 && REG_P (no_subreg_reg_operand[0])
2512 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2513 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2514 no_subreg_reg_operand[1])
2515 || (targetm.preferred_reload_class
2516 (no_subreg_reg_operand[1],
2517 (enum reg_class) curr_alt[1]) != NO_REGS))
2518 /* If it is a result of recent elimination in move
2519 insn we can transform it into an add still by
2520 using this alternative. */
2521 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2522 {
2523 /* We have a move insn and a new reload insn will be similar
2524 to the current insn. We should avoid such situation as it
2525 results in LRA cycling. */
2526 overall += LRA_MAX_REJECT;
2527 }
2528 ok_p = true;
2529 curr_alt_dont_inherit_ops_num = 0;
2530 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2531 {
2532 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2533 HARD_REG_SET temp_set;
2534
2535 i = early_clobbered_nops[nop];
2536 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2537 || hard_regno[i] < 0)
2538 continue;
2539 lra_assert (operand_reg[i] != NULL_RTX);
2540 clobbered_hard_regno = hard_regno[i];
2541 CLEAR_HARD_REG_SET (temp_set);
2542 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2543 first_conflict_j = last_conflict_j = -1;
2544 for (j = 0; j < n_operands; j++)
2545 if (j == i
2546 /* We don't want process insides of match_operator and
2547 match_parallel because otherwise we would process
2548 their operands once again generating a wrong
2549 code. */
2550 || curr_static_id->operand[j].is_operator)
2551 continue;
2552 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2553 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2554 continue;
2555 /* If we don't reload j-th operand, check conflicts. */
2556 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2557 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2558 {
2559 if (first_conflict_j < 0)
2560 first_conflict_j = j;
2561 last_conflict_j = j;
2562 }
2563 if (last_conflict_j < 0)
2564 continue;
2565 /* If earlyclobber operand conflicts with another
2566 non-matching operand which is actually the same register
2567 as the earlyclobber operand, it is better to reload the
2568 another operand as an operand matching the earlyclobber
2569 operand can be also the same. */
2570 if (first_conflict_j == last_conflict_j
2571 && operand_reg[last_conflict_j]
2572 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2573 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2574 {
2575 curr_alt_win[last_conflict_j] = false;
2576 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2577 = last_conflict_j;
2578 losers++;
2579 /* Early clobber was already reflected in REJECT. */
2580 lra_assert (reject > 0);
2581 if (lra_dump_file != NULL)
2582 fprintf
2583 (lra_dump_file,
2584 " %d Conflict early clobber reload: reject--\n",
2585 i);
2586 reject--;
2587 overall += LRA_LOSER_COST_FACTOR - 1;
2588 }
2589 else
2590 {
2591 /* We need to reload early clobbered register and the
2592 matched registers. */
2593 for (j = 0; j < n_operands; j++)
2594 if (curr_alt_matches[j] == i)
2595 {
2596 curr_alt_match_win[j] = false;
2597 losers++;
2598 overall += LRA_LOSER_COST_FACTOR;
2599 }
2600 if (! curr_alt_match_win[i])
2601 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2602 else
2603 {
2604 /* Remember pseudos used for match reloads are never
2605 inherited. */
2606 lra_assert (curr_alt_matches[i] >= 0);
2607 curr_alt_win[curr_alt_matches[i]] = false;
2608 }
2609 curr_alt_win[i] = curr_alt_match_win[i] = false;
2610 losers++;
2611 /* Early clobber was already reflected in REJECT. */
2612 lra_assert (reject > 0);
2613 if (lra_dump_file != NULL)
2614 fprintf
2615 (lra_dump_file,
2616 " %d Matched conflict early clobber reloads:"
2617 "reject--\n",
2618 i);
2619 reject--;
2620 overall += LRA_LOSER_COST_FACTOR - 1;
2621 }
2622 }
2623 if (lra_dump_file != NULL)
2624 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2625 nalt, overall, losers, reload_nregs);
2626
2627 /* If this alternative can be made to work by reloading, and it
2628 needs less reloading than the others checked so far, record
2629 it as the chosen goal for reloading. */
2630 if ((best_losers != 0 && losers == 0)
2631 || (((best_losers == 0 && losers == 0)
2632 || (best_losers != 0 && losers != 0))
2633 && (best_overall > overall
2634 || (best_overall == overall
2635 /* If the cost of the reloads is the same,
2636 prefer alternative which requires minimal
2637 number of reload regs. */
2638 && (reload_nregs < best_reload_nregs
2639 || (reload_nregs == best_reload_nregs
2640 && (best_reload_sum < reload_sum
2641 || (best_reload_sum == reload_sum
2642 && nalt < goal_alt_number))))))))
2643 {
2644 for (nop = 0; nop < n_operands; nop++)
2645 {
2646 goal_alt_win[nop] = curr_alt_win[nop];
2647 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2648 goal_alt_matches[nop] = curr_alt_matches[nop];
2649 goal_alt[nop] = curr_alt[nop];
2650 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2651 }
2652 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2653 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2654 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2655 goal_alt_swapped = curr_swapped;
2656 best_overall = overall;
2657 best_losers = losers;
2658 best_reload_nregs = reload_nregs;
2659 best_reload_sum = reload_sum;
2660 goal_alt_number = nalt;
2661 }
2662 if (losers == 0)
2663 /* Everything is satisfied. Do not process alternatives
2664 anymore. */
2665 break;
2666 fail:
2667 ;
2668 }
2669 return ok_p;
2670 }
2671
2672 /* Make reload base reg from address AD. */
2673 static rtx
2674 base_to_reg (struct address_info *ad)
2675 {
2676 enum reg_class cl;
2677 int code = -1;
2678 rtx new_inner = NULL_RTX;
2679 rtx new_reg = NULL_RTX;
2680 rtx_insn *insn;
2681 rtx_insn *last_insn = get_last_insn();
2682
2683 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2684 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2685 get_index_code (ad));
2686 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2687 cl, "base");
2688 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2689 ad->disp_term == NULL
2690 ? gen_int_mode (0, ad->mode)
2691 : *ad->disp_term);
2692 if (!valid_address_p (ad->mode, new_inner, ad->as))
2693 return NULL_RTX;
2694 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base_term));
2695 code = recog_memoized (insn);
2696 if (code < 0)
2697 {
2698 delete_insns_since (last_insn);
2699 return NULL_RTX;
2700 }
2701
2702 return new_inner;
2703 }
2704
2705 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2706 static rtx
2707 base_plus_disp_to_reg (struct address_info *ad)
2708 {
2709 enum reg_class cl;
2710 rtx new_reg;
2711
2712 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2713 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2714 get_index_code (ad));
2715 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2716 cl, "base + disp");
2717 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2718 return new_reg;
2719 }
2720
2721 /* Make reload of index part of address AD. Return the new
2722 pseudo. */
2723 static rtx
2724 index_part_to_reg (struct address_info *ad)
2725 {
2726 rtx new_reg;
2727
2728 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2729 INDEX_REG_CLASS, "index term");
2730 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2731 GEN_INT (get_index_scale (ad)), new_reg, 1);
2732 return new_reg;
2733 }
2734
2735 /* Return true if we can add a displacement to address AD, even if that
2736 makes the address invalid. The fix-up code requires any new address
2737 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2738 static bool
2739 can_add_disp_p (struct address_info *ad)
2740 {
2741 return (!ad->autoinc_p
2742 && ad->segment == NULL
2743 && ad->base == ad->base_term
2744 && ad->disp == ad->disp_term);
2745 }
2746
2747 /* Make equiv substitution in address AD. Return true if a substitution
2748 was made. */
2749 static bool
2750 equiv_address_substitution (struct address_info *ad)
2751 {
2752 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2753 HOST_WIDE_INT disp, scale;
2754 bool change_p;
2755
2756 base_term = strip_subreg (ad->base_term);
2757 if (base_term == NULL)
2758 base_reg = new_base_reg = NULL_RTX;
2759 else
2760 {
2761 base_reg = *base_term;
2762 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2763 }
2764 index_term = strip_subreg (ad->index_term);
2765 if (index_term == NULL)
2766 index_reg = new_index_reg = NULL_RTX;
2767 else
2768 {
2769 index_reg = *index_term;
2770 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2771 }
2772 if (base_reg == new_base_reg && index_reg == new_index_reg)
2773 return false;
2774 disp = 0;
2775 change_p = false;
2776 if (lra_dump_file != NULL)
2777 {
2778 fprintf (lra_dump_file, "Changing address in insn %d ",
2779 INSN_UID (curr_insn));
2780 dump_value_slim (lra_dump_file, *ad->outer, 1);
2781 }
2782 if (base_reg != new_base_reg)
2783 {
2784 if (REG_P (new_base_reg))
2785 {
2786 *base_term = new_base_reg;
2787 change_p = true;
2788 }
2789 else if (GET_CODE (new_base_reg) == PLUS
2790 && REG_P (XEXP (new_base_reg, 0))
2791 && CONST_INT_P (XEXP (new_base_reg, 1))
2792 && can_add_disp_p (ad))
2793 {
2794 disp += INTVAL (XEXP (new_base_reg, 1));
2795 *base_term = XEXP (new_base_reg, 0);
2796 change_p = true;
2797 }
2798 if (ad->base_term2 != NULL)
2799 *ad->base_term2 = *ad->base_term;
2800 }
2801 if (index_reg != new_index_reg)
2802 {
2803 if (REG_P (new_index_reg))
2804 {
2805 *index_term = new_index_reg;
2806 change_p = true;
2807 }
2808 else if (GET_CODE (new_index_reg) == PLUS
2809 && REG_P (XEXP (new_index_reg, 0))
2810 && CONST_INT_P (XEXP (new_index_reg, 1))
2811 && can_add_disp_p (ad)
2812 && (scale = get_index_scale (ad)))
2813 {
2814 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2815 *index_term = XEXP (new_index_reg, 0);
2816 change_p = true;
2817 }
2818 }
2819 if (disp != 0)
2820 {
2821 if (ad->disp != NULL)
2822 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2823 else
2824 {
2825 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2826 update_address (ad);
2827 }
2828 change_p = true;
2829 }
2830 if (lra_dump_file != NULL)
2831 {
2832 if (! change_p)
2833 fprintf (lra_dump_file, " -- no change\n");
2834 else
2835 {
2836 fprintf (lra_dump_file, " on equiv ");
2837 dump_value_slim (lra_dump_file, *ad->outer, 1);
2838 fprintf (lra_dump_file, "\n");
2839 }
2840 }
2841 return change_p;
2842 }
2843
2844 /* Major function to make reloads for an address in operand NOP or
2845 check its correctness (If CHECK_ONLY_P is true). The supported
2846 cases are:
2847
2848 1) an address that existed before LRA started, at which point it
2849 must have been valid. These addresses are subject to elimination
2850 and may have become invalid due to the elimination offset being out
2851 of range.
2852
2853 2) an address created by forcing a constant to memory
2854 (force_const_to_mem). The initial form of these addresses might
2855 not be valid, and it is this function's job to make them valid.
2856
2857 3) a frame address formed from a register and a (possibly zero)
2858 constant offset. As above, these addresses might not be valid and
2859 this function must make them so.
2860
2861 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2862 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2863 address. Return true for any RTL change.
2864
2865 The function is a helper function which does not produce all
2866 transformations (when CHECK_ONLY_P is false) which can be
2867 necessary. It does just basic steps. To do all necessary
2868 transformations use function process_address. */
2869 static bool
2870 process_address_1 (int nop, bool check_only_p,
2871 rtx_insn **before, rtx_insn **after)
2872 {
2873 struct address_info ad;
2874 rtx new_reg;
2875 rtx op = *curr_id->operand_loc[nop];
2876 const char *constraint = curr_static_id->operand[nop].constraint;
2877 enum constraint_num cn = lookup_constraint (constraint);
2878 bool change_p = false;
2879
2880 if (insn_extra_address_constraint (cn))
2881 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2882 else if (MEM_P (op))
2883 decompose_mem_address (&ad, op);
2884 else if (GET_CODE (op) == SUBREG
2885 && MEM_P (SUBREG_REG (op)))
2886 decompose_mem_address (&ad, SUBREG_REG (op));
2887 else
2888 return false;
2889 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
2890 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
2891 when INDEX_REG_CLASS is a single register class. */
2892 if (ad.base_term != NULL
2893 && ad.index_term != NULL
2894 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
2895 && REG_P (*ad.base_term)
2896 && REG_P (*ad.index_term)
2897 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
2898 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
2899 {
2900 std::swap (ad.base, ad.index);
2901 std::swap (ad.base_term, ad.index_term);
2902 }
2903 if (! check_only_p)
2904 change_p = equiv_address_substitution (&ad);
2905 if (ad.base_term != NULL
2906 && (process_addr_reg
2907 (ad.base_term, check_only_p, before,
2908 (ad.autoinc_p
2909 && !(REG_P (*ad.base_term)
2910 && find_regno_note (curr_insn, REG_DEAD,
2911 REGNO (*ad.base_term)) != NULL_RTX)
2912 ? after : NULL),
2913 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2914 get_index_code (&ad)))))
2915 {
2916 change_p = true;
2917 if (ad.base_term2 != NULL)
2918 *ad.base_term2 = *ad.base_term;
2919 }
2920 if (ad.index_term != NULL
2921 && process_addr_reg (ad.index_term, check_only_p,
2922 before, NULL, INDEX_REG_CLASS))
2923 change_p = true;
2924
2925 /* Target hooks sometimes don't treat extra-constraint addresses as
2926 legitimate address_operands, so handle them specially. */
2927 if (insn_extra_address_constraint (cn)
2928 && satisfies_address_constraint_p (&ad, cn))
2929 return change_p;
2930
2931 if (check_only_p)
2932 return change_p;
2933
2934 /* There are three cases where the shape of *AD.INNER may now be invalid:
2935
2936 1) the original address was valid, but either elimination or
2937 equiv_address_substitution was applied and that made
2938 the address invalid.
2939
2940 2) the address is an invalid symbolic address created by
2941 force_const_to_mem.
2942
2943 3) the address is a frame address with an invalid offset.
2944
2945 4) the address is a frame address with an invalid base.
2946
2947 All these cases involve a non-autoinc address, so there is no
2948 point revalidating other types. */
2949 if (ad.autoinc_p || valid_address_p (&ad))
2950 return change_p;
2951
2952 /* Any index existed before LRA started, so we can assume that the
2953 presence and shape of the index is valid. */
2954 push_to_sequence (*before);
2955 lra_assert (ad.disp == ad.disp_term);
2956 if (ad.base == NULL)
2957 {
2958 if (ad.index == NULL)
2959 {
2960 int code = -1;
2961 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2962 SCRATCH, SCRATCH);
2963 rtx addr = *ad.inner;
2964
2965 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2966 if (HAVE_lo_sum)
2967 {
2968 rtx_insn *insn;
2969 rtx_insn *last = get_last_insn ();
2970
2971 /* addr => lo_sum (new_base, addr), case (2) above. */
2972 insn = emit_insn (gen_rtx_SET
2973 (new_reg,
2974 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2975 code = recog_memoized (insn);
2976 if (code >= 0)
2977 {
2978 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2979 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2980 {
2981 /* Try to put lo_sum into register. */
2982 insn = emit_insn (gen_rtx_SET
2983 (new_reg,
2984 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2985 code = recog_memoized (insn);
2986 if (code >= 0)
2987 {
2988 *ad.inner = new_reg;
2989 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2990 {
2991 *ad.inner = addr;
2992 code = -1;
2993 }
2994 }
2995
2996 }
2997 }
2998 if (code < 0)
2999 delete_insns_since (last);
3000 }
3001
3002 if (code < 0)
3003 {
3004 /* addr => new_base, case (2) above. */
3005 lra_emit_move (new_reg, addr);
3006 *ad.inner = new_reg;
3007 }
3008 }
3009 else
3010 {
3011 /* index * scale + disp => new base + index * scale,
3012 case (1) above. */
3013 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3014 GET_CODE (*ad.index));
3015
3016 lra_assert (INDEX_REG_CLASS != NO_REGS);
3017 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3018 lra_emit_move (new_reg, *ad.disp);
3019 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3020 new_reg, *ad.index);
3021 }
3022 }
3023 else if (ad.index == NULL)
3024 {
3025 int regno;
3026 enum reg_class cl;
3027 rtx set;
3028 rtx_insn *insns, *last_insn;
3029 /* Try to reload base into register only if the base is invalid
3030 for the address but with valid offset, case (4) above. */
3031 start_sequence ();
3032 new_reg = base_to_reg (&ad);
3033
3034 /* base + disp => new base, cases (1) and (3) above. */
3035 /* Another option would be to reload the displacement into an
3036 index register. However, postreload has code to optimize
3037 address reloads that have the same base and different
3038 displacements, so reloading into an index register would
3039 not necessarily be a win. */
3040 if (new_reg == NULL_RTX)
3041 new_reg = base_plus_disp_to_reg (&ad);
3042 insns = get_insns ();
3043 last_insn = get_last_insn ();
3044 /* If we generated at least two insns, try last insn source as
3045 an address. If we succeed, we generate one less insn. */
3046 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3047 && GET_CODE (SET_SRC (set)) == PLUS
3048 && REG_P (XEXP (SET_SRC (set), 0))
3049 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3050 {
3051 *ad.inner = SET_SRC (set);
3052 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3053 {
3054 *ad.base_term = XEXP (SET_SRC (set), 0);
3055 *ad.disp_term = XEXP (SET_SRC (set), 1);
3056 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3057 get_index_code (&ad));
3058 regno = REGNO (*ad.base_term);
3059 if (regno >= FIRST_PSEUDO_REGISTER
3060 && cl != lra_get_allocno_class (regno))
3061 lra_change_class (regno, cl, " Change to", true);
3062 new_reg = SET_SRC (set);
3063 delete_insns_since (PREV_INSN (last_insn));
3064 }
3065 }
3066 /* Try if target can split displacement into legitimite new disp
3067 and offset. If it's the case, we replace the last insn with
3068 insns for base + offset => new_reg and set new_reg + new disp
3069 to *ad.inner. */
3070 last_insn = get_last_insn ();
3071 if ((set = single_set (last_insn)) != NULL_RTX
3072 && GET_CODE (SET_SRC (set)) == PLUS
3073 && REG_P (XEXP (SET_SRC (set), 0))
3074 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3075 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3076 {
3077 rtx addend, disp = XEXP (SET_SRC (set), 1);
3078 if (targetm.legitimize_address_displacement (&disp, &addend,
3079 ad.mode))
3080 {
3081 rtx_insn *new_insns;
3082 start_sequence ();
3083 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3084 new_insns = get_insns ();
3085 end_sequence ();
3086 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3087 delete_insns_since (PREV_INSN (last_insn));
3088 add_insn (new_insns);
3089 insns = get_insns ();
3090 }
3091 }
3092 end_sequence ();
3093 emit_insn (insns);
3094 *ad.inner = new_reg;
3095 }
3096 else if (ad.disp_term != NULL)
3097 {
3098 /* base + scale * index + disp => new base + scale * index,
3099 case (1) above. */
3100 new_reg = base_plus_disp_to_reg (&ad);
3101 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3102 new_reg, *ad.index);
3103 }
3104 else if (get_index_scale (&ad) == 1)
3105 {
3106 /* The last transformation to one reg will be made in
3107 curr_insn_transform function. */
3108 end_sequence ();
3109 return false;
3110 }
3111 else
3112 {
3113 /* base + scale * index => base + new_reg,
3114 case (1) above.
3115 Index part of address may become invalid. For example, we
3116 changed pseudo on the equivalent memory and a subreg of the
3117 pseudo onto the memory of different mode for which the scale is
3118 prohibitted. */
3119 new_reg = index_part_to_reg (&ad);
3120 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3121 *ad.base_term, new_reg);
3122 }
3123 *before = get_insns ();
3124 end_sequence ();
3125 return true;
3126 }
3127
3128 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3129 Use process_address_1 as a helper function. Return true for any
3130 RTL changes.
3131
3132 If CHECK_ONLY_P is true, just check address correctness. Return
3133 false if the address correct. */
3134 static bool
3135 process_address (int nop, bool check_only_p,
3136 rtx_insn **before, rtx_insn **after)
3137 {
3138 bool res = false;
3139
3140 while (process_address_1 (nop, check_only_p, before, after))
3141 {
3142 if (check_only_p)
3143 return true;
3144 res = true;
3145 }
3146 return res;
3147 }
3148
3149 /* Emit insns to reload VALUE into a new register. VALUE is an
3150 auto-increment or auto-decrement RTX whose operand is a register or
3151 memory location; so reloading involves incrementing that location.
3152 IN is either identical to VALUE, or some cheaper place to reload
3153 value being incremented/decremented from.
3154
3155 INC_AMOUNT is the number to increment or decrement by (always
3156 positive and ignored for POST_MODIFY/PRE_MODIFY).
3157
3158 Return pseudo containing the result. */
3159 static rtx
3160 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3161 {
3162 /* REG or MEM to be copied and incremented. */
3163 rtx incloc = XEXP (value, 0);
3164 /* Nonzero if increment after copying. */
3165 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3166 || GET_CODE (value) == POST_MODIFY);
3167 rtx_insn *last;
3168 rtx inc;
3169 rtx_insn *add_insn;
3170 int code;
3171 rtx real_in = in == value ? incloc : in;
3172 rtx result;
3173 bool plus_p = true;
3174
3175 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3176 {
3177 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3178 || GET_CODE (XEXP (value, 1)) == MINUS);
3179 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3180 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3181 inc = XEXP (XEXP (value, 1), 1);
3182 }
3183 else
3184 {
3185 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3186 inc_amount = -inc_amount;
3187
3188 inc = GEN_INT (inc_amount);
3189 }
3190
3191 if (! post && REG_P (incloc))
3192 result = incloc;
3193 else
3194 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3195 "INC/DEC result");
3196
3197 if (real_in != result)
3198 {
3199 /* First copy the location to the result register. */
3200 lra_assert (REG_P (result));
3201 emit_insn (gen_move_insn (result, real_in));
3202 }
3203
3204 /* We suppose that there are insns to add/sub with the constant
3205 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3206 old reload worked with this assumption. If the assumption
3207 becomes wrong, we should use approach in function
3208 base_plus_disp_to_reg. */
3209 if (in == value)
3210 {
3211 /* See if we can directly increment INCLOC. */
3212 last = get_last_insn ();
3213 add_insn = emit_insn (plus_p
3214 ? gen_add2_insn (incloc, inc)
3215 : gen_sub2_insn (incloc, inc));
3216
3217 code = recog_memoized (add_insn);
3218 if (code >= 0)
3219 {
3220 if (! post && result != incloc)
3221 emit_insn (gen_move_insn (result, incloc));
3222 return result;
3223 }
3224 delete_insns_since (last);
3225 }
3226
3227 /* If couldn't do the increment directly, must increment in RESULT.
3228 The way we do this depends on whether this is pre- or
3229 post-increment. For pre-increment, copy INCLOC to the reload
3230 register, increment it there, then save back. */
3231 if (! post)
3232 {
3233 if (real_in != result)
3234 emit_insn (gen_move_insn (result, real_in));
3235 if (plus_p)
3236 emit_insn (gen_add2_insn (result, inc));
3237 else
3238 emit_insn (gen_sub2_insn (result, inc));
3239 if (result != incloc)
3240 emit_insn (gen_move_insn (incloc, result));
3241 }
3242 else
3243 {
3244 /* Post-increment.
3245
3246 Because this might be a jump insn or a compare, and because
3247 RESULT may not be available after the insn in an input
3248 reload, we must do the incrementing before the insn being
3249 reloaded for.
3250
3251 We have already copied IN to RESULT. Increment the copy in
3252 RESULT, save that back, then decrement RESULT so it has
3253 the original value. */
3254 if (plus_p)
3255 emit_insn (gen_add2_insn (result, inc));
3256 else
3257 emit_insn (gen_sub2_insn (result, inc));
3258 emit_insn (gen_move_insn (incloc, result));
3259 /* Restore non-modified value for the result. We prefer this
3260 way because it does not require an additional hard
3261 register. */
3262 if (plus_p)
3263 {
3264 if (CONST_INT_P (inc))
3265 emit_insn (gen_add2_insn (result,
3266 gen_int_mode (-INTVAL (inc),
3267 GET_MODE (result))));
3268 else
3269 emit_insn (gen_sub2_insn (result, inc));
3270 }
3271 else
3272 emit_insn (gen_add2_insn (result, inc));
3273 }
3274 return result;
3275 }
3276
3277 /* Return true if the current move insn does not need processing as we
3278 already know that it satisfies its constraints. */
3279 static bool
3280 simple_move_p (void)
3281 {
3282 rtx dest, src;
3283 enum reg_class dclass, sclass;
3284
3285 lra_assert (curr_insn_set != NULL_RTX);
3286 dest = SET_DEST (curr_insn_set);
3287 src = SET_SRC (curr_insn_set);
3288 return ((dclass = get_op_class (dest)) != NO_REGS
3289 && (sclass = get_op_class (src)) != NO_REGS
3290 /* The backend guarantees that register moves of cost 2
3291 never need reloads. */
3292 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3293 }
3294
3295 /* Swap operands NOP and NOP + 1. */
3296 static inline void
3297 swap_operands (int nop)
3298 {
3299 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3300 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3301 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3302 /* Swap the duplicates too. */
3303 lra_update_dup (curr_id, nop);
3304 lra_update_dup (curr_id, nop + 1);
3305 }
3306
3307 /* Main entry point of the constraint code: search the body of the
3308 current insn to choose the best alternative. It is mimicking insn
3309 alternative cost calculation model of former reload pass. That is
3310 because machine descriptions were written to use this model. This
3311 model can be changed in future. Make commutative operand exchange
3312 if it is chosen.
3313
3314 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3315 constraints. Return true if any change happened during function
3316 call.
3317
3318 If CHECK_ONLY_P is true then don't do any transformation. Just
3319 check that the insn satisfies all constraints. If the insn does
3320 not satisfy any constraint, return true. */
3321 static bool
3322 curr_insn_transform (bool check_only_p)
3323 {
3324 int i, j, k;
3325 int n_operands;
3326 int n_alternatives;
3327 int commutative;
3328 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3329 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3330 rtx_insn *before, *after;
3331 bool alt_p = false;
3332 /* Flag that the insn has been changed through a transformation. */
3333 bool change_p;
3334 bool sec_mem_p;
3335 #ifdef SECONDARY_MEMORY_NEEDED
3336 bool use_sec_mem_p;
3337 #endif
3338 int max_regno_before;
3339 int reused_alternative_num;
3340
3341 curr_insn_set = single_set (curr_insn);
3342 if (curr_insn_set != NULL_RTX && simple_move_p ())
3343 return false;
3344
3345 no_input_reloads_p = no_output_reloads_p = false;
3346 goal_alt_number = -1;
3347 change_p = sec_mem_p = false;
3348 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3349 reloads; neither are insns that SET cc0. Insns that use CC0 are
3350 not allowed to have any input reloads. */
3351 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3352 no_output_reloads_p = true;
3353
3354 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3355 no_input_reloads_p = true;
3356 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3357 no_output_reloads_p = true;
3358
3359 n_operands = curr_static_id->n_operands;
3360 n_alternatives = curr_static_id->n_alternatives;
3361
3362 /* Just return "no reloads" if insn has no operands with
3363 constraints. */
3364 if (n_operands == 0 || n_alternatives == 0)
3365 return false;
3366
3367 max_regno_before = max_reg_num ();
3368
3369 for (i = 0; i < n_operands; i++)
3370 {
3371 goal_alt_matched[i][0] = -1;
3372 goal_alt_matches[i] = -1;
3373 }
3374
3375 commutative = curr_static_id->commutative;
3376
3377 /* Now see what we need for pseudos that didn't get hard regs or got
3378 the wrong kind of hard reg. For this, we must consider all the
3379 operands together against the register constraints. */
3380
3381 best_losers = best_overall = INT_MAX;
3382 best_reload_sum = 0;
3383
3384 curr_swapped = false;
3385 goal_alt_swapped = false;
3386
3387 if (! check_only_p)
3388 /* Make equivalence substitution and memory subreg elimination
3389 before address processing because an address legitimacy can
3390 depend on memory mode. */
3391 for (i = 0; i < n_operands; i++)
3392 {
3393 rtx op = *curr_id->operand_loc[i];
3394 rtx subst, old = op;
3395 bool op_change_p = false;
3396
3397 if (GET_CODE (old) == SUBREG)
3398 old = SUBREG_REG (old);
3399 subst = get_equiv_with_elimination (old, curr_insn);
3400 original_subreg_reg_mode[i] = VOIDmode;
3401 if (subst != old)
3402 {
3403 subst = copy_rtx (subst);
3404 lra_assert (REG_P (old));
3405 if (GET_CODE (op) != SUBREG)
3406 *curr_id->operand_loc[i] = subst;
3407 else
3408 {
3409 SUBREG_REG (op) = subst;
3410 if (GET_MODE (subst) == VOIDmode)
3411 original_subreg_reg_mode[i] = GET_MODE (old);
3412 }
3413 if (lra_dump_file != NULL)
3414 {
3415 fprintf (lra_dump_file,
3416 "Changing pseudo %d in operand %i of insn %u on equiv ",
3417 REGNO (old), i, INSN_UID (curr_insn));
3418 dump_value_slim (lra_dump_file, subst, 1);
3419 fprintf (lra_dump_file, "\n");
3420 }
3421 op_change_p = change_p = true;
3422 }
3423 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3424 {
3425 change_p = true;
3426 lra_update_dup (curr_id, i);
3427 }
3428 }
3429
3430 /* Reload address registers and displacements. We do it before
3431 finding an alternative because of memory constraints. */
3432 before = after = NULL;
3433 for (i = 0; i < n_operands; i++)
3434 if (! curr_static_id->operand[i].is_operator
3435 && process_address (i, check_only_p, &before, &after))
3436 {
3437 if (check_only_p)
3438 return true;
3439 change_p = true;
3440 lra_update_dup (curr_id, i);
3441 }
3442
3443 if (change_p)
3444 /* If we've changed the instruction then any alternative that
3445 we chose previously may no longer be valid. */
3446 lra_set_used_insn_alternative (curr_insn, -1);
3447
3448 if (! check_only_p && curr_insn_set != NULL_RTX
3449 && check_and_process_move (&change_p, &sec_mem_p))
3450 return change_p;
3451
3452 try_swapped:
3453
3454 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3455 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3456 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3457 reused_alternative_num, INSN_UID (curr_insn));
3458
3459 if (process_alt_operands (reused_alternative_num))
3460 alt_p = true;
3461
3462 if (check_only_p)
3463 return ! alt_p || best_losers != 0;
3464
3465 /* If insn is commutative (it's safe to exchange a certain pair of
3466 operands) then we need to try each alternative twice, the second
3467 time matching those two operands as if we had exchanged them. To
3468 do this, really exchange them in operands.
3469
3470 If we have just tried the alternatives the second time, return
3471 operands to normal and drop through. */
3472
3473 if (reused_alternative_num < 0 && commutative >= 0)
3474 {
3475 curr_swapped = !curr_swapped;
3476 if (curr_swapped)
3477 {
3478 swap_operands (commutative);
3479 goto try_swapped;
3480 }
3481 else
3482 swap_operands (commutative);
3483 }
3484
3485 if (! alt_p && ! sec_mem_p)
3486 {
3487 /* No alternative works with reloads?? */
3488 if (INSN_CODE (curr_insn) >= 0)
3489 fatal_insn ("unable to generate reloads for:", curr_insn);
3490 error_for_asm (curr_insn,
3491 "inconsistent operand constraints in an %<asm%>");
3492 /* Avoid further trouble with this insn. */
3493 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3494 lra_invalidate_insn_data (curr_insn);
3495 return true;
3496 }
3497
3498 /* If the best alternative is with operands 1 and 2 swapped, swap
3499 them. Update the operand numbers of any reloads already
3500 pushed. */
3501
3502 if (goal_alt_swapped)
3503 {
3504 if (lra_dump_file != NULL)
3505 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3506 INSN_UID (curr_insn));
3507
3508 /* Swap the duplicates too. */
3509 swap_operands (commutative);
3510 change_p = true;
3511 }
3512
3513 #ifdef SECONDARY_MEMORY_NEEDED
3514 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3515 too conservatively. So we use the secondary memory only if there
3516 is no any alternative without reloads. */
3517 use_sec_mem_p = false;
3518 if (! alt_p)
3519 use_sec_mem_p = true;
3520 else if (sec_mem_p)
3521 {
3522 for (i = 0; i < n_operands; i++)
3523 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3524 break;
3525 use_sec_mem_p = i < n_operands;
3526 }
3527
3528 if (use_sec_mem_p)
3529 {
3530 rtx new_reg, src, dest, rld;
3531 machine_mode sec_mode, rld_mode;
3532
3533 lra_assert (sec_mem_p);
3534 lra_assert (curr_static_id->operand[0].type == OP_OUT
3535 && curr_static_id->operand[1].type == OP_IN);
3536 dest = *curr_id->operand_loc[0];
3537 src = *curr_id->operand_loc[1];
3538 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3539 ? dest : src);
3540 rld_mode = GET_MODE (rld);
3541 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3542 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3543 #else
3544 sec_mode = rld_mode;
3545 #endif
3546 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3547 NO_REGS, "secondary");
3548 /* If the mode is changed, it should be wider. */
3549 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3550 if (sec_mode != rld_mode)
3551 {
3552 /* If the target says specifically to use another mode for
3553 secondary memory moves we can not reuse the original
3554 insn. */
3555 after = emit_spill_move (false, new_reg, dest);
3556 lra_process_new_insns (curr_insn, NULL, after,
3557 "Inserting the sec. move");
3558 /* We may have non null BEFORE here (e.g. after address
3559 processing. */
3560 push_to_sequence (before);
3561 before = emit_spill_move (true, new_reg, src);
3562 emit_insn (before);
3563 before = get_insns ();
3564 end_sequence ();
3565 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3566 lra_set_insn_deleted (curr_insn);
3567 }
3568 else if (dest == rld)
3569 {
3570 *curr_id->operand_loc[0] = new_reg;
3571 after = emit_spill_move (false, new_reg, dest);
3572 lra_process_new_insns (curr_insn, NULL, after,
3573 "Inserting the sec. move");
3574 }
3575 else
3576 {
3577 *curr_id->operand_loc[1] = new_reg;
3578 /* See comments above. */
3579 push_to_sequence (before);
3580 before = emit_spill_move (true, new_reg, src);
3581 emit_insn (before);
3582 before = get_insns ();
3583 end_sequence ();
3584 lra_process_new_insns (curr_insn, before, NULL,
3585 "Inserting the sec. move");
3586 }
3587 lra_update_insn_regno_info (curr_insn);
3588 return true;
3589 }
3590 #endif
3591
3592 lra_assert (goal_alt_number >= 0);
3593 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3594
3595 if (lra_dump_file != NULL)
3596 {
3597 const char *p;
3598
3599 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3600 goal_alt_number, INSN_UID (curr_insn));
3601 for (i = 0; i < n_operands; i++)
3602 {
3603 p = (curr_static_id->operand_alternative
3604 [goal_alt_number * n_operands + i].constraint);
3605 if (*p == '\0')
3606 continue;
3607 fprintf (lra_dump_file, " (%d) ", i);
3608 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3609 fputc (*p, lra_dump_file);
3610 }
3611 if (INSN_CODE (curr_insn) >= 0
3612 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3613 fprintf (lra_dump_file, " {%s}", p);
3614 if (curr_id->sp_offset != 0)
3615 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3616 curr_id->sp_offset);
3617 fprintf (lra_dump_file, "\n");
3618 }
3619
3620 /* Right now, for any pair of operands I and J that are required to
3621 match, with J < I, goal_alt_matches[I] is J. Add I to
3622 goal_alt_matched[J]. */
3623
3624 for (i = 0; i < n_operands; i++)
3625 if ((j = goal_alt_matches[i]) >= 0)
3626 {
3627 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3628 ;
3629 /* We allow matching one output operand and several input
3630 operands. */
3631 lra_assert (k == 0
3632 || (curr_static_id->operand[j].type == OP_OUT
3633 && curr_static_id->operand[i].type == OP_IN
3634 && (curr_static_id->operand
3635 [goal_alt_matched[j][0]].type == OP_IN)));
3636 goal_alt_matched[j][k] = i;
3637 goal_alt_matched[j][k + 1] = -1;
3638 }
3639
3640 for (i = 0; i < n_operands; i++)
3641 goal_alt_win[i] |= goal_alt_match_win[i];
3642
3643 /* Any constants that aren't allowed and can't be reloaded into
3644 registers are here changed into memory references. */
3645 for (i = 0; i < n_operands; i++)
3646 if (goal_alt_win[i])
3647 {
3648 int regno;
3649 enum reg_class new_class;
3650 rtx reg = *curr_id->operand_loc[i];
3651
3652 if (GET_CODE (reg) == SUBREG)
3653 reg = SUBREG_REG (reg);
3654
3655 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3656 {
3657 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3658
3659 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3660 {
3661 lra_assert (ok_p);
3662 lra_change_class (regno, new_class, " Change to", true);
3663 }
3664 }
3665 }
3666 else
3667 {
3668 const char *constraint;
3669 char c;
3670 rtx op = *curr_id->operand_loc[i];
3671 rtx subreg = NULL_RTX;
3672 machine_mode mode = curr_operand_mode[i];
3673
3674 if (GET_CODE (op) == SUBREG)
3675 {
3676 subreg = op;
3677 op = SUBREG_REG (op);
3678 mode = GET_MODE (op);
3679 }
3680
3681 if (CONST_POOL_OK_P (mode, op)
3682 && ((targetm.preferred_reload_class
3683 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3684 || no_input_reloads_p))
3685 {
3686 rtx tem = force_const_mem (mode, op);
3687
3688 change_p = true;
3689 if (subreg != NULL_RTX)
3690 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3691
3692 *curr_id->operand_loc[i] = tem;
3693 lra_update_dup (curr_id, i);
3694 process_address (i, false, &before, &after);
3695
3696 /* If the alternative accepts constant pool refs directly
3697 there will be no reload needed at all. */
3698 if (subreg != NULL_RTX)
3699 continue;
3700 /* Skip alternatives before the one requested. */
3701 constraint = (curr_static_id->operand_alternative
3702 [goal_alt_number * n_operands + i].constraint);
3703 for (;
3704 (c = *constraint) && c != ',' && c != '#';
3705 constraint += CONSTRAINT_LEN (c, constraint))
3706 {
3707 enum constraint_num cn = lookup_constraint (constraint);
3708 if (insn_extra_memory_constraint (cn)
3709 && satisfies_memory_constraint_p (tem, cn))
3710 break;
3711 }
3712 if (c == '\0' || c == ',' || c == '#')
3713 continue;
3714
3715 goal_alt_win[i] = true;
3716 }
3717 }
3718
3719 for (i = 0; i < n_operands; i++)
3720 {
3721 int regno;
3722 bool optional_p = false;
3723 rtx old, new_reg;
3724 rtx op = *curr_id->operand_loc[i];
3725
3726 if (goal_alt_win[i])
3727 {
3728 if (goal_alt[i] == NO_REGS
3729 && REG_P (op)
3730 /* When we assign NO_REGS it means that we will not
3731 assign a hard register to the scratch pseudo by
3732 assigment pass and the scratch pseudo will be
3733 spilled. Spilled scratch pseudos are transformed
3734 back to scratches at the LRA end. */
3735 && lra_former_scratch_operand_p (curr_insn, i))
3736 {
3737 int regno = REGNO (op);
3738 lra_change_class (regno, NO_REGS, " Change to", true);
3739 if (lra_get_regno_hard_regno (regno) >= 0)
3740 /* We don't have to mark all insn affected by the
3741 spilled pseudo as there is only one such insn, the
3742 current one. */
3743 reg_renumber[regno] = -1;
3744 }
3745 /* We can do an optional reload. If the pseudo got a hard
3746 reg, we might improve the code through inheritance. If
3747 it does not get a hard register we coalesce memory/memory
3748 moves later. Ignore move insns to avoid cycling. */
3749 if (! lra_simple_p
3750 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3751 && goal_alt[i] != NO_REGS && REG_P (op)
3752 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3753 && regno < new_regno_start
3754 && ! lra_former_scratch_p (regno)
3755 && reg_renumber[regno] < 0
3756 /* Check that the optional reload pseudo will be able to
3757 hold given mode value. */
3758 && ! (prohibited_class_reg_set_mode_p
3759 (goal_alt[i], reg_class_contents[goal_alt[i]],
3760 PSEUDO_REGNO_MODE (regno)))
3761 && (curr_insn_set == NULL_RTX
3762 || !((REG_P (SET_SRC (curr_insn_set))
3763 || MEM_P (SET_SRC (curr_insn_set))
3764 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3765 && (REG_P (SET_DEST (curr_insn_set))
3766 || MEM_P (SET_DEST (curr_insn_set))
3767 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3768 optional_p = true;
3769 else
3770 continue;
3771 }
3772
3773 /* Operands that match previous ones have already been handled. */
3774 if (goal_alt_matches[i] >= 0)
3775 continue;
3776
3777 /* We should not have an operand with a non-offsettable address
3778 appearing where an offsettable address will do. It also may
3779 be a case when the address should be special in other words
3780 not a general one (e.g. it needs no index reg). */
3781 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3782 {
3783 enum reg_class rclass;
3784 rtx *loc = &XEXP (op, 0);
3785 enum rtx_code code = GET_CODE (*loc);
3786
3787 push_to_sequence (before);
3788 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3789 MEM, SCRATCH);
3790 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3791 new_reg = emit_inc (rclass, *loc, *loc,
3792 /* This value does not matter for MODIFY. */
3793 GET_MODE_SIZE (GET_MODE (op)));
3794 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3795 "offsetable address", &new_reg))
3796 lra_emit_move (new_reg, *loc);
3797 before = get_insns ();
3798 end_sequence ();
3799 *loc = new_reg;
3800 lra_update_dup (curr_id, i);
3801 }
3802 else if (goal_alt_matched[i][0] == -1)
3803 {
3804 machine_mode mode;
3805 rtx reg, *loc;
3806 int hard_regno, byte;
3807 enum op_type type = curr_static_id->operand[i].type;
3808
3809 loc = curr_id->operand_loc[i];
3810 mode = curr_operand_mode[i];
3811 if (GET_CODE (*loc) == SUBREG)
3812 {
3813 reg = SUBREG_REG (*loc);
3814 byte = SUBREG_BYTE (*loc);
3815 if (REG_P (reg)
3816 /* Strict_low_part requires reload the register not
3817 the sub-register. */
3818 && (curr_static_id->operand[i].strict_low
3819 || (GET_MODE_SIZE (mode)
3820 <= GET_MODE_SIZE (GET_MODE (reg))
3821 && (hard_regno
3822 = get_try_hard_regno (REGNO (reg))) >= 0
3823 && (simplify_subreg_regno
3824 (hard_regno,
3825 GET_MODE (reg), byte, mode) < 0)
3826 && (goal_alt[i] == NO_REGS
3827 || (simplify_subreg_regno
3828 (ira_class_hard_regs[goal_alt[i]][0],
3829 GET_MODE (reg), byte, mode) >= 0)))))
3830 {
3831 if (type == OP_OUT)
3832 type = OP_INOUT;
3833 loc = &SUBREG_REG (*loc);
3834 mode = GET_MODE (*loc);
3835 }
3836 }
3837 old = *loc;
3838 if (get_reload_reg (type, mode, old, goal_alt[i],
3839 loc != curr_id->operand_loc[i], "", &new_reg)
3840 && type != OP_OUT)
3841 {
3842 push_to_sequence (before);
3843 lra_emit_move (new_reg, old);
3844 before = get_insns ();
3845 end_sequence ();
3846 }
3847 *loc = new_reg;
3848 if (type != OP_IN
3849 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3850 {
3851 start_sequence ();
3852 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3853 emit_insn (after);
3854 after = get_insns ();
3855 end_sequence ();
3856 *loc = new_reg;
3857 }
3858 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3859 if (goal_alt_dont_inherit_ops[j] == i)
3860 {
3861 lra_set_regno_unique_value (REGNO (new_reg));
3862 break;
3863 }
3864 lra_update_dup (curr_id, i);
3865 }
3866 else if (curr_static_id->operand[i].type == OP_IN
3867 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3868 == OP_OUT))
3869 {
3870 /* generate reloads for input and matched outputs. */
3871 match_inputs[0] = i;
3872 match_inputs[1] = -1;
3873 match_reload (goal_alt_matched[i][0], match_inputs,
3874 goal_alt[i], &before, &after,
3875 curr_static_id->operand_alternative
3876 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
3877 .earlyclobber);
3878 }
3879 else if (curr_static_id->operand[i].type == OP_OUT
3880 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3881 == OP_IN))
3882 /* Generate reloads for output and matched inputs. */
3883 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after,
3884 curr_static_id->operand_alternative
3885 [goal_alt_number * n_operands + i].earlyclobber);
3886 else if (curr_static_id->operand[i].type == OP_IN
3887 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3888 == OP_IN))
3889 {
3890 /* Generate reloads for matched inputs. */
3891 match_inputs[0] = i;
3892 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3893 match_inputs[j + 1] = k;
3894 match_inputs[j + 1] = -1;
3895 match_reload (-1, match_inputs, goal_alt[i], &before, &after, false);
3896 }
3897 else
3898 /* We must generate code in any case when function
3899 process_alt_operands decides that it is possible. */
3900 gcc_unreachable ();
3901 if (optional_p)
3902 {
3903 lra_assert (REG_P (op));
3904 regno = REGNO (op);
3905 op = *curr_id->operand_loc[i]; /* Substitution. */
3906 if (GET_CODE (op) == SUBREG)
3907 op = SUBREG_REG (op);
3908 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3909 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3910 lra_reg_info[REGNO (op)].restore_regno = regno;
3911 if (lra_dump_file != NULL)
3912 fprintf (lra_dump_file,
3913 " Making reload reg %d for reg %d optional\n",
3914 REGNO (op), regno);
3915 }
3916 }
3917 if (before != NULL_RTX || after != NULL_RTX
3918 || max_regno_before != max_reg_num ())
3919 change_p = true;
3920 if (change_p)
3921 {
3922 lra_update_operator_dups (curr_id);
3923 /* Something changes -- process the insn. */
3924 lra_update_insn_regno_info (curr_insn);
3925 }
3926 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3927 return change_p;
3928 }
3929
3930 /* Return true if INSN satisfies all constraints. In other words, no
3931 reload insns are needed. */
3932 bool
3933 lra_constrain_insn (rtx_insn *insn)
3934 {
3935 int saved_new_regno_start = new_regno_start;
3936 int saved_new_insn_uid_start = new_insn_uid_start;
3937 bool change_p;
3938
3939 curr_insn = insn;
3940 curr_id = lra_get_insn_recog_data (curr_insn);
3941 curr_static_id = curr_id->insn_static_data;
3942 new_insn_uid_start = get_max_uid ();
3943 new_regno_start = max_reg_num ();
3944 change_p = curr_insn_transform (true);
3945 new_regno_start = saved_new_regno_start;
3946 new_insn_uid_start = saved_new_insn_uid_start;
3947 return ! change_p;
3948 }
3949
3950 /* Return true if X is in LIST. */
3951 static bool
3952 in_list_p (rtx x, rtx list)
3953 {
3954 for (; list != NULL_RTX; list = XEXP (list, 1))
3955 if (XEXP (list, 0) == x)
3956 return true;
3957 return false;
3958 }
3959
3960 /* Return true if X contains an allocatable hard register (if
3961 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3962 static bool
3963 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3964 {
3965 int i, j;
3966 const char *fmt;
3967 enum rtx_code code;
3968
3969 code = GET_CODE (x);
3970 if (REG_P (x))
3971 {
3972 int regno = REGNO (x);
3973 HARD_REG_SET alloc_regs;
3974
3975 if (hard_reg_p)
3976 {
3977 if (regno >= FIRST_PSEUDO_REGISTER)
3978 regno = lra_get_regno_hard_regno (regno);
3979 if (regno < 0)
3980 return false;
3981 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3982 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3983 }
3984 else
3985 {
3986 if (regno < FIRST_PSEUDO_REGISTER)
3987 return false;
3988 if (! spilled_p)
3989 return true;
3990 return lra_get_regno_hard_regno (regno) < 0;
3991 }
3992 }
3993 fmt = GET_RTX_FORMAT (code);
3994 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3995 {
3996 if (fmt[i] == 'e')
3997 {
3998 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3999 return true;
4000 }
4001 else if (fmt[i] == 'E')
4002 {
4003 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4004 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4005 return true;
4006 }
4007 }
4008 return false;
4009 }
4010
4011 /* Return true if X contains a symbol reg. */
4012 static bool
4013 contains_symbol_ref_p (rtx x)
4014 {
4015 int i, j;
4016 const char *fmt;
4017 enum rtx_code code;
4018
4019 code = GET_CODE (x);
4020 if (code == SYMBOL_REF)
4021 return true;
4022 fmt = GET_RTX_FORMAT (code);
4023 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4024 {
4025 if (fmt[i] == 'e')
4026 {
4027 if (contains_symbol_ref_p (XEXP (x, i)))
4028 return true;
4029 }
4030 else if (fmt[i] == 'E')
4031 {
4032 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4033 if (contains_symbol_ref_p (XVECEXP (x, i, j)))
4034 return true;
4035 }
4036 }
4037 return false;
4038 }
4039
4040 /* Process all regs in location *LOC and change them on equivalent
4041 substitution. Return true if any change was done. */
4042 static bool
4043 loc_equivalence_change_p (rtx *loc)
4044 {
4045 rtx subst, reg, x = *loc;
4046 bool result = false;
4047 enum rtx_code code = GET_CODE (x);
4048 const char *fmt;
4049 int i, j;
4050
4051 if (code == SUBREG)
4052 {
4053 reg = SUBREG_REG (x);
4054 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4055 && GET_MODE (subst) == VOIDmode)
4056 {
4057 /* We cannot reload debug location. Simplify subreg here
4058 while we know the inner mode. */
4059 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4060 GET_MODE (reg), SUBREG_BYTE (x));
4061 return true;
4062 }
4063 }
4064 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4065 {
4066 *loc = subst;
4067 return true;
4068 }
4069
4070 /* Scan all the operand sub-expressions. */
4071 fmt = GET_RTX_FORMAT (code);
4072 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4073 {
4074 if (fmt[i] == 'e')
4075 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4076 else if (fmt[i] == 'E')
4077 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4078 result
4079 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4080 }
4081 return result;
4082 }
4083
4084 /* Similar to loc_equivalence_change_p, but for use as
4085 simplify_replace_fn_rtx callback. DATA is insn for which the
4086 elimination is done. If it null we don't do the elimination. */
4087 static rtx
4088 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4089 {
4090 if (!REG_P (loc))
4091 return NULL_RTX;
4092
4093 rtx subst = (data == NULL
4094 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4095 if (subst != loc)
4096 return subst;
4097
4098 return NULL_RTX;
4099 }
4100
4101 /* Maximum number of generated reload insns per an insn. It is for
4102 preventing this pass cycling in a bug case. */
4103 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4104
4105 /* The current iteration number of this LRA pass. */
4106 int lra_constraint_iter;
4107
4108 /* True if we substituted equiv which needs checking register
4109 allocation correctness because the equivalent value contains
4110 allocatable hard registers or when we restore multi-register
4111 pseudo. */
4112 bool lra_risky_transformations_p;
4113
4114 /* Return true if REGNO is referenced in more than one block. */
4115 static bool
4116 multi_block_pseudo_p (int regno)
4117 {
4118 basic_block bb = NULL;
4119 unsigned int uid;
4120 bitmap_iterator bi;
4121
4122 if (regno < FIRST_PSEUDO_REGISTER)
4123 return false;
4124
4125 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4126 if (bb == NULL)
4127 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4128 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4129 return true;
4130 return false;
4131 }
4132
4133 /* Return true if LIST contains a deleted insn. */
4134 static bool
4135 contains_deleted_insn_p (rtx_insn_list *list)
4136 {
4137 for (; list != NULL_RTX; list = list->next ())
4138 if (NOTE_P (list->insn ())
4139 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4140 return true;
4141 return false;
4142 }
4143
4144 /* Return true if X contains a pseudo dying in INSN. */
4145 static bool
4146 dead_pseudo_p (rtx x, rtx_insn *insn)
4147 {
4148 int i, j;
4149 const char *fmt;
4150 enum rtx_code code;
4151
4152 if (REG_P (x))
4153 return (insn != NULL_RTX
4154 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4155 code = GET_CODE (x);
4156 fmt = GET_RTX_FORMAT (code);
4157 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4158 {
4159 if (fmt[i] == 'e')
4160 {
4161 if (dead_pseudo_p (XEXP (x, i), insn))
4162 return true;
4163 }
4164 else if (fmt[i] == 'E')
4165 {
4166 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4167 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4168 return true;
4169 }
4170 }
4171 return false;
4172 }
4173
4174 /* Return true if INSN contains a dying pseudo in INSN right hand
4175 side. */
4176 static bool
4177 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4178 {
4179 rtx set = single_set (insn);
4180
4181 gcc_assert (set != NULL);
4182 return dead_pseudo_p (SET_SRC (set), insn);
4183 }
4184
4185 /* Return true if any init insn of REGNO contains a dying pseudo in
4186 insn right hand side. */
4187 static bool
4188 init_insn_rhs_dead_pseudo_p (int regno)
4189 {
4190 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4191
4192 if (insns == NULL)
4193 return false;
4194 for (; insns != NULL_RTX; insns = insns->next ())
4195 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4196 return true;
4197 return false;
4198 }
4199
4200 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4201 reverse only if we have one init insn with given REGNO as a
4202 source. */
4203 static bool
4204 reverse_equiv_p (int regno)
4205 {
4206 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4207 rtx set;
4208
4209 if (insns == NULL)
4210 return false;
4211 if (! INSN_P (insns->insn ())
4212 || insns->next () != NULL)
4213 return false;
4214 if ((set = single_set (insns->insn ())) == NULL_RTX)
4215 return false;
4216 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4217 }
4218
4219 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4220 call this function only for non-reverse equivalence. */
4221 static bool
4222 contains_reloaded_insn_p (int regno)
4223 {
4224 rtx set;
4225 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4226
4227 for (; list != NULL; list = list->next ())
4228 if ((set = single_set (list->insn ())) == NULL_RTX
4229 || ! REG_P (SET_DEST (set))
4230 || (int) REGNO (SET_DEST (set)) != regno)
4231 return true;
4232 return false;
4233 }
4234
4235 /* Entry function of LRA constraint pass. Return true if the
4236 constraint pass did change the code. */
4237 bool
4238 lra_constraints (bool first_p)
4239 {
4240 bool changed_p;
4241 int i, hard_regno, new_insns_num;
4242 unsigned int min_len, new_min_len, uid;
4243 rtx set, x, reg, dest_reg;
4244 basic_block last_bb;
4245 bitmap_head equiv_insn_bitmap;
4246 bitmap_iterator bi;
4247
4248 lra_constraint_iter++;
4249 if (lra_dump_file != NULL)
4250 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4251 lra_constraint_iter);
4252 changed_p = false;
4253 if (pic_offset_table_rtx
4254 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4255 lra_risky_transformations_p = true;
4256 else
4257 lra_risky_transformations_p = false;
4258 new_insn_uid_start = get_max_uid ();
4259 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4260 /* Mark used hard regs for target stack size calulations. */
4261 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4262 if (lra_reg_info[i].nrefs != 0
4263 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4264 {
4265 int j, nregs;
4266
4267 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4268 for (j = 0; j < nregs; j++)
4269 df_set_regs_ever_live (hard_regno + j, true);
4270 }
4271 /* Do elimination before the equivalence processing as we can spill
4272 some pseudos during elimination. */
4273 lra_eliminate (false, first_p);
4274 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4275 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4276 if (lra_reg_info[i].nrefs != 0)
4277 {
4278 ira_reg_equiv[i].profitable_p = true;
4279 reg = regno_reg_rtx[i];
4280 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4281 {
4282 bool pseudo_p = contains_reg_p (x, false, false);
4283
4284 /* After RTL transformation, we can not guarantee that
4285 pseudo in the substitution was not reloaded which might
4286 make equivalence invalid. For example, in reverse
4287 equiv of p0
4288
4289 p0 <- ...
4290 ...
4291 equiv_mem <- p0
4292
4293 the memory address register was reloaded before the 2nd
4294 insn. */
4295 if ((! first_p && pseudo_p)
4296 /* We don't use DF for compilation speed sake. So it
4297 is problematic to update live info when we use an
4298 equivalence containing pseudos in more than one
4299 BB. */
4300 || (pseudo_p && multi_block_pseudo_p (i))
4301 /* If an init insn was deleted for some reason, cancel
4302 the equiv. We could update the equiv insns after
4303 transformations including an equiv insn deletion
4304 but it is not worthy as such cases are extremely
4305 rare. */
4306 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4307 /* If it is not a reverse equivalence, we check that a
4308 pseudo in rhs of the init insn is not dying in the
4309 insn. Otherwise, the live info at the beginning of
4310 the corresponding BB might be wrong after we
4311 removed the insn. When the equiv can be a
4312 constant, the right hand side of the init insn can
4313 be a pseudo. */
4314 || (! reverse_equiv_p (i)
4315 && (init_insn_rhs_dead_pseudo_p (i)
4316 /* If we reloaded the pseudo in an equivalence
4317 init insn, we can not remove the equiv init
4318 insns and the init insns might write into
4319 const memory in this case. */
4320 || contains_reloaded_insn_p (i)))
4321 /* Prevent access beyond equivalent memory for
4322 paradoxical subregs. */
4323 || (MEM_P (x)
4324 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4325 > GET_MODE_SIZE (GET_MODE (x))))
4326 || (pic_offset_table_rtx
4327 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4328 && (targetm.preferred_reload_class
4329 (x, lra_get_allocno_class (i)) == NO_REGS))
4330 || contains_symbol_ref_p (x))))
4331 ira_reg_equiv[i].defined_p = false;
4332 if (contains_reg_p (x, false, true))
4333 ira_reg_equiv[i].profitable_p = false;
4334 if (get_equiv (reg) != reg)
4335 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4336 }
4337 }
4338 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4339 update_equiv (i);
4340 /* We should add all insns containing pseudos which should be
4341 substituted by their equivalences. */
4342 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4343 lra_push_insn_by_uid (uid);
4344 min_len = lra_insn_stack_length ();
4345 new_insns_num = 0;
4346 last_bb = NULL;
4347 changed_p = false;
4348 while ((new_min_len = lra_insn_stack_length ()) != 0)
4349 {
4350 curr_insn = lra_pop_insn ();
4351 --new_min_len;
4352 curr_bb = BLOCK_FOR_INSN (curr_insn);
4353 if (curr_bb != last_bb)
4354 {
4355 last_bb = curr_bb;
4356 bb_reload_num = lra_curr_reload_num;
4357 }
4358 if (min_len > new_min_len)
4359 {
4360 min_len = new_min_len;
4361 new_insns_num = 0;
4362 }
4363 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4364 internal_error
4365 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4366 MAX_RELOAD_INSNS_NUMBER);
4367 new_insns_num++;
4368 if (DEBUG_INSN_P (curr_insn))
4369 {
4370 /* We need to check equivalence in debug insn and change
4371 pseudo to the equivalent value if necessary. */
4372 curr_id = lra_get_insn_recog_data (curr_insn);
4373 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4374 {
4375 rtx old = *curr_id->operand_loc[0];
4376 *curr_id->operand_loc[0]
4377 = simplify_replace_fn_rtx (old, NULL_RTX,
4378 loc_equivalence_callback, curr_insn);
4379 if (old != *curr_id->operand_loc[0])
4380 {
4381 lra_update_insn_regno_info (curr_insn);
4382 changed_p = true;
4383 }
4384 }
4385 }
4386 else if (INSN_P (curr_insn))
4387 {
4388 if ((set = single_set (curr_insn)) != NULL_RTX)
4389 {
4390 dest_reg = SET_DEST (set);
4391 /* The equivalence pseudo could be set up as SUBREG in a
4392 case when it is a call restore insn in a mode
4393 different from the pseudo mode. */
4394 if (GET_CODE (dest_reg) == SUBREG)
4395 dest_reg = SUBREG_REG (dest_reg);
4396 if ((REG_P (dest_reg)
4397 && (x = get_equiv (dest_reg)) != dest_reg
4398 /* Remove insns which set up a pseudo whose value
4399 can not be changed. Such insns might be not in
4400 init_insns because we don't update equiv data
4401 during insn transformations.
4402
4403 As an example, let suppose that a pseudo got
4404 hard register and on the 1st pass was not
4405 changed to equivalent constant. We generate an
4406 additional insn setting up the pseudo because of
4407 secondary memory movement. Then the pseudo is
4408 spilled and we use the equiv constant. In this
4409 case we should remove the additional insn and
4410 this insn is not init_insns list. */
4411 && (! MEM_P (x) || MEM_READONLY_P (x)
4412 /* Check that this is actually an insn setting
4413 up the equivalence. */
4414 || in_list_p (curr_insn,
4415 ira_reg_equiv
4416 [REGNO (dest_reg)].init_insns)))
4417 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4418 && in_list_p (curr_insn,
4419 ira_reg_equiv
4420 [REGNO (SET_SRC (set))].init_insns)))
4421 {
4422 /* This is equiv init insn of pseudo which did not get a
4423 hard register -- remove the insn. */
4424 if (lra_dump_file != NULL)
4425 {
4426 fprintf (lra_dump_file,
4427 " Removing equiv init insn %i (freq=%d)\n",
4428 INSN_UID (curr_insn),
4429 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4430 dump_insn_slim (lra_dump_file, curr_insn);
4431 }
4432 if (contains_reg_p (x, true, false))
4433 lra_risky_transformations_p = true;
4434 lra_set_insn_deleted (curr_insn);
4435 continue;
4436 }
4437 }
4438 curr_id = lra_get_insn_recog_data (curr_insn);
4439 curr_static_id = curr_id->insn_static_data;
4440 init_curr_insn_input_reloads ();
4441 init_curr_operand_mode ();
4442 if (curr_insn_transform (false))
4443 changed_p = true;
4444 /* Check non-transformed insns too for equiv change as USE
4445 or CLOBBER don't need reloads but can contain pseudos
4446 being changed on their equivalences. */
4447 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4448 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4449 {
4450 lra_update_insn_regno_info (curr_insn);
4451 changed_p = true;
4452 }
4453 }
4454 }
4455 bitmap_clear (&equiv_insn_bitmap);
4456 /* If we used a new hard regno, changed_p should be true because the
4457 hard reg is assigned to a new pseudo. */
4458 #ifdef ENABLE_CHECKING
4459 if (! changed_p)
4460 {
4461 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4462 if (lra_reg_info[i].nrefs != 0
4463 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4464 {
4465 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4466
4467 for (j = 0; j < nregs; j++)
4468 lra_assert (df_regs_ever_live_p (hard_regno + j));
4469 }
4470 }
4471 #endif
4472 return changed_p;
4473 }
4474
4475 /* Initiate the LRA constraint pass. It is done once per
4476 function. */
4477 void
4478 lra_constraints_init (void)
4479 {
4480 }
4481
4482 /* Finalize the LRA constraint pass. It is done once per
4483 function. */
4484 void
4485 lra_constraints_finish (void)
4486 {
4487 }
4488
4489 \f
4490
4491 /* This page contains code to do inheritance/split
4492 transformations. */
4493
4494 /* Number of reloads passed so far in current EBB. */
4495 static int reloads_num;
4496
4497 /* Number of calls passed so far in current EBB. */
4498 static int calls_num;
4499
4500 /* Current reload pseudo check for validity of elements in
4501 USAGE_INSNS. */
4502 static int curr_usage_insns_check;
4503
4504 /* Info about last usage of registers in EBB to do inheritance/split
4505 transformation. Inheritance transformation is done from a spilled
4506 pseudo and split transformations from a hard register or a pseudo
4507 assigned to a hard register. */
4508 struct usage_insns
4509 {
4510 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4511 value INSNS is valid. The insns is chain of optional debug insns
4512 and a finishing non-debug insn using the corresponding reg. The
4513 value is also used to mark the registers which are set up in the
4514 current insn. The negated insn uid is used for this. */
4515 int check;
4516 /* Value of global reloads_num at the last insn in INSNS. */
4517 int reloads_num;
4518 /* Value of global reloads_nums at the last insn in INSNS. */
4519 int calls_num;
4520 /* It can be true only for splitting. And it means that the restore
4521 insn should be put after insn given by the following member. */
4522 bool after_p;
4523 /* Next insns in the current EBB which use the original reg and the
4524 original reg value is not changed between the current insn and
4525 the next insns. In order words, e.g. for inheritance, if we need
4526 to use the original reg value again in the next insns we can try
4527 to use the value in a hard register from a reload insn of the
4528 current insn. */
4529 rtx insns;
4530 };
4531
4532 /* Map: regno -> corresponding pseudo usage insns. */
4533 static struct usage_insns *usage_insns;
4534
4535 static void
4536 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4537 {
4538 usage_insns[regno].check = curr_usage_insns_check;
4539 usage_insns[regno].insns = insn;
4540 usage_insns[regno].reloads_num = reloads_num;
4541 usage_insns[regno].calls_num = calls_num;
4542 usage_insns[regno].after_p = after_p;
4543 }
4544
4545 /* The function is used to form list REGNO usages which consists of
4546 optional debug insns finished by a non-debug insn using REGNO.
4547 RELOADS_NUM is current number of reload insns processed so far. */
4548 static void
4549 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
4550 {
4551 rtx next_usage_insns;
4552
4553 if (usage_insns[regno].check == curr_usage_insns_check
4554 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4555 && DEBUG_INSN_P (insn))
4556 {
4557 /* Check that we did not add the debug insn yet. */
4558 if (next_usage_insns != insn
4559 && (GET_CODE (next_usage_insns) != INSN_LIST
4560 || XEXP (next_usage_insns, 0) != insn))
4561 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4562 next_usage_insns);
4563 }
4564 else if (NONDEBUG_INSN_P (insn))
4565 setup_next_usage_insn (regno, insn, reloads_num, false);
4566 else
4567 usage_insns[regno].check = 0;
4568 }
4569
4570 /* Return first non-debug insn in list USAGE_INSNS. */
4571 static rtx_insn *
4572 skip_usage_debug_insns (rtx usage_insns)
4573 {
4574 rtx insn;
4575
4576 /* Skip debug insns. */
4577 for (insn = usage_insns;
4578 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4579 insn = XEXP (insn, 1))
4580 ;
4581 return safe_as_a <rtx_insn *> (insn);
4582 }
4583
4584 /* Return true if we need secondary memory moves for insn in
4585 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4586 into the insn. */
4587 static bool
4588 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4589 rtx usage_insns ATTRIBUTE_UNUSED)
4590 {
4591 #ifndef SECONDARY_MEMORY_NEEDED
4592 return false;
4593 #else
4594 rtx_insn *insn;
4595 rtx set, dest;
4596 enum reg_class cl;
4597
4598 if (inher_cl == ALL_REGS
4599 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4600 return false;
4601 lra_assert (INSN_P (insn));
4602 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4603 return false;
4604 dest = SET_DEST (set);
4605 if (! REG_P (dest))
4606 return false;
4607 lra_assert (inher_cl != NO_REGS);
4608 cl = get_reg_class (REGNO (dest));
4609 return (cl != NO_REGS && cl != ALL_REGS
4610 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4611 #endif
4612 }
4613
4614 /* Registers involved in inheritance/split in the current EBB
4615 (inheritance/split pseudos and original registers). */
4616 static bitmap_head check_only_regs;
4617
4618 /* Do inheritance transformations for insn INSN, which defines (if
4619 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4620 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4621 form as the "insns" field of usage_insns. Return true if we
4622 succeed in such transformation.
4623
4624 The transformations look like:
4625
4626 p <- ... i <- ...
4627 ... p <- i (new insn)
4628 ... =>
4629 <- ... p ... <- ... i ...
4630 or
4631 ... i <- p (new insn)
4632 <- ... p ... <- ... i ...
4633 ... =>
4634 <- ... p ... <- ... i ...
4635 where p is a spilled original pseudo and i is a new inheritance pseudo.
4636
4637
4638 The inheritance pseudo has the smallest class of two classes CL and
4639 class of ORIGINAL REGNO. */
4640 static bool
4641 inherit_reload_reg (bool def_p, int original_regno,
4642 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4643 {
4644 if (optimize_function_for_size_p (cfun))
4645 return false;
4646
4647 enum reg_class rclass = lra_get_allocno_class (original_regno);
4648 rtx original_reg = regno_reg_rtx[original_regno];
4649 rtx new_reg, usage_insn;
4650 rtx_insn *new_insns;
4651
4652 lra_assert (! usage_insns[original_regno].after_p);
4653 if (lra_dump_file != NULL)
4654 fprintf (lra_dump_file,
4655 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4656 if (! ira_reg_classes_intersect_p[cl][rclass])
4657 {
4658 if (lra_dump_file != NULL)
4659 {
4660 fprintf (lra_dump_file,
4661 " Rejecting inheritance for %d "
4662 "because of disjoint classes %s and %s\n",
4663 original_regno, reg_class_names[cl],
4664 reg_class_names[rclass]);
4665 fprintf (lra_dump_file,
4666 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4667 }
4668 return false;
4669 }
4670 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4671 /* We don't use a subset of two classes because it can be
4672 NO_REGS. This transformation is still profitable in most
4673 cases even if the classes are not intersected as register
4674 move is probably cheaper than a memory load. */
4675 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4676 {
4677 if (lra_dump_file != NULL)
4678 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4679 reg_class_names[cl], reg_class_names[rclass]);
4680
4681 rclass = cl;
4682 }
4683 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4684 {
4685 /* Reject inheritance resulting in secondary memory moves.
4686 Otherwise, there is a danger in LRA cycling. Also such
4687 transformation will be unprofitable. */
4688 if (lra_dump_file != NULL)
4689 {
4690 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4691 rtx set = single_set (insn);
4692
4693 lra_assert (set != NULL_RTX);
4694
4695 rtx dest = SET_DEST (set);
4696
4697 lra_assert (REG_P (dest));
4698 fprintf (lra_dump_file,
4699 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4700 "as secondary mem is needed\n",
4701 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4702 original_regno, reg_class_names[rclass]);
4703 fprintf (lra_dump_file,
4704 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4705 }
4706 return false;
4707 }
4708 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4709 rclass, "inheritance");
4710 start_sequence ();
4711 if (def_p)
4712 lra_emit_move (original_reg, new_reg);
4713 else
4714 lra_emit_move (new_reg, original_reg);
4715 new_insns = get_insns ();
4716 end_sequence ();
4717 if (NEXT_INSN (new_insns) != NULL_RTX)
4718 {
4719 if (lra_dump_file != NULL)
4720 {
4721 fprintf (lra_dump_file,
4722 " Rejecting inheritance %d->%d "
4723 "as it results in 2 or more insns:\n",
4724 original_regno, REGNO (new_reg));
4725 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4726 fprintf (lra_dump_file,
4727 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4728 }
4729 return false;
4730 }
4731 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
4732 lra_update_insn_regno_info (insn);
4733 if (! def_p)
4734 /* We now have a new usage insn for original regno. */
4735 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4736 if (lra_dump_file != NULL)
4737 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4738 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4739 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4740 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4741 bitmap_set_bit (&check_only_regs, original_regno);
4742 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4743 if (def_p)
4744 lra_process_new_insns (insn, NULL, new_insns,
4745 "Add original<-inheritance");
4746 else
4747 lra_process_new_insns (insn, new_insns, NULL,
4748 "Add inheritance<-original");
4749 while (next_usage_insns != NULL_RTX)
4750 {
4751 if (GET_CODE (next_usage_insns) != INSN_LIST)
4752 {
4753 usage_insn = next_usage_insns;
4754 lra_assert (NONDEBUG_INSN_P (usage_insn));
4755 next_usage_insns = NULL;
4756 }
4757 else
4758 {
4759 usage_insn = XEXP (next_usage_insns, 0);
4760 lra_assert (DEBUG_INSN_P (usage_insn));
4761 next_usage_insns = XEXP (next_usage_insns, 1);
4762 }
4763 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
4764 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4765 if (lra_dump_file != NULL)
4766 {
4767 fprintf (lra_dump_file,
4768 " Inheritance reuse change %d->%d (bb%d):\n",
4769 original_regno, REGNO (new_reg),
4770 BLOCK_FOR_INSN (usage_insn)->index);
4771 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
4772 }
4773 }
4774 if (lra_dump_file != NULL)
4775 fprintf (lra_dump_file,
4776 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4777 return true;
4778 }
4779
4780 /* Return true if we need a caller save/restore for pseudo REGNO which
4781 was assigned to a hard register. */
4782 static inline bool
4783 need_for_call_save_p (int regno)
4784 {
4785 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4786 return (usage_insns[regno].calls_num < calls_num
4787 && (overlaps_hard_reg_set_p
4788 ((flag_ipa_ra &&
4789 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4790 ? lra_reg_info[regno].actual_call_used_reg_set
4791 : call_used_reg_set,
4792 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4793 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4794 PSEUDO_REGNO_MODE (regno))));
4795 }
4796
4797 /* Global registers occurring in the current EBB. */
4798 static bitmap_head ebb_global_regs;
4799
4800 /* Return true if we need a split for hard register REGNO or pseudo
4801 REGNO which was assigned to a hard register.
4802 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4803 used for reloads since the EBB end. It is an approximation of the
4804 used hard registers in the split range. The exact value would
4805 require expensive calculations. If we were aggressive with
4806 splitting because of the approximation, the split pseudo will save
4807 the same hard register assignment and will be removed in the undo
4808 pass. We still need the approximation because too aggressive
4809 splitting would result in too inaccurate cost calculation in the
4810 assignment pass because of too many generated moves which will be
4811 probably removed in the undo pass. */
4812 static inline bool
4813 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4814 {
4815 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4816
4817 lra_assert (hard_regno >= 0);
4818 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4819 /* Don't split eliminable hard registers, otherwise we can
4820 split hard registers like hard frame pointer, which
4821 lives on BB start/end according to DF-infrastructure,
4822 when there is a pseudo assigned to the register and
4823 living in the same BB. */
4824 && (regno >= FIRST_PSEUDO_REGISTER
4825 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4826 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4827 /* Don't split call clobbered hard regs living through
4828 calls, otherwise we might have a check problem in the
4829 assign sub-pass as in the most cases (exception is a
4830 situation when lra_risky_transformations_p value is
4831 true) the assign pass assumes that all pseudos living
4832 through calls are assigned to call saved hard regs. */
4833 && (regno >= FIRST_PSEUDO_REGISTER
4834 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4835 || usage_insns[regno].calls_num == calls_num)
4836 /* We need at least 2 reloads to make pseudo splitting
4837 profitable. We should provide hard regno splitting in
4838 any case to solve 1st insn scheduling problem when
4839 moving hard register definition up might result in
4840 impossibility to find hard register for reload pseudo of
4841 small register class. */
4842 && (usage_insns[regno].reloads_num
4843 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4844 && (regno < FIRST_PSEUDO_REGISTER
4845 /* For short living pseudos, spilling + inheritance can
4846 be considered a substitution for splitting.
4847 Therefore we do not splitting for local pseudos. It
4848 decreases also aggressiveness of splitting. The
4849 minimal number of references is chosen taking into
4850 account that for 2 references splitting has no sense
4851 as we can just spill the pseudo. */
4852 || (regno >= FIRST_PSEUDO_REGISTER
4853 && lra_reg_info[regno].nrefs > 3
4854 && bitmap_bit_p (&ebb_global_regs, regno))))
4855 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4856 }
4857
4858 /* Return class for the split pseudo created from original pseudo with
4859 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4860 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4861 results in no secondary memory movements. */
4862 static enum reg_class
4863 choose_split_class (enum reg_class allocno_class,
4864 int hard_regno ATTRIBUTE_UNUSED,
4865 machine_mode mode ATTRIBUTE_UNUSED)
4866 {
4867 #ifndef SECONDARY_MEMORY_NEEDED
4868 return allocno_class;
4869 #else
4870 int i;
4871 enum reg_class cl, best_cl = NO_REGS;
4872 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4873 = REGNO_REG_CLASS (hard_regno);
4874
4875 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4876 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4877 return allocno_class;
4878 for (i = 0;
4879 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4880 i++)
4881 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4882 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4883 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4884 && (best_cl == NO_REGS
4885 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4886 best_cl = cl;
4887 return best_cl;
4888 #endif
4889 }
4890
4891 /* Do split transformations for insn INSN, which defines or uses
4892 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4893 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4894 "insns" field of usage_insns.
4895
4896 The transformations look like:
4897
4898 p <- ... p <- ...
4899 ... s <- p (new insn -- save)
4900 ... =>
4901 ... p <- s (new insn -- restore)
4902 <- ... p ... <- ... p ...
4903 or
4904 <- ... p ... <- ... p ...
4905 ... s <- p (new insn -- save)
4906 ... =>
4907 ... p <- s (new insn -- restore)
4908 <- ... p ... <- ... p ...
4909
4910 where p is an original pseudo got a hard register or a hard
4911 register and s is a new split pseudo. The save is put before INSN
4912 if BEFORE_P is true. Return true if we succeed in such
4913 transformation. */
4914 static bool
4915 split_reg (bool before_p, int original_regno, rtx_insn *insn,
4916 rtx next_usage_insns)
4917 {
4918 enum reg_class rclass;
4919 rtx original_reg;
4920 int hard_regno, nregs;
4921 rtx new_reg, usage_insn;
4922 rtx_insn *restore, *save;
4923 bool after_p;
4924 bool call_save_p;
4925
4926 if (original_regno < FIRST_PSEUDO_REGISTER)
4927 {
4928 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4929 hard_regno = original_regno;
4930 call_save_p = false;
4931 nregs = 1;
4932 }
4933 else
4934 {
4935 hard_regno = reg_renumber[original_regno];
4936 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4937 rclass = lra_get_allocno_class (original_regno);
4938 original_reg = regno_reg_rtx[original_regno];
4939 call_save_p = need_for_call_save_p (original_regno);
4940 }
4941 original_reg = regno_reg_rtx[original_regno];
4942 lra_assert (hard_regno >= 0);
4943 if (lra_dump_file != NULL)
4944 fprintf (lra_dump_file,
4945 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4946 if (call_save_p)
4947 {
4948 machine_mode mode = GET_MODE (original_reg);
4949
4950 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4951 hard_regno_nregs[hard_regno][mode],
4952 mode);
4953 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4954 }
4955 else
4956 {
4957 rclass = choose_split_class (rclass, hard_regno,
4958 GET_MODE (original_reg));
4959 if (rclass == NO_REGS)
4960 {
4961 if (lra_dump_file != NULL)
4962 {
4963 fprintf (lra_dump_file,
4964 " Rejecting split of %d(%s): "
4965 "no good reg class for %d(%s)\n",
4966 original_regno,
4967 reg_class_names[lra_get_allocno_class (original_regno)],
4968 hard_regno,
4969 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4970 fprintf
4971 (lra_dump_file,
4972 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4973 }
4974 return false;
4975 }
4976 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4977 rclass, "split");
4978 reg_renumber[REGNO (new_reg)] = hard_regno;
4979 }
4980 save = emit_spill_move (true, new_reg, original_reg);
4981 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
4982 {
4983 if (lra_dump_file != NULL)
4984 {
4985 fprintf
4986 (lra_dump_file,
4987 " Rejecting split %d->%d resulting in > 2 save insns:\n",
4988 original_regno, REGNO (new_reg));
4989 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
4990 fprintf (lra_dump_file,
4991 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4992 }
4993 return false;
4994 }
4995 restore = emit_spill_move (false, new_reg, original_reg);
4996 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
4997 {
4998 if (lra_dump_file != NULL)
4999 {
5000 fprintf (lra_dump_file,
5001 " Rejecting split %d->%d "
5002 "resulting in > 2 restore insns:\n",
5003 original_regno, REGNO (new_reg));
5004 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
5005 fprintf (lra_dump_file,
5006 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5007 }
5008 return false;
5009 }
5010 after_p = usage_insns[original_regno].after_p;
5011 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
5012 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5013 bitmap_set_bit (&check_only_regs, original_regno);
5014 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
5015 for (;;)
5016 {
5017 if (GET_CODE (next_usage_insns) != INSN_LIST)
5018 {
5019 usage_insn = next_usage_insns;
5020 break;
5021 }
5022 usage_insn = XEXP (next_usage_insns, 0);
5023 lra_assert (DEBUG_INSN_P (usage_insn));
5024 next_usage_insns = XEXP (next_usage_insns, 1);
5025 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5026 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5027 if (lra_dump_file != NULL)
5028 {
5029 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5030 original_regno, REGNO (new_reg));
5031 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5032 }
5033 }
5034 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5035 lra_assert (usage_insn != insn || (after_p && before_p));
5036 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5037 after_p ? NULL : restore,
5038 after_p ? restore : NULL,
5039 call_save_p
5040 ? "Add reg<-save" : "Add reg<-split");
5041 lra_process_new_insns (insn, before_p ? save : NULL,
5042 before_p ? NULL : save,
5043 call_save_p
5044 ? "Add save<-reg" : "Add split<-reg");
5045 if (nregs > 1)
5046 /* If we are trying to split multi-register. We should check
5047 conflicts on the next assignment sub-pass. IRA can allocate on
5048 sub-register levels, LRA do this on pseudos level right now and
5049 this discrepancy may create allocation conflicts after
5050 splitting. */
5051 lra_risky_transformations_p = true;
5052 if (lra_dump_file != NULL)
5053 fprintf (lra_dump_file,
5054 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5055 return true;
5056 }
5057
5058 /* Recognize that we need a split transformation for insn INSN, which
5059 defines or uses REGNO in its insn biggest MODE (we use it only if
5060 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5061 hard registers which might be used for reloads since the EBB end.
5062 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5063 uid before starting INSN processing. Return true if we succeed in
5064 such transformation. */
5065 static bool
5066 split_if_necessary (int regno, machine_mode mode,
5067 HARD_REG_SET potential_reload_hard_regs,
5068 bool before_p, rtx_insn *insn, int max_uid)
5069 {
5070 bool res = false;
5071 int i, nregs = 1;
5072 rtx next_usage_insns;
5073
5074 if (regno < FIRST_PSEUDO_REGISTER)
5075 nregs = hard_regno_nregs[regno][mode];
5076 for (i = 0; i < nregs; i++)
5077 if (usage_insns[regno + i].check == curr_usage_insns_check
5078 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5079 /* To avoid processing the register twice or more. */
5080 && ((GET_CODE (next_usage_insns) != INSN_LIST
5081 && INSN_UID (next_usage_insns) < max_uid)
5082 || (GET_CODE (next_usage_insns) == INSN_LIST
5083 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5084 && need_for_split_p (potential_reload_hard_regs, regno + i)
5085 && split_reg (before_p, regno + i, insn, next_usage_insns))
5086 res = true;
5087 return res;
5088 }
5089
5090 /* Check only registers living at the current program point in the
5091 current EBB. */
5092 static bitmap_head live_regs;
5093
5094 /* Update live info in EBB given by its HEAD and TAIL insns after
5095 inheritance/split transformation. The function removes dead moves
5096 too. */
5097 static void
5098 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5099 {
5100 unsigned int j;
5101 int i, regno;
5102 bool live_p;
5103 rtx_insn *prev_insn;
5104 rtx set;
5105 bool remove_p;
5106 basic_block last_bb, prev_bb, curr_bb;
5107 bitmap_iterator bi;
5108 struct lra_insn_reg *reg;
5109 edge e;
5110 edge_iterator ei;
5111
5112 last_bb = BLOCK_FOR_INSN (tail);
5113 prev_bb = NULL;
5114 for (curr_insn = tail;
5115 curr_insn != PREV_INSN (head);
5116 curr_insn = prev_insn)
5117 {
5118 prev_insn = PREV_INSN (curr_insn);
5119 /* We need to process empty blocks too. They contain
5120 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5121 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5122 continue;
5123 curr_bb = BLOCK_FOR_INSN (curr_insn);
5124 if (curr_bb != prev_bb)
5125 {
5126 if (prev_bb != NULL)
5127 {
5128 /* Update df_get_live_in (prev_bb): */
5129 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5130 if (bitmap_bit_p (&live_regs, j))
5131 bitmap_set_bit (df_get_live_in (prev_bb), j);
5132 else
5133 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5134 }
5135 if (curr_bb != last_bb)
5136 {
5137 /* Update df_get_live_out (curr_bb): */
5138 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5139 {
5140 live_p = bitmap_bit_p (&live_regs, j);
5141 if (! live_p)
5142 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5143 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5144 {
5145 live_p = true;
5146 break;
5147 }
5148 if (live_p)
5149 bitmap_set_bit (df_get_live_out (curr_bb), j);
5150 else
5151 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5152 }
5153 }
5154 prev_bb = curr_bb;
5155 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5156 }
5157 if (! NONDEBUG_INSN_P (curr_insn))
5158 continue;
5159 curr_id = lra_get_insn_recog_data (curr_insn);
5160 curr_static_id = curr_id->insn_static_data;
5161 remove_p = false;
5162 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
5163 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5164 && bitmap_bit_p (&check_only_regs, regno)
5165 && ! bitmap_bit_p (&live_regs, regno))
5166 remove_p = true;
5167 /* See which defined values die here. */
5168 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5169 if (reg->type == OP_OUT && ! reg->subreg_p)
5170 bitmap_clear_bit (&live_regs, reg->regno);
5171 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5172 if (reg->type == OP_OUT && ! reg->subreg_p)
5173 bitmap_clear_bit (&live_regs, reg->regno);
5174 if (curr_id->arg_hard_regs != NULL)
5175 /* Make clobbered argument hard registers die. */
5176 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5177 if (regno >= FIRST_PSEUDO_REGISTER)
5178 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
5179 /* Mark each used value as live. */
5180 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5181 if (reg->type != OP_OUT
5182 && bitmap_bit_p (&check_only_regs, reg->regno))
5183 bitmap_set_bit (&live_regs, reg->regno);
5184 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5185 if (reg->type != OP_OUT
5186 && bitmap_bit_p (&check_only_regs, reg->regno))
5187 bitmap_set_bit (&live_regs, reg->regno);
5188 if (curr_id->arg_hard_regs != NULL)
5189 /* Make used argument hard registers live. */
5190 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5191 if (regno < FIRST_PSEUDO_REGISTER
5192 && bitmap_bit_p (&check_only_regs, regno))
5193 bitmap_set_bit (&live_regs, regno);
5194 /* It is quite important to remove dead move insns because it
5195 means removing dead store. We don't need to process them for
5196 constraints. */
5197 if (remove_p)
5198 {
5199 if (lra_dump_file != NULL)
5200 {
5201 fprintf (lra_dump_file, " Removing dead insn:\n ");
5202 dump_insn_slim (lra_dump_file, curr_insn);
5203 }
5204 lra_set_insn_deleted (curr_insn);
5205 }
5206 }
5207 }
5208
5209 /* The structure describes info to do an inheritance for the current
5210 insn. We need to collect such info first before doing the
5211 transformations because the transformations change the insn
5212 internal representation. */
5213 struct to_inherit
5214 {
5215 /* Original regno. */
5216 int regno;
5217 /* Subsequent insns which can inherit original reg value. */
5218 rtx insns;
5219 };
5220
5221 /* Array containing all info for doing inheritance from the current
5222 insn. */
5223 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5224
5225 /* Number elements in the previous array. */
5226 static int to_inherit_num;
5227
5228 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5229 structure to_inherit. */
5230 static void
5231 add_to_inherit (int regno, rtx insns)
5232 {
5233 int i;
5234
5235 for (i = 0; i < to_inherit_num; i++)
5236 if (to_inherit[i].regno == regno)
5237 return;
5238 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5239 to_inherit[to_inherit_num].regno = regno;
5240 to_inherit[to_inherit_num++].insns = insns;
5241 }
5242
5243 /* Return the last non-debug insn in basic block BB, or the block begin
5244 note if none. */
5245 static rtx_insn *
5246 get_last_insertion_point (basic_block bb)
5247 {
5248 rtx_insn *insn;
5249
5250 FOR_BB_INSNS_REVERSE (bb, insn)
5251 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5252 return insn;
5253 gcc_unreachable ();
5254 }
5255
5256 /* Set up RES by registers living on edges FROM except the edge (FROM,
5257 TO) or by registers set up in a jump insn in BB FROM. */
5258 static void
5259 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5260 {
5261 rtx_insn *last;
5262 struct lra_insn_reg *reg;
5263 edge e;
5264 edge_iterator ei;
5265
5266 lra_assert (to != NULL);
5267 bitmap_clear (res);
5268 FOR_EACH_EDGE (e, ei, from->succs)
5269 if (e->dest != to)
5270 bitmap_ior_into (res, df_get_live_in (e->dest));
5271 last = get_last_insertion_point (from);
5272 if (! JUMP_P (last))
5273 return;
5274 curr_id = lra_get_insn_recog_data (last);
5275 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5276 if (reg->type != OP_IN)
5277 bitmap_set_bit (res, reg->regno);
5278 }
5279
5280 /* Used as a temporary results of some bitmap calculations. */
5281 static bitmap_head temp_bitmap;
5282
5283 /* We split for reloads of small class of hard regs. The following
5284 defines how many hard regs the class should have to be qualified as
5285 small. The code is mostly oriented to x86/x86-64 architecture
5286 where some insns need to use only specific register or pair of
5287 registers and these register can live in RTL explicitly, e.g. for
5288 parameter passing. */
5289 static const int max_small_class_regs_num = 2;
5290
5291 /* Do inheritance/split transformations in EBB starting with HEAD and
5292 finishing on TAIL. We process EBB insns in the reverse order.
5293 Return true if we did any inheritance/split transformation in the
5294 EBB.
5295
5296 We should avoid excessive splitting which results in worse code
5297 because of inaccurate cost calculations for spilling new split
5298 pseudos in such case. To achieve this we do splitting only if
5299 register pressure is high in given basic block and there are reload
5300 pseudos requiring hard registers. We could do more register
5301 pressure calculations at any given program point to avoid necessary
5302 splitting even more but it is to expensive and the current approach
5303 works well enough. */
5304 static bool
5305 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5306 {
5307 int i, src_regno, dst_regno, nregs;
5308 bool change_p, succ_p, update_reloads_num_p;
5309 rtx_insn *prev_insn, *last_insn;
5310 rtx next_usage_insns, set;
5311 enum reg_class cl;
5312 struct lra_insn_reg *reg;
5313 basic_block last_processed_bb, curr_bb = NULL;
5314 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5315 bitmap to_process;
5316 unsigned int j;
5317 bitmap_iterator bi;
5318 bool head_p, after_p;
5319
5320 change_p = false;
5321 curr_usage_insns_check++;
5322 reloads_num = calls_num = 0;
5323 bitmap_clear (&check_only_regs);
5324 last_processed_bb = NULL;
5325 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5326 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5327 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5328 /* We don't process new insns generated in the loop. */
5329 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5330 {
5331 prev_insn = PREV_INSN (curr_insn);
5332 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5333 curr_bb = BLOCK_FOR_INSN (curr_insn);
5334 if (last_processed_bb != curr_bb)
5335 {
5336 /* We are at the end of BB. Add qualified living
5337 pseudos for potential splitting. */
5338 to_process = df_get_live_out (curr_bb);
5339 if (last_processed_bb != NULL)
5340 {
5341 /* We are somewhere in the middle of EBB. */
5342 get_live_on_other_edges (curr_bb, last_processed_bb,
5343 &temp_bitmap);
5344 to_process = &temp_bitmap;
5345 }
5346 last_processed_bb = curr_bb;
5347 last_insn = get_last_insertion_point (curr_bb);
5348 after_p = (! JUMP_P (last_insn)
5349 && (! CALL_P (last_insn)
5350 || (find_reg_note (last_insn,
5351 REG_NORETURN, NULL_RTX) == NULL_RTX
5352 && ! SIBLING_CALL_P (last_insn))));
5353 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5354 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5355 {
5356 if ((int) j >= lra_constraint_new_regno_start)
5357 break;
5358 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5359 {
5360 if (j < FIRST_PSEUDO_REGISTER)
5361 SET_HARD_REG_BIT (live_hard_regs, j);
5362 else
5363 add_to_hard_reg_set (&live_hard_regs,
5364 PSEUDO_REGNO_MODE (j),
5365 reg_renumber[j]);
5366 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5367 }
5368 }
5369 }
5370 src_regno = dst_regno = -1;
5371 if (NONDEBUG_INSN_P (curr_insn)
5372 && (set = single_set (curr_insn)) != NULL_RTX
5373 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5374 {
5375 src_regno = REGNO (SET_SRC (set));
5376 dst_regno = REGNO (SET_DEST (set));
5377 }
5378 update_reloads_num_p = true;
5379 if (src_regno < lra_constraint_new_regno_start
5380 && src_regno >= FIRST_PSEUDO_REGISTER
5381 && reg_renumber[src_regno] < 0
5382 && dst_regno >= lra_constraint_new_regno_start
5383 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5384 {
5385 /* 'reload_pseudo <- original_pseudo'. */
5386 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5387 reloads_num++;
5388 update_reloads_num_p = false;
5389 succ_p = false;
5390 if (usage_insns[src_regno].check == curr_usage_insns_check
5391 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5392 succ_p = inherit_reload_reg (false, src_regno, cl,
5393 curr_insn, next_usage_insns);
5394 if (succ_p)
5395 change_p = true;
5396 else
5397 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5398 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5399 IOR_HARD_REG_SET (potential_reload_hard_regs,
5400 reg_class_contents[cl]);
5401 }
5402 else if (src_regno >= lra_constraint_new_regno_start
5403 && dst_regno < lra_constraint_new_regno_start
5404 && dst_regno >= FIRST_PSEUDO_REGISTER
5405 && reg_renumber[dst_regno] < 0
5406 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5407 && usage_insns[dst_regno].check == curr_usage_insns_check
5408 && (next_usage_insns
5409 = usage_insns[dst_regno].insns) != NULL_RTX)
5410 {
5411 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5412 reloads_num++;
5413 update_reloads_num_p = false;
5414 /* 'original_pseudo <- reload_pseudo'. */
5415 if (! JUMP_P (curr_insn)
5416 && inherit_reload_reg (true, dst_regno, cl,
5417 curr_insn, next_usage_insns))
5418 change_p = true;
5419 /* Invalidate. */
5420 usage_insns[dst_regno].check = 0;
5421 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5422 IOR_HARD_REG_SET (potential_reload_hard_regs,
5423 reg_class_contents[cl]);
5424 }
5425 else if (INSN_P (curr_insn))
5426 {
5427 int iter;
5428 int max_uid = get_max_uid ();
5429
5430 curr_id = lra_get_insn_recog_data (curr_insn);
5431 curr_static_id = curr_id->insn_static_data;
5432 to_inherit_num = 0;
5433 /* Process insn definitions. */
5434 for (iter = 0; iter < 2; iter++)
5435 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5436 reg != NULL;
5437 reg = reg->next)
5438 if (reg->type != OP_IN
5439 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5440 {
5441 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5442 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5443 && usage_insns[dst_regno].check == curr_usage_insns_check
5444 && (next_usage_insns
5445 = usage_insns[dst_regno].insns) != NULL_RTX)
5446 {
5447 struct lra_insn_reg *r;
5448
5449 for (r = curr_id->regs; r != NULL; r = r->next)
5450 if (r->type != OP_OUT && r->regno == dst_regno)
5451 break;
5452 /* Don't do inheritance if the pseudo is also
5453 used in the insn. */
5454 if (r == NULL)
5455 /* We can not do inheritance right now
5456 because the current insn reg info (chain
5457 regs) can change after that. */
5458 add_to_inherit (dst_regno, next_usage_insns);
5459 }
5460 /* We can not process one reg twice here because of
5461 usage_insns invalidation. */
5462 if ((dst_regno < FIRST_PSEUDO_REGISTER
5463 || reg_renumber[dst_regno] >= 0)
5464 && ! reg->subreg_p && reg->type != OP_IN)
5465 {
5466 HARD_REG_SET s;
5467
5468 if (split_if_necessary (dst_regno, reg->biggest_mode,
5469 potential_reload_hard_regs,
5470 false, curr_insn, max_uid))
5471 change_p = true;
5472 CLEAR_HARD_REG_SET (s);
5473 if (dst_regno < FIRST_PSEUDO_REGISTER)
5474 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5475 else
5476 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5477 reg_renumber[dst_regno]);
5478 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5479 }
5480 /* We should invalidate potential inheritance or
5481 splitting for the current insn usages to the next
5482 usage insns (see code below) as the output pseudo
5483 prevents this. */
5484 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5485 && reg_renumber[dst_regno] < 0)
5486 || (reg->type == OP_OUT && ! reg->subreg_p
5487 && (dst_regno < FIRST_PSEUDO_REGISTER
5488 || reg_renumber[dst_regno] >= 0)))
5489 {
5490 /* Invalidate and mark definitions. */
5491 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5492 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5493 else
5494 {
5495 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5496 for (i = 0; i < nregs; i++)
5497 usage_insns[dst_regno + i].check
5498 = -(int) INSN_UID (curr_insn);
5499 }
5500 }
5501 }
5502 /* Process clobbered call regs. */
5503 if (curr_id->arg_hard_regs != NULL)
5504 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5505 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5506 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
5507 = -(int) INSN_UID (curr_insn);
5508 if (! JUMP_P (curr_insn))
5509 for (i = 0; i < to_inherit_num; i++)
5510 if (inherit_reload_reg (true, to_inherit[i].regno,
5511 ALL_REGS, curr_insn,
5512 to_inherit[i].insns))
5513 change_p = true;
5514 if (CALL_P (curr_insn))
5515 {
5516 rtx cheap, pat, dest;
5517 rtx_insn *restore;
5518 int regno, hard_regno;
5519
5520 calls_num++;
5521 if ((cheap = find_reg_note (curr_insn,
5522 REG_RETURNED, NULL_RTX)) != NULL_RTX
5523 && ((cheap = XEXP (cheap, 0)), true)
5524 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5525 && (hard_regno = reg_renumber[regno]) >= 0
5526 /* If there are pending saves/restores, the
5527 optimization is not worth. */
5528 && usage_insns[regno].calls_num == calls_num - 1
5529 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5530 {
5531 /* Restore the pseudo from the call result as
5532 REG_RETURNED note says that the pseudo value is
5533 in the call result and the pseudo is an argument
5534 of the call. */
5535 pat = PATTERN (curr_insn);
5536 if (GET_CODE (pat) == PARALLEL)
5537 pat = XVECEXP (pat, 0, 0);
5538 dest = SET_DEST (pat);
5539 /* For multiple return values dest is PARALLEL.
5540 Currently we handle only single return value case. */
5541 if (REG_P (dest))
5542 {
5543 start_sequence ();
5544 emit_move_insn (cheap, copy_rtx (dest));
5545 restore = get_insns ();
5546 end_sequence ();
5547 lra_process_new_insns (curr_insn, NULL, restore,
5548 "Inserting call parameter restore");
5549 /* We don't need to save/restore of the pseudo from
5550 this call. */
5551 usage_insns[regno].calls_num = calls_num;
5552 bitmap_set_bit (&check_only_regs, regno);
5553 }
5554 }
5555 }
5556 to_inherit_num = 0;
5557 /* Process insn usages. */
5558 for (iter = 0; iter < 2; iter++)
5559 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5560 reg != NULL;
5561 reg = reg->next)
5562 if ((reg->type != OP_OUT
5563 || (reg->type == OP_OUT && reg->subreg_p))
5564 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5565 {
5566 if (src_regno >= FIRST_PSEUDO_REGISTER
5567 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5568 {
5569 if (usage_insns[src_regno].check == curr_usage_insns_check
5570 && (next_usage_insns
5571 = usage_insns[src_regno].insns) != NULL_RTX
5572 && NONDEBUG_INSN_P (curr_insn))
5573 add_to_inherit (src_regno, next_usage_insns);
5574 else if (usage_insns[src_regno].check
5575 != -(int) INSN_UID (curr_insn))
5576 /* Add usages but only if the reg is not set up
5577 in the same insn. */
5578 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5579 }
5580 else if (src_regno < FIRST_PSEUDO_REGISTER
5581 || reg_renumber[src_regno] >= 0)
5582 {
5583 bool before_p;
5584 rtx_insn *use_insn = curr_insn;
5585
5586 before_p = (JUMP_P (curr_insn)
5587 || (CALL_P (curr_insn) && reg->type == OP_IN));
5588 if (NONDEBUG_INSN_P (curr_insn)
5589 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5590 && split_if_necessary (src_regno, reg->biggest_mode,
5591 potential_reload_hard_regs,
5592 before_p, curr_insn, max_uid))
5593 {
5594 if (reg->subreg_p)
5595 lra_risky_transformations_p = true;
5596 change_p = true;
5597 /* Invalidate. */
5598 usage_insns[src_regno].check = 0;
5599 if (before_p)
5600 use_insn = PREV_INSN (curr_insn);
5601 }
5602 if (NONDEBUG_INSN_P (curr_insn))
5603 {
5604 if (src_regno < FIRST_PSEUDO_REGISTER)
5605 add_to_hard_reg_set (&live_hard_regs,
5606 reg->biggest_mode, src_regno);
5607 else
5608 add_to_hard_reg_set (&live_hard_regs,
5609 PSEUDO_REGNO_MODE (src_regno),
5610 reg_renumber[src_regno]);
5611 }
5612 add_next_usage_insn (src_regno, use_insn, reloads_num);
5613 }
5614 }
5615 /* Process used call regs. */
5616 if (curr_id->arg_hard_regs != NULL)
5617 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5618 if (src_regno < FIRST_PSEUDO_REGISTER)
5619 {
5620 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5621 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5622 }
5623 for (i = 0; i < to_inherit_num; i++)
5624 {
5625 src_regno = to_inherit[i].regno;
5626 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5627 curr_insn, to_inherit[i].insns))
5628 change_p = true;
5629 else
5630 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5631 }
5632 }
5633 if (update_reloads_num_p
5634 && NONDEBUG_INSN_P (curr_insn)
5635 && (set = single_set (curr_insn)) != NULL_RTX)
5636 {
5637 int regno = -1;
5638 if ((REG_P (SET_DEST (set))
5639 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5640 && reg_renumber[regno] < 0
5641 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5642 || (REG_P (SET_SRC (set))
5643 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5644 && reg_renumber[regno] < 0
5645 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5646 {
5647 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5648 reloads_num++;
5649 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5650 IOR_HARD_REG_SET (potential_reload_hard_regs,
5651 reg_class_contents[cl]);
5652 }
5653 }
5654 /* We reached the start of the current basic block. */
5655 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5656 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5657 {
5658 /* We reached the beginning of the current block -- do
5659 rest of spliting in the current BB. */
5660 to_process = df_get_live_in (curr_bb);
5661 if (BLOCK_FOR_INSN (head) != curr_bb)
5662 {
5663 /* We are somewhere in the middle of EBB. */
5664 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5665 curr_bb, &temp_bitmap);
5666 to_process = &temp_bitmap;
5667 }
5668 head_p = true;
5669 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5670 {
5671 if ((int) j >= lra_constraint_new_regno_start)
5672 break;
5673 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5674 && usage_insns[j].check == curr_usage_insns_check
5675 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5676 {
5677 if (need_for_split_p (potential_reload_hard_regs, j))
5678 {
5679 if (lra_dump_file != NULL && head_p)
5680 {
5681 fprintf (lra_dump_file,
5682 " ----------------------------------\n");
5683 head_p = false;
5684 }
5685 if (split_reg (false, j, bb_note (curr_bb),
5686 next_usage_insns))
5687 change_p = true;
5688 }
5689 usage_insns[j].check = 0;
5690 }
5691 }
5692 }
5693 }
5694 return change_p;
5695 }
5696
5697 /* This value affects EBB forming. If probability of edge from EBB to
5698 a BB is not greater than the following value, we don't add the BB
5699 to EBB. */
5700 #define EBB_PROBABILITY_CUTOFF \
5701 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
5702
5703 /* Current number of inheritance/split iteration. */
5704 int lra_inheritance_iter;
5705
5706 /* Entry function for inheritance/split pass. */
5707 void
5708 lra_inheritance (void)
5709 {
5710 int i;
5711 basic_block bb, start_bb;
5712 edge e;
5713
5714 lra_inheritance_iter++;
5715 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5716 return;
5717 timevar_push (TV_LRA_INHERITANCE);
5718 if (lra_dump_file != NULL)
5719 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5720 lra_inheritance_iter);
5721 curr_usage_insns_check = 0;
5722 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5723 for (i = 0; i < lra_constraint_new_regno_start; i++)
5724 usage_insns[i].check = 0;
5725 bitmap_initialize (&check_only_regs, &reg_obstack);
5726 bitmap_initialize (&live_regs, &reg_obstack);
5727 bitmap_initialize (&temp_bitmap, &reg_obstack);
5728 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5729 FOR_EACH_BB_FN (bb, cfun)
5730 {
5731 start_bb = bb;
5732 if (lra_dump_file != NULL)
5733 fprintf (lra_dump_file, "EBB");
5734 /* Form a EBB starting with BB. */
5735 bitmap_clear (&ebb_global_regs);
5736 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5737 for (;;)
5738 {
5739 if (lra_dump_file != NULL)
5740 fprintf (lra_dump_file, " %d", bb->index);
5741 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5742 || LABEL_P (BB_HEAD (bb->next_bb)))
5743 break;
5744 e = find_fallthru_edge (bb->succs);
5745 if (! e)
5746 break;
5747 if (e->probability < EBB_PROBABILITY_CUTOFF)
5748 break;
5749 bb = bb->next_bb;
5750 }
5751 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5752 if (lra_dump_file != NULL)
5753 fprintf (lra_dump_file, "\n");
5754 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5755 /* Remember that the EBB head and tail can change in
5756 inherit_in_ebb. */
5757 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5758 }
5759 bitmap_clear (&ebb_global_regs);
5760 bitmap_clear (&temp_bitmap);
5761 bitmap_clear (&live_regs);
5762 bitmap_clear (&check_only_regs);
5763 free (usage_insns);
5764
5765 timevar_pop (TV_LRA_INHERITANCE);
5766 }
5767
5768 \f
5769
5770 /* This page contains code to undo failed inheritance/split
5771 transformations. */
5772
5773 /* Current number of iteration undoing inheritance/split. */
5774 int lra_undo_inheritance_iter;
5775
5776 /* Fix BB live info LIVE after removing pseudos created on pass doing
5777 inheritance/split which are REMOVED_PSEUDOS. */
5778 static void
5779 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5780 {
5781 unsigned int regno;
5782 bitmap_iterator bi;
5783
5784 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5785 if (bitmap_clear_bit (live, regno))
5786 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5787 }
5788
5789 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5790 number. */
5791 static int
5792 get_regno (rtx reg)
5793 {
5794 if (GET_CODE (reg) == SUBREG)
5795 reg = SUBREG_REG (reg);
5796 if (REG_P (reg))
5797 return REGNO (reg);
5798 return -1;
5799 }
5800
5801 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5802 return true if we did any change. The undo transformations for
5803 inheritance looks like
5804 i <- i2
5805 p <- i => p <- i2
5806 or removing
5807 p <- i, i <- p, and i <- i3
5808 where p is original pseudo from which inheritance pseudo i was
5809 created, i and i3 are removed inheritance pseudos, i2 is another
5810 not removed inheritance pseudo. All split pseudos or other
5811 occurrences of removed inheritance pseudos are changed on the
5812 corresponding original pseudos.
5813
5814 The function also schedules insns changed and created during
5815 inheritance/split pass for processing by the subsequent constraint
5816 pass. */
5817 static bool
5818 remove_inheritance_pseudos (bitmap remove_pseudos)
5819 {
5820 basic_block bb;
5821 int regno, sregno, prev_sregno, dregno, restore_regno;
5822 rtx set, prev_set;
5823 rtx_insn *prev_insn;
5824 bool change_p, done_p;
5825
5826 change_p = ! bitmap_empty_p (remove_pseudos);
5827 /* We can not finish the function right away if CHANGE_P is true
5828 because we need to marks insns affected by previous
5829 inheritance/split pass for processing by the subsequent
5830 constraint pass. */
5831 FOR_EACH_BB_FN (bb, cfun)
5832 {
5833 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5834 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5835 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5836 {
5837 if (! INSN_P (curr_insn))
5838 continue;
5839 done_p = false;
5840 sregno = dregno = -1;
5841 if (change_p && NONDEBUG_INSN_P (curr_insn)
5842 && (set = single_set (curr_insn)) != NULL_RTX)
5843 {
5844 dregno = get_regno (SET_DEST (set));
5845 sregno = get_regno (SET_SRC (set));
5846 }
5847
5848 if (sregno >= 0 && dregno >= 0)
5849 {
5850 if ((bitmap_bit_p (remove_pseudos, sregno)
5851 && (lra_reg_info[sregno].restore_regno == dregno
5852 || (bitmap_bit_p (remove_pseudos, dregno)
5853 && (lra_reg_info[sregno].restore_regno
5854 == lra_reg_info[dregno].restore_regno))))
5855 || (bitmap_bit_p (remove_pseudos, dregno)
5856 && lra_reg_info[dregno].restore_regno == sregno))
5857 /* One of the following cases:
5858 original <- removed inheritance pseudo
5859 removed inherit pseudo <- another removed inherit pseudo
5860 removed inherit pseudo <- original pseudo
5861 Or
5862 removed_split_pseudo <- original_reg
5863 original_reg <- removed_split_pseudo */
5864 {
5865 if (lra_dump_file != NULL)
5866 {
5867 fprintf (lra_dump_file, " Removing %s:\n",
5868 bitmap_bit_p (&lra_split_regs, sregno)
5869 || bitmap_bit_p (&lra_split_regs, dregno)
5870 ? "split" : "inheritance");
5871 dump_insn_slim (lra_dump_file, curr_insn);
5872 }
5873 lra_set_insn_deleted (curr_insn);
5874 done_p = true;
5875 }
5876 else if (bitmap_bit_p (remove_pseudos, sregno)
5877 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5878 {
5879 /* Search the following pattern:
5880 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5881 original_pseudo <- inherit_or_split_pseudo1
5882 where the 2nd insn is the current insn and
5883 inherit_or_split_pseudo2 is not removed. If it is found,
5884 change the current insn onto:
5885 original_pseudo <- inherit_or_split_pseudo2. */
5886 for (prev_insn = PREV_INSN (curr_insn);
5887 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5888 prev_insn = PREV_INSN (prev_insn))
5889 ;
5890 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5891 && (prev_set = single_set (prev_insn)) != NULL_RTX
5892 /* There should be no subregs in insn we are
5893 searching because only the original reg might
5894 be in subreg when we changed the mode of
5895 load/store for splitting. */
5896 && REG_P (SET_DEST (prev_set))
5897 && REG_P (SET_SRC (prev_set))
5898 && (int) REGNO (SET_DEST (prev_set)) == sregno
5899 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5900 >= FIRST_PSEUDO_REGISTER)
5901 /* As we consider chain of inheritance or
5902 splitting described in above comment we should
5903 check that sregno and prev_sregno were
5904 inheritance/split pseudos created from the
5905 same original regno. */
5906 && (lra_reg_info[sregno].restore_regno
5907 == lra_reg_info[prev_sregno].restore_regno)
5908 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5909 {
5910 lra_assert (GET_MODE (SET_SRC (prev_set))
5911 == GET_MODE (regno_reg_rtx[sregno]));
5912 if (GET_CODE (SET_SRC (set)) == SUBREG)
5913 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5914 else
5915 SET_SRC (set) = SET_SRC (prev_set);
5916 /* As we are finishing with processing the insn
5917 here, check the destination too as it might
5918 inheritance pseudo for another pseudo. */
5919 if (bitmap_bit_p (remove_pseudos, dregno)
5920 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
5921 && (restore_regno
5922 = lra_reg_info[dregno].restore_regno) >= 0)
5923 {
5924 if (GET_CODE (SET_DEST (set)) == SUBREG)
5925 SUBREG_REG (SET_DEST (set))
5926 = regno_reg_rtx[restore_regno];
5927 else
5928 SET_DEST (set) = regno_reg_rtx[restore_regno];
5929 }
5930 lra_push_insn_and_update_insn_regno_info (curr_insn);
5931 lra_set_used_insn_alternative_by_uid
5932 (INSN_UID (curr_insn), -1);
5933 done_p = true;
5934 if (lra_dump_file != NULL)
5935 {
5936 fprintf (lra_dump_file, " Change reload insn:\n");
5937 dump_insn_slim (lra_dump_file, curr_insn);
5938 }
5939 }
5940 }
5941 }
5942 if (! done_p)
5943 {
5944 struct lra_insn_reg *reg;
5945 bool restored_regs_p = false;
5946 bool kept_regs_p = false;
5947
5948 curr_id = lra_get_insn_recog_data (curr_insn);
5949 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5950 {
5951 regno = reg->regno;
5952 restore_regno = lra_reg_info[regno].restore_regno;
5953 if (restore_regno >= 0)
5954 {
5955 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5956 {
5957 lra_substitute_pseudo_within_insn
5958 (curr_insn, regno, regno_reg_rtx[restore_regno],
5959 false);
5960 restored_regs_p = true;
5961 }
5962 else
5963 kept_regs_p = true;
5964 }
5965 }
5966 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5967 {
5968 /* The instruction has changed since the previous
5969 constraints pass. */
5970 lra_push_insn_and_update_insn_regno_info (curr_insn);
5971 lra_set_used_insn_alternative_by_uid
5972 (INSN_UID (curr_insn), -1);
5973 }
5974 else if (restored_regs_p)
5975 /* The instruction has been restored to the form that
5976 it had during the previous constraints pass. */
5977 lra_update_insn_regno_info (curr_insn);
5978 if (restored_regs_p && lra_dump_file != NULL)
5979 {
5980 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5981 dump_insn_slim (lra_dump_file, curr_insn);
5982 }
5983 }
5984 }
5985 }
5986 return change_p;
5987 }
5988
5989 /* If optional reload pseudos failed to get a hard register or was not
5990 inherited, it is better to remove optional reloads. We do this
5991 transformation after undoing inheritance to figure out necessity to
5992 remove optional reloads easier. Return true if we do any
5993 change. */
5994 static bool
5995 undo_optional_reloads (void)
5996 {
5997 bool change_p, keep_p;
5998 unsigned int regno, uid;
5999 bitmap_iterator bi, bi2;
6000 rtx_insn *insn;
6001 rtx set, src, dest;
6002 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
6003
6004 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
6005 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
6006 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6007 {
6008 keep_p = false;
6009 /* Keep optional reloads from previous subpasses. */
6010 if (lra_reg_info[regno].restore_regno < 0
6011 /* If the original pseudo changed its allocation, just
6012 removing the optional pseudo is dangerous as the original
6013 pseudo will have longer live range. */
6014 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
6015 keep_p = true;
6016 else if (reg_renumber[regno] >= 0)
6017 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
6018 {
6019 insn = lra_insn_recog_data[uid]->insn;
6020 if ((set = single_set (insn)) == NULL_RTX)
6021 continue;
6022 src = SET_SRC (set);
6023 dest = SET_DEST (set);
6024 if (! REG_P (src) || ! REG_P (dest))
6025 continue;
6026 if (REGNO (dest) == regno
6027 /* Ignore insn for optional reloads itself. */
6028 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
6029 /* Check only inheritance on last inheritance pass. */
6030 && (int) REGNO (src) >= new_regno_start
6031 /* Check that the optional reload was inherited. */
6032 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6033 {
6034 keep_p = true;
6035 break;
6036 }
6037 }
6038 if (keep_p)
6039 {
6040 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6041 if (lra_dump_file != NULL)
6042 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6043 }
6044 }
6045 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6046 bitmap_initialize (&insn_bitmap, &reg_obstack);
6047 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6048 {
6049 if (lra_dump_file != NULL)
6050 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6051 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6052 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6053 {
6054 insn = lra_insn_recog_data[uid]->insn;
6055 if ((set = single_set (insn)) != NULL_RTX)
6056 {
6057 src = SET_SRC (set);
6058 dest = SET_DEST (set);
6059 if (REG_P (src) && REG_P (dest)
6060 && ((REGNO (src) == regno
6061 && (lra_reg_info[regno].restore_regno
6062 == (int) REGNO (dest)))
6063 || (REGNO (dest) == regno
6064 && (lra_reg_info[regno].restore_regno
6065 == (int) REGNO (src)))))
6066 {
6067 if (lra_dump_file != NULL)
6068 {
6069 fprintf (lra_dump_file, " Deleting move %u\n",
6070 INSN_UID (insn));
6071 dump_insn_slim (lra_dump_file, insn);
6072 }
6073 lra_set_insn_deleted (insn);
6074 continue;
6075 }
6076 /* We should not worry about generation memory-memory
6077 moves here as if the corresponding inheritance did
6078 not work (inheritance pseudo did not get a hard reg),
6079 we remove the inheritance pseudo and the optional
6080 reload. */
6081 }
6082 lra_substitute_pseudo_within_insn
6083 (insn, regno, regno_reg_rtx[lra_reg_info[regno].restore_regno],
6084 false);
6085 lra_update_insn_regno_info (insn);
6086 if (lra_dump_file != NULL)
6087 {
6088 fprintf (lra_dump_file,
6089 " Restoring original insn:\n");
6090 dump_insn_slim (lra_dump_file, insn);
6091 }
6092 }
6093 }
6094 /* Clear restore_regnos. */
6095 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6096 lra_reg_info[regno].restore_regno = -1;
6097 bitmap_clear (&insn_bitmap);
6098 bitmap_clear (&removed_optional_reload_pseudos);
6099 return change_p;
6100 }
6101
6102 /* Entry function for undoing inheritance/split transformation. Return true
6103 if we did any RTL change in this pass. */
6104 bool
6105 lra_undo_inheritance (void)
6106 {
6107 unsigned int regno;
6108 int restore_regno, hard_regno;
6109 int n_all_inherit, n_inherit, n_all_split, n_split;
6110 bitmap_head remove_pseudos;
6111 bitmap_iterator bi;
6112 bool change_p;
6113
6114 lra_undo_inheritance_iter++;
6115 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6116 return false;
6117 if (lra_dump_file != NULL)
6118 fprintf (lra_dump_file,
6119 "\n********** Undoing inheritance #%d: **********\n\n",
6120 lra_undo_inheritance_iter);
6121 bitmap_initialize (&remove_pseudos, &reg_obstack);
6122 n_inherit = n_all_inherit = 0;
6123 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6124 if (lra_reg_info[regno].restore_regno >= 0)
6125 {
6126 n_all_inherit++;
6127 if (reg_renumber[regno] < 0
6128 /* If the original pseudo changed its allocation, just
6129 removing inheritance is dangerous as for changing
6130 allocation we used shorter live-ranges. */
6131 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6132 bitmap_set_bit (&remove_pseudos, regno);
6133 else
6134 n_inherit++;
6135 }
6136 if (lra_dump_file != NULL && n_all_inherit != 0)
6137 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6138 n_inherit, n_all_inherit,
6139 (double) n_inherit / n_all_inherit * 100);
6140 n_split = n_all_split = 0;
6141 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6142 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6143 {
6144 n_all_split++;
6145 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6146 ? reg_renumber[restore_regno] : restore_regno);
6147 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6148 bitmap_set_bit (&remove_pseudos, regno);
6149 else
6150 {
6151 n_split++;
6152 if (lra_dump_file != NULL)
6153 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6154 regno, restore_regno);
6155 }
6156 }
6157 if (lra_dump_file != NULL && n_all_split != 0)
6158 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6159 n_split, n_all_split,
6160 (double) n_split / n_all_split * 100);
6161 change_p = remove_inheritance_pseudos (&remove_pseudos);
6162 bitmap_clear (&remove_pseudos);
6163 /* Clear restore_regnos. */
6164 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6165 lra_reg_info[regno].restore_regno = -1;
6166 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6167 lra_reg_info[regno].restore_regno = -1;
6168 change_p = undo_optional_reloads () || change_p;
6169 return change_p;
6170 }