OSDN Git Service

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