1 /* Move registers around to reduce number of move instructions needed.
2 Copyright (C) 1987, 88, 89, 92-98, 1999 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* This module looks for cases where matching constraints would force
23 an instruction to need a reload, and this reload would be a register
24 to register move. It then attempts to change the registers used by the
25 instruction to avoid the move instruction. */
29 #include "rtl.h" /* stdio.h must precede rtl.h for FFS. */
30 #include "insn-config.h"
35 #include "hard-reg-set.h"
38 #include "insn-flags.h"
39 #include "basic-block.h"
42 static int optimize_reg_copy_1
PROTO((rtx
, rtx
, rtx
));
43 static void optimize_reg_copy_2
PROTO((rtx
, rtx
, rtx
));
44 static void optimize_reg_copy_3
PROTO((rtx
, rtx
, rtx
));
45 static rtx gen_add3_insn
PROTO((rtx
, rtx
, rtx
));
46 static void copy_src_to_dest
PROTO((rtx
, rtx
, rtx
, int));
47 static int *regmove_bb_head
;
50 int with
[MAX_RECOG_OPERANDS
];
51 enum { READ
, WRITE
, READWRITE
} use
[MAX_RECOG_OPERANDS
];
52 int commutative
[MAX_RECOG_OPERANDS
];
53 int early_clobber
[MAX_RECOG_OPERANDS
];
56 static int try_auto_increment
PROTO((rtx
, rtx
, rtx
, rtx
, HOST_WIDE_INT
, int));
57 static int find_matches
PROTO((rtx
, struct match
*));
58 static int fixup_match_1
PROTO((rtx
, rtx
, rtx
, rtx
, rtx
, int, int, int, FILE *))
60 static int reg_is_remote_constant_p
PROTO((rtx
, rtx
, rtx
));
61 static int stable_but_for_p
PROTO((rtx
, rtx
, rtx
));
62 static int regclass_compatible_p
PROTO((int, int));
63 static int loop_depth
;
65 /* Return non-zero if registers with CLASS1 and CLASS2 can be merged without
66 causing too much register allocation problems. */
68 regclass_compatible_p (class0
, class1
)
71 return (class0
== class1
72 || (reg_class_subset_p (class0
, class1
)
73 && ! CLASS_LIKELY_SPILLED_P (class0
))
74 || (reg_class_subset_p (class1
, class0
)
75 && ! CLASS_LIKELY_SPILLED_P (class1
)));
78 /* Generate and return an insn body to add r1 and c,
79 storing the result in r0. */
81 gen_add3_insn (r0
, r1
, c
)
84 int icode
= (int) add_optab
->handlers
[(int) GET_MODE (r0
)].insn_code
;
86 if (icode
== CODE_FOR_nothing
87 || ! (*insn_operand_predicate
[icode
][0]) (r0
, insn_operand_mode
[icode
][0])
88 || ! (*insn_operand_predicate
[icode
][1]) (r1
, insn_operand_mode
[icode
][1])
89 || ! (*insn_operand_predicate
[icode
][2]) (c
, insn_operand_mode
[icode
][2]))
92 return (GEN_FCN (icode
) (r0
, r1
, c
));
96 /* INC_INSN is an instruction that adds INCREMENT to REG.
97 Try to fold INC_INSN as a post/pre in/decrement into INSN.
98 Iff INC_INSN_SET is nonzero, inc_insn has a destination different from src.
99 Return nonzero for success. */
101 try_auto_increment (insn
, inc_insn
, inc_insn_set
, reg
, increment
, pre
)
102 rtx reg
, insn
, inc_insn
,inc_insn_set
;
103 HOST_WIDE_INT increment
;
106 enum rtx_code inc_code
;
108 rtx pset
= single_set (insn
);
111 /* Can't use the size of SET_SRC, we might have something like
112 (sign_extend:SI (mem:QI ... */
113 rtx use
= find_use_as_address (pset
, reg
, 0);
114 if (use
!= 0 && use
!= (rtx
) 1)
116 int size
= GET_MODE_SIZE (GET_MODE (use
));
118 || (HAVE_POST_INCREMENT
119 && pre
== 0 && (inc_code
= POST_INC
, increment
== size
))
120 || (HAVE_PRE_INCREMENT
121 && pre
== 1 && (inc_code
= PRE_INC
, increment
== size
))
122 || (HAVE_POST_DECREMENT
123 && pre
== 0 && (inc_code
= POST_DEC
, increment
== -size
))
124 || (HAVE_PRE_DECREMENT
125 && pre
== 1 && (inc_code
= PRE_DEC
, increment
== -size
))
131 &SET_SRC (inc_insn_set
),
132 XEXP (SET_SRC (inc_insn_set
), 0), 1);
133 validate_change (insn
, &XEXP (use
, 0),
134 gen_rtx_fmt_e (inc_code
, Pmode
, reg
), 1);
135 if (apply_change_group ())
138 = gen_rtx_EXPR_LIST (REG_INC
,
139 reg
, REG_NOTES (insn
));
142 PUT_CODE (inc_insn
, NOTE
);
143 NOTE_LINE_NUMBER (inc_insn
) = NOTE_INSN_DELETED
;
144 NOTE_SOURCE_FILE (inc_insn
) = 0;
154 static int *regno_src_regno
;
156 /* Indicate how good a choice REG (which appears as a source) is to replace
157 a destination register with. The higher the returned value, the better
158 the choice. The main objective is to avoid using a register that is
159 a candidate for tying to a hard register, since the output might in
160 turn be a candidate to be tied to a different hard register. */
162 replacement_quality(reg
)
167 /* Bad if this isn't a register at all. */
168 if (GET_CODE (reg
) != REG
)
171 /* If this register is not meant to get a hard register,
172 it is a poor choice. */
173 if (REG_LIVE_LENGTH (REGNO (reg
)) < 0)
176 src_regno
= regno_src_regno
[REGNO (reg
)];
178 /* If it was not copied from another register, it is fine. */
182 /* Copied from a hard register? */
183 if (src_regno
< FIRST_PSEUDO_REGISTER
)
186 /* Copied from a pseudo register - not as bad as from a hard register,
187 yet still cumbersome, since the register live length will be lengthened
188 when the registers get tied. */
192 /* INSN is a copy from SRC to DEST, both registers, and SRC does not die
195 Search forward to see if SRC dies before either it or DEST is modified,
196 but don't scan past the end of a basic block. If so, we can replace SRC
197 with DEST and let SRC die in INSN.
199 This will reduce the number of registers live in that range and may enable
200 DEST to be tied to SRC, thus often saving one register in addition to a
201 register-register copy. */
204 optimize_reg_copy_1 (insn
, dest
, src
)
212 int sregno
= REGNO (src
);
213 int dregno
= REGNO (dest
);
215 /* We don't want to mess with hard regs if register classes are small. */
217 || (SMALL_REGISTER_CLASSES
218 && (sregno
< FIRST_PSEUDO_REGISTER
219 || dregno
< FIRST_PSEUDO_REGISTER
))
220 /* We don't see all updates to SP if they are in an auto-inc memory
221 reference, so we must disallow this optimization on them. */
222 || sregno
== STACK_POINTER_REGNUM
|| dregno
== STACK_POINTER_REGNUM
)
225 for (p
= NEXT_INSN (insn
); p
; p
= NEXT_INSN (p
))
227 if (GET_CODE (p
) == CODE_LABEL
|| GET_CODE (p
) == JUMP_INSN
228 || (GET_CODE (p
) == NOTE
229 && (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
230 || NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_END
)))
233 /* ??? We can't scan past the end of a basic block without updating
234 the register lifetime info (REG_DEAD/basic_block_live_at_start).
235 A CALL_INSN might be the last insn of a basic block, if it is inside
236 an EH region. There is no easy way to tell, so we just always break
237 when we see a CALL_INSN if flag_exceptions is nonzero. */
238 if (flag_exceptions
&& GET_CODE (p
) == CALL_INSN
)
241 if (GET_RTX_CLASS (GET_CODE (p
)) != 'i')
244 if (reg_set_p (src
, p
) || reg_set_p (dest
, p
)
245 /* Don't change a USE of a register. */
246 || (GET_CODE (PATTERN (p
)) == USE
247 && reg_overlap_mentioned_p (src
, XEXP (PATTERN (p
), 0))))
250 /* See if all of SRC dies in P. This test is slightly more
251 conservative than it needs to be. */
252 if ((note
= find_regno_note (p
, REG_DEAD
, sregno
)) != 0
253 && GET_MODE (XEXP (note
, 0)) == GET_MODE (src
))
261 /* We can do the optimization. Scan forward from INSN again,
262 replacing regs as we go. Set FAILED if a replacement can't
263 be done. In that case, we can't move the death note for SRC.
264 This should be rare. */
266 /* Set to stop at next insn. */
267 for (q
= next_real_insn (insn
);
268 q
!= next_real_insn (p
);
269 q
= next_real_insn (q
))
271 if (reg_overlap_mentioned_p (src
, PATTERN (q
)))
273 /* If SRC is a hard register, we might miss some
274 overlapping registers with validate_replace_rtx,
275 so we would have to undo it. We can't if DEST is
276 present in the insn, so fail in that combination
278 if (sregno
< FIRST_PSEUDO_REGISTER
279 && reg_mentioned_p (dest
, PATTERN (q
)))
282 /* Replace all uses and make sure that the register
283 isn't still present. */
284 else if (validate_replace_rtx (src
, dest
, q
)
285 && (sregno
>= FIRST_PSEUDO_REGISTER
286 || ! reg_overlap_mentioned_p (src
,
289 /* We assume that a register is used exactly once per
290 insn in the REG_N_REFS updates below. If this is not
291 correct, no great harm is done.
293 Since we do not know if we will change the lifetime of
294 SREGNO or DREGNO, we must not update REG_LIVE_LENGTH
295 or REG_N_CALLS_CROSSED at this time. */
296 if (sregno
>= FIRST_PSEUDO_REGISTER
)
297 REG_N_REFS (sregno
) -= loop_depth
;
299 if (dregno
>= FIRST_PSEUDO_REGISTER
)
300 REG_N_REFS (dregno
) += loop_depth
;
304 validate_replace_rtx (dest
, src
, q
);
309 /* For SREGNO, count the total number of insns scanned.
310 For DREGNO, count the total number of insns scanned after
311 passing the death note for DREGNO. */
316 /* If the insn in which SRC dies is a CALL_INSN, don't count it
317 as a call that has been crossed. Otherwise, count it. */
318 if (q
!= p
&& GET_CODE (q
) == CALL_INSN
)
320 /* Similarly, total calls for SREGNO, total calls beyond
321 the death note for DREGNO. */
327 /* If DEST dies here, remove the death note and save it for
328 later. Make sure ALL of DEST dies here; again, this is
329 overly conservative. */
331 && (dest_death
= find_regno_note (q
, REG_DEAD
, dregno
)) != 0)
333 if (GET_MODE (XEXP (dest_death
, 0)) != GET_MODE (dest
))
334 failed
= 1, dest_death
= 0;
336 remove_note (q
, dest_death
);
342 /* These counters need to be updated if and only if we are
343 going to move the REG_DEAD note. */
344 if (sregno
>= FIRST_PSEUDO_REGISTER
)
346 if (REG_LIVE_LENGTH (sregno
) >= 0)
348 REG_LIVE_LENGTH (sregno
) -= s_length
;
349 /* REG_LIVE_LENGTH is only an approximation after
350 combine if sched is not run, so make sure that we
351 still have a reasonable value. */
352 if (REG_LIVE_LENGTH (sregno
) < 2)
353 REG_LIVE_LENGTH (sregno
) = 2;
356 REG_N_CALLS_CROSSED (sregno
) -= s_n_calls
;
359 /* Move death note of SRC from P to INSN. */
360 remove_note (p
, note
);
361 XEXP (note
, 1) = REG_NOTES (insn
);
362 REG_NOTES (insn
) = note
;
365 /* Put death note of DEST on P if we saw it die. */
368 XEXP (dest_death
, 1) = REG_NOTES (p
);
369 REG_NOTES (p
) = dest_death
;
371 if (dregno
>= FIRST_PSEUDO_REGISTER
)
373 /* If and only if we are moving the death note for DREGNO,
374 then we need to update its counters. */
375 if (REG_LIVE_LENGTH (dregno
) >= 0)
376 REG_LIVE_LENGTH (dregno
) += d_length
;
377 REG_N_CALLS_CROSSED (dregno
) += d_n_calls
;
384 /* If SRC is a hard register which is set or killed in some other
385 way, we can't do this optimization. */
386 else if (sregno
< FIRST_PSEUDO_REGISTER
387 && dead_or_set_p (p
, src
))
393 /* INSN is a copy of SRC to DEST, in which SRC dies. See if we now have
394 a sequence of insns that modify DEST followed by an insn that sets
395 SRC to DEST in which DEST dies, with no prior modification of DEST.
396 (There is no need to check if the insns in between actually modify
397 DEST. We should not have cases where DEST is not modified, but
398 the optimization is safe if no such modification is detected.)
399 In that case, we can replace all uses of DEST, starting with INSN and
400 ending with the set of SRC to DEST, with SRC. We do not do this
401 optimization if a CALL_INSN is crossed unless SRC already crosses a
402 call or if DEST dies before the copy back to SRC.
404 It is assumed that DEST and SRC are pseudos; it is too complicated to do
405 this for hard registers since the substitutions we may make might fail. */
408 optimize_reg_copy_2 (insn
, dest
, src
)
415 int sregno
= REGNO (src
);
416 int dregno
= REGNO (dest
);
418 for (p
= NEXT_INSN (insn
); p
; p
= NEXT_INSN (p
))
420 if (GET_CODE (p
) == CODE_LABEL
|| GET_CODE (p
) == JUMP_INSN
421 || (GET_CODE (p
) == NOTE
422 && (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
423 || NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_END
)))
426 /* ??? We can't scan past the end of a basic block without updating
427 the register lifetime info (REG_DEAD/basic_block_live_at_start).
428 A CALL_INSN might be the last insn of a basic block, if it is inside
429 an EH region. There is no easy way to tell, so we just always break
430 when we see a CALL_INSN if flag_exceptions is nonzero. */
431 if (flag_exceptions
&& GET_CODE (p
) == CALL_INSN
)
434 if (GET_RTX_CLASS (GET_CODE (p
)) != 'i')
437 set
= single_set (p
);
438 if (set
&& SET_SRC (set
) == dest
&& SET_DEST (set
) == src
439 && find_reg_note (p
, REG_DEAD
, dest
))
441 /* We can do the optimization. Scan forward from INSN again,
442 replacing regs as we go. */
444 /* Set to stop at next insn. */
445 for (q
= insn
; q
!= NEXT_INSN (p
); q
= NEXT_INSN (q
))
446 if (GET_RTX_CLASS (GET_CODE (q
)) == 'i')
448 if (reg_mentioned_p (dest
, PATTERN (q
)))
450 PATTERN (q
) = replace_rtx (PATTERN (q
), dest
, src
);
452 /* We assume that a register is used exactly once per
453 insn in the updates below. If this is not correct,
454 no great harm is done. */
455 REG_N_REFS (dregno
) -= loop_depth
;
456 REG_N_REFS (sregno
) += loop_depth
;
460 if (GET_CODE (q
) == CALL_INSN
)
462 REG_N_CALLS_CROSSED (dregno
)--;
463 REG_N_CALLS_CROSSED (sregno
)++;
467 remove_note (p
, find_reg_note (p
, REG_DEAD
, dest
));
468 REG_N_DEATHS (dregno
)--;
469 remove_note (insn
, find_reg_note (insn
, REG_DEAD
, src
));
470 REG_N_DEATHS (sregno
)--;
474 if (reg_set_p (src
, p
)
475 || find_reg_note (p
, REG_DEAD
, dest
)
476 || (GET_CODE (p
) == CALL_INSN
&& REG_N_CALLS_CROSSED (sregno
) == 0))
480 /* INSN is a ZERO_EXTEND or SIGN_EXTEND of SRC to DEST.
481 Look if SRC dies there, and if it is only set once, by loading
482 it from memory. If so, try to encorporate the zero/sign extension
483 into the memory read, change SRC to the mode of DEST, and alter
484 the remaining accesses to use the appropriate SUBREG. This allows
485 SRC and DEST to be tied later. */
487 optimize_reg_copy_3 (insn
, dest
, src
)
492 rtx src_reg
= XEXP (src
, 0);
493 int src_no
= REGNO (src_reg
);
494 int dst_no
= REGNO (dest
);
496 enum machine_mode old_mode
;
498 if (src_no
< FIRST_PSEUDO_REGISTER
499 || dst_no
< FIRST_PSEUDO_REGISTER
500 || ! find_reg_note (insn
, REG_DEAD
, src_reg
)
501 || REG_N_SETS (src_no
) != 1)
503 for (p
= PREV_INSN (insn
); ! reg_set_p (src_reg
, p
); p
= PREV_INSN (p
))
505 if (GET_CODE (p
) == CODE_LABEL
|| GET_CODE (p
) == JUMP_INSN
506 || (GET_CODE (p
) == NOTE
507 && (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
508 || NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_END
)))
511 /* ??? We can't scan past the end of a basic block without updating
512 the register lifetime info (REG_DEAD/basic_block_live_at_start).
513 A CALL_INSN might be the last insn of a basic block, if it is inside
514 an EH region. There is no easy way to tell, so we just always break
515 when we see a CALL_INSN if flag_exceptions is nonzero. */
516 if (flag_exceptions
&& GET_CODE (p
) == CALL_INSN
)
519 if (GET_RTX_CLASS (GET_CODE (p
)) != 'i')
522 if (! (set
= single_set (p
))
523 || GET_CODE (SET_SRC (set
)) != MEM
524 || SET_DEST (set
) != src_reg
)
527 /* Be conserative: although this optimization is also valid for
528 volatile memory references, that could cause trouble in later passes. */
529 if (MEM_VOLATILE_P (SET_SRC (set
)))
532 /* Do not use a SUBREG to truncate from one mode to another if truncation
534 if (GET_MODE_BITSIZE (GET_MODE (src_reg
)) <= GET_MODE_BITSIZE (GET_MODE (src
))
535 && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (GET_MODE (src
)),
536 GET_MODE_BITSIZE (GET_MODE (src_reg
))))
539 old_mode
= GET_MODE (src_reg
);
540 PUT_MODE (src_reg
, GET_MODE (src
));
541 XEXP (src
, 0) = SET_SRC (set
);
543 /* Include this change in the group so that it's easily undone if
544 one of the changes in the group is invalid. */
545 validate_change (p
, &SET_SRC (set
), src
, 1);
547 /* Now walk forward making additional replacements. We want to be able
548 to undo all the changes if a later substitution fails. */
549 subreg
= gen_rtx_SUBREG (old_mode
, src_reg
, 0);
550 while (p
= NEXT_INSN (p
), p
!= insn
)
552 if (GET_RTX_CLASS (GET_CODE (p
)) != 'i')
555 /* Make a tenative change. */
556 validate_replace_rtx_group (src_reg
, subreg
, p
);
559 validate_replace_rtx_group (src
, src_reg
, insn
);
561 /* Now see if all the changes are valid. */
562 if (! apply_change_group ())
564 /* One or more changes were no good. Back out everything. */
565 PUT_MODE (src_reg
, old_mode
);
566 XEXP (src
, 0) = src_reg
;
571 /* If we were not able to update the users of src to use dest directly, try
572 instead moving the value to dest directly before the operation. */
575 copy_src_to_dest (insn
, src
, dest
, loop_depth
)
594 /* A REG_LIVE_LENGTH of -1 indicates the register is equivalent to a constant
595 or memory location and is used infrequently; a REG_LIVE_LENGTH of -2 is
596 parameter when there is no frame pointer that is not allocated a register.
597 For now, we just reject them, rather than incrementing the live length. */
599 if (GET_CODE (src
) == REG
600 && REG_LIVE_LENGTH (REGNO (src
)) > 0
601 && GET_CODE (dest
) == REG
602 && REG_LIVE_LENGTH (REGNO (dest
)) > 0
603 && (set
= single_set (insn
)) != NULL_RTX
604 && !reg_mentioned_p (dest
, SET_SRC (set
))
605 && GET_MODE (src
) == GET_MODE (dest
))
607 int old_num_regs
= reg_rtx_no
;
609 /* Generate the src->dest move. */
611 emit_move_insn (dest
, src
);
612 seq
= gen_sequence ();
614 /* If this sequence uses new registers, we may not use it. */
615 if (old_num_regs
!= reg_rtx_no
616 || ! validate_replace_rtx (src
, dest
, insn
))
618 /* We have to restore reg_rtx_no to its old value, lest
619 recompute_reg_usage will try to compute the usage of the
620 new regs, yet reg_n_info is not valid for them. */
621 reg_rtx_no
= old_num_regs
;
624 emit_insn_before (seq
, insn
);
625 move_insn
= PREV_INSN (insn
);
626 p_move_notes
= ®_NOTES (move_insn
);
627 p_insn_notes
= ®_NOTES (insn
);
629 /* Move any notes mentioning src to the move instruction */
630 for (link
= REG_NOTES (insn
); link
!= NULL_RTX
; link
= next
)
632 next
= XEXP (link
, 1);
633 if (XEXP (link
, 0) == src
)
635 *p_move_notes
= link
;
636 p_move_notes
= &XEXP (link
, 1);
640 *p_insn_notes
= link
;
641 p_insn_notes
= &XEXP (link
, 1);
645 *p_move_notes
= NULL_RTX
;
646 *p_insn_notes
= NULL_RTX
;
648 /* Is the insn the head of a basic block? If so extend it */
649 insn_uid
= INSN_UID (insn
);
650 move_uid
= INSN_UID (move_insn
);
651 bb
= regmove_bb_head
[insn_uid
];
654 BLOCK_HEAD (bb
) = move_insn
;
655 regmove_bb_head
[insn_uid
] = -1;
658 /* Update the various register tables. */
659 dest_regno
= REGNO (dest
);
660 REG_N_SETS (dest_regno
) += loop_depth
;
661 REG_N_REFS (dest_regno
) += loop_depth
;
662 REG_LIVE_LENGTH (dest_regno
)++;
663 if (REGNO_FIRST_UID (dest_regno
) == insn_uid
)
664 REGNO_FIRST_UID (dest_regno
) = move_uid
;
666 src_regno
= REGNO (src
);
667 if (! find_reg_note (move_insn
, REG_DEAD
, src
))
668 REG_LIVE_LENGTH (src_regno
)++;
670 if (REGNO_FIRST_UID (src_regno
) == insn_uid
)
671 REGNO_FIRST_UID (src_regno
) = move_uid
;
673 if (REGNO_LAST_UID (src_regno
) == insn_uid
)
674 REGNO_LAST_UID (src_regno
) = move_uid
;
676 if (REGNO_LAST_NOTE_UID (src_regno
) == insn_uid
)
677 REGNO_LAST_NOTE_UID (src_regno
) = move_uid
;
682 /* Return whether REG is set in only one location, and is set to a
683 constant, but is set in a different basic block from INSN (an
684 instructions which uses REG). In this case REG is equivalent to a
685 constant, and we don't want to break that equivalence, because that
686 may increase register pressure and make reload harder. If REG is
687 set in the same basic block as INSN, we don't worry about it,
688 because we'll probably need a register anyhow (??? but what if REG
689 is used in a different basic block as well as this one?). FIRST is
690 the first insn in the function. */
693 reg_is_remote_constant_p (reg
, insn
, first
)
700 if (REG_N_SETS (REGNO (reg
)) != 1)
703 /* Look for the set. */
704 for (p
= LOG_LINKS (insn
); p
; p
= XEXP (p
, 1))
708 if (REG_NOTE_KIND (p
) != 0)
710 s
= single_set (XEXP (p
, 0));
712 && GET_CODE (SET_DEST (s
)) == REG
713 && REGNO (SET_DEST (s
)) == REGNO (reg
))
715 /* The register is set in the same basic block. */
720 for (p
= first
; p
&& p
!= insn
; p
= NEXT_INSN (p
))
724 if (GET_RTX_CLASS (GET_CODE (p
)) != 'i')
728 && GET_CODE (SET_DEST (s
)) == REG
729 && REGNO (SET_DEST (s
)) == REGNO (reg
))
731 /* This is the instruction which sets REG. If there is a
732 REG_EQUAL note, then REG is equivalent to a constant. */
733 if (find_reg_note (p
, REG_EQUAL
, NULL_RTX
))
742 /* INSN is adding a CONST_INT to a REG. We search backwards looking for
743 another add immediate instruction with the same source and dest registers,
744 and if we find one, we change INSN to an increment, and return 1. If
745 no changes are made, we return 0.
748 (set (reg100) (plus reg1 offset1))
750 (set (reg100) (plus reg1 offset2))
752 (set (reg100) (plus reg1 offset1))
754 (set (reg100) (plus reg100 offset2-offset1)) */
756 /* ??? What does this comment mean? */
757 /* cse disrupts preincrement / postdecrement squences when it finds a
758 hard register as ultimate source, like the frame pointer. */
761 fixup_match_2 (insn
, dst
, src
, offset
, regmove_dump_file
)
762 rtx insn
, dst
, src
, offset
;
763 FILE *regmove_dump_file
;
765 rtx p
, dst_death
= 0;
766 int length
, num_calls
= 0;
768 /* If SRC dies in INSN, we'd have to move the death note. This is
769 considered to be very unlikely, so we just skip the optimization
771 if (find_regno_note (insn
, REG_DEAD
, REGNO (src
)))
774 /* Scan backward to find the first instruction that sets DST. */
776 for (length
= 0, p
= PREV_INSN (insn
); p
; p
= PREV_INSN (p
))
780 if (GET_CODE (p
) == CODE_LABEL
781 || GET_CODE (p
) == JUMP_INSN
782 || (GET_CODE (p
) == NOTE
783 && (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
784 || NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_END
)))
787 /* ??? We can't scan past the end of a basic block without updating
788 the register lifetime info (REG_DEAD/basic_block_live_at_start).
789 A CALL_INSN might be the last insn of a basic block, if it is inside
790 an EH region. There is no easy way to tell, so we just always break
791 when we see a CALL_INSN if flag_exceptions is nonzero. */
792 if (flag_exceptions
&& GET_CODE (p
) == CALL_INSN
)
795 if (GET_RTX_CLASS (GET_CODE (p
)) != 'i')
798 if (find_regno_note (p
, REG_DEAD
, REGNO (dst
)))
803 pset
= single_set (p
);
804 if (pset
&& SET_DEST (pset
) == dst
805 && GET_CODE (SET_SRC (pset
)) == PLUS
806 && XEXP (SET_SRC (pset
), 0) == src
807 && GET_CODE (XEXP (SET_SRC (pset
), 1)) == CONST_INT
)
809 HOST_WIDE_INT newconst
810 = INTVAL (offset
) - INTVAL (XEXP (SET_SRC (pset
), 1));
811 rtx add
= gen_add3_insn (dst
, dst
, GEN_INT (newconst
));
813 if (add
&& validate_change (insn
, &PATTERN (insn
), add
, 0))
815 /* Remove the death note for DST from DST_DEATH. */
818 remove_death (REGNO (dst
), dst_death
);
819 REG_LIVE_LENGTH (REGNO (dst
)) += length
;
820 REG_N_CALLS_CROSSED (REGNO (dst
)) += num_calls
;
823 REG_N_REFS (REGNO (dst
)) += loop_depth
;
824 REG_N_REFS (REGNO (src
)) -= loop_depth
;
826 if (regmove_dump_file
)
827 fprintf (regmove_dump_file
,
828 "Fixed operand of insn %d.\n",
832 for (p
= PREV_INSN (insn
); p
; p
= PREV_INSN (p
))
834 if (GET_CODE (p
) == CODE_LABEL
835 || GET_CODE (p
) == JUMP_INSN
836 || (GET_CODE (p
) == NOTE
837 && (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
838 || NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_END
)))
840 if (GET_RTX_CLASS (GET_CODE (p
)) != 'i')
842 if (reg_overlap_mentioned_p (dst
, PATTERN (p
)))
844 if (try_auto_increment (p
, insn
, 0, dst
, newconst
, 0))
849 for (p
= NEXT_INSN (insn
); p
; p
= NEXT_INSN (p
))
851 if (GET_CODE (p
) == CODE_LABEL
852 || GET_CODE (p
) == JUMP_INSN
853 || (GET_CODE (p
) == NOTE
854 && (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
855 || NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_END
)))
857 if (GET_RTX_CLASS (GET_CODE (p
)) != 'i')
859 if (reg_overlap_mentioned_p (dst
, PATTERN (p
)))
861 try_auto_increment (p
, insn
, 0, dst
, newconst
, 1);
870 if (reg_set_p (dst
, PATTERN (p
)))
873 /* If we have passed a call instruction, and the
874 pseudo-reg SRC is not already live across a call,
875 then don't perform the optimization. */
876 /* reg_set_p is overly conservative for CALL_INSNS, thinks that all
877 hard regs are clobbered. Thus, we only use it for src for
879 if (GET_CODE (p
) == CALL_INSN
)
884 if (REG_N_CALLS_CROSSED (REGNO (src
)) == 0)
887 if (call_used_regs
[REGNO (dst
)]
888 || find_reg_fusage (p
, CLOBBER
, dst
))
891 else if (reg_set_p (src
, PATTERN (p
)))
899 regmove_optimize (f
, nregs
, regmove_dump_file
)
902 FILE *regmove_dump_file
;
904 int old_max_uid
= get_max_uid ();
909 rtx copy_src
, copy_dst
;
911 regno_src_regno
= (int *)alloca (sizeof *regno_src_regno
* nregs
);
912 for (i
= nregs
; --i
>= 0; ) regno_src_regno
[i
] = -1;
914 regmove_bb_head
= (int *)alloca (sizeof (int) * (old_max_uid
+ 1));
915 for (i
= old_max_uid
; i
>= 0; i
--) regmove_bb_head
[i
] = -1;
916 for (i
= 0; i
< n_basic_blocks
; i
++)
917 regmove_bb_head
[INSN_UID (BLOCK_HEAD (i
))] = i
;
919 /* A forward/backward pass. Replace output operands with input operands. */
923 for (pass
= 0; pass
<= 2; pass
++)
925 if (! flag_regmove
&& pass
>= flag_expensive_optimizations
)
928 if (regmove_dump_file
)
929 fprintf (regmove_dump_file
, "Starting %s pass...\n",
930 pass
? "backward" : "forward");
932 for (insn
= pass
? get_last_insn () : f
; insn
;
933 insn
= pass
? PREV_INSN (insn
) : NEXT_INSN (insn
))
938 if (GET_CODE (insn
) == NOTE
)
940 if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_BEG
)
942 else if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_END
)
946 set
= single_set (insn
);
950 if (flag_expensive_optimizations
&& ! pass
951 && (GET_CODE (SET_SRC (set
)) == SIGN_EXTEND
952 || GET_CODE (SET_SRC (set
)) == ZERO_EXTEND
)
953 && GET_CODE (XEXP (SET_SRC (set
), 0)) == REG
954 && GET_CODE (SET_DEST(set
)) == REG
)
955 optimize_reg_copy_3 (insn
, SET_DEST (set
), SET_SRC (set
));
957 if (flag_expensive_optimizations
&& ! pass
958 && GET_CODE (SET_SRC (set
)) == REG
959 && GET_CODE (SET_DEST(set
)) == REG
)
961 /* If this is a register-register copy where SRC is not dead,
962 see if we can optimize it. If this optimization succeeds,
963 it will become a copy where SRC is dead. */
964 if ((find_reg_note (insn
, REG_DEAD
, SET_SRC (set
))
965 || optimize_reg_copy_1 (insn
, SET_DEST (set
), SET_SRC (set
)))
966 && REGNO (SET_DEST (set
)) >= FIRST_PSEUDO_REGISTER
)
968 /* Similarly for a pseudo-pseudo copy when SRC is dead. */
969 if (REGNO (SET_SRC (set
)) >= FIRST_PSEUDO_REGISTER
)
970 optimize_reg_copy_2 (insn
, SET_DEST (set
), SET_SRC (set
));
971 if (regno_src_regno
[REGNO (SET_DEST (set
))] < 0
972 && SET_SRC (set
) != SET_DEST (set
))
974 int srcregno
= REGNO (SET_SRC(set
));
975 if (regno_src_regno
[srcregno
] >= 0)
976 srcregno
= regno_src_regno
[srcregno
];
977 regno_src_regno
[REGNO (SET_DEST (set
))] = srcregno
;
984 #ifdef REGISTER_CONSTRAINTS
985 if (! find_matches (insn
, &match
))
988 /* Now scan through the operands looking for a source operand
989 which is supposed to match the destination operand.
990 Then scan forward for an instruction which uses the dest
992 If it dies there, then replace the dest in both operands with
993 the source operand. */
995 for (op_no
= 0; op_no
< recog_n_operands
; op_no
++)
997 rtx src
, dst
, src_subreg
;
998 enum reg_class src_class
, dst_class
;
1000 match_no
= match
.with
[op_no
];
1002 /* Nothing to do if the two operands aren't supposed to match. */
1006 src
= recog_operand
[op_no
];
1007 dst
= recog_operand
[match_no
];
1009 if (GET_CODE (src
) != REG
)
1013 if (GET_CODE (dst
) == SUBREG
1014 && GET_MODE_SIZE (GET_MODE (dst
))
1015 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (dst
))))
1018 = gen_rtx_SUBREG (GET_MODE (SUBREG_REG (dst
)),
1019 src
, SUBREG_WORD (dst
));
1020 dst
= SUBREG_REG (dst
);
1022 if (GET_CODE (dst
) != REG
1023 || REGNO (dst
) < FIRST_PSEUDO_REGISTER
)
1026 if (REGNO (src
) < FIRST_PSEUDO_REGISTER
)
1028 if (match
.commutative
[op_no
] < op_no
)
1029 regno_src_regno
[REGNO (dst
)] = REGNO (src
);
1033 if (REG_LIVE_LENGTH (REGNO (src
)) < 0)
1036 /* op_no/src must be a read-only operand, and
1037 match_operand/dst must be a write-only operand. */
1038 if (match
.use
[op_no
] != READ
1039 || match
.use
[match_no
] != WRITE
)
1042 if (match
.early_clobber
[match_no
]
1043 && count_occurrences (PATTERN (insn
), src
) > 1)
1046 /* Make sure match_operand is the destination. */
1047 if (recog_operand
[match_no
] != SET_DEST (set
))
1050 /* If the operands already match, then there is nothing to do. */
1051 /* But in the commutative case, we might find a better match. */
1052 if (operands_match_p (src
, dst
)
1053 || (match
.commutative
[op_no
] >= 0
1054 && operands_match_p (recog_operand
[match
.commutative
1056 && (replacement_quality (recog_operand
[match
.commutative
1058 >= replacement_quality (src
))))
1061 src_class
= reg_preferred_class (REGNO (src
));
1062 dst_class
= reg_preferred_class (REGNO (dst
));
1063 if (! regclass_compatible_p (src_class
, dst_class
))
1066 if (fixup_match_1 (insn
, set
, src
, src_subreg
, dst
, pass
,
1074 /* A backward pass. Replace input operands with output operands. */
1076 if (regmove_dump_file
)
1077 fprintf (regmove_dump_file
, "Starting backward pass...\n");
1081 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
1083 if (GET_CODE (insn
) == NOTE
)
1085 if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_END
)
1087 else if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_BEG
)
1090 if (GET_RTX_CLASS (GET_CODE (insn
)) == 'i')
1092 int op_no
, match_no
;
1095 if (! find_matches (insn
, &match
))
1098 /* Now scan through the operands looking for a destination operand
1099 which is supposed to match a source operand.
1100 Then scan backward for an instruction which sets the source
1101 operand. If safe, then replace the source operand with the
1102 dest operand in both instructions. */
1104 copy_src
= NULL_RTX
;
1105 copy_dst
= NULL_RTX
;
1106 for (op_no
= 0; op_no
< recog_n_operands
; op_no
++)
1108 rtx set
, p
, src
, dst
;
1109 rtx src_note
, dst_note
;
1111 enum reg_class src_class
, dst_class
;
1114 match_no
= match
.with
[op_no
];
1116 /* Nothing to do if the two operands aren't supposed to match. */
1120 dst
= recog_operand
[match_no
];
1121 src
= recog_operand
[op_no
];
1123 if (GET_CODE (src
) != REG
)
1126 if (GET_CODE (dst
) != REG
1127 || REGNO (dst
) < FIRST_PSEUDO_REGISTER
1128 || REG_LIVE_LENGTH (REGNO (dst
)) < 0)
1131 /* If the operands already match, then there is nothing to do. */
1132 if (operands_match_p (src
, dst
)
1133 || (match
.commutative
[op_no
] >= 0
1134 && operands_match_p (recog_operand
[match
.commutative
[op_no
]], dst
)))
1137 set
= single_set (insn
);
1141 /* match_no/dst must be a write-only operand, and
1142 operand_operand/src must be a read-only operand. */
1143 if (match
.use
[op_no
] != READ
1144 || match
.use
[match_no
] != WRITE
)
1147 if (match
.early_clobber
[match_no
]
1148 && count_occurrences (PATTERN (insn
), src
) > 1)
1151 /* Make sure match_no is the destination. */
1152 if (recog_operand
[match_no
] != SET_DEST (set
))
1155 if (REGNO (src
) < FIRST_PSEUDO_REGISTER
)
1157 if (GET_CODE (SET_SRC (set
)) == PLUS
1158 && GET_CODE (XEXP (SET_SRC (set
), 1)) == CONST_INT
1159 && XEXP (SET_SRC (set
), 0) == src
1160 && fixup_match_2 (insn
, dst
, src
,
1161 XEXP (SET_SRC (set
), 1),
1166 src_class
= reg_preferred_class (REGNO (src
));
1167 dst_class
= reg_preferred_class (REGNO (dst
));
1168 if (! regclass_compatible_p (src_class
, dst_class
))
1178 /* Can not modify an earlier insn to set dst if this insn
1179 uses an old value in the source. */
1180 if (reg_overlap_mentioned_p (dst
, SET_SRC (set
)))
1190 if (! (src_note
= find_reg_note (insn
, REG_DEAD
, src
)))
1201 /* If src is set once in a different basic block,
1202 and is set equal to a constant, then do not use
1203 it for this optimization, as this would make it
1204 no longer equivalent to a constant. */
1206 if (reg_is_remote_constant_p (src
, insn
, f
))
1217 if (regmove_dump_file
)
1218 fprintf (regmove_dump_file
,
1219 "Could fix operand %d of insn %d matching operand %d.\n",
1220 op_no
, INSN_UID (insn
), match_no
);
1222 /* Scan backward to find the first instruction that uses
1223 the input operand. If the operand is set here, then
1224 replace it in both instructions with match_no. */
1226 for (length
= 0, p
= PREV_INSN (insn
); p
; p
= PREV_INSN (p
))
1230 if (GET_CODE (p
) == CODE_LABEL
1231 || GET_CODE (p
) == JUMP_INSN
1232 || (GET_CODE (p
) == NOTE
1233 && (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
1234 || NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_END
)))
1237 /* ??? We can't scan past the end of a basic block without
1238 updating the register lifetime info
1239 (REG_DEAD/basic_block_live_at_start).
1240 A CALL_INSN might be the last insn of a basic block, if
1241 it is inside an EH region. There is no easy way to tell,
1242 so we just always break when we see a CALL_INSN if
1243 flag_exceptions is nonzero. */
1244 if (flag_exceptions
&& GET_CODE (p
) == CALL_INSN
)
1247 if (GET_RTX_CLASS (GET_CODE (p
)) != 'i')
1252 /* ??? See if all of SRC is set in P. This test is much
1253 more conservative than it needs to be. */
1254 pset
= single_set (p
);
1255 if (pset
&& SET_DEST (pset
) == src
)
1257 /* We use validate_replace_rtx, in case there
1258 are multiple identical source operands. All of
1259 them have to be changed at the same time. */
1260 if (validate_replace_rtx (src
, dst
, insn
))
1262 if (validate_change (p
, &SET_DEST (pset
),
1267 /* Change all source operands back.
1268 This modifies the dst as a side-effect. */
1269 validate_replace_rtx (dst
, src
, insn
);
1270 /* Now make sure the dst is right. */
1271 validate_change (insn
,
1272 recog_operand_loc
[match_no
],
1279 if (reg_overlap_mentioned_p (src
, PATTERN (p
))
1280 || reg_overlap_mentioned_p (dst
, PATTERN (p
)))
1283 /* If we have passed a call instruction, and the
1284 pseudo-reg DST is not already live across a call,
1285 then don't perform the optimization. */
1286 if (GET_CODE (p
) == CALL_INSN
)
1290 if (REG_N_CALLS_CROSSED (REGNO (dst
)) == 0)
1299 /* Remove the death note for SRC from INSN. */
1300 remove_note (insn
, src_note
);
1301 /* Move the death note for SRC to P if it is used
1303 if (reg_overlap_mentioned_p (src
, PATTERN (p
)))
1305 XEXP (src_note
, 1) = REG_NOTES (p
);
1306 REG_NOTES (p
) = src_note
;
1308 /* If there is a REG_DEAD note for DST on P, then remove
1309 it, because DST is now set there. */
1310 if ((dst_note
= find_reg_note (p
, REG_DEAD
, dst
)))
1311 remove_note (p
, dst_note
);
1313 dstno
= REGNO (dst
);
1314 srcno
= REGNO (src
);
1316 REG_N_SETS (dstno
)++;
1317 REG_N_SETS (srcno
)--;
1319 REG_N_CALLS_CROSSED (dstno
) += num_calls
;
1320 REG_N_CALLS_CROSSED (srcno
) -= num_calls
;
1322 REG_LIVE_LENGTH (dstno
) += length
;
1323 if (REG_LIVE_LENGTH (srcno
) >= 0)
1325 REG_LIVE_LENGTH (srcno
) -= length
;
1326 /* REG_LIVE_LENGTH is only an approximation after
1327 combine if sched is not run, so make sure that we
1328 still have a reasonable value. */
1329 if (REG_LIVE_LENGTH (srcno
) < 2)
1330 REG_LIVE_LENGTH (srcno
) = 2;
1333 /* We assume that a register is used exactly once per
1334 insn in the updates above. If this is not correct,
1335 no great harm is done. */
1337 REG_N_REFS (dstno
) += 2 * loop_depth
;
1338 REG_N_REFS (srcno
) -= 2 * loop_depth
;
1340 /* If that was the only time src was set,
1341 and src was not live at the start of the
1342 function, we know that we have no more
1343 references to src; clear REG_N_REFS so it
1344 won't make reload do any work. */
1345 if (REG_N_SETS (REGNO (src
)) == 0
1346 && ! regno_uninitialized (REGNO (src
)))
1347 REG_N_REFS (REGNO (src
)) = 0;
1349 if (regmove_dump_file
)
1350 fprintf (regmove_dump_file
,
1351 "Fixed operand %d of insn %d matching operand %d.\n",
1352 op_no
, INSN_UID (insn
), match_no
);
1358 /* If we weren't able to replace any of the alternatives, try an
1359 alternative appoach of copying the source to the destination. */
1360 if (!success
&& copy_src
!= NULL_RTX
)
1361 copy_src_to_dest (insn
, copy_src
, copy_dst
, loop_depth
);
1365 #endif /* REGISTER_CONSTRAINTS */
1367 /* In fixup_match_1, some insns may have been inserted after basic block
1368 ends. Fix that here. */
1369 for (i
= 0; i
< n_basic_blocks
; i
++)
1371 rtx end
= BLOCK_END (i
);
1373 rtx next
= NEXT_INSN (new);
1374 while (next
!= 0 && INSN_UID (next
) >= old_max_uid
1375 && (i
== n_basic_blocks
- 1 || BLOCK_HEAD (i
+ 1) != next
))
1376 new = next
, next
= NEXT_INSN (new);
1377 BLOCK_END (i
) = new;
1381 /* Returns nonzero if INSN's pattern has matching constraints for any operand.
1382 Returns 0 if INSN can't be recognized, or if the alternative can't be
1385 Initialize the info in MATCHP based on the constraints. */
1388 find_matches (insn
, matchp
)
1390 struct match
*matchp
;
1392 int likely_spilled
[MAX_RECOG_OPERANDS
];
1394 int any_matches
= 0;
1396 extract_insn (insn
);
1397 if (! constrain_operands (0))
1400 /* Must initialize this before main loop, because the code for
1401 the commutative case may set matches for operands other than
1403 for (op_no
= recog_n_operands
; --op_no
>= 0; )
1404 matchp
->with
[op_no
] = matchp
->commutative
[op_no
] = -1;
1406 for (op_no
= 0; op_no
< recog_n_operands
; op_no
++)
1411 p
= recog_constraints
[op_no
];
1413 likely_spilled
[op_no
] = 0;
1414 matchp
->use
[op_no
] = READ
;
1415 matchp
->early_clobber
[op_no
] = 0;
1417 matchp
->use
[op_no
] = WRITE
;
1419 matchp
->use
[op_no
] = READWRITE
;
1421 for (;*p
&& i
< which_alternative
; p
++)
1425 while ((c
= *p
++) != '\0' && c
!= ',')
1433 matchp
->early_clobber
[op_no
] = 1;
1436 matchp
->commutative
[op_no
] = op_no
+ 1;
1437 matchp
->commutative
[op_no
+ 1] = op_no
;
1439 case '0': case '1': case '2': case '3': case '4':
1440 case '5': case '6': case '7': case '8': case '9':
1442 if (c
< op_no
&& likely_spilled
[(unsigned char) c
])
1444 matchp
->with
[op_no
] = c
;
1446 if (matchp
->commutative
[op_no
] >= 0)
1447 matchp
->with
[matchp
->commutative
[op_no
]] = c
;
1449 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'h':
1450 case 'j': case 'k': case 'l': case 'p': case 'q': case 't': case 'u':
1451 case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B':
1452 case 'C': case 'D': case 'W': case 'Y': case 'Z':
1453 if (CLASS_LIKELY_SPILLED_P (REG_CLASS_FROM_LETTER ((unsigned char)c
)))
1454 likely_spilled
[op_no
] = 1;
1461 /* Try to replace output operand DST in SET, with input operand SRC. SET is
1462 the only set in INSN. INSN has just been recgnized and constrained.
1463 SRC is operand number OPERAND_NUMBER in INSN.
1464 DST is operand number MATCH_NUMBER in INSN.
1465 If BACKWARD is nonzero, we have been called in a backward pass.
1466 Return nonzero for success. */
1468 fixup_match_1 (insn
, set
, src
, src_subreg
, dst
, backward
, operand_number
,
1469 match_number
, regmove_dump_file
)
1470 rtx insn
, set
, src
, src_subreg
, dst
;
1471 int backward
, operand_number
, match_number
;
1472 FILE *regmove_dump_file
;
1475 rtx post_inc
= 0, post_inc_set
= 0, search_end
= 0;
1477 int num_calls
= 0, s_num_calls
= 0;
1478 enum rtx_code code
= NOTE
;
1479 HOST_WIDE_INT insn_const
, newconst
;
1480 rtx overlap
= 0; /* need to move insn ? */
1481 rtx src_note
= find_reg_note (insn
, REG_DEAD
, src
), dst_note
;
1482 int length
, s_length
, true_loop_depth
;
1486 /* Look for (set (regX) (op regA constX))
1487 (set (regY) (op regA constY))
1489 (set (regA) (op regA constX)).
1490 (set (regY) (op regA constY-constX)).
1491 This works for add and shift operations, if
1492 regA is dead after or set by the second insn. */
1494 code
= GET_CODE (SET_SRC (set
));
1495 if ((code
== PLUS
|| code
== LSHIFTRT
1496 || code
== ASHIFT
|| code
== ASHIFTRT
)
1497 && XEXP (SET_SRC (set
), 0) == src
1498 && GET_CODE (XEXP (SET_SRC (set
), 1)) == CONST_INT
)
1499 insn_const
= INTVAL (XEXP (SET_SRC (set
), 1));
1500 else if (! stable_but_for_p (SET_SRC (set
), src
, dst
))
1503 /* We might find a src_note while scanning. */
1507 if (regmove_dump_file
)
1508 fprintf (regmove_dump_file
,
1509 "Could fix operand %d of insn %d matching operand %d.\n",
1510 operand_number
, INSN_UID (insn
), match_number
);
1512 /* If SRC is equivalent to a constant set in a different basic block,
1513 then do not use it for this optimization. We want the equivalence
1514 so that if we have to reload this register, we can reload the
1515 constant, rather than extending the lifespan of the register. */
1516 if (reg_is_remote_constant_p (src
, insn
, get_insns ()))
1519 /* Scan forward to find the next instruction that
1520 uses the output operand. If the operand dies here,
1521 then replace it in both instructions with
1524 for (length
= s_length
= 0, p
= NEXT_INSN (insn
); p
; p
= NEXT_INSN (p
))
1526 if (GET_CODE (p
) == CODE_LABEL
|| GET_CODE (p
) == JUMP_INSN
1527 || (GET_CODE (p
) == NOTE
1528 && (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
1529 || NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_END
)))
1532 /* ??? We can't scan past the end of a basic block without updating
1533 the register lifetime info (REG_DEAD/basic_block_live_at_start).
1534 A CALL_INSN might be the last insn of a basic block, if it is
1535 inside an EH region. There is no easy way to tell, so we just
1536 always break when we see a CALL_INSN if flag_exceptions is nonzero. */
1537 if (flag_exceptions
&& GET_CODE (p
) == CALL_INSN
)
1540 if (GET_RTX_CLASS (GET_CODE (p
)) != 'i')
1547 if (reg_set_p (src
, p
) || reg_set_p (dst
, p
)
1548 || (GET_CODE (PATTERN (p
)) == USE
1549 && reg_overlap_mentioned_p (src
, XEXP (PATTERN (p
), 0))))
1552 /* See if all of DST dies in P. This test is
1553 slightly more conservative than it needs to be. */
1554 if ((dst_note
= find_regno_note (p
, REG_DEAD
, REGNO (dst
)))
1555 && (GET_MODE (XEXP (dst_note
, 0)) == GET_MODE (dst
)))
1562 /* If an optimization is done, the value of SRC while P
1563 is executed will be changed. Check that this is OK. */
1564 if (reg_overlap_mentioned_p (src
, PATTERN (p
)))
1566 for (q
= p
; q
; q
= NEXT_INSN (q
))
1568 if (GET_CODE (q
) == CODE_LABEL
|| GET_CODE (q
) == JUMP_INSN
1569 || (GET_CODE (q
) == NOTE
1570 && (NOTE_LINE_NUMBER (q
) == NOTE_INSN_LOOP_BEG
1571 || NOTE_LINE_NUMBER (q
) == NOTE_INSN_LOOP_END
)))
1577 /* ??? We can't scan past the end of a basic block without
1578 updating the register lifetime info
1579 (REG_DEAD/basic_block_live_at_start).
1580 A CALL_INSN might be the last insn of a basic block, if
1581 it is inside an EH region. There is no easy way to tell,
1582 so we just always break when we see a CALL_INSN if
1583 flag_exceptions is nonzero. */
1584 if (flag_exceptions
&& GET_CODE (q
) == CALL_INSN
)
1590 if (GET_RTX_CLASS (GET_CODE (q
)) != 'i')
1592 if (reg_overlap_mentioned_p (src
, PATTERN (q
))
1593 || reg_set_p (src
, q
))
1597 set2
= single_set (q
);
1598 if (! q
|| ! set2
|| GET_CODE (SET_SRC (set2
)) != code
1599 || XEXP (SET_SRC (set2
), 0) != src
1600 || GET_CODE (XEXP (SET_SRC (set2
), 1)) != CONST_INT
1601 || (SET_DEST (set2
) != src
1602 && ! find_reg_note (q
, REG_DEAD
, src
)))
1604 /* If this is a PLUS, we can still save a register by doing
1607 src -= insn_const; .
1608 This also gives opportunities for subsequent
1609 optimizations in the backward pass, so do it there. */
1610 if (code
== PLUS
&& backward
1611 /* Don't do this if we can likely tie DST to SET_DEST
1612 of P later; we can't do this tying here if we got a
1614 && ! (dst_note
&& ! REG_N_CALLS_CROSSED (REGNO (dst
))
1616 && GET_CODE (SET_DEST (single_set (p
))) == REG
1617 && (REGNO (SET_DEST (single_set (p
)))
1618 < FIRST_PSEUDO_REGISTER
))
1620 /* We may not emit an insn directly
1621 after P if the latter sets CC0. */
1622 && ! sets_cc0_p (PATTERN (p
))
1630 newconst
= -insn_const
;
1638 newconst
= INTVAL (XEXP (SET_SRC (set2
), 1)) - insn_const
;
1639 /* Reject out of range shifts. */
1643 >= GET_MODE_BITSIZE (GET_MODE (SET_SRC (set2
))))))
1648 if (SET_DEST (set2
) != src
)
1649 post_inc_set
= set2
;
1652 /* We use 1 as last argument to validate_change so that all
1653 changes are accepted or rejected together by apply_change_group
1654 when it is called by validate_replace_rtx . */
1655 validate_change (q
, &XEXP (SET_SRC (set2
), 1),
1656 GEN_INT (newconst
), 1);
1658 validate_change (insn
, recog_operand_loc
[match_number
], src
, 1);
1659 if (validate_replace_rtx (dst
, src_subreg
, p
))
1664 if (reg_overlap_mentioned_p (dst
, PATTERN (p
)))
1666 if (! src_note
&& reg_overlap_mentioned_p (src
, PATTERN (p
)))
1668 /* INSN was already checked to be movable when
1669 we found no REG_DEAD note for src on it. */
1671 src_note
= find_reg_note (p
, REG_DEAD
, src
);
1674 /* If we have passed a call instruction, and the pseudo-reg SRC is not
1675 already live across a call, then don't perform the optimization. */
1676 if (GET_CODE (p
) == CALL_INSN
)
1678 if (REG_N_CALLS_CROSSED (REGNO (src
)) == 0)
1692 true_loop_depth
= backward
? 2 - loop_depth
: loop_depth
;
1694 /* Remove the death note for DST from P. */
1695 remove_note (p
, dst_note
);
1698 post_inc
= emit_insn_after (copy_rtx (PATTERN (insn
)), p
);
1699 if ((HAVE_PRE_INCREMENT
|| HAVE_PRE_DECREMENT
)
1701 && try_auto_increment (search_end
, post_inc
, 0, src
, newconst
, 1))
1703 validate_change (insn
, &XEXP (SET_SRC (set
), 1), GEN_INT (insn_const
), 0);
1704 REG_N_SETS (REGNO (src
))++;
1705 REG_N_REFS (REGNO (src
)) += true_loop_depth
;
1706 REG_LIVE_LENGTH (REGNO (src
))++;
1710 /* The lifetime of src and dest overlap,
1711 but we can change this by moving insn. */
1712 rtx pat
= PATTERN (insn
);
1714 remove_note (overlap
, src_note
);
1715 #if defined (HAVE_POST_INCREMENT) || defined (HAVE_POST_DECREMENT)
1717 && try_auto_increment (overlap
, insn
, 0, src
, insn_const
, 0))
1722 rtx notes
= REG_NOTES (insn
);
1724 emit_insn_after_with_line_notes (pat
, PREV_INSN (p
), insn
);
1725 PUT_CODE (insn
, NOTE
);
1726 NOTE_LINE_NUMBER (insn
) = NOTE_INSN_DELETED
;
1727 NOTE_SOURCE_FILE (insn
) = 0;
1728 /* emit_insn_after_with_line_notes has no
1729 return value, so search for the new insn. */
1730 for (insn
= p
; PATTERN (insn
) != pat
; )
1731 insn
= PREV_INSN (insn
);
1733 REG_NOTES (insn
) = notes
;
1736 /* Sometimes we'd generate src = const; src += n;
1737 if so, replace the instruction that set src
1738 in the first place. */
1740 if (! overlap
&& (code
== PLUS
|| code
== MINUS
))
1742 rtx note
= find_reg_note (insn
, REG_EQUAL
, NULL_RTX
);
1744 int num_calls2
= 0, s_length2
= 0;
1746 if (note
&& CONSTANT_P (XEXP (note
, 0)))
1748 for (q
= PREV_INSN (insn
); q
; q
= PREV_INSN(q
))
1750 if (GET_CODE (q
) == CODE_LABEL
|| GET_CODE (q
) == JUMP_INSN
1751 || (GET_CODE (q
) == NOTE
1752 && (NOTE_LINE_NUMBER (q
) == NOTE_INSN_LOOP_BEG
1753 || NOTE_LINE_NUMBER (q
) == NOTE_INSN_LOOP_END
)))
1759 /* ??? We can't scan past the end of a basic block without
1760 updating the register lifetime info
1761 (REG_DEAD/basic_block_live_at_start).
1762 A CALL_INSN might be the last insn of a basic block, if
1763 it is inside an EH region. There is no easy way to tell,
1764 so we just always break when we see a CALL_INSN if
1765 flag_exceptions is nonzero. */
1766 if (flag_exceptions
&& GET_CODE (q
) == CALL_INSN
)
1772 if (GET_RTX_CLASS (GET_CODE (q
)) != 'i')
1775 if (reg_set_p (src
, q
))
1777 set2
= single_set (q
);
1780 if (reg_overlap_mentioned_p (src
, PATTERN (q
)))
1785 if (GET_CODE (p
) == CALL_INSN
)
1788 if (q
&& set2
&& SET_DEST (set2
) == src
&& CONSTANT_P (SET_SRC (set2
))
1789 && validate_change (insn
, &SET_SRC (set
), XEXP (note
, 0), 0))
1792 NOTE_LINE_NUMBER (q
) = NOTE_INSN_DELETED
;
1793 NOTE_SOURCE_FILE (q
) = 0;
1794 REG_N_SETS (REGNO (src
))--;
1795 REG_N_CALLS_CROSSED (REGNO (src
)) -= num_calls2
;
1796 REG_N_REFS (REGNO (src
)) -= true_loop_depth
;
1797 REG_LIVE_LENGTH (REGNO (src
)) -= s_length2
;
1803 /* Don't remove this seemingly useless if, it is needed to pair with the
1804 else in the next two conditionally included code blocks. */
1807 else if ((HAVE_PRE_INCREMENT
|| HAVE_PRE_DECREMENT
)
1808 && (code
== PLUS
|| code
== MINUS
) && insn_const
1809 && try_auto_increment (p
, insn
, 0, src
, insn_const
, 1))
1811 else if ((HAVE_POST_INCREMENT
|| HAVE_POST_DECREMENT
)
1813 && try_auto_increment (p
, post_inc
, post_inc_set
, src
, newconst
, 0))
1815 /* If post_inc still prevails, try to find an
1816 insn where it can be used as a pre-in/decrement.
1817 If code is MINUS, this was already tried. */
1818 if (post_inc
&& code
== PLUS
1819 /* Check that newconst is likely to be usable
1820 in a pre-in/decrement before starting the search. */
1821 && ((HAVE_PRE_INCREMENT
&& newconst
> 0 && newconst
<= MOVE_MAX
)
1822 || (HAVE_PRE_DECREMENT
&& newconst
< 0 && newconst
>= -MOVE_MAX
))
1823 && exact_log2 (newconst
))
1827 inc_dest
= post_inc_set
? SET_DEST (post_inc_set
) : src
;
1828 for (q
= post_inc
; (q
= NEXT_INSN (q
)); )
1830 if (GET_CODE (q
) == CODE_LABEL
|| GET_CODE (q
) == JUMP_INSN
1831 || (GET_CODE (q
) == NOTE
1832 && (NOTE_LINE_NUMBER (q
) == NOTE_INSN_LOOP_BEG
1833 || NOTE_LINE_NUMBER (q
) == NOTE_INSN_LOOP_END
)))
1836 /* ??? We can't scan past the end of a basic block without updating
1837 the register lifetime info (REG_DEAD/basic_block_live_at_start).
1838 A CALL_INSN might be the last insn of a basic block, if it
1839 is inside an EH region. There is no easy way to tell so we
1840 just always break when we see a CALL_INSN if flag_exceptions
1842 if (flag_exceptions
&& GET_CODE (q
) == CALL_INSN
)
1845 if (GET_RTX_CLASS (GET_CODE (q
)) != 'i')
1847 if (src
!= inc_dest
&& (reg_overlap_mentioned_p (src
, PATTERN (q
))
1848 || reg_set_p (src
, q
)))
1850 if (reg_set_p (inc_dest
, q
))
1852 if (reg_overlap_mentioned_p (inc_dest
, PATTERN (q
)))
1854 try_auto_increment (q
, post_inc
,
1855 post_inc_set
, inc_dest
, newconst
, 1);
1860 /* Move the death note for DST to INSN if it is used
1862 if (reg_overlap_mentioned_p (dst
, PATTERN (insn
)))
1864 XEXP (dst_note
, 1) = REG_NOTES (insn
);
1865 REG_NOTES (insn
) = dst_note
;
1870 /* Move the death note for SRC from INSN to P. */
1872 remove_note (insn
, src_note
);
1873 XEXP (src_note
, 1) = REG_NOTES (p
);
1874 REG_NOTES (p
) = src_note
;
1876 REG_N_CALLS_CROSSED (REGNO (src
)) += s_num_calls
;
1879 REG_N_SETS (REGNO (src
))++;
1880 REG_N_SETS (REGNO (dst
))--;
1882 REG_N_CALLS_CROSSED (REGNO (dst
)) -= num_calls
;
1884 REG_LIVE_LENGTH (REGNO (src
)) += s_length
;
1885 if (REG_LIVE_LENGTH (REGNO (dst
)) >= 0)
1887 REG_LIVE_LENGTH (REGNO (dst
)) -= length
;
1888 /* REG_LIVE_LENGTH is only an approximation after
1889 combine if sched is not run, so make sure that we
1890 still have a reasonable value. */
1891 if (REG_LIVE_LENGTH (REGNO (dst
)) < 2)
1892 REG_LIVE_LENGTH (REGNO (dst
)) = 2;
1895 /* We assume that a register is used exactly once per
1896 insn in the updates above. If this is not correct,
1897 no great harm is done. */
1899 REG_N_REFS (REGNO (src
)) += 2 * true_loop_depth
;
1900 REG_N_REFS (REGNO (dst
)) -= 2 * true_loop_depth
;
1902 /* If that was the only time dst was set,
1903 and dst was not live at the start of the
1904 function, we know that we have no more
1905 references to dst; clear REG_N_REFS so it
1906 won't make reload do any work. */
1907 if (REG_N_SETS (REGNO (dst
)) == 0
1908 && ! regno_uninitialized (REGNO (dst
)))
1909 REG_N_REFS (REGNO (dst
)) = 0;
1911 if (regmove_dump_file
)
1912 fprintf (regmove_dump_file
,
1913 "Fixed operand %d of insn %d matching operand %d.\n",
1914 operand_number
, INSN_UID (insn
), match_number
);
1919 /* return nonzero if X is stable but for mentioning SRC or mentioning /
1920 changing DST . If in doubt, presume it is unstable. */
1922 stable_but_for_p (x
, src
, dst
)
1925 RTX_CODE code
= GET_CODE (x
);
1926 switch (GET_RTX_CLASS (code
))
1928 case '<': case '1': case 'c': case '2': case 'b': case '3':
1931 char *fmt
= GET_RTX_FORMAT (code
);
1932 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1933 if (fmt
[i
] == 'e' && ! stable_but_for_p (XEXP (x
, i
), src
, dst
))
1938 if (x
== src
|| x
== dst
)
1942 return ! rtx_unstable_p (x
);
1946 /* Test if regmove seems profitable for this target. Regmove is useful only
1947 if some common patterns are two address, i.e. require matching constraints,
1948 so we check that condition here. */
1951 regmove_profitable_p ()
1953 #ifdef REGISTER_CONSTRAINTS
1955 enum machine_mode mode
;
1956 optab tstoptab
= add_optab
;
1957 do /* check add_optab and ashl_optab */
1958 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
); mode
!= VOIDmode
;
1959 mode
= GET_MODE_WIDER_MODE (mode
))
1961 int icode
= (int) tstoptab
->handlers
[(int) mode
].insn_code
;
1962 rtx reg0
, reg1
, reg2
, pat
;
1965 if (GET_MODE_BITSIZE (mode
) < 32 || icode
== CODE_FOR_nothing
)
1967 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
1968 if (TEST_HARD_REG_BIT (reg_class_contents
[GENERAL_REGS
], i
))
1970 if (i
+ 2 >= FIRST_PSEUDO_REGISTER
)
1972 reg0
= gen_rtx_REG (insn_operand_mode
[icode
][0], i
);
1973 reg1
= gen_rtx_REG (insn_operand_mode
[icode
][1], i
+ 1);
1974 reg2
= gen_rtx_REG (insn_operand_mode
[icode
][2], i
+ 2);
1975 if (! (*insn_operand_predicate
[icode
][0]) (reg0
, VOIDmode
)
1976 || ! (*insn_operand_predicate
[icode
][1]) (reg1
, VOIDmode
)
1977 || ! (*insn_operand_predicate
[icode
][2]) (reg2
, VOIDmode
))
1979 pat
= GEN_FCN (icode
) (reg0
, reg1
, reg2
);
1982 if (GET_CODE (pat
) == SEQUENCE
)
1983 pat
= XVECEXP (pat
, 0, XVECLEN (pat
, 0) - 1);
1985 pat
= make_insn_raw (pat
);
1986 if (! single_set (pat
)
1987 || GET_CODE (SET_SRC (single_set (pat
))) != tstoptab
->code
)
1988 /* Unexpected complexity; don't need to handle this unless
1989 we find a machine where this occurs and regmove should
1992 if (find_matches (pat
, &match
))
1996 while (tstoptab
!= ashl_optab
&& (tstoptab
= ashl_optab
, 1));
1997 #endif /* REGISTER_CONSTRAINTS */