OSDN Git Service

Replace recog_foo with recog_data.foo.
[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-98, 1999 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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21
22 /* This module looks for cases where matching constraints would force
23    an instruction to need a reload, and this reload would be a register
24    to register move.  It then attempts to change the registers used by the
25    instruction to avoid the move instruction.  */
26
27 #include "config.h"
28 #include "system.h"
29 #include "rtl.h" /* stdio.h must precede rtl.h for FFS.  */
30 #include "insn-config.h"
31 #include "recog.h"
32 #include "output.h"
33 #include "reload.h"
34 #include "regs.h"
35 #include "hard-reg-set.h"
36 #include "flags.h"
37 #include "function.h"
38 #include "expr.h"
39 #include "insn-flags.h"
40 #include "basic-block.h"
41 #include "toplev.h"
42
43 static int optimize_reg_copy_1  PROTO((rtx, rtx, rtx));
44 static void optimize_reg_copy_2 PROTO((rtx, rtx, rtx));
45 static void optimize_reg_copy_3 PROTO((rtx, rtx, rtx));
46 static rtx gen_add3_insn        PROTO((rtx, rtx, rtx));
47 static void copy_src_to_dest    PROTO((rtx, rtx, rtx, int, int));
48 static int *regmove_bb_head;
49
50 struct match {
51   int with[MAX_RECOG_OPERANDS];
52   enum { READ, WRITE, READWRITE } use[MAX_RECOG_OPERANDS];
53   int commutative[MAX_RECOG_OPERANDS];
54   int early_clobber[MAX_RECOG_OPERANDS];
55 };
56
57 static rtx discover_flags_reg PROTO((void));
58 static void mark_flags_life_zones PROTO((rtx));
59 static void flags_set_1 PROTO((rtx, rtx));
60
61 static int try_auto_increment PROTO((rtx, rtx, rtx, rtx, HOST_WIDE_INT, int));
62 static int find_matches PROTO((rtx, struct match *));
63 static int fixup_match_1 PROTO((rtx, rtx, rtx, rtx, rtx, int, int, int, FILE *))
64 ;
65 static int reg_is_remote_constant_p PROTO((rtx, rtx, rtx));
66 static int stable_but_for_p PROTO((rtx, rtx, rtx));
67 static int regclass_compatible_p PROTO((int, int));
68 static int loop_depth;
69
70 /* Return non-zero if registers with CLASS1 and CLASS2 can be merged without
71    causing too much register allocation problems.  */
72 static int
73 regclass_compatible_p (class0, class1)
74      int class0, class1;
75 {
76   return (class0 == class1
77           || (reg_class_subset_p (class0, class1)
78               && ! CLASS_LIKELY_SPILLED_P (class0))
79           || (reg_class_subset_p (class1, class0)
80               && ! CLASS_LIKELY_SPILLED_P (class1)));
81 }
82
83 /* Generate and return an insn body to add r1 and c,
84    storing the result in r0.  */
85 static rtx
86 gen_add3_insn (r0, r1, c)
87      rtx r0, r1, c;
88 {
89   int icode = (int) add_optab->handlers[(int) GET_MODE (r0)].insn_code;
90
91     if (icode == CODE_FOR_nothing
92       || ! (*insn_operand_predicate[icode][0]) (r0, insn_operand_mode[icode][0])
93       || ! (*insn_operand_predicate[icode][1]) (r1, insn_operand_mode[icode][1])
94       || ! (*insn_operand_predicate[icode][2]) (c, insn_operand_mode[icode][2]))
95     return NULL_RTX;
96
97   return (GEN_FCN (icode) (r0, r1, c));
98 }
99
100
101 /* INC_INSN is an instruction that adds INCREMENT to REG.
102    Try to fold INC_INSN as a post/pre in/decrement into INSN.
103    Iff INC_INSN_SET is nonzero, inc_insn has a destination different from src.
104    Return nonzero for success.  */
105 static int
106 try_auto_increment (insn, inc_insn, inc_insn_set, reg, increment, pre)
107      rtx reg, insn, inc_insn ,inc_insn_set;
108      HOST_WIDE_INT increment;
109      int pre;
110 {
111   enum rtx_code inc_code;
112
113   rtx pset = single_set (insn);
114   if (pset)
115     {
116       /* Can't use the size of SET_SRC, we might have something like
117          (sign_extend:SI (mem:QI ...  */
118       rtx use = find_use_as_address (pset, reg, 0);
119       if (use != 0 && use != (rtx) 1)
120         {
121           int size = GET_MODE_SIZE (GET_MODE (use));
122           if (0
123               || (HAVE_POST_INCREMENT
124                   && pre == 0 && (inc_code = POST_INC, increment == size))
125               || (HAVE_PRE_INCREMENT
126                   && pre == 1 && (inc_code = PRE_INC, increment == size))
127               || (HAVE_POST_DECREMENT
128                   && pre == 0 && (inc_code = POST_DEC, increment == -size))
129               || (HAVE_PRE_DECREMENT
130                   && pre == 1 && (inc_code = PRE_DEC, increment == -size))
131           )
132             {
133               if (inc_insn_set)
134                 validate_change
135                   (inc_insn, 
136                    &SET_SRC (inc_insn_set),
137                    XEXP (SET_SRC (inc_insn_set), 0), 1);
138               validate_change (insn, &XEXP (use, 0),
139                                gen_rtx_fmt_e (inc_code, Pmode, reg), 1);
140               if (apply_change_group ())
141                 {
142                   REG_NOTES (insn)
143                     = gen_rtx_EXPR_LIST (REG_INC,
144                                          reg, REG_NOTES (insn));
145                   if (! inc_insn_set)
146                     {
147                       PUT_CODE (inc_insn, NOTE);
148                       NOTE_LINE_NUMBER (inc_insn) = NOTE_INSN_DELETED;
149                       NOTE_SOURCE_FILE (inc_insn) = 0;
150                     }
151                   return 1;
152                 }
153             }
154         }
155     }
156   return 0;
157 }
158 \f
159 /* Determine if the pattern generated by add_optab has a clobber,
160    such as might be issued for a flags hard register.  To make the
161    code elsewhere simpler, we handle cc0 in this same framework.
162
163    Return the register if one was discovered.  Return NULL_RTX if
164    if no flags were found.  Return pc_rtx if we got confused.  */
165
166 static rtx
167 discover_flags_reg ()
168 {
169   rtx tmp;
170   tmp = gen_rtx_REG (word_mode, 10000);
171   tmp = gen_add3_insn (tmp, tmp, GEN_INT (2));
172
173   /* If we get something that isn't a simple set, or a 
174      [(set ..) (clobber ..)], this whole function will go wrong.  */
175   if (GET_CODE (tmp) == SET)
176     return NULL_RTX;
177   else if (GET_CODE (tmp) == PARALLEL)
178     {
179       int found;
180
181       if (XVECLEN (tmp, 0) != 2)
182         return pc_rtx;
183       tmp = XVECEXP (tmp, 0, 1);
184       if (GET_CODE (tmp) != CLOBBER)
185         return pc_rtx;
186       tmp = XEXP (tmp, 0);
187
188       /* Don't do anything foolish if the md wanted to clobber a
189          scratch or something.  We only care about hard regs.
190          Moreover we don't like the notion of subregs of hard regs.  */
191       if (GET_CODE (tmp) == SUBREG
192           && GET_CODE (SUBREG_REG (tmp)) == REG
193           && REGNO (SUBREG_REG (tmp)) < FIRST_PSEUDO_REGISTER)
194         return pc_rtx;
195       found = (GET_CODE (tmp) == REG && REGNO (tmp) < FIRST_PSEUDO_REGISTER);
196
197       return (found ? tmp : NULL_RTX);
198     }
199
200   return pc_rtx;
201 }
202
203 /* It is a tedious task identifying when the flags register is live and
204    when it is safe to optimize.  Since we process the instruction stream
205    multiple times, locate and record these live zones by marking the
206    mode of the instructions -- 
207
208    QImode is used on the instruction at which the flags becomes live.
209
210    HImode is used within the range (exclusive) that the flags are
211    live.  Thus the user of the flags is not marked.
212
213    All other instructions are cleared to VOIDmode.  */
214
215 /* Used to communicate with flags_set_1.  */
216 static rtx flags_set_1_rtx;
217 static int flags_set_1_set;
218
219 static void
220 mark_flags_life_zones (flags)
221      rtx flags;
222 {
223   int flags_regno;
224   int flags_nregs;
225   int block;
226
227 #ifdef HAVE_cc0
228   /* If we found a flags register on a cc0 host, bail.  */
229   if (flags == NULL_RTX)
230     flags = cc0_rtx;
231   else if (flags != cc0_rtx)
232     flags = pc_rtx;
233 #endif
234     
235   /* Simple cases first: if no flags, clear all modes.  If confusing,
236      mark the entire function as being in a flags shadow.  */
237   if (flags == NULL_RTX || flags == pc_rtx)
238     {
239       enum machine_mode mode = (flags ? HImode : VOIDmode);
240       rtx insn;
241       for (insn = get_insns(); insn; insn = NEXT_INSN (insn))
242         PUT_MODE (insn, mode);
243       return;
244     }
245
246 #ifdef HAVE_cc0
247   flags_regno = -1;
248   flags_nregs = 1;
249 #else
250   flags_regno = REGNO (flags);
251   flags_nregs = HARD_REGNO_NREGS (flags_regno, GET_MODE (flags));
252 #endif
253   flags_set_1_rtx = flags;
254
255   /* Process each basic block.  */
256   for (block = n_basic_blocks - 1; block >= 0; block--)
257     {
258       rtx insn, end;
259       int live;
260
261       insn = BLOCK_HEAD (block);
262       end = BLOCK_END (block);
263
264       /* Look out for the (unlikely) case of flags being live across
265          basic block boundaries.  */
266       live = 0;
267 #ifndef HAVE_cc0
268       {
269         int i;
270         for (i = 0; i < flags_nregs; ++i)
271           live |= REGNO_REG_SET_P (BASIC_BLOCK (block)->global_live_at_start,
272                                    flags_regno + i);
273       }
274 #endif
275
276       while (1)
277         {
278           /* Process liveness in reverse order of importance --
279              alive, death, birth.  This lets more important info
280              overwrite the mode of lesser info.  */
281
282           if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
283             {
284 #ifdef HAVE_cc0
285               /* In the cc0 case, death is not marked in reg notes,
286                  but is instead the mere use of cc0 when it is alive.  */
287               if (live && reg_mentioned_p (cc0_rtx, PATTERN (insn)))
288                 live = 0;
289 #else
290               /* In the hard reg case, we watch death notes.  */
291               if (live && find_regno_note (insn, REG_DEAD, flags_regno))
292                 live = 0;
293 #endif
294               PUT_MODE (insn, (live ? HImode : VOIDmode));
295
296               /* In either case, birth is denoted simply by it's presence
297                  as the destination of a set.  */
298               flags_set_1_set = 0;
299               note_stores (PATTERN (insn), flags_set_1);
300               if (flags_set_1_set)
301                 {
302                   live = 1;
303                   PUT_MODE (insn, QImode);
304                 }
305             }
306           else
307             PUT_MODE (insn, (live ? HImode : VOIDmode));
308
309           if (insn == end)
310             break;
311           insn = NEXT_INSN (insn);
312         }
313     }
314 }
315
316 /* A subroutine of mark_flags_life_zones, called through note_stores.  */
317
318 static void
319 flags_set_1 (x, pat)
320      rtx x, pat;
321 {
322   if (GET_CODE (pat) == SET
323       && reg_overlap_mentioned_p (x, flags_set_1_rtx))
324     flags_set_1_set = 1;
325 }
326 \f
327 static int *regno_src_regno;
328
329 /* Indicate how good a choice REG (which appears as a source) is to replace
330    a destination register with.  The higher the returned value, the better
331    the choice.  The main objective is to avoid using a register that is
332    a candidate for tying to a hard register, since the output might in
333    turn be a candidate to be tied to a different hard register.  */
334 int
335 replacement_quality(reg)
336      rtx reg;
337 {
338   int src_regno;
339
340   /* Bad if this isn't a register at all.  */
341   if (GET_CODE (reg) != REG)
342     return 0;
343
344   /* If this register is not meant to get a hard register,
345      it is a poor choice.  */
346   if (REG_LIVE_LENGTH (REGNO (reg)) < 0)
347     return 0;
348
349   src_regno = regno_src_regno[REGNO (reg)];
350
351   /* If it was not copied from another register, it is fine.  */
352   if (src_regno < 0)
353     return 3;
354
355   /* Copied from a hard register?  */
356   if (src_regno < FIRST_PSEUDO_REGISTER)
357     return 1;
358
359   /* Copied from a pseudo register - not as bad as from a hard register,
360      yet still cumbersome, since the register live length will be lengthened
361      when the registers get tied.  */
362   return 2;
363 }
364
365 /* INSN is a copy from SRC to DEST, both registers, and SRC does not die
366    in INSN.
367
368    Search forward to see if SRC dies before either it or DEST is modified,
369    but don't scan past the end of a basic block.  If so, we can replace SRC
370    with DEST and let SRC die in INSN. 
371
372    This will reduce the number of registers live in that range and may enable
373    DEST to be tied to SRC, thus often saving one register in addition to a
374    register-register copy.  */
375
376 static int
377 optimize_reg_copy_1 (insn, dest, src)
378      rtx insn;
379      rtx dest;
380      rtx src;
381 {
382   rtx p, q;
383   rtx note;
384   rtx dest_death = 0;
385   int sregno = REGNO (src);
386   int dregno = REGNO (dest);
387
388   /* We don't want to mess with hard regs if register classes are small. */
389   if (sregno == dregno
390       || (SMALL_REGISTER_CLASSES
391           && (sregno < FIRST_PSEUDO_REGISTER
392               || dregno < FIRST_PSEUDO_REGISTER))
393       /* We don't see all updates to SP if they are in an auto-inc memory
394          reference, so we must disallow this optimization on them.  */
395       || sregno == STACK_POINTER_REGNUM || dregno == STACK_POINTER_REGNUM)
396     return 0;
397
398   for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
399     {
400       if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
401           || (GET_CODE (p) == NOTE
402               && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
403                   || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
404         break;
405
406       /* ??? We can't scan past the end of a basic block without updating
407          the register lifetime info (REG_DEAD/basic_block_live_at_start).
408          A CALL_INSN might be the last insn of a basic block, if it is inside
409          an EH region.  There is no easy way to tell, so we just always break
410          when we see a CALL_INSN if flag_exceptions is nonzero.  */
411       if (flag_exceptions && GET_CODE (p) == CALL_INSN)
412         break;
413
414       if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
415         continue;
416
417       if (reg_set_p (src, p) || reg_set_p (dest, p)
418           /* Don't change a USE of a register.  */
419           || (GET_CODE (PATTERN (p)) == USE
420               && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
421         break;
422
423       /* See if all of SRC dies in P.  This test is slightly more
424          conservative than it needs to be.  */
425       if ((note = find_regno_note (p, REG_DEAD, sregno)) != 0
426           && GET_MODE (XEXP (note, 0)) == GET_MODE (src))
427         {
428           int failed = 0;
429           int d_length = 0;
430           int s_length = 0;
431           int d_n_calls = 0;
432           int s_n_calls = 0;
433
434           /* We can do the optimization.  Scan forward from INSN again,
435              replacing regs as we go.  Set FAILED if a replacement can't
436              be done.  In that case, we can't move the death note for SRC.
437              This should be rare.  */
438
439           /* Set to stop at next insn.  */
440           for (q = next_real_insn (insn);
441                q != next_real_insn (p);
442                q = next_real_insn (q))
443             {
444               if (reg_overlap_mentioned_p (src, PATTERN (q)))
445                 {
446                   /* If SRC is a hard register, we might miss some
447                      overlapping registers with validate_replace_rtx,
448                      so we would have to undo it.  We can't if DEST is
449                      present in the insn, so fail in that combination
450                      of cases.  */
451                   if (sregno < FIRST_PSEUDO_REGISTER
452                       && reg_mentioned_p (dest, PATTERN (q)))
453                     failed = 1;
454
455                   /* Replace all uses and make sure that the register
456                      isn't still present.  */
457                   else if (validate_replace_rtx (src, dest, q)
458                            && (sregno >= FIRST_PSEUDO_REGISTER
459                                || ! reg_overlap_mentioned_p (src,
460                                                              PATTERN (q))))
461                     {
462                       /* We assume that a register is used exactly once per
463                          insn in the REG_N_REFS updates below.  If this is not
464                          correct, no great harm is done.
465
466                          Since we do not know if we will change the lifetime of
467                          SREGNO or DREGNO, we must not update REG_LIVE_LENGTH
468                          or REG_N_CALLS_CROSSED at this time.   */
469                       if (sregno >= FIRST_PSEUDO_REGISTER)
470                         REG_N_REFS (sregno) -= loop_depth;
471
472                       if (dregno >= FIRST_PSEUDO_REGISTER)
473                         REG_N_REFS (dregno) += loop_depth;
474                     }
475                   else
476                     {
477                       validate_replace_rtx (dest, src, q);
478                       failed = 1;
479                     }
480                 }
481
482               /* For SREGNO, count the total number of insns scanned.
483                  For DREGNO, count the total number of insns scanned after
484                  passing the death note for DREGNO.  */
485               s_length++;
486               if (dest_death)
487                 d_length++;
488
489               /* If the insn in which SRC dies is a CALL_INSN, don't count it
490                  as a call that has been crossed.  Otherwise, count it.  */
491               if (q != p && GET_CODE (q) == CALL_INSN)
492                 {
493                   /* Similarly, total calls for SREGNO, total calls beyond
494                      the death note for DREGNO.  */
495                   s_n_calls++;
496                   if (dest_death)
497                     d_n_calls++;
498                 }
499
500               /* If DEST dies here, remove the death note and save it for
501                  later.  Make sure ALL of DEST dies here; again, this is
502                  overly conservative.  */
503               if (dest_death == 0
504                   && (dest_death = find_regno_note (q, REG_DEAD, dregno)) != 0)
505                 {
506                   if (GET_MODE (XEXP (dest_death, 0)) != GET_MODE (dest))
507                     failed = 1, dest_death = 0;
508                   else
509                     remove_note (q, dest_death);
510                 }
511             }
512
513           if (! failed)
514             {
515               /* These counters need to be updated if and only if we are
516                  going to move the REG_DEAD note.  */
517               if (sregno >= FIRST_PSEUDO_REGISTER)
518                 {
519                   if (REG_LIVE_LENGTH (sregno) >= 0)
520                     {
521                       REG_LIVE_LENGTH (sregno) -= s_length;
522                       /* REG_LIVE_LENGTH is only an approximation after
523                          combine if sched is not run, so make sure that we
524                          still have a reasonable value.  */
525                       if (REG_LIVE_LENGTH (sregno) < 2)
526                         REG_LIVE_LENGTH (sregno) = 2;
527                     }
528
529                   REG_N_CALLS_CROSSED (sregno) -= s_n_calls;
530                 }
531
532               /* Move death note of SRC from P to INSN.  */
533               remove_note (p, note);
534               XEXP (note, 1) = REG_NOTES (insn);
535               REG_NOTES (insn) = note;
536             }
537
538           /* Put death note of DEST on P if we saw it die.  */
539           if (dest_death)
540             {
541               XEXP (dest_death, 1) = REG_NOTES (p);
542               REG_NOTES (p) = dest_death;
543
544               if (dregno >= FIRST_PSEUDO_REGISTER)
545                 {
546                   /* If and only if we are moving the death note for DREGNO,
547                      then we need to update its counters.  */
548                   if (REG_LIVE_LENGTH (dregno) >= 0)
549                     REG_LIVE_LENGTH (dregno) += d_length;
550                   REG_N_CALLS_CROSSED (dregno) += d_n_calls;
551                 }
552             }
553
554           return ! failed;
555         }
556
557       /* If SRC is a hard register which is set or killed in some other
558          way, we can't do this optimization.  */
559       else if (sregno < FIRST_PSEUDO_REGISTER
560                && dead_or_set_p (p, src))
561         break;
562     }
563   return 0;
564 }
565 \f
566 /* INSN is a copy of SRC to DEST, in which SRC dies.  See if we now have
567    a sequence of insns that modify DEST followed by an insn that sets
568    SRC to DEST in which DEST dies, with no prior modification of DEST.
569    (There is no need to check if the insns in between actually modify
570    DEST.  We should not have cases where DEST is not modified, but
571    the optimization is safe if no such modification is detected.)
572    In that case, we can replace all uses of DEST, starting with INSN and
573    ending with the set of SRC to DEST, with SRC.  We do not do this
574    optimization if a CALL_INSN is crossed unless SRC already crosses a
575    call or if DEST dies before the copy back to SRC.
576
577    It is assumed that DEST and SRC are pseudos; it is too complicated to do
578    this for hard registers since the substitutions we may make might fail.  */
579
580 static void
581 optimize_reg_copy_2 (insn, dest, src)
582      rtx insn;
583      rtx dest;
584      rtx src;
585 {
586   rtx p, q;
587   rtx set;
588   int sregno = REGNO (src);
589   int dregno = REGNO (dest);
590
591   for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
592     {
593       if (GET_CODE (p) == CODE_LABEL || 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       set = single_set (p);
611       if (set && SET_SRC (set) == dest && SET_DEST (set) == src
612           && find_reg_note (p, REG_DEAD, dest))
613         {
614           /* We can do the optimization.  Scan forward from INSN again,
615              replacing regs as we go.  */
616
617           /* Set to stop at next insn.  */
618           for (q = insn; q != NEXT_INSN (p); q = NEXT_INSN (q))
619             if (GET_RTX_CLASS (GET_CODE (q)) == 'i')
620               {
621                 if (reg_mentioned_p (dest, PATTERN (q)))
622                   {
623                     PATTERN (q) = replace_rtx (PATTERN (q), dest, src);
624
625                     /* We assume that a register is used exactly once per
626                        insn in the updates below.  If this is not correct,
627                        no great harm is done.  */
628                     REG_N_REFS (dregno) -= loop_depth;
629                     REG_N_REFS (sregno) += loop_depth;
630                   }
631
632
633               if (GET_CODE (q) == CALL_INSN)
634                 {
635                   REG_N_CALLS_CROSSED (dregno)--;
636                   REG_N_CALLS_CROSSED (sregno)++;
637                 }
638               }
639
640           remove_note (p, find_reg_note (p, REG_DEAD, dest));
641           REG_N_DEATHS (dregno)--;
642           remove_note (insn, find_reg_note (insn, REG_DEAD, src));
643           REG_N_DEATHS (sregno)--;
644           return;
645         }
646
647       if (reg_set_p (src, p)
648           || find_reg_note (p, REG_DEAD, dest)
649           || (GET_CODE (p) == CALL_INSN && REG_N_CALLS_CROSSED (sregno) == 0))
650         break;
651     }
652 }
653 /* INSN is a ZERO_EXTEND or SIGN_EXTEND of SRC to DEST.
654    Look if SRC dies there, and if it is only set once, by loading
655    it from memory.  If so, try to encorporate the zero/sign extension
656    into the memory read, change SRC to the mode of DEST, and alter
657    the remaining accesses to use the appropriate SUBREG.  This allows
658    SRC and DEST to be tied later.  */
659 static void
660 optimize_reg_copy_3 (insn, dest, src)
661      rtx insn;
662      rtx dest;
663      rtx src;
664 {
665   rtx src_reg = XEXP (src, 0);
666   int src_no = REGNO (src_reg);
667   int dst_no = REGNO (dest);
668   rtx p, set, subreg;
669   enum machine_mode old_mode;
670
671   if (src_no < FIRST_PSEUDO_REGISTER
672       || dst_no < FIRST_PSEUDO_REGISTER
673       || ! find_reg_note (insn, REG_DEAD, src_reg)
674       || REG_N_SETS (src_no) != 1)
675     return;
676   for (p = PREV_INSN (insn); ! reg_set_p (src_reg, p); p = PREV_INSN (p))
677     {
678       if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
679           || (GET_CODE (p) == NOTE
680               && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
681                   || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
682         return;
683
684       /* ??? We can't scan past the end of a basic block without updating
685          the register lifetime info (REG_DEAD/basic_block_live_at_start).
686          A CALL_INSN might be the last insn of a basic block, if it is inside
687          an EH region.  There is no easy way to tell, so we just always break
688          when we see a CALL_INSN if flag_exceptions is nonzero.  */
689       if (flag_exceptions && GET_CODE (p) == CALL_INSN)
690         return;
691
692       if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
693         continue;
694     }
695   if (! (set = single_set (p))
696       || GET_CODE (SET_SRC (set)) != MEM
697       || SET_DEST (set) != src_reg)
698     return;
699
700   /* Be conserative: although this optimization is also valid for
701      volatile memory references, that could cause trouble in later passes.  */
702   if (MEM_VOLATILE_P (SET_SRC (set)))
703     return;
704
705   /* Do not use a SUBREG to truncate from one mode to another if truncation
706      is not a nop.  */
707   if (GET_MODE_BITSIZE (GET_MODE (src_reg)) <= GET_MODE_BITSIZE (GET_MODE (src))
708       && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (GET_MODE (src)),
709                                  GET_MODE_BITSIZE (GET_MODE (src_reg))))
710     return;
711
712   old_mode = GET_MODE (src_reg);
713   PUT_MODE (src_reg, GET_MODE (src));
714   XEXP (src, 0) = SET_SRC (set);
715
716   /* Include this change in the group so that it's easily undone if
717      one of the changes in the group is invalid.  */
718   validate_change (p, &SET_SRC (set), src, 1);
719
720   /* Now walk forward making additional replacements.  We want to be able
721      to undo all the changes if a later substitution fails.  */
722   subreg = gen_rtx_SUBREG (old_mode, src_reg, 0);
723   while (p = NEXT_INSN (p), p != insn)
724     {
725       if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
726         continue;
727
728       /* Make a tenative change.  */
729       validate_replace_rtx_group (src_reg, subreg, p);
730     }
731
732   validate_replace_rtx_group (src, src_reg, insn);
733
734   /* Now see if all the changes are valid.  */
735   if (! apply_change_group ())
736     {
737       /* One or more changes were no good.  Back out everything.  */
738       PUT_MODE (src_reg, old_mode);
739       XEXP (src, 0) = src_reg;
740     }
741 }
742
743 \f
744 /* If we were not able to update the users of src to use dest directly, try
745    instead moving the value to dest directly before the operation.  */
746
747 static void
748 copy_src_to_dest (insn, src, dest, loop_depth, old_max_uid)
749      rtx insn;
750      rtx src;
751      rtx dest;
752      int loop_depth;
753      int old_max_uid;
754 {
755   rtx seq;
756   rtx link;
757   rtx next;
758   rtx set;
759   rtx move_insn;
760   rtx *p_insn_notes;
761   rtx *p_move_notes;
762   int src_regno;
763   int dest_regno;
764   int bb;
765   int insn_uid;
766   int move_uid;
767
768   /* A REG_LIVE_LENGTH of -1 indicates the register is equivalent to a constant
769      or memory location and is used infrequently; a REG_LIVE_LENGTH of -2 is
770      parameter when there is no frame pointer that is not allocated a register.
771      For now, we just reject them, rather than incrementing the live length.  */
772
773   if (GET_CODE (src) == REG
774       && REG_LIVE_LENGTH (REGNO (src)) > 0
775       && GET_CODE (dest) == REG
776       && REG_LIVE_LENGTH (REGNO (dest)) > 0
777       && (set = single_set (insn)) != NULL_RTX
778       && !reg_mentioned_p (dest, SET_SRC (set))
779       && GET_MODE (src) == GET_MODE (dest))
780     {
781       int old_num_regs = reg_rtx_no;
782
783       /* Generate the src->dest move.  */
784       start_sequence ();
785       emit_move_insn (dest, src);
786       seq = gen_sequence ();
787       end_sequence ();
788       /* If this sequence uses new registers, we may not use it.  */
789       if (old_num_regs != reg_rtx_no
790           || ! validate_replace_rtx (src, dest, insn))
791         {
792           /* We have to restore reg_rtx_no to its old value, lest
793              recompute_reg_usage will try to compute the usage of the
794              new regs, yet reg_n_info is not valid for them.  */
795           reg_rtx_no = old_num_regs;
796           return;
797         }
798       emit_insn_before (seq, insn);
799       move_insn = PREV_INSN (insn);
800       p_move_notes = &REG_NOTES (move_insn);
801       p_insn_notes = &REG_NOTES (insn);
802
803       /* Move any notes mentioning src to the move instruction */
804       for (link = REG_NOTES (insn); link != NULL_RTX; link = next)
805         {
806           next = XEXP (link, 1);
807           if (XEXP (link, 0) == src)
808             {
809               *p_move_notes = link;
810               p_move_notes = &XEXP (link, 1);
811             }
812           else
813             {
814               *p_insn_notes = link;
815               p_insn_notes = &XEXP (link, 1);
816             }
817         }
818
819       *p_move_notes = NULL_RTX;
820       *p_insn_notes = NULL_RTX;
821
822       /* Is the insn the head of a basic block?  If so extend it */
823       insn_uid = INSN_UID (insn);
824       move_uid = INSN_UID (move_insn);
825       if (insn_uid < old_max_uid)
826         {
827           bb = regmove_bb_head[insn_uid];
828           if (bb >= 0)
829             {
830               BLOCK_HEAD (bb) = move_insn;
831               regmove_bb_head[insn_uid] = -1;
832             }
833         }
834
835       /* Update the various register tables.  */
836       dest_regno = REGNO (dest);
837       REG_N_SETS (dest_regno) += loop_depth;
838       REG_N_REFS (dest_regno) += loop_depth;
839       REG_LIVE_LENGTH (dest_regno)++;
840       if (REGNO_FIRST_UID (dest_regno) == insn_uid)
841         REGNO_FIRST_UID (dest_regno) = move_uid;
842
843       src_regno = REGNO (src);
844       if (! find_reg_note (move_insn, REG_DEAD, src))
845         REG_LIVE_LENGTH (src_regno)++;
846
847       if (REGNO_FIRST_UID (src_regno) == insn_uid)
848         REGNO_FIRST_UID (src_regno) = move_uid;
849
850       if (REGNO_LAST_UID (src_regno) == insn_uid)
851         REGNO_LAST_UID (src_regno) = move_uid;
852
853       if (REGNO_LAST_NOTE_UID (src_regno) == insn_uid)
854         REGNO_LAST_NOTE_UID (src_regno) = move_uid;
855     }
856 }
857
858 \f
859 /* Return whether REG is set in only one location, and is set to a
860    constant, but is set in a different basic block from INSN (an
861    instructions which uses REG).  In this case REG is equivalent to a
862    constant, and we don't want to break that equivalence, because that
863    may increase register pressure and make reload harder.  If REG is
864    set in the same basic block as INSN, we don't worry about it,
865    because we'll probably need a register anyhow (??? but what if REG
866    is used in a different basic block as well as this one?).  FIRST is
867    the first insn in the function.  */
868
869 static int
870 reg_is_remote_constant_p (reg, insn, first)
871      rtx reg;
872      rtx insn;
873      rtx first;
874 {
875   register rtx p;
876
877   if (REG_N_SETS (REGNO (reg)) != 1)
878     return 0;
879
880   /* Look for the set.  */
881   for (p = LOG_LINKS (insn); p; p = XEXP (p, 1))
882     {
883       rtx s;
884
885       if (REG_NOTE_KIND (p) != 0)
886         continue;
887       s = single_set (XEXP (p, 0));
888       if (s != 0
889           && GET_CODE (SET_DEST (s)) == REG
890           && REGNO (SET_DEST (s)) == REGNO (reg))
891         {
892           /* The register is set in the same basic block.  */
893           return 0;
894         }
895     }
896
897   for (p = first; p && p != insn; p = NEXT_INSN (p))
898     {
899       rtx s;
900
901       if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
902         continue;
903       s = single_set (p);
904       if (s != 0
905           && GET_CODE (SET_DEST (s)) == REG
906           && REGNO (SET_DEST (s)) == REGNO (reg))
907         {
908           /* This is the instruction which sets REG.  If there is a
909              REG_EQUAL note, then REG is equivalent to a constant.  */
910           if (find_reg_note (p, REG_EQUAL, NULL_RTX))
911             return 1;
912           return 0;
913         }
914     }
915
916   return 0;
917 }
918
919 /* INSN is adding a CONST_INT to a REG.  We search backwards looking for
920    another add immediate instruction with the same source and dest registers,
921    and if we find one, we change INSN to an increment, and return 1.  If
922    no changes are made, we return 0.
923
924    This changes
925      (set (reg100) (plus reg1 offset1))
926      ...
927      (set (reg100) (plus reg1 offset2))
928    to
929      (set (reg100) (plus reg1 offset1))
930      ...
931      (set (reg100) (plus reg100 offset2-offset1))  */
932
933 /* ??? What does this comment mean?  */
934 /* cse disrupts preincrement / postdecrement squences when it finds a
935    hard register as ultimate source, like the frame pointer.  */
936
937 int
938 fixup_match_2 (insn, dst, src, offset, regmove_dump_file)
939      rtx insn, dst, src, offset;
940      FILE *regmove_dump_file;
941 {
942   rtx p, dst_death = 0;
943   int length, num_calls = 0;
944
945   /* If SRC dies in INSN, we'd have to move the death note.  This is
946      considered to be very unlikely, so we just skip the optimization
947      in this case.  */
948   if (find_regno_note (insn, REG_DEAD, REGNO (src)))
949     return 0;
950
951   /* Scan backward to find the first instruction that sets DST.  */
952
953   for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
954     {
955       rtx pset;
956
957       if (GET_CODE (p) == CODE_LABEL
958           || GET_CODE (p) == JUMP_INSN
959           || (GET_CODE (p) == NOTE
960               && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
961                   || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
962         break;
963
964       /* ??? We can't scan past the end of a basic block without updating
965          the register lifetime info (REG_DEAD/basic_block_live_at_start).
966          A CALL_INSN might be the last insn of a basic block, if it is inside
967          an EH region.  There is no easy way to tell, so we just always break
968          when we see a CALL_INSN if flag_exceptions is nonzero.  */
969       if (flag_exceptions && GET_CODE (p) == CALL_INSN)
970         break;
971
972       if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
973         continue;
974
975       if (find_regno_note (p, REG_DEAD, REGNO (dst)))
976         dst_death = p;
977       if (! dst_death)
978         length++;
979
980       pset = single_set (p);
981       if (pset && SET_DEST (pset) == dst
982           && GET_CODE (SET_SRC (pset)) == PLUS
983           && XEXP (SET_SRC (pset), 0) == src
984           && GET_CODE (XEXP (SET_SRC (pset), 1)) == CONST_INT)
985         {
986           HOST_WIDE_INT newconst
987             = INTVAL (offset) - INTVAL (XEXP (SET_SRC (pset), 1));
988           rtx add = gen_add3_insn (dst, dst, GEN_INT (newconst));
989
990           if (add && validate_change (insn, &PATTERN (insn), add, 0))
991             {
992               /* Remove the death note for DST from DST_DEATH.  */
993               if (dst_death)
994                 {
995                   remove_death (REGNO (dst), dst_death);
996                   REG_LIVE_LENGTH (REGNO (dst)) += length;
997                   REG_N_CALLS_CROSSED (REGNO (dst)) += num_calls;
998                 }
999
1000               REG_N_REFS (REGNO (dst)) += loop_depth;
1001               REG_N_REFS (REGNO (src)) -= loop_depth;
1002
1003               if (regmove_dump_file)
1004                 fprintf (regmove_dump_file,
1005                          "Fixed operand of insn %d.\n",
1006                           INSN_UID (insn));
1007
1008 #ifdef AUTO_INC_DEC
1009               for (p = PREV_INSN (insn); p; p = PREV_INSN (p))
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                   if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
1018                     continue;
1019                   if (reg_overlap_mentioned_p (dst, PATTERN (p)))
1020                     {
1021                       if (try_auto_increment (p, insn, 0, dst, newconst, 0))
1022                         return 1;
1023                       break;
1024                     }
1025                 }
1026               for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
1027                 {
1028                   if (GET_CODE (p) == CODE_LABEL
1029                       || GET_CODE (p) == JUMP_INSN
1030                       || (GET_CODE (p) == NOTE
1031                           && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
1032                               || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
1033                     break;
1034                   if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
1035                     continue;
1036                   if (reg_overlap_mentioned_p (dst, PATTERN (p)))
1037                     {
1038                       try_auto_increment (p, insn, 0, dst, newconst, 1);
1039                       break;
1040                     }
1041                 }
1042 #endif
1043               return 1;
1044             }
1045         }
1046
1047       if (reg_set_p (dst, PATTERN (p)))
1048         break;
1049
1050       /* If we have passed a call instruction, and the
1051          pseudo-reg SRC is not already live across a call,
1052          then don't perform the optimization.  */
1053       /* reg_set_p is overly conservative for CALL_INSNS, thinks that all
1054          hard regs are clobbered.  Thus, we only use it for src for
1055          non-call insns.  */
1056       if (GET_CODE (p) == CALL_INSN)
1057         {
1058           if (! dst_death)
1059             num_calls++;
1060
1061           if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
1062             break;
1063
1064           if (call_used_regs [REGNO (dst)]
1065               || find_reg_fusage (p, CLOBBER, dst))
1066             break;
1067         }
1068       else if (reg_set_p (src, PATTERN (p)))
1069         break;
1070     }
1071
1072   return 0;
1073 }
1074
1075 void
1076 regmove_optimize (f, nregs, regmove_dump_file)
1077      rtx f;
1078      int nregs;
1079      FILE *regmove_dump_file;
1080 {
1081   int old_max_uid = get_max_uid ();
1082   rtx insn;
1083   struct match match;
1084   int pass;
1085   int i;
1086   rtx copy_src, copy_dst;
1087
1088   /* Find out where a potential flags register is live, and so that we
1089      can supress some optimizations in those zones.  */
1090   mark_flags_life_zones (discover_flags_reg ());
1091
1092   regno_src_regno = (int *)alloca (sizeof *regno_src_regno * nregs);
1093   for (i = nregs; --i >= 0; ) regno_src_regno[i] = -1;
1094
1095   regmove_bb_head = (int *)alloca (sizeof (int) * (old_max_uid + 1));
1096   for (i = old_max_uid; i >= 0; i--) regmove_bb_head[i] = -1;
1097   for (i = 0; i < n_basic_blocks; i++)
1098     regmove_bb_head[INSN_UID (BLOCK_HEAD (i))] = i;
1099
1100   /* A forward/backward pass.  Replace output operands with input operands.  */
1101
1102   loop_depth = 1;
1103
1104   for (pass = 0; pass <= 2; pass++)
1105     {
1106       if (! flag_regmove && pass >= flag_expensive_optimizations)
1107         return;
1108
1109       if (regmove_dump_file)
1110         fprintf (regmove_dump_file, "Starting %s pass...\n",
1111                  pass ? "backward" : "forward");
1112
1113       for (insn = pass ? get_last_insn () : f; insn;
1114            insn = pass ? PREV_INSN (insn) : NEXT_INSN (insn))
1115         {
1116           rtx set;
1117           int op_no, match_no;
1118
1119           if (GET_CODE (insn) == NOTE)
1120             {
1121               if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
1122                 loop_depth++;
1123               else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
1124                 loop_depth--;
1125             }
1126
1127           set = single_set (insn);
1128           if (! set)
1129             continue;
1130
1131           if (flag_expensive_optimizations && ! pass
1132               && (GET_CODE (SET_SRC (set)) == SIGN_EXTEND
1133                   || GET_CODE (SET_SRC (set)) == ZERO_EXTEND)
1134               && GET_CODE (XEXP (SET_SRC (set), 0)) == REG
1135               && GET_CODE (SET_DEST(set)) == REG)
1136             optimize_reg_copy_3 (insn, SET_DEST (set), SET_SRC (set));
1137
1138           if (flag_expensive_optimizations && ! pass
1139               && GET_CODE (SET_SRC (set)) == REG
1140               && GET_CODE (SET_DEST(set)) == REG)
1141             {
1142               /* If this is a register-register copy where SRC is not dead,
1143                  see if we can optimize it.  If this optimization succeeds,
1144                  it will become a copy where SRC is dead.  */
1145               if ((find_reg_note (insn, REG_DEAD, SET_SRC (set))
1146                    || optimize_reg_copy_1 (insn, SET_DEST (set), SET_SRC (set)))
1147                   && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
1148                 {
1149                   /* Similarly for a pseudo-pseudo copy when SRC is dead.  */
1150                   if (REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1151                     optimize_reg_copy_2 (insn, SET_DEST (set), SET_SRC (set));
1152                   if (regno_src_regno[REGNO (SET_DEST (set))] < 0
1153                       && SET_SRC (set) != SET_DEST (set))
1154                     {
1155                       int srcregno = REGNO (SET_SRC(set));
1156                       if (regno_src_regno[srcregno] >= 0)
1157                         srcregno = regno_src_regno[srcregno];
1158                       regno_src_regno[REGNO (SET_DEST (set))] = srcregno;
1159                     }
1160                 }
1161             }
1162           if (! flag_regmove)
1163             continue;
1164
1165 #ifdef REGISTER_CONSTRAINTS
1166           if (! find_matches (insn, &match))
1167             continue;
1168
1169           /* Now scan through the operands looking for a source operand
1170              which is supposed to match the destination operand.
1171              Then scan forward for an instruction which uses the dest
1172              operand.
1173              If it dies there, then replace the dest in both operands with
1174              the source operand.  */
1175
1176           for (op_no = 0; op_no < recog_data.n_operands; op_no++)
1177             {
1178               rtx src, dst, src_subreg;
1179               enum reg_class src_class, dst_class;
1180
1181               match_no = match.with[op_no];
1182
1183               /* Nothing to do if the two operands aren't supposed to match.  */
1184               if (match_no < 0)
1185                 continue;
1186
1187               src = recog_data.operand[op_no];
1188               dst = recog_data.operand[match_no];
1189
1190               if (GET_CODE (src) != REG)
1191                 continue;
1192
1193               src_subreg = src;
1194               if (GET_CODE (dst) == SUBREG
1195                   && GET_MODE_SIZE (GET_MODE (dst))
1196                      >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (dst))))
1197                 {
1198                   src_subreg
1199                     = gen_rtx_SUBREG (GET_MODE (SUBREG_REG (dst)),
1200                                       src, SUBREG_WORD (dst));
1201                   dst = SUBREG_REG (dst);
1202                 }
1203               if (GET_CODE (dst) != REG
1204                   || REGNO (dst) < FIRST_PSEUDO_REGISTER)
1205                 continue;
1206
1207               if (REGNO (src) < FIRST_PSEUDO_REGISTER)
1208                 {
1209                   if (match.commutative[op_no] < op_no)
1210                     regno_src_regno[REGNO (dst)] = REGNO (src);
1211                   continue;
1212                 }
1213
1214               if (REG_LIVE_LENGTH (REGNO (src)) < 0)
1215                 continue;
1216
1217               /* op_no/src must be a read-only operand, and
1218                  match_operand/dst must be a write-only operand.  */
1219               if (match.use[op_no] != READ
1220                   || match.use[match_no] != WRITE)
1221                 continue;
1222
1223               if (match.early_clobber[match_no]
1224                   && count_occurrences (PATTERN (insn), src) > 1)
1225                 continue;
1226
1227               /* Make sure match_operand is the destination.  */
1228               if (recog_data.operand[match_no] != SET_DEST (set))
1229                 continue;
1230
1231               /* If the operands already match, then there is nothing to do. */
1232               if (operands_match_p (src, dst))
1233                 continue;
1234
1235               /* But in the commutative case, we might find a better match.  */
1236               if (match.commutative[op_no] >= 0)
1237                 {
1238                   rtx comm = recog_data.operand[match.commutative[op_no]];
1239                   if (operands_match_p (comm, dst)
1240                       && (replacement_quality (comm)
1241                           >= replacement_quality (src)))
1242                     continue;
1243                 }
1244
1245               src_class = reg_preferred_class (REGNO (src));
1246               dst_class = reg_preferred_class (REGNO (dst));
1247               if (! regclass_compatible_p (src_class, dst_class))
1248                 continue;
1249           
1250               if (fixup_match_1 (insn, set, src, src_subreg, dst, pass,
1251                                  op_no, match_no,
1252                                  regmove_dump_file))
1253                 break;
1254             }
1255         }
1256     }
1257
1258   /* A backward pass.  Replace input operands with output operands.  */
1259
1260   if (regmove_dump_file)
1261     fprintf (regmove_dump_file, "Starting backward pass...\n");
1262
1263   loop_depth = 1;
1264
1265   for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1266     {
1267       if (GET_CODE (insn) == NOTE)
1268         {
1269           if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
1270             loop_depth++;
1271           else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
1272             loop_depth--;
1273         }
1274       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1275         {
1276           int op_no, match_no;
1277           int success = 0;
1278
1279           if (! find_matches (insn, &match))
1280             continue;
1281
1282           /* Now scan through the operands looking for a destination operand
1283              which is supposed to match a source operand.
1284              Then scan backward for an instruction which sets the source
1285              operand.  If safe, then replace the source operand with the
1286              dest operand in both instructions.  */
1287
1288           copy_src = NULL_RTX;
1289           copy_dst = NULL_RTX;
1290           for (op_no = 0; op_no < recog_data.n_operands; op_no++)
1291             {
1292               rtx set, p, src, dst;
1293               rtx src_note, dst_note;
1294               int num_calls = 0;
1295               enum reg_class src_class, dst_class;
1296               int length;
1297
1298               match_no = match.with[op_no];
1299
1300               /* Nothing to do if the two operands aren't supposed to match.  */
1301               if (match_no < 0)
1302                 continue;
1303
1304               dst = recog_data.operand[match_no];
1305               src = recog_data.operand[op_no];
1306
1307               if (GET_CODE (src) != REG)
1308                 continue;
1309
1310               if (GET_CODE (dst) != REG
1311                   || REGNO (dst) < FIRST_PSEUDO_REGISTER
1312                   || REG_LIVE_LENGTH (REGNO (dst)) < 0)
1313                 continue;
1314
1315               /* If the operands already match, then there is nothing to do. */
1316               if (operands_match_p (src, dst))
1317                 continue;
1318
1319               if (match.commutative[op_no] >= 0)
1320                 {
1321                   rtx comm = recog_data.operand[match.commutative[op_no]];
1322                   if (operands_match_p (comm, dst))
1323                     continue;
1324                 }
1325
1326               set = single_set (insn);
1327               if (! set)
1328                 continue;
1329
1330               /* match_no/dst must be a write-only operand, and
1331                  operand_operand/src must be a read-only operand.  */
1332               if (match.use[op_no] != READ
1333                   || match.use[match_no] != WRITE)
1334                 continue;
1335
1336               if (match.early_clobber[match_no]
1337                   && count_occurrences (PATTERN (insn), src) > 1)
1338                 continue;
1339
1340               /* Make sure match_no is the destination.  */
1341               if (recog_data.operand[match_no] != SET_DEST (set))
1342                 continue;
1343
1344               if (REGNO (src) < FIRST_PSEUDO_REGISTER)
1345                 {
1346                   if (GET_CODE (SET_SRC (set)) == PLUS
1347                       && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT
1348                       && XEXP (SET_SRC (set), 0) == src
1349                       && fixup_match_2 (insn, dst, src,
1350                                         XEXP (SET_SRC (set), 1),
1351                                         regmove_dump_file))
1352                     break;
1353                   continue;
1354                 }
1355               src_class = reg_preferred_class (REGNO (src));
1356               dst_class = reg_preferred_class (REGNO (dst));
1357               if (! regclass_compatible_p (src_class, dst_class))
1358                 {
1359                   if (!copy_src)
1360                     {
1361                       copy_src = src;
1362                       copy_dst = dst;
1363                     }
1364                   continue;
1365                 }
1366
1367               /* Can not modify an earlier insn to set dst if this insn
1368                  uses an old value in the source.  */
1369               if (reg_overlap_mentioned_p (dst, SET_SRC (set)))
1370                 {
1371                   if (!copy_src)
1372                     {
1373                       copy_src = src;
1374                       copy_dst = dst;
1375                     }
1376                   continue;
1377                 }
1378
1379               if (! (src_note = find_reg_note (insn, REG_DEAD, src)))
1380                 {
1381                   if (!copy_src)
1382                     {
1383                       copy_src = src;
1384                       copy_dst = dst;
1385                     }
1386                   continue;
1387                 }
1388
1389
1390               /* If src is set once in a different basic block,
1391                  and is set equal to a constant, then do not use
1392                  it for this optimization, as this would make it
1393                  no longer equivalent to a constant.  */
1394
1395               if (reg_is_remote_constant_p (src, insn, f))
1396                 {
1397                   if (!copy_src)
1398                     {
1399                       copy_src = src;
1400                       copy_dst = dst;
1401                     }
1402                   continue;
1403                 }
1404
1405
1406               if (regmove_dump_file)
1407                 fprintf (regmove_dump_file,
1408                          "Could fix operand %d of insn %d matching operand %d.\n",
1409                          op_no, INSN_UID (insn), match_no);
1410
1411               /* Scan backward to find the first instruction that uses
1412                  the input operand.  If the operand is set here, then
1413                  replace it in both instructions with match_no.  */
1414
1415               for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
1416                 {
1417                   rtx pset;
1418
1419                   if (GET_CODE (p) == CODE_LABEL
1420                       || GET_CODE (p) == JUMP_INSN
1421                       || (GET_CODE (p) == NOTE
1422                           && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
1423                               || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
1424                     break;
1425
1426                   /* ??? We can't scan past the end of a basic block without
1427                      updating the register lifetime info
1428                      (REG_DEAD/basic_block_live_at_start).
1429                      A CALL_INSN might be the last insn of a basic block, if
1430                      it is inside an EH region.  There is no easy way to tell,
1431                      so we just always break when we see a CALL_INSN if
1432                      flag_exceptions is nonzero.  */
1433                   if (flag_exceptions && GET_CODE (p) == CALL_INSN)
1434                     break;
1435
1436                   if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
1437                     continue;
1438
1439                   length++;
1440
1441                   /* ??? See if all of SRC is set in P.  This test is much
1442                      more conservative than it needs to be.  */
1443                   pset = single_set (p);
1444                   if (pset && SET_DEST (pset) == src)
1445                     {
1446                       /* We use validate_replace_rtx, in case there
1447                          are multiple identical source operands.  All of
1448                          them have to be changed at the same time.  */
1449                       if (validate_replace_rtx (src, dst, insn))
1450                         {
1451                           if (validate_change (p, &SET_DEST (pset),
1452                                                dst, 0))
1453                             success = 1;
1454                           else
1455                             {
1456                               /* Change all source operands back.
1457                                  This modifies the dst as a side-effect.  */
1458                               validate_replace_rtx (dst, src, insn);
1459                               /* Now make sure the dst is right.  */
1460                               validate_change (insn,
1461                                                recog_data.operand_loc[match_no],
1462                                                dst, 0);
1463                             }
1464                         }
1465                       break;
1466                     }
1467
1468                   if (reg_overlap_mentioned_p (src, PATTERN (p))
1469                       || reg_overlap_mentioned_p (dst, PATTERN (p)))
1470                     break;
1471
1472                   /* If we have passed a call instruction, and the
1473                      pseudo-reg DST is not already live across a call,
1474                      then don't perform the optimization.  */
1475                   if (GET_CODE (p) == CALL_INSN)
1476                     {
1477                       num_calls++;
1478
1479                       if (REG_N_CALLS_CROSSED (REGNO (dst)) == 0)
1480                         break;
1481                     }
1482                 }
1483
1484               if (success)
1485                 {
1486                   int dstno, srcno;
1487
1488                   /* Remove the death note for SRC from INSN.  */
1489                   remove_note (insn, src_note);
1490                   /* Move the death note for SRC to P if it is used
1491                      there.  */
1492                   if (reg_overlap_mentioned_p (src, PATTERN (p)))
1493                     {
1494                       XEXP (src_note, 1) = REG_NOTES (p);
1495                       REG_NOTES (p) = src_note;
1496                     }
1497                   /* If there is a REG_DEAD note for DST on P, then remove
1498                      it, because DST is now set there.  */
1499                   if ((dst_note = find_reg_note (p, REG_DEAD, dst)))
1500                     remove_note (p, dst_note);
1501
1502                   dstno = REGNO (dst);
1503                   srcno = REGNO (src);
1504
1505                   REG_N_SETS (dstno)++;
1506                   REG_N_SETS (srcno)--;
1507
1508                   REG_N_CALLS_CROSSED (dstno) += num_calls;
1509                   REG_N_CALLS_CROSSED (srcno) -= num_calls;
1510
1511                   REG_LIVE_LENGTH (dstno) += length;
1512                   if (REG_LIVE_LENGTH (srcno) >= 0)
1513                     {
1514                       REG_LIVE_LENGTH (srcno) -= length;
1515                       /* REG_LIVE_LENGTH is only an approximation after
1516                          combine if sched is not run, so make sure that we
1517                          still have a reasonable value.  */
1518                       if (REG_LIVE_LENGTH (srcno) < 2)
1519                         REG_LIVE_LENGTH (srcno) = 2;
1520                     }
1521
1522                   /* We assume that a register is used exactly once per
1523                      insn in the updates above.  If this is not correct,
1524                      no great harm is done.  */
1525
1526                   REG_N_REFS (dstno) += 2 * loop_depth;
1527                   REG_N_REFS (srcno) -= 2 * loop_depth;
1528
1529                   /* If that was the only time src was set,
1530                      and src was not live at the start of the
1531                      function, we know that we have no more
1532                      references to src; clear REG_N_REFS so it
1533                      won't make reload do any work.  */
1534                   if (REG_N_SETS (REGNO (src)) == 0
1535                       && ! regno_uninitialized (REGNO (src)))
1536                     REG_N_REFS (REGNO (src)) = 0;
1537
1538                   if (regmove_dump_file)
1539                     fprintf (regmove_dump_file,
1540                              "Fixed operand %d of insn %d matching operand %d.\n",
1541                              op_no, INSN_UID (insn), match_no);
1542
1543                   break;
1544                 }
1545             }
1546
1547           /* If we weren't able to replace any of the alternatives, try an
1548              alternative appoach of copying the source to the destination.  */
1549           if (!success && copy_src != NULL_RTX)
1550             copy_src_to_dest (insn, copy_src, copy_dst, loop_depth,
1551                               old_max_uid);
1552
1553         }
1554     }
1555 #endif /* REGISTER_CONSTRAINTS */
1556
1557   /* In fixup_match_1, some insns may have been inserted after basic block
1558      ends.  Fix that here.  */
1559   for (i = 0; i < n_basic_blocks; i++)
1560     {
1561       rtx end = BLOCK_END (i);
1562       rtx new = end;
1563       rtx next = NEXT_INSN (new);
1564       while (next != 0 && INSN_UID (next) >= old_max_uid
1565              && (i == n_basic_blocks - 1 || BLOCK_HEAD (i + 1) != next))
1566         new = next, next = NEXT_INSN (new);
1567       BLOCK_END (i) = new;
1568     }
1569 }
1570
1571 /* Returns nonzero if INSN's pattern has matching constraints for any operand.
1572    Returns 0 if INSN can't be recognized, or if the alternative can't be
1573    determined.
1574
1575    Initialize the info in MATCHP based on the constraints.  */
1576
1577 static int
1578 find_matches (insn, matchp)
1579      rtx insn;
1580      struct match *matchp;
1581 {
1582   int likely_spilled[MAX_RECOG_OPERANDS];
1583   int op_no;
1584   int any_matches = 0;
1585
1586   extract_insn (insn);
1587   if (! constrain_operands (0))
1588     return 0;
1589
1590   /* Must initialize this before main loop, because the code for
1591      the commutative case may set matches for operands other than
1592      the current one.  */
1593   for (op_no = recog_data.n_operands; --op_no >= 0; )
1594     matchp->with[op_no] = matchp->commutative[op_no] = -1;
1595
1596   for (op_no = 0; op_no < recog_data.n_operands; op_no++)
1597     {
1598       const char *p;
1599       char c;
1600       int i = 0;
1601
1602       p = recog_data.constraints[op_no];
1603
1604       likely_spilled[op_no] = 0;
1605       matchp->use[op_no] = READ;
1606       matchp->early_clobber[op_no] = 0;
1607       if (*p == '=')
1608         matchp->use[op_no] = WRITE;
1609       else if (*p == '+')
1610         matchp->use[op_no] = READWRITE;
1611
1612       for (;*p && i < which_alternative; p++)
1613         if (*p == ',')
1614           i++;
1615
1616       while ((c = *p++) != '\0' && c != ',')
1617         switch (c)
1618           {
1619           case '=':
1620             break;
1621           case '+':
1622             break;
1623           case '&':
1624             matchp->early_clobber[op_no] = 1;
1625             break;
1626           case '%':
1627             matchp->commutative[op_no] = op_no + 1;
1628             matchp->commutative[op_no + 1] = op_no;
1629             break;
1630           case '0': case '1': case '2': case '3': case '4':
1631           case '5': case '6': case '7': case '8': case '9':
1632             c -= '0';
1633             if (c < op_no && likely_spilled[(unsigned char) c])
1634               break;
1635             matchp->with[op_no] = c;
1636             any_matches = 1;
1637             if (matchp->commutative[op_no] >= 0)
1638               matchp->with[matchp->commutative[op_no]] = c;
1639             break;
1640           case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'h':
1641           case 'j': case 'k': case 'l': case 'p': case 'q': case 't': case 'u':
1642           case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B':
1643           case 'C': case 'D': case 'W': case 'Y': case 'Z':
1644             if (CLASS_LIKELY_SPILLED_P (REG_CLASS_FROM_LETTER ((unsigned char)c)))
1645               likely_spilled[op_no] = 1;
1646             break;
1647           }
1648     }
1649   return any_matches;
1650 }
1651
1652 /* Try to replace output operand DST in SET, with input operand SRC.  SET is
1653    the only set in INSN.  INSN has just been recognized and constrained.
1654    SRC is operand number OPERAND_NUMBER in INSN.
1655    DST is operand number MATCH_NUMBER in INSN.
1656    If BACKWARD is nonzero, we have been called in a backward pass.
1657    Return nonzero for success.  */
1658 static int
1659 fixup_match_1 (insn, set, src, src_subreg, dst, backward, operand_number,
1660                match_number, regmove_dump_file)
1661      rtx insn, set, src, src_subreg, dst;
1662      int backward, operand_number, match_number;
1663      FILE *regmove_dump_file;
1664 {
1665   rtx p;
1666   rtx post_inc = 0, post_inc_set = 0, search_end = 0;
1667   int success = 0;
1668   int num_calls = 0, s_num_calls = 0;
1669   enum rtx_code code = NOTE;
1670   HOST_WIDE_INT insn_const, newconst;
1671   rtx overlap = 0; /* need to move insn ? */
1672   rtx src_note = find_reg_note (insn, REG_DEAD, src), dst_note;
1673   int length, s_length, true_loop_depth;
1674
1675   if (! src_note)
1676     {
1677       /* Look for (set (regX) (op regA constX))
1678                   (set (regY) (op regA constY))
1679          and change that to
1680                   (set (regA) (op regA constX)).
1681                   (set (regY) (op regA constY-constX)).
1682          This works for add and shift operations, if
1683          regA is dead after or set by the second insn.  */
1684
1685       code = GET_CODE (SET_SRC (set));
1686       if ((code == PLUS || code == LSHIFTRT
1687            || code == ASHIFT || code == ASHIFTRT)
1688           && XEXP (SET_SRC (set), 0) == src
1689           && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
1690         insn_const = INTVAL (XEXP (SET_SRC (set), 1));
1691       else if (! stable_but_for_p (SET_SRC (set), src, dst))
1692         return 0;
1693       else
1694         /* We might find a src_note while scanning.  */
1695         code = NOTE;
1696     }
1697
1698   if (regmove_dump_file)
1699     fprintf (regmove_dump_file,
1700              "Could fix operand %d of insn %d matching operand %d.\n",
1701              operand_number, INSN_UID (insn), match_number);
1702
1703   /* If SRC is equivalent to a constant set in a different basic block,
1704      then do not use it for this optimization.  We want the equivalence
1705      so that if we have to reload this register, we can reload the
1706      constant, rather than extending the lifespan of the register.  */
1707   if (reg_is_remote_constant_p (src, insn, get_insns ()))
1708     return 0;
1709
1710   /* Scan forward to find the next instruction that
1711      uses the output operand.  If the operand dies here,
1712      then replace it in both instructions with
1713      operand_number.  */
1714
1715   for (length = s_length = 0, p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
1716     {
1717       if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
1718           || (GET_CODE (p) == NOTE
1719               && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
1720                   || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
1721         break;
1722
1723       /* ??? We can't scan past the end of a basic block without updating
1724          the register lifetime info (REG_DEAD/basic_block_live_at_start).
1725          A CALL_INSN might be the last insn of a basic block, if it is
1726          inside an EH region.  There is no easy way to tell, so we just
1727          always break when we see a CALL_INSN if flag_exceptions is nonzero.  */
1728       if (flag_exceptions && GET_CODE (p) == CALL_INSN)
1729         break;
1730
1731       if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
1732         continue;
1733
1734       length++;
1735       if (src_note)
1736         s_length++;
1737
1738       if (reg_set_p (src, p) || reg_set_p (dst, p)
1739           || (GET_CODE (PATTERN (p)) == USE
1740               && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
1741         break;
1742
1743       /* See if all of DST dies in P.  This test is
1744          slightly more conservative than it needs to be.  */
1745       if ((dst_note = find_regno_note (p, REG_DEAD, REGNO (dst)))
1746           && (GET_MODE (XEXP (dst_note, 0)) == GET_MODE (dst)))
1747         {
1748           /* If we would be moving INSN, check that we won't move it
1749              into the shadow of a live a live flags register.  */
1750           /* ??? We only try to move it in front of P, although
1751                  we could move it anywhere between OVERLAP and P.  */
1752           if (overlap && GET_MODE (PREV_INSN (p)) != VOIDmode)
1753             break;
1754
1755           if (! src_note)
1756             {
1757               rtx q;
1758               rtx set2;
1759
1760               /* If an optimization is done, the value of SRC while P
1761                  is executed will be changed.  Check that this is OK.  */
1762               if (reg_overlap_mentioned_p (src, PATTERN (p)))
1763                 break;
1764               for (q = p; q; q = NEXT_INSN (q))
1765                 {
1766                   if (GET_CODE (q) == CODE_LABEL || GET_CODE (q) == JUMP_INSN
1767                       || (GET_CODE (q) == NOTE
1768                           && (NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_BEG
1769                               || NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_END)))
1770                     {
1771                       q = 0;
1772                       break;
1773                     }
1774
1775                   /* ??? We can't scan past the end of a basic block without
1776                      updating the register lifetime info
1777                      (REG_DEAD/basic_block_live_at_start).
1778                      A CALL_INSN might be the last insn of a basic block, if
1779                      it is inside an EH region.  There is no easy way to tell,
1780                      so we just always break when we see a CALL_INSN if
1781                      flag_exceptions is nonzero.  */
1782                   if (flag_exceptions && GET_CODE (q) == CALL_INSN)
1783                     {
1784                       q = 0;
1785                       break;
1786                     }
1787
1788                   if (GET_RTX_CLASS (GET_CODE (q)) != 'i')
1789                     continue;
1790                   if (reg_overlap_mentioned_p (src, PATTERN (q))
1791                       || reg_set_p (src, q))
1792                     break;
1793                 }
1794               if (q)
1795                 set2 = single_set (q);
1796               if (! q || ! set2 || GET_CODE (SET_SRC (set2)) != code
1797                   || XEXP (SET_SRC (set2), 0) != src
1798                   || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT
1799                   || (SET_DEST (set2) != src
1800                       && ! find_reg_note (q, REG_DEAD, src)))
1801                 {
1802                   /* If this is a PLUS, we can still save a register by doing
1803                      src += insn_const;
1804                      P;
1805                      src -= insn_const; .
1806                      This also gives opportunities for subsequent
1807                      optimizations in the backward pass, so do it there.  */
1808                   if (code == PLUS && backward
1809                       /* Don't do this if we can likely tie DST to SET_DEST
1810                          of P later; we can't do this tying here if we got a
1811                          hard register.  */
1812                       && ! (dst_note && ! REG_N_CALLS_CROSSED (REGNO (dst))
1813                             && single_set (p)
1814                             && GET_CODE (SET_DEST (single_set (p))) == REG
1815                             && (REGNO (SET_DEST (single_set (p)))
1816                                 < FIRST_PSEUDO_REGISTER))
1817                       /* We may only emit an insn directly after P if we
1818                          are not in the shadow of a live flags register.  */
1819                       && GET_MODE (p) == VOIDmode)
1820                     {
1821                       search_end = q;
1822                       q = insn;
1823                       set2 = set;
1824                       newconst = -insn_const;
1825                       code = MINUS;
1826                     }
1827                   else
1828                     break;
1829                 }
1830               else
1831                 {
1832                   newconst = INTVAL (XEXP (SET_SRC (set2), 1)) - insn_const;
1833                   /* Reject out of range shifts.  */
1834                   if (code != PLUS
1835                       && (newconst < 0
1836                           || (newconst
1837                               >= GET_MODE_BITSIZE (GET_MODE (SET_SRC (set2))))))
1838                     break;
1839                   if (code == PLUS)
1840                     {
1841                       post_inc = q;
1842                       if (SET_DEST (set2) != src)
1843                         post_inc_set = set2;
1844                     }
1845                 }
1846               /* We use 1 as last argument to validate_change so that all
1847                  changes are accepted or rejected together by apply_change_group
1848                  when it is called by validate_replace_rtx .  */
1849               validate_change (q, &XEXP (SET_SRC (set2), 1),
1850                                GEN_INT (newconst), 1);
1851             }
1852           validate_change (insn, recog_data.operand_loc[match_number], src, 1);
1853           if (validate_replace_rtx (dst, src_subreg, p))
1854             success = 1;
1855           break;
1856         }
1857
1858       if (reg_overlap_mentioned_p (dst, PATTERN (p)))
1859         break;
1860       if (! src_note && reg_overlap_mentioned_p (src, PATTERN (p)))
1861         {
1862           /* INSN was already checked to be movable wrt. the registers that it
1863              sets / uses when we found no REG_DEAD note for src on it, but it
1864              still might clobber the flags register.  We'll have to check that
1865              we won't insert it into the shadow of a live flags register when
1866              we finally know where we are to move it.  */
1867           overlap = p;
1868           src_note = find_reg_note (p, REG_DEAD, src);
1869         }
1870
1871       /* If we have passed a call instruction, and the pseudo-reg SRC is not
1872          already live across a call, then don't perform the optimization.  */
1873       if (GET_CODE (p) == CALL_INSN)
1874         {
1875           if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
1876             break;
1877
1878           num_calls++;
1879
1880           if (src_note)
1881             s_num_calls++;
1882
1883         }
1884     }
1885
1886   if (! success)
1887     return 0;
1888
1889   true_loop_depth = backward ? 2 - loop_depth : loop_depth;
1890
1891   /* Remove the death note for DST from P.  */
1892   remove_note (p, dst_note);
1893   if (code == MINUS)
1894     {
1895       post_inc = emit_insn_after (copy_rtx (PATTERN (insn)), p);
1896       if ((HAVE_PRE_INCREMENT || HAVE_PRE_DECREMENT)
1897           && search_end
1898           && try_auto_increment (search_end, post_inc, 0, src, newconst, 1))
1899         post_inc = 0;
1900       validate_change (insn, &XEXP (SET_SRC (set), 1), GEN_INT (insn_const), 0);
1901       REG_N_SETS (REGNO (src))++;
1902       REG_N_REFS (REGNO (src)) += true_loop_depth;
1903       REG_LIVE_LENGTH (REGNO (src))++;
1904     }
1905   if (overlap)
1906     {
1907       /* The lifetime of src and dest overlap,
1908          but we can change this by moving insn.  */
1909       rtx pat = PATTERN (insn);
1910       if (src_note)
1911         remove_note (overlap, src_note);
1912       if ((HAVE_POST_INCREMENT || HAVE_POST_DECREMENT)
1913           && code == PLUS
1914           && try_auto_increment (overlap, insn, 0, src, insn_const, 0))
1915         insn = overlap;
1916       else
1917         {
1918           rtx notes = REG_NOTES (insn);
1919
1920           emit_insn_after_with_line_notes (pat, PREV_INSN (p), insn);
1921           PUT_CODE (insn, NOTE);
1922           NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1923           NOTE_SOURCE_FILE (insn) = 0;
1924           /* emit_insn_after_with_line_notes has no
1925              return value, so search for the new insn.  */
1926           insn = p;
1927           while (GET_RTX_CLASS (GET_CODE (insn)) != 'i'
1928                  || PATTERN (insn) != pat)
1929             insn = PREV_INSN (insn);
1930
1931           REG_NOTES (insn) = notes;
1932         }
1933     }
1934   /* Sometimes we'd generate src = const; src += n;
1935      if so, replace the instruction that set src
1936      in the first place.  */
1937
1938   if (! overlap && (code == PLUS || code == MINUS))
1939     {
1940       rtx note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
1941       rtx q, set2;
1942       int num_calls2 = 0, s_length2 = 0;
1943
1944       if (note && CONSTANT_P (XEXP (note, 0)))
1945         {
1946           for (q = PREV_INSN (insn); q; q = PREV_INSN(q))
1947             {
1948               if (GET_CODE (q) == CODE_LABEL || GET_CODE (q) == JUMP_INSN
1949                   || (GET_CODE (q) == NOTE
1950                       && (NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_BEG
1951                           || NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_END)))
1952                 {
1953                   q = 0;
1954                   break;
1955                 }
1956
1957               /* ??? We can't scan past the end of a basic block without
1958                  updating the register lifetime info
1959                  (REG_DEAD/basic_block_live_at_start).
1960                  A CALL_INSN might be the last insn of a basic block, if
1961                  it is inside an EH region.  There is no easy way to tell,
1962                  so we just always break when we see a CALL_INSN if
1963                  flag_exceptions is nonzero.  */
1964               if (flag_exceptions && GET_CODE (q) == CALL_INSN)
1965                 {
1966                   q = 0;
1967                   break;
1968                 }
1969
1970               if (GET_RTX_CLASS (GET_CODE (q)) != 'i')
1971                 continue;
1972               s_length2++;
1973               if (reg_set_p (src, q))
1974                 {
1975                   set2 = single_set (q);
1976                   break;
1977                 }
1978               if (reg_overlap_mentioned_p (src, PATTERN (q)))
1979                 {
1980                   q = 0;
1981                   break;
1982                 }
1983               if (GET_CODE (p) == CALL_INSN)
1984                 num_calls2++;
1985             }
1986           if (q && set2 && SET_DEST (set2) == src && CONSTANT_P (SET_SRC (set2))
1987               && validate_change (insn, &SET_SRC (set), XEXP (note, 0), 0))
1988             {
1989               PUT_CODE (q, NOTE);
1990               NOTE_LINE_NUMBER (q) = NOTE_INSN_DELETED;
1991               NOTE_SOURCE_FILE (q) = 0;
1992               REG_N_SETS (REGNO (src))--;
1993               REG_N_CALLS_CROSSED (REGNO (src)) -= num_calls2;
1994               REG_N_REFS (REGNO (src)) -= true_loop_depth;
1995               REG_LIVE_LENGTH (REGNO (src)) -= s_length2;
1996               insn_const = 0;
1997             }
1998         }
1999     }
2000
2001   if ((HAVE_PRE_INCREMENT || HAVE_PRE_DECREMENT)
2002            && (code == PLUS || code == MINUS) && insn_const
2003            && try_auto_increment (p, insn, 0, src, insn_const, 1))
2004     insn = p;
2005   else if ((HAVE_POST_INCREMENT || HAVE_POST_DECREMENT)
2006            && post_inc
2007            && try_auto_increment (p, post_inc, post_inc_set, src, newconst, 0))
2008     post_inc = 0;
2009   /* If post_inc still prevails, try to find an
2010      insn where it can be used as a pre-in/decrement.
2011      If code is MINUS, this was already tried.  */
2012   if (post_inc && code == PLUS
2013   /* Check that newconst is likely to be usable
2014      in a pre-in/decrement before starting the search.  */
2015       && ((HAVE_PRE_INCREMENT && newconst > 0 && newconst <= MOVE_MAX)
2016           || (HAVE_PRE_DECREMENT && newconst < 0 && newconst >= -MOVE_MAX))
2017       && exact_log2 (newconst))
2018     {
2019       rtx q, inc_dest;
2020
2021       inc_dest = post_inc_set ? SET_DEST (post_inc_set) : src;
2022       for (q = post_inc; (q = NEXT_INSN (q)); )
2023         {
2024           if (GET_CODE (q) == CODE_LABEL || GET_CODE (q) == JUMP_INSN
2025               || (GET_CODE (q) == NOTE
2026                   && (NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_BEG
2027                       || NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_END)))
2028             break;
2029
2030           /* ??? We can't scan past the end of a basic block without updating
2031              the register lifetime info (REG_DEAD/basic_block_live_at_start).
2032              A CALL_INSN might be the last insn of a basic block, if it
2033              is inside an EH region.  There is no easy way to tell so we
2034              just always break when we see a CALL_INSN if flag_exceptions
2035              is nonzero.  */
2036           if (flag_exceptions && GET_CODE (q) == CALL_INSN)
2037             break;
2038
2039           if (GET_RTX_CLASS (GET_CODE (q)) != 'i')
2040             continue;
2041           if (src != inc_dest && (reg_overlap_mentioned_p (src, PATTERN (q))
2042                                   || reg_set_p (src, q)))
2043             break;
2044           if (reg_set_p (inc_dest, q))
2045             break;
2046           if (reg_overlap_mentioned_p (inc_dest, PATTERN (q)))
2047             {
2048               try_auto_increment (q, post_inc,
2049                                   post_inc_set, inc_dest, newconst, 1);
2050               break;
2051             }
2052         }
2053     }
2054   /* Move the death note for DST to INSN if it is used
2055      there.  */
2056   if (reg_overlap_mentioned_p (dst, PATTERN (insn)))
2057     {
2058       XEXP (dst_note, 1) = REG_NOTES (insn);
2059       REG_NOTES (insn) = dst_note;
2060     }
2061
2062   if (src_note)
2063     {
2064       /* Move the death note for SRC from INSN to P.  */
2065       if (! overlap)
2066         remove_note (insn, src_note);
2067       XEXP (src_note, 1) = REG_NOTES (p);
2068       REG_NOTES (p) = src_note;
2069
2070       REG_N_CALLS_CROSSED (REGNO (src)) += s_num_calls;
2071     }
2072
2073   REG_N_SETS (REGNO (src))++;
2074   REG_N_SETS (REGNO (dst))--;
2075
2076   REG_N_CALLS_CROSSED (REGNO (dst)) -= num_calls;
2077
2078   REG_LIVE_LENGTH (REGNO (src)) += s_length;
2079   if (REG_LIVE_LENGTH (REGNO (dst)) >= 0)
2080     {
2081       REG_LIVE_LENGTH (REGNO (dst)) -= length;
2082       /* REG_LIVE_LENGTH is only an approximation after
2083          combine if sched is not run, so make sure that we
2084          still have a reasonable value.  */
2085       if (REG_LIVE_LENGTH (REGNO (dst)) < 2)
2086         REG_LIVE_LENGTH (REGNO (dst)) = 2;
2087     }
2088
2089   /* We assume that a register is used exactly once per
2090       insn in the updates above.  If this is not correct,
2091       no great harm is done.  */
2092
2093   REG_N_REFS (REGNO (src)) += 2 * true_loop_depth;
2094   REG_N_REFS (REGNO (dst)) -= 2 * true_loop_depth;
2095
2096   /* If that was the only time dst was set,
2097      and dst was not live at the start of the
2098      function, we know that we have no more
2099      references to dst; clear REG_N_REFS so it
2100      won't make reload do any work.  */
2101   if (REG_N_SETS (REGNO (dst)) == 0
2102       && ! regno_uninitialized (REGNO (dst)))
2103     REG_N_REFS (REGNO (dst)) = 0;
2104
2105   if (regmove_dump_file)
2106     fprintf (regmove_dump_file,
2107              "Fixed operand %d of insn %d matching operand %d.\n",
2108              operand_number, INSN_UID (insn), match_number);
2109   return 1;
2110 }
2111
2112
2113 /* return nonzero if X is stable but for mentioning SRC or mentioning /
2114    changing DST .  If in doubt, presume it is unstable.  */
2115 static int
2116 stable_but_for_p (x, src, dst)
2117      rtx x, src, dst;
2118 {
2119   RTX_CODE code = GET_CODE (x);
2120   switch (GET_RTX_CLASS (code))
2121     {
2122     case '<': case '1': case 'c': case '2': case 'b': case '3':
2123       {
2124         int i;
2125         const char *fmt = GET_RTX_FORMAT (code);
2126         for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2127           if (fmt[i] == 'e' && ! stable_but_for_p (XEXP (x, i), src, dst))
2128               return 0;
2129         return 1;
2130       }
2131     case 'o':
2132       if (x == src || x == dst)
2133         return 1;
2134       /* fall through */
2135     default:
2136       return ! rtx_unstable_p (x);
2137     }
2138 }