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