OSDN Git Service

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