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