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