1 /* Move registers around to reduce number of move instructions needed.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This module makes some simple RTL code transformations which
22 improve the subsequent register allocation. */
26 #include "coretypes.h"
30 #include "insn-config.h"
34 #include "hard-reg-set.h"
38 #include "basic-block.h"
40 #include "diagnostic-core.h"
42 #include "tree-pass.h"
46 static int optimize_reg_copy_1 (rtx
, rtx
, rtx
);
47 static void optimize_reg_copy_2 (rtx
, rtx
, rtx
);
48 static void optimize_reg_copy_3 (rtx
, rtx
, rtx
);
49 static void copy_src_to_dest (rtx
, rtx
, rtx
);
59 int with
[MAX_RECOG_OPERANDS
];
60 enum match_use use
[MAX_RECOG_OPERANDS
];
61 int commutative
[MAX_RECOG_OPERANDS
];
62 int early_clobber
[MAX_RECOG_OPERANDS
];
65 static int find_matches (rtx
, struct match
*);
66 static int fixup_match_2 (rtx
, rtx
, rtx
, rtx
);
68 /* Return nonzero if registers with CLASS1 and CLASS2 can be merged without
69 causing too much register allocation problems. */
71 regclass_compatible_p (reg_class_t class0
, reg_class_t class1
)
73 return (class0
== class1
74 || (reg_class_subset_p (class0
, class1
)
75 && ! targetm
.class_likely_spilled_p (class0
))
76 || (reg_class_subset_p (class1
, class0
)
77 && ! targetm
.class_likely_spilled_p (class1
)));
83 /* Find the place in the rtx X where REG is used as a memory address.
84 Return the MEM rtx that so uses it.
85 If PLUSCONST is nonzero, search instead for a memory address equivalent to
86 (plus REG (const_int PLUSCONST)).
88 If such an address does not appear, return 0.
89 If REG appears more than once, or is used other than in such an address,
93 find_use_as_address (rtx x
, rtx reg
, HOST_WIDE_INT plusconst
)
95 enum rtx_code code
= GET_CODE (x
);
96 const char * const fmt
= GET_RTX_FORMAT (code
);
101 if (code
== MEM
&& XEXP (x
, 0) == reg
&& plusconst
== 0)
104 if (code
== MEM
&& GET_CODE (XEXP (x
, 0)) == PLUS
105 && XEXP (XEXP (x
, 0), 0) == reg
106 && CONST_INT_P (XEXP (XEXP (x
, 0), 1))
107 && INTVAL (XEXP (XEXP (x
, 0), 1)) == plusconst
)
110 if (code
== SIGN_EXTRACT
|| code
== ZERO_EXTRACT
)
112 /* If REG occurs inside a MEM used in a bit-field reference,
113 that is unacceptable. */
114 if (find_use_as_address (XEXP (x
, 0), reg
, 0) != 0)
115 return (rtx
) (size_t) 1;
119 return (rtx
) (size_t) 1;
121 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
125 tem
= find_use_as_address (XEXP (x
, i
), reg
, plusconst
);
129 return (rtx
) (size_t) 1;
131 else if (fmt
[i
] == 'E')
134 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
136 tem
= find_use_as_address (XVECEXP (x
, i
, j
), reg
, plusconst
);
140 return (rtx
) (size_t) 1;
149 /* INC_INSN is an instruction that adds INCREMENT to REG.
150 Try to fold INC_INSN as a post/pre in/decrement into INSN.
151 Iff INC_INSN_SET is nonzero, inc_insn has a destination different from src.
152 Return nonzero for success. */
154 try_auto_increment (rtx insn
, rtx inc_insn
, rtx inc_insn_set
, rtx reg
,
155 HOST_WIDE_INT increment
, int pre
)
157 enum rtx_code inc_code
;
159 rtx pset
= single_set (insn
);
162 /* Can't use the size of SET_SRC, we might have something like
163 (sign_extend:SI (mem:QI ... */
164 rtx use
= find_use_as_address (pset
, reg
, 0);
165 if (use
!= 0 && use
!= (rtx
) (size_t) 1)
167 int size
= GET_MODE_SIZE (GET_MODE (use
));
169 || (HAVE_POST_INCREMENT
170 && pre
== 0 && (inc_code
= POST_INC
, increment
== size
))
171 || (HAVE_PRE_INCREMENT
172 && pre
== 1 && (inc_code
= PRE_INC
, increment
== size
))
173 || (HAVE_POST_DECREMENT
174 && pre
== 0 && (inc_code
= POST_DEC
, increment
== -size
))
175 || (HAVE_PRE_DECREMENT
176 && pre
== 1 && (inc_code
= PRE_DEC
, increment
== -size
))
182 &SET_SRC (inc_insn_set
),
183 XEXP (SET_SRC (inc_insn_set
), 0), 1);
184 validate_change (insn
, &XEXP (use
, 0),
185 gen_rtx_fmt_e (inc_code
,
186 GET_MODE (XEXP (use
, 0)), reg
),
188 if (apply_change_group ())
190 /* If there is a REG_DEAD note on this insn, we must
191 change this not to REG_UNUSED meaning that the register
192 is set, but the value is dead. Failure to do so will
193 result in sched1 dying -- when it recomputes lifetime
194 information, the number of REG_DEAD notes will have
196 rtx note
= find_reg_note (insn
, REG_DEAD
, reg
);
198 PUT_REG_NOTE_KIND (note
, REG_UNUSED
);
200 add_reg_note (insn
, REG_INC
, reg
);
203 delete_insn (inc_insn
);
214 static int *regno_src_regno
;
216 /* INSN is a copy from SRC to DEST, both registers, and SRC does not die
219 Search forward to see if SRC dies before either it or DEST is modified,
220 but don't scan past the end of a basic block. If so, we can replace SRC
221 with DEST and let SRC die in INSN.
223 This will reduce the number of registers live in that range and may enable
224 DEST to be tied to SRC, thus often saving one register in addition to a
225 register-register copy. */
228 optimize_reg_copy_1 (rtx insn
, rtx dest
, rtx src
)
233 int sregno
= REGNO (src
);
234 int dregno
= REGNO (dest
);
235 basic_block bb
= BLOCK_FOR_INSN (insn
);
237 /* We don't want to mess with hard regs if register classes are small. */
239 || (targetm
.small_register_classes_for_mode_p (GET_MODE (src
))
240 && (sregno
< FIRST_PSEUDO_REGISTER
241 || dregno
< FIRST_PSEUDO_REGISTER
))
242 /* We don't see all updates to SP if they are in an auto-inc memory
243 reference, so we must disallow this optimization on them. */
244 || sregno
== STACK_POINTER_REGNUM
|| dregno
== STACK_POINTER_REGNUM
)
247 for (p
= NEXT_INSN (insn
); p
; p
= NEXT_INSN (p
))
251 if (BLOCK_FOR_INSN (p
) != bb
)
254 if (reg_set_p (src
, p
) || reg_set_p (dest
, p
)
255 /* If SRC is an asm-declared register, it must not be replaced
256 in any asm. Unfortunately, the REG_EXPR tree for the asm
257 variable may be absent in the SRC rtx, so we can't check the
258 actual register declaration easily (the asm operand will have
259 it, though). To avoid complicating the test for a rare case,
260 we just don't perform register replacement for a hard reg
261 mentioned in an asm. */
262 || (sregno
< FIRST_PSEUDO_REGISTER
263 && asm_noperands (PATTERN (p
)) >= 0
264 && reg_overlap_mentioned_p (src
, PATTERN (p
)))
265 /* Don't change hard registers used by a call. */
266 || (CALL_P (p
) && sregno
< FIRST_PSEUDO_REGISTER
267 && find_reg_fusage (p
, USE
, src
))
268 /* Don't change a USE of a register. */
269 || (GET_CODE (PATTERN (p
)) == USE
270 && reg_overlap_mentioned_p (src
, XEXP (PATTERN (p
), 0))))
273 /* See if all of SRC dies in P. This test is slightly more
274 conservative than it needs to be. */
275 if ((note
= find_regno_note (p
, REG_DEAD
, sregno
)) != 0
276 && GET_MODE (XEXP (note
, 0)) == GET_MODE (src
))
283 int s_freq_calls
= 0;
284 int d_freq_calls
= 0;
286 /* We can do the optimization. Scan forward from INSN again,
287 replacing regs as we go. Set FAILED if a replacement can't
288 be done. In that case, we can't move the death note for SRC.
289 This should be rare. */
291 /* Set to stop at next insn. */
292 for (q
= next_real_insn (insn
);
293 q
!= next_real_insn (p
);
294 q
= next_real_insn (q
))
296 if (reg_overlap_mentioned_p (src
, PATTERN (q
)))
298 /* If SRC is a hard register, we might miss some
299 overlapping registers with validate_replace_rtx,
300 so we would have to undo it. We can't if DEST is
301 present in the insn, so fail in that combination
303 if (sregno
< FIRST_PSEUDO_REGISTER
304 && reg_mentioned_p (dest
, PATTERN (q
)))
307 /* Attempt to replace all uses. */
308 else if (!validate_replace_rtx (src
, dest
, q
))
311 /* If this succeeded, but some part of the register
312 is still present, undo the replacement. */
313 else if (sregno
< FIRST_PSEUDO_REGISTER
314 && reg_overlap_mentioned_p (src
, PATTERN (q
)))
316 validate_replace_rtx (dest
, src
, q
);
321 /* For SREGNO, count the total number of insns scanned.
322 For DREGNO, count the total number of insns scanned after
323 passing the death note for DREGNO. */
324 if (!DEBUG_INSN_P (p
))
331 /* If the insn in which SRC dies is a CALL_INSN, don't count it
332 as a call that has been crossed. Otherwise, count it. */
333 if (q
!= p
&& CALL_P (q
))
335 /* Similarly, total calls for SREGNO, total calls beyond
336 the death note for DREGNO. */
338 s_freq_calls
+= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (q
));
342 d_freq_calls
+= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (q
));
346 /* If DEST dies here, remove the death note and save it for
347 later. Make sure ALL of DEST dies here; again, this is
348 overly conservative. */
350 && (dest_death
= find_regno_note (q
, REG_DEAD
, dregno
)) != 0)
352 if (GET_MODE (XEXP (dest_death
, 0)) != GET_MODE (dest
))
353 failed
= 1, dest_death
= 0;
355 remove_note (q
, dest_death
);
361 /* These counters need to be updated if and only if we are
362 going to move the REG_DEAD note. */
363 if (sregno
>= FIRST_PSEUDO_REGISTER
)
365 if (REG_LIVE_LENGTH (sregno
) >= 0)
367 REG_LIVE_LENGTH (sregno
) -= s_length
;
368 /* REG_LIVE_LENGTH is only an approximation after
369 combine if sched is not run, so make sure that we
370 still have a reasonable value. */
371 if (REG_LIVE_LENGTH (sregno
) < 2)
372 REG_LIVE_LENGTH (sregno
) = 2;
375 REG_N_CALLS_CROSSED (sregno
) -= s_n_calls
;
376 REG_FREQ_CALLS_CROSSED (sregno
) -= s_freq_calls
;
379 /* Move death note of SRC from P to INSN. */
380 remove_note (p
, note
);
381 XEXP (note
, 1) = REG_NOTES (insn
);
382 REG_NOTES (insn
) = note
;
385 /* DEST is also dead if INSN has a REG_UNUSED note for DEST. */
387 && (dest_death
= find_regno_note (insn
, REG_UNUSED
, dregno
)))
389 PUT_REG_NOTE_KIND (dest_death
, REG_DEAD
);
390 remove_note (insn
, dest_death
);
393 /* Put death note of DEST on P if we saw it die. */
396 XEXP (dest_death
, 1) = REG_NOTES (p
);
397 REG_NOTES (p
) = dest_death
;
399 if (dregno
>= FIRST_PSEUDO_REGISTER
)
401 /* If and only if we are moving the death note for DREGNO,
402 then we need to update its counters. */
403 if (REG_LIVE_LENGTH (dregno
) >= 0)
404 REG_LIVE_LENGTH (dregno
) += d_length
;
405 REG_N_CALLS_CROSSED (dregno
) += d_n_calls
;
406 REG_FREQ_CALLS_CROSSED (dregno
) += d_freq_calls
;
413 /* If SRC is a hard register which is set or killed in some other
414 way, we can't do this optimization. */
415 else if (sregno
< FIRST_PSEUDO_REGISTER
416 && dead_or_set_p (p
, src
))
422 /* INSN is a copy of SRC to DEST, in which SRC dies. See if we now have
423 a sequence of insns that modify DEST followed by an insn that sets
424 SRC to DEST in which DEST dies, with no prior modification of DEST.
425 (There is no need to check if the insns in between actually modify
426 DEST. We should not have cases where DEST is not modified, but
427 the optimization is safe if no such modification is detected.)
428 In that case, we can replace all uses of DEST, starting with INSN and
429 ending with the set of SRC to DEST, with SRC. We do not do this
430 optimization if a CALL_INSN is crossed unless SRC already crosses a
431 call or if DEST dies before the copy back to SRC.
433 It is assumed that DEST and SRC are pseudos; it is too complicated to do
434 this for hard registers since the substitutions we may make might fail. */
437 optimize_reg_copy_2 (rtx insn
, rtx dest
, rtx src
)
441 int sregno
= REGNO (src
);
442 int dregno
= REGNO (dest
);
443 basic_block bb
= BLOCK_FOR_INSN (insn
);
445 for (p
= NEXT_INSN (insn
); p
; p
= NEXT_INSN (p
))
449 if (BLOCK_FOR_INSN (p
) != bb
)
452 set
= single_set (p
);
453 if (set
&& SET_SRC (set
) == dest
&& SET_DEST (set
) == src
454 && find_reg_note (p
, REG_DEAD
, dest
))
456 /* We can do the optimization. Scan forward from INSN again,
457 replacing regs as we go. */
459 /* Set to stop at next insn. */
460 for (q
= insn
; q
!= NEXT_INSN (p
); q
= NEXT_INSN (q
))
463 if (reg_mentioned_p (dest
, PATTERN (q
)))
467 PATTERN (q
) = replace_rtx (PATTERN (q
), dest
, src
);
468 note
= FIND_REG_INC_NOTE (q
, dest
);
471 remove_note (q
, note
);
472 add_reg_note (q
, REG_INC
, src
);
479 int freq
= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (q
));
480 REG_N_CALLS_CROSSED (dregno
)--;
481 REG_N_CALLS_CROSSED (sregno
)++;
482 REG_FREQ_CALLS_CROSSED (dregno
) -= freq
;
483 REG_FREQ_CALLS_CROSSED (sregno
) += freq
;
487 remove_note (p
, find_reg_note (p
, REG_DEAD
, dest
));
488 REG_N_DEATHS (dregno
)--;
489 remove_note (insn
, find_reg_note (insn
, REG_DEAD
, src
));
490 REG_N_DEATHS (sregno
)--;
494 if (reg_set_p (src
, p
)
495 || find_reg_note (p
, REG_DEAD
, dest
)
496 || (CALL_P (p
) && REG_N_CALLS_CROSSED (sregno
) == 0))
501 /* INSN is a ZERO_EXTEND or SIGN_EXTEND of SRC to DEST.
502 Look if SRC dies there, and if it is only set once, by loading
503 it from memory. If so, try to incorporate the zero/sign extension
504 into the memory read, change SRC to the mode of DEST, and alter
505 the remaining accesses to use the appropriate SUBREG. This allows
506 SRC and DEST to be tied later. */
508 optimize_reg_copy_3 (rtx insn
, rtx dest
, rtx src
)
510 rtx src_reg
= XEXP (src
, 0);
511 int src_no
= REGNO (src_reg
);
512 int dst_no
= REGNO (dest
);
513 rtx p
, set
, set_insn
;
514 enum machine_mode old_mode
;
515 basic_block bb
= BLOCK_FOR_INSN (insn
);
517 if (src_no
< FIRST_PSEUDO_REGISTER
518 || dst_no
< FIRST_PSEUDO_REGISTER
519 || ! find_reg_note (insn
, REG_DEAD
, src_reg
)
520 || REG_N_DEATHS (src_no
) != 1
521 || REG_N_SETS (src_no
) != 1)
524 for (p
= PREV_INSN (insn
); p
&& ! reg_set_p (src_reg
, p
); p
= PREV_INSN (p
))
525 if (INSN_P (p
) && BLOCK_FOR_INSN (p
) != bb
)
528 if (! p
|| BLOCK_FOR_INSN (p
) != bb
)
531 if (! (set
= single_set (p
))
532 || !MEM_P (SET_SRC (set
))
533 /* If there's a REG_EQUIV note, this must be an insn that loads an
534 argument. Prefer keeping the note over doing this optimization. */
535 || find_reg_note (p
, REG_EQUIV
, NULL_RTX
)
536 || SET_DEST (set
) != src_reg
)
539 /* Be conservative: although this optimization is also valid for
540 volatile memory references, that could cause trouble in later passes. */
541 if (MEM_VOLATILE_P (SET_SRC (set
)))
544 /* Do not use a SUBREG to truncate from one mode to another if truncation
546 if (GET_MODE_BITSIZE (GET_MODE (src_reg
)) <= GET_MODE_BITSIZE (GET_MODE (src
))
547 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (src
), GET_MODE (src_reg
)))
551 old_mode
= GET_MODE (src_reg
);
552 PUT_MODE (src_reg
, GET_MODE (src
));
553 XEXP (src
, 0) = SET_SRC (set
);
555 /* Include this change in the group so that it's easily undone if
556 one of the changes in the group is invalid. */
557 validate_change (p
, &SET_SRC (set
), src
, 1);
559 /* Now walk forward making additional replacements. We want to be able
560 to undo all the changes if a later substitution fails. */
561 while (p
= NEXT_INSN (p
), p
!= insn
)
566 /* Make a tentative change. */
567 validate_replace_rtx_group (src_reg
,
568 gen_lowpart_SUBREG (old_mode
, src_reg
),
572 validate_replace_rtx_group (src
, src_reg
, insn
);
574 /* Now see if all the changes are valid. */
575 if (! apply_change_group ())
577 /* One or more changes were no good. Back out everything. */
578 PUT_MODE (src_reg
, old_mode
);
579 XEXP (src
, 0) = src_reg
;
583 rtx note
= find_reg_note (set_insn
, REG_EQUAL
, NULL_RTX
);
586 if (rtx_equal_p (XEXP (note
, 0), XEXP (src
, 0)))
589 = gen_rtx_fmt_e (GET_CODE (src
), GET_MODE (src
),
591 df_notes_rescan (set_insn
);
594 remove_note (set_insn
, note
);
600 /* If we were not able to update the users of src to use dest directly, try
601 instead moving the value to dest directly before the operation. */
604 copy_src_to_dest (rtx insn
, rtx src
, rtx dest
)
616 /* A REG_LIVE_LENGTH of -1 indicates the register must not go into
617 a hard register, e.g. because it crosses as setjmp. See the
618 comment in regstat.c:regstat_bb_compute_ri. Don't try to apply
619 any transformations to such regs. */
622 && REG_LIVE_LENGTH (REGNO (src
)) > 0
624 && REG_LIVE_LENGTH (REGNO (dest
)) > 0
625 && (set
= single_set (insn
)) != NULL_RTX
626 && !reg_mentioned_p (dest
, SET_SRC (set
))
627 && GET_MODE (src
) == GET_MODE (dest
))
629 int old_num_regs
= reg_rtx_no
;
631 /* Generate the src->dest move. */
633 emit_move_insn (dest
, src
);
636 /* If this sequence uses new registers, we may not use it. */
637 if (old_num_regs
!= reg_rtx_no
638 || ! validate_replace_rtx (src
, dest
, insn
))
640 /* We have to restore reg_rtx_no to its old value, lest
641 recompute_reg_usage will try to compute the usage of the
642 new regs, yet reg_n_info is not valid for them. */
643 reg_rtx_no
= old_num_regs
;
646 emit_insn_before (seq
, insn
);
647 move_insn
= PREV_INSN (insn
);
648 p_move_notes
= ®_NOTES (move_insn
);
649 p_insn_notes
= ®_NOTES (insn
);
651 /* Move any notes mentioning src to the move instruction. */
652 for (link
= REG_NOTES (insn
); link
!= NULL_RTX
; link
= next
)
654 next
= XEXP (link
, 1);
655 if (XEXP (link
, 0) == src
)
657 *p_move_notes
= link
;
658 p_move_notes
= &XEXP (link
, 1);
662 *p_insn_notes
= link
;
663 p_insn_notes
= &XEXP (link
, 1);
667 *p_move_notes
= NULL_RTX
;
668 *p_insn_notes
= NULL_RTX
;
670 /* Update the various register tables. */
671 dest_regno
= REGNO (dest
);
672 INC_REG_N_SETS (dest_regno
, 1);
673 REG_LIVE_LENGTH (dest_regno
)++;
674 src_regno
= REGNO (src
);
675 if (! find_reg_note (move_insn
, REG_DEAD
, src
))
676 REG_LIVE_LENGTH (src_regno
)++;
680 /* reg_set_in_bb[REGNO] points to basic block iff the register is set
681 only once in the given block and has REG_EQUAL note. */
683 static basic_block
*reg_set_in_bb
;
685 /* Size of reg_set_in_bb array. */
686 static unsigned int max_reg_computed
;
689 /* Return whether REG is set in only one location, and is set to a
690 constant, but is set in a different basic block from INSN (an
691 instructions which uses REG). In this case REG is equivalent to a
692 constant, and we don't want to break that equivalence, because that
693 may increase register pressure and make reload harder. If REG is
694 set in the same basic block as INSN, we don't worry about it,
695 because we'll probably need a register anyhow (??? but what if REG
696 is used in a different basic block as well as this one?). */
699 reg_is_remote_constant_p (rtx reg
, rtx insn
)
707 max_reg_computed
= max
= max_reg_num ();
708 reg_set_in_bb
= XCNEWVEC (basic_block
, max
);
718 /* This is the instruction which sets REG. If there is a
719 REG_EQUAL note, then REG is equivalent to a constant. */
721 && REG_P (SET_DEST (s
))
722 && REG_N_SETS (REGNO (SET_DEST (s
))) == 1
723 && find_reg_note (p
, REG_EQUAL
, NULL_RTX
))
724 reg_set_in_bb
[REGNO (SET_DEST (s
))] = bb
;
728 gcc_assert (REGNO (reg
) < max_reg_computed
);
729 if (reg_set_in_bb
[REGNO (reg
)] == NULL
)
731 return (reg_set_in_bb
[REGNO (reg
)] != BLOCK_FOR_INSN (insn
));
734 /* INSN is adding a CONST_INT to a REG. We search backwards looking for
735 another add immediate instruction with the same source and dest registers,
736 and if we find one, we change INSN to an increment, and return 1. If
737 no changes are made, we return 0.
740 (set (reg100) (plus reg1 offset1))
742 (set (reg100) (plus reg1 offset2))
744 (set (reg100) (plus reg1 offset1))
746 (set (reg100) (plus reg100 offset2-offset1)) */
748 /* ??? What does this comment mean? */
749 /* cse disrupts preincrement / postdecrement sequences when it finds a
750 hard register as ultimate source, like the frame pointer. */
753 fixup_match_2 (rtx insn
, rtx dst
, rtx src
, rtx offset
)
755 rtx p
, dst_death
= 0;
756 int length
, num_calls
= 0, freq_calls
= 0;
757 basic_block bb
= BLOCK_FOR_INSN (insn
);
759 /* If SRC dies in INSN, we'd have to move the death note. This is
760 considered to be very unlikely, so we just skip the optimization
762 if (find_regno_note (insn
, REG_DEAD
, REGNO (src
)))
765 /* Scan backward to find the first instruction that sets DST. */
767 for (length
= 0, p
= PREV_INSN (insn
); p
; p
= PREV_INSN (p
))
773 if (BLOCK_FOR_INSN (p
) != bb
)
776 if (find_regno_note (p
, REG_DEAD
, REGNO (dst
)))
778 if (! dst_death
&& !DEBUG_INSN_P (p
))
781 pset
= single_set (p
);
782 if (pset
&& SET_DEST (pset
) == dst
783 && GET_CODE (SET_SRC (pset
)) == PLUS
784 && XEXP (SET_SRC (pset
), 0) == src
785 && CONST_INT_P (XEXP (SET_SRC (pset
), 1)))
787 HOST_WIDE_INT newconst
788 = INTVAL (offset
) - INTVAL (XEXP (SET_SRC (pset
), 1));
789 rtx add
= gen_add3_insn (dst
, dst
, GEN_INT (newconst
));
791 if (add
&& validate_change (insn
, &PATTERN (insn
), add
, 0))
793 /* Remove the death note for DST from DST_DEATH. */
796 remove_death (REGNO (dst
), dst_death
);
797 REG_LIVE_LENGTH (REGNO (dst
)) += length
;
798 REG_N_CALLS_CROSSED (REGNO (dst
)) += num_calls
;
799 REG_FREQ_CALLS_CROSSED (REGNO (dst
)) += freq_calls
;
804 "Fixed operand of insn %d.\n",
808 for (p
= PREV_INSN (insn
); p
; p
= PREV_INSN (p
))
812 if (BLOCK_FOR_INSN (p
) != bb
)
814 if (reg_overlap_mentioned_p (dst
, PATTERN (p
)))
816 if (try_auto_increment (p
, insn
, 0, dst
, newconst
, 0))
821 for (p
= NEXT_INSN (insn
); p
; p
= NEXT_INSN (p
))
825 if (BLOCK_FOR_INSN (p
) != bb
)
827 if (reg_overlap_mentioned_p (dst
, PATTERN (p
)))
829 try_auto_increment (p
, insn
, 0, dst
, newconst
, 1);
838 if (reg_set_p (dst
, PATTERN (p
)))
841 /* If we have passed a call instruction, and the
842 pseudo-reg SRC is not already live across a call,
843 then don't perform the optimization. */
844 /* reg_set_p is overly conservative for CALL_INSNS, thinks that all
845 hard regs are clobbered. Thus, we only use it for src for
852 freq_calls
+= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (p
));
855 if (REG_N_CALLS_CROSSED (REGNO (src
)) == 0)
858 if ((HARD_REGISTER_P (dst
) && call_used_regs
[REGNO (dst
)])
859 || find_reg_fusage (p
, CLOBBER
, dst
))
862 else if (reg_set_p (src
, PATTERN (p
)))
869 /* A forward pass. Replace output operands with input operands. */
872 regmove_forward_pass (void)
877 if (! flag_expensive_optimizations
)
881 fprintf (dump_file
, "Starting forward pass...\n");
885 FOR_BB_INSNS (bb
, insn
)
887 rtx set
= single_set (insn
);
891 if ((GET_CODE (SET_SRC (set
)) == SIGN_EXTEND
892 || GET_CODE (SET_SRC (set
)) == ZERO_EXTEND
)
893 && REG_P (XEXP (SET_SRC (set
), 0))
894 && REG_P (SET_DEST (set
)))
895 optimize_reg_copy_3 (insn
, SET_DEST (set
), SET_SRC (set
));
897 if (REG_P (SET_SRC (set
))
898 && REG_P (SET_DEST (set
)))
900 /* If this is a register-register copy where SRC is not dead,
901 see if we can optimize it. If this optimization succeeds,
902 it will become a copy where SRC is dead. */
903 if ((find_reg_note (insn
, REG_DEAD
, SET_SRC (set
))
904 || optimize_reg_copy_1 (insn
, SET_DEST (set
), SET_SRC (set
)))
905 && REGNO (SET_DEST (set
)) >= FIRST_PSEUDO_REGISTER
)
907 /* Similarly for a pseudo-pseudo copy when SRC is dead. */
908 if (REGNO (SET_SRC (set
)) >= FIRST_PSEUDO_REGISTER
)
909 optimize_reg_copy_2 (insn
, SET_DEST (set
), SET_SRC (set
));
910 if (regno_src_regno
[REGNO (SET_DEST (set
))] < 0
911 && SET_SRC (set
) != SET_DEST (set
))
913 int srcregno
= REGNO (SET_SRC (set
));
914 if (regno_src_regno
[srcregno
] >= 0)
915 srcregno
= regno_src_regno
[srcregno
];
916 regno_src_regno
[REGNO (SET_DEST (set
))] = srcregno
;
924 /* A backward pass. Replace input operands with output operands. */
927 regmove_backward_pass (void)
933 fprintf (dump_file
, "Starting backward pass...\n");
935 FOR_EACH_BB_REVERSE (bb
)
937 /* ??? Use the safe iterator because fixup_match_2 can remove
938 insns via try_auto_increment. */
939 FOR_BB_INSNS_REVERSE_SAFE (bb
, insn
, prev
)
942 rtx copy_src
, copy_dst
;
949 if (! find_matches (insn
, &match
))
952 /* Now scan through the operands looking for a destination operand
953 which is supposed to match a source operand.
954 Then scan backward for an instruction which sets the source
955 operand. If safe, then replace the source operand with the
956 dest operand in both instructions. */
960 for (op_no
= 0; op_no
< recog_data
.n_operands
; op_no
++)
962 rtx set
, p
, src
, dst
;
963 rtx src_note
, dst_note
;
964 int num_calls
= 0, freq_calls
= 0;
965 enum reg_class src_class
, dst_class
;
968 match_no
= match
.with
[op_no
];
970 /* Nothing to do if the two operands aren't supposed to match. */
974 dst
= recog_data
.operand
[match_no
];
975 src
= recog_data
.operand
[op_no
];
981 || REGNO (dst
) < FIRST_PSEUDO_REGISTER
982 || REG_LIVE_LENGTH (REGNO (dst
)) < 0
983 || GET_MODE (src
) != GET_MODE (dst
))
986 /* If the operands already match, then there is nothing to do. */
987 if (operands_match_p (src
, dst
))
990 if (match
.commutative
[op_no
] >= 0)
992 rtx comm
= recog_data
.operand
[match
.commutative
[op_no
]];
993 if (operands_match_p (comm
, dst
))
997 set
= single_set (insn
);
1001 /* Note that single_set ignores parts of a parallel set for
1002 which one of the destinations is REG_UNUSED. We can't
1003 handle that here, since we can wind up rewriting things
1004 such that a single register is set twice within a single
1006 if (reg_set_p (src
, insn
))
1009 /* match_no/dst must be a write-only operand, and
1010 operand_operand/src must be a read-only operand. */
1011 if (match
.use
[op_no
] != READ
1012 || match
.use
[match_no
] != WRITE
)
1015 if (match
.early_clobber
[match_no
]
1016 && count_occurrences (PATTERN (insn
), src
, 0) > 1)
1019 /* Make sure match_no is the destination. */
1020 if (recog_data
.operand
[match_no
] != SET_DEST (set
))
1023 if (REGNO (src
) < FIRST_PSEUDO_REGISTER
)
1025 if (GET_CODE (SET_SRC (set
)) == PLUS
1026 && CONST_INT_P (XEXP (SET_SRC (set
), 1))
1027 && XEXP (SET_SRC (set
), 0) == src
1028 && fixup_match_2 (insn
, dst
, src
,
1029 XEXP (SET_SRC (set
), 1)))
1033 src_class
= reg_preferred_class (REGNO (src
));
1034 dst_class
= reg_preferred_class (REGNO (dst
));
1036 if (! (src_note
= find_reg_note (insn
, REG_DEAD
, src
)))
1038 /* We used to force the copy here like in other cases, but
1039 it produces worse code, as it eliminates no copy
1040 instructions and the copy emitted will be produced by
1041 reload anyway. On patterns with multiple alternatives,
1042 there may be better solution available.
1044 In particular this change produced slower code for numeric
1050 if (! regclass_compatible_p (src_class
, dst_class
))
1060 /* Can not modify an earlier insn to set dst if this insn
1061 uses an old value in the source. */
1062 if (reg_overlap_mentioned_p (dst
, SET_SRC (set
)))
1072 /* If src is set once in a different basic block,
1073 and is set equal to a constant, then do not use
1074 it for this optimization, as this would make it
1075 no longer equivalent to a constant. */
1077 if (reg_is_remote_constant_p (src
, insn
))
1090 "Could fix operand %d of insn %d matching operand %d.\n",
1091 op_no
, INSN_UID (insn
), match_no
);
1093 /* Scan backward to find the first instruction that uses
1094 the input operand. If the operand is set here, then
1095 replace it in both instructions with match_no. */
1097 for (length
= 0, p
= PREV_INSN (insn
); p
; p
= PREV_INSN (p
))
1103 if (BLOCK_FOR_INSN (p
) != bb
)
1106 if (!DEBUG_INSN_P (p
))
1109 /* ??? See if all of SRC is set in P. This test is much
1110 more conservative than it needs to be. */
1111 pset
= single_set (p
);
1112 if (pset
&& SET_DEST (pset
) == src
)
1114 /* We use validate_replace_rtx, in case there
1115 are multiple identical source operands. All
1116 of them have to be changed at the same time:
1117 when validate_replace_rtx() calls
1118 apply_change_group(). */
1119 validate_change (p
, &SET_DEST (pset
), dst
, 1);
1120 if (validate_replace_rtx (src
, dst
, insn
))
1125 /* We can't make this change if DST is mentioned at
1126 all in P, since we are going to change its value.
1127 We can't make this change if SRC is read or
1128 partially written in P, since we are going to
1129 eliminate SRC. However, if it's a debug insn, we
1130 can't refrain from making the change, for this
1131 would cause codegen differences, so instead we
1132 invalidate debug expressions that reference DST,
1133 and adjust references to SRC in them so that they
1134 become references to DST. */
1135 if (reg_mentioned_p (dst
, PATTERN (p
)))
1137 if (DEBUG_INSN_P (p
))
1138 validate_change (p
, &INSN_VAR_LOCATION_LOC (p
),
1139 gen_rtx_UNKNOWN_VAR_LOC (), 1);
1143 if (reg_overlap_mentioned_p (src
, PATTERN (p
)))
1145 if (DEBUG_INSN_P (p
))
1146 validate_replace_rtx_group (src
, dst
, p
);
1151 /* If we have passed a call instruction, and the
1152 pseudo-reg DST is not already live across a call,
1153 then don't perform the optimization. */
1157 freq_calls
+= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (p
));
1159 if (REG_N_CALLS_CROSSED (REGNO (dst
)) == 0)
1168 /* Remove the death note for SRC from INSN. */
1169 remove_note (insn
, src_note
);
1170 /* Move the death note for SRC to P if it is used
1172 if (reg_overlap_mentioned_p (src
, PATTERN (p
)))
1174 XEXP (src_note
, 1) = REG_NOTES (p
);
1175 REG_NOTES (p
) = src_note
;
1177 /* If there is a REG_DEAD note for DST on P, then remove
1178 it, because DST is now set there. */
1179 if ((dst_note
= find_reg_note (p
, REG_DEAD
, dst
)))
1180 remove_note (p
, dst_note
);
1182 dstno
= REGNO (dst
);
1183 srcno
= REGNO (src
);
1185 INC_REG_N_SETS (dstno
, 1);
1186 INC_REG_N_SETS (srcno
, -1);
1188 REG_N_CALLS_CROSSED (dstno
) += num_calls
;
1189 REG_N_CALLS_CROSSED (srcno
) -= num_calls
;
1190 REG_FREQ_CALLS_CROSSED (dstno
) += freq_calls
;
1191 REG_FREQ_CALLS_CROSSED (srcno
) -= freq_calls
;
1193 REG_LIVE_LENGTH (dstno
) += length
;
1194 if (REG_LIVE_LENGTH (srcno
) >= 0)
1196 REG_LIVE_LENGTH (srcno
) -= length
;
1197 /* REG_LIVE_LENGTH is only an approximation after
1198 combine if sched is not run, so make sure that we
1199 still have a reasonable value. */
1200 if (REG_LIVE_LENGTH (srcno
) < 2)
1201 REG_LIVE_LENGTH (srcno
) = 2;
1206 "Fixed operand %d of insn %d matching operand %d.\n",
1207 op_no
, INSN_UID (insn
), match_no
);
1211 else if (num_changes_pending () > 0)
1215 /* If we weren't able to replace any of the alternatives, try an
1216 alternative approach of copying the source to the destination. */
1217 if (!success
&& copy_src
!= NULL_RTX
)
1218 copy_src_to_dest (insn
, copy_src
, copy_dst
);
1223 /* Main entry for the register move optimization. */
1226 regmove_optimize (void)
1229 int nregs
= max_reg_num ();
1231 df_note_add_problem ();
1234 regstat_init_n_sets_and_refs ();
1235 regstat_compute_ri ();
1237 if (flag_ira_loop_pressure
)
1238 ira_set_pseudo_classes (true, dump_file
);
1240 regno_src_regno
= XNEWVEC (int, nregs
);
1241 for (i
= nregs
; --i
>= 0; )
1242 regno_src_regno
[i
] = -1;
1244 /* A forward pass. Replace output operands with input operands. */
1245 regmove_forward_pass ();
1247 /* A backward pass. Replace input operands with output operands. */
1248 regmove_backward_pass ();
1251 free (regno_src_regno
);
1254 free (reg_set_in_bb
);
1255 reg_set_in_bb
= NULL
;
1257 regstat_free_n_sets_and_refs ();
1259 if (flag_ira_loop_pressure
)
1264 /* Returns nonzero if INSN's pattern has matching constraints for any operand.
1265 Returns 0 if INSN can't be recognized, or if the alternative can't be
1268 Initialize the info in MATCHP based on the constraints. */
1271 find_matches (rtx insn
, struct match
*matchp
)
1273 int likely_spilled
[MAX_RECOG_OPERANDS
];
1275 int any_matches
= 0;
1277 extract_insn (insn
);
1278 if (! constrain_operands (0))
1281 /* Must initialize this before main loop, because the code for
1282 the commutative case may set matches for operands other than
1284 for (op_no
= recog_data
.n_operands
; --op_no
>= 0; )
1285 matchp
->with
[op_no
] = matchp
->commutative
[op_no
] = -1;
1287 for (op_no
= 0; op_no
< recog_data
.n_operands
; op_no
++)
1293 p
= recog_data
.constraints
[op_no
];
1295 likely_spilled
[op_no
] = 0;
1296 matchp
->use
[op_no
] = READ
;
1297 matchp
->early_clobber
[op_no
] = 0;
1299 matchp
->use
[op_no
] = WRITE
;
1301 matchp
->use
[op_no
] = READWRITE
;
1303 for (;*p
&& i
< which_alternative
; p
++)
1307 while ((c
= *p
) != '\0' && c
!= ',')
1316 matchp
->early_clobber
[op_no
] = 1;
1319 matchp
->commutative
[op_no
] = op_no
+ 1;
1320 matchp
->commutative
[op_no
+ 1] = op_no
;
1323 case '0': case '1': case '2': case '3': case '4':
1324 case '5': case '6': case '7': case '8': case '9':
1327 unsigned long match_ul
= strtoul (p
, &end
, 10);
1328 int match
= match_ul
;
1332 if (match
< op_no
&& likely_spilled
[match
])
1334 matchp
->with
[op_no
] = match
;
1336 if (matchp
->commutative
[op_no
] >= 0)
1337 matchp
->with
[matchp
->commutative
[op_no
]] = match
;
1341 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'h':
1342 case 'j': case 'k': case 'l': case 'p': case 'q': case 't': case 'u':
1343 case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B':
1344 case 'C': case 'D': case 'W': case 'Y': case 'Z':
1345 if (targetm
.class_likely_spilled_p (REG_CLASS_FROM_CONSTRAINT ((unsigned char) c
, p
)))
1346 likely_spilled
[op_no
] = 1;
1349 p
+= CONSTRAINT_LEN (c
, p
);
1358 gate_handle_regmove (void)
1360 return (optimize
> 0 && flag_regmove
);
1364 struct rtl_opt_pass pass_regmove
=
1368 "regmove", /* name */
1369 OPTGROUP_NONE
, /* optinfo_flags */
1370 gate_handle_regmove
, /* gate */
1371 regmove_optimize
, /* execute */
1374 0, /* static_pass_number */
1375 TV_REGMOVE
, /* tv_id */
1376 0, /* properties_required */
1377 0, /* properties_provided */
1378 0, /* properties_destroyed */
1379 0, /* todo_flags_start */
1380 TODO_df_finish
| TODO_verify_rtl_sharing
/* todo_flags_finish */