1 /* Move registers around to reduce number of move instructions needed.
2 Copyright (C) 1987, 88, 89, 92-97, 1998 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* This module looks for cases where matching constraints would force
22 an instruction to need a reload, and this reload would be a register
23 to register move. It then attempts to change the registers used by the
24 instruction to avoid the move instruction. */
33 /* Must precede rtl.h for FFS. */
37 #include "insn-config.h"
42 #include "hard-reg-set.h"
45 #include "insn-flags.h"
47 #ifndef REG_N_CALLS_CROSSED
48 #define REG_N_CALLS_CROSSED(x) (reg_n_calls_crossed[(x)])
49 #define REG_N_SETS(x) (reg_n_sets[(x)])
50 #define REG_N_REFS(x) (reg_n_refs[(x)])
51 #define REG_N_DEATHS(x) (reg_n_deaths[(x)])
52 #define REG_LIVE_LENGTH(x) (reg_live_length[(x)])
55 static void optimize_reg_copy_3 PROTO((rtx, rtx, rtx));
57 extern int flag_regmove;
60 int with[MAX_RECOG_OPERANDS];
61 enum { READ, WRITE, READWRITE } use[MAX_RECOG_OPERANDS];
62 int commutative[MAX_RECOG_OPERANDS];
63 int early_clobber[MAX_RECOG_OPERANDS];
66 static int find_matches PROTO((rtx, struct match *));
67 static int fixup_match_1 PROTO((rtx, rtx, rtx, rtx, rtx, int, int, int, FILE *))
69 static int reg_is_remote_constant_p PROTO((rtx, rtx, rtx));
70 static int stable_but_for_p PROTO((rtx, rtx, rtx));
71 static int loop_depth;
75 /* INC_INSN is an instruction that adds INCREMENT to REG.
76 Try to fold INC_INSN as a post/pre in/decrement into INSN.
77 Iff INC_INSN_SET is nonzero, inc_insn has a destination different from src.
78 Return nonzero for success. */
80 try_auto_increment (insn, inc_insn, inc_insn_set, reg, increment, pre)
81 rtx reg, insn, inc_insn ,inc_insn_set;
82 HOST_WIDE_INT increment;
85 enum rtx_code inc_code;
87 rtx pset = single_set (insn);
90 /* Can't use the size of SET_SRC, we might have something like
91 (sign_extend:SI (mem:QI ... */
92 rtx use = find_use_as_address (pset, reg, 0);
93 if (use != 0 && use != (rtx) 1)
95 int size = GET_MODE_SIZE (GET_MODE (use));
97 #ifdef HAVE_POST_INCREMENT
98 || (pre == 0 && (inc_code = POST_INC, increment == size))
100 #ifdef HAVE_PRE_INCREMENT
101 || (pre == 1 && (inc_code = PRE_INC, increment == size))
103 #ifdef HAVE_POST_DECREMENT
104 || (pre == 0 && (inc_code = POST_DEC, increment == -size))
106 #ifdef HAVE_PRE_DECREMENT
107 || (pre == 1 && (inc_code = PRE_DEC, increment == -size))
114 &SET_SRC (inc_insn_set),
115 XEXP (SET_SRC (inc_insn_set), 0), 1);
116 validate_change (insn, &XEXP (use, 0),
117 gen_rtx_fmt_e (inc_code, Pmode, reg), 1);
118 if (apply_change_group ())
121 = gen_rtx_EXPR_LIST (REG_INC,
122 reg, REG_NOTES (insn));
125 PUT_CODE (inc_insn, NOTE);
126 NOTE_LINE_NUMBER (inc_insn) = NOTE_INSN_DELETED;
127 NOTE_SOURCE_FILE (inc_insn) = 0;
136 #endif /* AUTO_INC_DEC */
138 static int *regno_src_regno;
140 /* Indicate how good a choice REG (which appears as a source) is to replace
141 a destination register with. The higher the returned value, the better
142 the choice. The main objective is to avoid using a register that is
143 a candidate for tying to a hard register, since the output might in
144 turn be a candidate to be tied to a different hard register. */
146 replacement_quality(reg)
151 /* Bad if this isn't a register at all. */
152 if (GET_CODE (reg) != REG)
155 /* If this register is not meant to get a hard register,
156 it is a poor choice. */
157 if (REG_LIVE_LENGTH (REGNO (reg)) < 0)
160 src_regno = regno_src_regno[REGNO (reg)];
162 /* If it was not copied from another register, it is fine. */
166 /* Copied from a hard register? */
167 if (src_regno < FIRST_PSEUDO_REGISTER)
170 /* Copied from a pseudo register - not as bad as from a hard register,
171 yet still cumbersome, since the register live length will be lengthened
172 when the registers get tied. */
176 /* INSN is a ZERO_EXTEND or SIGN_EXTEND of SRC to DEST.
177 Look if SRC dies there, and if it is only set once, by loading
178 it from memory. If so, try to encorporate the zero/sign extension
179 into the memory read, change SRC to the mode of DEST, and alter
180 the remaining accesses to use the appropriate SUBREG. This allows
181 SRC and DEST to be tied later. */
183 optimize_reg_copy_3 (insn, dest, src)
188 rtx src_reg = XEXP (src, 0);
189 int src_no = REGNO (src_reg);
190 int dst_no = REGNO (dest);
192 enum machine_mode old_mode;
194 if (src_no < FIRST_PSEUDO_REGISTER
195 || dst_no < FIRST_PSEUDO_REGISTER
196 || ! find_reg_note (insn, REG_DEAD, src_reg)
197 || REG_N_SETS (src_no) != 1)
199 for (p = PREV_INSN (insn); ! reg_set_p (src_reg, p); p = PREV_INSN (p))
201 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
203 if (GET_CODE (p) == JUMP_INSN)
206 if (! (set = single_set (p))
207 || GET_CODE (SET_SRC (set)) != MEM
208 || SET_DEST (set) != src_reg)
210 old_mode = GET_MODE (src_reg);
211 PUT_MODE (src_reg, GET_MODE (src));
212 XEXP (src, 0) = SET_SRC (set);
213 if (! validate_change (p, &SET_SRC (set), src, 0))
215 PUT_MODE (src_reg, old_mode);
216 XEXP (src, 0) = src_reg;
219 subreg = gen_rtx_SUBREG (old_mode, src_reg, 0);
220 while (p = NEXT_INSN (p), p != insn)
222 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
224 validate_replace_rtx (src_reg, subreg, p);
226 validate_replace_rtx (src, src_reg, insn);
229 /* Return whether REG is set in only one location, and is set to a
230 constant, but is set in a different basic block from INSN (an
231 instructions which uses REG). In this case REG is equivalent to a
232 constant, and we don't want to break that equivalence, because that
233 may increase register pressure and make reload harder. If REG is
234 set in the same basic block as INSN, we don't worry about it,
235 because we'll probably need a register anyhow (??? but what if REG
236 is used in a different basic block as well as this one?). FIRST is
237 the first insn in the function. */
240 reg_is_remote_constant_p (reg, insn, first)
247 if (REG_N_SETS (REGNO (reg)) != 1)
250 /* Look for the set. */
251 for (p = LOG_LINKS (insn); p; p = XEXP (p, 1))
255 if (REG_NOTE_KIND (p) != 0)
257 s = single_set (XEXP (p, 0));
259 && GET_CODE (SET_DEST (s)) == REG
260 && REGNO (SET_DEST (s)) == REGNO (reg))
262 /* The register is set in the same basic block. */
267 for (p = first; p && p != insn; p = NEXT_INSN (p))
271 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
275 && GET_CODE (SET_DEST (s)) == REG
276 && REGNO (SET_DEST (s)) == REGNO (reg))
278 /* This is the instruction which sets REG. If there is a
279 REG_EQUAL note, then REG is equivalent to a constant. */
280 if (find_reg_note (p, REG_EQUAL, NULL_RTX))
289 /* cse disrupts preincrement / postdecrement squences when it finds a
290 hard register as ultimate source, like the frame pointer. */
291 int fixup_match_2 (insn, dst, src, offset, regmove_dump_file)
292 rtx insn, dst, src, offset;
293 FILE *regmove_dump_file;
295 rtx p, dst_death = 0;
296 int length, num_calls = 0;
298 /* If SRC dies in INSN, we'd have to move the death note. This is
299 considered to be very unlikely, so we just skip the optimization
301 if (find_regno_note (insn, REG_DEAD, REGNO (src)))
304 /* Scan backward to find the first instruction that sets DST. */
306 for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
310 if (GET_CODE (p) == CODE_LABEL
311 || GET_CODE (p) == JUMP_INSN
312 || (GET_CODE (p) == NOTE
313 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
314 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
317 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
320 if (find_regno_note (p, REG_DEAD, REGNO (dst)))
325 pset = single_set (p);
326 if (pset && SET_DEST (pset) == dst
327 && GET_CODE (SET_SRC (pset)) == PLUS
328 && XEXP (SET_SRC (pset), 0) == src
329 && GET_CODE (XEXP (SET_SRC (pset), 1)) == CONST_INT)
331 HOST_WIDE_INT newconst
332 = INTVAL (offset) - INTVAL (XEXP (SET_SRC (pset), 1));
333 if (validate_change (insn, &PATTERN (insn),
334 gen_addsi3 (dst, dst, GEN_INT (newconst)), 0))
336 /* Remove the death note for DST from DST_DEATH. */
339 remove_death (REGNO (dst), dst_death);
340 REG_LIVE_LENGTH (REGNO (dst)) += length;
341 REG_N_CALLS_CROSSED (REGNO (dst)) += num_calls;
344 REG_N_REFS (REGNO (dst)) += loop_depth;
345 REG_N_REFS (REGNO (src)) -= loop_depth;
347 if (regmove_dump_file)
348 fprintf (regmove_dump_file,
349 "Fixed operand of insn %d.\n",
353 for (p = PREV_INSN (insn); p; p = PREV_INSN (p))
355 if (GET_CODE (p) == CODE_LABEL
356 || GET_CODE (p) == JUMP_INSN
357 || (GET_CODE (p) == NOTE
358 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
359 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
361 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
363 if (try_auto_increment (p, insn, 0, dst, newconst, 0))
368 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
370 if (GET_CODE (p) == CODE_LABEL
371 || GET_CODE (p) == JUMP_INSN
372 || (GET_CODE (p) == NOTE
373 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
374 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
376 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
378 try_auto_increment (p, insn, 0, dst, newconst, 1);
387 if (reg_set_p (dst, PATTERN (p)))
390 /* If we have passed a call instruction, and the
391 pseudo-reg SRC is not already live across a call,
392 then don't perform the optimization. */
393 /* reg_set_p is overly conservative for CALL_INSNS, thinks that all
394 hard regs are clobbered. Thus, we only use it for src for
396 if (GET_CODE (p) == CALL_INSN)
401 if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
404 if (call_used_regs [REGNO (dst)]
405 || find_reg_fusage (p, CLOBBER, dst))
408 else if (reg_set_p (src, PATTERN (p)))
416 regmove_optimize (f, nregs, regmove_dump_file)
419 FILE *regmove_dump_file;
424 int maxregnum = max_reg_num (), i;
426 regno_src_regno = (int *)alloca (sizeof *regno_src_regno * maxregnum);
427 for (i = maxregnum; --i >= 0; ) regno_src_regno[i] = -1;
429 /* A forward/backward pass. Replace output operands with input operands. */
433 for (pass = 0; pass <= 2; pass++)
435 if (! flag_regmove && pass >= flag_expensive_optimizations)
438 if (regmove_dump_file)
439 fprintf (regmove_dump_file, "Starting %s pass...\n",
440 pass ? "backward" : "forward");
442 for (insn = pass ? get_last_insn () : f; insn;
443 insn = pass ? PREV_INSN (insn) : NEXT_INSN (insn))
446 int insn_code_number;
447 int operand_number, match_number;
449 if (GET_CODE (insn) == NOTE)
451 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
453 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
457 set = single_set (insn);
461 if (flag_expensive_optimizations && ! pass
462 && (GET_CODE (SET_SRC (set)) == SIGN_EXTEND
463 || GET_CODE (SET_SRC (set)) == ZERO_EXTEND)
464 && GET_CODE (XEXP (SET_SRC (set), 0)) == REG
465 && GET_CODE (SET_DEST(set)) == REG)
466 optimize_reg_copy_3 (insn, SET_DEST (set), SET_SRC (set));
468 if (flag_expensive_optimizations && ! pass
469 && GET_CODE (SET_SRC (set)) == REG
470 && GET_CODE (SET_DEST(set)) == REG)
472 /* If this is a register-register copy where SRC is not dead,
473 see if we can optimize it. If this optimization succeeds,
474 it will become a copy where SRC is dead. */
475 if ((find_reg_note (insn, REG_DEAD, SET_SRC (set))
476 || optimize_reg_copy_1 (insn, SET_DEST (set), SET_SRC (set)))
477 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
479 /* Similarly for a pseudo-pseudo copy when SRC is dead. */
480 if (REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
481 optimize_reg_copy_2 (insn, SET_DEST (set), SET_SRC (set));
482 if (regno_src_regno[REGNO (SET_DEST (set))] < 0
483 && SET_SRC (set) != SET_DEST (set))
485 int srcregno = REGNO (SET_SRC(set));
486 if (regno_src_regno[srcregno] >= 0)
487 srcregno = regno_src_regno[srcregno];
488 regno_src_regno[REGNO (SET_DEST (set))] = srcregno;
492 #ifdef REGISTER_CONSTRAINTS
494 = find_matches (insn, &match);
496 if (insn_code_number < 0)
499 /* Now scan through the operands looking for a source operand
500 which is supposed to match the destination operand.
501 Then scan forward for an instruction which uses the dest
503 If it dies there, then replace the dest in both operands with
504 the source operand. */
506 for (operand_number = 0;
507 operand_number < insn_n_operands[insn_code_number];
510 rtx p, src, dst, src_subreg;
511 enum reg_class src_class, dst_class;
513 match_number = match.with[operand_number];
515 /* Nothing to do if the two operands aren't supposed to match. */
516 if (match_number < 0)
519 src = recog_operand[operand_number];
520 dst = recog_operand[match_number];
522 if (GET_CODE (src) != REG)
526 if (GET_CODE (dst) == SUBREG
527 && GET_MODE_SIZE (GET_MODE (dst))
528 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (dst))))
531 = gen_rtx_SUBREG (GET_MODE (SUBREG_REG (dst)),
532 src, SUBREG_WORD (dst));
533 dst = SUBREG_REG (dst);
535 if (GET_CODE (dst) != REG
536 || REGNO (dst) < FIRST_PSEUDO_REGISTER)
539 if (REGNO (src) < FIRST_PSEUDO_REGISTER)
541 if (match.commutative[operand_number] < operand_number)
542 regno_src_regno[REGNO (dst)] = REGNO (src);
546 if (REG_LIVE_LENGTH (REGNO (src)) < 0)
549 /* operand_number/src must be a read-only operand, and
550 match_operand/dst must be a write-only operand. */
551 if (match.use[operand_number] != READ
552 || match.use[match_number] != WRITE)
555 if (match.early_clobber[match_number]
556 && count_occurrences (PATTERN (insn), src) > 1)
559 /* Make sure match_operand is the destination. */
560 if (recog_operand[match_number] != SET_DEST (set))
563 /* If the operands already match, then there is nothing to do. */
564 /* But in the commutative case, we might find a better match. */
565 if (operands_match_p (src, dst)
566 || (match.commutative[operand_number] >= 0
567 && operands_match_p (recog_operand[match.commutative
568 [operand_number]], dst)
569 && (replacement_quality (recog_operand[match.commutative
571 >= replacement_quality (src))))
574 src_class = reg_preferred_class (REGNO (src));
575 dst_class = reg_preferred_class (REGNO (dst));
576 if (src_class != dst_class
577 && (! reg_class_subset_p (src_class, dst_class)
578 || CLASS_LIKELY_SPILLED_P (src_class))
579 && (! reg_class_subset_p (dst_class, src_class)
580 || CLASS_LIKELY_SPILLED_P (dst_class)))
583 if (fixup_match_1 (insn, set, src, src_subreg, dst, pass,
584 operand_number, match_number,
591 /* A backward pass. Replace input operands with output operands. */
593 if (regmove_dump_file)
594 fprintf (regmove_dump_file, "Starting backward pass...\n");
598 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
600 if (GET_CODE (insn) == NOTE)
602 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
604 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
607 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
609 int insn_code_number = find_matches (insn, &match);
610 int operand_number, match_number;
612 if (insn_code_number < 0)
615 /* Now scan through the operands looking for a destination operand
616 which is supposed to match a source operand.
617 Then scan backward for an instruction which sets the source
618 operand. If safe, then replace the source operand with the
619 dest operand in both instructions. */
621 for (operand_number = 0;
622 operand_number < insn_n_operands[insn_code_number];
625 rtx set, p, src, dst;
626 rtx src_note, dst_note;
629 enum reg_class src_class, dst_class;
632 match_number = match.with[operand_number];
634 /* Nothing to do if the two operands aren't supposed to match. */
635 if (match_number < 0)
638 dst = recog_operand[match_number];
639 src = recog_operand[operand_number];
641 if (GET_CODE (src) != REG)
644 if (GET_CODE (dst) != REG
645 || REGNO (dst) < FIRST_PSEUDO_REGISTER
646 || REG_LIVE_LENGTH (REGNO (dst)) < 0)
649 /* If the operands already match, then there is nothing to do. */
650 if (operands_match_p (src, dst)
651 || (match.commutative[operand_number] >= 0
652 && operands_match_p (recog_operand[match.commutative[operand_number]], dst)))
655 set = single_set (insn);
659 /* match_number/dst must be a write-only operand, and
660 operand_operand/src must be a read-only operand. */
661 if (match.use[operand_number] != READ
662 || match.use[match_number] != WRITE)
665 if (match.early_clobber[match_number]
666 && count_occurrences (PATTERN (insn), src) > 1)
669 /* Make sure match_number is the destination. */
670 if (recog_operand[match_number] != SET_DEST (set))
673 if (REGNO (src) < FIRST_PSEUDO_REGISTER)
675 if (GET_CODE (SET_SRC (set)) == PLUS
676 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT
677 && XEXP (SET_SRC (set), 0) == src
678 && fixup_match_2 (insn, dst, src,
679 XEXP (SET_SRC (set), 1),
684 src_class = reg_preferred_class (REGNO (src));
685 dst_class = reg_preferred_class (REGNO (dst));
686 if (src_class != dst_class
687 && (! reg_class_subset_p (src_class, dst_class)
688 || CLASS_LIKELY_SPILLED_P (src_class))
689 && (! reg_class_subset_p (dst_class, src_class)
690 || CLASS_LIKELY_SPILLED_P (dst_class)))
693 if (! (src_note = find_reg_note (insn, REG_DEAD, src)))
696 /* Can not modify an earlier insn to set dst if this insn
697 uses an old value in the source. */
698 if (reg_overlap_mentioned_p (dst, SET_SRC (set)))
701 if (regmove_dump_file)
702 fprintf (regmove_dump_file,
703 "Could fix operand %d of insn %d matching operand %d.\n",
704 operand_number, INSN_UID (insn), match_number);
706 /* If src is set once in a different basic block,
707 and is set equal to a constant, then do not use
708 it for this optimization, as this would make it
709 no longer equivalent to a constant. */
710 if (reg_is_remote_constant_p (src, insn, f))
713 /* Scan backward to find the first instruction that uses
714 the input operand. If the operand is set here, then
715 replace it in both instructions with match_number. */
717 for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
721 if (GET_CODE (p) == CODE_LABEL
722 || GET_CODE (p) == JUMP_INSN
723 || (GET_CODE (p) == NOTE
724 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
725 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
728 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
733 /* ??? See if all of SRC is set in P. This test is much
734 more conservative than it needs to be. */
735 pset = single_set (p);
736 if (pset && SET_DEST (pset) == src)
738 /* We use validate_replace_rtx, in case there
739 are multiple identical source operands. All of
740 them have to be changed at the same time. */
741 if (validate_replace_rtx (src, dst, insn))
743 if (validate_change (p, &SET_DEST (pset),
748 /* Change all source operands back.
749 This modifies the dst as a side-effect. */
750 validate_replace_rtx (dst, src, insn);
751 /* Now make sure the dst is right. */
752 validate_change (insn,
753 recog_operand_loc[match_number],
760 if (reg_overlap_mentioned_p (src, PATTERN (p))
761 || reg_overlap_mentioned_p (dst, PATTERN (p)))
764 /* If we have passed a call instruction, and the
765 pseudo-reg DST is not already live across a call,
766 then don't perform the optimization. */
767 if (GET_CODE (p) == CALL_INSN)
771 if (REG_N_CALLS_CROSSED (REGNO (dst)) == 0)
780 /* Remove the death note for SRC from INSN. */
781 remove_note (insn, src_note);
782 /* Move the death note for SRC to P if it is used
784 if (reg_overlap_mentioned_p (src, PATTERN (p)))
786 XEXP (src_note, 1) = REG_NOTES (p);
787 REG_NOTES (p) = src_note;
789 /* If there is a REG_DEAD note for DST on P, then remove
790 it, because DST is now set there. */
791 if (dst_note = find_reg_note (p, REG_DEAD, dst))
792 remove_note (p, dst_note);
797 REG_N_SETS (dstno)++;
798 REG_N_SETS (srcno)--;
800 REG_N_CALLS_CROSSED (dstno) += num_calls;
801 REG_N_CALLS_CROSSED (srcno) -= num_calls;
803 REG_LIVE_LENGTH (dstno) += length;
804 if (REG_LIVE_LENGTH (srcno) >= 0)
806 REG_LIVE_LENGTH (srcno) -= length;
807 /* REG_LIVE_LENGTH is only an approximation after
808 combine if sched is not run, so make sure that we
809 still have a reasonable value. */
810 if (REG_LIVE_LENGTH (srcno) < 2)
811 REG_LIVE_LENGTH (srcno) = 2;
814 /* We assume that a register is used exactly once per
815 insn in the updates above. If this is not correct,
816 no great harm is done. */
818 REG_N_REFS (dstno) += 2 * loop_depth;
819 REG_N_REFS (srcno) -= 2 * loop_depth;
821 /* If that was the only time src was set,
822 and src was not live at the start of the
823 function, we know that we have no more
824 references to src; clear REG_N_REFS so it
825 won't make reload do any work. */
826 if (REG_N_SETS (REGNO (src)) == 0
827 && ! regno_uninitialized (REGNO (src)))
828 REG_N_REFS (REGNO (src)) = 0;
830 if (regmove_dump_file)
831 fprintf (regmove_dump_file,
832 "Fixed operand %d of insn %d matching operand %d.\n",
833 operand_number, INSN_UID (insn), match_number);
840 #endif /* REGISTER_CONSTRAINTS */
845 find_matches (insn, matchp)
847 struct match *matchp;
849 int likely_spilled[MAX_RECOG_OPERANDS];
851 int insn_code_number = recog_memoized (insn);
854 if (insn_code_number < 0)
858 if (! constrain_operands (insn_code_number, 0))
861 /* Must initialize this before main loop, because the code for
862 the commutative case may set matches for operands other than
864 for (operand_number = insn_n_operands[insn_code_number];
865 --operand_number >= 0; )
866 matchp->with[operand_number] = matchp->commutative[operand_number] = -1;
868 for (operand_number = 0; operand_number < insn_n_operands[insn_code_number];
871 int output_operand = 0;
872 int matching_operand = operand_number;
876 p = insn_operand_constraint[insn_code_number][operand_number];
878 likely_spilled[operand_number] = 0;
879 matchp->use[operand_number] = READ;
880 matchp->early_clobber[operand_number] = 0;
882 matchp->use[operand_number] = WRITE;
884 matchp->use[operand_number] = READWRITE;
886 for (;*p && i < which_alternative; p++)
890 while ((c = *p++) != '\0' && c != ',')
898 matchp->early_clobber[operand_number] = 1;
901 matchp->commutative[operand_number] = operand_number + 1;
902 matchp->commutative[operand_number + 1] = operand_number;
904 case '0': case '1': case '2': case '3': case '4':
905 case '5': case '6': case '7': case '8': case '9':
907 if (c < operand_number && likely_spilled[c])
909 matchp->with[operand_number] = c;
911 if (matchp->commutative[operand_number] >= 0)
912 matchp->with[matchp->commutative[operand_number]] = c;
914 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'h':
915 case 'j': case 'k': case 'l': case 'p': case 'q': case 't': case 'u':
916 case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B':
917 case 'C': case 'D': case 'W': case 'Y': case 'Z':
918 if (CLASS_LIKELY_SPILLED_P (REG_CLASS_FROM_LETTER (c)))
919 likely_spilled[operand_number] = 1;
923 return any_matches ? insn_code_number : -1;
926 /* Try to replace output operand DST in SET, with input operand SRC. SET is
927 the only set in INSN. INSN has just been recgnized and constrained.
928 SRC is operand number OPERAND_NUMBER in INSN.
929 DST is operand number MATCH_NUMBER in INSN.
930 If BACKWARD is nonzero, we have been called in a backward pass.
931 Return nonzero for success. */
933 fixup_match_1 (insn, set, src, src_subreg, dst, backward, operand_number,
934 match_number, regmove_dump_file)
935 rtx insn, set, src, src_subreg, dst;
936 int backward, operand_number, match_number;
937 FILE *regmove_dump_file;
940 rtx post_inc = 0, post_inc_set = 0, search_end = 0;
942 int num_calls = 0, s_num_calls = 0;
943 enum rtx_code code = NOTE;
944 HOST_WIDE_INT insn_const, newconst;
945 rtx overlap = 0; /* need to move insn ? */
946 rtx src_note = find_reg_note (insn, REG_DEAD, src), dst_note;
947 int length, s_length, true_loop_depth;
951 /* Look for (set (regX) (op regA constX))
952 (set (regY) (op regA constY))
954 (set (regA) (op regA constX)).
955 (set (regY) (op regA constY-constX)).
956 This works for add and shift operations, if
957 regA is dead after or set by the second insn. */
959 code = GET_CODE (SET_SRC (set));
960 if ((code == PLUS || code == LSHIFTRT
961 || code == ASHIFT || code == ASHIFTRT)
962 && XEXP (SET_SRC (set), 0) == src
963 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
964 insn_const = INTVAL (XEXP (SET_SRC (set), 1));
965 else if (! stable_but_for_p (SET_SRC (set), src, dst))
968 /* We might find a src_note while scanning. */
972 if (regmove_dump_file)
973 fprintf (regmove_dump_file,
974 "Could fix operand %d of insn %d matching operand %d.\n",
975 operand_number, INSN_UID (insn), match_number);
977 /* If SRC is equivalent to a constant set in a different basic block,
978 then do not use it for this optimization. We want the equivalence
979 so that if we have to reload this register, we can reload the
980 constant, rather than extending the lifespan of the register. */
981 if (reg_is_remote_constant_p (src, insn, get_insns ()))
984 /* Scan forward to find the next instruction that
985 uses the output operand. If the operand dies here,
986 then replace it in both instructions with
989 for (length = s_length = 0, p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
991 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
992 || (GET_CODE (p) == NOTE
993 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
994 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
997 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
1004 if (reg_set_p (src, p) || reg_set_p (dst, p)
1005 || (GET_CODE (PATTERN (p)) == USE
1006 && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
1009 /* See if all of DST dies in P. This test is
1010 slightly more conservative than it needs to be. */
1011 if ((dst_note = find_regno_note (p, REG_DEAD, REGNO (dst)))
1012 && (GET_MODE (XEXP (dst_note, 0)) == GET_MODE (dst)))
1019 /* If an optimization is done, the value of SRC while P
1020 is executed will be changed. Check that this is OK. */
1021 if (reg_overlap_mentioned_p (src, PATTERN (p)))
1023 for (q = p; q; q = NEXT_INSN (q))
1025 if (GET_CODE (q) == CODE_LABEL || GET_CODE (q) == JUMP_INSN
1026 || (GET_CODE (q) == NOTE
1027 && (NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_BEG
1028 || NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_END)))
1033 if (GET_RTX_CLASS (GET_CODE (q)) != 'i')
1035 if (reg_overlap_mentioned_p (src, PATTERN (q))
1036 || reg_set_p (src, q))
1040 set2 = single_set (q);
1041 if (! q || ! set2 || GET_CODE (SET_SRC (set2)) != code
1042 || XEXP (SET_SRC (set2), 0) != src
1043 || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT
1044 || (SET_DEST (set2) != src
1045 && ! find_reg_note (q, REG_DEAD, src)))
1047 /* If this is a PLUS, we can still save a register by doing
1050 src -= insn_const; .
1051 This also gives opportunities for subsequent
1052 optimizations in the backward pass, so do it there. */
1053 if (code == PLUS && backward
1055 /* We may not emit an insn directly
1056 after P if the latter sets CC0. */
1057 && ! sets_cc0_p (PATTERN (p))
1065 newconst = -insn_const;
1073 newconst = INTVAL (XEXP (SET_SRC (set2), 1)) - insn_const;
1074 /* Reject out of range shifts. */
1078 >= GET_MODE_BITSIZE (GET_MODE (SET_SRC (set2))))))
1083 if (SET_DEST (set2) != src)
1084 post_inc_set = set2;
1087 /* We use 1 as last argument to validate_change so that all
1088 changes are accepted or rejected together by apply_change_group
1089 when it is called by validate_replace_rtx . */
1090 validate_change (q, &XEXP (SET_SRC (set2), 1),
1091 GEN_INT (newconst), 1);
1093 validate_change (insn, recog_operand_loc[match_number], src, 1);
1094 if (validate_replace_rtx (dst, src_subreg, p))
1099 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
1101 if (! src_note && reg_overlap_mentioned_p (src, PATTERN (p)))
1103 /* INSN was already checked to be movable when
1104 we found no REG_DEAD note for src on it. */
1106 src_note = find_reg_note (p, REG_DEAD, src);
1109 /* If we have passed a call instruction, and the pseudo-reg SRC is not
1110 already live across a call, then don't perform the optimization. */
1111 if (GET_CODE (p) == CALL_INSN)
1113 if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
1127 true_loop_depth = backward ? 2 - loop_depth : loop_depth;
1129 /* Remove the death note for DST from P. */
1130 remove_note (p, dst_note);
1133 post_inc = emit_insn_after (copy_rtx (PATTERN (insn)), p);
1134 #if defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT)
1136 && try_auto_increment (search_end, post_inc, 0, src, newconst, 1))
1139 validate_change (insn, &XEXP (SET_SRC (set), 1), GEN_INT (insn_const), 0);
1140 REG_N_SETS (REGNO (src))++;
1141 REG_N_REFS (REGNO (src)) += true_loop_depth;
1142 REG_LIVE_LENGTH (REGNO (src))++;
1146 /* The lifetime of src and dest overlap,
1147 but we can change this by moving insn. */
1148 rtx pat = PATTERN (insn);
1150 remove_note (overlap, src_note);
1151 #if defined (HAVE_POST_INCREMENT) || defined (HAVE_POST_DECREMENT)
1153 && try_auto_increment (overlap, insn, 0, src, insn_const, 0))
1158 rtx notes = REG_NOTES (insn);
1160 emit_insn_after_with_line_notes (pat, PREV_INSN (p), insn);
1161 PUT_CODE (insn, NOTE);
1162 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1163 NOTE_SOURCE_FILE (insn) = 0;
1164 /* emit_insn_after_with_line_notes has no
1165 return value, so search for the new insn. */
1166 for (insn = p; PATTERN (insn) != pat; )
1167 insn = PREV_INSN (insn);
1169 REG_NOTES (insn) = notes;
1172 /* Sometimes we'd generate src = const; src += n;
1173 if so, replace the instruction that set src
1174 in the first place. */
1176 if (! overlap && (code == PLUS || code == MINUS))
1178 rtx note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
1180 int num_calls2 = 0, s_length2 = 0;
1182 if (note && CONSTANT_P (XEXP (note, 0)))
1184 for (q = PREV_INSN (insn); q; q = PREV_INSN(q))
1186 if (GET_CODE (q) == JUMP_INSN)
1191 if (GET_RTX_CLASS (GET_CODE (q)) != 'i')
1194 if (reg_set_p (src, q))
1196 set2 = single_set (q);
1199 if (reg_overlap_mentioned_p (src, PATTERN (q)))
1204 if (GET_CODE (p) == CALL_INSN)
1207 if (q && set2 && SET_DEST (set2) == src && CONSTANT_P (SET_SRC (set2))
1208 && validate_change (insn, &SET_SRC (set), XEXP (note, 0), 0))
1211 NOTE_LINE_NUMBER (q) = NOTE_INSN_DELETED;
1212 NOTE_SOURCE_FILE (q) = 0;
1213 REG_N_SETS (REGNO (src))--;
1214 REG_N_CALLS_CROSSED (REGNO (src)) -= num_calls2;
1215 REG_N_REFS (REGNO (src)) -= true_loop_depth;
1216 REG_LIVE_LENGTH (REGNO (src)) -= s_length2;
1222 #if defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT)
1223 else if ((code == PLUS || code == MINUS) && insn_const
1224 && try_auto_increment (p, insn, 0, src, insn_const, 1))
1227 #if defined (HAVE_POST_INCREMENT) || defined (HAVE_POST_DECREMENT)
1229 && try_auto_increment (p, post_inc, post_inc_set, src, newconst, 0))
1232 #if defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT)
1233 /* If post_inc still prevails, try to find an
1234 insn where it can be used as a pre-in/decrement.
1235 If code is MINUS, this was already tried. */
1236 if (post_inc && code == PLUS
1237 /* Check that newconst is likely to be usable
1238 in a pre-in/decrement before starting the search. */
1240 #if defined (HAVE_PRE_INCREMENT)
1241 || (newconst > 0 && newconst <= MOVE_MAX)
1243 #if defined (HAVE_PRE_DECREMENT)
1244 || (newconst < 0 && newconst >= -MOVE_MAX)
1246 ) && exact_log2 (newconst))
1250 inc_dest = post_inc_set ? SET_DEST (post_inc_set) : src;
1251 for (q = post_inc; q = NEXT_INSN (q); )
1253 if (GET_CODE (q) == CODE_LABEL || GET_CODE (q) == JUMP_INSN
1254 || (GET_CODE (q) == NOTE
1255 && (NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_BEG
1256 || NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_END)))
1258 if (GET_RTX_CLASS (GET_CODE (q)) != 'i')
1260 if (src != inc_dest && (reg_overlap_mentioned_p (src, PATTERN (q))
1261 || reg_set_p (src, q)))
1263 if (reg_set_p (inc_dest, q))
1265 if (reg_overlap_mentioned_p (inc_dest, PATTERN (q)))
1267 try_auto_increment (q, post_inc,
1268 post_inc_set, inc_dest, newconst, 1);
1273 #endif /* defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT) */
1274 /* Move the death note for DST to INSN if it is used
1276 if (reg_overlap_mentioned_p (dst, PATTERN (insn)))
1278 XEXP (dst_note, 1) = REG_NOTES (insn);
1279 REG_NOTES (insn) = dst_note;
1284 /* Move the death note for SRC from INSN to P. */
1286 remove_note (insn, src_note);
1287 XEXP (src_note, 1) = REG_NOTES (p);
1288 REG_NOTES (p) = src_note;
1290 REG_N_CALLS_CROSSED (REGNO (src)) += s_num_calls;
1293 REG_N_SETS (REGNO (src))++;
1294 REG_N_SETS (REGNO (dst))--;
1296 REG_N_CALLS_CROSSED (REGNO (dst)) -= num_calls;
1298 REG_LIVE_LENGTH (REGNO (src)) += s_length;
1299 if (REG_LIVE_LENGTH (REGNO (dst)) >= 0)
1301 REG_LIVE_LENGTH (REGNO (dst)) -= length;
1302 /* REG_LIVE_LENGTH is only an approximation after
1303 combine if sched is not run, so make sure that we
1304 still have a reasonable value. */
1305 if (REG_LIVE_LENGTH (REGNO (dst)) < 2)
1306 REG_LIVE_LENGTH (REGNO (dst)) = 2;
1309 /* We assume that a register is used exactly once per
1310 insn in the updates above. If this is not correct,
1311 no great harm is done. */
1313 REG_N_REFS (REGNO (src)) += 2 * true_loop_depth;
1314 REG_N_REFS (REGNO (dst)) -= 2 * true_loop_depth;
1316 /* If that was the only time dst was set,
1317 and dst was not live at the start of the
1318 function, we know that we have no more
1319 references to dst; clear REG_N_REFS so it
1320 won't make reload do any work. */
1321 if (REG_N_SETS (REGNO (dst)) == 0
1322 && ! regno_uninitialized (REGNO (dst)))
1323 REG_N_REFS (REGNO (dst)) = 0;
1325 if (regmove_dump_file)
1326 fprintf (regmove_dump_file,
1327 "Fixed operand %d of insn %d matching operand %d.\n",
1328 operand_number, INSN_UID (insn), match_number);
1333 /* return nonzero if X is stable but for mentioning SRC or mentioning /
1334 changing DST . If in doubt, presume it is unstable. */
1336 stable_but_for_p (x, src, dst)
1339 RTX_CODE code = GET_CODE (x);
1340 switch (GET_RTX_CLASS (code))
1342 case '<': case '1': case 'c': case '2': case 'b': case '3':
1345 char *fmt = GET_RTX_FORMAT (code);
1346 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1347 if (fmt[i] == 'e' && ! stable_but_for_p (XEXP (x, i), src, dst))
1352 if (x == src || x == dst)
1356 return ! rtx_unstable_p (x);
1360 /* Test if regmove seems profitable for this target. */
1362 regmove_profitable_p ()
1364 #ifdef REGISTER_CONSTRAINTS
1366 enum machine_mode mode;
1367 optab tstoptab = add_optab;
1368 do /* check add_optab and ashl_optab */
1369 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1370 mode = GET_MODE_WIDER_MODE (mode))
1372 int icode = (int) tstoptab->handlers[(int) mode].insn_code;
1373 rtx reg0, reg1, reg2, pat;
1376 if (GET_MODE_BITSIZE (mode) < 32 || icode == CODE_FOR_nothing)
1378 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1379 if (TEST_HARD_REG_BIT (reg_class_contents[GENERAL_REGS], i))
1381 if (i + 2 >= FIRST_PSEUDO_REGISTER)
1383 reg0 = gen_rtx_REG (insn_operand_mode[icode][0], i);
1384 reg1 = gen_rtx_REG (insn_operand_mode[icode][1], i + 1);
1385 reg2 = gen_rtx_REG (insn_operand_mode[icode][2], i + 2);
1386 if (! (*insn_operand_predicate[icode][0]) (reg0, VOIDmode)
1387 || ! (*insn_operand_predicate[icode][1]) (reg1, VOIDmode)
1388 || ! (*insn_operand_predicate[icode][2]) (reg2, VOIDmode))
1390 pat = GEN_FCN (icode) (reg0, reg1, reg2);
1393 if (GET_CODE (pat) == SEQUENCE)
1394 pat = XVECEXP (pat, 0, XVECLEN (pat, 0) - 1);
1396 pat = make_insn_raw (pat);
1397 if (! single_set (pat)
1398 || GET_CODE (SET_SRC (single_set (pat))) != tstoptab->code)
1399 /* Unexpected complexity; don't need to handle this unless
1400 we find a machine where this occurs and regmove should
1403 if (find_matches (pat, &match) >= 0)
1407 while (tstoptab != ashl_optab && (tstoptab = ashl_optab, 1));
1408 #endif /* REGISTER_CONSTRAINTS */