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