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