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. */
28 #include "rtl.h" /* stdio.h must precede rtl.h for FFS. */
29 #include "insn-config.h"
34 #include "hard-reg-set.h"
37 #include "insn-flags.h"
38 #include "basic-block.h"
41 static int optimize_reg_copy_1 PROTO((rtx, rtx, rtx));
42 static void optimize_reg_copy_2 PROTO((rtx, rtx, rtx));
43 static void optimize_reg_copy_3 PROTO((rtx, rtx, rtx));
44 static rtx gen_add3_insn PROTO((rtx, rtx, rtx));
45 static void copy_src_to_dest PROTO((rtx, rtx, rtx, int));
46 static int *regmove_bb_head;
49 int with[MAX_RECOG_OPERANDS];
50 enum { READ, WRITE, READWRITE } use[MAX_RECOG_OPERANDS];
51 int commutative[MAX_RECOG_OPERANDS];
52 int early_clobber[MAX_RECOG_OPERANDS];
55 static int try_auto_increment PROTO((rtx, rtx, rtx, rtx, HOST_WIDE_INT, int));
56 static int find_matches PROTO((rtx, struct match *));
57 static int fixup_match_1 PROTO((rtx, rtx, rtx, rtx, rtx, int, int, int, FILE *))
59 static int reg_is_remote_constant_p PROTO((rtx, rtx, rtx));
60 static int stable_but_for_p PROTO((rtx, rtx, rtx));
61 static int regclass_compatible_p PROTO((int, int));
62 static int loop_depth;
64 /* Return non-zero if registers with CLASS1 and CLASS2 can be merged without
65 causing too much register allocation problems. */
67 regclass_compatible_p (class0, class1)
70 return (class0 == class1
71 || (reg_class_subset_p (class0, class1)
72 && ! CLASS_LIKELY_SPILLED_P (class0))
73 || (reg_class_subset_p (class1, class0)
74 && ! CLASS_LIKELY_SPILLED_P (class1)));
77 /* Generate and return an insn body to add r1 and c,
78 storing the result in r0. */
80 gen_add3_insn (r0, r1, c)
83 int icode = (int) add_optab->handlers[(int) GET_MODE (r0)].insn_code;
85 if (icode == CODE_FOR_nothing
86 || ! (*insn_operand_predicate[icode][0]) (r0, insn_operand_mode[icode][0])
87 || ! (*insn_operand_predicate[icode][1]) (r1, insn_operand_mode[icode][1])
88 || ! (*insn_operand_predicate[icode][2]) (c, insn_operand_mode[icode][2]))
91 return (GEN_FCN (icode) (r0, r1, c));
95 /* INC_INSN is an instruction that adds INCREMENT to REG.
96 Try to fold INC_INSN as a post/pre in/decrement into INSN.
97 Iff INC_INSN_SET is nonzero, inc_insn has a destination different from src.
98 Return nonzero for success. */
100 try_auto_increment (insn, inc_insn, inc_insn_set, reg, increment, pre)
101 rtx reg, insn, inc_insn ,inc_insn_set;
102 HOST_WIDE_INT increment;
105 enum rtx_code inc_code;
107 rtx pset = single_set (insn);
110 /* Can't use the size of SET_SRC, we might have something like
111 (sign_extend:SI (mem:QI ... */
112 rtx use = find_use_as_address (pset, reg, 0);
113 if (use != 0 && use != (rtx) 1)
115 int size = GET_MODE_SIZE (GET_MODE (use));
117 || (HAVE_POST_INCREMENT
118 && pre == 0 && (inc_code = POST_INC, increment == size))
119 || (HAVE_PRE_INCREMENT
120 && pre == 1 && (inc_code = PRE_INC, increment == size))
121 || (HAVE_POST_DECREMENT
122 && pre == 0 && (inc_code = POST_DEC, increment == -size))
123 || (HAVE_PRE_DECREMENT
124 && 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;
153 static int *regno_src_regno;
155 /* Indicate how good a choice REG (which appears as a source) is to replace
156 a destination register with. The higher the returned value, the better
157 the choice. The main objective is to avoid using a register that is
158 a candidate for tying to a hard register, since the output might in
159 turn be a candidate to be tied to a different hard register. */
161 replacement_quality(reg)
166 /* Bad if this isn't a register at all. */
167 if (GET_CODE (reg) != REG)
170 /* If this register is not meant to get a hard register,
171 it is a poor choice. */
172 if (REG_LIVE_LENGTH (REGNO (reg)) < 0)
175 src_regno = regno_src_regno[REGNO (reg)];
177 /* If it was not copied from another register, it is fine. */
181 /* Copied from a hard register? */
182 if (src_regno < FIRST_PSEUDO_REGISTER)
185 /* Copied from a pseudo register - not as bad as from a hard register,
186 yet still cumbersome, since the register live length will be lengthened
187 when the registers get tied. */
191 /* INSN is a copy from SRC to DEST, both registers, and SRC does not die
194 Search forward to see if SRC dies before either it or DEST is modified,
195 but don't scan past the end of a basic block. If so, we can replace SRC
196 with DEST and let SRC die in INSN.
198 This will reduce the number of registers live in that range and may enable
199 DEST to be tied to SRC, thus often saving one register in addition to a
200 register-register copy. */
203 optimize_reg_copy_1 (insn, dest, src)
211 int sregno = REGNO (src);
212 int dregno = REGNO (dest);
214 /* We don't want to mess with hard regs if register classes are small. */
216 || (SMALL_REGISTER_CLASSES
217 && (sregno < FIRST_PSEUDO_REGISTER
218 || dregno < FIRST_PSEUDO_REGISTER))
219 /* We don't see all updates to SP if they are in an auto-inc memory
220 reference, so we must disallow this optimization on them. */
221 || sregno == STACK_POINTER_REGNUM || dregno == STACK_POINTER_REGNUM)
224 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
226 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
227 || (GET_CODE (p) == NOTE
228 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
229 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
232 /* ??? We can't scan past the end of a basic block without updating
233 the register lifetime info (REG_DEAD/basic_block_live_at_start).
234 A CALL_INSN might be the last insn of a basic block, if it is inside
235 an EH region. There is no easy way to tell, so we just always break
236 when we see a CALL_INSN if flag_exceptions is nonzero. */
237 if (flag_exceptions && GET_CODE (p) == CALL_INSN)
240 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
243 if (reg_set_p (src, p) || reg_set_p (dest, p)
244 /* Don't change a USE of a register. */
245 || (GET_CODE (PATTERN (p)) == USE
246 && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
249 /* See if all of SRC dies in P. This test is slightly more
250 conservative than it needs to be. */
251 if ((note = find_regno_note (p, REG_DEAD, sregno)) != 0
252 && GET_MODE (XEXP (note, 0)) == GET_MODE (src))
260 /* We can do the optimization. Scan forward from INSN again,
261 replacing regs as we go. Set FAILED if a replacement can't
262 be done. In that case, we can't move the death note for SRC.
263 This should be rare. */
265 /* Set to stop at next insn. */
266 for (q = next_real_insn (insn);
267 q != next_real_insn (p);
268 q = next_real_insn (q))
270 if (reg_overlap_mentioned_p (src, PATTERN (q)))
272 /* If SRC is a hard register, we might miss some
273 overlapping registers with validate_replace_rtx,
274 so we would have to undo it. We can't if DEST is
275 present in the insn, so fail in that combination
277 if (sregno < FIRST_PSEUDO_REGISTER
278 && reg_mentioned_p (dest, PATTERN (q)))
281 /* Replace all uses and make sure that the register
282 isn't still present. */
283 else if (validate_replace_rtx (src, dest, q)
284 && (sregno >= FIRST_PSEUDO_REGISTER
285 || ! reg_overlap_mentioned_p (src,
288 /* We assume that a register is used exactly once per
289 insn in the REG_N_REFS updates below. If this is not
290 correct, no great harm is done.
293 We do not undo this substitution if something later
294 fails. Therefore, we must update the other REG_N_*
295 counters now to keep them accurate. */
296 if (sregno >= FIRST_PSEUDO_REGISTER)
298 REG_N_REFS (sregno) -= loop_depth;
300 if (REG_LIVE_LENGTH (sregno) >= 0)
302 REG_LIVE_LENGTH (sregno) -= length;
303 /* REG_LIVE_LENGTH is only an approximation after
304 combine if sched is not run, so make sure that
305 we still have a reasonable value. */
306 if (REG_LIVE_LENGTH (sregno) < 2)
307 REG_LIVE_LENGTH (sregno) = 2;
310 REG_N_CALLS_CROSSED (sregno) -= n_calls;
313 if (dregno >= FIRST_PSEUDO_REGISTER)
315 REG_N_REFS (dregno) += loop_depth;
317 if (REG_LIVE_LENGTH (dregno) >= 0)
318 REG_LIVE_LENGTH (dregno) += d_length;
320 REG_N_CALLS_CROSSED (dregno) += d_n_calls;
323 /* We've done a substitution, clear the counters. */
331 validate_replace_rtx (dest, src, q);
336 /* Count the insns and CALL_INSNs passed. If we passed the
337 death note of DEST, show increased live length. */
342 /* If the insn in which SRC dies is a CALL_INSN, don't count it
343 as a call that has been crossed. Otherwise, count it. */
344 if (q != p && GET_CODE (q) == CALL_INSN)
351 /* If DEST dies here, remove the death note and save it for
352 later. Make sure ALL of DEST dies here; again, this is
353 overly conservative. */
355 && (dest_death = find_regno_note (q, REG_DEAD, dregno)) != 0)
357 if (GET_MODE (XEXP (dest_death, 0)) != GET_MODE (dest))
358 failed = 1, dest_death = 0;
360 remove_note (q, dest_death);
366 if (sregno >= FIRST_PSEUDO_REGISTER)
368 if (REG_LIVE_LENGTH (sregno) >= 0)
370 REG_LIVE_LENGTH (sregno) -= length;
371 /* REG_LIVE_LENGTH is only an approximation after
372 combine if sched is not run, so make sure that we
373 still have a reasonable value. */
374 if (REG_LIVE_LENGTH (sregno) < 2)
375 REG_LIVE_LENGTH (sregno) = 2;
378 REG_N_CALLS_CROSSED (sregno) -= n_calls;
381 if (dregno >= FIRST_PSEUDO_REGISTER)
383 if (REG_LIVE_LENGTH (dregno) >= 0)
384 REG_LIVE_LENGTH (dregno) += d_length;
386 REG_N_CALLS_CROSSED (dregno) += d_n_calls;
389 /* Move death note of SRC from P to INSN. */
390 remove_note (p, note);
391 XEXP (note, 1) = REG_NOTES (insn);
392 REG_NOTES (insn) = note;
395 /* Put death note of DEST on P if we saw it die. */
398 XEXP (dest_death, 1) = REG_NOTES (p);
399 REG_NOTES (p) = dest_death;
405 /* If SRC is a hard register which is set or killed in some other
406 way, we can't do this optimization. */
407 else if (sregno < FIRST_PSEUDO_REGISTER
408 && dead_or_set_p (p, src))
414 /* INSN is a copy of SRC to DEST, in which SRC dies. See if we now have
415 a sequence of insns that modify DEST followed by an insn that sets
416 SRC to DEST in which DEST dies, with no prior modification of DEST.
417 (There is no need to check if the insns in between actually modify
418 DEST. We should not have cases where DEST is not modified, but
419 the optimization is safe if no such modification is detected.)
420 In that case, we can replace all uses of DEST, starting with INSN and
421 ending with the set of SRC to DEST, with SRC. We do not do this
422 optimization if a CALL_INSN is crossed unless SRC already crosses a
423 call or if DEST dies before the copy back to SRC.
425 It is assumed that DEST and SRC are pseudos; it is too complicated to do
426 this for hard registers since the substitutions we may make might fail. */
429 optimize_reg_copy_2 (insn, dest, src)
436 int sregno = REGNO (src);
437 int dregno = REGNO (dest);
439 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
441 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
442 || (GET_CODE (p) == NOTE
443 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
444 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
447 /* ??? We can't scan past the end of a basic block without updating
448 the register lifetime info (REG_DEAD/basic_block_live_at_start).
449 A CALL_INSN might be the last insn of a basic block, if it is inside
450 an EH region. There is no easy way to tell, so we just always break
451 when we see a CALL_INSN if flag_exceptions is nonzero. */
452 if (flag_exceptions && GET_CODE (p) == CALL_INSN)
455 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
458 set = single_set (p);
459 if (set && SET_SRC (set) == dest && SET_DEST (set) == src
460 && find_reg_note (p, REG_DEAD, dest))
462 /* We can do the optimization. Scan forward from INSN again,
463 replacing regs as we go. */
465 /* Set to stop at next insn. */
466 for (q = insn; q != NEXT_INSN (p); q = NEXT_INSN (q))
467 if (GET_RTX_CLASS (GET_CODE (q)) == 'i')
469 if (reg_mentioned_p (dest, PATTERN (q)))
471 PATTERN (q) = replace_rtx (PATTERN (q), dest, src);
473 /* We assume that a register is used exactly once per
474 insn in the updates below. If this is not correct,
475 no great harm is done. */
476 REG_N_REFS (dregno) -= loop_depth;
477 REG_N_REFS (sregno) += loop_depth;
481 if (GET_CODE (q) == CALL_INSN)
483 REG_N_CALLS_CROSSED (dregno)--;
484 REG_N_CALLS_CROSSED (sregno)++;
488 remove_note (p, find_reg_note (p, REG_DEAD, dest));
489 REG_N_DEATHS (dregno)--;
490 remove_note (insn, find_reg_note (insn, REG_DEAD, src));
491 REG_N_DEATHS (sregno)--;
495 if (reg_set_p (src, p)
496 || find_reg_note (p, REG_DEAD, dest)
497 || (GET_CODE (p) == CALL_INSN && REG_N_CALLS_CROSSED (sregno) == 0))
501 /* INSN is a ZERO_EXTEND or SIGN_EXTEND of SRC to DEST.
502 Look if SRC dies there, and if it is only set once, by loading
503 it from memory. If so, try to encorporate the zero/sign extension
504 into the memory read, change SRC to the mode of DEST, and alter
505 the remaining accesses to use the appropriate SUBREG. This allows
506 SRC and DEST to be tied later. */
508 optimize_reg_copy_3 (insn, dest, src)
513 rtx src_reg = XEXP (src, 0);
514 int src_no = REGNO (src_reg);
515 int dst_no = REGNO (dest);
517 enum machine_mode old_mode;
519 if (src_no < FIRST_PSEUDO_REGISTER
520 || dst_no < FIRST_PSEUDO_REGISTER
521 || ! find_reg_note (insn, REG_DEAD, src_reg)
522 || REG_N_SETS (src_no) != 1)
524 for (p = PREV_INSN (insn); ! reg_set_p (src_reg, p); p = PREV_INSN (p))
526 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
527 || (GET_CODE (p) == NOTE
528 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
529 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
532 /* ??? We can't scan past the end of a basic block without updating
533 the register lifetime info (REG_DEAD/basic_block_live_at_start).
534 A CALL_INSN might be the last insn of a basic block, if it is inside
535 an EH region. There is no easy way to tell, so we just always break
536 when we see a CALL_INSN if flag_exceptions is nonzero. */
537 if (flag_exceptions && GET_CODE (p) == CALL_INSN)
540 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
543 if (! (set = single_set (p))
544 || GET_CODE (SET_SRC (set)) != MEM
545 || SET_DEST (set) != src_reg)
548 /* Be conserative: although this optimization is also valid for
549 volatile memory references, that could cause trouble in later passes. */
550 if (MEM_VOLATILE_P (SET_SRC (set)))
553 /* Do not use a SUBREG to truncate from one mode to another if truncation
555 if (GET_MODE_BITSIZE (GET_MODE (src_reg)) <= GET_MODE_BITSIZE (GET_MODE (src))
556 && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (GET_MODE (src)),
557 GET_MODE_BITSIZE (GET_MODE (src_reg))))
560 old_mode = GET_MODE (src_reg);
561 PUT_MODE (src_reg, GET_MODE (src));
562 XEXP (src, 0) = SET_SRC (set);
564 /* Include this change in the group so that it's easily undone if
565 one of the changes in the group is invalid. */
566 validate_change (p, &SET_SRC (set), src, 1);
568 /* Now walk forward making additional replacements. We want to be able
569 to undo all the changes if a later substitution fails. */
570 subreg = gen_rtx_SUBREG (old_mode, src_reg, 0);
571 while (p = NEXT_INSN (p), p != insn)
573 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
576 /* Make a tenative change. */
577 validate_replace_rtx_group (src_reg, subreg, p);
580 validate_replace_rtx_group (src, src_reg, insn);
582 /* Now see if all the changes are valid. */
583 if (! apply_change_group ())
585 /* One or more changes were no good. Back out everything. */
586 PUT_MODE (src_reg, old_mode);
587 XEXP (src, 0) = src_reg;
592 /* If we were not able to update the users of src to use dest directly, try
593 instead moving the value to dest directly before the operation. */
596 copy_src_to_dest (insn, src, dest, loop_depth)
615 /* A REG_LIVE_LENGTH of -1 indicates the register is equivalent to a constant
616 or memory location and is used infrequently; a REG_LIVE_LENGTH of -2 is
617 parameter when there is no frame pointer that is not allocated a register.
618 For now, we just reject them, rather than incrementing the live length. */
620 if (GET_CODE (src) == REG
621 && REG_LIVE_LENGTH (REGNO (src)) > 0
622 && GET_CODE (dest) == REG
623 && REG_LIVE_LENGTH (REGNO (dest)) > 0
624 && (set = single_set (insn)) != NULL_RTX
625 && !reg_mentioned_p (dest, SET_SRC (set))
626 && GET_MODE (src) == GET_MODE (dest))
628 int old_num_regs = reg_rtx_no;
630 /* Generate the src->dest move. */
632 emit_move_insn (dest, src);
633 seq = gen_sequence ();
635 /* If this sequence uses new registers, we may not use it. */
636 if (old_num_regs != reg_rtx_no
637 || ! validate_replace_rtx (src, dest, insn))
639 /* We have to restore reg_rtx_no to its old value, lest
640 recompute_reg_usage will try to compute the usage of the
641 new regs, yet reg_n_info is not valid for them. */
642 reg_rtx_no = old_num_regs;
645 emit_insn_before (seq, insn);
646 move_insn = PREV_INSN (insn);
647 p_move_notes = ®_NOTES (move_insn);
648 p_insn_notes = ®_NOTES (insn);
650 /* Move any notes mentioning src to the move instruction */
651 for (link = REG_NOTES (insn); link != NULL_RTX; link = next)
653 next = XEXP (link, 1);
654 if (XEXP (link, 0) == src)
656 *p_move_notes = link;
657 p_move_notes = &XEXP (link, 1);
661 *p_insn_notes = link;
662 p_insn_notes = &XEXP (link, 1);
666 *p_move_notes = NULL_RTX;
667 *p_insn_notes = NULL_RTX;
669 /* Is the insn the head of a basic block? If so extend it */
670 insn_uid = INSN_UID (insn);
671 move_uid = INSN_UID (move_insn);
672 bb = regmove_bb_head[insn_uid];
675 basic_block_head[bb] = move_insn;
676 regmove_bb_head[insn_uid] = -1;
679 /* Update the various register tables. */
680 dest_regno = REGNO (dest);
681 REG_N_SETS (dest_regno) += loop_depth;
682 REG_N_REFS (dest_regno) += loop_depth;
683 REG_LIVE_LENGTH (dest_regno)++;
684 if (REGNO_FIRST_UID (dest_regno) == insn_uid)
685 REGNO_FIRST_UID (dest_regno) = move_uid;
687 src_regno = REGNO (src);
688 if (! find_reg_note (move_insn, REG_DEAD, src))
689 REG_LIVE_LENGTH (src_regno)++;
691 if (REGNO_FIRST_UID (src_regno) == insn_uid)
692 REGNO_FIRST_UID (src_regno) = move_uid;
694 if (REGNO_LAST_UID (src_regno) == insn_uid)
695 REGNO_LAST_UID (src_regno) = move_uid;
697 if (REGNO_LAST_NOTE_UID (src_regno) == insn_uid)
698 REGNO_LAST_NOTE_UID (src_regno) = move_uid;
703 /* Return whether REG is set in only one location, and is set to a
704 constant, but is set in a different basic block from INSN (an
705 instructions which uses REG). In this case REG is equivalent to a
706 constant, and we don't want to break that equivalence, because that
707 may increase register pressure and make reload harder. If REG is
708 set in the same basic block as INSN, we don't worry about it,
709 because we'll probably need a register anyhow (??? but what if REG
710 is used in a different basic block as well as this one?). FIRST is
711 the first insn in the function. */
714 reg_is_remote_constant_p (reg, insn, first)
721 if (REG_N_SETS (REGNO (reg)) != 1)
724 /* Look for the set. */
725 for (p = LOG_LINKS (insn); p; p = XEXP (p, 1))
729 if (REG_NOTE_KIND (p) != 0)
731 s = single_set (XEXP (p, 0));
733 && GET_CODE (SET_DEST (s)) == REG
734 && REGNO (SET_DEST (s)) == REGNO (reg))
736 /* The register is set in the same basic block. */
741 for (p = first; p && p != insn; p = NEXT_INSN (p))
745 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
749 && GET_CODE (SET_DEST (s)) == REG
750 && REGNO (SET_DEST (s)) == REGNO (reg))
752 /* This is the instruction which sets REG. If there is a
753 REG_EQUAL note, then REG is equivalent to a constant. */
754 if (find_reg_note (p, REG_EQUAL, NULL_RTX))
763 /* INSN is adding a CONST_INT to a REG. We search backwards looking for
764 another add immediate instruction with the same source and dest registers,
765 and if we find one, we change INSN to an increment, and return 1. If
766 no changes are made, we return 0.
769 (set (reg100) (plus reg1 offset1))
771 (set (reg100) (plus reg1 offset2))
773 (set (reg100) (plus reg1 offset1))
775 (set (reg100) (plus reg100 offset2-offset1)) */
777 /* ??? What does this comment mean? */
778 /* cse disrupts preincrement / postdecrement squences when it finds a
779 hard register as ultimate source, like the frame pointer. */
782 fixup_match_2 (insn, dst, src, offset, regmove_dump_file)
783 rtx insn, dst, src, offset;
784 FILE *regmove_dump_file;
786 rtx p, dst_death = 0;
787 int length, num_calls = 0;
789 /* If SRC dies in INSN, we'd have to move the death note. This is
790 considered to be very unlikely, so we just skip the optimization
792 if (find_regno_note (insn, REG_DEAD, REGNO (src)))
795 /* Scan backward to find the first instruction that sets DST. */
797 for (length = 0, p = PREV_INSN (insn); p; p = PREV_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)))
808 /* ??? We can't scan past the end of a basic block without updating
809 the register lifetime info (REG_DEAD/basic_block_live_at_start).
810 A CALL_INSN might be the last insn of a basic block, if it is inside
811 an EH region. There is no easy way to tell, so we just always break
812 when we see a CALL_INSN if flag_exceptions is nonzero. */
813 if (flag_exceptions && GET_CODE (p) == CALL_INSN)
816 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
819 if (find_regno_note (p, REG_DEAD, REGNO (dst)))
824 pset = single_set (p);
825 if (pset && SET_DEST (pset) == dst
826 && GET_CODE (SET_SRC (pset)) == PLUS
827 && XEXP (SET_SRC (pset), 0) == src
828 && GET_CODE (XEXP (SET_SRC (pset), 1)) == CONST_INT)
830 HOST_WIDE_INT newconst
831 = INTVAL (offset) - INTVAL (XEXP (SET_SRC (pset), 1));
832 rtx add = gen_add3_insn (dst, dst, GEN_INT (newconst));
834 if (add && validate_change (insn, &PATTERN (insn), add, 0))
836 /* Remove the death note for DST from DST_DEATH. */
839 remove_death (REGNO (dst), dst_death);
840 REG_LIVE_LENGTH (REGNO (dst)) += length;
841 REG_N_CALLS_CROSSED (REGNO (dst)) += num_calls;
844 REG_N_REFS (REGNO (dst)) += loop_depth;
845 REG_N_REFS (REGNO (src)) -= loop_depth;
847 if (regmove_dump_file)
848 fprintf (regmove_dump_file,
849 "Fixed operand of insn %d.\n",
853 for (p = PREV_INSN (insn); p; p = PREV_INSN (p))
855 if (GET_CODE (p) == CODE_LABEL
856 || GET_CODE (p) == JUMP_INSN
857 || (GET_CODE (p) == NOTE
858 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
859 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
861 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
863 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
865 if (try_auto_increment (p, insn, 0, dst, newconst, 0))
870 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
872 if (GET_CODE (p) == CODE_LABEL
873 || GET_CODE (p) == JUMP_INSN
874 || (GET_CODE (p) == NOTE
875 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
876 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
878 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
880 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
882 try_auto_increment (p, insn, 0, dst, newconst, 1);
891 if (reg_set_p (dst, PATTERN (p)))
894 /* If we have passed a call instruction, and the
895 pseudo-reg SRC is not already live across a call,
896 then don't perform the optimization. */
897 /* reg_set_p is overly conservative for CALL_INSNS, thinks that all
898 hard regs are clobbered. Thus, we only use it for src for
900 if (GET_CODE (p) == CALL_INSN)
905 if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
908 if (call_used_regs [REGNO (dst)]
909 || find_reg_fusage (p, CLOBBER, dst))
912 else if (reg_set_p (src, PATTERN (p)))
920 regmove_optimize (f, nregs, regmove_dump_file)
923 FILE *regmove_dump_file;
925 int old_max_uid = get_max_uid ();
930 rtx copy_src, copy_dst;
932 regno_src_regno = (int *)alloca (sizeof *regno_src_regno * nregs);
933 for (i = nregs; --i >= 0; ) regno_src_regno[i] = -1;
935 regmove_bb_head = (int *)alloca (sizeof (int) * (old_max_uid + 1));
936 for (i = old_max_uid; i >= 0; i--) regmove_bb_head[i] = -1;
937 for (i = 0; i < n_basic_blocks; i++)
938 regmove_bb_head[INSN_UID (basic_block_head[i])] = i;
940 /* A forward/backward pass. Replace output operands with input operands. */
944 for (pass = 0; pass <= 2; pass++)
946 if (! flag_regmove && pass >= flag_expensive_optimizations)
949 if (regmove_dump_file)
950 fprintf (regmove_dump_file, "Starting %s pass...\n",
951 pass ? "backward" : "forward");
953 for (insn = pass ? get_last_insn () : f; insn;
954 insn = pass ? PREV_INSN (insn) : NEXT_INSN (insn))
959 if (GET_CODE (insn) == NOTE)
961 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
963 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
967 set = single_set (insn);
971 if (flag_expensive_optimizations && ! pass
972 && (GET_CODE (SET_SRC (set)) == SIGN_EXTEND
973 || GET_CODE (SET_SRC (set)) == ZERO_EXTEND)
974 && GET_CODE (XEXP (SET_SRC (set), 0)) == REG
975 && GET_CODE (SET_DEST(set)) == REG)
976 optimize_reg_copy_3 (insn, SET_DEST (set), SET_SRC (set));
978 if (flag_expensive_optimizations && ! pass
979 && GET_CODE (SET_SRC (set)) == REG
980 && GET_CODE (SET_DEST(set)) == REG)
982 /* If this is a register-register copy where SRC is not dead,
983 see if we can optimize it. If this optimization succeeds,
984 it will become a copy where SRC is dead. */
985 if ((find_reg_note (insn, REG_DEAD, SET_SRC (set))
986 || optimize_reg_copy_1 (insn, SET_DEST (set), SET_SRC (set)))
987 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
989 /* Similarly for a pseudo-pseudo copy when SRC is dead. */
990 if (REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
991 optimize_reg_copy_2 (insn, SET_DEST (set), SET_SRC (set));
992 if (regno_src_regno[REGNO (SET_DEST (set))] < 0
993 && SET_SRC (set) != SET_DEST (set))
995 int srcregno = REGNO (SET_SRC(set));
996 if (regno_src_regno[srcregno] >= 0)
997 srcregno = regno_src_regno[srcregno];
998 regno_src_regno[REGNO (SET_DEST (set))] = srcregno;
1005 #ifdef REGISTER_CONSTRAINTS
1006 if (! find_matches (insn, &match))
1009 /* Now scan through the operands looking for a source operand
1010 which is supposed to match the destination operand.
1011 Then scan forward for an instruction which uses the dest
1013 If it dies there, then replace the dest in both operands with
1014 the source operand. */
1016 for (op_no = 0; op_no < recog_n_operands; op_no++)
1018 rtx src, dst, src_subreg;
1019 enum reg_class src_class, dst_class;
1021 match_no = match.with[op_no];
1023 /* Nothing to do if the two operands aren't supposed to match. */
1027 src = recog_operand[op_no];
1028 dst = recog_operand[match_no];
1030 if (GET_CODE (src) != REG)
1034 if (GET_CODE (dst) == SUBREG
1035 && GET_MODE_SIZE (GET_MODE (dst))
1036 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (dst))))
1039 = gen_rtx_SUBREG (GET_MODE (SUBREG_REG (dst)),
1040 src, SUBREG_WORD (dst));
1041 dst = SUBREG_REG (dst);
1043 if (GET_CODE (dst) != REG
1044 || REGNO (dst) < FIRST_PSEUDO_REGISTER)
1047 if (REGNO (src) < FIRST_PSEUDO_REGISTER)
1049 if (match.commutative[op_no] < op_no)
1050 regno_src_regno[REGNO (dst)] = REGNO (src);
1054 if (REG_LIVE_LENGTH (REGNO (src)) < 0)
1057 /* op_no/src must be a read-only operand, and
1058 match_operand/dst must be a write-only operand. */
1059 if (match.use[op_no] != READ
1060 || match.use[match_no] != WRITE)
1063 if (match.early_clobber[match_no]
1064 && count_occurrences (PATTERN (insn), src) > 1)
1067 /* Make sure match_operand is the destination. */
1068 if (recog_operand[match_no] != SET_DEST (set))
1071 /* If the operands already match, then there is nothing to do. */
1072 /* But in the commutative case, we might find a better match. */
1073 if (operands_match_p (src, dst)
1074 || (match.commutative[op_no] >= 0
1075 && operands_match_p (recog_operand[match.commutative
1077 && (replacement_quality (recog_operand[match.commutative
1079 >= replacement_quality (src))))
1082 src_class = reg_preferred_class (REGNO (src));
1083 dst_class = reg_preferred_class (REGNO (dst));
1084 if (! regclass_compatible_p (src_class, dst_class))
1087 if (fixup_match_1 (insn, set, src, src_subreg, dst, pass,
1095 /* A backward pass. Replace input operands with output operands. */
1097 if (regmove_dump_file)
1098 fprintf (regmove_dump_file, "Starting backward pass...\n");
1102 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1104 if (GET_CODE (insn) == NOTE)
1106 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
1108 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
1111 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1113 int op_no, match_no;
1116 if (! find_matches (insn, &match))
1119 /* Now scan through the operands looking for a destination operand
1120 which is supposed to match a source operand.
1121 Then scan backward for an instruction which sets the source
1122 operand. If safe, then replace the source operand with the
1123 dest operand in both instructions. */
1125 copy_src = NULL_RTX;
1126 copy_dst = NULL_RTX;
1127 for (op_no = 0; op_no < recog_n_operands; op_no++)
1129 rtx set, p, src, dst;
1130 rtx src_note, dst_note;
1132 enum reg_class src_class, dst_class;
1135 match_no = match.with[op_no];
1137 /* Nothing to do if the two operands aren't supposed to match. */
1141 dst = recog_operand[match_no];
1142 src = recog_operand[op_no];
1144 if (GET_CODE (src) != REG)
1147 if (GET_CODE (dst) != REG
1148 || REGNO (dst) < FIRST_PSEUDO_REGISTER
1149 || REG_LIVE_LENGTH (REGNO (dst)) < 0)
1152 /* If the operands already match, then there is nothing to do. */
1153 if (operands_match_p (src, dst)
1154 || (match.commutative[op_no] >= 0
1155 && operands_match_p (recog_operand[match.commutative[op_no]], dst)))
1158 set = single_set (insn);
1162 /* match_no/dst must be a write-only operand, and
1163 operand_operand/src must be a read-only operand. */
1164 if (match.use[op_no] != READ
1165 || match.use[match_no] != WRITE)
1168 if (match.early_clobber[match_no]
1169 && count_occurrences (PATTERN (insn), src) > 1)
1172 /* Make sure match_no is the destination. */
1173 if (recog_operand[match_no] != SET_DEST (set))
1176 if (REGNO (src) < FIRST_PSEUDO_REGISTER)
1178 if (GET_CODE (SET_SRC (set)) == PLUS
1179 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT
1180 && XEXP (SET_SRC (set), 0) == src
1181 && fixup_match_2 (insn, dst, src,
1182 XEXP (SET_SRC (set), 1),
1187 src_class = reg_preferred_class (REGNO (src));
1188 dst_class = reg_preferred_class (REGNO (dst));
1189 if (! regclass_compatible_p (src_class, dst_class))
1199 /* Can not modify an earlier insn to set dst if this insn
1200 uses an old value in the source. */
1201 if (reg_overlap_mentioned_p (dst, SET_SRC (set)))
1211 if (! (src_note = find_reg_note (insn, REG_DEAD, src)))
1222 /* If src is set once in a different basic block,
1223 and is set equal to a constant, then do not use
1224 it for this optimization, as this would make it
1225 no longer equivalent to a constant. */
1227 if (reg_is_remote_constant_p (src, insn, f))
1238 if (regmove_dump_file)
1239 fprintf (regmove_dump_file,
1240 "Could fix operand %d of insn %d matching operand %d.\n",
1241 op_no, INSN_UID (insn), match_no);
1243 /* Scan backward to find the first instruction that uses
1244 the input operand. If the operand is set here, then
1245 replace it in both instructions with match_no. */
1247 for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
1251 if (GET_CODE (p) == CODE_LABEL
1252 || GET_CODE (p) == JUMP_INSN
1253 || (GET_CODE (p) == NOTE
1254 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
1255 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
1258 /* ??? We can't scan past the end of a basic block without
1259 updating the register lifetime info
1260 (REG_DEAD/basic_block_live_at_start).
1261 A CALL_INSN might be the last insn of a basic block, if
1262 it is inside an EH region. There is no easy way to tell,
1263 so we just always break when we see a CALL_INSN if
1264 flag_exceptions is nonzero. */
1265 if (flag_exceptions && GET_CODE (p) == CALL_INSN)
1268 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
1273 /* ??? See if all of SRC is set in P. This test is much
1274 more conservative than it needs to be. */
1275 pset = single_set (p);
1276 if (pset && SET_DEST (pset) == src)
1278 /* We use validate_replace_rtx, in case there
1279 are multiple identical source operands. All of
1280 them have to be changed at the same time. */
1281 if (validate_replace_rtx (src, dst, insn))
1283 if (validate_change (p, &SET_DEST (pset),
1288 /* Change all source operands back.
1289 This modifies the dst as a side-effect. */
1290 validate_replace_rtx (dst, src, insn);
1291 /* Now make sure the dst is right. */
1292 validate_change (insn,
1293 recog_operand_loc[match_no],
1300 if (reg_overlap_mentioned_p (src, PATTERN (p))
1301 || reg_overlap_mentioned_p (dst, PATTERN (p)))
1304 /* If we have passed a call instruction, and the
1305 pseudo-reg DST is not already live across a call,
1306 then don't perform the optimization. */
1307 if (GET_CODE (p) == CALL_INSN)
1311 if (REG_N_CALLS_CROSSED (REGNO (dst)) == 0)
1320 /* Remove the death note for SRC from INSN. */
1321 remove_note (insn, src_note);
1322 /* Move the death note for SRC to P if it is used
1324 if (reg_overlap_mentioned_p (src, PATTERN (p)))
1326 XEXP (src_note, 1) = REG_NOTES (p);
1327 REG_NOTES (p) = src_note;
1329 /* If there is a REG_DEAD note for DST on P, then remove
1330 it, because DST is now set there. */
1331 if ((dst_note = find_reg_note (p, REG_DEAD, dst)))
1332 remove_note (p, dst_note);
1334 dstno = REGNO (dst);
1335 srcno = REGNO (src);
1337 REG_N_SETS (dstno)++;
1338 REG_N_SETS (srcno)--;
1340 REG_N_CALLS_CROSSED (dstno) += num_calls;
1341 REG_N_CALLS_CROSSED (srcno) -= num_calls;
1343 REG_LIVE_LENGTH (dstno) += length;
1344 if (REG_LIVE_LENGTH (srcno) >= 0)
1346 REG_LIVE_LENGTH (srcno) -= length;
1347 /* REG_LIVE_LENGTH is only an approximation after
1348 combine if sched is not run, so make sure that we
1349 still have a reasonable value. */
1350 if (REG_LIVE_LENGTH (srcno) < 2)
1351 REG_LIVE_LENGTH (srcno) = 2;
1354 /* We assume that a register is used exactly once per
1355 insn in the updates above. If this is not correct,
1356 no great harm is done. */
1358 REG_N_REFS (dstno) += 2 * loop_depth;
1359 REG_N_REFS (srcno) -= 2 * loop_depth;
1361 /* If that was the only time src was set,
1362 and src was not live at the start of the
1363 function, we know that we have no more
1364 references to src; clear REG_N_REFS so it
1365 won't make reload do any work. */
1366 if (REG_N_SETS (REGNO (src)) == 0
1367 && ! regno_uninitialized (REGNO (src)))
1368 REG_N_REFS (REGNO (src)) = 0;
1370 if (regmove_dump_file)
1371 fprintf (regmove_dump_file,
1372 "Fixed operand %d of insn %d matching operand %d.\n",
1373 op_no, INSN_UID (insn), match_no);
1379 /* If we weren't able to replace any of the alternatives, try an
1380 alternative appoach of copying the source to the destination. */
1381 if (!success && copy_src != NULL_RTX)
1382 copy_src_to_dest (insn, copy_src, copy_dst, loop_depth);
1386 #endif /* REGISTER_CONSTRAINTS */
1388 /* In fixup_match_1, some insns may have been inserted after basic block
1389 ends. Fix that here. */
1390 for (i = 0; i < n_basic_blocks; i++)
1392 rtx end = basic_block_end[i];
1394 rtx next = NEXT_INSN (new);
1395 while (next != 0 && INSN_UID (next) >= old_max_uid
1396 && (i == n_basic_blocks - 1 || basic_block_head[i + 1] != next))
1397 new = next, next = NEXT_INSN (new);
1398 basic_block_end[i] = new;
1402 /* Returns nonzero if INSN's pattern has matching constraints for any operand.
1403 Returns 0 if INSN can't be recognized, or if the alternative can't be
1406 Initialize the info in MATCHP based on the constraints. */
1409 find_matches (insn, matchp)
1411 struct match *matchp;
1413 int likely_spilled[MAX_RECOG_OPERANDS];
1415 int any_matches = 0;
1417 extract_insn (insn);
1418 if (! constrain_operands (0))
1421 /* Must initialize this before main loop, because the code for
1422 the commutative case may set matches for operands other than
1424 for (op_no = recog_n_operands; --op_no >= 0; )
1425 matchp->with[op_no] = matchp->commutative[op_no] = -1;
1427 for (op_no = 0; op_no < recog_n_operands; op_no++)
1432 p = recog_constraints[op_no];
1434 likely_spilled[op_no] = 0;
1435 matchp->use[op_no] = READ;
1436 matchp->early_clobber[op_no] = 0;
1438 matchp->use[op_no] = WRITE;
1440 matchp->use[op_no] = READWRITE;
1442 for (;*p && i < which_alternative; p++)
1446 while ((c = *p++) != '\0' && c != ',')
1454 matchp->early_clobber[op_no] = 1;
1457 matchp->commutative[op_no] = op_no + 1;
1458 matchp->commutative[op_no + 1] = op_no;
1460 case '0': case '1': case '2': case '3': case '4':
1461 case '5': case '6': case '7': case '8': case '9':
1463 if (c < op_no && likely_spilled[(unsigned char) c])
1465 matchp->with[op_no] = c;
1467 if (matchp->commutative[op_no] >= 0)
1468 matchp->with[matchp->commutative[op_no]] = c;
1470 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'h':
1471 case 'j': case 'k': case 'l': case 'p': case 'q': case 't': case 'u':
1472 case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B':
1473 case 'C': case 'D': case 'W': case 'Y': case 'Z':
1474 if (CLASS_LIKELY_SPILLED_P (REG_CLASS_FROM_LETTER ((unsigned char)c)))
1475 likely_spilled[op_no] = 1;
1482 /* Try to replace output operand DST in SET, with input operand SRC. SET is
1483 the only set in INSN. INSN has just been recgnized and constrained.
1484 SRC is operand number OPERAND_NUMBER in INSN.
1485 DST is operand number MATCH_NUMBER in INSN.
1486 If BACKWARD is nonzero, we have been called in a backward pass.
1487 Return nonzero for success. */
1489 fixup_match_1 (insn, set, src, src_subreg, dst, backward, operand_number,
1490 match_number, regmove_dump_file)
1491 rtx insn, set, src, src_subreg, dst;
1492 int backward, operand_number, match_number;
1493 FILE *regmove_dump_file;
1496 rtx post_inc = 0, post_inc_set = 0, search_end = 0;
1498 int num_calls = 0, s_num_calls = 0;
1499 enum rtx_code code = NOTE;
1500 HOST_WIDE_INT insn_const, newconst;
1501 rtx overlap = 0; /* need to move insn ? */
1502 rtx src_note = find_reg_note (insn, REG_DEAD, src), dst_note;
1503 int length, s_length, true_loop_depth;
1507 /* Look for (set (regX) (op regA constX))
1508 (set (regY) (op regA constY))
1510 (set (regA) (op regA constX)).
1511 (set (regY) (op regA constY-constX)).
1512 This works for add and shift operations, if
1513 regA is dead after or set by the second insn. */
1515 code = GET_CODE (SET_SRC (set));
1516 if ((code == PLUS || code == LSHIFTRT
1517 || code == ASHIFT || code == ASHIFTRT)
1518 && XEXP (SET_SRC (set), 0) == src
1519 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
1520 insn_const = INTVAL (XEXP (SET_SRC (set), 1));
1521 else if (! stable_but_for_p (SET_SRC (set), src, dst))
1524 /* We might find a src_note while scanning. */
1528 if (regmove_dump_file)
1529 fprintf (regmove_dump_file,
1530 "Could fix operand %d of insn %d matching operand %d.\n",
1531 operand_number, INSN_UID (insn), match_number);
1533 /* If SRC is equivalent to a constant set in a different basic block,
1534 then do not use it for this optimization. We want the equivalence
1535 so that if we have to reload this register, we can reload the
1536 constant, rather than extending the lifespan of the register. */
1537 if (reg_is_remote_constant_p (src, insn, get_insns ()))
1540 /* Scan forward to find the next instruction that
1541 uses the output operand. If the operand dies here,
1542 then replace it in both instructions with
1545 for (length = s_length = 0, p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
1547 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
1548 || (GET_CODE (p) == NOTE
1549 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
1550 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
1553 /* ??? We can't scan past the end of a basic block without updating
1554 the register lifetime info (REG_DEAD/basic_block_live_at_start).
1555 A CALL_INSN might be the last insn of a basic block, if it is
1556 inside an EH region. There is no easy way to tell, so we just
1557 always break when we see a CALL_INSN if flag_exceptions is nonzero. */
1558 if (flag_exceptions && GET_CODE (p) == CALL_INSN)
1561 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
1568 if (reg_set_p (src, p) || reg_set_p (dst, p)
1569 || (GET_CODE (PATTERN (p)) == USE
1570 && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
1573 /* See if all of DST dies in P. This test is
1574 slightly more conservative than it needs to be. */
1575 if ((dst_note = find_regno_note (p, REG_DEAD, REGNO (dst)))
1576 && (GET_MODE (XEXP (dst_note, 0)) == GET_MODE (dst)))
1583 /* If an optimization is done, the value of SRC while P
1584 is executed will be changed. Check that this is OK. */
1585 if (reg_overlap_mentioned_p (src, PATTERN (p)))
1587 for (q = p; q; q = NEXT_INSN (q))
1589 if (GET_CODE (q) == CODE_LABEL || GET_CODE (q) == JUMP_INSN
1590 || (GET_CODE (q) == NOTE
1591 && (NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_BEG
1592 || NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_END)))
1598 /* ??? We can't scan past the end of a basic block without
1599 updating the register lifetime info
1600 (REG_DEAD/basic_block_live_at_start).
1601 A CALL_INSN might be the last insn of a basic block, if
1602 it is inside an EH region. There is no easy way to tell,
1603 so we just always break when we see a CALL_INSN if
1604 flag_exceptions is nonzero. */
1605 if (flag_exceptions && GET_CODE (q) == CALL_INSN)
1611 if (GET_RTX_CLASS (GET_CODE (q)) != 'i')
1613 if (reg_overlap_mentioned_p (src, PATTERN (q))
1614 || reg_set_p (src, q))
1618 set2 = single_set (q);
1619 if (! q || ! set2 || GET_CODE (SET_SRC (set2)) != code
1620 || XEXP (SET_SRC (set2), 0) != src
1621 || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT
1622 || (SET_DEST (set2) != src
1623 && ! find_reg_note (q, REG_DEAD, src)))
1625 /* If this is a PLUS, we can still save a register by doing
1628 src -= insn_const; .
1629 This also gives opportunities for subsequent
1630 optimizations in the backward pass, so do it there. */
1631 if (code == PLUS && backward
1632 /* Don't do this if we can likely tie DST to SET_DEST
1633 of P later; we can't do this tying here if we got a
1635 && ! (dst_note && ! REG_N_CALLS_CROSSED (REGNO (dst))
1637 && GET_CODE (SET_DEST (single_set (p))) == REG
1638 && (REGNO (SET_DEST (single_set (p)))
1639 < FIRST_PSEUDO_REGISTER))
1641 /* We may not emit an insn directly
1642 after P if the latter sets CC0. */
1643 && ! sets_cc0_p (PATTERN (p))
1651 newconst = -insn_const;
1659 newconst = INTVAL (XEXP (SET_SRC (set2), 1)) - insn_const;
1660 /* Reject out of range shifts. */
1664 >= GET_MODE_BITSIZE (GET_MODE (SET_SRC (set2))))))
1669 if (SET_DEST (set2) != src)
1670 post_inc_set = set2;
1673 /* We use 1 as last argument to validate_change so that all
1674 changes are accepted or rejected together by apply_change_group
1675 when it is called by validate_replace_rtx . */
1676 validate_change (q, &XEXP (SET_SRC (set2), 1),
1677 GEN_INT (newconst), 1);
1679 validate_change (insn, recog_operand_loc[match_number], src, 1);
1680 if (validate_replace_rtx (dst, src_subreg, p))
1685 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
1687 if (! src_note && reg_overlap_mentioned_p (src, PATTERN (p)))
1689 /* INSN was already checked to be movable when
1690 we found no REG_DEAD note for src on it. */
1692 src_note = find_reg_note (p, REG_DEAD, src);
1695 /* If we have passed a call instruction, and the pseudo-reg SRC is not
1696 already live across a call, then don't perform the optimization. */
1697 if (GET_CODE (p) == CALL_INSN)
1699 if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
1713 true_loop_depth = backward ? 2 - loop_depth : loop_depth;
1715 /* Remove the death note for DST from P. */
1716 remove_note (p, dst_note);
1719 post_inc = emit_insn_after (copy_rtx (PATTERN (insn)), p);
1720 if ((HAVE_PRE_INCREMENT || HAVE_PRE_DECREMENT)
1722 && try_auto_increment (search_end, post_inc, 0, src, newconst, 1))
1724 validate_change (insn, &XEXP (SET_SRC (set), 1), GEN_INT (insn_const), 0);
1725 REG_N_SETS (REGNO (src))++;
1726 REG_N_REFS (REGNO (src)) += true_loop_depth;
1727 REG_LIVE_LENGTH (REGNO (src))++;
1731 /* The lifetime of src and dest overlap,
1732 but we can change this by moving insn. */
1733 rtx pat = PATTERN (insn);
1735 remove_note (overlap, src_note);
1736 #if defined (HAVE_POST_INCREMENT) || defined (HAVE_POST_DECREMENT)
1738 && try_auto_increment (overlap, insn, 0, src, insn_const, 0))
1743 rtx notes = REG_NOTES (insn);
1745 emit_insn_after_with_line_notes (pat, PREV_INSN (p), insn);
1746 PUT_CODE (insn, NOTE);
1747 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1748 NOTE_SOURCE_FILE (insn) = 0;
1749 /* emit_insn_after_with_line_notes has no
1750 return value, so search for the new insn. */
1751 for (insn = p; PATTERN (insn) != pat; )
1752 insn = PREV_INSN (insn);
1754 REG_NOTES (insn) = notes;
1757 /* Sometimes we'd generate src = const; src += n;
1758 if so, replace the instruction that set src
1759 in the first place. */
1761 if (! overlap && (code == PLUS || code == MINUS))
1763 rtx note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
1765 int num_calls2 = 0, s_length2 = 0;
1767 if (note && CONSTANT_P (XEXP (note, 0)))
1769 for (q = PREV_INSN (insn); q; q = PREV_INSN(q))
1771 if (GET_CODE (q) == CODE_LABEL || GET_CODE (q) == JUMP_INSN
1772 || (GET_CODE (q) == NOTE
1773 && (NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_BEG
1774 || NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_END)))
1780 /* ??? We can't scan past the end of a basic block without
1781 updating the register lifetime info
1782 (REG_DEAD/basic_block_live_at_start).
1783 A CALL_INSN might be the last insn of a basic block, if
1784 it is inside an EH region. There is no easy way to tell,
1785 so we just always break when we see a CALL_INSN if
1786 flag_exceptions is nonzero. */
1787 if (flag_exceptions && GET_CODE (q) == CALL_INSN)
1793 if (GET_RTX_CLASS (GET_CODE (q)) != 'i')
1796 if (reg_set_p (src, q))
1798 set2 = single_set (q);
1801 if (reg_overlap_mentioned_p (src, PATTERN (q)))
1806 if (GET_CODE (p) == CALL_INSN)
1809 if (q && set2 && SET_DEST (set2) == src && CONSTANT_P (SET_SRC (set2))
1810 && validate_change (insn, &SET_SRC (set), XEXP (note, 0), 0))
1813 NOTE_LINE_NUMBER (q) = NOTE_INSN_DELETED;
1814 NOTE_SOURCE_FILE (q) = 0;
1815 REG_N_SETS (REGNO (src))--;
1816 REG_N_CALLS_CROSSED (REGNO (src)) -= num_calls2;
1817 REG_N_REFS (REGNO (src)) -= true_loop_depth;
1818 REG_LIVE_LENGTH (REGNO (src)) -= s_length2;
1824 /* Don't remove this seemingly useless if, it is needed to pair with the
1825 else in the next two conditionally included code blocks. */
1828 else if ((HAVE_PRE_INCREMENT || HAVE_PRE_DECREMENT)
1829 && (code == PLUS || code == MINUS) && insn_const
1830 && try_auto_increment (p, insn, 0, src, insn_const, 1))
1832 else if ((HAVE_POST_INCREMENT || HAVE_POST_DECREMENT)
1834 && try_auto_increment (p, post_inc, post_inc_set, src, newconst, 0))
1836 /* If post_inc still prevails, try to find an
1837 insn where it can be used as a pre-in/decrement.
1838 If code is MINUS, this was already tried. */
1839 if (post_inc && code == PLUS
1840 /* Check that newconst is likely to be usable
1841 in a pre-in/decrement before starting the search. */
1842 && ((HAVE_PRE_INCREMENT && newconst > 0 && newconst <= MOVE_MAX)
1843 || (HAVE_PRE_DECREMENT && newconst < 0 && newconst >= -MOVE_MAX))
1844 && exact_log2 (newconst))
1848 inc_dest = post_inc_set ? SET_DEST (post_inc_set) : src;
1849 for (q = post_inc; (q = NEXT_INSN (q)); )
1851 if (GET_CODE (q) == CODE_LABEL || GET_CODE (q) == JUMP_INSN
1852 || (GET_CODE (q) == NOTE
1853 && (NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_BEG
1854 || NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_END)))
1857 /* ??? We can't scan past the end of a basic block without updating
1858 the register lifetime info (REG_DEAD/basic_block_live_at_start).
1859 A CALL_INSN might be the last insn of a basic block, if it
1860 is inside an EH region. There is no easy way to tell so we
1861 just always break when we see a CALL_INSN if flag_exceptions
1863 if (flag_exceptions && GET_CODE (q) == CALL_INSN)
1866 if (GET_RTX_CLASS (GET_CODE (q)) != 'i')
1868 if (src != inc_dest && (reg_overlap_mentioned_p (src, PATTERN (q))
1869 || reg_set_p (src, q)))
1871 if (reg_set_p (inc_dest, q))
1873 if (reg_overlap_mentioned_p (inc_dest, PATTERN (q)))
1875 try_auto_increment (q, post_inc,
1876 post_inc_set, inc_dest, newconst, 1);
1881 /* Move the death note for DST to INSN if it is used
1883 if (reg_overlap_mentioned_p (dst, PATTERN (insn)))
1885 XEXP (dst_note, 1) = REG_NOTES (insn);
1886 REG_NOTES (insn) = dst_note;
1891 /* Move the death note for SRC from INSN to P. */
1893 remove_note (insn, src_note);
1894 XEXP (src_note, 1) = REG_NOTES (p);
1895 REG_NOTES (p) = src_note;
1897 REG_N_CALLS_CROSSED (REGNO (src)) += s_num_calls;
1900 REG_N_SETS (REGNO (src))++;
1901 REG_N_SETS (REGNO (dst))--;
1903 REG_N_CALLS_CROSSED (REGNO (dst)) -= num_calls;
1905 REG_LIVE_LENGTH (REGNO (src)) += s_length;
1906 if (REG_LIVE_LENGTH (REGNO (dst)) >= 0)
1908 REG_LIVE_LENGTH (REGNO (dst)) -= length;
1909 /* REG_LIVE_LENGTH is only an approximation after
1910 combine if sched is not run, so make sure that we
1911 still have a reasonable value. */
1912 if (REG_LIVE_LENGTH (REGNO (dst)) < 2)
1913 REG_LIVE_LENGTH (REGNO (dst)) = 2;
1916 /* We assume that a register is used exactly once per
1917 insn in the updates above. If this is not correct,
1918 no great harm is done. */
1920 REG_N_REFS (REGNO (src)) += 2 * true_loop_depth;
1921 REG_N_REFS (REGNO (dst)) -= 2 * true_loop_depth;
1923 /* If that was the only time dst was set,
1924 and dst was not live at the start of the
1925 function, we know that we have no more
1926 references to dst; clear REG_N_REFS so it
1927 won't make reload do any work. */
1928 if (REG_N_SETS (REGNO (dst)) == 0
1929 && ! regno_uninitialized (REGNO (dst)))
1930 REG_N_REFS (REGNO (dst)) = 0;
1932 if (regmove_dump_file)
1933 fprintf (regmove_dump_file,
1934 "Fixed operand %d of insn %d matching operand %d.\n",
1935 operand_number, INSN_UID (insn), match_number);
1940 /* return nonzero if X is stable but for mentioning SRC or mentioning /
1941 changing DST . If in doubt, presume it is unstable. */
1943 stable_but_for_p (x, src, dst)
1946 RTX_CODE code = GET_CODE (x);
1947 switch (GET_RTX_CLASS (code))
1949 case '<': case '1': case 'c': case '2': case 'b': case '3':
1952 char *fmt = GET_RTX_FORMAT (code);
1953 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1954 if (fmt[i] == 'e' && ! stable_but_for_p (XEXP (x, i), src, dst))
1959 if (x == src || x == dst)
1963 return ! rtx_unstable_p (x);
1967 /* Test if regmove seems profitable for this target. Regmove is useful only
1968 if some common patterns are two address, i.e. require matching constraints,
1969 so we check that condition here. */
1972 regmove_profitable_p ()
1974 #ifdef REGISTER_CONSTRAINTS
1976 enum machine_mode mode;
1977 optab tstoptab = add_optab;
1978 do /* check add_optab and ashl_optab */
1979 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1980 mode = GET_MODE_WIDER_MODE (mode))
1982 int icode = (int) tstoptab->handlers[(int) mode].insn_code;
1983 rtx reg0, reg1, reg2, pat;
1986 if (GET_MODE_BITSIZE (mode) < 32 || icode == CODE_FOR_nothing)
1988 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1989 if (TEST_HARD_REG_BIT (reg_class_contents[GENERAL_REGS], i))
1991 if (i + 2 >= FIRST_PSEUDO_REGISTER)
1993 reg0 = gen_rtx_REG (insn_operand_mode[icode][0], i);
1994 reg1 = gen_rtx_REG (insn_operand_mode[icode][1], i + 1);
1995 reg2 = gen_rtx_REG (insn_operand_mode[icode][2], i + 2);
1996 if (! (*insn_operand_predicate[icode][0]) (reg0, VOIDmode)
1997 || ! (*insn_operand_predicate[icode][1]) (reg1, VOIDmode)
1998 || ! (*insn_operand_predicate[icode][2]) (reg2, VOIDmode))
2000 pat = GEN_FCN (icode) (reg0, reg1, reg2);
2003 if (GET_CODE (pat) == SEQUENCE)
2004 pat = XVECEXP (pat, 0, XVECLEN (pat, 0) - 1);
2006 pat = make_insn_raw (pat);
2007 if (! single_set (pat)
2008 || GET_CODE (SET_SRC (single_set (pat))) != tstoptab->code)
2009 /* Unexpected complexity; don't need to handle this unless
2010 we find a machine where this occurs and regmove should
2013 if (find_matches (pat, &match))
2017 while (tstoptab != ashl_optab && (tstoptab = ashl_optab, 1));
2018 #endif /* REGISTER_CONSTRAINTS */