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