Import final gcc2 snapshot (990109)
[official-gcc.git] / gcc / regmove.c
blobb9b6a936ae74f373ac0e9728e4b887aa36b1377a
1 /* Move registers around to reduce number of move instructions needed.
2 Copyright (C) 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)
9 any later version.
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. */
26 #include "config.h"
27 #include "system.h"
28 #include "rtl.h"
29 #include "insn-config.h"
30 #include "recog.h"
31 #include "output.h"
32 #include "reload.h"
33 #include "regs.h"
34 #include "hard-reg-set.h"
35 #include "flags.h"
36 #include "expr.h"
37 #include "insn-flags.h"
40 /* Static nesting depth within loops; 1 is the top level. */
41 static int loop_depth;
43 /* ??? Missing documenation on this variable. */
44 static int *regno_src_regno;
46 /* Defines the uses of each operand. */
47 enum operand_use {READ, WRITE, READWRITE};
49 /* This structure describes the usage of operands.
51 WITH indicates which operand (-1 for none) an operand matches with.
53 COMMUTATIVE indicates which operand (-1 for none) and operand is
54 commutative with.
56 USE tells whether the operand is read, written, or both.
58 EARLY_CLOBBER, if nonzero, indicates that the operand is an output that
59 can be written before all inputs are used. */
61 struct match
63 int with[MAX_RECOG_OPERANDS];
64 enum operand_use use[MAX_RECOG_OPERANDS];
65 int commutative[MAX_RECOG_OPERANDS];
66 int early_clobber[MAX_RECOG_OPERANDS];
69 static rtx next_insn_for_regmove PROTO((rtx));
70 static rtx prev_insn_for_regmove PROTO((rtx));
71 static int try_auto_increment PROTO((rtx, rtx, rtx, rtx,
72 HOST_WIDE_INT, int));
73 static int replacement_quality PROTO((rtx));
74 static int optimize_reg_copy_1 PROTO((rtx, rtx, rtx));
75 static void optimize_reg_copy_2 PROTO((rtx, rtx, rtx));
76 static void optimize_reg_copy_extend PROTO((rtx, rtx, rtx));
77 static int reg_is_remote_constant_p PROTO((rtx, rtx, rtx));
78 static int merge_additions PROTO((rtx, rtx, rtx, rtx, FILE *));
79 static int find_matches PROTO((rtx, struct match *));
80 static int fixup_match PROTO((rtx, rtx, rtx, rtx, rtx, int,
81 int, int, FILE *));
82 static int stable_but_for_p PROTO((rtx, rtx, rtx));
84 /* Get next (previous) insn we consider. We only return something that's
85 an insn, but do not cross a basic block or loop boundary. Note that
86 if basic block boundaries change, this code must change as well. */
88 static rtx
89 next_insn_for_regmove (insn)
90 rtx insn;
92 while (insn != 0)
94 insn = NEXT_INSN (insn);
96 if (insn == 0
97 || GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN
98 || (GET_CODE (insn) == NOTE
99 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
100 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)))
101 return 0;
103 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
104 break;
107 return insn;
110 static rtx
111 prev_insn_for_regmove (insn)
112 rtx insn;
114 while (insn != 0)
116 insn = PREV_INSN (insn);
118 if (insn == 0
119 || GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN
120 || (GET_CODE (insn) == NOTE
121 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
122 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)))
123 return 0;
125 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
126 break;
129 return insn;
133 #ifdef AUTO_INC_DEC
135 /* INC_INSN is an instruction that adds INCREMENT to REG.
136 If PRE is nonzero, the addition was before INSN, after if it was zero.
137 If INC_SET is nonzero, it is the SET within INC_INSN that does
138 the incrementing and it is not to be deleted; otherwise, INC_INSN
139 has no effect other than doing the specified increment.
141 Try to replace INC_INSN with a post/pre in/decrement inside INSN.
142 Return nonzero if we were able to do the replacement. If INC_SET is
143 nonzero, its source is replaced by REG; otherwise INC_INSN is deleted. */
145 static int
146 try_auto_increment (insn, inc_insn, inc_set, reg, increment, pre)
147 rtx insn;
148 rtx inc_insn;
149 rtx inc_set;
150 rtx reg;
151 HOST_WIDE_INT increment;
152 int pre;
154 enum rtx_code inc_code;
155 rtx pset, use;
156 int size;
158 /* There must be exactly one usage of REG as an address inside INSN, which
159 also must have only one set. Then get the size of the memory. We must
160 do it this way since some insns (such as extensions) have
161 mixed modes within SET_SRC. */
162 if ((pset = single_set (insn)) == 0
163 || (use = find_use_as_address (pset, reg, 0)) == 0
164 || use == (rtx) 1)
165 return 0;
167 size = GET_MODE_SIZE (GET_MODE (use));
168 if (0
169 #ifdef HAVE_POST_INCREMENT
170 || (pre == 0 && (inc_code = POST_INC, increment == size))
171 #endif
172 #ifdef HAVE_PRE_INCREMENT
173 || (pre == 1 && (inc_code = PRE_INC, increment == size))
174 #endif
175 #ifdef HAVE_POST_DECREMENT
176 || (pre == 0 && (inc_code = POST_DEC, increment == - size))
177 #endif
178 #ifdef HAVE_PRE_DECREMENT
179 || (pre == 1 && (inc_code = PRE_DEC, increment == - size))
180 #endif
183 /* Make sure we can do any needed replacement before changing
184 anything. */
185 if (inc_set != 0)
186 validate_change (inc_insn, &SET_SRC (inc_set), reg, 1);
188 validate_change (insn, &XEXP (use, 0),
189 gen_rtx (inc_code, GET_MODE (reg), reg), 1);
191 if (! apply_change_group ())
192 return 0;
194 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_INC, reg, REG_NOTES (insn));
196 if (inc_set == 0)
198 PUT_CODE (inc_insn, NOTE);
199 NOTE_LINE_NUMBER (inc_insn) = NOTE_INSN_DELETED;
200 NOTE_SOURCE_FILE (inc_insn) = 0;
203 return 1;
206 return 0;
208 #endif /* AUTO_INC_DEC */
210 /* Indicate how good a choice REG (which appears as a source) is to replace
211 a destination register with. The higher the returned value, the better
212 the choice. The main objective is to avoid using a register that is
213 a candidate for tying to a hard register, since the output might in
214 turn be a candidate to be tied to a different hard register. */
216 static int
217 replacement_quality (reg)
218 rtx reg;
220 int src_regno;
222 /* Bad if this isn't a register at all. */
223 if (GET_CODE (reg) != REG)
224 return 0;
226 /* If this register is not meant to get a hard register,
227 it is a poor choice. */
228 if (REG_LIVE_LENGTH (REGNO (reg)) < 0)
229 return 0;
231 src_regno = regno_src_regno[REGNO (reg)];
233 /* If it was not copied from another register, it is fine. */
234 if (src_regno < 0)
235 return 3;
237 /* Copied from a hard register? */
238 if (src_regno < FIRST_PSEUDO_REGISTER)
239 return 1;
241 /* Copied from a pseudo register - not as bad as from a hard register,
242 yet still cumbersome, since the register live length will be lengthened
243 when the registers get tied. */
244 return 2;
247 /* INSN is a copy from SRC to DEST, both registers, and SRC does not die
248 in INSN.
250 Search forward to see if SRC dies before either it or DEST is modified,
251 but don't scan past the end of a basic block. If so, we can replace SRC
252 with DEST and let SRC die in INSN.
254 This will reduce the number of registers live in that range and may enable
255 DEST to be tied to SRC, thus often saving one register in addition to a
256 register-register copy.
258 Return 1 if we were able to perform this optimization. */
260 static int
261 optimize_reg_copy_1 (insn, dest, src)
262 rtx insn;
263 rtx dest;
264 rtx src;
266 rtx p, q;
267 rtx note;
268 rtx dest_death = 0;
269 int sregno = REGNO (src);
270 int dregno = REGNO (dest);
272 /* We don't want to mess with hard regs if register classes are small. */
273 if (sregno == dregno
274 || (SMALL_REGISTER_CLASSES
275 && (sregno < FIRST_PSEUDO_REGISTER
276 || dregno < FIRST_PSEUDO_REGISTER))
277 /* We don't see all updates to SP if they are in an auto-inc memory
278 reference, so we must disallow this optimization on them. */
279 || sregno == STACK_POINTER_REGNUM || dregno == STACK_POINTER_REGNUM)
280 return 0;
282 for (p = next_insn_for_regmove (insn); p != 0; p = next_insn_for_regmove (p))
284 if (reg_set_p (src, p) || reg_set_p (dest, p)
285 /* Don't change a USE of a register. */
286 || (GET_CODE (PATTERN (p)) == USE
287 && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
288 break;
290 /* See if all of SRC dies in P. This test is slightly more
291 conservative than it needs to be. */
292 if ((note = find_regno_note (p, REG_DEAD, sregno)) != 0
293 && GET_MODE (XEXP (note, 0)) == GET_MODE (src))
295 int failed = 0;
296 int length = 0;
297 int d_length = 0;
298 int n_calls = 0;
299 int d_n_calls = 0;
301 /* We can do the optimization. Scan forward from INSN again,
302 replacing regs as we go. Set FAILED if a replacement can't
303 be done. In that case, we can't move the death note for SRC.
304 This should be rare.
305 Set to stop at next insn. */
307 for (q = next_real_insn (insn);
308 q != next_real_insn (p);
309 q = next_real_insn (q))
311 if (reg_overlap_mentioned_p (src, PATTERN (q)))
313 /* If SRC is a hard register, we might miss some
314 overlapping registers with validate_replace_rtx,
315 so we would have to undo it. We can't if DEST is
316 present in the insn, so fail in that combination
317 of cases. */
318 if (sregno < FIRST_PSEUDO_REGISTER
319 && reg_mentioned_p (dest, PATTERN (q)))
320 failed = 1;
322 /* Replace all uses and make sure that the register
323 isn't still present. */
324 else if (validate_replace_rtx (src, dest, q)
325 && (sregno >= FIRST_PSEUDO_REGISTER
326 || ! reg_overlap_mentioned_p (src,
327 PATTERN (q))))
329 /* We assume that a register is used exactly once per
330 insn in the updates below. If this is not correct,
331 no great harm is done. */
332 if (sregno >= FIRST_PSEUDO_REGISTER)
333 REG_N_REFS (sregno) -= loop_depth;
334 if (dregno >= FIRST_PSEUDO_REGISTER)
335 REG_N_REFS (dregno) += loop_depth;
337 else
339 validate_replace_rtx (dest, src, q);
340 failed = 1;
344 /* Count the insns and CALL_INSNs passed. If we passed the
345 death note of DEST, show increased live length. */
346 length++;
347 if (dest_death)
348 d_length++;
350 /* If the insn in which SRC dies is a CALL_INSN, don't count it
351 as a call that has been crossed. Otherwise, count it. */
352 if (q != p && GET_CODE (q) == CALL_INSN)
354 n_calls++;
355 if (dest_death)
356 d_n_calls++;
359 /* If DEST dies here, remove the death note and save it for
360 later. Make sure ALL of DEST dies here; again, this is
361 overly conservative. */
362 if (dest_death == 0
363 && (dest_death = find_regno_note (q, REG_DEAD, dregno)) != 0)
365 if (GET_MODE (XEXP (dest_death, 0)) != GET_MODE (dest))
366 failed = 1, dest_death = 0;
367 else
368 remove_note (q, dest_death);
372 if (! failed)
374 if (sregno >= FIRST_PSEUDO_REGISTER)
376 if (REG_LIVE_LENGTH (sregno) >= 0)
378 REG_LIVE_LENGTH (sregno) -= length;
379 /* REG_LIVE_LENGTH is only an approximation after
380 combine if sched is not run, so make sure that we
381 still have a reasonable value. */
382 if (REG_LIVE_LENGTH (sregno) < 2)
383 REG_LIVE_LENGTH (sregno) = 2;
386 REG_N_CALLS_CROSSED (sregno) -= n_calls;
389 if (dregno >= FIRST_PSEUDO_REGISTER)
391 if (REG_LIVE_LENGTH (dregno) >= 0)
392 REG_LIVE_LENGTH (dregno) += d_length;
394 REG_N_CALLS_CROSSED (dregno) += d_n_calls;
397 /* Move death note of SRC from P to INSN. */
398 remove_note (p, note);
399 XEXP (note, 1) = REG_NOTES (insn);
400 REG_NOTES (insn) = note;
403 /* Put death note of DEST on P if we saw it die. */
404 if (dest_death)
406 XEXP (dest_death, 1) = REG_NOTES (p);
407 REG_NOTES (p) = dest_death;
410 return ! failed;
413 /* If SRC is a hard register which is set or killed in some other
414 way, we can't do this optimization. */
415 else if (sregno < FIRST_PSEUDO_REGISTER
416 && dead_or_set_p (p, src))
417 break;
420 return 0;
423 /* INSN is a copy of SRC to DEST, in which SRC dies. See if we now have
424 a sequence of insns that modify DEST followed by an insn that sets
425 SRC to DEST in which DEST dies, with no prior modification of DEST.
426 (There is no need to check if the insns in between actually modify
427 DEST. We should not have cases where DEST is not modified, but
428 the optimization is safe if no such modification is detected.)
429 In that case, we can replace all uses of DEST, starting with INSN and
430 ending with the set of SRC to DEST, with SRC. We do not do this
431 optimization if a CALL_INSN is crossed unless SRC already crosses a
432 call or if DEST dies before the copy back to SRC.
434 It is assumed that DEST and SRC are pseudos; it is too complicated to do
435 this for hard registers since the substitutions we may make might fail. */
437 static void
438 optimize_reg_copy_2 (insn, dest, src)
439 rtx insn;
440 rtx dest;
441 rtx src;
443 rtx p, q;
444 rtx set;
445 int sregno = REGNO (src);
446 int dregno = REGNO (dest);
448 for (p = next_insn_for_regmove (insn); p != 0; p = next_insn_for_regmove (p))
450 set = single_set (p);
451 if (set && SET_SRC (set) == dest && SET_DEST (set) == src
452 && find_reg_note (p, REG_DEAD, dest))
454 /* We can do the optimization. Scan forward from INSN again,
455 replacing regs as we go.
456 Set to stop at next insn. */
458 for (q = insn; q != NEXT_INSN (p); q = NEXT_INSN (q))
459 if (GET_RTX_CLASS (GET_CODE (q)) == 'i')
461 if (reg_mentioned_p (dest, PATTERN (q)))
463 PATTERN (q) = replace_rtx (PATTERN (q), dest, src);
465 /* We assume that a register is used exactly once per
466 insn in the updates below. If this is not correct,
467 no great harm is done. */
468 REG_N_REFS (dregno) -= loop_depth;
469 REG_N_REFS (sregno) += loop_depth;
472 if (GET_CODE (q) == CALL_INSN)
474 REG_N_CALLS_CROSSED (dregno)--;
475 REG_N_CALLS_CROSSED (sregno)++;
479 remove_note (p, find_reg_note (p, REG_DEAD, dest));
480 REG_N_DEATHS (dregno)--;
481 remove_note (insn, find_reg_note (insn, REG_DEAD, src));
482 REG_N_DEATHS (sregno)--;
483 return;
486 if (reg_set_p (src, p)
487 || find_reg_note (p, REG_DEAD, dest)
488 || (GET_CODE (p) == CALL_INSN && REG_N_CALLS_CROSSED (sregno) == 0))
489 break;
493 /* INSN contains a single which is a ZERO_ or SIGN_EXTEND. See if its souce
494 dies there and if it is only set once, by a load from memory. If so, try
495 to incorporate the zero/sign extension into the memory read, change the
496 source to the mode of the destination, and alter the remaining accesses to
497 use the appropriate SUBREG. This allows the two registers to be tied
498 later. */
500 static void
501 optimize_reg_extend (insn, set)
502 rtx insn;
503 rtx set;
505 rtx src = SET_SRC (set);
506 rtx src_reg = XEXP (src, 0);
507 int src_no = REGNO (src_reg);
508 enum machine_mode old_mode = GET_MODE (src_reg);
509 rtx p, p_set, subreg;
511 if (REGNO (src_reg) < FIRST_PSEUDO_REGISTER
512 || REGNO (SET_DEST (set)) < FIRST_PSEUDO_REGISTER
513 || ! find_reg_note (insn, REG_DEAD, src_reg)
514 || REG_N_SETS (REGNO (src_reg)) != 1)
515 return;
517 /* Find the previous insn we are allowed to consider that sets SRC_REG. */
518 for (p = prev_insn_for_regmove (insn); p != 0; p = prev_insn_for_regmove (p))
519 if (reg_set_p (src_reg, p))
520 break;
522 if (p == 0
523 || (p_set = single_set (p)) == 0
524 || GET_CODE (SET_SRC (p_set)) != MEM
525 || SET_DEST (p_set) != src_reg)
526 return;
528 /* Change SRC_REG to be the extended mode, change the insn we found to
529 extend from memory, and change out SET to use the extended form. */
531 PUT_MODE (src_reg, GET_MODE (src));
532 validate_change (p, &SET_SRC (p_set),
533 gen_rtx (GET_CODE (src), GET_MODE (src), src_reg),
535 validate_change (insn, &SET_SRC (set), src_reg, 1);
537 if (! apply_change_group ())
539 PUT_MODE (src_reg, old_mode);
540 return;
543 subreg = gen_lowpart (old_mode, src_reg);
544 for (; p != insn; p = next_real_insn (p))
545 validate_replace_rtx (src_reg, subreg, p);
548 /* Return nonzero if REG is set in only one location and is set to a
549 constant, but is set in a different basic block from INSN (an
550 instructions which uses REG). In this case REG is equivalent to a
551 constant, and we don't want to break that equivalence, because that
552 may increase register pressure and make reload harder. If REG is
553 set in the same basic block as INSN, we don't worry about it,
554 because we'll probably need a register anyhow (??? but what if REG
555 is used in a different basic block as well as this one?). FIRST is
556 the first insn in the function. */
558 static int
559 reg_is_remote_constant_p (reg, insn, first)
560 rtx reg;
561 rtx insn;
562 rtx first;
564 rtx p;
566 if (REG_N_SETS (REGNO (reg)) != 1)
567 return 0;
569 /* Look for the set. */
570 for (p = LOG_LINKS (insn); p != 0; p = XEXP (p, 1))
572 rtx s;
574 if (REG_NOTE_KIND (p) != 0)
575 continue;
577 if ((s = single_set (XEXP (p, 0))) != 0
578 && GET_CODE (SET_DEST (s)) == REG
579 && REGNO (SET_DEST (s)) == REGNO (reg))
580 /* The register is set in the same basic block. */
581 return 0;
584 for (p = first; p != 0 && p != insn; p = NEXT_INSN (p))
586 rtx s;
588 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
589 continue;
592 if ((s = single_set (p)) != 0
593 && GET_CODE (SET_DEST (s)) == REG
594 && REGNO (SET_DEST (s)) == REGNO (reg))
595 /* This is the instruction which sets REG. If there is a
596 REG_EQUAL note, then REG is equivalent to a constant. */
597 return find_reg_note (p, REG_EQUAL, NULL_RTX) != 0;
600 return 0;
603 /* INSN is adding OFFSET, a CONST_INT, to SRC, a REG, to produe DST, also a
604 REG. Search backwards looking for another add immediate insn with the
605 same source and destination registers. If we find one, change INSN to
606 have the same input and output register, but with an adjusted constant,
607 and return 1. If no changes are made, return 0.
609 This changes
611 (set (reg0) (plus reg1 offset1))
613 (set (reg0) (plus reg1 offset2))
617 (set (reg0) (plus reg1 offset1))
619 (set (reg0) (plus reg0 offset2-offset1)) */
621 static int
622 merge_additions (insn, dst, src, offset, regmove_dump_file)
623 rtx insn;
624 rtx dst;
625 rtx src;
626 rtx offset;
627 FILE *regmove_dump_file;
629 rtx p, dst_death = 0;
630 int length, num_calls = 0;
632 /* If SRC dies in INSN, we'd have to move the death note. This is
633 more work than worthwhile, so skip the optimization in this case. */
634 if (find_regno_note (insn, REG_DEAD, REGNO (src))
635 /* We don't want to mess with hard regs if register classes are small. */
636 || (SMALL_REGISTER_CLASSES
637 && (REGNO (src) < FIRST_PSEUDO_REGISTER
638 || REGNO (dst) < FIRST_PSEUDO_REGISTER))
639 /* We don't see all updates to SP if they are in an auto-inc memory
640 reference, so we must disallow this optimization on them. */
641 || src == stack_pointer_rtx || dst == stack_pointer_rtx)
642 return 0;
644 /* Scan backward to find the first instruction that sets DST. */
645 for (length = 0, p = prev_insn_for_regmove (insn);
646 p != 0;
647 p = prev_insn_for_regmove (insn))
649 rtx pset;
651 if (find_regno_note (p, REG_DEAD, REGNO (dst)))
652 dst_death = p;
654 if (dst_death == 0)
655 length++;
657 if ((pset = single_set (p)) != 0
658 && rtx_equal_p (SET_DEST (pset), dst)
659 && GET_CODE (SET_SRC (pset)) == PLUS
660 && rtx_equal_p (XEXP (SET_SRC (pset), 0), src)
661 && GET_CODE (XEXP (SET_SRC (pset), 1)) == CONST_INT)
663 HOST_WIDE_INT newconst
664 = INTVAL (offset) - INTVAL (XEXP (SET_SRC (pset), 1));
665 enum insn_code icode
666 = add_optab->handlers[(int) GET_MODE (dst)].insn_code;
667 rtx newpat = GEN_FCN (icode) (dst, dst, GEN_INT (newconst));
669 if (newpat != 0
670 && validate_change (insn, &PATTERN (insn), newpat, 0))
672 /* Remove the death note for DST from DST_DEATH. */
673 if (dst_death != 0)
675 remove_death (REGNO (dst), dst_death);
676 REG_LIVE_LENGTH (REGNO (dst)) += length;
677 REG_N_CALLS_CROSSED (REGNO (dst)) += num_calls;
680 REG_N_REFS (REGNO (dst)) += loop_depth;
681 REG_N_REFS (REGNO (src)) -= loop_depth;
683 if (regmove_dump_file)
684 fprintf (regmove_dump_file,
685 "Fixed operand of insn %d.\n", INSN_UID (insn));
687 #ifdef AUTO_INC_DEC
688 /* Now that we've changed this insn, see if that produces any
689 auto-increment opportunities. Try in both directions.
690 Stop after we've made one. */
691 for (p = prev_insn_for_regmove (insn); p != 0;
692 p = prev_insn_for_regmove (p))
693 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
695 if (try_auto_increment (p, insn, 0, dst, newconst, 0))
696 return 1;
697 break;
700 for (p = next_insn_for_regmove (insn); p != 0;
701 p = next_insn_for_regmove (p))
702 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
704 try_auto_increment (p, insn, 0, dst, newconst, 1);
705 break;
707 #endif
709 return 1;
713 if (reg_set_p (dst, PATTERN (p)))
714 break;
716 /* If we have passed a call instruction, and the pseudo-reg SRC is not
717 already live across a call, don't perform the optimization.
718 reg_set_p is overly conservative for CALL_INSNS and thinks that all
719 hard regs are clobbered. Thus, we only use it for src for
720 non-call insns. */
722 if (GET_CODE (p) == CALL_INSN)
724 if (dst_death == 0)
725 num_calls++;
727 if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
728 break;
730 if (call_used_regs [REGNO (dst)]
731 || find_reg_fusage (p, CLOBBER, dst))
732 break;
735 else if (reg_set_p (src, PATTERN (p)))
736 break;
739 return 0;
742 /* Returns the INSN_CODE for INSN if its pattern has matching constraints for
743 any operand. Returns -1 if INSN can't be recognized, or if the alternative
744 can't be determined.
746 Initialize the info in MATCHP based on the constraints. */
748 static int
749 find_matches (insn, matchp)
750 rtx insn;
751 struct match *matchp;
753 int likely_spilled[MAX_RECOG_OPERANDS];
754 int operand_number;
755 int insn_code_number = recog_memoized (insn);
756 int any_matches = 0;
758 if (insn_code_number < 0)
759 return -1;
761 insn_extract (insn);
762 if (! constrain_operands (insn_code_number, 0))
763 return -1;
765 /* Must initialize this before main loop, because the code for
766 the commutative case may set matches for operands other than
767 the current one. */
769 for (operand_number = insn_n_operands[insn_code_number];
770 --operand_number >= 0; )
771 matchp->with[operand_number] = matchp->commutative[operand_number] = -1;
773 for (operand_number = 0; operand_number < insn_n_operands[insn_code_number];
774 operand_number++)
776 char *p = insn_operand_constraint[insn_code_number][operand_number];
777 int i = 0;
778 char c;
780 likely_spilled[operand_number] = 0;
781 matchp->use[operand_number] = READ;
782 matchp->early_clobber[operand_number] = 0;
784 /* Constraint letters for read and read/write are global. All others
785 are per-constraint. */
786 if (*p == '=')
787 matchp->use[operand_number] = WRITE;
788 else if (*p == '+')
789 matchp->use[operand_number] = READWRITE;
791 for (; *p != '\0' && i < which_alternative; p++)
792 if (*p == ',')
793 i++;
795 /* Process each constraint letter for the alternative that this
796 insn matches, if it matches one yet. */
797 while ((c = *p++) != '\0' && c != ',')
798 switch (c)
800 case '=': case '+': case '?': case '*': case '#':
801 case 'X': case 'm': case '<': case '>': case 'o': case 'V':
802 case 'E': case 'F': case 'G': case 'H':
803 case 's': case 'n':
804 case 'I': case 'J': case 'K': case 'L': case 'M':
805 case 'N': case 'O': case 'P':
806 #ifdef EXTRA_CONSTRAINT
807 case 'Q': case 'R': case 'S': case 'T': case 'U':
808 #endif
809 break;
811 case '&':
812 matchp->early_clobber[operand_number] = 1;
813 break;
815 case '%':
816 matchp->commutative[operand_number] = operand_number + 1;
817 matchp->commutative[operand_number + 1] = operand_number;
818 break;
820 case '0': case '1': case '2': case '3': case '4':
821 case '5': case '6': case '7': case '8': case '9':
822 c -= '0';
823 if (c < operand_number && likely_spilled[c])
824 break;
826 matchp->with[operand_number] = c;
827 any_matches = 1;
828 if (matchp->commutative[operand_number] >= 0)
829 matchp->with[matchp->commutative[operand_number]] = c;
830 break;
832 case 'g': case 'r':
833 if (CLASS_LIKELY_SPILLED_P (GENERAL_REGS))
834 likely_spilled[operand_number] = 1;
835 break;
837 default:
838 if (CLASS_LIKELY_SPILLED_P (REG_CLASS_FROM_LETTER (c)))
839 likely_spilled[operand_number] = 1;
840 break;
845 return any_matches ? insn_code_number : -1;
848 /* Try to replace output operand DST in SET with input operand SRC. SET is
849 the only set in INSN. INSN has just been recgnized and constrained.
850 SRC is operand number OPERAND_NUMBER in INSN.
851 DST is operand number MATCH_NUMBER in INSN.
852 If BACKWARD is nonzero, we have been called in a backward pass.
853 All registers are known to be pseudo-registers.
854 Return nonzero for success. */
856 static int
857 fixup_match (insn, set, src, src_subreg, dst, backward, operand_number,
858 match_number, regmove_dump_file)
859 rtx insn, set, src, src_subreg, dst;
860 int backward, operand_number, match_number;
861 FILE *regmove_dump_file;
863 rtx src_note = find_reg_note (insn, REG_DEAD, src);
864 rtx post_inc = 0, post_inc_set = 0, search_end = 0;
865 rtx overlap = 0;
866 int success = 0;
867 int num_calls = 0, s_num_calls = 0;
868 enum rtx_code code = NOTE;
869 HOST_WIDE_INT insn_const, newconst;
870 rtx dst_note;
871 rtx p;
872 int length, s_length, true_loop_depth;
874 if (src_note == 0)
876 /* Look for (set (regX) (op regA constX))
877 (set (regY) (op regA constY))
878 and change that to
879 (set (regA) (op regA constX)).
880 (set (regY) (op regA constY-constX)).
882 This works for add and shift operations if
883 regA is dead after or set by the second insn. */
885 code = GET_CODE (SET_SRC (set));
886 if ((code == PLUS || code == LSHIFTRT
887 || code == ASHIFT || code == ASHIFTRT)
888 && rtx_equal_p (XEXP (SET_SRC (set), 0), src)
889 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
890 insn_const = INTVAL (XEXP (SET_SRC (set), 1));
891 else if (! stable_but_for_p (SET_SRC (set), src, dst))
892 return 0;
893 else
894 /* We might find a src_note while scanning. */
895 code = NOTE;
898 if (regmove_dump_file)
899 fprintf (regmove_dump_file,
900 "Could fix operand %d of insn %d matching operand %d.\n",
901 operand_number, INSN_UID (insn), match_number);
903 /* If SRC is equivalent to a constant set in a different basic block,
904 then do not use it for this optimization. We want the equivalence
905 so that if we have to reload this register, we can reload the
906 constant, rather than extending the lifespan of the register. */
907 if (reg_is_remote_constant_p (src, insn, get_insns ()))
908 return 0;
910 /* Scan forward to find the next instruction that
911 uses the output operand. If the operand dies here,
912 then replace it in both instructions with
913 operand_number. */
915 for (length = s_length = 0, p = next_insn_for_regmove (insn);
916 p != 0;
917 p = next_insn_for_regmove (p))
919 length++;
920 if (src_note != 0)
921 s_length++;
923 if (reg_set_p (src, p) || reg_set_p (dst, p)
924 || (GET_CODE (PATTERN (p)) == USE
925 && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
926 break;
928 /* See if all of DST dies in P. This test is
929 slightly more conservative than it needs to be. */
930 if ((dst_note = find_regno_note (p, REG_DEAD, REGNO (dst))) != 0
931 && (GET_MODE (XEXP (dst_note, 0)) == GET_MODE (dst)))
933 if (src_note == 0)
935 rtx q;
936 rtx set2;
938 /* If an optimization is done, the value of SRC while P
939 is executed will be changed. Check that this is OK. */
940 if (reg_overlap_mentioned_p (src, PATTERN (p)))
941 break;
943 for (q = p; q != 0; q = next_insn_for_regmove (q))
944 if (reg_overlap_mentioned_p (src, PATTERN (q))
945 || reg_set_p (src, q))
946 break;
948 if (q != 0)
949 set2 = single_set (q);
951 if (q == 0 || set2 == 0 || GET_CODE (SET_SRC (set2)) != code
952 || ! rtx_equal_p (XEXP (SET_SRC (set2), 0), src)
953 || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT
954 || (! rtx_equal_p (SET_DEST (set2), src)
955 && ! find_reg_note (q, REG_DEAD, src)))
957 /* If this is a PLUS, we can still save a register by doing
958 src += insn_const;
960 src -= insn_const; .
961 This also gives opportunities for subsequent
962 optimizations in the backward pass, so do it there. */
963 if (code == PLUS && backward
964 #ifdef HAVE_cc0
965 /* We may not emit an insn directly
966 after P if the latter sets CC0. */
967 && ! sets_cc0_p (PATTERN (p))
968 #endif
971 search_end = q;
972 q = insn;
973 set2 = set;
974 newconst = - insn_const;
975 code = MINUS;
977 else
978 break;
980 else
982 newconst = INTVAL (XEXP (SET_SRC (set2), 1)) - insn_const;
983 /* Reject out of range shifts. */
984 if (code != PLUS
985 && (newconst < 0
986 || (newconst
987 >= GET_MODE_BITSIZE (GET_MODE (SET_SRC (set2))))))
988 break;
990 if (code == PLUS)
992 post_inc = q;
993 if (! rtx_equal_p (SET_DEST (set2), src))
994 post_inc_set = set2;
998 /* We use 1 as last argument to validate_change so that all
999 changes are accepted or rejected together by
1000 apply_change_group when it is called below by
1001 validate_replace_rtx . */
1002 validate_change (q, &XEXP (SET_SRC (set2), 1),
1003 GEN_INT (newconst), 1);
1006 validate_change (insn, recog_operand_loc[match_number], src, 1);
1007 if (validate_replace_rtx (dst, src_subreg, p))
1008 success = 1;
1010 break;
1013 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
1014 break;
1016 if (src_note == 0 && reg_overlap_mentioned_p (src, PATTERN (p)))
1018 /* INSN was already checked to be movable when
1019 we found no REG_DEAD note for src on it. */
1020 overlap = p;
1021 src_note = find_reg_note (p, REG_DEAD, src);
1024 /* If we have passed a call instruction, and the pseudo-reg SRC is not
1025 already live across a call, then don't perform the optimization. */
1026 if (GET_CODE (p) == CALL_INSN)
1028 if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
1029 break;
1031 num_calls++;
1033 if (src_note)
1034 s_num_calls++;
1039 if (! success)
1040 return 0;
1042 true_loop_depth = backward ? 2 - loop_depth : loop_depth;
1044 /* Remove the death note for DST from P. */
1045 remove_note (p, dst_note);
1046 if (code == MINUS)
1048 post_inc = emit_insn_after (copy_rtx (PATTERN (insn)), p);
1050 #if defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT)
1051 if (search_end
1052 && try_auto_increment (search_end, post_inc, 0, src, newconst, 1))
1053 post_inc = 0;
1054 #endif
1056 validate_change (insn, &XEXP (SET_SRC (set), 1),
1057 GEN_INT (insn_const), 0);
1058 REG_N_SETS (REGNO (src))++;
1059 REG_N_REFS (REGNO (src)) += true_loop_depth;
1060 REG_LIVE_LENGTH (REGNO (src))++;
1063 if (overlap)
1065 /* The lifetime of src and dest overlap,
1066 but we can change this by moving insn. */
1067 rtx pat = PATTERN (insn);
1069 if (src_note != 0)
1070 remove_note (overlap, src_note);
1072 #if defined (HAVE_POST_INCREMENT) || defined (HAVE_POST_DECREMENT)
1073 if (code == PLUS
1074 && try_auto_increment (overlap, insn, 0, src, insn_const, 0))
1075 insn = overlap;
1076 else
1077 #endif
1079 rtx notes = REG_NOTES (insn);
1081 emit_insn_after_with_line_notes (pat, PREV_INSN (p), insn);
1082 PUT_CODE (insn, NOTE);
1083 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1084 NOTE_SOURCE_FILE (insn) = 0;
1086 /* emit_insn_after_with_line_notes has no
1087 return value, so search for the new insn. */
1088 for (insn = p; PATTERN (insn) != pat; )
1089 insn = PREV_INSN (insn);
1091 REG_NOTES (insn) = notes;
1095 /* Sometimes we'd generate src = const; src += n;
1096 if so, replace the instruction that set src
1097 in the first place. */
1099 if (! overlap && (code == PLUS || code == MINUS))
1101 rtx note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
1102 rtx q, set2;
1103 int num_calls2 = 0, s_length2 = 0;
1105 if (note != 0 && CONSTANT_P (XEXP (note, 0)))
1107 for (q = prev_insn_for_regmove (insn);
1108 q != 0;
1109 q = prev_insn_for_regmove (q))
1111 s_length2++;
1112 if (reg_set_p (src, q))
1114 set2 = single_set (q);
1115 break;
1118 if (reg_overlap_mentioned_p (src, PATTERN (q)))
1120 q = 0;
1121 break;
1124 if (GET_CODE (p) == CALL_INSN)
1125 num_calls2++;
1128 if (q != 0 && set2 != 0
1129 && rtx_equal_p (SET_DEST (set2), src)
1130 && CONSTANT_P (SET_SRC (set2))
1131 && validate_change (insn, &SET_SRC (set), XEXP (note, 0), 0))
1133 PUT_CODE (q, NOTE);
1134 NOTE_LINE_NUMBER (q) = NOTE_INSN_DELETED;
1135 NOTE_SOURCE_FILE (q) = 0;
1136 REG_N_SETS (REGNO (src))--;
1137 REG_N_CALLS_CROSSED (REGNO (src)) -= num_calls2;
1138 REG_N_REFS (REGNO (src)) -= true_loop_depth;
1139 REG_LIVE_LENGTH (REGNO (src)) -= s_length2;
1140 insn_const = 0;
1145 /* Don't remove this seemingly useless if, it is needed to pair with the
1146 else in the next two conditionally included code blocks. */
1147 if (0)
1150 #if defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT)
1151 else if ((code == PLUS || code == MINUS) && insn_const
1152 && try_auto_increment (p, insn, 0, src, insn_const, 1))
1153 insn = p;
1154 #endif
1155 #if defined (HAVE_POST_INCREMENT) || defined (HAVE_POST_DECREMENT)
1156 else if (post_inc
1157 && try_auto_increment (p, post_inc, post_inc_set, src, newconst, 0))
1158 post_inc = 0;
1159 #endif
1161 #if defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT)
1162 /* If post_inc still prevails, try to find an
1163 insn where it can be used as a pre-in/decrement.
1164 If code is MINUS, this was already tried. */
1165 if (post_inc && code == PLUS
1166 /* Check that newconst is likely to be usable
1167 in a pre-in/decrement before starting the search. */
1168 && (0
1169 #if defined (HAVE_PRE_INCREMENT)
1170 || (newconst > 0 && newconst <= MOVE_MAX)
1171 #endif
1172 #if defined (HAVE_PRE_DECREMENT)
1173 || (newconst < 0 && newconst >= -MOVE_MAX)
1174 #endif
1175 ) && exact_log2 (newconst))
1177 rtx q, inc_dest;
1179 inc_dest = post_inc_set ? SET_DEST (post_inc_set) : src;
1181 for (q = next_insn_for_regmove (post_inc);
1182 q != 0;
1183 q = next_insn_for_regmove (q))
1185 if (! rtx_equal_p (src, inc_dest)
1186 && (reg_overlap_mentioned_p (src, PATTERN (q))
1187 || reg_set_p (src, q)))
1188 break;
1190 if (reg_set_p (inc_dest, q))
1191 break;
1193 if (reg_overlap_mentioned_p (inc_dest, PATTERN (q)))
1195 try_auto_increment (q, post_inc,
1196 post_inc_set, inc_dest, newconst, 1);
1197 break;
1201 #endif /* defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT) */
1203 /* Move the death note for DST to INSN if it is used there. */
1204 if (reg_overlap_mentioned_p (dst, PATTERN (insn)))
1206 XEXP (dst_note, 1) = REG_NOTES (insn);
1207 REG_NOTES (insn) = dst_note;
1210 if (src_note != 0)
1212 /* Move the death note for SRC from INSN to P. */
1213 if (! overlap)
1214 remove_note (insn, src_note);
1216 XEXP (src_note, 1) = REG_NOTES (p);
1217 REG_NOTES (p) = src_note;
1219 REG_N_CALLS_CROSSED (REGNO (src)) += s_num_calls;
1222 REG_N_SETS (REGNO (src))++;
1223 REG_N_SETS (REGNO (dst))--;
1225 REG_N_CALLS_CROSSED (REGNO (dst)) -= num_calls;
1227 REG_LIVE_LENGTH (REGNO (src)) += s_length;
1228 if (REG_LIVE_LENGTH (REGNO (dst)) >= 0)
1230 REG_LIVE_LENGTH (REGNO (dst)) -= length;
1232 /* REG_LIVE_LENGTH is only an approximation after
1233 combine if sched is not run, so make sure that we
1234 still have a reasonable value. */
1235 if (REG_LIVE_LENGTH (REGNO (dst)) < 2)
1236 REG_LIVE_LENGTH (REGNO (dst)) = 2;
1239 /* We assume that a register is used exactly once per insn in the updates
1240 above. If this is not correct, no great harm is done. */
1241 REG_N_REFS (REGNO (src)) += 2 * true_loop_depth;
1242 REG_N_REFS (REGNO (dst)) -= 2 * true_loop_depth;
1244 /* If that was the only time DST was set and DST was not live at the start
1245 of the function, we know that we have no more references to DST; clear
1246 REG_N_REFS so it won't make reload do any work. */
1247 if (REG_N_SETS (REGNO (dst)) == 0
1248 && ! regno_uninitialized (REGNO (dst)))
1249 REG_N_REFS (REGNO (dst)) = 0;
1251 if (regmove_dump_file)
1252 fprintf (regmove_dump_file,
1253 "Fixed operand %d of insn %d matching operand %d.\n",
1254 operand_number, INSN_UID (insn), match_number);
1255 return 1;
1258 /* Return nonzero if X is stable except for mentioning SRC or mentioning
1259 and/or changing DST. If in doubt, presume it is unstable. */
1261 static int
1262 stable_but_for_p (x, src, dst)
1263 rtx x;
1264 rtx src;
1265 rtx dst;
1267 enum rtx_code code = GET_CODE (x);
1269 switch (GET_RTX_CLASS (code))
1271 case '<': case '1': case 'c': case '2': case 'b': case '3':
1273 int i;
1274 char *fmt = GET_RTX_FORMAT (code);
1276 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1277 if (fmt[i] == 'e' && ! stable_but_for_p (XEXP (x, i), src, dst))
1278 return 0;
1280 return 1;
1283 case 'o':
1284 if (x == src || x == dst)
1285 return 1;
1287 /* ... fall through ... */
1289 default:
1290 return ! rtx_unstable_p (x);
1294 /* This is the main routine for this file. */
1296 void
1297 regmove_optimize (f, nregs, regmove_dump_file)
1298 rtx f;
1299 int nregs;
1300 FILE *regmove_dump_file;
1302 rtx insn;
1303 struct match match;
1304 int pass;
1305 int maxregnum = max_reg_num (), i;
1307 regno_src_regno = (int *) alloca (sizeof *regno_src_regno * maxregnum);
1308 for (i = maxregnum; --i >= 0; )
1309 regno_src_regno[i] = -1;
1311 /* Alternate between forward and backward passes, replacing output operands
1312 with input operands. */
1314 loop_depth = 1;
1316 for (pass = 0; pass <= 2; pass++)
1318 if (! flag_regmove && pass >= flag_expensive_optimizations)
1319 return;
1321 if (regmove_dump_file)
1322 fprintf (regmove_dump_file, "Starting %s pass...\n",
1323 pass ? "backward" : "forward");
1325 for (insn = pass ? get_last_insn () : f; insn;
1326 insn = pass ? PREV_INSN (insn) : NEXT_INSN (insn))
1328 rtx set;
1329 int insn_code_number;
1330 int operand_number, match_number;
1332 if (GET_CODE (insn) == NOTE)
1334 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
1335 loop_depth++;
1336 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
1337 loop_depth--;
1340 if ((set = single_set (insn)) == 0)
1341 continue;
1343 if (flag_expensive_optimizations && ! pass
1344 && (GET_CODE (SET_SRC (set)) == SIGN_EXTEND
1345 || GET_CODE (SET_SRC (set)) == ZERO_EXTEND)
1346 && GET_CODE (XEXP (SET_SRC (set), 0)) == REG
1347 && GET_CODE (SET_DEST(set)) == REG)
1348 optimize_reg_extend (insn, set);
1350 if (flag_expensive_optimizations && ! pass
1351 && GET_CODE (SET_SRC (set)) == REG
1352 && GET_CODE (SET_DEST(set)) == REG)
1354 /* If this is a register-register copy where SRC is not dead,
1355 see if we can optimize it. If this optimization succeeds,
1356 it will become a copy where SRC is dead. */
1357 if ((find_reg_note (insn, REG_DEAD, SET_SRC (set))
1358 || optimize_reg_copy_1 (insn, SET_DEST (set),
1359 SET_SRC (set)))
1360 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
1362 /* Similarly for a pseudo-pseudo copy when SRC is dead. */
1363 if (REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1364 optimize_reg_copy_2 (insn, SET_DEST (set), SET_SRC (set));
1366 if (regno_src_regno[REGNO (SET_DEST (set))] < 0
1367 && SET_SRC (set) != SET_DEST (set))
1369 int srcregno = REGNO (SET_SRC(set));
1371 if (regno_src_regno[srcregno] >= 0)
1372 srcregno = regno_src_regno[srcregno];
1374 regno_src_regno[REGNO (SET_DEST (set))] = srcregno;
1379 #ifdef REGISTER_CONSTRAINTS
1380 insn_code_number = find_matches (insn, &match);
1382 if (insn_code_number < 0)
1383 continue;
1385 /* Now scan through the operands looking for a source operand that
1386 is supposed to match the destination operand. Then scan forward
1387 for an instruction which uses the destination operand. If it
1388 dies there, replace the destination in both operands with
1389 the source operand. */
1391 for (operand_number = 0;
1392 operand_number < insn_n_operands[insn_code_number];
1393 operand_number++)
1395 rtx src, dst, src_subreg;
1396 enum reg_class src_class, dst_class;
1398 match_number = match.with[operand_number];
1399 if (match_number < 0)
1400 continue;
1402 src = recog_operand[operand_number];
1403 dst = recog_operand[match_number];
1405 if (GET_CODE (src) != REG)
1406 continue;
1408 src_subreg = src;
1409 if (GET_CODE (dst) == SUBREG
1410 && GET_MODE_SIZE (GET_MODE (dst))
1411 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (dst))))
1413 src_subreg
1414 = gen_rtx_SUBREG (GET_MODE (SUBREG_REG (dst)),
1415 src, SUBREG_WORD (dst));
1416 dst = SUBREG_REG (dst);
1419 if (GET_CODE (dst) != REG
1420 || REGNO (dst) < FIRST_PSEUDO_REGISTER)
1421 continue;
1423 if (REGNO (src) < FIRST_PSEUDO_REGISTER)
1425 if (match.commutative[operand_number] < operand_number)
1426 regno_src_regno[REGNO (dst)] = REGNO (src);
1428 continue;
1431 if (REG_LIVE_LENGTH (REGNO (src)) < 0)
1432 continue;
1434 /* operand_number/src must be a read-only operand, and
1435 match_operand/dst must be a write-only operand. */
1436 if (match.use[operand_number] != READ
1437 || match.use[match_number] != WRITE)
1438 continue;
1440 if (match.early_clobber[match_number]
1441 && count_occurrences (PATTERN (insn), src) > 1)
1442 continue;
1444 /* Make sure match_operand is the destination. */
1445 if (recog_operand[match_number] != SET_DEST (set))
1446 continue;
1448 /* If the operands already match, there is nothing to do.
1449 But in the commutative case, we might find a better match. */
1450 if (operands_match_p (src, dst)
1451 || (match.commutative[operand_number] >= 0
1452 && operands_match_p (recog_operand[match.commutative
1453 [operand_number]],
1454 dst)
1455 && (replacement_quality (recog_operand[match.commutative
1456 [operand_number]])
1457 >= replacement_quality (src))))
1458 continue;
1460 src_class = reg_preferred_class (REGNO (src));
1461 dst_class = reg_preferred_class (REGNO (dst));
1462 if (src_class != dst_class
1463 && (! reg_class_subset_p (src_class, dst_class)
1464 || CLASS_LIKELY_SPILLED_P (src_class))
1465 && (! reg_class_subset_p (dst_class, src_class)
1466 || CLASS_LIKELY_SPILLED_P (dst_class)))
1467 continue;
1469 if (fixup_match (insn, set, src, src_subreg, dst, pass,
1470 operand_number, match_number,
1471 regmove_dump_file))
1472 break;
1477 /* A backward pass. Replace input operands with output operands. */
1479 if (regmove_dump_file)
1480 fprintf (regmove_dump_file, "Starting backward pass...\n");
1482 loop_depth = 1;
1484 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1486 if (GET_CODE (insn) == NOTE)
1488 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
1489 loop_depth++;
1490 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
1491 loop_depth--;
1494 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1496 int insn_code_number = find_matches (insn, &match);
1497 int operand_number, match_number;
1499 if (insn_code_number < 0)
1500 continue;
1502 /* Scan through the operands looking for a destination operand that
1503 is supposed to match a source operand. Then scan backward for
1504 an insn that sets the source operand. If safe, replace the
1505 source operand with the destination operand in both insns. */
1507 for (operand_number = 0;
1508 operand_number < insn_n_operands[insn_code_number];
1509 operand_number++)
1511 rtx set, p, src, dst;
1512 rtx src_note, dst_note;
1513 int success = 0;
1514 int num_calls = 0;
1515 enum reg_class src_class, dst_class;
1516 int length;
1518 match_number = match.with[operand_number];
1519 if (match_number < 0)
1520 continue;
1522 dst = recog_operand[match_number];
1523 src = recog_operand[operand_number];
1525 if (GET_CODE (src) != REG)
1526 continue;
1528 if (GET_CODE (dst) != REG
1529 || REGNO (dst) < FIRST_PSEUDO_REGISTER
1530 || REG_LIVE_LENGTH (REGNO (dst)) < 0)
1531 continue;
1533 /* If the operands already match, there is nothing to do. */
1534 if (operands_match_p (src, dst)
1535 || (match.commutative[operand_number] >= 0
1536 && (operands_match_p
1537 (recog_operand[match.commutative[operand_number]],
1538 dst))))
1539 continue;
1541 if ((set = single_set (insn)) == 0)
1542 continue;
1544 /* match_number/dst must be a write-only operand, and
1545 operand_operand/src must be a read-only operand. */
1546 if (match.use[operand_number] != READ
1547 || match.use[match_number] != WRITE)
1548 continue;
1550 if (match.early_clobber[match_number]
1551 && count_occurrences (PATTERN (insn), src) > 1)
1552 continue;
1554 /* Make sure match_number is the destination. */
1555 if (recog_operand[match_number] != SET_DEST (set))
1556 continue;
1558 if (REGNO (src) < FIRST_PSEUDO_REGISTER)
1560 if (GET_CODE (SET_SRC (set)) == PLUS
1561 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT
1562 && XEXP (SET_SRC (set), 0) == src
1563 && merge_additions (insn, dst, src,
1564 XEXP (SET_SRC (set), 1),
1565 regmove_dump_file))
1566 break;
1568 continue;
1571 src_class = reg_preferred_class (REGNO (src));
1572 dst_class = reg_preferred_class (REGNO (dst));
1573 if (src_class != dst_class
1574 && (! reg_class_subset_p (src_class, dst_class)
1575 || CLASS_LIKELY_SPILLED_P (src_class))
1576 && (! reg_class_subset_p (dst_class, src_class)
1577 || CLASS_LIKELY_SPILLED_P (dst_class)))
1578 continue;
1580 if ((src_note = find_reg_note (insn, REG_DEAD, src)) == 0)
1581 continue;
1583 /* Can not modify an earlier insn to set dst if this insn
1584 uses an old value in the source. */
1585 if (reg_overlap_mentioned_p (dst, SET_SRC (set)))
1586 continue;
1588 if (regmove_dump_file)
1589 fprintf (regmove_dump_file,
1590 "Could fix operand %d of insn %d matching op %d.\n",
1591 operand_number, INSN_UID (insn), match_number);
1593 /* If src is set once in a different basic block and is set
1594 equal to a constant, do not use it for this optimization, as
1595 this would make it no longer equivalent to the constant. */
1596 if (reg_is_remote_constant_p (src, insn, f))
1597 continue;
1599 /* Scan backward to find the first instruction that uses
1600 the input operand. If the operand is set here, then
1601 replace it in both instructions with match_number. */
1603 for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
1605 rtx pset;
1607 if (GET_CODE (p) == CODE_LABEL
1608 || GET_CODE (p) == JUMP_INSN
1609 || (GET_CODE (p) == NOTE
1610 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
1611 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
1612 break;
1614 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
1615 continue;
1617 length++;
1619 /* ??? See if all of SRC is set in P. This test is much
1620 more conservative than it needs to be. */
1621 if ((pset = single_set (p))!= 0
1622 && SET_DEST (pset) == src)
1624 /* We use validate_replace_rtx, in case there
1625 are multiple identical source operands. All of
1626 them have to be changed at the same time. */
1627 if (validate_replace_rtx (src, dst, insn))
1629 if (validate_change (p, &SET_DEST (pset),
1630 dst, 0))
1631 success = 1;
1632 else
1634 /* Change all source operands back.
1635 This modifies the dst as a side-effect. */
1636 validate_replace_rtx (dst, src, insn);
1638 /* Now make sure the dst is right. */
1639 validate_change (insn,
1640 recog_operand_loc[match_number],
1641 dst, 0);
1645 break;
1648 if (reg_overlap_mentioned_p (src, PATTERN (p))
1649 || reg_overlap_mentioned_p (dst, PATTERN (p)))
1650 break;
1652 /* If we have passed a call instruction, and the
1653 pseudo-reg DST is not already live across a call,
1654 then don't perform the optimization. */
1655 if (GET_CODE (p) == CALL_INSN)
1657 num_calls++;
1659 if (REG_N_CALLS_CROSSED (REGNO (dst)) == 0)
1660 break;
1664 if (success)
1666 int dstno, srcno;
1668 /* Remove the death note for SRC from INSN. */
1669 remove_note (insn, src_note);
1671 /* Move the death note for SRC to P if it is used
1672 there. */
1673 if (reg_overlap_mentioned_p (src, PATTERN (p)))
1675 XEXP (src_note, 1) = REG_NOTES (p);
1676 REG_NOTES (p) = src_note;
1679 /* If there is a REG_DEAD note for DST on P, then remove
1680 it, because DST is now set there. */
1681 if ((dst_note = find_reg_note (p, REG_DEAD, dst)))
1682 remove_note (p, dst_note);
1684 dstno = REGNO (dst);
1685 srcno = REGNO (src);
1687 REG_N_SETS (dstno)++;
1688 REG_N_SETS (srcno)--;
1690 REG_N_CALLS_CROSSED (dstno) += num_calls;
1691 REG_N_CALLS_CROSSED (srcno) -= num_calls;
1693 REG_LIVE_LENGTH (dstno) += length;
1694 if (REG_LIVE_LENGTH (srcno) >= 0)
1696 REG_LIVE_LENGTH (srcno) -= length;
1698 /* REG_LIVE_LENGTH is only an approximation after
1699 combine if sched is not run, so make sure that we
1700 still have a reasonable value. */
1701 if (REG_LIVE_LENGTH (srcno) < 2)
1702 REG_LIVE_LENGTH (srcno) = 2;
1705 /* We assume that a register is used exactly once per
1706 insn in the updates above. If this is not correct,
1707 no great harm is done. */
1709 REG_N_REFS (dstno) += 2 * loop_depth;
1710 REG_N_REFS (srcno) -= 2 * loop_depth;
1712 /* If that was the only time SRC was set and SRC was not
1713 live at the start of the function, we know we have no
1714 more references to SRC; clear REG_N_REFS so it won't
1715 make reload do any work. */
1716 if (REG_N_SETS (REGNO (src)) == 0
1717 && ! regno_uninitialized (REGNO (src)))
1718 REG_N_REFS (REGNO (src)) = 0;
1720 if (regmove_dump_file)
1721 fprintf (regmove_dump_file,
1722 "Fixed operand %d of insn %d matching op %d.\n",
1723 operand_number, INSN_UID (insn), match_number);
1725 break;
1730 #endif /* REGISTER_CONSTRAINTS */
1733 /* Test if regmove seems profitable for this target. Regmove is useful only
1734 if some common patterns are two address, i.e. require matching constraints,
1735 so we check that condition here.
1737 This can only be called after we can allocate RTL. */
1740 regmove_profitable_p ()
1742 #ifdef REGISTER_CONSTRAINTS
1743 struct match match;
1744 enum machine_mode mode;
1745 optab tstoptab = add_optab;
1746 char *free_point = (char *) oballoc (0);
1748 do /* check add_optab and ashl_optab */
1749 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1750 mode = GET_MODE_WIDER_MODE (mode))
1752 int icode = (int) tstoptab->handlers[(int) mode].insn_code;
1753 rtx reg0, reg1, reg2, pat;
1754 int i;
1756 if (GET_MODE_BITSIZE (mode) < 32 || icode == CODE_FOR_nothing)
1757 continue;
1759 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1760 if (TEST_HARD_REG_BIT (reg_class_contents[GENERAL_REGS], i))
1761 break;
1763 if (i + 2 >= FIRST_PSEUDO_REGISTER)
1764 break;
1766 reg0 = gen_rtx_REG (insn_operand_mode[icode][0], i);
1767 reg1 = gen_rtx_REG (insn_operand_mode[icode][1], i + 1);
1769 /* Use CONST_INT for a shift since some machines do odd things
1770 in the register case (e.g., PA). */
1771 if (tstoptab->code == ASHIFT)
1772 reg2 = const1_rtx;
1773 else
1774 reg2 = gen_rtx_REG (insn_operand_mode[icode][2], i + 2);
1776 if (! (*insn_operand_predicate[icode][0]) (reg0, VOIDmode)
1777 || ! (*insn_operand_predicate[icode][1]) (reg1, VOIDmode)
1778 || ! (*insn_operand_predicate[icode][2]) (reg2, VOIDmode))
1779 break;
1781 pat = GEN_FCN (icode) (reg0, reg1, reg2);
1782 if (pat == 0)
1783 continue;
1785 if (GET_CODE (pat) == SEQUENCE)
1786 pat = XVECEXP (pat, 0, XVECLEN (pat, 0) - 1);
1787 else
1788 pat = make_insn_raw (pat);
1790 if (! single_set (pat)
1791 || GET_CODE (SET_SRC (single_set (pat))) != tstoptab->code)
1792 /* Unexpected complexity; don't need to handle this unless
1793 we find a machine where this occurs and regmove should
1794 be enabled. */
1795 break;
1797 if (find_matches (pat, &match) >= 0)
1799 obfree (free_point);
1800 return 1;
1803 break;
1805 while (tstoptab != ashl_optab && (tstoptab = ashl_optab, 1));
1807 obfree (free_point);
1808 #endif /* REGISTER_CONSTRAINTS */
1810 return 0;