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