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