OSDN Git Service

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