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