OSDN Git Service

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