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"
46 #include "basic-block.h"
48 static int optimize_reg_copy_1 PROTO((rtx, rtx, rtx));
49 static void optimize_reg_copy_2 PROTO((rtx, rtx, rtx));
50 static void optimize_reg_copy_3 PROTO((rtx, rtx, rtx));
51 static rtx gen_add3_insn PROTO((rtx, rtx, rtx));
52 static void copy_src_to_dest PROTO((rtx, rtx, rtx, int));
53 static int *regmove_bb_head;
56 int with[MAX_RECOG_OPERANDS];
57 enum { READ, WRITE, READWRITE } use[MAX_RECOG_OPERANDS];
58 int commutative[MAX_RECOG_OPERANDS];
59 int early_clobber[MAX_RECOG_OPERANDS];
63 static int try_auto_increment PROTO((rtx, rtx, rtx, rtx, HOST_WIDE_INT, int));
65 static int find_matches PROTO((rtx, struct match *));
66 static int fixup_match_1 PROTO((rtx, rtx, rtx, rtx, rtx, int, int, int, FILE *))
68 static int reg_is_remote_constant_p PROTO((rtx, rtx, rtx));
69 static int stable_but_for_p PROTO((rtx, rtx, rtx));
70 static int loop_depth;
72 /* Generate and return an insn body to add r1 and c,
73 storing the result in r0. */
75 gen_add3_insn (r0, r1, c)
78 int icode = (int) add_optab->handlers[(int) GET_MODE (r0)].insn_code;
80 if (icode == CODE_FOR_nothing
81 || ! (*insn_operand_predicate[icode][0]) (r0, insn_operand_mode[icode][0])
82 || ! (*insn_operand_predicate[icode][1]) (r1, insn_operand_mode[icode][1])
83 || ! (*insn_operand_predicate[icode][2]) (c, insn_operand_mode[icode][2]))
86 return (GEN_FCN (icode) (r0, r1, c));
91 /* INC_INSN is an instruction that adds INCREMENT to REG.
92 Try to fold INC_INSN as a post/pre in/decrement into INSN.
93 Iff INC_INSN_SET is nonzero, inc_insn has a destination different from src.
94 Return nonzero for success. */
96 try_auto_increment (insn, inc_insn, inc_insn_set, reg, increment, pre)
97 rtx reg, insn, inc_insn ,inc_insn_set;
98 HOST_WIDE_INT increment;
101 enum rtx_code inc_code;
103 rtx pset = single_set (insn);
106 /* Can't use the size of SET_SRC, we might have something like
107 (sign_extend:SI (mem:QI ... */
108 rtx use = find_use_as_address (pset, reg, 0);
109 if (use != 0 && use != (rtx) 1)
111 int size = GET_MODE_SIZE (GET_MODE (use));
113 #ifdef HAVE_POST_INCREMENT
114 || (pre == 0 && (inc_code = POST_INC, increment == size))
116 #ifdef HAVE_PRE_INCREMENT
117 || (pre == 1 && (inc_code = PRE_INC, increment == size))
119 #ifdef HAVE_POST_DECREMENT
120 || (pre == 0 && (inc_code = POST_DEC, increment == -size))
122 #ifdef HAVE_PRE_DECREMENT
123 || (pre == 1 && (inc_code = PRE_DEC, increment == -size))
130 &SET_SRC (inc_insn_set),
131 XEXP (SET_SRC (inc_insn_set), 0), 1);
132 validate_change (insn, &XEXP (use, 0),
133 gen_rtx_fmt_e (inc_code, Pmode, reg), 1);
134 if (apply_change_group ())
137 = gen_rtx_EXPR_LIST (REG_INC,
138 reg, REG_NOTES (insn));
141 PUT_CODE (inc_insn, NOTE);
142 NOTE_LINE_NUMBER (inc_insn) = NOTE_INSN_DELETED;
143 NOTE_SOURCE_FILE (inc_insn) = 0;
152 #endif /* AUTO_INC_DEC */
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 updates below. If this is not correct,
291 no great harm is done. */
292 if (sregno >= FIRST_PSEUDO_REGISTER)
293 REG_N_REFS (sregno) -= loop_depth;
294 if (dregno >= FIRST_PSEUDO_REGISTER)
295 REG_N_REFS (dregno) += loop_depth;
299 validate_replace_rtx (dest, src, q);
304 /* Count the insns and CALL_INSNs passed. If we passed the
305 death note of DEST, show increased live length. */
310 /* If the insn in which SRC dies is a CALL_INSN, don't count it
311 as a call that has been crossed. Otherwise, count it. */
312 if (q != p && GET_CODE (q) == CALL_INSN)
319 /* If DEST dies here, remove the death note and save it for
320 later. Make sure ALL of DEST dies here; again, this is
321 overly conservative. */
323 && (dest_death = find_regno_note (q, REG_DEAD, dregno)) != 0)
325 if (GET_MODE (XEXP (dest_death, 0)) != GET_MODE (dest))
326 failed = 1, dest_death = 0;
328 remove_note (q, dest_death);
334 if (sregno >= FIRST_PSEUDO_REGISTER)
336 if (REG_LIVE_LENGTH (sregno) >= 0)
338 REG_LIVE_LENGTH (sregno) -= length;
339 /* REG_LIVE_LENGTH is only an approximation after
340 combine if sched is not run, so make sure that we
341 still have a reasonable value. */
342 if (REG_LIVE_LENGTH (sregno) < 2)
343 REG_LIVE_LENGTH (sregno) = 2;
346 REG_N_CALLS_CROSSED (sregno) -= n_calls;
349 if (dregno >= FIRST_PSEUDO_REGISTER)
351 if (REG_LIVE_LENGTH (dregno) >= 0)
352 REG_LIVE_LENGTH (dregno) += d_length;
354 REG_N_CALLS_CROSSED (dregno) += d_n_calls;
357 /* Move death note of SRC from P to INSN. */
358 remove_note (p, note);
359 XEXP (note, 1) = REG_NOTES (insn);
360 REG_NOTES (insn) = note;
363 /* Put death note of DEST on P if we saw it die. */
366 XEXP (dest_death, 1) = REG_NOTES (p);
367 REG_NOTES (p) = dest_death;
373 /* If SRC is a hard register which is set or killed in some other
374 way, we can't do this optimization. */
375 else if (sregno < FIRST_PSEUDO_REGISTER
376 && dead_or_set_p (p, src))
382 /* INSN is a copy of SRC to DEST, in which SRC dies. See if we now have
383 a sequence of insns that modify DEST followed by an insn that sets
384 SRC to DEST in which DEST dies, with no prior modification of DEST.
385 (There is no need to check if the insns in between actually modify
386 DEST. We should not have cases where DEST is not modified, but
387 the optimization is safe if no such modification is detected.)
388 In that case, we can replace all uses of DEST, starting with INSN and
389 ending with the set of SRC to DEST, with SRC. We do not do this
390 optimization if a CALL_INSN is crossed unless SRC already crosses a
391 call or if DEST dies before the copy back to SRC.
393 It is assumed that DEST and SRC are pseudos; it is too complicated to do
394 this for hard registers since the substitutions we may make might fail. */
397 optimize_reg_copy_2 (insn, dest, src)
404 int sregno = REGNO (src);
405 int dregno = REGNO (dest);
407 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
409 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
410 || (GET_CODE (p) == NOTE
411 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
412 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
415 /* ??? We can't scan past the end of a basic block without updating
416 the register lifetime info (REG_DEAD/basic_block_live_at_start).
417 A CALL_INSN might be the last insn of a basic block, if it is inside
418 an EH region. There is no easy way to tell, so we just always break
419 when we see a CALL_INSN if flag_exceptions is nonzero. */
420 if (flag_exceptions && GET_CODE (p) == CALL_INSN)
423 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
426 set = single_set (p);
427 if (set && SET_SRC (set) == dest && SET_DEST (set) == src
428 && find_reg_note (p, REG_DEAD, dest))
430 /* We can do the optimization. Scan forward from INSN again,
431 replacing regs as we go. */
433 /* Set to stop at next insn. */
434 for (q = insn; q != NEXT_INSN (p); q = NEXT_INSN (q))
435 if (GET_RTX_CLASS (GET_CODE (q)) == 'i')
437 if (reg_mentioned_p (dest, PATTERN (q)))
439 PATTERN (q) = replace_rtx (PATTERN (q), dest, src);
441 /* We assume that a register is used exactly once per
442 insn in the updates below. If this is not correct,
443 no great harm is done. */
444 REG_N_REFS (dregno) -= loop_depth;
445 REG_N_REFS (sregno) += loop_depth;
449 if (GET_CODE (q) == CALL_INSN)
451 REG_N_CALLS_CROSSED (dregno)--;
452 REG_N_CALLS_CROSSED (sregno)++;
456 remove_note (p, find_reg_note (p, REG_DEAD, dest));
457 REG_N_DEATHS (dregno)--;
458 remove_note (insn, find_reg_note (insn, REG_DEAD, src));
459 REG_N_DEATHS (sregno)--;
463 if (reg_set_p (src, p)
464 || find_reg_note (p, REG_DEAD, dest)
465 || (GET_CODE (p) == CALL_INSN && REG_N_CALLS_CROSSED (sregno) == 0))
469 /* INSN is a ZERO_EXTEND or SIGN_EXTEND of SRC to DEST.
470 Look if SRC dies there, and if it is only set once, by loading
471 it from memory. If so, try to encorporate the zero/sign extension
472 into the memory read, change SRC to the mode of DEST, and alter
473 the remaining accesses to use the appropriate SUBREG. This allows
474 SRC and DEST to be tied later. */
476 optimize_reg_copy_3 (insn, dest, src)
481 rtx src_reg = XEXP (src, 0);
482 int src_no = REGNO (src_reg);
483 int dst_no = REGNO (dest);
485 enum machine_mode old_mode;
487 if (src_no < FIRST_PSEUDO_REGISTER
488 || dst_no < FIRST_PSEUDO_REGISTER
489 || ! find_reg_note (insn, REG_DEAD, src_reg)
490 || REG_N_SETS (src_no) != 1)
492 for (p = PREV_INSN (insn); ! reg_set_p (src_reg, p); p = PREV_INSN (p))
494 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
495 || (GET_CODE (p) == NOTE
496 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
497 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
500 /* ??? We can't scan past the end of a basic block without updating
501 the register lifetime info (REG_DEAD/basic_block_live_at_start).
502 A CALL_INSN might be the last insn of a basic block, if it is inside
503 an EH region. There is no easy way to tell, so we just always break
504 when we see a CALL_INSN if flag_exceptions is nonzero. */
505 if (flag_exceptions && GET_CODE (p) == CALL_INSN)
508 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
511 if (! (set = single_set (p))
512 || GET_CODE (SET_SRC (set)) != MEM
513 || SET_DEST (set) != src_reg)
515 old_mode = GET_MODE (src_reg);
516 PUT_MODE (src_reg, GET_MODE (src));
517 XEXP (src, 0) = SET_SRC (set);
518 if (! validate_change (p, &SET_SRC (set), src, 0))
520 PUT_MODE (src_reg, old_mode);
521 XEXP (src, 0) = src_reg;
524 subreg = gen_rtx_SUBREG (old_mode, src_reg, 0);
525 while (p = NEXT_INSN (p), p != insn)
527 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
529 validate_replace_rtx (src_reg, subreg, p);
531 validate_replace_rtx (src, src_reg, insn);
535 /* If we were not able to update the users of src to use dest directly, try
536 instead moving the value to dest directly before the operation. */
539 copy_src_to_dest (insn, src, dest, loop_depth)
558 /* A REG_LIVE_LENGTH of -1 indicates the register is equivalent to a constant
559 or memory location and is used infrequently; a REG_LIVE_LENGTH of -2 is
560 parameter when there is no frame pointer that is not allocated a register.
561 For now, we just reject them, rather than incrementing the live length. */
563 if (GET_CODE (src) == REG
564 && REG_LIVE_LENGTH (REGNO (src)) > 0
565 && GET_CODE (dest) == REG
566 && REG_LIVE_LENGTH (REGNO (dest)) > 0
567 && (set = single_set (insn)) != NULL_RTX
568 && !reg_mentioned_p (dest, SET_SRC (set))
569 && validate_replace_rtx (src, dest, insn))
571 /* Generate the src->dest move. */
573 emit_move_insn (dest, src);
574 seq = gen_sequence ();
576 emit_insn_before (seq, insn);
577 move_insn = PREV_INSN (insn);
578 p_move_notes = ®_NOTES (move_insn);
579 p_insn_notes = ®_NOTES (insn);
581 /* Move any notes mentioning src to the move instruction */
582 for (link = REG_NOTES (insn); link != NULL_RTX; link = next)
584 next = XEXP (link, 1);
585 if (XEXP (link, 0) == src)
587 *p_move_notes = link;
588 p_move_notes = &XEXP (link, 1);
592 *p_insn_notes = link;
593 p_insn_notes = &XEXP (link, 1);
597 *p_move_notes = NULL_RTX;
598 *p_insn_notes = NULL_RTX;
600 /* Is the insn the head of a basic block? If so extend it */
601 insn_uid = INSN_UID (insn);
602 move_uid = INSN_UID (move_insn);
603 bb = regmove_bb_head[insn_uid];
606 basic_block_head[bb] = move_insn;
607 regmove_bb_head[insn_uid] = -1;
610 /* Update the various register tables. */
611 dest_regno = REGNO (dest);
612 REG_N_SETS (dest_regno) += loop_depth;
613 REG_N_REFS (dest_regno) += loop_depth;
614 REG_LIVE_LENGTH (dest_regno)++;
615 if (REGNO_FIRST_UID (dest_regno) == insn_uid)
616 REGNO_FIRST_UID (dest_regno) = move_uid;
618 src_regno = REGNO (src);
619 if (! find_reg_note (move_insn, REG_DEAD, src))
620 REG_LIVE_LENGTH (src_regno)++;
622 if (REGNO_FIRST_UID (src_regno) == insn_uid)
623 REGNO_FIRST_UID (src_regno) = move_uid;
625 if (REGNO_LAST_UID (src_regno) == insn_uid)
626 REGNO_LAST_UID (src_regno) = move_uid;
628 if (REGNO_LAST_NOTE_UID (src_regno) == insn_uid)
629 REGNO_LAST_NOTE_UID (src_regno) = move_uid;
634 /* Return whether REG is set in only one location, and is set to a
635 constant, but is set in a different basic block from INSN (an
636 instructions which uses REG). In this case REG is equivalent to a
637 constant, and we don't want to break that equivalence, because that
638 may increase register pressure and make reload harder. If REG is
639 set in the same basic block as INSN, we don't worry about it,
640 because we'll probably need a register anyhow (??? but what if REG
641 is used in a different basic block as well as this one?). FIRST is
642 the first insn in the function. */
645 reg_is_remote_constant_p (reg, insn, first)
652 if (REG_N_SETS (REGNO (reg)) != 1)
655 /* Look for the set. */
656 for (p = LOG_LINKS (insn); p; p = XEXP (p, 1))
660 if (REG_NOTE_KIND (p) != 0)
662 s = single_set (XEXP (p, 0));
664 && GET_CODE (SET_DEST (s)) == REG
665 && REGNO (SET_DEST (s)) == REGNO (reg))
667 /* The register is set in the same basic block. */
672 for (p = first; p && p != insn; p = NEXT_INSN (p))
676 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
680 && GET_CODE (SET_DEST (s)) == REG
681 && REGNO (SET_DEST (s)) == REGNO (reg))
683 /* This is the instruction which sets REG. If there is a
684 REG_EQUAL note, then REG is equivalent to a constant. */
685 if (find_reg_note (p, REG_EQUAL, NULL_RTX))
694 /* INSN is adding a CONST_INT to a REG. We search backwards looking for
695 another add immediate instruction with the same source and dest registers,
696 and if we find one, we change INSN to an increment, and return 1. If
697 no changes are made, we return 0.
700 (set (reg100) (plus reg1 offset1))
702 (set (reg100) (plus reg1 offset2))
704 (set (reg100) (plus reg1 offset1))
706 (set (reg100) (plus reg100 offset2-offset1)) */
708 /* ??? What does this comment mean? */
709 /* cse disrupts preincrement / postdecrement squences when it finds a
710 hard register as ultimate source, like the frame pointer. */
713 fixup_match_2 (insn, dst, src, offset, regmove_dump_file)
714 rtx insn, dst, src, offset;
715 FILE *regmove_dump_file;
717 rtx p, dst_death = 0;
718 int length, num_calls = 0;
720 /* If SRC dies in INSN, we'd have to move the death note. This is
721 considered to be very unlikely, so we just skip the optimization
723 if (find_regno_note (insn, REG_DEAD, REGNO (src)))
726 /* Scan backward to find the first instruction that sets DST. */
728 for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
732 if (GET_CODE (p) == CODE_LABEL
733 || GET_CODE (p) == JUMP_INSN
734 || (GET_CODE (p) == NOTE
735 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
736 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
739 /* ??? We can't scan past the end of a basic block without updating
740 the register lifetime info (REG_DEAD/basic_block_live_at_start).
741 A CALL_INSN might be the last insn of a basic block, if it is inside
742 an EH region. There is no easy way to tell, so we just always break
743 when we see a CALL_INSN if flag_exceptions is nonzero. */
744 if (flag_exceptions && GET_CODE (p) == CALL_INSN)
747 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
750 if (find_regno_note (p, REG_DEAD, REGNO (dst)))
755 pset = single_set (p);
756 if (pset && SET_DEST (pset) == dst
757 && GET_CODE (SET_SRC (pset)) == PLUS
758 && XEXP (SET_SRC (pset), 0) == src
759 && GET_CODE (XEXP (SET_SRC (pset), 1)) == CONST_INT)
761 HOST_WIDE_INT newconst
762 = INTVAL (offset) - INTVAL (XEXP (SET_SRC (pset), 1));
763 rtx add = gen_add3_insn (dst, dst, GEN_INT (newconst));
765 if (add && validate_change (insn, &PATTERN (insn), add, 0))
767 /* Remove the death note for DST from DST_DEATH. */
770 remove_death (REGNO (dst), dst_death);
771 REG_LIVE_LENGTH (REGNO (dst)) += length;
772 REG_N_CALLS_CROSSED (REGNO (dst)) += num_calls;
775 REG_N_REFS (REGNO (dst)) += loop_depth;
776 REG_N_REFS (REGNO (src)) -= loop_depth;
778 if (regmove_dump_file)
779 fprintf (regmove_dump_file,
780 "Fixed operand of insn %d.\n",
784 for (p = PREV_INSN (insn); p; p = PREV_INSN (p))
786 if (GET_CODE (p) == CODE_LABEL
787 || GET_CODE (p) == JUMP_INSN
788 || (GET_CODE (p) == NOTE
789 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
790 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
792 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
794 if (try_auto_increment (p, insn, 0, dst, newconst, 0))
799 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
801 if (GET_CODE (p) == CODE_LABEL
802 || GET_CODE (p) == JUMP_INSN
803 || (GET_CODE (p) == NOTE
804 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
805 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
807 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
809 try_auto_increment (p, insn, 0, dst, newconst, 1);
818 if (reg_set_p (dst, PATTERN (p)))
821 /* If we have passed a call instruction, and the
822 pseudo-reg SRC is not already live across a call,
823 then don't perform the optimization. */
824 /* reg_set_p is overly conservative for CALL_INSNS, thinks that all
825 hard regs are clobbered. Thus, we only use it for src for
827 if (GET_CODE (p) == CALL_INSN)
832 if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
835 if (call_used_regs [REGNO (dst)]
836 || find_reg_fusage (p, CLOBBER, dst))
839 else if (reg_set_p (src, PATTERN (p)))
847 regmove_optimize (f, nregs, regmove_dump_file)
850 FILE *regmove_dump_file;
855 int maxregnum = max_reg_num (), i;
856 rtx copy_src, copy_dst;
858 regno_src_regno = (int *)alloca (sizeof *regno_src_regno * maxregnum);
859 for (i = maxregnum; --i >= 0; ) regno_src_regno[i] = -1;
861 regmove_bb_head = (int *)alloca (sizeof (int) * (get_max_uid () + 1));
862 for (i = get_max_uid (); --i >= 0; ) regmove_bb_head[i] = -1;
863 for (i = 0; i < n_basic_blocks; i++)
864 regmove_bb_head[INSN_UID (basic_block_head[i])] = i;
866 /* A forward/backward pass. Replace output operands with input operands. */
870 for (pass = 0; pass <= 2; pass++)
872 if (! flag_regmove && pass >= flag_expensive_optimizations)
875 if (regmove_dump_file)
876 fprintf (regmove_dump_file, "Starting %s pass...\n",
877 pass ? "backward" : "forward");
879 for (insn = pass ? get_last_insn () : f; insn;
880 insn = pass ? PREV_INSN (insn) : NEXT_INSN (insn))
883 int insn_code_number;
884 int operand_number, match_number;
886 if (GET_CODE (insn) == NOTE)
888 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
890 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
894 set = single_set (insn);
898 if (flag_expensive_optimizations && ! pass
899 && (GET_CODE (SET_SRC (set)) == SIGN_EXTEND
900 || GET_CODE (SET_SRC (set)) == ZERO_EXTEND)
901 && GET_CODE (XEXP (SET_SRC (set), 0)) == REG
902 && GET_CODE (SET_DEST(set)) == REG)
903 optimize_reg_copy_3 (insn, SET_DEST (set), SET_SRC (set));
905 if (flag_expensive_optimizations && ! pass
906 && GET_CODE (SET_SRC (set)) == REG
907 && GET_CODE (SET_DEST(set)) == REG)
909 /* If this is a register-register copy where SRC is not dead,
910 see if we can optimize it. If this optimization succeeds,
911 it will become a copy where SRC is dead. */
912 if ((find_reg_note (insn, REG_DEAD, SET_SRC (set))
913 || optimize_reg_copy_1 (insn, SET_DEST (set), SET_SRC (set)))
914 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
916 /* Similarly for a pseudo-pseudo copy when SRC is dead. */
917 if (REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
918 optimize_reg_copy_2 (insn, SET_DEST (set), SET_SRC (set));
919 if (regno_src_regno[REGNO (SET_DEST (set))] < 0
920 && SET_SRC (set) != SET_DEST (set))
922 int srcregno = REGNO (SET_SRC(set));
923 if (regno_src_regno[srcregno] >= 0)
924 srcregno = regno_src_regno[srcregno];
925 regno_src_regno[REGNO (SET_DEST (set))] = srcregno;
929 #ifdef REGISTER_CONSTRAINTS
931 = find_matches (insn, &match);
933 if (insn_code_number < 0)
936 /* Now scan through the operands looking for a source operand
937 which is supposed to match the destination operand.
938 Then scan forward for an instruction which uses the dest
940 If it dies there, then replace the dest in both operands with
941 the source operand. */
943 for (operand_number = 0;
944 operand_number < insn_n_operands[insn_code_number];
947 rtx src, dst, src_subreg;
948 enum reg_class src_class, dst_class;
950 match_number = match.with[operand_number];
952 /* Nothing to do if the two operands aren't supposed to match. */
953 if (match_number < 0)
956 src = recog_operand[operand_number];
957 dst = recog_operand[match_number];
959 if (GET_CODE (src) != REG)
963 if (GET_CODE (dst) == SUBREG
964 && GET_MODE_SIZE (GET_MODE (dst))
965 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (dst))))
968 = gen_rtx_SUBREG (GET_MODE (SUBREG_REG (dst)),
969 src, SUBREG_WORD (dst));
970 dst = SUBREG_REG (dst);
972 if (GET_CODE (dst) != REG
973 || REGNO (dst) < FIRST_PSEUDO_REGISTER)
976 if (REGNO (src) < FIRST_PSEUDO_REGISTER)
978 if (match.commutative[operand_number] < operand_number)
979 regno_src_regno[REGNO (dst)] = REGNO (src);
983 if (REG_LIVE_LENGTH (REGNO (src)) < 0)
986 /* operand_number/src must be a read-only operand, and
987 match_operand/dst must be a write-only operand. */
988 if (match.use[operand_number] != READ
989 || match.use[match_number] != WRITE)
992 if (match.early_clobber[match_number]
993 && count_occurrences (PATTERN (insn), src) > 1)
996 /* Make sure match_operand is the destination. */
997 if (recog_operand[match_number] != SET_DEST (set))
1000 /* If the operands already match, then there is nothing to do. */
1001 /* But in the commutative case, we might find a better match. */
1002 if (operands_match_p (src, dst)
1003 || (match.commutative[operand_number] >= 0
1004 && operands_match_p (recog_operand[match.commutative
1005 [operand_number]], dst)
1006 && (replacement_quality (recog_operand[match.commutative
1008 >= replacement_quality (src))))
1011 src_class = reg_preferred_class (REGNO (src));
1012 dst_class = reg_preferred_class (REGNO (dst));
1013 if (src_class != dst_class
1014 && (! reg_class_subset_p (src_class, dst_class)
1015 || CLASS_LIKELY_SPILLED_P (src_class))
1016 && (! reg_class_subset_p (dst_class, src_class)
1017 || CLASS_LIKELY_SPILLED_P (dst_class)))
1020 if (fixup_match_1 (insn, set, src, src_subreg, dst, pass,
1021 operand_number, match_number,
1028 /* A backward pass. Replace input operands with output operands. */
1030 if (regmove_dump_file)
1031 fprintf (regmove_dump_file, "Starting backward pass...\n");
1035 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1037 if (GET_CODE (insn) == NOTE)
1039 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
1041 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
1044 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1046 int insn_code_number = find_matches (insn, &match);
1047 int operand_number, match_number;
1050 if (insn_code_number < 0)
1053 /* Now scan through the operands looking for a destination operand
1054 which is supposed to match a source operand.
1055 Then scan backward for an instruction which sets the source
1056 operand. If safe, then replace the source operand with the
1057 dest operand in both instructions. */
1059 copy_src = NULL_RTX;
1060 copy_dst = NULL_RTX;
1061 for (operand_number = 0;
1062 operand_number < insn_n_operands[insn_code_number];
1065 rtx set, p, src, dst;
1066 rtx src_note, dst_note;
1068 enum reg_class src_class, dst_class;
1071 match_number = match.with[operand_number];
1073 /* Nothing to do if the two operands aren't supposed to match. */
1074 if (match_number < 0)
1077 dst = recog_operand[match_number];
1078 src = recog_operand[operand_number];
1080 if (GET_CODE (src) != REG)
1083 if (GET_CODE (dst) != REG
1084 || REGNO (dst) < FIRST_PSEUDO_REGISTER
1085 || REG_LIVE_LENGTH (REGNO (dst)) < 0)
1088 /* If the operands already match, then there is nothing to do. */
1089 if (operands_match_p (src, dst)
1090 || (match.commutative[operand_number] >= 0
1091 && operands_match_p (recog_operand[match.commutative[operand_number]], dst)))
1094 set = single_set (insn);
1098 /* match_number/dst must be a write-only operand, and
1099 operand_operand/src must be a read-only operand. */
1100 if (match.use[operand_number] != READ
1101 || match.use[match_number] != WRITE)
1104 if (match.early_clobber[match_number]
1105 && count_occurrences (PATTERN (insn), src) > 1)
1108 /* Make sure match_number is the destination. */
1109 if (recog_operand[match_number] != SET_DEST (set))
1112 if (REGNO (src) < FIRST_PSEUDO_REGISTER)
1114 if (GET_CODE (SET_SRC (set)) == PLUS
1115 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT
1116 && XEXP (SET_SRC (set), 0) == src
1117 && fixup_match_2 (insn, dst, src,
1118 XEXP (SET_SRC (set), 1),
1123 src_class = reg_preferred_class (REGNO (src));
1124 dst_class = reg_preferred_class (REGNO (dst));
1125 if (src_class != dst_class
1126 && (! reg_class_subset_p (src_class, dst_class)
1127 || CLASS_LIKELY_SPILLED_P (src_class))
1128 && (! reg_class_subset_p (dst_class, src_class)
1129 || CLASS_LIKELY_SPILLED_P (dst_class)))
1139 /* Can not modify an earlier insn to set dst if this insn
1140 uses an old value in the source. */
1141 if (reg_overlap_mentioned_p (dst, SET_SRC (set)))
1151 if (! (src_note = find_reg_note (insn, REG_DEAD, src)))
1162 /* If src is set once in a different basic block,
1163 and is set equal to a constant, then do not use
1164 it for this optimization, as this would make it
1165 no longer equivalent to a constant. */
1167 if (reg_is_remote_constant_p (src, insn, f))
1178 if (regmove_dump_file)
1179 fprintf (regmove_dump_file,
1180 "Could fix operand %d of insn %d matching operand %d.\n",
1181 operand_number, INSN_UID (insn), match_number);
1183 /* Scan backward to find the first instruction that uses
1184 the input operand. If the operand is set here, then
1185 replace it in both instructions with match_number. */
1187 for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
1191 if (GET_CODE (p) == CODE_LABEL
1192 || GET_CODE (p) == JUMP_INSN
1193 || (GET_CODE (p) == NOTE
1194 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
1195 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
1198 /* ??? We can't scan past the end of a basic block without
1199 updating the register lifetime info
1200 (REG_DEAD/basic_block_live_at_start).
1201 A CALL_INSN might be the last insn of a basic block, if
1202 it is inside an EH region. There is no easy way to tell,
1203 so we just always break when we see a CALL_INSN if
1204 flag_exceptions is nonzero. */
1205 if (flag_exceptions && GET_CODE (p) == CALL_INSN)
1208 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
1213 /* ??? See if all of SRC is set in P. This test is much
1214 more conservative than it needs to be. */
1215 pset = single_set (p);
1216 if (pset && SET_DEST (pset) == src)
1218 /* We use validate_replace_rtx, in case there
1219 are multiple identical source operands. All of
1220 them have to be changed at the same time. */
1221 if (validate_replace_rtx (src, dst, insn))
1223 if (validate_change (p, &SET_DEST (pset),
1228 /* Change all source operands back.
1229 This modifies the dst as a side-effect. */
1230 validate_replace_rtx (dst, src, insn);
1231 /* Now make sure the dst is right. */
1232 validate_change (insn,
1233 recog_operand_loc[match_number],
1240 if (reg_overlap_mentioned_p (src, PATTERN (p))
1241 || reg_overlap_mentioned_p (dst, PATTERN (p)))
1244 /* If we have passed a call instruction, and the
1245 pseudo-reg DST is not already live across a call,
1246 then don't perform the optimization. */
1247 if (GET_CODE (p) == CALL_INSN)
1251 if (REG_N_CALLS_CROSSED (REGNO (dst)) == 0)
1260 /* Remove the death note for SRC from INSN. */
1261 remove_note (insn, src_note);
1262 /* Move the death note for SRC to P if it is used
1264 if (reg_overlap_mentioned_p (src, PATTERN (p)))
1266 XEXP (src_note, 1) = REG_NOTES (p);
1267 REG_NOTES (p) = src_note;
1269 /* If there is a REG_DEAD note for DST on P, then remove
1270 it, because DST is now set there. */
1271 if ((dst_note = find_reg_note (p, REG_DEAD, dst)))
1272 remove_note (p, dst_note);
1274 dstno = REGNO (dst);
1275 srcno = REGNO (src);
1277 REG_N_SETS (dstno)++;
1278 REG_N_SETS (srcno)--;
1280 REG_N_CALLS_CROSSED (dstno) += num_calls;
1281 REG_N_CALLS_CROSSED (srcno) -= num_calls;
1283 REG_LIVE_LENGTH (dstno) += length;
1284 if (REG_LIVE_LENGTH (srcno) >= 0)
1286 REG_LIVE_LENGTH (srcno) -= length;
1287 /* REG_LIVE_LENGTH is only an approximation after
1288 combine if sched is not run, so make sure that we
1289 still have a reasonable value. */
1290 if (REG_LIVE_LENGTH (srcno) < 2)
1291 REG_LIVE_LENGTH (srcno) = 2;
1294 /* We assume that a register is used exactly once per
1295 insn in the updates above. If this is not correct,
1296 no great harm is done. */
1298 REG_N_REFS (dstno) += 2 * loop_depth;
1299 REG_N_REFS (srcno) -= 2 * loop_depth;
1301 /* If that was the only time src was set,
1302 and src was not live at the start of the
1303 function, we know that we have no more
1304 references to src; clear REG_N_REFS so it
1305 won't make reload do any work. */
1306 if (REG_N_SETS (REGNO (src)) == 0
1307 && ! regno_uninitialized (REGNO (src)))
1308 REG_N_REFS (REGNO (src)) = 0;
1310 if (regmove_dump_file)
1311 fprintf (regmove_dump_file,
1312 "Fixed operand %d of insn %d matching operand %d.\n",
1313 operand_number, INSN_UID (insn), match_number);
1319 /* If we weren't able to replace any of the alternatives, try an
1320 alternative appoach of copying the source to the destination. */
1321 if (!success && copy_src != NULL_RTX)
1322 copy_src_to_dest (insn, copy_src, copy_dst, loop_depth);
1326 #endif /* REGISTER_CONSTRAINTS */
1329 /* Returns the INSN_CODE for INSN if its pattern has matching constraints for
1330 any operand. Returns -1 if INSN can't be recognized, or if the alternative
1331 can't be determined.
1333 Initialize the info in MATCHP based on the constraints. */
1336 find_matches (insn, matchp)
1338 struct match *matchp;
1340 int likely_spilled[MAX_RECOG_OPERANDS];
1342 int insn_code_number = recog_memoized (insn);
1343 int any_matches = 0;
1345 if (insn_code_number < 0)
1348 insn_extract (insn);
1349 if (! constrain_operands (insn_code_number, 0))
1352 /* Must initialize this before main loop, because the code for
1353 the commutative case may set matches for operands other than
1355 for (operand_number = insn_n_operands[insn_code_number];
1356 --operand_number >= 0; )
1357 matchp->with[operand_number] = matchp->commutative[operand_number] = -1;
1359 for (operand_number = 0; operand_number < insn_n_operands[insn_code_number];
1365 p = insn_operand_constraint[insn_code_number][operand_number];
1367 likely_spilled[operand_number] = 0;
1368 matchp->use[operand_number] = READ;
1369 matchp->early_clobber[operand_number] = 0;
1371 matchp->use[operand_number] = WRITE;
1373 matchp->use[operand_number] = READWRITE;
1375 for (;*p && i < which_alternative; p++)
1379 while ((c = *p++) != '\0' && c != ',')
1387 matchp->early_clobber[operand_number] = 1;
1390 matchp->commutative[operand_number] = operand_number + 1;
1391 matchp->commutative[operand_number + 1] = operand_number;
1393 case '0': case '1': case '2': case '3': case '4':
1394 case '5': case '6': case '7': case '8': case '9':
1396 if (c < operand_number && likely_spilled[(unsigned char) c])
1398 matchp->with[operand_number] = c;
1400 if (matchp->commutative[operand_number] >= 0)
1401 matchp->with[matchp->commutative[operand_number]] = c;
1403 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'h':
1404 case 'j': case 'k': case 'l': case 'p': case 'q': case 't': case 'u':
1405 case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B':
1406 case 'C': case 'D': case 'W': case 'Y': case 'Z':
1407 if (CLASS_LIKELY_SPILLED_P (REG_CLASS_FROM_LETTER (c)))
1408 likely_spilled[operand_number] = 1;
1412 return any_matches ? insn_code_number : -1;
1415 /* Try to replace output operand DST in SET, with input operand SRC. SET is
1416 the only set in INSN. INSN has just been recgnized and constrained.
1417 SRC is operand number OPERAND_NUMBER in INSN.
1418 DST is operand number MATCH_NUMBER in INSN.
1419 If BACKWARD is nonzero, we have been called in a backward pass.
1420 Return nonzero for success. */
1422 fixup_match_1 (insn, set, src, src_subreg, dst, backward, operand_number,
1423 match_number, regmove_dump_file)
1424 rtx insn, set, src, src_subreg, dst;
1425 int backward, operand_number, match_number;
1426 FILE *regmove_dump_file;
1429 rtx post_inc = 0, post_inc_set = 0, search_end = 0;
1431 int num_calls = 0, s_num_calls = 0;
1432 enum rtx_code code = NOTE;
1433 HOST_WIDE_INT insn_const, newconst;
1434 rtx overlap = 0; /* need to move insn ? */
1435 rtx src_note = find_reg_note (insn, REG_DEAD, src), dst_note;
1436 int length, s_length, true_loop_depth;
1440 /* Look for (set (regX) (op regA constX))
1441 (set (regY) (op regA constY))
1443 (set (regA) (op regA constX)).
1444 (set (regY) (op regA constY-constX)).
1445 This works for add and shift operations, if
1446 regA is dead after or set by the second insn. */
1448 code = GET_CODE (SET_SRC (set));
1449 if ((code == PLUS || code == LSHIFTRT
1450 || code == ASHIFT || code == ASHIFTRT)
1451 && XEXP (SET_SRC (set), 0) == src
1452 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
1453 insn_const = INTVAL (XEXP (SET_SRC (set), 1));
1454 else if (! stable_but_for_p (SET_SRC (set), src, dst))
1457 /* We might find a src_note while scanning. */
1461 if (regmove_dump_file)
1462 fprintf (regmove_dump_file,
1463 "Could fix operand %d of insn %d matching operand %d.\n",
1464 operand_number, INSN_UID (insn), match_number);
1466 /* If SRC is equivalent to a constant set in a different basic block,
1467 then do not use it for this optimization. We want the equivalence
1468 so that if we have to reload this register, we can reload the
1469 constant, rather than extending the lifespan of the register. */
1470 if (reg_is_remote_constant_p (src, insn, get_insns ()))
1473 /* Scan forward to find the next instruction that
1474 uses the output operand. If the operand dies here,
1475 then replace it in both instructions with
1478 for (length = s_length = 0, p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
1480 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
1481 || (GET_CODE (p) == NOTE
1482 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
1483 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
1486 /* ??? We can't scan past the end of a basic block without updating
1487 the register lifetime info (REG_DEAD/basic_block_live_at_start).
1488 A CALL_INSN might be the last insn of a basic block, if it is
1489 inside an EH region. There is no easy way to tell, so we just
1490 always break when we see a CALL_INSN if flag_exceptions is nonzero. */
1491 if (flag_exceptions && GET_CODE (p) == CALL_INSN)
1494 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
1501 if (reg_set_p (src, p) || reg_set_p (dst, p)
1502 || (GET_CODE (PATTERN (p)) == USE
1503 && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
1506 /* See if all of DST dies in P. This test is
1507 slightly more conservative than it needs to be. */
1508 if ((dst_note = find_regno_note (p, REG_DEAD, REGNO (dst)))
1509 && (GET_MODE (XEXP (dst_note, 0)) == GET_MODE (dst)))
1516 /* If an optimization is done, the value of SRC while P
1517 is executed will be changed. Check that this is OK. */
1518 if (reg_overlap_mentioned_p (src, PATTERN (p)))
1520 for (q = p; q; q = NEXT_INSN (q))
1522 if (GET_CODE (q) == CODE_LABEL || GET_CODE (q) == JUMP_INSN
1523 || (GET_CODE (q) == NOTE
1524 && (NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_BEG
1525 || NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_END)))
1531 /* ??? We can't scan past the end of a basic block without
1532 updating the register lifetime info
1533 (REG_DEAD/basic_block_live_at_start).
1534 A CALL_INSN might be the last insn of a basic block, if
1535 it is inside an EH region. There is no easy way to tell,
1536 so we just always break when we see a CALL_INSN if
1537 flag_exceptions is nonzero. */
1538 if (flag_exceptions && GET_CODE (q) == CALL_INSN)
1544 if (GET_RTX_CLASS (GET_CODE (q)) != 'i')
1546 if (reg_overlap_mentioned_p (src, PATTERN (q))
1547 || reg_set_p (src, q))
1551 set2 = single_set (q);
1552 if (! q || ! set2 || GET_CODE (SET_SRC (set2)) != code
1553 || XEXP (SET_SRC (set2), 0) != src
1554 || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT
1555 || (SET_DEST (set2) != src
1556 && ! find_reg_note (q, REG_DEAD, src)))
1558 /* If this is a PLUS, we can still save a register by doing
1561 src -= insn_const; .
1562 This also gives opportunities for subsequent
1563 optimizations in the backward pass, so do it there. */
1564 if (code == PLUS && backward
1566 /* We may not emit an insn directly
1567 after P if the latter sets CC0. */
1568 && ! sets_cc0_p (PATTERN (p))
1576 newconst = -insn_const;
1584 newconst = INTVAL (XEXP (SET_SRC (set2), 1)) - insn_const;
1585 /* Reject out of range shifts. */
1589 >= GET_MODE_BITSIZE (GET_MODE (SET_SRC (set2))))))
1594 if (SET_DEST (set2) != src)
1595 post_inc_set = set2;
1598 /* We use 1 as last argument to validate_change so that all
1599 changes are accepted or rejected together by apply_change_group
1600 when it is called by validate_replace_rtx . */
1601 validate_change (q, &XEXP (SET_SRC (set2), 1),
1602 GEN_INT (newconst), 1);
1604 validate_change (insn, recog_operand_loc[match_number], src, 1);
1605 if (validate_replace_rtx (dst, src_subreg, p))
1610 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
1612 if (! src_note && reg_overlap_mentioned_p (src, PATTERN (p)))
1614 /* INSN was already checked to be movable when
1615 we found no REG_DEAD note for src on it. */
1617 src_note = find_reg_note (p, REG_DEAD, src);
1620 /* If we have passed a call instruction, and the pseudo-reg SRC is not
1621 already live across a call, then don't perform the optimization. */
1622 if (GET_CODE (p) == CALL_INSN)
1624 if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
1638 true_loop_depth = backward ? 2 - loop_depth : loop_depth;
1640 /* Remove the death note for DST from P. */
1641 remove_note (p, dst_note);
1644 post_inc = emit_insn_after (copy_rtx (PATTERN (insn)), p);
1645 #if defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT)
1647 && try_auto_increment (search_end, post_inc, 0, src, newconst, 1))
1650 validate_change (insn, &XEXP (SET_SRC (set), 1), GEN_INT (insn_const), 0);
1651 REG_N_SETS (REGNO (src))++;
1652 REG_N_REFS (REGNO (src)) += true_loop_depth;
1653 REG_LIVE_LENGTH (REGNO (src))++;
1657 /* The lifetime of src and dest overlap,
1658 but we can change this by moving insn. */
1659 rtx pat = PATTERN (insn);
1661 remove_note (overlap, src_note);
1662 #if defined (HAVE_POST_INCREMENT) || defined (HAVE_POST_DECREMENT)
1664 && try_auto_increment (overlap, insn, 0, src, insn_const, 0))
1669 rtx notes = REG_NOTES (insn);
1671 emit_insn_after_with_line_notes (pat, PREV_INSN (p), insn);
1672 PUT_CODE (insn, NOTE);
1673 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1674 NOTE_SOURCE_FILE (insn) = 0;
1675 /* emit_insn_after_with_line_notes has no
1676 return value, so search for the new insn. */
1677 for (insn = p; PATTERN (insn) != pat; )
1678 insn = PREV_INSN (insn);
1680 REG_NOTES (insn) = notes;
1683 /* Sometimes we'd generate src = const; src += n;
1684 if so, replace the instruction that set src
1685 in the first place. */
1687 if (! overlap && (code == PLUS || code == MINUS))
1689 rtx note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
1691 int num_calls2 = 0, s_length2 = 0;
1693 if (note && CONSTANT_P (XEXP (note, 0)))
1695 for (q = PREV_INSN (insn); q; q = PREV_INSN(q))
1697 if (GET_CODE (q) == CODE_LABEL || GET_CODE (q) == JUMP_INSN
1698 || (GET_CODE (q) == NOTE
1699 && (NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_BEG
1700 || NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_END)))
1706 /* ??? We can't scan past the end of a basic block without
1707 updating the register lifetime info
1708 (REG_DEAD/basic_block_live_at_start).
1709 A CALL_INSN might be the last insn of a basic block, if
1710 it is inside an EH region. There is no easy way to tell,
1711 so we just always break when we see a CALL_INSN if
1712 flag_exceptions is nonzero. */
1713 if (flag_exceptions && GET_CODE (q) == CALL_INSN)
1719 if (GET_RTX_CLASS (GET_CODE (q)) != 'i')
1722 if (reg_set_p (src, q))
1724 set2 = single_set (q);
1727 if (reg_overlap_mentioned_p (src, PATTERN (q)))
1732 if (GET_CODE (p) == CALL_INSN)
1735 if (q && set2 && SET_DEST (set2) == src && CONSTANT_P (SET_SRC (set2))
1736 && validate_change (insn, &SET_SRC (set), XEXP (note, 0), 0))
1739 NOTE_LINE_NUMBER (q) = NOTE_INSN_DELETED;
1740 NOTE_SOURCE_FILE (q) = 0;
1741 REG_N_SETS (REGNO (src))--;
1742 REG_N_CALLS_CROSSED (REGNO (src)) -= num_calls2;
1743 REG_N_REFS (REGNO (src)) -= true_loop_depth;
1744 REG_LIVE_LENGTH (REGNO (src)) -= s_length2;
1750 /* Don't remove this seemingly useless if, it is needed to pair with the
1751 else in the next two conditionally included code blocks. */
1754 #if defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT)
1755 else if ((code == PLUS || code == MINUS) && insn_const
1756 && try_auto_increment (p, insn, 0, src, insn_const, 1))
1759 #if defined (HAVE_POST_INCREMENT) || defined (HAVE_POST_DECREMENT)
1761 && try_auto_increment (p, post_inc, post_inc_set, src, newconst, 0))
1764 #if defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT)
1765 /* If post_inc still prevails, try to find an
1766 insn where it can be used as a pre-in/decrement.
1767 If code is MINUS, this was already tried. */
1768 if (post_inc && code == PLUS
1769 /* Check that newconst is likely to be usable
1770 in a pre-in/decrement before starting the search. */
1772 #if defined (HAVE_PRE_INCREMENT)
1773 || (newconst > 0 && newconst <= MOVE_MAX)
1775 #if defined (HAVE_PRE_DECREMENT)
1776 || (newconst < 0 && newconst >= -MOVE_MAX)
1778 ) && exact_log2 (newconst))
1782 inc_dest = post_inc_set ? SET_DEST (post_inc_set) : src;
1783 for (q = post_inc; (q = NEXT_INSN (q)); )
1785 if (GET_CODE (q) == CODE_LABEL || GET_CODE (q) == JUMP_INSN
1786 || (GET_CODE (q) == NOTE
1787 && (NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_BEG
1788 || NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_END)))
1791 /* ??? We can't scan past the end of a basic block without updating
1792 the register lifetime info (REG_DEAD/basic_block_live_at_start).
1793 A CALL_INSN might be the last insn of a basic block, if it
1794 is inside an EH region. There is no easy way to tell so we
1795 just always break when we see a CALL_INSN if flag_exceptions
1797 if (flag_exceptions && GET_CODE (q) == CALL_INSN)
1800 if (GET_RTX_CLASS (GET_CODE (q)) != 'i')
1802 if (src != inc_dest && (reg_overlap_mentioned_p (src, PATTERN (q))
1803 || reg_set_p (src, q)))
1805 if (reg_set_p (inc_dest, q))
1807 if (reg_overlap_mentioned_p (inc_dest, PATTERN (q)))
1809 try_auto_increment (q, post_inc,
1810 post_inc_set, inc_dest, newconst, 1);
1815 #endif /* defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT) */
1816 /* Move the death note for DST to INSN if it is used
1818 if (reg_overlap_mentioned_p (dst, PATTERN (insn)))
1820 XEXP (dst_note, 1) = REG_NOTES (insn);
1821 REG_NOTES (insn) = dst_note;
1826 /* Move the death note for SRC from INSN to P. */
1828 remove_note (insn, src_note);
1829 XEXP (src_note, 1) = REG_NOTES (p);
1830 REG_NOTES (p) = src_note;
1832 REG_N_CALLS_CROSSED (REGNO (src)) += s_num_calls;
1835 REG_N_SETS (REGNO (src))++;
1836 REG_N_SETS (REGNO (dst))--;
1838 REG_N_CALLS_CROSSED (REGNO (dst)) -= num_calls;
1840 REG_LIVE_LENGTH (REGNO (src)) += s_length;
1841 if (REG_LIVE_LENGTH (REGNO (dst)) >= 0)
1843 REG_LIVE_LENGTH (REGNO (dst)) -= length;
1844 /* REG_LIVE_LENGTH is only an approximation after
1845 combine if sched is not run, so make sure that we
1846 still have a reasonable value. */
1847 if (REG_LIVE_LENGTH (REGNO (dst)) < 2)
1848 REG_LIVE_LENGTH (REGNO (dst)) = 2;
1851 /* We assume that a register is used exactly once per
1852 insn in the updates above. If this is not correct,
1853 no great harm is done. */
1855 REG_N_REFS (REGNO (src)) += 2 * true_loop_depth;
1856 REG_N_REFS (REGNO (dst)) -= 2 * true_loop_depth;
1858 /* If that was the only time dst was set,
1859 and dst was not live at the start of the
1860 function, we know that we have no more
1861 references to dst; clear REG_N_REFS so it
1862 won't make reload do any work. */
1863 if (REG_N_SETS (REGNO (dst)) == 0
1864 && ! regno_uninitialized (REGNO (dst)))
1865 REG_N_REFS (REGNO (dst)) = 0;
1867 if (regmove_dump_file)
1868 fprintf (regmove_dump_file,
1869 "Fixed operand %d of insn %d matching operand %d.\n",
1870 operand_number, INSN_UID (insn), match_number);
1875 /* return nonzero if X is stable but for mentioning SRC or mentioning /
1876 changing DST . If in doubt, presume it is unstable. */
1878 stable_but_for_p (x, src, dst)
1881 RTX_CODE code = GET_CODE (x);
1882 switch (GET_RTX_CLASS (code))
1884 case '<': case '1': case 'c': case '2': case 'b': case '3':
1887 char *fmt = GET_RTX_FORMAT (code);
1888 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1889 if (fmt[i] == 'e' && ! stable_but_for_p (XEXP (x, i), src, dst))
1894 if (x == src || x == dst)
1898 return ! rtx_unstable_p (x);
1902 /* Test if regmove seems profitable for this target. Regmove is useful only
1903 if some common patterns are two address, i.e. require matching constraints,
1904 so we check that condition here. */
1907 regmove_profitable_p ()
1909 #ifdef REGISTER_CONSTRAINTS
1911 enum machine_mode mode;
1912 optab tstoptab = add_optab;
1913 do /* check add_optab and ashl_optab */
1914 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1915 mode = GET_MODE_WIDER_MODE (mode))
1917 int icode = (int) tstoptab->handlers[(int) mode].insn_code;
1918 rtx reg0, reg1, reg2, pat;
1921 if (GET_MODE_BITSIZE (mode) < 32 || icode == CODE_FOR_nothing)
1923 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1924 if (TEST_HARD_REG_BIT (reg_class_contents[GENERAL_REGS], i))
1926 if (i + 2 >= FIRST_PSEUDO_REGISTER)
1928 reg0 = gen_rtx_REG (insn_operand_mode[icode][0], i);
1929 reg1 = gen_rtx_REG (insn_operand_mode[icode][1], i + 1);
1930 reg2 = gen_rtx_REG (insn_operand_mode[icode][2], i + 2);
1931 if (! (*insn_operand_predicate[icode][0]) (reg0, VOIDmode)
1932 || ! (*insn_operand_predicate[icode][1]) (reg1, VOIDmode)
1933 || ! (*insn_operand_predicate[icode][2]) (reg2, VOIDmode))
1935 pat = GEN_FCN (icode) (reg0, reg1, reg2);
1938 if (GET_CODE (pat) == SEQUENCE)
1939 pat = XVECEXP (pat, 0, XVECLEN (pat, 0) - 1);
1941 pat = make_insn_raw (pat);
1942 if (! single_set (pat)
1943 || GET_CODE (SET_SRC (single_set (pat))) != tstoptab->code)
1944 /* Unexpected complexity; don't need to handle this unless
1945 we find a machine where this occurs and regmove should
1948 if (find_matches (pat, &match) >= 0)
1952 while (tstoptab != ashl_optab && (tstoptab = ashl_optab, 1));
1953 #endif /* REGISTER_CONSTRAINTS */