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