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