OSDN Git Service

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