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