OSDN Git Service

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