OSDN Git Service

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