1 /* Move registers around to reduce number of move instructions needed.
2 Copyright (C) 1987, 88, 89, 92-97, 1998 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* This module looks for cases where matching constraints would force
22 an instruction to need a reload, and this reload would be a register
23 to register move. It then attempts to change the registers used by the
24 instruction to avoid the move instruction. */
33 /* stdio.h must precede rtl.h for FFS. */
37 #include "insn-config.h"
42 #include "hard-reg-set.h"
45 #include "insn-flags.h"
47 static int optimize_reg_copy_1
PROTO((rtx
, rtx
, rtx
));
48 static void optimize_reg_copy_2
PROTO((rtx
, rtx
, rtx
));
49 static void optimize_reg_copy_3
PROTO((rtx
, rtx
, rtx
));
52 int with
[MAX_RECOG_OPERANDS
];
53 enum { READ
, WRITE
, READWRITE
} use
[MAX_RECOG_OPERANDS
];
54 int commutative
[MAX_RECOG_OPERANDS
];
55 int early_clobber
[MAX_RECOG_OPERANDS
];
58 static int find_matches
PROTO((rtx
, struct match
*));
59 static int fixup_match_1
PROTO((rtx
, rtx
, rtx
, rtx
, rtx
, int, int, int, FILE *))
61 static int reg_is_remote_constant_p
PROTO((rtx
, rtx
, rtx
));
62 static int stable_but_for_p
PROTO((rtx
, rtx
, rtx
));
63 static int loop_depth
;
67 /* INC_INSN is an instruction that adds INCREMENT to REG.
68 Try to fold INC_INSN as a post/pre in/decrement into INSN.
69 Iff INC_INSN_SET is nonzero, inc_insn has a destination different from src.
70 Return nonzero for success. */
72 try_auto_increment (insn
, inc_insn
, inc_insn_set
, reg
, increment
, pre
)
73 rtx reg
, insn
, inc_insn
,inc_insn_set
;
74 HOST_WIDE_INT increment
;
77 enum rtx_code inc_code
;
79 rtx pset
= single_set (insn
);
82 /* Can't use the size of SET_SRC, we might have something like
83 (sign_extend:SI (mem:QI ... */
84 rtx use
= find_use_as_address (pset
, reg
, 0);
85 if (use
!= 0 && use
!= (rtx
) 1)
87 int size
= GET_MODE_SIZE (GET_MODE (use
));
89 #ifdef HAVE_POST_INCREMENT
90 || (pre
== 0 && (inc_code
= POST_INC
, increment
== size
))
92 #ifdef HAVE_PRE_INCREMENT
93 || (pre
== 1 && (inc_code
= PRE_INC
, increment
== size
))
95 #ifdef HAVE_POST_DECREMENT
96 || (pre
== 0 && (inc_code
= POST_DEC
, increment
== -size
))
98 #ifdef HAVE_PRE_DECREMENT
99 || (pre
== 1 && (inc_code
= PRE_DEC
, increment
== -size
))
106 &SET_SRC (inc_insn_set
),
107 XEXP (SET_SRC (inc_insn_set
), 0), 1);
108 validate_change (insn
, &XEXP (use
, 0),
109 gen_rtx_fmt_e (inc_code
, Pmode
, reg
), 1);
110 if (apply_change_group ())
113 = gen_rtx_EXPR_LIST (REG_INC
,
114 reg
, REG_NOTES (insn
));
117 PUT_CODE (inc_insn
, NOTE
);
118 NOTE_LINE_NUMBER (inc_insn
) = NOTE_INSN_DELETED
;
119 NOTE_SOURCE_FILE (inc_insn
) = 0;
128 #endif /* AUTO_INC_DEC */
130 static int *regno_src_regno
;
132 /* Indicate how good a choice REG (which appears as a source) is to replace
133 a destination register with. The higher the returned value, the better
134 the choice. The main objective is to avoid using a register that is
135 a candidate for tying to a hard register, since the output might in
136 turn be a candidate to be tied to a different hard register. */
138 replacement_quality(reg
)
143 /* Bad if this isn't a register at all. */
144 if (GET_CODE (reg
) != REG
)
147 /* If this register is not meant to get a hard register,
148 it is a poor choice. */
149 if (REG_LIVE_LENGTH (REGNO (reg
)) < 0)
152 src_regno
= regno_src_regno
[REGNO (reg
)];
154 /* If it was not copied from another register, it is fine. */
158 /* Copied from a hard register? */
159 if (src_regno
< FIRST_PSEUDO_REGISTER
)
162 /* Copied from a pseudo register - not as bad as from a hard register,
163 yet still cumbersome, since the register live length will be lengthened
164 when the registers get tied. */
168 /* INSN is a copy from SRC to DEST, both registers, and SRC does not die
171 Search forward to see if SRC dies before either it or DEST is modified,
172 but don't scan past the end of a basic block. If so, we can replace SRC
173 with DEST and let SRC die in INSN.
175 This will reduce the number of registers live in that range and may enable
176 DEST to be tied to SRC, thus often saving one register in addition to a
177 register-register copy. */
180 optimize_reg_copy_1 (insn
, dest
, src
)
188 int sregno
= REGNO (src
);
189 int dregno
= REGNO (dest
);
191 /* We don't want to mess with hard regs if register classes are small. */
193 || (SMALL_REGISTER_CLASSES
194 && (sregno
< FIRST_PSEUDO_REGISTER
195 || dregno
< FIRST_PSEUDO_REGISTER
))
196 /* We don't see all updates to SP if they are in an auto-inc memory
197 reference, so we must disallow this optimization on them. */
198 || sregno
== STACK_POINTER_REGNUM
|| dregno
== STACK_POINTER_REGNUM
)
201 for (p
= NEXT_INSN (insn
); p
; p
= NEXT_INSN (p
))
203 if (GET_CODE (p
) == CODE_LABEL
|| GET_CODE (p
) == JUMP_INSN
204 || (GET_CODE (p
) == NOTE
205 && (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
206 || NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_END
)))
209 /* ??? We can't scan past the end of a basic block without updating
210 the register lifetime info (REG_DEAD/basic_block_live_at_start).
211 A CALL_INSN might be the last insn of a basic block, if it is inside
212 an EH region. There is no easy way to tell, so we just always break
213 when we see a CALL_INSN if flag_exceptions is nonzero. */
214 if (flag_exceptions
&& GET_CODE (p
) == CALL_INSN
)
217 if (GET_RTX_CLASS (GET_CODE (p
)) != 'i')
220 if (reg_set_p (src
, p
) || reg_set_p (dest
, p
)
221 /* Don't change a USE of a register. */
222 || (GET_CODE (PATTERN (p
)) == USE
223 && reg_overlap_mentioned_p (src
, XEXP (PATTERN (p
), 0))))
226 /* See if all of SRC dies in P. This test is slightly more
227 conservative than it needs to be. */
228 if ((note
= find_regno_note (p
, REG_DEAD
, sregno
)) != 0
229 && GET_MODE (XEXP (note
, 0)) == GET_MODE (src
))
237 /* We can do the optimization. Scan forward from INSN again,
238 replacing regs as we go. Set FAILED if a replacement can't
239 be done. In that case, we can't move the death note for SRC.
240 This should be rare. */
242 /* Set to stop at next insn. */
243 for (q
= next_real_insn (insn
);
244 q
!= next_real_insn (p
);
245 q
= next_real_insn (q
))
247 if (reg_overlap_mentioned_p (src
, PATTERN (q
)))
249 /* If SRC is a hard register, we might miss some
250 overlapping registers with validate_replace_rtx,
251 so we would have to undo it. We can't if DEST is
252 present in the insn, so fail in that combination
254 if (sregno
< FIRST_PSEUDO_REGISTER
255 && reg_mentioned_p (dest
, PATTERN (q
)))
258 /* Replace all uses and make sure that the register
259 isn't still present. */
260 else if (validate_replace_rtx (src
, dest
, q
)
261 && (sregno
>= FIRST_PSEUDO_REGISTER
262 || ! reg_overlap_mentioned_p (src
,
265 /* We assume that a register is used exactly once per
266 insn in the updates below. If this is not correct,
267 no great harm is done. */
268 if (sregno
>= FIRST_PSEUDO_REGISTER
)
269 REG_N_REFS (sregno
) -= loop_depth
;
270 if (dregno
>= FIRST_PSEUDO_REGISTER
)
271 REG_N_REFS (dregno
) += loop_depth
;
275 validate_replace_rtx (dest
, src
, q
);
280 /* Count the insns and CALL_INSNs passed. If we passed the
281 death note of DEST, show increased live length. */
286 /* If the insn in which SRC dies is a CALL_INSN, don't count it
287 as a call that has been crossed. Otherwise, count it. */
288 if (q
!= p
&& GET_CODE (q
) == CALL_INSN
)
295 /* If DEST dies here, remove the death note and save it for
296 later. Make sure ALL of DEST dies here; again, this is
297 overly conservative. */
299 && (dest_death
= find_regno_note (q
, REG_DEAD
, dregno
)) != 0)
301 if (GET_MODE (XEXP (dest_death
, 0)) != GET_MODE (dest
))
302 failed
= 1, dest_death
= 0;
304 remove_note (q
, dest_death
);
310 if (sregno
>= FIRST_PSEUDO_REGISTER
)
312 if (REG_LIVE_LENGTH (sregno
) >= 0)
314 REG_LIVE_LENGTH (sregno
) -= length
;
315 /* REG_LIVE_LENGTH is only an approximation after
316 combine if sched is not run, so make sure that we
317 still have a reasonable value. */
318 if (REG_LIVE_LENGTH (sregno
) < 2)
319 REG_LIVE_LENGTH (sregno
) = 2;
322 REG_N_CALLS_CROSSED (sregno
) -= n_calls
;
325 if (dregno
>= FIRST_PSEUDO_REGISTER
)
327 if (REG_LIVE_LENGTH (dregno
) >= 0)
328 REG_LIVE_LENGTH (dregno
) += d_length
;
330 REG_N_CALLS_CROSSED (dregno
) += d_n_calls
;
333 /* Move death note of SRC from P to INSN. */
334 remove_note (p
, note
);
335 XEXP (note
, 1) = REG_NOTES (insn
);
336 REG_NOTES (insn
) = note
;
339 /* Put death note of DEST on P if we saw it die. */
342 XEXP (dest_death
, 1) = REG_NOTES (p
);
343 REG_NOTES (p
) = dest_death
;
349 /* If SRC is a hard register which is set or killed in some other
350 way, we can't do this optimization. */
351 else if (sregno
< FIRST_PSEUDO_REGISTER
352 && dead_or_set_p (p
, src
))
358 /* INSN is a copy of SRC to DEST, in which SRC dies. See if we now have
359 a sequence of insns that modify DEST followed by an insn that sets
360 SRC to DEST in which DEST dies, with no prior modification of DEST.
361 (There is no need to check if the insns in between actually modify
362 DEST. We should not have cases where DEST is not modified, but
363 the optimization is safe if no such modification is detected.)
364 In that case, we can replace all uses of DEST, starting with INSN and
365 ending with the set of SRC to DEST, with SRC. We do not do this
366 optimization if a CALL_INSN is crossed unless SRC already crosses a
367 call or if DEST dies before the copy back to SRC.
369 It is assumed that DEST and SRC are pseudos; it is too complicated to do
370 this for hard registers since the substitutions we may make might fail. */
373 optimize_reg_copy_2 (insn
, dest
, src
)
380 int sregno
= REGNO (src
);
381 int dregno
= REGNO (dest
);
383 for (p
= NEXT_INSN (insn
); p
; p
= NEXT_INSN (p
))
385 if (GET_CODE (p
) == CODE_LABEL
|| GET_CODE (p
) == JUMP_INSN
386 || (GET_CODE (p
) == NOTE
387 && (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
388 || NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_END
)))
391 /* ??? We can't scan past the end of a basic block without updating
392 the register lifetime info (REG_DEAD/basic_block_live_at_start).
393 A CALL_INSN might be the last insn of a basic block, if it is inside
394 an EH region. There is no easy way to tell, so we just always break
395 when we see a CALL_INSN if flag_exceptions is nonzero. */
396 if (flag_exceptions
&& GET_CODE (p
) == CALL_INSN
)
399 if (GET_RTX_CLASS (GET_CODE (p
)) != 'i')
402 set
= single_set (p
);
403 if (set
&& SET_SRC (set
) == dest
&& SET_DEST (set
) == src
404 && find_reg_note (p
, REG_DEAD
, dest
))
406 /* We can do the optimization. Scan forward from INSN again,
407 replacing regs as we go. */
409 /* Set to stop at next insn. */
410 for (q
= insn
; q
!= NEXT_INSN (p
); q
= NEXT_INSN (q
))
411 if (GET_RTX_CLASS (GET_CODE (q
)) == 'i')
413 if (reg_mentioned_p (dest
, PATTERN (q
)))
415 PATTERN (q
) = replace_rtx (PATTERN (q
), dest
, src
);
417 /* We assume that a register is used exactly once per
418 insn in the updates below. If this is not correct,
419 no great harm is done. */
420 REG_N_REFS (dregno
) -= loop_depth
;
421 REG_N_REFS (sregno
) += loop_depth
;
425 if (GET_CODE (q
) == CALL_INSN
)
427 REG_N_CALLS_CROSSED (dregno
)--;
428 REG_N_CALLS_CROSSED (sregno
)++;
432 remove_note (p
, find_reg_note (p
, REG_DEAD
, dest
));
433 REG_N_DEATHS (dregno
)--;
434 remove_note (insn
, find_reg_note (insn
, REG_DEAD
, src
));
435 REG_N_DEATHS (sregno
)--;
439 if (reg_set_p (src
, p
)
440 || find_reg_note (p
, REG_DEAD
, dest
)
441 || (GET_CODE (p
) == CALL_INSN
&& REG_N_CALLS_CROSSED (sregno
) == 0))
445 /* INSN is a ZERO_EXTEND or SIGN_EXTEND of SRC to DEST.
446 Look if SRC dies there, and if it is only set once, by loading
447 it from memory. If so, try to encorporate the zero/sign extension
448 into the memory read, change SRC to the mode of DEST, and alter
449 the remaining accesses to use the appropriate SUBREG. This allows
450 SRC and DEST to be tied later. */
452 optimize_reg_copy_3 (insn
, dest
, src
)
457 rtx src_reg
= XEXP (src
, 0);
458 int src_no
= REGNO (src_reg
);
459 int dst_no
= REGNO (dest
);
461 enum machine_mode old_mode
;
463 if (src_no
< FIRST_PSEUDO_REGISTER
464 || dst_no
< FIRST_PSEUDO_REGISTER
465 || ! find_reg_note (insn
, REG_DEAD
, src_reg
)
466 || REG_N_SETS (src_no
) != 1)
468 for (p
= PREV_INSN (insn
); ! reg_set_p (src_reg
, p
); p
= PREV_INSN (p
))
470 if (GET_CODE (p
) == CODE_LABEL
|| GET_CODE (p
) == JUMP_INSN
471 || (GET_CODE (p
) == NOTE
472 && (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
473 || NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_END
)))
476 /* ??? We can't scan past the end of a basic block without updating
477 the register lifetime info (REG_DEAD/basic_block_live_at_start).
478 A CALL_INSN might be the last insn of a basic block, if it is inside
479 an EH region. There is no easy way to tell, so we just always break
480 when we see a CALL_INSN if flag_exceptions is nonzero. */
481 if (flag_exceptions
&& GET_CODE (p
) == CALL_INSN
)
484 if (GET_RTX_CLASS (GET_CODE (p
)) != 'i')
487 if (! (set
= single_set (p
))
488 || GET_CODE (SET_SRC (set
)) != MEM
489 || SET_DEST (set
) != src_reg
)
491 old_mode
= GET_MODE (src_reg
);
492 PUT_MODE (src_reg
, GET_MODE (src
));
493 XEXP (src
, 0) = SET_SRC (set
);
494 if (! validate_change (p
, &SET_SRC (set
), src
, 0))
496 PUT_MODE (src_reg
, old_mode
);
497 XEXP (src
, 0) = src_reg
;
500 subreg
= gen_rtx_SUBREG (old_mode
, src_reg
, 0);
501 while (p
= NEXT_INSN (p
), p
!= insn
)
503 if (GET_RTX_CLASS (GET_CODE (p
)) != 'i')
505 validate_replace_rtx (src_reg
, subreg
, p
);
507 validate_replace_rtx (src
, src_reg
, insn
);
510 /* Return whether REG is set in only one location, and is set to a
511 constant, but is set in a different basic block from INSN (an
512 instructions which uses REG). In this case REG is equivalent to a
513 constant, and we don't want to break that equivalence, because that
514 may increase register pressure and make reload harder. If REG is
515 set in the same basic block as INSN, we don't worry about it,
516 because we'll probably need a register anyhow (??? but what if REG
517 is used in a different basic block as well as this one?). FIRST is
518 the first insn in the function. */
521 reg_is_remote_constant_p (reg
, insn
, first
)
528 if (REG_N_SETS (REGNO (reg
)) != 1)
531 /* Look for the set. */
532 for (p
= LOG_LINKS (insn
); p
; p
= XEXP (p
, 1))
536 if (REG_NOTE_KIND (p
) != 0)
538 s
= single_set (XEXP (p
, 0));
540 && GET_CODE (SET_DEST (s
)) == REG
541 && REGNO (SET_DEST (s
)) == REGNO (reg
))
543 /* The register is set in the same basic block. */
548 for (p
= first
; p
&& p
!= insn
; p
= NEXT_INSN (p
))
552 if (GET_RTX_CLASS (GET_CODE (p
)) != 'i')
556 && GET_CODE (SET_DEST (s
)) == REG
557 && REGNO (SET_DEST (s
)) == REGNO (reg
))
559 /* This is the instruction which sets REG. If there is a
560 REG_EQUAL note, then REG is equivalent to a constant. */
561 if (find_reg_note (p
, REG_EQUAL
, NULL_RTX
))
570 /* cse disrupts preincrement / postdecrement squences when it finds a
571 hard register as ultimate source, like the frame pointer. */
573 fixup_match_2 (insn
, dst
, src
, offset
, regmove_dump_file
)
574 rtx insn
, dst
, src
, offset
;
575 FILE *regmove_dump_file
;
577 rtx p
, dst_death
= 0;
578 int length
, num_calls
= 0;
580 /* If SRC dies in INSN, we'd have to move the death note. This is
581 considered to be very unlikely, so we just skip the optimization
583 if (find_regno_note (insn
, REG_DEAD
, REGNO (src
)))
586 /* Scan backward to find the first instruction that sets DST. */
588 for (length
= 0, p
= PREV_INSN (insn
); p
; p
= PREV_INSN (p
))
592 if (GET_CODE (p
) == CODE_LABEL
593 || GET_CODE (p
) == JUMP_INSN
594 || (GET_CODE (p
) == NOTE
595 && (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
596 || NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_END
)))
599 /* ??? We can't scan past the end of a basic block without updating
600 the register lifetime info (REG_DEAD/basic_block_live_at_start).
601 A CALL_INSN might be the last insn of a basic block, if it is inside
602 an EH region. There is no easy way to tell, so we just always break
603 when we see a CALL_INSN if flag_exceptions is nonzero. */
604 if (flag_exceptions
&& GET_CODE (p
) == CALL_INSN
)
607 if (GET_RTX_CLASS (GET_CODE (p
)) != 'i')
610 if (find_regno_note (p
, REG_DEAD
, REGNO (dst
)))
615 pset
= single_set (p
);
616 if (pset
&& SET_DEST (pset
) == dst
617 && GET_CODE (SET_SRC (pset
)) == PLUS
618 && XEXP (SET_SRC (pset
), 0) == src
619 && GET_CODE (XEXP (SET_SRC (pset
), 1)) == CONST_INT
)
621 HOST_WIDE_INT newconst
622 = INTVAL (offset
) - INTVAL (XEXP (SET_SRC (pset
), 1));
623 if (validate_change (insn
, &PATTERN (insn
),
624 gen_addsi3 (dst
, dst
, GEN_INT (newconst
)), 0))
626 /* Remove the death note for DST from DST_DEATH. */
629 remove_death (REGNO (dst
), dst_death
);
630 REG_LIVE_LENGTH (REGNO (dst
)) += length
;
631 REG_N_CALLS_CROSSED (REGNO (dst
)) += num_calls
;
634 REG_N_REFS (REGNO (dst
)) += loop_depth
;
635 REG_N_REFS (REGNO (src
)) -= loop_depth
;
637 if (regmove_dump_file
)
638 fprintf (regmove_dump_file
,
639 "Fixed operand of insn %d.\n",
643 for (p
= PREV_INSN (insn
); p
; p
= PREV_INSN (p
))
645 if (GET_CODE (p
) == CODE_LABEL
646 || GET_CODE (p
) == JUMP_INSN
647 || (GET_CODE (p
) == NOTE
648 && (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
649 || NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_END
)))
651 if (reg_overlap_mentioned_p (dst
, PATTERN (p
)))
653 if (try_auto_increment (p
, insn
, 0, dst
, newconst
, 0))
658 for (p
= NEXT_INSN (insn
); p
; p
= NEXT_INSN (p
))
660 if (GET_CODE (p
) == CODE_LABEL
661 || GET_CODE (p
) == JUMP_INSN
662 || (GET_CODE (p
) == NOTE
663 && (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
664 || NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_END
)))
666 if (reg_overlap_mentioned_p (dst
, PATTERN (p
)))
668 try_auto_increment (p
, insn
, 0, dst
, newconst
, 1);
677 if (reg_set_p (dst
, PATTERN (p
)))
680 /* If we have passed a call instruction, and the
681 pseudo-reg SRC is not already live across a call,
682 then don't perform the optimization. */
683 /* reg_set_p is overly conservative for CALL_INSNS, thinks that all
684 hard regs are clobbered. Thus, we only use it for src for
686 if (GET_CODE (p
) == CALL_INSN
)
691 if (REG_N_CALLS_CROSSED (REGNO (src
)) == 0)
694 if (call_used_regs
[REGNO (dst
)]
695 || find_reg_fusage (p
, CLOBBER
, dst
))
698 else if (reg_set_p (src
, PATTERN (p
)))
706 regmove_optimize (f
, nregs
, regmove_dump_file
)
709 FILE *regmove_dump_file
;
714 int maxregnum
= max_reg_num (), i
;
716 regno_src_regno
= (int *)alloca (sizeof *regno_src_regno
* maxregnum
);
717 for (i
= maxregnum
; --i
>= 0; ) regno_src_regno
[i
] = -1;
719 /* A forward/backward pass. Replace output operands with input operands. */
723 for (pass
= 0; pass
<= 2; pass
++)
725 if (! flag_regmove
&& pass
>= flag_expensive_optimizations
)
728 if (regmove_dump_file
)
729 fprintf (regmove_dump_file
, "Starting %s pass...\n",
730 pass
? "backward" : "forward");
732 for (insn
= pass
? get_last_insn () : f
; insn
;
733 insn
= pass
? PREV_INSN (insn
) : NEXT_INSN (insn
))
736 int insn_code_number
;
737 int operand_number
, match_number
;
739 if (GET_CODE (insn
) == NOTE
)
741 if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_BEG
)
743 else if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_END
)
747 set
= single_set (insn
);
751 if (flag_expensive_optimizations
&& ! pass
752 && (GET_CODE (SET_SRC (set
)) == SIGN_EXTEND
753 || GET_CODE (SET_SRC (set
)) == ZERO_EXTEND
)
754 && GET_CODE (XEXP (SET_SRC (set
), 0)) == REG
755 && GET_CODE (SET_DEST(set
)) == REG
)
756 optimize_reg_copy_3 (insn
, SET_DEST (set
), SET_SRC (set
));
758 if (flag_expensive_optimizations
&& ! pass
759 && GET_CODE (SET_SRC (set
)) == REG
760 && GET_CODE (SET_DEST(set
)) == REG
)
762 /* If this is a register-register copy where SRC is not dead,
763 see if we can optimize it. If this optimization succeeds,
764 it will become a copy where SRC is dead. */
765 if ((find_reg_note (insn
, REG_DEAD
, SET_SRC (set
))
766 || optimize_reg_copy_1 (insn
, SET_DEST (set
), SET_SRC (set
)))
767 && REGNO (SET_DEST (set
)) >= FIRST_PSEUDO_REGISTER
)
769 /* Similarly for a pseudo-pseudo copy when SRC is dead. */
770 if (REGNO (SET_SRC (set
)) >= FIRST_PSEUDO_REGISTER
)
771 optimize_reg_copy_2 (insn
, SET_DEST (set
), SET_SRC (set
));
772 if (regno_src_regno
[REGNO (SET_DEST (set
))] < 0
773 && SET_SRC (set
) != SET_DEST (set
))
775 int srcregno
= REGNO (SET_SRC(set
));
776 if (regno_src_regno
[srcregno
] >= 0)
777 srcregno
= regno_src_regno
[srcregno
];
778 regno_src_regno
[REGNO (SET_DEST (set
))] = srcregno
;
782 #ifdef REGISTER_CONSTRAINTS
784 = find_matches (insn
, &match
);
786 if (insn_code_number
< 0)
789 /* Now scan through the operands looking for a source operand
790 which is supposed to match the destination operand.
791 Then scan forward for an instruction which uses the dest
793 If it dies there, then replace the dest in both operands with
794 the source operand. */
796 for (operand_number
= 0;
797 operand_number
< insn_n_operands
[insn_code_number
];
800 rtx src
, dst
, src_subreg
;
801 enum reg_class src_class
, dst_class
;
803 match_number
= match
.with
[operand_number
];
805 /* Nothing to do if the two operands aren't supposed to match. */
806 if (match_number
< 0)
809 src
= recog_operand
[operand_number
];
810 dst
= recog_operand
[match_number
];
812 if (GET_CODE (src
) != REG
)
816 if (GET_CODE (dst
) == SUBREG
817 && GET_MODE_SIZE (GET_MODE (dst
))
818 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (dst
))))
821 = gen_rtx_SUBREG (GET_MODE (SUBREG_REG (dst
)),
822 src
, SUBREG_WORD (dst
));
823 dst
= SUBREG_REG (dst
);
825 if (GET_CODE (dst
) != REG
826 || REGNO (dst
) < FIRST_PSEUDO_REGISTER
)
829 if (REGNO (src
) < FIRST_PSEUDO_REGISTER
)
831 if (match
.commutative
[operand_number
] < operand_number
)
832 regno_src_regno
[REGNO (dst
)] = REGNO (src
);
836 if (REG_LIVE_LENGTH (REGNO (src
)) < 0)
839 /* operand_number/src must be a read-only operand, and
840 match_operand/dst must be a write-only operand. */
841 if (match
.use
[operand_number
] != READ
842 || match
.use
[match_number
] != WRITE
)
845 if (match
.early_clobber
[match_number
]
846 && count_occurrences (PATTERN (insn
), src
) > 1)
849 /* Make sure match_operand is the destination. */
850 if (recog_operand
[match_number
] != SET_DEST (set
))
853 /* If the operands already match, then there is nothing to do. */
854 /* But in the commutative case, we might find a better match. */
855 if (operands_match_p (src
, dst
)
856 || (match
.commutative
[operand_number
] >= 0
857 && operands_match_p (recog_operand
[match
.commutative
858 [operand_number
]], dst
)
859 && (replacement_quality (recog_operand
[match
.commutative
861 >= replacement_quality (src
))))
864 src_class
= reg_preferred_class (REGNO (src
));
865 dst_class
= reg_preferred_class (REGNO (dst
));
866 if (src_class
!= dst_class
867 && (! reg_class_subset_p (src_class
, dst_class
)
868 || CLASS_LIKELY_SPILLED_P (src_class
))
869 && (! reg_class_subset_p (dst_class
, src_class
)
870 || CLASS_LIKELY_SPILLED_P (dst_class
)))
873 if (fixup_match_1 (insn
, set
, src
, src_subreg
, dst
, pass
,
874 operand_number
, match_number
,
881 /* A backward pass. Replace input operands with output operands. */
883 if (regmove_dump_file
)
884 fprintf (regmove_dump_file
, "Starting backward pass...\n");
888 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
890 if (GET_CODE (insn
) == NOTE
)
892 if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_END
)
894 else if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_BEG
)
897 if (GET_RTX_CLASS (GET_CODE (insn
)) == 'i')
899 int insn_code_number
= find_matches (insn
, &match
);
900 int operand_number
, match_number
;
902 if (insn_code_number
< 0)
905 /* Now scan through the operands looking for a destination operand
906 which is supposed to match a source operand.
907 Then scan backward for an instruction which sets the source
908 operand. If safe, then replace the source operand with the
909 dest operand in both instructions. */
911 for (operand_number
= 0;
912 operand_number
< insn_n_operands
[insn_code_number
];
915 rtx set
, p
, src
, dst
;
916 rtx src_note
, dst_note
;
919 enum reg_class src_class
, dst_class
;
922 match_number
= match
.with
[operand_number
];
924 /* Nothing to do if the two operands aren't supposed to match. */
925 if (match_number
< 0)
928 dst
= recog_operand
[match_number
];
929 src
= recog_operand
[operand_number
];
931 if (GET_CODE (src
) != REG
)
934 if (GET_CODE (dst
) != REG
935 || REGNO (dst
) < FIRST_PSEUDO_REGISTER
936 || REG_LIVE_LENGTH (REGNO (dst
)) < 0)
939 /* If the operands already match, then there is nothing to do. */
940 if (operands_match_p (src
, dst
)
941 || (match
.commutative
[operand_number
] >= 0
942 && operands_match_p (recog_operand
[match
.commutative
[operand_number
]], dst
)))
945 set
= single_set (insn
);
949 /* match_number/dst must be a write-only operand, and
950 operand_operand/src must be a read-only operand. */
951 if (match
.use
[operand_number
] != READ
952 || match
.use
[match_number
] != WRITE
)
955 if (match
.early_clobber
[match_number
]
956 && count_occurrences (PATTERN (insn
), src
) > 1)
959 /* Make sure match_number is the destination. */
960 if (recog_operand
[match_number
] != SET_DEST (set
))
963 if (REGNO (src
) < FIRST_PSEUDO_REGISTER
)
965 if (GET_CODE (SET_SRC (set
)) == PLUS
966 && GET_CODE (XEXP (SET_SRC (set
), 1)) == CONST_INT
967 && XEXP (SET_SRC (set
), 0) == src
968 && fixup_match_2 (insn
, dst
, src
,
969 XEXP (SET_SRC (set
), 1),
974 src_class
= reg_preferred_class (REGNO (src
));
975 dst_class
= reg_preferred_class (REGNO (dst
));
976 if (src_class
!= dst_class
977 && (! reg_class_subset_p (src_class
, dst_class
)
978 || CLASS_LIKELY_SPILLED_P (src_class
))
979 && (! reg_class_subset_p (dst_class
, src_class
)
980 || CLASS_LIKELY_SPILLED_P (dst_class
)))
983 if (! (src_note
= find_reg_note (insn
, REG_DEAD
, src
)))
986 /* Can not modify an earlier insn to set dst if this insn
987 uses an old value in the source. */
988 if (reg_overlap_mentioned_p (dst
, SET_SRC (set
)))
991 if (regmove_dump_file
)
992 fprintf (regmove_dump_file
,
993 "Could fix operand %d of insn %d matching operand %d.\n",
994 operand_number
, INSN_UID (insn
), match_number
);
996 /* If src is set once in a different basic block,
997 and is set equal to a constant, then do not use
998 it for this optimization, as this would make it
999 no longer equivalent to a constant. */
1000 if (reg_is_remote_constant_p (src
, insn
, f
))
1003 /* Scan backward to find the first instruction that uses
1004 the input operand. If the operand is set here, then
1005 replace it in both instructions with match_number. */
1007 for (length
= 0, p
= PREV_INSN (insn
); p
; p
= PREV_INSN (p
))
1011 if (GET_CODE (p
) == CODE_LABEL
1012 || GET_CODE (p
) == JUMP_INSN
1013 || (GET_CODE (p
) == NOTE
1014 && (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
1015 || NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_END
)))
1018 /* ??? We can't scan past the end of a basic block without
1019 updating the register lifetime info
1020 (REG_DEAD/basic_block_live_at_start).
1021 A CALL_INSN might be the last insn of a basic block, if
1022 it is inside an EH region. There is no easy way to tell,
1023 so we just always break when we see a CALL_INSN if
1024 flag_exceptions is nonzero. */
1025 if (flag_exceptions
&& GET_CODE (p
) == CALL_INSN
)
1028 if (GET_RTX_CLASS (GET_CODE (p
)) != 'i')
1033 /* ??? See if all of SRC is set in P. This test is much
1034 more conservative than it needs to be. */
1035 pset
= single_set (p
);
1036 if (pset
&& SET_DEST (pset
) == src
)
1038 /* We use validate_replace_rtx, in case there
1039 are multiple identical source operands. All of
1040 them have to be changed at the same time. */
1041 if (validate_replace_rtx (src
, dst
, insn
))
1043 if (validate_change (p
, &SET_DEST (pset
),
1048 /* Change all source operands back.
1049 This modifies the dst as a side-effect. */
1050 validate_replace_rtx (dst
, src
, insn
);
1051 /* Now make sure the dst is right. */
1052 validate_change (insn
,
1053 recog_operand_loc
[match_number
],
1060 if (reg_overlap_mentioned_p (src
, PATTERN (p
))
1061 || reg_overlap_mentioned_p (dst
, PATTERN (p
)))
1064 /* If we have passed a call instruction, and the
1065 pseudo-reg DST is not already live across a call,
1066 then don't perform the optimization. */
1067 if (GET_CODE (p
) == CALL_INSN
)
1071 if (REG_N_CALLS_CROSSED (REGNO (dst
)) == 0)
1080 /* Remove the death note for SRC from INSN. */
1081 remove_note (insn
, src_note
);
1082 /* Move the death note for SRC to P if it is used
1084 if (reg_overlap_mentioned_p (src
, PATTERN (p
)))
1086 XEXP (src_note
, 1) = REG_NOTES (p
);
1087 REG_NOTES (p
) = src_note
;
1089 /* If there is a REG_DEAD note for DST on P, then remove
1090 it, because DST is now set there. */
1091 if ((dst_note
= find_reg_note (p
, REG_DEAD
, dst
)))
1092 remove_note (p
, dst_note
);
1094 dstno
= REGNO (dst
);
1095 srcno
= REGNO (src
);
1097 REG_N_SETS (dstno
)++;
1098 REG_N_SETS (srcno
)--;
1100 REG_N_CALLS_CROSSED (dstno
) += num_calls
;
1101 REG_N_CALLS_CROSSED (srcno
) -= num_calls
;
1103 REG_LIVE_LENGTH (dstno
) += length
;
1104 if (REG_LIVE_LENGTH (srcno
) >= 0)
1106 REG_LIVE_LENGTH (srcno
) -= length
;
1107 /* REG_LIVE_LENGTH is only an approximation after
1108 combine if sched is not run, so make sure that we
1109 still have a reasonable value. */
1110 if (REG_LIVE_LENGTH (srcno
) < 2)
1111 REG_LIVE_LENGTH (srcno
) = 2;
1114 /* We assume that a register is used exactly once per
1115 insn in the updates above. If this is not correct,
1116 no great harm is done. */
1118 REG_N_REFS (dstno
) += 2 * loop_depth
;
1119 REG_N_REFS (srcno
) -= 2 * loop_depth
;
1121 /* If that was the only time src was set,
1122 and src was not live at the start of the
1123 function, we know that we have no more
1124 references to src; clear REG_N_REFS so it
1125 won't make reload do any work. */
1126 if (REG_N_SETS (REGNO (src
)) == 0
1127 && ! regno_uninitialized (REGNO (src
)))
1128 REG_N_REFS (REGNO (src
)) = 0;
1130 if (regmove_dump_file
)
1131 fprintf (regmove_dump_file
,
1132 "Fixed operand %d of insn %d matching operand %d.\n",
1133 operand_number
, INSN_UID (insn
), match_number
);
1140 #endif /* REGISTER_CONSTRAINTS */
1145 find_matches (insn
, matchp
)
1147 struct match
*matchp
;
1149 int likely_spilled
[MAX_RECOG_OPERANDS
];
1151 int insn_code_number
= recog_memoized (insn
);
1152 int any_matches
= 0;
1154 if (insn_code_number
< 0)
1157 insn_extract (insn
);
1158 if (! constrain_operands (insn_code_number
, 0))
1161 /* Must initialize this before main loop, because the code for
1162 the commutative case may set matches for operands other than
1164 for (operand_number
= insn_n_operands
[insn_code_number
];
1165 --operand_number
>= 0; )
1166 matchp
->with
[operand_number
] = matchp
->commutative
[operand_number
] = -1;
1168 for (operand_number
= 0; operand_number
< insn_n_operands
[insn_code_number
];
1174 p
= insn_operand_constraint
[insn_code_number
][operand_number
];
1176 likely_spilled
[operand_number
] = 0;
1177 matchp
->use
[operand_number
] = READ
;
1178 matchp
->early_clobber
[operand_number
] = 0;
1180 matchp
->use
[operand_number
] = WRITE
;
1182 matchp
->use
[operand_number
] = READWRITE
;
1184 for (;*p
&& i
< which_alternative
; p
++)
1188 while ((c
= *p
++) != '\0' && c
!= ',')
1196 matchp
->early_clobber
[operand_number
] = 1;
1199 matchp
->commutative
[operand_number
] = operand_number
+ 1;
1200 matchp
->commutative
[operand_number
+ 1] = operand_number
;
1202 case '0': case '1': case '2': case '3': case '4':
1203 case '5': case '6': case '7': case '8': case '9':
1205 if (c
< operand_number
&& likely_spilled
[(unsigned char) c
])
1207 matchp
->with
[operand_number
] = c
;
1209 if (matchp
->commutative
[operand_number
] >= 0)
1210 matchp
->with
[matchp
->commutative
[operand_number
]] = c
;
1212 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'h':
1213 case 'j': case 'k': case 'l': case 'p': case 'q': case 't': case 'u':
1214 case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B':
1215 case 'C': case 'D': case 'W': case 'Y': case 'Z':
1216 if (CLASS_LIKELY_SPILLED_P (REG_CLASS_FROM_LETTER (c
)))
1217 likely_spilled
[operand_number
] = 1;
1221 return any_matches
? insn_code_number
: -1;
1224 /* Try to replace output operand DST in SET, with input operand SRC. SET is
1225 the only set in INSN. INSN has just been recgnized and constrained.
1226 SRC is operand number OPERAND_NUMBER in INSN.
1227 DST is operand number MATCH_NUMBER in INSN.
1228 If BACKWARD is nonzero, we have been called in a backward pass.
1229 Return nonzero for success. */
1231 fixup_match_1 (insn
, set
, src
, src_subreg
, dst
, backward
, operand_number
,
1232 match_number
, regmove_dump_file
)
1233 rtx insn
, set
, src
, src_subreg
, dst
;
1234 int backward
, operand_number
, match_number
;
1235 FILE *regmove_dump_file
;
1238 rtx post_inc
= 0, post_inc_set
= 0, search_end
= 0;
1240 int num_calls
= 0, s_num_calls
= 0;
1241 enum rtx_code code
= NOTE
;
1242 HOST_WIDE_INT insn_const
, newconst
;
1243 rtx overlap
= 0; /* need to move insn ? */
1244 rtx src_note
= find_reg_note (insn
, REG_DEAD
, src
), dst_note
;
1245 int length
, s_length
, true_loop_depth
;
1249 /* Look for (set (regX) (op regA constX))
1250 (set (regY) (op regA constY))
1252 (set (regA) (op regA constX)).
1253 (set (regY) (op regA constY-constX)).
1254 This works for add and shift operations, if
1255 regA is dead after or set by the second insn. */
1257 code
= GET_CODE (SET_SRC (set
));
1258 if ((code
== PLUS
|| code
== LSHIFTRT
1259 || code
== ASHIFT
|| code
== ASHIFTRT
)
1260 && XEXP (SET_SRC (set
), 0) == src
1261 && GET_CODE (XEXP (SET_SRC (set
), 1)) == CONST_INT
)
1262 insn_const
= INTVAL (XEXP (SET_SRC (set
), 1));
1263 else if (! stable_but_for_p (SET_SRC (set
), src
, dst
))
1266 /* We might find a src_note while scanning. */
1270 if (regmove_dump_file
)
1271 fprintf (regmove_dump_file
,
1272 "Could fix operand %d of insn %d matching operand %d.\n",
1273 operand_number
, INSN_UID (insn
), match_number
);
1275 /* If SRC is equivalent to a constant set in a different basic block,
1276 then do not use it for this optimization. We want the equivalence
1277 so that if we have to reload this register, we can reload the
1278 constant, rather than extending the lifespan of the register. */
1279 if (reg_is_remote_constant_p (src
, insn
, get_insns ()))
1282 /* Scan forward to find the next instruction that
1283 uses the output operand. If the operand dies here,
1284 then replace it in both instructions with
1287 for (length
= s_length
= 0, p
= NEXT_INSN (insn
); p
; p
= NEXT_INSN (p
))
1289 if (GET_CODE (p
) == CODE_LABEL
|| GET_CODE (p
) == JUMP_INSN
1290 || (GET_CODE (p
) == NOTE
1291 && (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
1292 || NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_END
)))
1295 /* ??? We can't scan past the end of a basic block without updating
1296 the register lifetime info (REG_DEAD/basic_block_live_at_start).
1297 A CALL_INSN might be the last insn of a basic block, if it is
1298 inside an EH region. There is no easy way to tell, so we just
1299 always break when we see a CALL_INSN if flag_exceptions is nonzero. */
1300 if (flag_exceptions
&& GET_CODE (p
) == CALL_INSN
)
1303 if (GET_RTX_CLASS (GET_CODE (p
)) != 'i')
1310 if (reg_set_p (src
, p
) || reg_set_p (dst
, p
)
1311 || (GET_CODE (PATTERN (p
)) == USE
1312 && reg_overlap_mentioned_p (src
, XEXP (PATTERN (p
), 0))))
1315 /* See if all of DST dies in P. This test is
1316 slightly more conservative than it needs to be. */
1317 if ((dst_note
= find_regno_note (p
, REG_DEAD
, REGNO (dst
)))
1318 && (GET_MODE (XEXP (dst_note
, 0)) == GET_MODE (dst
)))
1325 /* If an optimization is done, the value of SRC while P
1326 is executed will be changed. Check that this is OK. */
1327 if (reg_overlap_mentioned_p (src
, PATTERN (p
)))
1329 for (q
= p
; q
; q
= NEXT_INSN (q
))
1331 if (GET_CODE (q
) == CODE_LABEL
|| GET_CODE (q
) == JUMP_INSN
1332 || (GET_CODE (q
) == NOTE
1333 && (NOTE_LINE_NUMBER (q
) == NOTE_INSN_LOOP_BEG
1334 || NOTE_LINE_NUMBER (q
) == NOTE_INSN_LOOP_END
)))
1340 /* ??? We can't scan past the end of a basic block without
1341 updating the register lifetime info
1342 (REG_DEAD/basic_block_live_at_start).
1343 A CALL_INSN might be the last insn of a basic block, if
1344 it is inside an EH region. There is no easy way to tell,
1345 so we just always break when we see a CALL_INSN if
1346 flag_exceptions is nonzero. */
1347 if (flag_exceptions
&& GET_CODE (p
) == CALL_INSN
)
1353 if (GET_RTX_CLASS (GET_CODE (q
)) != 'i')
1355 if (reg_overlap_mentioned_p (src
, PATTERN (q
))
1356 || reg_set_p (src
, q
))
1360 set2
= single_set (q
);
1361 if (! q
|| ! set2
|| GET_CODE (SET_SRC (set2
)) != code
1362 || XEXP (SET_SRC (set2
), 0) != src
1363 || GET_CODE (XEXP (SET_SRC (set2
), 1)) != CONST_INT
1364 || (SET_DEST (set2
) != src
1365 && ! find_reg_note (q
, REG_DEAD
, src
)))
1367 /* If this is a PLUS, we can still save a register by doing
1370 src -= insn_const; .
1371 This also gives opportunities for subsequent
1372 optimizations in the backward pass, so do it there. */
1373 if (code
== PLUS
&& backward
1375 /* We may not emit an insn directly
1376 after P if the latter sets CC0. */
1377 && ! sets_cc0_p (PATTERN (p
))
1385 newconst
= -insn_const
;
1393 newconst
= INTVAL (XEXP (SET_SRC (set2
), 1)) - insn_const
;
1394 /* Reject out of range shifts. */
1398 >= GET_MODE_BITSIZE (GET_MODE (SET_SRC (set2
))))))
1403 if (SET_DEST (set2
) != src
)
1404 post_inc_set
= set2
;
1407 /* We use 1 as last argument to validate_change so that all
1408 changes are accepted or rejected together by apply_change_group
1409 when it is called by validate_replace_rtx . */
1410 validate_change (q
, &XEXP (SET_SRC (set2
), 1),
1411 GEN_INT (newconst
), 1);
1413 validate_change (insn
, recog_operand_loc
[match_number
], src
, 1);
1414 if (validate_replace_rtx (dst
, src_subreg
, p
))
1419 if (reg_overlap_mentioned_p (dst
, PATTERN (p
)))
1421 if (! src_note
&& reg_overlap_mentioned_p (src
, PATTERN (p
)))
1423 /* INSN was already checked to be movable when
1424 we found no REG_DEAD note for src on it. */
1426 src_note
= find_reg_note (p
, REG_DEAD
, src
);
1429 /* If we have passed a call instruction, and the pseudo-reg SRC is not
1430 already live across a call, then don't perform the optimization. */
1431 if (GET_CODE (p
) == CALL_INSN
)
1433 if (REG_N_CALLS_CROSSED (REGNO (src
)) == 0)
1447 true_loop_depth
= backward
? 2 - loop_depth
: loop_depth
;
1449 /* Remove the death note for DST from P. */
1450 remove_note (p
, dst_note
);
1453 post_inc
= emit_insn_after (copy_rtx (PATTERN (insn
)), p
);
1454 #if defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT)
1456 && try_auto_increment (search_end
, post_inc
, 0, src
, newconst
, 1))
1459 validate_change (insn
, &XEXP (SET_SRC (set
), 1), GEN_INT (insn_const
), 0);
1460 REG_N_SETS (REGNO (src
))++;
1461 REG_N_REFS (REGNO (src
)) += true_loop_depth
;
1462 REG_LIVE_LENGTH (REGNO (src
))++;
1466 /* The lifetime of src and dest overlap,
1467 but we can change this by moving insn. */
1468 rtx pat
= PATTERN (insn
);
1470 remove_note (overlap
, src_note
);
1471 #if defined (HAVE_POST_INCREMENT) || defined (HAVE_POST_DECREMENT)
1473 && try_auto_increment (overlap
, insn
, 0, src
, insn_const
, 0))
1478 rtx notes
= REG_NOTES (insn
);
1480 emit_insn_after_with_line_notes (pat
, PREV_INSN (p
), insn
);
1481 PUT_CODE (insn
, NOTE
);
1482 NOTE_LINE_NUMBER (insn
) = NOTE_INSN_DELETED
;
1483 NOTE_SOURCE_FILE (insn
) = 0;
1484 /* emit_insn_after_with_line_notes has no
1485 return value, so search for the new insn. */
1486 for (insn
= p
; PATTERN (insn
) != pat
; )
1487 insn
= PREV_INSN (insn
);
1489 REG_NOTES (insn
) = notes
;
1492 /* Sometimes we'd generate src = const; src += n;
1493 if so, replace the instruction that set src
1494 in the first place. */
1496 if (! overlap
&& (code
== PLUS
|| code
== MINUS
))
1498 rtx note
= find_reg_note (insn
, REG_EQUAL
, NULL_RTX
);
1500 int num_calls2
= 0, s_length2
= 0;
1502 if (note
&& CONSTANT_P (XEXP (note
, 0)))
1504 for (q
= PREV_INSN (insn
); q
; q
= PREV_INSN(q
))
1506 if (GET_CODE (q
) == CODE_LABEL
|| GET_CODE (q
) == JUMP_INSN
1507 || (GET_CODE (q
) == NOTE
1508 && (NOTE_LINE_NUMBER (q
) == NOTE_INSN_LOOP_BEG
1509 || NOTE_LINE_NUMBER (q
) == NOTE_INSN_LOOP_END
)))
1515 /* ??? We can't scan past the end of a basic block without
1516 updating the register lifetime info
1517 (REG_DEAD/basic_block_live_at_start).
1518 A CALL_INSN might be the last insn of a basic block, if
1519 it is inside an EH region. There is no easy way to tell,
1520 so we just always break when we see a CALL_INSN if
1521 flag_exceptions is nonzero. */
1522 if (flag_exceptions
&& GET_CODE (p
) == CALL_INSN
)
1528 if (GET_RTX_CLASS (GET_CODE (q
)) != 'i')
1531 if (reg_set_p (src
, q
))
1533 set2
= single_set (q
);
1536 if (reg_overlap_mentioned_p (src
, PATTERN (q
)))
1541 if (GET_CODE (p
) == CALL_INSN
)
1544 if (q
&& set2
&& SET_DEST (set2
) == src
&& CONSTANT_P (SET_SRC (set2
))
1545 && validate_change (insn
, &SET_SRC (set
), XEXP (note
, 0), 0))
1548 NOTE_LINE_NUMBER (q
) = NOTE_INSN_DELETED
;
1549 NOTE_SOURCE_FILE (q
) = 0;
1550 REG_N_SETS (REGNO (src
))--;
1551 REG_N_CALLS_CROSSED (REGNO (src
)) -= num_calls2
;
1552 REG_N_REFS (REGNO (src
)) -= true_loop_depth
;
1553 REG_LIVE_LENGTH (REGNO (src
)) -= s_length2
;
1559 /* Don't remove this seemingly useless if, it is needed to pair with the
1560 else in the next two conditionally included code blocks. */
1563 #if defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT)
1564 else if ((code
== PLUS
|| code
== MINUS
) && insn_const
1565 && try_auto_increment (p
, insn
, 0, src
, insn_const
, 1))
1568 #if defined (HAVE_POST_INCREMENT) || defined (HAVE_POST_DECREMENT)
1570 && try_auto_increment (p
, post_inc
, post_inc_set
, src
, newconst
, 0))
1573 #if defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT)
1574 /* If post_inc still prevails, try to find an
1575 insn where it can be used as a pre-in/decrement.
1576 If code is MINUS, this was already tried. */
1577 if (post_inc
&& code
== PLUS
1578 /* Check that newconst is likely to be usable
1579 in a pre-in/decrement before starting the search. */
1581 #if defined (HAVE_PRE_INCREMENT)
1582 || (newconst
> 0 && newconst
<= MOVE_MAX
)
1584 #if defined (HAVE_PRE_DECREMENT)
1585 || (newconst
< 0 && newconst
>= -MOVE_MAX
)
1587 ) && exact_log2 (newconst
))
1591 inc_dest
= post_inc_set
? SET_DEST (post_inc_set
) : src
;
1592 for (q
= post_inc
; (q
= NEXT_INSN (q
)); )
1594 if (GET_CODE (q
) == CODE_LABEL
|| GET_CODE (q
) == JUMP_INSN
1595 || (GET_CODE (q
) == NOTE
1596 && (NOTE_LINE_NUMBER (q
) == NOTE_INSN_LOOP_BEG
1597 || NOTE_LINE_NUMBER (q
) == NOTE_INSN_LOOP_END
)))
1600 /* ??? We can't scan past the end of a basic block without updating
1601 the register lifetime info (REG_DEAD/basic_block_live_at_start).
1602 A CALL_INSN might be the last insn of a basic block, if it
1603 is inside an EH region. There is no easy way to tell so we
1604 just always break when we see a CALL_INSN if flag_exceptions
1606 if (flag_exceptions
&& GET_CODE (p
) == CALL_INSN
)
1609 if (GET_RTX_CLASS (GET_CODE (q
)) != 'i')
1611 if (src
!= inc_dest
&& (reg_overlap_mentioned_p (src
, PATTERN (q
))
1612 || reg_set_p (src
, q
)))
1614 if (reg_set_p (inc_dest
, q
))
1616 if (reg_overlap_mentioned_p (inc_dest
, PATTERN (q
)))
1618 try_auto_increment (q
, post_inc
,
1619 post_inc_set
, inc_dest
, newconst
, 1);
1624 #endif /* defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT) */
1625 /* Move the death note for DST to INSN if it is used
1627 if (reg_overlap_mentioned_p (dst
, PATTERN (insn
)))
1629 XEXP (dst_note
, 1) = REG_NOTES (insn
);
1630 REG_NOTES (insn
) = dst_note
;
1635 /* Move the death note for SRC from INSN to P. */
1637 remove_note (insn
, src_note
);
1638 XEXP (src_note
, 1) = REG_NOTES (p
);
1639 REG_NOTES (p
) = src_note
;
1641 REG_N_CALLS_CROSSED (REGNO (src
)) += s_num_calls
;
1644 REG_N_SETS (REGNO (src
))++;
1645 REG_N_SETS (REGNO (dst
))--;
1647 REG_N_CALLS_CROSSED (REGNO (dst
)) -= num_calls
;
1649 REG_LIVE_LENGTH (REGNO (src
)) += s_length
;
1650 if (REG_LIVE_LENGTH (REGNO (dst
)) >= 0)
1652 REG_LIVE_LENGTH (REGNO (dst
)) -= length
;
1653 /* REG_LIVE_LENGTH is only an approximation after
1654 combine if sched is not run, so make sure that we
1655 still have a reasonable value. */
1656 if (REG_LIVE_LENGTH (REGNO (dst
)) < 2)
1657 REG_LIVE_LENGTH (REGNO (dst
)) = 2;
1660 /* We assume that a register is used exactly once per
1661 insn in the updates above. If this is not correct,
1662 no great harm is done. */
1664 REG_N_REFS (REGNO (src
)) += 2 * true_loop_depth
;
1665 REG_N_REFS (REGNO (dst
)) -= 2 * true_loop_depth
;
1667 /* If that was the only time dst was set,
1668 and dst was not live at the start of the
1669 function, we know that we have no more
1670 references to dst; clear REG_N_REFS so it
1671 won't make reload do any work. */
1672 if (REG_N_SETS (REGNO (dst
)) == 0
1673 && ! regno_uninitialized (REGNO (dst
)))
1674 REG_N_REFS (REGNO (dst
)) = 0;
1676 if (regmove_dump_file
)
1677 fprintf (regmove_dump_file
,
1678 "Fixed operand %d of insn %d matching operand %d.\n",
1679 operand_number
, INSN_UID (insn
), match_number
);
1684 /* return nonzero if X is stable but for mentioning SRC or mentioning /
1685 changing DST . If in doubt, presume it is unstable. */
1687 stable_but_for_p (x
, src
, dst
)
1690 RTX_CODE code
= GET_CODE (x
);
1691 switch (GET_RTX_CLASS (code
))
1693 case '<': case '1': case 'c': case '2': case 'b': case '3':
1696 char *fmt
= GET_RTX_FORMAT (code
);
1697 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1698 if (fmt
[i
] == 'e' && ! stable_but_for_p (XEXP (x
, i
), src
, dst
))
1703 if (x
== src
|| x
== dst
)
1707 return ! rtx_unstable_p (x
);
1711 /* Test if regmove seems profitable for this target. */
1713 regmove_profitable_p ()
1715 #ifdef REGISTER_CONSTRAINTS
1717 enum machine_mode mode
;
1718 optab tstoptab
= add_optab
;
1719 do /* check add_optab and ashl_optab */
1720 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
); mode
!= VOIDmode
;
1721 mode
= GET_MODE_WIDER_MODE (mode
))
1723 int icode
= (int) tstoptab
->handlers
[(int) mode
].insn_code
;
1724 rtx reg0
, reg1
, reg2
, pat
;
1727 if (GET_MODE_BITSIZE (mode
) < 32 || icode
== CODE_FOR_nothing
)
1729 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
1730 if (TEST_HARD_REG_BIT (reg_class_contents
[GENERAL_REGS
], i
))
1732 if (i
+ 2 >= FIRST_PSEUDO_REGISTER
)
1734 reg0
= gen_rtx_REG (insn_operand_mode
[icode
][0], i
);
1735 reg1
= gen_rtx_REG (insn_operand_mode
[icode
][1], i
+ 1);
1736 reg2
= gen_rtx_REG (insn_operand_mode
[icode
][2], i
+ 2);
1737 if (! (*insn_operand_predicate
[icode
][0]) (reg0
, VOIDmode
)
1738 || ! (*insn_operand_predicate
[icode
][1]) (reg1
, VOIDmode
)
1739 || ! (*insn_operand_predicate
[icode
][2]) (reg2
, VOIDmode
))
1741 pat
= GEN_FCN (icode
) (reg0
, reg1
, reg2
);
1744 if (GET_CODE (pat
) == SEQUENCE
)
1745 pat
= XVECEXP (pat
, 0, XVECLEN (pat
, 0) - 1);
1747 pat
= make_insn_raw (pat
);
1748 if (! single_set (pat
)
1749 || GET_CODE (SET_SRC (single_set (pat
))) != tstoptab
->code
)
1750 /* Unexpected complexity; don't need to handle this unless
1751 we find a machine where this occurs and regmove should
1754 if (find_matches (pat
, &match
) >= 0)
1758 while (tstoptab
!= ashl_optab
&& (tstoptab
= ashl_optab
, 1));
1759 #endif /* REGISTER_CONSTRAINTS */