OSDN Git Service

* rtl.h (addr_diff_vec_flags): New typedef.
[pf3gnuchains/gcc-fork.git] / gcc / regmove.c
1 /* Move registers around to reduce number of move instructions needed.
2    Copyright (C) 1987, 88, 89, 92-97, 1998 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
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.
10
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.
15
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.  */
19
20
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.  */
25
26 #include "config.h"
27 #ifdef __STDC__
28 #include <stdarg.h>
29 #else
30 #include <varargs.h>
31 #endif
32
33 /* Must precede rtl.h for FFS.  */
34 #include <stdio.h>
35
36 #include "rtl.h"
37 #include "insn-config.h"
38 #include "recog.h"
39 #include "output.h"
40 #include "reload.h"
41 #include "regs.h"
42 #include "hard-reg-set.h"
43 #include "flags.h"
44 #include "expr.h"
45 #include "insn-flags.h"
46
47 static int optimize_reg_copy_1  PROTO((rtx, rtx, rtx));
48 static void optimize_reg_copy_2 PROTO((rtx, rtx, rtx));
49 static void optimize_reg_copy_3 PROTO((rtx, rtx, rtx));
50
51 struct match {
52   int with[MAX_RECOG_OPERANDS];
53   enum { READ, WRITE, READWRITE } use[MAX_RECOG_OPERANDS];
54   int commutative[MAX_RECOG_OPERANDS];
55   int early_clobber[MAX_RECOG_OPERANDS];
56 };
57
58 static int find_matches PROTO((rtx, struct match *));
59 static int fixup_match_1 PROTO((rtx, rtx, rtx, rtx, rtx, int, int, int, FILE *))
60 ;
61 static int reg_is_remote_constant_p PROTO((rtx, rtx, rtx));
62 static int stable_but_for_p PROTO((rtx, rtx, rtx));
63 static int loop_depth;
64
65 #ifdef AUTO_INC_DEC
66
67 /* INC_INSN is an instruction that adds INCREMENT to REG.
68    Try to fold INC_INSN as a post/pre in/decrement into INSN.
69    Iff INC_INSN_SET is nonzero, inc_insn has a destination different from src.
70    Return nonzero for success.  */
71 static int
72 try_auto_increment (insn, inc_insn, inc_insn_set, reg, increment, pre)
73      rtx reg, insn, inc_insn ,inc_insn_set;
74      HOST_WIDE_INT increment;
75      int pre;
76 {
77   enum rtx_code inc_code;
78
79   rtx pset = single_set (insn);
80   if (pset)
81     {
82       /* Can't use the size of SET_SRC, we might have something like
83          (sign_extend:SI (mem:QI ...  */
84       rtx use = find_use_as_address (pset, reg, 0);
85       if (use != 0 && use != (rtx) 1)
86         {
87           int size = GET_MODE_SIZE (GET_MODE (use));
88           if (0
89 #ifdef HAVE_POST_INCREMENT
90               || (pre == 0 && (inc_code = POST_INC, increment == size))
91 #endif
92 #ifdef HAVE_PRE_INCREMENT
93               || (pre == 1 && (inc_code = PRE_INC, increment == size))
94 #endif
95 #ifdef HAVE_POST_DECREMENT
96               || (pre == 0 && (inc_code = POST_DEC, increment == -size))
97 #endif
98 #ifdef HAVE_PRE_DECREMENT
99               || (pre == 1 && (inc_code = PRE_DEC, increment == -size))
100 #endif
101           )
102             {
103               if (inc_insn_set)
104                 validate_change
105                   (inc_insn, 
106                    &SET_SRC (inc_insn_set),
107                    XEXP (SET_SRC (inc_insn_set), 0), 1);
108               validate_change (insn, &XEXP (use, 0),
109                                gen_rtx_fmt_e (inc_code, Pmode, reg), 1);
110               if (apply_change_group ())
111                 {
112                   REG_NOTES (insn)
113                     = gen_rtx_EXPR_LIST (REG_INC,
114                                          reg, REG_NOTES (insn));
115                   if (! inc_insn_set)
116                     {
117                       PUT_CODE (inc_insn, NOTE);
118                       NOTE_LINE_NUMBER (inc_insn) = NOTE_INSN_DELETED;
119                       NOTE_SOURCE_FILE (inc_insn) = 0;
120                     }
121                   return 1;
122                 }
123             }
124         }
125     }
126   return 0;
127 }
128 #endif  /* AUTO_INC_DEC */
129
130 static int *regno_src_regno;
131
132 /* Indicate how good a choice REG (which appears as a source) is to replace
133    a destination register with.  The higher the returned value, the better
134    the choice.  The main objective is to avoid using a register that is
135    a candidate for tying to a hard register, since the output might in
136    turn be a candidate to be tied to a different hard register.  */
137 int
138 replacement_quality(reg)
139      rtx reg;
140 {
141   int src_regno;
142
143   /* Bad if this isn't a register at all.  */
144   if (GET_CODE (reg) != REG)
145     return 0;
146
147   /* If this register is not meant to get a hard register,
148      it is a poor choice.  */
149   if (REG_LIVE_LENGTH (REGNO (reg)) < 0)
150     return 0;
151
152   src_regno = regno_src_regno[REGNO (reg)];
153
154   /* If it was not copied from another register, it is fine.  */
155   if (src_regno < 0)
156     return 3;
157
158   /* Copied from a hard register?  */
159   if (src_regno < FIRST_PSEUDO_REGISTER)
160     return 1;
161
162   /* Copied from a pseudo register - not as bad as from a hard register,
163      yet still cumbersome, since the register live length will be lengthened
164      when the registers get tied.  */
165   return 2;
166 }
167
168 /* INSN is a copy from SRC to DEST, both registers, and SRC does not die
169    in INSN.
170
171    Search forward to see if SRC dies before either it or DEST is modified,
172    but don't scan past the end of a basic block.  If so, we can replace SRC
173    with DEST and let SRC die in INSN. 
174
175    This will reduce the number of registers live in that range and may enable
176    DEST to be tied to SRC, thus often saving one register in addition to a
177    register-register copy.  */
178
179 static int
180 optimize_reg_copy_1 (insn, dest, src)
181      rtx insn;
182      rtx dest;
183      rtx src;
184 {
185   rtx p, q;
186   rtx note;
187   rtx dest_death = 0;
188   int sregno = REGNO (src);
189   int dregno = REGNO (dest);
190
191   /* We don't want to mess with hard regs if register classes are small. */
192   if (sregno == dregno
193       || (SMALL_REGISTER_CLASSES
194           && (sregno < FIRST_PSEUDO_REGISTER
195               || dregno < FIRST_PSEUDO_REGISTER))
196       /* We don't see all updates to SP if they are in an auto-inc memory
197          reference, so we must disallow this optimization on them.  */
198       || sregno == STACK_POINTER_REGNUM || dregno == STACK_POINTER_REGNUM)
199     return 0;
200
201   for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
202     {
203       if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
204           || (GET_CODE (p) == NOTE
205               && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
206                   || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
207         break;
208
209       /* ??? We can't scan past the end of a basic block without updating
210          the register lifetime info (REG_DEAD/basic_block_live_at_start).
211          A CALL_INSN might be the last insn of a basic block, if it is inside
212          an EH region.  There is no easy way to tell, so we just always break
213          when we see a CALL_INSN if flag_exceptions is nonzero.  */
214       if (flag_exceptions && GET_CODE (p) == CALL_INSN)
215         break;
216
217       if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
218         continue;
219
220       if (reg_set_p (src, p) || reg_set_p (dest, p)
221           /* Don't change a USE of a register.  */
222           || (GET_CODE (PATTERN (p)) == USE
223               && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
224         break;
225
226       /* See if all of SRC dies in P.  This test is slightly more
227          conservative than it needs to be.  */
228       if ((note = find_regno_note (p, REG_DEAD, sregno)) != 0
229           && GET_MODE (XEXP (note, 0)) == GET_MODE (src))
230         {
231           int failed = 0;
232           int length = 0;
233           int d_length = 0;
234           int n_calls = 0;
235           int d_n_calls = 0;
236
237           /* We can do the optimization.  Scan forward from INSN again,
238              replacing regs as we go.  Set FAILED if a replacement can't
239              be done.  In that case, we can't move the death note for SRC.
240              This should be rare.  */
241
242           /* Set to stop at next insn.  */
243           for (q = next_real_insn (insn);
244                q != next_real_insn (p);
245                q = next_real_insn (q))
246             {
247               if (reg_overlap_mentioned_p (src, PATTERN (q)))
248                 {
249                   /* If SRC is a hard register, we might miss some
250                      overlapping registers with validate_replace_rtx,
251                      so we would have to undo it.  We can't if DEST is
252                      present in the insn, so fail in that combination
253                      of cases.  */
254                   if (sregno < FIRST_PSEUDO_REGISTER
255                       && reg_mentioned_p (dest, PATTERN (q)))
256                     failed = 1;
257
258                   /* Replace all uses and make sure that the register
259                      isn't still present.  */
260                   else if (validate_replace_rtx (src, dest, q)
261                            && (sregno >= FIRST_PSEUDO_REGISTER
262                                || ! reg_overlap_mentioned_p (src,
263                                                              PATTERN (q))))
264                     {
265                       /* We assume that a register is used exactly once per
266                          insn in the updates below.  If this is not correct,
267                          no great harm is done.  */
268                       if (sregno >= FIRST_PSEUDO_REGISTER)
269                         REG_N_REFS (sregno) -= loop_depth;
270                       if (dregno >= FIRST_PSEUDO_REGISTER)
271                         REG_N_REFS (dregno) += loop_depth;
272                     }
273                   else
274                     {
275                       validate_replace_rtx (dest, src, q);
276                       failed = 1;
277                     }
278                 }
279
280               /* Count the insns and CALL_INSNs passed.  If we passed the
281                  death note of DEST, show increased live length.  */
282               length++;
283               if (dest_death)
284                 d_length++;
285
286               /* If the insn in which SRC dies is a CALL_INSN, don't count it
287                  as a call that has been crossed.  Otherwise, count it.  */
288               if (q != p && GET_CODE (q) == CALL_INSN)
289                 {
290                   n_calls++;
291                   if (dest_death)
292                     d_n_calls++;
293                 }
294
295               /* If DEST dies here, remove the death note and save it for
296                  later.  Make sure ALL of DEST dies here; again, this is
297                  overly conservative.  */
298               if (dest_death == 0
299                   && (dest_death = find_regno_note (q, REG_DEAD, dregno)) != 0)
300                 {
301                   if (GET_MODE (XEXP (dest_death, 0)) != GET_MODE (dest))
302                     failed = 1, dest_death = 0;
303                   else
304                     remove_note (q, dest_death);
305                 }
306             }
307
308           if (! failed)
309             {
310               if (sregno >= FIRST_PSEUDO_REGISTER)
311                 {
312                   if (REG_LIVE_LENGTH (sregno) >= 0)
313                     {
314                       REG_LIVE_LENGTH (sregno) -= length;
315                       /* REG_LIVE_LENGTH is only an approximation after
316                          combine if sched is not run, so make sure that we
317                          still have a reasonable value.  */
318                       if (REG_LIVE_LENGTH (sregno) < 2)
319                         REG_LIVE_LENGTH (sregno) = 2;
320                     }
321
322                   REG_N_CALLS_CROSSED (sregno) -= n_calls;
323                 }
324
325               if (dregno >= FIRST_PSEUDO_REGISTER)
326                 {
327                   if (REG_LIVE_LENGTH (dregno) >= 0)
328                     REG_LIVE_LENGTH (dregno) += d_length;
329
330                   REG_N_CALLS_CROSSED (dregno) += d_n_calls;
331                 }
332
333               /* Move death note of SRC from P to INSN.  */
334               remove_note (p, note);
335               XEXP (note, 1) = REG_NOTES (insn);
336               REG_NOTES (insn) = note;
337             }
338
339           /* Put death note of DEST on P if we saw it die.  */
340           if (dest_death)
341             {
342               XEXP (dest_death, 1) = REG_NOTES (p);
343               REG_NOTES (p) = dest_death;
344             }
345
346           return ! failed;
347         }
348
349       /* If SRC is a hard register which is set or killed in some other
350          way, we can't do this optimization.  */
351       else if (sregno < FIRST_PSEUDO_REGISTER
352                && dead_or_set_p (p, src))
353         break;
354     }
355   return 0;
356 }
357 \f
358 /* INSN is a copy of SRC to DEST, in which SRC dies.  See if we now have
359    a sequence of insns that modify DEST followed by an insn that sets
360    SRC to DEST in which DEST dies, with no prior modification of DEST.
361    (There is no need to check if the insns in between actually modify
362    DEST.  We should not have cases where DEST is not modified, but
363    the optimization is safe if no such modification is detected.)
364    In that case, we can replace all uses of DEST, starting with INSN and
365    ending with the set of SRC to DEST, with SRC.  We do not do this
366    optimization if a CALL_INSN is crossed unless SRC already crosses a
367    call or if DEST dies before the copy back to SRC.
368
369    It is assumed that DEST and SRC are pseudos; it is too complicated to do
370    this for hard registers since the substitutions we may make might fail.  */
371
372 static void
373 optimize_reg_copy_2 (insn, dest, src)
374      rtx insn;
375      rtx dest;
376      rtx src;
377 {
378   rtx p, q;
379   rtx set;
380   int sregno = REGNO (src);
381   int dregno = REGNO (dest);
382
383   for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
384     {
385       if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
386           || (GET_CODE (p) == NOTE
387               && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
388                   || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
389         break;
390
391       /* ??? We can't scan past the end of a basic block without updating
392          the register lifetime info (REG_DEAD/basic_block_live_at_start).
393          A CALL_INSN might be the last insn of a basic block, if it is inside
394          an EH region.  There is no easy way to tell, so we just always break
395          when we see a CALL_INSN if flag_exceptions is nonzero.  */
396       if (flag_exceptions && GET_CODE (p) == CALL_INSN)
397         break;
398
399       if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
400         continue;
401
402       set = single_set (p);
403       if (set && SET_SRC (set) == dest && SET_DEST (set) == src
404           && find_reg_note (p, REG_DEAD, dest))
405         {
406           /* We can do the optimization.  Scan forward from INSN again,
407              replacing regs as we go.  */
408
409           /* Set to stop at next insn.  */
410           for (q = insn; q != NEXT_INSN (p); q = NEXT_INSN (q))
411             if (GET_RTX_CLASS (GET_CODE (q)) == 'i')
412               {
413                 if (reg_mentioned_p (dest, PATTERN (q)))
414                   {
415                     PATTERN (q) = replace_rtx (PATTERN (q), dest, src);
416
417                     /* We assume that a register is used exactly once per
418                        insn in the updates below.  If this is not correct,
419                        no great harm is done.  */
420                     REG_N_REFS (dregno) -= loop_depth;
421                     REG_N_REFS (sregno) += loop_depth;
422                   }
423
424
425               if (GET_CODE (q) == CALL_INSN)
426                 {
427                   REG_N_CALLS_CROSSED (dregno)--;
428                   REG_N_CALLS_CROSSED (sregno)++;
429                 }
430               }
431
432           remove_note (p, find_reg_note (p, REG_DEAD, dest));
433           REG_N_DEATHS (dregno)--;
434           remove_note (insn, find_reg_note (insn, REG_DEAD, src));
435           REG_N_DEATHS (sregno)--;
436           return;
437         }
438
439       if (reg_set_p (src, p)
440           || find_reg_note (p, REG_DEAD, dest)
441           || (GET_CODE (p) == CALL_INSN && REG_N_CALLS_CROSSED (sregno) == 0))
442         break;
443     }
444 }
445 /* INSN is a ZERO_EXTEND or SIGN_EXTEND of SRC to DEST.
446    Look if SRC dies there, and if it is only set once, by loading
447    it from memory.  If so, try to encorporate the zero/sign extension
448    into the memory read, change SRC to the mode of DEST, and alter
449    the remaining accesses to use the appropriate SUBREG.  This allows
450    SRC and DEST to be tied later.  */
451 static void
452 optimize_reg_copy_3 (insn, dest, src)
453      rtx insn;
454      rtx dest;
455      rtx src;
456 {
457   rtx src_reg = XEXP (src, 0);
458   int src_no = REGNO (src_reg);
459   int dst_no = REGNO (dest);
460   rtx p, set, subreg;
461   enum machine_mode old_mode;
462
463   if (src_no < FIRST_PSEUDO_REGISTER
464       || dst_no < FIRST_PSEUDO_REGISTER
465       || ! find_reg_note (insn, REG_DEAD, src_reg)
466       || REG_N_SETS (src_no) != 1)
467     return;
468   for (p = PREV_INSN (insn); ! reg_set_p (src_reg, p); p = PREV_INSN (p))
469     {
470       if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
471           || (GET_CODE (p) == NOTE
472               && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
473                   || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
474         return;
475
476       /* ??? We can't scan past the end of a basic block without updating
477          the register lifetime info (REG_DEAD/basic_block_live_at_start).
478          A CALL_INSN might be the last insn of a basic block, if it is inside
479          an EH region.  There is no easy way to tell, so we just always break
480          when we see a CALL_INSN if flag_exceptions is nonzero.  */
481       if (flag_exceptions && GET_CODE (p) == CALL_INSN)
482         return;
483
484       if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
485         continue;
486     }
487   if (! (set = single_set (p))
488       || GET_CODE (SET_SRC (set)) != MEM
489       || SET_DEST (set) != src_reg)
490     return;
491   old_mode = GET_MODE (src_reg);
492   PUT_MODE (src_reg, GET_MODE (src));
493   XEXP (src, 0) = SET_SRC (set);
494   if (! validate_change (p, &SET_SRC (set), src, 0))
495     {
496       PUT_MODE (src_reg, old_mode);
497       XEXP (src, 0) = src_reg;
498       return;
499     }
500   subreg = gen_rtx_SUBREG (old_mode, src_reg, 0);
501   while (p = NEXT_INSN (p), p != insn)
502     {
503       if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
504         continue;
505       validate_replace_rtx (src_reg, subreg, p);
506     }
507   validate_replace_rtx (src, src_reg, insn);
508 }
509
510 /* Return whether REG is set in only one location, and is set to a
511    constant, but is set in a different basic block from INSN (an
512    instructions which uses REG).  In this case REG is equivalent to a
513    constant, and we don't want to break that equivalence, because that
514    may increase register pressure and make reload harder.  If REG is
515    set in the same basic block as INSN, we don't worry about it,
516    because we'll probably need a register anyhow (??? but what if REG
517    is used in a different basic block as well as this one?).  FIRST is
518    the first insn in the function.  */
519
520 static int
521 reg_is_remote_constant_p (reg, insn, first)
522      rtx reg;
523      rtx insn;
524      rtx first;
525 {
526   register rtx p;
527
528   if (REG_N_SETS (REGNO (reg)) != 1)
529     return 0;
530
531   /* Look for the set.  */
532   for (p = LOG_LINKS (insn); p; p = XEXP (p, 1))
533     {
534       rtx s;
535
536       if (REG_NOTE_KIND (p) != 0)
537         continue;
538       s = single_set (XEXP (p, 0));
539       if (s != 0
540           && GET_CODE (SET_DEST (s)) == REG
541           && REGNO (SET_DEST (s)) == REGNO (reg))
542         {
543           /* The register is set in the same basic block.  */
544           return 0;
545         }
546     }
547
548   for (p = first; p && p != insn; p = NEXT_INSN (p))
549     {
550       rtx s;
551
552       if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
553         continue;
554       s = single_set (p);
555       if (s != 0
556           && GET_CODE (SET_DEST (s)) == REG
557           && REGNO (SET_DEST (s)) == REGNO (reg))
558         {
559           /* This is the instruction which sets REG.  If there is a
560              REG_EQUAL note, then REG is equivalent to a constant.  */
561           if (find_reg_note (p, REG_EQUAL, NULL_RTX))
562             return 1;
563           return 0;
564         }
565     }
566
567   return 0;
568 }
569
570 /* cse disrupts preincrement / postdecrement squences when it finds a
571    hard register as ultimate source, like the frame pointer.  */
572 int
573 fixup_match_2 (insn, dst, src, offset, regmove_dump_file)
574      rtx insn, dst, src, offset;
575      FILE *regmove_dump_file;
576 {
577   rtx p, dst_death = 0;
578   int length, num_calls = 0;
579
580   /* If SRC dies in INSN, we'd have to move the death note.  This is
581      considered to be very unlikely, so we just skip the optimization
582      in this case.  */
583   if (find_regno_note (insn, REG_DEAD, REGNO (src)))
584     return 0;
585
586   /* Scan backward to find the first instruction that sets DST.  */
587
588   for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
589     {
590       rtx pset;
591
592       if (GET_CODE (p) == CODE_LABEL
593           || GET_CODE (p) == JUMP_INSN
594           || (GET_CODE (p) == NOTE
595               && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
596                   || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
597         break;
598
599       /* ??? We can't scan past the end of a basic block without updating
600          the register lifetime info (REG_DEAD/basic_block_live_at_start).
601          A CALL_INSN might be the last insn of a basic block, if it is inside
602          an EH region.  There is no easy way to tell, so we just always break
603          when we see a CALL_INSN if flag_exceptions is nonzero.  */
604       if (flag_exceptions && GET_CODE (p) == CALL_INSN)
605         break;
606
607       if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
608         continue;
609
610       if (find_regno_note (p, REG_DEAD, REGNO (dst)))
611         dst_death = p;
612       if (! dst_death)
613         length++;
614
615       pset = single_set (p);
616       if (pset && SET_DEST (pset) == dst
617           && GET_CODE (SET_SRC (pset)) == PLUS
618           && XEXP (SET_SRC (pset), 0) == src
619           && GET_CODE (XEXP (SET_SRC (pset), 1)) == CONST_INT)
620         {
621           HOST_WIDE_INT newconst
622             = INTVAL (offset) - INTVAL (XEXP (SET_SRC (pset), 1));
623           if (validate_change (insn, &PATTERN (insn),
624                                gen_addsi3 (dst, dst, GEN_INT (newconst)), 0))
625             {
626               /* Remove the death note for DST from DST_DEATH.  */
627               if (dst_death)
628                 {
629                   remove_death (REGNO (dst), dst_death);
630                   REG_LIVE_LENGTH (REGNO (dst)) += length;
631                   REG_N_CALLS_CROSSED (REGNO (dst)) += num_calls;
632                 }
633
634               REG_N_REFS (REGNO (dst)) += loop_depth;
635               REG_N_REFS (REGNO (src)) -= loop_depth;
636
637               if (regmove_dump_file)
638                 fprintf (regmove_dump_file,
639                          "Fixed operand of insn %d.\n",
640                           INSN_UID (insn));
641
642 #ifdef AUTO_INC_DEC
643               for (p = PREV_INSN (insn); p; p = PREV_INSN (p))
644                 {
645                   if (GET_CODE (p) == CODE_LABEL
646                       || GET_CODE (p) == JUMP_INSN
647                       || (GET_CODE (p) == NOTE
648                           && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
649                               || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
650                     break;
651                   if (reg_overlap_mentioned_p (dst, PATTERN (p)))
652                     {
653                       if (try_auto_increment (p, insn, 0, dst, newconst, 0))
654                         return 1;
655                       break;
656                     }
657                 }
658               for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
659                 {
660                   if (GET_CODE (p) == CODE_LABEL
661                       || GET_CODE (p) == JUMP_INSN
662                       || (GET_CODE (p) == NOTE
663                           && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
664                               || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
665                     break;
666                   if (reg_overlap_mentioned_p (dst, PATTERN (p)))
667                     {
668                       try_auto_increment (p, insn, 0, dst, newconst, 1);
669                       break;
670                     }
671                 }
672 #endif
673               return 1;
674             }
675         }
676
677       if (reg_set_p (dst, PATTERN (p)))
678         break;
679
680       /* If we have passed a call instruction, and the
681          pseudo-reg SRC is not already live across a call,
682          then don't perform the optimization.  */
683       /* reg_set_p is overly conservative for CALL_INSNS, thinks that all
684          hard regs are clobbered.  Thus, we only use it for src for
685          non-call insns.  */
686       if (GET_CODE (p) == CALL_INSN)
687         {
688           if (! dst_death)
689             num_calls++;
690
691           if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
692             break;
693
694           if (call_used_regs [REGNO (dst)]
695               || find_reg_fusage (p, CLOBBER, dst))
696             break;
697         }
698       else if (reg_set_p (src, PATTERN (p)))
699         break;
700     }
701
702   return 0;
703 }
704
705 void
706 regmove_optimize (f, nregs, regmove_dump_file)
707      rtx f;
708      int nregs;
709      FILE *regmove_dump_file;
710 {
711   rtx insn;
712   struct match match;
713   int pass;
714   int maxregnum = max_reg_num (), i;
715
716   regno_src_regno = (int *)alloca (sizeof *regno_src_regno * maxregnum);
717   for (i = maxregnum; --i >= 0; ) regno_src_regno[i] = -1;
718
719   /* A forward/backward pass.  Replace output operands with input operands.  */
720
721   loop_depth = 1;
722
723   for (pass = 0; pass <= 2; pass++)
724     {
725       if (! flag_regmove && pass >= flag_expensive_optimizations)
726         return;
727
728       if (regmove_dump_file)
729         fprintf (regmove_dump_file, "Starting %s pass...\n",
730                  pass ? "backward" : "forward");
731
732       for (insn = pass ? get_last_insn () : f; insn;
733            insn = pass ? PREV_INSN (insn) : NEXT_INSN (insn))
734         {
735           rtx set;
736           int insn_code_number;
737           int operand_number, match_number;
738
739           if (GET_CODE (insn) == NOTE)
740             {
741               if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
742                 loop_depth++;
743               else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
744                 loop_depth--;
745             }
746
747           set = single_set (insn);
748           if (! set)
749             continue;
750
751           if (flag_expensive_optimizations && ! pass
752               && (GET_CODE (SET_SRC (set)) == SIGN_EXTEND
753                   || GET_CODE (SET_SRC (set)) == ZERO_EXTEND)
754               && GET_CODE (XEXP (SET_SRC (set), 0)) == REG
755               && GET_CODE (SET_DEST(set)) == REG)
756             optimize_reg_copy_3 (insn, SET_DEST (set), SET_SRC (set));
757
758           if (flag_expensive_optimizations && ! pass
759               && GET_CODE (SET_SRC (set)) == REG
760               && GET_CODE (SET_DEST(set)) == REG)
761             {
762               /* If this is a register-register copy where SRC is not dead,
763                  see if we can optimize it.  If this optimization succeeds,
764                  it will become a copy where SRC is dead.  */
765               if ((find_reg_note (insn, REG_DEAD, SET_SRC (set))
766                    || optimize_reg_copy_1 (insn, SET_DEST (set), SET_SRC (set)))
767                   && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
768                 {
769                   /* Similarly for a pseudo-pseudo copy when SRC is dead.  */
770                   if (REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
771                     optimize_reg_copy_2 (insn, SET_DEST (set), SET_SRC (set));
772                   if (regno_src_regno[REGNO (SET_DEST (set))] < 0
773                       && SET_SRC (set) != SET_DEST (set))
774                     {
775                       int srcregno = REGNO (SET_SRC(set));
776                       if (regno_src_regno[srcregno] >= 0)
777                         srcregno = regno_src_regno[srcregno];
778                       regno_src_regno[REGNO (SET_DEST (set))] = srcregno;
779                     }
780                 }
781             }
782 #ifdef REGISTER_CONSTRAINTS
783           insn_code_number
784             = find_matches (insn, &match);
785
786           if (insn_code_number < 0)
787             continue;
788
789           /* Now scan through the operands looking for a source operand
790              which is supposed to match the destination operand.
791              Then scan forward for an instruction which uses the dest
792              operand.
793              If it dies there, then replace the dest in both operands with
794              the source operand.  */
795
796           for (operand_number = 0;
797                operand_number < insn_n_operands[insn_code_number];
798                operand_number++)
799             {
800               rtx src, dst, src_subreg;
801               enum reg_class src_class, dst_class;
802
803               match_number = match.with[operand_number];
804
805               /* Nothing to do if the two operands aren't supposed to match.  */
806               if (match_number < 0)
807                 continue;
808
809               src = recog_operand[operand_number];
810               dst = recog_operand[match_number];
811
812               if (GET_CODE (src) != REG)
813                 continue;
814
815               src_subreg = src;
816               if (GET_CODE (dst) == SUBREG
817                   && GET_MODE_SIZE (GET_MODE (dst))
818                      >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (dst))))
819                 {
820                   src_subreg
821                     = gen_rtx_SUBREG (GET_MODE (SUBREG_REG (dst)),
822                                       src, SUBREG_WORD (dst));
823                   dst = SUBREG_REG (dst);
824                 }
825               if (GET_CODE (dst) != REG
826                   || REGNO (dst) < FIRST_PSEUDO_REGISTER)
827                 continue;
828
829               if (REGNO (src) < FIRST_PSEUDO_REGISTER)
830                 {
831                   if (match.commutative[operand_number] < operand_number)
832                     regno_src_regno[REGNO (dst)] = REGNO (src);
833                   continue;
834                 }
835
836               if (REG_LIVE_LENGTH (REGNO (src)) < 0)
837                 continue;
838
839               /* operand_number/src must be a read-only operand, and
840                  match_operand/dst must be a write-only operand.  */
841               if (match.use[operand_number] != READ
842                   || match.use[match_number] != WRITE)
843                 continue;
844
845               if (match.early_clobber[match_number]
846                   && count_occurrences (PATTERN (insn), src) > 1)
847                 continue;
848
849               /* Make sure match_operand is the destination.  */
850               if (recog_operand[match_number] != SET_DEST (set))
851                 continue;
852
853               /* If the operands already match, then there is nothing to do.  */
854               /* But in the commutative case, we might find a better match.  */
855               if (operands_match_p (src, dst)
856                   || (match.commutative[operand_number] >= 0
857                       && operands_match_p (recog_operand[match.commutative
858                                                          [operand_number]], dst)
859                       && (replacement_quality (recog_operand[match.commutative
860                                                              [operand_number]])
861                           >= replacement_quality (src))))
862                 continue;
863
864               src_class = reg_preferred_class (REGNO (src));
865               dst_class = reg_preferred_class (REGNO (dst));
866               if (src_class != dst_class
867                   && (! reg_class_subset_p (src_class, dst_class)
868                       || CLASS_LIKELY_SPILLED_P (src_class))
869                   && (! reg_class_subset_p (dst_class, src_class)
870                       || CLASS_LIKELY_SPILLED_P (dst_class)))
871                 continue;
872           
873               if (fixup_match_1 (insn, set, src, src_subreg, dst, pass,
874                                  operand_number, match_number,
875                                  regmove_dump_file))
876                 break;
877             }
878         }
879     }
880
881   /* A backward pass.  Replace input operands with output operands.  */
882
883   if (regmove_dump_file)
884     fprintf (regmove_dump_file, "Starting backward pass...\n");
885
886   loop_depth = 1;
887
888   for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
889     {
890       if (GET_CODE (insn) == NOTE)
891         {
892           if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
893             loop_depth++;
894           else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
895             loop_depth--;
896         }
897       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
898         {
899           int insn_code_number = find_matches (insn, &match);
900           int operand_number, match_number;
901           
902           if (insn_code_number < 0)
903             continue;
904
905           /* Now scan through the operands looking for a destination operand
906              which is supposed to match a source operand.
907              Then scan backward for an instruction which sets the source
908              operand.  If safe, then replace the source operand with the
909              dest operand in both instructions.  */
910
911           for (operand_number = 0;
912                operand_number < insn_n_operands[insn_code_number];
913                operand_number++)
914             {
915               rtx set, p, src, dst;
916               rtx src_note, dst_note;
917               int success = 0;
918               int num_calls = 0;
919               enum reg_class src_class, dst_class;
920               int length;
921
922               match_number = match.with[operand_number];
923
924               /* Nothing to do if the two operands aren't supposed to match.  */
925               if (match_number < 0)
926                 continue;
927
928               dst = recog_operand[match_number];
929               src = recog_operand[operand_number];
930
931               if (GET_CODE (src) != REG)
932                 continue;
933
934               if (GET_CODE (dst) != REG
935                   || REGNO (dst) < FIRST_PSEUDO_REGISTER
936                   || REG_LIVE_LENGTH (REGNO (dst)) < 0)
937                 continue;
938
939               /* If the operands already match, then there is nothing to do.  */
940               if (operands_match_p (src, dst)
941                   || (match.commutative[operand_number] >= 0
942                       && operands_match_p (recog_operand[match.commutative[operand_number]], dst)))
943                 continue;
944
945               set = single_set (insn);
946               if (! set)
947                 continue;
948
949               /* match_number/dst must be a write-only operand, and
950                  operand_operand/src must be a read-only operand.  */
951               if (match.use[operand_number] != READ
952                   || match.use[match_number] != WRITE)
953                 continue;
954
955               if (match.early_clobber[match_number]
956                   && count_occurrences (PATTERN (insn), src) > 1)
957                 continue;
958
959               /* Make sure match_number is the destination.  */
960               if (recog_operand[match_number] != SET_DEST (set))
961                 continue;
962
963               if (REGNO (src) < FIRST_PSEUDO_REGISTER)
964                 {
965                   if (GET_CODE (SET_SRC (set)) == PLUS
966                       && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT
967                       && XEXP (SET_SRC (set), 0) == src
968                       && fixup_match_2 (insn, dst, src,
969                                         XEXP (SET_SRC (set), 1),
970                                         regmove_dump_file))
971                     break;
972                   continue;
973                 }
974               src_class = reg_preferred_class (REGNO (src));
975               dst_class = reg_preferred_class (REGNO (dst));
976               if (src_class != dst_class
977                   && (! reg_class_subset_p (src_class, dst_class)
978                       || CLASS_LIKELY_SPILLED_P (src_class))
979                   && (! reg_class_subset_p (dst_class, src_class)
980                       || CLASS_LIKELY_SPILLED_P (dst_class)))
981                 continue;
982           
983               if (! (src_note = find_reg_note (insn, REG_DEAD, src)))
984                 continue;
985
986               /* Can not modify an earlier insn to set dst if this insn
987                  uses an old value in the source.  */
988               if (reg_overlap_mentioned_p (dst, SET_SRC (set)))
989                 continue;
990
991               if (regmove_dump_file)
992                 fprintf (regmove_dump_file,
993                          "Could fix operand %d of insn %d matching operand %d.\n",
994                          operand_number, INSN_UID (insn), match_number);
995
996               /* If src is set once in a different basic block,
997                  and is set equal to a constant, then do not use
998                  it for this optimization, as this would make it
999                  no longer equivalent to a constant.  */
1000               if (reg_is_remote_constant_p (src, insn, f))
1001                 continue;
1002
1003               /* Scan backward to find the first instruction that uses
1004                  the input operand.  If the operand is set here, then
1005                  replace it in both instructions with match_number.  */
1006
1007               for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
1008                 {
1009                   rtx pset;
1010
1011                   if (GET_CODE (p) == CODE_LABEL
1012                       || GET_CODE (p) == JUMP_INSN
1013                       || (GET_CODE (p) == NOTE
1014                           && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
1015                               || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
1016                     break;
1017
1018                   /* ??? We can't scan past the end of a basic block without
1019                      updating the register lifetime info
1020                      (REG_DEAD/basic_block_live_at_start).
1021                      A CALL_INSN might be the last insn of a basic block, if
1022                      it is inside an EH region.  There is no easy way to tell,
1023                      so we just always break when we see a CALL_INSN if
1024                      flag_exceptions is nonzero.  */
1025                   if (flag_exceptions && GET_CODE (p) == CALL_INSN)
1026                     break;
1027
1028                   if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
1029                     continue;
1030
1031                   length++;
1032
1033                   /* ??? See if all of SRC is set in P.  This test is much
1034                      more conservative than it needs to be.  */
1035                   pset = single_set (p);
1036                   if (pset && SET_DEST (pset) == src)
1037                     {
1038                       /* We use validate_replace_rtx, in case there
1039                          are multiple identical source operands.  All of
1040                          them have to be changed at the same time.  */
1041                       if (validate_replace_rtx (src, dst, insn))
1042                         {
1043                           if (validate_change (p, &SET_DEST (pset),
1044                                                dst, 0))
1045                             success = 1;
1046                           else
1047                             {
1048                               /* Change all source operands back.
1049                                  This modifies the dst as a side-effect.  */
1050                               validate_replace_rtx (dst, src, insn);
1051                               /* Now make sure the dst is right.  */
1052                               validate_change (insn,
1053                                                recog_operand_loc[match_number],
1054                                                dst, 0);
1055                             }
1056                         }
1057                       break;
1058                     }
1059
1060                   if (reg_overlap_mentioned_p (src, PATTERN (p))
1061                       || reg_overlap_mentioned_p (dst, PATTERN (p)))
1062                     break;
1063
1064                   /* If we have passed a call instruction, and the
1065                      pseudo-reg DST is not already live across a call,
1066                      then don't perform the optimization.  */
1067                   if (GET_CODE (p) == CALL_INSN)
1068                     {
1069                       num_calls++;
1070
1071                       if (REG_N_CALLS_CROSSED (REGNO (dst)) == 0)
1072                         break;
1073                     }
1074                 }
1075
1076               if (success)
1077                 {
1078                   int dstno, srcno;
1079
1080                   /* Remove the death note for SRC from INSN.  */
1081                   remove_note (insn, src_note);
1082                   /* Move the death note for SRC to P if it is used
1083                      there.  */
1084                   if (reg_overlap_mentioned_p (src, PATTERN (p)))
1085                     {
1086                       XEXP (src_note, 1) = REG_NOTES (p);
1087                       REG_NOTES (p) = src_note;
1088                     }
1089                   /* If there is a REG_DEAD note for DST on P, then remove
1090                      it, because DST is now set there.  */
1091                   if ((dst_note = find_reg_note (p, REG_DEAD, dst)))
1092                     remove_note (p, dst_note);
1093
1094                   dstno = REGNO (dst);
1095                   srcno = REGNO (src);
1096
1097                   REG_N_SETS (dstno)++;
1098                   REG_N_SETS (srcno)--;
1099
1100                   REG_N_CALLS_CROSSED (dstno) += num_calls;
1101                   REG_N_CALLS_CROSSED (srcno) -= num_calls;
1102
1103                   REG_LIVE_LENGTH (dstno) += length;
1104                   if (REG_LIVE_LENGTH (srcno) >= 0)
1105                     {
1106                       REG_LIVE_LENGTH (srcno) -= length;
1107                       /* REG_LIVE_LENGTH is only an approximation after
1108                          combine if sched is not run, so make sure that we
1109                          still have a reasonable value.  */
1110                       if (REG_LIVE_LENGTH (srcno) < 2)
1111                         REG_LIVE_LENGTH (srcno) = 2;
1112                     }
1113
1114                   /* We assume that a register is used exactly once per
1115                      insn in the updates above.  If this is not correct,
1116                      no great harm is done.  */
1117
1118                   REG_N_REFS (dstno) += 2 * loop_depth;
1119                   REG_N_REFS (srcno) -= 2 * loop_depth;
1120
1121                   /* If that was the only time src was set,
1122                      and src was not live at the start of the
1123                      function, we know that we have no more
1124                      references to src; clear REG_N_REFS so it
1125                      won't make reload do any work.  */
1126                   if (REG_N_SETS (REGNO (src)) == 0
1127                       && ! regno_uninitialized (REGNO (src)))
1128                     REG_N_REFS (REGNO (src)) = 0;
1129
1130                   if (regmove_dump_file)
1131                     fprintf (regmove_dump_file,
1132                              "Fixed operand %d of insn %d matching operand %d.\n",
1133                              operand_number, INSN_UID (insn), match_number);
1134
1135                   break;
1136                 }
1137             }
1138         }
1139     }
1140 #endif /* REGISTER_CONSTRAINTS */
1141 }
1142
1143
1144 static int
1145 find_matches (insn, matchp)
1146      rtx insn;
1147      struct match *matchp;
1148 {
1149   int likely_spilled[MAX_RECOG_OPERANDS];
1150   int operand_number;
1151   int insn_code_number = recog_memoized (insn);
1152   int any_matches = 0;
1153
1154   if (insn_code_number < 0)
1155     return -1;
1156
1157   insn_extract (insn);
1158   if (! constrain_operands (insn_code_number, 0))
1159     return -1;
1160
1161   /* Must initialize this before main loop, because the code for
1162      the commutative case may set matches for operands other than
1163      the current one.  */
1164   for (operand_number = insn_n_operands[insn_code_number];
1165        --operand_number >= 0; )
1166     matchp->with[operand_number] = matchp->commutative[operand_number] = -1;
1167
1168   for (operand_number = 0; operand_number < insn_n_operands[insn_code_number];
1169        operand_number++)
1170     {
1171       char *p, c;
1172       int i = 0;
1173
1174       p = insn_operand_constraint[insn_code_number][operand_number];
1175
1176       likely_spilled[operand_number] = 0;
1177       matchp->use[operand_number] = READ;
1178       matchp->early_clobber[operand_number] = 0;
1179       if (*p == '=')
1180         matchp->use[operand_number] = WRITE;
1181       else if (*p == '+')
1182         matchp->use[operand_number] = READWRITE;
1183
1184       for (;*p && i < which_alternative; p++)
1185         if (*p == ',')
1186           i++;
1187
1188       while ((c = *p++) != '\0' && c != ',')
1189         switch (c)
1190           {
1191           case '=':
1192             break;
1193           case '+':
1194             break;
1195           case '&':
1196             matchp->early_clobber[operand_number] = 1;
1197             break;
1198           case '%':
1199             matchp->commutative[operand_number] = operand_number + 1;
1200             matchp->commutative[operand_number + 1] = operand_number;
1201             break;
1202           case '0': case '1': case '2': case '3': case '4':
1203           case '5': case '6': case '7': case '8': case '9':
1204             c -= '0';
1205             if (c < operand_number && likely_spilled[c])
1206               break;
1207             matchp->with[operand_number] = c;
1208             any_matches = 1;
1209             if (matchp->commutative[operand_number] >= 0)
1210               matchp->with[matchp->commutative[operand_number]] = c;
1211             break;
1212           case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'h':
1213           case 'j': case 'k': case 'l': case 'p': case 'q': case 't': case 'u':
1214           case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B':
1215           case 'C': case 'D': case 'W': case 'Y': case 'Z':
1216             if (CLASS_LIKELY_SPILLED_P (REG_CLASS_FROM_LETTER (c)))
1217               likely_spilled[operand_number] = 1;
1218             break;
1219           }
1220     }
1221   return any_matches ? insn_code_number : -1;
1222 }
1223
1224 /* Try to replace output operand DST in SET, with input operand SRC.  SET is
1225    the only set in INSN.  INSN has just been recgnized and constrained.
1226    SRC is operand number OPERAND_NUMBER in INSN.
1227    DST is operand number MATCH_NUMBER in INSN.
1228    If BACKWARD is nonzero, we have been called in a backward pass.
1229    Return nonzero for success.  */
1230 static int
1231 fixup_match_1 (insn, set, src, src_subreg, dst, backward, operand_number,
1232                match_number, regmove_dump_file)
1233      rtx insn, set, src, src_subreg, dst;
1234      int backward, operand_number, match_number;
1235      FILE *regmove_dump_file;
1236 {
1237   rtx p;
1238   rtx post_inc = 0, post_inc_set = 0, search_end = 0;
1239   int success = 0;
1240   int num_calls = 0, s_num_calls = 0;
1241   enum rtx_code code = NOTE;
1242   HOST_WIDE_INT insn_const, newconst;
1243   rtx overlap = 0; /* need to move insn ? */
1244   rtx src_note = find_reg_note (insn, REG_DEAD, src), dst_note;
1245   int length, s_length, true_loop_depth;
1246
1247   if (! src_note)
1248     {
1249       /* Look for (set (regX) (op regA constX))
1250                   (set (regY) (op regA constY))
1251          and change that to
1252                   (set (regA) (op regA constX)).
1253                   (set (regY) (op regA constY-constX)).
1254          This works for add and shift operations, if
1255          regA is dead after or set by the second insn.  */
1256
1257       code = GET_CODE (SET_SRC (set));
1258       if ((code == PLUS || code == LSHIFTRT
1259            || code == ASHIFT || code == ASHIFTRT)
1260           && XEXP (SET_SRC (set), 0) == src
1261           && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
1262         insn_const = INTVAL (XEXP (SET_SRC (set), 1));
1263       else if (! stable_but_for_p (SET_SRC (set), src, dst))
1264         return 0;
1265       else
1266         /* We might find a src_note while scanning.  */
1267         code = NOTE;
1268     }
1269
1270   if (regmove_dump_file)
1271     fprintf (regmove_dump_file,
1272              "Could fix operand %d of insn %d matching operand %d.\n",
1273              operand_number, INSN_UID (insn), match_number);
1274
1275   /* If SRC is equivalent to a constant set in a different basic block,
1276      then do not use it for this optimization.  We want the equivalence
1277      so that if we have to reload this register, we can reload the
1278      constant, rather than extending the lifespan of the register.  */
1279   if (reg_is_remote_constant_p (src, insn, get_insns ()))
1280     return 0;
1281
1282   /* Scan forward to find the next instruction that
1283      uses the output operand.  If the operand dies here,
1284      then replace it in both instructions with
1285      operand_number.  */
1286
1287   for (length = s_length = 0, p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
1288     {
1289       if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
1290           || (GET_CODE (p) == NOTE
1291               && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
1292                   || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
1293         break;
1294
1295       /* ??? We can't scan past the end of a basic block without updating
1296          the register lifetime info (REG_DEAD/basic_block_live_at_start).
1297          A CALL_INSN might be the last insn of a basic block, if it is
1298          inside an EH region.  There is no easy way to tell, so we just
1299          always break when we see a CALL_INSN if flag_exceptions is nonzero.  */
1300       if (flag_exceptions && GET_CODE (p) == CALL_INSN)
1301         break;
1302
1303       if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
1304         continue;
1305
1306       length++;
1307       if (src_note)
1308         s_length++;
1309
1310       if (reg_set_p (src, p) || reg_set_p (dst, p)
1311           || (GET_CODE (PATTERN (p)) == USE
1312               && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
1313         break;
1314
1315       /* See if all of DST dies in P.  This test is
1316          slightly more conservative than it needs to be.  */
1317       if ((dst_note = find_regno_note (p, REG_DEAD, REGNO (dst)))
1318           && (GET_MODE (XEXP (dst_note, 0)) == GET_MODE (dst)))
1319         {
1320           if (! src_note)
1321             {
1322               rtx q;
1323               rtx set2;
1324
1325               /* If an optimization is done, the value of SRC while P
1326                  is executed will be changed.  Check that this is OK.  */
1327               if (reg_overlap_mentioned_p (src, PATTERN (p)))
1328                 break;
1329               for (q = p; q; q = NEXT_INSN (q))
1330                 {
1331                   if (GET_CODE (q) == CODE_LABEL || GET_CODE (q) == JUMP_INSN
1332                       || (GET_CODE (q) == NOTE
1333                           && (NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_BEG
1334                               || NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_END)))
1335                     {
1336                       q = 0;
1337                       break;
1338                     }
1339
1340                   /* ??? We can't scan past the end of a basic block without
1341                      updating the register lifetime info
1342                      (REG_DEAD/basic_block_live_at_start).
1343                      A CALL_INSN might be the last insn of a basic block, if
1344                      it is inside an EH region.  There is no easy way to tell,
1345                      so we just always break when we see a CALL_INSN if
1346                      flag_exceptions is nonzero.  */
1347                   if (flag_exceptions && GET_CODE (p) == CALL_INSN)
1348                     {
1349                       q = 0;
1350                       break;
1351                     }
1352
1353                   if (GET_RTX_CLASS (GET_CODE (q)) != 'i')
1354                     continue;
1355                   if (reg_overlap_mentioned_p (src, PATTERN (q))
1356                       || reg_set_p (src, q))
1357                     break;
1358                 }
1359               if (q)
1360                 set2 = single_set (q);
1361               if (! q || ! set2 || GET_CODE (SET_SRC (set2)) != code
1362                   || XEXP (SET_SRC (set2), 0) != src
1363                   || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT
1364                   || (SET_DEST (set2) != src
1365                       && ! find_reg_note (q, REG_DEAD, src)))
1366                 {
1367                   /* If this is a PLUS, we can still save a register by doing
1368                      src += insn_const;
1369                      P;
1370                      src -= insn_const; .
1371                      This also gives opportunities for subsequent
1372                      optimizations in the backward pass, so do it there.  */
1373                   if (code == PLUS && backward
1374 #ifdef HAVE_cc0
1375                       /* We may not emit an insn directly
1376                          after P if the latter sets CC0.  */
1377                       && ! sets_cc0_p (PATTERN (p))
1378 #endif
1379                       )
1380
1381                     {
1382                       search_end = q;
1383                       q = insn;
1384                       set2 = set;
1385                       newconst = -insn_const;
1386                       code = MINUS;
1387                     }
1388                   else
1389                     break;
1390                 }
1391               else
1392                 {
1393                   newconst = INTVAL (XEXP (SET_SRC (set2), 1)) - insn_const;
1394                   /* Reject out of range shifts.  */
1395                   if (code != PLUS
1396                       && (newconst < 0
1397                           || (newconst
1398                               >= GET_MODE_BITSIZE (GET_MODE (SET_SRC (set2))))))
1399                     break;
1400                   if (code == PLUS)
1401                     {
1402                       post_inc = q;
1403                       if (SET_DEST (set2) != src)
1404                         post_inc_set = set2;
1405                     }
1406                 }
1407               /* We use 1 as last argument to validate_change so that all
1408                  changes are accepted or rejected together by apply_change_group
1409                  when it is called by validate_replace_rtx .  */
1410               validate_change (q, &XEXP (SET_SRC (set2), 1),
1411                                GEN_INT (newconst), 1);
1412             }
1413           validate_change (insn, recog_operand_loc[match_number], src, 1);
1414           if (validate_replace_rtx (dst, src_subreg, p))
1415             success = 1;
1416           break;
1417         }
1418
1419       if (reg_overlap_mentioned_p (dst, PATTERN (p)))
1420         break;
1421       if (! src_note && reg_overlap_mentioned_p (src, PATTERN (p)))
1422         {
1423           /* INSN was already checked to be movable when
1424              we found no REG_DEAD note for src on it.  */
1425           overlap = p;
1426           src_note = find_reg_note (p, REG_DEAD, src);
1427         }
1428
1429       /* If we have passed a call instruction, and the pseudo-reg SRC is not
1430          already live across a call, then don't perform the optimization.  */
1431       if (GET_CODE (p) == CALL_INSN)
1432         {
1433           if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
1434             break;
1435
1436           num_calls++;
1437
1438           if (src_note)
1439             s_num_calls++;
1440
1441         }
1442     }
1443
1444   if (! success)
1445     return 0;
1446
1447   true_loop_depth = backward ? 2 - loop_depth : loop_depth;
1448
1449   /* Remove the death note for DST from P.  */
1450   remove_note (p, dst_note);
1451   if (code == MINUS)
1452     {
1453       post_inc = emit_insn_after (copy_rtx (PATTERN (insn)), p);
1454 #if defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT)
1455       if (search_end
1456           && try_auto_increment (search_end, post_inc, 0, src, newconst, 1))
1457         post_inc = 0;
1458 #endif
1459       validate_change (insn, &XEXP (SET_SRC (set), 1), GEN_INT (insn_const), 0);
1460       REG_N_SETS (REGNO (src))++;
1461       REG_N_REFS (REGNO (src)) += true_loop_depth;
1462       REG_LIVE_LENGTH (REGNO (src))++;
1463     }
1464   if (overlap)
1465     {
1466       /* The lifetime of src and dest overlap,
1467          but we can change this by moving insn.  */
1468       rtx pat = PATTERN (insn);
1469       if (src_note)
1470         remove_note (overlap, src_note);
1471 #if defined (HAVE_POST_INCREMENT) || defined (HAVE_POST_DECREMENT)
1472       if (code == PLUS
1473           && try_auto_increment (overlap, insn, 0, src, insn_const, 0))
1474         insn = overlap;
1475       else
1476 #endif
1477         {
1478           rtx notes = REG_NOTES (insn);
1479
1480           emit_insn_after_with_line_notes (pat, PREV_INSN (p), insn);
1481           PUT_CODE (insn, NOTE);
1482           NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1483           NOTE_SOURCE_FILE (insn) = 0;
1484           /* emit_insn_after_with_line_notes has no
1485              return value, so search for the new insn.  */
1486           for (insn = p; PATTERN (insn) != pat; )
1487             insn = PREV_INSN (insn);
1488
1489           REG_NOTES (insn) = notes;
1490         }
1491     }
1492   /* Sometimes we'd generate src = const; src += n;
1493      if so, replace the instruction that set src
1494      in the first place.  */
1495
1496   if (! overlap && (code == PLUS || code == MINUS))
1497     {
1498       rtx note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
1499       rtx q, set2;
1500       int num_calls2 = 0, s_length2 = 0;
1501
1502       if (note && CONSTANT_P (XEXP (note, 0)))
1503         {
1504           for (q = PREV_INSN (insn); q; q = PREV_INSN(q))
1505             {
1506               if (GET_CODE (q) == CODE_LABEL || GET_CODE (q) == JUMP_INSN
1507                   || (GET_CODE (q) == NOTE
1508                       && (NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_BEG
1509                           || NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_END)))
1510                 {
1511                   q = 0;
1512                   break;
1513                 }
1514
1515               /* ??? We can't scan past the end of a basic block without
1516                  updating the register lifetime info
1517                  (REG_DEAD/basic_block_live_at_start).
1518                  A CALL_INSN might be the last insn of a basic block, if
1519                  it is inside an EH region.  There is no easy way to tell,
1520                  so we just always break when we see a CALL_INSN if
1521                  flag_exceptions is nonzero.  */
1522               if (flag_exceptions && GET_CODE (p) == CALL_INSN)
1523                 {
1524                   q = 0;
1525                   break;
1526                 }
1527
1528               if (GET_RTX_CLASS (GET_CODE (q)) != 'i')
1529                 continue;
1530               s_length2++;
1531               if (reg_set_p (src, q))
1532                 {
1533                   set2 = single_set (q);
1534                   break;
1535                 }
1536               if (reg_overlap_mentioned_p (src, PATTERN (q)))
1537                 {
1538                   q = 0;
1539                   break;
1540                 }
1541               if (GET_CODE (p) == CALL_INSN)
1542                 num_calls2++;
1543             }
1544           if (q && set2 && SET_DEST (set2) == src && CONSTANT_P (SET_SRC (set2))
1545               && validate_change (insn, &SET_SRC (set), XEXP (note, 0), 0))
1546             {
1547               PUT_CODE (q, NOTE);
1548               NOTE_LINE_NUMBER (q) = NOTE_INSN_DELETED;
1549               NOTE_SOURCE_FILE (q) = 0;
1550               REG_N_SETS (REGNO (src))--;
1551               REG_N_CALLS_CROSSED (REGNO (src)) -= num_calls2;
1552               REG_N_REFS (REGNO (src)) -= true_loop_depth;
1553               REG_LIVE_LENGTH (REGNO (src)) -= s_length2;
1554               insn_const = 0;
1555             }
1556         }
1557     }
1558
1559   /* Don't remove this seemingly useless if, it is needed to pair with the
1560      else in the next two conditionally included code blocks.  */
1561   if (0)
1562     {;}
1563 #if defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT)
1564   else if ((code == PLUS || code == MINUS) && insn_const
1565            && try_auto_increment (p, insn, 0, src, insn_const, 1))
1566     insn = p;
1567 #endif
1568 #if defined (HAVE_POST_INCREMENT) || defined (HAVE_POST_DECREMENT)
1569   else if (post_inc
1570            && try_auto_increment (p, post_inc, post_inc_set, src, newconst, 0))
1571     post_inc = 0;
1572 #endif
1573 #if defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT)
1574   /* If post_inc still prevails, try to find an
1575      insn where it can be used as a pre-in/decrement.
1576      If code is MINUS, this was already tried.  */
1577   if (post_inc && code == PLUS
1578   /* Check that newconst is likely to be usable
1579      in a pre-in/decrement before starting the search.  */
1580       && (0
1581 #if defined (HAVE_PRE_INCREMENT)
1582           || (newconst > 0 && newconst <= MOVE_MAX)
1583 #endif
1584 #if defined (HAVE_PRE_DECREMENT)
1585           || (newconst < 0 && newconst >= -MOVE_MAX)
1586 #endif
1587          ) && exact_log2 (newconst))
1588     {
1589       rtx q, inc_dest;
1590
1591       inc_dest = post_inc_set ? SET_DEST (post_inc_set) : src;
1592       for (q = post_inc; (q = NEXT_INSN (q)); )
1593         {
1594           if (GET_CODE (q) == CODE_LABEL || GET_CODE (q) == JUMP_INSN
1595               || (GET_CODE (q) == NOTE
1596                   && (NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_BEG
1597                       || NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_END)))
1598             break;
1599
1600           /* ??? We can't scan past the end of a basic block without updating
1601              the register lifetime info (REG_DEAD/basic_block_live_at_start).
1602              A CALL_INSN might be the last insn of a basic block, if it
1603              is inside an EH region.  There is no easy way to tell so we
1604              just always break when we see a CALL_INSN if flag_exceptions
1605              is nonzero.  */
1606           if (flag_exceptions && GET_CODE (p) == CALL_INSN)
1607             break;
1608
1609           if (GET_RTX_CLASS (GET_CODE (q)) != 'i')
1610             continue;
1611           if (src != inc_dest && (reg_overlap_mentioned_p (src, PATTERN (q))
1612                                   || reg_set_p (src, q)))
1613             break;
1614           if (reg_set_p (inc_dest, q))
1615             break;
1616           if (reg_overlap_mentioned_p (inc_dest, PATTERN (q)))
1617             {
1618               try_auto_increment (q, post_inc,
1619                                   post_inc_set, inc_dest, newconst, 1);
1620               break;
1621             }
1622         }
1623     }
1624 #endif /* defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT) */
1625   /* Move the death note for DST to INSN if it is used
1626      there.  */
1627   if (reg_overlap_mentioned_p (dst, PATTERN (insn)))
1628     {
1629       XEXP (dst_note, 1) = REG_NOTES (insn);
1630       REG_NOTES (insn) = dst_note;
1631     }
1632
1633   if (src_note)
1634     {
1635       /* Move the death note for SRC from INSN to P.  */
1636       if (! overlap)
1637         remove_note (insn, src_note);
1638       XEXP (src_note, 1) = REG_NOTES (p);
1639       REG_NOTES (p) = src_note;
1640
1641       REG_N_CALLS_CROSSED (REGNO (src)) += s_num_calls;
1642     }
1643
1644   REG_N_SETS (REGNO (src))++;
1645   REG_N_SETS (REGNO (dst))--;
1646
1647   REG_N_CALLS_CROSSED (REGNO (dst)) -= num_calls;
1648
1649   REG_LIVE_LENGTH (REGNO (src)) += s_length;
1650   if (REG_LIVE_LENGTH (REGNO (dst)) >= 0)
1651     {
1652       REG_LIVE_LENGTH (REGNO (dst)) -= length;
1653       /* REG_LIVE_LENGTH is only an approximation after
1654          combine if sched is not run, so make sure that we
1655          still have a reasonable value.  */
1656       if (REG_LIVE_LENGTH (REGNO (dst)) < 2)
1657         REG_LIVE_LENGTH (REGNO (dst)) = 2;
1658     }
1659
1660   /* We assume that a register is used exactly once per
1661       insn in the updates above.  If this is not correct,
1662       no great harm is done.  */
1663
1664   REG_N_REFS (REGNO (src)) += 2 * true_loop_depth;
1665   REG_N_REFS (REGNO (dst)) -= 2 * true_loop_depth;
1666
1667   /* If that was the only time dst was set,
1668      and dst was not live at the start of the
1669      function, we know that we have no more
1670      references to dst; clear REG_N_REFS so it
1671      won't make reload do any work.  */
1672   if (REG_N_SETS (REGNO (dst)) == 0
1673       && ! regno_uninitialized (REGNO (dst)))
1674     REG_N_REFS (REGNO (dst)) = 0;
1675
1676   if (regmove_dump_file)
1677     fprintf (regmove_dump_file,
1678              "Fixed operand %d of insn %d matching operand %d.\n",
1679              operand_number, INSN_UID (insn), match_number);
1680   return 1;
1681 }
1682
1683
1684 /* return nonzero if X is stable but for mentioning SRC or mentioning /
1685    changing DST .  If in doubt, presume it is unstable.  */
1686 static int
1687 stable_but_for_p (x, src, dst)
1688      rtx x, src, dst;
1689 {
1690   RTX_CODE code = GET_CODE (x);
1691   switch (GET_RTX_CLASS (code))
1692     {
1693     case '<': case '1': case 'c': case '2': case 'b': case '3':
1694       {
1695         int i;
1696         char *fmt = GET_RTX_FORMAT (code);
1697         for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1698           if (fmt[i] == 'e' && ! stable_but_for_p (XEXP (x, i), src, dst))
1699               return 0;
1700         return 1;
1701       }
1702     case 'o':
1703       if (x == src || x == dst)
1704         return 1;
1705       /* fall through */
1706     default:
1707       return ! rtx_unstable_p (x);
1708     }
1709 }
1710
1711 /* Test if regmove seems profitable for this target.  */
1712 int
1713 regmove_profitable_p ()
1714 {
1715 #ifdef REGISTER_CONSTRAINTS
1716   struct match match;
1717   enum machine_mode mode;
1718   optab tstoptab = add_optab;
1719   do /* check add_optab and ashl_optab */
1720     for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1721            mode = GET_MODE_WIDER_MODE (mode))
1722         {
1723           int icode = (int) tstoptab->handlers[(int) mode].insn_code;
1724           rtx reg0, reg1, reg2, pat;
1725           int i;
1726     
1727           if (GET_MODE_BITSIZE (mode) < 32 || icode == CODE_FOR_nothing)
1728             continue;
1729           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1730             if (TEST_HARD_REG_BIT (reg_class_contents[GENERAL_REGS], i))
1731               break;
1732           if (i + 2 >= FIRST_PSEUDO_REGISTER)
1733             break;
1734           reg0 = gen_rtx_REG (insn_operand_mode[icode][0], i);
1735           reg1 = gen_rtx_REG (insn_operand_mode[icode][1], i + 1);
1736           reg2 = gen_rtx_REG (insn_operand_mode[icode][2], i + 2);
1737           if (! (*insn_operand_predicate[icode][0]) (reg0, VOIDmode)
1738               || ! (*insn_operand_predicate[icode][1]) (reg1, VOIDmode)
1739               || ! (*insn_operand_predicate[icode][2]) (reg2, VOIDmode))
1740             break;
1741           pat = GEN_FCN (icode) (reg0, reg1, reg2);
1742           if (! pat)
1743             continue;
1744           if (GET_CODE (pat) == SEQUENCE)
1745             pat = XVECEXP (pat, 0,  XVECLEN (pat, 0) - 1);
1746           else
1747             pat = make_insn_raw (pat);
1748           if (! single_set (pat)
1749               || GET_CODE (SET_SRC (single_set (pat))) != tstoptab->code)
1750             /* Unexpected complexity;  don't need to handle this unless
1751                we find a machine where this occurs and regmove should
1752                be enabled.  */
1753             break;
1754           if (find_matches (pat, &match) >= 0)
1755             return 1;
1756           break;
1757         }
1758   while (tstoptab != ashl_optab && (tstoptab = ashl_optab, 1));
1759 #endif /* REGISTER_CONSTRAINTS */
1760   return 0;
1761 }