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