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