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