OSDN Git Service

* stor-layout.c (layout_type): Complain if an array's size can
[pf3gnuchains/gcc-fork.git] / gcc / regmove.c
1 /* Move registers around to reduce number of move instructions needed.
2    Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22
23 /* This module looks for cases where matching constraints would force
24    an instruction to need a reload, and this reload would be a register
25    to register move.  It then attempts to change the registers used by the
26    instruction to avoid the move instruction.  */
27
28 #include "config.h"
29 #include "system.h"
30 #include "rtl.h" /* stdio.h must precede rtl.h for FFS.  */
31 #include "tm_p.h"
32 #include "insn-config.h"
33 #include "recog.h"
34 #include "output.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 "basic-block.h"
41 #include "except.h"
42 #include "toplev.h"
43 #include "reload.h"
44
45
46 /* Turn STACK_GROWS_DOWNWARD into a boolean.  */
47 #ifdef STACK_GROWS_DOWNWARD
48 #undef STACK_GROWS_DOWNWARD
49 #define STACK_GROWS_DOWNWARD 1
50 #else
51 #define STACK_GROWS_DOWNWARD 0
52 #endif
53
54 static int perhaps_ends_bb_p    PARAMS ((rtx));
55 static int optimize_reg_copy_1  PARAMS ((rtx, rtx, rtx));
56 static void optimize_reg_copy_2 PARAMS ((rtx, rtx, rtx));
57 static void optimize_reg_copy_3 PARAMS ((rtx, rtx, rtx));
58 static void copy_src_to_dest    PARAMS ((rtx, rtx, rtx, int));
59 static int *regmove_bb_head;
60
61 struct match {
62   int with[MAX_RECOG_OPERANDS];
63   enum { READ, WRITE, READWRITE } use[MAX_RECOG_OPERANDS];
64   int commutative[MAX_RECOG_OPERANDS];
65   int early_clobber[MAX_RECOG_OPERANDS];
66 };
67
68 static rtx discover_flags_reg PARAMS ((void));
69 static void mark_flags_life_zones PARAMS ((rtx));
70 static void flags_set_1 PARAMS ((rtx, rtx, void *));
71
72 static int try_auto_increment PARAMS ((rtx, rtx, rtx, rtx, HOST_WIDE_INT, int));
73 static int find_matches PARAMS ((rtx, struct match *));
74 static void replace_in_call_usage PARAMS ((rtx *, unsigned int, rtx, rtx));
75 static int fixup_match_1 PARAMS ((rtx, rtx, rtx, rtx, rtx, int, int, int, FILE *))
76 ;
77 static int reg_is_remote_constant_p PARAMS ((rtx, rtx, rtx));
78 static int stable_and_no_regs_but_for_p PARAMS ((rtx, rtx, rtx));
79 static int regclass_compatible_p PARAMS ((int, int));
80 static int replacement_quality PARAMS ((rtx));
81 static int fixup_match_2 PARAMS ((rtx, rtx, rtx, rtx, FILE *));
82
83 /* Return non-zero if registers with CLASS1 and CLASS2 can be merged without
84    causing too much register allocation problems.  */
85 static int
86 regclass_compatible_p (class0, class1)
87      int class0, class1;
88 {
89   return (class0 == class1
90           || (reg_class_subset_p (class0, class1)
91               && ! CLASS_LIKELY_SPILLED_P (class0))
92           || (reg_class_subset_p (class1, class0)
93               && ! CLASS_LIKELY_SPILLED_P (class1)));
94 }
95
96 /* INC_INSN is an instruction that adds INCREMENT to REG.
97    Try to fold INC_INSN as a post/pre in/decrement into INSN.
98    Iff INC_INSN_SET is nonzero, inc_insn has a destination different from src.
99    Return nonzero for success.  */
100 static int
101 try_auto_increment (insn, inc_insn, inc_insn_set, reg, increment, pre)
102      rtx reg, insn, inc_insn ,inc_insn_set;
103      HOST_WIDE_INT increment;
104      int pre;
105 {
106   enum rtx_code inc_code;
107
108   rtx pset = single_set (insn);
109   if (pset)
110     {
111       /* Can't use the size of SET_SRC, we might have something like
112          (sign_extend:SI (mem:QI ...  */
113       rtx use = find_use_as_address (pset, reg, 0);
114       if (use != 0 && use != (rtx) 1)
115         {
116           int size = GET_MODE_SIZE (GET_MODE (use));
117           if (0
118               || (HAVE_POST_INCREMENT
119                   && pre == 0 && (inc_code = POST_INC, increment == size))
120               || (HAVE_PRE_INCREMENT
121                   && pre == 1 && (inc_code = PRE_INC, increment == size))
122               || (HAVE_POST_DECREMENT
123                   && pre == 0 && (inc_code = POST_DEC, increment == -size))
124               || (HAVE_PRE_DECREMENT
125                   && pre == 1 && (inc_code = PRE_DEC, increment == -size))
126           )
127             {
128               if (inc_insn_set)
129                 validate_change
130                   (inc_insn,
131                    &SET_SRC (inc_insn_set),
132                    XEXP (SET_SRC (inc_insn_set), 0), 1);
133               validate_change (insn, &XEXP (use, 0),
134                                gen_rtx_fmt_e (inc_code, Pmode, reg), 1);
135               if (apply_change_group ())
136                 {
137                   /* If there is a REG_DEAD note on this insn, we must
138                      change this not to REG_UNUSED meaning that the register
139                      is set, but the value is dead.  Failure to do so will
140                      result in a sched1 abort -- when it recomputes lifetime
141                      information, the number of REG_DEAD notes will have
142                      changed.  */
143                   rtx note = find_reg_note (insn, REG_DEAD, reg);
144                   if (note)
145                     PUT_MODE (note, REG_UNUSED);
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 (INSN_P (insn))
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 \f
371 /* Return 1 if INSN might end a basic block.  */
372
373 static int perhaps_ends_bb_p (insn)
374      rtx insn;
375 {
376   switch (GET_CODE (insn))
377     {
378     case CODE_LABEL:
379     case JUMP_INSN:
380       /* These always end a basic block.  */
381       return 1;
382
383     case CALL_INSN:
384       /* A CALL_INSN might be the last insn of a basic block, if it is inside
385          an EH region or if there are nonlocal gotos.  Note that this test is
386          very conservative.  */
387       if (nonlocal_goto_handler_labels)
388         return 1;
389       /* FALLTHRU */
390     default:
391       return can_throw_internal (insn);
392     }
393 }
394 \f
395 /* INSN is a copy from SRC to DEST, both registers, and SRC does not die
396    in INSN.
397
398    Search forward to see if SRC dies before either it or DEST is modified,
399    but don't scan past the end of a basic block.  If so, we can replace SRC
400    with DEST and let SRC die in INSN.
401
402    This will reduce the number of registers live in that range and may enable
403    DEST to be tied to SRC, thus often saving one register in addition to a
404    register-register copy.  */
405
406 static int
407 optimize_reg_copy_1 (insn, dest, src)
408      rtx insn;
409      rtx dest;
410      rtx src;
411 {
412   rtx p, q;
413   rtx note;
414   rtx dest_death = 0;
415   int sregno = REGNO (src);
416   int dregno = REGNO (dest);
417
418   /* We don't want to mess with hard regs if register classes are small.  */
419   if (sregno == dregno
420       || (SMALL_REGISTER_CLASSES
421           && (sregno < FIRST_PSEUDO_REGISTER
422               || dregno < FIRST_PSEUDO_REGISTER))
423       /* We don't see all updates to SP if they are in an auto-inc memory
424          reference, so we must disallow this optimization on them.  */
425       || sregno == STACK_POINTER_REGNUM || dregno == STACK_POINTER_REGNUM)
426     return 0;
427
428   for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
429     {
430       /* ??? We can't scan past the end of a basic block without updating
431          the register lifetime info (REG_DEAD/basic_block_live_at_start).  */
432       if (perhaps_ends_bb_p (p))
433         break;
434       else if (! INSN_P (p))
435         continue;
436
437       if (reg_set_p (src, p) || reg_set_p (dest, p)
438           /* Don't change a USE of a register.  */
439           || (GET_CODE (PATTERN (p)) == USE
440               && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
441         break;
442
443       /* See if all of SRC dies in P.  This test is slightly more
444          conservative than it needs to be.  */
445       if ((note = find_regno_note (p, REG_DEAD, sregno)) != 0
446           && GET_MODE (XEXP (note, 0)) == GET_MODE (src))
447         {
448           int failed = 0;
449           int d_length = 0;
450           int s_length = 0;
451           int d_n_calls = 0;
452           int s_n_calls = 0;
453
454           /* We can do the optimization.  Scan forward from INSN again,
455              replacing regs as we go.  Set FAILED if a replacement can't
456              be done.  In that case, we can't move the death note for SRC.
457              This should be rare.  */
458
459           /* Set to stop at next insn.  */
460           for (q = next_real_insn (insn);
461                q != next_real_insn (p);
462                q = next_real_insn (q))
463             {
464               if (reg_overlap_mentioned_p (src, PATTERN (q)))
465                 {
466                   /* If SRC is a hard register, we might miss some
467                      overlapping registers with validate_replace_rtx,
468                      so we would have to undo it.  We can't if DEST is
469                      present in the insn, so fail in that combination
470                      of cases.  */
471                   if (sregno < FIRST_PSEUDO_REGISTER
472                       && reg_mentioned_p (dest, PATTERN (q)))
473                     failed = 1;
474
475                   /* Replace all uses and make sure that the register
476                      isn't still present.  */
477                   else if (validate_replace_rtx (src, dest, q)
478                            && (sregno >= FIRST_PSEUDO_REGISTER
479                                || ! reg_overlap_mentioned_p (src,
480                                                              PATTERN (q))))
481                     ;
482                   else
483                     {
484                       validate_replace_rtx (dest, src, q);
485                       failed = 1;
486                     }
487                 }
488
489               /* For SREGNO, count the total number of insns scanned.
490                  For DREGNO, count the total number of insns scanned after
491                  passing the death note for DREGNO.  */
492               s_length++;
493               if (dest_death)
494                 d_length++;
495
496               /* If the insn in which SRC dies is a CALL_INSN, don't count it
497                  as a call that has been crossed.  Otherwise, count it.  */
498               if (q != p && GET_CODE (q) == CALL_INSN)
499                 {
500                   /* Similarly, total calls for SREGNO, total calls beyond
501                      the death note for DREGNO.  */
502                   s_n_calls++;
503                   if (dest_death)
504                     d_n_calls++;
505                 }
506
507               /* If DEST dies here, remove the death note and save it for
508                  later.  Make sure ALL of DEST dies here; again, this is
509                  overly conservative.  */
510               if (dest_death == 0
511                   && (dest_death = find_regno_note (q, REG_DEAD, dregno)) != 0)
512                 {
513                   if (GET_MODE (XEXP (dest_death, 0)) != GET_MODE (dest))
514                     failed = 1, dest_death = 0;
515                   else
516                     remove_note (q, dest_death);
517                 }
518             }
519
520           if (! failed)
521             {
522               /* These counters need to be updated if and only if we are
523                  going to move the REG_DEAD note.  */
524               if (sregno >= FIRST_PSEUDO_REGISTER)
525                 {
526                   if (REG_LIVE_LENGTH (sregno) >= 0)
527                     {
528                       REG_LIVE_LENGTH (sregno) -= s_length;
529                       /* REG_LIVE_LENGTH is only an approximation after
530                          combine if sched is not run, so make sure that we
531                          still have a reasonable value.  */
532                       if (REG_LIVE_LENGTH (sregno) < 2)
533                         REG_LIVE_LENGTH (sregno) = 2;
534                     }
535
536                   REG_N_CALLS_CROSSED (sregno) -= s_n_calls;
537                 }
538
539               /* Move death note of SRC from P to INSN.  */
540               remove_note (p, note);
541               XEXP (note, 1) = REG_NOTES (insn);
542               REG_NOTES (insn) = note;
543             }
544
545           /* DEST is also dead if INSN has a REG_UNUSED note for DEST.  */
546           if (! dest_death
547               && (dest_death = find_regno_note (insn, REG_UNUSED, dregno)))
548             {
549               PUT_REG_NOTE_KIND (dest_death, REG_DEAD);
550               remove_note (insn, dest_death);
551             }
552
553           /* Put death note of DEST on P if we saw it die.  */
554           if (dest_death)
555             {
556               XEXP (dest_death, 1) = REG_NOTES (p);
557               REG_NOTES (p) = dest_death;
558
559               if (dregno >= FIRST_PSEUDO_REGISTER)
560                 {
561                   /* If and only if we are moving the death note for DREGNO,
562                      then we need to update its counters.  */
563                   if (REG_LIVE_LENGTH (dregno) >= 0)
564                     REG_LIVE_LENGTH (dregno) += d_length;
565                   REG_N_CALLS_CROSSED (dregno) += d_n_calls;
566                 }
567             }
568
569           return ! failed;
570         }
571
572       /* If SRC is a hard register which is set or killed in some other
573          way, we can't do this optimization.  */
574       else if (sregno < FIRST_PSEUDO_REGISTER
575                && dead_or_set_p (p, src))
576         break;
577     }
578   return 0;
579 }
580 \f
581 /* INSN is a copy of SRC to DEST, in which SRC dies.  See if we now have
582    a sequence of insns that modify DEST followed by an insn that sets
583    SRC to DEST in which DEST dies, with no prior modification of DEST.
584    (There is no need to check if the insns in between actually modify
585    DEST.  We should not have cases where DEST is not modified, but
586    the optimization is safe if no such modification is detected.)
587    In that case, we can replace all uses of DEST, starting with INSN and
588    ending with the set of SRC to DEST, with SRC.  We do not do this
589    optimization if a CALL_INSN is crossed unless SRC already crosses a
590    call or if DEST dies before the copy back to SRC.
591
592    It is assumed that DEST and SRC are pseudos; it is too complicated to do
593    this for hard registers since the substitutions we may make might fail.  */
594
595 static void
596 optimize_reg_copy_2 (insn, dest, src)
597      rtx insn;
598      rtx dest;
599      rtx src;
600 {
601   rtx p, q;
602   rtx set;
603   int sregno = REGNO (src);
604   int dregno = REGNO (dest);
605
606   for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
607     {
608       /* ??? We can't scan past the end of a basic block without updating
609          the register lifetime info (REG_DEAD/basic_block_live_at_start).  */
610       if (perhaps_ends_bb_p (p))
611         break;
612       else if (! INSN_P (p))
613         continue;
614
615       set = single_set (p);
616       if (set && SET_SRC (set) == dest && SET_DEST (set) == src
617           && find_reg_note (p, REG_DEAD, dest))
618         {
619           /* We can do the optimization.  Scan forward from INSN again,
620              replacing regs as we go.  */
621
622           /* Set to stop at next insn.  */
623           for (q = insn; q != NEXT_INSN (p); q = NEXT_INSN (q))
624             if (INSN_P (q))
625               {
626                 if (reg_mentioned_p (dest, PATTERN (q)))
627                   PATTERN (q) = replace_rtx (PATTERN (q), dest, src);
628
629
630               if (GET_CODE (q) == CALL_INSN)
631                 {
632                   REG_N_CALLS_CROSSED (dregno)--;
633                   REG_N_CALLS_CROSSED (sregno)++;
634                 }
635               }
636
637           remove_note (p, find_reg_note (p, REG_DEAD, dest));
638           REG_N_DEATHS (dregno)--;
639           remove_note (insn, find_reg_note (insn, REG_DEAD, src));
640           REG_N_DEATHS (sregno)--;
641           return;
642         }
643
644       if (reg_set_p (src, p)
645           || find_reg_note (p, REG_DEAD, dest)
646           || (GET_CODE (p) == CALL_INSN && REG_N_CALLS_CROSSED (sregno) == 0))
647         break;
648     }
649 }
650 /* INSN is a ZERO_EXTEND or SIGN_EXTEND of SRC to DEST.
651    Look if SRC dies there, and if it is only set once, by loading
652    it from memory.  If so, try to encorporate the zero/sign extension
653    into the memory read, change SRC to the mode of DEST, and alter
654    the remaining accesses to use the appropriate SUBREG.  This allows
655    SRC and DEST to be tied later.  */
656 static void
657 optimize_reg_copy_3 (insn, dest, src)
658      rtx insn;
659      rtx dest;
660      rtx src;
661 {
662   rtx src_reg = XEXP (src, 0);
663   int src_no = REGNO (src_reg);
664   int dst_no = REGNO (dest);
665   rtx p, set, subreg;
666   enum machine_mode old_mode;
667
668   if (src_no < FIRST_PSEUDO_REGISTER
669       || dst_no < FIRST_PSEUDO_REGISTER
670       || ! find_reg_note (insn, REG_DEAD, src_reg)
671       || REG_N_SETS (src_no) != 1)
672     return;
673   for (p = PREV_INSN (insn); p && ! reg_set_p (src_reg, p); p = PREV_INSN (p))
674     /* ??? We can't scan past the end of a basic block without updating
675        the register lifetime info (REG_DEAD/basic_block_live_at_start).  */
676     if (perhaps_ends_bb_p (p))
677       break;
678
679   if (! p)
680     return;
681
682   if (! (set = single_set (p))
683       || GET_CODE (SET_SRC (set)) != MEM
684       /* If there's a REG_EQUIV note, this must be an insn that loads an
685          argument.  Prefer keeping the note over doing this optimization.  */
686       || find_reg_note (p, REG_EQUIV, NULL_RTX)
687       || SET_DEST (set) != src_reg)
688     return;
689
690   /* Be conserative: although this optimization is also valid for
691      volatile memory references, that could cause trouble in later passes.  */
692   if (MEM_VOLATILE_P (SET_SRC (set)))
693     return;
694
695   /* Do not use a SUBREG to truncate from one mode to another if truncation
696      is not a nop.  */
697   if (GET_MODE_BITSIZE (GET_MODE (src_reg)) <= GET_MODE_BITSIZE (GET_MODE (src))
698       && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (GET_MODE (src)),
699                                  GET_MODE_BITSIZE (GET_MODE (src_reg))))
700     return;
701
702   old_mode = GET_MODE (src_reg);
703   PUT_MODE (src_reg, GET_MODE (src));
704   XEXP (src, 0) = SET_SRC (set);
705
706   /* Include this change in the group so that it's easily undone if
707      one of the changes in the group is invalid.  */
708   validate_change (p, &SET_SRC (set), src, 1);
709
710   /* Now walk forward making additional replacements.  We want to be able
711      to undo all the changes if a later substitution fails.  */
712   subreg = gen_lowpart_SUBREG (old_mode, src_reg);
713   while (p = NEXT_INSN (p), p != insn)
714     {
715       if (! INSN_P (p))
716         continue;
717
718       /* Make a tenative change.  */
719       validate_replace_rtx_group (src_reg, subreg, p);
720     }
721
722   validate_replace_rtx_group (src, src_reg, insn);
723
724   /* Now see if all the changes are valid.  */
725   if (! apply_change_group ())
726     {
727       /* One or more changes were no good.  Back out everything.  */
728       PUT_MODE (src_reg, old_mode);
729       XEXP (src, 0) = src_reg;
730     }
731   else
732     {
733       rtx note = find_reg_note (p, REG_EQUAL, NULL_RTX);
734       if (note)
735         remove_note (p, note);
736     }
737 }
738
739 \f
740 /* If we were not able to update the users of src to use dest directly, try
741    instead moving the value to dest directly before the operation.  */
742
743 static void
744 copy_src_to_dest (insn, src, dest, old_max_uid)
745      rtx insn;
746      rtx src;
747      rtx dest;
748      int old_max_uid;
749 {
750   rtx seq;
751   rtx link;
752   rtx next;
753   rtx set;
754   rtx move_insn;
755   rtx *p_insn_notes;
756   rtx *p_move_notes;
757   int src_regno;
758   int dest_regno;
759   int bb;
760   int insn_uid;
761   int move_uid;
762
763   /* A REG_LIVE_LENGTH of -1 indicates the register is equivalent to a constant
764      or memory location and is used infrequently; a REG_LIVE_LENGTH of -2 is
765      parameter when there is no frame pointer that is not allocated a register.
766      For now, we just reject them, rather than incrementing the live length.  */
767
768   if (GET_CODE (src) == REG
769       && REG_LIVE_LENGTH (REGNO (src)) > 0
770       && GET_CODE (dest) == REG
771       && !RTX_UNCHANGING_P (dest)
772       && REG_LIVE_LENGTH (REGNO (dest)) > 0
773       && (set = single_set (insn)) != NULL_RTX
774       && !reg_mentioned_p (dest, SET_SRC (set))
775       && GET_MODE (src) == GET_MODE (dest))
776     {
777       int old_num_regs = reg_rtx_no;
778
779       /* Generate the src->dest move.  */
780       start_sequence ();
781       emit_move_insn (dest, src);
782       seq = gen_sequence ();
783       end_sequence ();
784       /* If this sequence uses new registers, we may not use it.  */
785       if (old_num_regs != reg_rtx_no
786           || ! validate_replace_rtx (src, dest, insn))
787         {
788           /* We have to restore reg_rtx_no to its old value, lest
789              recompute_reg_usage will try to compute the usage of the
790              new regs, yet reg_n_info is not valid for them.  */
791           reg_rtx_no = old_num_regs;
792           return;
793         }
794       emit_insn_before (seq, insn);
795       move_insn = PREV_INSN (insn);
796       p_move_notes = &REG_NOTES (move_insn);
797       p_insn_notes = &REG_NOTES (insn);
798
799       /* Move any notes mentioning src to the move instruction */
800       for (link = REG_NOTES (insn); link != NULL_RTX; link = next)
801         {
802           next = XEXP (link, 1);
803           if (XEXP (link, 0) == src)
804             {
805               *p_move_notes = link;
806               p_move_notes = &XEXP (link, 1);
807             }
808           else
809             {
810               *p_insn_notes = link;
811               p_insn_notes = &XEXP (link, 1);
812             }
813         }
814
815       *p_move_notes = NULL_RTX;
816       *p_insn_notes = NULL_RTX;
817
818       /* Is the insn the head of a basic block?  If so extend it */
819       insn_uid = INSN_UID (insn);
820       move_uid = INSN_UID (move_insn);
821       if (insn_uid < old_max_uid)
822         {
823           bb = regmove_bb_head[insn_uid];
824           if (bb >= 0)
825             {
826               BLOCK_HEAD (bb) = move_insn;
827               regmove_bb_head[insn_uid] = -1;
828             }
829         }
830
831       /* Update the various register tables.  */
832       dest_regno = REGNO (dest);
833       REG_N_SETS (dest_regno) ++;
834       REG_LIVE_LENGTH (dest_regno)++;
835       if (REGNO_FIRST_UID (dest_regno) == insn_uid)
836         REGNO_FIRST_UID (dest_regno) = move_uid;
837
838       src_regno = REGNO (src);
839       if (! find_reg_note (move_insn, REG_DEAD, src))
840         REG_LIVE_LENGTH (src_regno)++;
841
842       if (REGNO_FIRST_UID (src_regno) == insn_uid)
843         REGNO_FIRST_UID (src_regno) = move_uid;
844
845       if (REGNO_LAST_UID (src_regno) == insn_uid)
846         REGNO_LAST_UID (src_regno) = move_uid;
847
848       if (REGNO_LAST_NOTE_UID (src_regno) == insn_uid)
849         REGNO_LAST_NOTE_UID (src_regno) = move_uid;
850     }
851 }
852
853 \f
854 /* Return whether REG is set in only one location, and is set to a
855    constant, but is set in a different basic block from INSN (an
856    instructions which uses REG).  In this case REG is equivalent to a
857    constant, and we don't want to break that equivalence, because that
858    may increase register pressure and make reload harder.  If REG is
859    set in the same basic block as INSN, we don't worry about it,
860    because we'll probably need a register anyhow (??? but what if REG
861    is used in a different basic block as well as this one?).  FIRST is
862    the first insn in the function.  */
863
864 static int
865 reg_is_remote_constant_p (reg, insn, first)
866      rtx reg;
867      rtx insn;
868      rtx first;
869 {
870   register rtx p;
871
872   if (REG_N_SETS (REGNO (reg)) != 1)
873     return 0;
874
875   /* Look for the set.  */
876   for (p = LOG_LINKS (insn); p; p = XEXP (p, 1))
877     {
878       rtx s;
879
880       if (REG_NOTE_KIND (p) != 0)
881         continue;
882       s = single_set (XEXP (p, 0));
883       if (s != 0
884           && GET_CODE (SET_DEST (s)) == REG
885           && REGNO (SET_DEST (s)) == REGNO (reg))
886         {
887           /* The register is set in the same basic block.  */
888           return 0;
889         }
890     }
891
892   for (p = first; p && p != insn; p = NEXT_INSN (p))
893     {
894       rtx s;
895
896       if (! INSN_P (p))
897         continue;
898       s = single_set (p);
899       if (s != 0
900           && GET_CODE (SET_DEST (s)) == REG
901           && REGNO (SET_DEST (s)) == REGNO (reg))
902         {
903           /* This is the instruction which sets REG.  If there is a
904              REG_EQUAL note, then REG is equivalent to a constant.  */
905           if (find_reg_note (p, REG_EQUAL, NULL_RTX))
906             return 1;
907           return 0;
908         }
909     }
910
911   return 0;
912 }
913
914 /* INSN is adding a CONST_INT to a REG.  We search backwards looking for
915    another add immediate instruction with the same source and dest registers,
916    and if we find one, we change INSN to an increment, and return 1.  If
917    no changes are made, we return 0.
918
919    This changes
920      (set (reg100) (plus reg1 offset1))
921      ...
922      (set (reg100) (plus reg1 offset2))
923    to
924      (set (reg100) (plus reg1 offset1))
925      ...
926      (set (reg100) (plus reg100 offset2-offset1))  */
927
928 /* ??? What does this comment mean?  */
929 /* cse disrupts preincrement / postdecrement squences when it finds a
930    hard register as ultimate source, like the frame pointer.  */
931
932 static int
933 fixup_match_2 (insn, dst, src, offset, regmove_dump_file)
934      rtx insn, dst, src, offset;
935      FILE *regmove_dump_file;
936 {
937   rtx p, dst_death = 0;
938   int length, num_calls = 0;
939
940   /* If SRC dies in INSN, we'd have to move the death note.  This is
941      considered to be very unlikely, so we just skip the optimization
942      in this case.  */
943   if (find_regno_note (insn, REG_DEAD, REGNO (src)))
944     return 0;
945
946   /* Scan backward to find the first instruction that sets DST.  */
947
948   for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
949     {
950       rtx pset;
951
952       /* ??? We can't scan past the end of a basic block without updating
953          the register lifetime info (REG_DEAD/basic_block_live_at_start).  */
954       if (perhaps_ends_bb_p (p))
955         break;
956       else if (! INSN_P (p))
957         continue;
958
959       if (find_regno_note (p, REG_DEAD, REGNO (dst)))
960         dst_death = p;
961       if (! dst_death)
962         length++;
963
964       pset = single_set (p);
965       if (pset && SET_DEST (pset) == dst
966           && GET_CODE (SET_SRC (pset)) == PLUS
967           && XEXP (SET_SRC (pset), 0) == src
968           && GET_CODE (XEXP (SET_SRC (pset), 1)) == CONST_INT)
969         {
970           HOST_WIDE_INT newconst
971             = INTVAL (offset) - INTVAL (XEXP (SET_SRC (pset), 1));
972           rtx add = gen_add3_insn (dst, dst, GEN_INT (newconst));
973
974           if (add && validate_change (insn, &PATTERN (insn), add, 0))
975             {
976               /* Remove the death note for DST from DST_DEATH.  */
977               if (dst_death)
978                 {
979                   remove_death (REGNO (dst), dst_death);
980                   REG_LIVE_LENGTH (REGNO (dst)) += length;
981                   REG_N_CALLS_CROSSED (REGNO (dst)) += num_calls;
982                 }
983
984               if (regmove_dump_file)
985                 fprintf (regmove_dump_file,
986                          "Fixed operand of insn %d.\n",
987                           INSN_UID (insn));
988
989 #ifdef AUTO_INC_DEC
990               for (p = PREV_INSN (insn); p; p = PREV_INSN (p))
991                 {
992                   if (GET_CODE (p) == CODE_LABEL
993                       || GET_CODE (p) == JUMP_INSN)
994                     break;
995                   if (! INSN_P (p))
996                     continue;
997                   if (reg_overlap_mentioned_p (dst, PATTERN (p)))
998                     {
999                       if (try_auto_increment (p, insn, 0, dst, newconst, 0))
1000                         return 1;
1001                       break;
1002                     }
1003                 }
1004               for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
1005                 {
1006                   if (GET_CODE (p) == CODE_LABEL
1007                       || GET_CODE (p) == JUMP_INSN)
1008                     break;
1009                   if (! INSN_P (p))
1010                     continue;
1011                   if (reg_overlap_mentioned_p (dst, PATTERN (p)))
1012                     {
1013                       try_auto_increment (p, insn, 0, dst, newconst, 1);
1014                       break;
1015                     }
1016                 }
1017 #endif
1018               return 1;
1019             }
1020         }
1021
1022       if (reg_set_p (dst, PATTERN (p)))
1023         break;
1024
1025       /* If we have passed a call instruction, and the
1026          pseudo-reg SRC is not already live across a call,
1027          then don't perform the optimization.  */
1028       /* reg_set_p is overly conservative for CALL_INSNS, thinks that all
1029          hard regs are clobbered.  Thus, we only use it for src for
1030          non-call insns.  */
1031       if (GET_CODE (p) == CALL_INSN)
1032         {
1033           if (! dst_death)
1034             num_calls++;
1035
1036           if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
1037             break;
1038
1039           if (call_used_regs [REGNO (dst)]
1040               || find_reg_fusage (p, CLOBBER, dst))
1041             break;
1042         }
1043       else if (reg_set_p (src, PATTERN (p)))
1044         break;
1045     }
1046
1047   return 0;
1048 }
1049
1050 /* Main entry for the register move optimization.
1051    F is the first instruction.
1052    NREGS is one plus the highest pseudo-reg number used in the instruction.
1053    REGMOVE_DUMP_FILE is a stream for output of a trace of actions taken
1054    (or 0 if none should be output).  */
1055
1056 void
1057 regmove_optimize (f, nregs, regmove_dump_file)
1058      rtx f;
1059      int nregs;
1060      FILE *regmove_dump_file;
1061 {
1062   int old_max_uid = get_max_uid ();
1063   rtx insn;
1064   struct match match;
1065   int pass;
1066   int i;
1067   rtx copy_src, copy_dst;
1068
1069   /* ??? Hack.  Regmove doesn't examine the CFG, and gets mightily
1070      confused by non-call exceptions ending blocks.  */
1071   if (flag_non_call_exceptions)
1072     return;
1073
1074   /* Find out where a potential flags register is live, and so that we
1075      can supress some optimizations in those zones.  */
1076   mark_flags_life_zones (discover_flags_reg ());
1077
1078   regno_src_regno = (int *) xmalloc (sizeof *regno_src_regno * nregs);
1079   for (i = nregs; --i >= 0; ) regno_src_regno[i] = -1;
1080
1081   regmove_bb_head = (int *) xmalloc (sizeof (int) * (old_max_uid + 1));
1082   for (i = old_max_uid; i >= 0; i--) regmove_bb_head[i] = -1;
1083   for (i = 0; i < n_basic_blocks; i++)
1084     regmove_bb_head[INSN_UID (BLOCK_HEAD (i))] = i;
1085
1086   /* A forward/backward pass.  Replace output operands with input operands.  */
1087
1088   for (pass = 0; pass <= 2; pass++)
1089     {
1090       if (! flag_regmove && pass >= flag_expensive_optimizations)
1091         goto done;
1092
1093       if (regmove_dump_file)
1094         fprintf (regmove_dump_file, "Starting %s pass...\n",
1095                  pass ? "backward" : "forward");
1096
1097       for (insn = pass ? get_last_insn () : f; insn;
1098            insn = pass ? PREV_INSN (insn) : NEXT_INSN (insn))
1099         {
1100           rtx set;
1101           int op_no, match_no;
1102
1103           set = single_set (insn);
1104           if (! set)
1105             continue;
1106
1107           if (flag_expensive_optimizations && ! pass
1108               && (GET_CODE (SET_SRC (set)) == SIGN_EXTEND
1109                   || GET_CODE (SET_SRC (set)) == ZERO_EXTEND)
1110               && GET_CODE (XEXP (SET_SRC (set), 0)) == REG
1111               && GET_CODE (SET_DEST(set)) == REG)
1112             optimize_reg_copy_3 (insn, SET_DEST (set), SET_SRC (set));
1113
1114           if (flag_expensive_optimizations && ! pass
1115               && GET_CODE (SET_SRC (set)) == REG
1116               && GET_CODE (SET_DEST(set)) == REG)
1117             {
1118               /* If this is a register-register copy where SRC is not dead,
1119                  see if we can optimize it.  If this optimization succeeds,
1120                  it will become a copy where SRC is dead.  */
1121               if ((find_reg_note (insn, REG_DEAD, SET_SRC (set))
1122                    || optimize_reg_copy_1 (insn, SET_DEST (set), SET_SRC (set)))
1123                   && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
1124                 {
1125                   /* Similarly for a pseudo-pseudo copy when SRC is dead.  */
1126                   if (REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1127                     optimize_reg_copy_2 (insn, SET_DEST (set), SET_SRC (set));
1128                   if (regno_src_regno[REGNO (SET_DEST (set))] < 0
1129                       && SET_SRC (set) != SET_DEST (set))
1130                     {
1131                       int srcregno = REGNO (SET_SRC(set));
1132                       if (regno_src_regno[srcregno] >= 0)
1133                         srcregno = regno_src_regno[srcregno];
1134                       regno_src_regno[REGNO (SET_DEST (set))] = srcregno;
1135                     }
1136                 }
1137             }
1138           if (! flag_regmove)
1139             continue;
1140
1141           if (! find_matches (insn, &match))
1142             continue;
1143
1144           /* Now scan through the operands looking for a source operand
1145              which is supposed to match the destination operand.
1146              Then scan forward for an instruction which uses the dest
1147              operand.
1148              If it dies there, then replace the dest in both operands with
1149              the source operand.  */
1150
1151           for (op_no = 0; op_no < recog_data.n_operands; op_no++)
1152             {
1153               rtx src, dst, src_subreg;
1154               enum reg_class src_class, dst_class;
1155
1156               match_no = match.with[op_no];
1157
1158               /* Nothing to do if the two operands aren't supposed to match.  */
1159               if (match_no < 0)
1160                 continue;
1161
1162               src = recog_data.operand[op_no];
1163               dst = recog_data.operand[match_no];
1164
1165               if (GET_CODE (src) != REG)
1166                 continue;
1167
1168               src_subreg = src;
1169               if (GET_CODE (dst) == SUBREG
1170                   && GET_MODE_SIZE (GET_MODE (dst))
1171                      >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (dst))))
1172                 {
1173                   src_subreg
1174                     = gen_rtx_SUBREG (GET_MODE (SUBREG_REG (dst)),
1175                                       src, SUBREG_BYTE (dst));
1176                   dst = SUBREG_REG (dst);
1177                 }
1178               if (GET_CODE (dst) != REG
1179                   || REGNO (dst) < FIRST_PSEUDO_REGISTER)
1180                 continue;
1181
1182               if (REGNO (src) < FIRST_PSEUDO_REGISTER)
1183                 {
1184                   if (match.commutative[op_no] < op_no)
1185                     regno_src_regno[REGNO (dst)] = REGNO (src);
1186                   continue;
1187                 }
1188
1189               if (REG_LIVE_LENGTH (REGNO (src)) < 0)
1190                 continue;
1191
1192               /* op_no/src must be a read-only operand, and
1193                  match_operand/dst must be a write-only operand.  */
1194               if (match.use[op_no] != READ
1195                   || match.use[match_no] != WRITE)
1196                 continue;
1197
1198               if (match.early_clobber[match_no]
1199                   && count_occurrences (PATTERN (insn), src, 0) > 1)
1200                 continue;
1201
1202               /* Make sure match_operand is the destination.  */
1203               if (recog_data.operand[match_no] != SET_DEST (set))
1204                 continue;
1205
1206               /* If the operands already match, then there is nothing to do.  */
1207               if (operands_match_p (src, dst))
1208                 continue;
1209
1210               /* But in the commutative case, we might find a better match.  */
1211               if (match.commutative[op_no] >= 0)
1212                 {
1213                   rtx comm = recog_data.operand[match.commutative[op_no]];
1214                   if (operands_match_p (comm, dst)
1215                       && (replacement_quality (comm)
1216                           >= replacement_quality (src)))
1217                     continue;
1218                 }
1219
1220               src_class = reg_preferred_class (REGNO (src));
1221               dst_class = reg_preferred_class (REGNO (dst));
1222               if (! regclass_compatible_p (src_class, dst_class))
1223                 continue;
1224
1225               if (GET_MODE (src) != GET_MODE (dst))
1226                 continue;
1227
1228               if (fixup_match_1 (insn, set, src, src_subreg, dst, pass,
1229                                  op_no, match_no,
1230                                  regmove_dump_file))
1231                 break;
1232             }
1233         }
1234     }
1235
1236   /* A backward pass.  Replace input operands with output operands.  */
1237
1238   if (regmove_dump_file)
1239     fprintf (regmove_dump_file, "Starting backward pass...\n");
1240
1241   for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1242     {
1243       if (INSN_P (insn))
1244         {
1245           int op_no, match_no;
1246           int success = 0;
1247
1248           if (! find_matches (insn, &match))
1249             continue;
1250
1251           /* Now scan through the operands looking for a destination operand
1252              which is supposed to match a source operand.
1253              Then scan backward for an instruction which sets the source
1254              operand.  If safe, then replace the source operand with the
1255              dest operand in both instructions.  */
1256
1257           copy_src = NULL_RTX;
1258           copy_dst = NULL_RTX;
1259           for (op_no = 0; op_no < recog_data.n_operands; op_no++)
1260             {
1261               rtx set, p, src, dst;
1262               rtx src_note, dst_note;
1263               int num_calls = 0;
1264               enum reg_class src_class, dst_class;
1265               int length;
1266
1267               match_no = match.with[op_no];
1268
1269               /* Nothing to do if the two operands aren't supposed to match.  */
1270               if (match_no < 0)
1271                 continue;
1272
1273               dst = recog_data.operand[match_no];
1274               src = recog_data.operand[op_no];
1275
1276               if (GET_CODE (src) != REG)
1277                 continue;
1278
1279               if (GET_CODE (dst) != REG
1280                   || REGNO (dst) < FIRST_PSEUDO_REGISTER
1281                   || REG_LIVE_LENGTH (REGNO (dst)) < 0
1282                   || RTX_UNCHANGING_P (dst))
1283                 continue;
1284
1285               /* If the operands already match, then there is nothing to do.  */
1286               if (operands_match_p (src, dst))
1287                 continue;
1288
1289               if (match.commutative[op_no] >= 0)
1290                 {
1291                   rtx comm = recog_data.operand[match.commutative[op_no]];
1292                   if (operands_match_p (comm, dst))
1293                     continue;
1294                 }
1295
1296               set = single_set (insn);
1297               if (! set)
1298                 continue;
1299
1300               /* Note that single_set ignores parts of a parallel set for
1301                  which one of the destinations is REG_UNUSED.  We can't
1302                  handle that here, since we can wind up rewriting things
1303                  such that a single register is set twice within a single
1304                  parallel.  */
1305               if (reg_set_p (src, insn))
1306                 continue;
1307
1308               /* match_no/dst must be a write-only operand, and
1309                  operand_operand/src must be a read-only operand.  */
1310               if (match.use[op_no] != READ
1311                   || match.use[match_no] != WRITE)
1312                 continue;
1313
1314               if (match.early_clobber[match_no]
1315                   && count_occurrences (PATTERN (insn), src, 0) > 1)
1316                 continue;
1317
1318               /* Make sure match_no is the destination.  */
1319               if (recog_data.operand[match_no] != SET_DEST (set))
1320                 continue;
1321
1322               if (REGNO (src) < FIRST_PSEUDO_REGISTER)
1323                 {
1324                   if (GET_CODE (SET_SRC (set)) == PLUS
1325                       && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT
1326                       && XEXP (SET_SRC (set), 0) == src
1327                       && fixup_match_2 (insn, dst, src,
1328                                         XEXP (SET_SRC (set), 1),
1329                                         regmove_dump_file))
1330                     break;
1331                   continue;
1332                 }
1333               src_class = reg_preferred_class (REGNO (src));
1334               dst_class = reg_preferred_class (REGNO (dst));
1335               if (! regclass_compatible_p (src_class, dst_class))
1336                 {
1337                   if (!copy_src)
1338                     {
1339                       copy_src = src;
1340                       copy_dst = dst;
1341                     }
1342                   continue;
1343                 }
1344
1345               /* Can not modify an earlier insn to set dst if this insn
1346                  uses an old value in the source.  */
1347               if (reg_overlap_mentioned_p (dst, SET_SRC (set)))
1348                 {
1349                   if (!copy_src)
1350                     {
1351                       copy_src = src;
1352                       copy_dst = dst;
1353                     }
1354                   continue;
1355                 }
1356
1357               if (! (src_note = find_reg_note (insn, REG_DEAD, src)))
1358                 {
1359                   if (!copy_src)
1360                     {
1361                       copy_src = src;
1362                       copy_dst = dst;
1363                     }
1364                   continue;
1365                 }
1366
1367
1368               /* If src is set once in a different basic block,
1369                  and is set equal to a constant, then do not use
1370                  it for this optimization, as this would make it
1371                  no longer equivalent to a constant.  */
1372
1373               if (reg_is_remote_constant_p (src, insn, f))
1374                 {
1375                   if (!copy_src)
1376                     {
1377                       copy_src = src;
1378                       copy_dst = dst;
1379                     }
1380                   continue;
1381                 }
1382
1383
1384               if (regmove_dump_file)
1385                 fprintf (regmove_dump_file,
1386                          "Could fix operand %d of insn %d matching operand %d.\n",
1387                          op_no, INSN_UID (insn), match_no);
1388
1389               /* Scan backward to find the first instruction that uses
1390                  the input operand.  If the operand is set here, then
1391                  replace it in both instructions with match_no.  */
1392
1393               for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
1394                 {
1395                   rtx pset;
1396
1397                   /* ??? We can't scan past the end of a basic block without
1398                      updating the register lifetime info
1399                      (REG_DEAD/basic_block_live_at_start).  */
1400                   if (perhaps_ends_bb_p (p))
1401                     break;
1402                   else if (! INSN_P (p))
1403                     continue;
1404
1405                   length++;
1406
1407                   /* ??? See if all of SRC is set in P.  This test is much
1408                      more conservative than it needs to be.  */
1409                   pset = single_set (p);
1410                   if (pset && SET_DEST (pset) == src)
1411                     {
1412                       /* We use validate_replace_rtx, in case there
1413                          are multiple identical source operands.  All of
1414                          them have to be changed at the same time.  */
1415                       if (validate_replace_rtx (src, dst, insn))
1416                         {
1417                           if (validate_change (p, &SET_DEST (pset),
1418                                                dst, 0))
1419                             success = 1;
1420                           else
1421                             {
1422                               /* Change all source operands back.
1423                                  This modifies the dst as a side-effect.  */
1424                               validate_replace_rtx (dst, src, insn);
1425                               /* Now make sure the dst is right.  */
1426                               validate_change (insn,
1427                                                recog_data.operand_loc[match_no],
1428                                                dst, 0);
1429                             }
1430                         }
1431                       break;
1432                     }
1433
1434                   if (reg_overlap_mentioned_p (src, PATTERN (p))
1435                       || reg_overlap_mentioned_p (dst, PATTERN (p)))
1436                     break;
1437
1438                   /* If we have passed a call instruction, and the
1439                      pseudo-reg DST is not already live across a call,
1440                      then don't perform the optimization.  */
1441                   if (GET_CODE (p) == CALL_INSN)
1442                     {
1443                       num_calls++;
1444
1445                       if (REG_N_CALLS_CROSSED (REGNO (dst)) == 0)
1446                         break;
1447                     }
1448                 }
1449
1450               if (success)
1451                 {
1452                   int dstno, srcno;
1453
1454                   /* Remove the death note for SRC from INSN.  */
1455                   remove_note (insn, src_note);
1456                   /* Move the death note for SRC to P if it is used
1457                      there.  */
1458                   if (reg_overlap_mentioned_p (src, PATTERN (p)))
1459                     {
1460                       XEXP (src_note, 1) = REG_NOTES (p);
1461                       REG_NOTES (p) = src_note;
1462                     }
1463                   /* If there is a REG_DEAD note for DST on P, then remove
1464                      it, because DST is now set there.  */
1465                   if ((dst_note = find_reg_note (p, REG_DEAD, dst)))
1466                     remove_note (p, dst_note);
1467
1468                   dstno = REGNO (dst);
1469                   srcno = REGNO (src);
1470
1471                   REG_N_SETS (dstno)++;
1472                   REG_N_SETS (srcno)--;
1473
1474                   REG_N_CALLS_CROSSED (dstno) += num_calls;
1475                   REG_N_CALLS_CROSSED (srcno) -= num_calls;
1476
1477                   REG_LIVE_LENGTH (dstno) += length;
1478                   if (REG_LIVE_LENGTH (srcno) >= 0)
1479                     {
1480                       REG_LIVE_LENGTH (srcno) -= length;
1481                       /* REG_LIVE_LENGTH is only an approximation after
1482                          combine if sched is not run, so make sure that we
1483                          still have a reasonable value.  */
1484                       if (REG_LIVE_LENGTH (srcno) < 2)
1485                         REG_LIVE_LENGTH (srcno) = 2;
1486                     }
1487
1488                   if (regmove_dump_file)
1489                     fprintf (regmove_dump_file,
1490                              "Fixed operand %d of insn %d matching operand %d.\n",
1491                              op_no, INSN_UID (insn), match_no);
1492
1493                   break;
1494                 }
1495             }
1496
1497           /* If we weren't able to replace any of the alternatives, try an
1498              alternative appoach of copying the source to the destination.  */
1499           if (!success && copy_src != NULL_RTX)
1500             copy_src_to_dest (insn, copy_src, copy_dst, old_max_uid);
1501
1502         }
1503     }
1504
1505   /* In fixup_match_1, some insns may have been inserted after basic block
1506      ends.  Fix that here.  */
1507   for (i = 0; i < n_basic_blocks; i++)
1508     {
1509       rtx end = BLOCK_END (i);
1510       rtx new = end;
1511       rtx next = NEXT_INSN (new);
1512       while (next != 0 && INSN_UID (next) >= old_max_uid
1513              && (i == n_basic_blocks - 1 || BLOCK_HEAD (i + 1) != next))
1514         new = next, next = NEXT_INSN (new);
1515       BLOCK_END (i) = new;
1516     }
1517
1518  done:
1519   /* Clean up.  */
1520   free (regno_src_regno);
1521   free (regmove_bb_head);
1522 }
1523
1524 /* Returns nonzero if INSN's pattern has matching constraints for any operand.
1525    Returns 0 if INSN can't be recognized, or if the alternative can't be
1526    determined.
1527
1528    Initialize the info in MATCHP based on the constraints.  */
1529
1530 static int
1531 find_matches (insn, matchp)
1532      rtx insn;
1533      struct match *matchp;
1534 {
1535   int likely_spilled[MAX_RECOG_OPERANDS];
1536   int op_no;
1537   int any_matches = 0;
1538
1539   extract_insn (insn);
1540   if (! constrain_operands (0))
1541     return 0;
1542
1543   /* Must initialize this before main loop, because the code for
1544      the commutative case may set matches for operands other than
1545      the current one.  */
1546   for (op_no = recog_data.n_operands; --op_no >= 0; )
1547     matchp->with[op_no] = matchp->commutative[op_no] = -1;
1548
1549   for (op_no = 0; op_no < recog_data.n_operands; op_no++)
1550     {
1551       const char *p;
1552       char c;
1553       int i = 0;
1554
1555       p = recog_data.constraints[op_no];
1556
1557       likely_spilled[op_no] = 0;
1558       matchp->use[op_no] = READ;
1559       matchp->early_clobber[op_no] = 0;
1560       if (*p == '=')
1561         matchp->use[op_no] = WRITE;
1562       else if (*p == '+')
1563         matchp->use[op_no] = READWRITE;
1564
1565       for (;*p && i < which_alternative; p++)
1566         if (*p == ',')
1567           i++;
1568
1569       while ((c = *p++) != '\0' && c != ',')
1570         switch (c)
1571           {
1572           case '=':
1573             break;
1574           case '+':
1575             break;
1576           case '&':
1577             matchp->early_clobber[op_no] = 1;
1578             break;
1579           case '%':
1580             matchp->commutative[op_no] = op_no + 1;
1581             matchp->commutative[op_no + 1] = op_no;
1582             break;
1583           case '0': case '1': case '2': case '3': case '4':
1584           case '5': case '6': case '7': case '8': case '9':
1585             c -= '0';
1586             if (c < op_no && likely_spilled[(unsigned char) c])
1587               break;
1588             matchp->with[op_no] = c;
1589             any_matches = 1;
1590             if (matchp->commutative[op_no] >= 0)
1591               matchp->with[matchp->commutative[op_no]] = c;
1592             break;
1593           case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'h':
1594           case 'j': case 'k': case 'l': case 'p': case 'q': case 't': case 'u':
1595           case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B':
1596           case 'C': case 'D': case 'W': case 'Y': case 'Z':
1597             if (CLASS_LIKELY_SPILLED_P (REG_CLASS_FROM_LETTER ((unsigned char)c)))
1598               likely_spilled[op_no] = 1;
1599             break;
1600           }
1601     }
1602   return any_matches;
1603 }
1604
1605 /* Try to replace all occurrences of DST_REG with SRC in LOC, that is
1606    assumed to be in INSN.  */
1607
1608 static void
1609 replace_in_call_usage (loc, dst_reg, src, insn)
1610      rtx *loc;
1611      unsigned int dst_reg;
1612      rtx src;
1613      rtx insn;
1614 {
1615   rtx x = *loc;
1616   enum rtx_code code;
1617   const char *fmt;
1618   int i, j;
1619
1620   if (! x)
1621     return;
1622
1623   code = GET_CODE (x);
1624   if (code == REG)
1625     {
1626       if (REGNO (x) != dst_reg)
1627         return;
1628
1629       validate_change (insn, loc, src, 1);
1630
1631       return;
1632     }
1633
1634   /* Process each of our operands recursively.  */
1635   fmt = GET_RTX_FORMAT (code);
1636   for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
1637     if (*fmt == 'e')
1638       replace_in_call_usage (&XEXP (x, i), dst_reg, src, insn);
1639     else if (*fmt == 'E')
1640       for (j = 0; j < XVECLEN (x, i); j++)
1641         replace_in_call_usage (& XVECEXP (x, i, j), dst_reg, src, insn);
1642 }
1643
1644 /* Try to replace output operand DST in SET, with input operand SRC.  SET is
1645    the only set in INSN.  INSN has just been recognized and constrained.
1646    SRC is operand number OPERAND_NUMBER in INSN.
1647    DST is operand number MATCH_NUMBER in INSN.
1648    If BACKWARD is nonzero, we have been called in a backward pass.
1649    Return nonzero for success.  */
1650
1651 static int
1652 fixup_match_1 (insn, set, src, src_subreg, dst, backward, operand_number,
1653                match_number, regmove_dump_file)
1654      rtx insn, set, src, src_subreg, dst;
1655      int backward, operand_number, match_number;
1656      FILE *regmove_dump_file;
1657 {
1658   rtx p;
1659   rtx post_inc = 0, post_inc_set = 0, search_end = 0;
1660   int success = 0;
1661   int num_calls = 0, s_num_calls = 0;
1662   enum rtx_code code = NOTE;
1663   HOST_WIDE_INT insn_const = 0, newconst;
1664   rtx overlap = 0; /* need to move insn ? */
1665   rtx src_note = find_reg_note (insn, REG_DEAD, src), dst_note = NULL_RTX;
1666   int length, s_length;
1667
1668   /* If SRC is marked as unchanging, we may not change it.
1669      ??? Maybe we could get better code by removing the unchanging bit
1670      instead, and changing it back if we don't succeed?  */
1671   if (RTX_UNCHANGING_P (src))
1672     return 0;
1673
1674   if (! src_note)
1675     {
1676       /* Look for (set (regX) (op regA constX))
1677                   (set (regY) (op regA constY))
1678          and change that to
1679                   (set (regA) (op regA constX)).
1680                   (set (regY) (op regA constY-constX)).
1681          This works for add and shift operations, if
1682          regA is dead after or set by the second insn.  */
1683
1684       code = GET_CODE (SET_SRC (set));
1685       if ((code == PLUS || code == LSHIFTRT
1686            || code == ASHIFT || code == ASHIFTRT)
1687           && XEXP (SET_SRC (set), 0) == src
1688           && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
1689         insn_const = INTVAL (XEXP (SET_SRC (set), 1));
1690       else if (! stable_and_no_regs_but_for_p (SET_SRC (set), src, dst))
1691         return 0;
1692       else
1693         /* We might find a src_note while scanning.  */
1694         code = NOTE;
1695     }
1696
1697   if (regmove_dump_file)
1698     fprintf (regmove_dump_file,
1699              "Could fix operand %d of insn %d matching operand %d.\n",
1700              operand_number, INSN_UID (insn), match_number);
1701
1702   /* If SRC is equivalent to a constant set in a different basic block,
1703      then do not use it for this optimization.  We want the equivalence
1704      so that if we have to reload this register, we can reload the
1705      constant, rather than extending the lifespan of the register.  */
1706   if (reg_is_remote_constant_p (src, insn, get_insns ()))
1707     return 0;
1708
1709   /* Scan forward to find the next instruction that
1710      uses the output operand.  If the operand dies here,
1711      then replace it in both instructions with
1712      operand_number.  */
1713
1714   for (length = s_length = 0, p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
1715     {
1716       if (GET_CODE (p) == CALL_INSN)
1717         replace_in_call_usage (& CALL_INSN_FUNCTION_USAGE (p),
1718                                REGNO (dst), src, p);
1719
1720       /* ??? We can't scan past the end of a basic block without updating
1721          the register lifetime info (REG_DEAD/basic_block_live_at_start).  */
1722       if (perhaps_ends_bb_p (p))
1723         break;
1724       else if (! INSN_P (p))
1725         continue;
1726
1727       length++;
1728       if (src_note)
1729         s_length++;
1730
1731       if (reg_set_p (src, p) || reg_set_p (dst, p)
1732           || (GET_CODE (PATTERN (p)) == USE
1733               && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
1734         break;
1735
1736       /* See if all of DST dies in P.  This test is
1737          slightly more conservative than it needs to be.  */
1738       if ((dst_note = find_regno_note (p, REG_DEAD, REGNO (dst)))
1739           && (GET_MODE (XEXP (dst_note, 0)) == GET_MODE (dst)))
1740         {
1741           /* If we would be moving INSN, check that we won't move it
1742              into the shadow of a live a live flags register.  */
1743           /* ??? We only try to move it in front of P, although
1744                  we could move it anywhere between OVERLAP and P.  */
1745           if (overlap && GET_MODE (PREV_INSN (p)) != VOIDmode)
1746             break;
1747
1748           if (! src_note)
1749             {
1750               rtx q;
1751               rtx set2 = NULL_RTX;
1752
1753               /* If an optimization is done, the value of SRC while P
1754                  is executed will be changed.  Check that this is OK.  */
1755               if (reg_overlap_mentioned_p (src, PATTERN (p)))
1756                 break;
1757               for (q = p; q; q = NEXT_INSN (q))
1758                 {
1759                   /* ??? We can't scan past the end of a basic block without
1760                      updating the register lifetime info
1761                      (REG_DEAD/basic_block_live_at_start).  */
1762                   if (perhaps_ends_bb_p (q))
1763                     {
1764                       q = 0;
1765                       break;
1766                     }
1767                   else if (! INSN_P (q))
1768                     continue;
1769                   else if (reg_overlap_mentioned_p (src, PATTERN (q))
1770                            || reg_set_p (src, q))
1771                     break;
1772                 }
1773               if (q)
1774                 set2 = single_set (q);
1775               if (! q || ! set2 || GET_CODE (SET_SRC (set2)) != code
1776                   || XEXP (SET_SRC (set2), 0) != src
1777                   || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT
1778                   || (SET_DEST (set2) != src
1779                       && ! find_reg_note (q, REG_DEAD, src)))
1780                 {
1781                   /* If this is a PLUS, we can still save a register by doing
1782                      src += insn_const;
1783                      P;
1784                      src -= insn_const; .
1785                      This also gives opportunities for subsequent
1786                      optimizations in the backward pass, so do it there.  */
1787                   if (code == PLUS && backward
1788                       /* Don't do this if we can likely tie DST to SET_DEST
1789                          of P later; we can't do this tying here if we got a
1790                          hard register.  */
1791                       && ! (dst_note && ! REG_N_CALLS_CROSSED (REGNO (dst))
1792                             && single_set (p)
1793                             && GET_CODE (SET_DEST (single_set (p))) == REG
1794                             && (REGNO (SET_DEST (single_set (p)))
1795                                 < FIRST_PSEUDO_REGISTER))
1796                       /* We may only emit an insn directly after P if we
1797                          are not in the shadow of a live flags register.  */
1798                       && GET_MODE (p) == VOIDmode)
1799                     {
1800                       search_end = q;
1801                       q = insn;
1802                       set2 = set;
1803                       newconst = -insn_const;
1804                       code = MINUS;
1805                     }
1806                   else
1807                     break;
1808                 }
1809               else
1810                 {
1811                   newconst = INTVAL (XEXP (SET_SRC (set2), 1)) - insn_const;
1812                   /* Reject out of range shifts.  */
1813                   if (code != PLUS
1814                       && (newconst < 0
1815                           || ((unsigned HOST_WIDE_INT) newconst
1816                               >= (GET_MODE_BITSIZE (GET_MODE
1817                                                     (SET_SRC (set2)))))))
1818                     break;
1819                   if (code == PLUS)
1820                     {
1821                       post_inc = q;
1822                       if (SET_DEST (set2) != src)
1823                         post_inc_set = set2;
1824                     }
1825                 }
1826               /* We use 1 as last argument to validate_change so that all
1827                  changes are accepted or rejected together by apply_change_group
1828                  when it is called by validate_replace_rtx .  */
1829               validate_change (q, &XEXP (SET_SRC (set2), 1),
1830                                GEN_INT (newconst), 1);
1831             }
1832           validate_change (insn, recog_data.operand_loc[match_number], src, 1);
1833           if (validate_replace_rtx (dst, src_subreg, p))
1834             success = 1;
1835           break;
1836         }
1837
1838       if (reg_overlap_mentioned_p (dst, PATTERN (p)))
1839         break;
1840       if (! src_note && reg_overlap_mentioned_p (src, PATTERN (p)))
1841         {
1842           /* INSN was already checked to be movable wrt. the registers that it
1843              sets / uses when we found no REG_DEAD note for src on it, but it
1844              still might clobber the flags register.  We'll have to check that
1845              we won't insert it into the shadow of a live flags register when
1846              we finally know where we are to move it.  */
1847           overlap = p;
1848           src_note = find_reg_note (p, REG_DEAD, src);
1849         }
1850
1851       /* If we have passed a call instruction, and the pseudo-reg SRC is not
1852          already live across a call, then don't perform the optimization.  */
1853       if (GET_CODE (p) == CALL_INSN)
1854         {
1855           if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
1856             break;
1857
1858           num_calls++;
1859
1860           if (src_note)
1861             s_num_calls++;
1862
1863         }
1864     }
1865
1866   if (! success)
1867     return 0;
1868
1869   /* Remove the death note for DST from P.  */
1870   remove_note (p, dst_note);
1871   if (code == MINUS)
1872     {
1873       post_inc = emit_insn_after (copy_rtx (PATTERN (insn)), p);
1874       if ((HAVE_PRE_INCREMENT || HAVE_PRE_DECREMENT)
1875           && search_end
1876           && try_auto_increment (search_end, post_inc, 0, src, newconst, 1))
1877         post_inc = 0;
1878       validate_change (insn, &XEXP (SET_SRC (set), 1), GEN_INT (insn_const), 0);
1879       REG_N_SETS (REGNO (src))++;
1880       REG_LIVE_LENGTH (REGNO (src))++;
1881     }
1882   if (overlap)
1883     {
1884       /* The lifetime of src and dest overlap,
1885          but we can change this by moving insn.  */
1886       rtx pat = PATTERN (insn);
1887       if (src_note)
1888         remove_note (overlap, src_note);
1889       if ((HAVE_POST_INCREMENT || HAVE_POST_DECREMENT)
1890           && code == PLUS
1891           && try_auto_increment (overlap, insn, 0, src, insn_const, 0))
1892         insn = overlap;
1893       else
1894         {
1895           rtx notes = REG_NOTES (insn);
1896
1897           emit_insn_after_with_line_notes (pat, PREV_INSN (p), insn);
1898           PUT_CODE (insn, NOTE);
1899           NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1900           NOTE_SOURCE_FILE (insn) = 0;
1901           /* emit_insn_after_with_line_notes has no
1902              return value, so search for the new insn.  */
1903           insn = p;
1904           while (! INSN_P (insn) || PATTERN (insn) != pat)
1905             insn = PREV_INSN (insn);
1906
1907           REG_NOTES (insn) = notes;
1908         }
1909     }
1910   /* Sometimes we'd generate src = const; src += n;
1911      if so, replace the instruction that set src
1912      in the first place.  */
1913
1914   if (! overlap && (code == PLUS || code == MINUS))
1915     {
1916       rtx note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
1917       rtx q, set2 = NULL_RTX;
1918       int num_calls2 = 0, s_length2 = 0;
1919
1920       if (note && CONSTANT_P (XEXP (note, 0)))
1921         {
1922           for (q = PREV_INSN (insn); q; q = PREV_INSN(q))
1923             {
1924               /* ??? We can't scan past the end of a basic block without
1925                  updating the register lifetime info
1926                  (REG_DEAD/basic_block_live_at_start).  */
1927               if (perhaps_ends_bb_p (q))
1928                 {
1929                   q = 0;
1930                   break;
1931                 }
1932               else if (! INSN_P (q))
1933                 continue;
1934
1935               s_length2++;
1936               if (reg_set_p (src, q))
1937                 {
1938                   set2 = single_set (q);
1939                   break;
1940                 }
1941               if (reg_overlap_mentioned_p (src, PATTERN (q)))
1942                 {
1943                   q = 0;
1944                   break;
1945                 }
1946               if (GET_CODE (p) == CALL_INSN)
1947                 num_calls2++;
1948             }
1949           if (q && set2 && SET_DEST (set2) == src && CONSTANT_P (SET_SRC (set2))
1950               && validate_change (insn, &SET_SRC (set), XEXP (note, 0), 0))
1951             {
1952               PUT_CODE (q, NOTE);
1953               NOTE_LINE_NUMBER (q) = NOTE_INSN_DELETED;
1954               NOTE_SOURCE_FILE (q) = 0;
1955               REG_N_SETS (REGNO (src))--;
1956               REG_N_CALLS_CROSSED (REGNO (src)) -= num_calls2;
1957               REG_LIVE_LENGTH (REGNO (src)) -= s_length2;
1958               insn_const = 0;
1959             }
1960         }
1961     }
1962
1963   if ((HAVE_PRE_INCREMENT || HAVE_PRE_DECREMENT)
1964            && (code == PLUS || code == MINUS) && insn_const
1965            && try_auto_increment (p, insn, 0, src, insn_const, 1))
1966     insn = p;
1967   else if ((HAVE_POST_INCREMENT || HAVE_POST_DECREMENT)
1968            && post_inc
1969            && try_auto_increment (p, post_inc, post_inc_set, src, newconst, 0))
1970     post_inc = 0;
1971   /* If post_inc still prevails, try to find an
1972      insn where it can be used as a pre-in/decrement.
1973      If code is MINUS, this was already tried.  */
1974   if (post_inc && code == PLUS
1975   /* Check that newconst is likely to be usable
1976      in a pre-in/decrement before starting the search.  */
1977       && ((HAVE_PRE_INCREMENT && newconst > 0 && newconst <= MOVE_MAX)
1978           || (HAVE_PRE_DECREMENT && newconst < 0 && newconst >= -MOVE_MAX))
1979       && exact_log2 (newconst))
1980     {
1981       rtx q, inc_dest;
1982
1983       inc_dest = post_inc_set ? SET_DEST (post_inc_set) : src;
1984       for (q = post_inc; (q = NEXT_INSN (q)); )
1985         {
1986           /* ??? We can't scan past the end of a basic block without updating
1987              the register lifetime info
1988              (REG_DEAD/basic_block_live_at_start).  */
1989           if (perhaps_ends_bb_p (q))
1990             break;
1991           else if (! INSN_P (q))
1992             continue;
1993           else if (src != inc_dest
1994                    && (reg_overlap_mentioned_p (src, PATTERN (q))
1995                        || reg_set_p (src, q)))
1996             break;
1997           else if (reg_set_p (inc_dest, q))
1998             break;
1999           else if (reg_overlap_mentioned_p (inc_dest, PATTERN (q)))
2000             {
2001               try_auto_increment (q, post_inc,
2002                                   post_inc_set, inc_dest, newconst, 1);
2003               break;
2004             }
2005         }
2006     }
2007
2008   /* Move the death note for DST to INSN if it is used
2009      there.  */
2010   if (reg_overlap_mentioned_p (dst, PATTERN (insn)))
2011     {
2012       XEXP (dst_note, 1) = REG_NOTES (insn);
2013       REG_NOTES (insn) = dst_note;
2014     }
2015
2016   if (src_note)
2017     {
2018       /* Move the death note for SRC from INSN to P.  */
2019       if (! overlap)
2020         remove_note (insn, src_note);
2021       XEXP (src_note, 1) = REG_NOTES (p);
2022       REG_NOTES (p) = src_note;
2023
2024       REG_N_CALLS_CROSSED (REGNO (src)) += s_num_calls;
2025     }
2026
2027   REG_N_SETS (REGNO (src))++;
2028   REG_N_SETS (REGNO (dst))--;
2029
2030   REG_N_CALLS_CROSSED (REGNO (dst)) -= num_calls;
2031
2032   REG_LIVE_LENGTH (REGNO (src)) += s_length;
2033   if (REG_LIVE_LENGTH (REGNO (dst)) >= 0)
2034     {
2035       REG_LIVE_LENGTH (REGNO (dst)) -= length;
2036       /* REG_LIVE_LENGTH is only an approximation after
2037          combine if sched is not run, so make sure that we
2038          still have a reasonable value.  */
2039       if (REG_LIVE_LENGTH (REGNO (dst)) < 2)
2040         REG_LIVE_LENGTH (REGNO (dst)) = 2;
2041     }
2042   if (regmove_dump_file)
2043     fprintf (regmove_dump_file,
2044              "Fixed operand %d of insn %d matching operand %d.\n",
2045              operand_number, INSN_UID (insn), match_number);
2046   return 1;
2047 }
2048
2049
2050 /* return nonzero if X is stable and mentions no regsiters but for
2051    mentioning SRC or mentioning / changing DST .  If in doubt, presume
2052    it is unstable.
2053    The rationale is that we want to check if we can move an insn easily
2054    while just paying attention to SRC and DST.  A register is considered
2055    stable if it has the RTX_UNCHANGING_P bit set, but that would still
2056    leave the burden to update REG_DEAD / REG_UNUSED notes, so we don't
2057    want any registers but SRC and DST.  */
2058 static int
2059 stable_and_no_regs_but_for_p (x, src, dst)
2060      rtx x, src, dst;
2061 {
2062   RTX_CODE code = GET_CODE (x);
2063   switch (GET_RTX_CLASS (code))
2064     {
2065     case '<': case '1': case 'c': case '2': case 'b': case '3':
2066       {
2067         int i;
2068         const char *fmt = GET_RTX_FORMAT (code);
2069         for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2070           if (fmt[i] == 'e'
2071               && ! stable_and_no_regs_but_for_p (XEXP (x, i), src, dst))
2072               return 0;
2073         return 1;
2074       }
2075     case 'o':
2076       if (code == REG)
2077         return x == src || x == dst;
2078       /* If this is a MEM, look inside - there might be a register hidden in
2079          the address of an unchanging MEM.  */
2080       if (code == MEM
2081           && ! stable_and_no_regs_but_for_p (XEXP (x, 0), src, dst))
2082         return 0;
2083       /* fall through */
2084     default:
2085       return ! rtx_unstable_p (x);
2086     }
2087 }
2088 \f
2089 /* Track stack adjustments and stack memory references.  Attempt to
2090    reduce the number of stack adjustments by back-propogating across
2091    the memory references.
2092
2093    This is intended primarily for use with targets that do not define
2094    ACCUMULATE_OUTGOING_ARGS.  It is of significantly more value to
2095    targets that define PREFERRED_STACK_BOUNDARY more aligned than
2096    STACK_BOUNDARY (e.g. x86), or if not all registers can be pushed
2097    (e.g. x86 fp regs) which would ordinarily have to be implemented
2098    as a sub/mov pair due to restrictions in calls.c.
2099
2100    Propogation stops when any of the insns that need adjusting are
2101    (a) no longer valid because we've exceeded their range, (b) a
2102    non-trivial push instruction, or (c) a call instruction.
2103
2104    Restriction B is based on the assumption that push instructions
2105    are smaller or faster.  If a port really wants to remove all
2106    pushes, it should have defined ACCUMULATE_OUTGOING_ARGS.  The
2107    one exception that is made is for an add immediately followed
2108    by a push.  */
2109
2110 /* This structure records stack memory references between stack adjusting
2111    instructions.  */
2112
2113 struct csa_memlist
2114 {
2115   HOST_WIDE_INT sp_offset;
2116   rtx insn, *mem;
2117   struct csa_memlist *next;
2118 };
2119
2120 static int stack_memref_p               PARAMS ((rtx));
2121 static rtx single_set_for_csa           PARAMS ((rtx));
2122 static void free_csa_memlist            PARAMS ((struct csa_memlist *));
2123 static struct csa_memlist *record_one_stack_memref
2124   PARAMS ((rtx, rtx *, struct csa_memlist *));
2125 static int try_apply_stack_adjustment
2126   PARAMS ((rtx, struct csa_memlist *, HOST_WIDE_INT, HOST_WIDE_INT));
2127 static void combine_stack_adjustments_for_block PARAMS ((basic_block));
2128 static int record_stack_memrefs PARAMS ((rtx *, void *));
2129
2130
2131 /* Main entry point for stack adjustment combination.  */
2132
2133 void
2134 combine_stack_adjustments ()
2135 {
2136   int i;
2137
2138   for (i = 0; i < n_basic_blocks; ++i)
2139     combine_stack_adjustments_for_block (BASIC_BLOCK (i));
2140 }
2141
2142 /* Recognize a MEM of the form (sp) or (plus sp const).  */
2143
2144 static int
2145 stack_memref_p (x)
2146      rtx x;
2147 {
2148   if (GET_CODE (x) != MEM)
2149     return 0;
2150   x = XEXP (x, 0);
2151
2152   if (x == stack_pointer_rtx)
2153     return 1;
2154   if (GET_CODE (x) == PLUS
2155       && XEXP (x, 0) == stack_pointer_rtx
2156       && GET_CODE (XEXP (x, 1)) == CONST_INT)
2157     return 1;
2158
2159   return 0;
2160 }
2161
2162 /* Recognize either normal single_set or the hack in i386.md for
2163    tying fp and sp adjustments.  */
2164
2165 static rtx
2166 single_set_for_csa (insn)
2167      rtx insn;
2168 {
2169   int i;
2170   rtx tmp = single_set (insn);
2171   if (tmp)
2172     return tmp;
2173
2174   if (GET_CODE (insn) != INSN
2175       || GET_CODE (PATTERN (insn)) != PARALLEL)
2176     return NULL_RTX;
2177
2178   tmp = PATTERN (insn);
2179   if (GET_CODE (XVECEXP (tmp, 0, 0)) != SET)
2180     return NULL_RTX;
2181
2182   for (i = 1; i < XVECLEN (tmp, 0); ++i)
2183     {
2184       rtx this = XVECEXP (tmp, 0, i);
2185
2186       /* The special case is allowing a no-op set.  */
2187       if (GET_CODE (this) == SET
2188           && SET_SRC (this) == SET_DEST (this))
2189         ;
2190       else if (GET_CODE (this) != CLOBBER
2191                && GET_CODE (this) != USE)
2192         return NULL_RTX;
2193     }
2194
2195   return XVECEXP (tmp, 0, 0);
2196 }
2197
2198 /* Free the list of csa_memlist nodes.  */
2199
2200 static void
2201 free_csa_memlist (memlist)
2202      struct csa_memlist *memlist;
2203 {
2204   struct csa_memlist *next;
2205   for (; memlist ; memlist = next)
2206     {
2207       next = memlist->next;
2208       free (memlist);
2209     }
2210 }
2211
2212 /* Create a new csa_memlist node from the given memory reference.
2213    It is already known that the memory is stack_memref_p.  */
2214
2215 static struct csa_memlist *
2216 record_one_stack_memref (insn, mem, next_memlist)
2217      rtx insn, *mem;
2218      struct csa_memlist *next_memlist;
2219 {
2220   struct csa_memlist *ml;
2221
2222   ml = (struct csa_memlist *) xmalloc (sizeof (*ml));
2223
2224   if (XEXP (*mem, 0) == stack_pointer_rtx)
2225     ml->sp_offset = 0;
2226   else
2227     ml->sp_offset = INTVAL (XEXP (XEXP (*mem, 0), 1));
2228
2229   ml->insn = insn;
2230   ml->mem = mem;
2231   ml->next = next_memlist;
2232
2233   return ml;
2234 }
2235
2236 /* Attempt to apply ADJUST to the stack adjusting insn INSN, as well
2237    as each of the memories in MEMLIST.  Return true on success.  */
2238
2239 static int
2240 try_apply_stack_adjustment (insn, memlist, new_adjust, delta)
2241      rtx insn;
2242      struct csa_memlist *memlist;
2243      HOST_WIDE_INT new_adjust, delta;
2244 {
2245   struct csa_memlist *ml;
2246   rtx set;
2247
2248   set = single_set_for_csa (insn);
2249   validate_change (insn, &XEXP (SET_SRC (set), 1), GEN_INT (new_adjust), 1);
2250
2251   for (ml = memlist; ml ; ml = ml->next)
2252     validate_change
2253       (ml->insn, ml->mem,
2254        replace_equiv_address_nv (*ml->mem,
2255                                  plus_constant (stack_pointer_rtx,
2256                                                 ml->sp_offset - delta)), 1);
2257
2258   if (apply_change_group ())
2259     {
2260       /* Succeeded.  Update our knowledge of the memory references.  */
2261       for (ml = memlist; ml ; ml = ml->next)
2262         ml->sp_offset -= delta;
2263
2264       return 1;
2265     }
2266   else
2267     return 0;
2268 }
2269
2270 /* Called via for_each_rtx and used to record all stack memory references in
2271    the insn and discard all other stack pointer references.  */
2272 struct record_stack_memrefs_data
2273 {
2274   rtx insn;
2275   struct csa_memlist *memlist;
2276 };
2277
2278 static int
2279 record_stack_memrefs (xp, data)
2280      rtx *xp;
2281      void *data;
2282 {
2283   rtx x = *xp;
2284   struct record_stack_memrefs_data *d =
2285     (struct record_stack_memrefs_data *) data;
2286   if (!x)
2287     return 0;
2288   switch (GET_CODE (x))
2289     {
2290     case MEM:
2291       if (!reg_mentioned_p (stack_pointer_rtx, x))
2292         return -1;
2293       /* We are not able to handle correctly all possible memrefs containing
2294          stack pointer, so this check is neccesary.  */
2295       if (stack_memref_p (x))
2296         {
2297           d->memlist = record_one_stack_memref (d->insn, xp, d->memlist);
2298           return -1;
2299         }
2300       return 1;
2301     case REG:
2302       /* ??? We want be able to handle non-memory stack pointer
2303          references later.  For now just discard all insns refering to
2304          stack pointer outside mem expressions.  We would probably
2305          want to teach validate_replace to simplify expressions first.
2306
2307          We can't just compare with STACK_POINTER_RTX because the
2308          reference to the stack pointer might be in some other mode.
2309          In particular, an explict clobber in an asm statement will
2310          result in a QImode clober.  */
2311       if (REGNO (x) == STACK_POINTER_REGNUM)
2312         return 1;
2313       break;
2314     default:
2315       break;
2316     }
2317   return 0;
2318 }
2319
2320 /* Subroutine of combine_stack_adjustments, called for each basic block.  */
2321
2322 static void
2323 combine_stack_adjustments_for_block (bb)
2324      basic_block bb;
2325 {
2326   HOST_WIDE_INT last_sp_adjust = 0;
2327   rtx last_sp_set = NULL_RTX;
2328   struct csa_memlist *memlist = NULL;
2329   rtx pending_delete;
2330   rtx insn, next;
2331   struct record_stack_memrefs_data data;
2332
2333   for (insn = bb->head; ; insn = next)
2334     {
2335       rtx set;
2336
2337       pending_delete = NULL_RTX;
2338       next = NEXT_INSN (insn);
2339
2340       if (! INSN_P (insn))
2341         goto processed;
2342
2343       set = single_set_for_csa (insn);
2344       if (set)
2345         {
2346           rtx dest = SET_DEST (set);
2347           rtx src = SET_SRC (set);
2348
2349           /* Find constant additions to the stack pointer.  */
2350           if (dest == stack_pointer_rtx
2351               && GET_CODE (src) == PLUS
2352               && XEXP (src, 0) == stack_pointer_rtx
2353               && GET_CODE (XEXP (src, 1)) == CONST_INT)
2354             {
2355               HOST_WIDE_INT this_adjust = INTVAL (XEXP (src, 1));
2356
2357               /* If we've not seen an adjustment previously, record
2358                  it now and continue.  */
2359               if (! last_sp_set)
2360                 {
2361                   last_sp_set = insn;
2362                   last_sp_adjust = this_adjust;
2363                   goto processed;
2364                 }
2365
2366               /* If not all recorded memrefs can be adjusted, or the
2367                  adjustment is now too large for a constant addition,
2368                  we cannot merge the two stack adjustments.
2369
2370                  Also we need to be carefull to not move stack pointer
2371                  such that we create stack accesses outside the allocated
2372                  area.  We can combine an allocation into the first insn,
2373                  or a deallocation into the second insn.  We can not
2374                  combine an allocation followed by a deallocation.
2375
2376                  The only somewhat frequent ocurrence of the later is when
2377                  a function allocates a stack frame but does not use it.
2378                  For this case, we would need to analyze rtl stream to be
2379                  sure that allocated area is really unused.  This means not
2380                  only checking the memory references, but also all registers
2381                  or global memory references possibly containing a stack
2382                  frame address.
2383
2384                  Perhaps the best way to address this problem is to teach
2385                  gcc not to allocate stack for objects never used.  */
2386
2387               /* Combine an allocation into the first instruction.  */
2388               if (STACK_GROWS_DOWNWARD ? this_adjust <= 0 : this_adjust >= 0)
2389                 {
2390                   if (try_apply_stack_adjustment (last_sp_set, memlist,
2391                                                   last_sp_adjust + this_adjust,
2392                                                   this_adjust))
2393                     {
2394                       /* It worked!  */
2395                       pending_delete = insn;
2396                       last_sp_adjust += this_adjust;
2397                       goto processed;
2398                     }
2399                 }
2400
2401               /* Otherwise we have a deallocation.  Do not combine with
2402                  a previous allocation.  Combine into the second insn.  */
2403               else if (STACK_GROWS_DOWNWARD
2404                        ? last_sp_adjust >= 0 : last_sp_adjust <= 0)
2405                 {
2406                   if (try_apply_stack_adjustment (insn, memlist,
2407                                                   last_sp_adjust + this_adjust,
2408                                                   -last_sp_adjust))
2409                     {
2410                       /* It worked!  */
2411                       flow_delete_insn (last_sp_set);
2412                       last_sp_set = insn;
2413                       last_sp_adjust += this_adjust;
2414                       free_csa_memlist (memlist);
2415                       memlist = NULL;
2416                       goto processed;
2417                     }
2418                 }
2419
2420               /* Combination failed.  Restart processing from here.  */
2421               free_csa_memlist (memlist);
2422               memlist = NULL;
2423               last_sp_set = insn;
2424               last_sp_adjust = this_adjust;
2425               goto processed;
2426             }
2427
2428           /* Find a predecrement of exactly the previous adjustment and
2429              turn it into a direct store.  Obviously we can't do this if
2430              there were any intervening uses of the stack pointer.  */
2431           if (memlist == NULL
2432               && GET_CODE (dest) == MEM
2433               && ((GET_CODE (XEXP (dest, 0)) == PRE_DEC
2434                    && (last_sp_adjust
2435                        == (HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (dest))))
2436                   || (GET_CODE (XEXP (dest, 0)) == PRE_MODIFY
2437                       && GET_CODE (XEXP (XEXP (dest, 0), 1)) == PLUS
2438                       && XEXP (XEXP (XEXP (dest, 0), 1), 0) == stack_pointer_rtx
2439                       && (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2440                           == CONST_INT)
2441                       && (INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2442                           == -last_sp_adjust)))
2443               && XEXP (XEXP (dest, 0), 0) == stack_pointer_rtx
2444               && ! reg_mentioned_p (stack_pointer_rtx, src)
2445               && memory_address_p (GET_MODE (dest), stack_pointer_rtx)
2446               && validate_change (insn, &SET_DEST (set),
2447                                   replace_equiv_address (dest,
2448                                                          stack_pointer_rtx),
2449                                   0))
2450             {
2451               if (last_sp_set == bb->head)
2452                 bb->head = NEXT_INSN (last_sp_set);
2453               flow_delete_insn (last_sp_set);
2454
2455               free_csa_memlist (memlist);
2456               memlist = NULL;
2457               last_sp_set = NULL_RTX;
2458               last_sp_adjust = 0;
2459               goto processed;
2460             }
2461         }
2462
2463       data.insn = insn;
2464       data.memlist = memlist;
2465       if (GET_CODE (insn) != CALL_INSN && last_sp_set
2466           && !for_each_rtx (&PATTERN (insn), record_stack_memrefs, &data))
2467         {
2468            memlist = data.memlist;
2469            goto processed;
2470         }
2471       memlist = data.memlist;
2472
2473       /* Otherwise, we were not able to process the instruction.
2474          Do not continue collecting data across such a one.  */
2475       if (last_sp_set
2476           && (GET_CODE (insn) == CALL_INSN
2477               || reg_mentioned_p (stack_pointer_rtx, PATTERN (insn))))
2478         {
2479           free_csa_memlist (memlist);
2480           memlist = NULL;
2481           last_sp_set = NULL_RTX;
2482           last_sp_adjust = 0;
2483         }
2484
2485     processed:
2486       if (insn == bb->end)
2487         break;
2488
2489       if (pending_delete)
2490         flow_delete_insn (pending_delete);
2491     }
2492
2493   if (pending_delete)
2494     {
2495       bb->end = PREV_INSN (pending_delete);
2496       flow_delete_insn (pending_delete);
2497     }
2498 }