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